jdk

Форк
0
/
Stack013.java 
140 строк · 4.7 Кб
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/stack013.
29
 * VM testbase keywords: [stress, stack, nonconcurrent]
30
 * VM testbase readme:
31
 * DESCRIPTION
32
 *     This test provokes multiple stack overflows in the multiple
33
 *     threads -- by invoking virtual recursive method for the given
34
 *     fixed depth of recursion (though, for a large depth).
35
 *     This test measures a number of recursive invocations until
36
 *     stack overflow, and then tries to provoke similar stack overflows
37
 *     10 times in each of 10 threads. Each provocation consists of
38
 *     invoking that recursive method for the given fixed depth
39
 *     of invocations which is 100 times that depth measured before.
40
 *     The test is deemed passed, if VM have not crashed, and
41
 *     if exception other than due to stack overflow was not
42
 *     thrown.
43
 * COMMENTS
44
 *     This test crashes HS versions 2.0, 1.3, and 1.4 on both Win32
45
 *     and Solaris platforms.
46
 *     See the bug:
47
 *     4366625 (P4/S4) multiple stack overflow causes HS crash
48
 *
49
 * @requires vm.opt.DeoptimizeALot != true
50
 * @run main/othervm/timeout=900 Stack013
51
 */
52

53
public class Stack013 extends Stack013i {
54
    final static int THREADS = 10;
55
    final static int CYCLES = 10;
56

57
    public static void main(String[] args) {
58
        Stack013i test = new Stack013();
59
        //
60
        // Measure maximal recursion depth until stack overflow:
61
        //
62
        int maxDepth = 0;
63
        for (int depth = 10; ; depth += 10) {
64
            try {
65
                test.recurse(depth);
66
                maxDepth = depth;
67
            } catch (StackOverflowError | OutOfMemoryError err) {
68
                break;
69
            }
70
        }
71
        System.out.println("Max. depth: " + maxDepth);
72

73
        //
74
        // Execute multiple threads repeatedly provoking stack overflows:
75
        //
76
        Stack013i threads[] = new Stack013i[THREADS];
77
        for (int i = 0; i < threads.length; i++) {
78
            threads[i] = new Stack013();
79
            threads[i].depthToTry = 100 * maxDepth;
80
            threads[i].cycles = CYCLES;
81
            threads[i].start();
82
        }
83
        for (int i = 0; i < threads.length; i++) {
84
            if (threads[i].isAlive()) {
85
                try {
86
                    threads[i].join();
87
                } catch (InterruptedException exception) {
88
                    throw new RuntimeException(exception);
89
                }
90
            }
91
        }
92
        //
93
        // Check if unexpected exceptions were thrown:
94
        //
95
        for (int i = 0; i < threads.length; i++) {
96
            if (threads[i].thrown != null) {
97
                threads[i].thrown.printStackTrace();
98
                throw new RuntimeException("Exception in the thread " + threads[i], threads[i].thrown);
99
            }
100
        }
101
    }
102

103
    void recurse(int depth) {
104
        if (depth > 0) {
105
            recurse(depth - 1);
106
        }
107
    }
108
}
109

110
abstract class Stack013i extends Thread {
111
    //
112
    // Pure virtual method:
113
    //
114
    abstract void recurse(int depth);
115

116
    Throwable thrown = null;
117
    int depthToTry;
118
    int cycles;
119

120
    public void run() {
121
        //
122
        // Provoke multiple stack overflows:
123
        //
124
        for (int i = 0; i < cycles; i++) {
125
            try {
126
                recurse(depthToTry);
127
                throw new Exception(
128
                        "TEST_RFE: no stack overflow thrown" +
129
                                ", need to try deeper recursion?");
130

131
            } catch (StackOverflowError | OutOfMemoryError err) {
132
                // It's OK
133
            } catch (Throwable throwable) {
134
                // It isn't OK!
135
                thrown = throwable;
136
                break;
137
            }
138
        }
139
    }
140
}
141

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

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

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

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