Path: blob/master/test/hotspot/jtreg/serviceability/jvmti/RedefineClasses/TransformerDeadlockTest.java
66646 views
/*1* Copyright (c) 2020, 2021, 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 824139026* @summary Test recursively retransforms the same class. The test hangs if27* a deadlock happens.28* @requires vm.jvmti29* @requires vm.flagless30* @library /test/lib31* @modules java.instrument32* @compile TransformerDeadlockTest.java33* @run driver TransformerDeadlockTest34*/3536import jdk.test.lib.process.ProcessTools;37import jdk.test.lib.helpers.ClassFileInstaller;3839import java.lang.instrument.ClassFileTransformer;40import java.lang.instrument.IllegalClassFormatException;41import java.lang.instrument.Instrumentation;42import java.nio.file.Files;43import java.nio.file.Path;44import java.nio.file.Paths;45import java.security.ProtectionDomain;4647public class TransformerDeadlockTest {4849private static String manifest = "Premain-Class: " +50TransformerDeadlockTest.Agent.class.getName() + "\n"51+ "Can-Retransform-Classes: true\n"52+ "Can-Retransform-Classes: true\n";5354private static String CP = System.getProperty("test.classes");5556public static void main(String args[]) throws Throwable {57String agentJar = buildAgent();58ProcessTools.executeProcess(59ProcessTools.createJavaProcessBuilder(60"-javaagent:" + agentJar,61TransformerDeadlockTest.Agent.class.getName())62).shouldHaveExitValue(0);63}6465private static String buildAgent() throws Exception {66Path jar = Files.createTempFile(Paths.get("."), null, ".jar");67String jarPath = jar.toAbsolutePath().toString();68ClassFileInstaller.writeJar(jarPath,69ClassFileInstaller.Manifest.fromString(manifest),70TransformerDeadlockTest.class.getName());71return jarPath;72}7374public static class Agent implements ClassFileTransformer {75private static Instrumentation instrumentation;7677public static void premain(String agentArgs, Instrumentation inst) {78instrumentation = inst;79}8081@Override82public byte[] transform(83ClassLoader loader,84String className,85Class<?> classBeingRedefined,86ProtectionDomain protectionDomain,87byte[] classfileBuffer)88throws IllegalClassFormatException {8990if (!TransformerDeadlockTest.class.getName().replace(".", "/").equals(className)) {91return null;92}93invokeRetransform();94return classfileBuffer;9596}9798public static void main(String[] args) throws Exception {99instrumentation.addTransformer(new TransformerDeadlockTest.Agent(), true);100101try {102instrumentation.retransformClasses(TransformerDeadlockTest.class);103} catch (Exception e) {104throw new RuntimeException(e);105}106}107108private static void invokeRetransform() {109try {110instrumentation.retransformClasses(TransformerDeadlockTest.class);111} catch (Exception e) {112throw new RuntimeException(e);113} finally {114}115}116}117}118119120