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"
410
411 return (jobjectArray) JNIHandles::make_local(THREAD, result_h());
412 JVM_END
413
414
415 /*
416 * Return the temporary directory that the VM uses for the attach
417 * and perf data files.
418 *
419 * It is important that this directory is well-known and the
420 * same for all VM instances. It cannot be affected by configuration
421 * variables such as java.io.tmpdir.
422 */
423 JVM_ENTRY(jstring, JVM_GetTemporaryDirectory(JNIEnv *env))
424 HandleMark hm(THREAD);
425 const char* temp_dir = os::get_temp_directory();
426 Handle h = java_lang_String::create_from_platform_dependent_str(temp_dir, CHECK_NULL);
427 return (jstring) JNIHandles::make_local(THREAD, h());
428 JVM_END
429
430
431 // java.lang.Runtime /////////////////////////////////////////////////////////////////////////
432
433 extern volatile jint vm_created;
434
435 JVM_ENTRY_NO_ENV(void, JVM_BeforeHalt())
436 EventShutdown event;
437 if (event.should_commit()) {
438 event.set_reason("Shutdown requested from Java");
439 event.commit();
440 }
441 JVM_END
442
443
444 JVM_ENTRY_NO_ENV(void, JVM_Halt(jint code))
445 before_exit(thread, true);
446 vm_exit(code);
447 JVM_END
448
449
618
619 Handle stackStream_h(THREAD, JNIHandles::resolve_non_null(stackStream));
620 return StackWalk::fetchNextBatch(stackStream_h, mode, anchor, last_batch_count, buffer_size,
621 start_index, frames_array_h, THREAD);
622 JVM_END
623
624 JVM_ENTRY(void, JVM_SetStackWalkContinuation(JNIEnv *env, jobject stackStream, jlong anchor, jobjectArray frames, jobject cont))
625 objArrayOop fa = objArrayOop(JNIHandles::resolve_non_null(frames));
626 objArrayHandle frames_array_h(THREAD, fa);
627 Handle stackStream_h(THREAD, JNIHandles::resolve_non_null(stackStream));
628 Handle cont_h(THREAD, JNIHandles::resolve_non_null(cont));
629
630 StackWalk::setContinuation(stackStream_h, anchor, frames_array_h, cont_h, THREAD);
631 JVM_END
632
633 // java.lang.Object ///////////////////////////////////////////////
634
635
636 JVM_ENTRY(jint, JVM_IHashCode(JNIEnv* env, jobject handle))
637 // as implemented in the classic virtual machine; return 0 if object is null
638 return handle == nullptr ? 0 :
639 checked_cast<jint>(ObjectSynchronizer::FastHashCode (THREAD, JNIHandles::resolve_non_null(handle)));
640 JVM_END
641
642
643 JVM_ENTRY(void, JVM_MonitorWait(JNIEnv* env, jobject handle, jlong ms))
644 Handle obj(THREAD, JNIHandles::resolve_non_null(handle));
645 JavaThreadInObjectWaitState jtiows(thread, ms != 0);
646 if (JvmtiExport::should_post_monitor_wait()) {
647 JvmtiExport::post_monitor_wait(thread, obj(), ms);
648
649 // The current thread already owns the monitor and it has not yet
650 // been added to the wait queue so the current thread cannot be
651 // made the successor. This means that the JVMTI_EVENT_MONITOR_WAIT
652 // event handler cannot accidentally consume an unpark() meant for
653 // the ParkEvent associated with this ObjectMonitor.
654 }
655 ObjectSynchronizer::wait(obj, ms, CHECK);
656 JVM_END
657
658
659 JVM_ENTRY(void, JVM_MonitorNotify(JNIEnv* env, jobject handle))
677 // Just checking that the cloneable flag is set correct
678 if (obj->is_array()) {
679 guarantee(klass->is_cloneable(), "all arrays are cloneable");
680 } else {
681 guarantee(obj->is_instance(), "should be instanceOop");
682 bool cloneable = klass->is_subtype_of(vmClasses::Cloneable_klass());
683 guarantee(cloneable == klass->is_cloneable(), "incorrect cloneable flag");
684 }
685 #endif
686
687 // Check if class of obj supports the Cloneable interface.
688 // All arrays are considered to be cloneable (See JLS 20.1.5).
689 // All j.l.r.Reference classes are considered non-cloneable.
690 if (!klass->is_cloneable() ||
691 (klass->is_instance_klass() &&
692 InstanceKlass::cast(klass)->reference_type() != REF_NONE)) {
693 ResourceMark rm(THREAD);
694 THROW_MSG_NULL(vmSymbols::java_lang_CloneNotSupportedException(), klass->external_name());
695 }
696
697 // Make shallow object copy
698 const size_t size = obj->size();
699 oop new_obj_oop = nullptr;
700 if (obj->is_array()) {
701 const int length = ((arrayOop)obj())->length();
702 new_obj_oop = Universe::heap()->array_allocate(klass, size, length,
703 /* do_zero */ true, CHECK_NULL);
704 } else {
705 new_obj_oop = Universe::heap()->obj_allocate(klass, size, CHECK_NULL);
706 }
707
708 HeapAccess<>::clone(obj(), new_obj_oop, size);
709
710 Handle new_obj(THREAD, new_obj_oop);
711 // Caution: this involves a java upcall, so the clone should be
712 // "gc-robust" by this stage.
713 if (klass->has_finalizer()) {
714 assert(obj->is_instance(), "should be instanceOop");
715 new_obj_oop = InstanceKlass::register_finalizer(instanceOop(new_obj()), CHECK_NULL);
716 new_obj = Handle(THREAD, new_obj_oop);
1187 oop result = java_lang_Class::name(java_class, CHECK_NULL);
1188 return (jstring) JNIHandles::make_local(THREAD, result);
1189 JVM_END
1190
1191
1192 JVM_ENTRY(jobjectArray, JVM_GetClassInterfaces(JNIEnv *env, jclass cls))
1193 JvmtiVMObjectAllocEventCollector oam;
1194 oop mirror = JNIHandles::resolve_non_null(cls);
1195
1196 // Special handling for primitive objects
1197 if (java_lang_Class::is_primitive(mirror)) {
1198 // Primitive objects does not have any interfaces
1199 objArrayOop r = oopFactory::new_objArray(vmClasses::Class_klass(), 0, CHECK_NULL);
1200 return (jobjectArray) JNIHandles::make_local(THREAD, r);
1201 }
1202
1203 Klass* klass = java_lang_Class::as_Klass(mirror);
1204 // Figure size of result array
1205 int size;
1206 if (klass->is_instance_klass()) {
1207 size = InstanceKlass::cast(klass)->local_interfaces()->length();
1208 } else {
1209 assert(klass->is_objArray_klass() || klass->is_typeArray_klass(), "Illegal mirror klass");
1210 size = 2;
1211 }
1212
1213 // Allocate result array
1214 objArrayOop r = oopFactory::new_objArray(vmClasses::Class_klass(), size, CHECK_NULL);
1215 objArrayHandle result (THREAD, r);
1216 // Fill in result
1217 if (klass->is_instance_klass()) {
1218 // Regular instance klass, fill in all local interfaces
1219 for (int index = 0; index < size; index++) {
1220 Klass* k = InstanceKlass::cast(klass)->local_interfaces()->at(index);
1221 result->obj_at_put(index, k->java_mirror());
1222 }
1223 } else {
1224 // All arrays implement java.lang.Cloneable and java.io.Serializable
1225 result->obj_at_put(0, vmClasses::Cloneable_klass()->java_mirror());
1226 result->obj_at_put(1, vmClasses::Serializable_klass()->java_mirror());
1227 }
1228 return (jobjectArray) JNIHandles::make_local(THREAD, result());
1229 JVM_END
1230
1231
1232 JVM_ENTRY(jboolean, JVM_IsInterface(JNIEnv *env, jclass cls))
1233 oop mirror = JNIHandles::resolve_non_null(cls);
1234 if (java_lang_Class::is_primitive(mirror)) {
1235 return JNI_FALSE;
1236 }
1237 Klass* k = java_lang_Class::as_Klass(mirror);
1238 jboolean result = k->is_interface();
1239 assert(!result || k->is_instance_klass(),
1240 "all interfaces are instance types");
1241 // The compiler intrinsic for isInterface tests the
1242 // Klass::_access_flags bits in the same way.
1243 return result;
1244 JVM_END
1245
1246 JVM_ENTRY(jboolean, JVM_IsHiddenClass(JNIEnv *env, jclass cls))
1247 oop mirror = JNIHandles::resolve_non_null(cls);
1248 if (java_lang_Class::is_primitive(mirror)) {
1249 return JNI_FALSE;
1250 }
1251 Klass* k = java_lang_Class::as_Klass(mirror);
1252 return k->is_hidden();
1253 JVM_END
1254
1255
1256 JVM_ENTRY(jobject, JVM_GetProtectionDomain(JNIEnv *env, jclass cls))
1257 oop mirror = JNIHandles::resolve_non_null(cls);
1258 if (mirror == nullptr) {
1259 THROW_(vmSymbols::java_lang_NullPointerException(), nullptr);
1260 }
1261
1262 if (java_lang_Class::is_primitive(mirror)) {
1263 // Primitive types does not have a protection domain.
1264 return nullptr;
1265 }
1266
1267 oop pd = java_lang_Class::protection_domain(mirror);
1268 return (jobject) JNIHandles::make_local(THREAD, pd);
1269 JVM_END
1270
1271
1272 // Returns the inherited_access_control_context field of the running thread.
1273 JVM_ENTRY(jobject, JVM_GetInheritedAccessControlContext(JNIEnv *env, jclass cls))
1274 oop result = java_lang_Thread::inherited_access_control_context(thread->threadObj());
1844 }
1845
1846 InstanceKlass* k = InstanceKlass::cast(java_lang_Class::as_Klass(ofMirror));
1847
1848 // Ensure class is linked
1849 k->link_class(CHECK_NULL);
1850
1851 Array<Method*>* methods = k->methods();
1852 int methods_length = methods->length();
1853
1854 // Save original method_idnum in case of redefinition, which can change
1855 // the idnum of obsolete methods. The new method will have the same idnum
1856 // but if we refresh the methods array, the counts will be wrong.
1857 ResourceMark rm(THREAD);
1858 GrowableArray<int>* idnums = new GrowableArray<int>(methods_length);
1859 int num_methods = 0;
1860
1861 // Select methods matching the criteria.
1862 for (int i = 0; i < methods_length; i++) {
1863 Method* method = methods->at(i);
1864 if (want_constructor && !method->is_object_initializer()) {
1865 continue;
1866 }
1867 if (!want_constructor &&
1868 (method->is_object_initializer() || method->is_static_initializer() ||
1869 method->is_overpass())) {
1870 continue;
1871 }
1872 if (publicOnly && !method->is_public()) {
1873 continue;
1874 }
1875 idnums->push(method->method_idnum());
1876 ++num_methods;
1877 }
1878
1879 // Allocate result
1880 objArrayOop r = oopFactory::new_objArray(klass, num_methods, CHECK_NULL);
1881 objArrayHandle result (THREAD, r);
1882
1883 // Now just put the methods that we selected above, but go by their idnum
1884 // in case of redefinition. The methods can be redefined at any safepoint,
1885 // so above when allocating the oop array and below when creating reflect
1886 // objects.
1887 for (int i = 0; i < num_methods; i++) {
1888 methodHandle method(THREAD, k->method_with_idnum(idnums->at(i)));
1889 if (method.is_null()) {
1890 // Method may have been deleted and seems this API can handle null
1891 // Otherwise should probably put a method that throws NSME
1892 result->obj_at_put(i, nullptr);
1893 } else {
1894 oop m;
1895 if (want_constructor) {
1896 m = Reflection::new_constructor(method, CHECK_NULL);
1897 } else {
1898 m = Reflection::new_method(method, false, CHECK_NULL);
1899 }
1900 result->obj_at_put(i, m);
1901 }
1902 }
1903
1904 return (jobjectArray) JNIHandles::make_local(THREAD, result());
1905 }
1906
1907 JVM_ENTRY(jobjectArray, JVM_GetClassDeclaredMethods(JNIEnv *env, jclass ofClass, jboolean publicOnly))
1908 {
1909 return get_class_declared_methods_helper(env, ofClass, publicOnly,
1910 /*want_constructor*/ false,
1911 vmClasses::reflect_Method_klass(), THREAD);
1912 }
1913 JVM_END
1914
1915 JVM_ENTRY(jobjectArray, JVM_GetClassDeclaredConstructors(JNIEnv *env, jclass ofClass, jboolean publicOnly))
2158 constantTag tag = cp->tag_at(index);
2159 if (!tag.is_method() && !tag.is_interface_method()) {
2160 THROW_MSG_NULL(vmSymbols::java_lang_IllegalArgumentException(), "Wrong type at constant pool index");
2161 }
2162 int klass_ref = cp->uncached_klass_ref_index_at(index);
2163 Klass* k_o;
2164 if (force_resolution) {
2165 k_o = cp->klass_at(klass_ref, CHECK_NULL);
2166 } else {
2167 k_o = ConstantPool::klass_at_if_loaded(cp, klass_ref);
2168 if (k_o == nullptr) return nullptr;
2169 }
2170 InstanceKlass* k = InstanceKlass::cast(k_o);
2171 Symbol* name = cp->uncached_name_ref_at(index);
2172 Symbol* sig = cp->uncached_signature_ref_at(index);
2173 methodHandle m (THREAD, k->find_method(name, sig));
2174 if (m.is_null()) {
2175 THROW_MSG_NULL(vmSymbols::java_lang_RuntimeException(), "Unable to look up method in target class");
2176 }
2177 oop method;
2178 if (m->is_object_initializer()) {
2179 method = Reflection::new_constructor(m, CHECK_NULL);
2180 } else {
2181 // new_method accepts <clinit> as Method here
2182 method = Reflection::new_method(m, true, CHECK_NULL);
2183 }
2184 return JNIHandles::make_local(THREAD, method);
2185 }
2186
2187 JVM_ENTRY(jobject, JVM_ConstantPoolGetMethodAt(JNIEnv *env, jobject obj, jobject unused, jint index))
2188 {
2189 JvmtiVMObjectAllocEventCollector oam;
2190 constantPoolHandle cp = constantPoolHandle(THREAD, reflect_ConstantPool::get_cp(JNIHandles::resolve_non_null(obj)));
2191 bounds_check(cp, index, CHECK_NULL);
2192 jobject res = get_method_at_helper(cp, index, true, CHECK_NULL);
2193 return res;
2194 }
2195 JVM_END
2196
2197 JVM_ENTRY(jobject, JVM_ConstantPoolGetMethodAtIfLoaded(JNIEnv *env, jobject obj, jobject unused, jint index))
2198 {
2199 JvmtiVMObjectAllocEventCollector oam;
2200 constantPoolHandle cp = constantPoolHandle(THREAD, reflect_ConstantPool::get_cp(JNIHandles::resolve_non_null(obj)));
2201 bounds_check(cp, index, CHECK_NULL);
2433 Klass* k = java_lang_Class::as_Klass(r);
2434 assert(k->is_instance_klass(), "must be an instance klass");
2435 if (!k->is_instance_klass()) return false;
2436
2437 ResourceMark rm(THREAD);
2438 const char* name = k->name()->as_C_string();
2439 bool system_class = k->class_loader() == nullptr;
2440 return JavaAssertions::enabled(name, system_class);
2441
2442 JVM_END
2443
2444
2445 // Return a new AssertionStatusDirectives object with the fields filled in with
2446 // command-line assertion arguments (i.e., -ea, -da).
2447 JVM_ENTRY(jobject, JVM_AssertionStatusDirectives(JNIEnv *env, jclass unused))
2448 JvmtiVMObjectAllocEventCollector oam;
2449 oop asd = JavaAssertions::createAssertionStatusDirectives(CHECK_NULL);
2450 return JNIHandles::make_local(THREAD, asd);
2451 JVM_END
2452
2453 // Verification ////////////////////////////////////////////////////////////////////////////////
2454
2455 // Reflection for the verifier /////////////////////////////////////////////////////////////////
2456
2457 // RedefineClasses support: bug 6214132 caused verification to fail.
2458 // All functions from this section should call the jvmtiThreadSate function:
2459 // Klass* class_to_verify_considering_redefinition(Klass* klass).
2460 // The function returns a Klass* of the _scratch_class if the verifier
2461 // was invoked in the middle of the class redefinition.
2462 // Otherwise it returns its argument value which is the _the_class Klass*.
2463 // Please, refer to the description in the jvmtiThreadSate.hpp.
2464
2465 JVM_ENTRY(const char*, JVM_GetClassNameUTF(JNIEnv *env, jclass cls))
2466 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls));
2467 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
2468 return k->name()->as_utf8();
2469 JVM_END
2470
2471
2472 JVM_ENTRY(void, JVM_GetClassCPTypes(JNIEnv *env, jclass cls, unsigned char *types))
2562
2563 JVM_ENTRY(jint, JVM_GetMethodIxExceptionTableLength(JNIEnv *env, jclass cls, int method_index))
2564 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls));
2565 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
2566 Method* method = InstanceKlass::cast(k)->methods()->at(method_index);
2567 return method->exception_table_length();
2568 JVM_END
2569
2570
2571 JVM_ENTRY(jint, JVM_GetMethodIxModifiers(JNIEnv *env, jclass cls, int method_index))
2572 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls));
2573 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
2574 Method* method = InstanceKlass::cast(k)->methods()->at(method_index);
2575 return method->access_flags().as_int() & JVM_RECOGNIZED_METHOD_MODIFIERS;
2576 JVM_END
2577
2578
2579 JVM_ENTRY(jint, JVM_GetFieldIxModifiers(JNIEnv *env, jclass cls, int field_index))
2580 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls));
2581 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
2582 return InstanceKlass::cast(k)->field_access_flags(field_index) & JVM_RECOGNIZED_FIELD_MODIFIERS;
2583 JVM_END
2584
2585
2586 JVM_ENTRY(jint, JVM_GetMethodIxLocalsCount(JNIEnv *env, jclass cls, int method_index))
2587 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls));
2588 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
2589 Method* method = InstanceKlass::cast(k)->methods()->at(method_index);
2590 return method->max_locals();
2591 JVM_END
2592
2593
2594 JVM_ENTRY(jint, JVM_GetMethodIxArgsSize(JNIEnv *env, jclass cls, int method_index))
2595 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls));
2596 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
2597 Method* method = InstanceKlass::cast(k)->methods()->at(method_index);
2598 return method->size_of_parameters();
2599 JVM_END
2600
2601
2602 JVM_ENTRY(jint, JVM_GetMethodIxMaxStack(JNIEnv *env, jclass cls, int method_index))
2603 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls));
2604 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
2605 Method* method = InstanceKlass::cast(k)->methods()->at(method_index);
2606 return method->verifier_max_stack();
2607 JVM_END
2608
2609
2610 JVM_ENTRY(jboolean, JVM_IsConstructorIx(JNIEnv *env, jclass cls, int method_index))
2611 ResourceMark rm(THREAD);
2612 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls));
2613 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
2614 Method* method = InstanceKlass::cast(k)->methods()->at(method_index);
2615 return method->name() == vmSymbols::object_initializer_name();
2616 JVM_END
2617
2618
2619 JVM_ENTRY(jboolean, JVM_IsVMGeneratedMethodIx(JNIEnv *env, jclass cls, int method_index))
2620 ResourceMark rm(THREAD);
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->is_overpass();
2625 JVM_END
2626
2627 JVM_ENTRY(const char*, JVM_GetMethodIxNameUTF(JNIEnv *env, jclass cls, jint method_index))
2628 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls));
2629 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
2630 Method* method = InstanceKlass::cast(k)->methods()->at(method_index);
2631 return method->name()->as_utf8();
2632 JVM_END
2633
2634
2635 JVM_ENTRY(const char*, JVM_GetMethodIxSignatureUTF(JNIEnv *env, jclass cls, jint method_index))
2752 }
2753 ShouldNotReachHere();
2754 return nullptr;
2755 JVM_END
2756
2757
2758 JVM_ENTRY(jint, JVM_GetCPFieldModifiers(JNIEnv *env, jclass cls, int cp_index, jclass called_cls))
2759 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls));
2760 Klass* k_called = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(called_cls));
2761 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
2762 k_called = JvmtiThreadState::class_to_verify_considering_redefinition(k_called, thread);
2763 ConstantPool* cp = InstanceKlass::cast(k)->constants();
2764 ConstantPool* cp_called = InstanceKlass::cast(k_called)->constants();
2765 switch (cp->tag_at(cp_index).value()) {
2766 case JVM_CONSTANT_Fieldref: {
2767 Symbol* name = cp->uncached_name_ref_at(cp_index);
2768 Symbol* signature = cp->uncached_signature_ref_at(cp_index);
2769 InstanceKlass* ik = InstanceKlass::cast(k_called);
2770 for (JavaFieldStream fs(ik); !fs.done(); fs.next()) {
2771 if (fs.name() == name && fs.signature() == signature) {
2772 return fs.access_flags().as_short() & JVM_RECOGNIZED_FIELD_MODIFIERS;
2773 }
2774 }
2775 return -1;
2776 }
2777 default:
2778 fatal("JVM_GetCPFieldModifiers: illegal constant");
2779 }
2780 ShouldNotReachHere();
2781 return 0;
2782 JVM_END
2783
2784
2785 JVM_ENTRY(jint, JVM_GetCPMethodModifiers(JNIEnv *env, jclass cls, int cp_index, jclass called_cls))
2786 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls));
2787 Klass* k_called = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(called_cls));
2788 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
2789 k_called = JvmtiThreadState::class_to_verify_considering_redefinition(k_called, thread);
2790 ConstantPool* cp = InstanceKlass::cast(k)->constants();
2791 switch (cp->tag_at(cp_index).value()) {
2792 case JVM_CONSTANT_Methodref:
3425 JVM_LEAF(void*, JVM_FindLibraryEntry(void* handle, const char* name))
3426 void* find_result = os::dll_lookup(handle, name);
3427 log_info(library)("%s %s in library with handle " INTPTR_FORMAT,
3428 find_result != nullptr ? "Found" : "Failed to find",
3429 name, p2i(handle));
3430 return find_result;
3431 JVM_END
3432
3433
3434 // JNI version ///////////////////////////////////////////////////////////////////////////////
3435
3436 JVM_LEAF(jboolean, JVM_IsSupportedJNIVersion(jint version))
3437 return Threads::is_supported_jni_version_including_1_1(version);
3438 JVM_END
3439
3440
3441 JVM_LEAF(jboolean, JVM_IsPreviewEnabled(void))
3442 return Arguments::enable_preview() ? JNI_TRUE : JNI_FALSE;
3443 JVM_END
3444
3445 JVM_LEAF(jboolean, JVM_IsContinuationsSupported(void))
3446 return VMContinuations ? JNI_TRUE : JNI_FALSE;
3447 JVM_END
3448
3449 JVM_LEAF(jboolean, JVM_IsForeignLinkerSupported(void))
3450 return ForeignGlobals::is_foreign_linker_supported() ? JNI_TRUE : JNI_FALSE;
3451 JVM_END
3452
3453 JVM_ENTRY_NO_ENV(jboolean, JVM_IsStaticallyLinked(void))
3454 return is_vm_statically_linked() ? JNI_TRUE : JNI_FALSE;
3455 JVM_END
3456
3457 // String support ///////////////////////////////////////////////////////////////////////////
3458
3459 JVM_ENTRY(jstring, JVM_InternString(JNIEnv *env, jstring str))
3460 JvmtiVMObjectAllocEventCollector oam;
3461 if (str == nullptr) return nullptr;
3462 oop string = JNIHandles::resolve_non_null(str);
3463 oop result = StringTable::intern(string, CHECK_NULL);
3464 return (jstring) JNIHandles::make_local(THREAD, result);
3510 // the checkPackageAccess relative to the initiating class loader via the
3511 // protection_domain. The protection_domain is passed as null by the java code
3512 // if there is no security manager in 3-arg Class.forName().
3513 Klass* klass = SystemDictionary::resolve_or_fail(name, loader, protection_domain, throwError != 0, CHECK_NULL);
3514
3515 // Check if we should initialize the class
3516 if (init && klass->is_instance_klass()) {
3517 klass->initialize(CHECK_NULL);
3518 }
3519 return (jclass) JNIHandles::make_local(THREAD, klass->java_mirror());
3520 }
3521
3522
3523 // Method ///////////////////////////////////////////////////////////////////////////////////////////
3524
3525 JVM_ENTRY(jobject, JVM_InvokeMethod(JNIEnv *env, jobject method, jobject obj, jobjectArray args0))
3526 Handle method_handle;
3527 if (thread->stack_overflow_state()->stack_available((address) &method_handle) >= JVMInvokeMethodSlack) {
3528 method_handle = Handle(THREAD, JNIHandles::resolve(method));
3529 Handle receiver(THREAD, JNIHandles::resolve(obj));
3530 objArrayHandle args(THREAD, objArrayOop(JNIHandles::resolve(args0)));
3531 oop result = Reflection::invoke_method(method_handle(), receiver, args, CHECK_NULL);
3532 jobject res = JNIHandles::make_local(THREAD, result);
3533 if (JvmtiExport::should_post_vm_object_alloc()) {
3534 oop ret_type = java_lang_reflect_Method::return_type(method_handle());
3535 assert(ret_type != nullptr, "sanity check: ret_type oop must not be null!");
3536 if (java_lang_Class::is_primitive(ret_type)) {
3537 // Only for primitive type vm allocates memory for java object.
3538 // See box() method.
3539 JvmtiExport::post_vm_object_alloc(thread, result);
3540 }
3541 }
3542 return res;
3543 } else {
3544 THROW_NULL(vmSymbols::java_lang_StackOverflowError());
3545 }
3546 JVM_END
3547
3548
3549 JVM_ENTRY(jobject, JVM_NewInstanceFromConstructor(JNIEnv *env, jobject c, jobjectArray args0))
3550 oop constructor_mirror = JNIHandles::resolve(c);
3551 objArrayHandle args(THREAD, objArrayOop(JNIHandles::resolve(args0)));
3552 oop result = Reflection::invoke_constructor(constructor_mirror, args, CHECK_NULL);
3553 jobject res = JNIHandles::make_local(THREAD, result);
3554 if (JvmtiExport::should_post_vm_object_alloc()) {
3555 JvmtiExport::post_vm_object_alloc(thread, result);
3556 }
3557 return res;
3558 JVM_END
3559
3560 JVM_ENTRY(void, JVM_InitializeFromArchive(JNIEnv* env, jclass cls))
3561 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve(cls));
3562 assert(k->is_klass(), "just checking");
3563 HeapShared::initialize_from_archived_subgraph(THREAD, k);
3564 JVM_END
3565
3566 JVM_ENTRY(void, JVM_RegisterLambdaProxyClassForArchiving(JNIEnv* env,
3567 jclass caller,
3568 jstring interfaceMethodName,
3569 jobject factoryType,
3570 jobject interfaceMethodType,
3571 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"
411
412 return (jobjectArray) JNIHandles::make_local(THREAD, result_h());
413 JVM_END
414
415
416 /*
417 * Return the temporary directory that the VM uses for the attach
418 * and perf data files.
419 *
420 * It is important that this directory is well-known and the
421 * same for all VM instances. It cannot be affected by configuration
422 * variables such as java.io.tmpdir.
423 */
424 JVM_ENTRY(jstring, JVM_GetTemporaryDirectory(JNIEnv *env))
425 HandleMark hm(THREAD);
426 const char* temp_dir = os::get_temp_directory();
427 Handle h = java_lang_String::create_from_platform_dependent_str(temp_dir, CHECK_NULL);
428 return (jstring) JNIHandles::make_local(THREAD, h());
429 JVM_END
430
431 JVM_ENTRY(jarray, JVM_NewNullRestrictedArray(JNIEnv *env, jclass elmClass, jint len))
432 if (len < 0) {
433 THROW_MSG_NULL(vmSymbols::java_lang_IllegalArgumentException(), "Array length is negative");
434 }
435 oop mirror = JNIHandles::resolve_non_null(elmClass);
436 Klass* klass = java_lang_Class::as_Klass(mirror);
437 klass->initialize(CHECK_NULL);
438 if (klass->is_identity_class()) {
439 THROW_MSG_NULL(vmSymbols::java_lang_IllegalArgumentException(), "Element class is not a value class");
440 }
441 if (klass->is_abstract()) {
442 THROW_MSG_NULL(vmSymbols::java_lang_IllegalArgumentException(), "Element class is abstract");
443 }
444 InlineKlass* vk = InlineKlass::cast(klass);
445 if (!vk->is_implicitly_constructible()) {
446 THROW_MSG_NULL(vmSymbols::java_lang_IllegalArgumentException(), "Element class is not implicitly constructible");
447 }
448 oop array = oopFactory::new_valueArray(vk, len, CHECK_NULL);
449 return (jarray) JNIHandles::make_local(THREAD, array);
450 JVM_END
451
452
453 JVM_ENTRY(jboolean, JVM_IsNullRestrictedArray(JNIEnv *env, jobject obj))
454 arrayOop oop = arrayOop(JNIHandles::resolve_non_null(obj));
455 return oop->is_null_free_array();
456 JVM_END
457
458 // java.lang.Runtime /////////////////////////////////////////////////////////////////////////
459
460 extern volatile jint vm_created;
461
462 JVM_ENTRY_NO_ENV(void, JVM_BeforeHalt())
463 EventShutdown event;
464 if (event.should_commit()) {
465 event.set_reason("Shutdown requested from Java");
466 event.commit();
467 }
468 JVM_END
469
470
471 JVM_ENTRY_NO_ENV(void, JVM_Halt(jint code))
472 before_exit(thread, true);
473 vm_exit(code);
474 JVM_END
475
476
645
646 Handle stackStream_h(THREAD, JNIHandles::resolve_non_null(stackStream));
647 return StackWalk::fetchNextBatch(stackStream_h, mode, anchor, last_batch_count, buffer_size,
648 start_index, frames_array_h, THREAD);
649 JVM_END
650
651 JVM_ENTRY(void, JVM_SetStackWalkContinuation(JNIEnv *env, jobject stackStream, jlong anchor, jobjectArray frames, jobject cont))
652 objArrayOop fa = objArrayOop(JNIHandles::resolve_non_null(frames));
653 objArrayHandle frames_array_h(THREAD, fa);
654 Handle stackStream_h(THREAD, JNIHandles::resolve_non_null(stackStream));
655 Handle cont_h(THREAD, JNIHandles::resolve_non_null(cont));
656
657 StackWalk::setContinuation(stackStream_h, anchor, frames_array_h, cont_h, THREAD);
658 JVM_END
659
660 // java.lang.Object ///////////////////////////////////////////////
661
662
663 JVM_ENTRY(jint, JVM_IHashCode(JNIEnv* env, jobject handle))
664 // as implemented in the classic virtual machine; return 0 if object is null
665 if (handle == nullptr) {
666 return 0;
667 }
668 oop obj = JNIHandles::resolve_non_null(handle);
669 if (EnableValhalla && obj->klass()->is_inline_klass()) {
670 JavaValue result(T_INT);
671 JavaCallArguments args;
672 Handle ho(THREAD, obj);
673 args.push_oop(ho);
674 methodHandle method(THREAD, Universe::value_object_hash_code_method());
675 JavaCalls::call(&result, method, &args, THREAD);
676 if (HAS_PENDING_EXCEPTION) {
677 if (!PENDING_EXCEPTION->is_a(vmClasses::Error_klass())) {
678 Handle e(THREAD, PENDING_EXCEPTION);
679 CLEAR_PENDING_EXCEPTION;
680 THROW_MSG_CAUSE_(vmSymbols::java_lang_InternalError(), "Internal error in hashCode", e, false);
681 }
682 }
683 return result.get_jint();
684 } else {
685 return checked_cast<jint>(ObjectSynchronizer::FastHashCode(THREAD, obj));
686 }
687 JVM_END
688
689
690 JVM_ENTRY(void, JVM_MonitorWait(JNIEnv* env, jobject handle, jlong ms))
691 Handle obj(THREAD, JNIHandles::resolve_non_null(handle));
692 JavaThreadInObjectWaitState jtiows(thread, ms != 0);
693 if (JvmtiExport::should_post_monitor_wait()) {
694 JvmtiExport::post_monitor_wait(thread, obj(), ms);
695
696 // The current thread already owns the monitor and it has not yet
697 // been added to the wait queue so the current thread cannot be
698 // made the successor. This means that the JVMTI_EVENT_MONITOR_WAIT
699 // event handler cannot accidentally consume an unpark() meant for
700 // the ParkEvent associated with this ObjectMonitor.
701 }
702 ObjectSynchronizer::wait(obj, ms, CHECK);
703 JVM_END
704
705
706 JVM_ENTRY(void, JVM_MonitorNotify(JNIEnv* env, jobject handle))
724 // Just checking that the cloneable flag is set correct
725 if (obj->is_array()) {
726 guarantee(klass->is_cloneable(), "all arrays are cloneable");
727 } else {
728 guarantee(obj->is_instance(), "should be instanceOop");
729 bool cloneable = klass->is_subtype_of(vmClasses::Cloneable_klass());
730 guarantee(cloneable == klass->is_cloneable(), "incorrect cloneable flag");
731 }
732 #endif
733
734 // Check if class of obj supports the Cloneable interface.
735 // All arrays are considered to be cloneable (See JLS 20.1.5).
736 // All j.l.r.Reference classes are considered non-cloneable.
737 if (!klass->is_cloneable() ||
738 (klass->is_instance_klass() &&
739 InstanceKlass::cast(klass)->reference_type() != REF_NONE)) {
740 ResourceMark rm(THREAD);
741 THROW_MSG_NULL(vmSymbols::java_lang_CloneNotSupportedException(), klass->external_name());
742 }
743
744 if (klass->is_inline_klass()) {
745 // Value instances have no identity, so return the current instance instead of allocating a new one
746 // Value classes cannot have finalizers, so the method can return immediately
747 return JNIHandles::make_local(THREAD, obj());
748 }
749
750 // Make shallow object copy
751 const size_t size = obj->size();
752 oop new_obj_oop = nullptr;
753 if (obj->is_array()) {
754 const int length = ((arrayOop)obj())->length();
755 new_obj_oop = Universe::heap()->array_allocate(klass, size, length,
756 /* do_zero */ true, CHECK_NULL);
757 } else {
758 new_obj_oop = Universe::heap()->obj_allocate(klass, size, CHECK_NULL);
759 }
760
761 HeapAccess<>::clone(obj(), new_obj_oop, size);
762
763 Handle new_obj(THREAD, new_obj_oop);
764 // Caution: this involves a java upcall, so the clone should be
765 // "gc-robust" by this stage.
766 if (klass->has_finalizer()) {
767 assert(obj->is_instance(), "should be instanceOop");
768 new_obj_oop = InstanceKlass::register_finalizer(instanceOop(new_obj()), CHECK_NULL);
769 new_obj = Handle(THREAD, new_obj_oop);
1240 oop result = java_lang_Class::name(java_class, CHECK_NULL);
1241 return (jstring) JNIHandles::make_local(THREAD, result);
1242 JVM_END
1243
1244
1245 JVM_ENTRY(jobjectArray, JVM_GetClassInterfaces(JNIEnv *env, jclass cls))
1246 JvmtiVMObjectAllocEventCollector oam;
1247 oop mirror = JNIHandles::resolve_non_null(cls);
1248
1249 // Special handling for primitive objects
1250 if (java_lang_Class::is_primitive(mirror)) {
1251 // Primitive objects does not have any interfaces
1252 objArrayOop r = oopFactory::new_objArray(vmClasses::Class_klass(), 0, CHECK_NULL);
1253 return (jobjectArray) JNIHandles::make_local(THREAD, r);
1254 }
1255
1256 Klass* klass = java_lang_Class::as_Klass(mirror);
1257 // Figure size of result array
1258 int size;
1259 if (klass->is_instance_klass()) {
1260 InstanceKlass* ik = InstanceKlass::cast(klass);
1261 size = ik->local_interfaces()->length();
1262 } else {
1263 assert(klass->is_objArray_klass() || klass->is_typeArray_klass() || klass->is_flatArray_klass(), "Illegal mirror klass");
1264 size = 2;
1265 }
1266
1267 // Allocate result array
1268 objArrayOop r = oopFactory::new_objArray(vmClasses::Class_klass(), size, CHECK_NULL);
1269 objArrayHandle result (THREAD, r);
1270 // Fill in result
1271 if (klass->is_instance_klass()) {
1272 // Regular instance klass, fill in all local interfaces
1273 for (int index = 0; index < size; index++) {
1274 InstanceKlass* ik = InstanceKlass::cast(klass);
1275 Klass* k = ik->local_interfaces()->at(index);
1276 result->obj_at_put(index, k->java_mirror());
1277 }
1278 } else {
1279 // All arrays implement java.lang.Cloneable and java.io.Serializable
1280 result->obj_at_put(0, vmClasses::Cloneable_klass()->java_mirror());
1281 result->obj_at_put(1, vmClasses::Serializable_klass()->java_mirror());
1282 }
1283 return (jobjectArray) JNIHandles::make_local(THREAD, result());
1284 JVM_END
1285
1286
1287 JVM_ENTRY(jboolean, JVM_IsInterface(JNIEnv *env, jclass cls))
1288 oop mirror = JNIHandles::resolve_non_null(cls);
1289 if (java_lang_Class::is_primitive(mirror)) {
1290 return JNI_FALSE;
1291 }
1292 Klass* k = java_lang_Class::as_Klass(mirror);
1293 jboolean result = k->is_interface();
1294 assert(!result || k->is_instance_klass(),
1295 "all interfaces are instance types");
1296 // The compiler intrinsic for isInterface tests the
1297 // Klass::_access_flags bits in the same way.
1298 return result;
1299 JVM_END
1300
1301 JVM_ENTRY(jboolean, JVM_IsHiddenClass(JNIEnv *env, jclass cls))
1302 oop mirror = JNIHandles::resolve_non_null(cls);
1303 if (java_lang_Class::is_primitive(mirror)) {
1304 return JNI_FALSE;
1305 }
1306 Klass* k = java_lang_Class::as_Klass(mirror);
1307 return k->is_hidden();
1308 JVM_END
1309
1310 JVM_ENTRY(jboolean, JVM_IsIdentityClass(JNIEnv *env, jclass cls))
1311 oop mirror = JNIHandles::resolve_non_null(cls);
1312 if (java_lang_Class::is_primitive(mirror)) {
1313 return JNI_FALSE;
1314 }
1315 Klass* k = java_lang_Class::as_Klass(mirror);
1316 if (EnableValhalla) {
1317 return k->is_array_klass() || k->is_identity_class();
1318 } else {
1319 return k->is_interface() ? JNI_FALSE : JNI_TRUE;
1320 }
1321 JVM_END
1322
1323 JVM_ENTRY(jboolean, JVM_IsImplicitlyConstructibleClass(JNIEnv *env, jclass cls))
1324 oop mirror = JNIHandles::resolve_non_null(cls);
1325 InstanceKlass* ik = InstanceKlass::cast(java_lang_Class::as_Klass(mirror));
1326 return ik->is_implicitly_constructible();
1327 JVM_END
1328
1329
1330 JVM_ENTRY(jobject, JVM_GetProtectionDomain(JNIEnv *env, jclass cls))
1331 oop mirror = JNIHandles::resolve_non_null(cls);
1332 if (mirror == nullptr) {
1333 THROW_(vmSymbols::java_lang_NullPointerException(), nullptr);
1334 }
1335
1336 if (java_lang_Class::is_primitive(mirror)) {
1337 // Primitive types does not have a protection domain.
1338 return nullptr;
1339 }
1340
1341 oop pd = java_lang_Class::protection_domain(mirror);
1342 return (jobject) JNIHandles::make_local(THREAD, pd);
1343 JVM_END
1344
1345
1346 // Returns the inherited_access_control_context field of the running thread.
1347 JVM_ENTRY(jobject, JVM_GetInheritedAccessControlContext(JNIEnv *env, jclass cls))
1348 oop result = java_lang_Thread::inherited_access_control_context(thread->threadObj());
1918 }
1919
1920 InstanceKlass* k = InstanceKlass::cast(java_lang_Class::as_Klass(ofMirror));
1921
1922 // Ensure class is linked
1923 k->link_class(CHECK_NULL);
1924
1925 Array<Method*>* methods = k->methods();
1926 int methods_length = methods->length();
1927
1928 // Save original method_idnum in case of redefinition, which can change
1929 // the idnum of obsolete methods. The new method will have the same idnum
1930 // but if we refresh the methods array, the counts will be wrong.
1931 ResourceMark rm(THREAD);
1932 GrowableArray<int>* idnums = new GrowableArray<int>(methods_length);
1933 int num_methods = 0;
1934
1935 // Select methods matching the criteria.
1936 for (int i = 0; i < methods_length; i++) {
1937 Method* method = methods->at(i);
1938 if (want_constructor && !method->is_object_constructor()) {
1939 continue;
1940 }
1941 if (!want_constructor &&
1942 (method->is_object_constructor() || method->is_class_initializer() ||
1943 method->is_overpass())) {
1944 continue;
1945 }
1946 if (publicOnly && !method->is_public()) {
1947 continue;
1948 }
1949 idnums->push(method->method_idnum());
1950 ++num_methods;
1951 }
1952
1953 // Allocate result
1954 objArrayOop r = oopFactory::new_objArray(klass, num_methods, CHECK_NULL);
1955 objArrayHandle result (THREAD, r);
1956
1957 // Now just put the methods that we selected above, but go by their idnum
1958 // in case of redefinition. The methods can be redefined at any safepoint,
1959 // so above when allocating the oop array and below when creating reflect
1960 // objects.
1961 for (int i = 0; i < num_methods; i++) {
1962 methodHandle method(THREAD, k->method_with_idnum(idnums->at(i)));
1963 if (method.is_null()) {
1964 // Method may have been deleted and seems this API can handle null
1965 // Otherwise should probably put a method that throws NSME
1966 result->obj_at_put(i, nullptr);
1967 } else {
1968 oop m;
1969 if (want_constructor) {
1970 assert(method->is_object_constructor(), "must be");
1971 m = Reflection::new_constructor(method, CHECK_NULL);
1972 } else {
1973 m = Reflection::new_method(method, false, CHECK_NULL);
1974 }
1975 result->obj_at_put(i, m);
1976 }
1977 }
1978
1979 return (jobjectArray) JNIHandles::make_local(THREAD, result());
1980 }
1981
1982 JVM_ENTRY(jobjectArray, JVM_GetClassDeclaredMethods(JNIEnv *env, jclass ofClass, jboolean publicOnly))
1983 {
1984 return get_class_declared_methods_helper(env, ofClass, publicOnly,
1985 /*want_constructor*/ false,
1986 vmClasses::reflect_Method_klass(), THREAD);
1987 }
1988 JVM_END
1989
1990 JVM_ENTRY(jobjectArray, JVM_GetClassDeclaredConstructors(JNIEnv *env, jclass ofClass, jboolean publicOnly))
2233 constantTag tag = cp->tag_at(index);
2234 if (!tag.is_method() && !tag.is_interface_method()) {
2235 THROW_MSG_NULL(vmSymbols::java_lang_IllegalArgumentException(), "Wrong type at constant pool index");
2236 }
2237 int klass_ref = cp->uncached_klass_ref_index_at(index);
2238 Klass* k_o;
2239 if (force_resolution) {
2240 k_o = cp->klass_at(klass_ref, CHECK_NULL);
2241 } else {
2242 k_o = ConstantPool::klass_at_if_loaded(cp, klass_ref);
2243 if (k_o == nullptr) return nullptr;
2244 }
2245 InstanceKlass* k = InstanceKlass::cast(k_o);
2246 Symbol* name = cp->uncached_name_ref_at(index);
2247 Symbol* sig = cp->uncached_signature_ref_at(index);
2248 methodHandle m (THREAD, k->find_method(name, sig));
2249 if (m.is_null()) {
2250 THROW_MSG_NULL(vmSymbols::java_lang_RuntimeException(), "Unable to look up method in target class");
2251 }
2252 oop method;
2253 if (m->is_object_constructor()) {
2254 method = Reflection::new_constructor(m, CHECK_NULL);
2255 } else {
2256 method = Reflection::new_method(m, true, CHECK_NULL);
2257 }
2258 return JNIHandles::make_local(THREAD, method);
2259 }
2260
2261 JVM_ENTRY(jobject, JVM_ConstantPoolGetMethodAt(JNIEnv *env, jobject obj, jobject unused, jint index))
2262 {
2263 JvmtiVMObjectAllocEventCollector oam;
2264 constantPoolHandle cp = constantPoolHandle(THREAD, reflect_ConstantPool::get_cp(JNIHandles::resolve_non_null(obj)));
2265 bounds_check(cp, index, CHECK_NULL);
2266 jobject res = get_method_at_helper(cp, index, true, CHECK_NULL);
2267 return res;
2268 }
2269 JVM_END
2270
2271 JVM_ENTRY(jobject, JVM_ConstantPoolGetMethodAtIfLoaded(JNIEnv *env, jobject obj, jobject unused, jint index))
2272 {
2273 JvmtiVMObjectAllocEventCollector oam;
2274 constantPoolHandle cp = constantPoolHandle(THREAD, reflect_ConstantPool::get_cp(JNIHandles::resolve_non_null(obj)));
2275 bounds_check(cp, index, CHECK_NULL);
2507 Klass* k = java_lang_Class::as_Klass(r);
2508 assert(k->is_instance_klass(), "must be an instance klass");
2509 if (!k->is_instance_klass()) return false;
2510
2511 ResourceMark rm(THREAD);
2512 const char* name = k->name()->as_C_string();
2513 bool system_class = k->class_loader() == nullptr;
2514 return JavaAssertions::enabled(name, system_class);
2515
2516 JVM_END
2517
2518
2519 // Return a new AssertionStatusDirectives object with the fields filled in with
2520 // command-line assertion arguments (i.e., -ea, -da).
2521 JVM_ENTRY(jobject, JVM_AssertionStatusDirectives(JNIEnv *env, jclass unused))
2522 JvmtiVMObjectAllocEventCollector oam;
2523 oop asd = JavaAssertions::createAssertionStatusDirectives(CHECK_NULL);
2524 return JNIHandles::make_local(THREAD, asd);
2525 JVM_END
2526
2527 // Arrays support /////////////////////////////////////////////////////////////
2528
2529 JVM_ENTRY(jboolean, JVM_ArrayIsAccessAtomic(JNIEnv *env, jclass unused, jobject array))
2530 oop o = JNIHandles::resolve(array);
2531 Klass* k = o->klass();
2532 if ((o == nullptr) || (!k->is_array_klass())) {
2533 THROW_0(vmSymbols::java_lang_IllegalArgumentException());
2534 }
2535 return ArrayKlass::cast(k)->element_access_must_be_atomic();
2536 JVM_END
2537
2538 JVM_ENTRY(jobject, JVM_ArrayEnsureAccessAtomic(JNIEnv *env, jclass unused, jobject array))
2539 oop o = JNIHandles::resolve(array);
2540 Klass* k = o->klass();
2541 if ((o == nullptr) || (!k->is_array_klass())) {
2542 THROW_0(vmSymbols::java_lang_IllegalArgumentException());
2543 }
2544 if (k->is_flatArray_klass()) {
2545 FlatArrayKlass* vk = FlatArrayKlass::cast(k);
2546 if (!vk->element_access_must_be_atomic()) {
2547 /**
2548 * Need to decide how to implement:
2549 *
2550 * 1) Change to objArrayOop layout, therefore oop->klass() differs so
2551 * then "<atomic>[Qfoo;" klass needs to subclass "[Qfoo;" to pass through
2552 * "checkcast" & "instanceof"
2553 *
2554 * 2) Use extra header in the flatArrayOop to flag atomicity required and
2555 * possibly per instance lock structure. Said info, could be placed in
2556 * "trailer" rather than disturb the current arrayOop
2557 */
2558 Unimplemented();
2559 }
2560 }
2561 return array;
2562 JVM_END
2563
2564 // Verification ////////////////////////////////////////////////////////////////////////////////
2565
2566 // Reflection for the verifier /////////////////////////////////////////////////////////////////
2567
2568 // RedefineClasses support: bug 6214132 caused verification to fail.
2569 // All functions from this section should call the jvmtiThreadSate function:
2570 // Klass* class_to_verify_considering_redefinition(Klass* klass).
2571 // The function returns a Klass* of the _scratch_class if the verifier
2572 // was invoked in the middle of the class redefinition.
2573 // Otherwise it returns its argument value which is the _the_class Klass*.
2574 // Please, refer to the description in the jvmtiThreadSate.hpp.
2575
2576 JVM_ENTRY(const char*, JVM_GetClassNameUTF(JNIEnv *env, jclass cls))
2577 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls));
2578 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
2579 return k->name()->as_utf8();
2580 JVM_END
2581
2582
2583 JVM_ENTRY(void, JVM_GetClassCPTypes(JNIEnv *env, jclass cls, unsigned char *types))
2673
2674 JVM_ENTRY(jint, JVM_GetMethodIxExceptionTableLength(JNIEnv *env, jclass cls, int method_index))
2675 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls));
2676 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
2677 Method* method = InstanceKlass::cast(k)->methods()->at(method_index);
2678 return method->exception_table_length();
2679 JVM_END
2680
2681
2682 JVM_ENTRY(jint, JVM_GetMethodIxModifiers(JNIEnv *env, jclass cls, int method_index))
2683 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls));
2684 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
2685 Method* method = InstanceKlass::cast(k)->methods()->at(method_index);
2686 return method->access_flags().as_int() & JVM_RECOGNIZED_METHOD_MODIFIERS;
2687 JVM_END
2688
2689
2690 JVM_ENTRY(jint, JVM_GetFieldIxModifiers(JNIEnv *env, jclass cls, int field_index))
2691 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls));
2692 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
2693 return InstanceKlass::cast(k)->field_access_flags(field_index);
2694 JVM_END
2695
2696
2697 JVM_ENTRY(jint, JVM_GetMethodIxLocalsCount(JNIEnv *env, jclass cls, int method_index))
2698 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls));
2699 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
2700 Method* method = InstanceKlass::cast(k)->methods()->at(method_index);
2701 return method->max_locals();
2702 JVM_END
2703
2704
2705 JVM_ENTRY(jint, JVM_GetMethodIxArgsSize(JNIEnv *env, jclass cls, int method_index))
2706 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls));
2707 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
2708 Method* method = InstanceKlass::cast(k)->methods()->at(method_index);
2709 return method->size_of_parameters();
2710 JVM_END
2711
2712
2713 JVM_ENTRY(jint, JVM_GetMethodIxMaxStack(JNIEnv *env, jclass cls, int method_index))
2714 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls));
2715 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
2716 Method* method = InstanceKlass::cast(k)->methods()->at(method_index);
2717 return method->verifier_max_stack();
2718 JVM_END
2719
2720
2721 JVM_ENTRY(jboolean, JVM_IsConstructorIx(JNIEnv *env, jclass cls, int method_index))
2722 ResourceMark rm(THREAD);
2723 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls));
2724 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
2725 Method* method = InstanceKlass::cast(k)->methods()->at(method_index);
2726 return method->is_object_constructor();
2727 JVM_END
2728
2729
2730 JVM_ENTRY(jboolean, JVM_IsVMGeneratedMethodIx(JNIEnv *env, jclass cls, int method_index))
2731 ResourceMark rm(THREAD);
2732 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls));
2733 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
2734 Method* method = InstanceKlass::cast(k)->methods()->at(method_index);
2735 return method->is_overpass();
2736 JVM_END
2737
2738 JVM_ENTRY(const char*, JVM_GetMethodIxNameUTF(JNIEnv *env, jclass cls, jint method_index))
2739 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls));
2740 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
2741 Method* method = InstanceKlass::cast(k)->methods()->at(method_index);
2742 return method->name()->as_utf8();
2743 JVM_END
2744
2745
2746 JVM_ENTRY(const char*, JVM_GetMethodIxSignatureUTF(JNIEnv *env, jclass cls, jint method_index))
2863 }
2864 ShouldNotReachHere();
2865 return nullptr;
2866 JVM_END
2867
2868
2869 JVM_ENTRY(jint, JVM_GetCPFieldModifiers(JNIEnv *env, jclass cls, int cp_index, jclass called_cls))
2870 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls));
2871 Klass* k_called = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(called_cls));
2872 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
2873 k_called = JvmtiThreadState::class_to_verify_considering_redefinition(k_called, thread);
2874 ConstantPool* cp = InstanceKlass::cast(k)->constants();
2875 ConstantPool* cp_called = InstanceKlass::cast(k_called)->constants();
2876 switch (cp->tag_at(cp_index).value()) {
2877 case JVM_CONSTANT_Fieldref: {
2878 Symbol* name = cp->uncached_name_ref_at(cp_index);
2879 Symbol* signature = cp->uncached_signature_ref_at(cp_index);
2880 InstanceKlass* ik = InstanceKlass::cast(k_called);
2881 for (JavaFieldStream fs(ik); !fs.done(); fs.next()) {
2882 if (fs.name() == name && fs.signature() == signature) {
2883 return fs.access_flags().as_short();
2884 }
2885 }
2886 return -1;
2887 }
2888 default:
2889 fatal("JVM_GetCPFieldModifiers: illegal constant");
2890 }
2891 ShouldNotReachHere();
2892 return 0;
2893 JVM_END
2894
2895
2896 JVM_ENTRY(jint, JVM_GetCPMethodModifiers(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 switch (cp->tag_at(cp_index).value()) {
2903 case JVM_CONSTANT_Methodref:
3536 JVM_LEAF(void*, JVM_FindLibraryEntry(void* handle, const char* name))
3537 void* find_result = os::dll_lookup(handle, name);
3538 log_info(library)("%s %s in library with handle " INTPTR_FORMAT,
3539 find_result != nullptr ? "Found" : "Failed to find",
3540 name, p2i(handle));
3541 return find_result;
3542 JVM_END
3543
3544
3545 // JNI version ///////////////////////////////////////////////////////////////////////////////
3546
3547 JVM_LEAF(jboolean, JVM_IsSupportedJNIVersion(jint version))
3548 return Threads::is_supported_jni_version_including_1_1(version);
3549 JVM_END
3550
3551
3552 JVM_LEAF(jboolean, JVM_IsPreviewEnabled(void))
3553 return Arguments::enable_preview() ? JNI_TRUE : JNI_FALSE;
3554 JVM_END
3555
3556 JVM_LEAF(jboolean, JVM_IsValhallaEnabled(void))
3557 return EnableValhalla ? JNI_TRUE : JNI_FALSE;
3558 JVM_END
3559
3560 JVM_LEAF(jboolean, JVM_IsContinuationsSupported(void))
3561 return VMContinuations ? JNI_TRUE : JNI_FALSE;
3562 JVM_END
3563
3564 JVM_LEAF(jboolean, JVM_IsForeignLinkerSupported(void))
3565 return ForeignGlobals::is_foreign_linker_supported() ? JNI_TRUE : JNI_FALSE;
3566 JVM_END
3567
3568 JVM_ENTRY_NO_ENV(jboolean, JVM_IsStaticallyLinked(void))
3569 return is_vm_statically_linked() ? JNI_TRUE : JNI_FALSE;
3570 JVM_END
3571
3572 // String support ///////////////////////////////////////////////////////////////////////////
3573
3574 JVM_ENTRY(jstring, JVM_InternString(JNIEnv *env, jstring str))
3575 JvmtiVMObjectAllocEventCollector oam;
3576 if (str == nullptr) return nullptr;
3577 oop string = JNIHandles::resolve_non_null(str);
3578 oop result = StringTable::intern(string, CHECK_NULL);
3579 return (jstring) JNIHandles::make_local(THREAD, result);
3625 // the checkPackageAccess relative to the initiating class loader via the
3626 // protection_domain. The protection_domain is passed as null by the java code
3627 // if there is no security manager in 3-arg Class.forName().
3628 Klass* klass = SystemDictionary::resolve_or_fail(name, loader, protection_domain, throwError != 0, CHECK_NULL);
3629
3630 // Check if we should initialize the class
3631 if (init && klass->is_instance_klass()) {
3632 klass->initialize(CHECK_NULL);
3633 }
3634 return (jclass) JNIHandles::make_local(THREAD, klass->java_mirror());
3635 }
3636
3637
3638 // Method ///////////////////////////////////////////////////////////////////////////////////////////
3639
3640 JVM_ENTRY(jobject, JVM_InvokeMethod(JNIEnv *env, jobject method, jobject obj, jobjectArray args0))
3641 Handle method_handle;
3642 if (thread->stack_overflow_state()->stack_available((address) &method_handle) >= JVMInvokeMethodSlack) {
3643 method_handle = Handle(THREAD, JNIHandles::resolve(method));
3644 Handle receiver(THREAD, JNIHandles::resolve(obj));
3645 objArrayHandle args = oopFactory::ensure_objArray(JNIHandles::resolve(args0), CHECK_NULL);
3646 oop result = Reflection::invoke_method(method_handle(), receiver, args, CHECK_NULL);
3647 jobject res = JNIHandles::make_local(THREAD, result);
3648 if (JvmtiExport::should_post_vm_object_alloc()) {
3649 oop ret_type = java_lang_reflect_Method::return_type(method_handle());
3650 assert(ret_type != nullptr, "sanity check: ret_type oop must not be null!");
3651 if (java_lang_Class::is_primitive(ret_type)) {
3652 // Only for primitive type vm allocates memory for java object.
3653 // See box() method.
3654 JvmtiExport::post_vm_object_alloc(thread, result);
3655 }
3656 }
3657 return res;
3658 } else {
3659 THROW_NULL(vmSymbols::java_lang_StackOverflowError());
3660 }
3661 JVM_END
3662
3663
3664 JVM_ENTRY(jobject, JVM_NewInstanceFromConstructor(JNIEnv *env, jobject c, jobjectArray args0))
3665 objArrayHandle args = oopFactory::ensure_objArray(JNIHandles::resolve(args0), CHECK_NULL);
3666 oop constructor_mirror = JNIHandles::resolve(c);
3667 oop result = Reflection::invoke_constructor(constructor_mirror, args, CHECK_NULL);
3668 jobject res = JNIHandles::make_local(THREAD, result);
3669 if (JvmtiExport::should_post_vm_object_alloc()) {
3670 JvmtiExport::post_vm_object_alloc(thread, result);
3671 }
3672 return res;
3673 JVM_END
3674
3675 JVM_ENTRY(void, JVM_InitializeFromArchive(JNIEnv* env, jclass cls))
3676 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve(cls));
3677 assert(k->is_klass(), "just checking");
3678 HeapShared::initialize_from_archived_subgraph(THREAD, k);
3679 JVM_END
3680
3681 JVM_ENTRY(void, JVM_RegisterLambdaProxyClassForArchiving(JNIEnv* env,
3682 jclass caller,
3683 jstring interfaceMethodName,
3684 jobject factoryType,
3685 jobject interfaceMethodType,
3686 jobject implementationMember,
|