1 /*
   2  * Copyright (c) 2000, 2016, 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 // This function is a leaf since if the source and destination are both in native memory
 664 // the copy may potentially be very large, and we don't want to disable GC if we can avoid it.
 665 // If either source or destination (or both) are on the heap, the function will enter VM using
 666 // JVM_ENTRY_FROM_LEAF
 667 JVM_LEAF(void, Unsafe_CopySwapMemory0(JNIEnv *env, jobject unsafe, jobject srcObj, jlong srcOffset, jobject dstObj, jlong dstOffset, jlong size, jlong elemSize)) {
 668   UnsafeWrapper("Unsafe_CopySwapMemory0");
 669 
 670   size_t sz = (size_t)size;
 671   size_t esz = (size_t)elemSize;
 672 
 673   if (srcObj == NULL && dstObj == NULL) {
 674     // Both src & dst are in native memory
 675     address src = (address)srcOffset;
 676     address dst = (address)dstOffset;
 677 
 678     Copy::conjoint_swap(src, dst, sz, esz);
 679   } else {
 680     // At least one of src/dst are on heap, transition to VM to access raw pointers
 681 
 682     JVM_ENTRY_FROM_LEAF(env, void, Unsafe_CopySwapMemory0) {
 683       oop srcp = JNIHandles::resolve(srcObj);
 684       oop dstp = JNIHandles::resolve(dstObj);
 685 
 686       address src = (address)index_oop_from_field_offset_long(srcp, srcOffset);
 687       address dst = (address)index_oop_from_field_offset_long(dstp, dstOffset);
 688 
 689       Copy::conjoint_swap(src, dst, sz, esz);
 690     } JVM_END
 691   }
 692 } JVM_END
 693 
 694 ////// Random queries
 695 
 696 // See comment at file start about UNSAFE_LEAF
 697 //UNSAFE_LEAF(jint, Unsafe_AddressSize())
 698 UNSAFE_ENTRY(jint, Unsafe_AddressSize(JNIEnv *env, jobject unsafe))
 699   UnsafeWrapper("Unsafe_AddressSize");
 700   return sizeof(void*);
 701 UNSAFE_END
 702 
 703 // See comment at file start about UNSAFE_LEAF
 704 //UNSAFE_LEAF(jint, Unsafe_PageSize())
 705 UNSAFE_ENTRY(jint, Unsafe_PageSize(JNIEnv *env, jobject unsafe))
 706   UnsafeWrapper("Unsafe_PageSize");
 707   return os::vm_page_size();
 708 UNSAFE_END
 709 
 710 jint find_field_offset(jobject field, int must_be_static, TRAPS) {
 711   if (field == NULL) {
 712     THROW_0(vmSymbols::java_lang_NullPointerException());
 713   }
 714 
 715   oop reflected   = JNIHandles::resolve_non_null(field);
 716   oop mirror      = java_lang_reflect_Field::clazz(reflected);
 717   Klass* k      = java_lang_Class::as_Klass(mirror);
 718   int slot        = java_lang_reflect_Field::slot(reflected);
 719   int modifiers   = java_lang_reflect_Field::modifiers(reflected);
 720 
 721   if (must_be_static >= 0) {
 722     int really_is_static = ((modifiers & JVM_ACC_STATIC) != 0);
 723     if (must_be_static != really_is_static) {
 724       THROW_0(vmSymbols::java_lang_IllegalArgumentException());
 725     }
 726   }
 727 
 728   int offset = InstanceKlass::cast(k)->field_offset(slot);
 729   return field_offset_from_byte_offset(offset);
 730 }
 731 
 732 UNSAFE_ENTRY(jlong, Unsafe_ObjectFieldOffset(JNIEnv *env, jobject unsafe, jobject field))
 733   UnsafeWrapper("Unsafe_ObjectFieldOffset");
 734   return find_field_offset(field, 0, THREAD);
 735 UNSAFE_END
 736 
 737 UNSAFE_ENTRY(jlong, Unsafe_StaticFieldOffset(JNIEnv *env, jobject unsafe, jobject field))
 738   UnsafeWrapper("Unsafe_StaticFieldOffset");
 739   return find_field_offset(field, 1, THREAD);
 740 UNSAFE_END
 741 
 742 UNSAFE_ENTRY(jobject, Unsafe_StaticFieldBaseFromField(JNIEnv *env, jobject unsafe, jobject field))
 743   UnsafeWrapper("Unsafe_StaticFieldBase");
 744   // Note:  In this VM implementation, a field address is always a short
 745   // offset from the base of a a klass metaobject.  Thus, the full dynamic
 746   // range of the return type is never used.  However, some implementations
 747   // might put the static field inside an array shared by many classes,
 748   // or even at a fixed address, in which case the address could be quite
 749   // large.  In that last case, this function would return NULL, since
 750   // the address would operate alone, without any base pointer.
 751 
 752   if (field == NULL)  THROW_0(vmSymbols::java_lang_NullPointerException());
 753 
 754   oop reflected   = JNIHandles::resolve_non_null(field);
 755   oop mirror      = java_lang_reflect_Field::clazz(reflected);
 756   int modifiers   = java_lang_reflect_Field::modifiers(reflected);
 757 
 758   if ((modifiers & JVM_ACC_STATIC) == 0) {
 759     THROW_0(vmSymbols::java_lang_IllegalArgumentException());
 760   }
 761 
 762   return JNIHandles::make_local(env, mirror);
 763 UNSAFE_END
 764 
 765 UNSAFE_ENTRY(void, Unsafe_EnsureClassInitialized(JNIEnv *env, jobject unsafe, jobject clazz)) {
 766   UnsafeWrapper("Unsafe_EnsureClassInitialized");
 767   if (clazz == NULL) {
 768     THROW(vmSymbols::java_lang_NullPointerException());
 769   }
 770   oop mirror = JNIHandles::resolve_non_null(clazz);
 771 
 772   Klass* klass = java_lang_Class::as_Klass(mirror);
 773   if (klass != NULL && klass->should_be_initialized()) {
 774     InstanceKlass* k = InstanceKlass::cast(klass);
 775     k->initialize(CHECK);
 776   }
 777 }
 778 UNSAFE_END
 779 
 780 UNSAFE_ENTRY(jboolean, Unsafe_ShouldBeInitialized(JNIEnv *env, jobject unsafe, jobject clazz)) {
 781   UnsafeWrapper("Unsafe_ShouldBeInitialized");
 782   if (clazz == NULL) {
 783     THROW_(vmSymbols::java_lang_NullPointerException(), false);
 784   }
 785   oop mirror = JNIHandles::resolve_non_null(clazz);
 786   Klass* klass = java_lang_Class::as_Klass(mirror);
 787   if (klass != NULL && klass->should_be_initialized()) {
 788     return true;
 789   }
 790   return false;
 791 }
 792 UNSAFE_END
 793 
 794 static void getBaseAndScale(int& base, int& scale, jclass acls, TRAPS) {
 795   if (acls == NULL) {
 796     THROW(vmSymbols::java_lang_NullPointerException());
 797   }
 798   oop      mirror = JNIHandles::resolve_non_null(acls);
 799   Klass* k      = java_lang_Class::as_Klass(mirror);
 800   if (k == NULL || !k->is_array_klass()) {
 801     THROW(vmSymbols::java_lang_InvalidClassException());
 802   } else if (k->is_objArray_klass()) {
 803     base  = arrayOopDesc::base_offset_in_bytes(T_OBJECT);
 804     scale = heapOopSize;
 805   } else if (k->is_typeArray_klass()) {
 806     TypeArrayKlass* tak = TypeArrayKlass::cast(k);
 807     base  = tak->array_header_in_bytes();
 808     assert(base == arrayOopDesc::base_offset_in_bytes(tak->element_type()), "array_header_size semantics ok");
 809     scale = (1 << tak->log2_element_size());
 810   } else {
 811     ShouldNotReachHere();
 812   }
 813 }
 814 
 815 UNSAFE_ENTRY(jint, Unsafe_ArrayBaseOffset(JNIEnv *env, jobject unsafe, jclass acls))
 816   UnsafeWrapper("Unsafe_ArrayBaseOffset");
 817   int base = 0, scale = 0;
 818   getBaseAndScale(base, scale, acls, CHECK_0);
 819   return field_offset_from_byte_offset(base);
 820 UNSAFE_END
 821 
 822 
 823 UNSAFE_ENTRY(jint, Unsafe_ArrayIndexScale(JNIEnv *env, jobject unsafe, jclass acls))
 824   UnsafeWrapper("Unsafe_ArrayIndexScale");
 825   int base = 0, scale = 0;
 826   getBaseAndScale(base, scale, acls, CHECK_0);
 827   // This VM packs both fields and array elements down to the byte.
 828   // But watch out:  If this changes, so that array references for
 829   // a given primitive type (say, T_BOOLEAN) use different memory units
 830   // than fields, this method MUST return zero for such arrays.
 831   // For example, the VM used to store sub-word sized fields in full
 832   // words in the object layout, so that accessors like getByte(Object,int)
 833   // did not really do what one might expect for arrays.  Therefore,
 834   // this function used to report a zero scale factor, so that the user
 835   // would know not to attempt to access sub-word array elements.
 836   // // Code for unpacked fields:
 837   // if (scale < wordSize)  return 0;
 838 
 839   // The following allows for a pretty general fieldOffset cookie scheme,
 840   // but requires it to be linear in byte offset.
 841   return field_offset_from_byte_offset(scale) - field_offset_from_byte_offset(0);
 842 UNSAFE_END
 843 
 844 
 845 static inline void throw_new(JNIEnv *env, const char *ename) {
 846   char buf[100];
 847   jio_snprintf(buf, 100, "%s%s", "java/lang/", ename);
 848   jclass cls = env->FindClass(buf);
 849   if (env->ExceptionCheck()) {
 850     env->ExceptionClear();
 851     tty->print_cr("Unsafe: cannot throw %s because FindClass has failed", buf);
 852     return;
 853   }
 854   char* msg = NULL;
 855   env->ThrowNew(cls, msg);
 856 }
 857 
 858 static jclass Unsafe_DefineClass_impl(JNIEnv *env, jstring name, jbyteArray data, int offset, int length, jobject loader, jobject pd) {
 859   {
 860     // Code lifted from JDK 1.3 ClassLoader.c
 861 
 862     jbyte *body;
 863     char *utfName;
 864     jclass result = 0;
 865     char buf[128];
 866 
 867     if (UsePerfData) {
 868       ClassLoader::unsafe_defineClassCallCounter()->inc();
 869     }
 870 
 871     if (data == NULL) {
 872         throw_new(env, "NullPointerException");
 873         return 0;
 874     }
 875 
 876     /* Work around 4153825. malloc crashes on Solaris when passed a
 877      * negative size.
 878      */
 879     if (length < 0) {
 880         throw_new(env, "ArrayIndexOutOfBoundsException");
 881         return 0;
 882     }
 883 
 884     body = NEW_C_HEAP_ARRAY(jbyte, length, mtInternal);
 885 
 886     if (body == 0) {
 887         throw_new(env, "OutOfMemoryError");
 888         return 0;
 889     }
 890 
 891     env->GetByteArrayRegion(data, offset, length, body);
 892 
 893     if (env->ExceptionOccurred())
 894         goto free_body;
 895 
 896     if (name != NULL) {
 897         uint len = env->GetStringUTFLength(name);
 898         int unicode_len = env->GetStringLength(name);
 899         if (len >= sizeof(buf)) {
 900             utfName = NEW_C_HEAP_ARRAY(char, len + 1, mtInternal);
 901             if (utfName == NULL) {
 902                 throw_new(env, "OutOfMemoryError");
 903                 goto free_body;
 904             }
 905         } else {
 906             utfName = buf;
 907         }
 908         env->GetStringUTFRegion(name, 0, unicode_len, utfName);
 909         //VerifyFixClassname(utfName);
 910         for (uint i = 0; i < len; i++) {
 911           if (utfName[i] == '.')   utfName[i] = '/';
 912         }
 913     } else {
 914         utfName = NULL;
 915     }
 916 
 917     result = JVM_DefineClass(env, utfName, loader, body, length, pd);
 918 
 919     if (utfName && utfName != buf)
 920         FREE_C_HEAP_ARRAY(char, utfName);
 921 
 922  free_body:
 923     FREE_C_HEAP_ARRAY(jbyte, body);
 924     return result;
 925   }
 926 }
 927 
 928 
 929 UNSAFE_ENTRY(jclass, Unsafe_DefineClass(JNIEnv *env, jobject unsafe, jstring name, jbyteArray data, int offset, int length, jobject loader, jobject pd))
 930   UnsafeWrapper("Unsafe_DefineClass");
 931   {
 932     ThreadToNativeFromVM ttnfv(thread);
 933     return Unsafe_DefineClass_impl(env, name, data, offset, length, loader, pd);
 934   }
 935 UNSAFE_END
 936 
 937 
 938 // define a class but do not make it known to the class loader or system dictionary
 939 // - host_class:  supplies context for linkage, access control, protection domain, and class loader
 940 // - data:  bytes of a class file, a raw memory address (length gives the number of bytes)
 941 // - cp_patches:  where non-null entries exist, they replace corresponding CP entries in data
 942 
 943 // When you load an anonymous class U, it works as if you changed its name just before loading,
 944 // to a name that you will never use again.  Since the name is lost, no other class can directly
 945 // link to any member of U.  Just after U is loaded, the only way to use it is reflectively,
 946 // through java.lang.Class methods like Class.newInstance.
 947 
 948 // Access checks for linkage sites within U continue to follow the same rules as for named classes.
 949 // The package of an anonymous class is given by the package qualifier on the name under which it was loaded.
 950 // An anonymous class also has special privileges to access any member of its host class.
 951 // This is the main reason why this loading operation is unsafe.  The purpose of this is to
 952 // allow language implementations to simulate "open classes"; a host class in effect gets
 953 // new code when an anonymous class is loaded alongside it.  A less convenient but more
 954 // standard way to do this is with reflection, which can also be set to ignore access
 955 // restrictions.
 956 
 957 // Access into an anonymous class is possible only through reflection.  Therefore, there
 958 // are no special access rules for calling into an anonymous class.  The relaxed access
 959 // rule for the host class is applied in the opposite direction:  A host class reflectively
 960 // access one of its anonymous classes.
 961 
 962 // If you load the same bytecodes twice, you get two different classes.  You can reload
 963 // the same bytecodes with or without varying CP patches.
 964 
 965 // By using the CP patching array, you can have a new anonymous class U2 refer to an older one U1.
 966 // The bytecodes for U2 should refer to U1 by a symbolic name (doesn't matter what the name is).
 967 // The CONSTANT_Class entry for that name can be patched to refer directly to U1.
 968 
 969 // This allows, for example, U2 to use U1 as a superclass or super-interface, or as
 970 // an outer class (so that U2 is an anonymous inner class of anonymous U1).
 971 // It is not possible for a named class, or an older anonymous class, to refer by
 972 // name (via its CP) to a newer anonymous class.
 973 
 974 // CP patching may also be used to modify (i.e., hack) the names of methods, classes,
 975 // or type descriptors used in the loaded anonymous class.
 976 
 977 // Finally, CP patching may be used to introduce "live" objects into the constant pool,
 978 // instead of "dead" strings.  A compiled statement like println((Object)"hello") can
 979 // be changed to println(greeting), where greeting is an arbitrary object created before
 980 // the anonymous class is loaded.  This is useful in dynamic languages, in which
 981 // various kinds of metaobjects must be introduced as constants into bytecode.
 982 // Note the cast (Object), which tells the verifier to expect an arbitrary object,
 983 // not just a literal string.  For such ldc instructions, the verifier uses the
 984 // type Object instead of String, if the loaded constant is not in fact a String.
 985 
 986 static instanceKlassHandle
 987 Unsafe_DefineAnonymousClass_impl(JNIEnv *env,
 988                                  jclass host_class, jbyteArray data, jobjectArray cp_patches_jh,
 989                                  HeapWord* *temp_alloc,
 990                                  TRAPS) {
 991 
 992   if (UsePerfData) {
 993     ClassLoader::unsafe_defineClassCallCounter()->inc();
 994   }
 995 
 996   if (data == NULL) {
 997     THROW_0(vmSymbols::java_lang_NullPointerException());
 998   }
 999 
1000   jint length = typeArrayOop(JNIHandles::resolve_non_null(data))->length();
1001   jint word_length = (length + sizeof(HeapWord)-1) / sizeof(HeapWord);
1002   HeapWord* body = NEW_C_HEAP_ARRAY(HeapWord, word_length, mtInternal);
1003   if (body == NULL) {
1004     THROW_0(vmSymbols::java_lang_OutOfMemoryError());
1005   }
1006 
1007   // caller responsible to free it:
1008   (*temp_alloc) = body;
1009 
1010   {
1011     jbyte* array_base = typeArrayOop(JNIHandles::resolve_non_null(data))->byte_at_addr(0);
1012     Copy::conjoint_words((HeapWord*) array_base, body, word_length);
1013   }
1014 
1015   u1* class_bytes = (u1*) body;
1016   int class_bytes_length = (int) length;
1017   if (class_bytes_length < 0)  class_bytes_length = 0;
1018   if (class_bytes == NULL
1019       || host_class == NULL
1020       || length != class_bytes_length)
1021     THROW_0(vmSymbols::java_lang_IllegalArgumentException());
1022 
1023   objArrayHandle cp_patches_h;
1024   if (cp_patches_jh != NULL) {
1025     oop p = JNIHandles::resolve_non_null(cp_patches_jh);
1026     if (!p->is_objArray())
1027       THROW_0(vmSymbols::java_lang_IllegalArgumentException());
1028     cp_patches_h = objArrayHandle(THREAD, (objArrayOop)p);
1029   }
1030 
1031   const Klass* host_klass = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(host_class));
1032   assert(host_klass != NULL, "invariant");
1033 
1034   const char* host_source = host_klass->external_name();
1035   Handle      host_loader(THREAD, host_klass->class_loader());
1036   Handle      host_domain(THREAD, host_klass->protection_domain());
1037 
1038   GrowableArray<Handle>* cp_patches = NULL;
1039   if (cp_patches_h.not_null()) {
1040     int alen = cp_patches_h->length();
1041     for (int i = alen-1; i >= 0; i--) {
1042       oop p = cp_patches_h->obj_at(i);
1043       if (p != NULL) {
1044         Handle patch(THREAD, p);
1045         if (cp_patches == NULL)
1046           cp_patches = new GrowableArray<Handle>(i+1, i+1, Handle());
1047         cp_patches->at_put(i, patch);
1048       }
1049     }
1050   }
1051 
1052   ClassFileStream st(class_bytes,
1053                      class_bytes_length,
1054                      host_source,
1055                      ClassFileStream::verify);
1056 
1057   instanceKlassHandle anon_klass;
1058   {
1059     Symbol* no_class_name = NULL;
1060     Klass* anonk = SystemDictionary::parse_stream(no_class_name,
1061                                                   host_loader,
1062                                                   host_domain,
1063                                                   &st,
1064                                                   host_klass,
1065                                                   cp_patches,
1066                                                   CHECK_NULL);
1067     if (anonk == NULL)  return NULL;
1068     anon_klass = instanceKlassHandle(THREAD, anonk);
1069   }
1070 
1071   return anon_klass;
1072 }
1073 
1074 UNSAFE_ENTRY(jclass, Unsafe_DefineAnonymousClass(JNIEnv *env, jobject unsafe, jclass host_class, jbyteArray data, jobjectArray cp_patches_jh))
1075 {
1076   instanceKlassHandle anon_klass;
1077   jobject res_jh = NULL;
1078 
1079   UnsafeWrapper("Unsafe_DefineAnonymousClass");
1080   ResourceMark rm(THREAD);
1081 
1082   HeapWord* temp_alloc = NULL;
1083 
1084   anon_klass = Unsafe_DefineAnonymousClass_impl(env, host_class, data,
1085                                                 cp_patches_jh,
1086                                                    &temp_alloc, THREAD);
1087   if (anon_klass() != NULL)
1088     res_jh = JNIHandles::make_local(env, anon_klass->java_mirror());
1089 
1090   // try/finally clause:
1091   if (temp_alloc != NULL) {
1092     FREE_C_HEAP_ARRAY(HeapWord, temp_alloc);
1093   }
1094 
1095   // The anonymous class loader data has been artificially been kept alive to
1096   // this point.   The mirror and any instances of this class have to keep
1097   // it alive afterwards.
1098   if (anon_klass() != NULL) {
1099     anon_klass->class_loader_data()->set_keep_alive(false);
1100   }
1101 
1102   // let caller initialize it as needed...
1103 
1104   return (jclass) res_jh;
1105 }
1106 UNSAFE_END
1107 
1108 
1109 
1110 UNSAFE_ENTRY(void, Unsafe_ThrowException(JNIEnv *env, jobject unsafe, jthrowable thr))
1111   UnsafeWrapper("Unsafe_ThrowException");
1112   {
1113     ThreadToNativeFromVM ttnfv(thread);
1114     env->Throw(thr);
1115   }
1116 UNSAFE_END
1117 
1118 // JSR166 ------------------------------------------------------------------
1119 
1120 UNSAFE_ENTRY(jboolean, Unsafe_CompareAndSwapObject(JNIEnv *env, jobject unsafe, jobject obj, jlong offset, jobject e_h, jobject x_h))
1121   UnsafeWrapper("Unsafe_CompareAndSwapObject");
1122   oop x = JNIHandles::resolve(x_h);
1123   oop e = JNIHandles::resolve(e_h);
1124   oop p = JNIHandles::resolve(obj);
1125   HeapWord* addr = (HeapWord *)index_oop_from_field_offset_long(p, offset);
1126   oop res = oopDesc::atomic_compare_exchange_oop(x, addr, e, true);
1127   jboolean success  = (res == e);
1128   if (success)
1129     update_barrier_set((void*)addr, x);
1130   return success;
1131 UNSAFE_END
1132 
1133 UNSAFE_ENTRY(jboolean, Unsafe_CompareAndSwapInt(JNIEnv *env, jobject unsafe, jobject obj, jlong offset, jint e, jint x))
1134   UnsafeWrapper("Unsafe_CompareAndSwapInt");
1135   oop p = JNIHandles::resolve(obj);
1136   jint* addr = (jint *) index_oop_from_field_offset_long(p, offset);
1137   return (jint)(Atomic::cmpxchg(x, addr, e)) == e;
1138 UNSAFE_END
1139 
1140 UNSAFE_ENTRY(jboolean, Unsafe_CompareAndSwapLong(JNIEnv *env, jobject unsafe, jobject obj, jlong offset, jlong e, jlong x))
1141   UnsafeWrapper("Unsafe_CompareAndSwapLong");
1142   Handle p (THREAD, JNIHandles::resolve(obj));
1143   jlong* addr = (jlong*)(index_oop_from_field_offset_long(p(), offset));
1144 #ifdef SUPPORTS_NATIVE_CX8
1145   return (jlong)(Atomic::cmpxchg(x, addr, e)) == e;
1146 #else
1147   if (VM_Version::supports_cx8())
1148     return (jlong)(Atomic::cmpxchg(x, addr, e)) == e;
1149   else {
1150     jboolean success = false;
1151     MutexLockerEx mu(UnsafeJlong_lock, Mutex::_no_safepoint_check_flag);
1152     jlong val = Atomic::load(addr);
1153     if (val == e) { Atomic::store(x, addr); success = true; }
1154     return success;
1155   }
1156 #endif
1157 UNSAFE_END
1158 
1159 UNSAFE_ENTRY(void, Unsafe_Park(JNIEnv *env, jobject unsafe, jboolean isAbsolute, jlong time))
1160   UnsafeWrapper("Unsafe_Park");
1161   EventThreadPark event;
1162   HOTSPOT_THREAD_PARK_BEGIN((uintptr_t) thread->parker(), (int) isAbsolute, time);
1163 
1164   JavaThreadParkedState jtps(thread, time != 0);
1165   thread->parker()->park(isAbsolute != 0, time);
1166 
1167   HOTSPOT_THREAD_PARK_END((uintptr_t) thread->parker());
1168   if (event.should_commit()) {
1169     oop obj = thread->current_park_blocker();
1170     event.set_klass((obj != NULL) ? obj->klass() : NULL);
1171     event.set_timeout(time);
1172     event.set_address((obj != NULL) ? (TYPE_ADDRESS) cast_from_oop<uintptr_t>(obj) : 0);
1173     event.commit();
1174   }
1175 UNSAFE_END
1176 
1177 UNSAFE_ENTRY(void, Unsafe_Unpark(JNIEnv *env, jobject unsafe, jobject jthread))
1178   UnsafeWrapper("Unsafe_Unpark");
1179   Parker* p = NULL;
1180   if (jthread != NULL) {
1181     oop java_thread = JNIHandles::resolve_non_null(jthread);
1182     if (java_thread != NULL) {
1183       jlong lp = java_lang_Thread::park_event(java_thread);
1184       if (lp != 0) {
1185         // This cast is OK even though the jlong might have been read
1186         // non-atomically on 32bit systems, since there, one word will
1187         // always be zero anyway and the value set is always the same
1188         p = (Parker*)addr_from_java(lp);
1189       } else {
1190         // Grab lock if apparently null or using older version of library
1191         MutexLocker mu(Threads_lock);
1192         java_thread = JNIHandles::resolve_non_null(jthread);
1193         if (java_thread != NULL) {
1194           JavaThread* thr = java_lang_Thread::thread(java_thread);
1195           if (thr != NULL) {
1196             p = thr->parker();
1197             if (p != NULL) { // Bind to Java thread for next time.
1198               java_lang_Thread::set_park_event(java_thread, addr_to_java(p));
1199             }
1200           }
1201         }
1202       }
1203     }
1204   }
1205   if (p != NULL) {
1206     HOTSPOT_THREAD_UNPARK((uintptr_t) p);
1207     p->unpark();
1208   }
1209 UNSAFE_END
1210 
1211 UNSAFE_ENTRY(jint, Unsafe_Loadavg(JNIEnv *env, jobject unsafe, jdoubleArray loadavg, jint nelem))
1212   UnsafeWrapper("Unsafe_Loadavg");
1213   const int max_nelem = 3;
1214   double la[max_nelem];
1215   jint ret;
1216 
1217   typeArrayOop a = typeArrayOop(JNIHandles::resolve_non_null(loadavg));
1218   assert(a->is_typeArray(), "must be type array");
1219 
1220   if (nelem < 0 || nelem > max_nelem || a->length() < nelem) {
1221     ThreadToNativeFromVM ttnfv(thread);
1222     throw_new(env, "ArrayIndexOutOfBoundsException");
1223     return -1;
1224   }
1225 
1226   ret = os::loadavg(la, nelem);
1227   if (ret == -1) return -1;
1228 
1229   // if successful, ret is the number of samples actually retrieved.
1230   assert(ret >= 0 && ret <= max_nelem, "Unexpected loadavg return value");
1231   switch(ret) {
1232     case 3: a->double_at_put(2, (jdouble)la[2]); // fall through
1233     case 2: a->double_at_put(1, (jdouble)la[1]); // fall through
1234     case 1: a->double_at_put(0, (jdouble)la[0]); break;
1235   }
1236   return ret;
1237 UNSAFE_END
1238 
1239 
1240 /// JVM_RegisterUnsafeMethods
1241 
1242 #define ADR "J"
1243 
1244 #define LANG "Ljava/lang/"
1245 
1246 #define OBJ LANG "Object;"
1247 #define CLS LANG "Class;"
1248 #define FLD LANG "reflect/Field;"
1249 #define THR LANG "Throwable;"
1250 
1251 #define DC_Args  LANG "String;[BII" LANG "ClassLoader;" "Ljava/security/ProtectionDomain;"
1252 #define DAC_Args CLS "[B[" OBJ
1253 
1254 #define CC (char*)  /*cast a literal from (const char*)*/
1255 #define FN_PTR(f) CAST_FROM_FN_PTR(void*, &f)
1256 
1257 #define DECLARE_GETPUTOOP(Boolean, Z) \
1258     {CC "get" #Boolean,      CC "(" OBJ "J)" #Z,       FN_PTR(Unsafe_Get##Boolean)}, \
1259     {CC "put" #Boolean,      CC "(" OBJ "J" #Z ")V",   FN_PTR(Unsafe_Set##Boolean)}, \
1260     {CC "get" #Boolean "Volatile",      CC "(" OBJ "J)" #Z,       FN_PTR(Unsafe_Get##Boolean##Volatile)}, \
1261     {CC "put" #Boolean "Volatile",      CC "(" OBJ "J" #Z ")V",   FN_PTR(Unsafe_Set##Boolean##Volatile)}
1262 
1263 
1264 #define DECLARE_GETPUTNATIVE(Byte, B) \
1265     {CC "get" #Byte,         CC "(" ADR ")" #B,       FN_PTR(Unsafe_GetNative##Byte)}, \
1266     {CC "put" #Byte,         CC "(" ADR#B ")V",       FN_PTR(Unsafe_SetNative##Byte)}
1267 
1268 
1269 static JNINativeMethod sun_misc_Unsafe_methods[] = {
1270     {CC "getObject",        CC "(" OBJ "J)" OBJ "",   FN_PTR(Unsafe_GetObject)},
1271     {CC "putObject",        CC "(" OBJ "J" OBJ ")V",  FN_PTR(Unsafe_SetObject)},
1272     {CC "getObjectVolatile",CC "(" OBJ "J)" OBJ "",   FN_PTR(Unsafe_GetObjectVolatile)},
1273     {CC "putObjectVolatile",CC "(" OBJ "J" OBJ ")V",  FN_PTR(Unsafe_SetObjectVolatile)},
1274 
1275     {CC "getUncompressedObject", CC "(" ADR ")" OBJ,  FN_PTR(Unsafe_GetUncompressedObject)},
1276     {CC "getJavaMirror",         CC "(" ADR ")" CLS,  FN_PTR(Unsafe_GetJavaMirror)},
1277     {CC "getKlassPointer",       CC "(" OBJ ")" ADR,  FN_PTR(Unsafe_GetKlassPointer)},
1278 
1279     DECLARE_GETPUTOOP(Boolean, Z),
1280     DECLARE_GETPUTOOP(Byte, B),
1281     DECLARE_GETPUTOOP(Short, S),
1282     DECLARE_GETPUTOOP(Char, C),
1283     DECLARE_GETPUTOOP(Int, I),
1284     DECLARE_GETPUTOOP(Long, J),
1285     DECLARE_GETPUTOOP(Float, F),
1286     DECLARE_GETPUTOOP(Double, D),
1287 
1288     DECLARE_GETPUTNATIVE(Byte, B),
1289     DECLARE_GETPUTNATIVE(Short, S),
1290     DECLARE_GETPUTNATIVE(Char, C),
1291     DECLARE_GETPUTNATIVE(Int, I),
1292     DECLARE_GETPUTNATIVE(Long, J),
1293     DECLARE_GETPUTNATIVE(Float, F),
1294     DECLARE_GETPUTNATIVE(Double, D),
1295 
1296     {CC "getAddress",         CC "(" ADR ")" ADR,        FN_PTR(Unsafe_GetNativeAddress)},
1297     {CC "putAddress",         CC "(" ADR "" ADR ")V",    FN_PTR(Unsafe_SetNativeAddress)},
1298 
1299     {CC "allocateMemory",     CC "(J)" ADR,              FN_PTR(Unsafe_AllocateMemory)},
1300     {CC "reallocateMemory",   CC "(" ADR "J)" ADR,       FN_PTR(Unsafe_ReallocateMemory)},
1301     {CC "freeMemory",         CC "(" ADR ")V",           FN_PTR(Unsafe_FreeMemory)},
1302 
1303     {CC "objectFieldOffset",  CC "(" FLD ")J",           FN_PTR(Unsafe_ObjectFieldOffset)},
1304     {CC "staticFieldOffset",  CC "(" FLD ")J",           FN_PTR(Unsafe_StaticFieldOffset)},
1305     {CC "staticFieldBase",    CC "(" FLD ")" OBJ,        FN_PTR(Unsafe_StaticFieldBaseFromField)},
1306     {CC "ensureClassInitialized",CC "(" CLS ")V",        FN_PTR(Unsafe_EnsureClassInitialized)},
1307     {CC "arrayBaseOffset",    CC "(" CLS ")I",           FN_PTR(Unsafe_ArrayBaseOffset)},
1308     {CC "arrayIndexScale",    CC "(" CLS ")I",           FN_PTR(Unsafe_ArrayIndexScale)},
1309     {CC "addressSize",        CC "()I",                  FN_PTR(Unsafe_AddressSize)},
1310     {CC "pageSize",           CC "()I",                  FN_PTR(Unsafe_PageSize)},
1311 
1312     {CC "defineClass",        CC "(" DC_Args ")" CLS,    FN_PTR(Unsafe_DefineClass)},
1313     {CC "allocateInstance",   CC "(" CLS ")" OBJ,        FN_PTR(Unsafe_AllocateInstance)},
1314     {CC "throwException",     CC "(" THR ")V",           FN_PTR(Unsafe_ThrowException)},
1315     {CC "compareAndSwapObject", CC "(" OBJ "J" OBJ "" OBJ ")Z", FN_PTR(Unsafe_CompareAndSwapObject)},
1316     {CC "compareAndSwapInt",  CC "(" OBJ "J""I""I"")Z",  FN_PTR(Unsafe_CompareAndSwapInt)},
1317     {CC "compareAndSwapLong", CC "(" OBJ "J""J""J"")Z",  FN_PTR(Unsafe_CompareAndSwapLong)},
1318     {CC "putOrderedObject",   CC "(" OBJ "J" OBJ ")V",   FN_PTR(Unsafe_SetOrderedObject)},
1319     {CC "putOrderedInt",      CC "(" OBJ "JI)V",         FN_PTR(Unsafe_SetOrderedInt)},
1320     {CC "putOrderedLong",     CC "(" OBJ "JJ)V",         FN_PTR(Unsafe_SetOrderedLong)},
1321     {CC "park",               CC "(ZJ)V",                FN_PTR(Unsafe_Park)},
1322     {CC "unpark",             CC "(" OBJ ")V",           FN_PTR(Unsafe_Unpark)},
1323 
1324     {CC "getLoadAverage",     CC "([DI)I",               FN_PTR(Unsafe_Loadavg)},
1325 
1326     {CC "copyMemory",         CC "(" OBJ "J" OBJ "JJ)V", FN_PTR(Unsafe_CopyMemory)},
1327     {CC "setMemory",          CC "(" OBJ "JJB)V",        FN_PTR(Unsafe_SetMemory)},
1328 
1329     {CC "defineAnonymousClass", CC "(" DAC_Args ")" CLS, FN_PTR(Unsafe_DefineAnonymousClass)},
1330 
1331     {CC "shouldBeInitialized",CC "(" CLS ")Z",           FN_PTR(Unsafe_ShouldBeInitialized)},
1332 
1333     {CC "loadFence",          CC "()V",                  FN_PTR(Unsafe_LoadFence)},
1334     {CC "storeFence",         CC "()V",                  FN_PTR(Unsafe_StoreFence)},
1335     {CC "fullFence",          CC "()V",                  FN_PTR(Unsafe_FullFence)},
1336 };
1337 
1338 static JNINativeMethod jdk_internal_misc_Unsafe_methods[] = {
1339     {CC "getObject",        CC "(" OBJ "J)" OBJ "",   FN_PTR(Unsafe_GetObject)},
1340     {CC "putObject",        CC "(" OBJ "J" OBJ ")V",  FN_PTR(Unsafe_SetObject)},
1341     {CC "getObjectVolatile",CC "(" OBJ "J)" OBJ "",   FN_PTR(Unsafe_GetObjectVolatile)},
1342     {CC "putObjectVolatile",CC "(" OBJ "J" OBJ ")V",  FN_PTR(Unsafe_SetObjectVolatile)},
1343 
1344     {CC "getUncompressedObject", CC "(" ADR ")" OBJ,  FN_PTR(Unsafe_GetUncompressedObject)},
1345     {CC "getJavaMirror",         CC "(" ADR ")" CLS,  FN_PTR(Unsafe_GetJavaMirror)},
1346     {CC "getKlassPointer",       CC "(" OBJ ")" ADR,  FN_PTR(Unsafe_GetKlassPointer)},
1347 
1348     DECLARE_GETPUTOOP(Boolean, Z),
1349     DECLARE_GETPUTOOP(Byte, B),
1350     DECLARE_GETPUTOOP(Short, S),
1351     DECLARE_GETPUTOOP(Char, C),
1352     DECLARE_GETPUTOOP(Int, I),
1353     DECLARE_GETPUTOOP(Long, J),
1354     DECLARE_GETPUTOOP(Float, F),
1355     DECLARE_GETPUTOOP(Double, D),
1356 
1357     DECLARE_GETPUTNATIVE(Byte, B),
1358     DECLARE_GETPUTNATIVE(Short, S),
1359     DECLARE_GETPUTNATIVE(Char, C),
1360     DECLARE_GETPUTNATIVE(Int, I),
1361     DECLARE_GETPUTNATIVE(Long, J),
1362     DECLARE_GETPUTNATIVE(Float, F),
1363     DECLARE_GETPUTNATIVE(Double, D),
1364 
1365     {CC "getAddress",         CC "(" ADR ")" ADR,        FN_PTR(Unsafe_GetNativeAddress)},
1366     {CC "putAddress",         CC "(" ADR "" ADR ")V",    FN_PTR(Unsafe_SetNativeAddress)},
1367 
1368     {CC "allocateMemory",     CC "(J)" ADR,              FN_PTR(Unsafe_AllocateMemory)},
1369     {CC "reallocateMemory",   CC "(" ADR "J)" ADR,       FN_PTR(Unsafe_ReallocateMemory)},
1370     {CC "freeMemory",         CC "(" ADR ")V",           FN_PTR(Unsafe_FreeMemory)},
1371 
1372     {CC "objectFieldOffset",  CC "(" FLD ")J",           FN_PTR(Unsafe_ObjectFieldOffset)},
1373     {CC "staticFieldOffset",  CC "(" FLD ")J",           FN_PTR(Unsafe_StaticFieldOffset)},
1374     {CC "staticFieldBase",    CC "(" FLD ")" OBJ,        FN_PTR(Unsafe_StaticFieldBaseFromField)},
1375     {CC "ensureClassInitialized",CC "(" CLS ")V",        FN_PTR(Unsafe_EnsureClassInitialized)},
1376     {CC "arrayBaseOffset",    CC "(" CLS ")I",           FN_PTR(Unsafe_ArrayBaseOffset)},
1377     {CC "arrayIndexScale",    CC "(" CLS ")I",           FN_PTR(Unsafe_ArrayIndexScale)},
1378     {CC "addressSize",        CC "()I",                  FN_PTR(Unsafe_AddressSize)},
1379     {CC "pageSize",           CC "()I",                  FN_PTR(Unsafe_PageSize)},
1380 
1381     {CC "defineClass",        CC "(" DC_Args ")" CLS,    FN_PTR(Unsafe_DefineClass)},
1382     {CC "allocateInstance",   CC "(" CLS ")" OBJ,        FN_PTR(Unsafe_AllocateInstance)},
1383     {CC "throwException",     CC "(" THR ")V",           FN_PTR(Unsafe_ThrowException)},
1384     {CC "compareAndSwapObject", CC "(" OBJ "J" OBJ "" OBJ ")Z", FN_PTR(Unsafe_CompareAndSwapObject)},
1385     {CC "compareAndSwapInt",  CC "(" OBJ "J""I""I"")Z",  FN_PTR(Unsafe_CompareAndSwapInt)},
1386     {CC "compareAndSwapLong", CC "(" OBJ "J""J""J"")Z",  FN_PTR(Unsafe_CompareAndSwapLong)},
1387     {CC "putOrderedObject",   CC "(" OBJ "J" OBJ ")V",   FN_PTR(Unsafe_SetOrderedObject)},
1388     {CC "putOrderedInt",      CC "(" OBJ "JI)V",         FN_PTR(Unsafe_SetOrderedInt)},
1389     {CC "putOrderedLong",     CC "(" OBJ "JJ)V",         FN_PTR(Unsafe_SetOrderedLong)},
1390     {CC "park",               CC "(ZJ)V",                FN_PTR(Unsafe_Park)},
1391     {CC "unpark",             CC "(" OBJ ")V",           FN_PTR(Unsafe_Unpark)},
1392 
1393     {CC "getLoadAverage",     CC "([DI)I",               FN_PTR(Unsafe_Loadavg)},
1394 
1395     {CC "copyMemory",         CC "(" OBJ "J" OBJ "JJ)V", FN_PTR(Unsafe_CopyMemory)},
1396     {CC "copySwapMemory0",    CC "(" OBJ "J" OBJ "JJJ)V", FN_PTR(Unsafe_CopySwapMemory0)},
1397     {CC "setMemory",          CC "(" OBJ "JJB)V",        FN_PTR(Unsafe_SetMemory)},
1398 
1399     {CC "defineAnonymousClass", CC "(" DAC_Args ")" CLS, FN_PTR(Unsafe_DefineAnonymousClass)},
1400 
1401     {CC "shouldBeInitialized",CC "(" CLS ")Z",           FN_PTR(Unsafe_ShouldBeInitialized)},
1402 
1403     {CC "loadFence",          CC "()V",                  FN_PTR(Unsafe_LoadFence)},
1404     {CC "storeFence",         CC "()V",                  FN_PTR(Unsafe_StoreFence)},
1405     {CC "fullFence",          CC "()V",                  FN_PTR(Unsafe_FullFence)},
1406 
1407     {CC "isBigEndian0",       CC "()Z",                  FN_PTR(Unsafe_isBigEndian0)},
1408     {CC "unalignedAccess0",   CC "()Z",                  FN_PTR(Unsafe_unalignedAccess0)}
1409 };
1410 
1411 #undef CC
1412 #undef FN_PTR
1413 
1414 #undef ADR
1415 #undef LANG
1416 #undef OBJ
1417 #undef CLS
1418 #undef FLD
1419 #undef THR
1420 #undef DC_Args
1421 #undef DAC_Args
1422 
1423 #undef DECLARE_GETPUTOOP
1424 #undef DECLARE_GETPUTNATIVE
1425 
1426 
1427 // These two functions are exported, used by NativeLookup.
1428 // The Unsafe_xxx functions above are called only from the interpreter.
1429 // The optimizer looks at names and signatures to recognize
1430 // individual functions.
1431 
1432 JVM_ENTRY(void, JVM_RegisterSunMiscUnsafeMethods(JNIEnv *env, jclass unsafeclass))
1433   UnsafeWrapper("JVM_RegisterSunMiscUnsafeMethods");
1434   {
1435     ThreadToNativeFromVM ttnfv(thread);
1436 
1437     int ok = env->RegisterNatives(unsafeclass, sun_misc_Unsafe_methods, sizeof(sun_misc_Unsafe_methods)/sizeof(JNINativeMethod));
1438     guarantee(ok == 0, "register sun.misc.Unsafe natives");
1439   }
1440 JVM_END
1441 
1442 JVM_ENTRY(void, JVM_RegisterJDKInternalMiscUnsafeMethods(JNIEnv *env, jclass unsafeclass))
1443   UnsafeWrapper("JVM_RegisterJDKInternalMiscUnsafeMethods");
1444   {
1445     ThreadToNativeFromVM ttnfv(thread);
1446 
1447     int ok = env->RegisterNatives(unsafeclass, jdk_internal_misc_Unsafe_methods, sizeof(jdk_internal_misc_Unsafe_methods)/sizeof(JNINativeMethod));
1448     guarantee(ok == 0, "register jdk.internal.misc.Unsafe natives");
1449   }
1450 JVM_END