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/stack016.
  29  * VM testbase keywords: [stress, diehard, stack, nonconcurrent, exclude]
  30  * VM testbase comments: 8139875
  31  * VM testbase readme:
  32  * DESCRIPTION
  33  *     The test provokes second stack overflow from within the
  34  *     stack overflow handler -- repeatedly multiple times, and
  35  *     in multiple threads.
  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  *     and then trying to invoke that recursive method once again
  42  *     from within the catch clause just caught StackOverflowError.
  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 both
  48  *     Solaris and Win32 platforms.
  49  *     See the bug:
  50  *     4366625 (P4/S4) multiple stack overflow causes HS crash
  51  *
  52  * @ignore 8139875
  53  * @requires vm.opt.DeoptimizeALot == null | vm.opt.DeoptimizeALot == false
  54  * @run main/othervm nsk.stress.stack.stack016 -eager
  55  */
  56 
  57 package nsk.stress.stack;
  58 
  59 
  60 import nsk.share.Harakiri;
  61 
  62 import java.io.PrintStream;
  63 
  64 public class stack016 extends Thread {
  65     private final static int THREADS = 10;
  66     private final static int CYCLES = 10;
  67     private final static int STEP = 10;
  68     private final static int RESERVE = 10;
  69     private final static int PROBES = STEP * RESERVE;
  70 
  71     public static void main(String[] args) {
  72         int exitCode = run(args, System.out);
  73         System.exit(exitCode + 95);
  74     }
  75 
  76     public static int run(String args[], PrintStream out) {
  77         verbose = false;
  78         boolean eager = false;
  79         for (int i = 0; i < args.length; i++)
  80             if (args[i].toLowerCase().equals("-verbose"))
  81                 verbose = true;
  82             else if (args[i].toLowerCase().equals("-eager"))
  83                 eager = true;
  84         if (!eager)
  85             Harakiri.appoint(Harakiri.parseAppointment(args));
  86         stack016.out = out;
  87         stack016 test = new stack016();
  88         return test.doRun();
  89     }
  90 
  91     private static boolean verbose;
  92     private static PrintStream out;
  93 
  94     private void display(Object message) {
  95         if (!verbose)
  96             return;
  97         synchronized (out) {
  98             out.println(message.toString());
  99         }
 100     }
 101 
 102     private int doRun() {
 103         //
 104         // Measure recursive depth before stack overflow:
 105         //
 106         int maxDepth = 0;
 107         for (depthToTry = 0; ; depthToTry += STEP)
 108             try {
 109                 trickyRecurse(depthToTry);
 110                 maxDepth = depthToTry;
 111             } catch (Error error) {
 112                 break;
 113             }
 114         out.println("Maximal recursion depth: " + maxDepth);
 115 
 116         //
 117         // Run the tested threads:
 118         //
 119         stack016 threads[] = new stack016[THREADS];
 120         for (int i = 0; i < threads.length; i++) {
 121             threads[i] = new stack016();
 122             threads[i].setName("Thread: " + (i + 1) + "/" + THREADS);
 123             threads[i].depthToTry = RESERVE * maxDepth;
 124             threads[i].start();
 125         }
 126         for (int i = 0; i < threads.length; i++)
 127             if (threads[i].isAlive())
 128                 try {
 129                     threads[i].join();
 130                 } catch (InterruptedException exception) {
 131                     exception.printStackTrace(out);
 132                     return 2;
 133                 }
 134 
 135         //
 136         // Check if unexpected exceptions were thrown:
 137         //
 138         int exitCode = 0;
 139         for (int i = 0; i < threads.length; i++)
 140             if (threads[i].thrown != null) {
 141                 threads[i].thrown.printStackTrace(out);
 142                 exitCode = 2;
 143             }
 144 
 145         if (exitCode != 0)
 146             out.println("# TEST FAILED");
 147         return exitCode;
 148     }
 149 
 150     private int stackTop = 0;
 151     private int depthToTry = 0;
 152     private Throwable thrown = null;
 153 
 154     private void trickyRecurse(int depth) {
 155         stackTop = depthToTry - depth;
 156         if (depth > 0)
 157             try {
 158                 trickyRecurse(depth - 1);
 159             } catch (Error error) {
 160                 if (!(error instanceof StackOverflowError) &&
 161                         !(error instanceof OutOfMemoryError))
 162                     throw error;
 163 
 164                 //
 165                 // Provoke more stack overflow,
 166                 // if current stack is deep enough:
 167                 //
 168                 if (depthToTry - depth < stackTop - PROBES)
 169                     throw error;
 170                 recurse(depthToTry);
 171 
 172                 throw new Error("TEST_RFE: try deeper recursion!");
 173             }
 174     }
 175 
 176     private static void recurse(int depth) {
 177         if (depth > 0)
 178             recurse(depth - 1);
 179     }
 180 
 181     public void run() {
 182         String threadName = Thread.currentThread().getName();
 183         for (int i = 1; i <= CYCLES; i++)
 184             try {
 185                 display(threadName + ", iteration: " + i + "/" + CYCLES);
 186                 trickyRecurse(depthToTry);
 187                 throw new Error(
 188                         "TEST_BUG: trickyRecursion() must throw an error anyway!");
 189 
 190             } catch (StackOverflowError error) {
 191                 // It's OK: stack overflow was expected.
 192             } catch (OutOfMemoryError oome) {
 193                 // Also OK, if there is no memory for stack expansion.
 194 
 195             } catch (Throwable throwable) {
 196                 if (throwable instanceof ThreadDeath)
 197                     throw (ThreadDeath) throwable;
 198                 thrown = throwable;
 199                 break;
 200             }
 201     }
 202 }