1 /* 2 * Copyright (c) 2023, 2025, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 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 /* 26 * @test 27 * @summary Test how various AOT optimizations handle classes that are excluded from the AOT cache. 28 * @requires vm.cds.write.archived.java.heap 29 * @library /test/jdk/lib/testlibrary /test/lib 30 * /test/hotspot/jtreg/runtime/cds/appcds/aotCache/test-classes 31 * @build ExcludedClasses CustyWithLoop 32 * @run driver jdk.test.lib.helpers.ClassFileInstaller -jar app.jar 33 * TestApp 34 * TestApp$Foo 35 * TestApp$Foo$BadFieldSig 36 * TestApp$Foo$BadMethodSig 37 * TestApp$Foo$Bar 38 * TestApp$Foo$GoodSig1 39 * TestApp$Foo$GoodSig2 40 * TestApp$Foo$ShouldBeExcluded 41 * TestApp$Foo$ShouldBeExcludedChild 42 * TestApp$Foo$Taz 43 * TestApp$MyInvocationHandler 44 * @run driver jdk.test.lib.helpers.ClassFileInstaller -jar cust.jar 45 * CustyWithLoop 46 * @run driver ExcludedClasses 47 */ 48 49 import java.io.File; 50 import java.lang.reflect.Array; 51 import java.lang.reflect.Field; 52 import java.lang.reflect.InvocationHandler; 53 import java.lang.reflect.Method; 54 import java.lang.reflect.Proxy; 55 import java.net.URL; 56 import java.net.URLClassLoader; 57 import java.security.ProtectionDomain; 58 import java.util.Map; 59 60 import jdk.jfr.Event; 61 import jdk.test.lib.cds.CDSAppTester; 62 import jdk.test.lib.helpers.ClassFileInstaller; 63 import jdk.test.lib.process.OutputAnalyzer; 64 65 public class ExcludedClasses { 66 static final String appJar = ClassFileInstaller.getJarPath("app.jar"); 67 static final String mainClass = "TestApp"; 68 69 public static void main(String[] args) throws Exception { 70 Tester tester = new Tester(); 71 tester.runAOTWorkflow("AOT", "--two-step-training"); 72 } 73 74 static class Tester extends CDSAppTester { 75 public Tester() { 76 super(mainClass); 77 } 78 79 @Override 80 public String classpath(RunMode runMode) { 81 return appJar; 82 } 83 84 @Override 85 public String[] vmArgs(RunMode runMode) { 86 return new String[] { 87 "-Xlog:aot=debug", 88 "-Xlog:aot+class=debug", 89 "-Xlog:aot+resolve=trace", 90 "-Xlog:aot+verification=trace", 91 "-Xlog:class+load", 92 }; 93 } 94 95 @Override 96 public String[] appCommandLine(RunMode runMode) { 97 return new String[] { 98 mainClass, runMode.name() 99 }; 100 } 101 102 @Override 103 public void checkExecution(OutputAnalyzer out, RunMode runMode) { 104 if (runMode == RunMode.ASSEMBLY) { 105 out.shouldNotMatch("aot,resolve.*archived field.*TestApp.Foo => TestApp.Foo.ShouldBeExcluded.f:I"); 106 out.shouldContain("Archived reflection data in TestApp$Foo$GoodSig1"); 107 out.shouldContain("Archived reflection data in TestApp$Foo$GoodSig2"); 108 out.shouldContain("Cannot archive reflection data in TestApp$Foo$BadMethodSig"); 109 out.shouldContain("Cannot archive reflection data in TestApp$Foo$BadFieldSig"); 110 } else if (runMode == RunMode.PRODUCTION) { 111 out.shouldContain("jdk.jfr.Event source: jrt:/jdk.jfr"); 112 out.shouldMatch("TestApp[$]Foo[$]ShouldBeExcluded source: .*/app.jar"); 113 out.shouldMatch("TestApp[$]Foo[$]ShouldBeExcludedChild source: .*/app.jar"); 114 } 115 } 116 } 117 } 118 119 class TestApp { 120 static volatile Object custInstance; 121 static volatile Object custArrayInstance; 122 123 public static void main(String args[]) throws Exception { 124 // In AOT workflow, classes from custom loaders are passed from the preimage 125 // to the final image. See FinalImageRecipes::record_all_classes(). 126 custInstance = initFromCustomLoader(); 127 custArrayInstance = Array.newInstance(custInstance.getClass(), 0); 128 System.out.println(custArrayInstance); 129 System.out.println("Counter = " + Foo.hotSpot()); 130 } 131 132 static Object initFromCustomLoader() throws Exception { 133 String path = "cust.jar"; 134 URL url = new File(path).toURI().toURL(); 135 URL[] urls = new URL[] {url}; 136 URLClassLoader urlClassLoader = 137 new URLClassLoader("MyLoader", urls, null); 138 Class c = Class.forName("CustyWithLoop", true, urlClassLoader); 139 return c.newInstance(); 140 } 141 142 static class MyInvocationHandler implements InvocationHandler { 143 volatile static int cnt; 144 145 public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { 146 long start = System.currentTimeMillis(); 147 while (System.currentTimeMillis() - start < 20) { 148 cnt += 2; 149 for (int i = 0; i < 1000; i++) { 150 int n = cnt - 2; 151 if (n < 2) { 152 n = 2; 153 } 154 cnt += (i + cnt) % n + cnt % 2; 155 } 156 } 157 return Integer.valueOf(cnt); 158 } 159 } 160 161 static class Foo { 162 volatile static int counter; 163 static Class c = ShouldBeExcluded.class; 164 165 static Map mapProxy = (Map) Proxy.newProxyInstance( 166 Foo.class.getClassLoader(), 167 new Class[] { Map.class }, 168 new MyInvocationHandler()); 169 170 static int hotSpot() { 171 ShouldBeExcluded s = new ShouldBeExcluded(); 172 Bar b = new Bar(); 173 174 long start = System.currentTimeMillis(); 175 while (System.currentTimeMillis() - start < 1000) { 176 lambdaHotSpot(); 177 s.hotSpot2(); 178 b.hotSpot3(); 179 Taz.hotSpot4(); 180 reflectHotSpots(); 181 182 // In JDK mainline, generated proxy classes are excluded from the AOT cache. 183 // In Leyden/premain, generated proxy classes included. The following code should 184 // work with either repos. 185 Integer i = (Integer)mapProxy.get(null); 186 counter += i.intValue(); 187 188 if (custInstance != null) { 189 // Classes loaded by custom loaders are included in the AOT cache 190 // but their array classes are excluded. 191 counter += custInstance.equals(null) ? 1 : 2; 192 } 193 194 if (custArrayInstance != null) { 195 if ((counter % 3) == 0) { 196 counter += (custArrayInstance instanceof String) ? 0 : 1; 197 } else { 198 counter += (custArrayInstance instanceof Object) ? 0 : 1; 199 } 200 } 201 } 202 203 return counter + s.m() + s.f + b.m() + b.f; 204 } 205 206 static void f() { 207 if (counter % 2 == 1) { 208 counter ++; 209 } 210 } 211 212 // Generated Lambda classes should be excluded from CDS preimage. 213 static void lambdaHotSpot() { 214 long start = System.currentTimeMillis(); 215 while (System.currentTimeMillis() - start < 20) { 216 doit(() -> counter ++ ); 217 } 218 } 219 220 static void doit(Runnable r) { 221 r.run(); 222 } 223 224 // All subclasses of jdk.jfr.Event are excluded from the CDS archive. 225 static class ShouldBeExcluded extends jdk.jfr.Event { 226 int f = (int)(System.currentTimeMillis()) + 123; 227 int m() { 228 return f + 456; 229 } 230 231 void hotSpot2() { 232 long start = System.currentTimeMillis(); 233 while (System.currentTimeMillis() - start < 20) { 234 for (int i = 0; i < 50000; i++) { 235 counter += i; 236 } 237 f(); 238 } 239 } 240 int func() { 241 return 1; 242 } 243 } 244 245 static class ShouldBeExcludedChild extends ShouldBeExcluded { 246 @Override 247 int func() { 248 return 2; 249 } 250 } 251 252 static class Bar { 253 int f = (int)(System.currentTimeMillis()) + 123; 254 int m() { 255 return f + 456; 256 } 257 258 void hotSpot3() { 259 long start = System.currentTimeMillis(); 260 while (System.currentTimeMillis() - start < 20) { 261 for (int i = 0; i < 50000; i++) { 262 counter += i; 263 } 264 f(); 265 } 266 } 267 } 268 269 static class Taz { 270 static ShouldBeExcluded m() { 271 // Taz should be excluded from the AOT cache because it has a verification constraint that 272 // "ShouldBeExcludedChild must be a subtype of ShouldBeExcluded", but ShouldBeExcluded is 273 // excluded from the AOT cache. 274 return new ShouldBeExcludedChild(); 275 } 276 static void hotSpot4() { 277 long start = System.currentTimeMillis(); 278 while (System.currentTimeMillis() - start < 20) { 279 for (int i = 0; i < 50000; i++) { 280 counter += i; 281 } 282 f(); 283 } 284 } 285 } 286 287 static volatile Object dummyObj; 288 289 static void reflectHotSpots() { 290 try { 291 // f.clazz points to an excluded class, so we should not archive any fields in 292 // the BadFieldSig 293 Field f = BadFieldSig.class.getDeclaredField("myField"); 294 Method m = BadMethodSig.class.getDeclaredMethod("myMethod", Object.class, ShouldBeExcluded.class); 295 296 // It's OK to archive the reflection data of this class even if its method signatures 297 // refers to a class that's not loaded at all during the assembly phase. 298 // Note: because the app did not reflect on the methods of GoodSig1, we don't 299 // AOT-generate method reflection data for GoodSig. 300 Field f2 = GoodSig1.class.getDeclaredField("myField"); 301 302 // Opposite case as GoodSig1. We should archive its reflection data 303 Method m2 = GoodSig2.class.getDeclaredMethod("myMethod", Object.class, Object.class); 304 305 long start = System.currentTimeMillis(); 306 while (System.currentTimeMillis() - start < 50) { 307 for (int i = 0; i < 50000; i++) { 308 dummyObj = f.get(null); 309 dummyObj = m.invoke(null, null, null); 310 dummyObj = f2.get(null); 311 dummyObj = m2.invoke(null, null, null); 312 } 313 } 314 } catch (Throwable t) { 315 throw new RuntimeException("Unexpected exception", t); 316 } 317 } 318 319 static class BadFieldSig { 320 static ShouldBeExcluded myField = new ShouldBeExcluded(); 321 } 322 323 static class BadMethodSig { 324 static String myMethod(Object o, ShouldBeExcluded s) { 325 return "Foofoo"; 326 } 327 } 328 329 static class GoodSig1 { 330 static Object myField = new Object(); 331 static void method(UnavailableClass1 arg) {} 332 static void method(UnavailableClass2[] arg) {} 333 } 334 335 static class GoodSig2 { 336 static String myMethod(Object o, Object o2) { 337 return "Foofoo"; 338 } 339 340 UnavailableClass2[] unusedField; 341 342 // This field has never been reflected up or resolved during the training run, so the 343 // array type GoodSig2[][][][] is not resolved during either the training run or the 344 // assembly phase. 345 GoodSig2[][][][] unusedField2; 346 } 347 } 348 } 349 350 // These classes are NOT part of app.jar, so they cannot be resolved by the app. 351 class UnavailableClass1 {} 352 class UnavailableClass2 {} 353