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