Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/hotspot/jtreg/runtime/MemberName/ResolvedMethodTableHash.java
40942 views
1
/*
2
* Copyright (c) 2020, 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 8249719
27
* @summary ResolvedMethodTable hash function should take method class into account
28
* @run main/othervm/manual -Xmx256m -XX:MaxMetaspaceSize=256m ResolvedMethodTableHash 200000
29
*/
30
31
import java.lang.invoke.MethodHandle;
32
import java.lang.invoke.MethodHandles;
33
import java.lang.invoke.MethodType;
34
import java.nio.ByteBuffer;
35
import java.util.ArrayList;
36
import java.util.List;
37
38
// The test generates thousands MethodHandles to the methods of the same name
39
// and the same signature. This should not take too long, unless Method hash
40
// function takes only the name and the signature as an input.
41
public class ResolvedMethodTableHash extends ClassLoader {
42
43
// Generate a MethodHandle for ClassName.m()
44
private MethodHandle generate(String className) throws ReflectiveOperationException {
45
byte[] buf = new byte[100];
46
int size = writeClass(buf, className);
47
Class<?> cls = defineClass(null, buf, 0, size);
48
return MethodHandles.publicLookup().findStatic(cls, "m", MethodType.methodType(void.class));
49
}
50
51
private MethodHandle generateWithSameName() throws ReflectiveOperationException {
52
byte[] buf = new byte[100];
53
int size = writeClass(buf, "MH$$");
54
// use different classloader instances to load the classes with the same name
55
Class<?> cls = new ResolvedMethodTableHash().defineClass(null, buf, 0, size);
56
return MethodHandles.publicLookup().findStatic(cls, "m", MethodType.methodType(void.class));
57
}
58
59
// Produce a class file with the given name and a single method:
60
// public static native void m();
61
private int writeClass(byte[] buf, String className) {
62
return ByteBuffer.wrap(buf)
63
.putInt(0xCAFEBABE) // magic
64
.putInt(50) // version: 50
65
.putShort((short) 7) // constant_pool_count: 7
66
.put((byte) 7).putShort((short) 2)
67
.put((byte) 1).putShort((short) className.length()).put(className.getBytes())
68
.put((byte) 7).putShort((short) 4)
69
.put((byte) 1).putShort((short) 16).put("java/lang/Object".getBytes())
70
.put((byte) 1).putShort((short) 1).put("m".getBytes())
71
.put((byte) 1).putShort((short) 3).put("()V".getBytes())
72
.putShort((short) 0x21) // access_flags: public super
73
.putShort((short) 1) // this_class: #1
74
.putShort((short) 3) // super_class: #3
75
.putShort((short) 0) // interfaces_count: 0
76
.putShort((short) 0) // fields_count: 0
77
.putShort((short) 1) // methods_count: 1
78
.putShort((short) 0x109) // access_flags: public static native
79
.putShort((short) 5) // name_index: #5
80
.putShort((short) 6) // descriptor_index: #6
81
.putShort((short) 0) // attributes_count: 0
82
.putShort((short) 0) // attributes_count: 0
83
.position();
84
}
85
86
public static void main(String[] args) throws Exception {
87
ResolvedMethodTableHash generator = new ResolvedMethodTableHash();
88
List<MethodHandle> handles = new ArrayList<>();
89
90
int count = args.length > 0 ? Integer.parseInt(args[0]) : 200000;
91
92
for (int i = 0; i < count; i++) {
93
// prevents metaspace oom
94
if (i % 20 != 0) {
95
handles.add(generator.generate("MH$" + i));
96
} else {
97
handles.add(generator.generateWithSameName());
98
}
99
if (i % 1000 == 0) {
100
System.out.println("Generated " + i + " handles");
101
}
102
}
103
104
System.out.println("Test passed");
105
}
106
}
107
108