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