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/stack002.
  29  * VM testbase keywords: [stress, quick, stack, nonconcurrent]
  30  * VM testbase readme:
  31  * DESCRIPTION
  32  *     Provoke StackOverflowError by infinite recursion in Java method,
  33  *     intercept the exception and continue to invoke that method until
  34  *     the test exceeds timeout, or until Java VM crashes.
  35  * COMMENTS
  36  *     I believe that the test causes HS crashes due to the following bug:
  37  *     4330318 (P2/S2) NSK test fails as An irrecoverable stack overflow
  38  *     See also bugs (lots of bugs!):
  39  *     Evaluated:
  40  *     4217960 [native stack overflow bug] reflection test causes crash
  41  *     Accepted:
  42  *     4285716 native stack overflow causes crash on Solaris
  43  *     4281578 Second stack overflow crashes HotSpot VM
  44  *     Closed (duplicate):
  45  *     4027933     Native stack overflows not detected or handled correctly
  46  *     4134353     (hpi) sysThreadCheckStack is a no-op on win32
  47  *     4185411     Various crashes when using recursive reflection.
  48  *     4167055     infinite recursion in FindClass
  49  *     4222359     Infinite recursion crashes jvm
  50  *     Closed (will not fix):
  51  *     4231968 StackOverflowError in a native method causes Segmentation Fault
  52  *     4254634     println() while catching StackOverflowError causes hotspot VM crash
  53  *     4302288 the second stack overflow causes Classic VM to exit on win32
  54  *
  55  * @run main/othervm nsk.stress.stack.stack002
  56  */
  57 
  58 package nsk.stress.stack;
  59 
  60 
  61 import java.io.PrintStream;
  62 
  63 public class stack002 {
  64     static final long timeout = 10000; // 10 seconds
  65 
  66     public static void main(String[] args) {
  67         int exitCode = run(args, System.out);
  68         System.exit(exitCode + 95);
  69     }
  70 
  71     public static int run(String args[], PrintStream out) {
  72         Tester tester = new Tester(out);
  73         Timer timer = new Timer(tester);
  74         timer.start();
  75         tester.start();
  76         while (timer.isAlive())
  77             try {
  78                 timer.join();
  79             } catch (InterruptedException e) {
  80                 e.printStackTrace(out);
  81                 return 2;
  82             }
  83         //      if (tester.isAlive())
  84 //          return 2;
  85         out.println("Maximal depth: " + tester.maxdepth);
  86         return 0;
  87     }
  88 
  89     private static class Tester extends Thread {
  90         int maxdepth;
  91         PrintStream out;
  92 
  93         public Tester(PrintStream out) {
  94             this.out = out;
  95             maxdepth = 0;
  96         }
  97 
  98         public void run() {
  99             recurse(0);
 100         }
 101 
 102         void recurse(int depth) {
 103             maxdepth = depth;
 104             try {
 105                 recurse(depth + 1);
 106 //          } catch (StackOverflowError e) {
 107 //
 108 // OutOfMemoryError is also eligible to indicate stack overflow:
 109 //
 110             } catch (Error error) {
 111                 if (!(error instanceof StackOverflowError) &&
 112                         !(error instanceof OutOfMemoryError))
 113                     throw error;
 114 
 115 /***
 116  *** Originally, I supposed that VM crashes because of unexpected
 117  *** native stack overflow (println() invokes native method).
 118  *** However, I found that HS 1.3 and HS 2.0 crash even on
 119  *** invocation of Java (not native) method.
 120  ***
 121  out.println("StackOverflowError, depth=" + depth);
 122  ***/
 123                 recurse(depth + 1);
 124             }
 125         }
 126     }
 127 
 128     private static class Timer extends Thread {
 129         private Tester tester;
 130 
 131         public Timer(Tester tester) {
 132             this.tester = tester;
 133         }
 134 
 135         public void run() {
 136             long started;
 137             started = System.currentTimeMillis();
 138             while (System.currentTimeMillis() - started < timeout)
 139                 ; /***
 140              *** The test hangs on JDK 1.2.2 Classic VM if sleep() is invoked.
 141              ***
 142              try {
 143              this.sleep(1000);
 144              } catch (InterruptedException e) {
 145              e.printStackTrace(tester.out);
 146              return;
 147              };
 148              ***/
 149             tester.stop();
 150         }
 151     }
 152 }