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