1 /*
   2  * Copyright (c) 2016, 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 Test jvmti class file loader hook interaction with AppCDS
  28  * @library /test/lib /test/hotspot/jtreg/runtime/cds
  29  * @requires vm.cds
  30  * @build ClassFileLoadHook
  31  * @run main/othervm/native ClassFileLoadHookTest
  32  */
  33 
  34 
  35 import jdk.test.lib.cds.CDSOptions;
  36 import jdk.test.lib.process.OutputAnalyzer;
  37 import jdk.test.lib.process.ProcessTools;
  38 
  39 
  40 public class ClassFileLoadHookTest {
  41     public static String sharedClasses[] = {
  42         "ClassFileLoadHook",
  43         "ClassFileLoadHook$TestCaseId",
  44         "ClassFileLoadHook$1",
  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", "sun.hotspot.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         ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(
  58                 "-XX:+UnlockDiagnosticVMOptions",
  59                 "-XX:+WhiteBoxAPI",
  60                 useWb,
  61                 "-agentlib:SimpleClassFileLoadHook=LoadMe,beforeHook,after_Hook",
  62                 "ClassFileLoadHook",
  63                 "" + ClassFileLoadHook.TestCaseId.SHARING_OFF_CFLH_ON);
  64         TestCommon.executeAndLog(pb, "no-sharing").shouldHaveExitValue(0);
  65 
  66         // Run with AppCDS, but w/o CFLH - second baseline
  67         TestCommon.testDump(appJar, sharedClasses, useWb);
  68         OutputAnalyzer out = TestCommon.exec(appJar,
  69                 "-XX:+UnlockDiagnosticVMOptions",
  70                 "-XX:+WhiteBoxAPI", useWb,
  71                 "ClassFileLoadHook",
  72                 "" + ClassFileLoadHook.TestCaseId.SHARING_ON_CFLH_OFF);
  73 
  74         TestCommon.checkExec(out);
  75 
  76 
  77         // Now, run with AppCDS with -Xshare:auto and CFLH
  78         out = TestCommon.execAuto("-cp", appJar,
  79                 "-XX:+UnlockDiagnosticVMOptions",
  80                 "-XX:+WhiteBoxAPI", useWb,
  81                 "-agentlib:SimpleClassFileLoadHook=LoadMe,beforeHook,after_Hook",
  82                 "ClassFileLoadHook",
  83                 "" + ClassFileLoadHook.TestCaseId.SHARING_AUTO_CFLH_ON);
  84 
  85         CDSOptions opts = (new CDSOptions()).setXShareMode("auto");
  86         TestCommon.checkExec(out, opts);
  87 
  88         // Now, run with AppCDS -Xshare:on and CFLH
  89         out = TestCommon.exec(appJar,
  90                 "-XX:+UnlockDiagnosticVMOptions",
  91                 "-XX:+WhiteBoxAPI", useWb,
  92                 "-agentlib:SimpleClassFileLoadHook=LoadMe,beforeHook,after_Hook",
  93                 "ClassFileLoadHook",
  94                 "" + ClassFileLoadHook.TestCaseId.SHARING_ON_CFLH_ON);
  95         TestCommon.checkExec(out);
  96     }
  97 }