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