1 /*
   2  * Copyright (c) 2020, 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  * @bug 8249276
  28  * @summary When dumping the CDS archive, try to lock some objects. These objects should be archived
  29  *          without the locking bits in the markWord.
  30  * @library /test/lib /test/hotspot/jtreg/runtime/cds/appcds
  31  * @requires vm.cds
  32  * @requires vm.flavor != "minimal"
  33  * @modules java.instrument
  34  * @run driver LockDuringDump
  35  */
  36 
  37 import jdk.test.lib.process.OutputAnalyzer;
  38 
  39 public class LockDuringDump {
  40     public static String appClasses[] = {
  41         LockDuringDumpApp.class.getName(),
  42     };
  43     public static String agentClasses[] = {
  44         LockDuringDumpAgent.class.getName(),
  45     };
  46 
  47     public static void main(String[] args) throws Throwable {
  48         String agentJar =
  49             ClassFileInstaller.writeJar("LockDuringDumpAgent.jar",
  50                                         ClassFileInstaller.Manifest.fromSourceFile("LockDuringDumpAgent.mf"),
  51                                         agentClasses);
  52 
  53         String appJar =
  54             ClassFileInstaller.writeJar("LockDuringDumpApp.jar", appClasses);
  55 
  56         for (int i = 0; i < 3; i++) {
  57             // i = 0 -- dump without agent
  58             // i = 1 -- dump with agent = disable BiasedLocking
  59             // i = 2 -- dump with agent = enable BiasedLocking
  60 
  61             String agentArg   = (i == 0) ? "-showversion" : "-javaagent:" + agentJar;
  62             String agentArg2  = (i == 0) ? "-showversion" : "-XX:+AllowArchivingWithJavaAgent";
  63             String biasedLock = (i != 2) ? "-showversion" : "-XX:+UseBiasedLocking";
  64 
  65             OutputAnalyzer out = 
  66                 TestCommon.testDump(appJar, TestCommon.list(LockDuringDumpApp.class.getName()),
  67                                     "-XX:+UnlockDiagnosticVMOptions",
  68                                     agentArg, agentArg2, biasedLock);
  69             if (i != 0) {
  70                 out.shouldContain("Let's hold the lock on the literal string");
  71             }
  72 
  73             TestCommon.run(
  74                 "-cp", appJar,
  75                 "-XX:+UnlockDiagnosticVMOptions", agentArg2, biasedLock,
  76                 LockDuringDumpApp.class.getName())
  77               .assertNormalExit("I am able to lock the literal string");
  78         }
  79     }
  80 }
  81