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