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