jdk

Форк
0
/
Stack014.java 
143 строки · 4.9 Кб
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/stack014.
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 synchronized virtual recursive method
34
 *     for the given fixed depth of recursion (though, for a large
35
 *     depth). Note however, that different threads are not actual
36
 *     synchronized, because different instances having the recursive
37
 *     method are used.
38
 *     This test measures a number of recursive invocations until
39
 *     stack overflow, and then tries to provoke similar stack overflows
40
 *     10 times in each of 10 threads. Each provocation consists of
41
 *     invoking that recursive method for the given fixed depth
42
 *     of invocations which is 100 times that depth measured before.
43
 *     The test is deemed passed, if VM have not crashed, and
44
 *     if exception other than due to stack overflow was not
45
 *     thrown.
46
 * COMMENTS
47
 *     This test crashes HS versions 2.0, 1.3, and 1.4 on Solaris.
48
 *     However, it passes against all these HS versions on Win32.
49
 *     See the bug:
50
 *     4366625 (P4/S4) multiple stack overflow causes HS crash
51
 *
52
 * @requires vm.opt.DeoptimizeALot != true
53
 * @run main/othervm/timeout=900 Stack014
54
 */
55

56
public class Stack014 extends Stack014i {
57
    final static int THREADS = 10;
58
    final static int CYCLES = 10;
59

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

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

106
    synchronized void recurse(int depth) {
107
        if (depth > 0) {
108
            recurse(depth - 1);
109
        }
110
    }
111
}
112

113
abstract class Stack014i extends Thread {
114
    //
115
    // Pure virtual method:
116
    //
117
    abstract void recurse(int depth);
118

119
    Throwable thrown = null;
120
    int depthToTry;
121
    int cycles;
122

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

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

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

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

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

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