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/stack011.
  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 final static recursive method for the
  34  *     given 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, 1.4 on Win32 and Solaris
  45  *     platforms.
  46  *     See the bug:
  47  *     4366625 (P4/S4) multiple stack overflow causes HS crash
  48  *
  49  * @run main/othervm nsk.stress.stack.stack011
  50  */
  51 
  52 package nsk.stress.stack;
  53 
  54 
  55 import java.io.PrintStream;
  56 
  57 public class stack011 extends Thread {
  58     final static int THREADS = 10;
  59     final static int CYCLES = 10;
  60 
  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         //
  68         // Measure maximal recursion depth until stack overflow:
  69         //
  70         int maxDepth = 0;
  71         for (int depth = 10; ; depth += 10)
  72             try {
  73                 recurse(depth);
  74                 maxDepth = depth;
  75             } catch (StackOverflowError soe) {
  76                 break;
  77             } catch (OutOfMemoryError oome) {
  78                 break;
  79             }
  80         out.println("Max. depth: " + maxDepth);
  81 
  82         //
  83         // Execute multiple threads repeatedly provoking stack overflows:
  84         //
  85         stack011 threads[] = new stack011[THREADS];
  86         for (int i = 0; i < threads.length; i++) {
  87             threads[i] = new stack011();
  88             threads[i].depthToTry = 10 * maxDepth;
  89             threads[i].start();
  90         }
  91         for (int i = 0; i < threads.length; i++)
  92             if (threads[i].isAlive())
  93                 try {
  94                     threads[i].join();
  95                 } catch (InterruptedException exception) {
  96                     exception.printStackTrace(out);
  97                     return 2;
  98                 }
  99 
 100         //
 101         // Check if unexpected exceptions were not thrown:
 102         //
 103         int exitCode = 0;
 104         for (int i = 0; i < threads.length; i++)
 105             if (threads[i].thrown != null) {
 106                 threads[i].thrown.printStackTrace(out);
 107                 exitCode = 2;
 108             }
 109 
 110         if (exitCode != 0)
 111             out.println("# TEST FAILED");
 112         return exitCode;
 113     }
 114 
 115     int depthToTry = 0;
 116     Throwable thrown = null;
 117 
 118     public void run() {
 119         for (int i = 0; i < CYCLES; i++)
 120             try {
 121                 recurse(depthToTry);
 122                 throw new Exception(
 123                         "TEST_RFE: no stack overflow thrown" +
 124                                 ", need to try deeper recursion?");
 125 
 126             } catch (StackOverflowError error) {
 127                 // It's OK: stack overflow was expected.
 128             } catch (OutOfMemoryError oome) {
 129                 // Also OK: recursion may result in memory lack.
 130 
 131             } catch (Throwable throwable) {
 132                 if (throwable instanceof ThreadDeath)
 133                     throw (ThreadDeath) throwable;
 134                 // It isn't OK!
 135                 thrown = throwable;
 136                 break;
 137             }
 138     }
 139 
 140     final static void recurse(int depth) {
 141         if (depth > 0)
 142             recurse(depth - 1);
 143     }
 144 }