Skip to content

Commit

Permalink
8296329: jar validator doesn't account for minor class file version
Browse files Browse the repository at this point in the history
Reviewed-by: yan
Backport-of: faf48e61be4f97f725b053aa351d3c64638546bf
  • Loading branch information
rvansa authored and Yuri Nesterenko committed Feb 6, 2023
1 parent ceabaee commit 4ab869e
Show file tree
Hide file tree
Showing 3 changed files with 126 additions and 4 deletions.
6 changes: 3 additions & 3 deletions src/jdk.jartool/share/classes/sun/tools/jar/FingerPrint.java
Expand Up @@ -105,7 +105,7 @@ public boolean isIdentical(FingerPrint that) {
}

public boolean isCompatibleVersion(FingerPrint that) {
return attrs.version >= that.attrs.version;
return attrs.majorVersion >= that.attrs.majorVersion;
}

public boolean isSameAPI(FingerPrint that) {
Expand Down Expand Up @@ -236,7 +236,7 @@ private static final class ClassAttributes extends ClassVisitor {
private String name;
private String outerClassName;
private String superName;
private int version;
private int majorVersion;
private int access;
private boolean publicClass;
private boolean nestedClass;
Expand All @@ -255,7 +255,7 @@ private boolean isPublic(int access) {
@Override
public void visit(int version, int access, String name, String signature,
String superName, String[] interfaces) {
this.version = version;
this.majorVersion = version & 0xFFFF; // JDK-8296329: extract major version only
this.access = access;
this.name = name;
this.nestedClass = name.contains("$");
Expand Down
6 changes: 5 additions & 1 deletion test/jdk/tools/jar/multiRelease/MRTestBase.java
Expand Up @@ -102,6 +102,10 @@ protected void compare(String jarfile,
}

void javac(Path dest, Path... sourceFiles) throws Throwable {
javac(dest, List.of(), sourceFiles);
}

void javac(Path dest, List<String> extraParameters, Path... sourceFiles) throws Throwable {

List<String> commands = new ArrayList<>();
String opts = System.getProperty("test.compiler.opts");
Expand All @@ -114,6 +118,7 @@ void javac(Path dest, Path... sourceFiles) throws Throwable {
Stream.of(sourceFiles)
.map(Object::toString)
.forEach(x -> commands.add(x));
commands.addAll(extraParameters);

StringWriter sw = new StringWriter();
try (PrintWriter pw = new PrintWriter(sw)) {
Expand All @@ -122,7 +127,6 @@ void javac(Path dest, Path... sourceFiles) throws Throwable {
throw new RuntimeException(sw.toString());
}
}

}

OutputAnalyzer jarWithStdin(File stdinSource,
Expand Down
118 changes: 118 additions & 0 deletions test/jdk/tools/jar/multiRelease/VersionValidatorTest.java
@@ -0,0 +1,118 @@
/*
* Copyright (c) 2022, 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 8296329
* @summary Tests for version validator.
* @library /test/lib
* @modules java.base/jdk.internal.misc
* jdk.compiler
* jdk.jartool
* @build jdk.test.lib.Utils
* jdk.test.lib.Asserts
* jdk.test.lib.JDKToolFinder
* jdk.test.lib.JDKToolLauncher
* jdk.test.lib.Platform
* jdk.test.lib.process.*
* MRTestBase
* @run testng/timeout=1200 VersionValidatorTest
*/

import org.testng.annotations.BeforeMethod;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

import java.lang.reflect.Method;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;

public class VersionValidatorTest extends MRTestBase {
private Path root;

@BeforeMethod
void testInit(Method method) {
root = Paths.get(method.getName());
}

@Test(dataProvider = "differentMajorVersions")
public void onlyCompatibleVersionIsAllowedInMultiReleaseJar(String baseMajorVersion, String otherMajorVersion,
boolean enablePreviewForBaseVersion, boolean enablePreviewForOtherVersion, boolean isAcceptable)
throws Throwable {
Path baseVersionClassesDir = compileLibClass(baseMajorVersion, enablePreviewForBaseVersion);
Path otherVersionClassesDir = compileLibClass(otherMajorVersion, enablePreviewForOtherVersion);

var result = jar("--create", "--file", "lib.jar", "-C", baseVersionClassesDir.toString(), "Lib.class",
"--release", otherMajorVersion, "-C", otherVersionClassesDir.toString(), "Lib.class");

if (isAcceptable) {
result.shouldHaveExitValue(SUCCESS)
.shouldBeEmptyIgnoreVMWarnings();
} else {
result.shouldNotHaveExitValue(SUCCESS)
.shouldContain("has a class version incompatible with an earlier version");
}
}

private Path compileLibClass(String majorVersion, boolean enablePreview) throws Throwable {
String classTemplate = """
public class Lib {
public static int version = $VERSION;
}
""";

Path sourceFile = Files.createDirectories(root.resolve("src").resolve(majorVersion)).resolve("Lib.java");
Files.write(sourceFile, classTemplate.replace("$VERSION", majorVersion).getBytes());

Path classesDir = root.resolve("classes").resolve(majorVersion);

javac(classesDir, List.of("--release", majorVersion), sourceFile);
if (enablePreview) {
rewriteMinorVersionForEnablePreviewClass(classesDir.resolve("Lib.class"));
}
return classesDir;
}

private void rewriteMinorVersionForEnablePreviewClass(Path classFile) throws Throwable {
byte[] classBytes = Files.readAllBytes(classFile);
classBytes[4] = -1;
classBytes[5] = -1;
Files.write(classFile, classBytes);
}

@DataProvider
Object[][] differentMajorVersions() {
return new Object[][] {
{ "16", "17", false, true, true },
{ "16", "17", false, false, true },
{ "16", "17", true, true, true },
{ "16", "17", true, false, true },
{ "17", "16", false, true, false },
{ "17", "16", false, false, false },
{ "17", "16", true, true, false },
{ "17", "16", true, false, false },
};
}
}

0 comments on commit 4ab869e

Please sign in to comment.