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