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