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 // Without -XX:+AOTClassLinking: 110 // java/lang/System is in the boot loader but ResolvedConstantsApp is loaded by the app loader. 111 // Even though System is in the vmClasses list, when ResolvedConstantsApp looks up 112 // "java/lang/System" in its ConstantPool, the app loader may not have resolved the System 113 // class yet (i.e., there's no initiaited class entry for System in the app loader's dictionary) 114 .shouldMatch(AOTLINK_ONLY("klass.* ResolvedConstantsApp .*java/lang/System")); 115 116 testGroup("Field References", out) 117 // Always resolve references to fields in the current class or super class(es) 118 .shouldMatch(ALWAYS("field.* ResolvedConstantsBar => ResolvedConstantsBar.b:I")) 119 .shouldMatch(ALWAYS("field.* ResolvedConstantsBar => ResolvedConstantsBar.a:I")) 120 .shouldMatch(ALWAYS("field.* ResolvedConstantsBar => ResolvedConstantsFoo.a:I")) 121 .shouldMatch(ALWAYS("field.* ResolvedConstantsFoo => ResolvedConstantsFoo.a:I")) 122 123 // Resolve field references to child classes ONLY when using -XX:+AOTClassLinking 124 .shouldMatch(AOTLINK_ONLY("field.* ResolvedConstantsFoo => ResolvedConstantsBar.a:I")) 125 .shouldMatch(AOTLINK_ONLY("field.* ResolvedConstantsFoo => ResolvedConstantsBar.b:I")) 126 127 // Resolve field references to unrelated classes ONLY when using -XX:+AOTClassLinking 128 .shouldMatch(AOTLINK_ONLY("field.* ResolvedConstantsApp => ResolvedConstantsBar.a:I")) 129 .shouldMatch(AOTLINK_ONLY("field.* ResolvedConstantsApp => ResolvedConstantsBar.b:I")); 130 131 if (args[0].equals("DYNAMIC")) { 132 // AOT resolution of CP methods/indy references is not implemeted 133 return; 134 } 135 136 testGroup("Method References", out) 137 // Should resolve references to own constructor 138 .shouldMatch(ALWAYS("method.* ResolvedConstantsApp ResolvedConstantsApp.<init>:")) 139 // Should resolve references to super constructor 140 .shouldMatch(ALWAYS("method.* ResolvedConstantsApp java/lang/Object.<init>:")) 141 142 // Should resolve interface methods in VM classes 143 .shouldMatch(ALWAYS("interface method .* ResolvedConstantsApp java/lang/Runnable.run:")) 144 145 // Should resolve references to own non-static method (private or public) 146 .shouldMatch(ALWAYS("method.*: ResolvedConstantsBar ResolvedConstantsBar.doBar:")) 147 .shouldMatch(ALWAYS("method.*: ResolvedConstantsApp ResolvedConstantsApp.privateInstanceCall:")) 148 .shouldMatch(ALWAYS("method.*: ResolvedConstantsApp ResolvedConstantsApp.publicInstanceCall:")) 149 150 // Should not resolve references to static method 151 .shouldNotMatch(ALWAYS("method.*: ResolvedConstantsApp ResolvedConstantsApp.staticCall:")) 152 153 // Should resolve references to method in super type 154 .shouldMatch(ALWAYS("method.*: ResolvedConstantsBar ResolvedConstantsFoo.doBar:")) 155 156 // Without -XX:+AOTClassLinking App class cannot resolve references to methods in boot classes: 157 // When the app class loader tries to resolve a class X that's normally loaded by 158 // the boot loader, it's possible for the app class loader to get a different copy of 159 // X (by using MethodHandles.Lookup.defineClass(), etc). Therefore, let's be on 160 // the side of safety and revert all such references. 161 .shouldMatch(AOTLINK_ONLY("method.*: ResolvedConstantsApp java/io/PrintStream.println:")) 162 .shouldMatch(AOTLINK_ONLY("method.*: ResolvedConstantsBar java/lang/Class.getName:")) 163 164 // Resole resolve methods in unrelated classes ONLY when using -XX:+AOTClassLinking 165 .shouldMatch(AOTLINK_ONLY("method.*: ResolvedConstantsApp ResolvedConstantsBar.doit:")) 166 167 // End --- 168 ; 169 170 171 // Indy References --- 172 if (aotClassLinking) { 173 testGroup("Indy References", out) 174 .shouldContain("Cannot aot-resolve Lambda proxy because OldConsumer is excluded") 175 .shouldContain("Cannot aot-resolve Lambda proxy because OldProvider is excluded") 176 .shouldContain("Cannot aot-resolve Lambda proxy because OldClass is excluded") 177 .shouldContain("Cannot aot-resolve Lambda proxy of interface type InterfaceWithClinit") 178 .shouldMatch("klasses.* app *NormalClass[$][$]Lambda/.* hidden aot-linked inited") 179 .shouldNotMatch("klasses.* app *SubOfOldClass[$][$]Lambda/") 180 .shouldMatch("archived indy *CP entry.*StringConcatTest .* => java/lang/invoke/StringConcatFactory.makeConcatWithConstants") 181 .shouldNotMatch("archived indy *CP entry.*StringConcatTestOld .* => java/lang/invoke/StringConcatFactory.makeConcatWithConstants"); 182 } 183 } 184 185 static String ALWAYS(String s) { 186 return "cds,resolve.*archived " + s; 187 } 188 189 static String AOTLINK_ONLY(String s) { 190 if (aotClassLinking) { 191 return ALWAYS(s); 192 } else { 193 return "cds,resolve.*reverted " + s; 194 } 195 } 196 197 static OutputAnalyzer testGroup(String name, OutputAnalyzer out) { 198 System.out.println("Checking for: " + name); 199 return out; 200 } 201 } 202 203 class ResolvedConstantsApp implements Runnable { 204 public static void main(String args[]) { 205 System.out.println("Hello ResolvedConstantsApp"); 206 ResolvedConstantsApp app = new ResolvedConstantsApp(); 207 ResolvedConstantsApp.staticCall(); 208 app.privateInstanceCall(); 209 app.publicInstanceCall(); 210 Object a = app; 211 ((Runnable)a).run(); 212 213 ResolvedConstantsFoo foo = new ResolvedConstantsFoo(); 214 ResolvedConstantsBar bar = new ResolvedConstantsBar(); 215 bar.a ++; 216 bar.b ++; 217 bar.doit(); 218 219 testLambda(); 220 StringConcatTest.test(); 221 StringConcatTestOld.main(null); 222 } 223 private static void staticCall() {} 224 private void privateInstanceCall() {} 225 public void publicInstanceCall() {} 226 227 public void run() {} 228 229 static void testLambda() { 230 // The functional type used in the Lambda is an excluded class 231 OldProvider op = () -> { 232 return null; 233 }; 234 235 // A captured value is an instance of an excluded Class 236 OldClass c = new OldClass(); 237 Runnable r = () -> { 238 System.out.println("Test 1 " + c); 239 }; 240 r.run(); 241 242 // The functional interface accepts an argument that's an excluded class 243 MyInterface i = (o) -> { 244 System.out.println("Test 2 " + o); 245 }; 246 i.dispatch(c); 247 248 // Method reference to old class 249 OldConsumer oldConsumer = new OldConsumer(); 250 Consumer<String> wrapper = oldConsumer::consumeString; 251 wrapper.accept("Hello"); 252 253 // Lambda of interfaces that have <clinit> are not archived. 254 InterfaceWithClinit i2 = () -> { 255 System.out.println("Test 3"); 256 }; 257 i2.dispatch(); 258 259 // These two classes have almost identical source code, but 260 // only NormalClass should have its lambdas pre-resolved. 261 // SubOfOldClass is "old" -- it should be excluded from the AOT cache, 262 // so none of its lambda proxies should be cached 263 NormalClass.testLambda(); // Lambda proxy should be cached 264 SubOfOldClass.testLambda(); // Lambda proxy shouldn't be cached 265 } 266 } 267 268 class StringConcatTest { 269 static void test() { 270 System.out.println("StringConcatTest <concat> " + new StringConcatTest()); // concat should be aot-resolved 271 } 272 } 273 274 /* see StringConcatTestOld.jasm 275 276 class StringConcatTestOld { 277 public static void main(String args[]) { 278 // concat should be aot-resolved => the MethodType refers to an old class 279 System.out.println("StringConcatTestOld <concat> " + new OldConsumer()); 280 } 281 } 282 */ 283 284 class NormalClass { 285 static void testLambda() { 286 Runnable r = () -> { 287 System.out.println("NormalClass testLambda"); 288 }; 289 r.run(); 290 } 291 } 292 293 class SubOfOldClass extends OldClass { 294 static void testLambda() { 295 Runnable r = () -> { 296 System.out.println("SubOfOldClass testLambda"); 297 }; 298 r.run(); 299 } 300 } 301 302 interface MyInterface { 303 void dispatch(OldClass c); 304 } 305 306 interface InterfaceWithClinit { 307 static final long X = System.currentTimeMillis(); 308 void dispatch(); 309 default long dummy() { return X; } 310 } 311 312 class ResolvedConstantsFoo { 313 int a = 1; 314 void doit() { 315 } 316 317 void doBar(ResolvedConstantsBar bar) { 318 bar.a ++; 319 bar.b ++; 320 } 321 } 322 323 class ResolvedConstantsBar extends ResolvedConstantsFoo { 324 int b = 2; 325 void doit() { 326 System.out.println("Hello ResolvedConstantsBar and " + ResolvedConstantsFoo.class.getName()); 327 System.out.println("a = " + a); 328 System.out.println("a = " + ((ResolvedConstantsFoo)this).a); 329 System.out.println("b = " + b); 330 331 doBar(this); 332 333 ((ResolvedConstantsFoo)this).doBar(this); 334 } 335 }