1 /* 2 * Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 * 23 */ 24 25 #include "precompiled.hpp" 26 #include "classfile/vmSymbols.hpp" 27 #include "utilities/macros.hpp" 28 #if INCLUDE_ALL_GCS 29 #include "gc_implementation/g1/g1SATBCardTableModRefBS.hpp" 30 #include "gc_implementation/shenandoah/shenandoahBarrierSet.inline.hpp" 31 #endif // INCLUDE_ALL_GCS 32 #include "jfr/jfrEvents.hpp" 33 #include "memory/allocation.inline.hpp" 34 #include "prims/jni.h" 35 #include "prims/jvm.h" 36 #include "runtime/globals.hpp" 37 #include "runtime/interfaceSupport.hpp" 38 #include "runtime/prefetch.inline.hpp" 39 #include "runtime/orderAccess.inline.hpp" 40 #include "runtime/reflection.hpp" 41 #include "runtime/synchronizer.hpp" 42 #include "services/threadService.hpp" 43 #include "utilities/copy.hpp" 44 #include "utilities/dtrace.hpp" 45 46 PRAGMA_FORMAT_MUTE_WARNINGS_FOR_GCC 47 48 /* 49 * Implementation of class sun.misc.Unsafe 50 */ 51 52 #ifndef USDT2 53 HS_DTRACE_PROBE_DECL3(hotspot, thread__park__begin, uintptr_t, int, long long); 54 HS_DTRACE_PROBE_DECL1(hotspot, thread__park__end, uintptr_t); 55 HS_DTRACE_PROBE_DECL1(hotspot, thread__unpark, uintptr_t); 56 #endif /* !USDT2 */ 57 58 #define MAX_OBJECT_SIZE \ 59 ( arrayOopDesc::header_size(T_DOUBLE) * HeapWordSize \ 60 + ((julong)max_jint * sizeof(double)) ) 61 62 63 #define UNSAFE_ENTRY(result_type, header) \ 64 JVM_ENTRY(result_type, header) 65 66 // Can't use UNSAFE_LEAF because it has the signature of a straight 67 // call into the runtime (just like JVM_LEAF, funny that) but it's 68 // called like a Java Native and thus the wrapper built for it passes 69 // arguments like a JNI call. It expects those arguments to be popped 70 // from the stack on Intel like all good JNI args are, and adjusts the 71 // stack according. Since the JVM_LEAF call expects no extra 72 // arguments the stack isn't popped in the C code, is pushed by the 73 // wrapper and we get sick. 74 //#define UNSAFE_LEAF(result_type, header) \ 75 // JVM_LEAF(result_type, header) 76 77 #define UNSAFE_END JVM_END 78 79 #define UnsafeWrapper(arg) /*nothing, for the present*/ 80 81 82 inline void* addr_from_java(jlong addr) { 83 // This assert fails in a variety of ways on 32-bit systems. 84 // It is impossible to predict whether native code that converts 85 // pointers to longs will sign-extend or zero-extend the addresses. 86 //assert(addr == (uintptr_t)addr, "must not be odd high bits"); 87 return (void*)(uintptr_t)addr; 88 } 89 90 inline jlong addr_to_java(void* p) { 91 assert(p == (void*)(uintptr_t)p, "must not be odd high bits"); 92 return (uintptr_t)p; 93 } 94 95 96 // Note: The VM's obj_field and related accessors use byte-scaled 97 // ("unscaled") offsets, just as the unsafe methods do. 98 99 // However, the method Unsafe.fieldOffset explicitly declines to 100 // guarantee this. The field offset values manipulated by the Java user 101 // through the Unsafe API are opaque cookies that just happen to be byte 102 // offsets. We represent this state of affairs by passing the cookies 103 // through conversion functions when going between the VM and the Unsafe API. 104 // The conversion functions just happen to be no-ops at present. 105 106 inline jlong field_offset_to_byte_offset(jlong field_offset) { 107 return field_offset; 108 } 109 110 inline jlong field_offset_from_byte_offset(jlong byte_offset) { 111 return byte_offset; 112 } 113 114 inline jint invocation_key_from_method_slot(jint slot) { 115 return slot; 116 } 117 118 inline jint invocation_key_to_method_slot(jint key) { 119 return key; 120 } 121 122 inline void* index_oop_from_field_offset_long(oop p, jlong field_offset) { 123 jlong byte_offset = field_offset_to_byte_offset(field_offset); 124 #ifdef ASSERT 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 = (address)p + byte_offset; 129 assert((void*)p->obj_field_addr<oop>((jint)byte_offset) == ptr_plus_disp, 130 "raw [ptr+disp] must be consistent with oop::field_base"); 131 } 132 jlong p_size = HeapWordSize * (jlong)(p->size()); 133 assert(byte_offset < p_size, err_msg("Unsafe access: offset " INT64_FORMAT " > object's size " INT64_FORMAT, byte_offset, p_size)); 134 } 135 #endif 136 if (sizeof(char*) == sizeof(jint)) // (this constant folds!) 137 return (address)p + (jint) byte_offset; 138 else 139 return (address)p + byte_offset; 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 jint Unsafe_invocation_key_from_method_slot(jint slot) { 151 return invocation_key_from_method_slot(slot); 152 } 153 jint Unsafe_invocation_key_to_method_slot(jint key) { 154 return invocation_key_to_method_slot(key); 155 } 156 157 158 ///// Data in the Java heap. 159 160 #define truncate_jboolean(x) ((x) & 1) 161 #define truncate_jbyte(x) (x) 162 #define truncate_jshort(x) (x) 163 #define truncate_jchar(x) (x) 164 #define truncate_jint(x) (x) 165 #define truncate_jlong(x) (x) 166 #define truncate_jfloat(x) (x) 167 #define truncate_jdouble(x) (x) 168 169 #define GET_FIELD(obj, offset, type_name, v) \ 170 oop p = JNIHandles::resolve(obj); \ 171 type_name v = *(type_name*)index_oop_from_field_offset_long(p, offset) 172 173 #define SET_FIELD(obj, offset, type_name, x) \ 174 oop p = JNIHandles::resolve(obj); \ 175 *(type_name*)index_oop_from_field_offset_long(p, offset) = truncate_##type_name(x) 176 177 #define GET_FIELD_VOLATILE(obj, offset, type_name, v) \ 178 oop p = JNIHandles::resolve(obj); \ 179 if (support_IRIW_for_not_multiple_copy_atomic_cpu) { \ 180 OrderAccess::fence(); \ 181 } \ 182 volatile type_name v = OrderAccess::load_acquire((volatile type_name*)index_oop_from_field_offset_long(p, offset)); 183 184 #define SET_FIELD_VOLATILE(obj, offset, type_name, x) \ 185 oop p = JNIHandles::resolve(obj); \ 186 OrderAccess::release_store_fence((volatile type_name*)index_oop_from_field_offset_long(p, offset), truncate_##type_name(x)); 187 188 // Macros for oops that check UseCompressedOops 189 190 #define GET_OOP_FIELD(obj, offset, v) \ 191 oop p = JNIHandles::resolve(obj); \ 192 oop v; \ 193 if (UseCompressedOops) { \ 194 narrowOop n = *(narrowOop*)index_oop_from_field_offset_long(p, offset); \ 195 v = oopDesc::decode_heap_oop(n); \ 196 } else { \ 197 v = *(oop*)index_oop_from_field_offset_long(p, offset); \ 198 } 199 200 // Get/SetObject must be special-cased, since it works with handles. 201 202 // We could be accessing the referent field in a reference 203 // object. If G1 is enabled then we need to register non-null 204 // referent with the SATB barrier. 205 206 #if INCLUDE_ALL_GCS 207 static bool is_java_lang_ref_Reference_access(oop o, jlong offset) { 208 if (offset == java_lang_ref_Reference::referent_offset && o != NULL) { 209 Klass* k = o->klass(); 210 if (InstanceKlass::cast(k)->reference_type() != REF_NONE) { 211 assert(InstanceKlass::cast(k)->is_subclass_of(SystemDictionary::Reference_klass()), "sanity"); 212 return true; 213 } 214 } 215 return false; 216 } 217 #endif 218 219 static void ensure_satb_referent_alive(oop o, jlong offset, oop v) { 220 #if INCLUDE_ALL_GCS 221 if ((UseG1GC || (UseShenandoahGC && ShenandoahSATBBarrier)) && v != NULL && is_java_lang_ref_Reference_access(o, offset)) { 222 G1SATBCardTableModRefBS::enqueue(v); 223 } 224 #endif 225 } 226 227 // The xxx140 variants for backward compatibility do not allow a full-width offset. 228 UNSAFE_ENTRY(jobject, Unsafe_GetObject140(JNIEnv *env, jobject unsafe, jobject obj, jint offset)) 229 UnsafeWrapper("Unsafe_GetObject"); 230 if (obj == NULL) THROW_0(vmSymbols::java_lang_NullPointerException()); 231 GET_OOP_FIELD(obj, offset, v) 232 233 #if INCLUDE_ALL_GCS 234 if (UseShenandoahGC) { 235 v = ShenandoahBarrierSet::barrier_set()->load_reference_barrier(v); 236 } 237 #endif 238 239 ensure_satb_referent_alive(p, offset, v); 240 241 return JNIHandles::make_local(env, v); 242 UNSAFE_END 243 244 UNSAFE_ENTRY(void, Unsafe_SetObject140(JNIEnv *env, jobject unsafe, jobject obj, jint offset, jobject x_h)) 245 UnsafeWrapper("Unsafe_SetObject"); 246 if (obj == NULL) THROW(vmSymbols::java_lang_NullPointerException()); 247 oop x = JNIHandles::resolve(x_h); 248 //SET_FIELD(obj, offset, oop, x); 249 oop p = JNIHandles::resolve(obj); 250 if (UseCompressedOops) { 251 if (x != NULL) { 252 // If there is a heap base pointer, we are obliged to emit a store barrier. 253 oop_store((narrowOop*)index_oop_from_field_offset_long(p, offset), x); 254 } else { 255 narrowOop n = oopDesc::encode_heap_oop_not_null(x); 256 *(narrowOop*)index_oop_from_field_offset_long(p, offset) = n; 257 } 258 } else { 259 if (x != NULL) { 260 // If there is a heap base pointer, we are obliged to emit a store barrier. 261 oop_store((oop*)index_oop_from_field_offset_long(p, offset), x); 262 } else { 263 *(oop*)index_oop_from_field_offset_long(p, offset) = x; 264 } 265 } 266 UNSAFE_END 267 268 // The normal variants allow a null base pointer with an arbitrary address. 269 // But if the base pointer is non-null, the offset should make some sense. 270 // That is, it should be in the range [0, MAX_OBJECT_SIZE]. 271 UNSAFE_ENTRY(jobject, Unsafe_GetObject(JNIEnv *env, jobject unsafe, jobject obj, jlong offset)) 272 UnsafeWrapper("Unsafe_GetObject"); 273 GET_OOP_FIELD(obj, offset, v) 274 275 #if INCLUDE_ALL_GCS 276 if (UseShenandoahGC) { 277 v = ShenandoahBarrierSet::barrier_set()->load_reference_barrier(v); 278 } 279 #endif 280 281 ensure_satb_referent_alive(p, offset, v); 282 283 return JNIHandles::make_local(env, v); 284 UNSAFE_END 285 286 UNSAFE_ENTRY(void, Unsafe_SetObject(JNIEnv *env, jobject unsafe, jobject obj, jlong offset, jobject x_h)) 287 UnsafeWrapper("Unsafe_SetObject"); 288 oop x = JNIHandles::resolve(x_h); 289 oop p = JNIHandles::resolve(obj); 290 if (UseCompressedOops) { 291 oop_store((narrowOop*)index_oop_from_field_offset_long(p, offset), x); 292 } else { 293 oop_store((oop*)index_oop_from_field_offset_long(p, offset), x); 294 } 295 UNSAFE_END 296 297 UNSAFE_ENTRY(jobject, Unsafe_GetObjectVolatile(JNIEnv *env, jobject unsafe, jobject obj, jlong offset)) 298 UnsafeWrapper("Unsafe_GetObjectVolatile"); 299 oop p = JNIHandles::resolve(obj); 300 void* addr = index_oop_from_field_offset_long(p, offset); 301 volatile oop v; 302 if (UseCompressedOops) { 303 volatile narrowOop n = *(volatile narrowOop*) addr; 304 (void)const_cast<oop&>(v = oopDesc::decode_heap_oop(n)); 305 } else { 306 (void)const_cast<oop&>(v = *(volatile oop*) addr); 307 } 308 309 #if INCLUDE_ALL_GCS 310 if (UseShenandoahGC) { 311 (void)const_cast<oop&>(v = ShenandoahBarrierSet::barrier_set()->load_reference_barrier(v)); 312 } 313 #endif 314 315 ensure_satb_referent_alive(p, offset, v); 316 317 OrderAccess::acquire(); 318 return JNIHandles::make_local(env, v); 319 UNSAFE_END 320 321 UNSAFE_ENTRY(void, Unsafe_SetObjectVolatile(JNIEnv *env, jobject unsafe, jobject obj, jlong offset, jobject x_h)) 322 UnsafeWrapper("Unsafe_SetObjectVolatile"); 323 oop x = JNIHandles::resolve(x_h); 324 oop p = JNIHandles::resolve(obj); 325 void* addr = index_oop_from_field_offset_long(p, offset); 326 OrderAccess::release(); 327 if (UseCompressedOops) { 328 oop_store((narrowOop*)addr, x); 329 } else { 330 oop_store((oop*)addr, x); 331 } 332 OrderAccess::fence(); 333 UNSAFE_END 334 335 #ifndef SUPPORTS_NATIVE_CX8 336 337 // VM_Version::supports_cx8() is a surrogate for 'supports atomic long memory ops'. 338 // 339 // On platforms which do not support atomic compare-and-swap of jlong (8 byte) 340 // values we have to use a lock-based scheme to enforce atomicity. This has to be 341 // applied to all Unsafe operations that set the value of a jlong field. Even so 342 // the compareAndSwapLong operation will not be atomic with respect to direct stores 343 // to the field from Java code. It is important therefore that any Java code that 344 // utilizes these Unsafe jlong operations does not perform direct stores. To permit 345 // direct loads of the field from Java code we must also use Atomic::store within the 346 // locked regions. And for good measure, in case there are direct stores, we also 347 // employ Atomic::load within those regions. Note that the field in question must be 348 // volatile and so must have atomic load/store accesses applied at the Java level. 349 // 350 // The locking scheme could utilize a range of strategies for controlling the locking 351 // granularity: from a lock per-field through to a single global lock. The latter is 352 // the simplest and is used for the current implementation. Note that the Java object 353 // that contains the field, can not, in general, be used for locking. To do so can lead 354 // to deadlocks as we may introduce locking into what appears to the Java code to be a 355 // lock-free path. 356 // 357 // As all the locked-regions are very short and themselves non-blocking we can treat 358 // them as leaf routines and elide safepoint checks (ie we don't perform any thread 359 // state transitions even when blocking for the lock). Note that if we do choose to 360 // add safepoint checks and thread state transitions, we must ensure that we calculate 361 // the address of the field _after_ we have acquired the lock, else the object may have 362 // been moved by the GC 363 364 UNSAFE_ENTRY(jlong, Unsafe_GetLongVolatile(JNIEnv *env, jobject unsafe, jobject obj, jlong offset)) 365 UnsafeWrapper("Unsafe_GetLongVolatile"); 366 { 367 if (VM_Version::supports_cx8()) { 368 GET_FIELD_VOLATILE(obj, offset, jlong, v); 369 return v; 370 } 371 else { 372 Handle p (THREAD, JNIHandles::resolve(obj)); 373 jlong* addr = (jlong*)(index_oop_from_field_offset_long(p(), offset)); 374 MutexLockerEx mu(UnsafeJlong_lock, Mutex::_no_safepoint_check_flag); 375 jlong value = Atomic::load(addr); 376 return value; 377 } 378 } 379 UNSAFE_END 380 381 UNSAFE_ENTRY(void, Unsafe_SetLongVolatile(JNIEnv *env, jobject unsafe, jobject obj, jlong offset, jlong x)) 382 UnsafeWrapper("Unsafe_SetLongVolatile"); 383 { 384 if (VM_Version::supports_cx8()) { 385 SET_FIELD_VOLATILE(obj, offset, jlong, x); 386 } 387 else { 388 Handle p (THREAD, JNIHandles::resolve(obj)); 389 jlong* addr = (jlong*)(index_oop_from_field_offset_long(p(), offset)); 390 MutexLockerEx mu(UnsafeJlong_lock, Mutex::_no_safepoint_check_flag); 391 Atomic::store(x, addr); 392 } 393 } 394 UNSAFE_END 395 396 #endif // not SUPPORTS_NATIVE_CX8 397 398 #define DEFINE_GETSETOOP(jboolean, Boolean) \ 399 \ 400 UNSAFE_ENTRY(jboolean, Unsafe_Get##Boolean##140(JNIEnv *env, jobject unsafe, jobject obj, jint offset)) \ 401 UnsafeWrapper("Unsafe_Get"#Boolean); \ 402 if (obj == NULL) THROW_0(vmSymbols::java_lang_NullPointerException()); \ 403 GET_FIELD(obj, offset, jboolean, v); \ 404 return v; \ 405 UNSAFE_END \ 406 \ 407 UNSAFE_ENTRY(void, Unsafe_Set##Boolean##140(JNIEnv *env, jobject unsafe, jobject obj, jint offset, jboolean x)) \ 408 UnsafeWrapper("Unsafe_Set"#Boolean); \ 409 if (obj == NULL) THROW(vmSymbols::java_lang_NullPointerException()); \ 410 SET_FIELD(obj, offset, jboolean, x); \ 411 UNSAFE_END \ 412 \ 413 UNSAFE_ENTRY(jboolean, Unsafe_Get##Boolean(JNIEnv *env, jobject unsafe, jobject obj, jlong offset)) \ 414 UnsafeWrapper("Unsafe_Get"#Boolean); \ 415 GET_FIELD(obj, offset, jboolean, v); \ 416 return v; \ 417 UNSAFE_END \ 418 \ 419 UNSAFE_ENTRY(void, Unsafe_Set##Boolean(JNIEnv *env, jobject unsafe, jobject obj, jlong offset, jboolean x)) \ 420 UnsafeWrapper("Unsafe_Set"#Boolean); \ 421 SET_FIELD(obj, offset, jboolean, x); \ 422 UNSAFE_END \ 423 \ 424 // END DEFINE_GETSETOOP. 425 426 DEFINE_GETSETOOP(jboolean, Boolean) 427 DEFINE_GETSETOOP(jbyte, Byte) 428 DEFINE_GETSETOOP(jshort, Short); 429 DEFINE_GETSETOOP(jchar, Char); 430 DEFINE_GETSETOOP(jint, Int); 431 DEFINE_GETSETOOP(jlong, Long); 432 DEFINE_GETSETOOP(jfloat, Float); 433 DEFINE_GETSETOOP(jdouble, Double); 434 435 #undef DEFINE_GETSETOOP 436 437 #define DEFINE_GETSETOOP_VOLATILE(jboolean, Boolean) \ 438 \ 439 UNSAFE_ENTRY(jboolean, Unsafe_Get##Boolean##Volatile(JNIEnv *env, jobject unsafe, jobject obj, jlong offset)) \ 440 UnsafeWrapper("Unsafe_Get"#Boolean); \ 441 GET_FIELD_VOLATILE(obj, offset, jboolean, v); \ 442 return v; \ 443 UNSAFE_END \ 444 \ 445 UNSAFE_ENTRY(void, Unsafe_Set##Boolean##Volatile(JNIEnv *env, jobject unsafe, jobject obj, jlong offset, jboolean x)) \ 446 UnsafeWrapper("Unsafe_Set"#Boolean); \ 447 SET_FIELD_VOLATILE(obj, offset, jboolean, x); \ 448 UNSAFE_END \ 449 \ 450 // END DEFINE_GETSETOOP_VOLATILE. 451 452 DEFINE_GETSETOOP_VOLATILE(jboolean, Boolean) 453 DEFINE_GETSETOOP_VOLATILE(jbyte, Byte) 454 DEFINE_GETSETOOP_VOLATILE(jshort, Short); 455 DEFINE_GETSETOOP_VOLATILE(jchar, Char); 456 DEFINE_GETSETOOP_VOLATILE(jint, Int); 457 DEFINE_GETSETOOP_VOLATILE(jfloat, Float); 458 DEFINE_GETSETOOP_VOLATILE(jdouble, Double); 459 460 #ifdef SUPPORTS_NATIVE_CX8 461 DEFINE_GETSETOOP_VOLATILE(jlong, Long); 462 #endif 463 464 #undef DEFINE_GETSETOOP_VOLATILE 465 466 // The non-intrinsified versions of setOrdered just use setVolatile 467 468 UNSAFE_ENTRY(void, Unsafe_SetOrderedInt(JNIEnv *env, jobject unsafe, jobject obj, jlong offset, jint x)) 469 UnsafeWrapper("Unsafe_SetOrderedInt"); 470 SET_FIELD_VOLATILE(obj, offset, jint, x); 471 UNSAFE_END 472 473 UNSAFE_ENTRY(void, Unsafe_SetOrderedObject(JNIEnv *env, jobject unsafe, jobject obj, jlong offset, jobject x_h)) 474 UnsafeWrapper("Unsafe_SetOrderedObject"); 475 oop x = JNIHandles::resolve(x_h); 476 oop p = JNIHandles::resolve(obj); 477 void* addr = index_oop_from_field_offset_long(p, offset); 478 OrderAccess::release(); 479 if (UseCompressedOops) { 480 oop_store((narrowOop*)addr, x); 481 } else { 482 oop_store((oop*)addr, x); 483 } 484 OrderAccess::fence(); 485 UNSAFE_END 486 487 UNSAFE_ENTRY(void, Unsafe_SetOrderedLong(JNIEnv *env, jobject unsafe, jobject obj, jlong offset, jlong x)) 488 UnsafeWrapper("Unsafe_SetOrderedLong"); 489 #ifdef SUPPORTS_NATIVE_CX8 490 SET_FIELD_VOLATILE(obj, offset, jlong, x); 491 #else 492 // Keep old code for platforms which may not have atomic long (8 bytes) instructions 493 { 494 if (VM_Version::supports_cx8()) { 495 SET_FIELD_VOLATILE(obj, offset, jlong, x); 496 } 497 else { 498 Handle p (THREAD, JNIHandles::resolve(obj)); 499 jlong* addr = (jlong*)(index_oop_from_field_offset_long(p(), offset)); 500 MutexLockerEx mu(UnsafeJlong_lock, Mutex::_no_safepoint_check_flag); 501 Atomic::store(x, addr); 502 } 503 } 504 #endif 505 UNSAFE_END 506 507 UNSAFE_ENTRY(void, Unsafe_LoadFence(JNIEnv *env, jobject unsafe)) 508 UnsafeWrapper("Unsafe_LoadFence"); 509 OrderAccess::acquire(); 510 UNSAFE_END 511 512 UNSAFE_ENTRY(void, Unsafe_StoreFence(JNIEnv *env, jobject unsafe)) 513 UnsafeWrapper("Unsafe_StoreFence"); 514 OrderAccess::release(); 515 UNSAFE_END 516 517 UNSAFE_ENTRY(void, Unsafe_FullFence(JNIEnv *env, jobject unsafe)) 518 UnsafeWrapper("Unsafe_FullFence"); 519 OrderAccess::fence(); 520 UNSAFE_END 521 522 ////// Data in the C heap. 523 524 // Note: These do not throw NullPointerException for bad pointers. 525 // They just crash. Only a oop base pointer can generate a NullPointerException. 526 // 527 #define DEFINE_GETSETNATIVE(java_type, Type, native_type) \ 528 \ 529 UNSAFE_ENTRY(java_type, Unsafe_GetNative##Type(JNIEnv *env, jobject unsafe, jlong addr)) \ 530 UnsafeWrapper("Unsafe_GetNative"#Type); \ 531 void* p = addr_from_java(addr); \ 532 JavaThread* t = JavaThread::current(); \ 533 t->set_doing_unsafe_access(true); \ 534 java_type x = *(volatile native_type*)p; \ 535 t->set_doing_unsafe_access(false); \ 536 return x; \ 537 UNSAFE_END \ 538 \ 539 UNSAFE_ENTRY(void, Unsafe_SetNative##Type(JNIEnv *env, jobject unsafe, jlong addr, java_type x)) \ 540 UnsafeWrapper("Unsafe_SetNative"#Type); \ 541 JavaThread* t = JavaThread::current(); \ 542 t->set_doing_unsafe_access(true); \ 543 void* p = addr_from_java(addr); \ 544 *(volatile native_type*)p = x; \ 545 t->set_doing_unsafe_access(false); \ 546 UNSAFE_END \ 547 \ 548 // END DEFINE_GETSETNATIVE. 549 550 DEFINE_GETSETNATIVE(jbyte, Byte, signed char) 551 DEFINE_GETSETNATIVE(jshort, Short, signed short); 552 DEFINE_GETSETNATIVE(jchar, Char, unsigned short); 553 DEFINE_GETSETNATIVE(jint, Int, jint); 554 // no long -- handled specially 555 DEFINE_GETSETNATIVE(jfloat, Float, float); 556 DEFINE_GETSETNATIVE(jdouble, Double, double); 557 558 #undef DEFINE_GETSETNATIVE 559 560 UNSAFE_ENTRY(jlong, Unsafe_GetNativeLong(JNIEnv *env, jobject unsafe, jlong addr)) 561 UnsafeWrapper("Unsafe_GetNativeLong"); 562 JavaThread* t = JavaThread::current(); 563 // We do it this way to avoid problems with access to heap using 64 564 // bit loads, as jlong in heap could be not 64-bit aligned, and on 565 // some CPUs (SPARC) it leads to SIGBUS. 566 t->set_doing_unsafe_access(true); 567 void* p = addr_from_java(addr); 568 jlong x; 569 if (((intptr_t)p & 7) == 0) { 570 // jlong is aligned, do a volatile access 571 x = *(volatile jlong*)p; 572 } else { 573 jlong_accessor acc; 574 acc.words[0] = ((volatile jint*)p)[0]; 575 acc.words[1] = ((volatile jint*)p)[1]; 576 x = acc.long_value; 577 } 578 t->set_doing_unsafe_access(false); 579 return x; 580 UNSAFE_END 581 582 UNSAFE_ENTRY(void, Unsafe_SetNativeLong(JNIEnv *env, jobject unsafe, jlong addr, jlong x)) 583 UnsafeWrapper("Unsafe_SetNativeLong"); 584 JavaThread* t = JavaThread::current(); 585 // see comment for Unsafe_GetNativeLong 586 t->set_doing_unsafe_access(true); 587 void* p = addr_from_java(addr); 588 if (((intptr_t)p & 7) == 0) { 589 // jlong is aligned, do a volatile access 590 *(volatile jlong*)p = x; 591 } else { 592 jlong_accessor acc; 593 acc.long_value = x; 594 ((volatile jint*)p)[0] = acc.words[0]; 595 ((volatile jint*)p)[1] = acc.words[1]; 596 } 597 t->set_doing_unsafe_access(false); 598 UNSAFE_END 599 600 601 UNSAFE_ENTRY(jlong, Unsafe_GetNativeAddress(JNIEnv *env, jobject unsafe, jlong addr)) 602 UnsafeWrapper("Unsafe_GetNativeAddress"); 603 void* p = addr_from_java(addr); 604 return addr_to_java(*(void**)p); 605 UNSAFE_END 606 607 UNSAFE_ENTRY(void, Unsafe_SetNativeAddress(JNIEnv *env, jobject unsafe, jlong addr, jlong x)) 608 UnsafeWrapper("Unsafe_SetNativeAddress"); 609 void* p = addr_from_java(addr); 610 *(void**)p = addr_from_java(x); 611 UNSAFE_END 612 613 614 ////// Allocation requests 615 616 UNSAFE_ENTRY(jobject, Unsafe_AllocateInstance(JNIEnv *env, jobject unsafe, jclass cls)) 617 UnsafeWrapper("Unsafe_AllocateInstance"); 618 { 619 ThreadToNativeFromVM ttnfv(thread); 620 return env->AllocObject(cls); 621 } 622 UNSAFE_END 623 624 UNSAFE_ENTRY(jlong, Unsafe_AllocateMemory(JNIEnv *env, jobject unsafe, jlong size)) 625 UnsafeWrapper("Unsafe_AllocateMemory"); 626 size_t sz = (size_t)size; 627 if (sz != (julong)size || size < 0) { 628 THROW_0(vmSymbols::java_lang_IllegalArgumentException()); 629 } 630 if (sz == 0) { 631 return 0; 632 } 633 sz = round_to(sz, HeapWordSize); 634 void* x = os::malloc(sz, mtInternal); 635 if (x == NULL) { 636 THROW_0(vmSymbols::java_lang_OutOfMemoryError()); 637 } 638 //Copy::fill_to_words((HeapWord*)x, sz / HeapWordSize); 639 return addr_to_java(x); 640 UNSAFE_END 641 642 UNSAFE_ENTRY(jlong, Unsafe_ReallocateMemory(JNIEnv *env, jobject unsafe, jlong addr, jlong size)) 643 UnsafeWrapper("Unsafe_ReallocateMemory"); 644 void* p = addr_from_java(addr); 645 size_t sz = (size_t)size; 646 if (sz != (julong)size || size < 0) { 647 THROW_0(vmSymbols::java_lang_IllegalArgumentException()); 648 } 649 if (sz == 0) { 650 os::free(p); 651 return 0; 652 } 653 sz = round_to(sz, HeapWordSize); 654 void* x = (p == NULL) ? os::malloc(sz, mtInternal) : os::realloc(p, sz, mtInternal); 655 if (x == NULL) { 656 THROW_0(vmSymbols::java_lang_OutOfMemoryError()); 657 } 658 return addr_to_java(x); 659 UNSAFE_END 660 661 UNSAFE_ENTRY(void, Unsafe_FreeMemory(JNIEnv *env, jobject unsafe, jlong addr)) 662 UnsafeWrapper("Unsafe_FreeMemory"); 663 void* p = addr_from_java(addr); 664 if (p == NULL) { 665 return; 666 } 667 os::free(p); 668 UNSAFE_END 669 670 UNSAFE_ENTRY(void, Unsafe_SetMemory(JNIEnv *env, jobject unsafe, jlong addr, jlong size, jbyte value)) 671 UnsafeWrapper("Unsafe_SetMemory"); 672 size_t sz = (size_t)size; 673 if (sz != (julong)size || size < 0) { 674 THROW(vmSymbols::java_lang_IllegalArgumentException()); 675 } 676 char* p = (char*) addr_from_java(addr); 677 Copy::fill_to_memory_atomic(p, sz, value); 678 UNSAFE_END 679 680 UNSAFE_ENTRY(void, Unsafe_SetMemory2(JNIEnv *env, jobject unsafe, jobject obj, jlong offset, jlong size, jbyte value)) 681 UnsafeWrapper("Unsafe_SetMemory"); 682 size_t sz = (size_t)size; 683 if (sz != (julong)size || size < 0) { 684 THROW(vmSymbols::java_lang_IllegalArgumentException()); 685 } 686 oop base = JNIHandles::resolve(obj); 687 void* p = index_oop_from_field_offset_long(base, offset); 688 Copy::fill_to_memory_atomic(p, sz, value); 689 UNSAFE_END 690 691 UNSAFE_ENTRY(void, Unsafe_CopyMemory(JNIEnv *env, jobject unsafe, jlong srcAddr, jlong dstAddr, jlong size)) 692 UnsafeWrapper("Unsafe_CopyMemory"); 693 if (size == 0) { 694 return; 695 } 696 size_t sz = (size_t)size; 697 if (sz != (julong)size || size < 0) { 698 THROW(vmSymbols::java_lang_IllegalArgumentException()); 699 } 700 void* src = addr_from_java(srcAddr); 701 void* dst = addr_from_java(dstAddr); 702 Copy::conjoint_memory_atomic(src, dst, sz); 703 UNSAFE_END 704 705 UNSAFE_ENTRY(void, Unsafe_CopyMemory2(JNIEnv *env, jobject unsafe, jobject srcObj, jlong srcOffset, jobject dstObj, jlong dstOffset, jlong size)) 706 UnsafeWrapper("Unsafe_CopyMemory"); 707 if (size == 0) { 708 return; 709 } 710 size_t sz = (size_t)size; 711 if (sz != (julong)size || size < 0) { 712 THROW(vmSymbols::java_lang_IllegalArgumentException()); 713 } 714 oop srcp = JNIHandles::resolve(srcObj); 715 oop dstp = JNIHandles::resolve(dstObj); 716 if (dstp != NULL && !dstp->is_typeArray()) { 717 // NYI: This works only for non-oop arrays at present. 718 // Generalizing it would be reasonable, but requires card marking. 719 // Also, autoboxing a Long from 0L in copyMemory(x,y, 0L,z, n) would be bad. 720 THROW(vmSymbols::java_lang_IllegalArgumentException()); 721 } 722 void* src = index_oop_from_field_offset_long(srcp, srcOffset); 723 void* dst = index_oop_from_field_offset_long(dstp, dstOffset); 724 Copy::conjoint_memory_atomic(src, dst, sz); 725 UNSAFE_END 726 727 728 ////// Random queries 729 730 // See comment at file start about UNSAFE_LEAF 731 //UNSAFE_LEAF(jint, Unsafe_AddressSize()) 732 UNSAFE_ENTRY(jint, Unsafe_AddressSize(JNIEnv *env, jobject unsafe)) 733 UnsafeWrapper("Unsafe_AddressSize"); 734 return sizeof(void*); 735 UNSAFE_END 736 737 // See comment at file start about UNSAFE_LEAF 738 //UNSAFE_LEAF(jint, Unsafe_PageSize()) 739 UNSAFE_ENTRY(jint, Unsafe_PageSize(JNIEnv *env, jobject unsafe)) 740 UnsafeWrapper("Unsafe_PageSize"); 741 return os::vm_page_size(); 742 UNSAFE_END 743 744 jint find_field_offset(jobject field, int must_be_static, TRAPS) { 745 if (field == NULL) { 746 THROW_0(vmSymbols::java_lang_NullPointerException()); 747 } 748 749 oop reflected = JNIHandles::resolve_non_null(field); 750 oop mirror = java_lang_reflect_Field::clazz(reflected); 751 Klass* k = java_lang_Class::as_Klass(mirror); 752 int slot = java_lang_reflect_Field::slot(reflected); 753 int modifiers = java_lang_reflect_Field::modifiers(reflected); 754 755 if (must_be_static >= 0) { 756 int really_is_static = ((modifiers & JVM_ACC_STATIC) != 0); 757 if (must_be_static != really_is_static) { 758 THROW_0(vmSymbols::java_lang_IllegalArgumentException()); 759 } 760 } 761 762 int offset = InstanceKlass::cast(k)->field_offset(slot); 763 return field_offset_from_byte_offset(offset); 764 } 765 766 UNSAFE_ENTRY(jlong, Unsafe_ObjectFieldOffset(JNIEnv *env, jobject unsafe, jobject field)) 767 UnsafeWrapper("Unsafe_ObjectFieldOffset"); 768 return find_field_offset(field, 0, THREAD); 769 UNSAFE_END 770 771 UNSAFE_ENTRY(jlong, Unsafe_StaticFieldOffset(JNIEnv *env, jobject unsafe, jobject field)) 772 UnsafeWrapper("Unsafe_StaticFieldOffset"); 773 return find_field_offset(field, 1, THREAD); 774 UNSAFE_END 775 776 UNSAFE_ENTRY(jobject, Unsafe_StaticFieldBaseFromField(JNIEnv *env, jobject unsafe, jobject field)) 777 UnsafeWrapper("Unsafe_StaticFieldBase"); 778 // Note: In this VM implementation, a field address is always a short 779 // offset from the base of a a klass metaobject. Thus, the full dynamic 780 // range of the return type is never used. However, some implementations 781 // might put the static field inside an array shared by many classes, 782 // or even at a fixed address, in which case the address could be quite 783 // large. In that last case, this function would return NULL, since 784 // the address would operate alone, without any base pointer. 785 786 if (field == NULL) THROW_0(vmSymbols::java_lang_NullPointerException()); 787 788 oop reflected = JNIHandles::resolve_non_null(field); 789 oop mirror = java_lang_reflect_Field::clazz(reflected); 790 int modifiers = java_lang_reflect_Field::modifiers(reflected); 791 792 if ((modifiers & JVM_ACC_STATIC) == 0) { 793 THROW_0(vmSymbols::java_lang_IllegalArgumentException()); 794 } 795 796 return JNIHandles::make_local(env, mirror); 797 UNSAFE_END 798 799 //@deprecated 800 UNSAFE_ENTRY(jint, Unsafe_FieldOffset(JNIEnv *env, jobject unsafe, jobject field)) 801 UnsafeWrapper("Unsafe_FieldOffset"); 802 // tries (but fails) to be polymorphic between static and non-static: 803 jlong offset = find_field_offset(field, -1, THREAD); 804 guarantee(offset == (jint)offset, "offset fits in 32 bits"); 805 return (jint)offset; 806 UNSAFE_END 807 808 //@deprecated 809 UNSAFE_ENTRY(jobject, Unsafe_StaticFieldBaseFromClass(JNIEnv *env, jobject unsafe, jobject clazz)) 810 UnsafeWrapper("Unsafe_StaticFieldBase"); 811 if (clazz == NULL) { 812 THROW_0(vmSymbols::java_lang_NullPointerException()); 813 } 814 return JNIHandles::make_local(env, JNIHandles::resolve_non_null(clazz)); 815 UNSAFE_END 816 817 UNSAFE_ENTRY(void, Unsafe_EnsureClassInitialized(JNIEnv *env, jobject unsafe, jobject clazz)) { 818 UnsafeWrapper("Unsafe_EnsureClassInitialized"); 819 if (clazz == NULL) { 820 THROW(vmSymbols::java_lang_NullPointerException()); 821 } 822 oop mirror = JNIHandles::resolve_non_null(clazz); 823 824 Klass* klass = java_lang_Class::as_Klass(mirror); 825 if (klass != NULL && klass->should_be_initialized()) { 826 InstanceKlass* k = InstanceKlass::cast(klass); 827 k->initialize(CHECK); 828 } 829 } 830 UNSAFE_END 831 832 UNSAFE_ENTRY(jboolean, Unsafe_ShouldBeInitialized(JNIEnv *env, jobject unsafe, jobject clazz)) { 833 UnsafeWrapper("Unsafe_ShouldBeInitialized"); 834 if (clazz == NULL) { 835 THROW_(vmSymbols::java_lang_NullPointerException(), false); 836 } 837 oop mirror = JNIHandles::resolve_non_null(clazz); 838 Klass* klass = java_lang_Class::as_Klass(mirror); 839 if (klass != NULL && klass->should_be_initialized()) { 840 return true; 841 } 842 return false; 843 } 844 UNSAFE_END 845 846 static void getBaseAndScale(int& base, int& scale, jclass acls, TRAPS) { 847 if (acls == NULL) { 848 THROW(vmSymbols::java_lang_NullPointerException()); 849 } 850 oop mirror = JNIHandles::resolve_non_null(acls); 851 Klass* k = java_lang_Class::as_Klass(mirror); 852 if (k == NULL || !k->oop_is_array()) { 853 THROW(vmSymbols::java_lang_InvalidClassException()); 854 } else if (k->oop_is_objArray()) { 855 base = arrayOopDesc::base_offset_in_bytes(T_OBJECT); 856 scale = heapOopSize; 857 } else if (k->oop_is_typeArray()) { 858 TypeArrayKlass* tak = TypeArrayKlass::cast(k); 859 base = tak->array_header_in_bytes(); 860 assert(base == arrayOopDesc::base_offset_in_bytes(tak->element_type()), "array_header_size semantics ok"); 861 scale = (1 << tak->log2_element_size()); 862 } else { 863 ShouldNotReachHere(); 864 } 865 } 866 867 UNSAFE_ENTRY(jint, Unsafe_ArrayBaseOffset(JNIEnv *env, jobject unsafe, jclass acls)) 868 UnsafeWrapper("Unsafe_ArrayBaseOffset"); 869 int base = 0, scale = 0; 870 getBaseAndScale(base, scale, acls, CHECK_0); 871 return field_offset_from_byte_offset(base); 872 UNSAFE_END 873 874 875 UNSAFE_ENTRY(jint, Unsafe_ArrayIndexScale(JNIEnv *env, jobject unsafe, jclass acls)) 876 UnsafeWrapper("Unsafe_ArrayIndexScale"); 877 int base = 0, scale = 0; 878 getBaseAndScale(base, scale, acls, CHECK_0); 879 // This VM packs both fields and array elements down to the byte. 880 // But watch out: If this changes, so that array references for 881 // a given primitive type (say, T_BOOLEAN) use different memory units 882 // than fields, this method MUST return zero for such arrays. 883 // For example, the VM used to store sub-word sized fields in full 884 // words in the object layout, so that accessors like getByte(Object,int) 885 // did not really do what one might expect for arrays. Therefore, 886 // this function used to report a zero scale factor, so that the user 887 // would know not to attempt to access sub-word array elements. 888 // // Code for unpacked fields: 889 // if (scale < wordSize) return 0; 890 891 // The following allows for a pretty general fieldOffset cookie scheme, 892 // but requires it to be linear in byte offset. 893 return field_offset_from_byte_offset(scale) - field_offset_from_byte_offset(0); 894 UNSAFE_END 895 896 897 static inline void throw_new(JNIEnv *env, const char *ename) { 898 char buf[100]; 899 strcpy(buf, "java/lang/"); 900 strcat(buf, ename); 901 jclass cls = env->FindClass(buf); 902 if (env->ExceptionCheck()) { 903 env->ExceptionClear(); 904 tty->print_cr("Unsafe: cannot throw %s because FindClass has failed", buf); 905 return; 906 } 907 char* msg = NULL; 908 env->ThrowNew(cls, msg); 909 } 910 911 static jclass Unsafe_DefineClass_impl(JNIEnv *env, jstring name, jbyteArray data, int offset, int length, jobject loader, jobject pd) { 912 { 913 // Code lifted from JDK 1.3 ClassLoader.c 914 915 jbyte *body; 916 char *utfName; 917 jclass result = 0; 918 char buf[128]; 919 920 if (UsePerfData) { 921 ClassLoader::unsafe_defineClassCallCounter()->inc(); 922 } 923 924 if (data == NULL) { 925 throw_new(env, "NullPointerException"); 926 return 0; 927 } 928 929 /* Work around 4153825. malloc crashes on Solaris when passed a 930 * negative size. 931 */ 932 if (length < 0) { 933 throw_new(env, "ArrayIndexOutOfBoundsException"); 934 return 0; 935 } 936 937 body = NEW_C_HEAP_ARRAY(jbyte, length, mtInternal); 938 939 if (body == 0) { 940 throw_new(env, "OutOfMemoryError"); 941 return 0; 942 } 943 944 env->GetByteArrayRegion(data, offset, length, body); 945 946 if (env->ExceptionOccurred()) 947 goto free_body; 948 949 if (name != NULL) { 950 uint len = env->GetStringUTFLength(name); 951 int unicode_len = env->GetStringLength(name); 952 if (len >= sizeof(buf)) { 953 utfName = NEW_C_HEAP_ARRAY(char, len + 1, mtInternal); 954 if (utfName == NULL) { 955 throw_new(env, "OutOfMemoryError"); 956 goto free_body; 957 } 958 } else { 959 utfName = buf; 960 } 961 env->GetStringUTFRegion(name, 0, unicode_len, utfName); 962 //VerifyFixClassname(utfName); 963 for (uint i = 0; i < len; i++) { 964 if (utfName[i] == '.') utfName[i] = '/'; 965 } 966 } else { 967 utfName = NULL; 968 } 969 970 result = JVM_DefineClass(env, utfName, loader, body, length, pd); 971 972 if (utfName && utfName != buf) 973 FREE_C_HEAP_ARRAY(char, utfName, mtInternal); 974 975 free_body: 976 FREE_C_HEAP_ARRAY(jbyte, body, mtInternal); 977 return result; 978 } 979 } 980 981 982 UNSAFE_ENTRY(jclass, Unsafe_DefineClass(JNIEnv *env, jobject unsafe, jstring name, jbyteArray data, int offset, int length, jobject loader, jobject pd)) 983 UnsafeWrapper("Unsafe_DefineClass"); 984 { 985 ThreadToNativeFromVM ttnfv(thread); 986 return Unsafe_DefineClass_impl(env, name, data, offset, length, loader, pd); 987 } 988 UNSAFE_END 989 990 991 UNSAFE_ENTRY(jclass, Unsafe_DefineClass0(JNIEnv *env, jobject unsafe, jstring name, jbyteArray data, int offset, int length)) 992 UnsafeWrapper("Unsafe_DefineClass"); 993 { 994 ThreadToNativeFromVM ttnfv(thread); 995 996 int depthFromDefineClass0 = 1; 997 jclass caller = JVM_GetCallerClass(env, depthFromDefineClass0); 998 jobject loader = (caller == NULL) ? NULL : JVM_GetClassLoader(env, caller); 999 jobject pd = (caller == NULL) ? NULL : JVM_GetProtectionDomain(env, caller); 1000 1001 return Unsafe_DefineClass_impl(env, name, data, offset, length, loader, pd); 1002 } 1003 UNSAFE_END 1004 1005 1006 #define DAC_Args CLS"[B["OBJ 1007 // define a class but do not make it known to the class loader or system dictionary 1008 // - host_class: supplies context for linkage, access control, protection domain, and class loader 1009 // - data: bytes of a class file, a raw memory address (length gives the number of bytes) 1010 // - cp_patches: where non-null entries exist, they replace corresponding CP entries in data 1011 1012 // When you load an anonymous class U, it works as if you changed its name just before loading, 1013 // to a name that you will never use again. Since the name is lost, no other class can directly 1014 // link to any member of U. Just after U is loaded, the only way to use it is reflectively, 1015 // through java.lang.Class methods like Class.newInstance. 1016 1017 // Access checks for linkage sites within U continue to follow the same rules as for named classes. 1018 // The package of an anonymous class is given by the package qualifier on the name under which it was loaded. 1019 // An anonymous class also has special privileges to access any member of its host class. 1020 // This is the main reason why this loading operation is unsafe. The purpose of this is to 1021 // allow language implementations to simulate "open classes"; a host class in effect gets 1022 // new code when an anonymous class is loaded alongside it. A less convenient but more 1023 // standard way to do this is with reflection, which can also be set to ignore access 1024 // restrictions. 1025 1026 // Access into an anonymous class is possible only through reflection. Therefore, there 1027 // are no special access rules for calling into an anonymous class. The relaxed access 1028 // rule for the host class is applied in the opposite direction: A host class reflectively 1029 // access one of its anonymous classes. 1030 1031 // If you load the same bytecodes twice, you get two different classes. You can reload 1032 // the same bytecodes with or without varying CP patches. 1033 1034 // By using the CP patching array, you can have a new anonymous class U2 refer to an older one U1. 1035 // The bytecodes for U2 should refer to U1 by a symbolic name (doesn't matter what the name is). 1036 // The CONSTANT_Class entry for that name can be patched to refer directly to U1. 1037 1038 // This allows, for example, U2 to use U1 as a superclass or super-interface, or as 1039 // an outer class (so that U2 is an anonymous inner class of anonymous U1). 1040 // It is not possible for a named class, or an older anonymous class, to refer by 1041 // name (via its CP) to a newer anonymous class. 1042 1043 // CP patching may also be used to modify (i.e., hack) the names of methods, classes, 1044 // or type descriptors used in the loaded anonymous class. 1045 1046 // Finally, CP patching may be used to introduce "live" objects into the constant pool, 1047 // instead of "dead" strings. A compiled statement like println((Object)"hello") can 1048 // be changed to println(greeting), where greeting is an arbitrary object created before 1049 // the anonymous class is loaded. This is useful in dynamic languages, in which 1050 // various kinds of metaobjects must be introduced as constants into bytecode. 1051 // Note the cast (Object), which tells the verifier to expect an arbitrary object, 1052 // not just a literal string. For such ldc instructions, the verifier uses the 1053 // type Object instead of String, if the loaded constant is not in fact a String. 1054 1055 static instanceKlassHandle 1056 Unsafe_DefineAnonymousClass_impl(JNIEnv *env, 1057 jclass host_class, jbyteArray data, jobjectArray cp_patches_jh, 1058 HeapWord* *temp_alloc, 1059 TRAPS) { 1060 1061 if (UsePerfData) { 1062 ClassLoader::unsafe_defineClassCallCounter()->inc(); 1063 } 1064 1065 if (data == NULL) { 1066 THROW_0(vmSymbols::java_lang_NullPointerException()); 1067 } 1068 1069 jint length = typeArrayOop(JNIHandles::resolve_non_null(data))->length(); 1070 jint word_length = (length + sizeof(HeapWord)-1) / sizeof(HeapWord); 1071 HeapWord* body = NEW_C_HEAP_ARRAY(HeapWord, word_length, mtInternal); 1072 if (body == NULL) { 1073 THROW_0(vmSymbols::java_lang_OutOfMemoryError()); 1074 } 1075 1076 // caller responsible to free it: 1077 (*temp_alloc) = body; 1078 1079 { 1080 jbyte* array_base = typeArrayOop(JNIHandles::resolve_non_null(data))->byte_at_addr(0); 1081 Copy::conjoint_words((HeapWord*) array_base, body, word_length); 1082 } 1083 1084 u1* class_bytes = (u1*) body; 1085 int class_bytes_length = (int) length; 1086 if (class_bytes_length < 0) class_bytes_length = 0; 1087 if (class_bytes == NULL 1088 || host_class == NULL 1089 || length != class_bytes_length) 1090 THROW_0(vmSymbols::java_lang_IllegalArgumentException()); 1091 1092 objArrayHandle cp_patches_h; 1093 if (cp_patches_jh != NULL) { 1094 oop p = JNIHandles::resolve_non_null(cp_patches_jh); 1095 if (!p->is_objArray()) 1096 THROW_0(vmSymbols::java_lang_IllegalArgumentException()); 1097 cp_patches_h = objArrayHandle(THREAD, (objArrayOop)p); 1098 } 1099 1100 KlassHandle host_klass(THREAD, java_lang_Class::as_Klass(JNIHandles::resolve_non_null(host_class))); 1101 const char* host_source = host_klass->external_name(); 1102 Handle host_loader(THREAD, host_klass->class_loader()); 1103 Handle host_domain(THREAD, host_klass->protection_domain()); 1104 1105 GrowableArray<Handle>* cp_patches = NULL; 1106 if (cp_patches_h.not_null()) { 1107 int alen = cp_patches_h->length(); 1108 for (int i = alen-1; i >= 0; i--) { 1109 oop p = cp_patches_h->obj_at(i); 1110 if (p != NULL) { 1111 Handle patch(THREAD, p); 1112 if (cp_patches == NULL) 1113 cp_patches = new GrowableArray<Handle>(i+1, i+1, Handle()); 1114 cp_patches->at_put(i, patch); 1115 } 1116 } 1117 } 1118 1119 ClassFileStream st(class_bytes, class_bytes_length, (char*) host_source); 1120 1121 instanceKlassHandle anon_klass; 1122 { 1123 Symbol* no_class_name = NULL; 1124 Klass* anonk = SystemDictionary::parse_stream(no_class_name, 1125 host_loader, host_domain, 1126 &st, host_klass, cp_patches, 1127 CHECK_NULL); 1128 if (anonk == NULL) return NULL; 1129 anon_klass = instanceKlassHandle(THREAD, anonk); 1130 } 1131 1132 return anon_klass; 1133 } 1134 1135 UNSAFE_ENTRY(jclass, Unsafe_DefineAnonymousClass(JNIEnv *env, jobject unsafe, jclass host_class, jbyteArray data, jobjectArray cp_patches_jh)) 1136 { 1137 instanceKlassHandle anon_klass; 1138 jobject res_jh = NULL; 1139 1140 UnsafeWrapper("Unsafe_DefineAnonymousClass"); 1141 ResourceMark rm(THREAD); 1142 1143 HeapWord* temp_alloc = NULL; 1144 1145 anon_klass = Unsafe_DefineAnonymousClass_impl(env, host_class, data, 1146 cp_patches_jh, 1147 &temp_alloc, THREAD); 1148 if (anon_klass() != NULL) 1149 res_jh = JNIHandles::make_local(env, anon_klass->java_mirror()); 1150 1151 // try/finally clause: 1152 if (temp_alloc != NULL) { 1153 FREE_C_HEAP_ARRAY(HeapWord, temp_alloc, mtInternal); 1154 } 1155 1156 // The anonymous class loader data has been artificially been kept alive to 1157 // this point. The mirror and any instances of this class have to keep 1158 // it alive afterwards. 1159 if (anon_klass() != NULL) { 1160 anon_klass->class_loader_data()->set_keep_alive(false); 1161 } 1162 1163 // let caller initialize it as needed... 1164 1165 return (jclass) res_jh; 1166 } 1167 UNSAFE_END 1168 1169 1170 1171 UNSAFE_ENTRY(void, Unsafe_MonitorEnter(JNIEnv *env, jobject unsafe, jobject jobj)) 1172 UnsafeWrapper("Unsafe_MonitorEnter"); 1173 { 1174 if (jobj == NULL) { 1175 THROW(vmSymbols::java_lang_NullPointerException()); 1176 } 1177 Handle obj(thread, JNIHandles::resolve_non_null(jobj)); 1178 ObjectSynchronizer::jni_enter(obj, CHECK); 1179 } 1180 UNSAFE_END 1181 1182 1183 UNSAFE_ENTRY(jboolean, Unsafe_TryMonitorEnter(JNIEnv *env, jobject unsafe, jobject jobj)) 1184 UnsafeWrapper("Unsafe_TryMonitorEnter"); 1185 { 1186 if (jobj == NULL) { 1187 THROW_(vmSymbols::java_lang_NullPointerException(), JNI_FALSE); 1188 } 1189 Handle obj(thread, JNIHandles::resolve_non_null(jobj)); 1190 bool res = ObjectSynchronizer::jni_try_enter(obj, CHECK_0); 1191 return (res ? JNI_TRUE : JNI_FALSE); 1192 } 1193 UNSAFE_END 1194 1195 1196 UNSAFE_ENTRY(void, Unsafe_MonitorExit(JNIEnv *env, jobject unsafe, jobject jobj)) 1197 UnsafeWrapper("Unsafe_MonitorExit"); 1198 { 1199 if (jobj == NULL) { 1200 THROW(vmSymbols::java_lang_NullPointerException()); 1201 } 1202 Handle obj(THREAD, JNIHandles::resolve_non_null(jobj)); 1203 ObjectSynchronizer::jni_exit(obj(), CHECK); 1204 } 1205 UNSAFE_END 1206 1207 1208 UNSAFE_ENTRY(void, Unsafe_ThrowException(JNIEnv *env, jobject unsafe, jthrowable thr)) 1209 UnsafeWrapper("Unsafe_ThrowException"); 1210 { 1211 ThreadToNativeFromVM ttnfv(thread); 1212 env->Throw(thr); 1213 } 1214 UNSAFE_END 1215 1216 // JSR166 ------------------------------------------------------------------ 1217 1218 UNSAFE_ENTRY(jboolean, Unsafe_CompareAndSwapObject(JNIEnv *env, jobject unsafe, jobject obj, jlong offset, jobject e_h, jobject x_h)) 1219 UnsafeWrapper("Unsafe_CompareAndSwapObject"); 1220 oop x = JNIHandles::resolve(x_h); 1221 oop e = JNIHandles::resolve(e_h); 1222 oop p = JNIHandles::resolve(obj); 1223 HeapWord* addr = (HeapWord *)index_oop_from_field_offset_long(p, offset); 1224 oop res = oopDesc::atomic_compare_exchange_oop(x, addr, e, true); 1225 jboolean success = (res == e); 1226 if (success) 1227 update_barrier_set((void*)addr, x); 1228 return success; 1229 UNSAFE_END 1230 1231 UNSAFE_ENTRY(jboolean, Unsafe_CompareAndSwapInt(JNIEnv *env, jobject unsafe, jobject obj, jlong offset, jint e, jint x)) 1232 UnsafeWrapper("Unsafe_CompareAndSwapInt"); 1233 oop p = JNIHandles::resolve(obj); 1234 jint* addr = (jint *) index_oop_from_field_offset_long(p, offset); 1235 return (jint)(Atomic::cmpxchg(x, addr, e)) == e; 1236 UNSAFE_END 1237 1238 UNSAFE_ENTRY(jboolean, Unsafe_CompareAndSwapLong(JNIEnv *env, jobject unsafe, jobject obj, jlong offset, jlong e, jlong x)) 1239 UnsafeWrapper("Unsafe_CompareAndSwapLong"); 1240 Handle p (THREAD, JNIHandles::resolve(obj)); 1241 jlong* addr = (jlong*)(index_oop_from_field_offset_long(p(), offset)); 1242 #ifdef SUPPORTS_NATIVE_CX8 1243 return (jlong)(Atomic::cmpxchg(x, addr, e)) == e; 1244 #else 1245 if (VM_Version::supports_cx8()) 1246 return (jlong)(Atomic::cmpxchg(x, addr, e)) == e; 1247 else { 1248 jboolean success = false; 1249 MutexLockerEx mu(UnsafeJlong_lock, Mutex::_no_safepoint_check_flag); 1250 jlong val = Atomic::load(addr); 1251 if (val == e) { Atomic::store(x, addr); success = true; } 1252 return success; 1253 } 1254 #endif 1255 UNSAFE_END 1256 1257 static void post_thread_park_event(EventThreadPark* event, const oop obj, jlong timeout_nanos, jlong until_epoch_millis) { 1258 assert(event != NULL, "invariant"); 1259 assert(event->should_commit(), "invariant"); 1260 event->set_parkedClass((obj != NULL) ? obj->klass() : NULL); 1261 event->set_timeout(timeout_nanos); 1262 event->set_until(until_epoch_millis); 1263 event->set_address((obj != NULL) ? (u8)cast_from_oop<uintptr_t>(obj) : 0); 1264 event->commit(); 1265 } 1266 1267 UNSAFE_ENTRY(void, Unsafe_Park(JNIEnv *env, jobject unsafe, jboolean isAbsolute, jlong time)) 1268 UnsafeWrapper("Unsafe_Park"); 1269 EventThreadPark event; 1270 #ifndef USDT2 1271 HS_DTRACE_PROBE3(hotspot, thread__park__begin, thread->parker(), (int) isAbsolute, time); 1272 #else /* USDT2 */ 1273 HOTSPOT_THREAD_PARK_BEGIN( 1274 (uintptr_t) thread->parker(), (int) isAbsolute, time); 1275 #endif /* USDT2 */ 1276 JavaThreadParkedState jtps(thread, time != 0); 1277 thread->parker()->park(isAbsolute != 0, time); 1278 #ifndef USDT2 1279 HS_DTRACE_PROBE1(hotspot, thread__park__end, thread->parker()); 1280 #else /* USDT2 */ 1281 HOTSPOT_THREAD_PARK_END( 1282 (uintptr_t) thread->parker()); 1283 #endif /* USDT2 */ 1284 if (event.should_commit()) { 1285 const oop obj = thread->current_park_blocker(); 1286 if (time == 0) { 1287 post_thread_park_event(&event, obj, min_jlong, min_jlong); 1288 } else { 1289 if (isAbsolute != 0) { 1290 post_thread_park_event(&event, obj, min_jlong, time); 1291 } else { 1292 post_thread_park_event(&event, obj, time, min_jlong); 1293 } 1294 } 1295 } 1296 UNSAFE_END 1297 1298 UNSAFE_ENTRY(void, Unsafe_Unpark(JNIEnv *env, jobject unsafe, jobject jthread)) 1299 UnsafeWrapper("Unsafe_Unpark"); 1300 Parker* p = NULL; 1301 if (jthread != NULL) { 1302 oop java_thread = JNIHandles::resolve_non_null(jthread); 1303 if (java_thread != NULL) { 1304 jlong lp = java_lang_Thread::park_event(java_thread); 1305 if (lp != 0) { 1306 // This cast is OK even though the jlong might have been read 1307 // non-atomically on 32bit systems, since there, one word will 1308 // always be zero anyway and the value set is always the same 1309 p = (Parker*)addr_from_java(lp); 1310 } else { 1311 // Grab lock if apparently null or using older version of library 1312 MutexLocker mu(Threads_lock); 1313 java_thread = JNIHandles::resolve_non_null(jthread); 1314 if (java_thread != NULL) { 1315 JavaThread* thr = java_lang_Thread::thread(java_thread); 1316 if (thr != NULL) { 1317 p = thr->parker(); 1318 if (p != NULL) { // Bind to Java thread for next time. 1319 java_lang_Thread::set_park_event(java_thread, addr_to_java(p)); 1320 } 1321 } 1322 } 1323 } 1324 } 1325 } 1326 if (p != NULL) { 1327 #ifndef USDT2 1328 HS_DTRACE_PROBE1(hotspot, thread__unpark, p); 1329 #else /* USDT2 */ 1330 HOTSPOT_THREAD_UNPARK( 1331 (uintptr_t) p); 1332 #endif /* USDT2 */ 1333 p->unpark(); 1334 } 1335 UNSAFE_END 1336 1337 UNSAFE_ENTRY(jint, Unsafe_Loadavg(JNIEnv *env, jobject unsafe, jdoubleArray loadavg, jint nelem)) 1338 UnsafeWrapper("Unsafe_Loadavg"); 1339 const int max_nelem = 3; 1340 double la[max_nelem]; 1341 jint ret; 1342 1343 typeArrayOop a = typeArrayOop(JNIHandles::resolve_non_null(loadavg)); 1344 assert(a->is_typeArray(), "must be type array"); 1345 1346 if (nelem < 0 || nelem > max_nelem || a->length() < nelem) { 1347 ThreadToNativeFromVM ttnfv(thread); 1348 throw_new(env, "ArrayIndexOutOfBoundsException"); 1349 return -1; 1350 } 1351 1352 ret = os::loadavg(la, nelem); 1353 if (ret == -1) return -1; 1354 1355 // if successful, ret is the number of samples actually retrieved. 1356 assert(ret >= 0 && ret <= max_nelem, "Unexpected loadavg return value"); 1357 switch(ret) { 1358 case 3: a->double_at_put(2, (jdouble)la[2]); // fall through 1359 case 2: a->double_at_put(1, (jdouble)la[1]); // fall through 1360 case 1: a->double_at_put(0, (jdouble)la[0]); break; 1361 } 1362 return ret; 1363 UNSAFE_END 1364 1365 UNSAFE_ENTRY(void, Unsafe_PrefetchRead(JNIEnv* env, jclass ignored, jobject obj, jlong offset)) 1366 UnsafeWrapper("Unsafe_PrefetchRead"); 1367 oop p = JNIHandles::resolve(obj); 1368 void* addr = index_oop_from_field_offset_long(p, 0); 1369 Prefetch::read(addr, (intx)offset); 1370 UNSAFE_END 1371 1372 UNSAFE_ENTRY(void, Unsafe_PrefetchWrite(JNIEnv* env, jclass ignored, jobject obj, jlong offset)) 1373 UnsafeWrapper("Unsafe_PrefetchWrite"); 1374 oop p = JNIHandles::resolve(obj); 1375 void* addr = index_oop_from_field_offset_long(p, 0); 1376 Prefetch::write(addr, (intx)offset); 1377 UNSAFE_END 1378 1379 1380 /// JVM_RegisterUnsafeMethods 1381 1382 #define ADR "J" 1383 1384 #define LANG "Ljava/lang/" 1385 1386 #define OBJ LANG "Object;" 1387 #define CLS LANG "Class;" 1388 #define CTR LANG "reflect/Constructor;" 1389 #define FLD LANG "reflect/Field;" 1390 #define MTH LANG "reflect/Method;" 1391 #define THR LANG "Throwable;" 1392 1393 #define DC0_Args LANG "String;[BII" 1394 #define DC_Args DC0_Args LANG "ClassLoader;" "Ljava/security/ProtectionDomain;" 1395 1396 #define CC (char*) /*cast a literal from (const char*)*/ 1397 #define FN_PTR(f) CAST_FROM_FN_PTR(void*, &f) 1398 1399 // define deprecated accessors for compabitility with 1.4.0 1400 #define DECLARE_GETSETOOP_140(Boolean, Z) \ 1401 {CC "get" #Boolean, CC "(" OBJ "I)" #Z, FN_PTR(Unsafe_Get##Boolean##140)}, \ 1402 {CC "put" #Boolean, CC "(" OBJ "I" #Z ")V", FN_PTR(Unsafe_Set##Boolean##140)} 1403 1404 // Note: In 1.4.1, getObject and kin take both int and long offsets. 1405 #define DECLARE_GETSETOOP_141(Boolean, Z) \ 1406 {CC "get" #Boolean, CC "(" OBJ "J)" #Z, FN_PTR(Unsafe_Get##Boolean)}, \ 1407 {CC "put" #Boolean, CC "(" OBJ "J" #Z ")V", FN_PTR(Unsafe_Set##Boolean)} 1408 1409 // Note: In 1.5.0, there are volatile versions too 1410 #define DECLARE_GETSETOOP(Boolean, Z) \ 1411 {CC "get" #Boolean, CC "(" OBJ "J)" #Z, FN_PTR(Unsafe_Get##Boolean)}, \ 1412 {CC "put" #Boolean, CC "(" OBJ "J" #Z ")V", FN_PTR(Unsafe_Set##Boolean)}, \ 1413 {CC "get" #Boolean "Volatile", CC "(" OBJ "J)" #Z, FN_PTR(Unsafe_Get##Boolean##Volatile)}, \ 1414 {CC "put" #Boolean "Volatile", CC "(" OBJ "J" #Z ")V", FN_PTR(Unsafe_Set##Boolean##Volatile)} 1415 1416 1417 #define DECLARE_GETSETNATIVE(Byte, B) \ 1418 {CC "get" #Byte, CC "(" ADR ")" #B, FN_PTR(Unsafe_GetNative##Byte)}, \ 1419 {CC "put" #Byte, CC "(" ADR#B ")V", FN_PTR(Unsafe_SetNative##Byte)} 1420 1421 1422 1423 // These are the methods for 1.4.0 1424 static JNINativeMethod methods_140[] = { 1425 {CC "getObject", CC "(" OBJ "I)" OBJ "", FN_PTR(Unsafe_GetObject140)}, 1426 {CC "putObject", CC "(" OBJ "I" OBJ ")V", FN_PTR(Unsafe_SetObject140)}, 1427 1428 DECLARE_GETSETOOP_140(Boolean, Z), 1429 DECLARE_GETSETOOP_140(Byte, B), 1430 DECLARE_GETSETOOP_140(Short, S), 1431 DECLARE_GETSETOOP_140(Char, C), 1432 DECLARE_GETSETOOP_140(Int, I), 1433 DECLARE_GETSETOOP_140(Long, J), 1434 DECLARE_GETSETOOP_140(Float, F), 1435 DECLARE_GETSETOOP_140(Double, D), 1436 1437 DECLARE_GETSETNATIVE(Byte, B), 1438 DECLARE_GETSETNATIVE(Short, S), 1439 DECLARE_GETSETNATIVE(Char, C), 1440 DECLARE_GETSETNATIVE(Int, I), 1441 DECLARE_GETSETNATIVE(Long, J), 1442 DECLARE_GETSETNATIVE(Float, F), 1443 DECLARE_GETSETNATIVE(Double, D), 1444 1445 {CC "getAddress", CC "(" ADR ")" ADR, FN_PTR(Unsafe_GetNativeAddress)}, 1446 {CC "putAddress", CC "(" ADR "" ADR ")V", FN_PTR(Unsafe_SetNativeAddress)}, 1447 1448 {CC "allocateMemory", CC "(J)" ADR, FN_PTR(Unsafe_AllocateMemory)}, 1449 {CC "reallocateMemory", CC "(" ADR "J)" ADR, FN_PTR(Unsafe_ReallocateMemory)}, 1450 {CC "freeMemory", CC "(" ADR ")V", FN_PTR(Unsafe_FreeMemory)}, 1451 1452 {CC "fieldOffset", CC "(" FLD ")I", FN_PTR(Unsafe_FieldOffset)}, 1453 {CC "staticFieldBase", CC "(" CLS ")" OBJ, FN_PTR(Unsafe_StaticFieldBaseFromClass)}, 1454 {CC "ensureClassInitialized",CC "(" CLS ")V", FN_PTR(Unsafe_EnsureClassInitialized)}, 1455 {CC "arrayBaseOffset", CC "(" CLS ")I", FN_PTR(Unsafe_ArrayBaseOffset)}, 1456 {CC "arrayIndexScale", CC "(" CLS ")I", FN_PTR(Unsafe_ArrayIndexScale)}, 1457 {CC "addressSize", CC "()I", FN_PTR(Unsafe_AddressSize)}, 1458 {CC "pageSize", CC "()I", FN_PTR(Unsafe_PageSize)}, 1459 1460 {CC "defineClass", CC "(" DC0_Args ")" CLS, FN_PTR(Unsafe_DefineClass0)}, 1461 {CC "defineClass", CC "(" DC_Args ")" CLS, FN_PTR(Unsafe_DefineClass)}, 1462 {CC "allocateInstance", CC "(" CLS ")" OBJ, FN_PTR(Unsafe_AllocateInstance)}, 1463 {CC "monitorEnter", CC "(" OBJ ")V", FN_PTR(Unsafe_MonitorEnter)}, 1464 {CC "monitorExit", CC "(" OBJ ")V", FN_PTR(Unsafe_MonitorExit)}, 1465 {CC "throwException", CC "(" THR ")V", FN_PTR(Unsafe_ThrowException)} 1466 }; 1467 1468 // These are the methods prior to the JSR 166 changes in 1.5.0 1469 static JNINativeMethod methods_141[] = { 1470 {CC "getObject", CC "(" OBJ "J)" OBJ "", FN_PTR(Unsafe_GetObject)}, 1471 {CC "putObject", CC "(" OBJ "J" OBJ ")V", FN_PTR(Unsafe_SetObject)}, 1472 1473 DECLARE_GETSETOOP_141(Boolean, Z), 1474 DECLARE_GETSETOOP_141(Byte, B), 1475 DECLARE_GETSETOOP_141(Short, S), 1476 DECLARE_GETSETOOP_141(Char, C), 1477 DECLARE_GETSETOOP_141(Int, I), 1478 DECLARE_GETSETOOP_141(Long, J), 1479 DECLARE_GETSETOOP_141(Float, F), 1480 DECLARE_GETSETOOP_141(Double, D), 1481 1482 DECLARE_GETSETNATIVE(Byte, B), 1483 DECLARE_GETSETNATIVE(Short, S), 1484 DECLARE_GETSETNATIVE(Char, C), 1485 DECLARE_GETSETNATIVE(Int, I), 1486 DECLARE_GETSETNATIVE(Long, J), 1487 DECLARE_GETSETNATIVE(Float, F), 1488 DECLARE_GETSETNATIVE(Double, D), 1489 1490 {CC "getAddress", CC "(" ADR ")" ADR, FN_PTR(Unsafe_GetNativeAddress)}, 1491 {CC "putAddress", CC "(" ADR "" ADR ")V", FN_PTR(Unsafe_SetNativeAddress)}, 1492 1493 {CC "allocateMemory", CC "(J)" ADR, FN_PTR(Unsafe_AllocateMemory)}, 1494 {CC "reallocateMemory", CC "(" ADR "J)" ADR, FN_PTR(Unsafe_ReallocateMemory)}, 1495 {CC "freeMemory", CC "(" ADR ")V", FN_PTR(Unsafe_FreeMemory)}, 1496 1497 {CC "objectFieldOffset", CC "(" FLD ")J", FN_PTR(Unsafe_ObjectFieldOffset)}, 1498 {CC "staticFieldOffset", CC "(" FLD ")J", FN_PTR(Unsafe_StaticFieldOffset)}, 1499 {CC "staticFieldBase", CC "(" FLD ")" OBJ, FN_PTR(Unsafe_StaticFieldBaseFromField)}, 1500 {CC "ensureClassInitialized",CC "(" CLS ")V", FN_PTR(Unsafe_EnsureClassInitialized)}, 1501 {CC "arrayBaseOffset", CC "(" CLS ")I", FN_PTR(Unsafe_ArrayBaseOffset)}, 1502 {CC "arrayIndexScale", CC "(" CLS ")I", FN_PTR(Unsafe_ArrayIndexScale)}, 1503 {CC "addressSize", CC "()I", FN_PTR(Unsafe_AddressSize)}, 1504 {CC "pageSize", CC "()I", FN_PTR(Unsafe_PageSize)}, 1505 1506 {CC "defineClass", CC "(" DC0_Args ")" CLS, FN_PTR(Unsafe_DefineClass0)}, 1507 {CC "defineClass", CC "(" DC_Args ")" CLS, FN_PTR(Unsafe_DefineClass)}, 1508 {CC "allocateInstance", CC "(" CLS ")" OBJ, FN_PTR(Unsafe_AllocateInstance)}, 1509 {CC "monitorEnter", CC "(" OBJ ")V", FN_PTR(Unsafe_MonitorEnter)}, 1510 {CC "monitorExit", CC "(" OBJ ")V", FN_PTR(Unsafe_MonitorExit)}, 1511 {CC "throwException", CC "(" THR ")V", FN_PTR(Unsafe_ThrowException)} 1512 1513 }; 1514 1515 // These are the methods prior to the JSR 166 changes in 1.6.0 1516 static JNINativeMethod methods_15[] = { 1517 {CC "getObject", CC "(" OBJ "J)" OBJ "", FN_PTR(Unsafe_GetObject)}, 1518 {CC "putObject", CC "(" OBJ "J" OBJ ")V", FN_PTR(Unsafe_SetObject)}, 1519 {CC "getObjectVolatile",CC "(" OBJ "J)" OBJ "", FN_PTR(Unsafe_GetObjectVolatile)}, 1520 {CC "putObjectVolatile",CC "(" OBJ "J" OBJ ")V", FN_PTR(Unsafe_SetObjectVolatile)}, 1521 1522 1523 DECLARE_GETSETOOP(Boolean, Z), 1524 DECLARE_GETSETOOP(Byte, B), 1525 DECLARE_GETSETOOP(Short, S), 1526 DECLARE_GETSETOOP(Char, C), 1527 DECLARE_GETSETOOP(Int, I), 1528 DECLARE_GETSETOOP(Long, J), 1529 DECLARE_GETSETOOP(Float, F), 1530 DECLARE_GETSETOOP(Double, D), 1531 1532 DECLARE_GETSETNATIVE(Byte, B), 1533 DECLARE_GETSETNATIVE(Short, S), 1534 DECLARE_GETSETNATIVE(Char, C), 1535 DECLARE_GETSETNATIVE(Int, I), 1536 DECLARE_GETSETNATIVE(Long, J), 1537 DECLARE_GETSETNATIVE(Float, F), 1538 DECLARE_GETSETNATIVE(Double, D), 1539 1540 {CC "getAddress", CC "(" ADR ")" ADR, FN_PTR(Unsafe_GetNativeAddress)}, 1541 {CC "putAddress", CC "(" ADR "" ADR ")V", FN_PTR(Unsafe_SetNativeAddress)}, 1542 1543 {CC "allocateMemory", CC "(J)" ADR, FN_PTR(Unsafe_AllocateMemory)}, 1544 {CC "reallocateMemory", CC "(" ADR "J)" ADR, FN_PTR(Unsafe_ReallocateMemory)}, 1545 {CC "freeMemory", CC "(" ADR ")V", FN_PTR(Unsafe_FreeMemory)}, 1546 1547 {CC "objectFieldOffset", CC "(" FLD ")J", FN_PTR(Unsafe_ObjectFieldOffset)}, 1548 {CC "staticFieldOffset", CC "(" FLD ")J", FN_PTR(Unsafe_StaticFieldOffset)}, 1549 {CC "staticFieldBase", CC "(" FLD ")" OBJ, FN_PTR(Unsafe_StaticFieldBaseFromField)}, 1550 {CC "ensureClassInitialized",CC "(" CLS ")V", FN_PTR(Unsafe_EnsureClassInitialized)}, 1551 {CC "arrayBaseOffset", CC "(" CLS ")I", FN_PTR(Unsafe_ArrayBaseOffset)}, 1552 {CC "arrayIndexScale", CC "(" CLS ")I", FN_PTR(Unsafe_ArrayIndexScale)}, 1553 {CC "addressSize", CC "()I", FN_PTR(Unsafe_AddressSize)}, 1554 {CC "pageSize", CC "()I", FN_PTR(Unsafe_PageSize)}, 1555 1556 {CC "defineClass", CC "(" DC0_Args ")" CLS, FN_PTR(Unsafe_DefineClass0)}, 1557 {CC "defineClass", CC "(" DC_Args ")" CLS, FN_PTR(Unsafe_DefineClass)}, 1558 {CC "allocateInstance", CC "(" CLS ")" OBJ, FN_PTR(Unsafe_AllocateInstance)}, 1559 {CC "monitorEnter", CC "(" OBJ ")V", FN_PTR(Unsafe_MonitorEnter)}, 1560 {CC "monitorExit", CC "(" OBJ ")V", FN_PTR(Unsafe_MonitorExit)}, 1561 {CC "throwException", CC "(" THR ")V", FN_PTR(Unsafe_ThrowException)}, 1562 {CC "compareAndSwapObject", CC "(" OBJ "J" OBJ "" OBJ ")Z", FN_PTR(Unsafe_CompareAndSwapObject)}, 1563 {CC "compareAndSwapInt", CC "(" OBJ "J""I""I"")Z", FN_PTR(Unsafe_CompareAndSwapInt)}, 1564 {CC "compareAndSwapLong", CC "(" OBJ "J""J""J"")Z", FN_PTR(Unsafe_CompareAndSwapLong)}, 1565 {CC "park", CC "(ZJ)V", FN_PTR(Unsafe_Park)}, 1566 {CC "unpark", CC "(" OBJ ")V", FN_PTR(Unsafe_Unpark)} 1567 1568 }; 1569 1570 // These are the methods for 1.6.0 and 1.7.0 1571 static JNINativeMethod methods_16[] = { 1572 {CC "getObject", CC "(" OBJ "J)" OBJ "", FN_PTR(Unsafe_GetObject)}, 1573 {CC "putObject", CC "(" OBJ "J" OBJ ")V", FN_PTR(Unsafe_SetObject)}, 1574 {CC "getObjectVolatile",CC "(" OBJ "J)" OBJ "", FN_PTR(Unsafe_GetObjectVolatile)}, 1575 {CC "putObjectVolatile",CC "(" OBJ "J" OBJ ")V", FN_PTR(Unsafe_SetObjectVolatile)}, 1576 1577 DECLARE_GETSETOOP(Boolean, Z), 1578 DECLARE_GETSETOOP(Byte, B), 1579 DECLARE_GETSETOOP(Short, S), 1580 DECLARE_GETSETOOP(Char, C), 1581 DECLARE_GETSETOOP(Int, I), 1582 DECLARE_GETSETOOP(Long, J), 1583 DECLARE_GETSETOOP(Float, F), 1584 DECLARE_GETSETOOP(Double, D), 1585 1586 DECLARE_GETSETNATIVE(Byte, B), 1587 DECLARE_GETSETNATIVE(Short, S), 1588 DECLARE_GETSETNATIVE(Char, C), 1589 DECLARE_GETSETNATIVE(Int, I), 1590 DECLARE_GETSETNATIVE(Long, J), 1591 DECLARE_GETSETNATIVE(Float, F), 1592 DECLARE_GETSETNATIVE(Double, D), 1593 1594 {CC "getAddress", CC "(" ADR ")" ADR, FN_PTR(Unsafe_GetNativeAddress)}, 1595 {CC "putAddress", CC "(" ADR "" ADR ")V", FN_PTR(Unsafe_SetNativeAddress)}, 1596 1597 {CC "allocateMemory", CC "(J)" ADR, FN_PTR(Unsafe_AllocateMemory)}, 1598 {CC "reallocateMemory", CC "(" ADR "J)" ADR, FN_PTR(Unsafe_ReallocateMemory)}, 1599 {CC "freeMemory", CC "(" ADR ")V", FN_PTR(Unsafe_FreeMemory)}, 1600 1601 {CC "objectFieldOffset", CC "(" FLD ")J", FN_PTR(Unsafe_ObjectFieldOffset)}, 1602 {CC "staticFieldOffset", CC "(" FLD ")J", FN_PTR(Unsafe_StaticFieldOffset)}, 1603 {CC "staticFieldBase", CC "(" FLD ")" OBJ, FN_PTR(Unsafe_StaticFieldBaseFromField)}, 1604 {CC "ensureClassInitialized",CC "(" CLS ")V", FN_PTR(Unsafe_EnsureClassInitialized)}, 1605 {CC "arrayBaseOffset", CC "(" CLS ")I", FN_PTR(Unsafe_ArrayBaseOffset)}, 1606 {CC "arrayIndexScale", CC "(" CLS ")I", FN_PTR(Unsafe_ArrayIndexScale)}, 1607 {CC "addressSize", CC "()I", FN_PTR(Unsafe_AddressSize)}, 1608 {CC "pageSize", CC "()I", FN_PTR(Unsafe_PageSize)}, 1609 1610 {CC "defineClass", CC "(" DC0_Args ")" CLS, FN_PTR(Unsafe_DefineClass0)}, 1611 {CC "defineClass", CC "(" DC_Args ")" CLS, FN_PTR(Unsafe_DefineClass)}, 1612 {CC "allocateInstance", CC "(" CLS ")" OBJ, FN_PTR(Unsafe_AllocateInstance)}, 1613 {CC "monitorEnter", CC "(" OBJ ")V", FN_PTR(Unsafe_MonitorEnter)}, 1614 {CC "monitorExit", CC "(" OBJ ")V", FN_PTR(Unsafe_MonitorExit)}, 1615 {CC "tryMonitorEnter", CC "(" OBJ ")Z", FN_PTR(Unsafe_TryMonitorEnter)}, 1616 {CC "throwException", CC "(" THR ")V", FN_PTR(Unsafe_ThrowException)}, 1617 {CC "compareAndSwapObject", CC "(" OBJ "J" OBJ "" OBJ ")Z", FN_PTR(Unsafe_CompareAndSwapObject)}, 1618 {CC "compareAndSwapInt", CC "(" OBJ "J""I""I"")Z", FN_PTR(Unsafe_CompareAndSwapInt)}, 1619 {CC "compareAndSwapLong", CC "(" OBJ "J""J""J"")Z", FN_PTR(Unsafe_CompareAndSwapLong)}, 1620 {CC "putOrderedObject", CC "(" OBJ "J" OBJ ")V", FN_PTR(Unsafe_SetOrderedObject)}, 1621 {CC "putOrderedInt", CC "(" OBJ "JI)V", FN_PTR(Unsafe_SetOrderedInt)}, 1622 {CC "putOrderedLong", CC "(" OBJ "JJ)V", FN_PTR(Unsafe_SetOrderedLong)}, 1623 {CC "park", CC "(ZJ)V", FN_PTR(Unsafe_Park)}, 1624 {CC "unpark", CC "(" OBJ ")V", FN_PTR(Unsafe_Unpark)} 1625 }; 1626 1627 // These are the methods for 1.8.0 1628 static JNINativeMethod methods_18[] = { 1629 {CC "getObject", CC "(" OBJ "J)" OBJ "", FN_PTR(Unsafe_GetObject)}, 1630 {CC "putObject", CC "(" OBJ "J" OBJ ")V", FN_PTR(Unsafe_SetObject)}, 1631 {CC "getObjectVolatile",CC "(" OBJ "J)" OBJ "", FN_PTR(Unsafe_GetObjectVolatile)}, 1632 {CC "putObjectVolatile",CC "(" OBJ "J" OBJ ")V", FN_PTR(Unsafe_SetObjectVolatile)}, 1633 1634 DECLARE_GETSETOOP(Boolean, Z), 1635 DECLARE_GETSETOOP(Byte, B), 1636 DECLARE_GETSETOOP(Short, S), 1637 DECLARE_GETSETOOP(Char, C), 1638 DECLARE_GETSETOOP(Int, I), 1639 DECLARE_GETSETOOP(Long, J), 1640 DECLARE_GETSETOOP(Float, F), 1641 DECLARE_GETSETOOP(Double, D), 1642 1643 DECLARE_GETSETNATIVE(Byte, B), 1644 DECLARE_GETSETNATIVE(Short, S), 1645 DECLARE_GETSETNATIVE(Char, C), 1646 DECLARE_GETSETNATIVE(Int, I), 1647 DECLARE_GETSETNATIVE(Long, J), 1648 DECLARE_GETSETNATIVE(Float, F), 1649 DECLARE_GETSETNATIVE(Double, D), 1650 1651 {CC "getAddress", CC "(" ADR ")" ADR, FN_PTR(Unsafe_GetNativeAddress)}, 1652 {CC "putAddress", CC "(" ADR "" ADR ")V", FN_PTR(Unsafe_SetNativeAddress)}, 1653 1654 {CC "allocateMemory", CC "(J)" ADR, FN_PTR(Unsafe_AllocateMemory)}, 1655 {CC "reallocateMemory", CC "(" ADR "J)" ADR, FN_PTR(Unsafe_ReallocateMemory)}, 1656 {CC "freeMemory", CC "(" ADR ")V", FN_PTR(Unsafe_FreeMemory)}, 1657 1658 {CC "objectFieldOffset", CC "(" FLD ")J", FN_PTR(Unsafe_ObjectFieldOffset)}, 1659 {CC "staticFieldOffset", CC "(" FLD ")J", FN_PTR(Unsafe_StaticFieldOffset)}, 1660 {CC "staticFieldBase", CC "(" FLD ")" OBJ, FN_PTR(Unsafe_StaticFieldBaseFromField)}, 1661 {CC "ensureClassInitialized",CC "(" CLS ")V", FN_PTR(Unsafe_EnsureClassInitialized)}, 1662 {CC "arrayBaseOffset", CC "(" CLS ")I", FN_PTR(Unsafe_ArrayBaseOffset)}, 1663 {CC "arrayIndexScale", CC "(" CLS ")I", FN_PTR(Unsafe_ArrayIndexScale)}, 1664 {CC "addressSize", CC "()I", FN_PTR(Unsafe_AddressSize)}, 1665 {CC "pageSize", CC "()I", FN_PTR(Unsafe_PageSize)}, 1666 1667 {CC "defineClass", CC "(" DC_Args ")" CLS, FN_PTR(Unsafe_DefineClass)}, 1668 {CC "allocateInstance", CC "(" CLS ")" OBJ, FN_PTR(Unsafe_AllocateInstance)}, 1669 {CC "monitorEnter", CC "(" OBJ ")V", FN_PTR(Unsafe_MonitorEnter)}, 1670 {CC "monitorExit", CC "(" OBJ ")V", FN_PTR(Unsafe_MonitorExit)}, 1671 {CC "tryMonitorEnter", CC "(" OBJ ")Z", FN_PTR(Unsafe_TryMonitorEnter)}, 1672 {CC "throwException", CC "(" THR ")V", FN_PTR(Unsafe_ThrowException)}, 1673 {CC "compareAndSwapObject", CC "(" OBJ "J" OBJ "" OBJ ")Z", FN_PTR(Unsafe_CompareAndSwapObject)}, 1674 {CC "compareAndSwapInt", CC "(" OBJ "J""I""I"")Z", FN_PTR(Unsafe_CompareAndSwapInt)}, 1675 {CC "compareAndSwapLong", CC "(" OBJ "J""J""J"")Z", FN_PTR(Unsafe_CompareAndSwapLong)}, 1676 {CC "putOrderedObject", CC "(" OBJ "J" OBJ ")V", FN_PTR(Unsafe_SetOrderedObject)}, 1677 {CC "putOrderedInt", CC "(" OBJ "JI)V", FN_PTR(Unsafe_SetOrderedInt)}, 1678 {CC "putOrderedLong", CC "(" OBJ "JJ)V", FN_PTR(Unsafe_SetOrderedLong)}, 1679 {CC "park", CC "(ZJ)V", FN_PTR(Unsafe_Park)}, 1680 {CC "unpark", CC "(" OBJ ")V", FN_PTR(Unsafe_Unpark)} 1681 }; 1682 1683 JNINativeMethod loadavg_method[] = { 1684 {CC "getLoadAverage", CC "([DI)I", FN_PTR(Unsafe_Loadavg)} 1685 }; 1686 1687 JNINativeMethod prefetch_methods[] = { 1688 {CC "prefetchRead", CC "(" OBJ "J)V", FN_PTR(Unsafe_PrefetchRead)}, 1689 {CC "prefetchWrite", CC "(" OBJ "J)V", FN_PTR(Unsafe_PrefetchWrite)}, 1690 {CC "prefetchReadStatic", CC "(" OBJ "J)V", FN_PTR(Unsafe_PrefetchRead)}, 1691 {CC "prefetchWriteStatic",CC "(" OBJ "J)V", FN_PTR(Unsafe_PrefetchWrite)} 1692 }; 1693 1694 JNINativeMethod memcopy_methods_17[] = { 1695 {CC "copyMemory", CC "(" OBJ "J" OBJ "JJ)V", FN_PTR(Unsafe_CopyMemory2)}, 1696 {CC "setMemory", CC "(" OBJ "JJB)V", FN_PTR(Unsafe_SetMemory2)} 1697 }; 1698 1699 JNINativeMethod memcopy_methods_15[] = { 1700 {CC "setMemory", CC "(" ADR "JB)V", FN_PTR(Unsafe_SetMemory)}, 1701 {CC "copyMemory", CC "(" ADR ADR "J)V", FN_PTR(Unsafe_CopyMemory)} 1702 }; 1703 1704 JNINativeMethod anonk_methods[] = { 1705 {CC "defineAnonymousClass", CC "(" DAC_Args ")" CLS, FN_PTR(Unsafe_DefineAnonymousClass)}, 1706 }; 1707 1708 JNINativeMethod lform_methods[] = { 1709 {CC "shouldBeInitialized",CC "(" CLS ")Z", FN_PTR(Unsafe_ShouldBeInitialized)}, 1710 }; 1711 1712 JNINativeMethod fence_methods[] = { 1713 {CC "loadFence", CC "()V", FN_PTR(Unsafe_LoadFence)}, 1714 {CC "storeFence", CC "()V", FN_PTR(Unsafe_StoreFence)}, 1715 {CC "fullFence", CC "()V", FN_PTR(Unsafe_FullFence)}, 1716 }; 1717 1718 #undef CC 1719 #undef FN_PTR 1720 1721 #undef ADR 1722 #undef LANG 1723 #undef OBJ 1724 #undef CLS 1725 #undef CTR 1726 #undef FLD 1727 #undef MTH 1728 #undef THR 1729 #undef DC0_Args 1730 #undef DC_Args 1731 1732 #undef DECLARE_GETSETOOP 1733 #undef DECLARE_GETSETNATIVE 1734 1735 1736 /** 1737 * Helper method to register native methods. 1738 */ 1739 static bool register_natives(const char* message, JNIEnv* env, jclass clazz, const JNINativeMethod* methods, jint nMethods) { 1740 int status = env->RegisterNatives(clazz, methods, nMethods); 1741 if (status < 0 || env->ExceptionOccurred()) { 1742 if (PrintMiscellaneous && (Verbose || WizardMode)) { 1743 tty->print_cr("Unsafe: failed registering %s", message); 1744 } 1745 env->ExceptionClear(); 1746 return false; 1747 } else { 1748 if (PrintMiscellaneous && (Verbose || WizardMode)) { 1749 tty->print_cr("Unsafe: successfully registered %s", message); 1750 } 1751 return true; 1752 } 1753 } 1754 1755 1756 // This one function is exported, used by NativeLookup. 1757 // The Unsafe_xxx functions above are called only from the interpreter. 1758 // The optimizer looks at names and signatures to recognize 1759 // individual functions. 1760 1761 JVM_ENTRY(void, JVM_RegisterUnsafeMethods(JNIEnv *env, jclass unsafecls)) 1762 UnsafeWrapper("JVM_RegisterUnsafeMethods"); 1763 { 1764 ThreadToNativeFromVM ttnfv(thread); 1765 1766 // Unsafe methods 1767 { 1768 bool success = false; 1769 // We need to register the 1.6 methods first because the 1.8 methods would register fine on 1.7 and 1.6 1770 if (!success) { 1771 success = register_natives("1.6 methods", env, unsafecls, methods_16, sizeof(methods_16)/sizeof(JNINativeMethod)); 1772 } 1773 if (!success) { 1774 success = register_natives("1.8 methods", env, unsafecls, methods_18, sizeof(methods_18)/sizeof(JNINativeMethod)); 1775 } 1776 if (!success) { 1777 success = register_natives("1.5 methods", env, unsafecls, methods_15, sizeof(methods_15)/sizeof(JNINativeMethod)); 1778 } 1779 if (!success) { 1780 success = register_natives("1.4.1 methods", env, unsafecls, methods_141, sizeof(methods_141)/sizeof(JNINativeMethod)); 1781 } 1782 if (!success) { 1783 success = register_natives("1.4.0 methods", env, unsafecls, methods_140, sizeof(methods_140)/sizeof(JNINativeMethod)); 1784 } 1785 guarantee(success, "register unsafe natives"); 1786 } 1787 1788 // Unsafe.getLoadAverage 1789 register_natives("1.6 loadavg method", env, unsafecls, loadavg_method, sizeof(loadavg_method)/sizeof(JNINativeMethod)); 1790 1791 // Prefetch methods 1792 register_natives("1.6 prefetch methods", env, unsafecls, prefetch_methods, sizeof(prefetch_methods)/sizeof(JNINativeMethod)); 1793 1794 // Memory copy methods 1795 { 1796 bool success = false; 1797 if (!success) { 1798 success = register_natives("1.7 memory copy methods", env, unsafecls, memcopy_methods_17, sizeof(memcopy_methods_17)/sizeof(JNINativeMethod)); 1799 } 1800 if (!success) { 1801 success = register_natives("1.5 memory copy methods", env, unsafecls, memcopy_methods_15, sizeof(memcopy_methods_15)/sizeof(JNINativeMethod)); 1802 } 1803 } 1804 1805 // Unsafe.defineAnonymousClass 1806 if (EnableInvokeDynamic) { 1807 register_natives("1.7 define anonymous class method", env, unsafecls, anonk_methods, sizeof(anonk_methods)/sizeof(JNINativeMethod)); 1808 } 1809 1810 // Unsafe.shouldBeInitialized 1811 if (EnableInvokeDynamic) { 1812 register_natives("1.7 LambdaForm support", env, unsafecls, lform_methods, sizeof(lform_methods)/sizeof(JNINativeMethod)); 1813 } 1814 1815 // Fence methods 1816 register_natives("1.8 fence methods", env, unsafecls, fence_methods, sizeof(fence_methods)/sizeof(JNINativeMethod)); 1817 } 1818 JVM_END