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 /** premain allows static method pre-resolution
 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 /** premain allows static method pre-resolution
129             // Should not resolve references to static method
130             .shouldNotMatch(ALWAYS("method.*: ResolvedConstantsApp ResolvedConstantsApp.staticCall:"))
131 **/
132 
133             // Should resolve references to method in super type
134             .shouldMatch(ALWAYS("method.*: ResolvedConstantsBar ResolvedConstantsFoo.doBar:"))
135 
136             // Without -XX:+AOTClassLinking App class cannot resolve references to methods in boot classes:
137             //    When the app class loader tries to resolve a class X that's normally loaded by
138             //    the boot loader, it's possible for the app class loader to get a different copy of
139             //    X (by using MethodHandles.Lookup.defineClass(), etc). Therefore, let's be on
140             //    the side of safety and revert all such references.
141             .shouldMatch(AOTLINK_ONLY("method.*: ResolvedConstantsApp java/io/PrintStream.println:"))
142             .shouldMatch(AOTLINK_ONLY("method.*: ResolvedConstantsBar java/lang/Class.getName:"))
143 
144             // Resole resolve methods in unrelated classes ONLY when using -XX:+AOTClassLinking
145             .shouldMatch(AOTLINK_ONLY("method.*: ResolvedConstantsApp ResolvedConstantsBar.doit:"))
146 
147           // End ---
148             ;
149 
150 
151         // Indy References ---
152         if (aotClassLinking) {
153 /** premain allows Old classes to be linked
154             out.shouldContain("Cannot aot-resolve Lambda proxy because OldConsumer is excluded")
155                .shouldContain("Cannot aot-resolve Lambda proxy because OldProvider is excluded")
156                .shouldContain("Cannot aot-resolve Lambda proxy because OldClass is excluded")
157                .shouldContain("Cannot aot-resolve Lambda proxy of interface type InterfaceWithClinit")
158                .shouldMatch("klasses.* app *NormalClass[$][$]Lambda/.* hidden aot-linked inited")
159                .shouldNotMatch("klasses.* app *SubOfOldClass[$][$]Lambda/")
160                .shouldMatch("archived indy *CP entry.*StringConcatTest .* => java/lang/invoke/StringConcatFactory.makeConcatWithConstants")
161                .shouldNotMatch("archived indy *CP entry.*StringConcatTestOld .* => java/lang/invoke/StringConcatFactory.makeConcatWithConstants");
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         StringConcatTest.test();
197         StringConcatTestOld.main(null);
198     }
199     private static void staticCall() {}
200     private void privateInstanceCall() {}
201     public void publicInstanceCall() {}
202 
203     public void run() {}
204 
205     static void testLambda() {
206         // The functional type used in the Lambda is an excluded class
207         OldProvider op = () -> {
208             return null;
209         };
210 
211         // A captured value is an instance of an excluded Class
212         OldClass c = new OldClass();
213         Runnable r = () -> {
214             System.out.println("Test 1 " + c);
215         };
216         r.run();
217 
218         // The functional interface accepts an argument that's an excluded class
219         MyInterface i = (o) -> {
220             System.out.println("Test 2 " + o);
221         };
222         i.dispatch(c);
223 
224         // Method reference to old class
225         OldConsumer oldConsumer = new OldConsumer();
226         Consumer<String> wrapper = oldConsumer::consumeString;
227         wrapper.accept("Hello");
228 
229         // Lambda of interfaces that have <clinit> are not archived.
230         InterfaceWithClinit i2 = () -> {
231             System.out.println("Test 3");
232         };
233         i2.dispatch();
234 
235         // These two classes have almost identical source code, but
236         // only NormalClass should have its lambdas pre-resolved.
237         // SubOfOldClass is "old" -- it should be excluded from the AOT cache,
238         // so none of its lambda proxies should be cached
239         NormalClass.testLambda();   // Lambda proxy should be cached
240         SubOfOldClass.testLambda(); // Lambda proxy shouldn't be cached
241     }
242 }
243 
244 class StringConcatTest {
245     static void test() {
246         System.out.println("StringConcatTest <concat> " + new StringConcatTest()); // concat should be aot-resolved
247     }
248 }
249 
250 /* see StringConcatTestOld.jasm
251 
252 class StringConcatTestOld {
253     public static void main(String args[]) {
254         // concat should be aot-resolved => the MethodType refers to an old class
255         System.out.println("StringConcatTestOld <concat> " + new OldConsumer());
256     }
257 }
258 */
259 
260 class NormalClass {
261     static void testLambda() {
262         Runnable r = () -> {
263             System.out.println("NormalClass testLambda");
264         };
265         r.run();
266     }
267 }
268 
269 class SubOfOldClass extends OldClass {
270     static void testLambda() {
271         Runnable r = () -> {
272             System.out.println("SubOfOldClass testLambda");
273         };
274         r.run();
275     }
276 }
277 
278 interface MyInterface {
279     void dispatch(OldClass c);
280 }
281 
282 interface InterfaceWithClinit {
283     static final long X = System.currentTimeMillis();
284     void dispatch();
285     default long dummy() { return X; }
286 }
287 
288 class ResolvedConstantsFoo {
289     int a = 1;
290     void doit() {
291     }
292 
293     void doBar(ResolvedConstantsBar bar) {
294         bar.a ++;
295         bar.b ++;
296     }
297 }
298 
299 class ResolvedConstantsBar extends ResolvedConstantsFoo {
300     int b = 2;
301     void doit() {
302         System.out.println("Hello ResolvedConstantsBar and " + ResolvedConstantsFoo.class.getName());
303         System.out.println("a = " + a);
304         System.out.println("a = " + ((ResolvedConstantsFoo)this).a);
305         System.out.println("b = " + b);
306 
307         doBar(this);
308 
309         ((ResolvedConstantsFoo)this).doBar(this);
310     }
311 }