|
| 1 | +/* |
| 2 | + * Copyright (c) 2020, 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 8241390 |
| 27 | + * @summary Test recursively retransforms the same class. The test hangs if |
| 28 | + * a deadlock happens. |
| 29 | + * @requires vm.jvmti |
| 30 | + * @library /test/lib |
| 31 | + * @modules java.instrument |
| 32 | + * @compile TransformerDeadlockTest.java |
| 33 | + * @run driver TransformerDeadlockTest |
| 34 | + */ |
| 35 | + |
| 36 | +import jdk.test.lib.process.ProcessTools; |
| 37 | + |
| 38 | +import java.lang.instrument.ClassFileTransformer; |
| 39 | +import java.lang.instrument.IllegalClassFormatException; |
| 40 | +import java.lang.instrument.Instrumentation; |
| 41 | +import java.nio.file.Files; |
| 42 | +import java.nio.file.Path; |
| 43 | +import java.nio.file.Paths; |
| 44 | +import java.security.ProtectionDomain; |
| 45 | + |
| 46 | + |
| 47 | +public class TransformerDeadlockTest { |
| 48 | + |
| 49 | + private static String manifest = "Premain-Class: " + |
| 50 | + TransformerDeadlockTest.Agent.class.getName() + "\n" |
| 51 | + + "Can-Retransform-Classes: true\n" |
| 52 | + + "Can-Retransform-Classes: true\n"; |
| 53 | + |
| 54 | + private static String CP = System.getProperty("test.classes"); |
| 55 | + |
| 56 | + public static void main(String args[]) throws Throwable { |
| 57 | + String agentJar = buildAgent(); |
| 58 | + ProcessTools.executeProcess( |
| 59 | + ProcessTools.createJavaProcessBuilder( |
| 60 | + "-javaagent:" + agentJar, |
| 61 | + TransformerDeadlockTest.Agent.class.getName()) |
| 62 | + ).shouldHaveExitValue(0); |
| 63 | + } |
| 64 | + |
| 65 | + private static String buildAgent() throws Exception { |
| 66 | + Path jar = Files.createTempFile(Paths.get("."), null, ".jar"); |
| 67 | + String jarPath = jar.toAbsolutePath().toString(); |
| 68 | + ClassFileInstaller.writeJar(jarPath, |
| 69 | + ClassFileInstaller.Manifest.fromString(manifest), |
| 70 | + TransformerDeadlockTest.class.getName()); |
| 71 | + return jarPath; |
| 72 | + } |
| 73 | + |
| 74 | + public static class Agent implements ClassFileTransformer { |
| 75 | + private static Instrumentation instrumentation; |
| 76 | + |
| 77 | + public static void premain(String agentArgs, Instrumentation inst) { |
| 78 | + instrumentation = inst; |
| 79 | + } |
| 80 | + |
| 81 | + @Override |
| 82 | + public byte[] transform( |
| 83 | + ClassLoader loader, |
| 84 | + String className, |
| 85 | + Class<?> classBeingRedefined, |
| 86 | + ProtectionDomain protectionDomain, |
| 87 | + byte[] classfileBuffer) |
| 88 | + throws IllegalClassFormatException { |
| 89 | + |
| 90 | + if (!TransformerDeadlockTest.class.getName().replace(".", "/").equals(className)) { |
| 91 | + return null; |
| 92 | + } |
| 93 | + invokeRetransform(); |
| 94 | + return classfileBuffer; |
| 95 | + |
| 96 | + } |
| 97 | + |
| 98 | + public static void main(String[] args) throws Exception { |
| 99 | + instrumentation.addTransformer(new TransformerDeadlockTest.Agent(), true); |
| 100 | + |
| 101 | + try { |
| 102 | + instrumentation.retransformClasses(TransformerDeadlockTest.class); |
| 103 | + } catch (Exception e) { |
| 104 | + throw new RuntimeException(e); |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + private static void invokeRetransform() { |
| 109 | + try { |
| 110 | + instrumentation.retransformClasses(TransformerDeadlockTest.class); |
| 111 | + } catch (Exception e) { |
| 112 | + throw new RuntimeException(e); |
| 113 | + } finally { |
| 114 | + } |
| 115 | + } |
| 116 | + } |
| 117 | +} |
0 commit comments