hotspot/test/runtime/whitebox/WBStackSize.java
author ctornqvi
Fri, 19 Aug 2016 10:06:30 -0400
changeset 40631 ed82623d7831
parent 38152 80e5da81fb2c
permissions -rw-r--r--
8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder Reviewed-by: coleenp, gtriantafill, mseledtsov, iignatyev, dholmes, dsamersoff
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
24671
0ebd6fe70309 8032970: Add stack size check methods to WhiteBox API
iignatyev
parents:
diff changeset
     1
/*
40631
ed82623d7831 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents: 38152
diff changeset
     2
 * Copyright (c) 2014, 2016, Oracle and/or its affiliates. All rights reserved.
24671
0ebd6fe70309 8032970: Add stack size check methods to WhiteBox API
iignatyev
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0ebd6fe70309 8032970: Add stack size check methods to WhiteBox API
iignatyev
parents:
diff changeset
     4
 *
0ebd6fe70309 8032970: Add stack size check methods to WhiteBox API
iignatyev
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
0ebd6fe70309 8032970: Add stack size check methods to WhiteBox API
iignatyev
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
0ebd6fe70309 8032970: Add stack size check methods to WhiteBox API
iignatyev
parents:
diff changeset
     7
 * published by the Free Software Foundation.
0ebd6fe70309 8032970: Add stack size check methods to WhiteBox API
iignatyev
parents:
diff changeset
     8
 *
0ebd6fe70309 8032970: Add stack size check methods to WhiteBox API
iignatyev
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
0ebd6fe70309 8032970: Add stack size check methods to WhiteBox API
iignatyev
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0ebd6fe70309 8032970: Add stack size check methods to WhiteBox API
iignatyev
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
0ebd6fe70309 8032970: Add stack size check methods to WhiteBox API
iignatyev
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
0ebd6fe70309 8032970: Add stack size check methods to WhiteBox API
iignatyev
parents:
diff changeset
    13
 * accompanied this code).
0ebd6fe70309 8032970: Add stack size check methods to WhiteBox API
iignatyev
parents:
diff changeset
    14
 *
0ebd6fe70309 8032970: Add stack size check methods to WhiteBox API
iignatyev
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
0ebd6fe70309 8032970: Add stack size check methods to WhiteBox API
iignatyev
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
0ebd6fe70309 8032970: Add stack size check methods to WhiteBox API
iignatyev
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0ebd6fe70309 8032970: Add stack size check methods to WhiteBox API
iignatyev
parents:
diff changeset
    18
 *
0ebd6fe70309 8032970: Add stack size check methods to WhiteBox API
iignatyev
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
0ebd6fe70309 8032970: Add stack size check methods to WhiteBox API
iignatyev
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
0ebd6fe70309 8032970: Add stack size check methods to WhiteBox API
iignatyev
parents:
diff changeset
    21
 * questions.
0ebd6fe70309 8032970: Add stack size check methods to WhiteBox API
iignatyev
parents:
diff changeset
    22
 */
0ebd6fe70309 8032970: Add stack size check methods to WhiteBox API
iignatyev
parents:
diff changeset
    23
0ebd6fe70309 8032970: Add stack size check methods to WhiteBox API
iignatyev
parents:
diff changeset
    24
/*
0ebd6fe70309 8032970: Add stack size check methods to WhiteBox API
iignatyev
parents:
diff changeset
    25
 * @test WBStackSize
0ebd6fe70309 8032970: Add stack size check methods to WhiteBox API
iignatyev
parents:
diff changeset
    26
 * @summary verify that whitebox functions getThreadFullStackSize() and getThreadRemainingStackSize are working
38152
80e5da81fb2c 8154258: [TESTBUG] Various serviceability tests fail compilation
dsamersoff
parents: 33730
diff changeset
    27
 * @modules java.base/jdk.internal.misc
40631
ed82623d7831 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents: 38152
diff changeset
    28
 * @library /test/lib
ed82623d7831 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents: 38152
diff changeset
    29
 * @build sun.hotspot.WhiteBox
24671
0ebd6fe70309 8032970: Add stack size check methods to WhiteBox API
iignatyev
parents:
diff changeset
    30
 * @run main ClassFileInstaller sun.hotspot.WhiteBox
25958
8dc85547d6d6 8011397: JTREG needs to copy additional WhiteBox class file to JTwork/scratch/sun/hotspot
mgerdin
parents: 25736
diff changeset
    31
 *                              sun.hotspot.WhiteBox$WhiteBoxPermission
24671
0ebd6fe70309 8032970: Add stack size check methods to WhiteBox API
iignatyev
parents:
diff changeset
    32
 * @run main/othervm -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI -Xss512k WBStackSize
0ebd6fe70309 8032970: Add stack size check methods to WhiteBox API
iignatyev
parents:
diff changeset
    33
 */
0ebd6fe70309 8032970: Add stack size check methods to WhiteBox API
iignatyev
parents:
diff changeset
    34
0ebd6fe70309 8032970: Add stack size check methods to WhiteBox API
iignatyev
parents:
diff changeset
    35
/*
0ebd6fe70309 8032970: Add stack size check methods to WhiteBox API
iignatyev
parents:
diff changeset
    36
 * The test may product a false failure if too big StackYellowPages/StackRedPages/ShackShadowPages
0ebd6fe70309 8032970: Add stack size check methods to WhiteBox API
iignatyev
parents:
diff changeset
    37
 * VM options are specified. The proper test would retrieve the page size from VM and account for these options
0ebd6fe70309 8032970: Add stack size check methods to WhiteBox API
iignatyev
parents:
diff changeset
    38
 * instead of check below:
0ebd6fe70309 8032970: Add stack size check methods to WhiteBox API
iignatyev
parents:
diff changeset
    39
 *     Math.abs(actualStackSize - configStackSize) > configStackSize * 0.1
0ebd6fe70309 8032970: Add stack size check methods to WhiteBox API
iignatyev
parents:
diff changeset
    40
 *
0ebd6fe70309 8032970: Add stack size check methods to WhiteBox API
iignatyev
parents:
diff changeset
    41
 * Please file a test bug, if this is a problem.
0ebd6fe70309 8032970: Add stack size check methods to WhiteBox API
iignatyev
parents:
diff changeset
    42
 */
0ebd6fe70309 8032970: Add stack size check methods to WhiteBox API
iignatyev
parents:
diff changeset
    43
0ebd6fe70309 8032970: Add stack size check methods to WhiteBox API
iignatyev
parents:
diff changeset
    44
import sun.hotspot.WhiteBox;
0ebd6fe70309 8032970: Add stack size check methods to WhiteBox API
iignatyev
parents:
diff changeset
    45
0ebd6fe70309 8032970: Add stack size check methods to WhiteBox API
iignatyev
parents:
diff changeset
    46
public class WBStackSize {
0ebd6fe70309 8032970: Add stack size check methods to WhiteBox API
iignatyev
parents:
diff changeset
    47
0ebd6fe70309 8032970: Add stack size check methods to WhiteBox API
iignatyev
parents:
diff changeset
    48
    static final long K = 1024;
0ebd6fe70309 8032970: Add stack size check methods to WhiteBox API
iignatyev
parents:
diff changeset
    49
0ebd6fe70309 8032970: Add stack size check methods to WhiteBox API
iignatyev
parents:
diff changeset
    50
    static final long MIN_STACK_SIZE = 8 * K;
28497
a7aecf0ffb6b 8067941: [TESTBUG] Fix tests for OS with 64K page size.
goetz
parents: 28190
diff changeset
    51
    static final long MAX_STACK_SIZE_ALLOCATED_IN_MAIN = 150 * K; // current value is about 130k on 64-bit platforms
24671
0ebd6fe70309 8032970: Add stack size check methods to WhiteBox API
iignatyev
parents:
diff changeset
    52
0ebd6fe70309 8032970: Add stack size check methods to WhiteBox API
iignatyev
parents:
diff changeset
    53
    static final WhiteBox wb = WhiteBox.getWhiteBox();
0ebd6fe70309 8032970: Add stack size check methods to WhiteBox API
iignatyev
parents:
diff changeset
    54
0ebd6fe70309 8032970: Add stack size check methods to WhiteBox API
iignatyev
parents:
diff changeset
    55
    static long stackSizeOnOverflow = -1;
0ebd6fe70309 8032970: Add stack size check methods to WhiteBox API
iignatyev
parents:
diff changeset
    56
0ebd6fe70309 8032970: Add stack size check methods to WhiteBox API
iignatyev
parents:
diff changeset
    57
    static int eatAllStack() {
0ebd6fe70309 8032970: Add stack size check methods to WhiteBox API
iignatyev
parents:
diff changeset
    58
        return eatAllStack() * 2;
0ebd6fe70309 8032970: Add stack size check methods to WhiteBox API
iignatyev
parents:
diff changeset
    59
    }
0ebd6fe70309 8032970: Add stack size check methods to WhiteBox API
iignatyev
parents:
diff changeset
    60
0ebd6fe70309 8032970: Add stack size check methods to WhiteBox API
iignatyev
parents:
diff changeset
    61
    static void testStackOverflow() {
0ebd6fe70309 8032970: Add stack size check methods to WhiteBox API
iignatyev
parents:
diff changeset
    62
0ebd6fe70309 8032970: Add stack size check methods to WhiteBox API
iignatyev
parents:
diff changeset
    63
        stackSizeOnOverflow = wb.getThreadRemainingStackSize();
0ebd6fe70309 8032970: Add stack size check methods to WhiteBox API
iignatyev
parents:
diff changeset
    64
0ebd6fe70309 8032970: Add stack size check methods to WhiteBox API
iignatyev
parents:
diff changeset
    65
        if (stackSizeOnOverflow > MIN_STACK_SIZE) {
0ebd6fe70309 8032970: Add stack size check methods to WhiteBox API
iignatyev
parents:
diff changeset
    66
0ebd6fe70309 8032970: Add stack size check methods to WhiteBox API
iignatyev
parents:
diff changeset
    67
            try {
0ebd6fe70309 8032970: Add stack size check methods to WhiteBox API
iignatyev
parents:
diff changeset
    68
                testStackOverflow();
0ebd6fe70309 8032970: Add stack size check methods to WhiteBox API
iignatyev
parents:
diff changeset
    69
            } catch (StackOverflowError e) {
0ebd6fe70309 8032970: Add stack size check methods to WhiteBox API
iignatyev
parents:
diff changeset
    70
                // We caught SOE too early. The error will be reported in main()
0ebd6fe70309 8032970: Add stack size check methods to WhiteBox API
iignatyev
parents:
diff changeset
    71
            }
0ebd6fe70309 8032970: Add stack size check methods to WhiteBox API
iignatyev
parents:
diff changeset
    72
0ebd6fe70309 8032970: Add stack size check methods to WhiteBox API
iignatyev
parents:
diff changeset
    73
        } else {
0ebd6fe70309 8032970: Add stack size check methods to WhiteBox API
iignatyev
parents:
diff changeset
    74
0ebd6fe70309 8032970: Add stack size check methods to WhiteBox API
iignatyev
parents:
diff changeset
    75
            try {
0ebd6fe70309 8032970: Add stack size check methods to WhiteBox API
iignatyev
parents:
diff changeset
    76
                eatAllStack();
0ebd6fe70309 8032970: Add stack size check methods to WhiteBox API
iignatyev
parents:
diff changeset
    77
                throw new RuntimeException("Haven't caught StackOverflowError at all");
0ebd6fe70309 8032970: Add stack size check methods to WhiteBox API
iignatyev
parents:
diff changeset
    78
            } catch (StackOverflowError e) {
0ebd6fe70309 8032970: Add stack size check methods to WhiteBox API
iignatyev
parents:
diff changeset
    79
                // OK: we caught the anticipated error
0ebd6fe70309 8032970: Add stack size check methods to WhiteBox API
iignatyev
parents:
diff changeset
    80
            }
0ebd6fe70309 8032970: Add stack size check methods to WhiteBox API
iignatyev
parents:
diff changeset
    81
        }
0ebd6fe70309 8032970: Add stack size check methods to WhiteBox API
iignatyev
parents:
diff changeset
    82
    }
0ebd6fe70309 8032970: Add stack size check methods to WhiteBox API
iignatyev
parents:
diff changeset
    83
0ebd6fe70309 8032970: Add stack size check methods to WhiteBox API
iignatyev
parents:
diff changeset
    84
    public static void main(String[] args) {
25736
6f35dbe32581 8032449: Get rid of JMX in test/compiler
iignatyev
parents: 24671
diff changeset
    85
        long configStackSize = wb.getIntxVMFlag("ThreadStackSize") * K;
28497
a7aecf0ffb6b 8067941: [TESTBUG] Fix tests for OS with 64K page size.
goetz
parents: 28190
diff changeset
    86
        System.out.println("ThreadStackSize VM option: " + configStackSize);
24671
0ebd6fe70309 8032970: Add stack size check methods to WhiteBox API
iignatyev
parents:
diff changeset
    87
28497
a7aecf0ffb6b 8067941: [TESTBUG] Fix tests for OS with 64K page size.
goetz
parents: 28190
diff changeset
    88
        long stackProtectionSize = wb.getIntxVMFlag("StackShadowPages") * wb.getVMPageSize();
a7aecf0ffb6b 8067941: [TESTBUG] Fix tests for OS with 64K page size.
goetz
parents: 28190
diff changeset
    89
        System.out.println("Size of protected shadow pages: " + stackProtectionSize);
24671
0ebd6fe70309 8032970: Add stack size check methods to WhiteBox API
iignatyev
parents:
diff changeset
    90
0ebd6fe70309 8032970: Add stack size check methods to WhiteBox API
iignatyev
parents:
diff changeset
    91
        long actualStackSize = wb.getThreadStackSize();
0ebd6fe70309 8032970: Add stack size check methods to WhiteBox API
iignatyev
parents:
diff changeset
    92
        System.out.println("Full stack size: " + actualStackSize);
0ebd6fe70309 8032970: Add stack size check methods to WhiteBox API
iignatyev
parents:
diff changeset
    93
0ebd6fe70309 8032970: Add stack size check methods to WhiteBox API
iignatyev
parents:
diff changeset
    94
        if (Math.abs(actualStackSize - configStackSize) > configStackSize * 0.1) {
0ebd6fe70309 8032970: Add stack size check methods to WhiteBox API
iignatyev
parents:
diff changeset
    95
            throw new RuntimeException("getThreadFullStackSize value [" + actualStackSize
0ebd6fe70309 8032970: Add stack size check methods to WhiteBox API
iignatyev
parents:
diff changeset
    96
                                     + "] should be within 90%..110% of the value returned by HotSpotDiagnosticMXBean");
0ebd6fe70309 8032970: Add stack size check methods to WhiteBox API
iignatyev
parents:
diff changeset
    97
        }
0ebd6fe70309 8032970: Add stack size check methods to WhiteBox API
iignatyev
parents:
diff changeset
    98
0ebd6fe70309 8032970: Add stack size check methods to WhiteBox API
iignatyev
parents:
diff changeset
    99
        long remainingStackSize = wb.getThreadRemainingStackSize();
0ebd6fe70309 8032970: Add stack size check methods to WhiteBox API
iignatyev
parents:
diff changeset
   100
        System.out.println("Remaining stack size in main(): " + remainingStackSize);
0ebd6fe70309 8032970: Add stack size check methods to WhiteBox API
iignatyev
parents:
diff changeset
   101
28497
a7aecf0ffb6b 8067941: [TESTBUG] Fix tests for OS with 64K page size.
goetz
parents: 28190
diff changeset
   102
        // Up to 150k can be already allocated by VM and some space is used for stack protection.
a7aecf0ffb6b 8067941: [TESTBUG] Fix tests for OS with 64K page size.
goetz
parents: 28190
diff changeset
   103
        long spaceAlreadyOccupied = MAX_STACK_SIZE_ALLOCATED_IN_MAIN + stackProtectionSize;
a7aecf0ffb6b 8067941: [TESTBUG] Fix tests for OS with 64K page size.
goetz
parents: 28190
diff changeset
   104
24671
0ebd6fe70309 8032970: Add stack size check methods to WhiteBox API
iignatyev
parents:
diff changeset
   105
        if (remainingStackSize > configStackSize
28497
a7aecf0ffb6b 8067941: [TESTBUG] Fix tests for OS with 64K page size.
goetz
parents: 28190
diff changeset
   106
            || (configStackSize > spaceAlreadyOccupied
a7aecf0ffb6b 8067941: [TESTBUG] Fix tests for OS with 64K page size.
goetz
parents: 28190
diff changeset
   107
                && remainingStackSize < configStackSize - spaceAlreadyOccupied)) {
24671
0ebd6fe70309 8032970: Add stack size check methods to WhiteBox API
iignatyev
parents:
diff changeset
   108
0ebd6fe70309 8032970: Add stack size check methods to WhiteBox API
iignatyev
parents:
diff changeset
   109
            throw new RuntimeException("getThreadRemainingStackSize value [" + remainingStackSize
0ebd6fe70309 8032970: Add stack size check methods to WhiteBox API
iignatyev
parents:
diff changeset
   110
                                     + "] should be at least ThreadStackSize value [" + configStackSize + "] minus ["
28497
a7aecf0ffb6b 8067941: [TESTBUG] Fix tests for OS with 64K page size.
goetz
parents: 28190
diff changeset
   111
                                     + spaceAlreadyOccupied + "]");
24671
0ebd6fe70309 8032970: Add stack size check methods to WhiteBox API
iignatyev
parents:
diff changeset
   112
        }
0ebd6fe70309 8032970: Add stack size check methods to WhiteBox API
iignatyev
parents:
diff changeset
   113
0ebd6fe70309 8032970: Add stack size check methods to WhiteBox API
iignatyev
parents:
diff changeset
   114
        testStackOverflow();
0ebd6fe70309 8032970: Add stack size check methods to WhiteBox API
iignatyev
parents:
diff changeset
   115
0ebd6fe70309 8032970: Add stack size check methods to WhiteBox API
iignatyev
parents:
diff changeset
   116
        if (stackSizeOnOverflow > MIN_STACK_SIZE) {
0ebd6fe70309 8032970: Add stack size check methods to WhiteBox API
iignatyev
parents:
diff changeset
   117
            throw new RuntimeException("Caught StackOverflowError too early: when there were "
0ebd6fe70309 8032970: Add stack size check methods to WhiteBox API
iignatyev
parents:
diff changeset
   118
                                     + stackSizeOnOverflow + " bytes in stack");
0ebd6fe70309 8032970: Add stack size check methods to WhiteBox API
iignatyev
parents:
diff changeset
   119
        } else if (stackSizeOnOverflow < 0) {
0ebd6fe70309 8032970: Add stack size check methods to WhiteBox API
iignatyev
parents:
diff changeset
   120
            throw new RuntimeException("Internal test error: stackRemainingSize < 0");
0ebd6fe70309 8032970: Add stack size check methods to WhiteBox API
iignatyev
parents:
diff changeset
   121
        } else {
0ebd6fe70309 8032970: Add stack size check methods to WhiteBox API
iignatyev
parents:
diff changeset
   122
            System.out.println("Caught StackOverflowError as expected");
0ebd6fe70309 8032970: Add stack size check methods to WhiteBox API
iignatyev
parents:
diff changeset
   123
        }
0ebd6fe70309 8032970: Add stack size check methods to WhiteBox API
iignatyev
parents:
diff changeset
   124
    }
0ebd6fe70309 8032970: Add stack size check methods to WhiteBox API
iignatyev
parents:
diff changeset
   125
}