Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/stress/stack/stack016.java
41159 views
/*1* Copyright (c) 2000, 2021, 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/stack016.28* VM testbase keywords: [stress, diehard, stack, nonconcurrent]29* VM testbase readme:30* DESCRIPTION31* The test provokes second stack overflow from within the32* stack overflow handler -- repeatedly multiple times, and33* in multiple threads.34* This test measures a number of recursive invocations until35* stack overflow, and then tries to provoke similar stack overflows36* in 10 times in each of 10 threads. Each provocation consists of37* invoking that recursive method for the given fixed depth38* of invocations which is 10 times that depth measured before,39* and then trying to invoke that recursive method once again40* from within the catch clause just caught StackOverflowError.41* The test is deemed passed, if VM have not crashed, and42* if exception other than due to stack overflow was not43* thrown.44* COMMENTS45* This test crashes HS versions 2.0, 1.3, and 1.4 on both46* Solaris and Win32 platforms.47* See the bug:48* 4366625 (P4/S4) multiple stack overflow causes HS crash49*50* @requires (vm.opt.DeoptimizeALot != true & vm.compMode != "Xcomp")51* @library /vmTestbase52* @build nsk.share.Terminator53* @run main/othervm/timeout=900 -Xint -Xss448K nsk.stress.stack.stack016 -eager54* @run main/othervm/timeout=900 -Xcomp -Xss448K nsk.stress.stack.stack016 -eager55* @run main/othervm/timeout=900 -Xcomp -XX:-TieredCompilation -Xss448K nsk.stress.stack.stack016 -eager56*/5758package nsk.stress.stack;596061import nsk.share.Terminator;6263import java.io.PrintStream;6465public class stack016 extends Thread {66private final static int THREADS = 10;67private final static int CYCLES = 10;68private final static int STEP = 10;69private final static int RESERVE = 10;70private final static int PROBES = STEP * RESERVE;7172public static void main(String[] args) {73int exitCode = run(args, System.out);74System.exit(exitCode + 95);75}7677public static int run(String args[], PrintStream out) {78verbose = false;79boolean eager = false;80for (int i = 0; i < args.length; i++)81if (args[i].toLowerCase().equals("-verbose"))82verbose = true;83else if (args[i].toLowerCase().equals("-eager"))84eager = true;85if (!eager)86Terminator.appoint(Terminator.parseAppointment(args));87stack016.out = out;88stack016 test = new stack016();89return test.doRun();90}9192private static boolean verbose;93private static PrintStream out;9495private void display(Object message) {96if (!verbose)97return;98synchronized (out) {99out.println(message.toString());100}101}102103private int doRun() {104//105// Measure recursive depth before stack overflow:106//107int maxDepth = 0;108for (depthToTry = 0; ; depthToTry += STEP) {109try {110trickyRecurse(depthToTry);111maxDepth = depthToTry;112} catch (StackOverflowError | OutOfMemoryError ex) {113break;114}115}116out.println("Maximal recursion depth: " + maxDepth);117118//119// Run the tested threads:120//121stack016 threads[] = new stack016[THREADS];122for (int i = 0; i < threads.length; i++) {123threads[i] = new stack016();124threads[i].setName("Thread: " + (i + 1) + "/" + THREADS);125threads[i].depthToTry = RESERVE * maxDepth;126threads[i].start();127}128for (int i = 0; i < threads.length; i++) {129if (threads[i].isAlive()) {130try {131threads[i].join();132} catch (InterruptedException exception) {133exception.printStackTrace(out);134return 2;135}136}137}138139//140// Check if unexpected exceptions were thrown:141//142int exitCode = 0;143for (int i = 0; i < threads.length; i++) {144if (threads[i].thrown != null) {145threads[i].thrown.printStackTrace(out);146exitCode = 2;147}148}149if (exitCode != 0)150out.println("# TEST FAILED");151return exitCode;152}153154private int stackTop = 0;155private int depthToTry = 0;156private Throwable thrown = null;157158private void trickyRecurse(int depth) {159stackTop = depthToTry - depth;160if (depth > 0) {161try {162trickyRecurse(depth - 1);163} catch (Error error) {164if (!(error instanceof StackOverflowError) &&165!(error instanceof OutOfMemoryError))166throw error;167168//169// Provoke more stack overflow,170// if current stack is deep enough:171//172if (depthToTry - depth < stackTop - PROBES)173throw error;174recurse(depthToTry);175176throw new Error("TEST_RFE: try deeper recursion!");177}178}179}180181private static void recurse(int depth) {182if (depth > 0)183recurse(depth - 1);184}185186public void run() {187String threadName = Thread.currentThread().getName();188for (int i = 1; i <= CYCLES; i++) {189try {190display(threadName + ", iteration: " + i + "/" + CYCLES +191", depthToTry: " + depthToTry);192trickyRecurse(depthToTry);193throw new Error(194"TEST_BUG: trickyRecursion() must throw an error anyway!");195196} catch (StackOverflowError error) {197// It's OK: stack overflow was expected.198} catch (OutOfMemoryError oome) {199// Also OK, if there is no memory for stack expansion.200201} catch (Throwable throwable) {202if (throwable instanceof ThreadDeath)203throw (ThreadDeath) throwable;204thrown = throwable;205break;206}207}208}209}210211212