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