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