1 /*
  2  * Copyright (c) 2025, 2026, 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  * @test id=aot
 26  * @bug 8362566
 27  * @summary Test the contents of -Xlog:aot+map with AOT workflow
 28  * @requires vm.cds.supports.aot.class.linking
 29  * @library /test/lib /test/hotspot/jtreg/runtime/cds /test/hotspot/jtreg/runtime/cds/appcds/test-classes
 30  * @build AOTMapTest Hello
 31  * @run driver jdk.test.lib.helpers.ClassFileInstaller -jar app.jar AOTMapTestApp
 32  * @run driver jdk.test.lib.helpers.ClassFileInstaller -jar cust.jar Hello
 33  * @run driver/timeout=240 AOTMapTest AOT --two-step-training
 34  */
 35 
 36 /**
 37  * @test id=dynamic
 38  * @bug 8362566
 39  * @summary Test the contents of -Xlog:aot+map with dynamic CDS archive
 40  * @requires vm.cds.supports.aot.class.linking
 41  * @library /test/lib /test/hotspot/jtreg/runtime/cds /test/hotspot/jtreg/runtime/cds/appcds/test-classes
 42  * @build jdk.test.whitebox.WhiteBox
 43  * @run driver jdk.test.lib.helpers.ClassFileInstaller jdk.test.whitebox.WhiteBox
 44  * @build AOTMapTest Hello
 45  * @run driver jdk.test.lib.helpers.ClassFileInstaller -jar app.jar AOTMapTestApp
 46  * @run driver jdk.test.lib.helpers.ClassFileInstaller -jar cust.jar Hello
 47  * @run main/othervm/timeout=240 -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI -Xbootclasspath/a:. AOTMapTest DYNAMIC
 48  */
 49 
 50 /**
 51  * @test id=valhalla
 52  * @bug 8362566
 53  * @summary Test the contents of -Xlog:aot+map with AOT workflow and flat arrays
 54  * @enablePreview
 55  * @requires vm.cds.supports.aot.class.linking & vm.debug & vm.cds.write.archived.java.heap
 56  * @library /test/lib /test/hotspot/jtreg/runtime/cds /test/hotspot/jtreg/runtime/cds/appcds/test-classes
 57  * @modules java.base/jdk.internal.value java.base/jdk.internal.misc java.base/jdk.internal.vm.annotation
 58  * @build Hello
 59  * @compile test-classes/AOTMapTestApp.java
 60  * @run driver jdk.test.lib.helpers.ClassFileInstaller -jar app.jar AOTMapTestApp AOTMapTestApp$Wrapper AOTMapTestApp$WrapperWrapper
 61  *                                                                  AOTMapTestApp$ArchivedData Hello
 62  * @run main/othervm/timeout=240 AOTMapTest STATIC
 63  */
 64 
 65 import java.io.File;
 66 import java.net.URL;
 67 import java.net.URLClassLoader;
 68 import java.util.ArrayList;
 69 import jdk.test.lib.cds.CDSAppTester;
 70 import jdk.test.lib.helpers.ClassFileInstaller;
 71 import jdk.test.lib.Platform;
 72 
 73 public class AOTMapTest {
 74     static final String appJar = ClassFileInstaller.getJarPath("app.jar");
 75     static final String mainClass = "AOTMapTestApp";
 76     static final String classLoadLogFile = "production.class.load.log";
 77 
 78     public static void main(String[] args) throws Exception {
 79         doTest(args);
 80     }
 81 
 82     public static void doTest(String[] args) throws Exception {
 83         Tester tester = new Tester();
 84         tester.run(args);
 85 
 86         if (tester.isDynamicWorkflow()) {
 87             // For dynamic workflow, the AOT map file doesn't include classes in the base archive, so
 88             // AOTMapReader.validateClasses() will fail.
 89             validate(tester.dumpMapFile, false);
 90         } else {
 91             validate(tester.dumpMapFile, true);
 92         }
 93         validate(tester.runMapFile, true);
 94     }
 95 
 96     static void validate(String mapFileName, boolean checkClases) throws Exception {
 97         AOTMapReader.MapFile mapFile = AOTMapReader.read(mapFileName);
 98         if (checkClases) {
 99             AOTMapReader.validate(mapFile, classLoadLogFile);
100         } else {
101             AOTMapReader.validate(mapFile, null);
102         }
103         mapFile.shouldHaveClass("AOTMapTestApp"); // built-in class
104         mapFile.shouldHaveClass("Hello"); // unregistered class
105     }
106 
107     static class Tester extends CDSAppTester {
108         String dumpMapFile;
109         String runMapFile;
110 
111         public Tester() {
112             super(mainClass);
113 
114             dumpMapFile = "test" + "0" + ".dump.aotmap";
115             runMapFile  = "test" + "0" + ".run.aotmap";
116         }
117 
118         @Override
119         public String classpath(RunMode runMode) {
120             return appJar;
121         }
122 
123         @Override
124         public String[] vmArgs(RunMode runMode) {
125             ArrayList<String> vmArgs = new ArrayList<>();
126 
127             vmArgs.add("-Xmx128M");
128             vmArgs.add("-Xlog:aot=debug");
129 
130             if (isStaticWorkflow()) {
131                 vmArgs.add("--enable-preview");
132                 vmArgs.add("--add-exports");
133                 vmArgs.add("java.base/jdk.internal.value=ALL-UNNAMED");
134                 vmArgs.add("--add-exports");
135                 vmArgs.add("java.base/jdk.internal.misc=ALL-UNNAMED");
136                 vmArgs.add("-Xbootclasspath/a:" + appJar);
137                 vmArgs.add("-XX:ArchiveHeapTestClass=AOTMapTestApp");
138             }
139 
140             // filesize=0 ensures that a large map file not broken up in multiple files.
141             String logMapPrefix = "-Xlog:aot+map=trace,aot+map+oops=trace:file=";
142             String logSuffix = ":none:filesize=0";
143 
144             if (runMode == RunMode.ASSEMBLY || runMode == RunMode.DUMP_DYNAMIC || runMode == RunMode.DUMP_STATIC) {
145                 vmArgs.add(logMapPrefix + dumpMapFile + logSuffix);
146             } else if (runMode == RunMode.PRODUCTION) {
147                 vmArgs.add(logMapPrefix + runMapFile + logSuffix);
148                 vmArgs.add("-Xlog:class+load:file=" + classLoadLogFile + logSuffix);
149             }
150 
151             return vmArgs.toArray(new String[vmArgs.size()]);
152         }
153 
154         @Override
155         public String[] appCommandLine(RunMode runMode) {
156             return new String[] {
157                 mainClass,
158             };
159         }
160     }
161 }
162 
163 class AOTMapTestApp {
164     static URLClassLoader loader; // keep Hello class alive
165     public static void main(String[] args) throws Exception {
166         System.out.println("Hello AOTMapTestApp");
167         testCustomLoader();
168     }
169 
170     static void testCustomLoader() throws Exception {
171         File custJar = new File("cust.jar");
172         URL[] urls = new URL[] {custJar.toURI().toURL()};
173         loader = new URLClassLoader(urls, AOTMapTestApp.class.getClassLoader());
174         Class<?> c = loader.loadClass("Hello");
175         System.out.println(c);
176     }
177 }
--- EOF ---