8022853: add ability to load uncompressed object and Klass references in a compressed environment to Unsafe
authortwisti
Mon, 27 Apr 2015 09:02:41 -0700
changeset 30288 476c276de939
parent 30287 b39d2cec5056
child 30289 10b7b61d759a
child 30291 54cdc5c1a9cb
8022853: add ability to load uncompressed object and Klass references in a compressed environment to Unsafe Reviewed-by: coleenp, jrose, kvn
hotspot/src/share/vm/prims/unsafe.cpp
hotspot/test/runtime/Unsafe/GetKlassPointerGetJavaMirror.java
hotspot/test/runtime/Unsafe/GetUncompressedObject.java
--- a/hotspot/src/share/vm/prims/unsafe.cpp	Mon Apr 27 14:41:49 2015 +0200
+++ b/hotspot/src/share/vm/prims/unsafe.cpp	Mon Apr 27 09:02:41 2015 -0700
@@ -172,18 +172,6 @@
   oop p = JNIHandles::resolve(obj); \
   OrderAccess::release_store_fence((volatile type_name*)index_oop_from_field_offset_long(p, offset), x);
 
-// Macros for oops that check UseCompressedOops
-
-#define GET_OOP_FIELD(obj, offset, v) \
-  oop p = JNIHandles::resolve(obj);   \
-  oop v;                              \
-  if (UseCompressedOops) {            \
-    narrowOop n = *(narrowOop*)index_oop_from_field_offset_long(p, offset); \
-    v = oopDesc::decode_heap_oop(n);                                \
-  } else {                            \
-    v = *(oop*)index_oop_from_field_offset_long(p, offset);                 \
-  }
-
 
 // Get/SetObject must be special-cased, since it works with handles.
 
@@ -192,7 +180,14 @@
 // That is, it should be in the range [0, MAX_OBJECT_SIZE].
 UNSAFE_ENTRY(jobject, Unsafe_GetObject(JNIEnv *env, jobject unsafe, jobject obj, jlong offset))
   UnsafeWrapper("Unsafe_GetObject");
-  GET_OOP_FIELD(obj, offset, v)
+  oop p = JNIHandles::resolve(obj);
+  oop v;
+  if (UseCompressedOops) {
+    narrowOop n = *(narrowOop*)index_oop_from_field_offset_long(p, offset);
+    v = oopDesc::decode_heap_oop(n);
+  } else {
+    v = *(oop*)index_oop_from_field_offset_long(p, offset);
+  }
   jobject ret = JNIHandles::make_local(env, v);
 #if INCLUDE_ALL_GCS
   // We could be accessing the referent field in a reference
@@ -261,6 +256,25 @@
   OrderAccess::fence();
 UNSAFE_END
 
+UNSAFE_ENTRY(jobject, Unsafe_GetUncompressedObject(JNIEnv *env, jobject unsafe, jlong addr))
+  UnsafeWrapper("Unsafe_GetUncompressedObject");
+  oop v = *(oop*) (address) addr;
+  return JNIHandles::make_local(env, v);
+UNSAFE_END
+
+UNSAFE_ENTRY(jclass, Unsafe_GetJavaMirror(JNIEnv *env, jobject unsafe, jlong metaspace_klass))
+  UnsafeWrapper("Unsafe_GetJavaMirror");
+  Klass* klass = (Klass*) (address) metaspace_klass;
+  return (jclass) JNIHandles::make_local(klass->java_mirror());
+UNSAFE_END
+
+UNSAFE_ENTRY(jlong, Unsafe_GetKlassPointer(JNIEnv *env, jobject unsafe, jobject obj))
+  UnsafeWrapper("Unsafe_GetKlassPointer");
+  oop o = JNIHandles::resolve(obj);
+  jlong klass = (jlong) (address) o->klass();
+  return klass;
+UNSAFE_END
+
 #ifndef SUPPORTS_NATIVE_CX8
 
 // VM_Version::supports_cx8() is a surrogate for 'supports atomic long memory ops'.
@@ -1222,6 +1236,10 @@
     {CC"getObjectVolatile",CC"("OBJ"J)"OBJ"",   FN_PTR(Unsafe_GetObjectVolatile)},
     {CC"putObjectVolatile",CC"("OBJ"J"OBJ")V",  FN_PTR(Unsafe_SetObjectVolatile)},
 
+    {CC"getUncompressedObject", CC"("ADR")"OBJ, FN_PTR(Unsafe_GetUncompressedObject)},
+    {CC"getJavaMirror",         CC"("ADR")"CLS, FN_PTR(Unsafe_GetJavaMirror)},
+    {CC"getKlassPointer",       CC"("OBJ")"ADR, FN_PTR(Unsafe_GetKlassPointer)},
+
     DECLARE_GETPUTOOP(Boolean, Z),
     DECLARE_GETPUTOOP(Byte, B),
     DECLARE_GETPUTOOP(Short, S),
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/hotspot/test/runtime/Unsafe/GetKlassPointerGetJavaMirror.java	Mon Apr 27 09:02:41 2015 -0700
@@ -0,0 +1,46 @@
+/*
+ * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+/* @test
+ * @bug 8022853
+ * @library /testlibrary
+ * @modules java.base/sun.misc
+ * @run main GetKlassPointerGetJavaMirror
+ */
+
+import static com.oracle.java.testlibrary.Asserts.*;
+
+import com.oracle.java.testlibrary.*;
+import sun.misc.Unsafe;
+
+public class GetKlassPointerGetJavaMirror {
+
+    public static void main(String args[]) throws Exception {
+        Unsafe unsafe = Utils.getUnsafe();
+        Object o = new GetKlassPointerGetJavaMirror();
+        final long metaspaceKlass = unsafe.getKlassPointer(o);
+        Class<?> c = unsafe.getJavaMirror(metaspaceKlass);
+        assertEquals(o.getClass(), c);
+    }
+
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/hotspot/test/runtime/Unsafe/GetUncompressedObject.java	Mon Apr 27 09:02:41 2015 -0700
@@ -0,0 +1,56 @@
+/*
+ * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+/* @test
+ * @bug 8022853
+ * @library /testlibrary
+ * @modules java.base/sun.misc
+ * @run main GetUncompressedObject
+ */
+
+import static com.oracle.java.testlibrary.Asserts.*;
+
+import com.oracle.java.testlibrary.*;
+import sun.misc.Unsafe;
+
+public class GetUncompressedObject {
+
+    public static void main(String args[]) throws Exception {
+        Unsafe unsafe = Utils.getUnsafe();
+
+        // Allocate some memory and fill it with non-zero values.
+        final int size = 32;
+        final long address = unsafe.allocateMemory(size);
+        unsafe.setMemory(address, size, (byte) 0x23);
+
+        // The only thing we can do is check for null-ness.
+        // So, store a null somewhere.
+        unsafe.putAddress(address + 16, 0);
+
+        Object nullObj = unsafe.getUncompressedObject(address + 16);
+        if (nullObj != null) {
+            throw new InternalError("should be null");
+        }
+    }
+
+}