Path: blob/master/test/hotspot/jtreg/compiler/jsr292/RedefineMethodUsedByMultipleMethodHandles.java
41152 views
/*1* Copyright (c) 2014, 2020, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/**24* @test25* @bug 804223526* @summary redefining method used by multiple MethodHandles crashes VM27* @library /28* @modules java.base/jdk.internal.org.objectweb.asm29* java.compiler30* java.instrument31* jdk.attach32* @requires vm.jvmti33*34* @run main/othervm -Djdk.attach.allowAttachSelf compiler.jsr292.RedefineMethodUsedByMultipleMethodHandles35*/3637package compiler.jsr292;3839import jdk.internal.org.objectweb.asm.ClassReader;40import jdk.internal.org.objectweb.asm.ClassVisitor;41import jdk.internal.org.objectweb.asm.ClassWriter;42import jdk.internal.org.objectweb.asm.MethodVisitor;43import jdk.internal.org.objectweb.asm.Opcodes;4445import java.io.FileOutputStream;46import java.io.IOException;47import java.io.InputStream;48import java.lang.instrument.ClassFileTransformer;49import java.lang.instrument.IllegalClassFormatException;50import java.lang.instrument.Instrumentation;51import java.lang.invoke.MethodHandle;52import java.lang.invoke.MethodHandles;53import java.lang.invoke.MethodHandles.Lookup;54import java.lang.reflect.Method;55import java.nio.file.Files;56import java.nio.file.Path;57import java.security.ProtectionDomain;58import java.util.jar.Attributes;59import java.util.jar.JarEntry;60import java.util.jar.JarOutputStream;61import java.util.jar.Manifest;6263public class RedefineMethodUsedByMultipleMethodHandles {6465static class Foo {66public static Object getName() {67return "foo";68}69}7071public static void main(String[] args) throws Throwable {7273Lookup lookup = MethodHandles.lookup();74Method fooMethod = Foo.class.getDeclaredMethod("getName");7576// fooMH2 displaces fooMH1 from the MemberNamesTable77MethodHandle fooMH1 = lookup.unreflect(fooMethod);78MethodHandle fooMH2 = lookup.unreflect(fooMethod);7980System.out.println("fooMH1.invoke = " + fooMH1.invokeExact());81System.out.println("fooMH2.invoke = " + fooMH2.invokeExact());8283// Redefining Foo.getName() causes vmtarget to be updated84// in fooMH2 but not fooMH185redefineFoo();8687// Full GC causes fooMH1.vmtarget to be deallocated88System.gc();8990// Calling fooMH1.vmtarget crashes the VM91System.out.println("fooMH1.invoke = " + fooMH1.invokeExact());92}9394/**95* Adds the class file bytes for {@code c} to {@code jar}.96*/97static void add(JarOutputStream jar, Class<?> c) throws IOException {98String classAsPath = c.getName().replace('.', '/') + ".class";99jar.putNextEntry(new JarEntry(classAsPath));100InputStream stream = c.getClassLoader().getResourceAsStream(classAsPath);101102int b;103while ((b = stream.read()) != -1) {104jar.write(b);105}106}107108static void redefineFoo() throws Exception {109Manifest manifest = new Manifest();110manifest.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION, "1.0");111Attributes mainAttrs = manifest.getMainAttributes();112mainAttrs.putValue("Agent-Class", FooAgent.class.getName());113mainAttrs.putValue("Can-Redefine-Classes", "true");114mainAttrs.putValue("Can-Retransform-Classes", "true");115116// The jar file will be added to the system classloader search path. It is not safe117// to delete it while the JVM is running, so make sure to create it in the test118// directory so it will be cleaned up by the test harness.119Path jar = Files.createTempFile(Path.of(""), "myagent", ".jar");120JarOutputStream jarStream = new JarOutputStream(new FileOutputStream(jar.toFile()), manifest);121add(jarStream, FooAgent.class);122add(jarStream, FooTransformer.class);123jarStream.close();124runAgent(jar);125}126127public static void runAgent(Path agent) throws Exception {128String pid = Long.toString(ProcessHandle.current().pid());129ClassLoader cl = ClassLoader.getSystemClassLoader();130Class<?> c = Class.forName("com.sun.tools.attach.VirtualMachine", true, cl);131Method attach = c.getDeclaredMethod("attach", String.class);132Method loadAgent = c.getDeclaredMethod("loadAgent", String.class);133Method detach = c.getDeclaredMethod("detach");134Object vm = attach.invoke(null, pid);135loadAgent.invoke(vm, agent.toString());136detach.invoke(vm);137}138139public static class FooAgent {140141public static void agentmain(@SuppressWarnings("unused") String args, Instrumentation inst) throws Exception {142assert inst.isRedefineClassesSupported();143assert inst.isRetransformClassesSupported();144inst.addTransformer(new FooTransformer(), true);145Class<?>[] classes = inst.getAllLoadedClasses();146for (int i = 0; i < classes.length; i++) {147Class<?> c = classes[i];148if (c == Foo.class) {149inst.retransformClasses(new Class[]{c});150}151}152}153}154155static class FooTransformer implements ClassFileTransformer {156157@Override158public byte[] transform(ClassLoader cl, String className, Class<?> classBeingRedefined, ProtectionDomain protectionDomain, byte[] classfileBuffer) throws IllegalClassFormatException {159if (Foo.class.equals(classBeingRedefined)) {160System.out.println("redefining " + classBeingRedefined);161ClassReader cr = new ClassReader(classfileBuffer);162ClassWriter cw = new ClassWriter(cr, ClassWriter.COMPUTE_FRAMES);163ClassVisitor adapter = new ClassVisitor(Opcodes.ASM5, cw) {164@Override165public MethodVisitor visitMethod(int access, String base, String desc, String signature, String[] exceptions) {166MethodVisitor mv = cv.visitMethod(access, base, desc, signature, exceptions);167if (mv != null) {168mv = new MethodVisitor(Opcodes.ASM5, mv) {169@Override170public void visitLdcInsn(Object cst) {171System.out.println("replacing \"" + cst + "\" with \"bar\"");172mv.visitLdcInsn("bar");173}174};175}176return mv;177}178};179180cr.accept(adapter, ClassReader.SKIP_FRAMES);181cw.visitEnd();182return cw.toByteArray();183}184return classfileBuffer;185}186}187}188189190