Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/stress/except/except006.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/except006.
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.except006
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 except006 {
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
* Storage for a lot of tiny objects
111
* "static volatile" keywords are for preventing heap optimization
112
*/
113
private static volatile Object pool[] = null;
114
/**
115
* Temporary <code>log</code> for error messages, warnings and/or execution trace.
116
*
117
* @see #messages
118
*/
119
private static String log[] = new String[1000]; // up to 1000 messages
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("# While printing this message, JVM seems to initiate the output");
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
// Allocate repository for a lots of tiny objects:
183
for (int size = 1 << 30; size > 0 && pool == null; size >>= 1)
184
try {
185
pool = new Object[size];
186
} catch (OutOfMemoryError oome) {
187
}
188
if (pool == null)
189
throw new Error("HS bug: cannot allocate new Object[1]");
190
int poolSize = pool.length;
191
int index = 0;
192
// Sum up exit code:
193
int exitCode = 0; // apparently PASSED
194
int skipped = 0; // some checks may correctly suffer OutOfMemoryError
195
Object junkIt;
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
totalDelay += seconds;
214
if (totalDelay > 60) {
215
if (TRACE_ON)
216
out.println(
217
"Memory allocation became slow; so, heap seems exhausted.");
218
break;
219
}
220
}
221
}
222
} catch (OutOfMemoryError oome) {
223
if (TRACE_ON)
224
log[messages++] = "Heap seems exhausted - OutOfMemoryError thrown.";
225
}
226
if (index > poolSize - 1000) {
227
if (WARN_ON)
228
log[messages++] = "Warning: pool[] is full; so, checks would not be enough hard...";
229
}
230
231
232
// Check InstantiationException (negative):
233
try {
234
junkIt = Abra_Cadabra.class.newInstance(); // illegal - should fail
235
// Object junkIt = Abra.Cadabra.class.newInstance(); // legal - should pass
236
log[messages++] = "Failure: InstantiationException (negative)";
237
exitCode = 2;
238
} catch (IllegalAccessException iae) {
239
log[messages++] =
240
"Failure: InstantiationException (negative) incorrectly thrown IllegalAccessException";
241
exitCode = 2;
242
} catch (InstantiationException ie) {
243
if (TRACE_ON)
244
log[messages++] = "Success: InstantiationException (negative)";
245
} catch (OutOfMemoryError oome) {
246
if (WARN_ON)
247
log[messages++] = "Skipped: InstantiationException (negative)";
248
skipped++;
249
}
250
251
return exitCode;
252
}
253
254
/**
255
* Re-call to <code>run(args,out)</code>, and return JCK-like exit status.
256
* (The stream <code>out</code> is assigned to <code>System.out</code> here.)
257
*
258
* @see #run(String[], PrintStream)
259
*/
260
public static void main(String args[]) {
261
Thread.currentThread().setUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
262
// Last try. If there is some exception outside the code, test should end correctly
263
@Override
264
public void uncaughtException(Thread t, Throwable e) {
265
try {
266
pool = null;
267
log = null;
268
System.gc();
269
if (e instanceof OutOfMemoryError) {
270
try {
271
System.out.println("OOME : Test Skipped");
272
System.exit(0);
273
} catch (Throwable ignore) {
274
} // No code in the handler can provoke correct exceptions.
275
} else {
276
e.printStackTrace();
277
throw (RuntimeException) e;
278
}
279
} catch (OutOfMemoryError oome) {
280
}
281
}
282
});
283
int exitCode = run(args, System.out);
284
System.exit(exitCode + 95);
285
// JCK-like exit status.
286
}
287
288
/**
289
* This class should be used to check <code>CloneNotSupportedException</code>,
290
* <code>IllegalAccessException</code>, and <code>IllegalArgumentException</code>.
291
* The class extends <code>except006</code> in order that its (protected)
292
* method <code>clone()</code> be available from <code>except006</code>.
293
*/
294
private static class Abra {
295
/**
296
* Will correctly instantiate <code>Abra.Cadabra</code> object.
297
*/
298
public static class Cadabra {
299
}
300
301
/**
302
* Will try to correctly instantiate <code>Abra.Cadabra</code>,
303
* not <code>Abra</code>.
304
*/
305
private Abra() {
306
}
307
308
}
309
310
/**
311
* Will try to incorrectly instantiate and object of this class.
312
*/
313
private interface Abra_Cadabra {
314
}
315
316
}
317
318