Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/stress/stack/stack015.java
41159 views
/*1* Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @key stress26*27* @summary converted from VM testbase nsk/stress/stack/stack015.28* VM testbase keywords: [stress, stack, nonconcurrent]29* VM testbase readme:30* DESCRIPTION31* This test provokes multiple stack overflows in the multiple32* threads -- by invoking synchronized virtual recursive method33* for the given fixed depth of recursion from within another34* recursive method already deeply invoked.35* This test measures a number of recursive invocations until36* stack overflow, and then tries to provoke similar stack overflows37* in 10 times in each of 10 threads. Each provocation consists of38* invoking that recursive method for the given fixed depth39* of invocations which is 10 times that depth measured before.40* The test is deemed passed, if VM have not crashed, and41* if exception other than due to stack overflow was not42* thrown.43* COMMENTS44* This test crashes HS versions 2.0, 1.3, and 1.4 on Solaris.45* However, it passes against all these HS versions on Win32.46* See the bug:47* 4366625 (P4/S4) multiple stack overflow causes HS crash48*49* @requires vm.opt.DeoptimizeALot != true50* @run main/othervm/timeout=900 nsk.stress.stack.stack01551*/5253package nsk.stress.stack;545556import java.io.PrintStream;5758public class stack015 extends stack015i {59final static int THREADS = 10;60final static int CYCLES = 10;61final static int STEP = 10;62final static int RESERVE = 10;6364public static void main(String[] args) {65int exitCode = run(args, System.out);66System.exit(exitCode + 95);67}6869public static int run(String args[], PrintStream out) {70//71// The test will invoke the particular stack015.recurse()72// method via abstract test.recurse() invocations.73//74stack015i test = new stack015();75stack015i.test = test;7677//78// Measure maximal recursion depth until stack overflow:79//80int maxDepth = 0;81for (int depth = 0; ; depth += STEP)82try {83test.recurse(depth);84maxDepth = depth;85} catch (StackOverflowError soe) {86break;87} catch (OutOfMemoryError oome) {88break;89}90out.println("Max. depth: " + maxDepth);9192//93// Execute multiple threads repeatedly provoking stack overflows:94//95stack015i threads[] = new stack015i[THREADS];96for (int i = 0; i < threads.length; i++) {97threads[i] = new stack015();98threads[i].depthToTry = RESERVE * maxDepth;99threads[i].start();100}101for (int i = 0; i < threads.length; i++)102if (threads[i].isAlive())103try {104threads[i].join();105} catch (InterruptedException exception) {106exception.printStackTrace(out);107return 2;108}109110//111// Check if unexpected exceptions were thrown:112//113int exitCode = 0;114for (int i = 0; i < threads.length; i++)115if (threads[i].thrown != null) {116threads[i].thrown.printStackTrace(out);117exitCode = 2;118}119120if (exitCode != 0)121out.println("# TEST FAILED");122return exitCode;123}124125synchronized void syncRecurse(int depth) {126if (depth > 0)127syncRecurse(depth - 1);128}129}130131abstract class stack015i extends Thread {132//133// Pure virtual method:134//135abstract void syncRecurse(int depth);136137void recurse(int depth) {138//139// Stack overflow must occur here:140//141syncRecurse(stack015.STEP);142//143// If no stack overflow occured, try again with deeper stack:144//145if (depth > 0)146recurse(depth - 1);147}148149Throwable thrown = null;150int depthToTry;151152static stack015i test;153154public void run() {155//156// Provoke multiple stack overflows:157//158for (int i = 0; i < stack015.CYCLES; i++)159try {160//161// All threads invoke the same synchronized method:162//163test.recurse(depthToTry);164165throw new Exception(166"TEST_RFE: no stack overflow thrown" +167", need to try deeper recursion?");168169} catch (StackOverflowError error) {170// It's OK: stack overflow was expected.171} catch (OutOfMemoryError oome) {172// Also OK: there may be no memory for stack expansion.173174} catch (Throwable throwable) {175if (throwable instanceof ThreadDeath)176throw (ThreadDeath) throwable;177// It isn't OK!178thrown = throwable;179break;180}181}182}183184185