8134759: jdb: Incorrect stepping inside finally block

Tue, 27 Oct 2015 10:35:14 +0100

author
aeriksso
date
Tue, 27 Oct 2015 10:35:14 +0100
changeset 3000
4044eb07194d
parent 2999
683b3e7e05a7
child 3001
dcd12fa5b58a

8134759: jdb: Incorrect stepping inside finally block
Summary: Add LineNumberTable attribute for return bytecodes split around finally code
Reviewed-by: mcimadamore

src/share/classes/com/sun/tools/javac/jvm/Gen.java file | annotate | diff | comparison | revisions
test/tools/javac/linenumbers/FinallyLineNumberTest.java file | annotate | diff | comparison | revisions
     1.1 --- a/src/share/classes/com/sun/tools/javac/jvm/Gen.java	Mon Oct 26 13:23:30 2015 -0700
     1.2 +++ b/src/share/classes/com/sun/tools/javac/jvm/Gen.java	Tue Oct 27 10:35:14 2015 +0100
     1.3 @@ -1793,6 +1793,11 @@
     1.4      public void visitReturn(JCReturn tree) {
     1.5          int limit = code.nextreg;
     1.6          final Env<GenContext> targetEnv;
     1.7 +
     1.8 +        /* Save and then restore the location of the return in case a finally
     1.9 +         * is expanded (with unwind()) in the middle of our bytecodes.
    1.10 +         */
    1.11 +        int tmpPos = code.pendingStatPos;
    1.12          if (tree.expr != null) {
    1.13              Item r = genExpr(tree.expr, pt).load();
    1.14              if (hasFinally(env.enclMethod, env)) {
    1.15 @@ -1800,17 +1805,10 @@
    1.16                  r.store();
    1.17              }
    1.18              targetEnv = unwind(env.enclMethod, env);
    1.19 +            code.pendingStatPos = tmpPos;
    1.20              r.load();
    1.21              code.emitop0(ireturn + Code.truncate(Code.typecode(pt)));
    1.22          } else {
    1.23 -            /*  If we have a statement like:
    1.24 -             *
    1.25 -             *  return;
    1.26 -             *
    1.27 -             *  we need to store the code.pendingStatPos value before generating
    1.28 -             *  the finalizer.
    1.29 -             */
    1.30 -            int tmpPos = code.pendingStatPos;
    1.31              targetEnv = unwind(env.enclMethod, env);
    1.32              code.pendingStatPos = tmpPos;
    1.33              code.emitop0(return_);
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/test/tools/javac/linenumbers/FinallyLineNumberTest.java	Tue Oct 27 10:35:14 2015 +0100
     2.3 @@ -0,0 +1,108 @@
     2.4 +/*
     2.5 + * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
     2.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     2.7 + *
     2.8 + * This code is free software; you can redistribute it and/or modify it
     2.9 + * under the terms of the GNU General Public License version 2 only, as
    2.10 + * published by the Free Software Foundation.
    2.11 + *
    2.12 + * This code is distributed in the hope that it will be useful, but WITHOUT
    2.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    2.14 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    2.15 + * version 2 for more details (a copy is included in the LICENSE file that
    2.16 + * accompanied this code).
    2.17 + *
    2.18 + * You should have received a copy of the GNU General Public License version
    2.19 + * 2 along with this work; if not, write to the Free Software Foundation,
    2.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    2.21 + *
    2.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    2.23 + * or visit www.oracle.com if you need additional information or have any
    2.24 + * questions.
    2.25 + */
    2.26 +
    2.27 +/*
    2.28 + * @test
    2.29 + * @bug 8134759
    2.30 + * @summary Add LineNumberTable attribute for return bytecodes split around finally code
    2.31 + * @modules jdk.jdeps/com.sun.tools.classfile
    2.32 + */
    2.33 +
    2.34 +import com.sun.tools.classfile.ClassFile;
    2.35 +import com.sun.tools.classfile.ConstantPoolException;
    2.36 +import com.sun.tools.classfile.Method;
    2.37 +import com.sun.tools.classfile.Attribute;
    2.38 +import com.sun.tools.classfile.Code_attribute;
    2.39 +import com.sun.tools.classfile.LineNumberTable_attribute;
    2.40 +import com.sun.tools.classfile.LineNumberTable_attribute.Entry;
    2.41 +
    2.42 +import java.io.File;
    2.43 +import java.io.IOException;
    2.44 +
    2.45 +public class FinallyLineNumberTest {
    2.46 +    public static void main(String[] args) throws Exception {
    2.47 +        // check that we have 5 consecutive entries for method()
    2.48 +        Entry[] lines = findEntries();
    2.49 +        if (lines == null) {
    2.50 +            throw new Exception("finally line number table could not be loaded");
    2.51 +        }
    2.52 +        if (lines.length != 4) {
    2.53 +            // Help debug
    2.54 +            System.err.println("LineTable error, got lines:");
    2.55 +            for (Entry e : lines) {
    2.56 +                System.err.println(e.line_number);
    2.57 +            }
    2.58 +            throw new Exception("finally line number table incorrect: length=" + lines.length + " expected length=4");
    2.59 +        }
    2.60 +
    2.61 +        // return null line, for the load null operation
    2.62 +        int current = lines[0].line_number;
    2.63 +        int first = current;
    2.64 +
    2.65 +        // finally line
    2.66 +        current = lines[1].line_number;
    2.67 +        if (current != first + 2) {
    2.68 +            throw new Exception("finally line number table incorrect: got=" + current + " expected=" + (first + 2));
    2.69 +        }
    2.70 +
    2.71 +        // return null line, for the return operation
    2.72 +        current = lines[2].line_number;
    2.73 +        if (current != first) {
    2.74 +            throw new Exception("finally line number table incorrect: got=" + current + " expected=" + first);
    2.75 +        }
    2.76 +
    2.77 +        // finally line, for when exception is thrown
    2.78 +        current = lines[3].line_number;
    2.79 +        if (current != first + 2) {
    2.80 +            throw new Exception("finally line number table incorrect: got=" + current + " expected=" + (first + 2));
    2.81 +        }
    2.82 +    }
    2.83 +
    2.84 +    static Entry[] findEntries() throws IOException, ConstantPoolException {
    2.85 +        ClassFile self = ClassFile.read(FinallyLineNumberTest.class.getResourceAsStream("FinallyLineNumberTest.class"));
    2.86 +        for (Method m : self.methods) {
    2.87 +            if ("method".equals(m.getName(self.constant_pool))) {
    2.88 +                Code_attribute code_attribute = (Code_attribute)m.attributes.get(Attribute.Code);
    2.89 +                for (Attribute at : code_attribute.attributes) {
    2.90 +                    if (Attribute.LineNumberTable.equals(at.getName(self.constant_pool))) {
    2.91 +                        return ((LineNumberTable_attribute)at).line_number_table;
    2.92 +                    }
    2.93 +                }
    2.94 +            }
    2.95 +        }
    2.96 +        return null;
    2.97 +    }
    2.98 +
    2.99 +    // This method should get LineNumberTable entries for:
   2.100 +    // *) The load of the null
   2.101 +    // *) The finally code for when an exception is *not* thrown
   2.102 +    // *) The actual return, which should have the same line as the load of the null
   2.103 +    // *) The finally code for when an exception *is* thrown, should have the same line as above finally code
   2.104 +    public static String method(int field) {
   2.105 +        try {
   2.106 +            return null;
   2.107 +        } finally {
   2.108 +            field+=1; // Dummy
   2.109 +        }
   2.110 +    }
   2.111 +}

mercurial