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