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