Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/hotspot/jtreg/runtime/MemberName/MemberNameLeak.java
40942 views
1
/*
2
* Copyright (c) 2017, 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
* @bug 8174749 8213307
27
* @summary MemberNameTable should reuse entries
28
* @library /test/lib
29
* @modules java.base/jdk.internal.misc
30
* @modules java.compiler
31
* @build sun.hotspot.WhiteBox
32
* @run driver jdk.test.lib.helpers.ClassFileInstaller sun.hotspot.WhiteBox
33
* @run main/othervm -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI -Xbootclasspath/a:. MemberNameLeak
34
*/
35
36
import java.io.*;
37
import java.nio.file.*;
38
import java.lang.invoke.*;
39
import java.lang.reflect.*;
40
import java.text.*;
41
import java.util.*;
42
import jdk.test.lib.process.OutputAnalyzer;
43
import jdk.test.lib.process.ProcessTools;
44
import jdk.test.lib.Utils;
45
import sun.hotspot.WhiteBox;
46
import sun.hotspot.code.Compiler;
47
import sun.hotspot.gc.GC;
48
import jdk.test.lib.classloader.ClassWithManyMethodsClassLoader;
49
50
public class MemberNameLeak {
51
private static String className = "MemberNameLeakTestClass";
52
private static String methodPrefix = "method";
53
// The size of the ResolvedMethodTable is 1024. 2000 entries
54
// is enough to trigger a grow/cleaning of the table after a GC.
55
private static int methodCount = 2000;
56
public static ArrayList<MethodHandle> keepAlive;
57
58
static class Leak {
59
public void callMe() {
60
}
61
62
public static void main(String[] args) throws Throwable {
63
Leak leak = new Leak();
64
WhiteBox wb = WhiteBox.getWhiteBox();
65
66
keepAlive = new ArrayList<>(methodCount);
67
68
ClassWithManyMethodsClassLoader classLoader = new ClassWithManyMethodsClassLoader();
69
Class<?> clazz = classLoader.create(className, methodPrefix, methodCount);
70
71
long before = wb.resolvedMethodItemsCount();
72
73
Object o = clazz.newInstance();
74
MethodHandles.Lookup lookup = MethodHandles.privateLookupIn(clazz, MethodHandles.lookup());
75
76
for (int i = 0; i < methodCount; i++) {
77
MethodType mt = MethodType.fromMethodDescriptorString("()V", classLoader);
78
String methodName = methodPrefix + i;
79
// findSpecial leaks some native mem
80
// Add entry to ResolvedMethodTable.
81
MethodHandle mh0 = lookup.findSpecial(clazz, methodName, mt, clazz);
82
// Find entry in ResolvedMethodTable.
83
MethodHandle mh1 = lookup.findSpecial(clazz, methodName, mt, clazz);
84
85
mh1.invoke(o);
86
87
keepAlive.add(mh1);
88
}
89
90
long after = wb.resolvedMethodItemsCount();
91
92
System.out.println("wb.resolvedMethodItemsCount() after setup: " + after);
93
94
if (after == before) {
95
throw new RuntimeException("Too few resolved methods");
96
}
97
98
keepAlive = null;
99
100
// Wait until ServiceThread cleans ResolvedMethod table
101
int cnt = 0;
102
while (true) {
103
if (cnt++ % 30 == 0) {
104
System.gc(); // make mh unused
105
}
106
107
if (after > wb.resolvedMethodItemsCount() + 50) {
108
// Entries have been removed.
109
break;
110
}
111
112
Thread.sleep(100);
113
}
114
}
115
}
116
117
private static Path createGcLogPath(String prefix) throws IOException {
118
Path gcLog = Utils.createTempFile(prefix, "log");
119
Files.delete(gcLog);
120
return gcLog;
121
}
122
123
private static DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
124
125
public static void test(GC gc, boolean doConcurrent) throws Throwable {
126
Path gcLogPath = createGcLogPath("gc." + gc + "." + doConcurrent);
127
System.err.println("test(" + gc + ", " + doConcurrent + ")" + " " + dateFormat.format(new Date()));
128
// Run this Leak class with logging
129
ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(
130
"-Xlog:membername+table=trace,gc+verify=debug,gc:" + gcLogPath + ":time,utctime,uptime,pid,level,tags",
131
"-XX:+UnlockExperimentalVMOptions",
132
"-XX:+UnlockDiagnosticVMOptions",
133
"-XX:+WhiteBoxAPI",
134
"-Xbootclasspath/a:.",
135
"-XX:+VerifyBeforeGC",
136
"-XX:+VerifyAfterGC",
137
doConcurrent ? "-XX:+ExplicitGCInvokesConcurrent" : "-XX:-ExplicitGCInvokesConcurrent",
138
"-XX:+ClassUnloading",
139
"-XX:+ClassUnloadingWithConcurrentMark",
140
"-XX:+Use" + gc + "GC",
141
Leak.class.getName());
142
143
// Check process
144
OutputAnalyzer output = new OutputAnalyzer(pb.start());
145
output.outputTo(System.out);
146
output.errorTo(System.err);
147
output.shouldHaveExitValue(0);
148
149
// Check gc log file
150
OutputAnalyzer gcLogOutput = new OutputAnalyzer(gcLogPath);
151
152
// Hardcoded names for classes generated by GeneratedClassLoader
153
String descriptor = className + "." + methodPrefix + "0()V";
154
gcLogOutput.shouldContain("ResolvedMethod entry added for " + descriptor);
155
gcLogOutput.shouldContain("ResolvedMethod entry found for " + descriptor);
156
gcLogOutput.shouldContain("ResolvedMethod entry removed");
157
158
System.err.println("test(" + gc + ", " + doConcurrent + ")" + " done " + dateFormat.format(new Date()));
159
}
160
161
private static boolean supportsSTW(GC gc) {
162
return !(gc == GC.Epsilon);
163
}
164
165
private static boolean supportsConcurrent(GC gc) {
166
return !(gc == GC.Epsilon || gc == GC.Serial || gc == GC.Parallel);
167
}
168
169
private static void test(GC gc) throws Throwable {
170
if (supportsSTW(gc)) {
171
test(gc, false);
172
}
173
if (supportsConcurrent(gc)) {
174
test(gc, true);
175
}
176
}
177
178
public static void main(java.lang.String[] unused) throws Throwable {
179
test(GC.selected());
180
}
181
}
182
183