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 import jdk.test.lib.Platform;
  47 
  48 public class WBStackSize {
  49 
  50     static final long K = 1024;
  51 
  52     static final long MIN_STACK_SIZE = 8 * K;
  53     static final long MAX_STACK_SIZE_ALLOCATED_IN_MAIN = 150 * K; // current value is about 130k on 64-bit platforms
  54 
  55     static final WhiteBox wb = WhiteBox.getWhiteBox();
  56 
  57     static long stackSizeOnOverflow = -1;
  58 
  59     static int eatAllStack() {
  60         return eatAllStack() * 2;
  61     }
  62 
  63     static void testStackOverflow() {
  64 
  65         stackSizeOnOverflow = wb.getThreadRemainingStackSize();
  66 
  67         if (stackSizeOnOverflow > MIN_STACK_SIZE) {
  68 
  69             try {
  70                 testStackOverflow();
  71             } catch (StackOverflowError e) {
  72                 // We caught SOE too early. The error will be reported in main()
  73             }
  74 
  75         } else {
  76 
  77             try {
  78                 eatAllStack();
  79                 throw new RuntimeException("Haven't caught StackOverflowError at all");
  80             } catch (StackOverflowError e) {
  81                 // OK: we caught the anticipated error
  82             }
  83         }
  84     }
  85 
  86     public static void main(String[] args) {
  87         long pageSize = wb.getVMPageSize();
  88 
  89         long configStackSize = wb.getIntxVMFlag("ThreadStackSize") * K;
  90         System.out.println("ThreadStackSize VM option: " + configStackSize);
  91 
  92         long stackProtectionSize = wb.getIntxVMFlag("StackShadowPages") * pageSize;
  93         System.out.println("Size of protected shadow pages: " + stackProtectionSize);
  94 
  95         long actualStackSize = wb.getThreadStackSize();
  96         System.out.println("Full stack size: " + actualStackSize);
  97 
  98         if (!Platform.isAix()) {
  99             if (Math.abs(actualStackSize - configStackSize) > configStackSize * 0.1) {
 100                 throw new RuntimeException("getThreadStackSize value [" + actualStackSize
 101                                            + "] should be within 90%..110% of ThreadStackSize value");
 102             }
 103         } else {
 104             // AIX pthread implementation returns stacks with sizes varying a lot.
 105             // We add +64K to assure stacks are not too small, thus we get
 106             // even more variation to bigger sizes. So only check the lower bound.
 107             // Allow for at least one page deviation.
 108             long slack = (long)(configStackSize * 0.1);
 109             if (slack < pageSize) {
 110                 if (configStackSize - actualStackSize > pageSize) {
 111                     throw new RuntimeException("getThreadStackSize value [" + actualStackSize
 112                                                + "] should not be more than one page smaller than "
 113                                                + "ThreadStackSize value");
 114                 }
 115             } else {
 116                 if (configStackSize - actualStackSize > slack) {
 117                     throw new RuntimeException("getThreadStackSize value [" + actualStackSize
 118                                                + "] should not be less than 90% of ThreadStackSize value");
 119                 }
 120             }
 121         }
 122 
 123         long remainingStackSize = wb.getThreadRemainingStackSize();
 124         System.out.println("Remaining stack size in main(): " + remainingStackSize);
 125 
 126         // Up to 150k can be already allocated by VM and some space is used for stack protection.
 127         long spaceAlreadyOccupied = MAX_STACK_SIZE_ALLOCATED_IN_MAIN + stackProtectionSize;
 128 
 129         if (remainingStackSize > configStackSize
 130             || (configStackSize > spaceAlreadyOccupied
 131                 && remainingStackSize < configStackSize - spaceAlreadyOccupied)) {
 132 
 133             throw new RuntimeException("getThreadRemainingStackSize value [" + remainingStackSize
 134                                      + "] should be at least ThreadStackSize value [" + configStackSize + "] minus ["
 135                                      + spaceAlreadyOccupied + "]");
 136         }
 137 
 138         testStackOverflow();
 139 
 140         if (stackSizeOnOverflow > MIN_STACK_SIZE) {
 141             throw new RuntimeException("Caught StackOverflowError too early: when there were "
 142                                      + stackSizeOnOverflow + " bytes in stack");
 143         } else if (stackSizeOnOverflow < 0) {
 144             throw new RuntimeException("Internal test error: stackRemainingSize < 0");
 145         } else {
 146             System.out.println("Caught StackOverflowError as expected");
 147         }
 148     }
 149 }