Path: blob/master/test/hotspot/jtreg/serviceability/jvmti/RedefineClasses/RedefineLeak.java
66646 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* @requires vm.flagless29* @modules java.base/jdk.internal.misc30* @modules java.instrument31* jdk.jartool/sun.tools.jar32* @run driver RedefineLeak buildagent33* @run driver/timeout=6000 RedefineLeak runtest34*/3536import java.io.FileNotFoundException;37import java.io.PrintWriter;38import java.lang.RuntimeException;39import java.lang.instrument.ClassFileTransformer;40import java.lang.instrument.Instrumentation;41import java.security.ProtectionDomain;42import java.lang.instrument.IllegalClassFormatException;43import jdk.test.lib.process.ProcessTools;44import jdk.test.lib.process.OutputAnalyzer;45import jdk.test.lib.helpers.ClassFileInstaller;4647public class RedefineLeak {48static class Tester {}4950static class LoggingTransformer implements ClassFileTransformer {51static int transformCount = 0;5253public LoggingTransformer() {}5455public byte[] transform(ClassLoader loader, String className, Class classBeingRedefined,56ProtectionDomain protectionDomain, byte[] classfileBuffer) throws IllegalClassFormatException {5758transformCount++;59if (transformCount % 1000 == 0) System.out.println("transformCount:" + transformCount);60return null;61}62}6364public static void premain(String agentArgs, Instrumentation inst) throws Exception {65LoggingTransformer t = new LoggingTransformer();66inst.addTransformer(t, true);67{68Class demoClass = Class.forName("RedefineLeak$Tester");6970for (int i = 0; i < 10000; i++) {71inst.retransformClasses(demoClass);72}73}74System.gc();75}76private static void buildAgent() {77try {78ClassFileInstaller.main("RedefineLeak");79} catch (Exception e) {80throw new RuntimeException("Could not write agent classfile", e);81}8283try {84PrintWriter pw = new PrintWriter("MANIFEST.MF");85pw.println("Premain-Class: RedefineLeak");86pw.println("Agent-Class: RedefineLeak");87pw.println("Can-Redefine-Classes: true");88pw.println("Can-Retransform-Classes: true");89pw.close();90} catch (FileNotFoundException e) {91throw new RuntimeException("Could not write manifest file for the agent", e);92}9394sun.tools.jar.Main jarTool = new sun.tools.jar.Main(System.out, System.err, "jar");95if (!jarTool.run(new String[] { "-cmf", "MANIFEST.MF", "redefineagent.jar", "RedefineLeak.class" })) {96throw new RuntimeException("Could not write the agent jar file");97}98}99public static void main(String argv[]) throws Exception {100if (argv.length == 1 && argv[0].equals("buildagent")) {101buildAgent();102return;103}104if (argv.length == 1 && argv[0].equals("runtest")) {105// run outside of jtreg to not OOM on jtreg classes that are loaded after metaspace is full106ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(107"-XX:MetaspaceSize=12m",108"-XX:MaxMetaspaceSize=12m",109"-javaagent:redefineagent.jar",110"RedefineLeak");111OutputAnalyzer output = new OutputAnalyzer(pb.start());112output.shouldContain("transformCount:10000");113output.shouldHaveExitValue(0);114}115}116}117118119