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 * @comment work around JDK-8345635 30 * @requires !vm.jvmci.enabled 31 * @library /test/jdk/lib/testlibrary /test/lib 32 * /test/hotspot/jtreg/runtime/cds/appcds/aotCache/test-classes 33 * @build ExcludedClasses CustyWithLoop 34 * @run driver jdk.test.lib.helpers.ClassFileInstaller -jar app.jar 35 * TestApp 36 * TestApp$Foo 37 * TestApp$Foo$Bar 38 * TestApp$Foo$ShouldBeExcluded 39 * TestApp$MyInvocationHandler 40 * @run driver jdk.test.lib.helpers.ClassFileInstaller -jar cust.jar 41 * CustyWithLoop 42 * @run driver ExcludedClasses 43 */ 44 45 import java.io.File; 46 import java.lang.reflect.Array; 47 import java.lang.reflect.InvocationHandler; 48 import java.lang.reflect.Method; 49 import java.lang.reflect.Proxy; 50 import java.net.URL; 51 import java.net.URLClassLoader; 52 import java.security.ProtectionDomain; 53 import java.util.Map; 54 55 import jdk.jfr.Event; 56 import jdk.test.lib.cds.CDSAppTester; 57 import jdk.test.lib.helpers.ClassFileInstaller; 58 import jdk.test.lib.process.OutputAnalyzer; 59 60 public class ExcludedClasses { 61 static final String appJar = ClassFileInstaller.getJarPath("app.jar"); 62 static final String mainClass = "TestApp"; 63 64 public static void main(String[] args) throws Exception { 65 Tester tester = new Tester(); 66 tester.runAOTWorkflow(); 67 } 68 69 static class Tester extends CDSAppTester { 70 public Tester() { 71 super(mainClass); 72 } 73 74 @Override 75 public String classpath(RunMode runMode) { 76 return appJar; 77 } 78 79 @Override 80 public String[] vmArgs(RunMode runMode) { 81 return new String[] { 82 "-Xlog:cds+resolve=trace", 83 }; 84 } 85 86 @Override 87 public String[] appCommandLine(RunMode runMode) { 88 return new String[] { 89 mainClass, runMode.name() 90 }; 91 } 92 93 @Override 94 public void checkExecution(OutputAnalyzer out, RunMode runMode) { 95 if (isDumping(runMode)) { 96 out.shouldNotMatch("cds,resolve.*archived field.*TestApp.Foo => TestApp.Foo.ShouldBeExcluded.f:I"); 97 } 98 } 99 } 100 } 101 102 class TestApp { 103 static volatile Object custInstance; 104 static volatile Object custArrayInstance; 105 106 public static void main(String args[]) throws Exception { 107 // In new workflow, classes from custom loaders are passed from the preimage 108 // to the final image. See ClassPrelinker::record_unregistered_klasses(). 109 custInstance = initFromCustomLoader(); 110 custArrayInstance = Array.newInstance(custInstance.getClass(), 0); 111 System.out.println(custArrayInstance); 112 System.out.println("Counter = " + Foo.hotSpot()); 113 } 114 115 static Object initFromCustomLoader() throws Exception { 116 String path = "cust.jar"; 117 URL url = new File(path).toURI().toURL(); 118 URL[] urls = new URL[] {url}; 119 URLClassLoader urlClassLoader = 120 new URLClassLoader("MyLoader", urls, null); 121 Class c = Class.forName("CustyWithLoop", true, urlClassLoader); 122 return c.newInstance(); 123 } 124 125 static class MyInvocationHandler implements InvocationHandler { 126 volatile static int cnt; 127 128 public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { 129 long start = System.currentTimeMillis(); 130 while (System.currentTimeMillis() - start < 20) { 131 cnt += 2; 132 for (int i = 0; i < 1000; i++) { 133 int n = cnt - 2; 134 if (n < 2) { 135 n = 2; 136 } 137 cnt += (i + cnt) % n + cnt % 2; 138 } 139 } 140 return Integer.valueOf(cnt); 141 } 142 } 143 144 static class Foo { 145 volatile static int counter; 146 static Class c = ShouldBeExcluded.class; 147 148 static Map mapProxy = (Map) Proxy.newProxyInstance( 149 Foo.class.getClassLoader(), 150 new Class[] { Map.class }, 151 new MyInvocationHandler()); 152 153 static int hotSpot() { 154 ShouldBeExcluded s = new ShouldBeExcluded(); 155 Bar b = new Bar(); 156 157 long start = System.currentTimeMillis(); 158 while (System.currentTimeMillis() - start < 1000) { 159 lambdaHotSpot(); 160 s.hotSpot2(); 161 b.hotSpot3(); 162 163 // In JDK mainline, generated proxy classes are excluded from the AOT cache. 164 // In Leyden/premain, generated proxy classes included. The following code should 165 // work with either repos. 166 Integer i = (Integer)mapProxy.get(null); 167 counter += i.intValue(); 168 169 if (custInstance != null) { 170 // Classes loaded by custom loaders are included included in the AOT cache 171 // but their array classes are excluded. 172 counter += custInstance.equals(null) ? 1 : 2; 173 } 174 175 if (custArrayInstance != null) { 176 if ((counter % 3) == 0) { 177 counter += (custArrayInstance instanceof String) ? 0 : 1; 178 } else { 179 counter += (custArrayInstance instanceof Object) ? 0 : 1; 180 } 181 } 182 } 183 184 return counter + s.m() + s.f + b.m() + b.f; 185 } 186 187 static void f() { 188 if (counter % 2 == 1) { 189 counter ++; 190 } 191 } 192 193 // Generated Lambda classes should be excluded from CDS preimage. 194 static void lambdaHotSpot() { 195 long start = System.currentTimeMillis(); 196 while (System.currentTimeMillis() - start < 20) { 197 doit(() -> counter ++ ); 198 } 199 } 200 201 static void doit(Runnable r) { 202 r.run(); 203 } 204 205 // All subclasses of jdk.jfr.Event are excluded from the CDS archive. 206 static class ShouldBeExcluded extends jdk.jfr.Event { 207 int f = (int)(System.currentTimeMillis()) + 123; 208 int m() { 209 return f + 456; 210 } 211 212 void hotSpot2() { 213 long start = System.currentTimeMillis(); 214 while (System.currentTimeMillis() - start < 20) { 215 for (int i = 0; i < 50000; i++) { 216 counter += i; 217 } 218 f(); 219 } 220 } 221 } 222 223 static class Bar { 224 int f = (int)(System.currentTimeMillis()) + 123; 225 int m() { 226 return f + 456; 227 } 228 229 void hotSpot3() { 230 long start = System.currentTimeMillis(); 231 while (System.currentTimeMillis() - start < 20) { 232 for (int i = 0; i < 50000; i++) { 233 counter += i; 234 } 235 f(); 236 } 237 } 238 } 239 } 240 } 241