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 resolutiom 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
 32  * @build ResolvedConstants
 33  * @run driver jdk.test.lib.helpers.ClassFileInstaller -jar app.jar ResolvedConstantsApp ResolvedConstantsFoo ResolvedConstantsBar
 34  * @run driver ResolvedConstants
 35  */
 36 
 37 import jdk.test.lib.cds.CDSOptions;
 38 import jdk.test.lib.cds.CDSTestUtils;
 39 import jdk.test.lib.helpers.ClassFileInstaller;
 40 
 41 public class ResolvedConstants {
 42     static final String classList = "ResolvedConstants.classlist";
 43     static final String appJar = ClassFileInstaller.getJarPath("app.jar");
 44     static final String mainClass = ResolvedConstantsApp.class.getName();
 45 
 46     static boolean aotClassLinking;
 47     public static void main(String[] args) throws Exception {
 48         test(false);
 49         test(true);
 50     }
 51 
 52     static void test(boolean testMode) throws Exception {
 53         aotClassLinking = testMode;
 54         CDSTestUtils.dumpClassList(classList, "-cp", appJar, mainClass)
 55             .assertNormalExit(output -> {
 56                 output.shouldContain("Hello ResolvedConstantsApp");
 57             });
 58 
 59         CDSOptions opts = (new CDSOptions())
 60             .addPrefix("-XX:ExtraSharedClassListFile=" + classList,
 61                        "-cp", appJar,
 62                        "-Xlog:cds+resolve=trace");
 63         if (aotClassLinking) {
 64             opts.addPrefix("-XX:+AOTClassLinking");
 65         } else {
 66             opts.addPrefix("-XX:-AOTClassLinking");
 67         }
 68 
 69         CDSTestUtils.createArchiveAndCheck(opts)
 70           // Class References ---
 71 
 72             // Always resolve reference when a class references itself
 73             .shouldMatch(ALWAYS("klass.* ResolvedConstantsApp app => ResolvedConstantsApp app"))
 74 
 75             // Always resolve reference when a class references a super class
 76             .shouldMatch(ALWAYS("klass.* ResolvedConstantsApp app => java/lang/Object boot"))
 77             .shouldMatch(ALWAYS("klass.* ResolvedConstantsBar app => ResolvedConstantsFoo app"))
 78 
 79             // Always resolve reference when a class references a super interface
 80             .shouldMatch(ALWAYS("klass.* ResolvedConstantsApp app => java/lang/Runnable boot"))
 81 
 82 /** premain allows static method pre-resolution
 83 
 84             // Without -XX:+AOTClassLinking:
 85             //   java/lang/System is in the boot loader but ResolvedConstantsApp is loaded by the app loader.
 86             //   Even though System is in the vmClasses list, when ResolvedConstantsApp looks up
 87             //   "java/lang/System" in its ConstantPool, the app loader may not have resolved the System
 88             //   class yet (i.e., there's no initiaited class entry for System in the app loader's dictionary)
 89             .shouldMatch(AOTLINK_ONLY("klass.* ResolvedConstantsApp .*java/lang/System"))
 90 **/
 91           // Field References ---
 92 
 93             // Always resolve references to fields in the current class or super class(es)
 94             .shouldMatch(ALWAYS("field.* ResolvedConstantsBar => ResolvedConstantsBar.b:I"))
 95             .shouldMatch(ALWAYS("field.* ResolvedConstantsBar => ResolvedConstantsBar.a:I"))
 96             .shouldMatch(ALWAYS("field.* ResolvedConstantsBar => ResolvedConstantsFoo.a:I"))
 97             .shouldMatch(ALWAYS("field.* ResolvedConstantsFoo => ResolvedConstantsFoo.a:I"))
 98 
 99             // Resolve field references to child classes ONLY when using -XX:+AOTClassLinking
100             .shouldMatch(AOTLINK_ONLY("field.* ResolvedConstantsFoo => ResolvedConstantsBar.a:I"))
101             .shouldMatch(AOTLINK_ONLY("field.* ResolvedConstantsFoo => ResolvedConstantsBar.b:I"))
102 
103             // Resolve field references to unrelated classes ONLY when using -XX:+AOTClassLinking
104             .shouldMatch(AOTLINK_ONLY("field.* ResolvedConstantsApp => ResolvedConstantsBar.a:I"))
105             .shouldMatch(AOTLINK_ONLY("field.* ResolvedConstantsApp => ResolvedConstantsBar.b:I"))
106 
107           // Method References ---
108 
109             // Should resolve references to own constructor
110             .shouldMatch(ALWAYS("method.* ResolvedConstantsApp ResolvedConstantsApp.<init>:"))
111             // Should resolve references to super constructor
112             .shouldMatch(ALWAYS("method.* ResolvedConstantsApp java/lang/Object.<init>:"))
113 
114             // Should resolve interface methods in VM classes
115             .shouldMatch(ALWAYS("interface method .* ResolvedConstantsApp java/lang/Runnable.run:"))
116 
117             // Should resolve references to own non-static method (private or public)
118             .shouldMatch(ALWAYS("method.*: ResolvedConstantsBar ResolvedConstantsBar.doBar:"))
119             .shouldMatch(ALWAYS("method.*: ResolvedConstantsApp ResolvedConstantsApp.privateInstanceCall:"))
120             .shouldMatch(ALWAYS("method.*: ResolvedConstantsApp ResolvedConstantsApp.publicInstanceCall:"))
121 
122 /** premain allows static method pre-resolution
123 
124             // Should not resolve references to static method
125             .shouldNotMatch(ALWAYS("method.*: ResolvedConstantsApp ResolvedConstantsApp.staticCall:"))
126 **/
127 
128             // Should resolve references to method in super type
129             .shouldMatch(ALWAYS("method.*: ResolvedConstantsBar ResolvedConstantsFoo.doBar:"))
130 
131             // Without -XX:+AOTClassLinking App class cannot resolve references to methods in boot classes:
132             //    When the app class loader tries to resolve a class X that's normally loaded by
133             //    the boot loader, it's possible for the app class loader to get a different copy of
134             //    X (by using MethodHandles.Lookup.defineClass(), etc). Therefore, let's be on
135             //    the side of safety and revert all such references.
136             .shouldMatch(AOTLINK_ONLY("method.*: ResolvedConstantsApp java/io/PrintStream.println:"))
137             .shouldMatch(AOTLINK_ONLY("method.*: ResolvedConstantsBar java/lang/Class.getName:"))
138 
139             // Resole resolve methods in unrelated classes ONLY when using -XX:+AOTClassLinking
140             .shouldMatch(AOTLINK_ONLY("method.*: ResolvedConstantsApp ResolvedConstantsBar.doit:"))
141 
142           // End ---
143             ;
144     }
145 
146     static String ALWAYS(String s) {
147         return "cds,resolve.*archived " + s;
148     }
149 
150     static String AOTLINK_ONLY(String s) {
151         if (aotClassLinking) {
152             return ALWAYS(s);
153         } else {
154             return "cds,resolve.*reverted " + s;
155         }
156     }
157 }
158 
159 class ResolvedConstantsApp implements Runnable {
160     public static void main(String args[]) {
161         System.out.println("Hello ResolvedConstantsApp");
162         ResolvedConstantsApp app = new ResolvedConstantsApp();
163         ResolvedConstantsApp.staticCall();
164         app.privateInstanceCall();
165         app.publicInstanceCall();
166         Object a = app;
167         ((Runnable)a).run();
168 
169         ResolvedConstantsFoo foo = new ResolvedConstantsFoo();
170         ResolvedConstantsBar bar = new ResolvedConstantsBar();
171         bar.a ++;
172         bar.b ++;
173         bar.doit();
174     }
175     private static void staticCall() {}
176     private void privateInstanceCall() {}
177     public void publicInstanceCall() {}
178 
179     public void run() {}
180 }
181 
182 class ResolvedConstantsFoo {
183     int a = 1;
184     void doit() {
185     }
186 
187     void doBar(ResolvedConstantsBar bar) {
188         bar.a ++;
189         bar.b ++;
190     }
191 }
192 
193 class ResolvedConstantsBar extends ResolvedConstantsFoo {
194     int b = 2;
195     void doit() {
196         System.out.println("Hello ResolvedConstantsBar and " + ResolvedConstantsFoo.class.getName());
197         System.out.println("a = " + a);
198         System.out.println("a = " + ((ResolvedConstantsFoo)this).a);
199         System.out.println("b = " + b);
200 
201         doBar(this);
202 
203         ((ResolvedConstantsFoo)this).doBar(this);
204     }
205 }