jdk

Форк
0
/
Stack008.java 
138 строк · 5.0 Кб
1
/*
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.
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
 * @test
26
 * @key stress
27
 *
28
 * @summary converted from VM testbase nsk/stress/stack/stack008.
29
 * VM testbase keywords: [stress, stack, nonconcurrent]
30
 * VM testbase readme:
31
 * DESCRIPTION
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.
41
 * COMMENTS
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
44
 *     and 1.4 on Win32.
45
 *     See the bug:
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.
49
 *
50
 * @requires (vm.opt.DeoptimizeALot != true & vm.compMode != "Xcomp" & vm.pageSize == 4096)
51
 * @run main/othervm/timeout=900 -Xss200K Stack008
52
 */
53

54
import java.lang.reflect.InvocationTargetException;
55
import java.lang.reflect.Method;
56

57
public class Stack008 {
58
    public static void main(String[] args) {
59
        int depth;
60
        //
61
        // Measure maximal recursion depth until stack overflow:
62
        //
63
        for (depth = 100; ; depth += 100) {
64
            try {
65
                invokeRecurse(depth);
66
            } catch (Throwable exception) {
67
                Throwable target = getTargetException(exception);
68
                if ((target instanceof StackOverflowError) ||
69
                        (target instanceof OutOfMemoryError))
70
                    break; // OK.
71
                target.printStackTrace();
72
                throw new RuntimeException(exception);
73
            }
74
        }
75
        System.out.println("Max. depth: " + depth);
76
        //
77
        // Provoke stack overflow multiple times:
78
        //
79
        for (int i = 0; i < 100; i++) {
80
            try {
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))
87
                    continue; // OK.
88
                target.printStackTrace();
89
                throw new RuntimeException(exception);
90
            }
91
        }
92
    }
93

94
    private static Throwable getTargetException(Throwable exception) {
95
        Throwable target;
96
        //
97
        // Unwrap deep chain of exceptions:
98
        //
99
        for (
100
                target = exception;
101
                target instanceof InvocationTargetException;
102
                target = ((InvocationTargetException) target).getTargetException()
103
                )
104
            ;
105
        return target;
106
    }
107

108
    static Method method = null;
109
    static Stack008 instance = null;
110
    static Object params[] = null;
111

112
    private static void invokeRecurse(int depth) throws Exception {
113
        if (method == null) {
114
            //
115
            // Optimization trick: allocate once, use everywhere.
116
            //
117
            instance = new Stack008();
118
            method = Stack008.class.getMethod("recurse");
119
            params = new Object[]{};
120
        }
121
        //
122
        // Note, that the same instance.depth is used in all invocations:
123
        //
124
        instance.depth = depth;
125
        method.invoke(instance, params);
126
    }
127

128
    int depth = 0;
129

130
    public void recurse() throws Exception {
131
        if (depth > 0) {
132
            //
133
            // Self-invoke via reflection:
134
            //
135
            invokeRecurse(depth - 1);
136
        }
137
    }
138
}
139

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.