1 /* 2 * Copyright (c) 2016, 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 * @library /test/lib 27 * @summary Test that redefinition reuses metaspace blocks that are freed 28 * @modules java.base/jdk.internal.misc 29 * @modules java.instrument 30 * jdk.jartool/sun.tools.jar 31 * @run main RedefineLeak buildagent 32 * @run main/othervm/timeout=6000 RedefineLeak runtest 33 */ 34 35 import java.io.FileNotFoundException; 36 import java.io.PrintWriter; 37 import java.lang.RuntimeException; 38 import java.lang.instrument.ClassFileTransformer; 39 import java.lang.instrument.Instrumentation; 40 import java.security.ProtectionDomain; 41 import java.lang.instrument.IllegalClassFormatException; 42 import jdk.test.lib.process.ProcessTools; 43 import jdk.test.lib.process.OutputAnalyzer; 44 45 public class RedefineLeak { 46 static class Tester {} 47 48 static class LoggingTransformer implements ClassFileTransformer { 49 static int transformCount = 0; 50 51 public LoggingTransformer() {} 52 53 public byte[] transform(ClassLoader loader, String className, Class classBeingRedefined, 54 ProtectionDomain protectionDomain, byte[] classfileBuffer) throws IllegalClassFormatException { 55 56 transformCount++; 57 if (transformCount % 1000 == 0) System.out.println("transformCount:" + transformCount); 58 return null; 59 } 60 } 61 62 public static void premain(String agentArgs, Instrumentation inst) throws Exception { 63 LoggingTransformer t = new LoggingTransformer(); 64 inst.addTransformer(t, true); 65 { 66 Class demoClass = Class.forName("RedefineLeak$Tester"); 67 68 for (int i = 0; i < 10000; i++) { 69 inst.retransformClasses(demoClass); 70 } 71 } 72 System.gc(); 73 } 74 private static void buildAgent() { 75 try { 76 ClassFileInstaller.main("RedefineLeak"); 77 } catch (Exception e) { 78 throw new RuntimeException("Could not write agent classfile", e); 79 } 80 81 try { 82 PrintWriter pw = new PrintWriter("MANIFEST.MF"); 83 pw.println("Premain-Class: RedefineLeak"); 84 pw.println("Agent-Class: RedefineLeak"); 85 pw.println("Can-Redefine-Classes: true"); 86 pw.println("Can-Retransform-Classes: true"); 87 pw.close(); 88 } catch (FileNotFoundException e) { 89 throw new RuntimeException("Could not write manifest file for the agent", e); 90 } 91 92 sun.tools.jar.Main jarTool = new sun.tools.jar.Main(System.out, System.err, "jar"); 93 if (!jarTool.run(new String[] { "-cmf", "MANIFEST.MF", "redefineagent.jar", "RedefineLeak.class" })) { 94 throw new RuntimeException("Could not write the agent jar file"); 95 } 96 } 97 public static void main(String argv[]) throws Exception { 98 if (argv.length == 1 && argv[0].equals("buildagent")) { 99 buildAgent(); 100 return; 101 } 102 if (argv.length == 1 && argv[0].equals("runtest")) { 103 // run outside of jtreg to not OOM on jtreg classes that are loaded after metaspace is full 104 String[] javaArgs1 = { "-XX:MetaspaceSize=12m", "-XX:MaxMetaspaceSize=12m", 105 "-javaagent:redefineagent.jar", "RedefineLeak"}; 106 ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(javaArgs1); 107 108 OutputAnalyzer output = new OutputAnalyzer(pb.start()); 109 output.shouldContain("transformCount:10000"); 110 } 111 } 112 }