Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/hotspot/jtreg/gc/TestJNIWeak/TestJNIWeak.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
package gc.TestJNIWeak;
25
26
/* @test
27
* @bug 8166188 8178813
28
* @summary Test return of JNI weak global refs during concurrent
29
* marking, verifying the use of the load barrier to keep the
30
* referent alive.
31
* @library /test/lib
32
* @build sun.hotspot.WhiteBox
33
* @run driver jdk.test.lib.helpers.ClassFileInstaller sun.hotspot.WhiteBox
34
* @run main/othervm/native
35
* -Xbootclasspath/a:.
36
* -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI
37
* -Xint
38
* gc.TestJNIWeak.TestJNIWeak
39
* @run main/othervm/native
40
* -Xbootclasspath/a:.
41
* -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI
42
* -Xcomp
43
* gc.TestJNIWeak.TestJNIWeak
44
*/
45
46
import sun.hotspot.gc.GC;
47
import sun.hotspot.WhiteBox;
48
import jtreg.SkippedException;
49
import java.lang.ref.Reference;
50
51
public final class TestJNIWeak {
52
53
static {
54
System.loadLibrary("TestJNIWeak");
55
}
56
57
private static final WhiteBox WB = WhiteBox.getWhiteBox();
58
59
private static final class TestObject {
60
public final int value;
61
62
public TestObject(int value) {
63
this.value = value;
64
}
65
}
66
67
private volatile TestObject testObject = null;
68
69
private static native void registerObject(Object o);
70
private static native void unregisterObject();
71
private static native Object getReturnedWeak();
72
private static native Object getResolvedWeak();
73
74
// resolve controls whether getObject returns an explicitly
75
// resolved jweak value (when resolve is true), or returns the
76
// jweak directly and invokes the implicit resolution in the
77
// native call return value handling path (when resolve is false).
78
private boolean resolve = true;
79
80
TestJNIWeak(boolean resolve) {
81
this.resolve = resolve;
82
}
83
84
private Object getObject() {
85
if (resolve) {
86
return getResolvedWeak();
87
} else {
88
return getReturnedWeak();
89
}
90
}
91
92
// Create the test object and record it both strongly and weakly.
93
private void remember(int value) {
94
TestObject o = new TestObject(value);
95
registerObject(o);
96
testObject = o;
97
}
98
99
// Remove both strong and weak references to the current test object.
100
private void forget() {
101
unregisterObject();
102
testObject = null;
103
}
104
105
// Repeatedly perform full GC until o is in the old generation.
106
private void gcUntilOld(Object o) {
107
while (!WB.isObjectInOldGen(o)) {
108
WB.fullGC();
109
}
110
}
111
112
// Verify the weakly recorded object
113
private void checkValue(int value) throws Exception {
114
Object o = getObject();
115
if (o == null) {
116
throw new RuntimeException("Weak reference unexpectedly null");
117
}
118
TestObject t = (TestObject)o;
119
if (t.value != value) {
120
throw new RuntimeException("Incorrect value");
121
}
122
}
123
124
// Verify we can create a weak reference and get it back.
125
private void checkSanity() throws Exception {
126
System.out.println("running checkSanity");
127
try {
128
// Inhibit concurrent GC during this check.
129
WB.concurrentGCAcquireControl();
130
131
int value = 5;
132
try {
133
remember(value);
134
checkValue(value);
135
} finally {
136
forget();
137
}
138
139
} finally {
140
WB.concurrentGCReleaseControl();
141
}
142
}
143
144
// Verify weak ref value survives across collection if strong ref exists.
145
private void checkSurvival() throws Exception {
146
System.out.println("running checkSurvival");
147
try {
148
int value = 10;
149
try {
150
remember(value);
151
checkValue(value);
152
gcUntilOld(testObject);
153
// Run a concurrent collection after object is old.
154
WB.concurrentGCAcquireControl();
155
WB.concurrentGCRunTo(WB.AFTER_MARKING_STARTED);
156
WB.concurrentGCRunToIdle();
157
// Verify weak ref still has expected value.
158
checkValue(value);
159
} finally {
160
forget();
161
}
162
} finally {
163
WB.concurrentGCReleaseControl();
164
}
165
}
166
167
// Verify weak ref cleared if no strong ref exists.
168
private void checkClear() throws Exception {
169
System.out.println("running checkClear");
170
try {
171
int value = 15;
172
try {
173
remember(value);
174
checkValue(value);
175
gcUntilOld(testObject);
176
// Run a concurrent collection after object is old.
177
WB.concurrentGCAcquireControl();
178
WB.concurrentGCRunTo(WB.AFTER_MARKING_STARTED);
179
WB.concurrentGCRunToIdle();
180
checkValue(value);
181
testObject = null;
182
// Run a concurrent collection after strong ref removed.
183
WB.concurrentGCRunTo(WB.AFTER_MARKING_STARTED);
184
WB.concurrentGCRunToIdle();
185
// Verify weak ref cleared as expected.
186
Object recorded = getObject();
187
if (recorded != null) {
188
throw new RuntimeException("expected clear");
189
}
190
} finally {
191
forget();
192
}
193
} finally {
194
WB.concurrentGCReleaseControl();
195
}
196
}
197
198
// Verify weak ref not cleared if no strong ref at start of
199
// collection but weak ref read during marking.
200
private void checkShouldNotClear() throws Exception {
201
System.out.println("running checkShouldNotClear");
202
try {
203
int value = 20;
204
try {
205
remember(value);
206
checkValue(value);
207
gcUntilOld(testObject);
208
// Block concurrent cycle until we're ready.
209
WB.concurrentGCAcquireControl();
210
checkValue(value);
211
testObject = null; // Discard strong ref
212
// Run through most of marking
213
WB.concurrentGCRunTo(WB.BEFORE_MARKING_COMPLETED);
214
// Fetch weak ref'ed object. Should be kept alive now.
215
Object recovered = getObject();
216
if (recovered == null) {
217
throw new RuntimeException("unexpected clear during mark");
218
}
219
// Finish collection, including reference processing.
220
// Block any further cycles while we finish check.
221
WB.concurrentGCRunToIdle();
222
// Fetch weak ref'ed object. Referent is manifestly
223
// live in recovered; the earlier fetch should have
224
// kept it alive through collection, so weak ref
225
// should not have been cleared.
226
if (getObject() == null) {
227
// 8166188 problem results in not doing the
228
// keep-alive of earlier getObject result, so
229
// recovered is now reachable but not marked.
230
// We may eventually crash.
231
throw new RuntimeException("cleared jweak for live object");
232
}
233
Reference.reachabilityFence(recovered);
234
} finally {
235
forget();
236
}
237
} finally {
238
WB.concurrentGCReleaseControl();
239
}
240
}
241
242
private void check() throws Exception {
243
checkSanity();
244
checkSurvival();
245
checkClear();
246
checkShouldNotClear();
247
System.out.println("Check passed");
248
}
249
250
public static void main(String[] args) throws Exception {
251
if (!WB.supportsConcurrentGCBreakpoints()) {
252
throw new SkippedException(
253
GC.selected().name() + " doesn't support concurrent GC breakpoints");
254
}
255
256
// Perform check with direct jweak resolution.
257
System.out.println("Check with jweak resolved");
258
new TestJNIWeak(true).check();
259
260
// Perform check with implicit jweak resolution by native
261
// call's return value handling.
262
System.out.println("Check with jweak returned");
263
new TestJNIWeak(false).check();
264
}
265
}
266
267