1 /*
   2  * Copyright (c) 1997, 2017, Oracle and/or its affiliates. All rights reserved.
   3  * Copyright (c) 2012 Red Hat, Inc.
   4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   5  *
   6  * This code is free software; you can redistribute it and/or modify it
   7  * under the terms of the GNU General Public License version 2 only, as
   8  * published by the Free Software Foundation.
   9  *
  10  * This code is distributed in the hope that it will be useful, but WITHOUT
  11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  13  * version 2 for more details (a copy is included in the LICENSE file that
  14  * accompanied this code).
  15  *
  16  * You should have received a copy of the GNU General Public License version
  17  * 2 along with this work; if not, write to the Free Software Foundation,
  18  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  19  *
  20  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  21  * or visit www.oracle.com if you need additional information or have any
  22  * questions.
  23  *
  24  */
  25 
  26 #include "precompiled.hpp"
  27 #include "ci/ciReplay.hpp"
  28 #include "classfile/altHashing.hpp"
  29 #include "classfile/classFileStream.hpp"
  30 #include "classfile/classLoader.hpp"
  31 #include "classfile/javaClasses.hpp"
  32 #include "classfile/javaClasses.inline.hpp"
  33 #include "classfile/modules.hpp"
  34 #include "classfile/symbolTable.hpp"
  35 #include "classfile/systemDictionary.hpp"
  36 #include "classfile/vmSymbols.hpp"
  37 #include "gc/shared/gcLocker.inline.hpp"
  38 #include "interpreter/linkResolver.hpp"
  39 #include "memory/allocation.hpp"
  40 #include "memory/allocation.inline.hpp"
  41 #include "memory/oopFactory.hpp"
  42 #include "memory/resourceArea.hpp"
  43 #include "memory/universe.inline.hpp"
  44 #include "oops/instanceKlass.hpp"
  45 #include "oops/instanceOop.hpp"
  46 #include "oops/markOop.hpp"
  47 #include "oops/method.hpp"
  48 #include "oops/objArrayKlass.hpp"
  49 #include "oops/objArrayOop.inline.hpp"
  50 #include "oops/oop.inline.hpp"
  51 #include "oops/symbol.hpp"
  52 #include "oops/typeArrayKlass.hpp"
  53 #include "oops/typeArrayOop.hpp"
  54 #include "prims/jni.h"
  55 #include "prims/jniCheck.hpp"
  56 #include "prims/jniExport.hpp"
  57 #include "prims/jniFastGetField.hpp"
  58 #include "prims/jvm.h"
  59 #include "prims/jvm_misc.hpp"
  60 #include "prims/jvmtiExport.hpp"
  61 #include "prims/jvmtiThreadState.hpp"
  62 #include "runtime/atomic.hpp"
  63 #include "runtime/compilationPolicy.hpp"
  64 #include "runtime/fieldDescriptor.hpp"
  65 #include "runtime/fprofiler.hpp"
  66 #include "runtime/handles.inline.hpp"
  67 #include "runtime/interfaceSupport.hpp"
  68 #include "runtime/java.hpp"
  69 #include "runtime/javaCalls.hpp"
  70 #include "runtime/jfieldIDWorkaround.hpp"
  71 #include "runtime/orderAccess.inline.hpp"
  72 #include "runtime/reflection.hpp"
  73 #include "runtime/sharedRuntime.hpp"
  74 #include "runtime/signature.hpp"
  75 #include "runtime/thread.inline.hpp"
  76 #include "runtime/vm_operations.hpp"
  77 #include "services/memTracker.hpp"
  78 #include "services/runtimeService.hpp"
  79 #include "trace/traceMacros.hpp"
  80 #include "trace/tracing.hpp"
  81 #include "utilities/defaultStream.hpp"
  82 #include "utilities/dtrace.hpp"
  83 #include "utilities/events.hpp"
  84 #include "utilities/histogram.hpp"
  85 #include "utilities/internalVMTests.hpp"
  86 #include "utilities/macros.hpp"

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