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