Wed, 07 Jun 2017 00:04:12 -0700
8180660: missing LNT entry for finally block
Reviewed-by: mcimadamore, vromero
Contributed-by: maurizio.cimadamore@oracle.com, vicente.romero@oracle.com
1.1 --- a/src/share/classes/com/sun/tools/javac/jvm/Gen.java Tue May 23 08:07:26 2017 -0700 1.2 +++ b/src/share/classes/com/sun/tools/javac/jvm/Gen.java Wed Jun 07 00:04:12 2017 -0700 1.3 @@ -27,6 +27,7 @@ 1.4 1.5 import java.util.*; 1.6 1.7 +import com.sun.tools.javac.tree.TreeInfo.PosKind; 1.8 import com.sun.tools.javac.util.*; 1.9 import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition; 1.10 import com.sun.tools.javac.util.List; 1.11 @@ -1531,12 +1532,16 @@ 1.12 catchallpc, 0); 1.13 startseg = env.info.gaps.next().intValue(); 1.14 } 1.15 - code.statBegin(TreeInfo.finalizerPos(env.tree)); 1.16 + code.statBegin(TreeInfo.finalizerPos(env.tree, PosKind.FIRST_STAT_POS)); 1.17 code.markStatBegin(); 1.18 1.19 Item excVar = makeTemp(syms.throwableType); 1.20 excVar.store(); 1.21 genFinalizer(env); 1.22 + code.resolvePending(); 1.23 + code.statBegin(TreeInfo.finalizerPos(env.tree, PosKind.END_POS)); 1.24 + code.markStatBegin(); 1.25 + 1.26 excVar.load(); 1.27 registerCatch(body.pos(), startseg, 1.28 env.info.gaps.next().intValue(), 1.29 @@ -1550,7 +1555,7 @@ 1.30 code.resolve(env.info.cont); 1.31 1.32 // Mark statement line number 1.33 - code.statBegin(TreeInfo.finalizerPos(env.tree)); 1.34 + code.statBegin(TreeInfo.finalizerPos(env.tree, PosKind.FIRST_STAT_POS)); 1.35 code.markStatBegin(); 1.36 1.37 // Save return address.
2.1 --- a/src/share/classes/com/sun/tools/javac/tree/TreeInfo.java Tue May 23 08:07:26 2017 -0700 2.2 +++ b/src/share/classes/com/sun/tools/javac/tree/TreeInfo.java Wed Jun 07 00:04:12 2017 -0700 2.3 @@ -1,5 +1,5 @@ 2.4 /* 2.5 - * Copyright (c) 1999, 2014, Oracle and/or its affiliates. All rights reserved. 2.6 + * Copyright (c) 1999, 2017, Oracle and/or its affiliates. All rights reserved. 2.7 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 2.8 * 2.9 * This code is free software; you can redistribute it and/or modify it 2.10 @@ -35,6 +35,7 @@ 2.11 import com.sun.tools.javac.tree.JCTree.*; 2.12 import com.sun.tools.javac.tree.JCTree.JCPolyExpression.*; 2.13 import com.sun.tools.javac.util.*; 2.14 + 2.15 import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition; 2.16 import static com.sun.tools.javac.code.Flags.*; 2.17 import static com.sun.tools.javac.code.TypeTag.BOT; 2.18 @@ -611,13 +612,21 @@ 2.19 }; 2.20 } 2.21 2.22 + public enum PosKind { 2.23 + START_POS() { int toPos(JCTree tree) { return TreeInfo.getStartPos(tree); } }, 2.24 + FIRST_STAT_POS() { int toPos(JCTree tree) { return firstStatPos(tree); } }, 2.25 + END_POS() { int toPos(JCTree tree) { return endPos(tree); } }; 2.26 + 2.27 + abstract int toPos(JCTree tree); 2.28 + } 2.29 + 2.30 /** The position of the finalizer of given try/synchronized statement. 2.31 */ 2.32 - public static int finalizerPos(JCTree tree) { 2.33 + public static int finalizerPos(JCTree tree, PosKind posKind) { 2.34 if (tree.hasTag(TRY)) { 2.35 JCTry t = (JCTry) tree; 2.36 Assert.checkNonNull(t.finalizer); 2.37 - return firstStatPos(t.finalizer); 2.38 + return posKind.toPos(t.finalizer); 2.39 } else if (tree.hasTag(SYNCHRONIZED)) { 2.40 return endPos(((JCSynchronized) tree).body); 2.41 } else {
3.1 --- a/test/tools/javac/T7008643/InlinedFinallyConfuseDebuggersTest.java Tue May 23 08:07:26 2017 -0700 3.2 +++ b/test/tools/javac/T7008643/InlinedFinallyConfuseDebuggersTest.java Wed Jun 07 00:04:12 2017 -0700 3.3 @@ -1,5 +1,5 @@ 3.4 /* 3.5 - * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved. 3.6 + * Copyright (c) 2013, 2017, Oracle and/or its affiliates. All rights reserved. 3.7 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 3.8 * 3.9 * This code is free software; you can redistribute it and/or modify it 3.10 @@ -64,6 +64,7 @@ 3.11 {9, 21}, //System.out.println("finally"); 3.12 {10, 29}, 3.13 {9, 32}, //System.out.println("finally"); 3.14 + {10, 41}, //} 3.15 {11, 43}, 3.16 }; 3.17
4.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 4.2 +++ b/test/tools/javac/T8180660/MissingLNTEntryForFinalizerTest.java Wed Jun 07 00:04:12 2017 -0700 4.3 @@ -0,0 +1,170 @@ 4.4 +/* 4.5 + * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved. 4.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4.7 + * 4.8 + * This code is free software; you can redistribute it and/or modify it 4.9 + * under the terms of the GNU General Public License version 2 only, as 4.10 + * published by the Free Software Foundation. 4.11 + * 4.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 4.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 4.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 4.15 + * version 2 for more details (a copy is included in the LICENSE file that 4.16 + * accompanied this code). 4.17 + * 4.18 + * You should have received a copy of the GNU General Public License version 4.19 + * 2 along with this work; if not, write to the Free Software Foundation, 4.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 4.21 + * 4.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 4.23 + * or visit www.oracle.com if you need additional information or have any 4.24 + * questions. 4.25 + */ 4.26 + 4.27 +/* 4.28 + * @test 4.29 + * @bug 8180141 4.30 + * @summary Missing entry in LineNumberTable for break statement that jumps out of try-finally 4.31 + * @compile -g MissingLNTEntryForFinalizerTest.java 4.32 + * @run main MissingLNTEntryForFinalizerTest 4.33 + */ 4.34 + 4.35 +import java.io.File; 4.36 +import java.net.URI; 4.37 + 4.38 +import javax.tools.JavaFileObject; 4.39 +import javax.tools.SimpleJavaFileObject; 4.40 + 4.41 +import com.sun.tools.classfile.*; 4.42 +import com.sun.tools.javac.comp.Attr; 4.43 +import com.sun.tools.javac.comp.AttrContext; 4.44 +import com.sun.tools.javac.comp.Env; 4.45 +import com.sun.tools.javac.file.JavacFileManager; 4.46 +import com.sun.tools.javac.main.JavaCompiler; 4.47 +import com.sun.tools.javac.tree.JCTree; 4.48 +import com.sun.tools.javac.tree.JCTree.*; 4.49 +import com.sun.tools.javac.util.Context; 4.50 +import com.sun.tools.javac.util.List; 4.51 + 4.52 +import static com.sun.tools.javac.util.List.of; 4.53 +import static com.sun.tools.javac.tree.JCTree.Tag.*; 4.54 + 4.55 +public class MissingLNTEntryForFinalizerTest { 4.56 + protected ReusableJavaCompiler tool; 4.57 + Context context; 4.58 + 4.59 + MissingLNTEntryForFinalizerTest() { 4.60 + context = new Context(); 4.61 + JavacFileManager.preRegister(context); 4.62 + MyAttr.preRegister(context); 4.63 + tool = new ReusableJavaCompiler(context); 4.64 + } 4.65 + 4.66 + public static void main(String... args) throws Throwable { 4.67 + new MissingLNTEntryForFinalizerTest().test(); 4.68 + } 4.69 + 4.70 + void test() throws Throwable { 4.71 + JavaSource source = new JavaSource("1"); 4.72 + tool.clear(); 4.73 + List<JavaFileObject> inputs = of(source); 4.74 + try { 4.75 + tool.compile(inputs); 4.76 + } catch (Throwable ex) { 4.77 + throw new AssertionError(ex); 4.78 + } 4.79 + File testClasses = new File("."); 4.80 + File file = new File(testClasses, "Test1.class"); 4.81 + ClassFile classFile = ClassFile.read(file); 4.82 + for (Method m : classFile.methods) { 4.83 + if (classFile.constant_pool.getUTF8Value(m.name_index).equals("foo")) { 4.84 + Code_attribute code = (Code_attribute)m.attributes.get(Attribute.Code); 4.85 + LineNumberTable_attribute lnt = (LineNumberTable_attribute)code.attributes.get(Attribute.LineNumberTable); 4.86 + checkLNT(lnt, MyAttr.lineNumber); 4.87 + } 4.88 + } 4.89 + } 4.90 + 4.91 + void checkLNT(LineNumberTable_attribute lnt, int lineToCheckFor) { 4.92 + for (LineNumberTable_attribute.Entry e: lnt.line_number_table) { 4.93 + if (e.line_number == lineToCheckFor) { 4.94 + return; 4.95 + } 4.96 + } 4.97 + throw new AssertionError("seek line number not found in the LNT for method foo()"); 4.98 + } 4.99 + 4.100 + class JavaSource extends SimpleJavaFileObject { 4.101 + String id; 4.102 + String template = 4.103 + "import java.util.*;\n" + 4.104 + "class Test#Id {\n" + 4.105 + " void foo() {\n" + 4.106 + " List<String> l = null;\n" + 4.107 + " String first = null;\n" + 4.108 + " try {\n" + 4.109 + " first = l.get(0);\n" + 4.110 + " } finally {\n" + 4.111 + " if (first != null) {\n" + 4.112 + " System.out.println(\"finalizer\");\n" + 4.113 + " }\n" + 4.114 + " }\n" + 4.115 + " }\n" + 4.116 + "}"; 4.117 + 4.118 + JavaSource(String id) { 4.119 + super(URI.create("myfo:/Test.java"), JavaFileObject.Kind.SOURCE); 4.120 + this.id = id; 4.121 + } 4.122 + 4.123 + @Override 4.124 + public CharSequence getCharContent(boolean ignoreEncodingErrors) { 4.125 + return template.replace("#Id", id); 4.126 + } 4.127 + } 4.128 + 4.129 + /* this class has been set up to do not depend on a fixed line number, this Attr subclass will 4.130 + * look for 'break' or 'continue' statements in order to find the actual line number they occupy. 4.131 + * This way the test can find if that line number appears in the LNT generated for a given class. 4.132 + */ 4.133 + static class MyAttr extends Attr { 4.134 + static int lineNumber; 4.135 + 4.136 + static void preRegister(Context context) { 4.137 + context.put(attrKey, (com.sun.tools.javac.util.Context.Factory<Attr>) c -> new MyAttr(c)); 4.138 + } 4.139 + 4.140 + MyAttr(Context context) { 4.141 + super(context); 4.142 + } 4.143 + 4.144 + @Override 4.145 + public com.sun.tools.javac.code.Type attribStat(JCTree tree, Env<AttrContext> env) { 4.146 + com.sun.tools.javac.code.Type result = super.attribStat(tree, env); 4.147 + if (tree.hasTag(TRY)) { 4.148 + JCTry tryTree = (JCTry)tree; 4.149 + lineNumber = env.toplevel.lineMap.getLineNumber(tryTree.finalizer.endpos); 4.150 + } 4.151 + return result; 4.152 + } 4.153 + } 4.154 + 4.155 + static class ReusableJavaCompiler extends JavaCompiler { 4.156 + ReusableJavaCompiler(Context context) { 4.157 + super(context); 4.158 + } 4.159 + 4.160 + protected void checkReusable() { 4.161 + // do nothing 4.162 + } 4.163 + 4.164 + @Override 4.165 + public void close() { 4.166 + //do nothing 4.167 + } 4.168 + 4.169 + void clear() { 4.170 + //do nothing 4.171 + } 4.172 + } 4.173 +}
5.1 --- a/test/tools/javac/linenumbers/FinallyLineNumberTest.java Tue May 23 08:07:26 2017 -0700 5.2 +++ b/test/tools/javac/linenumbers/FinallyLineNumberTest.java Wed Jun 07 00:04:12 2017 -0700 5.3 @@ -1,5 +1,5 @@ 5.4 /* 5.5 - * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved. 5.6 + * Copyright (c) 2015, 2017, Oracle and/or its affiliates. All rights reserved. 5.7 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 5.8 * 5.9 * This code is free software; you can redistribute it and/or modify it 5.10 @@ -36,7 +36,6 @@ 5.11 import com.sun.tools.classfile.LineNumberTable_attribute; 5.12 import com.sun.tools.classfile.LineNumberTable_attribute.Entry; 5.13 5.14 -import java.io.File; 5.15 import java.io.IOException; 5.16 5.17 public class FinallyLineNumberTest { 5.18 @@ -46,13 +45,13 @@ 5.19 if (lines == null) { 5.20 throw new Exception("finally line number table could not be loaded"); 5.21 } 5.22 - if (lines.length != 4) { 5.23 + if (lines.length != 5) { 5.24 // Help debug 5.25 System.err.println("LineTable error, got lines:"); 5.26 for (Entry e : lines) { 5.27 System.err.println(e.line_number); 5.28 } 5.29 - throw new Exception("finally line number table incorrect: length=" + lines.length + " expected length=4"); 5.30 + throw new Exception("finally line number table incorrect: length=" + lines.length + " expected length=5"); 5.31 } 5.32 5.33 // return null line, for the load null operation 5.34 @@ -71,11 +70,17 @@ 5.35 throw new Exception("finally line number table incorrect: got=" + current + " expected=" + first); 5.36 } 5.37 5.38 - // finally line, for when exception is thrown 5.39 + // for when exception is thrown 5.40 current = lines[3].line_number; 5.41 if (current != first + 2) { 5.42 throw new Exception("finally line number table incorrect: got=" + current + " expected=" + (first + 2)); 5.43 } 5.44 + 5.45 + // the '}' closing the finally block 5.46 + current = lines[4].line_number; 5.47 + if (current != first + 3) { 5.48 + throw new Exception("finally line number table incorrect: got=" + current + " expected=" + (first + 3)); 5.49 + } 5.50 } 5.51 5.52 static Entry[] findEntries() throws IOException, ConstantPoolException {