1 /* 2 * Copyright (c) 2000, 2015, 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_ThrowException(JNIEnv *env, jobject unsafe, jthrowable thr)) 1108 UnsafeWrapper("Unsafe_ThrowException"); 1109 { 1110 ThreadToNativeFromVM ttnfv(thread); 1111 env->Throw(thr); 1112 } 1113 UNSAFE_END 1114 1115 // JSR166 ------------------------------------------------------------------ 1116 1117 UNSAFE_ENTRY(jboolean, Unsafe_CompareAndSwapObject(JNIEnv *env, jobject unsafe, jobject obj, jlong offset, jobject e_h, jobject x_h)) 1118 UnsafeWrapper("Unsafe_CompareAndSwapObject"); 1119 oop x = JNIHandles::resolve(x_h); 1120 oop e = JNIHandles::resolve(e_h); 1121 oop p = JNIHandles::resolve(obj); 1122 HeapWord* addr = (HeapWord *)index_oop_from_field_offset_long(p, offset); 1123 oop res = oopDesc::atomic_compare_exchange_oop(x, addr, e, true); 1124 jboolean success = (res == e); 1125 if (success) 1126 update_barrier_set((void*)addr, x); 1127 return success; 1128 UNSAFE_END 1129 1130 UNSAFE_ENTRY(jboolean, Unsafe_CompareAndSwapInt(JNIEnv *env, jobject unsafe, jobject obj, jlong offset, jint e, jint x)) 1131 UnsafeWrapper("Unsafe_CompareAndSwapInt"); 1132 oop p = JNIHandles::resolve(obj); 1133 jint* addr = (jint *) index_oop_from_field_offset_long(p, offset); 1134 return (jint)(Atomic::cmpxchg(x, addr, e)) == e; 1135 UNSAFE_END 1136 1137 UNSAFE_ENTRY(jboolean, Unsafe_CompareAndSwapLong(JNIEnv *env, jobject unsafe, jobject obj, jlong offset, jlong e, jlong x)) 1138 UnsafeWrapper("Unsafe_CompareAndSwapLong"); 1139 Handle p (THREAD, JNIHandles::resolve(obj)); 1140 jlong* addr = (jlong*)(index_oop_from_field_offset_long(p(), offset)); 1141 #ifdef SUPPORTS_NATIVE_CX8 1142 return (jlong)(Atomic::cmpxchg(x, addr, e)) == e; 1143 #else 1144 if (VM_Version::supports_cx8()) 1145 return (jlong)(Atomic::cmpxchg(x, addr, e)) == e; 1146 else { 1147 jboolean success = false; 1148 MutexLockerEx mu(UnsafeJlong_lock, Mutex::_no_safepoint_check_flag); 1149 jlong val = Atomic::load(addr); 1150 if (val == e) { Atomic::store(x, addr); success = true; } 1151 return success; 1152 } 1153 #endif 1154 UNSAFE_END 1155 1156 UNSAFE_ENTRY(void, Unsafe_Park(JNIEnv *env, jobject unsafe, jboolean isAbsolute, jlong time)) 1157 UnsafeWrapper("Unsafe_Park"); 1158 EventThreadPark event; 1159 HOTSPOT_THREAD_PARK_BEGIN((uintptr_t) thread->parker(), (int) isAbsolute, time); 1160 1161 JavaThreadParkedState jtps(thread, time != 0); 1162 thread->parker()->park(isAbsolute != 0, time); 1163 1164 HOTSPOT_THREAD_PARK_END((uintptr_t) thread->parker()); 1165 if (event.should_commit()) { 1166 oop obj = thread->current_park_blocker(); 1167 event.set_klass((obj != NULL) ? obj->klass() : NULL); 1168 event.set_timeout(time); 1169 event.set_address((obj != NULL) ? (TYPE_ADDRESS) cast_from_oop<uintptr_t>(obj) : 0); 1170 event.commit(); 1171 } 1172 UNSAFE_END 1173 1174 UNSAFE_ENTRY(void, Unsafe_Unpark(JNIEnv *env, jobject unsafe, jobject jthread)) 1175 UnsafeWrapper("Unsafe_Unpark"); 1176 Parker* p = NULL; 1177 if (jthread != NULL) { 1178 oop java_thread = JNIHandles::resolve_non_null(jthread); 1179 if (java_thread != NULL) { 1180 jlong lp = java_lang_Thread::park_event(java_thread); 1181 if (lp != 0) { 1182 // This cast is OK even though the jlong might have been read 1183 // non-atomically on 32bit systems, since there, one word will 1184 // always be zero anyway and the value set is always the same 1185 p = (Parker*)addr_from_java(lp); 1186 } else { 1187 // Grab lock if apparently null or using older version of library 1188 MutexLocker mu(Threads_lock); 1189 java_thread = JNIHandles::resolve_non_null(jthread); 1190 if (java_thread != NULL) { 1191 JavaThread* thr = java_lang_Thread::thread(java_thread); 1192 if (thr != NULL) { 1193 p = thr->parker(); 1194 if (p != NULL) { // Bind to Java thread for next time. 1195 java_lang_Thread::set_park_event(java_thread, addr_to_java(p)); 1196 } 1197 } 1198 } 1199 } 1200 } 1201 } 1202 if (p != NULL) { 1203 HOTSPOT_THREAD_UNPARK((uintptr_t) p); 1204 p->unpark(); 1205 } 1206 UNSAFE_END 1207 1208 UNSAFE_ENTRY(jint, Unsafe_Loadavg(JNIEnv *env, jobject unsafe, jdoubleArray loadavg, jint nelem)) 1209 UnsafeWrapper("Unsafe_Loadavg"); 1210 const int max_nelem = 3; 1211 double la[max_nelem]; 1212 jint ret; 1213 1214 typeArrayOop a = typeArrayOop(JNIHandles::resolve_non_null(loadavg)); 1215 assert(a->is_typeArray(), "must be type array"); 1216 1217 if (nelem < 0 || nelem > max_nelem || a->length() < nelem) { 1218 ThreadToNativeFromVM ttnfv(thread); 1219 throw_new(env, "ArrayIndexOutOfBoundsException"); 1220 return -1; 1221 } 1222 1223 ret = os::loadavg(la, nelem); 1224 if (ret == -1) return -1; 1225 1226 // if successful, ret is the number of samples actually retrieved. 1227 assert(ret >= 0 && ret <= max_nelem, "Unexpected loadavg return value"); 1228 switch(ret) { 1229 case 3: a->double_at_put(2, (jdouble)la[2]); // fall through 1230 case 2: a->double_at_put(1, (jdouble)la[1]); // fall through 1231 case 1: a->double_at_put(0, (jdouble)la[0]); break; 1232 } 1233 return ret; 1234 UNSAFE_END 1235 1236 UNSAFE_ENTRY(void, Unsafe_PrefetchRead(JNIEnv* env, jclass ignored, jobject obj, jlong offset)) 1237 UnsafeWrapper("Unsafe_PrefetchRead"); 1238 oop p = JNIHandles::resolve(obj); 1239 void* addr = index_oop_from_field_offset_long(p, 0); 1240 Prefetch::read(addr, (intx)offset); 1241 UNSAFE_END 1242 1243 UNSAFE_ENTRY(void, Unsafe_PrefetchWrite(JNIEnv* env, jclass ignored, jobject obj, jlong offset)) 1244 UnsafeWrapper("Unsafe_PrefetchWrite"); 1245 oop p = JNIHandles::resolve(obj); 1246 void* addr = index_oop_from_field_offset_long(p, 0); 1247 Prefetch::write(addr, (intx)offset); 1248 UNSAFE_END 1249 1250 1251 /// JVM_RegisterUnsafeMethods 1252 1253 #define ADR "J" 1254 1255 #define LANG "Ljava/lang/" 1256 1257 #define OBJ LANG"Object;" 1258 #define CLS LANG"Class;" 1259 #define CTR LANG"reflect/Constructor;" 1260 #define FLD LANG"reflect/Field;" 1261 #define MTH LANG"reflect/Method;" 1262 #define THR LANG"Throwable;" 1263 1264 #define DC0_Args LANG"String;[BII" 1265 #define DC_Args DC0_Args LANG"ClassLoader;" "Ljava/security/ProtectionDomain;" 1266 1267 #define CC (char*) /*cast a literal from (const char*)*/ 1268 #define FN_PTR(f) CAST_FROM_FN_PTR(void*, &f) 1269 1270 // define deprecated accessors for compabitility with 1.4.0 1271 #define DECLARE_GETSETOOP_140(Boolean, Z) \ 1272 {CC"get"#Boolean, CC"("OBJ"I)"#Z, FN_PTR(Unsafe_Get##Boolean##140)}, \ 1273 {CC"put"#Boolean, CC"("OBJ"I"#Z")V", FN_PTR(Unsafe_Set##Boolean##140)} 1274 1275 // Note: In 1.4.1, getObject and kin take both int and long offsets. 1276 #define DECLARE_GETSETOOP_141(Boolean, Z) \ 1277 {CC"get"#Boolean, CC"("OBJ"J)"#Z, FN_PTR(Unsafe_Get##Boolean)}, \ 1278 {CC"put"#Boolean, CC"("OBJ"J"#Z")V", FN_PTR(Unsafe_Set##Boolean)} 1279 1280 // Note: In 1.5.0, there are volatile versions too 1281 #define DECLARE_GETSETOOP(Boolean, Z) \ 1282 {CC"get"#Boolean, CC"("OBJ"J)"#Z, FN_PTR(Unsafe_Get##Boolean)}, \ 1283 {CC"put"#Boolean, CC"("OBJ"J"#Z")V", FN_PTR(Unsafe_Set##Boolean)}, \ 1284 {CC"get"#Boolean"Volatile", CC"("OBJ"J)"#Z, FN_PTR(Unsafe_Get##Boolean##Volatile)}, \ 1285 {CC"put"#Boolean"Volatile", CC"("OBJ"J"#Z")V", FN_PTR(Unsafe_Set##Boolean##Volatile)} 1286 1287 1288 #define DECLARE_GETSETNATIVE(Byte, B) \ 1289 {CC"get"#Byte, CC"("ADR")"#B, FN_PTR(Unsafe_GetNative##Byte)}, \ 1290 {CC"put"#Byte, CC"("ADR#B")V", FN_PTR(Unsafe_SetNative##Byte)} 1291 1292 1293 1294 // These are the methods for 1.4.0 1295 static JNINativeMethod methods_140[] = { 1296 DECLARE_GETSETOOP_140(Boolean, Z), 1297 DECLARE_GETSETOOP_140(Byte, B), 1298 DECLARE_GETSETOOP_140(Short, S), 1299 DECLARE_GETSETOOP_140(Char, C), 1300 DECLARE_GETSETOOP_140(Int, I), 1301 DECLARE_GETSETOOP_140(Long, J), 1302 DECLARE_GETSETOOP_140(Float, F), 1303 DECLARE_GETSETOOP_140(Double, D), 1304 1305 DECLARE_GETSETNATIVE(Byte, B), 1306 DECLARE_GETSETNATIVE(Short, S), 1307 DECLARE_GETSETNATIVE(Char, C), 1308 DECLARE_GETSETNATIVE(Int, I), 1309 DECLARE_GETSETNATIVE(Long, J), 1310 DECLARE_GETSETNATIVE(Float, F), 1311 DECLARE_GETSETNATIVE(Double, D), 1312 1313 {CC"getAddress", CC"("ADR")"ADR, FN_PTR(Unsafe_GetNativeAddress)}, 1314 {CC"putAddress", CC"("ADR""ADR")V", FN_PTR(Unsafe_SetNativeAddress)}, 1315 1316 {CC"allocateMemory", CC"(J)"ADR, FN_PTR(Unsafe_AllocateMemory)}, 1317 {CC"reallocateMemory", CC"("ADR"J)"ADR, FN_PTR(Unsafe_ReallocateMemory)}, 1318 {CC"freeMemory", CC"("ADR")V", FN_PTR(Unsafe_FreeMemory)}, 1319 1320 {CC"fieldOffset", CC"("FLD")I", FN_PTR(Unsafe_FieldOffset)}, 1321 {CC"staticFieldBase", CC"("CLS")"OBJ, FN_PTR(Unsafe_StaticFieldBaseFromClass)}, 1322 {CC"ensureClassInitialized",CC"("CLS")V", FN_PTR(Unsafe_EnsureClassInitialized)}, 1323 {CC"arrayBaseOffset", CC"("CLS")I", FN_PTR(Unsafe_ArrayBaseOffset)}, 1324 {CC"arrayIndexScale", CC"("CLS")I", FN_PTR(Unsafe_ArrayIndexScale)}, 1325 {CC"addressSize", CC"()I", FN_PTR(Unsafe_AddressSize)}, 1326 {CC"pageSize", CC"()I", FN_PTR(Unsafe_PageSize)}, 1327 1328 {CC"defineClass", CC"("DC0_Args")"CLS, FN_PTR(Unsafe_DefineClass0)}, 1329 {CC"defineClass", CC"("DC_Args")"CLS, FN_PTR(Unsafe_DefineClass)}, 1330 {CC"allocateInstance", CC"("CLS")"OBJ, FN_PTR(Unsafe_AllocateInstance)}, 1331 {CC"throwException", CC"("THR")V", FN_PTR(Unsafe_ThrowException)} 1332 }; 1333 1334 // These are the methods prior to the JSR 166 changes in 1.5.0 1335 static JNINativeMethod methods_141[] = { 1336 {CC"getObject", CC"("OBJ"J)"OBJ"", FN_PTR(Unsafe_GetObject)}, 1337 {CC"putObject", CC"("OBJ"J"OBJ")V", FN_PTR(Unsafe_SetObject)}, 1338 1339 DECLARE_GETSETOOP_141(Boolean, Z), 1340 DECLARE_GETSETOOP_141(Byte, B), 1341 DECLARE_GETSETOOP_141(Short, S), 1342 DECLARE_GETSETOOP_141(Char, C), 1343 DECLARE_GETSETOOP_141(Int, I), 1344 DECLARE_GETSETOOP_141(Long, J), 1345 DECLARE_GETSETOOP_141(Float, F), 1346 DECLARE_GETSETOOP_141(Double, D), 1347 1348 DECLARE_GETSETNATIVE(Byte, B), 1349 DECLARE_GETSETNATIVE(Short, S), 1350 DECLARE_GETSETNATIVE(Char, C), 1351 DECLARE_GETSETNATIVE(Int, I), 1352 DECLARE_GETSETNATIVE(Long, J), 1353 DECLARE_GETSETNATIVE(Float, F), 1354 DECLARE_GETSETNATIVE(Double, D), 1355 1356 {CC"getAddress", CC"("ADR")"ADR, FN_PTR(Unsafe_GetNativeAddress)}, 1357 {CC"putAddress", CC"("ADR""ADR")V", FN_PTR(Unsafe_SetNativeAddress)}, 1358 1359 {CC"allocateMemory", CC"(J)"ADR, FN_PTR(Unsafe_AllocateMemory)}, 1360 {CC"reallocateMemory", CC"("ADR"J)"ADR, FN_PTR(Unsafe_ReallocateMemory)}, 1361 {CC"freeMemory", CC"("ADR")V", FN_PTR(Unsafe_FreeMemory)}, 1362 1363 {CC"objectFieldOffset", CC"("FLD")J", FN_PTR(Unsafe_ObjectFieldOffset)}, 1364 {CC"staticFieldOffset", CC"("FLD")J", FN_PTR(Unsafe_StaticFieldOffset)}, 1365 {CC"staticFieldBase", CC"("FLD")"OBJ, FN_PTR(Unsafe_StaticFieldBaseFromField)}, 1366 {CC"ensureClassInitialized",CC"("CLS")V", FN_PTR(Unsafe_EnsureClassInitialized)}, 1367 {CC"arrayBaseOffset", CC"("CLS")I", FN_PTR(Unsafe_ArrayBaseOffset)}, 1368 {CC"arrayIndexScale", CC"("CLS")I", FN_PTR(Unsafe_ArrayIndexScale)}, 1369 {CC"addressSize", CC"()I", FN_PTR(Unsafe_AddressSize)}, 1370 {CC"pageSize", CC"()I", FN_PTR(Unsafe_PageSize)}, 1371 1372 {CC"defineClass", CC"("DC0_Args")"CLS, FN_PTR(Unsafe_DefineClass0)}, 1373 {CC"defineClass", CC"("DC_Args")"CLS, FN_PTR(Unsafe_DefineClass)}, 1374 {CC"allocateInstance", CC"("CLS")"OBJ, FN_PTR(Unsafe_AllocateInstance)}, 1375 {CC"throwException", CC"("THR")V", FN_PTR(Unsafe_ThrowException)} 1376 1377 }; 1378 1379 // These are the methods prior to the JSR 166 changes in 1.6.0 1380 static JNINativeMethod methods_15[] = { 1381 {CC"getObject", CC"("OBJ"J)"OBJ"", FN_PTR(Unsafe_GetObject)}, 1382 {CC"putObject", CC"("OBJ"J"OBJ")V", FN_PTR(Unsafe_SetObject)}, 1383 {CC"getObjectVolatile",CC"("OBJ"J)"OBJ"", FN_PTR(Unsafe_GetObjectVolatile)}, 1384 {CC"putObjectVolatile",CC"("OBJ"J"OBJ")V", FN_PTR(Unsafe_SetObjectVolatile)}, 1385 1386 1387 DECLARE_GETSETOOP(Boolean, Z), 1388 DECLARE_GETSETOOP(Byte, B), 1389 DECLARE_GETSETOOP(Short, S), 1390 DECLARE_GETSETOOP(Char, C), 1391 DECLARE_GETSETOOP(Int, I), 1392 DECLARE_GETSETOOP(Long, J), 1393 DECLARE_GETSETOOP(Float, F), 1394 DECLARE_GETSETOOP(Double, D), 1395 1396 DECLARE_GETSETNATIVE(Byte, B), 1397 DECLARE_GETSETNATIVE(Short, S), 1398 DECLARE_GETSETNATIVE(Char, C), 1399 DECLARE_GETSETNATIVE(Int, I), 1400 DECLARE_GETSETNATIVE(Long, J), 1401 DECLARE_GETSETNATIVE(Float, F), 1402 DECLARE_GETSETNATIVE(Double, D), 1403 1404 {CC"getAddress", CC"("ADR")"ADR, FN_PTR(Unsafe_GetNativeAddress)}, 1405 {CC"putAddress", CC"("ADR""ADR")V", FN_PTR(Unsafe_SetNativeAddress)}, 1406 1407 {CC"allocateMemory", CC"(J)"ADR, FN_PTR(Unsafe_AllocateMemory)}, 1408 {CC"reallocateMemory", CC"("ADR"J)"ADR, FN_PTR(Unsafe_ReallocateMemory)}, 1409 {CC"freeMemory", CC"("ADR")V", FN_PTR(Unsafe_FreeMemory)}, 1410 1411 {CC"objectFieldOffset", CC"("FLD")J", FN_PTR(Unsafe_ObjectFieldOffset)}, 1412 {CC"staticFieldOffset", CC"("FLD")J", FN_PTR(Unsafe_StaticFieldOffset)}, 1413 {CC"staticFieldBase", CC"("FLD")"OBJ, FN_PTR(Unsafe_StaticFieldBaseFromField)}, 1414 {CC"ensureClassInitialized",CC"("CLS")V", FN_PTR(Unsafe_EnsureClassInitialized)}, 1415 {CC"arrayBaseOffset", CC"("CLS")I", FN_PTR(Unsafe_ArrayBaseOffset)}, 1416 {CC"arrayIndexScale", CC"("CLS")I", FN_PTR(Unsafe_ArrayIndexScale)}, 1417 {CC"addressSize", CC"()I", FN_PTR(Unsafe_AddressSize)}, 1418 {CC"pageSize", CC"()I", FN_PTR(Unsafe_PageSize)}, 1419 1420 {CC"defineClass", CC"("DC0_Args")"CLS, FN_PTR(Unsafe_DefineClass0)}, 1421 {CC"defineClass", CC"("DC_Args")"CLS, FN_PTR(Unsafe_DefineClass)}, 1422 {CC"allocateInstance", CC"("CLS")"OBJ, FN_PTR(Unsafe_AllocateInstance)}, 1423 {CC"throwException", CC"("THR")V", FN_PTR(Unsafe_ThrowException)}, 1424 {CC"compareAndSwapObject", CC"("OBJ"J"OBJ""OBJ")Z", FN_PTR(Unsafe_CompareAndSwapObject)}, 1425 {CC"compareAndSwapInt", CC"("OBJ"J""I""I"")Z", FN_PTR(Unsafe_CompareAndSwapInt)}, 1426 {CC"compareAndSwapLong", CC"("OBJ"J""J""J"")Z", FN_PTR(Unsafe_CompareAndSwapLong)}, 1427 {CC"park", CC"(ZJ)V", FN_PTR(Unsafe_Park)}, 1428 {CC"unpark", CC"("OBJ")V", FN_PTR(Unsafe_Unpark)} 1429 1430 }; 1431 1432 // These are the methods for 1.6.0 and 1.7.0 1433 static JNINativeMethod methods_16[] = { 1434 {CC"getObject", CC"("OBJ"J)"OBJ"", FN_PTR(Unsafe_GetObject)}, 1435 {CC"putObject", CC"("OBJ"J"OBJ")V", FN_PTR(Unsafe_SetObject)}, 1436 {CC"getObjectVolatile",CC"("OBJ"J)"OBJ"", FN_PTR(Unsafe_GetObjectVolatile)}, 1437 {CC"putObjectVolatile",CC"("OBJ"J"OBJ")V", FN_PTR(Unsafe_SetObjectVolatile)}, 1438 1439 DECLARE_GETSETOOP(Boolean, Z), 1440 DECLARE_GETSETOOP(Byte, B), 1441 DECLARE_GETSETOOP(Short, S), 1442 DECLARE_GETSETOOP(Char, C), 1443 DECLARE_GETSETOOP(Int, I), 1444 DECLARE_GETSETOOP(Long, J), 1445 DECLARE_GETSETOOP(Float, F), 1446 DECLARE_GETSETOOP(Double, D), 1447 1448 DECLARE_GETSETNATIVE(Byte, B), 1449 DECLARE_GETSETNATIVE(Short, S), 1450 DECLARE_GETSETNATIVE(Char, C), 1451 DECLARE_GETSETNATIVE(Int, I), 1452 DECLARE_GETSETNATIVE(Long, J), 1453 DECLARE_GETSETNATIVE(Float, F), 1454 DECLARE_GETSETNATIVE(Double, D), 1455 1456 {CC"getAddress", CC"("ADR")"ADR, FN_PTR(Unsafe_GetNativeAddress)}, 1457 {CC"putAddress", CC"("ADR""ADR")V", FN_PTR(Unsafe_SetNativeAddress)}, 1458 1459 {CC"allocateMemory", CC"(J)"ADR, FN_PTR(Unsafe_AllocateMemory)}, 1460 {CC"reallocateMemory", CC"("ADR"J)"ADR, FN_PTR(Unsafe_ReallocateMemory)}, 1461 {CC"freeMemory", CC"("ADR")V", FN_PTR(Unsafe_FreeMemory)}, 1462 1463 {CC"objectFieldOffset", CC"("FLD")J", FN_PTR(Unsafe_ObjectFieldOffset)}, 1464 {CC"staticFieldOffset", CC"("FLD")J", FN_PTR(Unsafe_StaticFieldOffset)}, 1465 {CC"staticFieldBase", CC"("FLD")"OBJ, FN_PTR(Unsafe_StaticFieldBaseFromField)}, 1466 {CC"ensureClassInitialized",CC"("CLS")V", FN_PTR(Unsafe_EnsureClassInitialized)}, 1467 {CC"arrayBaseOffset", CC"("CLS")I", FN_PTR(Unsafe_ArrayBaseOffset)}, 1468 {CC"arrayIndexScale", CC"("CLS")I", FN_PTR(Unsafe_ArrayIndexScale)}, 1469 {CC"addressSize", CC"()I", FN_PTR(Unsafe_AddressSize)}, 1470 {CC"pageSize", CC"()I", FN_PTR(Unsafe_PageSize)}, 1471 1472 {CC"defineClass", CC"("DC0_Args")"CLS, FN_PTR(Unsafe_DefineClass0)}, 1473 {CC"defineClass", CC"("DC_Args")"CLS, FN_PTR(Unsafe_DefineClass)}, 1474 {CC"allocateInstance", CC"("CLS")"OBJ, FN_PTR(Unsafe_AllocateInstance)}, 1475 {CC"throwException", CC"("THR")V", FN_PTR(Unsafe_ThrowException)}, 1476 {CC"compareAndSwapObject", CC"("OBJ"J"OBJ""OBJ")Z", FN_PTR(Unsafe_CompareAndSwapObject)}, 1477 {CC"compareAndSwapInt", CC"("OBJ"J""I""I"")Z", FN_PTR(Unsafe_CompareAndSwapInt)}, 1478 {CC"compareAndSwapLong", CC"("OBJ"J""J""J"")Z", FN_PTR(Unsafe_CompareAndSwapLong)}, 1479 {CC"putOrderedObject", CC"("OBJ"J"OBJ")V", FN_PTR(Unsafe_SetOrderedObject)}, 1480 {CC"putOrderedInt", CC"("OBJ"JI)V", FN_PTR(Unsafe_SetOrderedInt)}, 1481 {CC"putOrderedLong", CC"("OBJ"JJ)V", FN_PTR(Unsafe_SetOrderedLong)}, 1482 {CC"park", CC"(ZJ)V", FN_PTR(Unsafe_Park)}, 1483 {CC"unpark", CC"("OBJ")V", FN_PTR(Unsafe_Unpark)} 1484 }; 1485 1486 // These are the methods for 1.8.0 1487 static JNINativeMethod methods_18[] = { 1488 {CC"getObject", CC"("OBJ"J)"OBJ"", FN_PTR(Unsafe_GetObject)}, 1489 {CC"putObject", CC"("OBJ"J"OBJ")V", FN_PTR(Unsafe_SetObject)}, 1490 {CC"getObjectVolatile",CC"("OBJ"J)"OBJ"", FN_PTR(Unsafe_GetObjectVolatile)}, 1491 {CC"putObjectVolatile",CC"("OBJ"J"OBJ")V", FN_PTR(Unsafe_SetObjectVolatile)}, 1492 1493 DECLARE_GETSETOOP(Boolean, Z), 1494 DECLARE_GETSETOOP(Byte, B), 1495 DECLARE_GETSETOOP(Short, S), 1496 DECLARE_GETSETOOP(Char, C), 1497 DECLARE_GETSETOOP(Int, I), 1498 DECLARE_GETSETOOP(Long, J), 1499 DECLARE_GETSETOOP(Float, F), 1500 DECLARE_GETSETOOP(Double, D), 1501 1502 DECLARE_GETSETNATIVE(Byte, B), 1503 DECLARE_GETSETNATIVE(Short, S), 1504 DECLARE_GETSETNATIVE(Char, C), 1505 DECLARE_GETSETNATIVE(Int, I), 1506 DECLARE_GETSETNATIVE(Long, J), 1507 DECLARE_GETSETNATIVE(Float, F), 1508 DECLARE_GETSETNATIVE(Double, D), 1509 1510 {CC"getAddress", CC"("ADR")"ADR, FN_PTR(Unsafe_GetNativeAddress)}, 1511 {CC"putAddress", CC"("ADR""ADR")V", FN_PTR(Unsafe_SetNativeAddress)}, 1512 1513 {CC"allocateMemory", CC"(J)"ADR, FN_PTR(Unsafe_AllocateMemory)}, 1514 {CC"reallocateMemory", CC"("ADR"J)"ADR, FN_PTR(Unsafe_ReallocateMemory)}, 1515 {CC"freeMemory", CC"("ADR")V", FN_PTR(Unsafe_FreeMemory)}, 1516 1517 {CC"objectFieldOffset", CC"("FLD")J", FN_PTR(Unsafe_ObjectFieldOffset)}, 1518 {CC"staticFieldOffset", CC"("FLD")J", FN_PTR(Unsafe_StaticFieldOffset)}, 1519 {CC"staticFieldBase", CC"("FLD")"OBJ, FN_PTR(Unsafe_StaticFieldBaseFromField)}, 1520 {CC"ensureClassInitialized",CC"("CLS")V", FN_PTR(Unsafe_EnsureClassInitialized)}, 1521 {CC"arrayBaseOffset", CC"("CLS")I", FN_PTR(Unsafe_ArrayBaseOffset)}, 1522 {CC"arrayIndexScale", CC"("CLS")I", FN_PTR(Unsafe_ArrayIndexScale)}, 1523 {CC"addressSize", CC"()I", FN_PTR(Unsafe_AddressSize)}, 1524 {CC"pageSize", CC"()I", FN_PTR(Unsafe_PageSize)}, 1525 1526 {CC"defineClass", CC"("DC_Args")"CLS, FN_PTR(Unsafe_DefineClass)}, 1527 {CC"allocateInstance", CC"("CLS")"OBJ, FN_PTR(Unsafe_AllocateInstance)}, 1528 {CC"throwException", CC"("THR")V", FN_PTR(Unsafe_ThrowException)}, 1529 {CC"compareAndSwapObject", CC"("OBJ"J"OBJ""OBJ")Z", FN_PTR(Unsafe_CompareAndSwapObject)}, 1530 {CC"compareAndSwapInt", CC"("OBJ"J""I""I"")Z", FN_PTR(Unsafe_CompareAndSwapInt)}, 1531 {CC"compareAndSwapLong", CC"("OBJ"J""J""J"")Z", FN_PTR(Unsafe_CompareAndSwapLong)}, 1532 {CC"putOrderedObject", CC"("OBJ"J"OBJ")V", FN_PTR(Unsafe_SetOrderedObject)}, 1533 {CC"putOrderedInt", CC"("OBJ"JI)V", FN_PTR(Unsafe_SetOrderedInt)}, 1534 {CC"putOrderedLong", CC"("OBJ"JJ)V", FN_PTR(Unsafe_SetOrderedLong)}, 1535 {CC"park", CC"(ZJ)V", FN_PTR(Unsafe_Park)}, 1536 {CC"unpark", CC"("OBJ")V", FN_PTR(Unsafe_Unpark)} 1537 }; 1538 1539 JNINativeMethod loadavg_method[] = { 1540 {CC"getLoadAverage", CC"([DI)I", FN_PTR(Unsafe_Loadavg)} 1541 }; 1542 1543 JNINativeMethod prefetch_methods[] = { 1544 {CC"prefetchRead", CC"("OBJ"J)V", FN_PTR(Unsafe_PrefetchRead)}, 1545 {CC"prefetchWrite", CC"("OBJ"J)V", FN_PTR(Unsafe_PrefetchWrite)}, 1546 {CC"prefetchReadStatic", CC"("OBJ"J)V", FN_PTR(Unsafe_PrefetchRead)}, 1547 {CC"prefetchWriteStatic",CC"("OBJ"J)V", FN_PTR(Unsafe_PrefetchWrite)} 1548 }; 1549 1550 JNINativeMethod memcopy_methods_17[] = { 1551 {CC"copyMemory", CC"("OBJ"J"OBJ"JJ)V", FN_PTR(Unsafe_CopyMemory2)}, 1552 {CC"setMemory", CC"("OBJ"JJB)V", FN_PTR(Unsafe_SetMemory2)} 1553 }; 1554 1555 JNINativeMethod memcopy_methods_15[] = { 1556 {CC"setMemory", CC"("ADR"JB)V", FN_PTR(Unsafe_SetMemory)}, 1557 {CC"copyMemory", CC"("ADR ADR"J)V", FN_PTR(Unsafe_CopyMemory)} 1558 }; 1559 1560 JNINativeMethod anonk_methods[] = { 1561 {CC"defineAnonymousClass", CC"("DAC_Args")"CLS, FN_PTR(Unsafe_DefineAnonymousClass)}, 1562 }; 1563 1564 JNINativeMethod lform_methods[] = { 1565 {CC"shouldBeInitialized",CC"("CLS")Z", FN_PTR(Unsafe_ShouldBeInitialized)}, 1566 }; 1567 1568 JNINativeMethod fence_methods[] = { 1569 {CC"loadFence", CC"()V", FN_PTR(Unsafe_LoadFence)}, 1570 {CC"storeFence", CC"()V", FN_PTR(Unsafe_StoreFence)}, 1571 {CC"fullFence", CC"()V", FN_PTR(Unsafe_FullFence)}, 1572 }; 1573 1574 #undef CC 1575 #undef FN_PTR 1576 1577 #undef ADR 1578 #undef LANG 1579 #undef OBJ 1580 #undef CLS 1581 #undef CTR 1582 #undef FLD 1583 #undef MTH 1584 #undef THR 1585 #undef DC0_Args 1586 #undef DC_Args 1587 1588 #undef DECLARE_GETSETOOP 1589 #undef DECLARE_GETSETNATIVE 1590 1591 1592 /** 1593 * Helper method to register native methods. 1594 */ 1595 static bool register_natives(const char* message, JNIEnv* env, jclass clazz, const JNINativeMethod* methods, jint nMethods) { 1596 int status = env->RegisterNatives(clazz, methods, nMethods); 1597 if (status < 0 || env->ExceptionOccurred()) { 1598 if (PrintMiscellaneous && (Verbose || WizardMode)) { 1599 tty->print_cr("Unsafe: failed registering %s", message); 1600 } 1601 env->ExceptionClear(); 1602 return false; 1603 } else { 1604 if (PrintMiscellaneous && (Verbose || WizardMode)) { 1605 tty->print_cr("Unsafe: successfully registered %s", message); 1606 } 1607 return true; 1608 } 1609 } 1610 1611 1612 // This one function is exported, used by NativeLookup. 1613 // The Unsafe_xxx functions above are called only from the interpreter. 1614 // The optimizer looks at names and signatures to recognize 1615 // individual functions. 1616 1617 JVM_ENTRY(void, JVM_RegisterUnsafeMethods(JNIEnv *env, jclass unsafecls)) 1618 UnsafeWrapper("JVM_RegisterUnsafeMethods"); 1619 { 1620 ThreadToNativeFromVM ttnfv(thread); 1621 1622 // Unsafe methods 1623 { 1624 bool success = false; 1625 // We need to register the 1.6 methods first because the 1.8 methods would register fine on 1.7 and 1.6 1626 if (!success) { 1627 success = register_natives("1.6 methods", env, unsafecls, methods_16, sizeof(methods_16)/sizeof(JNINativeMethod)); 1628 } 1629 if (!success) { 1630 success = register_natives("1.8 methods", env, unsafecls, methods_18, sizeof(methods_18)/sizeof(JNINativeMethod)); 1631 } 1632 if (!success) { 1633 success = register_natives("1.5 methods", env, unsafecls, methods_15, sizeof(methods_15)/sizeof(JNINativeMethod)); 1634 } 1635 if (!success) { 1636 success = register_natives("1.4.1 methods", env, unsafecls, methods_141, sizeof(methods_141)/sizeof(JNINativeMethod)); 1637 } 1638 if (!success) { 1639 success = register_natives("1.4.0 methods", env, unsafecls, methods_140, sizeof(methods_140)/sizeof(JNINativeMethod)); 1640 } 1641 guarantee(success, "register unsafe natives"); 1642 } 1643 1644 // Unsafe.getLoadAverage 1645 register_natives("1.6 loadavg method", env, unsafecls, loadavg_method, sizeof(loadavg_method)/sizeof(JNINativeMethod)); 1646 1647 // Prefetch methods 1648 register_natives("1.6 prefetch methods", env, unsafecls, prefetch_methods, sizeof(prefetch_methods)/sizeof(JNINativeMethod)); 1649 1650 // Memory copy methods 1651 { 1652 bool success = false; 1653 if (!success) { 1654 success = register_natives("1.7 memory copy methods", env, unsafecls, memcopy_methods_17, sizeof(memcopy_methods_17)/sizeof(JNINativeMethod)); 1655 } 1656 if (!success) { 1657 success = register_natives("1.5 memory copy methods", env, unsafecls, memcopy_methods_15, sizeof(memcopy_methods_15)/sizeof(JNINativeMethod)); 1658 } 1659 } 1660 1661 // Unsafe.defineAnonymousClass 1662 register_natives("1.7 define anonymous class method", env, unsafecls, anonk_methods, sizeof(anonk_methods)/sizeof(JNINativeMethod)); 1663 1664 // Unsafe.shouldBeInitialized 1665 register_natives("1.7 LambdaForm support", env, unsafecls, lform_methods, sizeof(lform_methods)/sizeof(JNINativeMethod)); 1666 1667 // Fence methods 1668 register_natives("1.8 fence methods", env, unsafecls, fence_methods, sizeof(fence_methods)/sizeof(JNINativeMethod)); 1669 } 1670 JVM_END