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