1 /*
  2  * Copyright (c) 2018, 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  * @summary JvmtiEnv::AddToBootstrapClassLoaderSearch and JvmtiEnv::AddToSystemClassLoaderSearch should disable AppCDS
 28  * @requires vm.cds
 29  * @library /test/jdk/lib/testlibrary /test/lib /test/hotspot/jtreg/runtime/cds/appcds
 30  * @build jdk.test.whitebox.WhiteBox
 31  * @run driver jdk.test.lib.helpers.ClassFileInstaller jdk.test.whitebox.WhiteBox
 32  * @compile ../../test-classes/JvmtiApp.java
 33  * @run driver/timeout=240 JvmtiAddPath
 34  */
 35 
 36 import java.io.File;
 37 import java.nio.file.Files;
 38 import java.nio.file.Path;
 39 import java.nio.file.Paths;
 40 import jdk.test.lib.cds.CDSTestUtils;
 41 import jdk.test.lib.process.OutputAnalyzer;
 42 import jdk.test.whitebox.WhiteBox;
 43 
 44 public class JvmtiAddPath {
 45     static String use_whitebox_jar;
 46     static String[] no_extra_matches = {};
 47     static String[] check_appcds_enabled = {
 48         "[class,load] ExtraClass source: shared object"
 49     };
 50     static String[] check_appcds_disabled = {
 51         "[class,load] ExtraClass source: file:"
 52     };
 53 
 54     private static final Path USER_DIR = Paths.get(CDSTestUtils.getOutputDir());
 55 
 56     private static final String TEST_SRC = System.getProperty("test.src");
 57 
 58     private static final Path SRC_DIR = Paths.get(TEST_SRC, "src");
 59     private static final Path MODS_DIR = Paths.get("mods");
 60 
 61     // the module name of the test module
 62     private static final String TEST_MODULE1 = "com.simple";
 63 
 64     // the module main class
 65     private static final String MAIN_CLASS = "com.simple.Main";
 66 
 67     private static Path moduleDir = null;
 68     private static Path mainJar = null;
 69 
 70     public static void buildTestModule() throws Exception {
 71 
 72         // javac -d mods/$TESTMODULE --module-path MOD_DIR src/$TESTMODULE/**
 73         JarBuilder.compileModule(SRC_DIR.resolve(TEST_MODULE1),
 74                                  MODS_DIR.resolve(TEST_MODULE1),
 75                                  MODS_DIR.toString());
 76 
 77         moduleDir = Files.createTempDirectory(USER_DIR, "mlib");
 78 
 79         mainJar = moduleDir.resolve(TEST_MODULE1 + ".jar");
 80         String classes = MODS_DIR.resolve(TEST_MODULE1).toString();
 81         JarBuilder.createModularJar(mainJar.toString(), classes, MAIN_CLASS);
 82     }
 83 
 84     static void run(String cp, String... args) throws Exception {
 85         run(no_extra_matches, cp, args);
 86     }
 87 
 88     static void run(String[] extra_matches, String cp, String... args) throws Exception {
 89         String[] opts = {"-cp", cp, "-XX:+UnlockDiagnosticVMOptions", "-XX:+WhiteBoxAPI", use_whitebox_jar};
 90         opts = TestCommon.concat(opts, args);
 91         TestCommon.run(opts).assertNormalExit(extra_matches);
 92     }
 93 
 94     public static void main(String[] args) throws Exception {
 95         buildTestModule();
 96         JarBuilder.build("jvmti_app", "JvmtiApp", "ExtraClass");
 97         JarBuilder.build(true, "WhiteBox", "jdk/test/whitebox/WhiteBox");
 98 
 99         // In all the test cases below, appJar does not contain Hello.class. Instead, we
100         // append JAR file(s) that contain Hello.class to the boot classpath, the app
101         // classpath, or both, and verify that Hello.class is loaded by the expected ClassLoader.
102         String appJar = TestCommon.getTestJar("jvmti_app.jar");         // contains JvmtiApp.class
103         String addappJar = mainJar.toString();  // contains Main.class
104         String addbootJar = mainJar.toString(); // contains Main.class
105         String twoAppJars = appJar + File.pathSeparator + addappJar;
106         String modulePath = "--module-path=" + moduleDir.toString();
107         String wbJar = TestCommon.getTestJar("WhiteBox.jar");
108         use_whitebox_jar = "-Xbootclasspath/a:" + wbJar;
109 
110         OutputAnalyzer output = TestCommon.createArchive(
111                                     appJar,
112                                     TestCommon.list("JvmtiApp", "ExtraClass", MAIN_CLASS),
113                                     use_whitebox_jar,
114                                     modulePath);
115         TestCommon.checkDump(output);
116 
117         System.out.println("Test case 1: not adding module path - Hello.class should not be found");
118         run(check_appcds_enabled, appJar,
119             "-Xlog:class+load", "JvmtiApp", "noadd", MAIN_CLASS); // appcds should be enabled
120 
121         System.out.println("Test case 2: add to boot classpath only - should find Hello.class in boot loader");
122         run(check_appcds_disabled, appJar,
123             "-Xlog:class+load=trace",
124             modulePath,
125             "JvmtiApp", "bootonly", addbootJar, MAIN_CLASS); // appcds should be disabled
126 
127         System.out.println("Test case 3: add to app classpath only - should find Hello.class in app loader");
128         run(appJar, modulePath,
129             "JvmtiApp", "apponly", addappJar, MAIN_CLASS);
130 
131         System.out.println("Test case 4: add to boot and app paths - should find Hello.class in boot loader");
132         run(appJar, modulePath,
133             "JvmtiApp", "appandboot", addbootJar, addappJar, MAIN_CLASS);
134 
135         System.out.println("Test case 5: add to app using -cp, but add to boot using JVMTI - should find Hello.class in boot loader");
136         run(appJar, modulePath,
137             "JvmtiApp", "bootonly", addappJar, MAIN_CLASS);
138 
139         System.out.println("Test case 6: add to app using AppCDS, but add to boot using JVMTI - should find Hello.class in boot loader");
140         output = TestCommon.createArchive(
141                      appJar, TestCommon.list("JvmtiApp", "ExtraClass"),
142                      use_whitebox_jar,
143                      modulePath);
144         TestCommon.checkDump(output);
145         run(twoAppJars, modulePath,
146             "JvmtiApp", "bootonly", addappJar, MAIN_CLASS);
147 
148         System.out.println("Test case 7: add to app using AppCDS, no JVMTI calls - should find Hello.class in app loader");
149         run(twoAppJars, modulePath,
150             "JvmtiApp", "noadd-appcds", MAIN_CLASS);
151     }
152 }