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/stack014.
  29  * VM testbase keywords: [stress, stack, nonconcurrent]
  30  * VM testbase readme:
  31  * DESCRIPTION
  32  *     This test provokes multiple stack overflows in the multiple
  33  *     threads -- by invoking synchronized virtual recursive method
  34  *     for the given fixed depth of recursion (though, for a large
  35  *     depth). Note however, that different threads are not actual
  36  *     synchronized, because different instances having the recursive
  37  *     method are used.
  38  *     This test measures a number of recursive invocations until
  39  *     stack overflow, and then tries to provoke similar stack overflows
  40  *     10 times in each of 10 threads. Each provocation consists of
  41  *     invoking that recursive method for the given fixed depth
  42  *     of invocations which is 10 times that depth measured before.
  43  *     The test is deemed passed, if VM have not crashed, and
  44  *     if exception other than due to stack overflow was not
  45  *     thrown.
  46  * COMMENTS
  47  *     This test crashes HS versions 2.0, 1.3, and 1.4 on Solaris.
  48  *     However, it passes against all these HS versions on Win32.
  49  *     See the bug:
  50  *     4366625 (P4/S4) multiple stack overflow causes HS crash
  51  *
  52  * @requires vm.opt.DeoptimizeALot == null | vm.opt.DeoptimizeALot == false
  53  * @run main/othervm nsk.stress.stack.stack014
  54  */
  55 
  56 package nsk.stress.stack;
  57 
  58 
  59 import java.io.PrintStream;
  60 
  61 public class stack014 extends stack014i {
  62     final static int THREADS = 10;
  63     final static int CYCLES = 10;
  64 
  65     public static void main(String[] args) {
  66         int exitCode = run(args, System.out);
  67         System.exit(exitCode + 95);
  68     }
  69 
  70     public static int run(String args[], PrintStream out) {
  71         stack014i test = new stack014();
  72         //
  73         // Measure maximal recursion depth until stack overflow:
  74         //
  75         int maxDepth = 0;
  76         for (int depth = 10; ; depth += 10)
  77             try {
  78                 test.recurse(depth);
  79                 maxDepth = depth;
  80             } catch (StackOverflowError soe) {
  81                 break;
  82             } catch (OutOfMemoryError oome) {
  83                 break;
  84             }
  85         out.println("Max. depth: " + maxDepth);
  86 
  87         //
  88         // Execute multiple threads repeatedly provoking stack overflows:
  89         //
  90         stack014i threads[] = new stack014i[THREADS];
  91         for (int i = 0; i < threads.length; i++) {
  92             threads[i] = new stack014();
  93             threads[i].depthToTry = 10 * maxDepth;
  94             threads[i].cycles = CYCLES;
  95             threads[i].start();
  96         }
  97         for (int i = 0; i < threads.length; i++)
  98             if (threads[i].isAlive())
  99                 try {
 100                     threads[i].join();
 101                 } catch (InterruptedException exception) {
 102                     exception.printStackTrace(out);
 103                     return 2;
 104                 }
 105 
 106         //
 107         // Check if unexpected exceptions were thrown:
 108         //
 109         int exitCode = 0;
 110         for (int i = 0; i < threads.length; i++)
 111             if (threads[i].thrown != null) {
 112                 threads[i].thrown.printStackTrace(out);
 113                 exitCode = 2;
 114             }
 115 
 116         if (exitCode != 0)
 117             out.println("# TEST FAILED");
 118         return exitCode;
 119     }
 120 
 121     synchronized void recurse(int depth) {
 122         if (depth > 0)
 123             recurse(depth - 1);
 124     }
 125 }
 126 
 127 abstract class stack014i extends Thread {
 128     //
 129     // Pure virtual method:
 130     //
 131     abstract void recurse(int depth);
 132 
 133     Throwable thrown = null;
 134     int depthToTry;
 135     int cycles;
 136 
 137     public void run() {
 138         //
 139         // Provoke multiple stack overflows:
 140         //
 141         for (int i = 0; i < cycles; i++)
 142             try {
 143                 recurse(depthToTry);
 144                 throw new Exception(
 145                         "TEST_RFE: no stack overflow thrown" +
 146                                 ", need to try deeper recursion?");
 147 
 148             } catch (StackOverflowError error) {
 149                 // It's OK: stack overflow was expected.
 150             } catch (OutOfMemoryError oome) {
 151                 // Also OK: if there is no memory for stack expansion.
 152 
 153             } catch (Throwable throwable) {
 154                 if (throwable instanceof ThreadDeath)
 155                     throw (ThreadDeath) throwable;
 156                 // It isn't OK!
 157                 thrown = throwable;
 158                 break;
 159             }
 160     }
 161 }