58775
|
1 |
/*
|
|
2 |
* Copyright (c) 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 |
* @test
|
|
26 |
* @bug 8232613
|
|
27 |
* @summary Ensure Object natives stay registered after redefinition
|
|
28 |
* @library /test/lib
|
|
29 |
* @modules java.base/jdk.internal.misc
|
|
30 |
* java.base/jdk.internal.org.objectweb.asm
|
|
31 |
* java.compiler
|
|
32 |
* java.instrument
|
|
33 |
* jdk.jartool/sun.tools.jar
|
|
34 |
* @run main RedefineObject buildagent
|
|
35 |
* @run main/othervm -javaagent:redefineagent.jar RedefineObject
|
|
36 |
*/
|
|
37 |
|
|
38 |
import static jdk.test.lib.Asserts.assertTrue;
|
|
39 |
import java.io.FileNotFoundException;
|
|
40 |
import java.io.PrintWriter;
|
|
41 |
import java.lang.RuntimeException;
|
|
42 |
import java.lang.instrument.ClassFileTransformer;
|
|
43 |
import java.lang.instrument.IllegalClassFormatException;
|
|
44 |
import java.lang.instrument.Instrumentation;
|
|
45 |
import java.lang.instrument.UnmodifiableClassException;
|
|
46 |
import java.security.ProtectionDomain;
|
|
47 |
import java.util.Arrays;
|
|
48 |
|
|
49 |
import jdk.internal.org.objectweb.asm.ClassReader;
|
|
50 |
import jdk.internal.org.objectweb.asm.ClassVisitor;
|
|
51 |
import jdk.internal.org.objectweb.asm.ClassWriter;
|
|
52 |
|
|
53 |
import static jdk.internal.org.objectweb.asm.Opcodes.ASM6;
|
|
54 |
import static jdk.internal.org.objectweb.asm.Opcodes.V1_8;
|
|
55 |
|
|
56 |
public class RedefineObject {
|
|
57 |
|
|
58 |
static Instrumentation inst;
|
|
59 |
|
|
60 |
public static void premain(String agentArgs, Instrumentation inst) {
|
|
61 |
RedefineObject.inst = inst;
|
|
62 |
}
|
|
63 |
|
|
64 |
static class Transformer implements ClassFileTransformer {
|
|
65 |
|
|
66 |
public byte[] asm(ClassLoader loader, String className,
|
|
67 |
Class<?> classBeingRedefined,
|
|
68 |
ProtectionDomain protectionDomain, byte[] classfileBuffer)
|
|
69 |
throws IllegalClassFormatException {
|
|
70 |
ClassWriter cw = new ClassWriter(0);
|
|
71 |
// Force an older ASM to force a bytecode update
|
|
72 |
ClassVisitor cv = new DummyClassVisitor(ASM6, cw) { };
|
|
73 |
ClassReader cr = new ClassReader(classfileBuffer);
|
|
74 |
cr.accept(cv, 0);
|
|
75 |
byte[] bytes = cw.toByteArray();
|
|
76 |
return bytes;
|
|
77 |
}
|
|
78 |
|
|
79 |
public class DummyClassVisitor extends ClassVisitor {
|
|
80 |
|
|
81 |
public DummyClassVisitor(int api, ClassVisitor cv) {
|
|
82 |
super(api, cv);
|
|
83 |
}
|
|
84 |
|
|
85 |
public void visit(
|
|
86 |
final int version,
|
|
87 |
final int access,
|
|
88 |
final String name,
|
|
89 |
final String signature,
|
|
90 |
final String superName,
|
|
91 |
final String[] interfaces) {
|
|
92 |
// Artificially lower to JDK 8 version to force a redefine
|
|
93 |
cv.visit(V1_8, access, name, signature, superName, interfaces);
|
|
94 |
}
|
|
95 |
}
|
|
96 |
|
|
97 |
@Override public byte[] transform(ClassLoader loader, String className,
|
|
98 |
Class<?> classBeingRedefined,
|
|
99 |
ProtectionDomain protectionDomain, byte[] classfileBuffer)
|
|
100 |
throws IllegalClassFormatException {
|
|
101 |
|
|
102 |
if (className.contains("java/lang/Object")) {
|
|
103 |
try {
|
|
104 |
// Here we remove and re-add the dummy fields. This shuffles the constant pool
|
|
105 |
return asm(loader, className, classBeingRedefined, protectionDomain, classfileBuffer);
|
|
106 |
} catch (Throwable e) {
|
|
107 |
// The retransform native code that called this method does not propagate
|
|
108 |
// exceptions. Instead of getting an uninformative generic error, catch
|
|
109 |
// problems here and print it, then exit.
|
|
110 |
e.printStackTrace();
|
|
111 |
System.exit(1);
|
|
112 |
}
|
|
113 |
}
|
|
114 |
return null;
|
|
115 |
}
|
|
116 |
}
|
|
117 |
|
|
118 |
private static void buildAgent() {
|
|
119 |
try {
|
|
120 |
ClassFileInstaller.main("RedefineObject");
|
|
121 |
} catch (Exception e) {
|
|
122 |
throw new RuntimeException("Could not write agent classfile", e);
|
|
123 |
}
|
|
124 |
|
|
125 |
try {
|
|
126 |
PrintWriter pw = new PrintWriter("MANIFEST.MF");
|
|
127 |
pw.println("Premain-Class: RedefineObject");
|
|
128 |
pw.println("Agent-Class: RedefineObject");
|
|
129 |
pw.println("Can-Retransform-Classes: true");
|
|
130 |
pw.close();
|
|
131 |
} catch (FileNotFoundException e) {
|
|
132 |
throw new RuntimeException("Could not write manifest file for the agent", e);
|
|
133 |
}
|
|
134 |
|
|
135 |
sun.tools.jar.Main jarTool = new sun.tools.jar.Main(System.out, System.err, "jar");
|
|
136 |
if (!jarTool.run(new String[] { "-cmf", "MANIFEST.MF", "redefineagent.jar", "RedefineObject.class" })) {
|
|
137 |
throw new RuntimeException("Could not write the agent jar file");
|
|
138 |
}
|
|
139 |
}
|
|
140 |
|
|
141 |
public static void main(String[] args) throws Exception {
|
|
142 |
|
|
143 |
int objHash = System.identityHashCode(Object.class);
|
|
144 |
System.out.println("Object hashCode: " + objHash);
|
|
145 |
if (args.length == 1 && args[0].equals("buildagent")) {
|
|
146 |
buildAgent();
|
|
147 |
return;
|
|
148 |
}
|
|
149 |
|
|
150 |
if (inst == null) {
|
|
151 |
throw new RuntimeException("Instrumentation object was null");
|
|
152 |
}
|
|
153 |
|
|
154 |
try {
|
|
155 |
inst.addTransformer(new RedefineObject.Transformer(), true);
|
|
156 |
inst.retransformClasses(Object.class);
|
|
157 |
} catch (UnmodifiableClassException e) {
|
|
158 |
throw new RuntimeException(e);
|
|
159 |
}
|
|
160 |
|
|
161 |
// Exercise native methods on Object after transform
|
|
162 |
Object b = new Object();
|
|
163 |
b.hashCode();
|
|
164 |
|
|
165 |
C c = new C();
|
|
166 |
assertTrue(c.hashCode() != c.clone().hashCode() || c != c.clone());
|
|
167 |
assertTrue(c.clone() instanceof C);
|
|
168 |
c = (C)c.clone(); // native method on new Object
|
|
169 |
}
|
|
170 |
|
|
171 |
private static class C implements Cloneable {
|
|
172 |
@Override
|
|
173 |
protected Object clone() throws CloneNotSupportedException {
|
|
174 |
return super.clone();
|
|
175 |
}
|
|
176 |
}
|
|
177 |
}
|