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