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