41 #include "classfile/modules.hpp"
42 #include "classfile/packageEntry.hpp"
43 #include "classfile/stringTable.hpp"
44 #include "classfile/symbolTable.hpp"
45 #include "classfile/systemDictionary.hpp"
46 #include "classfile/vmClasses.hpp"
47 #include "classfile/vmSymbols.hpp"
48 #include "gc/shared/collectedHeap.inline.hpp"
49 #include "interpreter/bytecode.hpp"
50 #include "interpreter/bytecodeUtils.hpp"
51 #include "jfr/jfrEvents.hpp"
52 #include "jvm.h"
53 #include "logging/log.hpp"
54 #include "memory/oopFactory.hpp"
55 #include "memory/referenceType.hpp"
56 #include "memory/resourceArea.hpp"
57 #include "memory/universe.hpp"
58 #include "oops/access.inline.hpp"
59 #include "oops/constantPool.hpp"
60 #include "oops/fieldStreams.inline.hpp"
61 #include "oops/instanceKlass.hpp"
62 #include "oops/klass.inline.hpp"
63 #include "oops/method.hpp"
64 #include "oops/objArrayKlass.hpp"
65 #include "oops/objArrayOop.inline.hpp"
66 #include "oops/oop.inline.hpp"
67 #include "oops/recordComponent.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/atomicAccess.hpp"
75 #include "runtime/continuation.hpp"
76 #include "runtime/deoptimization.hpp"
77 #include "runtime/globals_extension.hpp"
78 #include "runtime/handles.inline.hpp"
79 #include "runtime/handshake.hpp"
80 #include "runtime/init.hpp"
81 #include "runtime/interfaceSupport.inline.hpp"
82 #include "runtime/java.hpp"
83 #include "runtime/javaCalls.hpp"
84 #include "runtime/javaThread.hpp"
85 #include "runtime/jfieldIDWorkaround.hpp"
86 #include "runtime/jniHandles.inline.hpp"
87 #include "runtime/mountUnmountDisabler.hpp"
405
406 return (jobjectArray) JNIHandles::make_local(THREAD, result_h());
407 JVM_END
408
409
410 /*
411 * Return the temporary directory that the VM uses for the attach
412 * and perf data files.
413 *
414 * It is important that this directory is well-known and the
415 * same for all VM instances. It cannot be affected by configuration
416 * variables such as java.io.tmpdir.
417 */
418 JVM_ENTRY(jstring, JVM_GetTemporaryDirectory(JNIEnv *env))
419 HandleMark hm(THREAD);
420 const char* temp_dir = os::get_temp_directory();
421 Handle h = java_lang_String::create_from_platform_dependent_str(temp_dir, CHECK_NULL);
422 return (jstring) JNIHandles::make_local(THREAD, h());
423 JVM_END
424
425
426 // java.lang.Runtime /////////////////////////////////////////////////////////////////////////
427
428 extern volatile jint vm_created;
429
430 JVM_ENTRY_NO_ENV(void, JVM_BeforeHalt())
431 EventShutdown event;
432 if (event.should_commit()) {
433 event.set_reason("Shutdown requested from Java");
434 event.commit();
435 }
436 JVM_END
437
438
439 JVM_ENTRY_NO_ENV(void, JVM_Halt(jint code))
440 before_exit(thread, true);
441 vm_exit(code);
442 JVM_END
443
444
522 }
523 if (method->is_native()) {
524 return nullptr;
525 }
526
527 stringStream ss;
528 bool ok = BytecodeUtils::get_NPE_message_at(&ss, method, bci);
529 if (ok) {
530 oop result = java_lang_String::create_oop_from_str(ss.base(), CHECK_NULL);
531 return (jstring) JNIHandles::make_local(THREAD, result);
532 } else {
533 return nullptr;
534 }
535 JVM_END
536
537 // java.lang.StackTraceElement //////////////////////////////////////////////
538
539
540 JVM_ENTRY(void, JVM_InitStackTraceElementArray(JNIEnv *env, jobjectArray elements, jobject backtrace, jint depth))
541 Handle backtraceh(THREAD, JNIHandles::resolve(backtrace));
542 objArrayOop st = objArrayOop(JNIHandles::resolve(elements));
543 objArrayHandle stack_trace(THREAD, st);
544 // Fill in the allocated stack trace
545 java_lang_Throwable::get_stack_trace_elements(depth, backtraceh, stack_trace, CHECK);
546 JVM_END
547
548
549 JVM_ENTRY(void, JVM_InitStackTraceElement(JNIEnv* env, jobject element, jobject stackFrameInfo))
550 Handle stack_frame_info(THREAD, JNIHandles::resolve_non_null(stackFrameInfo));
551 Handle stack_trace_element(THREAD, JNIHandles::resolve_non_null(element));
552 java_lang_StackFrameInfo::to_stack_trace_element(stack_frame_info, stack_trace_element, CHECK);
553 JVM_END
554
555
556 // java.lang.StackWalker //////////////////////////////////////////////////////
557 JVM_ENTRY(void, JVM_ExpandStackFrameInfo(JNIEnv *env, jobject obj))
558 Handle stack_frame_info(THREAD, JNIHandles::resolve_non_null(obj));
559
560 bool have_name = (java_lang_StackFrameInfo::name(stack_frame_info()) != nullptr);
561 bool have_type = (java_lang_StackFrameInfo::type(stack_frame_info()) != nullptr);
562 Method* method = java_lang_StackFrameInfo::get_method(stack_frame_info());
563 if (!have_name) {
566 }
567 if (!have_type) {
568 Handle type = java_lang_String::create_from_symbol(method->signature(), CHECK);
569 java_lang_StackFrameInfo::set_type(stack_frame_info(), type());
570 }
571 JVM_END
572
573 JVM_ENTRY(jobject, JVM_CallStackWalk(JNIEnv *env, jobject stackStream, jint mode,
574 jint skip_frames, jobject contScope, jobject cont,
575 jint buffer_size, jint start_index, jobjectArray frames))
576 if (!thread->has_last_Java_frame()) {
577 THROW_MSG_(vmSymbols::java_lang_InternalError(), "doStackWalk: no stack trace", nullptr);
578 }
579
580 Handle stackStream_h(THREAD, JNIHandles::resolve_non_null(stackStream));
581 Handle contScope_h(THREAD, JNIHandles::resolve(contScope));
582 Handle cont_h(THREAD, JNIHandles::resolve(cont));
583 // frames array is a ClassFrameInfo[] array when only getting caller reference,
584 // and a StackFrameInfo[] array (or derivative) otherwise. It should never
585 // be null.
586 objArrayOop fa = objArrayOop(JNIHandles::resolve_non_null(frames));
587 objArrayHandle frames_array_h(THREAD, fa);
588
589 if (frames_array_h->length() < buffer_size) {
590 THROW_MSG_(vmSymbols::java_lang_IllegalArgumentException(), "not enough space in buffers", nullptr);
591 }
592
593 oop result = StackWalk::walk(stackStream_h, mode, skip_frames, contScope_h, cont_h,
594 buffer_size, start_index, frames_array_h, CHECK_NULL);
595 return JNIHandles::make_local(THREAD, result);
596 JVM_END
597
598
599 JVM_ENTRY(jint, JVM_MoreStackWalk(JNIEnv *env, jobject stackStream, jint mode, jlong anchor,
600 jint last_batch_count, jint buffer_size, jint start_index,
601 jobjectArray frames))
602 // frames array is a ClassFrameInfo[] array when only getting caller reference,
603 // and a StackFrameInfo[] array (or derivative) otherwise. It should never
604 // be null.
605 objArrayOop fa = objArrayOop(JNIHandles::resolve_non_null(frames));
606 objArrayHandle frames_array_h(THREAD, fa);
607
608 if (frames_array_h->length() < buffer_size) {
609 THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(), "not enough space in buffers");
610 }
611
612 Handle stackStream_h(THREAD, JNIHandles::resolve_non_null(stackStream));
613 return StackWalk::fetchNextBatch(stackStream_h, mode, anchor, last_batch_count, buffer_size,
614 start_index, frames_array_h, THREAD);
615 JVM_END
616
617 JVM_ENTRY(void, JVM_SetStackWalkContinuation(JNIEnv *env, jobject stackStream, jlong anchor, jobjectArray frames, jobject cont))
618 objArrayOop fa = objArrayOop(JNIHandles::resolve_non_null(frames));
619 objArrayHandle frames_array_h(THREAD, fa);
620 Handle stackStream_h(THREAD, JNIHandles::resolve_non_null(stackStream));
621 Handle cont_h(THREAD, JNIHandles::resolve_non_null(cont));
622
623 StackWalk::setContinuation(stackStream_h, anchor, frames_array_h, cont_h, THREAD);
624 JVM_END
625
626 // java.lang.Object ///////////////////////////////////////////////
627
628
629 JVM_ENTRY(jint, JVM_IHashCode(JNIEnv* env, jobject handle))
630 // as implemented in the classic virtual machine; return 0 if object is null
631 return handle == nullptr ? 0 :
632 checked_cast<jint>(ObjectSynchronizer::FastHashCode (THREAD, JNIHandles::resolve_non_null(handle)));
633 JVM_END
634
635
636 JVM_ENTRY(void, JVM_MonitorWait(JNIEnv* env, jobject handle, jlong ms))
637 Handle obj(THREAD, JNIHandles::resolve_non_null(handle));
638 ObjectSynchronizer::wait(obj, ms, CHECK);
639 JVM_END
640
641
642 JVM_ENTRY(void, JVM_MonitorNotify(JNIEnv* env, jobject handle))
643 Handle obj(THREAD, JNIHandles::resolve_non_null(handle));
644 ObjectSynchronizer::notify(obj, CHECK);
645 JVM_END
646
647
648 JVM_ENTRY(void, JVM_MonitorNotifyAll(JNIEnv* env, jobject handle))
649 Handle obj(THREAD, JNIHandles::resolve_non_null(handle));
650 ObjectSynchronizer::notifyall(obj, CHECK);
651 JVM_END
652
660 // Just checking that the cloneable flag is set correct
661 if (obj->is_array()) {
662 guarantee(klass->is_cloneable(), "all arrays are cloneable");
663 } else {
664 guarantee(obj->is_instance(), "should be instanceOop");
665 bool cloneable = klass->is_subtype_of(vmClasses::Cloneable_klass());
666 guarantee(cloneable == klass->is_cloneable(), "incorrect cloneable flag");
667 }
668 #endif
669
670 // Check if class of obj supports the Cloneable interface.
671 // All arrays are considered to be cloneable (See JLS 20.1.5).
672 // All j.l.r.Reference classes are considered non-cloneable.
673 if (!klass->is_cloneable() ||
674 (klass->is_instance_klass() &&
675 InstanceKlass::cast(klass)->reference_type() != REF_NONE)) {
676 ResourceMark rm(THREAD);
677 THROW_MSG_NULL(vmSymbols::java_lang_CloneNotSupportedException(), klass->external_name());
678 }
679
680 // Make shallow object copy
681 const size_t size = obj->size();
682 oop new_obj_oop = nullptr;
683 if (obj->is_array()) {
684 const int length = ((arrayOop)obj())->length();
685 new_obj_oop = Universe::heap()->array_allocate(klass, size, length,
686 /* do_zero */ true, CHECK_NULL);
687 } else {
688 new_obj_oop = Universe::heap()->obj_allocate(klass, size, CHECK_NULL);
689 }
690
691 HeapAccess<>::clone(obj(), new_obj_oop, size);
692
693 Handle new_obj(THREAD, new_obj_oop);
694 // Caution: this involves a java upcall, so the clone should be
695 // "gc-robust" by this stage.
696 if (klass->has_finalizer()) {
697 assert(obj->is_instance(), "should be instanceOop");
698 new_obj_oop = InstanceKlass::register_finalizer(instanceOop(new_obj()), CHECK_NULL);
699 new_obj = Handle(THREAD, new_obj_oop);
1152 oop result = java_lang_Class::name(java_class, CHECK_NULL);
1153 return (jstring) JNIHandles::make_local(THREAD, result);
1154 JVM_END
1155
1156
1157 JVM_ENTRY(jobjectArray, JVM_GetClassInterfaces(JNIEnv *env, jclass cls))
1158 JvmtiVMObjectAllocEventCollector oam;
1159 oop mirror = JNIHandles::resolve_non_null(cls);
1160
1161 // Special handling for primitive objects
1162 if (java_lang_Class::is_primitive(mirror)) {
1163 // Primitive objects does not have any interfaces
1164 objArrayOop r = oopFactory::new_objArray(vmClasses::Class_klass(), 0, CHECK_NULL);
1165 return (jobjectArray) JNIHandles::make_local(THREAD, r);
1166 }
1167
1168 Klass* klass = java_lang_Class::as_Klass(mirror);
1169 // Figure size of result array
1170 int size;
1171 if (klass->is_instance_klass()) {
1172 size = InstanceKlass::cast(klass)->local_interfaces()->length();
1173 } else {
1174 assert(klass->is_objArray_klass() || klass->is_typeArray_klass(), "Illegal mirror klass");
1175 size = 2;
1176 }
1177
1178 // Allocate result array
1179 objArrayOop r = oopFactory::new_objArray(vmClasses::Class_klass(), size, CHECK_NULL);
1180 objArrayHandle result (THREAD, r);
1181 // Fill in result
1182 if (klass->is_instance_klass()) {
1183 // Regular instance klass, fill in all local interfaces
1184 for (int index = 0; index < size; index++) {
1185 InstanceKlass* k = InstanceKlass::cast(klass)->local_interfaces()->at(index);
1186 result->obj_at_put(index, k->java_mirror());
1187 }
1188 } else {
1189 // All arrays implement java.lang.Cloneable and java.io.Serializable
1190 result->obj_at_put(0, vmClasses::Cloneable_klass()->java_mirror());
1191 result->obj_at_put(1, vmClasses::Serializable_klass()->java_mirror());
1192 }
1240
1241 return nullptr;
1242 JVM_END
1243
1244 JVM_ENTRY(jobjectArray, JVM_GetDeclaredClasses(JNIEnv *env, jclass ofClass))
1245 JvmtiVMObjectAllocEventCollector oam;
1246 // ofClass is a reference to a java_lang_Class object. The mirror object
1247 // of an InstanceKlass
1248 oop ofMirror = JNIHandles::resolve_non_null(ofClass);
1249 if (java_lang_Class::is_primitive(ofMirror) ||
1250 ! java_lang_Class::as_Klass(ofMirror)->is_instance_klass()) {
1251 oop result = oopFactory::new_objArray(vmClasses::Class_klass(), 0, CHECK_NULL);
1252 return (jobjectArray)JNIHandles::make_local(THREAD, result);
1253 }
1254
1255 InstanceKlass* k = java_lang_Class::as_InstanceKlass(ofMirror);
1256 InnerClassesIterator iter(k);
1257
1258 if (iter.length() == 0) {
1259 // Neither an inner nor outer class
1260 oop result = oopFactory::new_objArray(vmClasses::Class_klass(), 0, CHECK_NULL);
1261 return (jobjectArray)JNIHandles::make_local(THREAD, result);
1262 }
1263
1264 // find inner class info
1265 constantPoolHandle cp(thread, k->constants());
1266 int length = iter.length();
1267
1268 // Allocate temp. result array
1269 objArrayOop r = oopFactory::new_objArray(vmClasses::Class_klass(), length/4, CHECK_NULL);
1270 objArrayHandle result (THREAD, r);
1271 int members = 0;
1272
1273 for (; !iter.done(); iter.next()) {
1274 int ioff = iter.inner_class_info_index();
1275 int ooff = iter.outer_class_info_index();
1276
1277 if (ioff != 0 && ooff != 0) {
1278 // Check to see if the name matches the class we're looking for
1279 // before attempting to find the class.
1280 if (cp->klass_name_at_matches(k, ooff)) {
1281 Klass* outer_klass = cp->klass_at(ooff, CHECK_NULL);
1282 if (outer_klass == k) {
1283 Klass* ik = cp->klass_at(ioff, CHECK_NULL);
1284 InstanceKlass* inner_klass = InstanceKlass::cast(ik);
1285
1286 // Throws an exception if outer klass has not declared k as
1287 // an inner klass
1288 Reflection::check_for_inner_class(k, inner_klass, true, CHECK_NULL);
1289
1290 result->obj_at_put(members, inner_klass->java_mirror());
1291 members++;
1292 }
1293 }
1294 }
1295 }
1296
1297 if (members != length) {
1298 // Return array of right length
1299 objArrayOop res = oopFactory::new_objArray(vmClasses::Class_klass(), members, CHECK_NULL);
1300 for(int i = 0; i < members; i++) {
1301 res->obj_at_put(i, result->obj_at(i));
1302 }
1303 return (jobjectArray)JNIHandles::make_local(THREAD, res);
1304 }
1305
1306 return (jobjectArray)JNIHandles::make_local(THREAD, result());
1307 JVM_END
1308
1309
1310 JVM_ENTRY(jclass, JVM_GetDeclaringClass(JNIEnv *env, jclass ofClass))
1311 {
1312 // ofClass is a reference to a java_lang_Class object.
1313 oop ofMirror = JNIHandles::resolve_non_null(ofClass);
1314 if (java_lang_Class::is_primitive(ofMirror)) {
1315 return nullptr;
1316 }
1317 Klass* klass = java_lang_Class::as_Klass(ofMirror);
1318 if (!klass->is_instance_klass()) {
1319 return nullptr;
1656 }
1657
1658 InstanceKlass* k = java_lang_Class::as_InstanceKlass(ofMirror);
1659
1660 // Ensure class is linked
1661 k->link_class(CHECK_NULL);
1662
1663 Array<Method*>* methods = k->methods();
1664 int methods_length = methods->length();
1665
1666 // Save original method_idnum in case of redefinition, which can change
1667 // the idnum of obsolete methods. The new method will have the same idnum
1668 // but if we refresh the methods array, the counts will be wrong.
1669 ResourceMark rm(THREAD);
1670 GrowableArray<int>* idnums = new GrowableArray<int>(methods_length);
1671 int num_methods = 0;
1672
1673 // Select methods matching the criteria.
1674 for (int i = 0; i < methods_length; i++) {
1675 Method* method = methods->at(i);
1676 if (want_constructor && !method->is_object_initializer()) {
1677 continue;
1678 }
1679 if (!want_constructor &&
1680 (method->is_object_initializer() || method->is_static_initializer() ||
1681 method->is_overpass())) {
1682 continue;
1683 }
1684 if (publicOnly && !method->is_public()) {
1685 continue;
1686 }
1687 idnums->push(method->method_idnum());
1688 ++num_methods;
1689 }
1690
1691 // Allocate result
1692 objArrayOop r = oopFactory::new_objArray(klass, num_methods, CHECK_NULL);
1693 objArrayHandle result (THREAD, r);
1694
1695 // Now just put the methods that we selected above, but go by their idnum
1696 // in case of redefinition. The methods can be redefined at any safepoint,
1697 // so above when allocating the oop array and below when creating reflect
1698 // objects.
1699 for (int i = 0; i < num_methods; i++) {
1700 methodHandle method(THREAD, k->method_with_idnum(idnums->at(i)));
1701 if (method.is_null()) {
1702 // Method may have been deleted and seems this API can handle null
1703 // Otherwise should probably put a method that throws NSME
1704 result->obj_at_put(i, nullptr);
1705 } else {
1706 oop m;
1707 if (want_constructor) {
1708 m = Reflection::new_constructor(method, CHECK_NULL);
1709 } else {
1710 m = Reflection::new_method(method, false, CHECK_NULL);
1711 }
1712 result->obj_at_put(i, m);
1713 }
1714 }
1715
1716 return (jobjectArray) JNIHandles::make_local(THREAD, result());
1717 }
1718
1719 JVM_ENTRY(jobjectArray, JVM_GetClassDeclaredMethods(JNIEnv *env, jclass ofClass, jboolean publicOnly))
1720 {
1721 return get_class_declared_methods_helper(env, ofClass, publicOnly,
1722 /*want_constructor*/ false,
1723 vmClasses::reflect_Method_klass(), THREAD);
1724 }
1725 JVM_END
1726
1727 JVM_ENTRY(jobjectArray, JVM_GetClassDeclaredConstructors(JNIEnv *env, jclass ofClass, jboolean publicOnly))
1750 }
1751 JVM_END
1752
1753 JVM_ENTRY(jobjectArray, JVM_GetNestMembers(JNIEnv* env, jclass current))
1754 {
1755 // current is not a primitive or array class
1756 ResourceMark rm(THREAD);
1757 InstanceKlass* c = java_lang_Class::as_InstanceKlass(JNIHandles::resolve_non_null(current));
1758 InstanceKlass* host = c->nest_host(THREAD);
1759
1760 log_trace(class, nestmates)("Calling GetNestMembers for type %s with nest-host %s",
1761 c->external_name(), host->external_name());
1762 {
1763 JvmtiVMObjectAllocEventCollector oam;
1764 Array<u2>* members = host->nest_members();
1765 int length = members == nullptr ? 0 : members->length();
1766
1767 log_trace(class, nestmates)(" - host has %d listed nest members", length);
1768
1769 // nest host is first in the array so make it one bigger
1770 objArrayOop r = oopFactory::new_objArray(vmClasses::Class_klass(),
1771 length + 1, CHECK_NULL);
1772 objArrayHandle result(THREAD, r);
1773 result->obj_at_put(0, host->java_mirror());
1774 if (length != 0) {
1775 int count = 0;
1776 for (int i = 0; i < length; i++) {
1777 int cp_index = members->at(i);
1778 Klass* k = host->constants()->klass_at(cp_index, THREAD);
1779 if (HAS_PENDING_EXCEPTION) {
1780 if (PENDING_EXCEPTION->is_a(vmClasses::VirtualMachineError_klass())) {
1781 return nullptr; // propagate VMEs
1782 }
1783 if (log_is_enabled(Trace, class, nestmates)) {
1784 stringStream ss;
1785 char* target_member_class = host->constants()->klass_name_at(cp_index)->as_C_string();
1786 ss.print(" - resolution of nest member %s failed: ", target_member_class);
1787 java_lang_Throwable::print(PENDING_EXCEPTION, &ss);
1788 log_trace(class, nestmates)("%s", ss.as_string());
1789 }
1790 CLEAR_PENDING_EXCEPTION;
1791 continue;
1792 }
1827 }
1828 }
1829 JVM_END
1830
1831 JVM_ENTRY(jobjectArray, JVM_GetPermittedSubclasses(JNIEnv* env, jclass current))
1832 {
1833 oop mirror = JNIHandles::resolve_non_null(current);
1834 assert(!java_lang_Class::is_primitive(mirror), "should not be");
1835 InstanceKlass* ik = java_lang_Class::as_InstanceKlass(mirror);
1836
1837 ResourceMark rm(THREAD);
1838 log_trace(class, sealed)("Calling GetPermittedSubclasses for %s type %s",
1839 ik->is_sealed() ? "sealed" : "non-sealed", ik->external_name());
1840 if (ik->is_sealed()) {
1841 JvmtiVMObjectAllocEventCollector oam;
1842 Array<u2>* subclasses = ik->permitted_subclasses();
1843 int length = subclasses->length();
1844
1845 log_trace(class, sealed)(" - sealed class has %d permitted subclasses", length);
1846
1847 objArrayOop r = oopFactory::new_objArray(vmClasses::Class_klass(),
1848 length, CHECK_NULL);
1849 objArrayHandle result(THREAD, r);
1850 int count = 0;
1851 for (int i = 0; i < length; i++) {
1852 int cp_index = subclasses->at(i);
1853 Klass* k = ik->constants()->klass_at(cp_index, THREAD);
1854 if (HAS_PENDING_EXCEPTION) {
1855 if (PENDING_EXCEPTION->is_a(vmClasses::VirtualMachineError_klass())) {
1856 return nullptr; // propagate VMEs
1857 }
1858 if (log_is_enabled(Trace, class, sealed)) {
1859 stringStream ss;
1860 char* permitted_subclass = ik->constants()->klass_name_at(cp_index)->as_C_string();
1861 ss.print(" - resolution of permitted subclass %s failed: ", permitted_subclass);
1862 java_lang_Throwable::print(PENDING_EXCEPTION, &ss);
1863 log_trace(class, sealed)("%s", ss.as_string());
1864 }
1865
1866 CLEAR_PENDING_EXCEPTION;
1867 continue;
1868 }
1869 if (k->is_instance_klass()) {
1948 constantTag tag = cp->tag_at(index);
1949 if (!tag.is_method() && !tag.is_interface_method()) {
1950 THROW_MSG_NULL(vmSymbols::java_lang_IllegalArgumentException(), "Wrong type at constant pool index");
1951 }
1952 int klass_ref = cp->uncached_klass_ref_index_at(index);
1953 Klass* k_o;
1954 if (force_resolution) {
1955 k_o = cp->klass_at(klass_ref, CHECK_NULL);
1956 } else {
1957 k_o = ConstantPool::klass_at_if_loaded(cp, klass_ref);
1958 if (k_o == nullptr) return nullptr;
1959 }
1960 InstanceKlass* k = InstanceKlass::cast(k_o);
1961 Symbol* name = cp->uncached_name_ref_at(index);
1962 Symbol* sig = cp->uncached_signature_ref_at(index);
1963 methodHandle m (THREAD, k->find_method(name, sig));
1964 if (m.is_null()) {
1965 THROW_MSG_NULL(vmSymbols::java_lang_RuntimeException(), "Unable to look up method in target class");
1966 }
1967 oop method;
1968 if (m->is_object_initializer()) {
1969 method = Reflection::new_constructor(m, CHECK_NULL);
1970 } else {
1971 // new_method accepts <clinit> as Method here
1972 method = Reflection::new_method(m, true, CHECK_NULL);
1973 }
1974 return JNIHandles::make_local(THREAD, method);
1975 }
1976
1977 JVM_ENTRY(jobject, JVM_ConstantPoolGetMethodAt(JNIEnv *env, jobject obj, jint index))
1978 {
1979 JvmtiVMObjectAllocEventCollector oam;
1980 constantPoolHandle cp = constantPoolHandle(THREAD, reflect_ConstantPool::get_cp(JNIHandles::resolve_non_null(obj)));
1981 bounds_check(cp, index, CHECK_NULL);
1982 jobject res = get_method_at_helper(cp, index, true, CHECK_NULL);
1983 return res;
1984 }
1985 JVM_END
1986
1987 JVM_ENTRY(jobject, JVM_ConstantPoolGetMethodAtIfLoaded(JNIEnv *env, jobject obj, jint index))
1988 {
1989 JvmtiVMObjectAllocEventCollector oam;
1990 constantPoolHandle cp = constantPoolHandle(THREAD, reflect_ConstantPool::get_cp(JNIHandles::resolve_non_null(obj)));
1991 bounds_check(cp, index, CHECK_NULL);
2398
2399
2400 JVM_ENTRY(jint, JVM_GetMethodIxArgsSize(JNIEnv *env, jclass cls, int method_index))
2401 InstanceKlass* ik = get_instance_klass_considering_redefinition(cls, thread);
2402 Method* method = ik->methods()->at(method_index);
2403 return method->size_of_parameters();
2404 JVM_END
2405
2406
2407 JVM_ENTRY(jint, JVM_GetMethodIxMaxStack(JNIEnv *env, jclass cls, int method_index))
2408 InstanceKlass* ik = get_instance_klass_considering_redefinition(cls, thread);
2409 Method* method = ik->methods()->at(method_index);
2410 return method->verifier_max_stack();
2411 JVM_END
2412
2413
2414 JVM_ENTRY(jboolean, JVM_IsConstructorIx(JNIEnv *env, jclass cls, int method_index))
2415 ResourceMark rm(THREAD);
2416 InstanceKlass* ik = get_instance_klass_considering_redefinition(cls, thread);
2417 Method* method = ik->methods()->at(method_index);
2418 return method->name() == vmSymbols::object_initializer_name();
2419 JVM_END
2420
2421
2422 JVM_ENTRY(jboolean, JVM_IsVMGeneratedMethodIx(JNIEnv *env, jclass cls, int method_index))
2423 ResourceMark rm(THREAD);
2424 InstanceKlass* ik = get_instance_klass_considering_redefinition(cls, thread);
2425 Method* method = ik->methods()->at(method_index);
2426 return method->is_overpass();
2427 JVM_END
2428
2429 JVM_ENTRY(const char*, JVM_GetMethodIxNameUTF(JNIEnv *env, jclass cls, jint method_index))
2430 InstanceKlass* ik = get_instance_klass_considering_redefinition(cls, thread);
2431 Method* method = ik->methods()->at(method_index);
2432 return method->name()->as_utf8();
2433 JVM_END
2434
2435
2436 JVM_ENTRY(const char*, JVM_GetMethodIxSignatureUTF(JNIEnv *env, jclass cls, jint method_index))
2437 InstanceKlass* ik = get_instance_klass_considering_redefinition(cls, thread);
2438 Method* method = ik->methods()->at(method_index);
3267
3268 jclass find_class_from_class_loader(JNIEnv* env, Symbol* name, jboolean init,
3269 Handle loader, jboolean throwError, TRAPS) {
3270 Klass* klass = SystemDictionary::resolve_or_fail(name, loader, throwError != 0, CHECK_NULL);
3271
3272 // Check if we should initialize the class
3273 if (init && klass->is_instance_klass()) {
3274 klass->initialize(CHECK_NULL);
3275 }
3276 return (jclass) JNIHandles::make_local(THREAD, klass->java_mirror());
3277 }
3278
3279
3280 // Method ///////////////////////////////////////////////////////////////////////////////////////////
3281
3282 JVM_ENTRY(jobject, JVM_InvokeMethod(JNIEnv *env, jobject method, jobject obj, jobjectArray args0))
3283 Handle method_handle;
3284 if (thread->stack_overflow_state()->stack_available((address) &method_handle) >= JVMInvokeMethodSlack) {
3285 method_handle = Handle(THREAD, JNIHandles::resolve(method));
3286 Handle receiver(THREAD, JNIHandles::resolve(obj));
3287 objArrayHandle args(THREAD, objArrayOop(JNIHandles::resolve(args0)));
3288 oop result = Reflection::invoke_method(method_handle(), receiver, args, CHECK_NULL);
3289 jobject res = JNIHandles::make_local(THREAD, result);
3290 if (JvmtiExport::should_post_vm_object_alloc()) {
3291 oop ret_type = java_lang_reflect_Method::return_type(method_handle());
3292 assert(ret_type != nullptr, "sanity check: ret_type oop must not be null!");
3293 if (java_lang_Class::is_primitive(ret_type)) {
3294 // Only for primitive type vm allocates memory for java object.
3295 // See box() method.
3296 JvmtiExport::post_vm_object_alloc(thread, result);
3297 }
3298 }
3299 return res;
3300 } else {
3301 THROW_NULL(vmSymbols::java_lang_StackOverflowError());
3302 }
3303 JVM_END
3304
3305
3306 JVM_ENTRY(jobject, JVM_NewInstanceFromConstructor(JNIEnv *env, jobject c, jobjectArray args0))
3307 oop constructor_mirror = JNIHandles::resolve(c);
3308 objArrayHandle args(THREAD, objArrayOop(JNIHandles::resolve(args0)));
3309 oop result = Reflection::invoke_constructor(constructor_mirror, args, CHECK_NULL);
3310 jobject res = JNIHandles::make_local(THREAD, result);
3311 if (JvmtiExport::should_post_vm_object_alloc()) {
3312 JvmtiExport::post_vm_object_alloc(thread, result);
3313 }
3314 return res;
3315 JVM_END
3316
3317 JVM_ENTRY(void, JVM_InitializeFromArchive(JNIEnv* env, jclass cls))
3318 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve(cls));
3319 HeapShared::initialize_from_archived_subgraph(THREAD, k);
3320 JVM_END
3321
3322 JVM_ENTRY(void, JVM_RegisterLambdaProxyClassForArchiving(JNIEnv* env,
3323 jclass caller,
3324 jstring interfaceMethodName,
3325 jobject factoryType,
3326 jobject interfaceMethodType,
3327 jobject implementationMember,
3328 jobject dynamicMethodType,
3512 for (int i = 0; i < num_threads; i++) {
3513 Handle h = tle.get_threadObj(i);
3514 threads_ah->obj_at_put(i, h());
3515 }
3516
3517 return (jobjectArray) JNIHandles::make_local(THREAD, threads_ah());
3518 JVM_END
3519
3520
3521 // Support for java.lang.Thread.getStackTrace() and getAllStackTraces() methods
3522 // Return StackTraceElement[][], each element is the stack trace of a thread in
3523 // the corresponding entry in the given threads array
3524 JVM_ENTRY(jobjectArray, JVM_DumpThreads(JNIEnv *env, jclass threadClass, jobjectArray threads))
3525 JvmtiVMObjectAllocEventCollector oam;
3526
3527 // Check if threads is null
3528 if (threads == nullptr) {
3529 THROW_NULL(vmSymbols::java_lang_NullPointerException());
3530 }
3531
3532 objArrayOop a = objArrayOop(JNIHandles::resolve_non_null(threads));
3533 objArrayHandle ah(THREAD, a);
3534 int num_threads = ah->length();
3535 // check if threads is non-empty array
3536 if (num_threads == 0) {
3537 THROW_NULL(vmSymbols::java_lang_IllegalArgumentException());
3538 }
3539
3540 // check if threads is not an array of objects of Thread class
3541 Klass* k = ObjArrayKlass::cast(ah->klass())->element_klass();
3542 if (k != vmClasses::Thread_klass()) {
3543 THROW_NULL(vmSymbols::java_lang_IllegalArgumentException());
3544 }
3545
3546 ResourceMark rm(THREAD);
3547
3548 GrowableArray<instanceHandle>* thread_handle_array = new GrowableArray<instanceHandle>(num_threads);
3549 for (int i = 0; i < num_threads; i++) {
3550 oop thread_obj = ah->obj_at(i);
3551 instanceHandle h(THREAD, (instanceOop) thread_obj);
3552 thread_handle_array->append(h);
3553 }
|
41 #include "classfile/modules.hpp"
42 #include "classfile/packageEntry.hpp"
43 #include "classfile/stringTable.hpp"
44 #include "classfile/symbolTable.hpp"
45 #include "classfile/systemDictionary.hpp"
46 #include "classfile/vmClasses.hpp"
47 #include "classfile/vmSymbols.hpp"
48 #include "gc/shared/collectedHeap.inline.hpp"
49 #include "interpreter/bytecode.hpp"
50 #include "interpreter/bytecodeUtils.hpp"
51 #include "jfr/jfrEvents.hpp"
52 #include "jvm.h"
53 #include "logging/log.hpp"
54 #include "memory/oopFactory.hpp"
55 #include "memory/referenceType.hpp"
56 #include "memory/resourceArea.hpp"
57 #include "memory/universe.hpp"
58 #include "oops/access.inline.hpp"
59 #include "oops/constantPool.hpp"
60 #include "oops/fieldStreams.inline.hpp"
61 #include "oops/flatArrayKlass.hpp"
62 #include "oops/inlineKlass.inline.hpp"
63 #include "oops/instanceKlass.hpp"
64 #include "oops/klass.inline.hpp"
65 #include "oops/method.hpp"
66 #include "oops/objArrayKlass.hpp"
67 #include "oops/objArrayOop.inline.hpp"
68 #include "oops/oop.inline.hpp"
69 #include "oops/oopCast.inline.hpp"
70 #include "oops/recordComponent.hpp"
71 #include "oops/refArrayOop.inline.hpp"
72 #include "oops/valuePayload.inline.hpp"
73 #include "prims/foreignGlobals.hpp"
74 #include "prims/jvm_misc.hpp"
75 #include "prims/jvmtiExport.hpp"
76 #include "prims/jvmtiThreadState.inline.hpp"
77 #include "prims/stackwalk.hpp"
78 #include "runtime/arguments.hpp"
79 #include "runtime/atomicAccess.hpp"
80 #include "runtime/continuation.hpp"
81 #include "runtime/deoptimization.hpp"
82 #include "runtime/globals_extension.hpp"
83 #include "runtime/handles.inline.hpp"
84 #include "runtime/handshake.hpp"
85 #include "runtime/init.hpp"
86 #include "runtime/interfaceSupport.inline.hpp"
87 #include "runtime/java.hpp"
88 #include "runtime/javaCalls.hpp"
89 #include "runtime/javaThread.hpp"
90 #include "runtime/jfieldIDWorkaround.hpp"
91 #include "runtime/jniHandles.inline.hpp"
92 #include "runtime/mountUnmountDisabler.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 JVM_ENTRY(jarray, JVM_CopyOfSpecialArray(JNIEnv *env, jarray orig, jint from, jint to))
431 oop o = JNIHandles::resolve_non_null(orig);
432 assert(o->is_array(), "Must be");
433 oop array = nullptr;
434 arrayOop org = (arrayOop)o;
435 arrayHandle oh(THREAD, org);
436 ObjArrayKlass* ak = ObjArrayKlass::cast(org->klass());
437 InlineKlass* vk = InlineKlass::cast(ak->element_klass());
438 int len = to - from; // length of the new array
439 if (ak->is_null_free_array_klass()) {
440 if ((len != 0) && (from >= org->length() || to > org->length())) {
441 THROW_MSG_NULL(vmSymbols::java_lang_IllegalArgumentException(), "Copying of null-free array with uninitialized elements");
442 }
443 }
444 if (ak->is_flatArray_klass()) {
445 // The whole JVM_CopyOfSpecialArray is currently broken. Fix this in a separate bugfix.
446 int org_length = org->length();
447 int copy_len = MIN2(to, org_length) - MIN2(from, org_length);
448 FlatArrayKlass* const fak = FlatArrayKlass::cast(org->klass());
449 flatArrayOop dst = fak->allocate_instance(len, CHECK_NULL);
450 assert(!ak->is_null_free_array_klass() || copy_len == len,
451 "Failed to throw the IllegalArgumentException");
452 if (copy_len != 0) {
453 int start = MIN2(from, org_length - 1);
454 FlatArrayPayload src_payload(flatArrayOop(oh()), start, fak);
455 FlatArrayPayload dst_payload(dst, 0, fak);
456 int end = to < oh()->length() ? to : oh()->length();
457 for (int i = from; i < end; i++) {
458 // Copy a value
459 src_payload.copy_to(dst_payload);
460
461 // Advance to the next element
462 src_payload.next_element();
463 dst_payload.next_element();
464 }
465 }
466 array = dst;
467 } else {
468 const ArrayProperties props = ArrayProperties::Default().with_null_restricted(ak->is_null_free_array_klass());
469
470 array = oopFactory::new_objArray(vk, len, props, CHECK_NULL);
471 int end = to < oh()->length() ? to : oh()->length();
472 for (int i = from; i < end; i++) {
473 if (i < ((objArrayOop)oh())->length()) {
474 oop val = ((objArrayOop)oh())->obj_at(i, CHECK_NULL);
475 ((objArrayOop)array)->obj_at_put(i - from, val);
476 } else {
477 assert(!ak->is_null_free_array_klass(), "Must be a nullable array");
478 ((objArrayOop)array)->obj_at_put(i - from, nullptr);
479 }
480 }
481 }
482 return (jarray) JNIHandles::make_local(THREAD, array);
483 JVM_END
484
485 #ifdef ASSERT
486 static void verify_array_arguments(jclass elmClass, jint len) {
487 assert(len >= 0, "Negative array length");
488 assert(elmClass != nullptr, "Null element class");
489 oop mirror = JNIHandles::resolve_non_null(elmClass);
490 Klass* klass = java_lang_Class::as_Klass(mirror);
491 assert(klass->is_inline_klass(), "Element class must be an inline class");
492 }
493 #endif // ASSERT
494
495 JVM_ENTRY(jarray, JVM_NewNullRestrictedNonAtomicArray(JNIEnv *env, jclass elmClass, jint len, jobject initVal))
496 DEBUG_ONLY(verify_array_arguments(elmClass, len));
497 oop mirror = JNIHandles::resolve_non_null(elmClass);
498 Klass* klass = java_lang_Class::as_Klass(mirror);
499 oop init = JNIHandles::resolve_non_null(initVal);
500 Handle init_h(THREAD, init);
501 assert(klass == init_h()->klass(), "Type mismatch between array and initial value");
502 const ArrayProperties props = ArrayProperties::Default()
503 .with_null_restricted()
504 .with_non_atomic();
505 objArrayOop array = oopFactory::new_objArray(klass, len, props, CHECK_NULL);
506 for (int i = 0; i < len; i++) {
507 array->obj_at_put(i, init_h() /*, CHECK_NULL*/ );
508 }
509 return (jarray) JNIHandles::make_local(THREAD, array);
510 JVM_END
511
512 JVM_ENTRY(jarray, JVM_NewNullRestrictedAtomicArray(JNIEnv *env, jclass elmClass, jint len, jobject initVal))
513 DEBUG_ONLY(verify_array_arguments(elmClass, len);)
514 oop mirror = JNIHandles::resolve_non_null(elmClass);
515 Klass* klass = java_lang_Class::as_Klass(mirror);
516 oop init = JNIHandles::resolve_non_null(initVal);
517 Handle init_h(THREAD, init);
518 assert(klass == init_h()->klass(), "Type mismatch between array and initial value");
519 const ArrayProperties props = ArrayProperties::Default().with_null_restricted();
520 objArrayOop array = oopFactory::new_objArray(klass, len, props, CHECK_NULL);
521 for (int i = 0; i < len; i++) {
522 array->obj_at_put(i, init_h() /*, CHECK_NULL*/ );
523 }
524 return (jarray) JNIHandles::make_local(THREAD, array);
525 JVM_END
526
527 JVM_ENTRY(jarray, JVM_NewNullableAtomicArray(JNIEnv *env, jclass elmClass, jint len))
528 DEBUG_ONLY(verify_array_arguments(elmClass, len);)
529 oop mirror = JNIHandles::resolve_non_null(elmClass);
530 Klass* klass = java_lang_Class::as_Klass(mirror);
531 objArrayOop array = oopFactory::new_objArray(klass, len, ArrayProperties::Default(), CHECK_NULL);
532 return (jarray) JNIHandles::make_local(THREAD, array);
533 JVM_END
534
535 JVM_ENTRY(jarray, JVM_NewReferenceArray(JNIEnv *env, jclass elmClass, jint len))
536 DEBUG_ONLY(verify_array_arguments(elmClass, len);)
537 oop mirror = JNIHandles::resolve_non_null(elmClass);
538 Klass* klass = java_lang_Class::as_Klass(mirror);
539 refArrayOop array = oopFactory::new_refArray(klass, len, ArrayProperties::Default(), CHECK_NULL);
540 return (jarray) JNIHandles::make_local(THREAD, array);
541 JVM_END
542
543 JVM_ENTRY(jboolean, JVM_IsFlatArray(JNIEnv *env, jarray array))
544 oop o = JNIHandles::resolve_non_null(array);
545 Klass* klass = o->klass();
546
547 assert(klass->is_objArray_klass(), "Expects an object array");
548
549 return klass->is_flatArray_klass();
550 JVM_END
551
552 JVM_ENTRY(jboolean, JVM_IsNullRestrictedArray(JNIEnv *env, jarray array))
553 oop o = JNIHandles::resolve_non_null(array);
554 Klass* klass = o->klass();
555
556 assert(klass->is_objArray_klass(), "Expects an object array");
557
558 return klass->is_null_free_array_klass();
559 JVM_END
560
561 JVM_ENTRY(jboolean, JVM_IsAtomicArray(JNIEnv *env, jarray array))
562 // There are multiple cases where an array can/must support atomic access:
563 // - the array is a reference array
564 // - the array uses an atomic flat layout: NULLABLE_ATOMIC_FLAT or NULL_FREE_ATOMIC_FLAT
565 // - the array is flat and its component type is naturally atomic
566 oop o = JNIHandles::resolve_non_null(array);
567 Klass* klass = o->klass();
568
569 assert(klass->is_objArray_klass(), "Expects an object array");
570
571 if (klass->is_refArray_klass()) {
572 return true;
573 }
574
575 if (klass->is_flatArray_klass()) {
576 FlatArrayKlass* fak = FlatArrayKlass::cast(klass);
577 if (LayoutKindHelper::is_atomic_flat(fak->layout_kind())) {
578 return true;
579 }
580 bool is_null_free = !LayoutKindHelper::is_nullable_flat(fak->layout_kind());
581 if (fak->element_klass()->is_naturally_atomic(is_null_free)) {
582 return true;
583 }
584
585 return false;
586 }
587
588 ShouldNotReachHere();
589 JVM_END
590
591 // java.lang.Runtime /////////////////////////////////////////////////////////////////////////
592
593 extern volatile jint vm_created;
594
595 JVM_ENTRY_NO_ENV(void, JVM_BeforeHalt())
596 EventShutdown event;
597 if (event.should_commit()) {
598 event.set_reason("Shutdown requested from Java");
599 event.commit();
600 }
601 JVM_END
602
603
604 JVM_ENTRY_NO_ENV(void, JVM_Halt(jint code))
605 before_exit(thread, true);
606 vm_exit(code);
607 JVM_END
608
609
687 }
688 if (method->is_native()) {
689 return nullptr;
690 }
691
692 stringStream ss;
693 bool ok = BytecodeUtils::get_NPE_message_at(&ss, method, bci);
694 if (ok) {
695 oop result = java_lang_String::create_oop_from_str(ss.base(), CHECK_NULL);
696 return (jstring) JNIHandles::make_local(THREAD, result);
697 } else {
698 return nullptr;
699 }
700 JVM_END
701
702 // java.lang.StackTraceElement //////////////////////////////////////////////
703
704
705 JVM_ENTRY(void, JVM_InitStackTraceElementArray(JNIEnv *env, jobjectArray elements, jobject backtrace, jint depth))
706 Handle backtraceh(THREAD, JNIHandles::resolve(backtrace));
707 refArrayOop st = refArrayOop(JNIHandles::resolve(elements));
708 refArrayHandle stack_trace(THREAD, st);
709 // Fill in the allocated stack trace
710 java_lang_Throwable::get_stack_trace_elements(depth, backtraceh, stack_trace, CHECK);
711 JVM_END
712
713
714 JVM_ENTRY(void, JVM_InitStackTraceElement(JNIEnv* env, jobject element, jobject stackFrameInfo))
715 Handle stack_frame_info(THREAD, JNIHandles::resolve_non_null(stackFrameInfo));
716 Handle stack_trace_element(THREAD, JNIHandles::resolve_non_null(element));
717 java_lang_StackFrameInfo::to_stack_trace_element(stack_frame_info, stack_trace_element, CHECK);
718 JVM_END
719
720
721 // java.lang.StackWalker //////////////////////////////////////////////////////
722 JVM_ENTRY(void, JVM_ExpandStackFrameInfo(JNIEnv *env, jobject obj))
723 Handle stack_frame_info(THREAD, JNIHandles::resolve_non_null(obj));
724
725 bool have_name = (java_lang_StackFrameInfo::name(stack_frame_info()) != nullptr);
726 bool have_type = (java_lang_StackFrameInfo::type(stack_frame_info()) != nullptr);
727 Method* method = java_lang_StackFrameInfo::get_method(stack_frame_info());
728 if (!have_name) {
731 }
732 if (!have_type) {
733 Handle type = java_lang_String::create_from_symbol(method->signature(), CHECK);
734 java_lang_StackFrameInfo::set_type(stack_frame_info(), type());
735 }
736 JVM_END
737
738 JVM_ENTRY(jobject, JVM_CallStackWalk(JNIEnv *env, jobject stackStream, jint mode,
739 jint skip_frames, jobject contScope, jobject cont,
740 jint buffer_size, jint start_index, jobjectArray frames))
741 if (!thread->has_last_Java_frame()) {
742 THROW_MSG_(vmSymbols::java_lang_InternalError(), "doStackWalk: no stack trace", nullptr);
743 }
744
745 Handle stackStream_h(THREAD, JNIHandles::resolve_non_null(stackStream));
746 Handle contScope_h(THREAD, JNIHandles::resolve(contScope));
747 Handle cont_h(THREAD, JNIHandles::resolve(cont));
748 // frames array is a ClassFrameInfo[] array when only getting caller reference,
749 // and a StackFrameInfo[] array (or derivative) otherwise. It should never
750 // be null.
751 refArrayOop fa = refArrayOop(JNIHandles::resolve_non_null(frames));
752 refArrayHandle frames_array_h(THREAD, fa);
753
754 if (frames_array_h->length() < buffer_size) {
755 THROW_MSG_(vmSymbols::java_lang_IllegalArgumentException(), "not enough space in buffers", nullptr);
756 }
757
758 oop result = StackWalk::walk(stackStream_h, mode, skip_frames, contScope_h, cont_h,
759 buffer_size, start_index, frames_array_h, CHECK_NULL);
760 return JNIHandles::make_local(THREAD, result);
761 JVM_END
762
763
764 JVM_ENTRY(jint, JVM_MoreStackWalk(JNIEnv *env, jobject stackStream, jint mode, jlong anchor,
765 jint last_batch_count, jint buffer_size, jint start_index,
766 jobjectArray frames))
767 // frames array is a ClassFrameInfo[] array when only getting caller reference,
768 // and a StackFrameInfo[] array (or derivative) otherwise. It should never
769 // be null.
770 refArrayOop fa = refArrayOop(JNIHandles::resolve_non_null(frames));
771 refArrayHandle frames_array_h(THREAD, fa);
772
773 if (frames_array_h->length() < buffer_size) {
774 THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(), "not enough space in buffers");
775 }
776
777 Handle stackStream_h(THREAD, JNIHandles::resolve_non_null(stackStream));
778 return StackWalk::fetchNextBatch(stackStream_h, mode, anchor, last_batch_count, buffer_size,
779 start_index, frames_array_h, THREAD);
780 JVM_END
781
782 JVM_ENTRY(void, JVM_SetStackWalkContinuation(JNIEnv *env, jobject stackStream, jlong anchor, jobjectArray frames, jobject cont))
783 refArrayOop fa = refArrayOop(JNIHandles::resolve_non_null(frames));
784 refArrayHandle frames_array_h(THREAD, fa);
785 Handle stackStream_h(THREAD, JNIHandles::resolve_non_null(stackStream));
786 Handle cont_h(THREAD, JNIHandles::resolve_non_null(cont));
787
788 StackWalk::setContinuation(stackStream_h, anchor, frames_array_h, cont_h, THREAD);
789 JVM_END
790
791 // java.lang.Object ///////////////////////////////////////////////
792
793
794 JVM_ENTRY(jint, JVM_IHashCode(JNIEnv* env, jobject handle))
795 // as implemented in the classic virtual machine; return 0 if object is null
796 if (handle == nullptr) {
797 return 0;
798 }
799 oop obj = JNIHandles::resolve_non_null(handle);
800 if (Arguments::is_valhalla_enabled() && obj->klass()->is_inline_klass()) {
801 const intptr_t obj_identity_hash = obj->mark().hash();
802 // Check if mark word contains hash code already.
803 // It is possible that the generated identity hash is 0, which is not
804 // distinct from the no_hash value. In such a case, the hash will be
805 // computed and set every time JVM_IHashCode is called. If that happens,
806 // the only consequence is losing out on the optimization.
807 if (obj_identity_hash != markWord::no_hash) {
808 return checked_cast<jint>(obj_identity_hash);
809 }
810
811 // Compute hash by calling ValueObjectMethods.valueObjectHashCode.
812 // The identity hash is invariantly immutable (see its JavaDoc comment).
813 JavaValue result(T_INT);
814 JavaCallArguments args;
815 Handle ho(THREAD, obj);
816 args.push_oop(ho);
817 methodHandle method(THREAD, Universe::value_object_hash_code_method());
818 JavaCalls::call(&result, method, &args, THREAD);
819 Exceptions::wrap_exception_in_internal_error("Internal error in hashCode", CHECK_0);
820
821 const intptr_t identity_hash = result.get_jint();
822
823 // We now have to set the hash via CAS. It's possible that this will race
824 // other threads. By our invariant of immutability, when there is a
825 // race, the identity hash code is going to be one of the following:
826 // a) 0, another thread updated other markWord bits; b) identity_hash set
827 // by another thread; or c) identity_hash set by the current thread.
828 // A nonzero identity hash code that is not the identity_hash computed
829 // earlier indicates a violation of the invariant.
830 markWord current_mark, old_mark, new_mark;
831 do {
832 current_mark = ho->mark();
833 new_mark = current_mark.copy_set_hash(identity_hash);
834 old_mark = ho->cas_set_mark(new_mark, current_mark);
835 assert(old_mark.has_no_hash() || old_mark.hash() == new_mark.hash(),
836 "CAS identity hash invariant violated, expected=" INTPTR_FORMAT " actual=" INTPTR_FORMAT,
837 new_mark.hash(),
838 old_mark.hash());
839 } while (old_mark != current_mark);
840
841 return checked_cast<jint>(new_mark.hash());
842 } else {
843 return checked_cast<jint>(ObjectSynchronizer::FastHashCode(THREAD, obj));
844 }
845 JVM_END
846
847
848 JVM_ENTRY(void, JVM_MonitorWait(JNIEnv* env, jobject handle, jlong ms))
849 Handle obj(THREAD, JNIHandles::resolve_non_null(handle));
850 ObjectSynchronizer::wait(obj, ms, CHECK);
851 JVM_END
852
853
854 JVM_ENTRY(void, JVM_MonitorNotify(JNIEnv* env, jobject handle))
855 Handle obj(THREAD, JNIHandles::resolve_non_null(handle));
856 ObjectSynchronizer::notify(obj, CHECK);
857 JVM_END
858
859
860 JVM_ENTRY(void, JVM_MonitorNotifyAll(JNIEnv* env, jobject handle))
861 Handle obj(THREAD, JNIHandles::resolve_non_null(handle));
862 ObjectSynchronizer::notifyall(obj, CHECK);
863 JVM_END
864
872 // Just checking that the cloneable flag is set correct
873 if (obj->is_array()) {
874 guarantee(klass->is_cloneable(), "all arrays are cloneable");
875 } else {
876 guarantee(obj->is_instance(), "should be instanceOop");
877 bool cloneable = klass->is_subtype_of(vmClasses::Cloneable_klass());
878 guarantee(cloneable == klass->is_cloneable(), "incorrect cloneable flag");
879 }
880 #endif
881
882 // Check if class of obj supports the Cloneable interface.
883 // All arrays are considered to be cloneable (See JLS 20.1.5).
884 // All j.l.r.Reference classes are considered non-cloneable.
885 if (!klass->is_cloneable() ||
886 (klass->is_instance_klass() &&
887 InstanceKlass::cast(klass)->reference_type() != REF_NONE)) {
888 ResourceMark rm(THREAD);
889 THROW_MSG_NULL(vmSymbols::java_lang_CloneNotSupportedException(), klass->external_name());
890 }
891
892 if (klass->is_inline_klass()) {
893 // Value instances have no identity, so return the current instance instead of allocating a new one
894 // Value classes cannot have finalizers, so the method can return immediately
895 return JNIHandles::make_local(THREAD, obj());
896 }
897
898 // Make shallow object copy
899 const size_t size = obj->size();
900 oop new_obj_oop = nullptr;
901 if (obj->is_array()) {
902 const int length = ((arrayOop)obj())->length();
903 new_obj_oop = Universe::heap()->array_allocate(klass, size, length,
904 /* do_zero */ true, CHECK_NULL);
905 } else {
906 new_obj_oop = Universe::heap()->obj_allocate(klass, size, CHECK_NULL);
907 }
908
909 HeapAccess<>::clone(obj(), new_obj_oop, size);
910
911 Handle new_obj(THREAD, new_obj_oop);
912 // Caution: this involves a java upcall, so the clone should be
913 // "gc-robust" by this stage.
914 if (klass->has_finalizer()) {
915 assert(obj->is_instance(), "should be instanceOop");
916 new_obj_oop = InstanceKlass::register_finalizer(instanceOop(new_obj()), CHECK_NULL);
917 new_obj = Handle(THREAD, new_obj_oop);
1370 oop result = java_lang_Class::name(java_class, CHECK_NULL);
1371 return (jstring) JNIHandles::make_local(THREAD, result);
1372 JVM_END
1373
1374
1375 JVM_ENTRY(jobjectArray, JVM_GetClassInterfaces(JNIEnv *env, jclass cls))
1376 JvmtiVMObjectAllocEventCollector oam;
1377 oop mirror = JNIHandles::resolve_non_null(cls);
1378
1379 // Special handling for primitive objects
1380 if (java_lang_Class::is_primitive(mirror)) {
1381 // Primitive objects does not have any interfaces
1382 objArrayOop r = oopFactory::new_objArray(vmClasses::Class_klass(), 0, CHECK_NULL);
1383 return (jobjectArray) JNIHandles::make_local(THREAD, r);
1384 }
1385
1386 Klass* klass = java_lang_Class::as_Klass(mirror);
1387 // Figure size of result array
1388 int size;
1389 if (klass->is_instance_klass()) {
1390 InstanceKlass* ik = InstanceKlass::cast(klass);
1391 size = ik->local_interfaces()->length();
1392 } else {
1393 assert(klass->is_objArray_klass() || klass->is_typeArray_klass(), "Illegal mirror klass");
1394 size = 2;
1395 }
1396
1397 // Allocate result array
1398 objArrayOop r = oopFactory::new_objArray(vmClasses::Class_klass(), size, CHECK_NULL);
1399 objArrayHandle result (THREAD, r);
1400 // Fill in result
1401 if (klass->is_instance_klass()) {
1402 // Regular instance klass, fill in all local interfaces
1403 for (int index = 0; index < size; index++) {
1404 InstanceKlass* k = InstanceKlass::cast(klass)->local_interfaces()->at(index);
1405 result->obj_at_put(index, k->java_mirror());
1406 }
1407 } else {
1408 // All arrays implement java.lang.Cloneable and java.io.Serializable
1409 result->obj_at_put(0, vmClasses::Cloneable_klass()->java_mirror());
1410 result->obj_at_put(1, vmClasses::Serializable_klass()->java_mirror());
1411 }
1459
1460 return nullptr;
1461 JVM_END
1462
1463 JVM_ENTRY(jobjectArray, JVM_GetDeclaredClasses(JNIEnv *env, jclass ofClass))
1464 JvmtiVMObjectAllocEventCollector oam;
1465 // ofClass is a reference to a java_lang_Class object. The mirror object
1466 // of an InstanceKlass
1467 oop ofMirror = JNIHandles::resolve_non_null(ofClass);
1468 if (java_lang_Class::is_primitive(ofMirror) ||
1469 ! java_lang_Class::as_Klass(ofMirror)->is_instance_klass()) {
1470 oop result = oopFactory::new_objArray(vmClasses::Class_klass(), 0, CHECK_NULL);
1471 return (jobjectArray)JNIHandles::make_local(THREAD, result);
1472 }
1473
1474 InstanceKlass* k = java_lang_Class::as_InstanceKlass(ofMirror);
1475 InnerClassesIterator iter(k);
1476
1477 if (iter.length() == 0) {
1478 // Neither an inner nor outer class
1479 oop result = oopFactory::new_refArray(vmClasses::Class_klass(), 0, CHECK_NULL);
1480 return (jobjectArray)JNIHandles::make_local(THREAD, result);
1481 }
1482
1483 // find inner class info
1484 constantPoolHandle cp(thread, k->constants());
1485 int length = iter.length();
1486
1487 // Allocate temp. result array
1488 refArrayOop r = oopFactory::new_refArray(vmClasses::Class_klass(), length / 4, CHECK_NULL);
1489 refArrayHandle result(THREAD, r);
1490 int members = 0;
1491
1492 for (; !iter.done(); iter.next()) {
1493 int ioff = iter.inner_class_info_index();
1494 int ooff = iter.outer_class_info_index();
1495
1496 if (ioff != 0 && ooff != 0) {
1497 // Check to see if the name matches the class we're looking for
1498 // before attempting to find the class.
1499 if (cp->klass_name_at_matches(k, ooff)) {
1500 Klass* outer_klass = cp->klass_at(ooff, CHECK_NULL);
1501 if (outer_klass == k) {
1502 Klass* ik = cp->klass_at(ioff, CHECK_NULL);
1503 InstanceKlass* inner_klass = InstanceKlass::cast(ik);
1504
1505 // Throws an exception if outer klass has not declared k as
1506 // an inner klass
1507 Reflection::check_for_inner_class(k, inner_klass, true, CHECK_NULL);
1508
1509 result->obj_at_put(members, inner_klass->java_mirror());
1510 members++;
1511 }
1512 }
1513 }
1514 }
1515
1516 if (members != length) {
1517 // Return array of right length
1518 refArrayOop res = oopFactory::new_refArray(vmClasses::Class_klass(), members, CHECK_NULL);
1519 for(int i = 0; i < members; i++) {
1520 res->obj_at_put(i, result->obj_at(i));
1521 }
1522 return (jobjectArray)JNIHandles::make_local(THREAD, res);
1523 }
1524
1525 return (jobjectArray)JNIHandles::make_local(THREAD, result());
1526 JVM_END
1527
1528
1529 JVM_ENTRY(jclass, JVM_GetDeclaringClass(JNIEnv *env, jclass ofClass))
1530 {
1531 // ofClass is a reference to a java_lang_Class object.
1532 oop ofMirror = JNIHandles::resolve_non_null(ofClass);
1533 if (java_lang_Class::is_primitive(ofMirror)) {
1534 return nullptr;
1535 }
1536 Klass* klass = java_lang_Class::as_Klass(ofMirror);
1537 if (!klass->is_instance_klass()) {
1538 return nullptr;
1875 }
1876
1877 InstanceKlass* k = java_lang_Class::as_InstanceKlass(ofMirror);
1878
1879 // Ensure class is linked
1880 k->link_class(CHECK_NULL);
1881
1882 Array<Method*>* methods = k->methods();
1883 int methods_length = methods->length();
1884
1885 // Save original method_idnum in case of redefinition, which can change
1886 // the idnum of obsolete methods. The new method will have the same idnum
1887 // but if we refresh the methods array, the counts will be wrong.
1888 ResourceMark rm(THREAD);
1889 GrowableArray<int>* idnums = new GrowableArray<int>(methods_length);
1890 int num_methods = 0;
1891
1892 // Select methods matching the criteria.
1893 for (int i = 0; i < methods_length; i++) {
1894 Method* method = methods->at(i);
1895 if (want_constructor && !method->is_object_constructor()) {
1896 continue;
1897 }
1898 if (!want_constructor &&
1899 (method->is_object_constructor() || method->is_class_initializer() ||
1900 method->is_overpass())) {
1901 continue;
1902 }
1903 if (publicOnly && !method->is_public()) {
1904 continue;
1905 }
1906 idnums->push(method->method_idnum());
1907 ++num_methods;
1908 }
1909
1910 // Allocate result
1911 objArrayOop r = oopFactory::new_objArray(klass, num_methods, CHECK_NULL);
1912 objArrayHandle result (THREAD, r);
1913
1914 // Now just put the methods that we selected above, but go by their idnum
1915 // in case of redefinition. The methods can be redefined at any safepoint,
1916 // so above when allocating the oop array and below when creating reflect
1917 // objects.
1918 for (int i = 0; i < num_methods; i++) {
1919 methodHandle method(THREAD, k->method_with_idnum(idnums->at(i)));
1920 if (method.is_null()) {
1921 // Method may have been deleted and seems this API can handle null
1922 // Otherwise should probably put a method that throws NSME
1923 result->obj_at_put(i, nullptr);
1924 } else {
1925 oop m;
1926 if (want_constructor) {
1927 assert(method->is_object_constructor(), "must be");
1928 m = Reflection::new_constructor(method, CHECK_NULL);
1929 } else {
1930 m = Reflection::new_method(method, false, CHECK_NULL);
1931 }
1932 result->obj_at_put(i, m);
1933 }
1934 }
1935
1936 return (jobjectArray) JNIHandles::make_local(THREAD, result());
1937 }
1938
1939 JVM_ENTRY(jobjectArray, JVM_GetClassDeclaredMethods(JNIEnv *env, jclass ofClass, jboolean publicOnly))
1940 {
1941 return get_class_declared_methods_helper(env, ofClass, publicOnly,
1942 /*want_constructor*/ false,
1943 vmClasses::reflect_Method_klass(), THREAD);
1944 }
1945 JVM_END
1946
1947 JVM_ENTRY(jobjectArray, JVM_GetClassDeclaredConstructors(JNIEnv *env, jclass ofClass, jboolean publicOnly))
1970 }
1971 JVM_END
1972
1973 JVM_ENTRY(jobjectArray, JVM_GetNestMembers(JNIEnv* env, jclass current))
1974 {
1975 // current is not a primitive or array class
1976 ResourceMark rm(THREAD);
1977 InstanceKlass* c = java_lang_Class::as_InstanceKlass(JNIHandles::resolve_non_null(current));
1978 InstanceKlass* host = c->nest_host(THREAD);
1979
1980 log_trace(class, nestmates)("Calling GetNestMembers for type %s with nest-host %s",
1981 c->external_name(), host->external_name());
1982 {
1983 JvmtiVMObjectAllocEventCollector oam;
1984 Array<u2>* members = host->nest_members();
1985 int length = members == nullptr ? 0 : members->length();
1986
1987 log_trace(class, nestmates)(" - host has %d listed nest members", length);
1988
1989 // nest host is first in the array so make it one bigger
1990 refArrayOop r = oopFactory::new_refArray(vmClasses::Class_klass(), length + 1, CHECK_NULL);
1991 refArrayHandle result(THREAD, r);
1992 result->obj_at_put(0, host->java_mirror());
1993 if (length != 0) {
1994 int count = 0;
1995 for (int i = 0; i < length; i++) {
1996 int cp_index = members->at(i);
1997 Klass* k = host->constants()->klass_at(cp_index, THREAD);
1998 if (HAS_PENDING_EXCEPTION) {
1999 if (PENDING_EXCEPTION->is_a(vmClasses::VirtualMachineError_klass())) {
2000 return nullptr; // propagate VMEs
2001 }
2002 if (log_is_enabled(Trace, class, nestmates)) {
2003 stringStream ss;
2004 char* target_member_class = host->constants()->klass_name_at(cp_index)->as_C_string();
2005 ss.print(" - resolution of nest member %s failed: ", target_member_class);
2006 java_lang_Throwable::print(PENDING_EXCEPTION, &ss);
2007 log_trace(class, nestmates)("%s", ss.as_string());
2008 }
2009 CLEAR_PENDING_EXCEPTION;
2010 continue;
2011 }
2046 }
2047 }
2048 JVM_END
2049
2050 JVM_ENTRY(jobjectArray, JVM_GetPermittedSubclasses(JNIEnv* env, jclass current))
2051 {
2052 oop mirror = JNIHandles::resolve_non_null(current);
2053 assert(!java_lang_Class::is_primitive(mirror), "should not be");
2054 InstanceKlass* ik = java_lang_Class::as_InstanceKlass(mirror);
2055
2056 ResourceMark rm(THREAD);
2057 log_trace(class, sealed)("Calling GetPermittedSubclasses for %s type %s",
2058 ik->is_sealed() ? "sealed" : "non-sealed", ik->external_name());
2059 if (ik->is_sealed()) {
2060 JvmtiVMObjectAllocEventCollector oam;
2061 Array<u2>* subclasses = ik->permitted_subclasses();
2062 int length = subclasses->length();
2063
2064 log_trace(class, sealed)(" - sealed class has %d permitted subclasses", length);
2065
2066 refArrayOop r = oopFactory::new_refArray(vmClasses::Class_klass(), length, CHECK_NULL);
2067 refArrayHandle result(THREAD, r);
2068 int count = 0;
2069 for (int i = 0; i < length; i++) {
2070 int cp_index = subclasses->at(i);
2071 Klass* k = ik->constants()->klass_at(cp_index, THREAD);
2072 if (HAS_PENDING_EXCEPTION) {
2073 if (PENDING_EXCEPTION->is_a(vmClasses::VirtualMachineError_klass())) {
2074 return nullptr; // propagate VMEs
2075 }
2076 if (log_is_enabled(Trace, class, sealed)) {
2077 stringStream ss;
2078 char* permitted_subclass = ik->constants()->klass_name_at(cp_index)->as_C_string();
2079 ss.print(" - resolution of permitted subclass %s failed: ", permitted_subclass);
2080 java_lang_Throwable::print(PENDING_EXCEPTION, &ss);
2081 log_trace(class, sealed)("%s", ss.as_string());
2082 }
2083
2084 CLEAR_PENDING_EXCEPTION;
2085 continue;
2086 }
2087 if (k->is_instance_klass()) {
2166 constantTag tag = cp->tag_at(index);
2167 if (!tag.is_method() && !tag.is_interface_method()) {
2168 THROW_MSG_NULL(vmSymbols::java_lang_IllegalArgumentException(), "Wrong type at constant pool index");
2169 }
2170 int klass_ref = cp->uncached_klass_ref_index_at(index);
2171 Klass* k_o;
2172 if (force_resolution) {
2173 k_o = cp->klass_at(klass_ref, CHECK_NULL);
2174 } else {
2175 k_o = ConstantPool::klass_at_if_loaded(cp, klass_ref);
2176 if (k_o == nullptr) return nullptr;
2177 }
2178 InstanceKlass* k = InstanceKlass::cast(k_o);
2179 Symbol* name = cp->uncached_name_ref_at(index);
2180 Symbol* sig = cp->uncached_signature_ref_at(index);
2181 methodHandle m (THREAD, k->find_method(name, sig));
2182 if (m.is_null()) {
2183 THROW_MSG_NULL(vmSymbols::java_lang_RuntimeException(), "Unable to look up method in target class");
2184 }
2185 oop method;
2186 if (m->is_object_constructor()) {
2187 method = Reflection::new_constructor(m, CHECK_NULL);
2188 } else {
2189 method = Reflection::new_method(m, true, CHECK_NULL);
2190 }
2191 return JNIHandles::make_local(THREAD, method);
2192 }
2193
2194 JVM_ENTRY(jobject, JVM_ConstantPoolGetMethodAt(JNIEnv *env, jobject obj, jint index))
2195 {
2196 JvmtiVMObjectAllocEventCollector oam;
2197 constantPoolHandle cp = constantPoolHandle(THREAD, reflect_ConstantPool::get_cp(JNIHandles::resolve_non_null(obj)));
2198 bounds_check(cp, index, CHECK_NULL);
2199 jobject res = get_method_at_helper(cp, index, true, CHECK_NULL);
2200 return res;
2201 }
2202 JVM_END
2203
2204 JVM_ENTRY(jobject, JVM_ConstantPoolGetMethodAtIfLoaded(JNIEnv *env, jobject obj, jint index))
2205 {
2206 JvmtiVMObjectAllocEventCollector oam;
2207 constantPoolHandle cp = constantPoolHandle(THREAD, reflect_ConstantPool::get_cp(JNIHandles::resolve_non_null(obj)));
2208 bounds_check(cp, index, CHECK_NULL);
2615
2616
2617 JVM_ENTRY(jint, JVM_GetMethodIxArgsSize(JNIEnv *env, jclass cls, int method_index))
2618 InstanceKlass* ik = get_instance_klass_considering_redefinition(cls, thread);
2619 Method* method = ik->methods()->at(method_index);
2620 return method->size_of_parameters();
2621 JVM_END
2622
2623
2624 JVM_ENTRY(jint, JVM_GetMethodIxMaxStack(JNIEnv *env, jclass cls, int method_index))
2625 InstanceKlass* ik = get_instance_klass_considering_redefinition(cls, thread);
2626 Method* method = ik->methods()->at(method_index);
2627 return method->verifier_max_stack();
2628 JVM_END
2629
2630
2631 JVM_ENTRY(jboolean, JVM_IsConstructorIx(JNIEnv *env, jclass cls, int method_index))
2632 ResourceMark rm(THREAD);
2633 InstanceKlass* ik = get_instance_klass_considering_redefinition(cls, thread);
2634 Method* method = ik->methods()->at(method_index);
2635 return method->is_object_constructor();
2636 JVM_END
2637
2638
2639 JVM_ENTRY(jboolean, JVM_IsVMGeneratedMethodIx(JNIEnv *env, jclass cls, int method_index))
2640 ResourceMark rm(THREAD);
2641 InstanceKlass* ik = get_instance_klass_considering_redefinition(cls, thread);
2642 Method* method = ik->methods()->at(method_index);
2643 return method->is_overpass();
2644 JVM_END
2645
2646 JVM_ENTRY(const char*, JVM_GetMethodIxNameUTF(JNIEnv *env, jclass cls, jint method_index))
2647 InstanceKlass* ik = get_instance_klass_considering_redefinition(cls, thread);
2648 Method* method = ik->methods()->at(method_index);
2649 return method->name()->as_utf8();
2650 JVM_END
2651
2652
2653 JVM_ENTRY(const char*, JVM_GetMethodIxSignatureUTF(JNIEnv *env, jclass cls, jint method_index))
2654 InstanceKlass* ik = get_instance_klass_considering_redefinition(cls, thread);
2655 Method* method = ik->methods()->at(method_index);
3484
3485 jclass find_class_from_class_loader(JNIEnv* env, Symbol* name, jboolean init,
3486 Handle loader, jboolean throwError, TRAPS) {
3487 Klass* klass = SystemDictionary::resolve_or_fail(name, loader, throwError != 0, CHECK_NULL);
3488
3489 // Check if we should initialize the class
3490 if (init && klass->is_instance_klass()) {
3491 klass->initialize(CHECK_NULL);
3492 }
3493 return (jclass) JNIHandles::make_local(THREAD, klass->java_mirror());
3494 }
3495
3496
3497 // Method ///////////////////////////////////////////////////////////////////////////////////////////
3498
3499 JVM_ENTRY(jobject, JVM_InvokeMethod(JNIEnv *env, jobject method, jobject obj, jobjectArray args0))
3500 Handle method_handle;
3501 if (thread->stack_overflow_state()->stack_available((address) &method_handle) >= JVMInvokeMethodSlack) {
3502 method_handle = Handle(THREAD, JNIHandles::resolve(method));
3503 Handle receiver(THREAD, JNIHandles::resolve(obj));
3504 objArrayHandle args(THREAD, (objArrayOop)JNIHandles::resolve(args0));
3505 assert(args() == nullptr || !args->is_flatArray(), "args are never flat");
3506
3507 oop result = Reflection::invoke_method(method_handle(), receiver, args, CHECK_NULL);
3508 jobject res = JNIHandles::make_local(THREAD, result);
3509 if (JvmtiExport::should_post_vm_object_alloc()) {
3510 oop ret_type = java_lang_reflect_Method::return_type(method_handle());
3511 assert(ret_type != nullptr, "sanity check: ret_type oop must not be null!");
3512 if (java_lang_Class::is_primitive(ret_type)) {
3513 // Only for primitive type vm allocates memory for java object.
3514 // See box() method.
3515 JvmtiExport::post_vm_object_alloc(thread, result);
3516 }
3517 }
3518 return res;
3519 } else {
3520 THROW_NULL(vmSymbols::java_lang_StackOverflowError());
3521 }
3522 JVM_END
3523
3524
3525 JVM_ENTRY(jobject, JVM_NewInstanceFromConstructor(JNIEnv *env, jobject c, jobjectArray args0))
3526 objArrayHandle args(THREAD, (objArrayOop)JNIHandles::resolve(args0));
3527 assert(args() == nullptr || !args->is_flatArray(), "args are never flat");
3528 oop constructor_mirror = JNIHandles::resolve(c);
3529 oop result = Reflection::invoke_constructor(constructor_mirror, args, CHECK_NULL);
3530 jobject res = JNIHandles::make_local(THREAD, result);
3531 if (JvmtiExport::should_post_vm_object_alloc()) {
3532 JvmtiExport::post_vm_object_alloc(thread, result);
3533 }
3534 return res;
3535 JVM_END
3536
3537 JVM_ENTRY(void, JVM_InitializeFromArchive(JNIEnv* env, jclass cls))
3538 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve(cls));
3539 HeapShared::initialize_from_archived_subgraph(THREAD, k);
3540 JVM_END
3541
3542 JVM_ENTRY(void, JVM_RegisterLambdaProxyClassForArchiving(JNIEnv* env,
3543 jclass caller,
3544 jstring interfaceMethodName,
3545 jobject factoryType,
3546 jobject interfaceMethodType,
3547 jobject implementationMember,
3548 jobject dynamicMethodType,
3732 for (int i = 0; i < num_threads; i++) {
3733 Handle h = tle.get_threadObj(i);
3734 threads_ah->obj_at_put(i, h());
3735 }
3736
3737 return (jobjectArray) JNIHandles::make_local(THREAD, threads_ah());
3738 JVM_END
3739
3740
3741 // Support for java.lang.Thread.getStackTrace() and getAllStackTraces() methods
3742 // Return StackTraceElement[][], each element is the stack trace of a thread in
3743 // the corresponding entry in the given threads array
3744 JVM_ENTRY(jobjectArray, JVM_DumpThreads(JNIEnv *env, jclass threadClass, jobjectArray threads))
3745 JvmtiVMObjectAllocEventCollector oam;
3746
3747 // Check if threads is null
3748 if (threads == nullptr) {
3749 THROW_NULL(vmSymbols::java_lang_NullPointerException());
3750 }
3751
3752 refArrayOop a = oop_cast<refArrayOop>(JNIHandles::resolve_non_null(threads));
3753 refArrayHandle ah(THREAD, a);
3754 int num_threads = ah->length();
3755 // check if threads is non-empty array
3756 if (num_threads == 0) {
3757 THROW_NULL(vmSymbols::java_lang_IllegalArgumentException());
3758 }
3759
3760 // check if threads is not an array of objects of Thread class
3761 Klass* k = ObjArrayKlass::cast(ah->klass())->element_klass();
3762 if (k != vmClasses::Thread_klass()) {
3763 THROW_NULL(vmSymbols::java_lang_IllegalArgumentException());
3764 }
3765
3766 ResourceMark rm(THREAD);
3767
3768 GrowableArray<instanceHandle>* thread_handle_array = new GrowableArray<instanceHandle>(num_threads);
3769 for (int i = 0; i < num_threads; i++) {
3770 oop thread_obj = ah->obj_at(i);
3771 instanceHandle h(THREAD, (instanceOop) thread_obj);
3772 thread_handle_array->append(h);
3773 }
|