1 /*
2 * Copyright (c) 1997, 2024, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
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:
3422 JVM_LEAF(void*, JVM_FindLibraryEntry(void* handle, const char* name))
3423 void* find_result = os::dll_lookup(handle, name);
3424 log_info(library)("%s %s in library with handle " INTPTR_FORMAT,
3425 find_result != nullptr ? "Found" : "Failed to find",
3426 name, p2i(handle));
3427 return find_result;
3428 JVM_END
3429
3430
3431 // JNI version ///////////////////////////////////////////////////////////////////////////////
3432
3433 JVM_LEAF(jboolean, JVM_IsSupportedJNIVersion(jint version))
3434 return Threads::is_supported_jni_version_including_1_1(version);
3435 JVM_END
3436
3437
3438 JVM_LEAF(jboolean, JVM_IsPreviewEnabled(void))
3439 return Arguments::enable_preview() ? JNI_TRUE : JNI_FALSE;
3440 JVM_END
3441
3442 JVM_LEAF(jboolean, JVM_IsContinuationsSupported(void))
3443 return VMContinuations ? JNI_TRUE : JNI_FALSE;
3444 JVM_END
3445
3446 JVM_LEAF(jboolean, JVM_IsForeignLinkerSupported(void))
3447 return ForeignGlobals::is_foreign_linker_supported() ? JNI_TRUE : JNI_FALSE;
3448 JVM_END
3449
3450 JVM_ENTRY_NO_ENV(jboolean, JVM_IsStaticallyLinked(void))
3451 return is_vm_statically_linked() ? JNI_TRUE : JNI_FALSE;
3452 JVM_END
3453
3454 // String support ///////////////////////////////////////////////////////////////////////////
3455
3456 JVM_ENTRY(jstring, JVM_InternString(JNIEnv *env, jstring str))
3457 JvmtiVMObjectAllocEventCollector oam;
3458 if (str == nullptr) return nullptr;
3459 oop string = JNIHandles::resolve_non_null(str);
3460 oop result = StringTable::intern(string, CHECK_NULL);
3461 return (jstring) JNIHandles::make_local(THREAD, result);
3507 // the checkPackageAccess relative to the initiating class loader via the
3508 // protection_domain. The protection_domain is passed as null by the java code
3509 // if there is no security manager in 3-arg Class.forName().
3510 Klass* klass = SystemDictionary::resolve_or_fail(name, loader, protection_domain, throwError != 0, CHECK_NULL);
3511
3512 // Check if we should initialize the class
3513 if (init && klass->is_instance_klass()) {
3514 klass->initialize(CHECK_NULL);
3515 }
3516 return (jclass) JNIHandles::make_local(THREAD, klass->java_mirror());
3517 }
3518
3519
3520 // Method ///////////////////////////////////////////////////////////////////////////////////////////
3521
3522 JVM_ENTRY(jobject, JVM_InvokeMethod(JNIEnv *env, jobject method, jobject obj, jobjectArray args0))
3523 Handle method_handle;
3524 if (thread->stack_overflow_state()->stack_available((address) &method_handle) >= JVMInvokeMethodSlack) {
3525 method_handle = Handle(THREAD, JNIHandles::resolve(method));
3526 Handle receiver(THREAD, JNIHandles::resolve(obj));
3527 objArrayHandle args(THREAD, objArrayOop(JNIHandles::resolve(args0)));
3528 oop result = Reflection::invoke_method(method_handle(), receiver, args, CHECK_NULL);
3529 jobject res = JNIHandles::make_local(THREAD, result);
3530 if (JvmtiExport::should_post_vm_object_alloc()) {
3531 oop ret_type = java_lang_reflect_Method::return_type(method_handle());
3532 assert(ret_type != nullptr, "sanity check: ret_type oop must not be null!");
3533 if (java_lang_Class::is_primitive(ret_type)) {
3534 // Only for primitive type vm allocates memory for java object.
3535 // See box() method.
3536 JvmtiExport::post_vm_object_alloc(thread, result);
3537 }
3538 }
3539 return res;
3540 } else {
3541 THROW_NULL(vmSymbols::java_lang_StackOverflowError());
3542 }
3543 JVM_END
3544
3545
3546 JVM_ENTRY(jobject, JVM_NewInstanceFromConstructor(JNIEnv *env, jobject c, jobjectArray args0))
3547 oop constructor_mirror = JNIHandles::resolve(c);
3548 objArrayHandle args(THREAD, objArrayOop(JNIHandles::resolve(args0)));
3549 oop result = Reflection::invoke_constructor(constructor_mirror, args, CHECK_NULL);
3550 jobject res = JNIHandles::make_local(THREAD, result);
3551 if (JvmtiExport::should_post_vm_object_alloc()) {
3552 JvmtiExport::post_vm_object_alloc(thread, result);
3553 }
3554 return res;
3555 JVM_END
3556
3557 JVM_ENTRY(void, JVM_InitializeFromArchive(JNIEnv* env, jclass cls))
3558 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve(cls));
3559 assert(k->is_klass(), "just checking");
3560 HeapShared::initialize_from_archived_subgraph(THREAD, k);
3561 JVM_END
3562
3563 JVM_ENTRY(void, JVM_RegisterLambdaProxyClassForArchiving(JNIEnv* env,
3564 jclass caller,
3565 jstring interfaceMethodName,
3566 jobject factoryType,
3567 jobject interfaceMethodType,
3568 jobject implementationMember,
|
1 /*
2 * Copyright (c) 1997, 2025, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
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 static void validate_array_arguments(Klass* elmClass, jint len, TRAPS) {
432 if (len < 0) {
433 THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), "Array length is negative");
434 }
435 elmClass->initialize(CHECK);
436 if (elmClass->is_identity_class()) {
437 THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), "Element class is not a value class");
438 }
439 if (elmClass->is_abstract()) {
440 THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), "Element class is abstract");
441 }
442 }
443
444 JVM_ENTRY(jarray, JVM_NewNullRestrictedArray(JNIEnv *env, jclass elmClass, jint len))
445 oop mirror = JNIHandles::resolve_non_null(elmClass);
446 Klass* klass = java_lang_Class::as_Klass(mirror);
447 klass->initialize(CHECK_NULL);
448 validate_array_arguments(klass, len, CHECK_NULL);
449 InlineKlass* vk = InlineKlass::cast(klass);
450 if (!vk->is_implicitly_constructible()) {
451 THROW_MSG_NULL(vmSymbols::java_lang_IllegalArgumentException(), "Element class is not implicitly constructible");
452 }
453 oop array = nullptr;
454 if (vk->flat_array()) {
455 array = oopFactory::new_flatArray(vk, len, LayoutKind::NON_ATOMIC_FLAT, CHECK_NULL);
456 } else {
457 array = oopFactory::new_null_free_objArray(vk, len, CHECK_NULL);
458 }
459 return (jarray) JNIHandles::make_local(THREAD, array);
460 JVM_END
461
462 JVM_ENTRY(jarray, JVM_NewNullRestrictedAtomicArray(JNIEnv *env, jclass elmClass, jint len))
463 oop mirror = JNIHandles::resolve_non_null(elmClass);
464 Klass* klass = java_lang_Class::as_Klass(mirror);
465 klass->initialize(CHECK_NULL);
466 validate_array_arguments(klass, len, CHECK_NULL);
467 InlineKlass* vk = InlineKlass::cast(klass);
468 if (!vk->is_implicitly_constructible()) {
469 THROW_MSG_NULL(vmSymbols::java_lang_IllegalArgumentException(), "Element class is not implicitly constructible");
470 }
471 oop array = nullptr;
472 if (UseFlatArray && vk->has_atomic_layout()) {
473 array = oopFactory::new_flatArray(vk, len, LayoutKind::ATOMIC_FLAT, CHECK_NULL);
474 } else if (UseFlatArray && vk->is_naturally_atomic()) {
475 array = oopFactory::new_flatArray(vk, len, LayoutKind::NON_ATOMIC_FLAT, CHECK_NULL);
476 } else {
477 array = oopFactory::new_null_free_objArray(vk, len, CHECK_NULL);
478 }
479 return (jarray) JNIHandles::make_local(THREAD, array);
480 JVM_END
481
482 JVM_ENTRY(jarray, JVM_NewNullableAtomicArray(JNIEnv *env, jclass elmClass, jint len))
483 oop mirror = JNIHandles::resolve_non_null(elmClass);
484 Klass* klass = java_lang_Class::as_Klass(mirror);
485 klass->initialize(CHECK_NULL);
486 validate_array_arguments(klass, len, CHECK_NULL);
487 InlineKlass* vk = InlineKlass::cast(klass);
488 if (!vk->is_implicitly_constructible()) {
489 THROW_MSG_NULL(vmSymbols::java_lang_IllegalArgumentException(), "Element class is not implicitly constructible");
490 }
491 oop array = nullptr;
492 if (UseFlatArray && vk->has_nullable_atomic_layout()) {
493 array = oopFactory::new_flatArray(vk, len, LayoutKind::NULLABLE_ATOMIC_FLAT, CHECK_NULL);
494 } else {
495 array = oopFactory::new_objArray(vk, len, CHECK_NULL);
496 }
497 return (jarray) JNIHandles::make_local(THREAD, array);
498 JVM_END
499
500 JVM_ENTRY(jboolean, JVM_IsFlatArray(JNIEnv *env, jobject obj))
501 arrayOop oop = arrayOop(JNIHandles::resolve_non_null(obj));
502 return oop->is_flatArray();
503 JVM_END
504
505 JVM_ENTRY(jboolean, JVM_IsNullRestrictedArray(JNIEnv *env, jobject obj))
506 arrayOop oop = arrayOop(JNIHandles::resolve_non_null(obj));
507 return oop->is_null_free_array();
508 JVM_END
509
510 // java.lang.Runtime /////////////////////////////////////////////////////////////////////////
511
512 extern volatile jint vm_created;
513
514 JVM_ENTRY_NO_ENV(void, JVM_BeforeHalt())
515 EventShutdown event;
516 if (event.should_commit()) {
517 event.set_reason("Shutdown requested from Java");
518 event.commit();
519 }
520 JVM_END
521
522
523 JVM_ENTRY_NO_ENV(void, JVM_Halt(jint code))
524 before_exit(thread, true);
525 vm_exit(code);
526 JVM_END
527
528
697
698 Handle stackStream_h(THREAD, JNIHandles::resolve_non_null(stackStream));
699 return StackWalk::fetchNextBatch(stackStream_h, mode, anchor, last_batch_count, buffer_size,
700 start_index, frames_array_h, THREAD);
701 JVM_END
702
703 JVM_ENTRY(void, JVM_SetStackWalkContinuation(JNIEnv *env, jobject stackStream, jlong anchor, jobjectArray frames, jobject cont))
704 objArrayOop fa = objArrayOop(JNIHandles::resolve_non_null(frames));
705 objArrayHandle frames_array_h(THREAD, fa);
706 Handle stackStream_h(THREAD, JNIHandles::resolve_non_null(stackStream));
707 Handle cont_h(THREAD, JNIHandles::resolve_non_null(cont));
708
709 StackWalk::setContinuation(stackStream_h, anchor, frames_array_h, cont_h, THREAD);
710 JVM_END
711
712 // java.lang.Object ///////////////////////////////////////////////
713
714
715 JVM_ENTRY(jint, JVM_IHashCode(JNIEnv* env, jobject handle))
716 // as implemented in the classic virtual machine; return 0 if object is null
717 if (handle == nullptr) {
718 return 0;
719 }
720 oop obj = JNIHandles::resolve_non_null(handle);
721 if (EnableValhalla && obj->klass()->is_inline_klass()) {
722 JavaValue result(T_INT);
723 JavaCallArguments args;
724 Handle ho(THREAD, obj);
725 args.push_oop(ho);
726 methodHandle method(THREAD, Universe::value_object_hash_code_method());
727 JavaCalls::call(&result, method, &args, THREAD);
728 if (HAS_PENDING_EXCEPTION) {
729 if (!PENDING_EXCEPTION->is_a(vmClasses::Error_klass())) {
730 Handle e(THREAD, PENDING_EXCEPTION);
731 CLEAR_PENDING_EXCEPTION;
732 THROW_MSG_CAUSE_(vmSymbols::java_lang_InternalError(), "Internal error in hashCode", e, false);
733 }
734 }
735 return result.get_jint();
736 } else {
737 return checked_cast<jint>(ObjectSynchronizer::FastHashCode(THREAD, obj));
738 }
739 JVM_END
740
741
742 JVM_ENTRY(void, JVM_MonitorWait(JNIEnv* env, jobject handle, jlong ms))
743 Handle obj(THREAD, JNIHandles::resolve_non_null(handle));
744 JavaThreadInObjectWaitState jtiows(thread, ms != 0);
745 if (JvmtiExport::should_post_monitor_wait()) {
746 JvmtiExport::post_monitor_wait(thread, obj(), ms);
747
748 // The current thread already owns the monitor and it has not yet
749 // been added to the wait queue so the current thread cannot be
750 // made the successor. This means that the JVMTI_EVENT_MONITOR_WAIT
751 // event handler cannot accidentally consume an unpark() meant for
752 // the ParkEvent associated with this ObjectMonitor.
753 }
754 ObjectSynchronizer::wait(obj, ms, CHECK);
755 JVM_END
756
757
758 JVM_ENTRY(void, JVM_MonitorNotify(JNIEnv* env, jobject handle))
776 // Just checking that the cloneable flag is set correct
777 if (obj->is_array()) {
778 guarantee(klass->is_cloneable(), "all arrays are cloneable");
779 } else {
780 guarantee(obj->is_instance(), "should be instanceOop");
781 bool cloneable = klass->is_subtype_of(vmClasses::Cloneable_klass());
782 guarantee(cloneable == klass->is_cloneable(), "incorrect cloneable flag");
783 }
784 #endif
785
786 // Check if class of obj supports the Cloneable interface.
787 // All arrays are considered to be cloneable (See JLS 20.1.5).
788 // All j.l.r.Reference classes are considered non-cloneable.
789 if (!klass->is_cloneable() ||
790 (klass->is_instance_klass() &&
791 InstanceKlass::cast(klass)->reference_type() != REF_NONE)) {
792 ResourceMark rm(THREAD);
793 THROW_MSG_NULL(vmSymbols::java_lang_CloneNotSupportedException(), klass->external_name());
794 }
795
796 if (klass->is_inline_klass()) {
797 // Value instances have no identity, so return the current instance instead of allocating a new one
798 // Value classes cannot have finalizers, so the method can return immediately
799 return JNIHandles::make_local(THREAD, obj());
800 }
801
802 // Make shallow object copy
803 const size_t size = obj->size();
804 oop new_obj_oop = nullptr;
805 if (obj->is_array()) {
806 const int length = ((arrayOop)obj())->length();
807 new_obj_oop = Universe::heap()->array_allocate(klass, size, length,
808 /* do_zero */ true, CHECK_NULL);
809 } else {
810 new_obj_oop = Universe::heap()->obj_allocate(klass, size, CHECK_NULL);
811 }
812
813 HeapAccess<>::clone(obj(), new_obj_oop, size);
814
815 Handle new_obj(THREAD, new_obj_oop);
816 // Caution: this involves a java upcall, so the clone should be
817 // "gc-robust" by this stage.
818 if (klass->has_finalizer()) {
819 assert(obj->is_instance(), "should be instanceOop");
820 new_obj_oop = InstanceKlass::register_finalizer(instanceOop(new_obj()), CHECK_NULL);
821 new_obj = Handle(THREAD, new_obj_oop);
1292 oop result = java_lang_Class::name(java_class, CHECK_NULL);
1293 return (jstring) JNIHandles::make_local(THREAD, result);
1294 JVM_END
1295
1296
1297 JVM_ENTRY(jobjectArray, JVM_GetClassInterfaces(JNIEnv *env, jclass cls))
1298 JvmtiVMObjectAllocEventCollector oam;
1299 oop mirror = JNIHandles::resolve_non_null(cls);
1300
1301 // Special handling for primitive objects
1302 if (java_lang_Class::is_primitive(mirror)) {
1303 // Primitive objects does not have any interfaces
1304 objArrayOop r = oopFactory::new_objArray(vmClasses::Class_klass(), 0, CHECK_NULL);
1305 return (jobjectArray) JNIHandles::make_local(THREAD, r);
1306 }
1307
1308 Klass* klass = java_lang_Class::as_Klass(mirror);
1309 // Figure size of result array
1310 int size;
1311 if (klass->is_instance_klass()) {
1312 InstanceKlass* ik = InstanceKlass::cast(klass);
1313 size = ik->local_interfaces()->length();
1314 } else {
1315 assert(klass->is_objArray_klass() || klass->is_typeArray_klass() || klass->is_flatArray_klass(), "Illegal mirror klass");
1316 size = 2;
1317 }
1318
1319 // Allocate result array
1320 objArrayOop r = oopFactory::new_objArray(vmClasses::Class_klass(), size, CHECK_NULL);
1321 objArrayHandle result (THREAD, r);
1322 // Fill in result
1323 if (klass->is_instance_klass()) {
1324 // Regular instance klass, fill in all local interfaces
1325 for (int index = 0; index < size; index++) {
1326 InstanceKlass* ik = InstanceKlass::cast(klass);
1327 Klass* k = ik->local_interfaces()->at(index);
1328 result->obj_at_put(index, k->java_mirror());
1329 }
1330 } else {
1331 // All arrays implement java.lang.Cloneable and java.io.Serializable
1332 result->obj_at_put(0, vmClasses::Cloneable_klass()->java_mirror());
1333 result->obj_at_put(1, vmClasses::Serializable_klass()->java_mirror());
1334 }
1335 return (jobjectArray) JNIHandles::make_local(THREAD, result());
1336 JVM_END
1337
1338
1339 JVM_ENTRY(jboolean, JVM_IsInterface(JNIEnv *env, jclass cls))
1340 oop mirror = JNIHandles::resolve_non_null(cls);
1341 if (java_lang_Class::is_primitive(mirror)) {
1342 return JNI_FALSE;
1343 }
1344 Klass* k = java_lang_Class::as_Klass(mirror);
1345 jboolean result = k->is_interface();
1346 assert(!result || k->is_instance_klass(),
1347 "all interfaces are instance types");
1348 // The compiler intrinsic for isInterface tests the
1349 // Klass::_access_flags bits in the same way.
1350 return result;
1351 JVM_END
1352
1353 JVM_ENTRY(jboolean, JVM_IsHiddenClass(JNIEnv *env, jclass cls))
1354 oop mirror = JNIHandles::resolve_non_null(cls);
1355 if (java_lang_Class::is_primitive(mirror)) {
1356 return JNI_FALSE;
1357 }
1358 Klass* k = java_lang_Class::as_Klass(mirror);
1359 return k->is_hidden();
1360 JVM_END
1361
1362 JVM_ENTRY(jboolean, JVM_IsIdentityClass(JNIEnv *env, jclass cls))
1363 oop mirror = JNIHandles::resolve_non_null(cls);
1364 if (java_lang_Class::is_primitive(mirror)) {
1365 return JNI_FALSE;
1366 }
1367 Klass* k = java_lang_Class::as_Klass(mirror);
1368 if (EnableValhalla) {
1369 return k->is_array_klass() || k->is_identity_class();
1370 } else {
1371 return k->is_interface() ? JNI_FALSE : JNI_TRUE;
1372 }
1373 JVM_END
1374
1375 JVM_ENTRY(jboolean, JVM_IsImplicitlyConstructibleClass(JNIEnv *env, jclass cls))
1376 oop mirror = JNIHandles::resolve_non_null(cls);
1377 InstanceKlass* ik = InstanceKlass::cast(java_lang_Class::as_Klass(mirror));
1378 return ik->is_implicitly_constructible();
1379 JVM_END
1380
1381
1382 JVM_ENTRY(jobject, JVM_GetProtectionDomain(JNIEnv *env, jclass cls))
1383 oop mirror = JNIHandles::resolve_non_null(cls);
1384 if (mirror == nullptr) {
1385 THROW_(vmSymbols::java_lang_NullPointerException(), nullptr);
1386 }
1387
1388 if (java_lang_Class::is_primitive(mirror)) {
1389 // Primitive types does not have a protection domain.
1390 return nullptr;
1391 }
1392
1393 oop pd = java_lang_Class::protection_domain(mirror);
1394 return (jobject) JNIHandles::make_local(THREAD, pd);
1395 JVM_END
1396
1397
1398 // Returns the inherited_access_control_context field of the running thread.
1399 JVM_ENTRY(jobject, JVM_GetInheritedAccessControlContext(JNIEnv *env, jclass cls))
1400 oop result = java_lang_Thread::inherited_access_control_context(thread->threadObj());
1970 }
1971
1972 InstanceKlass* k = InstanceKlass::cast(java_lang_Class::as_Klass(ofMirror));
1973
1974 // Ensure class is linked
1975 k->link_class(CHECK_NULL);
1976
1977 Array<Method*>* methods = k->methods();
1978 int methods_length = methods->length();
1979
1980 // Save original method_idnum in case of redefinition, which can change
1981 // the idnum of obsolete methods. The new method will have the same idnum
1982 // but if we refresh the methods array, the counts will be wrong.
1983 ResourceMark rm(THREAD);
1984 GrowableArray<int>* idnums = new GrowableArray<int>(methods_length);
1985 int num_methods = 0;
1986
1987 // Select methods matching the criteria.
1988 for (int i = 0; i < methods_length; i++) {
1989 Method* method = methods->at(i);
1990 if (want_constructor && !method->is_object_constructor()) {
1991 continue;
1992 }
1993 if (!want_constructor &&
1994 (method->is_object_constructor() || method->is_class_initializer() ||
1995 method->is_overpass())) {
1996 continue;
1997 }
1998 if (publicOnly && !method->is_public()) {
1999 continue;
2000 }
2001 idnums->push(method->method_idnum());
2002 ++num_methods;
2003 }
2004
2005 // Allocate result
2006 objArrayOop r = oopFactory::new_objArray(klass, num_methods, CHECK_NULL);
2007 objArrayHandle result (THREAD, r);
2008
2009 // Now just put the methods that we selected above, but go by their idnum
2010 // in case of redefinition. The methods can be redefined at any safepoint,
2011 // so above when allocating the oop array and below when creating reflect
2012 // objects.
2013 for (int i = 0; i < num_methods; i++) {
2014 methodHandle method(THREAD, k->method_with_idnum(idnums->at(i)));
2015 if (method.is_null()) {
2016 // Method may have been deleted and seems this API can handle null
2017 // Otherwise should probably put a method that throws NSME
2018 result->obj_at_put(i, nullptr);
2019 } else {
2020 oop m;
2021 if (want_constructor) {
2022 assert(method->is_object_constructor(), "must be");
2023 m = Reflection::new_constructor(method, CHECK_NULL);
2024 } else {
2025 m = Reflection::new_method(method, false, CHECK_NULL);
2026 }
2027 result->obj_at_put(i, m);
2028 }
2029 }
2030
2031 return (jobjectArray) JNIHandles::make_local(THREAD, result());
2032 }
2033
2034 JVM_ENTRY(jobjectArray, JVM_GetClassDeclaredMethods(JNIEnv *env, jclass ofClass, jboolean publicOnly))
2035 {
2036 return get_class_declared_methods_helper(env, ofClass, publicOnly,
2037 /*want_constructor*/ false,
2038 vmClasses::reflect_Method_klass(), THREAD);
2039 }
2040 JVM_END
2041
2042 JVM_ENTRY(jobjectArray, JVM_GetClassDeclaredConstructors(JNIEnv *env, jclass ofClass, jboolean publicOnly))
2285 constantTag tag = cp->tag_at(index);
2286 if (!tag.is_method() && !tag.is_interface_method()) {
2287 THROW_MSG_NULL(vmSymbols::java_lang_IllegalArgumentException(), "Wrong type at constant pool index");
2288 }
2289 int klass_ref = cp->uncached_klass_ref_index_at(index);
2290 Klass* k_o;
2291 if (force_resolution) {
2292 k_o = cp->klass_at(klass_ref, CHECK_NULL);
2293 } else {
2294 k_o = ConstantPool::klass_at_if_loaded(cp, klass_ref);
2295 if (k_o == nullptr) return nullptr;
2296 }
2297 InstanceKlass* k = InstanceKlass::cast(k_o);
2298 Symbol* name = cp->uncached_name_ref_at(index);
2299 Symbol* sig = cp->uncached_signature_ref_at(index);
2300 methodHandle m (THREAD, k->find_method(name, sig));
2301 if (m.is_null()) {
2302 THROW_MSG_NULL(vmSymbols::java_lang_RuntimeException(), "Unable to look up method in target class");
2303 }
2304 oop method;
2305 if (m->is_object_constructor()) {
2306 method = Reflection::new_constructor(m, CHECK_NULL);
2307 } else {
2308 method = Reflection::new_method(m, true, CHECK_NULL);
2309 }
2310 return JNIHandles::make_local(THREAD, method);
2311 }
2312
2313 JVM_ENTRY(jobject, JVM_ConstantPoolGetMethodAt(JNIEnv *env, jobject obj, jobject unused, jint index))
2314 {
2315 JvmtiVMObjectAllocEventCollector oam;
2316 constantPoolHandle cp = constantPoolHandle(THREAD, reflect_ConstantPool::get_cp(JNIHandles::resolve_non_null(obj)));
2317 bounds_check(cp, index, CHECK_NULL);
2318 jobject res = get_method_at_helper(cp, index, true, CHECK_NULL);
2319 return res;
2320 }
2321 JVM_END
2322
2323 JVM_ENTRY(jobject, JVM_ConstantPoolGetMethodAtIfLoaded(JNIEnv *env, jobject obj, jobject unused, jint index))
2324 {
2325 JvmtiVMObjectAllocEventCollector oam;
2326 constantPoolHandle cp = constantPoolHandle(THREAD, reflect_ConstantPool::get_cp(JNIHandles::resolve_non_null(obj)));
2327 bounds_check(cp, index, CHECK_NULL);
2559 Klass* k = java_lang_Class::as_Klass(r);
2560 assert(k->is_instance_klass(), "must be an instance klass");
2561 if (!k->is_instance_klass()) return false;
2562
2563 ResourceMark rm(THREAD);
2564 const char* name = k->name()->as_C_string();
2565 bool system_class = k->class_loader() == nullptr;
2566 return JavaAssertions::enabled(name, system_class);
2567
2568 JVM_END
2569
2570
2571 // Return a new AssertionStatusDirectives object with the fields filled in with
2572 // command-line assertion arguments (i.e., -ea, -da).
2573 JVM_ENTRY(jobject, JVM_AssertionStatusDirectives(JNIEnv *env, jclass unused))
2574 JvmtiVMObjectAllocEventCollector oam;
2575 oop asd = JavaAssertions::createAssertionStatusDirectives(CHECK_NULL);
2576 return JNIHandles::make_local(THREAD, asd);
2577 JVM_END
2578
2579 // Arrays support /////////////////////////////////////////////////////////////
2580
2581 JVM_ENTRY(jboolean, JVM_ArrayIsAccessAtomic(JNIEnv *env, jclass unused, jobject array))
2582 oop o = JNIHandles::resolve(array);
2583 Klass* k = o->klass();
2584 if ((o == nullptr) || (!k->is_array_klass())) {
2585 THROW_0(vmSymbols::java_lang_IllegalArgumentException());
2586 }
2587 return ArrayKlass::cast(k)->element_access_must_be_atomic();
2588 JVM_END
2589
2590 JVM_ENTRY(jobject, JVM_ArrayEnsureAccessAtomic(JNIEnv *env, jclass unused, jobject array))
2591 oop o = JNIHandles::resolve(array);
2592 Klass* k = o->klass();
2593 if ((o == nullptr) || (!k->is_array_klass())) {
2594 THROW_0(vmSymbols::java_lang_IllegalArgumentException());
2595 }
2596 if (k->is_flatArray_klass()) {
2597 FlatArrayKlass* vk = FlatArrayKlass::cast(k);
2598 if (!vk->element_access_must_be_atomic()) {
2599 /**
2600 * Need to decide how to implement:
2601 *
2602 * 1) Change to objArrayOop layout, therefore oop->klass() differs so
2603 * then "<atomic>[Qfoo;" klass needs to subclass "[Qfoo;" to pass through
2604 * "checkcast" & "instanceof"
2605 *
2606 * 2) Use extra header in the flatArrayOop to flag atomicity required and
2607 * possibly per instance lock structure. Said info, could be placed in
2608 * "trailer" rather than disturb the current arrayOop
2609 */
2610 Unimplemented();
2611 }
2612 }
2613 return array;
2614 JVM_END
2615
2616 // Verification ////////////////////////////////////////////////////////////////////////////////
2617
2618 // Reflection for the verifier /////////////////////////////////////////////////////////////////
2619
2620 // RedefineClasses support: bug 6214132 caused verification to fail.
2621 // All functions from this section should call the jvmtiThreadSate function:
2622 // Klass* class_to_verify_considering_redefinition(Klass* klass).
2623 // The function returns a Klass* of the _scratch_class if the verifier
2624 // was invoked in the middle of the class redefinition.
2625 // Otherwise it returns its argument value which is the _the_class Klass*.
2626 // Please, refer to the description in the jvmtiThreadSate.hpp.
2627
2628 JVM_ENTRY(const char*, JVM_GetClassNameUTF(JNIEnv *env, jclass cls))
2629 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls));
2630 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
2631 return k->name()->as_utf8();
2632 JVM_END
2633
2634
2635 JVM_ENTRY(void, JVM_GetClassCPTypes(JNIEnv *env, jclass cls, unsigned char *types))
2725
2726 JVM_ENTRY(jint, JVM_GetMethodIxExceptionTableLength(JNIEnv *env, jclass cls, int method_index))
2727 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls));
2728 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
2729 Method* method = InstanceKlass::cast(k)->methods()->at(method_index);
2730 return method->exception_table_length();
2731 JVM_END
2732
2733
2734 JVM_ENTRY(jint, JVM_GetMethodIxModifiers(JNIEnv *env, jclass cls, int method_index))
2735 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls));
2736 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
2737 Method* method = InstanceKlass::cast(k)->methods()->at(method_index);
2738 return method->access_flags().as_int() & JVM_RECOGNIZED_METHOD_MODIFIERS;
2739 JVM_END
2740
2741
2742 JVM_ENTRY(jint, JVM_GetFieldIxModifiers(JNIEnv *env, jclass cls, int field_index))
2743 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls));
2744 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
2745 return InstanceKlass::cast(k)->field_access_flags(field_index);
2746 JVM_END
2747
2748
2749 JVM_ENTRY(jint, JVM_GetMethodIxLocalsCount(JNIEnv *env, jclass cls, int method_index))
2750 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls));
2751 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
2752 Method* method = InstanceKlass::cast(k)->methods()->at(method_index);
2753 return method->max_locals();
2754 JVM_END
2755
2756
2757 JVM_ENTRY(jint, JVM_GetMethodIxArgsSize(JNIEnv *env, jclass cls, int method_index))
2758 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls));
2759 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
2760 Method* method = InstanceKlass::cast(k)->methods()->at(method_index);
2761 return method->size_of_parameters();
2762 JVM_END
2763
2764
2765 JVM_ENTRY(jint, JVM_GetMethodIxMaxStack(JNIEnv *env, jclass cls, int method_index))
2766 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls));
2767 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
2768 Method* method = InstanceKlass::cast(k)->methods()->at(method_index);
2769 return method->verifier_max_stack();
2770 JVM_END
2771
2772
2773 JVM_ENTRY(jboolean, JVM_IsConstructorIx(JNIEnv *env, jclass cls, int method_index))
2774 ResourceMark rm(THREAD);
2775 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls));
2776 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
2777 Method* method = InstanceKlass::cast(k)->methods()->at(method_index);
2778 return method->is_object_constructor();
2779 JVM_END
2780
2781
2782 JVM_ENTRY(jboolean, JVM_IsVMGeneratedMethodIx(JNIEnv *env, jclass cls, int method_index))
2783 ResourceMark rm(THREAD);
2784 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls));
2785 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
2786 Method* method = InstanceKlass::cast(k)->methods()->at(method_index);
2787 return method->is_overpass();
2788 JVM_END
2789
2790 JVM_ENTRY(const char*, JVM_GetMethodIxNameUTF(JNIEnv *env, jclass cls, jint method_index))
2791 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls));
2792 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
2793 Method* method = InstanceKlass::cast(k)->methods()->at(method_index);
2794 return method->name()->as_utf8();
2795 JVM_END
2796
2797
2798 JVM_ENTRY(const char*, JVM_GetMethodIxSignatureUTF(JNIEnv *env, jclass cls, jint method_index))
2915 }
2916 ShouldNotReachHere();
2917 return nullptr;
2918 JVM_END
2919
2920
2921 JVM_ENTRY(jint, JVM_GetCPFieldModifiers(JNIEnv *env, jclass cls, int cp_index, jclass called_cls))
2922 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls));
2923 Klass* k_called = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(called_cls));
2924 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
2925 k_called = JvmtiThreadState::class_to_verify_considering_redefinition(k_called, thread);
2926 ConstantPool* cp = InstanceKlass::cast(k)->constants();
2927 ConstantPool* cp_called = InstanceKlass::cast(k_called)->constants();
2928 switch (cp->tag_at(cp_index).value()) {
2929 case JVM_CONSTANT_Fieldref: {
2930 Symbol* name = cp->uncached_name_ref_at(cp_index);
2931 Symbol* signature = cp->uncached_signature_ref_at(cp_index);
2932 InstanceKlass* ik = InstanceKlass::cast(k_called);
2933 for (JavaFieldStream fs(ik); !fs.done(); fs.next()) {
2934 if (fs.name() == name && fs.signature() == signature) {
2935 return fs.access_flags().as_short();
2936 }
2937 }
2938 return -1;
2939 }
2940 default:
2941 fatal("JVM_GetCPFieldModifiers: illegal constant");
2942 }
2943 ShouldNotReachHere();
2944 return 0;
2945 JVM_END
2946
2947
2948 JVM_ENTRY(jint, JVM_GetCPMethodModifiers(JNIEnv *env, jclass cls, int cp_index, jclass called_cls))
2949 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls));
2950 Klass* k_called = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(called_cls));
2951 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
2952 k_called = JvmtiThreadState::class_to_verify_considering_redefinition(k_called, thread);
2953 ConstantPool* cp = InstanceKlass::cast(k)->constants();
2954 switch (cp->tag_at(cp_index).value()) {
2955 case JVM_CONSTANT_Methodref:
3585 JVM_LEAF(void*, JVM_FindLibraryEntry(void* handle, const char* name))
3586 void* find_result = os::dll_lookup(handle, name);
3587 log_info(library)("%s %s in library with handle " INTPTR_FORMAT,
3588 find_result != nullptr ? "Found" : "Failed to find",
3589 name, p2i(handle));
3590 return find_result;
3591 JVM_END
3592
3593
3594 // JNI version ///////////////////////////////////////////////////////////////////////////////
3595
3596 JVM_LEAF(jboolean, JVM_IsSupportedJNIVersion(jint version))
3597 return Threads::is_supported_jni_version_including_1_1(version);
3598 JVM_END
3599
3600
3601 JVM_LEAF(jboolean, JVM_IsPreviewEnabled(void))
3602 return Arguments::enable_preview() ? JNI_TRUE : JNI_FALSE;
3603 JVM_END
3604
3605 JVM_LEAF(jboolean, JVM_IsValhallaEnabled(void))
3606 return EnableValhalla ? JNI_TRUE : JNI_FALSE;
3607 JVM_END
3608
3609 JVM_LEAF(jboolean, JVM_IsContinuationsSupported(void))
3610 return VMContinuations ? JNI_TRUE : JNI_FALSE;
3611 JVM_END
3612
3613 JVM_LEAF(jboolean, JVM_IsForeignLinkerSupported(void))
3614 return ForeignGlobals::is_foreign_linker_supported() ? JNI_TRUE : JNI_FALSE;
3615 JVM_END
3616
3617 JVM_ENTRY_NO_ENV(jboolean, JVM_IsStaticallyLinked(void))
3618 return is_vm_statically_linked() ? JNI_TRUE : JNI_FALSE;
3619 JVM_END
3620
3621 // String support ///////////////////////////////////////////////////////////////////////////
3622
3623 JVM_ENTRY(jstring, JVM_InternString(JNIEnv *env, jstring str))
3624 JvmtiVMObjectAllocEventCollector oam;
3625 if (str == nullptr) return nullptr;
3626 oop string = JNIHandles::resolve_non_null(str);
3627 oop result = StringTable::intern(string, CHECK_NULL);
3628 return (jstring) JNIHandles::make_local(THREAD, result);
3674 // the checkPackageAccess relative to the initiating class loader via the
3675 // protection_domain. The protection_domain is passed as null by the java code
3676 // if there is no security manager in 3-arg Class.forName().
3677 Klass* klass = SystemDictionary::resolve_or_fail(name, loader, protection_domain, throwError != 0, CHECK_NULL);
3678
3679 // Check if we should initialize the class
3680 if (init && klass->is_instance_klass()) {
3681 klass->initialize(CHECK_NULL);
3682 }
3683 return (jclass) JNIHandles::make_local(THREAD, klass->java_mirror());
3684 }
3685
3686
3687 // Method ///////////////////////////////////////////////////////////////////////////////////////////
3688
3689 JVM_ENTRY(jobject, JVM_InvokeMethod(JNIEnv *env, jobject method, jobject obj, jobjectArray args0))
3690 Handle method_handle;
3691 if (thread->stack_overflow_state()->stack_available((address) &method_handle) >= JVMInvokeMethodSlack) {
3692 method_handle = Handle(THREAD, JNIHandles::resolve(method));
3693 Handle receiver(THREAD, JNIHandles::resolve(obj));
3694 objArrayHandle args = oopFactory::ensure_objArray(JNIHandles::resolve(args0), CHECK_NULL);
3695 oop result = Reflection::invoke_method(method_handle(), receiver, args, CHECK_NULL);
3696 jobject res = JNIHandles::make_local(THREAD, result);
3697 if (JvmtiExport::should_post_vm_object_alloc()) {
3698 oop ret_type = java_lang_reflect_Method::return_type(method_handle());
3699 assert(ret_type != nullptr, "sanity check: ret_type oop must not be null!");
3700 if (java_lang_Class::is_primitive(ret_type)) {
3701 // Only for primitive type vm allocates memory for java object.
3702 // See box() method.
3703 JvmtiExport::post_vm_object_alloc(thread, result);
3704 }
3705 }
3706 return res;
3707 } else {
3708 THROW_NULL(vmSymbols::java_lang_StackOverflowError());
3709 }
3710 JVM_END
3711
3712
3713 JVM_ENTRY(jobject, JVM_NewInstanceFromConstructor(JNIEnv *env, jobject c, jobjectArray args0))
3714 objArrayHandle args = oopFactory::ensure_objArray(JNIHandles::resolve(args0), CHECK_NULL);
3715 oop constructor_mirror = JNIHandles::resolve(c);
3716 oop result = Reflection::invoke_constructor(constructor_mirror, args, CHECK_NULL);
3717 jobject res = JNIHandles::make_local(THREAD, result);
3718 if (JvmtiExport::should_post_vm_object_alloc()) {
3719 JvmtiExport::post_vm_object_alloc(thread, result);
3720 }
3721 return res;
3722 JVM_END
3723
3724 JVM_ENTRY(void, JVM_InitializeFromArchive(JNIEnv* env, jclass cls))
3725 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve(cls));
3726 assert(k->is_klass(), "just checking");
3727 HeapShared::initialize_from_archived_subgraph(THREAD, k);
3728 JVM_END
3729
3730 JVM_ENTRY(void, JVM_RegisterLambdaProxyClassForArchiving(JNIEnv* env,
3731 jclass caller,
3732 jstring interfaceMethodName,
3733 jobject factoryType,
3734 jobject interfaceMethodType,
3735 jobject implementationMember,
|