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