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