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