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