Path: blob/master/test/jdk/tools/launcher/TooSmallStackSize.java
41144 views
/*1* Copyright (c) 2014, 2019, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 6762191 822233426* @summary Setting stack size to 16K causes segmentation fault27* @compile TooSmallStackSize.java28* @run main TooSmallStackSize29*/3031/*32* The primary purpose of this test is to make sure we can run with a 16k stack33* size without crashing. Also this test will determine the minimum allowed34* stack size for the platform (as provided by the JVM error message when a very35* small stack is used), and then verify that the JVM can be launched with that stack36* size without a crash or any error messages.37*38* Note: The '-Xss<size>' and '-XX:ThreadStackSize=<k-bytes>' options39* both control Java thread stack size. This repo's version of the test40* exercises the '-Xss' option. The hotspot repo's version of the test41* exercises the '-XX:ThreadStackSize' VM option.42*/4344public class TooSmallStackSize extends TestHelper {45/* for debugging. Normally false. */46static final boolean verbose = false;4748static void printTestOutput(TestResult tr) {49System.out.println("*** exitValue = " + tr.exitValue);50for (String x : tr.testOutput) {51System.out.println(x);52}53}5455/*56* Returns the minimum stack size this platform will allowed based on the57* contents of the error message the JVM outputs when too small of a58* -Xss size was used.59*60* The TestResult argument must contain the result of having already run61* the JVM with too small of a stack size.62*/63static String getMinStackAllowed(TestResult tr) {64/*65* The JVM output will contain in one of the lines:66* "The Java thread stack size specified is too small. Specify at least 100k"67* Although the actual size will vary. We need to extract this size68* string from the output and return it.69*/70String matchStr = "Specify at least ";71for (String x : tr.testOutput) {72int match_idx = x.indexOf(matchStr);73if (match_idx >= 0) {74int size_start_idx = match_idx + matchStr.length();75int k_start_idx = x.indexOf("k", size_start_idx);76return x.substring(size_start_idx, k_start_idx + 1); // include the "k"77}78}7980System.out.println("Expect='" + matchStr + "'");81System.out.println("Actual:");82printTestOutput(tr);83System.out.println("FAILED: Could not get the stack size from the output");84throw new RuntimeException("test fails");85}8687/*88* Run the JVM with the specified stack size.89*90* Returns the minimum allowed stack size gleaned from the error message,91* if there is an error message. Otherwise returns the stack size passed in.92*/93static String checkStack(String stackSize) {94String min_stack_allowed;9596if (verbose)97System.out.println("*** Testing " + stackSize);98TestResult tr = doExec(javaCmd, "-Xss" + stackSize, "-version");99if (verbose)100printTestOutput(tr);101102if (tr.isOK()) {103System.out.println("PASSED: got no error message with stack size of " + stackSize);104min_stack_allowed = stackSize;105} else {106String matchStr = "The Java thread stack size specified is too small";107if (tr.contains(matchStr)) {108System.out.println("PASSED: got expected error message with stack size of " + stackSize);109min_stack_allowed = getMinStackAllowed(tr);110} else {111// Likely a crash112System.out.println("Expect='" + matchStr + "'");113System.out.println("Actual:");114printTestOutput(tr);115System.out.println("FAILED: Did not get expected error message with stack size of " + stackSize);116throw new RuntimeException("test fails");117}118}119120return min_stack_allowed;121}122123/*124* Run the JVM with the minimum allowed stack size. This should always succeed.125*/126static void checkMinStackAllowed(String stackSize) {127if (verbose)128System.out.println("*** Testing " + stackSize);129TestResult tr = doExec(javaCmd, "-Xss" + stackSize, "-version");130if (verbose)131printTestOutput(tr);132133if (tr.isOK()) {134System.out.println("PASSED: VM launched with minimum allowed stack size of " + stackSize);135} else {136// Likely a crash137System.out.println("Test output:");138printTestOutput(tr);139System.out.println("FAILED: VM failed to launch with minimum allowed stack size of " + stackSize);140throw new RuntimeException("test fails");141}142}143144public static void main(String... args) {145/*146* The result of a 16k stack size should be a quick exit with a complaint147* that the stack size is too small. However, for some win32 builds, the148* stack is always at least 64k, and this also sometimes is the minimum149* allowed size, so we won't see an error in this case.150*151* This test case will also produce a crash on some platforms if the fix152* for 6762191 is not yet in place.153*/154checkStack("16k");155156/*157* Try with a 64k stack size, which is the size that the launcher will158* set to if you try setting to anything smaller. This should produce the same159* result as setting to 16k if the fix for 6762191 is in place.160*/161String min_stack_allowed = checkStack("64k");162163/*164* Try again with a the minimum stack size that was given in the error message165*/166checkMinStackAllowed(min_stack_allowed);167168/*169* Try again with a size that is not OS page aligned. This is to help test that170* asserts added for 8176768 are not triggered.171*/172checkMinStackAllowed("513k");173174/*175* Try with 0k which indicates that the default thread stack size either from JVM or system176* will be used, this should always succeed.177*/178checkMinStackAllowed("0k");179}180}181182183