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