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