Skip to content

Commit

Permalink
8278131: runtime/cds/appcds/dynamicArchive/* tests failing in loom repo
Browse files Browse the repository at this point in the history
Reviewed-by: iklam, dholmes, minqi
  • Loading branch information
calvinccheung committed Dec 16, 2021
1 parent e6b28e0 commit 0dbe4c5
Show file tree
Hide file tree
Showing 9 changed files with 66 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import java.lang.invoke.MethodType;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodHandles.Lookup;
import java.lang.invoke.MethodHandles.Lookup.ClassOption;
import static java.lang.invoke.MethodHandles.Lookup.ClassOption.*;
import jdk.test.lib.compiler.InMemoryJavaCompiler;

Expand All @@ -44,12 +45,19 @@ public class InstantiateHiddenClass {
" } } ");

public static void main(String[] args) throws Throwable {
// This class is also used by the appcds/dynamicArchive/RegularHiddenClass.java
// test which will pass the "keep-alive" argument during dynamic CDS dump
// for preventing from being GC'ed prior to the dumping operation.
boolean keepAlive = false;
if (args.length == 1 && args[0].equals("keep-alive")) {
keepAlive = true;
}

// Test that a hidden class cannot be found through its name.
try {
Lookup lookup = MethodHandles.lookup();
Class<?> cl = lookup.defineHiddenClass(klassbuf, false, NESTMATE).lookupClass();
Class.forName(cl.getName()).newInstance();
Class<?> c0 = lookup.defineHiddenClass(klassbuf, false, NESTMATE).lookupClass();
Class.forName(c0.getName()).newInstance();
throw new RuntimeException("Expected ClassNotFoundException not thrown");
} catch (ClassNotFoundException e ) {
// Test passed
Expand All @@ -60,8 +68,9 @@ public static void main(String[] args) throws Throwable {
// Verify that the references to these objects are different and references
// to their classes are not equal either.
Lookup lookup = MethodHandles.lookup();
Class<?> c1 = lookup.defineHiddenClass(klassbuf, false, NESTMATE).lookupClass();
Class<?> c2 = lookup.defineHiddenClass(klassbuf, false, NESTMATE).lookupClass();
ClassOption classOption = keepAlive ? STRONG : NESTMATE;
Class<?> c1 = lookup.defineHiddenClass(klassbuf, false, classOption).lookupClass();
Class<?> c2 = lookup.defineHiddenClass(klassbuf, false, classOption).lookupClass();
Object o1 = c1.newInstance();
Object o2 = c2.newInstance();
if (o1 == o2) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,12 @@

public class HelloUnload {
private static String className = "CustomLoadee";
// Prevent the following class from being GC'ed too soon.
private static Class keptC = null;

public static void main(String args[]) throws Exception {
if (args.length != 3) {
throw new RuntimeException("Unexpected number of arguments: expected 3, actual " + args.length);
if (args.length < 3) {
throw new RuntimeException("Unexpected number of arguments: expected at least 3, actual " + args.length);
}

String path = args[0];
Expand Down Expand Up @@ -62,9 +64,20 @@ public static void main(String args[]) throws Exception {
throw new RuntimeException("args[2] can only be either \"true\" or \"false\", actual " + args[1]);
}

// The HelloDynamicCustom.java and PrintSharedArchiveAndExit.java tests
// under appcds/dynamicArchive pass the keep-alive argument for preventing
// the class from being GC'ed prior to dumping of the dynamic CDS archive.
boolean keepAlive = false;
if (args[args.length - 1].equals("keep-alive")) {
keepAlive = true;
}

URLClassLoader urlClassLoader =
new URLClassLoader("HelloClassLoader", urls, null);
Class c = Class.forName(className, true, urlClassLoader);
if (keepAlive) {
keptC = c;
}
System.out.println(c);
System.out.println(c.getClassLoader());
Object o = c.newInstance();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,12 @@
import java.io.File;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.HashMap;
import sun.hotspot.WhiteBox;

public class OldClassApp {
// Prevent the classes from being GC'ed too soon.
static HashMap<String, Class> clsMap = new HashMap<>();
public static void main(String args[]) throws Exception {
String path = args[0];
URL url = new File(path).toURI().toURL();
Expand All @@ -44,13 +47,26 @@ public static void main(String args[]) throws Exception {
throw new RuntimeException("args[1] can only be either \"true\" or \"false\", actual " + args[1]);
}

// The OldClassAndInf.java test under appcds/dynamicArchive passes the keep-alive
// argument for preventing the classes from being GC'ed prior to dumping of
// the dynamic CDS archive.
int startIdx = 2;
boolean keepAlive = false;
if (args[2].equals("keep-alive")) {
keepAlive = true;
startIdx = 3;
}

URLClassLoader urlClassLoader =
new URLClassLoader("OldClassAppClassLoader", urls, null);

for (int i = 2; i < args.length; i++) {
for (int i = startIdx; i < args.length; i++) {
Class c = urlClassLoader.loadClass(args[i]);
System.out.println(c);
System.out.println(c.getClassLoader());
if (keepAlive) {
clsMap.put(args[i], c);
}

// [1] Check that class is defined by the correct loader
if (c.getClassLoader() != urlClassLoader) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ private static void testDefaultBase() throws Exception {
"-Xlog:cds",
"-Xlog:cds+dynamic=debug",
"-cp", appJar,
mainAppClass, customJarPath, "false", "false")
mainAppClass, customJarPath, "false", "false", "keep-alive")
.assertNormalExit(output -> {
output.shouldContain("Written dynamic archive 0x")
.shouldNotContain("klasses.*=.*CustomLoadee")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ static void test() throws Exception {
// 1. Host class loaded by a custom loader is initialized during dump time.
dump(topArchiveName,
"-Xlog:class+load,cds=debug,cds+dynamic",
"-cp", appJar, mainClass, appJar, "init")
"-cp", appJar, mainClass, appJar, "init", "keep-alive")
.assertNormalExit(output -> {
output.shouldMatch("Skipping.LambHello[$][$]Lambda[$].*0x.*:.Hidden.class")
.shouldHaveExitValue(0);
Expand All @@ -67,7 +67,7 @@ static void test() throws Exception {
// 2. Host class loaded by a custom loader is NOT initialized during dump time.
dump(topArchiveName,
"-Xlog:class+load,cds=debug,cds+dynamic",
"-cp", appJar, mainClass, appJar)
"-cp", appJar, mainClass, appJar, "keep-alive")
.assertNormalExit(output -> {
output.shouldHaveExitValue(0);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ private static void doTest(String loadeesJar, String inArchive, String ...loadee
"-Xlog:cds",
"-Xlog:cds+dynamic=debug",
"-cp", appJar,
mainAppClass, loadeesJar, inArchive),
mainAppClass, loadeesJar, inArchive, "keep-alive"),
loadees))
.assertNormalExit(output -> {
output.shouldContain("Written dynamic archive 0x")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public static void testPrtNExit() throws Exception {
"-Xlog:cds",
"-Xlog:cds+dynamic=debug",
"-cp", appJar,
mainAppClass, customJarPath, "false", "false")
mainAppClass, customJarPath, "false", "false", "keep-alive")
.assertNormalExit(output -> {
output.shouldContain("Written dynamic archive 0x")
.shouldNotContain("klasses.*=.*CustomLoadee")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ static void test() throws Exception {

dump(topArchiveName,
"-Xlog:class+load=debug,cds+dynamic,cds=debug",
"-cp", appJar, mainClass)
"-cp", appJar, mainClass, "keep-alive")
.assertNormalExit(output -> {
output.shouldMatch("cds.*Skipping.TestClass.0x.*Hidden.class")
.shouldNotMatch("cds.dynamic.*Archiving.hidden.TestClass.*")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@

public class CustomLoaderApp {
private static String className = "LambHello";
// Prevent the class from being GC'ed too soon.
private static Class keptC = null;

public static void main(String args[]) throws Exception {
String path = args[0];
Expand All @@ -37,13 +39,25 @@ public static void main(String args[]) throws Exception {
System.out.println(url);

boolean init = false;
if (args.length ==2 && args[1].equals("init")) {
if (args.length >= 2 && args[1].equals("init")) {
init = true;
}

// The dynamicArchive/LambdaCustomLoader.java test passes the keep-alive
// argument for preventing the class from being GC'ed prior to dumping of
// the dynamic CDS archive.
boolean keepAlive = false;
if (args[args.length - 1].equals("keep-alive")) {
keepAlive = true;
}

URLClassLoader urlClassLoader =
new URLClassLoader("HelloClassLoader", urls, null);
Class c = Class.forName(className, init, urlClassLoader);
if (keepAlive) {
keptC = c;
}

System.out.println(c);
System.out.println(c.getClassLoader());
if (c.getClassLoader() != urlClassLoader) {
Expand Down

1 comment on commit 0dbe4c5

@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.