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( |