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  * @run main/othervm nsk.stress.stack.stack008
  51  */
  52 
  53 package nsk.stress.stack;
  54 
  55 
  56 import java.io.PrintStream;
  57 import java.lang.reflect.InvocationTargetException;
  58 import java.lang.reflect.Method;
  59 
  60 public class stack008 {
  61     public static void main(String[] args) {
  62         int exitCode = run(args, System.out);
  63         System.exit(exitCode + 95);
  64     }
  65 
  66     public static int run(String args[], PrintStream out) {
  67         int depth;
  68         //
  69         // Measure maximal recursion depth until stack overflow:
  70         //
  71         for (depth = 100; ; depth += 100)
  72             try {
  73                 invokeRecurse(depth);
  74             } catch (Throwable exception) {
  75                 Throwable target = getTargetException(exception);
  76                 if ((target instanceof StackOverflowError) ||
  77                         (target instanceof OutOfMemoryError))
  78                     break; // OK.
  79                 target.printStackTrace(out);
  80                 if (target instanceof ThreadDeath)
  81                     throw (ThreadDeath) target;
  82                 return 2;
  83             }
  84         out.println("Max. depth: " + depth);
  85         //
  86         // Provoke stack overflow multiple times:
  87         //
  88         for (int i = 0; i < 100; i++)
  89             try {
  90                 invokeRecurse(2 * depth);
  91 //              out.println("?");
  92             } catch (Throwable exception) {
  93                 Throwable target = getTargetException(exception);
  94                 if ((target instanceof StackOverflowError) ||
  95                         (target instanceof OutOfMemoryError))
  96                     continue; // OK.
  97                 target.printStackTrace(out);
  98                 if (target instanceof ThreadDeath)
  99                     throw (ThreadDeath) target;
 100                 return 2;
 101             }
 102         return 0;
 103     }
 104 
 105     private static Throwable getTargetException(Throwable exception) {
 106         Throwable target;
 107         //
 108         // Unwrap deep chain of exceptions:
 109         //
 110         for (
 111                 target = exception;
 112                 target instanceof InvocationTargetException;
 113                 target = ((InvocationTargetException) target).getTargetException()
 114                 )
 115             ;
 116         return target;
 117     }
 118 
 119     static Method method = null;
 120     static stack008 instance = null;
 121     static Object params[] = null;
 122 
 123     private static void invokeRecurse(int depth) throws Exception {
 124         if (method == null) {
 125             //
 126             // Optimization trick: allocate once, use everywhere.
 127             //
 128             instance = new stack008();
 129             method = stack008.class.getMethod("recurse");
 130             params = new Object[]{};
 131         }
 132         //
 133         // Note, that the same instance.depth is used in all invocations:
 134         //
 135         instance.depth = depth;
 136         method.invoke(instance, params);
 137     }
 138 
 139     int depth = 0;
 140 
 141     public void recurse() throws Exception {
 142         if (depth > 0)
 143             //
 144             // Self-invoke via reflection:
 145             //
 146             invokeRecurse(depth - 1);
 147     }
 148 }