40 #include "classfile/modules.hpp"
41 #include "classfile/packageEntry.hpp"
42 #include "classfile/stringTable.hpp"
43 #include "classfile/symbolTable.hpp"
44 #include "classfile/systemDictionary.hpp"
45 #include "classfile/vmClasses.hpp"
46 #include "classfile/vmSymbols.hpp"
47 #include "gc/shared/collectedHeap.inline.hpp"
48 #include "interpreter/bytecode.hpp"
49 #include "interpreter/bytecodeUtils.hpp"
50 #include "jfr/jfrEvents.hpp"
51 #include "jvm.h"
52 #include "logging/log.hpp"
53 #include "memory/oopFactory.hpp"
54 #include "memory/referenceType.hpp"
55 #include "memory/resourceArea.hpp"
56 #include "memory/universe.hpp"
57 #include "oops/access.inline.hpp"
58 #include "oops/constantPool.hpp"
59 #include "oops/fieldStreams.inline.hpp"
60 #include "oops/instanceKlass.hpp"
61 #include "oops/klass.inline.hpp"
62 #include "oops/method.hpp"
63 #include "oops/recordComponent.hpp"
64 #include "oops/objArrayKlass.hpp"
65 #include "oops/objArrayOop.inline.hpp"
66 #include "oops/oop.inline.hpp"
67 #include "prims/foreignGlobals.hpp"
68 #include "prims/jvm_misc.hpp"
69 #include "prims/jvmtiExport.hpp"
70 #include "prims/jvmtiThreadState.inline.hpp"
71 #include "prims/stackwalk.hpp"
72 #include "runtime/arguments.hpp"
73 #include "runtime/atomic.hpp"
74 #include "runtime/continuation.hpp"
75 #include "runtime/globals_extension.hpp"
76 #include "runtime/handles.inline.hpp"
77 #include "runtime/init.hpp"
78 #include "runtime/interfaceSupport.inline.hpp"
79 #include "runtime/deoptimization.hpp"
407
408 return (jobjectArray) JNIHandles::make_local(THREAD, result_h());
409 JVM_END
410
411
412 /*
413 * Return the temporary directory that the VM uses for the attach
414 * and perf data files.
415 *
416 * It is important that this directory is well-known and the
417 * same for all VM instances. It cannot be affected by configuration
418 * variables such as java.io.tmpdir.
419 */
420 JVM_ENTRY(jstring, JVM_GetTemporaryDirectory(JNIEnv *env))
421 HandleMark hm(THREAD);
422 const char* temp_dir = os::get_temp_directory();
423 Handle h = java_lang_String::create_from_platform_dependent_str(temp_dir, CHECK_NULL);
424 return (jstring) JNIHandles::make_local(THREAD, h());
425 JVM_END
426
427
428 // java.lang.Runtime /////////////////////////////////////////////////////////////////////////
429
430 extern volatile jint vm_created;
431
432 JVM_ENTRY_NO_ENV(void, JVM_BeforeHalt())
433 EventShutdown event;
434 if (event.should_commit()) {
435 event.set_reason("Shutdown requested from Java");
436 event.commit();
437 }
438 JVM_END
439
440
441 JVM_ENTRY_NO_ENV(void, JVM_Halt(jint code))
442 before_exit(thread, true);
443 vm_exit(code);
444 JVM_END
445
446
606
607 Handle stackStream_h(THREAD, JNIHandles::resolve_non_null(stackStream));
608 return StackWalk::fetchNextBatch(stackStream_h, mode, anchor, last_batch_count, buffer_size,
609 start_index, frames_array_h, THREAD);
610 JVM_END
611
612 JVM_ENTRY(void, JVM_SetStackWalkContinuation(JNIEnv *env, jobject stackStream, jlong anchor, jobjectArray frames, jobject cont))
613 objArrayOop fa = objArrayOop(JNIHandles::resolve_non_null(frames));
614 objArrayHandle frames_array_h(THREAD, fa);
615 Handle stackStream_h(THREAD, JNIHandles::resolve_non_null(stackStream));
616 Handle cont_h(THREAD, JNIHandles::resolve_non_null(cont));
617
618 StackWalk::setContinuation(stackStream_h, anchor, frames_array_h, cont_h, THREAD);
619 JVM_END
620
621 // java.lang.Object ///////////////////////////////////////////////
622
623
624 JVM_ENTRY(jint, JVM_IHashCode(JNIEnv* env, jobject handle))
625 // as implemented in the classic virtual machine; return 0 if object is null
626 return handle == nullptr ? 0 :
627 checked_cast<jint>(ObjectSynchronizer::FastHashCode (THREAD, JNIHandles::resolve_non_null(handle)));
628 JVM_END
629
630
631 JVM_ENTRY(void, JVM_MonitorWait(JNIEnv* env, jobject handle, jlong ms))
632 Handle obj(THREAD, JNIHandles::resolve_non_null(handle));
633 JavaThreadInObjectWaitState jtiows(thread, ms != 0);
634 if (JvmtiExport::should_post_monitor_wait()) {
635 JvmtiExport::post_monitor_wait(thread, obj(), ms);
636
637 // The current thread already owns the monitor and it has not yet
638 // been added to the wait queue so the current thread cannot be
639 // made the successor. This means that the JVMTI_EVENT_MONITOR_WAIT
640 // event handler cannot accidentally consume an unpark() meant for
641 // the ParkEvent associated with this ObjectMonitor.
642 }
643 ObjectSynchronizer::wait(obj, ms, CHECK);
644 JVM_END
645
646
647 JVM_ENTRY(void, JVM_MonitorNotify(JNIEnv* env, jobject handle))
665 // Just checking that the cloneable flag is set correct
666 if (obj->is_array()) {
667 guarantee(klass->is_cloneable(), "all arrays are cloneable");
668 } else {
669 guarantee(obj->is_instance(), "should be instanceOop");
670 bool cloneable = klass->is_subtype_of(vmClasses::Cloneable_klass());
671 guarantee(cloneable == klass->is_cloneable(), "incorrect cloneable flag");
672 }
673 #endif
674
675 // Check if class of obj supports the Cloneable interface.
676 // All arrays are considered to be cloneable (See JLS 20.1.5).
677 // All j.l.r.Reference classes are considered non-cloneable.
678 if (!klass->is_cloneable() ||
679 (klass->is_instance_klass() &&
680 InstanceKlass::cast(klass)->reference_type() != REF_NONE)) {
681 ResourceMark rm(THREAD);
682 THROW_MSG_0(vmSymbols::java_lang_CloneNotSupportedException(), klass->external_name());
683 }
684
685 // Make shallow object copy
686 const size_t size = obj->size();
687 oop new_obj_oop = nullptr;
688 if (obj->is_array()) {
689 const int length = ((arrayOop)obj())->length();
690 new_obj_oop = Universe::heap()->array_allocate(klass, size, length,
691 /* do_zero */ true, CHECK_NULL);
692 } else {
693 new_obj_oop = Universe::heap()->obj_allocate(klass, size, CHECK_NULL);
694 }
695
696 HeapAccess<>::clone(obj(), new_obj_oop, size);
697
698 Handle new_obj(THREAD, new_obj_oop);
699 // Caution: this involves a java upcall, so the clone should be
700 // "gc-robust" by this stage.
701 if (klass->has_finalizer()) {
702 assert(obj->is_instance(), "should be instanceOop");
703 new_obj_oop = InstanceKlass::register_finalizer(instanceOop(new_obj()), CHECK_NULL);
704 new_obj = Handle(THREAD, new_obj_oop);
1175 oop result = java_lang_Class::name(java_class, CHECK_NULL);
1176 return (jstring) JNIHandles::make_local(THREAD, result);
1177 JVM_END
1178
1179
1180 JVM_ENTRY(jobjectArray, JVM_GetClassInterfaces(JNIEnv *env, jclass cls))
1181 JvmtiVMObjectAllocEventCollector oam;
1182 oop mirror = JNIHandles::resolve_non_null(cls);
1183
1184 // Special handling for primitive objects
1185 if (java_lang_Class::is_primitive(mirror)) {
1186 // Primitive objects does not have any interfaces
1187 objArrayOop r = oopFactory::new_objArray(vmClasses::Class_klass(), 0, CHECK_NULL);
1188 return (jobjectArray) JNIHandles::make_local(THREAD, r);
1189 }
1190
1191 Klass* klass = java_lang_Class::as_Klass(mirror);
1192 // Figure size of result array
1193 int size;
1194 if (klass->is_instance_klass()) {
1195 size = InstanceKlass::cast(klass)->local_interfaces()->length();
1196 } else {
1197 assert(klass->is_objArray_klass() || klass->is_typeArray_klass(), "Illegal mirror klass");
1198 size = 2;
1199 }
1200
1201 // Allocate result array
1202 objArrayOop r = oopFactory::new_objArray(vmClasses::Class_klass(), size, CHECK_NULL);
1203 objArrayHandle result (THREAD, r);
1204 // Fill in result
1205 if (klass->is_instance_klass()) {
1206 // Regular instance klass, fill in all local interfaces
1207 for (int index = 0; index < size; index++) {
1208 Klass* k = InstanceKlass::cast(klass)->local_interfaces()->at(index);
1209 result->obj_at_put(index, k->java_mirror());
1210 }
1211 } else {
1212 // All arrays implement java.lang.Cloneable and java.io.Serializable
1213 result->obj_at_put(0, vmClasses::Cloneable_klass()->java_mirror());
1214 result->obj_at_put(1, vmClasses::Serializable_klass()->java_mirror());
1215 }
1216 return (jobjectArray) JNIHandles::make_local(THREAD, result());
1217 JVM_END
1218
1219
1220 JVM_ENTRY(jboolean, JVM_IsInterface(JNIEnv *env, jclass cls))
1221 oop mirror = JNIHandles::resolve_non_null(cls);
1222 if (java_lang_Class::is_primitive(mirror)) {
1223 return JNI_FALSE;
1224 }
1225 Klass* k = java_lang_Class::as_Klass(mirror);
1226 jboolean result = k->is_interface();
1227 assert(!result || k->is_instance_klass(),
1228 "all interfaces are instance types");
1229 // The compiler intrinsic for isInterface tests the
1230 // Klass::_access_flags bits in the same way.
1231 return result;
1232 JVM_END
1233
1234 JVM_ENTRY(jboolean, JVM_IsHiddenClass(JNIEnv *env, jclass cls))
1235 oop mirror = JNIHandles::resolve_non_null(cls);
1236 if (java_lang_Class::is_primitive(mirror)) {
1237 return JNI_FALSE;
1238 }
1239 Klass* k = java_lang_Class::as_Klass(mirror);
1240 return k->is_hidden();
1241 JVM_END
1242
1243 JVM_ENTRY(jobjectArray, JVM_GetClassSigners(JNIEnv *env, jclass cls))
1244 JvmtiVMObjectAllocEventCollector oam;
1245 oop mirror = JNIHandles::resolve_non_null(cls);
1246 if (java_lang_Class::is_primitive(mirror)) {
1247 // There are no signers for primitive types
1248 return nullptr;
1249 }
1250
1251 objArrayHandle signers(THREAD, java_lang_Class::signers(mirror));
1252
1253 // If there are no signers set in the class, or if the class
1254 // is an array, return null.
1255 if (signers == nullptr) return nullptr;
1256
1257 // copy of the signers array
1258 Klass* element = ObjArrayKlass::cast(signers->klass())->element_klass();
1259 objArrayOop signers_copy = oopFactory::new_objArray(element, signers->length(), CHECK_NULL);
1260 for (int index = 0; index < signers->length(); index++) {
1261 signers_copy->obj_at_put(index, signers->obj_at(index));
1262 }
1837 int length = components->length();
1838 assert(length >= 0, "unexpected record_components length");
1839 objArrayOop record_components =
1840 oopFactory::new_objArray(vmClasses::RecordComponent_klass(), length, CHECK_NULL);
1841 objArrayHandle components_h (THREAD, record_components);
1842
1843 for (int x = 0; x < length; x++) {
1844 RecordComponent* component = components->at(x);
1845 assert(component != nullptr, "unexpected null record component");
1846 oop component_oop = java_lang_reflect_RecordComponent::create(ik, component, CHECK_NULL);
1847 components_h->obj_at_put(x, component_oop);
1848 }
1849 return (jobjectArray)JNIHandles::make_local(THREAD, components_h());
1850 }
1851
1852 return nullptr;
1853 }
1854 JVM_END
1855
1856 static bool select_method(const methodHandle& method, bool want_constructor) {
1857 if (want_constructor) {
1858 return (method->is_initializer() && !method->is_static());
1859 } else {
1860 return (!method->is_initializer() && !method->is_overpass());
1861 }
1862 }
1863
1864 static jobjectArray get_class_declared_methods_helper(
1865 JNIEnv *env,
1866 jclass ofClass, jboolean publicOnly,
1867 bool want_constructor,
1868 Klass* klass, TRAPS) {
1869
1870 JvmtiVMObjectAllocEventCollector oam;
1871
1872 oop ofMirror = JNIHandles::resolve_non_null(ofClass);
1873 // Exclude primitive types and array types
1874 if (java_lang_Class::is_primitive(ofMirror)
1875 || java_lang_Class::as_Klass(ofMirror)->is_array_klass()) {
1876 // Return empty array
1877 oop res = oopFactory::new_objArray(klass, 0, CHECK_NULL);
1878 return (jobjectArray) JNIHandles::make_local(THREAD, res);
1879 }
1880
1903 }
1904 }
1905
1906 // Allocate result
1907 objArrayOop r = oopFactory::new_objArray(klass, num_methods, CHECK_NULL);
1908 objArrayHandle result (THREAD, r);
1909
1910 // Now just put the methods that we selected above, but go by their idnum
1911 // in case of redefinition. The methods can be redefined at any safepoint,
1912 // so above when allocating the oop array and below when creating reflect
1913 // objects.
1914 for (int i = 0; i < num_methods; i++) {
1915 methodHandle method(THREAD, k->method_with_idnum(idnums->at(i)));
1916 if (method.is_null()) {
1917 // Method may have been deleted and seems this API can handle null
1918 // Otherwise should probably put a method that throws NSME
1919 result->obj_at_put(i, nullptr);
1920 } else {
1921 oop m;
1922 if (want_constructor) {
1923 m = Reflection::new_constructor(method, CHECK_NULL);
1924 } else {
1925 m = Reflection::new_method(method, false, CHECK_NULL);
1926 }
1927 result->obj_at_put(i, m);
1928 }
1929 }
1930
1931 return (jobjectArray) JNIHandles::make_local(THREAD, result());
1932 }
1933
1934 JVM_ENTRY(jobjectArray, JVM_GetClassDeclaredMethods(JNIEnv *env, jclass ofClass, jboolean publicOnly))
1935 {
1936 return get_class_declared_methods_helper(env, ofClass, publicOnly,
1937 /*want_constructor*/ false,
1938 vmClasses::reflect_Method_klass(), THREAD);
1939 }
1940 JVM_END
1941
1942 JVM_ENTRY(jobjectArray, JVM_GetClassDeclaredConstructors(JNIEnv *env, jclass ofClass, jboolean publicOnly))
2185 constantTag tag = cp->tag_at(index);
2186 if (!tag.is_method() && !tag.is_interface_method()) {
2187 THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(), "Wrong type at constant pool index");
2188 }
2189 int klass_ref = cp->uncached_klass_ref_index_at(index);
2190 Klass* k_o;
2191 if (force_resolution) {
2192 k_o = cp->klass_at(klass_ref, CHECK_NULL);
2193 } else {
2194 k_o = ConstantPool::klass_at_if_loaded(cp, klass_ref);
2195 if (k_o == nullptr) return nullptr;
2196 }
2197 InstanceKlass* k = InstanceKlass::cast(k_o);
2198 Symbol* name = cp->uncached_name_ref_at(index);
2199 Symbol* sig = cp->uncached_signature_ref_at(index);
2200 methodHandle m (THREAD, k->find_method(name, sig));
2201 if (m.is_null()) {
2202 THROW_MSG_0(vmSymbols::java_lang_RuntimeException(), "Unable to look up method in target class");
2203 }
2204 oop method;
2205 if (!m->is_initializer() || m->is_static()) {
2206 method = Reflection::new_method(m, true, CHECK_NULL);
2207 } else {
2208 method = Reflection::new_constructor(m, CHECK_NULL);
2209 }
2210 return JNIHandles::make_local(THREAD, method);
2211 }
2212
2213 JVM_ENTRY(jobject, JVM_ConstantPoolGetMethodAt(JNIEnv *env, jobject obj, jobject unused, jint index))
2214 {
2215 JvmtiVMObjectAllocEventCollector oam;
2216 constantPoolHandle cp = constantPoolHandle(THREAD, reflect_ConstantPool::get_cp(JNIHandles::resolve_non_null(obj)));
2217 bounds_check(cp, index, CHECK_NULL);
2218 jobject res = get_method_at_helper(cp, index, true, CHECK_NULL);
2219 return res;
2220 }
2221 JVM_END
2222
2223 JVM_ENTRY(jobject, JVM_ConstantPoolGetMethodAtIfLoaded(JNIEnv *env, jobject obj, jobject unused, jint index))
2224 {
2225 JvmtiVMObjectAllocEventCollector oam;
2226 constantPoolHandle cp = constantPoolHandle(THREAD, reflect_ConstantPool::get_cp(JNIHandles::resolve_non_null(obj)));
2227 bounds_check(cp, index, CHECK_NULL);
2228 jobject res = get_method_at_helper(cp, index, false, CHECK_NULL);
2459 Klass* k = java_lang_Class::as_Klass(r);
2460 assert(k->is_instance_klass(), "must be an instance klass");
2461 if (!k->is_instance_klass()) return false;
2462
2463 ResourceMark rm(THREAD);
2464 const char* name = k->name()->as_C_string();
2465 bool system_class = k->class_loader() == nullptr;
2466 return JavaAssertions::enabled(name, system_class);
2467
2468 JVM_END
2469
2470
2471 // Return a new AssertionStatusDirectives object with the fields filled in with
2472 // command-line assertion arguments (i.e., -ea, -da).
2473 JVM_ENTRY(jobject, JVM_AssertionStatusDirectives(JNIEnv *env, jclass unused))
2474 JvmtiVMObjectAllocEventCollector oam;
2475 oop asd = JavaAssertions::createAssertionStatusDirectives(CHECK_NULL);
2476 return JNIHandles::make_local(THREAD, asd);
2477 JVM_END
2478
2479 // Verification ////////////////////////////////////////////////////////////////////////////////
2480
2481 // Reflection for the verifier /////////////////////////////////////////////////////////////////
2482
2483 // RedefineClasses support: bug 6214132 caused verification to fail.
2484 // All functions from this section should call the jvmtiThreadSate function:
2485 // Klass* class_to_verify_considering_redefinition(Klass* klass).
2486 // The function returns a Klass* of the _scratch_class if the verifier
2487 // was invoked in the middle of the class redefinition.
2488 // Otherwise it returns its argument value which is the _the_class Klass*.
2489 // Please, refer to the description in the jvmtiThreadSate.hpp.
2490
2491 JVM_ENTRY(const char*, JVM_GetClassNameUTF(JNIEnv *env, jclass cls))
2492 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls));
2493 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
2494 return k->name()->as_utf8();
2495 JVM_END
2496
2497
2498 JVM_ENTRY(void, JVM_GetClassCPTypes(JNIEnv *env, jclass cls, unsigned char *types))
2588
2589 JVM_ENTRY(jint, JVM_GetMethodIxExceptionTableLength(JNIEnv *env, jclass cls, int method_index))
2590 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls));
2591 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
2592 Method* method = InstanceKlass::cast(k)->methods()->at(method_index);
2593 return method->exception_table_length();
2594 JVM_END
2595
2596
2597 JVM_ENTRY(jint, JVM_GetMethodIxModifiers(JNIEnv *env, jclass cls, int method_index))
2598 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls));
2599 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
2600 Method* method = InstanceKlass::cast(k)->methods()->at(method_index);
2601 return method->access_flags().as_int() & JVM_RECOGNIZED_METHOD_MODIFIERS;
2602 JVM_END
2603
2604
2605 JVM_ENTRY(jint, JVM_GetFieldIxModifiers(JNIEnv *env, jclass cls, int field_index))
2606 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls));
2607 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
2608 return InstanceKlass::cast(k)->field_access_flags(field_index) & JVM_RECOGNIZED_FIELD_MODIFIERS;
2609 JVM_END
2610
2611
2612 JVM_ENTRY(jint, JVM_GetMethodIxLocalsCount(JNIEnv *env, jclass cls, int method_index))
2613 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls));
2614 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
2615 Method* method = InstanceKlass::cast(k)->methods()->at(method_index);
2616 return method->max_locals();
2617 JVM_END
2618
2619
2620 JVM_ENTRY(jint, JVM_GetMethodIxArgsSize(JNIEnv *env, jclass cls, int method_index))
2621 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls));
2622 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
2623 Method* method = InstanceKlass::cast(k)->methods()->at(method_index);
2624 return method->size_of_parameters();
2625 JVM_END
2626
2627
2628 JVM_ENTRY(jint, JVM_GetMethodIxMaxStack(JNIEnv *env, jclass cls, int method_index))
2629 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls));
2630 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
2631 Method* method = InstanceKlass::cast(k)->methods()->at(method_index);
2632 return method->verifier_max_stack();
2633 JVM_END
2634
2635
2636 JVM_ENTRY(jboolean, JVM_IsConstructorIx(JNIEnv *env, jclass cls, int method_index))
2637 ResourceMark rm(THREAD);
2638 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls));
2639 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
2640 Method* method = InstanceKlass::cast(k)->methods()->at(method_index);
2641 return method->name() == vmSymbols::object_initializer_name();
2642 JVM_END
2643
2644
2645 JVM_ENTRY(jboolean, JVM_IsVMGeneratedMethodIx(JNIEnv *env, jclass cls, int method_index))
2646 ResourceMark rm(THREAD);
2647 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls));
2648 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
2649 Method* method = InstanceKlass::cast(k)->methods()->at(method_index);
2650 return method->is_overpass();
2651 JVM_END
2652
2653 JVM_ENTRY(const char*, JVM_GetMethodIxNameUTF(JNIEnv *env, jclass cls, jint method_index))
2654 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls));
2655 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
2656 Method* method = InstanceKlass::cast(k)->methods()->at(method_index);
2657 return method->name()->as_utf8();
2658 JVM_END
2659
2660
2661 JVM_ENTRY(const char*, JVM_GetMethodIxSignatureUTF(JNIEnv *env, jclass cls, jint method_index))
2778 }
2779 ShouldNotReachHere();
2780 return nullptr;
2781 JVM_END
2782
2783
2784 JVM_ENTRY(jint, JVM_GetCPFieldModifiers(JNIEnv *env, jclass cls, int cp_index, jclass called_cls))
2785 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls));
2786 Klass* k_called = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(called_cls));
2787 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
2788 k_called = JvmtiThreadState::class_to_verify_considering_redefinition(k_called, thread);
2789 ConstantPool* cp = InstanceKlass::cast(k)->constants();
2790 ConstantPool* cp_called = InstanceKlass::cast(k_called)->constants();
2791 switch (cp->tag_at(cp_index).value()) {
2792 case JVM_CONSTANT_Fieldref: {
2793 Symbol* name = cp->uncached_name_ref_at(cp_index);
2794 Symbol* signature = cp->uncached_signature_ref_at(cp_index);
2795 InstanceKlass* ik = InstanceKlass::cast(k_called);
2796 for (JavaFieldStream fs(ik); !fs.done(); fs.next()) {
2797 if (fs.name() == name && fs.signature() == signature) {
2798 return fs.access_flags().as_short() & JVM_RECOGNIZED_FIELD_MODIFIERS;
2799 }
2800 }
2801 return -1;
2802 }
2803 default:
2804 fatal("JVM_GetCPFieldModifiers: illegal constant");
2805 }
2806 ShouldNotReachHere();
2807 return 0;
2808 JVM_END
2809
2810
2811 JVM_ENTRY(jint, JVM_GetCPMethodModifiers(JNIEnv *env, jclass cls, int cp_index, jclass called_cls))
2812 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls));
2813 Klass* k_called = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(called_cls));
2814 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
2815 k_called = JvmtiThreadState::class_to_verify_considering_redefinition(k_called, thread);
2816 ConstantPool* cp = InstanceKlass::cast(k)->constants();
2817 switch (cp->tag_at(cp_index).value()) {
2818 case JVM_CONSTANT_Methodref:
3458 JVM_LEAF(void*, JVM_FindLibraryEntry(void* handle, const char* name))
3459 void* find_result = os::dll_lookup(handle, name);
3460 log_info(library)("%s %s in library with handle " INTPTR_FORMAT,
3461 find_result != nullptr ? "Found" : "Failed to find",
3462 name, p2i(handle));
3463 return find_result;
3464 JVM_END
3465
3466
3467 // JNI version ///////////////////////////////////////////////////////////////////////////////
3468
3469 JVM_LEAF(jboolean, JVM_IsSupportedJNIVersion(jint version))
3470 return Threads::is_supported_jni_version_including_1_1(version);
3471 JVM_END
3472
3473
3474 JVM_LEAF(jboolean, JVM_IsPreviewEnabled(void))
3475 return Arguments::enable_preview() ? JNI_TRUE : JNI_FALSE;
3476 JVM_END
3477
3478 JVM_LEAF(jboolean, JVM_IsContinuationsSupported(void))
3479 return VMContinuations ? JNI_TRUE : JNI_FALSE;
3480 JVM_END
3481
3482 JVM_LEAF(jboolean, JVM_IsForeignLinkerSupported(void))
3483 return ForeignGlobals::is_foreign_linker_supported() ? JNI_TRUE : JNI_FALSE;
3484 JVM_END
3485
3486 // String support ///////////////////////////////////////////////////////////////////////////
3487
3488 JVM_ENTRY(jstring, JVM_InternString(JNIEnv *env, jstring str))
3489 JvmtiVMObjectAllocEventCollector oam;
3490 if (str == nullptr) return nullptr;
3491 oop string = JNIHandles::resolve_non_null(str);
3492 oop result = StringTable::intern(string, CHECK_NULL);
3493 return (jstring) JNIHandles::make_local(THREAD, result);
3494 JVM_END
3495
3496
3497 // VM Raw monitor support //////////////////////////////////////////////////////////////////////
3539 // the checkPackageAccess relative to the initiating class loader via the
3540 // protection_domain. The protection_domain is passed as null by the java code
3541 // if there is no security manager in 3-arg Class.forName().
3542 Klass* klass = SystemDictionary::resolve_or_fail(name, loader, protection_domain, throwError != 0, CHECK_NULL);
3543
3544 // Check if we should initialize the class
3545 if (init && klass->is_instance_klass()) {
3546 klass->initialize(CHECK_NULL);
3547 }
3548 return (jclass) JNIHandles::make_local(THREAD, klass->java_mirror());
3549 }
3550
3551
3552 // Method ///////////////////////////////////////////////////////////////////////////////////////////
3553
3554 JVM_ENTRY(jobject, JVM_InvokeMethod(JNIEnv *env, jobject method, jobject obj, jobjectArray args0))
3555 Handle method_handle;
3556 if (thread->stack_overflow_state()->stack_available((address) &method_handle) >= JVMInvokeMethodSlack) {
3557 method_handle = Handle(THREAD, JNIHandles::resolve(method));
3558 Handle receiver(THREAD, JNIHandles::resolve(obj));
3559 objArrayHandle args(THREAD, objArrayOop(JNIHandles::resolve(args0)));
3560 oop result = Reflection::invoke_method(method_handle(), receiver, args, CHECK_NULL);
3561 jobject res = JNIHandles::make_local(THREAD, result);
3562 if (JvmtiExport::should_post_vm_object_alloc()) {
3563 oop ret_type = java_lang_reflect_Method::return_type(method_handle());
3564 assert(ret_type != nullptr, "sanity check: ret_type oop must not be null!");
3565 if (java_lang_Class::is_primitive(ret_type)) {
3566 // Only for primitive type vm allocates memory for java object.
3567 // See box() method.
3568 JvmtiExport::post_vm_object_alloc(thread, result);
3569 }
3570 }
3571 return res;
3572 } else {
3573 THROW_0(vmSymbols::java_lang_StackOverflowError());
3574 }
3575 JVM_END
3576
3577
3578 JVM_ENTRY(jobject, JVM_NewInstanceFromConstructor(JNIEnv *env, jobject c, jobjectArray args0))
3579 oop constructor_mirror = JNIHandles::resolve(c);
3580 objArrayHandle args(THREAD, objArrayOop(JNIHandles::resolve(args0)));
3581 oop result = Reflection::invoke_constructor(constructor_mirror, args, CHECK_NULL);
3582 jobject res = JNIHandles::make_local(THREAD, result);
3583 if (JvmtiExport::should_post_vm_object_alloc()) {
3584 JvmtiExport::post_vm_object_alloc(thread, result);
3585 }
3586 return res;
3587 JVM_END
3588
3589 JVM_ENTRY(void, JVM_InitializeFromArchive(JNIEnv* env, jclass cls))
3590 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve(cls));
3591 assert(k->is_klass(), "just checking");
3592 HeapShared::initialize_from_archived_subgraph(THREAD, k);
3593 JVM_END
3594
3595 JVM_ENTRY(void, JVM_RegisterLambdaProxyClassForArchiving(JNIEnv* env,
3596 jclass caller,
3597 jstring interfaceMethodName,
3598 jobject factoryType,
3599 jobject interfaceMethodType,
3600 jobject implementationMember,
|
40 #include "classfile/modules.hpp"
41 #include "classfile/packageEntry.hpp"
42 #include "classfile/stringTable.hpp"
43 #include "classfile/symbolTable.hpp"
44 #include "classfile/systemDictionary.hpp"
45 #include "classfile/vmClasses.hpp"
46 #include "classfile/vmSymbols.hpp"
47 #include "gc/shared/collectedHeap.inline.hpp"
48 #include "interpreter/bytecode.hpp"
49 #include "interpreter/bytecodeUtils.hpp"
50 #include "jfr/jfrEvents.hpp"
51 #include "jvm.h"
52 #include "logging/log.hpp"
53 #include "memory/oopFactory.hpp"
54 #include "memory/referenceType.hpp"
55 #include "memory/resourceArea.hpp"
56 #include "memory/universe.hpp"
57 #include "oops/access.inline.hpp"
58 #include "oops/constantPool.hpp"
59 #include "oops/fieldStreams.inline.hpp"
60 #include "oops/flatArrayKlass.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"
408
409 return (jobjectArray) JNIHandles::make_local(THREAD, result_h());
410 JVM_END
411
412
413 /*
414 * Return the temporary directory that the VM uses for the attach
415 * and perf data files.
416 *
417 * It is important that this directory is well-known and the
418 * same for all VM instances. It cannot be affected by configuration
419 * variables such as java.io.tmpdir.
420 */
421 JVM_ENTRY(jstring, JVM_GetTemporaryDirectory(JNIEnv *env))
422 HandleMark hm(THREAD);
423 const char* temp_dir = os::get_temp_directory();
424 Handle h = java_lang_String::create_from_platform_dependent_str(temp_dir, CHECK_NULL);
425 return (jstring) JNIHandles::make_local(THREAD, h());
426 JVM_END
427
428 JVM_ENTRY(jarray, JVM_NewNullRestrictedArray(JNIEnv *env, jclass elmClass, jint len))
429 if (len < 0) {
430 THROW_MSG_NULL(vmSymbols::java_lang_IllegalArgumentException(), "Array length is negative");
431 }
432 oop mirror = JNIHandles::resolve_non_null(elmClass);
433 Klass* klass = java_lang_Class::as_Klass(mirror);
434 klass->initialize(CHECK_NULL);
435 if (klass->is_identity_class()) {
436 THROW_MSG_NULL(vmSymbols::java_lang_IllegalArgumentException(), "Element class is not a value class");
437 }
438 InstanceKlass* ik = InstanceKlass::cast(klass);
439 if (!ik->is_implicitly_constructible()) {
440 THROW_MSG_NULL(vmSymbols::java_lang_IllegalArgumentException(), "Element class is not implicitly constructible");
441 }
442 oop array = oopFactory::new_valueArray(ik, len, CHECK_NULL);
443 return (jarray) JNIHandles::make_local(THREAD, array);
444 JVM_END
445
446
447 JVM_ENTRY(jboolean, JVM_IsNullRestrictedArray(JNIEnv *env, jobject obj))
448 arrayOop oop = arrayOop(JNIHandles::resolve_non_null(obj));
449 return oop->is_null_free_array();
450 JVM_END
451
452 // java.lang.Runtime /////////////////////////////////////////////////////////////////////////
453
454 extern volatile jint vm_created;
455
456 JVM_ENTRY_NO_ENV(void, JVM_BeforeHalt())
457 EventShutdown event;
458 if (event.should_commit()) {
459 event.set_reason("Shutdown requested from Java");
460 event.commit();
461 }
462 JVM_END
463
464
465 JVM_ENTRY_NO_ENV(void, JVM_Halt(jint code))
466 before_exit(thread, true);
467 vm_exit(code);
468 JVM_END
469
470
630
631 Handle stackStream_h(THREAD, JNIHandles::resolve_non_null(stackStream));
632 return StackWalk::fetchNextBatch(stackStream_h, mode, anchor, last_batch_count, buffer_size,
633 start_index, frames_array_h, THREAD);
634 JVM_END
635
636 JVM_ENTRY(void, JVM_SetStackWalkContinuation(JNIEnv *env, jobject stackStream, jlong anchor, jobjectArray frames, jobject cont))
637 objArrayOop fa = objArrayOop(JNIHandles::resolve_non_null(frames));
638 objArrayHandle frames_array_h(THREAD, fa);
639 Handle stackStream_h(THREAD, JNIHandles::resolve_non_null(stackStream));
640 Handle cont_h(THREAD, JNIHandles::resolve_non_null(cont));
641
642 StackWalk::setContinuation(stackStream_h, anchor, frames_array_h, cont_h, THREAD);
643 JVM_END
644
645 // java.lang.Object ///////////////////////////////////////////////
646
647
648 JVM_ENTRY(jint, JVM_IHashCode(JNIEnv* env, jobject handle))
649 // as implemented in the classic virtual machine; return 0 if object is null
650 if (handle == nullptr) {
651 return 0;
652 }
653 oop obj = JNIHandles::resolve_non_null(handle);
654 if (EnableValhalla && obj->klass()->is_inline_klass()) {
655 JavaValue result(T_INT);
656 JavaCallArguments args;
657 Handle ho(THREAD, obj);
658 args.push_oop(ho);
659 methodHandle method(THREAD, Universe::value_object_hash_code_method());
660 JavaCalls::call(&result, method, &args, THREAD);
661 if (HAS_PENDING_EXCEPTION) {
662 if (!PENDING_EXCEPTION->is_a(vmClasses::Error_klass())) {
663 Handle e(THREAD, PENDING_EXCEPTION);
664 CLEAR_PENDING_EXCEPTION;
665 THROW_MSG_CAUSE_(vmSymbols::java_lang_InternalError(), "Internal error in hashCode", e, false);
666 }
667 }
668 return result.get_jint();
669 } else {
670 return checked_cast<jint>(ObjectSynchronizer::FastHashCode(THREAD, obj));
671 }
672 JVM_END
673
674
675 JVM_ENTRY(void, JVM_MonitorWait(JNIEnv* env, jobject handle, jlong ms))
676 Handle obj(THREAD, JNIHandles::resolve_non_null(handle));
677 JavaThreadInObjectWaitState jtiows(thread, ms != 0);
678 if (JvmtiExport::should_post_monitor_wait()) {
679 JvmtiExport::post_monitor_wait(thread, obj(), ms);
680
681 // The current thread already owns the monitor and it has not yet
682 // been added to the wait queue so the current thread cannot be
683 // made the successor. This means that the JVMTI_EVENT_MONITOR_WAIT
684 // event handler cannot accidentally consume an unpark() meant for
685 // the ParkEvent associated with this ObjectMonitor.
686 }
687 ObjectSynchronizer::wait(obj, ms, CHECK);
688 JVM_END
689
690
691 JVM_ENTRY(void, JVM_MonitorNotify(JNIEnv* env, jobject handle))
709 // Just checking that the cloneable flag is set correct
710 if (obj->is_array()) {
711 guarantee(klass->is_cloneable(), "all arrays are cloneable");
712 } else {
713 guarantee(obj->is_instance(), "should be instanceOop");
714 bool cloneable = klass->is_subtype_of(vmClasses::Cloneable_klass());
715 guarantee(cloneable == klass->is_cloneable(), "incorrect cloneable flag");
716 }
717 #endif
718
719 // Check if class of obj supports the Cloneable interface.
720 // All arrays are considered to be cloneable (See JLS 20.1.5).
721 // All j.l.r.Reference classes are considered non-cloneable.
722 if (!klass->is_cloneable() ||
723 (klass->is_instance_klass() &&
724 InstanceKlass::cast(klass)->reference_type() != REF_NONE)) {
725 ResourceMark rm(THREAD);
726 THROW_MSG_0(vmSymbols::java_lang_CloneNotSupportedException(), klass->external_name());
727 }
728
729 if (klass->is_inline_klass()) {
730 // Value instances have no identity, so return the current instance instead of allocating a new one
731 // Value classes cannot have finalizers, so the method can return immediately
732 return JNIHandles::make_local(THREAD, obj());
733 }
734
735 // Make shallow object copy
736 const size_t size = obj->size();
737 oop new_obj_oop = nullptr;
738 if (obj->is_array()) {
739 const int length = ((arrayOop)obj())->length();
740 new_obj_oop = Universe::heap()->array_allocate(klass, size, length,
741 /* do_zero */ true, CHECK_NULL);
742 } else {
743 new_obj_oop = Universe::heap()->obj_allocate(klass, size, CHECK_NULL);
744 }
745
746 HeapAccess<>::clone(obj(), new_obj_oop, size);
747
748 Handle new_obj(THREAD, new_obj_oop);
749 // Caution: this involves a java upcall, so the clone should be
750 // "gc-robust" by this stage.
751 if (klass->has_finalizer()) {
752 assert(obj->is_instance(), "should be instanceOop");
753 new_obj_oop = InstanceKlass::register_finalizer(instanceOop(new_obj()), CHECK_NULL);
754 new_obj = Handle(THREAD, new_obj_oop);
1225 oop result = java_lang_Class::name(java_class, CHECK_NULL);
1226 return (jstring) JNIHandles::make_local(THREAD, result);
1227 JVM_END
1228
1229
1230 JVM_ENTRY(jobjectArray, JVM_GetClassInterfaces(JNIEnv *env, jclass cls))
1231 JvmtiVMObjectAllocEventCollector oam;
1232 oop mirror = JNIHandles::resolve_non_null(cls);
1233
1234 // Special handling for primitive objects
1235 if (java_lang_Class::is_primitive(mirror)) {
1236 // Primitive objects does not have any interfaces
1237 objArrayOop r = oopFactory::new_objArray(vmClasses::Class_klass(), 0, CHECK_NULL);
1238 return (jobjectArray) JNIHandles::make_local(THREAD, r);
1239 }
1240
1241 Klass* klass = java_lang_Class::as_Klass(mirror);
1242 // Figure size of result array
1243 int size;
1244 if (klass->is_instance_klass()) {
1245 InstanceKlass* ik = InstanceKlass::cast(klass);
1246 size = ik->local_interfaces()->length();
1247 } else {
1248 assert(klass->is_objArray_klass() || klass->is_typeArray_klass() || klass->is_flatArray_klass(), "Illegal mirror klass");
1249 size = 2;
1250 }
1251
1252 // Allocate result array
1253 objArrayOop r = oopFactory::new_objArray(vmClasses::Class_klass(), size, CHECK_NULL);
1254 objArrayHandle result (THREAD, r);
1255 // Fill in result
1256 if (klass->is_instance_klass()) {
1257 // Regular instance klass, fill in all local interfaces
1258 for (int index = 0; index < size; index++) {
1259 InstanceKlass* ik = InstanceKlass::cast(klass);
1260 Klass* k = ik->local_interfaces()->at(index);
1261 result->obj_at_put(index, k->java_mirror());
1262 }
1263 } else {
1264 // All arrays implement java.lang.Cloneable and java.io.Serializable
1265 result->obj_at_put(0, vmClasses::Cloneable_klass()->java_mirror());
1266 result->obj_at_put(1, vmClasses::Serializable_klass()->java_mirror());
1267 }
1268 return (jobjectArray) JNIHandles::make_local(THREAD, result());
1269 JVM_END
1270
1271
1272 JVM_ENTRY(jboolean, JVM_IsInterface(JNIEnv *env, jclass cls))
1273 oop mirror = JNIHandles::resolve_non_null(cls);
1274 if (java_lang_Class::is_primitive(mirror)) {
1275 return JNI_FALSE;
1276 }
1277 Klass* k = java_lang_Class::as_Klass(mirror);
1278 jboolean result = k->is_interface();
1279 assert(!result || k->is_instance_klass(),
1280 "all interfaces are instance types");
1281 // The compiler intrinsic for isInterface tests the
1282 // Klass::_access_flags bits in the same way.
1283 return result;
1284 JVM_END
1285
1286 JVM_ENTRY(jboolean, JVM_IsHiddenClass(JNIEnv *env, jclass cls))
1287 oop mirror = JNIHandles::resolve_non_null(cls);
1288 if (java_lang_Class::is_primitive(mirror)) {
1289 return JNI_FALSE;
1290 }
1291 Klass* k = java_lang_Class::as_Klass(mirror);
1292 return k->is_hidden();
1293 JVM_END
1294
1295 JVM_ENTRY(jboolean, JVM_IsIdentityClass(JNIEnv *env, jclass cls))
1296 oop mirror = JNIHandles::resolve_non_null(cls);
1297 if (java_lang_Class::is_primitive(mirror)) {
1298 return JNI_FALSE;
1299 }
1300 Klass* k = java_lang_Class::as_Klass(mirror);
1301 if (EnableValhalla) {
1302 return k->is_array_klass() || k->is_identity_class();
1303 } else {
1304 return k->is_interface() ? JNI_FALSE : JNI_TRUE;
1305 }
1306 JVM_END
1307
1308 JVM_ENTRY(jboolean, JVM_IsImplicitlyConstructibleClass(JNIEnv *env, jclass cls))
1309 oop mirror = JNIHandles::resolve_non_null(cls);
1310 InstanceKlass* ik = InstanceKlass::cast(java_lang_Class::as_Klass(mirror));
1311 return ik->is_implicitly_constructible();
1312 JVM_END
1313
1314 JVM_ENTRY(jobjectArray, JVM_GetClassSigners(JNIEnv *env, jclass cls))
1315 JvmtiVMObjectAllocEventCollector oam;
1316 oop mirror = JNIHandles::resolve_non_null(cls);
1317 if (java_lang_Class::is_primitive(mirror)) {
1318 // There are no signers for primitive types
1319 return nullptr;
1320 }
1321
1322 objArrayHandle signers(THREAD, java_lang_Class::signers(mirror));
1323
1324 // If there are no signers set in the class, or if the class
1325 // is an array, return null.
1326 if (signers == nullptr) return nullptr;
1327
1328 // copy of the signers array
1329 Klass* element = ObjArrayKlass::cast(signers->klass())->element_klass();
1330 objArrayOop signers_copy = oopFactory::new_objArray(element, signers->length(), CHECK_NULL);
1331 for (int index = 0; index < signers->length(); index++) {
1332 signers_copy->obj_at_put(index, signers->obj_at(index));
1333 }
1908 int length = components->length();
1909 assert(length >= 0, "unexpected record_components length");
1910 objArrayOop record_components =
1911 oopFactory::new_objArray(vmClasses::RecordComponent_klass(), length, CHECK_NULL);
1912 objArrayHandle components_h (THREAD, record_components);
1913
1914 for (int x = 0; x < length; x++) {
1915 RecordComponent* component = components->at(x);
1916 assert(component != nullptr, "unexpected null record component");
1917 oop component_oop = java_lang_reflect_RecordComponent::create(ik, component, CHECK_NULL);
1918 components_h->obj_at_put(x, component_oop);
1919 }
1920 return (jobjectArray)JNIHandles::make_local(THREAD, components_h());
1921 }
1922
1923 return nullptr;
1924 }
1925 JVM_END
1926
1927 static bool select_method(const methodHandle& method, bool want_constructor) {
1928 bool is_ctor = (method->is_object_constructor());
1929 if (want_constructor) {
1930 return is_ctor;
1931 } else {
1932 return (!is_ctor &&
1933 !method->is_class_initializer() &&
1934 !method->is_overpass());
1935 }
1936 }
1937
1938 static jobjectArray get_class_declared_methods_helper(
1939 JNIEnv *env,
1940 jclass ofClass, jboolean publicOnly,
1941 bool want_constructor,
1942 Klass* klass, TRAPS) {
1943
1944 JvmtiVMObjectAllocEventCollector oam;
1945
1946 oop ofMirror = JNIHandles::resolve_non_null(ofClass);
1947 // Exclude primitive types and array types
1948 if (java_lang_Class::is_primitive(ofMirror)
1949 || java_lang_Class::as_Klass(ofMirror)->is_array_klass()) {
1950 // Return empty array
1951 oop res = oopFactory::new_objArray(klass, 0, CHECK_NULL);
1952 return (jobjectArray) JNIHandles::make_local(THREAD, res);
1953 }
1954
1977 }
1978 }
1979
1980 // Allocate result
1981 objArrayOop r = oopFactory::new_objArray(klass, num_methods, CHECK_NULL);
1982 objArrayHandle result (THREAD, r);
1983
1984 // Now just put the methods that we selected above, but go by their idnum
1985 // in case of redefinition. The methods can be redefined at any safepoint,
1986 // so above when allocating the oop array and below when creating reflect
1987 // objects.
1988 for (int i = 0; i < num_methods; i++) {
1989 methodHandle method(THREAD, k->method_with_idnum(idnums->at(i)));
1990 if (method.is_null()) {
1991 // Method may have been deleted and seems this API can handle null
1992 // Otherwise should probably put a method that throws NSME
1993 result->obj_at_put(i, nullptr);
1994 } else {
1995 oop m;
1996 if (want_constructor) {
1997 assert(method->is_object_constructor(), "must be");
1998 m = Reflection::new_constructor(method, CHECK_NULL);
1999 } else {
2000 m = Reflection::new_method(method, false, CHECK_NULL);
2001 }
2002 result->obj_at_put(i, m);
2003 }
2004 }
2005
2006 return (jobjectArray) JNIHandles::make_local(THREAD, result());
2007 }
2008
2009 JVM_ENTRY(jobjectArray, JVM_GetClassDeclaredMethods(JNIEnv *env, jclass ofClass, jboolean publicOnly))
2010 {
2011 return get_class_declared_methods_helper(env, ofClass, publicOnly,
2012 /*want_constructor*/ false,
2013 vmClasses::reflect_Method_klass(), THREAD);
2014 }
2015 JVM_END
2016
2017 JVM_ENTRY(jobjectArray, JVM_GetClassDeclaredConstructors(JNIEnv *env, jclass ofClass, jboolean publicOnly))
2260 constantTag tag = cp->tag_at(index);
2261 if (!tag.is_method() && !tag.is_interface_method()) {
2262 THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(), "Wrong type at constant pool index");
2263 }
2264 int klass_ref = cp->uncached_klass_ref_index_at(index);
2265 Klass* k_o;
2266 if (force_resolution) {
2267 k_o = cp->klass_at(klass_ref, CHECK_NULL);
2268 } else {
2269 k_o = ConstantPool::klass_at_if_loaded(cp, klass_ref);
2270 if (k_o == nullptr) return nullptr;
2271 }
2272 InstanceKlass* k = InstanceKlass::cast(k_o);
2273 Symbol* name = cp->uncached_name_ref_at(index);
2274 Symbol* sig = cp->uncached_signature_ref_at(index);
2275 methodHandle m (THREAD, k->find_method(name, sig));
2276 if (m.is_null()) {
2277 THROW_MSG_0(vmSymbols::java_lang_RuntimeException(), "Unable to look up method in target class");
2278 }
2279 oop method;
2280 if (m->is_object_constructor()) {
2281 method = Reflection::new_constructor(m, CHECK_NULL);
2282 } else {
2283 method = Reflection::new_method(m, true, CHECK_NULL);
2284 }
2285 return JNIHandles::make_local(THREAD, method);
2286 }
2287
2288 JVM_ENTRY(jobject, JVM_ConstantPoolGetMethodAt(JNIEnv *env, jobject obj, jobject unused, jint index))
2289 {
2290 JvmtiVMObjectAllocEventCollector oam;
2291 constantPoolHandle cp = constantPoolHandle(THREAD, reflect_ConstantPool::get_cp(JNIHandles::resolve_non_null(obj)));
2292 bounds_check(cp, index, CHECK_NULL);
2293 jobject res = get_method_at_helper(cp, index, true, CHECK_NULL);
2294 return res;
2295 }
2296 JVM_END
2297
2298 JVM_ENTRY(jobject, JVM_ConstantPoolGetMethodAtIfLoaded(JNIEnv *env, jobject obj, jobject unused, jint index))
2299 {
2300 JvmtiVMObjectAllocEventCollector oam;
2301 constantPoolHandle cp = constantPoolHandle(THREAD, reflect_ConstantPool::get_cp(JNIHandles::resolve_non_null(obj)));
2302 bounds_check(cp, index, CHECK_NULL);
2303 jobject res = get_method_at_helper(cp, index, false, CHECK_NULL);
2534 Klass* k = java_lang_Class::as_Klass(r);
2535 assert(k->is_instance_klass(), "must be an instance klass");
2536 if (!k->is_instance_klass()) return false;
2537
2538 ResourceMark rm(THREAD);
2539 const char* name = k->name()->as_C_string();
2540 bool system_class = k->class_loader() == nullptr;
2541 return JavaAssertions::enabled(name, system_class);
2542
2543 JVM_END
2544
2545
2546 // Return a new AssertionStatusDirectives object with the fields filled in with
2547 // command-line assertion arguments (i.e., -ea, -da).
2548 JVM_ENTRY(jobject, JVM_AssertionStatusDirectives(JNIEnv *env, jclass unused))
2549 JvmtiVMObjectAllocEventCollector oam;
2550 oop asd = JavaAssertions::createAssertionStatusDirectives(CHECK_NULL);
2551 return JNIHandles::make_local(THREAD, asd);
2552 JVM_END
2553
2554 // Arrays support /////////////////////////////////////////////////////////////
2555
2556 JVM_ENTRY(jboolean, JVM_ArrayIsAccessAtomic(JNIEnv *env, jclass unused, jobject array))
2557 oop o = JNIHandles::resolve(array);
2558 Klass* k = o->klass();
2559 if ((o == nullptr) || (!k->is_array_klass())) {
2560 THROW_0(vmSymbols::java_lang_IllegalArgumentException());
2561 }
2562 return ArrayKlass::cast(k)->element_access_is_atomic();
2563 JVM_END
2564
2565 JVM_ENTRY(jobject, JVM_ArrayEnsureAccessAtomic(JNIEnv *env, jclass unused, jobject array))
2566 oop o = JNIHandles::resolve(array);
2567 Klass* k = o->klass();
2568 if ((o == nullptr) || (!k->is_array_klass())) {
2569 THROW_0(vmSymbols::java_lang_IllegalArgumentException());
2570 }
2571 if (k->is_flatArray_klass()) {
2572 FlatArrayKlass* vk = FlatArrayKlass::cast(k);
2573 if (!vk->element_access_is_atomic()) {
2574 /**
2575 * Need to decide how to implement:
2576 *
2577 * 1) Change to objArrayOop layout, therefore oop->klass() differs so
2578 * then "<atomic>[Qfoo;" klass needs to subclass "[Qfoo;" to pass through
2579 * "checkcast" & "instanceof"
2580 *
2581 * 2) Use extra header in the flatArrayOop to flag atomicity required and
2582 * possibly per instance lock structure. Said info, could be placed in
2583 * "trailer" rather than disturb the current arrayOop
2584 */
2585 Unimplemented();
2586 }
2587 }
2588 return array;
2589 JVM_END
2590
2591 // Verification ////////////////////////////////////////////////////////////////////////////////
2592
2593 // Reflection for the verifier /////////////////////////////////////////////////////////////////
2594
2595 // RedefineClasses support: bug 6214132 caused verification to fail.
2596 // All functions from this section should call the jvmtiThreadSate function:
2597 // Klass* class_to_verify_considering_redefinition(Klass* klass).
2598 // The function returns a Klass* of the _scratch_class if the verifier
2599 // was invoked in the middle of the class redefinition.
2600 // Otherwise it returns its argument value which is the _the_class Klass*.
2601 // Please, refer to the description in the jvmtiThreadSate.hpp.
2602
2603 JVM_ENTRY(const char*, JVM_GetClassNameUTF(JNIEnv *env, jclass cls))
2604 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls));
2605 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
2606 return k->name()->as_utf8();
2607 JVM_END
2608
2609
2610 JVM_ENTRY(void, JVM_GetClassCPTypes(JNIEnv *env, jclass cls, unsigned char *types))
2700
2701 JVM_ENTRY(jint, JVM_GetMethodIxExceptionTableLength(JNIEnv *env, jclass cls, int method_index))
2702 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls));
2703 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
2704 Method* method = InstanceKlass::cast(k)->methods()->at(method_index);
2705 return method->exception_table_length();
2706 JVM_END
2707
2708
2709 JVM_ENTRY(jint, JVM_GetMethodIxModifiers(JNIEnv *env, jclass cls, int method_index))
2710 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls));
2711 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
2712 Method* method = InstanceKlass::cast(k)->methods()->at(method_index);
2713 return method->access_flags().as_int() & JVM_RECOGNIZED_METHOD_MODIFIERS;
2714 JVM_END
2715
2716
2717 JVM_ENTRY(jint, JVM_GetFieldIxModifiers(JNIEnv *env, jclass cls, int field_index))
2718 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls));
2719 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
2720 return InstanceKlass::cast(k)->field_access_flags(field_index);
2721 JVM_END
2722
2723
2724 JVM_ENTRY(jint, JVM_GetMethodIxLocalsCount(JNIEnv *env, jclass cls, int method_index))
2725 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls));
2726 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
2727 Method* method = InstanceKlass::cast(k)->methods()->at(method_index);
2728 return method->max_locals();
2729 JVM_END
2730
2731
2732 JVM_ENTRY(jint, JVM_GetMethodIxArgsSize(JNIEnv *env, jclass cls, int method_index))
2733 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls));
2734 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
2735 Method* method = InstanceKlass::cast(k)->methods()->at(method_index);
2736 return method->size_of_parameters();
2737 JVM_END
2738
2739
2740 JVM_ENTRY(jint, JVM_GetMethodIxMaxStack(JNIEnv *env, jclass cls, int method_index))
2741 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls));
2742 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
2743 Method* method = InstanceKlass::cast(k)->methods()->at(method_index);
2744 return method->verifier_max_stack();
2745 JVM_END
2746
2747
2748 JVM_ENTRY(jboolean, JVM_IsConstructorIx(JNIEnv *env, jclass cls, int method_index))
2749 ResourceMark rm(THREAD);
2750 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls));
2751 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
2752 Method* method = InstanceKlass::cast(k)->methods()->at(method_index);
2753 return method->is_object_constructor();
2754 JVM_END
2755
2756
2757 JVM_ENTRY(jboolean, JVM_IsVMGeneratedMethodIx(JNIEnv *env, jclass cls, int method_index))
2758 ResourceMark rm(THREAD);
2759 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls));
2760 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
2761 Method* method = InstanceKlass::cast(k)->methods()->at(method_index);
2762 return method->is_overpass();
2763 JVM_END
2764
2765 JVM_ENTRY(const char*, JVM_GetMethodIxNameUTF(JNIEnv *env, jclass cls, jint method_index))
2766 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls));
2767 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
2768 Method* method = InstanceKlass::cast(k)->methods()->at(method_index);
2769 return method->name()->as_utf8();
2770 JVM_END
2771
2772
2773 JVM_ENTRY(const char*, JVM_GetMethodIxSignatureUTF(JNIEnv *env, jclass cls, jint method_index))
2890 }
2891 ShouldNotReachHere();
2892 return nullptr;
2893 JVM_END
2894
2895
2896 JVM_ENTRY(jint, JVM_GetCPFieldModifiers(JNIEnv *env, jclass cls, int cp_index, jclass called_cls))
2897 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls));
2898 Klass* k_called = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(called_cls));
2899 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
2900 k_called = JvmtiThreadState::class_to_verify_considering_redefinition(k_called, thread);
2901 ConstantPool* cp = InstanceKlass::cast(k)->constants();
2902 ConstantPool* cp_called = InstanceKlass::cast(k_called)->constants();
2903 switch (cp->tag_at(cp_index).value()) {
2904 case JVM_CONSTANT_Fieldref: {
2905 Symbol* name = cp->uncached_name_ref_at(cp_index);
2906 Symbol* signature = cp->uncached_signature_ref_at(cp_index);
2907 InstanceKlass* ik = InstanceKlass::cast(k_called);
2908 for (JavaFieldStream fs(ik); !fs.done(); fs.next()) {
2909 if (fs.name() == name && fs.signature() == signature) {
2910 return fs.access_flags().as_short();
2911 }
2912 }
2913 return -1;
2914 }
2915 default:
2916 fatal("JVM_GetCPFieldModifiers: illegal constant");
2917 }
2918 ShouldNotReachHere();
2919 return 0;
2920 JVM_END
2921
2922
2923 JVM_ENTRY(jint, JVM_GetCPMethodModifiers(JNIEnv *env, jclass cls, int cp_index, jclass called_cls))
2924 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls));
2925 Klass* k_called = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(called_cls));
2926 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
2927 k_called = JvmtiThreadState::class_to_verify_considering_redefinition(k_called, thread);
2928 ConstantPool* cp = InstanceKlass::cast(k)->constants();
2929 switch (cp->tag_at(cp_index).value()) {
2930 case JVM_CONSTANT_Methodref:
3570 JVM_LEAF(void*, JVM_FindLibraryEntry(void* handle, const char* name))
3571 void* find_result = os::dll_lookup(handle, name);
3572 log_info(library)("%s %s in library with handle " INTPTR_FORMAT,
3573 find_result != nullptr ? "Found" : "Failed to find",
3574 name, p2i(handle));
3575 return find_result;
3576 JVM_END
3577
3578
3579 // JNI version ///////////////////////////////////////////////////////////////////////////////
3580
3581 JVM_LEAF(jboolean, JVM_IsSupportedJNIVersion(jint version))
3582 return Threads::is_supported_jni_version_including_1_1(version);
3583 JVM_END
3584
3585
3586 JVM_LEAF(jboolean, JVM_IsPreviewEnabled(void))
3587 return Arguments::enable_preview() ? JNI_TRUE : JNI_FALSE;
3588 JVM_END
3589
3590 JVM_LEAF(jboolean, JVM_IsValhallaEnabled(void))
3591 return EnableValhalla ? JNI_TRUE : JNI_FALSE;
3592 JVM_END
3593
3594 JVM_LEAF(jboolean, JVM_IsContinuationsSupported(void))
3595 return VMContinuations ? JNI_TRUE : JNI_FALSE;
3596 JVM_END
3597
3598 JVM_LEAF(jboolean, JVM_IsForeignLinkerSupported(void))
3599 return ForeignGlobals::is_foreign_linker_supported() ? JNI_TRUE : JNI_FALSE;
3600 JVM_END
3601
3602 // String support ///////////////////////////////////////////////////////////////////////////
3603
3604 JVM_ENTRY(jstring, JVM_InternString(JNIEnv *env, jstring str))
3605 JvmtiVMObjectAllocEventCollector oam;
3606 if (str == nullptr) return nullptr;
3607 oop string = JNIHandles::resolve_non_null(str);
3608 oop result = StringTable::intern(string, CHECK_NULL);
3609 return (jstring) JNIHandles::make_local(THREAD, result);
3610 JVM_END
3611
3612
3613 // VM Raw monitor support //////////////////////////////////////////////////////////////////////
3655 // the checkPackageAccess relative to the initiating class loader via the
3656 // protection_domain. The protection_domain is passed as null by the java code
3657 // if there is no security manager in 3-arg Class.forName().
3658 Klass* klass = SystemDictionary::resolve_or_fail(name, loader, protection_domain, throwError != 0, CHECK_NULL);
3659
3660 // Check if we should initialize the class
3661 if (init && klass->is_instance_klass()) {
3662 klass->initialize(CHECK_NULL);
3663 }
3664 return (jclass) JNIHandles::make_local(THREAD, klass->java_mirror());
3665 }
3666
3667
3668 // Method ///////////////////////////////////////////////////////////////////////////////////////////
3669
3670 JVM_ENTRY(jobject, JVM_InvokeMethod(JNIEnv *env, jobject method, jobject obj, jobjectArray args0))
3671 Handle method_handle;
3672 if (thread->stack_overflow_state()->stack_available((address) &method_handle) >= JVMInvokeMethodSlack) {
3673 method_handle = Handle(THREAD, JNIHandles::resolve(method));
3674 Handle receiver(THREAD, JNIHandles::resolve(obj));
3675 objArrayHandle args = oopFactory::ensure_objArray(JNIHandles::resolve(args0), CHECK_NULL);
3676 oop result = Reflection::invoke_method(method_handle(), receiver, args, CHECK_NULL);
3677 jobject res = JNIHandles::make_local(THREAD, result);
3678 if (JvmtiExport::should_post_vm_object_alloc()) {
3679 oop ret_type = java_lang_reflect_Method::return_type(method_handle());
3680 assert(ret_type != nullptr, "sanity check: ret_type oop must not be null!");
3681 if (java_lang_Class::is_primitive(ret_type)) {
3682 // Only for primitive type vm allocates memory for java object.
3683 // See box() method.
3684 JvmtiExport::post_vm_object_alloc(thread, result);
3685 }
3686 }
3687 return res;
3688 } else {
3689 THROW_0(vmSymbols::java_lang_StackOverflowError());
3690 }
3691 JVM_END
3692
3693
3694 JVM_ENTRY(jobject, JVM_NewInstanceFromConstructor(JNIEnv *env, jobject c, jobjectArray args0))
3695 objArrayHandle args = oopFactory::ensure_objArray(JNIHandles::resolve(args0), CHECK_NULL);
3696 oop constructor_mirror = JNIHandles::resolve(c);
3697 oop result = Reflection::invoke_constructor(constructor_mirror, args, CHECK_NULL);
3698 jobject res = JNIHandles::make_local(THREAD, result);
3699 if (JvmtiExport::should_post_vm_object_alloc()) {
3700 JvmtiExport::post_vm_object_alloc(thread, result);
3701 }
3702 return res;
3703 JVM_END
3704
3705 JVM_ENTRY(void, JVM_InitializeFromArchive(JNIEnv* env, jclass cls))
3706 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve(cls));
3707 assert(k->is_klass(), "just checking");
3708 HeapShared::initialize_from_archived_subgraph(THREAD, k);
3709 JVM_END
3710
3711 JVM_ENTRY(void, JVM_RegisterLambdaProxyClassForArchiving(JNIEnv* env,
3712 jclass caller,
3713 jstring interfaceMethodName,
3714 jobject factoryType,
3715 jobject interfaceMethodType,
3716 jobject implementationMember,
|