Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/stress/except/except008.java
41155 views
1
/*
2
* Copyright (c) 1999, 2018, 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
26
/*
27
* @test
28
* @key stress
29
*
30
* @summary converted from VM testbase nsk/stress/except/except008.
31
* VM testbase keywords: [stress, diehard, slow, nonconcurrent, quick]
32
* VM testbase readme:
33
* DESCRIPTION
34
* This checks if various exceptions are thrown (and caught) correctly
35
* when there apparently are no free space in the heap to allocate new
36
* Throwable instance.
37
* The test tries to occupy all of memory available in the heap by allocating
38
* lots of new Object() instances. Instances of the type Object are the smallest
39
* objects, so they apparently should occupy most fine-grained fragments in the
40
* heap and leave no free space for new Throwable instance. After that, the test
41
* provokes various exceptions (e.g.: by executing integer division by 0 and so
42
* on), and checks if appropriate exceptions are thrown.
43
* COMMENTS
44
* The test needs a lot of memory to start up, so it should not run under older
45
* JDK 1.1.x release due to its poorer heap utilization. Also, some checks are
46
* skipped when testing classic VM, because OutOfMemoryError is correctly thrown
47
* instead of target exception.
48
* When the test is being self-initiating (i.e.: eating heap), memory occupation
49
* is terminated if memory allocation slows down crucially. This is a workaround
50
* intended to avoid the HotSpot bug:
51
* #4248801 (P1/S5) slow memory allocation when heap is almost exhausted
52
* There is also a workaround involved to avoid the following bugs known
53
* for HotSpot and for classic VM:
54
* #4239841 (P1/S5) 1.1: poor garbage collector performance (HotSpot bug)
55
* #4245060 (P4/S5) poor garbage collector performance (Classic VM bug)
56
* However, printing of the test's error messages, warnings, and of execution
57
* trace fails under JDK 1.2 for Win32 even so. If the test fails due to this
58
* problem, exit status 96 is returned instead of 97.
59
* JDK 1.3 classic VM for Sparc may crash (core dump) due to the known bug:
60
* #4245057 (P2/S3) VM crashes when heap is exhausted
61
*
62
* @run main/othervm -Xms50M -Xmx200M -XX:-UseGCOverheadLimit nsk.stress.except.except008
63
*/
64
65
package nsk.stress.except;
66
67
import java.io.PrintStream;
68
import java.lang.reflect.Field;
69
import java.lang.reflect.Method;
70
71
/**
72
* This checks if various exceptions are thrown (and caught) correctly
73
* when there apparently are no free space in the heap to allocate new
74
* <code>Throwable</code> instance.
75
* <p>
76
* <p>The test tries to occupy all of memory available in the heap by
77
* allocating lots of new <code>Object()</code> instances. Instances of the
78
* type <code>Object</code> are the smallest objects, so they apparently should
79
* occupy most fine-grained fragments in the heap and leave no free space for
80
* new <code>Throwable</code> instance. After that, the test provokes various
81
* exceptions (e.g.: by executing integer division by 0 and so on), and checks
82
* if appropriate exceptions are thrown.
83
* <p>
84
* <p>Note, that memory occupation is terminated if memory allocation slows
85
* down crucially. This is a workaround intended to avoid the HotSpot bug:
86
* <br>&nbsp;&nbsp;
87
* #4248801 (P1/S5) slow memory allocation when heap is almost exhausted
88
* <p>
89
* <p>There is also a workaround involved to avoid the following bugs known
90
* for HotSpot and for classic VM:
91
* <br>&nbsp;&nbsp;
92
* #4239841 (P1/S5) 1.1: poor garbage collector performance
93
* <br>&nbsp;&nbsp;
94
* #4245060 (P4/S5) poor garbage collector performance
95
* <br>However, printing of the test's error messages, warnings, and of
96
* execution trace may fail even so. If the test fails due to poor GC
97
* performance, exit status 96 is returned instead of 97.
98
* <p>
99
* <p>Also note, that the test needs a lot of memory to start up, so it should
100
* not run under older JDK 1.1.x release due to its poor heap utilization.
101
*/
102
public class except008 {
103
/**
104
* Either allow or supress printing of execution trace.
105
*/
106
private static boolean TRACE_ON = false;
107
/**
108
* Either allow or supress printing of warning messages.
109
*/
110
private static final boolean WARN_ON = true;
111
/*
112
* Storage for a lot of tiny objects
113
* "static volatile" keywords are for preventing heap optimization
114
*/
115
private static volatile Object pool[] = null;
116
/**
117
* Temporary <code>log</code> for error messages, warnings and/or execution trace.
118
*
119
* @see #messages
120
*/
121
private static String log[] = new String[1000]; // up to 1000 messages
122
/**
123
* How many <code>messages</code> were submitted to the <code>log</code>.
124
*
125
* @see #log
126
*/
127
private static int messages = 0;
128
129
/**
130
* Re-call to the method <code>run(out)</code> (ignore <code>args[]</code>),
131
* and print the test summary - either test passed of failed.
132
*/
133
public static int run(String args[], PrintStream out) {
134
if (args.length > 0) {
135
if (args[0].toLowerCase().startsWith("-v"))
136
TRACE_ON = true;
137
}
138
139
int exitCode = run(out);
140
pool = null;
141
System.gc();
142
// Print the log[] and the test summary:
143
try {
144
for (int i = 0; i < messages; i++)
145
out.println(log[i]);
146
if (exitCode == 0) {
147
if (TRACE_ON)
148
out.println("Test passed.");
149
} else
150
out.println("Test failed.");
151
} catch (OutOfMemoryError oome) {
152
// Poor performance of garbage collector:
153
exitCode = 1;
154
}
155
156
return exitCode;
157
}
158
159
/**
160
* Allocate as much <code>Object</code> instances as possible to bring JVM
161
* into stress, and then check if exceptions are correctly thrown accordingly
162
* to various situations like integer division by 0, etc.
163
*/
164
private static int run(PrintStream out) {
165
out.println("# While printing this message, JVM seems to initiate the output");
166
out.println("# stream, so that it will not need more memory to print later,");
167
out.println("# when the heap would fail to provide more memory.");
168
out.println("# ");
169
out.println("# Note, that the test maintains especial static log[] field in");
170
out.println("# order to avoid printing when the heap seems exhausted.");
171
out.println("# Nevertheless, printing could arise OutOfMemoryError even");
172
out.println("# after all the memory allocated by the test is released.");
173
out.println("# ");
174
out.println("# That problem is caused by the known JDK/HotSpot bugs:");
175
out.println("# 4239841 (P1/S5) 1.1: poor garbage collector performance");
176
out.println("# 4245060 (P4/S5) poor garbage collector performance");
177
out.println("# ");
178
out.println("# This message is just intended to work-around that problem.");
179
out.println("# If printing should fail even so, the test will try to return");
180
out.println("# the exit status 96 instead of 97 to indicate the problem.");
181
out.println("# However, the test may fail or even crash on some platforms");
182
out.println("# suffering the bug 4239841 or 4245060.");
183
184
// Prepare some items, which will be used by the test:
185
Zoo zoo = new Zoo(); // load the class Zoo
186
Class noArgs[] = new Class[0];
187
188
// Sum up exit code:
189
int exitCode = 0; // apparently PASSED
190
int skipped = 0; // some checks may correctly suffer OutOfMemoryError
191
192
// Allocate repository for a lots of tiny objects:
193
for (int size = 1 << 30; size > 0 && pool == null; size >>= 1)
194
try {
195
pool = new Object[size];
196
} catch (OutOfMemoryError oome) {
197
}
198
if (pool == null)
199
throw new Error("HS bug: cannot allocate new Object[1]");
200
int poolSize = pool.length;
201
202
int index = 0;
203
pool[index++] = new Object();
204
205
// Sum up time spent, when it was hard to JVM to allocate next object
206
// (i.e.: when JVM has spent more than 1 second to allocate new object):
207
double totalDelay = 0;
208
long timeMark = System.currentTimeMillis();
209
try {
210
for (; index < poolSize; index++) {
211
//-------------------------
212
pool[index] = new Object();
213
long nextTimeMark = System.currentTimeMillis();
214
long elapsed = nextTimeMark - timeMark;
215
timeMark = nextTimeMark;
216
//----------------------
217
if (elapsed > 1000) {
218
double seconds = elapsed / 1000.0;
219
if (TRACE_ON)
220
out.println(
221
"pool[" + index + "]=new Object(); // elapsed " + seconds + "s");
222
totalDelay += seconds;
223
if (totalDelay > 60) {
224
if (TRACE_ON)
225
out.println(
226
"Memory allocation became slow; so, heap seems exhausted.");
227
break;
228
}
229
}
230
}
231
} catch (OutOfMemoryError oome) {
232
if (TRACE_ON)
233
log[messages++] = "Heap seems exhausted - OutOfMemoryError thrown.";
234
}
235
236
if (index > poolSize - 1000) {
237
if (WARN_ON)
238
log[messages++] = "Warning: pool[] is full; so, checks would not be enough hard...";
239
}
240
241
// Check NoSuchFieldException (positive):
242
try {
243
Field valid = Zoo.class.getField("PUBLIC_FIELD"); // should pass
244
// Field wrong = Zoo.class.getField("PRIVATE_FIELD"); // should fail
245
if (TRACE_ON)
246
log[messages++] = "Success: NoSuchFieldException not thrown as expected";
247
} catch (NoSuchFieldException nsfe) {
248
pool[index++] = nsfe;
249
log[messages++] = "Failure: NoSuchFieldException thrown unexpectedly";
250
exitCode = 2;
251
} catch (OutOfMemoryError oome) {
252
if (WARN_ON)
253
log[messages++] =
254
"Skipped: NoSuchFieldException positive check - OutOfMemoryError thrown";
255
skipped++;
256
}
257
258
// Check NoSuchFieldException (negative):
259
try {
260
// Field valid = Zoo.class.getField("PUBLIC_FIELD"); // should pass
261
Field wrong = Zoo.class.getField("PRIVATE_FIELD"); // should fail
262
log[messages++] = "Failure: NoSuchFieldException incorrectly not thrown";
263
exitCode = 2;
264
} catch (NoSuchFieldException nsfe) {
265
if (TRACE_ON)
266
log[messages++] = "Success: NoSuchFieldException thrown as expected";
267
} catch (OutOfMemoryError oome) {
268
if (WARN_ON)
269
log[messages++] =
270
"NoSuchFieldException negative check - OutOfMemoryError thrown";
271
skipped++;
272
}
273
274
// Check NoSuchMethodException (positive):
275
try {
276
Method valid = Zoo.class.getMethod("PUBLIC_METHOD", noArgs); // should pass
277
// Method wrong = Zoo.class.getMethod("PRIVATE_METHOD",noArgs); // should fail
278
if (TRACE_ON)
279
log[messages++] = "Success: NoSuchFieldException not thrown as expected";
280
} catch (NoSuchMethodException nsme) {
281
pool[index++] = nsme;
282
log[messages++] = "Failure: NoSuchMethodException thrown unexpectedly";
283
exitCode = 2;
284
} catch (OutOfMemoryError oome) {
285
if (WARN_ON)
286
log[messages++] =
287
"Skipped: NoSuchMethodException positive check - OutOfMemoryError thrown";
288
pool[index++] = oome;
289
skipped++;
290
}
291
292
// Check NoSuchMethodException (negative):
293
try {
294
// Method valid = Zoo.class.getMethod("PUBLIC_METHOD",noArgs); // should pass
295
Method wrong = Zoo.class.getMethod("PRIVATE_METHOD", noArgs); // should fail
296
log[messages++] = "Failure: NoSuchMethodException incorrectly not thrown";
297
exitCode = 2;
298
} catch (NoSuchMethodException nsme) {
299
pool[index++] = nsme;
300
if (TRACE_ON)
301
log[messages++] = "Success: NoSuchFieldException thrown as expected";
302
} catch (OutOfMemoryError oome) {
303
if (WARN_ON)
304
log[messages++] =
305
"Skipped: NoSuchMethodException negative check - OutOfMemoryError thrown";
306
skipped++;
307
}
308
309
return exitCode;
310
}
311
312
/**
313
* Several items used to check reflections.
314
*/
315
private static class Zoo {
316
public String PUBLIC_FIELD = "Accessible via reflection";
317
private String PRIVATE_FIELD = "Inaccessible via reflection";
318
319
public String PUBLIC_METHOD() {
320
return "Accessible via reflection";
321
}
322
323
private String PRIVATE_METHOD() {
324
return "Inaccessible via reflection";
325
}
326
327
}
328
329
/**
330
* Re-call to <code>run(args,out)</code>, and return JCK-like exit status.
331
* (The stream <code>out</code> is assigned to <code>System.out</code> here.)
332
*
333
* @see #run(String[], PrintStream)
334
*/
335
public static void main(String args[]) {
336
Thread.currentThread().setUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
337
// Last try. If there is some exception outside the code, test should end correctly
338
@Override
339
public void uncaughtException(Thread t, Throwable e) {
340
try {
341
pool = null;
342
log = null;
343
System.gc();
344
if (e instanceof OutOfMemoryError) {
345
try {
346
System.out.println("OOME : Test Skipped");
347
System.exit(0);
348
} catch (Throwable ignore) {
349
} // No code in the handler can provoke correct exceptions.
350
} else {
351
e.printStackTrace();
352
throw (RuntimeException) e;
353
}
354
} catch (OutOfMemoryError oome) {
355
}
356
}
357
});
358
int exitCode = run(args, System.out);
359
System.exit(exitCode + 95);
360
// JCK-like exit status.
361
}
362
363
}
364
365