1 /* 2 * Copyright (c) 2000, 2021, 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/classLoadInfo.hpp" 31 #include "classfile/javaClasses.inline.hpp" 32 #include "classfile/systemDictionary.hpp" 33 #include "classfile/vmSymbols.hpp" 34 #include "jfr/jfrEvents.hpp" 35 #include "memory/allocation.inline.hpp" 36 #include "memory/resourceArea.hpp" 37 #include "logging/log.hpp" 38 #include "logging/logStream.hpp" 39 #include "oops/access.inline.hpp" 40 #include "oops/fieldStreams.inline.hpp" 41 #include "oops/flatArrayKlass.hpp" 42 #include "oops/flatArrayOop.inline.hpp" 43 #include "oops/inlineKlass.inline.hpp" 44 #include "oops/instanceKlass.inline.hpp" 45 #include "oops/klass.inline.hpp" 46 #include "oops/objArrayOop.inline.hpp" 47 #include "oops/oop.inline.hpp" 48 #include "oops/typeArrayOop.inline.hpp" 49 #include "prims/jvmtiExport.hpp" 50 #include "prims/unsafe.hpp" 51 #include "runtime/fieldDescriptor.inline.hpp" 52 #include "runtime/globals.hpp" 53 #include "runtime/handles.inline.hpp" 54 #include "runtime/interfaceSupport.inline.hpp" 55 #include "runtime/jniHandles.inline.hpp" 56 #include "runtime/orderAccess.hpp" 57 #include "runtime/reflection.hpp" 58 #include "runtime/sharedRuntime.hpp" 59 #include "runtime/stubRoutines.hpp" 60 #include "runtime/thread.hpp" 61 #include "runtime/threadSMR.hpp" 62 #include "runtime/vmOperations.hpp" 63 #include "runtime/vm_version.hpp" 64 #include "services/threadService.hpp" 65 #include "utilities/align.hpp" 66 #include "utilities/copy.hpp" 67 #include "utilities/dtrace.hpp" 68 #include "utilities/macros.hpp" 69 70 /** 71 * Implementation of the jdk.internal.misc.Unsafe class 72 */ 73 74 75 #define MAX_OBJECT_SIZE \ 76 ( arrayOopDesc::header_size(T_DOUBLE) * HeapWordSize \ 77 + ((julong)max_jint * sizeof(double)) ) 78 79 80 #define UNSAFE_ENTRY(result_type, header) \ 81 JVM_ENTRY(static result_type, header) 82 83 #define UNSAFE_LEAF(result_type, header) \ 84 JVM_LEAF(static result_type, header) 85 86 #define UNSAFE_END JVM_END 87 88 89 static inline void* addr_from_java(jlong addr) { 90 // This assert fails in a variety of ways on 32-bit systems. 91 // It is impossible to predict whether native code that converts 92 // pointers to longs will sign-extend or zero-extend the addresses. 93 //assert(addr == (uintptr_t)addr, "must not be odd high bits"); 94 return (void*)(uintptr_t)addr; 95 } 96 97 static inline jlong addr_to_java(void* p) { 98 assert(p == (void*)(uintptr_t)p, "must not be odd high bits"); 99 return (uintptr_t)p; 100 } 101 102 103 // Note: The VM's obj_field and related accessors use byte-scaled 104 // ("unscaled") offsets, just as the unsafe methods do. 105 106 // However, the method Unsafe.fieldOffset explicitly declines to 107 // guarantee this. The field offset values manipulated by the Java user 108 // through the Unsafe API are opaque cookies that just happen to be byte 109 // offsets. We represent this state of affairs by passing the cookies 110 // through conversion functions when going between the VM and the Unsafe API. 111 // The conversion functions just happen to be no-ops at present. 112 113 static inline jlong field_offset_to_byte_offset(jlong field_offset) { 114 return field_offset; 115 } 116 117 static inline jlong field_offset_from_byte_offset(jlong byte_offset) { 118 return byte_offset; 119 } 120 121 static inline void assert_field_offset_sane(oop p, jlong field_offset) { 122 #ifdef ASSERT 123 jlong byte_offset = field_offset_to_byte_offset(field_offset); 124 125 if (p != NULL) { 126 assert(byte_offset >= 0 && byte_offset <= (jlong)MAX_OBJECT_SIZE, "sane offset"); 127 if (byte_offset == (jint)byte_offset) { 128 void* ptr_plus_disp = cast_from_oop<address>(p) + byte_offset; 129 assert(p->field_addr<void>((jint)byte_offset) == ptr_plus_disp, 130 "raw [ptr+disp] must be consistent with oop::field_addr"); 131 } 132 jlong p_size = HeapWordSize * (jlong)(p->size()); 133 assert(byte_offset < p_size, "Unsafe access: offset " INT64_FORMAT " > object's size " INT64_FORMAT, (int64_t)byte_offset, (int64_t)p_size); 134 } 135 #endif 136 } 137 138 static inline void* index_oop_from_field_offset_long(oop p, jlong field_offset) { 139 assert_field_offset_sane(p, field_offset); 140 jlong byte_offset = field_offset_to_byte_offset(field_offset); 141 142 if (sizeof(char*) == sizeof(jint)) { // (this constant folds!) 143 return cast_from_oop<address>(p) + (jint) byte_offset; 144 } else { 145 return cast_from_oop<address>(p) + byte_offset; 146 } 147 } 148 149 // Externally callable versions: 150 // (Use these in compiler intrinsics which emulate unsafe primitives.) 151 jlong Unsafe_field_offset_to_byte_offset(jlong field_offset) { 152 return field_offset; 153 } 154 jlong Unsafe_field_offset_from_byte_offset(jlong byte_offset) { 155 return byte_offset; 156 } 157 158 ///// Data read/writes on the Java heap and in native (off-heap) memory 159 160 /** 161 * Helper class to wrap memory accesses in JavaThread::doing_unsafe_access() 162 */ 163 class GuardUnsafeAccess { 164 JavaThread* _thread; 165 166 public: 167 GuardUnsafeAccess(JavaThread* thread) : _thread(thread) { 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 _thread->set_doing_unsafe_access(true); 172 } 173 174 ~GuardUnsafeAccess() { 175 _thread->set_doing_unsafe_access(false); 176 } 177 }; 178 179 /** 180 * Helper class for accessing memory. 181 * 182 * Normalizes values and wraps accesses in 183 * JavaThread::doing_unsafe_access() if needed. 184 */ 185 template <typename T> 186 class MemoryAccess : StackObj { 187 JavaThread* _thread; 188 oop _obj; 189 ptrdiff_t _offset; 190 191 // Resolves and returns the address of the memory access. 192 // This raw memory access may fault, so we make sure it happens within the 193 // guarded scope by making the access volatile at least. Since the store 194 // of Thread::set_doing_unsafe_access() is also volatile, these accesses 195 // can not be reordered by the compiler. Therefore, if the access triggers 196 // a fault, we will know that Thread::doing_unsafe_access() returns true. 197 volatile T* addr() { 198 void* addr = index_oop_from_field_offset_long(_obj, _offset); 199 return static_cast<volatile T*>(addr); 200 } 201 202 template <typename U> 203 U normalize_for_write(U x) { 204 return x; 205 } 206 207 jboolean normalize_for_write(jboolean x) { 208 return x & 1; 209 } 210 211 template <typename U> 212 U normalize_for_read(U x) { 213 return x; 214 } 215 216 jboolean normalize_for_read(jboolean x) { 217 return x != 0; 218 } 219 220 public: 221 MemoryAccess(JavaThread* thread, jobject obj, jlong offset) 222 : _thread(thread), _obj(JNIHandles::resolve(obj)), _offset((ptrdiff_t)offset) { 223 assert_field_offset_sane(_obj, offset); 224 } 225 226 T get() { 227 GuardUnsafeAccess guard(_thread); 228 return normalize_for_read(*addr()); 229 } 230 231 void put(T x) { 232 GuardUnsafeAccess guard(_thread); 233 assert(_obj == NULL || !_obj->is_inline_type() || _obj->mark().is_larval_state(), "must be an object instance or a larval inline type"); 234 *addr() = normalize_for_write(x); 235 } 236 237 T get_volatile() { 238 GuardUnsafeAccess guard(_thread); 239 volatile T ret = RawAccess<MO_SEQ_CST>::load(addr()); 240 return normalize_for_read(ret); 241 } 242 243 void put_volatile(T x) { 244 GuardUnsafeAccess guard(_thread); 245 RawAccess<MO_SEQ_CST>::store(addr(), normalize_for_write(x)); 246 } 247 }; 248 249 #ifdef ASSERT 250 /* 251 * Get the field descriptor of the field of the given object at the given offset. 252 */ 253 static bool get_field_descriptor(oop p, jlong offset, fieldDescriptor* fd) { 254 bool found = false; 255 Klass* k = p->klass(); 256 if (k->is_instance_klass()) { 257 InstanceKlass* ik = InstanceKlass::cast(k); 258 found = ik->find_field_from_offset((int)offset, false, fd); 259 if (!found && ik->is_mirror_instance_klass()) { 260 Klass* k2 = java_lang_Class::as_Klass(p); 261 if (k2->is_instance_klass()) { 262 ik = InstanceKlass::cast(k2); 263 found = ik->find_field_from_offset((int)offset, true, fd); 264 } 265 } 266 } 267 return found; 268 } 269 #endif // ASSERT 270 271 static void assert_and_log_unsafe_value_access(oop p, jlong offset, InlineKlass* vk) { 272 Klass* k = p->klass(); 273 #ifdef ASSERT 274 if (k->is_instance_klass()) { 275 assert_field_offset_sane(p, offset); 276 fieldDescriptor fd; 277 bool found = get_field_descriptor(p, offset, &fd); 278 if (found) { 279 assert(found, "value field not found"); 280 assert(fd.is_inlined(), "field not flat"); 281 } else { 282 if (log_is_enabled(Trace, valuetypes)) { 283 log_trace(valuetypes)("not a field in %s at offset " UINT64_FORMAT_X, 284 p->klass()->external_name(), (uint64_t)offset); 285 } 286 } 287 } else if (k->is_flatArray_klass()) { 288 FlatArrayKlass* vak = FlatArrayKlass::cast(k); 289 int index = (offset - vak->array_header_in_bytes()) / vak->element_byte_size(); 290 address dest = (address)((flatArrayOop)p)->value_at_addr(index, vak->layout_helper()); 291 assert(dest == (cast_from_oop<address>(p) + offset), "invalid offset"); 292 } else { 293 ShouldNotReachHere(); 294 } 295 #endif // ASSERT 296 if (log_is_enabled(Trace, valuetypes)) { 297 if (k->is_flatArray_klass()) { 298 FlatArrayKlass* vak = FlatArrayKlass::cast(k); 299 int index = (offset - vak->array_header_in_bytes()) / vak->element_byte_size(); 300 address dest = (address)((flatArrayOop)p)->value_at_addr(index, vak->layout_helper()); 301 log_trace(valuetypes)("%s array type %s index %d element size %d offset " UINT64_FORMAT_X " at " INTPTR_FORMAT, 302 p->klass()->external_name(), vak->external_name(), 303 index, vak->element_byte_size(), (uint64_t)offset, p2i(dest)); 304 } else { 305 log_trace(valuetypes)("%s field type %s at offset " UINT64_FORMAT_X, 306 p->klass()->external_name(), vk->external_name(), (uint64_t)offset); 307 } 308 } 309 } 310 311 // These functions allow a null base pointer with an arbitrary address. 312 // But if the base pointer is non-null, the offset should make some sense. 313 // That is, it should be in the range [0, MAX_OBJECT_SIZE]. 314 UNSAFE_ENTRY(jobject, Unsafe_GetReference(JNIEnv *env, jobject unsafe, jobject obj, jlong offset)) { 315 oop p = JNIHandles::resolve(obj); 316 assert_field_offset_sane(p, offset); 317 oop v = HeapAccess<ON_UNKNOWN_OOP_REF>::oop_load_at(p, offset); 318 return JNIHandles::make_local(THREAD, v); 319 } UNSAFE_END 320 321 UNSAFE_ENTRY(void, Unsafe_PutReference(JNIEnv *env, jobject unsafe, jobject obj, jlong offset, jobject x_h)) { 322 oop x = JNIHandles::resolve(x_h); 323 oop p = JNIHandles::resolve(obj); 324 assert_field_offset_sane(p, offset); 325 assert(!p->is_inline_type() || p->mark().is_larval_state(), "must be an object instance or a larval inline type"); 326 HeapAccess<ON_UNKNOWN_OOP_REF>::oop_store_at(p, offset, x); 327 } UNSAFE_END 328 329 UNSAFE_ENTRY(jlong, Unsafe_ValueHeaderSize(JNIEnv *env, jobject unsafe, jclass c)) { 330 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(c)); 331 InlineKlass* vk = InlineKlass::cast(k); 332 return vk->first_field_offset(); 333 } UNSAFE_END 334 335 UNSAFE_ENTRY(jboolean, Unsafe_IsFlattenedField(JNIEnv *env, jobject unsafe, jobject o)) { 336 oop f = JNIHandles::resolve_non_null(o); 337 Klass* k = java_lang_Class::as_Klass(java_lang_reflect_Field::clazz(f)); 338 int slot = java_lang_reflect_Field::slot(f); 339 return InstanceKlass::cast(k)->field_is_inlined(slot); 340 } UNSAFE_END 341 342 UNSAFE_ENTRY(jboolean, Unsafe_IsFlattenedArray(JNIEnv *env, jobject unsafe, jclass c)) { 343 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(c)); 344 return k->is_flatArray_klass(); 345 } UNSAFE_END 346 347 UNSAFE_ENTRY(jobject, Unsafe_UninitializedDefaultValue(JNIEnv *env, jobject unsafe, jclass vc)) { 348 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(vc)); 349 InlineKlass* vk = InlineKlass::cast(k); 350 oop v = vk->default_value(); 351 return JNIHandles::make_local(THREAD, v); 352 } UNSAFE_END 353 354 UNSAFE_ENTRY(jobject, Unsafe_GetValue(JNIEnv *env, jobject unsafe, jobject obj, jlong offset, jclass vc)) { 355 oop base = JNIHandles::resolve(obj); 356 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(vc)); 357 InlineKlass* vk = InlineKlass::cast(k); 358 assert_and_log_unsafe_value_access(base, offset, vk); 359 Handle base_h(THREAD, base); 360 oop v = vk->read_inlined_field(base_h(), offset, CHECK_NULL); 361 return JNIHandles::make_local(THREAD, v); 362 } UNSAFE_END 363 364 UNSAFE_ENTRY(void, Unsafe_PutValue(JNIEnv *env, jobject unsafe, jobject obj, jlong offset, jclass vc, jobject value)) { 365 oop base = JNIHandles::resolve(obj); 366 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(vc)); 367 InlineKlass* vk = InlineKlass::cast(k); 368 assert(!base->is_inline_type() || base->mark().is_larval_state(), "must be an object instance or a larval inline type"); 369 assert_and_log_unsafe_value_access(base, offset, vk); 370 oop v = JNIHandles::resolve(value); 371 vk->write_inlined_field(base, offset, v, CHECK); 372 } UNSAFE_END 373 374 UNSAFE_ENTRY(jobject, Unsafe_MakePrivateBuffer(JNIEnv *env, jobject unsafe, jobject value)) { 375 oop v = JNIHandles::resolve_non_null(value); 376 assert(v->is_inline_type(), "must be an inline type instance"); 377 Handle vh(THREAD, v); 378 InlineKlass* vk = InlineKlass::cast(v->klass()); 379 instanceOop new_value = vk->allocate_instance_buffer(CHECK_NULL); 380 vk->inline_copy_oop_to_new_oop(vh(), new_value); 381 markWord mark = new_value->mark(); 382 new_value->set_mark(mark.enter_larval_state()); 383 return JNIHandles::make_local(THREAD, new_value); 384 } UNSAFE_END 385 386 UNSAFE_ENTRY(jobject, Unsafe_FinishPrivateBuffer(JNIEnv *env, jobject unsafe, jobject value)) { 387 oop v = JNIHandles::resolve(value); 388 assert(v->mark().is_larval_state(), "must be a larval value"); 389 markWord mark = v->mark(); 390 v->set_mark(mark.exit_larval_state()); 391 return JNIHandles::make_local(THREAD, v); 392 } UNSAFE_END 393 394 UNSAFE_ENTRY(jobject, Unsafe_GetReferenceVolatile(JNIEnv *env, jobject unsafe, jobject obj, jlong offset)) { 395 oop p = JNIHandles::resolve(obj); 396 assert_field_offset_sane(p, offset); 397 oop v = HeapAccess<MO_SEQ_CST | ON_UNKNOWN_OOP_REF>::oop_load_at(p, offset); 398 return JNIHandles::make_local(THREAD, v); 399 } UNSAFE_END 400 401 UNSAFE_ENTRY(void, Unsafe_PutReferenceVolatile(JNIEnv *env, jobject unsafe, jobject obj, jlong offset, jobject x_h)) { 402 oop x = JNIHandles::resolve(x_h); 403 oop p = JNIHandles::resolve(obj); 404 assert_field_offset_sane(p, offset); 405 HeapAccess<MO_SEQ_CST | ON_UNKNOWN_OOP_REF>::oop_store_at(p, offset, x); 406 } UNSAFE_END 407 408 UNSAFE_ENTRY(jobject, Unsafe_GetUncompressedObject(JNIEnv *env, jobject unsafe, jlong addr)) { 409 oop v = *(oop*) (address) addr; 410 return JNIHandles::make_local(THREAD, v); 411 } UNSAFE_END 412 413 #define DEFINE_GETSETOOP(java_type, Type) \ 414 \ 415 UNSAFE_ENTRY(java_type, Unsafe_Get##Type(JNIEnv *env, jobject unsafe, jobject obj, jlong offset)) { \ 416 return MemoryAccess<java_type>(thread, obj, offset).get(); \ 417 } UNSAFE_END \ 418 \ 419 UNSAFE_ENTRY(void, Unsafe_Put##Type(JNIEnv *env, jobject unsafe, jobject obj, jlong offset, java_type x)) { \ 420 MemoryAccess<java_type>(thread, obj, offset).put(x); \ 421 } UNSAFE_END \ 422 \ 423 // END DEFINE_GETSETOOP. 424 425 DEFINE_GETSETOOP(jboolean, Boolean) 426 DEFINE_GETSETOOP(jbyte, Byte) 427 DEFINE_GETSETOOP(jshort, Short); 428 DEFINE_GETSETOOP(jchar, Char); 429 DEFINE_GETSETOOP(jint, Int); 430 DEFINE_GETSETOOP(jlong, Long); 431 DEFINE_GETSETOOP(jfloat, Float); 432 DEFINE_GETSETOOP(jdouble, Double); 433 434 #undef DEFINE_GETSETOOP 435 436 #define DEFINE_GETSETOOP_VOLATILE(java_type, Type) \ 437 \ 438 UNSAFE_ENTRY(java_type, Unsafe_Get##Type##Volatile(JNIEnv *env, jobject unsafe, jobject obj, jlong offset)) { \ 439 return MemoryAccess<java_type>(thread, obj, offset).get_volatile(); \ 440 } UNSAFE_END \ 441 \ 442 UNSAFE_ENTRY(void, Unsafe_Put##Type##Volatile(JNIEnv *env, jobject unsafe, jobject obj, jlong offset, java_type x)) { \ 443 MemoryAccess<java_type>(thread, obj, offset).put_volatile(x); \ 444 } UNSAFE_END \ 445 \ 446 // END DEFINE_GETSETOOP_VOLATILE. 447 448 DEFINE_GETSETOOP_VOLATILE(jboolean, Boolean) 449 DEFINE_GETSETOOP_VOLATILE(jbyte, Byte) 450 DEFINE_GETSETOOP_VOLATILE(jshort, Short); 451 DEFINE_GETSETOOP_VOLATILE(jchar, Char); 452 DEFINE_GETSETOOP_VOLATILE(jint, Int); 453 DEFINE_GETSETOOP_VOLATILE(jlong, Long); 454 DEFINE_GETSETOOP_VOLATILE(jfloat, Float); 455 DEFINE_GETSETOOP_VOLATILE(jdouble, Double); 456 457 #undef DEFINE_GETSETOOP_VOLATILE 458 459 UNSAFE_LEAF(void, Unsafe_FullFence(JNIEnv *env, jobject unsafe)) { 460 OrderAccess::fence(); 461 } UNSAFE_END 462 463 ////// Allocation requests 464 465 UNSAFE_ENTRY(jobject, Unsafe_AllocateInstance(JNIEnv *env, jobject unsafe, jclass cls)) { 466 oop mirror = JNIHandles::resolve_non_null(cls); 467 InstanceKlass* k = InstanceKlass::cast(java_lang_Class::as_Klass(mirror)); 468 if (k == vmClasses::Object_klass() && vmClasses::Object_klass()->is_abstract()) { 469 k = vmClasses::Identity_klass(); 470 } 471 JvmtiVMObjectAllocEventCollector oam; 472 instanceOop i = InstanceKlass::allocate_instance(k->java_mirror(), CHECK_NULL); 473 return JNIHandles::make_local(THREAD, i); 474 } UNSAFE_END 475 476 UNSAFE_ENTRY(jlong, Unsafe_AllocateMemory0(JNIEnv *env, jobject unsafe, jlong size)) { 477 size_t sz = (size_t)size; 478 479 assert(is_aligned(sz, HeapWordSize), "sz not aligned"); 480 481 void* x = os::malloc(sz, mtOther); 482 483 return addr_to_java(x); 484 } UNSAFE_END 485 486 UNSAFE_ENTRY(jlong, Unsafe_ReallocateMemory0(JNIEnv *env, jobject unsafe, jlong addr, jlong size)) { 487 void* p = addr_from_java(addr); 488 size_t sz = (size_t)size; 489 490 assert(is_aligned(sz, HeapWordSize), "sz not aligned"); 491 492 void* x = os::realloc(p, sz, mtOther); 493 494 return addr_to_java(x); 495 } UNSAFE_END 496 497 UNSAFE_ENTRY(void, Unsafe_FreeMemory0(JNIEnv *env, jobject unsafe, jlong addr)) { 498 void* p = addr_from_java(addr); 499 500 os::free(p); 501 } UNSAFE_END 502 503 UNSAFE_ENTRY(void, Unsafe_SetMemory0(JNIEnv *env, jobject unsafe, jobject obj, jlong offset, jlong size, jbyte value)) { 504 size_t sz = (size_t)size; 505 506 oop base = JNIHandles::resolve(obj); 507 void* p = index_oop_from_field_offset_long(base, offset); 508 509 Copy::fill_to_memory_atomic(p, sz, value); 510 } UNSAFE_END 511 512 UNSAFE_ENTRY(void, Unsafe_CopyMemory0(JNIEnv *env, jobject unsafe, jobject srcObj, jlong srcOffset, jobject dstObj, jlong dstOffset, jlong size)) { 513 size_t sz = (size_t)size; 514 515 oop srcp = JNIHandles::resolve(srcObj); 516 oop dstp = JNIHandles::resolve(dstObj); 517 518 void* src = index_oop_from_field_offset_long(srcp, srcOffset); 519 void* dst = index_oop_from_field_offset_long(dstp, dstOffset); 520 { 521 GuardUnsafeAccess guard(thread); 522 if (StubRoutines::unsafe_arraycopy() != NULL) { 523 MACOS_AARCH64_ONLY(ThreadWXEnable wx(WXExec, thread)); 524 StubRoutines::UnsafeArrayCopy_stub()(src, dst, sz); 525 } else { 526 Copy::conjoint_memory_atomic(src, dst, sz); 527 } 528 } 529 } UNSAFE_END 530 531 // This function is a leaf since if the source and destination are both in native memory 532 // the copy may potentially be very large, and we don't want to disable GC if we can avoid it. 533 // If either source or destination (or both) are on the heap, the function will enter VM using 534 // JVM_ENTRY_FROM_LEAF 535 UNSAFE_LEAF(void, Unsafe_CopySwapMemory0(JNIEnv *env, jobject unsafe, jobject srcObj, jlong srcOffset, jobject dstObj, jlong dstOffset, jlong size, jlong elemSize)) { 536 size_t sz = (size_t)size; 537 size_t esz = (size_t)elemSize; 538 539 if (srcObj == NULL && dstObj == NULL) { 540 // Both src & dst are in native memory 541 address src = (address)srcOffset; 542 address dst = (address)dstOffset; 543 544 { 545 JavaThread* thread = JavaThread::thread_from_jni_environment(env); 546 GuardUnsafeAccess guard(thread); 547 Copy::conjoint_swap(src, dst, sz, esz); 548 } 549 } else { 550 // At least one of src/dst are on heap, transition to VM to access raw pointers 551 552 JVM_ENTRY_FROM_LEAF(env, void, Unsafe_CopySwapMemory0) { 553 oop srcp = JNIHandles::resolve(srcObj); 554 oop dstp = JNIHandles::resolve(dstObj); 555 556 address src = (address)index_oop_from_field_offset_long(srcp, srcOffset); 557 address dst = (address)index_oop_from_field_offset_long(dstp, dstOffset); 558 559 { 560 GuardUnsafeAccess guard(thread); 561 Copy::conjoint_swap(src, dst, sz, esz); 562 } 563 } JVM_END 564 } 565 } UNSAFE_END 566 567 UNSAFE_LEAF (void, Unsafe_WriteBack0(JNIEnv *env, jobject unsafe, jlong line)) { 568 assert(VM_Version::supports_data_cache_line_flush(), "should not get here"); 569 #ifdef ASSERT 570 if (TraceMemoryWriteback) { 571 tty->print_cr("Unsafe: writeback 0x%p", addr_from_java(line)); 572 } 573 #endif 574 575 MACOS_AARCH64_ONLY(ThreadWXEnable wx(WXExec, Thread::current())); 576 assert(StubRoutines::data_cache_writeback() != NULL, "sanity"); 577 (StubRoutines::DataCacheWriteback_stub())(addr_from_java(line)); 578 } UNSAFE_END 579 580 static void doWriteBackSync0(bool is_pre) 581 { 582 MACOS_AARCH64_ONLY(ThreadWXEnable wx(WXExec, Thread::current())); 583 assert(StubRoutines::data_cache_writeback_sync() != NULL, "sanity"); 584 (StubRoutines::DataCacheWritebackSync_stub())(is_pre); 585 } 586 587 UNSAFE_LEAF (void, Unsafe_WriteBackPreSync0(JNIEnv *env, jobject unsafe)) { 588 assert(VM_Version::supports_data_cache_line_flush(), "should not get here"); 589 #ifdef ASSERT 590 if (TraceMemoryWriteback) { 591 tty->print_cr("Unsafe: writeback pre-sync"); 592 } 593 #endif 594 595 doWriteBackSync0(true); 596 } UNSAFE_END 597 598 UNSAFE_LEAF (void, Unsafe_WriteBackPostSync0(JNIEnv *env, jobject unsafe)) { 599 assert(VM_Version::supports_data_cache_line_flush(), "should not get here"); 600 #ifdef ASSERT 601 if (TraceMemoryWriteback) { 602 tty->print_cr("Unsafe: writeback pre-sync"); 603 } 604 #endif 605 606 doWriteBackSync0(false); 607 } UNSAFE_END 608 609 ////// Random queries 610 611 static jlong find_field_offset(jclass clazz, jstring name, TRAPS) { 612 assert(clazz != NULL, "clazz must not be NULL"); 613 assert(name != NULL, "name must not be NULL"); 614 615 ResourceMark rm(THREAD); 616 char *utf_name = java_lang_String::as_utf8_string(JNIHandles::resolve_non_null(name)); 617 618 InstanceKlass* k = InstanceKlass::cast(java_lang_Class::as_Klass(JNIHandles::resolve_non_null(clazz))); 619 620 jint offset = -1; 621 for (JavaFieldStream fs(k); !fs.done(); fs.next()) { 622 Symbol *name = fs.name(); 623 if (name->equals(utf_name)) { 624 offset = fs.offset(); 625 break; 626 } 627 } 628 if (offset < 0) { 629 THROW_0(vmSymbols::java_lang_InternalError()); 630 } 631 return field_offset_from_byte_offset(offset); 632 } 633 634 static jlong find_field_offset(jobject field, int must_be_static, TRAPS) { 635 assert(field != NULL, "field must not be NULL"); 636 637 oop reflected = JNIHandles::resolve_non_null(field); 638 oop mirror = java_lang_reflect_Field::clazz(reflected); 639 Klass* k = java_lang_Class::as_Klass(mirror); 640 int slot = java_lang_reflect_Field::slot(reflected); 641 int modifiers = java_lang_reflect_Field::modifiers(reflected); 642 643 if (must_be_static >= 0) { 644 int really_is_static = ((modifiers & JVM_ACC_STATIC) != 0); 645 if (must_be_static != really_is_static) { 646 THROW_0(vmSymbols::java_lang_IllegalArgumentException()); 647 } 648 } 649 650 int offset = InstanceKlass::cast(k)->field_offset(slot); 651 return field_offset_from_byte_offset(offset); 652 } 653 654 UNSAFE_ENTRY(jlong, Unsafe_ObjectFieldOffset0(JNIEnv *env, jobject unsafe, jobject field)) { 655 return find_field_offset(field, 0, THREAD); 656 } UNSAFE_END 657 658 UNSAFE_ENTRY(jlong, Unsafe_ObjectFieldOffset1(JNIEnv *env, jobject unsafe, jclass c, jstring name)) { 659 return find_field_offset(c, name, THREAD); 660 } UNSAFE_END 661 662 UNSAFE_ENTRY(jlong, Unsafe_StaticFieldOffset0(JNIEnv *env, jobject unsafe, jobject field)) { 663 return find_field_offset(field, 1, THREAD); 664 } UNSAFE_END 665 666 UNSAFE_ENTRY(jobject, Unsafe_StaticFieldBase0(JNIEnv *env, jobject unsafe, jobject field)) { 667 assert(field != NULL, "field must not be NULL"); 668 669 // Note: In this VM implementation, a field address is always a short 670 // offset from the base of a a klass metaobject. Thus, the full dynamic 671 // range of the return type is never used. However, some implementations 672 // might put the static field inside an array shared by many classes, 673 // or even at a fixed address, in which case the address could be quite 674 // large. In that last case, this function would return NULL, since 675 // the address would operate alone, without any base pointer. 676 677 oop reflected = JNIHandles::resolve_non_null(field); 678 oop mirror = java_lang_reflect_Field::clazz(reflected); 679 int modifiers = java_lang_reflect_Field::modifiers(reflected); 680 681 if ((modifiers & JVM_ACC_STATIC) == 0) { 682 THROW_0(vmSymbols::java_lang_IllegalArgumentException()); 683 } 684 685 return JNIHandles::make_local(THREAD, mirror); 686 } UNSAFE_END 687 688 UNSAFE_ENTRY(void, Unsafe_EnsureClassInitialized0(JNIEnv *env, jobject unsafe, jobject clazz)) { 689 assert(clazz != NULL, "clazz must not be NULL"); 690 691 oop mirror = JNIHandles::resolve_non_null(clazz); 692 693 Klass* klass = java_lang_Class::as_Klass(mirror); 694 if (klass != NULL && klass->should_be_initialized()) { 695 InstanceKlass* k = InstanceKlass::cast(klass); 696 k->initialize(CHECK); 697 } 698 } 699 UNSAFE_END 700 701 UNSAFE_ENTRY(jboolean, Unsafe_ShouldBeInitialized0(JNIEnv *env, jobject unsafe, jobject clazz)) { 702 assert(clazz != NULL, "clazz must not be NULL"); 703 704 oop mirror = JNIHandles::resolve_non_null(clazz); 705 Klass* klass = java_lang_Class::as_Klass(mirror); 706 707 if (klass != NULL && klass->should_be_initialized()) { 708 return true; 709 } 710 711 return false; 712 } 713 UNSAFE_END 714 715 static void getBaseAndScale(int& base, int& scale, jclass clazz, TRAPS) { 716 assert(clazz != NULL, "clazz must not be NULL"); 717 718 oop mirror = JNIHandles::resolve_non_null(clazz); 719 Klass* k = java_lang_Class::as_Klass(mirror); 720 721 if (k == NULL || !k->is_array_klass()) { 722 THROW(vmSymbols::java_lang_InvalidClassException()); 723 } else if (k->is_objArray_klass()) { 724 base = arrayOopDesc::base_offset_in_bytes(T_OBJECT); 725 scale = heapOopSize; 726 } else if (k->is_typeArray_klass()) { 727 TypeArrayKlass* tak = TypeArrayKlass::cast(k); 728 base = tak->array_header_in_bytes(); 729 assert(base == arrayOopDesc::base_offset_in_bytes(tak->element_type()), "array_header_size semantics ok"); 730 scale = (1 << tak->log2_element_size()); 731 } else if (k->is_flatArray_klass()) { 732 FlatArrayKlass* vak = FlatArrayKlass::cast(k); 733 InlineKlass* vklass = vak->element_klass(); 734 base = vak->array_header_in_bytes(); 735 scale = vak->element_byte_size(); 736 } else { 737 ShouldNotReachHere(); 738 } 739 } 740 741 UNSAFE_ENTRY(jint, Unsafe_ArrayBaseOffset0(JNIEnv *env, jobject unsafe, jclass clazz)) { 742 int base = 0, scale = 0; 743 getBaseAndScale(base, scale, clazz, CHECK_0); 744 745 return field_offset_from_byte_offset(base); 746 } UNSAFE_END 747 748 749 UNSAFE_ENTRY(jint, Unsafe_ArrayIndexScale0(JNIEnv *env, jobject unsafe, jclass clazz)) { 750 int base = 0, scale = 0; 751 getBaseAndScale(base, scale, clazz, CHECK_0); 752 753 // This VM packs both fields and array elements down to the byte. 754 // But watch out: If this changes, so that array references for 755 // a given primitive type (say, T_BOOLEAN) use different memory units 756 // than fields, this method MUST return zero for such arrays. 757 // For example, the VM used to store sub-word sized fields in full 758 // words in the object layout, so that accessors like getByte(Object,int) 759 // did not really do what one might expect for arrays. Therefore, 760 // this function used to report a zero scale factor, so that the user 761 // would know not to attempt to access sub-word array elements. 762 // // Code for unpacked fields: 763 // if (scale < wordSize) return 0; 764 765 // The following allows for a pretty general fieldOffset cookie scheme, 766 // but requires it to be linear in byte offset. 767 return field_offset_from_byte_offset(scale) - field_offset_from_byte_offset(0); 768 } UNSAFE_END 769 770 771 UNSAFE_ENTRY(jlong, Unsafe_GetObjectSize0(JNIEnv* env, jobject o, jobject obj)) 772 oop p = JNIHandles::resolve(obj); 773 return p->size() * HeapWordSize; 774 UNSAFE_END 775 776 777 static inline void throw_new(JNIEnv *env, const char *ename) { 778 jclass cls = env->FindClass(ename); 779 if (env->ExceptionCheck()) { 780 env->ExceptionClear(); 781 tty->print_cr("Unsafe: cannot throw %s because FindClass has failed", ename); 782 return; 783 } 784 785 env->ThrowNew(cls, NULL); 786 } 787 788 static jclass Unsafe_DefineClass_impl(JNIEnv *env, jstring name, jbyteArray data, int offset, int length, jobject loader, jobject pd) { 789 // Code lifted from JDK 1.3 ClassLoader.c 790 791 jbyte *body; 792 char *utfName = NULL; 793 jclass result = 0; 794 char buf[128]; 795 796 assert(data != NULL, "Class bytes must not be NULL"); 797 assert(length >= 0, "length must not be negative: %d", length); 798 799 if (UsePerfData) { 800 ClassLoader::unsafe_defineClassCallCounter()->inc(); 801 } 802 803 body = NEW_C_HEAP_ARRAY_RETURN_NULL(jbyte, length, mtInternal); 804 if (body == NULL) { 805 throw_new(env, "java/lang/OutOfMemoryError"); 806 return 0; 807 } 808 809 env->GetByteArrayRegion(data, offset, length, body); 810 if (env->ExceptionOccurred()) { 811 goto free_body; 812 } 813 814 if (name != NULL) { 815 uint len = env->GetStringUTFLength(name); 816 int unicode_len = env->GetStringLength(name); 817 818 if (len >= sizeof(buf)) { 819 utfName = NEW_C_HEAP_ARRAY_RETURN_NULL(char, len + 1, mtInternal); 820 if (utfName == NULL) { 821 throw_new(env, "java/lang/OutOfMemoryError"); 822 goto free_body; 823 } 824 } else { 825 utfName = buf; 826 } 827 828 env->GetStringUTFRegion(name, 0, unicode_len, utfName); 829 830 for (uint i = 0; i < len; i++) { 831 if (utfName[i] == '.') utfName[i] = '/'; 832 } 833 } 834 835 result = JVM_DefineClass(env, utfName, loader, body, length, pd); 836 837 if (utfName && utfName != buf) { 838 FREE_C_HEAP_ARRAY(char, utfName); 839 } 840 841 free_body: 842 FREE_C_HEAP_ARRAY(jbyte, body); 843 return result; 844 } 845 846 847 UNSAFE_ENTRY(jclass, Unsafe_DefineClass0(JNIEnv *env, jobject unsafe, jstring name, jbyteArray data, int offset, int length, jobject loader, jobject pd)) { 848 ThreadToNativeFromVM ttnfv(thread); 849 850 return Unsafe_DefineClass_impl(env, name, data, offset, length, loader, pd); 851 } UNSAFE_END 852 853 854 UNSAFE_ENTRY(void, Unsafe_ThrowException(JNIEnv *env, jobject unsafe, jthrowable thr)) { 855 ThreadToNativeFromVM ttnfv(thread); 856 env->Throw(thr); 857 } UNSAFE_END 858 859 // JSR166 ------------------------------------------------------------------ 860 861 UNSAFE_ENTRY(jobject, Unsafe_CompareAndExchangeReference(JNIEnv *env, jobject unsafe, jobject obj, jlong offset, jobject e_h, jobject x_h)) { 862 oop x = JNIHandles::resolve(x_h); 863 oop e = JNIHandles::resolve(e_h); 864 oop p = JNIHandles::resolve(obj); 865 assert_field_offset_sane(p, offset); 866 oop res = HeapAccess<ON_UNKNOWN_OOP_REF>::oop_atomic_cmpxchg_at(p, (ptrdiff_t)offset, e, x); 867 return JNIHandles::make_local(THREAD, res); 868 } UNSAFE_END 869 870 UNSAFE_ENTRY(jint, Unsafe_CompareAndExchangeInt(JNIEnv *env, jobject unsafe, jobject obj, jlong offset, jint e, jint x)) { 871 oop p = JNIHandles::resolve(obj); 872 volatile jint* addr = (volatile jint*)index_oop_from_field_offset_long(p, offset); 873 return Atomic::cmpxchg(addr, e, x); 874 } UNSAFE_END 875 876 UNSAFE_ENTRY(jlong, Unsafe_CompareAndExchangeLong(JNIEnv *env, jobject unsafe, jobject obj, jlong offset, jlong e, jlong x)) { 877 oop p = JNIHandles::resolve(obj); 878 volatile jlong* addr = (volatile jlong*)index_oop_from_field_offset_long(p, offset); 879 return Atomic::cmpxchg(addr, e, x); 880 } UNSAFE_END 881 882 UNSAFE_ENTRY(jboolean, Unsafe_CompareAndSetReference(JNIEnv *env, jobject unsafe, jobject obj, jlong offset, jobject e_h, jobject x_h)) { 883 oop x = JNIHandles::resolve(x_h); 884 oop e = JNIHandles::resolve(e_h); 885 oop p = JNIHandles::resolve(obj); 886 assert_field_offset_sane(p, offset); 887 oop ret = HeapAccess<ON_UNKNOWN_OOP_REF>::oop_atomic_cmpxchg_at(p, (ptrdiff_t)offset, e, x); 888 return ret == e; 889 } UNSAFE_END 890 891 UNSAFE_ENTRY(jboolean, Unsafe_CompareAndSetInt(JNIEnv *env, jobject unsafe, jobject obj, jlong offset, jint e, jint x)) { 892 oop p = JNIHandles::resolve(obj); 893 volatile jint* addr = (volatile jint*)index_oop_from_field_offset_long(p, offset); 894 return Atomic::cmpxchg(addr, e, x) == e; 895 } UNSAFE_END 896 897 UNSAFE_ENTRY(jboolean, Unsafe_CompareAndSetLong(JNIEnv *env, jobject unsafe, jobject obj, jlong offset, jlong e, jlong x)) { 898 oop p = JNIHandles::resolve(obj); 899 volatile jlong* addr = (volatile jlong*)index_oop_from_field_offset_long(p, offset); 900 return Atomic::cmpxchg(addr, e, x) == e; 901 } UNSAFE_END 902 903 static void post_thread_park_event(EventThreadPark* event, const oop obj, jlong timeout_nanos, jlong until_epoch_millis) { 904 assert(event != NULL, "invariant"); 905 assert(event->should_commit(), "invariant"); 906 event->set_parkedClass((obj != NULL) ? obj->klass() : NULL); 907 event->set_timeout(timeout_nanos); 908 event->set_until(until_epoch_millis); 909 event->set_address((obj != NULL) ? (u8)cast_from_oop<uintptr_t>(obj) : 0); 910 event->commit(); 911 } 912 913 UNSAFE_ENTRY(void, Unsafe_Park(JNIEnv *env, jobject unsafe, jboolean isAbsolute, jlong time)) { 914 HOTSPOT_THREAD_PARK_BEGIN((uintptr_t) thread->parker(), (int) isAbsolute, time); 915 EventThreadPark event; 916 917 JavaThreadParkedState jtps(thread, time != 0); 918 thread->parker()->park(isAbsolute != 0, time); 919 if (event.should_commit()) { 920 const oop obj = thread->current_park_blocker(); 921 if (time == 0) { 922 post_thread_park_event(&event, obj, min_jlong, min_jlong); 923 } else { 924 if (isAbsolute != 0) { 925 post_thread_park_event(&event, obj, min_jlong, time); 926 } else { 927 post_thread_park_event(&event, obj, time, min_jlong); 928 } 929 } 930 } 931 HOTSPOT_THREAD_PARK_END((uintptr_t) thread->parker()); 932 } UNSAFE_END 933 934 UNSAFE_ENTRY(void, Unsafe_Unpark(JNIEnv *env, jobject unsafe, jobject jthread)) { 935 if (jthread != NULL) { 936 ThreadsListHandle tlh; 937 JavaThread* thr = NULL; 938 oop java_thread = NULL; 939 (void) tlh.cv_internal_thread_to_JavaThread(jthread, &thr, &java_thread); 940 if (java_thread != NULL) { 941 // This is a valid oop. 942 if (thr != NULL) { 943 // The JavaThread is alive. 944 Parker* p = thr->parker(); 945 HOTSPOT_THREAD_UNPARK((uintptr_t) p); 946 p->unpark(); 947 } 948 } 949 } // ThreadsListHandle is destroyed here. 950 951 } UNSAFE_END 952 953 UNSAFE_ENTRY(jint, Unsafe_GetLoadAverage0(JNIEnv *env, jobject unsafe, jdoubleArray loadavg, jint nelem)) { 954 const int max_nelem = 3; 955 double la[max_nelem]; 956 jint ret; 957 958 typeArrayOop a = typeArrayOop(JNIHandles::resolve_non_null(loadavg)); 959 assert(a->is_typeArray(), "must be type array"); 960 961 ret = os::loadavg(la, nelem); 962 if (ret == -1) { 963 return -1; 964 } 965 966 // if successful, ret is the number of samples actually retrieved. 967 assert(ret >= 0 && ret <= max_nelem, "Unexpected loadavg return value"); 968 switch(ret) { 969 case 3: a->double_at_put(2, (jdouble)la[2]); // fall through 970 case 2: a->double_at_put(1, (jdouble)la[1]); // fall through 971 case 1: a->double_at_put(0, (jdouble)la[0]); break; 972 } 973 974 return ret; 975 } UNSAFE_END 976 977 978 /// JVM_RegisterUnsafeMethods 979 980 #define ADR "J" 981 982 #define LANG "Ljava/lang/" 983 984 #define OBJ LANG "Object;" 985 #define CLS LANG "Class;" 986 #define FLD LANG "reflect/Field;" 987 #define THR LANG "Throwable;" 988 989 #define DC_Args LANG "String;[BII" LANG "ClassLoader;" "Ljava/security/ProtectionDomain;" 990 #define DAC_Args CLS "[B[" OBJ 991 992 #define CC (char*) /*cast a literal from (const char*)*/ 993 #define FN_PTR(f) CAST_FROM_FN_PTR(void*, &f) 994 995 #define DECLARE_GETPUTOOP(Type, Desc) \ 996 {CC "get" #Type, CC "(" OBJ "J)" #Desc, FN_PTR(Unsafe_Get##Type)}, \ 997 {CC "put" #Type, CC "(" OBJ "J" #Desc ")V", FN_PTR(Unsafe_Put##Type)}, \ 998 {CC "get" #Type "Volatile", CC "(" OBJ "J)" #Desc, FN_PTR(Unsafe_Get##Type##Volatile)}, \ 999 {CC "put" #Type "Volatile", CC "(" OBJ "J" #Desc ")V", FN_PTR(Unsafe_Put##Type##Volatile)} 1000 1001 1002 static JNINativeMethod jdk_internal_misc_Unsafe_methods[] = { 1003 {CC "getReference", CC "(" OBJ "J)" OBJ "", FN_PTR(Unsafe_GetReference)}, 1004 {CC "putReference", CC "(" OBJ "J" OBJ ")V", FN_PTR(Unsafe_PutReference)}, 1005 {CC "getReferenceVolatile", CC "(" OBJ "J)" OBJ, FN_PTR(Unsafe_GetReferenceVolatile)}, 1006 {CC "putReferenceVolatile", CC "(" OBJ "J" OBJ ")V", FN_PTR(Unsafe_PutReferenceVolatile)}, 1007 1008 {CC "isFlattenedArray", CC "(" CLS ")Z", FN_PTR(Unsafe_IsFlattenedArray)}, 1009 {CC "isFlattenedField0", CC "(" OBJ ")Z", FN_PTR(Unsafe_IsFlattenedField)}, 1010 {CC "getValue", CC "(" OBJ "J" CLS ")" OBJ, FN_PTR(Unsafe_GetValue)}, 1011 {CC "putValue", CC "(" OBJ "J" CLS OBJ ")V", FN_PTR(Unsafe_PutValue)}, 1012 {CC "uninitializedDefaultValue", CC "(" CLS ")" OBJ, FN_PTR(Unsafe_UninitializedDefaultValue)}, 1013 {CC "makePrivateBuffer", CC "(" OBJ ")" OBJ, FN_PTR(Unsafe_MakePrivateBuffer)}, 1014 {CC "finishPrivateBuffer", CC "(" OBJ ")" OBJ, FN_PTR(Unsafe_FinishPrivateBuffer)}, 1015 {CC "valueHeaderSize", CC "(" CLS ")J", FN_PTR(Unsafe_ValueHeaderSize)}, 1016 1017 {CC "getUncompressedObject", CC "(" ADR ")" OBJ, FN_PTR(Unsafe_GetUncompressedObject)}, 1018 1019 DECLARE_GETPUTOOP(Boolean, Z), 1020 DECLARE_GETPUTOOP(Byte, B), 1021 DECLARE_GETPUTOOP(Short, S), 1022 DECLARE_GETPUTOOP(Char, C), 1023 DECLARE_GETPUTOOP(Int, I), 1024 DECLARE_GETPUTOOP(Long, J), 1025 DECLARE_GETPUTOOP(Float, F), 1026 DECLARE_GETPUTOOP(Double, D), 1027 1028 {CC "allocateMemory0", CC "(J)" ADR, FN_PTR(Unsafe_AllocateMemory0)}, 1029 {CC "reallocateMemory0", CC "(" ADR "J)" ADR, FN_PTR(Unsafe_ReallocateMemory0)}, 1030 {CC "freeMemory0", CC "(" ADR ")V", FN_PTR(Unsafe_FreeMemory0)}, 1031 1032 {CC "objectFieldOffset0", CC "(" FLD ")J", FN_PTR(Unsafe_ObjectFieldOffset0)}, 1033 {CC "objectFieldOffset1", CC "(" CLS LANG "String;)J", FN_PTR(Unsafe_ObjectFieldOffset1)}, 1034 {CC "staticFieldOffset0", CC "(" FLD ")J", FN_PTR(Unsafe_StaticFieldOffset0)}, 1035 {CC "staticFieldBase0", CC "(" FLD ")" OBJ, FN_PTR(Unsafe_StaticFieldBase0)}, 1036 {CC "ensureClassInitialized0", CC "(" CLS ")V", FN_PTR(Unsafe_EnsureClassInitialized0)}, 1037 {CC "arrayBaseOffset0", CC "(" CLS ")I", FN_PTR(Unsafe_ArrayBaseOffset0)}, 1038 {CC "arrayIndexScale0", CC "(" CLS ")I", FN_PTR(Unsafe_ArrayIndexScale0)}, 1039 {CC "getObjectSize0", CC "(Ljava/lang/Object;)J", FN_PTR(Unsafe_GetObjectSize0)}, 1040 1041 {CC "defineClass0", CC "(" DC_Args ")" CLS, FN_PTR(Unsafe_DefineClass0)}, 1042 {CC "allocateInstance", CC "(" CLS ")" OBJ, FN_PTR(Unsafe_AllocateInstance)}, 1043 {CC "throwException", CC "(" THR ")V", FN_PTR(Unsafe_ThrowException)}, 1044 {CC "compareAndSetReference",CC "(" OBJ "J" OBJ "" OBJ ")Z", FN_PTR(Unsafe_CompareAndSetReference)}, 1045 {CC "compareAndSetInt", CC "(" OBJ "J""I""I"")Z", FN_PTR(Unsafe_CompareAndSetInt)}, 1046 {CC "compareAndSetLong", CC "(" OBJ "J""J""J"")Z", FN_PTR(Unsafe_CompareAndSetLong)}, 1047 {CC "compareAndExchangeReference", CC "(" OBJ "J" OBJ "" OBJ ")" OBJ, FN_PTR(Unsafe_CompareAndExchangeReference)}, 1048 {CC "compareAndExchangeInt", CC "(" OBJ "J""I""I"")I", FN_PTR(Unsafe_CompareAndExchangeInt)}, 1049 {CC "compareAndExchangeLong", CC "(" OBJ "J""J""J"")J", FN_PTR(Unsafe_CompareAndExchangeLong)}, 1050 1051 {CC "park", CC "(ZJ)V", FN_PTR(Unsafe_Park)}, 1052 {CC "unpark", CC "(" OBJ ")V", FN_PTR(Unsafe_Unpark)}, 1053 1054 {CC "getLoadAverage0", CC "([DI)I", FN_PTR(Unsafe_GetLoadAverage0)}, 1055 1056 {CC "copyMemory0", CC "(" OBJ "J" OBJ "JJ)V", FN_PTR(Unsafe_CopyMemory0)}, 1057 {CC "copySwapMemory0", CC "(" OBJ "J" OBJ "JJJ)V", FN_PTR(Unsafe_CopySwapMemory0)}, 1058 {CC "writeback0", CC "(" "J" ")V", FN_PTR(Unsafe_WriteBack0)}, 1059 {CC "writebackPreSync0", CC "()V", FN_PTR(Unsafe_WriteBackPreSync0)}, 1060 {CC "writebackPostSync0", CC "()V", FN_PTR(Unsafe_WriteBackPostSync0)}, 1061 {CC "setMemory0", CC "(" OBJ "JJB)V", FN_PTR(Unsafe_SetMemory0)}, 1062 1063 {CC "shouldBeInitialized0", CC "(" CLS ")Z", FN_PTR(Unsafe_ShouldBeInitialized0)}, 1064 1065 {CC "fullFence", CC "()V", FN_PTR(Unsafe_FullFence)}, 1066 }; 1067 1068 #undef CC 1069 #undef FN_PTR 1070 1071 #undef ADR 1072 #undef LANG 1073 #undef OBJ 1074 #undef CLS 1075 #undef FLD 1076 #undef THR 1077 #undef DC_Args 1078 #undef DAC_Args 1079 1080 #undef DECLARE_GETPUTOOP 1081 1082 1083 // This function is exported, used by NativeLookup. 1084 // The Unsafe_xxx functions above are called only from the interpreter. 1085 // The optimizer looks at names and signatures to recognize 1086 // individual functions. 1087 1088 JVM_ENTRY(void, JVM_RegisterJDKInternalMiscUnsafeMethods(JNIEnv *env, jclass unsafeclass)) { 1089 ThreadToNativeFromVM ttnfv(thread); 1090 1091 int ok = env->RegisterNatives(unsafeclass, jdk_internal_misc_Unsafe_methods, sizeof(jdk_internal_misc_Unsafe_methods)/sizeof(JNINativeMethod)); 1092 guarantee(ok == 0, "register jdk.internal.misc.Unsafe natives"); 1093 } JVM_END