hotspot/test/runtime/memory/ReserveMemory.java
changeset 17824 24dc83d7c1d7
parent 17083 14000894ef39
child 20390 a86c9ed78205
equal deleted inserted replaced
17612:1b6801b0796c 17824:24dc83d7c1d7
    32  * @run main ReserveMemory
    32  * @run main ReserveMemory
    33  */
    33  */
    34 
    34 
    35 import com.oracle.java.testlibrary.*;
    35 import com.oracle.java.testlibrary.*;
    36 
    36 
    37 import java.lang.reflect.Field;
       
    38 import sun.hotspot.WhiteBox;
    37 import sun.hotspot.WhiteBox;
    39 import sun.misc.Unsafe;
       
    40 
    38 
    41 public class ReserveMemory {
    39 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() {
    40   private static boolean isWindows() {
    49     return System.getProperty("os.name").toLowerCase().startsWith("win");
    41     return System.getProperty("os.name").toLowerCase().startsWith("win");
    50   }
    42   }
    51 
    43 
       
    44   private static boolean isOsx() {
       
    45     return System.getProperty("os.name").toLowerCase().startsWith("mac");
       
    46   }
       
    47 
    52   public static void main(String args[]) throws Exception {
    48   public static void main(String args[]) throws Exception {
    53     if (args.length > 0) {
    49     if (args.length > 0) {
    54       long address = WhiteBox.getWhiteBox().reserveMemory(4096);
    50       WhiteBox.getWhiteBox().readReservedMemory();
    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 
    51 
    61       throw new Exception("Read of reserved/uncommitted memory unexpectedly succeeded, expected crash!");
    52       throw new Exception("Read of reserved/uncommitted memory unexpectedly succeeded, expected crash!");
    62     }
    53     }
    63 
    54 
    64     ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(
    55     ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(
    69           "test");
    60           "test");
    70 
    61 
    71     OutputAnalyzer output = new OutputAnalyzer(pb.start());
    62     OutputAnalyzer output = new OutputAnalyzer(pb.start());
    72     if (isWindows()) {
    63     if (isWindows()) {
    73       output.shouldContain("EXCEPTION_ACCESS_VIOLATION");
    64       output.shouldContain("EXCEPTION_ACCESS_VIOLATION");
       
    65     } else if (isOsx()) {
       
    66       output.shouldContain("SIGBUS");
    74     } else {
    67     } else {
    75       output.shouldContain("SIGSEGV");
    68       output.shouldContain("SIGSEGV");
    76     }
    69     }
    77   }
    70   }
    78 }
    71 }