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