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/stack019.
  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 in a single thread.
  35  *     The test is deemed passed, if VM have not crashed, and if exception
  36  *     other than due to stack overflow was not thrown.
  37  * COMMENTS
  38  *     This test crashes HS versions 2.0, 1.3, and 1.4 on both
  39  *     Solaris and Win32 platforms.
  40  *     See the bug:
  41  *     4366625 (P4/S4) multiple stack overflow causes HS crash
  42  *
  43  * @ignore 8139875
  44  * @requires vm.opt.DeoptimizeALot == null | vm.opt.DeoptimizeALot == false
  45  * @run main/othervm nsk.stress.stack.stack019 -eager
  46  */
  47 
  48 package nsk.stress.stack;
  49 
  50 
  51 import nsk.share.Harakiri;
  52 
  53 import java.io.PrintStream;
  54 
  55 public class stack019 {
  56     private final static int CYCLES = 100;
  57     private final static int PROBES = 100;
  58 
  59     public static void main(String[] args) {
  60         int exitCode = run(args, System.out);
  61         System.exit(exitCode + 95);
  62     }
  63 
  64     public static int run(String args[], PrintStream out) {
  65         boolean verbose = false, eager = false;
  66         for (int i = 0; i < args.length; i++)
  67             if (args[i].toLowerCase().equals("-verbose"))
  68                 verbose = true;
  69             else if (args[i].toLowerCase().equals("-eager"))
  70                 eager = true;
  71         if (!eager)
  72             Harakiri.appoint(Harakiri.parseAppointment(args));
  73         //
  74         // Measure recursive depth before stack overflow:
  75         //
  76         try {
  77             recurse(0);
  78         } catch (StackOverflowError soe) {
  79         } catch (OutOfMemoryError oome) {
  80         }
  81         out.println("Maximal recursion depth: " + maxDepth);
  82         depthToTry = maxDepth;
  83 
  84         //
  85         // Run the tested threads:
  86         //
  87         for (int i = 0; i < CYCLES; i++) {
  88             try {
  89                 out.println("Iteration: " + i + "/" + CYCLES);
  90                 trickyRecurse(0);
  91                 out.println("# TEST_BUG: stack overflow was expected!");
  92                 return 2;
  93 
  94             } catch (StackOverflowError error) {
  95             } catch (OutOfMemoryError oome) {
  96                 // It's OK: stack overflow was expected.
  97 
  98             } catch (Throwable throwable) {
  99                 if (throwable instanceof ThreadDeath)
 100                     throw (ThreadDeath) throwable;
 101                 throwable.printStackTrace(out);
 102                 return 2;
 103             }
 104         }
 105         return 0;
 106     }
 107 
 108     private static int maxDepth;
 109     private static int depthToTry;
 110 
 111     private static void recurse(int depth) {
 112         maxDepth = depth;
 113         recurse(depth + 1);
 114     }
 115 
 116     private static void trickyRecurse(int depth) {
 117         try {
 118             maxDepth = depth;
 119             trickyRecurse(depth + 1);
 120         } catch (Error error) {
 121             if (!(error instanceof StackOverflowError) &&
 122                     !(error instanceof OutOfMemoryError))
 123                 throw error;
 124 
 125             //
 126             // Stack problem caugth: provoke it again,
 127             // if current stack is enough deep:
 128             //
 129             if (depth < depthToTry - PROBES)
 130                 throw error;
 131             recurse(depth + 1);
 132         }
 133     }
 134 }