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