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