Skip to content

Commit

Permalink
8231599: NPE when loading a preview classfile from a future Java version
Browse files Browse the repository at this point in the history
Reviewed-by: vromero
  • Loading branch information
lahodaj committed Nov 11, 2020
1 parent 5181f9c commit 129ff97
Show file tree
Hide file tree
Showing 2 changed files with 137 additions and 2 deletions.
Expand Up @@ -2577,10 +2577,12 @@ private void readClassBuffer(ClassSymbol c) throws IOException {
majorVersion = nextChar();
int maxMajor = Version.MAX().major;
int maxMinor = Version.MAX().minor;
boolean previewClassFile =
minorVersion == ClassFile.PREVIEW_MINOR_VERSION;
if (majorVersion > maxMajor ||
majorVersion * 1000 + minorVersion <
Version.MIN().major * 1000 + Version.MIN().minor) {
if (majorVersion == (maxMajor + 1))
if (majorVersion == (maxMajor + 1) && !previewClassFile)
log.warning(Warnings.BigMajorVersion(currentClassFile,
majorVersion,
maxMajor));
Expand All @@ -2592,7 +2594,7 @@ private void readClassBuffer(ClassSymbol c) throws IOException {
Integer.toString(maxMinor));
}

if (minorVersion == ClassFile.PREVIEW_MINOR_VERSION) {
if (previewClassFile) {
if (!preview.isEnabled()) {
log.error(preview.disabledError(currentClassFile, majorVersion));
} else {
Expand Down
@@ -0,0 +1,133 @@
/*
* Copyright (c) 2020, 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 8231599
* @summary Verify javac does not crash on preview classfiles from the future
Java versions.
* @library /tools/lib
* @modules
* jdk.compiler/com.sun.tools.javac.api
* jdk.compiler/com.sun.tools.javac.jvm
* jdk.compiler/com.sun.tools.javac.main
* @build toolbox.ToolBox toolbox.JavacTask
* @run main TooNewMajorVersionTest
*/

import toolbox.JavacTask;
import toolbox.Task;
import toolbox.TestRunner;
import toolbox.ToolBox;

import com.sun.tools.javac.jvm.ClassFile.Version;

import java.io.RandomAccessFile;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;

public class TooNewMajorVersionTest extends TestRunner {

protected ToolBox tb;

TooNewMajorVersionTest() {
super(System.err);
tb = new ToolBox();
}

public static void main(String... args) throws Exception {
TooNewMajorVersionTest t = new TooNewMajorVersionTest();
t.runTests();
}

protected void runTests() throws Exception {
runTests(m -> new Object[] { Paths.get(m.getName()) });
}

@Test
public void brokenMajorVersionWithPreview(Path base) throws Exception {
Path src = base.resolve("src");
tb.writeJavaFiles(src, """
class C {
private Object o = null;
private boolean b = o instanceof String s;
}
""");
Path classes = base.resolve("classes");

Files.createDirectories(classes);

for (int upgrade = 1; upgrade < 3; upgrade++) {
new JavacTask(tb)
.outdir(classes)
.options("-XDforcePreview",
"--enable-preview",
"--release", String.valueOf(Runtime.version().feature()))
.files(tb.findJavaFiles(src))
.run()
.writeAll();

Path classfile = classes.resolve("C.class");
int wrongMajor;

try (RandomAccessFile f = new RandomAccessFile(classfile.toFile(), "rw")) {
f.readInt();
short minor = f.readShort();
if (minor != (-1)) {
throw new AssertionError("Unexpected minor version: " + minor);
}
long point = f.getFilePointer();
short major = f.readShort();
f.seek(point);
f.writeShort(wrongMajor = major + upgrade);
}

Path test = base.resolve("test");
tb.writeJavaFiles(test, "class Test extends C {}");
Path testClasses = base.resolve("classes");

Files.createDirectories(testClasses);

for (String extraOption : new String[] {"-XDignored", "--enable-preview"}) {
List<String> log = new JavacTask(tb)
.outdir(testClasses)
.options("-XDrawDiagnostics",
"-classpath", classes.toString(),
"--release", String.valueOf(Runtime.version().feature()),
extraOption)
.files(tb.findJavaFiles(test))
.run(Task.Expect.FAIL)
.writeAll()
.getOutputLines(Task.OutputKind.DIRECT);
List<String> expected = List.of(
"Test.java:1:20: compiler.err.cant.access: C, (compiler.misc.bad.class.file.header: C.class, (compiler.misc.wrong.version: " + wrongMajor + ", 65535, " + Version.MAX().major + ", 0))",
"1 error"
);
if (!log.equals(expected))
throw new Exception("expected output not found" + log);
}
}
}
}

1 comment on commit 129ff97

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.