1 /*
   2  * Copyright (c) 2016, 2017, 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 Tests how CDS works when critical library classes are replaced with JVMTI ClassFileLoadHook
  28  * @library /test/lib
  29  * @requires vm.cds
  30  *
  31  * @comment CDS should not be disabled -- these critical classes will be replaced because JvmtiExport::early_class_hook_env() is true.
  32  * @run main/othervm/native -Xshare:auto -agentlib:SimpleClassFileLoadHook=+early,java/lang/Object,XXX,XXX     -showversion ReplaceCriticalClasses
  33  * @run main/othervm/native -Xshare:auto -agentlib:SimpleClassFileLoadHook=+early,java/lang/String,XXX,XXX     -showversion ReplaceCriticalClasses
  34  * @run main/othervm/native -Xshare:auto -agentlib:SimpleClassFileLoadHook=+early,java/lang/Cloneable,XXX,XXX  -showversion ReplaceCriticalClasses
  35  * @run main/othervm/native -Xshare:auto -agentlib:SimpleClassFileLoadHook=+early,java/io/Serializable,XXX,XXX -showversion ReplaceCriticalClasses
  36  *
  37  * @comment CDS should not be disabled -- these critical classes cannot be replaced because JvmtiExport::early_class_hook_env() is false.
  38  * @run main/othervm/native -Xshare:auto -agentlib:SimpleClassFileLoadHook=java/lang/Object,XXX,XXX     -showversion ReplaceCriticalClasses
  39  * @run main/othervm/native -Xshare:auto -agentlib:SimpleClassFileLoadHook=java/lang/String,XXX,XXX     -showversion ReplaceCriticalClasses
  40  * @run main/othervm/native -Xshare:auto -agentlib:SimpleClassFileLoadHook=java/lang/Cloneable,XXX,XXX  -showversion ReplaceCriticalClasses
  41  * @run main/othervm/native -Xshare:auto -agentlib:SimpleClassFileLoadHook=java/io/Serializable,XXX,XXX -showversion ReplaceCriticalClasses
  42  *
  43  * @comment Try to replace classes that are used by the archived subgraph graphs. As of 2018/10/21 the classes won't be replaced because
  44  *          all archived subgraphs were loaded in JVMTI_PHASE_PRIMORDIAL.
  45  * @run main/othervm/native -Xshare:auto -agentlib:SimpleClassFileLoadHook=java/util/ArrayList,XXX,XXX             -verbose -showversion -Xlog:cds+heap=debug ReplaceCriticalClasses
  46  * @run main/othervm/native -Xshare:auto -agentlib:SimpleClassFileLoadHook=java/lang/module/ResolvedModule,XXX,XXX -verbose -showversion -Xlog:cds+heap=debug ReplaceCriticalClasses
  47  *
  48  * @comment Replace classes that are loaded after JVMTI_PHASE_PRIMORDIAL. It's OK to replace such classes even when CDS is enabled.
  49  *          Nothing bad should happen.
  50  * @run main/othervm/native -Xshare:auto -agentlib:SimpleClassFileLoadHook=jdk/internal/vm/PostVMInitHook,XXX,XXX  -showversion ReplaceCriticalClasses
  51  * @run main/othervm/native -Xshare:auto -agentlib:SimpleClassFileLoadHook=java/util/Locale,XXX,XXX                -showversion ReplaceCriticalClasses
  52  * @run main/othervm/native -Xshare:auto -agentlib:SimpleClassFileLoadHook=sun/util/locale/BaseLocale,XXX,XXX      -showversion ReplaceCriticalClasses
  53  * @run main/othervm/native -Xshare:auto -agentlib:SimpleClassFileLoadHook=java/lang/Readable,XXX,XXX              -showversion ReplaceCriticalClasses
  54  */
  55 
  56 public class ReplaceCriticalClasses {
  57     public static void main(String args[]) {
  58         String strings[] = {
  59             // interned strings from j.l.Object
  60             "@",
  61             "nanosecond timeout value out of range",
  62             "timeoutMillis value is negative",
  63 
  64             // interned strings from j.l.Integer
  65             "0",
  66             "0X",
  67             "0x",
  68             "int"
  69         };
  70 
  71         // Make sure the interned string table is same
  72         for (String s : strings) {
  73             String i = s.intern();
  74             if (s != i) {
  75                 throw new RuntimeException("Interned string mismatch: \"" + s + "\" @ " + System.identityHashCode(s) +
  76                                            " vs \"" + i + "\" @ " + System.identityHashCode(i));
  77             }
  78         }
  79         // We have tried to use ClassFileLoadHook to replace critical library classes (which may
  80         // may not have succeeded, depending on whether the agent has requested
  81         // can_generate_all_class_hook_events/can_generate_early_class_hook_events capabilities).
  82         //
  83         // In any case, the JVM should have started properly (perhaps with CDS disabled) and
  84         // the above operations should succeed.
  85         System.out.println("If I can come to here without crashing, things should be OK");
  86     }
  87 }