Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/stress/stack/stack017.java
51746 views
/*1* Copyright (c) 2000, 2020, 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/stack017.28* VM testbase keywords: [stress, diehard, stack, nonconcurrent]29* VM testbase readme:30* DESCRIPTION31* The test invokes infinitely recursive method from within stack32* overflow handler -- repeatedly multiple times, and in multiple33* threads.34* The test is deemed passed, if VM have not crashed, and35* if exception other than due to stack overflow was not36* thrown.37* COMMENTS38* This test crashes HS versions 2.0, 1.3, and 1.4 on both39* Solaris and Win32 platforms.40* See the bug:41* 4366625 (P4/S4) multiple stack overflow causes HS crash42*43* @requires (vm.opt.DeoptimizeALot != true & vm.compMode != "Xcomp" & vm.pageSize == 4096)44* @library /vmTestbase45* @build nsk.share.Terminator46* @run main/othervm/timeout=900 -Xss220K nsk.stress.stack.stack017 -eager47*/4849package nsk.stress.stack;505152import nsk.share.Terminator;5354import java.io.PrintStream;5556public class stack017 extends Thread {57private final static int THREADS = 10;58private final static int CYCLES = 10;59private final static int PROBES = 100;6061public static void main(String[] args) {62int exitCode = run(args, System.out);63System.exit(exitCode + 95);64}6566public static int run(String args[], PrintStream out) {67verbose = false;68boolean eager = false;69for (int i = 0; i < args.length; i++)70if (args[i].toLowerCase().equals("-verbose"))71verbose = true;72else if (args[i].toLowerCase().equals("-eager"))73eager = true;74if (!eager)75Terminator.appoint(Terminator.parseAppointment(args));76stack017.out = out;77stack017 test = new stack017();78return test.doRun();79}8081private static boolean verbose;82private static PrintStream out;8384private void display(Object message) {85if (!verbose)86return;87synchronized (out) {88out.println(message.toString());89}90}9192private static int depthToTry;9394private int doRun() {95//96// Measure recursive depth before stack overflow:97//98try {99recurse(0);100} catch (StackOverflowError soe) {101} catch (OutOfMemoryError oome) {102}103out.println("Maximal recursion depth: " + maxDepth);104depthToTry = maxDepth;105106//107// Run the tested threads:108//109stack017 threads[] = new stack017[THREADS];110for (int i = 0; i < threads.length; i++) {111threads[i] = new stack017();112threads[i].setName("Thread: " + (i + 1) + "/" + THREADS);113threads[i].start();114}115for (int i = 0; i < threads.length; i++)116if (threads[i].isAlive())117try {118threads[i].join();119} catch (InterruptedException exception) {120exception.printStackTrace(out);121return 2;122}123124//125// Check if unexpected exceptions were thrown:126//127int exitCode = 0;128for (int i = 0; i < threads.length; i++)129if (threads[i].thrown != null) {130threads[i].thrown.printStackTrace(out);131exitCode = 2;132}133134if (exitCode != 0)135out.println("# TEST FAILED");136return exitCode;137}138139private int maxDepth = 0;140141private void recurse(int depth) {142maxDepth = depth;143recurse(depth + 1);144}145146private void trickyRecurse(int depth) {147try {148maxDepth = depth;149trickyRecurse(depth + 1);150} catch (Error error) {151if (!(error instanceof StackOverflowError) &&152!(error instanceof OutOfMemoryError))153throw error;154155//156// Stack problem caught: provoke it again,157// if current stack is enough deep:158//159if (depth < depthToTry - PROBES)160throw error;161recurse(depth + 1);162}163}164165private Throwable thrown = null;166167public void run() {168String threadName = Thread.currentThread().getName();169for (int i = 1; i <= CYCLES; i++)170try {171display(threadName + ", iteration: " + i + "/" + CYCLES);172trickyRecurse(0);173throw new Exception(174"TEST_BUG: stack overflow was expected!");175176} catch (StackOverflowError oome) {177// It's OK: stack overflow was expected.178} catch (OutOfMemoryError oome) {179// Also OK, if there is no memory for stack expansion.180181} catch (Throwable throwable) {182if (throwable instanceof ThreadDeath)183throw (ThreadDeath) throwable;184// It isn't OK!185thrown = throwable;186break;187}188}189}190191192