8182487: Add Unsafe.objectFieldOffset(Class, String)
authorredestad
Thu, 22 Jun 2017 15:53:07 +0200
changeset 46568 4a430bd81cc6
parent 46565 8dcbf532ea00
child 46569 33bfb73333f9
8182487: Add Unsafe.objectFieldOffset(Class, String) Reviewed-by: dsimms, twisti, bchristi, mgerdin, psandoz, chegar
hotspot/src/share/vm/prims/unsafe.cpp
--- a/hotspot/src/share/vm/prims/unsafe.cpp	Tue Apr 11 23:45:37 2017 +0200
+++ b/hotspot/src/share/vm/prims/unsafe.cpp	Thu Jun 22 15:53:07 2017 +0200
@@ -27,6 +27,7 @@
 #include "classfile/vmSymbols.hpp"
 #include "memory/allocation.inline.hpp"
 #include "memory/resourceArea.hpp"
+#include "oops/fieldStreams.hpp"
 #include "oops/objArrayOop.inline.hpp"
 #include "oops/oop.inline.hpp"
 #include "prims/jni.h"
@@ -583,7 +584,30 @@
   return os::vm_page_size();
 } UNSAFE_END
 
-static jint find_field_offset(jobject field, int must_be_static, TRAPS) {
+static jlong find_field_offset(jclass clazz, jstring name, TRAPS) {
+  assert(clazz != NULL, "clazz must not be NULL");
+  assert(name != NULL, "name must not be NULL");
+
+  ResourceMark rm(THREAD);
+  char *utf_name = java_lang_String::as_utf8_string(JNIHandles::resolve_non_null(name));
+
+  InstanceKlass* k = InstanceKlass::cast(java_lang_Class::as_Klass(JNIHandles::resolve_non_null(clazz)));
+
+  jint offset = -1;
+  for (JavaFieldStream fs(k); !fs.done(); fs.next()) {
+    Symbol *name = fs.name();
+    if (name->equals(utf_name)) {
+      offset = fs.offset();
+      break;
+    }
+  }
+  if (offset < 0) {
+    THROW_0(vmSymbols::java_lang_InternalError());
+  }
+  return field_offset_from_byte_offset(offset);
+}
+
+static jlong find_field_offset(jobject field, int must_be_static, TRAPS) {
   assert(field != NULL, "field must not be NULL");
 
   oop reflected   = JNIHandles::resolve_non_null(field);
@@ -607,6 +631,10 @@
   return find_field_offset(field, 0, THREAD);
 } UNSAFE_END
 
+UNSAFE_ENTRY(jlong, Unsafe_ObjectFieldOffset1(JNIEnv *env, jobject unsafe, jclass c, jstring name)) {
+  return find_field_offset(c, name, THREAD);
+} UNSAFE_END
+
 UNSAFE_ENTRY(jlong, Unsafe_StaticFieldOffset0(JNIEnv *env, jobject unsafe, jobject field)) {
   return find_field_offset(field, 1, THREAD);
 } UNSAFE_END
@@ -712,14 +740,10 @@
 
 
 static inline void throw_new(JNIEnv *env, const char *ename) {
-  char buf[100];
-
-  jio_snprintf(buf, 100, "%s%s", "java/lang/", ename);
-
-  jclass cls = env->FindClass(buf);
+  jclass cls = env->FindClass(ename);
   if (env->ExceptionCheck()) {
     env->ExceptionClear();
-    tty->print_cr("Unsafe: cannot throw %s because FindClass has failed", buf);
+    tty->print_cr("Unsafe: cannot throw %s because FindClass has failed", ename);
     return;
   }
 
@@ -743,7 +767,7 @@
 
   body = NEW_C_HEAP_ARRAY(jbyte, length, mtInternal);
   if (body == NULL) {
-    throw_new(env, "OutOfMemoryError");
+    throw_new(env, "java/lang/OutOfMemoryError");
     return 0;
   }
 
@@ -759,7 +783,7 @@
     if (len >= sizeof(buf)) {
       utfName = NEW_C_HEAP_ARRAY(char, len + 1, mtInternal);
       if (utfName == NULL) {
-        throw_new(env, "OutOfMemoryError");
+        throw_new(env, "java/lang/OutOfMemoryError");
         goto free_body;
       }
     } else {
@@ -1182,6 +1206,7 @@
     {CC "freeMemory0",        CC "(" ADR ")V",           FN_PTR(Unsafe_FreeMemory0)},
 
     {CC "objectFieldOffset0", CC "(" FLD ")J",           FN_PTR(Unsafe_ObjectFieldOffset0)},
+    {CC "objectFieldOffset1", CC "(" CLS LANG "String;)J", FN_PTR(Unsafe_ObjectFieldOffset1)},
     {CC "staticFieldOffset0", CC "(" FLD ")J",           FN_PTR(Unsafe_StaticFieldOffset0)},
     {CC "staticFieldBase0",   CC "(" FLD ")" OBJ,        FN_PTR(Unsafe_StaticFieldBase0)},
     {CC "ensureClassInitialized0", CC "(" CLS ")V",      FN_PTR(Unsafe_EnsureClassInitialized0)},