8013726: runtime/memory/ReserveMemory.java fails due to 'assert(bytes % os::vm_allocation_granularity() == 0) failed: reserve block size'
authormikael
Tue, 21 May 2013 09:43:23 -0700
changeset 17824 24dc83d7c1d7
parent 17612 1b6801b0796c
child 17825 b6fc2755ec91
8013726: runtime/memory/ReserveMemory.java fails due to 'assert(bytes % os::vm_allocation_granularity() == 0) failed: reserve block size' Summary: Fix regression test to work on all platforms Reviewed-by: ctornqvi, dholmes
hotspot/src/share/vm/prims/whitebox.cpp
hotspot/test/runtime/memory/ReserveMemory.java
hotspot/test/testlibrary/whitebox/sun/hotspot/WhiteBox.java
--- a/hotspot/src/share/vm/prims/whitebox.cpp	Mon May 20 23:41:27 2013 +0400
+++ b/hotspot/src/share/vm/prims/whitebox.cpp	Tue May 21 09:43:23 2013 -0700
@@ -37,6 +37,7 @@
 #include "runtime/os.hpp"
 #include "utilities/debug.hpp"
 #include "utilities/macros.hpp"
+#include "utilities/exceptions.hpp"
 
 #if INCLUDE_ALL_GCS
 #include "gc_implementation/g1/concurrentMark.hpp"
@@ -330,8 +331,18 @@
 WB_END
 
 
-WB_ENTRY(jlong, WB_ReserveMemory(JNIEnv* env, jobject o, jlong size))
-  return (jlong)os::reserve_memory(size, NULL, 0);
+WB_ENTRY(void, WB_ReadReservedMemory(JNIEnv* env, jobject o))
+  // static+volatile in order to force the read to happen
+  // (not be eliminated by the compiler)
+  static char c;
+  static volatile char* p;
+
+  p = os::reserve_memory(os::vm_allocation_granularity(), NULL, 0);
+  if (p == NULL) {
+    THROW_MSG(vmSymbols::java_lang_OutOfMemoryError(), "Failed to reserve memory");
+  }
+
+  c = *p;
 WB_END
 
 //Some convenience methods to deal with objects from java
@@ -437,7 +448,7 @@
   {CC"isInStringTable",   CC"(Ljava/lang/String;)Z",  (void*)&WB_IsInStringTable  },
   {CC"fullGC",   CC"()V",                             (void*)&WB_FullGC },
 
-  {CC"reserveMemory", CC"(J)J", (void*)&WB_ReserveMemory },
+  {CC"readReservedMemory", CC"()V",                   (void*)&WB_ReadReservedMemory },
 };
 
 #undef CC
--- a/hotspot/test/runtime/memory/ReserveMemory.java	Mon May 20 23:41:27 2013 +0400
+++ b/hotspot/test/runtime/memory/ReserveMemory.java	Tue May 21 09:43:23 2013 -0700
@@ -34,29 +34,20 @@
 
 import com.oracle.java.testlibrary.*;
 
-import java.lang.reflect.Field;
 import sun.hotspot.WhiteBox;
-import sun.misc.Unsafe;
 
 public class ReserveMemory {
-  private static Unsafe getUnsafe() throws Exception {
-    Field f = Unsafe.class.getDeclaredField("theUnsafe");
-    f.setAccessible(true);
-    return (Unsafe)f.get(null);
-  }
-
   private static boolean isWindows() {
     return System.getProperty("os.name").toLowerCase().startsWith("win");
   }
 
+  private static boolean isOsx() {
+    return System.getProperty("os.name").toLowerCase().startsWith("mac");
+  }
+
   public static void main(String args[]) throws Exception {
     if (args.length > 0) {
-      long address = WhiteBox.getWhiteBox().reserveMemory(4096);
-
-      System.out.println("Reserved memory at address: 0x" + Long.toHexString(address));
-      System.out.println("Will now read from the address, expecting a crash!");
-
-      int x = getUnsafe().getInt(address);
+      WhiteBox.getWhiteBox().readReservedMemory();
 
       throw new Exception("Read of reserved/uncommitted memory unexpectedly succeeded, expected crash!");
     }
@@ -71,6 +62,8 @@
     OutputAnalyzer output = new OutputAnalyzer(pb.start());
     if (isWindows()) {
       output.shouldContain("EXCEPTION_ACCESS_VIOLATION");
+    } else if (isOsx()) {
+      output.shouldContain("SIGBUS");
     } else {
       output.shouldContain("SIGSEGV");
     }
--- a/hotspot/test/testlibrary/whitebox/sun/hotspot/WhiteBox.java	Mon May 20 23:41:27 2013 +0400
+++ b/hotspot/test/testlibrary/whitebox/sun/hotspot/WhiteBox.java	Tue May 21 09:43:23 2013 -0700
@@ -115,7 +115,7 @@
   public native boolean isInStringTable(String str);
 
   // Memory
-  public native long reserveMemory(long size);
+  public native void readReservedMemory();
 
   // force Full GC
   public native void fullGC();