hotspot/test/runtime/memory/ReserveMemory.java
changeset 17083 14000894ef39
child 17824 24dc83d7c1d7
equal deleted inserted replaced
17082:cfd184759531 17083:14000894ef39
       
     1 /*
       
     2  * Copyright (c) 2013, 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
       
    26  * @key regression
       
    27  * @bug 8012015
       
    28  * @summary Make sure reserved (but uncommitted) memory is not accessible
       
    29  * @library /testlibrary /testlibrary/whitebox
       
    30  * @build ReserveMemory
       
    31  * @run main ClassFileInstaller sun.hotspot.WhiteBox
       
    32  * @run main ReserveMemory
       
    33  */
       
    34 
       
    35 import com.oracle.java.testlibrary.*;
       
    36 
       
    37 import java.lang.reflect.Field;
       
    38 import sun.hotspot.WhiteBox;
       
    39 import sun.misc.Unsafe;
       
    40 
       
    41 public class ReserveMemory {
       
    42   private static Unsafe getUnsafe() throws Exception {
       
    43     Field f = Unsafe.class.getDeclaredField("theUnsafe");
       
    44     f.setAccessible(true);
       
    45     return (Unsafe)f.get(null);
       
    46   }
       
    47 
       
    48   private static boolean isWindows() {
       
    49     return System.getProperty("os.name").toLowerCase().startsWith("win");
       
    50   }
       
    51 
       
    52   public static void main(String args[]) throws Exception {
       
    53     if (args.length > 0) {
       
    54       long address = WhiteBox.getWhiteBox().reserveMemory(4096);
       
    55 
       
    56       System.out.println("Reserved memory at address: 0x" + Long.toHexString(address));
       
    57       System.out.println("Will now read from the address, expecting a crash!");
       
    58 
       
    59       int x = getUnsafe().getInt(address);
       
    60 
       
    61       throw new Exception("Read of reserved/uncommitted memory unexpectedly succeeded, expected crash!");
       
    62     }
       
    63 
       
    64     ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(
       
    65           "-Xbootclasspath/a:.",
       
    66           "-XX:+UnlockDiagnosticVMOptions",
       
    67           "-XX:+WhiteBoxAPI",
       
    68           "ReserveMemory",
       
    69           "test");
       
    70 
       
    71     OutputAnalyzer output = new OutputAnalyzer(pb.start());
       
    72     if (isWindows()) {
       
    73       output.shouldContain("EXCEPTION_ACCESS_VIOLATION");
       
    74     } else {
       
    75       output.shouldContain("SIGSEGV");
       
    76     }
       
    77   }
       
    78 }