< prev index next >

src/hotspot/share/prims/unsafe.cpp

Print this page

   1 /*
   2  * Copyright (c) 2000, 2025, 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 "classfile/classFileStream.hpp"
  26 #include "classfile/classLoader.hpp"
  27 #include "classfile/classLoadInfo.hpp"
  28 #include "classfile/javaClasses.inline.hpp"
  29 #include "classfile/systemDictionary.hpp"
  30 #include "classfile/vmSymbols.hpp"
  31 #include "jfr/jfrEvents.hpp"
  32 #include "jni.h"
  33 #include "jvm.h"


  34 #include "memory/allocation.inline.hpp"

  35 #include "memory/resourceArea.hpp"
  36 #include "oops/access.inline.hpp"
  37 #include "oops/fieldStreams.inline.hpp"



  38 #include "oops/instanceKlass.inline.hpp"
  39 #include "oops/klass.inline.hpp"
  40 #include "oops/objArrayOop.inline.hpp"
  41 #include "oops/oop.inline.hpp"
  42 #include "oops/typeArrayOop.inline.hpp"

  43 #include "prims/jvmtiExport.hpp"
  44 #include "prims/unsafe.hpp"

  45 #include "runtime/globals.hpp"
  46 #include "runtime/handles.inline.hpp"
  47 #include "runtime/interfaceSupport.inline.hpp"
  48 #include "runtime/javaThread.inline.hpp"
  49 #include "runtime/jniHandles.inline.hpp"
  50 #include "runtime/orderAccess.hpp"
  51 #include "runtime/reflection.hpp"
  52 #include "runtime/sharedRuntime.hpp"
  53 #include "runtime/stubRoutines.hpp"
  54 #include "runtime/threadSMR.hpp"
  55 #include "runtime/vm_version.hpp"
  56 #include "runtime/vmOperations.hpp"
  57 #include "sanitizers/ub.hpp"
  58 #include "services/threadService.hpp"
  59 #include "utilities/align.hpp"
  60 #include "utilities/copy.hpp"
  61 #include "utilities/dtrace.hpp"
  62 #include "utilities/macros.hpp"
  63 
  64 /**

 151   }
 152 #endif
 153 }
 154 
 155 static inline void* index_oop_from_field_offset_long(oop p, jlong field_offset) {
 156   assert_field_offset_sane(p, field_offset);
 157   uintptr_t base_address = cast_from_oop<uintptr_t>(p);
 158   uintptr_t byte_offset  = (uintptr_t)field_offset_to_byte_offset(field_offset);
 159   return (void*)(base_address + byte_offset);
 160 }
 161 
 162 // Externally callable versions:
 163 // (Use these in compiler intrinsics which emulate unsafe primitives.)
 164 jlong Unsafe_field_offset_to_byte_offset(jlong field_offset) {
 165   return field_offset;
 166 }
 167 jlong Unsafe_field_offset_from_byte_offset(jlong byte_offset) {
 168   return byte_offset;
 169 }
 170 
 171 
 172 ///// Data read/writes on the Java heap and in native (off-heap) memory
 173 
 174 /**
 175  * Helper class to wrap memory accesses in JavaThread::doing_unsafe_access()
 176  */
 177 class GuardUnsafeAccess {
 178   JavaThread* _thread;
 179 
 180 public:
 181   GuardUnsafeAccess(JavaThread* thread) : _thread(thread) {
 182     // native/off-heap access which may raise SIGBUS if accessing
 183     // memory mapped file data in a region of the file which has
 184     // been truncated and is now invalid.
 185     _thread->set_doing_unsafe_access(true);
 186   }
 187 
 188   ~GuardUnsafeAccess() {
 189     _thread->set_doing_unsafe_access(false);
 190   }
 191 };

 230   jboolean normalize_for_read(jboolean x) {
 231     return x != 0;
 232   }
 233 
 234 public:
 235   MemoryAccess(JavaThread* thread, jobject obj, jlong offset)
 236     : _thread(thread), _obj(JNIHandles::resolve(obj)), _offset((ptrdiff_t)offset) {
 237     assert_field_offset_sane(_obj, offset);
 238   }
 239 
 240   T get() {
 241     GuardUnsafeAccess guard(_thread);
 242     return normalize_for_read(*addr());
 243   }
 244 
 245   // we use this method at some places for writing to 0 e.g. to cause a crash;
 246   // ubsan does not know that this is the desired behavior
 247   ATTRIBUTE_NO_UBSAN
 248   void put(T x) {
 249     GuardUnsafeAccess guard(_thread);

 250     *addr() = normalize_for_write(x);
 251   }
 252 
 253 
 254   T get_volatile() {
 255     GuardUnsafeAccess guard(_thread);
 256     volatile T ret = RawAccess<MO_SEQ_CST>::load(addr());
 257     return normalize_for_read(ret);
 258   }
 259 
 260   void put_volatile(T x) {
 261     GuardUnsafeAccess guard(_thread);
 262     RawAccess<MO_SEQ_CST>::store(addr(), normalize_for_write(x));
 263   }
 264 };
 265 






























































 266 // These functions allow a null base pointer with an arbitrary address.
 267 // But if the base pointer is non-null, the offset should make some sense.
 268 // That is, it should be in the range [0, MAX_OBJECT_SIZE].
 269 UNSAFE_ENTRY(jobject, Unsafe_GetReference(JNIEnv *env, jobject unsafe, jobject obj, jlong offset)) {
 270   oop p = JNIHandles::resolve(obj);
 271   assert_field_offset_sane(p, offset);
 272   oop v = HeapAccess<ON_UNKNOWN_OOP_REF>::oop_load_at(p, offset);
 273   return JNIHandles::make_local(THREAD, v);
 274 } UNSAFE_END
 275 
 276 UNSAFE_ENTRY(void, Unsafe_PutReference(JNIEnv *env, jobject unsafe, jobject obj, jlong offset, jobject x_h)) {
 277   oop x = JNIHandles::resolve(x_h);
 278   oop p = JNIHandles::resolve(obj);
 279   assert_field_offset_sane(p, offset);

 280   HeapAccess<ON_UNKNOWN_OOP_REF>::oop_store_at(p, offset, x);
 281 } UNSAFE_END
 282 
























































































































 283 UNSAFE_ENTRY(jobject, Unsafe_GetReferenceVolatile(JNIEnv *env, jobject unsafe, jobject obj, jlong offset)) {
 284   oop p = JNIHandles::resolve(obj);
 285   assert_field_offset_sane(p, offset);
 286   oop v = HeapAccess<MO_SEQ_CST | ON_UNKNOWN_OOP_REF>::oop_load_at(p, offset);
 287   return JNIHandles::make_local(THREAD, v);
 288 } UNSAFE_END
 289 
 290 UNSAFE_ENTRY(void, Unsafe_PutReferenceVolatile(JNIEnv *env, jobject unsafe, jobject obj, jlong offset, jobject x_h)) {
 291   oop x = JNIHandles::resolve(x_h);
 292   oop p = JNIHandles::resolve(obj);
 293   assert_field_offset_sane(p, offset);
 294   HeapAccess<MO_SEQ_CST | ON_UNKNOWN_OOP_REF>::oop_store_at(p, offset, x);
 295 } UNSAFE_END
 296 
 297 UNSAFE_ENTRY(jobject, Unsafe_GetUncompressedObject(JNIEnv *env, jobject unsafe, jlong addr)) {
 298   oop v = *(oop*) (address) addr;
 299   return JNIHandles::make_local(THREAD, v);
 300 } UNSAFE_END
 301 
 302 #define DEFINE_GETSETOOP(java_type, Type) \

 573     InstanceKlass* k = InstanceKlass::cast(klass);
 574     k->initialize(CHECK);
 575   }
 576 }
 577 UNSAFE_END
 578 
 579 UNSAFE_ENTRY(jboolean, Unsafe_ShouldBeInitialized0(JNIEnv *env, jobject unsafe, jobject clazz)) {
 580   assert(clazz != nullptr, "clazz must not be null");
 581 
 582   oop mirror = JNIHandles::resolve_non_null(clazz);
 583   Klass* klass = java_lang_Class::as_Klass(mirror);
 584 
 585   if (klass != nullptr && klass->should_be_initialized()) {
 586     return true;
 587   }
 588 
 589   return false;
 590 }
 591 UNSAFE_END
 592 





















 593 static void getBaseAndScale(int& base, int& scale, jclass clazz, TRAPS) {
 594   assert(clazz != nullptr, "clazz must not be null");
 595 
 596   oop mirror = JNIHandles::resolve_non_null(clazz);
 597   Klass* k = java_lang_Class::as_Klass(mirror);
 598 
 599   if (k == nullptr || !k->is_array_klass()) {
 600     THROW(vmSymbols::java_lang_InvalidClassException());
 601   } else if (k->is_objArray_klass()) {
 602     base  = arrayOopDesc::base_offset_in_bytes(T_OBJECT);
 603     scale = heapOopSize;
 604   } else if (k->is_typeArray_klass()) {
 605     TypeArrayKlass* tak = TypeArrayKlass::cast(k);
 606     base  = tak->array_header_in_bytes();
 607     assert(base == arrayOopDesc::base_offset_in_bytes(tak->element_type()), "array_header_size semantics ok");
 608     scale = (1 << tak->log2_element_size());









 609   } else {
 610     ShouldNotReachHere();
 611   }
 612 }
 613 















 614 UNSAFE_ENTRY(jint, Unsafe_ArrayBaseOffset0(JNIEnv *env, jobject unsafe, jclass clazz)) {
 615   int base = 0, scale = 0;
 616   getBaseAndScale(base, scale, clazz, CHECK_0);
 617 
 618   return field_offset_from_byte_offset(base);
 619 } UNSAFE_END
 620 
 621 
 622 UNSAFE_ENTRY(jint, Unsafe_ArrayIndexScale0(JNIEnv *env, jobject unsafe, jclass clazz)) {
 623   int base = 0, scale = 0;
 624   getBaseAndScale(base, scale, clazz, CHECK_0);
 625 
 626   // This VM packs both fields and array elements down to the byte.
 627   // But watch out:  If this changes, so that array references for
 628   // a given primitive type (say, T_BOOLEAN) use different memory units
 629   // than fields, this method MUST return zero for such arrays.
 630   // For example, the VM used to store sub-word sized fields in full
 631   // words in the object layout, so that accessors like getByte(Object,int)
 632   // did not really do what one might expect for arrays.  Therefore,
 633   // this function used to report a zero scale factor, so that the user
 634   // would know not to attempt to access sub-word array elements.
 635   // // Code for unpacked fields:
 636   // if (scale < wordSize)  return 0;
 637 
 638   // The following allows for a pretty general fieldOffset cookie scheme,
 639   // but requires it to be linear in byte offset.
 640   return field_offset_from_byte_offset(scale) - field_offset_from_byte_offset(0);
 641 } UNSAFE_END
 642 

































 643 
 644 static inline void throw_new(JNIEnv *env, const char *ename) {
 645   jclass cls = env->FindClass(ename);
 646   if (env->ExceptionCheck()) {
 647     env->ExceptionClear();
 648     tty->print_cr("Unsafe: cannot throw %s because FindClass has failed", ename);
 649     return;
 650   }
 651 
 652   env->ThrowNew(cls, nullptr);
 653 }
 654 
 655 static jclass Unsafe_DefineClass_impl(JNIEnv *env, jstring name, jbyteArray data, int offset, int length, jobject loader, jobject pd) {
 656   // Code lifted from JDK 1.3 ClassLoader.c
 657 
 658   jbyte *body;
 659   char *utfName = nullptr;
 660   jclass result = nullptr;
 661   char buf[128];
 662 

 836     case 3: a->double_at_put(2, (jdouble)la[2]); // fall through
 837     case 2: a->double_at_put(1, (jdouble)la[1]); // fall through
 838     case 1: a->double_at_put(0, (jdouble)la[0]); break;
 839   }
 840 
 841   return ret;
 842 } UNSAFE_END
 843 
 844 
 845 /// JVM_RegisterUnsafeMethods
 846 
 847 #define ADR "J"
 848 
 849 #define LANG "Ljava/lang/"
 850 
 851 #define OBJ LANG "Object;"
 852 #define CLS LANG "Class;"
 853 #define FLD LANG "reflect/Field;"
 854 #define THR LANG "Throwable;"
 855 


 856 #define DC_Args  LANG "String;[BII" LANG "ClassLoader;" "Ljava/security/ProtectionDomain;"
 857 #define DAC_Args CLS "[B[" OBJ
 858 
 859 #define CC (char*)  /*cast a literal from (const char*)*/
 860 #define FN_PTR(f) CAST_FROM_FN_PTR(void*, &f)
 861 
 862 #define DECLARE_GETPUTOOP(Type, Desc) \
 863     {CC "get" #Type,      CC "(" OBJ "J)" #Desc,       FN_PTR(Unsafe_Get##Type)}, \
 864     {CC "put" #Type,      CC "(" OBJ "J" #Desc ")V",   FN_PTR(Unsafe_Put##Type)}, \
 865     {CC "get" #Type "Volatile",      CC "(" OBJ "J)" #Desc,       FN_PTR(Unsafe_Get##Type##Volatile)}, \
 866     {CC "put" #Type "Volatile",      CC "(" OBJ "J" #Desc ")V",   FN_PTR(Unsafe_Put##Type##Volatile)}
 867 
 868 
 869 static JNINativeMethod jdk_internal_misc_Unsafe_methods[] = {
 870     {CC "getReference",         CC "(" OBJ "J)" OBJ "",   FN_PTR(Unsafe_GetReference)},
 871     {CC "putReference",         CC "(" OBJ "J" OBJ ")V",  FN_PTR(Unsafe_PutReference)},
 872     {CC "getReferenceVolatile", CC "(" OBJ "J)" OBJ,      FN_PTR(Unsafe_GetReferenceVolatile)},
 873     {CC "putReferenceVolatile", CC "(" OBJ "J" OBJ ")V",  FN_PTR(Unsafe_PutReferenceVolatile)},
 874 










 875     {CC "getUncompressedObject", CC "(" ADR ")" OBJ,  FN_PTR(Unsafe_GetUncompressedObject)},
 876 
 877     DECLARE_GETPUTOOP(Boolean, Z),
 878     DECLARE_GETPUTOOP(Byte, B),
 879     DECLARE_GETPUTOOP(Short, S),
 880     DECLARE_GETPUTOOP(Char, C),
 881     DECLARE_GETPUTOOP(Int, I),
 882     DECLARE_GETPUTOOP(Long, J),
 883     DECLARE_GETPUTOOP(Float, F),
 884     DECLARE_GETPUTOOP(Double, D),
 885 
 886     {CC "allocateMemory0",    CC "(J)" ADR,              FN_PTR(Unsafe_AllocateMemory0)},
 887     {CC "reallocateMemory0",  CC "(" ADR "J)" ADR,       FN_PTR(Unsafe_ReallocateMemory0)},
 888     {CC "freeMemory0",        CC "(" ADR ")V",           FN_PTR(Unsafe_FreeMemory0)},
 889 
 890     {CC "objectFieldOffset0", CC "(" FLD ")J",           FN_PTR(Unsafe_ObjectFieldOffset0)},
 891     {CC "knownObjectFieldOffset0", CC "(" CLS LANG "String;)J", FN_PTR(Unsafe_KnownObjectFieldOffset0)},
 892     {CC "staticFieldOffset0", CC "(" FLD ")J",           FN_PTR(Unsafe_StaticFieldOffset0)},
 893     {CC "staticFieldBase0",   CC "(" FLD ")" OBJ,        FN_PTR(Unsafe_StaticFieldBase0)},
 894     {CC "ensureClassInitialized0", CC "(" CLS ")V",      FN_PTR(Unsafe_EnsureClassInitialized0)},
 895     {CC "arrayBaseOffset0",   CC "(" CLS ")I",           FN_PTR(Unsafe_ArrayBaseOffset0)},

 896     {CC "arrayIndexScale0",   CC "(" CLS ")I",           FN_PTR(Unsafe_ArrayIndexScale0)},



 897 
 898     {CC "defineClass0",       CC "(" DC_Args ")" CLS,    FN_PTR(Unsafe_DefineClass0)},
 899     {CC "allocateInstance",   CC "(" CLS ")" OBJ,        FN_PTR(Unsafe_AllocateInstance)},
 900     {CC "throwException",     CC "(" THR ")V",           FN_PTR(Unsafe_ThrowException)},
 901     {CC "compareAndSetReference",CC "(" OBJ "J" OBJ "" OBJ ")Z", FN_PTR(Unsafe_CompareAndSetReference)},
 902     {CC "compareAndSetInt",   CC "(" OBJ "J""I""I"")Z",  FN_PTR(Unsafe_CompareAndSetInt)},
 903     {CC "compareAndSetLong",  CC "(" OBJ "J""J""J"")Z",  FN_PTR(Unsafe_CompareAndSetLong)},
 904     {CC "compareAndExchangeReference", CC "(" OBJ "J" OBJ "" OBJ ")" OBJ, FN_PTR(Unsafe_CompareAndExchangeReference)},
 905     {CC "compareAndExchangeInt",  CC "(" OBJ "J""I""I"")I", FN_PTR(Unsafe_CompareAndExchangeInt)},
 906     {CC "compareAndExchangeLong", CC "(" OBJ "J""J""J"")J", FN_PTR(Unsafe_CompareAndExchangeLong)},
 907 
 908     {CC "park",               CC "(ZJ)V",                FN_PTR(Unsafe_Park)},
 909     {CC "unpark",             CC "(" OBJ ")V",           FN_PTR(Unsafe_Unpark)},
 910 
 911     {CC "getLoadAverage0",    CC "([DI)I",               FN_PTR(Unsafe_GetLoadAverage0)},
 912 
 913     {CC "copyMemory0",        CC "(" OBJ "J" OBJ "JJ)V", FN_PTR(Unsafe_CopyMemory0)},
 914     {CC "copySwapMemory0",    CC "(" OBJ "J" OBJ "JJJ)V", FN_PTR(Unsafe_CopySwapMemory0)},
 915     {CC "writeback0",         CC "(" "J" ")V",           FN_PTR(Unsafe_WriteBack0)},
 916     {CC "writebackPreSync0",  CC "()V",                  FN_PTR(Unsafe_WriteBackPreSync0)},
 917     {CC "writebackPostSync0", CC "()V",                  FN_PTR(Unsafe_WriteBackPostSync0)},
 918     {CC "setMemory0",         CC "(" OBJ "JJB)V",        FN_PTR(Unsafe_SetMemory0)},
 919 
 920     {CC "shouldBeInitialized0", CC "(" CLS ")Z",         FN_PTR(Unsafe_ShouldBeInitialized0)},

 921 
 922     {CC "fullFence",          CC "()V",                  FN_PTR(Unsafe_FullFence)},
 923 };
 924 
 925 #undef CC
 926 #undef FN_PTR
 927 
 928 #undef ADR
 929 #undef LANG
 930 #undef OBJ
 931 #undef CLS
 932 #undef FLD
 933 #undef THR
 934 #undef DC_Args
 935 #undef DAC_Args
 936 
 937 #undef DECLARE_GETPUTOOP
 938 
 939 
 940 // This function is exported, used by NativeLookup.

   1 /*
   2  * Copyright (c) 2000, 2026, 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 "classfile/classFileStream.hpp"
  26 #include "classfile/classLoader.hpp"
  27 #include "classfile/classLoadInfo.hpp"
  28 #include "classfile/javaClasses.inline.hpp"
  29 #include "classfile/systemDictionary.hpp"
  30 #include "classfile/vmSymbols.hpp"
  31 #include "jfr/jfrEvents.hpp"
  32 #include "jni.h"
  33 #include "jvm.h"
  34 #include "logging/log.hpp"
  35 #include "logging/logStream.hpp"
  36 #include "memory/allocation.inline.hpp"
  37 #include "memory/oopFactory.hpp"
  38 #include "memory/resourceArea.hpp"
  39 #include "oops/access.inline.hpp"
  40 #include "oops/fieldStreams.inline.hpp"
  41 #include "oops/flatArrayKlass.hpp"
  42 #include "oops/flatArrayOop.inline.hpp"
  43 #include "oops/inlineKlass.inline.hpp"
  44 #include "oops/instanceKlass.inline.hpp"
  45 #include "oops/klass.inline.hpp"
  46 #include "oops/objArrayOop.inline.hpp"
  47 #include "oops/oop.inline.hpp"
  48 #include "oops/typeArrayOop.inline.hpp"
  49 #include "oops/valuePayload.hpp"
  50 #include "prims/jvmtiExport.hpp"
  51 #include "prims/unsafe.hpp"
  52 #include "runtime/fieldDescriptor.inline.hpp"
  53 #include "runtime/globals.hpp"
  54 #include "runtime/handles.inline.hpp"
  55 #include "runtime/interfaceSupport.inline.hpp"
  56 #include "runtime/javaThread.inline.hpp"
  57 #include "runtime/jniHandles.inline.hpp"
  58 #include "runtime/orderAccess.hpp"
  59 #include "runtime/reflection.hpp"
  60 #include "runtime/sharedRuntime.hpp"
  61 #include "runtime/stubRoutines.hpp"
  62 #include "runtime/threadSMR.hpp"
  63 #include "runtime/vm_version.hpp"
  64 #include "runtime/vmOperations.hpp"
  65 #include "sanitizers/ub.hpp"
  66 #include "services/threadService.hpp"
  67 #include "utilities/align.hpp"
  68 #include "utilities/copy.hpp"
  69 #include "utilities/dtrace.hpp"
  70 #include "utilities/macros.hpp"
  71 
  72 /**

 159   }
 160 #endif
 161 }
 162 
 163 static inline void* index_oop_from_field_offset_long(oop p, jlong field_offset) {
 164   assert_field_offset_sane(p, field_offset);
 165   uintptr_t base_address = cast_from_oop<uintptr_t>(p);
 166   uintptr_t byte_offset  = (uintptr_t)field_offset_to_byte_offset(field_offset);
 167   return (void*)(base_address + byte_offset);
 168 }
 169 
 170 // Externally callable versions:
 171 // (Use these in compiler intrinsics which emulate unsafe primitives.)
 172 jlong Unsafe_field_offset_to_byte_offset(jlong field_offset) {
 173   return field_offset;
 174 }
 175 jlong Unsafe_field_offset_from_byte_offset(jlong byte_offset) {
 176   return byte_offset;
 177 }
 178 

 179 ///// Data read/writes on the Java heap and in native (off-heap) memory
 180 
 181 /**
 182  * Helper class to wrap memory accesses in JavaThread::doing_unsafe_access()
 183  */
 184 class GuardUnsafeAccess {
 185   JavaThread* _thread;
 186 
 187 public:
 188   GuardUnsafeAccess(JavaThread* thread) : _thread(thread) {
 189     // native/off-heap access which may raise SIGBUS if accessing
 190     // memory mapped file data in a region of the file which has
 191     // been truncated and is now invalid.
 192     _thread->set_doing_unsafe_access(true);
 193   }
 194 
 195   ~GuardUnsafeAccess() {
 196     _thread->set_doing_unsafe_access(false);
 197   }
 198 };

 237   jboolean normalize_for_read(jboolean x) {
 238     return x != 0;
 239   }
 240 
 241 public:
 242   MemoryAccess(JavaThread* thread, jobject obj, jlong offset)
 243     : _thread(thread), _obj(JNIHandles::resolve(obj)), _offset((ptrdiff_t)offset) {
 244     assert_field_offset_sane(_obj, offset);
 245   }
 246 
 247   T get() {
 248     GuardUnsafeAccess guard(_thread);
 249     return normalize_for_read(*addr());
 250   }
 251 
 252   // we use this method at some places for writing to 0 e.g. to cause a crash;
 253   // ubsan does not know that this is the desired behavior
 254   ATTRIBUTE_NO_UBSAN
 255   void put(T x) {
 256     GuardUnsafeAccess guard(_thread);
 257     assert(_obj == nullptr || !_obj->is_inline_type() || _obj->mark().is_larval_state(), "must be an object instance or a larval inline type");
 258     *addr() = normalize_for_write(x);
 259   }
 260 

 261   T get_volatile() {
 262     GuardUnsafeAccess guard(_thread);
 263     volatile T ret = RawAccess<MO_SEQ_CST>::load(addr());
 264     return normalize_for_read(ret);
 265   }
 266 
 267   void put_volatile(T x) {
 268     GuardUnsafeAccess guard(_thread);
 269     RawAccess<MO_SEQ_CST>::store(addr(), normalize_for_write(x));
 270   }
 271 };
 272 
 273 #ifdef ASSERT
 274 /*
 275  * Get the field descriptor of the field of the given object at the given offset.
 276  */
 277 static bool get_field_descriptor(oop p, jlong offset, fieldDescriptor* fd) {
 278   bool found = false;
 279   Klass* k = p->klass();
 280   if (k->is_instance_klass()) {
 281     InstanceKlass* ik = InstanceKlass::cast(k);
 282     found = ik->find_field_from_offset((int)offset, false, fd);
 283     if (!found && ik->is_mirror_instance_klass()) {
 284       Klass* k2 = java_lang_Class::as_Klass(p);
 285       if (k2->is_instance_klass()) {
 286         ik = InstanceKlass::cast(k2);
 287         found = ik->find_field_from_offset((int)offset, true, fd);
 288       }
 289     }
 290   }
 291   return found;
 292 }
 293 #endif // ASSERT
 294 
 295 static void assert_and_log_unsafe_value_access(oop p, jlong offset, InlineKlass* vk) {
 296   Klass* k = p->klass();
 297 #ifdef ASSERT
 298   if (k->is_instance_klass()) {
 299     assert_field_offset_sane(p, offset);
 300     fieldDescriptor fd;
 301     bool found = get_field_descriptor(p, offset, &fd);
 302     if (found) {
 303       assert(found, "value field not found");
 304       assert(fd.is_flat(), "field not flat");
 305     } else {
 306       if (log_is_enabled(Trace, valuetypes)) {
 307         log_trace(valuetypes)("not a field in %s at offset " UINT64_FORMAT_X,
 308                               p->klass()->external_name(), (uint64_t)offset);
 309       }
 310     }
 311   } else if (k->is_flatArray_klass()) {
 312     FlatArrayKlass* vak = FlatArrayKlass::cast(k);
 313     int index = (offset - vak->array_header_in_bytes()) / vak->element_byte_size();
 314     address dest = (address)((flatArrayOop)p)->value_at_addr(index, vak->layout_helper());
 315     assert(dest == (cast_from_oop<address>(p) + offset), "invalid offset");
 316   } else {
 317     ShouldNotReachHere();
 318   }
 319 #endif // ASSERT
 320   if (log_is_enabled(Trace, valuetypes)) {
 321     if (k->is_flatArray_klass()) {
 322       FlatArrayKlass* vak = FlatArrayKlass::cast(k);
 323       int index = (offset - vak->array_header_in_bytes()) / vak->element_byte_size();
 324       address dest = (address)((flatArrayOop)p)->value_at_addr(index, vak->layout_helper());
 325       log_trace(valuetypes)("%s array type %s index %d element size %d offset " UINT64_FORMAT_X " at " INTPTR_FORMAT,
 326                             p->klass()->external_name(), vak->external_name(),
 327                             index, vak->element_byte_size(), (uint64_t)offset, p2i(dest));
 328     } else {
 329       log_trace(valuetypes)("%s field type %s at offset " UINT64_FORMAT_X,
 330                             p->klass()->external_name(), vk->external_name(), (uint64_t)offset);
 331     }
 332   }
 333 }
 334 
 335 // These functions allow a null base pointer with an arbitrary address.
 336 // But if the base pointer is non-null, the offset should make some sense.
 337 // That is, it should be in the range [0, MAX_OBJECT_SIZE].
 338 UNSAFE_ENTRY(jobject, Unsafe_GetReference(JNIEnv *env, jobject unsafe, jobject obj, jlong offset)) {
 339   oop p = JNIHandles::resolve(obj);
 340   assert_field_offset_sane(p, offset);
 341   oop v = HeapAccess<ON_UNKNOWN_OOP_REF>::oop_load_at(p, offset);
 342   return JNIHandles::make_local(THREAD, v);
 343 } UNSAFE_END
 344 
 345 UNSAFE_ENTRY(void, Unsafe_PutReference(JNIEnv *env, jobject unsafe, jobject obj, jlong offset, jobject x_h)) {
 346   oop x = JNIHandles::resolve(x_h);
 347   oop p = JNIHandles::resolve(obj);
 348   assert_field_offset_sane(p, offset);
 349   assert(!p->is_inline_type() || p->mark().is_larval_state(), "must be an object instance or a larval inline type");
 350   HeapAccess<ON_UNKNOWN_OOP_REF>::oop_store_at(p, offset, x);
 351 } UNSAFE_END
 352 
 353 UNSAFE_ENTRY(jlong, Unsafe_ValueHeaderSize(JNIEnv *env, jobject unsafe, jclass c)) {
 354   Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(c));
 355   InlineKlass* vk = InlineKlass::cast(k);
 356   return vk->payload_offset();
 357 } UNSAFE_END
 358 
 359 UNSAFE_ENTRY(jboolean, Unsafe_IsFlatField(JNIEnv *env, jobject unsafe, jobject o)) {
 360   oop f = JNIHandles::resolve_non_null(o);
 361   Klass* k = java_lang_Class::as_Klass(java_lang_reflect_Field::clazz(f));
 362   int slot = java_lang_reflect_Field::slot(f);
 363   return InstanceKlass::cast(k)->field_is_flat(slot);
 364 } UNSAFE_END
 365 
 366 UNSAFE_ENTRY(jboolean, Unsafe_HasNullMarker(JNIEnv *env, jobject unsafe, jobject o)) {
 367   oop f = JNIHandles::resolve_non_null(o);
 368   Klass* k = java_lang_Class::as_Klass(java_lang_reflect_Field::clazz(f));
 369   int slot = java_lang_reflect_Field::slot(f);
 370   return InstanceKlass::cast(k)->field_has_null_marker(slot);
 371 } UNSAFE_END
 372 
 373 UNSAFE_ENTRY(jint, Unsafe_NullMarkerOffset(JNIEnv *env, jobject unsafe, jobject o)) {
 374   oop f = JNIHandles::resolve_non_null(o);
 375   Klass* k = java_lang_Class::as_Klass(java_lang_reflect_Field::clazz(f));
 376   int slot = java_lang_reflect_Field::slot(f);
 377   return InstanceKlass::cast(k)->field_null_marker_offset(slot);
 378 } UNSAFE_END
 379 
 380 UNSAFE_ENTRY(jint, Unsafe_ArrayLayout(JNIEnv *env, jobject unsafe, jarray array)) {
 381   oop ar = JNIHandles::resolve_non_null(array);
 382   ArrayKlass* ak = ArrayKlass::cast(ar->klass());
 383   if (ak->is_refArray_klass()) {
 384     return (jint)LayoutKind::REFERENCE;
 385   } else if (ak->is_flatArray_klass()) {
 386     return (jint)FlatArrayKlass::cast(ak)->layout_kind();
 387   } else {
 388     ShouldNotReachHere();
 389     return -1;
 390   }
 391 } UNSAFE_END
 392 
 393 UNSAFE_ENTRY(jint, Unsafe_FieldLayout(JNIEnv *env, jobject unsafe, jobject field)) {
 394   assert(field != nullptr, "field must not be null");
 395 
 396   oop reflected   = JNIHandles::resolve_non_null(field);
 397   oop mirror      = java_lang_reflect_Field::clazz(reflected);
 398   Klass* k        = java_lang_Class::as_Klass(mirror);
 399   int slot        = java_lang_reflect_Field::slot(reflected);
 400   int modifiers   = java_lang_reflect_Field::modifiers(reflected);
 401 
 402   if ((modifiers & JVM_ACC_STATIC) != 0) {
 403     return (jint)LayoutKind::REFERENCE; // static fields are never flat
 404   } else {
 405     InstanceKlass* ik = InstanceKlass::cast(k);
 406     if (ik->field_is_flat(slot)) {
 407       return (jint)ik->inline_layout_info(slot).kind();
 408     } else {
 409       return (jint)LayoutKind::REFERENCE;
 410     }
 411   }
 412 } UNSAFE_END
 413 
 414 UNSAFE_ENTRY(jarray, Unsafe_NewSpecialArray(JNIEnv *env, jobject unsafe, jclass elmClass, jint len, jint layoutKind)) {
 415   oop mirror = JNIHandles::resolve_non_null(elmClass);
 416   Klass* klass = java_lang_Class::as_Klass(mirror);
 417   klass->initialize(CHECK_NULL);
 418   if (len < 0) {
 419     THROW_MSG_NULL(vmSymbols::java_lang_IllegalArgumentException(), "Array length is negative");
 420   }
 421   if (klass->is_array_klass() || klass->is_identity_class()) {
 422     THROW_MSG_NULL(vmSymbols::java_lang_IllegalArgumentException(), "Element class is not a value class");
 423   }
 424   if (klass->is_abstract()) {
 425     THROW_MSG_NULL(vmSymbols::java_lang_IllegalArgumentException(), "Element class is abstract");
 426   }
 427   LayoutKind lk = static_cast<LayoutKind>(layoutKind);
 428   if (lk <= LayoutKind::REFERENCE || lk == LayoutKind::NULLABLE_NON_ATOMIC_FLAT || lk >= LayoutKind::UNKNOWN) {
 429     THROW_MSG_NULL(vmSymbols::java_lang_IllegalArgumentException(), "Invalid layout kind");
 430   }
 431   InlineKlass* vk = InlineKlass::cast(klass);
 432   // WARNING: test below will need modifications when flat layouts supported for fields
 433   // but not for arrays are introduce (NULLABLE_NON_ATOMIC_FLAT for instance)
 434   if (!UseArrayFlattening || !vk->is_layout_supported(lk)) {
 435     THROW_MSG_NULL(vmSymbols::java_lang_UnsupportedOperationException(), "Layout not supported");
 436   }
 437   ArrayProperties props = ArrayKlass::array_properties_from_layout(lk);
 438   oop array = oopFactory::new_flatArray(vk, len, props, lk, CHECK_NULL);
 439   return (jarray) JNIHandles::make_local(THREAD, array);
 440 } UNSAFE_END
 441 
 442 UNSAFE_ENTRY(jobject, Unsafe_GetFlatValue(JNIEnv *env, jobject unsafe, jobject obj, jlong offset, jint layoutKind, jclass vc)) {
 443   assert(layoutKind != (int)LayoutKind::UNKNOWN, "Sanity");
 444   assert(layoutKind != (int)LayoutKind::REFERENCE, "This method handles only flat layouts");
 445   oop base = JNIHandles::resolve(obj);
 446   if (base == nullptr) {
 447     THROW_NULL(vmSymbols::java_lang_NullPointerException());
 448   }
 449   Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(vc));
 450   InlineKlass* vk = InlineKlass::cast(k);
 451   assert_and_log_unsafe_value_access(base, offset, vk);
 452   LayoutKind lk = (LayoutKind)layoutKind;
 453   FlatValuePayload payload = FlatValuePayload::construct_from_parts(base, offset, vk, lk);
 454   oop v = payload.read(CHECK_NULL);
 455   return JNIHandles::make_local(THREAD, v);
 456 } UNSAFE_END
 457 
 458 UNSAFE_ENTRY(void, Unsafe_PutFlatValue(JNIEnv *env, jobject unsafe, jobject obj, jlong offset, jint layoutKind, jclass vc, jobject value)) {
 459   assert(layoutKind != (int)LayoutKind::UNKNOWN, "Sanity");
 460   assert(layoutKind != (int)LayoutKind::REFERENCE, "This method handles only flat layouts");
 461   oop base = JNIHandles::resolve(obj);
 462   if (base == nullptr) {
 463     THROW(vmSymbols::java_lang_NullPointerException());
 464   }
 465 
 466   InlineKlass* vk = InlineKlass::cast(java_lang_Class::as_Klass(JNIHandles::resolve_non_null(vc)));
 467   assert_and_log_unsafe_value_access(base, offset, vk);
 468   LayoutKind lk = (LayoutKind)layoutKind;
 469   FlatValuePayload payload = FlatValuePayload::construct_from_parts(base, offset, vk, lk);
 470   payload.write(inlineOop(JNIHandles::resolve(value)), CHECK);
 471 } UNSAFE_END
 472 
 473 UNSAFE_ENTRY(jobject, Unsafe_GetReferenceVolatile(JNIEnv *env, jobject unsafe, jobject obj, jlong offset)) {
 474   oop p = JNIHandles::resolve(obj);
 475   assert_field_offset_sane(p, offset);
 476   oop v = HeapAccess<MO_SEQ_CST | ON_UNKNOWN_OOP_REF>::oop_load_at(p, offset);
 477   return JNIHandles::make_local(THREAD, v);
 478 } UNSAFE_END
 479 
 480 UNSAFE_ENTRY(void, Unsafe_PutReferenceVolatile(JNIEnv *env, jobject unsafe, jobject obj, jlong offset, jobject x_h)) {
 481   oop x = JNIHandles::resolve(x_h);
 482   oop p = JNIHandles::resolve(obj);
 483   assert_field_offset_sane(p, offset);
 484   HeapAccess<MO_SEQ_CST | ON_UNKNOWN_OOP_REF>::oop_store_at(p, offset, x);
 485 } UNSAFE_END
 486 
 487 UNSAFE_ENTRY(jobject, Unsafe_GetUncompressedObject(JNIEnv *env, jobject unsafe, jlong addr)) {
 488   oop v = *(oop*) (address) addr;
 489   return JNIHandles::make_local(THREAD, v);
 490 } UNSAFE_END
 491 
 492 #define DEFINE_GETSETOOP(java_type, Type) \

 763     InstanceKlass* k = InstanceKlass::cast(klass);
 764     k->initialize(CHECK);
 765   }
 766 }
 767 UNSAFE_END
 768 
 769 UNSAFE_ENTRY(jboolean, Unsafe_ShouldBeInitialized0(JNIEnv *env, jobject unsafe, jobject clazz)) {
 770   assert(clazz != nullptr, "clazz must not be null");
 771 
 772   oop mirror = JNIHandles::resolve_non_null(clazz);
 773   Klass* klass = java_lang_Class::as_Klass(mirror);
 774 
 775   if (klass != nullptr && klass->should_be_initialized()) {
 776     return true;
 777   }
 778 
 779   return false;
 780 }
 781 UNSAFE_END
 782 
 783 UNSAFE_ENTRY(void, Unsafe_NotifyStrictStaticAccess0(JNIEnv *env, jobject unsafe, jobject clazz,
 784                                                     jlong sfoffset, jboolean writing)) {
 785   assert(clazz != nullptr, "clazz must not be null");
 786 
 787   oop mirror = JNIHandles::resolve_non_null(clazz);
 788   Klass* klass = java_lang_Class::as_Klass(mirror);
 789 
 790   if (klass != nullptr && klass->is_instance_klass()) {
 791     InstanceKlass* ik = InstanceKlass::cast(klass);
 792     fieldDescriptor fd;
 793     if (ik->find_local_field_from_offset((int)sfoffset, true, &fd)) {
 794       // Note: The Unsafe API takes an OFFSET, but the InstanceKlass wants the INDEX.
 795       // We could surface field indexes into Unsafe, but that's too much churn.
 796       ik->notify_strict_static_access(fd.index(), writing, CHECK);
 797       return;
 798     }
 799   }
 800   THROW(vmSymbols::java_lang_InternalError());
 801 }
 802 UNSAFE_END
 803 
 804 static void getBaseAndScale(int& base, int& scale, jclass clazz, TRAPS) {
 805   assert(clazz != nullptr, "clazz must not be null");
 806 
 807   oop mirror = JNIHandles::resolve_non_null(clazz);
 808   Klass* k = java_lang_Class::as_Klass(mirror);
 809 
 810   if (k == nullptr || !k->is_array_klass()) {
 811     THROW(vmSymbols::java_lang_InvalidClassException());



 812   } else if (k->is_typeArray_klass()) {
 813     TypeArrayKlass* tak = TypeArrayKlass::cast(k);
 814     base  = tak->array_header_in_bytes();
 815     assert(base == arrayOopDesc::base_offset_in_bytes(tak->element_type()), "array_header_size semantics ok");
 816     scale = (1 << tak->log2_element_size());
 817   } else if (k->is_objArray_klass()) {
 818     Klass* ek = ObjArrayKlass::cast(k)->element_klass();
 819     if (!ek->is_identity_class() && !ek->is_abstract()) {
 820       // Arrays of a concrete value class type can have multiple layouts
 821       // There's no good value to return, so throwing an exception is the way out
 822       THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), "Arrays of a concrete value class don't have a single base and offset");
 823     }
 824     base  = arrayOopDesc::base_offset_in_bytes(T_OBJECT);
 825     scale = heapOopSize;
 826   } else {
 827     ShouldNotReachHere();
 828   }
 829 }
 830 
 831 UNSAFE_ENTRY(jint, Unsafe_ArrayInstanceBaseOffset0(JNIEnv *env, jobject unsafe, jarray array)) {
 832   assert(array != nullptr, "array must not be null");
 833   oop ar = JNIHandles::resolve_non_null(array);
 834   assert(ar->is_array(), "Must be an array");
 835   ArrayKlass* ak = ArrayKlass::cast(ar->klass());
 836   if (ak->is_refArray_klass()) {
 837     return arrayOopDesc::base_offset_in_bytes(T_OBJECT);
 838   } else if (ak->is_flatArray_klass()) {
 839     FlatArrayKlass* fak = FlatArrayKlass::cast(ak);
 840     return fak->array_header_in_bytes();
 841   } else {
 842     ShouldNotReachHere();
 843   }
 844 } UNSAFE_END
 845 
 846 UNSAFE_ENTRY(jint, Unsafe_ArrayBaseOffset0(JNIEnv *env, jobject unsafe, jclass clazz)) {
 847   int base = 0, scale = 0;
 848   getBaseAndScale(base, scale, clazz, CHECK_0);
 849 
 850   return field_offset_from_byte_offset(base);
 851 } UNSAFE_END
 852 
 853 
 854 UNSAFE_ENTRY(jint, Unsafe_ArrayIndexScale0(JNIEnv *env, jobject unsafe, jclass clazz)) {
 855   int base = 0, scale = 0;
 856   getBaseAndScale(base, scale, clazz, CHECK_0);
 857 
 858   // This VM packs both fields and array elements down to the byte.
 859   // But watch out:  If this changes, so that array references for
 860   // a given primitive type (say, T_BOOLEAN) use different memory units
 861   // than fields, this method MUST return zero for such arrays.
 862   // For example, the VM used to store sub-word sized fields in full
 863   // words in the object layout, so that accessors like getByte(Object,int)
 864   // did not really do what one might expect for arrays.  Therefore,
 865   // this function used to report a zero scale factor, so that the user
 866   // would know not to attempt to access sub-word array elements.
 867   // // Code for unpacked fields:
 868   // if (scale < wordSize)  return 0;
 869 
 870   // The following allows for a pretty general fieldOffset cookie scheme,
 871   // but requires it to be linear in byte offset.
 872   return field_offset_from_byte_offset(scale) - field_offset_from_byte_offset(0);
 873 } UNSAFE_END
 874 
 875 UNSAFE_ENTRY(jint, Unsafe_ArrayInstanceIndexScale0(JNIEnv *env, jobject unsafe, jarray array)) {
 876   assert(array != nullptr, "array must not be null");
 877   oop ar = JNIHandles::resolve_non_null(array);
 878   assert(ar->is_array(), "Must be an array");
 879   ArrayKlass* ak = ArrayKlass::cast(ar->klass());
 880   if (ak->is_refArray_klass()) {
 881     return heapOopSize;
 882   } else if (ak->is_flatArray_klass()) {
 883     FlatArrayKlass* fak = FlatArrayKlass::cast(ak);
 884     return fak->element_byte_size();
 885   } else {
 886     ShouldNotReachHere();
 887   }
 888 } UNSAFE_END
 889 
 890 UNSAFE_ENTRY(jarray, Unsafe_GetFieldMap0(JNIEnv* env, jobject unsafe, jclass clazz)) {
 891   oop mirror = JNIHandles::resolve_non_null(clazz);
 892   Klass* k = java_lang_Class::as_Klass(mirror);
 893 
 894   if (!k->is_inline_klass()) {
 895     THROW_MSG_NULL(vmSymbols::java_lang_IllegalArgumentException(), "Argument is not a concrete value class");
 896   }
 897   InlineKlass* vk = InlineKlass::cast(k);
 898   oop map = mirror->obj_field(vk->acmp_maps_offset());
 899   return (jarray) JNIHandles::make_local(THREAD, map);
 900 } UNSAFE_END
 901 
 902 
 903 UNSAFE_ENTRY(jlong, Unsafe_GetObjectSize0(JNIEnv* env, jobject o, jobject obj))
 904   oop p = JNIHandles::resolve(obj);
 905   return p->size() * HeapWordSize;
 906 UNSAFE_END
 907 
 908 
 909 static inline void throw_new(JNIEnv *env, const char *ename) {
 910   jclass cls = env->FindClass(ename);
 911   if (env->ExceptionCheck()) {
 912     env->ExceptionClear();
 913     tty->print_cr("Unsafe: cannot throw %s because FindClass has failed", ename);
 914     return;
 915   }
 916 
 917   env->ThrowNew(cls, nullptr);
 918 }
 919 
 920 static jclass Unsafe_DefineClass_impl(JNIEnv *env, jstring name, jbyteArray data, int offset, int length, jobject loader, jobject pd) {
 921   // Code lifted from JDK 1.3 ClassLoader.c
 922 
 923   jbyte *body;
 924   char *utfName = nullptr;
 925   jclass result = nullptr;
 926   char buf[128];
 927 

1101     case 3: a->double_at_put(2, (jdouble)la[2]); // fall through
1102     case 2: a->double_at_put(1, (jdouble)la[1]); // fall through
1103     case 1: a->double_at_put(0, (jdouble)la[0]); break;
1104   }
1105 
1106   return ret;
1107 } UNSAFE_END
1108 
1109 
1110 /// JVM_RegisterUnsafeMethods
1111 
1112 #define ADR "J"
1113 
1114 #define LANG "Ljava/lang/"
1115 
1116 #define OBJ LANG "Object;"
1117 #define CLS LANG "Class;"
1118 #define FLD LANG "reflect/Field;"
1119 #define THR LANG "Throwable;"
1120 
1121 #define OBJ_ARR "[" OBJ
1122 
1123 #define DC_Args  LANG "String;[BII" LANG "ClassLoader;" "Ljava/security/ProtectionDomain;"
1124 #define DAC_Args CLS "[B[" OBJ
1125 
1126 #define CC (char*)  /*cast a literal from (const char*)*/
1127 #define FN_PTR(f) CAST_FROM_FN_PTR(void*, &f)
1128 
1129 #define DECLARE_GETPUTOOP(Type, Desc) \
1130     {CC "get"  #Type,      CC "(" OBJ "J)" #Desc,                 FN_PTR(Unsafe_Get##Type)}, \
1131     {CC "put"  #Type,      CC "(" OBJ "J" #Desc ")V",             FN_PTR(Unsafe_Put##Type)}, \
1132     {CC "get"  #Type "Volatile",      CC "(" OBJ "J)" #Desc,      FN_PTR(Unsafe_Get##Type##Volatile)}, \
1133     {CC "put"  #Type "Volatile",      CC "(" OBJ "J" #Desc ")V",  FN_PTR(Unsafe_Put##Type##Volatile)}
1134 
1135 
1136 static JNINativeMethod jdk_internal_misc_Unsafe_methods[] = {
1137     {CC "getReference",         CC "(" OBJ "J)" OBJ "",   FN_PTR(Unsafe_GetReference)},
1138     {CC "putReference",         CC "(" OBJ "J" OBJ ")V",  FN_PTR(Unsafe_PutReference)},
1139     {CC "getReferenceVolatile", CC "(" OBJ "J)" OBJ,      FN_PTR(Unsafe_GetReferenceVolatile)},
1140     {CC "putReferenceVolatile", CC "(" OBJ "J" OBJ ")V",  FN_PTR(Unsafe_PutReferenceVolatile)},
1141 
1142     {CC "isFlatField0",         CC "(" OBJ ")Z",          FN_PTR(Unsafe_IsFlatField)},
1143     {CC "hasNullMarker0",       CC "(" OBJ ")Z",          FN_PTR(Unsafe_HasNullMarker)},
1144     {CC "nullMarkerOffset0",    CC "(" OBJ ")I",          FN_PTR(Unsafe_NullMarkerOffset)},
1145     {CC "arrayLayout0",         CC "(" OBJ_ARR ")I",      FN_PTR(Unsafe_ArrayLayout)},
1146     {CC "fieldLayout0",         CC "(" OBJ ")I",          FN_PTR(Unsafe_FieldLayout)},
1147     {CC "newSpecialArray",      CC "(" CLS "II)[" OBJ,    FN_PTR(Unsafe_NewSpecialArray)},
1148     {CC "getFlatValue",         CC "(" OBJ "JI" CLS ")" OBJ, FN_PTR(Unsafe_GetFlatValue)},
1149     {CC "putFlatValue",         CC "(" OBJ "JI" CLS OBJ ")V", FN_PTR(Unsafe_PutFlatValue)},
1150     {CC "valueHeaderSize",       CC "(" CLS ")J",         FN_PTR(Unsafe_ValueHeaderSize)},
1151 
1152     {CC "getUncompressedObject", CC "(" ADR ")" OBJ,  FN_PTR(Unsafe_GetUncompressedObject)},
1153 
1154     DECLARE_GETPUTOOP(Boolean, Z),
1155     DECLARE_GETPUTOOP(Byte, B),
1156     DECLARE_GETPUTOOP(Short, S),
1157     DECLARE_GETPUTOOP(Char, C),
1158     DECLARE_GETPUTOOP(Int, I),
1159     DECLARE_GETPUTOOP(Long, J),
1160     DECLARE_GETPUTOOP(Float, F),
1161     DECLARE_GETPUTOOP(Double, D),
1162 
1163     {CC "allocateMemory0",    CC "(J)" ADR,              FN_PTR(Unsafe_AllocateMemory0)},
1164     {CC "reallocateMemory0",  CC "(" ADR "J)" ADR,       FN_PTR(Unsafe_ReallocateMemory0)},
1165     {CC "freeMemory0",        CC "(" ADR ")V",           FN_PTR(Unsafe_FreeMemory0)},
1166 
1167     {CC "objectFieldOffset0", CC "(" FLD ")J",           FN_PTR(Unsafe_ObjectFieldOffset0)},
1168     {CC "knownObjectFieldOffset0", CC "(" CLS LANG "String;)J", FN_PTR(Unsafe_KnownObjectFieldOffset0)},
1169     {CC "staticFieldOffset0", CC "(" FLD ")J",           FN_PTR(Unsafe_StaticFieldOffset0)},
1170     {CC "staticFieldBase0",   CC "(" FLD ")" OBJ,        FN_PTR(Unsafe_StaticFieldBase0)},
1171     {CC "ensureClassInitialized0", CC "(" CLS ")V",      FN_PTR(Unsafe_EnsureClassInitialized0)},
1172     {CC "arrayBaseOffset0",   CC "(" CLS ")I",           FN_PTR(Unsafe_ArrayBaseOffset0)},
1173     {CC "arrayInstanceBaseOffset0",   CC "(" OBJ_ARR ")I", FN_PTR(Unsafe_ArrayInstanceBaseOffset0)},
1174     {CC "arrayIndexScale0",   CC "(" CLS ")I",           FN_PTR(Unsafe_ArrayIndexScale0)},
1175     {CC "arrayInstanceIndexScale0",   CC "(" OBJ_ARR ")I", FN_PTR(Unsafe_ArrayInstanceIndexScale0)},
1176     {CC "getFieldMap0",       CC "(Ljava/lang/Class;)[I", FN_PTR(Unsafe_GetFieldMap0)},
1177     {CC "getObjectSize0",     CC "(Ljava/lang/Object;)J", FN_PTR(Unsafe_GetObjectSize0)},
1178 
1179     {CC "defineClass0",       CC "(" DC_Args ")" CLS,    FN_PTR(Unsafe_DefineClass0)},
1180     {CC "allocateInstance",   CC "(" CLS ")" OBJ,        FN_PTR(Unsafe_AllocateInstance)},
1181     {CC "throwException",     CC "(" THR ")V",           FN_PTR(Unsafe_ThrowException)},
1182     {CC "compareAndSetReference",CC "(" OBJ "J" OBJ "" OBJ ")Z", FN_PTR(Unsafe_CompareAndSetReference)},
1183     {CC "compareAndSetInt",   CC "(" OBJ "J""I""I"")Z",  FN_PTR(Unsafe_CompareAndSetInt)},
1184     {CC "compareAndSetLong",  CC "(" OBJ "J""J""J"")Z",  FN_PTR(Unsafe_CompareAndSetLong)},
1185     {CC "compareAndExchangeReference", CC "(" OBJ "J" OBJ "" OBJ ")" OBJ, FN_PTR(Unsafe_CompareAndExchangeReference)},
1186     {CC "compareAndExchangeInt",  CC "(" OBJ "J""I""I"")I", FN_PTR(Unsafe_CompareAndExchangeInt)},
1187     {CC "compareAndExchangeLong", CC "(" OBJ "J""J""J"")J", FN_PTR(Unsafe_CompareAndExchangeLong)},
1188 
1189     {CC "park",               CC "(ZJ)V",                FN_PTR(Unsafe_Park)},
1190     {CC "unpark",             CC "(" OBJ ")V",           FN_PTR(Unsafe_Unpark)},
1191 
1192     {CC "getLoadAverage0",    CC "([DI)I",               FN_PTR(Unsafe_GetLoadAverage0)},
1193 
1194     {CC "copyMemory0",        CC "(" OBJ "J" OBJ "JJ)V", FN_PTR(Unsafe_CopyMemory0)},
1195     {CC "copySwapMemory0",    CC "(" OBJ "J" OBJ "JJJ)V", FN_PTR(Unsafe_CopySwapMemory0)},
1196     {CC "writeback0",         CC "(" "J" ")V",           FN_PTR(Unsafe_WriteBack0)},
1197     {CC "writebackPreSync0",  CC "()V",                  FN_PTR(Unsafe_WriteBackPreSync0)},
1198     {CC "writebackPostSync0", CC "()V",                  FN_PTR(Unsafe_WriteBackPostSync0)},
1199     {CC "setMemory0",         CC "(" OBJ "JJB)V",        FN_PTR(Unsafe_SetMemory0)},
1200 
1201     {CC "shouldBeInitialized0", CC "(" CLS ")Z",         FN_PTR(Unsafe_ShouldBeInitialized0)},
1202     {CC "notifyStrictStaticAccess0", CC "(" CLS "JZ)V",  FN_PTR(Unsafe_NotifyStrictStaticAccess0)},
1203 
1204     {CC "fullFence",          CC "()V",                  FN_PTR(Unsafe_FullFence)},
1205 };
1206 
1207 #undef CC
1208 #undef FN_PTR
1209 
1210 #undef ADR
1211 #undef LANG
1212 #undef OBJ
1213 #undef CLS
1214 #undef FLD
1215 #undef THR
1216 #undef DC_Args
1217 #undef DAC_Args
1218 
1219 #undef DECLARE_GETPUTOOP
1220 
1221 
1222 // This function is exported, used by NativeLookup.
< prev index next >