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 import java.io.File;
 51 import java.net.URL;
 52 import java.net.URLClassLoader;
 53 import java.util.ArrayList;
 54 import jdk.test.lib.cds.CDSAppTester;
 55 import jdk.test.lib.helpers.ClassFileInstaller;
 56 import jdk.test.lib.Platform;
 57 
 58 public class AOTMapTest {
 59     static final String appJar = ClassFileInstaller.getJarPath("app.jar");
 60     static final String mainClass = "AOTMapTestApp";
 61     static final String classLoadLogFile = "production.class.load.log";
 62 
 63     public static void main(String[] args) throws Exception {
 64         doTest(args);
 65     }
 66 
 67     public static void doTest(String[] args) throws Exception {
 68         Tester tester = new Tester();
 69         tester.run(args);
 70 
 71         if (tester.isDynamicWorkflow()) {
 72             // For dynamic workflow, the AOT map file doesn't include classes in the base archive, so
 73             // AOTMapReader.validateClasses() will fail.
 74             validate(tester.dumpMapFile, false);
 75         } else {
 76             validate(tester.dumpMapFile, true);
 77         }
 78         validate(tester.runMapFile, true);
 79     }
 80 
 81     static void validate(String mapFileName, boolean checkClases) throws Exception {
 82         AOTMapReader.MapFile mapFile = AOTMapReader.read(mapFileName);
 83         if (checkClases) {
 84             AOTMapReader.validate(mapFile, classLoadLogFile);
 85         } else {
 86             AOTMapReader.validate(mapFile, null);
 87         }
 88         mapFile.shouldHaveClass("AOTMapTestApp"); // built-in class
 89         mapFile.shouldHaveClass("Hello"); // unregistered class
 90     }
 91 
 92     static class Tester extends CDSAppTester {
 93         String dumpMapFile;
 94         String runMapFile;
 95 
 96         public Tester() {
 97             super(mainClass);
 98 
 99             dumpMapFile = "test" + "0" + ".dump.aotmap";
100             runMapFile  = "test" + "0" + ".run.aotmap";
101         }
102 
103         @Override
104         public String classpath(RunMode runMode) {
105             return appJar;
106         }
107 
108         @Override
109         public String[] vmArgs(RunMode runMode) {
110             ArrayList<String> vmArgs = new ArrayList<>();
111 
112             vmArgs.add("-Xmx128M");
113             vmArgs.add("-Xlog:aot=debug");
114 
115             // filesize=0 ensures that a large map file not broken up in multiple files.
116             String logMapPrefix = "-Xlog:aot+map=debug,aot+map+oops=trace:file=";
117             String logSuffix = ":none:filesize=0";
118 
119             if (runMode == RunMode.ASSEMBLY || runMode == RunMode.DUMP_DYNAMIC) {
120                 vmArgs.add(logMapPrefix + dumpMapFile + logSuffix);
121             } else if (runMode == RunMode.PRODUCTION) {
122                 vmArgs.add(logMapPrefix + runMapFile + logSuffix);
123                 vmArgs.add("-Xlog:class+load:file=" + classLoadLogFile + logSuffix);
124             }
125 
126             return vmArgs.toArray(new String[vmArgs.size()]);
127         }
128 
129         @Override
130         public String[] appCommandLine(RunMode runMode) {
131             return new String[] {
132                 mainClass,
133             };
134         }
135     }
136 }
137 
138 class AOTMapTestApp {
139     static URLClassLoader loader; // keep Hello class alive
140     public static void main(String[] args) throws Exception {
141         System.out.println("Hello AOTMapTestApp");
142         testCustomLoader();
143     }
144 
145     static void testCustomLoader() throws Exception {
146         File custJar = new File("cust.jar");
147         URL[] urls = new URL[] {custJar.toURI().toURL()};
148         loader = new URLClassLoader(urls, AOTMapTestApp.class.getClassLoader());
149         Class<?> c = loader.loadClass("Hello");
150         System.out.println(c);
151     }
152 }