< prev index next >

test/hotspot/jtreg/runtime/cds/appcds/resolvedConstants/ResolvedConstants.java

Print this page

  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.compMode != "Xcomp"
 30  * @library /test/lib
 31  * @build ResolvedConstants
 32  * @run driver jdk.test.lib.helpers.ClassFileInstaller -jar app.jar ResolvedConstantsApp ResolvedConstantsFoo ResolvedConstantsBar
 33  * @run driver ResolvedConstants
 34  */
 35 
 36 import jdk.test.lib.cds.CDSOptions;
 37 import jdk.test.lib.cds.CDSTestUtils;
 38 import jdk.test.lib.helpers.ClassFileInstaller;
 39 
 40 public class ResolvedConstants {
 41     static final String classList = "ResolvedConstants.classlist";
 42     static final String appJar = ClassFileInstaller.getJarPath("app.jar");
 43     static final String mainClass = ResolvedConstantsApp.class.getName();
 44 

 45     public static void main(String[] args) throws Exception {
 46         // dump class list





 47         CDSTestUtils.dumpClassList(classList, "-cp", appJar, mainClass)
 48             .assertNormalExit(output -> {
 49                 output.shouldContain("Hello ResolvedConstantsApp");
 50             });
 51 
 52         CDSOptions opts = (new CDSOptions())
 53             .addPrefix("-XX:ExtraSharedClassListFile=" + classList,
 54                        "-cp", appJar,
 55                        "-Xlog:cds+resolve=trace");






 56         CDSTestUtils.createArchiveAndCheck(opts)
 57           // Class References ---
 58 
 59             // Always resolve reference when a class references itself
 60             .shouldMatch("cds,resolve.*archived klass.* ResolvedConstantsApp app => ResolvedConstantsApp app")
 61 
 62             // Always resolve reference when a class references a super class
 63             .shouldMatch("cds,resolve.*archived klass.* ResolvedConstantsApp app => java/lang/Object boot")
 64             .shouldMatch("cds,resolve.*archived klass.* ResolvedConstantsBar app => ResolvedConstantsFoo app")
 65 
 66             // Always resolve reference when a class references a super interface
 67             .shouldMatch("cds,resolve.*archived klass.* ResolvedConstantsApp app => java/lang/Runnable boot")
 68 
 69             // java/lang/System is in the root loader but ResolvedConstantsApp is loaded by the app loader.
 70             // Even though System is in the vmClasses list, when ResolvedConstantsApp looks up
 71             // "java/lang/System" in its ConstantPool, the app loader may not have resolved the System
 72             // class yet (i.e., there's no initiaited class entry for System in the app loader's dictionary)
 73             .shouldMatch("cds,resolve.*reverted klass.* ResolvedConstantsApp .*java/lang/System")
 74 







 75           // Field References ---
 76 
 77             // Always resolve references to fields in the current class or super class(es)
 78             .shouldMatch("cds,resolve.*archived field.* ResolvedConstantsBar => ResolvedConstantsBar.b:I")
 79             .shouldMatch("cds,resolve.*archived field.* ResolvedConstantsBar => ResolvedConstantsBar.a:I")
 80             .shouldMatch("cds,resolve.*archived field.* ResolvedConstantsBar => ResolvedConstantsFoo.a:I")

 81 
 82             // Do not resolve field references to child classes
 83             .shouldMatch("cds,resolve.*archived field.* ResolvedConstantsFoo => ResolvedConstantsFoo.a:I")
 84             .shouldMatch("cds,resolve.*reverted field.* ResolvedConstantsFoo    ResolvedConstantsBar.a:I")
 85             .shouldMatch("cds,resolve.*reverted field.* ResolvedConstantsFoo    ResolvedConstantsBar.b:I")
 86 
 87             // Do not resolve field references to unrelated classes
 88             .shouldMatch("cds,resolve.*reverted field.* ResolvedConstantsApp    ResolvedConstantsBar.a:I")
 89             .shouldMatch("cds,resolve.*reverted field.* ResolvedConstantsApp    ResolvedConstantsBar.b:I")
 90 
 91           // Method References ---
 92 
 93             // Should resolve references to own constructor
 94             .shouldMatch("cds,resolve.*archived method .* ResolvedConstantsApp ResolvedConstantsApp.<init>:")
 95             // Should resolve references to super constructor
 96             .shouldMatch("cds,resolve.*archived method .* ResolvedConstantsApp java/lang/Object.<init>:")
 97 
 98             // Should resolve interface methods in VM classes
 99             .shouldMatch("cds,resolve.*archived interface method .* ResolvedConstantsApp java/lang/Runnable.run:")
100 
101             // Should resolve references to own non-static method (private or public)
102             .shouldMatch("archived method.*: ResolvedConstantsBar ResolvedConstantsBar.doBar:")
103             .shouldMatch("archived method.*: ResolvedConstantsApp ResolvedConstantsApp.privateInstanceCall:")
104             .shouldMatch("archived method.*: ResolvedConstantsApp ResolvedConstantsApp.publicInstanceCall:")


105 
106             // Should not resolve references to static method
107             .shouldNotMatch(" archived method CP entry.*: ResolvedConstantsApp ResolvedConstantsApp.staticCall:")

108 
109             // Should resolve references to method in super type
110             .shouldMatch(" archived method CP entry.*: ResolvedConstantsBar ResolvedConstantsFoo.doBar:")
111 
112             // App class cannot resolve references to methods in boot classes:
113             //    When the app class loader tries to resolve a class X that's normally loaded by
114             //    the boot loader, it's possible for the app class loader to get a different copy of
115             //    X (by using MethodHandles.Lookup.defineClass(), etc). Therefore, let's be on
116             //    the side of safety and revert all such references.
117             //
118             //    This will be addressed in JDK-8315737.
119             .shouldMatch("reverted method.*: ResolvedConstantsApp java/io/PrintStream.println:")
120             .shouldMatch("reverted method.*: ResolvedConstantsBar java/lang/Class.getName:")
121 
122             // Should not resolve methods in unrelated classes.
123             .shouldMatch("reverted method.*: ResolvedConstantsApp ResolvedConstantsBar.doit:")
124 
125           // End ---
126             ;
127     }












128 }
129 
130 class ResolvedConstantsApp implements Runnable {
131     public static void main(String args[]) {
132         System.out.println("Hello ResolvedConstantsApp");
133         ResolvedConstantsApp app = new ResolvedConstantsApp();
134         ResolvedConstantsApp.staticCall();
135         app.privateInstanceCall();
136         app.publicInstanceCall();
137         Object a = app;
138         ((Runnable)a).run();
139 
140         ResolvedConstantsFoo foo = new ResolvedConstantsFoo();
141         ResolvedConstantsBar bar = new ResolvedConstantsBar();
142         bar.a ++;
143         bar.b ++;
144         bar.doit();
145     }
146     private static void staticCall() {}
147     private void privateInstanceCall() {}

  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() {}
< prev index next >