1 /*
   2  * Copyright (c) 2017, 2018, 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 Redefine shared class. GC should not cause crash with cached resolved_references.
  28  * @library /test/lib /test/hotspot/jtreg/runtime/appcds /test/hotspot/jtreg/runtime/appcds/test-classes /test/hotspot/jtreg/runtime/appcds/jvmti
  29  * @requires vm.cds.archived.java.heap
  30  * @modules java.base/jdk.internal.misc
  31  *          jdk.jartool/sun.tools.jar
  32  *          java.management
  33  * @build sun.hotspot.WhiteBox
  34  *        RedefineClassApp
  35  *        InstrumentationClassFileTransformer
  36  *        InstrumentationRegisterClassFileTransformer
  37  * @run main/othervm RedefineClassTest
  38  */
  39 
  40 import com.sun.tools.attach.VirtualMachine;
  41 import com.sun.tools.attach.VirtualMachineDescriptor;
  42 import java.io.File;
  43 import java.util.List;
  44 import jdk.test.lib.Asserts;
  45 import jdk.test.lib.cds.CDSOptions;
  46 import jdk.test.lib.process.OutputAnalyzer;
  47 import jdk.test.lib.process.ProcessTools;
  48 
  49 public class RedefineClassTest {
  50     public static String bootClasses[] = {
  51         "RedefineClassApp$Intf",
  52         "RedefineClassApp$Bar",
  53         "sun.hotspot.WhiteBox",
  54     };
  55     public static String appClasses[] = {
  56         "RedefineClassApp",
  57         "RedefineClassApp$Foo",
  58     };
  59     public static String sharedClasses[] = TestCommon.concat(bootClasses, appClasses);
  60 
  61     public static String agentClasses[] = {
  62         "InstrumentationClassFileTransformer",
  63         "InstrumentationRegisterClassFileTransformer",
  64         "Util",
  65     };
  66 
  67     public static void main(String[] args) throws Throwable {
  68         runTest();
  69     }
  70 
  71     public static void runTest() throws Throwable {
  72         String bootJar =
  73             ClassFileInstaller.writeJar("RedefineClassBoot.jar", bootClasses);
  74         String appJar =
  75             ClassFileInstaller.writeJar("RedefineClassApp.jar", appClasses);
  76         String agentJar =
  77             ClassFileInstaller.writeJar("InstrumentationAgent.jar",
  78                                         ClassFileInstaller.Manifest.fromSourceFile("InstrumentationAgent.mf"),
  79                                         agentClasses);
  80 
  81         String bootCP = "-Xbootclasspath/a:" + bootJar;
  82 
  83         String agentCmdArg;
  84         agentCmdArg = "-javaagent:" + agentJar;
  85 
  86         TestCommon.testDump(appJar, sharedClasses, bootCP, "-Xlog:gc+region=trace");
  87 
  88         OutputAnalyzer out = TestCommon.execAuto("-cp", appJar,
  89                 bootCP,
  90                 "-XX:+UnlockDiagnosticVMOptions",
  91                 "-XX:+WhiteBoxAPI",
  92                 "-Xlog:gc+region=trace,cds=info",
  93                 agentCmdArg,
  94                "RedefineClassApp", bootJar, appJar);
  95         out.reportDiagnosticSummary();
  96 
  97         CDSOptions opts = (new CDSOptions()).setXShareMode("auto");
  98         TestCommon.checkExec(out, opts);
  99     }
 100 }
 101