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