Path: blob/master/test/hotspot/jtreg/serviceability/jvmti/RedefineClasses/TransformerDeadlockTest.java
41153 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* @library /test/lib30* @modules java.instrument31* @compile TransformerDeadlockTest.java32* @run driver TransformerDeadlockTest33*/3435import jdk.test.lib.process.ProcessTools;36import jdk.test.lib.helpers.ClassFileInstaller;3738import java.lang.instrument.ClassFileTransformer;39import java.lang.instrument.IllegalClassFormatException;40import java.lang.instrument.Instrumentation;41import java.nio.file.Files;42import java.nio.file.Path;43import java.nio.file.Paths;44import java.security.ProtectionDomain;4546public class TransformerDeadlockTest {4748private static String manifest = "Premain-Class: " +49TransformerDeadlockTest.Agent.class.getName() + "\n"50+ "Can-Retransform-Classes: true\n"51+ "Can-Retransform-Classes: true\n";5253private static String CP = System.getProperty("test.classes");5455public static void main(String args[]) throws Throwable {56String agentJar = buildAgent();57ProcessTools.executeProcess(58ProcessTools.createJavaProcessBuilder(59"-javaagent:" + agentJar,60TransformerDeadlockTest.Agent.class.getName())61).shouldHaveExitValue(0);62}6364private static String buildAgent() throws Exception {65Path jar = Files.createTempFile(Paths.get("."), null, ".jar");66String jarPath = jar.toAbsolutePath().toString();67ClassFileInstaller.writeJar(jarPath,68ClassFileInstaller.Manifest.fromString(manifest),69TransformerDeadlockTest.class.getName());70return jarPath;71}7273public static class Agent implements ClassFileTransformer {74private static Instrumentation instrumentation;7576public static void premain(String agentArgs, Instrumentation inst) {77instrumentation = inst;78}7980@Override81public byte[] transform(82ClassLoader loader,83String className,84Class<?> classBeingRedefined,85ProtectionDomain protectionDomain,86byte[] classfileBuffer)87throws IllegalClassFormatException {8889if (!TransformerDeadlockTest.class.getName().replace(".", "/").equals(className)) {90return null;91}92invokeRetransform();93return classfileBuffer;9495}9697public static void main(String[] args) throws Exception {98instrumentation.addTransformer(new TransformerDeadlockTest.Agent(), true);99100try {101instrumentation.retransformClasses(TransformerDeadlockTest.class);102} catch (Exception e) {103throw new RuntimeException(e);104}105}106107private static void invokeRetransform() {108try {109instrumentation.retransformClasses(TransformerDeadlockTest.class);110} catch (Exception e) {111throw new RuntimeException(e);112} finally {113}114}115}116}117118119