1 /*
  2  * Copyright (c) 2014, 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  * @bug 8060592
 30  * @library /test/lib
 31  * @build jdk.test.whitebox.WhiteBox
 32  * @run driver jdk.test.lib.helpers.ClassFileInstaller jdk.test.whitebox.WhiteBox
 33  * @compile test-classes/Hello.java
 34  * @compile test-classes/JvmtiApp.java
 35  * @run driver JvmtiAddPath
 36  */
 37 
 38 import java.io.File;
 39 import jdk.test.lib.process.OutputAnalyzer;
 40 import jdk.test.whitebox.WhiteBox;
 41 
 42 public class JvmtiAddPath {
 43     static String use_whitebox_jar;
 44     static String[] no_extra_matches = {};
 45     static String[] check_appcds_enabled = {
 46         "[class,load] ExtraClass source: shared object"
 47     };
 48     static String[] check_appcds_disabled = {
 49         "[class,load] ExtraClass source: file:"
 50     };
 51 
 52     static void run(String cp, String... args) throws Exception {
 53         run(no_extra_matches, cp, args);
 54     }
 55 
 56     static void run(String[] extra_matches, String cp, String... args) throws Exception {
 57         String[] opts = {"-cp", cp, "-XX:+UnlockDiagnosticVMOptions", "-XX:+WhiteBoxAPI", use_whitebox_jar};
 58         opts = TestCommon.concat(opts, args);
 59         TestCommon.run(opts).assertNormalExit(extra_matches);
 60     }
 61 
 62     public static void main(String[] args) throws Exception {
 63         JarBuilder.build("jvmti_addboot", "Hello");
 64         JarBuilder.build("jvmti_addapp", "Hello");
 65         JarBuilder.build("jvmti_app", "JvmtiApp", "ExtraClass");
 66         JarBuilder.build(true, "WhiteBox", "jdk/test/whitebox/WhiteBox");
 67 
 68         // In all the test cases below, appJar does not contain Hello.class. Instead, we
 69         // append JAR file(s) that contain Hello.class to the boot classpath, the app
 70         // classpath, or both, and verify that Hello.class is loaded by the expected ClassLoader.
 71         String appJar = TestCommon.getTestJar("jvmti_app.jar");         // contains JvmtiApp.class
 72         String addappJar = TestCommon.getTestJar("jvmti_addapp.jar");   // contains Hello.class
 73         String addbootJar = TestCommon.getTestJar("jvmti_addboot.jar"); // contains Hello.class
 74         String twoAppJars = appJar + File.pathSeparator + addappJar;
 75         String wbJar = TestCommon.getTestJar("WhiteBox.jar");
 76         use_whitebox_jar = "-Xbootclasspath/a:" + wbJar;
 77 
 78         TestCommon.testDump(appJar, TestCommon.list("JvmtiApp", "ExtraClass"), "-XX:-PreloadSharedClasses", use_whitebox_jar);
 79 
 80         System.out.println("Test case 1: not adding any paths - Hello.class should not be found");
 81         run(check_appcds_enabled, appJar, "-Xlog:class+load", "JvmtiApp", "noadd"); // appcds should be enabled
 82 
 83         System.out.println("Test case 2: add to boot classpath only - should find Hello.class in boot loader");
 84         run(check_appcds_disabled, appJar, "-Xlog:class+load", "JvmtiApp", "bootonly", addbootJar); // appcds should be disabled
 85 
 86         System.out.println("Test case 3: add to app classpath only - should find Hello.class in app loader");
 87         run(appJar, "JvmtiApp", "apponly", addappJar);
 88 
 89         System.out.println("Test case 4: add to boot and app paths - should find Hello.class in boot loader");
 90         run(appJar, "JvmtiApp", "appandboot", addbootJar, addappJar);
 91 
 92         System.out.println("Test case 5: add to app using -cp, but add to boot using JVMTI - should find Hello.class in boot loader");
 93         run(twoAppJars, "JvmtiApp", "bootonly", addappJar);
 94 
 95         System.out.println("Test case 6: add to app using AppCDS, but add to boot using JVMTI - should find Hello.class in boot loader");
 96         TestCommon.testDump(twoAppJars, TestCommon.list("JvmtiApp", "ExtraClass", "Hello"), "-XX:-PreloadSharedClasses", use_whitebox_jar);
 97         if (!TestCommon.isDynamicArchive()) {
 98             // skip for dynamic archive, the Hello class will be loaded from
 99             // the dynamic archive
100             run(twoAppJars, "JvmtiApp", "bootonly", addappJar);
101         }
102 
103         System.out.println("Test case 7: add to app using AppCDS, no JVMTI calls - should find Hello.class in app loader");
104         run(twoAppJars, "JvmtiApp", "noadd-appcds");
105     }
106 }