Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/stress/except/except003.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
* @test
27
* @key stress
28
*
29
* @summary converted from VM testbase nsk/stress/except/except003.
30
* VM testbase keywords: [stress, diehard, 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.except003
62
*/
63
64
package nsk.stress.except;
65
66
import java.io.PrintStream;
67
68
/**
69
* This checks if various exceptions are thrown (and caught) correctly
70
* when there apparently are no free space in the heap to allocate new
71
* <code>Throwable</code> instance.
72
* <p>
73
* <p>The test tries to occupy all of memory available in the heap by
74
* allocating lots of new <code>Object()</code> instances. Instances of the
75
* type <code>Object</code> are the smallest objects, so they apparently should
76
* occupy most fine-grained fragments in the heap and leave no free space for
77
* new <code>Throwable</code> instance. After that, the test provokes various
78
* exceptions (e.g.: by executing integer division by 0 and so on), and checks
79
* if appropriate exceptions are thrown.
80
* <p>
81
* <p>Note, that memory occupation is terminated if memory allocation slows
82
* down crucially. This is a workaround intended to avoid the HotSpot bug:
83
* <br>&nbsp;&nbsp;
84
* #4248801 (P1/S5) slow memory allocation when heap is almost exhausted
85
* <p>
86
* <p>There is also a workaround involved to avoid the following bugs known
87
* for HotSpot and for classic VM:
88
* <br>&nbsp;&nbsp;
89
* #4239841 (P1/S5) 1.1: poor garbage collector performance
90
* <br>&nbsp;&nbsp;
91
* #4245060 (P4/S5) poor garbage collector performance
92
* <br>However, printing of the test's error messages, warnings, and of
93
* execution trace may fail even so. If the test fails due to poor GC
94
* performance, exit status 96 is returned instead of 97.
95
* <p>
96
* <p>Also note, that the test needs a lot of memory to start up, so it should
97
* not run under older JDK 1.1.x release due to its poor heap utilization.
98
*/
99
public class except003 {
100
/**
101
* Either allow or supress printing of execution trace.
102
*/
103
private static boolean TRACE_ON = false;
104
/**
105
* Either allow or supress printing of warning messages.
106
*/
107
private static final boolean WARN_ON = true;
108
/*
109
* Storage for a lot of tiny objects
110
* "static volatile" keywords are for preventing heap optimization
111
*/
112
private static volatile Object pool[] = null;
113
/**
114
* Temporary <code>log</code> for error messages, warnings and/or execution trace.
115
*
116
* @see #messages
117
*/
118
private static String log[] = new String[1000]; // up to 1000 messages
119
/**
120
* How many <code>messages</code> were submitted to the <code>log</code>.
121
*
122
* @see #log
123
*/
124
private static int messages = 0;
125
126
/**
127
* Re-call to the method <code>run(out)</code> (ignore <code>args[]</code>),
128
* and print the test summary - either test passed of failed.
129
*/
130
public static int run(String args[], PrintStream out) {
131
if (args.length > 0) {
132
if (args[0].toLowerCase().startsWith("-v"))
133
TRACE_ON = true;
134
}
135
136
int exitCode = run(out);
137
pool = null;
138
System.gc();
139
// Print the log[] and the test summary:
140
try {
141
for (int i = 0; i < messages; i++)
142
out.println(log[i]);
143
if (exitCode == 0) {
144
if (TRACE_ON)
145
out.println("Test passed.");
146
} else
147
out.println("Test failed.");
148
} catch (OutOfMemoryError oome) {
149
// Poor performance of garbage collector:
150
exitCode = 1;
151
}
152
153
return exitCode;
154
}
155
156
/**
157
* Allocate as much <code>Object</code> instances as possible to bring JVM
158
* into stress, and then check if exceptions are correctly thrown accordingly
159
* to various situations like integer division by 0, etc.
160
*/
161
private static int run(PrintStream out) {
162
out.println("# While printing this message, JVM seems to initiate the output");
163
out.println("# stream, so that it will not need more memory to print later,");
164
out.println("# when the heap would fail to provide more memory.");
165
out.println("# ");
166
out.println("# Note, that the test maintains especial static log[] field in");
167
out.println("# order to avoid printing when the heap seems exhausted.");
168
out.println("# Nevertheless, printing could arise OutOfMemoryError even");
169
out.println("# after all the memory allocated by the test is released.");
170
out.println("# ");
171
out.println("# That problem is caused by the known JDK/HotSpot bugs:");
172
out.println("# 4239841 (P1/S5) 1.1: poor garbage collector performance");
173
out.println("# 4245060 (P4/S5) poor garbage collector performance");
174
out.println("# ");
175
out.println("# This message is just intended to work-around that problem.");
176
out.println("# If printing should fail even so, the test will return the");
177
out.println("# exit status 96 instead of 97 to indicate the problem.");
178
179
// Prepare some items, which will be used by the test:
180
Object trash = null;
181
182
// Allocate repository for a lots of tiny objects:
183
pool = null;
184
// Sum up exit code:
185
int exitCode = 0; // apparently PASSED
186
int skipped = 0; // some checks may correctly suffer OutOfMemoryError
187
for (int size = 1 << 30; size > 0 && pool == null; size >>= 1)
188
try {
189
pool = new Object[size];
190
} catch (OutOfMemoryError oome) {
191
}
192
if (pool == null)
193
throw new Error("HS bug: cannot allocate new Object[1]");
194
int poolSize = pool.length;
195
int index = 0;
196
197
// Sum up time spent, when it was hard to JVM to allocate next object
198
// (i.e.: when JVM has spent more than 1 second to allocate new object):
199
double totalDelay = 0;
200
long timeMark = System.currentTimeMillis();
201
try {
202
for (; index < poolSize; index++) {
203
//-------------------------
204
pool[index] = new Object();
205
long nextTimeMark = System.currentTimeMillis();
206
long elapsed = nextTimeMark - timeMark;
207
timeMark = nextTimeMark;
208
//----------------------
209
if (elapsed > 1000) {
210
double seconds = elapsed / 1000.0;
211
if (TRACE_ON)
212
out.println(
213
"pool[" + index + "]=new Object(); // elapsed " + seconds + "s");
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
// Do not release any byte once allocated:
228
pool[index++] = oome;
229
}
230
231
if (index > poolSize - 1000) {
232
if (WARN_ON)
233
log[messages++] = "Warning: pool[] is full; so, checks would not be enough hard...";
234
}
235
236
// Check ClassNotFoundException (negative):
237
try {
238
// trash = Class.forName("nsk.stress.except.except003$Abra$Cadabra"); // correct - should pass
239
trash = Class.forName("nsk.stress.except.except003.Abra.Cadabra"); // incorrect - should fail
240
log[messages++] = "Failure: ClassNotFoundException (negative)";
241
exitCode = 2;
242
} catch (ClassNotFoundException cnfe) {
243
if (TRACE_ON)
244
log[messages++] = "Success: ClassNotFoundException (negative)";
245
} catch (OutOfMemoryError oome) {
246
if (WARN_ON)
247
log[messages++] = "Skipped: ClassNotFoundException (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(95);
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>ClassNotFoundException</code>
290
* and <code>IllegalAccessException</code>.
291
*/
292
private static class Abra {
293
/**
294
* Will try to incorrectly find this class as <code>Cadabra</code>
295
* instead of <code>Abra$Cadabra</code>.
296
*/
297
public static class Cadabra {
298
}
299
300
}
301
302
}
303
304