jni.cpp 214.9 KB
Newer Older
D
duke 已提交
1
/*
2
 * Copyright (c) 1997, 2018, Oracle and/or its affiliates. All rights reserved.
3
 * Copyright (c) 2012 Red Hat, Inc.
D
duke 已提交
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
20 21 22
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
D
duke 已提交
23 24 25
 *
 */

26
#include "precompiled.hpp"
27
#include "ci/ciReplay.hpp"
28
#include "classfile/altHashing.hpp"
29 30 31 32 33 34
#include "classfile/classLoader.hpp"
#include "classfile/javaClasses.hpp"
#include "classfile/symbolTable.hpp"
#include "classfile/systemDictionary.hpp"
#include "classfile/vmSymbols.hpp"
#include "interpreter/linkResolver.hpp"
A
apetushkov 已提交
35 36
#include "jfr/jfrEvents.hpp"
#include "jfr/support/jfrThreadId.hpp"
37
#include "utilities/macros.hpp"
38
#include "utilities/ostream.hpp"
39
#if INCLUDE_ALL_GCS
40
#include "gc_implementation/g1/g1SATBCardTableModRefBS.hpp"
41
#endif // INCLUDE_ALL_GCS
Z
zgu 已提交
42
#include "memory/allocation.hpp"
43 44 45 46 47 48 49
#include "memory/allocation.inline.hpp"
#include "memory/gcLocker.inline.hpp"
#include "memory/oopFactory.hpp"
#include "memory/universe.inline.hpp"
#include "oops/instanceKlass.hpp"
#include "oops/instanceOop.hpp"
#include "oops/markOop.hpp"
50
#include "oops/method.hpp"
51 52 53
#include "oops/objArrayKlass.hpp"
#include "oops/objArrayOop.hpp"
#include "oops/oop.inline.hpp"
54
#include "oops/symbol.hpp"
55 56 57 58
#include "oops/typeArrayKlass.hpp"
#include "oops/typeArrayOop.hpp"
#include "prims/jni.h"
#include "prims/jniCheck.hpp"
59
#include "prims/jniExport.hpp"
60 61 62 63 64 65 66 67 68 69 70 71 72
#include "prims/jniFastGetField.hpp"
#include "prims/jvm.h"
#include "prims/jvm_misc.hpp"
#include "prims/jvmtiExport.hpp"
#include "prims/jvmtiThreadState.hpp"
#include "runtime/compilationPolicy.hpp"
#include "runtime/fieldDescriptor.hpp"
#include "runtime/fprofiler.hpp"
#include "runtime/handles.inline.hpp"
#include "runtime/interfaceSupport.hpp"
#include "runtime/java.hpp"
#include "runtime/javaCalls.hpp"
#include "runtime/jfieldIDWorkaround.hpp"
73
#include "runtime/orderAccess.inline.hpp"
74 75 76
#include "runtime/reflection.hpp"
#include "runtime/sharedRuntime.hpp"
#include "runtime/signature.hpp"
77
#include "runtime/thread.inline.hpp"
78
#include "runtime/vm_operations.hpp"
79
#include "services/memTracker.hpp"
80 81 82 83 84 85 86 87 88 89 90 91 92 93
#include "services/runtimeService.hpp"
#include "utilities/defaultStream.hpp"
#include "utilities/dtrace.hpp"
#include "utilities/events.hpp"
#include "utilities/histogram.hpp"
#ifdef TARGET_OS_FAMILY_linux
# include "os_linux.inline.hpp"
#endif
#ifdef TARGET_OS_FAMILY_solaris
# include "os_solaris.inline.hpp"
#endif
#ifdef TARGET_OS_FAMILY_windows
# include "os_windows.inline.hpp"
#endif
N
never 已提交
94 95 96
#ifdef TARGET_OS_FAMILY_bsd
# include "os_bsd.inline.hpp"
#endif
D
duke 已提交
97

98
static jint CurrentVersion = JNI_VERSION_1_8;
D
duke 已提交
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120


// The DT_RETURN_MARK macros create a scoped object to fire the dtrace
// '-return' probe regardless of the return path is taken out of the function.
// Methods that have multiple return paths use this to avoid having to
// instrument each return path.  Methods that use CHECK or THROW must use this
// since those macros can cause an immedate uninstrumented return.
//
// In order to get the return value, a reference to the variable containing
// the return value must be passed to the contructor of the object, and
// the return value must be set before return (since the mark object has
// a reference to it).
//
// Example:
// DT_RETURN_MARK_DECL(SomeFunc, int);
// JNI_ENTRY(int, SomeFunc, ...)
//   int return_value = 0;
//   DT_RETURN_MARK(SomeFunc, int, (const int&)return_value);
//   foo(CHECK_0)
//   return_value = 5;
//   return return_value;
// JNI_END
D
dcubed 已提交
121
#ifndef USDT2
D
duke 已提交
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
#define DT_RETURN_MARK_DECL(name, type)                                    \
  HS_DTRACE_PROBE_DECL1(hotspot_jni, name##__return, type);                \
  DTRACE_ONLY(                                                             \
    class DTraceReturnProbeMark_##name {                                   \
     public:                                                               \
      const type& _ret_ref;                                                \
      DTraceReturnProbeMark_##name(const type& v) : _ret_ref(v) {}         \
      ~DTraceReturnProbeMark_##name() {                                    \
        HS_DTRACE_PROBE1(hotspot_jni, name##__return, _ret_ref);           \
      }                                                                    \
    }                                                                      \
  )
// Void functions are simpler since there's no return value
#define DT_VOID_RETURN_MARK_DECL(name)                                     \
  HS_DTRACE_PROBE_DECL0(hotspot_jni, name##__return);                      \
  DTRACE_ONLY(                                                             \
    class DTraceReturnProbeMark_##name {                                   \
     public:                                                               \
      ~DTraceReturnProbeMark_##name() {                                    \
        HS_DTRACE_PROBE0(hotspot_jni, name##__return);                     \
      }                                                                    \
    }                                                                      \
  )

D
dcubed 已提交
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
#else /* USDT2 */

#define DT_RETURN_MARK_DECL(name, type, probe)                             \
  DTRACE_ONLY(                                                             \
    class DTraceReturnProbeMark_##name {                                   \
     public:                                                               \
      const type& _ret_ref;                                                \
      DTraceReturnProbeMark_##name(const type& v) : _ret_ref(v) {}         \
      ~DTraceReturnProbeMark_##name() {                                    \
        probe;                                                             \
      }                                                                    \
    }                                                                      \
  )
// Void functions are simpler since there's no return value
#define DT_VOID_RETURN_MARK_DECL(name, probe)                              \
  DTRACE_ONLY(                                                             \
    class DTraceReturnProbeMark_##name {                                   \
     public:                                                               \
      ~DTraceReturnProbeMark_##name() {                                    \
        probe;                                                             \
      }                                                                    \
    }                                                                      \
  )
#endif /* USDT2 */
D
duke 已提交
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197

// Place these macros in the function to mark the return.  Non-void
// functions need the type and address of the return value.
#define DT_RETURN_MARK(name, type, ref) \
  DTRACE_ONLY( DTraceReturnProbeMark_##name dtrace_return_mark(ref) )
#define DT_VOID_RETURN_MARK(name) \
  DTRACE_ONLY( DTraceReturnProbeMark_##name dtrace_return_mark )


// Use these to select distinct code for floating-point vs. non-floating point
// situations.  Used from within common macros where we need slightly
// different behavior for Float/Double
#define FP_SELECT_Boolean(intcode, fpcode) intcode
#define FP_SELECT_Byte(intcode, fpcode)    intcode
#define FP_SELECT_Char(intcode, fpcode)    intcode
#define FP_SELECT_Short(intcode, fpcode)   intcode
#define FP_SELECT_Object(intcode, fpcode)  intcode
#define FP_SELECT_Int(intcode, fpcode)     intcode
#define FP_SELECT_Long(intcode, fpcode)    intcode
#define FP_SELECT_Float(intcode, fpcode)   fpcode
#define FP_SELECT_Double(intcode, fpcode)  fpcode
#define FP_SELECT(TypeName, intcode, fpcode) \
  FP_SELECT_##TypeName(intcode, fpcode)

#define COMMA ,

// Choose DT_RETURN_MARK macros  based on the type: float/double -> void
// (dtrace doesn't do FP yet)
D
dcubed 已提交
198
#ifndef USDT2
D
duke 已提交
199 200 201
#define DT_RETURN_MARK_DECL_FOR(TypeName, name, type) \
  FP_SELECT(TypeName, \
    DT_RETURN_MARK_DECL(name, type), DT_VOID_RETURN_MARK_DECL(name) )
D
dcubed 已提交
202 203 204 205 206
#else /* USDT2 */
#define DT_RETURN_MARK_DECL_FOR(TypeName, name, type, probe)    \
  FP_SELECT(TypeName, \
    DT_RETURN_MARK_DECL(name, type, probe), DT_VOID_RETURN_MARK_DECL(name, probe) )
#endif /* USDT2 */
D
duke 已提交
207 208 209 210 211 212 213
#define DT_RETURN_MARK_FOR(TypeName, name, type, ref) \
  FP_SELECT(TypeName, \
    DT_RETURN_MARK(name, type, ref), DT_VOID_RETURN_MARK(name) )


// out-of-line helpers for class jfieldIDWorkaround:

214
bool jfieldIDWorkaround::is_valid_jfieldID(Klass* k, jfieldID id) {
D
duke 已提交
215 216 217 218 219 220 221 222
  if (jfieldIDWorkaround::is_instance_jfieldID(k, id)) {
    uintptr_t as_uint = (uintptr_t) id;
    intptr_t offset = raw_instance_offset(id);
    if (is_checked_jfieldID(id)) {
      if (!klass_hash_ok(k, id)) {
        return false;
      }
    }
223
    return InstanceKlass::cast(k)->contains_field_offset(offset);
D
duke 已提交
224 225 226 227 228 229 230 231 232 233 234
  } else {
    JNIid* result = (JNIid*) id;
#ifdef ASSERT
    return result != NULL && result->is_static_field_id();
#else
    return result != NULL;
#endif
  }
}


235
intptr_t jfieldIDWorkaround::encode_klass_hash(Klass* k, intptr_t offset) {
D
duke 已提交
236
  if (offset <= small_offset_mask) {
237
    Klass* field_klass = k;
H
hseigel 已提交
238
    Klass* super_klass = field_klass->super();
239 240
    // With compressed oops the most super class with nonstatic fields would
    // be the owner of fields embedded in the header.
241 242
    while (InstanceKlass::cast(super_klass)->has_nonstatic_fields() &&
           InstanceKlass::cast(super_klass)->contains_field_offset(offset)) {
D
duke 已提交
243
      field_klass = super_klass;   // super contains the field also
H
hseigel 已提交
244
      super_klass = field_klass->super();
D
duke 已提交
245 246 247 248 249 250 251 252 253
    }
    debug_only(No_Safepoint_Verifier nosafepoint;)
    uintptr_t klass_hash = field_klass->identity_hash();
    return ((klass_hash & klass_mask) << klass_shift) | checked_mask_in_place;
  } else {
#if 0
    #ifndef PRODUCT
    {
      ResourceMark rm;
H
hseigel 已提交
254
      warning("VerifyJNIFields: long offset %d in %s", offset, k->external_name());
D
duke 已提交
255 256 257 258 259 260 261
    }
    #endif
#endif
    return 0;
  }
}

262
bool jfieldIDWorkaround::klass_hash_ok(Klass* k, jfieldID id) {
D
duke 已提交
263 264 265 266 267 268 269
  uintptr_t as_uint = (uintptr_t) id;
  intptr_t klass_hash = (as_uint >> klass_shift) & klass_mask;
  do {
    debug_only(No_Safepoint_Verifier nosafepoint;)
    // Could use a non-blocking query for identity_hash here...
    if ((k->identity_hash() & klass_mask) == klass_hash)
      return true;
H
hseigel 已提交
270
    k = k->super();
D
duke 已提交
271 272 273 274
  } while (k != NULL);
  return false;
}

275
void jfieldIDWorkaround::verify_instance_jfieldID(Klass* k, jfieldID id) {
D
duke 已提交
276 277 278 279 280 281 282 283 284 285 286 287
  guarantee(jfieldIDWorkaround::is_instance_jfieldID(k, id), "must be an instance field" );
  uintptr_t as_uint = (uintptr_t) id;
  intptr_t offset = raw_instance_offset(id);
  if (VerifyJNIFields) {
    if (is_checked_jfieldID(id)) {
      guarantee(klass_hash_ok(k, id),
    "Bug in native code: jfieldID class must match object");
    } else {
#if 0
      #ifndef PRODUCT
      if (Verbose) {
  ResourceMark rm;
H
hseigel 已提交
288
  warning("VerifyJNIFields: unverified offset %d for %s", offset, k->external_name());
D
duke 已提交
289 290 291 292 293
      }
      #endif
#endif
    }
  }
294
  guarantee(InstanceKlass::cast(k)->contains_field_offset(offset),
D
duke 已提交
295 296 297 298 299 300 301 302 303 304 305
      "Bug in native code: jfieldID offset must address interior of object");
}

// Wrapper to trace JNI functions

#ifdef ASSERT
  Histogram* JNIHistogram;
  static volatile jint JNIHistogram_lock = 0;

  class JNITraceWrapper : public StackObj {
   public:
306
    JNITraceWrapper(const char* format, ...) ATTRIBUTE_PRINTF(2, 3) {
D
duke 已提交
307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355
      if (TraceJNICalls) {
        va_list ap;
        va_start(ap, format);
        tty->print("JNI ");
        tty->vprint_cr(format, ap);
        va_end(ap);
      }
    }
  };

  class JNIHistogramElement : public HistogramElement {
    public:
     JNIHistogramElement(const char* name);
  };

  JNIHistogramElement::JNIHistogramElement(const char* elementName) {
    _name = elementName;
    uintx count = 0;

    while (Atomic::cmpxchg(1, &JNIHistogram_lock, 0) != 0) {
      while (OrderAccess::load_acquire(&JNIHistogram_lock) != 0) {
        count +=1;
        if ( (WarnOnStalledSpinLock > 0)
          && (count % WarnOnStalledSpinLock == 0)) {
          warning("JNIHistogram_lock seems to be stalled");
        }
      }
     }


    if(JNIHistogram == NULL)
      JNIHistogram = new Histogram("JNI Call Counts",100);

    JNIHistogram->add_element(this);
    Atomic::dec(&JNIHistogram_lock);
  }

  #define JNICountWrapper(arg)                                     \
     static JNIHistogramElement* e = new JNIHistogramElement(arg); \
      /* There is a MT-race condition in VC++. So we need to make sure that that e has been initialized */ \
     if (e != NULL) e->increment_count()
  #define JNIWrapper(arg) JNICountWrapper(arg); JNITraceWrapper(arg)
#else
  #define JNIWrapper(arg)
#endif


// Implementation of JNI entries

D
dcubed 已提交
356
#ifndef USDT2
D
duke 已提交
357
DT_RETURN_MARK_DECL(DefineClass, jclass);
D
dcubed 已提交
358 359 360 361
#else /* USDT2 */
DT_RETURN_MARK_DECL(DefineClass, jclass
                    , HOTSPOT_JNI_DEFINECLASS_RETURN(_ret_ref));
#endif /* USDT2 */
D
duke 已提交
362 363 364 365 366

JNI_ENTRY(jclass, jni_DefineClass(JNIEnv *env, const char *name, jobject loaderRef,
                                  const jbyte *buf, jsize bufLen))
  JNIWrapper("DefineClass");

D
dcubed 已提交
367
#ifndef USDT2
D
duke 已提交
368 369
  DTRACE_PROBE5(hotspot_jni, DefineClass__entry,
    env, name, loaderRef, buf, bufLen);
D
dcubed 已提交
370 371 372 373
#else /* USDT2 */
  HOTSPOT_JNI_DEFINECLASS_ENTRY(
    env, (char*) name, loaderRef, (char*) buf, bufLen);
#endif /* USDT2 */
D
duke 已提交
374 375 376
  jclass cls = NULL;
  DT_RETURN_MARK(DefineClass, jclass, (const jclass&)cls);

377
  TempNewSymbol class_name = NULL;
D
duke 已提交
378 379 380 381
  // Since exceptions can be thrown, class initialization can take place
  // if name is NULL no check for class name in .class stream has to be made.
  if (name != NULL) {
    const int str_len = (int)strlen(name);
382
    if (str_len > Symbol::max_length()) {
D
duke 已提交
383 384 385 386
      // It's impossible to create this class;  the name cannot fit
      // into the constant pool.
      THROW_MSG_0(vmSymbols::java_lang_NoClassDefFoundError(), name);
    }
387
    class_name = SymbolTable::new_symbol(name, CHECK_NULL);
D
duke 已提交
388 389 390 391 392 393 394 395 396 397 398 399 400 401
  }
  ResourceMark rm(THREAD);
  ClassFileStream st((u1*) buf, bufLen, NULL);
  Handle class_loader (THREAD, JNIHandles::resolve(loaderRef));

  if (UsePerfData && !class_loader.is_null()) {
    // check whether the current caller thread holds the lock or not.
    // If not, increment the corresponding counter
    if (ObjectSynchronizer::
        query_lock_ownership((JavaThread*)THREAD, class_loader) !=
        ObjectSynchronizer::owner_self) {
      ClassLoader::sync_JNIDefineClassLockFreeCounter()->inc();
    }
  }
402
  Klass* k = SystemDictionary::resolve_from_stream(class_name, class_loader,
403 404
                                                     Handle(), &st, true,
                                                     CHECK_NULL);
D
duke 已提交
405

406 407 408 409
  if (TraceClassResolution && k != NULL) {
    trace_class_resolution(k);
  }

D
duke 已提交
410
  cls = (jclass)JNIHandles::make_local(
H
hseigel 已提交
411
    env, k->java_mirror());
D
duke 已提交
412 413 414 415 416 417 418
  return cls;
JNI_END



static bool first_time_FindClass = true;

D
dcubed 已提交
419
#ifndef USDT2
D
duke 已提交
420
DT_RETURN_MARK_DECL(FindClass, jclass);
D
dcubed 已提交
421 422 423 424
#else /* USDT2 */
DT_RETURN_MARK_DECL(FindClass, jclass
                    , HOTSPOT_JNI_FINDCLASS_RETURN(_ret_ref));
#endif /* USDT2 */
D
duke 已提交
425 426 427

JNI_ENTRY(jclass, jni_FindClass(JNIEnv *env, const char *name))
  JNIWrapper("FindClass");
D
dcubed 已提交
428
#ifndef USDT2
D
duke 已提交
429
  DTRACE_PROBE2(hotspot_jni, FindClass__entry, env, name);
D
dcubed 已提交
430 431 432 433
#else /* USDT2 */
  HOTSPOT_JNI_FINDCLASS_ENTRY(
                              env, (char *)name);
#endif /* USDT2 */
D
duke 已提交
434 435 436 437 438 439 440 441 442 443

  jclass result = NULL;
  DT_RETURN_MARK(FindClass, jclass, (const jclass&)result);

  // Remember if we are the first invocation of jni_FindClass
  bool first_time = first_time_FindClass;
  first_time_FindClass = false;

  // Sanity check the name:  it cannot be null or larger than the maximum size
  // name we can fit in the constant pool.
444
  if (name == NULL || (int)strlen(name) > Symbol::max_length()) {
D
duke 已提交
445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460
    THROW_MSG_0(vmSymbols::java_lang_NoClassDefFoundError(), name);
  }

  //%note jni_3
  Handle loader;
  Handle protection_domain;
  // Find calling class
  instanceKlassHandle k (THREAD, thread->security_get_caller_class(0));
  if (k.not_null()) {
    loader = Handle(THREAD, k->class_loader());
    // Special handling to make sure JNI_OnLoad and JNI_OnUnload are executed
    // in the correct class context.
    if (loader.is_null() &&
        k->name() == vmSymbols::java_lang_ClassLoader_NativeLibrary()) {
      JavaValue result(T_OBJECT);
      JavaCalls::call_static(&result, k,
461 462
                                      vmSymbols::getFromClass_name(),
                                      vmSymbols::void_class_signature(),
D
duke 已提交
463 464 465 466 467 468 469 470
                                      thread);
      if (HAS_PENDING_EXCEPTION) {
        Handle ex(thread, thread->pending_exception());
        CLEAR_PENDING_EXCEPTION;
        THROW_HANDLE_0(ex);
      }
      oop mirror = (oop) result.get_jobject();
      loader = Handle(THREAD,
471
        InstanceKlass::cast(java_lang_Class::as_Klass(mirror))->class_loader());
D
duke 已提交
472
      protection_domain = Handle(THREAD,
473
        InstanceKlass::cast(java_lang_Class::as_Klass(mirror))->protection_domain());
D
duke 已提交
474 475 476 477 478 479
    }
  } else {
    // We call ClassLoader.getSystemClassLoader to obtain the system class loader.
    loader = Handle(THREAD, SystemDictionary::java_system_loader());
  }

480
  TempNewSymbol sym = SymbolTable::new_symbol(name, CHECK_NULL);
D
duke 已提交
481 482 483
  result = find_class_from_class_loader(env, sym, true, loader,
                                        protection_domain, true, thread);

484
  if (TraceClassResolution && result != NULL) {
485
    trace_class_resolution(java_lang_Class::as_Klass(JNIHandles::resolve_non_null(result)));
486 487
  }

D
duke 已提交
488 489 490 491 492 493 494 495 496
  // If we were the first invocation of jni_FindClass, we enable compilation again
  // rather than just allowing invocation counter to overflow and decay.
  // Controlled by flag DelayCompilationDuringStartup.
  if (first_time && !CompileTheWorld)
    CompilationPolicy::completed_vm_startup();

  return result;
JNI_END

D
dcubed 已提交
497
#ifndef USDT2
D
duke 已提交
498
DT_RETURN_MARK_DECL(FromReflectedMethod, jmethodID);
D
dcubed 已提交
499 500 501 502
#else /* USDT2 */
DT_RETURN_MARK_DECL(FromReflectedMethod, jmethodID
                    , HOTSPOT_JNI_FROMREFLECTEDMETHOD_RETURN((uintptr_t)_ret_ref));
#endif /* USDT2 */
D
duke 已提交
503 504 505

JNI_ENTRY(jmethodID, jni_FromReflectedMethod(JNIEnv *env, jobject method))
  JNIWrapper("FromReflectedMethod");
D
dcubed 已提交
506
#ifndef USDT2
D
duke 已提交
507
  DTRACE_PROBE2(hotspot_jni, FromReflectedMethod__entry, env, method);
D
dcubed 已提交
508 509 510 511
#else /* USDT2 */
  HOTSPOT_JNI_FROMREFLECTEDMETHOD_ENTRY(
                                        env, method);
#endif /* USDT2 */
D
duke 已提交
512 513 514 515 516 517 518 519
  jmethodID ret = NULL;
  DT_RETURN_MARK(FromReflectedMethod, jmethodID, (const jmethodID&)ret);

  // method is a handle to a java.lang.reflect.Method object
  oop reflected  = JNIHandles::resolve_non_null(method);
  oop mirror     = NULL;
  int slot       = 0;

520
  if (reflected->klass() == SystemDictionary::reflect_Constructor_klass()) {
D
duke 已提交
521 522 523
    mirror = java_lang_reflect_Constructor::clazz(reflected);
    slot   = java_lang_reflect_Constructor::slot(reflected);
  } else {
524
    assert(reflected->klass() == SystemDictionary::reflect_Method_klass(), "wrong type");
D
duke 已提交
525 526 527
    mirror = java_lang_reflect_Method::clazz(reflected);
    slot   = java_lang_reflect_Method::slot(reflected);
  }
528
  Klass* k     = java_lang_Class::as_Klass(mirror);
D
duke 已提交
529 530 531

  KlassHandle k1(THREAD, k);
  // Make sure class is initialized before handing id's out to methods
H
hseigel 已提交
532
  k1()->initialize(CHECK_NULL);
533
  Method* m = InstanceKlass::cast(k1())->method_with_idnum(slot);
D
duke 已提交
534 535 536 537
  ret = m==NULL? NULL : m->jmethod_id();  // return NULL if reflected method deleted
  return ret;
JNI_END

D
dcubed 已提交
538
#ifndef USDT2
D
duke 已提交
539
DT_RETURN_MARK_DECL(FromReflectedField, jfieldID);
D
dcubed 已提交
540 541 542 543
#else /* USDT2 */
DT_RETURN_MARK_DECL(FromReflectedField, jfieldID
                    , HOTSPOT_JNI_FROMREFLECTEDFIELD_RETURN((uintptr_t)_ret_ref));
#endif /* USDT2 */
D
duke 已提交
544 545 546

JNI_ENTRY(jfieldID, jni_FromReflectedField(JNIEnv *env, jobject field))
  JNIWrapper("FromReflectedField");
D
dcubed 已提交
547
#ifndef USDT2
D
duke 已提交
548
  DTRACE_PROBE2(hotspot_jni, FromReflectedField__entry, env, field);
D
dcubed 已提交
549 550 551 552
#else /* USDT2 */
  HOTSPOT_JNI_FROMREFLECTEDFIELD_ENTRY(
                                       env, field);
#endif /* USDT2 */
D
duke 已提交
553 554 555 556 557 558
  jfieldID ret = NULL;
  DT_RETURN_MARK(FromReflectedField, jfieldID, (const jfieldID&)ret);

  // field is a handle to a java.lang.reflect.Field object
  oop reflected   = JNIHandles::resolve_non_null(field);
  oop mirror      = java_lang_reflect_Field::clazz(reflected);
559
  Klass* k      = java_lang_Class::as_Klass(mirror);
D
duke 已提交
560 561 562 563 564
  int slot        = java_lang_reflect_Field::slot(reflected);
  int modifiers   = java_lang_reflect_Field::modifiers(reflected);

  KlassHandle k1(THREAD, k);
  // Make sure class is initialized before handing id's out to fields
H
hseigel 已提交
565
  k1()->initialize(CHECK_NULL);
D
duke 已提交
566 567 568

  // First check if this is a static field
  if (modifiers & JVM_ACC_STATIC) {
569 570
    intptr_t offset = InstanceKlass::cast(k1())->field_offset( slot );
    JNIid* id = InstanceKlass::cast(k1())->jni_id_for(offset);
D
duke 已提交
571 572
    assert(id != NULL, "corrupt Field object");
    debug_only(id->set_is_static_field_id();)
573
    // A jfieldID for a static field is a JNIid specifying the field holder and the offset within the Klass*
D
duke 已提交
574 575 576 577 578 579 580
    ret = jfieldIDWorkaround::to_static_jfieldID(id);
    return ret;
  }

  // The slot is the index of the field description in the field-array
  // The jfieldID is the offset of the field within the object
  // It may also have hash bits for k, if VerifyJNIFields is turned on.
581 582
  intptr_t offset = InstanceKlass::cast(k1())->field_offset( slot );
  assert(InstanceKlass::cast(k1())->contains_field_offset(offset), "stay within object");
D
duke 已提交
583 584 585 586
  ret = jfieldIDWorkaround::to_instance_jfieldID(k1(), offset);
  return ret;
JNI_END

D
dcubed 已提交
587
#ifndef USDT2
D
duke 已提交
588
DT_RETURN_MARK_DECL(ToReflectedMethod, jobject);
D
dcubed 已提交
589 590 591 592
#else /* USDT2 */
DT_RETURN_MARK_DECL(ToReflectedMethod, jobject
                    , HOTSPOT_JNI_TOREFLECTEDMETHOD_RETURN(_ret_ref));
#endif /* USDT2 */
D
duke 已提交
593 594 595

JNI_ENTRY(jobject, jni_ToReflectedMethod(JNIEnv *env, jclass cls, jmethodID method_id, jboolean isStatic))
  JNIWrapper("ToReflectedMethod");
D
dcubed 已提交
596
#ifndef USDT2
D
duke 已提交
597
  DTRACE_PROBE4(hotspot_jni, ToReflectedMethod__entry, env, cls, method_id, isStatic);
D
dcubed 已提交
598 599 600 601
#else /* USDT2 */
  HOTSPOT_JNI_TOREFLECTEDMETHOD_ENTRY(
                                      env, cls, (uintptr_t) method_id, isStatic);
#endif /* USDT2 */
D
duke 已提交
602 603 604
  jobject ret = NULL;
  DT_RETURN_MARK(ToReflectedMethod, jobject, (const jobject&)ret);

605
  methodHandle m (THREAD, Method::resolve_jmethod_id(method_id));
D
duke 已提交
606 607 608 609 610 611 612 613 614 615 616
  assert(m->is_static() == (isStatic != 0), "jni_ToReflectedMethod access flags doesn't match");
  oop reflection_method;
  if (m->is_initializer()) {
    reflection_method = Reflection::new_constructor(m, CHECK_NULL);
  } else {
    reflection_method = Reflection::new_method(m, UseNewReflection, false, CHECK_NULL);
  }
  ret = JNIHandles::make_local(env, reflection_method);
  return ret;
JNI_END

D
dcubed 已提交
617
#ifndef USDT2
D
duke 已提交
618
DT_RETURN_MARK_DECL(GetSuperclass, jclass);
D
dcubed 已提交
619 620 621 622
#else /* USDT2 */
DT_RETURN_MARK_DECL(GetSuperclass, jclass
                    , HOTSPOT_JNI_GETSUPERCLASS_RETURN(_ret_ref));
#endif /* USDT2 */
D
duke 已提交
623 624 625

JNI_ENTRY(jclass, jni_GetSuperclass(JNIEnv *env, jclass sub))
  JNIWrapper("GetSuperclass");
D
dcubed 已提交
626
#ifndef USDT2
D
duke 已提交
627
  DTRACE_PROBE2(hotspot_jni, GetSuperclass__entry, env, sub);
D
dcubed 已提交
628 629 630 631
#else /* USDT2 */
  HOTSPOT_JNI_GETSUPERCLASS_ENTRY(
                                  env, sub);
#endif /* USDT2 */
D
duke 已提交
632 633 634 635 636 637 638 639 640 641 642
  jclass obj = NULL;
  DT_RETURN_MARK(GetSuperclass, jclass, (const jclass&)obj);

  oop mirror = JNIHandles::resolve_non_null(sub);
  // primitive classes return NULL
  if (java_lang_Class::is_primitive(mirror)) return NULL;

  // Rules of Class.getSuperClass as implemented by KLass::java_super:
  // arrays return Object
  // interfaces return NULL
  // proper classes return Klass::super()
643
  Klass* k = java_lang_Class::as_Klass(mirror);
H
hseigel 已提交
644
  if (k->is_interface()) return NULL;
D
duke 已提交
645 646

  // return mirror for superclass
H
hseigel 已提交
647
  Klass* super = k->java_super();
D
duke 已提交
648
  // super2 is the value computed by the compiler's getSuperClass intrinsic:
H
hseigel 已提交
649
  debug_only(Klass* super2 = ( k->oop_is_array()
650
                                 ? SystemDictionary::Object_klass()
H
hseigel 已提交
651
                                 : k->super() ) );
D
duke 已提交
652 653
  assert(super == super2,
         "java_super computation depends on interface, array, other super");
H
hseigel 已提交
654
  obj = (super == NULL) ? NULL : (jclass) JNIHandles::make_local(super->java_mirror());
D
duke 已提交
655 656 657 658 659
  return obj;
JNI_END

JNI_QUICK_ENTRY(jboolean, jni_IsAssignableFrom(JNIEnv *env, jclass sub, jclass super))
  JNIWrapper("IsSubclassOf");
D
dcubed 已提交
660
#ifndef USDT2
D
duke 已提交
661
  DTRACE_PROBE3(hotspot_jni, IsAssignableFrom__entry, env, sub, super);
D
dcubed 已提交
662 663 664 665
#else /* USDT2 */
  HOTSPOT_JNI_ISASSIGNABLEFROM_ENTRY(
                                     env, sub, super);
#endif /* USDT2 */
D
duke 已提交
666 667 668 669 670
  oop sub_mirror   = JNIHandles::resolve_non_null(sub);
  oop super_mirror = JNIHandles::resolve_non_null(super);
  if (java_lang_Class::is_primitive(sub_mirror) ||
      java_lang_Class::is_primitive(super_mirror)) {
    jboolean ret = (sub_mirror == super_mirror);
D
dcubed 已提交
671
#ifndef USDT2
D
duke 已提交
672
    DTRACE_PROBE1(hotspot_jni, IsAssignableFrom__return, ret);
D
dcubed 已提交
673 674 675 676
#else /* USDT2 */
    HOTSPOT_JNI_ISASSIGNABLEFROM_RETURN(
                                        ret);
#endif /* USDT2 */
D
duke 已提交
677 678
    return ret;
  }
679 680
  Klass* sub_klass   = java_lang_Class::as_Klass(sub_mirror);
  Klass* super_klass = java_lang_Class::as_Klass(super_mirror);
D
duke 已提交
681
  assert(sub_klass != NULL && super_klass != NULL, "invalid arguments to jni_IsAssignableFrom");
H
hseigel 已提交
682
  jboolean ret = sub_klass->is_subtype_of(super_klass) ?
D
duke 已提交
683
                   JNI_TRUE : JNI_FALSE;
D
dcubed 已提交
684
#ifndef USDT2
D
duke 已提交
685
  DTRACE_PROBE1(hotspot_jni, IsAssignableFrom__return, ret);
D
dcubed 已提交
686 687 688 689
#else /* USDT2 */
  HOTSPOT_JNI_ISASSIGNABLEFROM_RETURN(
                                      ret);
#endif /* USDT2 */
D
duke 已提交
690 691 692
  return ret;
JNI_END

D
dcubed 已提交
693
#ifndef USDT2
D
duke 已提交
694
DT_RETURN_MARK_DECL(Throw, jint);
D
dcubed 已提交
695 696 697 698
#else /* USDT2 */
DT_RETURN_MARK_DECL(Throw, jint
                    , HOTSPOT_JNI_THROW_RETURN(_ret_ref));
#endif /* USDT2 */
D
duke 已提交
699 700 701

JNI_ENTRY(jint, jni_Throw(JNIEnv *env, jthrowable obj))
  JNIWrapper("Throw");
D
dcubed 已提交
702
#ifndef USDT2
D
duke 已提交
703
  DTRACE_PROBE2(hotspot_jni, Throw__entry, env, obj);
D
dcubed 已提交
704 705 706 707
#else /* USDT2 */
  HOTSPOT_JNI_THROW_ENTRY(
 env, obj);
#endif /* USDT2 */
D
duke 已提交
708 709 710 711 712
  jint ret = JNI_OK;
  DT_RETURN_MARK(Throw, jint, (const jint&)ret);

  THROW_OOP_(JNIHandles::resolve(obj), JNI_OK);
  ShouldNotReachHere();
713
  return 0; // Mute compiler.
D
duke 已提交
714 715
JNI_END

D
dcubed 已提交
716
#ifndef USDT2
D
duke 已提交
717
DT_RETURN_MARK_DECL(ThrowNew, jint);
D
dcubed 已提交
718 719 720 721
#else /* USDT2 */
DT_RETURN_MARK_DECL(ThrowNew, jint
                    , HOTSPOT_JNI_THROWNEW_RETURN(_ret_ref));
#endif /* USDT2 */
D
duke 已提交
722 723 724

JNI_ENTRY(jint, jni_ThrowNew(JNIEnv *env, jclass clazz, const char *message))
  JNIWrapper("ThrowNew");
D
dcubed 已提交
725
#ifndef USDT2
D
duke 已提交
726
  DTRACE_PROBE3(hotspot_jni, ThrowNew__entry, env, clazz, message);
D
dcubed 已提交
727 728 729 730
#else /* USDT2 */
  HOTSPOT_JNI_THROWNEW_ENTRY(
                             env, clazz, (char *) message);
#endif /* USDT2 */
D
duke 已提交
731 732 733
  jint ret = JNI_OK;
  DT_RETURN_MARK(ThrowNew, jint, (const jint&)ret);

734
  InstanceKlass* k = InstanceKlass::cast(java_lang_Class::as_Klass(JNIHandles::resolve_non_null(clazz)));
735
  Symbol*  name = k->name();
D
duke 已提交
736 737 738 739
  Handle class_loader (THREAD,  k->class_loader());
  Handle protection_domain (THREAD, k->protection_domain());
  THROW_MSG_LOADER_(name, (char *)message, class_loader, protection_domain, JNI_OK);
  ShouldNotReachHere();
740
  return 0; // Mute compiler.
D
duke 已提交
741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759
JNI_END


// JNI functions only transform a pending async exception to a synchronous
// exception in ExceptionOccurred and ExceptionCheck calls, since
// delivering an async exception in other places won't change the native
// code's control flow and would be harmful when native code further calls
// JNI functions with a pending exception. Async exception is also checked
// during the call, so ExceptionOccurred/ExceptionCheck won't return
// false but deliver the async exception at the very end during
// state transition.

static void jni_check_async_exceptions(JavaThread *thread) {
  assert(thread == Thread::current(), "must be itself");
  thread->check_and_handle_async_exceptions();
}

JNI_ENTRY_NO_PRESERVE(jthrowable, jni_ExceptionOccurred(JNIEnv *env))
  JNIWrapper("ExceptionOccurred");
D
dcubed 已提交
760
#ifndef USDT2
D
duke 已提交
761
  DTRACE_PROBE1(hotspot_jni, ExceptionOccurred__entry, env);
D
dcubed 已提交
762 763 764 765
#else /* USDT2 */
  HOTSPOT_JNI_EXCEPTIONOCCURRED_ENTRY(
                                      env);
#endif /* USDT2 */
D
duke 已提交
766 767 768
  jni_check_async_exceptions(thread);
  oop exception = thread->pending_exception();
  jthrowable ret = (jthrowable) JNIHandles::make_local(env, exception);
D
dcubed 已提交
769
#ifndef USDT2
D
duke 已提交
770
  DTRACE_PROBE1(hotspot_jni, ExceptionOccurred__return, ret);
D
dcubed 已提交
771 772 773 774
#else /* USDT2 */
  HOTSPOT_JNI_EXCEPTIONOCCURRED_RETURN(
                                       ret);
#endif /* USDT2 */
D
duke 已提交
775 776 777 778 779 780
  return ret;
JNI_END


JNI_ENTRY_NO_PRESERVE(void, jni_ExceptionDescribe(JNIEnv *env))
  JNIWrapper("ExceptionDescribe");
D
dcubed 已提交
781
#ifndef USDT2
D
duke 已提交
782
  DTRACE_PROBE1(hotspot_jni, ExceptionDescribe__entry, env);
D
dcubed 已提交
783 784 785 786
#else /* USDT2 */
  HOTSPOT_JNI_EXCEPTIONDESCRIBE_ENTRY(
                                      env);
#endif /* USDT2 */
D
duke 已提交
787 788 789
  if (thread->has_pending_exception()) {
    Handle ex(thread, thread->pending_exception());
    thread->clear_pending_exception();
790
    if (ex->is_a(SystemDictionary::ThreadDeath_klass())) {
D
duke 已提交
791 792 793 794 795 796 797 798
      // Don't print anything if we are being killed.
    } else {
      jio_fprintf(defaultStream::error_stream(), "Exception ");
      if (thread != NULL && thread->threadObj() != NULL) {
        ResourceMark rm(THREAD);
        jio_fprintf(defaultStream::error_stream(),
        "in thread \"%s\" ", thread->get_thread_name());
      }
799
      if (ex->is_a(SystemDictionary::Throwable_klass())) {
D
duke 已提交
800 801 802 803
        JavaValue result(T_VOID);
        JavaCalls::call_virtual(&result,
                                ex,
                                KlassHandle(THREAD,
804
                                  SystemDictionary::Throwable_klass()),
805 806
                                vmSymbols::printStackTrace_name(),
                                vmSymbols::void_method_signature(),
D
duke 已提交
807 808 809 810 811 812 813 814 815 816 817
                                THREAD);
        // If an exception is thrown in the call it gets thrown away. Not much
        // we can do with it. The native code that calls this, does not check
        // for the exception - hence, it might still be in the thread when DestroyVM gets
        // called, potentially causing a few asserts to trigger - since no pending exception
        // is expected.
        CLEAR_PENDING_EXCEPTION;
      } else {
        ResourceMark rm(THREAD);
        jio_fprintf(defaultStream::error_stream(),
        ". Uncaught exception of type %s.",
H
hseigel 已提交
818
        ex->klass()->external_name());
D
duke 已提交
819 820 821
      }
    }
  }
D
dcubed 已提交
822
#ifndef USDT2
D
duke 已提交
823
  DTRACE_PROBE(hotspot_jni, ExceptionDescribe__return);
D
dcubed 已提交
824 825 826 827
#else /* USDT2 */
  HOTSPOT_JNI_EXCEPTIONDESCRIBE_RETURN(
                                       );
#endif /* USDT2 */
D
duke 已提交
828 829 830 831 832
JNI_END


JNI_QUICK_ENTRY(void, jni_ExceptionClear(JNIEnv *env))
  JNIWrapper("ExceptionClear");
D
dcubed 已提交
833
#ifndef USDT2
D
duke 已提交
834
  DTRACE_PROBE1(hotspot_jni, ExceptionClear__entry, env);
D
dcubed 已提交
835 836 837 838
#else /* USDT2 */
  HOTSPOT_JNI_EXCEPTIONCLEAR_ENTRY(
                                   env);
#endif /* USDT2 */
D
duke 已提交
839 840 841 842 843 844 845 846

  // The jni code might be using this API to clear java thrown exception.
  // So just mark jvmti thread exception state as exception caught.
  JvmtiThreadState *state = JavaThread::current()->jvmti_thread_state();
  if (state != NULL && state->is_exception_detected()) {
    state->set_exception_caught();
  }
  thread->clear_pending_exception();
D
dcubed 已提交
847
#ifndef USDT2
D
duke 已提交
848
  DTRACE_PROBE(hotspot_jni, ExceptionClear__return);
D
dcubed 已提交
849 850 851 852
#else /* USDT2 */
  HOTSPOT_JNI_EXCEPTIONCLEAR_RETURN(
                                    );
#endif /* USDT2 */
D
duke 已提交
853 854 855 856 857
JNI_END


JNI_ENTRY(void, jni_FatalError(JNIEnv *env, const char *msg))
  JNIWrapper("FatalError");
D
dcubed 已提交
858
#ifndef USDT2
D
duke 已提交
859
  DTRACE_PROBE2(hotspot_jni, FatalError__entry, env, msg);
D
dcubed 已提交
860 861 862 863
#else /* USDT2 */
  HOTSPOT_JNI_FATALERROR_ENTRY(
                               env, (char *) msg);
#endif /* USDT2 */
D
duke 已提交
864 865
  tty->print_cr("FATAL ERROR in native method: %s", msg);
  thread->print_stack();
866
  os::abort(); // Dump core and abort
D
duke 已提交
867 868 869 870 871
JNI_END


JNI_ENTRY(jint, jni_PushLocalFrame(JNIEnv *env, jint capacity))
  JNIWrapper("PushLocalFrame");
D
dcubed 已提交
872
#ifndef USDT2
D
duke 已提交
873
  DTRACE_PROBE2(hotspot_jni, PushLocalFrame__entry, env, capacity);
D
dcubed 已提交
874 875 876 877
#else /* USDT2 */
  HOTSPOT_JNI_PUSHLOCALFRAME_ENTRY(
                                   env, capacity);
#endif /* USDT2 */
D
duke 已提交
878
  //%note jni_11
879 880
  if (capacity < 0 ||
      ((MaxJNILocalCapacity > 0) && (capacity > MaxJNILocalCapacity))) {
D
dcubed 已提交
881
#ifndef USDT2
D
duke 已提交
882
    DTRACE_PROBE1(hotspot_jni, PushLocalFrame__return, JNI_ERR);
D
dcubed 已提交
883 884 885 886
#else /* USDT2 */
    HOTSPOT_JNI_PUSHLOCALFRAME_RETURN(
                                      (uint32_t)JNI_ERR);
#endif /* USDT2 */
D
duke 已提交
887 888 889 890 891 892 893 894
    return JNI_ERR;
  }
  JNIHandleBlock* old_handles = thread->active_handles();
  JNIHandleBlock* new_handles = JNIHandleBlock::allocate_block(thread);
  assert(new_handles != NULL, "should not be NULL");
  new_handles->set_pop_frame_link(old_handles);
  thread->set_active_handles(new_handles);
  jint ret = JNI_OK;
D
dcubed 已提交
895
#ifndef USDT2
D
duke 已提交
896
  DTRACE_PROBE1(hotspot_jni, PushLocalFrame__return, ret);
D
dcubed 已提交
897 898 899 900
#else /* USDT2 */
  HOTSPOT_JNI_PUSHLOCALFRAME_RETURN(
                                    ret);
#endif /* USDT2 */
D
duke 已提交
901 902 903 904 905 906
  return ret;
JNI_END


JNI_ENTRY(jobject, jni_PopLocalFrame(JNIEnv *env, jobject result))
  JNIWrapper("PopLocalFrame");
D
dcubed 已提交
907
#ifndef USDT2
D
duke 已提交
908
  DTRACE_PROBE2(hotspot_jni, PopLocalFrame__entry, env, result);
D
dcubed 已提交
909 910 911 912
#else /* USDT2 */
  HOTSPOT_JNI_POPLOCALFRAME_ENTRY(
                                  env, result);
#endif /* USDT2 */
D
duke 已提交
913 914 915 916 917 918 919 920 921 922 923 924 925 926
  //%note jni_11
  Handle result_handle(thread, JNIHandles::resolve(result));
  JNIHandleBlock* old_handles = thread->active_handles();
  JNIHandleBlock* new_handles = old_handles->pop_frame_link();
  if (new_handles != NULL) {
    // As a sanity check we only release the handle blocks if the pop_frame_link is not NULL.
    // This way code will still work if PopLocalFrame is called without a corresponding
    // PushLocalFrame call. Note that we set the pop_frame_link to NULL explicitly, otherwise
    // the release_block call will release the blocks.
    thread->set_active_handles(new_handles);
    old_handles->set_pop_frame_link(NULL);              // clear link we won't release new_handles below
    JNIHandleBlock::release_block(old_handles, thread); // may block
    result = JNIHandles::make_local(thread, result_handle());
  }
D
dcubed 已提交
927
#ifndef USDT2
D
duke 已提交
928
  DTRACE_PROBE1(hotspot_jni, PopLocalFrame__return, result);
D
dcubed 已提交
929 930 931 932
#else /* USDT2 */
  HOTSPOT_JNI_POPLOCALFRAME_RETURN(
                                   result);
#endif /* USDT2 */
D
duke 已提交
933 934 935 936 937 938
  return result;
JNI_END


JNI_ENTRY(jobject, jni_NewGlobalRef(JNIEnv *env, jobject ref))
  JNIWrapper("NewGlobalRef");
D
dcubed 已提交
939
#ifndef USDT2
D
duke 已提交
940
  DTRACE_PROBE2(hotspot_jni, NewGlobalRef__entry, env, ref);
D
dcubed 已提交
941 942 943 944
#else /* USDT2 */
  HOTSPOT_JNI_NEWGLOBALREF_ENTRY(
                                 env, ref);
#endif /* USDT2 */
D
duke 已提交
945 946
  Handle ref_handle(thread, JNIHandles::resolve(ref));
  jobject ret = JNIHandles::make_global(ref_handle);
D
dcubed 已提交
947
#ifndef USDT2
D
duke 已提交
948
  DTRACE_PROBE1(hotspot_jni, NewGlobalRef__return, ret);
D
dcubed 已提交
949 950 951 952
#else /* USDT2 */
  HOTSPOT_JNI_NEWGLOBALREF_RETURN(
                                  ret);
#endif /* USDT2 */
D
duke 已提交
953 954 955 956 957 958
  return ret;
JNI_END

// Must be JNI_ENTRY (with HandleMark)
JNI_ENTRY_NO_PRESERVE(void, jni_DeleteGlobalRef(JNIEnv *env, jobject ref))
  JNIWrapper("DeleteGlobalRef");
D
dcubed 已提交
959
#ifndef USDT2
D
duke 已提交
960
  DTRACE_PROBE2(hotspot_jni, DeleteGlobalRef__entry, env, ref);
D
dcubed 已提交
961 962 963 964
#else /* USDT2 */
  HOTSPOT_JNI_DELETEGLOBALREF_ENTRY(
                                    env, ref);
#endif /* USDT2 */
D
duke 已提交
965
  JNIHandles::destroy_global(ref);
D
dcubed 已提交
966
#ifndef USDT2
D
duke 已提交
967
  DTRACE_PROBE(hotspot_jni, DeleteGlobalRef__return);
D
dcubed 已提交
968 969 970 971
#else /* USDT2 */
  HOTSPOT_JNI_DELETEGLOBALREF_RETURN(
                                     );
#endif /* USDT2 */
D
duke 已提交
972 973 974 975
JNI_END

JNI_QUICK_ENTRY(void, jni_DeleteLocalRef(JNIEnv *env, jobject obj))
  JNIWrapper("DeleteLocalRef");
D
dcubed 已提交
976
#ifndef USDT2
D
duke 已提交
977
  DTRACE_PROBE2(hotspot_jni, DeleteLocalRef__entry, env, obj);
D
dcubed 已提交
978 979 980 981
#else /* USDT2 */
  HOTSPOT_JNI_DELETELOCALREF_ENTRY(
                                   env, obj);
#endif /* USDT2 */
D
duke 已提交
982
  JNIHandles::destroy_local(obj);
D
dcubed 已提交
983
#ifndef USDT2
D
duke 已提交
984
  DTRACE_PROBE(hotspot_jni, DeleteLocalRef__return);
D
dcubed 已提交
985 986 987 988
#else /* USDT2 */
  HOTSPOT_JNI_DELETELOCALREF_RETURN(
                                    );
#endif /* USDT2 */
D
duke 已提交
989 990 991 992
JNI_END

JNI_QUICK_ENTRY(jboolean, jni_IsSameObject(JNIEnv *env, jobject r1, jobject r2))
  JNIWrapper("IsSameObject");
D
dcubed 已提交
993
#ifndef USDT2
D
duke 已提交
994
  DTRACE_PROBE3(hotspot_jni, IsSameObject__entry, env, r1, r2);
D
dcubed 已提交
995 996 997 998
#else /* USDT2 */
  HOTSPOT_JNI_ISSAMEOBJECT_ENTRY(
                                 env, r1, r2);
#endif /* USDT2 */
D
duke 已提交
999 1000 1001
  oop a = JNIHandles::resolve(r1);
  oop b = JNIHandles::resolve(r2);
  jboolean ret = (a == b) ? JNI_TRUE : JNI_FALSE;
D
dcubed 已提交
1002
#ifndef USDT2
D
duke 已提交
1003
  DTRACE_PROBE1(hotspot_jni, IsSameObject__return, ret);
D
dcubed 已提交
1004 1005 1006 1007
#else /* USDT2 */
  HOTSPOT_JNI_ISSAMEOBJECT_RETURN(
                                  ret);
#endif /* USDT2 */
D
duke 已提交
1008 1009 1010 1011 1012 1013
  return ret;
JNI_END


JNI_ENTRY(jobject, jni_NewLocalRef(JNIEnv *env, jobject ref))
  JNIWrapper("NewLocalRef");
D
dcubed 已提交
1014
#ifndef USDT2
D
duke 已提交
1015
  DTRACE_PROBE2(hotspot_jni, NewLocalRef__entry, env, ref);
D
dcubed 已提交
1016 1017 1018 1019
#else /* USDT2 */
  HOTSPOT_JNI_NEWLOCALREF_ENTRY(
                                env, ref);
#endif /* USDT2 */
D
duke 已提交
1020
  jobject ret = JNIHandles::make_local(env, JNIHandles::resolve(ref));
D
dcubed 已提交
1021
#ifndef USDT2
D
duke 已提交
1022
  DTRACE_PROBE1(hotspot_jni, NewLocalRef__return, ret);
D
dcubed 已提交
1023 1024 1025 1026
#else /* USDT2 */
  HOTSPOT_JNI_NEWLOCALREF_RETURN(
                                 ret);
#endif /* USDT2 */
D
duke 已提交
1027 1028 1029 1030 1031
  return ret;
JNI_END

JNI_LEAF(jint, jni_EnsureLocalCapacity(JNIEnv *env, jint capacity))
  JNIWrapper("EnsureLocalCapacity");
D
dcubed 已提交
1032
#ifndef USDT2
D
duke 已提交
1033
  DTRACE_PROBE2(hotspot_jni, EnsureLocalCapacity__entry, env, capacity);
D
dcubed 已提交
1034 1035 1036 1037
#else /* USDT2 */
  HOTSPOT_JNI_ENSURELOCALCAPACITY_ENTRY(
                                        env, capacity);
#endif /* USDT2 */
D
duke 已提交
1038
  jint ret;
1039 1040
  if (capacity >= 0 &&
      ((MaxJNILocalCapacity <= 0) || (capacity <= MaxJNILocalCapacity))) {
D
duke 已提交
1041 1042 1043 1044
    ret = JNI_OK;
  } else {
    ret = JNI_ERR;
  }
D
dcubed 已提交
1045
#ifndef USDT2
D
duke 已提交
1046
  DTRACE_PROBE1(hotspot_jni, EnsureLocalCapacity__return, ret);
D
dcubed 已提交
1047 1048 1049 1050
#else /* USDT2 */
  HOTSPOT_JNI_ENSURELOCALCAPACITY_RETURN(
                                         ret);
#endif /* USDT2 */
D
duke 已提交
1051 1052 1053 1054 1055 1056
  return ret;
JNI_END

// Return the Handle Type
JNI_LEAF(jobjectRefType, jni_GetObjectRefType(JNIEnv *env, jobject obj))
  JNIWrapper("GetObjectRefType");
D
dcubed 已提交
1057
#ifndef USDT2
D
duke 已提交
1058
  DTRACE_PROBE2(hotspot_jni, GetObjectRefType__entry, env, obj);
D
dcubed 已提交
1059 1060 1061 1062
#else /* USDT2 */
  HOTSPOT_JNI_GETOBJECTREFTYPE_ENTRY(
                                     env, obj);
#endif /* USDT2 */
D
duke 已提交
1063 1064 1065 1066 1067 1068 1069 1070 1071 1072
  jobjectRefType ret;
  if (JNIHandles::is_local_handle(thread, obj) ||
      JNIHandles::is_frame_handle(thread, obj))
    ret = JNILocalRefType;
  else if (JNIHandles::is_global_handle(obj))
    ret = JNIGlobalRefType;
  else if (JNIHandles::is_weak_global_handle(obj))
    ret = JNIWeakGlobalRefType;
  else
    ret = JNIInvalidRefType;
D
dcubed 已提交
1073
#ifndef USDT2
D
duke 已提交
1074
  DTRACE_PROBE1(hotspot_jni, GetObjectRefType__return, ret);
D
dcubed 已提交
1075 1076 1077 1078
#else /* USDT2 */
  HOTSPOT_JNI_GETOBJECTREFTYPE_RETURN(
                                      (void *) ret);
#endif /* USDT2 */
D
duke 已提交
1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096
  return ret;
JNI_END


class JNI_ArgumentPusher : public SignatureIterator {
 protected:
  JavaCallArguments*  _arguments;

  virtual void get_bool   () = 0;
  virtual void get_char   () = 0;
  virtual void get_short  () = 0;
  virtual void get_byte   () = 0;
  virtual void get_int    () = 0;
  virtual void get_long   () = 0;
  virtual void get_float  () = 0;
  virtual void get_double () = 0;
  virtual void get_object () = 0;

1097
  JNI_ArgumentPusher(Symbol* signature) : SignatureIterator(signature) {
D
duke 已提交
1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127
    this->_return_type = T_ILLEGAL;
    _arguments = NULL;
  }

 public:
  virtual void iterate( uint64_t fingerprint ) = 0;

  void set_java_argument_object(JavaCallArguments *arguments) { _arguments = arguments; }

  inline void do_bool()                     { if (!is_return_type()) get_bool();   }
  inline void do_char()                     { if (!is_return_type()) get_char();   }
  inline void do_short()                    { if (!is_return_type()) get_short();  }
  inline void do_byte()                     { if (!is_return_type()) get_byte();   }
  inline void do_int()                      { if (!is_return_type()) get_int();    }
  inline void do_long()                     { if (!is_return_type()) get_long();   }
  inline void do_float()                    { if (!is_return_type()) get_float();  }
  inline void do_double()                   { if (!is_return_type()) get_double(); }
  inline void do_object(int begin, int end) { if (!is_return_type()) get_object(); }
  inline void do_array(int begin, int end)  { if (!is_return_type()) get_object(); } // do_array uses get_object -- there is no get_array
  inline void do_void()                     { }

  JavaCallArguments* arguments()     { return _arguments; }
  void push_receiver(Handle h)       { _arguments->push_oop(h); }
};


class JNI_ArgumentPusherVaArg : public JNI_ArgumentPusher {
 protected:
  va_list _ap;

1128 1129 1130 1131 1132 1133 1134 1135
  inline void get_bool()   {
    // Normalize boolean arguments from native code by converting 1-255 to JNI_TRUE and
    // 0 to JNI_FALSE.  Boolean return values from native are normalized the same in
    // TemplateInterpreterGenerator::generate_result_handler_for and
    // SharedRuntime::generate_native_wrapper.
    jboolean b = va_arg(_ap, jint);
    _arguments->push_int((jint)(b == 0 ? JNI_FALSE : JNI_TRUE));
  }
D
duke 已提交
1136 1137 1138 1139 1140 1141 1142 1143 1144 1145
  inline void get_char()   { _arguments->push_int(va_arg(_ap, jint)); } // char is coerced to int when using va_arg
  inline void get_short()  { _arguments->push_int(va_arg(_ap, jint)); } // short is coerced to int when using va_arg
  inline void get_byte()   { _arguments->push_int(va_arg(_ap, jint)); } // byte is coerced to int when using va_arg
  inline void get_int()    { _arguments->push_int(va_arg(_ap, jint)); }

  // each of these paths is exercized by the various jck Call[Static,Nonvirtual,][Void,Int,..]Method[A,V,] tests

  inline void get_long()   { _arguments->push_long(va_arg(_ap, jlong)); }
  inline void get_float()  { _arguments->push_float((jfloat)va_arg(_ap, jdouble)); } // float is coerced to double w/ va_arg
  inline void get_double() { _arguments->push_double(va_arg(_ap, jdouble)); }
1146
  inline void get_object() { _arguments->push_jobject(va_arg(_ap, jobject)); }
D
duke 已提交
1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158

  inline void set_ap(va_list rap) {
#ifdef va_copy
    va_copy(_ap, rap);
#elif defined (__va_copy)
    __va_copy(_ap, rap);
#else
    _ap = rap;
#endif
  }

 public:
1159 1160
  JNI_ArgumentPusherVaArg(Symbol* signature, va_list rap)
       : JNI_ArgumentPusher(signature) {
D
duke 已提交
1161 1162
    set_ap(rap);
  }
1163
  JNI_ArgumentPusherVaArg(jmethodID method_id, va_list rap)
1164
      : JNI_ArgumentPusher(Method::resolve_jmethod_id(method_id)->signature()) {
D
duke 已提交
1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179
    set_ap(rap);
  }

  // Optimized path if we have the bitvector form of signature
  void iterate( uint64_t fingerprint ) {
    if ( fingerprint == UCONST64(-1) ) SignatureIterator::iterate();// Must be too many arguments
    else {
      _return_type = (BasicType)((fingerprint >> static_feature_size) &
                                  result_feature_mask);

      assert(fingerprint, "Fingerprint should not be 0");
      fingerprint = fingerprint >> (static_feature_size + result_feature_size);
      while ( 1 ) {
        switch ( fingerprint & parameter_feature_mask ) {
          case bool_parm:
1180 1181
            get_bool();
            break;
D
duke 已提交
1182
          case char_parm:
1183 1184
            get_char();
            break;
D
duke 已提交
1185
          case short_parm:
1186 1187
            get_short();
            break;
D
duke 已提交
1188
          case byte_parm:
1189 1190
            get_byte();
            break;
D
duke 已提交
1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223
          case int_parm:
            get_int();
            break;
          case obj_parm:
            get_object();
            break;
          case long_parm:
            get_long();
            break;
          case float_parm:
            get_float();
            break;
          case double_parm:
            get_double();
            break;
          case done_parm:
            return;
            break;
          default:
            ShouldNotReachHere();
            break;
        }
        fingerprint >>= parameter_feature_size;
      }
    }
  }
};


class JNI_ArgumentPusherArray : public JNI_ArgumentPusher {
 protected:
  const jvalue *_ap;

1224 1225 1226 1227 1228 1229 1230 1231
  inline void get_bool()   {
    // Normalize boolean arguments from native code by converting 1-255 to JNI_TRUE and
    // 0 to JNI_FALSE.  Boolean return values from native are normalized the same in
    // TemplateInterpreterGenerator::generate_result_handler_for and
    // SharedRuntime::generate_native_wrapper.
    jboolean b = (_ap++)->z;
    _arguments->push_int((jint)(b == 0 ? JNI_FALSE : JNI_TRUE));
  }
D
duke 已提交
1232 1233 1234 1235 1236 1237 1238 1239
  inline void get_char()   { _arguments->push_int((jint)(_ap++)->c); }
  inline void get_short()  { _arguments->push_int((jint)(_ap++)->s); }
  inline void get_byte()   { _arguments->push_int((jint)(_ap++)->b); }
  inline void get_int()    { _arguments->push_int((jint)(_ap++)->i); }

  inline void get_long()   { _arguments->push_long((_ap++)->j);  }
  inline void get_float()  { _arguments->push_float((_ap++)->f); }
  inline void get_double() { _arguments->push_double((_ap++)->d);}
1240
  inline void get_object() { _arguments->push_jobject((_ap++)->l); }
D
duke 已提交
1241 1242 1243 1244

  inline void set_ap(const jvalue *rap) { _ap = rap; }

 public:
1245 1246
  JNI_ArgumentPusherArray(Symbol* signature, const jvalue *rap)
       : JNI_ArgumentPusher(signature) {
D
duke 已提交
1247 1248
    set_ap(rap);
  }
1249
  JNI_ArgumentPusherArray(jmethodID method_id, const jvalue *rap)
1250
      : JNI_ArgumentPusher(Method::resolve_jmethod_id(method_id)->signature()) {
D
duke 已提交
1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313
    set_ap(rap);
  }

  // Optimized path if we have the bitvector form of signature
  void iterate( uint64_t fingerprint ) {
    if ( fingerprint == UCONST64(-1) ) SignatureIterator::iterate(); // Must be too many arguments
    else {
      _return_type = (BasicType)((fingerprint >> static_feature_size) &
                                  result_feature_mask);
      assert(fingerprint, "Fingerprint should not be 0");
      fingerprint = fingerprint >> (static_feature_size + result_feature_size);
      while ( 1 ) {
        switch ( fingerprint & parameter_feature_mask ) {
          case bool_parm:
            get_bool();
            break;
          case char_parm:
            get_char();
            break;
          case short_parm:
            get_short();
            break;
          case byte_parm:
            get_byte();
            break;
          case int_parm:
            get_int();
            break;
          case obj_parm:
            get_object();
            break;
          case long_parm:
            get_long();
            break;
          case float_parm:
            get_float();
            break;
          case double_parm:
            get_double();
            break;
          case done_parm:
            return;
            break;
          default:
            ShouldNotReachHere();
            break;
        }
        fingerprint >>= parameter_feature_size;
      }
    }
  }
};


enum JNICallType {
  JNI_STATIC,
  JNI_VIRTUAL,
  JNI_NONVIRTUAL
};



static void jni_invoke_static(JNIEnv *env, JavaValue* result, jobject receiver, JNICallType call_type, jmethodID method_id, JNI_ArgumentPusher *args, TRAPS) {
1314
  methodHandle method(THREAD, Method::resolve_jmethod_id(method_id));
D
duke 已提交
1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325

  // Create object to hold arguments for the JavaCall, and associate it with
  // the jni parser
  ResourceMark rm(THREAD);
  int number_of_parameters = method->size_of_parameters();
  JavaCallArguments java_args(number_of_parameters);
  args->set_java_argument_object(&java_args);

  assert(method->is_static(), "method should be static");

  // Fill out JavaCallArguments object
1326
  args->iterate( Fingerprinter(method).fingerprint() );
D
duke 已提交
1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347
  // Initialize result type
  result->set_type(args->get_ret_type());

  // Invoke the method. Result is returned as oop.
  JavaCalls::call(result, method, &java_args, CHECK);

  // Convert result
  if (result->get_type() == T_OBJECT || result->get_type() == T_ARRAY) {
    result->set_jobject(JNIHandles::make_local(env, (oop) result->get_jobject()));
  }
}


static void jni_invoke_nonstatic(JNIEnv *env, JavaValue* result, jobject receiver, JNICallType call_type, jmethodID method_id, JNI_ArgumentPusher *args, TRAPS) {
  oop recv = JNIHandles::resolve(receiver);
  if (recv == NULL) {
    THROW(vmSymbols::java_lang_NullPointerException());
  }
  Handle h_recv(THREAD, recv);

  int number_of_parameters;
1348
  Method* selected_method;
D
duke 已提交
1349
  {
1350
    Method* m = Method::resolve_jmethod_id(method_id);
D
duke 已提交
1351
    number_of_parameters = m->size_of_parameters();
1352
    Klass* holder = m->method_holder();
1353 1354 1355
    if (call_type != JNI_VIRTUAL) {
        selected_method = m;
    } else if (!m->has_itable_index()) {
D
duke 已提交
1356 1357
      // non-interface call -- for that little speed boost, don't handlize
      debug_only(No_Safepoint_Verifier nosafepoint;)
1358 1359 1360 1361 1362 1363 1364 1365 1366 1367
      // jni_GetMethodID makes sure class is linked and initialized
      // so m should have a valid vtable index.
      assert(m->valid_vtable_index(), "no valid vtable index");
      int vtbl_index = m->vtable_index();
      if (vtbl_index != Method::nonvirtual_vtable_index) {
        Klass* k = h_recv->klass();
        // k might be an arrayKlassOop but all vtables start at
        // the same place. The cast is to avoid virtual call and assertion.
        InstanceKlass *ik = (InstanceKlass*)k;
        selected_method = ik->method_at_vtable(vtbl_index);
D
duke 已提交
1368
      } else {
1369
        // final method
D
duke 已提交
1370 1371 1372 1373 1374 1375
        selected_method = m;
      }
    } else {
      // interface call
      KlassHandle h_holder(THREAD, holder);

1376 1377 1378
      int itbl_index = m->itable_index();
      Klass* k = h_recv->klass();
      selected_method = InstanceKlass::cast(k)->method_at_itable(h_holder(), itbl_index, CHECK);
D
duke 已提交
1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394
    }
  }

  methodHandle method(THREAD, selected_method);

  // Create object to hold arguments for the JavaCall, and associate it with
  // the jni parser
  ResourceMark rm(THREAD);
  JavaCallArguments java_args(number_of_parameters);
  args->set_java_argument_object(&java_args);

  // handle arguments
  assert(!method->is_static(), "method should not be static");
  args->push_receiver(h_recv); // Push jobject handle

  // Fill out JavaCallArguments object
1395
  args->iterate( Fingerprinter(method).fingerprint() );
D
duke 已提交
1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409
  // Initialize result type
  result->set_type(args->get_ret_type());

  // Invoke the method. Result is returned as oop.
  JavaCalls::call(result, method, &java_args, CHECK);

  // Convert result
  if (result->get_type() == T_OBJECT || result->get_type() == T_ARRAY) {
    result->set_jobject(JNIHandles::make_local(env, (oop) result->get_jobject()));
  }
}


static instanceOop alloc_object(jclass clazz, TRAPS) {
1410
  KlassHandle k(THREAD, java_lang_Class::as_Klass(JNIHandles::resolve_non_null(clazz)));
H
hseigel 已提交
1411
  k()->check_valid_for_instantiation(false, CHECK_NULL);
1412 1413
  InstanceKlass::cast(k())->initialize(CHECK_NULL);
  instanceOop ih = InstanceKlass::cast(k())->allocate_instance(THREAD);
D
duke 已提交
1414 1415 1416
  return ih;
}

D
dcubed 已提交
1417
#ifndef USDT2
D
duke 已提交
1418
DT_RETURN_MARK_DECL(AllocObject, jobject);
D
dcubed 已提交
1419 1420 1421 1422
#else /* USDT2 */
DT_RETURN_MARK_DECL(AllocObject, jobject
                    , HOTSPOT_JNI_ALLOCOBJECT_RETURN(_ret_ref));
#endif /* USDT2 */
D
duke 已提交
1423 1424 1425 1426

JNI_ENTRY(jobject, jni_AllocObject(JNIEnv *env, jclass clazz))
  JNIWrapper("AllocObject");

D
dcubed 已提交
1427
#ifndef USDT2
D
duke 已提交
1428
  DTRACE_PROBE2(hotspot_jni, AllocObject__entry, env, clazz);
D
dcubed 已提交
1429 1430 1431 1432
#else /* USDT2 */
  HOTSPOT_JNI_ALLOCOBJECT_ENTRY(
                                env, clazz);
#endif /* USDT2 */
D
duke 已提交
1433 1434 1435 1436 1437 1438 1439 1440
  jobject ret = NULL;
  DT_RETURN_MARK(AllocObject, jobject, (const jobject&)ret);

  instanceOop i = alloc_object(clazz, CHECK_NULL);
  ret = JNIHandles::make_local(env, i);
  return ret;
JNI_END

D
dcubed 已提交
1441
#ifndef USDT2
D
duke 已提交
1442
DT_RETURN_MARK_DECL(NewObjectA, jobject);
D
dcubed 已提交
1443 1444 1445 1446
#else /* USDT2 */
DT_RETURN_MARK_DECL(NewObjectA, jobject
                    , HOTSPOT_JNI_NEWOBJECTA_RETURN(_ret_ref));
#endif /* USDT2 */
D
duke 已提交
1447 1448 1449

JNI_ENTRY(jobject, jni_NewObjectA(JNIEnv *env, jclass clazz, jmethodID methodID, const jvalue *args))
  JNIWrapper("NewObjectA");
D
dcubed 已提交
1450
#ifndef USDT2
D
duke 已提交
1451
  DTRACE_PROBE3(hotspot_jni, NewObjectA__entry, env, clazz, methodID);
D
dcubed 已提交
1452 1453 1454 1455
#else /* USDT2 */
  HOTSPOT_JNI_NEWOBJECTA_ENTRY(
                               env, clazz, (uintptr_t) methodID);
#endif /* USDT2 */
D
duke 已提交
1456 1457 1458 1459 1460 1461
  jobject obj = NULL;
  DT_RETURN_MARK(NewObjectA, jobject, (const jobject)obj);

  instanceOop i = alloc_object(clazz, CHECK_NULL);
  obj = JNIHandles::make_local(env, i);
  JavaValue jvalue(T_VOID);
1462
  JNI_ArgumentPusherArray ap(methodID, args);
D
duke 已提交
1463 1464 1465 1466
  jni_invoke_nonstatic(env, &jvalue, obj, JNI_NONVIRTUAL, methodID, &ap, CHECK_NULL);
  return obj;
JNI_END

D
dcubed 已提交
1467
#ifndef USDT2
D
duke 已提交
1468
DT_RETURN_MARK_DECL(NewObjectV, jobject);
D
dcubed 已提交
1469 1470 1471 1472
#else /* USDT2 */
DT_RETURN_MARK_DECL(NewObjectV, jobject
                    , HOTSPOT_JNI_NEWOBJECTV_RETURN(_ret_ref));
#endif /* USDT2 */
D
duke 已提交
1473 1474 1475

JNI_ENTRY(jobject, jni_NewObjectV(JNIEnv *env, jclass clazz, jmethodID methodID, va_list args))
  JNIWrapper("NewObjectV");
D
dcubed 已提交
1476
#ifndef USDT2
D
duke 已提交
1477
  DTRACE_PROBE3(hotspot_jni, NewObjectV__entry, env, clazz, methodID);
D
dcubed 已提交
1478 1479 1480 1481
#else /* USDT2 */
  HOTSPOT_JNI_NEWOBJECTV_ENTRY(
                               env, clazz, (uintptr_t) methodID);
#endif /* USDT2 */
D
duke 已提交
1482 1483 1484 1485 1486 1487
  jobject obj = NULL;
  DT_RETURN_MARK(NewObjectV, jobject, (const jobject&)obj);

  instanceOop i = alloc_object(clazz, CHECK_NULL);
  obj = JNIHandles::make_local(env, i);
  JavaValue jvalue(T_VOID);
1488
  JNI_ArgumentPusherVaArg ap(methodID, args);
D
duke 已提交
1489 1490 1491 1492
  jni_invoke_nonstatic(env, &jvalue, obj, JNI_NONVIRTUAL, methodID, &ap, CHECK_NULL);
  return obj;
JNI_END

D
dcubed 已提交
1493
#ifndef USDT2
D
duke 已提交
1494
DT_RETURN_MARK_DECL(NewObject, jobject);
D
dcubed 已提交
1495 1496 1497 1498
#else /* USDT2 */
DT_RETURN_MARK_DECL(NewObject, jobject
                    , HOTSPOT_JNI_NEWOBJECT_RETURN(_ret_ref));
#endif /* USDT2 */
D
duke 已提交
1499 1500 1501

JNI_ENTRY(jobject, jni_NewObject(JNIEnv *env, jclass clazz, jmethodID methodID, ...))
  JNIWrapper("NewObject");
D
dcubed 已提交
1502
#ifndef USDT2
D
duke 已提交
1503
  DTRACE_PROBE3(hotspot_jni, NewObject__entry, env, clazz, methodID);
D
dcubed 已提交
1504 1505 1506 1507
#else /* USDT2 */
  HOTSPOT_JNI_NEWOBJECT_ENTRY(
                              env, clazz, (uintptr_t) methodID);
#endif /* USDT2 */
D
duke 已提交
1508 1509 1510 1511 1512 1513 1514 1515
  jobject obj = NULL;
  DT_RETURN_MARK(NewObject, jobject, (const jobject&)obj);

  instanceOop i = alloc_object(clazz, CHECK_NULL);
  obj = JNIHandles::make_local(env, i);
  va_list args;
  va_start(args, methodID);
  JavaValue jvalue(T_VOID);
1516
  JNI_ArgumentPusherVaArg ap(methodID, args);
D
duke 已提交
1517 1518 1519 1520 1521 1522 1523 1524
  jni_invoke_nonstatic(env, &jvalue, obj, JNI_NONVIRTUAL, methodID, &ap, CHECK_NULL);
  va_end(args);
  return obj;
JNI_END


JNI_ENTRY(jclass, jni_GetObjectClass(JNIEnv *env, jobject obj))
  JNIWrapper("GetObjectClass");
D
dcubed 已提交
1525
#ifndef USDT2
D
duke 已提交
1526
  DTRACE_PROBE2(hotspot_jni, GetObjectClass__entry, env, obj);
D
dcubed 已提交
1527 1528 1529 1530
#else /* USDT2 */
  HOTSPOT_JNI_GETOBJECTCLASS_ENTRY(
                                   env, obj);
#endif /* USDT2 */
1531
  Klass* k = JNIHandles::resolve_non_null(obj)->klass();
D
duke 已提交
1532
  jclass ret =
H
hseigel 已提交
1533
    (jclass) JNIHandles::make_local(env, k->java_mirror());
D
dcubed 已提交
1534
#ifndef USDT2
D
duke 已提交
1535
  DTRACE_PROBE1(hotspot_jni, GetObjectClass__return, ret);
D
dcubed 已提交
1536 1537 1538 1539
#else /* USDT2 */
  HOTSPOT_JNI_GETOBJECTCLASS_RETURN(
                                    ret);
#endif /* USDT2 */
D
duke 已提交
1540 1541 1542 1543 1544
  return ret;
JNI_END

JNI_QUICK_ENTRY(jboolean, jni_IsInstanceOf(JNIEnv *env, jobject obj, jclass clazz))
  JNIWrapper("IsInstanceOf");
D
dcubed 已提交
1545
#ifndef USDT2
D
duke 已提交
1546
  DTRACE_PROBE3(hotspot_jni, IsInstanceOf__entry, env, obj, clazz);
D
dcubed 已提交
1547 1548 1549 1550
#else /* USDT2 */
  HOTSPOT_JNI_ISINSTANCEOF_ENTRY(
                                 env, obj, clazz);
#endif /* USDT2 */
D
duke 已提交
1551 1552 1553
  jboolean ret = JNI_TRUE;
  if (obj != NULL) {
    ret = JNI_FALSE;
1554
    Klass* k = java_lang_Class::as_Klass(
D
duke 已提交
1555 1556 1557 1558 1559
      JNIHandles::resolve_non_null(clazz));
    if (k != NULL) {
      ret = JNIHandles::resolve_non_null(obj)->is_a(k) ? JNI_TRUE : JNI_FALSE;
    }
  }
D
dcubed 已提交
1560
#ifndef USDT2
D
duke 已提交
1561
  DTRACE_PROBE1(hotspot_jni, IsInstanceOf__return, ret);
D
dcubed 已提交
1562 1563 1564 1565
#else /* USDT2 */
  HOTSPOT_JNI_ISINSTANCEOF_RETURN(
                                  ret);
#endif /* USDT2 */
D
duke 已提交
1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576
  return ret;
JNI_END


static jmethodID get_method_id(JNIEnv *env, jclass clazz, const char *name_str,
                               const char *sig, bool is_static, TRAPS) {
  // %%%% This code should probably just call into a method in the LinkResolver
  //
  // The class should have been loaded (we have an instance of the class
  // passed in) so the method and signature should already be in the symbol
  // table.  If they're not there, the method doesn't exist.
1577 1578 1579 1580 1581 1582 1583
  const char *name_to_probe = (name_str == NULL)
                        ? vmSymbols::object_initializer_name()->as_C_string()
                        : name_str;
  TempNewSymbol name = SymbolTable::probe(name_to_probe, (int)strlen(name_to_probe));
  TempNewSymbol signature = SymbolTable::probe(sig, (int)strlen(sig));

  if (name == NULL || signature == NULL) {
D
duke 已提交
1584 1585 1586 1587 1588 1589 1590 1591 1592 1593
    THROW_MSG_0(vmSymbols::java_lang_NoSuchMethodError(), name_str);
  }

  // Throw a NoSuchMethodError exception if we have an instance of a
  // primitive java.lang.Class
  if (java_lang_Class::is_primitive(JNIHandles::resolve_non_null(clazz))) {
    THROW_MSG_0(vmSymbols::java_lang_NoSuchMethodError(), name_str);
  }

  KlassHandle klass(THREAD,
1594
               java_lang_Class::as_Klass(JNIHandles::resolve_non_null(clazz)));
D
duke 已提交
1595 1596

  // Make sure class is linked and initialized before handing id's out to
1597
  // Method*s.
H
hseigel 已提交
1598
  klass()->initialize(CHECK_NULL);
D
duke 已提交
1599

1600
  Method* m;
1601 1602
  if (name == vmSymbols::object_initializer_name() ||
      name == vmSymbols::class_initializer_name()) {
D
duke 已提交
1603 1604
    // Never search superclasses for constructors
    if (klass->oop_is_instance()) {
1605
      m = InstanceKlass::cast(klass())->find_method(name, signature);
D
duke 已提交
1606 1607 1608 1609
    } else {
      m = NULL;
    }
  } else {
1610
    m = klass->lookup_method(name, signature);
1611 1612
    if (m == NULL &&  klass->oop_is_instance()) {
      m = InstanceKlass::cast(klass())->lookup_method_in_ordered_interfaces(name, signature);
D
duke 已提交
1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624
    }
  }
  if (m == NULL || (m->is_static() != is_static)) {
    THROW_MSG_0(vmSymbols::java_lang_NoSuchMethodError(), name_str);
  }
  return m->jmethod_id();
}


JNI_ENTRY(jmethodID, jni_GetMethodID(JNIEnv *env, jclass clazz,
          const char *name, const char *sig))
  JNIWrapper("GetMethodID");
D
dcubed 已提交
1625
#ifndef USDT2
D
duke 已提交
1626
  DTRACE_PROBE4(hotspot_jni, GetMethodID__entry, env, clazz, name, sig);
D
dcubed 已提交
1627 1628 1629 1630
#else /* USDT2 */
  HOTSPOT_JNI_GETMETHODID_ENTRY(
                                env, clazz, (char *) name, (char *) sig);
#endif /* USDT2 */
D
duke 已提交
1631
  jmethodID ret = get_method_id(env, clazz, name, sig, false, thread);
D
dcubed 已提交
1632
#ifndef USDT2
D
duke 已提交
1633
  DTRACE_PROBE1(hotspot_jni, GetMethodID__return, ret);
D
dcubed 已提交
1634 1635 1636 1637
#else /* USDT2 */
  HOTSPOT_JNI_GETMETHODID_RETURN(
                                 (uintptr_t) ret);
#endif /* USDT2 */
D
duke 已提交
1638 1639 1640 1641 1642 1643 1644
  return ret;
JNI_END


JNI_ENTRY(jmethodID, jni_GetStaticMethodID(JNIEnv *env, jclass clazz,
          const char *name, const char *sig))
  JNIWrapper("GetStaticMethodID");
D
dcubed 已提交
1645
#ifndef USDT2
D
duke 已提交
1646
  DTRACE_PROBE4(hotspot_jni, GetStaticMethodID__entry, env, clazz, name, sig);
D
dcubed 已提交
1647 1648 1649 1650
#else /* USDT2 */
  HOTSPOT_JNI_GETSTATICMETHODID_ENTRY(
                                      env, (char *) clazz, (char *) name, (char *)sig);
#endif /* USDT2 */
D
duke 已提交
1651
  jmethodID ret = get_method_id(env, clazz, name, sig, true, thread);
D
dcubed 已提交
1652
#ifndef USDT2
D
duke 已提交
1653
  DTRACE_PROBE1(hotspot_jni, GetStaticMethodID__return, ret);
D
dcubed 已提交
1654 1655 1656 1657
#else /* USDT2 */
  HOTSPOT_JNI_GETSTATICMETHODID_RETURN(
                                       (uintptr_t) ret);
#endif /* USDT2 */
D
duke 已提交
1658 1659 1660 1661 1662 1663 1664 1665 1666
  return ret;
JNI_END



//
// Calling Methods
//

D
dcubed 已提交
1667
#ifndef USDT2
D
duke 已提交
1668 1669 1670 1671 1672 1673 1674 1675 1676 1677
#define DEFINE_CALLMETHOD(ResultType, Result, Tag) \
\
  DT_RETURN_MARK_DECL_FOR(Result, Call##Result##Method, ResultType);\
  DT_RETURN_MARK_DECL_FOR(Result, Call##Result##MethodV, ResultType);\
  DT_RETURN_MARK_DECL_FOR(Result, Call##Result##MethodA, ResultType);\
\
JNI_ENTRY(ResultType, \
          jni_Call##Result##Method(JNIEnv *env, jobject obj, jmethodID methodID, ...)) \
  JNIWrapper("Call" XSTR(Result) "Method"); \
\
D
dcubed 已提交
1678
  DTRACE_PROBE3(hotspot_jni, Call##Result##Method__entry, env, obj, methodID); \
D
duke 已提交
1679 1680 1681 1682 1683 1684 1685
  ResultType ret = 0;\
  DT_RETURN_MARK_FOR(Result, Call##Result##Method, ResultType, \
                     (const ResultType&)ret);\
\
  va_list args; \
  va_start(args, methodID); \
  JavaValue jvalue(Tag); \
1686
  JNI_ArgumentPusherVaArg ap(methodID, args); \
D
duke 已提交
1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697
  jni_invoke_nonstatic(env, &jvalue, obj, JNI_VIRTUAL, methodID, &ap, CHECK_0); \
  va_end(args); \
  ret = jvalue.get_##ResultType(); \
  return ret;\
JNI_END \
\
\
JNI_ENTRY(ResultType, \
          jni_Call##Result##MethodV(JNIEnv *env, jobject obj, jmethodID methodID, va_list args)) \
  JNIWrapper("Call" XSTR(Result) "MethodV"); \
\
D
dcubed 已提交
1698
  DTRACE_PROBE3(hotspot_jni, Call##Result##MethodV__entry, env, obj, methodID); \
D
duke 已提交
1699 1700 1701 1702 1703
  ResultType ret = 0;\
  DT_RETURN_MARK_FOR(Result, Call##Result##MethodV, ResultType, \
                     (const ResultType&)ret);\
\
  JavaValue jvalue(Tag); \
1704
  JNI_ArgumentPusherVaArg ap(methodID, args); \
D
duke 已提交
1705 1706 1707 1708 1709 1710 1711 1712 1713
  jni_invoke_nonstatic(env, &jvalue, obj, JNI_VIRTUAL, methodID, &ap, CHECK_0); \
  ret = jvalue.get_##ResultType(); \
  return ret;\
JNI_END \
\
\
JNI_ENTRY(ResultType, \
          jni_Call##Result##MethodA(JNIEnv *env, jobject obj, jmethodID methodID, const jvalue *args)) \
  JNIWrapper("Call" XSTR(Result) "MethodA"); \
D
dcubed 已提交
1714
  DTRACE_PROBE3(hotspot_jni, Call##Result##MethodA__entry, env, obj, methodID); \
D
duke 已提交
1715 1716 1717 1718 1719
  ResultType ret = 0;\
  DT_RETURN_MARK_FOR(Result, Call##Result##MethodA, ResultType, \
                     (const ResultType&)ret);\
\
  JavaValue jvalue(Tag); \
1720
  JNI_ArgumentPusherArray ap(methodID, args); \
D
duke 已提交
1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741
  jni_invoke_nonstatic(env, &jvalue, obj, JNI_VIRTUAL, methodID, &ap, CHECK_0); \
  ret = jvalue.get_##ResultType(); \
  return ret;\
JNI_END

// the runtime type of subword integral basic types is integer
DEFINE_CALLMETHOD(jboolean, Boolean, T_BOOLEAN)
DEFINE_CALLMETHOD(jbyte,    Byte,    T_BYTE)
DEFINE_CALLMETHOD(jchar,    Char,    T_CHAR)
DEFINE_CALLMETHOD(jshort,   Short,   T_SHORT)

DEFINE_CALLMETHOD(jobject,  Object,  T_OBJECT)
DEFINE_CALLMETHOD(jint,     Int,     T_INT)
DEFINE_CALLMETHOD(jlong,    Long,    T_LONG)
DEFINE_CALLMETHOD(jfloat,   Float,   T_FLOAT)
DEFINE_CALLMETHOD(jdouble,  Double,  T_DOUBLE)

DT_VOID_RETURN_MARK_DECL(CallVoidMethod);
DT_VOID_RETURN_MARK_DECL(CallVoidMethodV);
DT_VOID_RETURN_MARK_DECL(CallVoidMethodA);

D
dcubed 已提交
1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823
#else /* USDT2 */

#define DEFINE_CALLMETHOD(ResultType, Result, Tag \
                          , EntryProbe, ReturnProbe)    \
\
  DT_RETURN_MARK_DECL_FOR(Result, Call##Result##Method, ResultType \
                          , ReturnProbe);                          \
\
JNI_ENTRY(ResultType, \
          jni_Call##Result##Method(JNIEnv *env, jobject obj, jmethodID methodID, ...)) \
  JNIWrapper("Call" XSTR(Result) "Method"); \
\
  EntryProbe; \
  ResultType ret = 0;\
  DT_RETURN_MARK_FOR(Result, Call##Result##Method, ResultType, \
                     (const ResultType&)ret);\
\
  va_list args; \
  va_start(args, methodID); \
  JavaValue jvalue(Tag); \
  JNI_ArgumentPusherVaArg ap(methodID, args); \
  jni_invoke_nonstatic(env, &jvalue, obj, JNI_VIRTUAL, methodID, &ap, CHECK_0); \
  va_end(args); \
  ret = jvalue.get_##ResultType(); \
  return ret;\
JNI_END

// the runtime type of subword integral basic types is integer
DEFINE_CALLMETHOD(jboolean, Boolean, T_BOOLEAN
                  , HOTSPOT_JNI_CALLBOOLEANMETHOD_ENTRY(env, obj, (uintptr_t)methodID),
                  HOTSPOT_JNI_CALLBOOLEANMETHOD_RETURN(_ret_ref))
DEFINE_CALLMETHOD(jbyte,    Byte,    T_BYTE
                  , HOTSPOT_JNI_CALLBYTEMETHOD_ENTRY(env, obj, (uintptr_t)methodID),
                  HOTSPOT_JNI_CALLBYTEMETHOD_RETURN(_ret_ref))
DEFINE_CALLMETHOD(jchar,    Char,    T_CHAR
                  , HOTSPOT_JNI_CALLCHARMETHOD_ENTRY(env, obj, (uintptr_t)methodID),
                  HOTSPOT_JNI_CALLCHARMETHOD_RETURN(_ret_ref))
DEFINE_CALLMETHOD(jshort,   Short,   T_SHORT
                  , HOTSPOT_JNI_CALLSHORTMETHOD_ENTRY(env, obj, (uintptr_t)methodID),
                  HOTSPOT_JNI_CALLSHORTMETHOD_RETURN(_ret_ref))

DEFINE_CALLMETHOD(jobject,  Object,  T_OBJECT
                  , HOTSPOT_JNI_CALLOBJECTMETHOD_ENTRY(env, obj, (uintptr_t)methodID),
                  HOTSPOT_JNI_CALLOBJECTMETHOD_RETURN(_ret_ref))
DEFINE_CALLMETHOD(jint,     Int,     T_INT,
                  HOTSPOT_JNI_CALLINTMETHOD_ENTRY(env, obj, (uintptr_t)methodID),
                  HOTSPOT_JNI_CALLINTMETHOD_RETURN(_ret_ref))
DEFINE_CALLMETHOD(jlong,    Long,    T_LONG
                  , HOTSPOT_JNI_CALLLONGMETHOD_ENTRY(env, obj, (uintptr_t)methodID),
                  HOTSPOT_JNI_CALLLONGMETHOD_RETURN(_ret_ref))
// Float and double probes don't return value because dtrace doesn't currently support it
DEFINE_CALLMETHOD(jfloat,   Float,   T_FLOAT
                  , HOTSPOT_JNI_CALLFLOATMETHOD_ENTRY(env, obj, (uintptr_t)methodID),
                  HOTSPOT_JNI_CALLFLOATMETHOD_RETURN())
DEFINE_CALLMETHOD(jdouble,  Double,  T_DOUBLE
                  , HOTSPOT_JNI_CALLDOUBLEMETHOD_ENTRY(env, obj, (uintptr_t)methodID),
                  HOTSPOT_JNI_CALLDOUBLEMETHOD_RETURN())

#define DEFINE_CALLMETHODV(ResultType, Result, Tag \
                          , EntryProbe, ReturnProbe)    \
\
  DT_RETURN_MARK_DECL_FOR(Result, Call##Result##MethodV, ResultType \
                          , ReturnProbe);                          \
\
JNI_ENTRY(ResultType, \
          jni_Call##Result##MethodV(JNIEnv *env, jobject obj, jmethodID methodID, va_list args)) \
  JNIWrapper("Call" XSTR(Result) "MethodV"); \
\
  EntryProbe;\
  ResultType ret = 0;\
  DT_RETURN_MARK_FOR(Result, Call##Result##MethodV, ResultType, \
                     (const ResultType&)ret);\
\
  JavaValue jvalue(Tag); \
  JNI_ArgumentPusherVaArg ap(methodID, args); \
  jni_invoke_nonstatic(env, &jvalue, obj, JNI_VIRTUAL, methodID, &ap, CHECK_0); \
  ret = jvalue.get_##ResultType(); \
  return ret;\
JNI_END

// the runtime type of subword integral basic types is integer
DEFINE_CALLMETHODV(jboolean, Boolean, T_BOOLEAN
1824 1825
                  , HOTSPOT_JNI_CALLBOOLEANMETHODV_ENTRY(env, obj, (uintptr_t)methodID),
                  HOTSPOT_JNI_CALLBOOLEANMETHODV_RETURN(_ret_ref))
D
dcubed 已提交
1826
DEFINE_CALLMETHODV(jbyte,    Byte,    T_BYTE
1827 1828
                  , HOTSPOT_JNI_CALLBYTEMETHODV_ENTRY(env, obj, (uintptr_t)methodID),
                  HOTSPOT_JNI_CALLBYTEMETHODV_RETURN(_ret_ref))
D
dcubed 已提交
1829
DEFINE_CALLMETHODV(jchar,    Char,    T_CHAR
1830 1831
                  , HOTSPOT_JNI_CALLCHARMETHODV_ENTRY(env, obj, (uintptr_t)methodID),
                  HOTSPOT_JNI_CALLCHARMETHODV_RETURN(_ret_ref))
D
dcubed 已提交
1832
DEFINE_CALLMETHODV(jshort,   Short,   T_SHORT
1833 1834
                  , HOTSPOT_JNI_CALLSHORTMETHODV_ENTRY(env, obj, (uintptr_t)methodID),
                  HOTSPOT_JNI_CALLSHORTMETHODV_RETURN(_ret_ref))
D
dcubed 已提交
1835 1836

DEFINE_CALLMETHODV(jobject,  Object,  T_OBJECT
1837 1838
                  , HOTSPOT_JNI_CALLOBJECTMETHODV_ENTRY(env, obj, (uintptr_t)methodID),
                  HOTSPOT_JNI_CALLOBJECTMETHODV_RETURN(_ret_ref))
D
dcubed 已提交
1839
DEFINE_CALLMETHODV(jint,     Int,     T_INT,
1840 1841
                  HOTSPOT_JNI_CALLINTMETHODV_ENTRY(env, obj, (uintptr_t)methodID),
                  HOTSPOT_JNI_CALLINTMETHODV_RETURN(_ret_ref))
D
dcubed 已提交
1842
DEFINE_CALLMETHODV(jlong,    Long,    T_LONG
1843 1844
                  , HOTSPOT_JNI_CALLLONGMETHODV_ENTRY(env, obj, (uintptr_t)methodID),
                  HOTSPOT_JNI_CALLLONGMETHODV_RETURN(_ret_ref))
D
dcubed 已提交
1845 1846
// Float and double probes don't return value because dtrace doesn't currently support it
DEFINE_CALLMETHODV(jfloat,   Float,   T_FLOAT
1847 1848
                  , HOTSPOT_JNI_CALLFLOATMETHODV_ENTRY(env, obj, (uintptr_t)methodID),
                  HOTSPOT_JNI_CALLFLOATMETHODV_RETURN())
D
dcubed 已提交
1849
DEFINE_CALLMETHODV(jdouble,  Double,  T_DOUBLE
1850 1851
                  , HOTSPOT_JNI_CALLDOUBLEMETHODV_ENTRY(env, obj, (uintptr_t)methodID),
                  HOTSPOT_JNI_CALLDOUBLEMETHODV_RETURN())
D
dcubed 已提交
1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875

#define DEFINE_CALLMETHODA(ResultType, Result, Tag \
                          , EntryProbe, ReturnProbe)    \
\
  DT_RETURN_MARK_DECL_FOR(Result, Call##Result##MethodA, ResultType \
                          , ReturnProbe);                          \
\
JNI_ENTRY(ResultType, \
          jni_Call##Result##MethodA(JNIEnv *env, jobject obj, jmethodID methodID, const jvalue *args)) \
  JNIWrapper("Call" XSTR(Result) "MethodA"); \
  EntryProbe; \
  ResultType ret = 0;\
  DT_RETURN_MARK_FOR(Result, Call##Result##MethodA, ResultType, \
                     (const ResultType&)ret);\
\
  JavaValue jvalue(Tag); \
  JNI_ArgumentPusherArray ap(methodID, args); \
  jni_invoke_nonstatic(env, &jvalue, obj, JNI_VIRTUAL, methodID, &ap, CHECK_0); \
  ret = jvalue.get_##ResultType(); \
  return ret;\
JNI_END

// the runtime type of subword integral basic types is integer
DEFINE_CALLMETHODA(jboolean, Boolean, T_BOOLEAN
1876 1877
                  , HOTSPOT_JNI_CALLBOOLEANMETHODA_ENTRY(env, obj, (uintptr_t)methodID),
                  HOTSPOT_JNI_CALLBOOLEANMETHODA_RETURN(_ret_ref))
D
dcubed 已提交
1878
DEFINE_CALLMETHODA(jbyte,    Byte,    T_BYTE
1879 1880
                  , HOTSPOT_JNI_CALLBYTEMETHODA_ENTRY(env, obj, (uintptr_t)methodID),
                  HOTSPOT_JNI_CALLBYTEMETHODA_RETURN(_ret_ref))
D
dcubed 已提交
1881
DEFINE_CALLMETHODA(jchar,    Char,    T_CHAR
1882 1883
                  , HOTSPOT_JNI_CALLCHARMETHODA_ENTRY(env, obj, (uintptr_t)methodID),
                  HOTSPOT_JNI_CALLCHARMETHODA_RETURN(_ret_ref))
D
dcubed 已提交
1884
DEFINE_CALLMETHODA(jshort,   Short,   T_SHORT
1885 1886
                  , HOTSPOT_JNI_CALLSHORTMETHODA_ENTRY(env, obj, (uintptr_t)methodID),
                  HOTSPOT_JNI_CALLSHORTMETHODA_RETURN(_ret_ref))
D
dcubed 已提交
1887 1888

DEFINE_CALLMETHODA(jobject,  Object,  T_OBJECT
1889 1890
                  , HOTSPOT_JNI_CALLOBJECTMETHODA_ENTRY(env, obj, (uintptr_t)methodID),
                  HOTSPOT_JNI_CALLOBJECTMETHODA_RETURN(_ret_ref))
D
dcubed 已提交
1891
DEFINE_CALLMETHODA(jint,     Int,     T_INT,
1892 1893
                  HOTSPOT_JNI_CALLINTMETHODA_ENTRY(env, obj, (uintptr_t)methodID),
                  HOTSPOT_JNI_CALLINTMETHODA_RETURN(_ret_ref))
D
dcubed 已提交
1894
DEFINE_CALLMETHODA(jlong,    Long,    T_LONG
1895 1896
                  , HOTSPOT_JNI_CALLLONGMETHODA_ENTRY(env, obj, (uintptr_t)methodID),
                  HOTSPOT_JNI_CALLLONGMETHODA_RETURN(_ret_ref))
D
dcubed 已提交
1897 1898
// Float and double probes don't return value because dtrace doesn't currently support it
DEFINE_CALLMETHODA(jfloat,   Float,   T_FLOAT
1899 1900
                  , HOTSPOT_JNI_CALLFLOATMETHODA_ENTRY(env, obj, (uintptr_t)methodID),
                  HOTSPOT_JNI_CALLFLOATMETHODA_RETURN())
D
dcubed 已提交
1901
DEFINE_CALLMETHODA(jdouble,  Double,  T_DOUBLE
1902 1903
                  , HOTSPOT_JNI_CALLDOUBLEMETHODA_ENTRY(env, obj, (uintptr_t)methodID),
                  HOTSPOT_JNI_CALLDOUBLEMETHODA_RETURN())
D
dcubed 已提交
1904 1905 1906 1907 1908 1909 1910

DT_VOID_RETURN_MARK_DECL(CallVoidMethod, HOTSPOT_JNI_CALLVOIDMETHOD_RETURN());
DT_VOID_RETURN_MARK_DECL(CallVoidMethodV, HOTSPOT_JNI_CALLVOIDMETHODV_RETURN());
DT_VOID_RETURN_MARK_DECL(CallVoidMethodA, HOTSPOT_JNI_CALLVOIDMETHODA_RETURN());

#endif /* USDT2 */

D
duke 已提交
1911 1912
JNI_ENTRY(void, jni_CallVoidMethod(JNIEnv *env, jobject obj, jmethodID methodID, ...))
  JNIWrapper("CallVoidMethod");
D
dcubed 已提交
1913
#ifndef USDT2
D
duke 已提交
1914
  DTRACE_PROBE3(hotspot_jni, CallVoidMethod__entry, env, obj, methodID);
D
dcubed 已提交
1915 1916 1917 1918
#else /* USDT2 */
  HOTSPOT_JNI_CALLVOIDMETHOD_ENTRY(
                                   env, obj, (uintptr_t) methodID);
#endif /* USDT2 */
D
duke 已提交
1919 1920 1921 1922 1923
  DT_VOID_RETURN_MARK(CallVoidMethod);

  va_list args;
  va_start(args, methodID);
  JavaValue jvalue(T_VOID);
1924
  JNI_ArgumentPusherVaArg ap(methodID, args);
D
duke 已提交
1925 1926 1927 1928 1929 1930 1931
  jni_invoke_nonstatic(env, &jvalue, obj, JNI_VIRTUAL, methodID, &ap, CHECK);
  va_end(args);
JNI_END


JNI_ENTRY(void, jni_CallVoidMethodV(JNIEnv *env, jobject obj, jmethodID methodID, va_list args))
  JNIWrapper("CallVoidMethodV");
D
dcubed 已提交
1932
#ifndef USDT2
D
duke 已提交
1933
  DTRACE_PROBE3(hotspot_jni, CallVoidMethodV__entry, env, obj, methodID);
D
dcubed 已提交
1934 1935 1936 1937
#else /* USDT2 */
  HOTSPOT_JNI_CALLVOIDMETHODV_ENTRY(
                                    env, obj, (uintptr_t) methodID);
#endif /* USDT2 */
D
duke 已提交
1938 1939 1940
  DT_VOID_RETURN_MARK(CallVoidMethodV);

  JavaValue jvalue(T_VOID);
1941
  JNI_ArgumentPusherVaArg ap(methodID, args);
D
duke 已提交
1942 1943 1944 1945 1946 1947
  jni_invoke_nonstatic(env, &jvalue, obj, JNI_VIRTUAL, methodID, &ap, CHECK);
JNI_END


JNI_ENTRY(void, jni_CallVoidMethodA(JNIEnv *env, jobject obj, jmethodID methodID, const jvalue *args))
  JNIWrapper("CallVoidMethodA");
D
dcubed 已提交
1948
#ifndef USDT2
D
duke 已提交
1949
  DTRACE_PROBE3(hotspot_jni, CallVoidMethodA__entry, env, obj, methodID);
D
dcubed 已提交
1950 1951 1952 1953
#else /* USDT2 */
  HOTSPOT_JNI_CALLVOIDMETHODA_ENTRY(
                                    env, obj, (uintptr_t) methodID);
#endif /* USDT2 */
D
duke 已提交
1954 1955 1956
  DT_VOID_RETURN_MARK(CallVoidMethodA);

  JavaValue jvalue(T_VOID);
1957
  JNI_ArgumentPusherArray ap(methodID, args);
D
duke 已提交
1958 1959 1960 1961
  jni_invoke_nonstatic(env, &jvalue, obj, JNI_VIRTUAL, methodID, &ap, CHECK);
JNI_END


D
dcubed 已提交
1962
#ifndef USDT2
D
duke 已提交
1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980
#define DEFINE_CALLNONVIRTUALMETHOD(ResultType, Result, Tag) \
\
  DT_RETURN_MARK_DECL_FOR(Result, CallNonvirtual##Result##Method, ResultType);\
  DT_RETURN_MARK_DECL_FOR(Result, CallNonvirtual##Result##MethodV, ResultType);\
  DT_RETURN_MARK_DECL_FOR(Result, CallNonvirtual##Result##MethodA, ResultType);\
\
JNI_ENTRY(ResultType, \
          jni_CallNonvirtual##Result##Method(JNIEnv *env, jobject obj, jclass cls, jmethodID methodID, ...)) \
  JNIWrapper("CallNonvitual" XSTR(Result) "Method"); \
\
  DTRACE_PROBE4(hotspot_jni, CallNonvirtual##Result##Method__entry, env, obj, cls, methodID);\
  ResultType ret;\
  DT_RETURN_MARK_FOR(Result, CallNonvirtual##Result##Method, ResultType, \
                     (const ResultType&)ret);\
\
  va_list args; \
  va_start(args, methodID); \
  JavaValue jvalue(Tag); \
1981
  JNI_ArgumentPusherVaArg ap(methodID, args); \
D
duke 已提交
1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996
  jni_invoke_nonstatic(env, &jvalue, obj, JNI_NONVIRTUAL, methodID, &ap, CHECK_0); \
  va_end(args); \
  ret = jvalue.get_##ResultType(); \
  return ret;\
JNI_END \
\
JNI_ENTRY(ResultType, \
          jni_CallNonvirtual##Result##MethodV(JNIEnv *env, jobject obj, jclass cls, jmethodID methodID, va_list args)) \
  JNIWrapper("CallNonvitual" XSTR(Result) "#MethodV"); \
  DTRACE_PROBE4(hotspot_jni, CallNonvirtual##Result##MethodV__entry, env, obj, cls, methodID);\
  ResultType ret;\
  DT_RETURN_MARK_FOR(Result, CallNonvirtual##Result##MethodV, ResultType, \
                     (const ResultType&)ret);\
\
  JavaValue jvalue(Tag); \
1997
  JNI_ArgumentPusherVaArg ap(methodID, args); \
D
duke 已提交
1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011
  jni_invoke_nonstatic(env, &jvalue, obj, JNI_NONVIRTUAL, methodID, &ap, CHECK_0); \
  ret = jvalue.get_##ResultType(); \
  return ret;\
JNI_END \
\
JNI_ENTRY(ResultType, \
          jni_CallNonvirtual##Result##MethodA(JNIEnv *env, jobject obj, jclass cls, jmethodID methodID, const jvalue *args)) \
  JNIWrapper("CallNonvitual" XSTR(Result) "MethodA"); \
  DTRACE_PROBE4(hotspot_jni, CallNonvirtual##Result##MethodA__entry, env, obj, cls, methodID);\
  ResultType ret;\
  DT_RETURN_MARK_FOR(Result, CallNonvirtual##Result##MethodA, ResultType, \
                     (const ResultType&)ret);\
\
  JavaValue jvalue(Tag); \
2012
  JNI_ArgumentPusherArray ap(methodID, args); \
D
duke 已提交
2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034
  jni_invoke_nonstatic(env, &jvalue, obj, JNI_NONVIRTUAL, methodID, &ap, CHECK_0); \
  ret = jvalue.get_##ResultType(); \
  return ret;\
JNI_END

// the runtime type of subword integral basic types is integer
DEFINE_CALLNONVIRTUALMETHOD(jboolean, Boolean, T_BOOLEAN)
DEFINE_CALLNONVIRTUALMETHOD(jbyte,    Byte,    T_BYTE)
DEFINE_CALLNONVIRTUALMETHOD(jchar,    Char,    T_CHAR)
DEFINE_CALLNONVIRTUALMETHOD(jshort,   Short,   T_SHORT)

DEFINE_CALLNONVIRTUALMETHOD(jobject,  Object,  T_OBJECT)
DEFINE_CALLNONVIRTUALMETHOD(jint,     Int,     T_INT)
DEFINE_CALLNONVIRTUALMETHOD(jlong,    Long,    T_LONG)
DEFINE_CALLNONVIRTUALMETHOD(jfloat,   Float,   T_FLOAT)
DEFINE_CALLNONVIRTUALMETHOD(jdouble,  Double,  T_DOUBLE)


DT_VOID_RETURN_MARK_DECL(CallNonvirtualVoidMethod);
DT_VOID_RETURN_MARK_DECL(CallNonvirtualVoidMethodV);
DT_VOID_RETURN_MARK_DECL(CallNonvirtualVoidMethodA);

D
dcubed 已提交
2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206
#else /* USDT2 */

#define DEFINE_CALLNONVIRTUALMETHOD(ResultType, Result, Tag \
                                    , EntryProbe, ReturnProbe)      \
\
  DT_RETURN_MARK_DECL_FOR(Result, CallNonvirtual##Result##Method, ResultType \
                          , ReturnProbe);\
\
JNI_ENTRY(ResultType, \
          jni_CallNonvirtual##Result##Method(JNIEnv *env, jobject obj, jclass cls, jmethodID methodID, ...)) \
  JNIWrapper("CallNonvitual" XSTR(Result) "Method"); \
\
  EntryProbe;\
  ResultType ret;\
  DT_RETURN_MARK_FOR(Result, CallNonvirtual##Result##Method, ResultType, \
                     (const ResultType&)ret);\
\
  va_list args; \
  va_start(args, methodID); \
  JavaValue jvalue(Tag); \
  JNI_ArgumentPusherVaArg ap(methodID, args); \
  jni_invoke_nonstatic(env, &jvalue, obj, JNI_NONVIRTUAL, methodID, &ap, CHECK_0); \
  va_end(args); \
  ret = jvalue.get_##ResultType(); \
  return ret;\
JNI_END

// the runtime type of subword integral basic types is integer
DEFINE_CALLNONVIRTUALMETHOD(jboolean, Boolean, T_BOOLEAN
                            , HOTSPOT_JNI_CALLNONVIRTUALBOOLEANMETHOD_ENTRY(env, obj, cls, (uintptr_t)methodID),
                            HOTSPOT_JNI_CALLNONVIRTUALBOOLEANMETHOD_RETURN(_ret_ref))
DEFINE_CALLNONVIRTUALMETHOD(jbyte,    Byte,    T_BYTE
                            , HOTSPOT_JNI_CALLNONVIRTUALBYTEMETHOD_ENTRY(env, obj, cls, (uintptr_t)methodID),
                            HOTSPOT_JNI_CALLNONVIRTUALBYTEMETHOD_RETURN(_ret_ref))
DEFINE_CALLNONVIRTUALMETHOD(jchar,    Char,    T_CHAR
                            , HOTSPOT_JNI_CALLNONVIRTUALCHARMETHOD_ENTRY(env, obj, cls, (uintptr_t)methodID),
                            HOTSPOT_JNI_CALLNONVIRTUALCHARMETHOD_RETURN(_ret_ref))
DEFINE_CALLNONVIRTUALMETHOD(jshort,   Short,   T_SHORT
                            , HOTSPOT_JNI_CALLNONVIRTUALSHORTMETHOD_ENTRY(env, obj, cls, (uintptr_t)methodID),
                            HOTSPOT_JNI_CALLNONVIRTUALSHORTMETHOD_RETURN(_ret_ref))

DEFINE_CALLNONVIRTUALMETHOD(jobject,  Object,  T_OBJECT
                            , HOTSPOT_JNI_CALLNONVIRTUALOBJECTMETHOD_ENTRY(env, obj, cls, (uintptr_t)methodID),
                            HOTSPOT_JNI_CALLNONVIRTUALOBJECTMETHOD_RETURN(_ret_ref))
DEFINE_CALLNONVIRTUALMETHOD(jint,     Int,     T_INT
                            , HOTSPOT_JNI_CALLNONVIRTUALINTMETHOD_ENTRY(env, obj, cls, (uintptr_t)methodID),
                            HOTSPOT_JNI_CALLNONVIRTUALINTMETHOD_RETURN(_ret_ref))
DEFINE_CALLNONVIRTUALMETHOD(jlong,    Long,    T_LONG
                            , HOTSPOT_JNI_CALLNONVIRTUALLONGMETHOD_ENTRY(env, obj, cls, (uintptr_t)methodID),
// Float and double probes don't return value because dtrace doesn't currently support it
                            HOTSPOT_JNI_CALLNONVIRTUALLONGMETHOD_RETURN(_ret_ref))
DEFINE_CALLNONVIRTUALMETHOD(jfloat,   Float,   T_FLOAT
                            , HOTSPOT_JNI_CALLNONVIRTUALFLOATMETHOD_ENTRY(env, obj, cls, (uintptr_t)methodID),
                            HOTSPOT_JNI_CALLNONVIRTUALFLOATMETHOD_RETURN())
DEFINE_CALLNONVIRTUALMETHOD(jdouble,  Double,  T_DOUBLE
                            , HOTSPOT_JNI_CALLNONVIRTUALDOUBLEMETHOD_ENTRY(env, obj, cls, (uintptr_t)methodID),
                            HOTSPOT_JNI_CALLNONVIRTUALDOUBLEMETHOD_RETURN())

#define DEFINE_CALLNONVIRTUALMETHODV(ResultType, Result, Tag \
                                    , EntryProbe, ReturnProbe)      \
\
  DT_RETURN_MARK_DECL_FOR(Result, CallNonvirtual##Result##MethodV, ResultType \
                          , ReturnProbe);\
\
JNI_ENTRY(ResultType, \
          jni_CallNonvirtual##Result##MethodV(JNIEnv *env, jobject obj, jclass cls, jmethodID methodID, va_list args)) \
  JNIWrapper("CallNonvitual" XSTR(Result) "MethodV"); \
\
  EntryProbe;\
  ResultType ret;\
  DT_RETURN_MARK_FOR(Result, CallNonvirtual##Result##MethodV, ResultType, \
                     (const ResultType&)ret);\
\
  JavaValue jvalue(Tag); \
  JNI_ArgumentPusherVaArg ap(methodID, args); \
  jni_invoke_nonstatic(env, &jvalue, obj, JNI_NONVIRTUAL, methodID, &ap, CHECK_0); \
  ret = jvalue.get_##ResultType(); \
  return ret;\
JNI_END

// the runtime type of subword integral basic types is integer
DEFINE_CALLNONVIRTUALMETHODV(jboolean, Boolean, T_BOOLEAN
                            , HOTSPOT_JNI_CALLNONVIRTUALBOOLEANMETHODV_ENTRY(env, obj, cls, (uintptr_t)methodID),
                            HOTSPOT_JNI_CALLNONVIRTUALBOOLEANMETHODV_RETURN(_ret_ref))
DEFINE_CALLNONVIRTUALMETHODV(jbyte,    Byte,    T_BYTE
                            , HOTSPOT_JNI_CALLNONVIRTUALBYTEMETHODV_ENTRY(env, obj, cls, (uintptr_t)methodID),
                            HOTSPOT_JNI_CALLNONVIRTUALBYTEMETHODV_RETURN(_ret_ref))
DEFINE_CALLNONVIRTUALMETHODV(jchar,    Char,    T_CHAR
                            , HOTSPOT_JNI_CALLNONVIRTUALCHARMETHODV_ENTRY(env, obj, cls, (uintptr_t)methodID),
                            HOTSPOT_JNI_CALLNONVIRTUALCHARMETHODV_RETURN(_ret_ref))
DEFINE_CALLNONVIRTUALMETHODV(jshort,   Short,   T_SHORT
                            , HOTSPOT_JNI_CALLNONVIRTUALSHORTMETHODV_ENTRY(env, obj, cls, (uintptr_t)methodID),
                            HOTSPOT_JNI_CALLNONVIRTUALSHORTMETHODV_RETURN(_ret_ref))

DEFINE_CALLNONVIRTUALMETHODV(jobject,  Object,  T_OBJECT
                            , HOTSPOT_JNI_CALLNONVIRTUALOBJECTMETHODV_ENTRY(env, obj, cls, (uintptr_t)methodID),
                            HOTSPOT_JNI_CALLNONVIRTUALOBJECTMETHODV_RETURN(_ret_ref))
DEFINE_CALLNONVIRTUALMETHODV(jint,     Int,     T_INT
                            , HOTSPOT_JNI_CALLNONVIRTUALINTMETHODV_ENTRY(env, obj, cls, (uintptr_t)methodID),
                            HOTSPOT_JNI_CALLNONVIRTUALINTMETHODV_RETURN(_ret_ref))
DEFINE_CALLNONVIRTUALMETHODV(jlong,    Long,    T_LONG
                            , HOTSPOT_JNI_CALLNONVIRTUALLONGMETHODV_ENTRY(env, obj, cls, (uintptr_t)methodID),
// Float and double probes don't return value because dtrace doesn't currently support it
                            HOTSPOT_JNI_CALLNONVIRTUALLONGMETHODV_RETURN(_ret_ref))
DEFINE_CALLNONVIRTUALMETHODV(jfloat,   Float,   T_FLOAT
                            , HOTSPOT_JNI_CALLNONVIRTUALFLOATMETHODV_ENTRY(env, obj, cls, (uintptr_t)methodID),
                            HOTSPOT_JNI_CALLNONVIRTUALFLOATMETHODV_RETURN())
DEFINE_CALLNONVIRTUALMETHODV(jdouble,  Double,  T_DOUBLE
                            , HOTSPOT_JNI_CALLNONVIRTUALDOUBLEMETHODV_ENTRY(env, obj, cls, (uintptr_t)methodID),
                            HOTSPOT_JNI_CALLNONVIRTUALDOUBLEMETHODV_RETURN())

#define DEFINE_CALLNONVIRTUALMETHODA(ResultType, Result, Tag \
                                    , EntryProbe, ReturnProbe)      \
\
  DT_RETURN_MARK_DECL_FOR(Result, CallNonvirtual##Result##MethodA, ResultType \
                          , ReturnProbe);\
\
JNI_ENTRY(ResultType, \
          jni_CallNonvirtual##Result##MethodA(JNIEnv *env, jobject obj, jclass cls, jmethodID methodID, const jvalue *args)) \
  JNIWrapper("CallNonvitual" XSTR(Result) "MethodA"); \
\
  EntryProbe;\
  ResultType ret;\
  DT_RETURN_MARK_FOR(Result, CallNonvirtual##Result##MethodA, ResultType, \
                     (const ResultType&)ret);\
\
  JavaValue jvalue(Tag); \
  JNI_ArgumentPusherArray ap(methodID, args); \
  jni_invoke_nonstatic(env, &jvalue, obj, JNI_NONVIRTUAL, methodID, &ap, CHECK_0); \
  ret = jvalue.get_##ResultType(); \
  return ret;\
JNI_END

// the runtime type of subword integral basic types is integer
DEFINE_CALLNONVIRTUALMETHODA(jboolean, Boolean, T_BOOLEAN
                            , HOTSPOT_JNI_CALLNONVIRTUALBOOLEANMETHODA_ENTRY(env, obj, cls, (uintptr_t)methodID),
                            HOTSPOT_JNI_CALLNONVIRTUALBOOLEANMETHODA_RETURN(_ret_ref))
DEFINE_CALLNONVIRTUALMETHODA(jbyte,    Byte,    T_BYTE
                            , HOTSPOT_JNI_CALLNONVIRTUALBYTEMETHODA_ENTRY(env, obj, cls, (uintptr_t)methodID),
                            HOTSPOT_JNI_CALLNONVIRTUALBYTEMETHODA_RETURN(_ret_ref))
DEFINE_CALLNONVIRTUALMETHODA(jchar,    Char,    T_CHAR
                            , HOTSPOT_JNI_CALLNONVIRTUALCHARMETHODA_ENTRY(env, obj, cls, (uintptr_t)methodID),
                            HOTSPOT_JNI_CALLNONVIRTUALCHARMETHODA_RETURN(_ret_ref))
DEFINE_CALLNONVIRTUALMETHODA(jshort,   Short,   T_SHORT
                            , HOTSPOT_JNI_CALLNONVIRTUALSHORTMETHODA_ENTRY(env, obj, cls, (uintptr_t)methodID),
                            HOTSPOT_JNI_CALLNONVIRTUALSHORTMETHODA_RETURN(_ret_ref))

DEFINE_CALLNONVIRTUALMETHODA(jobject,  Object,  T_OBJECT
                            , HOTSPOT_JNI_CALLNONVIRTUALOBJECTMETHODA_ENTRY(env, obj, cls, (uintptr_t)methodID),
                            HOTSPOT_JNI_CALLNONVIRTUALOBJECTMETHODA_RETURN(_ret_ref))
DEFINE_CALLNONVIRTUALMETHODA(jint,     Int,     T_INT
                            , HOTSPOT_JNI_CALLNONVIRTUALINTMETHODA_ENTRY(env, obj, cls, (uintptr_t)methodID),
                            HOTSPOT_JNI_CALLNONVIRTUALINTMETHODA_RETURN(_ret_ref))
DEFINE_CALLNONVIRTUALMETHODA(jlong,    Long,    T_LONG
                            , HOTSPOT_JNI_CALLNONVIRTUALLONGMETHODA_ENTRY(env, obj, cls, (uintptr_t)methodID),
// Float and double probes don't return value because dtrace doesn't currently support it
                            HOTSPOT_JNI_CALLNONVIRTUALLONGMETHODA_RETURN(_ret_ref))
DEFINE_CALLNONVIRTUALMETHODA(jfloat,   Float,   T_FLOAT
                            , HOTSPOT_JNI_CALLNONVIRTUALFLOATMETHODA_ENTRY(env, obj, cls, (uintptr_t)methodID),
                            HOTSPOT_JNI_CALLNONVIRTUALFLOATMETHODA_RETURN())
DEFINE_CALLNONVIRTUALMETHODA(jdouble,  Double,  T_DOUBLE
                            , HOTSPOT_JNI_CALLNONVIRTUALDOUBLEMETHODA_ENTRY(env, obj, cls, (uintptr_t)methodID),
                            HOTSPOT_JNI_CALLNONVIRTUALDOUBLEMETHODA_RETURN())

DT_VOID_RETURN_MARK_DECL(CallNonvirtualVoidMethod
                         , HOTSPOT_JNI_CALLNONVIRTUALVOIDMETHOD_RETURN());
DT_VOID_RETURN_MARK_DECL(CallNonvirtualVoidMethodV
                         , HOTSPOT_JNI_CALLNONVIRTUALVOIDMETHODV_RETURN());
DT_VOID_RETURN_MARK_DECL(CallNonvirtualVoidMethodA
                         , HOTSPOT_JNI_CALLNONVIRTUALVOIDMETHODA_RETURN());
#endif /* USDT2 */

D
duke 已提交
2207 2208 2209
JNI_ENTRY(void, jni_CallNonvirtualVoidMethod(JNIEnv *env, jobject obj, jclass cls, jmethodID methodID, ...))
  JNIWrapper("CallNonvirtualVoidMethod");

D
dcubed 已提交
2210
#ifndef USDT2
D
duke 已提交
2211 2212
  DTRACE_PROBE4(hotspot_jni, CallNonvirtualVoidMethod__entry,
               env, obj, cls, methodID);
D
dcubed 已提交
2213 2214 2215 2216
#else /* USDT2 */
  HOTSPOT_JNI_CALLNONVIRTUALVOIDMETHOD_ENTRY(
               env, obj, cls, (uintptr_t) methodID);
#endif /* USDT2 */
D
duke 已提交
2217 2218 2219 2220 2221
  DT_VOID_RETURN_MARK(CallNonvirtualVoidMethod);

  va_list args;
  va_start(args, methodID);
  JavaValue jvalue(T_VOID);
2222
  JNI_ArgumentPusherVaArg ap(methodID, args);
D
duke 已提交
2223 2224 2225 2226 2227 2228 2229 2230
  jni_invoke_nonstatic(env, &jvalue, obj, JNI_NONVIRTUAL, methodID, &ap, CHECK);
  va_end(args);
JNI_END


JNI_ENTRY(void, jni_CallNonvirtualVoidMethodV(JNIEnv *env, jobject obj, jclass cls, jmethodID methodID, va_list args))
  JNIWrapper("CallNonvirtualVoidMethodV");

D
dcubed 已提交
2231
#ifndef USDT2
D
duke 已提交
2232 2233
  DTRACE_PROBE4(hotspot_jni, CallNonvirtualVoidMethodV__entry,
               env, obj, cls, methodID);
D
dcubed 已提交
2234 2235 2236 2237
#else /* USDT2 */
  HOTSPOT_JNI_CALLNONVIRTUALVOIDMETHODV_ENTRY(
               env, obj, cls, (uintptr_t) methodID);
#endif /* USDT2 */
D
duke 已提交
2238 2239 2240
  DT_VOID_RETURN_MARK(CallNonvirtualVoidMethodV);

  JavaValue jvalue(T_VOID);
2241
  JNI_ArgumentPusherVaArg ap(methodID, args);
D
duke 已提交
2242 2243 2244 2245 2246 2247
  jni_invoke_nonstatic(env, &jvalue, obj, JNI_NONVIRTUAL, methodID, &ap, CHECK);
JNI_END


JNI_ENTRY(void, jni_CallNonvirtualVoidMethodA(JNIEnv *env, jobject obj, jclass cls, jmethodID methodID, const jvalue *args))
  JNIWrapper("CallNonvirtualVoidMethodA");
D
dcubed 已提交
2248
#ifndef USDT2
D
duke 已提交
2249 2250
  DTRACE_PROBE4(hotspot_jni, CallNonvirtualVoidMethodA__entry,
                env, obj, cls, methodID);
D
dcubed 已提交
2251 2252 2253 2254
#else /* USDT2 */
  HOTSPOT_JNI_CALLNONVIRTUALVOIDMETHODA_ENTRY(
                env, obj, cls, (uintptr_t) methodID);
#endif /* USDT2 */
D
duke 已提交
2255 2256
  DT_VOID_RETURN_MARK(CallNonvirtualVoidMethodA);
  JavaValue jvalue(T_VOID);
2257
  JNI_ArgumentPusherArray ap(methodID, args);
D
duke 已提交
2258 2259 2260 2261
  jni_invoke_nonstatic(env, &jvalue, obj, JNI_NONVIRTUAL, methodID, &ap, CHECK);
JNI_END


D
dcubed 已提交
2262
#ifndef USDT2
D
duke 已提交
2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280
#define DEFINE_CALLSTATICMETHOD(ResultType, Result, Tag) \
\
  DT_RETURN_MARK_DECL_FOR(Result, CallStatic##Result##Method, ResultType);\
  DT_RETURN_MARK_DECL_FOR(Result, CallStatic##Result##MethodV, ResultType);\
  DT_RETURN_MARK_DECL_FOR(Result, CallStatic##Result##MethodA, ResultType);\
\
JNI_ENTRY(ResultType, \
          jni_CallStatic##Result##Method(JNIEnv *env, jclass cls, jmethodID methodID, ...)) \
  JNIWrapper("CallStatic" XSTR(Result) "Method"); \
\
  DTRACE_PROBE3(hotspot_jni, CallStatic##Result##Method__entry, env, cls, methodID);\
  ResultType ret = 0;\
  DT_RETURN_MARK_FOR(Result, CallStatic##Result##Method, ResultType, \
                     (const ResultType&)ret);\
\
  va_list args; \
  va_start(args, methodID); \
  JavaValue jvalue(Tag); \
2281
  JNI_ArgumentPusherVaArg ap(methodID, args); \
D
duke 已提交
2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296
  jni_invoke_static(env, &jvalue, NULL, JNI_STATIC, methodID, &ap, CHECK_0); \
  va_end(args); \
  ret = jvalue.get_##ResultType(); \
  return ret;\
JNI_END \
\
JNI_ENTRY(ResultType, \
          jni_CallStatic##Result##MethodV(JNIEnv *env, jclass cls, jmethodID methodID, va_list args)) \
  JNIWrapper("CallStatic" XSTR(Result) "MethodV"); \
  DTRACE_PROBE3(hotspot_jni, CallStatic##Result##MethodV__entry, env, cls, methodID);\
  ResultType ret = 0;\
  DT_RETURN_MARK_FOR(Result, CallStatic##Result##MethodV, ResultType, \
                     (const ResultType&)ret);\
\
  JavaValue jvalue(Tag); \
2297
  JNI_ArgumentPusherVaArg ap(methodID, args); \
D
duke 已提交
2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311
  jni_invoke_static(env, &jvalue, NULL, JNI_STATIC, methodID, &ap, CHECK_0); \
  ret = jvalue.get_##ResultType(); \
  return ret;\
JNI_END \
\
JNI_ENTRY(ResultType, \
          jni_CallStatic##Result##MethodA(JNIEnv *env, jclass cls, jmethodID methodID, const jvalue *args)) \
  JNIWrapper("CallStatic" XSTR(Result) "MethodA"); \
  DTRACE_PROBE3(hotspot_jni, CallStatic##Result##MethodA__entry, env, cls, methodID);\
  ResultType ret = 0;\
  DT_RETURN_MARK_FOR(Result, CallStatic##Result##MethodA, ResultType, \
                     (const ResultType&)ret);\
\
  JavaValue jvalue(Tag); \
2312
  JNI_ArgumentPusherArray ap(methodID, args); \
D
duke 已提交
2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334
  jni_invoke_static(env, &jvalue, NULL, JNI_STATIC, methodID, &ap, CHECK_0); \
  ret = jvalue.get_##ResultType(); \
  return ret;\
JNI_END

// the runtime type of subword integral basic types is integer
DEFINE_CALLSTATICMETHOD(jboolean, Boolean, T_BOOLEAN)
DEFINE_CALLSTATICMETHOD(jbyte,    Byte,    T_BYTE)
DEFINE_CALLSTATICMETHOD(jchar,    Char,    T_CHAR)
DEFINE_CALLSTATICMETHOD(jshort,   Short,   T_SHORT)

DEFINE_CALLSTATICMETHOD(jobject,  Object,  T_OBJECT)
DEFINE_CALLSTATICMETHOD(jint,     Int,     T_INT)
DEFINE_CALLSTATICMETHOD(jlong,    Long,    T_LONG)
DEFINE_CALLSTATICMETHOD(jfloat,   Float,   T_FLOAT)
DEFINE_CALLSTATICMETHOD(jdouble,  Double,  T_DOUBLE)


DT_VOID_RETURN_MARK_DECL(CallStaticVoidMethod);
DT_VOID_RETURN_MARK_DECL(CallStaticVoidMethodV);
DT_VOID_RETURN_MARK_DECL(CallStaticVoidMethodA);

D
dcubed 已提交
2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410
#else /* USDT2 */

#define DEFINE_CALLSTATICMETHOD(ResultType, Result, Tag \
                                , EntryProbe, ResultProbe) \
\
  DT_RETURN_MARK_DECL_FOR(Result, CallStatic##Result##Method, ResultType \
                          , ResultProbe);                               \
\
JNI_ENTRY(ResultType, \
          jni_CallStatic##Result##Method(JNIEnv *env, jclass cls, jmethodID methodID, ...)) \
  JNIWrapper("CallStatic" XSTR(Result) "Method"); \
\
  EntryProbe; \
  ResultType ret = 0;\
  DT_RETURN_MARK_FOR(Result, CallStatic##Result##Method, ResultType, \
                     (const ResultType&)ret);\
\
  va_list args; \
  va_start(args, methodID); \
  JavaValue jvalue(Tag); \
  JNI_ArgumentPusherVaArg ap(methodID, args); \
  jni_invoke_static(env, &jvalue, NULL, JNI_STATIC, methodID, &ap, CHECK_0); \
  va_end(args); \
  ret = jvalue.get_##ResultType(); \
  return ret;\
JNI_END

// the runtime type of subword integral basic types is integer
DEFINE_CALLSTATICMETHOD(jboolean, Boolean, T_BOOLEAN
                        , HOTSPOT_JNI_CALLSTATICBOOLEANMETHOD_ENTRY(env, cls, (uintptr_t)methodID),
                        HOTSPOT_JNI_CALLSTATICBOOLEANMETHOD_RETURN(_ret_ref));
DEFINE_CALLSTATICMETHOD(jbyte,    Byte,    T_BYTE
                        , HOTSPOT_JNI_CALLSTATICBYTEMETHOD_ENTRY(env, cls, (uintptr_t)methodID),
                        HOTSPOT_JNI_CALLSTATICBYTEMETHOD_RETURN(_ret_ref));
DEFINE_CALLSTATICMETHOD(jchar,    Char,    T_CHAR
                        , HOTSPOT_JNI_CALLSTATICCHARMETHOD_ENTRY(env, cls, (uintptr_t)methodID),
                        HOTSPOT_JNI_CALLSTATICCHARMETHOD_RETURN(_ret_ref));
DEFINE_CALLSTATICMETHOD(jshort,   Short,   T_SHORT
                        , HOTSPOT_JNI_CALLSTATICSHORTMETHOD_ENTRY(env, cls, (uintptr_t)methodID),
                        HOTSPOT_JNI_CALLSTATICSHORTMETHOD_RETURN(_ret_ref));

DEFINE_CALLSTATICMETHOD(jobject,  Object,  T_OBJECT
                        , HOTSPOT_JNI_CALLSTATICOBJECTMETHOD_ENTRY(env, cls, (uintptr_t)methodID),
                        HOTSPOT_JNI_CALLSTATICOBJECTMETHOD_RETURN(_ret_ref));
DEFINE_CALLSTATICMETHOD(jint,     Int,     T_INT
                        , HOTSPOT_JNI_CALLSTATICINTMETHOD_ENTRY(env, cls, (uintptr_t)methodID),
                        HOTSPOT_JNI_CALLSTATICINTMETHOD_RETURN(_ret_ref));
DEFINE_CALLSTATICMETHOD(jlong,    Long,    T_LONG
                        , HOTSPOT_JNI_CALLSTATICLONGMETHOD_ENTRY(env, cls, (uintptr_t)methodID),
                        HOTSPOT_JNI_CALLSTATICLONGMETHOD_RETURN(_ret_ref));
// Float and double probes don't return value because dtrace doesn't currently support it
DEFINE_CALLSTATICMETHOD(jfloat,   Float,   T_FLOAT
                        , HOTSPOT_JNI_CALLSTATICFLOATMETHOD_ENTRY(env, cls, (uintptr_t)methodID),
                        HOTSPOT_JNI_CALLSTATICFLOATMETHOD_RETURN());
DEFINE_CALLSTATICMETHOD(jdouble,  Double,  T_DOUBLE
                        , HOTSPOT_JNI_CALLSTATICDOUBLEMETHOD_ENTRY(env, cls, (uintptr_t)methodID),
                        HOTSPOT_JNI_CALLSTATICDOUBLEMETHOD_RETURN());

#define DEFINE_CALLSTATICMETHODV(ResultType, Result, Tag \
                                , EntryProbe, ResultProbe) \
\
  DT_RETURN_MARK_DECL_FOR(Result, CallStatic##Result##MethodV, ResultType \
                          , ResultProbe);                               \
\
JNI_ENTRY(ResultType, \
          jni_CallStatic##Result##MethodV(JNIEnv *env, jclass cls, jmethodID methodID, va_list args)) \
  JNIWrapper("CallStatic" XSTR(Result) "MethodV"); \
\
  EntryProbe; \
  ResultType ret = 0;\
  DT_RETURN_MARK_FOR(Result, CallStatic##Result##MethodV, ResultType, \
                     (const ResultType&)ret);\
\
  JavaValue jvalue(Tag); \
  JNI_ArgumentPusherVaArg ap(methodID, args); \
  /* Make sure class is initialized before trying to invoke its method */ \
2411
  KlassHandle k(THREAD, java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls))); \
H
hseigel 已提交
2412
  k()->initialize(CHECK_0); \
D
dcubed 已提交
2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510
  jni_invoke_static(env, &jvalue, NULL, JNI_STATIC, methodID, &ap, CHECK_0); \
  va_end(args); \
  ret = jvalue.get_##ResultType(); \
  return ret;\
JNI_END

// the runtime type of subword integral basic types is integer
DEFINE_CALLSTATICMETHODV(jboolean, Boolean, T_BOOLEAN
                        , HOTSPOT_JNI_CALLSTATICBOOLEANMETHODV_ENTRY(env, cls, (uintptr_t)methodID),
                        HOTSPOT_JNI_CALLSTATICBOOLEANMETHODV_RETURN(_ret_ref));
DEFINE_CALLSTATICMETHODV(jbyte,    Byte,    T_BYTE
                        , HOTSPOT_JNI_CALLSTATICBYTEMETHODV_ENTRY(env, cls, (uintptr_t)methodID),
                        HOTSPOT_JNI_CALLSTATICBYTEMETHODV_RETURN(_ret_ref));
DEFINE_CALLSTATICMETHODV(jchar,    Char,    T_CHAR
                        , HOTSPOT_JNI_CALLSTATICCHARMETHODV_ENTRY(env, cls, (uintptr_t)methodID),
                        HOTSPOT_JNI_CALLSTATICCHARMETHODV_RETURN(_ret_ref));
DEFINE_CALLSTATICMETHODV(jshort,   Short,   T_SHORT
                        , HOTSPOT_JNI_CALLSTATICSHORTMETHODV_ENTRY(env, cls, (uintptr_t)methodID),
                        HOTSPOT_JNI_CALLSTATICSHORTMETHODV_RETURN(_ret_ref));

DEFINE_CALLSTATICMETHODV(jobject,  Object,  T_OBJECT
                        , HOTSPOT_JNI_CALLSTATICOBJECTMETHODV_ENTRY(env, cls, (uintptr_t)methodID),
                        HOTSPOT_JNI_CALLSTATICOBJECTMETHODV_RETURN(_ret_ref));
DEFINE_CALLSTATICMETHODV(jint,     Int,     T_INT
                        , HOTSPOT_JNI_CALLSTATICINTMETHODV_ENTRY(env, cls, (uintptr_t)methodID),
                        HOTSPOT_JNI_CALLSTATICINTMETHODV_RETURN(_ret_ref));
DEFINE_CALLSTATICMETHODV(jlong,    Long,    T_LONG
                        , HOTSPOT_JNI_CALLSTATICLONGMETHODV_ENTRY(env, cls, (uintptr_t)methodID),
                        HOTSPOT_JNI_CALLSTATICLONGMETHODV_RETURN(_ret_ref));
// Float and double probes don't return value because dtrace doesn't currently support it
DEFINE_CALLSTATICMETHODV(jfloat,   Float,   T_FLOAT
                        , HOTSPOT_JNI_CALLSTATICFLOATMETHODV_ENTRY(env, cls, (uintptr_t)methodID),
                        HOTSPOT_JNI_CALLSTATICFLOATMETHODV_RETURN());
DEFINE_CALLSTATICMETHODV(jdouble,  Double,  T_DOUBLE
                        , HOTSPOT_JNI_CALLSTATICDOUBLEMETHODV_ENTRY(env, cls, (uintptr_t)methodID),
                        HOTSPOT_JNI_CALLSTATICDOUBLEMETHODV_RETURN());

#define DEFINE_CALLSTATICMETHODA(ResultType, Result, Tag \
                                , EntryProbe, ResultProbe) \
\
  DT_RETURN_MARK_DECL_FOR(Result, CallStatic##Result##MethodA, ResultType \
                          , ResultProbe);                               \
\
JNI_ENTRY(ResultType, \
          jni_CallStatic##Result##MethodA(JNIEnv *env, jclass cls, jmethodID methodID, const jvalue *args)) \
  JNIWrapper("CallStatic" XSTR(Result) "MethodA"); \
\
  EntryProbe; \
  ResultType ret = 0;\
  DT_RETURN_MARK_FOR(Result, CallStatic##Result##MethodA, ResultType, \
                     (const ResultType&)ret);\
\
  JavaValue jvalue(Tag); \
  JNI_ArgumentPusherArray ap(methodID, args); \
  jni_invoke_static(env, &jvalue, NULL, JNI_STATIC, methodID, &ap, CHECK_0); \
  ret = jvalue.get_##ResultType(); \
  return ret;\
JNI_END

// the runtime type of subword integral basic types is integer
DEFINE_CALLSTATICMETHODA(jboolean, Boolean, T_BOOLEAN
                        , HOTSPOT_JNI_CALLSTATICBOOLEANMETHODA_ENTRY(env, cls, (uintptr_t)methodID),
                        HOTSPOT_JNI_CALLSTATICBOOLEANMETHODA_RETURN(_ret_ref));
DEFINE_CALLSTATICMETHODA(jbyte,    Byte,    T_BYTE
                        , HOTSPOT_JNI_CALLSTATICBYTEMETHODA_ENTRY(env, cls, (uintptr_t)methodID),
                        HOTSPOT_JNI_CALLSTATICBYTEMETHODA_RETURN(_ret_ref));
DEFINE_CALLSTATICMETHODA(jchar,    Char,    T_CHAR
                        , HOTSPOT_JNI_CALLSTATICCHARMETHODA_ENTRY(env, cls, (uintptr_t)methodID),
                        HOTSPOT_JNI_CALLSTATICCHARMETHODA_RETURN(_ret_ref));
DEFINE_CALLSTATICMETHODA(jshort,   Short,   T_SHORT
                        , HOTSPOT_JNI_CALLSTATICSHORTMETHODA_ENTRY(env, cls, (uintptr_t)methodID),
                        HOTSPOT_JNI_CALLSTATICSHORTMETHODA_RETURN(_ret_ref));

DEFINE_CALLSTATICMETHODA(jobject,  Object,  T_OBJECT
                        , HOTSPOT_JNI_CALLSTATICOBJECTMETHODA_ENTRY(env, cls, (uintptr_t)methodID),
                        HOTSPOT_JNI_CALLSTATICOBJECTMETHODA_RETURN(_ret_ref));
DEFINE_CALLSTATICMETHODA(jint,     Int,     T_INT
                        , HOTSPOT_JNI_CALLSTATICINTMETHODA_ENTRY(env, cls, (uintptr_t)methodID),
                        HOTSPOT_JNI_CALLSTATICINTMETHODA_RETURN(_ret_ref));
DEFINE_CALLSTATICMETHODA(jlong,    Long,    T_LONG
                        , HOTSPOT_JNI_CALLSTATICLONGMETHODA_ENTRY(env, cls, (uintptr_t)methodID),
                        HOTSPOT_JNI_CALLSTATICLONGMETHODA_RETURN(_ret_ref));
// Float and double probes don't return value because dtrace doesn't currently support it
DEFINE_CALLSTATICMETHODA(jfloat,   Float,   T_FLOAT
                        , HOTSPOT_JNI_CALLSTATICFLOATMETHODA_ENTRY(env, cls, (uintptr_t)methodID),
                        HOTSPOT_JNI_CALLSTATICFLOATMETHODA_RETURN());
DEFINE_CALLSTATICMETHODA(jdouble,  Double,  T_DOUBLE
                        , HOTSPOT_JNI_CALLSTATICDOUBLEMETHODA_ENTRY(env, cls, (uintptr_t)methodID),
                        HOTSPOT_JNI_CALLSTATICDOUBLEMETHODA_RETURN());

DT_VOID_RETURN_MARK_DECL(CallStaticVoidMethod
                         , HOTSPOT_JNI_CALLSTATICVOIDMETHOD_RETURN());
DT_VOID_RETURN_MARK_DECL(CallStaticVoidMethodV
                         , HOTSPOT_JNI_CALLSTATICVOIDMETHODV_RETURN());
DT_VOID_RETURN_MARK_DECL(CallStaticVoidMethodA
                         , HOTSPOT_JNI_CALLSTATICVOIDMETHODA_RETURN());
#endif /* USDT2 */

D
duke 已提交
2511 2512
JNI_ENTRY(void, jni_CallStaticVoidMethod(JNIEnv *env, jclass cls, jmethodID methodID, ...))
  JNIWrapper("CallStaticVoidMethod");
D
dcubed 已提交
2513
#ifndef USDT2
D
duke 已提交
2514
  DTRACE_PROBE3(hotspot_jni, CallStaticVoidMethod__entry, env, cls, methodID);
D
dcubed 已提交
2515 2516 2517 2518
#else /* USDT2 */
  HOTSPOT_JNI_CALLSTATICVOIDMETHOD_ENTRY(
                                         env, cls, (uintptr_t) methodID);
#endif /* USDT2 */
D
duke 已提交
2519 2520 2521 2522 2523
  DT_VOID_RETURN_MARK(CallStaticVoidMethod);

  va_list args;
  va_start(args, methodID);
  JavaValue jvalue(T_VOID);
2524
  JNI_ArgumentPusherVaArg ap(methodID, args);
D
duke 已提交
2525 2526 2527 2528 2529 2530 2531
  jni_invoke_static(env, &jvalue, NULL, JNI_STATIC, methodID, &ap, CHECK);
  va_end(args);
JNI_END


JNI_ENTRY(void, jni_CallStaticVoidMethodV(JNIEnv *env, jclass cls, jmethodID methodID, va_list args))
  JNIWrapper("CallStaticVoidMethodV");
D
dcubed 已提交
2532
#ifndef USDT2
D
duke 已提交
2533
  DTRACE_PROBE3(hotspot_jni, CallStaticVoidMethodV__entry, env, cls, methodID);
D
dcubed 已提交
2534 2535 2536 2537
#else /* USDT2 */
  HOTSPOT_JNI_CALLSTATICVOIDMETHODV_ENTRY(
                                          env, cls, (uintptr_t) methodID);
#endif /* USDT2 */
D
duke 已提交
2538 2539 2540
  DT_VOID_RETURN_MARK(CallStaticVoidMethodV);

  JavaValue jvalue(T_VOID);
2541
  JNI_ArgumentPusherVaArg ap(methodID, args);
D
duke 已提交
2542 2543 2544 2545 2546 2547
  jni_invoke_static(env, &jvalue, NULL, JNI_STATIC, methodID, &ap, CHECK);
JNI_END


JNI_ENTRY(void, jni_CallStaticVoidMethodA(JNIEnv *env, jclass cls, jmethodID methodID, const jvalue *args))
  JNIWrapper("CallStaticVoidMethodA");
D
dcubed 已提交
2548
#ifndef USDT2
D
duke 已提交
2549
  DTRACE_PROBE3(hotspot_jni, CallStaticVoidMethodA__entry, env, cls, methodID);
D
dcubed 已提交
2550 2551 2552 2553
#else /* USDT2 */
  HOTSPOT_JNI_CALLSTATICVOIDMETHODA_ENTRY(
                                          env, cls, (uintptr_t) methodID);
#endif /* USDT2 */
D
duke 已提交
2554 2555 2556
  DT_VOID_RETURN_MARK(CallStaticVoidMethodA);

  JavaValue jvalue(T_VOID);
2557
  JNI_ArgumentPusherArray ap(methodID, args);
D
duke 已提交
2558 2559 2560 2561 2562 2563 2564 2565 2566
  jni_invoke_static(env, &jvalue, NULL, JNI_STATIC, methodID, &ap, CHECK);
JNI_END


//
// Accessing Fields
//


D
dcubed 已提交
2567
#ifndef USDT2
D
duke 已提交
2568
DT_RETURN_MARK_DECL(GetFieldID, jfieldID);
D
dcubed 已提交
2569 2570 2571 2572
#else /* USDT2 */
DT_RETURN_MARK_DECL(GetFieldID, jfieldID
                    , HOTSPOT_JNI_GETFIELDID_RETURN((uintptr_t)_ret_ref));
#endif /* USDT2 */
D
duke 已提交
2573 2574 2575 2576

JNI_ENTRY(jfieldID, jni_GetFieldID(JNIEnv *env, jclass clazz,
          const char *name, const char *sig))
  JNIWrapper("GetFieldID");
D
dcubed 已提交
2577
#ifndef USDT2
D
duke 已提交
2578
  DTRACE_PROBE4(hotspot_jni, GetFieldID__entry, env, clazz, name, sig);
D
dcubed 已提交
2579 2580 2581 2582
#else /* USDT2 */
  HOTSPOT_JNI_GETFIELDID_ENTRY(
                               env, clazz, (char *) name, (char *) sig);
#endif /* USDT2 */
D
duke 已提交
2583 2584 2585 2586 2587 2588
  jfieldID ret = 0;
  DT_RETURN_MARK(GetFieldID, jfieldID, (const jfieldID&)ret);

  // The class should have been loaded (we have an instance of the class
  // passed in) so the field and signature should already be in the symbol
  // table.  If they're not there, the field doesn't exist.
2589 2590 2591
  TempNewSymbol fieldname = SymbolTable::probe(name, (int)strlen(name));
  TempNewSymbol signame = SymbolTable::probe(sig, (int)strlen(sig));
  if (fieldname == NULL || signame == NULL) {
D
duke 已提交
2592 2593 2594
    THROW_MSG_0(vmSymbols::java_lang_NoSuchFieldError(), (char*) name);
  }
  KlassHandle k(THREAD,
2595
                java_lang_Class::as_Klass(JNIHandles::resolve_non_null(clazz)));
D
duke 已提交
2596
  // Make sure class is initialized before handing id's out to fields
H
hseigel 已提交
2597
  k()->initialize(CHECK_NULL);
D
duke 已提交
2598 2599

  fieldDescriptor fd;
H
hseigel 已提交
2600
  if (!k()->oop_is_instance() ||
2601
      !InstanceKlass::cast(k())->find_field(fieldname, signame, false, &fd)) {
D
duke 已提交
2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613
    THROW_MSG_0(vmSymbols::java_lang_NoSuchFieldError(), (char*) name);
  }

  // A jfieldID for a non-static field is simply the offset of the field within the instanceOop
  // It may also have hash bits for k, if VerifyJNIFields is turned on.
  ret = jfieldIDWorkaround::to_instance_jfieldID(k(), fd.offset());
  return ret;
JNI_END


JNI_ENTRY(jobject, jni_GetObjectField(JNIEnv *env, jobject obj, jfieldID fieldID))
  JNIWrapper("GetObjectField");
D
dcubed 已提交
2614
#ifndef USDT2
D
duke 已提交
2615
  DTRACE_PROBE3(hotspot_jni, GetObjectField__entry, env, obj, fieldID);
D
dcubed 已提交
2616 2617 2618 2619
#else /* USDT2 */
  HOTSPOT_JNI_GETOBJECTFIELD_ENTRY(
                                   env, obj, (uintptr_t) fieldID);
#endif /* USDT2 */
D
duke 已提交
2620
  oop o = JNIHandles::resolve_non_null(obj);
2621
  Klass* k = o->klass();
D
duke 已提交
2622 2623 2624 2625 2626 2627 2628
  int offset = jfieldIDWorkaround::from_instance_jfieldID(k, fieldID);
  // Keep JVMTI addition small and only check enabled flag here.
  // jni_GetField_probe() assumes that is okay to create handles.
  if (JvmtiExport::should_post_field_access()) {
    o = JvmtiExport::jni_GetField_probe(thread, obj, o, k, fieldID, false);
  }
  jobject ret = JNIHandles::make_local(env, o->obj_field(offset));
2629
#if INCLUDE_ALL_GCS
2630 2631 2632 2633 2634 2635 2636 2637
  // If G1 is enabled and we are accessing the value of the referent
  // field in a reference object then we need to register a non-null
  // referent with the SATB barrier.
  if (UseG1GC) {
    bool needs_barrier = false;

    if (ret != NULL &&
        offset == java_lang_ref_Reference::referent_offset &&
2638 2639
        InstanceKlass::cast(k)->reference_type() != REF_NONE) {
      assert(InstanceKlass::cast(k)->is_subclass_of(SystemDictionary::Reference_klass()), "sanity");
2640 2641 2642 2643 2644 2645 2646 2647
      needs_barrier = true;
    }

    if (needs_barrier) {
      oop referent = JNIHandles::resolve(ret);
      G1SATBCardTableModRefBS::enqueue(referent);
    }
  }
2648
#endif // INCLUDE_ALL_GCS
D
dcubed 已提交
2649
#ifndef USDT2
D
duke 已提交
2650
  DTRACE_PROBE1(hotspot_jni, GetObjectField__return, ret);
D
dcubed 已提交
2651 2652 2653 2654
#else /* USDT2 */
HOTSPOT_JNI_GETOBJECTFIELD_RETURN(
                                  ret);
#endif /* USDT2 */
D
duke 已提交
2655 2656 2657 2658
  return ret;
JNI_END


D
dcubed 已提交
2659
#ifndef USDT2
D
duke 已提交
2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671
#define DEFINE_GETFIELD(Return,Fieldname,Result) \
\
  DT_RETURN_MARK_DECL_FOR(Result, Get##Result##Field, Return);\
\
JNI_QUICK_ENTRY(Return, jni_Get##Result##Field(JNIEnv *env, jobject obj, jfieldID fieldID)) \
  JNIWrapper("Get" XSTR(Result) "Field"); \
\
  DTRACE_PROBE3(hotspot_jni, Get##Result##Field__entry, env, obj, fieldID);\
  Return ret = 0;\
  DT_RETURN_MARK_FOR(Result, Get##Result##Field, Return, (const Return&)ret);\
\
  oop o = JNIHandles::resolve_non_null(obj); \
2672
  Klass* k = o->klass(); \
D
duke 已提交
2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692
  int offset = jfieldIDWorkaround::from_instance_jfieldID(k, fieldID);  \
  /* Keep JVMTI addition small and only check enabled flag here.       */ \
  /* jni_GetField_probe_nh() assumes that is not okay to create handles */ \
  /* and creates a ResetNoHandleMark.                                   */ \
  if (JvmtiExport::should_post_field_access()) { \
    o = JvmtiExport::jni_GetField_probe_nh(thread, obj, o, k, fieldID, false); \
  } \
  ret = o->Fieldname##_field(offset); \
  return ret; \
JNI_END

DEFINE_GETFIELD(jboolean, bool,   Boolean)
DEFINE_GETFIELD(jbyte,    byte,   Byte)
DEFINE_GETFIELD(jchar,    char,   Char)
DEFINE_GETFIELD(jshort,   short,  Short)
DEFINE_GETFIELD(jint,     int,    Int)
DEFINE_GETFIELD(jlong,    long,   Long)
DEFINE_GETFIELD(jfloat,   float,  Float)
DEFINE_GETFIELD(jdouble,  double, Double)

D
dcubed 已提交
2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708
#else /* USDT2 */

#define DEFINE_GETFIELD(Return,Fieldname,Result \
  , EntryProbe, ReturnProbe) \
\
  DT_RETURN_MARK_DECL_FOR(Result, Get##Result##Field, Return \
  , ReturnProbe); \
\
JNI_QUICK_ENTRY(Return, jni_Get##Result##Field(JNIEnv *env, jobject obj, jfieldID fieldID)) \
  JNIWrapper("Get" XSTR(Result) "Field"); \
\
  EntryProbe; \
  Return ret = 0;\
  DT_RETURN_MARK_FOR(Result, Get##Result##Field, Return, (const Return&)ret);\
\
  oop o = JNIHandles::resolve_non_null(obj); \
2709
  Klass* k = o->klass(); \
D
dcubed 已提交
2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747
  int offset = jfieldIDWorkaround::from_instance_jfieldID(k, fieldID);  \
  /* Keep JVMTI addition small and only check enabled flag here.       */ \
  /* jni_GetField_probe_nh() assumes that is not okay to create handles */ \
  /* and creates a ResetNoHandleMark.                                   */ \
  if (JvmtiExport::should_post_field_access()) { \
    o = JvmtiExport::jni_GetField_probe_nh(thread, obj, o, k, fieldID, false); \
  } \
  ret = o->Fieldname##_field(offset); \
  return ret; \
JNI_END

DEFINE_GETFIELD(jboolean, bool,   Boolean
                , HOTSPOT_JNI_GETBOOLEANFIELD_ENTRY(env, obj, (uintptr_t)fieldID),
                HOTSPOT_JNI_GETBOOLEANFIELD_RETURN(_ret_ref))
DEFINE_GETFIELD(jbyte,    byte,   Byte
                , HOTSPOT_JNI_GETBYTEFIELD_ENTRY(env, obj, (uintptr_t)fieldID),
                HOTSPOT_JNI_GETBYTEFIELD_RETURN(_ret_ref))
DEFINE_GETFIELD(jchar,    char,   Char
                , HOTSPOT_JNI_GETCHARFIELD_ENTRY(env, obj, (uintptr_t)fieldID),
                HOTSPOT_JNI_GETCHARFIELD_RETURN(_ret_ref))
DEFINE_GETFIELD(jshort,   short,  Short
                , HOTSPOT_JNI_GETSHORTFIELD_ENTRY(env, obj, (uintptr_t)fieldID),
                HOTSPOT_JNI_GETSHORTFIELD_RETURN(_ret_ref))
DEFINE_GETFIELD(jint,     int,    Int
                , HOTSPOT_JNI_GETINTFIELD_ENTRY(env, obj, (uintptr_t)fieldID),
                HOTSPOT_JNI_GETINTFIELD_RETURN(_ret_ref))
DEFINE_GETFIELD(jlong,    long,   Long
                , HOTSPOT_JNI_GETLONGFIELD_ENTRY(env, obj, (uintptr_t)fieldID),
                HOTSPOT_JNI_GETLONGFIELD_RETURN(_ret_ref))
// Float and double probes don't return value because dtrace doesn't currently support it
DEFINE_GETFIELD(jfloat,   float,  Float
                , HOTSPOT_JNI_GETFLOATFIELD_ENTRY(env, obj, (uintptr_t)fieldID),
                HOTSPOT_JNI_GETFLOATFIELD_RETURN())
DEFINE_GETFIELD(jdouble,  double, Double
                , HOTSPOT_JNI_GETDOUBLEFIELD_ENTRY(env, obj, (uintptr_t)fieldID),
                HOTSPOT_JNI_GETDOUBLEFIELD_RETURN())
#endif /* USDT2 */

D
duke 已提交
2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774
address jni_GetBooleanField_addr() {
  return (address)jni_GetBooleanField;
}
address jni_GetByteField_addr() {
  return (address)jni_GetByteField;
}
address jni_GetCharField_addr() {
  return (address)jni_GetCharField;
}
address jni_GetShortField_addr() {
  return (address)jni_GetShortField;
}
address jni_GetIntField_addr() {
  return (address)jni_GetIntField;
}
address jni_GetLongField_addr() {
  return (address)jni_GetLongField;
}
address jni_GetFloatField_addr() {
  return (address)jni_GetFloatField;
}
address jni_GetDoubleField_addr() {
  return (address)jni_GetDoubleField;
}

JNI_QUICK_ENTRY(void, jni_SetObjectField(JNIEnv *env, jobject obj, jfieldID fieldID, jobject value))
  JNIWrapper("SetObjectField");
D
dcubed 已提交
2775
#ifndef USDT2
D
duke 已提交
2776
  DTRACE_PROBE4(hotspot_jni, SetObjectField__entry, env, obj, fieldID, value);
D
dcubed 已提交
2777 2778 2779 2780
#else /* USDT2 */
  HOTSPOT_JNI_SETOBJECTFIELD_ENTRY(
                                   env, obj, (uintptr_t) fieldID, value);
#endif /* USDT2 */
D
duke 已提交
2781
  oop o = JNIHandles::resolve_non_null(obj);
2782
  Klass* k = o->klass();
D
duke 已提交
2783 2784 2785 2786 2787 2788 2789 2790 2791 2792
  int offset = jfieldIDWorkaround::from_instance_jfieldID(k, fieldID);
  // Keep JVMTI addition small and only check enabled flag here.
  // jni_SetField_probe_nh() assumes that is not okay to create handles
  // and creates a ResetNoHandleMark.
  if (JvmtiExport::should_post_field_modification()) {
    jvalue field_value;
    field_value.l = value;
    o = JvmtiExport::jni_SetField_probe_nh(thread, obj, o, k, fieldID, false, 'L', (jvalue *)&field_value);
  }
  o->obj_field_put(offset, JNIHandles::resolve(value));
D
dcubed 已提交
2793
#ifndef USDT2
D
duke 已提交
2794
  DTRACE_PROBE(hotspot_jni, SetObjectField__return);
D
dcubed 已提交
2795 2796 2797 2798
#else /* USDT2 */
  HOTSPOT_JNI_SETOBJECTFIELD_RETURN(
);
#endif /* USDT2 */
D
duke 已提交
2799 2800
JNI_END

D
dcubed 已提交
2801
#ifndef USDT2
D
duke 已提交
2802 2803 2804 2805 2806
#define DEFINE_SETFIELD(Argument,Fieldname,Result,SigType,unionType) \
\
JNI_QUICK_ENTRY(void, jni_Set##Result##Field(JNIEnv *env, jobject obj, jfieldID fieldID, Argument value)) \
  JNIWrapper("Set" XSTR(Result) "Field"); \
\
2807 2808 2809
  FP_SELECT_##Result( \
    DTRACE_PROBE4(hotspot_jni, Set##Result##Field__entry, env, obj, fieldID, value), \
    DTRACE_PROBE3(hotspot_jni, Set##Result##Field__entry, env, obj, fieldID)); \
D
duke 已提交
2810 2811
\
  oop o = JNIHandles::resolve_non_null(obj); \
2812
  Klass* k = o->klass(); \
D
duke 已提交
2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834
  int offset = jfieldIDWorkaround::from_instance_jfieldID(k, fieldID);  \
  /* Keep JVMTI addition small and only check enabled flag here.       */ \
  /* jni_SetField_probe_nh() assumes that is not okay to create handles */ \
  /* and creates a ResetNoHandleMark.                                   */ \
  if (JvmtiExport::should_post_field_modification()) { \
    jvalue field_value; \
    field_value.unionType = value; \
    o = JvmtiExport::jni_SetField_probe_nh(thread, obj, o, k, fieldID, false, SigType, (jvalue *)&field_value); \
  } \
  o->Fieldname##_field_put(offset, value); \
  DTRACE_PROBE(hotspot_jni, Set##Result##Field__return);\
JNI_END

DEFINE_SETFIELD(jboolean, bool,   Boolean, 'Z', z)
DEFINE_SETFIELD(jbyte,    byte,   Byte,    'B', b)
DEFINE_SETFIELD(jchar,    char,   Char,    'C', c)
DEFINE_SETFIELD(jshort,   short,  Short,   'S', s)
DEFINE_SETFIELD(jint,     int,    Int,     'I', i)
DEFINE_SETFIELD(jlong,    long,   Long,    'J', j)
DEFINE_SETFIELD(jfloat,   float,  Float,   'F', f)
DEFINE_SETFIELD(jdouble,  double, Double,  'D', d)

D
dcubed 已提交
2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845
#else /* USDT2 */

#define DEFINE_SETFIELD(Argument,Fieldname,Result,SigType,unionType \
                        , EntryProbe, ReturnProbe) \
\
JNI_QUICK_ENTRY(void, jni_Set##Result##Field(JNIEnv *env, jobject obj, jfieldID fieldID, Argument value)) \
  JNIWrapper("Set" XSTR(Result) "Field"); \
\
  EntryProbe; \
\
  oop o = JNIHandles::resolve_non_null(obj); \
2846
  Klass* k = o->klass(); \
D
dcubed 已提交
2847 2848 2849 2850 2851 2852 2853 2854 2855
  int offset = jfieldIDWorkaround::from_instance_jfieldID(k, fieldID);  \
  /* Keep JVMTI addition small and only check enabled flag here.       */ \
  /* jni_SetField_probe_nh() assumes that is not okay to create handles */ \
  /* and creates a ResetNoHandleMark.                                   */ \
  if (JvmtiExport::should_post_field_modification()) { \
    jvalue field_value; \
    field_value.unionType = value; \
    o = JvmtiExport::jni_SetField_probe_nh(thread, obj, o, k, fieldID, false, SigType, (jvalue *)&field_value); \
  } \
K
kevinw 已提交
2856
  if (SigType == 'Z') { value = ((jboolean)value) & 1; } \
D
dcubed 已提交
2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888
  o->Fieldname##_field_put(offset, value); \
  ReturnProbe; \
JNI_END

DEFINE_SETFIELD(jboolean, bool,   Boolean, 'Z', z
                , HOTSPOT_JNI_SETBOOLEANFIELD_ENTRY(env, obj, (uintptr_t)fieldID, value),
                HOTSPOT_JNI_SETBOOLEANFIELD_RETURN())
DEFINE_SETFIELD(jbyte,    byte,   Byte,    'B', b
                , HOTSPOT_JNI_SETBYTEFIELD_ENTRY(env, obj, (uintptr_t)fieldID, value),
                HOTSPOT_JNI_SETBYTEFIELD_RETURN())
DEFINE_SETFIELD(jchar,    char,   Char,    'C', c
                , HOTSPOT_JNI_SETCHARFIELD_ENTRY(env, obj, (uintptr_t)fieldID, value),
                HOTSPOT_JNI_SETCHARFIELD_RETURN())
DEFINE_SETFIELD(jshort,   short,  Short,   'S', s
                , HOTSPOT_JNI_SETSHORTFIELD_ENTRY(env, obj, (uintptr_t)fieldID, value),
                HOTSPOT_JNI_SETSHORTFIELD_RETURN())
DEFINE_SETFIELD(jint,     int,    Int,     'I', i
                , HOTSPOT_JNI_SETINTFIELD_ENTRY(env, obj, (uintptr_t)fieldID, value),
                HOTSPOT_JNI_SETINTFIELD_RETURN())
DEFINE_SETFIELD(jlong,    long,   Long,    'J', j
                , HOTSPOT_JNI_SETLONGFIELD_ENTRY(env, obj, (uintptr_t)fieldID, value),
                HOTSPOT_JNI_SETLONGFIELD_RETURN())
// Float and double probes don't return value because dtrace doesn't currently support it
DEFINE_SETFIELD(jfloat,   float,  Float,   'F', f
                , HOTSPOT_JNI_SETFLOATFIELD_ENTRY(env, obj, (uintptr_t)fieldID),
                HOTSPOT_JNI_SETFLOATFIELD_RETURN())
DEFINE_SETFIELD(jdouble,  double, Double,  'D', d
                , HOTSPOT_JNI_SETDOUBLEFIELD_ENTRY(env, obj, (uintptr_t)fieldID),
                HOTSPOT_JNI_SETDOUBLEFIELD_RETURN())
#endif /* USDT2 */

#ifndef USDT2
D
duke 已提交
2889
DT_RETURN_MARK_DECL(ToReflectedField, jobject);
D
dcubed 已提交
2890 2891 2892 2893
#else /* USDT2 */
DT_RETURN_MARK_DECL(ToReflectedField, jobject
                    , HOTSPOT_JNI_TOREFLECTEDFIELD_RETURN(_ret_ref));
#endif /* USDT2 */
D
duke 已提交
2894 2895 2896

JNI_ENTRY(jobject, jni_ToReflectedField(JNIEnv *env, jclass cls, jfieldID fieldID, jboolean isStatic))
  JNIWrapper("ToReflectedField");
D
dcubed 已提交
2897
#ifndef USDT2
D
duke 已提交
2898 2899
  DTRACE_PROBE4(hotspot_jni, ToReflectedField__entry,
                env, cls, fieldID, isStatic);
D
dcubed 已提交
2900 2901 2902 2903
#else /* USDT2 */
  HOTSPOT_JNI_TOREFLECTEDFIELD_ENTRY(
                env, cls, (uintptr_t) fieldID, isStatic);
#endif /* USDT2 */
D
duke 已提交
2904 2905 2906 2907 2908
  jobject ret = NULL;
  DT_RETURN_MARK(ToReflectedField, jobject, (const jobject&)ret);

  fieldDescriptor fd;
  bool found = false;
2909
  Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls));
D
duke 已提交
2910 2911 2912 2913

  assert(jfieldIDWorkaround::is_static_jfieldID(fieldID) == (isStatic != 0), "invalid fieldID");

  if (isStatic) {
2914
    // Static field. The fieldID a JNIid specifying the field holder and the offset within the Klass*.
D
duke 已提交
2915 2916
    JNIid* id = jfieldIDWorkaround::from_static_jfieldID(fieldID);
    assert(id->is_static_field_id(), "invalid static field id");
2917
    found = id->find_local_field(&fd);
D
duke 已提交
2918 2919 2920
  } else {
    // Non-static field. The fieldID is really the offset of the field within the instanceOop.
    int offset = jfieldIDWorkaround::from_instance_jfieldID(k, fieldID);
2921
    found = InstanceKlass::cast(k)->find_field_from_offset(offset, false, &fd);
D
duke 已提交
2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932
  }
  assert(found, "bad fieldID passed into jni_ToReflectedField");
  oop reflected = Reflection::new_field(&fd, UseNewReflection, CHECK_NULL);
  ret = JNIHandles::make_local(env, reflected);
  return ret;
JNI_END


//
// Accessing Static Fields
//
D
dcubed 已提交
2933
#ifndef USDT2
D
duke 已提交
2934
DT_RETURN_MARK_DECL(GetStaticFieldID, jfieldID);
D
dcubed 已提交
2935 2936 2937 2938
#else /* USDT2 */
DT_RETURN_MARK_DECL(GetStaticFieldID, jfieldID
                    , HOTSPOT_JNI_GETSTATICFIELDID_RETURN((uintptr_t)_ret_ref));
#endif /* USDT2 */
D
duke 已提交
2939 2940 2941 2942

JNI_ENTRY(jfieldID, jni_GetStaticFieldID(JNIEnv *env, jclass clazz,
          const char *name, const char *sig))
  JNIWrapper("GetStaticFieldID");
D
dcubed 已提交
2943
#ifndef USDT2
D
duke 已提交
2944
  DTRACE_PROBE4(hotspot_jni, GetStaticFieldID__entry, env, clazz, name, sig);
D
dcubed 已提交
2945 2946 2947 2948
#else /* USDT2 */
  HOTSPOT_JNI_GETSTATICFIELDID_ENTRY(
                                     env, clazz, (char *) name, (char *) sig);
#endif /* USDT2 */
D
duke 已提交
2949 2950 2951 2952 2953 2954
  jfieldID ret = NULL;
  DT_RETURN_MARK(GetStaticFieldID, jfieldID, (const jfieldID&)ret);

  // The class should have been loaded (we have an instance of the class
  // passed in) so the field and signature should already be in the symbol
  // table.  If they're not there, the field doesn't exist.
2955 2956 2957
  TempNewSymbol fieldname = SymbolTable::probe(name, (int)strlen(name));
  TempNewSymbol signame = SymbolTable::probe(sig, (int)strlen(sig));
  if (fieldname == NULL || signame == NULL) {
D
duke 已提交
2958 2959 2960
    THROW_MSG_0(vmSymbols::java_lang_NoSuchFieldError(), (char*) name);
  }
  KlassHandle k(THREAD,
2961
                java_lang_Class::as_Klass(JNIHandles::resolve_non_null(clazz)));
D
duke 已提交
2962
  // Make sure class is initialized before handing id's out to static fields
H
hseigel 已提交
2963
  k()->initialize(CHECK_NULL);
D
duke 已提交
2964 2965

  fieldDescriptor fd;
H
hseigel 已提交
2966
  if (!k()->oop_is_instance() ||
2967
      !InstanceKlass::cast(k())->find_field(fieldname, signame, true, &fd)) {
D
duke 已提交
2968 2969 2970
    THROW_MSG_0(vmSymbols::java_lang_NoSuchFieldError(), (char*) name);
  }

2971
  // A jfieldID for a static field is a JNIid specifying the field holder and the offset within the Klass*
2972
  JNIid* id = fd.field_holder()->jni_id_for(fd.offset());
D
duke 已提交
2973 2974
  debug_only(id->set_is_static_field_id();)

2975
  debug_only(id->verify(fd.field_holder()));
D
duke 已提交
2976 2977 2978 2979 2980 2981 2982 2983

  ret = jfieldIDWorkaround::to_static_jfieldID(id);
  return ret;
JNI_END


JNI_ENTRY(jobject, jni_GetStaticObjectField(JNIEnv *env, jclass clazz, jfieldID fieldID))
  JNIWrapper("GetStaticObjectField");
D
dcubed 已提交
2984
#ifndef USDT2
D
duke 已提交
2985
  DTRACE_PROBE3(hotspot_jni, GetStaticObjectField__entry, env, clazz, fieldID);
D
dcubed 已提交
2986 2987 2988 2989
#else /* USDT2 */
  HOTSPOT_JNI_GETSTATICOBJECTFIELD_ENTRY(
                                         env, clazz, (uintptr_t) fieldID);
#endif /* USDT2 */
2990
#if INCLUDE_JNI_CHECK
2991
  DEBUG_ONLY(Klass* param_k = jniCheck::validate_class(thread, clazz);)
2992
#endif // INCLUDE_JNI_CHECK
D
duke 已提交
2993 2994 2995 2996 2997 2998 2999
  JNIid* id = jfieldIDWorkaround::from_static_jfieldID(fieldID);
  assert(id->is_static_field_id(), "invalid static field id");
  // Keep JVMTI addition small and only check enabled flag here.
  // jni_GetField_probe() assumes that is okay to create handles.
  if (JvmtiExport::should_post_field_access()) {
    JvmtiExport::jni_GetField_probe(thread, NULL, NULL, id->holder(), fieldID, true);
  }
3000
  jobject ret = JNIHandles::make_local(id->holder()->java_mirror()->obj_field(id->offset()));
D
dcubed 已提交
3001
#ifndef USDT2
D
duke 已提交
3002
  DTRACE_PROBE1(hotspot_jni, GetStaticObjectField__return, ret);
D
dcubed 已提交
3003 3004 3005 3006
#else /* USDT2 */
  HOTSPOT_JNI_GETSTATICOBJECTFIELD_RETURN(
                                          ret);
#endif /* USDT2 */
D
duke 已提交
3007 3008 3009
  return ret;
JNI_END

D
dcubed 已提交
3010
#ifndef USDT2
D
duke 已提交
3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027
#define DEFINE_GETSTATICFIELD(Return,Fieldname,Result) \
\
  DT_RETURN_MARK_DECL_FOR(Result, GetStatic##Result##Field, Return);\
\
JNI_ENTRY(Return, jni_GetStatic##Result##Field(JNIEnv *env, jclass clazz, jfieldID fieldID)) \
  JNIWrapper("GetStatic" XSTR(Result) "Field"); \
  DTRACE_PROBE3(hotspot_jni, GetStatic##Result##Field__entry, env, clazz, fieldID);\
  Return ret = 0;\
  DT_RETURN_MARK_FOR(Result, GetStatic##Result##Field, Return, \
                     (const Return&)ret);\
  JNIid* id = jfieldIDWorkaround::from_static_jfieldID(fieldID); \
  assert(id->is_static_field_id(), "invalid static field id"); \
  /* Keep JVMTI addition small and only check enabled flag here. */ \
  /* jni_GetField_probe() assumes that is okay to create handles. */ \
  if (JvmtiExport::should_post_field_access()) { \
    JvmtiExport::jni_GetField_probe(thread, NULL, NULL, id->holder(), fieldID, true); \
  } \
3028
  ret = id->holder()->java_mirror()-> Fieldname##_field (id->offset()); \
D
duke 已提交
3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040
  return ret;\
JNI_END

DEFINE_GETSTATICFIELD(jboolean, bool,   Boolean)
DEFINE_GETSTATICFIELD(jbyte,    byte,   Byte)
DEFINE_GETSTATICFIELD(jchar,    char,   Char)
DEFINE_GETSTATICFIELD(jshort,   short,  Short)
DEFINE_GETSTATICFIELD(jint,     int,    Int)
DEFINE_GETSTATICFIELD(jlong,    long,   Long)
DEFINE_GETSTATICFIELD(jfloat,   float,  Float)
DEFINE_GETSTATICFIELD(jdouble,  double, Double)

D
dcubed 已提交
3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083
#else /* USDT2 */

#define DEFINE_GETSTATICFIELD(Return,Fieldname,Result \
                              , EntryProbe, ReturnProbe) \
\
  DT_RETURN_MARK_DECL_FOR(Result, GetStatic##Result##Field, Return \
                          , ReturnProbe);                                          \
\
JNI_ENTRY(Return, jni_GetStatic##Result##Field(JNIEnv *env, jclass clazz, jfieldID fieldID)) \
  JNIWrapper("GetStatic" XSTR(Result) "Field"); \
  EntryProbe; \
  Return ret = 0;\
  DT_RETURN_MARK_FOR(Result, GetStatic##Result##Field, Return, \
                     (const Return&)ret);\
  JNIid* id = jfieldIDWorkaround::from_static_jfieldID(fieldID); \
  assert(id->is_static_field_id(), "invalid static field id"); \
  /* Keep JVMTI addition small and only check enabled flag here. */ \
  /* jni_GetField_probe() assumes that is okay to create handles. */ \
  if (JvmtiExport::should_post_field_access()) { \
    JvmtiExport::jni_GetField_probe(thread, NULL, NULL, id->holder(), fieldID, true); \
  } \
  ret = id->holder()->java_mirror()-> Fieldname##_field (id->offset()); \
  return ret;\
JNI_END

DEFINE_GETSTATICFIELD(jboolean, bool,   Boolean
                      , HOTSPOT_JNI_GETSTATICBOOLEANFIELD_ENTRY(env, clazz, (uintptr_t) fieldID), HOTSPOT_JNI_GETSTATICBOOLEANFIELD_RETURN(_ret_ref))
DEFINE_GETSTATICFIELD(jbyte,    byte,   Byte
                      , HOTSPOT_JNI_GETSTATICBYTEFIELD_ENTRY(env, clazz, (uintptr_t) fieldID),    HOTSPOT_JNI_GETSTATICBYTEFIELD_RETURN(_ret_ref)   )
DEFINE_GETSTATICFIELD(jchar,    char,   Char
                      , HOTSPOT_JNI_GETSTATICCHARFIELD_ENTRY(env, clazz, (uintptr_t) fieldID),    HOTSPOT_JNI_GETSTATICCHARFIELD_RETURN(_ret_ref)   )
DEFINE_GETSTATICFIELD(jshort,   short,  Short
                      , HOTSPOT_JNI_GETSTATICSHORTFIELD_ENTRY(env, clazz, (uintptr_t) fieldID),   HOTSPOT_JNI_GETSTATICSHORTFIELD_RETURN(_ret_ref)  )
DEFINE_GETSTATICFIELD(jint,     int,    Int
                      , HOTSPOT_JNI_GETSTATICINTFIELD_ENTRY(env, clazz, (uintptr_t) fieldID),     HOTSPOT_JNI_GETSTATICINTFIELD_RETURN(_ret_ref)    )
DEFINE_GETSTATICFIELD(jlong,    long,   Long
                      , HOTSPOT_JNI_GETSTATICLONGFIELD_ENTRY(env, clazz, (uintptr_t) fieldID),    HOTSPOT_JNI_GETSTATICLONGFIELD_RETURN(_ret_ref)   )
// Float and double probes don't return value because dtrace doesn't currently support it
DEFINE_GETSTATICFIELD(jfloat,   float,  Float
                      , HOTSPOT_JNI_GETSTATICFLOATFIELD_ENTRY(env, clazz, (uintptr_t) fieldID),   HOTSPOT_JNI_GETSTATICFLOATFIELD_RETURN()          )
DEFINE_GETSTATICFIELD(jdouble,  double, Double
                      , HOTSPOT_JNI_GETSTATICDOUBLEFIELD_ENTRY(env, clazz, (uintptr_t) fieldID),  HOTSPOT_JNI_GETSTATICDOUBLEFIELD_RETURN()         )
#endif /* USDT2 */
D
duke 已提交
3084 3085 3086

JNI_ENTRY(void, jni_SetStaticObjectField(JNIEnv *env, jclass clazz, jfieldID fieldID, jobject value))
  JNIWrapper("SetStaticObjectField");
D
dcubed 已提交
3087
#ifndef USDT2
D
duke 已提交
3088
  DTRACE_PROBE4(hotspot_jni, SetStaticObjectField__entry, env, clazz, fieldID, value);
D
dcubed 已提交
3089 3090 3091 3092
#else /* USDT2 */
 HOTSPOT_JNI_SETSTATICOBJECTFIELD_ENTRY(
                                        env, clazz, (uintptr_t) fieldID, value);
#endif /* USDT2 */
D
duke 已提交
3093 3094 3095 3096 3097 3098 3099 3100 3101
  JNIid* id = jfieldIDWorkaround::from_static_jfieldID(fieldID);
  assert(id->is_static_field_id(), "invalid static field id");
  // Keep JVMTI addition small and only check enabled flag here.
  // jni_SetField_probe() assumes that is okay to create handles.
  if (JvmtiExport::should_post_field_modification()) {
    jvalue field_value;
    field_value.l = value;
    JvmtiExport::jni_SetField_probe(thread, NULL, NULL, id->holder(), fieldID, true, 'L', (jvalue *)&field_value);
  }
3102
  id->holder()->java_mirror()->obj_field_put(id->offset(), JNIHandles::resolve(value));
D
dcubed 已提交
3103
#ifndef USDT2
D
duke 已提交
3104
  DTRACE_PROBE(hotspot_jni, SetStaticObjectField__return);
D
dcubed 已提交
3105 3106 3107 3108
#else /* USDT2 */
  HOTSPOT_JNI_SETSTATICOBJECTFIELD_RETURN(
                                          );
#endif /* USDT2 */
D
duke 已提交
3109 3110 3111
JNI_END


D
dcubed 已提交
3112
#ifndef USDT2
D
duke 已提交
3113 3114 3115 3116
#define DEFINE_SETSTATICFIELD(Argument,Fieldname,Result,SigType,unionType) \
\
JNI_ENTRY(void, jni_SetStatic##Result##Field(JNIEnv *env, jclass clazz, jfieldID fieldID, Argument value)) \
  JNIWrapper("SetStatic" XSTR(Result) "Field"); \
3117 3118 3119
  FP_SELECT_##Result( \
     DTRACE_PROBE4(hotspot_jni, SetStatic##Result##Field__entry, env, clazz, fieldID, value), \
     DTRACE_PROBE3(hotspot_jni, SetStatic##Result##Field__entry, env, clazz, fieldID)); \
D
duke 已提交
3120 3121 3122 3123 3124 3125 3126 3127 3128 3129
\
  JNIid* id = jfieldIDWorkaround::from_static_jfieldID(fieldID); \
  assert(id->is_static_field_id(), "invalid static field id"); \
  /* Keep JVMTI addition small and only check enabled flag here. */ \
  /* jni_SetField_probe() assumes that is okay to create handles. */ \
  if (JvmtiExport::should_post_field_modification()) { \
    jvalue field_value; \
    field_value.unionType = value; \
    JvmtiExport::jni_SetField_probe(thread, NULL, NULL, id->holder(), fieldID, true, SigType, (jvalue *)&field_value); \
  } \
3130
  id->holder()->java_mirror()-> Fieldname##_field_put (id->offset(), value); \
D
duke 已提交
3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142
  DTRACE_PROBE(hotspot_jni, SetStatic##Result##Field__return);\
JNI_END

DEFINE_SETSTATICFIELD(jboolean, bool,   Boolean, 'Z', z)
DEFINE_SETSTATICFIELD(jbyte,    byte,   Byte,    'B', b)
DEFINE_SETSTATICFIELD(jchar,    char,   Char,    'C', c)
DEFINE_SETSTATICFIELD(jshort,   short,  Short,   'S', s)
DEFINE_SETSTATICFIELD(jint,     int,    Int,     'I', i)
DEFINE_SETSTATICFIELD(jlong,    long,   Long,    'J', j)
DEFINE_SETSTATICFIELD(jfloat,   float,  Float,   'F', f)
DEFINE_SETSTATICFIELD(jdouble,  double, Double,  'D', d)

D
dcubed 已提交
3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160
#else /* USDT2 */

#define DEFINE_SETSTATICFIELD(Argument,Fieldname,Result,SigType,unionType \
                              , EntryProbe, ReturnProbe) \
\
JNI_ENTRY(void, jni_SetStatic##Result##Field(JNIEnv *env, jclass clazz, jfieldID fieldID, Argument value)) \
  JNIWrapper("SetStatic" XSTR(Result) "Field"); \
  EntryProbe; \
\
  JNIid* id = jfieldIDWorkaround::from_static_jfieldID(fieldID); \
  assert(id->is_static_field_id(), "invalid static field id"); \
  /* Keep JVMTI addition small and only check enabled flag here. */ \
  /* jni_SetField_probe() assumes that is okay to create handles. */ \
  if (JvmtiExport::should_post_field_modification()) { \
    jvalue field_value; \
    field_value.unionType = value; \
    JvmtiExport::jni_SetField_probe(thread, NULL, NULL, id->holder(), fieldID, true, SigType, (jvalue *)&field_value); \
  } \
K
kevinw 已提交
3161
  if (SigType == 'Z') { value = ((jboolean)value) & 1; } \
D
dcubed 已提交
3162 3163 3164 3165 3166
  id->holder()->java_mirror()-> Fieldname##_field_put (id->offset(), value); \
  ReturnProbe;\
JNI_END

DEFINE_SETSTATICFIELD(jboolean, bool,   Boolean, 'Z', z
3167
                      , HOTSPOT_JNI_SETSTATICBOOLEANFIELD_ENTRY(env, clazz, (uintptr_t)fieldID, value),
D
dcubed 已提交
3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191
                      HOTSPOT_JNI_SETBOOLEANFIELD_RETURN())
DEFINE_SETSTATICFIELD(jbyte,    byte,   Byte,    'B', b
                      , HOTSPOT_JNI_SETSTATICBYTEFIELD_ENTRY(env, clazz, (uintptr_t) fieldID, value),
                      HOTSPOT_JNI_SETSTATICBYTEFIELD_RETURN())
DEFINE_SETSTATICFIELD(jchar,    char,   Char,    'C', c
                      , HOTSPOT_JNI_SETSTATICCHARFIELD_ENTRY(env, clazz, (uintptr_t) fieldID, value),
                      HOTSPOT_JNI_SETSTATICCHARFIELD_RETURN())
DEFINE_SETSTATICFIELD(jshort,   short,  Short,   'S', s
                      , HOTSPOT_JNI_SETSTATICSHORTFIELD_ENTRY(env, clazz, (uintptr_t) fieldID, value),
                      HOTSPOT_JNI_SETSTATICSHORTFIELD_RETURN())
DEFINE_SETSTATICFIELD(jint,     int,    Int,     'I', i
                      , HOTSPOT_JNI_SETSTATICINTFIELD_ENTRY(env, clazz, (uintptr_t) fieldID, value),
                      HOTSPOT_JNI_SETSTATICINTFIELD_RETURN())
DEFINE_SETSTATICFIELD(jlong,    long,   Long,    'J', j
                      , HOTSPOT_JNI_SETSTATICLONGFIELD_ENTRY(env, clazz, (uintptr_t) fieldID, value),
                      HOTSPOT_JNI_SETSTATICLONGFIELD_RETURN())
// Float and double probes don't return value because dtrace doesn't currently support it
DEFINE_SETSTATICFIELD(jfloat,   float,  Float,   'F', f
                      , HOTSPOT_JNI_SETSTATICFLOATFIELD_ENTRY(env, clazz, (uintptr_t) fieldID),
                      HOTSPOT_JNI_SETSTATICFLOATFIELD_RETURN())
DEFINE_SETSTATICFIELD(jdouble,  double, Double,  'D', d
                      , HOTSPOT_JNI_SETSTATICDOUBLEFIELD_ENTRY(env, clazz, (uintptr_t) fieldID),
                      HOTSPOT_JNI_SETSTATICDOUBLEFIELD_RETURN())
#endif /* USDT2 */
D
duke 已提交
3192 3193 3194 3195 3196 3197 3198

//
// String Operations
//

// Unicode Interface

D
dcubed 已提交
3199
#ifndef USDT2
D
duke 已提交
3200
DT_RETURN_MARK_DECL(NewString, jstring);
D
dcubed 已提交
3201 3202 3203 3204
#else /* USDT2 */
DT_RETURN_MARK_DECL(NewString, jstring
                    , HOTSPOT_JNI_NEWSTRING_RETURN(_ret_ref));
#endif /* USDT2 */
D
duke 已提交
3205 3206 3207

JNI_ENTRY(jstring, jni_NewString(JNIEnv *env, const jchar *unicodeChars, jsize len))
  JNIWrapper("NewString");
D
dcubed 已提交
3208
#ifndef USDT2
D
duke 已提交
3209
  DTRACE_PROBE3(hotspot_jni, NewString__entry, env, unicodeChars, len);
D
dcubed 已提交
3210 3211 3212 3213
#else /* USDT2 */
 HOTSPOT_JNI_NEWSTRING_ENTRY(
                             env, (uint16_t *) unicodeChars, len);
#endif /* USDT2 */
D
duke 已提交
3214 3215 3216 3217 3218 3219 3220 3221 3222 3223
  jstring ret = NULL;
  DT_RETURN_MARK(NewString, jstring, (const jstring&)ret);
  oop string=java_lang_String::create_oop_from_unicode((jchar*) unicodeChars, len, CHECK_NULL);
  ret = (jstring) JNIHandles::make_local(env, string);
  return ret;
JNI_END


JNI_QUICK_ENTRY(jsize, jni_GetStringLength(JNIEnv *env, jstring string))
  JNIWrapper("GetStringLength");
D
dcubed 已提交
3224
#ifndef USDT2
D
duke 已提交
3225
  DTRACE_PROBE2(hotspot_jni, GetStringLength__entry, env, string);
D
dcubed 已提交
3226 3227 3228 3229
#else /* USDT2 */
  HOTSPOT_JNI_GETSTRINGLENGTH_ENTRY(
                                    env, string);
#endif /* USDT2 */
3230 3231 3232 3233 3234
  jsize ret = 0;
  oop s = JNIHandles::resolve_non_null(string);
  if (java_lang_String::value(s) != NULL) {
    ret = java_lang_String::length(s);
  }
D
dcubed 已提交
3235
#ifndef USDT2
D
duke 已提交
3236
  DTRACE_PROBE1(hotspot_jni, GetStringLength__return, ret);
D
dcubed 已提交
3237 3238 3239 3240
#else /* USDT2 */
 HOTSPOT_JNI_GETSTRINGLENGTH_RETURN(
                                    ret);
#endif /* USDT2 */
D
duke 已提交
3241 3242 3243 3244 3245 3246 3247
  return ret;
JNI_END


JNI_QUICK_ENTRY(const jchar*, jni_GetStringChars(
  JNIEnv *env, jstring string, jboolean *isCopy))
  JNIWrapper("GetStringChars");
D
dcubed 已提交
3248
#ifndef USDT2
D
duke 已提交
3249
  DTRACE_PROBE3(hotspot_jni, GetStringChars__entry, env, string, isCopy);
D
dcubed 已提交
3250 3251 3252 3253
#else /* USDT2 */
 HOTSPOT_JNI_GETSTRINGCHARS_ENTRY(
                                  env, string, (uintptr_t *) isCopy);
#endif /* USDT2 */
3254
  jchar* buf = NULL;
D
duke 已提交
3255 3256
  oop s = JNIHandles::resolve_non_null(string);
  typeArrayOop s_value = java_lang_String::value(s);
3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270
  if (s_value != NULL) {
    int s_len = java_lang_String::length(s);
    int s_offset = java_lang_String::offset(s);
    buf = NEW_C_HEAP_ARRAY_RETURN_NULL(jchar, s_len + 1, mtInternal);  // add one for zero termination
    /* JNI Specification states return NULL on OOM */
    if (buf != NULL) {
      if (s_len > 0) {
        memcpy(buf, s_value->char_at_addr(s_offset), sizeof(jchar)*s_len);
      }
      buf[s_len] = 0;
      //%note jni_5
      if (isCopy != NULL) {
        *isCopy = JNI_TRUE;
      }
3271
    }
D
duke 已提交
3272
  }
D
dcubed 已提交
3273
#ifndef USDT2
D
duke 已提交
3274
  DTRACE_PROBE1(hotspot_jni, GetStringChars__return, buf);
D
dcubed 已提交
3275 3276 3277 3278
#else /* USDT2 */
  HOTSPOT_JNI_GETSTRINGCHARS_RETURN(
                                    buf);
#endif /* USDT2 */
D
duke 已提交
3279 3280 3281 3282 3283 3284
  return buf;
JNI_END


JNI_QUICK_ENTRY(void, jni_ReleaseStringChars(JNIEnv *env, jstring str, const jchar *chars))
  JNIWrapper("ReleaseStringChars");
D
dcubed 已提交
3285
#ifndef USDT2
D
duke 已提交
3286
  DTRACE_PROBE3(hotspot_jni, ReleaseStringChars__entry, env, str, chars);
D
dcubed 已提交
3287 3288 3289 3290
#else /* USDT2 */
  HOTSPOT_JNI_RELEASESTRINGCHARS_ENTRY(
                                       env, str, (uint16_t *) chars);
#endif /* USDT2 */
D
duke 已提交
3291 3292 3293 3294 3295 3296
  //%note jni_6
  if (chars != NULL) {
    // Since String objects are supposed to be immutable, don't copy any
    // new data back.  A bad user will have to go after the char array.
    FreeHeap((void*) chars);
  }
D
dcubed 已提交
3297
#ifndef USDT2
D
duke 已提交
3298
  DTRACE_PROBE(hotspot_jni, ReleaseStringChars__return);
D
dcubed 已提交
3299 3300 3301 3302
#else /* USDT2 */
  HOTSPOT_JNI_RELEASESTRINGCHARS_RETURN(
);
#endif /* USDT2 */
D
duke 已提交
3303 3304 3305 3306 3307
JNI_END


// UTF Interface

D
dcubed 已提交
3308
#ifndef USDT2
D
duke 已提交
3309
DT_RETURN_MARK_DECL(NewStringUTF, jstring);
D
dcubed 已提交
3310 3311 3312 3313
#else /* USDT2 */
DT_RETURN_MARK_DECL(NewStringUTF, jstring
                    , HOTSPOT_JNI_NEWSTRINGUTF_RETURN(_ret_ref));
#endif /* USDT2 */
D
duke 已提交
3314 3315 3316

JNI_ENTRY(jstring, jni_NewStringUTF(JNIEnv *env, const char *bytes))
  JNIWrapper("NewStringUTF");
D
dcubed 已提交
3317
#ifndef USDT2
D
duke 已提交
3318
  DTRACE_PROBE2(hotspot_jni, NewStringUTF__entry, env, bytes);
D
dcubed 已提交
3319 3320 3321 3322
#else /* USDT2 */
  HOTSPOT_JNI_NEWSTRINGUTF_ENTRY(
                                 env, (char *) bytes);
#endif /* USDT2 */
D
duke 已提交
3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333
  jstring ret;
  DT_RETURN_MARK(NewStringUTF, jstring, (const jstring&)ret);

  oop result = java_lang_String::create_oop_from_str((char*) bytes, CHECK_NULL);
  ret = (jstring) JNIHandles::make_local(env, result);
  return ret;
JNI_END


JNI_ENTRY(jsize, jni_GetStringUTFLength(JNIEnv *env, jstring string))
  JNIWrapper("GetStringUTFLength");
D
dcubed 已提交
3334
#ifndef USDT2
D
duke 已提交
3335
  DTRACE_PROBE2(hotspot_jni, GetStringUTFLength__entry, env, string);
D
dcubed 已提交
3336 3337 3338 3339
#else /* USDT2 */
 HOTSPOT_JNI_GETSTRINGUTFLENGTH_ENTRY(
                                      env, string);
#endif /* USDT2 */
3340 3341 3342 3343 3344
  jsize ret = 0;
  oop java_string = JNIHandles::resolve_non_null(string);
  if (java_lang_String::value(java_string) != NULL) {
    ret = java_lang_String::utf8_length(java_string);
  }
D
dcubed 已提交
3345
#ifndef USDT2
D
duke 已提交
3346
  DTRACE_PROBE1(hotspot_jni, GetStringUTFLength__return, ret);
D
dcubed 已提交
3347 3348 3349 3350
#else /* USDT2 */
  HOTSPOT_JNI_GETSTRINGUTFLENGTH_RETURN(
                                        ret);
#endif /* USDT2 */
D
duke 已提交
3351 3352 3353 3354 3355 3356
  return ret;
JNI_END


JNI_ENTRY(const char*, jni_GetStringUTFChars(JNIEnv *env, jstring string, jboolean *isCopy))
  JNIWrapper("GetStringUTFChars");
D
dcubed 已提交
3357
#ifndef USDT2
D
duke 已提交
3358
  DTRACE_PROBE3(hotspot_jni, GetStringUTFChars__entry, env, string, isCopy);
D
dcubed 已提交
3359 3360 3361 3362
#else /* USDT2 */
 HOTSPOT_JNI_GETSTRINGUTFCHARS_ENTRY(
                                     env, string, (uintptr_t *) isCopy);
#endif /* USDT2 */
3363
  char* result = NULL;
3364
  oop java_string = JNIHandles::resolve_non_null(string);
3365 3366 3367 3368 3369 3370 3371 3372 3373
  if (java_lang_String::value(java_string) != NULL) {
    size_t length = java_lang_String::utf8_length(java_string);
    /* JNI Specification states return NULL on OOM */
    result = AllocateHeap(length + 1, mtInternal, 0, AllocFailStrategy::RETURN_NULL);
    if (result != NULL) {
      java_lang_String::as_utf8_string(java_string, result, (int) length + 1);
      if (isCopy != NULL) {
        *isCopy = JNI_TRUE;
      }
3374 3375
    }
  }
D
dcubed 已提交
3376
#ifndef USDT2
D
duke 已提交
3377
  DTRACE_PROBE1(hotspot_jni, GetStringUTFChars__return, result);
D
dcubed 已提交
3378 3379 3380 3381
#else /* USDT2 */
 HOTSPOT_JNI_GETSTRINGUTFCHARS_RETURN(
                                      result);
#endif /* USDT2 */
D
duke 已提交
3382 3383 3384 3385 3386 3387
  return result;
JNI_END


JNI_LEAF(void, jni_ReleaseStringUTFChars(JNIEnv *env, jstring str, const char *chars))
  JNIWrapper("ReleaseStringUTFChars");
D
dcubed 已提交
3388
#ifndef USDT2
D
duke 已提交
3389
  DTRACE_PROBE3(hotspot_jni, ReleaseStringUTFChars__entry, env, str, chars);
D
dcubed 已提交
3390 3391 3392 3393
#else /* USDT2 */
 HOTSPOT_JNI_RELEASESTRINGUTFCHARS_ENTRY(
                                         env, str, (char *) chars);
#endif /* USDT2 */
D
duke 已提交
3394 3395 3396
  if (chars != NULL) {
    FreeHeap((char*) chars);
  }
D
dcubed 已提交
3397
#ifndef USDT2
D
duke 已提交
3398
  DTRACE_PROBE(hotspot_jni, ReleaseStringUTFChars__return);
D
dcubed 已提交
3399 3400 3401 3402
#else /* USDT2 */
HOTSPOT_JNI_RELEASESTRINGUTFCHARS_RETURN(
);
#endif /* USDT2 */
D
duke 已提交
3403 3404 3405 3406 3407
JNI_END


JNI_QUICK_ENTRY(jsize, jni_GetArrayLength(JNIEnv *env, jarray array))
  JNIWrapper("GetArrayLength");
D
dcubed 已提交
3408
#ifndef USDT2
D
duke 已提交
3409
  DTRACE_PROBE2(hotspot_jni, GetArrayLength__entry, env, array);
D
dcubed 已提交
3410 3411 3412 3413
#else /* USDT2 */
 HOTSPOT_JNI_GETARRAYLENGTH_ENTRY(
                                  env, array);
#endif /* USDT2 */
D
duke 已提交
3414 3415 3416
  arrayOop a = arrayOop(JNIHandles::resolve_non_null(array));
  assert(a->is_array(), "must be array");
  jsize ret = a->length();
D
dcubed 已提交
3417
#ifndef USDT2
D
duke 已提交
3418
  DTRACE_PROBE1(hotspot_jni, GetArrayLength__return, ret);
D
dcubed 已提交
3419 3420 3421 3422
#else /* USDT2 */
 HOTSPOT_JNI_GETARRAYLENGTH_RETURN(
                                   ret);
#endif /* USDT2 */
D
duke 已提交
3423 3424 3425 3426 3427 3428 3429 3430
  return ret;
JNI_END


//
// Object Array Operations
//

D
dcubed 已提交
3431
#ifndef USDT2
D
duke 已提交
3432
DT_RETURN_MARK_DECL(NewObjectArray, jobjectArray);
D
dcubed 已提交
3433 3434 3435 3436
#else /* USDT2 */
DT_RETURN_MARK_DECL(NewObjectArray, jobjectArray
                    , HOTSPOT_JNI_NEWOBJECTARRAY_RETURN(_ret_ref));
#endif /* USDT2 */
D
duke 已提交
3437 3438 3439

JNI_ENTRY(jobjectArray, jni_NewObjectArray(JNIEnv *env, jsize length, jclass elementClass, jobject initialElement))
  JNIWrapper("NewObjectArray");
D
dcubed 已提交
3440
#ifndef USDT2
D
duke 已提交
3441
  DTRACE_PROBE4(hotspot_jni, NewObjectArray__entry, env, length, elementClass, initialElement);
D
dcubed 已提交
3442 3443 3444 3445
#else /* USDT2 */
 HOTSPOT_JNI_NEWOBJECTARRAY_ENTRY(
                                  env, length, elementClass, initialElement);
#endif /* USDT2 */
D
duke 已提交
3446 3447
  jobjectArray ret = NULL;
  DT_RETURN_MARK(NewObjectArray, jobjectArray, (const jobjectArray&)ret);
3448
  KlassHandle ek(THREAD, java_lang_Class::as_Klass(JNIHandles::resolve_non_null(elementClass)));
H
hseigel 已提交
3449
  Klass* ako = ek()->array_klass(CHECK_NULL);
D
duke 已提交
3450
  KlassHandle ak = KlassHandle(THREAD, ako);
3451 3452
  ObjArrayKlass::cast(ak())->initialize(CHECK_NULL);
  objArrayOop result = ObjArrayKlass::cast(ak())->allocate(length, CHECK_NULL);
D
duke 已提交
3453 3454 3455 3456 3457 3458 3459 3460 3461 3462
  oop initial_value = JNIHandles::resolve(initialElement);
  if (initial_value != NULL) {  // array already initialized with NULL
    for (int index = 0; index < length; index++) {
      result->obj_at_put(index, initial_value);
    }
  }
  ret = (jobjectArray) JNIHandles::make_local(env, result);
  return ret;
JNI_END

D
dcubed 已提交
3463
#ifndef USDT2
D
duke 已提交
3464
DT_RETURN_MARK_DECL(GetObjectArrayElement, jobject);
D
dcubed 已提交
3465 3466 3467 3468
#else /* USDT2 */
DT_RETURN_MARK_DECL(GetObjectArrayElement, jobject
                    , HOTSPOT_JNI_GETOBJECTARRAYELEMENT_RETURN(_ret_ref));
#endif /* USDT2 */
D
duke 已提交
3469 3470 3471

JNI_ENTRY(jobject, jni_GetObjectArrayElement(JNIEnv *env, jobjectArray array, jsize index))
  JNIWrapper("GetObjectArrayElement");
D
dcubed 已提交
3472
#ifndef USDT2
D
duke 已提交
3473
  DTRACE_PROBE3(hotspot_jni, GetObjectArrayElement__entry, env, array, index);
D
dcubed 已提交
3474 3475 3476 3477
#else /* USDT2 */
 HOTSPOT_JNI_GETOBJECTARRAYELEMENT_ENTRY(
                                         env, array, index);
#endif /* USDT2 */
D
duke 已提交
3478 3479 3480 3481
  jobject ret = NULL;
  DT_RETURN_MARK(GetObjectArrayElement, jobject, (const jobject&)ret);
  objArrayOop a = objArrayOop(JNIHandles::resolve_non_null(array));
  if (a->is_within_bounds(index)) {
3482
    ret = JNIHandles::make_local(env, a->obj_at(index));
D
duke 已提交
3483 3484 3485 3486 3487 3488 3489 3490
    return ret;
  } else {
    char buf[jintAsStringSize];
    sprintf(buf, "%d", index);
    THROW_MSG_0(vmSymbols::java_lang_ArrayIndexOutOfBoundsException(), buf);
  }
JNI_END

D
dcubed 已提交
3491
#ifndef USDT2
D
duke 已提交
3492
DT_VOID_RETURN_MARK_DECL(SetObjectArrayElement);
D
dcubed 已提交
3493 3494 3495 3496
#else /* USDT2 */
DT_VOID_RETURN_MARK_DECL(SetObjectArrayElement
                         , HOTSPOT_JNI_SETOBJECTARRAYELEMENT_RETURN());
#endif /* USDT2 */
D
duke 已提交
3497 3498 3499

JNI_ENTRY(void, jni_SetObjectArrayElement(JNIEnv *env, jobjectArray array, jsize index, jobject value))
  JNIWrapper("SetObjectArrayElement");
D
dcubed 已提交
3500
#ifndef USDT2
D
duke 已提交
3501
  DTRACE_PROBE4(hotspot_jni, SetObjectArrayElement__entry, env, array, index, value);
D
dcubed 已提交
3502 3503 3504 3505
#else /* USDT2 */
 HOTSPOT_JNI_SETOBJECTARRAYELEMENT_ENTRY(
                                         env, array, index, value);
#endif /* USDT2 */
D
duke 已提交
3506 3507 3508 3509 3510
  DT_VOID_RETURN_MARK(SetObjectArrayElement);

  objArrayOop a = objArrayOop(JNIHandles::resolve_non_null(array));
  oop v = JNIHandles::resolve(value);
  if (a->is_within_bounds(index)) {
3511
    if (v == NULL || v->is_a(ObjArrayKlass::cast(a->klass())->element_klass())) {
D
duke 已提交
3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523
      a->obj_at_put(index, v);
    } else {
      THROW(vmSymbols::java_lang_ArrayStoreException());
    }
  } else {
    char buf[jintAsStringSize];
    sprintf(buf, "%d", index);
    THROW_MSG(vmSymbols::java_lang_ArrayIndexOutOfBoundsException(), buf);
  }
JNI_END


D
dcubed 已提交
3524
#ifndef USDT2
D
duke 已提交
3525 3526
#define DEFINE_NEWSCALARARRAY(Return,Allocator,Result) \
\
D
dcubed 已提交
3527
  DT_RETURN_MARK_DECL(New##Result##Array, Return); \
D
duke 已提交
3528 3529 3530 3531 3532 3533
\
JNI_ENTRY(Return, \
          jni_New##Result##Array(JNIEnv *env, jsize len)) \
  JNIWrapper("New" XSTR(Result) "Array"); \
  DTRACE_PROBE2(hotspot_jni, New##Result##Array__entry, env, len);\
  Return ret = NULL;\
3534
  DT_RETURN_MARK(New##Result##Array, Return, (const Return&)ret);\
D
duke 已提交
3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549
\
  oop obj= oopFactory::Allocator(len, CHECK_0); \
  ret = (Return) JNIHandles::make_local(env, obj); \
  return ret;\
JNI_END

DEFINE_NEWSCALARARRAY(jbooleanArray, new_boolArray,   Boolean)
DEFINE_NEWSCALARARRAY(jbyteArray,    new_byteArray,   Byte)
DEFINE_NEWSCALARARRAY(jshortArray,   new_shortArray,  Short)
DEFINE_NEWSCALARARRAY(jcharArray,    new_charArray,   Char)
DEFINE_NEWSCALARARRAY(jintArray,     new_intArray,    Int)
DEFINE_NEWSCALARARRAY(jlongArray,    new_longArray,   Long)
DEFINE_NEWSCALARARRAY(jfloatArray,   new_singleArray, Float)
DEFINE_NEWSCALARARRAY(jdoubleArray,  new_doubleArray, Double)

D
dcubed 已提交
3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594
#else /* USDT2 */

#define DEFINE_NEWSCALARARRAY(Return,Allocator,Result \
                              ,EntryProbe,ReturnProbe)  \
\
  DT_RETURN_MARK_DECL(New##Result##Array, Return \
                      , ReturnProbe); \
\
JNI_ENTRY(Return, \
          jni_New##Result##Array(JNIEnv *env, jsize len)) \
  JNIWrapper("New" XSTR(Result) "Array"); \
  EntryProbe; \
  Return ret = NULL;\
  DT_RETURN_MARK(New##Result##Array, Return, (const Return&)ret);\
\
  oop obj= oopFactory::Allocator(len, CHECK_0); \
  ret = (Return) JNIHandles::make_local(env, obj); \
  return ret;\
JNI_END

DEFINE_NEWSCALARARRAY(jbooleanArray, new_boolArray,   Boolean,
                      HOTSPOT_JNI_NEWBOOLEANARRAY_ENTRY(env, len),
                      HOTSPOT_JNI_NEWBOOLEANARRAY_RETURN(_ret_ref))
DEFINE_NEWSCALARARRAY(jbyteArray,    new_byteArray,   Byte,
                      HOTSPOT_JNI_NEWBYTEARRAY_ENTRY(env, len),
                      HOTSPOT_JNI_NEWBYTEARRAY_RETURN(_ret_ref))
DEFINE_NEWSCALARARRAY(jshortArray,   new_shortArray,  Short,
                      HOTSPOT_JNI_NEWSHORTARRAY_ENTRY(env, len),
                      HOTSPOT_JNI_NEWSHORTARRAY_RETURN(_ret_ref))
DEFINE_NEWSCALARARRAY(jcharArray,    new_charArray,   Char,
                      HOTSPOT_JNI_NEWCHARARRAY_ENTRY(env, len),
                      HOTSPOT_JNI_NEWCHARARRAY_RETURN(_ret_ref))
DEFINE_NEWSCALARARRAY(jintArray,     new_intArray,    Int,
                      HOTSPOT_JNI_NEWINTARRAY_ENTRY(env, len),
                      HOTSPOT_JNI_NEWINTARRAY_RETURN(_ret_ref))
DEFINE_NEWSCALARARRAY(jlongArray,    new_longArray,   Long,
                      HOTSPOT_JNI_NEWLONGARRAY_ENTRY(env, len),
                      HOTSPOT_JNI_NEWLONGARRAY_RETURN(_ret_ref))
DEFINE_NEWSCALARARRAY(jfloatArray,   new_singleArray, Float,
                      HOTSPOT_JNI_NEWFLOATARRAY_ENTRY(env, len),
                      HOTSPOT_JNI_NEWFLOATARRAY_RETURN(_ret_ref))
DEFINE_NEWSCALARARRAY(jdoubleArray,  new_doubleArray, Double,
                      HOTSPOT_JNI_NEWDOUBLEARRAY_ENTRY(env, len),
                      HOTSPOT_JNI_NEWDOUBLEARRAY_RETURN(_ret_ref))
#endif /* USDT2 */
D
duke 已提交
3595 3596 3597 3598 3599 3600 3601 3602 3603

// Return an address which will fault if the caller writes to it.

static char* get_bad_address() {
  static char* bad_address = NULL;
  if (bad_address == NULL) {
    size_t size = os::vm_allocation_granularity();
    bad_address = os::reserve_memory(size);
    if (bad_address != NULL) {
3604 3605
      os::protect_memory(bad_address, size, os::MEM_PROT_READ,
                         /*is_committed*/false);
3606
      MemTracker::record_virtual_memory_type((void*)bad_address, mtInternal);
D
duke 已提交
3607 3608 3609 3610 3611 3612
    }
  }
  return bad_address;
}


D
dcubed 已提交
3613
#ifndef USDT2
D
duke 已提交
3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629
#define DEFINE_GETSCALARARRAYELEMENTS(ElementTag,ElementType,Result, Tag) \
\
JNI_QUICK_ENTRY(ElementType*, \
          jni_Get##Result##ArrayElements(JNIEnv *env, ElementType##Array array, jboolean *isCopy)) \
  JNIWrapper("Get" XSTR(Result) "ArrayElements"); \
  DTRACE_PROBE3(hotspot_jni, Get##Result##ArrayElements__entry, env, array, isCopy);\
  /* allocate an chunk of memory in c land */ \
  typeArrayOop a = typeArrayOop(JNIHandles::resolve_non_null(array)); \
  ElementType* result; \
  int len = a->length(); \
  if (len == 0) { \
    /* Empty array: legal but useless, can't return NULL. \
     * Return a pointer to something useless. \
     * Avoid asserts in typeArrayOop. */ \
    result = (ElementType*)get_bad_address(); \
  } else { \
3630 3631 3632 3633 3634 3635 3636 3637 3638
    /* JNI Specification states return NULL on OOM */                    \
    result = NEW_C_HEAP_ARRAY_RETURN_NULL(ElementType, len, mtInternal); \
    if (result != NULL) {                                                \
      /* copy the array to the c chunk */                                \
      memcpy(result, a->Tag##_at_addr(0), sizeof(ElementType)*len);      \
      if (isCopy) {                                                      \
        *isCopy = JNI_TRUE;                                              \
      }                                                                  \
    }                                                                    \
D
duke 已提交
3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652
  } \
  DTRACE_PROBE1(hotspot_jni, Get##Result##ArrayElements__return, result);\
  return result; \
JNI_END

DEFINE_GETSCALARARRAYELEMENTS(T_BOOLEAN, jboolean, Boolean, bool)
DEFINE_GETSCALARARRAYELEMENTS(T_BYTE,    jbyte,    Byte,    byte)
DEFINE_GETSCALARARRAYELEMENTS(T_SHORT,   jshort,   Short,   short)
DEFINE_GETSCALARARRAYELEMENTS(T_CHAR,    jchar,    Char,    char)
DEFINE_GETSCALARARRAYELEMENTS(T_INT,     jint,     Int,     int)
DEFINE_GETSCALARARRAYELEMENTS(T_LONG,    jlong,    Long,    long)
DEFINE_GETSCALARARRAYELEMENTS(T_FLOAT,   jfloat,   Float,   float)
DEFINE_GETSCALARARRAYELEMENTS(T_DOUBLE,  jdouble,  Double,  double)

D
dcubed 已提交
3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671
#else /* USDT2 */

#define DEFINE_GETSCALARARRAYELEMENTS(ElementTag,ElementType,Result, Tag \
                                      , EntryProbe, ReturnProbe) \
\
JNI_QUICK_ENTRY(ElementType*, \
          jni_Get##Result##ArrayElements(JNIEnv *env, ElementType##Array array, jboolean *isCopy)) \
  JNIWrapper("Get" XSTR(Result) "ArrayElements"); \
  EntryProbe; \
  /* allocate an chunk of memory in c land */ \
  typeArrayOop a = typeArrayOop(JNIHandles::resolve_non_null(array)); \
  ElementType* result; \
  int len = a->length(); \
  if (len == 0) { \
    /* Empty array: legal but useless, can't return NULL. \
     * Return a pointer to something useless. \
     * Avoid asserts in typeArrayOop. */ \
    result = (ElementType*)get_bad_address(); \
  } else { \
3672 3673 3674 3675 3676 3677 3678 3679 3680
    /* JNI Specification states return NULL on OOM */                    \
    result = NEW_C_HEAP_ARRAY_RETURN_NULL(ElementType, len, mtInternal); \
    if (result != NULL) {                                                \
      /* copy the array to the c chunk */                                \
      memcpy(result, a->Tag##_at_addr(0), sizeof(ElementType)*len);      \
      if (isCopy) {                                                      \
        *isCopy = JNI_TRUE;                                              \
      }                                                                  \
    }                                                                    \
D
dcubed 已提交
3681 3682 3683 3684
  } \
  ReturnProbe; \
  return result; \
JNI_END
D
duke 已提交
3685

D
dcubed 已提交
3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713
DEFINE_GETSCALARARRAYELEMENTS(T_BOOLEAN, jboolean, Boolean, bool
                              , HOTSPOT_JNI_GETBOOLEANARRAYELEMENTS_ENTRY(env, array, (uintptr_t *) isCopy),
                              HOTSPOT_JNI_GETBOOLEANARRAYELEMENTS_RETURN((uintptr_t*)result))
DEFINE_GETSCALARARRAYELEMENTS(T_BYTE,    jbyte,    Byte,    byte
                              , HOTSPOT_JNI_GETBYTEARRAYELEMENTS_ENTRY(env, array, (uintptr_t *) isCopy),
                              HOTSPOT_JNI_GETBYTEARRAYELEMENTS_RETURN((char*)result))
DEFINE_GETSCALARARRAYELEMENTS(T_SHORT,   jshort,   Short,   short
                              , HOTSPOT_JNI_GETSHORTARRAYELEMENTS_ENTRY(env, (uint16_t*) array, (uintptr_t *) isCopy),
                              HOTSPOT_JNI_GETSHORTARRAYELEMENTS_RETURN((uint16_t*)result))
DEFINE_GETSCALARARRAYELEMENTS(T_CHAR,    jchar,    Char,    char
                              , HOTSPOT_JNI_GETCHARARRAYELEMENTS_ENTRY(env, (uint16_t*) array, (uintptr_t *) isCopy),
                              HOTSPOT_JNI_GETCHARARRAYELEMENTS_RETURN(result))
DEFINE_GETSCALARARRAYELEMENTS(T_INT,     jint,     Int,     int
                              , HOTSPOT_JNI_GETINTARRAYELEMENTS_ENTRY(env, array, (uintptr_t *) isCopy),
                              HOTSPOT_JNI_GETINTARRAYELEMENTS_RETURN((uint32_t*)result))
DEFINE_GETSCALARARRAYELEMENTS(T_LONG,    jlong,    Long,    long
                              , HOTSPOT_JNI_GETLONGARRAYELEMENTS_ENTRY(env, array, (uintptr_t *) isCopy),
                              HOTSPOT_JNI_GETLONGARRAYELEMENTS_RETURN(((uintptr_t*)result)))
// Float and double probes don't return value because dtrace doesn't currently support it
DEFINE_GETSCALARARRAYELEMENTS(T_FLOAT,   jfloat,   Float,   float
                              , HOTSPOT_JNI_GETFLOATARRAYELEMENTS_ENTRY(env, array, (uintptr_t *) isCopy),
                              HOTSPOT_JNI_GETFLOATARRAYELEMENTS_RETURN(result))
DEFINE_GETSCALARARRAYELEMENTS(T_DOUBLE,  jdouble,  Double,  double
                              , HOTSPOT_JNI_GETDOUBLEARRAYELEMENTS_ENTRY(env, array, (uintptr_t *) isCopy),
                              HOTSPOT_JNI_GETDOUBLEARRAYELEMENTS_RETURN(result))
#endif /* USDT2 */

#ifndef USDT2
D
duke 已提交
3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742
#define DEFINE_RELEASESCALARARRAYELEMENTS(ElementTag,ElementType,Result,Tag) \
\
JNI_QUICK_ENTRY(void, \
          jni_Release##Result##ArrayElements(JNIEnv *env, ElementType##Array array, \
                                             ElementType *buf, jint mode)) \
  JNIWrapper("Release" XSTR(Result) "ArrayElements"); \
  DTRACE_PROBE4(hotspot_jni, Release##Result##ArrayElements__entry, env, array, buf, mode);\
  typeArrayOop a = typeArrayOop(JNIHandles::resolve_non_null(array)); \
  int len = a->length(); \
  if (len != 0) {   /* Empty array:  nothing to free or copy. */  \
    if ((mode == 0) || (mode == JNI_COMMIT)) { \
      memcpy(a->Tag##_at_addr(0), buf, sizeof(ElementType)*len); \
    } \
    if ((mode == 0) || (mode == JNI_ABORT)) { \
      FreeHeap(buf); \
    } \
  } \
  DTRACE_PROBE(hotspot_jni, Release##Result##ArrayElements__return);\
JNI_END

DEFINE_RELEASESCALARARRAYELEMENTS(T_BOOLEAN, jboolean, Boolean, bool)
DEFINE_RELEASESCALARARRAYELEMENTS(T_BYTE,    jbyte,    Byte,    byte)
DEFINE_RELEASESCALARARRAYELEMENTS(T_SHORT,   jshort,   Short,   short)
DEFINE_RELEASESCALARARRAYELEMENTS(T_CHAR,    jchar,    Char,    char)
DEFINE_RELEASESCALARARRAYELEMENTS(T_INT,     jint,     Int,     int)
DEFINE_RELEASESCALARARRAYELEMENTS(T_LONG,    jlong,    Long,    long)
DEFINE_RELEASESCALARARRAYELEMENTS(T_FLOAT,   jfloat,   Float,   float)
DEFINE_RELEASESCALARARRAYELEMENTS(T_DOUBLE,  jdouble,  Double,  double)

D
dcubed 已提交
3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792
#else /* USDT2 */

#define DEFINE_RELEASESCALARARRAYELEMENTS(ElementTag,ElementType,Result,Tag \
                                          , EntryProbe, ReturnProbe);\
\
JNI_QUICK_ENTRY(void, \
          jni_Release##Result##ArrayElements(JNIEnv *env, ElementType##Array array, \
                                             ElementType *buf, jint mode)) \
  JNIWrapper("Release" XSTR(Result) "ArrayElements"); \
  EntryProbe; \
  typeArrayOop a = typeArrayOop(JNIHandles::resolve_non_null(array)); \
  int len = a->length(); \
  if (len != 0) {   /* Empty array:  nothing to free or copy. */  \
    if ((mode == 0) || (mode == JNI_COMMIT)) { \
      memcpy(a->Tag##_at_addr(0), buf, sizeof(ElementType)*len); \
    } \
    if ((mode == 0) || (mode == JNI_ABORT)) { \
      FreeHeap(buf); \
    } \
  } \
  ReturnProbe; \
JNI_END

DEFINE_RELEASESCALARARRAYELEMENTS(T_BOOLEAN, jboolean, Boolean, bool
                                  , HOTSPOT_JNI_RELEASEBOOLEANARRAYELEMENTS_ENTRY(env, array, (uintptr_t *) buf, mode),
                                  HOTSPOT_JNI_RELEASEBOOLEANARRAYELEMENTS_RETURN())
DEFINE_RELEASESCALARARRAYELEMENTS(T_BYTE,    jbyte,    Byte,    byte
                                  , HOTSPOT_JNI_RELEASEBYTEARRAYELEMENTS_ENTRY(env, array, (char *) buf, mode),
                                  HOTSPOT_JNI_RELEASEBYTEARRAYELEMENTS_RETURN())
DEFINE_RELEASESCALARARRAYELEMENTS(T_SHORT,   jshort,   Short,   short
                                  ,  HOTSPOT_JNI_RELEASESHORTARRAYELEMENTS_ENTRY(env, array, (uint16_t *) buf, mode),
                                  HOTSPOT_JNI_RELEASESHORTARRAYELEMENTS_RETURN())
DEFINE_RELEASESCALARARRAYELEMENTS(T_CHAR,    jchar,    Char,    char
                                  ,  HOTSPOT_JNI_RELEASECHARARRAYELEMENTS_ENTRY(env, array, (uint16_t *) buf, mode),
                                  HOTSPOT_JNI_RELEASECHARARRAYELEMENTS_RETURN())
DEFINE_RELEASESCALARARRAYELEMENTS(T_INT,     jint,     Int,     int
                                  , HOTSPOT_JNI_RELEASEINTARRAYELEMENTS_ENTRY(env, array, (uint32_t *) buf, mode),
                                  HOTSPOT_JNI_RELEASEINTARRAYELEMENTS_RETURN())
DEFINE_RELEASESCALARARRAYELEMENTS(T_LONG,    jlong,    Long,    long
                                  , HOTSPOT_JNI_RELEASELONGARRAYELEMENTS_ENTRY(env, array, (uintptr_t *) buf, mode),
                                  HOTSPOT_JNI_RELEASELONGARRAYELEMENTS_RETURN())
DEFINE_RELEASESCALARARRAYELEMENTS(T_FLOAT,   jfloat,   Float,   float
                                  , HOTSPOT_JNI_RELEASEFLOATARRAYELEMENTS_ENTRY(env, array, (float *) buf, mode),
                                  HOTSPOT_JNI_RELEASEFLOATARRAYELEMENTS_RETURN())
DEFINE_RELEASESCALARARRAYELEMENTS(T_DOUBLE,  jdouble,  Double,  double
                                  , HOTSPOT_JNI_RELEASEDOUBLEARRAYELEMENTS_ENTRY(env, array, (double *) buf, mode),
                                  HOTSPOT_JNI_RELEASEDOUBLEARRAYELEMENTS_RETURN())
#endif /* USDT2 */

#ifndef USDT2
D
duke 已提交
3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806
#define DEFINE_GETSCALARARRAYREGION(ElementTag,ElementType,Result, Tag) \
  DT_VOID_RETURN_MARK_DECL(Get##Result##ArrayRegion);\
\
JNI_ENTRY(void, \
jni_Get##Result##ArrayRegion(JNIEnv *env, ElementType##Array array, jsize start, \
             jsize len, ElementType *buf)) \
  JNIWrapper("Get" XSTR(Result) "ArrayRegion"); \
  DTRACE_PROBE5(hotspot_jni, Get##Result##ArrayRegion__entry, env, array, start, len, buf);\
  DT_VOID_RETURN_MARK(Get##Result##ArrayRegion); \
  typeArrayOop src = typeArrayOop(JNIHandles::resolve_non_null(array)); \
  if (start < 0 || len < 0 || ((unsigned int)start + (unsigned int)len > (unsigned int)src->length())) { \
    THROW(vmSymbols::java_lang_ArrayIndexOutOfBoundsException()); \
  } else { \
    if (len > 0) { \
3807
      int sc = TypeArrayKlass::cast(src->klass())->log2_element_size(); \
D
duke 已提交
3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823
      memcpy((u_char*) buf, \
             (u_char*) src->Tag##_at_addr(start), \
             len << sc);                          \
    } \
  } \
JNI_END

DEFINE_GETSCALARARRAYREGION(T_BOOLEAN, jboolean,Boolean, bool)
DEFINE_GETSCALARARRAYREGION(T_BYTE,    jbyte,   Byte,    byte)
DEFINE_GETSCALARARRAYREGION(T_SHORT,   jshort,  Short,   short)
DEFINE_GETSCALARARRAYREGION(T_CHAR,    jchar,   Char,    char)
DEFINE_GETSCALARARRAYREGION(T_INT,     jint,    Int,     int)
DEFINE_GETSCALARARRAYREGION(T_LONG,    jlong,   Long,    long)
DEFINE_GETSCALARARRAYREGION(T_FLOAT,   jfloat,  Float,   float)
DEFINE_GETSCALARARRAYREGION(T_DOUBLE,  jdouble, Double,  double)

D
dcubed 已提交
3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837
#else /* USDT2 */

#define DEFINE_GETSCALARARRAYREGION(ElementTag,ElementType,Result, Tag \
                                    , EntryProbe, ReturnProbe); \
  DT_VOID_RETURN_MARK_DECL(Get##Result##ArrayRegion \
                           , ReturnProbe); \
\
JNI_ENTRY(void, \
jni_Get##Result##ArrayRegion(JNIEnv *env, ElementType##Array array, jsize start, \
             jsize len, ElementType *buf)) \
  JNIWrapper("Get" XSTR(Result) "ArrayRegion"); \
  EntryProbe; \
  DT_VOID_RETURN_MARK(Get##Result##ArrayRegion); \
  typeArrayOop src = typeArrayOop(JNIHandles::resolve_non_null(array)); \
3838
  if (start < 0 || len < 0 || (start > src->length() - len)) { \
D
dcubed 已提交
3839 3840 3841
    THROW(vmSymbols::java_lang_ArrayIndexOutOfBoundsException()); \
  } else { \
    if (len > 0) { \
3842
      int sc = TypeArrayKlass::cast(src->klass())->log2_element_size(); \
D
dcubed 已提交
3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876
      memcpy((u_char*) buf, \
             (u_char*) src->Tag##_at_addr(start), \
             len << sc);                          \
    } \
  } \
JNI_END

DEFINE_GETSCALARARRAYREGION(T_BOOLEAN, jboolean,Boolean, bool
                            , HOTSPOT_JNI_GETBOOLEANARRAYREGION_ENTRY(env, array, start, len, (uintptr_t *) buf),
                            HOTSPOT_JNI_GETBOOLEANARRAYREGION_RETURN());
DEFINE_GETSCALARARRAYREGION(T_BYTE,    jbyte,   Byte,    byte
                            ,  HOTSPOT_JNI_GETBYTEARRAYREGION_ENTRY(env, array, start, len, (char *) buf),
                            HOTSPOT_JNI_GETBYTEARRAYREGION_RETURN());
DEFINE_GETSCALARARRAYREGION(T_SHORT,   jshort,  Short,   short
                            , HOTSPOT_JNI_GETSHORTARRAYREGION_ENTRY(env, array, start, len, (uint16_t *) buf),
                            HOTSPOT_JNI_GETSHORTARRAYREGION_RETURN());
DEFINE_GETSCALARARRAYREGION(T_CHAR,    jchar,   Char,    char
                            ,  HOTSPOT_JNI_GETCHARARRAYREGION_ENTRY(env, array, start, len, (uint16_t*) buf),
                            HOTSPOT_JNI_GETCHARARRAYREGION_RETURN());
DEFINE_GETSCALARARRAYREGION(T_INT,     jint,    Int,     int
                            , HOTSPOT_JNI_GETINTARRAYREGION_ENTRY(env, array, start, len, (uint32_t*) buf),
                            HOTSPOT_JNI_GETINTARRAYREGION_RETURN());
DEFINE_GETSCALARARRAYREGION(T_LONG,    jlong,   Long,    long
                            ,  HOTSPOT_JNI_GETLONGARRAYREGION_ENTRY(env, array, start, len, (uintptr_t *) buf),
                            HOTSPOT_JNI_GETLONGARRAYREGION_RETURN());
DEFINE_GETSCALARARRAYREGION(T_FLOAT,   jfloat,  Float,   float
                            , HOTSPOT_JNI_GETFLOATARRAYREGION_ENTRY(env, array, start, len, (float *) buf),
                            HOTSPOT_JNI_GETFLOATARRAYREGION_RETURN());
DEFINE_GETSCALARARRAYREGION(T_DOUBLE,  jdouble, Double,  double
                            , HOTSPOT_JNI_GETDOUBLEARRAYREGION_ENTRY(env, array, start, len, (double *) buf),
                            HOTSPOT_JNI_GETDOUBLEARRAYREGION_RETURN());
#endif /* USDT2 */

#ifndef USDT2
D
duke 已提交
3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890
#define DEFINE_SETSCALARARRAYREGION(ElementTag,ElementType,Result, Tag) \
  DT_VOID_RETURN_MARK_DECL(Set##Result##ArrayRegion);\
\
JNI_ENTRY(void, \
jni_Set##Result##ArrayRegion(JNIEnv *env, ElementType##Array array, jsize start, \
             jsize len, const ElementType *buf)) \
  JNIWrapper("Set" XSTR(Result) "ArrayRegion"); \
  DTRACE_PROBE5(hotspot_jni, Set##Result##ArrayRegion__entry, env, array, start, len, buf);\
  DT_VOID_RETURN_MARK(Set##Result##ArrayRegion); \
  typeArrayOop dst = typeArrayOop(JNIHandles::resolve_non_null(array)); \
  if (start < 0 || len < 0 || ((unsigned int)start + (unsigned int)len > (unsigned int)dst->length())) { \
    THROW(vmSymbols::java_lang_ArrayIndexOutOfBoundsException()); \
  } else { \
    if (len > 0) { \
3891
      int sc = TypeArrayKlass::cast(dst->klass())->log2_element_size(); \
D
duke 已提交
3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907
      memcpy((u_char*) dst->Tag##_at_addr(start), \
             (u_char*) buf, \
             len << sc);    \
    } \
  } \
JNI_END

DEFINE_SETSCALARARRAYREGION(T_BOOLEAN, jboolean, Boolean, bool)
DEFINE_SETSCALARARRAYREGION(T_BYTE,    jbyte,    Byte,    byte)
DEFINE_SETSCALARARRAYREGION(T_SHORT,   jshort,   Short,   short)
DEFINE_SETSCALARARRAYREGION(T_CHAR,    jchar,    Char,    char)
DEFINE_SETSCALARARRAYREGION(T_INT,     jint,     Int,     int)
DEFINE_SETSCALARARRAYREGION(T_LONG,    jlong,    Long,    long)
DEFINE_SETSCALARARRAYREGION(T_FLOAT,   jfloat,   Float,   float)
DEFINE_SETSCALARARRAYREGION(T_DOUBLE,  jdouble,  Double,  double)

D
dcubed 已提交
3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921
#else /* USDT2 */

#define DEFINE_SETSCALARARRAYREGION(ElementTag,ElementType,Result, Tag \
                                    , EntryProbe, ReturnProbe); \
  DT_VOID_RETURN_MARK_DECL(Set##Result##ArrayRegion \
                           ,ReturnProbe);           \
\
JNI_ENTRY(void, \
jni_Set##Result##ArrayRegion(JNIEnv *env, ElementType##Array array, jsize start, \
             jsize len, const ElementType *buf)) \
  JNIWrapper("Set" XSTR(Result) "ArrayRegion"); \
  EntryProbe; \
  DT_VOID_RETURN_MARK(Set##Result##ArrayRegion); \
  typeArrayOop dst = typeArrayOop(JNIHandles::resolve_non_null(array)); \
3922
  if (start < 0 || len < 0 || (start > dst->length() - len)) { \
D
dcubed 已提交
3923 3924 3925
    THROW(vmSymbols::java_lang_ArrayIndexOutOfBoundsException()); \
  } else { \
    if (len > 0) { \
3926
      int sc = TypeArrayKlass::cast(dst->klass())->log2_element_size(); \
D
dcubed 已提交
3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959
      memcpy((u_char*) dst->Tag##_at_addr(start), \
             (u_char*) buf, \
             len << sc);    \
    } \
  } \
JNI_END

DEFINE_SETSCALARARRAYREGION(T_BOOLEAN, jboolean, Boolean, bool
                            , HOTSPOT_JNI_SETBOOLEANARRAYREGION_ENTRY(env, array, start, len, (uintptr_t *)buf),
                            HOTSPOT_JNI_SETBOOLEANARRAYREGION_RETURN())
DEFINE_SETSCALARARRAYREGION(T_BYTE,    jbyte,    Byte,    byte
                            , HOTSPOT_JNI_SETBYTEARRAYREGION_ENTRY(env, array, start, len, (char *) buf),
                            HOTSPOT_JNI_SETBYTEARRAYREGION_RETURN())
DEFINE_SETSCALARARRAYREGION(T_SHORT,   jshort,   Short,   short
                            , HOTSPOT_JNI_SETSHORTARRAYREGION_ENTRY(env, array, start, len, (uint16_t *) buf),
                            HOTSPOT_JNI_SETSHORTARRAYREGION_RETURN())
DEFINE_SETSCALARARRAYREGION(T_CHAR,    jchar,    Char,    char
                            , HOTSPOT_JNI_SETCHARARRAYREGION_ENTRY(env, array, start, len, (uint16_t *) buf),
                            HOTSPOT_JNI_SETCHARARRAYREGION_RETURN())
DEFINE_SETSCALARARRAYREGION(T_INT,     jint,     Int,     int
                            , HOTSPOT_JNI_SETINTARRAYREGION_ENTRY(env, array, start, len, (uint32_t *) buf),
                            HOTSPOT_JNI_SETINTARRAYREGION_RETURN())
DEFINE_SETSCALARARRAYREGION(T_LONG,    jlong,    Long,    long
                            , HOTSPOT_JNI_SETLONGARRAYREGION_ENTRY(env, array, start, len, (uintptr_t *) buf),
                            HOTSPOT_JNI_SETLONGARRAYREGION_RETURN())
DEFINE_SETSCALARARRAYREGION(T_FLOAT,   jfloat,   Float,   float
                            , HOTSPOT_JNI_SETFLOATARRAYREGION_ENTRY(env, array, start, len, (float *) buf),
                            HOTSPOT_JNI_SETFLOATARRAYREGION_RETURN())
DEFINE_SETSCALARARRAYREGION(T_DOUBLE,  jdouble,  Double,  double
                            , HOTSPOT_JNI_SETDOUBLEARRAYREGION_ENTRY(env, array, start, len, (double *) buf),
                            HOTSPOT_JNI_SETDOUBLEARRAYREGION_RETURN())
#endif /* USDT2 */

D
duke 已提交
3960 3961 3962 3963 3964 3965 3966 3967 3968

//
// Interception of natives
//

// The RegisterNatives call being attempted tried to register with a method that
// is not native.  Ask JVM TI what prefixes have been specified.  Then check
// to see if the native method is now wrapped with the prefixes.  See the
// SetNativeMethodPrefix(es) functions in the JVM TI Spec for details.
3969
static Method* find_prefixed_native(KlassHandle k,
3970
                                      Symbol* name, Symbol* signature, TRAPS) {
3971
#if INCLUDE_JVMTI
D
duke 已提交
3972
  ResourceMark rm(THREAD);
3973
  Method* method;
D
duke 已提交
3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986
  int name_len = name->utf8_length();
  char* name_str = name->as_utf8();
  int prefix_count;
  char** prefixes = JvmtiExport::get_all_native_method_prefixes(&prefix_count);
  for (int i = 0; i < prefix_count; i++) {
    char* prefix = prefixes[i];
    int prefix_len = (int)strlen(prefix);

    // try adding this prefix to the method name and see if it matches another method name
    int trial_len = name_len + prefix_len;
    char* trial_name_str = NEW_RESOURCE_ARRAY(char, trial_len + 1);
    strcpy(trial_name_str, prefix);
    strcat(trial_name_str, name_str);
3987 3988
    TempNewSymbol trial_name = SymbolTable::probe(trial_name_str, trial_len);
    if (trial_name == NULL) {
D
duke 已提交
3989 3990
      continue; // no such symbol, so this prefix wasn't used, try the next prefix
    }
H
hseigel 已提交
3991
    method = k()->lookup_method(trial_name, signature);
D
duke 已提交
3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002
    if (method == NULL) {
      continue; // signature doesn't match, try the next prefix
    }
    if (method->is_native()) {
      method->set_is_prefixed_native();
      return method; // wahoo, we found a prefixed version of the method, return it
    }
    // found as non-native, so prefix is good, add it, probably just need more prefixes
    name_len = trial_len;
    name_str = trial_name_str;
  }
4003
#endif // INCLUDE_JVMTI
D
duke 已提交
4004 4005 4006
  return NULL; // not found
}

4007
static bool register_native(KlassHandle k, Symbol* name, Symbol* signature, address entry, TRAPS) {
H
hseigel 已提交
4008
  Method* method = k()->lookup_method(name, signature);
D
duke 已提交
4009 4010 4011 4012
  if (method == NULL) {
    ResourceMark rm;
    stringStream st;
    st.print("Method %s name or signature does not match",
H
hseigel 已提交
4013
             Method::name_and_sig_as_C_string(k(), name, signature));
D
duke 已提交
4014 4015 4016 4017 4018 4019 4020 4021 4022
    THROW_MSG_(vmSymbols::java_lang_NoSuchMethodError(), st.as_string(), false);
  }
  if (!method->is_native()) {
    // trying to register to a non-native method, see if a JVM TI agent has added prefix(es)
    method = find_prefixed_native(k, name, signature, THREAD);
    if (method == NULL) {
      ResourceMark rm;
      stringStream st;
      st.print("Method %s is not declared as native",
H
hseigel 已提交
4023
               Method::name_and_sig_as_C_string(k(), name, signature));
D
duke 已提交
4024 4025 4026 4027 4028 4029
      THROW_MSG_(vmSymbols::java_lang_NoSuchMethodError(), st.as_string(), false);
    }
  }

  if (entry != NULL) {
    method->set_native_function(entry,
4030
      Method::native_bind_event_is_interesting);
D
duke 已提交
4031 4032 4033 4034 4035 4036
  } else {
    method->clear_native_function();
  }
  if (PrintJNIResolving) {
    ResourceMark rm(THREAD);
    tty->print_cr("[Registering JNI native method %s.%s]",
4037
      method->method_holder()->external_name(),
D
duke 已提交
4038 4039 4040 4041 4042
      method->name()->as_C_string());
  }
  return true;
}

D
dcubed 已提交
4043
#ifndef USDT2
D
duke 已提交
4044
DT_RETURN_MARK_DECL(RegisterNatives, jint);
D
dcubed 已提交
4045 4046 4047 4048
#else /* USDT2 */
DT_RETURN_MARK_DECL(RegisterNatives, jint
                    , HOTSPOT_JNI_REGISTERNATIVES_RETURN(_ret_ref));
#endif /* USDT2 */
D
duke 已提交
4049 4050 4051 4052 4053

JNI_ENTRY(jint, jni_RegisterNatives(JNIEnv *env, jclass clazz,
                                    const JNINativeMethod *methods,
                                    jint nMethods))
  JNIWrapper("RegisterNatives");
D
dcubed 已提交
4054
#ifndef USDT2
D
duke 已提交
4055
  DTRACE_PROBE4(hotspot_jni, RegisterNatives__entry, env, clazz, methods, nMethods);
D
dcubed 已提交
4056 4057 4058 4059
#else /* USDT2 */
  HOTSPOT_JNI_REGISTERNATIVES_ENTRY(
                                    env, clazz, (void *) methods, nMethods);
#endif /* USDT2 */
D
duke 已提交
4060 4061 4062
  jint ret = 0;
  DT_RETURN_MARK(RegisterNatives, jint, (const jint&)ret);

4063
  KlassHandle h_k(thread, java_lang_Class::as_Klass(JNIHandles::resolve_non_null(clazz)));
D
duke 已提交
4064 4065 4066 4067 4068 4069 4070 4071 4072

  for (int index = 0; index < nMethods; index++) {
    const char* meth_name = methods[index].name;
    const char* meth_sig = methods[index].signature;
    int meth_name_len = (int)strlen(meth_name);

    // The class should have been loaded (we have an instance of the class
    // passed in) so the method and signature should already be in the symbol
    // table.  If they're not there, the method doesn't exist.
4073 4074
    TempNewSymbol  name = SymbolTable::probe(meth_name, meth_name_len);
    TempNewSymbol  signature = SymbolTable::probe(meth_sig, (int)strlen(meth_sig));
D
duke 已提交
4075

4076
    if (name == NULL || signature == NULL) {
D
duke 已提交
4077 4078
      ResourceMark rm;
      stringStream st;
H
hseigel 已提交
4079
      st.print("Method %s.%s%s not found", h_k()->external_name(), meth_name, meth_sig);
D
duke 已提交
4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095 4096
      // Must return negative value on failure
      THROW_MSG_(vmSymbols::java_lang_NoSuchMethodError(), st.as_string(), -1);
    }

    bool res = register_native(h_k, name, signature,
                               (address) methods[index].fnPtr, THREAD);
    if (!res) {
      ret = -1;
      break;
    }
  }
  return ret;
JNI_END


JNI_ENTRY(jint, jni_UnregisterNatives(JNIEnv *env, jclass clazz))
  JNIWrapper("UnregisterNatives");
D
dcubed 已提交
4097
#ifndef USDT2
D
duke 已提交
4098
  DTRACE_PROBE2(hotspot_jni, UnregisterNatives__entry, env, clazz);
D
dcubed 已提交
4099 4100 4101 4102
#else /* USDT2 */
 HOTSPOT_JNI_UNREGISTERNATIVES_ENTRY(
                                     env, clazz);
#endif /* USDT2 */
4103
  Klass* k   = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(clazz));
D
duke 已提交
4104
  //%note jni_2
H
hseigel 已提交
4105
  if (k->oop_is_instance()) {
4106 4107
    for (int index = 0; index < InstanceKlass::cast(k)->methods()->length(); index++) {
      Method* m = InstanceKlass::cast(k)->methods()->at(index);
D
duke 已提交
4108 4109 4110 4111 4112 4113
      if (m->is_native()) {
        m->clear_native_function();
        m->set_signature_handler(NULL);
      }
    }
  }
D
dcubed 已提交
4114
#ifndef USDT2
D
duke 已提交
4115
  DTRACE_PROBE1(hotspot_jni, UnregisterNatives__return, 0);
D
dcubed 已提交
4116 4117 4118 4119
#else /* USDT2 */
 HOTSPOT_JNI_UNREGISTERNATIVES_RETURN(
                                      0);
#endif /* USDT2 */
D
duke 已提交
4120 4121 4122 4123 4124 4125 4126
  return 0;
JNI_END

//
// Monitor functions
//

D
dcubed 已提交
4127
#ifndef USDT2
D
duke 已提交
4128
DT_RETURN_MARK_DECL(MonitorEnter, jint);
D
dcubed 已提交
4129 4130 4131 4132
#else /* USDT2 */
DT_RETURN_MARK_DECL(MonitorEnter, jint
                    , HOTSPOT_JNI_MONITORENTER_RETURN(_ret_ref));
#endif /* USDT2 */
D
duke 已提交
4133 4134

JNI_ENTRY(jint, jni_MonitorEnter(JNIEnv *env, jobject jobj))
D
dcubed 已提交
4135
#ifndef USDT2
D
duke 已提交
4136
  DTRACE_PROBE2(hotspot_jni, MonitorEnter__entry, env, jobj);
D
dcubed 已提交
4137 4138 4139 4140
#else /* USDT2 */
 HOTSPOT_JNI_MONITORENTER_ENTRY(
                                env, jobj);
#endif /* USDT2 */
D
duke 已提交
4141 4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154
  jint ret = JNI_ERR;
  DT_RETURN_MARK(MonitorEnter, jint, (const jint&)ret);

  // If the object is null, we can't do anything with it
  if (jobj == NULL) {
    THROW_(vmSymbols::java_lang_NullPointerException(), JNI_ERR);
  }

  Handle obj(thread, JNIHandles::resolve_non_null(jobj));
  ObjectSynchronizer::jni_enter(obj, CHECK_(JNI_ERR));
  ret = JNI_OK;
  return ret;
JNI_END

D
dcubed 已提交
4155
#ifndef USDT2
D
duke 已提交
4156
DT_RETURN_MARK_DECL(MonitorExit, jint);
D
dcubed 已提交
4157 4158 4159 4160
#else /* USDT2 */
DT_RETURN_MARK_DECL(MonitorExit, jint
                    , HOTSPOT_JNI_MONITOREXIT_RETURN(_ret_ref));
#endif /* USDT2 */
D
duke 已提交
4161 4162

JNI_ENTRY(jint, jni_MonitorExit(JNIEnv *env, jobject jobj))
D
dcubed 已提交
4163
#ifndef USDT2
D
duke 已提交
4164
  DTRACE_PROBE2(hotspot_jni, MonitorExit__entry, env, jobj);
D
dcubed 已提交
4165 4166 4167 4168
#else /* USDT2 */
 HOTSPOT_JNI_MONITOREXIT_ENTRY(
                               env, jobj);
#endif /* USDT2 */
D
duke 已提交
4169 4170 4171 4172 4173 4174 4175 4176 4177 4178 4179 4180 4181 4182 4183 4184 4185 4186 4187
  jint ret = JNI_ERR;
  DT_RETURN_MARK(MonitorExit, jint, (const jint&)ret);

  // Don't do anything with a null object
  if (jobj == NULL) {
    THROW_(vmSymbols::java_lang_NullPointerException(), JNI_ERR);
  }

  Handle obj(THREAD, JNIHandles::resolve_non_null(jobj));
  ObjectSynchronizer::jni_exit(obj(), CHECK_(JNI_ERR));

  ret = JNI_OK;
  return ret;
JNI_END

//
// Extensions
//

D
dcubed 已提交
4188
#ifndef USDT2
D
duke 已提交
4189
DT_VOID_RETURN_MARK_DECL(GetStringRegion);
D
dcubed 已提交
4190 4191 4192 4193
#else /* USDT2 */
DT_VOID_RETURN_MARK_DECL(GetStringRegion
                         , HOTSPOT_JNI_GETSTRINGREGION_RETURN());
#endif /* USDT2 */
D
duke 已提交
4194 4195 4196

JNI_ENTRY(void, jni_GetStringRegion(JNIEnv *env, jstring string, jsize start, jsize len, jchar *buf))
  JNIWrapper("GetStringRegion");
D
dcubed 已提交
4197
#ifndef USDT2
D
duke 已提交
4198
  DTRACE_PROBE5(hotspot_jni, GetStringRegion__entry, env, string, start, len, buf);
D
dcubed 已提交
4199 4200 4201 4202
#else /* USDT2 */
 HOTSPOT_JNI_GETSTRINGREGION_ENTRY(
                                   env, string, start, len, buf);
#endif /* USDT2 */
D
duke 已提交
4203 4204 4205
  DT_VOID_RETURN_MARK(GetStringRegion);
  oop s = JNIHandles::resolve_non_null(string);
  int s_len = java_lang_String::length(s);
4206
  if (start < 0 || len < 0 || start > s_len - len) {
D
duke 已提交
4207 4208 4209 4210 4211 4212 4213 4214 4215 4216
    THROW(vmSymbols::java_lang_StringIndexOutOfBoundsException());
  } else {
    if (len > 0) {
      int s_offset = java_lang_String::offset(s);
      typeArrayOop s_value = java_lang_String::value(s);
      memcpy(buf, s_value->char_at_addr(s_offset+start), sizeof(jchar)*len);
    }
  }
JNI_END

D
dcubed 已提交
4217
#ifndef USDT2
D
duke 已提交
4218
DT_VOID_RETURN_MARK_DECL(GetStringUTFRegion);
D
dcubed 已提交
4219 4220 4221 4222
#else /* USDT2 */
DT_VOID_RETURN_MARK_DECL(GetStringUTFRegion
                         , HOTSPOT_JNI_GETSTRINGUTFREGION_RETURN());
#endif /* USDT2 */
D
duke 已提交
4223 4224 4225

JNI_ENTRY(void, jni_GetStringUTFRegion(JNIEnv *env, jstring string, jsize start, jsize len, char *buf))
  JNIWrapper("GetStringUTFRegion");
D
dcubed 已提交
4226
#ifndef USDT2
D
duke 已提交
4227
  DTRACE_PROBE5(hotspot_jni, GetStringUTFRegion__entry, env, string, start, len, buf);
D
dcubed 已提交
4228 4229 4230 4231
#else /* USDT2 */
 HOTSPOT_JNI_GETSTRINGUTFREGION_ENTRY(
                                      env, string, start, len, buf);
#endif /* USDT2 */
D
duke 已提交
4232 4233 4234
  DT_VOID_RETURN_MARK(GetStringUTFRegion);
  oop s = JNIHandles::resolve_non_null(string);
  int s_len = java_lang_String::length(s);
4235
  if (start < 0 || len < 0 || start > s_len - len) {
D
duke 已提交
4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254 4255 4256
    THROW(vmSymbols::java_lang_StringIndexOutOfBoundsException());
  } else {
    //%note jni_7
    if (len > 0) {
      ResourceMark rm(THREAD);
      char *utf_region = java_lang_String::as_utf8_string(s, start, len);
      int utf_len = (int)strlen(utf_region);
      memcpy(buf, utf_region, utf_len);
      buf[utf_len] = 0;
    } else {
      // JDK null-terminates the buffer even in len is zero
      if (buf != NULL) {
        buf[0] = 0;
      }
    }
  }
JNI_END


JNI_ENTRY(void*, jni_GetPrimitiveArrayCritical(JNIEnv *env, jarray array, jboolean *isCopy))
  JNIWrapper("GetPrimitiveArrayCritical");
D
dcubed 已提交
4257
#ifndef USDT2
D
duke 已提交
4258
  DTRACE_PROBE3(hotspot_jni, GetPrimitiveArrayCritical__entry, env, array, isCopy);
D
dcubed 已提交
4259 4260 4261 4262
#else /* USDT2 */
 HOTSPOT_JNI_GETPRIMITIVEARRAYCRITICAL_ENTRY(
                                             env, array, (uintptr_t *) isCopy);
#endif /* USDT2 */
D
duke 已提交
4263 4264 4265 4266 4267 4268 4269 4270 4271 4272
  GC_locker::lock_critical(thread);
  if (isCopy != NULL) {
    *isCopy = JNI_FALSE;
  }
  oop a = JNIHandles::resolve_non_null(array);
  assert(a->is_array(), "just checking");
  BasicType type;
  if (a->is_objArray()) {
    type = T_OBJECT;
  } else {
4273
    type = TypeArrayKlass::cast(a->klass())->element_type();
D
duke 已提交
4274 4275
  }
  void* ret = arrayOop(a)->base(type);
D
dcubed 已提交
4276
#ifndef USDT2
D
duke 已提交
4277
  DTRACE_PROBE1(hotspot_jni, GetPrimitiveArrayCritical__return, ret);
D
dcubed 已提交
4278 4279 4280 4281
#else /* USDT2 */
 HOTSPOT_JNI_GETPRIMITIVEARRAYCRITICAL_RETURN(
                                              ret);
#endif /* USDT2 */
D
duke 已提交
4282 4283 4284 4285 4286 4287
  return ret;
JNI_END


JNI_ENTRY(void, jni_ReleasePrimitiveArrayCritical(JNIEnv *env, jarray array, void *carray, jint mode))
  JNIWrapper("ReleasePrimitiveArrayCritical");
D
dcubed 已提交
4288
#ifndef USDT2
D
duke 已提交
4289
  DTRACE_PROBE4(hotspot_jni, ReleasePrimitiveArrayCritical__entry, env, array, carray, mode);
D
dcubed 已提交
4290 4291 4292 4293
#else /* USDT2 */
  HOTSPOT_JNI_RELEASEPRIMITIVEARRAYCRITICAL_ENTRY(
                                                  env, array, carray, mode);
#endif /* USDT2 */
D
duke 已提交
4294 4295
  // The array, carray and mode arguments are ignored
  GC_locker::unlock_critical(thread);
D
dcubed 已提交
4296
#ifndef USDT2
D
duke 已提交
4297
  DTRACE_PROBE(hotspot_jni, ReleasePrimitiveArrayCritical__return);
D
dcubed 已提交
4298 4299 4300 4301
#else /* USDT2 */
HOTSPOT_JNI_RELEASEPRIMITIVEARRAYCRITICAL_RETURN(
);
#endif /* USDT2 */
D
duke 已提交
4302 4303 4304 4305 4306
JNI_END


JNI_ENTRY(const jchar*, jni_GetStringCritical(JNIEnv *env, jstring string, jboolean *isCopy))
  JNIWrapper("GetStringCritical");
D
dcubed 已提交
4307
#ifndef USDT2
D
duke 已提交
4308
  DTRACE_PROBE3(hotspot_jni, GetStringCritical__entry, env, string, isCopy);
D
dcubed 已提交
4309 4310 4311 4312
#else /* USDT2 */
  HOTSPOT_JNI_GETSTRINGCRITICAL_ENTRY(
                                      env, string, (uintptr_t *) isCopy);
#endif /* USDT2 */
D
duke 已提交
4313 4314 4315 4316 4317 4318 4319 4320 4321 4322 4323 4324 4325 4326
  GC_locker::lock_critical(thread);
  if (isCopy != NULL) {
    *isCopy = JNI_FALSE;
  }
  oop s = JNIHandles::resolve_non_null(string);
  int s_len = java_lang_String::length(s);
  typeArrayOop s_value = java_lang_String::value(s);
  int s_offset = java_lang_String::offset(s);
  const jchar* ret;
  if (s_len > 0) {
    ret = s_value->char_at_addr(s_offset);
  } else {
    ret = (jchar*) s_value->base(T_CHAR);
  }
D
dcubed 已提交
4327
#ifndef USDT2
D
duke 已提交
4328
  DTRACE_PROBE1(hotspot_jni, GetStringCritical__return, ret);
D
dcubed 已提交
4329 4330 4331 4332
#else /* USDT2 */
 HOTSPOT_JNI_GETSTRINGCRITICAL_RETURN(
                                      (uint16_t *) ret);
#endif /* USDT2 */
D
duke 已提交
4333 4334 4335 4336 4337 4338
  return ret;
JNI_END


JNI_ENTRY(void, jni_ReleaseStringCritical(JNIEnv *env, jstring str, const jchar *chars))
  JNIWrapper("ReleaseStringCritical");
D
dcubed 已提交
4339
#ifndef USDT2
D
duke 已提交
4340
  DTRACE_PROBE3(hotspot_jni, ReleaseStringCritical__entry, env, str, chars);
D
dcubed 已提交
4341 4342 4343 4344
#else /* USDT2 */
  HOTSPOT_JNI_RELEASESTRINGCRITICAL_ENTRY(
                                          env, str, (uint16_t *) chars);
#endif /* USDT2 */
D
duke 已提交
4345 4346
  // The str and chars arguments are ignored
  GC_locker::unlock_critical(thread);
D
dcubed 已提交
4347
#ifndef USDT2
D
duke 已提交
4348
  DTRACE_PROBE(hotspot_jni, ReleaseStringCritical__return);
D
dcubed 已提交
4349 4350 4351 4352
#else /* USDT2 */
HOTSPOT_JNI_RELEASESTRINGCRITICAL_RETURN(
);
#endif /* USDT2 */
D
duke 已提交
4353 4354 4355 4356 4357
JNI_END


JNI_ENTRY(jweak, jni_NewWeakGlobalRef(JNIEnv *env, jobject ref))
  JNIWrapper("jni_NewWeakGlobalRef");
D
dcubed 已提交
4358
#ifndef USDT2
D
duke 已提交
4359
  DTRACE_PROBE2(hotspot_jni, NewWeakGlobalRef__entry, env, ref);
D
dcubed 已提交
4360 4361 4362 4363
#else /* USDT2 */
 HOTSPOT_JNI_NEWWEAKGLOBALREF_ENTRY(
                                    env, ref);
#endif /* USDT2 */
D
duke 已提交
4364 4365
  Handle ref_handle(thread, JNIHandles::resolve(ref));
  jweak ret = JNIHandles::make_weak_global(ref_handle);
D
dcubed 已提交
4366
#ifndef USDT2
D
duke 已提交
4367
  DTRACE_PROBE1(hotspot_jni, NewWeakGlobalRef__return, ret);
D
dcubed 已提交
4368 4369 4370 4371
#else /* USDT2 */
 HOTSPOT_JNI_NEWWEAKGLOBALREF_RETURN(
                                     ret);
#endif /* USDT2 */
D
duke 已提交
4372 4373 4374 4375 4376 4377
  return ret;
JNI_END

// Must be JNI_ENTRY (with HandleMark)
JNI_ENTRY(void, jni_DeleteWeakGlobalRef(JNIEnv *env, jweak ref))
  JNIWrapper("jni_DeleteWeakGlobalRef");
D
dcubed 已提交
4378
#ifndef USDT2
D
duke 已提交
4379
  DTRACE_PROBE2(hotspot_jni, DeleteWeakGlobalRef__entry, env, ref);
D
dcubed 已提交
4380 4381 4382 4383
#else /* USDT2 */
  HOTSPOT_JNI_DELETEWEAKGLOBALREF_ENTRY(
                                        env, ref);
#endif /* USDT2 */
D
duke 已提交
4384
  JNIHandles::destroy_weak_global(ref);
D
dcubed 已提交
4385
#ifndef USDT2
D
duke 已提交
4386
  DTRACE_PROBE(hotspot_jni, DeleteWeakGlobalRef__return);
D
dcubed 已提交
4387 4388 4389 4390
#else /* USDT2 */
  HOTSPOT_JNI_DELETEWEAKGLOBALREF_RETURN(
                                         );
#endif /* USDT2 */
D
duke 已提交
4391 4392 4393 4394 4395
JNI_END


JNI_QUICK_ENTRY(jboolean, jni_ExceptionCheck(JNIEnv *env))
  JNIWrapper("jni_ExceptionCheck");
D
dcubed 已提交
4396
#ifndef USDT2
D
duke 已提交
4397
  DTRACE_PROBE1(hotspot_jni, ExceptionCheck__entry, env);
D
dcubed 已提交
4398 4399 4400 4401
#else /* USDT2 */
 HOTSPOT_JNI_EXCEPTIONCHECK_ENTRY(
                                  env);
#endif /* USDT2 */
D
duke 已提交
4402 4403
  jni_check_async_exceptions(thread);
  jboolean ret = (thread->has_pending_exception()) ? JNI_TRUE : JNI_FALSE;
D
dcubed 已提交
4404
#ifndef USDT2
D
duke 已提交
4405
  DTRACE_PROBE1(hotspot_jni, ExceptionCheck__return, ret);
D
dcubed 已提交
4406 4407 4408 4409
#else /* USDT2 */
 HOTSPOT_JNI_EXCEPTIONCHECK_RETURN(
                                   ret);
#endif /* USDT2 */
D
duke 已提交
4410 4411 4412 4413 4414 4415 4416 4417 4418 4419 4420 4421 4422 4423 4424 4425 4426 4427 4428 4429
  return ret;
JNI_END


// Initialization state for three routines below relating to
// java.nio.DirectBuffers
static          jint directBufferSupportInitializeStarted = 0;
static volatile jint directBufferSupportInitializeEnded   = 0;
static volatile jint directBufferSupportInitializeFailed  = 0;
static jclass    bufferClass                 = NULL;
static jclass    directBufferClass           = NULL;
static jclass    directByteBufferClass       = NULL;
static jmethodID directByteBufferConstructor = NULL;
static jfieldID  directBufferAddressField    = NULL;
static jfieldID  bufferCapacityField         = NULL;

static jclass lookupOne(JNIEnv* env, const char* name, TRAPS) {
  Handle loader;            // null (bootstrap) loader
  Handle protection_domain; // null protection domain

4430
  TempNewSymbol sym = SymbolTable::new_symbol(name, CHECK_NULL);
4431 4432 4433
  jclass result =  find_class_from_class_loader(env, sym, true, loader, protection_domain, true, CHECK_NULL);

  if (TraceClassResolution && result != NULL) {
4434
    trace_class_resolution(java_lang_Class::as_Klass(JNIHandles::resolve_non_null(result)));
4435 4436
  }
  return result;
D
duke 已提交
4437 4438 4439 4440 4441 4442 4443 4444 4445 4446 4447 4448 4449 4450 4451 4452 4453 4454 4455 4456 4457 4458 4459 4460 4461 4462 4463 4464 4465 4466 4467 4468
}

// These lookups are done with the NULL (bootstrap) ClassLoader to
// circumvent any security checks that would be done by jni_FindClass.
JNI_ENTRY(bool, lookupDirectBufferClasses(JNIEnv* env))
{
  if ((bufferClass           = lookupOne(env, "java/nio/Buffer", thread))           == NULL) { return false; }
  if ((directBufferClass     = lookupOne(env, "sun/nio/ch/DirectBuffer", thread))   == NULL) { return false; }
  if ((directByteBufferClass = lookupOne(env, "java/nio/DirectByteBuffer", thread)) == NULL) { return false; }
  return true;
}
JNI_END


static bool initializeDirectBufferSupport(JNIEnv* env, JavaThread* thread) {
  if (directBufferSupportInitializeFailed) {
    return false;
  }

  if (Atomic::cmpxchg(1, &directBufferSupportInitializeStarted, 0) == 0) {
    if (!lookupDirectBufferClasses(env)) {
      directBufferSupportInitializeFailed = 1;
      return false;
    }

    // Make global references for these
    bufferClass           = (jclass) env->NewGlobalRef(bufferClass);
    directBufferClass     = (jclass) env->NewGlobalRef(directBufferClass);
    directByteBufferClass = (jclass) env->NewGlobalRef(directByteBufferClass);

    // Get needed field and method IDs
    directByteBufferConstructor = env->GetMethodID(directByteBufferClass, "<init>", "(JI)V");
4469 4470 4471 4472 4473
    if (env->ExceptionCheck()) {
      env->ExceptionClear();
      directBufferSupportInitializeFailed = 1;
      return false;
    }
D
duke 已提交
4474
    directBufferAddressField    = env->GetFieldID(bufferClass, "address", "J");
4475 4476 4477 4478 4479
    if (env->ExceptionCheck()) {
      env->ExceptionClear();
      directBufferSupportInitializeFailed = 1;
      return false;
    }
D
duke 已提交
4480
    bufferCapacityField         = env->GetFieldID(bufferClass, "capacity", "I");
4481 4482 4483 4484 4485
    if (env->ExceptionCheck()) {
      env->ExceptionClear();
      directBufferSupportInitializeFailed = 1;
      return false;
    }
D
duke 已提交
4486 4487 4488 4489 4490 4491 4492 4493 4494 4495 4496

    if ((directByteBufferConstructor == NULL) ||
        (directBufferAddressField    == NULL) ||
        (bufferCapacityField         == NULL)) {
      directBufferSupportInitializeFailed = 1;
      return false;
    }

    directBufferSupportInitializeEnded = 1;
  } else {
    while (!directBufferSupportInitializeEnded && !directBufferSupportInitializeFailed) {
4497 4498 4499 4500 4501 4502
      // Set state as yield_all can call os:sleep. On Solaris, yield_all calls
      // os::sleep which requires the VM state transition. On other platforms, it
      // is not necessary. The following call to change the VM state is purposely
      // put inside the loop to avoid potential deadlock when multiple threads
      // try to call this method. See 6791815 for more details.
      ThreadInVMfromNative tivn(thread);
D
duke 已提交
4503 4504 4505 4506 4507 4508 4509 4510 4511 4512 4513 4514 4515
      os::yield_all();
    }
  }

  return !directBufferSupportInitializeFailed;
}

extern "C" jobject JNICALL jni_NewDirectByteBuffer(JNIEnv *env, void* address, jlong capacity)
{
  // thread_from_jni_environment() will block if VM is gone.
  JavaThread* thread = JavaThread::thread_from_jni_environment(env);

  JNIWrapper("jni_NewDirectByteBuffer");
D
dcubed 已提交
4516
#ifndef USDT2
D
duke 已提交
4517
  DTRACE_PROBE3(hotspot_jni, NewDirectByteBuffer__entry, env, address, capacity);
D
dcubed 已提交
4518 4519 4520 4521
#else /* USDT2 */
 HOTSPOT_JNI_NEWDIRECTBYTEBUFFER_ENTRY(
                                       env, address, capacity);
#endif /* USDT2 */
D
duke 已提交
4522 4523 4524

  if (!directBufferSupportInitializeEnded) {
    if (!initializeDirectBufferSupport(env, thread)) {
D
dcubed 已提交
4525
#ifndef USDT2
D
duke 已提交
4526
      DTRACE_PROBE1(hotspot_jni, NewDirectByteBuffer__return, NULL);
D
dcubed 已提交
4527 4528 4529 4530
#else /* USDT2 */
      HOTSPOT_JNI_NEWDIRECTBYTEBUFFER_RETURN(
                                             NULL);
#endif /* USDT2 */
D
duke 已提交
4531 4532 4533 4534 4535 4536 4537 4538 4539 4540
      return NULL;
    }
  }

  // Being paranoid about accidental sign extension on address
  jlong addr = (jlong) ((uintptr_t) address);
  // NOTE that package-private DirectByteBuffer constructor currently
  // takes int capacity
  jint  cap  = (jint)  capacity;
  jobject ret = env->NewObject(directByteBufferClass, directByteBufferConstructor, addr, cap);
D
dcubed 已提交
4541
#ifndef USDT2
D
duke 已提交
4542
  DTRACE_PROBE1(hotspot_jni, NewDirectByteBuffer__return, ret);
D
dcubed 已提交
4543 4544 4545 4546
#else /* USDT2 */
  HOTSPOT_JNI_NEWDIRECTBYTEBUFFER_RETURN(
                                         ret);
#endif /* USDT2 */
D
duke 已提交
4547 4548 4549
  return ret;
}

D
dcubed 已提交
4550
#ifndef USDT2
D
duke 已提交
4551
DT_RETURN_MARK_DECL(GetDirectBufferAddress, void*);
D
dcubed 已提交
4552 4553 4554 4555
#else /* USDT2 */
DT_RETURN_MARK_DECL(GetDirectBufferAddress, void*
                    , HOTSPOT_JNI_GETDIRECTBUFFERADDRESS_RETURN((void*) _ret_ref));
#endif /* USDT2 */
D
duke 已提交
4556 4557 4558 4559 4560 4561 4562

extern "C" void* JNICALL jni_GetDirectBufferAddress(JNIEnv *env, jobject buf)
{
  // thread_from_jni_environment() will block if VM is gone.
  JavaThread* thread = JavaThread::thread_from_jni_environment(env);

  JNIWrapper("jni_GetDirectBufferAddress");
D
dcubed 已提交
4563
#ifndef USDT2
D
duke 已提交
4564
  DTRACE_PROBE2(hotspot_jni, GetDirectBufferAddress__entry, env, buf);
D
dcubed 已提交
4565 4566 4567 4568
#else /* USDT2 */
  HOTSPOT_JNI_GETDIRECTBUFFERADDRESS_ENTRY(
                                           env, buf);
#endif /* USDT2 */
D
duke 已提交
4569 4570 4571 4572 4573 4574 4575 4576 4577 4578 4579 4580 4581 4582 4583 4584 4585
  void* ret = NULL;
  DT_RETURN_MARK(GetDirectBufferAddress, void*, (const void*&)ret);

  if (!directBufferSupportInitializeEnded) {
    if (!initializeDirectBufferSupport(env, thread)) {
      return 0;
    }
  }

  if ((buf != NULL) && (!env->IsInstanceOf(buf, directBufferClass))) {
    return 0;
  }

  ret = (void*)(intptr_t)env->GetLongField(buf, directBufferAddressField);
  return ret;
}

D
dcubed 已提交
4586
#ifndef USDT2
D
duke 已提交
4587
DT_RETURN_MARK_DECL(GetDirectBufferCapacity, jlong);
D
dcubed 已提交
4588 4589 4590 4591
#else /* USDT2 */
DT_RETURN_MARK_DECL(GetDirectBufferCapacity, jlong
                    , HOTSPOT_JNI_GETDIRECTBUFFERCAPACITY_RETURN(_ret_ref));
#endif /* USDT2 */
D
duke 已提交
4592 4593 4594 4595 4596 4597 4598

extern "C" jlong JNICALL jni_GetDirectBufferCapacity(JNIEnv *env, jobject buf)
{
  // thread_from_jni_environment() will block if VM is gone.
  JavaThread* thread = JavaThread::thread_from_jni_environment(env);

  JNIWrapper("jni_GetDirectBufferCapacity");
D
dcubed 已提交
4599
#ifndef USDT2
D
duke 已提交
4600
  DTRACE_PROBE2(hotspot_jni, GetDirectBufferCapacity__entry, env, buf);
D
dcubed 已提交
4601 4602 4603 4604
#else /* USDT2 */
  HOTSPOT_JNI_GETDIRECTBUFFERCAPACITY_ENTRY(
                                            env, buf);
#endif /* USDT2 */
D
duke 已提交
4605 4606 4607 4608 4609 4610 4611 4612 4613 4614 4615 4616 4617 4618 4619 4620 4621 4622 4623 4624 4625 4626 4627 4628 4629 4630
  jlong ret = -1;
  DT_RETURN_MARK(GetDirectBufferCapacity, jlong, (const jlong&)ret);

  if (!directBufferSupportInitializeEnded) {
    if (!initializeDirectBufferSupport(env, thread)) {
      ret = 0;
      return ret;
    }
  }

  if (buf == NULL) {
    return -1;
  }

  if (!env->IsInstanceOf(buf, directBufferClass)) {
    return -1;
  }

  // NOTE that capacity is currently an int in the implementation
  ret = env->GetIntField(buf, bufferCapacityField);
  return ret;
}


JNI_LEAF(jint, jni_GetVersion(JNIEnv *env))
  JNIWrapper("GetVersion");
D
dcubed 已提交
4631
#ifndef USDT2
D
duke 已提交
4632
  DTRACE_PROBE1(hotspot_jni, GetVersion__entry, env);
D
dcubed 已提交
4633 4634 4635 4636 4637
#else /* USDT2 */
  HOTSPOT_JNI_GETVERSION_ENTRY(
                               env);
#endif /* USDT2 */
#ifndef USDT2
D
duke 已提交
4638
  DTRACE_PROBE1(hotspot_jni, GetVersion__return, CurrentVersion);
D
dcubed 已提交
4639 4640 4641 4642
#else /* USDT2 */
  HOTSPOT_JNI_GETVERSION_RETURN(
                                CurrentVersion);
#endif /* USDT2 */
D
duke 已提交
4643 4644 4645 4646 4647 4648 4649
  return CurrentVersion;
JNI_END

extern struct JavaVM_ main_vm;

JNI_LEAF(jint, jni_GetJavaVM(JNIEnv *env, JavaVM **vm))
  JNIWrapper("jni_GetJavaVM");
D
dcubed 已提交
4650
#ifndef USDT2
D
duke 已提交
4651
  DTRACE_PROBE2(hotspot_jni, GetJavaVM__entry, env, vm);
D
dcubed 已提交
4652 4653 4654 4655
#else /* USDT2 */
  HOTSPOT_JNI_GETJAVAVM_ENTRY(
                              env, (void **) vm);
#endif /* USDT2 */
D
duke 已提交
4656
  *vm  = (JavaVM *)(&main_vm);
D
dcubed 已提交
4657
#ifndef USDT2
D
duke 已提交
4658
  DTRACE_PROBE1(hotspot_jni, GetJavaVM__return, JNI_OK);
D
dcubed 已提交
4659 4660 4661 4662
#else /* USDT2 */
  HOTSPOT_JNI_GETJAVAVM_RETURN(
                               JNI_OK);
#endif /* USDT2 */
D
duke 已提交
4663 4664 4665 4666 4667 4668 4669 4670 4671 4672 4673 4674 4675 4676 4677 4678 4679 4680 4681 4682 4683 4684 4685 4686 4687 4688 4689 4690 4691 4692 4693 4694 4695 4696 4697 4698 4699 4700 4701 4702 4703 4704 4705 4706 4707 4708 4709 4710 4711 4712 4713 4714 4715 4716 4717 4718 4719 4720 4721 4722 4723 4724 4725 4726 4727 4728 4729 4730 4731 4732 4733 4734 4735 4736 4737 4738 4739 4740 4741 4742 4743 4744 4745 4746 4747 4748 4749 4750 4751 4752 4753 4754 4755 4756 4757 4758 4759 4760 4761 4762 4763 4764 4765 4766 4767 4768 4769 4770 4771 4772 4773 4774 4775 4776 4777 4778 4779 4780 4781 4782 4783 4784 4785 4786 4787 4788 4789 4790 4791 4792 4793 4794 4795 4796 4797 4798 4799 4800 4801 4802 4803 4804 4805 4806 4807 4808 4809 4810 4811 4812 4813 4814 4815 4816 4817 4818 4819 4820 4821 4822 4823 4824 4825 4826 4827 4828 4829 4830 4831 4832 4833 4834 4835 4836 4837 4838 4839 4840 4841 4842 4843 4844 4845 4846 4847 4848 4849 4850 4851 4852 4853 4854 4855 4856 4857 4858 4859 4860 4861 4862 4863 4864 4865 4866 4867 4868 4869 4870 4871 4872 4873 4874 4875 4876 4877 4878 4879 4880 4881 4882 4883 4884 4885 4886 4887 4888 4889 4890 4891 4892 4893 4894 4895 4896 4897 4898 4899 4900 4901 4902 4903 4904 4905 4906 4907 4908 4909 4910 4911 4912 4913 4914 4915 4916 4917 4918 4919 4920 4921 4922 4923 4924 4925 4926 4927 4928 4929 4930 4931 4932 4933 4934 4935 4936 4937 4938 4939 4940 4941 4942 4943 4944 4945 4946 4947 4948 4949 4950 4951 4952 4953 4954 4955 4956 4957 4958 4959 4960 4961 4962 4963 4964 4965 4966 4967 4968 4969 4970 4971 4972 4973 4974 4975 4976 4977 4978 4979 4980 4981 4982 4983 4984 4985 4986 4987 4988 4989 4990 4991 4992 4993 4994 4995 4996 4997 4998 4999 5000 5001 5002 5003 5004 5005 5006 5007 5008 5009 5010 5011
  return JNI_OK;
JNI_END

// Structure containing all jni functions
struct JNINativeInterface_ jni_NativeInterface = {
    NULL,
    NULL,
    NULL,

    NULL,

    jni_GetVersion,

    jni_DefineClass,
    jni_FindClass,

    jni_FromReflectedMethod,
    jni_FromReflectedField,

    jni_ToReflectedMethod,

    jni_GetSuperclass,
    jni_IsAssignableFrom,

    jni_ToReflectedField,

    jni_Throw,
    jni_ThrowNew,
    jni_ExceptionOccurred,
    jni_ExceptionDescribe,
    jni_ExceptionClear,
    jni_FatalError,

    jni_PushLocalFrame,
    jni_PopLocalFrame,

    jni_NewGlobalRef,
    jni_DeleteGlobalRef,
    jni_DeleteLocalRef,
    jni_IsSameObject,

    jni_NewLocalRef,
    jni_EnsureLocalCapacity,

    jni_AllocObject,
    jni_NewObject,
    jni_NewObjectV,
    jni_NewObjectA,

    jni_GetObjectClass,
    jni_IsInstanceOf,

    jni_GetMethodID,

    jni_CallObjectMethod,
    jni_CallObjectMethodV,
    jni_CallObjectMethodA,
    jni_CallBooleanMethod,
    jni_CallBooleanMethodV,
    jni_CallBooleanMethodA,
    jni_CallByteMethod,
    jni_CallByteMethodV,
    jni_CallByteMethodA,
    jni_CallCharMethod,
    jni_CallCharMethodV,
    jni_CallCharMethodA,
    jni_CallShortMethod,
    jni_CallShortMethodV,
    jni_CallShortMethodA,
    jni_CallIntMethod,
    jni_CallIntMethodV,
    jni_CallIntMethodA,
    jni_CallLongMethod,
    jni_CallLongMethodV,
    jni_CallLongMethodA,
    jni_CallFloatMethod,
    jni_CallFloatMethodV,
    jni_CallFloatMethodA,
    jni_CallDoubleMethod,
    jni_CallDoubleMethodV,
    jni_CallDoubleMethodA,
    jni_CallVoidMethod,
    jni_CallVoidMethodV,
    jni_CallVoidMethodA,

    jni_CallNonvirtualObjectMethod,
    jni_CallNonvirtualObjectMethodV,
    jni_CallNonvirtualObjectMethodA,
    jni_CallNonvirtualBooleanMethod,
    jni_CallNonvirtualBooleanMethodV,
    jni_CallNonvirtualBooleanMethodA,
    jni_CallNonvirtualByteMethod,
    jni_CallNonvirtualByteMethodV,
    jni_CallNonvirtualByteMethodA,
    jni_CallNonvirtualCharMethod,
    jni_CallNonvirtualCharMethodV,
    jni_CallNonvirtualCharMethodA,
    jni_CallNonvirtualShortMethod,
    jni_CallNonvirtualShortMethodV,
    jni_CallNonvirtualShortMethodA,
    jni_CallNonvirtualIntMethod,
    jni_CallNonvirtualIntMethodV,
    jni_CallNonvirtualIntMethodA,
    jni_CallNonvirtualLongMethod,
    jni_CallNonvirtualLongMethodV,
    jni_CallNonvirtualLongMethodA,
    jni_CallNonvirtualFloatMethod,
    jni_CallNonvirtualFloatMethodV,
    jni_CallNonvirtualFloatMethodA,
    jni_CallNonvirtualDoubleMethod,
    jni_CallNonvirtualDoubleMethodV,
    jni_CallNonvirtualDoubleMethodA,
    jni_CallNonvirtualVoidMethod,
    jni_CallNonvirtualVoidMethodV,
    jni_CallNonvirtualVoidMethodA,

    jni_GetFieldID,

    jni_GetObjectField,
    jni_GetBooleanField,
    jni_GetByteField,
    jni_GetCharField,
    jni_GetShortField,
    jni_GetIntField,
    jni_GetLongField,
    jni_GetFloatField,
    jni_GetDoubleField,

    jni_SetObjectField,
    jni_SetBooleanField,
    jni_SetByteField,
    jni_SetCharField,
    jni_SetShortField,
    jni_SetIntField,
    jni_SetLongField,
    jni_SetFloatField,
    jni_SetDoubleField,

    jni_GetStaticMethodID,

    jni_CallStaticObjectMethod,
    jni_CallStaticObjectMethodV,
    jni_CallStaticObjectMethodA,
    jni_CallStaticBooleanMethod,
    jni_CallStaticBooleanMethodV,
    jni_CallStaticBooleanMethodA,
    jni_CallStaticByteMethod,
    jni_CallStaticByteMethodV,
    jni_CallStaticByteMethodA,
    jni_CallStaticCharMethod,
    jni_CallStaticCharMethodV,
    jni_CallStaticCharMethodA,
    jni_CallStaticShortMethod,
    jni_CallStaticShortMethodV,
    jni_CallStaticShortMethodA,
    jni_CallStaticIntMethod,
    jni_CallStaticIntMethodV,
    jni_CallStaticIntMethodA,
    jni_CallStaticLongMethod,
    jni_CallStaticLongMethodV,
    jni_CallStaticLongMethodA,
    jni_CallStaticFloatMethod,
    jni_CallStaticFloatMethodV,
    jni_CallStaticFloatMethodA,
    jni_CallStaticDoubleMethod,
    jni_CallStaticDoubleMethodV,
    jni_CallStaticDoubleMethodA,
    jni_CallStaticVoidMethod,
    jni_CallStaticVoidMethodV,
    jni_CallStaticVoidMethodA,

    jni_GetStaticFieldID,

    jni_GetStaticObjectField,
    jni_GetStaticBooleanField,
    jni_GetStaticByteField,
    jni_GetStaticCharField,
    jni_GetStaticShortField,
    jni_GetStaticIntField,
    jni_GetStaticLongField,
    jni_GetStaticFloatField,
    jni_GetStaticDoubleField,

    jni_SetStaticObjectField,
    jni_SetStaticBooleanField,
    jni_SetStaticByteField,
    jni_SetStaticCharField,
    jni_SetStaticShortField,
    jni_SetStaticIntField,
    jni_SetStaticLongField,
    jni_SetStaticFloatField,
    jni_SetStaticDoubleField,

    jni_NewString,
    jni_GetStringLength,
    jni_GetStringChars,
    jni_ReleaseStringChars,

    jni_NewStringUTF,
    jni_GetStringUTFLength,
    jni_GetStringUTFChars,
    jni_ReleaseStringUTFChars,

    jni_GetArrayLength,

    jni_NewObjectArray,
    jni_GetObjectArrayElement,
    jni_SetObjectArrayElement,

    jni_NewBooleanArray,
    jni_NewByteArray,
    jni_NewCharArray,
    jni_NewShortArray,
    jni_NewIntArray,
    jni_NewLongArray,
    jni_NewFloatArray,
    jni_NewDoubleArray,

    jni_GetBooleanArrayElements,
    jni_GetByteArrayElements,
    jni_GetCharArrayElements,
    jni_GetShortArrayElements,
    jni_GetIntArrayElements,
    jni_GetLongArrayElements,
    jni_GetFloatArrayElements,
    jni_GetDoubleArrayElements,

    jni_ReleaseBooleanArrayElements,
    jni_ReleaseByteArrayElements,
    jni_ReleaseCharArrayElements,
    jni_ReleaseShortArrayElements,
    jni_ReleaseIntArrayElements,
    jni_ReleaseLongArrayElements,
    jni_ReleaseFloatArrayElements,
    jni_ReleaseDoubleArrayElements,

    jni_GetBooleanArrayRegion,
    jni_GetByteArrayRegion,
    jni_GetCharArrayRegion,
    jni_GetShortArrayRegion,
    jni_GetIntArrayRegion,
    jni_GetLongArrayRegion,
    jni_GetFloatArrayRegion,
    jni_GetDoubleArrayRegion,

    jni_SetBooleanArrayRegion,
    jni_SetByteArrayRegion,
    jni_SetCharArrayRegion,
    jni_SetShortArrayRegion,
    jni_SetIntArrayRegion,
    jni_SetLongArrayRegion,
    jni_SetFloatArrayRegion,
    jni_SetDoubleArrayRegion,

    jni_RegisterNatives,
    jni_UnregisterNatives,

    jni_MonitorEnter,
    jni_MonitorExit,

    jni_GetJavaVM,

    jni_GetStringRegion,
    jni_GetStringUTFRegion,

    jni_GetPrimitiveArrayCritical,
    jni_ReleasePrimitiveArrayCritical,

    jni_GetStringCritical,
    jni_ReleaseStringCritical,

    jni_NewWeakGlobalRef,
    jni_DeleteWeakGlobalRef,

    jni_ExceptionCheck,

    jni_NewDirectByteBuffer,
    jni_GetDirectBufferAddress,
    jni_GetDirectBufferCapacity,

    // New 1_6 features

    jni_GetObjectRefType
};


// For jvmti use to modify jni function table.
// Java threads in native contiues to run until it is transitioned
// to VM at safepoint. Before the transition or before it is blocked
// for safepoint it may access jni function table. VM could crash if
// any java thread access the jni function table in the middle of memcpy.
// To avoid this each function pointers are copied automically.
void copy_jni_function_table(const struct JNINativeInterface_ *new_jni_NativeInterface) {
  assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint");
  intptr_t *a = (intptr_t *) jni_functions();
  intptr_t *b = (intptr_t *) new_jni_NativeInterface;
  for (uint i=0; i <  sizeof(struct JNINativeInterface_)/sizeof(void *); i++) {
    Atomic::store_ptr(*b++, a++);
  }
}

void quicken_jni_functions() {
  // Replace Get<Primitive>Field with fast versions
  if (UseFastJNIAccessors && !JvmtiExport::can_post_field_access()
      && !VerifyJNIFields && !TraceJNICalls && !CountJNICalls && !CheckJNICalls
#if defined(_WINDOWS) && defined(IA32) && defined(COMPILER2)
      // windows x86 currently needs SEH wrapper and the gain of the fast
      // versions currently isn't certain for server vm on uniprocessor.
      && os::is_MP()
#endif
  ) {
    address func;
    func = JNI_FastGetField::generate_fast_get_boolean_field();
    if (func != (address)-1) {
      jni_NativeInterface.GetBooleanField = (GetBooleanField_t)func;
    }
    func = JNI_FastGetField::generate_fast_get_byte_field();
    if (func != (address)-1) {
      jni_NativeInterface.GetByteField = (GetByteField_t)func;
    }
    func = JNI_FastGetField::generate_fast_get_char_field();
    if (func != (address)-1) {
      jni_NativeInterface.GetCharField = (GetCharField_t)func;
    }
    func = JNI_FastGetField::generate_fast_get_short_field();
    if (func != (address)-1) {
      jni_NativeInterface.GetShortField = (GetShortField_t)func;
    }
    func = JNI_FastGetField::generate_fast_get_int_field();
    if (func != (address)-1) {
      jni_NativeInterface.GetIntField = (GetIntField_t)func;
    }
    func = JNI_FastGetField::generate_fast_get_long_field();
    if (func != (address)-1) {
      jni_NativeInterface.GetLongField = (GetLongField_t)func;
    }
    func = JNI_FastGetField::generate_fast_get_float_field();
    if (func != (address)-1) {
      jni_NativeInterface.GetFloatField = (GetFloatField_t)func;
    }
    func = JNI_FastGetField::generate_fast_get_double_field();
    if (func != (address)-1) {
      jni_NativeInterface.GetDoubleField = (GetDoubleField_t)func;
    }
  }
}

// Returns the function structure
struct JNINativeInterface_* jni_functions() {
5012
#if INCLUDE_JNI_CHECK
D
duke 已提交
5013
  if (CheckJNICalls) return jni_functions_check();
5014
#endif // INCLUDE_JNI_CHECK
D
duke 已提交
5015 5016 5017 5018 5019 5020 5021 5022
  return &jni_NativeInterface;
}

// Returns the function structure
struct JNINativeInterface_* jni_functions_nocheck() {
  return &jni_NativeInterface;
}

A
apetushkov 已提交
5023 5024 5025 5026 5027
static void post_thread_start_event(const JavaThread* jt) {
  assert(jt != NULL, "invariant");
  EventThreadStart event;
  if (event.should_commit()) {
    event.set_thread(JFR_THREAD_ID(jt));
5028 5029 5030 5031 5032 5033 5034 5035
    event.set_parentThread((traceid)0);
    if (EventThreadStart::is_stacktrace_enabled()) {
      jt->jfr_thread_local()->set_cached_stack_trace_id((traceid)0);
      event.commit();
      jt->jfr_thread_local()->clear_cached_stack_trace();
    } else {
      event.commit();
    }
A
apetushkov 已提交
5036 5037
  }
}
D
duke 已提交
5038 5039 5040 5041 5042 5043 5044 5045 5046 5047 5048 5049 5050 5051 5052 5053 5054

// Invocation API


// Forward declaration
extern const struct JNIInvokeInterface_ jni_InvokeInterface;

// Global invocation API vars
volatile jint vm_created = 0;
// Indicate whether it is safe to recreate VM
volatile jint safe_to_recreate_vm = 1;
struct JavaVM_ main_vm = {&jni_InvokeInterface};


#define JAVASTACKSIZE (400 * 1024)    /* Default size of a thread java stack */
enum { VERIFY_NONE, VERIFY_REMOTE, VERIFY_ALL };

D
dcubed 已提交
5055
#ifndef USDT2
D
duke 已提交
5056 5057
HS_DTRACE_PROBE_DECL1(hotspot_jni, GetDefaultJavaVMInitArgs__entry, void*);
DT_RETURN_MARK_DECL(GetDefaultJavaVMInitArgs, jint);
D
dcubed 已提交
5058 5059 5060 5061
#else /* USDT2 */
DT_RETURN_MARK_DECL(GetDefaultJavaVMInitArgs, jint
                    , HOTSPOT_JNI_GETDEFAULTJAVAVMINITARGS_RETURN(_ret_ref));
#endif /* USDT2 */
D
duke 已提交
5062 5063

_JNI_IMPORT_OR_EXPORT_ jint JNICALL JNI_GetDefaultJavaVMInitArgs(void *args_) {
D
dcubed 已提交
5064
#ifndef USDT2
D
duke 已提交
5065
  HS_DTRACE_PROBE1(hotspot_jni, GetDefaultJavaVMInitArgs__entry, args_);
D
dcubed 已提交
5066 5067 5068 5069
#else /* USDT2 */
  HOTSPOT_JNI_GETDEFAULTJAVAVMINITARGS_ENTRY(
                                             args_);
#endif /* USDT2 */
D
duke 已提交
5070 5071 5072 5073 5074 5075 5076 5077 5078 5079 5080 5081 5082 5083 5084 5085 5086 5087 5088 5089
  JDK1_1InitArgs *args = (JDK1_1InitArgs *)args_;
  jint ret = JNI_ERR;
  DT_RETURN_MARK(GetDefaultJavaVMInitArgs, jint, (const jint&)ret);

  if (Threads::is_supported_jni_version(args->version)) {
    ret = JNI_OK;
  }
  // 1.1 style no longer supported in hotspot.
  // According the JNI spec, we should update args->version on return.
  // We also use the structure to communicate with launcher about default
  // stack size.
  if (args->version == JNI_VERSION_1_1) {
    args->version = JNI_VERSION_1_2;
    // javaStackSize is int in arguments structure
    assert(jlong(ThreadStackSize) * K < INT_MAX, "integer overflow");
    args->javaStackSize = (jint)(ThreadStackSize * K);
  }
  return ret;
}

5090 5091
#ifndef PRODUCT

S
sla 已提交
5092
#include "gc_implementation/shared/gcTimer.hpp"
S
stefank 已提交
5093
#include "gc_interface/collectedHeap.hpp"
5094 5095 5096
#if INCLUDE_ALL_GCS
#include "gc_implementation/g1/heapRegionRemSet.hpp"
#endif
5097
#include "memory/guardedMemory.hpp"
5098
#include "utilities/quickSort.hpp"
5099
#include "utilities/ostream.hpp"
5100 5101 5102
#if INCLUDE_VM_STRUCTS
#include "runtime/vmStructs.hpp"
#endif
5103

S
stefank 已提交
5104 5105 5106 5107
#define run_unit_test(unit_test_function_call)              \
  tty->print_cr("Running test: " #unit_test_function_call); \
  unit_test_function_call

5108
// Forward declaration
5109
void TestOS_test();
5110 5111
void TestReservedSpace_test();
void TestReserveMemorySpecial_test();
5112
void TestVirtualSpace_test();
5113
void TestMetaspaceAux_test();
5114
void SpaceManager_test_adjust_initial_chunk_size();
5115
void TestMetachunk_test();
5116
void TestVirtualSpaceNode_test();
5117
void TestNewSize_test();
5118
void TestKlass_test();
5119
void Test_linked_list();
5120
void TestResourcehash_test();
5121
void TestChunkedList_test();
5122
#if INCLUDE_ALL_GCS
J
jwilhelm 已提交
5123
void TestOldFreeSpaceCalculation_test();
5124
void TestG1BiasedArray_test();
5125
void TestBufferingOopClosure_test();
5126
void TestCodeCacheRemSet_test();
5127
void FreeRegionList_test();
5128
void ChunkManager_test_list_index();
5129
#endif
5130

5131 5132
void execute_internal_vm_tests() {
  if (ExecuteInternalVMTests) {
S
stefank 已提交
5133
    tty->print_cr("Running internal VM tests");
5134
    run_unit_test(TestOS_test());
5135 5136
    run_unit_test(TestReservedSpace_test());
    run_unit_test(TestReserveMemorySpecial_test());
5137
    run_unit_test(TestVirtualSpace_test());
5138
    run_unit_test(TestMetaspaceAux_test());
5139
    run_unit_test(TestMetachunk_test());
5140
    run_unit_test(TestVirtualSpaceNode_test());
5141
    run_unit_test(GlobalDefinitions::test_globals());
5142
    run_unit_test(GlobalDefinitions::test_proper_unit());
S
sla 已提交
5143
    run_unit_test(GCTimerAllTest::all());
S
stefank 已提交
5144 5145 5146
    run_unit_test(arrayOopDesc::test_max_array_length());
    run_unit_test(CollectedHeap::test_is_in());
    run_unit_test(QuickSort::test_quick_sort());
5147
    run_unit_test(GuardedMemory::test_guarded_memory());
5148
    run_unit_test(AltHashing::test_alt_hash());
5149
    run_unit_test(test_loggc_filename());
5150
    run_unit_test(test_snprintf());
5151
    run_unit_test(TestNewSize_test());
5152
    run_unit_test(TestKlass_test());
5153
    run_unit_test(TestResourcehash_test());
5154
    run_unit_test(Test_linked_list());
5155
    run_unit_test(TestChunkedList_test());
5156
    run_unit_test(ObjectMonitor::sanity_checks());
5157 5158
#if INCLUDE_VM_STRUCTS
    run_unit_test(VMStructs::test());
5159 5160
#endif
#if INCLUDE_ALL_GCS
J
jwilhelm 已提交
5161
    run_unit_test(TestOldFreeSpaceCalculation_test());
5162
    run_unit_test(TestG1BiasedArray_test());
5163
    run_unit_test(HeapRegionRemSet::test_prt());
5164
    run_unit_test(SpaceManager_test_adjust_initial_chunk_size());
5165
    run_unit_test(ChunkManager_test_list_index());
5166
    run_unit_test(TestBufferingOopClosure_test());
5167
    run_unit_test(TestCodeCacheRemSet_test());
5168 5169 5170
    if (UseG1GC) {
      run_unit_test(FreeRegionList_test());
    }
5171
#endif
5172
    tty->print_cr("All internal VM tests passed");
5173 5174 5175
  }
}

S
stefank 已提交
5176 5177
#undef run_unit_test

5178 5179
#endif

D
dcubed 已提交
5180
#ifndef USDT2
D
duke 已提交
5181 5182
HS_DTRACE_PROBE_DECL3(hotspot_jni, CreateJavaVM__entry, vm, penv, args);
DT_RETURN_MARK_DECL(CreateJavaVM, jint);
D
dcubed 已提交
5183 5184 5185 5186
#else /* USDT2 */
DT_RETURN_MARK_DECL(CreateJavaVM, jint
                    , HOTSPOT_JNI_CREATEJAVAVM_RETURN(_ret_ref));
#endif /* USDT2 */
D
duke 已提交
5187 5188

_JNI_IMPORT_OR_EXPORT_ jint JNICALL JNI_CreateJavaVM(JavaVM **vm, void **penv, void *args) {
D
dcubed 已提交
5189
#ifndef USDT2
D
duke 已提交
5190
  HS_DTRACE_PROBE3(hotspot_jni, CreateJavaVM__entry, vm, penv, args);
D
dcubed 已提交
5191 5192 5193 5194
#else /* USDT2 */
  HOTSPOT_JNI_CREATEJAVAVM_ENTRY(
                                 (void **) vm, penv, args);
#endif /* USDT2 */
D
duke 已提交
5195 5196 5197 5198

  jint result = JNI_ERR;
  DT_RETURN_MARK(CreateJavaVM, jint, (const jint&)result);

T
twisti 已提交
5199 5200 5201 5202 5203 5204 5205 5206 5207 5208
  // We're about to use Atomic::xchg for synchronization.  Some Zero
  // platforms use the GCC builtin __sync_lock_test_and_set for this,
  // but __sync_lock_test_and_set is not guaranteed to do what we want
  // on all architectures.  So we check it works before relying on it.
#if defined(ZERO) && defined(ASSERT)
  {
    jint a = 0xcafebabe;
    jint b = Atomic::xchg(0xdeadbeef, &a);
    void *c = &a;
    void *d = Atomic::xchg_ptr(&b, &c);
T
twisti 已提交
5209
    assert(a == (jint) 0xdeadbeef && b == (jint) 0xcafebabe, "Atomic::xchg() works");
T
twisti 已提交
5210 5211 5212 5213
    assert(c == &b && d == &a, "Atomic::xchg_ptr() works");
  }
#endif // ZERO && ASSERT

D
duke 已提交
5214 5215 5216 5217 5218 5219 5220 5221 5222 5223 5224 5225 5226
  // At the moment it's only possible to have one Java VM,
  // since some of the runtime state is in global variables.

  // We cannot use our mutex locks here, since they only work on
  // Threads. We do an atomic compare and exchange to ensure only
  // one thread can call this method at a time

  // We use Atomic::xchg rather than Atomic::add/dec since on some platforms
  // the add/dec implementations are dependent on whether we are running
  // on a multiprocessor, and at this stage of initialization the os::is_MP
  // function used to determine this will always return false. Atomic::xchg
  // does not have this problem.
  if (Atomic::xchg(1, &vm_created) == 1) {
5227
    return JNI_EEXIST;   // already created, or create attempt in progress
D
duke 已提交
5228 5229 5230 5231 5232 5233 5234 5235 5236 5237 5238 5239 5240 5241 5242 5243 5244 5245 5246 5247 5248 5249 5250 5251 5252 5253 5254 5255 5256 5257 5258 5259 5260
  }
  if (Atomic::xchg(0, &safe_to_recreate_vm) == 0) {
    return JNI_ERR;  // someone tried and failed and retry not allowed.
  }

  assert(vm_created == 1, "vm_created is true during the creation");

  /**
   * Certain errors during initialization are recoverable and do not
   * prevent this method from being called again at a later time
   * (perhaps with different arguments).  However, at a certain
   * point during initialization if an error occurs we cannot allow
   * this function to be called again (or it will crash).  In those
   * situations, the 'canTryAgain' flag is set to false, which atomically
   * sets safe_to_recreate_vm to 1, such that any new call to
   * JNI_CreateJavaVM will immediately fail using the above logic.
   */
  bool can_try_again = true;

  result = Threads::create_vm((JavaVMInitArgs*) args, &can_try_again);
  if (result == JNI_OK) {
    JavaThread *thread = JavaThread::current();
    /* thread is thread_in_vm here */
    *vm = (JavaVM *)(&main_vm);
    *(JNIEnv**)penv = thread->jni_environment();

    // Tracks the time application was running before GC
    RuntimeService::record_application_start();

    // Notify JVMTI
    if (JvmtiExport::should_post_thread_life()) {
       JvmtiExport::post_thread_start(thread);
    }
5261

A
apetushkov 已提交
5262
    post_thread_start_event(thread);
5263

5264
#ifndef PRODUCT
5265
  #ifndef CALL_TEST_FUNC_WITH_WRAPPER_IF_NEEDED
5266 5267 5268
    #define CALL_TEST_FUNC_WITH_WRAPPER_IF_NEEDED(f) f()
  #endif

D
duke 已提交
5269
    // Check if we should compile all classes on bootclasspath
5270 5271 5272 5273 5274 5275 5276 5277 5278
    if (CompileTheWorld) ClassLoader::compile_the_world();
    if (ReplayCompiles) ciReplay::replay(thread);

    // Some platforms (like Win*) need a wrapper around these test
    // functions in order to properly handle error conditions.
    CALL_TEST_FUNC_WITH_WRAPPER_IF_NEEDED(test_error_handler);
    CALL_TEST_FUNC_WITH_WRAPPER_IF_NEEDED(execute_internal_vm_tests);
#endif

D
duke 已提交
5279 5280 5281 5282 5283 5284 5285 5286 5287 5288 5289 5290 5291 5292 5293 5294 5295 5296 5297
    // Since this is not a JVM_ENTRY we have to set the thread state manually before leaving.
    ThreadStateTransition::transition_and_fence(thread, _thread_in_vm, _thread_in_native);
  } else {
    if (can_try_again) {
      // reset safe_to_recreate_vm to 1 so that retrial would be possible
      safe_to_recreate_vm = 1;
    }

    // Creation failed. We must reset vm_created
    *vm = 0;
    *(JNIEnv**)penv = 0;
    // reset vm_created last to avoid race condition. Use OrderAccess to
    // control both compiler and architectural-based reordering.
    OrderAccess::release_store(&vm_created, 0);
  }

  return result;
}

D
dcubed 已提交
5298
#ifndef USDT2
D
duke 已提交
5299 5300 5301
HS_DTRACE_PROBE_DECL3(hotspot_jni, GetCreatedJavaVMs__entry, \
  JavaVM**, jsize, jsize*);
HS_DTRACE_PROBE_DECL1(hotspot_jni, GetCreatedJavaVMs__return, jint);
D
dcubed 已提交
5302
#endif /* !USDT2 */
D
duke 已提交
5303 5304 5305 5306

_JNI_IMPORT_OR_EXPORT_ jint JNICALL JNI_GetCreatedJavaVMs(JavaVM **vm_buf, jsize bufLen, jsize *numVMs) {
  // See bug 4367188, the wrapper can sometimes cause VM crashes
  // JNIWrapper("GetCreatedJavaVMs");
D
dcubed 已提交
5307
#ifndef USDT2
D
duke 已提交
5308 5309
  HS_DTRACE_PROBE3(hotspot_jni, GetCreatedJavaVMs__entry, \
    vm_buf, bufLen, numVMs);
D
dcubed 已提交
5310 5311 5312 5313
#else /* USDT2 */
  HOTSPOT_JNI_GETCREATEDJAVAVMS_ENTRY(
                                      (void **) vm_buf, bufLen, (uintptr_t *) numVMs);
#endif /* USDT2 */
D
duke 已提交
5314 5315 5316 5317 5318 5319
  if (vm_created) {
    if (numVMs != NULL) *numVMs = 1;
    if (bufLen > 0)     *vm_buf = (JavaVM *)(&main_vm);
  } else {
    if (numVMs != NULL) *numVMs = 0;
  }
D
dcubed 已提交
5320
#ifndef USDT2
D
duke 已提交
5321
  HS_DTRACE_PROBE1(hotspot_jni, GetCreatedJavaVMs__return, JNI_OK);
D
dcubed 已提交
5322 5323 5324 5325
#else /* USDT2 */
  HOTSPOT_JNI_GETCREATEDJAVAVMS_RETURN(
                                    JNI_OK);
#endif /* USDT2 */
D
duke 已提交
5326 5327 5328 5329 5330
  return JNI_OK;
}

extern "C" {

D
dcubed 已提交
5331
#ifndef USDT2
D
duke 已提交
5332
DT_RETURN_MARK_DECL(DestroyJavaVM, jint);
D
dcubed 已提交
5333 5334 5335 5336
#else /* USDT2 */
DT_RETURN_MARK_DECL(DestroyJavaVM, jint
                    , HOTSPOT_JNI_DESTROYJAVAVM_RETURN(_ret_ref));
#endif /* USDT2 */
D
duke 已提交
5337 5338

jint JNICALL jni_DestroyJavaVM(JavaVM *vm) {
D
dcubed 已提交
5339
#ifndef USDT2
D
duke 已提交
5340
  DTRACE_PROBE1(hotspot_jni, DestroyJavaVM__entry, vm);
D
dcubed 已提交
5341 5342 5343 5344
#else /* USDT2 */
  HOTSPOT_JNI_DESTROYJAVAVM_ENTRY(
                                  vm);
#endif /* USDT2 */
D
duke 已提交
5345 5346 5347 5348 5349 5350 5351 5352 5353 5354 5355 5356 5357 5358 5359 5360 5361 5362 5363 5364 5365 5366 5367 5368 5369 5370 5371 5372 5373 5374 5375 5376 5377 5378 5379 5380 5381 5382 5383 5384 5385 5386 5387 5388 5389 5390 5391 5392 5393 5394 5395 5396 5397 5398 5399 5400 5401 5402 5403 5404 5405 5406
  jint res = JNI_ERR;
  DT_RETURN_MARK(DestroyJavaVM, jint, (const jint&)res);

  if (!vm_created) {
    res = JNI_ERR;
    return res;
  }

  JNIWrapper("DestroyJavaVM");
  JNIEnv *env;
  JavaVMAttachArgs destroyargs;
  destroyargs.version = CurrentVersion;
  destroyargs.name = (char *)"DestroyJavaVM";
  destroyargs.group = NULL;
  res = vm->AttachCurrentThread((void **)&env, (void *)&destroyargs);
  if (res != JNI_OK) {
    return res;
  }

  // Since this is not a JVM_ENTRY we have to set the thread state manually before entering.
  JavaThread* thread = JavaThread::current();
  ThreadStateTransition::transition_from_native(thread, _thread_in_vm);
  if (Threads::destroy_vm()) {
    // Should not change thread state, VM is gone
    vm_created = false;
    res = JNI_OK;
    return res;
  } else {
    ThreadStateTransition::transition_and_fence(thread, _thread_in_vm, _thread_in_native);
    res = JNI_ERR;
    return res;
  }
}


static jint attach_current_thread(JavaVM *vm, void **penv, void *_args, bool daemon) {
  JavaVMAttachArgs *args = (JavaVMAttachArgs *) _args;

  // Check below commented out from JDK1.2fcs as well
  /*
  if (args && (args->version != JNI_VERSION_1_1 || args->version != JNI_VERSION_1_2)) {
    return JNI_EVERSION;
  }
  */

  Thread* t = ThreadLocalStorage::get_thread_slow();
  if (t != NULL) {
    // If the thread has been attached this operation is a no-op
    *(JNIEnv**)penv = ((JavaThread*) t)->jni_environment();
    return JNI_OK;
  }

  // Create a thread and mark it as attaching so it will be skipped by the
  // ThreadsListEnumerator - see CR 6404306
  JavaThread* thread = new JavaThread(true);

  // Set correct safepoint info. The thread is going to call into Java when
  // initializing the Java level thread object. Hence, the correct state must
  // be set in order for the Safepoint code to deal with it correctly.
  thread->set_thread_state(_thread_in_vm);
  // Must do this before initialize_thread_local_storage
  thread->record_stack_base_and_size();
Y
yunyao.zxl 已提交
5407 5408 5409
  if (EnableCoroutine) {
    thread->initialize_coroutine_support();
  }
D
duke 已提交
5410 5411 5412 5413 5414 5415
  thread->initialize_thread_local_storage();

  if (!os::create_attached_thread(thread)) {
    delete thread;
    return JNI_ERR;
  }
5416 5417 5418
  // Enable stack overflow checks
  thread->create_stack_guard_pages();

D
duke 已提交
5419 5420
  thread->initialize_tlab();

5421 5422
  thread->cache_global_variables();

D
duke 已提交
5423 5424 5425 5426 5427 5428 5429 5430 5431 5432 5433 5434 5435 5436 5437 5438 5439 5440 5441 5442 5443 5444 5445 5446 5447 5448 5449 5450 5451 5452 5453 5454 5455 5456 5457 5458 5459 5460 5461 5462 5463
  // Crucial that we do not have a safepoint check for this thread, since it has
  // not been added to the Thread list yet.
  { Threads_lock->lock_without_safepoint_check();
    // This must be inside this lock in order to get FullGCALot to work properly, i.e., to
    // avoid this thread trying to do a GC before it is added to the thread-list
    thread->set_active_handles(JNIHandleBlock::allocate_block());
    Threads::add(thread, daemon);
    Threads_lock->unlock();
  }
  // Create thread group and name info from attach arguments
  oop group = NULL;
  char* thread_name = NULL;
  if (args != NULL && Threads::is_supported_jni_version(args->version)) {
    group = JNIHandles::resolve(args->group);
    thread_name = args->name; // may be NULL
  }
  if (group == NULL) group = Universe::main_thread_group();

  // Create Java level thread object and attach it to this thread
  bool attach_failed = false;
  {
    EXCEPTION_MARK;
    HandleMark hm(THREAD);
    Handle thread_group(THREAD, group);
    thread->allocate_threadObj(thread_group, thread_name, daemon, THREAD);
    if (HAS_PENDING_EXCEPTION) {
      CLEAR_PENDING_EXCEPTION;
      // cleanup outside the handle mark.
      attach_failed = true;
    }
  }

  if (attach_failed) {
    // Added missing cleanup
    thread->cleanup_failed_attach_current_thread();
    return JNI_ERR;
  }

  // mark the thread as no longer attaching
  // this uses a fence to push the change through so we don't have
  // to regrab the threads_lock
D
dcubed 已提交
5464
  thread->set_done_attaching_via_jni();
D
duke 已提交
5465 5466 5467 5468 5469 5470 5471 5472 5473 5474

  // Set java thread status.
  java_lang_Thread::set_thread_status(thread->threadObj(),
              java_lang_Thread::RUNNABLE);

  // Notify the debugger
  if (JvmtiExport::should_post_thread_life()) {
    JvmtiExport::post_thread_start(thread);
  }

A
apetushkov 已提交
5475
  post_thread_start_event(thread);
5476

D
duke 已提交
5477 5478
  *(JNIEnv**)penv = thread->jni_environment();

Y
yunyao.zxl 已提交
5479 5480 5481 5482
  if (EnableCoroutine) {
    Coroutine::initialize_coroutine_support(JavaThread::current());
  }

D
duke 已提交
5483 5484 5485 5486 5487 5488 5489 5490 5491 5492 5493 5494 5495 5496 5497
  // Now leaving the VM, so change thread_state. This is normally automatically taken care
  // of in the JVM_ENTRY. But in this situation we have to do it manually. Notice, that by
  // using ThreadStateTransition::transition, we do a callback to the safepoint code if
  // needed.

  ThreadStateTransition::transition_and_fence(thread, _thread_in_vm, _thread_in_native);

  // Perform any platform dependent FPU setup
  os::setup_fpu();

  return JNI_OK;
}


jint JNICALL jni_AttachCurrentThread(JavaVM *vm, void **penv, void *_args) {
D
dcubed 已提交
5498
#ifndef USDT2
D
duke 已提交
5499
  DTRACE_PROBE3(hotspot_jni, AttachCurrentThread__entry, vm, penv, _args);
D
dcubed 已提交
5500 5501 5502 5503
#else /* USDT2 */
  HOTSPOT_JNI_ATTACHCURRENTTHREAD_ENTRY(
                                        vm, penv, _args);
#endif /* USDT2 */
D
duke 已提交
5504
  if (!vm_created) {
D
dcubed 已提交
5505
#ifndef USDT2
D
duke 已提交
5506
    DTRACE_PROBE1(hotspot_jni, AttachCurrentThread__return, JNI_ERR);
D
dcubed 已提交
5507 5508 5509 5510
#else /* USDT2 */
  HOTSPOT_JNI_ATTACHCURRENTTHREAD_RETURN(
                                         (uint32_t) JNI_ERR);
#endif /* USDT2 */
D
duke 已提交
5511 5512 5513 5514 5515
    return JNI_ERR;
  }

  JNIWrapper("AttachCurrentThread");
  jint ret = attach_current_thread(vm, penv, _args, false);
D
dcubed 已提交
5516
#ifndef USDT2
D
duke 已提交
5517
  DTRACE_PROBE1(hotspot_jni, AttachCurrentThread__return, ret);
D
dcubed 已提交
5518 5519 5520 5521
#else /* USDT2 */
  HOTSPOT_JNI_ATTACHCURRENTTHREAD_RETURN(
                                         ret);
#endif /* USDT2 */
D
duke 已提交
5522 5523 5524 5525 5526
  return ret;
}


jint JNICALL jni_DetachCurrentThread(JavaVM *vm)  {
D
dcubed 已提交
5527
#ifndef USDT2
D
duke 已提交
5528
  DTRACE_PROBE1(hotspot_jni, DetachCurrentThread__entry, vm);
D
dcubed 已提交
5529 5530 5531 5532
#else /* USDT2 */
  HOTSPOT_JNI_DETACHCURRENTTHREAD_ENTRY(
                                        vm);
#endif /* USDT2 */
D
duke 已提交
5533 5534 5535 5536 5537 5538
  VM_Exit::block_if_vm_exited();

  JNIWrapper("DetachCurrentThread");

  // If the thread has been deattacted the operations is a no-op
  if (ThreadLocalStorage::thread() == NULL) {
D
dcubed 已提交
5539
#ifndef USDT2
D
duke 已提交
5540
    DTRACE_PROBE1(hotspot_jni, DetachCurrentThread__return, JNI_OK);
D
dcubed 已提交
5541 5542 5543 5544
#else /* USDT2 */
  HOTSPOT_JNI_DETACHCURRENTTHREAD_RETURN(
                                         JNI_OK);
#endif /* USDT2 */
D
duke 已提交
5545 5546 5547 5548 5549
    return JNI_OK;
  }

  JavaThread* thread = JavaThread::current();
  if (thread->has_last_Java_frame()) {
D
dcubed 已提交
5550
#ifndef USDT2
D
duke 已提交
5551
    DTRACE_PROBE1(hotspot_jni, DetachCurrentThread__return, JNI_ERR);
D
dcubed 已提交
5552 5553 5554 5555
#else /* USDT2 */
  HOTSPOT_JNI_DETACHCURRENTTHREAD_RETURN(
                                         (uint32_t) JNI_ERR);
#endif /* USDT2 */
D
duke 已提交
5556 5557 5558 5559 5560 5561 5562 5563 5564 5565 5566 5567 5568 5569 5570 5571 5572 5573 5574 5575
    // Can't detach a thread that's running java, that can't work.
    return JNI_ERR;
  }

  // Safepoint support. Have to do call-back to safepoint code, if in the
  // middel of a safepoint operation
  ThreadStateTransition::transition_from_native(thread, _thread_in_vm);

  // XXX: Note that JavaThread::exit() call below removes the guards on the
  // stack pages set up via enable_stack_{red,yellow}_zone() calls
  // above in jni_AttachCurrentThread. Unfortunately, while the setting
  // of the guards is visible in jni_AttachCurrentThread above,
  // the removal of the guards is buried below in JavaThread::exit()
  // here. The abstraction should be more symmetrically either exposed
  // or hidden (e.g. it could probably be hidden in the same
  // (platform-dependent) methods where we do alternate stack
  // maintenance work?)
  thread->exit(false, JavaThread::jni_detach);
  delete thread;

D
dcubed 已提交
5576
#ifndef USDT2
D
duke 已提交
5577
  DTRACE_PROBE1(hotspot_jni, DetachCurrentThread__return, JNI_OK);
D
dcubed 已提交
5578 5579 5580 5581
#else /* USDT2 */
  HOTSPOT_JNI_DETACHCURRENTTHREAD_RETURN(
                                         JNI_OK);
#endif /* USDT2 */
D
duke 已提交
5582 5583 5584
  return JNI_OK;
}

D
dcubed 已提交
5585
#ifndef USDT2
D
duke 已提交
5586
DT_RETURN_MARK_DECL(GetEnv, jint);
D
dcubed 已提交
5587 5588 5589 5590
#else /* USDT2 */
DT_RETURN_MARK_DECL(GetEnv, jint
                    , HOTSPOT_JNI_GETENV_RETURN(_ret_ref));
#endif /* USDT2 */
D
duke 已提交
5591 5592

jint JNICALL jni_GetEnv(JavaVM *vm, void **penv, jint version) {
D
dcubed 已提交
5593
#ifndef USDT2
D
duke 已提交
5594
  DTRACE_PROBE3(hotspot_jni, GetEnv__entry, vm, penv, version);
D
dcubed 已提交
5595 5596 5597 5598
#else /* USDT2 */
  HOTSPOT_JNI_GETENV_ENTRY(
                           vm, penv, version);
#endif /* USDT2 */
D
duke 已提交
5599 5600 5601 5602 5603 5604 5605 5606 5607
  jint ret = JNI_ERR;
  DT_RETURN_MARK(GetEnv, jint, (const jint&)ret);

  if (!vm_created) {
    *penv = NULL;
    ret = JNI_EDETACHED;
    return ret;
  }

5608
  if (JniExportedInterface::GetExportedInterface(vm, penv, version, &ret)) {
D
duke 已提交
5609 5610 5611 5612 5613 5614 5615 5616 5617 5618 5619 5620 5621 5622 5623 5624 5625
    return ret;
  }

#ifndef JVMPI_VERSION_1
// need these in order to be polite about older agents
#define JVMPI_VERSION_1   ((jint)0x10000001)
#define JVMPI_VERSION_1_1 ((jint)0x10000002)
#define JVMPI_VERSION_1_2 ((jint)0x10000003)
#endif // !JVMPI_VERSION_1

  Thread* thread = ThreadLocalStorage::thread();
  if (thread != NULL && thread->is_Java_thread()) {
    if (Threads::is_supported_jni_version_including_1_1(version)) {
      *(JNIEnv**)penv = ((JavaThread*) thread)->jni_environment();
      ret = JNI_OK;
      return ret;

5626 5627 5628 5629
    } else if (TENANT_ENV_VERSION_1_0 == version) { //get the tenant environment for java thread.
      *(TenantEnv**)penv = ((JavaThread*) thread)->tenant_environment();
      ret = JNI_OK;
      return ret;
D
duke 已提交
5630 5631 5632 5633 5634 5635 5636 5637 5638 5639 5640 5641 5642 5643 5644 5645 5646 5647 5648 5649 5650 5651 5652 5653 5654 5655
    } else if (version == JVMPI_VERSION_1 ||
               version == JVMPI_VERSION_1_1 ||
               version == JVMPI_VERSION_1_2) {
      tty->print_cr("ERROR: JVMPI, an experimental interface, is no longer supported.");
      tty->print_cr("Please use the supported interface: the JVM Tool Interface (JVM TI).");
      ret = JNI_EVERSION;
      return ret;
    } else if (JvmtiExport::is_jvmdi_version(version)) {
      tty->print_cr("FATAL ERROR: JVMDI is no longer supported.");
      tty->print_cr("Please use the supported interface: the JVM Tool Interface (JVM TI).");
      ret = JNI_EVERSION;
      return ret;
    } else {
      *penv = NULL;
      ret = JNI_EVERSION;
      return ret;
    }
  } else {
    *penv = NULL;
    ret = JNI_EDETACHED;
    return ret;
  }
}


jint JNICALL jni_AttachCurrentThreadAsDaemon(JavaVM *vm, void **penv, void *_args) {
D
dcubed 已提交
5656
#ifndef USDT2
D
duke 已提交
5657
  DTRACE_PROBE3(hotspot_jni, AttachCurrentThreadAsDaemon__entry, vm, penv, _args);
D
dcubed 已提交
5658 5659 5660 5661
#else /* USDT2 */
  HOTSPOT_JNI_ATTACHCURRENTTHREADASDAEMON_ENTRY(
                                                vm, penv, _args);
#endif /* USDT2 */
D
duke 已提交
5662
  if (!vm_created) {
D
dcubed 已提交
5663
#ifndef USDT2
D
duke 已提交
5664
    DTRACE_PROBE1(hotspot_jni, AttachCurrentThreadAsDaemon__return, JNI_ERR);
D
dcubed 已提交
5665 5666 5667 5668
#else /* USDT2 */
  HOTSPOT_JNI_ATTACHCURRENTTHREADASDAEMON_RETURN(
                                                 (uint32_t) JNI_ERR);
#endif /* USDT2 */
D
duke 已提交
5669 5670 5671 5672 5673
    return JNI_ERR;
  }

  JNIWrapper("AttachCurrentThreadAsDaemon");
  jint ret = attach_current_thread(vm, penv, _args, true);
D
dcubed 已提交
5674
#ifndef USDT2
D
duke 已提交
5675
  DTRACE_PROBE1(hotspot_jni, AttachCurrentThreadAsDaemon__return, ret);
D
dcubed 已提交
5676 5677 5678 5679
#else /* USDT2 */
  HOTSPOT_JNI_ATTACHCURRENTTHREADASDAEMON_RETURN(
                                                 ret);
#endif /* USDT2 */
D
duke 已提交
5680 5681 5682 5683 5684 5685 5686 5687 5688 5689 5690 5691 5692 5693 5694 5695 5696
  return ret;
}


} // End extern "C"

const struct JNIInvokeInterface_ jni_InvokeInterface = {
    NULL,
    NULL,
    NULL,

    jni_DestroyJavaVM,
    jni_AttachCurrentThread,
    jni_DetachCurrentThread,
    jni_GetEnv,
    jni_AttachCurrentThreadAsDaemon
};