41 #include "classfile/modules.hpp"
42 #include "classfile/packageEntry.hpp"
43 #include "classfile/stringTable.hpp"
44 #include "classfile/symbolTable.hpp"
45 #include "classfile/systemDictionary.hpp"
46 #include "classfile/vmClasses.hpp"
47 #include "classfile/vmSymbols.hpp"
48 #include "gc/shared/collectedHeap.inline.hpp"
49 #include "interpreter/bytecode.hpp"
50 #include "interpreter/bytecodeUtils.hpp"
51 #include "jfr/jfrEvents.hpp"
52 #include "jvm.h"
53 #include "logging/log.hpp"
54 #include "memory/oopFactory.hpp"
55 #include "memory/referenceType.hpp"
56 #include "memory/resourceArea.hpp"
57 #include "memory/universe.hpp"
58 #include "oops/access.inline.hpp"
59 #include "oops/constantPool.hpp"
60 #include "oops/fieldStreams.inline.hpp"
61 #include "oops/instanceKlass.hpp"
62 #include "oops/klass.inline.hpp"
63 #include "oops/method.hpp"
64 #include "oops/objArrayKlass.hpp"
65 #include "oops/objArrayOop.inline.hpp"
66 #include "oops/oop.inline.hpp"
67 #include "oops/recordComponent.hpp"
68 #include "prims/foreignGlobals.hpp"
69 #include "prims/jvm_misc.hpp"
70 #include "prims/jvmtiExport.hpp"
71 #include "prims/jvmtiThreadState.inline.hpp"
72 #include "prims/stackwalk.hpp"
73 #include "runtime/arguments.hpp"
74 #include "runtime/atomicAccess.hpp"
75 #include "runtime/continuation.hpp"
76 #include "runtime/deoptimization.hpp"
77 #include "runtime/globals_extension.hpp"
78 #include "runtime/handles.inline.hpp"
79 #include "runtime/handshake.hpp"
80 #include "runtime/init.hpp"
81 #include "runtime/interfaceSupport.inline.hpp"
82 #include "runtime/java.hpp"
83 #include "runtime/javaCalls.hpp"
84 #include "runtime/javaThread.hpp"
85 #include "runtime/jfieldIDWorkaround.hpp"
86 #include "runtime/jniHandles.inline.hpp"
87 #include "runtime/os.inline.hpp"
396
397 return (jobjectArray) JNIHandles::make_local(THREAD, result_h());
398 JVM_END
399
400
401 /*
402 * Return the temporary directory that the VM uses for the attach
403 * and perf data files.
404 *
405 * It is important that this directory is well-known and the
406 * same for all VM instances. It cannot be affected by configuration
407 * variables such as java.io.tmpdir.
408 */
409 JVM_ENTRY(jstring, JVM_GetTemporaryDirectory(JNIEnv *env))
410 HandleMark hm(THREAD);
411 const char* temp_dir = os::get_temp_directory();
412 Handle h = java_lang_String::create_from_platform_dependent_str(temp_dir, CHECK_NULL);
413 return (jstring) JNIHandles::make_local(THREAD, h());
414 JVM_END
415
416
417 // java.lang.Runtime /////////////////////////////////////////////////////////////////////////
418
419 extern volatile jint vm_created;
420
421 JVM_ENTRY_NO_ENV(void, JVM_BeforeHalt())
422 EventShutdown event;
423 if (event.should_commit()) {
424 event.set_reason("Shutdown requested from Java");
425 event.commit();
426 }
427 JVM_END
428
429
430 JVM_ENTRY_NO_ENV(void, JVM_Halt(jint code))
431 before_exit(thread, true);
432 vm_exit(code);
433 JVM_END
434
435
604
605 Handle stackStream_h(THREAD, JNIHandles::resolve_non_null(stackStream));
606 return StackWalk::fetchNextBatch(stackStream_h, mode, anchor, last_batch_count, buffer_size,
607 start_index, frames_array_h, THREAD);
608 JVM_END
609
610 JVM_ENTRY(void, JVM_SetStackWalkContinuation(JNIEnv *env, jobject stackStream, jlong anchor, jobjectArray frames, jobject cont))
611 objArrayOop fa = objArrayOop(JNIHandles::resolve_non_null(frames));
612 objArrayHandle frames_array_h(THREAD, fa);
613 Handle stackStream_h(THREAD, JNIHandles::resolve_non_null(stackStream));
614 Handle cont_h(THREAD, JNIHandles::resolve_non_null(cont));
615
616 StackWalk::setContinuation(stackStream_h, anchor, frames_array_h, cont_h, THREAD);
617 JVM_END
618
619 // java.lang.Object ///////////////////////////////////////////////
620
621
622 JVM_ENTRY(jint, JVM_IHashCode(JNIEnv* env, jobject handle))
623 // as implemented in the classic virtual machine; return 0 if object is null
624 return handle == nullptr ? 0 :
625 checked_cast<jint>(ObjectSynchronizer::FastHashCode (THREAD, JNIHandles::resolve_non_null(handle)));
626 JVM_END
627
628
629 JVM_ENTRY(void, JVM_MonitorWait(JNIEnv* env, jobject handle, jlong ms))
630 Handle obj(THREAD, JNIHandles::resolve_non_null(handle));
631 ObjectSynchronizer::wait(obj, ms, CHECK);
632 JVM_END
633
634
635 JVM_ENTRY(void, JVM_MonitorNotify(JNIEnv* env, jobject handle))
636 Handle obj(THREAD, JNIHandles::resolve_non_null(handle));
637 ObjectSynchronizer::notify(obj, CHECK);
638 JVM_END
639
640
641 JVM_ENTRY(void, JVM_MonitorNotifyAll(JNIEnv* env, jobject handle))
642 Handle obj(THREAD, JNIHandles::resolve_non_null(handle));
643 ObjectSynchronizer::notifyall(obj, CHECK);
644 JVM_END
645
653 // Just checking that the cloneable flag is set correct
654 if (obj->is_array()) {
655 guarantee(klass->is_cloneable(), "all arrays are cloneable");
656 } else {
657 guarantee(obj->is_instance(), "should be instanceOop");
658 bool cloneable = klass->is_subtype_of(vmClasses::Cloneable_klass());
659 guarantee(cloneable == klass->is_cloneable(), "incorrect cloneable flag");
660 }
661 #endif
662
663 // Check if class of obj supports the Cloneable interface.
664 // All arrays are considered to be cloneable (See JLS 20.1.5).
665 // All j.l.r.Reference classes are considered non-cloneable.
666 if (!klass->is_cloneable() ||
667 (klass->is_instance_klass() &&
668 InstanceKlass::cast(klass)->reference_type() != REF_NONE)) {
669 ResourceMark rm(THREAD);
670 THROW_MSG_NULL(vmSymbols::java_lang_CloneNotSupportedException(), klass->external_name());
671 }
672
673 // Make shallow object copy
674 const size_t size = obj->size();
675 oop new_obj_oop = nullptr;
676 if (obj->is_array()) {
677 const int length = ((arrayOop)obj())->length();
678 new_obj_oop = Universe::heap()->array_allocate(klass, size, length,
679 /* do_zero */ true, CHECK_NULL);
680 } else {
681 new_obj_oop = Universe::heap()->obj_allocate(klass, size, CHECK_NULL);
682 }
683
684 HeapAccess<>::clone(obj(), new_obj_oop, size);
685
686 Handle new_obj(THREAD, new_obj_oop);
687 // Caution: this involves a java upcall, so the clone should be
688 // "gc-robust" by this stage.
689 if (klass->has_finalizer()) {
690 assert(obj->is_instance(), "should be instanceOop");
691 new_obj_oop = InstanceKlass::register_finalizer(instanceOop(new_obj()), CHECK_NULL);
692 new_obj = Handle(THREAD, new_obj_oop);
1149 oop result = java_lang_Class::name(java_class, CHECK_NULL);
1150 return (jstring) JNIHandles::make_local(THREAD, result);
1151 JVM_END
1152
1153
1154 JVM_ENTRY(jobjectArray, JVM_GetClassInterfaces(JNIEnv *env, jclass cls))
1155 JvmtiVMObjectAllocEventCollector oam;
1156 oop mirror = JNIHandles::resolve_non_null(cls);
1157
1158 // Special handling for primitive objects
1159 if (java_lang_Class::is_primitive(mirror)) {
1160 // Primitive objects does not have any interfaces
1161 objArrayOop r = oopFactory::new_objArray(vmClasses::Class_klass(), 0, CHECK_NULL);
1162 return (jobjectArray) JNIHandles::make_local(THREAD, r);
1163 }
1164
1165 Klass* klass = java_lang_Class::as_Klass(mirror);
1166 // Figure size of result array
1167 int size;
1168 if (klass->is_instance_klass()) {
1169 size = InstanceKlass::cast(klass)->local_interfaces()->length();
1170 } else {
1171 assert(klass->is_objArray_klass() || klass->is_typeArray_klass(), "Illegal mirror klass");
1172 size = 2;
1173 }
1174
1175 // Allocate result array
1176 objArrayOop r = oopFactory::new_objArray(vmClasses::Class_klass(), size, CHECK_NULL);
1177 objArrayHandle result (THREAD, r);
1178 // Fill in result
1179 if (klass->is_instance_klass()) {
1180 // Regular instance klass, fill in all local interfaces
1181 for (int index = 0; index < size; index++) {
1182 InstanceKlass* k = InstanceKlass::cast(klass)->local_interfaces()->at(index);
1183 result->obj_at_put(index, k->java_mirror());
1184 }
1185 } else {
1186 // All arrays implement java.lang.Cloneable and java.io.Serializable
1187 result->obj_at_put(0, vmClasses::Cloneable_klass()->java_mirror());
1188 result->obj_at_put(1, vmClasses::Serializable_klass()->java_mirror());
1189 }
1190 return (jobjectArray) JNIHandles::make_local(THREAD, result());
1191 JVM_END
1192
1193
1194 JVM_ENTRY(jboolean, JVM_IsHiddenClass(JNIEnv *env, jclass cls))
1195 oop mirror = JNIHandles::resolve_non_null(cls);
1196 if (java_lang_Class::is_primitive(mirror)) {
1197 return JNI_FALSE;
1198 }
1199 Klass* k = java_lang_Class::as_Klass(mirror);
1200 return k->is_hidden();
1201 JVM_END
1202
1203
1204 class ScopedValueBindingsResolver {
1205 public:
1206 InstanceKlass* Carrier_klass;
1207 ScopedValueBindingsResolver(JavaThread* THREAD) {
1208 Klass *k = SystemDictionary::resolve_or_fail(vmSymbols::java_lang_ScopedValue_Carrier(), true, THREAD);
1209 Carrier_klass = InstanceKlass::cast(k);
1210 }
1211 };
1212
1213 JVM_ENTRY(jobject, JVM_FindScopedValueBindings(JNIEnv *env, jclass cls))
1214 ResourceMark rm(THREAD);
1215 GrowableArray<Handle>* local_array = new GrowableArray<Handle>(12);
1216 JvmtiVMObjectAllocEventCollector oam;
1217
1218 static ScopedValueBindingsResolver resolver(THREAD);
1219
1220 // Iterate through Java frames
1221 vframeStream vfst(thread);
1222 for(; !vfst.at_end(); vfst.next()) {
1223 int loc = -1;
1664 }
1665
1666 InstanceKlass* k = java_lang_Class::as_InstanceKlass(ofMirror);
1667
1668 // Ensure class is linked
1669 k->link_class(CHECK_NULL);
1670
1671 Array<Method*>* methods = k->methods();
1672 int methods_length = methods->length();
1673
1674 // Save original method_idnum in case of redefinition, which can change
1675 // the idnum of obsolete methods. The new method will have the same idnum
1676 // but if we refresh the methods array, the counts will be wrong.
1677 ResourceMark rm(THREAD);
1678 GrowableArray<int>* idnums = new GrowableArray<int>(methods_length);
1679 int num_methods = 0;
1680
1681 // Select methods matching the criteria.
1682 for (int i = 0; i < methods_length; i++) {
1683 Method* method = methods->at(i);
1684 if (want_constructor && !method->is_object_initializer()) {
1685 continue;
1686 }
1687 if (!want_constructor &&
1688 (method->is_object_initializer() || method->is_static_initializer() ||
1689 method->is_overpass())) {
1690 continue;
1691 }
1692 if (publicOnly && !method->is_public()) {
1693 continue;
1694 }
1695 idnums->push(method->method_idnum());
1696 ++num_methods;
1697 }
1698
1699 // Allocate result
1700 objArrayOop r = oopFactory::new_objArray(klass, num_methods, CHECK_NULL);
1701 objArrayHandle result (THREAD, r);
1702
1703 // Now just put the methods that we selected above, but go by their idnum
1704 // in case of redefinition. The methods can be redefined at any safepoint,
1705 // so above when allocating the oop array and below when creating reflect
1706 // objects.
1707 for (int i = 0; i < num_methods; i++) {
1708 methodHandle method(THREAD, k->method_with_idnum(idnums->at(i)));
1709 if (method.is_null()) {
1710 // Method may have been deleted and seems this API can handle null
1711 // Otherwise should probably put a method that throws NSME
1712 result->obj_at_put(i, nullptr);
1713 } else {
1714 oop m;
1715 if (want_constructor) {
1716 m = Reflection::new_constructor(method, CHECK_NULL);
1717 } else {
1718 m = Reflection::new_method(method, false, CHECK_NULL);
1719 }
1720 result->obj_at_put(i, m);
1721 }
1722 }
1723
1724 return (jobjectArray) JNIHandles::make_local(THREAD, result());
1725 }
1726
1727 JVM_ENTRY(jobjectArray, JVM_GetClassDeclaredMethods(JNIEnv *env, jclass ofClass, jboolean publicOnly))
1728 {
1729 return get_class_declared_methods_helper(env, ofClass, publicOnly,
1730 /*want_constructor*/ false,
1731 vmClasses::reflect_Method_klass(), THREAD);
1732 }
1733 JVM_END
1734
1735 JVM_ENTRY(jobjectArray, JVM_GetClassDeclaredConstructors(JNIEnv *env, jclass ofClass, jboolean publicOnly))
1956 constantTag tag = cp->tag_at(index);
1957 if (!tag.is_method() && !tag.is_interface_method()) {
1958 THROW_MSG_NULL(vmSymbols::java_lang_IllegalArgumentException(), "Wrong type at constant pool index");
1959 }
1960 int klass_ref = cp->uncached_klass_ref_index_at(index);
1961 Klass* k_o;
1962 if (force_resolution) {
1963 k_o = cp->klass_at(klass_ref, CHECK_NULL);
1964 } else {
1965 k_o = ConstantPool::klass_at_if_loaded(cp, klass_ref);
1966 if (k_o == nullptr) return nullptr;
1967 }
1968 InstanceKlass* k = InstanceKlass::cast(k_o);
1969 Symbol* name = cp->uncached_name_ref_at(index);
1970 Symbol* sig = cp->uncached_signature_ref_at(index);
1971 methodHandle m (THREAD, k->find_method(name, sig));
1972 if (m.is_null()) {
1973 THROW_MSG_NULL(vmSymbols::java_lang_RuntimeException(), "Unable to look up method in target class");
1974 }
1975 oop method;
1976 if (m->is_object_initializer()) {
1977 method = Reflection::new_constructor(m, CHECK_NULL);
1978 } else {
1979 // new_method accepts <clinit> as Method here
1980 method = Reflection::new_method(m, true, CHECK_NULL);
1981 }
1982 return JNIHandles::make_local(THREAD, method);
1983 }
1984
1985 JVM_ENTRY(jobject, JVM_ConstantPoolGetMethodAt(JNIEnv *env, jobject obj, jint index))
1986 {
1987 JvmtiVMObjectAllocEventCollector oam;
1988 constantPoolHandle cp = constantPoolHandle(THREAD, reflect_ConstantPool::get_cp(JNIHandles::resolve_non_null(obj)));
1989 bounds_check(cp, index, CHECK_NULL);
1990 jobject res = get_method_at_helper(cp, index, true, CHECK_NULL);
1991 return res;
1992 }
1993 JVM_END
1994
1995 JVM_ENTRY(jobject, JVM_ConstantPoolGetMethodAtIfLoaded(JNIEnv *env, jobject obj, jint index))
1996 {
1997 JvmtiVMObjectAllocEventCollector oam;
1998 constantPoolHandle cp = constantPoolHandle(THREAD, reflect_ConstantPool::get_cp(JNIHandles::resolve_non_null(obj)));
1999 bounds_check(cp, index, CHECK_NULL);
2406
2407
2408 JVM_ENTRY(jint, JVM_GetMethodIxArgsSize(JNIEnv *env, jclass cls, int method_index))
2409 InstanceKlass* ik = get_instance_klass_considering_redefinition(cls, thread);
2410 Method* method = ik->methods()->at(method_index);
2411 return method->size_of_parameters();
2412 JVM_END
2413
2414
2415 JVM_ENTRY(jint, JVM_GetMethodIxMaxStack(JNIEnv *env, jclass cls, int method_index))
2416 InstanceKlass* ik = get_instance_klass_considering_redefinition(cls, thread);
2417 Method* method = ik->methods()->at(method_index);
2418 return method->verifier_max_stack();
2419 JVM_END
2420
2421
2422 JVM_ENTRY(jboolean, JVM_IsConstructorIx(JNIEnv *env, jclass cls, int method_index))
2423 ResourceMark rm(THREAD);
2424 InstanceKlass* ik = get_instance_klass_considering_redefinition(cls, thread);
2425 Method* method = ik->methods()->at(method_index);
2426 return method->name() == vmSymbols::object_initializer_name();
2427 JVM_END
2428
2429
2430 JVM_ENTRY(jboolean, JVM_IsVMGeneratedMethodIx(JNIEnv *env, jclass cls, int method_index))
2431 ResourceMark rm(THREAD);
2432 InstanceKlass* ik = get_instance_klass_considering_redefinition(cls, thread);
2433 Method* method = ik->methods()->at(method_index);
2434 return method->is_overpass();
2435 JVM_END
2436
2437 JVM_ENTRY(const char*, JVM_GetMethodIxNameUTF(JNIEnv *env, jclass cls, jint method_index))
2438 InstanceKlass* ik = get_instance_klass_considering_redefinition(cls, thread);
2439 Method* method = ik->methods()->at(method_index);
2440 return method->name()->as_utf8();
2441 JVM_END
2442
2443
2444 JVM_ENTRY(const char*, JVM_GetMethodIxSignatureUTF(JNIEnv *env, jclass cls, jint method_index))
2445 InstanceKlass* ik = get_instance_klass_considering_redefinition(cls, thread);
2446 Method* method = ik->methods()->at(method_index);
3200 JVM_LEAF(void*, JVM_FindLibraryEntry(void* handle, const char* name))
3201 void* find_result = os::dll_lookup(handle, name);
3202 log_info(library)("%s %s in library with handle " INTPTR_FORMAT,
3203 find_result != nullptr ? "Found" : "Failed to find",
3204 name, p2i(handle));
3205 return find_result;
3206 JVM_END
3207
3208
3209 // JNI version ///////////////////////////////////////////////////////////////////////////////
3210
3211 JVM_LEAF(jboolean, JVM_IsSupportedJNIVersion(jint version))
3212 return Threads::is_supported_jni_version_including_1_1(version);
3213 JVM_END
3214
3215
3216 JVM_LEAF(jboolean, JVM_IsPreviewEnabled(void))
3217 return Arguments::enable_preview() ? JNI_TRUE : JNI_FALSE;
3218 JVM_END
3219
3220 JVM_LEAF(jboolean, JVM_IsContinuationsSupported(void))
3221 return VMContinuations ? JNI_TRUE : JNI_FALSE;
3222 JVM_END
3223
3224 JVM_LEAF(jboolean, JVM_IsForeignLinkerSupported(void))
3225 return ForeignGlobals::is_foreign_linker_supported() ? JNI_TRUE : JNI_FALSE;
3226 JVM_END
3227
3228 JVM_LEAF(jboolean, JVM_IsStaticallyLinked(void))
3229 return is_vm_statically_linked() ? JNI_TRUE : JNI_FALSE;
3230 JVM_END
3231
3232 // String support ///////////////////////////////////////////////////////////////////////////
3233
3234 JVM_ENTRY(jstring, JVM_InternString(JNIEnv *env, jstring str))
3235 JvmtiVMObjectAllocEventCollector oam;
3236 if (str == nullptr) return nullptr;
3237 oop string = JNIHandles::resolve_non_null(str);
3238 oop result = StringTable::intern(string, CHECK_NULL);
3239 return (jstring) JNIHandles::make_local(THREAD, result);
3279
3280 jclass find_class_from_class_loader(JNIEnv* env, Symbol* name, jboolean init,
3281 Handle loader, jboolean throwError, TRAPS) {
3282 Klass* klass = SystemDictionary::resolve_or_fail(name, loader, throwError != 0, CHECK_NULL);
3283
3284 // Check if we should initialize the class
3285 if (init && klass->is_instance_klass()) {
3286 klass->initialize(CHECK_NULL);
3287 }
3288 return (jclass) JNIHandles::make_local(THREAD, klass->java_mirror());
3289 }
3290
3291
3292 // Method ///////////////////////////////////////////////////////////////////////////////////////////
3293
3294 JVM_ENTRY(jobject, JVM_InvokeMethod(JNIEnv *env, jobject method, jobject obj, jobjectArray args0))
3295 Handle method_handle;
3296 if (thread->stack_overflow_state()->stack_available((address) &method_handle) >= JVMInvokeMethodSlack) {
3297 method_handle = Handle(THREAD, JNIHandles::resolve(method));
3298 Handle receiver(THREAD, JNIHandles::resolve(obj));
3299 objArrayHandle args(THREAD, objArrayOop(JNIHandles::resolve(args0)));
3300 oop result = Reflection::invoke_method(method_handle(), receiver, args, CHECK_NULL);
3301 jobject res = JNIHandles::make_local(THREAD, result);
3302 if (JvmtiExport::should_post_vm_object_alloc()) {
3303 oop ret_type = java_lang_reflect_Method::return_type(method_handle());
3304 assert(ret_type != nullptr, "sanity check: ret_type oop must not be null!");
3305 if (java_lang_Class::is_primitive(ret_type)) {
3306 // Only for primitive type vm allocates memory for java object.
3307 // See box() method.
3308 JvmtiExport::post_vm_object_alloc(thread, result);
3309 }
3310 }
3311 return res;
3312 } else {
3313 THROW_NULL(vmSymbols::java_lang_StackOverflowError());
3314 }
3315 JVM_END
3316
3317
3318 JVM_ENTRY(jobject, JVM_NewInstanceFromConstructor(JNIEnv *env, jobject c, jobjectArray args0))
3319 oop constructor_mirror = JNIHandles::resolve(c);
3320 objArrayHandle args(THREAD, objArrayOop(JNIHandles::resolve(args0)));
3321 oop result = Reflection::invoke_constructor(constructor_mirror, args, CHECK_NULL);
3322 jobject res = JNIHandles::make_local(THREAD, result);
3323 if (JvmtiExport::should_post_vm_object_alloc()) {
3324 JvmtiExport::post_vm_object_alloc(thread, result);
3325 }
3326 return res;
3327 JVM_END
3328
3329 JVM_ENTRY(void, JVM_InitializeFromArchive(JNIEnv* env, jclass cls))
3330 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve(cls));
3331 HeapShared::initialize_from_archived_subgraph(THREAD, k);
3332 JVM_END
3333
3334 JVM_ENTRY(void, JVM_RegisterLambdaProxyClassForArchiving(JNIEnv* env,
3335 jclass caller,
3336 jstring interfaceMethodName,
3337 jobject factoryType,
3338 jobject interfaceMethodType,
3339 jobject implementationMember,
3340 jobject dynamicMethodType,
3537 JvmtiVMObjectAllocEventCollector oam;
3538
3539 // Check if threads is null
3540 if (threads == nullptr) {
3541 THROW_NULL(vmSymbols::java_lang_NullPointerException());
3542 }
3543
3544 objArrayOop a = objArrayOop(JNIHandles::resolve_non_null(threads));
3545 objArrayHandle ah(THREAD, a);
3546 int num_threads = ah->length();
3547 // check if threads is non-empty array
3548 if (num_threads == 0) {
3549 THROW_NULL(vmSymbols::java_lang_IllegalArgumentException());
3550 }
3551
3552 // check if threads is not an array of objects of Thread class
3553 Klass* k = ObjArrayKlass::cast(ah->klass())->element_klass();
3554 if (k != vmClasses::Thread_klass()) {
3555 THROW_NULL(vmSymbols::java_lang_IllegalArgumentException());
3556 }
3557
3558 ResourceMark rm(THREAD);
3559
3560 GrowableArray<instanceHandle>* thread_handle_array = new GrowableArray<instanceHandle>(num_threads);
3561 for (int i = 0; i < num_threads; i++) {
3562 oop thread_obj = ah->obj_at(i);
3563 instanceHandle h(THREAD, (instanceOop) thread_obj);
3564 thread_handle_array->append(h);
3565 }
3566
3567 // The JavaThread references in thread_handle_array are validated
3568 // in VM_ThreadDump::doit().
3569 Handle stacktraces = ThreadService::dump_stack_traces(thread_handle_array, num_threads, CHECK_NULL);
3570 return (jobjectArray)JNIHandles::make_local(THREAD, stacktraces());
3571
3572 JVM_END
3573
3574 // JVM monitoring and management support
3575 JVM_LEAF(void*, JVM_GetManagement(jint version))
3576 return Management::get_jmm_interface(version);
3577 JVM_END
3578
3579 // com.sun.tools.attach.VirtualMachine agent properties support
3580 //
3581 // Initialize the agent properties with the properties maintained in the VM
3582 JVM_ENTRY(jobject, JVM_InitAgentProperties(JNIEnv *env, jobject properties))
|
41 #include "classfile/modules.hpp"
42 #include "classfile/packageEntry.hpp"
43 #include "classfile/stringTable.hpp"
44 #include "classfile/symbolTable.hpp"
45 #include "classfile/systemDictionary.hpp"
46 #include "classfile/vmClasses.hpp"
47 #include "classfile/vmSymbols.hpp"
48 #include "gc/shared/collectedHeap.inline.hpp"
49 #include "interpreter/bytecode.hpp"
50 #include "interpreter/bytecodeUtils.hpp"
51 #include "jfr/jfrEvents.hpp"
52 #include "jvm.h"
53 #include "logging/log.hpp"
54 #include "memory/oopFactory.hpp"
55 #include "memory/referenceType.hpp"
56 #include "memory/resourceArea.hpp"
57 #include "memory/universe.hpp"
58 #include "oops/access.inline.hpp"
59 #include "oops/constantPool.hpp"
60 #include "oops/fieldStreams.inline.hpp"
61 #include "oops/flatArrayKlass.hpp"
62 #include "oops/instanceKlass.hpp"
63 #include "oops/klass.inline.hpp"
64 #include "oops/method.hpp"
65 #include "oops/objArrayKlass.hpp"
66 #include "oops/objArrayOop.inline.hpp"
67 #include "oops/oop.inline.hpp"
68 #include "oops/recordComponent.hpp"
69 #include "oops/refArrayOop.inline.hpp"
70 #include "prims/foreignGlobals.hpp"
71 #include "prims/jvm_misc.hpp"
72 #include "prims/jvmtiExport.hpp"
73 #include "prims/jvmtiThreadState.inline.hpp"
74 #include "prims/stackwalk.hpp"
75 #include "runtime/arguments.hpp"
76 #include "runtime/atomicAccess.hpp"
77 #include "runtime/continuation.hpp"
78 #include "runtime/deoptimization.hpp"
79 #include "runtime/globals_extension.hpp"
80 #include "runtime/handles.inline.hpp"
81 #include "runtime/handshake.hpp"
82 #include "runtime/init.hpp"
83 #include "runtime/interfaceSupport.inline.hpp"
84 #include "runtime/java.hpp"
85 #include "runtime/javaCalls.hpp"
86 #include "runtime/javaThread.hpp"
87 #include "runtime/jfieldIDWorkaround.hpp"
88 #include "runtime/jniHandles.inline.hpp"
89 #include "runtime/os.inline.hpp"
398
399 return (jobjectArray) JNIHandles::make_local(THREAD, result_h());
400 JVM_END
401
402
403 /*
404 * Return the temporary directory that the VM uses for the attach
405 * and perf data files.
406 *
407 * It is important that this directory is well-known and the
408 * same for all VM instances. It cannot be affected by configuration
409 * variables such as java.io.tmpdir.
410 */
411 JVM_ENTRY(jstring, JVM_GetTemporaryDirectory(JNIEnv *env))
412 HandleMark hm(THREAD);
413 const char* temp_dir = os::get_temp_directory();
414 Handle h = java_lang_String::create_from_platform_dependent_str(temp_dir, CHECK_NULL);
415 return (jstring) JNIHandles::make_local(THREAD, h());
416 JVM_END
417
418 static void validate_array_arguments(Klass* elmClass, jint len, TRAPS) {
419 if (len < 0) {
420 THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), "Array length is negative");
421 }
422 elmClass->initialize(CHECK);
423 if (elmClass->is_array_klass() || elmClass->is_identity_class()) {
424 THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), "Element class is not a value class");
425 }
426 if (elmClass->is_abstract()) {
427 THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), "Element class is abstract");
428 }
429 }
430
431 JVM_ENTRY(jarray, JVM_CopyOfSpecialArray(JNIEnv *env, jarray orig, jint from, jint to))
432 oop o = JNIHandles::resolve_non_null(orig);
433 assert(o->is_array(), "Must be");
434 oop array = nullptr;
435 arrayOop org = (arrayOop)o;
436 arrayHandle oh(THREAD, org);
437 ObjArrayKlass* ak = ObjArrayKlass::cast(org->klass());
438 InlineKlass* vk = InlineKlass::cast(ak->element_klass());
439 int len = to - from; // length of the new array
440 if (ak->is_null_free_array_klass()) {
441 if ((len != 0) && (from >= org->length() || to > org->length())) {
442 THROW_MSG_NULL(vmSymbols::java_lang_IllegalArgumentException(), "Copying of null-free array with uninitialized elements");
443 }
444 }
445 if (org->is_flatArray()) {
446 FlatArrayKlass* fak = FlatArrayKlass::cast(org->klass());
447 LayoutKind lk = fak->layout_kind();
448 ArrayKlass::ArrayProperties props = ArrayKlass::ArrayProperties::DEFAULT;
449 switch(lk) {
450 case LayoutKind::ATOMIC_FLAT:
451 props = ArrayKlass::ArrayProperties::NULL_RESTRICTED;
452 break;
453 case LayoutKind::NON_ATOMIC_FLAT:
454 props = (ArrayKlass::ArrayProperties)(ArrayKlass::ArrayProperties::NULL_RESTRICTED | ArrayKlass::ArrayProperties::NON_ATOMIC);
455 break;
456 case LayoutKind::NULLABLE_ATOMIC_FLAT:
457 props = ArrayKlass::ArrayProperties::NON_ATOMIC;
458 break;
459 default:
460 ShouldNotReachHere();
461 }
462 array = oopFactory::new_flatArray(vk, len, props, lk, CHECK_NULL);
463 arrayHandle ah(THREAD, (arrayOop)array);
464 int end = to < oh()->length() ? to : oh()->length();
465 for (int i = from; i < end; i++) {
466 void* src = ((flatArrayOop)oh())->value_at_addr(i, fak->layout_helper());
467 void* dst = ((flatArrayOop)ah())->value_at_addr(i - from, fak->layout_helper());
468 vk->copy_payload_to_addr(src, dst, lk, false);
469 }
470 array = ah();
471 } else {
472 ArrayKlass::ArrayProperties props = org->is_null_free_array() ? ArrayKlass::ArrayProperties::NULL_RESTRICTED : ArrayKlass::ArrayProperties::DEFAULT;
473 array = oopFactory::new_objArray(vk, len, props, CHECK_NULL);
474 int end = to < oh()->length() ? to : oh()->length();
475 for (int i = from; i < end; i++) {
476 if (i < ((objArrayOop)oh())->length()) {
477 ((objArrayOop)array)->obj_at_put(i - from, ((objArrayOop)oh())->obj_at(i));
478 } else {
479 assert(!ak->is_null_free_array_klass(), "Must be a nullable array");
480 ((objArrayOop)array)->obj_at_put(i - from, nullptr);
481 }
482 }
483 }
484 return (jarray) JNIHandles::make_local(THREAD, array);
485 JVM_END
486
487 JVM_ENTRY(jarray, JVM_NewNullRestrictedNonAtomicArray(JNIEnv *env, jclass elmClass, jint len, jobject initVal))
488 oop mirror = JNIHandles::resolve_non_null(elmClass);
489 oop init = JNIHandles::resolve(initVal);
490 if (init == nullptr) {
491 THROW_MSG_NULL(vmSymbols::java_lang_IllegalArgumentException(), "Initial value cannot be null");
492 }
493 Handle init_h(THREAD, init);
494 Klass* klass = java_lang_Class::as_Klass(mirror);
495 if (klass != init_h()->klass()) {
496 THROW_MSG_NULL(vmSymbols::java_lang_IllegalArgumentException(), "Type mismatch between array and initial value");
497 }
498 validate_array_arguments(klass, len, CHECK_NULL);
499 InlineKlass* vk = InlineKlass::cast(klass);
500 ArrayKlass::ArrayProperties props = (ArrayKlass::ArrayProperties)(ArrayKlass::ArrayProperties::NON_ATOMIC | ArrayKlass::ArrayProperties::NULL_RESTRICTED);
501 objArrayOop array = oopFactory::new_objArray(klass, len, props, CHECK_NULL);
502 for (int i = 0; i < len; i++) {
503 array->obj_at_put(i, init_h() /*, CHECK_NULL*/ );
504 }
505 return (jarray) JNIHandles::make_local(THREAD, array);
506 JVM_END
507
508 JVM_ENTRY(jarray, JVM_NewNullRestrictedAtomicArray(JNIEnv *env, jclass elmClass, jint len, jobject initVal))
509 oop mirror = JNIHandles::resolve_non_null(elmClass);
510 oop init = JNIHandles::resolve(initVal);
511 if (init == nullptr) {
512 THROW_MSG_NULL(vmSymbols::java_lang_IllegalArgumentException(), "Initial value cannot be null");
513 }
514 Handle init_h(THREAD, init);
515 Klass* klass = java_lang_Class::as_Klass(mirror);
516 if (klass != init_h()->klass()) {
517 THROW_MSG_NULL(vmSymbols::java_lang_IllegalArgumentException(), "Type mismatch between array and initial value");
518 }
519 validate_array_arguments(klass, len, CHECK_NULL);
520 InlineKlass* vk = InlineKlass::cast(klass);
521 ArrayKlass::ArrayProperties props = (ArrayKlass::ArrayProperties)(ArrayKlass::ArrayProperties::NULL_RESTRICTED);
522 objArrayOop array = oopFactory::new_objArray(klass, len, props, CHECK_NULL);
523 for (int i = 0; i < len; i++) {
524 array->obj_at_put(i, init_h() /*, CHECK_NULL*/ );
525 }
526 return (jarray) JNIHandles::make_local(THREAD, array);
527 JVM_END
528
529 JVM_ENTRY(jarray, JVM_NewNullableAtomicArray(JNIEnv *env, jclass elmClass, jint len))
530 oop mirror = JNIHandles::resolve_non_null(elmClass);
531 Klass* klass = java_lang_Class::as_Klass(mirror);
532 klass->initialize(CHECK_NULL);
533 validate_array_arguments(klass, len, CHECK_NULL);
534 InlineKlass* vk = InlineKlass::cast(klass);
535 ArrayKlass::ArrayProperties props = (ArrayKlass::ArrayProperties)(ArrayKlass::ArrayProperties::DEFAULT);
536 objArrayOop array = oopFactory::new_objArray(klass, len, props, CHECK_NULL);
537 return (jarray) JNIHandles::make_local(THREAD, array);
538 JVM_END
539
540 JVM_ENTRY(jboolean, JVM_IsFlatArray(JNIEnv *env, jobject obj))
541 arrayOop oop = arrayOop(JNIHandles::resolve_non_null(obj));
542 return oop->is_flatArray();
543 JVM_END
544
545 JVM_ENTRY(jboolean, JVM_IsNullRestrictedArray(JNIEnv *env, jobject obj))
546 arrayOop oop = arrayOop(JNIHandles::resolve_non_null(obj));
547 return oop->is_null_free_array();
548 JVM_END
549
550 JVM_ENTRY(jboolean, JVM_IsAtomicArray(JNIEnv *env, jobject obj))
551 // There are multiple cases where an array can/must support atomic access:
552 // - the array is a reference array
553 // - the array uses an atomic flat layout: NULLABLE_ATOMIC_FLAT or ATOMIC_FLAT
554 // - the array is flat and its component type is naturally atomic
555 arrayOop oop = arrayOop(JNIHandles::resolve_non_null(obj));
556 if (oop->is_refArray()) return true;
557 if (oop->is_flatArray()) {
558 FlatArrayKlass* fak = FlatArrayKlass::cast(oop->klass());
559 if (fak->layout_kind() == LayoutKind::ATOMIC_FLAT || fak->layout_kind() == LayoutKind::NULLABLE_ATOMIC_FLAT) {
560 return true;
561 }
562 if (fak->element_klass()->is_naturally_atomic()) return true;
563 }
564 return false;
565 JVM_END
566
567 // java.lang.Runtime /////////////////////////////////////////////////////////////////////////
568
569 extern volatile jint vm_created;
570
571 JVM_ENTRY_NO_ENV(void, JVM_BeforeHalt())
572 EventShutdown event;
573 if (event.should_commit()) {
574 event.set_reason("Shutdown requested from Java");
575 event.commit();
576 }
577 JVM_END
578
579
580 JVM_ENTRY_NO_ENV(void, JVM_Halt(jint code))
581 before_exit(thread, true);
582 vm_exit(code);
583 JVM_END
584
585
754
755 Handle stackStream_h(THREAD, JNIHandles::resolve_non_null(stackStream));
756 return StackWalk::fetchNextBatch(stackStream_h, mode, anchor, last_batch_count, buffer_size,
757 start_index, frames_array_h, THREAD);
758 JVM_END
759
760 JVM_ENTRY(void, JVM_SetStackWalkContinuation(JNIEnv *env, jobject stackStream, jlong anchor, jobjectArray frames, jobject cont))
761 objArrayOop fa = objArrayOop(JNIHandles::resolve_non_null(frames));
762 objArrayHandle frames_array_h(THREAD, fa);
763 Handle stackStream_h(THREAD, JNIHandles::resolve_non_null(stackStream));
764 Handle cont_h(THREAD, JNIHandles::resolve_non_null(cont));
765
766 StackWalk::setContinuation(stackStream_h, anchor, frames_array_h, cont_h, THREAD);
767 JVM_END
768
769 // java.lang.Object ///////////////////////////////////////////////
770
771
772 JVM_ENTRY(jint, JVM_IHashCode(JNIEnv* env, jobject handle))
773 // as implemented in the classic virtual machine; return 0 if object is null
774 if (handle == nullptr) {
775 return 0;
776 }
777 oop obj = JNIHandles::resolve_non_null(handle);
778 if (EnableValhalla && obj->klass()->is_inline_klass()) {
779 JavaValue result(T_INT);
780 JavaCallArguments args;
781 Handle ho(THREAD, obj);
782 args.push_oop(ho);
783 methodHandle method(THREAD, Universe::value_object_hash_code_method());
784 JavaCalls::call(&result, method, &args, THREAD);
785 if (HAS_PENDING_EXCEPTION) {
786 if (!PENDING_EXCEPTION->is_a(vmClasses::Error_klass())) {
787 Handle e(THREAD, PENDING_EXCEPTION);
788 CLEAR_PENDING_EXCEPTION;
789 THROW_MSG_CAUSE_(vmSymbols::java_lang_InternalError(), "Internal error in hashCode", e, false);
790 }
791 }
792 return result.get_jint();
793 } else {
794 return checked_cast<jint>(ObjectSynchronizer::FastHashCode(THREAD, obj));
795 }
796 JVM_END
797
798
799 JVM_ENTRY(void, JVM_MonitorWait(JNIEnv* env, jobject handle, jlong ms))
800 Handle obj(THREAD, JNIHandles::resolve_non_null(handle));
801 ObjectSynchronizer::wait(obj, ms, CHECK);
802 JVM_END
803
804
805 JVM_ENTRY(void, JVM_MonitorNotify(JNIEnv* env, jobject handle))
806 Handle obj(THREAD, JNIHandles::resolve_non_null(handle));
807 ObjectSynchronizer::notify(obj, CHECK);
808 JVM_END
809
810
811 JVM_ENTRY(void, JVM_MonitorNotifyAll(JNIEnv* env, jobject handle))
812 Handle obj(THREAD, JNIHandles::resolve_non_null(handle));
813 ObjectSynchronizer::notifyall(obj, CHECK);
814 JVM_END
815
823 // Just checking that the cloneable flag is set correct
824 if (obj->is_array()) {
825 guarantee(klass->is_cloneable(), "all arrays are cloneable");
826 } else {
827 guarantee(obj->is_instance(), "should be instanceOop");
828 bool cloneable = klass->is_subtype_of(vmClasses::Cloneable_klass());
829 guarantee(cloneable == klass->is_cloneable(), "incorrect cloneable flag");
830 }
831 #endif
832
833 // Check if class of obj supports the Cloneable interface.
834 // All arrays are considered to be cloneable (See JLS 20.1.5).
835 // All j.l.r.Reference classes are considered non-cloneable.
836 if (!klass->is_cloneable() ||
837 (klass->is_instance_klass() &&
838 InstanceKlass::cast(klass)->reference_type() != REF_NONE)) {
839 ResourceMark rm(THREAD);
840 THROW_MSG_NULL(vmSymbols::java_lang_CloneNotSupportedException(), klass->external_name());
841 }
842
843 if (klass->is_inline_klass()) {
844 // Value instances have no identity, so return the current instance instead of allocating a new one
845 // Value classes cannot have finalizers, so the method can return immediately
846 return JNIHandles::make_local(THREAD, obj());
847 }
848
849 // Make shallow object copy
850 const size_t size = obj->size();
851 oop new_obj_oop = nullptr;
852 if (obj->is_array()) {
853 const int length = ((arrayOop)obj())->length();
854 new_obj_oop = Universe::heap()->array_allocate(klass, size, length,
855 /* do_zero */ true, CHECK_NULL);
856 } else {
857 new_obj_oop = Universe::heap()->obj_allocate(klass, size, CHECK_NULL);
858 }
859
860 HeapAccess<>::clone(obj(), new_obj_oop, size);
861
862 Handle new_obj(THREAD, new_obj_oop);
863 // Caution: this involves a java upcall, so the clone should be
864 // "gc-robust" by this stage.
865 if (klass->has_finalizer()) {
866 assert(obj->is_instance(), "should be instanceOop");
867 new_obj_oop = InstanceKlass::register_finalizer(instanceOop(new_obj()), CHECK_NULL);
868 new_obj = Handle(THREAD, new_obj_oop);
1325 oop result = java_lang_Class::name(java_class, CHECK_NULL);
1326 return (jstring) JNIHandles::make_local(THREAD, result);
1327 JVM_END
1328
1329
1330 JVM_ENTRY(jobjectArray, JVM_GetClassInterfaces(JNIEnv *env, jclass cls))
1331 JvmtiVMObjectAllocEventCollector oam;
1332 oop mirror = JNIHandles::resolve_non_null(cls);
1333
1334 // Special handling for primitive objects
1335 if (java_lang_Class::is_primitive(mirror)) {
1336 // Primitive objects does not have any interfaces
1337 objArrayOop r = oopFactory::new_objArray(vmClasses::Class_klass(), 0, CHECK_NULL);
1338 return (jobjectArray) JNIHandles::make_local(THREAD, r);
1339 }
1340
1341 Klass* klass = java_lang_Class::as_Klass(mirror);
1342 // Figure size of result array
1343 int size;
1344 if (klass->is_instance_klass()) {
1345 InstanceKlass* ik = InstanceKlass::cast(klass);
1346 size = ik->local_interfaces()->length();
1347 } else {
1348 assert(klass->is_objArray_klass() || klass->is_typeArray_klass(), "Illegal mirror klass");
1349 size = 2;
1350 }
1351
1352 // Allocate result array
1353 objArrayOop r = oopFactory::new_objArray(vmClasses::Class_klass(), size, CHECK_NULL);
1354 objArrayHandle result (THREAD, r);
1355 // Fill in result
1356 if (klass->is_instance_klass()) {
1357 // Regular instance klass, fill in all local interfaces
1358 for (int index = 0; index < size; index++) {
1359 InstanceKlass* k = InstanceKlass::cast(klass)->local_interfaces()->at(index);
1360 result->obj_at_put(index, k->java_mirror());
1361 }
1362 } else {
1363 // All arrays implement java.lang.Cloneable and java.io.Serializable
1364 result->obj_at_put(0, vmClasses::Cloneable_klass()->java_mirror());
1365 result->obj_at_put(1, vmClasses::Serializable_klass()->java_mirror());
1366 }
1367 return (jobjectArray) JNIHandles::make_local(THREAD, result());
1368 JVM_END
1369
1370
1371 JVM_ENTRY(jboolean, JVM_IsHiddenClass(JNIEnv *env, jclass cls))
1372 oop mirror = JNIHandles::resolve_non_null(cls);
1373 if (java_lang_Class::is_primitive(mirror)) {
1374 return JNI_FALSE;
1375 }
1376 Klass* k = java_lang_Class::as_Klass(mirror);
1377 return k->is_hidden();
1378 JVM_END
1379
1380 class ScopedValueBindingsResolver {
1381 public:
1382 InstanceKlass* Carrier_klass;
1383 ScopedValueBindingsResolver(JavaThread* THREAD) {
1384 Klass *k = SystemDictionary::resolve_or_fail(vmSymbols::java_lang_ScopedValue_Carrier(), true, THREAD);
1385 Carrier_klass = InstanceKlass::cast(k);
1386 }
1387 };
1388
1389 JVM_ENTRY(jobject, JVM_FindScopedValueBindings(JNIEnv *env, jclass cls))
1390 ResourceMark rm(THREAD);
1391 GrowableArray<Handle>* local_array = new GrowableArray<Handle>(12);
1392 JvmtiVMObjectAllocEventCollector oam;
1393
1394 static ScopedValueBindingsResolver resolver(THREAD);
1395
1396 // Iterate through Java frames
1397 vframeStream vfst(thread);
1398 for(; !vfst.at_end(); vfst.next()) {
1399 int loc = -1;
1840 }
1841
1842 InstanceKlass* k = java_lang_Class::as_InstanceKlass(ofMirror);
1843
1844 // Ensure class is linked
1845 k->link_class(CHECK_NULL);
1846
1847 Array<Method*>* methods = k->methods();
1848 int methods_length = methods->length();
1849
1850 // Save original method_idnum in case of redefinition, which can change
1851 // the idnum of obsolete methods. The new method will have the same idnum
1852 // but if we refresh the methods array, the counts will be wrong.
1853 ResourceMark rm(THREAD);
1854 GrowableArray<int>* idnums = new GrowableArray<int>(methods_length);
1855 int num_methods = 0;
1856
1857 // Select methods matching the criteria.
1858 for (int i = 0; i < methods_length; i++) {
1859 Method* method = methods->at(i);
1860 if (want_constructor && !method->is_object_constructor()) {
1861 continue;
1862 }
1863 if (!want_constructor &&
1864 (method->is_object_constructor() || method->is_class_initializer() ||
1865 method->is_overpass())) {
1866 continue;
1867 }
1868 if (publicOnly && !method->is_public()) {
1869 continue;
1870 }
1871 idnums->push(method->method_idnum());
1872 ++num_methods;
1873 }
1874
1875 // Allocate result
1876 objArrayOop r = oopFactory::new_objArray(klass, num_methods, CHECK_NULL);
1877 objArrayHandle result (THREAD, r);
1878
1879 // Now just put the methods that we selected above, but go by their idnum
1880 // in case of redefinition. The methods can be redefined at any safepoint,
1881 // so above when allocating the oop array and below when creating reflect
1882 // objects.
1883 for (int i = 0; i < num_methods; i++) {
1884 methodHandle method(THREAD, k->method_with_idnum(idnums->at(i)));
1885 if (method.is_null()) {
1886 // Method may have been deleted and seems this API can handle null
1887 // Otherwise should probably put a method that throws NSME
1888 result->obj_at_put(i, nullptr);
1889 } else {
1890 oop m;
1891 if (want_constructor) {
1892 assert(method->is_object_constructor(), "must be");
1893 m = Reflection::new_constructor(method, CHECK_NULL);
1894 } else {
1895 m = Reflection::new_method(method, false, CHECK_NULL);
1896 }
1897 result->obj_at_put(i, m);
1898 }
1899 }
1900
1901 return (jobjectArray) JNIHandles::make_local(THREAD, result());
1902 }
1903
1904 JVM_ENTRY(jobjectArray, JVM_GetClassDeclaredMethods(JNIEnv *env, jclass ofClass, jboolean publicOnly))
1905 {
1906 return get_class_declared_methods_helper(env, ofClass, publicOnly,
1907 /*want_constructor*/ false,
1908 vmClasses::reflect_Method_klass(), THREAD);
1909 }
1910 JVM_END
1911
1912 JVM_ENTRY(jobjectArray, JVM_GetClassDeclaredConstructors(JNIEnv *env, jclass ofClass, jboolean publicOnly))
2133 constantTag tag = cp->tag_at(index);
2134 if (!tag.is_method() && !tag.is_interface_method()) {
2135 THROW_MSG_NULL(vmSymbols::java_lang_IllegalArgumentException(), "Wrong type at constant pool index");
2136 }
2137 int klass_ref = cp->uncached_klass_ref_index_at(index);
2138 Klass* k_o;
2139 if (force_resolution) {
2140 k_o = cp->klass_at(klass_ref, CHECK_NULL);
2141 } else {
2142 k_o = ConstantPool::klass_at_if_loaded(cp, klass_ref);
2143 if (k_o == nullptr) return nullptr;
2144 }
2145 InstanceKlass* k = InstanceKlass::cast(k_o);
2146 Symbol* name = cp->uncached_name_ref_at(index);
2147 Symbol* sig = cp->uncached_signature_ref_at(index);
2148 methodHandle m (THREAD, k->find_method(name, sig));
2149 if (m.is_null()) {
2150 THROW_MSG_NULL(vmSymbols::java_lang_RuntimeException(), "Unable to look up method in target class");
2151 }
2152 oop method;
2153 if (m->is_object_constructor()) {
2154 method = Reflection::new_constructor(m, CHECK_NULL);
2155 } else {
2156 method = Reflection::new_method(m, true, CHECK_NULL);
2157 }
2158 return JNIHandles::make_local(THREAD, method);
2159 }
2160
2161 JVM_ENTRY(jobject, JVM_ConstantPoolGetMethodAt(JNIEnv *env, jobject obj, jint index))
2162 {
2163 JvmtiVMObjectAllocEventCollector oam;
2164 constantPoolHandle cp = constantPoolHandle(THREAD, reflect_ConstantPool::get_cp(JNIHandles::resolve_non_null(obj)));
2165 bounds_check(cp, index, CHECK_NULL);
2166 jobject res = get_method_at_helper(cp, index, true, CHECK_NULL);
2167 return res;
2168 }
2169 JVM_END
2170
2171 JVM_ENTRY(jobject, JVM_ConstantPoolGetMethodAtIfLoaded(JNIEnv *env, jobject obj, jint index))
2172 {
2173 JvmtiVMObjectAllocEventCollector oam;
2174 constantPoolHandle cp = constantPoolHandle(THREAD, reflect_ConstantPool::get_cp(JNIHandles::resolve_non_null(obj)));
2175 bounds_check(cp, index, CHECK_NULL);
2582
2583
2584 JVM_ENTRY(jint, JVM_GetMethodIxArgsSize(JNIEnv *env, jclass cls, int method_index))
2585 InstanceKlass* ik = get_instance_klass_considering_redefinition(cls, thread);
2586 Method* method = ik->methods()->at(method_index);
2587 return method->size_of_parameters();
2588 JVM_END
2589
2590
2591 JVM_ENTRY(jint, JVM_GetMethodIxMaxStack(JNIEnv *env, jclass cls, int method_index))
2592 InstanceKlass* ik = get_instance_klass_considering_redefinition(cls, thread);
2593 Method* method = ik->methods()->at(method_index);
2594 return method->verifier_max_stack();
2595 JVM_END
2596
2597
2598 JVM_ENTRY(jboolean, JVM_IsConstructorIx(JNIEnv *env, jclass cls, int method_index))
2599 ResourceMark rm(THREAD);
2600 InstanceKlass* ik = get_instance_klass_considering_redefinition(cls, thread);
2601 Method* method = ik->methods()->at(method_index);
2602 return method->is_object_constructor();
2603 JVM_END
2604
2605
2606 JVM_ENTRY(jboolean, JVM_IsVMGeneratedMethodIx(JNIEnv *env, jclass cls, int method_index))
2607 ResourceMark rm(THREAD);
2608 InstanceKlass* ik = get_instance_klass_considering_redefinition(cls, thread);
2609 Method* method = ik->methods()->at(method_index);
2610 return method->is_overpass();
2611 JVM_END
2612
2613 JVM_ENTRY(const char*, JVM_GetMethodIxNameUTF(JNIEnv *env, jclass cls, jint method_index))
2614 InstanceKlass* ik = get_instance_klass_considering_redefinition(cls, thread);
2615 Method* method = ik->methods()->at(method_index);
2616 return method->name()->as_utf8();
2617 JVM_END
2618
2619
2620 JVM_ENTRY(const char*, JVM_GetMethodIxSignatureUTF(JNIEnv *env, jclass cls, jint method_index))
2621 InstanceKlass* ik = get_instance_klass_considering_redefinition(cls, thread);
2622 Method* method = ik->methods()->at(method_index);
3376 JVM_LEAF(void*, JVM_FindLibraryEntry(void* handle, const char* name))
3377 void* find_result = os::dll_lookup(handle, name);
3378 log_info(library)("%s %s in library with handle " INTPTR_FORMAT,
3379 find_result != nullptr ? "Found" : "Failed to find",
3380 name, p2i(handle));
3381 return find_result;
3382 JVM_END
3383
3384
3385 // JNI version ///////////////////////////////////////////////////////////////////////////////
3386
3387 JVM_LEAF(jboolean, JVM_IsSupportedJNIVersion(jint version))
3388 return Threads::is_supported_jni_version_including_1_1(version);
3389 JVM_END
3390
3391
3392 JVM_LEAF(jboolean, JVM_IsPreviewEnabled(void))
3393 return Arguments::enable_preview() ? JNI_TRUE : JNI_FALSE;
3394 JVM_END
3395
3396 JVM_LEAF(jboolean, JVM_IsValhallaEnabled(void))
3397 return EnableValhalla ? JNI_TRUE : JNI_FALSE;
3398 JVM_END
3399
3400 JVM_LEAF(jboolean, JVM_IsContinuationsSupported(void))
3401 return VMContinuations ? JNI_TRUE : JNI_FALSE;
3402 JVM_END
3403
3404 JVM_LEAF(jboolean, JVM_IsForeignLinkerSupported(void))
3405 return ForeignGlobals::is_foreign_linker_supported() ? JNI_TRUE : JNI_FALSE;
3406 JVM_END
3407
3408 JVM_LEAF(jboolean, JVM_IsStaticallyLinked(void))
3409 return is_vm_statically_linked() ? JNI_TRUE : JNI_FALSE;
3410 JVM_END
3411
3412 // String support ///////////////////////////////////////////////////////////////////////////
3413
3414 JVM_ENTRY(jstring, JVM_InternString(JNIEnv *env, jstring str))
3415 JvmtiVMObjectAllocEventCollector oam;
3416 if (str == nullptr) return nullptr;
3417 oop string = JNIHandles::resolve_non_null(str);
3418 oop result = StringTable::intern(string, CHECK_NULL);
3419 return (jstring) JNIHandles::make_local(THREAD, result);
3459
3460 jclass find_class_from_class_loader(JNIEnv* env, Symbol* name, jboolean init,
3461 Handle loader, jboolean throwError, TRAPS) {
3462 Klass* klass = SystemDictionary::resolve_or_fail(name, loader, throwError != 0, CHECK_NULL);
3463
3464 // Check if we should initialize the class
3465 if (init && klass->is_instance_klass()) {
3466 klass->initialize(CHECK_NULL);
3467 }
3468 return (jclass) JNIHandles::make_local(THREAD, klass->java_mirror());
3469 }
3470
3471
3472 // Method ///////////////////////////////////////////////////////////////////////////////////////////
3473
3474 JVM_ENTRY(jobject, JVM_InvokeMethod(JNIEnv *env, jobject method, jobject obj, jobjectArray args0))
3475 Handle method_handle;
3476 if (thread->stack_overflow_state()->stack_available((address) &method_handle) >= JVMInvokeMethodSlack) {
3477 method_handle = Handle(THREAD, JNIHandles::resolve(method));
3478 Handle receiver(THREAD, JNIHandles::resolve(obj));
3479 objArrayHandle args(THREAD, (objArrayOop)JNIHandles::resolve(args0));
3480 assert(args() == nullptr || !args->is_flatArray(), "args are never flat or are they???");
3481
3482 oop result = Reflection::invoke_method(method_handle(), receiver, args, CHECK_NULL);
3483 jobject res = JNIHandles::make_local(THREAD, result);
3484 if (JvmtiExport::should_post_vm_object_alloc()) {
3485 oop ret_type = java_lang_reflect_Method::return_type(method_handle());
3486 assert(ret_type != nullptr, "sanity check: ret_type oop must not be null!");
3487 if (java_lang_Class::is_primitive(ret_type)) {
3488 // Only for primitive type vm allocates memory for java object.
3489 // See box() method.
3490 JvmtiExport::post_vm_object_alloc(thread, result);
3491 }
3492 }
3493 return res;
3494 } else {
3495 THROW_NULL(vmSymbols::java_lang_StackOverflowError());
3496 }
3497 JVM_END
3498
3499
3500 JVM_ENTRY(jobject, JVM_NewInstanceFromConstructor(JNIEnv *env, jobject c, jobjectArray args0))
3501 objArrayHandle args(THREAD, (objArrayOop)JNIHandles::resolve(args0));
3502 assert(args() == nullptr || !args->is_flatArray(), "args are never flat or are they???");
3503 oop constructor_mirror = JNIHandles::resolve(c);
3504 oop result = Reflection::invoke_constructor(constructor_mirror, args, CHECK_NULL);
3505 jobject res = JNIHandles::make_local(THREAD, result);
3506 if (JvmtiExport::should_post_vm_object_alloc()) {
3507 JvmtiExport::post_vm_object_alloc(thread, result);
3508 }
3509 return res;
3510 JVM_END
3511
3512 JVM_ENTRY(void, JVM_InitializeFromArchive(JNIEnv* env, jclass cls))
3513 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve(cls));
3514 HeapShared::initialize_from_archived_subgraph(THREAD, k);
3515 JVM_END
3516
3517 JVM_ENTRY(void, JVM_RegisterLambdaProxyClassForArchiving(JNIEnv* env,
3518 jclass caller,
3519 jstring interfaceMethodName,
3520 jobject factoryType,
3521 jobject interfaceMethodType,
3522 jobject implementationMember,
3523 jobject dynamicMethodType,
3720 JvmtiVMObjectAllocEventCollector oam;
3721
3722 // Check if threads is null
3723 if (threads == nullptr) {
3724 THROW_NULL(vmSymbols::java_lang_NullPointerException());
3725 }
3726
3727 objArrayOop a = objArrayOop(JNIHandles::resolve_non_null(threads));
3728 objArrayHandle ah(THREAD, a);
3729 int num_threads = ah->length();
3730 // check if threads is non-empty array
3731 if (num_threads == 0) {
3732 THROW_NULL(vmSymbols::java_lang_IllegalArgumentException());
3733 }
3734
3735 // check if threads is not an array of objects of Thread class
3736 Klass* k = ObjArrayKlass::cast(ah->klass())->element_klass();
3737 if (k != vmClasses::Thread_klass()) {
3738 THROW_NULL(vmSymbols::java_lang_IllegalArgumentException());
3739 }
3740 refArrayHandle rah(THREAD, (refArrayOop)ah()); // j.l.Thread is an identity class, arrays are always reference arrays
3741
3742 ResourceMark rm(THREAD);
3743
3744 GrowableArray<instanceHandle>* thread_handle_array = new GrowableArray<instanceHandle>(num_threads);
3745 for (int i = 0; i < num_threads; i++) {
3746 oop thread_obj = rah->obj_at(i);
3747 instanceHandle h(THREAD, (instanceOop) thread_obj);
3748 thread_handle_array->append(h);
3749 }
3750
3751 // The JavaThread references in thread_handle_array are validated
3752 // in VM_ThreadDump::doit().
3753 Handle stacktraces = ThreadService::dump_stack_traces(thread_handle_array, num_threads, CHECK_NULL);
3754 return (jobjectArray)JNIHandles::make_local(THREAD, stacktraces());
3755
3756 JVM_END
3757
3758 // JVM monitoring and management support
3759 JVM_LEAF(void*, JVM_GetManagement(jint version))
3760 return Management::get_jmm_interface(version);
3761 JVM_END
3762
3763 // com.sun.tools.attach.VirtualMachine agent properties support
3764 //
3765 // Initialize the agent properties with the properties maintained in the VM
3766 JVM_ENTRY(jobject, JVM_InitAgentProperties(JNIEnv *env, jobject properties))
|