2
* Copyright (c) 2000, 2024, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
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).
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.
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
28
* @summary converted from VM testbase nsk/stress/stack/stack008.
29
* VM testbase keywords: [stress, stack, nonconcurrent]
32
* This test provokes multiple stack overflows in the same thread
33
* by invocations via reflection. Recursive method is invoked for
34
* the given fixed depth of recursion (though, for a large depth).
35
* This test makes measures a number of recursive invocations
36
* before 1st StackOverflowError, and then tries to reproduce
37
* such StackOverflowError 100 times -- each time by trying to
38
* invoke the same recursive method for the given fixed depth
39
* of invocations (which is 200 times that depth just measured).
40
* The test is deemed passed, if VM have not crashed.
42
* This test crashes all HS versions (2.0, 1.3, 1.4) on Solaris,
43
* and crashes HS 2.0 on win32. However, it passes against HS 1.3
46
* 4366625 (P4/S4) multiple stack overflow causes HS crash
47
* The stack size is too small to run on systems with > 4K page size.
48
* Making it bigger could cause timeouts on other platform.
50
* @requires (vm.opt.DeoptimizeALot != true & vm.compMode != "Xcomp" & vm.pageSize == 4096)
51
* @run main/othervm/timeout=900 -Xss200K Stack008
54
import java.lang.reflect.InvocationTargetException;
55
import java.lang.reflect.Method;
57
public class Stack008 {
58
public static void main(String[] args) {
61
// Measure maximal recursion depth until stack overflow:
63
for (depth = 100; ; depth += 100) {
66
} catch (Throwable exception) {
67
Throwable target = getTargetException(exception);
68
if ((target instanceof StackOverflowError) ||
69
(target instanceof OutOfMemoryError))
71
target.printStackTrace();
72
throw new RuntimeException(exception);
75
System.out.println("Max. depth: " + depth);
77
// Provoke stack overflow multiple times:
79
for (int i = 0; i < 100; i++) {
81
invokeRecurse(200 * depth);
82
// System.out.println("?");
83
} catch (Throwable exception) {
84
Throwable target = getTargetException(exception);
85
if ((target instanceof StackOverflowError) ||
86
(target instanceof OutOfMemoryError))
88
target.printStackTrace();
89
throw new RuntimeException(exception);
94
private static Throwable getTargetException(Throwable exception) {
97
// Unwrap deep chain of exceptions:
101
target instanceof InvocationTargetException;
102
target = ((InvocationTargetException) target).getTargetException()
108
static Method method = null;
109
static Stack008 instance = null;
110
static Object params[] = null;
112
private static void invokeRecurse(int depth) throws Exception {
113
if (method == null) {
115
// Optimization trick: allocate once, use everywhere.
117
instance = new Stack008();
118
method = Stack008.class.getMethod("recurse");
119
params = new Object[]{};
122
// Note, that the same instance.depth is used in all invocations:
124
instance.depth = depth;
125
method.invoke(instance, params);
130
public void recurse() throws Exception {
133
// Self-invoke via reflection:
135
invokeRecurse(depth - 1);