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