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  * @run main/othervm nsk.stress.stack.stack019 -eager
  45  */
  46 
  47 package nsk.stress.stack;
  48 
  49 
  50 import nsk.share.Harakiri;
  51 
  52 import java.io.PrintStream;
  53 
  54 public class stack019 {
  55     private final static int CYCLES = 100;
  56     private final static int PROBES = 100;
  57 
  58     public static void main(String[] args) {
  59         int exitCode = run(args, System.out);
  60         System.exit(exitCode + 95);
  61     }
  62 
  63     public static int run(String args[], PrintStream out) {
  64         boolean verbose = false, eager = false;
  65         for (int i = 0; i < args.length; i++)
  66             if (args[i].toLowerCase().equals("-verbose"))
  67                 verbose = true;
  68             else if (args[i].toLowerCase().equals("-eager"))
  69                 eager = true;
  70         if (!eager)
  71             Harakiri.appoint(Harakiri.parseAppointment(args));
  72         //
  73         // Measure recursive depth before stack overflow:
  74         //
  75         try {
  76             recurse(0);
  77         } catch (StackOverflowError soe) {
  78         } catch (OutOfMemoryError oome) {
  79         }
  80         out.println("Maximal recursion depth: " + maxDepth);
  81         depthToTry = maxDepth;
  82 
  83         //
  84         // Run the tested threads:
  85         //
  86         for (int i = 0; i < CYCLES; i++) {
  87             try {
  88                 out.println("Iteration: " + i + "/" + CYCLES);
  89                 trickyRecurse(0);
  90                 out.println("# TEST_BUG: stack overflow was expected!");
  91                 return 2;
  92 
  93             } catch (StackOverflowError error) {
  94             } catch (OutOfMemoryError oome) {
  95                 // It's OK: stack overflow was expected.
  96 
  97             } catch (Throwable throwable) {
  98                 if (throwable instanceof ThreadDeath)
  99                     throw (ThreadDeath) throwable;
 100                 throwable.printStackTrace(out);
 101                 return 2;
 102             }
 103         }
 104         return 0;
 105     }
 106 
 107     private static int maxDepth;
 108     private static int depthToTry;
 109 
 110     private static void recurse(int depth) {
 111         maxDepth = depth;
 112         recurse(depth + 1);
 113     }
 114 
 115     private static void trickyRecurse(int depth) {
 116         try {
 117             maxDepth = depth;
 118             trickyRecurse(depth + 1);
 119         } catch (Error error) {
 120             if (!(error instanceof StackOverflowError) &&
 121                     !(error instanceof OutOfMemoryError))
 122                 throw error;
 123 
 124             //
 125             // Stack problem caugth: provoke it again,
 126             // if current stack is enough deep:
 127             //
 128             if (depth < depthToTry - PROBES)
 129                 throw error;
 130             recurse(depth + 1);
 131         }
 132     }
 133 }