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