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