1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
|
/*
* Copyright (c) 2017, 2018, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*
*/
import java.lang.instrument.ClassDefinition;
import java.lang.instrument.Instrumentation;
import java.lang.instrument.UnmodifiableClassException;
import java.net.URL;
import java.net.URLClassLoader;
import java.io.File;
import java.security.CodeSigner;
import java.security.CodeSource;
import java.security.ProtectionDomain;
import sun.hotspot.WhiteBox;
public class RedefineClassApp {
static WhiteBox wb = WhiteBox.getWhiteBox();
public static interface Intf { // Loaded from Boot class loader (-Xbootclasspath/a).
public String get();
}
public static class Bar implements Intf { // Loaded from Boot class loader.
public String get() {
return "buzz";
}
}
public static class Foo implements Intf { // Loaded from AppClassLoader
public String get() {
return "buzz";
}
}
static int numTests = 0;
static int failed = 0;
static Instrumentation instrumentation;
public static void main(String args[]) throws Throwable {
if (wb.areSharedStringsIgnored()) {
System.out.println("Shared strings are ignored.");
return;
}
File bootJar = new File(args[0]);
File appJar = new File(args[1]);
instrumentation = InstrumentationRegisterClassFileTransformer.getInstrumentation();
System.out.println("INFO: instrumentation = " + instrumentation);
testBootstrapCDS("Bootstrap Loader", bootJar);
testAppCDSv1("Application Loader", appJar);
if (failed > 0) {
throw new RuntimeException("FINAL RESULT: " + failed + " out of " + numTests + " test case(s) have failed");
} else {
System.out.println("FINAL RESULT: All " + numTests + " test case(s) have passed!");
}
// Full GC. The cached objects in adjustable archive heap regions are
// scanned. The archive regions are verified. No error should be
// reported.
wb.fullGC();
}
static void testBootstrapCDS(String group, File jar) throws Throwable {
doTest(group, new Bar(), jar);
}
static void testAppCDSv1(String group, File jar) throws Throwable {
doTest(group, new Foo(), jar);
}
static void checkArchivedMirrorObject(Class klass) {
if (wb.areOpenArchiveHeapObjectsMapped()) {
if (!wb.isShared(klass)) {
failed ++;
System.out.println("FAILED. " + klass + " mirror object is not archived");
return;
}
}
}
static void doTest(String group, Intf object, File jar) throws Throwable {
numTests ++;
Class klass = object.getClass();
System.out.println();
System.out.println("++++++++++++++++++++++++++");
System.out.println("Test group: " + group);
System.out.println("Testing with classloader = " + klass.getClassLoader());
System.out.println("Testing with class = " + klass);
System.out.println("Test is shared = " + wb.isSharedClass(klass));
System.out.println("++++++++++++++++++++++++++");
// Check archived mirror object before redefine
checkArchivedMirrorObject(klass);
// Call get() before redefine. All strings in archived classes are shared.
String res = object.get();
System.out.println("get() returns " + res);
if (res.equals("buzz") && wb.isShared(res)) {
System.out.println("get() returns " + res + ", string is shared");
} else {
if (!res.equals("buzz")) {
System.out.println("FAILED. buzz is expected but got " + res);
} else {
System.out.println("FAILED. " + res + " is not shared");
}
failed ++;
return;
}
res = null; // release the local reference to the string
// Run GC
System.gc();
System.gc();
System.gc();
// Redefine the shared class
byte[] buff = Util.getClassFileFromJar(jar, klass.getName());
Util.replace(buff, "buzz", "huzz");
String f = "(failed)";
try {
instrumentation.redefineClasses(new ClassDefinition(klass, buff));
f = object.get();
} catch (UnmodifiableClassException|UnsupportedOperationException e) {
e.printStackTrace();
}
if (f.equals("huzz")) {
System.out.println("PASSED: object.get() after redefinition returns " + f);
} else {
System.out.println("FAILED: object.get() after redefinition returns " + f);
failed ++;
}
// Run GC. Should not crash.
System.gc();
System.gc();
System.gc();
// Check archived mirror object after redefine and GC
checkArchivedMirrorObject(klass);
System.out.println("++++++++++++++++++++++++++++++++++++++++++++++++ (done)\n\n");
}
}
|