1 /* 2 * Copyright (c) 2024, 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 id=static 27 * @summary Dump time resolution of constant pool entries (Static CDS archive). 28 * @requires vm.cds 29 * @requires vm.cds.supports.aot.class.linking 30 * @requires vm.compMode != "Xcomp" 31 * @library /test/lib /test/hotspot/jtreg/runtime/cds/appcds/test-classes/ 32 * @build OldProvider OldClass OldConsumer StringConcatTestOld 33 * @build ResolvedConstants 34 * @run driver jdk.test.lib.helpers.ClassFileInstaller -jar app.jar 35 * ResolvedConstantsApp ResolvedConstantsFoo ResolvedConstantsBar 36 * MyInterface InterfaceWithClinit NormalClass 37 * OldProvider OldClass OldConsumer SubOfOldClass 38 * StringConcatTest StringConcatTestOld 39 * @run driver ResolvedConstants STATIC 40 */ 41 42 /* 43 * @test id=dynamic 44 * @summary Dump time resolution of constant pool entries (Dynamic CDS archive) 45 * @requires vm.cds 46 * @requires vm.cds.supports.aot.class.linking 47 * @requires vm.compMode != "Xcomp" 48 * @library /test/lib /test/hotspot/jtreg/runtime/cds/appcds/test-classes/ 49 * @build OldProvider OldClass OldConsumer StringConcatTestOld 50 * @build ResolvedConstants 51 * @run driver jdk.test.lib.helpers.ClassFileInstaller -jar app.jar 52 * ResolvedConstantsApp ResolvedConstantsFoo ResolvedConstantsBar 53 * MyInterface InterfaceWithClinit NormalClass 54 * OldProvider OldClass OldConsumer SubOfOldClass 55 * StringConcatTest StringConcatTestOld 56 * @build jdk.test.whitebox.WhiteBox 57 * @run driver jdk.test.lib.helpers.ClassFileInstaller jdk.test.whitebox.WhiteBox 58 * @run main/othervm -Dcds.app.tester.workflow=DYNAMIC -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI -Xbootclasspath/a:. ResolvedConstants DYNAMIC 59 */ 60 61 import java.util.function.Consumer; 62 import jdk.test.lib.cds.CDSOptions; 63 import jdk.test.lib.cds.CDSTestUtils; 64 import jdk.test.lib.cds.SimpleCDSAppTester; 65 import jdk.test.lib.helpers.ClassFileInstaller; 66 import jdk.test.lib.process.OutputAnalyzer; 67 68 public class ResolvedConstants { 69 static final String classList = "ResolvedConstants.classlist"; 70 static final String appJar = ClassFileInstaller.getJarPath("app.jar"); 71 static final String mainClass = ResolvedConstantsApp.class.getName(); 72 73 static boolean aotClassLinking; 74 public static void main(String[] args) throws Exception { 75 test(args, false); 76 test(args, true); 77 } 78 79 static void test(String[] args, boolean testMode) throws Exception { 80 aotClassLinking = testMode; 81 82 SimpleCDSAppTester.of("ResolvedConstantsApp" + (aotClassLinking ? "1" : "0")) 83 .addVmArgs(aotClassLinking ? "-XX:+AOTClassLinking" : "-XX:-AOTClassLinking", 84 "-Xlog:cds+resolve=trace", 85 "-Xlog:cds+class=debug") 86 .classpath(appJar) 87 .appCommandLine(mainClass) 88 .setAssemblyChecker((OutputAnalyzer out) -> { 89 checkAssemblyOutput(args, out); 90 }) 91 .setProductionChecker((OutputAnalyzer out) -> { 92 out.shouldContain("Hello ResolvedConstantsApp"); 93 }) 94 .run(args); 95 } 96 97 static void checkAssemblyOutput(String args[], OutputAnalyzer out) { 98 testGroup("Class References", out) 99 // Always resolve reference when a class references itself 100 .shouldMatch(ALWAYS("klass.* ResolvedConstantsApp app => ResolvedConstantsApp app")) 101 102 // Always resolve reference when a class references a super class 103 .shouldMatch(ALWAYS("klass.* ResolvedConstantsApp app => java/lang/Object boot")) 104 .shouldMatch(ALWAYS("klass.* ResolvedConstantsBar app => ResolvedConstantsFoo app")) 105 106 // Always resolve reference when a class references a super interface 107 .shouldMatch(ALWAYS("klass.* ResolvedConstantsApp app => java/lang/Runnable boot")) 108 109 /** premain allows static method pre-resolution 110 // Without -XX:+AOTClassLinking: 111 // java/lang/System is in the boot loader but ResolvedConstantsApp is loaded by the app loader. 112 // Even though System is in the vmClasses list, when ResolvedConstantsApp looks up 113 // "java/lang/System" in its ConstantPool, the app loader may not have resolved the System 114 // class yet (i.e., there's no initiaited class entry for System in the app loader's dictionary) 115 .shouldMatch(AOTLINK_ONLY("klass.* ResolvedConstantsApp .*java/lang/System")) 116 **/ 117 ; 118 119 testGroup("Field References", out) 120 // Always resolve references to fields in the current class or super class(es) 121 .shouldMatch(ALWAYS("field.* ResolvedConstantsBar => ResolvedConstantsBar.b:I")) 122 .shouldMatch(ALWAYS("field.* ResolvedConstantsBar => ResolvedConstantsBar.a:I")) 123 .shouldMatch(ALWAYS("field.* ResolvedConstantsBar => ResolvedConstantsFoo.a:I")) 124 .shouldMatch(ALWAYS("field.* ResolvedConstantsFoo => ResolvedConstantsFoo.a:I")) 125 126 // Resolve field references to child classes ONLY when using -XX:+AOTClassLinking 127 .shouldMatch(AOTLINK_ONLY("field.* ResolvedConstantsFoo => ResolvedConstantsBar.a:I")) 128 .shouldMatch(AOTLINK_ONLY("field.* ResolvedConstantsFoo => ResolvedConstantsBar.b:I")) 129 130 // Resolve field references to unrelated classes ONLY when using -XX:+AOTClassLinking 131 .shouldMatch(AOTLINK_ONLY("field.* ResolvedConstantsApp => ResolvedConstantsBar.a:I")) 132 .shouldMatch(AOTLINK_ONLY("field.* ResolvedConstantsApp => ResolvedConstantsBar.b:I")); 133 134 if (args[0].equals("DYNAMIC")) { 135 // AOT resolution of CP methods/indy references is not implemeted 136 return; 137 } 138 139 testGroup("Method References", out) 140 // Should resolve references to own constructor 141 .shouldMatch(ALWAYS("method.* ResolvedConstantsApp ResolvedConstantsApp.<init>:")) 142 // Should resolve references to super constructor 143 .shouldMatch(ALWAYS("method.* ResolvedConstantsApp java/lang/Object.<init>:")) 144 145 // Should resolve interface methods in VM classes 146 .shouldMatch(ALWAYS("interface method .* ResolvedConstantsApp java/lang/Runnable.run:")) 147 148 // Should resolve references to own non-static method (private or public) 149 .shouldMatch(ALWAYS("method.*: ResolvedConstantsBar ResolvedConstantsBar.doBar:")) 150 .shouldMatch(ALWAYS("method.*: ResolvedConstantsApp ResolvedConstantsApp.privateInstanceCall:")) 151 .shouldMatch(ALWAYS("method.*: ResolvedConstantsApp ResolvedConstantsApp.publicInstanceCall:")) 152 /** premain allows static method pre-resolution 153 // Should not resolve references to static method 154 .shouldNotMatch(ALWAYS("method.*: ResolvedConstantsApp ResolvedConstantsApp.staticCall:")) 155 **/ 156 157 // Should resolve references to method in super type 158 .shouldMatch(ALWAYS("method.*: ResolvedConstantsBar ResolvedConstantsFoo.doBar:")) 159 160 // Without -XX:+AOTClassLinking App class cannot resolve references to methods in boot classes: 161 // When the app class loader tries to resolve a class X that's normally loaded by 162 // the boot loader, it's possible for the app class loader to get a different copy of 163 // X (by using MethodHandles.Lookup.defineClass(), etc). Therefore, let's be on 164 // the side of safety and revert all such references. 165 .shouldMatch(AOTLINK_ONLY("method.*: ResolvedConstantsApp java/io/PrintStream.println:")) 166 .shouldMatch(AOTLINK_ONLY("method.*: ResolvedConstantsBar java/lang/Class.getName:")) 167 168 // Resole resolve methods in unrelated classes ONLY when using -XX:+AOTClassLinking 169 .shouldMatch(AOTLINK_ONLY("method.*: ResolvedConstantsApp ResolvedConstantsBar.doit:")) 170 171 // End --- 172 ; 173 174 175 // Indy References --- 176 if (aotClassLinking) { 177 /** premain allows Old classes to be linked 178 testGroup("Indy References", out) 179 .shouldContain("Cannot aot-resolve Lambda proxy because OldConsumer is excluded") 180 .shouldContain("Cannot aot-resolve Lambda proxy because OldProvider is excluded") 181 .shouldContain("Cannot aot-resolve Lambda proxy because OldClass is excluded") 182 .shouldContain("Cannot aot-resolve Lambda proxy of interface type InterfaceWithClinit") 183 .shouldMatch("klasses.* app *NormalClass[$][$]Lambda/.* hidden aot-linked inited") 184 .shouldNotMatch("klasses.* app *SubOfOldClass[$][$]Lambda/") 185 .shouldMatch("archived indy *CP entry.*StringConcatTest .* => java/lang/invoke/StringConcatFactory.makeConcatWithConstants") 186 .shouldNotMatch("archived indy *CP entry.*StringConcatTestOld .* => java/lang/invoke/StringConcatFactory.makeConcatWithConstants"); 187 **/ 188 } 189 } 190 191 static String ALWAYS(String s) { 192 return "cds,resolve.*archived " + s; 193 } 194 195 static String AOTLINK_ONLY(String s) { 196 if (aotClassLinking) { 197 return ALWAYS(s); 198 } else { 199 return "cds,resolve.*reverted " + s; 200 } 201 } 202 203 static OutputAnalyzer testGroup(String name, OutputAnalyzer out) { 204 System.out.println("Checking for: " + name); 205 return out; 206 } 207 } 208 209 class ResolvedConstantsApp implements Runnable { 210 public static void main(String args[]) { 211 System.out.println("Hello ResolvedConstantsApp"); 212 ResolvedConstantsApp app = new ResolvedConstantsApp(); 213 ResolvedConstantsApp.staticCall(); 214 app.privateInstanceCall(); 215 app.publicInstanceCall(); 216 Object a = app; 217 ((Runnable)a).run(); 218 219 ResolvedConstantsFoo foo = new ResolvedConstantsFoo(); 220 ResolvedConstantsBar bar = new ResolvedConstantsBar(); 221 bar.a ++; 222 bar.b ++; 223 bar.doit(); 224 225 testLambda(); 226 StringConcatTest.test(); 227 StringConcatTestOld.main(null); 228 } 229 private static void staticCall() {} 230 private void privateInstanceCall() {} 231 public void publicInstanceCall() {} 232 233 public void run() {} 234 235 static void testLambda() { 236 // The functional type used in the Lambda is an excluded class 237 OldProvider op = () -> { 238 return null; 239 }; 240 241 // A captured value is an instance of an excluded Class 242 OldClass c = new OldClass(); 243 Runnable r = () -> { 244 System.out.println("Test 1 " + c); 245 }; 246 r.run(); 247 248 // The functional interface accepts an argument that's an excluded class 249 MyInterface i = (o) -> { 250 System.out.println("Test 2 " + o); 251 }; 252 i.dispatch(c); 253 254 // Method reference to old class 255 OldConsumer oldConsumer = new OldConsumer(); 256 Consumer<String> wrapper = oldConsumer::consumeString; 257 wrapper.accept("Hello"); 258 259 // Lambda of interfaces that have <clinit> are not archived. 260 InterfaceWithClinit i2 = () -> { 261 System.out.println("Test 3"); 262 }; 263 i2.dispatch(); 264 265 // These two classes have almost identical source code, but 266 // only NormalClass should have its lambdas pre-resolved. 267 // SubOfOldClass is "old" -- it should be excluded from the AOT cache, 268 // so none of its lambda proxies should be cached 269 NormalClass.testLambda(); // Lambda proxy should be cached 270 SubOfOldClass.testLambda(); // Lambda proxy shouldn't be cached 271 } 272 } 273 274 class StringConcatTest { 275 static void test() { 276 System.out.println("StringConcatTest <concat> " + new StringConcatTest()); // concat should be aot-resolved 277 } 278 } 279 280 /* see StringConcatTestOld.jasm 281 282 class StringConcatTestOld { 283 public static void main(String args[]) { 284 // concat should be aot-resolved => the MethodType refers to an old class 285 System.out.println("StringConcatTestOld <concat> " + new OldConsumer()); 286 } 287 } 288 */ 289 290 class NormalClass { 291 static void testLambda() { 292 Runnable r = () -> { 293 System.out.println("NormalClass testLambda"); 294 }; 295 r.run(); 296 } 297 } 298 299 class SubOfOldClass extends OldClass { 300 static void testLambda() { 301 Runnable r = () -> { 302 System.out.println("SubOfOldClass testLambda"); 303 }; 304 r.run(); 305 } 306 } 307 308 interface MyInterface { 309 void dispatch(OldClass c); 310 } 311 312 interface InterfaceWithClinit { 313 static final long X = System.currentTimeMillis(); 314 void dispatch(); 315 default long dummy() { return X; } 316 } 317 318 class ResolvedConstantsFoo { 319 int a = 1; 320 void doit() { 321 } 322 323 void doBar(ResolvedConstantsBar bar) { 324 bar.a ++; 325 bar.b ++; 326 } 327 } 328 329 class ResolvedConstantsBar extends ResolvedConstantsFoo { 330 int b = 2; 331 void doit() { 332 System.out.println("Hello ResolvedConstantsBar and " + ResolvedConstantsFoo.class.getName()); 333 System.out.println("a = " + a); 334 System.out.println("a = " + ((ResolvedConstantsFoo)this).a); 335 System.out.println("b = " + b); 336 337 doBar(this); 338 339 ((ResolvedConstantsFoo)this).doBar(this); 340 } 341 }