Path: blob/master/test/hotspot/jtreg/serviceability/jvmti/RedefineClasses/RedefineLeak.java
41153 views
/*1* Copyright (c) 2016, 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* @library /test/lib26* @summary Test that redefinition reuses metaspace blocks that are freed27* @requires vm.jvmti28* @modules java.base/jdk.internal.misc29* @modules java.instrument30* jdk.jartool/sun.tools.jar31* @run main RedefineLeak buildagent32* @run main/othervm/timeout=6000 RedefineLeak runtest33*/3435import java.io.FileNotFoundException;36import java.io.PrintWriter;37import java.lang.RuntimeException;38import java.lang.instrument.ClassFileTransformer;39import java.lang.instrument.Instrumentation;40import java.security.ProtectionDomain;41import java.lang.instrument.IllegalClassFormatException;42import jdk.test.lib.process.ProcessTools;43import jdk.test.lib.process.OutputAnalyzer;44import jdk.test.lib.helpers.ClassFileInstaller;4546public class RedefineLeak {47static class Tester {}4849static class LoggingTransformer implements ClassFileTransformer {50static int transformCount = 0;5152public LoggingTransformer() {}5354public byte[] transform(ClassLoader loader, String className, Class classBeingRedefined,55ProtectionDomain protectionDomain, byte[] classfileBuffer) throws IllegalClassFormatException {5657transformCount++;58if (transformCount % 1000 == 0) System.out.println("transformCount:" + transformCount);59return null;60}61}6263public static void premain(String agentArgs, Instrumentation inst) throws Exception {64LoggingTransformer t = new LoggingTransformer();65inst.addTransformer(t, true);66{67Class demoClass = Class.forName("RedefineLeak$Tester");6869for (int i = 0; i < 10000; i++) {70inst.retransformClasses(demoClass);71}72}73System.gc();74}75private static void buildAgent() {76try {77ClassFileInstaller.main("RedefineLeak");78} catch (Exception e) {79throw new RuntimeException("Could not write agent classfile", e);80}8182try {83PrintWriter pw = new PrintWriter("MANIFEST.MF");84pw.println("Premain-Class: RedefineLeak");85pw.println("Agent-Class: RedefineLeak");86pw.println("Can-Redefine-Classes: true");87pw.println("Can-Retransform-Classes: true");88pw.close();89} catch (FileNotFoundException e) {90throw new RuntimeException("Could not write manifest file for the agent", e);91}9293sun.tools.jar.Main jarTool = new sun.tools.jar.Main(System.out, System.err, "jar");94if (!jarTool.run(new String[] { "-cmf", "MANIFEST.MF", "redefineagent.jar", "RedefineLeak.class" })) {95throw new RuntimeException("Could not write the agent jar file");96}97}98public static void main(String argv[]) throws Exception {99if (argv.length == 1 && argv[0].equals("buildagent")) {100buildAgent();101return;102}103if (argv.length == 1 && argv[0].equals("runtest")) {104// run outside of jtreg to not OOM on jtreg classes that are loaded after metaspace is full105ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(106"-XX:MetaspaceSize=12m",107"-XX:MaxMetaspaceSize=12m",108"-javaagent:redefineagent.jar",109"RedefineLeak");110OutputAnalyzer output = new OutputAnalyzer(pb.start());111output.shouldContain("transformCount:10000");112output.shouldHaveExitValue(0);113}114}115}116117118