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