1 /*
   2  * Copyright (c) 2014, 2016, 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  * @bug 6762191
  27  * @summary Setting stack size to 16K causes segmentation fault
  28  * @modules java.compiler
  29  * @compile TooSmallStackSize.java
  30  * @run main TooSmallStackSize
  31  */
  32 
  33 /*
  34  * The primary purpose of this test is to make sure we can run with a 16k stack
  35  * size without crashing. Also this test will determine the minimum allowed
  36  * stack size for the platform (as provided by the JVM error message when a very
  37  * small stack is used), and then verify that the JVM can be launched with that stack
  38  * size without a crash or any error messages.
  39  *
  40  * Note: The '-Xss<size>' and '-XX:ThreadStackSize=<k-bytes>' options
  41  * both control Java thread stack size. This repo's version of the test
  42  * exercises the '-Xss' option. The hotspot repo's version of the test
  43  * exercises the '-XX:ThreadStackSize' VM option.
  44  */
  45 
  46 public class TooSmallStackSize extends TestHelper {
  47     /* for debugging. Normally false. */
  48     static final boolean verbose = false;
  49 
  50     static void printTestOutput(TestResult tr) {
  51         System.out.println("*** exitValue = " + tr.exitValue);
  52         for (String x : tr.testOutput) {
  53             System.out.println(x);
  54         }
  55     }
  56 
  57     /*
  58      * Returns the minimum stack size this platform will allowed based on the
  59      * contents of the error message the JVM outputs when too small of a
  60      * -Xss size was used.
  61      *
  62      * The TestResult argument must contain the result of having already run
  63      * the JVM with too small of a stack size.
  64      */
  65     static String getMinStackAllowed(TestResult tr) {
  66         /*
  67          * The JVM output will contain in one of the lines:
  68          *   "The Java thread stack size specified is too small. Specify at least 100k"
  69          * Although the actual size will vary. We need to extract this size
  70          * string from the output and return it.
  71          */
  72         String matchStr = "Specify at least ";
  73         for (String x : tr.testOutput) {
  74             int match_idx = x.indexOf(matchStr);
  75             if (match_idx >= 0) {
  76                 int size_start_idx = match_idx + matchStr.length();
  77                 int k_start_idx = x.indexOf("k", size_start_idx);
  78                 return x.substring(size_start_idx, k_start_idx + 1); // include the "k"
  79             }
  80         }
  81 
  82         System.out.println("Expect='" + matchStr + "'");
  83         System.out.println("Actual:");
  84         printTestOutput(tr);
  85         System.out.println("FAILED: Could not get the stack size from the output");
  86         throw new RuntimeException("test fails");
  87     }
  88 
  89     /*
  90      * Run the JVM with the specified stack size.
  91      *
  92      * Returns the minimum allowed stack size gleaned from the error message,
  93      * if there is an error message. Otherwise returns the stack size passed in.
  94      */
  95     static String checkStack(String stackSize) {
  96         String min_stack_allowed;
  97 
  98         if (verbose)
  99             System.out.println("*** Testing " + stackSize);
 100         TestResult tr = doExec(javaCmd, "-Xss" + stackSize, "-version");
 101         if (verbose)
 102             printTestOutput(tr);
 103 
 104         if (tr.isOK()) {
 105             System.out.println("PASSED: got no error message with stack size of " + stackSize);
 106             min_stack_allowed = stackSize;
 107         } else {
 108             String matchStr = "The Java thread stack size specified is too small";
 109             if (tr.contains(matchStr)) {
 110                 System.out.println("PASSED: got expected error message with stack size of " + stackSize);
 111                 min_stack_allowed = getMinStackAllowed(tr);
 112             } else {
 113                 // Likely a crash
 114                 System.out.println("Expect='" + matchStr + "'");
 115                 System.out.println("Actual:");
 116                 printTestOutput(tr);
 117                 System.out.println("FAILED: Did not get expected error message with stack size of " + stackSize);
 118                 throw new RuntimeException("test fails");
 119             }
 120         }
 121 
 122         return min_stack_allowed;
 123     }
 124 
 125     /*
 126      * Run the JVM with the minimum allowed stack size. This should always succeed.
 127      */
 128     static void checkMinStackAllowed(String stackSize) {
 129         if (verbose)
 130             System.out.println("*** Testing " + stackSize);
 131         TestResult tr = doExec(javaCmd, "-Xss" + stackSize, "-version");
 132         if (verbose)
 133             printTestOutput(tr);
 134 
 135         if (tr.isOK()) {
 136             System.out.println("PASSED: VM launched with minimum allowed stack size of " + stackSize);
 137         } else {
 138             // Likely a crash
 139             System.out.println("Test output:");
 140             printTestOutput(tr);
 141             System.out.println("FAILED: VM failed to launch with minimum allowed stack size of " + stackSize);
 142             throw new RuntimeException("test fails");
 143         }
 144     }
 145 
 146     public static void main(String... args) {
 147         /*
 148          * The result of a 16k stack size should be a quick exit with a complaint
 149          * that the stack size is too small. However, for some win32 builds, the
 150          * stack is always at least 64k, and this also sometimes is the minimum
 151          * allowed size, so we won't see an error in this case.
 152          *
 153          * This test case will also produce a crash on some platforms if the fix
 154          * for 6762191 is not yet in place.
 155          */
 156         checkStack("16k");
 157 
 158         /*
 159          * Try with a 32k stack size, which is the size that the launcher will
 160          * set to if you try setting to anything smaller. This should produce the same
 161          * result as setting to 16k if the fix for 6762191 is in place.
 162          */
 163         String min_stack_allowed = checkStack("32k");
 164 
 165         /*
 166          * Try again with a the minimum stack size that was given in the error message
 167          */
 168         checkMinStackAllowed(min_stack_allowed);
 169     }
 170 }