1 /*
   2  * Copyright (c) 2000, 2015, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #include "precompiled.hpp"
  26 #include "classfile/classFileStream.hpp"
  27 #include "classfile/vmSymbols.hpp"
  28 #include "memory/allocation.inline.hpp"
  29 #include "oops/objArrayOop.inline.hpp"
  30 #include "oops/oop.inline.hpp"
  31 #include "prims/jni.h"
  32 #include "prims/jvm.h"
  33 #include "runtime/atomic.inline.hpp"
  34 #include "runtime/globals.hpp"
  35 #include "runtime/interfaceSupport.hpp"
  36 #include "runtime/orderAccess.inline.hpp"
  37 #include "runtime/reflection.hpp"
  38 #include "runtime/vm_version.hpp"
  39 #include "services/threadService.hpp"
  40 #include "trace/tracing.hpp"
  41 #include "utilities/copy.hpp"
  42 #include "utilities/dtrace.hpp"
  43 #include "utilities/macros.hpp"
  44 #if INCLUDE_ALL_GCS
  45 #include "gc/g1/g1SATBCardTableModRefBS.hpp"
  46 #endif // INCLUDE_ALL_GCS
  47 
  48 /*
  49  *      Implementation of class Unsafe
  50  */
  51 
  52 
  53 #define MAX_OBJECT_SIZE \
  54   ( arrayOopDesc::header_size(T_DOUBLE) * HeapWordSize \
  55     + ((julong)max_jint * sizeof(double)) )
  56 
  57 
  58 #define UNSAFE_ENTRY(result_type, header) \
  59   JVM_ENTRY(result_type, header)
  60 
  61 // Can't use UNSAFE_LEAF because it has the signature of a straight
  62 // call into the runtime (just like JVM_LEAF, funny that) but it's
  63 // called like a Java Native and thus the wrapper built for it passes
  64 // arguments like a JNI call.  It expects those arguments to be popped
  65 // from the stack on Intel like all good JNI args are, and adjusts the
  66 // stack according.  Since the JVM_LEAF call expects no extra
  67 // arguments the stack isn't popped in the C code, is pushed by the
  68 // wrapper and we get sick.
  69 //#define UNSAFE_LEAF(result_type, header) \
  70 //  JVM_LEAF(result_type, header)
  71 
  72 #define UNSAFE_END JVM_END
  73 
  74 #define UnsafeWrapper(arg) /*nothing, for the present*/
  75 
  76 
  77 inline void* addr_from_java(jlong addr) {
  78   // This assert fails in a variety of ways on 32-bit systems.
  79   // It is impossible to predict whether native code that converts
  80   // pointers to longs will sign-extend or zero-extend the addresses.
  81   //assert(addr == (uintptr_t)addr, "must not be odd high bits");
  82   return (void*)(uintptr_t)addr;
  83 }
  84 
  85 inline jlong addr_to_java(void* p) {
  86   assert(p == (void*)(uintptr_t)p, "must not be odd high bits");
  87   return (uintptr_t)p;
  88 }
  89 
  90 
  91 // Note: The VM's obj_field and related accessors use byte-scaled
  92 // ("unscaled") offsets, just as the unsafe methods do.
  93 
  94 // However, the method Unsafe.fieldOffset explicitly declines to
  95 // guarantee this.  The field offset values manipulated by the Java user
  96 // through the Unsafe API are opaque cookies that just happen to be byte
  97 // offsets.  We represent this state of affairs by passing the cookies
  98 // through conversion functions when going between the VM and the Unsafe API.
  99 // The conversion functions just happen to be no-ops at present.
 100 
 101 inline jlong field_offset_to_byte_offset(jlong field_offset) {
 102   return field_offset;
 103 }
 104 
 105 inline jlong field_offset_from_byte_offset(jlong byte_offset) {
 106   return byte_offset;
 107 }
 108 
 109 inline jint invocation_key_from_method_slot(jint slot) {
 110   return slot;
 111 }
 112 
 113 inline jint invocation_key_to_method_slot(jint key) {
 114   return key;
 115 }
 116 
 117 inline void* index_oop_from_field_offset_long(oop p, jlong field_offset) {
 118   jlong byte_offset = field_offset_to_byte_offset(field_offset);
 119 #ifdef ASSERT
 120   if (p != NULL) {
 121     assert(byte_offset >= 0 && byte_offset <= (jlong)MAX_OBJECT_SIZE, "sane offset");
 122     if (byte_offset == (jint)byte_offset) {
 123       void* ptr_plus_disp = (address)p + byte_offset;
 124       assert((void*)p->obj_field_addr<oop>((jint)byte_offset) == ptr_plus_disp,
 125              "raw [ptr+disp] must be consistent with oop::field_base");
 126     }
 127     jlong p_size = HeapWordSize * (jlong)(p->size());
 128     assert(byte_offset < p_size, "Unsafe access: offset " INT64_FORMAT " > object's size " INT64_FORMAT, byte_offset, p_size);
 129   }
 130 #endif
 131   if (sizeof(char*) == sizeof(jint))    // (this constant folds!)
 132     return (address)p + (jint) byte_offset;
 133   else
 134     return (address)p +        byte_offset;
 135 }
 136 
 137 // Externally callable versions:
 138 // (Use these in compiler intrinsics which emulate unsafe primitives.)
 139 jlong Unsafe_field_offset_to_byte_offset(jlong field_offset) {
 140   return field_offset;
 141 }
 142 jlong Unsafe_field_offset_from_byte_offset(jlong byte_offset) {
 143   return byte_offset;
 144 }
 145 jint Unsafe_invocation_key_from_method_slot(jint slot) {
 146   return invocation_key_from_method_slot(slot);
 147 }
 148 jint Unsafe_invocation_key_to_method_slot(jint key) {
 149   return invocation_key_to_method_slot(key);
 150 }
 151 
 152 
 153 ///// Data in the Java heap.
 154 
 155 #define GET_FIELD(obj, offset, type_name, v) \
 156   oop p = JNIHandles::resolve(obj); \
 157   type_name v = *(type_name*)index_oop_from_field_offset_long(p, offset)
 158 
 159 #define SET_FIELD(obj, offset, type_name, x) \
 160   oop p = JNIHandles::resolve(obj); \
 161   *(type_name*)index_oop_from_field_offset_long(p, offset) = x
 162 
 163 #define GET_FIELD_VOLATILE(obj, offset, type_name, v) \
 164   oop p = JNIHandles::resolve(obj); \
 165   if (support_IRIW_for_not_multiple_copy_atomic_cpu) { \
 166     OrderAccess::fence(); \
 167   } \
 168   volatile type_name v = OrderAccess::load_acquire((volatile type_name*)index_oop_from_field_offset_long(p, offset));
 169 
 170 #define SET_FIELD_VOLATILE(obj, offset, type_name, x) \
 171   oop p = JNIHandles::resolve(obj); \
 172   OrderAccess::release_store_fence((volatile type_name*)index_oop_from_field_offset_long(p, offset), x);
 173 
 174 
 175 // Get/SetObject must be special-cased, since it works with handles.
 176 
 177 // These functions allow a null base pointer with an arbitrary address.
 178 // But if the base pointer is non-null, the offset should make some sense.
 179 // That is, it should be in the range [0, MAX_OBJECT_SIZE].
 180 UNSAFE_ENTRY(jobject, Unsafe_GetObject(JNIEnv *env, jobject unsafe, jobject obj, jlong offset))
 181   UnsafeWrapper("Unsafe_GetObject");
 182   oop p = JNIHandles::resolve(obj);
 183   oop v;
 184   if (UseCompressedOops) {
 185     narrowOop n = *(narrowOop*)index_oop_from_field_offset_long(p, offset);
 186     v = oopDesc::decode_heap_oop(n);
 187   } else {
 188     v = *(oop*)index_oop_from_field_offset_long(p, offset);
 189   }
 190   jobject ret = JNIHandles::make_local(env, v);
 191 #if INCLUDE_ALL_GCS
 192   // We could be accessing the referent field in a reference
 193   // object. If G1 is enabled then we need to register non-null
 194   // referent with the SATB barrier.
 195   if (UseG1GC) {
 196     bool needs_barrier = false;
 197 
 198     if (ret != NULL) {
 199       if (offset == java_lang_ref_Reference::referent_offset && obj != NULL) {
 200         oop o = JNIHandles::resolve(obj);
 201         Klass* k = o->klass();
 202         if (InstanceKlass::cast(k)->reference_type() != REF_NONE) {
 203           assert(InstanceKlass::cast(k)->is_subclass_of(SystemDictionary::Reference_klass()), "sanity");
 204           needs_barrier = true;
 205         }
 206       }
 207     }
 208 
 209     if (needs_barrier) {
 210       oop referent = JNIHandles::resolve(ret);
 211       G1SATBCardTableModRefBS::enqueue(referent);
 212     }
 213   }
 214 #endif // INCLUDE_ALL_GCS
 215   return ret;
 216 UNSAFE_END
 217 
 218 UNSAFE_ENTRY(void, Unsafe_SetObject(JNIEnv *env, jobject unsafe, jobject obj, jlong offset, jobject x_h))
 219   UnsafeWrapper("Unsafe_SetObject");
 220   oop x = JNIHandles::resolve(x_h);
 221   oop p = JNIHandles::resolve(obj);
 222   if (UseCompressedOops) {
 223     oop_store((narrowOop*)index_oop_from_field_offset_long(p, offset), x);
 224   } else {
 225     oop_store((oop*)index_oop_from_field_offset_long(p, offset), x);
 226   }
 227 UNSAFE_END
 228 
 229 UNSAFE_ENTRY(jobject, Unsafe_GetObjectVolatile(JNIEnv *env, jobject unsafe, jobject obj, jlong offset))
 230   UnsafeWrapper("Unsafe_GetObjectVolatile");
 231   oop p = JNIHandles::resolve(obj);
 232   void* addr = index_oop_from_field_offset_long(p, offset);
 233   volatile oop v;
 234   if (UseCompressedOops) {
 235     volatile narrowOop n = *(volatile narrowOop*) addr;
 236     (void)const_cast<oop&>(v = oopDesc::decode_heap_oop(n));
 237   } else {
 238     (void)const_cast<oop&>(v = *(volatile oop*) addr);
 239   }
 240   OrderAccess::acquire();
 241   return JNIHandles::make_local(env, v);
 242 UNSAFE_END
 243 
 244 UNSAFE_ENTRY(void, Unsafe_SetObjectVolatile(JNIEnv *env, jobject unsafe, jobject obj, jlong offset, jobject x_h))
 245   UnsafeWrapper("Unsafe_SetObjectVolatile");
 246   oop x = JNIHandles::resolve(x_h);
 247   oop p = JNIHandles::resolve(obj);
 248   void* addr = index_oop_from_field_offset_long(p, offset);
 249   OrderAccess::release();
 250   if (UseCompressedOops) {
 251     oop_store((narrowOop*)addr, x);
 252   } else {
 253     oop_store((oop*)addr, x);
 254   }
 255   OrderAccess::fence();
 256 UNSAFE_END
 257 
 258 UNSAFE_ENTRY(jobject, Unsafe_GetUncompressedObject(JNIEnv *env, jobject unsafe, jlong addr))
 259   UnsafeWrapper("Unsafe_GetUncompressedObject");
 260   oop v = *(oop*) (address) addr;
 261   return JNIHandles::make_local(env, v);
 262 UNSAFE_END
 263 
 264 UNSAFE_ENTRY(jclass, Unsafe_GetJavaMirror(JNIEnv *env, jobject unsafe, jlong metaspace_klass))
 265   UnsafeWrapper("Unsafe_GetJavaMirror");
 266   Klass* klass = (Klass*) (address) metaspace_klass;
 267   return (jclass) JNIHandles::make_local(klass->java_mirror());
 268 UNSAFE_END
 269 
 270 UNSAFE_ENTRY(jlong, Unsafe_GetKlassPointer(JNIEnv *env, jobject unsafe, jobject obj))
 271   UnsafeWrapper("Unsafe_GetKlassPointer");
 272   oop o = JNIHandles::resolve(obj);
 273   jlong klass = (jlong) (address) o->klass();
 274   return klass;
 275 UNSAFE_END
 276 
 277 #ifndef SUPPORTS_NATIVE_CX8
 278 
 279 // VM_Version::supports_cx8() is a surrogate for 'supports atomic long memory ops'.
 280 //
 281 // On platforms which do not support atomic compare-and-swap of jlong (8 byte)
 282 // values we have to use a lock-based scheme to enforce atomicity. This has to be
 283 // applied to all Unsafe operations that set the value of a jlong field. Even so
 284 // the compareAndSwapLong operation will not be atomic with respect to direct stores
 285 // to the field from Java code. It is important therefore that any Java code that
 286 // utilizes these Unsafe jlong operations does not perform direct stores. To permit
 287 // direct loads of the field from Java code we must also use Atomic::store within the
 288 // locked regions. And for good measure, in case there are direct stores, we also
 289 // employ Atomic::load within those regions. Note that the field in question must be
 290 // volatile and so must have atomic load/store accesses applied at the Java level.
 291 //
 292 // The locking scheme could utilize a range of strategies for controlling the locking
 293 // granularity: from a lock per-field through to a single global lock. The latter is
 294 // the simplest and is used for the current implementation. Note that the Java object
 295 // that contains the field, can not, in general, be used for locking. To do so can lead
 296 // to deadlocks as we may introduce locking into what appears to the Java code to be a
 297 // lock-free path.
 298 //
 299 // As all the locked-regions are very short and themselves non-blocking we can treat
 300 // them as leaf routines and elide safepoint checks (ie we don't perform any thread
 301 // state transitions even when blocking for the lock). Note that if we do choose to
 302 // add safepoint checks and thread state transitions, we must ensure that we calculate
 303 // the address of the field _after_ we have acquired the lock, else the object may have
 304 // been moved by the GC
 305 
 306 UNSAFE_ENTRY(jlong, Unsafe_GetLongVolatile(JNIEnv *env, jobject unsafe, jobject obj, jlong offset))
 307   UnsafeWrapper("Unsafe_GetLongVolatile");
 308   {
 309     if (VM_Version::supports_cx8()) {
 310       GET_FIELD_VOLATILE(obj, offset, jlong, v);
 311       return v;
 312     }
 313     else {
 314       Handle p (THREAD, JNIHandles::resolve(obj));
 315       jlong* addr = (jlong*)(index_oop_from_field_offset_long(p(), offset));
 316       MutexLockerEx mu(UnsafeJlong_lock, Mutex::_no_safepoint_check_flag);
 317       jlong value = Atomic::load(addr);
 318       return value;
 319     }
 320   }
 321 UNSAFE_END
 322 
 323 UNSAFE_ENTRY(void, Unsafe_SetLongVolatile(JNIEnv *env, jobject unsafe, jobject obj, jlong offset, jlong x))
 324   UnsafeWrapper("Unsafe_SetLongVolatile");
 325   {
 326     if (VM_Version::supports_cx8()) {
 327       SET_FIELD_VOLATILE(obj, offset, jlong, x);
 328     }
 329     else {
 330       Handle p (THREAD, JNIHandles::resolve(obj));
 331       jlong* addr = (jlong*)(index_oop_from_field_offset_long(p(), offset));
 332       MutexLockerEx mu(UnsafeJlong_lock, Mutex::_no_safepoint_check_flag);
 333       Atomic::store(x, addr);
 334     }
 335   }
 336 UNSAFE_END
 337 
 338 #endif // not SUPPORTS_NATIVE_CX8
 339 
 340 UNSAFE_ENTRY(jboolean, Unsafe_isBigEndian0(JNIEnv *env, jobject unsafe))
 341   UnsafeWrapper("Unsafe_IsBigEndian0");
 342   {
 343 #ifdef VM_LITTLE_ENDIAN
 344     return false;
 345 #else
 346     return true;
 347 #endif
 348   }
 349 UNSAFE_END
 350 
 351 UNSAFE_ENTRY(jint, Unsafe_unalignedAccess0(JNIEnv *env, jobject unsafe))
 352   UnsafeWrapper("Unsafe_UnalignedAccess0");
 353   {
 354     return UseUnalignedAccesses;
 355   }
 356 UNSAFE_END
 357 
 358 #define DEFINE_GETSETOOP(jboolean, Boolean) \
 359  \
 360 UNSAFE_ENTRY(jboolean, Unsafe_Get##Boolean##140(JNIEnv *env, jobject unsafe, jobject obj, jint offset)) \
 361   UnsafeWrapper("Unsafe_Get"#Boolean); \
 362   if (obj == NULL)  THROW_0(vmSymbols::java_lang_NullPointerException()); \
 363   GET_FIELD(obj, offset, jboolean, v); \
 364   return v; \
 365 UNSAFE_END \
 366  \
 367 UNSAFE_ENTRY(void, Unsafe_Set##Boolean##140(JNIEnv *env, jobject unsafe, jobject obj, jint offset, jboolean x)) \
 368   UnsafeWrapper("Unsafe_Set"#Boolean); \
 369   if (obj == NULL)  THROW(vmSymbols::java_lang_NullPointerException()); \
 370   SET_FIELD(obj, offset, jboolean, x); \
 371 UNSAFE_END \
 372  \
 373 UNSAFE_ENTRY(jboolean, Unsafe_Get##Boolean(JNIEnv *env, jobject unsafe, jobject obj, jlong offset)) \
 374   UnsafeWrapper("Unsafe_Get"#Boolean); \
 375   GET_FIELD(obj, offset, jboolean, v); \
 376   return v; \
 377 UNSAFE_END \
 378  \
 379 UNSAFE_ENTRY(void, Unsafe_Set##Boolean(JNIEnv *env, jobject unsafe, jobject obj, jlong offset, jboolean x)) \
 380   UnsafeWrapper("Unsafe_Set"#Boolean); \
 381   SET_FIELD(obj, offset, jboolean, x); \
 382 UNSAFE_END \
 383  \
 384 // END DEFINE_GETSETOOP.
 385 
 386 DEFINE_GETSETOOP(jboolean, Boolean)
 387 DEFINE_GETSETOOP(jbyte, Byte)
 388 DEFINE_GETSETOOP(jshort, Short);
 389 DEFINE_GETSETOOP(jchar, Char);
 390 DEFINE_GETSETOOP(jint, Int);
 391 DEFINE_GETSETOOP(jlong, Long);
 392 DEFINE_GETSETOOP(jfloat, Float);
 393 DEFINE_GETSETOOP(jdouble, Double);
 394 
 395 #undef DEFINE_GETSETOOP
 396 
 397 #define DEFINE_GETSETOOP_VOLATILE(jboolean, Boolean) \
 398  \
 399 UNSAFE_ENTRY(jboolean, Unsafe_Get##Boolean##Volatile(JNIEnv *env, jobject unsafe, jobject obj, jlong offset)) \
 400   UnsafeWrapper("Unsafe_Get"#Boolean); \
 401   GET_FIELD_VOLATILE(obj, offset, jboolean, v); \
 402   return v; \
 403 UNSAFE_END \
 404  \
 405 UNSAFE_ENTRY(void, Unsafe_Set##Boolean##Volatile(JNIEnv *env, jobject unsafe, jobject obj, jlong offset, jboolean x)) \
 406   UnsafeWrapper("Unsafe_Set"#Boolean); \
 407   SET_FIELD_VOLATILE(obj, offset, jboolean, x); \
 408 UNSAFE_END \
 409  \
 410 // END DEFINE_GETSETOOP_VOLATILE.
 411 
 412 DEFINE_GETSETOOP_VOLATILE(jboolean, Boolean)
 413 DEFINE_GETSETOOP_VOLATILE(jbyte, Byte)
 414 DEFINE_GETSETOOP_VOLATILE(jshort, Short);
 415 DEFINE_GETSETOOP_VOLATILE(jchar, Char);
 416 DEFINE_GETSETOOP_VOLATILE(jint, Int);
 417 DEFINE_GETSETOOP_VOLATILE(jfloat, Float);
 418 DEFINE_GETSETOOP_VOLATILE(jdouble, Double);
 419 
 420 #ifdef SUPPORTS_NATIVE_CX8
 421 DEFINE_GETSETOOP_VOLATILE(jlong, Long);
 422 #endif
 423 
 424 #undef DEFINE_GETSETOOP_VOLATILE
 425 
 426 // The non-intrinsified versions of setOrdered just use setVolatile
 427 
 428 UNSAFE_ENTRY(void, Unsafe_SetOrderedInt(JNIEnv *env, jobject unsafe, jobject obj, jlong offset, jint x))
 429   UnsafeWrapper("Unsafe_SetOrderedInt");
 430   SET_FIELD_VOLATILE(obj, offset, jint, x);
 431 UNSAFE_END
 432 
 433 UNSAFE_ENTRY(void, Unsafe_SetOrderedObject(JNIEnv *env, jobject unsafe, jobject obj, jlong offset, jobject x_h))
 434   UnsafeWrapper("Unsafe_SetOrderedObject");
 435   oop x = JNIHandles::resolve(x_h);
 436   oop p = JNIHandles::resolve(obj);
 437   void* addr = index_oop_from_field_offset_long(p, offset);
 438   OrderAccess::release();
 439   if (UseCompressedOops) {
 440     oop_store((narrowOop*)addr, x);
 441   } else {
 442     oop_store((oop*)addr, x);
 443   }
 444   OrderAccess::fence();
 445 UNSAFE_END
 446 
 447 UNSAFE_ENTRY(void, Unsafe_SetOrderedLong(JNIEnv *env, jobject unsafe, jobject obj, jlong offset, jlong x))
 448   UnsafeWrapper("Unsafe_SetOrderedLong");
 449 #ifdef SUPPORTS_NATIVE_CX8
 450   SET_FIELD_VOLATILE(obj, offset, jlong, x);
 451 #else
 452   // Keep old code for platforms which may not have atomic long (8 bytes) instructions
 453   {
 454     if (VM_Version::supports_cx8()) {
 455       SET_FIELD_VOLATILE(obj, offset, jlong, x);
 456     }
 457     else {
 458       Handle p (THREAD, JNIHandles::resolve(obj));
 459       jlong* addr = (jlong*)(index_oop_from_field_offset_long(p(), offset));
 460       MutexLockerEx mu(UnsafeJlong_lock, Mutex::_no_safepoint_check_flag);
 461       Atomic::store(x, addr);
 462     }
 463   }
 464 #endif
 465 UNSAFE_END
 466 
 467 UNSAFE_ENTRY(void, Unsafe_LoadFence(JNIEnv *env, jobject unsafe))
 468   UnsafeWrapper("Unsafe_LoadFence");
 469   OrderAccess::acquire();
 470 UNSAFE_END
 471 
 472 UNSAFE_ENTRY(void, Unsafe_StoreFence(JNIEnv *env, jobject unsafe))
 473   UnsafeWrapper("Unsafe_StoreFence");
 474   OrderAccess::release();
 475 UNSAFE_END
 476 
 477 UNSAFE_ENTRY(void, Unsafe_FullFence(JNIEnv *env, jobject unsafe))
 478   UnsafeWrapper("Unsafe_FullFence");
 479   OrderAccess::fence();
 480 UNSAFE_END
 481 
 482 ////// Data in the C heap.
 483 
 484 // Note:  These do not throw NullPointerException for bad pointers.
 485 // They just crash.  Only a oop base pointer can generate a NullPointerException.
 486 //
 487 #define DEFINE_GETSETNATIVE(java_type, Type, native_type) \
 488  \
 489 UNSAFE_ENTRY(java_type, Unsafe_GetNative##Type(JNIEnv *env, jobject unsafe, jlong addr)) \
 490   UnsafeWrapper("Unsafe_GetNative"#Type); \
 491   void* p = addr_from_java(addr); \
 492   JavaThread* t = JavaThread::current(); \
 493   t->set_doing_unsafe_access(true); \
 494   java_type x = *(volatile native_type*)p; \
 495   t->set_doing_unsafe_access(false); \
 496   return x; \
 497 UNSAFE_END \
 498  \
 499 UNSAFE_ENTRY(void, Unsafe_SetNative##Type(JNIEnv *env, jobject unsafe, jlong addr, java_type x)) \
 500   UnsafeWrapper("Unsafe_SetNative"#Type); \
 501   JavaThread* t = JavaThread::current(); \
 502   t->set_doing_unsafe_access(true); \
 503   void* p = addr_from_java(addr); \
 504   *(volatile native_type*)p = x; \
 505   t->set_doing_unsafe_access(false); \
 506 UNSAFE_END \
 507  \
 508 // END DEFINE_GETSETNATIVE.
 509 
 510 DEFINE_GETSETNATIVE(jbyte, Byte, signed char)
 511 DEFINE_GETSETNATIVE(jshort, Short, signed short);
 512 DEFINE_GETSETNATIVE(jchar, Char, unsigned short);
 513 DEFINE_GETSETNATIVE(jint, Int, jint);
 514 // no long -- handled specially
 515 DEFINE_GETSETNATIVE(jfloat, Float, float);
 516 DEFINE_GETSETNATIVE(jdouble, Double, double);
 517 
 518 #undef DEFINE_GETSETNATIVE
 519 
 520 UNSAFE_ENTRY(jlong, Unsafe_GetNativeLong(JNIEnv *env, jobject unsafe, jlong addr))
 521   UnsafeWrapper("Unsafe_GetNativeLong");
 522   JavaThread* t = JavaThread::current();
 523   // We do it this way to avoid problems with access to heap using 64
 524   // bit loads, as jlong in heap could be not 64-bit aligned, and on
 525   // some CPUs (SPARC) it leads to SIGBUS.
 526   t->set_doing_unsafe_access(true);
 527   void* p = addr_from_java(addr);
 528   jlong x;
 529   if (((intptr_t)p & 7) == 0) {
 530     // jlong is aligned, do a volatile access
 531     x = *(volatile jlong*)p;
 532   } else {
 533     jlong_accessor acc;
 534     acc.words[0] = ((volatile jint*)p)[0];
 535     acc.words[1] = ((volatile jint*)p)[1];
 536     x = acc.long_value;
 537   }
 538   t->set_doing_unsafe_access(false);
 539   return x;
 540 UNSAFE_END
 541 
 542 UNSAFE_ENTRY(void, Unsafe_SetNativeLong(JNIEnv *env, jobject unsafe, jlong addr, jlong x))
 543   UnsafeWrapper("Unsafe_SetNativeLong");
 544   JavaThread* t = JavaThread::current();
 545   // see comment for Unsafe_GetNativeLong
 546   t->set_doing_unsafe_access(true);
 547   void* p = addr_from_java(addr);
 548   if (((intptr_t)p & 7) == 0) {
 549     // jlong is aligned, do a volatile access
 550     *(volatile jlong*)p = x;
 551   } else {
 552     jlong_accessor acc;
 553     acc.long_value = x;
 554     ((volatile jint*)p)[0] = acc.words[0];
 555     ((volatile jint*)p)[1] = acc.words[1];
 556   }
 557   t->set_doing_unsafe_access(false);
 558 UNSAFE_END
 559 
 560 
 561 UNSAFE_ENTRY(jlong, Unsafe_GetNativeAddress(JNIEnv *env, jobject unsafe, jlong addr))
 562   UnsafeWrapper("Unsafe_GetNativeAddress");
 563   void* p = addr_from_java(addr);
 564   return addr_to_java(*(void**)p);
 565 UNSAFE_END
 566 
 567 UNSAFE_ENTRY(void, Unsafe_SetNativeAddress(JNIEnv *env, jobject unsafe, jlong addr, jlong x))
 568   UnsafeWrapper("Unsafe_SetNativeAddress");
 569   void* p = addr_from_java(addr);
 570   *(void**)p = addr_from_java(x);
 571 UNSAFE_END
 572 
 573 
 574 ////// Allocation requests
 575 
 576 UNSAFE_ENTRY(jobject, Unsafe_AllocateInstance(JNIEnv *env, jobject unsafe, jclass cls))
 577   UnsafeWrapper("Unsafe_AllocateInstance");
 578   {
 579     ThreadToNativeFromVM ttnfv(thread);
 580     return env->AllocObject(cls);
 581   }
 582 UNSAFE_END
 583 
 584 UNSAFE_ENTRY(jlong, Unsafe_AllocateMemory(JNIEnv *env, jobject unsafe, jlong size))
 585   UnsafeWrapper("Unsafe_AllocateMemory");
 586   size_t sz = (size_t)size;
 587   if (sz != (julong)size || size < 0) {
 588     THROW_0(vmSymbols::java_lang_IllegalArgumentException());
 589   }
 590   if (sz == 0) {
 591     return 0;
 592   }
 593   sz = round_to(sz, HeapWordSize);
 594   void* x = os::malloc(sz, mtInternal);
 595   if (x == NULL) {
 596     THROW_0(vmSymbols::java_lang_OutOfMemoryError());
 597   }
 598   //Copy::fill_to_words((HeapWord*)x, sz / HeapWordSize);
 599   return addr_to_java(x);
 600 UNSAFE_END
 601 
 602 UNSAFE_ENTRY(jlong, Unsafe_ReallocateMemory(JNIEnv *env, jobject unsafe, jlong addr, jlong size))
 603   UnsafeWrapper("Unsafe_ReallocateMemory");
 604   void* p = addr_from_java(addr);
 605   size_t sz = (size_t)size;
 606   if (sz != (julong)size || size < 0) {
 607     THROW_0(vmSymbols::java_lang_IllegalArgumentException());
 608   }
 609   if (sz == 0) {
 610     os::free(p);
 611     return 0;
 612   }
 613   sz = round_to(sz, HeapWordSize);
 614   void* x = (p == NULL) ? os::malloc(sz, mtInternal) : os::realloc(p, sz, mtInternal);
 615   if (x == NULL) {
 616     THROW_0(vmSymbols::java_lang_OutOfMemoryError());
 617   }
 618   return addr_to_java(x);
 619 UNSAFE_END
 620 
 621 UNSAFE_ENTRY(void, Unsafe_FreeMemory(JNIEnv *env, jobject unsafe, jlong addr))
 622   UnsafeWrapper("Unsafe_FreeMemory");
 623   void* p = addr_from_java(addr);
 624   if (p == NULL) {
 625     return;
 626   }
 627   os::free(p);
 628 UNSAFE_END
 629 
 630 UNSAFE_ENTRY(void, Unsafe_SetMemory(JNIEnv *env, jobject unsafe, jobject obj, jlong offset, jlong size, jbyte value))
 631   UnsafeWrapper("Unsafe_SetMemory");
 632   size_t sz = (size_t)size;
 633   if (sz != (julong)size || size < 0) {
 634     THROW(vmSymbols::java_lang_IllegalArgumentException());
 635   }
 636   oop base = JNIHandles::resolve(obj);
 637   void* p = index_oop_from_field_offset_long(base, offset);
 638   Copy::fill_to_memory_atomic(p, sz, value);
 639 UNSAFE_END
 640 
 641 UNSAFE_ENTRY(void, Unsafe_CopyMemory(JNIEnv *env, jobject unsafe, jobject srcObj, jlong srcOffset, jobject dstObj, jlong dstOffset, jlong size))
 642   UnsafeWrapper("Unsafe_CopyMemory");
 643   if (size == 0) {
 644     return;
 645   }
 646   size_t sz = (size_t)size;
 647   if (sz != (julong)size || size < 0) {
 648     THROW(vmSymbols::java_lang_IllegalArgumentException());
 649   }
 650   oop srcp = JNIHandles::resolve(srcObj);
 651   oop dstp = JNIHandles::resolve(dstObj);
 652   if (dstp != NULL && !dstp->is_typeArray()) {
 653     // NYI:  This works only for non-oop arrays at present.
 654     // Generalizing it would be reasonable, but requires card marking.
 655     // Also, autoboxing a Long from 0L in copyMemory(x,y, 0L,z, n) would be bad.
 656     THROW(vmSymbols::java_lang_IllegalArgumentException());
 657   }
 658   void* src = index_oop_from_field_offset_long(srcp, srcOffset);
 659   void* dst = index_oop_from_field_offset_long(dstp, dstOffset);
 660   Copy::conjoint_memory_atomic(src, dst, sz);
 661 UNSAFE_END
 662 
 663 
 664 ////// Random queries
 665 
 666 // See comment at file start about UNSAFE_LEAF
 667 //UNSAFE_LEAF(jint, Unsafe_AddressSize())
 668 UNSAFE_ENTRY(jint, Unsafe_AddressSize(JNIEnv *env, jobject unsafe))
 669   UnsafeWrapper("Unsafe_AddressSize");
 670   return sizeof(void*);
 671 UNSAFE_END
 672 
 673 // See comment at file start about UNSAFE_LEAF
 674 //UNSAFE_LEAF(jint, Unsafe_PageSize())
 675 UNSAFE_ENTRY(jint, Unsafe_PageSize(JNIEnv *env, jobject unsafe))
 676   UnsafeWrapper("Unsafe_PageSize");
 677   return os::vm_page_size();
 678 UNSAFE_END
 679 
 680 jint find_field_offset(jobject field, int must_be_static, TRAPS) {
 681   if (field == NULL) {
 682     THROW_0(vmSymbols::java_lang_NullPointerException());
 683   }
 684 
 685   oop reflected   = JNIHandles::resolve_non_null(field);
 686   oop mirror      = java_lang_reflect_Field::clazz(reflected);
 687   Klass* k      = java_lang_Class::as_Klass(mirror);
 688   int slot        = java_lang_reflect_Field::slot(reflected);
 689   int modifiers   = java_lang_reflect_Field::modifiers(reflected);
 690 
 691   if (must_be_static >= 0) {
 692     int really_is_static = ((modifiers & JVM_ACC_STATIC) != 0);
 693     if (must_be_static != really_is_static) {
 694       THROW_0(vmSymbols::java_lang_IllegalArgumentException());
 695     }
 696   }
 697 
 698   int offset = InstanceKlass::cast(k)->field_offset(slot);
 699   return field_offset_from_byte_offset(offset);
 700 }
 701 
 702 UNSAFE_ENTRY(jlong, Unsafe_ObjectFieldOffset(JNIEnv *env, jobject unsafe, jobject field))
 703   UnsafeWrapper("Unsafe_ObjectFieldOffset");
 704   return find_field_offset(field, 0, THREAD);
 705 UNSAFE_END
 706 
 707 UNSAFE_ENTRY(jlong, Unsafe_StaticFieldOffset(JNIEnv *env, jobject unsafe, jobject field))
 708   UnsafeWrapper("Unsafe_StaticFieldOffset");
 709   return find_field_offset(field, 1, THREAD);
 710 UNSAFE_END
 711 
 712 UNSAFE_ENTRY(jobject, Unsafe_StaticFieldBaseFromField(JNIEnv *env, jobject unsafe, jobject field))
 713   UnsafeWrapper("Unsafe_StaticFieldBase");
 714   // Note:  In this VM implementation, a field address is always a short
 715   // offset from the base of a a klass metaobject.  Thus, the full dynamic
 716   // range of the return type is never used.  However, some implementations
 717   // might put the static field inside an array shared by many classes,
 718   // or even at a fixed address, in which case the address could be quite
 719   // large.  In that last case, this function would return NULL, since
 720   // the address would operate alone, without any base pointer.
 721 
 722   if (field == NULL)  THROW_0(vmSymbols::java_lang_NullPointerException());
 723 
 724   oop reflected   = JNIHandles::resolve_non_null(field);
 725   oop mirror      = java_lang_reflect_Field::clazz(reflected);
 726   int modifiers   = java_lang_reflect_Field::modifiers(reflected);
 727 
 728   if ((modifiers & JVM_ACC_STATIC) == 0) {
 729     THROW_0(vmSymbols::java_lang_IllegalArgumentException());
 730   }
 731 
 732   return JNIHandles::make_local(env, mirror);
 733 UNSAFE_END
 734 
 735 UNSAFE_ENTRY(void, Unsafe_EnsureClassInitialized(JNIEnv *env, jobject unsafe, jobject clazz)) {
 736   UnsafeWrapper("Unsafe_EnsureClassInitialized");
 737   if (clazz == NULL) {
 738     THROW(vmSymbols::java_lang_NullPointerException());
 739   }
 740   oop mirror = JNIHandles::resolve_non_null(clazz);
 741 
 742   Klass* klass = java_lang_Class::as_Klass(mirror);
 743   if (klass != NULL && klass->should_be_initialized()) {
 744     InstanceKlass* k = InstanceKlass::cast(klass);
 745     k->initialize(CHECK);
 746   }
 747 }
 748 UNSAFE_END
 749 
 750 UNSAFE_ENTRY(jboolean, Unsafe_ShouldBeInitialized(JNIEnv *env, jobject unsafe, jobject clazz)) {
 751   UnsafeWrapper("Unsafe_ShouldBeInitialized");
 752   if (clazz == NULL) {
 753     THROW_(vmSymbols::java_lang_NullPointerException(), false);
 754   }
 755   oop mirror = JNIHandles::resolve_non_null(clazz);
 756   Klass* klass = java_lang_Class::as_Klass(mirror);
 757   if (klass != NULL && klass->should_be_initialized()) {
 758     return true;
 759   }
 760   return false;
 761 }
 762 UNSAFE_END
 763 
 764 static void getBaseAndScale(int& base, int& scale, jclass acls, TRAPS) {
 765   if (acls == NULL) {
 766     THROW(vmSymbols::java_lang_NullPointerException());
 767   }
 768   oop      mirror = JNIHandles::resolve_non_null(acls);
 769   Klass* k      = java_lang_Class::as_Klass(mirror);
 770   if (k == NULL || !k->is_array_klass()) {
 771     THROW(vmSymbols::java_lang_InvalidClassException());
 772   } else if (k->is_objArray_klass()) {
 773     base  = arrayOopDesc::base_offset_in_bytes(T_OBJECT);
 774     scale = heapOopSize;
 775   } else if (k->is_typeArray_klass()) {
 776     TypeArrayKlass* tak = TypeArrayKlass::cast(k);
 777     base  = tak->array_header_in_bytes();
 778     assert(base == arrayOopDesc::base_offset_in_bytes(tak->element_type()), "array_header_size semantics ok");
 779     scale = (1 << tak->log2_element_size());
 780   } else {
 781     ShouldNotReachHere();
 782   }
 783 }
 784 
 785 UNSAFE_ENTRY(jint, Unsafe_ArrayBaseOffset(JNIEnv *env, jobject unsafe, jclass acls))
 786   UnsafeWrapper("Unsafe_ArrayBaseOffset");
 787   int base = 0, scale = 0;
 788   getBaseAndScale(base, scale, acls, CHECK_0);
 789   return field_offset_from_byte_offset(base);
 790 UNSAFE_END
 791 
 792 
 793 UNSAFE_ENTRY(jint, Unsafe_ArrayIndexScale(JNIEnv *env, jobject unsafe, jclass acls))
 794   UnsafeWrapper("Unsafe_ArrayIndexScale");
 795   int base = 0, scale = 0;
 796   getBaseAndScale(base, scale, acls, CHECK_0);
 797   // This VM packs both fields and array elements down to the byte.
 798   // But watch out:  If this changes, so that array references for
 799   // a given primitive type (say, T_BOOLEAN) use different memory units
 800   // than fields, this method MUST return zero for such arrays.
 801   // For example, the VM used to store sub-word sized fields in full
 802   // words in the object layout, so that accessors like getByte(Object,int)
 803   // did not really do what one might expect for arrays.  Therefore,
 804   // this function used to report a zero scale factor, so that the user
 805   // would know not to attempt to access sub-word array elements.
 806   // // Code for unpacked fields:
 807   // if (scale < wordSize)  return 0;
 808 
 809   // The following allows for a pretty general fieldOffset cookie scheme,
 810   // but requires it to be linear in byte offset.
 811   return field_offset_from_byte_offset(scale) - field_offset_from_byte_offset(0);
 812 UNSAFE_END
 813 
 814 
 815 static inline void throw_new(JNIEnv *env, const char *ename) {
 816   char buf[100];
 817   jio_snprintf(buf, 100, "%s%s", "java/lang/", ename);
 818   jclass cls = env->FindClass(buf);
 819   if (env->ExceptionCheck()) {
 820     env->ExceptionClear();
 821     tty->print_cr("Unsafe: cannot throw %s because FindClass has failed", buf);
 822     return;
 823   }
 824   char* msg = NULL;
 825   env->ThrowNew(cls, msg);
 826 }
 827 
 828 static jclass Unsafe_DefineClass_impl(JNIEnv *env, jstring name, jbyteArray data, int offset, int length, jobject loader, jobject pd) {
 829   {
 830     // Code lifted from JDK 1.3 ClassLoader.c
 831 
 832     jbyte *body;
 833     char *utfName;
 834     jclass result = 0;
 835     char buf[128];
 836 
 837     if (UsePerfData) {
 838       ClassLoader::unsafe_defineClassCallCounter()->inc();
 839     }
 840 
 841     if (data == NULL) {
 842         throw_new(env, "NullPointerException");
 843         return 0;
 844     }
 845 
 846     /* Work around 4153825. malloc crashes on Solaris when passed a
 847      * negative size.
 848      */
 849     if (length < 0) {
 850         throw_new(env, "ArrayIndexOutOfBoundsException");
 851         return 0;
 852     }
 853 
 854     body = NEW_C_HEAP_ARRAY(jbyte, length, mtInternal);
 855 
 856     if (body == 0) {
 857         throw_new(env, "OutOfMemoryError");
 858         return 0;
 859     }
 860 
 861     env->GetByteArrayRegion(data, offset, length, body);
 862 
 863     if (env->ExceptionOccurred())
 864         goto free_body;
 865 
 866     if (name != NULL) {
 867         uint len = env->GetStringUTFLength(name);
 868         int unicode_len = env->GetStringLength(name);
 869         if (len >= sizeof(buf)) {
 870             utfName = NEW_C_HEAP_ARRAY(char, len + 1, mtInternal);
 871             if (utfName == NULL) {
 872                 throw_new(env, "OutOfMemoryError");
 873                 goto free_body;
 874             }
 875         } else {
 876             utfName = buf;
 877         }
 878         env->GetStringUTFRegion(name, 0, unicode_len, utfName);
 879         //VerifyFixClassname(utfName);
 880         for (uint i = 0; i < len; i++) {
 881           if (utfName[i] == '.')   utfName[i] = '/';
 882         }
 883     } else {
 884         utfName = NULL;
 885     }
 886 
 887     result = JVM_DefineClass(env, utfName, loader, body, length, pd);
 888 
 889     if (utfName && utfName != buf)
 890         FREE_C_HEAP_ARRAY(char, utfName);
 891 
 892  free_body:
 893     FREE_C_HEAP_ARRAY(jbyte, body);
 894     return result;
 895   }
 896 }
 897 
 898 
 899 UNSAFE_ENTRY(jclass, Unsafe_DefineClass(JNIEnv *env, jobject unsafe, jstring name, jbyteArray data, int offset, int length, jobject loader, jobject pd))
 900   UnsafeWrapper("Unsafe_DefineClass");
 901   {
 902     ThreadToNativeFromVM ttnfv(thread);
 903     return Unsafe_DefineClass_impl(env, name, data, offset, length, loader, pd);
 904   }
 905 UNSAFE_END
 906 
 907 
 908 // define a class but do not make it known to the class loader or system dictionary
 909 // - host_class:  supplies context for linkage, access control, protection domain, and class loader
 910 // - data:  bytes of a class file, a raw memory address (length gives the number of bytes)
 911 // - cp_patches:  where non-null entries exist, they replace corresponding CP entries in data
 912 
 913 // When you load an anonymous class U, it works as if you changed its name just before loading,
 914 // to a name that you will never use again.  Since the name is lost, no other class can directly
 915 // link to any member of U.  Just after U is loaded, the only way to use it is reflectively,
 916 // through java.lang.Class methods like Class.newInstance.
 917 
 918 // Access checks for linkage sites within U continue to follow the same rules as for named classes.
 919 // The package of an anonymous class is given by the package qualifier on the name under which it was loaded.
 920 // An anonymous class also has special privileges to access any member of its host class.
 921 // This is the main reason why this loading operation is unsafe.  The purpose of this is to
 922 // allow language implementations to simulate "open classes"; a host class in effect gets
 923 // new code when an anonymous class is loaded alongside it.  A less convenient but more
 924 // standard way to do this is with reflection, which can also be set to ignore access
 925 // restrictions.
 926 
 927 // Access into an anonymous class is possible only through reflection.  Therefore, there
 928 // are no special access rules for calling into an anonymous class.  The relaxed access
 929 // rule for the host class is applied in the opposite direction:  A host class reflectively
 930 // access one of its anonymous classes.
 931 
 932 // If you load the same bytecodes twice, you get two different classes.  You can reload
 933 // the same bytecodes with or without varying CP patches.
 934 
 935 // By using the CP patching array, you can have a new anonymous class U2 refer to an older one U1.
 936 // The bytecodes for U2 should refer to U1 by a symbolic name (doesn't matter what the name is).
 937 // The CONSTANT_Class entry for that name can be patched to refer directly to U1.
 938 
 939 // This allows, for example, U2 to use U1 as a superclass or super-interface, or as
 940 // an outer class (so that U2 is an anonymous inner class of anonymous U1).
 941 // It is not possible for a named class, or an older anonymous class, to refer by
 942 // name (via its CP) to a newer anonymous class.
 943 
 944 // CP patching may also be used to modify (i.e., hack) the names of methods, classes,
 945 // or type descriptors used in the loaded anonymous class.
 946 
 947 // Finally, CP patching may be used to introduce "live" objects into the constant pool,
 948 // instead of "dead" strings.  A compiled statement like println((Object)"hello") can
 949 // be changed to println(greeting), where greeting is an arbitrary object created before
 950 // the anonymous class is loaded.  This is useful in dynamic languages, in which
 951 // various kinds of metaobjects must be introduced as constants into bytecode.
 952 // Note the cast (Object), which tells the verifier to expect an arbitrary object,
 953 // not just a literal string.  For such ldc instructions, the verifier uses the
 954 // type Object instead of String, if the loaded constant is not in fact a String.
 955 
 956 static instanceKlassHandle
 957 Unsafe_DefineAnonymousClass_impl(JNIEnv *env,
 958                                  jclass host_class, jbyteArray data, jobjectArray cp_patches_jh,
 959                                  HeapWord* *temp_alloc,
 960                                  TRAPS) {
 961 
 962   if (UsePerfData) {
 963     ClassLoader::unsafe_defineClassCallCounter()->inc();
 964   }
 965 
 966   if (data == NULL) {
 967     THROW_0(vmSymbols::java_lang_NullPointerException());
 968   }
 969 
 970   jint length = typeArrayOop(JNIHandles::resolve_non_null(data))->length();
 971   jint word_length = (length + sizeof(HeapWord)-1) / sizeof(HeapWord);
 972   HeapWord* body = NEW_C_HEAP_ARRAY(HeapWord, word_length, mtInternal);
 973   if (body == NULL) {
 974     THROW_0(vmSymbols::java_lang_OutOfMemoryError());
 975   }
 976 
 977   // caller responsible to free it:
 978   (*temp_alloc) = body;
 979 
 980   {
 981     jbyte* array_base = typeArrayOop(JNIHandles::resolve_non_null(data))->byte_at_addr(0);
 982     Copy::conjoint_words((HeapWord*) array_base, body, word_length);
 983   }
 984 
 985   u1* class_bytes = (u1*) body;
 986   int class_bytes_length = (int) length;
 987   if (class_bytes_length < 0)  class_bytes_length = 0;
 988   if (class_bytes == NULL
 989       || host_class == NULL
 990       || length != class_bytes_length)
 991     THROW_0(vmSymbols::java_lang_IllegalArgumentException());
 992 
 993   objArrayHandle cp_patches_h;
 994   if (cp_patches_jh != NULL) {
 995     oop p = JNIHandles::resolve_non_null(cp_patches_jh);
 996     if (!p->is_objArray())
 997       THROW_0(vmSymbols::java_lang_IllegalArgumentException());
 998     cp_patches_h = objArrayHandle(THREAD, (objArrayOop)p);
 999   }
1000 
1001   const Klass* host_klass = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(host_class));
1002   assert(host_klass != NULL, "invariant");
1003 
1004   const char* host_source = host_klass->external_name();
1005   Handle      host_loader(THREAD, host_klass->class_loader());
1006   Handle      host_domain(THREAD, host_klass->protection_domain());
1007 
1008   GrowableArray<Handle>* cp_patches = NULL;
1009   if (cp_patches_h.not_null()) {
1010     int alen = cp_patches_h->length();
1011     for (int i = alen-1; i >= 0; i--) {
1012       oop p = cp_patches_h->obj_at(i);
1013       if (p != NULL) {
1014         Handle patch(THREAD, p);
1015         if (cp_patches == NULL)
1016           cp_patches = new GrowableArray<Handle>(i+1, i+1, Handle());
1017         cp_patches->at_put(i, patch);
1018       }
1019     }
1020   }
1021 
1022   ClassFileStream st(class_bytes,
1023                      class_bytes_length,
1024                      host_source,
1025                      ClassFileStream::verify);
1026 
1027   instanceKlassHandle anon_klass;
1028   {
1029     Symbol* no_class_name = NULL;
1030     Klass* anonk = SystemDictionary::parse_stream(no_class_name,
1031                                                   host_loader,
1032                                                   host_domain,
1033                                                   &st,
1034                                                   host_klass,
1035                                                   cp_patches,
1036                                                   CHECK_NULL);
1037     if (anonk == NULL)  return NULL;
1038     anon_klass = instanceKlassHandle(THREAD, anonk);
1039   }
1040 
1041   return anon_klass;
1042 }
1043 
1044 UNSAFE_ENTRY(jclass, Unsafe_DefineAnonymousClass(JNIEnv *env, jobject unsafe, jclass host_class, jbyteArray data, jobjectArray cp_patches_jh))
1045 {
1046   instanceKlassHandle anon_klass;
1047   jobject res_jh = NULL;
1048 
1049   UnsafeWrapper("Unsafe_DefineAnonymousClass");
1050   ResourceMark rm(THREAD);
1051 
1052   HeapWord* temp_alloc = NULL;
1053 
1054   anon_klass = Unsafe_DefineAnonymousClass_impl(env, host_class, data,
1055                                                 cp_patches_jh,
1056                                                    &temp_alloc, THREAD);
1057   if (anon_klass() != NULL)
1058     res_jh = JNIHandles::make_local(env, anon_klass->java_mirror());
1059 
1060   // try/finally clause:
1061   if (temp_alloc != NULL) {
1062     FREE_C_HEAP_ARRAY(HeapWord, temp_alloc);
1063   }
1064 
1065   // The anonymous class loader data has been artificially been kept alive to
1066   // this point.   The mirror and any instances of this class have to keep
1067   // it alive afterwards.
1068   if (anon_klass() != NULL) {
1069     anon_klass->class_loader_data()->set_keep_alive(false);
1070   }
1071 
1072   // let caller initialize it as needed...
1073 
1074   return (jclass) res_jh;
1075 }
1076 UNSAFE_END
1077 
1078 
1079 
1080 UNSAFE_ENTRY(void, Unsafe_ThrowException(JNIEnv *env, jobject unsafe, jthrowable thr))
1081   UnsafeWrapper("Unsafe_ThrowException");
1082   {
1083     ThreadToNativeFromVM ttnfv(thread);
1084     env->Throw(thr);
1085   }
1086 UNSAFE_END
1087 
1088 // JSR166 ------------------------------------------------------------------
1089 
1090 UNSAFE_ENTRY(jboolean, Unsafe_CompareAndSwapObject(JNIEnv *env, jobject unsafe, jobject obj, jlong offset, jobject e_h, jobject x_h))
1091   UnsafeWrapper("Unsafe_CompareAndSwapObject");
1092   oop x = JNIHandles::resolve(x_h);
1093   oop e = JNIHandles::resolve(e_h);
1094   oop p = JNIHandles::resolve(obj);
1095   HeapWord* addr = (HeapWord *)index_oop_from_field_offset_long(p, offset);
1096   oop res = oopDesc::atomic_compare_exchange_oop(x, addr, e, true);
1097   jboolean success  = (res == e);
1098   if (success)
1099     update_barrier_set((void*)addr, x);
1100   return success;
1101 UNSAFE_END
1102 
1103 UNSAFE_ENTRY(jboolean, Unsafe_CompareAndSwapInt(JNIEnv *env, jobject unsafe, jobject obj, jlong offset, jint e, jint x))
1104   UnsafeWrapper("Unsafe_CompareAndSwapInt");
1105   oop p = JNIHandles::resolve(obj);
1106   jint* addr = (jint *) index_oop_from_field_offset_long(p, offset);
1107   return (jint)(Atomic::cmpxchg(x, addr, e)) == e;
1108 UNSAFE_END
1109 
1110 UNSAFE_ENTRY(jboolean, Unsafe_CompareAndSwapLong(JNIEnv *env, jobject unsafe, jobject obj, jlong offset, jlong e, jlong x))
1111   UnsafeWrapper("Unsafe_CompareAndSwapLong");
1112   Handle p (THREAD, JNIHandles::resolve(obj));
1113   jlong* addr = (jlong*)(index_oop_from_field_offset_long(p(), offset));
1114 #ifdef SUPPORTS_NATIVE_CX8
1115   return (jlong)(Atomic::cmpxchg(x, addr, e)) == e;
1116 #else
1117   if (VM_Version::supports_cx8())
1118     return (jlong)(Atomic::cmpxchg(x, addr, e)) == e;
1119   else {
1120     jboolean success = false;
1121     MutexLockerEx mu(UnsafeJlong_lock, Mutex::_no_safepoint_check_flag);
1122     jlong val = Atomic::load(addr);
1123     if (val == e) { Atomic::store(x, addr); success = true; }
1124     return success;
1125   }
1126 #endif
1127 UNSAFE_END
1128 
1129 UNSAFE_ENTRY(void, Unsafe_Park(JNIEnv *env, jobject unsafe, jboolean isAbsolute, jlong time))
1130   UnsafeWrapper("Unsafe_Park");
1131   EventThreadPark event;
1132   HOTSPOT_THREAD_PARK_BEGIN((uintptr_t) thread->parker(), (int) isAbsolute, time);
1133 
1134   JavaThreadParkedState jtps(thread, time != 0);
1135   thread->parker()->park(isAbsolute != 0, time);
1136 
1137   HOTSPOT_THREAD_PARK_END((uintptr_t) thread->parker());
1138   if (event.should_commit()) {
1139     oop obj = thread->current_park_blocker();
1140     event.set_klass((obj != NULL) ? obj->klass() : NULL);
1141     event.set_timeout(time);
1142     event.set_address((obj != NULL) ? (TYPE_ADDRESS) cast_from_oop<uintptr_t>(obj) : 0);
1143     event.commit();
1144   }
1145 UNSAFE_END
1146 
1147 UNSAFE_ENTRY(void, Unsafe_Unpark(JNIEnv *env, jobject unsafe, jobject jthread))
1148   UnsafeWrapper("Unsafe_Unpark");
1149   Parker* p = NULL;
1150   if (jthread != NULL) {
1151     oop java_thread = JNIHandles::resolve_non_null(jthread);
1152     if (java_thread != NULL) {
1153       jlong lp = java_lang_Thread::park_event(java_thread);
1154       if (lp != 0) {
1155         // This cast is OK even though the jlong might have been read
1156         // non-atomically on 32bit systems, since there, one word will
1157         // always be zero anyway and the value set is always the same
1158         p = (Parker*)addr_from_java(lp);
1159       } else {
1160         // Grab lock if apparently null or using older version of library
1161         MutexLocker mu(Threads_lock);
1162         java_thread = JNIHandles::resolve_non_null(jthread);
1163         if (java_thread != NULL) {
1164           JavaThread* thr = java_lang_Thread::thread(java_thread);
1165           if (thr != NULL) {
1166             p = thr->parker();
1167             if (p != NULL) { // Bind to Java thread for next time.
1168               java_lang_Thread::set_park_event(java_thread, addr_to_java(p));
1169             }
1170           }
1171         }
1172       }
1173     }
1174   }
1175   if (p != NULL) {
1176     HOTSPOT_THREAD_UNPARK((uintptr_t) p);
1177     p->unpark();
1178   }
1179 UNSAFE_END
1180 
1181 UNSAFE_ENTRY(jint, Unsafe_Loadavg(JNIEnv *env, jobject unsafe, jdoubleArray loadavg, jint nelem))
1182   UnsafeWrapper("Unsafe_Loadavg");
1183   const int max_nelem = 3;
1184   double la[max_nelem];
1185   jint ret;
1186 
1187   typeArrayOop a = typeArrayOop(JNIHandles::resolve_non_null(loadavg));
1188   assert(a->is_typeArray(), "must be type array");
1189 
1190   if (nelem < 0 || nelem > max_nelem || a->length() < nelem) {
1191     ThreadToNativeFromVM ttnfv(thread);
1192     throw_new(env, "ArrayIndexOutOfBoundsException");
1193     return -1;
1194   }
1195 
1196   ret = os::loadavg(la, nelem);
1197   if (ret == -1) return -1;
1198 
1199   // if successful, ret is the number of samples actually retrieved.
1200   assert(ret >= 0 && ret <= max_nelem, "Unexpected loadavg return value");
1201   switch(ret) {
1202     case 3: a->double_at_put(2, (jdouble)la[2]); // fall through
1203     case 2: a->double_at_put(1, (jdouble)la[1]); // fall through
1204     case 1: a->double_at_put(0, (jdouble)la[0]); break;
1205   }
1206   return ret;
1207 UNSAFE_END
1208 
1209 
1210 /// JVM_RegisterUnsafeMethods
1211 
1212 #define ADR "J"
1213 
1214 #define LANG "Ljava/lang/"
1215 
1216 #define OBJ LANG "Object;"
1217 #define CLS LANG "Class;"
1218 #define FLD LANG "reflect/Field;"
1219 #define THR LANG "Throwable;"
1220 
1221 #define DC_Args  LANG "String;[BII" LANG "ClassLoader;" "Ljava/security/ProtectionDomain;"
1222 #define DAC_Args CLS "[B[" OBJ
1223 
1224 #define CC (char*)  /*cast a literal from (const char*)*/
1225 #define FN_PTR(f) CAST_FROM_FN_PTR(void*, &f)
1226 
1227 #define DECLARE_GETPUTOOP(Boolean, Z) \
1228     {CC "get" #Boolean,      CC "(" OBJ "J)" #Z,       FN_PTR(Unsafe_Get##Boolean)}, \
1229     {CC "put" #Boolean,      CC "(" OBJ "J" #Z ")V",   FN_PTR(Unsafe_Set##Boolean)}, \
1230     {CC "get" #Boolean "Volatile",      CC "(" OBJ "J)" #Z,       FN_PTR(Unsafe_Get##Boolean##Volatile)}, \
1231     {CC "put" #Boolean "Volatile",      CC "(" OBJ "J" #Z ")V",   FN_PTR(Unsafe_Set##Boolean##Volatile)}
1232 
1233 
1234 #define DECLARE_GETPUTNATIVE(Byte, B) \
1235     {CC "get" #Byte,         CC "(" ADR ")" #B,       FN_PTR(Unsafe_GetNative##Byte)}, \
1236     {CC "put" #Byte,         CC "(" ADR#B ")V",       FN_PTR(Unsafe_SetNative##Byte)}
1237 
1238 
1239 static JNINativeMethod sun_misc_Unsafe_methods[] = {
1240     {CC "getObject",        CC "(" OBJ "J)" OBJ "",   FN_PTR(Unsafe_GetObject)},
1241     {CC "putObject",        CC "(" OBJ "J" OBJ ")V",  FN_PTR(Unsafe_SetObject)},
1242     {CC "getObjectVolatile",CC "(" OBJ "J)" OBJ "",   FN_PTR(Unsafe_GetObjectVolatile)},
1243     {CC "putObjectVolatile",CC "(" OBJ "J" OBJ ")V",  FN_PTR(Unsafe_SetObjectVolatile)},
1244 
1245     {CC "getUncompressedObject", CC "(" ADR ")" OBJ,  FN_PTR(Unsafe_GetUncompressedObject)},
1246     {CC "getJavaMirror",         CC "(" ADR ")" CLS,  FN_PTR(Unsafe_GetJavaMirror)},
1247     {CC "getKlassPointer",       CC "(" OBJ ")" ADR,  FN_PTR(Unsafe_GetKlassPointer)},
1248 
1249     DECLARE_GETPUTOOP(Boolean, Z),
1250     DECLARE_GETPUTOOP(Byte, B),
1251     DECLARE_GETPUTOOP(Short, S),
1252     DECLARE_GETPUTOOP(Char, C),
1253     DECLARE_GETPUTOOP(Int, I),
1254     DECLARE_GETPUTOOP(Long, J),
1255     DECLARE_GETPUTOOP(Float, F),
1256     DECLARE_GETPUTOOP(Double, D),
1257 
1258     DECLARE_GETPUTNATIVE(Byte, B),
1259     DECLARE_GETPUTNATIVE(Short, S),
1260     DECLARE_GETPUTNATIVE(Char, C),
1261     DECLARE_GETPUTNATIVE(Int, I),
1262     DECLARE_GETPUTNATIVE(Long, J),
1263     DECLARE_GETPUTNATIVE(Float, F),
1264     DECLARE_GETPUTNATIVE(Double, D),
1265 
1266     {CC "getAddress",         CC "(" ADR ")" ADR,        FN_PTR(Unsafe_GetNativeAddress)},
1267     {CC "putAddress",         CC "(" ADR "" ADR ")V",    FN_PTR(Unsafe_SetNativeAddress)},
1268 
1269     {CC "allocateMemory",     CC "(J)" ADR,              FN_PTR(Unsafe_AllocateMemory)},
1270     {CC "reallocateMemory",   CC "(" ADR "J)" ADR,       FN_PTR(Unsafe_ReallocateMemory)},
1271     {CC "freeMemory",         CC "(" ADR ")V",           FN_PTR(Unsafe_FreeMemory)},
1272 
1273     {CC "objectFieldOffset",  CC "(" FLD ")J",           FN_PTR(Unsafe_ObjectFieldOffset)},
1274     {CC "staticFieldOffset",  CC "(" FLD ")J",           FN_PTR(Unsafe_StaticFieldOffset)},
1275     {CC "staticFieldBase",    CC "(" FLD ")" OBJ,        FN_PTR(Unsafe_StaticFieldBaseFromField)},
1276     {CC "ensureClassInitialized",CC "(" CLS ")V",        FN_PTR(Unsafe_EnsureClassInitialized)},
1277     {CC "arrayBaseOffset",    CC "(" CLS ")I",           FN_PTR(Unsafe_ArrayBaseOffset)},
1278     {CC "arrayIndexScale",    CC "(" CLS ")I",           FN_PTR(Unsafe_ArrayIndexScale)},
1279     {CC "addressSize",        CC "()I",                  FN_PTR(Unsafe_AddressSize)},
1280     {CC "pageSize",           CC "()I",                  FN_PTR(Unsafe_PageSize)},
1281 
1282     {CC "defineClass",        CC "(" DC_Args ")" CLS,    FN_PTR(Unsafe_DefineClass)},
1283     {CC "allocateInstance",   CC "(" CLS ")" OBJ,        FN_PTR(Unsafe_AllocateInstance)},
1284     {CC "throwException",     CC "(" THR ")V",           FN_PTR(Unsafe_ThrowException)},
1285     {CC "compareAndSwapObject", CC "(" OBJ "J" OBJ "" OBJ ")Z", FN_PTR(Unsafe_CompareAndSwapObject)},
1286     {CC "compareAndSwapInt",  CC "(" OBJ "J""I""I"")Z",  FN_PTR(Unsafe_CompareAndSwapInt)},
1287     {CC "compareAndSwapLong", CC "(" OBJ "J""J""J"")Z",  FN_PTR(Unsafe_CompareAndSwapLong)},
1288     {CC "putOrderedObject",   CC "(" OBJ "J" OBJ ")V",   FN_PTR(Unsafe_SetOrderedObject)},
1289     {CC "putOrderedInt",      CC "(" OBJ "JI)V",         FN_PTR(Unsafe_SetOrderedInt)},
1290     {CC "putOrderedLong",     CC "(" OBJ "JJ)V",         FN_PTR(Unsafe_SetOrderedLong)},
1291     {CC "park",               CC "(ZJ)V",                FN_PTR(Unsafe_Park)},
1292     {CC "unpark",             CC "(" OBJ ")V",           FN_PTR(Unsafe_Unpark)},
1293 
1294     {CC "getLoadAverage",     CC "([DI)I",               FN_PTR(Unsafe_Loadavg)},
1295 
1296     {CC "copyMemory",         CC "(" OBJ "J" OBJ "JJ)V", FN_PTR(Unsafe_CopyMemory)},
1297     {CC "setMemory",          CC "(" OBJ "JJB)V",        FN_PTR(Unsafe_SetMemory)},
1298 
1299     {CC "defineAnonymousClass", CC "(" DAC_Args ")" CLS, FN_PTR(Unsafe_DefineAnonymousClass)},
1300 
1301     {CC "shouldBeInitialized",CC "(" CLS ")Z",           FN_PTR(Unsafe_ShouldBeInitialized)},
1302 
1303     {CC "loadFence",          CC "()V",                  FN_PTR(Unsafe_LoadFence)},
1304     {CC "storeFence",         CC "()V",                  FN_PTR(Unsafe_StoreFence)},
1305     {CC "fullFence",          CC "()V",                  FN_PTR(Unsafe_FullFence)},
1306 };
1307 
1308 static JNINativeMethod jdk_internal_misc_Unsafe_methods[] = {
1309     {CC "getObject",        CC "(" OBJ "J)" OBJ "",   FN_PTR(Unsafe_GetObject)},
1310     {CC "putObject",        CC "(" OBJ "J" OBJ ")V",  FN_PTR(Unsafe_SetObject)},
1311     {CC "getObjectVolatile",CC "(" OBJ "J)" OBJ "",   FN_PTR(Unsafe_GetObjectVolatile)},
1312     {CC "putObjectVolatile",CC "(" OBJ "J" OBJ ")V",  FN_PTR(Unsafe_SetObjectVolatile)},
1313 
1314     {CC "getUncompressedObject", CC "(" ADR ")" OBJ,  FN_PTR(Unsafe_GetUncompressedObject)},
1315     {CC "getJavaMirror",         CC "(" ADR ")" CLS,  FN_PTR(Unsafe_GetJavaMirror)},
1316     {CC "getKlassPointer",       CC "(" OBJ ")" ADR,  FN_PTR(Unsafe_GetKlassPointer)},
1317 
1318     DECLARE_GETPUTOOP(Boolean, Z),
1319     DECLARE_GETPUTOOP(Byte, B),
1320     DECLARE_GETPUTOOP(Short, S),
1321     DECLARE_GETPUTOOP(Char, C),
1322     DECLARE_GETPUTOOP(Int, I),
1323     DECLARE_GETPUTOOP(Long, J),
1324     DECLARE_GETPUTOOP(Float, F),
1325     DECLARE_GETPUTOOP(Double, D),
1326 
1327     DECLARE_GETPUTNATIVE(Byte, B),
1328     DECLARE_GETPUTNATIVE(Short, S),
1329     DECLARE_GETPUTNATIVE(Char, C),
1330     DECLARE_GETPUTNATIVE(Int, I),
1331     DECLARE_GETPUTNATIVE(Long, J),
1332     DECLARE_GETPUTNATIVE(Float, F),
1333     DECLARE_GETPUTNATIVE(Double, D),
1334 
1335     {CC "getAddress",         CC "(" ADR ")" ADR,        FN_PTR(Unsafe_GetNativeAddress)},
1336     {CC "putAddress",         CC "(" ADR "" ADR ")V",    FN_PTR(Unsafe_SetNativeAddress)},
1337 
1338     {CC "allocateMemory",     CC "(J)" ADR,              FN_PTR(Unsafe_AllocateMemory)},
1339     {CC "reallocateMemory",   CC "(" ADR "J)" ADR,       FN_PTR(Unsafe_ReallocateMemory)},
1340     {CC "freeMemory",         CC "(" ADR ")V",           FN_PTR(Unsafe_FreeMemory)},
1341 
1342     {CC "objectFieldOffset",  CC "(" FLD ")J",           FN_PTR(Unsafe_ObjectFieldOffset)},
1343     {CC "staticFieldOffset",  CC "(" FLD ")J",           FN_PTR(Unsafe_StaticFieldOffset)},
1344     {CC "staticFieldBase",    CC "(" FLD ")" OBJ,        FN_PTR(Unsafe_StaticFieldBaseFromField)},
1345     {CC "ensureClassInitialized",CC "(" CLS ")V",        FN_PTR(Unsafe_EnsureClassInitialized)},
1346     {CC "arrayBaseOffset",    CC "(" CLS ")I",           FN_PTR(Unsafe_ArrayBaseOffset)},
1347     {CC "arrayIndexScale",    CC "(" CLS ")I",           FN_PTR(Unsafe_ArrayIndexScale)},
1348     {CC "addressSize",        CC "()I",                  FN_PTR(Unsafe_AddressSize)},
1349     {CC "pageSize",           CC "()I",                  FN_PTR(Unsafe_PageSize)},
1350 
1351     {CC "defineClass",        CC "(" DC_Args ")" CLS,    FN_PTR(Unsafe_DefineClass)},
1352     {CC "allocateInstance",   CC "(" CLS ")" OBJ,        FN_PTR(Unsafe_AllocateInstance)},
1353     {CC "throwException",     CC "(" THR ")V",           FN_PTR(Unsafe_ThrowException)},
1354     {CC "compareAndSwapObject", CC "(" OBJ "J" OBJ "" OBJ ")Z", FN_PTR(Unsafe_CompareAndSwapObject)},
1355     {CC "compareAndSwapInt",  CC "(" OBJ "J""I""I"")Z",  FN_PTR(Unsafe_CompareAndSwapInt)},
1356     {CC "compareAndSwapLong", CC "(" OBJ "J""J""J"")Z",  FN_PTR(Unsafe_CompareAndSwapLong)},
1357     {CC "putOrderedObject",   CC "(" OBJ "J" OBJ ")V",   FN_PTR(Unsafe_SetOrderedObject)},
1358     {CC "putOrderedInt",      CC "(" OBJ "JI)V",         FN_PTR(Unsafe_SetOrderedInt)},
1359     {CC "putOrderedLong",     CC "(" OBJ "JJ)V",         FN_PTR(Unsafe_SetOrderedLong)},
1360     {CC "park",               CC "(ZJ)V",                FN_PTR(Unsafe_Park)},
1361     {CC "unpark",             CC "(" OBJ ")V",           FN_PTR(Unsafe_Unpark)},
1362 
1363     {CC "getLoadAverage",     CC "([DI)I",               FN_PTR(Unsafe_Loadavg)},
1364 
1365     {CC "copyMemory",         CC "(" OBJ "J" OBJ "JJ)V", FN_PTR(Unsafe_CopyMemory)},
1366     {CC "setMemory",          CC "(" OBJ "JJB)V",        FN_PTR(Unsafe_SetMemory)},
1367 
1368     {CC "defineAnonymousClass", CC "(" DAC_Args ")" CLS, FN_PTR(Unsafe_DefineAnonymousClass)},
1369 
1370     {CC "shouldBeInitialized",CC "(" CLS ")Z",           FN_PTR(Unsafe_ShouldBeInitialized)},
1371 
1372     {CC "loadFence",          CC "()V",                  FN_PTR(Unsafe_LoadFence)},
1373     {CC "storeFence",         CC "()V",                  FN_PTR(Unsafe_StoreFence)},
1374     {CC "fullFence",          CC "()V",                  FN_PTR(Unsafe_FullFence)},
1375 
1376     {CC "isBigEndian0",       CC "()Z",                  FN_PTR(Unsafe_isBigEndian0)},
1377     {CC "unalignedAccess0",   CC "()Z",                  FN_PTR(Unsafe_unalignedAccess0)}
1378 };
1379 
1380 #undef CC
1381 #undef FN_PTR
1382 
1383 #undef ADR
1384 #undef LANG
1385 #undef OBJ
1386 #undef CLS
1387 #undef FLD
1388 #undef THR
1389 #undef DC_Args
1390 #undef DAC_Args
1391 
1392 #undef DECLARE_GETPUTOOP
1393 #undef DECLARE_GETPUTNATIVE
1394 
1395 
1396 // These two functions are exported, used by NativeLookup.
1397 // The Unsafe_xxx functions above are called only from the interpreter.
1398 // The optimizer looks at names and signatures to recognize
1399 // individual functions.
1400 
1401 JVM_ENTRY(void, JVM_RegisterSunMiscUnsafeMethods(JNIEnv *env, jclass unsafeclass))
1402   UnsafeWrapper("JVM_RegisterSunMiscUnsafeMethods");
1403   {
1404     ThreadToNativeFromVM ttnfv(thread);
1405 
1406     int ok = env->RegisterNatives(unsafeclass, sun_misc_Unsafe_methods, sizeof(sun_misc_Unsafe_methods)/sizeof(JNINativeMethod));
1407     guarantee(ok == 0, "register sun.misc.Unsafe natives");
1408   }
1409 JVM_END
1410 
1411 JVM_ENTRY(void, JVM_RegisterJDKInternalMiscUnsafeMethods(JNIEnv *env, jclass unsafeclass))
1412   UnsafeWrapper("JVM_RegisterJDKInternalMiscUnsafeMethods");
1413   {
1414     ThreadToNativeFromVM ttnfv(thread);
1415 
1416     int ok = env->RegisterNatives(unsafeclass, jdk_internal_misc_Unsafe_methods, sizeof(jdk_internal_misc_Unsafe_methods)/sizeof(JNINativeMethod));
1417     guarantee(ok == 0, "register jdk.internal.misc.Unsafe natives");
1418   }
1419 JVM_END