8198378: javac crash when checking imports and a broken class is present
authorjlahoda
Mon, 04 Jun 2018 12:54:13 +0200
changeset 50374 2d0647b9ac18
parent 50373 22f611c395b3
child 50375 bfbe7d8369bb
8198378: javac crash when checking imports and a broken class is present Summary: Properly handling bad classfile while processing imports. Reviewed-by: vromero
src/jdk.compiler/share/classes/com/sun/tools/javac/code/Scope.java
src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Resolve.java
src/jdk.compiler/share/classes/com/sun/tools/javac/comp/TypeEnter.java
test/langtools/tools/javac/6330997/T6330997.java
test/langtools/tools/javac/T6435291/T6435291.java
test/langtools/tools/javac/api/6400303/T6400303.java
test/langtools/tools/javac/classreader/8171132/BadConstantValue.java
test/langtools/tools/javac/importscope/BadClassFileDuringImport.java
--- a/src/jdk.compiler/share/classes/com/sun/tools/javac/code/Scope.java	Mon Jun 04 12:51:24 2018 +0200
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/code/Scope.java	Mon Jun 04 12:54:13 2018 +0200
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 1999, 2016, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1999, 2018, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -746,7 +746,8 @@
         }
 
         protected Scope finalizeSingleScope(Scope impScope) {
-            if (impScope instanceof FilterImportScope && impScope.owner.kind == Kind.TYP) {
+            if (impScope instanceof FilterImportScope && impScope.owner.kind == Kind.TYP &&
+                ((FilterImportScope) impScope).isStaticallyImported()) {
                 WriteableScope finalized = WriteableScope.create(impScope.owner);
 
                 for (Symbol sym : impScope.getSymbols()) {
@@ -973,6 +974,10 @@
 
         @Override
         public boolean isStaticallyImported(Symbol byName) {
+            return isStaticallyImported();
+        }
+
+        public boolean isStaticallyImported() {
             return imp.staticImport;
         }
 
--- a/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Resolve.java	Mon Jun 04 12:51:24 2018 +0200
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Resolve.java	Mon Jun 04 12:54:13 2018 +0200
@@ -1978,7 +1978,7 @@
             ClassSymbol c = finder.loadClass(env.toplevel.modle, name);
             return isAccessible(env, c) ? c : new AccessError(env, null, c);
         } catch (ClassFinder.BadClassFile err) {
-            throw err;
+            return new BadClassFileError(err);
         } catch (CompletionFailure ex) {
             Symbol candidate = recoveryLoadClass.loadClass(env, name);
 
@@ -4499,6 +4499,27 @@
         }
     }
 
+    class BadClassFileError extends InvalidSymbolError {
+
+        private final CompletionFailure ex;
+
+        public BadClassFileError(CompletionFailure ex) {
+            super(HIDDEN, ex.sym, "BadClassFileError");
+            this.name = sym.name;
+            this.ex = ex;
+        }
+
+        @Override
+        JCDiagnostic getDiagnostic(DiagnosticType dkind, DiagnosticPosition pos, Symbol location, Type site, Name name, List<Type> argtypes, List<Type> typeargtypes) {
+            JCDiagnostic d = diags.create(dkind, log.currentSource(), pos,
+                "cant.access", ex.sym, ex.getDetailValue());
+
+            d.setFlag(DiagnosticFlag.NON_DEFERRABLE);
+            return d;
+        }
+
+    }
+
     /**
      * Helper class for method resolution diagnostic simplification.
      * Certain resolution diagnostic are rewritten as simpler diagnostic
--- a/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/TypeEnter.java	Mon Jun 04 12:51:24 2018 +0200
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/TypeEnter.java	Mon Jun 04 12:54:13 2018 +0200
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2003, 2017, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2003, 2018, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -223,6 +223,8 @@
             chk.checkImportedPackagesObservable(toplevel);
             toplevel.namedImportScope.finalizeScope();
             toplevel.starImportScope.finalizeScope();
+        } catch (CompletionFailure cf) {
+            chk.completionError(toplevel.pos(), cf);
         } finally {
             log.useSource(prev);
         }
--- a/test/langtools/tools/javac/6330997/T6330997.java	Mon Jun 04 12:51:24 2018 +0200
+++ b/test/langtools/tools/javac/6330997/T6330997.java	Mon Jun 04 12:54:13 2018 +0200
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2006, 2017, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2006, 2018, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -44,7 +44,7 @@
 import com.sun.tools.javac.api.JavacTaskImpl;
 import com.sun.tools.javac.code.ClassFinder.BadClassFile;
 import com.sun.tools.javac.code.Symtab;
-import com.sun.tools.javac.main.JavaCompiler;
+import com.sun.tools.javac.util.Names;
 import javax.tools.ToolProvider;
 
 public class T6330997 {
@@ -53,17 +53,17 @@
         increaseMajor("T2.class", 2);
         javax.tools.JavaCompiler tool = ToolProvider.getSystemJavaCompiler();
         JavacTaskImpl task = (JavacTaskImpl)tool.getTask(null, null, null, null, null, null);
-        JavaCompiler compiler = JavaCompiler.instance(task.getContext());
         Symtab syms = Symtab.instance(task.getContext());
+        Names names = Names.instance(task.getContext());
         task.ensureEntered();
         try {
-            compiler.resolveIdent(syms.unnamedModule, "T1").complete();
+            syms.enterClass(syms.unnamedModule, names.fromString("T1")).complete();
         } catch (Exception e) {
             e.printStackTrace();
             throw new RuntimeException("Failed: unexpected exception while reading class T1");
         }
         try {
-            compiler.resolveIdent(syms.unnamedModule, "T2").complete();
+            syms.enterClass(syms.unnamedModule, names.fromString("T2")).complete();
         } catch (BadClassFile e) {
             System.err.println("Passed: expected completion failure " + e.getClass().getName());
             return;
--- a/test/langtools/tools/javac/T6435291/T6435291.java	Mon Jun 04 12:51:24 2018 +0200
+++ b/test/langtools/tools/javac/T6435291/T6435291.java	Mon Jun 04 12:54:13 2018 +0200
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2006, 2016, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2006, 2018, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -38,7 +38,7 @@
 import com.sun.tools.javac.api.JavacTaskImpl;
 import com.sun.tools.javac.code.ClassFinder.BadClassFile;
 import com.sun.tools.javac.code.Symtab;
-import com.sun.tools.javac.main.JavaCompiler;
+import com.sun.tools.javac.util.Names;
 import javax.tools.ToolProvider;
 
 public class T6435291 {
@@ -46,10 +46,10 @@
         javax.tools.JavaCompiler tool = ToolProvider.getSystemJavaCompiler();
         JavacTaskImpl task = (JavacTaskImpl)tool.getTask(null, null, null, null, null, null);
         Symtab syms = Symtab.instance(task.getContext());
+        Names names = Names.instance(task.getContext());
         task.ensureEntered();
-        JavaCompiler compiler = JavaCompiler.instance(task.getContext());
         try {
-            compiler.resolveIdent(syms.unnamedModule, "T").complete();
+            syms.enterClass(syms.unnamedModule, names.fromString("T")).complete();
         } catch (BadClassFile e) {
             System.err.println("Passed: expected completion failure " + e.getClass().getName());
             return;
--- a/test/langtools/tools/javac/api/6400303/T6400303.java	Mon Jun 04 12:51:24 2018 +0200
+++ b/test/langtools/tools/javac/api/6400303/T6400303.java	Mon Jun 04 12:54:13 2018 +0200
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2006, 2016, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2006, 2018, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -41,17 +41,17 @@
 import com.sun.tools.javac.api.JavacTaskImpl;
 import com.sun.tools.javac.code.Symbol.CompletionFailure;
 import com.sun.tools.javac.code.Symtab;
-import com.sun.tools.javac.main.JavaCompiler;
+import com.sun.tools.javac.util.Names;
 
 public class T6400303 {
     public static void main(String... args) {
         javax.tools.JavaCompiler tool = ToolProvider.getSystemJavaCompiler();
         JavacTaskImpl task = (JavacTaskImpl)tool.getTask(null, null, null, null, null, null);
+        Names names = Names.instance(task.getContext());
         Symtab syms = Symtab.instance(task.getContext());
         task.ensureEntered();
-        JavaCompiler compiler = JavaCompiler.instance(task.getContext());
         try {
-            compiler.resolveIdent(syms.unnamedModule, "Test$1").complete();
+            syms.enterClass(syms.unnamedModule, names.fromString("Test$1")).complete();
         } catch (CompletionFailure ex) {
             System.err.println("Got expected completion failure: " + ex.getLocalizedMessage());
             return;
--- a/test/langtools/tools/javac/classreader/8171132/BadConstantValue.java	Mon Jun 04 12:51:24 2018 +0200
+++ b/test/langtools/tools/javac/classreader/8171132/BadConstantValue.java	Mon Jun 04 12:54:13 2018 +0200
@@ -37,11 +37,8 @@
  * @run main BadConstantValue
  */
 
-import com.sun.tools.classfile.Attribute;
 import com.sun.tools.classfile.ClassFile;
 import com.sun.tools.classfile.ClassWriter;
-import com.sun.tools.classfile.ConstantPool.CONSTANT_Integer_info;
-import com.sun.tools.classfile.ConstantValue_attribute;
 import com.sun.tools.classfile.Field;
 import com.sun.tools.javac.api.JavacTaskImpl;
 import com.sun.tools.javac.code.ClassFinder.BadClassFile;
@@ -49,6 +46,7 @@
 import com.sun.tools.javac.jvm.Target;
 import com.sun.tools.javac.util.Assert;
 import com.sun.tools.javac.util.JCDiagnostic;
+import com.sun.tools.javac.util.Names;
 import java.io.File;
 import java.io.FileWriter;
 import java.io.IOException;
@@ -169,12 +167,11 @@
         JavaCompiler c = ToolProvider.getSystemJavaCompiler();
         JavacTaskImpl task = (JavacTaskImpl) c.getTask(null, null, null,
                 Arrays.asList("-classpath", classesdir.getPath()), null, null);
+        Names names = Names.instance(task.getContext());
         Symtab syms = Symtab.instance(task.getContext());
         task.ensureEntered();
-        BadClassFile badClassFile;
         try {
-            com.sun.tools.javac.main.JavaCompiler.instance(task.getContext())
-                    .resolveIdent(syms.unnamedModule, className).complete();
+            syms.enterClass(syms.unnamedModule, names.fromString(className)).complete();
         } catch (BadClassFile e) {
             return e;
         }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/langtools/tools/javac/importscope/BadClassFileDuringImport.java	Mon Jun 04 12:54:13 2018 +0200
@@ -0,0 +1,157 @@
+/*
+ * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+/**
+ * @test
+ * @bug 8198378
+ * @summary Verify that BadClassFile related to imports are handled properly.
+ * @library /tools/lib
+ * @modules jdk.compiler/com.sun.tools.javac.api
+ *          jdk.compiler/com.sun.tools.javac.main
+ * @build toolbox.ToolBox toolbox.JavacTask
+ * @run main BadClassFileDuringImport
+ */
+
+import java.io.OutputStream;
+import java.nio.file.Files;
+import java.nio.file.Paths;
+import java.util.Arrays;
+import java.util.List;
+
+import toolbox.JavacTask;
+import toolbox.Task;
+import toolbox.ToolBox;
+
+public class BadClassFileDuringImport {
+    public static void main(String... args) throws Exception {
+        new BadClassFileDuringImport().run();
+    }
+
+    ToolBox tb = new ToolBox();
+
+    void run() throws Exception {
+        new JavacTask(tb)
+          .outdir(".")
+          .sources("package p; public class A { }",
+                   "package p; public class B { public static class I { } }")
+          .run()
+          .writeAll();
+
+        try (OutputStream out = Files.newOutputStream(Paths.get(".", "p", "A.class"))) {
+            out.write("broken".getBytes("UTF-8"));
+        }
+
+        try (OutputStream out = Files.newOutputStream(Paths.get(".", "p", "B$I.class"))) {
+            out.write("broken".getBytes("UTF-8"));
+        }
+
+        doTest("import p.A;",
+               "",
+               "Test.java:2:9: compiler.err.cant.access: p.A, (compiler.misc.bad.class.file.header: A.class, (compiler.misc.illegal.start.of.class.file))",
+               "1 error");
+        doTest("import p.A;",
+               "A a;",
+               "Test.java:2:9: compiler.err.cant.access: p.A, (compiler.misc.bad.class.file.header: A.class, (compiler.misc.illegal.start.of.class.file))",
+               "Test.java:2:33: compiler.err.cant.resolve.location: kindname.class, A, , , (compiler.misc.location: kindname.class, Test, null)",
+               "2 errors");
+        doTest("import p.A;",
+               "void test() { A a; }",
+               "Test.java:2:9: compiler.err.cant.access: p.A, (compiler.misc.bad.class.file.header: A.class, (compiler.misc.illegal.start.of.class.file))",
+               "Test.java:2:47: compiler.err.cant.resolve.location: kindname.class, A, , , (compiler.misc.location: kindname.class, Test, null)",
+               "2 errors");
+        doTest("import p.*;",
+               "",
+               (String[]) null);
+        doTest("import p.*;",
+               "A a;",
+               "Test.java:2:33: compiler.err.cant.access: p.A, (compiler.misc.bad.class.file.header: A.class, (compiler.misc.illegal.start.of.class.file))",
+               "1 error");
+        doTest("import p.*;",
+               "void test() { A a; }",
+               "Test.java:2:47: compiler.err.cant.access: p.A, (compiler.misc.bad.class.file.header: A.class, (compiler.misc.illegal.start.of.class.file))",
+               "1 error");
+
+        doTest("import p.B.I;",
+               "",
+               "Test.java:2:11: compiler.err.cant.access: p.B.I, (compiler.misc.bad.class.file.header: B$I.class, (compiler.misc.illegal.start.of.class.file))",
+               "1 error");
+        doTest("import p.B.I;",
+               "I i;",
+               "Test.java:2:11: compiler.err.cant.access: p.B.I, (compiler.misc.bad.class.file.header: B$I.class, (compiler.misc.illegal.start.of.class.file))",
+               "1 error");
+        doTest("import p.B.I;",
+               "void test() { I i; }",
+               "Test.java:2:11: compiler.err.cant.access: p.B.I, (compiler.misc.bad.class.file.header: B$I.class, (compiler.misc.illegal.start.of.class.file))",
+               "1 error");
+        doTest("import p.B.*;",
+               "",
+               (String[]) null);
+        doTest("import p.B.*;",
+               "I i;",
+               "Test.java:2:35: compiler.err.cant.access: p.B.I, (compiler.misc.bad.class.file.header: B$I.class, (compiler.misc.illegal.start.of.class.file))",
+               "1 error");
+        doTest("import p.B.*;",
+               "void test() { I i; }",
+               "Test.java:2:49: compiler.err.cant.access: p.B.I, (compiler.misc.bad.class.file.header: B$I.class, (compiler.misc.illegal.start.of.class.file))",
+               "1 error");
+
+        doTest("import static p.B.I;",
+               "",
+               "Test.java:2:1: compiler.err.cant.access: p.B.I, (compiler.misc.bad.class.file.header: B$I.class, (compiler.misc.illegal.start.of.class.file))",
+               "1 error");
+        doTest("import static p.B.I;",
+               "I i;",
+               "Test.java:2:42: compiler.err.cant.access: p.B.I, (compiler.misc.bad.class.file.header: B$I.class, (compiler.misc.illegal.start.of.class.file))",
+               "1 error");
+        doTest("import static p.B.I;",
+               "void test() { I i; }",
+               "Test.java:2:1: compiler.err.cant.access: p.B.I, (compiler.misc.bad.class.file.header: B$I.class, (compiler.misc.illegal.start.of.class.file))",
+               "1 error");
+        doTest("import static p.B.*;",
+               "",
+               "Test.java:2:1: compiler.err.cant.access: p.B.I, (compiler.misc.bad.class.file.header: B$I.class, (compiler.misc.illegal.start.of.class.file))",
+               "1 error");
+        doTest("import static p.B.*;",
+               "I i;",
+               "Test.java:2:42: compiler.err.cant.access: p.B.I, (compiler.misc.bad.class.file.header: B$I.class, (compiler.misc.illegal.start.of.class.file))",
+               "1 error");
+        doTest("import static p.B.*;",
+               "void test() { I i; }",
+               "Test.java:2:1: compiler.err.cant.access: p.B.I, (compiler.misc.bad.class.file.header: B$I.class, (compiler.misc.illegal.start.of.class.file))",
+               "1 error");
+    }
+
+    void doTest(String importText, String useText, String... expectedOutput) {
+        List<String> log = new JavacTask(tb)
+                .classpath(".")
+                .sources("\n" + importText + " public class Test { " + useText + " }")
+                .options("-XDrawDiagnostics")
+                .run(expectedOutput != null ? Task.Expect.FAIL : Task.Expect.SUCCESS)
+                .writeAll()
+                .getOutputLines(Task.OutputKind.DIRECT);
+
+        if (expectedOutput != null && !log.equals(Arrays.asList(expectedOutput))) {
+            throw new AssertionError("Unexpected output: " + log);
+        }
+    }
+}