1 /*
   2  * Copyright (c) 2000, 2018, 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 "jni.h"
  27 #include "jvm.h"
  28 #include "classfile/classFileStream.hpp"
  29 #include "classfile/vmSymbols.hpp"
  30 #include "memory/allocation.inline.hpp"
  31 #include "memory/resourceArea.hpp"
  32 #include "oops/access.inline.hpp"
  33 #include "oops/fieldStreams.hpp"
  34 #include "oops/objArrayOop.inline.hpp"
  35 #include "oops/oop.inline.hpp"
  36 #include "oops/typeArrayOop.inline.hpp"
  37 #include "prims/unsafe.hpp"
  38 #include "runtime/atomic.hpp"
  39 #include "runtime/globals.hpp"
  40 #include "runtime/interfaceSupport.hpp"
  41 #include "runtime/jniHandles.inline.hpp"
  42 #include "runtime/orderAccess.inline.hpp"
  43 #include "runtime/reflection.hpp"
  44 #include "runtime/thread.hpp"
  45 #include "runtime/threadSMR.hpp"
  46 #include "runtime/vm_version.hpp"
  47 #include "services/threadService.hpp"
  48 #include "trace/tracing.hpp"
  49 #include "utilities/align.hpp"
  50 #include "utilities/copy.hpp"
  51 #include "utilities/dtrace.hpp"
  52 #include "utilities/macros.hpp"
  53 
  54 /**
  55  * Implementation of the jdk.internal.misc.Unsafe class
  56  */
  57 
  58 
  59 #define MAX_OBJECT_SIZE \
  60   ( arrayOopDesc::header_size(T_DOUBLE) * HeapWordSize \
  61     + ((julong)max_jint * sizeof(double)) )
  62 
  63 
  64 #define UNSAFE_ENTRY(result_type, header) \
  65   JVM_ENTRY(static result_type, header)
  66 
  67 #define UNSAFE_LEAF(result_type, header) \
  68   JVM_LEAF(static result_type, header)
  69 
  70 #define UNSAFE_END JVM_END
  71 
  72 
  73 static inline void* addr_from_java(jlong addr) {
  74   // This assert fails in a variety of ways on 32-bit systems.
  75   // It is impossible to predict whether native code that converts
  76   // pointers to longs will sign-extend or zero-extend the addresses.
  77   //assert(addr == (uintptr_t)addr, "must not be odd high bits");
  78   return (void*)(uintptr_t)addr;
  79 }
  80 
  81 static inline jlong addr_to_java(void* p) {
  82   assert(p == (void*)(uintptr_t)p, "must not be odd high bits");
  83   return (uintptr_t)p;
  84 }
  85 
  86 
  87 // Note: The VM's obj_field and related accessors use byte-scaled
  88 // ("unscaled") offsets, just as the unsafe methods do.
  89 
  90 // However, the method Unsafe.fieldOffset explicitly declines to
  91 // guarantee this.  The field offset values manipulated by the Java user
  92 // through the Unsafe API are opaque cookies that just happen to be byte
  93 // offsets.  We represent this state of affairs by passing the cookies
  94 // through conversion functions when going between the VM and the Unsafe API.
  95 // The conversion functions just happen to be no-ops at present.
  96 
  97 static inline jlong field_offset_to_byte_offset(jlong field_offset) {
  98   return field_offset;
  99 }
 100 
 101 static inline jlong field_offset_from_byte_offset(jlong byte_offset) {
 102   return byte_offset;
 103 }
 104 
 105 static inline void assert_field_offset_sane(oop p, jlong field_offset) {
 106 #ifdef ASSERT
 107   jlong byte_offset = field_offset_to_byte_offset(field_offset);
 108 
 109   if (p != NULL) {
 110     assert(byte_offset >= 0 && byte_offset <= (jlong)MAX_OBJECT_SIZE, "sane offset");
 111     if (byte_offset == (jint)byte_offset) {
 112       void* ptr_plus_disp = (address)p + byte_offset;
 113       assert(p->field_addr_raw((jint)byte_offset) == ptr_plus_disp,
 114              "raw [ptr+disp] must be consistent with oop::field_addr_raw");
 115     }
 116     jlong p_size = HeapWordSize * (jlong)(p->size());
 117     assert(byte_offset < p_size, "Unsafe access: offset " INT64_FORMAT " > object's size " INT64_FORMAT, (int64_t)byte_offset, (int64_t)p_size);
 118   }
 119 #endif
 120 }
 121 
 122 static inline void* index_oop_from_field_offset_long(oop p, jlong field_offset) {
 123   assert_field_offset_sane(p, field_offset);
 124   jlong byte_offset = field_offset_to_byte_offset(field_offset);
 125 
 126   if (sizeof(char*) == sizeof(jint)) {   // (this constant folds!)
 127     return (address)p + (jint) byte_offset;
 128   } else {
 129     return (address)p +        byte_offset;
 130   }
 131 }
 132 
 133 // Externally callable versions:
 134 // (Use these in compiler intrinsics which emulate unsafe primitives.)
 135 jlong Unsafe_field_offset_to_byte_offset(jlong field_offset) {
 136   return field_offset;
 137 }
 138 jlong Unsafe_field_offset_from_byte_offset(jlong byte_offset) {
 139   return byte_offset;
 140 }
 141 
 142 
 143 ///// Data read/writes on the Java heap and in native (off-heap) memory
 144 
 145 /**
 146  * Helper class for accessing memory.
 147  *
 148  * Normalizes values and wraps accesses in
 149  * JavaThread::doing_unsafe_access() if needed.
 150  */
 151 template <typename T>
 152 class MemoryAccess : StackObj {
 153   JavaThread* _thread;
 154   oop _obj;
 155   ptrdiff_t _offset;
 156 
 157   // Resolves and returns the address of the memory access.
 158   // This raw memory access may fault, so we make sure it happens within the
 159   // guarded scope by making the access volatile at least. Since the store
 160   // of Thread::set_doing_unsafe_access() is also volatile, these accesses
 161   // can not be reordered by the compiler. Therefore, if the access triggers
 162   // a fault, we will know that Thread::doing_unsafe_access() returns true.
 163   volatile T* addr() {
 164     void* addr = index_oop_from_field_offset_long(_obj, _offset);
 165     return static_cast<volatile T*>(addr);
 166   }
 167 
 168   template <typename U>
 169   U normalize_for_write(U x) {
 170     return x;
 171   }
 172 
 173   jboolean normalize_for_write(jboolean x) {
 174     return x & 1;
 175   }
 176 
 177   template <typename U>
 178   U normalize_for_read(U x) {
 179     return x;
 180   }
 181 
 182   jboolean normalize_for_read(jboolean x) {
 183     return x != 0;
 184   }
 185 
 186   /**
 187    * Helper class to wrap memory accesses in JavaThread::doing_unsafe_access()
 188    */
 189   class GuardUnsafeAccess {
 190     JavaThread* _thread;
 191 
 192   public:
 193     GuardUnsafeAccess(JavaThread* thread) : _thread(thread) {
 194       // native/off-heap access which may raise SIGBUS if accessing
 195       // memory mapped file data in a region of the file which has
 196       // been truncated and is now invalid
 197       _thread->set_doing_unsafe_access(true);
 198     }
 199 
 200     ~GuardUnsafeAccess() {
 201       _thread->set_doing_unsafe_access(false);
 202     }
 203   };
 204 
 205 public:
 206   MemoryAccess(JavaThread* thread, jobject obj, jlong offset)
 207     : _thread(thread), _obj(JNIHandles::resolve(obj)), _offset((ptrdiff_t)offset) {
 208     assert_field_offset_sane(_obj, offset);
 209   }
 210 
 211   T get() {
 212     if (oopDesc::is_null(_obj)) {
 213       GuardUnsafeAccess guard(_thread);
 214       T ret = RawAccess<>::load(addr());
 215       return normalize_for_read(ret);
 216     } else {
 217       T ret = HeapAccess<>::load_at(_obj, _offset);
 218       return normalize_for_read(ret);
 219     }
 220   }
 221 
 222   void put(T x) {
 223     if (oopDesc::is_null(_obj)) {
 224       GuardUnsafeAccess guard(_thread);
 225       RawAccess<>::store(addr(), normalize_for_write(x));
 226     } else {
 227       HeapAccess<>::store_at(_obj, _offset, normalize_for_write(x));
 228     }
 229   }
 230 
 231 
 232   T get_volatile() {
 233     if (oopDesc::is_null(_obj)) {
 234       GuardUnsafeAccess guard(_thread);
 235       volatile T ret = RawAccess<MO_SEQ_CST>::load(addr());
 236       return normalize_for_read(ret);
 237     } else {
 238       T ret = HeapAccess<MO_SEQ_CST>::load_at(_obj, _offset);
 239       return normalize_for_read(ret);
 240     }
 241   }
 242 
 243   void put_volatile(T x) {
 244     if (oopDesc::is_null(_obj)) {
 245       GuardUnsafeAccess guard(_thread);
 246       RawAccess<MO_SEQ_CST>::store(addr(), normalize_for_write(x));
 247     } else {
 248       HeapAccess<MO_SEQ_CST>::store_at(_obj, _offset, normalize_for_write(x));
 249     }
 250   }
 251 };
 252 
 253 // These functions allow a null base pointer with an arbitrary address.
 254 // But if the base pointer is non-null, the offset should make some sense.
 255 // That is, it should be in the range [0, MAX_OBJECT_SIZE].
 256 UNSAFE_ENTRY(jobject, Unsafe_GetObject(JNIEnv *env, jobject unsafe, jobject obj, jlong offset)) {
 257   oop p = JNIHandles::resolve(obj);
 258   assert_field_offset_sane(p, offset);
 259   oop v = HeapAccess<ON_UNKNOWN_OOP_REF>::oop_load_at(p, offset);
 260   return JNIHandles::make_local(env, v);
 261 } UNSAFE_END
 262 
 263 UNSAFE_ENTRY(void, Unsafe_PutObject(JNIEnv *env, jobject unsafe, jobject obj, jlong offset, jobject x_h)) {
 264   oop x = JNIHandles::resolve(x_h);
 265   oop p = JNIHandles::resolve(obj);
 266   assert_field_offset_sane(p, offset);
 267   HeapAccess<ON_UNKNOWN_OOP_REF>::oop_store_at(p, offset, x);
 268 } UNSAFE_END
 269 
 270 UNSAFE_ENTRY(jobject, Unsafe_GetObjectVolatile(JNIEnv *env, jobject unsafe, jobject obj, jlong offset)) {
 271   oop p = JNIHandles::resolve(obj);
 272   assert_field_offset_sane(p, offset);
 273   oop v = HeapAccess<MO_SEQ_CST | ON_UNKNOWN_OOP_REF>::oop_load_at(p, offset);
 274   return JNIHandles::make_local(env, v);
 275 } UNSAFE_END
 276 
 277 UNSAFE_ENTRY(void, Unsafe_PutObjectVolatile(JNIEnv *env, jobject unsafe, jobject obj, jlong offset, jobject x_h)) {
 278   oop x = JNIHandles::resolve(x_h);
 279   oop p = JNIHandles::resolve(obj);
 280   assert_field_offset_sane(p, offset);
 281   HeapAccess<MO_SEQ_CST | ON_UNKNOWN_OOP_REF>::oop_store_at(p, offset, x);
 282 } UNSAFE_END
 283 
 284 UNSAFE_ENTRY(jobject, Unsafe_GetUncompressedObject(JNIEnv *env, jobject unsafe, jlong addr)) {
 285   oop v = *(oop*) (address) addr;
 286   return JNIHandles::make_local(env, v);
 287 } UNSAFE_END
 288 
 289 UNSAFE_LEAF(jboolean, Unsafe_isBigEndian0(JNIEnv *env, jobject unsafe)) {
 290 #ifdef VM_LITTLE_ENDIAN
 291   return false;
 292 #else
 293   return true;
 294 #endif
 295 } UNSAFE_END
 296 
 297 UNSAFE_LEAF(jint, Unsafe_unalignedAccess0(JNIEnv *env, jobject unsafe)) {
 298   return UseUnalignedAccesses;
 299 } UNSAFE_END
 300 
 301 #define DEFINE_GETSETOOP(java_type, Type) \
 302  \
 303 UNSAFE_ENTRY(java_type, Unsafe_Get##Type(JNIEnv *env, jobject unsafe, jobject obj, jlong offset)) { \
 304   return MemoryAccess<java_type>(thread, obj, offset).get(); \
 305 } UNSAFE_END \
 306  \
 307 UNSAFE_ENTRY(void, Unsafe_Put##Type(JNIEnv *env, jobject unsafe, jobject obj, jlong offset, java_type x)) { \
 308   MemoryAccess<java_type>(thread, obj, offset).put(x); \
 309 } UNSAFE_END \
 310  \
 311 // END DEFINE_GETSETOOP.
 312 
 313 DEFINE_GETSETOOP(jboolean, Boolean)
 314 DEFINE_GETSETOOP(jbyte, Byte)
 315 DEFINE_GETSETOOP(jshort, Short);
 316 DEFINE_GETSETOOP(jchar, Char);
 317 DEFINE_GETSETOOP(jint, Int);
 318 DEFINE_GETSETOOP(jlong, Long);
 319 DEFINE_GETSETOOP(jfloat, Float);
 320 DEFINE_GETSETOOP(jdouble, Double);
 321 
 322 #undef DEFINE_GETSETOOP
 323 
 324 #define DEFINE_GETSETOOP_VOLATILE(java_type, Type) \
 325  \
 326 UNSAFE_ENTRY(java_type, Unsafe_Get##Type##Volatile(JNIEnv *env, jobject unsafe, jobject obj, jlong offset)) { \
 327   return MemoryAccess<java_type>(thread, obj, offset).get_volatile(); \
 328 } UNSAFE_END \
 329  \
 330 UNSAFE_ENTRY(void, Unsafe_Put##Type##Volatile(JNIEnv *env, jobject unsafe, jobject obj, jlong offset, java_type x)) { \
 331   MemoryAccess<java_type>(thread, obj, offset).put_volatile(x); \
 332 } UNSAFE_END \
 333  \
 334 // END DEFINE_GETSETOOP_VOLATILE.
 335 
 336 DEFINE_GETSETOOP_VOLATILE(jboolean, Boolean)
 337 DEFINE_GETSETOOP_VOLATILE(jbyte, Byte)
 338 DEFINE_GETSETOOP_VOLATILE(jshort, Short);
 339 DEFINE_GETSETOOP_VOLATILE(jchar, Char);
 340 DEFINE_GETSETOOP_VOLATILE(jint, Int);
 341 DEFINE_GETSETOOP_VOLATILE(jlong, Long);
 342 DEFINE_GETSETOOP_VOLATILE(jfloat, Float);
 343 DEFINE_GETSETOOP_VOLATILE(jdouble, Double);
 344 
 345 #undef DEFINE_GETSETOOP_VOLATILE
 346 
 347 UNSAFE_LEAF(void, Unsafe_LoadFence(JNIEnv *env, jobject unsafe)) {
 348   OrderAccess::acquire();
 349 } UNSAFE_END
 350 
 351 UNSAFE_LEAF(void, Unsafe_StoreFence(JNIEnv *env, jobject unsafe)) {
 352   OrderAccess::release();
 353 } UNSAFE_END
 354 
 355 UNSAFE_LEAF(void, Unsafe_FullFence(JNIEnv *env, jobject unsafe)) {
 356   OrderAccess::fence();
 357 } UNSAFE_END
 358 
 359 ////// Allocation requests
 360 
 361 UNSAFE_ENTRY(jobject, Unsafe_AllocateInstance(JNIEnv *env, jobject unsafe, jclass cls)) {
 362   ThreadToNativeFromVM ttnfv(thread);
 363   return env->AllocObject(cls);
 364 } UNSAFE_END
 365 
 366 UNSAFE_ENTRY(jlong, Unsafe_AllocateMemory0(JNIEnv *env, jobject unsafe, jlong size)) {
 367   size_t sz = (size_t)size;
 368 
 369   sz = align_up(sz, HeapWordSize);
 370   void* x = os::malloc(sz, mtInternal);
 371 
 372   return addr_to_java(x);
 373 } UNSAFE_END
 374 
 375 UNSAFE_ENTRY(jlong, Unsafe_ReallocateMemory0(JNIEnv *env, jobject unsafe, jlong addr, jlong size)) {
 376   void* p = addr_from_java(addr);
 377   size_t sz = (size_t)size;
 378   sz = align_up(sz, HeapWordSize);
 379 
 380   void* x = os::realloc(p, sz, mtInternal);
 381 
 382   return addr_to_java(x);
 383 } UNSAFE_END
 384 
 385 UNSAFE_ENTRY(void, Unsafe_FreeMemory0(JNIEnv *env, jobject unsafe, jlong addr)) {
 386   void* p = addr_from_java(addr);
 387 
 388   os::free(p);
 389 } UNSAFE_END
 390 
 391 UNSAFE_ENTRY(void, Unsafe_SetMemory0(JNIEnv *env, jobject unsafe, jobject obj, jlong offset, jlong size, jbyte value)) {
 392   size_t sz = (size_t)size;
 393 
 394   oop base = JNIHandles::resolve(obj);
 395   void* p = index_oop_from_field_offset_long(base, offset);
 396 
 397   Copy::fill_to_memory_atomic(p, sz, value);
 398 } UNSAFE_END
 399 
 400 UNSAFE_ENTRY(void, Unsafe_CopyMemory0(JNIEnv *env, jobject unsafe, jobject srcObj, jlong srcOffset, jobject dstObj, jlong dstOffset, jlong size)) {
 401   size_t sz = (size_t)size;
 402 
 403   oop srcp = JNIHandles::resolve(srcObj);
 404   oop dstp = JNIHandles::resolve(dstObj);
 405 
 406   void* src = index_oop_from_field_offset_long(srcp, srcOffset);
 407   void* dst = index_oop_from_field_offset_long(dstp, dstOffset);
 408 
 409   Copy::conjoint_memory_atomic(src, dst, sz);
 410 } UNSAFE_END
 411 
 412 // This function is a leaf since if the source and destination are both in native memory
 413 // the copy may potentially be very large, and we don't want to disable GC if we can avoid it.
 414 // If either source or destination (or both) are on the heap, the function will enter VM using
 415 // JVM_ENTRY_FROM_LEAF
 416 UNSAFE_LEAF(void, Unsafe_CopySwapMemory0(JNIEnv *env, jobject unsafe, jobject srcObj, jlong srcOffset, jobject dstObj, jlong dstOffset, jlong size, jlong elemSize)) {
 417   size_t sz = (size_t)size;
 418   size_t esz = (size_t)elemSize;
 419 
 420   if (srcObj == NULL && dstObj == NULL) {
 421     // Both src & dst are in native memory
 422     address src = (address)srcOffset;
 423     address dst = (address)dstOffset;
 424 
 425     Copy::conjoint_swap(src, dst, sz, esz);
 426   } else {
 427     // At least one of src/dst are on heap, transition to VM to access raw pointers
 428 
 429     JVM_ENTRY_FROM_LEAF(env, void, Unsafe_CopySwapMemory0) {
 430       oop srcp = JNIHandles::resolve(srcObj);
 431       oop dstp = JNIHandles::resolve(dstObj);
 432 
 433       address src = (address)index_oop_from_field_offset_long(srcp, srcOffset);
 434       address dst = (address)index_oop_from_field_offset_long(dstp, dstOffset);
 435 
 436       Copy::conjoint_swap(src, dst, sz, esz);
 437     } JVM_END
 438   }
 439 } UNSAFE_END
 440 
 441 ////// Random queries
 442 
 443 UNSAFE_LEAF(jint, Unsafe_AddressSize0(JNIEnv *env, jobject unsafe)) {
 444   return sizeof(void*);
 445 } UNSAFE_END
 446 
 447 UNSAFE_LEAF(jint, Unsafe_PageSize()) {
 448   return os::vm_page_size();
 449 } UNSAFE_END
 450 
 451 static jlong find_field_offset(jclass clazz, jstring name, TRAPS) {
 452   assert(clazz != NULL, "clazz must not be NULL");
 453   assert(name != NULL, "name must not be NULL");
 454 
 455   ResourceMark rm(THREAD);
 456   char *utf_name = java_lang_String::as_utf8_string(JNIHandles::resolve_non_null(name));
 457 
 458   InstanceKlass* k = InstanceKlass::cast(java_lang_Class::as_Klass(JNIHandles::resolve_non_null(clazz)));
 459 
 460   jint offset = -1;
 461   for (JavaFieldStream fs(k); !fs.done(); fs.next()) {
 462     Symbol *name = fs.name();
 463     if (name->equals(utf_name)) {
 464       offset = fs.offset();
 465       break;
 466     }
 467   }
 468   if (offset < 0) {
 469     THROW_0(vmSymbols::java_lang_InternalError());
 470   }
 471   return field_offset_from_byte_offset(offset);
 472 }
 473 
 474 static jlong find_field_offset(jobject field, int must_be_static, TRAPS) {
 475   assert(field != NULL, "field must not be NULL");
 476 
 477   oop reflected   = JNIHandles::resolve_non_null(field);
 478   oop mirror      = java_lang_reflect_Field::clazz(reflected);
 479   Klass* k        = java_lang_Class::as_Klass(mirror);
 480   int slot        = java_lang_reflect_Field::slot(reflected);
 481   int modifiers   = java_lang_reflect_Field::modifiers(reflected);
 482 
 483   if (must_be_static >= 0) {
 484     int really_is_static = ((modifiers & JVM_ACC_STATIC) != 0);
 485     if (must_be_static != really_is_static) {
 486       THROW_0(vmSymbols::java_lang_IllegalArgumentException());
 487     }
 488   }
 489 
 490   int offset = InstanceKlass::cast(k)->field_offset(slot);
 491   return field_offset_from_byte_offset(offset);
 492 }
 493 
 494 UNSAFE_ENTRY(jlong, Unsafe_ObjectFieldOffset0(JNIEnv *env, jobject unsafe, jobject field)) {
 495   return find_field_offset(field, 0, THREAD);
 496 } UNSAFE_END
 497 
 498 UNSAFE_ENTRY(jlong, Unsafe_ObjectFieldOffset1(JNIEnv *env, jobject unsafe, jclass c, jstring name)) {
 499   return find_field_offset(c, name, THREAD);
 500 } UNSAFE_END
 501 
 502 UNSAFE_ENTRY(jlong, Unsafe_StaticFieldOffset0(JNIEnv *env, jobject unsafe, jobject field)) {
 503   return find_field_offset(field, 1, THREAD);
 504 } UNSAFE_END
 505 
 506 UNSAFE_ENTRY(jobject, Unsafe_StaticFieldBase0(JNIEnv *env, jobject unsafe, jobject field)) {
 507   assert(field != NULL, "field must not be NULL");
 508 
 509   // Note:  In this VM implementation, a field address is always a short
 510   // offset from the base of a a klass metaobject.  Thus, the full dynamic
 511   // range of the return type is never used.  However, some implementations
 512   // might put the static field inside an array shared by many classes,
 513   // or even at a fixed address, in which case the address could be quite
 514   // large.  In that last case, this function would return NULL, since
 515   // the address would operate alone, without any base pointer.
 516 
 517   oop reflected   = JNIHandles::resolve_non_null(field);
 518   oop mirror      = java_lang_reflect_Field::clazz(reflected);
 519   int modifiers   = java_lang_reflect_Field::modifiers(reflected);
 520 
 521   if ((modifiers & JVM_ACC_STATIC) == 0) {
 522     THROW_0(vmSymbols::java_lang_IllegalArgumentException());
 523   }
 524 
 525   return JNIHandles::make_local(env, mirror);
 526 } UNSAFE_END
 527 
 528 UNSAFE_ENTRY(void, Unsafe_EnsureClassInitialized0(JNIEnv *env, jobject unsafe, jobject clazz)) {
 529   assert(clazz != NULL, "clazz must not be NULL");
 530 
 531   oop mirror = JNIHandles::resolve_non_null(clazz);
 532 
 533   Klass* klass = java_lang_Class::as_Klass(mirror);
 534   if (klass != NULL && klass->should_be_initialized()) {
 535     InstanceKlass* k = InstanceKlass::cast(klass);
 536     k->initialize(CHECK);
 537   }
 538 }
 539 UNSAFE_END
 540 
 541 UNSAFE_ENTRY(jboolean, Unsafe_ShouldBeInitialized0(JNIEnv *env, jobject unsafe, jobject clazz)) {
 542   assert(clazz != NULL, "clazz must not be NULL");
 543 
 544   oop mirror = JNIHandles::resolve_non_null(clazz);
 545   Klass* klass = java_lang_Class::as_Klass(mirror);
 546 
 547   if (klass != NULL && klass->should_be_initialized()) {
 548     return true;
 549   }
 550 
 551   return false;
 552 }
 553 UNSAFE_END
 554 
 555 static void getBaseAndScale(int& base, int& scale, jclass clazz, TRAPS) {
 556   assert(clazz != NULL, "clazz must not be NULL");
 557 
 558   oop mirror = JNIHandles::resolve_non_null(clazz);
 559   Klass* k = java_lang_Class::as_Klass(mirror);
 560 
 561   if (k == NULL || !k->is_array_klass()) {
 562     THROW(vmSymbols::java_lang_InvalidClassException());
 563   } else if (k->is_objArray_klass()) {
 564     base  = arrayOopDesc::base_offset_in_bytes(T_OBJECT);
 565     scale = heapOopSize;
 566   } else if (k->is_typeArray_klass()) {
 567     TypeArrayKlass* tak = TypeArrayKlass::cast(k);
 568     base  = tak->array_header_in_bytes();
 569     assert(base == arrayOopDesc::base_offset_in_bytes(tak->element_type()), "array_header_size semantics ok");
 570     scale = (1 << tak->log2_element_size());
 571   } else {
 572     ShouldNotReachHere();
 573   }
 574 }
 575 
 576 UNSAFE_ENTRY(jint, Unsafe_ArrayBaseOffset0(JNIEnv *env, jobject unsafe, jclass clazz)) {
 577   int base = 0, scale = 0;
 578   getBaseAndScale(base, scale, clazz, CHECK_0);
 579 
 580   return field_offset_from_byte_offset(base);
 581 } UNSAFE_END
 582 
 583 
 584 UNSAFE_ENTRY(jint, Unsafe_ArrayIndexScale0(JNIEnv *env, jobject unsafe, jclass clazz)) {
 585   int base = 0, scale = 0;
 586   getBaseAndScale(base, scale, clazz, CHECK_0);
 587 
 588   // This VM packs both fields and array elements down to the byte.
 589   // But watch out:  If this changes, so that array references for
 590   // a given primitive type (say, T_BOOLEAN) use different memory units
 591   // than fields, this method MUST return zero for such arrays.
 592   // For example, the VM used to store sub-word sized fields in full
 593   // words in the object layout, so that accessors like getByte(Object,int)
 594   // did not really do what one might expect for arrays.  Therefore,
 595   // this function used to report a zero scale factor, so that the user
 596   // would know not to attempt to access sub-word array elements.
 597   // // Code for unpacked fields:
 598   // if (scale < wordSize)  return 0;
 599 
 600   // The following allows for a pretty general fieldOffset cookie scheme,
 601   // but requires it to be linear in byte offset.
 602   return field_offset_from_byte_offset(scale) - field_offset_from_byte_offset(0);
 603 } UNSAFE_END
 604 
 605 
 606 static inline void throw_new(JNIEnv *env, const char *ename) {
 607   jclass cls = env->FindClass(ename);
 608   if (env->ExceptionCheck()) {
 609     env->ExceptionClear();
 610     tty->print_cr("Unsafe: cannot throw %s because FindClass has failed", ename);
 611     return;
 612   }
 613 
 614   env->ThrowNew(cls, NULL);
 615 }
 616 
 617 static jclass Unsafe_DefineClass_impl(JNIEnv *env, jstring name, jbyteArray data, int offset, int length, jobject loader, jobject pd) {
 618   // Code lifted from JDK 1.3 ClassLoader.c
 619 
 620   jbyte *body;
 621   char *utfName = NULL;
 622   jclass result = 0;
 623   char buf[128];
 624 
 625   assert(data != NULL, "Class bytes must not be NULL");
 626   assert(length >= 0, "length must not be negative: %d", length);
 627 
 628   if (UsePerfData) {
 629     ClassLoader::unsafe_defineClassCallCounter()->inc();
 630   }
 631 
 632   body = NEW_C_HEAP_ARRAY(jbyte, length, mtInternal);
 633   if (body == NULL) {
 634     throw_new(env, "java/lang/OutOfMemoryError");
 635     return 0;
 636   }
 637 
 638   env->GetByteArrayRegion(data, offset, length, body);
 639   if (env->ExceptionOccurred()) {
 640     goto free_body;
 641   }
 642 
 643   if (name != NULL) {
 644     uint len = env->GetStringUTFLength(name);
 645     int unicode_len = env->GetStringLength(name);
 646 
 647     if (len >= sizeof(buf)) {
 648       utfName = NEW_C_HEAP_ARRAY(char, len + 1, mtInternal);
 649       if (utfName == NULL) {
 650         throw_new(env, "java/lang/OutOfMemoryError");
 651         goto free_body;
 652       }
 653     } else {
 654       utfName = buf;
 655     }
 656 
 657     env->GetStringUTFRegion(name, 0, unicode_len, utfName);
 658 
 659     for (uint i = 0; i < len; i++) {
 660       if (utfName[i] == '.')   utfName[i] = '/';
 661     }
 662   }
 663 
 664   result = JVM_DefineClass(env, utfName, loader, body, length, pd);
 665 
 666   if (utfName && utfName != buf) {
 667     FREE_C_HEAP_ARRAY(char, utfName);
 668   }
 669 
 670  free_body:
 671   FREE_C_HEAP_ARRAY(jbyte, body);
 672   return result;
 673 }
 674 
 675 
 676 UNSAFE_ENTRY(jclass, Unsafe_DefineClass0(JNIEnv *env, jobject unsafe, jstring name, jbyteArray data, int offset, int length, jobject loader, jobject pd)) {
 677   ThreadToNativeFromVM ttnfv(thread);
 678 
 679   return Unsafe_DefineClass_impl(env, name, data, offset, length, loader, pd);
 680 } UNSAFE_END
 681 
 682 
 683 // define a class but do not make it known to the class loader or system dictionary
 684 // - host_class:  supplies context for linkage, access control, protection domain, and class loader
 685 //                if host_class is itself anonymous then it is replaced with its host class.
 686 // - data:  bytes of a class file, a raw memory address (length gives the number of bytes)
 687 // - cp_patches:  where non-null entries exist, they replace corresponding CP entries in data
 688 
 689 // When you load an anonymous class U, it works as if you changed its name just before loading,
 690 // to a name that you will never use again.  Since the name is lost, no other class can directly
 691 // link to any member of U.  Just after U is loaded, the only way to use it is reflectively,
 692 // through java.lang.Class methods like Class.newInstance.
 693 
 694 // The package of an anonymous class must either match its host's class's package or be in the
 695 // unnamed package.  If it is in the unnamed package then it will be put in its host class's
 696 // package.
 697 //
 698 
 699 // Access checks for linkage sites within U continue to follow the same rules as for named classes.
 700 // An anonymous class also has special privileges to access any member of its host class.
 701 // This is the main reason why this loading operation is unsafe.  The purpose of this is to
 702 // allow language implementations to simulate "open classes"; a host class in effect gets
 703 // new code when an anonymous class is loaded alongside it.  A less convenient but more
 704 // standard way to do this is with reflection, which can also be set to ignore access
 705 // restrictions.
 706 
 707 // Access into an anonymous class is possible only through reflection.  Therefore, there
 708 // are no special access rules for calling into an anonymous class.  The relaxed access
 709 // rule for the host class is applied in the opposite direction:  A host class reflectively
 710 // access one of its anonymous classes.
 711 
 712 // If you load the same bytecodes twice, you get two different classes.  You can reload
 713 // the same bytecodes with or without varying CP patches.
 714 
 715 // By using the CP patching array, you can have a new anonymous class U2 refer to an older one U1.
 716 // The bytecodes for U2 should refer to U1 by a symbolic name (doesn't matter what the name is).
 717 // The CONSTANT_Class entry for that name can be patched to refer directly to U1.
 718 
 719 // This allows, for example, U2 to use U1 as a superclass or super-interface, or as
 720 // an outer class (so that U2 is an anonymous inner class of anonymous U1).
 721 // It is not possible for a named class, or an older anonymous class, to refer by
 722 // name (via its CP) to a newer anonymous class.
 723 
 724 // CP patching may also be used to modify (i.e., hack) the names of methods, classes,
 725 // or type descriptors used in the loaded anonymous class.
 726 
 727 // Finally, CP patching may be used to introduce "live" objects into the constant pool,
 728 // instead of "dead" strings.  A compiled statement like println((Object)"hello") can
 729 // be changed to println(greeting), where greeting is an arbitrary object created before
 730 // the anonymous class is loaded.  This is useful in dynamic languages, in which
 731 // various kinds of metaobjects must be introduced as constants into bytecode.
 732 // Note the cast (Object), which tells the verifier to expect an arbitrary object,
 733 // not just a literal string.  For such ldc instructions, the verifier uses the
 734 // type Object instead of String, if the loaded constant is not in fact a String.
 735 
 736 static InstanceKlass*
 737 Unsafe_DefineAnonymousClass_impl(JNIEnv *env,
 738                                  jclass host_class, jbyteArray data, jobjectArray cp_patches_jh,
 739                                  u1** temp_alloc,
 740                                  TRAPS) {
 741   assert(host_class != NULL, "host_class must not be NULL");
 742   assert(data != NULL, "data must not be NULL");
 743 
 744   if (UsePerfData) {
 745     ClassLoader::unsafe_defineClassCallCounter()->inc();
 746   }
 747 
 748   jint length = typeArrayOop(JNIHandles::resolve_non_null(data))->length();
 749   assert(length >= 0, "class_bytes_length must not be negative: %d", length);
 750 
 751   int class_bytes_length = (int) length;
 752 
 753   u1* class_bytes = NEW_C_HEAP_ARRAY(u1, length, mtInternal);
 754   if (class_bytes == NULL) {
 755     THROW_0(vmSymbols::java_lang_OutOfMemoryError());
 756   }
 757 
 758   // caller responsible to free it:
 759   *temp_alloc = class_bytes;
 760 
 761   jbyte* array_base = typeArrayOop(JNIHandles::resolve_non_null(data))->byte_at_addr(0);
 762   Copy::conjoint_jbytes(array_base, class_bytes, length);
 763 
 764   objArrayHandle cp_patches_h;
 765   if (cp_patches_jh != NULL) {
 766     oop p = JNIHandles::resolve_non_null(cp_patches_jh);
 767     assert(p->is_objArray(), "cp_patches must be an object[]");
 768     cp_patches_h = objArrayHandle(THREAD, (objArrayOop)p);
 769   }
 770 
 771   const Klass* host_klass = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(host_class));
 772 
 773   // Make sure it's the real host class, not another anonymous class.
 774   while (host_klass != NULL && host_klass->is_instance_klass() &&
 775          InstanceKlass::cast(host_klass)->is_anonymous()) {
 776     host_klass = InstanceKlass::cast(host_klass)->host_klass();
 777   }
 778 
 779   // Primitive types have NULL Klass* fields in their java.lang.Class instances.
 780   if (host_klass == NULL) {
 781     THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(), "Host class is null");
 782   }
 783 
 784   assert(host_klass->is_instance_klass(), "Host class must be an instance class");
 785 
 786   const char* host_source = host_klass->external_name();
 787   Handle      host_loader(THREAD, host_klass->class_loader());
 788   Handle      host_domain(THREAD, host_klass->protection_domain());
 789 
 790   GrowableArray<Handle>* cp_patches = NULL;
 791 
 792   if (cp_patches_h.not_null()) {
 793     int alen = cp_patches_h->length();
 794 
 795     for (int i = alen-1; i >= 0; i--) {
 796       oop p = cp_patches_h->obj_at(i);
 797       if (p != NULL) {
 798         Handle patch(THREAD, p);
 799 
 800         if (cp_patches == NULL) {
 801           cp_patches = new GrowableArray<Handle>(i+1, i+1, Handle());
 802         }
 803 
 804         cp_patches->at_put(i, patch);
 805       }
 806     }
 807   }
 808 
 809   ClassFileStream st(class_bytes, class_bytes_length, host_source, ClassFileStream::verify);
 810 
 811   Symbol* no_class_name = NULL;
 812   Klass* anonk = SystemDictionary::parse_stream(no_class_name,
 813                                                 host_loader,
 814                                                 host_domain,
 815                                                 &st,
 816                                                 InstanceKlass::cast(host_klass),
 817                                                 cp_patches,
 818                                                 CHECK_NULL);
 819   if (anonk == NULL) {
 820     return NULL;
 821   }
 822 
 823   return InstanceKlass::cast(anonk);
 824 }
 825 
 826 UNSAFE_ENTRY(jclass, Unsafe_DefineAnonymousClass0(JNIEnv *env, jobject unsafe, jclass host_class, jbyteArray data, jobjectArray cp_patches_jh)) {
 827   ResourceMark rm(THREAD);
 828 
 829   jobject res_jh = NULL;
 830   u1* temp_alloc = NULL;
 831 
 832   InstanceKlass* anon_klass = Unsafe_DefineAnonymousClass_impl(env, host_class, data, cp_patches_jh, &temp_alloc, THREAD);
 833   if (anon_klass != NULL) {
 834     res_jh = JNIHandles::make_local(env, anon_klass->java_mirror());
 835   }
 836 
 837   // try/finally clause:
 838   if (temp_alloc != NULL) {
 839     FREE_C_HEAP_ARRAY(u1, temp_alloc);
 840   }
 841 
 842   // The anonymous class loader data has been artificially been kept alive to
 843   // this point.   The mirror and any instances of this class have to keep
 844   // it alive afterwards.
 845   if (anon_klass != NULL) {
 846     anon_klass->class_loader_data()->dec_keep_alive();
 847   }
 848 
 849   // let caller initialize it as needed...
 850 
 851   return (jclass) res_jh;
 852 } UNSAFE_END
 853 
 854 
 855 
 856 UNSAFE_ENTRY(void, Unsafe_ThrowException(JNIEnv *env, jobject unsafe, jthrowable thr)) {
 857   ThreadToNativeFromVM ttnfv(thread);
 858   env->Throw(thr);
 859 } UNSAFE_END
 860 
 861 // JSR166 ------------------------------------------------------------------
 862 
 863 UNSAFE_ENTRY(jobject, Unsafe_CompareAndExchangeObject(JNIEnv *env, jobject unsafe, jobject obj, jlong offset, jobject e_h, jobject x_h)) {
 864   oop x = JNIHandles::resolve(x_h);
 865   oop e = JNIHandles::resolve(e_h);
 866   oop p = JNIHandles::resolve(obj);
 867   assert_field_offset_sane(p, offset);
 868   oop res = HeapAccess<ON_UNKNOWN_OOP_REF>::oop_atomic_cmpxchg_at(x, p, (ptrdiff_t)offset, e);
 869   return JNIHandles::make_local(env, res);
 870 } UNSAFE_END
 871 
 872 UNSAFE_ENTRY(jint, Unsafe_CompareAndExchangeInt(JNIEnv *env, jobject unsafe, jobject obj, jlong offset, jint e, jint x)) {
 873   oop p = JNIHandles::resolve(obj);
 874   if (oopDesc::is_null(p)) {
 875     volatile jint* addr = (volatile jint*)index_oop_from_field_offset_long(p, offset);
 876     return RawAccess<>::atomic_cmpxchg(x, addr, e);
 877   } else {
 878     assert_field_offset_sane(p, offset);
 879     return HeapAccess<>::atomic_cmpxchg_at(x, p, (ptrdiff_t)offset, e);
 880   }
 881 } UNSAFE_END
 882 
 883 UNSAFE_ENTRY(jlong, Unsafe_CompareAndExchangeLong(JNIEnv *env, jobject unsafe, jobject obj, jlong offset, jlong e, jlong x)) {
 884   oop p = JNIHandles::resolve(obj);
 885   if (oopDesc::is_null(p)) {
 886     volatile jlong* addr = (volatile jlong*)index_oop_from_field_offset_long(p, offset);
 887     return RawAccess<>::atomic_cmpxchg(x, addr, e);
 888   } else {
 889     assert_field_offset_sane(p, offset);
 890     return HeapAccess<>::atomic_cmpxchg_at(x, p, (ptrdiff_t)offset, e);
 891   }
 892 } UNSAFE_END
 893 
 894 UNSAFE_ENTRY(jboolean, Unsafe_CompareAndSetObject(JNIEnv *env, jobject unsafe, jobject obj, jlong offset, jobject e_h, jobject x_h)) {
 895   oop x = JNIHandles::resolve(x_h);
 896   oop e = JNIHandles::resolve(e_h);
 897   oop p = JNIHandles::resolve(obj);
 898   assert_field_offset_sane(p, offset);
 899   oop ret = HeapAccess<ON_UNKNOWN_OOP_REF>::oop_atomic_cmpxchg_at(x, p, (ptrdiff_t)offset, e);
 900   return ret == e;
 901 } UNSAFE_END
 902 
 903 UNSAFE_ENTRY(jboolean, Unsafe_CompareAndSetInt(JNIEnv *env, jobject unsafe, jobject obj, jlong offset, jint e, jint x)) {
 904   oop p = JNIHandles::resolve(obj);
 905   if (oopDesc::is_null(p)) {
 906     volatile jint* addr = (volatile jint*)index_oop_from_field_offset_long(p, offset);
 907     return RawAccess<>::atomic_cmpxchg(x, addr, e) == e;
 908   } else {
 909     assert_field_offset_sane(p, offset);
 910     return HeapAccess<>::atomic_cmpxchg_at(x, p, (ptrdiff_t)offset, e) == e;
 911   }
 912 } UNSAFE_END
 913 
 914 UNSAFE_ENTRY(jboolean, Unsafe_CompareAndSetLong(JNIEnv *env, jobject unsafe, jobject obj, jlong offset, jlong e, jlong x)) {
 915   oop p = JNIHandles::resolve(obj);
 916   if (oopDesc::is_null(p)) {
 917     volatile jlong* addr = (volatile jlong*)index_oop_from_field_offset_long(p, offset);
 918     return RawAccess<>::atomic_cmpxchg(x, addr, e) == e;
 919   } else {
 920     assert_field_offset_sane(p, offset);
 921     return HeapAccess<>::atomic_cmpxchg_at(x, p, (ptrdiff_t)offset, e) == e;
 922   }
 923 } UNSAFE_END
 924 
 925 UNSAFE_ENTRY(void, Unsafe_Park(JNIEnv *env, jobject unsafe, jboolean isAbsolute, jlong time)) {
 926   EventThreadPark event;
 927   HOTSPOT_THREAD_PARK_BEGIN((uintptr_t) thread->parker(), (int) isAbsolute, time);
 928 
 929   JavaThreadParkedState jtps(thread, time != 0);
 930   thread->parker()->park(isAbsolute != 0, time);
 931 
 932   HOTSPOT_THREAD_PARK_END((uintptr_t) thread->parker());
 933 
 934   if (event.should_commit()) {
 935     oop obj = thread->current_park_blocker();
 936     event.set_parkedClass((obj != NULL) ? obj->klass() : NULL);
 937     event.set_timeout(time);
 938     event.set_address((obj != NULL) ? (TYPE_ADDRESS) cast_from_oop<uintptr_t>(obj) : 0);
 939     event.commit();
 940   }
 941 } UNSAFE_END
 942 
 943 UNSAFE_ENTRY(void, Unsafe_Unpark(JNIEnv *env, jobject unsafe, jobject jthread)) {
 944   Parker* p = NULL;
 945 
 946   if (jthread != NULL) {
 947     ThreadsListHandle tlh;
 948     JavaThread* thr = NULL;
 949     oop java_thread = NULL;
 950     (void) tlh.cv_internal_thread_to_JavaThread(jthread, &thr, &java_thread);
 951     if (java_thread != NULL) {
 952       // This is a valid oop.
 953       jlong lp = java_lang_Thread::park_event(java_thread);
 954       if (lp != 0) {
 955         // This cast is OK even though the jlong might have been read
 956         // non-atomically on 32bit systems, since there, one word will
 957         // always be zero anyway and the value set is always the same
 958         p = (Parker*)addr_from_java(lp);
 959       } else {
 960         // Not cached in the java.lang.Thread oop yet (could be an
 961         // older version of library).
 962         if (thr != NULL) {
 963           // The JavaThread is alive.
 964           p = thr->parker();
 965           if (p != NULL) {
 966             // Cache the Parker in the java.lang.Thread oop for next time.
 967             java_lang_Thread::set_park_event(java_thread, addr_to_java(p));
 968           }
 969         }
 970       }
 971     }
 972   } // ThreadsListHandle is destroyed here.
 973 
 974   if (p != NULL) {
 975     HOTSPOT_THREAD_UNPARK((uintptr_t) p);
 976     p->unpark();
 977   }
 978 } UNSAFE_END
 979 
 980 UNSAFE_ENTRY(jint, Unsafe_GetLoadAverage0(JNIEnv *env, jobject unsafe, jdoubleArray loadavg, jint nelem)) {
 981   const int max_nelem = 3;
 982   double la[max_nelem];
 983   jint ret;
 984 
 985   typeArrayOop a = typeArrayOop(JNIHandles::resolve_non_null(loadavg));
 986   assert(a->is_typeArray(), "must be type array");
 987 
 988   ret = os::loadavg(la, nelem);
 989   if (ret == -1) {
 990     return -1;
 991   }
 992 
 993   // if successful, ret is the number of samples actually retrieved.
 994   assert(ret >= 0 && ret <= max_nelem, "Unexpected loadavg return value");
 995   switch(ret) {
 996     case 3: a->double_at_put(2, (jdouble)la[2]); // fall through
 997     case 2: a->double_at_put(1, (jdouble)la[1]); // fall through
 998     case 1: a->double_at_put(0, (jdouble)la[0]); break;
 999   }
1000 
1001   return ret;
1002 } UNSAFE_END
1003 
1004 
1005 /// JVM_RegisterUnsafeMethods
1006 
1007 #define ADR "J"
1008 
1009 #define LANG "Ljava/lang/"
1010 
1011 #define OBJ LANG "Object;"
1012 #define CLS LANG "Class;"
1013 #define FLD LANG "reflect/Field;"
1014 #define THR LANG "Throwable;"
1015 
1016 #define DC_Args  LANG "String;[BII" LANG "ClassLoader;" "Ljava/security/ProtectionDomain;"
1017 #define DAC_Args CLS "[B[" OBJ
1018 
1019 #define CC (char*)  /*cast a literal from (const char*)*/
1020 #define FN_PTR(f) CAST_FROM_FN_PTR(void*, &f)
1021 
1022 #define DECLARE_GETPUTOOP(Type, Desc) \
1023     {CC "get" #Type,      CC "(" OBJ "J)" #Desc,       FN_PTR(Unsafe_Get##Type)}, \
1024     {CC "put" #Type,      CC "(" OBJ "J" #Desc ")V",   FN_PTR(Unsafe_Put##Type)}, \
1025     {CC "get" #Type "Volatile",      CC "(" OBJ "J)" #Desc,       FN_PTR(Unsafe_Get##Type##Volatile)}, \
1026     {CC "put" #Type "Volatile",      CC "(" OBJ "J" #Desc ")V",   FN_PTR(Unsafe_Put##Type##Volatile)}
1027 
1028 
1029 static JNINativeMethod jdk_internal_misc_Unsafe_methods[] = {
1030     {CC "getObject",        CC "(" OBJ "J)" OBJ "",   FN_PTR(Unsafe_GetObject)},
1031     {CC "putObject",        CC "(" OBJ "J" OBJ ")V",  FN_PTR(Unsafe_PutObject)},
1032     {CC "getObjectVolatile",CC "(" OBJ "J)" OBJ "",   FN_PTR(Unsafe_GetObjectVolatile)},
1033     {CC "putObjectVolatile",CC "(" OBJ "J" OBJ ")V",  FN_PTR(Unsafe_PutObjectVolatile)},
1034 
1035     {CC "getUncompressedObject", CC "(" ADR ")" OBJ,  FN_PTR(Unsafe_GetUncompressedObject)},
1036 
1037     DECLARE_GETPUTOOP(Boolean, Z),
1038     DECLARE_GETPUTOOP(Byte, B),
1039     DECLARE_GETPUTOOP(Short, S),
1040     DECLARE_GETPUTOOP(Char, C),
1041     DECLARE_GETPUTOOP(Int, I),
1042     DECLARE_GETPUTOOP(Long, J),
1043     DECLARE_GETPUTOOP(Float, F),
1044     DECLARE_GETPUTOOP(Double, D),
1045 
1046     {CC "allocateMemory0",    CC "(J)" ADR,              FN_PTR(Unsafe_AllocateMemory0)},
1047     {CC "reallocateMemory0",  CC "(" ADR "J)" ADR,       FN_PTR(Unsafe_ReallocateMemory0)},
1048     {CC "freeMemory0",        CC "(" ADR ")V",           FN_PTR(Unsafe_FreeMemory0)},
1049 
1050     {CC "objectFieldOffset0", CC "(" FLD ")J",           FN_PTR(Unsafe_ObjectFieldOffset0)},
1051     {CC "objectFieldOffset1", CC "(" CLS LANG "String;)J", FN_PTR(Unsafe_ObjectFieldOffset1)},
1052     {CC "staticFieldOffset0", CC "(" FLD ")J",           FN_PTR(Unsafe_StaticFieldOffset0)},
1053     {CC "staticFieldBase0",   CC "(" FLD ")" OBJ,        FN_PTR(Unsafe_StaticFieldBase0)},
1054     {CC "ensureClassInitialized0", CC "(" CLS ")V",      FN_PTR(Unsafe_EnsureClassInitialized0)},
1055     {CC "arrayBaseOffset0",   CC "(" CLS ")I",           FN_PTR(Unsafe_ArrayBaseOffset0)},
1056     {CC "arrayIndexScale0",   CC "(" CLS ")I",           FN_PTR(Unsafe_ArrayIndexScale0)},
1057     {CC "addressSize0",       CC "()I",                  FN_PTR(Unsafe_AddressSize0)},
1058     {CC "pageSize",           CC "()I",                  FN_PTR(Unsafe_PageSize)},
1059 
1060     {CC "defineClass0",       CC "(" DC_Args ")" CLS,    FN_PTR(Unsafe_DefineClass0)},
1061     {CC "allocateInstance",   CC "(" CLS ")" OBJ,        FN_PTR(Unsafe_AllocateInstance)},
1062     {CC "throwException",     CC "(" THR ")V",           FN_PTR(Unsafe_ThrowException)},
1063     {CC "compareAndSetObject",CC "(" OBJ "J" OBJ "" OBJ ")Z", FN_PTR(Unsafe_CompareAndSetObject)},
1064     {CC "compareAndSetInt",   CC "(" OBJ "J""I""I"")Z",  FN_PTR(Unsafe_CompareAndSetInt)},
1065     {CC "compareAndSetLong",  CC "(" OBJ "J""J""J"")Z",  FN_PTR(Unsafe_CompareAndSetLong)},
1066     {CC "compareAndExchangeObject", CC "(" OBJ "J" OBJ "" OBJ ")" OBJ, FN_PTR(Unsafe_CompareAndExchangeObject)},
1067     {CC "compareAndExchangeInt",  CC "(" OBJ "J""I""I"")I", FN_PTR(Unsafe_CompareAndExchangeInt)},
1068     {CC "compareAndExchangeLong", CC "(" OBJ "J""J""J"")J", FN_PTR(Unsafe_CompareAndExchangeLong)},
1069 
1070     {CC "park",               CC "(ZJ)V",                FN_PTR(Unsafe_Park)},
1071     {CC "unpark",             CC "(" OBJ ")V",           FN_PTR(Unsafe_Unpark)},
1072 
1073     {CC "getLoadAverage0",    CC "([DI)I",               FN_PTR(Unsafe_GetLoadAverage0)},
1074 
1075     {CC "copyMemory0",        CC "(" OBJ "J" OBJ "JJ)V", FN_PTR(Unsafe_CopyMemory0)},
1076     {CC "copySwapMemory0",    CC "(" OBJ "J" OBJ "JJJ)V", FN_PTR(Unsafe_CopySwapMemory0)},
1077     {CC "setMemory0",         CC "(" OBJ "JJB)V",        FN_PTR(Unsafe_SetMemory0)},
1078 
1079     {CC "defineAnonymousClass0", CC "(" DAC_Args ")" CLS, FN_PTR(Unsafe_DefineAnonymousClass0)},
1080 
1081     {CC "shouldBeInitialized0", CC "(" CLS ")Z",         FN_PTR(Unsafe_ShouldBeInitialized0)},
1082 
1083     {CC "loadFence",          CC "()V",                  FN_PTR(Unsafe_LoadFence)},
1084     {CC "storeFence",         CC "()V",                  FN_PTR(Unsafe_StoreFence)},
1085     {CC "fullFence",          CC "()V",                  FN_PTR(Unsafe_FullFence)},
1086 
1087     {CC "isBigEndian0",       CC "()Z",                  FN_PTR(Unsafe_isBigEndian0)},
1088     {CC "unalignedAccess0",   CC "()Z",                  FN_PTR(Unsafe_unalignedAccess0)}
1089 };
1090 
1091 #undef CC
1092 #undef FN_PTR
1093 
1094 #undef ADR
1095 #undef LANG
1096 #undef OBJ
1097 #undef CLS
1098 #undef FLD
1099 #undef THR
1100 #undef DC_Args
1101 #undef DAC_Args
1102 
1103 #undef DECLARE_GETPUTOOP
1104 
1105 
1106 // This function is exported, used by NativeLookup.
1107 // The Unsafe_xxx functions above are called only from the interpreter.
1108 // The optimizer looks at names and signatures to recognize
1109 // individual functions.
1110 
1111 JVM_ENTRY(void, JVM_RegisterJDKInternalMiscUnsafeMethods(JNIEnv *env, jclass unsafeclass)) {
1112   ThreadToNativeFromVM ttnfv(thread);
1113 
1114   int ok = env->RegisterNatives(unsafeclass, jdk_internal_misc_Unsafe_methods, sizeof(jdk_internal_misc_Unsafe_methods)/sizeof(JNINativeMethod));
1115   guarantee(ok == 0, "register jdk.internal.misc.Unsafe natives");
1116 } JVM_END