1 /* 2 * Copyright (c) 2016, 2024, 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 Test jvmti class file loader hook interaction with AppCDS 28 * @library /test/lib /test/hotspot/jtreg/runtime/cds/appcds 29 * @requires vm.cds 30 * @requires vm.jvmti 31 * @build ClassFileLoadHook 32 * @run main/othervm/native ClassFileLoadHookTest 33 */ 34 35 36 import jdk.test.lib.cds.CDSOptions; 37 import jdk.test.lib.cds.CDSTestUtils; 38 import jdk.test.lib.process.OutputAnalyzer; 39 import jdk.test.lib.helpers.ClassFileInstaller; 40 41 public class ClassFileLoadHookTest { 42 public static String sharedClasses[] = { 43 "ClassFileLoadHook", 44 "ClassFileLoadHook$TestCaseId", 45 "LoadMe", 46 "java/sql/SQLException" 47 }; 48 49 public static void main(String[] args) throws Exception { 50 String wbJar = 51 ClassFileInstaller.writeJar("WhiteBox.jar", "jdk.test.whitebox.WhiteBox"); 52 String appJar = 53 ClassFileInstaller.writeJar("ClassFileLoadHook.jar", sharedClasses); 54 String useWb = "-Xbootclasspath/a:" + wbJar; 55 56 // First, run the test class directly, w/o sharing, as a baseline reference 57 CDSOptions opts = (new CDSOptions()) 58 .setUseVersion(false) 59 .setXShareMode("off") 60 .addSuffix("-XX:+UnlockDiagnosticVMOptions", 61 "-XX:+WhiteBoxAPI", 62 useWb, 63 "-agentlib:SimpleClassFileLoadHook=LoadMe,beforeHook,after_Hook", 64 "ClassFileLoadHook", 65 "" + ClassFileLoadHook.TestCaseId.SHARING_OFF_CFLH_ON); 66 CDSTestUtils.run(opts) 67 .assertNormalExit(); 68 69 // Run with AppCDS, but w/o CFLH - second baseline 70 TestCommon.testDump(appJar, sharedClasses, useWb); 71 OutputAnalyzer out = TestCommon.exec(appJar, 72 "-XX:+UnlockDiagnosticVMOptions", 73 "-XX:+WhiteBoxAPI", useWb, 74 "ClassFileLoadHook", 75 "" + ClassFileLoadHook.TestCaseId.SHARING_ON_CFLH_OFF); 76 77 TestCommon.checkExec(out); 78 79 80 // Now, run with AppCDS with -Xshare:auto and CFLH 81 out = TestCommon.execAuto("-cp", appJar, 82 "-XX:+UnlockDiagnosticVMOptions", 83 "-XX:+WhiteBoxAPI", useWb, 84 "-agentlib:SimpleClassFileLoadHook=LoadMe,beforeHook,after_Hook", 85 "ClassFileLoadHook", 86 "" + ClassFileLoadHook.TestCaseId.SHARING_AUTO_CFLH_ON); 87 88 opts = (new CDSOptions()).setXShareMode("auto"); 89 TestCommon.checkExec(out, opts); 90 91 // Now, run with AppCDS -Xshare:on and CFLH 92 out = TestCommon.exec(appJar, 93 "-XX:+UnlockDiagnosticVMOptions", 94 "-XX:+WhiteBoxAPI", useWb, 95 "-agentlib:SimpleClassFileLoadHook=LoadMe,beforeHook,after_Hook", 96 "ClassFileLoadHook", 97 "" + ClassFileLoadHook.TestCaseId.SHARING_ON_CFLH_ON); 98 TestCommon.checkExec(out); 99 100 // JEP 483: if dumped with -XX:+AOTClassLinking, cannot use archive when CFLH 101 TestCommon.testDump(appJar, sharedClasses, useWb, "-XX:+AOTClassLinking"); 102 out = TestCommon.exec(appJar, 103 "-XX:+UnlockDiagnosticVMOptions", 104 "-XX:+WhiteBoxAPI", useWb, 105 "-agentlib:SimpleClassFileLoadHook=LoadMe,beforeHook,after_Hook", 106 "-Xlog:cds", 107 "ClassFileLoadHook", 108 "" + ClassFileLoadHook.TestCaseId.SHARING_ON_CFLH_ON); 109 if (out.contains("Using AOT-linked classes: false")) { 110 // We are running with VM options that do not support -XX:+AOTClassLinking 111 out.shouldHaveExitValue(0); 112 } else { 113 out.shouldContain("CDS archive has aot-linked classes. It cannot be used when JVMTI ClassFileLoadHook is in use."); 114 out.shouldNotHaveExitValue(0); 115 } 116 } 117 }