Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/stress/stack/stack008.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/stack008.28* VM testbase keywords: [stress, stack, nonconcurrent]29* VM testbase readme:30* DESCRIPTION31* This test provokes multiple stack overflows in the same thread32* by invocations via reflection. Recursive method is invoked for33* the given fixed depth of recursion (though, for a large depth).34* This test makes measures a number of recursive invocations35* before 1st StackOverflowError, and then tries to reproduce36* such StackOverflowError 100 times -- each time by trying to37* invoke the same recursive method for the given fixed depth38* of invocations (which is twice that depth just measured).39* The test is deemed passed, if VM have not crashed.40* COMMENTS41* This test crashes all HS versions (2.0, 1.3, 1.4) on Solaris,42* and crashes HS 2.0 on win32. However, it passes against HS 1.343* and 1.4 on Win32.44* See the bug:45* 4366625 (P4/S4) multiple stack overflow causes HS crash46* The stack size is too small to run on systems with > 4K page size.47* Making it bigger could cause timeouts on other platform.48*49* @requires (vm.opt.DeoptimizeALot != true & vm.compMode != "Xcomp" & vm.pageSize == 4096)50* @run main/othervm/timeout=900 -Xss200K nsk.stress.stack.stack00851*/5253package nsk.stress.stack;545556import java.io.PrintStream;57import java.lang.reflect.InvocationTargetException;58import java.lang.reflect.Method;5960public class stack008 {61public static void main(String[] args) {62int exitCode = run(args, System.out);63System.exit(exitCode + 95);64}6566public static int run(String args[], PrintStream out) {67int depth;68//69// Measure maximal recursion depth until stack overflow:70//71for (depth = 100; ; depth += 100)72try {73invokeRecurse(depth);74} catch (Throwable exception) {75Throwable target = getTargetException(exception);76if ((target instanceof StackOverflowError) ||77(target instanceof OutOfMemoryError))78break; // OK.79target.printStackTrace(out);80if (target instanceof ThreadDeath)81throw (ThreadDeath) target;82return 2;83}84out.println("Max. depth: " + depth);85//86// Provoke stack overflow multiple times:87//88for (int i = 0; i < 100; i++)89try {90invokeRecurse(2 * depth);91// out.println("?");92} catch (Throwable exception) {93Throwable target = getTargetException(exception);94if ((target instanceof StackOverflowError) ||95(target instanceof OutOfMemoryError))96continue; // OK.97target.printStackTrace(out);98if (target instanceof ThreadDeath)99throw (ThreadDeath) target;100return 2;101}102return 0;103}104105private static Throwable getTargetException(Throwable exception) {106Throwable target;107//108// Unwrap deep chain of exceptions:109//110for (111target = exception;112target instanceof InvocationTargetException;113target = ((InvocationTargetException) target).getTargetException()114)115;116return target;117}118119static Method method = null;120static stack008 instance = null;121static Object params[] = null;122123private static void invokeRecurse(int depth) throws Exception {124if (method == null) {125//126// Optimization trick: allocate once, use everywhere.127//128instance = new stack008();129method = stack008.class.getMethod("recurse");130params = new Object[]{};131}132//133// Note, that the same instance.depth is used in all invocations:134//135instance.depth = depth;136method.invoke(instance, params);137}138139int depth = 0;140141public void recurse() throws Exception {142if (depth > 0)143//144// Self-invoke via reflection:145//146invokeRecurse(depth - 1);147}148}149150151