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