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