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