14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
23 */
24
25 // AOT-linked classes are loaded during VM bootstrap by the C++ class AOTLinkedClassBulkLoader.
26 // Make sure that the Module, Package, CodeSource and ProtectionDomain of these classes are
27 // set up properly.
28
29 /*
30 * @test id=static
31 * @requires vm.cds.supports.aot.class.linking
32 * @comment work around JDK-8345635
33 * @requires !vm.jvmci.enabled
34 * @library /test/jdk/lib/testlibrary /test/lib
35 * @build InitiatingLoaderTester BadOldClassA BadOldClassB
36 * @build BulkLoaderTest
37 * @run driver jdk.test.lib.helpers.ClassFileInstaller -jar BulkLoaderTestApp.jar BulkLoaderTestApp MyUtil InitiatingLoaderTester
38 * BadOldClassA BadOldClassB
39 * @run driver BulkLoaderTest STATIC
40 */
41
42 /*
43 * @test id=dynamic
44 * @requires vm.cds.supports.aot.class.linking
45 * @comment work around JDK-8345635
46 * @requires !vm.jvmci.enabled
47 * @library /test/jdk/lib/testlibrary /test/lib
48 * @build InitiatingLoaderTester BadOldClassA BadOldClassB
49 * @build jdk.test.whitebox.WhiteBox BulkLoaderTest
50 * @run driver jdk.test.lib.helpers.ClassFileInstaller -jar BulkLoaderTestApp.jar BulkLoaderTestApp MyUtil InitiatingLoaderTester
51 * BadOldClassA BadOldClassB
52 * @run driver jdk.test.lib.helpers.ClassFileInstaller jdk.test.whitebox.WhiteBox
53 * @run main/othervm -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI -Xbootclasspath/a:. BulkLoaderTest DYNAMIC
54 */
55
56 /*
57 * @test id=aot
58 * @requires vm.cds.supports.aot.class.linking
59 * @comment work around JDK-8345635
60 * @requires !vm.jvmci.enabled
61 * @library /test/jdk/lib/testlibrary /test/lib
62 * @build InitiatingLoaderTester BadOldClassA BadOldClassB
63 * @build BulkLoaderTest
64 * @run driver jdk.test.lib.helpers.ClassFileInstaller -jar BulkLoaderTestApp.jar BulkLoaderTestApp MyUtil InitiatingLoaderTester
65 * BadOldClassA BadOldClassB
66 * @run driver BulkLoaderTest AOT
67 */
68
69 import java.io.File;
70 import java.lang.StackWalker.StackFrame;
71 import java.util.List;
72 import java.util.regex.Matcher;
73 import java.util.regex.Pattern;
74 import java.util.stream.Collectors;
75 import java.util.Set;
76 import jdk.test.lib.cds.CDSAppTester;
77 import jdk.test.lib.helpers.ClassFileInstaller;
78 import jdk.test.lib.process.OutputAnalyzer;
79
80 public class BulkLoaderTest {
81 static final String appJar = ClassFileInstaller.getJarPath("BulkLoaderTestApp.jar");
82 static final String mainClass = "BulkLoaderTestApp";
83
84 public static void main(String[] args) throws Exception {
85 Tester t = new Tester();
86
87 // Run with archived FMG loaded
88 t.run(args);
89
90 // Run with an extra classpath -- archived FMG can still load.
107 OutputAnalyzer out = t.productionRun(extraVmArgs);
108 out.shouldHaveExitValue(1);
109 out.shouldContain("CDS archive has aot-linked classes. It cannot be used when archived full module graph is not used.");
110 t.setCheckExitValue(true);
111 }
112 }
113
114 static class Tester extends CDSAppTester {
115 public Tester() {
116 super(mainClass);
117 }
118
119 @Override
120 public String classpath(RunMode runMode) {
121 return appJar;
122 }
123
124 @Override
125 public String[] vmArgs(RunMode runMode) {
126 return new String[] {
127 "-Xlog:cds,cds+aot+load",
128 "-XX:+AOTClassLinking",
129 };
130 }
131
132 @Override
133 public String[] appCommandLine(RunMode runMode) {
134 return new String[] {
135 mainClass,
136 };
137 }
138
139 @Override
140 public void checkExecution(OutputAnalyzer out, RunMode runMode) throws Exception {
141 if (isAOTWorkflow() && runMode == RunMode.TRAINING) {
142 out.shouldContain("Skipping BadOldClassA: Unlinked class not supported by AOTConfiguration");
143 }
144 }
145 }
146 }
147
148 class BulkLoaderTestApp {
149 static String allPerms = "null.*<no principals>.*java.security.Permissions.*,*java.security.AllPermission.*<all permissions>.*<all actions>";
150
151 public static void main(String args[]) throws Exception {
152 checkClasses();
153 checkInitiatingLoader();
154 checkOldClasses();
155 }
156
157 // Check the ClassLoader/Module/Package/ProtectionDomain/CodeSource of classes that are aot-linked
158 static void checkClasses() throws Exception {
159 check(String.class,
160 "null", // loader
161 "module java.base",
162 "package java.lang",
163 "null",
164 allPerms);
165
166 check(Class.forName("sun.util.logging.internal.LoggingProviderImpl"),
167 "null",
168 "module java.logging",
169 "package sun.util.logging.internal",
170 "null",
171 allPerms);
172
173
174 check(javax.tools.FileObject.class,
258 // implNote: BadOldClassA will be excluded, so any resolved refereces
259 // to BadOldClassA should be removed from the archived constant pool.
260 Class c = BadOldClassA.class;
261 Object n = new Object();
262 if (c.isInstance(n)) { // Note that type-testing BadOldClassA here neither links nor initializes it.
263 throw new RuntimeException("Must not succeed");
264 }
265
266 try {
267 // In dynamic dump, the VM loads BadOldClassB and then attempts to
268 // link it. This will leave BadOldClassB in a "failed verification" state.
269 // All refernces to BadOldClassB from the CP should be purged from the CDS
270 // archive.
271 c = BadOldClassB.class;
272 c.newInstance();
273 throw new RuntimeException("Must not succeed");
274 } catch (VerifyError e) {
275 System.out.println("Caught VerifyError for BadOldClassB: " + e);
276 }
277 }
278 }
279
280 class MyUtil {
281 // depth is 0-based -- i.e., depth==0 returns the class of the immediate caller of getCallerClass
282 static Class<?> getCallerClass(int depth) {
283 // Need to add the frame of the getCallerClass -- so the immediate caller (depth==0) of this method
284 // is at stack.get(1) == stack.get(depth+1);
285 StackWalker walker = StackWalker.getInstance(
286 Set.of(StackWalker.Option.RETAIN_CLASS_REFERENCE,
287 StackWalker.Option.SHOW_HIDDEN_FRAMES));
288 List<StackFrame> stack = walker.walk(s -> s.limit(depth+2).collect(Collectors.toList()));
289 return stack.get(depth+1).getDeclaringClass();
290 }
291 }
|
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
23 */
24
25 // AOT-linked classes are loaded during VM bootstrap by the C++ class AOTLinkedClassBulkLoader.
26 // Make sure that the Module, Package, CodeSource and ProtectionDomain of these classes are
27 // set up properly.
28
29 /*
30 * @test id=static
31 * @requires vm.cds.supports.aot.class.linking
32 * @comment work around JDK-8345635
33 * @requires !vm.jvmci.enabled
34 * @library /test/jdk/lib/testlibrary /test/lib /test/hotspot/jtreg/runtime/cds/appcds/test-classes
35 * @build InitiatingLoaderTester BadOldClassA BadOldClassB
36 * @build BulkLoaderTest SimpleCusty
37 * @run driver jdk.test.lib.helpers.ClassFileInstaller -jar BulkLoaderTestApp.jar BulkLoaderTestApp MyUtil InitiatingLoaderTester
38 * BadOldClassA BadOldClassB
39 * @run driver jdk.test.lib.helpers.ClassFileInstaller -jar cust.jar
40 * SimpleCusty
41 * @run driver BulkLoaderTest STATIC
42 */
43
44 /*
45 * @test id=dynamic
46 * @requires vm.cds.supports.aot.class.linking
47 * @comment work around JDK-8345635
48 * @requires !vm.jvmci.enabled
49 * @library /test/jdk/lib/testlibrary /test/lib /test/hotspot/jtreg/runtime/cds/appcds/test-classes
50 * @build InitiatingLoaderTester BadOldClassA BadOldClassB
51 * @build jdk.test.whitebox.WhiteBox BulkLoaderTest SimpleCusty
52 * @run driver jdk.test.lib.helpers.ClassFileInstaller -jar BulkLoaderTestApp.jar BulkLoaderTestApp MyUtil InitiatingLoaderTester
53 * BadOldClassA BadOldClassB
54 * @run driver jdk.test.lib.helpers.ClassFileInstaller -jar cust.jar
55 * SimpleCusty
56 * @run driver jdk.test.lib.helpers.ClassFileInstaller jdk.test.whitebox.WhiteBox
57 * @run main/othervm -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI -Xbootclasspath/a:. BulkLoaderTest DYNAMIC
58 */
59
60 /*
61 * @test id=aot
62 * @requires vm.cds.supports.aot.class.linking
63 * @comment work around JDK-8345635
64 * @requires !vm.jvmci.enabled
65 * @library /test/jdk/lib/testlibrary /test/lib /test/hotspot/jtreg/runtime/cds/appcds/test-classes
66 * @build InitiatingLoaderTester BadOldClassA BadOldClassB
67 * @build BulkLoaderTest SimpleCusty
68 * @run driver jdk.test.lib.helpers.ClassFileInstaller -jar BulkLoaderTestApp.jar BulkLoaderTestApp MyUtil InitiatingLoaderTester
69 * BadOldClassA BadOldClassB
70 * @run driver jdk.test.lib.helpers.ClassFileInstaller -jar cust.jar
71 * SimpleCusty
72 * @run driver BulkLoaderTest AOT
73 */
74
75 import java.io.File;
76 import java.lang.StackWalker.StackFrame;
77 import java.net.URL;
78 import java.net.URLClassLoader;
79 import java.util.List;
80 import java.util.regex.Matcher;
81 import java.util.regex.Pattern;
82 import java.util.stream.Collectors;
83 import java.util.Set;
84 import jdk.test.lib.cds.CDSAppTester;
85 import jdk.test.lib.helpers.ClassFileInstaller;
86 import jdk.test.lib.process.OutputAnalyzer;
87
88 public class BulkLoaderTest {
89 static final String appJar = ClassFileInstaller.getJarPath("BulkLoaderTestApp.jar");
90 static final String mainClass = "BulkLoaderTestApp";
91
92 public static void main(String[] args) throws Exception {
93 Tester t = new Tester();
94
95 // Run with archived FMG loaded
96 t.run(args);
97
98 // Run with an extra classpath -- archived FMG can still load.
115 OutputAnalyzer out = t.productionRun(extraVmArgs);
116 out.shouldHaveExitValue(1);
117 out.shouldContain("CDS archive has aot-linked classes. It cannot be used when archived full module graph is not used.");
118 t.setCheckExitValue(true);
119 }
120 }
121
122 static class Tester extends CDSAppTester {
123 public Tester() {
124 super(mainClass);
125 }
126
127 @Override
128 public String classpath(RunMode runMode) {
129 return appJar;
130 }
131
132 @Override
133 public String[] vmArgs(RunMode runMode) {
134 return new String[] {
135 "-Xlog:cds,cds+aot+load,cds+class=debug",
136 "-XX:+AOTClassLinking",
137 };
138 }
139
140 @Override
141 public String[] appCommandLine(RunMode runMode) {
142 return new String[] {
143 mainClass,
144 };
145 }
146
147 @Override
148 public void checkExecution(OutputAnalyzer out, RunMode runMode) throws Exception {
149 if (isAOTWorkflow() && runMode == RunMode.TRAINING) {
150 out.shouldContain("Skipping BadOldClassA: Unlinked class not supported by AOTConfiguration");
151 }
152
153 if (isDumping(runMode)) {
154 // Check that we are archiving classes for custom class loaders.
155 out.shouldMatch("cds,class.* SimpleCusty");
156 }
157 }
158 }
159 }
160
161 class BulkLoaderTestApp {
162 static String allPerms = "null.*<no principals>.*java.security.Permissions.*,*java.security.AllPermission.*<all permissions>.*<all actions>";
163
164 public static void main(String args[]) throws Exception {
165 checkClasses();
166 checkInitiatingLoader();
167 checkOldClasses();
168 checkCustomLoader();
169 }
170
171 // Check the ClassLoader/Module/Package/ProtectionDomain/CodeSource of classes that are aot-linked
172 static void checkClasses() throws Exception {
173 check(String.class,
174 "null", // loader
175 "module java.base",
176 "package java.lang",
177 "null",
178 allPerms);
179
180 check(Class.forName("sun.util.logging.internal.LoggingProviderImpl"),
181 "null",
182 "module java.logging",
183 "package sun.util.logging.internal",
184 "null",
185 allPerms);
186
187
188 check(javax.tools.FileObject.class,
272 // implNote: BadOldClassA will be excluded, so any resolved refereces
273 // to BadOldClassA should be removed from the archived constant pool.
274 Class c = BadOldClassA.class;
275 Object n = new Object();
276 if (c.isInstance(n)) { // Note that type-testing BadOldClassA here neither links nor initializes it.
277 throw new RuntimeException("Must not succeed");
278 }
279
280 try {
281 // In dynamic dump, the VM loads BadOldClassB and then attempts to
282 // link it. This will leave BadOldClassB in a "failed verification" state.
283 // All refernces to BadOldClassB from the CP should be purged from the CDS
284 // archive.
285 c = BadOldClassB.class;
286 c.newInstance();
287 throw new RuntimeException("Must not succeed");
288 } catch (VerifyError e) {
289 System.out.println("Caught VerifyError for BadOldClassB: " + e);
290 }
291 }
292
293
294 static void checkCustomLoader() throws Exception {
295 Object o = initFromCustomLoader();
296 System.out.println(o);
297 }
298
299 static Object initFromCustomLoader() throws Exception {
300 String path = "cust.jar";
301 URL url = new File(path).toURI().toURL();
302 URL[] urls = new URL[] {url};
303 URLClassLoader urlClassLoader =
304 new URLClassLoader("MyLoader", urls, null);
305 Class c = Class.forName("SimpleCusty", true, urlClassLoader);
306 return c.newInstance();
307 }
308 }
309
310 class MyUtil {
311 // depth is 0-based -- i.e., depth==0 returns the class of the immediate caller of getCallerClass
312 static Class<?> getCallerClass(int depth) {
313 // Need to add the frame of the getCallerClass -- so the immediate caller (depth==0) of this method
314 // is at stack.get(1) == stack.get(depth+1);
315 StackWalker walker = StackWalker.getInstance(
316 Set.of(StackWalker.Option.RETAIN_CLASS_REFERENCE,
317 StackWalker.Option.SHOW_HIDDEN_FRAMES));
318 List<StackFrame> stack = walker.walk(s -> s.limit(depth+2).collect(Collectors.toList()));
319 return stack.get(depth+1).getDeclaringClass();
320 }
321 }
|