# HG changeset patch # User ccheung # Date 1565911421 25200 # Node ID a89ec7fc99aacf5e39d5d62f6ccae1991d9e24d4 # Parent 2c66dbb9422722a95549dfc087864b7d4374d9fb 8226645: [TESTBUG] some AppCDS tests rely on illegal reflective access Summary: Updated tests to use Lookup.defineClass instead of ClassLoader.defineClass. Reviewed-by: iklam, dholmes, alanb diff -r 2c66dbb94227 -r a89ec7fc99aa test/hotspot/jtreg/runtime/cds/appcds/RewriteBytecodesTest.java --- a/test/hotspot/jtreg/runtime/cds/appcds/RewriteBytecodesTest.java Thu Aug 15 19:29:58 2019 +0000 +++ b/test/hotspot/jtreg/runtime/cds/appcds/RewriteBytecodesTest.java Thu Aug 15 16:23:41 2019 -0700 @@ -24,7 +24,7 @@ /* * @test - * @summary Use ClassLoader.defineClass() to load a class with rewritten bytecode. Make sure + * @summary Use Lookup.defineClass() to load a class with rewritten bytecode. Make sure * the archived class with the same name is not loaded. * @requires vm.cds * @library /test/lib @@ -52,7 +52,6 @@ OutputAnalyzer output = TestCommon.exec(appJar, // command-line arguments ... - "--add-opens=java.base/java.lang=ALL-UNNAMED", use_whitebox_jar, "-XX:+UnlockDiagnosticVMOptions", "-XX:+WhiteBoxAPI", diff -r 2c66dbb94227 -r a89ec7fc99aa test/hotspot/jtreg/runtime/cds/appcds/customLoader/LoaderSegregationTest.java --- a/test/hotspot/jtreg/runtime/cds/appcds/customLoader/LoaderSegregationTest.java Thu Aug 15 19:29:58 2019 +0000 +++ b/test/hotspot/jtreg/runtime/cds/appcds/customLoader/LoaderSegregationTest.java Thu Aug 15 16:23:41 2019 -0700 @@ -63,7 +63,7 @@ String wbJar = JarBuilder.build(true, "WhiteBox", "sun/hotspot/WhiteBox"); String use_whitebox_jar = "-Xbootclasspath/a:" + wbJar; - String appJar = JarBuilder.build("LoaderSegregation_app", "LoaderSegregation", + String appJar = JarBuilder.build("LoaderSegregation_app", "LoaderSegregation", "LoaderSegregation$1", "CustomLoadee", "CustomLoadee2", "CustomLoadee3Child", "CustomInterface2_ia", "OnlyBuiltin", "Util"); @@ -110,8 +110,6 @@ output = TestCommon.exec(TestCommon.concatPaths(appJar, app2Jar), // command-line arguments ... - "--add-opens=java.base/java.lang=ALL-UNNAMED", - "--add-opens=java.base/java.security=ALL-UNNAMED", use_whitebox_jar, "-XX:+UnlockDiagnosticVMOptions", "-XX:+WhiteBoxAPI", diff -r 2c66dbb94227 -r a89ec7fc99aa test/hotspot/jtreg/runtime/cds/appcds/customLoader/test-classes/LoaderSegregation.java --- a/test/hotspot/jtreg/runtime/cds/appcds/customLoader/test-classes/LoaderSegregation.java Thu Aug 15 19:29:58 2019 +0000 +++ b/test/hotspot/jtreg/runtime/cds/appcds/customLoader/test-classes/LoaderSegregation.java Thu Aug 15 16:23:41 2019 -0700 @@ -81,8 +81,25 @@ } { // UNREGISTERED LOADER - URLClassLoader urlClassLoader = new URLClassLoader(urls); - Class c2 = Util.defineClassFromJAR(urlClassLoader, jarFile, ONLY_BUILTIN); + URLClassLoader urlClassLoader = new URLClassLoader(urls) { + protected Class> loadClass(String name, boolean resolve) throws ClassNotFoundException { + synchronized (getClassLoadingLock(name)) { + Class> c = findLoadedClass(name); + if (c == null) { + try { + c = findClass(name); + } catch (ClassNotFoundException e) { + c = getParent().loadClass(name); + } + } + if (resolve) { + resolveClass(c); + } + return c; + } + } + }; + Class> c2 = urlClassLoader.loadClass(ONLY_BUILTIN); if (c2.getClassLoader() != urlClassLoader) { throw new RuntimeException("Error in test"); diff -r 2c66dbb94227 -r a89ec7fc99aa test/hotspot/jtreg/runtime/cds/appcds/test-classes/RewriteBytecodes.java --- a/test/hotspot/jtreg/runtime/cds/appcds/test-classes/RewriteBytecodes.java Thu Aug 15 19:29:58 2019 +0000 +++ b/test/hotspot/jtreg/runtime/cds/appcds/test-classes/RewriteBytecodes.java Thu Aug 15 16:23:41 2019 -0700 @@ -23,6 +23,7 @@ */ import java.io.File; +import java.lang.invoke.MethodHandles; import sun.hotspot.WhiteBox; public class RewriteBytecodes { @@ -30,7 +31,7 @@ String from = "___xxx___"; String to = "___yyy___"; File clsFile = new File(args[0]); - Class superClass = Util.defineModifiedClass(RewriteBytecodes.class.getClassLoader(), clsFile, from, to); + Class superClass = Util.defineModifiedClass(MethodHandles.lookup(), clsFile, from, to); Child child = new Child(); diff -r 2c66dbb94227 -r a89ec7fc99aa test/hotspot/jtreg/runtime/cds/appcds/test-classes/Util.java --- a/test/hotspot/jtreg/runtime/cds/appcds/test-classes/Util.java Thu Aug 15 19:29:58 2019 +0000 +++ b/test/hotspot/jtreg/runtime/cds/appcds/test-classes/Util.java Thu Aug 15 16:23:41 2019 -0700 @@ -23,19 +23,21 @@ */ import java.io.*; +import java.lang.invoke.MethodHandles; +import java.lang.invoke.MethodHandles.Lookup; import java.lang.reflect.*; import java.util.jar.*; public class Util { /** - * Invoke the loader.defineClass() class method to define the class stored in clsFile, + * Define the class as stored in clsFile with the provided lookup instance, * with the following modification: *