1 /* 2 * Copyright (c) 2000, 2018, 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/stack008. 29 * VM testbase keywords: [stress, stack, nonconcurrent, exclude] 30 * VM testbase comments: 8139875 31 * VM testbase readme: 32 * DESCRIPTION 33 * This test provokes multiple stack overflows in the same thread 34 * by invocations via reflection. Recursive method is invoked for 35 * the given fixed depth of recursion (though, for a large depth). 36 * This test makes measures a number of recursive invocations 37 * before 1st StackOverflowError, and then tries to reproduce 38 * such StackOverflowError 100 times -- each time by trying to 39 * invoke the same recursive method for the given fixed depth 40 * of invocations (which is twice that depth just measured). 41 * The test is deemed passed, if VM have not crashed. 42 * COMMENTS 43 * This test crashes all HS versions (2.0, 1.3, 1.4) on Solaris, 44 * and crashes HS 2.0 on win32. However, it passes against HS 1.3 45 * and 1.4 on Win32. 46 * See the bug: 47 * 4366625 (P4/S4) multiple stack overflow causes HS crash 48 * 49 * @ignore 8139875 50 * @requires vm.opt.DeoptimizeALot == null | vm.opt.DeoptimizeALot == false 51 * @run main/othervm nsk.stress.stack.stack008 52 */ 53 54 package nsk.stress.stack; 55 56 57 import java.io.PrintStream; 58 import java.lang.reflect.InvocationTargetException; 59 import java.lang.reflect.Method; 60 61 public class stack008 { 62 public static void main(String[] args) { 63 int exitCode = run(args, System.out); 64 System.exit(exitCode + 95); 65 } 66 67 public static int run(String args[], PrintStream out) { 68 int depth; 69 // 70 // Measure maximal recursion depth until stack overflow: 71 // 72 for (depth = 100; ; depth += 100) 73 try { 74 invokeRecurse(depth); 75 } catch (Throwable exception) { 76 Throwable target = getTargetException(exception); 77 if ((target instanceof StackOverflowError) || 78 (target instanceof OutOfMemoryError)) 79 break; // OK. 80 target.printStackTrace(out); 81 if (target instanceof ThreadDeath) 82 throw (ThreadDeath) target; 83 return 2; 84 } 85 out.println("Max. depth: " + depth); 86 // 87 // Provoke stack overflow multiple times: 88 // 89 for (int i = 0; i < 100; i++) 90 try { 91 invokeRecurse(2 * depth); 92 // out.println("?"); 93 } catch (Throwable exception) { 94 Throwable target = getTargetException(exception); 95 if ((target instanceof StackOverflowError) || 96 (target instanceof OutOfMemoryError)) 97 continue; // OK. 98 target.printStackTrace(out); 99 if (target instanceof ThreadDeath) 100 throw (ThreadDeath) target; 101 return 2; 102 } 103 return 0; 104 } 105 106 private static Throwable getTargetException(Throwable exception) { 107 Throwable target; 108 // 109 // Unwrap deep chain of exceptions: 110 // 111 for ( 112 target = exception; 113 target instanceof InvocationTargetException; 114 target = ((InvocationTargetException) target).getTargetException() 115 ) 116 ; 117 return target; 118 } 119 120 static Method method = null; 121 static stack008 instance = null; 122 static Object params[] = null; 123 124 private static void invokeRecurse(int depth) throws Exception { 125 if (method == null) { 126 // 127 // Optimization trick: allocate once, use everywhere. 128 // 129 instance = new stack008(); 130 method = stack008.class.getMethod("recurse"); 131 params = new Object[]{}; 132 } 133 // 134 // Note, that the same instance.depth is used in all invocations: 135 // 136 instance.depth = depth; 137 method.invoke(instance, params); 138 } 139 140 int depth = 0; 141 142 public void recurse() throws Exception { 143 if (depth > 0) 144 // 145 // Self-invoke via reflection: 146 // 147 invokeRecurse(depth - 1); 148 } 149 }