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