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