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/recordComponent.hpp"
65 #include "oops/objArrayKlass.hpp"
66 #include "oops/objArrayOop.inline.hpp"
67 #include "oops/oop.inline.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/atomic.hpp"
75 #include "runtime/continuation.hpp"
76 #include "runtime/globals_extension.hpp"
77 #include "runtime/handles.inline.hpp"
78 #include "runtime/init.hpp"
79 #include "runtime/interfaceSupport.inline.hpp"
80 #include "runtime/deoptimization.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);
1147 oop result = java_lang_Class::name(java_class, CHECK_NULL);
1148 return (jstring) JNIHandles::make_local(THREAD, result);
1149 JVM_END
1150
1151
1152 JVM_ENTRY(jobjectArray, JVM_GetClassInterfaces(JNIEnv *env, jclass cls))
1153 JvmtiVMObjectAllocEventCollector oam;
1154 oop mirror = JNIHandles::resolve_non_null(cls);
1155
1156 // Special handling for primitive objects
1157 if (java_lang_Class::is_primitive(mirror)) {
1158 // Primitive objects does not have any interfaces
1159 objArrayOop r = oopFactory::new_objArray(vmClasses::Class_klass(), 0, CHECK_NULL);
1160 return (jobjectArray) JNIHandles::make_local(THREAD, r);
1161 }
1162
1163 Klass* klass = java_lang_Class::as_Klass(mirror);
1164 // Figure size of result array
1165 int size;
1166 if (klass->is_instance_klass()) {
1167 size = InstanceKlass::cast(klass)->local_interfaces()->length();
1168 } else {
1169 assert(klass->is_objArray_klass() || klass->is_typeArray_klass(), "Illegal mirror klass");
1170 size = 2;
1171 }
1172
1173 // Allocate result array
1174 objArrayOop r = oopFactory::new_objArray(vmClasses::Class_klass(), size, CHECK_NULL);
1175 objArrayHandle result (THREAD, r);
1176 // Fill in result
1177 if (klass->is_instance_klass()) {
1178 // Regular instance klass, fill in all local interfaces
1179 for (int index = 0; index < size; index++) {
1180 Klass* k = InstanceKlass::cast(klass)->local_interfaces()->at(index);
1181 result->obj_at_put(index, k->java_mirror());
1182 }
1183 } else {
1184 // All arrays implement java.lang.Cloneable and java.io.Serializable
1185 result->obj_at_put(0, vmClasses::Cloneable_klass()->java_mirror());
1186 result->obj_at_put(1, vmClasses::Serializable_klass()->java_mirror());
1187 }
1188 return (jobjectArray) JNIHandles::make_local(THREAD, result());
1189 JVM_END
1190
1191
1192 JVM_ENTRY(jboolean, JVM_IsHiddenClass(JNIEnv *env, jclass cls))
1193 oop mirror = JNIHandles::resolve_non_null(cls);
1194 if (java_lang_Class::is_primitive(mirror)) {
1195 return JNI_FALSE;
1196 }
1197 Klass* k = java_lang_Class::as_Klass(mirror);
1198 return k->is_hidden();
1199 JVM_END
1200
1201
1202 class ScopedValueBindingsResolver {
1203 public:
1204 InstanceKlass* Carrier_klass;
1205 ScopedValueBindingsResolver(JavaThread* THREAD) {
1206 Klass *k = SystemDictionary::resolve_or_fail(vmSymbols::java_lang_ScopedValue_Carrier(), true, THREAD);
1207 Carrier_klass = InstanceKlass::cast(k);
1208 }
1209 };
1210
1211 JVM_ENTRY(jobject, JVM_FindScopedValueBindings(JNIEnv *env, jclass cls))
1212 ResourceMark rm(THREAD);
1213 GrowableArray<Handle>* local_array = new GrowableArray<Handle>(12);
1214 JvmtiVMObjectAllocEventCollector oam;
1215
1216 static ScopedValueBindingsResolver resolver(THREAD);
1217
1218 // Iterate through Java frames
1219 vframeStream vfst(thread);
1220 for(; !vfst.at_end(); vfst.next()) {
1665 }
1666
1667 InstanceKlass* k = InstanceKlass::cast(java_lang_Class::as_Klass(ofMirror));
1668
1669 // Ensure class is linked
1670 k->link_class(CHECK_NULL);
1671
1672 Array<Method*>* methods = k->methods();
1673 int methods_length = methods->length();
1674
1675 // Save original method_idnum in case of redefinition, which can change
1676 // the idnum of obsolete methods. The new method will have the same idnum
1677 // but if we refresh the methods array, the counts will be wrong.
1678 ResourceMark rm(THREAD);
1679 GrowableArray<int>* idnums = new GrowableArray<int>(methods_length);
1680 int num_methods = 0;
1681
1682 // Select methods matching the criteria.
1683 for (int i = 0; i < methods_length; i++) {
1684 Method* method = methods->at(i);
1685 if (want_constructor && !method->is_object_initializer()) {
1686 continue;
1687 }
1688 if (!want_constructor &&
1689 (method->is_object_initializer() || method->is_static_initializer() ||
1690 method->is_overpass())) {
1691 continue;
1692 }
1693 if (publicOnly && !method->is_public()) {
1694 continue;
1695 }
1696 idnums->push(method->method_idnum());
1697 ++num_methods;
1698 }
1699
1700 // Allocate result
1701 objArrayOop r = oopFactory::new_objArray(klass, num_methods, CHECK_NULL);
1702 objArrayHandle result (THREAD, r);
1703
1704 // Now just put the methods that we selected above, but go by their idnum
1705 // in case of redefinition. The methods can be redefined at any safepoint,
1706 // so above when allocating the oop array and below when creating reflect
1707 // objects.
1708 for (int i = 0; i < num_methods; i++) {
1709 methodHandle method(THREAD, k->method_with_idnum(idnums->at(i)));
1710 if (method.is_null()) {
1711 // Method may have been deleted and seems this API can handle null
1712 // Otherwise should probably put a method that throws NSME
1713 result->obj_at_put(i, nullptr);
1714 } else {
1715 oop m;
1716 if (want_constructor) {
1717 m = Reflection::new_constructor(method, CHECK_NULL);
1718 } else {
1719 m = Reflection::new_method(method, false, CHECK_NULL);
1720 }
1721 result->obj_at_put(i, m);
1722 }
1723 }
1724
1725 return (jobjectArray) JNIHandles::make_local(THREAD, result());
1726 }
1727
1728 JVM_ENTRY(jobjectArray, JVM_GetClassDeclaredMethods(JNIEnv *env, jclass ofClass, jboolean publicOnly))
1729 {
1730 return get_class_declared_methods_helper(env, ofClass, publicOnly,
1731 /*want_constructor*/ false,
1732 vmClasses::reflect_Method_klass(), THREAD);
1733 }
1734 JVM_END
1735
1736 JVM_ENTRY(jobjectArray, JVM_GetClassDeclaredConstructors(JNIEnv *env, jclass ofClass, jboolean publicOnly))
1979 constantTag tag = cp->tag_at(index);
1980 if (!tag.is_method() && !tag.is_interface_method()) {
1981 THROW_MSG_NULL(vmSymbols::java_lang_IllegalArgumentException(), "Wrong type at constant pool index");
1982 }
1983 int klass_ref = cp->uncached_klass_ref_index_at(index);
1984 Klass* k_o;
1985 if (force_resolution) {
1986 k_o = cp->klass_at(klass_ref, CHECK_NULL);
1987 } else {
1988 k_o = ConstantPool::klass_at_if_loaded(cp, klass_ref);
1989 if (k_o == nullptr) return nullptr;
1990 }
1991 InstanceKlass* k = InstanceKlass::cast(k_o);
1992 Symbol* name = cp->uncached_name_ref_at(index);
1993 Symbol* sig = cp->uncached_signature_ref_at(index);
1994 methodHandle m (THREAD, k->find_method(name, sig));
1995 if (m.is_null()) {
1996 THROW_MSG_NULL(vmSymbols::java_lang_RuntimeException(), "Unable to look up method in target class");
1997 }
1998 oop method;
1999 if (m->is_object_initializer()) {
2000 method = Reflection::new_constructor(m, CHECK_NULL);
2001 } else {
2002 // new_method accepts <clinit> as Method here
2003 method = Reflection::new_method(m, true, CHECK_NULL);
2004 }
2005 return JNIHandles::make_local(THREAD, method);
2006 }
2007
2008 JVM_ENTRY(jobject, JVM_ConstantPoolGetMethodAt(JNIEnv *env, jobject obj, jobject unused, jint index))
2009 {
2010 JvmtiVMObjectAllocEventCollector oam;
2011 constantPoolHandle cp = constantPoolHandle(THREAD, reflect_ConstantPool::get_cp(JNIHandles::resolve_non_null(obj)));
2012 bounds_check(cp, index, CHECK_NULL);
2013 jobject res = get_method_at_helper(cp, index, true, CHECK_NULL);
2014 return res;
2015 }
2016 JVM_END
2017
2018 JVM_ENTRY(jobject, JVM_ConstantPoolGetMethodAtIfLoaded(JNIEnv *env, jobject obj, jobject unused, jint index))
2019 {
2020 JvmtiVMObjectAllocEventCollector oam;
2021 constantPoolHandle cp = constantPoolHandle(THREAD, reflect_ConstantPool::get_cp(JNIHandles::resolve_non_null(obj)));
2022 bounds_check(cp, index, CHECK_NULL);
2254 Klass* k = java_lang_Class::as_Klass(r);
2255 assert(k->is_instance_klass(), "must be an instance klass");
2256 if (!k->is_instance_klass()) return false;
2257
2258 ResourceMark rm(THREAD);
2259 const char* name = k->name()->as_C_string();
2260 bool system_class = k->class_loader() == nullptr;
2261 return JavaAssertions::enabled(name, system_class);
2262
2263 JVM_END
2264
2265
2266 // Return a new AssertionStatusDirectives object with the fields filled in with
2267 // command-line assertion arguments (i.e., -ea, -da).
2268 JVM_ENTRY(jobject, JVM_AssertionStatusDirectives(JNIEnv *env, jclass unused))
2269 JvmtiVMObjectAllocEventCollector oam;
2270 oop asd = JavaAssertions::createAssertionStatusDirectives(CHECK_NULL);
2271 return JNIHandles::make_local(THREAD, asd);
2272 JVM_END
2273
2274 // Verification ////////////////////////////////////////////////////////////////////////////////
2275
2276 // Reflection for the verifier /////////////////////////////////////////////////////////////////
2277
2278 // RedefineClasses support: bug 6214132 caused verification to fail.
2279 // All functions from this section should call the jvmtiThreadSate function:
2280 // Klass* class_to_verify_considering_redefinition(Klass* klass).
2281 // The function returns a Klass* of the _scratch_class if the verifier
2282 // was invoked in the middle of the class redefinition.
2283 // Otherwise it returns its argument value which is the _the_class Klass*.
2284 // Please, refer to the description in the jvmtiThreadState.hpp.
2285
2286 JVM_ENTRY(jboolean, JVM_IsInterface(JNIEnv *env, jclass cls))
2287 oop mirror = JNIHandles::resolve_non_null(cls);
2288 if (java_lang_Class::is_primitive(mirror)) {
2289 return JNI_FALSE;
2290 }
2291 Klass* k = java_lang_Class::as_Klass(mirror);
2292 // This isn't necessary since answer is the same since redefinition
2293 // has already checked this matches for the scratch class.
2432 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls));
2433 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
2434 Method* method = InstanceKlass::cast(k)->methods()->at(method_index);
2435 return method->size_of_parameters();
2436 JVM_END
2437
2438
2439 JVM_ENTRY(jint, JVM_GetMethodIxMaxStack(JNIEnv *env, jclass cls, int method_index))
2440 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls));
2441 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
2442 Method* method = InstanceKlass::cast(k)->methods()->at(method_index);
2443 return method->verifier_max_stack();
2444 JVM_END
2445
2446
2447 JVM_ENTRY(jboolean, JVM_IsConstructorIx(JNIEnv *env, jclass cls, int method_index))
2448 ResourceMark rm(THREAD);
2449 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls));
2450 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
2451 Method* method = InstanceKlass::cast(k)->methods()->at(method_index);
2452 return method->name() == vmSymbols::object_initializer_name();
2453 JVM_END
2454
2455
2456 JVM_ENTRY(jboolean, JVM_IsVMGeneratedMethodIx(JNIEnv *env, jclass cls, int method_index))
2457 ResourceMark rm(THREAD);
2458 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls));
2459 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
2460 Method* method = InstanceKlass::cast(k)->methods()->at(method_index);
2461 return method->is_overpass();
2462 JVM_END
2463
2464 JVM_ENTRY(const char*, JVM_GetMethodIxNameUTF(JNIEnv *env, jclass cls, jint method_index))
2465 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls));
2466 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
2467 Method* method = InstanceKlass::cast(k)->methods()->at(method_index);
2468 return method->name()->as_utf8();
2469 JVM_END
2470
2471
2472 JVM_ENTRY(const char*, JVM_GetMethodIxSignatureUTF(JNIEnv *env, jclass cls, jint method_index))
3223 JVM_LEAF(void*, JVM_FindLibraryEntry(void* handle, const char* name))
3224 void* find_result = os::dll_lookup(handle, name);
3225 log_info(library)("%s %s in library with handle " INTPTR_FORMAT,
3226 find_result != nullptr ? "Found" : "Failed to find",
3227 name, p2i(handle));
3228 return find_result;
3229 JVM_END
3230
3231
3232 // JNI version ///////////////////////////////////////////////////////////////////////////////
3233
3234 JVM_LEAF(jboolean, JVM_IsSupportedJNIVersion(jint version))
3235 return Threads::is_supported_jni_version_including_1_1(version);
3236 JVM_END
3237
3238
3239 JVM_LEAF(jboolean, JVM_IsPreviewEnabled(void))
3240 return Arguments::enable_preview() ? JNI_TRUE : JNI_FALSE;
3241 JVM_END
3242
3243 JVM_LEAF(jboolean, JVM_IsContinuationsSupported(void))
3244 return VMContinuations ? JNI_TRUE : JNI_FALSE;
3245 JVM_END
3246
3247 JVM_LEAF(jboolean, JVM_IsForeignLinkerSupported(void))
3248 return ForeignGlobals::is_foreign_linker_supported() ? JNI_TRUE : JNI_FALSE;
3249 JVM_END
3250
3251 JVM_LEAF(jboolean, JVM_IsStaticallyLinked(void))
3252 return is_vm_statically_linked() ? JNI_TRUE : JNI_FALSE;
3253 JVM_END
3254
3255 // String support ///////////////////////////////////////////////////////////////////////////
3256
3257 JVM_ENTRY(jstring, JVM_InternString(JNIEnv *env, jstring str))
3258 JvmtiVMObjectAllocEventCollector oam;
3259 if (str == nullptr) return nullptr;
3260 oop string = JNIHandles::resolve_non_null(str);
3261 oop result = StringTable::intern(string, CHECK_NULL);
3262 return (jstring) JNIHandles::make_local(THREAD, result);
3302
3303 jclass find_class_from_class_loader(JNIEnv* env, Symbol* name, jboolean init,
3304 Handle loader, jboolean throwError, TRAPS) {
3305 Klass* klass = SystemDictionary::resolve_or_fail(name, loader, throwError != 0, CHECK_NULL);
3306
3307 // Check if we should initialize the class
3308 if (init && klass->is_instance_klass()) {
3309 klass->initialize(CHECK_NULL);
3310 }
3311 return (jclass) JNIHandles::make_local(THREAD, klass->java_mirror());
3312 }
3313
3314
3315 // Method ///////////////////////////////////////////////////////////////////////////////////////////
3316
3317 JVM_ENTRY(jobject, JVM_InvokeMethod(JNIEnv *env, jobject method, jobject obj, jobjectArray args0))
3318 Handle method_handle;
3319 if (thread->stack_overflow_state()->stack_available((address) &method_handle) >= JVMInvokeMethodSlack) {
3320 method_handle = Handle(THREAD, JNIHandles::resolve(method));
3321 Handle receiver(THREAD, JNIHandles::resolve(obj));
3322 objArrayHandle args(THREAD, objArrayOop(JNIHandles::resolve(args0)));
3323 oop result = Reflection::invoke_method(method_handle(), receiver, args, CHECK_NULL);
3324 jobject res = JNIHandles::make_local(THREAD, result);
3325 if (JvmtiExport::should_post_vm_object_alloc()) {
3326 oop ret_type = java_lang_reflect_Method::return_type(method_handle());
3327 assert(ret_type != nullptr, "sanity check: ret_type oop must not be null!");
3328 if (java_lang_Class::is_primitive(ret_type)) {
3329 // Only for primitive type vm allocates memory for java object.
3330 // See box() method.
3331 JvmtiExport::post_vm_object_alloc(thread, result);
3332 }
3333 }
3334 return res;
3335 } else {
3336 THROW_NULL(vmSymbols::java_lang_StackOverflowError());
3337 }
3338 JVM_END
3339
3340
3341 JVM_ENTRY(jobject, JVM_NewInstanceFromConstructor(JNIEnv *env, jobject c, jobjectArray args0))
3342 oop constructor_mirror = JNIHandles::resolve(c);
3343 objArrayHandle args(THREAD, objArrayOop(JNIHandles::resolve(args0)));
3344 oop result = Reflection::invoke_constructor(constructor_mirror, args, CHECK_NULL);
3345 jobject res = JNIHandles::make_local(THREAD, result);
3346 if (JvmtiExport::should_post_vm_object_alloc()) {
3347 JvmtiExport::post_vm_object_alloc(thread, result);
3348 }
3349 return res;
3350 JVM_END
3351
3352 JVM_ENTRY(void, JVM_InitializeFromArchive(JNIEnv* env, jclass cls))
3353 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve(cls));
3354 HeapShared::initialize_from_archived_subgraph(THREAD, k);
3355 JVM_END
3356
3357 JVM_ENTRY(void, JVM_RegisterLambdaProxyClassForArchiving(JNIEnv* env,
3358 jclass caller,
3359 jstring interfaceMethodName,
3360 jobject factoryType,
3361 jobject interfaceMethodType,
3362 jobject implementationMember,
3363 jobject dynamicMethodType,
|
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/recordComponent.hpp"
66 #include "oops/objArrayKlass.hpp"
67 #include "oops/objArrayOop.inline.hpp"
68 #include "oops/oop.inline.hpp"
69 #include "prims/foreignGlobals.hpp"
70 #include "prims/jvm_misc.hpp"
71 #include "prims/jvmtiExport.hpp"
72 #include "prims/jvmtiThreadState.inline.hpp"
73 #include "prims/stackwalk.hpp"
74 #include "runtime/arguments.hpp"
75 #include "runtime/atomic.hpp"
76 #include "runtime/continuation.hpp"
77 #include "runtime/globals_extension.hpp"
78 #include "runtime/handles.inline.hpp"
79 #include "runtime/init.hpp"
80 #include "runtime/interfaceSupport.inline.hpp"
81 #include "runtime/deoptimization.hpp"
397
398 return (jobjectArray) JNIHandles::make_local(THREAD, result_h());
399 JVM_END
400
401
402 /*
403 * Return the temporary directory that the VM uses for the attach
404 * and perf data files.
405 *
406 * It is important that this directory is well-known and the
407 * same for all VM instances. It cannot be affected by configuration
408 * variables such as java.io.tmpdir.
409 */
410 JVM_ENTRY(jstring, JVM_GetTemporaryDirectory(JNIEnv *env))
411 HandleMark hm(THREAD);
412 const char* temp_dir = os::get_temp_directory();
413 Handle h = java_lang_String::create_from_platform_dependent_str(temp_dir, CHECK_NULL);
414 return (jstring) JNIHandles::make_local(THREAD, h());
415 JVM_END
416
417 static void validate_array_arguments(Klass* elmClass, jint len, TRAPS) {
418 if (len < 0) {
419 THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), "Array length is negative");
420 }
421 elmClass->initialize(CHECK);
422 if (elmClass->is_identity_class()) {
423 THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), "Element class is not a value class");
424 }
425 if (elmClass->is_abstract()) {
426 THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), "Element class is abstract");
427 }
428 }
429
430 JVM_ENTRY(jarray, JVM_CopyOfSpecialArray(JNIEnv *env, jarray orig, jint from, jint to))
431 oop o = JNIHandles::resolve_non_null(orig);
432 assert(o->is_array(), "Must be");
433 oop array = nullptr;
434 arrayOop org = (arrayOop)o;
435 arrayHandle oh(THREAD, org);
436 ArrayKlass* ak = ArrayKlass::cast(org->klass());
437 InlineKlass* vk = InlineKlass::cast(ak->element_klass());
438 int len = to - from; // length of the new array
439 if (ak->is_null_free_array_klass()) {
440 if (from >= org->length() || to > org->length()) {
441 THROW_MSG_NULL(vmSymbols::java_lang_IllegalArgumentException(), "Copying of null-free array with uninitialized elements");
442 }
443 }
444 if (org->is_flatArray()) {
445 FlatArrayKlass* fak = FlatArrayKlass::cast(org->klass());
446 LayoutKind lk = fak->layout_kind();
447 array = oopFactory::new_flatArray(vk, len, lk, CHECK_NULL);
448 arrayHandle ah(THREAD, (arrayOop)array);
449 int end = to < oh()->length() ? to : oh()->length();
450 for (int i = from; i < end; i++) {
451 void* src = ((flatArrayOop)oh())->value_at_addr(i, fak->layout_helper());
452 void* dst = ((flatArrayOop)ah())->value_at_addr(i - from, fak->layout_helper());
453 vk->copy_payload_to_addr(src, dst, lk, false);
454 }
455 array = ah();
456 } else {
457 if (org->is_null_free_array()) {
458 array = oopFactory::new_null_free_objArray(vk, len, CHECK_NULL);
459 } else {
460 array = oopFactory::new_objArray(vk, len, CHECK_NULL);
461 }
462 int end = to < oh()->length() ? to : oh()->length();
463 for (int i = from; i < end; i++) {
464 if (i < ((objArrayOop)oh())->length()) {
465 ((objArrayOop)array)->obj_at_put(i - from, ((objArrayOop)oh())->obj_at(i));
466 } else {
467 assert(!ak->is_null_free_array_klass(), "Must be a nullable array");
468 ((objArrayOop)array)->obj_at_put(i - from, nullptr);
469 }
470 }
471 }
472 return (jarray) JNIHandles::make_local(THREAD, array);
473 JVM_END
474
475 JVM_ENTRY(jarray, JVM_NewNullRestrictedNonAtomicArray(JNIEnv *env, jclass elmClass, jint len, jobject initVal))
476 oop mirror = JNIHandles::resolve_non_null(elmClass);
477 oop init = JNIHandles::resolve(initVal);
478 if (init == nullptr) {
479 THROW_MSG_NULL(vmSymbols::java_lang_IllegalArgumentException(), "Initial value cannot be null");
480 }
481 Handle init_h(THREAD, init);
482 Klass* klass = java_lang_Class::as_Klass(mirror);
483 if (klass != init_h()->klass()) {
484 THROW_MSG_NULL(vmSymbols::java_lang_IllegalArgumentException(), "Type mismatch between array and initial value");
485 }
486 validate_array_arguments(klass, len, CHECK_NULL);
487 InlineKlass* vk = InlineKlass::cast(klass);
488 // if (!vk->is_implicitly_constructible()) {
489 // THROW_MSG_NULL(vmSymbols::java_lang_IllegalArgumentException(), "Element class is not implicitly constructible");
490 // }
491 oop array = nullptr;
492 if (vk->flat_array() && vk->has_non_atomic_layout()) {
493 array = oopFactory::new_flatArray(vk, len, LayoutKind::NON_ATOMIC_FLAT, CHECK_NULL);
494 for (int i = 0; i < len; i++) {
495 ((flatArrayOop)array)->write_value_to_flat_array(init_h(), i, CHECK_NULL);
496 }
497 } else {
498 array = oopFactory::new_null_free_objArray(vk, len, CHECK_NULL);
499 for (int i = 0; i < len; i++) {
500 ((objArrayOop)array)->obj_at_put(i, init_h());
501 }
502 }
503 return (jarray) JNIHandles::make_local(THREAD, array);
504 JVM_END
505
506 JVM_ENTRY(jarray, JVM_NewNullRestrictedAtomicArray(JNIEnv *env, jclass elmClass, jint len, jobject initVal))
507 oop mirror = JNIHandles::resolve_non_null(elmClass);
508 oop init = JNIHandles::resolve(initVal);
509 if (init == nullptr) {
510 THROW_MSG_NULL(vmSymbols::java_lang_IllegalArgumentException(), "Initial value cannot be null");
511 }
512 Handle init_h(THREAD, init);
513 Klass* klass = java_lang_Class::as_Klass(mirror);
514 if (klass != init_h()->klass()) {
515 THROW_MSG_NULL(vmSymbols::java_lang_IllegalArgumentException(), "Type mismatch between array and initial value");
516 }
517 validate_array_arguments(klass, len, CHECK_NULL);
518 InlineKlass* vk = InlineKlass::cast(klass);
519 // if (!vk->is_implicitly_constructible()) {
520 // THROW_MSG_NULL(vmSymbols::java_lang_IllegalArgumentException(), "Element class is not implicitly constructible");
521 // }
522 oop array = nullptr;
523 if (UseArrayFlattening && vk->is_naturally_atomic() && vk->has_non_atomic_layout()) {
524 array = oopFactory::new_flatArray(vk, len, LayoutKind::NON_ATOMIC_FLAT, CHECK_NULL);
525 for (int i = 0; i < len; i++) {
526 ((flatArrayOop)array)->write_value_to_flat_array(init_h(), i, CHECK_NULL);
527 }
528 } else if (UseArrayFlattening && vk->has_atomic_layout()) {
529 array = oopFactory::new_flatArray(vk, len, LayoutKind::ATOMIC_FLAT, CHECK_NULL);
530 for (int i = 0; i < len; i++) {
531 ((flatArrayOop)array)->write_value_to_flat_array(init_h(), i, CHECK_NULL);
532 }
533 } else {
534 array = oopFactory::new_null_free_objArray(vk, len, CHECK_NULL);
535 for (int i = 0; i < len; i++) {
536 // need a type check here
537
538 ((objArrayOop)array)->obj_at_put(i, init_h());
539 }
540 }
541 return (jarray) JNIHandles::make_local(THREAD, array);
542 JVM_END
543
544 JVM_ENTRY(jarray, JVM_NewNullableAtomicArray(JNIEnv *env, jclass elmClass, jint len))
545 oop mirror = JNIHandles::resolve_non_null(elmClass);
546 Klass* klass = java_lang_Class::as_Klass(mirror);
547 klass->initialize(CHECK_NULL);
548 validate_array_arguments(klass, len, CHECK_NULL);
549 InlineKlass* vk = InlineKlass::cast(klass);
550 oop array = nullptr;
551 if (UseArrayFlattening && vk->has_nullable_atomic_layout()) {
552 array = oopFactory::new_flatArray(vk, len, LayoutKind::NULLABLE_ATOMIC_FLAT, CHECK_NULL);
553 } else {
554 array = oopFactory::new_objArray(vk, len, CHECK_NULL);
555 }
556 return (jarray) JNIHandles::make_local(THREAD, array);
557 JVM_END
558
559 JVM_ENTRY(jboolean, JVM_IsFlatArray(JNIEnv *env, jobject obj))
560 arrayOop oop = arrayOop(JNIHandles::resolve_non_null(obj));
561 return oop->is_flatArray();
562 JVM_END
563
564 JVM_ENTRY(jboolean, JVM_IsNullRestrictedArray(JNIEnv *env, jobject obj))
565 arrayOop oop = arrayOop(JNIHandles::resolve_non_null(obj));
566 return oop->is_null_free_array();
567 JVM_END
568
569 JVM_ENTRY(jboolean, JVM_IsAtomicArray(JNIEnv *env, jobject obj))
570 // There are multiple cases where an array can/must support atomic access:
571 // - the array is a reference array
572 // - the array uses an atomic flat layout: NULLABLE_ATOMIC_FLAT or ATOMIC_FLAT
573 // - the array is flat and its component type is naturally atomic
574 arrayOop oop = arrayOop(JNIHandles::resolve_non_null(obj));
575 if (oop->is_objArray()) return true;
576 if (oop->is_flatArray()) {
577 FlatArrayKlass* fak = FlatArrayKlass::cast(oop->klass());
578 if (fak->layout_kind() == LayoutKind::ATOMIC_FLAT || fak->layout_kind() == LayoutKind::NULLABLE_ATOMIC_FLAT) {
579 return true;
580 }
581 if (fak->element_klass()->is_naturally_atomic()) return true;
582 }
583 return false;
584 JVM_END
585
586 // java.lang.Runtime /////////////////////////////////////////////////////////////////////////
587
588 extern volatile jint vm_created;
589
590 JVM_ENTRY_NO_ENV(void, JVM_BeforeHalt())
591 EventShutdown event;
592 if (event.should_commit()) {
593 event.set_reason("Shutdown requested from Java");
594 event.commit();
595 }
596 JVM_END
597
598
599 JVM_ENTRY_NO_ENV(void, JVM_Halt(jint code))
600 before_exit(thread, true);
601 vm_exit(code);
602 JVM_END
603
604
773
774 Handle stackStream_h(THREAD, JNIHandles::resolve_non_null(stackStream));
775 return StackWalk::fetchNextBatch(stackStream_h, mode, anchor, last_batch_count, buffer_size,
776 start_index, frames_array_h, THREAD);
777 JVM_END
778
779 JVM_ENTRY(void, JVM_SetStackWalkContinuation(JNIEnv *env, jobject stackStream, jlong anchor, jobjectArray frames, jobject cont))
780 objArrayOop fa = objArrayOop(JNIHandles::resolve_non_null(frames));
781 objArrayHandle frames_array_h(THREAD, fa);
782 Handle stackStream_h(THREAD, JNIHandles::resolve_non_null(stackStream));
783 Handle cont_h(THREAD, JNIHandles::resolve_non_null(cont));
784
785 StackWalk::setContinuation(stackStream_h, anchor, frames_array_h, cont_h, THREAD);
786 JVM_END
787
788 // java.lang.Object ///////////////////////////////////////////////
789
790
791 JVM_ENTRY(jint, JVM_IHashCode(JNIEnv* env, jobject handle))
792 // as implemented in the classic virtual machine; return 0 if object is null
793 if (handle == nullptr) {
794 return 0;
795 }
796 oop obj = JNIHandles::resolve_non_null(handle);
797 if (EnableValhalla && obj->klass()->is_inline_klass()) {
798 JavaValue result(T_INT);
799 JavaCallArguments args;
800 Handle ho(THREAD, obj);
801 args.push_oop(ho);
802 methodHandle method(THREAD, Universe::value_object_hash_code_method());
803 JavaCalls::call(&result, method, &args, THREAD);
804 if (HAS_PENDING_EXCEPTION) {
805 if (!PENDING_EXCEPTION->is_a(vmClasses::Error_klass())) {
806 Handle e(THREAD, PENDING_EXCEPTION);
807 CLEAR_PENDING_EXCEPTION;
808 THROW_MSG_CAUSE_(vmSymbols::java_lang_InternalError(), "Internal error in hashCode", e, false);
809 }
810 }
811 return result.get_jint();
812 } else {
813 return checked_cast<jint>(ObjectSynchronizer::FastHashCode(THREAD, obj));
814 }
815 JVM_END
816
817
818 JVM_ENTRY(void, JVM_MonitorWait(JNIEnv* env, jobject handle, jlong ms))
819 Handle obj(THREAD, JNIHandles::resolve_non_null(handle));
820 ObjectSynchronizer::wait(obj, ms, CHECK);
821 JVM_END
822
823
824 JVM_ENTRY(void, JVM_MonitorNotify(JNIEnv* env, jobject handle))
825 Handle obj(THREAD, JNIHandles::resolve_non_null(handle));
826 ObjectSynchronizer::notify(obj, CHECK);
827 JVM_END
828
829
830 JVM_ENTRY(void, JVM_MonitorNotifyAll(JNIEnv* env, jobject handle))
831 Handle obj(THREAD, JNIHandles::resolve_non_null(handle));
832 ObjectSynchronizer::notifyall(obj, CHECK);
833 JVM_END
834
842 // Just checking that the cloneable flag is set correct
843 if (obj->is_array()) {
844 guarantee(klass->is_cloneable(), "all arrays are cloneable");
845 } else {
846 guarantee(obj->is_instance(), "should be instanceOop");
847 bool cloneable = klass->is_subtype_of(vmClasses::Cloneable_klass());
848 guarantee(cloneable == klass->is_cloneable(), "incorrect cloneable flag");
849 }
850 #endif
851
852 // Check if class of obj supports the Cloneable interface.
853 // All arrays are considered to be cloneable (See JLS 20.1.5).
854 // All j.l.r.Reference classes are considered non-cloneable.
855 if (!klass->is_cloneable() ||
856 (klass->is_instance_klass() &&
857 InstanceKlass::cast(klass)->reference_type() != REF_NONE)) {
858 ResourceMark rm(THREAD);
859 THROW_MSG_NULL(vmSymbols::java_lang_CloneNotSupportedException(), klass->external_name());
860 }
861
862 if (klass->is_inline_klass()) {
863 // Value instances have no identity, so return the current instance instead of allocating a new one
864 // Value classes cannot have finalizers, so the method can return immediately
865 return JNIHandles::make_local(THREAD, obj());
866 }
867
868 // Make shallow object copy
869 const size_t size = obj->size();
870 oop new_obj_oop = nullptr;
871 if (obj->is_array()) {
872 const int length = ((arrayOop)obj())->length();
873 new_obj_oop = Universe::heap()->array_allocate(klass, size, length,
874 /* do_zero */ true, CHECK_NULL);
875 } else {
876 new_obj_oop = Universe::heap()->obj_allocate(klass, size, CHECK_NULL);
877 }
878
879 HeapAccess<>::clone(obj(), new_obj_oop, size);
880
881 Handle new_obj(THREAD, new_obj_oop);
882 // Caution: this involves a java upcall, so the clone should be
883 // "gc-robust" by this stage.
884 if (klass->has_finalizer()) {
885 assert(obj->is_instance(), "should be instanceOop");
886 new_obj_oop = InstanceKlass::register_finalizer(instanceOop(new_obj()), CHECK_NULL);
887 new_obj = Handle(THREAD, new_obj_oop);
1342 oop result = java_lang_Class::name(java_class, CHECK_NULL);
1343 return (jstring) JNIHandles::make_local(THREAD, result);
1344 JVM_END
1345
1346
1347 JVM_ENTRY(jobjectArray, JVM_GetClassInterfaces(JNIEnv *env, jclass cls))
1348 JvmtiVMObjectAllocEventCollector oam;
1349 oop mirror = JNIHandles::resolve_non_null(cls);
1350
1351 // Special handling for primitive objects
1352 if (java_lang_Class::is_primitive(mirror)) {
1353 // Primitive objects does not have any interfaces
1354 objArrayOop r = oopFactory::new_objArray(vmClasses::Class_klass(), 0, CHECK_NULL);
1355 return (jobjectArray) JNIHandles::make_local(THREAD, r);
1356 }
1357
1358 Klass* klass = java_lang_Class::as_Klass(mirror);
1359 // Figure size of result array
1360 int size;
1361 if (klass->is_instance_klass()) {
1362 InstanceKlass* ik = InstanceKlass::cast(klass);
1363 size = ik->local_interfaces()->length();
1364 } else {
1365 assert(klass->is_objArray_klass() || klass->is_typeArray_klass() || klass->is_flatArray_klass(), "Illegal mirror klass");
1366 size = 2;
1367 }
1368
1369 // Allocate result array
1370 objArrayOop r = oopFactory::new_objArray(vmClasses::Class_klass(), size, CHECK_NULL);
1371 objArrayHandle result (THREAD, r);
1372 // Fill in result
1373 if (klass->is_instance_klass()) {
1374 // Regular instance klass, fill in all local interfaces
1375 for (int index = 0; index < size; index++) {
1376 InstanceKlass* ik = InstanceKlass::cast(klass);
1377 Klass* k = ik->local_interfaces()->at(index);
1378 result->obj_at_put(index, k->java_mirror());
1379 }
1380 } else {
1381 // All arrays implement java.lang.Cloneable and java.io.Serializable
1382 result->obj_at_put(0, vmClasses::Cloneable_klass()->java_mirror());
1383 result->obj_at_put(1, vmClasses::Serializable_klass()->java_mirror());
1384 }
1385 return (jobjectArray) JNIHandles::make_local(THREAD, result());
1386 JVM_END
1387
1388
1389 JVM_ENTRY(jboolean, JVM_IsHiddenClass(JNIEnv *env, jclass cls))
1390 oop mirror = JNIHandles::resolve_non_null(cls);
1391 if (java_lang_Class::is_primitive(mirror)) {
1392 return JNI_FALSE;
1393 }
1394 Klass* k = java_lang_Class::as_Klass(mirror);
1395 return k->is_hidden();
1396 JVM_END
1397
1398 JVM_ENTRY(jboolean, JVM_IsIdentityClass(JNIEnv *env, jclass cls))
1399 oop mirror = JNIHandles::resolve_non_null(cls);
1400 if (java_lang_Class::is_primitive(mirror)) {
1401 return JNI_FALSE;
1402 }
1403 Klass* k = java_lang_Class::as_Klass(mirror);
1404 if (EnableValhalla) {
1405 return k->is_array_klass() || k->is_identity_class();
1406 } else {
1407 return k->is_interface() ? JNI_FALSE : JNI_TRUE;
1408 }
1409 JVM_END
1410
1411 class ScopedValueBindingsResolver {
1412 public:
1413 InstanceKlass* Carrier_klass;
1414 ScopedValueBindingsResolver(JavaThread* THREAD) {
1415 Klass *k = SystemDictionary::resolve_or_fail(vmSymbols::java_lang_ScopedValue_Carrier(), true, THREAD);
1416 Carrier_klass = InstanceKlass::cast(k);
1417 }
1418 };
1419
1420 JVM_ENTRY(jobject, JVM_FindScopedValueBindings(JNIEnv *env, jclass cls))
1421 ResourceMark rm(THREAD);
1422 GrowableArray<Handle>* local_array = new GrowableArray<Handle>(12);
1423 JvmtiVMObjectAllocEventCollector oam;
1424
1425 static ScopedValueBindingsResolver resolver(THREAD);
1426
1427 // Iterate through Java frames
1428 vframeStream vfst(thread);
1429 for(; !vfst.at_end(); vfst.next()) {
1874 }
1875
1876 InstanceKlass* k = InstanceKlass::cast(java_lang_Class::as_Klass(ofMirror));
1877
1878 // Ensure class is linked
1879 k->link_class(CHECK_NULL);
1880
1881 Array<Method*>* methods = k->methods();
1882 int methods_length = methods->length();
1883
1884 // Save original method_idnum in case of redefinition, which can change
1885 // the idnum of obsolete methods. The new method will have the same idnum
1886 // but if we refresh the methods array, the counts will be wrong.
1887 ResourceMark rm(THREAD);
1888 GrowableArray<int>* idnums = new GrowableArray<int>(methods_length);
1889 int num_methods = 0;
1890
1891 // Select methods matching the criteria.
1892 for (int i = 0; i < methods_length; i++) {
1893 Method* method = methods->at(i);
1894 if (want_constructor && !method->is_object_constructor()) {
1895 continue;
1896 }
1897 if (!want_constructor &&
1898 (method->is_object_constructor() || method->is_class_initializer() ||
1899 method->is_overpass())) {
1900 continue;
1901 }
1902 if (publicOnly && !method->is_public()) {
1903 continue;
1904 }
1905 idnums->push(method->method_idnum());
1906 ++num_methods;
1907 }
1908
1909 // Allocate result
1910 objArrayOop r = oopFactory::new_objArray(klass, num_methods, CHECK_NULL);
1911 objArrayHandle result (THREAD, r);
1912
1913 // Now just put the methods that we selected above, but go by their idnum
1914 // in case of redefinition. The methods can be redefined at any safepoint,
1915 // so above when allocating the oop array and below when creating reflect
1916 // objects.
1917 for (int i = 0; i < num_methods; i++) {
1918 methodHandle method(THREAD, k->method_with_idnum(idnums->at(i)));
1919 if (method.is_null()) {
1920 // Method may have been deleted and seems this API can handle null
1921 // Otherwise should probably put a method that throws NSME
1922 result->obj_at_put(i, nullptr);
1923 } else {
1924 oop m;
1925 if (want_constructor) {
1926 assert(method->is_object_constructor(), "must be");
1927 m = Reflection::new_constructor(method, CHECK_NULL);
1928 } else {
1929 m = Reflection::new_method(method, false, CHECK_NULL);
1930 }
1931 result->obj_at_put(i, m);
1932 }
1933 }
1934
1935 return (jobjectArray) JNIHandles::make_local(THREAD, result());
1936 }
1937
1938 JVM_ENTRY(jobjectArray, JVM_GetClassDeclaredMethods(JNIEnv *env, jclass ofClass, jboolean publicOnly))
1939 {
1940 return get_class_declared_methods_helper(env, ofClass, publicOnly,
1941 /*want_constructor*/ false,
1942 vmClasses::reflect_Method_klass(), THREAD);
1943 }
1944 JVM_END
1945
1946 JVM_ENTRY(jobjectArray, JVM_GetClassDeclaredConstructors(JNIEnv *env, jclass ofClass, jboolean publicOnly))
2189 constantTag tag = cp->tag_at(index);
2190 if (!tag.is_method() && !tag.is_interface_method()) {
2191 THROW_MSG_NULL(vmSymbols::java_lang_IllegalArgumentException(), "Wrong type at constant pool index");
2192 }
2193 int klass_ref = cp->uncached_klass_ref_index_at(index);
2194 Klass* k_o;
2195 if (force_resolution) {
2196 k_o = cp->klass_at(klass_ref, CHECK_NULL);
2197 } else {
2198 k_o = ConstantPool::klass_at_if_loaded(cp, klass_ref);
2199 if (k_o == nullptr) return nullptr;
2200 }
2201 InstanceKlass* k = InstanceKlass::cast(k_o);
2202 Symbol* name = cp->uncached_name_ref_at(index);
2203 Symbol* sig = cp->uncached_signature_ref_at(index);
2204 methodHandle m (THREAD, k->find_method(name, sig));
2205 if (m.is_null()) {
2206 THROW_MSG_NULL(vmSymbols::java_lang_RuntimeException(), "Unable to look up method in target class");
2207 }
2208 oop method;
2209 if (m->is_object_constructor()) {
2210 method = Reflection::new_constructor(m, CHECK_NULL);
2211 } else {
2212 method = Reflection::new_method(m, true, CHECK_NULL);
2213 }
2214 return JNIHandles::make_local(THREAD, method);
2215 }
2216
2217 JVM_ENTRY(jobject, JVM_ConstantPoolGetMethodAt(JNIEnv *env, jobject obj, jobject unused, jint index))
2218 {
2219 JvmtiVMObjectAllocEventCollector oam;
2220 constantPoolHandle cp = constantPoolHandle(THREAD, reflect_ConstantPool::get_cp(JNIHandles::resolve_non_null(obj)));
2221 bounds_check(cp, index, CHECK_NULL);
2222 jobject res = get_method_at_helper(cp, index, true, CHECK_NULL);
2223 return res;
2224 }
2225 JVM_END
2226
2227 JVM_ENTRY(jobject, JVM_ConstantPoolGetMethodAtIfLoaded(JNIEnv *env, jobject obj, jobject unused, jint index))
2228 {
2229 JvmtiVMObjectAllocEventCollector oam;
2230 constantPoolHandle cp = constantPoolHandle(THREAD, reflect_ConstantPool::get_cp(JNIHandles::resolve_non_null(obj)));
2231 bounds_check(cp, index, CHECK_NULL);
2463 Klass* k = java_lang_Class::as_Klass(r);
2464 assert(k->is_instance_klass(), "must be an instance klass");
2465 if (!k->is_instance_klass()) return false;
2466
2467 ResourceMark rm(THREAD);
2468 const char* name = k->name()->as_C_string();
2469 bool system_class = k->class_loader() == nullptr;
2470 return JavaAssertions::enabled(name, system_class);
2471
2472 JVM_END
2473
2474
2475 // Return a new AssertionStatusDirectives object with the fields filled in with
2476 // command-line assertion arguments (i.e., -ea, -da).
2477 JVM_ENTRY(jobject, JVM_AssertionStatusDirectives(JNIEnv *env, jclass unused))
2478 JvmtiVMObjectAllocEventCollector oam;
2479 oop asd = JavaAssertions::createAssertionStatusDirectives(CHECK_NULL);
2480 return JNIHandles::make_local(THREAD, asd);
2481 JVM_END
2482
2483 // Arrays support /////////////////////////////////////////////////////////////
2484
2485 JVM_ENTRY(jboolean, JVM_ArrayIsAccessAtomic(JNIEnv *env, jclass unused, jobject array))
2486 oop o = JNIHandles::resolve(array);
2487 Klass* k = o->klass();
2488 if ((o == nullptr) || (!k->is_array_klass())) {
2489 THROW_0(vmSymbols::java_lang_IllegalArgumentException());
2490 }
2491 return ArrayKlass::cast(k)->element_access_must_be_atomic();
2492 JVM_END
2493
2494 JVM_ENTRY(jobject, JVM_ArrayEnsureAccessAtomic(JNIEnv *env, jclass unused, jobject array))
2495 oop o = JNIHandles::resolve(array);
2496 Klass* k = o->klass();
2497 if ((o == nullptr) || (!k->is_array_klass())) {
2498 THROW_0(vmSymbols::java_lang_IllegalArgumentException());
2499 }
2500 if (k->is_flatArray_klass()) {
2501 FlatArrayKlass* vk = FlatArrayKlass::cast(k);
2502 if (!vk->element_access_must_be_atomic()) {
2503 /**
2504 * Need to decide how to implement:
2505 *
2506 * 1) Change to objArrayOop layout, therefore oop->klass() differs so
2507 * then "<atomic>[Qfoo;" klass needs to subclass "[Qfoo;" to pass through
2508 * "checkcast" & "instanceof"
2509 *
2510 * 2) Use extra header in the flatArrayOop to flag atomicity required and
2511 * possibly per instance lock structure. Said info, could be placed in
2512 * "trailer" rather than disturb the current arrayOop
2513 */
2514 Unimplemented();
2515 }
2516 }
2517 return array;
2518 JVM_END
2519
2520 // Verification ////////////////////////////////////////////////////////////////////////////////
2521
2522 // Reflection for the verifier /////////////////////////////////////////////////////////////////
2523
2524 // RedefineClasses support: bug 6214132 caused verification to fail.
2525 // All functions from this section should call the jvmtiThreadSate function:
2526 // Klass* class_to_verify_considering_redefinition(Klass* klass).
2527 // The function returns a Klass* of the _scratch_class if the verifier
2528 // was invoked in the middle of the class redefinition.
2529 // Otherwise it returns its argument value which is the _the_class Klass*.
2530 // Please, refer to the description in the jvmtiThreadState.hpp.
2531
2532 JVM_ENTRY(jboolean, JVM_IsInterface(JNIEnv *env, jclass cls))
2533 oop mirror = JNIHandles::resolve_non_null(cls);
2534 if (java_lang_Class::is_primitive(mirror)) {
2535 return JNI_FALSE;
2536 }
2537 Klass* k = java_lang_Class::as_Klass(mirror);
2538 // This isn't necessary since answer is the same since redefinition
2539 // has already checked this matches for the scratch class.
2678 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls));
2679 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
2680 Method* method = InstanceKlass::cast(k)->methods()->at(method_index);
2681 return method->size_of_parameters();
2682 JVM_END
2683
2684
2685 JVM_ENTRY(jint, JVM_GetMethodIxMaxStack(JNIEnv *env, jclass cls, int method_index))
2686 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls));
2687 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
2688 Method* method = InstanceKlass::cast(k)->methods()->at(method_index);
2689 return method->verifier_max_stack();
2690 JVM_END
2691
2692
2693 JVM_ENTRY(jboolean, JVM_IsConstructorIx(JNIEnv *env, jclass cls, int method_index))
2694 ResourceMark rm(THREAD);
2695 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls));
2696 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
2697 Method* method = InstanceKlass::cast(k)->methods()->at(method_index);
2698 return method->is_object_constructor();
2699 JVM_END
2700
2701
2702 JVM_ENTRY(jboolean, JVM_IsVMGeneratedMethodIx(JNIEnv *env, jclass cls, int method_index))
2703 ResourceMark rm(THREAD);
2704 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls));
2705 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
2706 Method* method = InstanceKlass::cast(k)->methods()->at(method_index);
2707 return method->is_overpass();
2708 JVM_END
2709
2710 JVM_ENTRY(const char*, JVM_GetMethodIxNameUTF(JNIEnv *env, jclass cls, jint method_index))
2711 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls));
2712 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
2713 Method* method = InstanceKlass::cast(k)->methods()->at(method_index);
2714 return method->name()->as_utf8();
2715 JVM_END
2716
2717
2718 JVM_ENTRY(const char*, JVM_GetMethodIxSignatureUTF(JNIEnv *env, jclass cls, jint method_index))
3469 JVM_LEAF(void*, JVM_FindLibraryEntry(void* handle, const char* name))
3470 void* find_result = os::dll_lookup(handle, name);
3471 log_info(library)("%s %s in library with handle " INTPTR_FORMAT,
3472 find_result != nullptr ? "Found" : "Failed to find",
3473 name, p2i(handle));
3474 return find_result;
3475 JVM_END
3476
3477
3478 // JNI version ///////////////////////////////////////////////////////////////////////////////
3479
3480 JVM_LEAF(jboolean, JVM_IsSupportedJNIVersion(jint version))
3481 return Threads::is_supported_jni_version_including_1_1(version);
3482 JVM_END
3483
3484
3485 JVM_LEAF(jboolean, JVM_IsPreviewEnabled(void))
3486 return Arguments::enable_preview() ? JNI_TRUE : JNI_FALSE;
3487 JVM_END
3488
3489 JVM_LEAF(jboolean, JVM_IsValhallaEnabled(void))
3490 return EnableValhalla ? JNI_TRUE : JNI_FALSE;
3491 JVM_END
3492
3493 JVM_LEAF(jboolean, JVM_IsContinuationsSupported(void))
3494 return VMContinuations ? JNI_TRUE : JNI_FALSE;
3495 JVM_END
3496
3497 JVM_LEAF(jboolean, JVM_IsForeignLinkerSupported(void))
3498 return ForeignGlobals::is_foreign_linker_supported() ? JNI_TRUE : JNI_FALSE;
3499 JVM_END
3500
3501 JVM_LEAF(jboolean, JVM_IsStaticallyLinked(void))
3502 return is_vm_statically_linked() ? JNI_TRUE : JNI_FALSE;
3503 JVM_END
3504
3505 // String support ///////////////////////////////////////////////////////////////////////////
3506
3507 JVM_ENTRY(jstring, JVM_InternString(JNIEnv *env, jstring str))
3508 JvmtiVMObjectAllocEventCollector oam;
3509 if (str == nullptr) return nullptr;
3510 oop string = JNIHandles::resolve_non_null(str);
3511 oop result = StringTable::intern(string, CHECK_NULL);
3512 return (jstring) JNIHandles::make_local(THREAD, result);
3552
3553 jclass find_class_from_class_loader(JNIEnv* env, Symbol* name, jboolean init,
3554 Handle loader, jboolean throwError, TRAPS) {
3555 Klass* klass = SystemDictionary::resolve_or_fail(name, loader, throwError != 0, CHECK_NULL);
3556
3557 // Check if we should initialize the class
3558 if (init && klass->is_instance_klass()) {
3559 klass->initialize(CHECK_NULL);
3560 }
3561 return (jclass) JNIHandles::make_local(THREAD, klass->java_mirror());
3562 }
3563
3564
3565 // Method ///////////////////////////////////////////////////////////////////////////////////////////
3566
3567 JVM_ENTRY(jobject, JVM_InvokeMethod(JNIEnv *env, jobject method, jobject obj, jobjectArray args0))
3568 Handle method_handle;
3569 if (thread->stack_overflow_state()->stack_available((address) &method_handle) >= JVMInvokeMethodSlack) {
3570 method_handle = Handle(THREAD, JNIHandles::resolve(method));
3571 Handle receiver(THREAD, JNIHandles::resolve(obj));
3572 objArrayHandle args = oopFactory::ensure_objArray(JNIHandles::resolve(args0), CHECK_NULL);
3573 oop result = Reflection::invoke_method(method_handle(), receiver, args, CHECK_NULL);
3574 jobject res = JNIHandles::make_local(THREAD, result);
3575 if (JvmtiExport::should_post_vm_object_alloc()) {
3576 oop ret_type = java_lang_reflect_Method::return_type(method_handle());
3577 assert(ret_type != nullptr, "sanity check: ret_type oop must not be null!");
3578 if (java_lang_Class::is_primitive(ret_type)) {
3579 // Only for primitive type vm allocates memory for java object.
3580 // See box() method.
3581 JvmtiExport::post_vm_object_alloc(thread, result);
3582 }
3583 }
3584 return res;
3585 } else {
3586 THROW_NULL(vmSymbols::java_lang_StackOverflowError());
3587 }
3588 JVM_END
3589
3590
3591 JVM_ENTRY(jobject, JVM_NewInstanceFromConstructor(JNIEnv *env, jobject c, jobjectArray args0))
3592 objArrayHandle args = oopFactory::ensure_objArray(JNIHandles::resolve(args0), CHECK_NULL);
3593 oop constructor_mirror = JNIHandles::resolve(c);
3594 oop result = Reflection::invoke_constructor(constructor_mirror, args, CHECK_NULL);
3595 jobject res = JNIHandles::make_local(THREAD, result);
3596 if (JvmtiExport::should_post_vm_object_alloc()) {
3597 JvmtiExport::post_vm_object_alloc(thread, result);
3598 }
3599 return res;
3600 JVM_END
3601
3602 JVM_ENTRY(void, JVM_InitializeFromArchive(JNIEnv* env, jclass cls))
3603 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve(cls));
3604 HeapShared::initialize_from_archived_subgraph(THREAD, k);
3605 JVM_END
3606
3607 JVM_ENTRY(void, JVM_RegisterLambdaProxyClassForArchiving(JNIEnv* env,
3608 jclass caller,
3609 jstring interfaceMethodName,
3610 jobject factoryType,
3611 jobject interfaceMethodType,
3612 jobject implementationMember,
3613 jobject dynamicMethodType,
|