Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/stress/except/except005.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/except005.
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 nsk.stress.except.except005
63
*/
64
65
package nsk.stress.except;
66
67
import java.io.PrintStream;
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 except005 {
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
* <code>pool</code> to store tiny objects to fill up the Heap
118
*/
119
private static volatile Object pool[] = null;
120
/**
121
* How many <code>messages</code> were submitted to the <code>log</code>.
122
*
123
* @see #log
124
*/
125
private static int messages = 0;
126
127
/**
128
* Re-call to the method <code>run(out)</code> (ignore <code>args[]</code>),
129
* and print the test summary - either test passed of failed.
130
*/
131
public static int run(String args[], PrintStream out) {
132
if (args.length > 0) {
133
if (args[0].toLowerCase().startsWith("-v"))
134
TRACE_ON = true;
135
}
136
137
int exitCode = run(out);
138
pool = null;
139
System.gc();
140
// Print the log[] and the test summary:
141
try {
142
for (int i = 0; i < messages; i++)
143
out.println(log[i]);
144
if (exitCode == 0) {
145
if (TRACE_ON)
146
out.println("Test passed.");
147
} else
148
out.println("Test failed.");
149
} catch (OutOfMemoryError oome) {
150
// Poor performance of garbage collector:
151
exitCode = 1;
152
}
153
154
return exitCode;
155
}
156
157
/**
158
* Allocate as much <code>Object</code> instances as possible to bring JVM
159
* into stress, and then check if exceptions are correctly thrown accordingly
160
* to various situations like integer division by 0, etc.
161
*/
162
private static int run(PrintStream out) {
163
out.println("# Test have been updated!");
164
out.println("# stream, so that it will not need more memory to print later,");
165
out.println("# when the heap would fail to provide more memory.");
166
out.println("# ");
167
out.println("# Note, that the test maintains especial static log[] field in");
168
out.println("# order to avoid printing when the heap seems exhausted.");
169
out.println("# Nevertheless, printing could arise OutOfMemoryError even");
170
out.println("# after all the memory allocated by the test is released.");
171
out.println("# ");
172
out.println("# That problem is caused by the known JDK/HotSpot bugs:");
173
out.println("# 4239841 (P1/S5) 1.1: poor garbage collector performance");
174
out.println("# 4245060 (P4/S5) poor garbage collector performance");
175
out.println("# ");
176
out.println("# This message is just intended to work-around that problem.");
177
out.println("# If printing should fail even so, the test will try to return");
178
out.println("# the exit status 96 instead of 97 to indicate the problem.");
179
out.println("# However, the test may fail or even crash on some platforms");
180
out.println("# suffering the bug 4239841 or 4245060.");
181
182
// Sum up exit code:
183
int exitCode = 0; // apparently PASSED
184
int skipped = 0; // some checks may correctly suffer OutOfMemoryError
185
// Allocate repository for a lots of tiny objects:
186
for (int size = 1 << 30; size > 0 && pool == null; size >>= 1)
187
try {
188
pool = new Object[size];
189
} catch (OutOfMemoryError oome) {
190
}
191
if (pool == null)
192
throw new Error("HS bug: cannot allocate new Object[1]");
193
int poolSize = pool.length;
194
int index = 0;
195
196
// Sum up time spent, when it was hard to JVM to allocate next object
197
// (i.e.: when JVM has spent more than 1 second to allocate new object):
198
double totalDelay = 0;
199
long timeMark = System.currentTimeMillis();
200
try {
201
for (; index < poolSize; index++) {
202
//-------------------------
203
pool[index] = new Object();
204
long nextTimeMark = System.currentTimeMillis();
205
long elapsed = nextTimeMark - timeMark;
206
timeMark = nextTimeMark;
207
//----------------------
208
if (elapsed > 1000) {
209
double seconds = elapsed / 1000.0;
210
if (TRACE_ON) {
211
out.println(
212
"pool[" + index + "]=new Object(); // elapsed " + seconds + "s");
213
}
214
totalDelay += seconds;
215
if (totalDelay > 60) {
216
if (TRACE_ON)
217
out.println(
218
"Memory allocation became slow; so, heap seems exhausted.");
219
break;
220
}
221
}
222
}
223
} catch (OutOfMemoryError oome) {
224
if (TRACE_ON)
225
log[messages++] = "Heap seems exhausted - OutOfMemoryError thrown.";
226
}
227
if (index > poolSize - 1000) {
228
if (WARN_ON)
229
log[messages++] = "Warning: pool[] is full; so, checks would not be enough hard...";
230
}
231
232
// Check InstantiationException (positive):
233
try {
234
// Object junkIt = Abra_Cadabra.class.newInstance(); // illegal - should fail
235
Object junkIt = Abra.Cadabra.class.newInstance(); // legal - should pass
236
if (TRACE_ON)
237
log[messages++] = "Success: InstantiationException (positive)";
238
} catch (IllegalAccessException iae) {
239
log[messages++] =
240
"Failure: InstantiationException (positive) incorrectly thrown IllegalAccessException";
241
pool[index++] = iae;
242
exitCode = 2;
243
} catch (InstantiationException ie) {
244
log[messages++] = "Failure: InstantiationException (positive)";
245
pool[index++] = ie;
246
exitCode = 2;
247
} catch (OutOfMemoryError oome) {
248
if (WARN_ON)
249
log[messages++] = "Skipped: InstantiationException (positive)";
250
pool[index++] = oome;
251
skipped++;
252
}
253
254
return exitCode;
255
}
256
257
/**
258
* Re-call to <code>run(args,out)</code>, and return JCK-like exit status.
259
* (The stream <code>out</code> is assigned to <code>System.out</code> here.)
260
*
261
* @see #run(String[], PrintStream)
262
*/
263
public static void main(String args[]) {
264
Thread.currentThread().setUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
265
// Last try. If there is some OOME, test should end correctly
266
@Override
267
public void uncaughtException(Thread t, Throwable e) {
268
try {
269
pool = null;
270
log = null;
271
System.gc();
272
if (e instanceof OutOfMemoryError) {
273
try {
274
System.out.println("OOME : Test Skipped");
275
System.exit(0);
276
} catch (Throwable ignore) {
277
} // No code in the handler can provoke correct exceptions.
278
} else {
279
e.printStackTrace();
280
throw (RuntimeException) e;
281
}
282
} catch (OutOfMemoryError oome) {
283
}
284
}
285
});
286
int exitCode = run(args, System.out);
287
System.exit(exitCode + 95);
288
// JCK-like exit status.
289
}
290
291
/**
292
* This class should be used to check <code>CloneNotSupportedException</code>,
293
* <code>IllegalAccessException</code>, and <code>IllegalArgumentException</code>.
294
* The class extends <code>except005</code> in order that its (protected)
295
* method <code>clone()</code> be available from <code>except005</code>.
296
*/
297
private static class Abra {
298
/**
299
* Will correctly instantiate <code>Abra.Cadabra</code> object.
300
*/
301
public static class Cadabra {
302
}
303
304
/**
305
* Will try to correctly instantiate <code>Abra.Cadabra</code>,
306
* not <code>Abra</code>.
307
*/
308
private Abra() {
309
}
310
311
}
312
313
/**
314
* Will try to incorrectly instantiate and object of this class.
315
*/
316
private interface Abra_Cadabra {
317
}
318
319
}
320
321