Mercurial > hg > graal-jvmci-8
comparison jvmci/jdk.vm.ci.runtime.test/src/jdk/vm/ci/runtime/test/RedefineClassTest.java @ 22672:1bbd4a7c274b
Rename jdk.internal.jvmci to jdk.vm.ci
author | Tom Rodriguez <tom.rodriguez@oracle.com> |
---|---|
date | Thu, 08 Oct 2015 17:28:41 -0700 |
parents | jvmci/jdk.internal.jvmci.runtime.test/src/jdk/internal/jvmci/runtime/test/RedefineClassTest.java@ec96f33a101d |
children |
comparison
equal
deleted
inserted
replaced
22671:97f30e4d0e95 | 22672:1bbd4a7c274b |
---|---|
1 /* | |
2 * Copyright (c) 2011, 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 package jdk.vm.ci.runtime.test; | |
24 | |
25 import static org.junit.Assume.assumeTrue; | |
26 | |
27 import java.io.FileOutputStream; | |
28 import java.io.IOException; | |
29 import java.io.InputStream; | |
30 import java.lang.instrument.ClassFileTransformer; | |
31 import java.lang.instrument.IllegalClassFormatException; | |
32 import java.lang.instrument.Instrumentation; | |
33 import java.lang.management.ManagementFactory; | |
34 import java.lang.reflect.Method; | |
35 import java.nio.file.Files; | |
36 import java.nio.file.Path; | |
37 import java.security.ProtectionDomain; | |
38 import java.util.Arrays; | |
39 import java.util.jar.Attributes; | |
40 import java.util.jar.JarEntry; | |
41 import java.util.jar.JarOutputStream; | |
42 import java.util.jar.Manifest; | |
43 | |
44 import javax.tools.ToolProvider; | |
45 | |
46 import jdk.vm.ci.meta.ResolvedJavaMethod; | |
47 | |
48 import org.junit.Assert; | |
49 import org.junit.Test; | |
50 | |
51 /** | |
52 * Tests that {@link ResolvedJavaMethod}s are safe in the context of class redefinition being used | |
53 * to redefine the method to which they refer. | |
54 */ | |
55 public class RedefineClassTest extends TypeUniverse { | |
56 | |
57 static class Foo { | |
58 public static Object getName() { | |
59 return "foo"; | |
60 } | |
61 } | |
62 | |
63 @Test | |
64 public void test() throws Throwable { | |
65 | |
66 Method fooMethod = Foo.class.getDeclaredMethod("getName"); | |
67 | |
68 ResolvedJavaMethod foo1 = metaAccess.lookupJavaMethod(fooMethod); | |
69 ResolvedJavaMethod foo2 = metaAccess.lookupJavaMethod(fooMethod); | |
70 | |
71 String foo1Code = Arrays.toString(foo1.getCode()); | |
72 String foo2Code = Arrays.toString(foo2.getCode()); | |
73 | |
74 Assert.assertEquals("foo", Foo.getName()); | |
75 | |
76 redefineFoo(); | |
77 System.gc(); | |
78 | |
79 // Make sure the transformation happened | |
80 Assert.assertEquals("bar", Foo.getName()); | |
81 | |
82 Assert.assertEquals(foo1Code, Arrays.toString(foo1.getCode())); | |
83 Assert.assertEquals(foo2Code, Arrays.toString(foo1.getCode())); | |
84 } | |
85 | |
86 /** | |
87 * Adds the class file bytes for a given class to a JAR stream. | |
88 */ | |
89 static void add(JarOutputStream jar, Class<?> c) throws IOException { | |
90 String name = c.getName(); | |
91 String classAsPath = name.replace('.', '/') + ".class"; | |
92 jar.putNextEntry(new JarEntry(classAsPath)); | |
93 | |
94 InputStream stream = c.getClassLoader().getResourceAsStream(classAsPath); | |
95 | |
96 int nRead; | |
97 byte[] buf = new byte[1024]; | |
98 while ((nRead = stream.read(buf, 0, buf.length)) != -1) { | |
99 jar.write(buf, 0, nRead); | |
100 } | |
101 | |
102 jar.closeEntry(); | |
103 } | |
104 | |
105 protected void redefineFoo() throws Exception { | |
106 Manifest manifest = new Manifest(); | |
107 manifest.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION, "1.0"); | |
108 Attributes mainAttrs = manifest.getMainAttributes(); | |
109 mainAttrs.putValue("Agent-Class", FooAgent.class.getName()); | |
110 mainAttrs.putValue("Can-Redefine-Classes", "true"); | |
111 mainAttrs.putValue("Can-Retransform-Classes", "true"); | |
112 | |
113 Path jar = Files.createTempFile("myagent", ".jar"); | |
114 try { | |
115 JarOutputStream jarStream = new JarOutputStream(new FileOutputStream(jar.toFile()), manifest); | |
116 add(jarStream, FooAgent.class); | |
117 add(jarStream, FooTransformer.class); | |
118 jarStream.close(); | |
119 | |
120 loadAgent(jar); | |
121 } finally { | |
122 Files.deleteIfExists(jar); | |
123 } | |
124 } | |
125 | |
126 public static void loadAgent(Path agent) throws Exception { | |
127 String vmName = ManagementFactory.getRuntimeMXBean().getName(); | |
128 int p = vmName.indexOf('@'); | |
129 assumeTrue("VM name not in <pid>@<host> format: " + vmName, p != -1); | |
130 String pid = vmName.substring(0, p); | |
131 ClassLoader cl = ToolProvider.getSystemToolClassLoader(); | |
132 Class<?> c = Class.forName("com.sun.tools.attach.VirtualMachine", true, cl); | |
133 Method attach = c.getDeclaredMethod("attach", String.class); | |
134 Method loadAgent = c.getDeclaredMethod("loadAgent", String.class, String.class); | |
135 Method detach = c.getDeclaredMethod("detach"); | |
136 Object vm = attach.invoke(null, pid); | |
137 loadAgent.invoke(vm, agent.toString(), ""); | |
138 detach.invoke(vm); | |
139 } | |
140 | |
141 public static class FooAgent { | |
142 | |
143 public static void agentmain(@SuppressWarnings("unused") String args, Instrumentation inst) throws Exception { | |
144 if (inst.isRedefineClassesSupported() && inst.isRetransformClassesSupported()) { | |
145 inst.addTransformer(new FooTransformer(), true); | |
146 Class<?>[] allClasses = inst.getAllLoadedClasses(); | |
147 for (int i = 0; i < allClasses.length; i++) { | |
148 Class<?> c = allClasses[i]; | |
149 if (c == Foo.class) { | |
150 inst.retransformClasses(new Class<?>[]{c}); | |
151 } | |
152 } | |
153 } | |
154 } | |
155 } | |
156 | |
157 /** | |
158 * This transformer replaces the first instance of the constant "foo" in the class file for | |
159 * {@link Foo} with "bar". | |
160 */ | |
161 static class FooTransformer implements ClassFileTransformer { | |
162 | |
163 @Override | |
164 public byte[] transform(ClassLoader cl, String className, Class<?> classBeingRedefined, ProtectionDomain protectionDomain, byte[] classfileBuffer) throws IllegalClassFormatException { | |
165 if (Foo.class.equals(classBeingRedefined)) { | |
166 String cf = new String(classfileBuffer); | |
167 int i = cf.indexOf("foo"); | |
168 Assert.assertTrue("cannot find \"foo\" constant in " + Foo.class.getSimpleName() + "'s class file", i > 0); | |
169 classfileBuffer[i] = 'b'; | |
170 classfileBuffer[i + 1] = 'a'; | |
171 classfileBuffer[i + 2] = 'r'; | |
172 } | |
173 return classfileBuffer; | |
174 } | |
175 } | |
176 } |