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());
1810 int length = components->length();
1811 assert(length >= 0, "unexpected record_components length");
1812 objArrayOop record_components =
1813 oopFactory::new_objArray(vmClasses::RecordComponent_klass(), length, CHECK_NULL);
1814 objArrayHandle components_h (THREAD, record_components);
1815
1816 for (int x = 0; x < length; x++) {
1817 RecordComponent* component = components->at(x);
1818 assert(component != nullptr, "unexpected null record component");
1819 oop component_oop = java_lang_reflect_RecordComponent::create(ik, component, CHECK_NULL);
1820 components_h->obj_at_put(x, component_oop);
1821 }
1822 return (jobjectArray)JNIHandles::make_local(THREAD, components_h());
1823 }
1824
1825 return nullptr;
1826 }
1827 JVM_END
1828
1829 static bool select_method(const methodHandle& method, bool want_constructor) {
1830 if (want_constructor) {
1831 return (method->is_initializer() && !method->is_static());
1832 } else {
1833 return (!method->is_initializer() && !method->is_overpass());
1834 }
1835 }
1836
1837 static jobjectArray get_class_declared_methods_helper(
1838 JNIEnv *env,
1839 jclass ofClass, jboolean publicOnly,
1840 bool want_constructor,
1841 Klass* klass, TRAPS) {
1842
1843 JvmtiVMObjectAllocEventCollector oam;
1844
1845 oop ofMirror = JNIHandles::resolve_non_null(ofClass);
1846 // Exclude primitive types and array types
1847 if (java_lang_Class::is_primitive(ofMirror)
1848 || java_lang_Class::as_Klass(ofMirror)->is_array_klass()) {
1849 // Return empty array
1850 oop res = oopFactory::new_objArray(klass, 0, CHECK_NULL);
1851 return (jobjectArray) JNIHandles::make_local(THREAD, res);
1852 }
1853
1876 }
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_initializer() || m->is_static()) {
2179 method = Reflection::new_method(m, true, CHECK_NULL);
2180 } else {
2181 method = Reflection::new_constructor(m, CHECK_NULL);
2182 }
2183 return JNIHandles::make_local(THREAD, method);
2184 }
2185
2186 JVM_ENTRY(jobject, JVM_ConstantPoolGetMethodAt(JNIEnv *env, jobject obj, jobject unused, jint index))
2187 {
2188 JvmtiVMObjectAllocEventCollector oam;
2189 constantPoolHandle cp = constantPoolHandle(THREAD, reflect_ConstantPool::get_cp(JNIHandles::resolve_non_null(obj)));
2190 bounds_check(cp, index, CHECK_NULL);
2191 jobject res = get_method_at_helper(cp, index, true, CHECK_NULL);
2192 return res;
2193 }
2194 JVM_END
2195
2196 JVM_ENTRY(jobject, JVM_ConstantPoolGetMethodAtIfLoaded(JNIEnv *env, jobject obj, jobject unused, jint index))
2197 {
2198 JvmtiVMObjectAllocEventCollector oam;
2199 constantPoolHandle cp = constantPoolHandle(THREAD, reflect_ConstantPool::get_cp(JNIHandles::resolve_non_null(obj)));
2200 bounds_check(cp, index, CHECK_NULL);
2201 jobject res = get_method_at_helper(cp, index, false, CHECK_NULL);
2432 Klass* k = java_lang_Class::as_Klass(r);
2433 assert(k->is_instance_klass(), "must be an instance klass");
2434 if (!k->is_instance_klass()) return false;
2435
2436 ResourceMark rm(THREAD);
2437 const char* name = k->name()->as_C_string();
2438 bool system_class = k->class_loader() == nullptr;
2439 return JavaAssertions::enabled(name, system_class);
2440
2441 JVM_END
2442
2443
2444 // Return a new AssertionStatusDirectives object with the fields filled in with
2445 // command-line assertion arguments (i.e., -ea, -da).
2446 JVM_ENTRY(jobject, JVM_AssertionStatusDirectives(JNIEnv *env, jclass unused))
2447 JvmtiVMObjectAllocEventCollector oam;
2448 oop asd = JavaAssertions::createAssertionStatusDirectives(CHECK_NULL);
2449 return JNIHandles::make_local(THREAD, asd);
2450 JVM_END
2451
2452 // Verification ////////////////////////////////////////////////////////////////////////////////
2453
2454 // Reflection for the verifier /////////////////////////////////////////////////////////////////
2455
2456 // RedefineClasses support: bug 6214132 caused verification to fail.
2457 // All functions from this section should call the jvmtiThreadSate function:
2458 // Klass* class_to_verify_considering_redefinition(Klass* klass).
2459 // The function returns a Klass* of the _scratch_class if the verifier
2460 // was invoked in the middle of the class redefinition.
2461 // Otherwise it returns its argument value which is the _the_class Klass*.
2462 // Please, refer to the description in the jvmtiThreadSate.hpp.
2463
2464 JVM_ENTRY(const char*, JVM_GetClassNameUTF(JNIEnv *env, jclass cls))
2465 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls));
2466 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
2467 return k->name()->as_utf8();
2468 JVM_END
2469
2470
2471 JVM_ENTRY(void, JVM_GetClassCPTypes(JNIEnv *env, jclass cls, unsigned char *types))
2561
2562 JVM_ENTRY(jint, JVM_GetMethodIxExceptionTableLength(JNIEnv *env, jclass cls, int method_index))
2563 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls));
2564 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
2565 Method* method = InstanceKlass::cast(k)->methods()->at(method_index);
2566 return method->exception_table_length();
2567 JVM_END
2568
2569
2570 JVM_ENTRY(jint, JVM_GetMethodIxModifiers(JNIEnv *env, jclass cls, int method_index))
2571 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls));
2572 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
2573 Method* method = InstanceKlass::cast(k)->methods()->at(method_index);
2574 return method->access_flags().as_int() & JVM_RECOGNIZED_METHOD_MODIFIERS;
2575 JVM_END
2576
2577
2578 JVM_ENTRY(jint, JVM_GetFieldIxModifiers(JNIEnv *env, jclass cls, int field_index))
2579 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls));
2580 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
2581 return InstanceKlass::cast(k)->field_access_flags(field_index) & JVM_RECOGNIZED_FIELD_MODIFIERS;
2582 JVM_END
2583
2584
2585 JVM_ENTRY(jint, JVM_GetMethodIxLocalsCount(JNIEnv *env, jclass cls, int method_index))
2586 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls));
2587 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
2588 Method* method = InstanceKlass::cast(k)->methods()->at(method_index);
2589 return method->max_locals();
2590 JVM_END
2591
2592
2593 JVM_ENTRY(jint, JVM_GetMethodIxArgsSize(JNIEnv *env, jclass cls, int method_index))
2594 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls));
2595 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
2596 Method* method = InstanceKlass::cast(k)->methods()->at(method_index);
2597 return method->size_of_parameters();
2598 JVM_END
2599
2600
2601 JVM_ENTRY(jint, JVM_GetMethodIxMaxStack(JNIEnv *env, jclass cls, int method_index))
2602 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls));
2603 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
2604 Method* method = InstanceKlass::cast(k)->methods()->at(method_index);
2605 return method->verifier_max_stack();
2606 JVM_END
2607
2608
2609 JVM_ENTRY(jboolean, JVM_IsConstructorIx(JNIEnv *env, jclass cls, int method_index))
2610 ResourceMark rm(THREAD);
2611 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls));
2612 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
2613 Method* method = InstanceKlass::cast(k)->methods()->at(method_index);
2614 return method->name() == vmSymbols::object_initializer_name();
2615 JVM_END
2616
2617
2618 JVM_ENTRY(jboolean, JVM_IsVMGeneratedMethodIx(JNIEnv *env, jclass cls, int method_index))
2619 ResourceMark rm(THREAD);
2620 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls));
2621 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
2622 Method* method = InstanceKlass::cast(k)->methods()->at(method_index);
2623 return method->is_overpass();
2624 JVM_END
2625
2626 JVM_ENTRY(const char*, JVM_GetMethodIxNameUTF(JNIEnv *env, jclass cls, jint method_index))
2627 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls));
2628 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
2629 Method* method = InstanceKlass::cast(k)->methods()->at(method_index);
2630 return method->name()->as_utf8();
2631 JVM_END
2632
2633
2634 JVM_ENTRY(const char*, JVM_GetMethodIxSignatureUTF(JNIEnv *env, jclass cls, jint method_index))
2751 }
2752 ShouldNotReachHere();
2753 return nullptr;
2754 JVM_END
2755
2756
2757 JVM_ENTRY(jint, JVM_GetCPFieldModifiers(JNIEnv *env, jclass cls, int cp_index, jclass called_cls))
2758 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls));
2759 Klass* k_called = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(called_cls));
2760 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
2761 k_called = JvmtiThreadState::class_to_verify_considering_redefinition(k_called, thread);
2762 ConstantPool* cp = InstanceKlass::cast(k)->constants();
2763 ConstantPool* cp_called = InstanceKlass::cast(k_called)->constants();
2764 switch (cp->tag_at(cp_index).value()) {
2765 case JVM_CONSTANT_Fieldref: {
2766 Symbol* name = cp->uncached_name_ref_at(cp_index);
2767 Symbol* signature = cp->uncached_signature_ref_at(cp_index);
2768 InstanceKlass* ik = InstanceKlass::cast(k_called);
2769 for (JavaFieldStream fs(ik); !fs.done(); fs.next()) {
2770 if (fs.name() == name && fs.signature() == signature) {
2771 return fs.access_flags().as_short() & JVM_RECOGNIZED_FIELD_MODIFIERS;
2772 }
2773 }
2774 return -1;
2775 }
2776 default:
2777 fatal("JVM_GetCPFieldModifiers: illegal constant");
2778 }
2779 ShouldNotReachHere();
2780 return 0;
2781 JVM_END
2782
2783
2784 JVM_ENTRY(jint, JVM_GetCPMethodModifiers(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 switch (cp->tag_at(cp_index).value()) {
2791 case JVM_CONSTANT_Methodref:
3423 JVM_LEAF(void*, JVM_FindLibraryEntry(void* handle, const char* name))
3424 void* find_result = os::dll_lookup(handle, name);
3425 log_info(library)("%s %s in library with handle " INTPTR_FORMAT,
3426 find_result != nullptr ? "Found" : "Failed to find",
3427 name, p2i(handle));
3428 return find_result;
3429 JVM_END
3430
3431
3432 // JNI version ///////////////////////////////////////////////////////////////////////////////
3433
3434 JVM_LEAF(jboolean, JVM_IsSupportedJNIVersion(jint version))
3435 return Threads::is_supported_jni_version_including_1_1(version);
3436 JVM_END
3437
3438
3439 JVM_LEAF(jboolean, JVM_IsPreviewEnabled(void))
3440 return Arguments::enable_preview() ? JNI_TRUE : JNI_FALSE;
3441 JVM_END
3442
3443 JVM_LEAF(jboolean, JVM_IsContinuationsSupported(void))
3444 return VMContinuations ? JNI_TRUE : JNI_FALSE;
3445 JVM_END
3446
3447 JVM_LEAF(jboolean, JVM_IsForeignLinkerSupported(void))
3448 return ForeignGlobals::is_foreign_linker_supported() ? JNI_TRUE : JNI_FALSE;
3449 JVM_END
3450
3451 JVM_ENTRY_NO_ENV(jboolean, JVM_IsStaticallyLinked(void))
3452 return is_vm_statically_linked() ? JNI_TRUE : JNI_FALSE;
3453 JVM_END
3454
3455 // String support ///////////////////////////////////////////////////////////////////////////
3456
3457 JVM_ENTRY(jstring, JVM_InternString(JNIEnv *env, jstring str))
3458 JvmtiVMObjectAllocEventCollector oam;
3459 if (str == nullptr) return nullptr;
3460 oop string = JNIHandles::resolve_non_null(str);
3461 oop result = StringTable::intern(string, CHECK_NULL);
3462 return (jstring) JNIHandles::make_local(THREAD, result);
3508 // the checkPackageAccess relative to the initiating class loader via the
3509 // protection_domain. The protection_domain is passed as null by the java code
3510 // if there is no security manager in 3-arg Class.forName().
3511 Klass* klass = SystemDictionary::resolve_or_fail(name, loader, protection_domain, throwError != 0, CHECK_NULL);
3512
3513 // Check if we should initialize the class
3514 if (init && klass->is_instance_klass()) {
3515 klass->initialize(CHECK_NULL);
3516 }
3517 return (jclass) JNIHandles::make_local(THREAD, klass->java_mirror());
3518 }
3519
3520
3521 // Method ///////////////////////////////////////////////////////////////////////////////////////////
3522
3523 JVM_ENTRY(jobject, JVM_InvokeMethod(JNIEnv *env, jobject method, jobject obj, jobjectArray args0))
3524 Handle method_handle;
3525 if (thread->stack_overflow_state()->stack_available((address) &method_handle) >= JVMInvokeMethodSlack) {
3526 method_handle = Handle(THREAD, JNIHandles::resolve(method));
3527 Handle receiver(THREAD, JNIHandles::resolve(obj));
3528 objArrayHandle args(THREAD, objArrayOop(JNIHandles::resolve(args0)));
3529 oop result = Reflection::invoke_method(method_handle(), receiver, args, CHECK_NULL);
3530 jobject res = JNIHandles::make_local(THREAD, result);
3531 if (JvmtiExport::should_post_vm_object_alloc()) {
3532 oop ret_type = java_lang_reflect_Method::return_type(method_handle());
3533 assert(ret_type != nullptr, "sanity check: ret_type oop must not be null!");
3534 if (java_lang_Class::is_primitive(ret_type)) {
3535 // Only for primitive type vm allocates memory for java object.
3536 // See box() method.
3537 JvmtiExport::post_vm_object_alloc(thread, result);
3538 }
3539 }
3540 return res;
3541 } else {
3542 THROW_NULL(vmSymbols::java_lang_StackOverflowError());
3543 }
3544 JVM_END
3545
3546
3547 JVM_ENTRY(jobject, JVM_NewInstanceFromConstructor(JNIEnv *env, jobject c, jobjectArray args0))
3548 oop constructor_mirror = JNIHandles::resolve(c);
3549 objArrayHandle args(THREAD, objArrayOop(JNIHandles::resolve(args0)));
3550 oop result = Reflection::invoke_constructor(constructor_mirror, args, CHECK_NULL);
3551 jobject res = JNIHandles::make_local(THREAD, result);
3552 if (JvmtiExport::should_post_vm_object_alloc()) {
3553 JvmtiExport::post_vm_object_alloc(thread, result);
3554 }
3555 return res;
3556 JVM_END
3557
3558 JVM_ENTRY(void, JVM_InitializeFromArchive(JNIEnv* env, jclass cls))
3559 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve(cls));
3560 assert(k->is_klass(), "just checking");
3561 HeapShared::initialize_from_archived_subgraph(THREAD, k);
3562 JVM_END
3563
3564 JVM_ENTRY(void, JVM_RegisterLambdaProxyClassForArchiving(JNIEnv* env,
3565 jclass caller,
3566 jstring interfaceMethodName,
3567 jobject factoryType,
3568 jobject interfaceMethodType,
3569 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 InstanceKlass* ik = InstanceKlass::cast(klass);
442 if (!ik->is_implicitly_constructible()) {
443 THROW_MSG_NULL(vmSymbols::java_lang_IllegalArgumentException(), "Element class is not implicitly constructible");
444 }
445 oop array = oopFactory::new_valueArray(ik, len, CHECK_NULL);
446 return (jarray) JNIHandles::make_local(THREAD, array);
447 JVM_END
448
449
450 JVM_ENTRY(jboolean, JVM_IsNullRestrictedArray(JNIEnv *env, jobject obj))
451 arrayOop oop = arrayOop(JNIHandles::resolve_non_null(obj));
452 return oop->is_null_free_array();
453 JVM_END
454
455 // java.lang.Runtime /////////////////////////////////////////////////////////////////////////
456
457 extern volatile jint vm_created;
458
459 JVM_ENTRY_NO_ENV(void, JVM_BeforeHalt())
460 EventShutdown event;
461 if (event.should_commit()) {
462 event.set_reason("Shutdown requested from Java");
463 event.commit();
464 }
465 JVM_END
466
467
468 JVM_ENTRY_NO_ENV(void, JVM_Halt(jint code))
469 before_exit(thread, true);
470 vm_exit(code);
471 JVM_END
472
473
642
643 Handle stackStream_h(THREAD, JNIHandles::resolve_non_null(stackStream));
644 return StackWalk::fetchNextBatch(stackStream_h, mode, anchor, last_batch_count, buffer_size,
645 start_index, frames_array_h, THREAD);
646 JVM_END
647
648 JVM_ENTRY(void, JVM_SetStackWalkContinuation(JNIEnv *env, jobject stackStream, jlong anchor, jobjectArray frames, jobject cont))
649 objArrayOop fa = objArrayOop(JNIHandles::resolve_non_null(frames));
650 objArrayHandle frames_array_h(THREAD, fa);
651 Handle stackStream_h(THREAD, JNIHandles::resolve_non_null(stackStream));
652 Handle cont_h(THREAD, JNIHandles::resolve_non_null(cont));
653
654 StackWalk::setContinuation(stackStream_h, anchor, frames_array_h, cont_h, THREAD);
655 JVM_END
656
657 // java.lang.Object ///////////////////////////////////////////////
658
659
660 JVM_ENTRY(jint, JVM_IHashCode(JNIEnv* env, jobject handle))
661 // as implemented in the classic virtual machine; return 0 if object is null
662 if (handle == nullptr) {
663 return 0;
664 }
665 oop obj = JNIHandles::resolve_non_null(handle);
666 if (EnableValhalla && obj->klass()->is_inline_klass()) {
667 JavaValue result(T_INT);
668 JavaCallArguments args;
669 Handle ho(THREAD, obj);
670 args.push_oop(ho);
671 methodHandle method(THREAD, Universe::value_object_hash_code_method());
672 JavaCalls::call(&result, method, &args, THREAD);
673 if (HAS_PENDING_EXCEPTION) {
674 if (!PENDING_EXCEPTION->is_a(vmClasses::Error_klass())) {
675 Handle e(THREAD, PENDING_EXCEPTION);
676 CLEAR_PENDING_EXCEPTION;
677 THROW_MSG_CAUSE_(vmSymbols::java_lang_InternalError(), "Internal error in hashCode", e, false);
678 }
679 }
680 return result.get_jint();
681 } else {
682 return checked_cast<jint>(ObjectSynchronizer::FastHashCode(THREAD, obj));
683 }
684 JVM_END
685
686
687 JVM_ENTRY(void, JVM_MonitorWait(JNIEnv* env, jobject handle, jlong ms))
688 Handle obj(THREAD, JNIHandles::resolve_non_null(handle));
689 JavaThreadInObjectWaitState jtiows(thread, ms != 0);
690 if (JvmtiExport::should_post_monitor_wait()) {
691 JvmtiExport::post_monitor_wait(thread, obj(), ms);
692
693 // The current thread already owns the monitor and it has not yet
694 // been added to the wait queue so the current thread cannot be
695 // made the successor. This means that the JVMTI_EVENT_MONITOR_WAIT
696 // event handler cannot accidentally consume an unpark() meant for
697 // the ParkEvent associated with this ObjectMonitor.
698 }
699 ObjectSynchronizer::wait(obj, ms, CHECK);
700 JVM_END
701
702
703 JVM_ENTRY(void, JVM_MonitorNotify(JNIEnv* env, jobject handle))
721 // Just checking that the cloneable flag is set correct
722 if (obj->is_array()) {
723 guarantee(klass->is_cloneable(), "all arrays are cloneable");
724 } else {
725 guarantee(obj->is_instance(), "should be instanceOop");
726 bool cloneable = klass->is_subtype_of(vmClasses::Cloneable_klass());
727 guarantee(cloneable == klass->is_cloneable(), "incorrect cloneable flag");
728 }
729 #endif
730
731 // Check if class of obj supports the Cloneable interface.
732 // All arrays are considered to be cloneable (See JLS 20.1.5).
733 // All j.l.r.Reference classes are considered non-cloneable.
734 if (!klass->is_cloneable() ||
735 (klass->is_instance_klass() &&
736 InstanceKlass::cast(klass)->reference_type() != REF_NONE)) {
737 ResourceMark rm(THREAD);
738 THROW_MSG_NULL(vmSymbols::java_lang_CloneNotSupportedException(), klass->external_name());
739 }
740
741 if (klass->is_inline_klass()) {
742 // Value instances have no identity, so return the current instance instead of allocating a new one
743 // Value classes cannot have finalizers, so the method can return immediately
744 return JNIHandles::make_local(THREAD, obj());
745 }
746
747 // Make shallow object copy
748 const size_t size = obj->size();
749 oop new_obj_oop = nullptr;
750 if (obj->is_array()) {
751 const int length = ((arrayOop)obj())->length();
752 new_obj_oop = Universe::heap()->array_allocate(klass, size, length,
753 /* do_zero */ true, CHECK_NULL);
754 } else {
755 new_obj_oop = Universe::heap()->obj_allocate(klass, size, CHECK_NULL);
756 }
757
758 HeapAccess<>::clone(obj(), new_obj_oop, size);
759
760 Handle new_obj(THREAD, new_obj_oop);
761 // Caution: this involves a java upcall, so the clone should be
762 // "gc-robust" by this stage.
763 if (klass->has_finalizer()) {
764 assert(obj->is_instance(), "should be instanceOop");
765 new_obj_oop = InstanceKlass::register_finalizer(instanceOop(new_obj()), CHECK_NULL);
766 new_obj = Handle(THREAD, new_obj_oop);
1237 oop result = java_lang_Class::name(java_class, CHECK_NULL);
1238 return (jstring) JNIHandles::make_local(THREAD, result);
1239 JVM_END
1240
1241
1242 JVM_ENTRY(jobjectArray, JVM_GetClassInterfaces(JNIEnv *env, jclass cls))
1243 JvmtiVMObjectAllocEventCollector oam;
1244 oop mirror = JNIHandles::resolve_non_null(cls);
1245
1246 // Special handling for primitive objects
1247 if (java_lang_Class::is_primitive(mirror)) {
1248 // Primitive objects does not have any interfaces
1249 objArrayOop r = oopFactory::new_objArray(vmClasses::Class_klass(), 0, CHECK_NULL);
1250 return (jobjectArray) JNIHandles::make_local(THREAD, r);
1251 }
1252
1253 Klass* klass = java_lang_Class::as_Klass(mirror);
1254 // Figure size of result array
1255 int size;
1256 if (klass->is_instance_klass()) {
1257 InstanceKlass* ik = InstanceKlass::cast(klass);
1258 size = ik->local_interfaces()->length();
1259 } else {
1260 assert(klass->is_objArray_klass() || klass->is_typeArray_klass() || klass->is_flatArray_klass(), "Illegal mirror klass");
1261 size = 2;
1262 }
1263
1264 // Allocate result array
1265 objArrayOop r = oopFactory::new_objArray(vmClasses::Class_klass(), size, CHECK_NULL);
1266 objArrayHandle result (THREAD, r);
1267 // Fill in result
1268 if (klass->is_instance_klass()) {
1269 // Regular instance klass, fill in all local interfaces
1270 for (int index = 0; index < size; index++) {
1271 InstanceKlass* ik = InstanceKlass::cast(klass);
1272 Klass* k = ik->local_interfaces()->at(index);
1273 result->obj_at_put(index, k->java_mirror());
1274 }
1275 } else {
1276 // All arrays implement java.lang.Cloneable and java.io.Serializable
1277 result->obj_at_put(0, vmClasses::Cloneable_klass()->java_mirror());
1278 result->obj_at_put(1, vmClasses::Serializable_klass()->java_mirror());
1279 }
1280 return (jobjectArray) JNIHandles::make_local(THREAD, result());
1281 JVM_END
1282
1283
1284 JVM_ENTRY(jboolean, JVM_IsInterface(JNIEnv *env, jclass cls))
1285 oop mirror = JNIHandles::resolve_non_null(cls);
1286 if (java_lang_Class::is_primitive(mirror)) {
1287 return JNI_FALSE;
1288 }
1289 Klass* k = java_lang_Class::as_Klass(mirror);
1290 jboolean result = k->is_interface();
1291 assert(!result || k->is_instance_klass(),
1292 "all interfaces are instance types");
1293 // The compiler intrinsic for isInterface tests the
1294 // Klass::_access_flags bits in the same way.
1295 return result;
1296 JVM_END
1297
1298 JVM_ENTRY(jboolean, JVM_IsHiddenClass(JNIEnv *env, jclass cls))
1299 oop mirror = JNIHandles::resolve_non_null(cls);
1300 if (java_lang_Class::is_primitive(mirror)) {
1301 return JNI_FALSE;
1302 }
1303 Klass* k = java_lang_Class::as_Klass(mirror);
1304 return k->is_hidden();
1305 JVM_END
1306
1307 JVM_ENTRY(jboolean, JVM_IsIdentityClass(JNIEnv *env, jclass cls))
1308 oop mirror = JNIHandles::resolve_non_null(cls);
1309 if (java_lang_Class::is_primitive(mirror)) {
1310 return JNI_FALSE;
1311 }
1312 Klass* k = java_lang_Class::as_Klass(mirror);
1313 if (EnableValhalla) {
1314 return k->is_array_klass() || k->is_identity_class();
1315 } else {
1316 return k->is_interface() ? JNI_FALSE : JNI_TRUE;
1317 }
1318 JVM_END
1319
1320 JVM_ENTRY(jboolean, JVM_IsImplicitlyConstructibleClass(JNIEnv *env, jclass cls))
1321 oop mirror = JNIHandles::resolve_non_null(cls);
1322 InstanceKlass* ik = InstanceKlass::cast(java_lang_Class::as_Klass(mirror));
1323 return ik->is_implicitly_constructible();
1324 JVM_END
1325
1326
1327 JVM_ENTRY(jobject, JVM_GetProtectionDomain(JNIEnv *env, jclass cls))
1328 oop mirror = JNIHandles::resolve_non_null(cls);
1329 if (mirror == nullptr) {
1330 THROW_(vmSymbols::java_lang_NullPointerException(), nullptr);
1331 }
1332
1333 if (java_lang_Class::is_primitive(mirror)) {
1334 // Primitive types does not have a protection domain.
1335 return nullptr;
1336 }
1337
1338 oop pd = java_lang_Class::protection_domain(mirror);
1339 return (jobject) JNIHandles::make_local(THREAD, pd);
1340 JVM_END
1341
1342
1343 // Returns the inherited_access_control_context field of the running thread.
1344 JVM_ENTRY(jobject, JVM_GetInheritedAccessControlContext(JNIEnv *env, jclass cls))
1345 oop result = java_lang_Thread::inherited_access_control_context(thread->threadObj());
1881 int length = components->length();
1882 assert(length >= 0, "unexpected record_components length");
1883 objArrayOop record_components =
1884 oopFactory::new_objArray(vmClasses::RecordComponent_klass(), length, CHECK_NULL);
1885 objArrayHandle components_h (THREAD, record_components);
1886
1887 for (int x = 0; x < length; x++) {
1888 RecordComponent* component = components->at(x);
1889 assert(component != nullptr, "unexpected null record component");
1890 oop component_oop = java_lang_reflect_RecordComponent::create(ik, component, CHECK_NULL);
1891 components_h->obj_at_put(x, component_oop);
1892 }
1893 return (jobjectArray)JNIHandles::make_local(THREAD, components_h());
1894 }
1895
1896 return nullptr;
1897 }
1898 JVM_END
1899
1900 static bool select_method(const methodHandle& method, bool want_constructor) {
1901 bool is_ctor = (method->is_object_constructor());
1902 if (want_constructor) {
1903 return is_ctor;
1904 } else {
1905 return (!is_ctor &&
1906 !method->is_class_initializer() &&
1907 !method->is_overpass());
1908 }
1909 }
1910
1911 static jobjectArray get_class_declared_methods_helper(
1912 JNIEnv *env,
1913 jclass ofClass, jboolean publicOnly,
1914 bool want_constructor,
1915 Klass* klass, TRAPS) {
1916
1917 JvmtiVMObjectAllocEventCollector oam;
1918
1919 oop ofMirror = JNIHandles::resolve_non_null(ofClass);
1920 // Exclude primitive types and array types
1921 if (java_lang_Class::is_primitive(ofMirror)
1922 || java_lang_Class::as_Klass(ofMirror)->is_array_klass()) {
1923 // Return empty array
1924 oop res = oopFactory::new_objArray(klass, 0, CHECK_NULL);
1925 return (jobjectArray) JNIHandles::make_local(THREAD, res);
1926 }
1927
1950 }
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);
2276 jobject res = get_method_at_helper(cp, index, false, 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_is_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_is_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:
3535 JVM_LEAF(void*, JVM_FindLibraryEntry(void* handle, const char* name))
3536 void* find_result = os::dll_lookup(handle, name);
3537 log_info(library)("%s %s in library with handle " INTPTR_FORMAT,
3538 find_result != nullptr ? "Found" : "Failed to find",
3539 name, p2i(handle));
3540 return find_result;
3541 JVM_END
3542
3543
3544 // JNI version ///////////////////////////////////////////////////////////////////////////////
3545
3546 JVM_LEAF(jboolean, JVM_IsSupportedJNIVersion(jint version))
3547 return Threads::is_supported_jni_version_including_1_1(version);
3548 JVM_END
3549
3550
3551 JVM_LEAF(jboolean, JVM_IsPreviewEnabled(void))
3552 return Arguments::enable_preview() ? JNI_TRUE : JNI_FALSE;
3553 JVM_END
3554
3555 JVM_LEAF(jboolean, JVM_IsValhallaEnabled(void))
3556 return EnableValhalla ? JNI_TRUE : JNI_FALSE;
3557 JVM_END
3558
3559 JVM_LEAF(jboolean, JVM_IsContinuationsSupported(void))
3560 return VMContinuations ? JNI_TRUE : JNI_FALSE;
3561 JVM_END
3562
3563 JVM_LEAF(jboolean, JVM_IsForeignLinkerSupported(void))
3564 return ForeignGlobals::is_foreign_linker_supported() ? JNI_TRUE : JNI_FALSE;
3565 JVM_END
3566
3567 JVM_ENTRY_NO_ENV(jboolean, JVM_IsStaticallyLinked(void))
3568 return is_vm_statically_linked() ? JNI_TRUE : JNI_FALSE;
3569 JVM_END
3570
3571 // String support ///////////////////////////////////////////////////////////////////////////
3572
3573 JVM_ENTRY(jstring, JVM_InternString(JNIEnv *env, jstring str))
3574 JvmtiVMObjectAllocEventCollector oam;
3575 if (str == nullptr) return nullptr;
3576 oop string = JNIHandles::resolve_non_null(str);
3577 oop result = StringTable::intern(string, CHECK_NULL);
3578 return (jstring) JNIHandles::make_local(THREAD, result);
3624 // the checkPackageAccess relative to the initiating class loader via the
3625 // protection_domain. The protection_domain is passed as null by the java code
3626 // if there is no security manager in 3-arg Class.forName().
3627 Klass* klass = SystemDictionary::resolve_or_fail(name, loader, protection_domain, throwError != 0, CHECK_NULL);
3628
3629 // Check if we should initialize the class
3630 if (init && klass->is_instance_klass()) {
3631 klass->initialize(CHECK_NULL);
3632 }
3633 return (jclass) JNIHandles::make_local(THREAD, klass->java_mirror());
3634 }
3635
3636
3637 // Method ///////////////////////////////////////////////////////////////////////////////////////////
3638
3639 JVM_ENTRY(jobject, JVM_InvokeMethod(JNIEnv *env, jobject method, jobject obj, jobjectArray args0))
3640 Handle method_handle;
3641 if (thread->stack_overflow_state()->stack_available((address) &method_handle) >= JVMInvokeMethodSlack) {
3642 method_handle = Handle(THREAD, JNIHandles::resolve(method));
3643 Handle receiver(THREAD, JNIHandles::resolve(obj));
3644 objArrayHandle args = oopFactory::ensure_objArray(JNIHandles::resolve(args0), CHECK_NULL);
3645 oop result = Reflection::invoke_method(method_handle(), receiver, args, CHECK_NULL);
3646 jobject res = JNIHandles::make_local(THREAD, result);
3647 if (JvmtiExport::should_post_vm_object_alloc()) {
3648 oop ret_type = java_lang_reflect_Method::return_type(method_handle());
3649 assert(ret_type != nullptr, "sanity check: ret_type oop must not be null!");
3650 if (java_lang_Class::is_primitive(ret_type)) {
3651 // Only for primitive type vm allocates memory for java object.
3652 // See box() method.
3653 JvmtiExport::post_vm_object_alloc(thread, result);
3654 }
3655 }
3656 return res;
3657 } else {
3658 THROW_NULL(vmSymbols::java_lang_StackOverflowError());
3659 }
3660 JVM_END
3661
3662
3663 JVM_ENTRY(jobject, JVM_NewInstanceFromConstructor(JNIEnv *env, jobject c, jobjectArray args0))
3664 objArrayHandle args = oopFactory::ensure_objArray(JNIHandles::resolve(args0), CHECK_NULL);
3665 oop constructor_mirror = JNIHandles::resolve(c);
3666 oop result = Reflection::invoke_constructor(constructor_mirror, args, CHECK_NULL);
3667 jobject res = JNIHandles::make_local(THREAD, result);
3668 if (JvmtiExport::should_post_vm_object_alloc()) {
3669 JvmtiExport::post_vm_object_alloc(thread, result);
3670 }
3671 return res;
3672 JVM_END
3673
3674 JVM_ENTRY(void, JVM_InitializeFromArchive(JNIEnv* env, jclass cls))
3675 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve(cls));
3676 assert(k->is_klass(), "just checking");
3677 HeapShared::initialize_from_archived_subgraph(THREAD, k);
3678 JVM_END
3679
3680 JVM_ENTRY(void, JVM_RegisterLambdaProxyClassForArchiving(JNIEnv* env,
3681 jclass caller,
3682 jstring interfaceMethodName,
3683 jobject factoryType,
3684 jobject interfaceMethodType,
3685 jobject implementationMember,
|