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           memcpy(buf, s_value->char_at_addr(0), sizeof(jchar)*s_len);

2470         } else {
2471           for (int i = 0; i < s_len; i++) {
2472             buf[i] = ((jchar) s_value->byte_at(i)) & 0xff;
2473           }
2474         }
2475       }
2476       buf[s_len] = 0;
2477       //%note jni_5
2478       if (isCopy != NULL) {
2479         *isCopy = JNI_TRUE;
2480       }
2481     }
2482   }
2483   HOTSPOT_JNI_GETSTRINGCHARS_RETURN(buf);
2484   return buf;
2485 JNI_END
2486 
2487 
2488 JNI_QUICK_ENTRY(void, jni_ReleaseStringChars(JNIEnv *env, jstring str, const jchar *chars))
2489   JNIWrapper("ReleaseStringChars");
2490   HOTSPOT_JNI_RELEASESTRINGCHARS_ENTRY(env, str, (uint16_t *) chars);
2491   //%note jni_6
2492   if (chars != NULL) {
2493     // Since String objects are supposed to be immutable, don't copy any
2494     // new data back.  A bad user will have to go after the char array.
2495     FreeHeap((void*) chars);
2496   }
2497   HOTSPOT_JNI_RELEASESTRINGCHARS_RETURN();
2498 JNI_END
2499 
2500 
2501 // UTF Interface
2502 
2503 DT_RETURN_MARK_DECL(NewStringUTF, jstring
2504                     , HOTSPOT_JNI_NEWSTRINGUTF_RETURN(_ret_ref));
2505 
2506 JNI_ENTRY(jstring, jni_NewStringUTF(JNIEnv *env, const char *bytes))
2507   JNIWrapper("NewStringUTF");
2508   HOTSPOT_JNI_NEWSTRINGUTF_ENTRY(env, (char *) bytes);
2509   jstring ret;
2510   DT_RETURN_MARK(NewStringUTF, jstring, (const jstring&)ret);
2511 
2512   oop result = java_lang_String::create_oop_from_str((char*) bytes, CHECK_NULL);
2513   ret = (jstring) JNIHandles::make_local(env, result);
2514   return ret;
2515 JNI_END
2516 
2517 
2518 JNI_ENTRY(jsize, jni_GetStringUTFLength(JNIEnv *env, jstring string))
2519   JNIWrapper("GetStringUTFLength");
2520  HOTSPOT_JNI_GETSTRINGUTFLENGTH_ENTRY(env, string);
2521   jsize ret = 0;
2522   oop java_string = JNIHandles::resolve_non_null(string);
2523   if (java_lang_String::value(java_string) != NULL) {
2524     ret = java_lang_String::utf8_length(java_string);
2525   }
2526   HOTSPOT_JNI_GETSTRINGUTFLENGTH_RETURN(ret);
2527   return ret;
2528 JNI_END
2529 
2530 
2531 JNI_ENTRY(const char*, jni_GetStringUTFChars(JNIEnv *env, jstring string, jboolean *isCopy))
2532   JNIWrapper("GetStringUTFChars");
2533  HOTSPOT_JNI_GETSTRINGUTFCHARS_ENTRY(env, string, (uintptr_t *) isCopy);
2534   char* result = NULL;
2535   oop java_string = JNIHandles::resolve_non_null(string);
2536   if (java_lang_String::value(java_string) != NULL) {
2537     size_t length = java_lang_String::utf8_length(java_string);
2538     /* JNI Specification states return NULL on OOM */
2539     result = AllocateHeap(length + 1, mtInternal, 0, AllocFailStrategy::RETURN_NULL);
2540     if (result != NULL) {
2541       java_lang_String::as_utf8_string(java_string, result, (int) length + 1);
2542       if (isCopy != NULL) {
2543         *isCopy = JNI_TRUE;
2544       }
2545     }
2546   }
2547  HOTSPOT_JNI_GETSTRINGUTFCHARS_RETURN(result);
2548   return result;
2549 JNI_END
2550 
2551 
2552 JNI_LEAF(void, jni_ReleaseStringUTFChars(JNIEnv *env, jstring str, const char *chars))
2553   JNIWrapper("ReleaseStringUTFChars");
2554  HOTSPOT_JNI_RELEASESTRINGUTFCHARS_ENTRY(env, str, (char *) chars);
2555   if (chars != NULL) {
2556     FreeHeap((char*) chars);
2557   }
2558 HOTSPOT_JNI_RELEASESTRINGUTFCHARS_RETURN();
2559 JNI_END
2560 
2561 
2562 JNI_QUICK_ENTRY(jsize, jni_GetArrayLength(JNIEnv *env, jarray array))
2563   JNIWrapper("GetArrayLength");
2564  HOTSPOT_JNI_GETARRAYLENGTH_ENTRY(env, array);
2565   arrayOop a = arrayOop(JNIHandles::resolve_non_null(array));
2566   assert(a->is_array(), "must be array");
2567   jsize ret = a->length();
2568  HOTSPOT_JNI_GETARRAYLENGTH_RETURN(ret);
2569   return ret;
2570 JNI_END
2571 
2572 
2573 //
2574 // Object Array Operations
2575 //
2576 
2577 DT_RETURN_MARK_DECL(NewObjectArray, jobjectArray
2578                     , HOTSPOT_JNI_NEWOBJECTARRAY_RETURN(_ret_ref));
2579 
2580 JNI_ENTRY(jobjectArray, jni_NewObjectArray(JNIEnv *env, jsize length, jclass elementClass, jobject initialElement))
2581   JNIWrapper("NewObjectArray");
2582  HOTSPOT_JNI_NEWOBJECTARRAY_ENTRY(env, length, elementClass, initialElement);
2583   jobjectArray ret = NULL;
2584   DT_RETURN_MARK(NewObjectArray, jobjectArray, (const jobjectArray&)ret);
2585   Klass* ek = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(elementClass));
2586   Klass* ak = ek->array_klass(CHECK_NULL);
2587   ObjArrayKlass::cast(ak)->initialize(CHECK_NULL);
2588   objArrayOop result = ObjArrayKlass::cast(ak)->allocate(length, CHECK_NULL);
2589   oop initial_value = JNIHandles::resolve(initialElement);
2590   if (initial_value != NULL) {  // array already initialized with NULL
2591     for (int index = 0; index < length; index++) {
2592       result->obj_at_put(index, initial_value);
2593     }
2594   }
2595   ret = (jobjectArray) JNIHandles::make_local(env, result);
2596   return ret;
2597 JNI_END
2598 
2599 DT_RETURN_MARK_DECL(GetObjectArrayElement, jobject
2600                     , HOTSPOT_JNI_GETOBJECTARRAYELEMENT_RETURN(_ret_ref));
2601 
2602 JNI_ENTRY(jobject, jni_GetObjectArrayElement(JNIEnv *env, jobjectArray array, jsize index))
2603   JNIWrapper("GetObjectArrayElement");
2604  HOTSPOT_JNI_GETOBJECTARRAYELEMENT_ENTRY(env, array, index);
2605   jobject ret = NULL;
2606   DT_RETURN_MARK(GetObjectArrayElement, jobject, (const jobject&)ret);
2607   objArrayOop a = objArrayOop(JNIHandles::resolve_non_null(array));
2608   if (a->is_within_bounds(index)) {
2609     ret = JNIHandles::make_local(env, a->obj_at(index));
2610     return ret;
2611   } else {
2612     char buf[jintAsStringSize];
2613     sprintf(buf, "%d", index);
2614     THROW_MSG_0(vmSymbols::java_lang_ArrayIndexOutOfBoundsException(), buf);
2615   }
2616 JNI_END
2617 
2618 DT_VOID_RETURN_MARK_DECL(SetObjectArrayElement
2619                          , HOTSPOT_JNI_SETOBJECTARRAYELEMENT_RETURN());
2620 
2621 JNI_ENTRY(void, jni_SetObjectArrayElement(JNIEnv *env, jobjectArray array, jsize index, jobject value))
2622   JNIWrapper("SetObjectArrayElement");
2623  HOTSPOT_JNI_SETOBJECTARRAYELEMENT_ENTRY(env, array, index, value);
2624   DT_VOID_RETURN_MARK(SetObjectArrayElement);
2625 
2626   objArrayOop a = objArrayOop(JNIHandles::resolve_non_null(array));
2627   oop v = JNIHandles::resolve(value);
2628   if (a->is_within_bounds(index)) {
2629     if (v == NULL || v->is_a(ObjArrayKlass::cast(a->klass())->element_klass())) {
2630       a->obj_at_put(index, v);
2631     } else {
2632       THROW(vmSymbols::java_lang_ArrayStoreException());
2633     }
2634   } else {
2635     char buf[jintAsStringSize];
2636     sprintf(buf, "%d", index);
2637     THROW_MSG(vmSymbols::java_lang_ArrayIndexOutOfBoundsException(), buf);
2638   }
2639 JNI_END
2640 
2641 
2642 
2643 #define DEFINE_NEWSCALARARRAY(Return,Allocator,Result \
2644                               ,EntryProbe,ReturnProbe)  \
2645 \
2646   DT_RETURN_MARK_DECL(New##Result##Array, Return \
2647                       , ReturnProbe); \
2648 \
2649 JNI_ENTRY(Return, \
2650           jni_New##Result##Array(JNIEnv *env, jsize len)) \
2651   JNIWrapper("New" XSTR(Result) "Array"); \
2652   EntryProbe; \
2653   Return ret = NULL;\
2654   DT_RETURN_MARK(New##Result##Array, Return, (const Return&)ret);\
2655 \
2656   oop obj= oopFactory::Allocator(len, CHECK_0); \
2657   ret = (Return) JNIHandles::make_local(env, obj); \
2658   return ret;\
2659 JNI_END
2660 
2661 DEFINE_NEWSCALARARRAY(jbooleanArray, new_boolArray,   Boolean,
2662                       HOTSPOT_JNI_NEWBOOLEANARRAY_ENTRY(env, len),
2663                       HOTSPOT_JNI_NEWBOOLEANARRAY_RETURN(_ret_ref))
2664 DEFINE_NEWSCALARARRAY(jbyteArray,    new_byteArray,   Byte,
2665                       HOTSPOT_JNI_NEWBYTEARRAY_ENTRY(env, len),
2666                       HOTSPOT_JNI_NEWBYTEARRAY_RETURN(_ret_ref))
2667 DEFINE_NEWSCALARARRAY(jshortArray,   new_shortArray,  Short,
2668                       HOTSPOT_JNI_NEWSHORTARRAY_ENTRY(env, len),
2669                       HOTSPOT_JNI_NEWSHORTARRAY_RETURN(_ret_ref))
2670 DEFINE_NEWSCALARARRAY(jcharArray,    new_charArray,   Char,
2671                       HOTSPOT_JNI_NEWCHARARRAY_ENTRY(env, len),
2672                       HOTSPOT_JNI_NEWCHARARRAY_RETURN(_ret_ref))
2673 DEFINE_NEWSCALARARRAY(jintArray,     new_intArray,    Int,
2674                       HOTSPOT_JNI_NEWINTARRAY_ENTRY(env, len),
2675                       HOTSPOT_JNI_NEWINTARRAY_RETURN(_ret_ref))
2676 DEFINE_NEWSCALARARRAY(jlongArray,    new_longArray,   Long,
2677                       HOTSPOT_JNI_NEWLONGARRAY_ENTRY(env, len),
2678                       HOTSPOT_JNI_NEWLONGARRAY_RETURN(_ret_ref))
2679 DEFINE_NEWSCALARARRAY(jfloatArray,   new_singleArray, Float,
2680                       HOTSPOT_JNI_NEWFLOATARRAY_ENTRY(env, len),
2681                       HOTSPOT_JNI_NEWFLOATARRAY_RETURN(_ret_ref))
2682 DEFINE_NEWSCALARARRAY(jdoubleArray,  new_doubleArray, Double,
2683                       HOTSPOT_JNI_NEWDOUBLEARRAY_ENTRY(env, len),
2684                       HOTSPOT_JNI_NEWDOUBLEARRAY_RETURN(_ret_ref))
2685 
2686 // Return an address which will fault if the caller writes to it.
2687 
2688 static char* get_bad_address() {
2689   static char* bad_address = NULL;
2690   if (bad_address == NULL) {
2691     size_t size = os::vm_allocation_granularity();
2692     bad_address = os::reserve_memory(size);
2693     if (bad_address != NULL) {
2694       os::protect_memory(bad_address, size, os::MEM_PROT_READ,
2695                          /*is_committed*/false);
2696       MemTracker::record_virtual_memory_type((void*)bad_address, mtInternal);
2697     }
2698   }
2699   return bad_address;
2700 }
2701 
2702 
2703 
2704 #define DEFINE_GETSCALARARRAYELEMENTS(ElementTag,ElementType,Result, Tag \
2705                                       , EntryProbe, ReturnProbe) \
2706 \
2707 JNI_QUICK_ENTRY(ElementType*, \
2708           jni_Get##Result##ArrayElements(JNIEnv *env, ElementType##Array array, jboolean *isCopy)) \
2709   JNIWrapper("Get" XSTR(Result) "ArrayElements"); \
2710   EntryProbe; \
2711   /* allocate an chunk of memory in c land */ \
2712   typeArrayOop a = typeArrayOop(JNIHandles::resolve_non_null(array)); \
2713   ElementType* result; \
2714   int len = a->length(); \
2715   if (len == 0) { \
2716     /* Empty array: legal but useless, can't return NULL. \
2717      * Return a pointer to something useless. \
2718      * Avoid asserts in typeArrayOop. */ \
2719     result = (ElementType*)get_bad_address(); \
2720   } else { \
2721     /* JNI Specification states return NULL on OOM */                    \
2722     result = NEW_C_HEAP_ARRAY_RETURN_NULL(ElementType, len, mtInternal); \
2723     if (result != NULL) {                                                \
2724       /* copy the array to the c chunk */                                \
2725       memcpy(result, a->Tag##_at_addr(0), sizeof(ElementType)*len);      \

2726       if (isCopy) {                                                      \
2727         *isCopy = JNI_TRUE;                                              \
2728       }                                                                  \
2729     }                                                                    \
2730   } \
2731   ReturnProbe; \
2732   return result; \
2733 JNI_END
2734 
2735 DEFINE_GETSCALARARRAYELEMENTS(T_BOOLEAN, jboolean, Boolean, bool
2736                               , HOTSPOT_JNI_GETBOOLEANARRAYELEMENTS_ENTRY(env, array, (uintptr_t *) isCopy),
2737                               HOTSPOT_JNI_GETBOOLEANARRAYELEMENTS_RETURN((uintptr_t*)result))
2738 DEFINE_GETSCALARARRAYELEMENTS(T_BYTE,    jbyte,    Byte,    byte
2739                               , HOTSPOT_JNI_GETBYTEARRAYELEMENTS_ENTRY(env, array, (uintptr_t *) isCopy),
2740                               HOTSPOT_JNI_GETBYTEARRAYELEMENTS_RETURN((char*)result))
2741 DEFINE_GETSCALARARRAYELEMENTS(T_SHORT,   jshort,   Short,   short
2742                               , HOTSPOT_JNI_GETSHORTARRAYELEMENTS_ENTRY(env, (uint16_t*) array, (uintptr_t *) isCopy),
2743                               HOTSPOT_JNI_GETSHORTARRAYELEMENTS_RETURN((uint16_t*)result))
2744 DEFINE_GETSCALARARRAYELEMENTS(T_CHAR,    jchar,    Char,    char
2745                               , HOTSPOT_JNI_GETCHARARRAYELEMENTS_ENTRY(env, (uint16_t*) array, (uintptr_t *) isCopy),
2746                               HOTSPOT_JNI_GETCHARARRAYELEMENTS_RETURN(result))
2747 DEFINE_GETSCALARARRAYELEMENTS(T_INT,     jint,     Int,     int
2748                               , HOTSPOT_JNI_GETINTARRAYELEMENTS_ENTRY(env, array, (uintptr_t *) isCopy),
2749                               HOTSPOT_JNI_GETINTARRAYELEMENTS_RETURN((uint32_t*)result))
2750 DEFINE_GETSCALARARRAYELEMENTS(T_LONG,    jlong,    Long,    long
2751                               , HOTSPOT_JNI_GETLONGARRAYELEMENTS_ENTRY(env, array, (uintptr_t *) isCopy),
2752                               HOTSPOT_JNI_GETLONGARRAYELEMENTS_RETURN(((uintptr_t*)result)))
2753 // Float and double probes don't return value because dtrace doesn't currently support it
2754 DEFINE_GETSCALARARRAYELEMENTS(T_FLOAT,   jfloat,   Float,   float
2755                               , HOTSPOT_JNI_GETFLOATARRAYELEMENTS_ENTRY(env, array, (uintptr_t *) isCopy),
2756                               HOTSPOT_JNI_GETFLOATARRAYELEMENTS_RETURN(result))
2757 DEFINE_GETSCALARARRAYELEMENTS(T_DOUBLE,  jdouble,  Double,  double
2758                               , HOTSPOT_JNI_GETDOUBLEARRAYELEMENTS_ENTRY(env, array, (uintptr_t *) isCopy),
2759                               HOTSPOT_JNI_GETDOUBLEARRAYELEMENTS_RETURN(result))
2760 
2761 
2762 #define DEFINE_RELEASESCALARARRAYELEMENTS(ElementTag,ElementType,Result,Tag \
2763                                           , EntryProbe, ReturnProbe);\
2764 \
2765 JNI_QUICK_ENTRY(void, \
2766           jni_Release##Result##ArrayElements(JNIEnv *env, ElementType##Array array, \
2767                                              ElementType *buf, jint mode)) \
2768   JNIWrapper("Release" XSTR(Result) "ArrayElements"); \
2769   EntryProbe; \
2770   typeArrayOop a = typeArrayOop(JNIHandles::resolve_non_null(array)); \
2771   int len = a->length(); \
2772   if (len != 0) {   /* Empty array:  nothing to free or copy. */  \
2773     if ((mode == 0) || (mode == JNI_COMMIT)) { \
2774       memcpy(a->Tag##_at_addr(0), buf, sizeof(ElementType)*len); \
2775     } \
2776     if ((mode == 0) || (mode == JNI_ABORT)) { \
2777       FreeHeap(buf); \
2778     } \
2779   } \
2780   ReturnProbe; \
2781 JNI_END
2782 
2783 DEFINE_RELEASESCALARARRAYELEMENTS(T_BOOLEAN, jboolean, Boolean, bool
2784                                   , HOTSPOT_JNI_RELEASEBOOLEANARRAYELEMENTS_ENTRY(env, array, (uintptr_t *) buf, mode),
2785                                   HOTSPOT_JNI_RELEASEBOOLEANARRAYELEMENTS_RETURN())
2786 DEFINE_RELEASESCALARARRAYELEMENTS(T_BYTE,    jbyte,    Byte,    byte
2787                                   , HOTSPOT_JNI_RELEASEBYTEARRAYELEMENTS_ENTRY(env, array, (char *) buf, mode),
2788                                   HOTSPOT_JNI_RELEASEBYTEARRAYELEMENTS_RETURN())
2789 DEFINE_RELEASESCALARARRAYELEMENTS(T_SHORT,   jshort,   Short,   short
2790                                   ,  HOTSPOT_JNI_RELEASESHORTARRAYELEMENTS_ENTRY(env, array, (uint16_t *) buf, mode),
2791                                   HOTSPOT_JNI_RELEASESHORTARRAYELEMENTS_RETURN())
2792 DEFINE_RELEASESCALARARRAYELEMENTS(T_CHAR,    jchar,    Char,    char
2793                                   ,  HOTSPOT_JNI_RELEASECHARARRAYELEMENTS_ENTRY(env, array, (uint16_t *) buf, mode),
2794                                   HOTSPOT_JNI_RELEASECHARARRAYELEMENTS_RETURN())
2795 DEFINE_RELEASESCALARARRAYELEMENTS(T_INT,     jint,     Int,     int
2796                                   , HOTSPOT_JNI_RELEASEINTARRAYELEMENTS_ENTRY(env, array, (uint32_t *) buf, mode),
2797                                   HOTSPOT_JNI_RELEASEINTARRAYELEMENTS_RETURN())
2798 DEFINE_RELEASESCALARARRAYELEMENTS(T_LONG,    jlong,    Long,    long
2799                                   , HOTSPOT_JNI_RELEASELONGARRAYELEMENTS_ENTRY(env, array, (uintptr_t *) buf, mode),
2800                                   HOTSPOT_JNI_RELEASELONGARRAYELEMENTS_RETURN())
2801 DEFINE_RELEASESCALARARRAYELEMENTS(T_FLOAT,   jfloat,   Float,   float
2802                                   , HOTSPOT_JNI_RELEASEFLOATARRAYELEMENTS_ENTRY(env, array, (float *) buf, mode),
2803                                   HOTSPOT_JNI_RELEASEFLOATARRAYELEMENTS_RETURN())
2804 DEFINE_RELEASESCALARARRAYELEMENTS(T_DOUBLE,  jdouble,  Double,  double
2805                                   , HOTSPOT_JNI_RELEASEDOUBLEARRAYELEMENTS_ENTRY(env, array, (double *) buf, mode),
2806                                   HOTSPOT_JNI_RELEASEDOUBLEARRAYELEMENTS_RETURN())
2807 
2808 
2809 #define DEFINE_GETSCALARARRAYREGION(ElementTag,ElementType,Result, Tag \
2810                                     , EntryProbe, ReturnProbe); \
2811   DT_VOID_RETURN_MARK_DECL(Get##Result##ArrayRegion \
2812                            , ReturnProbe); \
2813 \
2814 JNI_ENTRY(void, \
2815 jni_Get##Result##ArrayRegion(JNIEnv *env, ElementType##Array array, jsize start, \
2816              jsize len, ElementType *buf)) \
2817   JNIWrapper("Get" XSTR(Result) "ArrayRegion"); \
2818   EntryProbe; \
2819   DT_VOID_RETURN_MARK(Get##Result##ArrayRegion); \
2820   typeArrayOop src = typeArrayOop(JNIHandles::resolve_non_null(array)); \
2821   if (start < 0 || len < 0 || (start > src->length() - len)) { \
2822     THROW(vmSymbols::java_lang_ArrayIndexOutOfBoundsException()); \
2823   } else { \
2824     if (len > 0) { \
2825       int sc = TypeArrayKlass::cast(src->klass())->log2_element_size(); \
2826       memcpy((u_char*) buf, \
2827              (u_char*) src->Tag##_at_addr(start), \
2828              len << sc);                          \
2829     } \
2830   } \
2831 JNI_END
2832 
2833 DEFINE_GETSCALARARRAYREGION(T_BOOLEAN, jboolean,Boolean, bool
2834                             , HOTSPOT_JNI_GETBOOLEANARRAYREGION_ENTRY(env, array, start, len, (uintptr_t *) buf),
2835                             HOTSPOT_JNI_GETBOOLEANARRAYREGION_RETURN());
2836 DEFINE_GETSCALARARRAYREGION(T_BYTE,    jbyte,   Byte,    byte
2837                             ,  HOTSPOT_JNI_GETBYTEARRAYREGION_ENTRY(env, array, start, len, (char *) buf),
2838                             HOTSPOT_JNI_GETBYTEARRAYREGION_RETURN());
2839 DEFINE_GETSCALARARRAYREGION(T_SHORT,   jshort,  Short,   short
2840                             , HOTSPOT_JNI_GETSHORTARRAYREGION_ENTRY(env, array, start, len, (uint16_t *) buf),
2841                             HOTSPOT_JNI_GETSHORTARRAYREGION_RETURN());
2842 DEFINE_GETSCALARARRAYREGION(T_CHAR,    jchar,   Char,    char
2843                             ,  HOTSPOT_JNI_GETCHARARRAYREGION_ENTRY(env, array, start, len, (uint16_t*) buf),
2844                             HOTSPOT_JNI_GETCHARARRAYREGION_RETURN());
2845 DEFINE_GETSCALARARRAYREGION(T_INT,     jint,    Int,     int
2846                             , HOTSPOT_JNI_GETINTARRAYREGION_ENTRY(env, array, start, len, (uint32_t*) buf),
2847                             HOTSPOT_JNI_GETINTARRAYREGION_RETURN());
2848 DEFINE_GETSCALARARRAYREGION(T_LONG,    jlong,   Long,    long
2849                             ,  HOTSPOT_JNI_GETLONGARRAYREGION_ENTRY(env, array, start, len, (uintptr_t *) buf),
2850                             HOTSPOT_JNI_GETLONGARRAYREGION_RETURN());
2851 DEFINE_GETSCALARARRAYREGION(T_FLOAT,   jfloat,  Float,   float
2852                             , HOTSPOT_JNI_GETFLOATARRAYREGION_ENTRY(env, array, start, len, (float *) buf),
2853                             HOTSPOT_JNI_GETFLOATARRAYREGION_RETURN());
2854 DEFINE_GETSCALARARRAYREGION(T_DOUBLE,  jdouble, Double,  double
2855                             , HOTSPOT_JNI_GETDOUBLEARRAYREGION_ENTRY(env, array, start, len, (double *) buf),
2856                             HOTSPOT_JNI_GETDOUBLEARRAYREGION_RETURN());
2857 
2858 
2859 #define DEFINE_SETSCALARARRAYREGION(ElementTag,ElementType,Result, Tag \
2860                                     , EntryProbe, ReturnProbe); \
2861   DT_VOID_RETURN_MARK_DECL(Set##Result##ArrayRegion \
2862                            ,ReturnProbe);           \
2863 \
2864 JNI_ENTRY(void, \
2865 jni_Set##Result##ArrayRegion(JNIEnv *env, ElementType##Array array, jsize start, \
2866              jsize len, const ElementType *buf)) \
2867   JNIWrapper("Set" XSTR(Result) "ArrayRegion"); \
2868   EntryProbe; \
2869   DT_VOID_RETURN_MARK(Set##Result##ArrayRegion); \
2870   typeArrayOop dst = typeArrayOop(JNIHandles::resolve_non_null(array)); \
2871   if (start < 0 || len < 0 || (start > dst->length() - len)) { \
2872     THROW(vmSymbols::java_lang_ArrayIndexOutOfBoundsException()); \
2873   } else { \
2874     if (len > 0) { \
2875       int sc = TypeArrayKlass::cast(dst->klass())->log2_element_size(); \
2876       memcpy((u_char*) dst->Tag##_at_addr(start), \
2877              (u_char*) buf, \
2878              len << sc);    \
2879     } \
2880   } \
2881 JNI_END
2882 
2883 DEFINE_SETSCALARARRAYREGION(T_BOOLEAN, jboolean, Boolean, bool
2884                             , HOTSPOT_JNI_SETBOOLEANARRAYREGION_ENTRY(env, array, start, len, (uintptr_t *)buf),
2885                             HOTSPOT_JNI_SETBOOLEANARRAYREGION_RETURN())
2886 DEFINE_SETSCALARARRAYREGION(T_BYTE,    jbyte,    Byte,    byte
2887                             , HOTSPOT_JNI_SETBYTEARRAYREGION_ENTRY(env, array, start, len, (char *) buf),
2888                             HOTSPOT_JNI_SETBYTEARRAYREGION_RETURN())
2889 DEFINE_SETSCALARARRAYREGION(T_SHORT,   jshort,   Short,   short
2890                             , HOTSPOT_JNI_SETSHORTARRAYREGION_ENTRY(env, array, start, len, (uint16_t *) buf),
2891                             HOTSPOT_JNI_SETSHORTARRAYREGION_RETURN())
2892 DEFINE_SETSCALARARRAYREGION(T_CHAR,    jchar,    Char,    char
2893                             , HOTSPOT_JNI_SETCHARARRAYREGION_ENTRY(env, array, start, len, (uint16_t *) buf),
2894                             HOTSPOT_JNI_SETCHARARRAYREGION_RETURN())
2895 DEFINE_SETSCALARARRAYREGION(T_INT,     jint,     Int,     int
2896                             , HOTSPOT_JNI_SETINTARRAYREGION_ENTRY(env, array, start, len, (uint32_t *) buf),
2897                             HOTSPOT_JNI_SETINTARRAYREGION_RETURN())
2898 DEFINE_SETSCALARARRAYREGION(T_LONG,    jlong,    Long,    long
2899                             , HOTSPOT_JNI_SETLONGARRAYREGION_ENTRY(env, array, start, len, (uintptr_t *) buf),
2900                             HOTSPOT_JNI_SETLONGARRAYREGION_RETURN())
2901 DEFINE_SETSCALARARRAYREGION(T_FLOAT,   jfloat,   Float,   float
2902                             , HOTSPOT_JNI_SETFLOATARRAYREGION_ENTRY(env, array, start, len, (float *) buf),
2903                             HOTSPOT_JNI_SETFLOATARRAYREGION_RETURN())
2904 DEFINE_SETSCALARARRAYREGION(T_DOUBLE,  jdouble,  Double,  double
2905                             , HOTSPOT_JNI_SETDOUBLEARRAYREGION_ENTRY(env, array, start, len, (double *) buf),
2906                             HOTSPOT_JNI_SETDOUBLEARRAYREGION_RETURN())
2907 
2908 
2909 //
2910 // Interception of natives
2911 //
2912 
2913 // The RegisterNatives call being attempted tried to register with a method that
2914 // is not native.  Ask JVM TI what prefixes have been specified.  Then check
2915 // to see if the native method is now wrapped with the prefixes.  See the
2916 // SetNativeMethodPrefix(es) functions in the JVM TI Spec for details.
2917 static Method* find_prefixed_native(Klass* k, Symbol* name, Symbol* signature, TRAPS) {
2918 #if INCLUDE_JVMTI
2919   ResourceMark rm(THREAD);
2920   Method* method;
2921   int name_len = name->utf8_length();
2922   char* name_str = name->as_utf8();
2923   int prefix_count;
2924   char** prefixes = JvmtiExport::get_all_native_method_prefixes(&prefix_count);
2925   for (int i = 0; i < prefix_count; i++) {
2926     char* prefix = prefixes[i];
2927     int prefix_len = (int)strlen(prefix);
2928 
2929     // try adding this prefix to the method name and see if it matches another method name
2930     int trial_len = name_len + prefix_len;
2931     char* trial_name_str = NEW_RESOURCE_ARRAY(char, trial_len + 1);
2932     strcpy(trial_name_str, prefix);
2933     strcat(trial_name_str, name_str);
2934     TempNewSymbol trial_name = SymbolTable::probe(trial_name_str, trial_len);
2935     if (trial_name == NULL) {
2936       continue; // no such symbol, so this prefix wasn't used, try the next prefix
2937     }
2938     method = k->lookup_method(trial_name, signature);
2939     if (method == NULL) {
2940       continue; // signature doesn't match, try the next prefix
2941     }
2942     if (method->is_native()) {
2943       method->set_is_prefixed_native();
2944       return method; // wahoo, we found a prefixed version of the method, return it
2945     }
2946     // found as non-native, so prefix is good, add it, probably just need more prefixes
2947     name_len = trial_len;
2948     name_str = trial_name_str;
2949   }
2950 #endif // INCLUDE_JVMTI
2951   return NULL; // not found
2952 }
2953 
2954 static bool register_native(Klass* k, Symbol* name, Symbol* signature, address entry, TRAPS) {
2955   Method* method = k->lookup_method(name, signature);
2956   if (method == NULL) {
2957     ResourceMark rm;
2958     stringStream st;
2959     st.print("Method %s name or signature does not match",
2960              Method::name_and_sig_as_C_string(k, name, signature));
2961     THROW_MSG_(vmSymbols::java_lang_NoSuchMethodError(), st.as_string(), false);
2962   }
2963   if (!method->is_native()) {
2964     // trying to register to a non-native method, see if a JVM TI agent has added prefix(es)
2965     method = find_prefixed_native(k, name, signature, THREAD);
2966     if (method == NULL) {
2967       ResourceMark rm;
2968       stringStream st;
2969       st.print("Method %s is not declared as native",
2970                Method::name_and_sig_as_C_string(k, name, signature));
2971       THROW_MSG_(vmSymbols::java_lang_NoSuchMethodError(), st.as_string(), false);
2972     }
2973   }
2974 
2975   if (entry != NULL) {
2976     method->set_native_function(entry,
2977       Method::native_bind_event_is_interesting);
2978   } else {
2979     method->clear_native_function();
2980   }
2981   if (PrintJNIResolving) {
2982     ResourceMark rm(THREAD);
2983     tty->print_cr("[Registering JNI native method %s.%s]",
2984       method->method_holder()->external_name(),
2985       method->name()->as_C_string());
2986   }
2987   return true;
2988 }
2989 
2990 DT_RETURN_MARK_DECL(RegisterNatives, jint
2991                     , HOTSPOT_JNI_REGISTERNATIVES_RETURN(_ret_ref));
2992 
2993 JNI_ENTRY(jint, jni_RegisterNatives(JNIEnv *env, jclass clazz,
2994                                     const JNINativeMethod *methods,
2995                                     jint nMethods))
2996   JNIWrapper("RegisterNatives");
2997   HOTSPOT_JNI_REGISTERNATIVES_ENTRY(env, clazz, (void *) methods, nMethods);
2998   jint ret = 0;
2999   DT_RETURN_MARK(RegisterNatives, jint, (const jint&)ret);
3000 
3001   Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(clazz));
3002 
3003   for (int index = 0; index < nMethods; index++) {
3004     const char* meth_name = methods[index].name;
3005     const char* meth_sig = methods[index].signature;
3006     int meth_name_len = (int)strlen(meth_name);
3007 
3008     // The class should have been loaded (we have an instance of the class
3009     // passed in) so the method and signature should already be in the symbol
3010     // table.  If they're not there, the method doesn't exist.
3011     TempNewSymbol  name = SymbolTable::probe(meth_name, meth_name_len);
3012     TempNewSymbol  signature = SymbolTable::probe(meth_sig, (int)strlen(meth_sig));
3013 
3014     if (name == NULL || signature == NULL) {
3015       ResourceMark rm;
3016       stringStream st;
3017       st.print("Method %s.%s%s not found", k->external_name(), meth_name, meth_sig);
3018       // Must return negative value on failure
3019       THROW_MSG_(vmSymbols::java_lang_NoSuchMethodError(), st.as_string(), -1);
3020     }
3021 
3022     bool res = register_native(k, name, signature,
3023                                (address) methods[index].fnPtr, THREAD);
3024     if (!res) {
3025       ret = -1;
3026       break;
3027     }
3028   }
3029   return ret;
3030 JNI_END
3031 
3032 
3033 JNI_ENTRY(jint, jni_UnregisterNatives(JNIEnv *env, jclass clazz))
3034   JNIWrapper("UnregisterNatives");
3035  HOTSPOT_JNI_UNREGISTERNATIVES_ENTRY(env, clazz);
3036   Klass* k   = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(clazz));
3037   //%note jni_2
3038   if (k->is_instance_klass()) {
3039     for (int index = 0; index < InstanceKlass::cast(k)->methods()->length(); index++) {
3040       Method* m = InstanceKlass::cast(k)->methods()->at(index);
3041       if (m->is_native()) {
3042         m->clear_native_function();
3043         m->set_signature_handler(NULL);
3044       }
3045     }
3046   }
3047  HOTSPOT_JNI_UNREGISTERNATIVES_RETURN(0);
3048   return 0;
3049 JNI_END
3050 
3051 //
3052 // Monitor functions
3053 //
3054 
3055 DT_RETURN_MARK_DECL(MonitorEnter, jint
3056                     , HOTSPOT_JNI_MONITORENTER_RETURN(_ret_ref));
3057 
3058 JNI_ENTRY(jint, jni_MonitorEnter(JNIEnv *env, jobject jobj))
3059  HOTSPOT_JNI_MONITORENTER_ENTRY(env, jobj);
3060   jint ret = JNI_ERR;
3061   DT_RETURN_MARK(MonitorEnter, jint, (const jint&)ret);
3062 
3063   // If the object is null, we can't do anything with it
3064   if (jobj == NULL) {
3065     THROW_(vmSymbols::java_lang_NullPointerException(), JNI_ERR);
3066   }
3067 
3068   Handle obj(thread, JNIHandles::resolve_non_null(jobj));
3069   ObjectSynchronizer::jni_enter(obj, CHECK_(JNI_ERR));
3070   ret = JNI_OK;
3071   return ret;
3072 JNI_END
3073 
3074 DT_RETURN_MARK_DECL(MonitorExit, jint
3075                     , HOTSPOT_JNI_MONITOREXIT_RETURN(_ret_ref));
3076 
3077 JNI_ENTRY(jint, jni_MonitorExit(JNIEnv *env, jobject jobj))
3078  HOTSPOT_JNI_MONITOREXIT_ENTRY(env, jobj);
3079   jint ret = JNI_ERR;
3080   DT_RETURN_MARK(MonitorExit, jint, (const jint&)ret);
3081 
3082   // Don't do anything with a null object
3083   if (jobj == NULL) {
3084     THROW_(vmSymbols::java_lang_NullPointerException(), JNI_ERR);
3085   }
3086 
3087   Handle obj(THREAD, JNIHandles::resolve_non_null(jobj));
3088   ObjectSynchronizer::jni_exit(obj(), CHECK_(JNI_ERR));
3089 
3090   ret = JNI_OK;
3091   return ret;
3092 JNI_END
3093 
3094 //
3095 // Extensions
3096 //
3097 
3098 DT_VOID_RETURN_MARK_DECL(GetStringRegion
3099                          , HOTSPOT_JNI_GETSTRINGREGION_RETURN());
3100 
3101 JNI_ENTRY(void, jni_GetStringRegion(JNIEnv *env, jstring string, jsize start, jsize len, jchar *buf))
3102   JNIWrapper("GetStringRegion");
3103  HOTSPOT_JNI_GETSTRINGREGION_ENTRY(env, string, start, len, buf);
3104   DT_VOID_RETURN_MARK(GetStringRegion);
3105   oop s = JNIHandles::resolve_non_null(string);
3106   int s_len = java_lang_String::length(s);
3107   if (start < 0 || len < 0 || start > s_len - len) {
3108     THROW(vmSymbols::java_lang_StringIndexOutOfBoundsException());
3109   } else {
3110     if (len > 0) {
3111       typeArrayOop s_value = java_lang_String::value(s);
3112       bool is_latin1 = java_lang_String::is_latin1(s);
3113       if (!is_latin1) {
3114         memcpy(buf, s_value->char_at_addr(start), sizeof(jchar)*len);

3115       } else {
3116         for (int i = 0; i < len; i++) {
3117           buf[i] = ((jchar) s_value->byte_at(i + start)) & 0xff;
3118         }
3119       }
3120     }
3121   }
3122 JNI_END
3123 
3124 DT_VOID_RETURN_MARK_DECL(GetStringUTFRegion
3125                          , HOTSPOT_JNI_GETSTRINGUTFREGION_RETURN());
3126 
3127 JNI_ENTRY(void, jni_GetStringUTFRegion(JNIEnv *env, jstring string, jsize start, jsize len, char *buf))
3128   JNIWrapper("GetStringUTFRegion");
3129  HOTSPOT_JNI_GETSTRINGUTFREGION_ENTRY(env, string, start, len, buf);
3130   DT_VOID_RETURN_MARK(GetStringUTFRegion);
3131   oop s = JNIHandles::resolve_non_null(string);
3132   int s_len = java_lang_String::length(s);
3133   if (start < 0 || len < 0 || start > s_len - len) {
3134     THROW(vmSymbols::java_lang_StringIndexOutOfBoundsException());
3135   } else {
3136     //%note jni_7
3137     if (len > 0) {
3138       // Assume the buffer is large enough as the JNI spec. does not require user error checking
3139       java_lang_String::as_utf8_string(s, start, len, buf, INT_MAX);
3140       // as_utf8_string null-terminates the result string
3141     } else {
3142       // JDK null-terminates the buffer even in len is zero
3143       if (buf != NULL) {
3144         buf[0] = 0;
3145       }
3146     }
3147   }
3148 JNI_END
3149 
3150 static oop lock_gc_or_pin_object(JavaThread* thread, jobject obj) {
3151   if (Universe::heap()->supports_object_pinning()) {
3152     const oop o = JNIHandles::resolve_non_null(obj);
3153     return Universe::heap()->pin_object(thread, o);
3154   } else {
3155     GCLocker::lock_critical(thread);
3156     return JNIHandles::resolve_non_null(obj);
3157   }
3158 }
3159 
3160 static void unlock_gc_or_unpin_object(JavaThread* thread, jobject obj) {
3161   if (Universe::heap()->supports_object_pinning()) {
3162     const oop o = JNIHandles::resolve_non_null(obj);
3163     return Universe::heap()->unpin_object(thread, o);
3164   } else {
3165     GCLocker::unlock_critical(thread);
3166   }
3167 }
3168 
3169 JNI_ENTRY(void*, jni_GetPrimitiveArrayCritical(JNIEnv *env, jarray array, jboolean *isCopy))
3170   JNIWrapper("GetPrimitiveArrayCritical");
3171  HOTSPOT_JNI_GETPRIMITIVEARRAYCRITICAL_ENTRY(env, array, (uintptr_t *) isCopy);
3172   if (isCopy != NULL) {
3173     *isCopy = JNI_FALSE;
3174   }
3175   oop a = lock_gc_or_pin_object(thread, array);
3176   assert(a->is_array(), "just checking");
3177   BasicType type;
3178   if (a->is_objArray()) {
3179     type = T_OBJECT;
3180   } else {
3181     type = TypeArrayKlass::cast(a->klass())->element_type();
3182   }
3183   void* ret = arrayOop(a)->base(type);
3184  HOTSPOT_JNI_GETPRIMITIVEARRAYCRITICAL_RETURN(ret);
3185   return ret;
3186 JNI_END
3187 
3188 
3189 JNI_ENTRY(void, jni_ReleasePrimitiveArrayCritical(JNIEnv *env, jarray array, void *carray, jint mode))
3190   JNIWrapper("ReleasePrimitiveArrayCritical");
3191   HOTSPOT_JNI_RELEASEPRIMITIVEARRAYCRITICAL_ENTRY(env, array, carray, mode);
3192   unlock_gc_or_unpin_object(thread, array);
3193 HOTSPOT_JNI_RELEASEPRIMITIVEARRAYCRITICAL_RETURN();
3194 JNI_END
3195 
3196 
3197 JNI_ENTRY(const jchar*, jni_GetStringCritical(JNIEnv *env, jstring string, jboolean *isCopy))
3198   JNIWrapper("GetStringCritical");
3199   HOTSPOT_JNI_GETSTRINGCRITICAL_ENTRY(env, string, (uintptr_t *) isCopy);
3200   oop s = lock_gc_or_pin_object(thread, string);
3201   typeArrayOop s_value = java_lang_String::value(s);
3202   bool is_latin1 = java_lang_String::is_latin1(s);
3203   if (isCopy != NULL) {
3204     *isCopy = is_latin1 ? JNI_TRUE : JNI_FALSE;
3205   }
3206   jchar* ret;
3207   if (!is_latin1) {
3208     ret = (jchar*) s_value->base(T_CHAR);
3209   } else {
3210     // Inflate latin1 encoded string to UTF16
3211     int s_len = java_lang_String::length(s);
3212     ret = NEW_C_HEAP_ARRAY_RETURN_NULL(jchar, s_len + 1, mtInternal);  // add one for zero termination
3213     /* JNI Specification states return NULL on OOM */
3214     if (ret != NULL) {
3215       for (int i = 0; i < s_len; i++) {
3216         ret[i] = ((jchar) s_value->byte_at(i)) & 0xff;
3217       }
3218       ret[s_len] = 0;
3219     }
3220   }
3221  HOTSPOT_JNI_GETSTRINGCRITICAL_RETURN((uint16_t *) ret);
3222   return ret;
3223 JNI_END
3224 
3225 
3226 JNI_ENTRY(void, jni_ReleaseStringCritical(JNIEnv *env, jstring str, const jchar *chars))
3227   JNIWrapper("ReleaseStringCritical");
3228   HOTSPOT_JNI_RELEASESTRINGCRITICAL_ENTRY(env, str, (uint16_t *) chars);
3229   // The str and chars arguments are ignored for UTF16 strings
3230   oop s = JNIHandles::resolve_non_null(str);
3231   bool is_latin1 = java_lang_String::is_latin1(s);
3232   if (is_latin1) {
3233     // For latin1 string, free jchar array allocated by earlier call to GetStringCritical.
3234     // This assumes that ReleaseStringCritical bookends GetStringCritical.
3235     FREE_C_HEAP_ARRAY(jchar, chars);
3236   }
3237   unlock_gc_or_unpin_object(thread, str);
3238 HOTSPOT_JNI_RELEASESTRINGCRITICAL_RETURN();
3239 JNI_END
3240 
3241 
3242 JNI_ENTRY(jweak, jni_NewWeakGlobalRef(JNIEnv *env, jobject ref))
3243   JNIWrapper("jni_NewWeakGlobalRef");
3244  HOTSPOT_JNI_NEWWEAKGLOBALREF_ENTRY(env, ref);
3245   Handle ref_handle(thread, JNIHandles::resolve(ref));
3246   jweak ret = JNIHandles::make_weak_global(ref_handle);
3247  HOTSPOT_JNI_NEWWEAKGLOBALREF_RETURN(ret);
3248   return ret;
3249 JNI_END
3250 
3251 // Must be JNI_ENTRY (with HandleMark)
3252 JNI_ENTRY(void, jni_DeleteWeakGlobalRef(JNIEnv *env, jweak ref))
3253   JNIWrapper("jni_DeleteWeakGlobalRef");
3254   HOTSPOT_JNI_DELETEWEAKGLOBALREF_ENTRY(env, ref);
3255   JNIHandles::destroy_weak_global(ref);
3256   HOTSPOT_JNI_DELETEWEAKGLOBALREF_RETURN();
3257 JNI_END
3258 
3259 
3260 JNI_QUICK_ENTRY(jboolean, jni_ExceptionCheck(JNIEnv *env))
3261   JNIWrapper("jni_ExceptionCheck");
3262  HOTSPOT_JNI_EXCEPTIONCHECK_ENTRY(env);
3263   jni_check_async_exceptions(thread);
3264   jboolean ret = (thread->has_pending_exception()) ? JNI_TRUE : JNI_FALSE;
3265  HOTSPOT_JNI_EXCEPTIONCHECK_RETURN(ret);
3266   return ret;
3267 JNI_END
3268 
3269 
3270 // Initialization state for three routines below relating to
3271 // java.nio.DirectBuffers
3272 static          int directBufferSupportInitializeStarted = 0;
3273 static volatile int directBufferSupportInitializeEnded   = 0;
3274 static volatile int directBufferSupportInitializeFailed  = 0;
3275 static jclass    bufferClass                 = NULL;
3276 static jclass    directBufferClass           = NULL;
3277 static jclass    directByteBufferClass       = NULL;
3278 static jmethodID directByteBufferConstructor = NULL;
3279 static jfieldID  directBufferAddressField    = NULL;
3280 static jfieldID  bufferCapacityField         = NULL;
3281 
3282 static jclass lookupOne(JNIEnv* env, const char* name, TRAPS) {
3283   Handle loader;            // null (bootstrap) loader
3284   Handle protection_domain; // null protection domain
3285 
3286   TempNewSymbol sym = SymbolTable::new_symbol(name, CHECK_NULL);
3287   jclass result =  find_class_from_class_loader(env, sym, true, loader, protection_domain, true, CHECK_NULL);
3288 
3289   if (log_is_enabled(Debug, class, resolve) && result != NULL) {
3290     trace_class_resolution(java_lang_Class::as_Klass(JNIHandles::resolve_non_null(result)));
3291   }
3292   return result;
3293 }
3294 
3295 // These lookups are done with the NULL (bootstrap) ClassLoader to
3296 // circumvent any security checks that would be done by jni_FindClass.
3297 JNI_ENTRY(bool, lookupDirectBufferClasses(JNIEnv* env))
3298 {
3299   if ((bufferClass           = lookupOne(env, "java/nio/Buffer", thread))           == NULL) { return false; }
3300   if ((directBufferClass     = lookupOne(env, "sun/nio/ch/DirectBuffer", thread))   == NULL) { return false; }
3301   if ((directByteBufferClass = lookupOne(env, "java/nio/DirectByteBuffer", thread)) == NULL) { return false; }
3302   return true;
3303 }
3304 JNI_END
3305 
3306 
3307 static bool initializeDirectBufferSupport(JNIEnv* env, JavaThread* thread) {
3308   if (directBufferSupportInitializeFailed) {
3309     return false;
3310   }
3311 
3312   if (Atomic::cmpxchg(1, &directBufferSupportInitializeStarted, 0) == 0) {
3313     if (!lookupDirectBufferClasses(env)) {
3314       directBufferSupportInitializeFailed = 1;
3315       return false;
3316     }
3317 
3318     // Make global references for these
3319     bufferClass           = (jclass) env->NewGlobalRef(bufferClass);
3320     directBufferClass     = (jclass) env->NewGlobalRef(directBufferClass);
3321     directByteBufferClass = (jclass) env->NewGlobalRef(directByteBufferClass);
3322 
3323     // Get needed field and method IDs
3324     directByteBufferConstructor = env->GetMethodID(directByteBufferClass, "<init>", "(JI)V");
3325     if (env->ExceptionCheck()) {
3326       env->ExceptionClear();
3327       directBufferSupportInitializeFailed = 1;
3328       return false;
3329     }
3330     directBufferAddressField    = env->GetFieldID(bufferClass, "address", "J");
3331     if (env->ExceptionCheck()) {
3332       env->ExceptionClear();
3333       directBufferSupportInitializeFailed = 1;
3334       return false;
3335     }
3336     bufferCapacityField         = env->GetFieldID(bufferClass, "capacity", "I");
3337     if (env->ExceptionCheck()) {
3338       env->ExceptionClear();
3339       directBufferSupportInitializeFailed = 1;
3340       return false;
3341     }
3342 
3343     if ((directByteBufferConstructor == NULL) ||
3344         (directBufferAddressField    == NULL) ||
3345         (bufferCapacityField         == NULL)) {
3346       directBufferSupportInitializeFailed = 1;
3347       return false;
3348     }
3349 
3350     directBufferSupportInitializeEnded = 1;
3351   } else {
3352     while (!directBufferSupportInitializeEnded && !directBufferSupportInitializeFailed) {
3353       os::naked_yield();
3354     }
3355   }
3356 
3357   return !directBufferSupportInitializeFailed;
3358 }
3359 
3360 extern "C" jobject JNICALL jni_NewDirectByteBuffer(JNIEnv *env, void* address, jlong capacity)
3361 {
3362   // thread_from_jni_environment() will block if VM is gone.
3363   JavaThread* thread = JavaThread::thread_from_jni_environment(env);
3364 
3365   JNIWrapper("jni_NewDirectByteBuffer");
3366  HOTSPOT_JNI_NEWDIRECTBYTEBUFFER_ENTRY(env, address, capacity);
3367 
3368   if (!directBufferSupportInitializeEnded) {
3369     if (!initializeDirectBufferSupport(env, thread)) {
3370       HOTSPOT_JNI_NEWDIRECTBYTEBUFFER_RETURN(NULL);
3371       return NULL;
3372     }
3373   }
3374 
3375   // Being paranoid about accidental sign extension on address
3376   jlong addr = (jlong) ((uintptr_t) address);
3377   // NOTE that package-private DirectByteBuffer constructor currently
3378   // takes int capacity
3379   jint  cap  = (jint)  capacity;
3380   jobject ret = env->NewObject(directByteBufferClass, directByteBufferConstructor, addr, cap);
3381   HOTSPOT_JNI_NEWDIRECTBYTEBUFFER_RETURN(ret);
3382   return ret;
3383 }
3384 
3385 DT_RETURN_MARK_DECL(GetDirectBufferAddress, void*
3386                     , HOTSPOT_JNI_GETDIRECTBUFFERADDRESS_RETURN((void*) _ret_ref));
3387 
3388 extern "C" void* JNICALL jni_GetDirectBufferAddress(JNIEnv *env, jobject buf)
3389 {
3390   // thread_from_jni_environment() will block if VM is gone.
3391   JavaThread* thread = JavaThread::thread_from_jni_environment(env);
3392 
3393   JNIWrapper("jni_GetDirectBufferAddress");
3394   HOTSPOT_JNI_GETDIRECTBUFFERADDRESS_ENTRY(env, buf);
3395   void* ret = NULL;
3396   DT_RETURN_MARK(GetDirectBufferAddress, void*, (const void*&)ret);
3397 
3398   if (!directBufferSupportInitializeEnded) {
3399     if (!initializeDirectBufferSupport(env, thread)) {
3400       return 0;
3401     }
3402   }
3403 
3404   if ((buf != NULL) && (!env->IsInstanceOf(buf, directBufferClass))) {
3405     return 0;
3406   }
3407 
3408   ret = (void*)(intptr_t)env->GetLongField(buf, directBufferAddressField);
3409   return ret;
3410 }
3411 
3412 DT_RETURN_MARK_DECL(GetDirectBufferCapacity, jlong
3413                     , HOTSPOT_JNI_GETDIRECTBUFFERCAPACITY_RETURN(_ret_ref));
3414 
3415 extern "C" jlong JNICALL jni_GetDirectBufferCapacity(JNIEnv *env, jobject buf)
3416 {
3417   // thread_from_jni_environment() will block if VM is gone.
3418   JavaThread* thread = JavaThread::thread_from_jni_environment(env);
3419 
3420   JNIWrapper("jni_GetDirectBufferCapacity");
3421   HOTSPOT_JNI_GETDIRECTBUFFERCAPACITY_ENTRY(env, buf);
3422   jlong ret = -1;
3423   DT_RETURN_MARK(GetDirectBufferCapacity, jlong, (const jlong&)ret);
3424 
3425   if (!directBufferSupportInitializeEnded) {
3426     if (!initializeDirectBufferSupport(env, thread)) {
3427       ret = 0;
3428       return ret;
3429     }
3430   }
3431 
3432   if (buf == NULL) {
3433     return -1;
3434   }
3435 
3436   if (!env->IsInstanceOf(buf, directBufferClass)) {
3437     return -1;
3438   }
3439 
3440   // NOTE that capacity is currently an int in the implementation
3441   ret = env->GetIntField(buf, bufferCapacityField);
3442   return ret;
3443 }
3444 
3445 
3446 JNI_LEAF(jint, jni_GetVersion(JNIEnv *env))
3447   JNIWrapper("GetVersion");
3448   HOTSPOT_JNI_GETVERSION_ENTRY(env);
3449   HOTSPOT_JNI_GETVERSION_RETURN(CurrentVersion);
3450   return CurrentVersion;
3451 JNI_END
3452 
3453 extern struct JavaVM_ main_vm;
3454 
3455 JNI_LEAF(jint, jni_GetJavaVM(JNIEnv *env, JavaVM **vm))
3456   JNIWrapper("jni_GetJavaVM");
3457   HOTSPOT_JNI_GETJAVAVM_ENTRY(env, (void **) vm);
3458   *vm  = (JavaVM *)(&main_vm);
3459   HOTSPOT_JNI_GETJAVAVM_RETURN(JNI_OK);
3460   return JNI_OK;
3461 JNI_END
3462 
3463 
3464 JNI_ENTRY(jobject, jni_GetModule(JNIEnv* env, jclass clazz))
3465   JNIWrapper("GetModule");
3466   return Modules::get_module(clazz, THREAD);
3467 JNI_END
3468 
3469 
3470 // Structure containing all jni functions
3471 struct JNINativeInterface_ jni_NativeInterface = {
3472     NULL,
3473     NULL,
3474     NULL,
3475 
3476     NULL,
3477 
3478     jni_GetVersion,
3479 
3480     jni_DefineClass,
3481     jni_FindClass,
3482 
3483     jni_FromReflectedMethod,
3484     jni_FromReflectedField,
3485 
3486     jni_ToReflectedMethod,
3487 
3488     jni_GetSuperclass,
3489     jni_IsAssignableFrom,
3490 
3491     jni_ToReflectedField,
3492 
3493     jni_Throw,
3494     jni_ThrowNew,
3495     jni_ExceptionOccurred,
3496     jni_ExceptionDescribe,
3497     jni_ExceptionClear,
3498     jni_FatalError,
3499 
3500     jni_PushLocalFrame,
3501     jni_PopLocalFrame,
3502 
3503     jni_NewGlobalRef,
3504     jni_DeleteGlobalRef,
3505     jni_DeleteLocalRef,
3506     jni_IsSameObject,
3507 
3508     jni_NewLocalRef,
3509     jni_EnsureLocalCapacity,
3510 
3511     jni_AllocObject,
3512     jni_NewObject,
3513     jni_NewObjectV,
3514     jni_NewObjectA,
3515 
3516     jni_GetObjectClass,
3517     jni_IsInstanceOf,
3518 
3519     jni_GetMethodID,
3520 
3521     jni_CallObjectMethod,
3522     jni_CallObjectMethodV,
3523     jni_CallObjectMethodA,
3524     jni_CallBooleanMethod,
3525     jni_CallBooleanMethodV,
3526     jni_CallBooleanMethodA,
3527     jni_CallByteMethod,
3528     jni_CallByteMethodV,
3529     jni_CallByteMethodA,
3530     jni_CallCharMethod,
3531     jni_CallCharMethodV,
3532     jni_CallCharMethodA,
3533     jni_CallShortMethod,
3534     jni_CallShortMethodV,
3535     jni_CallShortMethodA,
3536     jni_CallIntMethod,
3537     jni_CallIntMethodV,
3538     jni_CallIntMethodA,
3539     jni_CallLongMethod,
3540     jni_CallLongMethodV,
3541     jni_CallLongMethodA,
3542     jni_CallFloatMethod,
3543     jni_CallFloatMethodV,
3544     jni_CallFloatMethodA,
3545     jni_CallDoubleMethod,
3546     jni_CallDoubleMethodV,
3547     jni_CallDoubleMethodA,
3548     jni_CallVoidMethod,
3549     jni_CallVoidMethodV,
3550     jni_CallVoidMethodA,
3551 
3552     jni_CallNonvirtualObjectMethod,
3553     jni_CallNonvirtualObjectMethodV,
3554     jni_CallNonvirtualObjectMethodA,
3555     jni_CallNonvirtualBooleanMethod,
3556     jni_CallNonvirtualBooleanMethodV,
3557     jni_CallNonvirtualBooleanMethodA,
3558     jni_CallNonvirtualByteMethod,
3559     jni_CallNonvirtualByteMethodV,
3560     jni_CallNonvirtualByteMethodA,
3561     jni_CallNonvirtualCharMethod,
3562     jni_CallNonvirtualCharMethodV,
3563     jni_CallNonvirtualCharMethodA,
3564     jni_CallNonvirtualShortMethod,
3565     jni_CallNonvirtualShortMethodV,
3566     jni_CallNonvirtualShortMethodA,
3567     jni_CallNonvirtualIntMethod,
3568     jni_CallNonvirtualIntMethodV,
3569     jni_CallNonvirtualIntMethodA,
3570     jni_CallNonvirtualLongMethod,
3571     jni_CallNonvirtualLongMethodV,
3572     jni_CallNonvirtualLongMethodA,
3573     jni_CallNonvirtualFloatMethod,
3574     jni_CallNonvirtualFloatMethodV,
3575     jni_CallNonvirtualFloatMethodA,
3576     jni_CallNonvirtualDoubleMethod,
3577     jni_CallNonvirtualDoubleMethodV,
3578     jni_CallNonvirtualDoubleMethodA,
3579     jni_CallNonvirtualVoidMethod,
3580     jni_CallNonvirtualVoidMethodV,
3581     jni_CallNonvirtualVoidMethodA,
3582 
3583     jni_GetFieldID,
3584 
3585     jni_GetObjectField,
3586     jni_GetBooleanField,
3587     jni_GetByteField,
3588     jni_GetCharField,
3589     jni_GetShortField,
3590     jni_GetIntField,
3591     jni_GetLongField,
3592     jni_GetFloatField,
3593     jni_GetDoubleField,
3594 
3595     jni_SetObjectField,
3596     jni_SetBooleanField,
3597     jni_SetByteField,
3598     jni_SetCharField,
3599     jni_SetShortField,
3600     jni_SetIntField,
3601     jni_SetLongField,
3602     jni_SetFloatField,
3603     jni_SetDoubleField,
3604 
3605     jni_GetStaticMethodID,
3606 
3607     jni_CallStaticObjectMethod,
3608     jni_CallStaticObjectMethodV,
3609     jni_CallStaticObjectMethodA,
3610     jni_CallStaticBooleanMethod,
3611     jni_CallStaticBooleanMethodV,
3612     jni_CallStaticBooleanMethodA,
3613     jni_CallStaticByteMethod,
3614     jni_CallStaticByteMethodV,
3615     jni_CallStaticByteMethodA,
3616     jni_CallStaticCharMethod,
3617     jni_CallStaticCharMethodV,
3618     jni_CallStaticCharMethodA,
3619     jni_CallStaticShortMethod,
3620     jni_CallStaticShortMethodV,
3621     jni_CallStaticShortMethodA,
3622     jni_CallStaticIntMethod,
3623     jni_CallStaticIntMethodV,
3624     jni_CallStaticIntMethodA,
3625     jni_CallStaticLongMethod,
3626     jni_CallStaticLongMethodV,
3627     jni_CallStaticLongMethodA,
3628     jni_CallStaticFloatMethod,
3629     jni_CallStaticFloatMethodV,
3630     jni_CallStaticFloatMethodA,
3631     jni_CallStaticDoubleMethod,
3632     jni_CallStaticDoubleMethodV,
3633     jni_CallStaticDoubleMethodA,
3634     jni_CallStaticVoidMethod,
3635     jni_CallStaticVoidMethodV,
3636     jni_CallStaticVoidMethodA,
3637 
3638     jni_GetStaticFieldID,
3639 
3640     jni_GetStaticObjectField,
3641     jni_GetStaticBooleanField,
3642     jni_GetStaticByteField,
3643     jni_GetStaticCharField,
3644     jni_GetStaticShortField,
3645     jni_GetStaticIntField,
3646     jni_GetStaticLongField,
3647     jni_GetStaticFloatField,
3648     jni_GetStaticDoubleField,
3649 
3650     jni_SetStaticObjectField,
3651     jni_SetStaticBooleanField,
3652     jni_SetStaticByteField,
3653     jni_SetStaticCharField,
3654     jni_SetStaticShortField,
3655     jni_SetStaticIntField,
3656     jni_SetStaticLongField,
3657     jni_SetStaticFloatField,
3658     jni_SetStaticDoubleField,
3659 
3660     jni_NewString,
3661     jni_GetStringLength,
3662     jni_GetStringChars,
3663     jni_ReleaseStringChars,
3664 
3665     jni_NewStringUTF,
3666     jni_GetStringUTFLength,
3667     jni_GetStringUTFChars,
3668     jni_ReleaseStringUTFChars,
3669 
3670     jni_GetArrayLength,
3671 
3672     jni_NewObjectArray,
3673     jni_GetObjectArrayElement,
3674     jni_SetObjectArrayElement,
3675 
3676     jni_NewBooleanArray,
3677     jni_NewByteArray,
3678     jni_NewCharArray,
3679     jni_NewShortArray,
3680     jni_NewIntArray,
3681     jni_NewLongArray,
3682     jni_NewFloatArray,
3683     jni_NewDoubleArray,
3684 
3685     jni_GetBooleanArrayElements,
3686     jni_GetByteArrayElements,
3687     jni_GetCharArrayElements,
3688     jni_GetShortArrayElements,
3689     jni_GetIntArrayElements,
3690     jni_GetLongArrayElements,
3691     jni_GetFloatArrayElements,
3692     jni_GetDoubleArrayElements,
3693 
3694     jni_ReleaseBooleanArrayElements,
3695     jni_ReleaseByteArrayElements,
3696     jni_ReleaseCharArrayElements,
3697     jni_ReleaseShortArrayElements,
3698     jni_ReleaseIntArrayElements,
3699     jni_ReleaseLongArrayElements,
3700     jni_ReleaseFloatArrayElements,
3701     jni_ReleaseDoubleArrayElements,
3702 
3703     jni_GetBooleanArrayRegion,
3704     jni_GetByteArrayRegion,
3705     jni_GetCharArrayRegion,
3706     jni_GetShortArrayRegion,
3707     jni_GetIntArrayRegion,
3708     jni_GetLongArrayRegion,
3709     jni_GetFloatArrayRegion,
3710     jni_GetDoubleArrayRegion,
3711 
3712     jni_SetBooleanArrayRegion,
3713     jni_SetByteArrayRegion,
3714     jni_SetCharArrayRegion,
3715     jni_SetShortArrayRegion,
3716     jni_SetIntArrayRegion,
3717     jni_SetLongArrayRegion,
3718     jni_SetFloatArrayRegion,
3719     jni_SetDoubleArrayRegion,
3720 
3721     jni_RegisterNatives,
3722     jni_UnregisterNatives,
3723 
3724     jni_MonitorEnter,
3725     jni_MonitorExit,
3726 
3727     jni_GetJavaVM,
3728 
3729     jni_GetStringRegion,
3730     jni_GetStringUTFRegion,
3731 
3732     jni_GetPrimitiveArrayCritical,
3733     jni_ReleasePrimitiveArrayCritical,
3734 
3735     jni_GetStringCritical,
3736     jni_ReleaseStringCritical,
3737 
3738     jni_NewWeakGlobalRef,
3739     jni_DeleteWeakGlobalRef,
3740 
3741     jni_ExceptionCheck,
3742 
3743     jni_NewDirectByteBuffer,
3744     jni_GetDirectBufferAddress,
3745     jni_GetDirectBufferCapacity,
3746 
3747     // New 1_6 features
3748 
3749     jni_GetObjectRefType,
3750 
3751     // Module features
3752 
3753     jni_GetModule
3754 };
3755 
3756 
3757 // For jvmti use to modify jni function table.
3758 // Java threads in native contiues to run until it is transitioned
3759 // to VM at safepoint. Before the transition or before it is blocked
3760 // for safepoint it may access jni function table. VM could crash if
3761 // any java thread access the jni function table in the middle of memcpy.
3762 // To avoid this each function pointers are copied automically.
3763 void copy_jni_function_table(const struct JNINativeInterface_ *new_jni_NativeInterface) {
3764   assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint");
3765   intptr_t *a = (intptr_t *) jni_functions();
3766   intptr_t *b = (intptr_t *) new_jni_NativeInterface;
3767   for (uint i=0; i <  sizeof(struct JNINativeInterface_)/sizeof(void *); i++) {
3768     Atomic::store(*b++, a++);
3769   }
3770 }
3771 
3772 void quicken_jni_functions() {
3773   // Replace Get<Primitive>Field with fast versions
3774   if (UseFastJNIAccessors && !JvmtiExport::can_post_field_access()
3775       && !VerifyJNIFields && !CountJNICalls && !CheckJNICalls
3776 #if defined(_WINDOWS) && defined(IA32) && defined(COMPILER2)
3777       // windows x86 currently needs SEH wrapper and the gain of the fast
3778       // versions currently isn't certain for server vm on uniprocessor.
3779       && os::is_MP()
3780 #endif
3781   ) {
3782     address func;
3783     func = JNI_FastGetField::generate_fast_get_boolean_field();
3784     if (func != (address)-1) {
3785       jni_NativeInterface.GetBooleanField = (GetBooleanField_t)func;
3786     }
3787     func = JNI_FastGetField::generate_fast_get_byte_field();
3788     if (func != (address)-1) {
3789       jni_NativeInterface.GetByteField = (GetByteField_t)func;
3790     }
3791     func = JNI_FastGetField::generate_fast_get_char_field();
3792     if (func != (address)-1) {
3793       jni_NativeInterface.GetCharField = (GetCharField_t)func;
3794     }
3795     func = JNI_FastGetField::generate_fast_get_short_field();
3796     if (func != (address)-1) {
3797       jni_NativeInterface.GetShortField = (GetShortField_t)func;
3798     }
3799     func = JNI_FastGetField::generate_fast_get_int_field();
3800     if (func != (address)-1) {
3801       jni_NativeInterface.GetIntField = (GetIntField_t)func;
3802     }
3803     func = JNI_FastGetField::generate_fast_get_long_field();
3804     if (func != (address)-1) {
3805       jni_NativeInterface.GetLongField = (GetLongField_t)func;
3806     }
3807     func = JNI_FastGetField::generate_fast_get_float_field();
3808     if (func != (address)-1) {
3809       jni_NativeInterface.GetFloatField = (GetFloatField_t)func;
3810     }
3811     func = JNI_FastGetField::generate_fast_get_double_field();
3812     if (func != (address)-1) {
3813       jni_NativeInterface.GetDoubleField = (GetDoubleField_t)func;
3814     }
3815   }
3816 }
3817 
3818 // Returns the function structure
3819 struct JNINativeInterface_* jni_functions() {
3820 #if INCLUDE_JNI_CHECK
3821   if (CheckJNICalls) return jni_functions_check();
3822 #endif // INCLUDE_JNI_CHECK
3823   return &jni_NativeInterface;
3824 }
3825 
3826 // Returns the function structure
3827 struct JNINativeInterface_* jni_functions_nocheck() {
3828   return &jni_NativeInterface;
3829 }
3830 
3831 static void post_thread_start_event(const JavaThread* jt) {
3832   assert(jt != NULL, "invariant");
3833   EventThreadStart event;
3834   if (event.should_commit()) {
3835     event.set_thread(JFR_THREAD_ID(jt));
3836     event.commit();
3837   }
3838 }
3839 
3840 // Invocation API
3841 
3842 
3843 // Forward declaration
3844 extern const struct JNIInvokeInterface_ jni_InvokeInterface;
3845 
3846 // Global invocation API vars
3847 volatile int vm_created = 0;
3848 // Indicate whether it is safe to recreate VM
3849 volatile int safe_to_recreate_vm = 1;
3850 struct JavaVM_ main_vm = {&jni_InvokeInterface};
3851 
3852 
3853 #define JAVASTACKSIZE (400 * 1024)    /* Default size of a thread java stack */
3854 enum { VERIFY_NONE, VERIFY_REMOTE, VERIFY_ALL };
3855 
3856 DT_RETURN_MARK_DECL(GetDefaultJavaVMInitArgs, jint
3857                     , HOTSPOT_JNI_GETDEFAULTJAVAVMINITARGS_RETURN(_ret_ref));
3858 
3859 _JNI_IMPORT_OR_EXPORT_ jint JNICALL JNI_GetDefaultJavaVMInitArgs(void *args_) {
3860   HOTSPOT_JNI_GETDEFAULTJAVAVMINITARGS_ENTRY(args_);
3861   JDK1_1InitArgs *args = (JDK1_1InitArgs *)args_;
3862   jint ret = JNI_ERR;
3863   DT_RETURN_MARK(GetDefaultJavaVMInitArgs, jint, (const jint&)ret);
3864 
3865   if (Threads::is_supported_jni_version(args->version)) {
3866     ret = JNI_OK;
3867   }
3868   // 1.1 style no longer supported in hotspot.
3869   // According the JNI spec, we should update args->version on return.
3870   // We also use the structure to communicate with launcher about default
3871   // stack size.
3872   if (args->version == JNI_VERSION_1_1) {
3873     args->version = JNI_VERSION_1_2;
3874     // javaStackSize is int in arguments structure
3875     assert(jlong(ThreadStackSize) * K < INT_MAX, "integer overflow");
3876     args->javaStackSize = (jint)(ThreadStackSize * K);
3877   }
3878   return ret;
3879 }
3880 
3881 DT_RETURN_MARK_DECL(CreateJavaVM, jint
3882                     , HOTSPOT_JNI_CREATEJAVAVM_RETURN(_ret_ref));
3883 
3884 static jint JNI_CreateJavaVM_inner(JavaVM **vm, void **penv, void *args) {
3885   HOTSPOT_JNI_CREATEJAVAVM_ENTRY((void **) vm, penv, args);
3886 
3887   jint result = JNI_ERR;
3888   DT_RETURN_MARK(CreateJavaVM, jint, (const jint&)result);
3889 
3890   // We're about to use Atomic::xchg for synchronization.  Some Zero
3891   // platforms use the GCC builtin __sync_lock_test_and_set for this,
3892   // but __sync_lock_test_and_set is not guaranteed to do what we want
3893   // on all architectures.  So we check it works before relying on it.
3894 #if defined(ZERO) && defined(ASSERT)
3895   {
3896     jint a = 0xcafebabe;
3897     jint b = Atomic::xchg((jint) 0xdeadbeef, &a);
3898     void *c = &a;
3899     void *d = Atomic::xchg(&b, &c);
3900     assert(a == (jint) 0xdeadbeef && b == (jint) 0xcafebabe, "Atomic::xchg() works");
3901     assert(c == &b && d == &a, "Atomic::xchg() works");
3902   }
3903 #endif // ZERO && ASSERT
3904 
3905   // At the moment it's only possible to have one Java VM,
3906   // since some of the runtime state is in global variables.
3907 
3908   // We cannot use our mutex locks here, since they only work on
3909   // Threads. We do an atomic compare and exchange to ensure only
3910   // one thread can call this method at a time
3911 
3912   // We use Atomic::xchg rather than Atomic::add/dec since on some platforms
3913   // the add/dec implementations are dependent on whether we are running
3914   // on a multiprocessor, and at this stage of initialization the os::is_MP
3915   // function used to determine this will always return false. Atomic::xchg
3916   // does not have this problem.
3917   if (Atomic::xchg(1, &vm_created) == 1) {
3918     return JNI_EEXIST;   // already created, or create attempt in progress
3919   }
3920   if (Atomic::xchg(0, &safe_to_recreate_vm) == 0) {
3921     return JNI_ERR;  // someone tried and failed and retry not allowed.
3922   }
3923 
3924   assert(vm_created == 1, "vm_created is true during the creation");
3925 
3926   /**
3927    * Certain errors during initialization are recoverable and do not
3928    * prevent this method from being called again at a later time
3929    * (perhaps with different arguments).  However, at a certain
3930    * point during initialization if an error occurs we cannot allow
3931    * this function to be called again (or it will crash).  In those
3932    * situations, the 'canTryAgain' flag is set to false, which atomically
3933    * sets safe_to_recreate_vm to 1, such that any new call to
3934    * JNI_CreateJavaVM will immediately fail using the above logic.
3935    */
3936   bool can_try_again = true;
3937 
3938   result = Threads::create_vm((JavaVMInitArgs*) args, &can_try_again);
3939   if (result == JNI_OK) {
3940     JavaThread *thread = JavaThread::current();
3941     assert(!thread->has_pending_exception(), "should have returned not OK");
3942     /* thread is thread_in_vm here */
3943     *vm = (JavaVM *)(&main_vm);
3944     *(JNIEnv**)penv = thread->jni_environment();
3945 
3946 #if INCLUDE_JVMCI
3947     if (EnableJVMCI) {
3948       if (UseJVMCICompiler) {
3949         // JVMCI is initialized on a CompilerThread
3950         if (BootstrapJVMCI) {
3951           JavaThread* THREAD = thread;
3952           JVMCICompiler* compiler = JVMCICompiler::instance(true, CATCH);
3953           compiler->bootstrap(THREAD);
3954           if (HAS_PENDING_EXCEPTION) {
3955             HandleMark hm;
3956             vm_exit_during_initialization(Handle(THREAD, PENDING_EXCEPTION));
3957           }
3958         }
3959       }
3960     }
3961 #endif
3962 
3963     // Tracks the time application was running before GC
3964     RuntimeService::record_application_start();
3965 
3966     // Notify JVMTI
3967     if (JvmtiExport::should_post_thread_life()) {
3968        JvmtiExport::post_thread_start(thread);
3969     }
3970 
3971     post_thread_start_event(thread);
3972 
3973 #ifndef PRODUCT
3974     // Check if we should compile all classes on bootclasspath
3975     if (CompileTheWorld) ClassLoader::compile_the_world();
3976     if (ReplayCompiles) ciReplay::replay(thread);
3977 
3978     // Some platforms (like Win*) need a wrapper around these test
3979     // functions in order to properly handle error conditions.
3980     VMError::test_error_handler();
3981     if (ExecuteInternalVMTests) {
3982       InternalVMTests::run();
3983     }
3984 #endif
3985 
3986     // Since this is not a JVM_ENTRY we have to set the thread state manually before leaving.
3987     ThreadStateTransition::transition_and_fence(thread, _thread_in_vm, _thread_in_native);
3988   } else {
3989     // If create_vm exits because of a pending exception, exit with that
3990     // exception.  In the future when we figure out how to reclaim memory,
3991     // we may be able to exit with JNI_ERR and allow the calling application
3992     // to continue.
3993     if (Universe::is_fully_initialized()) {
3994       // otherwise no pending exception possible - VM will already have aborted
3995       JavaThread* THREAD = JavaThread::current();
3996       if (HAS_PENDING_EXCEPTION) {
3997         HandleMark hm;
3998         vm_exit_during_initialization(Handle(THREAD, PENDING_EXCEPTION));
3999       }
4000     }
4001 
4002     if (can_try_again) {
4003       // reset safe_to_recreate_vm to 1 so that retrial would be possible
4004       safe_to_recreate_vm = 1;
4005     }
4006 
4007     // Creation failed. We must reset vm_created
4008     *vm = 0;
4009     *(JNIEnv**)penv = 0;
4010     // reset vm_created last to avoid race condition. Use OrderAccess to
4011     // control both compiler and architectural-based reordering.
4012     OrderAccess::release_store(&vm_created, 0);
4013   }
4014 
4015   // Flush stdout and stderr before exit.
4016   fflush(stdout);
4017   fflush(stderr);
4018 
4019   return result;
4020 
4021 }
4022 
4023 _JNI_IMPORT_OR_EXPORT_ jint JNICALL JNI_CreateJavaVM(JavaVM **vm, void **penv, void *args) {
4024   jint result = JNI_ERR;
4025   // On Windows, let CreateJavaVM run with SEH protection
4026 #ifdef _WIN32
4027   __try {
4028 #endif
4029     result = JNI_CreateJavaVM_inner(vm, penv, args);
4030 #ifdef _WIN32
4031   } __except(topLevelExceptionFilter((_EXCEPTION_POINTERS*)_exception_info())) {
4032     // Nothing to do.
4033   }
4034 #endif
4035   return result;
4036 }
4037 
4038 _JNI_IMPORT_OR_EXPORT_ jint JNICALL JNI_GetCreatedJavaVMs(JavaVM **vm_buf, jsize bufLen, jsize *numVMs) {
4039   // See bug 4367188, the wrapper can sometimes cause VM crashes
4040   // JNIWrapper("GetCreatedJavaVMs");
4041 
4042   HOTSPOT_JNI_GETCREATEDJAVAVMS_ENTRY((void **) vm_buf, bufLen, (uintptr_t *) numVMs);
4043 
4044   if (vm_created == 1) {
4045     if (numVMs != NULL) *numVMs = 1;
4046     if (bufLen > 0)     *vm_buf = (JavaVM *)(&main_vm);
4047   } else {
4048     if (numVMs != NULL) *numVMs = 0;
4049   }
4050   HOTSPOT_JNI_GETCREATEDJAVAVMS_RETURN(JNI_OK);
4051   return JNI_OK;
4052 }
4053 
4054 extern "C" {
4055 
4056 DT_RETURN_MARK_DECL(DestroyJavaVM, jint
4057                     , HOTSPOT_JNI_DESTROYJAVAVM_RETURN(_ret_ref));
4058 
4059 static jint JNICALL jni_DestroyJavaVM_inner(JavaVM *vm) {
4060   HOTSPOT_JNI_DESTROYJAVAVM_ENTRY(vm);
4061   jint res = JNI_ERR;
4062   DT_RETURN_MARK(DestroyJavaVM, jint, (const jint&)res);
4063 
4064   if (vm_created == 0) {
4065     res = JNI_ERR;
4066     return res;
4067   }
4068 
4069   JNIWrapper("DestroyJavaVM");
4070   JNIEnv *env;
4071   JavaVMAttachArgs destroyargs;
4072   destroyargs.version = CurrentVersion;
4073   destroyargs.name = (char *)"DestroyJavaVM";
4074   destroyargs.group = NULL;
4075   res = vm->AttachCurrentThread((void **)&env, (void *)&destroyargs);
4076   if (res != JNI_OK) {
4077     return res;
4078   }
4079 
4080   // Since this is not a JVM_ENTRY we have to set the thread state manually before entering.
4081   JavaThread* thread = JavaThread::current();
4082   ThreadStateTransition::transition_from_native(thread, _thread_in_vm);
4083   if (Threads::destroy_vm()) {
4084     // Should not change thread state, VM is gone
4085     vm_created = 0;
4086     res = JNI_OK;
4087     return res;
4088   } else {
4089     ThreadStateTransition::transition_and_fence(thread, _thread_in_vm, _thread_in_native);
4090     res = JNI_ERR;
4091     return res;
4092   }
4093 }
4094 
4095 jint JNICALL jni_DestroyJavaVM(JavaVM *vm) {
4096   jint result = JNI_ERR;
4097   // On Windows, we need SEH protection
4098 #ifdef _WIN32
4099   __try {
4100 #endif
4101     result = jni_DestroyJavaVM_inner(vm);
4102 #ifdef _WIN32
4103   } __except(topLevelExceptionFilter((_EXCEPTION_POINTERS*)_exception_info())) {
4104     // Nothing to do.
4105   }
4106 #endif
4107   return result;
4108 }
4109 
4110 static jint attach_current_thread(JavaVM *vm, void **penv, void *_args, bool daemon) {
4111   JavaVMAttachArgs *args = (JavaVMAttachArgs *) _args;
4112 
4113   // Check below commented out from JDK1.2fcs as well
4114   /*
4115   if (args && (args->version != JNI_VERSION_1_1 || args->version != JNI_VERSION_1_2)) {
4116     return JNI_EVERSION;
4117   }
4118   */
4119 
4120   Thread* t = Thread::current_or_null();
4121   if (t != NULL) {
4122     // If the thread has been attached this operation is a no-op
4123     *(JNIEnv**)penv = ((JavaThread*) t)->jni_environment();
4124     return JNI_OK;
4125   }
4126 
4127   // Create a thread and mark it as attaching so it will be skipped by the
4128   // ThreadsListEnumerator - see CR 6404306
4129   JavaThread* thread = new JavaThread(true);
4130 
4131   // Set correct safepoint info. The thread is going to call into Java when
4132   // initializing the Java level thread object. Hence, the correct state must
4133   // be set in order for the Safepoint code to deal with it correctly.
4134   thread->set_thread_state(_thread_in_vm);
4135   thread->record_stack_base_and_size();
4136   thread->initialize_thread_current();
4137 
4138   if (!os::create_attached_thread(thread)) {
4139     thread->smr_delete();
4140     return JNI_ERR;
4141   }
4142   // Enable stack overflow checks
4143   thread->create_stack_guard_pages();
4144 
4145   thread->initialize_tlab();
4146 
4147   thread->cache_global_variables();
4148 
4149   // Crucial that we do not have a safepoint check for this thread, since it has
4150   // not been added to the Thread list yet.
4151   { Threads_lock->lock_without_safepoint_check();
4152     // This must be inside this lock in order to get FullGCALot to work properly, i.e., to
4153     // avoid this thread trying to do a GC before it is added to the thread-list
4154     thread->set_active_handles(JNIHandleBlock::allocate_block());
4155     Threads::add(thread, daemon);
4156     Threads_lock->unlock();
4157   }
4158   // Create thread group and name info from attach arguments
4159   oop group = NULL;
4160   char* thread_name = NULL;
4161   if (args != NULL && Threads::is_supported_jni_version(args->version)) {
4162     group = JNIHandles::resolve(args->group);
4163     thread_name = args->name; // may be NULL
4164   }
4165   if (group == NULL) group = Universe::main_thread_group();
4166 
4167   // Create Java level thread object and attach it to this thread
4168   bool attach_failed = false;
4169   {
4170     EXCEPTION_MARK;
4171     HandleMark hm(THREAD);
4172     Handle thread_group(THREAD, group);
4173     thread->allocate_threadObj(thread_group, thread_name, daemon, THREAD);
4174     if (HAS_PENDING_EXCEPTION) {
4175       CLEAR_PENDING_EXCEPTION;
4176       // cleanup outside the handle mark.
4177       attach_failed = true;
4178     }
4179   }
4180 
4181   if (attach_failed) {
4182     // Added missing cleanup
4183     thread->cleanup_failed_attach_current_thread();
4184     return JNI_ERR;
4185   }
4186 
4187   // mark the thread as no longer attaching
4188   // this uses a fence to push the change through so we don't have
4189   // to regrab the threads_lock
4190   thread->set_done_attaching_via_jni();
4191 
4192   // Set java thread status.
4193   java_lang_Thread::set_thread_status(thread->threadObj(),
4194               java_lang_Thread::RUNNABLE);
4195 
4196   // Notify the debugger
4197   if (JvmtiExport::should_post_thread_life()) {
4198     JvmtiExport::post_thread_start(thread);
4199   }
4200 
4201   post_thread_start_event(thread);
4202 
4203   *(JNIEnv**)penv = thread->jni_environment();
4204 
4205   // Now leaving the VM, so change thread_state. This is normally automatically taken care
4206   // of in the JVM_ENTRY. But in this situation we have to do it manually. Notice, that by
4207   // using ThreadStateTransition::transition, we do a callback to the safepoint code if
4208   // needed.
4209 
4210   ThreadStateTransition::transition_and_fence(thread, _thread_in_vm, _thread_in_native);
4211 
4212   // Perform any platform dependent FPU setup
4213   os::setup_fpu();
4214 
4215   return JNI_OK;
4216 }
4217 
4218 
4219 jint JNICALL jni_AttachCurrentThread(JavaVM *vm, void **penv, void *_args) {
4220   HOTSPOT_JNI_ATTACHCURRENTTHREAD_ENTRY(vm, penv, _args);
4221   if (vm_created == 0) {
4222   HOTSPOT_JNI_ATTACHCURRENTTHREAD_RETURN((uint32_t) JNI_ERR);
4223     return JNI_ERR;
4224   }
4225 
4226   JNIWrapper("AttachCurrentThread");
4227   jint ret = attach_current_thread(vm, penv, _args, false);
4228   HOTSPOT_JNI_ATTACHCURRENTTHREAD_RETURN(ret);
4229   return ret;
4230 }
4231 
4232 
4233 jint JNICALL jni_DetachCurrentThread(JavaVM *vm)  {
4234   HOTSPOT_JNI_DETACHCURRENTTHREAD_ENTRY(vm);
4235 
4236   JNIWrapper("DetachCurrentThread");
4237 
4238   // If the thread has already been detached the operation is a no-op
4239   if (Thread::current_or_null() == NULL) {
4240     HOTSPOT_JNI_DETACHCURRENTTHREAD_RETURN(JNI_OK);
4241     return JNI_OK;
4242   }
4243 
4244   VM_Exit::block_if_vm_exited();
4245 
4246   JavaThread* thread = JavaThread::current();
4247   if (thread->has_last_Java_frame()) {
4248     HOTSPOT_JNI_DETACHCURRENTTHREAD_RETURN((uint32_t) JNI_ERR);
4249     // Can't detach a thread that's running java, that can't work.
4250     return JNI_ERR;
4251   }
4252 
4253   // Safepoint support. Have to do call-back to safepoint code, if in the
4254   // middle of a safepoint operation
4255   ThreadStateTransition::transition_from_native(thread, _thread_in_vm);
4256 
4257   // XXX: Note that JavaThread::exit() call below removes the guards on the
4258   // stack pages set up via enable_stack_{red,yellow}_zone() calls
4259   // above in jni_AttachCurrentThread. Unfortunately, while the setting
4260   // of the guards is visible in jni_AttachCurrentThread above,
4261   // the removal of the guards is buried below in JavaThread::exit()
4262   // here. The abstraction should be more symmetrically either exposed
4263   // or hidden (e.g. it could probably be hidden in the same
4264   // (platform-dependent) methods where we do alternate stack
4265   // maintenance work?)
4266   thread->exit(false, JavaThread::jni_detach);
4267   thread->smr_delete();
4268 
4269   HOTSPOT_JNI_DETACHCURRENTTHREAD_RETURN(JNI_OK);
4270   return JNI_OK;
4271 }
4272 
4273 DT_RETURN_MARK_DECL(GetEnv, jint
4274                     , HOTSPOT_JNI_GETENV_RETURN(_ret_ref));
4275 
4276 jint JNICALL jni_GetEnv(JavaVM *vm, void **penv, jint version) {
4277   HOTSPOT_JNI_GETENV_ENTRY(vm, penv, version);
4278   jint ret = JNI_ERR;
4279   DT_RETURN_MARK(GetEnv, jint, (const jint&)ret);
4280 
4281   if (vm_created == 0) {
4282     *penv = NULL;
4283     ret = JNI_EDETACHED;
4284     return ret;
4285   }
4286 
4287   if (JniExportedInterface::GetExportedInterface(vm, penv, version, &ret)) {
4288     return ret;
4289   }
4290 
4291 #ifndef JVMPI_VERSION_1
4292 // need these in order to be polite about older agents
4293 #define JVMPI_VERSION_1   ((jint)0x10000001)
4294 #define JVMPI_VERSION_1_1 ((jint)0x10000002)
4295 #define JVMPI_VERSION_1_2 ((jint)0x10000003)
4296 #endif // !JVMPI_VERSION_1
4297 
4298   Thread* thread = Thread::current_or_null();
4299   if (thread != NULL && thread->is_Java_thread()) {
4300     if (Threads::is_supported_jni_version_including_1_1(version)) {
4301       *(JNIEnv**)penv = ((JavaThread*) thread)->jni_environment();
4302       ret = JNI_OK;
4303       return ret;
4304 
4305     } else if (version == JVMPI_VERSION_1 ||
4306                version == JVMPI_VERSION_1_1 ||
4307                version == JVMPI_VERSION_1_2) {
4308       tty->print_cr("ERROR: JVMPI, an experimental interface, is no longer supported.");
4309       tty->print_cr("Please use the supported interface: the JVM Tool Interface (JVM TI).");
4310       ret = JNI_EVERSION;
4311       return ret;
4312     } else if (JvmtiExport::is_jvmdi_version(version)) {
4313       tty->print_cr("FATAL ERROR: JVMDI is no longer supported.");
4314       tty->print_cr("Please use the supported interface: the JVM Tool Interface (JVM TI).");
4315       ret = JNI_EVERSION;
4316       return ret;
4317     } else {
4318       *penv = NULL;
4319       ret = JNI_EVERSION;
4320       return ret;
4321     }
4322   } else {
4323     *penv = NULL;
4324     ret = JNI_EDETACHED;
4325     return ret;
4326   }
4327 }
4328 
4329 
4330 jint JNICALL jni_AttachCurrentThreadAsDaemon(JavaVM *vm, void **penv, void *_args) {
4331   HOTSPOT_JNI_ATTACHCURRENTTHREADASDAEMON_ENTRY(vm, penv, _args);
4332   if (vm_created == 0) {
4333   HOTSPOT_JNI_ATTACHCURRENTTHREADASDAEMON_RETURN((uint32_t) JNI_ERR);
4334     return JNI_ERR;
4335   }
4336 
4337   JNIWrapper("AttachCurrentThreadAsDaemon");
4338   jint ret = attach_current_thread(vm, penv, _args, true);
4339   HOTSPOT_JNI_ATTACHCURRENTTHREADASDAEMON_RETURN(ret);
4340   return ret;
4341 }
4342 
4343 
4344 } // End extern "C"
4345 
4346 const struct JNIInvokeInterface_ jni_InvokeInterface = {
4347     NULL,
4348     NULL,
4349     NULL,
4350 
4351     jni_DestroyJavaVM,
4352     jni_AttachCurrentThread,
4353     jni_DetachCurrentThread,
4354     jni_GetEnv,
4355     jni_AttachCurrentThreadAsDaemon
4356 };
--- EOF ---