rev 49674 : 8198285: More consistent Access API for arraycopy

   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/safepointVerifiers.hpp"
  76 #include "runtime/sharedRuntime.hpp"
  77 #include "runtime/signature.hpp"
  78 #include "runtime/thread.inline.hpp"
  79 #include "runtime/vm_operations.hpp"
  80 #include "services/memTracker.hpp"
  81 #include "services/runtimeService.hpp"
  82 #include "trace/traceMacros.hpp"
  83 #include "trace/tracing.hpp"
  84 #include "utilities/defaultStream.hpp"
  85 #include "utilities/dtrace.hpp"
  86 #include "utilities/events.hpp"
  87 #include "utilities/histogram.hpp"
  88 #include "utilities/internalVMTests.hpp"
  89 #include "utilities/macros.hpp"
  90 #include "utilities/vmError.hpp"
  91 #if INCLUDE_JVMCI
  92 #include "jvmci/jvmciCompiler.hpp"
  93 #include "jvmci/jvmciRuntime.hpp"
  94 #endif
  95 
  96 static jint CurrentVersion = JNI_VERSION_10;
  97 
  98 #ifdef _WIN32
  99 extern LONG WINAPI topLevelExceptionFilter(_EXCEPTION_POINTERS* );
 100 #endif
 101 
 102 // The DT_RETURN_MARK macros create a scoped object to fire the dtrace
 103 // '-return' probe regardless of the return path is taken out of the function.
 104 // Methods that have multiple return paths use this to avoid having to
 105 // instrument each return path.  Methods that use CHECK or THROW must use this
 106 // since those macros can cause an immedate uninstrumented return.
 107 //
 108 // In order to get the return value, a reference to the variable containing
 109 // the return value must be passed to the contructor of the object, and
 110 // the return value must be set before return (since the mark object has
 111 // a reference to it).
 112 //
 113 // Example:
 114 // DT_RETURN_MARK_DECL(SomeFunc, int);
 115 // JNI_ENTRY(int, SomeFunc, ...)
 116 //   int return_value = 0;
 117 //   DT_RETURN_MARK(SomeFunc, int, (const int&)return_value);
 118 //   foo(CHECK_0)
 119 //   return_value = 5;
 120 //   return return_value;
 121 // JNI_END
 122 #define DT_RETURN_MARK_DECL(name, type, probe)                             \
 123   DTRACE_ONLY(                                                             \
 124     class DTraceReturnProbeMark_##name {                                   \
 125      public:                                                               \
 126       const type& _ret_ref;                                                \
 127       DTraceReturnProbeMark_##name(const type& v) : _ret_ref(v) {}         \
 128       ~DTraceReturnProbeMark_##name() {                                    \
 129         probe;                                                             \
 130       }                                                                    \
 131     }                                                                      \
 132   )
 133 // Void functions are simpler since there's no return value
 134 #define DT_VOID_RETURN_MARK_DECL(name, probe)                              \
 135   DTRACE_ONLY(                                                             \
 136     class DTraceReturnProbeMark_##name {                                   \
 137      public:                                                               \
 138       ~DTraceReturnProbeMark_##name() {                                    \
 139         probe;                                                             \
 140       }                                                                    \
 141     }                                                                      \
 142   )
 143 
 144 // Place these macros in the function to mark the return.  Non-void
 145 // functions need the type and address of the return value.
 146 #define DT_RETURN_MARK(name, type, ref) \
 147   DTRACE_ONLY( DTraceReturnProbeMark_##name dtrace_return_mark(ref) )
 148 #define DT_VOID_RETURN_MARK(name) \
 149   DTRACE_ONLY( DTraceReturnProbeMark_##name dtrace_return_mark )
 150 
 151 
 152 // Use these to select distinct code for floating-point vs. non-floating point
 153 // situations.  Used from within common macros where we need slightly
 154 // different behavior for Float/Double
 155 #define FP_SELECT_Boolean(intcode, fpcode) intcode
 156 #define FP_SELECT_Byte(intcode, fpcode)    intcode
 157 #define FP_SELECT_Char(intcode, fpcode)    intcode
 158 #define FP_SELECT_Short(intcode, fpcode)   intcode
 159 #define FP_SELECT_Object(intcode, fpcode)  intcode
 160 #define FP_SELECT_Int(intcode, fpcode)     intcode
 161 #define FP_SELECT_Long(intcode, fpcode)    intcode
 162 #define FP_SELECT_Float(intcode, fpcode)   fpcode
 163 #define FP_SELECT_Double(intcode, fpcode)  fpcode
 164 #define FP_SELECT(TypeName, intcode, fpcode) \
 165   FP_SELECT_##TypeName(intcode, fpcode)
 166 
 167 // Choose DT_RETURN_MARK macros  based on the type: float/double -> void
 168 // (dtrace doesn't do FP yet)
 169 #define DT_RETURN_MARK_DECL_FOR(TypeName, name, type, probe)    \
 170   FP_SELECT(TypeName, \
 171     DT_RETURN_MARK_DECL(name, type, probe), DT_VOID_RETURN_MARK_DECL(name, probe) )
 172 #define DT_RETURN_MARK_FOR(TypeName, name, type, ref) \
 173   FP_SELECT(TypeName, \
 174     DT_RETURN_MARK(name, type, ref), DT_VOID_RETURN_MARK(name) )
 175 
 176 
 177 // out-of-line helpers for class jfieldIDWorkaround:
 178 
 179 bool jfieldIDWorkaround::is_valid_jfieldID(Klass* k, jfieldID id) {
 180   if (jfieldIDWorkaround::is_instance_jfieldID(k, id)) {
 181     uintptr_t as_uint = (uintptr_t) id;
 182     intptr_t offset = raw_instance_offset(id);
 183     if (is_checked_jfieldID(id)) {
 184       if (!klass_hash_ok(k, id)) {
 185         return false;
 186       }
 187     }
 188     return InstanceKlass::cast(k)->contains_field_offset(offset);
 189   } else {
 190     JNIid* result = (JNIid*) id;
 191 #ifdef ASSERT
 192     return result != NULL && result->is_static_field_id();
 193 #else
 194     return result != NULL;
 195 #endif
 196   }
 197 }
 198 
 199 
 200 intptr_t jfieldIDWorkaround::encode_klass_hash(Klass* k, intptr_t offset) {
 201   if (offset <= small_offset_mask) {
 202     Klass* field_klass = k;
 203     Klass* super_klass = field_klass->super();
 204     // With compressed oops the most super class with nonstatic fields would
 205     // be the owner of fields embedded in the header.
 206     while (InstanceKlass::cast(super_klass)->has_nonstatic_fields() &&
 207            InstanceKlass::cast(super_klass)->contains_field_offset(offset)) {
 208       field_klass = super_klass;   // super contains the field also
 209       super_klass = field_klass->super();
 210     }
 211     debug_only(NoSafepointVerifier nosafepoint;)
 212     uintptr_t klass_hash = field_klass->identity_hash();
 213     return ((klass_hash & klass_mask) << klass_shift) | checked_mask_in_place;
 214   } else {
 215 #if 0
 216     #ifndef PRODUCT
 217     {
 218       ResourceMark rm;
 219       warning("VerifyJNIFields: long offset %d in %s", offset, k->external_name());
 220     }
 221     #endif
 222 #endif
 223     return 0;
 224   }
 225 }
 226 
 227 bool jfieldIDWorkaround::klass_hash_ok(Klass* k, jfieldID id) {
 228   uintptr_t as_uint = (uintptr_t) id;
 229   intptr_t klass_hash = (as_uint >> klass_shift) & klass_mask;
 230   do {
 231     debug_only(NoSafepointVerifier nosafepoint;)
 232     // Could use a non-blocking query for identity_hash here...
 233     if ((k->identity_hash() & klass_mask) == klass_hash)
 234       return true;
 235     k = k->super();
 236   } while (k != NULL);
 237   return false;
 238 }
 239 
 240 void jfieldIDWorkaround::verify_instance_jfieldID(Klass* k, jfieldID id) {
 241   guarantee(jfieldIDWorkaround::is_instance_jfieldID(k, id), "must be an instance field" );
 242   uintptr_t as_uint = (uintptr_t) id;
 243   intptr_t offset = raw_instance_offset(id);
 244   if (VerifyJNIFields) {
 245     if (is_checked_jfieldID(id)) {
 246       guarantee(klass_hash_ok(k, id),
 247     "Bug in native code: jfieldID class must match object");
 248     } else {
 249 #if 0
 250       #ifndef PRODUCT
 251       if (Verbose) {
 252   ResourceMark rm;
 253   warning("VerifyJNIFields: unverified offset %d for %s", offset, k->external_name());
 254       }
 255       #endif
 256 #endif
 257     }
 258   }
 259   guarantee(InstanceKlass::cast(k)->contains_field_offset(offset),
 260       "Bug in native code: jfieldID offset must address interior of object");
 261 }
 262 
 263 // Wrapper to trace JNI functions
 264 
 265 #ifdef ASSERT
 266   Histogram* JNIHistogram;
 267   static volatile int JNIHistogram_lock = 0;
 268 
 269   class JNIHistogramElement : public HistogramElement {
 270     public:
 271      JNIHistogramElement(const char* name);
 272   };
 273 
 274   JNIHistogramElement::JNIHistogramElement(const char* elementName) {
 275     _name = elementName;
 276     uintx count = 0;
 277 
 278     while (Atomic::cmpxchg(1, &JNIHistogram_lock, 0) != 0) {
 279       while (OrderAccess::load_acquire(&JNIHistogram_lock) != 0) {
 280         count +=1;
 281         if ( (WarnOnStalledSpinLock > 0)
 282           && (count % WarnOnStalledSpinLock == 0)) {
 283           warning("JNIHistogram_lock seems to be stalled");
 284         }
 285       }
 286      }
 287 
 288 
 289     if(JNIHistogram == NULL)
 290       JNIHistogram = new Histogram("JNI Call Counts",100);
 291 
 292     JNIHistogram->add_element(this);
 293     Atomic::dec(&JNIHistogram_lock);
 294   }
 295 
 296   #define JNICountWrapper(arg)                                     \
 297      static JNIHistogramElement* e = new JNIHistogramElement(arg); \
 298       /* There is a MT-race condition in VC++. So we need to make sure that that e has been initialized */ \
 299      if (e != NULL) e->increment_count()
 300   #define JNIWrapper(arg) JNICountWrapper(arg);
 301 #else
 302   #define JNIWrapper(arg)
 303 #endif
 304 
 305 
 306 // Implementation of JNI entries
 307 
 308 DT_RETURN_MARK_DECL(DefineClass, jclass
 309                     , HOTSPOT_JNI_DEFINECLASS_RETURN(_ret_ref));
 310 
 311 JNI_ENTRY(jclass, jni_DefineClass(JNIEnv *env, const char *name, jobject loaderRef,
 312                                   const jbyte *buf, jsize bufLen))
 313   JNIWrapper("DefineClass");
 314 
 315   HOTSPOT_JNI_DEFINECLASS_ENTRY(
 316     env, (char*) name, loaderRef, (char*) buf, bufLen);
 317 
 318   jclass cls = NULL;
 319   DT_RETURN_MARK(DefineClass, jclass, (const jclass&)cls);
 320 
 321   TempNewSymbol class_name = NULL;
 322   // Since exceptions can be thrown, class initialization can take place
 323   // if name is NULL no check for class name in .class stream has to be made.
 324   if (name != NULL) {
 325     const int str_len = (int)strlen(name);
 326     if (str_len > Symbol::max_length()) {
 327       // It's impossible to create this class;  the name cannot fit
 328       // into the constant pool.
 329       Exceptions::fthrow(THREAD_AND_LOCATION,
 330                          vmSymbols::java_lang_NoClassDefFoundError(),
 331                          "Class name exceeds maximum length of %d: %s",
 332                          Symbol::max_length(),
 333                          name);
 334       return 0;
 335     }
 336     class_name = SymbolTable::new_symbol(name, CHECK_NULL);
 337   }
 338   ResourceMark rm(THREAD);
 339   ClassFileStream st((u1*)buf, bufLen, NULL, ClassFileStream::verify);
 340   Handle class_loader (THREAD, JNIHandles::resolve(loaderRef));
 341 
 342   if (UsePerfData && !class_loader.is_null()) {
 343     // check whether the current caller thread holds the lock or not.
 344     // If not, increment the corresponding counter
 345     if (ObjectSynchronizer::
 346         query_lock_ownership((JavaThread*)THREAD, class_loader) !=
 347         ObjectSynchronizer::owner_self) {
 348       ClassLoader::sync_JNIDefineClassLockFreeCounter()->inc();
 349     }
 350   }
 351   Klass* k = SystemDictionary::resolve_from_stream(class_name,
 352                                                    class_loader,
 353                                                    Handle(),
 354                                                    &st,
 355                                                    CHECK_NULL);
 356 
 357   if (log_is_enabled(Debug, class, resolve) && k != NULL) {
 358     trace_class_resolution(k);
 359   }
 360 
 361   cls = (jclass)JNIHandles::make_local(
 362     env, k->java_mirror());
 363   return cls;
 364 JNI_END
 365 
 366 
 367 
 368 static bool first_time_FindClass = true;
 369 
 370 DT_RETURN_MARK_DECL(FindClass, jclass
 371                     , HOTSPOT_JNI_FINDCLASS_RETURN(_ret_ref));
 372 
 373 JNI_ENTRY(jclass, jni_FindClass(JNIEnv *env, const char *name))
 374   JNIWrapper("FindClass");
 375 
 376   HOTSPOT_JNI_FINDCLASS_ENTRY(env, (char *)name);
 377 
 378   jclass result = NULL;
 379   DT_RETURN_MARK(FindClass, jclass, (const jclass&)result);
 380 
 381   // Remember if we are the first invocation of jni_FindClass
 382   bool first_time = first_time_FindClass;
 383   first_time_FindClass = false;
 384 
 385   // Sanity check the name:  it cannot be null or larger than the maximum size
 386   // name we can fit in the constant pool.
 387   if (name == NULL) {
 388     THROW_MSG_0(vmSymbols::java_lang_NoClassDefFoundError(), "No class name given");
 389   }
 390   if ((int)strlen(name) > Symbol::max_length()) {
 391     Exceptions::fthrow(THREAD_AND_LOCATION,
 392                        vmSymbols::java_lang_NoClassDefFoundError(),
 393                        "Class name exceeds maximum length of %d: %s",
 394                        Symbol::max_length(),
 395                        name);
 396     return 0;
 397   }
 398 
 399   //%note jni_3
 400   Handle protection_domain;
 401   // Find calling class
 402   Klass* k = thread->security_get_caller_class(0);
 403   // default to the system loader when no context
 404   Handle loader(THREAD, SystemDictionary::java_system_loader());
 405   if (k != NULL) {
 406     // Special handling to make sure JNI_OnLoad and JNI_OnUnload are executed
 407     // in the correct class context.
 408     if (k->class_loader() == NULL &&
 409         k->name() == vmSymbols::java_lang_ClassLoader_NativeLibrary()) {
 410       JavaValue result(T_OBJECT);
 411       JavaCalls::call_static(&result, k,
 412                              vmSymbols::getFromClass_name(),
 413                              vmSymbols::void_class_signature(),
 414                              CHECK_NULL);
 415       // When invoked from JNI_OnLoad, NativeLibrary::getFromClass returns
 416       // a non-NULL Class object.  When invoked from JNI_OnUnload,
 417       // it will return NULL to indicate no context.
 418       oop mirror = (oop) result.get_jobject();
 419       if (mirror != NULL) {
 420         Klass* fromClass = java_lang_Class::as_Klass(mirror);
 421         loader = Handle(THREAD, fromClass->class_loader());
 422         protection_domain = Handle(THREAD, fromClass->protection_domain());
 423       }
 424     } else {
 425       loader = Handle(THREAD, k->class_loader());
 426     }
 427   }
 428 
 429   TempNewSymbol sym = SymbolTable::new_symbol(name, CHECK_NULL);
 430   result = find_class_from_class_loader(env, sym, true, loader,
 431                                         protection_domain, true, thread);
 432 
 433   if (log_is_enabled(Debug, class, resolve) && result != NULL) {
 434     trace_class_resolution(java_lang_Class::as_Klass(JNIHandles::resolve_non_null(result)));
 435   }
 436 
 437   // If we were the first invocation of jni_FindClass, we enable compilation again
 438   // rather than just allowing invocation counter to overflow and decay.
 439   // Controlled by flag DelayCompilationDuringStartup.
 440   if (first_time && !CompileTheWorld)
 441     CompilationPolicy::completed_vm_startup();
 442 
 443   return result;
 444 JNI_END
 445 
 446 DT_RETURN_MARK_DECL(FromReflectedMethod, jmethodID
 447                     , HOTSPOT_JNI_FROMREFLECTEDMETHOD_RETURN((uintptr_t)_ret_ref));
 448 
 449 JNI_ENTRY(jmethodID, jni_FromReflectedMethod(JNIEnv *env, jobject method))
 450   JNIWrapper("FromReflectedMethod");
 451 
 452   HOTSPOT_JNI_FROMREFLECTEDMETHOD_ENTRY(env, method);
 453 
 454   jmethodID ret = NULL;
 455   DT_RETURN_MARK(FromReflectedMethod, jmethodID, (const jmethodID&)ret);
 456 
 457   // method is a handle to a java.lang.reflect.Method object
 458   oop reflected  = JNIHandles::resolve_non_null(method);
 459   oop mirror     = NULL;
 460   int slot       = 0;
 461 
 462   if (reflected->klass() == SystemDictionary::reflect_Constructor_klass()) {
 463     mirror = java_lang_reflect_Constructor::clazz(reflected);
 464     slot   = java_lang_reflect_Constructor::slot(reflected);
 465   } else {
 466     assert(reflected->klass() == SystemDictionary::reflect_Method_klass(), "wrong type");
 467     mirror = java_lang_reflect_Method::clazz(reflected);
 468     slot   = java_lang_reflect_Method::slot(reflected);
 469   }
 470   Klass* k1 = java_lang_Class::as_Klass(mirror);
 471 
 472   // Make sure class is initialized before handing id's out to methods
 473   k1->initialize(CHECK_NULL);
 474   Method* m = InstanceKlass::cast(k1)->method_with_idnum(slot);
 475   ret = m==NULL? NULL : m->jmethod_id();  // return NULL if reflected method deleted
 476   return ret;
 477 JNI_END
 478 
 479 DT_RETURN_MARK_DECL(FromReflectedField, jfieldID
 480                     , HOTSPOT_JNI_FROMREFLECTEDFIELD_RETURN((uintptr_t)_ret_ref));
 481 
 482 JNI_ENTRY(jfieldID, jni_FromReflectedField(JNIEnv *env, jobject field))
 483   JNIWrapper("FromReflectedField");
 484 
 485   HOTSPOT_JNI_FROMREFLECTEDFIELD_ENTRY(env, field);
 486 
 487   jfieldID ret = NULL;
 488   DT_RETURN_MARK(FromReflectedField, jfieldID, (const jfieldID&)ret);
 489 
 490   // field is a handle to a java.lang.reflect.Field object
 491   oop reflected   = JNIHandles::resolve_non_null(field);
 492   oop mirror      = java_lang_reflect_Field::clazz(reflected);
 493   Klass* k1       = java_lang_Class::as_Klass(mirror);
 494   int slot        = java_lang_reflect_Field::slot(reflected);
 495   int modifiers   = java_lang_reflect_Field::modifiers(reflected);
 496 
 497   // Make sure class is initialized before handing id's out to fields
 498   k1->initialize(CHECK_NULL);
 499 
 500   // First check if this is a static field
 501   if (modifiers & JVM_ACC_STATIC) {
 502     intptr_t offset = InstanceKlass::cast(k1)->field_offset( slot );
 503     JNIid* id = InstanceKlass::cast(k1)->jni_id_for(offset);
 504     assert(id != NULL, "corrupt Field object");
 505     debug_only(id->set_is_static_field_id();)
 506     // A jfieldID for a static field is a JNIid specifying the field holder and the offset within the Klass*
 507     ret = jfieldIDWorkaround::to_static_jfieldID(id);
 508     return ret;
 509   }
 510 
 511   // The slot is the index of the field description in the field-array
 512   // The jfieldID is the offset of the field within the object
 513   // It may also have hash bits for k, if VerifyJNIFields is turned on.
 514   intptr_t offset = InstanceKlass::cast(k1)->field_offset( slot );
 515   assert(InstanceKlass::cast(k1)->contains_field_offset(offset), "stay within object");
 516   ret = jfieldIDWorkaround::to_instance_jfieldID(k1, offset);
 517   return ret;
 518 JNI_END
 519 
 520 
 521 DT_RETURN_MARK_DECL(ToReflectedMethod, jobject
 522                     , HOTSPOT_JNI_TOREFLECTEDMETHOD_RETURN(_ret_ref));
 523 
 524 JNI_ENTRY(jobject, jni_ToReflectedMethod(JNIEnv *env, jclass cls, jmethodID method_id, jboolean isStatic))
 525   JNIWrapper("ToReflectedMethod");
 526 
 527   HOTSPOT_JNI_TOREFLECTEDMETHOD_ENTRY(env, cls, (uintptr_t) method_id, isStatic);
 528 
 529   jobject ret = NULL;
 530   DT_RETURN_MARK(ToReflectedMethod, jobject, (const jobject&)ret);
 531 
 532   methodHandle m (THREAD, Method::resolve_jmethod_id(method_id));
 533   assert(m->is_static() == (isStatic != 0), "jni_ToReflectedMethod access flags doesn't match");
 534   oop reflection_method;
 535   if (m->is_initializer()) {
 536     reflection_method = Reflection::new_constructor(m, CHECK_NULL);
 537   } else {
 538     reflection_method = Reflection::new_method(m, false, CHECK_NULL);
 539   }
 540   ret = JNIHandles::make_local(env, reflection_method);
 541   return ret;
 542 JNI_END
 543 
 544 DT_RETURN_MARK_DECL(GetSuperclass, jclass
 545                     , HOTSPOT_JNI_GETSUPERCLASS_RETURN(_ret_ref));
 546 
 547 JNI_ENTRY(jclass, jni_GetSuperclass(JNIEnv *env, jclass sub))
 548   JNIWrapper("GetSuperclass");
 549 
 550   HOTSPOT_JNI_GETSUPERCLASS_ENTRY(env, sub);
 551 
 552   jclass obj = NULL;
 553   DT_RETURN_MARK(GetSuperclass, jclass, (const jclass&)obj);
 554 
 555   oop mirror = JNIHandles::resolve_non_null(sub);
 556   // primitive classes return NULL
 557   if (java_lang_Class::is_primitive(mirror)) return NULL;
 558 
 559   // Rules of Class.getSuperClass as implemented by KLass::java_super:
 560   // arrays return Object
 561   // interfaces return NULL
 562   // proper classes return Klass::super()
 563   Klass* k = java_lang_Class::as_Klass(mirror);
 564   if (k->is_interface()) return NULL;
 565 
 566   // return mirror for superclass
 567   Klass* super = k->java_super();
 568   // super2 is the value computed by the compiler's getSuperClass intrinsic:
 569   debug_only(Klass* super2 = ( k->is_array_klass()
 570                                  ? SystemDictionary::Object_klass()
 571                                  : k->super() ) );
 572   assert(super == super2,
 573          "java_super computation depends on interface, array, other super");
 574   obj = (super == NULL) ? NULL : (jclass) JNIHandles::make_local(super->java_mirror());
 575   return obj;
 576 JNI_END
 577 
 578 JNI_QUICK_ENTRY(jboolean, jni_IsAssignableFrom(JNIEnv *env, jclass sub, jclass super))
 579   JNIWrapper("IsSubclassOf");
 580 
 581   HOTSPOT_JNI_ISASSIGNABLEFROM_ENTRY(env, sub, super);
 582 
 583   oop sub_mirror   = JNIHandles::resolve_non_null(sub);
 584   oop super_mirror = JNIHandles::resolve_non_null(super);
 585   if (java_lang_Class::is_primitive(sub_mirror) ||
 586       java_lang_Class::is_primitive(super_mirror)) {
 587     jboolean ret = oopDesc::equals(sub_mirror, super_mirror);
 588 
 589     HOTSPOT_JNI_ISASSIGNABLEFROM_RETURN(ret);
 590     return ret;
 591   }
 592   Klass* sub_klass   = java_lang_Class::as_Klass(sub_mirror);
 593   Klass* super_klass = java_lang_Class::as_Klass(super_mirror);
 594   assert(sub_klass != NULL && super_klass != NULL, "invalid arguments to jni_IsAssignableFrom");
 595   jboolean ret = sub_klass->is_subtype_of(super_klass) ?
 596                    JNI_TRUE : JNI_FALSE;
 597 
 598   HOTSPOT_JNI_ISASSIGNABLEFROM_RETURN(ret);
 599   return ret;
 600 JNI_END
 601 
 602 
 603 DT_RETURN_MARK_DECL(Throw, jint
 604                     , HOTSPOT_JNI_THROW_RETURN(_ret_ref));
 605 
 606 JNI_ENTRY(jint, jni_Throw(JNIEnv *env, jthrowable obj))
 607   JNIWrapper("Throw");
 608 
 609   HOTSPOT_JNI_THROW_ENTRY(env, obj);
 610 
 611   jint ret = JNI_OK;
 612   DT_RETURN_MARK(Throw, jint, (const jint&)ret);
 613 
 614   THROW_OOP_(JNIHandles::resolve(obj), JNI_OK);
 615   ShouldNotReachHere();
 616   return 0;  // Mute compiler.
 617 JNI_END
 618 
 619 
 620 DT_RETURN_MARK_DECL(ThrowNew, jint
 621                     , HOTSPOT_JNI_THROWNEW_RETURN(_ret_ref));
 622 
 623 JNI_ENTRY(jint, jni_ThrowNew(JNIEnv *env, jclass clazz, const char *message))
 624   JNIWrapper("ThrowNew");
 625 
 626   HOTSPOT_JNI_THROWNEW_ENTRY(env, clazz, (char *) message);
 627 
 628   jint ret = JNI_OK;
 629   DT_RETURN_MARK(ThrowNew, jint, (const jint&)ret);
 630 
 631   InstanceKlass* k = InstanceKlass::cast(java_lang_Class::as_Klass(JNIHandles::resolve_non_null(clazz)));
 632   Symbol*  name = k->name();
 633   Handle class_loader (THREAD,  k->class_loader());
 634   Handle protection_domain (THREAD, k->protection_domain());
 635   THROW_MSG_LOADER_(name, (char *)message, class_loader, protection_domain, JNI_OK);
 636   ShouldNotReachHere();
 637   return 0;  // Mute compiler.
 638 JNI_END
 639 
 640 
 641 // JNI functions only transform a pending async exception to a synchronous
 642 // exception in ExceptionOccurred and ExceptionCheck calls, since
 643 // delivering an async exception in other places won't change the native
 644 // code's control flow and would be harmful when native code further calls
 645 // JNI functions with a pending exception. Async exception is also checked
 646 // during the call, so ExceptionOccurred/ExceptionCheck won't return
 647 // false but deliver the async exception at the very end during
 648 // state transition.
 649 
 650 static void jni_check_async_exceptions(JavaThread *thread) {
 651   assert(thread == Thread::current(), "must be itself");
 652   thread->check_and_handle_async_exceptions();
 653 }
 654 
 655 JNI_ENTRY_NO_PRESERVE(jthrowable, jni_ExceptionOccurred(JNIEnv *env))
 656   JNIWrapper("ExceptionOccurred");
 657 
 658   HOTSPOT_JNI_EXCEPTIONOCCURRED_ENTRY(env);
 659 
 660   jni_check_async_exceptions(thread);
 661   oop exception = thread->pending_exception();
 662   jthrowable ret = (jthrowable) JNIHandles::make_local(env, exception);
 663 
 664   HOTSPOT_JNI_EXCEPTIONOCCURRED_RETURN(ret);
 665   return ret;
 666 JNI_END
 667 
 668 
 669 JNI_ENTRY_NO_PRESERVE(void, jni_ExceptionDescribe(JNIEnv *env))
 670   JNIWrapper("ExceptionDescribe");
 671 
 672   HOTSPOT_JNI_EXCEPTIONDESCRIBE_ENTRY(env);
 673 
 674   if (thread->has_pending_exception()) {
 675     Handle ex(thread, thread->pending_exception());
 676     thread->clear_pending_exception();
 677     if (ex->is_a(SystemDictionary::ThreadDeath_klass())) {
 678       // Don't print anything if we are being killed.
 679     } else {
 680       jio_fprintf(defaultStream::error_stream(), "Exception ");
 681       if (thread != NULL && thread->threadObj() != NULL) {
 682         ResourceMark rm(THREAD);
 683         jio_fprintf(defaultStream::error_stream(),
 684         "in thread \"%s\" ", thread->get_thread_name());
 685       }
 686       if (ex->is_a(SystemDictionary::Throwable_klass())) {
 687         JavaValue result(T_VOID);
 688         JavaCalls::call_virtual(&result,
 689                                 ex,
 690                                 SystemDictionary::Throwable_klass(),
 691                                 vmSymbols::printStackTrace_name(),
 692                                 vmSymbols::void_method_signature(),
 693                                 THREAD);
 694         // If an exception is thrown in the call it gets thrown away. Not much
 695         // we can do with it. The native code that calls this, does not check
 696         // for the exception - hence, it might still be in the thread when DestroyVM gets
 697         // called, potentially causing a few asserts to trigger - since no pending exception
 698         // is expected.
 699         CLEAR_PENDING_EXCEPTION;
 700       } else {
 701         ResourceMark rm(THREAD);
 702         jio_fprintf(defaultStream::error_stream(),
 703         ". Uncaught exception of type %s.",
 704         ex->klass()->external_name());
 705       }
 706     }
 707   }
 708 
 709   HOTSPOT_JNI_EXCEPTIONDESCRIBE_RETURN();
 710 JNI_END
 711 
 712 
 713 JNI_QUICK_ENTRY(void, jni_ExceptionClear(JNIEnv *env))
 714   JNIWrapper("ExceptionClear");
 715 
 716   HOTSPOT_JNI_EXCEPTIONCLEAR_ENTRY(env);
 717 
 718   // The jni code might be using this API to clear java thrown exception.
 719   // So just mark jvmti thread exception state as exception caught.
 720   JvmtiThreadState *state = JavaThread::current()->jvmti_thread_state();
 721   if (state != NULL && state->is_exception_detected()) {
 722     state->set_exception_caught();
 723   }
 724   thread->clear_pending_exception();
 725 
 726   HOTSPOT_JNI_EXCEPTIONCLEAR_RETURN();
 727 JNI_END
 728 
 729 
 730 JNI_ENTRY(void, jni_FatalError(JNIEnv *env, const char *msg))
 731   JNIWrapper("FatalError");
 732 
 733   HOTSPOT_JNI_FATALERROR_ENTRY(env, (char *) msg);
 734 
 735   tty->print_cr("FATAL ERROR in native method: %s", msg);
 736   thread->print_stack();
 737   os::abort(); // Dump core and abort
 738 JNI_END
 739 
 740 
 741 JNI_ENTRY(jint, jni_PushLocalFrame(JNIEnv *env, jint capacity))
 742   JNIWrapper("PushLocalFrame");
 743 
 744   HOTSPOT_JNI_PUSHLOCALFRAME_ENTRY(env, capacity);
 745 
 746   //%note jni_11
 747   if (capacity < 0 ||
 748       ((MaxJNILocalCapacity > 0) && (capacity > MaxJNILocalCapacity))) {
 749     HOTSPOT_JNI_PUSHLOCALFRAME_RETURN((uint32_t)JNI_ERR);
 750     return JNI_ERR;
 751   }
 752   JNIHandleBlock* old_handles = thread->active_handles();
 753   JNIHandleBlock* new_handles = JNIHandleBlock::allocate_block(thread);
 754   assert(new_handles != NULL, "should not be NULL");
 755   new_handles->set_pop_frame_link(old_handles);
 756   thread->set_active_handles(new_handles);
 757   jint ret = JNI_OK;
 758   HOTSPOT_JNI_PUSHLOCALFRAME_RETURN(ret);
 759   return ret;
 760 JNI_END
 761 
 762 
 763 JNI_ENTRY(jobject, jni_PopLocalFrame(JNIEnv *env, jobject result))
 764   JNIWrapper("PopLocalFrame");
 765 
 766   HOTSPOT_JNI_POPLOCALFRAME_ENTRY(env, result);
 767 
 768   //%note jni_11
 769   Handle result_handle(thread, JNIHandles::resolve(result));
 770   JNIHandleBlock* old_handles = thread->active_handles();
 771   JNIHandleBlock* new_handles = old_handles->pop_frame_link();
 772   if (new_handles != NULL) {
 773     // As a sanity check we only release the handle blocks if the pop_frame_link is not NULL.
 774     // This way code will still work if PopLocalFrame is called without a corresponding
 775     // PushLocalFrame call. Note that we set the pop_frame_link to NULL explicitly, otherwise
 776     // the release_block call will release the blocks.
 777     thread->set_active_handles(new_handles);
 778     old_handles->set_pop_frame_link(NULL);              // clear link we won't release new_handles below
 779     JNIHandleBlock::release_block(old_handles, thread); // may block
 780     result = JNIHandles::make_local(thread, result_handle());
 781   }
 782   HOTSPOT_JNI_POPLOCALFRAME_RETURN(result);
 783   return result;
 784 JNI_END
 785 
 786 
 787 JNI_ENTRY(jobject, jni_NewGlobalRef(JNIEnv *env, jobject ref))
 788   JNIWrapper("NewGlobalRef");
 789 
 790   HOTSPOT_JNI_NEWGLOBALREF_ENTRY(env, ref);
 791 
 792   Handle ref_handle(thread, JNIHandles::resolve(ref));
 793   jobject ret = JNIHandles::make_global(ref_handle);
 794 
 795   HOTSPOT_JNI_NEWGLOBALREF_RETURN(ret);
 796   return ret;
 797 JNI_END
 798 
 799 // Must be JNI_ENTRY (with HandleMark)
 800 JNI_ENTRY_NO_PRESERVE(void, jni_DeleteGlobalRef(JNIEnv *env, jobject ref))
 801   JNIWrapper("DeleteGlobalRef");
 802 
 803   HOTSPOT_JNI_DELETEGLOBALREF_ENTRY(env, ref);
 804 
 805   JNIHandles::destroy_global(ref);
 806 
 807   HOTSPOT_JNI_DELETEGLOBALREF_RETURN();
 808 JNI_END
 809 
 810 JNI_QUICK_ENTRY(void, jni_DeleteLocalRef(JNIEnv *env, jobject obj))
 811   JNIWrapper("DeleteLocalRef");
 812 
 813   HOTSPOT_JNI_DELETELOCALREF_ENTRY(env, obj);
 814 
 815   JNIHandles::destroy_local(obj);
 816 
 817   HOTSPOT_JNI_DELETELOCALREF_RETURN();
 818 JNI_END
 819 
 820 JNI_QUICK_ENTRY(jboolean, jni_IsSameObject(JNIEnv *env, jobject r1, jobject r2))
 821   JNIWrapper("IsSameObject");
 822 
 823   HOTSPOT_JNI_ISSAMEOBJECT_ENTRY(env, r1, r2);
 824 
 825   oop a = JNIHandles::resolve(r1);
 826   oop b = JNIHandles::resolve(r2);
 827   jboolean ret = oopDesc::equals(a, b) ? JNI_TRUE : JNI_FALSE;
 828 
 829   HOTSPOT_JNI_ISSAMEOBJECT_RETURN(ret);
 830   return ret;
 831 JNI_END
 832 
 833 
 834 JNI_ENTRY(jobject, jni_NewLocalRef(JNIEnv *env, jobject ref))
 835   JNIWrapper("NewLocalRef");
 836 
 837   HOTSPOT_JNI_NEWLOCALREF_ENTRY(env, ref);
 838 
 839   jobject ret = JNIHandles::make_local(env, JNIHandles::resolve(ref));
 840 
 841   HOTSPOT_JNI_NEWLOCALREF_RETURN(ret);
 842   return ret;
 843 JNI_END
 844 
 845 JNI_LEAF(jint, jni_EnsureLocalCapacity(JNIEnv *env, jint capacity))
 846   JNIWrapper("EnsureLocalCapacity");
 847 
 848   HOTSPOT_JNI_ENSURELOCALCAPACITY_ENTRY(env, capacity);
 849 
 850   jint ret;
 851   if (capacity >= 0 &&
 852       ((MaxJNILocalCapacity <= 0) || (capacity <= MaxJNILocalCapacity))) {
 853     ret = JNI_OK;
 854   } else {
 855     ret = JNI_ERR;
 856   }
 857 
 858   HOTSPOT_JNI_ENSURELOCALCAPACITY_RETURN(ret);
 859   return ret;
 860 JNI_END
 861 
 862 // Return the Handle Type
 863 JNI_LEAF(jobjectRefType, jni_GetObjectRefType(JNIEnv *env, jobject obj))
 864   JNIWrapper("GetObjectRefType");
 865 
 866   HOTSPOT_JNI_GETOBJECTREFTYPE_ENTRY(env, obj);
 867 
 868   jobjectRefType ret = JNIInvalidRefType;
 869   if (obj != NULL) {
 870     ret = JNIHandles::handle_type(thread, obj);
 871   }
 872 
 873   HOTSPOT_JNI_GETOBJECTREFTYPE_RETURN((void *) ret);
 874   return ret;
 875 JNI_END
 876 
 877 
 878 class JNI_ArgumentPusher : public SignatureIterator {
 879  protected:
 880   JavaCallArguments*  _arguments;
 881 
 882   virtual void get_bool   () = 0;
 883   virtual void get_char   () = 0;
 884   virtual void get_short  () = 0;
 885   virtual void get_byte   () = 0;
 886   virtual void get_int    () = 0;
 887   virtual void get_long   () = 0;
 888   virtual void get_float  () = 0;
 889   virtual void get_double () = 0;
 890   virtual void get_object () = 0;
 891 
 892   JNI_ArgumentPusher(Symbol* signature) : SignatureIterator(signature) {
 893     this->_return_type = T_ILLEGAL;
 894     _arguments = NULL;
 895   }
 896 
 897  public:
 898   virtual void iterate( uint64_t fingerprint ) = 0;
 899 
 900   void set_java_argument_object(JavaCallArguments *arguments) { _arguments = arguments; }
 901 
 902   inline void do_bool()                     { if (!is_return_type()) get_bool();   }
 903   inline void do_char()                     { if (!is_return_type()) get_char();   }
 904   inline void do_short()                    { if (!is_return_type()) get_short();  }
 905   inline void do_byte()                     { if (!is_return_type()) get_byte();   }
 906   inline void do_int()                      { if (!is_return_type()) get_int();    }
 907   inline void do_long()                     { if (!is_return_type()) get_long();   }
 908   inline void do_float()                    { if (!is_return_type()) get_float();  }
 909   inline void do_double()                   { if (!is_return_type()) get_double(); }
 910   inline void do_object(int begin, int end) { if (!is_return_type()) get_object(); }
 911   inline void do_array(int begin, int end)  { if (!is_return_type()) get_object(); } // do_array uses get_object -- there is no get_array
 912   inline void do_void()                     { }
 913 
 914   JavaCallArguments* arguments()     { return _arguments; }
 915   void push_receiver(Handle h)       { _arguments->push_oop(h); }
 916 };
 917 
 918 
 919 class JNI_ArgumentPusherVaArg : public JNI_ArgumentPusher {
 920  protected:
 921   va_list _ap;
 922 
 923   inline void get_bool()   {
 924     // Normalize boolean arguments from native code by converting 1-255 to JNI_TRUE and
 925     // 0 to JNI_FALSE.  Boolean return values from native are normalized the same in
 926     // TemplateInterpreterGenerator::generate_result_handler_for and
 927     // SharedRuntime::generate_native_wrapper.
 928     jboolean b = va_arg(_ap, jint);
 929     _arguments->push_int((jint)(b == 0 ? JNI_FALSE : JNI_TRUE));
 930   }
 931   inline void get_char()   { _arguments->push_int(va_arg(_ap, jint)); } // char is coerced to int when using va_arg
 932   inline void get_short()  { _arguments->push_int(va_arg(_ap, jint)); } // short is coerced to int when using va_arg
 933   inline void get_byte()   { _arguments->push_int(va_arg(_ap, jint)); } // byte is coerced to int when using va_arg
 934   inline void get_int()    { _arguments->push_int(va_arg(_ap, jint)); }
 935 
 936   // each of these paths is exercized by the various jck Call[Static,Nonvirtual,][Void,Int,..]Method[A,V,] tests
 937 
 938   inline void get_long()   { _arguments->push_long(va_arg(_ap, jlong)); }
 939   inline void get_float()  { _arguments->push_float((jfloat)va_arg(_ap, jdouble)); } // float is coerced to double w/ va_arg
 940   inline void get_double() { _arguments->push_double(va_arg(_ap, jdouble)); }
 941   inline void get_object() { _arguments->push_jobject(va_arg(_ap, jobject)); }
 942 
 943   inline void set_ap(va_list rap) {
 944     va_copy(_ap, rap);
 945   }
 946 
 947  public:
 948   JNI_ArgumentPusherVaArg(Symbol* signature, va_list rap)
 949        : JNI_ArgumentPusher(signature) {
 950     set_ap(rap);
 951   }
 952   JNI_ArgumentPusherVaArg(jmethodID method_id, va_list rap)
 953       : JNI_ArgumentPusher(Method::resolve_jmethod_id(method_id)->signature()) {
 954     set_ap(rap);
 955   }
 956 
 957   // Optimized path if we have the bitvector form of signature
 958   void iterate( uint64_t fingerprint ) {
 959     if (fingerprint == (uint64_t)CONST64(-1)) {
 960       SignatureIterator::iterate(); // Must be too many arguments
 961     } else {
 962       _return_type = (BasicType)((fingerprint >> static_feature_size) &
 963                                   result_feature_mask);
 964 
 965       assert(fingerprint, "Fingerprint should not be 0");
 966       fingerprint = fingerprint >> (static_feature_size + result_feature_size);
 967       while ( 1 ) {
 968         switch ( fingerprint & parameter_feature_mask ) {
 969           case bool_parm:
 970             get_bool();
 971             break;
 972           case char_parm:
 973             get_char();
 974             break;
 975           case short_parm:
 976             get_short();
 977             break;
 978           case byte_parm:
 979             get_byte();
 980             break;
 981           case int_parm:
 982             get_int();
 983             break;
 984           case obj_parm:
 985             get_object();
 986             break;
 987           case long_parm:
 988             get_long();
 989             break;
 990           case float_parm:
 991             get_float();
 992             break;
 993           case double_parm:
 994             get_double();
 995             break;
 996           case done_parm:
 997             return;
 998             break;
 999           default:
1000             ShouldNotReachHere();
1001             break;
1002         }
1003         fingerprint >>= parameter_feature_size;
1004       }
1005     }
1006   }
1007 };
1008 
1009 
1010 class JNI_ArgumentPusherArray : public JNI_ArgumentPusher {
1011  protected:
1012   const jvalue *_ap;
1013 
1014   inline void get_bool()   {
1015     // Normalize boolean arguments from native code by converting 1-255 to JNI_TRUE and
1016     // 0 to JNI_FALSE.  Boolean return values from native are normalized the same in
1017     // TemplateInterpreterGenerator::generate_result_handler_for and
1018     // SharedRuntime::generate_native_wrapper.
1019     jboolean b = (_ap++)->z;
1020     _arguments->push_int((jint)(b == 0 ? JNI_FALSE : JNI_TRUE));
1021   }
1022   inline void get_char()   { _arguments->push_int((jint)(_ap++)->c); }
1023   inline void get_short()  { _arguments->push_int((jint)(_ap++)->s); }
1024   inline void get_byte()   { _arguments->push_int((jint)(_ap++)->b); }
1025   inline void get_int()    { _arguments->push_int((jint)(_ap++)->i); }
1026 
1027   inline void get_long()   { _arguments->push_long((_ap++)->j);  }
1028   inline void get_float()  { _arguments->push_float((_ap++)->f); }
1029   inline void get_double() { _arguments->push_double((_ap++)->d);}
1030   inline void get_object() { _arguments->push_jobject((_ap++)->l); }
1031 
1032   inline void set_ap(const jvalue *rap) { _ap = rap; }
1033 
1034  public:
1035   JNI_ArgumentPusherArray(Symbol* signature, const jvalue *rap)
1036        : JNI_ArgumentPusher(signature) {
1037     set_ap(rap);
1038   }
1039   JNI_ArgumentPusherArray(jmethodID method_id, const jvalue *rap)
1040       : JNI_ArgumentPusher(Method::resolve_jmethod_id(method_id)->signature()) {
1041     set_ap(rap);
1042   }
1043 
1044   // Optimized path if we have the bitvector form of signature
1045   void iterate( uint64_t fingerprint ) {
1046     if (fingerprint == (uint64_t)CONST64(-1)) {
1047       SignatureIterator::iterate(); // Must be too many arguments
1048     } else {
1049       _return_type = (BasicType)((fingerprint >> static_feature_size) &
1050                                   result_feature_mask);
1051       assert(fingerprint, "Fingerprint should not be 0");
1052       fingerprint = fingerprint >> (static_feature_size + result_feature_size);
1053       while ( 1 ) {
1054         switch ( fingerprint & parameter_feature_mask ) {
1055           case bool_parm:
1056             get_bool();
1057             break;
1058           case char_parm:
1059             get_char();
1060             break;
1061           case short_parm:
1062             get_short();
1063             break;
1064           case byte_parm:
1065             get_byte();
1066             break;
1067           case int_parm:
1068             get_int();
1069             break;
1070           case obj_parm:
1071             get_object();
1072             break;
1073           case long_parm:
1074             get_long();
1075             break;
1076           case float_parm:
1077             get_float();
1078             break;
1079           case double_parm:
1080             get_double();
1081             break;
1082           case done_parm:
1083             return;
1084             break;
1085           default:
1086             ShouldNotReachHere();
1087             break;
1088         }
1089         fingerprint >>= parameter_feature_size;
1090       }
1091     }
1092   }
1093 };
1094 
1095 
1096 enum JNICallType {
1097   JNI_STATIC,
1098   JNI_VIRTUAL,
1099   JNI_NONVIRTUAL
1100 };
1101 
1102 
1103 
1104 static void jni_invoke_static(JNIEnv *env, JavaValue* result, jobject receiver, JNICallType call_type, jmethodID method_id, JNI_ArgumentPusher *args, TRAPS) {
1105   methodHandle method(THREAD, Method::resolve_jmethod_id(method_id));
1106 
1107   // Create object to hold arguments for the JavaCall, and associate it with
1108   // the jni parser
1109   ResourceMark rm(THREAD);
1110   int number_of_parameters = method->size_of_parameters();
1111   JavaCallArguments java_args(number_of_parameters);
1112   args->set_java_argument_object(&java_args);
1113 
1114   assert(method->is_static(), "method should be static");
1115 
1116   // Fill out JavaCallArguments object
1117   args->iterate( Fingerprinter(method).fingerprint() );
1118   // Initialize result type
1119   result->set_type(args->get_ret_type());
1120 
1121   // Invoke the method. Result is returned as oop.
1122   JavaCalls::call(result, method, &java_args, CHECK);
1123 
1124   // Convert result
1125   if (result->get_type() == T_OBJECT || result->get_type() == T_ARRAY) {
1126     result->set_jobject(JNIHandles::make_local(env, (oop) result->get_jobject()));
1127   }
1128 }
1129 
1130 
1131 static void jni_invoke_nonstatic(JNIEnv *env, JavaValue* result, jobject receiver, JNICallType call_type, jmethodID method_id, JNI_ArgumentPusher *args, TRAPS) {
1132   oop recv = JNIHandles::resolve(receiver);
1133   if (recv == NULL) {
1134     THROW(vmSymbols::java_lang_NullPointerException());
1135   }
1136   Handle h_recv(THREAD, recv);
1137 
1138   int number_of_parameters;
1139   Method* selected_method;
1140   {
1141     Method* m = Method::resolve_jmethod_id(method_id);
1142     number_of_parameters = m->size_of_parameters();
1143     Klass* holder = m->method_holder();
1144     if (call_type != JNI_VIRTUAL) {
1145         selected_method = m;
1146     } else if (!m->has_itable_index()) {
1147       // non-interface call -- for that little speed boost, don't handlize
1148       debug_only(NoSafepointVerifier nosafepoint;)
1149       // jni_GetMethodID makes sure class is linked and initialized
1150       // so m should have a valid vtable index.
1151       assert(m->valid_vtable_index(), "no valid vtable index");
1152       int vtbl_index = m->vtable_index();
1153       if (vtbl_index != Method::nonvirtual_vtable_index) {
1154         selected_method = h_recv->klass()->method_at_vtable(vtbl_index);
1155       } else {
1156         // final method
1157         selected_method = m;
1158       }
1159     } else {
1160       // interface call
1161       int itbl_index = m->itable_index();
1162       Klass* k = h_recv->klass();
1163       selected_method = InstanceKlass::cast(k)->method_at_itable(holder, itbl_index, CHECK);
1164     }
1165   }
1166 
1167   methodHandle method(THREAD, selected_method);
1168 
1169   // Create object to hold arguments for the JavaCall, and associate it with
1170   // the jni parser
1171   ResourceMark rm(THREAD);
1172   JavaCallArguments java_args(number_of_parameters);
1173   args->set_java_argument_object(&java_args);
1174 
1175   // handle arguments
1176   assert(!method->is_static(), "method %s should not be static", method->name_and_sig_as_C_string());
1177   args->push_receiver(h_recv); // Push jobject handle
1178 
1179   // Fill out JavaCallArguments object
1180   args->iterate( Fingerprinter(method).fingerprint() );
1181   // Initialize result type
1182   result->set_type(args->get_ret_type());
1183 
1184   // Invoke the method. Result is returned as oop.
1185   JavaCalls::call(result, method, &java_args, CHECK);
1186 
1187   // Convert result
1188   if (result->get_type() == T_OBJECT || result->get_type() == T_ARRAY) {
1189     result->set_jobject(JNIHandles::make_local(env, (oop) result->get_jobject()));
1190   }
1191 }
1192 
1193 
1194 static instanceOop alloc_object(jclass clazz, TRAPS) {
1195   Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(clazz));
1196   if (k == NULL) {
1197     ResourceMark rm(THREAD);
1198     THROW_(vmSymbols::java_lang_InstantiationException(), NULL);
1199   }
1200   k->check_valid_for_instantiation(false, CHECK_NULL);
1201   k->initialize(CHECK_NULL);
1202   instanceOop ih = InstanceKlass::cast(k)->allocate_instance(THREAD);
1203   return ih;
1204 }
1205 
1206 DT_RETURN_MARK_DECL(AllocObject, jobject
1207                     , HOTSPOT_JNI_ALLOCOBJECT_RETURN(_ret_ref));
1208 
1209 JNI_ENTRY(jobject, jni_AllocObject(JNIEnv *env, jclass clazz))
1210   JNIWrapper("AllocObject");
1211 
1212   HOTSPOT_JNI_ALLOCOBJECT_ENTRY(env, clazz);
1213 
1214   jobject ret = NULL;
1215   DT_RETURN_MARK(AllocObject, jobject, (const jobject&)ret);
1216 
1217   instanceOop i = alloc_object(clazz, CHECK_NULL);
1218   ret = JNIHandles::make_local(env, i);
1219   return ret;
1220 JNI_END
1221 
1222 DT_RETURN_MARK_DECL(NewObjectA, jobject
1223                     , HOTSPOT_JNI_NEWOBJECTA_RETURN(_ret_ref));
1224 
1225 JNI_ENTRY(jobject, jni_NewObjectA(JNIEnv *env, jclass clazz, jmethodID methodID, const jvalue *args))
1226   JNIWrapper("NewObjectA");
1227 
1228   HOTSPOT_JNI_NEWOBJECTA_ENTRY(env, clazz, (uintptr_t) methodID);
1229 
1230   jobject obj = NULL;
1231   DT_RETURN_MARK(NewObjectA, jobject, (const jobject)obj);
1232 
1233   instanceOop i = alloc_object(clazz, CHECK_NULL);
1234   obj = JNIHandles::make_local(env, i);
1235   JavaValue jvalue(T_VOID);
1236   JNI_ArgumentPusherArray ap(methodID, args);
1237   jni_invoke_nonstatic(env, &jvalue, obj, JNI_NONVIRTUAL, methodID, &ap, CHECK_NULL);
1238   return obj;
1239 JNI_END
1240 
1241 
1242 DT_RETURN_MARK_DECL(NewObjectV, jobject
1243                     , HOTSPOT_JNI_NEWOBJECTV_RETURN(_ret_ref));
1244 
1245 JNI_ENTRY(jobject, jni_NewObjectV(JNIEnv *env, jclass clazz, jmethodID methodID, va_list args))
1246   JNIWrapper("NewObjectV");
1247 
1248   HOTSPOT_JNI_NEWOBJECTV_ENTRY(env, clazz, (uintptr_t) methodID);
1249 
1250   jobject obj = NULL;
1251   DT_RETURN_MARK(NewObjectV, jobject, (const jobject&)obj);
1252 
1253   instanceOop i = alloc_object(clazz, CHECK_NULL);
1254   obj = JNIHandles::make_local(env, i);
1255   JavaValue jvalue(T_VOID);
1256   JNI_ArgumentPusherVaArg ap(methodID, args);
1257   jni_invoke_nonstatic(env, &jvalue, obj, JNI_NONVIRTUAL, methodID, &ap, CHECK_NULL);
1258   return obj;
1259 JNI_END
1260 
1261 
1262 DT_RETURN_MARK_DECL(NewObject, jobject
1263                     , HOTSPOT_JNI_NEWOBJECT_RETURN(_ret_ref));
1264 
1265 JNI_ENTRY(jobject, jni_NewObject(JNIEnv *env, jclass clazz, jmethodID methodID, ...))
1266   JNIWrapper("NewObject");
1267 
1268   HOTSPOT_JNI_NEWOBJECT_ENTRY(env, clazz, (uintptr_t) methodID);
1269 
1270   jobject obj = NULL;
1271   DT_RETURN_MARK(NewObject, jobject, (const jobject&)obj);
1272 
1273   instanceOop i = alloc_object(clazz, CHECK_NULL);
1274   obj = JNIHandles::make_local(env, i);
1275   va_list args;
1276   va_start(args, methodID);
1277   JavaValue jvalue(T_VOID);
1278   JNI_ArgumentPusherVaArg ap(methodID, args);
1279   jni_invoke_nonstatic(env, &jvalue, obj, JNI_NONVIRTUAL, methodID, &ap, CHECK_NULL);
1280   va_end(args);
1281   return obj;
1282 JNI_END
1283 
1284 
1285 JNI_ENTRY(jclass, jni_GetObjectClass(JNIEnv *env, jobject obj))
1286   JNIWrapper("GetObjectClass");
1287 
1288   HOTSPOT_JNI_GETOBJECTCLASS_ENTRY(env, obj);
1289 
1290   Klass* k = JNIHandles::resolve_non_null(obj)->klass();
1291   jclass ret =
1292     (jclass) JNIHandles::make_local(env, k->java_mirror());
1293 
1294   HOTSPOT_JNI_GETOBJECTCLASS_RETURN(ret);
1295   return ret;
1296 JNI_END
1297 
1298 JNI_QUICK_ENTRY(jboolean, jni_IsInstanceOf(JNIEnv *env, jobject obj, jclass clazz))
1299   JNIWrapper("IsInstanceOf");
1300 
1301   HOTSPOT_JNI_ISINSTANCEOF_ENTRY(env, obj, clazz);
1302 
1303   jboolean ret = JNI_TRUE;
1304   if (obj != NULL) {
1305     ret = JNI_FALSE;
1306     Klass* k = java_lang_Class::as_Klass(
1307       JNIHandles::resolve_non_null(clazz));
1308     if (k != NULL) {
1309       ret = JNIHandles::resolve_non_null(obj)->is_a(k) ? JNI_TRUE : JNI_FALSE;
1310     }
1311   }
1312 
1313   HOTSPOT_JNI_ISINSTANCEOF_RETURN(ret);
1314   return ret;
1315 JNI_END
1316 
1317 
1318 static jmethodID get_method_id(JNIEnv *env, jclass clazz, const char *name_str,
1319                                const char *sig, bool is_static, TRAPS) {
1320   // %%%% This code should probably just call into a method in the LinkResolver
1321   //
1322   // The class should have been loaded (we have an instance of the class
1323   // passed in) so the method and signature should already be in the symbol
1324   // table.  If they're not there, the method doesn't exist.
1325   const char *name_to_probe = (name_str == NULL)
1326                         ? vmSymbols::object_initializer_name()->as_C_string()
1327                         : name_str;
1328   TempNewSymbol name = SymbolTable::probe(name_to_probe, (int)strlen(name_to_probe));
1329   TempNewSymbol signature = SymbolTable::probe(sig, (int)strlen(sig));
1330 
1331   if (name == NULL || signature == NULL) {
1332     THROW_MSG_0(vmSymbols::java_lang_NoSuchMethodError(), name_str);
1333   }
1334 
1335   // Throw a NoSuchMethodError exception if we have an instance of a
1336   // primitive java.lang.Class
1337   if (java_lang_Class::is_primitive(JNIHandles::resolve_non_null(clazz))) {
1338     THROW_MSG_0(vmSymbols::java_lang_NoSuchMethodError(), name_str);
1339   }
1340 
1341   Klass* klass = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(clazz));
1342 
1343   // Make sure class is linked and initialized before handing id's out to
1344   // Method*s.
1345   klass->initialize(CHECK_NULL);
1346 
1347   Method* m;
1348   if (name == vmSymbols::object_initializer_name() ||
1349       name == vmSymbols::class_initializer_name()) {
1350     // Never search superclasses for constructors
1351     if (klass->is_instance_klass()) {
1352       m = InstanceKlass::cast(klass)->find_method(name, signature);
1353     } else {
1354       m = NULL;
1355     }
1356   } else {
1357     m = klass->lookup_method(name, signature);
1358     if (m == NULL &&  klass->is_instance_klass()) {
1359       m = InstanceKlass::cast(klass)->lookup_method_in_ordered_interfaces(name, signature);
1360     }
1361   }
1362   if (m == NULL || (m->is_static() != is_static)) {
1363     THROW_MSG_0(vmSymbols::java_lang_NoSuchMethodError(), name_str);
1364   }
1365   return m->jmethod_id();
1366 }
1367 
1368 
1369 JNI_ENTRY(jmethodID, jni_GetMethodID(JNIEnv *env, jclass clazz,
1370           const char *name, const char *sig))
1371   JNIWrapper("GetMethodID");
1372   HOTSPOT_JNI_GETMETHODID_ENTRY(env, clazz, (char *) name, (char *) sig);
1373   jmethodID ret = get_method_id(env, clazz, name, sig, false, thread);
1374   HOTSPOT_JNI_GETMETHODID_RETURN((uintptr_t) ret);
1375   return ret;
1376 JNI_END
1377 
1378 
1379 JNI_ENTRY(jmethodID, jni_GetStaticMethodID(JNIEnv *env, jclass clazz,
1380           const char *name, const char *sig))
1381   JNIWrapper("GetStaticMethodID");
1382   HOTSPOT_JNI_GETSTATICMETHODID_ENTRY(env, (char *) clazz, (char *) name, (char *)sig);
1383   jmethodID ret = get_method_id(env, clazz, name, sig, true, thread);
1384   HOTSPOT_JNI_GETSTATICMETHODID_RETURN((uintptr_t) ret);
1385   return ret;
1386 JNI_END
1387 
1388 
1389 
1390 //
1391 // Calling Methods
1392 //
1393 
1394 
1395 #define DEFINE_CALLMETHOD(ResultType, Result, Tag \
1396                           , EntryProbe, ReturnProbe)    \
1397 \
1398   DT_RETURN_MARK_DECL_FOR(Result, Call##Result##Method, ResultType \
1399                           , ReturnProbe);                          \
1400 \
1401 JNI_ENTRY(ResultType, \
1402           jni_Call##Result##Method(JNIEnv *env, jobject obj, jmethodID methodID, ...)) \
1403   JNIWrapper("Call" XSTR(Result) "Method"); \
1404 \
1405   EntryProbe; \
1406   ResultType ret = 0;\
1407   DT_RETURN_MARK_FOR(Result, Call##Result##Method, ResultType, \
1408                      (const ResultType&)ret);\
1409 \
1410   va_list args; \
1411   va_start(args, methodID); \
1412   JavaValue jvalue(Tag); \
1413   JNI_ArgumentPusherVaArg ap(methodID, args); \
1414   jni_invoke_nonstatic(env, &jvalue, obj, JNI_VIRTUAL, methodID, &ap, CHECK_0); \
1415   va_end(args); \
1416   ret = jvalue.get_##ResultType(); \
1417   return ret;\
1418 JNI_END
1419 
1420 // the runtime type of subword integral basic types is integer
1421 DEFINE_CALLMETHOD(jboolean, Boolean, T_BOOLEAN
1422                   , HOTSPOT_JNI_CALLBOOLEANMETHOD_ENTRY(env, obj, (uintptr_t)methodID),
1423                   HOTSPOT_JNI_CALLBOOLEANMETHOD_RETURN(_ret_ref))
1424 DEFINE_CALLMETHOD(jbyte,    Byte,    T_BYTE
1425                   , HOTSPOT_JNI_CALLBYTEMETHOD_ENTRY(env, obj, (uintptr_t)methodID),
1426                   HOTSPOT_JNI_CALLBYTEMETHOD_RETURN(_ret_ref))
1427 DEFINE_CALLMETHOD(jchar,    Char,    T_CHAR
1428                   , HOTSPOT_JNI_CALLCHARMETHOD_ENTRY(env, obj, (uintptr_t)methodID),
1429                   HOTSPOT_JNI_CALLCHARMETHOD_RETURN(_ret_ref))
1430 DEFINE_CALLMETHOD(jshort,   Short,   T_SHORT
1431                   , HOTSPOT_JNI_CALLSHORTMETHOD_ENTRY(env, obj, (uintptr_t)methodID),
1432                   HOTSPOT_JNI_CALLSHORTMETHOD_RETURN(_ret_ref))
1433 
1434 DEFINE_CALLMETHOD(jobject,  Object,  T_OBJECT
1435                   , HOTSPOT_JNI_CALLOBJECTMETHOD_ENTRY(env, obj, (uintptr_t)methodID),
1436                   HOTSPOT_JNI_CALLOBJECTMETHOD_RETURN(_ret_ref))
1437 DEFINE_CALLMETHOD(jint,     Int,     T_INT,
1438                   HOTSPOT_JNI_CALLINTMETHOD_ENTRY(env, obj, (uintptr_t)methodID),
1439                   HOTSPOT_JNI_CALLINTMETHOD_RETURN(_ret_ref))
1440 DEFINE_CALLMETHOD(jlong,    Long,    T_LONG
1441                   , HOTSPOT_JNI_CALLLONGMETHOD_ENTRY(env, obj, (uintptr_t)methodID),
1442                   HOTSPOT_JNI_CALLLONGMETHOD_RETURN(_ret_ref))
1443 // Float and double probes don't return value because dtrace doesn't currently support it
1444 DEFINE_CALLMETHOD(jfloat,   Float,   T_FLOAT
1445                   , HOTSPOT_JNI_CALLFLOATMETHOD_ENTRY(env, obj, (uintptr_t)methodID),
1446                   HOTSPOT_JNI_CALLFLOATMETHOD_RETURN())
1447 DEFINE_CALLMETHOD(jdouble,  Double,  T_DOUBLE
1448                   , HOTSPOT_JNI_CALLDOUBLEMETHOD_ENTRY(env, obj, (uintptr_t)methodID),
1449                   HOTSPOT_JNI_CALLDOUBLEMETHOD_RETURN())
1450 
1451 #define DEFINE_CALLMETHODV(ResultType, Result, Tag \
1452                           , EntryProbe, ReturnProbe)    \
1453 \
1454   DT_RETURN_MARK_DECL_FOR(Result, Call##Result##MethodV, ResultType \
1455                           , ReturnProbe);                          \
1456 \
1457 JNI_ENTRY(ResultType, \
1458           jni_Call##Result##MethodV(JNIEnv *env, jobject obj, jmethodID methodID, va_list args)) \
1459   JNIWrapper("Call" XSTR(Result) "MethodV"); \
1460 \
1461   EntryProbe;\
1462   ResultType ret = 0;\
1463   DT_RETURN_MARK_FOR(Result, Call##Result##MethodV, ResultType, \
1464                      (const ResultType&)ret);\
1465 \
1466   JavaValue jvalue(Tag); \
1467   JNI_ArgumentPusherVaArg ap(methodID, args); \
1468   jni_invoke_nonstatic(env, &jvalue, obj, JNI_VIRTUAL, methodID, &ap, CHECK_0); \
1469   ret = jvalue.get_##ResultType(); \
1470   return ret;\
1471 JNI_END
1472 
1473 // the runtime type of subword integral basic types is integer
1474 DEFINE_CALLMETHODV(jboolean, Boolean, T_BOOLEAN
1475                   , HOTSPOT_JNI_CALLBOOLEANMETHODV_ENTRY(env, obj, (uintptr_t)methodID),
1476                   HOTSPOT_JNI_CALLBOOLEANMETHODV_RETURN(_ret_ref))
1477 DEFINE_CALLMETHODV(jbyte,    Byte,    T_BYTE
1478                   , HOTSPOT_JNI_CALLBYTEMETHODV_ENTRY(env, obj, (uintptr_t)methodID),
1479                   HOTSPOT_JNI_CALLBYTEMETHODV_RETURN(_ret_ref))
1480 DEFINE_CALLMETHODV(jchar,    Char,    T_CHAR
1481                   , HOTSPOT_JNI_CALLCHARMETHODV_ENTRY(env, obj, (uintptr_t)methodID),
1482                   HOTSPOT_JNI_CALLCHARMETHODV_RETURN(_ret_ref))
1483 DEFINE_CALLMETHODV(jshort,   Short,   T_SHORT
1484                   , HOTSPOT_JNI_CALLSHORTMETHODV_ENTRY(env, obj, (uintptr_t)methodID),
1485                   HOTSPOT_JNI_CALLSHORTMETHODV_RETURN(_ret_ref))
1486 
1487 DEFINE_CALLMETHODV(jobject,  Object,  T_OBJECT
1488                   , HOTSPOT_JNI_CALLOBJECTMETHODV_ENTRY(env, obj, (uintptr_t)methodID),
1489                   HOTSPOT_JNI_CALLOBJECTMETHODV_RETURN(_ret_ref))
1490 DEFINE_CALLMETHODV(jint,     Int,     T_INT,
1491                   HOTSPOT_JNI_CALLINTMETHODV_ENTRY(env, obj, (uintptr_t)methodID),
1492                   HOTSPOT_JNI_CALLINTMETHODV_RETURN(_ret_ref))
1493 DEFINE_CALLMETHODV(jlong,    Long,    T_LONG
1494                   , HOTSPOT_JNI_CALLLONGMETHODV_ENTRY(env, obj, (uintptr_t)methodID),
1495                   HOTSPOT_JNI_CALLLONGMETHODV_RETURN(_ret_ref))
1496 // Float and double probes don't return value because dtrace doesn't currently support it
1497 DEFINE_CALLMETHODV(jfloat,   Float,   T_FLOAT
1498                   , HOTSPOT_JNI_CALLFLOATMETHODV_ENTRY(env, obj, (uintptr_t)methodID),
1499                   HOTSPOT_JNI_CALLFLOATMETHODV_RETURN())
1500 DEFINE_CALLMETHODV(jdouble,  Double,  T_DOUBLE
1501                   , HOTSPOT_JNI_CALLDOUBLEMETHODV_ENTRY(env, obj, (uintptr_t)methodID),
1502                   HOTSPOT_JNI_CALLDOUBLEMETHODV_RETURN())
1503 
1504 #define DEFINE_CALLMETHODA(ResultType, Result, Tag \
1505                           , EntryProbe, ReturnProbe)    \
1506 \
1507   DT_RETURN_MARK_DECL_FOR(Result, Call##Result##MethodA, ResultType \
1508                           , ReturnProbe);                          \
1509 \
1510 JNI_ENTRY(ResultType, \
1511           jni_Call##Result##MethodA(JNIEnv *env, jobject obj, jmethodID methodID, const jvalue *args)) \
1512   JNIWrapper("Call" XSTR(Result) "MethodA"); \
1513   EntryProbe; \
1514   ResultType ret = 0;\
1515   DT_RETURN_MARK_FOR(Result, Call##Result##MethodA, ResultType, \
1516                      (const ResultType&)ret);\
1517 \
1518   JavaValue jvalue(Tag); \
1519   JNI_ArgumentPusherArray ap(methodID, args); \
1520   jni_invoke_nonstatic(env, &jvalue, obj, JNI_VIRTUAL, methodID, &ap, CHECK_0); \
1521   ret = jvalue.get_##ResultType(); \
1522   return ret;\
1523 JNI_END
1524 
1525 // the runtime type of subword integral basic types is integer
1526 DEFINE_CALLMETHODA(jboolean, Boolean, T_BOOLEAN
1527                   , HOTSPOT_JNI_CALLBOOLEANMETHODA_ENTRY(env, obj, (uintptr_t)methodID),
1528                   HOTSPOT_JNI_CALLBOOLEANMETHODA_RETURN(_ret_ref))
1529 DEFINE_CALLMETHODA(jbyte,    Byte,    T_BYTE
1530                   , HOTSPOT_JNI_CALLBYTEMETHODA_ENTRY(env, obj, (uintptr_t)methodID),
1531                   HOTSPOT_JNI_CALLBYTEMETHODA_RETURN(_ret_ref))
1532 DEFINE_CALLMETHODA(jchar,    Char,    T_CHAR
1533                   , HOTSPOT_JNI_CALLCHARMETHODA_ENTRY(env, obj, (uintptr_t)methodID),
1534                   HOTSPOT_JNI_CALLCHARMETHODA_RETURN(_ret_ref))
1535 DEFINE_CALLMETHODA(jshort,   Short,   T_SHORT
1536                   , HOTSPOT_JNI_CALLSHORTMETHODA_ENTRY(env, obj, (uintptr_t)methodID),
1537                   HOTSPOT_JNI_CALLSHORTMETHODA_RETURN(_ret_ref))
1538 
1539 DEFINE_CALLMETHODA(jobject,  Object,  T_OBJECT
1540                   , HOTSPOT_JNI_CALLOBJECTMETHODA_ENTRY(env, obj, (uintptr_t)methodID),
1541                   HOTSPOT_JNI_CALLOBJECTMETHODA_RETURN(_ret_ref))
1542 DEFINE_CALLMETHODA(jint,     Int,     T_INT,
1543                   HOTSPOT_JNI_CALLINTMETHODA_ENTRY(env, obj, (uintptr_t)methodID),
1544                   HOTSPOT_JNI_CALLINTMETHODA_RETURN(_ret_ref))
1545 DEFINE_CALLMETHODA(jlong,    Long,    T_LONG
1546                   , HOTSPOT_JNI_CALLLONGMETHODA_ENTRY(env, obj, (uintptr_t)methodID),
1547                   HOTSPOT_JNI_CALLLONGMETHODA_RETURN(_ret_ref))
1548 // Float and double probes don't return value because dtrace doesn't currently support it
1549 DEFINE_CALLMETHODA(jfloat,   Float,   T_FLOAT
1550                   , HOTSPOT_JNI_CALLFLOATMETHODA_ENTRY(env, obj, (uintptr_t)methodID),
1551                   HOTSPOT_JNI_CALLFLOATMETHODA_RETURN())
1552 DEFINE_CALLMETHODA(jdouble,  Double,  T_DOUBLE
1553                   , HOTSPOT_JNI_CALLDOUBLEMETHODA_ENTRY(env, obj, (uintptr_t)methodID),
1554                   HOTSPOT_JNI_CALLDOUBLEMETHODA_RETURN())
1555 
1556 DT_VOID_RETURN_MARK_DECL(CallVoidMethod, HOTSPOT_JNI_CALLVOIDMETHOD_RETURN());
1557 DT_VOID_RETURN_MARK_DECL(CallVoidMethodV, HOTSPOT_JNI_CALLVOIDMETHODV_RETURN());
1558 DT_VOID_RETURN_MARK_DECL(CallVoidMethodA, HOTSPOT_JNI_CALLVOIDMETHODA_RETURN());
1559 
1560 
1561 JNI_ENTRY(void, jni_CallVoidMethod(JNIEnv *env, jobject obj, jmethodID methodID, ...))
1562   JNIWrapper("CallVoidMethod");
1563   HOTSPOT_JNI_CALLVOIDMETHOD_ENTRY(env, obj, (uintptr_t) methodID);
1564   DT_VOID_RETURN_MARK(CallVoidMethod);
1565 
1566   va_list args;
1567   va_start(args, methodID);
1568   JavaValue jvalue(T_VOID);
1569   JNI_ArgumentPusherVaArg ap(methodID, args);
1570   jni_invoke_nonstatic(env, &jvalue, obj, JNI_VIRTUAL, methodID, &ap, CHECK);
1571   va_end(args);
1572 JNI_END
1573 
1574 
1575 JNI_ENTRY(void, jni_CallVoidMethodV(JNIEnv *env, jobject obj, jmethodID methodID, va_list args))
1576   JNIWrapper("CallVoidMethodV");
1577   HOTSPOT_JNI_CALLVOIDMETHODV_ENTRY(env, obj, (uintptr_t) methodID);
1578   DT_VOID_RETURN_MARK(CallVoidMethodV);
1579 
1580   JavaValue jvalue(T_VOID);
1581   JNI_ArgumentPusherVaArg ap(methodID, args);
1582   jni_invoke_nonstatic(env, &jvalue, obj, JNI_VIRTUAL, methodID, &ap, CHECK);
1583 JNI_END
1584 
1585 
1586 JNI_ENTRY(void, jni_CallVoidMethodA(JNIEnv *env, jobject obj, jmethodID methodID, const jvalue *args))
1587   JNIWrapper("CallVoidMethodA");
1588   HOTSPOT_JNI_CALLVOIDMETHODA_ENTRY(env, obj, (uintptr_t) methodID);
1589   DT_VOID_RETURN_MARK(CallVoidMethodA);
1590 
1591   JavaValue jvalue(T_VOID);
1592   JNI_ArgumentPusherArray ap(methodID, args);
1593   jni_invoke_nonstatic(env, &jvalue, obj, JNI_VIRTUAL, methodID, &ap, CHECK);
1594 JNI_END
1595 
1596 
1597 
1598 #define DEFINE_CALLNONVIRTUALMETHOD(ResultType, Result, Tag \
1599                                     , EntryProbe, ReturnProbe)      \
1600 \
1601   DT_RETURN_MARK_DECL_FOR(Result, CallNonvirtual##Result##Method, ResultType \
1602                           , ReturnProbe);\
1603 \
1604 JNI_ENTRY(ResultType, \
1605           jni_CallNonvirtual##Result##Method(JNIEnv *env, jobject obj, jclass cls, jmethodID methodID, ...)) \
1606   JNIWrapper("CallNonvitual" XSTR(Result) "Method"); \
1607 \
1608   EntryProbe;\
1609   ResultType ret;\
1610   DT_RETURN_MARK_FOR(Result, CallNonvirtual##Result##Method, ResultType, \
1611                      (const ResultType&)ret);\
1612 \
1613   va_list args; \
1614   va_start(args, methodID); \
1615   JavaValue jvalue(Tag); \
1616   JNI_ArgumentPusherVaArg ap(methodID, args); \
1617   jni_invoke_nonstatic(env, &jvalue, obj, JNI_NONVIRTUAL, methodID, &ap, CHECK_0); \
1618   va_end(args); \
1619   ret = jvalue.get_##ResultType(); \
1620   return ret;\
1621 JNI_END
1622 
1623 // the runtime type of subword integral basic types is integer
1624 DEFINE_CALLNONVIRTUALMETHOD(jboolean, Boolean, T_BOOLEAN
1625                             , HOTSPOT_JNI_CALLNONVIRTUALBOOLEANMETHOD_ENTRY(env, obj, cls, (uintptr_t)methodID),
1626                             HOTSPOT_JNI_CALLNONVIRTUALBOOLEANMETHOD_RETURN(_ret_ref))
1627 DEFINE_CALLNONVIRTUALMETHOD(jbyte,    Byte,    T_BYTE
1628                             , HOTSPOT_JNI_CALLNONVIRTUALBYTEMETHOD_ENTRY(env, obj, cls, (uintptr_t)methodID),
1629                             HOTSPOT_JNI_CALLNONVIRTUALBYTEMETHOD_RETURN(_ret_ref))
1630 DEFINE_CALLNONVIRTUALMETHOD(jchar,    Char,    T_CHAR
1631                             , HOTSPOT_JNI_CALLNONVIRTUALCHARMETHOD_ENTRY(env, obj, cls, (uintptr_t)methodID),
1632                             HOTSPOT_JNI_CALLNONVIRTUALCHARMETHOD_RETURN(_ret_ref))
1633 DEFINE_CALLNONVIRTUALMETHOD(jshort,   Short,   T_SHORT
1634                             , HOTSPOT_JNI_CALLNONVIRTUALSHORTMETHOD_ENTRY(env, obj, cls, (uintptr_t)methodID),
1635                             HOTSPOT_JNI_CALLNONVIRTUALSHORTMETHOD_RETURN(_ret_ref))
1636 
1637 DEFINE_CALLNONVIRTUALMETHOD(jobject,  Object,  T_OBJECT
1638                             , HOTSPOT_JNI_CALLNONVIRTUALOBJECTMETHOD_ENTRY(env, obj, cls, (uintptr_t)methodID),
1639                             HOTSPOT_JNI_CALLNONVIRTUALOBJECTMETHOD_RETURN(_ret_ref))
1640 DEFINE_CALLNONVIRTUALMETHOD(jint,     Int,     T_INT
1641                             , HOTSPOT_JNI_CALLNONVIRTUALINTMETHOD_ENTRY(env, obj, cls, (uintptr_t)methodID),
1642                             HOTSPOT_JNI_CALLNONVIRTUALINTMETHOD_RETURN(_ret_ref))
1643 DEFINE_CALLNONVIRTUALMETHOD(jlong,    Long,    T_LONG
1644                             , HOTSPOT_JNI_CALLNONVIRTUALLONGMETHOD_ENTRY(env, obj, cls, (uintptr_t)methodID),
1645 // Float and double probes don't return value because dtrace doesn't currently support it
1646                             HOTSPOT_JNI_CALLNONVIRTUALLONGMETHOD_RETURN(_ret_ref))
1647 DEFINE_CALLNONVIRTUALMETHOD(jfloat,   Float,   T_FLOAT
1648                             , HOTSPOT_JNI_CALLNONVIRTUALFLOATMETHOD_ENTRY(env, obj, cls, (uintptr_t)methodID),
1649                             HOTSPOT_JNI_CALLNONVIRTUALFLOATMETHOD_RETURN())
1650 DEFINE_CALLNONVIRTUALMETHOD(jdouble,  Double,  T_DOUBLE
1651                             , HOTSPOT_JNI_CALLNONVIRTUALDOUBLEMETHOD_ENTRY(env, obj, cls, (uintptr_t)methodID),
1652                             HOTSPOT_JNI_CALLNONVIRTUALDOUBLEMETHOD_RETURN())
1653 
1654 #define DEFINE_CALLNONVIRTUALMETHODV(ResultType, Result, Tag \
1655                                     , EntryProbe, ReturnProbe)      \
1656 \
1657   DT_RETURN_MARK_DECL_FOR(Result, CallNonvirtual##Result##MethodV, ResultType \
1658                           , ReturnProbe);\
1659 \
1660 JNI_ENTRY(ResultType, \
1661           jni_CallNonvirtual##Result##MethodV(JNIEnv *env, jobject obj, jclass cls, jmethodID methodID, va_list args)) \
1662   JNIWrapper("CallNonvitual" XSTR(Result) "MethodV"); \
1663 \
1664   EntryProbe;\
1665   ResultType ret;\
1666   DT_RETURN_MARK_FOR(Result, CallNonvirtual##Result##MethodV, ResultType, \
1667                      (const ResultType&)ret);\
1668 \
1669   JavaValue jvalue(Tag); \
1670   JNI_ArgumentPusherVaArg ap(methodID, args); \
1671   jni_invoke_nonstatic(env, &jvalue, obj, JNI_NONVIRTUAL, methodID, &ap, CHECK_0); \
1672   ret = jvalue.get_##ResultType(); \
1673   return ret;\
1674 JNI_END
1675 
1676 // the runtime type of subword integral basic types is integer
1677 DEFINE_CALLNONVIRTUALMETHODV(jboolean, Boolean, T_BOOLEAN
1678                             , HOTSPOT_JNI_CALLNONVIRTUALBOOLEANMETHODV_ENTRY(env, obj, cls, (uintptr_t)methodID),
1679                             HOTSPOT_JNI_CALLNONVIRTUALBOOLEANMETHODV_RETURN(_ret_ref))
1680 DEFINE_CALLNONVIRTUALMETHODV(jbyte,    Byte,    T_BYTE
1681                             , HOTSPOT_JNI_CALLNONVIRTUALBYTEMETHODV_ENTRY(env, obj, cls, (uintptr_t)methodID),
1682                             HOTSPOT_JNI_CALLNONVIRTUALBYTEMETHODV_RETURN(_ret_ref))
1683 DEFINE_CALLNONVIRTUALMETHODV(jchar,    Char,    T_CHAR
1684                             , HOTSPOT_JNI_CALLNONVIRTUALCHARMETHODV_ENTRY(env, obj, cls, (uintptr_t)methodID),
1685                             HOTSPOT_JNI_CALLNONVIRTUALCHARMETHODV_RETURN(_ret_ref))
1686 DEFINE_CALLNONVIRTUALMETHODV(jshort,   Short,   T_SHORT
1687                             , HOTSPOT_JNI_CALLNONVIRTUALSHORTMETHODV_ENTRY(env, obj, cls, (uintptr_t)methodID),
1688                             HOTSPOT_JNI_CALLNONVIRTUALSHORTMETHODV_RETURN(_ret_ref))
1689 
1690 DEFINE_CALLNONVIRTUALMETHODV(jobject,  Object,  T_OBJECT
1691                             , HOTSPOT_JNI_CALLNONVIRTUALOBJECTMETHODV_ENTRY(env, obj, cls, (uintptr_t)methodID),
1692                             HOTSPOT_JNI_CALLNONVIRTUALOBJECTMETHODV_RETURN(_ret_ref))
1693 DEFINE_CALLNONVIRTUALMETHODV(jint,     Int,     T_INT
1694                             , HOTSPOT_JNI_CALLNONVIRTUALINTMETHODV_ENTRY(env, obj, cls, (uintptr_t)methodID),
1695                             HOTSPOT_JNI_CALLNONVIRTUALINTMETHODV_RETURN(_ret_ref))
1696 DEFINE_CALLNONVIRTUALMETHODV(jlong,    Long,    T_LONG
1697                             , HOTSPOT_JNI_CALLNONVIRTUALLONGMETHODV_ENTRY(env, obj, cls, (uintptr_t)methodID),
1698 // Float and double probes don't return value because dtrace doesn't currently support it
1699                             HOTSPOT_JNI_CALLNONVIRTUALLONGMETHODV_RETURN(_ret_ref))
1700 DEFINE_CALLNONVIRTUALMETHODV(jfloat,   Float,   T_FLOAT
1701                             , HOTSPOT_JNI_CALLNONVIRTUALFLOATMETHODV_ENTRY(env, obj, cls, (uintptr_t)methodID),
1702                             HOTSPOT_JNI_CALLNONVIRTUALFLOATMETHODV_RETURN())
1703 DEFINE_CALLNONVIRTUALMETHODV(jdouble,  Double,  T_DOUBLE
1704                             , HOTSPOT_JNI_CALLNONVIRTUALDOUBLEMETHODV_ENTRY(env, obj, cls, (uintptr_t)methodID),
1705                             HOTSPOT_JNI_CALLNONVIRTUALDOUBLEMETHODV_RETURN())
1706 
1707 #define DEFINE_CALLNONVIRTUALMETHODA(ResultType, Result, Tag \
1708                                     , EntryProbe, ReturnProbe)      \
1709 \
1710   DT_RETURN_MARK_DECL_FOR(Result, CallNonvirtual##Result##MethodA, ResultType \
1711                           , ReturnProbe);\
1712 \
1713 JNI_ENTRY(ResultType, \
1714           jni_CallNonvirtual##Result##MethodA(JNIEnv *env, jobject obj, jclass cls, jmethodID methodID, const jvalue *args)) \
1715   JNIWrapper("CallNonvitual" XSTR(Result) "MethodA"); \
1716 \
1717   EntryProbe;\
1718   ResultType ret;\
1719   DT_RETURN_MARK_FOR(Result, CallNonvirtual##Result##MethodA, ResultType, \
1720                      (const ResultType&)ret);\
1721 \
1722   JavaValue jvalue(Tag); \
1723   JNI_ArgumentPusherArray ap(methodID, args); \
1724   jni_invoke_nonstatic(env, &jvalue, obj, JNI_NONVIRTUAL, methodID, &ap, CHECK_0); \
1725   ret = jvalue.get_##ResultType(); \
1726   return ret;\
1727 JNI_END
1728 
1729 // the runtime type of subword integral basic types is integer
1730 DEFINE_CALLNONVIRTUALMETHODA(jboolean, Boolean, T_BOOLEAN
1731                             , HOTSPOT_JNI_CALLNONVIRTUALBOOLEANMETHODA_ENTRY(env, obj, cls, (uintptr_t)methodID),
1732                             HOTSPOT_JNI_CALLNONVIRTUALBOOLEANMETHODA_RETURN(_ret_ref))
1733 DEFINE_CALLNONVIRTUALMETHODA(jbyte,    Byte,    T_BYTE
1734                             , HOTSPOT_JNI_CALLNONVIRTUALBYTEMETHODA_ENTRY(env, obj, cls, (uintptr_t)methodID),
1735                             HOTSPOT_JNI_CALLNONVIRTUALBYTEMETHODA_RETURN(_ret_ref))
1736 DEFINE_CALLNONVIRTUALMETHODA(jchar,    Char,    T_CHAR
1737                             , HOTSPOT_JNI_CALLNONVIRTUALCHARMETHODA_ENTRY(env, obj, cls, (uintptr_t)methodID),
1738                             HOTSPOT_JNI_CALLNONVIRTUALCHARMETHODA_RETURN(_ret_ref))
1739 DEFINE_CALLNONVIRTUALMETHODA(jshort,   Short,   T_SHORT
1740                             , HOTSPOT_JNI_CALLNONVIRTUALSHORTMETHODA_ENTRY(env, obj, cls, (uintptr_t)methodID),
1741                             HOTSPOT_JNI_CALLNONVIRTUALSHORTMETHODA_RETURN(_ret_ref))
1742 
1743 DEFINE_CALLNONVIRTUALMETHODA(jobject,  Object,  T_OBJECT
1744                             , HOTSPOT_JNI_CALLNONVIRTUALOBJECTMETHODA_ENTRY(env, obj, cls, (uintptr_t)methodID),
1745                             HOTSPOT_JNI_CALLNONVIRTUALOBJECTMETHODA_RETURN(_ret_ref))
1746 DEFINE_CALLNONVIRTUALMETHODA(jint,     Int,     T_INT
1747                             , HOTSPOT_JNI_CALLNONVIRTUALINTMETHODA_ENTRY(env, obj, cls, (uintptr_t)methodID),
1748                             HOTSPOT_JNI_CALLNONVIRTUALINTMETHODA_RETURN(_ret_ref))
1749 DEFINE_CALLNONVIRTUALMETHODA(jlong,    Long,    T_LONG
1750                             , HOTSPOT_JNI_CALLNONVIRTUALLONGMETHODA_ENTRY(env, obj, cls, (uintptr_t)methodID),
1751 // Float and double probes don't return value because dtrace doesn't currently support it
1752                             HOTSPOT_JNI_CALLNONVIRTUALLONGMETHODA_RETURN(_ret_ref))
1753 DEFINE_CALLNONVIRTUALMETHODA(jfloat,   Float,   T_FLOAT
1754                             , HOTSPOT_JNI_CALLNONVIRTUALFLOATMETHODA_ENTRY(env, obj, cls, (uintptr_t)methodID),
1755                             HOTSPOT_JNI_CALLNONVIRTUALFLOATMETHODA_RETURN())
1756 DEFINE_CALLNONVIRTUALMETHODA(jdouble,  Double,  T_DOUBLE
1757                             , HOTSPOT_JNI_CALLNONVIRTUALDOUBLEMETHODA_ENTRY(env, obj, cls, (uintptr_t)methodID),
1758                             HOTSPOT_JNI_CALLNONVIRTUALDOUBLEMETHODA_RETURN())
1759 
1760 DT_VOID_RETURN_MARK_DECL(CallNonvirtualVoidMethod
1761                          , HOTSPOT_JNI_CALLNONVIRTUALVOIDMETHOD_RETURN());
1762 DT_VOID_RETURN_MARK_DECL(CallNonvirtualVoidMethodV
1763                          , HOTSPOT_JNI_CALLNONVIRTUALVOIDMETHODV_RETURN());
1764 DT_VOID_RETURN_MARK_DECL(CallNonvirtualVoidMethodA
1765                          , HOTSPOT_JNI_CALLNONVIRTUALVOIDMETHODA_RETURN());
1766 
1767 JNI_ENTRY(void, jni_CallNonvirtualVoidMethod(JNIEnv *env, jobject obj, jclass cls, jmethodID methodID, ...))
1768   JNIWrapper("CallNonvirtualVoidMethod");
1769 
1770   HOTSPOT_JNI_CALLNONVIRTUALVOIDMETHOD_ENTRY(env, obj, cls, (uintptr_t) methodID);
1771   DT_VOID_RETURN_MARK(CallNonvirtualVoidMethod);
1772 
1773   va_list args;
1774   va_start(args, methodID);
1775   JavaValue jvalue(T_VOID);
1776   JNI_ArgumentPusherVaArg ap(methodID, args);
1777   jni_invoke_nonstatic(env, &jvalue, obj, JNI_NONVIRTUAL, methodID, &ap, CHECK);
1778   va_end(args);
1779 JNI_END
1780 
1781 
1782 JNI_ENTRY(void, jni_CallNonvirtualVoidMethodV(JNIEnv *env, jobject obj, jclass cls, jmethodID methodID, va_list args))
1783   JNIWrapper("CallNonvirtualVoidMethodV");
1784 
1785   HOTSPOT_JNI_CALLNONVIRTUALVOIDMETHODV_ENTRY(
1786                env, obj, cls, (uintptr_t) methodID);
1787   DT_VOID_RETURN_MARK(CallNonvirtualVoidMethodV);
1788 
1789   JavaValue jvalue(T_VOID);
1790   JNI_ArgumentPusherVaArg ap(methodID, args);
1791   jni_invoke_nonstatic(env, &jvalue, obj, JNI_NONVIRTUAL, methodID, &ap, CHECK);
1792 JNI_END
1793 
1794 
1795 JNI_ENTRY(void, jni_CallNonvirtualVoidMethodA(JNIEnv *env, jobject obj, jclass cls, jmethodID methodID, const jvalue *args))
1796   JNIWrapper("CallNonvirtualVoidMethodA");
1797   HOTSPOT_JNI_CALLNONVIRTUALVOIDMETHODA_ENTRY(
1798                 env, obj, cls, (uintptr_t) methodID);
1799   DT_VOID_RETURN_MARK(CallNonvirtualVoidMethodA);
1800   JavaValue jvalue(T_VOID);
1801   JNI_ArgumentPusherArray ap(methodID, args);
1802   jni_invoke_nonstatic(env, &jvalue, obj, JNI_NONVIRTUAL, methodID, &ap, CHECK);
1803 JNI_END
1804 
1805 
1806 
1807 #define DEFINE_CALLSTATICMETHOD(ResultType, Result, Tag \
1808                                 , EntryProbe, ResultProbe) \
1809 \
1810   DT_RETURN_MARK_DECL_FOR(Result, CallStatic##Result##Method, ResultType \
1811                           , ResultProbe);                               \
1812 \
1813 JNI_ENTRY(ResultType, \
1814           jni_CallStatic##Result##Method(JNIEnv *env, jclass cls, jmethodID methodID, ...)) \
1815   JNIWrapper("CallStatic" XSTR(Result) "Method"); \
1816 \
1817   EntryProbe; \
1818   ResultType ret = 0;\
1819   DT_RETURN_MARK_FOR(Result, CallStatic##Result##Method, ResultType, \
1820                      (const ResultType&)ret);\
1821 \
1822   va_list args; \
1823   va_start(args, methodID); \
1824   JavaValue jvalue(Tag); \
1825   JNI_ArgumentPusherVaArg ap(methodID, args); \
1826   jni_invoke_static(env, &jvalue, NULL, JNI_STATIC, methodID, &ap, CHECK_0); \
1827   va_end(args); \
1828   ret = jvalue.get_##ResultType(); \
1829   return ret;\
1830 JNI_END
1831 
1832 // the runtime type of subword integral basic types is integer
1833 DEFINE_CALLSTATICMETHOD(jboolean, Boolean, T_BOOLEAN
1834                         , HOTSPOT_JNI_CALLSTATICBOOLEANMETHOD_ENTRY(env, cls, (uintptr_t)methodID),
1835                         HOTSPOT_JNI_CALLSTATICBOOLEANMETHOD_RETURN(_ret_ref));
1836 DEFINE_CALLSTATICMETHOD(jbyte,    Byte,    T_BYTE
1837                         , HOTSPOT_JNI_CALLSTATICBYTEMETHOD_ENTRY(env, cls, (uintptr_t)methodID),
1838                         HOTSPOT_JNI_CALLSTATICBYTEMETHOD_RETURN(_ret_ref));
1839 DEFINE_CALLSTATICMETHOD(jchar,    Char,    T_CHAR
1840                         , HOTSPOT_JNI_CALLSTATICCHARMETHOD_ENTRY(env, cls, (uintptr_t)methodID),
1841                         HOTSPOT_JNI_CALLSTATICCHARMETHOD_RETURN(_ret_ref));
1842 DEFINE_CALLSTATICMETHOD(jshort,   Short,   T_SHORT
1843                         , HOTSPOT_JNI_CALLSTATICSHORTMETHOD_ENTRY(env, cls, (uintptr_t)methodID),
1844                         HOTSPOT_JNI_CALLSTATICSHORTMETHOD_RETURN(_ret_ref));
1845 
1846 DEFINE_CALLSTATICMETHOD(jobject,  Object,  T_OBJECT
1847                         , HOTSPOT_JNI_CALLSTATICOBJECTMETHOD_ENTRY(env, cls, (uintptr_t)methodID),
1848                         HOTSPOT_JNI_CALLSTATICOBJECTMETHOD_RETURN(_ret_ref));
1849 DEFINE_CALLSTATICMETHOD(jint,     Int,     T_INT
1850                         , HOTSPOT_JNI_CALLSTATICINTMETHOD_ENTRY(env, cls, (uintptr_t)methodID),
1851                         HOTSPOT_JNI_CALLSTATICINTMETHOD_RETURN(_ret_ref));
1852 DEFINE_CALLSTATICMETHOD(jlong,    Long,    T_LONG
1853                         , HOTSPOT_JNI_CALLSTATICLONGMETHOD_ENTRY(env, cls, (uintptr_t)methodID),
1854                         HOTSPOT_JNI_CALLSTATICLONGMETHOD_RETURN(_ret_ref));
1855 // Float and double probes don't return value because dtrace doesn't currently support it
1856 DEFINE_CALLSTATICMETHOD(jfloat,   Float,   T_FLOAT
1857                         , HOTSPOT_JNI_CALLSTATICFLOATMETHOD_ENTRY(env, cls, (uintptr_t)methodID),
1858                         HOTSPOT_JNI_CALLSTATICFLOATMETHOD_RETURN());
1859 DEFINE_CALLSTATICMETHOD(jdouble,  Double,  T_DOUBLE
1860                         , HOTSPOT_JNI_CALLSTATICDOUBLEMETHOD_ENTRY(env, cls, (uintptr_t)methodID),
1861                         HOTSPOT_JNI_CALLSTATICDOUBLEMETHOD_RETURN());
1862 
1863 #define DEFINE_CALLSTATICMETHODV(ResultType, Result, Tag \
1864                                 , EntryProbe, ResultProbe) \
1865 \
1866   DT_RETURN_MARK_DECL_FOR(Result, CallStatic##Result##MethodV, ResultType \
1867                           , ResultProbe);                               \
1868 \
1869 JNI_ENTRY(ResultType, \
1870           jni_CallStatic##Result##MethodV(JNIEnv *env, jclass cls, jmethodID methodID, va_list args)) \
1871   JNIWrapper("CallStatic" XSTR(Result) "MethodV"); \
1872 \
1873   EntryProbe; \
1874   ResultType ret = 0;\
1875   DT_RETURN_MARK_FOR(Result, CallStatic##Result##MethodV, ResultType, \
1876                      (const ResultType&)ret);\
1877 \
1878   JavaValue jvalue(Tag); \
1879   JNI_ArgumentPusherVaArg ap(methodID, args); \
1880   /* Make sure class is initialized before trying to invoke its method */ \
1881   Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls)); \
1882   k->initialize(CHECK_0); \
1883   jni_invoke_static(env, &jvalue, NULL, JNI_STATIC, methodID, &ap, CHECK_0); \
1884   va_end(args); \
1885   ret = jvalue.get_##ResultType(); \
1886   return ret;\
1887 JNI_END
1888 
1889 // the runtime type of subword integral basic types is integer
1890 DEFINE_CALLSTATICMETHODV(jboolean, Boolean, T_BOOLEAN
1891                         , HOTSPOT_JNI_CALLSTATICBOOLEANMETHODV_ENTRY(env, cls, (uintptr_t)methodID),
1892                         HOTSPOT_JNI_CALLSTATICBOOLEANMETHODV_RETURN(_ret_ref));
1893 DEFINE_CALLSTATICMETHODV(jbyte,    Byte,    T_BYTE
1894                         , HOTSPOT_JNI_CALLSTATICBYTEMETHODV_ENTRY(env, cls, (uintptr_t)methodID),
1895                         HOTSPOT_JNI_CALLSTATICBYTEMETHODV_RETURN(_ret_ref));
1896 DEFINE_CALLSTATICMETHODV(jchar,    Char,    T_CHAR
1897                         , HOTSPOT_JNI_CALLSTATICCHARMETHODV_ENTRY(env, cls, (uintptr_t)methodID),
1898                         HOTSPOT_JNI_CALLSTATICCHARMETHODV_RETURN(_ret_ref));
1899 DEFINE_CALLSTATICMETHODV(jshort,   Short,   T_SHORT
1900                         , HOTSPOT_JNI_CALLSTATICSHORTMETHODV_ENTRY(env, cls, (uintptr_t)methodID),
1901                         HOTSPOT_JNI_CALLSTATICSHORTMETHODV_RETURN(_ret_ref));
1902 
1903 DEFINE_CALLSTATICMETHODV(jobject,  Object,  T_OBJECT
1904                         , HOTSPOT_JNI_CALLSTATICOBJECTMETHODV_ENTRY(env, cls, (uintptr_t)methodID),
1905                         HOTSPOT_JNI_CALLSTATICOBJECTMETHODV_RETURN(_ret_ref));
1906 DEFINE_CALLSTATICMETHODV(jint,     Int,     T_INT
1907                         , HOTSPOT_JNI_CALLSTATICINTMETHODV_ENTRY(env, cls, (uintptr_t)methodID),
1908                         HOTSPOT_JNI_CALLSTATICINTMETHODV_RETURN(_ret_ref));
1909 DEFINE_CALLSTATICMETHODV(jlong,    Long,    T_LONG
1910                         , HOTSPOT_JNI_CALLSTATICLONGMETHODV_ENTRY(env, cls, (uintptr_t)methodID),
1911                         HOTSPOT_JNI_CALLSTATICLONGMETHODV_RETURN(_ret_ref));
1912 // Float and double probes don't return value because dtrace doesn't currently support it
1913 DEFINE_CALLSTATICMETHODV(jfloat,   Float,   T_FLOAT
1914                         , HOTSPOT_JNI_CALLSTATICFLOATMETHODV_ENTRY(env, cls, (uintptr_t)methodID),
1915                         HOTSPOT_JNI_CALLSTATICFLOATMETHODV_RETURN());
1916 DEFINE_CALLSTATICMETHODV(jdouble,  Double,  T_DOUBLE
1917                         , HOTSPOT_JNI_CALLSTATICDOUBLEMETHODV_ENTRY(env, cls, (uintptr_t)methodID),
1918                         HOTSPOT_JNI_CALLSTATICDOUBLEMETHODV_RETURN());
1919 
1920 #define DEFINE_CALLSTATICMETHODA(ResultType, Result, Tag \
1921                                 , EntryProbe, ResultProbe) \
1922 \
1923   DT_RETURN_MARK_DECL_FOR(Result, CallStatic##Result##MethodA, ResultType \
1924                           , ResultProbe);                               \
1925 \
1926 JNI_ENTRY(ResultType, \
1927           jni_CallStatic##Result##MethodA(JNIEnv *env, jclass cls, jmethodID methodID, const jvalue *args)) \
1928   JNIWrapper("CallStatic" XSTR(Result) "MethodA"); \
1929 \
1930   EntryProbe; \
1931   ResultType ret = 0;\
1932   DT_RETURN_MARK_FOR(Result, CallStatic##Result##MethodA, ResultType, \
1933                      (const ResultType&)ret);\
1934 \
1935   JavaValue jvalue(Tag); \
1936   JNI_ArgumentPusherArray ap(methodID, args); \
1937   jni_invoke_static(env, &jvalue, NULL, JNI_STATIC, methodID, &ap, CHECK_0); \
1938   ret = jvalue.get_##ResultType(); \
1939   return ret;\
1940 JNI_END
1941 
1942 // the runtime type of subword integral basic types is integer
1943 DEFINE_CALLSTATICMETHODA(jboolean, Boolean, T_BOOLEAN
1944                         , HOTSPOT_JNI_CALLSTATICBOOLEANMETHODA_ENTRY(env, cls, (uintptr_t)methodID),
1945                         HOTSPOT_JNI_CALLSTATICBOOLEANMETHODA_RETURN(_ret_ref));
1946 DEFINE_CALLSTATICMETHODA(jbyte,    Byte,    T_BYTE
1947                         , HOTSPOT_JNI_CALLSTATICBYTEMETHODA_ENTRY(env, cls, (uintptr_t)methodID),
1948                         HOTSPOT_JNI_CALLSTATICBYTEMETHODA_RETURN(_ret_ref));
1949 DEFINE_CALLSTATICMETHODA(jchar,    Char,    T_CHAR
1950                         , HOTSPOT_JNI_CALLSTATICCHARMETHODA_ENTRY(env, cls, (uintptr_t)methodID),
1951                         HOTSPOT_JNI_CALLSTATICCHARMETHODA_RETURN(_ret_ref));
1952 DEFINE_CALLSTATICMETHODA(jshort,   Short,   T_SHORT
1953                         , HOTSPOT_JNI_CALLSTATICSHORTMETHODA_ENTRY(env, cls, (uintptr_t)methodID),
1954                         HOTSPOT_JNI_CALLSTATICSHORTMETHODA_RETURN(_ret_ref));
1955 
1956 DEFINE_CALLSTATICMETHODA(jobject,  Object,  T_OBJECT
1957                         , HOTSPOT_JNI_CALLSTATICOBJECTMETHODA_ENTRY(env, cls, (uintptr_t)methodID),
1958                         HOTSPOT_JNI_CALLSTATICOBJECTMETHODA_RETURN(_ret_ref));
1959 DEFINE_CALLSTATICMETHODA(jint,     Int,     T_INT
1960                         , HOTSPOT_JNI_CALLSTATICINTMETHODA_ENTRY(env, cls, (uintptr_t)methodID),
1961                         HOTSPOT_JNI_CALLSTATICINTMETHODA_RETURN(_ret_ref));
1962 DEFINE_CALLSTATICMETHODA(jlong,    Long,    T_LONG
1963                         , HOTSPOT_JNI_CALLSTATICLONGMETHODA_ENTRY(env, cls, (uintptr_t)methodID),
1964                         HOTSPOT_JNI_CALLSTATICLONGMETHODA_RETURN(_ret_ref));
1965 // Float and double probes don't return value because dtrace doesn't currently support it
1966 DEFINE_CALLSTATICMETHODA(jfloat,   Float,   T_FLOAT
1967                         , HOTSPOT_JNI_CALLSTATICFLOATMETHODA_ENTRY(env, cls, (uintptr_t)methodID),
1968                         HOTSPOT_JNI_CALLSTATICFLOATMETHODA_RETURN());
1969 DEFINE_CALLSTATICMETHODA(jdouble,  Double,  T_DOUBLE
1970                         , HOTSPOT_JNI_CALLSTATICDOUBLEMETHODA_ENTRY(env, cls, (uintptr_t)methodID),
1971                         HOTSPOT_JNI_CALLSTATICDOUBLEMETHODA_RETURN());
1972 
1973 DT_VOID_RETURN_MARK_DECL(CallStaticVoidMethod
1974                          , HOTSPOT_JNI_CALLSTATICVOIDMETHOD_RETURN());
1975 DT_VOID_RETURN_MARK_DECL(CallStaticVoidMethodV
1976                          , HOTSPOT_JNI_CALLSTATICVOIDMETHODV_RETURN());
1977 DT_VOID_RETURN_MARK_DECL(CallStaticVoidMethodA
1978                          , HOTSPOT_JNI_CALLSTATICVOIDMETHODA_RETURN());
1979 
1980 JNI_ENTRY(void, jni_CallStaticVoidMethod(JNIEnv *env, jclass cls, jmethodID methodID, ...))
1981   JNIWrapper("CallStaticVoidMethod");
1982   HOTSPOT_JNI_CALLSTATICVOIDMETHOD_ENTRY(env, cls, (uintptr_t) methodID);
1983   DT_VOID_RETURN_MARK(CallStaticVoidMethod);
1984 
1985   va_list args;
1986   va_start(args, methodID);
1987   JavaValue jvalue(T_VOID);
1988   JNI_ArgumentPusherVaArg ap(methodID, args);
1989   jni_invoke_static(env, &jvalue, NULL, JNI_STATIC, methodID, &ap, CHECK);
1990   va_end(args);
1991 JNI_END
1992 
1993 
1994 JNI_ENTRY(void, jni_CallStaticVoidMethodV(JNIEnv *env, jclass cls, jmethodID methodID, va_list args))
1995   JNIWrapper("CallStaticVoidMethodV");
1996   HOTSPOT_JNI_CALLSTATICVOIDMETHODV_ENTRY(env, cls, (uintptr_t) methodID);
1997   DT_VOID_RETURN_MARK(CallStaticVoidMethodV);
1998 
1999   JavaValue jvalue(T_VOID);
2000   JNI_ArgumentPusherVaArg ap(methodID, args);
2001   jni_invoke_static(env, &jvalue, NULL, JNI_STATIC, methodID, &ap, CHECK);
2002 JNI_END
2003 
2004 
2005 JNI_ENTRY(void, jni_CallStaticVoidMethodA(JNIEnv *env, jclass cls, jmethodID methodID, const jvalue *args))
2006   JNIWrapper("CallStaticVoidMethodA");
2007   HOTSPOT_JNI_CALLSTATICVOIDMETHODA_ENTRY(env, cls, (uintptr_t) methodID);
2008   DT_VOID_RETURN_MARK(CallStaticVoidMethodA);
2009 
2010   JavaValue jvalue(T_VOID);
2011   JNI_ArgumentPusherArray ap(methodID, args);
2012   jni_invoke_static(env, &jvalue, NULL, JNI_STATIC, methodID, &ap, CHECK);
2013 JNI_END
2014 
2015 
2016 //
2017 // Accessing Fields
2018 //
2019 
2020 
2021 DT_RETURN_MARK_DECL(GetFieldID, jfieldID
2022                     , HOTSPOT_JNI_GETFIELDID_RETURN((uintptr_t)_ret_ref));
2023 
2024 JNI_ENTRY(jfieldID, jni_GetFieldID(JNIEnv *env, jclass clazz,
2025           const char *name, const char *sig))
2026   JNIWrapper("GetFieldID");
2027   HOTSPOT_JNI_GETFIELDID_ENTRY(env, clazz, (char *) name, (char *) sig);
2028   jfieldID ret = 0;
2029   DT_RETURN_MARK(GetFieldID, jfieldID, (const jfieldID&)ret);
2030 
2031   // The class should have been loaded (we have an instance of the class
2032   // passed in) so the field and signature should already be in the symbol
2033   // table.  If they're not there, the field doesn't exist.
2034   TempNewSymbol fieldname = SymbolTable::probe(name, (int)strlen(name));
2035   TempNewSymbol signame = SymbolTable::probe(sig, (int)strlen(sig));
2036   if (fieldname == NULL || signame == NULL) {
2037     THROW_MSG_0(vmSymbols::java_lang_NoSuchFieldError(), (char*) name);
2038   }
2039   Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(clazz));
2040   // Make sure class is initialized before handing id's out to fields
2041   k->initialize(CHECK_NULL);
2042 
2043   fieldDescriptor fd;
2044   if (!k->is_instance_klass() ||
2045       !InstanceKlass::cast(k)->find_field(fieldname, signame, false, &fd)) {
2046     THROW_MSG_0(vmSymbols::java_lang_NoSuchFieldError(), (char*) name);
2047   }
2048 
2049   // A jfieldID for a non-static field is simply the offset of the field within the instanceOop
2050   // It may also have hash bits for k, if VerifyJNIFields is turned on.
2051   ret = jfieldIDWorkaround::to_instance_jfieldID(k, fd.offset());
2052   return ret;
2053 JNI_END
2054 
2055 
2056 JNI_ENTRY(jobject, jni_GetObjectField(JNIEnv *env, jobject obj, jfieldID fieldID))
2057   JNIWrapper("GetObjectField");
2058   HOTSPOT_JNI_GETOBJECTFIELD_ENTRY(env, obj, (uintptr_t) fieldID);
2059   oop o = JNIHandles::resolve_non_null(obj);
2060   Klass* k = o->klass();
2061   int offset = jfieldIDWorkaround::from_instance_jfieldID(k, fieldID);
2062   // Keep JVMTI addition small and only check enabled flag here.
2063   // jni_GetField_probe() assumes that is okay to create handles.
2064   if (JvmtiExport::should_post_field_access()) {
2065     o = JvmtiExport::jni_GetField_probe(thread, obj, o, k, fieldID, false);
2066   }
2067   oop loaded_obj = HeapAccess<ON_UNKNOWN_OOP_REF>::oop_load_at(o, offset);
2068   jobject ret = JNIHandles::make_local(env, loaded_obj);
2069   HOTSPOT_JNI_GETOBJECTFIELD_RETURN(ret);
2070   return ret;
2071 JNI_END
2072 
2073 
2074 
2075 #define DEFINE_GETFIELD(Return,Fieldname,Result \
2076   , EntryProbe, ReturnProbe) \
2077 \
2078   DT_RETURN_MARK_DECL_FOR(Result, Get##Result##Field, Return \
2079   , ReturnProbe); \
2080 \
2081 JNI_QUICK_ENTRY(Return, jni_Get##Result##Field(JNIEnv *env, jobject obj, jfieldID fieldID)) \
2082   JNIWrapper("Get" XSTR(Result) "Field"); \
2083 \
2084   EntryProbe; \
2085   Return ret = 0;\
2086   DT_RETURN_MARK_FOR(Result, Get##Result##Field, Return, (const Return&)ret);\
2087 \
2088   oop o = JNIHandles::resolve_non_null(obj); \
2089   Klass* k = o->klass(); \
2090   int offset = jfieldIDWorkaround::from_instance_jfieldID(k, fieldID);  \
2091   /* Keep JVMTI addition small and only check enabled flag here.       */ \
2092   /* jni_GetField_probe_nh() assumes that is not okay to create handles */ \
2093   /* and creates a ResetNoHandleMark.                                   */ \
2094   if (JvmtiExport::should_post_field_access()) { \
2095     o = JvmtiExport::jni_GetField_probe_nh(thread, obj, o, k, fieldID, false); \
2096   } \
2097   ret = o->Fieldname##_field(offset); \
2098   return ret; \
2099 JNI_END
2100 
2101 DEFINE_GETFIELD(jboolean, bool,   Boolean
2102                 , HOTSPOT_JNI_GETBOOLEANFIELD_ENTRY(env, obj, (uintptr_t)fieldID),
2103                 HOTSPOT_JNI_GETBOOLEANFIELD_RETURN(_ret_ref))
2104 DEFINE_GETFIELD(jbyte,    byte,   Byte
2105                 , HOTSPOT_JNI_GETBYTEFIELD_ENTRY(env, obj, (uintptr_t)fieldID),
2106                 HOTSPOT_JNI_GETBYTEFIELD_RETURN(_ret_ref))
2107 DEFINE_GETFIELD(jchar,    char,   Char
2108                 , HOTSPOT_JNI_GETCHARFIELD_ENTRY(env, obj, (uintptr_t)fieldID),
2109                 HOTSPOT_JNI_GETCHARFIELD_RETURN(_ret_ref))
2110 DEFINE_GETFIELD(jshort,   short,  Short
2111                 , HOTSPOT_JNI_GETSHORTFIELD_ENTRY(env, obj, (uintptr_t)fieldID),
2112                 HOTSPOT_JNI_GETSHORTFIELD_RETURN(_ret_ref))
2113 DEFINE_GETFIELD(jint,     int,    Int
2114                 , HOTSPOT_JNI_GETINTFIELD_ENTRY(env, obj, (uintptr_t)fieldID),
2115                 HOTSPOT_JNI_GETINTFIELD_RETURN(_ret_ref))
2116 DEFINE_GETFIELD(jlong,    long,   Long
2117                 , HOTSPOT_JNI_GETLONGFIELD_ENTRY(env, obj, (uintptr_t)fieldID),
2118                 HOTSPOT_JNI_GETLONGFIELD_RETURN(_ret_ref))
2119 // Float and double probes don't return value because dtrace doesn't currently support it
2120 DEFINE_GETFIELD(jfloat,   float,  Float
2121                 , HOTSPOT_JNI_GETFLOATFIELD_ENTRY(env, obj, (uintptr_t)fieldID),
2122                 HOTSPOT_JNI_GETFLOATFIELD_RETURN())
2123 DEFINE_GETFIELD(jdouble,  double, Double
2124                 , HOTSPOT_JNI_GETDOUBLEFIELD_ENTRY(env, obj, (uintptr_t)fieldID),
2125                 HOTSPOT_JNI_GETDOUBLEFIELD_RETURN())
2126 
2127 address jni_GetBooleanField_addr() {
2128   return (address)jni_GetBooleanField;
2129 }
2130 address jni_GetByteField_addr() {
2131   return (address)jni_GetByteField;
2132 }
2133 address jni_GetCharField_addr() {
2134   return (address)jni_GetCharField;
2135 }
2136 address jni_GetShortField_addr() {
2137   return (address)jni_GetShortField;
2138 }
2139 address jni_GetIntField_addr() {
2140   return (address)jni_GetIntField;
2141 }
2142 address jni_GetLongField_addr() {
2143   return (address)jni_GetLongField;
2144 }
2145 address jni_GetFloatField_addr() {
2146   return (address)jni_GetFloatField;
2147 }
2148 address jni_GetDoubleField_addr() {
2149   return (address)jni_GetDoubleField;
2150 }
2151 
2152 JNI_QUICK_ENTRY(void, jni_SetObjectField(JNIEnv *env, jobject obj, jfieldID fieldID, jobject value))
2153   JNIWrapper("SetObjectField");
2154   HOTSPOT_JNI_SETOBJECTFIELD_ENTRY(env, obj, (uintptr_t) fieldID, value);
2155   oop o = JNIHandles::resolve_non_null(obj);
2156   Klass* k = o->klass();
2157   int offset = jfieldIDWorkaround::from_instance_jfieldID(k, fieldID);
2158   // Keep JVMTI addition small and only check enabled flag here.
2159   // jni_SetField_probe_nh() assumes that is not okay to create handles
2160   // and creates a ResetNoHandleMark.
2161   if (JvmtiExport::should_post_field_modification()) {
2162     jvalue field_value;
2163     field_value.l = value;
2164     o = JvmtiExport::jni_SetField_probe_nh(thread, obj, o, k, fieldID, false, 'L', (jvalue *)&field_value);
2165   }
2166   HeapAccess<ON_UNKNOWN_OOP_REF>::oop_store_at(o, offset, JNIHandles::resolve(value));
2167   HOTSPOT_JNI_SETOBJECTFIELD_RETURN();
2168 JNI_END
2169 
2170 
2171 #define DEFINE_SETFIELD(Argument,Fieldname,Result,SigType,unionType \
2172                         , EntryProbe, ReturnProbe) \
2173 \
2174 JNI_QUICK_ENTRY(void, jni_Set##Result##Field(JNIEnv *env, jobject obj, jfieldID fieldID, Argument value)) \
2175   JNIWrapper("Set" XSTR(Result) "Field"); \
2176 \
2177   EntryProbe; \
2178 \
2179   oop o = JNIHandles::resolve_non_null(obj); \
2180   Klass* k = o->klass(); \
2181   int offset = jfieldIDWorkaround::from_instance_jfieldID(k, fieldID);  \
2182   /* Keep JVMTI addition small and only check enabled flag here.       */ \
2183   /* jni_SetField_probe_nh() assumes that is not okay to create handles */ \
2184   /* and creates a ResetNoHandleMark.                                   */ \
2185   if (JvmtiExport::should_post_field_modification()) { \
2186     jvalue field_value; \
2187     field_value.unionType = value; \
2188     o = JvmtiExport::jni_SetField_probe_nh(thread, obj, o, k, fieldID, false, SigType, (jvalue *)&field_value); \
2189   } \
2190   if (SigType == 'Z') { value = ((jboolean)value) & 1; } \
2191   o->Fieldname##_field_put(offset, value); \
2192   ReturnProbe; \
2193 JNI_END
2194 
2195 DEFINE_SETFIELD(jboolean, bool,   Boolean, 'Z', z
2196                 , HOTSPOT_JNI_SETBOOLEANFIELD_ENTRY(env, obj, (uintptr_t)fieldID, value),
2197                 HOTSPOT_JNI_SETBOOLEANFIELD_RETURN())
2198 DEFINE_SETFIELD(jbyte,    byte,   Byte,    'B', b
2199                 , HOTSPOT_JNI_SETBYTEFIELD_ENTRY(env, obj, (uintptr_t)fieldID, value),
2200                 HOTSPOT_JNI_SETBYTEFIELD_RETURN())
2201 DEFINE_SETFIELD(jchar,    char,   Char,    'C', c
2202                 , HOTSPOT_JNI_SETCHARFIELD_ENTRY(env, obj, (uintptr_t)fieldID, value),
2203                 HOTSPOT_JNI_SETCHARFIELD_RETURN())
2204 DEFINE_SETFIELD(jshort,   short,  Short,   'S', s
2205                 , HOTSPOT_JNI_SETSHORTFIELD_ENTRY(env, obj, (uintptr_t)fieldID, value),
2206                 HOTSPOT_JNI_SETSHORTFIELD_RETURN())
2207 DEFINE_SETFIELD(jint,     int,    Int,     'I', i
2208                 , HOTSPOT_JNI_SETINTFIELD_ENTRY(env, obj, (uintptr_t)fieldID, value),
2209                 HOTSPOT_JNI_SETINTFIELD_RETURN())
2210 DEFINE_SETFIELD(jlong,    long,   Long,    'J', j
2211                 , HOTSPOT_JNI_SETLONGFIELD_ENTRY(env, obj, (uintptr_t)fieldID, value),
2212                 HOTSPOT_JNI_SETLONGFIELD_RETURN())
2213 // Float and double probes don't return value because dtrace doesn't currently support it
2214 DEFINE_SETFIELD(jfloat,   float,  Float,   'F', f
2215                 , HOTSPOT_JNI_SETFLOATFIELD_ENTRY(env, obj, (uintptr_t)fieldID),
2216                 HOTSPOT_JNI_SETFLOATFIELD_RETURN())
2217 DEFINE_SETFIELD(jdouble,  double, Double,  'D', d
2218                 , HOTSPOT_JNI_SETDOUBLEFIELD_ENTRY(env, obj, (uintptr_t)fieldID),
2219                 HOTSPOT_JNI_SETDOUBLEFIELD_RETURN())
2220 
2221 DT_RETURN_MARK_DECL(ToReflectedField, jobject
2222                     , HOTSPOT_JNI_TOREFLECTEDFIELD_RETURN(_ret_ref));
2223 
2224 JNI_ENTRY(jobject, jni_ToReflectedField(JNIEnv *env, jclass cls, jfieldID fieldID, jboolean isStatic))
2225   JNIWrapper("ToReflectedField");
2226   HOTSPOT_JNI_TOREFLECTEDFIELD_ENTRY(env, cls, (uintptr_t) fieldID, isStatic);
2227   jobject ret = NULL;
2228   DT_RETURN_MARK(ToReflectedField, jobject, (const jobject&)ret);
2229 
2230   fieldDescriptor fd;
2231   bool found = false;
2232   Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls));
2233 
2234   assert(jfieldIDWorkaround::is_static_jfieldID(fieldID) == (isStatic != 0), "invalid fieldID");
2235 
2236   if (isStatic) {
2237     // Static field. The fieldID a JNIid specifying the field holder and the offset within the Klass*.
2238     JNIid* id = jfieldIDWorkaround::from_static_jfieldID(fieldID);
2239     assert(id->is_static_field_id(), "invalid static field id");
2240     found = id->find_local_field(&fd);
2241   } else {
2242     // Non-static field. The fieldID is really the offset of the field within the instanceOop.
2243     int offset = jfieldIDWorkaround::from_instance_jfieldID(k, fieldID);
2244     found = InstanceKlass::cast(k)->find_field_from_offset(offset, false, &fd);
2245   }
2246   assert(found, "bad fieldID passed into jni_ToReflectedField");
2247   oop reflected = Reflection::new_field(&fd, CHECK_NULL);
2248   ret = JNIHandles::make_local(env, reflected);
2249   return ret;
2250 JNI_END
2251 
2252 
2253 //
2254 // Accessing Static Fields
2255 //
2256 DT_RETURN_MARK_DECL(GetStaticFieldID, jfieldID
2257                     , HOTSPOT_JNI_GETSTATICFIELDID_RETURN((uintptr_t)_ret_ref));
2258 
2259 JNI_ENTRY(jfieldID, jni_GetStaticFieldID(JNIEnv *env, jclass clazz,
2260           const char *name, const char *sig))
2261   JNIWrapper("GetStaticFieldID");
2262   HOTSPOT_JNI_GETSTATICFIELDID_ENTRY(env, clazz, (char *) name, (char *) sig);
2263   jfieldID ret = NULL;
2264   DT_RETURN_MARK(GetStaticFieldID, jfieldID, (const jfieldID&)ret);
2265 
2266   // The class should have been loaded (we have an instance of the class
2267   // passed in) so the field and signature should already be in the symbol
2268   // table.  If they're not there, the field doesn't exist.
2269   TempNewSymbol fieldname = SymbolTable::probe(name, (int)strlen(name));
2270   TempNewSymbol signame = SymbolTable::probe(sig, (int)strlen(sig));
2271   if (fieldname == NULL || signame == NULL) {
2272     THROW_MSG_0(vmSymbols::java_lang_NoSuchFieldError(), (char*) name);
2273   }
2274   Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(clazz));
2275   // Make sure class is initialized before handing id's out to static fields
2276   k->initialize(CHECK_NULL);
2277 
2278   fieldDescriptor fd;
2279   if (!k->is_instance_klass() ||
2280       !InstanceKlass::cast(k)->find_field(fieldname, signame, true, &fd)) {
2281     THROW_MSG_0(vmSymbols::java_lang_NoSuchFieldError(), (char*) name);
2282   }
2283 
2284   // A jfieldID for a static field is a JNIid specifying the field holder and the offset within the Klass*
2285   JNIid* id = fd.field_holder()->jni_id_for(fd.offset());
2286   debug_only(id->set_is_static_field_id();)
2287 
2288   debug_only(id->verify(fd.field_holder()));
2289 
2290   ret = jfieldIDWorkaround::to_static_jfieldID(id);
2291   return ret;
2292 JNI_END
2293 
2294 
2295 JNI_ENTRY(jobject, jni_GetStaticObjectField(JNIEnv *env, jclass clazz, jfieldID fieldID))
2296   JNIWrapper("GetStaticObjectField");
2297   HOTSPOT_JNI_GETSTATICOBJECTFIELD_ENTRY(env, clazz, (uintptr_t) fieldID);
2298 #if INCLUDE_JNI_CHECK
2299   DEBUG_ONLY(Klass* param_k = jniCheck::validate_class(thread, clazz);)
2300 #endif // INCLUDE_JNI_CHECK
2301   JNIid* id = jfieldIDWorkaround::from_static_jfieldID(fieldID);
2302   assert(id->is_static_field_id(), "invalid static field id");
2303   // Keep JVMTI addition small and only check enabled flag here.
2304   // jni_GetField_probe() assumes that is okay to create handles.
2305   if (JvmtiExport::should_post_field_access()) {
2306     JvmtiExport::jni_GetField_probe(thread, NULL, NULL, id->holder(), fieldID, true);
2307   }
2308   jobject ret = JNIHandles::make_local(id->holder()->java_mirror()->obj_field(id->offset()));
2309   HOTSPOT_JNI_GETSTATICOBJECTFIELD_RETURN(ret);
2310   return ret;
2311 JNI_END
2312 
2313 
2314 #define DEFINE_GETSTATICFIELD(Return,Fieldname,Result \
2315                               , EntryProbe, ReturnProbe) \
2316 \
2317   DT_RETURN_MARK_DECL_FOR(Result, GetStatic##Result##Field, Return \
2318                           , ReturnProbe);                                          \
2319 \
2320 JNI_ENTRY(Return, jni_GetStatic##Result##Field(JNIEnv *env, jclass clazz, jfieldID fieldID)) \
2321   JNIWrapper("GetStatic" XSTR(Result) "Field"); \
2322   EntryProbe; \
2323   Return ret = 0;\
2324   DT_RETURN_MARK_FOR(Result, GetStatic##Result##Field, Return, \
2325                      (const Return&)ret);\
2326   JNIid* id = jfieldIDWorkaround::from_static_jfieldID(fieldID); \
2327   assert(id->is_static_field_id(), "invalid static field id"); \
2328   /* Keep JVMTI addition small and only check enabled flag here. */ \
2329   /* jni_GetField_probe() assumes that is okay to create handles. */ \
2330   if (JvmtiExport::should_post_field_access()) { \
2331     JvmtiExport::jni_GetField_probe(thread, NULL, NULL, id->holder(), fieldID, true); \
2332   } \
2333   ret = id->holder()->java_mirror()-> Fieldname##_field (id->offset()); \
2334   return ret;\
2335 JNI_END
2336 
2337 DEFINE_GETSTATICFIELD(jboolean, bool,   Boolean
2338                       , HOTSPOT_JNI_GETSTATICBOOLEANFIELD_ENTRY(env, clazz, (uintptr_t) fieldID), HOTSPOT_JNI_GETSTATICBOOLEANFIELD_RETURN(_ret_ref))
2339 DEFINE_GETSTATICFIELD(jbyte,    byte,   Byte
2340                       , HOTSPOT_JNI_GETSTATICBYTEFIELD_ENTRY(env, clazz, (uintptr_t) fieldID),    HOTSPOT_JNI_GETSTATICBYTEFIELD_RETURN(_ret_ref)   )
2341 DEFINE_GETSTATICFIELD(jchar,    char,   Char
2342                       , HOTSPOT_JNI_GETSTATICCHARFIELD_ENTRY(env, clazz, (uintptr_t) fieldID),    HOTSPOT_JNI_GETSTATICCHARFIELD_RETURN(_ret_ref)   )
2343 DEFINE_GETSTATICFIELD(jshort,   short,  Short
2344                       , HOTSPOT_JNI_GETSTATICSHORTFIELD_ENTRY(env, clazz, (uintptr_t) fieldID),   HOTSPOT_JNI_GETSTATICSHORTFIELD_RETURN(_ret_ref)  )
2345 DEFINE_GETSTATICFIELD(jint,     int,    Int
2346                       , HOTSPOT_JNI_GETSTATICINTFIELD_ENTRY(env, clazz, (uintptr_t) fieldID),     HOTSPOT_JNI_GETSTATICINTFIELD_RETURN(_ret_ref)    )
2347 DEFINE_GETSTATICFIELD(jlong,    long,   Long
2348                       , HOTSPOT_JNI_GETSTATICLONGFIELD_ENTRY(env, clazz, (uintptr_t) fieldID),    HOTSPOT_JNI_GETSTATICLONGFIELD_RETURN(_ret_ref)   )
2349 // Float and double probes don't return value because dtrace doesn't currently support it
2350 DEFINE_GETSTATICFIELD(jfloat,   float,  Float
2351                       , HOTSPOT_JNI_GETSTATICFLOATFIELD_ENTRY(env, clazz, (uintptr_t) fieldID),   HOTSPOT_JNI_GETSTATICFLOATFIELD_RETURN()          )
2352 DEFINE_GETSTATICFIELD(jdouble,  double, Double
2353                       , HOTSPOT_JNI_GETSTATICDOUBLEFIELD_ENTRY(env, clazz, (uintptr_t) fieldID),  HOTSPOT_JNI_GETSTATICDOUBLEFIELD_RETURN()         )
2354 
2355 JNI_ENTRY(void, jni_SetStaticObjectField(JNIEnv *env, jclass clazz, jfieldID fieldID, jobject value))
2356   JNIWrapper("SetStaticObjectField");
2357  HOTSPOT_JNI_SETSTATICOBJECTFIELD_ENTRY(env, clazz, (uintptr_t) fieldID, value);
2358   JNIid* id = jfieldIDWorkaround::from_static_jfieldID(fieldID);
2359   assert(id->is_static_field_id(), "invalid static field id");
2360   // Keep JVMTI addition small and only check enabled flag here.
2361   // jni_SetField_probe() assumes that is okay to create handles.
2362   if (JvmtiExport::should_post_field_modification()) {
2363     jvalue field_value;
2364     field_value.l = value;
2365     JvmtiExport::jni_SetField_probe(thread, NULL, NULL, id->holder(), fieldID, true, 'L', (jvalue *)&field_value);
2366   }
2367   id->holder()->java_mirror()->obj_field_put(id->offset(), JNIHandles::resolve(value));
2368   HOTSPOT_JNI_SETSTATICOBJECTFIELD_RETURN();
2369 JNI_END
2370 
2371 
2372 
2373 #define DEFINE_SETSTATICFIELD(Argument,Fieldname,Result,SigType,unionType \
2374                               , EntryProbe, ReturnProbe) \
2375 \
2376 JNI_ENTRY(void, jni_SetStatic##Result##Field(JNIEnv *env, jclass clazz, jfieldID fieldID, Argument value)) \
2377   JNIWrapper("SetStatic" XSTR(Result) "Field"); \
2378   EntryProbe; \
2379 \
2380   JNIid* id = jfieldIDWorkaround::from_static_jfieldID(fieldID); \
2381   assert(id->is_static_field_id(), "invalid static field id"); \
2382   /* Keep JVMTI addition small and only check enabled flag here. */ \
2383   /* jni_SetField_probe() assumes that is okay to create handles. */ \
2384   if (JvmtiExport::should_post_field_modification()) { \
2385     jvalue field_value; \
2386     field_value.unionType = value; \
2387     JvmtiExport::jni_SetField_probe(thread, NULL, NULL, id->holder(), fieldID, true, SigType, (jvalue *)&field_value); \
2388   } \
2389   if (SigType == 'Z') { value = ((jboolean)value) & 1; } \
2390   id->holder()->java_mirror()-> Fieldname##_field_put (id->offset(), value); \
2391   ReturnProbe;\
2392 JNI_END
2393 
2394 DEFINE_SETSTATICFIELD(jboolean, bool,   Boolean, 'Z', z
2395                       , HOTSPOT_JNI_SETSTATICBOOLEANFIELD_ENTRY(env, clazz, (uintptr_t)fieldID, value),
2396                       HOTSPOT_JNI_SETSTATICBOOLEANFIELD_RETURN())
2397 DEFINE_SETSTATICFIELD(jbyte,    byte,   Byte,    'B', b
2398                       , HOTSPOT_JNI_SETSTATICBYTEFIELD_ENTRY(env, clazz, (uintptr_t) fieldID, value),
2399                       HOTSPOT_JNI_SETSTATICBYTEFIELD_RETURN())
2400 DEFINE_SETSTATICFIELD(jchar,    char,   Char,    'C', c
2401                       , HOTSPOT_JNI_SETSTATICCHARFIELD_ENTRY(env, clazz, (uintptr_t) fieldID, value),
2402                       HOTSPOT_JNI_SETSTATICCHARFIELD_RETURN())
2403 DEFINE_SETSTATICFIELD(jshort,   short,  Short,   'S', s
2404                       , HOTSPOT_JNI_SETSTATICSHORTFIELD_ENTRY(env, clazz, (uintptr_t) fieldID, value),
2405                       HOTSPOT_JNI_SETSTATICSHORTFIELD_RETURN())
2406 DEFINE_SETSTATICFIELD(jint,     int,    Int,     'I', i
2407                       , HOTSPOT_JNI_SETSTATICINTFIELD_ENTRY(env, clazz, (uintptr_t) fieldID, value),
2408                       HOTSPOT_JNI_SETSTATICINTFIELD_RETURN())
2409 DEFINE_SETSTATICFIELD(jlong,    long,   Long,    'J', j
2410                       , HOTSPOT_JNI_SETSTATICLONGFIELD_ENTRY(env, clazz, (uintptr_t) fieldID, value),
2411                       HOTSPOT_JNI_SETSTATICLONGFIELD_RETURN())
2412 // Float and double probes don't return value because dtrace doesn't currently support it
2413 DEFINE_SETSTATICFIELD(jfloat,   float,  Float,   'F', f
2414                       , HOTSPOT_JNI_SETSTATICFLOATFIELD_ENTRY(env, clazz, (uintptr_t) fieldID),
2415                       HOTSPOT_JNI_SETSTATICFLOATFIELD_RETURN())
2416 DEFINE_SETSTATICFIELD(jdouble,  double, Double,  'D', d
2417                       , HOTSPOT_JNI_SETSTATICDOUBLEFIELD_ENTRY(env, clazz, (uintptr_t) fieldID),
2418                       HOTSPOT_JNI_SETSTATICDOUBLEFIELD_RETURN())
2419 
2420 //
2421 // String Operations
2422 //
2423 
2424 // Unicode Interface
2425 
2426 DT_RETURN_MARK_DECL(NewString, jstring
2427                     , HOTSPOT_JNI_NEWSTRING_RETURN(_ret_ref));
2428 
2429 JNI_ENTRY(jstring, jni_NewString(JNIEnv *env, const jchar *unicodeChars, jsize len))
2430   JNIWrapper("NewString");
2431  HOTSPOT_JNI_NEWSTRING_ENTRY(env, (uint16_t *) unicodeChars, len);
2432   jstring ret = NULL;
2433   DT_RETURN_MARK(NewString, jstring, (const jstring&)ret);
2434   oop string=java_lang_String::create_oop_from_unicode((jchar*) unicodeChars, len, CHECK_NULL);
2435   ret = (jstring) JNIHandles::make_local(env, string);
2436   return ret;
2437 JNI_END
2438 
2439 
2440 JNI_QUICK_ENTRY(jsize, jni_GetStringLength(JNIEnv *env, jstring string))
2441   JNIWrapper("GetStringLength");
2442   HOTSPOT_JNI_GETSTRINGLENGTH_ENTRY(env, string);
2443   jsize ret = 0;
2444   oop s = JNIHandles::resolve_non_null(string);
2445   if (java_lang_String::value(s) != NULL) {
2446     ret = java_lang_String::length(s);
2447   }
2448  HOTSPOT_JNI_GETSTRINGLENGTH_RETURN(ret);
2449   return ret;
2450 JNI_END
2451 
2452 
2453 JNI_QUICK_ENTRY(const jchar*, jni_GetStringChars(
2454   JNIEnv *env, jstring string, jboolean *isCopy))
2455   JNIWrapper("GetStringChars");
2456  HOTSPOT_JNI_GETSTRINGCHARS_ENTRY(env, string, (uintptr_t *) isCopy);
2457   jchar* buf = NULL;
2458   oop s = JNIHandles::resolve_non_null(string);
2459   typeArrayOop s_value = java_lang_String::value(s);
2460   if (s_value != NULL) {
2461     int s_len = java_lang_String::length(s);
2462     bool is_latin1 = java_lang_String::is_latin1(s);
2463     buf = NEW_C_HEAP_ARRAY_RETURN_NULL(jchar, s_len + 1, mtInternal);  // add one for zero termination
2464     /* JNI Specification states return NULL on OOM */
2465     if (buf != NULL) {
2466       if (s_len > 0) {
2467         if (!is_latin1) {
2468           memcpy(buf, s_value->char_at_addr(0), sizeof(jchar)*s_len);

2469         } else {
2470           for (int i = 0; i < s_len; i++) {
2471             buf[i] = ((jchar) s_value->byte_at(i)) & 0xff;
2472           }
2473         }
2474       }
2475       buf[s_len] = 0;
2476       //%note jni_5
2477       if (isCopy != NULL) {
2478         *isCopy = JNI_TRUE;
2479       }
2480     }
2481   }
2482   HOTSPOT_JNI_GETSTRINGCHARS_RETURN(buf);
2483   return buf;
2484 JNI_END
2485 
2486 
2487 JNI_QUICK_ENTRY(void, jni_ReleaseStringChars(JNIEnv *env, jstring str, const jchar *chars))
2488   JNIWrapper("ReleaseStringChars");
2489   HOTSPOT_JNI_RELEASESTRINGCHARS_ENTRY(env, str, (uint16_t *) chars);
2490   //%note jni_6
2491   if (chars != NULL) {
2492     // Since String objects are supposed to be immutable, don't copy any
2493     // new data back.  A bad user will have to go after the char array.
2494     FreeHeap((void*) chars);
2495   }
2496   HOTSPOT_JNI_RELEASESTRINGCHARS_RETURN();
2497 JNI_END
2498 
2499 
2500 // UTF Interface
2501 
2502 DT_RETURN_MARK_DECL(NewStringUTF, jstring
2503                     , HOTSPOT_JNI_NEWSTRINGUTF_RETURN(_ret_ref));
2504 
2505 JNI_ENTRY(jstring, jni_NewStringUTF(JNIEnv *env, const char *bytes))
2506   JNIWrapper("NewStringUTF");
2507   HOTSPOT_JNI_NEWSTRINGUTF_ENTRY(env, (char *) bytes);
2508   jstring ret;
2509   DT_RETURN_MARK(NewStringUTF, jstring, (const jstring&)ret);
2510 
2511   oop result = java_lang_String::create_oop_from_str((char*) bytes, CHECK_NULL);
2512   ret = (jstring) JNIHandles::make_local(env, result);
2513   return ret;
2514 JNI_END
2515 
2516 
2517 JNI_ENTRY(jsize, jni_GetStringUTFLength(JNIEnv *env, jstring string))
2518   JNIWrapper("GetStringUTFLength");
2519  HOTSPOT_JNI_GETSTRINGUTFLENGTH_ENTRY(env, string);
2520   jsize ret = 0;
2521   oop java_string = JNIHandles::resolve_non_null(string);
2522   if (java_lang_String::value(java_string) != NULL) {
2523     ret = java_lang_String::utf8_length(java_string);
2524   }
2525   HOTSPOT_JNI_GETSTRINGUTFLENGTH_RETURN(ret);
2526   return ret;
2527 JNI_END
2528 
2529 
2530 JNI_ENTRY(const char*, jni_GetStringUTFChars(JNIEnv *env, jstring string, jboolean *isCopy))
2531   JNIWrapper("GetStringUTFChars");
2532  HOTSPOT_JNI_GETSTRINGUTFCHARS_ENTRY(env, string, (uintptr_t *) isCopy);
2533   char* result = NULL;
2534   oop java_string = JNIHandles::resolve_non_null(string);
2535   if (java_lang_String::value(java_string) != NULL) {
2536     size_t length = java_lang_String::utf8_length(java_string);
2537     /* JNI Specification states return NULL on OOM */
2538     result = AllocateHeap(length + 1, mtInternal, 0, AllocFailStrategy::RETURN_NULL);
2539     if (result != NULL) {
2540       java_lang_String::as_utf8_string(java_string, result, (int) length + 1);
2541       if (isCopy != NULL) {
2542         *isCopy = JNI_TRUE;
2543       }
2544     }
2545   }
2546  HOTSPOT_JNI_GETSTRINGUTFCHARS_RETURN(result);
2547   return result;
2548 JNI_END
2549 
2550 
2551 JNI_LEAF(void, jni_ReleaseStringUTFChars(JNIEnv *env, jstring str, const char *chars))
2552   JNIWrapper("ReleaseStringUTFChars");
2553  HOTSPOT_JNI_RELEASESTRINGUTFCHARS_ENTRY(env, str, (char *) chars);
2554   if (chars != NULL) {
2555     FreeHeap((char*) chars);
2556   }
2557 HOTSPOT_JNI_RELEASESTRINGUTFCHARS_RETURN();
2558 JNI_END
2559 
2560 
2561 JNI_QUICK_ENTRY(jsize, jni_GetArrayLength(JNIEnv *env, jarray array))
2562   JNIWrapper("GetArrayLength");
2563  HOTSPOT_JNI_GETARRAYLENGTH_ENTRY(env, array);
2564   arrayOop a = arrayOop(JNIHandles::resolve_non_null(array));
2565   assert(a->is_array(), "must be array");
2566   jsize ret = a->length();
2567  HOTSPOT_JNI_GETARRAYLENGTH_RETURN(ret);
2568   return ret;
2569 JNI_END
2570 
2571 
2572 //
2573 // Object Array Operations
2574 //
2575 
2576 DT_RETURN_MARK_DECL(NewObjectArray, jobjectArray
2577                     , HOTSPOT_JNI_NEWOBJECTARRAY_RETURN(_ret_ref));
2578 
2579 JNI_ENTRY(jobjectArray, jni_NewObjectArray(JNIEnv *env, jsize length, jclass elementClass, jobject initialElement))
2580   JNIWrapper("NewObjectArray");
2581  HOTSPOT_JNI_NEWOBJECTARRAY_ENTRY(env, length, elementClass, initialElement);
2582   jobjectArray ret = NULL;
2583   DT_RETURN_MARK(NewObjectArray, jobjectArray, (const jobjectArray&)ret);
2584   Klass* ek = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(elementClass));
2585   Klass* ak = ek->array_klass(CHECK_NULL);
2586   ObjArrayKlass::cast(ak)->initialize(CHECK_NULL);
2587   objArrayOop result = ObjArrayKlass::cast(ak)->allocate(length, CHECK_NULL);
2588   oop initial_value = JNIHandles::resolve(initialElement);
2589   if (initial_value != NULL) {  // array already initialized with NULL
2590     for (int index = 0; index < length; index++) {
2591       result->obj_at_put(index, initial_value);
2592     }
2593   }
2594   ret = (jobjectArray) JNIHandles::make_local(env, result);
2595   return ret;
2596 JNI_END
2597 
2598 DT_RETURN_MARK_DECL(GetObjectArrayElement, jobject
2599                     , HOTSPOT_JNI_GETOBJECTARRAYELEMENT_RETURN(_ret_ref));
2600 
2601 JNI_ENTRY(jobject, jni_GetObjectArrayElement(JNIEnv *env, jobjectArray array, jsize index))
2602   JNIWrapper("GetObjectArrayElement");
2603  HOTSPOT_JNI_GETOBJECTARRAYELEMENT_ENTRY(env, array, index);
2604   jobject ret = NULL;
2605   DT_RETURN_MARK(GetObjectArrayElement, jobject, (const jobject&)ret);
2606   objArrayOop a = objArrayOop(JNIHandles::resolve_non_null(array));
2607   if (a->is_within_bounds(index)) {
2608     ret = JNIHandles::make_local(env, a->obj_at(index));
2609     return ret;
2610   } else {
2611     char buf[jintAsStringSize];
2612     sprintf(buf, "%d", index);
2613     THROW_MSG_0(vmSymbols::java_lang_ArrayIndexOutOfBoundsException(), buf);
2614   }
2615 JNI_END
2616 
2617 DT_VOID_RETURN_MARK_DECL(SetObjectArrayElement
2618                          , HOTSPOT_JNI_SETOBJECTARRAYELEMENT_RETURN());
2619 
2620 JNI_ENTRY(void, jni_SetObjectArrayElement(JNIEnv *env, jobjectArray array, jsize index, jobject value))
2621   JNIWrapper("SetObjectArrayElement");
2622  HOTSPOT_JNI_SETOBJECTARRAYELEMENT_ENTRY(env, array, index, value);
2623   DT_VOID_RETURN_MARK(SetObjectArrayElement);
2624 
2625   objArrayOop a = objArrayOop(JNIHandles::resolve_non_null(array));
2626   oop v = JNIHandles::resolve(value);
2627   if (a->is_within_bounds(index)) {
2628     if (v == NULL || v->is_a(ObjArrayKlass::cast(a->klass())->element_klass())) {
2629       a->obj_at_put(index, v);
2630     } else {
2631       THROW(vmSymbols::java_lang_ArrayStoreException());
2632     }
2633   } else {
2634     char buf[jintAsStringSize];
2635     sprintf(buf, "%d", index);
2636     THROW_MSG(vmSymbols::java_lang_ArrayIndexOutOfBoundsException(), buf);
2637   }
2638 JNI_END
2639 
2640 
2641 
2642 #define DEFINE_NEWSCALARARRAY(Return,Allocator,Result \
2643                               ,EntryProbe,ReturnProbe)  \
2644 \
2645   DT_RETURN_MARK_DECL(New##Result##Array, Return \
2646                       , ReturnProbe); \
2647 \
2648 JNI_ENTRY(Return, \
2649           jni_New##Result##Array(JNIEnv *env, jsize len)) \
2650   JNIWrapper("New" XSTR(Result) "Array"); \
2651   EntryProbe; \
2652   Return ret = NULL;\
2653   DT_RETURN_MARK(New##Result##Array, Return, (const Return&)ret);\
2654 \
2655   oop obj= oopFactory::Allocator(len, CHECK_0); \
2656   ret = (Return) JNIHandles::make_local(env, obj); \
2657   return ret;\
2658 JNI_END
2659 
2660 DEFINE_NEWSCALARARRAY(jbooleanArray, new_boolArray,   Boolean,
2661                       HOTSPOT_JNI_NEWBOOLEANARRAY_ENTRY(env, len),
2662                       HOTSPOT_JNI_NEWBOOLEANARRAY_RETURN(_ret_ref))
2663 DEFINE_NEWSCALARARRAY(jbyteArray,    new_byteArray,   Byte,
2664                       HOTSPOT_JNI_NEWBYTEARRAY_ENTRY(env, len),
2665                       HOTSPOT_JNI_NEWBYTEARRAY_RETURN(_ret_ref))
2666 DEFINE_NEWSCALARARRAY(jshortArray,   new_shortArray,  Short,
2667                       HOTSPOT_JNI_NEWSHORTARRAY_ENTRY(env, len),
2668                       HOTSPOT_JNI_NEWSHORTARRAY_RETURN(_ret_ref))
2669 DEFINE_NEWSCALARARRAY(jcharArray,    new_charArray,   Char,
2670                       HOTSPOT_JNI_NEWCHARARRAY_ENTRY(env, len),
2671                       HOTSPOT_JNI_NEWCHARARRAY_RETURN(_ret_ref))
2672 DEFINE_NEWSCALARARRAY(jintArray,     new_intArray,    Int,
2673                       HOTSPOT_JNI_NEWINTARRAY_ENTRY(env, len),
2674                       HOTSPOT_JNI_NEWINTARRAY_RETURN(_ret_ref))
2675 DEFINE_NEWSCALARARRAY(jlongArray,    new_longArray,   Long,
2676                       HOTSPOT_JNI_NEWLONGARRAY_ENTRY(env, len),
2677                       HOTSPOT_JNI_NEWLONGARRAY_RETURN(_ret_ref))
2678 DEFINE_NEWSCALARARRAY(jfloatArray,   new_singleArray, Float,
2679                       HOTSPOT_JNI_NEWFLOATARRAY_ENTRY(env, len),
2680                       HOTSPOT_JNI_NEWFLOATARRAY_RETURN(_ret_ref))
2681 DEFINE_NEWSCALARARRAY(jdoubleArray,  new_doubleArray, Double,
2682                       HOTSPOT_JNI_NEWDOUBLEARRAY_ENTRY(env, len),
2683                       HOTSPOT_JNI_NEWDOUBLEARRAY_RETURN(_ret_ref))
2684 
2685 // Return an address which will fault if the caller writes to it.
2686 
2687 static char* get_bad_address() {
2688   static char* bad_address = NULL;
2689   if (bad_address == NULL) {
2690     size_t size = os::vm_allocation_granularity();
2691     bad_address = os::reserve_memory(size);
2692     if (bad_address != NULL) {
2693       os::protect_memory(bad_address, size, os::MEM_PROT_READ,
2694                          /*is_committed*/false);
2695       MemTracker::record_virtual_memory_type((void*)bad_address, mtInternal);
2696     }
2697   }
2698   return bad_address;
2699 }
2700 
2701 
2702 
2703 #define DEFINE_GETSCALARARRAYELEMENTS(ElementTag,ElementType,Result, Tag \
2704                                       , EntryProbe, ReturnProbe) \
2705 \
2706 JNI_QUICK_ENTRY(ElementType*, \
2707           jni_Get##Result##ArrayElements(JNIEnv *env, ElementType##Array array, jboolean *isCopy)) \
2708   JNIWrapper("Get" XSTR(Result) "ArrayElements"); \
2709   EntryProbe; \
2710   /* allocate an chunk of memory in c land */ \
2711   typeArrayOop a = typeArrayOop(JNIHandles::resolve_non_null(array)); \
2712   ElementType* result; \
2713   int len = a->length(); \
2714   if (len == 0) { \
2715     /* Empty array: legal but useless, can't return NULL. \
2716      * Return a pointer to something useless. \
2717      * Avoid asserts in typeArrayOop. */ \
2718     result = (ElementType*)get_bad_address(); \
2719   } else { \
2720     /* JNI Specification states return NULL on OOM */                    \
2721     result = NEW_C_HEAP_ARRAY_RETURN_NULL(ElementType, len, mtInternal); \
2722     if (result != NULL) {                                                \
2723       /* copy the array to the c chunk */                                \
2724       memcpy(result, a->Tag##_at_addr(0), sizeof(ElementType)*len);      \

2725       if (isCopy) {                                                      \
2726         *isCopy = JNI_TRUE;                                              \
2727       }                                                                  \
2728     }                                                                    \
2729   } \
2730   ReturnProbe; \
2731   return result; \
2732 JNI_END
2733 
2734 DEFINE_GETSCALARARRAYELEMENTS(T_BOOLEAN, jboolean, Boolean, bool
2735                               , HOTSPOT_JNI_GETBOOLEANARRAYELEMENTS_ENTRY(env, array, (uintptr_t *) isCopy),
2736                               HOTSPOT_JNI_GETBOOLEANARRAYELEMENTS_RETURN((uintptr_t*)result))
2737 DEFINE_GETSCALARARRAYELEMENTS(T_BYTE,    jbyte,    Byte,    byte
2738                               , HOTSPOT_JNI_GETBYTEARRAYELEMENTS_ENTRY(env, array, (uintptr_t *) isCopy),
2739                               HOTSPOT_JNI_GETBYTEARRAYELEMENTS_RETURN((char*)result))
2740 DEFINE_GETSCALARARRAYELEMENTS(T_SHORT,   jshort,   Short,   short
2741                               , HOTSPOT_JNI_GETSHORTARRAYELEMENTS_ENTRY(env, (uint16_t*) array, (uintptr_t *) isCopy),
2742                               HOTSPOT_JNI_GETSHORTARRAYELEMENTS_RETURN((uint16_t*)result))
2743 DEFINE_GETSCALARARRAYELEMENTS(T_CHAR,    jchar,    Char,    char
2744                               , HOTSPOT_JNI_GETCHARARRAYELEMENTS_ENTRY(env, (uint16_t*) array, (uintptr_t *) isCopy),
2745                               HOTSPOT_JNI_GETCHARARRAYELEMENTS_RETURN(result))
2746 DEFINE_GETSCALARARRAYELEMENTS(T_INT,     jint,     Int,     int
2747                               , HOTSPOT_JNI_GETINTARRAYELEMENTS_ENTRY(env, array, (uintptr_t *) isCopy),
2748                               HOTSPOT_JNI_GETINTARRAYELEMENTS_RETURN((uint32_t*)result))
2749 DEFINE_GETSCALARARRAYELEMENTS(T_LONG,    jlong,    Long,    long
2750                               , HOTSPOT_JNI_GETLONGARRAYELEMENTS_ENTRY(env, array, (uintptr_t *) isCopy),
2751                               HOTSPOT_JNI_GETLONGARRAYELEMENTS_RETURN(((uintptr_t*)result)))
2752 // Float and double probes don't return value because dtrace doesn't currently support it
2753 DEFINE_GETSCALARARRAYELEMENTS(T_FLOAT,   jfloat,   Float,   float
2754                               , HOTSPOT_JNI_GETFLOATARRAYELEMENTS_ENTRY(env, array, (uintptr_t *) isCopy),
2755                               HOTSPOT_JNI_GETFLOATARRAYELEMENTS_RETURN(result))
2756 DEFINE_GETSCALARARRAYELEMENTS(T_DOUBLE,  jdouble,  Double,  double
2757                               , HOTSPOT_JNI_GETDOUBLEARRAYELEMENTS_ENTRY(env, array, (uintptr_t *) isCopy),
2758                               HOTSPOT_JNI_GETDOUBLEARRAYELEMENTS_RETURN(result))
2759 
2760 
2761 #define DEFINE_RELEASESCALARARRAYELEMENTS(ElementTag,ElementType,Result,Tag \
2762                                           , EntryProbe, ReturnProbe);\
2763 \
2764 JNI_QUICK_ENTRY(void, \
2765           jni_Release##Result##ArrayElements(JNIEnv *env, ElementType##Array array, \
2766                                              ElementType *buf, jint mode)) \
2767   JNIWrapper("Release" XSTR(Result) "ArrayElements"); \
2768   EntryProbe; \
2769   typeArrayOop a = typeArrayOop(JNIHandles::resolve_non_null(array)); \
2770   int len = a->length(); \
2771   if (len != 0) {   /* Empty array:  nothing to free or copy. */  \
2772     if ((mode == 0) || (mode == JNI_COMMIT)) { \
2773       memcpy(a->Tag##_at_addr(0), buf, sizeof(ElementType)*len); \
2774     } \
2775     if ((mode == 0) || (mode == JNI_ABORT)) { \
2776       FreeHeap(buf); \
2777     } \
2778   } \
2779   ReturnProbe; \
2780 JNI_END
2781 
2782 DEFINE_RELEASESCALARARRAYELEMENTS(T_BOOLEAN, jboolean, Boolean, bool
2783                                   , HOTSPOT_JNI_RELEASEBOOLEANARRAYELEMENTS_ENTRY(env, array, (uintptr_t *) buf, mode),
2784                                   HOTSPOT_JNI_RELEASEBOOLEANARRAYELEMENTS_RETURN())
2785 DEFINE_RELEASESCALARARRAYELEMENTS(T_BYTE,    jbyte,    Byte,    byte
2786                                   , HOTSPOT_JNI_RELEASEBYTEARRAYELEMENTS_ENTRY(env, array, (char *) buf, mode),
2787                                   HOTSPOT_JNI_RELEASEBYTEARRAYELEMENTS_RETURN())
2788 DEFINE_RELEASESCALARARRAYELEMENTS(T_SHORT,   jshort,   Short,   short
2789                                   ,  HOTSPOT_JNI_RELEASESHORTARRAYELEMENTS_ENTRY(env, array, (uint16_t *) buf, mode),
2790                                   HOTSPOT_JNI_RELEASESHORTARRAYELEMENTS_RETURN())
2791 DEFINE_RELEASESCALARARRAYELEMENTS(T_CHAR,    jchar,    Char,    char
2792                                   ,  HOTSPOT_JNI_RELEASECHARARRAYELEMENTS_ENTRY(env, array, (uint16_t *) buf, mode),
2793                                   HOTSPOT_JNI_RELEASECHARARRAYELEMENTS_RETURN())
2794 DEFINE_RELEASESCALARARRAYELEMENTS(T_INT,     jint,     Int,     int
2795                                   , HOTSPOT_JNI_RELEASEINTARRAYELEMENTS_ENTRY(env, array, (uint32_t *) buf, mode),
2796                                   HOTSPOT_JNI_RELEASEINTARRAYELEMENTS_RETURN())
2797 DEFINE_RELEASESCALARARRAYELEMENTS(T_LONG,    jlong,    Long,    long
2798                                   , HOTSPOT_JNI_RELEASELONGARRAYELEMENTS_ENTRY(env, array, (uintptr_t *) buf, mode),
2799                                   HOTSPOT_JNI_RELEASELONGARRAYELEMENTS_RETURN())
2800 DEFINE_RELEASESCALARARRAYELEMENTS(T_FLOAT,   jfloat,   Float,   float
2801                                   , HOTSPOT_JNI_RELEASEFLOATARRAYELEMENTS_ENTRY(env, array, (float *) buf, mode),
2802                                   HOTSPOT_JNI_RELEASEFLOATARRAYELEMENTS_RETURN())
2803 DEFINE_RELEASESCALARARRAYELEMENTS(T_DOUBLE,  jdouble,  Double,  double
2804                                   , HOTSPOT_JNI_RELEASEDOUBLEARRAYELEMENTS_ENTRY(env, array, (double *) buf, mode),
2805                                   HOTSPOT_JNI_RELEASEDOUBLEARRAYELEMENTS_RETURN())
2806 
2807 
2808 #define DEFINE_GETSCALARARRAYREGION(ElementTag,ElementType,Result, Tag \
2809                                     , EntryProbe, ReturnProbe); \
2810   DT_VOID_RETURN_MARK_DECL(Get##Result##ArrayRegion \
2811                            , ReturnProbe); \
2812 \
2813 JNI_ENTRY(void, \
2814 jni_Get##Result##ArrayRegion(JNIEnv *env, ElementType##Array array, jsize start, \
2815              jsize len, ElementType *buf)) \
2816   JNIWrapper("Get" XSTR(Result) "ArrayRegion"); \
2817   EntryProbe; \
2818   DT_VOID_RETURN_MARK(Get##Result##ArrayRegion); \
2819   typeArrayOop src = typeArrayOop(JNIHandles::resolve_non_null(array)); \
2820   if (start < 0 || len < 0 || (start > src->length() - len)) { \
2821     THROW(vmSymbols::java_lang_ArrayIndexOutOfBoundsException()); \
2822   } else { \
2823     if (len > 0) { \
2824       int sc = TypeArrayKlass::cast(src->klass())->log2_element_size(); \
2825       memcpy((u_char*) buf, \
2826              (u_char*) src->Tag##_at_addr(start), \
2827              len << sc);                          \
2828     } \
2829   } \
2830 JNI_END
2831 
2832 DEFINE_GETSCALARARRAYREGION(T_BOOLEAN, jboolean,Boolean, bool
2833                             , HOTSPOT_JNI_GETBOOLEANARRAYREGION_ENTRY(env, array, start, len, (uintptr_t *) buf),
2834                             HOTSPOT_JNI_GETBOOLEANARRAYREGION_RETURN());
2835 DEFINE_GETSCALARARRAYREGION(T_BYTE,    jbyte,   Byte,    byte
2836                             ,  HOTSPOT_JNI_GETBYTEARRAYREGION_ENTRY(env, array, start, len, (char *) buf),
2837                             HOTSPOT_JNI_GETBYTEARRAYREGION_RETURN());
2838 DEFINE_GETSCALARARRAYREGION(T_SHORT,   jshort,  Short,   short
2839                             , HOTSPOT_JNI_GETSHORTARRAYREGION_ENTRY(env, array, start, len, (uint16_t *) buf),
2840                             HOTSPOT_JNI_GETSHORTARRAYREGION_RETURN());
2841 DEFINE_GETSCALARARRAYREGION(T_CHAR,    jchar,   Char,    char
2842                             ,  HOTSPOT_JNI_GETCHARARRAYREGION_ENTRY(env, array, start, len, (uint16_t*) buf),
2843                             HOTSPOT_JNI_GETCHARARRAYREGION_RETURN());
2844 DEFINE_GETSCALARARRAYREGION(T_INT,     jint,    Int,     int
2845                             , HOTSPOT_JNI_GETINTARRAYREGION_ENTRY(env, array, start, len, (uint32_t*) buf),
2846                             HOTSPOT_JNI_GETINTARRAYREGION_RETURN());
2847 DEFINE_GETSCALARARRAYREGION(T_LONG,    jlong,   Long,    long
2848                             ,  HOTSPOT_JNI_GETLONGARRAYREGION_ENTRY(env, array, start, len, (uintptr_t *) buf),
2849                             HOTSPOT_JNI_GETLONGARRAYREGION_RETURN());
2850 DEFINE_GETSCALARARRAYREGION(T_FLOAT,   jfloat,  Float,   float
2851                             , HOTSPOT_JNI_GETFLOATARRAYREGION_ENTRY(env, array, start, len, (float *) buf),
2852                             HOTSPOT_JNI_GETFLOATARRAYREGION_RETURN());
2853 DEFINE_GETSCALARARRAYREGION(T_DOUBLE,  jdouble, Double,  double
2854                             , HOTSPOT_JNI_GETDOUBLEARRAYREGION_ENTRY(env, array, start, len, (double *) buf),
2855                             HOTSPOT_JNI_GETDOUBLEARRAYREGION_RETURN());
2856 
2857 
2858 #define DEFINE_SETSCALARARRAYREGION(ElementTag,ElementType,Result, Tag \
2859                                     , EntryProbe, ReturnProbe); \
2860   DT_VOID_RETURN_MARK_DECL(Set##Result##ArrayRegion \
2861                            ,ReturnProbe);           \
2862 \
2863 JNI_ENTRY(void, \
2864 jni_Set##Result##ArrayRegion(JNIEnv *env, ElementType##Array array, jsize start, \
2865              jsize len, const ElementType *buf)) \
2866   JNIWrapper("Set" XSTR(Result) "ArrayRegion"); \
2867   EntryProbe; \
2868   DT_VOID_RETURN_MARK(Set##Result##ArrayRegion); \
2869   typeArrayOop dst = typeArrayOop(JNIHandles::resolve_non_null(array)); \
2870   if (start < 0 || len < 0 || (start > dst->length() - len)) { \
2871     THROW(vmSymbols::java_lang_ArrayIndexOutOfBoundsException()); \
2872   } else { \
2873     if (len > 0) { \
2874       int sc = TypeArrayKlass::cast(dst->klass())->log2_element_size(); \
2875       memcpy((u_char*) dst->Tag##_at_addr(start), \
2876              (u_char*) buf, \
2877              len << sc);    \
2878     } \
2879   } \
2880 JNI_END
2881 
2882 DEFINE_SETSCALARARRAYREGION(T_BOOLEAN, jboolean, Boolean, bool
2883                             , HOTSPOT_JNI_SETBOOLEANARRAYREGION_ENTRY(env, array, start, len, (uintptr_t *)buf),
2884                             HOTSPOT_JNI_SETBOOLEANARRAYREGION_RETURN())
2885 DEFINE_SETSCALARARRAYREGION(T_BYTE,    jbyte,    Byte,    byte
2886                             , HOTSPOT_JNI_SETBYTEARRAYREGION_ENTRY(env, array, start, len, (char *) buf),
2887                             HOTSPOT_JNI_SETBYTEARRAYREGION_RETURN())
2888 DEFINE_SETSCALARARRAYREGION(T_SHORT,   jshort,   Short,   short
2889                             , HOTSPOT_JNI_SETSHORTARRAYREGION_ENTRY(env, array, start, len, (uint16_t *) buf),
2890                             HOTSPOT_JNI_SETSHORTARRAYREGION_RETURN())
2891 DEFINE_SETSCALARARRAYREGION(T_CHAR,    jchar,    Char,    char
2892                             , HOTSPOT_JNI_SETCHARARRAYREGION_ENTRY(env, array, start, len, (uint16_t *) buf),
2893                             HOTSPOT_JNI_SETCHARARRAYREGION_RETURN())
2894 DEFINE_SETSCALARARRAYREGION(T_INT,     jint,     Int,     int
2895                             , HOTSPOT_JNI_SETINTARRAYREGION_ENTRY(env, array, start, len, (uint32_t *) buf),
2896                             HOTSPOT_JNI_SETINTARRAYREGION_RETURN())
2897 DEFINE_SETSCALARARRAYREGION(T_LONG,    jlong,    Long,    long
2898                             , HOTSPOT_JNI_SETLONGARRAYREGION_ENTRY(env, array, start, len, (uintptr_t *) buf),
2899                             HOTSPOT_JNI_SETLONGARRAYREGION_RETURN())
2900 DEFINE_SETSCALARARRAYREGION(T_FLOAT,   jfloat,   Float,   float
2901                             , HOTSPOT_JNI_SETFLOATARRAYREGION_ENTRY(env, array, start, len, (float *) buf),
2902                             HOTSPOT_JNI_SETFLOATARRAYREGION_RETURN())
2903 DEFINE_SETSCALARARRAYREGION(T_DOUBLE,  jdouble,  Double,  double
2904                             , HOTSPOT_JNI_SETDOUBLEARRAYREGION_ENTRY(env, array, start, len, (double *) buf),
2905                             HOTSPOT_JNI_SETDOUBLEARRAYREGION_RETURN())
2906 
2907 
2908 //
2909 // Interception of natives
2910 //
2911 
2912 // The RegisterNatives call being attempted tried to register with a method that
2913 // is not native.  Ask JVM TI what prefixes have been specified.  Then check
2914 // to see if the native method is now wrapped with the prefixes.  See the
2915 // SetNativeMethodPrefix(es) functions in the JVM TI Spec for details.
2916 static Method* find_prefixed_native(Klass* k, Symbol* name, Symbol* signature, TRAPS) {
2917 #if INCLUDE_JVMTI
2918   ResourceMark rm(THREAD);
2919   Method* method;
2920   int name_len = name->utf8_length();
2921   char* name_str = name->as_utf8();
2922   int prefix_count;
2923   char** prefixes = JvmtiExport::get_all_native_method_prefixes(&prefix_count);
2924   for (int i = 0; i < prefix_count; i++) {
2925     char* prefix = prefixes[i];
2926     int prefix_len = (int)strlen(prefix);
2927 
2928     // try adding this prefix to the method name and see if it matches another method name
2929     int trial_len = name_len + prefix_len;
2930     char* trial_name_str = NEW_RESOURCE_ARRAY(char, trial_len + 1);
2931     strcpy(trial_name_str, prefix);
2932     strcat(trial_name_str, name_str);
2933     TempNewSymbol trial_name = SymbolTable::probe(trial_name_str, trial_len);
2934     if (trial_name == NULL) {
2935       continue; // no such symbol, so this prefix wasn't used, try the next prefix
2936     }
2937     method = k->lookup_method(trial_name, signature);
2938     if (method == NULL) {
2939       continue; // signature doesn't match, try the next prefix
2940     }
2941     if (method->is_native()) {
2942       method->set_is_prefixed_native();
2943       return method; // wahoo, we found a prefixed version of the method, return it
2944     }
2945     // found as non-native, so prefix is good, add it, probably just need more prefixes
2946     name_len = trial_len;
2947     name_str = trial_name_str;
2948   }
2949 #endif // INCLUDE_JVMTI
2950   return NULL; // not found
2951 }
2952 
2953 static bool register_native(Klass* k, Symbol* name, Symbol* signature, address entry, TRAPS) {
2954   Method* method = k->lookup_method(name, signature);
2955   if (method == NULL) {
2956     ResourceMark rm;
2957     stringStream st;
2958     st.print("Method %s name or signature does not match",
2959              Method::name_and_sig_as_C_string(k, name, signature));
2960     THROW_MSG_(vmSymbols::java_lang_NoSuchMethodError(), st.as_string(), false);
2961   }
2962   if (!method->is_native()) {
2963     // trying to register to a non-native method, see if a JVM TI agent has added prefix(es)
2964     method = find_prefixed_native(k, name, signature, THREAD);
2965     if (method == NULL) {
2966       ResourceMark rm;
2967       stringStream st;
2968       st.print("Method %s is not declared as native",
2969                Method::name_and_sig_as_C_string(k, name, signature));
2970       THROW_MSG_(vmSymbols::java_lang_NoSuchMethodError(), st.as_string(), false);
2971     }
2972   }
2973 
2974   if (entry != NULL) {
2975     method->set_native_function(entry,
2976       Method::native_bind_event_is_interesting);
2977   } else {
2978     method->clear_native_function();
2979   }
2980   if (PrintJNIResolving) {
2981     ResourceMark rm(THREAD);
2982     tty->print_cr("[Registering JNI native method %s.%s]",
2983       method->method_holder()->external_name(),
2984       method->name()->as_C_string());
2985   }
2986   return true;
2987 }
2988 
2989 DT_RETURN_MARK_DECL(RegisterNatives, jint
2990                     , HOTSPOT_JNI_REGISTERNATIVES_RETURN(_ret_ref));
2991 
2992 JNI_ENTRY(jint, jni_RegisterNatives(JNIEnv *env, jclass clazz,
2993                                     const JNINativeMethod *methods,
2994                                     jint nMethods))
2995   JNIWrapper("RegisterNatives");
2996   HOTSPOT_JNI_REGISTERNATIVES_ENTRY(env, clazz, (void *) methods, nMethods);
2997   jint ret = 0;
2998   DT_RETURN_MARK(RegisterNatives, jint, (const jint&)ret);
2999 
3000   Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(clazz));
3001 
3002   for (int index = 0; index < nMethods; index++) {
3003     const char* meth_name = methods[index].name;
3004     const char* meth_sig = methods[index].signature;
3005     int meth_name_len = (int)strlen(meth_name);
3006 
3007     // The class should have been loaded (we have an instance of the class
3008     // passed in) so the method and signature should already be in the symbol
3009     // table.  If they're not there, the method doesn't exist.
3010     TempNewSymbol  name = SymbolTable::probe(meth_name, meth_name_len);
3011     TempNewSymbol  signature = SymbolTable::probe(meth_sig, (int)strlen(meth_sig));
3012 
3013     if (name == NULL || signature == NULL) {
3014       ResourceMark rm;
3015       stringStream st;
3016       st.print("Method %s.%s%s not found", k->external_name(), meth_name, meth_sig);
3017       // Must return negative value on failure
3018       THROW_MSG_(vmSymbols::java_lang_NoSuchMethodError(), st.as_string(), -1);
3019     }
3020 
3021     bool res = register_native(k, name, signature,
3022                                (address) methods[index].fnPtr, THREAD);
3023     if (!res) {
3024       ret = -1;
3025       break;
3026     }
3027   }
3028   return ret;
3029 JNI_END
3030 
3031 
3032 JNI_ENTRY(jint, jni_UnregisterNatives(JNIEnv *env, jclass clazz))
3033   JNIWrapper("UnregisterNatives");
3034  HOTSPOT_JNI_UNREGISTERNATIVES_ENTRY(env, clazz);
3035   Klass* k   = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(clazz));
3036   //%note jni_2
3037   if (k->is_instance_klass()) {
3038     for (int index = 0; index < InstanceKlass::cast(k)->methods()->length(); index++) {
3039       Method* m = InstanceKlass::cast(k)->methods()->at(index);
3040       if (m->is_native()) {
3041         m->clear_native_function();
3042         m->set_signature_handler(NULL);
3043       }
3044     }
3045   }
3046  HOTSPOT_JNI_UNREGISTERNATIVES_RETURN(0);
3047   return 0;
3048 JNI_END
3049 
3050 //
3051 // Monitor functions
3052 //
3053 
3054 DT_RETURN_MARK_DECL(MonitorEnter, jint
3055                     , HOTSPOT_JNI_MONITORENTER_RETURN(_ret_ref));
3056 
3057 JNI_ENTRY(jint, jni_MonitorEnter(JNIEnv *env, jobject jobj))
3058  HOTSPOT_JNI_MONITORENTER_ENTRY(env, jobj);
3059   jint ret = JNI_ERR;
3060   DT_RETURN_MARK(MonitorEnter, jint, (const jint&)ret);
3061 
3062   // If the object is null, we can't do anything with it
3063   if (jobj == NULL) {
3064     THROW_(vmSymbols::java_lang_NullPointerException(), JNI_ERR);
3065   }
3066 
3067   Handle obj(thread, JNIHandles::resolve_non_null(jobj));
3068   ObjectSynchronizer::jni_enter(obj, CHECK_(JNI_ERR));
3069   ret = JNI_OK;
3070   return ret;
3071 JNI_END
3072 
3073 DT_RETURN_MARK_DECL(MonitorExit, jint
3074                     , HOTSPOT_JNI_MONITOREXIT_RETURN(_ret_ref));
3075 
3076 JNI_ENTRY(jint, jni_MonitorExit(JNIEnv *env, jobject jobj))
3077  HOTSPOT_JNI_MONITOREXIT_ENTRY(env, jobj);
3078   jint ret = JNI_ERR;
3079   DT_RETURN_MARK(MonitorExit, jint, (const jint&)ret);
3080 
3081   // Don't do anything with a null object
3082   if (jobj == NULL) {
3083     THROW_(vmSymbols::java_lang_NullPointerException(), JNI_ERR);
3084   }
3085 
3086   Handle obj(THREAD, JNIHandles::resolve_non_null(jobj));
3087   ObjectSynchronizer::jni_exit(obj(), CHECK_(JNI_ERR));
3088 
3089   ret = JNI_OK;
3090   return ret;
3091 JNI_END
3092 
3093 //
3094 // Extensions
3095 //
3096 
3097 DT_VOID_RETURN_MARK_DECL(GetStringRegion
3098                          , HOTSPOT_JNI_GETSTRINGREGION_RETURN());
3099 
3100 JNI_ENTRY(void, jni_GetStringRegion(JNIEnv *env, jstring string, jsize start, jsize len, jchar *buf))
3101   JNIWrapper("GetStringRegion");
3102  HOTSPOT_JNI_GETSTRINGREGION_ENTRY(env, string, start, len, buf);
3103   DT_VOID_RETURN_MARK(GetStringRegion);
3104   oop s = JNIHandles::resolve_non_null(string);
3105   int s_len = java_lang_String::length(s);
3106   if (start < 0 || len < 0 || start > s_len - len) {
3107     THROW(vmSymbols::java_lang_StringIndexOutOfBoundsException());
3108   } else {
3109     if (len > 0) {
3110       typeArrayOop s_value = java_lang_String::value(s);
3111       bool is_latin1 = java_lang_String::is_latin1(s);
3112       if (!is_latin1) {
3113         memcpy(buf, s_value->char_at_addr(start), sizeof(jchar)*len);

3114       } else {
3115         for (int i = 0; i < len; i++) {
3116           buf[i] = ((jchar) s_value->byte_at(i + start)) & 0xff;
3117         }
3118       }
3119     }
3120   }
3121 JNI_END
3122 
3123 DT_VOID_RETURN_MARK_DECL(GetStringUTFRegion
3124                          , HOTSPOT_JNI_GETSTRINGUTFREGION_RETURN());
3125 
3126 JNI_ENTRY(void, jni_GetStringUTFRegion(JNIEnv *env, jstring string, jsize start, jsize len, char *buf))
3127   JNIWrapper("GetStringUTFRegion");
3128  HOTSPOT_JNI_GETSTRINGUTFREGION_ENTRY(env, string, start, len, buf);
3129   DT_VOID_RETURN_MARK(GetStringUTFRegion);
3130   oop s = JNIHandles::resolve_non_null(string);
3131   int s_len = java_lang_String::length(s);
3132   if (start < 0 || len < 0 || start > s_len - len) {
3133     THROW(vmSymbols::java_lang_StringIndexOutOfBoundsException());
3134   } else {
3135     //%note jni_7
3136     if (len > 0) {
3137       // Assume the buffer is large enough as the JNI spec. does not require user error checking
3138       java_lang_String::as_utf8_string(s, start, len, buf, INT_MAX);
3139       // as_utf8_string null-terminates the result string
3140     } else {
3141       // JDK null-terminates the buffer even in len is zero
3142       if (buf != NULL) {
3143         buf[0] = 0;
3144       }
3145     }
3146   }
3147 JNI_END
3148 
3149 static oop lock_gc_or_pin_object(JavaThread* thread, jobject obj) {
3150   if (Universe::heap()->supports_object_pinning()) {
3151     const oop o = JNIHandles::resolve_non_null(obj);
3152     return Universe::heap()->pin_object(thread, o);
3153   } else {
3154     GCLocker::lock_critical(thread);
3155     return JNIHandles::resolve_non_null(obj);
3156   }
3157 }
3158 
3159 static void unlock_gc_or_unpin_object(JavaThread* thread, jobject obj) {
3160   if (Universe::heap()->supports_object_pinning()) {
3161     const oop o = JNIHandles::resolve_non_null(obj);
3162     return Universe::heap()->unpin_object(thread, o);
3163   } else {
3164     GCLocker::unlock_critical(thread);
3165   }
3166 }
3167 
3168 JNI_ENTRY(void*, jni_GetPrimitiveArrayCritical(JNIEnv *env, jarray array, jboolean *isCopy))
3169   JNIWrapper("GetPrimitiveArrayCritical");
3170  HOTSPOT_JNI_GETPRIMITIVEARRAYCRITICAL_ENTRY(env, array, (uintptr_t *) isCopy);
3171   if (isCopy != NULL) {
3172     *isCopy = JNI_FALSE;
3173   }
3174   oop a = lock_gc_or_pin_object(thread, array);
3175   assert(a->is_array(), "just checking");
3176   BasicType type;
3177   if (a->is_objArray()) {
3178     type = T_OBJECT;
3179   } else {
3180     type = TypeArrayKlass::cast(a->klass())->element_type();
3181   }
3182   void* ret = arrayOop(a)->base(type);
3183  HOTSPOT_JNI_GETPRIMITIVEARRAYCRITICAL_RETURN(ret);
3184   return ret;
3185 JNI_END
3186 
3187 
3188 JNI_ENTRY(void, jni_ReleasePrimitiveArrayCritical(JNIEnv *env, jarray array, void *carray, jint mode))
3189   JNIWrapper("ReleasePrimitiveArrayCritical");
3190   HOTSPOT_JNI_RELEASEPRIMITIVEARRAYCRITICAL_ENTRY(env, array, carray, mode);
3191   unlock_gc_or_unpin_object(thread, array);
3192 HOTSPOT_JNI_RELEASEPRIMITIVEARRAYCRITICAL_RETURN();
3193 JNI_END
3194 
3195 
3196 JNI_ENTRY(const jchar*, jni_GetStringCritical(JNIEnv *env, jstring string, jboolean *isCopy))
3197   JNIWrapper("GetStringCritical");
3198   HOTSPOT_JNI_GETSTRINGCRITICAL_ENTRY(env, string, (uintptr_t *) isCopy);
3199   oop s = lock_gc_or_pin_object(thread, string);
3200   typeArrayOop s_value = java_lang_String::value(s);
3201   bool is_latin1 = java_lang_String::is_latin1(s);
3202   if (isCopy != NULL) {
3203     *isCopy = is_latin1 ? JNI_TRUE : JNI_FALSE;
3204   }
3205   jchar* ret;
3206   if (!is_latin1) {
3207     ret = (jchar*) s_value->base(T_CHAR);
3208   } else {
3209     // Inflate latin1 encoded string to UTF16
3210     int s_len = java_lang_String::length(s);
3211     ret = NEW_C_HEAP_ARRAY_RETURN_NULL(jchar, s_len + 1, mtInternal);  // add one for zero termination
3212     /* JNI Specification states return NULL on OOM */
3213     if (ret != NULL) {
3214       for (int i = 0; i < s_len; i++) {
3215         ret[i] = ((jchar) s_value->byte_at(i)) & 0xff;
3216       }
3217       ret[s_len] = 0;
3218     }
3219   }
3220  HOTSPOT_JNI_GETSTRINGCRITICAL_RETURN((uint16_t *) ret);
3221   return ret;
3222 JNI_END
3223 
3224 
3225 JNI_ENTRY(void, jni_ReleaseStringCritical(JNIEnv *env, jstring str, const jchar *chars))
3226   JNIWrapper("ReleaseStringCritical");
3227   HOTSPOT_JNI_RELEASESTRINGCRITICAL_ENTRY(env, str, (uint16_t *) chars);
3228   // The str and chars arguments are ignored for UTF16 strings
3229   oop s = JNIHandles::resolve_non_null(str);
3230   bool is_latin1 = java_lang_String::is_latin1(s);
3231   if (is_latin1) {
3232     // For latin1 string, free jchar array allocated by earlier call to GetStringCritical.
3233     // This assumes that ReleaseStringCritical bookends GetStringCritical.
3234     FREE_C_HEAP_ARRAY(jchar, chars);
3235   }
3236   unlock_gc_or_unpin_object(thread, str);
3237 HOTSPOT_JNI_RELEASESTRINGCRITICAL_RETURN();
3238 JNI_END
3239 
3240 
3241 JNI_ENTRY(jweak, jni_NewWeakGlobalRef(JNIEnv *env, jobject ref))
3242   JNIWrapper("jni_NewWeakGlobalRef");
3243  HOTSPOT_JNI_NEWWEAKGLOBALREF_ENTRY(env, ref);
3244   Handle ref_handle(thread, JNIHandles::resolve(ref));
3245   jweak ret = JNIHandles::make_weak_global(ref_handle);
3246  HOTSPOT_JNI_NEWWEAKGLOBALREF_RETURN(ret);
3247   return ret;
3248 JNI_END
3249 
3250 // Must be JNI_ENTRY (with HandleMark)
3251 JNI_ENTRY(void, jni_DeleteWeakGlobalRef(JNIEnv *env, jweak ref))
3252   JNIWrapper("jni_DeleteWeakGlobalRef");
3253   HOTSPOT_JNI_DELETEWEAKGLOBALREF_ENTRY(env, ref);
3254   JNIHandles::destroy_weak_global(ref);
3255   HOTSPOT_JNI_DELETEWEAKGLOBALREF_RETURN();
3256 JNI_END
3257 
3258 
3259 JNI_QUICK_ENTRY(jboolean, jni_ExceptionCheck(JNIEnv *env))
3260   JNIWrapper("jni_ExceptionCheck");
3261  HOTSPOT_JNI_EXCEPTIONCHECK_ENTRY(env);
3262   jni_check_async_exceptions(thread);
3263   jboolean ret = (thread->has_pending_exception()) ? JNI_TRUE : JNI_FALSE;
3264  HOTSPOT_JNI_EXCEPTIONCHECK_RETURN(ret);
3265   return ret;
3266 JNI_END
3267 
3268 
3269 // Initialization state for three routines below relating to
3270 // java.nio.DirectBuffers
3271 static          int directBufferSupportInitializeStarted = 0;
3272 static volatile int directBufferSupportInitializeEnded   = 0;
3273 static volatile int directBufferSupportInitializeFailed  = 0;
3274 static jclass    bufferClass                 = NULL;
3275 static jclass    directBufferClass           = NULL;
3276 static jclass    directByteBufferClass       = NULL;
3277 static jmethodID directByteBufferConstructor = NULL;
3278 static jfieldID  directBufferAddressField    = NULL;
3279 static jfieldID  bufferCapacityField         = NULL;
3280 
3281 static jclass lookupOne(JNIEnv* env, const char* name, TRAPS) {
3282   Handle loader;            // null (bootstrap) loader
3283   Handle protection_domain; // null protection domain
3284 
3285   TempNewSymbol sym = SymbolTable::new_symbol(name, CHECK_NULL);
3286   jclass result =  find_class_from_class_loader(env, sym, true, loader, protection_domain, true, CHECK_NULL);
3287 
3288   if (log_is_enabled(Debug, class, resolve) && result != NULL) {
3289     trace_class_resolution(java_lang_Class::as_Klass(JNIHandles::resolve_non_null(result)));
3290   }
3291   return result;
3292 }
3293 
3294 // These lookups are done with the NULL (bootstrap) ClassLoader to
3295 // circumvent any security checks that would be done by jni_FindClass.
3296 JNI_ENTRY(bool, lookupDirectBufferClasses(JNIEnv* env))
3297 {
3298   if ((bufferClass           = lookupOne(env, "java/nio/Buffer", thread))           == NULL) { return false; }
3299   if ((directBufferClass     = lookupOne(env, "sun/nio/ch/DirectBuffer", thread))   == NULL) { return false; }
3300   if ((directByteBufferClass = lookupOne(env, "java/nio/DirectByteBuffer", thread)) == NULL) { return false; }
3301   return true;
3302 }
3303 JNI_END
3304 
3305 
3306 static bool initializeDirectBufferSupport(JNIEnv* env, JavaThread* thread) {
3307   if (directBufferSupportInitializeFailed) {
3308     return false;
3309   }
3310 
3311   if (Atomic::cmpxchg(1, &directBufferSupportInitializeStarted, 0) == 0) {
3312     if (!lookupDirectBufferClasses(env)) {
3313       directBufferSupportInitializeFailed = 1;
3314       return false;
3315     }
3316 
3317     // Make global references for these
3318     bufferClass           = (jclass) env->NewGlobalRef(bufferClass);
3319     directBufferClass     = (jclass) env->NewGlobalRef(directBufferClass);
3320     directByteBufferClass = (jclass) env->NewGlobalRef(directByteBufferClass);
3321 
3322     // Get needed field and method IDs
3323     directByteBufferConstructor = env->GetMethodID(directByteBufferClass, "<init>", "(JI)V");
3324     if (env->ExceptionCheck()) {
3325       env->ExceptionClear();
3326       directBufferSupportInitializeFailed = 1;
3327       return false;
3328     }
3329     directBufferAddressField    = env->GetFieldID(bufferClass, "address", "J");
3330     if (env->ExceptionCheck()) {
3331       env->ExceptionClear();
3332       directBufferSupportInitializeFailed = 1;
3333       return false;
3334     }
3335     bufferCapacityField         = env->GetFieldID(bufferClass, "capacity", "I");
3336     if (env->ExceptionCheck()) {
3337       env->ExceptionClear();
3338       directBufferSupportInitializeFailed = 1;
3339       return false;
3340     }
3341 
3342     if ((directByteBufferConstructor == NULL) ||
3343         (directBufferAddressField    == NULL) ||
3344         (bufferCapacityField         == NULL)) {
3345       directBufferSupportInitializeFailed = 1;
3346       return false;
3347     }
3348 
3349     directBufferSupportInitializeEnded = 1;
3350   } else {
3351     while (!directBufferSupportInitializeEnded && !directBufferSupportInitializeFailed) {
3352       os::naked_yield();
3353     }
3354   }
3355 
3356   return !directBufferSupportInitializeFailed;
3357 }
3358 
3359 extern "C" jobject JNICALL jni_NewDirectByteBuffer(JNIEnv *env, void* address, jlong capacity)
3360 {
3361   // thread_from_jni_environment() will block if VM is gone.
3362   JavaThread* thread = JavaThread::thread_from_jni_environment(env);
3363 
3364   JNIWrapper("jni_NewDirectByteBuffer");
3365  HOTSPOT_JNI_NEWDIRECTBYTEBUFFER_ENTRY(env, address, capacity);
3366 
3367   if (!directBufferSupportInitializeEnded) {
3368     if (!initializeDirectBufferSupport(env, thread)) {
3369       HOTSPOT_JNI_NEWDIRECTBYTEBUFFER_RETURN(NULL);
3370       return NULL;
3371     }
3372   }
3373 
3374   // Being paranoid about accidental sign extension on address
3375   jlong addr = (jlong) ((uintptr_t) address);
3376   // NOTE that package-private DirectByteBuffer constructor currently
3377   // takes int capacity
3378   jint  cap  = (jint)  capacity;
3379   jobject ret = env->NewObject(directByteBufferClass, directByteBufferConstructor, addr, cap);
3380   HOTSPOT_JNI_NEWDIRECTBYTEBUFFER_RETURN(ret);
3381   return ret;
3382 }
3383 
3384 DT_RETURN_MARK_DECL(GetDirectBufferAddress, void*
3385                     , HOTSPOT_JNI_GETDIRECTBUFFERADDRESS_RETURN((void*) _ret_ref));
3386 
3387 extern "C" void* JNICALL jni_GetDirectBufferAddress(JNIEnv *env, jobject buf)
3388 {
3389   // thread_from_jni_environment() will block if VM is gone.
3390   JavaThread* thread = JavaThread::thread_from_jni_environment(env);
3391 
3392   JNIWrapper("jni_GetDirectBufferAddress");
3393   HOTSPOT_JNI_GETDIRECTBUFFERADDRESS_ENTRY(env, buf);
3394   void* ret = NULL;
3395   DT_RETURN_MARK(GetDirectBufferAddress, void*, (const void*&)ret);
3396 
3397   if (!directBufferSupportInitializeEnded) {
3398     if (!initializeDirectBufferSupport(env, thread)) {
3399       return 0;
3400     }
3401   }
3402 
3403   if ((buf != NULL) && (!env->IsInstanceOf(buf, directBufferClass))) {
3404     return 0;
3405   }
3406 
3407   ret = (void*)(intptr_t)env->GetLongField(buf, directBufferAddressField);
3408   return ret;
3409 }
3410 
3411 DT_RETURN_MARK_DECL(GetDirectBufferCapacity, jlong
3412                     , HOTSPOT_JNI_GETDIRECTBUFFERCAPACITY_RETURN(_ret_ref));
3413 
3414 extern "C" jlong JNICALL jni_GetDirectBufferCapacity(JNIEnv *env, jobject buf)
3415 {
3416   // thread_from_jni_environment() will block if VM is gone.
3417   JavaThread* thread = JavaThread::thread_from_jni_environment(env);
3418 
3419   JNIWrapper("jni_GetDirectBufferCapacity");
3420   HOTSPOT_JNI_GETDIRECTBUFFERCAPACITY_ENTRY(env, buf);
3421   jlong ret = -1;
3422   DT_RETURN_MARK(GetDirectBufferCapacity, jlong, (const jlong&)ret);
3423 
3424   if (!directBufferSupportInitializeEnded) {
3425     if (!initializeDirectBufferSupport(env, thread)) {
3426       ret = 0;
3427       return ret;
3428     }
3429   }
3430 
3431   if (buf == NULL) {
3432     return -1;
3433   }
3434 
3435   if (!env->IsInstanceOf(buf, directBufferClass)) {
3436     return -1;
3437   }
3438 
3439   // NOTE that capacity is currently an int in the implementation
3440   ret = env->GetIntField(buf, bufferCapacityField);
3441   return ret;
3442 }
3443 
3444 
3445 JNI_LEAF(jint, jni_GetVersion(JNIEnv *env))
3446   JNIWrapper("GetVersion");
3447   HOTSPOT_JNI_GETVERSION_ENTRY(env);
3448   HOTSPOT_JNI_GETVERSION_RETURN(CurrentVersion);
3449   return CurrentVersion;
3450 JNI_END
3451 
3452 extern struct JavaVM_ main_vm;
3453 
3454 JNI_LEAF(jint, jni_GetJavaVM(JNIEnv *env, JavaVM **vm))
3455   JNIWrapper("jni_GetJavaVM");
3456   HOTSPOT_JNI_GETJAVAVM_ENTRY(env, (void **) vm);
3457   *vm  = (JavaVM *)(&main_vm);
3458   HOTSPOT_JNI_GETJAVAVM_RETURN(JNI_OK);
3459   return JNI_OK;
3460 JNI_END
3461 
3462 
3463 JNI_ENTRY(jobject, jni_GetModule(JNIEnv* env, jclass clazz))
3464   JNIWrapper("GetModule");
3465   return Modules::get_module(clazz, THREAD);
3466 JNI_END
3467 
3468 
3469 // Structure containing all jni functions
3470 struct JNINativeInterface_ jni_NativeInterface = {
3471     NULL,
3472     NULL,
3473     NULL,
3474 
3475     NULL,
3476 
3477     jni_GetVersion,
3478 
3479     jni_DefineClass,
3480     jni_FindClass,
3481 
3482     jni_FromReflectedMethod,
3483     jni_FromReflectedField,
3484 
3485     jni_ToReflectedMethod,
3486 
3487     jni_GetSuperclass,
3488     jni_IsAssignableFrom,
3489 
3490     jni_ToReflectedField,
3491 
3492     jni_Throw,
3493     jni_ThrowNew,
3494     jni_ExceptionOccurred,
3495     jni_ExceptionDescribe,
3496     jni_ExceptionClear,
3497     jni_FatalError,
3498 
3499     jni_PushLocalFrame,
3500     jni_PopLocalFrame,
3501 
3502     jni_NewGlobalRef,
3503     jni_DeleteGlobalRef,
3504     jni_DeleteLocalRef,
3505     jni_IsSameObject,
3506 
3507     jni_NewLocalRef,
3508     jni_EnsureLocalCapacity,
3509 
3510     jni_AllocObject,
3511     jni_NewObject,
3512     jni_NewObjectV,
3513     jni_NewObjectA,
3514 
3515     jni_GetObjectClass,
3516     jni_IsInstanceOf,
3517 
3518     jni_GetMethodID,
3519 
3520     jni_CallObjectMethod,
3521     jni_CallObjectMethodV,
3522     jni_CallObjectMethodA,
3523     jni_CallBooleanMethod,
3524     jni_CallBooleanMethodV,
3525     jni_CallBooleanMethodA,
3526     jni_CallByteMethod,
3527     jni_CallByteMethodV,
3528     jni_CallByteMethodA,
3529     jni_CallCharMethod,
3530     jni_CallCharMethodV,
3531     jni_CallCharMethodA,
3532     jni_CallShortMethod,
3533     jni_CallShortMethodV,
3534     jni_CallShortMethodA,
3535     jni_CallIntMethod,
3536     jni_CallIntMethodV,
3537     jni_CallIntMethodA,
3538     jni_CallLongMethod,
3539     jni_CallLongMethodV,
3540     jni_CallLongMethodA,
3541     jni_CallFloatMethod,
3542     jni_CallFloatMethodV,
3543     jni_CallFloatMethodA,
3544     jni_CallDoubleMethod,
3545     jni_CallDoubleMethodV,
3546     jni_CallDoubleMethodA,
3547     jni_CallVoidMethod,
3548     jni_CallVoidMethodV,
3549     jni_CallVoidMethodA,
3550 
3551     jni_CallNonvirtualObjectMethod,
3552     jni_CallNonvirtualObjectMethodV,
3553     jni_CallNonvirtualObjectMethodA,
3554     jni_CallNonvirtualBooleanMethod,
3555     jni_CallNonvirtualBooleanMethodV,
3556     jni_CallNonvirtualBooleanMethodA,
3557     jni_CallNonvirtualByteMethod,
3558     jni_CallNonvirtualByteMethodV,
3559     jni_CallNonvirtualByteMethodA,
3560     jni_CallNonvirtualCharMethod,
3561     jni_CallNonvirtualCharMethodV,
3562     jni_CallNonvirtualCharMethodA,
3563     jni_CallNonvirtualShortMethod,
3564     jni_CallNonvirtualShortMethodV,
3565     jni_CallNonvirtualShortMethodA,
3566     jni_CallNonvirtualIntMethod,
3567     jni_CallNonvirtualIntMethodV,
3568     jni_CallNonvirtualIntMethodA,
3569     jni_CallNonvirtualLongMethod,
3570     jni_CallNonvirtualLongMethodV,
3571     jni_CallNonvirtualLongMethodA,
3572     jni_CallNonvirtualFloatMethod,
3573     jni_CallNonvirtualFloatMethodV,
3574     jni_CallNonvirtualFloatMethodA,
3575     jni_CallNonvirtualDoubleMethod,
3576     jni_CallNonvirtualDoubleMethodV,
3577     jni_CallNonvirtualDoubleMethodA,
3578     jni_CallNonvirtualVoidMethod,
3579     jni_CallNonvirtualVoidMethodV,
3580     jni_CallNonvirtualVoidMethodA,
3581 
3582     jni_GetFieldID,
3583 
3584     jni_GetObjectField,
3585     jni_GetBooleanField,
3586     jni_GetByteField,
3587     jni_GetCharField,
3588     jni_GetShortField,
3589     jni_GetIntField,
3590     jni_GetLongField,
3591     jni_GetFloatField,
3592     jni_GetDoubleField,
3593 
3594     jni_SetObjectField,
3595     jni_SetBooleanField,
3596     jni_SetByteField,
3597     jni_SetCharField,
3598     jni_SetShortField,
3599     jni_SetIntField,
3600     jni_SetLongField,
3601     jni_SetFloatField,
3602     jni_SetDoubleField,
3603 
3604     jni_GetStaticMethodID,
3605 
3606     jni_CallStaticObjectMethod,
3607     jni_CallStaticObjectMethodV,
3608     jni_CallStaticObjectMethodA,
3609     jni_CallStaticBooleanMethod,
3610     jni_CallStaticBooleanMethodV,
3611     jni_CallStaticBooleanMethodA,
3612     jni_CallStaticByteMethod,
3613     jni_CallStaticByteMethodV,
3614     jni_CallStaticByteMethodA,
3615     jni_CallStaticCharMethod,
3616     jni_CallStaticCharMethodV,
3617     jni_CallStaticCharMethodA,
3618     jni_CallStaticShortMethod,
3619     jni_CallStaticShortMethodV,
3620     jni_CallStaticShortMethodA,
3621     jni_CallStaticIntMethod,
3622     jni_CallStaticIntMethodV,
3623     jni_CallStaticIntMethodA,
3624     jni_CallStaticLongMethod,
3625     jni_CallStaticLongMethodV,
3626     jni_CallStaticLongMethodA,
3627     jni_CallStaticFloatMethod,
3628     jni_CallStaticFloatMethodV,
3629     jni_CallStaticFloatMethodA,
3630     jni_CallStaticDoubleMethod,
3631     jni_CallStaticDoubleMethodV,
3632     jni_CallStaticDoubleMethodA,
3633     jni_CallStaticVoidMethod,
3634     jni_CallStaticVoidMethodV,
3635     jni_CallStaticVoidMethodA,
3636 
3637     jni_GetStaticFieldID,
3638 
3639     jni_GetStaticObjectField,
3640     jni_GetStaticBooleanField,
3641     jni_GetStaticByteField,
3642     jni_GetStaticCharField,
3643     jni_GetStaticShortField,
3644     jni_GetStaticIntField,
3645     jni_GetStaticLongField,
3646     jni_GetStaticFloatField,
3647     jni_GetStaticDoubleField,
3648 
3649     jni_SetStaticObjectField,
3650     jni_SetStaticBooleanField,
3651     jni_SetStaticByteField,
3652     jni_SetStaticCharField,
3653     jni_SetStaticShortField,
3654     jni_SetStaticIntField,
3655     jni_SetStaticLongField,
3656     jni_SetStaticFloatField,
3657     jni_SetStaticDoubleField,
3658 
3659     jni_NewString,
3660     jni_GetStringLength,
3661     jni_GetStringChars,
3662     jni_ReleaseStringChars,
3663 
3664     jni_NewStringUTF,
3665     jni_GetStringUTFLength,
3666     jni_GetStringUTFChars,
3667     jni_ReleaseStringUTFChars,
3668 
3669     jni_GetArrayLength,
3670 
3671     jni_NewObjectArray,
3672     jni_GetObjectArrayElement,
3673     jni_SetObjectArrayElement,
3674 
3675     jni_NewBooleanArray,
3676     jni_NewByteArray,
3677     jni_NewCharArray,
3678     jni_NewShortArray,
3679     jni_NewIntArray,
3680     jni_NewLongArray,
3681     jni_NewFloatArray,
3682     jni_NewDoubleArray,
3683 
3684     jni_GetBooleanArrayElements,
3685     jni_GetByteArrayElements,
3686     jni_GetCharArrayElements,
3687     jni_GetShortArrayElements,
3688     jni_GetIntArrayElements,
3689     jni_GetLongArrayElements,
3690     jni_GetFloatArrayElements,
3691     jni_GetDoubleArrayElements,
3692 
3693     jni_ReleaseBooleanArrayElements,
3694     jni_ReleaseByteArrayElements,
3695     jni_ReleaseCharArrayElements,
3696     jni_ReleaseShortArrayElements,
3697     jni_ReleaseIntArrayElements,
3698     jni_ReleaseLongArrayElements,
3699     jni_ReleaseFloatArrayElements,
3700     jni_ReleaseDoubleArrayElements,
3701 
3702     jni_GetBooleanArrayRegion,
3703     jni_GetByteArrayRegion,
3704     jni_GetCharArrayRegion,
3705     jni_GetShortArrayRegion,
3706     jni_GetIntArrayRegion,
3707     jni_GetLongArrayRegion,
3708     jni_GetFloatArrayRegion,
3709     jni_GetDoubleArrayRegion,
3710 
3711     jni_SetBooleanArrayRegion,
3712     jni_SetByteArrayRegion,
3713     jni_SetCharArrayRegion,
3714     jni_SetShortArrayRegion,
3715     jni_SetIntArrayRegion,
3716     jni_SetLongArrayRegion,
3717     jni_SetFloatArrayRegion,
3718     jni_SetDoubleArrayRegion,
3719 
3720     jni_RegisterNatives,
3721     jni_UnregisterNatives,
3722 
3723     jni_MonitorEnter,
3724     jni_MonitorExit,
3725 
3726     jni_GetJavaVM,
3727 
3728     jni_GetStringRegion,
3729     jni_GetStringUTFRegion,
3730 
3731     jni_GetPrimitiveArrayCritical,
3732     jni_ReleasePrimitiveArrayCritical,
3733 
3734     jni_GetStringCritical,
3735     jni_ReleaseStringCritical,
3736 
3737     jni_NewWeakGlobalRef,
3738     jni_DeleteWeakGlobalRef,
3739 
3740     jni_ExceptionCheck,
3741 
3742     jni_NewDirectByteBuffer,
3743     jni_GetDirectBufferAddress,
3744     jni_GetDirectBufferCapacity,
3745 
3746     // New 1_6 features
3747 
3748     jni_GetObjectRefType,
3749 
3750     // Module features
3751 
3752     jni_GetModule
3753 };
3754 
3755 
3756 // For jvmti use to modify jni function table.
3757 // Java threads in native contiues to run until it is transitioned
3758 // to VM at safepoint. Before the transition or before it is blocked
3759 // for safepoint it may access jni function table. VM could crash if
3760 // any java thread access the jni function table in the middle of memcpy.
3761 // To avoid this each function pointers are copied automically.
3762 void copy_jni_function_table(const struct JNINativeInterface_ *new_jni_NativeInterface) {
3763   assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint");
3764   intptr_t *a = (intptr_t *) jni_functions();
3765   intptr_t *b = (intptr_t *) new_jni_NativeInterface;
3766   for (uint i=0; i <  sizeof(struct JNINativeInterface_)/sizeof(void *); i++) {
3767     Atomic::store(*b++, a++);
3768   }
3769 }
3770 
3771 void quicken_jni_functions() {
3772   // Replace Get<Primitive>Field with fast versions
3773   if (UseFastJNIAccessors && !JvmtiExport::can_post_field_access()
3774       && !VerifyJNIFields && !CountJNICalls && !CheckJNICalls
3775 #if defined(_WINDOWS) && defined(IA32) && defined(COMPILER2)
3776       // windows x86 currently needs SEH wrapper and the gain of the fast
3777       // versions currently isn't certain for server vm on uniprocessor.
3778       && os::is_MP()
3779 #endif
3780   ) {
3781     address func;
3782     func = JNI_FastGetField::generate_fast_get_boolean_field();
3783     if (func != (address)-1) {
3784       jni_NativeInterface.GetBooleanField = (GetBooleanField_t)func;
3785     }
3786     func = JNI_FastGetField::generate_fast_get_byte_field();
3787     if (func != (address)-1) {
3788       jni_NativeInterface.GetByteField = (GetByteField_t)func;
3789     }
3790     func = JNI_FastGetField::generate_fast_get_char_field();
3791     if (func != (address)-1) {
3792       jni_NativeInterface.GetCharField = (GetCharField_t)func;
3793     }
3794     func = JNI_FastGetField::generate_fast_get_short_field();
3795     if (func != (address)-1) {
3796       jni_NativeInterface.GetShortField = (GetShortField_t)func;
3797     }
3798     func = JNI_FastGetField::generate_fast_get_int_field();
3799     if (func != (address)-1) {
3800       jni_NativeInterface.GetIntField = (GetIntField_t)func;
3801     }
3802     func = JNI_FastGetField::generate_fast_get_long_field();
3803     if (func != (address)-1) {
3804       jni_NativeInterface.GetLongField = (GetLongField_t)func;
3805     }
3806     func = JNI_FastGetField::generate_fast_get_float_field();
3807     if (func != (address)-1) {
3808       jni_NativeInterface.GetFloatField = (GetFloatField_t)func;
3809     }
3810     func = JNI_FastGetField::generate_fast_get_double_field();
3811     if (func != (address)-1) {
3812       jni_NativeInterface.GetDoubleField = (GetDoubleField_t)func;
3813     }
3814   }
3815 }
3816 
3817 // Returns the function structure
3818 struct JNINativeInterface_* jni_functions() {
3819 #if INCLUDE_JNI_CHECK
3820   if (CheckJNICalls) return jni_functions_check();
3821 #endif // INCLUDE_JNI_CHECK
3822   return &jni_NativeInterface;
3823 }
3824 
3825 // Returns the function structure
3826 struct JNINativeInterface_* jni_functions_nocheck() {
3827   return &jni_NativeInterface;
3828 }
3829 
3830 
3831 // Invocation API
3832 
3833 
3834 // Forward declaration
3835 extern const struct JNIInvokeInterface_ jni_InvokeInterface;
3836 
3837 // Global invocation API vars
3838 volatile int vm_created = 0;
3839 // Indicate whether it is safe to recreate VM
3840 volatile int safe_to_recreate_vm = 1;
3841 struct JavaVM_ main_vm = {&jni_InvokeInterface};
3842 
3843 
3844 #define JAVASTACKSIZE (400 * 1024)    /* Default size of a thread java stack */
3845 enum { VERIFY_NONE, VERIFY_REMOTE, VERIFY_ALL };
3846 
3847 DT_RETURN_MARK_DECL(GetDefaultJavaVMInitArgs, jint
3848                     , HOTSPOT_JNI_GETDEFAULTJAVAVMINITARGS_RETURN(_ret_ref));
3849 
3850 _JNI_IMPORT_OR_EXPORT_ jint JNICALL JNI_GetDefaultJavaVMInitArgs(void *args_) {
3851   HOTSPOT_JNI_GETDEFAULTJAVAVMINITARGS_ENTRY(args_);
3852   JDK1_1InitArgs *args = (JDK1_1InitArgs *)args_;
3853   jint ret = JNI_ERR;
3854   DT_RETURN_MARK(GetDefaultJavaVMInitArgs, jint, (const jint&)ret);
3855 
3856   if (Threads::is_supported_jni_version(args->version)) {
3857     ret = JNI_OK;
3858   }
3859   // 1.1 style no longer supported in hotspot.
3860   // According the JNI spec, we should update args->version on return.
3861   // We also use the structure to communicate with launcher about default
3862   // stack size.
3863   if (args->version == JNI_VERSION_1_1) {
3864     args->version = JNI_VERSION_1_2;
3865     // javaStackSize is int in arguments structure
3866     assert(jlong(ThreadStackSize) * K < INT_MAX, "integer overflow");
3867     args->javaStackSize = (jint)(ThreadStackSize * K);
3868   }
3869   return ret;
3870 }
3871 
3872 DT_RETURN_MARK_DECL(CreateJavaVM, jint
3873                     , HOTSPOT_JNI_CREATEJAVAVM_RETURN(_ret_ref));
3874 
3875 static jint JNI_CreateJavaVM_inner(JavaVM **vm, void **penv, void *args) {
3876   HOTSPOT_JNI_CREATEJAVAVM_ENTRY((void **) vm, penv, args);
3877 
3878   jint result = JNI_ERR;
3879   DT_RETURN_MARK(CreateJavaVM, jint, (const jint&)result);
3880 
3881   // We're about to use Atomic::xchg for synchronization.  Some Zero
3882   // platforms use the GCC builtin __sync_lock_test_and_set for this,
3883   // but __sync_lock_test_and_set is not guaranteed to do what we want
3884   // on all architectures.  So we check it works before relying on it.
3885 #if defined(ZERO) && defined(ASSERT)
3886   {
3887     jint a = 0xcafebabe;
3888     jint b = Atomic::xchg((jint) 0xdeadbeef, &a);
3889     void *c = &a;
3890     void *d = Atomic::xchg(&b, &c);
3891     assert(a == (jint) 0xdeadbeef && b == (jint) 0xcafebabe, "Atomic::xchg() works");
3892     assert(c == &b && d == &a, "Atomic::xchg() works");
3893   }
3894 #endif // ZERO && ASSERT
3895 
3896   // At the moment it's only possible to have one Java VM,
3897   // since some of the runtime state is in global variables.
3898 
3899   // We cannot use our mutex locks here, since they only work on
3900   // Threads. We do an atomic compare and exchange to ensure only
3901   // one thread can call this method at a time
3902 
3903   // We use Atomic::xchg rather than Atomic::add/dec since on some platforms
3904   // the add/dec implementations are dependent on whether we are running
3905   // on a multiprocessor, and at this stage of initialization the os::is_MP
3906   // function used to determine this will always return false. Atomic::xchg
3907   // does not have this problem.
3908   if (Atomic::xchg(1, &vm_created) == 1) {
3909     return JNI_EEXIST;   // already created, or create attempt in progress
3910   }
3911   if (Atomic::xchg(0, &safe_to_recreate_vm) == 0) {
3912     return JNI_ERR;  // someone tried and failed and retry not allowed.
3913   }
3914 
3915   assert(vm_created == 1, "vm_created is true during the creation");
3916 
3917   /**
3918    * Certain errors during initialization are recoverable and do not
3919    * prevent this method from being called again at a later time
3920    * (perhaps with different arguments).  However, at a certain
3921    * point during initialization if an error occurs we cannot allow
3922    * this function to be called again (or it will crash).  In those
3923    * situations, the 'canTryAgain' flag is set to false, which atomically
3924    * sets safe_to_recreate_vm to 1, such that any new call to
3925    * JNI_CreateJavaVM will immediately fail using the above logic.
3926    */
3927   bool can_try_again = true;
3928 
3929   result = Threads::create_vm((JavaVMInitArgs*) args, &can_try_again);
3930   if (result == JNI_OK) {
3931     JavaThread *thread = JavaThread::current();
3932     assert(!thread->has_pending_exception(), "should have returned not OK");
3933     /* thread is thread_in_vm here */
3934     *vm = (JavaVM *)(&main_vm);
3935     *(JNIEnv**)penv = thread->jni_environment();
3936 
3937 #if INCLUDE_JVMCI
3938     if (EnableJVMCI) {
3939       if (UseJVMCICompiler) {
3940         // JVMCI is initialized on a CompilerThread
3941         if (BootstrapJVMCI) {
3942           JavaThread* THREAD = thread;
3943           JVMCICompiler* compiler = JVMCICompiler::instance(true, CATCH);
3944           compiler->bootstrap(THREAD);
3945           if (HAS_PENDING_EXCEPTION) {
3946             HandleMark hm;
3947             vm_exit_during_initialization(Handle(THREAD, PENDING_EXCEPTION));
3948           }
3949         }
3950       }
3951     }
3952 #endif
3953 
3954     // Tracks the time application was running before GC
3955     RuntimeService::record_application_start();
3956 
3957     // Notify JVMTI
3958     if (JvmtiExport::should_post_thread_life()) {
3959        JvmtiExport::post_thread_start(thread);
3960     }
3961 
3962     EventThreadStart event;
3963     if (event.should_commit()) {
3964       event.set_thread(THREAD_TRACE_ID(thread));
3965       event.commit();
3966     }
3967 
3968 #ifndef PRODUCT
3969     // Check if we should compile all classes on bootclasspath
3970     if (CompileTheWorld) ClassLoader::compile_the_world();
3971     if (ReplayCompiles) ciReplay::replay(thread);
3972 
3973     // Some platforms (like Win*) need a wrapper around these test
3974     // functions in order to properly handle error conditions.
3975     VMError::test_error_handler();
3976     if (ExecuteInternalVMTests) {
3977       InternalVMTests::run();
3978     }
3979 #endif
3980 
3981     // Since this is not a JVM_ENTRY we have to set the thread state manually before leaving.
3982     ThreadStateTransition::transition_and_fence(thread, _thread_in_vm, _thread_in_native);
3983   } else {
3984     // If create_vm exits because of a pending exception, exit with that
3985     // exception.  In the future when we figure out how to reclaim memory,
3986     // we may be able to exit with JNI_ERR and allow the calling application
3987     // to continue.
3988     if (Universe::is_fully_initialized()) {
3989       // otherwise no pending exception possible - VM will already have aborted
3990       JavaThread* THREAD = JavaThread::current();
3991       if (HAS_PENDING_EXCEPTION) {
3992         HandleMark hm;
3993         vm_exit_during_initialization(Handle(THREAD, PENDING_EXCEPTION));
3994       }
3995     }
3996 
3997     if (can_try_again) {
3998       // reset safe_to_recreate_vm to 1 so that retrial would be possible
3999       safe_to_recreate_vm = 1;
4000     }
4001 
4002     // Creation failed. We must reset vm_created
4003     *vm = 0;
4004     *(JNIEnv**)penv = 0;
4005     // reset vm_created last to avoid race condition. Use OrderAccess to
4006     // control both compiler and architectural-based reordering.
4007     OrderAccess::release_store(&vm_created, 0);
4008   }
4009 
4010   // Flush stdout and stderr before exit.
4011   fflush(stdout);
4012   fflush(stderr);
4013 
4014   return result;
4015 
4016 }
4017 
4018 _JNI_IMPORT_OR_EXPORT_ jint JNICALL JNI_CreateJavaVM(JavaVM **vm, void **penv, void *args) {
4019   jint result = JNI_ERR;
4020   // On Windows, let CreateJavaVM run with SEH protection
4021 #ifdef _WIN32
4022   __try {
4023 #endif
4024     result = JNI_CreateJavaVM_inner(vm, penv, args);
4025 #ifdef _WIN32
4026   } __except(topLevelExceptionFilter((_EXCEPTION_POINTERS*)_exception_info())) {
4027     // Nothing to do.
4028   }
4029 #endif
4030   return result;
4031 }
4032 
4033 _JNI_IMPORT_OR_EXPORT_ jint JNICALL JNI_GetCreatedJavaVMs(JavaVM **vm_buf, jsize bufLen, jsize *numVMs) {
4034   // See bug 4367188, the wrapper can sometimes cause VM crashes
4035   // JNIWrapper("GetCreatedJavaVMs");
4036 
4037   HOTSPOT_JNI_GETCREATEDJAVAVMS_ENTRY((void **) vm_buf, bufLen, (uintptr_t *) numVMs);
4038 
4039   if (vm_created == 1) {
4040     if (numVMs != NULL) *numVMs = 1;
4041     if (bufLen > 0)     *vm_buf = (JavaVM *)(&main_vm);
4042   } else {
4043     if (numVMs != NULL) *numVMs = 0;
4044   }
4045   HOTSPOT_JNI_GETCREATEDJAVAVMS_RETURN(JNI_OK);
4046   return JNI_OK;
4047 }
4048 
4049 extern "C" {
4050 
4051 DT_RETURN_MARK_DECL(DestroyJavaVM, jint
4052                     , HOTSPOT_JNI_DESTROYJAVAVM_RETURN(_ret_ref));
4053 
4054 static jint JNICALL jni_DestroyJavaVM_inner(JavaVM *vm) {
4055   HOTSPOT_JNI_DESTROYJAVAVM_ENTRY(vm);
4056   jint res = JNI_ERR;
4057   DT_RETURN_MARK(DestroyJavaVM, jint, (const jint&)res);
4058 
4059   if (vm_created == 0) {
4060     res = JNI_ERR;
4061     return res;
4062   }
4063 
4064   JNIWrapper("DestroyJavaVM");
4065   JNIEnv *env;
4066   JavaVMAttachArgs destroyargs;
4067   destroyargs.version = CurrentVersion;
4068   destroyargs.name = (char *)"DestroyJavaVM";
4069   destroyargs.group = NULL;
4070   res = vm->AttachCurrentThread((void **)&env, (void *)&destroyargs);
4071   if (res != JNI_OK) {
4072     return res;
4073   }
4074 
4075   // Since this is not a JVM_ENTRY we have to set the thread state manually before entering.
4076   JavaThread* thread = JavaThread::current();
4077   ThreadStateTransition::transition_from_native(thread, _thread_in_vm);
4078   if (Threads::destroy_vm()) {
4079     // Should not change thread state, VM is gone
4080     vm_created = 0;
4081     res = JNI_OK;
4082     return res;
4083   } else {
4084     ThreadStateTransition::transition_and_fence(thread, _thread_in_vm, _thread_in_native);
4085     res = JNI_ERR;
4086     return res;
4087   }
4088 }
4089 
4090 jint JNICALL jni_DestroyJavaVM(JavaVM *vm) {
4091   jint result = JNI_ERR;
4092   // On Windows, we need SEH protection
4093 #ifdef _WIN32
4094   __try {
4095 #endif
4096     result = jni_DestroyJavaVM_inner(vm);
4097 #ifdef _WIN32
4098   } __except(topLevelExceptionFilter((_EXCEPTION_POINTERS*)_exception_info())) {
4099     // Nothing to do.
4100   }
4101 #endif
4102   return result;
4103 }
4104 
4105 static jint attach_current_thread(JavaVM *vm, void **penv, void *_args, bool daemon) {
4106   JavaVMAttachArgs *args = (JavaVMAttachArgs *) _args;
4107 
4108   // Check below commented out from JDK1.2fcs as well
4109   /*
4110   if (args && (args->version != JNI_VERSION_1_1 || args->version != JNI_VERSION_1_2)) {
4111     return JNI_EVERSION;
4112   }
4113   */
4114 
4115   Thread* t = Thread::current_or_null();
4116   if (t != NULL) {
4117     // If the thread has been attached this operation is a no-op
4118     *(JNIEnv**)penv = ((JavaThread*) t)->jni_environment();
4119     return JNI_OK;
4120   }
4121 
4122   // Create a thread and mark it as attaching so it will be skipped by the
4123   // ThreadsListEnumerator - see CR 6404306
4124   JavaThread* thread = new JavaThread(true);
4125 
4126   // Set correct safepoint info. The thread is going to call into Java when
4127   // initializing the Java level thread object. Hence, the correct state must
4128   // be set in order for the Safepoint code to deal with it correctly.
4129   thread->set_thread_state(_thread_in_vm);
4130   thread->record_stack_base_and_size();
4131   thread->initialize_thread_current();
4132 
4133   if (!os::create_attached_thread(thread)) {
4134     thread->smr_delete();
4135     return JNI_ERR;
4136   }
4137   // Enable stack overflow checks
4138   thread->create_stack_guard_pages();
4139 
4140   thread->initialize_tlab();
4141 
4142   thread->cache_global_variables();
4143 
4144   // Crucial that we do not have a safepoint check for this thread, since it has
4145   // not been added to the Thread list yet.
4146   { Threads_lock->lock_without_safepoint_check();
4147     // This must be inside this lock in order to get FullGCALot to work properly, i.e., to
4148     // avoid this thread trying to do a GC before it is added to the thread-list
4149     thread->set_active_handles(JNIHandleBlock::allocate_block());
4150     Threads::add(thread, daemon);
4151     Threads_lock->unlock();
4152   }
4153   // Create thread group and name info from attach arguments
4154   oop group = NULL;
4155   char* thread_name = NULL;
4156   if (args != NULL && Threads::is_supported_jni_version(args->version)) {
4157     group = JNIHandles::resolve(args->group);
4158     thread_name = args->name; // may be NULL
4159   }
4160   if (group == NULL) group = Universe::main_thread_group();
4161 
4162   // Create Java level thread object and attach it to this thread
4163   bool attach_failed = false;
4164   {
4165     EXCEPTION_MARK;
4166     HandleMark hm(THREAD);
4167     Handle thread_group(THREAD, group);
4168     thread->allocate_threadObj(thread_group, thread_name, daemon, THREAD);
4169     if (HAS_PENDING_EXCEPTION) {
4170       CLEAR_PENDING_EXCEPTION;
4171       // cleanup outside the handle mark.
4172       attach_failed = true;
4173     }
4174   }
4175 
4176   if (attach_failed) {
4177     // Added missing cleanup
4178     thread->cleanup_failed_attach_current_thread();
4179     return JNI_ERR;
4180   }
4181 
4182   // mark the thread as no longer attaching
4183   // this uses a fence to push the change through so we don't have
4184   // to regrab the threads_lock
4185   thread->set_done_attaching_via_jni();
4186 
4187   // Set java thread status.
4188   java_lang_Thread::set_thread_status(thread->threadObj(),
4189               java_lang_Thread::RUNNABLE);
4190 
4191   // Notify the debugger
4192   if (JvmtiExport::should_post_thread_life()) {
4193     JvmtiExport::post_thread_start(thread);
4194   }
4195 
4196   EventThreadStart event;
4197   if (event.should_commit()) {
4198     event.set_thread(THREAD_TRACE_ID(thread));
4199     event.commit();
4200   }
4201 
4202   *(JNIEnv**)penv = thread->jni_environment();
4203 
4204   // Now leaving the VM, so change thread_state. This is normally automatically taken care
4205   // of in the JVM_ENTRY. But in this situation we have to do it manually. Notice, that by
4206   // using ThreadStateTransition::transition, we do a callback to the safepoint code if
4207   // needed.
4208 
4209   ThreadStateTransition::transition_and_fence(thread, _thread_in_vm, _thread_in_native);
4210 
4211   // Perform any platform dependent FPU setup
4212   os::setup_fpu();
4213 
4214   return JNI_OK;
4215 }
4216 
4217 
4218 jint JNICALL jni_AttachCurrentThread(JavaVM *vm, void **penv, void *_args) {
4219   HOTSPOT_JNI_ATTACHCURRENTTHREAD_ENTRY(vm, penv, _args);
4220   if (vm_created == 0) {
4221   HOTSPOT_JNI_ATTACHCURRENTTHREAD_RETURN((uint32_t) JNI_ERR);
4222     return JNI_ERR;
4223   }
4224 
4225   JNIWrapper("AttachCurrentThread");
4226   jint ret = attach_current_thread(vm, penv, _args, false);
4227   HOTSPOT_JNI_ATTACHCURRENTTHREAD_RETURN(ret);
4228   return ret;
4229 }
4230 
4231 
4232 jint JNICALL jni_DetachCurrentThread(JavaVM *vm)  {
4233   HOTSPOT_JNI_DETACHCURRENTTHREAD_ENTRY(vm);
4234 
4235   JNIWrapper("DetachCurrentThread");
4236 
4237   // If the thread has already been detached the operation is a no-op
4238   if (Thread::current_or_null() == NULL) {
4239     HOTSPOT_JNI_DETACHCURRENTTHREAD_RETURN(JNI_OK);
4240     return JNI_OK;
4241   }
4242 
4243   VM_Exit::block_if_vm_exited();
4244 
4245   JavaThread* thread = JavaThread::current();
4246   if (thread->has_last_Java_frame()) {
4247     HOTSPOT_JNI_DETACHCURRENTTHREAD_RETURN((uint32_t) JNI_ERR);
4248     // Can't detach a thread that's running java, that can't work.
4249     return JNI_ERR;
4250   }
4251 
4252   // Safepoint support. Have to do call-back to safepoint code, if in the
4253   // middle of a safepoint operation
4254   ThreadStateTransition::transition_from_native(thread, _thread_in_vm);
4255 
4256   // XXX: Note that JavaThread::exit() call below removes the guards on the
4257   // stack pages set up via enable_stack_{red,yellow}_zone() calls
4258   // above in jni_AttachCurrentThread. Unfortunately, while the setting
4259   // of the guards is visible in jni_AttachCurrentThread above,
4260   // the removal of the guards is buried below in JavaThread::exit()
4261   // here. The abstraction should be more symmetrically either exposed
4262   // or hidden (e.g. it could probably be hidden in the same
4263   // (platform-dependent) methods where we do alternate stack
4264   // maintenance work?)
4265   thread->exit(false, JavaThread::jni_detach);
4266   thread->smr_delete();
4267 
4268   HOTSPOT_JNI_DETACHCURRENTTHREAD_RETURN(JNI_OK);
4269   return JNI_OK;
4270 }
4271 
4272 DT_RETURN_MARK_DECL(GetEnv, jint
4273                     , HOTSPOT_JNI_GETENV_RETURN(_ret_ref));
4274 
4275 jint JNICALL jni_GetEnv(JavaVM *vm, void **penv, jint version) {
4276   HOTSPOT_JNI_GETENV_ENTRY(vm, penv, version);
4277   jint ret = JNI_ERR;
4278   DT_RETURN_MARK(GetEnv, jint, (const jint&)ret);
4279 
4280   if (vm_created == 0) {
4281     *penv = NULL;
4282     ret = JNI_EDETACHED;
4283     return ret;
4284   }
4285 
4286   if (JniExportedInterface::GetExportedInterface(vm, penv, version, &ret)) {
4287     return ret;
4288   }
4289 
4290 #ifndef JVMPI_VERSION_1
4291 // need these in order to be polite about older agents
4292 #define JVMPI_VERSION_1   ((jint)0x10000001)
4293 #define JVMPI_VERSION_1_1 ((jint)0x10000002)
4294 #define JVMPI_VERSION_1_2 ((jint)0x10000003)
4295 #endif // !JVMPI_VERSION_1
4296 
4297   Thread* thread = Thread::current_or_null();
4298   if (thread != NULL && thread->is_Java_thread()) {
4299     if (Threads::is_supported_jni_version_including_1_1(version)) {
4300       *(JNIEnv**)penv = ((JavaThread*) thread)->jni_environment();
4301       ret = JNI_OK;
4302       return ret;
4303 
4304     } else if (version == JVMPI_VERSION_1 ||
4305                version == JVMPI_VERSION_1_1 ||
4306                version == JVMPI_VERSION_1_2) {
4307       tty->print_cr("ERROR: JVMPI, an experimental interface, is no longer supported.");
4308       tty->print_cr("Please use the supported interface: the JVM Tool Interface (JVM TI).");
4309       ret = JNI_EVERSION;
4310       return ret;
4311     } else if (JvmtiExport::is_jvmdi_version(version)) {
4312       tty->print_cr("FATAL ERROR: JVMDI is no longer supported.");
4313       tty->print_cr("Please use the supported interface: the JVM Tool Interface (JVM TI).");
4314       ret = JNI_EVERSION;
4315       return ret;
4316     } else {
4317       *penv = NULL;
4318       ret = JNI_EVERSION;
4319       return ret;
4320     }
4321   } else {
4322     *penv = NULL;
4323     ret = JNI_EDETACHED;
4324     return ret;
4325   }
4326 }
4327 
4328 
4329 jint JNICALL jni_AttachCurrentThreadAsDaemon(JavaVM *vm, void **penv, void *_args) {
4330   HOTSPOT_JNI_ATTACHCURRENTTHREADASDAEMON_ENTRY(vm, penv, _args);
4331   if (vm_created == 0) {
4332   HOTSPOT_JNI_ATTACHCURRENTTHREADASDAEMON_RETURN((uint32_t) JNI_ERR);
4333     return JNI_ERR;
4334   }
4335 
4336   JNIWrapper("AttachCurrentThreadAsDaemon");
4337   jint ret = attach_current_thread(vm, penv, _args, true);
4338   HOTSPOT_JNI_ATTACHCURRENTTHREADASDAEMON_RETURN(ret);
4339   return ret;
4340 }
4341 
4342 
4343 } // End extern "C"
4344 
4345 const struct JNIInvokeInterface_ jni_InvokeInterface = {
4346     NULL,
4347     NULL,
4348     NULL,
4349 
4350     jni_DestroyJavaVM,
4351     jni_AttachCurrentThread,
4352     jni_DetachCurrentThread,
4353     jni_GetEnv,
4354     jni_AttachCurrentThreadAsDaemon
4355 };
--- EOF ---