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