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/stack017.
  29  * VM testbase keywords: [stress, diehard, stack, nonconcurrent, exclude]
  30  * VM testbase comments: 8139875
  31  * VM testbase readme:
  32  * DESCRIPTION
  33  *     The test invokes infinitely recursive method from within stack
  34  *     overflow handler  -- repeatedly multiple times, and in multiple
  35  *     threads.
  36  *     The test is deemed passed, if VM have not crashed, and
  37  *     if exception other than due to stack overflow was not
  38  *     thrown.
  39  * COMMENTS
  40  *     This test crashes HS versions 2.0, 1.3, and 1.4 on both
  41  *     Solaris and Win32 platforms.
  42  *     See the bug:
  43  *     4366625 (P4/S4) multiple stack overflow causes HS crash
  44  *
  45  * @ignore 8139875
  46  * @requires vm.opt.DeoptimizeALot == null | vm.opt.DeoptimizeALot == false
  47  * @run main/othervm nsk.stress.stack.stack017 -eager
  48  */
  49 
  50 package nsk.stress.stack;
  51 
  52 
  53 import nsk.share.Harakiri;
  54 
  55 import java.io.PrintStream;
  56 
  57 public class stack017 extends Thread {
  58     private final static int THREADS = 10;
  59     private final static int CYCLES = 10;
  60     private final static int PROBES = 100;
  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         verbose = false;
  69         boolean eager = false;
  70         for (int i = 0; i < args.length; i++)
  71             if (args[i].toLowerCase().equals("-verbose"))
  72                 verbose = true;
  73             else if (args[i].toLowerCase().equals("-eager"))
  74                 eager = true;
  75         if (!eager)
  76             Harakiri.appoint(Harakiri.parseAppointment(args));
  77         stack017.out = out;
  78         stack017 test = new stack017();
  79         return test.doRun();
  80     }
  81 
  82     private static boolean verbose;
  83     private static PrintStream out;
  84 
  85     private void display(Object message) {
  86         if (!verbose)
  87             return;
  88         synchronized (out) {
  89             out.println(message.toString());
  90         }
  91     }
  92 
  93     private static int depthToTry;
  94 
  95     private int doRun() {
  96         //
  97         // Measure recursive depth before stack overflow:
  98         //
  99         try {
 100             recurse(0);
 101         } catch (StackOverflowError soe) {
 102         } catch (OutOfMemoryError oome) {
 103         }
 104         out.println("Maximal recursion depth: " + maxDepth);
 105         depthToTry = maxDepth;
 106 
 107         //
 108         // Run the tested threads:
 109         //
 110         stack017 threads[] = new stack017[THREADS];
 111         for (int i = 0; i < threads.length; i++) {
 112             threads[i] = new stack017();
 113             threads[i].setName("Thread: " + (i + 1) + "/" + THREADS);
 114             threads[i].start();
 115         }
 116         for (int i = 0; i < threads.length; i++)
 117             if (threads[i].isAlive())
 118                 try {
 119                     threads[i].join();
 120                 } catch (InterruptedException exception) {
 121                     exception.printStackTrace(out);
 122                     return 2;
 123                 }
 124 
 125         //
 126         // Check if unexpected exceptions were thrown:
 127         //
 128         int exitCode = 0;
 129         for (int i = 0; i < threads.length; i++)
 130             if (threads[i].thrown != null) {
 131                 threads[i].thrown.printStackTrace(out);
 132                 exitCode = 2;
 133             }
 134 
 135         if (exitCode != 0)
 136             out.println("# TEST FAILED");
 137         return exitCode;
 138     }
 139 
 140     private int maxDepth = 0;
 141 
 142     private void recurse(int depth) {
 143         maxDepth = depth;
 144         recurse(depth + 1);
 145     }
 146 
 147     private void trickyRecurse(int depth) {
 148         try {
 149             maxDepth = depth;
 150             trickyRecurse(depth + 1);
 151         } catch (Error error) {
 152             if (!(error instanceof StackOverflowError) &&
 153                     !(error instanceof OutOfMemoryError))
 154                 throw error;
 155 
 156             //
 157             // Stack problem caugth: provoke it again,
 158             // if current stack is enough deep:
 159             //
 160             if (depth < depthToTry - PROBES)
 161                 throw error;
 162             recurse(depth + 1);
 163         }
 164     }
 165 
 166     private Throwable thrown = null;
 167 
 168     public void run() {
 169         String threadName = Thread.currentThread().getName();
 170         for (int i = 1; i <= CYCLES; i++)
 171             try {
 172                 display(threadName + ", iteration: " + i + "/" + CYCLES);
 173                 trickyRecurse(0);
 174                 throw new Exception(
 175                         "TEST_BUG: stack overflow was expected!");
 176 
 177             } catch (StackOverflowError oome) {
 178                 // It's OK: stack overflow was expected.
 179             } catch (OutOfMemoryError oome) {
 180                 // Also OK, if there is no memory for stack expansion.
 181 
 182             } catch (Throwable throwable) {
 183                 if (throwable instanceof ThreadDeath)
 184                     throw (ThreadDeath) throwable;
 185                 // It isn't OK!
 186                 thrown = throwable;
 187                 break;
 188             }
 189     }
 190 }