1 /*
 2  * Copyright (c) 2018, 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 /*
26  * @test
27  * @summary Tests how CDS works when critical library classes are replaced with JVMTI ClassFileLoadHook
28  * @library /test/lib
29  * @requires vm.cds.write.archived.java.heap
30  * @requires !vm.jvmci.enabled
31  * @modules java.base/jdk.internal.misc
32  * @build jdk.test.whitebox.WhiteBox
33  * @run driver jdk.test.lib.helpers.ClassFileInstaller -jar whitebox.jar jdk.test.whitebox.WhiteBox
34  * @run main/othervm/native ReplaceCriticalClassesForSubgraphs
35  */
36 
37 import java.util.ArrayList;
38 import java.util.Arrays;
39 import jdk.internal.misc.PreviewFeatures;
40 
41 public class ReplaceCriticalClassesForSubgraphs extends ReplaceCriticalClasses {
42     public static void main(String args[]) throws Throwable {
43         ReplaceCriticalClassesForSubgraphs rcc = new ReplaceCriticalClassesForSubgraphs();
44         rcc.process(args);
45     }
46 
47     public String[] getTests() {
48         ArrayList<String> tests = new ArrayList<String>(Arrays.asList(
49             // Try to replace classes that are used by the archived subgraph graphs. (CDS should be disabled)
50             "-early -notshared -subgraph java/lang/module/ResolvedModule jdk.internal.module.ArchivedModuleGraph",
51             "-early -notshared -subgraph java/lang/Integer java.lang.Integer$IntegerCache",
52 
53             // CDS should not be disabled -- these critical classes cannot be replaced because
54             // JvmtiExport::early_class_hook_env() is false.
55             "-subgraph java/lang/module/Configuration java.lang.module.Configuration",
56             "-subgraph java/lang/ModuleLayer java.lang.ModuleLayer",
57 
58             // Tests for archived full module graph. We cannot use whitebox, which requires appending to bootclasspath.
59             // VM will disable full module graph if bootclasspath is appended.
60             "-nowhitebox -early -notshared -subgraph java/lang/Module jdk.internal.module.ArchivedBootLayer",
61             "-nowhitebox -early -notshared -subgraph java/lang/ModuleLayer jdk.internal.module.ArchivedBootLayer",
62             "-nowhitebox -subgraph java/lang/Module jdk.internal.module.ArchivedBootLayer",
63             "-nowhitebox -subgraph java/lang/ModuleLayer jdk.internal.module.ArchivedBootLayer"));
64 
65         // IntegerCache does not exist in preview mode so it should not be expected in the output
66         if (!PreviewFeatures.isEnabled()) {
67             // CDS should not be disabled -- these critical classes cannot be replaced because
68             // JvmtiExport::early_class_hook_env() is false.
69             tests.add("-subgraph java/lang/Integer java.lang.Integer$IntegerCache");
70         }
71 
72         return tests.toArray(new String[0]);
73     }
74 }