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 WBStackSize
  26  * @summary verify that whitebox functions getThreadFullStackSize() and getThreadRemainingStackSize are working
  27  * @modules java.base/jdk.internal.misc
  28  * @library /test/lib
  29  * @build sun.hotspot.WhiteBox
  30  * @run driver ClassFileInstaller sun.hotspot.WhiteBox
  31  *                              sun.hotspot.WhiteBox$WhiteBoxPermission
  32  * @run main/othervm -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI -Xss512k WBStackSize
  33  */
  34 
  35 /*
  36  * The test may product a false failure if too big StackYellowPages/StackRedPages/ShackShadowPages
  37  * VM options are specified. The proper test would retrieve the page size from VM and account for these options
  38  * instead of check below:
  39  *     Math.abs(actualStackSize - configStackSize) > configStackSize * 0.1
  40  *
  41  * Please file a test bug, if this is a problem.
  42  */
  43 
  44 import sun.hotspot.WhiteBox;
  45 
  46 public class WBStackSize {
  47 
  48     static final long K = 1024;
  49 
  50     static final long MIN_STACK_SIZE = 8 * K;
  51     static final long MAX_STACK_SIZE_ALLOCATED_IN_MAIN = 150 * K; // current value is about 130k on 64-bit platforms
  52 
  53     static final WhiteBox wb = WhiteBox.getWhiteBox();
  54 
  55     static long stackSizeOnOverflow = -1;
  56 
  57     static int eatAllStack() {
  58         return eatAllStack() * 2;
  59     }
  60 
  61     static void testStackOverflow() {
  62 
  63         stackSizeOnOverflow = wb.getThreadRemainingStackSize();
  64 
  65         if (stackSizeOnOverflow > MIN_STACK_SIZE) {
  66 
  67             try {
  68                 testStackOverflow();
  69             } catch (StackOverflowError e) {
  70                 // We caught SOE too early. The error will be reported in main()
  71             }
  72 
  73         } else {
  74 
  75             try {
  76                 eatAllStack();
  77                 throw new RuntimeException("Haven't caught StackOverflowError at all");
  78             } catch (StackOverflowError e) {
  79                 // OK: we caught the anticipated error
  80             }
  81         }
  82     }
  83 
  84     public static void main(String[] args) {
  85         long configStackSize = wb.getIntxVMFlag("ThreadStackSize") * K;
  86         System.out.println("ThreadStackSize VM option: " + configStackSize);
  87 
  88         long stackProtectionSize = wb.getIntxVMFlag("StackShadowPages") * wb.getVMPageSize();
  89         System.out.println("Size of protected shadow pages: " + stackProtectionSize);
  90 
  91         long actualStackSize = wb.getThreadStackSize();
  92         System.out.println("Full stack size: " + actualStackSize);
  93 
  94         if (Math.abs(actualStackSize - configStackSize) > configStackSize * 0.1) {
  95             throw new RuntimeException("getThreadFullStackSize value [" + actualStackSize
  96                                      + "] should be within 90%..110% of the value returned by HotSpotDiagnosticMXBean");
  97         }
  98 
  99         long remainingStackSize = wb.getThreadRemainingStackSize();
 100         System.out.println("Remaining stack size in main(): " + remainingStackSize);
 101 
 102         // Up to 150k can be already allocated by VM and some space is used for stack protection.
 103         long spaceAlreadyOccupied = MAX_STACK_SIZE_ALLOCATED_IN_MAIN + stackProtectionSize;
 104 
 105         if (remainingStackSize > configStackSize
 106             || (configStackSize > spaceAlreadyOccupied
 107                 && remainingStackSize < configStackSize - spaceAlreadyOccupied)) {
 108 
 109             throw new RuntimeException("getThreadRemainingStackSize value [" + remainingStackSize
 110                                      + "] should be at least ThreadStackSize value [" + configStackSize + "] minus ["
 111                                      + spaceAlreadyOccupied + "]");
 112         }
 113 
 114         testStackOverflow();
 115 
 116         if (stackSizeOnOverflow > MIN_STACK_SIZE) {
 117             throw new RuntimeException("Caught StackOverflowError too early: when there were "
 118                                      + stackSizeOnOverflow + " bytes in stack");
 119         } else if (stackSizeOnOverflow < 0) {
 120             throw new RuntimeException("Internal test error: stackRemainingSize < 0");
 121         } else {
 122             System.out.println("Caught StackOverflowError as expected");
 123         }
 124     }
 125 }