< prev index next >

test/hotspot/jtreg/runtime/cds/appcds/cacheObject/ArchiveHeapTestClass.java

Print this page

  1 /*
  2  * Copyright (c) 2022, 2023, 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  * @bug 8214781 8293187
 28  * @summary Test for the -XX:ArchiveHeapTestClass flag
 29  * @requires vm.debug == true & vm.cds.write.archived.java.heap
 30  * @modules java.base/sun.invoke.util java.logging
 31  * @library /test/jdk/lib/testlibrary /test/lib
 32  *          /test/hotspot/jtreg/runtime/cds/appcds
 33  *          /test/hotspot/jtreg/runtime/cds/appcds/test-classes
 34  * @build ArchiveHeapTestClass Hello pkg.ClassInPackage
 35  * @run driver jdk.test.lib.helpers.ClassFileInstaller -jar boot.jar
 36  *             CDSTestClassA CDSTestClassA$XX CDSTestClassA$YY
 37  *             CDSTestClassB CDSTestClassC CDSTestClassD
 38  *             CDSTestClassE CDSTestClassF CDSTestClassG
 39  *             pkg.ClassInPackage
 40  * @run driver jdk.test.lib.helpers.ClassFileInstaller -jar app.jar Hello
 41  * @run driver ArchiveHeapTestClass
 42  */
 43 
 44 import jdk.test.lib.Platform;
 45 import jdk.test.lib.helpers.ClassFileInstaller;
 46 import jdk.test.lib.process.OutputAnalyzer;

 47 
 48 public class ArchiveHeapTestClass {
 49     static final String bootJar = ClassFileInstaller.getJarPath("boot.jar");
 50     static final String appJar = ClassFileInstaller.getJarPath("app.jar");
 51     static final String[] appClassList = {"Hello"};
 52 
 53     static final String CDSTestClassA_name = CDSTestClassA.class.getName();
 54     static final String CDSTestClassB_name = CDSTestClassB.class.getName();
 55     static final String CDSTestClassC_name = CDSTestClassC.class.getName();
 56     static final String CDSTestClassD_name = CDSTestClassD.class.getName();
 57     static final String CDSTestClassE_name = CDSTestClassE.class.getName();
 58     static final String CDSTestClassF_name = CDSTestClassF.class.getName();
 59     static final String CDSTestClassG_name = CDSTestClassG.class.getName();
 60     static final String ClassInPackage_name = pkg.ClassInPackage.class.getName().replace('.', '/');
 61     static final String ARCHIVE_TEST_FIELD_NAME = "archivedObjects";
 62 
 63     public static void main(String[] args) throws Exception {
 64         testDebugBuild();
 65     }
 66 

142         testCase("Field not found");
143         output = dumpBootAndHello(CDSTestClassB_name);
144         mustFail(output, "Unable to find the static T_OBJECT field CDSTestClassB::archivedObjects");
145 
146         testCase("Not a static field");
147         output = dumpBootAndHello(CDSTestClassC_name);
148         mustFail(output, "Unable to find the static T_OBJECT field CDSTestClassC::archivedObjects");
149 
150         testCase("Not a T_OBJECT field");
151         output = dumpBootAndHello(CDSTestClassD_name);
152         mustFail(output, "Unable to find the static T_OBJECT field CDSTestClassD::archivedObjects");
153 
154         testCase("Use a disallowed class: in unnamed module but not in unname package");
155         output = dumpBootAndHello(CDSTestClassE_name);
156         mustFail(output, "Class pkg.ClassInPackage not allowed in archive heap");
157 
158         testCase("Use a disallowed class: not in java.base module");
159         output = dumpBootAndHello(CDSTestClassF_name);
160         mustFail(output, "Class java.util.logging.Level not allowed in archive heap");
161 
162         if (false) { // JDK-8293187
163             testCase("sun.invoke.util.Wrapper");
164             output = dumpBootAndHello(CDSTestClassG_name);
165             mustSucceed(output);
166         }







167     }
168 }
169 
170 class CDSTestClassA {
171     static final String output = "CDSTestClassA.<clinit> was executed";
172     static Object[] archivedObjects;
173     static {
174         archivedObjects = new Object[5];
175         archivedObjects[0] = output;
176         archivedObjects[1] = new CDSTestClassA[0];
177         archivedObjects[2] = new YY();
178         archivedObjects[3] = new int[0];
179         archivedObjects[4] = new int[2][2];
180         System.out.println(output);
181         System.out.println("CDSTestClassA   module  = " + CDSTestClassA.class.getModule());
182         System.out.println("CDSTestClassA   package = " + CDSTestClassA.class.getPackage());
183         System.out.println("CDSTestClassA[] module  = " + archivedObjects[1].getClass().getModule());
184         System.out.println("CDSTestClassA[] package = " + archivedObjects[1].getClass().getPackage());
185     }
186 

252     static Object[] archivedObjects;
253     static {
254         // Not in unnamed package of unnamed module
255         archivedObjects = new Object[1];
256         archivedObjects[0] = new pkg.ClassInPackage();
257     }
258 }
259 
260 class CDSTestClassF {
261     static Object[] archivedObjects;
262     static {
263         // Not in java.base
264         archivedObjects = new Object[1];
265         archivedObjects[0] = java.util.logging.Level.OFF;
266     }
267 }
268 
269 class CDSTestClassG {
270     static Object[] archivedObjects;
271     static {
272         // Not in java.base
273         archivedObjects = new Object[1];
274         archivedObjects[0] = sun.invoke.util.Wrapper.BOOLEAN;
































































































275     }
276 }

  1 /*
  2  * Copyright (c) 2022, 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  * @bug 8214781 8293187
 28  * @summary Test for the -XX:ArchiveHeapTestClass flag
 29  * @requires vm.debug == true & vm.cds.write.archived.java.heap
 30  * @modules java.base/sun.invoke.util java.base/jdk.internal.misc java.logging
 31  * @library /test/jdk/lib/testlibrary /test/lib
 32  *          /test/hotspot/jtreg/runtime/cds/appcds
 33  *          /test/hotspot/jtreg/runtime/cds/appcds/test-classes
 34  * @build ArchiveHeapTestClass Hello pkg.ClassInPackage
 35  * @run driver jdk.test.lib.helpers.ClassFileInstaller -jar boot.jar
 36  *             CDSTestClassA CDSTestClassA$XX CDSTestClassA$YY
 37  *             CDSTestClassB CDSTestClassC CDSTestClassD
 38  *             CDSTestClassE CDSTestClassF CDSTestClassG CDSTestClassG$MyEnum
 39  *             pkg.ClassInPackage
 40  * @run driver jdk.test.lib.helpers.ClassFileInstaller -jar app.jar Hello
 41  * @run driver ArchiveHeapTestClass
 42  */
 43 
 44 import jdk.test.lib.Platform;
 45 import jdk.test.lib.helpers.ClassFileInstaller;
 46 import jdk.test.lib.process.OutputAnalyzer;
 47 import jdk.internal.misc.CDS;
 48 
 49 public class ArchiveHeapTestClass {
 50     static final String bootJar = ClassFileInstaller.getJarPath("boot.jar");
 51     static final String appJar = ClassFileInstaller.getJarPath("app.jar");
 52     static final String[] appClassList = {"Hello"};
 53 
 54     static final String CDSTestClassA_name = CDSTestClassA.class.getName();
 55     static final String CDSTestClassB_name = CDSTestClassB.class.getName();
 56     static final String CDSTestClassC_name = CDSTestClassC.class.getName();
 57     static final String CDSTestClassD_name = CDSTestClassD.class.getName();
 58     static final String CDSTestClassE_name = CDSTestClassE.class.getName();
 59     static final String CDSTestClassF_name = CDSTestClassF.class.getName();
 60     static final String CDSTestClassG_name = CDSTestClassG.class.getName();
 61     static final String ClassInPackage_name = pkg.ClassInPackage.class.getName().replace('.', '/');
 62     static final String ARCHIVE_TEST_FIELD_NAME = "archivedObjects";
 63 
 64     public static void main(String[] args) throws Exception {
 65         testDebugBuild();
 66     }
 67 

143         testCase("Field not found");
144         output = dumpBootAndHello(CDSTestClassB_name);
145         mustFail(output, "Unable to find the static T_OBJECT field CDSTestClassB::archivedObjects");
146 
147         testCase("Not a static field");
148         output = dumpBootAndHello(CDSTestClassC_name);
149         mustFail(output, "Unable to find the static T_OBJECT field CDSTestClassC::archivedObjects");
150 
151         testCase("Not a T_OBJECT field");
152         output = dumpBootAndHello(CDSTestClassD_name);
153         mustFail(output, "Unable to find the static T_OBJECT field CDSTestClassD::archivedObjects");
154 
155         testCase("Use a disallowed class: in unnamed module but not in unname package");
156         output = dumpBootAndHello(CDSTestClassE_name);
157         mustFail(output, "Class pkg.ClassInPackage not allowed in archive heap");
158 
159         testCase("Use a disallowed class: not in java.base module");
160         output = dumpBootAndHello(CDSTestClassF_name);
161         mustFail(output, "Class java.util.logging.Level not allowed in archive heap");
162 
163         testCase("sun.invoke.util.Wrapper");
164         output = dumpBootAndHello(CDSTestClassG_name,
165                                   "--add-exports", "java.base/sun.invoke.util=ALL-UNNAMED",
166                                   "--add-exports", "java.base/jdk.internal.misc=ALL-UNNAMED");
167         mustSucceed(output);
168 
169         TestCommon.run("-Xbootclasspath/a:" + bootJar, "-cp", appJar, "-Xlog:cds+heap",
170                        "--add-exports", "java.base/sun.invoke.util=ALL-UNNAMED",
171                        "--add-exports", "java.base/jdk.internal.misc=ALL-UNNAMED",
172                        CDSTestClassG_name)
173             .assertNormalExit("resolve subgraph " + CDSTestClassG_name,
174                               "Initialized from CDS");
175     }
176 }
177 
178 class CDSTestClassA {
179     static final String output = "CDSTestClassA.<clinit> was executed";
180     static Object[] archivedObjects;
181     static {
182         archivedObjects = new Object[5];
183         archivedObjects[0] = output;
184         archivedObjects[1] = new CDSTestClassA[0];
185         archivedObjects[2] = new YY();
186         archivedObjects[3] = new int[0];
187         archivedObjects[4] = new int[2][2];
188         System.out.println(output);
189         System.out.println("CDSTestClassA   module  = " + CDSTestClassA.class.getModule());
190         System.out.println("CDSTestClassA   package = " + CDSTestClassA.class.getPackage());
191         System.out.println("CDSTestClassA[] module  = " + archivedObjects[1].getClass().getModule());
192         System.out.println("CDSTestClassA[] package = " + archivedObjects[1].getClass().getPackage());
193     }
194 

260     static Object[] archivedObjects;
261     static {
262         // Not in unnamed package of unnamed module
263         archivedObjects = new Object[1];
264         archivedObjects[0] = new pkg.ClassInPackage();
265     }
266 }
267 
268 class CDSTestClassF {
269     static Object[] archivedObjects;
270     static {
271         // Not in java.base
272         archivedObjects = new Object[1];
273         archivedObjects[0] = java.util.logging.Level.OFF;
274     }
275 }
276 
277 class CDSTestClassG {
278     static Object[] archivedObjects;
279     static {
280         CDS.initializeFromArchive(CDSTestClassG.class);
281         if (archivedObjects == null) {
282             archivedObjects = new Object[13];
283             archivedObjects[0] = sun.invoke.util.Wrapper.BOOLEAN;
284             archivedObjects[1] = sun.invoke.util.Wrapper.INT.zero();
285             archivedObjects[2] = sun.invoke.util.Wrapper.DOUBLE.zero();
286             archivedObjects[3] = MyEnum.DUMMY1;
287 
288             archivedObjects[4] = Boolean.class;
289             archivedObjects[5] = Byte.class;
290             archivedObjects[6] = Character.class;
291             archivedObjects[7] = Short.class;
292             archivedObjects[8] = Integer.class;
293             archivedObjects[9] = Long.class;
294             archivedObjects[10] = Float.class;
295             archivedObjects[11] = Double.class;
296             archivedObjects[12] = Void.class;
297         } else {
298             System.out.println("Initialized from CDS");
299         }
300     }
301 
302     public static void main(String args[]) {
303         if (archivedObjects[0] != sun.invoke.util.Wrapper.BOOLEAN) {
304             throw new RuntimeException("Huh 0");
305         }
306 
307         if (archivedObjects[1] != sun.invoke.util.Wrapper.INT.zero()) {
308             throw new RuntimeException("Huh 1");
309         }
310 
311         if (archivedObjects[2] != sun.invoke.util.Wrapper.DOUBLE.zero()) {
312             throw new RuntimeException("Huh 2");
313         }
314 
315         if (archivedObjects[3] != MyEnum.DUMMY1) {
316             throw new RuntimeException("Huh 3");
317         }
318 
319         if (MyEnum.BOOLEAN != true) {
320             throw new RuntimeException("Huh 10.1");
321         }
322         if (MyEnum.BYTE != -128) {
323             throw new RuntimeException("Huh 10.2");
324         }
325         if (MyEnum.CHAR != 'c') {
326             throw new RuntimeException("Huh 10.3");
327         }
328         if (MyEnum.SHORT != -12345) {
329             throw new RuntimeException("Huh 10.4");
330         }
331         if (MyEnum.INT != -123456) {
332             throw new RuntimeException("Huh 10.5");
333         }
334         if (MyEnum.LONG != 0x1234567890L) {
335             throw new RuntimeException("Huh 10.6");
336         }
337         if (MyEnum.LONG2 != -0x1234567890L) {
338             throw new RuntimeException("Huh 10.7");
339         }
340         if (MyEnum.FLOAT != 567891.0f) {
341             throw new RuntimeException("Huh 10.8");
342         }
343         if (MyEnum.DOUBLE != 12345678905678.890) {
344             throw new RuntimeException("Huh 10.9");
345         }
346 
347         checkClass(4, Boolean.class);
348         checkClass(5, Byte.class);
349         checkClass(6, Character.class);
350         checkClass(7, Short.class);
351         checkClass(8, Integer.class);
352         checkClass(9, Long.class);
353         checkClass(10, Float.class);
354         checkClass(11, Double.class);
355         checkClass(12, Void.class);
356 
357         System.out.println("Success!");
358     }
359 
360   static void checkClass(int index, Class c) {
361       if (archivedObjects[index] != c) {
362           throw new RuntimeException("archivedObjects[" + index + "] should be " + c);
363       }
364   }
365 
366     enum MyEnum {
367         DUMMY1,
368         DUMMY2;
369 
370         static final boolean BOOLEAN = true;
371         static final byte    BYTE    = -128;
372         static final short   SHORT   = -12345;
373         static final char    CHAR    = 'c';
374         static final int     INT     = -123456;
375         static final long    LONG    =  0x1234567890L;
376         static final long    LONG2   = -0x1234567890L;
377         static final float   FLOAT   = 567891.0f;
378         static final double  DOUBLE  = 12345678905678.890;
379     }
380 }
< prev index next >