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