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