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