rev 50331 : 8198285: More consistent Access API for arraycopy
rev 50332 : [mq]: JDK-8203232-2.patch
rev 50333 : [mq]: JDK-8198285-3.patch

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