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