1 /*
   2  * Copyright (c) 1997, 2023, Oracle and/or its affiliates. All rights reserved.
   3  * Copyright (c) 2012 Red Hat, Inc.
   4  * Copyright (c) 2021, Azul Systems, Inc. All rights reserved.
   5  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   6  *
   7  * This code is free software; you can redistribute it and/or modify it
   8  * under the terms of the GNU General Public License version 2 only, as
   9  * published by the Free Software Foundation.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  *
  25  */
  26 
  27 #include "precompiled.hpp"
  28 #include "ci/ciReplay.hpp"
  29 #include "classfile/altHashing.hpp"
  30 #include "classfile/classFileStream.hpp"
  31 #include "classfile/classLoader.hpp"
  32 #include "classfile/classLoadInfo.hpp"
  33 #include "classfile/javaClasses.hpp"
  34 #include "classfile/javaClasses.inline.hpp"
  35 #include "classfile/javaThreadStatus.hpp"
  36 #include "classfile/moduleEntry.hpp"
  37 #include "classfile/modules.hpp"
  38 #include "classfile/symbolTable.hpp"
  39 #include "classfile/systemDictionary.hpp"
  40 #include "classfile/vmClasses.hpp"
  41 #include "classfile/vmSymbols.hpp"
  42 #include "compiler/compiler_globals.hpp"
  43 #include "gc/shared/collectedHeap.hpp"
  44 #include "gc/shared/gcLocker.inline.hpp"
  45 #include "gc/shared/stringdedup/stringDedup.hpp"
  46 #include "interpreter/linkResolver.hpp"
  47 #include "jni.h"
  48 #include "jvm.h"
  49 #include "logging/log.hpp"
  50 #include "memory/allocation.hpp"
  51 #include "memory/allocation.inline.hpp"
  52 #include "memory/oopFactory.hpp"
  53 #include "memory/resourceArea.hpp"
  54 #include "memory/universe.hpp"
  55 #include "nmt/memTracker.hpp"
  56 #include "oops/access.inline.hpp"
  57 #include "oops/arrayOop.hpp"
  58 #include "oops/flatArrayOop.inline.hpp"
  59 #include "oops/inlineKlass.inline.hpp"
  60 #include "oops/instanceKlass.inline.hpp"
  61 #include "oops/instanceOop.hpp"
  62 #include "oops/klass.inline.hpp"
  63 #include "oops/markWord.hpp"
  64 #include "oops/method.hpp"
  65 #include "oops/objArrayKlass.hpp"
  66 #include "oops/objArrayOop.inline.hpp"
  67 #include "oops/oop.inline.hpp"
  68 #include "oops/symbol.hpp"
  69 #include "oops/typeArrayKlass.hpp"
  70 #include "oops/typeArrayOop.inline.hpp"
  71 #include "prims/jniCheck.hpp"
  72 #include "prims/jniExport.hpp"
  73 #include "prims/jniFastGetField.hpp"
  74 #include "prims/jvm_misc.hpp"
  75 #include "prims/jvmtiExport.hpp"
  76 #include "prims/jvmtiThreadState.hpp"
  77 #include "runtime/arguments.hpp"
  78 #include "runtime/atomic.hpp"
  79 #include "runtime/fieldDescriptor.inline.hpp"
  80 #include "runtime/handles.inline.hpp"
  81 #include "runtime/interfaceSupport.inline.hpp"
  82 #include "runtime/java.hpp"
  83 #include "runtime/javaCalls.hpp"
  84 #include "runtime/javaThread.inline.hpp"
  85 #include "runtime/jfieldIDWorkaround.hpp"
  86 #include "runtime/jniHandles.inline.hpp"
  87 #include "runtime/reflection.hpp"
  88 #include "runtime/safepointVerifiers.hpp"
  89 #include "runtime/sharedRuntime.hpp"
  90 #include "runtime/signature.hpp"
  91 #include "runtime/synchronizer.hpp"
  92 #include "runtime/thread.inline.hpp"
  93 #include "runtime/vmOperations.hpp"
  94 #include "services/runtimeService.hpp"
  95 #include "utilities/defaultStream.hpp"
  96 #include "utilities/dtrace.hpp"
  97 #include "utilities/events.hpp"
  98 #include "utilities/macros.hpp"
  99 #include "utilities/vmError.hpp"
 100 #if INCLUDE_JVMCI
 101 #include "jvmci/jvmciCompiler.hpp"
 102 #endif
 103 #if INCLUDE_JFR
 104 #include "jfr/jfr.hpp"
 105 #endif
 106 
 107 static jint CurrentVersion = JNI_VERSION_21;
 108 
 109 #if defined(_WIN32) && !defined(USE_VECTORED_EXCEPTION_HANDLING)
 110 extern LONG WINAPI topLevelExceptionFilter(_EXCEPTION_POINTERS* );
 111 #endif
 112 
 113 // The DT_RETURN_MARK macros create a scoped object to fire the dtrace
 114 // '-return' probe regardless of the return path is taken out of the function.
 115 // Methods that have multiple return paths use this to avoid having to
 116 // instrument each return path.  Methods that use CHECK or THROW must use this
 117 // since those macros can cause an immediate uninstrumented return.
 118 //
 119 // In order to get the return value, a reference to the variable containing
 120 // the return value must be passed to the constructor of the object, and
 121 // the return value must be set before return (since the mark object has
 122 // a reference to it).
 123 //
 124 // Example:
 125 // DT_RETURN_MARK_DECL(SomeFunc, int);
 126 // JNI_ENTRY(int, SomeFunc, ...)
 127 //   int return_value = 0;
 128 //   DT_RETURN_MARK(SomeFunc, int, (const int&)return_value);
 129 //   foo(CHECK_0)
 130 //   return_value = 5;
 131 //   return return_value;
 132 // JNI_END
 133 #define DT_RETURN_MARK_DECL(name, type, probe)                             \
 134   DTRACE_ONLY(                                                             \
 135     class DTraceReturnProbeMark_##name {                                   \
 136      public:                                                               \
 137       const type& _ret_ref;                                                \
 138       DTraceReturnProbeMark_##name(const type& v) : _ret_ref(v) {}         \
 139       ~DTraceReturnProbeMark_##name() {                                    \
 140         probe;                                                             \
 141       }                                                                    \
 142     }                                                                      \
 143   )
 144 // Void functions are simpler since there's no return value
 145 #define DT_VOID_RETURN_MARK_DECL(name, probe)                              \
 146   DTRACE_ONLY(                                                             \
 147     class DTraceReturnProbeMark_##name {                                   \
 148      public:                                                               \
 149       ~DTraceReturnProbeMark_##name() {                                    \
 150         probe;                                                             \
 151       }                                                                    \
 152     }                                                                      \
 153   )
 154 
 155 // Place these macros in the function to mark the return.  Non-void
 156 // functions need the type and address of the return value.
 157 #define DT_RETURN_MARK(name, type, ref) \
 158   DTRACE_ONLY( DTraceReturnProbeMark_##name dtrace_return_mark(ref) )
 159 #define DT_VOID_RETURN_MARK(name) \
 160   DTRACE_ONLY( DTraceReturnProbeMark_##name dtrace_return_mark )
 161 
 162 
 163 // Use these to select distinct code for floating-point vs. non-floating point
 164 // situations.  Used from within common macros where we need slightly
 165 // different behavior for Float/Double
 166 #define FP_SELECT_Boolean(intcode, fpcode) intcode
 167 #define FP_SELECT_Byte(intcode, fpcode)    intcode
 168 #define FP_SELECT_Char(intcode, fpcode)    intcode
 169 #define FP_SELECT_Short(intcode, fpcode)   intcode
 170 #define FP_SELECT_Object(intcode, fpcode)  intcode
 171 #define FP_SELECT_Int(intcode, fpcode)     intcode
 172 #define FP_SELECT_Long(intcode, fpcode)    intcode
 173 #define FP_SELECT_Float(intcode, fpcode)   fpcode
 174 #define FP_SELECT_Double(intcode, fpcode)  fpcode
 175 #define FP_SELECT(TypeName, intcode, fpcode) \
 176   FP_SELECT_##TypeName(intcode, fpcode)
 177 
 178 // Choose DT_RETURN_MARK macros  based on the type: float/double -> void
 179 // (dtrace doesn't do FP yet)
 180 #define DT_RETURN_MARK_DECL_FOR(TypeName, name, type, probe)    \
 181   FP_SELECT(TypeName, \
 182     DT_RETURN_MARK_DECL(name, type, probe), DT_VOID_RETURN_MARK_DECL(name, probe) )
 183 #define DT_RETURN_MARK_FOR(TypeName, name, type, ref) \
 184   FP_SELECT(TypeName, \
 185     DT_RETURN_MARK(name, type, ref), DT_VOID_RETURN_MARK(name) )
 186 
 187 
 188 // out-of-line helpers for class jfieldIDWorkaround:
 189 
 190 bool jfieldIDWorkaround::is_valid_jfieldID(Klass* k, jfieldID id) {
 191   if (jfieldIDWorkaround::is_instance_jfieldID(k, id)) {
 192     uintptr_t as_uint = (uintptr_t) id;
 193     int offset = raw_instance_offset(id);
 194     if (is_checked_jfieldID(id)) {
 195       if (!klass_hash_ok(k, id)) {
 196         return false;
 197       }
 198     }
 199     return InstanceKlass::cast(k)->contains_field_offset(offset);
 200   } else {
 201     JNIid* result = (JNIid*) id;
 202 #ifdef ASSERT
 203     return result != nullptr && result->is_static_field_id();
 204 #else
 205     return result != nullptr;
 206 #endif
 207   }
 208 }
 209 
 210 
 211 intptr_t jfieldIDWorkaround::encode_klass_hash(Klass* k, int offset) {
 212   if (offset <= small_offset_mask) {
 213     Klass* field_klass = k;
 214     Klass* super_klass = field_klass->super();
 215     // With compressed oops the most super class with nonstatic fields would
 216     // be the owner of fields embedded in the header.
 217     while (InstanceKlass::cast(super_klass)->has_nonstatic_fields() &&
 218            InstanceKlass::cast(super_klass)->contains_field_offset(offset)) {
 219       field_klass = super_klass;   // super contains the field also
 220       super_klass = field_klass->super();
 221     }
 222     debug_only(NoSafepointVerifier nosafepoint;)
 223     uintptr_t klass_hash = field_klass->identity_hash();
 224     return ((klass_hash & klass_mask) << klass_shift) | checked_mask_in_place;
 225   } else {
 226 #if 0
 227     #ifndef PRODUCT
 228     {
 229       ResourceMark rm;
 230       warning("VerifyJNIFields: long offset %d in %s", offset, k->external_name());
 231     }
 232     #endif
 233 #endif
 234     return 0;
 235   }
 236 }
 237 
 238 bool jfieldIDWorkaround::klass_hash_ok(Klass* k, jfieldID id) {
 239   uintptr_t as_uint = (uintptr_t) id;
 240   intptr_t klass_hash = (as_uint >> klass_shift) & klass_mask;
 241   do {
 242     debug_only(NoSafepointVerifier nosafepoint;)
 243     // Could use a non-blocking query for identity_hash here...
 244     if ((k->identity_hash() & klass_mask) == klass_hash)
 245       return true;
 246     k = k->super();
 247   } while (k != nullptr);
 248   return false;
 249 }
 250 
 251 void jfieldIDWorkaround::verify_instance_jfieldID(Klass* k, jfieldID id) {
 252   guarantee(jfieldIDWorkaround::is_instance_jfieldID(k, id), "must be an instance field" );
 253   uintptr_t as_uint = (uintptr_t) id;
 254   int offset = raw_instance_offset(id);
 255   if (VerifyJNIFields) {
 256     if (is_checked_jfieldID(id)) {
 257       guarantee(klass_hash_ok(k, id),
 258     "Bug in native code: jfieldID class must match object");
 259     } else {
 260 #if 0
 261       #ifndef PRODUCT
 262       if (Verbose) {
 263   ResourceMark rm;
 264   warning("VerifyJNIFields: unverified offset %d for %s", offset, k->external_name());
 265       }
 266       #endif
 267 #endif
 268     }
 269   }
 270   guarantee(InstanceKlass::cast(k)->contains_field_offset(offset),
 271       "Bug in native code: jfieldID offset must address interior of object");
 272 }
 273 
 274 // Implementation of JNI entries
 275 
 276 DT_RETURN_MARK_DECL(DefineClass, jclass
 277                     , HOTSPOT_JNI_DEFINECLASS_RETURN(_ret_ref));
 278 
 279 JNI_ENTRY(jclass, jni_DefineClass(JNIEnv *env, const char *name, jobject loaderRef,
 280                                   const jbyte *buf, jsize bufLen))
 281   HOTSPOT_JNI_DEFINECLASS_ENTRY(
 282     env, (char*) name, loaderRef, (char*) buf, bufLen);
 283 
 284   jclass cls = nullptr;
 285   DT_RETURN_MARK(DefineClass, jclass, (const jclass&)cls);
 286 
 287   // Class resolution will get the class name from the .class stream if the name is null.
 288   TempNewSymbol class_name = name == nullptr ? nullptr :
 289     SystemDictionary::class_name_symbol(name, vmSymbols::java_lang_NoClassDefFoundError(),
 290                                         CHECK_NULL);
 291 
 292   ResourceMark rm(THREAD);
 293   ClassFileStream st((u1*)buf, bufLen, nullptr, ClassFileStream::verify);
 294   Handle class_loader (THREAD, JNIHandles::resolve(loaderRef));
 295   Handle protection_domain;
 296   ClassLoadInfo cl_info(protection_domain);
 297   Klass* k = SystemDictionary::resolve_from_stream(&st, class_name,
 298                                                    class_loader,
 299                                                    cl_info,
 300                                                    CHECK_NULL);
 301 
 302   if (log_is_enabled(Debug, class, resolve)) {
 303     trace_class_resolution(k);
 304   }
 305 
 306   cls = (jclass)JNIHandles::make_local(THREAD, k->java_mirror());
 307   return cls;
 308 JNI_END
 309 
 310 
 311 
 312 DT_RETURN_MARK_DECL(FindClass, jclass
 313                     , HOTSPOT_JNI_FINDCLASS_RETURN(_ret_ref));
 314 
 315 JNI_ENTRY(jclass, jni_FindClass(JNIEnv *env, const char *name))
 316   HOTSPOT_JNI_FINDCLASS_ENTRY(env, (char *)name);
 317 
 318   jclass result = nullptr;
 319   DT_RETURN_MARK(FindClass, jclass, (const jclass&)result);
 320 
 321   // This should be ClassNotFoundException imo.
 322   TempNewSymbol class_name =
 323     SystemDictionary::class_name_symbol(name, vmSymbols::java_lang_NoClassDefFoundError(),
 324                                         CHECK_NULL);
 325 
 326   //%note jni_3
 327   Handle protection_domain;
 328   // Find calling class
 329   Klass* k = thread->security_get_caller_class(0);
 330   // default to the system loader when no context
 331   Handle loader(THREAD, SystemDictionary::java_system_loader());
 332   if (k != nullptr) {
 333     // Special handling to make sure JNI_OnLoad and JNI_OnUnload are executed
 334     // in the correct class context.
 335     if (k->class_loader() == nullptr &&
 336         k->name() == vmSymbols::jdk_internal_loader_NativeLibraries()) {
 337       JavaValue result(T_OBJECT);
 338       JavaCalls::call_static(&result, k,
 339                              vmSymbols::getFromClass_name(),
 340                              vmSymbols::void_class_signature(),
 341                              CHECK_NULL);
 342       // When invoked from JNI_OnLoad, NativeLibraries::getFromClass returns
 343       // a non-null Class object.  When invoked from JNI_OnUnload,
 344       // it will return null to indicate no context.
 345       oop mirror = result.get_oop();
 346       if (mirror != nullptr) {
 347         Klass* fromClass = java_lang_Class::as_Klass(mirror);
 348         loader = Handle(THREAD, fromClass->class_loader());
 349         protection_domain = Handle(THREAD, fromClass->protection_domain());
 350       }
 351     } else {
 352       loader = Handle(THREAD, k->class_loader());
 353     }
 354   }
 355 
 356   result = find_class_from_class_loader(env, class_name, true, loader,
 357                                         protection_domain, true, thread);
 358 
 359   if (log_is_enabled(Debug, class, resolve) && result != nullptr) {
 360     trace_class_resolution(java_lang_Class::as_Klass(JNIHandles::resolve_non_null(result)));
 361   }
 362 
 363   return result;
 364 JNI_END
 365 
 366 DT_RETURN_MARK_DECL(FromReflectedMethod, jmethodID
 367                     , HOTSPOT_JNI_FROMREFLECTEDMETHOD_RETURN((uintptr_t)_ret_ref));
 368 
 369 JNI_ENTRY(jmethodID, jni_FromReflectedMethod(JNIEnv *env, jobject method))
 370   HOTSPOT_JNI_FROMREFLECTEDMETHOD_ENTRY(env, method);
 371 
 372   jmethodID ret = nullptr;
 373   DT_RETURN_MARK(FromReflectedMethod, jmethodID, (const jmethodID&)ret);
 374 
 375   // method is a handle to a java.lang.reflect.Method object
 376   oop reflected  = JNIHandles::resolve_non_null(method);
 377   oop mirror     = nullptr;
 378   int slot       = 0;
 379 
 380   if (reflected->klass() == vmClasses::reflect_Constructor_klass()) {
 381     mirror = java_lang_reflect_Constructor::clazz(reflected);
 382     slot   = java_lang_reflect_Constructor::slot(reflected);
 383   } else {
 384     assert(reflected->klass() == vmClasses::reflect_Method_klass(), "wrong type");
 385     mirror = java_lang_reflect_Method::clazz(reflected);
 386     slot   = java_lang_reflect_Method::slot(reflected);
 387   }
 388   Klass* k1 = java_lang_Class::as_Klass(mirror);
 389 
 390   // Make sure class is initialized before handing id's out to methods
 391   k1->initialize(CHECK_NULL);
 392   Method* m = InstanceKlass::cast(k1)->method_with_idnum(slot);
 393   ret = m==nullptr? nullptr : m->jmethod_id();  // return null if reflected method deleted
 394   return ret;
 395 JNI_END
 396 
 397 DT_RETURN_MARK_DECL(FromReflectedField, jfieldID
 398                     , HOTSPOT_JNI_FROMREFLECTEDFIELD_RETURN((uintptr_t)_ret_ref));
 399 
 400 JNI_ENTRY(jfieldID, jni_FromReflectedField(JNIEnv *env, jobject field))
 401   HOTSPOT_JNI_FROMREFLECTEDFIELD_ENTRY(env, field);
 402 
 403   jfieldID ret = nullptr;
 404   DT_RETURN_MARK(FromReflectedField, jfieldID, (const jfieldID&)ret);
 405 
 406   // field is a handle to a java.lang.reflect.Field object
 407   oop reflected   = JNIHandles::resolve_non_null(field);
 408   oop mirror      = java_lang_reflect_Field::clazz(reflected);
 409   Klass* k1       = java_lang_Class::as_Klass(mirror);
 410   int slot        = java_lang_reflect_Field::slot(reflected);
 411   int modifiers   = java_lang_reflect_Field::modifiers(reflected);
 412 
 413   // Make sure class is initialized before handing id's out to fields
 414   k1->initialize(CHECK_NULL);
 415 
 416   // First check if this is a static field
 417   if (modifiers & JVM_ACC_STATIC) {
 418     int offset = InstanceKlass::cast(k1)->field_offset( slot );
 419     JNIid* id = InstanceKlass::cast(k1)->jni_id_for(offset);
 420     assert(id != nullptr, "corrupt Field object");
 421     debug_only(id->set_is_static_field_id();)
 422     // A jfieldID for a static field is a JNIid specifying the field holder and the offset within the Klass*
 423     ret = jfieldIDWorkaround::to_static_jfieldID(id);
 424     return ret;
 425   }
 426 
 427   // The slot is the index of the field description in the field-array
 428   // The jfieldID is the offset of the field within the object
 429   // It may also have hash bits for k, if VerifyJNIFields is turned on.
 430   int offset = InstanceKlass::cast(k1)->field_offset( slot );
 431   bool is_flat = InstanceKlass::cast(k1)->field_is_flat(slot);
 432   assert(InstanceKlass::cast(k1)->contains_field_offset(offset), "stay within object");
 433   ret = jfieldIDWorkaround::to_instance_jfieldID(k1, offset, is_flat);
 434   return ret;
 435 JNI_END
 436 
 437 
 438 DT_RETURN_MARK_DECL(ToReflectedMethod, jobject
 439                     , HOTSPOT_JNI_TOREFLECTEDMETHOD_RETURN(_ret_ref));
 440 
 441 JNI_ENTRY(jobject, jni_ToReflectedMethod(JNIEnv *env, jclass cls, jmethodID method_id, jboolean isStatic))
 442   HOTSPOT_JNI_TOREFLECTEDMETHOD_ENTRY(env, cls, (uintptr_t) method_id, isStatic);
 443 
 444   jobject ret = nullptr;
 445   DT_RETURN_MARK(ToReflectedMethod, jobject, (const jobject&)ret);
 446 
 447   methodHandle m (THREAD, Method::resolve_jmethod_id(method_id));
 448   assert(m->is_static() == (isStatic != 0), "jni_ToReflectedMethod access flags doesn't match");
 449   oop reflection_method;
 450   if (m->is_object_constructor() || m->is_static_vnew_factory()) {
 451     reflection_method = Reflection::new_constructor(m, CHECK_NULL);
 452   } else {
 453     reflection_method = Reflection::new_method(m, false, CHECK_NULL);
 454   }
 455   ret = JNIHandles::make_local(THREAD, reflection_method);
 456   return ret;
 457 JNI_END
 458 
 459 DT_RETURN_MARK_DECL(GetSuperclass, jclass
 460                     , HOTSPOT_JNI_GETSUPERCLASS_RETURN(_ret_ref));
 461 
 462 JNI_ENTRY(jclass, jni_GetSuperclass(JNIEnv *env, jclass sub))
 463   HOTSPOT_JNI_GETSUPERCLASS_ENTRY(env, sub);
 464 
 465   jclass obj = nullptr;
 466   DT_RETURN_MARK(GetSuperclass, jclass, (const jclass&)obj);
 467 
 468   oop mirror = JNIHandles::resolve_non_null(sub);
 469   // primitive classes return null
 470   if (java_lang_Class::is_primitive(mirror)) return nullptr;
 471 
 472   // Rules of Class.getSuperClass as implemented by KLass::java_super:
 473   // arrays return Object
 474   // interfaces return null
 475   // proper classes return Klass::super()
 476   Klass* k = java_lang_Class::as_Klass(mirror);
 477   if (k->is_interface()) return nullptr;
 478 
 479   // return mirror for superclass
 480   Klass* super = k->java_super();
 481   // super2 is the value computed by the compiler's getSuperClass intrinsic:
 482   debug_only(Klass* super2 = ( k->is_array_klass()
 483                                  ? vmClasses::Object_klass()
 484                                  : k->super() ) );
 485   assert(super == super2,
 486          "java_super computation depends on interface, array, other super");
 487   obj = (super == nullptr) ? nullptr : (jclass) JNIHandles::make_local(THREAD, super->java_mirror());
 488   return obj;
 489 JNI_END
 490 
 491 JNI_ENTRY_NO_PRESERVE(jboolean, jni_IsAssignableFrom(JNIEnv *env, jclass sub, jclass super))
 492   HOTSPOT_JNI_ISASSIGNABLEFROM_ENTRY(env, sub, super);
 493 
 494   oop sub_mirror   = JNIHandles::resolve_non_null(sub);
 495   oop super_mirror = JNIHandles::resolve_non_null(super);
 496   if (java_lang_Class::is_primitive(sub_mirror) ||
 497       java_lang_Class::is_primitive(super_mirror)) {
 498     jboolean ret = (sub_mirror == super_mirror);
 499 
 500     HOTSPOT_JNI_ISASSIGNABLEFROM_RETURN(ret);
 501     return ret;
 502   }
 503   Klass* sub_klass   = java_lang_Class::as_Klass(sub_mirror);
 504   Klass* super_klass = java_lang_Class::as_Klass(super_mirror);
 505   assert(sub_klass != nullptr && super_klass != nullptr, "invalid arguments to jni_IsAssignableFrom");
 506   jboolean ret;
 507   if (sub_klass == super_klass && sub_klass->is_inline_klass()) {
 508     // val type is a subtype of ref type
 509     InlineKlass* ik = InlineKlass::cast(sub_klass);
 510     if (sub_mirror == super_mirror || (ik->val_mirror() == sub_mirror && ik->ref_mirror() == super_mirror)) {
 511       ret = JNI_TRUE;
 512     } else {
 513       ret = JNI_FALSE;
 514     }
 515   } else {
 516     ret = sub_klass->is_subtype_of(super_klass) ? JNI_TRUE : JNI_FALSE;
 517   }
 518   HOTSPOT_JNI_ISASSIGNABLEFROM_RETURN(ret);
 519   return ret;
 520 JNI_END
 521 
 522 
 523 DT_RETURN_MARK_DECL(Throw, jint
 524                     , HOTSPOT_JNI_THROW_RETURN(_ret_ref));
 525 
 526 JNI_ENTRY(jint, jni_Throw(JNIEnv *env, jthrowable obj))
 527   HOTSPOT_JNI_THROW_ENTRY(env, obj);
 528 
 529   jint ret = JNI_OK;
 530   DT_RETURN_MARK(Throw, jint, (const jint&)ret);
 531 
 532   THROW_OOP_(JNIHandles::resolve(obj), JNI_OK);
 533   ShouldNotReachHere();
 534   return 0;  // Mute compiler.
 535 JNI_END
 536 
 537 
 538 DT_RETURN_MARK_DECL(ThrowNew, jint
 539                     , HOTSPOT_JNI_THROWNEW_RETURN(_ret_ref));
 540 
 541 JNI_ENTRY(jint, jni_ThrowNew(JNIEnv *env, jclass clazz, const char *message))
 542   HOTSPOT_JNI_THROWNEW_ENTRY(env, clazz, (char *) message);
 543 
 544   jint ret = JNI_OK;
 545   DT_RETURN_MARK(ThrowNew, jint, (const jint&)ret);
 546 
 547   InstanceKlass* k = InstanceKlass::cast(java_lang_Class::as_Klass(JNIHandles::resolve_non_null(clazz)));
 548   Symbol*  name = k->name();
 549   Handle class_loader (THREAD,  k->class_loader());
 550   Handle protection_domain (THREAD, k->protection_domain());
 551   THROW_MSG_LOADER_(name, (char *)message, class_loader, protection_domain, JNI_OK);
 552   ShouldNotReachHere();
 553   return 0;  // Mute compiler.
 554 JNI_END
 555 
 556 
 557 // JNI functions only transform a pending async exception to a synchronous
 558 // exception in ExceptionOccurred and ExceptionCheck calls, since
 559 // delivering an async exception in other places won't change the native
 560 // code's control flow and would be harmful when native code further calls
 561 // JNI functions with a pending exception. Async exception is also checked
 562 // during the call, so ExceptionOccurred/ExceptionCheck won't return
 563 // false but deliver the async exception at the very end during
 564 // state transition.
 565 
 566 static void jni_check_async_exceptions(JavaThread *thread) {
 567   assert(thread == Thread::current(), "must be itself");
 568   if (thread->has_async_exception_condition()) {
 569     SafepointMechanism::process_if_requested_with_exit_check(thread, true /* check asyncs */);
 570   }
 571 }
 572 
 573 JNI_ENTRY_NO_PRESERVE(jthrowable, jni_ExceptionOccurred(JNIEnv *env))
 574   HOTSPOT_JNI_EXCEPTIONOCCURRED_ENTRY(env);
 575 
 576   jni_check_async_exceptions(thread);
 577   oop exception = thread->pending_exception();
 578   jthrowable ret = (jthrowable) JNIHandles::make_local(THREAD, exception);
 579 
 580   HOTSPOT_JNI_EXCEPTIONOCCURRED_RETURN(ret);
 581   return ret;
 582 JNI_END
 583 
 584 
 585 JNI_ENTRY_NO_PRESERVE(void, jni_ExceptionDescribe(JNIEnv *env))
 586   HOTSPOT_JNI_EXCEPTIONDESCRIBE_ENTRY(env);
 587 
 588   if (thread->has_pending_exception()) {
 589     Handle ex(thread, thread->pending_exception());
 590     thread->clear_pending_exception();
 591     jio_fprintf(defaultStream::error_stream(), "Exception ");
 592     if (thread != nullptr && thread->threadObj() != nullptr) {
 593       ResourceMark rm(THREAD);
 594       jio_fprintf(defaultStream::error_stream(),
 595                   "in thread \"%s\" ", thread->name());
 596     }
 597     if (ex->is_a(vmClasses::Throwable_klass())) {
 598       JavaValue result(T_VOID);
 599       JavaCalls::call_virtual(&result,
 600                               ex,
 601                               vmClasses::Throwable_klass(),
 602                               vmSymbols::printStackTrace_name(),
 603                               vmSymbols::void_method_signature(),
 604                               THREAD);
 605       // If an exception is thrown in the call it gets thrown away. Not much
 606       // we can do with it. The native code that calls this, does not check
 607       // for the exception - hence, it might still be in the thread when DestroyVM gets
 608       // called, potentially causing a few asserts to trigger - since no pending exception
 609       // is expected.
 610       CLEAR_PENDING_EXCEPTION;
 611     } else {
 612       ResourceMark rm(THREAD);
 613       jio_fprintf(defaultStream::error_stream(),
 614                   ". Uncaught exception of type %s.",
 615                   ex->klass()->external_name());
 616     }
 617   }
 618 
 619   HOTSPOT_JNI_EXCEPTIONDESCRIBE_RETURN();
 620 JNI_END
 621 
 622 
 623 JNI_ENTRY_NO_PRESERVE(void, jni_ExceptionClear(JNIEnv *env))
 624   HOTSPOT_JNI_EXCEPTIONCLEAR_ENTRY(env);
 625 
 626   // The jni code might be using this API to clear java thrown exception.
 627   // So just mark jvmti thread exception state as exception caught.
 628   JvmtiThreadState *state = JavaThread::current()->jvmti_thread_state();
 629   if (state != nullptr && state->is_exception_detected()) {
 630     state->set_exception_caught();
 631   }
 632   thread->clear_pending_exception();
 633 
 634   HOTSPOT_JNI_EXCEPTIONCLEAR_RETURN();
 635 JNI_END
 636 
 637 
 638 JNI_ENTRY(void, jni_FatalError(JNIEnv *env, const char *msg))
 639   HOTSPOT_JNI_FATALERROR_ENTRY(env, (char *) msg);
 640 
 641   tty->print_cr("FATAL ERROR in native method: %s", msg);
 642   thread->print_jni_stack();
 643   os::abort(); // Dump core and abort
 644 JNI_END
 645 
 646 
 647 JNI_ENTRY(jint, jni_PushLocalFrame(JNIEnv *env, jint capacity))
 648   HOTSPOT_JNI_PUSHLOCALFRAME_ENTRY(env, capacity);
 649 
 650   //%note jni_11
 651   if (capacity < 0 ||
 652       ((MaxJNILocalCapacity > 0) && (capacity > MaxJNILocalCapacity))) {
 653     HOTSPOT_JNI_PUSHLOCALFRAME_RETURN((uint32_t)JNI_ERR);
 654     return JNI_ERR;
 655   }
 656 
 657   thread->push_jni_handle_block();
 658   jint ret = JNI_OK;
 659   HOTSPOT_JNI_PUSHLOCALFRAME_RETURN(ret);
 660   return ret;
 661 JNI_END
 662 
 663 
 664 JNI_ENTRY(jobject, jni_PopLocalFrame(JNIEnv *env, jobject result))
 665   HOTSPOT_JNI_POPLOCALFRAME_ENTRY(env, result);
 666 
 667   //%note jni_11
 668   Handle result_handle(thread, JNIHandles::resolve(result));
 669   JNIHandleBlock* old_handles = thread->active_handles();
 670   JNIHandleBlock* new_handles = old_handles->pop_frame_link();
 671   if (new_handles != nullptr) {
 672     // As a sanity check we only release the handle blocks if the pop_frame_link is not null.
 673     // This way code will still work if PopLocalFrame is called without a corresponding
 674     // PushLocalFrame call. Note that we set the pop_frame_link to null explicitly, otherwise
 675     // the release_block call will release the blocks.
 676     thread->set_active_handles(new_handles);
 677     old_handles->set_pop_frame_link(nullptr);              // clear link we won't release new_handles below
 678     JNIHandleBlock::release_block(old_handles, thread); // may block
 679     result = JNIHandles::make_local(thread, result_handle());
 680   }
 681   HOTSPOT_JNI_POPLOCALFRAME_RETURN(result);
 682   return result;
 683 JNI_END
 684 
 685 
 686 JNI_ENTRY(jobject, jni_NewGlobalRef(JNIEnv *env, jobject ref))
 687   HOTSPOT_JNI_NEWGLOBALREF_ENTRY(env, ref);
 688 
 689   Handle ref_handle(thread, JNIHandles::resolve(ref));
 690   jobject ret = JNIHandles::make_global(ref_handle, AllocFailStrategy::RETURN_NULL);
 691 
 692   HOTSPOT_JNI_NEWGLOBALREF_RETURN(ret);
 693   return ret;
 694 JNI_END
 695 
 696 // Must be JNI_ENTRY (with HandleMark)
 697 JNI_ENTRY_NO_PRESERVE(void, jni_DeleteGlobalRef(JNIEnv *env, jobject ref))
 698   HOTSPOT_JNI_DELETEGLOBALREF_ENTRY(env, ref);
 699 
 700   JNIHandles::destroy_global(ref);
 701 
 702   HOTSPOT_JNI_DELETEGLOBALREF_RETURN();
 703 JNI_END
 704 
 705 JNI_ENTRY_NO_PRESERVE(void, jni_DeleteLocalRef(JNIEnv *env, jobject obj))
 706   HOTSPOT_JNI_DELETELOCALREF_ENTRY(env, obj);
 707 
 708   JNIHandles::destroy_local(obj);
 709 
 710   HOTSPOT_JNI_DELETELOCALREF_RETURN();
 711 JNI_END
 712 
 713 JNI_ENTRY_NO_PRESERVE(jboolean, jni_IsSameObject(JNIEnv *env, jobject r1, jobject r2))
 714   HOTSPOT_JNI_ISSAMEOBJECT_ENTRY(env, r1, r2);
 715 
 716   jboolean ret = JNIHandles::is_same_object(r1, r2) ? JNI_TRUE : JNI_FALSE;
 717 
 718   HOTSPOT_JNI_ISSAMEOBJECT_RETURN(ret);
 719   return ret;
 720 JNI_END
 721 
 722 
 723 JNI_ENTRY(jobject, jni_NewLocalRef(JNIEnv *env, jobject ref))
 724   HOTSPOT_JNI_NEWLOCALREF_ENTRY(env, ref);
 725 
 726   jobject ret = JNIHandles::make_local(THREAD, JNIHandles::resolve(ref),
 727                                        AllocFailStrategy::RETURN_NULL);
 728 
 729   HOTSPOT_JNI_NEWLOCALREF_RETURN(ret);
 730   return ret;
 731 JNI_END
 732 
 733 JNI_LEAF(jint, jni_EnsureLocalCapacity(JNIEnv *env, jint capacity))
 734   HOTSPOT_JNI_ENSURELOCALCAPACITY_ENTRY(env, capacity);
 735 
 736   jint ret;
 737   if (capacity >= 0 &&
 738       ((MaxJNILocalCapacity <= 0) || (capacity <= MaxJNILocalCapacity))) {
 739     ret = JNI_OK;
 740   } else {
 741     ret = JNI_ERR;
 742   }
 743 
 744   HOTSPOT_JNI_ENSURELOCALCAPACITY_RETURN(ret);
 745   return ret;
 746 JNI_END
 747 
 748 // Return the Handle Type
 749 JNI_LEAF(jobjectRefType, jni_GetObjectRefType(JNIEnv *env, jobject obj))
 750   HOTSPOT_JNI_GETOBJECTREFTYPE_ENTRY(env, obj);
 751 
 752   jobjectRefType ret = JNIInvalidRefType;
 753   if (obj != nullptr) {
 754     ret = JNIHandles::handle_type(thread, obj);
 755   }
 756 
 757   HOTSPOT_JNI_GETOBJECTREFTYPE_RETURN((void *) ret);
 758   return ret;
 759 JNI_END
 760 
 761 
 762 class JNI_ArgumentPusher : public SignatureIterator {
 763  protected:
 764   JavaCallArguments*  _arguments;
 765 
 766   void push_int(jint x)         { _arguments->push_int(x); }
 767   void push_long(jlong x)       { _arguments->push_long(x); }
 768   void push_float(jfloat x)     { _arguments->push_float(x); }
 769   void push_double(jdouble x)   { _arguments->push_double(x); }
 770   void push_object(jobject x)   { _arguments->push_jobject(x); }
 771 
 772   void push_boolean(jboolean b) {
 773     // Normalize boolean arguments from native code by converting 1-255 to JNI_TRUE and
 774     // 0 to JNI_FALSE.  Boolean return values from native are normalized the same in
 775     // TemplateInterpreterGenerator::generate_result_handler_for and
 776     // SharedRuntime::generate_native_wrapper.
 777     push_int(b == 0 ? JNI_FALSE : JNI_TRUE);
 778   }
 779 
 780   JNI_ArgumentPusher(Method* method)
 781     : SignatureIterator(method->signature(),
 782                         Fingerprinter(methodHandle(Thread::current(), method)).fingerprint())
 783   {
 784     _arguments = nullptr;
 785   }
 786 
 787  public:
 788   virtual void push_arguments_on(JavaCallArguments* arguments) = 0;
 789 };
 790 
 791 
 792 class JNI_ArgumentPusherVaArg : public JNI_ArgumentPusher {
 793   va_list _ap;
 794 
 795   void set_ap(va_list rap) {
 796     va_copy(_ap, rap);
 797   }
 798 
 799   friend class SignatureIterator;  // so do_parameters_on can call do_type
 800   void do_type(BasicType type) {
 801     switch (type) {
 802     // these are coerced to int when using va_arg
 803     case T_BYTE:
 804     case T_CHAR:
 805     case T_SHORT:
 806     case T_INT:         push_int(va_arg(_ap, jint)); break;
 807     case T_BOOLEAN:     push_boolean((jboolean) va_arg(_ap, jint)); break;
 808 
 809     // each of these paths is exercised by the various jck Call[Static,Nonvirtual,][Void,Int,..]Method[A,V,] tests
 810 
 811     case T_LONG:        push_long(va_arg(_ap, jlong)); break;
 812     // float is coerced to double w/ va_arg
 813     case T_FLOAT:       push_float((jfloat) va_arg(_ap, jdouble)); break;
 814     case T_DOUBLE:      push_double(va_arg(_ap, jdouble)); break;
 815 
 816     case T_ARRAY:
 817     case T_OBJECT:
 818     case T_PRIMITIVE_OBJECT: push_object(va_arg(_ap, jobject)); break;
 819     default:            ShouldNotReachHere();
 820     }
 821   }
 822 
 823  public:
 824   JNI_ArgumentPusherVaArg(jmethodID method_id, va_list rap)
 825       : JNI_ArgumentPusher(Method::resolve_jmethod_id(method_id)) {
 826     set_ap(rap);
 827   }
 828 
 829   ~JNI_ArgumentPusherVaArg() {
 830     va_end(_ap);
 831   }
 832 
 833   virtual void push_arguments_on(JavaCallArguments* arguments) {
 834     _arguments = arguments;
 835     do_parameters_on(this);
 836   }
 837 };
 838 
 839 
 840 class JNI_ArgumentPusherArray : public JNI_ArgumentPusher {
 841  protected:
 842   const jvalue *_ap;
 843 
 844   inline void set_ap(const jvalue *rap) { _ap = rap; }
 845 
 846   friend class SignatureIterator;  // so do_parameters_on can call do_type
 847   void do_type(BasicType type) {
 848     switch (type) {
 849     case T_CHAR:        push_int((_ap++)->c); break;
 850     case T_SHORT:       push_int((_ap++)->s); break;
 851     case T_BYTE:        push_int((_ap++)->b); break;
 852     case T_INT:         push_int((_ap++)->i); break;
 853     case T_BOOLEAN:     push_boolean((_ap++)->z); break;
 854     case T_LONG:        push_long((_ap++)->j); break;
 855     case T_FLOAT:       push_float((_ap++)->f); break;
 856     case T_DOUBLE:      push_double((_ap++)->d); break;
 857     case T_ARRAY:
 858     case T_OBJECT:
 859     case T_PRIMITIVE_OBJECT: push_object((_ap++)->l); break;
 860     default:            ShouldNotReachHere();
 861     }
 862   }
 863 
 864  public:
 865   JNI_ArgumentPusherArray(jmethodID method_id, const jvalue *rap)
 866       : JNI_ArgumentPusher(Method::resolve_jmethod_id(method_id)) {
 867     set_ap(rap);
 868   }
 869 
 870   virtual void push_arguments_on(JavaCallArguments* arguments) {
 871     _arguments = arguments;
 872     do_parameters_on(this);
 873   }
 874 };
 875 
 876 
 877 enum JNICallType {
 878   JNI_STATIC,
 879   JNI_VIRTUAL,
 880   JNI_NONVIRTUAL
 881 };
 882 
 883 
 884 
 885 static void jni_invoke_static(JNIEnv *env, JavaValue* result, jobject receiver, JNICallType call_type, jmethodID method_id, JNI_ArgumentPusher *args, TRAPS) {
 886   methodHandle method(THREAD, Method::resolve_jmethod_id(method_id));
 887 
 888   // Create object to hold arguments for the JavaCall, and associate it with
 889   // the jni parser
 890   ResourceMark rm(THREAD);
 891   int number_of_parameters = method->size_of_parameters();
 892   JavaCallArguments java_args(number_of_parameters);
 893 
 894   assert(method->is_static(), "method should be static");
 895 
 896   // Fill out JavaCallArguments object
 897   args->push_arguments_on(&java_args);
 898   // Initialize result type
 899   result->set_type(args->return_type());
 900 
 901   // Invoke the method. Result is returned as oop.
 902   JavaCalls::call(result, method, &java_args, CHECK);
 903 
 904   // Convert result
 905   if (is_reference_type(result->get_type())) {
 906     result->set_jobject(JNIHandles::make_local(THREAD, result->get_oop()));
 907   }
 908 }
 909 
 910 
 911 static void jni_invoke_nonstatic(JNIEnv *env, JavaValue* result, jobject receiver, JNICallType call_type, jmethodID method_id, JNI_ArgumentPusher *args, TRAPS) {
 912   oop recv = JNIHandles::resolve(receiver);
 913   if (recv == nullptr) {
 914     THROW(vmSymbols::java_lang_NullPointerException());
 915   }
 916   Handle h_recv(THREAD, recv);
 917 
 918   int number_of_parameters;
 919   Method* selected_method;
 920   {
 921     Method* m = Method::resolve_jmethod_id(method_id);
 922     number_of_parameters = m->size_of_parameters();
 923     InstanceKlass* holder = m->method_holder();
 924     if (call_type != JNI_VIRTUAL) {
 925         selected_method = m;
 926     } else if (!m->has_itable_index()) {
 927       // non-interface call -- for that little speed boost, don't handlize
 928       debug_only(NoSafepointVerifier nosafepoint;)
 929       // jni_GetMethodID makes sure class is linked and initialized
 930       // so m should have a valid vtable index.
 931       assert(m->valid_vtable_index(), "no valid vtable index");
 932       int vtbl_index = m->vtable_index();
 933       if (vtbl_index != Method::nonvirtual_vtable_index) {
 934         selected_method = h_recv->klass()->method_at_vtable(vtbl_index);
 935       } else {
 936         // final method
 937         selected_method = m;
 938       }
 939     } else {
 940       // interface call
 941       int itbl_index = m->itable_index();
 942       Klass* k = h_recv->klass();
 943       selected_method = InstanceKlass::cast(k)->method_at_itable(holder, itbl_index, CHECK);
 944     }
 945   }
 946 
 947   methodHandle method(THREAD, selected_method);
 948 
 949   // Create object to hold arguments for the JavaCall, and associate it with
 950   // the jni parser
 951   ResourceMark rm(THREAD);
 952   JavaCallArguments java_args(number_of_parameters);
 953 
 954   // handle arguments
 955   assert(!method->is_static(), "method %s should not be static", method->name_and_sig_as_C_string());
 956   java_args.push_oop(h_recv); // Push jobject handle
 957 
 958   // Fill out JavaCallArguments object
 959   args->push_arguments_on(&java_args);
 960   // Initialize result type
 961   result->set_type(args->return_type());
 962 
 963   // Invoke the method. Result is returned as oop.
 964   JavaCalls::call(result, method, &java_args, CHECK);
 965 
 966   // Convert result
 967   if (is_reference_type(result->get_type())) {
 968     result->set_jobject(JNIHandles::make_local(THREAD, result->get_oop()));
 969   }
 970 }
 971 
 972 DT_RETURN_MARK_DECL(AllocObject, jobject
 973                     , HOTSPOT_JNI_ALLOCOBJECT_RETURN(_ret_ref));
 974 
 975 JNI_ENTRY(jobject, jni_AllocObject(JNIEnv *env, jclass clazz))
 976   HOTSPOT_JNI_ALLOCOBJECT_ENTRY(env, clazz);
 977 
 978   jobject ret = nullptr;
 979   DT_RETURN_MARK(AllocObject, jobject, (const jobject&)ret);
 980 
 981   instanceOop i = InstanceKlass::allocate_instance(JNIHandles::resolve_non_null(clazz), CHECK_NULL);
 982   ret = JNIHandles::make_local(THREAD, i);
 983   return ret;
 984 JNI_END
 985 
 986 DT_RETURN_MARK_DECL(NewObjectA, jobject
 987                     , HOTSPOT_JNI_NEWOBJECTA_RETURN(_ret_ref));
 988 
 989 JNI_ENTRY(jobject, jni_NewObjectA(JNIEnv *env, jclass clazz, jmethodID methodID, const jvalue *args))
 990   HOTSPOT_JNI_NEWOBJECTA_ENTRY(env, clazz, (uintptr_t) methodID);
 991 
 992   jobject obj = nullptr;
 993   DT_RETURN_MARK(NewObjectA, jobject, (const jobject&)obj);
 994 
 995   oop clazzoop = JNIHandles::resolve_non_null(clazz);
 996   Klass* k = java_lang_Class::as_Klass(clazzoop);
 997   if (k == nullptr) {
 998     ResourceMark rm(THREAD);
 999     THROW_(vmSymbols::java_lang_InstantiationException(), nullptr);
1000   }
1001 
1002   if (!k->is_inline_klass()) {
1003     instanceOop i = InstanceKlass::allocate_instance(clazzoop, CHECK_NULL);
1004     obj = JNIHandles::make_local(THREAD, i);
1005     JavaValue jvalue(T_VOID);
1006     JNI_ArgumentPusherArray ap(methodID, args);
1007     jni_invoke_nonstatic(env, &jvalue, obj, JNI_NONVIRTUAL, methodID, &ap, CHECK_NULL);
1008   } else {
1009     JavaValue jvalue(T_PRIMITIVE_OBJECT);
1010     JNI_ArgumentPusherArray ap(methodID, args);
1011     jni_invoke_static(env, &jvalue, nullptr, JNI_STATIC, methodID, &ap, CHECK_NULL);
1012     obj = jvalue.get_jobject();
1013   }
1014   return obj;
1015   JNI_END
1016 
1017 
1018 DT_RETURN_MARK_DECL(NewObjectV, jobject
1019                     , HOTSPOT_JNI_NEWOBJECTV_RETURN(_ret_ref));
1020 
1021 JNI_ENTRY(jobject, jni_NewObjectV(JNIEnv *env, jclass clazz, jmethodID methodID, va_list args))
1022   HOTSPOT_JNI_NEWOBJECTV_ENTRY(env, clazz, (uintptr_t) methodID);
1023 
1024   jobject obj = nullptr;
1025   DT_RETURN_MARK(NewObjectV, jobject, (const jobject&)obj);
1026 
1027   oop clazzoop = JNIHandles::resolve_non_null(clazz);
1028   Klass* k = java_lang_Class::as_Klass(clazzoop);
1029   if (k == nullptr) {
1030     ResourceMark rm(THREAD);
1031     THROW_(vmSymbols::java_lang_InstantiationException(), nullptr);
1032   }
1033 
1034   if (!k->is_inline_klass()) {
1035     instanceOop i = InstanceKlass::allocate_instance(clazzoop, CHECK_NULL);
1036     obj = JNIHandles::make_local(THREAD, i);
1037     JavaValue jvalue(T_VOID);
1038     JNI_ArgumentPusherVaArg ap(methodID, args);
1039     jni_invoke_nonstatic(env, &jvalue, obj, JNI_NONVIRTUAL, methodID, &ap, CHECK_NULL);
1040   } else {
1041     JavaValue jvalue(T_PRIMITIVE_OBJECT);
1042     JNI_ArgumentPusherVaArg ap(methodID, args);
1043     jni_invoke_static(env, &jvalue, nullptr, JNI_STATIC, methodID, &ap, CHECK_NULL);
1044     obj = jvalue.get_jobject();
1045   }
1046   return obj;
1047 JNI_END
1048 
1049 
1050 DT_RETURN_MARK_DECL(NewObject, jobject
1051                     , HOTSPOT_JNI_NEWOBJECT_RETURN(_ret_ref));
1052 
1053 JNI_ENTRY(jobject, jni_NewObject(JNIEnv *env, jclass clazz, jmethodID methodID, ...))
1054   HOTSPOT_JNI_NEWOBJECT_ENTRY(env, clazz, (uintptr_t) methodID);
1055 
1056   jobject obj = nullptr;
1057   DT_RETURN_MARK(NewObject, jobject, (const jobject&)obj);
1058 
1059   oop clazzoop = JNIHandles::resolve_non_null(clazz);
1060   Klass* k = java_lang_Class::as_Klass(clazzoop);
1061   if (k == nullptr) {
1062     ResourceMark rm(THREAD);
1063     THROW_(vmSymbols::java_lang_InstantiationException(), nullptr);
1064   }
1065 
1066   if (!k->is_inline_klass()) {
1067     instanceOop i = InstanceKlass::allocate_instance(clazzoop, CHECK_NULL);
1068     obj = JNIHandles::make_local(THREAD, i);
1069     va_list args;
1070     va_start(args, methodID);
1071     JavaValue jvalue(T_VOID);
1072     JNI_ArgumentPusherVaArg ap(methodID, args);
1073     jni_invoke_nonstatic(env, &jvalue, obj, JNI_NONVIRTUAL, methodID, &ap, CHECK_NULL);
1074     va_end(args);
1075   } else {
1076     va_list args;
1077     va_start(args, methodID);
1078     JavaValue jvalue(T_PRIMITIVE_OBJECT);
1079     JNI_ArgumentPusherVaArg ap(methodID, args);
1080     jni_invoke_static(env, &jvalue, nullptr, JNI_STATIC, methodID, &ap, CHECK_NULL);
1081     va_end(args);
1082     obj = jvalue.get_jobject();
1083   }
1084   return obj;
1085 JNI_END
1086 
1087 
1088 JNI_ENTRY(jclass, jni_GetObjectClass(JNIEnv *env, jobject obj))
1089   HOTSPOT_JNI_GETOBJECTCLASS_ENTRY(env, obj);
1090 
1091   Klass* k = JNIHandles::resolve_non_null(obj)->klass();
1092   jclass ret =
1093     (jclass) JNIHandles::make_local(THREAD, k->java_mirror());
1094 
1095   HOTSPOT_JNI_GETOBJECTCLASS_RETURN(ret);
1096   return ret;
1097 JNI_END
1098 
1099 JNI_ENTRY_NO_PRESERVE(jboolean, jni_IsInstanceOf(JNIEnv *env, jobject obj, jclass clazz))
1100   HOTSPOT_JNI_ISINSTANCEOF_ENTRY(env, obj, clazz);
1101 
1102   jboolean ret = JNI_TRUE;
1103   if (obj != nullptr) {
1104     ret = JNI_FALSE;
1105     Klass* k = java_lang_Class::as_Klass(
1106       JNIHandles::resolve_non_null(clazz));
1107     if (k != nullptr) {
1108       ret = JNIHandles::resolve_non_null(obj)->is_a(k) ? JNI_TRUE : JNI_FALSE;
1109     }
1110   }
1111 
1112   HOTSPOT_JNI_ISINSTANCEOF_RETURN(ret);
1113   return ret;
1114 JNI_END
1115 
1116 
1117 static jmethodID get_method_id(JNIEnv *env, jclass clazz, const char *name_str,
1118                                const char *sig, bool is_static, TRAPS) {
1119   // %%%% This code should probably just call into a method in the LinkResolver
1120   //
1121   // The class should have been loaded (we have an instance of the class
1122   // passed in) so the method and signature should already be in the symbol
1123   // table.  If they're not there, the method doesn't exist.
1124   const char *name_to_probe = (name_str == nullptr)
1125                         ? vmSymbols::object_initializer_name()->as_C_string()
1126                         : name_str;
1127   TempNewSymbol name = SymbolTable::probe(name_to_probe, (int)strlen(name_to_probe));
1128   TempNewSymbol signature = SymbolTable::probe(sig, (int)strlen(sig));
1129 
1130   if (name == nullptr || signature == nullptr) {
1131     THROW_MSG_0(vmSymbols::java_lang_NoSuchMethodError(), name_str);
1132   }
1133 
1134   oop mirror = JNIHandles::resolve_non_null(clazz);
1135   Klass* klass = java_lang_Class::as_Klass(mirror);
1136 
1137   // Throw a NoSuchMethodError exception if we have an instance of a
1138   // primitive java.lang.Class
1139   if (java_lang_Class::is_primitive(mirror)) {
1140     ResourceMark rm(THREAD);
1141     THROW_MSG_0(vmSymbols::java_lang_NoSuchMethodError(), err_msg("%s%s.%s%s", is_static ? "static " : "", klass->signature_name(), name_str, sig));
1142   }
1143 
1144   // Make sure class is linked and initialized before handing id's out to
1145   // Method*s.
1146   klass->initialize(CHECK_NULL);
1147 
1148   Method* m;
1149   if (name == vmSymbols::object_initializer_name() ||
1150       name == vmSymbols::class_initializer_name()) {
1151     // Never search superclasses for constructors
1152     if (klass->is_instance_klass()) {
1153       m = InstanceKlass::cast(klass)->find_method(name, signature);
1154     } else {
1155       m = nullptr;
1156     }
1157   } else {
1158     m = klass->lookup_method(name, signature);
1159     if (m == nullptr &&  klass->is_instance_klass()) {
1160       m = InstanceKlass::cast(klass)->lookup_method_in_ordered_interfaces(name, signature);
1161     }
1162   }
1163   if (m == nullptr || (m->is_static() != is_static)) {
1164     ResourceMark rm(THREAD);
1165     THROW_MSG_0(vmSymbols::java_lang_NoSuchMethodError(), err_msg("%s%s.%s%s", is_static ? "static " : "", klass->signature_name(), name_str, sig));
1166   }
1167   return m->jmethod_id();
1168 }
1169 
1170 
1171 JNI_ENTRY(jmethodID, jni_GetMethodID(JNIEnv *env, jclass clazz,
1172           const char *name, const char *sig))
1173   HOTSPOT_JNI_GETMETHODID_ENTRY(env, clazz, (char *) name, (char *) sig);
1174   jmethodID ret = get_method_id(env, clazz, name, sig, false, thread);
1175   HOTSPOT_JNI_GETMETHODID_RETURN((uintptr_t) ret);
1176   return ret;
1177 JNI_END
1178 
1179 
1180 JNI_ENTRY(jmethodID, jni_GetStaticMethodID(JNIEnv *env, jclass clazz,
1181           const char *name, const char *sig))
1182   HOTSPOT_JNI_GETSTATICMETHODID_ENTRY(env, (char *) clazz, (char *) name, (char *)sig);
1183   jmethodID ret = get_method_id(env, clazz, name, sig, true, thread);
1184   HOTSPOT_JNI_GETSTATICMETHODID_RETURN((uintptr_t) ret);
1185   return ret;
1186 JNI_END
1187 
1188 
1189 
1190 //
1191 // Calling Methods
1192 //
1193 
1194 
1195 #define DEFINE_CALLMETHOD(ResultType, Result, Tag \
1196                           , EntryProbe, ReturnProbe)    \
1197 \
1198   DT_RETURN_MARK_DECL_FOR(Result, Call##Result##Method, ResultType \
1199                           , ReturnProbe);                          \
1200 \
1201 JNI_ENTRY(ResultType, \
1202           jni_Call##Result##Method(JNIEnv *env, jobject obj, jmethodID methodID, ...)) \
1203 \
1204   EntryProbe; \
1205   ResultType ret = 0;\
1206   DT_RETURN_MARK_FOR(Result, Call##Result##Method, ResultType, \
1207                      (const ResultType&)ret);\
1208 \
1209   va_list args; \
1210   va_start(args, methodID); \
1211   JavaValue jvalue(Tag); \
1212   JNI_ArgumentPusherVaArg ap(methodID, args); \
1213   jni_invoke_nonstatic(env, &jvalue, obj, JNI_VIRTUAL, methodID, &ap, CHECK_0); \
1214   va_end(args); \
1215   ret = jvalue.get_##ResultType(); \
1216   return ret;\
1217 JNI_END
1218 
1219 // the runtime type of subword integral basic types is integer
1220 DEFINE_CALLMETHOD(jboolean, Boolean, T_BOOLEAN
1221                   , HOTSPOT_JNI_CALLBOOLEANMETHOD_ENTRY(env, obj, (uintptr_t)methodID),
1222                   HOTSPOT_JNI_CALLBOOLEANMETHOD_RETURN(_ret_ref))
1223 DEFINE_CALLMETHOD(jbyte,    Byte,    T_BYTE
1224                   , HOTSPOT_JNI_CALLBYTEMETHOD_ENTRY(env, obj, (uintptr_t)methodID),
1225                   HOTSPOT_JNI_CALLBYTEMETHOD_RETURN(_ret_ref))
1226 DEFINE_CALLMETHOD(jchar,    Char,    T_CHAR
1227                   , HOTSPOT_JNI_CALLCHARMETHOD_ENTRY(env, obj, (uintptr_t)methodID),
1228                   HOTSPOT_JNI_CALLCHARMETHOD_RETURN(_ret_ref))
1229 DEFINE_CALLMETHOD(jshort,   Short,   T_SHORT
1230                   , HOTSPOT_JNI_CALLSHORTMETHOD_ENTRY(env, obj, (uintptr_t)methodID),
1231                   HOTSPOT_JNI_CALLSHORTMETHOD_RETURN(_ret_ref))
1232 
1233 DEFINE_CALLMETHOD(jobject,  Object,  T_OBJECT
1234                   , HOTSPOT_JNI_CALLOBJECTMETHOD_ENTRY(env, obj, (uintptr_t)methodID),
1235                   HOTSPOT_JNI_CALLOBJECTMETHOD_RETURN(_ret_ref))
1236 DEFINE_CALLMETHOD(jint,     Int,     T_INT,
1237                   HOTSPOT_JNI_CALLINTMETHOD_ENTRY(env, obj, (uintptr_t)methodID),
1238                   HOTSPOT_JNI_CALLINTMETHOD_RETURN(_ret_ref))
1239 DEFINE_CALLMETHOD(jlong,    Long,    T_LONG
1240                   , HOTSPOT_JNI_CALLLONGMETHOD_ENTRY(env, obj, (uintptr_t)methodID),
1241                   HOTSPOT_JNI_CALLLONGMETHOD_RETURN(_ret_ref))
1242 // Float and double probes don't return value because dtrace doesn't currently support it
1243 DEFINE_CALLMETHOD(jfloat,   Float,   T_FLOAT
1244                   , HOTSPOT_JNI_CALLFLOATMETHOD_ENTRY(env, obj, (uintptr_t)methodID),
1245                   HOTSPOT_JNI_CALLFLOATMETHOD_RETURN())
1246 DEFINE_CALLMETHOD(jdouble,  Double,  T_DOUBLE
1247                   , HOTSPOT_JNI_CALLDOUBLEMETHOD_ENTRY(env, obj, (uintptr_t)methodID),
1248                   HOTSPOT_JNI_CALLDOUBLEMETHOD_RETURN())
1249 
1250 #define DEFINE_CALLMETHODV(ResultType, Result, Tag \
1251                           , EntryProbe, ReturnProbe)    \
1252 \
1253   DT_RETURN_MARK_DECL_FOR(Result, Call##Result##MethodV, ResultType \
1254                           , ReturnProbe);                          \
1255 \
1256 JNI_ENTRY(ResultType, \
1257           jni_Call##Result##MethodV(JNIEnv *env, jobject obj, jmethodID methodID, va_list args)) \
1258 \
1259   EntryProbe;\
1260   ResultType ret = 0;\
1261   DT_RETURN_MARK_FOR(Result, Call##Result##MethodV, ResultType, \
1262                      (const ResultType&)ret);\
1263 \
1264   JavaValue jvalue(Tag); \
1265   JNI_ArgumentPusherVaArg ap(methodID, args); \
1266   jni_invoke_nonstatic(env, &jvalue, obj, JNI_VIRTUAL, methodID, &ap, CHECK_0); \
1267   ret = jvalue.get_##ResultType(); \
1268   return ret;\
1269 JNI_END
1270 
1271 // the runtime type of subword integral basic types is integer
1272 DEFINE_CALLMETHODV(jboolean, Boolean, T_BOOLEAN
1273                   , HOTSPOT_JNI_CALLBOOLEANMETHODV_ENTRY(env, obj, (uintptr_t)methodID),
1274                   HOTSPOT_JNI_CALLBOOLEANMETHODV_RETURN(_ret_ref))
1275 DEFINE_CALLMETHODV(jbyte,    Byte,    T_BYTE
1276                   , HOTSPOT_JNI_CALLBYTEMETHODV_ENTRY(env, obj, (uintptr_t)methodID),
1277                   HOTSPOT_JNI_CALLBYTEMETHODV_RETURN(_ret_ref))
1278 DEFINE_CALLMETHODV(jchar,    Char,    T_CHAR
1279                   , HOTSPOT_JNI_CALLCHARMETHODV_ENTRY(env, obj, (uintptr_t)methodID),
1280                   HOTSPOT_JNI_CALLCHARMETHODV_RETURN(_ret_ref))
1281 DEFINE_CALLMETHODV(jshort,   Short,   T_SHORT
1282                   , HOTSPOT_JNI_CALLSHORTMETHODV_ENTRY(env, obj, (uintptr_t)methodID),
1283                   HOTSPOT_JNI_CALLSHORTMETHODV_RETURN(_ret_ref))
1284 
1285 DEFINE_CALLMETHODV(jobject,  Object,  T_OBJECT
1286                   , HOTSPOT_JNI_CALLOBJECTMETHODV_ENTRY(env, obj, (uintptr_t)methodID),
1287                   HOTSPOT_JNI_CALLOBJECTMETHODV_RETURN(_ret_ref))
1288 DEFINE_CALLMETHODV(jint,     Int,     T_INT,
1289                   HOTSPOT_JNI_CALLINTMETHODV_ENTRY(env, obj, (uintptr_t)methodID),
1290                   HOTSPOT_JNI_CALLINTMETHODV_RETURN(_ret_ref))
1291 DEFINE_CALLMETHODV(jlong,    Long,    T_LONG
1292                   , HOTSPOT_JNI_CALLLONGMETHODV_ENTRY(env, obj, (uintptr_t)methodID),
1293                   HOTSPOT_JNI_CALLLONGMETHODV_RETURN(_ret_ref))
1294 // Float and double probes don't return value because dtrace doesn't currently support it
1295 DEFINE_CALLMETHODV(jfloat,   Float,   T_FLOAT
1296                   , HOTSPOT_JNI_CALLFLOATMETHODV_ENTRY(env, obj, (uintptr_t)methodID),
1297                   HOTSPOT_JNI_CALLFLOATMETHODV_RETURN())
1298 DEFINE_CALLMETHODV(jdouble,  Double,  T_DOUBLE
1299                   , HOTSPOT_JNI_CALLDOUBLEMETHODV_ENTRY(env, obj, (uintptr_t)methodID),
1300                   HOTSPOT_JNI_CALLDOUBLEMETHODV_RETURN())
1301 
1302 #define DEFINE_CALLMETHODA(ResultType, Result, Tag \
1303                           , EntryProbe, ReturnProbe)    \
1304 \
1305   DT_RETURN_MARK_DECL_FOR(Result, Call##Result##MethodA, ResultType \
1306                           , ReturnProbe);                          \
1307 \
1308 JNI_ENTRY(ResultType, \
1309           jni_Call##Result##MethodA(JNIEnv *env, jobject obj, jmethodID methodID, const jvalue *args)) \
1310   EntryProbe; \
1311   ResultType ret = 0;\
1312   DT_RETURN_MARK_FOR(Result, Call##Result##MethodA, ResultType, \
1313                      (const ResultType&)ret);\
1314 \
1315   JavaValue jvalue(Tag); \
1316   JNI_ArgumentPusherArray ap(methodID, args); \
1317   jni_invoke_nonstatic(env, &jvalue, obj, JNI_VIRTUAL, methodID, &ap, CHECK_0); \
1318   ret = jvalue.get_##ResultType(); \
1319   return ret;\
1320 JNI_END
1321 
1322 // the runtime type of subword integral basic types is integer
1323 DEFINE_CALLMETHODA(jboolean, Boolean, T_BOOLEAN
1324                   , HOTSPOT_JNI_CALLBOOLEANMETHODA_ENTRY(env, obj, (uintptr_t)methodID),
1325                   HOTSPOT_JNI_CALLBOOLEANMETHODA_RETURN(_ret_ref))
1326 DEFINE_CALLMETHODA(jbyte,    Byte,    T_BYTE
1327                   , HOTSPOT_JNI_CALLBYTEMETHODA_ENTRY(env, obj, (uintptr_t)methodID),
1328                   HOTSPOT_JNI_CALLBYTEMETHODA_RETURN(_ret_ref))
1329 DEFINE_CALLMETHODA(jchar,    Char,    T_CHAR
1330                   , HOTSPOT_JNI_CALLCHARMETHODA_ENTRY(env, obj, (uintptr_t)methodID),
1331                   HOTSPOT_JNI_CALLCHARMETHODA_RETURN(_ret_ref))
1332 DEFINE_CALLMETHODA(jshort,   Short,   T_SHORT
1333                   , HOTSPOT_JNI_CALLSHORTMETHODA_ENTRY(env, obj, (uintptr_t)methodID),
1334                   HOTSPOT_JNI_CALLSHORTMETHODA_RETURN(_ret_ref))
1335 
1336 DEFINE_CALLMETHODA(jobject,  Object,  T_OBJECT
1337                   , HOTSPOT_JNI_CALLOBJECTMETHODA_ENTRY(env, obj, (uintptr_t)methodID),
1338                   HOTSPOT_JNI_CALLOBJECTMETHODA_RETURN(_ret_ref))
1339 DEFINE_CALLMETHODA(jint,     Int,     T_INT,
1340                   HOTSPOT_JNI_CALLINTMETHODA_ENTRY(env, obj, (uintptr_t)methodID),
1341                   HOTSPOT_JNI_CALLINTMETHODA_RETURN(_ret_ref))
1342 DEFINE_CALLMETHODA(jlong,    Long,    T_LONG
1343                   , HOTSPOT_JNI_CALLLONGMETHODA_ENTRY(env, obj, (uintptr_t)methodID),
1344                   HOTSPOT_JNI_CALLLONGMETHODA_RETURN(_ret_ref))
1345 // Float and double probes don't return value because dtrace doesn't currently support it
1346 DEFINE_CALLMETHODA(jfloat,   Float,   T_FLOAT
1347                   , HOTSPOT_JNI_CALLFLOATMETHODA_ENTRY(env, obj, (uintptr_t)methodID),
1348                   HOTSPOT_JNI_CALLFLOATMETHODA_RETURN())
1349 DEFINE_CALLMETHODA(jdouble,  Double,  T_DOUBLE
1350                   , HOTSPOT_JNI_CALLDOUBLEMETHODA_ENTRY(env, obj, (uintptr_t)methodID),
1351                   HOTSPOT_JNI_CALLDOUBLEMETHODA_RETURN())
1352 
1353 DT_VOID_RETURN_MARK_DECL(CallVoidMethod, HOTSPOT_JNI_CALLVOIDMETHOD_RETURN());
1354 DT_VOID_RETURN_MARK_DECL(CallVoidMethodV, HOTSPOT_JNI_CALLVOIDMETHODV_RETURN());
1355 DT_VOID_RETURN_MARK_DECL(CallVoidMethodA, HOTSPOT_JNI_CALLVOIDMETHODA_RETURN());
1356 
1357 
1358 JNI_ENTRY(void, jni_CallVoidMethod(JNIEnv *env, jobject obj, jmethodID methodID, ...))
1359   HOTSPOT_JNI_CALLVOIDMETHOD_ENTRY(env, obj, (uintptr_t) methodID);
1360   DT_VOID_RETURN_MARK(CallVoidMethod);
1361 
1362   va_list args;
1363   va_start(args, methodID);
1364   JavaValue jvalue(T_VOID);
1365   JNI_ArgumentPusherVaArg ap(methodID, args);
1366   jni_invoke_nonstatic(env, &jvalue, obj, JNI_VIRTUAL, methodID, &ap, CHECK);
1367   va_end(args);
1368 JNI_END
1369 
1370 
1371 JNI_ENTRY(void, jni_CallVoidMethodV(JNIEnv *env, jobject obj, jmethodID methodID, va_list args))
1372   HOTSPOT_JNI_CALLVOIDMETHODV_ENTRY(env, obj, (uintptr_t) methodID);
1373   DT_VOID_RETURN_MARK(CallVoidMethodV);
1374 
1375   JavaValue jvalue(T_VOID);
1376   JNI_ArgumentPusherVaArg ap(methodID, args);
1377   jni_invoke_nonstatic(env, &jvalue, obj, JNI_VIRTUAL, methodID, &ap, CHECK);
1378 JNI_END
1379 
1380 
1381 JNI_ENTRY(void, jni_CallVoidMethodA(JNIEnv *env, jobject obj, jmethodID methodID, const jvalue *args))
1382   HOTSPOT_JNI_CALLVOIDMETHODA_ENTRY(env, obj, (uintptr_t) methodID);
1383   DT_VOID_RETURN_MARK(CallVoidMethodA);
1384 
1385   JavaValue jvalue(T_VOID);
1386   JNI_ArgumentPusherArray ap(methodID, args);
1387   jni_invoke_nonstatic(env, &jvalue, obj, JNI_VIRTUAL, methodID, &ap, CHECK);
1388 JNI_END
1389 
1390 
1391 
1392 #define DEFINE_CALLNONVIRTUALMETHOD(ResultType, Result, Tag \
1393                                     , EntryProbe, ReturnProbe)      \
1394 \
1395   DT_RETURN_MARK_DECL_FOR(Result, CallNonvirtual##Result##Method, ResultType \
1396                           , ReturnProbe);\
1397 \
1398 JNI_ENTRY(ResultType, \
1399           jni_CallNonvirtual##Result##Method(JNIEnv *env, jobject obj, jclass cls, jmethodID methodID, ...)) \
1400 \
1401   EntryProbe;\
1402   ResultType ret;\
1403   DT_RETURN_MARK_FOR(Result, CallNonvirtual##Result##Method, ResultType, \
1404                      (const ResultType&)ret);\
1405 \
1406   va_list args; \
1407   va_start(args, methodID); \
1408   JavaValue jvalue(Tag); \
1409   JNI_ArgumentPusherVaArg ap(methodID, args); \
1410   jni_invoke_nonstatic(env, &jvalue, obj, JNI_NONVIRTUAL, methodID, &ap, CHECK_0); \
1411   va_end(args); \
1412   ret = jvalue.get_##ResultType(); \
1413   return ret;\
1414 JNI_END
1415 
1416 // the runtime type of subword integral basic types is integer
1417 DEFINE_CALLNONVIRTUALMETHOD(jboolean, Boolean, T_BOOLEAN
1418                             , HOTSPOT_JNI_CALLNONVIRTUALBOOLEANMETHOD_ENTRY(env, obj, cls, (uintptr_t)methodID),
1419                             HOTSPOT_JNI_CALLNONVIRTUALBOOLEANMETHOD_RETURN(_ret_ref))
1420 DEFINE_CALLNONVIRTUALMETHOD(jbyte,    Byte,    T_BYTE
1421                             , HOTSPOT_JNI_CALLNONVIRTUALBYTEMETHOD_ENTRY(env, obj, cls, (uintptr_t)methodID),
1422                             HOTSPOT_JNI_CALLNONVIRTUALBYTEMETHOD_RETURN(_ret_ref))
1423 DEFINE_CALLNONVIRTUALMETHOD(jchar,    Char,    T_CHAR
1424                             , HOTSPOT_JNI_CALLNONVIRTUALCHARMETHOD_ENTRY(env, obj, cls, (uintptr_t)methodID),
1425                             HOTSPOT_JNI_CALLNONVIRTUALCHARMETHOD_RETURN(_ret_ref))
1426 DEFINE_CALLNONVIRTUALMETHOD(jshort,   Short,   T_SHORT
1427                             , HOTSPOT_JNI_CALLNONVIRTUALSHORTMETHOD_ENTRY(env, obj, cls, (uintptr_t)methodID),
1428                             HOTSPOT_JNI_CALLNONVIRTUALSHORTMETHOD_RETURN(_ret_ref))
1429 
1430 DEFINE_CALLNONVIRTUALMETHOD(jobject,  Object,  T_OBJECT
1431                             , HOTSPOT_JNI_CALLNONVIRTUALOBJECTMETHOD_ENTRY(env, obj, cls, (uintptr_t)methodID),
1432                             HOTSPOT_JNI_CALLNONVIRTUALOBJECTMETHOD_RETURN(_ret_ref))
1433 DEFINE_CALLNONVIRTUALMETHOD(jint,     Int,     T_INT
1434                             , HOTSPOT_JNI_CALLNONVIRTUALINTMETHOD_ENTRY(env, obj, cls, (uintptr_t)methodID),
1435                             HOTSPOT_JNI_CALLNONVIRTUALINTMETHOD_RETURN(_ret_ref))
1436 DEFINE_CALLNONVIRTUALMETHOD(jlong,    Long,    T_LONG
1437                             , HOTSPOT_JNI_CALLNONVIRTUALLONGMETHOD_ENTRY(env, obj, cls, (uintptr_t)methodID),
1438 // Float and double probes don't return value because dtrace doesn't currently support it
1439                             HOTSPOT_JNI_CALLNONVIRTUALLONGMETHOD_RETURN(_ret_ref))
1440 DEFINE_CALLNONVIRTUALMETHOD(jfloat,   Float,   T_FLOAT
1441                             , HOTSPOT_JNI_CALLNONVIRTUALFLOATMETHOD_ENTRY(env, obj, cls, (uintptr_t)methodID),
1442                             HOTSPOT_JNI_CALLNONVIRTUALFLOATMETHOD_RETURN())
1443 DEFINE_CALLNONVIRTUALMETHOD(jdouble,  Double,  T_DOUBLE
1444                             , HOTSPOT_JNI_CALLNONVIRTUALDOUBLEMETHOD_ENTRY(env, obj, cls, (uintptr_t)methodID),
1445                             HOTSPOT_JNI_CALLNONVIRTUALDOUBLEMETHOD_RETURN())
1446 
1447 #define DEFINE_CALLNONVIRTUALMETHODV(ResultType, Result, Tag \
1448                                     , EntryProbe, ReturnProbe)      \
1449 \
1450   DT_RETURN_MARK_DECL_FOR(Result, CallNonvirtual##Result##MethodV, ResultType \
1451                           , ReturnProbe);\
1452 \
1453 JNI_ENTRY(ResultType, \
1454           jni_CallNonvirtual##Result##MethodV(JNIEnv *env, jobject obj, jclass cls, jmethodID methodID, va_list args)) \
1455 \
1456   EntryProbe;\
1457   ResultType ret;\
1458   DT_RETURN_MARK_FOR(Result, CallNonvirtual##Result##MethodV, ResultType, \
1459                      (const ResultType&)ret);\
1460 \
1461   JavaValue jvalue(Tag); \
1462   JNI_ArgumentPusherVaArg ap(methodID, args); \
1463   jni_invoke_nonstatic(env, &jvalue, obj, JNI_NONVIRTUAL, methodID, &ap, CHECK_0); \
1464   ret = jvalue.get_##ResultType(); \
1465   return ret;\
1466 JNI_END
1467 
1468 // the runtime type of subword integral basic types is integer
1469 DEFINE_CALLNONVIRTUALMETHODV(jboolean, Boolean, T_BOOLEAN
1470                             , HOTSPOT_JNI_CALLNONVIRTUALBOOLEANMETHODV_ENTRY(env, obj, cls, (uintptr_t)methodID),
1471                             HOTSPOT_JNI_CALLNONVIRTUALBOOLEANMETHODV_RETURN(_ret_ref))
1472 DEFINE_CALLNONVIRTUALMETHODV(jbyte,    Byte,    T_BYTE
1473                             , HOTSPOT_JNI_CALLNONVIRTUALBYTEMETHODV_ENTRY(env, obj, cls, (uintptr_t)methodID),
1474                             HOTSPOT_JNI_CALLNONVIRTUALBYTEMETHODV_RETURN(_ret_ref))
1475 DEFINE_CALLNONVIRTUALMETHODV(jchar,    Char,    T_CHAR
1476                             , HOTSPOT_JNI_CALLNONVIRTUALCHARMETHODV_ENTRY(env, obj, cls, (uintptr_t)methodID),
1477                             HOTSPOT_JNI_CALLNONVIRTUALCHARMETHODV_RETURN(_ret_ref))
1478 DEFINE_CALLNONVIRTUALMETHODV(jshort,   Short,   T_SHORT
1479                             , HOTSPOT_JNI_CALLNONVIRTUALSHORTMETHODV_ENTRY(env, obj, cls, (uintptr_t)methodID),
1480                             HOTSPOT_JNI_CALLNONVIRTUALSHORTMETHODV_RETURN(_ret_ref))
1481 
1482 DEFINE_CALLNONVIRTUALMETHODV(jobject,  Object,  T_OBJECT
1483                             , HOTSPOT_JNI_CALLNONVIRTUALOBJECTMETHODV_ENTRY(env, obj, cls, (uintptr_t)methodID),
1484                             HOTSPOT_JNI_CALLNONVIRTUALOBJECTMETHODV_RETURN(_ret_ref))
1485 DEFINE_CALLNONVIRTUALMETHODV(jint,     Int,     T_INT
1486                             , HOTSPOT_JNI_CALLNONVIRTUALINTMETHODV_ENTRY(env, obj, cls, (uintptr_t)methodID),
1487                             HOTSPOT_JNI_CALLNONVIRTUALINTMETHODV_RETURN(_ret_ref))
1488 DEFINE_CALLNONVIRTUALMETHODV(jlong,    Long,    T_LONG
1489                             , HOTSPOT_JNI_CALLNONVIRTUALLONGMETHODV_ENTRY(env, obj, cls, (uintptr_t)methodID),
1490 // Float and double probes don't return value because dtrace doesn't currently support it
1491                             HOTSPOT_JNI_CALLNONVIRTUALLONGMETHODV_RETURN(_ret_ref))
1492 DEFINE_CALLNONVIRTUALMETHODV(jfloat,   Float,   T_FLOAT
1493                             , HOTSPOT_JNI_CALLNONVIRTUALFLOATMETHODV_ENTRY(env, obj, cls, (uintptr_t)methodID),
1494                             HOTSPOT_JNI_CALLNONVIRTUALFLOATMETHODV_RETURN())
1495 DEFINE_CALLNONVIRTUALMETHODV(jdouble,  Double,  T_DOUBLE
1496                             , HOTSPOT_JNI_CALLNONVIRTUALDOUBLEMETHODV_ENTRY(env, obj, cls, (uintptr_t)methodID),
1497                             HOTSPOT_JNI_CALLNONVIRTUALDOUBLEMETHODV_RETURN())
1498 
1499 #define DEFINE_CALLNONVIRTUALMETHODA(ResultType, Result, Tag \
1500                                     , EntryProbe, ReturnProbe)      \
1501 \
1502   DT_RETURN_MARK_DECL_FOR(Result, CallNonvirtual##Result##MethodA, ResultType \
1503                           , ReturnProbe);\
1504 \
1505 JNI_ENTRY(ResultType, \
1506           jni_CallNonvirtual##Result##MethodA(JNIEnv *env, jobject obj, jclass cls, jmethodID methodID, const jvalue *args)) \
1507 \
1508   EntryProbe;\
1509   ResultType ret;\
1510   DT_RETURN_MARK_FOR(Result, CallNonvirtual##Result##MethodA, ResultType, \
1511                      (const ResultType&)ret);\
1512 \
1513   JavaValue jvalue(Tag); \
1514   JNI_ArgumentPusherArray ap(methodID, args); \
1515   jni_invoke_nonstatic(env, &jvalue, obj, JNI_NONVIRTUAL, methodID, &ap, CHECK_0); \
1516   ret = jvalue.get_##ResultType(); \
1517   return ret;\
1518 JNI_END
1519 
1520 // the runtime type of subword integral basic types is integer
1521 DEFINE_CALLNONVIRTUALMETHODA(jboolean, Boolean, T_BOOLEAN
1522                             , HOTSPOT_JNI_CALLNONVIRTUALBOOLEANMETHODA_ENTRY(env, obj, cls, (uintptr_t)methodID),
1523                             HOTSPOT_JNI_CALLNONVIRTUALBOOLEANMETHODA_RETURN(_ret_ref))
1524 DEFINE_CALLNONVIRTUALMETHODA(jbyte,    Byte,    T_BYTE
1525                             , HOTSPOT_JNI_CALLNONVIRTUALBYTEMETHODA_ENTRY(env, obj, cls, (uintptr_t)methodID),
1526                             HOTSPOT_JNI_CALLNONVIRTUALBYTEMETHODA_RETURN(_ret_ref))
1527 DEFINE_CALLNONVIRTUALMETHODA(jchar,    Char,    T_CHAR
1528                             , HOTSPOT_JNI_CALLNONVIRTUALCHARMETHODA_ENTRY(env, obj, cls, (uintptr_t)methodID),
1529                             HOTSPOT_JNI_CALLNONVIRTUALCHARMETHODA_RETURN(_ret_ref))
1530 DEFINE_CALLNONVIRTUALMETHODA(jshort,   Short,   T_SHORT
1531                             , HOTSPOT_JNI_CALLNONVIRTUALSHORTMETHODA_ENTRY(env, obj, cls, (uintptr_t)methodID),
1532                             HOTSPOT_JNI_CALLNONVIRTUALSHORTMETHODA_RETURN(_ret_ref))
1533 
1534 DEFINE_CALLNONVIRTUALMETHODA(jobject,  Object,  T_OBJECT
1535                             , HOTSPOT_JNI_CALLNONVIRTUALOBJECTMETHODA_ENTRY(env, obj, cls, (uintptr_t)methodID),
1536                             HOTSPOT_JNI_CALLNONVIRTUALOBJECTMETHODA_RETURN(_ret_ref))
1537 DEFINE_CALLNONVIRTUALMETHODA(jint,     Int,     T_INT
1538                             , HOTSPOT_JNI_CALLNONVIRTUALINTMETHODA_ENTRY(env, obj, cls, (uintptr_t)methodID),
1539                             HOTSPOT_JNI_CALLNONVIRTUALINTMETHODA_RETURN(_ret_ref))
1540 DEFINE_CALLNONVIRTUALMETHODA(jlong,    Long,    T_LONG
1541                             , HOTSPOT_JNI_CALLNONVIRTUALLONGMETHODA_ENTRY(env, obj, cls, (uintptr_t)methodID),
1542 // Float and double probes don't return value because dtrace doesn't currently support it
1543                             HOTSPOT_JNI_CALLNONVIRTUALLONGMETHODA_RETURN(_ret_ref))
1544 DEFINE_CALLNONVIRTUALMETHODA(jfloat,   Float,   T_FLOAT
1545                             , HOTSPOT_JNI_CALLNONVIRTUALFLOATMETHODA_ENTRY(env, obj, cls, (uintptr_t)methodID),
1546                             HOTSPOT_JNI_CALLNONVIRTUALFLOATMETHODA_RETURN())
1547 DEFINE_CALLNONVIRTUALMETHODA(jdouble,  Double,  T_DOUBLE
1548                             , HOTSPOT_JNI_CALLNONVIRTUALDOUBLEMETHODA_ENTRY(env, obj, cls, (uintptr_t)methodID),
1549                             HOTSPOT_JNI_CALLNONVIRTUALDOUBLEMETHODA_RETURN())
1550 
1551 DT_VOID_RETURN_MARK_DECL(CallNonvirtualVoidMethod
1552                          , HOTSPOT_JNI_CALLNONVIRTUALVOIDMETHOD_RETURN());
1553 DT_VOID_RETURN_MARK_DECL(CallNonvirtualVoidMethodV
1554                          , HOTSPOT_JNI_CALLNONVIRTUALVOIDMETHODV_RETURN());
1555 DT_VOID_RETURN_MARK_DECL(CallNonvirtualVoidMethodA
1556                          , HOTSPOT_JNI_CALLNONVIRTUALVOIDMETHODA_RETURN());
1557 
1558 JNI_ENTRY(void, jni_CallNonvirtualVoidMethod(JNIEnv *env, jobject obj, jclass cls, jmethodID methodID, ...))
1559   HOTSPOT_JNI_CALLNONVIRTUALVOIDMETHOD_ENTRY(env, obj, cls, (uintptr_t) methodID);
1560   DT_VOID_RETURN_MARK(CallNonvirtualVoidMethod);
1561 
1562   va_list args;
1563   va_start(args, methodID);
1564   JavaValue jvalue(T_VOID);
1565   JNI_ArgumentPusherVaArg ap(methodID, args);
1566   jni_invoke_nonstatic(env, &jvalue, obj, JNI_NONVIRTUAL, methodID, &ap, CHECK);
1567   va_end(args);
1568 JNI_END
1569 
1570 
1571 JNI_ENTRY(void, jni_CallNonvirtualVoidMethodV(JNIEnv *env, jobject obj, jclass cls, jmethodID methodID, va_list args))
1572   HOTSPOT_JNI_CALLNONVIRTUALVOIDMETHODV_ENTRY(
1573                env, obj, cls, (uintptr_t) methodID);
1574   DT_VOID_RETURN_MARK(CallNonvirtualVoidMethodV);
1575 
1576   JavaValue jvalue(T_VOID);
1577   JNI_ArgumentPusherVaArg ap(methodID, args);
1578   jni_invoke_nonstatic(env, &jvalue, obj, JNI_NONVIRTUAL, methodID, &ap, CHECK);
1579 JNI_END
1580 
1581 
1582 JNI_ENTRY(void, jni_CallNonvirtualVoidMethodA(JNIEnv *env, jobject obj, jclass cls, jmethodID methodID, const jvalue *args))
1583   HOTSPOT_JNI_CALLNONVIRTUALVOIDMETHODA_ENTRY(
1584                 env, obj, cls, (uintptr_t) methodID);
1585   DT_VOID_RETURN_MARK(CallNonvirtualVoidMethodA);
1586   JavaValue jvalue(T_VOID);
1587   JNI_ArgumentPusherArray ap(methodID, args);
1588   jni_invoke_nonstatic(env, &jvalue, obj, JNI_NONVIRTUAL, methodID, &ap, CHECK);
1589 JNI_END
1590 
1591 
1592 
1593 #define DEFINE_CALLSTATICMETHOD(ResultType, Result, Tag \
1594                                 , EntryProbe, ResultProbe) \
1595 \
1596   DT_RETURN_MARK_DECL_FOR(Result, CallStatic##Result##Method, ResultType \
1597                           , ResultProbe);                               \
1598 \
1599 JNI_ENTRY(ResultType, \
1600           jni_CallStatic##Result##Method(JNIEnv *env, jclass cls, jmethodID methodID, ...)) \
1601 \
1602   EntryProbe; \
1603   ResultType ret = 0;\
1604   DT_RETURN_MARK_FOR(Result, CallStatic##Result##Method, ResultType, \
1605                      (const ResultType&)ret);\
1606 \
1607   va_list args; \
1608   va_start(args, methodID); \
1609   JavaValue jvalue(Tag); \
1610   JNI_ArgumentPusherVaArg ap(methodID, args); \
1611   jni_invoke_static(env, &jvalue, nullptr, JNI_STATIC, methodID, &ap, CHECK_0); \
1612   va_end(args); \
1613   ret = jvalue.get_##ResultType(); \
1614   return ret;\
1615 JNI_END
1616 
1617 // the runtime type of subword integral basic types is integer
1618 DEFINE_CALLSTATICMETHOD(jboolean, Boolean, T_BOOLEAN
1619                         , HOTSPOT_JNI_CALLSTATICBOOLEANMETHOD_ENTRY(env, cls, (uintptr_t)methodID),
1620                         HOTSPOT_JNI_CALLSTATICBOOLEANMETHOD_RETURN(_ret_ref));
1621 DEFINE_CALLSTATICMETHOD(jbyte,    Byte,    T_BYTE
1622                         , HOTSPOT_JNI_CALLSTATICBYTEMETHOD_ENTRY(env, cls, (uintptr_t)methodID),
1623                         HOTSPOT_JNI_CALLSTATICBYTEMETHOD_RETURN(_ret_ref));
1624 DEFINE_CALLSTATICMETHOD(jchar,    Char,    T_CHAR
1625                         , HOTSPOT_JNI_CALLSTATICCHARMETHOD_ENTRY(env, cls, (uintptr_t)methodID),
1626                         HOTSPOT_JNI_CALLSTATICCHARMETHOD_RETURN(_ret_ref));
1627 DEFINE_CALLSTATICMETHOD(jshort,   Short,   T_SHORT
1628                         , HOTSPOT_JNI_CALLSTATICSHORTMETHOD_ENTRY(env, cls, (uintptr_t)methodID),
1629                         HOTSPOT_JNI_CALLSTATICSHORTMETHOD_RETURN(_ret_ref));
1630 
1631 DEFINE_CALLSTATICMETHOD(jobject,  Object,  T_OBJECT
1632                         , HOTSPOT_JNI_CALLSTATICOBJECTMETHOD_ENTRY(env, cls, (uintptr_t)methodID),
1633                         HOTSPOT_JNI_CALLSTATICOBJECTMETHOD_RETURN(_ret_ref));
1634 DEFINE_CALLSTATICMETHOD(jint,     Int,     T_INT
1635                         , HOTSPOT_JNI_CALLSTATICINTMETHOD_ENTRY(env, cls, (uintptr_t)methodID),
1636                         HOTSPOT_JNI_CALLSTATICINTMETHOD_RETURN(_ret_ref));
1637 DEFINE_CALLSTATICMETHOD(jlong,    Long,    T_LONG
1638                         , HOTSPOT_JNI_CALLSTATICLONGMETHOD_ENTRY(env, cls, (uintptr_t)methodID),
1639                         HOTSPOT_JNI_CALLSTATICLONGMETHOD_RETURN(_ret_ref));
1640 // Float and double probes don't return value because dtrace doesn't currently support it
1641 DEFINE_CALLSTATICMETHOD(jfloat,   Float,   T_FLOAT
1642                         , HOTSPOT_JNI_CALLSTATICFLOATMETHOD_ENTRY(env, cls, (uintptr_t)methodID),
1643                         HOTSPOT_JNI_CALLSTATICFLOATMETHOD_RETURN());
1644 DEFINE_CALLSTATICMETHOD(jdouble,  Double,  T_DOUBLE
1645                         , HOTSPOT_JNI_CALLSTATICDOUBLEMETHOD_ENTRY(env, cls, (uintptr_t)methodID),
1646                         HOTSPOT_JNI_CALLSTATICDOUBLEMETHOD_RETURN());
1647 
1648 #define DEFINE_CALLSTATICMETHODV(ResultType, Result, Tag \
1649                                 , EntryProbe, ResultProbe) \
1650 \
1651   DT_RETURN_MARK_DECL_FOR(Result, CallStatic##Result##MethodV, ResultType \
1652                           , ResultProbe);                               \
1653 \
1654 JNI_ENTRY(ResultType, \
1655           jni_CallStatic##Result##MethodV(JNIEnv *env, jclass cls, jmethodID methodID, va_list args)) \
1656 \
1657   EntryProbe; \
1658   ResultType ret = 0;\
1659   DT_RETURN_MARK_FOR(Result, CallStatic##Result##MethodV, ResultType, \
1660                      (const ResultType&)ret);\
1661 \
1662   JavaValue jvalue(Tag); \
1663   JNI_ArgumentPusherVaArg ap(methodID, args); \
1664   /* Make sure class is initialized before trying to invoke its method */ \
1665   Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls)); \
1666   k->initialize(CHECK_0); \
1667   jni_invoke_static(env, &jvalue, nullptr, JNI_STATIC, methodID, &ap, CHECK_0); \
1668   va_end(args); \
1669   ret = jvalue.get_##ResultType(); \
1670   return ret;\
1671 JNI_END
1672 
1673 // the runtime type of subword integral basic types is integer
1674 DEFINE_CALLSTATICMETHODV(jboolean, Boolean, T_BOOLEAN
1675                         , HOTSPOT_JNI_CALLSTATICBOOLEANMETHODV_ENTRY(env, cls, (uintptr_t)methodID),
1676                         HOTSPOT_JNI_CALLSTATICBOOLEANMETHODV_RETURN(_ret_ref));
1677 DEFINE_CALLSTATICMETHODV(jbyte,    Byte,    T_BYTE
1678                         , HOTSPOT_JNI_CALLSTATICBYTEMETHODV_ENTRY(env, cls, (uintptr_t)methodID),
1679                         HOTSPOT_JNI_CALLSTATICBYTEMETHODV_RETURN(_ret_ref));
1680 DEFINE_CALLSTATICMETHODV(jchar,    Char,    T_CHAR
1681                         , HOTSPOT_JNI_CALLSTATICCHARMETHODV_ENTRY(env, cls, (uintptr_t)methodID),
1682                         HOTSPOT_JNI_CALLSTATICCHARMETHODV_RETURN(_ret_ref));
1683 DEFINE_CALLSTATICMETHODV(jshort,   Short,   T_SHORT
1684                         , HOTSPOT_JNI_CALLSTATICSHORTMETHODV_ENTRY(env, cls, (uintptr_t)methodID),
1685                         HOTSPOT_JNI_CALLSTATICSHORTMETHODV_RETURN(_ret_ref));
1686 
1687 DEFINE_CALLSTATICMETHODV(jobject,  Object,  T_OBJECT
1688                         , HOTSPOT_JNI_CALLSTATICOBJECTMETHODV_ENTRY(env, cls, (uintptr_t)methodID),
1689                         HOTSPOT_JNI_CALLSTATICOBJECTMETHODV_RETURN(_ret_ref));
1690 DEFINE_CALLSTATICMETHODV(jint,     Int,     T_INT
1691                         , HOTSPOT_JNI_CALLSTATICINTMETHODV_ENTRY(env, cls, (uintptr_t)methodID),
1692                         HOTSPOT_JNI_CALLSTATICINTMETHODV_RETURN(_ret_ref));
1693 DEFINE_CALLSTATICMETHODV(jlong,    Long,    T_LONG
1694                         , HOTSPOT_JNI_CALLSTATICLONGMETHODV_ENTRY(env, cls, (uintptr_t)methodID),
1695                         HOTSPOT_JNI_CALLSTATICLONGMETHODV_RETURN(_ret_ref));
1696 // Float and double probes don't return value because dtrace doesn't currently support it
1697 DEFINE_CALLSTATICMETHODV(jfloat,   Float,   T_FLOAT
1698                         , HOTSPOT_JNI_CALLSTATICFLOATMETHODV_ENTRY(env, cls, (uintptr_t)methodID),
1699                         HOTSPOT_JNI_CALLSTATICFLOATMETHODV_RETURN());
1700 DEFINE_CALLSTATICMETHODV(jdouble,  Double,  T_DOUBLE
1701                         , HOTSPOT_JNI_CALLSTATICDOUBLEMETHODV_ENTRY(env, cls, (uintptr_t)methodID),
1702                         HOTSPOT_JNI_CALLSTATICDOUBLEMETHODV_RETURN());
1703 
1704 #define DEFINE_CALLSTATICMETHODA(ResultType, Result, Tag \
1705                                 , EntryProbe, ResultProbe) \
1706 \
1707   DT_RETURN_MARK_DECL_FOR(Result, CallStatic##Result##MethodA, ResultType \
1708                           , ResultProbe);                               \
1709 \
1710 JNI_ENTRY(ResultType, \
1711           jni_CallStatic##Result##MethodA(JNIEnv *env, jclass cls, jmethodID methodID, const jvalue *args)) \
1712 \
1713   EntryProbe; \
1714   ResultType ret = 0;\
1715   DT_RETURN_MARK_FOR(Result, CallStatic##Result##MethodA, ResultType, \
1716                      (const ResultType&)ret);\
1717 \
1718   JavaValue jvalue(Tag); \
1719   JNI_ArgumentPusherArray ap(methodID, args); \
1720   jni_invoke_static(env, &jvalue, nullptr, JNI_STATIC, methodID, &ap, CHECK_0); \
1721   ret = jvalue.get_##ResultType(); \
1722   return ret;\
1723 JNI_END
1724 
1725 // the runtime type of subword integral basic types is integer
1726 DEFINE_CALLSTATICMETHODA(jboolean, Boolean, T_BOOLEAN
1727                         , HOTSPOT_JNI_CALLSTATICBOOLEANMETHODA_ENTRY(env, cls, (uintptr_t)methodID),
1728                         HOTSPOT_JNI_CALLSTATICBOOLEANMETHODA_RETURN(_ret_ref));
1729 DEFINE_CALLSTATICMETHODA(jbyte,    Byte,    T_BYTE
1730                         , HOTSPOT_JNI_CALLSTATICBYTEMETHODA_ENTRY(env, cls, (uintptr_t)methodID),
1731                         HOTSPOT_JNI_CALLSTATICBYTEMETHODA_RETURN(_ret_ref));
1732 DEFINE_CALLSTATICMETHODA(jchar,    Char,    T_CHAR
1733                         , HOTSPOT_JNI_CALLSTATICCHARMETHODA_ENTRY(env, cls, (uintptr_t)methodID),
1734                         HOTSPOT_JNI_CALLSTATICCHARMETHODA_RETURN(_ret_ref));
1735 DEFINE_CALLSTATICMETHODA(jshort,   Short,   T_SHORT
1736                         , HOTSPOT_JNI_CALLSTATICSHORTMETHODA_ENTRY(env, cls, (uintptr_t)methodID),
1737                         HOTSPOT_JNI_CALLSTATICSHORTMETHODA_RETURN(_ret_ref));
1738 
1739 DEFINE_CALLSTATICMETHODA(jobject,  Object,  T_OBJECT
1740                         , HOTSPOT_JNI_CALLSTATICOBJECTMETHODA_ENTRY(env, cls, (uintptr_t)methodID),
1741                         HOTSPOT_JNI_CALLSTATICOBJECTMETHODA_RETURN(_ret_ref));
1742 DEFINE_CALLSTATICMETHODA(jint,     Int,     T_INT
1743                         , HOTSPOT_JNI_CALLSTATICINTMETHODA_ENTRY(env, cls, (uintptr_t)methodID),
1744                         HOTSPOT_JNI_CALLSTATICINTMETHODA_RETURN(_ret_ref));
1745 DEFINE_CALLSTATICMETHODA(jlong,    Long,    T_LONG
1746                         , HOTSPOT_JNI_CALLSTATICLONGMETHODA_ENTRY(env, cls, (uintptr_t)methodID),
1747                         HOTSPOT_JNI_CALLSTATICLONGMETHODA_RETURN(_ret_ref));
1748 // Float and double probes don't return value because dtrace doesn't currently support it
1749 DEFINE_CALLSTATICMETHODA(jfloat,   Float,   T_FLOAT
1750                         , HOTSPOT_JNI_CALLSTATICFLOATMETHODA_ENTRY(env, cls, (uintptr_t)methodID),
1751                         HOTSPOT_JNI_CALLSTATICFLOATMETHODA_RETURN());
1752 DEFINE_CALLSTATICMETHODA(jdouble,  Double,  T_DOUBLE
1753                         , HOTSPOT_JNI_CALLSTATICDOUBLEMETHODA_ENTRY(env, cls, (uintptr_t)methodID),
1754                         HOTSPOT_JNI_CALLSTATICDOUBLEMETHODA_RETURN());
1755 
1756 DT_VOID_RETURN_MARK_DECL(CallStaticVoidMethod
1757                          , HOTSPOT_JNI_CALLSTATICVOIDMETHOD_RETURN());
1758 DT_VOID_RETURN_MARK_DECL(CallStaticVoidMethodV
1759                          , HOTSPOT_JNI_CALLSTATICVOIDMETHODV_RETURN());
1760 DT_VOID_RETURN_MARK_DECL(CallStaticVoidMethodA
1761                          , HOTSPOT_JNI_CALLSTATICVOIDMETHODA_RETURN());
1762 
1763 JNI_ENTRY(void, jni_CallStaticVoidMethod(JNIEnv *env, jclass cls, jmethodID methodID, ...))
1764   HOTSPOT_JNI_CALLSTATICVOIDMETHOD_ENTRY(env, cls, (uintptr_t) methodID);
1765   DT_VOID_RETURN_MARK(CallStaticVoidMethod);
1766 
1767   va_list args;
1768   va_start(args, methodID);
1769   JavaValue jvalue(T_VOID);
1770   JNI_ArgumentPusherVaArg ap(methodID, args);
1771   jni_invoke_static(env, &jvalue, nullptr, JNI_STATIC, methodID, &ap, CHECK);
1772   va_end(args);
1773 JNI_END
1774 
1775 
1776 JNI_ENTRY(void, jni_CallStaticVoidMethodV(JNIEnv *env, jclass cls, jmethodID methodID, va_list args))
1777   HOTSPOT_JNI_CALLSTATICVOIDMETHODV_ENTRY(env, cls, (uintptr_t) methodID);
1778   DT_VOID_RETURN_MARK(CallStaticVoidMethodV);
1779 
1780   JavaValue jvalue(T_VOID);
1781   JNI_ArgumentPusherVaArg ap(methodID, args);
1782   jni_invoke_static(env, &jvalue, nullptr, JNI_STATIC, methodID, &ap, CHECK);
1783 JNI_END
1784 
1785 
1786 JNI_ENTRY(void, jni_CallStaticVoidMethodA(JNIEnv *env, jclass cls, jmethodID methodID, const jvalue *args))
1787   HOTSPOT_JNI_CALLSTATICVOIDMETHODA_ENTRY(env, cls, (uintptr_t) methodID);
1788   DT_VOID_RETURN_MARK(CallStaticVoidMethodA);
1789 
1790   JavaValue jvalue(T_VOID);
1791   JNI_ArgumentPusherArray ap(methodID, args);
1792   jni_invoke_static(env, &jvalue, nullptr, JNI_STATIC, methodID, &ap, CHECK);
1793 JNI_END
1794 
1795 
1796 //
1797 // Accessing Fields
1798 //
1799 
1800 
1801 DT_RETURN_MARK_DECL(GetFieldID, jfieldID
1802                     , HOTSPOT_JNI_GETFIELDID_RETURN((uintptr_t)_ret_ref));
1803 
1804 JNI_ENTRY(jfieldID, jni_GetFieldID(JNIEnv *env, jclass clazz,
1805           const char *name, const char *sig))
1806   HOTSPOT_JNI_GETFIELDID_ENTRY(env, clazz, (char *) name, (char *) sig);
1807   jfieldID ret = 0;
1808   DT_RETURN_MARK(GetFieldID, jfieldID, (const jfieldID&)ret);
1809 
1810   Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(clazz));
1811 
1812   // The class should have been loaded (we have an instance of the class
1813   // passed in) so the field and signature should already be in the symbol
1814   // table.  If they're not there, the field doesn't exist.
1815   TempNewSymbol fieldname = SymbolTable::probe(name, (int)strlen(name));
1816   TempNewSymbol signame = SymbolTable::probe(sig, (int)strlen(sig));
1817   if (fieldname == nullptr || signame == nullptr) {
1818     ResourceMark rm;
1819     THROW_MSG_0(vmSymbols::java_lang_NoSuchFieldError(), err_msg("%s.%s %s", k->external_name(), name, sig));
1820   }
1821 
1822   // Make sure class is initialized before handing id's out to fields
1823   k->initialize(CHECK_NULL);
1824 
1825   fieldDescriptor fd;
1826   if (!k->is_instance_klass() ||
1827       !InstanceKlass::cast(k)->find_field(fieldname, signame, false, &fd)) {
1828     ResourceMark rm;
1829     THROW_MSG_0(vmSymbols::java_lang_NoSuchFieldError(), err_msg("%s.%s %s", k->external_name(), name, sig));
1830   }
1831 
1832   // A jfieldID for a non-static field is simply the offset of the field within the instanceOop
1833   // It may also have hash bits for k, if VerifyJNIFields is turned on.
1834   ret = jfieldIDWorkaround::to_instance_jfieldID(k, fd.offset(), fd.is_flat());
1835   return ret;
1836 JNI_END
1837 
1838 
1839 JNI_ENTRY(jobject, jni_GetObjectField(JNIEnv *env, jobject obj, jfieldID fieldID))
1840   HOTSPOT_JNI_GETOBJECTFIELD_ENTRY(env, obj, (uintptr_t) fieldID);
1841   oop o = JNIHandles::resolve_non_null(obj);
1842   Klass* k = o->klass();
1843   int offset = jfieldIDWorkaround::from_instance_jfieldID(k, fieldID);
1844   oop res = nullptr;
1845   // Keep JVMTI addition small and only check enabled flag here.
1846   // jni_GetField_probe() assumes that is okay to create handles.
1847   if (JvmtiExport::should_post_field_access()) {
1848     o = JvmtiExport::jni_GetField_probe(thread, obj, o, k, fieldID, false);
1849   }
1850   if (!jfieldIDWorkaround::is_flat_jfieldID(fieldID)) {
1851     res = HeapAccess<ON_UNKNOWN_OOP_REF>::oop_load_at(o, offset);
1852   } else {
1853     assert(k->is_instance_klass(), "Only instance can have flat fields");
1854     InstanceKlass* ik = InstanceKlass::cast(k);
1855     fieldDescriptor fd;
1856     ik->find_field_from_offset(offset, false, &fd);  // performance bottleneck
1857     InstanceKlass* holder = fd.field_holder();
1858     InlineKlass* field_vklass = InlineKlass::cast(holder->get_inline_type_field_klass(fd.index()));
1859     res = field_vklass->read_flat_field(o, ik->field_offset(fd.index()), CHECK_NULL);
1860   }
1861   jobject ret = JNIHandles::make_local(THREAD, res);
1862   HOTSPOT_JNI_GETOBJECTFIELD_RETURN(ret);
1863   return ret;
1864 JNI_END
1865 
1866 
1867 
1868 #define DEFINE_GETFIELD(Return,Fieldname,Result \
1869   , EntryProbe, ReturnProbe) \
1870 \
1871   DT_RETURN_MARK_DECL_FOR(Result, Get##Result##Field, Return \
1872   , ReturnProbe); \
1873 \
1874 JNI_ENTRY_NO_PRESERVE(Return, jni_Get##Result##Field(JNIEnv *env, jobject obj, jfieldID fieldID)) \
1875 \
1876   EntryProbe; \
1877   Return ret = 0;\
1878   DT_RETURN_MARK_FOR(Result, Get##Result##Field, Return, (const Return&)ret);\
1879 \
1880   oop o = JNIHandles::resolve_non_null(obj); \
1881   Klass* k = o->klass(); \
1882   int offset = jfieldIDWorkaround::from_instance_jfieldID(k, fieldID);  \
1883   /* Keep JVMTI addition small and only check enabled flag here.       */ \
1884   if (JvmtiExport::should_post_field_access()) { \
1885     o = JvmtiExport::jni_GetField_probe(thread, obj, o, k, fieldID, false); \
1886   } \
1887   ret = o->Fieldname##_field(offset); \
1888   return ret; \
1889 JNI_END
1890 
1891 DEFINE_GETFIELD(jboolean, bool,   Boolean
1892                 , HOTSPOT_JNI_GETBOOLEANFIELD_ENTRY(env, obj, (uintptr_t)fieldID),
1893                 HOTSPOT_JNI_GETBOOLEANFIELD_RETURN(_ret_ref))
1894 DEFINE_GETFIELD(jbyte,    byte,   Byte
1895                 , HOTSPOT_JNI_GETBYTEFIELD_ENTRY(env, obj, (uintptr_t)fieldID),
1896                 HOTSPOT_JNI_GETBYTEFIELD_RETURN(_ret_ref))
1897 DEFINE_GETFIELD(jchar,    char,   Char
1898                 , HOTSPOT_JNI_GETCHARFIELD_ENTRY(env, obj, (uintptr_t)fieldID),
1899                 HOTSPOT_JNI_GETCHARFIELD_RETURN(_ret_ref))
1900 DEFINE_GETFIELD(jshort,   short,  Short
1901                 , HOTSPOT_JNI_GETSHORTFIELD_ENTRY(env, obj, (uintptr_t)fieldID),
1902                 HOTSPOT_JNI_GETSHORTFIELD_RETURN(_ret_ref))
1903 DEFINE_GETFIELD(jint,     int,    Int
1904                 , HOTSPOT_JNI_GETINTFIELD_ENTRY(env, obj, (uintptr_t)fieldID),
1905                 HOTSPOT_JNI_GETINTFIELD_RETURN(_ret_ref))
1906 DEFINE_GETFIELD(jlong,    long,   Long
1907                 , HOTSPOT_JNI_GETLONGFIELD_ENTRY(env, obj, (uintptr_t)fieldID),
1908                 HOTSPOT_JNI_GETLONGFIELD_RETURN(_ret_ref))
1909 // Float and double probes don't return value because dtrace doesn't currently support it
1910 DEFINE_GETFIELD(jfloat,   float,  Float
1911                 , HOTSPOT_JNI_GETFLOATFIELD_ENTRY(env, obj, (uintptr_t)fieldID),
1912                 HOTSPOT_JNI_GETFLOATFIELD_RETURN())
1913 DEFINE_GETFIELD(jdouble,  double, Double
1914                 , HOTSPOT_JNI_GETDOUBLEFIELD_ENTRY(env, obj, (uintptr_t)fieldID),
1915                 HOTSPOT_JNI_GETDOUBLEFIELD_RETURN())
1916 
1917 address jni_GetBooleanField_addr() {
1918   return (address)jni_GetBooleanField;
1919 }
1920 address jni_GetByteField_addr() {
1921   return (address)jni_GetByteField;
1922 }
1923 address jni_GetCharField_addr() {
1924   return (address)jni_GetCharField;
1925 }
1926 address jni_GetShortField_addr() {
1927   return (address)jni_GetShortField;
1928 }
1929 address jni_GetIntField_addr() {
1930   return (address)jni_GetIntField;
1931 }
1932 address jni_GetLongField_addr() {
1933   return (address)jni_GetLongField;
1934 }
1935 address jni_GetFloatField_addr() {
1936   return (address)jni_GetFloatField;
1937 }
1938 address jni_GetDoubleField_addr() {
1939   return (address)jni_GetDoubleField;
1940 }
1941 
1942 JNI_ENTRY_NO_PRESERVE(void, jni_SetObjectField(JNIEnv *env, jobject obj, jfieldID fieldID, jobject value))
1943   HOTSPOT_JNI_SETOBJECTFIELD_ENTRY(env, obj, (uintptr_t) fieldID, value);
1944   oop o = JNIHandles::resolve_non_null(obj);
1945   Klass* k = o->klass();
1946   int offset = jfieldIDWorkaround::from_instance_jfieldID(k, fieldID);
1947   // Keep JVMTI addition small and only check enabled flag here.
1948   if (JvmtiExport::should_post_field_modification()) {
1949     jvalue field_value;
1950     field_value.l = value;
1951     o = JvmtiExport::jni_SetField_probe(thread, obj, o, k, fieldID, false, JVM_SIGNATURE_CLASS, (jvalue *)&field_value);
1952   }
1953   if (!jfieldIDWorkaround::is_flat_jfieldID(fieldID)) {
1954     HeapAccess<ON_UNKNOWN_OOP_REF>::oop_store_at(o, offset, JNIHandles::resolve(value));
1955   } else {
1956     assert(k->is_instance_klass(), "Only instances can have flat fields");
1957     InstanceKlass* ik = InstanceKlass::cast(k);
1958     fieldDescriptor fd;
1959     ik->find_field_from_offset(offset, false, &fd);
1960     InstanceKlass* holder = fd.field_holder();
1961     InlineKlass* vklass = InlineKlass::cast(holder->get_inline_type_field_klass(fd.index()));
1962     oop v = JNIHandles::resolve_non_null(value);
1963     vklass->write_flat_field(o, offset, v, CHECK);
1964   }
1965   HOTSPOT_JNI_SETOBJECTFIELD_RETURN();
1966 JNI_END
1967 
1968 // TODO: make this a template
1969 
1970 #define DEFINE_SETFIELD(Argument,Fieldname,Result,SigType,unionType \
1971                         , EntryProbe, ReturnProbe) \
1972 \
1973 JNI_ENTRY_NO_PRESERVE(void, jni_Set##Result##Field(JNIEnv *env, jobject obj, jfieldID fieldID, Argument value)) \
1974 \
1975   EntryProbe; \
1976 \
1977   oop o = JNIHandles::resolve_non_null(obj); \
1978   Klass* k = o->klass(); \
1979   int offset = jfieldIDWorkaround::from_instance_jfieldID(k, fieldID);  \
1980   /* Keep JVMTI addition small and only check enabled flag here.       */ \
1981   if (JvmtiExport::should_post_field_modification()) { \
1982     jvalue field_value; \
1983     field_value.unionType = value; \
1984     o = JvmtiExport::jni_SetField_probe(thread, obj, o, k, fieldID, false, SigType, (jvalue *)&field_value); \
1985   } \
1986   o->Fieldname##_field_put(offset, value); \
1987   ReturnProbe; \
1988 JNI_END
1989 
1990 DEFINE_SETFIELD(jboolean, bool,   Boolean, JVM_SIGNATURE_BOOLEAN, z
1991                 , HOTSPOT_JNI_SETBOOLEANFIELD_ENTRY(env, obj, (uintptr_t)fieldID, value),
1992                 HOTSPOT_JNI_SETBOOLEANFIELD_RETURN())
1993 DEFINE_SETFIELD(jbyte,    byte,   Byte,    JVM_SIGNATURE_BYTE, b
1994                 , HOTSPOT_JNI_SETBYTEFIELD_ENTRY(env, obj, (uintptr_t)fieldID, value),
1995                 HOTSPOT_JNI_SETBYTEFIELD_RETURN())
1996 DEFINE_SETFIELD(jchar,    char,   Char,    JVM_SIGNATURE_CHAR, c
1997                 , HOTSPOT_JNI_SETCHARFIELD_ENTRY(env, obj, (uintptr_t)fieldID, value),
1998                 HOTSPOT_JNI_SETCHARFIELD_RETURN())
1999 DEFINE_SETFIELD(jshort,   short,  Short,   JVM_SIGNATURE_SHORT, s
2000                 , HOTSPOT_JNI_SETSHORTFIELD_ENTRY(env, obj, (uintptr_t)fieldID, value),
2001                 HOTSPOT_JNI_SETSHORTFIELD_RETURN())
2002 DEFINE_SETFIELD(jint,     int,    Int,     JVM_SIGNATURE_INT, i
2003                 , HOTSPOT_JNI_SETINTFIELD_ENTRY(env, obj, (uintptr_t)fieldID, value),
2004                 HOTSPOT_JNI_SETINTFIELD_RETURN())
2005 DEFINE_SETFIELD(jlong,    long,   Long,    JVM_SIGNATURE_LONG, j
2006                 , HOTSPOT_JNI_SETLONGFIELD_ENTRY(env, obj, (uintptr_t)fieldID, value),
2007                 HOTSPOT_JNI_SETLONGFIELD_RETURN())
2008 // Float and double probes don't return value because dtrace doesn't currently support it
2009 DEFINE_SETFIELD(jfloat,   float,  Float,   JVM_SIGNATURE_FLOAT, f
2010                 , HOTSPOT_JNI_SETFLOATFIELD_ENTRY(env, obj, (uintptr_t)fieldID),
2011                 HOTSPOT_JNI_SETFLOATFIELD_RETURN())
2012 DEFINE_SETFIELD(jdouble,  double, Double,  JVM_SIGNATURE_DOUBLE, d
2013                 , HOTSPOT_JNI_SETDOUBLEFIELD_ENTRY(env, obj, (uintptr_t)fieldID),
2014                 HOTSPOT_JNI_SETDOUBLEFIELD_RETURN())
2015 
2016 DT_RETURN_MARK_DECL(ToReflectedField, jobject
2017                     , HOTSPOT_JNI_TOREFLECTEDFIELD_RETURN(_ret_ref));
2018 
2019 JNI_ENTRY(jobject, jni_ToReflectedField(JNIEnv *env, jclass cls, jfieldID fieldID, jboolean isStatic))
2020   HOTSPOT_JNI_TOREFLECTEDFIELD_ENTRY(env, cls, (uintptr_t) fieldID, isStatic);
2021   jobject ret = nullptr;
2022   DT_RETURN_MARK(ToReflectedField, jobject, (const jobject&)ret);
2023 
2024   fieldDescriptor fd;
2025   bool found = false;
2026   Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls));
2027 
2028   assert(jfieldIDWorkaround::is_static_jfieldID(fieldID) == (isStatic != 0), "invalid fieldID");
2029 
2030   if (isStatic) {
2031     // Static field. The fieldID a JNIid specifying the field holder and the offset within the Klass*.
2032     JNIid* id = jfieldIDWorkaround::from_static_jfieldID(fieldID);
2033     assert(id->is_static_field_id(), "invalid static field id");
2034     found = id->find_local_field(&fd);
2035   } else {
2036     // Non-static field. The fieldID is really the offset of the field within the instanceOop.
2037     int offset = jfieldIDWorkaround::from_instance_jfieldID(k, fieldID);
2038     found = InstanceKlass::cast(k)->find_field_from_offset(offset, false, &fd);
2039   }
2040   assert(found, "bad fieldID passed into jni_ToReflectedField");
2041   oop reflected = Reflection::new_field(&fd, CHECK_NULL);
2042   ret = JNIHandles::make_local(THREAD, reflected);
2043   return ret;
2044 JNI_END
2045 
2046 
2047 //
2048 // Accessing Static Fields
2049 //
2050 DT_RETURN_MARK_DECL(GetStaticFieldID, jfieldID
2051                     , HOTSPOT_JNI_GETSTATICFIELDID_RETURN((uintptr_t)_ret_ref));
2052 
2053 JNI_ENTRY(jfieldID, jni_GetStaticFieldID(JNIEnv *env, jclass clazz,
2054           const char *name, const char *sig))
2055   HOTSPOT_JNI_GETSTATICFIELDID_ENTRY(env, clazz, (char *) name, (char *) sig);
2056   jfieldID ret = nullptr;
2057   DT_RETURN_MARK(GetStaticFieldID, jfieldID, (const jfieldID&)ret);
2058 
2059   // The class should have been loaded (we have an instance of the class
2060   // passed in) so the field and signature should already be in the symbol
2061   // table.  If they're not there, the field doesn't exist.
2062   TempNewSymbol fieldname = SymbolTable::probe(name, (int)strlen(name));
2063   TempNewSymbol signame = SymbolTable::probe(sig, (int)strlen(sig));
2064   if (fieldname == nullptr || signame == nullptr) {
2065     THROW_MSG_0(vmSymbols::java_lang_NoSuchFieldError(), (char*) name);
2066   }
2067   Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(clazz));
2068   // Make sure class is initialized before handing id's out to static fields
2069   k->initialize(CHECK_NULL);
2070 
2071   fieldDescriptor fd;
2072   if (!k->is_instance_klass() ||
2073       !InstanceKlass::cast(k)->find_field(fieldname, signame, true, &fd)) {
2074     THROW_MSG_0(vmSymbols::java_lang_NoSuchFieldError(), (char*) name);
2075   }
2076 
2077   // A jfieldID for a static field is a JNIid specifying the field holder and the offset within the Klass*
2078   JNIid* id = fd.field_holder()->jni_id_for(fd.offset());
2079   debug_only(id->set_is_static_field_id();)
2080 
2081   debug_only(id->verify(fd.field_holder()));
2082 
2083   ret = jfieldIDWorkaround::to_static_jfieldID(id);
2084   return ret;
2085 JNI_END
2086 
2087 
2088 JNI_ENTRY(jobject, jni_GetStaticObjectField(JNIEnv *env, jclass clazz, jfieldID fieldID))
2089   HOTSPOT_JNI_GETSTATICOBJECTFIELD_ENTRY(env, clazz, (uintptr_t) fieldID);
2090 #if INCLUDE_JNI_CHECK
2091   DEBUG_ONLY(Klass* param_k = jniCheck::validate_class(thread, clazz);)
2092 #endif // INCLUDE_JNI_CHECK
2093   JNIid* id = jfieldIDWorkaround::from_static_jfieldID(fieldID);
2094   assert(id->is_static_field_id(), "invalid static field id");
2095   // Keep JVMTI addition small and only check enabled flag here.
2096   // jni_GetField_probe() assumes that is okay to create handles.
2097   if (JvmtiExport::should_post_field_access()) {
2098     JvmtiExport::jni_GetField_probe(thread, nullptr, nullptr, id->holder(), fieldID, true);
2099   }
2100   jobject ret = JNIHandles::make_local(THREAD, id->holder()->java_mirror()->obj_field(id->offset()));
2101   HOTSPOT_JNI_GETSTATICOBJECTFIELD_RETURN(ret);
2102   return ret;
2103 JNI_END
2104 
2105 
2106 #define DEFINE_GETSTATICFIELD(Return,Fieldname,Result \
2107                               , EntryProbe, ReturnProbe) \
2108 \
2109   DT_RETURN_MARK_DECL_FOR(Result, GetStatic##Result##Field, Return \
2110                           , ReturnProbe);                                          \
2111 \
2112 JNI_ENTRY(Return, jni_GetStatic##Result##Field(JNIEnv *env, jclass clazz, jfieldID fieldID)) \
2113   EntryProbe; \
2114   Return ret = 0;\
2115   DT_RETURN_MARK_FOR(Result, GetStatic##Result##Field, Return, \
2116                      (const Return&)ret);\
2117   JNIid* id = jfieldIDWorkaround::from_static_jfieldID(fieldID); \
2118   assert(id->is_static_field_id(), "invalid static field id"); \
2119   /* Keep JVMTI addition small and only check enabled flag here. */ \
2120   /* jni_GetField_probe() assumes that is okay to create handles. */ \
2121   if (JvmtiExport::should_post_field_access()) { \
2122     JvmtiExport::jni_GetField_probe(thread, nullptr, nullptr, id->holder(), fieldID, true); \
2123   } \
2124   ret = id->holder()->java_mirror()-> Fieldname##_field (id->offset()); \
2125   return ret;\
2126 JNI_END
2127 
2128 DEFINE_GETSTATICFIELD(jboolean, bool,   Boolean
2129                       , HOTSPOT_JNI_GETSTATICBOOLEANFIELD_ENTRY(env, clazz, (uintptr_t) fieldID), HOTSPOT_JNI_GETSTATICBOOLEANFIELD_RETURN(_ret_ref))
2130 DEFINE_GETSTATICFIELD(jbyte,    byte,   Byte
2131                       , HOTSPOT_JNI_GETSTATICBYTEFIELD_ENTRY(env, clazz, (uintptr_t) fieldID),    HOTSPOT_JNI_GETSTATICBYTEFIELD_RETURN(_ret_ref)   )
2132 DEFINE_GETSTATICFIELD(jchar,    char,   Char
2133                       , HOTSPOT_JNI_GETSTATICCHARFIELD_ENTRY(env, clazz, (uintptr_t) fieldID),    HOTSPOT_JNI_GETSTATICCHARFIELD_RETURN(_ret_ref)   )
2134 DEFINE_GETSTATICFIELD(jshort,   short,  Short
2135                       , HOTSPOT_JNI_GETSTATICSHORTFIELD_ENTRY(env, clazz, (uintptr_t) fieldID),   HOTSPOT_JNI_GETSTATICSHORTFIELD_RETURN(_ret_ref)  )
2136 DEFINE_GETSTATICFIELD(jint,     int,    Int
2137                       , HOTSPOT_JNI_GETSTATICINTFIELD_ENTRY(env, clazz, (uintptr_t) fieldID),     HOTSPOT_JNI_GETSTATICINTFIELD_RETURN(_ret_ref)    )
2138 DEFINE_GETSTATICFIELD(jlong,    long,   Long
2139                       , HOTSPOT_JNI_GETSTATICLONGFIELD_ENTRY(env, clazz, (uintptr_t) fieldID),    HOTSPOT_JNI_GETSTATICLONGFIELD_RETURN(_ret_ref)   )
2140 // Float and double probes don't return value because dtrace doesn't currently support it
2141 DEFINE_GETSTATICFIELD(jfloat,   float,  Float
2142                       , HOTSPOT_JNI_GETSTATICFLOATFIELD_ENTRY(env, clazz, (uintptr_t) fieldID),   HOTSPOT_JNI_GETSTATICFLOATFIELD_RETURN()          )
2143 DEFINE_GETSTATICFIELD(jdouble,  double, Double
2144                       , HOTSPOT_JNI_GETSTATICDOUBLEFIELD_ENTRY(env, clazz, (uintptr_t) fieldID),  HOTSPOT_JNI_GETSTATICDOUBLEFIELD_RETURN()         )
2145 
2146 JNI_ENTRY(void, jni_SetStaticObjectField(JNIEnv *env, jclass clazz, jfieldID fieldID, jobject value))
2147  HOTSPOT_JNI_SETSTATICOBJECTFIELD_ENTRY(env, clazz, (uintptr_t) fieldID, value);
2148   JNIid* id = jfieldIDWorkaround::from_static_jfieldID(fieldID);
2149   assert(id->is_static_field_id(), "invalid static field id");
2150   // Keep JVMTI addition small and only check enabled flag here.
2151   // jni_SetField_probe() assumes that is okay to create handles.
2152   if (JvmtiExport::should_post_field_modification()) {
2153     jvalue field_value;
2154     field_value.l = value;
2155     JvmtiExport::jni_SetField_probe(thread, nullptr, nullptr, id->holder(), fieldID, true, JVM_SIGNATURE_CLASS, (jvalue *)&field_value);
2156   }
2157   id->holder()->java_mirror()->obj_field_put(id->offset(), JNIHandles::resolve(value));
2158   HOTSPOT_JNI_SETSTATICOBJECTFIELD_RETURN();
2159 JNI_END
2160 
2161 
2162 
2163 #define DEFINE_SETSTATICFIELD(Argument,Fieldname,Result,SigType,unionType \
2164                               , EntryProbe, ReturnProbe) \
2165 \
2166 JNI_ENTRY(void, jni_SetStatic##Result##Field(JNIEnv *env, jclass clazz, jfieldID fieldID, Argument value)) \
2167   EntryProbe; \
2168 \
2169   JNIid* id = jfieldIDWorkaround::from_static_jfieldID(fieldID); \
2170   assert(id->is_static_field_id(), "invalid static field id"); \
2171   /* Keep JVMTI addition small and only check enabled flag here. */ \
2172   /* jni_SetField_probe() assumes that is okay to create handles. */ \
2173   if (JvmtiExport::should_post_field_modification()) { \
2174     jvalue field_value; \
2175     field_value.unionType = value; \
2176     JvmtiExport::jni_SetField_probe(thread, nullptr, nullptr, id->holder(), fieldID, true, SigType, (jvalue *)&field_value); \
2177   } \
2178   id->holder()->java_mirror()-> Fieldname##_field_put (id->offset(), value); \
2179   ReturnProbe;\
2180 JNI_END
2181 
2182 DEFINE_SETSTATICFIELD(jboolean, bool,   Boolean, JVM_SIGNATURE_BOOLEAN, z
2183                       , HOTSPOT_JNI_SETSTATICBOOLEANFIELD_ENTRY(env, clazz, (uintptr_t)fieldID, value),
2184                       HOTSPOT_JNI_SETSTATICBOOLEANFIELD_RETURN())
2185 DEFINE_SETSTATICFIELD(jbyte,    byte,   Byte,    JVM_SIGNATURE_BYTE, b
2186                       , HOTSPOT_JNI_SETSTATICBYTEFIELD_ENTRY(env, clazz, (uintptr_t) fieldID, value),
2187                       HOTSPOT_JNI_SETSTATICBYTEFIELD_RETURN())
2188 DEFINE_SETSTATICFIELD(jchar,    char,   Char,    JVM_SIGNATURE_CHAR, c
2189                       , HOTSPOT_JNI_SETSTATICCHARFIELD_ENTRY(env, clazz, (uintptr_t) fieldID, value),
2190                       HOTSPOT_JNI_SETSTATICCHARFIELD_RETURN())
2191 DEFINE_SETSTATICFIELD(jshort,   short,  Short,   JVM_SIGNATURE_SHORT, s
2192                       , HOTSPOT_JNI_SETSTATICSHORTFIELD_ENTRY(env, clazz, (uintptr_t) fieldID, value),
2193                       HOTSPOT_JNI_SETSTATICSHORTFIELD_RETURN())
2194 DEFINE_SETSTATICFIELD(jint,     int,    Int,     JVM_SIGNATURE_INT, i
2195                       , HOTSPOT_JNI_SETSTATICINTFIELD_ENTRY(env, clazz, (uintptr_t) fieldID, value),
2196                       HOTSPOT_JNI_SETSTATICINTFIELD_RETURN())
2197 DEFINE_SETSTATICFIELD(jlong,    long,   Long,    JVM_SIGNATURE_LONG, j
2198                       , HOTSPOT_JNI_SETSTATICLONGFIELD_ENTRY(env, clazz, (uintptr_t) fieldID, value),
2199                       HOTSPOT_JNI_SETSTATICLONGFIELD_RETURN())
2200 // Float and double probes don't return value because dtrace doesn't currently support it
2201 DEFINE_SETSTATICFIELD(jfloat,   float,  Float,   JVM_SIGNATURE_FLOAT, f
2202                       , HOTSPOT_JNI_SETSTATICFLOATFIELD_ENTRY(env, clazz, (uintptr_t) fieldID),
2203                       HOTSPOT_JNI_SETSTATICFLOATFIELD_RETURN())
2204 DEFINE_SETSTATICFIELD(jdouble,  double, Double,  JVM_SIGNATURE_DOUBLE, d
2205                       , HOTSPOT_JNI_SETSTATICDOUBLEFIELD_ENTRY(env, clazz, (uintptr_t) fieldID),
2206                       HOTSPOT_JNI_SETSTATICDOUBLEFIELD_RETURN())
2207 
2208 //
2209 // String Operations
2210 //
2211 
2212 // Unicode Interface
2213 
2214 DT_RETURN_MARK_DECL(NewString, jstring
2215                     , HOTSPOT_JNI_NEWSTRING_RETURN(_ret_ref));
2216 
2217 JNI_ENTRY(jstring, jni_NewString(JNIEnv *env, const jchar *unicodeChars, jsize len))
2218  HOTSPOT_JNI_NEWSTRING_ENTRY(env, (uint16_t *) unicodeChars, len);
2219   jstring ret = nullptr;
2220   DT_RETURN_MARK(NewString, jstring, (const jstring&)ret);
2221   oop string=java_lang_String::create_oop_from_unicode((jchar*) unicodeChars, len, CHECK_NULL);
2222   ret = (jstring) JNIHandles::make_local(THREAD, string);
2223   return ret;
2224 JNI_END
2225 
2226 
2227 JNI_ENTRY_NO_PRESERVE(jsize, jni_GetStringLength(JNIEnv *env, jstring string))
2228   HOTSPOT_JNI_GETSTRINGLENGTH_ENTRY(env, string);
2229   jsize ret = 0;
2230   oop s = JNIHandles::resolve_non_null(string);
2231   ret = java_lang_String::length(s);
2232  HOTSPOT_JNI_GETSTRINGLENGTH_RETURN(ret);
2233   return ret;
2234 JNI_END
2235 
2236 
2237 JNI_ENTRY_NO_PRESERVE(const jchar*, jni_GetStringChars(
2238   JNIEnv *env, jstring string, jboolean *isCopy))
2239  HOTSPOT_JNI_GETSTRINGCHARS_ENTRY(env, string, (uintptr_t *) isCopy);
2240   jchar* buf = nullptr;
2241   oop s = JNIHandles::resolve_non_null(string);
2242   typeArrayOop s_value = java_lang_String::value(s);
2243   if (s_value != nullptr) {
2244     int s_len = java_lang_String::length(s, s_value);
2245     bool is_latin1 = java_lang_String::is_latin1(s);
2246     buf = NEW_C_HEAP_ARRAY_RETURN_NULL(jchar, s_len + 1, mtInternal);  // add one for zero termination
2247     /* JNI Specification states return null on OOM */
2248     if (buf != nullptr) {
2249       if (s_len > 0) {
2250         if (!is_latin1) {
2251           ArrayAccess<>::arraycopy_to_native(s_value, (size_t) typeArrayOopDesc::element_offset<jchar>(0),
2252                                              buf, s_len);
2253         } else {
2254           for (int i = 0; i < s_len; i++) {
2255             buf[i] = ((jchar) s_value->byte_at(i)) & 0xff;
2256           }
2257         }
2258       }
2259       buf[s_len] = 0;
2260       //%note jni_5
2261       if (isCopy != nullptr) {
2262         *isCopy = JNI_TRUE;
2263       }
2264     }
2265   }
2266   HOTSPOT_JNI_GETSTRINGCHARS_RETURN(buf);
2267   return buf;
2268 JNI_END
2269 
2270 
2271 JNI_ENTRY_NO_PRESERVE(void, jni_ReleaseStringChars(JNIEnv *env, jstring str, const jchar *chars))
2272   HOTSPOT_JNI_RELEASESTRINGCHARS_ENTRY(env, str, (uint16_t *) chars);
2273   //%note jni_6
2274   if (chars != nullptr) {
2275     // Since String objects are supposed to be immutable, don't copy any
2276     // new data back.  A bad user will have to go after the char array.
2277     FreeHeap((void*) chars);
2278   }
2279   HOTSPOT_JNI_RELEASESTRINGCHARS_RETURN();
2280 JNI_END
2281 
2282 
2283 // UTF Interface
2284 
2285 DT_RETURN_MARK_DECL(NewStringUTF, jstring
2286                     , HOTSPOT_JNI_NEWSTRINGUTF_RETURN(_ret_ref));
2287 
2288 JNI_ENTRY(jstring, jni_NewStringUTF(JNIEnv *env, const char *bytes))
2289   HOTSPOT_JNI_NEWSTRINGUTF_ENTRY(env, (char *) bytes);
2290   jstring ret;
2291   DT_RETURN_MARK(NewStringUTF, jstring, (const jstring&)ret);
2292 
2293   oop result = java_lang_String::create_oop_from_str((char*) bytes, CHECK_NULL);
2294   ret = (jstring) JNIHandles::make_local(THREAD, result);
2295   return ret;
2296 JNI_END
2297 
2298 
2299 JNI_ENTRY(jsize, jni_GetStringUTFLength(JNIEnv *env, jstring string))
2300  HOTSPOT_JNI_GETSTRINGUTFLENGTH_ENTRY(env, string);
2301   oop java_string = JNIHandles::resolve_non_null(string);
2302   jsize ret = java_lang_String::utf8_length(java_string);
2303   HOTSPOT_JNI_GETSTRINGUTFLENGTH_RETURN(ret);
2304   return ret;
2305 JNI_END
2306 
2307 
2308 JNI_ENTRY(const char*, jni_GetStringUTFChars(JNIEnv *env, jstring string, jboolean *isCopy))
2309  HOTSPOT_JNI_GETSTRINGUTFCHARS_ENTRY(env, string, (uintptr_t *) isCopy);
2310   char* result = nullptr;
2311   oop java_string = JNIHandles::resolve_non_null(string);
2312   typeArrayOop s_value = java_lang_String::value(java_string);
2313   if (s_value != nullptr) {
2314     size_t length = java_lang_String::utf8_length(java_string, s_value);
2315     /* JNI Specification states return null on OOM */
2316     result = AllocateHeap(length + 1, mtInternal, AllocFailStrategy::RETURN_NULL);
2317     if (result != nullptr) {
2318       java_lang_String::as_utf8_string(java_string, s_value, result, (int) length + 1);
2319       if (isCopy != nullptr) {
2320         *isCopy = JNI_TRUE;
2321       }
2322     }
2323   }
2324  HOTSPOT_JNI_GETSTRINGUTFCHARS_RETURN(result);
2325   return result;
2326 JNI_END
2327 
2328 
2329 JNI_LEAF(void, jni_ReleaseStringUTFChars(JNIEnv *env, jstring str, const char *chars))
2330  HOTSPOT_JNI_RELEASESTRINGUTFCHARS_ENTRY(env, str, (char *) chars);
2331   if (chars != nullptr) {
2332     FreeHeap((char*) chars);
2333   }
2334 HOTSPOT_JNI_RELEASESTRINGUTFCHARS_RETURN();
2335 JNI_END
2336 
2337 
2338 JNI_ENTRY_NO_PRESERVE(jsize, jni_GetArrayLength(JNIEnv *env, jarray array))
2339  HOTSPOT_JNI_GETARRAYLENGTH_ENTRY(env, array);
2340   arrayOop a = arrayOop(JNIHandles::resolve_non_null(array));
2341   assert(a->is_array(), "must be array");
2342   jsize ret = a->length();
2343  HOTSPOT_JNI_GETARRAYLENGTH_RETURN(ret);
2344   return ret;
2345 JNI_END
2346 
2347 
2348 //
2349 // Object Array Operations
2350 //
2351 
2352 DT_RETURN_MARK_DECL(NewObjectArray, jobjectArray
2353                     , HOTSPOT_JNI_NEWOBJECTARRAY_RETURN(_ret_ref));
2354 
2355 JNI_ENTRY(jobjectArray, jni_NewObjectArray(JNIEnv *env, jsize length, jclass elementClass, jobject initialElement))
2356  HOTSPOT_JNI_NEWOBJECTARRAY_ENTRY(env, length, elementClass, initialElement);
2357   jobjectArray ret = nullptr;
2358   DT_RETURN_MARK(NewObjectArray, jobjectArray, (const jobjectArray&)ret);
2359   Klass* ek = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(elementClass));
2360   Klass* ak = ek->array_klass(CHECK_NULL);
2361   ObjArrayKlass::cast(ak)->initialize(CHECK_NULL);
2362   objArrayOop result = ObjArrayKlass::cast(ak)->allocate(length, CHECK_NULL);
2363   oop initial_value = JNIHandles::resolve(initialElement);
2364   if (initial_value != nullptr) {  // array already initialized with null
2365     for (int index = 0; index < length; index++) {
2366       result->obj_at_put(index, initial_value);
2367     }
2368   }
2369   ret = (jobjectArray) JNIHandles::make_local(THREAD, result);
2370   return ret;
2371 JNI_END
2372 
2373 DT_RETURN_MARK_DECL(GetObjectArrayElement, jobject
2374                     , HOTSPOT_JNI_GETOBJECTARRAYELEMENT_RETURN(_ret_ref));
2375 
2376 JNI_ENTRY(jobject, jni_GetObjectArrayElement(JNIEnv *env, jobjectArray array, jsize index))
2377  HOTSPOT_JNI_GETOBJECTARRAYELEMENT_ENTRY(env, array, index);
2378   jobject ret = nullptr;
2379   DT_RETURN_MARK(GetObjectArrayElement, jobject, (const jobject&)ret);
2380   oop res = nullptr;
2381   arrayOop arr((arrayOop)JNIHandles::resolve_non_null(array));
2382   if (arr->is_within_bounds(index)) {
2383     if (arr->is_flatArray()) {
2384       flatArrayOop a = flatArrayOop(JNIHandles::resolve_non_null(array));
2385       flatArrayHandle vah(thread, a);
2386       res = flatArrayOopDesc::value_alloc_copy_from_index(vah, index, CHECK_NULL);
2387       assert(res != nullptr, "Must be set in one of two paths above");
2388     } else {
2389       assert(arr->is_objArray(), "If not a valueArray. must be an objArray");
2390       objArrayOop a = objArrayOop(JNIHandles::resolve_non_null(array));
2391       res = a->obj_at(index);
2392     }
2393   } else {
2394     ResourceMark rm(THREAD);
2395     stringStream ss;
2396     ss.print("Index %d out of bounds for length %d", index,arr->length());
2397     THROW_MSG_0(vmSymbols::java_lang_ArrayIndexOutOfBoundsException(), ss.as_string());
2398   }
2399   ret = JNIHandles::make_local(THREAD, res);
2400   return ret;
2401 JNI_END
2402 
2403 DT_VOID_RETURN_MARK_DECL(SetObjectArrayElement
2404                          , HOTSPOT_JNI_SETOBJECTARRAYELEMENT_RETURN());
2405 
2406 JNI_ENTRY(void, jni_SetObjectArrayElement(JNIEnv *env, jobjectArray array, jsize index, jobject value))
2407  HOTSPOT_JNI_SETOBJECTARRAYELEMENT_ENTRY(env, array, index, value);
2408   DT_VOID_RETURN_MARK(SetObjectArrayElement);
2409 
2410    bool oob = false;
2411    int length = -1;
2412    oop res = nullptr;
2413    arrayOop arr((arrayOop)JNIHandles::resolve_non_null(array));
2414    if (arr->is_within_bounds(index)) {
2415      if (arr->is_flatArray()) {
2416        flatArrayOop a = flatArrayOop(JNIHandles::resolve_non_null(array));
2417        oop v = JNIHandles::resolve(value);
2418        FlatArrayKlass* vaklass = FlatArrayKlass::cast(a->klass());
2419        InlineKlass* element_vklass = vaklass->element_klass();
2420        if (v != nullptr && v->is_a(element_vklass)) {
2421          a->value_copy_to_index(v, index);
2422        } else {
2423          ResourceMark rm(THREAD);
2424          stringStream ss;
2425          Klass *kl = FlatArrayKlass::cast(a->klass());
2426          ss.print("type mismatch: can not store %s to %s[%d]",
2427              v->klass()->external_name(),
2428              kl->external_name(),
2429              index);
2430          for (int dims = ArrayKlass::cast(a->klass())->dimension(); dims > 1; --dims) {
2431            ss.print("[]");
2432          }
2433          THROW_MSG(vmSymbols::java_lang_ArrayStoreException(), ss.as_string());
2434        }
2435      } else {
2436        assert(arr->is_objArray(), "If not a valueArray. must be an objArray");
2437        objArrayOop a = objArrayOop(JNIHandles::resolve_non_null(array));
2438        oop v = JNIHandles::resolve(value);
2439        if (v == nullptr || v->is_a(ObjArrayKlass::cast(a->klass())->element_klass())) {
2440          a->obj_at_put(index, v);
2441        } else {
2442          ResourceMark rm(THREAD);
2443          stringStream ss;
2444          Klass *bottom_kl = ObjArrayKlass::cast(a->klass())->bottom_klass();
2445          ss.print("type mismatch: can not store %s to %s[%d]",
2446              v->klass()->external_name(),
2447              bottom_kl->is_typeArray_klass() ? type2name_tab[ArrayKlass::cast(bottom_kl)->element_type()] : bottom_kl->external_name(),
2448                  index);
2449          for (int dims = ArrayKlass::cast(a->klass())->dimension(); dims > 1; --dims) {
2450            ss.print("[]");
2451          }
2452          THROW_MSG(vmSymbols::java_lang_ArrayStoreException(), ss.as_string());
2453        }
2454      }
2455    } else {
2456      ResourceMark rm(THREAD);
2457      stringStream ss;
2458      ss.print("Index %d out of bounds for length %d", index, arr->length());
2459      THROW_MSG(vmSymbols::java_lang_ArrayIndexOutOfBoundsException(), ss.as_string());
2460    }
2461 JNI_END
2462 
2463 
2464 
2465 #define DEFINE_NEWSCALARARRAY(Return,Allocator,Result \
2466                               ,EntryProbe,ReturnProbe)  \
2467 \
2468   DT_RETURN_MARK_DECL(New##Result##Array, Return \
2469                       , ReturnProbe); \
2470 \
2471 JNI_ENTRY(Return, \
2472           jni_New##Result##Array(JNIEnv *env, jsize len)) \
2473   EntryProbe; \
2474   Return ret = nullptr;\
2475   DT_RETURN_MARK(New##Result##Array, Return, (const Return&)ret);\
2476 \
2477   oop obj= oopFactory::Allocator(len, CHECK_NULL); \
2478   ret = (Return) JNIHandles::make_local(THREAD, obj); \
2479   return ret;\
2480 JNI_END
2481 
2482 DEFINE_NEWSCALARARRAY(jbooleanArray, new_boolArray,   Boolean,
2483                       HOTSPOT_JNI_NEWBOOLEANARRAY_ENTRY(env, len),
2484                       HOTSPOT_JNI_NEWBOOLEANARRAY_RETURN(_ret_ref))
2485 DEFINE_NEWSCALARARRAY(jbyteArray,    new_byteArray,   Byte,
2486                       HOTSPOT_JNI_NEWBYTEARRAY_ENTRY(env, len),
2487                       HOTSPOT_JNI_NEWBYTEARRAY_RETURN(_ret_ref))
2488 DEFINE_NEWSCALARARRAY(jshortArray,   new_shortArray,  Short,
2489                       HOTSPOT_JNI_NEWSHORTARRAY_ENTRY(env, len),
2490                       HOTSPOT_JNI_NEWSHORTARRAY_RETURN(_ret_ref))
2491 DEFINE_NEWSCALARARRAY(jcharArray,    new_charArray,   Char,
2492                       HOTSPOT_JNI_NEWCHARARRAY_ENTRY(env, len),
2493                       HOTSPOT_JNI_NEWCHARARRAY_RETURN(_ret_ref))
2494 DEFINE_NEWSCALARARRAY(jintArray,     new_intArray,    Int,
2495                       HOTSPOT_JNI_NEWINTARRAY_ENTRY(env, len),
2496                       HOTSPOT_JNI_NEWINTARRAY_RETURN(_ret_ref))
2497 DEFINE_NEWSCALARARRAY(jlongArray,    new_longArray,   Long,
2498                       HOTSPOT_JNI_NEWLONGARRAY_ENTRY(env, len),
2499                       HOTSPOT_JNI_NEWLONGARRAY_RETURN(_ret_ref))
2500 DEFINE_NEWSCALARARRAY(jfloatArray,   new_floatArray,  Float,
2501                       HOTSPOT_JNI_NEWFLOATARRAY_ENTRY(env, len),
2502                       HOTSPOT_JNI_NEWFLOATARRAY_RETURN(_ret_ref))
2503 DEFINE_NEWSCALARARRAY(jdoubleArray,  new_doubleArray, Double,
2504                       HOTSPOT_JNI_NEWDOUBLEARRAY_ENTRY(env, len),
2505                       HOTSPOT_JNI_NEWDOUBLEARRAY_RETURN(_ret_ref))
2506 
2507 // Return an address which will fault if the caller writes to it.
2508 
2509 static char* get_bad_address() {
2510   static char* bad_address = nullptr;
2511   if (bad_address == nullptr) {
2512     size_t size = os::vm_allocation_granularity();
2513     bad_address = os::reserve_memory(size);
2514     if (bad_address != nullptr) {
2515       os::protect_memory(bad_address, size, os::MEM_PROT_READ,
2516                          /*is_committed*/false);
2517       MemTracker::record_virtual_memory_type((void*)bad_address, mtInternal);
2518     }
2519   }
2520   return bad_address;
2521 }
2522 
2523 
2524 
2525 #define DEFINE_GETSCALARARRAYELEMENTS(ElementTag,ElementType,Result, Tag \
2526                                       , EntryProbe, ReturnProbe) \
2527 \
2528 JNI_ENTRY_NO_PRESERVE(ElementType*, \
2529           jni_Get##Result##ArrayElements(JNIEnv *env, ElementType##Array array, jboolean *isCopy)) \
2530   EntryProbe; \
2531   /* allocate an chunk of memory in c land */ \
2532   typeArrayOop a = typeArrayOop(JNIHandles::resolve_non_null(array)); \
2533   ElementType* result; \
2534   int len = a->length(); \
2535   if (len == 0) { \
2536     if (isCopy != nullptr) { \
2537       *isCopy = JNI_FALSE; \
2538     } \
2539     /* Empty array: legal but useless, can't return null. \
2540      * Return a pointer to something useless. \
2541      * Avoid asserts in typeArrayOop. */ \
2542     result = (ElementType*)get_bad_address(); \
2543   } else { \
2544     /* JNI Specification states return null on OOM */                    \
2545     result = NEW_C_HEAP_ARRAY_RETURN_NULL(ElementType, len, mtInternal); \
2546     if (result != nullptr) {                                             \
2547       /* copy the array to the c chunk */                                \
2548       ArrayAccess<>::arraycopy_to_native(a, typeArrayOopDesc::element_offset<ElementType>(0), \
2549                                          result, len);                   \
2550       if (isCopy) {                                                      \
2551         *isCopy = JNI_TRUE;                                              \
2552       }                                                                  \
2553     }                                                                    \
2554   } \
2555   ReturnProbe; \
2556   return result; \
2557 JNI_END
2558 
2559 DEFINE_GETSCALARARRAYELEMENTS(T_BOOLEAN, jboolean, Boolean, bool
2560                               , HOTSPOT_JNI_GETBOOLEANARRAYELEMENTS_ENTRY(env, array, (uintptr_t *) isCopy),
2561                               HOTSPOT_JNI_GETBOOLEANARRAYELEMENTS_RETURN((uintptr_t*)result))
2562 DEFINE_GETSCALARARRAYELEMENTS(T_BYTE,    jbyte,    Byte,    byte
2563                               , HOTSPOT_JNI_GETBYTEARRAYELEMENTS_ENTRY(env, array, (uintptr_t *) isCopy),
2564                               HOTSPOT_JNI_GETBYTEARRAYELEMENTS_RETURN((char*)result))
2565 DEFINE_GETSCALARARRAYELEMENTS(T_SHORT,   jshort,   Short,   short
2566                               , HOTSPOT_JNI_GETSHORTARRAYELEMENTS_ENTRY(env, (uint16_t*) array, (uintptr_t *) isCopy),
2567                               HOTSPOT_JNI_GETSHORTARRAYELEMENTS_RETURN((uint16_t*)result))
2568 DEFINE_GETSCALARARRAYELEMENTS(T_CHAR,    jchar,    Char,    char
2569                               , HOTSPOT_JNI_GETCHARARRAYELEMENTS_ENTRY(env, (uint16_t*) array, (uintptr_t *) isCopy),
2570                               HOTSPOT_JNI_GETCHARARRAYELEMENTS_RETURN(result))
2571 DEFINE_GETSCALARARRAYELEMENTS(T_INT,     jint,     Int,     int
2572                               , HOTSPOT_JNI_GETINTARRAYELEMENTS_ENTRY(env, array, (uintptr_t *) isCopy),
2573                               HOTSPOT_JNI_GETINTARRAYELEMENTS_RETURN((uint32_t*)result))
2574 DEFINE_GETSCALARARRAYELEMENTS(T_LONG,    jlong,    Long,    long
2575                               , HOTSPOT_JNI_GETLONGARRAYELEMENTS_ENTRY(env, array, (uintptr_t *) isCopy),
2576                               HOTSPOT_JNI_GETLONGARRAYELEMENTS_RETURN(((uintptr_t*)result)))
2577 // Float and double probes don't return value because dtrace doesn't currently support it
2578 DEFINE_GETSCALARARRAYELEMENTS(T_FLOAT,   jfloat,   Float,   float
2579                               , HOTSPOT_JNI_GETFLOATARRAYELEMENTS_ENTRY(env, array, (uintptr_t *) isCopy),
2580                               HOTSPOT_JNI_GETFLOATARRAYELEMENTS_RETURN(result))
2581 DEFINE_GETSCALARARRAYELEMENTS(T_DOUBLE,  jdouble,  Double,  double
2582                               , HOTSPOT_JNI_GETDOUBLEARRAYELEMENTS_ENTRY(env, array, (uintptr_t *) isCopy),
2583                               HOTSPOT_JNI_GETDOUBLEARRAYELEMENTS_RETURN(result))
2584 
2585 
2586 #define DEFINE_RELEASESCALARARRAYELEMENTS(ElementTag,ElementType,Result,Tag \
2587                                           , EntryProbe, ReturnProbe);\
2588 \
2589 JNI_ENTRY_NO_PRESERVE(void, \
2590           jni_Release##Result##ArrayElements(JNIEnv *env, ElementType##Array array, \
2591                                              ElementType *buf, jint mode)) \
2592   EntryProbe; \
2593   typeArrayOop a = typeArrayOop(JNIHandles::resolve_non_null(array)); \
2594   int len = a->length(); \
2595   if (len != 0) {   /* Empty array:  nothing to free or copy. */  \
2596     if ((mode == 0) || (mode == JNI_COMMIT)) { \
2597       ArrayAccess<>::arraycopy_from_native(buf, a, typeArrayOopDesc::element_offset<ElementType>(0), len); \
2598     } \
2599     if ((mode == 0) || (mode == JNI_ABORT)) { \
2600       FreeHeap(buf); \
2601     } \
2602   } \
2603   ReturnProbe; \
2604 JNI_END
2605 
2606 DEFINE_RELEASESCALARARRAYELEMENTS(T_BOOLEAN, jboolean, Boolean, bool
2607                                   , HOTSPOT_JNI_RELEASEBOOLEANARRAYELEMENTS_ENTRY(env, array, (uintptr_t *) buf, mode),
2608                                   HOTSPOT_JNI_RELEASEBOOLEANARRAYELEMENTS_RETURN())
2609 DEFINE_RELEASESCALARARRAYELEMENTS(T_BYTE,    jbyte,    Byte,    byte
2610                                   , HOTSPOT_JNI_RELEASEBYTEARRAYELEMENTS_ENTRY(env, array, (char *) buf, mode),
2611                                   HOTSPOT_JNI_RELEASEBYTEARRAYELEMENTS_RETURN())
2612 DEFINE_RELEASESCALARARRAYELEMENTS(T_SHORT,   jshort,   Short,   short
2613                                   ,  HOTSPOT_JNI_RELEASESHORTARRAYELEMENTS_ENTRY(env, array, (uint16_t *) buf, mode),
2614                                   HOTSPOT_JNI_RELEASESHORTARRAYELEMENTS_RETURN())
2615 DEFINE_RELEASESCALARARRAYELEMENTS(T_CHAR,    jchar,    Char,    char
2616                                   ,  HOTSPOT_JNI_RELEASECHARARRAYELEMENTS_ENTRY(env, array, (uint16_t *) buf, mode),
2617                                   HOTSPOT_JNI_RELEASECHARARRAYELEMENTS_RETURN())
2618 DEFINE_RELEASESCALARARRAYELEMENTS(T_INT,     jint,     Int,     int
2619                                   , HOTSPOT_JNI_RELEASEINTARRAYELEMENTS_ENTRY(env, array, (uint32_t *) buf, mode),
2620                                   HOTSPOT_JNI_RELEASEINTARRAYELEMENTS_RETURN())
2621 DEFINE_RELEASESCALARARRAYELEMENTS(T_LONG,    jlong,    Long,    long
2622                                   , HOTSPOT_JNI_RELEASELONGARRAYELEMENTS_ENTRY(env, array, (uintptr_t *) buf, mode),
2623                                   HOTSPOT_JNI_RELEASELONGARRAYELEMENTS_RETURN())
2624 DEFINE_RELEASESCALARARRAYELEMENTS(T_FLOAT,   jfloat,   Float,   float
2625                                   , HOTSPOT_JNI_RELEASEFLOATARRAYELEMENTS_ENTRY(env, array, (float *) buf, mode),
2626                                   HOTSPOT_JNI_RELEASEFLOATARRAYELEMENTS_RETURN())
2627 DEFINE_RELEASESCALARARRAYELEMENTS(T_DOUBLE,  jdouble,  Double,  double
2628                                   , HOTSPOT_JNI_RELEASEDOUBLEARRAYELEMENTS_ENTRY(env, array, (double *) buf, mode),
2629                                   HOTSPOT_JNI_RELEASEDOUBLEARRAYELEMENTS_RETURN())
2630 
2631 static void check_bounds(jsize start, jsize copy_len, jsize array_len, TRAPS) {
2632   ResourceMark rm(THREAD);
2633   if (copy_len < 0) {
2634     stringStream ss;
2635     ss.print("Length %d is negative", copy_len);
2636     THROW_MSG(vmSymbols::java_lang_ArrayIndexOutOfBoundsException(), ss.as_string());
2637   } else if (start < 0 || (start > array_len - copy_len)) {
2638     stringStream ss;
2639     ss.print("Array region %d.." INT64_FORMAT " out of bounds for length %d",
2640              start, (int64_t)start+(int64_t)copy_len, array_len);
2641     THROW_MSG(vmSymbols::java_lang_ArrayIndexOutOfBoundsException(), ss.as_string());
2642   }
2643 }
2644 
2645 #define DEFINE_GETSCALARARRAYREGION(ElementTag,ElementType,Result, Tag \
2646                                     , EntryProbe, ReturnProbe); \
2647   DT_VOID_RETURN_MARK_DECL(Get##Result##ArrayRegion \
2648                            , ReturnProbe); \
2649 \
2650 JNI_ENTRY(void, \
2651 jni_Get##Result##ArrayRegion(JNIEnv *env, ElementType##Array array, jsize start, \
2652              jsize len, ElementType *buf)) \
2653   EntryProbe; \
2654   DT_VOID_RETURN_MARK(Get##Result##ArrayRegion); \
2655   typeArrayOop src = typeArrayOop(JNIHandles::resolve_non_null(array)); \
2656   check_bounds(start, len, src->length(), CHECK); \
2657   if (len > 0) {    \
2658     ArrayAccess<>::arraycopy_to_native(src, typeArrayOopDesc::element_offset<ElementType>(start), buf, len); \
2659   } \
2660 JNI_END
2661 
2662 DEFINE_GETSCALARARRAYREGION(T_BOOLEAN, jboolean,Boolean, bool
2663                             , HOTSPOT_JNI_GETBOOLEANARRAYREGION_ENTRY(env, array, start, len, (uintptr_t *) buf),
2664                             HOTSPOT_JNI_GETBOOLEANARRAYREGION_RETURN());
2665 DEFINE_GETSCALARARRAYREGION(T_BYTE,    jbyte,   Byte,    byte
2666                             ,  HOTSPOT_JNI_GETBYTEARRAYREGION_ENTRY(env, array, start, len, (char *) buf),
2667                             HOTSPOT_JNI_GETBYTEARRAYREGION_RETURN());
2668 DEFINE_GETSCALARARRAYREGION(T_SHORT,   jshort,  Short,   short
2669                             , HOTSPOT_JNI_GETSHORTARRAYREGION_ENTRY(env, array, start, len, (uint16_t *) buf),
2670                             HOTSPOT_JNI_GETSHORTARRAYREGION_RETURN());
2671 DEFINE_GETSCALARARRAYREGION(T_CHAR,    jchar,   Char,    char
2672                             ,  HOTSPOT_JNI_GETCHARARRAYREGION_ENTRY(env, array, start, len, (uint16_t*) buf),
2673                             HOTSPOT_JNI_GETCHARARRAYREGION_RETURN());
2674 DEFINE_GETSCALARARRAYREGION(T_INT,     jint,    Int,     int
2675                             , HOTSPOT_JNI_GETINTARRAYREGION_ENTRY(env, array, start, len, (uint32_t*) buf),
2676                             HOTSPOT_JNI_GETINTARRAYREGION_RETURN());
2677 DEFINE_GETSCALARARRAYREGION(T_LONG,    jlong,   Long,    long
2678                             ,  HOTSPOT_JNI_GETLONGARRAYREGION_ENTRY(env, array, start, len, (uintptr_t *) buf),
2679                             HOTSPOT_JNI_GETLONGARRAYREGION_RETURN());
2680 DEFINE_GETSCALARARRAYREGION(T_FLOAT,   jfloat,  Float,   float
2681                             , HOTSPOT_JNI_GETFLOATARRAYREGION_ENTRY(env, array, start, len, (float *) buf),
2682                             HOTSPOT_JNI_GETFLOATARRAYREGION_RETURN());
2683 DEFINE_GETSCALARARRAYREGION(T_DOUBLE,  jdouble, Double,  double
2684                             , HOTSPOT_JNI_GETDOUBLEARRAYREGION_ENTRY(env, array, start, len, (double *) buf),
2685                             HOTSPOT_JNI_GETDOUBLEARRAYREGION_RETURN());
2686 
2687 
2688 #define DEFINE_SETSCALARARRAYREGION(ElementTag,ElementType,Result, Tag \
2689                                     , EntryProbe, ReturnProbe); \
2690   DT_VOID_RETURN_MARK_DECL(Set##Result##ArrayRegion \
2691                            ,ReturnProbe);           \
2692 \
2693 JNI_ENTRY(void, \
2694 jni_Set##Result##ArrayRegion(JNIEnv *env, ElementType##Array array, jsize start, \
2695              jsize len, const ElementType *buf)) \
2696   EntryProbe; \
2697   DT_VOID_RETURN_MARK(Set##Result##ArrayRegion); \
2698   typeArrayOop dst = typeArrayOop(JNIHandles::resolve_non_null(array)); \
2699   check_bounds(start, len, dst->length(), CHECK); \
2700   if (len > 0) { \
2701     ArrayAccess<>::arraycopy_from_native(buf, dst, typeArrayOopDesc::element_offset<ElementType>(start), len); \
2702   } \
2703 JNI_END
2704 
2705 DEFINE_SETSCALARARRAYREGION(T_BOOLEAN, jboolean, Boolean, bool
2706                             , HOTSPOT_JNI_SETBOOLEANARRAYREGION_ENTRY(env, array, start, len, (uintptr_t *)buf),
2707                             HOTSPOT_JNI_SETBOOLEANARRAYREGION_RETURN())
2708 DEFINE_SETSCALARARRAYREGION(T_BYTE,    jbyte,    Byte,    byte
2709                             , HOTSPOT_JNI_SETBYTEARRAYREGION_ENTRY(env, array, start, len, (char *) buf),
2710                             HOTSPOT_JNI_SETBYTEARRAYREGION_RETURN())
2711 DEFINE_SETSCALARARRAYREGION(T_SHORT,   jshort,   Short,   short
2712                             , HOTSPOT_JNI_SETSHORTARRAYREGION_ENTRY(env, array, start, len, (uint16_t *) buf),
2713                             HOTSPOT_JNI_SETSHORTARRAYREGION_RETURN())
2714 DEFINE_SETSCALARARRAYREGION(T_CHAR,    jchar,    Char,    char
2715                             , HOTSPOT_JNI_SETCHARARRAYREGION_ENTRY(env, array, start, len, (uint16_t *) buf),
2716                             HOTSPOT_JNI_SETCHARARRAYREGION_RETURN())
2717 DEFINE_SETSCALARARRAYREGION(T_INT,     jint,     Int,     int
2718                             , HOTSPOT_JNI_SETINTARRAYREGION_ENTRY(env, array, start, len, (uint32_t *) buf),
2719                             HOTSPOT_JNI_SETINTARRAYREGION_RETURN())
2720 DEFINE_SETSCALARARRAYREGION(T_LONG,    jlong,    Long,    long
2721                             , HOTSPOT_JNI_SETLONGARRAYREGION_ENTRY(env, array, start, len, (uintptr_t *) buf),
2722                             HOTSPOT_JNI_SETLONGARRAYREGION_RETURN())
2723 DEFINE_SETSCALARARRAYREGION(T_FLOAT,   jfloat,   Float,   float
2724                             , HOTSPOT_JNI_SETFLOATARRAYREGION_ENTRY(env, array, start, len, (float *) buf),
2725                             HOTSPOT_JNI_SETFLOATARRAYREGION_RETURN())
2726 DEFINE_SETSCALARARRAYREGION(T_DOUBLE,  jdouble,  Double,  double
2727                             , HOTSPOT_JNI_SETDOUBLEARRAYREGION_ENTRY(env, array, start, len, (double *) buf),
2728                             HOTSPOT_JNI_SETDOUBLEARRAYREGION_RETURN())
2729 
2730 
2731 DT_RETURN_MARK_DECL(RegisterNatives, jint
2732                     , HOTSPOT_JNI_REGISTERNATIVES_RETURN(_ret_ref));
2733 
2734 JNI_ENTRY(jint, jni_RegisterNatives(JNIEnv *env, jclass clazz,
2735                                     const JNINativeMethod *methods,
2736                                     jint nMethods))
2737   HOTSPOT_JNI_REGISTERNATIVES_ENTRY(env, clazz, (void *) methods, nMethods);
2738   jint ret = 0;
2739   DT_RETURN_MARK(RegisterNatives, jint, (const jint&)ret);
2740 
2741   Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(clazz));
2742 
2743   // There are no restrictions on native code registering native methods,
2744   // which allows agents to redefine the bindings to native methods, however
2745   // we issue a warning if any code running outside of the boot/platform
2746   // loader is rebinding any native methods in classes loaded by the
2747   // boot/platform loader that are in named modules. That will catch changes
2748   // to platform classes while excluding classes added to the bootclasspath.
2749   bool do_warning = false;
2750 
2751   // Only instanceKlasses can have native methods
2752   if (k->is_instance_klass()) {
2753     oop cl = k->class_loader();
2754     InstanceKlass* ik = InstanceKlass::cast(k);
2755     // Check for a platform class
2756     if ((cl ==  nullptr || SystemDictionary::is_platform_class_loader(cl)) &&
2757         ik->module()->is_named()) {
2758       Klass* caller = thread->security_get_caller_class(1);
2759       // If no caller class, or caller class has a different loader, then
2760       // issue a warning below.
2761       do_warning = (caller == nullptr) || caller->class_loader() != cl;
2762     }
2763   }
2764 
2765 
2766   for (int index = 0; index < nMethods; index++) {
2767     const char* meth_name = methods[index].name;
2768     const char* meth_sig = methods[index].signature;
2769     int meth_name_len = (int)strlen(meth_name);
2770 
2771     // The class should have been loaded (we have an instance of the class
2772     // passed in) so the method and signature should already be in the symbol
2773     // table.  If they're not there, the method doesn't exist.
2774     TempNewSymbol  name = SymbolTable::probe(meth_name, meth_name_len);
2775     TempNewSymbol  signature = SymbolTable::probe(meth_sig, (int)strlen(meth_sig));
2776 
2777     if (name == nullptr || signature == nullptr) {
2778       ResourceMark rm(THREAD);
2779       stringStream st;
2780       st.print("Method %s.%s%s not found", k->external_name(), meth_name, meth_sig);
2781       // Must return negative value on failure
2782       THROW_MSG_(vmSymbols::java_lang_NoSuchMethodError(), st.as_string(), -1);
2783     }
2784 
2785     if (do_warning) {
2786       ResourceMark rm(THREAD);
2787       log_warning(jni, resolve)("Re-registering of platform native method: %s.%s%s "
2788               "from code in a different classloader", k->external_name(), meth_name, meth_sig);
2789     }
2790 
2791     bool res = Method::register_native(k, name, signature,
2792                                        (address) methods[index].fnPtr, THREAD);
2793     if (!res) {
2794       ret = -1;
2795       break;
2796     }
2797   }
2798   return ret;
2799 JNI_END
2800 
2801 
2802 JNI_ENTRY(jint, jni_UnregisterNatives(JNIEnv *env, jclass clazz))
2803  HOTSPOT_JNI_UNREGISTERNATIVES_ENTRY(env, clazz);
2804   Klass* k   = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(clazz));
2805   //%note jni_2
2806   if (k->is_instance_klass()) {
2807     for (int index = 0; index < InstanceKlass::cast(k)->methods()->length(); index++) {
2808       Method* m = InstanceKlass::cast(k)->methods()->at(index);
2809       if (m->is_native()) {
2810         m->clear_native_function();
2811         m->set_signature_handler(nullptr);
2812       }
2813     }
2814   }
2815  HOTSPOT_JNI_UNREGISTERNATIVES_RETURN(0);
2816   return 0;
2817 JNI_END
2818 
2819 //
2820 // Monitor functions
2821 //
2822 
2823 DT_RETURN_MARK_DECL(MonitorEnter, jint
2824                     , HOTSPOT_JNI_MONITORENTER_RETURN(_ret_ref));
2825 
2826 JNI_ENTRY(jint, jni_MonitorEnter(JNIEnv *env, jobject jobj))
2827  HOTSPOT_JNI_MONITORENTER_ENTRY(env, jobj);
2828   jint ret = JNI_ERR;
2829   DT_RETURN_MARK(MonitorEnter, jint, (const jint&)ret);
2830 
2831   // If the object is null, we can't do anything with it
2832   if (jobj == nullptr) {
2833     THROW_(vmSymbols::java_lang_NullPointerException(), JNI_ERR);
2834   }
2835 
2836   Handle obj(thread, JNIHandles::resolve_non_null(jobj));
2837   ObjectSynchronizer::jni_enter(obj, thread);
2838   return JNI_OK;
2839 JNI_END
2840 
2841 DT_RETURN_MARK_DECL(MonitorExit, jint
2842                     , HOTSPOT_JNI_MONITOREXIT_RETURN(_ret_ref));
2843 
2844 JNI_ENTRY(jint, jni_MonitorExit(JNIEnv *env, jobject jobj))
2845  HOTSPOT_JNI_MONITOREXIT_ENTRY(env, jobj);
2846   jint ret = JNI_ERR;
2847   DT_RETURN_MARK(MonitorExit, jint, (const jint&)ret);
2848 
2849   // Don't do anything with a null object
2850   if (jobj == nullptr) {
2851     THROW_(vmSymbols::java_lang_NullPointerException(), JNI_ERR);
2852   }
2853 
2854   Handle obj(THREAD, JNIHandles::resolve_non_null(jobj));
2855   ObjectSynchronizer::jni_exit(obj(), CHECK_(JNI_ERR));
2856   return JNI_OK;
2857 JNI_END
2858 
2859 //
2860 // Extensions
2861 //
2862 
2863 DT_VOID_RETURN_MARK_DECL(GetStringRegion
2864                          , HOTSPOT_JNI_GETSTRINGREGION_RETURN());
2865 
2866 JNI_ENTRY(void, jni_GetStringRegion(JNIEnv *env, jstring string, jsize start, jsize len, jchar *buf))
2867  HOTSPOT_JNI_GETSTRINGREGION_ENTRY(env, string, start, len, buf);
2868   DT_VOID_RETURN_MARK(GetStringRegion);
2869   oop s = JNIHandles::resolve_non_null(string);
2870   typeArrayOop s_value = java_lang_String::value(s);
2871   int s_len = java_lang_String::length(s, s_value);
2872   if (start < 0 || len < 0 || start > s_len - len) {
2873     THROW(vmSymbols::java_lang_StringIndexOutOfBoundsException());
2874   } else {
2875     if (len > 0) {
2876       bool is_latin1 = java_lang_String::is_latin1(s);
2877       if (!is_latin1) {
2878         ArrayAccess<>::arraycopy_to_native(s_value, typeArrayOopDesc::element_offset<jchar>(start),
2879                                            buf, len);
2880       } else {
2881         for (int i = 0; i < len; i++) {
2882           buf[i] = ((jchar) s_value->byte_at(i + start)) & 0xff;
2883         }
2884       }
2885     }
2886   }
2887 JNI_END
2888 
2889 DT_VOID_RETURN_MARK_DECL(GetStringUTFRegion
2890                          , HOTSPOT_JNI_GETSTRINGUTFREGION_RETURN());
2891 
2892 JNI_ENTRY(void, jni_GetStringUTFRegion(JNIEnv *env, jstring string, jsize start, jsize len, char *buf))
2893  HOTSPOT_JNI_GETSTRINGUTFREGION_ENTRY(env, string, start, len, buf);
2894   DT_VOID_RETURN_MARK(GetStringUTFRegion);
2895   oop s = JNIHandles::resolve_non_null(string);
2896   typeArrayOop s_value = java_lang_String::value(s);
2897   int s_len = java_lang_String::length(s, s_value);
2898   if (start < 0 || len < 0 || start > s_len - len) {
2899     THROW(vmSymbols::java_lang_StringIndexOutOfBoundsException());
2900   } else {
2901     //%note jni_7
2902     if (len > 0) {
2903       // Assume the buffer is large enough as the JNI spec. does not require user error checking
2904       java_lang_String::as_utf8_string(s, s_value, start, len, buf, INT_MAX);
2905       // as_utf8_string null-terminates the result string
2906     } else {
2907       // JDK null-terminates the buffer even in len is zero
2908       if (buf != nullptr) {
2909         buf[0] = 0;
2910       }
2911     }
2912   }
2913 JNI_END
2914 
2915 JNI_ENTRY(void*, jni_GetPrimitiveArrayCritical(JNIEnv *env, jarray array, jboolean *isCopy))
2916  HOTSPOT_JNI_GETPRIMITIVEARRAYCRITICAL_ENTRY(env, array, (uintptr_t *) isCopy);
2917   Handle a(thread, JNIHandles::resolve_non_null(array));
2918   assert(a->is_typeArray(), "just checking");
2919 
2920   // Pin object
2921   Universe::heap()->pin_object(thread, a());
2922 
2923   BasicType type = TypeArrayKlass::cast(a->klass())->element_type();
2924   void* ret = arrayOop(a())->base(type);
2925   if (isCopy != nullptr) {
2926     *isCopy = JNI_FALSE;
2927   }
2928  HOTSPOT_JNI_GETPRIMITIVEARRAYCRITICAL_RETURN(ret);
2929   return ret;
2930 JNI_END
2931 
2932 
2933 JNI_ENTRY(void, jni_ReleasePrimitiveArrayCritical(JNIEnv *env, jarray array, void *carray, jint mode))
2934   HOTSPOT_JNI_RELEASEPRIMITIVEARRAYCRITICAL_ENTRY(env, array, carray, mode);
2935   // Unpin object
2936   Universe::heap()->unpin_object(thread, JNIHandles::resolve_non_null(array));
2937 HOTSPOT_JNI_RELEASEPRIMITIVEARRAYCRITICAL_RETURN();
2938 JNI_END
2939 
2940 
2941 JNI_ENTRY(const jchar*, jni_GetStringCritical(JNIEnv *env, jstring string, jboolean *isCopy))
2942   HOTSPOT_JNI_GETSTRINGCRITICAL_ENTRY(env, string, (uintptr_t *) isCopy);
2943   oop s = JNIHandles::resolve_non_null(string);
2944   jchar* ret;
2945   if (!java_lang_String::is_latin1(s)) {
2946     typeArrayHandle s_value(thread, java_lang_String::value(s));
2947 
2948     // Pin value array
2949     Universe::heap()->pin_object(thread, s_value());
2950 
2951     ret = (jchar*) s_value->base(T_CHAR);
2952     if (isCopy != nullptr) *isCopy = JNI_FALSE;
2953   } else {
2954     // Inflate latin1 encoded string to UTF16
2955     typeArrayOop s_value = java_lang_String::value(s);
2956     int s_len = java_lang_String::length(s, s_value);
2957     ret = NEW_C_HEAP_ARRAY_RETURN_NULL(jchar, s_len + 1, mtInternal);  // add one for zero termination
2958     /* JNI Specification states return null on OOM */
2959     if (ret != nullptr) {
2960       for (int i = 0; i < s_len; i++) {
2961         ret[i] = ((jchar) s_value->byte_at(i)) & 0xff;
2962       }
2963       ret[s_len] = 0;
2964     }
2965     if (isCopy != nullptr) *isCopy = JNI_TRUE;
2966   }
2967  HOTSPOT_JNI_GETSTRINGCRITICAL_RETURN((uint16_t *) ret);
2968   return ret;
2969 JNI_END
2970 
2971 
2972 JNI_ENTRY(void, jni_ReleaseStringCritical(JNIEnv *env, jstring str, const jchar *chars))
2973   HOTSPOT_JNI_RELEASESTRINGCRITICAL_ENTRY(env, str, (uint16_t *) chars);
2974   oop s = JNIHandles::resolve_non_null(str);
2975   bool is_latin1 = java_lang_String::is_latin1(s);
2976 
2977   if (is_latin1) {
2978     // For latin1 string, free jchar array allocated by earlier call to GetStringCritical.
2979     // This assumes that ReleaseStringCritical bookends GetStringCritical.
2980     FREE_C_HEAP_ARRAY(jchar, chars);
2981   } else {
2982     // StringDedup can have replaced the value array, so don't fetch the array from 's'.
2983     // Instead, we calculate the address based on the jchar array exposed with GetStringCritical.
2984     oop value = cast_to_oop((address)chars - arrayOopDesc::base_offset_in_bytes(T_CHAR));
2985 
2986     // Unpin value array
2987     Universe::heap()->unpin_object(thread, value);
2988   }
2989 HOTSPOT_JNI_RELEASESTRINGCRITICAL_RETURN();
2990 JNI_END
2991 
2992 
2993 JNI_ENTRY(jweak, jni_NewWeakGlobalRef(JNIEnv *env, jobject ref))
2994   HOTSPOT_JNI_NEWWEAKGLOBALREF_ENTRY(env, ref);
2995   Handle ref_handle(thread, JNIHandles::resolve(ref));
2996   jweak ret = JNIHandles::make_weak_global(ref_handle, AllocFailStrategy::RETURN_NULL);
2997   if (ret == nullptr && ref_handle.not_null()) {
2998     THROW_OOP_(Universe::out_of_memory_error_c_heap(), nullptr);
2999   }
3000   HOTSPOT_JNI_NEWWEAKGLOBALREF_RETURN(ret);
3001   return ret;
3002 JNI_END
3003 
3004 // Must be JNI_ENTRY (with HandleMark)
3005 JNI_ENTRY(void, jni_DeleteWeakGlobalRef(JNIEnv *env, jweak ref))
3006   HOTSPOT_JNI_DELETEWEAKGLOBALREF_ENTRY(env, ref);
3007   JNIHandles::destroy_weak_global(ref);
3008   HOTSPOT_JNI_DELETEWEAKGLOBALREF_RETURN();
3009 JNI_END
3010 
3011 
3012 JNI_ENTRY_NO_PRESERVE(jboolean, jni_ExceptionCheck(JNIEnv *env))
3013  HOTSPOT_JNI_EXCEPTIONCHECK_ENTRY(env);
3014   jni_check_async_exceptions(thread);
3015   jboolean ret = (thread->has_pending_exception()) ? JNI_TRUE : JNI_FALSE;
3016  HOTSPOT_JNI_EXCEPTIONCHECK_RETURN(ret);
3017   return ret;
3018 JNI_END
3019 
3020 
3021 // Initialization state for three routines below relating to
3022 // java.nio.DirectBuffers
3023 static          int directBufferSupportInitializeStarted = 0;
3024 static volatile int directBufferSupportInitializeEnded   = 0;
3025 static volatile int directBufferSupportInitializeFailed  = 0;
3026 static jclass    bufferClass                 = nullptr;
3027 static jclass    directBufferClass           = nullptr;
3028 static jclass    directByteBufferClass       = nullptr;
3029 static jmethodID directByteBufferConstructor = nullptr;
3030 static jfieldID  directBufferAddressField    = nullptr;
3031 static jfieldID  bufferCapacityField         = nullptr;
3032 
3033 static jclass lookupOne(JNIEnv* env, const char* name, TRAPS) {
3034   Handle loader;            // null (bootstrap) loader
3035   Handle protection_domain; // null protection domain
3036 
3037   TempNewSymbol sym = SymbolTable::new_symbol(name);
3038   jclass result =  find_class_from_class_loader(env, sym, true, loader, protection_domain, true, CHECK_NULL);
3039 
3040   if (log_is_enabled(Debug, class, resolve) && result != nullptr) {
3041     trace_class_resolution(java_lang_Class::as_Klass(JNIHandles::resolve_non_null(result)));
3042   }
3043   return result;
3044 }
3045 
3046 // These lookups are done with the null (bootstrap) ClassLoader to
3047 // circumvent any security checks that would be done by jni_FindClass.
3048 JNI_ENTRY(bool, lookupDirectBufferClasses(JNIEnv* env))
3049 {
3050   if ((bufferClass           = lookupOne(env, "java/nio/Buffer", thread))           == nullptr) { return false; }
3051   if ((directBufferClass     = lookupOne(env, "sun/nio/ch/DirectBuffer", thread))   == nullptr) { return false; }
3052   if ((directByteBufferClass = lookupOne(env, "java/nio/DirectByteBuffer", thread)) == nullptr) { return false; }
3053   return true;
3054 }
3055 JNI_END
3056 
3057 
3058 static bool initializeDirectBufferSupport(JNIEnv* env, JavaThread* thread) {
3059   if (directBufferSupportInitializeFailed) {
3060     return false;
3061   }
3062 
3063   if (Atomic::cmpxchg(&directBufferSupportInitializeStarted, 0, 1) == 0) {
3064     if (!lookupDirectBufferClasses(env)) {
3065       directBufferSupportInitializeFailed = 1;
3066       return false;
3067     }
3068 
3069     // Make global references for these
3070     bufferClass           = (jclass) env->NewGlobalRef(bufferClass);
3071     directBufferClass     = (jclass) env->NewGlobalRef(directBufferClass);
3072     directByteBufferClass = (jclass) env->NewGlobalRef(directByteBufferClass);
3073 
3074     // Global refs will be null if out-of-memory (no exception is pending)
3075     if (bufferClass == nullptr || directBufferClass == nullptr || directByteBufferClass == nullptr) {
3076       directBufferSupportInitializeFailed = 1;
3077       return false;
3078     }
3079 
3080     // Get needed field and method IDs
3081     directByteBufferConstructor = env->GetMethodID(directByteBufferClass, "<init>", "(JJ)V");
3082     if (env->ExceptionCheck()) {
3083       env->ExceptionClear();
3084       directBufferSupportInitializeFailed = 1;
3085       return false;
3086     }
3087     directBufferAddressField    = env->GetFieldID(bufferClass, "address", "J");
3088     if (env->ExceptionCheck()) {
3089       env->ExceptionClear();
3090       directBufferSupportInitializeFailed = 1;
3091       return false;
3092     }
3093     bufferCapacityField         = env->GetFieldID(bufferClass, "capacity", "I");
3094     if (env->ExceptionCheck()) {
3095       env->ExceptionClear();
3096       directBufferSupportInitializeFailed = 1;
3097       return false;
3098     }
3099 
3100     if ((directByteBufferConstructor == nullptr) ||
3101         (directBufferAddressField    == nullptr) ||
3102         (bufferCapacityField         == nullptr)) {
3103       directBufferSupportInitializeFailed = 1;
3104       return false;
3105     }
3106 
3107     directBufferSupportInitializeEnded = 1;
3108   } else {
3109     while (!directBufferSupportInitializeEnded && !directBufferSupportInitializeFailed) {
3110       os::naked_yield();
3111     }
3112   }
3113 
3114   return !directBufferSupportInitializeFailed;
3115 }
3116 
3117 extern "C" jobject JNICALL jni_NewDirectByteBuffer(JNIEnv *env, void* address, jlong capacity)
3118 {
3119   // thread_from_jni_environment() will block if VM is gone.
3120   JavaThread* thread = JavaThread::thread_from_jni_environment(env);
3121 
3122  HOTSPOT_JNI_NEWDIRECTBYTEBUFFER_ENTRY(env, address, capacity);
3123 
3124   if (!directBufferSupportInitializeEnded) {
3125     if (!initializeDirectBufferSupport(env, thread)) {
3126       HOTSPOT_JNI_NEWDIRECTBYTEBUFFER_RETURN(nullptr);
3127       return nullptr;
3128     }
3129   }
3130 
3131   // Being paranoid about accidental sign extension on address
3132   jlong addr = (jlong) ((uintptr_t) address);
3133   jobject ret = env->NewObject(directByteBufferClass, directByteBufferConstructor, addr, capacity);
3134   HOTSPOT_JNI_NEWDIRECTBYTEBUFFER_RETURN(ret);
3135   return ret;
3136 }
3137 
3138 DT_RETURN_MARK_DECL(GetDirectBufferAddress, void*
3139                     , HOTSPOT_JNI_GETDIRECTBUFFERADDRESS_RETURN((void*) _ret_ref));
3140 
3141 extern "C" void* JNICALL jni_GetDirectBufferAddress(JNIEnv *env, jobject buf)
3142 {
3143   // thread_from_jni_environment() will block if VM is gone.
3144   JavaThread* thread = JavaThread::thread_from_jni_environment(env);
3145 
3146   HOTSPOT_JNI_GETDIRECTBUFFERADDRESS_ENTRY(env, buf);
3147   void* ret = nullptr;
3148   DT_RETURN_MARK(GetDirectBufferAddress, void*, (const void*&)ret);
3149 
3150   if (!directBufferSupportInitializeEnded) {
3151     if (!initializeDirectBufferSupport(env, thread)) {
3152       return 0;
3153     }
3154   }
3155 
3156   if ((buf != nullptr) && (!env->IsInstanceOf(buf, directBufferClass))) {
3157     return 0;
3158   }
3159 
3160   ret = (void*)(intptr_t)env->GetLongField(buf, directBufferAddressField);
3161   return ret;
3162 }
3163 
3164 DT_RETURN_MARK_DECL(GetDirectBufferCapacity, jlong
3165                     , HOTSPOT_JNI_GETDIRECTBUFFERCAPACITY_RETURN(_ret_ref));
3166 
3167 extern "C" jlong JNICALL jni_GetDirectBufferCapacity(JNIEnv *env, jobject buf)
3168 {
3169   // thread_from_jni_environment() will block if VM is gone.
3170   JavaThread* thread = JavaThread::thread_from_jni_environment(env);
3171 
3172   HOTSPOT_JNI_GETDIRECTBUFFERCAPACITY_ENTRY(env, buf);
3173   jlong ret = -1;
3174   DT_RETURN_MARK(GetDirectBufferCapacity, jlong, (const jlong&)ret);
3175 
3176   if (!directBufferSupportInitializeEnded) {
3177     if (!initializeDirectBufferSupport(env, thread)) {
3178       ret = 0;
3179       return ret;
3180     }
3181   }
3182 
3183   if (buf == nullptr) {
3184     return -1;
3185   }
3186 
3187   if (!env->IsInstanceOf(buf, directBufferClass)) {
3188     return -1;
3189   }
3190 
3191   // NOTE that capacity is currently an int in the implementation
3192   ret = env->GetIntField(buf, bufferCapacityField);
3193   return ret;
3194 }
3195 
3196 
3197 JNI_LEAF(jint, jni_GetVersion(JNIEnv *env))
3198   HOTSPOT_JNI_GETVERSION_ENTRY(env);
3199   HOTSPOT_JNI_GETVERSION_RETURN(CurrentVersion);
3200   return CurrentVersion;
3201 JNI_END
3202 
3203 extern struct JavaVM_ main_vm;
3204 
3205 JNI_LEAF(jint, jni_GetJavaVM(JNIEnv *env, JavaVM **vm))
3206   HOTSPOT_JNI_GETJAVAVM_ENTRY(env, (void **) vm);
3207   *vm  = (JavaVM *)(&main_vm);
3208   HOTSPOT_JNI_GETJAVAVM_RETURN(JNI_OK);
3209   return JNI_OK;
3210 JNI_END
3211 
3212 
3213 JNI_ENTRY(jobject, jni_GetModule(JNIEnv* env, jclass clazz))
3214   return Modules::get_module(clazz, THREAD);
3215 JNI_END
3216 
3217 JNI_ENTRY(jboolean, jni_IsVirtualThread(JNIEnv* env, jobject obj))
3218   oop thread_obj = JNIHandles::resolve_external_guard(obj);
3219   if (thread_obj != nullptr && thread_obj->is_a(vmClasses::BaseVirtualThread_klass())) {
3220     return JNI_TRUE;
3221   } else {
3222     return JNI_FALSE;
3223   }
3224 JNI_END
3225 
3226 
3227 // Structure containing all jni functions
3228 struct JNINativeInterface_ jni_NativeInterface = {
3229     nullptr,
3230     nullptr,
3231     nullptr,
3232 
3233     nullptr,
3234 
3235     jni_GetVersion,
3236 
3237     jni_DefineClass,
3238     jni_FindClass,
3239 
3240     jni_FromReflectedMethod,
3241     jni_FromReflectedField,
3242 
3243     jni_ToReflectedMethod,
3244 
3245     jni_GetSuperclass,
3246     jni_IsAssignableFrom,
3247 
3248     jni_ToReflectedField,
3249 
3250     jni_Throw,
3251     jni_ThrowNew,
3252     jni_ExceptionOccurred,
3253     jni_ExceptionDescribe,
3254     jni_ExceptionClear,
3255     jni_FatalError,
3256 
3257     jni_PushLocalFrame,
3258     jni_PopLocalFrame,
3259 
3260     jni_NewGlobalRef,
3261     jni_DeleteGlobalRef,
3262     jni_DeleteLocalRef,
3263     jni_IsSameObject,
3264 
3265     jni_NewLocalRef,
3266     jni_EnsureLocalCapacity,
3267 
3268     jni_AllocObject,
3269     jni_NewObject,
3270     jni_NewObjectV,
3271     jni_NewObjectA,
3272 
3273     jni_GetObjectClass,
3274     jni_IsInstanceOf,
3275 
3276     jni_GetMethodID,
3277 
3278     jni_CallObjectMethod,
3279     jni_CallObjectMethodV,
3280     jni_CallObjectMethodA,
3281     jni_CallBooleanMethod,
3282     jni_CallBooleanMethodV,
3283     jni_CallBooleanMethodA,
3284     jni_CallByteMethod,
3285     jni_CallByteMethodV,
3286     jni_CallByteMethodA,
3287     jni_CallCharMethod,
3288     jni_CallCharMethodV,
3289     jni_CallCharMethodA,
3290     jni_CallShortMethod,
3291     jni_CallShortMethodV,
3292     jni_CallShortMethodA,
3293     jni_CallIntMethod,
3294     jni_CallIntMethodV,
3295     jni_CallIntMethodA,
3296     jni_CallLongMethod,
3297     jni_CallLongMethodV,
3298     jni_CallLongMethodA,
3299     jni_CallFloatMethod,
3300     jni_CallFloatMethodV,
3301     jni_CallFloatMethodA,
3302     jni_CallDoubleMethod,
3303     jni_CallDoubleMethodV,
3304     jni_CallDoubleMethodA,
3305     jni_CallVoidMethod,
3306     jni_CallVoidMethodV,
3307     jni_CallVoidMethodA,
3308 
3309     jni_CallNonvirtualObjectMethod,
3310     jni_CallNonvirtualObjectMethodV,
3311     jni_CallNonvirtualObjectMethodA,
3312     jni_CallNonvirtualBooleanMethod,
3313     jni_CallNonvirtualBooleanMethodV,
3314     jni_CallNonvirtualBooleanMethodA,
3315     jni_CallNonvirtualByteMethod,
3316     jni_CallNonvirtualByteMethodV,
3317     jni_CallNonvirtualByteMethodA,
3318     jni_CallNonvirtualCharMethod,
3319     jni_CallNonvirtualCharMethodV,
3320     jni_CallNonvirtualCharMethodA,
3321     jni_CallNonvirtualShortMethod,
3322     jni_CallNonvirtualShortMethodV,
3323     jni_CallNonvirtualShortMethodA,
3324     jni_CallNonvirtualIntMethod,
3325     jni_CallNonvirtualIntMethodV,
3326     jni_CallNonvirtualIntMethodA,
3327     jni_CallNonvirtualLongMethod,
3328     jni_CallNonvirtualLongMethodV,
3329     jni_CallNonvirtualLongMethodA,
3330     jni_CallNonvirtualFloatMethod,
3331     jni_CallNonvirtualFloatMethodV,
3332     jni_CallNonvirtualFloatMethodA,
3333     jni_CallNonvirtualDoubleMethod,
3334     jni_CallNonvirtualDoubleMethodV,
3335     jni_CallNonvirtualDoubleMethodA,
3336     jni_CallNonvirtualVoidMethod,
3337     jni_CallNonvirtualVoidMethodV,
3338     jni_CallNonvirtualVoidMethodA,
3339 
3340     jni_GetFieldID,
3341 
3342     jni_GetObjectField,
3343     jni_GetBooleanField,
3344     jni_GetByteField,
3345     jni_GetCharField,
3346     jni_GetShortField,
3347     jni_GetIntField,
3348     jni_GetLongField,
3349     jni_GetFloatField,
3350     jni_GetDoubleField,
3351 
3352     jni_SetObjectField,
3353     jni_SetBooleanField,
3354     jni_SetByteField,
3355     jni_SetCharField,
3356     jni_SetShortField,
3357     jni_SetIntField,
3358     jni_SetLongField,
3359     jni_SetFloatField,
3360     jni_SetDoubleField,
3361 
3362     jni_GetStaticMethodID,
3363 
3364     jni_CallStaticObjectMethod,
3365     jni_CallStaticObjectMethodV,
3366     jni_CallStaticObjectMethodA,
3367     jni_CallStaticBooleanMethod,
3368     jni_CallStaticBooleanMethodV,
3369     jni_CallStaticBooleanMethodA,
3370     jni_CallStaticByteMethod,
3371     jni_CallStaticByteMethodV,
3372     jni_CallStaticByteMethodA,
3373     jni_CallStaticCharMethod,
3374     jni_CallStaticCharMethodV,
3375     jni_CallStaticCharMethodA,
3376     jni_CallStaticShortMethod,
3377     jni_CallStaticShortMethodV,
3378     jni_CallStaticShortMethodA,
3379     jni_CallStaticIntMethod,
3380     jni_CallStaticIntMethodV,
3381     jni_CallStaticIntMethodA,
3382     jni_CallStaticLongMethod,
3383     jni_CallStaticLongMethodV,
3384     jni_CallStaticLongMethodA,
3385     jni_CallStaticFloatMethod,
3386     jni_CallStaticFloatMethodV,
3387     jni_CallStaticFloatMethodA,
3388     jni_CallStaticDoubleMethod,
3389     jni_CallStaticDoubleMethodV,
3390     jni_CallStaticDoubleMethodA,
3391     jni_CallStaticVoidMethod,
3392     jni_CallStaticVoidMethodV,
3393     jni_CallStaticVoidMethodA,
3394 
3395     jni_GetStaticFieldID,
3396 
3397     jni_GetStaticObjectField,
3398     jni_GetStaticBooleanField,
3399     jni_GetStaticByteField,
3400     jni_GetStaticCharField,
3401     jni_GetStaticShortField,
3402     jni_GetStaticIntField,
3403     jni_GetStaticLongField,
3404     jni_GetStaticFloatField,
3405     jni_GetStaticDoubleField,
3406 
3407     jni_SetStaticObjectField,
3408     jni_SetStaticBooleanField,
3409     jni_SetStaticByteField,
3410     jni_SetStaticCharField,
3411     jni_SetStaticShortField,
3412     jni_SetStaticIntField,
3413     jni_SetStaticLongField,
3414     jni_SetStaticFloatField,
3415     jni_SetStaticDoubleField,
3416 
3417     jni_NewString,
3418     jni_GetStringLength,
3419     jni_GetStringChars,
3420     jni_ReleaseStringChars,
3421 
3422     jni_NewStringUTF,
3423     jni_GetStringUTFLength,
3424     jni_GetStringUTFChars,
3425     jni_ReleaseStringUTFChars,
3426 
3427     jni_GetArrayLength,
3428 
3429     jni_NewObjectArray,
3430     jni_GetObjectArrayElement,
3431     jni_SetObjectArrayElement,
3432 
3433     jni_NewBooleanArray,
3434     jni_NewByteArray,
3435     jni_NewCharArray,
3436     jni_NewShortArray,
3437     jni_NewIntArray,
3438     jni_NewLongArray,
3439     jni_NewFloatArray,
3440     jni_NewDoubleArray,
3441 
3442     jni_GetBooleanArrayElements,
3443     jni_GetByteArrayElements,
3444     jni_GetCharArrayElements,
3445     jni_GetShortArrayElements,
3446     jni_GetIntArrayElements,
3447     jni_GetLongArrayElements,
3448     jni_GetFloatArrayElements,
3449     jni_GetDoubleArrayElements,
3450 
3451     jni_ReleaseBooleanArrayElements,
3452     jni_ReleaseByteArrayElements,
3453     jni_ReleaseCharArrayElements,
3454     jni_ReleaseShortArrayElements,
3455     jni_ReleaseIntArrayElements,
3456     jni_ReleaseLongArrayElements,
3457     jni_ReleaseFloatArrayElements,
3458     jni_ReleaseDoubleArrayElements,
3459 
3460     jni_GetBooleanArrayRegion,
3461     jni_GetByteArrayRegion,
3462     jni_GetCharArrayRegion,
3463     jni_GetShortArrayRegion,
3464     jni_GetIntArrayRegion,
3465     jni_GetLongArrayRegion,
3466     jni_GetFloatArrayRegion,
3467     jni_GetDoubleArrayRegion,
3468 
3469     jni_SetBooleanArrayRegion,
3470     jni_SetByteArrayRegion,
3471     jni_SetCharArrayRegion,
3472     jni_SetShortArrayRegion,
3473     jni_SetIntArrayRegion,
3474     jni_SetLongArrayRegion,
3475     jni_SetFloatArrayRegion,
3476     jni_SetDoubleArrayRegion,
3477 
3478     jni_RegisterNatives,
3479     jni_UnregisterNatives,
3480 
3481     jni_MonitorEnter,
3482     jni_MonitorExit,
3483 
3484     jni_GetJavaVM,
3485 
3486     jni_GetStringRegion,
3487     jni_GetStringUTFRegion,
3488 
3489     jni_GetPrimitiveArrayCritical,
3490     jni_ReleasePrimitiveArrayCritical,
3491 
3492     jni_GetStringCritical,
3493     jni_ReleaseStringCritical,
3494 
3495     jni_NewWeakGlobalRef,
3496     jni_DeleteWeakGlobalRef,
3497 
3498     jni_ExceptionCheck,
3499 
3500     jni_NewDirectByteBuffer,
3501     jni_GetDirectBufferAddress,
3502     jni_GetDirectBufferCapacity,
3503 
3504     // New 1_6 features
3505 
3506     jni_GetObjectRefType,
3507 
3508     // Module features
3509 
3510     jni_GetModule,
3511 
3512     // Virtual threads
3513 
3514     jni_IsVirtualThread
3515 };
3516 
3517 
3518 // For jvmti use to modify jni function table.
3519 // Java threads in native contiues to run until it is transitioned
3520 // to VM at safepoint. Before the transition or before it is blocked
3521 // for safepoint it may access jni function table. VM could crash if
3522 // any java thread access the jni function table in the middle of memcpy.
3523 // To avoid this each function pointers are copied automically.
3524 void copy_jni_function_table(const struct JNINativeInterface_ *new_jni_NativeInterface) {
3525   assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint");
3526   intptr_t *a = (intptr_t *) jni_functions();
3527   intptr_t *b = (intptr_t *) new_jni_NativeInterface;
3528   for (uint i=0; i <  sizeof(struct JNINativeInterface_)/sizeof(void *); i++) {
3529     Atomic::store(a++, *b++);
3530   }
3531 }
3532 
3533 void quicken_jni_functions() {
3534   // Replace Get<Primitive>Field with fast versions
3535   if (UseFastJNIAccessors && !VerifyJNIFields && !CheckJNICalls) {
3536     address func;
3537     func = JNI_FastGetField::generate_fast_get_boolean_field();
3538     if (func != (address)-1) {
3539       jni_NativeInterface.GetBooleanField = (GetBooleanField_t)func;
3540     }
3541     func = JNI_FastGetField::generate_fast_get_byte_field();
3542     if (func != (address)-1) {
3543       jni_NativeInterface.GetByteField = (GetByteField_t)func;
3544     }
3545     func = JNI_FastGetField::generate_fast_get_char_field();
3546     if (func != (address)-1) {
3547       jni_NativeInterface.GetCharField = (GetCharField_t)func;
3548     }
3549     func = JNI_FastGetField::generate_fast_get_short_field();
3550     if (func != (address)-1) {
3551       jni_NativeInterface.GetShortField = (GetShortField_t)func;
3552     }
3553     func = JNI_FastGetField::generate_fast_get_int_field();
3554     if (func != (address)-1) {
3555       jni_NativeInterface.GetIntField = (GetIntField_t)func;
3556     }
3557     func = JNI_FastGetField::generate_fast_get_long_field();
3558     if (func != (address)-1) {
3559       jni_NativeInterface.GetLongField = (GetLongField_t)func;
3560     }
3561     func = JNI_FastGetField::generate_fast_get_float_field();
3562     if (func != (address)-1) {
3563       jni_NativeInterface.GetFloatField = (GetFloatField_t)func;
3564     }
3565     func = JNI_FastGetField::generate_fast_get_double_field();
3566     if (func != (address)-1) {
3567       jni_NativeInterface.GetDoubleField = (GetDoubleField_t)func;
3568     }
3569   }
3570 }
3571 
3572 // Returns the function structure
3573 struct JNINativeInterface_* jni_functions() {
3574 #if INCLUDE_JNI_CHECK
3575   if (CheckJNICalls) return jni_functions_check();
3576 #endif // INCLUDE_JNI_CHECK
3577   return &jni_NativeInterface;
3578 }
3579 
3580 // Returns the function structure
3581 struct JNINativeInterface_* jni_functions_nocheck() {
3582   return &jni_NativeInterface;
3583 }
3584 
3585 
3586 // Invocation API
3587 
3588 
3589 // Forward declaration
3590 extern const struct JNIInvokeInterface_ jni_InvokeInterface;
3591 
3592 // Global invocation API vars
3593 enum VM_Creation_State {
3594   NOT_CREATED = 0,
3595   IN_PROGRESS,  // Most JNI operations are permitted during this phase to
3596                 // allow for initialization actions by libraries and agents.
3597   COMPLETE
3598 };
3599 
3600 volatile VM_Creation_State vm_created = NOT_CREATED;
3601 
3602 // Indicate whether it is safe to recreate VM. Recreation is only
3603 // possible after a failed initial creation attempt in some cases.
3604 volatile int safe_to_recreate_vm = 1;
3605 struct JavaVM_ main_vm = {&jni_InvokeInterface};
3606 
3607 
3608 #define JAVASTACKSIZE (400 * 1024)    /* Default size of a thread java stack */
3609 enum { VERIFY_NONE, VERIFY_REMOTE, VERIFY_ALL };
3610 
3611 DT_RETURN_MARK_DECL(GetDefaultJavaVMInitArgs, jint
3612                     , HOTSPOT_JNI_GETDEFAULTJAVAVMINITARGS_RETURN(_ret_ref));
3613 
3614 _JNI_IMPORT_OR_EXPORT_ jint JNICALL JNI_GetDefaultJavaVMInitArgs(void *args_) {
3615   HOTSPOT_JNI_GETDEFAULTJAVAVMINITARGS_ENTRY(args_);
3616   JDK1_1InitArgs *args = (JDK1_1InitArgs *)args_;
3617   jint ret = JNI_ERR;
3618   DT_RETURN_MARK(GetDefaultJavaVMInitArgs, jint, (const jint&)ret);
3619 
3620   if (Threads::is_supported_jni_version(args->version)) {
3621     ret = JNI_OK;
3622   }
3623   // 1.1 style no longer supported in hotspot.
3624   // According the JNI spec, we should update args->version on return.
3625   // We also use the structure to communicate with launcher about default
3626   // stack size.
3627   if (args->version == JNI_VERSION_1_1) {
3628     args->version = JNI_VERSION_1_2;
3629     // javaStackSize is int in arguments structure
3630     assert(jlong(ThreadStackSize) * K < INT_MAX, "integer overflow");
3631     args->javaStackSize = (jint)(ThreadStackSize * K);
3632   }
3633   return ret;
3634 }
3635 
3636 DT_RETURN_MARK_DECL(CreateJavaVM, jint
3637                     , HOTSPOT_JNI_CREATEJAVAVM_RETURN(_ret_ref));
3638 
3639 static jint JNI_CreateJavaVM_inner(JavaVM **vm, void **penv, void *args) {
3640   HOTSPOT_JNI_CREATEJAVAVM_ENTRY((void **) vm, penv, args);
3641 
3642   jint result = JNI_ERR;
3643   DT_RETURN_MARK(CreateJavaVM, jint, (const jint&)result);
3644 
3645   // We're about to use Atomic::xchg for synchronization.  Some Zero
3646   // platforms use the GCC builtin __sync_lock_test_and_set for this,
3647   // but __sync_lock_test_and_set is not guaranteed to do what we want
3648   // on all architectures.  So we check it works before relying on it.
3649 #if defined(ZERO) && defined(ASSERT)
3650   {
3651     jint a = 0xcafebabe;
3652     jint b = Atomic::xchg(&a, (jint) 0xdeadbeef);
3653     void *c = &a;
3654     void *d = Atomic::xchg(&c, &b);
3655     assert(a == (jint) 0xdeadbeef && b == (jint) 0xcafebabe, "Atomic::xchg() works");
3656     assert(c == &b && d == &a, "Atomic::xchg() works");
3657   }
3658 #endif // ZERO && ASSERT
3659 
3660   // At the moment it's only possible to have one Java VM,
3661   // since some of the runtime state is in global variables.
3662 
3663   // We cannot use our mutex locks here, since they only work on
3664   // Threads. We do an atomic compare and exchange to ensure only
3665   // one thread can call this method at a time
3666 
3667   // We use Atomic::xchg rather than Atomic::add/dec since on some platforms
3668   // the add/dec implementations are dependent on whether we are running
3669   // on a multiprocessor Atomic::xchg does not have this problem.
3670   if (Atomic::xchg(&vm_created, IN_PROGRESS) != NOT_CREATED) {
3671     return JNI_EEXIST;   // already created, or create attempt in progress
3672   }
3673 
3674   // If a previous creation attempt failed but can be retried safely,
3675   // then safe_to_recreate_vm will have been reset to 1 after being
3676   // cleared here. If a previous creation attempt succeeded and we then
3677   // destroyed that VM, we will be prevented from trying to recreate
3678   // the VM in the same process, as the value will still be 0.
3679   if (Atomic::xchg(&safe_to_recreate_vm, 0) == 0) {
3680     return JNI_ERR;
3681   }
3682 
3683   /**
3684    * Certain errors during initialization are recoverable and do not
3685    * prevent this method from being called again at a later time
3686    * (perhaps with different arguments).  However, at a certain
3687    * point during initialization if an error occurs we cannot allow
3688    * this function to be called again (or it will crash).  In those
3689    * situations, the 'canTryAgain' flag is set to false, which atomically
3690    * sets safe_to_recreate_vm to 1, such that any new call to
3691    * JNI_CreateJavaVM will immediately fail using the above logic.
3692    */
3693   bool can_try_again = true;
3694 
3695   result = Threads::create_vm((JavaVMInitArgs*) args, &can_try_again);
3696   if (result == JNI_OK) {
3697     JavaThread *thread = JavaThread::current();
3698     assert(!thread->has_pending_exception(), "should have returned not OK");
3699     // thread is thread_in_vm here
3700     *vm = (JavaVM *)(&main_vm);
3701     *(JNIEnv**)penv = thread->jni_environment();
3702     // mark creation complete for other JNI ops
3703     Atomic::release_store(&vm_created, COMPLETE);
3704 
3705 #if INCLUDE_JVMCI
3706     if (EnableJVMCI) {
3707       if (UseJVMCICompiler) {
3708         // JVMCI is initialized on a CompilerThread
3709         if (BootstrapJVMCI) {
3710           JavaThread* THREAD = thread; // For exception macros.
3711           JVMCICompiler* compiler = JVMCICompiler::instance(true, CATCH);
3712           compiler->bootstrap(THREAD);
3713           if (HAS_PENDING_EXCEPTION) {
3714             HandleMark hm(THREAD);
3715             vm_exit_during_initialization(Handle(THREAD, PENDING_EXCEPTION));
3716           }
3717         }
3718       }
3719     }
3720 #endif
3721 
3722     // Notify JVMTI
3723     if (JvmtiExport::should_post_thread_life()) {
3724        JvmtiExport::post_thread_start(thread);
3725     }
3726 
3727     JFR_ONLY(Jfr::on_thread_start(thread);)
3728 
3729     if (ReplayCompiles) ciReplay::replay(thread);
3730 
3731 #ifdef ASSERT
3732     // Some platforms (like Win*) need a wrapper around these test
3733     // functions in order to properly handle error conditions.
3734     if (ErrorHandlerTest != 0) {
3735       VMError::controlled_crash(ErrorHandlerTest);
3736     }
3737 #endif
3738 
3739     // Since this is not a JVM_ENTRY we have to set the thread state manually before leaving.
3740     ThreadStateTransition::transition_from_vm(thread, _thread_in_native);
3741     MACOS_AARCH64_ONLY(thread->enable_wx(WXExec));
3742   } else {
3743     // If create_vm exits because of a pending exception, exit with that
3744     // exception.  In the future when we figure out how to reclaim memory,
3745     // we may be able to exit with JNI_ERR and allow the calling application
3746     // to continue.
3747     if (Universe::is_fully_initialized()) {
3748       // otherwise no pending exception possible - VM will already have aborted
3749       Thread* current = Thread::current_or_null();
3750       if (current != nullptr) {
3751         JavaThread* THREAD = JavaThread::cast(current); // For exception macros.
3752         assert(HAS_PENDING_EXCEPTION, "must be - else no current thread exists");
3753         HandleMark hm(THREAD);
3754         vm_exit_during_initialization(Handle(THREAD, PENDING_EXCEPTION));
3755       }
3756     }
3757 
3758     if (can_try_again) {
3759       // reset safe_to_recreate_vm to 1 so that retrial would be possible
3760       safe_to_recreate_vm = 1;
3761     }
3762 
3763     // Creation failed. We must reset vm_created
3764     *vm = 0;
3765     *(JNIEnv**)penv = 0;
3766     // reset vm_created last to avoid race condition. Use OrderAccess to
3767     // control both compiler and architectural-based reordering.
3768     assert(vm_created == IN_PROGRESS, "must be");
3769     Atomic::release_store(&vm_created, NOT_CREATED);
3770   }
3771 
3772   // Flush stdout and stderr before exit.
3773   fflush(stdout);
3774   fflush(stderr);
3775 
3776   return result;
3777 
3778 }
3779 
3780 _JNI_IMPORT_OR_EXPORT_ jint JNICALL JNI_CreateJavaVM(JavaVM **vm, void **penv, void *args) {
3781   jint result = JNI_ERR;
3782   // On Windows, let CreateJavaVM run with SEH protection
3783 #if defined(_WIN32) && !defined(USE_VECTORED_EXCEPTION_HANDLING)
3784   __try {
3785 #endif
3786     result = JNI_CreateJavaVM_inner(vm, penv, args);
3787 #if defined(_WIN32) && !defined(USE_VECTORED_EXCEPTION_HANDLING)
3788   } __except(topLevelExceptionFilter((_EXCEPTION_POINTERS*)_exception_info())) {
3789     // Nothing to do.
3790   }
3791 #endif
3792   return result;
3793 }
3794 
3795 _JNI_IMPORT_OR_EXPORT_ jint JNICALL JNI_GetCreatedJavaVMs(JavaVM **vm_buf, jsize bufLen, jsize *numVMs) {
3796   HOTSPOT_JNI_GETCREATEDJAVAVMS_ENTRY((void **) vm_buf, bufLen, (uintptr_t *) numVMs);
3797 
3798   if (vm_created == COMPLETE) {
3799     if (numVMs != nullptr) *numVMs = 1;
3800     if (bufLen > 0)     *vm_buf = (JavaVM *)(&main_vm);
3801   } else {
3802     if (numVMs != nullptr) *numVMs = 0;
3803   }
3804   HOTSPOT_JNI_GETCREATEDJAVAVMS_RETURN(JNI_OK);
3805   return JNI_OK;
3806 }
3807 
3808 extern "C" {
3809 
3810 DT_RETURN_MARK_DECL(DestroyJavaVM, jint
3811                     , HOTSPOT_JNI_DESTROYJAVAVM_RETURN(_ret_ref));
3812 
3813 static jint JNICALL jni_DestroyJavaVM_inner(JavaVM *vm) {
3814   HOTSPOT_JNI_DESTROYJAVAVM_ENTRY(vm);
3815   jint res = JNI_ERR;
3816   DT_RETURN_MARK(DestroyJavaVM, jint, (const jint&)res);
3817 
3818   if (vm_created == NOT_CREATED) {
3819     res = JNI_ERR;
3820     return res;
3821   }
3822 
3823   JNIEnv *env;
3824   JavaVMAttachArgs destroyargs;
3825   destroyargs.version = CurrentVersion;
3826   destroyargs.name = (char *)"DestroyJavaVM";
3827   destroyargs.group = nullptr;
3828   res = vm->AttachCurrentThread((void **)&env, (void *)&destroyargs);
3829   if (res != JNI_OK) {
3830     return res;
3831   }
3832 
3833   JavaThread* thread = JavaThread::current();
3834 
3835   // Make sure we are actually in a newly attached thread, with no
3836   // existing Java frame.
3837   if (thread->has_last_Java_frame()) {
3838     return JNI_ERR;
3839   }
3840 
3841   // Since this is not a JVM_ENTRY we have to set the thread state manually before entering.
3842 
3843   // We are going to VM, change W^X state to the expected one.
3844   MACOS_AARCH64_ONLY(WXMode oldmode = thread->enable_wx(WXWrite));
3845 
3846   ThreadStateTransition::transition_from_native(thread, _thread_in_vm);
3847   Threads::destroy_vm();
3848   // Don't bother restoring thread state, VM is gone.
3849   vm_created = NOT_CREATED;
3850   return JNI_OK;
3851 }
3852 
3853 jint JNICALL jni_DestroyJavaVM(JavaVM *vm) {
3854   jint result = JNI_ERR;
3855   // On Windows, we need SEH protection
3856 #if defined(_WIN32) && !defined(USE_VECTORED_EXCEPTION_HANDLING)
3857   __try {
3858 #endif
3859     result = jni_DestroyJavaVM_inner(vm);
3860 #if defined(_WIN32) && !defined(USE_VECTORED_EXCEPTION_HANDLING)
3861   } __except(topLevelExceptionFilter((_EXCEPTION_POINTERS*)_exception_info())) {
3862     // Nothing to do.
3863   }
3864 #endif
3865   return result;
3866 }
3867 
3868 static jint attach_current_thread(JavaVM *vm, void **penv, void *_args, bool daemon) {
3869   JavaVMAttachArgs *args = (JavaVMAttachArgs *) _args;
3870 
3871   // Check below commented out from JDK1.2fcs as well
3872   /*
3873   if (args && (args->version != JNI_VERSION_1_1 || args->version != JNI_VERSION_1_2)) {
3874     return JNI_EVERSION;
3875   }
3876   */
3877 
3878   Thread* t = Thread::current_or_null();
3879   if (t != nullptr) {
3880     // If executing from an atexit hook we may be in the VMThread.
3881     if (t->is_Java_thread()) {
3882       // If the thread has been attached this operation is a no-op
3883       *(JNIEnv**)penv = JavaThread::cast(t)->jni_environment();
3884       return JNI_OK;
3885     } else {
3886       return JNI_ERR;
3887     }
3888   }
3889 
3890   // Create a thread and mark it as attaching so it will be skipped by the
3891   // ThreadsListEnumerator - see CR 6404306
3892   JavaThread* thread = new JavaThread(true);
3893 
3894   // Set correct safepoint info. The thread is going to call into Java when
3895   // initializing the Java level thread object. Hence, the correct state must
3896   // be set in order for the Safepoint code to deal with it correctly.
3897   thread->set_thread_state(_thread_in_vm);
3898   thread->record_stack_base_and_size();
3899   thread->register_thread_stack_with_NMT();
3900   thread->initialize_thread_current();
3901   MACOS_AARCH64_ONLY(thread->init_wx());
3902 
3903   if (!os::create_attached_thread(thread)) {
3904     thread->smr_delete();
3905     return JNI_ERR;
3906   }
3907   // Enable stack overflow checks
3908   thread->stack_overflow_state()->create_stack_guard_pages();
3909 
3910   thread->initialize_tlab();
3911 
3912   thread->cache_global_variables();
3913 
3914   // This thread will not do a safepoint check, since it has
3915   // not been added to the Thread list yet.
3916   { MutexLocker ml(Threads_lock);
3917     // This must be inside this lock in order to get FullGCALot to work properly, i.e., to
3918     // avoid this thread trying to do a GC before it is added to the thread-list
3919     thread->set_active_handles(JNIHandleBlock::allocate_block());
3920     Threads::add(thread, daemon);
3921   }
3922   // Create thread group and name info from attach arguments
3923   oop group = nullptr;
3924   char* thread_name = nullptr;
3925   if (args != nullptr && Threads::is_supported_jni_version(args->version)) {
3926     group = JNIHandles::resolve(args->group);
3927     thread_name = args->name; // may be null
3928   }
3929   if (group == nullptr) group = Universe::main_thread_group();
3930 
3931   // Create Java level thread object and attach it to this thread
3932   bool attach_failed = false;
3933   {
3934     EXCEPTION_MARK;
3935     HandleMark hm(THREAD);
3936     Handle thread_group(THREAD, group);
3937     thread->allocate_threadObj(thread_group, thread_name, daemon, THREAD);
3938     if (HAS_PENDING_EXCEPTION) {
3939       CLEAR_PENDING_EXCEPTION;
3940       // cleanup outside the handle mark.
3941       attach_failed = true;
3942     }
3943   }
3944 
3945   if (attach_failed) {
3946     // Added missing cleanup
3947     thread->cleanup_failed_attach_current_thread(daemon);
3948     return JNI_ERR;
3949   }
3950 
3951   // mark the thread as no longer attaching
3952   // this uses a fence to push the change through so we don't have
3953   // to regrab the threads_lock
3954   thread->set_done_attaching_via_jni();
3955 
3956   // Set java thread status.
3957   java_lang_Thread::set_thread_status(thread->threadObj(),
3958               JavaThreadStatus::RUNNABLE);
3959 
3960   // Notify the debugger
3961   if (JvmtiExport::should_post_thread_life()) {
3962     JvmtiExport::post_thread_start(thread);
3963   }
3964 
3965   JFR_ONLY(Jfr::on_thread_start(thread);)
3966 
3967   *(JNIEnv**)penv = thread->jni_environment();
3968 
3969   // Now leaving the VM, so change thread_state. This is normally automatically taken care
3970   // of in the JVM_ENTRY. But in this situation we have to do it manually.
3971   ThreadStateTransition::transition_from_vm(thread, _thread_in_native);
3972   MACOS_AARCH64_ONLY(thread->enable_wx(WXExec));
3973 
3974   // Perform any platform dependent FPU setup
3975   os::setup_fpu();
3976 
3977   return JNI_OK;
3978 }
3979 
3980 
3981 jint JNICALL jni_AttachCurrentThread(JavaVM *vm, void **penv, void *_args) {
3982   HOTSPOT_JNI_ATTACHCURRENTTHREAD_ENTRY(vm, penv, _args);
3983   if (vm_created == NOT_CREATED) {
3984     // Not sure how we could possibly get here.
3985     HOTSPOT_JNI_ATTACHCURRENTTHREAD_RETURN((uint32_t) JNI_ERR);
3986     return JNI_ERR;
3987   }
3988 
3989   jint ret = attach_current_thread(vm, penv, _args, false);
3990   HOTSPOT_JNI_ATTACHCURRENTTHREAD_RETURN(ret);
3991   return ret;
3992 }
3993 
3994 
3995 jint JNICALL jni_DetachCurrentThread(JavaVM *vm)  {
3996   HOTSPOT_JNI_DETACHCURRENTTHREAD_ENTRY(vm);
3997   if (vm_created == NOT_CREATED) {
3998     // Not sure how we could possibly get here.
3999     HOTSPOT_JNI_DETACHCURRENTTHREAD_RETURN(JNI_ERR);
4000     return JNI_ERR;
4001   }
4002 
4003   Thread* current = Thread::current_or_null();
4004 
4005   // If the thread has already been detached the operation is a no-op
4006   if (current == nullptr) {
4007     HOTSPOT_JNI_DETACHCURRENTTHREAD_RETURN(JNI_OK);
4008     return JNI_OK;
4009   }
4010 
4011   // If executing from an atexit hook we may be in the VMThread.
4012   if (!current->is_Java_thread()) {
4013     HOTSPOT_JNI_DETACHCURRENTTHREAD_RETURN((uint32_t) JNI_ERR);
4014     return JNI_ERR;
4015   }
4016 
4017   VM_Exit::block_if_vm_exited();
4018 
4019   JavaThread* thread = JavaThread::cast(current);
4020   if (thread->has_last_Java_frame()) {
4021     HOTSPOT_JNI_DETACHCURRENTTHREAD_RETURN((uint32_t) JNI_ERR);
4022     // Can't detach a thread that's running java, that can't work.
4023     return JNI_ERR;
4024   }
4025 
4026   // We are going to VM, change W^X state to the expected one.
4027   MACOS_AARCH64_ONLY(thread->enable_wx(WXWrite));
4028 
4029   // Safepoint support. Have to do call-back to safepoint code, if in the
4030   // middle of a safepoint operation
4031   ThreadStateTransition::transition_from_native(thread, _thread_in_vm);
4032 
4033   // XXX: Note that JavaThread::exit() call below removes the guards on the
4034   // stack pages set up via enable_stack_{red,yellow}_zone() calls
4035   // above in jni_AttachCurrentThread. Unfortunately, while the setting
4036   // of the guards is visible in jni_AttachCurrentThread above,
4037   // the removal of the guards is buried below in JavaThread::exit()
4038   // here. The abstraction should be more symmetrically either exposed
4039   // or hidden (e.g. it could probably be hidden in the same
4040   // (platform-dependent) methods where we do alternate stack
4041   // maintenance work?)
4042   thread->exit(false, JavaThread::jni_detach);
4043   thread->smr_delete();
4044 
4045   // Go to the execute mode, the initial state of the thread on creation.
4046   // Use os interface as the thread is not a JavaThread anymore.
4047   MACOS_AARCH64_ONLY(os::current_thread_enable_wx(WXExec));
4048 
4049   HOTSPOT_JNI_DETACHCURRENTTHREAD_RETURN(JNI_OK);
4050   return JNI_OK;
4051 }
4052 
4053 DT_RETURN_MARK_DECL(GetEnv, jint
4054                     , HOTSPOT_JNI_GETENV_RETURN(_ret_ref));
4055 
4056 jint JNICALL jni_GetEnv(JavaVM *vm, void **penv, jint version) {
4057   HOTSPOT_JNI_GETENV_ENTRY(vm, penv, version);
4058   jint ret = JNI_ERR;
4059   DT_RETURN_MARK(GetEnv, jint, (const jint&)ret);
4060 
4061   if (vm_created == NOT_CREATED) {
4062     *penv = nullptr;
4063     ret = JNI_EDETACHED;
4064     return ret;
4065   }
4066 
4067   if (JniExportedInterface::GetExportedInterface(vm, penv, version, &ret)) {
4068     return ret;
4069   }
4070 
4071 #ifndef JVMPI_VERSION_1
4072 // need these in order to be polite about older agents
4073 #define JVMPI_VERSION_1   ((jint)0x10000001)
4074 #define JVMPI_VERSION_1_1 ((jint)0x10000002)
4075 #define JVMPI_VERSION_1_2 ((jint)0x10000003)
4076 #endif // !JVMPI_VERSION_1
4077 
4078   Thread* thread = Thread::current_or_null();
4079   if (thread != nullptr && thread->is_Java_thread()) {
4080     if (Threads::is_supported_jni_version_including_1_1(version)) {
4081       *(JNIEnv**)penv = JavaThread::cast(thread)->jni_environment();
4082       ret = JNI_OK;
4083       return ret;
4084 
4085     } else if (version == JVMPI_VERSION_1 ||
4086                version == JVMPI_VERSION_1_1 ||
4087                version == JVMPI_VERSION_1_2) {
4088       tty->print_cr("ERROR: JVMPI, an experimental interface, is no longer supported.");
4089       tty->print_cr("Please use the supported interface: the JVM Tool Interface (JVM TI).");
4090       ret = JNI_EVERSION;
4091       return ret;
4092     } else if (JvmtiExport::is_jvmdi_version(version)) {
4093       tty->print_cr("FATAL ERROR: JVMDI is no longer supported.");
4094       tty->print_cr("Please use the supported interface: the JVM Tool Interface (JVM TI).");
4095       ret = JNI_EVERSION;
4096       return ret;
4097     } else {
4098       *penv = nullptr;
4099       ret = JNI_EVERSION;
4100       return ret;
4101     }
4102   } else {
4103     *penv = nullptr;
4104     ret = JNI_EDETACHED;
4105     return ret;
4106   }
4107 }
4108 
4109 
4110 jint JNICALL jni_AttachCurrentThreadAsDaemon(JavaVM *vm, void **penv, void *_args) {
4111   HOTSPOT_JNI_ATTACHCURRENTTHREADASDAEMON_ENTRY(vm, penv, _args);
4112   if (vm_created == NOT_CREATED) {
4113     // Not sure how we could possibly get here.
4114     HOTSPOT_JNI_ATTACHCURRENTTHREADASDAEMON_RETURN((uint32_t) JNI_ERR);
4115     return JNI_ERR;
4116   }
4117 
4118   jint ret = attach_current_thread(vm, penv, _args, true);
4119   HOTSPOT_JNI_ATTACHCURRENTTHREADASDAEMON_RETURN(ret);
4120   return ret;
4121 }
4122 
4123 
4124 } // End extern "C"
4125 
4126 const struct JNIInvokeInterface_ jni_InvokeInterface = {
4127     nullptr,
4128     nullptr,
4129     nullptr,
4130 
4131     jni_DestroyJavaVM,
4132     jni_AttachCurrentThread,
4133     jni_DetachCurrentThread,
4134     jni_GetEnv,
4135     jni_AttachCurrentThreadAsDaemon
4136 };