1 /*
   2  * Copyright (c) 1997, 2022, 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 "jni.h"
  29 #include "jni.h"
  30 #include "jvm.h"
  31 #include "ci/ciReplay.hpp"
  32 #include "classfile/altHashing.hpp"
  33 #include "classfile/classFileStream.hpp"
  34 #include "classfile/classLoader.hpp"
  35 #include "classfile/classLoadInfo.hpp"
  36 #include "classfile/javaClasses.hpp"
  37 #include "classfile/javaClasses.inline.hpp"
  38 #include "classfile/javaThreadStatus.hpp"
  39 #include "classfile/moduleEntry.hpp"
  40 #include "classfile/modules.hpp"
  41 #include "classfile/symbolTable.hpp"
  42 #include "classfile/systemDictionary.hpp"
  43 #include "classfile/vmClasses.hpp"
  44 #include "classfile/vmSymbols.hpp"
  45 #include "compiler/compiler_globals.hpp"
  46 #include "gc/shared/collectedHeap.hpp"
  47 #include "gc/shared/gcLocker.inline.hpp"
  48 #include "gc/shared/stringdedup/stringDedup.hpp"
  49 #include "interpreter/linkResolver.hpp"
  50 #include "jfr/jfrEvents.hpp"
  51 #include "jfr/support/jfrThreadId.hpp"
  52 #include "logging/log.hpp"
  53 #include "memory/allocation.hpp"
  54 #include "memory/allocation.inline.hpp"
  55 #include "memory/oopFactory.hpp"
  56 #include "memory/resourceArea.hpp"
  57 #include "memory/universe.hpp"
  58 #include "oops/access.inline.hpp"
  59 #include "oops/arrayOop.hpp"
  60 #include "oops/flatArrayOop.inline.hpp"
  61 #include "oops/inlineKlass.inline.hpp"
  62 #include "oops/instanceKlass.inline.hpp"
  63 #include "oops/instanceOop.hpp"
  64 #include "oops/klass.inline.hpp"
  65 #include "oops/markWord.hpp"
  66 #include "oops/method.hpp"
  67 #include "oops/objArrayKlass.hpp"
  68 #include "oops/objArrayOop.inline.hpp"
  69 #include "oops/oop.inline.hpp"
  70 #include "oops/symbol.hpp"
  71 #include "oops/typeArrayKlass.hpp"
  72 #include "oops/typeArrayOop.inline.hpp"
  73 #include "prims/jniCheck.hpp"
  74 #include "prims/jniExport.hpp"
  75 #include "prims/jniFastGetField.hpp"
  76 #include "prims/jvm_misc.hpp"
  77 #include "prims/jvmtiExport.hpp"
  78 #include "prims/jvmtiThreadState.hpp"
  79 #include "runtime/arguments.hpp"
  80 #include "runtime/atomic.hpp"
  81 #include "runtime/fieldDescriptor.inline.hpp"
  82 #include "runtime/handles.inline.hpp"
  83 #include "runtime/interfaceSupport.inline.hpp"
  84 #include "runtime/java.hpp"
  85 #include "runtime/javaCalls.hpp"
  86 #include "runtime/javaThread.inline.hpp"
  87 #include "runtime/jfieldIDWorkaround.hpp"
  88 #include "runtime/jniHandles.inline.hpp"
  89 #include "runtime/reflection.hpp"
  90 #include "runtime/safepointVerifiers.hpp"
  91 #include "runtime/sharedRuntime.hpp"
  92 #include "runtime/signature.hpp"
  93 #include "runtime/synchronizer.hpp"
  94 #include "runtime/thread.inline.hpp"
  95 #include "runtime/vmOperations.hpp"
  96 #include "services/memTracker.hpp"
  97 #include "services/runtimeService.hpp"
  98 #include "utilities/defaultStream.hpp"
  99 #include "utilities/dtrace.hpp"
 100 #include "utilities/events.hpp"
 101 #include "utilities/macros.hpp"
 102 #include "utilities/vmError.hpp"
 103 #if INCLUDE_JVMCI
 104 #include "jvmci/jvmciCompiler.hpp"
 105 #endif
 106 
 107 static jint CurrentVersion = JNI_VERSION_20;
 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     intptr_t 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 != NULL && result->is_static_field_id();
 204 #else
 205     return result != NULL;
 206 #endif
 207   }
 208 }
 209 
 210 
 211 intptr_t jfieldIDWorkaround::encode_klass_hash(Klass* k, intptr_t 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 != NULL);
 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   intptr_t 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 = NULL;
 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 == NULL ? NULL :
 289     SystemDictionary::class_name_symbol(name, vmSymbols::java_lang_NoClassDefFoundError(),
 290                                         CHECK_NULL);
 291 
 292   ResourceMark rm(THREAD);
 293   ClassFileStream st((u1*)buf, bufLen, NULL, 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 = NULL;
 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 != NULL) {
 333     // Special handling to make sure JNI_OnLoad and JNI_OnUnload are executed
 334     // in the correct class context.
 335     if (k->class_loader() == NULL &&
 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 != NULL) {
 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 != NULL) {
 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 = NULL;
 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     = NULL;
 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==NULL? NULL : 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 = NULL;
 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     intptr_t offset = InstanceKlass::cast(k1)->field_offset( slot );
 419     JNIid* id = InstanceKlass::cast(k1)->jni_id_for(offset);
 420     assert(id != NULL, "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   intptr_t offset = InstanceKlass::cast(k1)->field_offset( slot );
 431   bool is_inlined = InstanceKlass::cast(k1)->field_is_inlined(slot);
 432   assert(InstanceKlass::cast(k1)->contains_field_offset(offset), "stay within object");
 433   ret = jfieldIDWorkaround::to_instance_jfieldID(k1, offset, is_inlined);
 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 = NULL;
 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 = NULL;
 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 NULL;
 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 NULL;
 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 == NULL) ? NULL : (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 != NULL && super_klass != NULL, "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 != NULL && thread->threadObj() != NULL) {
 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 != NULL && 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_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 != NULL) {
 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(NULL);              // 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 != NULL) {
 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 = NULL;
 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 == NULL) {
 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 = NULL;
 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 = NULL;
 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 == NULL) {
 998     ResourceMark rm(THREAD);
 999     THROW_(vmSymbols::java_lang_InstantiationException(), NULL);
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, NULL, 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 = NULL;
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 == NULL) {
1030     ResourceMark rm(THREAD);
1031     THROW_(vmSymbols::java_lang_InstantiationException(), NULL);
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, NULL, 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 = NULL;
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 == NULL) {
1062     ResourceMark rm(THREAD);
1063     THROW_(vmSymbols::java_lang_InstantiationException(), NULL);
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, NULL, 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 != NULL) {
1104     ret = JNI_FALSE;
1105     Klass* k = java_lang_Class::as_Klass(
1106       JNIHandles::resolve_non_null(clazz));
1107     if (k != NULL) {
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 == NULL)
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 == NULL || signature == NULL) {
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 = NULL;
1156     }
1157   } else {
1158     m = klass->lookup_method(name, signature);
1159     if (m == NULL &&  klass->is_instance_klass()) {
1160       m = InstanceKlass::cast(klass)->lookup_method_in_ordered_interfaces(name, signature);
1161     }
1162   }
1163   if (m == NULL || (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, NULL, 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, NULL, 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, NULL, 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, NULL, 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, NULL, 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, NULL, 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 == NULL || signame == NULL) {
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_inlined());
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 = NULL;
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_inlined_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 inlined 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_inlined_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_inlined_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 inlined 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_inlined_field(o, offset, v, CHECK);
1964   }
1965   HOTSPOT_JNI_SETOBJECTFIELD_RETURN();
1966 JNI_END
1967 
1968 
1969 #define DEFINE_SETFIELD(Argument,Fieldname,Result,SigType,unionType \
1970                         , EntryProbe, ReturnProbe) \
1971 \
1972 JNI_ENTRY_NO_PRESERVE(void, jni_Set##Result##Field(JNIEnv *env, jobject obj, jfieldID fieldID, Argument value)) \
1973 \
1974   EntryProbe; \
1975 \
1976   oop o = JNIHandles::resolve_non_null(obj); \
1977   Klass* k = o->klass(); \
1978   int offset = jfieldIDWorkaround::from_instance_jfieldID(k, fieldID);  \
1979   /* Keep JVMTI addition small and only check enabled flag here.       */ \
1980   if (JvmtiExport::should_post_field_modification()) { \
1981     jvalue field_value; \
1982     field_value.unionType = value; \
1983     o = JvmtiExport::jni_SetField_probe(thread, obj, o, k, fieldID, false, SigType, (jvalue *)&field_value); \
1984   } \
1985   if (SigType == JVM_SIGNATURE_BOOLEAN) { value = ((jboolean)value) & 1; } \
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 = NULL;
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 = NULL;
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 == NULL || signame == NULL) {
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, NULL, NULL, 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, NULL, NULL, 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, NULL, NULL, 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, NULL, NULL, id->holder(), fieldID, true, SigType, (jvalue *)&field_value); \
2177   } \
2178   if (SigType == JVM_SIGNATURE_BOOLEAN) { value = ((jboolean)value) & 1; } \
2179   id->holder()->java_mirror()-> Fieldname##_field_put (id->offset(), value); \
2180   ReturnProbe;\
2181 JNI_END
2182 
2183 DEFINE_SETSTATICFIELD(jboolean, bool,   Boolean, JVM_SIGNATURE_BOOLEAN, z
2184                       , HOTSPOT_JNI_SETSTATICBOOLEANFIELD_ENTRY(env, clazz, (uintptr_t)fieldID, value),
2185                       HOTSPOT_JNI_SETSTATICBOOLEANFIELD_RETURN())
2186 DEFINE_SETSTATICFIELD(jbyte,    byte,   Byte,    JVM_SIGNATURE_BYTE, b
2187                       , HOTSPOT_JNI_SETSTATICBYTEFIELD_ENTRY(env, clazz, (uintptr_t) fieldID, value),
2188                       HOTSPOT_JNI_SETSTATICBYTEFIELD_RETURN())
2189 DEFINE_SETSTATICFIELD(jchar,    char,   Char,    JVM_SIGNATURE_CHAR, c
2190                       , HOTSPOT_JNI_SETSTATICCHARFIELD_ENTRY(env, clazz, (uintptr_t) fieldID, value),
2191                       HOTSPOT_JNI_SETSTATICCHARFIELD_RETURN())
2192 DEFINE_SETSTATICFIELD(jshort,   short,  Short,   JVM_SIGNATURE_SHORT, s
2193                       , HOTSPOT_JNI_SETSTATICSHORTFIELD_ENTRY(env, clazz, (uintptr_t) fieldID, value),
2194                       HOTSPOT_JNI_SETSTATICSHORTFIELD_RETURN())
2195 DEFINE_SETSTATICFIELD(jint,     int,    Int,     JVM_SIGNATURE_INT, i
2196                       , HOTSPOT_JNI_SETSTATICINTFIELD_ENTRY(env, clazz, (uintptr_t) fieldID, value),
2197                       HOTSPOT_JNI_SETSTATICINTFIELD_RETURN())
2198 DEFINE_SETSTATICFIELD(jlong,    long,   Long,    JVM_SIGNATURE_LONG, j
2199                       , HOTSPOT_JNI_SETSTATICLONGFIELD_ENTRY(env, clazz, (uintptr_t) fieldID, value),
2200                       HOTSPOT_JNI_SETSTATICLONGFIELD_RETURN())
2201 // Float and double probes don't return value because dtrace doesn't currently support it
2202 DEFINE_SETSTATICFIELD(jfloat,   float,  Float,   JVM_SIGNATURE_FLOAT, f
2203                       , HOTSPOT_JNI_SETSTATICFLOATFIELD_ENTRY(env, clazz, (uintptr_t) fieldID),
2204                       HOTSPOT_JNI_SETSTATICFLOATFIELD_RETURN())
2205 DEFINE_SETSTATICFIELD(jdouble,  double, Double,  JVM_SIGNATURE_DOUBLE, d
2206                       , HOTSPOT_JNI_SETSTATICDOUBLEFIELD_ENTRY(env, clazz, (uintptr_t) fieldID),
2207                       HOTSPOT_JNI_SETSTATICDOUBLEFIELD_RETURN())
2208 
2209 //
2210 // String Operations
2211 //
2212 
2213 // Unicode Interface
2214 
2215 DT_RETURN_MARK_DECL(NewString, jstring
2216                     , HOTSPOT_JNI_NEWSTRING_RETURN(_ret_ref));
2217 
2218 JNI_ENTRY(jstring, jni_NewString(JNIEnv *env, const jchar *unicodeChars, jsize len))
2219  HOTSPOT_JNI_NEWSTRING_ENTRY(env, (uint16_t *) unicodeChars, len);
2220   jstring ret = NULL;
2221   DT_RETURN_MARK(NewString, jstring, (const jstring&)ret);
2222   oop string=java_lang_String::create_oop_from_unicode((jchar*) unicodeChars, len, CHECK_NULL);
2223   ret = (jstring) JNIHandles::make_local(THREAD, string);
2224   return ret;
2225 JNI_END
2226 
2227 
2228 JNI_ENTRY_NO_PRESERVE(jsize, jni_GetStringLength(JNIEnv *env, jstring string))
2229   HOTSPOT_JNI_GETSTRINGLENGTH_ENTRY(env, string);
2230   jsize ret = 0;
2231   oop s = JNIHandles::resolve_non_null(string);
2232   ret = java_lang_String::length(s);
2233  HOTSPOT_JNI_GETSTRINGLENGTH_RETURN(ret);
2234   return ret;
2235 JNI_END
2236 
2237 
2238 JNI_ENTRY_NO_PRESERVE(const jchar*, jni_GetStringChars(
2239   JNIEnv *env, jstring string, jboolean *isCopy))
2240  HOTSPOT_JNI_GETSTRINGCHARS_ENTRY(env, string, (uintptr_t *) isCopy);
2241   jchar* buf = NULL;
2242   oop s = JNIHandles::resolve_non_null(string);
2243   typeArrayOop s_value = java_lang_String::value(s);
2244   if (s_value != NULL) {
2245     int s_len = java_lang_String::length(s, s_value);
2246     bool is_latin1 = java_lang_String::is_latin1(s);
2247     buf = NEW_C_HEAP_ARRAY_RETURN_NULL(jchar, s_len + 1, mtInternal);  // add one for zero termination
2248     /* JNI Specification states return NULL on OOM */
2249     if (buf != NULL) {
2250       if (s_len > 0) {
2251         if (!is_latin1) {
2252           ArrayAccess<>::arraycopy_to_native(s_value, (size_t) typeArrayOopDesc::element_offset<jchar>(0),
2253                                              buf, s_len);
2254         } else {
2255           for (int i = 0; i < s_len; i++) {
2256             buf[i] = ((jchar) s_value->byte_at(i)) & 0xff;
2257           }
2258         }
2259       }
2260       buf[s_len] = 0;
2261       //%note jni_5
2262       if (isCopy != NULL) {
2263         *isCopy = JNI_TRUE;
2264       }
2265     }
2266   }
2267   HOTSPOT_JNI_GETSTRINGCHARS_RETURN(buf);
2268   return buf;
2269 JNI_END
2270 
2271 
2272 JNI_ENTRY_NO_PRESERVE(void, jni_ReleaseStringChars(JNIEnv *env, jstring str, const jchar *chars))
2273   HOTSPOT_JNI_RELEASESTRINGCHARS_ENTRY(env, str, (uint16_t *) chars);
2274   //%note jni_6
2275   if (chars != NULL) {
2276     // Since String objects are supposed to be immutable, don't copy any
2277     // new data back.  A bad user will have to go after the char array.
2278     FreeHeap((void*) chars);
2279   }
2280   HOTSPOT_JNI_RELEASESTRINGCHARS_RETURN();
2281 JNI_END
2282 
2283 
2284 // UTF Interface
2285 
2286 DT_RETURN_MARK_DECL(NewStringUTF, jstring
2287                     , HOTSPOT_JNI_NEWSTRINGUTF_RETURN(_ret_ref));
2288 
2289 JNI_ENTRY(jstring, jni_NewStringUTF(JNIEnv *env, const char *bytes))
2290   HOTSPOT_JNI_NEWSTRINGUTF_ENTRY(env, (char *) bytes);
2291   jstring ret;
2292   DT_RETURN_MARK(NewStringUTF, jstring, (const jstring&)ret);
2293 
2294   oop result = java_lang_String::create_oop_from_str((char*) bytes, CHECK_NULL);
2295   ret = (jstring) JNIHandles::make_local(THREAD, result);
2296   return ret;
2297 JNI_END
2298 
2299 
2300 JNI_ENTRY(jsize, jni_GetStringUTFLength(JNIEnv *env, jstring string))
2301  HOTSPOT_JNI_GETSTRINGUTFLENGTH_ENTRY(env, string);
2302   oop java_string = JNIHandles::resolve_non_null(string);
2303   jsize ret = java_lang_String::utf8_length(java_string);
2304   HOTSPOT_JNI_GETSTRINGUTFLENGTH_RETURN(ret);
2305   return ret;
2306 JNI_END
2307 
2308 
2309 JNI_ENTRY(const char*, jni_GetStringUTFChars(JNIEnv *env, jstring string, jboolean *isCopy))
2310  HOTSPOT_JNI_GETSTRINGUTFCHARS_ENTRY(env, string, (uintptr_t *) isCopy);
2311   char* result = NULL;
2312   oop java_string = JNIHandles::resolve_non_null(string);
2313   typeArrayOop s_value = java_lang_String::value(java_string);
2314   if (s_value != NULL) {
2315     size_t length = java_lang_String::utf8_length(java_string, s_value);
2316     /* JNI Specification states return NULL on OOM */
2317     result = AllocateHeap(length + 1, mtInternal, AllocFailStrategy::RETURN_NULL);
2318     if (result != NULL) {
2319       java_lang_String::as_utf8_string(java_string, s_value, result, (int) length + 1);
2320       if (isCopy != NULL) {
2321         *isCopy = JNI_TRUE;
2322       }
2323     }
2324   }
2325  HOTSPOT_JNI_GETSTRINGUTFCHARS_RETURN(result);
2326   return result;
2327 JNI_END
2328 
2329 
2330 JNI_LEAF(void, jni_ReleaseStringUTFChars(JNIEnv *env, jstring str, const char *chars))
2331  HOTSPOT_JNI_RELEASESTRINGUTFCHARS_ENTRY(env, str, (char *) chars);
2332   if (chars != NULL) {
2333     FreeHeap((char*) chars);
2334   }
2335 HOTSPOT_JNI_RELEASESTRINGUTFCHARS_RETURN();
2336 JNI_END
2337 
2338 
2339 JNI_ENTRY_NO_PRESERVE(jsize, jni_GetArrayLength(JNIEnv *env, jarray array))
2340  HOTSPOT_JNI_GETARRAYLENGTH_ENTRY(env, array);
2341   arrayOop a = arrayOop(JNIHandles::resolve_non_null(array));
2342   assert(a->is_array(), "must be array");
2343   jsize ret = a->length();
2344  HOTSPOT_JNI_GETARRAYLENGTH_RETURN(ret);
2345   return ret;
2346 JNI_END
2347 
2348 
2349 //
2350 // Object Array Operations
2351 //
2352 
2353 DT_RETURN_MARK_DECL(NewObjectArray, jobjectArray
2354                     , HOTSPOT_JNI_NEWOBJECTARRAY_RETURN(_ret_ref));
2355 
2356 JNI_ENTRY(jobjectArray, jni_NewObjectArray(JNIEnv *env, jsize length, jclass elementClass, jobject initialElement))
2357  HOTSPOT_JNI_NEWOBJECTARRAY_ENTRY(env, length, elementClass, initialElement);
2358   jobjectArray ret = NULL;
2359   DT_RETURN_MARK(NewObjectArray, jobjectArray, (const jobjectArray&)ret);
2360   Klass* ek = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(elementClass));
2361   Klass* ak = ek->array_klass(CHECK_NULL);
2362   ObjArrayKlass::cast(ak)->initialize(CHECK_NULL);
2363   objArrayOop result = ObjArrayKlass::cast(ak)->allocate(length, CHECK_NULL);
2364   oop initial_value = JNIHandles::resolve(initialElement);
2365   if (initial_value != NULL) {  // array already initialized with NULL
2366     for (int index = 0; index < length; index++) {
2367       result->obj_at_put(index, initial_value);
2368     }
2369   }
2370   ret = (jobjectArray) JNIHandles::make_local(THREAD, result);
2371   return ret;
2372 JNI_END
2373 
2374 DT_RETURN_MARK_DECL(GetObjectArrayElement, jobject
2375                     , HOTSPOT_JNI_GETOBJECTARRAYELEMENT_RETURN(_ret_ref));
2376 
2377 JNI_ENTRY(jobject, jni_GetObjectArrayElement(JNIEnv *env, jobjectArray array, jsize index))
2378  HOTSPOT_JNI_GETOBJECTARRAYELEMENT_ENTRY(env, array, index);
2379   jobject ret = NULL;
2380   DT_RETURN_MARK(GetObjectArrayElement, jobject, (const jobject&)ret);
2381   oop res = NULL;
2382   arrayOop arr((arrayOop)JNIHandles::resolve_non_null(array));
2383   if (arr->is_within_bounds(index)) {
2384     if (arr->is_flatArray()) {
2385       flatArrayOop a = flatArrayOop(JNIHandles::resolve_non_null(array));
2386       flatArrayHandle vah(thread, a);
2387       res = flatArrayOopDesc::value_alloc_copy_from_index(vah, index, CHECK_NULL);
2388       assert(res != NULL, "Must be set in one of two paths above");
2389     } else {
2390       assert(arr->is_objArray(), "If not a valueArray. must be an objArray");
2391       objArrayOop a = objArrayOop(JNIHandles::resolve_non_null(array));
2392       res = a->obj_at(index);
2393     }
2394   } else {
2395     ResourceMark rm(THREAD);
2396     stringStream ss;
2397     ss.print("Index %d out of bounds for length %d", index,arr->length());
2398     THROW_MSG_0(vmSymbols::java_lang_ArrayIndexOutOfBoundsException(), ss.as_string());
2399   }
2400   ret = JNIHandles::make_local(THREAD, res);
2401   return ret;
2402 JNI_END
2403 
2404 DT_VOID_RETURN_MARK_DECL(SetObjectArrayElement
2405                          , HOTSPOT_JNI_SETOBJECTARRAYELEMENT_RETURN());
2406 
2407 JNI_ENTRY(void, jni_SetObjectArrayElement(JNIEnv *env, jobjectArray array, jsize index, jobject value))
2408  HOTSPOT_JNI_SETOBJECTARRAYELEMENT_ENTRY(env, array, index, value);
2409   DT_VOID_RETURN_MARK(SetObjectArrayElement);
2410 
2411    bool oob = false;
2412    int length = -1;
2413    oop res = NULL;
2414    arrayOop arr((arrayOop)JNIHandles::resolve_non_null(array));
2415    if (arr->is_within_bounds(index)) {
2416      if (arr->is_flatArray()) {
2417        flatArrayOop a = flatArrayOop(JNIHandles::resolve_non_null(array));
2418        oop v = JNIHandles::resolve(value);
2419        FlatArrayKlass* vaklass = FlatArrayKlass::cast(a->klass());
2420        InlineKlass* element_vklass = vaklass->element_klass();
2421        if (v != NULL && v->is_a(element_vklass)) {
2422          a->value_copy_to_index(v, index);
2423        } else {
2424          ResourceMark rm(THREAD);
2425          stringStream ss;
2426          Klass *kl = FlatArrayKlass::cast(a->klass());
2427          ss.print("type mismatch: can not store %s to %s[%d]",
2428              v->klass()->external_name(),
2429              kl->external_name(),
2430              index);
2431          for (int dims = ArrayKlass::cast(a->klass())->dimension(); dims > 1; --dims) {
2432            ss.print("[]");
2433          }
2434          THROW_MSG(vmSymbols::java_lang_ArrayStoreException(), ss.as_string());
2435        }
2436      } else {
2437        assert(arr->is_objArray(), "If not a valueArray. must be an objArray");
2438        objArrayOop a = objArrayOop(JNIHandles::resolve_non_null(array));
2439        oop v = JNIHandles::resolve(value);
2440        if (v == NULL || v->is_a(ObjArrayKlass::cast(a->klass())->element_klass())) {
2441          a->obj_at_put(index, v);
2442        } else {
2443          ResourceMark rm(THREAD);
2444          stringStream ss;
2445          Klass *bottom_kl = ObjArrayKlass::cast(a->klass())->bottom_klass();
2446          ss.print("type mismatch: can not store %s to %s[%d]",
2447              v->klass()->external_name(),
2448              bottom_kl->is_typeArray_klass() ? type2name_tab[ArrayKlass::cast(bottom_kl)->element_type()] : bottom_kl->external_name(),
2449                  index);
2450          for (int dims = ArrayKlass::cast(a->klass())->dimension(); dims > 1; --dims) {
2451            ss.print("[]");
2452          }
2453          THROW_MSG(vmSymbols::java_lang_ArrayStoreException(), ss.as_string());
2454        }
2455      }
2456    } else {
2457      ResourceMark rm(THREAD);
2458      stringStream ss;
2459      ss.print("Index %d out of bounds for length %d", index, arr->length());
2460      THROW_MSG(vmSymbols::java_lang_ArrayIndexOutOfBoundsException(), ss.as_string());
2461    }
2462 JNI_END
2463 
2464 
2465 
2466 #define DEFINE_NEWSCALARARRAY(Return,Allocator,Result \
2467                               ,EntryProbe,ReturnProbe)  \
2468 \
2469   DT_RETURN_MARK_DECL(New##Result##Array, Return \
2470                       , ReturnProbe); \
2471 \
2472 JNI_ENTRY(Return, \
2473           jni_New##Result##Array(JNIEnv *env, jsize len)) \
2474   EntryProbe; \
2475   Return ret = NULL;\
2476   DT_RETURN_MARK(New##Result##Array, Return, (const Return&)ret);\
2477 \
2478   oop obj= oopFactory::Allocator(len, CHECK_NULL); \
2479   ret = (Return) JNIHandles::make_local(THREAD, obj); \
2480   return ret;\
2481 JNI_END
2482 
2483 DEFINE_NEWSCALARARRAY(jbooleanArray, new_boolArray,   Boolean,
2484                       HOTSPOT_JNI_NEWBOOLEANARRAY_ENTRY(env, len),
2485                       HOTSPOT_JNI_NEWBOOLEANARRAY_RETURN(_ret_ref))
2486 DEFINE_NEWSCALARARRAY(jbyteArray,    new_byteArray,   Byte,
2487                       HOTSPOT_JNI_NEWBYTEARRAY_ENTRY(env, len),
2488                       HOTSPOT_JNI_NEWBYTEARRAY_RETURN(_ret_ref))
2489 DEFINE_NEWSCALARARRAY(jshortArray,   new_shortArray,  Short,
2490                       HOTSPOT_JNI_NEWSHORTARRAY_ENTRY(env, len),
2491                       HOTSPOT_JNI_NEWSHORTARRAY_RETURN(_ret_ref))
2492 DEFINE_NEWSCALARARRAY(jcharArray,    new_charArray,   Char,
2493                       HOTSPOT_JNI_NEWCHARARRAY_ENTRY(env, len),
2494                       HOTSPOT_JNI_NEWCHARARRAY_RETURN(_ret_ref))
2495 DEFINE_NEWSCALARARRAY(jintArray,     new_intArray,    Int,
2496                       HOTSPOT_JNI_NEWINTARRAY_ENTRY(env, len),
2497                       HOTSPOT_JNI_NEWINTARRAY_RETURN(_ret_ref))
2498 DEFINE_NEWSCALARARRAY(jlongArray,    new_longArray,   Long,
2499                       HOTSPOT_JNI_NEWLONGARRAY_ENTRY(env, len),
2500                       HOTSPOT_JNI_NEWLONGARRAY_RETURN(_ret_ref))
2501 DEFINE_NEWSCALARARRAY(jfloatArray,   new_floatArray,  Float,
2502                       HOTSPOT_JNI_NEWFLOATARRAY_ENTRY(env, len),
2503                       HOTSPOT_JNI_NEWFLOATARRAY_RETURN(_ret_ref))
2504 DEFINE_NEWSCALARARRAY(jdoubleArray,  new_doubleArray, Double,
2505                       HOTSPOT_JNI_NEWDOUBLEARRAY_ENTRY(env, len),
2506                       HOTSPOT_JNI_NEWDOUBLEARRAY_RETURN(_ret_ref))
2507 
2508 // Return an address which will fault if the caller writes to it.
2509 
2510 static char* get_bad_address() {
2511   static char* bad_address = NULL;
2512   if (bad_address == NULL) {
2513     size_t size = os::vm_allocation_granularity();
2514     bad_address = os::reserve_memory(size);
2515     if (bad_address != NULL) {
2516       os::protect_memory(bad_address, size, os::MEM_PROT_READ,
2517                          /*is_committed*/false);
2518       MemTracker::record_virtual_memory_type((void*)bad_address, mtInternal);
2519     }
2520   }
2521   return bad_address;
2522 }
2523 
2524 
2525 
2526 #define DEFINE_GETSCALARARRAYELEMENTS(ElementTag,ElementType,Result, Tag \
2527                                       , EntryProbe, ReturnProbe) \
2528 \
2529 JNI_ENTRY_NO_PRESERVE(ElementType*, \
2530           jni_Get##Result##ArrayElements(JNIEnv *env, ElementType##Array array, jboolean *isCopy)) \
2531   EntryProbe; \
2532   /* allocate an chunk of memory in c land */ \
2533   typeArrayOop a = typeArrayOop(JNIHandles::resolve_non_null(array)); \
2534   ElementType* result; \
2535   int len = a->length(); \
2536   if (len == 0) { \
2537     if (isCopy != NULL) { \
2538       *isCopy = JNI_FALSE; \
2539     } \
2540     /* Empty array: legal but useless, can't return NULL. \
2541      * Return a pointer to something useless. \
2542      * Avoid asserts in typeArrayOop. */ \
2543     result = (ElementType*)get_bad_address(); \
2544   } else { \
2545     /* JNI Specification states return NULL on OOM */                    \
2546     result = NEW_C_HEAP_ARRAY_RETURN_NULL(ElementType, len, mtInternal); \
2547     if (result != NULL) {                                                \
2548       /* copy the array to the c chunk */                                \
2549       ArrayAccess<>::arraycopy_to_native(a, typeArrayOopDesc::element_offset<ElementType>(0), \
2550                                          result, len);                   \
2551       if (isCopy) {                                                      \
2552         *isCopy = JNI_TRUE;                                              \
2553       }                                                                  \
2554     }                                                                    \
2555   } \
2556   ReturnProbe; \
2557   return result; \
2558 JNI_END
2559 
2560 DEFINE_GETSCALARARRAYELEMENTS(T_BOOLEAN, jboolean, Boolean, bool
2561                               , HOTSPOT_JNI_GETBOOLEANARRAYELEMENTS_ENTRY(env, array, (uintptr_t *) isCopy),
2562                               HOTSPOT_JNI_GETBOOLEANARRAYELEMENTS_RETURN((uintptr_t*)result))
2563 DEFINE_GETSCALARARRAYELEMENTS(T_BYTE,    jbyte,    Byte,    byte
2564                               , HOTSPOT_JNI_GETBYTEARRAYELEMENTS_ENTRY(env, array, (uintptr_t *) isCopy),
2565                               HOTSPOT_JNI_GETBYTEARRAYELEMENTS_RETURN((char*)result))
2566 DEFINE_GETSCALARARRAYELEMENTS(T_SHORT,   jshort,   Short,   short
2567                               , HOTSPOT_JNI_GETSHORTARRAYELEMENTS_ENTRY(env, (uint16_t*) array, (uintptr_t *) isCopy),
2568                               HOTSPOT_JNI_GETSHORTARRAYELEMENTS_RETURN((uint16_t*)result))
2569 DEFINE_GETSCALARARRAYELEMENTS(T_CHAR,    jchar,    Char,    char
2570                               , HOTSPOT_JNI_GETCHARARRAYELEMENTS_ENTRY(env, (uint16_t*) array, (uintptr_t *) isCopy),
2571                               HOTSPOT_JNI_GETCHARARRAYELEMENTS_RETURN(result))
2572 DEFINE_GETSCALARARRAYELEMENTS(T_INT,     jint,     Int,     int
2573                               , HOTSPOT_JNI_GETINTARRAYELEMENTS_ENTRY(env, array, (uintptr_t *) isCopy),
2574                               HOTSPOT_JNI_GETINTARRAYELEMENTS_RETURN((uint32_t*)result))
2575 DEFINE_GETSCALARARRAYELEMENTS(T_LONG,    jlong,    Long,    long
2576                               , HOTSPOT_JNI_GETLONGARRAYELEMENTS_ENTRY(env, array, (uintptr_t *) isCopy),
2577                               HOTSPOT_JNI_GETLONGARRAYELEMENTS_RETURN(((uintptr_t*)result)))
2578 // Float and double probes don't return value because dtrace doesn't currently support it
2579 DEFINE_GETSCALARARRAYELEMENTS(T_FLOAT,   jfloat,   Float,   float
2580                               , HOTSPOT_JNI_GETFLOATARRAYELEMENTS_ENTRY(env, array, (uintptr_t *) isCopy),
2581                               HOTSPOT_JNI_GETFLOATARRAYELEMENTS_RETURN(result))
2582 DEFINE_GETSCALARARRAYELEMENTS(T_DOUBLE,  jdouble,  Double,  double
2583                               , HOTSPOT_JNI_GETDOUBLEARRAYELEMENTS_ENTRY(env, array, (uintptr_t *) isCopy),
2584                               HOTSPOT_JNI_GETDOUBLEARRAYELEMENTS_RETURN(result))
2585 
2586 
2587 #define DEFINE_RELEASESCALARARRAYELEMENTS(ElementTag,ElementType,Result,Tag \
2588                                           , EntryProbe, ReturnProbe);\
2589 \
2590 JNI_ENTRY_NO_PRESERVE(void, \
2591           jni_Release##Result##ArrayElements(JNIEnv *env, ElementType##Array array, \
2592                                              ElementType *buf, jint mode)) \
2593   EntryProbe; \
2594   typeArrayOop a = typeArrayOop(JNIHandles::resolve_non_null(array)); \
2595   int len = a->length(); \
2596   if (len != 0) {   /* Empty array:  nothing to free or copy. */  \
2597     if ((mode == 0) || (mode == JNI_COMMIT)) { \
2598       ArrayAccess<>::arraycopy_from_native(buf, a, typeArrayOopDesc::element_offset<ElementType>(0), len); \
2599     } \
2600     if ((mode == 0) || (mode == JNI_ABORT)) { \
2601       FreeHeap(buf); \
2602     } \
2603   } \
2604   ReturnProbe; \
2605 JNI_END
2606 
2607 DEFINE_RELEASESCALARARRAYELEMENTS(T_BOOLEAN, jboolean, Boolean, bool
2608                                   , HOTSPOT_JNI_RELEASEBOOLEANARRAYELEMENTS_ENTRY(env, array, (uintptr_t *) buf, mode),
2609                                   HOTSPOT_JNI_RELEASEBOOLEANARRAYELEMENTS_RETURN())
2610 DEFINE_RELEASESCALARARRAYELEMENTS(T_BYTE,    jbyte,    Byte,    byte
2611                                   , HOTSPOT_JNI_RELEASEBYTEARRAYELEMENTS_ENTRY(env, array, (char *) buf, mode),
2612                                   HOTSPOT_JNI_RELEASEBYTEARRAYELEMENTS_RETURN())
2613 DEFINE_RELEASESCALARARRAYELEMENTS(T_SHORT,   jshort,   Short,   short
2614                                   ,  HOTSPOT_JNI_RELEASESHORTARRAYELEMENTS_ENTRY(env, array, (uint16_t *) buf, mode),
2615                                   HOTSPOT_JNI_RELEASESHORTARRAYELEMENTS_RETURN())
2616 DEFINE_RELEASESCALARARRAYELEMENTS(T_CHAR,    jchar,    Char,    char
2617                                   ,  HOTSPOT_JNI_RELEASECHARARRAYELEMENTS_ENTRY(env, array, (uint16_t *) buf, mode),
2618                                   HOTSPOT_JNI_RELEASECHARARRAYELEMENTS_RETURN())
2619 DEFINE_RELEASESCALARARRAYELEMENTS(T_INT,     jint,     Int,     int
2620                                   , HOTSPOT_JNI_RELEASEINTARRAYELEMENTS_ENTRY(env, array, (uint32_t *) buf, mode),
2621                                   HOTSPOT_JNI_RELEASEINTARRAYELEMENTS_RETURN())
2622 DEFINE_RELEASESCALARARRAYELEMENTS(T_LONG,    jlong,    Long,    long
2623                                   , HOTSPOT_JNI_RELEASELONGARRAYELEMENTS_ENTRY(env, array, (uintptr_t *) buf, mode),
2624                                   HOTSPOT_JNI_RELEASELONGARRAYELEMENTS_RETURN())
2625 DEFINE_RELEASESCALARARRAYELEMENTS(T_FLOAT,   jfloat,   Float,   float
2626                                   , HOTSPOT_JNI_RELEASEFLOATARRAYELEMENTS_ENTRY(env, array, (float *) buf, mode),
2627                                   HOTSPOT_JNI_RELEASEFLOATARRAYELEMENTS_RETURN())
2628 DEFINE_RELEASESCALARARRAYELEMENTS(T_DOUBLE,  jdouble,  Double,  double
2629                                   , HOTSPOT_JNI_RELEASEDOUBLEARRAYELEMENTS_ENTRY(env, array, (double *) buf, mode),
2630                                   HOTSPOT_JNI_RELEASEDOUBLEARRAYELEMENTS_RETURN())
2631 
2632 static void check_bounds(jsize start, jsize copy_len, jsize array_len, TRAPS) {
2633   ResourceMark rm(THREAD);
2634   if (copy_len < 0) {
2635     stringStream ss;
2636     ss.print("Length %d is negative", copy_len);
2637     THROW_MSG(vmSymbols::java_lang_ArrayIndexOutOfBoundsException(), ss.as_string());
2638   } else if (start < 0 || (start > array_len - copy_len)) {
2639     stringStream ss;
2640     ss.print("Array region %d.." INT64_FORMAT " out of bounds for length %d",
2641              start, (int64_t)start+(int64_t)copy_len, array_len);
2642     THROW_MSG(vmSymbols::java_lang_ArrayIndexOutOfBoundsException(), ss.as_string());
2643   }
2644 }
2645 
2646 #define DEFINE_GETSCALARARRAYREGION(ElementTag,ElementType,Result, Tag \
2647                                     , EntryProbe, ReturnProbe); \
2648   DT_VOID_RETURN_MARK_DECL(Get##Result##ArrayRegion \
2649                            , ReturnProbe); \
2650 \
2651 JNI_ENTRY(void, \
2652 jni_Get##Result##ArrayRegion(JNIEnv *env, ElementType##Array array, jsize start, \
2653              jsize len, ElementType *buf)) \
2654   EntryProbe; \
2655   DT_VOID_RETURN_MARK(Get##Result##ArrayRegion); \
2656   typeArrayOop src = typeArrayOop(JNIHandles::resolve_non_null(array)); \
2657   check_bounds(start, len, src->length(), CHECK); \
2658   if (len > 0) {    \
2659     ArrayAccess<>::arraycopy_to_native(src, typeArrayOopDesc::element_offset<ElementType>(start), buf, len); \
2660   } \
2661 JNI_END
2662 
2663 DEFINE_GETSCALARARRAYREGION(T_BOOLEAN, jboolean,Boolean, bool
2664                             , HOTSPOT_JNI_GETBOOLEANARRAYREGION_ENTRY(env, array, start, len, (uintptr_t *) buf),
2665                             HOTSPOT_JNI_GETBOOLEANARRAYREGION_RETURN());
2666 DEFINE_GETSCALARARRAYREGION(T_BYTE,    jbyte,   Byte,    byte
2667                             ,  HOTSPOT_JNI_GETBYTEARRAYREGION_ENTRY(env, array, start, len, (char *) buf),
2668                             HOTSPOT_JNI_GETBYTEARRAYREGION_RETURN());
2669 DEFINE_GETSCALARARRAYREGION(T_SHORT,   jshort,  Short,   short
2670                             , HOTSPOT_JNI_GETSHORTARRAYREGION_ENTRY(env, array, start, len, (uint16_t *) buf),
2671                             HOTSPOT_JNI_GETSHORTARRAYREGION_RETURN());
2672 DEFINE_GETSCALARARRAYREGION(T_CHAR,    jchar,   Char,    char
2673                             ,  HOTSPOT_JNI_GETCHARARRAYREGION_ENTRY(env, array, start, len, (uint16_t*) buf),
2674                             HOTSPOT_JNI_GETCHARARRAYREGION_RETURN());
2675 DEFINE_GETSCALARARRAYREGION(T_INT,     jint,    Int,     int
2676                             , HOTSPOT_JNI_GETINTARRAYREGION_ENTRY(env, array, start, len, (uint32_t*) buf),
2677                             HOTSPOT_JNI_GETINTARRAYREGION_RETURN());
2678 DEFINE_GETSCALARARRAYREGION(T_LONG,    jlong,   Long,    long
2679                             ,  HOTSPOT_JNI_GETLONGARRAYREGION_ENTRY(env, array, start, len, (uintptr_t *) buf),
2680                             HOTSPOT_JNI_GETLONGARRAYREGION_RETURN());
2681 DEFINE_GETSCALARARRAYREGION(T_FLOAT,   jfloat,  Float,   float
2682                             , HOTSPOT_JNI_GETFLOATARRAYREGION_ENTRY(env, array, start, len, (float *) buf),
2683                             HOTSPOT_JNI_GETFLOATARRAYREGION_RETURN());
2684 DEFINE_GETSCALARARRAYREGION(T_DOUBLE,  jdouble, Double,  double
2685                             , HOTSPOT_JNI_GETDOUBLEARRAYREGION_ENTRY(env, array, start, len, (double *) buf),
2686                             HOTSPOT_JNI_GETDOUBLEARRAYREGION_RETURN());
2687 
2688 
2689 #define DEFINE_SETSCALARARRAYREGION(ElementTag,ElementType,Result, Tag \
2690                                     , EntryProbe, ReturnProbe); \
2691   DT_VOID_RETURN_MARK_DECL(Set##Result##ArrayRegion \
2692                            ,ReturnProbe);           \
2693 \
2694 JNI_ENTRY(void, \
2695 jni_Set##Result##ArrayRegion(JNIEnv *env, ElementType##Array array, jsize start, \
2696              jsize len, const ElementType *buf)) \
2697   EntryProbe; \
2698   DT_VOID_RETURN_MARK(Set##Result##ArrayRegion); \
2699   typeArrayOop dst = typeArrayOop(JNIHandles::resolve_non_null(array)); \
2700   check_bounds(start, len, dst->length(), CHECK); \
2701   if (len > 0) { \
2702     ArrayAccess<>::arraycopy_from_native(buf, dst, typeArrayOopDesc::element_offset<ElementType>(start), len); \
2703   } \
2704 JNI_END
2705 
2706 DEFINE_SETSCALARARRAYREGION(T_BOOLEAN, jboolean, Boolean, bool
2707                             , HOTSPOT_JNI_SETBOOLEANARRAYREGION_ENTRY(env, array, start, len, (uintptr_t *)buf),
2708                             HOTSPOT_JNI_SETBOOLEANARRAYREGION_RETURN())
2709 DEFINE_SETSCALARARRAYREGION(T_BYTE,    jbyte,    Byte,    byte
2710                             , HOTSPOT_JNI_SETBYTEARRAYREGION_ENTRY(env, array, start, len, (char *) buf),
2711                             HOTSPOT_JNI_SETBYTEARRAYREGION_RETURN())
2712 DEFINE_SETSCALARARRAYREGION(T_SHORT,   jshort,   Short,   short
2713                             , HOTSPOT_JNI_SETSHORTARRAYREGION_ENTRY(env, array, start, len, (uint16_t *) buf),
2714                             HOTSPOT_JNI_SETSHORTARRAYREGION_RETURN())
2715 DEFINE_SETSCALARARRAYREGION(T_CHAR,    jchar,    Char,    char
2716                             , HOTSPOT_JNI_SETCHARARRAYREGION_ENTRY(env, array, start, len, (uint16_t *) buf),
2717                             HOTSPOT_JNI_SETCHARARRAYREGION_RETURN())
2718 DEFINE_SETSCALARARRAYREGION(T_INT,     jint,     Int,     int
2719                             , HOTSPOT_JNI_SETINTARRAYREGION_ENTRY(env, array, start, len, (uint32_t *) buf),
2720                             HOTSPOT_JNI_SETINTARRAYREGION_RETURN())
2721 DEFINE_SETSCALARARRAYREGION(T_LONG,    jlong,    Long,    long
2722                             , HOTSPOT_JNI_SETLONGARRAYREGION_ENTRY(env, array, start, len, (uintptr_t *) buf),
2723                             HOTSPOT_JNI_SETLONGARRAYREGION_RETURN())
2724 DEFINE_SETSCALARARRAYREGION(T_FLOAT,   jfloat,   Float,   float
2725                             , HOTSPOT_JNI_SETFLOATARRAYREGION_ENTRY(env, array, start, len, (float *) buf),
2726                             HOTSPOT_JNI_SETFLOATARRAYREGION_RETURN())
2727 DEFINE_SETSCALARARRAYREGION(T_DOUBLE,  jdouble,  Double,  double
2728                             , HOTSPOT_JNI_SETDOUBLEARRAYREGION_ENTRY(env, array, start, len, (double *) buf),
2729                             HOTSPOT_JNI_SETDOUBLEARRAYREGION_RETURN())
2730 
2731 
2732 DT_RETURN_MARK_DECL(RegisterNatives, jint
2733                     , HOTSPOT_JNI_REGISTERNATIVES_RETURN(_ret_ref));
2734 
2735 JNI_ENTRY(jint, jni_RegisterNatives(JNIEnv *env, jclass clazz,
2736                                     const JNINativeMethod *methods,
2737                                     jint nMethods))
2738   HOTSPOT_JNI_REGISTERNATIVES_ENTRY(env, clazz, (void *) methods, nMethods);
2739   jint ret = 0;
2740   DT_RETURN_MARK(RegisterNatives, jint, (const jint&)ret);
2741 
2742   Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(clazz));
2743 
2744   // There are no restrictions on native code registering native methods,
2745   // which allows agents to redefine the bindings to native methods, however
2746   // we issue a warning if any code running outside of the boot/platform
2747   // loader is rebinding any native methods in classes loaded by the
2748   // boot/platform loader that are in named modules. That will catch changes
2749   // to platform classes while excluding classes added to the bootclasspath.
2750   bool do_warning = false;
2751 
2752   // Only instanceKlasses can have native methods
2753   if (k->is_instance_klass()) {
2754     oop cl = k->class_loader();
2755     InstanceKlass* ik = InstanceKlass::cast(k);
2756     // Check for a platform class
2757     if ((cl ==  NULL || SystemDictionary::is_platform_class_loader(cl)) &&
2758         ik->module()->is_named()) {
2759       Klass* caller = thread->security_get_caller_class(1);
2760       // If no caller class, or caller class has a different loader, then
2761       // issue a warning below.
2762       do_warning = (caller == NULL) || caller->class_loader() != cl;
2763     }
2764   }
2765 
2766 
2767   for (int index = 0; index < nMethods; index++) {
2768     const char* meth_name = methods[index].name;
2769     const char* meth_sig = methods[index].signature;
2770     int meth_name_len = (int)strlen(meth_name);
2771 
2772     // The class should have been loaded (we have an instance of the class
2773     // passed in) so the method and signature should already be in the symbol
2774     // table.  If they're not there, the method doesn't exist.
2775     TempNewSymbol  name = SymbolTable::probe(meth_name, meth_name_len);
2776     TempNewSymbol  signature = SymbolTable::probe(meth_sig, (int)strlen(meth_sig));
2777 
2778     if (name == NULL || signature == NULL) {
2779       ResourceMark rm(THREAD);
2780       stringStream st;
2781       st.print("Method %s.%s%s not found", k->external_name(), meth_name, meth_sig);
2782       // Must return negative value on failure
2783       THROW_MSG_(vmSymbols::java_lang_NoSuchMethodError(), st.as_string(), -1);
2784     }
2785 
2786     if (do_warning) {
2787       ResourceMark rm(THREAD);
2788       log_warning(jni, resolve)("Re-registering of platform native method: %s.%s%s "
2789               "from code in a different classloader", k->external_name(), meth_name, meth_sig);
2790     }
2791 
2792     bool res = Method::register_native(k, name, signature,
2793                                        (address) methods[index].fnPtr, THREAD);
2794     if (!res) {
2795       ret = -1;
2796       break;
2797     }
2798   }
2799   return ret;
2800 JNI_END
2801 
2802 
2803 JNI_ENTRY(jint, jni_UnregisterNatives(JNIEnv *env, jclass clazz))
2804  HOTSPOT_JNI_UNREGISTERNATIVES_ENTRY(env, clazz);
2805   Klass* k   = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(clazz));
2806   //%note jni_2
2807   if (k->is_instance_klass()) {
2808     for (int index = 0; index < InstanceKlass::cast(k)->methods()->length(); index++) {
2809       Method* m = InstanceKlass::cast(k)->methods()->at(index);
2810       if (m->is_native()) {
2811         m->clear_native_function();
2812         m->set_signature_handler(NULL);
2813       }
2814     }
2815   }
2816  HOTSPOT_JNI_UNREGISTERNATIVES_RETURN(0);
2817   return 0;
2818 JNI_END
2819 
2820 //
2821 // Monitor functions
2822 //
2823 
2824 DT_RETURN_MARK_DECL(MonitorEnter, jint
2825                     , HOTSPOT_JNI_MONITORENTER_RETURN(_ret_ref));
2826 
2827 JNI_ENTRY(jint, jni_MonitorEnter(JNIEnv *env, jobject jobj))
2828  HOTSPOT_JNI_MONITORENTER_ENTRY(env, jobj);
2829   jint ret = JNI_ERR;
2830   DT_RETURN_MARK(MonitorEnter, jint, (const jint&)ret);
2831 
2832   // If the object is null, we can't do anything with it
2833   if (jobj == NULL) {
2834     THROW_(vmSymbols::java_lang_NullPointerException(), JNI_ERR);
2835   }
2836 
2837   Handle obj(thread, JNIHandles::resolve_non_null(jobj));
2838   ObjectSynchronizer::jni_enter(obj, thread);
2839   return JNI_OK;
2840 JNI_END
2841 
2842 DT_RETURN_MARK_DECL(MonitorExit, jint
2843                     , HOTSPOT_JNI_MONITOREXIT_RETURN(_ret_ref));
2844 
2845 JNI_ENTRY(jint, jni_MonitorExit(JNIEnv *env, jobject jobj))
2846  HOTSPOT_JNI_MONITOREXIT_ENTRY(env, jobj);
2847   jint ret = JNI_ERR;
2848   DT_RETURN_MARK(MonitorExit, jint, (const jint&)ret);
2849 
2850   // Don't do anything with a null object
2851   if (jobj == NULL) {
2852     THROW_(vmSymbols::java_lang_NullPointerException(), JNI_ERR);
2853   }
2854 
2855   Handle obj(THREAD, JNIHandles::resolve_non_null(jobj));
2856   ObjectSynchronizer::jni_exit(obj(), CHECK_(JNI_ERR));
2857   return JNI_OK;
2858 JNI_END
2859 
2860 //
2861 // Extensions
2862 //
2863 
2864 DT_VOID_RETURN_MARK_DECL(GetStringRegion
2865                          , HOTSPOT_JNI_GETSTRINGREGION_RETURN());
2866 
2867 JNI_ENTRY(void, jni_GetStringRegion(JNIEnv *env, jstring string, jsize start, jsize len, jchar *buf))
2868  HOTSPOT_JNI_GETSTRINGREGION_ENTRY(env, string, start, len, buf);
2869   DT_VOID_RETURN_MARK(GetStringRegion);
2870   oop s = JNIHandles::resolve_non_null(string);
2871   typeArrayOop s_value = java_lang_String::value(s);
2872   int s_len = java_lang_String::length(s, s_value);
2873   if (start < 0 || len < 0 || start > s_len - len) {
2874     THROW(vmSymbols::java_lang_StringIndexOutOfBoundsException());
2875   } else {
2876     if (len > 0) {
2877       bool is_latin1 = java_lang_String::is_latin1(s);
2878       if (!is_latin1) {
2879         ArrayAccess<>::arraycopy_to_native(s_value, typeArrayOopDesc::element_offset<jchar>(start),
2880                                            buf, len);
2881       } else {
2882         for (int i = 0; i < len; i++) {
2883           buf[i] = ((jchar) s_value->byte_at(i + start)) & 0xff;
2884         }
2885       }
2886     }
2887   }
2888 JNI_END
2889 
2890 DT_VOID_RETURN_MARK_DECL(GetStringUTFRegion
2891                          , HOTSPOT_JNI_GETSTRINGUTFREGION_RETURN());
2892 
2893 JNI_ENTRY(void, jni_GetStringUTFRegion(JNIEnv *env, jstring string, jsize start, jsize len, char *buf))
2894  HOTSPOT_JNI_GETSTRINGUTFREGION_ENTRY(env, string, start, len, buf);
2895   DT_VOID_RETURN_MARK(GetStringUTFRegion);
2896   oop s = JNIHandles::resolve_non_null(string);
2897   typeArrayOop s_value = java_lang_String::value(s);
2898   int s_len = java_lang_String::length(s, s_value);
2899   if (start < 0 || len < 0 || start > s_len - len) {
2900     THROW(vmSymbols::java_lang_StringIndexOutOfBoundsException());
2901   } else {
2902     //%note jni_7
2903     if (len > 0) {
2904       // Assume the buffer is large enough as the JNI spec. does not require user error checking
2905       java_lang_String::as_utf8_string(s, s_value, start, len, buf, INT_MAX);
2906       // as_utf8_string null-terminates the result string
2907     } else {
2908       // JDK null-terminates the buffer even in len is zero
2909       if (buf != NULL) {
2910         buf[0] = 0;
2911       }
2912     }
2913   }
2914 JNI_END
2915 
2916 static oop lock_gc_or_pin_object(JavaThread* thread, jobject obj) {
2917   if (Universe::heap()->supports_object_pinning()) {
2918     const oop o = JNIHandles::resolve_non_null(obj);
2919     return Universe::heap()->pin_object(thread, o);
2920   } else {
2921     GCLocker::lock_critical(thread);
2922     return JNIHandles::resolve_non_null(obj);
2923   }
2924 }
2925 
2926 static void unlock_gc_or_unpin_object(JavaThread* thread, jobject obj) {
2927   if (Universe::heap()->supports_object_pinning()) {
2928     const oop o = JNIHandles::resolve_non_null(obj);
2929     return Universe::heap()->unpin_object(thread, o);
2930   } else {
2931     GCLocker::unlock_critical(thread);
2932   }
2933 }
2934 
2935 JNI_ENTRY(void*, jni_GetPrimitiveArrayCritical(JNIEnv *env, jarray array, jboolean *isCopy))
2936  HOTSPOT_JNI_GETPRIMITIVEARRAYCRITICAL_ENTRY(env, array, (uintptr_t *) isCopy);
2937   if (isCopy != NULL) {
2938     *isCopy = JNI_FALSE;
2939   }
2940   oop a = lock_gc_or_pin_object(thread, array);
2941   assert(a->is_typeArray(), "Primitive array only");
2942   BasicType type = TypeArrayKlass::cast(a->klass())->element_type();
2943   void* ret = arrayOop(a)->base(type);
2944  HOTSPOT_JNI_GETPRIMITIVEARRAYCRITICAL_RETURN(ret);
2945   return ret;
2946 JNI_END
2947 
2948 
2949 JNI_ENTRY(void, jni_ReleasePrimitiveArrayCritical(JNIEnv *env, jarray array, void *carray, jint mode))
2950   HOTSPOT_JNI_RELEASEPRIMITIVEARRAYCRITICAL_ENTRY(env, array, carray, mode);
2951   unlock_gc_or_unpin_object(thread, array);
2952 HOTSPOT_JNI_RELEASEPRIMITIVEARRAYCRITICAL_RETURN();
2953 JNI_END
2954 
2955 
2956 static typeArrayOop lock_gc_or_pin_string_value(JavaThread* thread, oop str) {
2957   if (Universe::heap()->supports_object_pinning()) {
2958     // Forbid deduplication before obtaining the value array, to prevent
2959     // deduplication from replacing the value array while setting up or in
2960     // the critical section.  That would lead to the release operation
2961     // unpinning the wrong object.
2962     if (StringDedup::is_enabled()) {
2963       NoSafepointVerifier nsv;
2964       StringDedup::forbid_deduplication(str);
2965     }
2966     typeArrayOop s_value = java_lang_String::value(str);
2967     return (typeArrayOop) Universe::heap()->pin_object(thread, s_value);
2968   } else {
2969     Handle h(thread, str);      // Handlize across potential safepoint.
2970     GCLocker::lock_critical(thread);
2971     return java_lang_String::value(h());
2972   }
2973 }
2974 
2975 static void unlock_gc_or_unpin_string_value(JavaThread* thread, oop str) {
2976   if (Universe::heap()->supports_object_pinning()) {
2977     typeArrayOop s_value = java_lang_String::value(str);
2978     Universe::heap()->unpin_object(thread, s_value);
2979   } else {
2980     GCLocker::unlock_critical(thread);
2981   }
2982 }
2983 
2984 JNI_ENTRY(const jchar*, jni_GetStringCritical(JNIEnv *env, jstring string, jboolean *isCopy))
2985   HOTSPOT_JNI_GETSTRINGCRITICAL_ENTRY(env, string, (uintptr_t *) isCopy);
2986   oop s = JNIHandles::resolve_non_null(string);
2987   jchar* ret;
2988   if (!java_lang_String::is_latin1(s)) {
2989     typeArrayOop s_value = lock_gc_or_pin_string_value(thread, s);
2990     ret = (jchar*) s_value->base(T_CHAR);
2991     if (isCopy != NULL) *isCopy = JNI_FALSE;
2992   } else {
2993     // Inflate latin1 encoded string to UTF16
2994     typeArrayOop s_value = java_lang_String::value(s);
2995     int s_len = java_lang_String::length(s, s_value);
2996     ret = NEW_C_HEAP_ARRAY_RETURN_NULL(jchar, s_len + 1, mtInternal);  // add one for zero termination
2997     /* JNI Specification states return NULL on OOM */
2998     if (ret != NULL) {
2999       for (int i = 0; i < s_len; i++) {
3000         ret[i] = ((jchar) s_value->byte_at(i)) & 0xff;
3001       }
3002       ret[s_len] = 0;
3003     }
3004     if (isCopy != NULL) *isCopy = JNI_TRUE;
3005   }
3006  HOTSPOT_JNI_GETSTRINGCRITICAL_RETURN((uint16_t *) ret);
3007   return ret;
3008 JNI_END
3009 
3010 
3011 JNI_ENTRY(void, jni_ReleaseStringCritical(JNIEnv *env, jstring str, const jchar *chars))
3012   HOTSPOT_JNI_RELEASESTRINGCRITICAL_ENTRY(env, str, (uint16_t *) chars);
3013   oop s = JNIHandles::resolve_non_null(str);
3014   bool is_latin1 = java_lang_String::is_latin1(s);
3015   if (is_latin1) {
3016     // For latin1 string, free jchar array allocated by earlier call to GetStringCritical.
3017     // This assumes that ReleaseStringCritical bookends GetStringCritical.
3018     FREE_C_HEAP_ARRAY(jchar, chars);
3019   } else {
3020     // For non-latin1 string, drop the associated gc-locker/pin.
3021     unlock_gc_or_unpin_string_value(thread, s);
3022   }
3023 HOTSPOT_JNI_RELEASESTRINGCRITICAL_RETURN();
3024 JNI_END
3025 
3026 
3027 JNI_ENTRY(jweak, jni_NewWeakGlobalRef(JNIEnv *env, jobject ref))
3028   HOTSPOT_JNI_NEWWEAKGLOBALREF_ENTRY(env, ref);
3029   Handle ref_handle(thread, JNIHandles::resolve(ref));
3030   jweak ret = JNIHandles::make_weak_global(ref_handle, AllocFailStrategy::RETURN_NULL);
3031   if (ret == NULL) {
3032     THROW_OOP_(Universe::out_of_memory_error_c_heap(), NULL);
3033   }
3034   HOTSPOT_JNI_NEWWEAKGLOBALREF_RETURN(ret);
3035   return ret;
3036 JNI_END
3037 
3038 // Must be JNI_ENTRY (with HandleMark)
3039 JNI_ENTRY(void, jni_DeleteWeakGlobalRef(JNIEnv *env, jweak ref))
3040   HOTSPOT_JNI_DELETEWEAKGLOBALREF_ENTRY(env, ref);
3041   JNIHandles::destroy_weak_global(ref);
3042   HOTSPOT_JNI_DELETEWEAKGLOBALREF_RETURN();
3043 JNI_END
3044 
3045 
3046 JNI_ENTRY_NO_PRESERVE(jboolean, jni_ExceptionCheck(JNIEnv *env))
3047  HOTSPOT_JNI_EXCEPTIONCHECK_ENTRY(env);
3048   jni_check_async_exceptions(thread);
3049   jboolean ret = (thread->has_pending_exception()) ? JNI_TRUE : JNI_FALSE;
3050  HOTSPOT_JNI_EXCEPTIONCHECK_RETURN(ret);
3051   return ret;
3052 JNI_END
3053 
3054 
3055 // Initialization state for three routines below relating to
3056 // java.nio.DirectBuffers
3057 static          int directBufferSupportInitializeStarted = 0;
3058 static volatile int directBufferSupportInitializeEnded   = 0;
3059 static volatile int directBufferSupportInitializeFailed  = 0;
3060 static jclass    bufferClass                 = NULL;
3061 static jclass    directBufferClass           = NULL;
3062 static jclass    directByteBufferClass       = NULL;
3063 static jmethodID directByteBufferConstructor = NULL;
3064 static jfieldID  directBufferAddressField    = NULL;
3065 static jfieldID  bufferCapacityField         = NULL;
3066 
3067 static jclass lookupOne(JNIEnv* env, const char* name, TRAPS) {
3068   Handle loader;            // null (bootstrap) loader
3069   Handle protection_domain; // null protection domain
3070 
3071   TempNewSymbol sym = SymbolTable::new_symbol(name);
3072   jclass result =  find_class_from_class_loader(env, sym, true, loader, protection_domain, true, CHECK_NULL);
3073 
3074   if (log_is_enabled(Debug, class, resolve) && result != NULL) {
3075     trace_class_resolution(java_lang_Class::as_Klass(JNIHandles::resolve_non_null(result)));
3076   }
3077   return result;
3078 }
3079 
3080 // These lookups are done with the NULL (bootstrap) ClassLoader to
3081 // circumvent any security checks that would be done by jni_FindClass.
3082 JNI_ENTRY(bool, lookupDirectBufferClasses(JNIEnv* env))
3083 {
3084   if ((bufferClass           = lookupOne(env, "java/nio/Buffer", thread))           == NULL) { return false; }
3085   if ((directBufferClass     = lookupOne(env, "sun/nio/ch/DirectBuffer", thread))   == NULL) { return false; }
3086   if ((directByteBufferClass = lookupOne(env, "java/nio/DirectByteBuffer", thread)) == NULL) { return false; }
3087   return true;
3088 }
3089 JNI_END
3090 
3091 
3092 static bool initializeDirectBufferSupport(JNIEnv* env, JavaThread* thread) {
3093   if (directBufferSupportInitializeFailed) {
3094     return false;
3095   }
3096 
3097   if (Atomic::cmpxchg(&directBufferSupportInitializeStarted, 0, 1) == 0) {
3098     if (!lookupDirectBufferClasses(env)) {
3099       directBufferSupportInitializeFailed = 1;
3100       return false;
3101     }
3102 
3103     // Make global references for these
3104     bufferClass           = (jclass) env->NewGlobalRef(bufferClass);
3105     directBufferClass     = (jclass) env->NewGlobalRef(directBufferClass);
3106     directByteBufferClass = (jclass) env->NewGlobalRef(directByteBufferClass);
3107 
3108     // Global refs will be NULL if out-of-memory (no exception is pending)
3109     if (bufferClass == NULL || directBufferClass == NULL || directByteBufferClass == NULL) {
3110       directBufferSupportInitializeFailed = 1;
3111       return false;
3112     }
3113 
3114     // Get needed field and method IDs
3115     directByteBufferConstructor = env->GetMethodID(directByteBufferClass, "<init>", "(JI)V");
3116     if (env->ExceptionCheck()) {
3117       env->ExceptionClear();
3118       directBufferSupportInitializeFailed = 1;
3119       return false;
3120     }
3121     directBufferAddressField    = env->GetFieldID(bufferClass, "address", "J");
3122     if (env->ExceptionCheck()) {
3123       env->ExceptionClear();
3124       directBufferSupportInitializeFailed = 1;
3125       return false;
3126     }
3127     bufferCapacityField         = env->GetFieldID(bufferClass, "capacity", "I");
3128     if (env->ExceptionCheck()) {
3129       env->ExceptionClear();
3130       directBufferSupportInitializeFailed = 1;
3131       return false;
3132     }
3133 
3134     if ((directByteBufferConstructor == NULL) ||
3135         (directBufferAddressField    == NULL) ||
3136         (bufferCapacityField         == NULL)) {
3137       directBufferSupportInitializeFailed = 1;
3138       return false;
3139     }
3140 
3141     directBufferSupportInitializeEnded = 1;
3142   } else {
3143     while (!directBufferSupportInitializeEnded && !directBufferSupportInitializeFailed) {
3144       os::naked_yield();
3145     }
3146   }
3147 
3148   return !directBufferSupportInitializeFailed;
3149 }
3150 
3151 extern "C" jobject JNICALL jni_NewDirectByteBuffer(JNIEnv *env, void* address, jlong capacity)
3152 {
3153   // thread_from_jni_environment() will block if VM is gone.
3154   JavaThread* thread = JavaThread::thread_from_jni_environment(env);
3155 
3156  HOTSPOT_JNI_NEWDIRECTBYTEBUFFER_ENTRY(env, address, capacity);
3157 
3158   if (!directBufferSupportInitializeEnded) {
3159     if (!initializeDirectBufferSupport(env, thread)) {
3160       HOTSPOT_JNI_NEWDIRECTBYTEBUFFER_RETURN(NULL);
3161       return NULL;
3162     }
3163   }
3164 
3165   // Being paranoid about accidental sign extension on address
3166   jlong addr = (jlong) ((uintptr_t) address);
3167   // NOTE that package-private DirectByteBuffer constructor currently
3168   // takes int capacity
3169   jint  cap  = (jint)  capacity;
3170   jobject ret = env->NewObject(directByteBufferClass, directByteBufferConstructor, addr, cap);
3171   HOTSPOT_JNI_NEWDIRECTBYTEBUFFER_RETURN(ret);
3172   return ret;
3173 }
3174 
3175 DT_RETURN_MARK_DECL(GetDirectBufferAddress, void*
3176                     , HOTSPOT_JNI_GETDIRECTBUFFERADDRESS_RETURN((void*) _ret_ref));
3177 
3178 extern "C" void* JNICALL jni_GetDirectBufferAddress(JNIEnv *env, jobject buf)
3179 {
3180   // thread_from_jni_environment() will block if VM is gone.
3181   JavaThread* thread = JavaThread::thread_from_jni_environment(env);
3182 
3183   HOTSPOT_JNI_GETDIRECTBUFFERADDRESS_ENTRY(env, buf);
3184   void* ret = NULL;
3185   DT_RETURN_MARK(GetDirectBufferAddress, void*, (const void*&)ret);
3186 
3187   if (!directBufferSupportInitializeEnded) {
3188     if (!initializeDirectBufferSupport(env, thread)) {
3189       return 0;
3190     }
3191   }
3192 
3193   if ((buf != NULL) && (!env->IsInstanceOf(buf, directBufferClass))) {
3194     return 0;
3195   }
3196 
3197   ret = (void*)(intptr_t)env->GetLongField(buf, directBufferAddressField);
3198   return ret;
3199 }
3200 
3201 DT_RETURN_MARK_DECL(GetDirectBufferCapacity, jlong
3202                     , HOTSPOT_JNI_GETDIRECTBUFFERCAPACITY_RETURN(_ret_ref));
3203 
3204 extern "C" jlong JNICALL jni_GetDirectBufferCapacity(JNIEnv *env, jobject buf)
3205 {
3206   // thread_from_jni_environment() will block if VM is gone.
3207   JavaThread* thread = JavaThread::thread_from_jni_environment(env);
3208 
3209   HOTSPOT_JNI_GETDIRECTBUFFERCAPACITY_ENTRY(env, buf);
3210   jlong ret = -1;
3211   DT_RETURN_MARK(GetDirectBufferCapacity, jlong, (const jlong&)ret);
3212 
3213   if (!directBufferSupportInitializeEnded) {
3214     if (!initializeDirectBufferSupport(env, thread)) {
3215       ret = 0;
3216       return ret;
3217     }
3218   }
3219 
3220   if (buf == NULL) {
3221     return -1;
3222   }
3223 
3224   if (!env->IsInstanceOf(buf, directBufferClass)) {
3225     return -1;
3226   }
3227 
3228   // NOTE that capacity is currently an int in the implementation
3229   ret = env->GetIntField(buf, bufferCapacityField);
3230   return ret;
3231 }
3232 
3233 
3234 JNI_LEAF(jint, jni_GetVersion(JNIEnv *env))
3235   HOTSPOT_JNI_GETVERSION_ENTRY(env);
3236   HOTSPOT_JNI_GETVERSION_RETURN(CurrentVersion);
3237   return CurrentVersion;
3238 JNI_END
3239 
3240 extern struct JavaVM_ main_vm;
3241 
3242 JNI_LEAF(jint, jni_GetJavaVM(JNIEnv *env, JavaVM **vm))
3243   HOTSPOT_JNI_GETJAVAVM_ENTRY(env, (void **) vm);
3244   *vm  = (JavaVM *)(&main_vm);
3245   HOTSPOT_JNI_GETJAVAVM_RETURN(JNI_OK);
3246   return JNI_OK;
3247 JNI_END
3248 
3249 
3250 JNI_ENTRY(jobject, jni_GetModule(JNIEnv* env, jclass clazz))
3251   return Modules::get_module(clazz, THREAD);
3252 JNI_END
3253 
3254 JNI_ENTRY(jboolean, jni_IsVirtualThread(JNIEnv* env, jobject obj))
3255   oop thread_obj = JNIHandles::resolve_external_guard(obj);
3256   if (thread_obj != NULL && thread_obj->is_a(vmClasses::BasicVirtualThread_klass())) {
3257     return JNI_TRUE;
3258   } else {
3259     return JNI_FALSE;
3260   }
3261 JNI_END
3262 
3263 
3264 // Structure containing all jni functions
3265 struct JNINativeInterface_ jni_NativeInterface = {
3266     NULL,
3267     NULL,
3268     NULL,
3269 
3270     NULL,
3271 
3272     jni_GetVersion,
3273 
3274     jni_DefineClass,
3275     jni_FindClass,
3276 
3277     jni_FromReflectedMethod,
3278     jni_FromReflectedField,
3279 
3280     jni_ToReflectedMethod,
3281 
3282     jni_GetSuperclass,
3283     jni_IsAssignableFrom,
3284 
3285     jni_ToReflectedField,
3286 
3287     jni_Throw,
3288     jni_ThrowNew,
3289     jni_ExceptionOccurred,
3290     jni_ExceptionDescribe,
3291     jni_ExceptionClear,
3292     jni_FatalError,
3293 
3294     jni_PushLocalFrame,
3295     jni_PopLocalFrame,
3296 
3297     jni_NewGlobalRef,
3298     jni_DeleteGlobalRef,
3299     jni_DeleteLocalRef,
3300     jni_IsSameObject,
3301 
3302     jni_NewLocalRef,
3303     jni_EnsureLocalCapacity,
3304 
3305     jni_AllocObject,
3306     jni_NewObject,
3307     jni_NewObjectV,
3308     jni_NewObjectA,
3309 
3310     jni_GetObjectClass,
3311     jni_IsInstanceOf,
3312 
3313     jni_GetMethodID,
3314 
3315     jni_CallObjectMethod,
3316     jni_CallObjectMethodV,
3317     jni_CallObjectMethodA,
3318     jni_CallBooleanMethod,
3319     jni_CallBooleanMethodV,
3320     jni_CallBooleanMethodA,
3321     jni_CallByteMethod,
3322     jni_CallByteMethodV,
3323     jni_CallByteMethodA,
3324     jni_CallCharMethod,
3325     jni_CallCharMethodV,
3326     jni_CallCharMethodA,
3327     jni_CallShortMethod,
3328     jni_CallShortMethodV,
3329     jni_CallShortMethodA,
3330     jni_CallIntMethod,
3331     jni_CallIntMethodV,
3332     jni_CallIntMethodA,
3333     jni_CallLongMethod,
3334     jni_CallLongMethodV,
3335     jni_CallLongMethodA,
3336     jni_CallFloatMethod,
3337     jni_CallFloatMethodV,
3338     jni_CallFloatMethodA,
3339     jni_CallDoubleMethod,
3340     jni_CallDoubleMethodV,
3341     jni_CallDoubleMethodA,
3342     jni_CallVoidMethod,
3343     jni_CallVoidMethodV,
3344     jni_CallVoidMethodA,
3345 
3346     jni_CallNonvirtualObjectMethod,
3347     jni_CallNonvirtualObjectMethodV,
3348     jni_CallNonvirtualObjectMethodA,
3349     jni_CallNonvirtualBooleanMethod,
3350     jni_CallNonvirtualBooleanMethodV,
3351     jni_CallNonvirtualBooleanMethodA,
3352     jni_CallNonvirtualByteMethod,
3353     jni_CallNonvirtualByteMethodV,
3354     jni_CallNonvirtualByteMethodA,
3355     jni_CallNonvirtualCharMethod,
3356     jni_CallNonvirtualCharMethodV,
3357     jni_CallNonvirtualCharMethodA,
3358     jni_CallNonvirtualShortMethod,
3359     jni_CallNonvirtualShortMethodV,
3360     jni_CallNonvirtualShortMethodA,
3361     jni_CallNonvirtualIntMethod,
3362     jni_CallNonvirtualIntMethodV,
3363     jni_CallNonvirtualIntMethodA,
3364     jni_CallNonvirtualLongMethod,
3365     jni_CallNonvirtualLongMethodV,
3366     jni_CallNonvirtualLongMethodA,
3367     jni_CallNonvirtualFloatMethod,
3368     jni_CallNonvirtualFloatMethodV,
3369     jni_CallNonvirtualFloatMethodA,
3370     jni_CallNonvirtualDoubleMethod,
3371     jni_CallNonvirtualDoubleMethodV,
3372     jni_CallNonvirtualDoubleMethodA,
3373     jni_CallNonvirtualVoidMethod,
3374     jni_CallNonvirtualVoidMethodV,
3375     jni_CallNonvirtualVoidMethodA,
3376 
3377     jni_GetFieldID,
3378 
3379     jni_GetObjectField,
3380     jni_GetBooleanField,
3381     jni_GetByteField,
3382     jni_GetCharField,
3383     jni_GetShortField,
3384     jni_GetIntField,
3385     jni_GetLongField,
3386     jni_GetFloatField,
3387     jni_GetDoubleField,
3388 
3389     jni_SetObjectField,
3390     jni_SetBooleanField,
3391     jni_SetByteField,
3392     jni_SetCharField,
3393     jni_SetShortField,
3394     jni_SetIntField,
3395     jni_SetLongField,
3396     jni_SetFloatField,
3397     jni_SetDoubleField,
3398 
3399     jni_GetStaticMethodID,
3400 
3401     jni_CallStaticObjectMethod,
3402     jni_CallStaticObjectMethodV,
3403     jni_CallStaticObjectMethodA,
3404     jni_CallStaticBooleanMethod,
3405     jni_CallStaticBooleanMethodV,
3406     jni_CallStaticBooleanMethodA,
3407     jni_CallStaticByteMethod,
3408     jni_CallStaticByteMethodV,
3409     jni_CallStaticByteMethodA,
3410     jni_CallStaticCharMethod,
3411     jni_CallStaticCharMethodV,
3412     jni_CallStaticCharMethodA,
3413     jni_CallStaticShortMethod,
3414     jni_CallStaticShortMethodV,
3415     jni_CallStaticShortMethodA,
3416     jni_CallStaticIntMethod,
3417     jni_CallStaticIntMethodV,
3418     jni_CallStaticIntMethodA,
3419     jni_CallStaticLongMethod,
3420     jni_CallStaticLongMethodV,
3421     jni_CallStaticLongMethodA,
3422     jni_CallStaticFloatMethod,
3423     jni_CallStaticFloatMethodV,
3424     jni_CallStaticFloatMethodA,
3425     jni_CallStaticDoubleMethod,
3426     jni_CallStaticDoubleMethodV,
3427     jni_CallStaticDoubleMethodA,
3428     jni_CallStaticVoidMethod,
3429     jni_CallStaticVoidMethodV,
3430     jni_CallStaticVoidMethodA,
3431 
3432     jni_GetStaticFieldID,
3433 
3434     jni_GetStaticObjectField,
3435     jni_GetStaticBooleanField,
3436     jni_GetStaticByteField,
3437     jni_GetStaticCharField,
3438     jni_GetStaticShortField,
3439     jni_GetStaticIntField,
3440     jni_GetStaticLongField,
3441     jni_GetStaticFloatField,
3442     jni_GetStaticDoubleField,
3443 
3444     jni_SetStaticObjectField,
3445     jni_SetStaticBooleanField,
3446     jni_SetStaticByteField,
3447     jni_SetStaticCharField,
3448     jni_SetStaticShortField,
3449     jni_SetStaticIntField,
3450     jni_SetStaticLongField,
3451     jni_SetStaticFloatField,
3452     jni_SetStaticDoubleField,
3453 
3454     jni_NewString,
3455     jni_GetStringLength,
3456     jni_GetStringChars,
3457     jni_ReleaseStringChars,
3458 
3459     jni_NewStringUTF,
3460     jni_GetStringUTFLength,
3461     jni_GetStringUTFChars,
3462     jni_ReleaseStringUTFChars,
3463 
3464     jni_GetArrayLength,
3465 
3466     jni_NewObjectArray,
3467     jni_GetObjectArrayElement,
3468     jni_SetObjectArrayElement,
3469 
3470     jni_NewBooleanArray,
3471     jni_NewByteArray,
3472     jni_NewCharArray,
3473     jni_NewShortArray,
3474     jni_NewIntArray,
3475     jni_NewLongArray,
3476     jni_NewFloatArray,
3477     jni_NewDoubleArray,
3478 
3479     jni_GetBooleanArrayElements,
3480     jni_GetByteArrayElements,
3481     jni_GetCharArrayElements,
3482     jni_GetShortArrayElements,
3483     jni_GetIntArrayElements,
3484     jni_GetLongArrayElements,
3485     jni_GetFloatArrayElements,
3486     jni_GetDoubleArrayElements,
3487 
3488     jni_ReleaseBooleanArrayElements,
3489     jni_ReleaseByteArrayElements,
3490     jni_ReleaseCharArrayElements,
3491     jni_ReleaseShortArrayElements,
3492     jni_ReleaseIntArrayElements,
3493     jni_ReleaseLongArrayElements,
3494     jni_ReleaseFloatArrayElements,
3495     jni_ReleaseDoubleArrayElements,
3496 
3497     jni_GetBooleanArrayRegion,
3498     jni_GetByteArrayRegion,
3499     jni_GetCharArrayRegion,
3500     jni_GetShortArrayRegion,
3501     jni_GetIntArrayRegion,
3502     jni_GetLongArrayRegion,
3503     jni_GetFloatArrayRegion,
3504     jni_GetDoubleArrayRegion,
3505 
3506     jni_SetBooleanArrayRegion,
3507     jni_SetByteArrayRegion,
3508     jni_SetCharArrayRegion,
3509     jni_SetShortArrayRegion,
3510     jni_SetIntArrayRegion,
3511     jni_SetLongArrayRegion,
3512     jni_SetFloatArrayRegion,
3513     jni_SetDoubleArrayRegion,
3514 
3515     jni_RegisterNatives,
3516     jni_UnregisterNatives,
3517 
3518     jni_MonitorEnter,
3519     jni_MonitorExit,
3520 
3521     jni_GetJavaVM,
3522 
3523     jni_GetStringRegion,
3524     jni_GetStringUTFRegion,
3525 
3526     jni_GetPrimitiveArrayCritical,
3527     jni_ReleasePrimitiveArrayCritical,
3528 
3529     jni_GetStringCritical,
3530     jni_ReleaseStringCritical,
3531 
3532     jni_NewWeakGlobalRef,
3533     jni_DeleteWeakGlobalRef,
3534 
3535     jni_ExceptionCheck,
3536 
3537     jni_NewDirectByteBuffer,
3538     jni_GetDirectBufferAddress,
3539     jni_GetDirectBufferCapacity,
3540 
3541     // New 1_6 features
3542 
3543     jni_GetObjectRefType,
3544 
3545     // Module features
3546 
3547     jni_GetModule,
3548 
3549     // Virtual threads
3550 
3551     jni_IsVirtualThread
3552 };
3553 
3554 
3555 // For jvmti use to modify jni function table.
3556 // Java threads in native contiues to run until it is transitioned
3557 // to VM at safepoint. Before the transition or before it is blocked
3558 // for safepoint it may access jni function table. VM could crash if
3559 // any java thread access the jni function table in the middle of memcpy.
3560 // To avoid this each function pointers are copied automically.
3561 void copy_jni_function_table(const struct JNINativeInterface_ *new_jni_NativeInterface) {
3562   assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint");
3563   intptr_t *a = (intptr_t *) jni_functions();
3564   intptr_t *b = (intptr_t *) new_jni_NativeInterface;
3565   for (uint i=0; i <  sizeof(struct JNINativeInterface_)/sizeof(void *); i++) {
3566     Atomic::store(a++, *b++);
3567   }
3568 }
3569 
3570 void quicken_jni_functions() {
3571   // Replace Get<Primitive>Field with fast versions
3572   if (UseFastJNIAccessors && !VerifyJNIFields && !CheckJNICalls) {
3573     address func;
3574     func = JNI_FastGetField::generate_fast_get_boolean_field();
3575     if (func != (address)-1) {
3576       jni_NativeInterface.GetBooleanField = (GetBooleanField_t)func;
3577     }
3578     func = JNI_FastGetField::generate_fast_get_byte_field();
3579     if (func != (address)-1) {
3580       jni_NativeInterface.GetByteField = (GetByteField_t)func;
3581     }
3582     func = JNI_FastGetField::generate_fast_get_char_field();
3583     if (func != (address)-1) {
3584       jni_NativeInterface.GetCharField = (GetCharField_t)func;
3585     }
3586     func = JNI_FastGetField::generate_fast_get_short_field();
3587     if (func != (address)-1) {
3588       jni_NativeInterface.GetShortField = (GetShortField_t)func;
3589     }
3590     func = JNI_FastGetField::generate_fast_get_int_field();
3591     if (func != (address)-1) {
3592       jni_NativeInterface.GetIntField = (GetIntField_t)func;
3593     }
3594     func = JNI_FastGetField::generate_fast_get_long_field();
3595     if (func != (address)-1) {
3596       jni_NativeInterface.GetLongField = (GetLongField_t)func;
3597     }
3598     func = JNI_FastGetField::generate_fast_get_float_field();
3599     if (func != (address)-1) {
3600       jni_NativeInterface.GetFloatField = (GetFloatField_t)func;
3601     }
3602     func = JNI_FastGetField::generate_fast_get_double_field();
3603     if (func != (address)-1) {
3604       jni_NativeInterface.GetDoubleField = (GetDoubleField_t)func;
3605     }
3606   }
3607 }
3608 
3609 // Returns the function structure
3610 struct JNINativeInterface_* jni_functions() {
3611 #if INCLUDE_JNI_CHECK
3612   if (CheckJNICalls) return jni_functions_check();
3613 #endif // INCLUDE_JNI_CHECK
3614   return &jni_NativeInterface;
3615 }
3616 
3617 // Returns the function structure
3618 struct JNINativeInterface_* jni_functions_nocheck() {
3619   return &jni_NativeInterface;
3620 }
3621 
3622 static void post_thread_start_event(const JavaThread* jt) {
3623   assert(jt != NULL, "invariant");
3624   EventThreadStart event;
3625   if (event.should_commit()) {
3626     event.set_thread(JFR_JVM_THREAD_ID(jt));
3627     event.set_parentThread((traceid)0);
3628 #if INCLUDE_JFR
3629     if (EventThreadStart::is_stacktrace_enabled()) {
3630       jt->jfr_thread_local()->set_cached_stack_trace_id((traceid)0);
3631       event.commit();
3632       jt->jfr_thread_local()->clear_cached_stack_trace();
3633     } else
3634 #endif
3635     {
3636       event.commit();
3637     }
3638   }
3639 }
3640 
3641 // Invocation API
3642 
3643 
3644 // Forward declaration
3645 extern const struct JNIInvokeInterface_ jni_InvokeInterface;
3646 
3647 // Global invocation API vars
3648 volatile int vm_created = 0;
3649 // Indicate whether it is safe to recreate VM. Recreation is only
3650 // possible after a failed initial creation attempt in some cases.
3651 volatile int safe_to_recreate_vm = 1;
3652 struct JavaVM_ main_vm = {&jni_InvokeInterface};
3653 
3654 
3655 #define JAVASTACKSIZE (400 * 1024)    /* Default size of a thread java stack */
3656 enum { VERIFY_NONE, VERIFY_REMOTE, VERIFY_ALL };
3657 
3658 DT_RETURN_MARK_DECL(GetDefaultJavaVMInitArgs, jint
3659                     , HOTSPOT_JNI_GETDEFAULTJAVAVMINITARGS_RETURN(_ret_ref));
3660 
3661 _JNI_IMPORT_OR_EXPORT_ jint JNICALL JNI_GetDefaultJavaVMInitArgs(void *args_) {
3662   HOTSPOT_JNI_GETDEFAULTJAVAVMINITARGS_ENTRY(args_);
3663   JDK1_1InitArgs *args = (JDK1_1InitArgs *)args_;
3664   jint ret = JNI_ERR;
3665   DT_RETURN_MARK(GetDefaultJavaVMInitArgs, jint, (const jint&)ret);
3666 
3667   if (Threads::is_supported_jni_version(args->version)) {
3668     ret = JNI_OK;
3669   }
3670   // 1.1 style no longer supported in hotspot.
3671   // According the JNI spec, we should update args->version on return.
3672   // We also use the structure to communicate with launcher about default
3673   // stack size.
3674   if (args->version == JNI_VERSION_1_1) {
3675     args->version = JNI_VERSION_1_2;
3676     // javaStackSize is int in arguments structure
3677     assert(jlong(ThreadStackSize) * K < INT_MAX, "integer overflow");
3678     args->javaStackSize = (jint)(ThreadStackSize * K);
3679   }
3680   return ret;
3681 }
3682 
3683 DT_RETURN_MARK_DECL(CreateJavaVM, jint
3684                     , HOTSPOT_JNI_CREATEJAVAVM_RETURN(_ret_ref));
3685 
3686 static jint JNI_CreateJavaVM_inner(JavaVM **vm, void **penv, void *args) {
3687   HOTSPOT_JNI_CREATEJAVAVM_ENTRY((void **) vm, penv, args);
3688 
3689   jint result = JNI_ERR;
3690   DT_RETURN_MARK(CreateJavaVM, jint, (const jint&)result);
3691 
3692   // We're about to use Atomic::xchg for synchronization.  Some Zero
3693   // platforms use the GCC builtin __sync_lock_test_and_set for this,
3694   // but __sync_lock_test_and_set is not guaranteed to do what we want
3695   // on all architectures.  So we check it works before relying on it.
3696 #if defined(ZERO) && defined(ASSERT)
3697   {
3698     jint a = 0xcafebabe;
3699     jint b = Atomic::xchg(&a, (jint) 0xdeadbeef);
3700     void *c = &a;
3701     void *d = Atomic::xchg(&c, &b);
3702     assert(a == (jint) 0xdeadbeef && b == (jint) 0xcafebabe, "Atomic::xchg() works");
3703     assert(c == &b && d == &a, "Atomic::xchg() works");
3704   }
3705 #endif // ZERO && ASSERT
3706 
3707   // At the moment it's only possible to have one Java VM,
3708   // since some of the runtime state is in global variables.
3709 
3710   // We cannot use our mutex locks here, since they only work on
3711   // Threads. We do an atomic compare and exchange to ensure only
3712   // one thread can call this method at a time
3713 
3714   // We use Atomic::xchg rather than Atomic::add/dec since on some platforms
3715   // the add/dec implementations are dependent on whether we are running
3716   // on a multiprocessor Atomic::xchg does not have this problem.
3717   if (Atomic::xchg(&vm_created, 1) == 1) {
3718     return JNI_EEXIST;   // already created, or create attempt in progress
3719   }
3720 
3721   // If a previous creation attempt failed but can be retried safely,
3722   // then safe_to_recreate_vm will have been reset to 1 after being
3723   // cleared here. If a previous creation attempt succeeded and we then
3724   // destroyed that VM, we will be prevented from trying to recreate
3725   // the VM in the same process, as the value will still be 0.
3726   if (Atomic::xchg(&safe_to_recreate_vm, 0) == 0) {
3727     return JNI_ERR;
3728   }
3729 
3730   assert(vm_created == 1, "vm_created is true during the creation");
3731 
3732   /**
3733    * Certain errors during initialization are recoverable and do not
3734    * prevent this method from being called again at a later time
3735    * (perhaps with different arguments).  However, at a certain
3736    * point during initialization if an error occurs we cannot allow
3737    * this function to be called again (or it will crash).  In those
3738    * situations, the 'canTryAgain' flag is set to false, which atomically
3739    * sets safe_to_recreate_vm to 1, such that any new call to
3740    * JNI_CreateJavaVM will immediately fail using the above logic.
3741    */
3742   bool can_try_again = true;
3743 
3744   result = Threads::create_vm((JavaVMInitArgs*) args, &can_try_again);
3745   if (result == JNI_OK) {
3746     JavaThread *thread = JavaThread::current();
3747     assert(!thread->has_pending_exception(), "should have returned not OK");
3748     /* thread is thread_in_vm here */
3749     *vm = (JavaVM *)(&main_vm);
3750     *(JNIEnv**)penv = thread->jni_environment();
3751 
3752 #if INCLUDE_JVMCI
3753     if (EnableJVMCI) {
3754       if (UseJVMCICompiler) {
3755         // JVMCI is initialized on a CompilerThread
3756         if (BootstrapJVMCI) {
3757           JavaThread* THREAD = thread; // For exception macros.
3758           JVMCICompiler* compiler = JVMCICompiler::instance(true, CATCH);
3759           compiler->bootstrap(THREAD);
3760           if (HAS_PENDING_EXCEPTION) {
3761             HandleMark hm(THREAD);
3762             vm_exit_during_initialization(Handle(THREAD, PENDING_EXCEPTION));
3763           }
3764         }
3765       }
3766     }
3767 #endif
3768 
3769     // Notify JVMTI
3770     if (JvmtiExport::should_post_thread_life()) {
3771        JvmtiExport::post_thread_start(thread);
3772     }
3773 
3774     post_thread_start_event(thread);
3775 
3776     if (ReplayCompiles) ciReplay::replay(thread);
3777 
3778 #ifdef ASSERT
3779     // Some platforms (like Win*) need a wrapper around these test
3780     // functions in order to properly handle error conditions.
3781     if (ErrorHandlerTest != 0) {
3782       VMError::controlled_crash(ErrorHandlerTest);
3783     }
3784 #endif
3785 
3786     // Since this is not a JVM_ENTRY we have to set the thread state manually before leaving.
3787     ThreadStateTransition::transition_from_vm(thread, _thread_in_native);
3788     MACOS_AARCH64_ONLY(thread->enable_wx(WXExec));
3789   } else {
3790     // If create_vm exits because of a pending exception, exit with that
3791     // exception.  In the future when we figure out how to reclaim memory,
3792     // we may be able to exit with JNI_ERR and allow the calling application
3793     // to continue.
3794     if (Universe::is_fully_initialized()) {
3795       // otherwise no pending exception possible - VM will already have aborted
3796       JavaThread* THREAD = JavaThread::current(); // For exception macros.
3797       if (HAS_PENDING_EXCEPTION) {
3798         HandleMark hm(THREAD);
3799         vm_exit_during_initialization(Handle(THREAD, PENDING_EXCEPTION));
3800       }
3801     }
3802 
3803     if (can_try_again) {
3804       // reset safe_to_recreate_vm to 1 so that retrial would be possible
3805       safe_to_recreate_vm = 1;
3806     }
3807 
3808     // Creation failed. We must reset vm_created
3809     *vm = 0;
3810     *(JNIEnv**)penv = 0;
3811     // reset vm_created last to avoid race condition. Use OrderAccess to
3812     // control both compiler and architectural-based reordering.
3813     Atomic::release_store(&vm_created, 0);
3814   }
3815 
3816   // Flush stdout and stderr before exit.
3817   fflush(stdout);
3818   fflush(stderr);
3819 
3820   return result;
3821 
3822 }
3823 
3824 _JNI_IMPORT_OR_EXPORT_ jint JNICALL JNI_CreateJavaVM(JavaVM **vm, void **penv, void *args) {
3825   jint result = JNI_ERR;
3826   // On Windows, let CreateJavaVM run with SEH protection
3827 #if defined(_WIN32) && !defined(USE_VECTORED_EXCEPTION_HANDLING)
3828   __try {
3829 #endif
3830     result = JNI_CreateJavaVM_inner(vm, penv, args);
3831 #if defined(_WIN32) && !defined(USE_VECTORED_EXCEPTION_HANDLING)
3832   } __except(topLevelExceptionFilter((_EXCEPTION_POINTERS*)_exception_info())) {
3833     // Nothing to do.
3834   }
3835 #endif
3836   return result;
3837 }
3838 
3839 _JNI_IMPORT_OR_EXPORT_ jint JNICALL JNI_GetCreatedJavaVMs(JavaVM **vm_buf, jsize bufLen, jsize *numVMs) {
3840   HOTSPOT_JNI_GETCREATEDJAVAVMS_ENTRY((void **) vm_buf, bufLen, (uintptr_t *) numVMs);
3841 
3842   if (vm_created == 1) {
3843     if (numVMs != NULL) *numVMs = 1;
3844     if (bufLen > 0)     *vm_buf = (JavaVM *)(&main_vm);
3845   } else {
3846     if (numVMs != NULL) *numVMs = 0;
3847   }
3848   HOTSPOT_JNI_GETCREATEDJAVAVMS_RETURN(JNI_OK);
3849   return JNI_OK;
3850 }
3851 
3852 extern "C" {
3853 
3854 DT_RETURN_MARK_DECL(DestroyJavaVM, jint
3855                     , HOTSPOT_JNI_DESTROYJAVAVM_RETURN(_ret_ref));
3856 
3857 static jint JNICALL jni_DestroyJavaVM_inner(JavaVM *vm) {
3858   HOTSPOT_JNI_DESTROYJAVAVM_ENTRY(vm);
3859   jint res = JNI_ERR;
3860   DT_RETURN_MARK(DestroyJavaVM, jint, (const jint&)res);
3861 
3862   if (vm_created == 0) {
3863     res = JNI_ERR;
3864     return res;
3865   }
3866 
3867   JNIEnv *env;
3868   JavaVMAttachArgs destroyargs;
3869   destroyargs.version = CurrentVersion;
3870   destroyargs.name = (char *)"DestroyJavaVM";
3871   destroyargs.group = NULL;
3872   res = vm->AttachCurrentThread((void **)&env, (void *)&destroyargs);
3873   if (res != JNI_OK) {
3874     return res;
3875   }
3876 
3877   JavaThread* thread = JavaThread::current();
3878 
3879   // Make sure we are actually in a newly attached thread, with no
3880   // existing Java frame.
3881   if (thread->has_last_Java_frame()) {
3882     return JNI_ERR;
3883   }
3884 
3885   // Since this is not a JVM_ENTRY we have to set the thread state manually before entering.
3886 
3887   // We are going to VM, change W^X state to the expected one.
3888   MACOS_AARCH64_ONLY(WXMode oldmode = thread->enable_wx(WXWrite));
3889 
3890   ThreadStateTransition::transition_from_native(thread, _thread_in_vm);
3891   Threads::destroy_vm();
3892   // Don't bother restoring thread state, VM is gone.
3893   vm_created = 0;
3894   return JNI_OK;
3895 }
3896 
3897 jint JNICALL jni_DestroyJavaVM(JavaVM *vm) {
3898   jint result = JNI_ERR;
3899   // On Windows, we need SEH protection
3900 #if defined(_WIN32) && !defined(USE_VECTORED_EXCEPTION_HANDLING)
3901   __try {
3902 #endif
3903     result = jni_DestroyJavaVM_inner(vm);
3904 #if defined(_WIN32) && !defined(USE_VECTORED_EXCEPTION_HANDLING)
3905   } __except(topLevelExceptionFilter((_EXCEPTION_POINTERS*)_exception_info())) {
3906     // Nothing to do.
3907   }
3908 #endif
3909   return result;
3910 }
3911 
3912 static jint attach_current_thread(JavaVM *vm, void **penv, void *_args, bool daemon) {
3913   JavaVMAttachArgs *args = (JavaVMAttachArgs *) _args;
3914 
3915   // Check below commented out from JDK1.2fcs as well
3916   /*
3917   if (args && (args->version != JNI_VERSION_1_1 || args->version != JNI_VERSION_1_2)) {
3918     return JNI_EVERSION;
3919   }
3920   */
3921 
3922   Thread* t = Thread::current_or_null();
3923   if (t != NULL) {
3924     // If executing from an atexit hook we may be in the VMThread.
3925     if (t->is_Java_thread()) {
3926       // If the thread has been attached this operation is a no-op
3927       *(JNIEnv**)penv = JavaThread::cast(t)->jni_environment();
3928       return JNI_OK;
3929     } else {
3930       return JNI_ERR;
3931     }
3932   }
3933 
3934   // Create a thread and mark it as attaching so it will be skipped by the
3935   // ThreadsListEnumerator - see CR 6404306
3936   JavaThread* thread = new JavaThread(true);
3937 
3938   // Set correct safepoint info. The thread is going to call into Java when
3939   // initializing the Java level thread object. Hence, the correct state must
3940   // be set in order for the Safepoint code to deal with it correctly.
3941   thread->set_thread_state(_thread_in_vm);
3942   thread->record_stack_base_and_size();
3943   thread->register_thread_stack_with_NMT();
3944   thread->initialize_thread_current();
3945   MACOS_AARCH64_ONLY(thread->init_wx());
3946 
3947   if (!os::create_attached_thread(thread)) {
3948     thread->smr_delete();
3949     return JNI_ERR;
3950   }
3951   // Enable stack overflow checks
3952   thread->stack_overflow_state()->create_stack_guard_pages();
3953 
3954   thread->initialize_tlab();
3955 
3956   thread->cache_global_variables();
3957 
3958   // This thread will not do a safepoint check, since it has
3959   // not been added to the Thread list yet.
3960   { MutexLocker ml(Threads_lock);
3961     // This must be inside this lock in order to get FullGCALot to work properly, i.e., to
3962     // avoid this thread trying to do a GC before it is added to the thread-list
3963     thread->set_active_handles(JNIHandleBlock::allocate_block());
3964     Threads::add(thread, daemon);
3965   }
3966   // Create thread group and name info from attach arguments
3967   oop group = NULL;
3968   char* thread_name = NULL;
3969   if (args != NULL && Threads::is_supported_jni_version(args->version)) {
3970     group = JNIHandles::resolve(args->group);
3971     thread_name = args->name; // may be NULL
3972   }
3973   if (group == NULL) group = Universe::main_thread_group();
3974 
3975   // Create Java level thread object and attach it to this thread
3976   bool attach_failed = false;
3977   {
3978     EXCEPTION_MARK;
3979     HandleMark hm(THREAD);
3980     Handle thread_group(THREAD, group);
3981     thread->allocate_threadObj(thread_group, thread_name, daemon, THREAD);
3982     if (HAS_PENDING_EXCEPTION) {
3983       CLEAR_PENDING_EXCEPTION;
3984       // cleanup outside the handle mark.
3985       attach_failed = true;
3986     }
3987   }
3988 
3989   if (attach_failed) {
3990     // Added missing cleanup
3991     thread->cleanup_failed_attach_current_thread(daemon);
3992     return JNI_ERR;
3993   }
3994 
3995   // mark the thread as no longer attaching
3996   // this uses a fence to push the change through so we don't have
3997   // to regrab the threads_lock
3998   thread->set_done_attaching_via_jni();
3999 
4000   // Set java thread status.
4001   java_lang_Thread::set_thread_status(thread->threadObj(),
4002               JavaThreadStatus::RUNNABLE);
4003 
4004   // Notify the debugger
4005   if (JvmtiExport::should_post_thread_life()) {
4006     JvmtiExport::post_thread_start(thread);
4007   }
4008 
4009   post_thread_start_event(thread);
4010 
4011   *(JNIEnv**)penv = thread->jni_environment();
4012 
4013   // Now leaving the VM, so change thread_state. This is normally automatically taken care
4014   // of in the JVM_ENTRY. But in this situation we have to do it manually.
4015   ThreadStateTransition::transition_from_vm(thread, _thread_in_native);
4016   MACOS_AARCH64_ONLY(thread->enable_wx(WXExec));
4017 
4018   // Perform any platform dependent FPU setup
4019   os::setup_fpu();
4020 
4021   return JNI_OK;
4022 }
4023 
4024 
4025 jint JNICALL jni_AttachCurrentThread(JavaVM *vm, void **penv, void *_args) {
4026   HOTSPOT_JNI_ATTACHCURRENTTHREAD_ENTRY(vm, penv, _args);
4027   if (vm_created == 0) {
4028     HOTSPOT_JNI_ATTACHCURRENTTHREAD_RETURN((uint32_t) JNI_ERR);
4029     return JNI_ERR;
4030   }
4031 
4032   jint ret = attach_current_thread(vm, penv, _args, false);
4033   HOTSPOT_JNI_ATTACHCURRENTTHREAD_RETURN(ret);
4034   return ret;
4035 }
4036 
4037 
4038 jint JNICALL jni_DetachCurrentThread(JavaVM *vm)  {
4039   HOTSPOT_JNI_DETACHCURRENTTHREAD_ENTRY(vm);
4040   if (vm_created == 0) {
4041     HOTSPOT_JNI_DETACHCURRENTTHREAD_RETURN(JNI_ERR);
4042     return JNI_ERR;
4043   }
4044 
4045   Thread* current = Thread::current_or_null();
4046 
4047   // If the thread has already been detached the operation is a no-op
4048   if (current == NULL) {
4049     HOTSPOT_JNI_DETACHCURRENTTHREAD_RETURN(JNI_OK);
4050     return JNI_OK;
4051   }
4052 
4053   // If executing from an atexit hook we may be in the VMThread.
4054   if (!current->is_Java_thread()) {
4055     HOTSPOT_JNI_DETACHCURRENTTHREAD_RETURN((uint32_t) JNI_ERR);
4056     return JNI_ERR;
4057   }
4058 
4059   VM_Exit::block_if_vm_exited();
4060 
4061   JavaThread* thread = JavaThread::cast(current);
4062   if (thread->has_last_Java_frame()) {
4063     HOTSPOT_JNI_DETACHCURRENTTHREAD_RETURN((uint32_t) JNI_ERR);
4064     // Can't detach a thread that's running java, that can't work.
4065     return JNI_ERR;
4066   }
4067 
4068   // We are going to VM, change W^X state to the expected one.
4069   MACOS_AARCH64_ONLY(thread->enable_wx(WXWrite));
4070 
4071   // Safepoint support. Have to do call-back to safepoint code, if in the
4072   // middle of a safepoint operation
4073   ThreadStateTransition::transition_from_native(thread, _thread_in_vm);
4074 
4075   // XXX: Note that JavaThread::exit() call below removes the guards on the
4076   // stack pages set up via enable_stack_{red,yellow}_zone() calls
4077   // above in jni_AttachCurrentThread. Unfortunately, while the setting
4078   // of the guards is visible in jni_AttachCurrentThread above,
4079   // the removal of the guards is buried below in JavaThread::exit()
4080   // here. The abstraction should be more symmetrically either exposed
4081   // or hidden (e.g. it could probably be hidden in the same
4082   // (platform-dependent) methods where we do alternate stack
4083   // maintenance work?)
4084   thread->exit(false, JavaThread::jni_detach);
4085   thread->smr_delete();
4086 
4087   // Go to the execute mode, the initial state of the thread on creation.
4088   // Use os interface as the thread is not a JavaThread anymore.
4089   MACOS_AARCH64_ONLY(os::current_thread_enable_wx(WXExec));
4090 
4091   HOTSPOT_JNI_DETACHCURRENTTHREAD_RETURN(JNI_OK);
4092   return JNI_OK;
4093 }
4094 
4095 DT_RETURN_MARK_DECL(GetEnv, jint
4096                     , HOTSPOT_JNI_GETENV_RETURN(_ret_ref));
4097 
4098 jint JNICALL jni_GetEnv(JavaVM *vm, void **penv, jint version) {
4099   HOTSPOT_JNI_GETENV_ENTRY(vm, penv, version);
4100   jint ret = JNI_ERR;
4101   DT_RETURN_MARK(GetEnv, jint, (const jint&)ret);
4102 
4103   if (vm_created == 0) {
4104     *penv = NULL;
4105     ret = JNI_EDETACHED;
4106     return ret;
4107   }
4108 
4109   // No JVM TI with --enable-preview and no continuations support.
4110   if (!VMContinuations && Arguments::enable_preview() && JvmtiExport::is_jvmti_version(version)) {
4111     *penv = NULL;
4112     ret = JNI_EVERSION;
4113     return ret;
4114   }
4115 
4116   if (JniExportedInterface::GetExportedInterface(vm, penv, version, &ret)) {
4117     return ret;
4118   }
4119 
4120 #ifndef JVMPI_VERSION_1
4121 // need these in order to be polite about older agents
4122 #define JVMPI_VERSION_1   ((jint)0x10000001)
4123 #define JVMPI_VERSION_1_1 ((jint)0x10000002)
4124 #define JVMPI_VERSION_1_2 ((jint)0x10000003)
4125 #endif // !JVMPI_VERSION_1
4126 
4127   Thread* thread = Thread::current_or_null();
4128   if (thread != NULL && thread->is_Java_thread()) {
4129     if (Threads::is_supported_jni_version_including_1_1(version)) {
4130       *(JNIEnv**)penv = JavaThread::cast(thread)->jni_environment();
4131       ret = JNI_OK;
4132       return ret;
4133 
4134     } else if (version == JVMPI_VERSION_1 ||
4135                version == JVMPI_VERSION_1_1 ||
4136                version == JVMPI_VERSION_1_2) {
4137       tty->print_cr("ERROR: JVMPI, an experimental interface, is no longer supported.");
4138       tty->print_cr("Please use the supported interface: the JVM Tool Interface (JVM TI).");
4139       ret = JNI_EVERSION;
4140       return ret;
4141     } else if (JvmtiExport::is_jvmdi_version(version)) {
4142       tty->print_cr("FATAL ERROR: JVMDI is no longer supported.");
4143       tty->print_cr("Please use the supported interface: the JVM Tool Interface (JVM TI).");
4144       ret = JNI_EVERSION;
4145       return ret;
4146     } else {
4147       *penv = NULL;
4148       ret = JNI_EVERSION;
4149       return ret;
4150     }
4151   } else {
4152     *penv = NULL;
4153     ret = JNI_EDETACHED;
4154     return ret;
4155   }
4156 }
4157 
4158 
4159 jint JNICALL jni_AttachCurrentThreadAsDaemon(JavaVM *vm, void **penv, void *_args) {
4160   HOTSPOT_JNI_ATTACHCURRENTTHREADASDAEMON_ENTRY(vm, penv, _args);
4161   if (vm_created == 0) {
4162   HOTSPOT_JNI_ATTACHCURRENTTHREADASDAEMON_RETURN((uint32_t) JNI_ERR);
4163     return JNI_ERR;
4164   }
4165 
4166   jint ret = attach_current_thread(vm, penv, _args, true);
4167   HOTSPOT_JNI_ATTACHCURRENTTHREADASDAEMON_RETURN(ret);
4168   return ret;
4169 }
4170 
4171 
4172 } // End extern "C"
4173 
4174 const struct JNIInvokeInterface_ jni_InvokeInterface = {
4175     NULL,
4176     NULL,
4177     NULL,
4178 
4179     jni_DestroyJavaVM,
4180     jni_AttachCurrentThread,
4181     jni_DetachCurrentThread,
4182     jni_GetEnv,
4183     jni_AttachCurrentThreadAsDaemon
4184 };