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 *
23 */
24
25 #include "precompiled.hpp"
26 #include "cds/cdsConfig.hpp"
27 #include "cds/classListParser.hpp"
28 #include "cds/classListWriter.hpp"
29 #include "cds/dynamicArchive.hpp"
30 #include "cds/heapShared.hpp"
31 #include "cds/lambdaFormInvokers.hpp"
32 #include "classfile/classFileStream.hpp"
33 #include "classfile/classLoader.inline.hpp"
34 #include "classfile/classLoaderData.hpp"
35 #include "classfile/classLoaderData.inline.hpp"
36 #include "classfile/classLoadInfo.hpp"
37 #include "classfile/javaAssertions.hpp"
38 #include "classfile/javaClasses.inline.hpp"
39 #include "classfile/moduleEntry.hpp"
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"
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"
80 #include "runtime/handshake.hpp"
81 #include "runtime/java.hpp"
82 #include "runtime/javaCalls.hpp"
83 #include "runtime/javaThread.hpp"
84 #include "runtime/jfieldIDWorkaround.hpp"
85 #include "runtime/jniHandles.inline.hpp"
86 #include "runtime/os.inline.hpp"
87 #include "runtime/osThread.hpp"
88 #include "runtime/perfData.hpp"
89 #include "runtime/reflection.hpp"
90 #include "runtime/synchronizer.hpp"
91 #include "runtime/threadIdentifier.hpp"
92 #include "runtime/threadSMR.hpp"
93 #include "runtime/vframe.inline.hpp"
94 #include "runtime/vmOperations.hpp"
95 #include "runtime/vm_version.hpp"
96 #include "services/attachListener.hpp"
97 #include "services/management.hpp"
98 #include "services/threadService.hpp"
99 #include "utilities/checkedCast.hpp"
100 #include "utilities/copy.hpp"
101 #include "utilities/defaultStream.hpp"
102 #include "utilities/dtrace.hpp"
103 #include "utilities/events.hpp"
104 #include "utilities/macros.hpp"
105 #include "utilities/utf8.hpp"
106 #include "utilities/zipLibrary.hpp"
107 #if INCLUDE_CDS
108 #include "classfile/systemDictionaryShared.hpp"
226 if (source_file != nullptr) {
227 log_debug(class, resolve)("%s %s %s:%d (%s)", from, to, source_file, line_number, trace);
228 } else {
229 log_debug(class, resolve)("%s %s (%s)", from, to, trace);
230 }
231 }
232 }
233 }
234
235 void trace_class_resolution(Klass* to_class) {
236 EXCEPTION_MARK;
237 trace_class_resolution_impl(to_class, THREAD);
238 if (HAS_PENDING_EXCEPTION) {
239 CLEAR_PENDING_EXCEPTION;
240 }
241 }
242
243 // java.lang.System //////////////////////////////////////////////////////////////////////
244
245
246 JVM_LEAF(jlong, JVM_CurrentTimeMillis(JNIEnv *env, jclass ignored))
247 return os::javaTimeMillis();
248 JVM_END
249
250 JVM_LEAF(jlong, JVM_NanoTime(JNIEnv *env, jclass ignored))
251 return os::javaTimeNanos();
252 JVM_END
253
254 // The function below is actually exposed by jdk.internal.misc.VM and not
255 // java.lang.System, but we choose to keep it here so that it stays next
256 // to JVM_CurrentTimeMillis and JVM_NanoTime
257
258 const jlong MAX_DIFF_SECS = CONST64(0x0100000000); // 2^32
259 const jlong MIN_DIFF_SECS = -MAX_DIFF_SECS; // -2^32
260
261 JVM_LEAF(jlong, JVM_GetNanoTimeAdjustment(JNIEnv *env, jclass ignored, jlong offset_secs))
262 jlong seconds;
263 jlong nanos;
264
265 os::javaTimeSystemUTC(seconds, nanos);
266
267 // We're going to verify that the result can fit in a long.
268 // For that we need the difference in seconds between 'seconds'
269 // and 'offset_secs' to be such that:
270 // |seconds - offset_secs| < (2^63/10^9)
271 // We're going to approximate 10^9 ~< 2^30 (1000^3 ~< 1024^3)
272 // which makes |seconds - offset_secs| < 2^33
273 // and we will prefer +/- 2^32 as the maximum acceptable diff
274 // as 2^32 has a more natural feel than 2^33...
275 //
276 // So if |seconds - offset_secs| >= 2^32 - we return a special
277 // sentinel value (-1) which the caller should take as an
278 // exception value indicating that the offset given to us is
279 // too far from range of the current time - leading to too big
280 // a nano adjustment. The caller is expected to recover by
281 // computing a more accurate offset and calling this method
282 // again. (For the record 2^32 secs is ~136 years, so that
283 // should rarely happen)
284 //
285 jlong diff = seconds - offset_secs;
286 if (diff >= MAX_DIFF_SECS || diff <= MIN_DIFF_SECS) {
287 return -1; // sentinel value: the offset is too far off the target
288 }
289
290 // return the adjustment. If you compute a time by adding
291 // this number of nanoseconds along with the number of seconds
292 // in the offset you should get the current UTC time.
293 return (diff * (jlong)1000000000) + nanos;
294 JVM_END
295
296 JVM_ENTRY(void, JVM_ArrayCopy(JNIEnv *env, jclass ignored, jobject src, jint src_pos,
297 jobject dst, jint dst_pos, jint length))
298 // Check if we have null pointers
299 if (src == nullptr || dst == nullptr) {
300 THROW(vmSymbols::java_lang_NullPointerException());
301 }
302 arrayOop s = arrayOop(JNIHandles::resolve_non_null(src));
303 arrayOop d = arrayOop(JNIHandles::resolve_non_null(dst));
304 assert(oopDesc::is_oop(s), "JVM_ArrayCopy: src not an oop");
305 assert(oopDesc::is_oop(d), "JVM_ArrayCopy: dst not an oop");
306 // Do copy
307 s->klass()->copy_array(s, src_pos, d, dst_pos, length, thread);
308 JVM_END
309
310
311 static void set_property(Handle props, const char* key, const char* value, TRAPS) {
312 JavaValue r(T_OBJECT);
313 // public synchronized Object put(Object key, Object value);
314 HandleMark hm(THREAD);
315 Handle key_str = java_lang_String::create_from_platform_dependent_str(key, CHECK);
316 Handle value_str = java_lang_String::create_from_platform_dependent_str((value != nullptr ? value : ""), CHECK);
317 JavaCalls::call_virtual(&r,
318 props,
319 vmClasses::Properties_klass(),
320 vmSymbols::put_name(),
321 vmSymbols::object_object_object_signature(),
322 key_str,
323 value_str,
324 THREAD);
325 }
326
327
328 #define PUTPROP(props, name, value) set_property((props), (name), (value), CHECK_(properties));
329
330 /*
331 * Return all of the system properties in a Java String array with alternating
332 * names and values from the jvm SystemProperty.
333 * Which includes some internal and all commandline -D defined properties.
334 */
335 JVM_ENTRY(jobjectArray, JVM_GetProperties(JNIEnv *env))
336 ResourceMark rm(THREAD);
337 HandleMark hm(THREAD);
338 int ndx = 0;
339 int fixedCount = 2;
340
341 SystemProperty* p = Arguments::system_properties();
342 int count = Arguments::PropertyList_count(p);
343
344 // Allocate result String array
345 InstanceKlass* ik = vmClasses::String_klass();
346 objArrayOop r = oopFactory::new_objArray(ik, (count + fixedCount) * 2, CHECK_NULL);
347 objArrayHandle result_h(THREAD, r);
348
349 while (p != nullptr) {
350 const char * key = p->key();
351 if (strcmp(key, "sun.nio.MaxDirectMemorySize") != 0) {
352 const char * value = p->value();
353 Handle key_str = java_lang_String::create_from_platform_dependent_str(key, CHECK_NULL);
354 Handle value_str = java_lang_String::create_from_platform_dependent_str((value != nullptr ? value : ""), CHECK_NULL);
355 result_h->obj_at_put(ndx * 2, key_str());
403 Handle key_str = java_lang_String::create_from_platform_dependent_str("sun.management.compiler", CHECK_NULL);
404 Handle value_str = java_lang_String::create_from_platform_dependent_str(compiler_name, CHECK_NULL);
405 result_h->obj_at_put(ndx * 2, key_str());
406 result_h->obj_at_put(ndx * 2 + 1, value_str());
407 ndx++;
408 }
409 }
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
450 JVM_ENTRY_NO_ENV(void, JVM_GC(void))
451 if (!DisableExplicitGC) {
452 EventSystemGC event;
453 event.set_invokedConcurrent(ExplicitGCInvokesConcurrent);
454 Universe::heap()->collect(GCCause::_java_lang_system_gc);
455 event.commit();
456 }
457 JVM_END
458
459
460 JVM_LEAF(jlong, JVM_MaxObjectInspectionAge(void))
461 return Universe::heap()->millis_since_last_whole_heap_examined();
462 JVM_END
463
464
465 static inline jlong convert_size_t_to_jlong(size_t val) {
466 // In the 64-bit vm, a size_t can overflow a jlong (which is signed).
467 NOT_LP64 (return (jlong)val;)
468 LP64_ONLY(return (jlong)MIN2(val, (size_t)max_jlong);)
469 }
470
471 JVM_ENTRY_NO_ENV(jlong, JVM_TotalMemory(void))
472 size_t n = Universe::heap()->capacity();
473 return convert_size_t_to_jlong(n);
474 JVM_END
475
476
477 JVM_ENTRY_NO_ENV(jlong, JVM_FreeMemory(void))
478 size_t n = Universe::heap()->unused();
479 return convert_size_t_to_jlong(n);
480 JVM_END
481
482
483 JVM_ENTRY_NO_ENV(jlong, JVM_MaxMemory(void))
484 size_t n = Universe::heap()->max_capacity();
485 return convert_size_t_to_jlong(n);
486 JVM_END
487
488
489 JVM_ENTRY_NO_ENV(jint, JVM_ActiveProcessorCount(void))
490 return os::active_processor_count();
491 JVM_END
492
493 JVM_LEAF(jboolean, JVM_IsUseContainerSupport(void))
494 #ifdef LINUX
495 if (UseContainerSupport) {
496 return JNI_TRUE;
497 }
498 #endif
499 return JNI_FALSE;
500 JVM_END
501
502 JVM_LEAF(jboolean, JVM_IsContainerized(void))
503 #ifdef LINUX
504 if (OSContainer::is_containerized()) {
505 return JNI_TRUE;
506 }
507 #endif
508 return JNI_FALSE;
509 JVM_END
510
511 // java.lang.Throwable //////////////////////////////////////////////////////
512
513 JVM_ENTRY(void, JVM_FillInStackTrace(JNIEnv *env, jobject receiver))
514 Handle exception(thread, JNIHandles::resolve_non_null(receiver));
515 java_lang_Throwable::fill_in_stack_trace(exception);
516 JVM_END
517
518 // java.lang.NullPointerException ///////////////////////////////////////////
519
520 JVM_ENTRY(jstring, JVM_GetExtendedNPEMessage(JNIEnv *env, jthrowable throwable))
521 if (!ShowCodeDetailsInExceptionMessages) return nullptr;
522
523 oop exc = JNIHandles::resolve_non_null(throwable);
524
525 Method* method;
526 int bci;
527 if (!java_lang_Throwable::get_top_method_and_bci(exc, &method, &bci)) {
528 return nullptr;
529 }
530 if (method->is_native()) {
531 return nullptr;
532 }
533
534 stringStream ss;
535 bool ok = BytecodeUtils::get_NPE_message_at(&ss, method, bci);
536 if (ok) {
537 oop result = java_lang_String::create_oop_from_str(ss.base(), CHECK_NULL);
538 return (jstring) JNIHandles::make_local(THREAD, result);
539 } else {
540 return nullptr;
541 }
542 JVM_END
543
544 // java.lang.StackTraceElement //////////////////////////////////////////////
545
546
547 JVM_ENTRY(void, JVM_InitStackTraceElementArray(JNIEnv *env, jobjectArray elements, jobject backtrace, jint depth))
548 Handle backtraceh(THREAD, JNIHandles::resolve(backtrace));
549 objArrayOop st = objArrayOop(JNIHandles::resolve(elements));
550 objArrayHandle stack_trace(THREAD, st);
551 // Fill in the allocated stack trace
552 java_lang_Throwable::get_stack_trace_elements(depth, backtraceh, stack_trace, CHECK);
553 JVM_END
554
555
556 JVM_ENTRY(void, JVM_InitStackTraceElement(JNIEnv* env, jobject element, jobject stackFrameInfo))
557 Handle stack_frame_info(THREAD, JNIHandles::resolve_non_null(stackFrameInfo));
558 Handle stack_trace_element(THREAD, JNIHandles::resolve_non_null(element));
559 java_lang_StackFrameInfo::to_stack_trace_element(stack_frame_info, stack_trace_element, CHECK);
560 JVM_END
561
562
563 // java.lang.StackWalker //////////////////////////////////////////////////////
564 JVM_ENTRY(void, JVM_ExpandStackFrameInfo(JNIEnv *env, jobject obj))
565 Handle stack_frame_info(THREAD, JNIHandles::resolve_non_null(obj));
566
567 bool have_name = (java_lang_StackFrameInfo::name(stack_frame_info()) != nullptr);
568 bool have_type = (java_lang_StackFrameInfo::type(stack_frame_info()) != nullptr);
569 Method* method = java_lang_StackFrameInfo::get_method(stack_frame_info());
570 if (!have_name) {
571 oop name = StringTable::intern(method->name(), CHECK);
572 java_lang_StackFrameInfo::set_name(stack_frame_info(), name);
573 }
574 if (!have_type) {
575 Handle type = java_lang_String::create_from_symbol(method->signature(), CHECK);
576 java_lang_StackFrameInfo::set_type(stack_frame_info(), type());
577 }
578 JVM_END
579
580 JVM_ENTRY(jobject, JVM_CallStackWalk(JNIEnv *env, jobject stackStream, jint mode,
581 jint skip_frames, jobject contScope, jobject cont,
582 jint buffer_size, jint start_index, jobjectArray frames))
583 if (!thread->has_last_Java_frame()) {
584 THROW_MSG_(vmSymbols::java_lang_InternalError(), "doStackWalk: no stack trace", nullptr);
585 }
586
587 Handle stackStream_h(THREAD, JNIHandles::resolve_non_null(stackStream));
588 Handle contScope_h(THREAD, JNIHandles::resolve(contScope));
589 Handle cont_h(THREAD, JNIHandles::resolve(cont));
590 // frames array is a ClassFrameInfo[] array when only getting caller reference,
591 // and a StackFrameInfo[] array (or derivative) otherwise. It should never
592 // be null.
593 objArrayOop fa = objArrayOop(JNIHandles::resolve_non_null(frames));
594 objArrayHandle frames_array_h(THREAD, fa);
595
596 if (frames_array_h->length() < buffer_size) {
597 THROW_MSG_(vmSymbols::java_lang_IllegalArgumentException(), "not enough space in buffers", nullptr);
598 }
599
600 oop result = StackWalk::walk(stackStream_h, mode, skip_frames, contScope_h, cont_h,
601 buffer_size, start_index, frames_array_h, CHECK_NULL);
602 return JNIHandles::make_local(THREAD, result);
603 JVM_END
604
605
606 JVM_ENTRY(jint, JVM_MoreStackWalk(JNIEnv *env, jobject stackStream, jint mode, jlong anchor,
607 jint last_batch_count, jint buffer_size, jint start_index,
608 jobjectArray frames))
609 // frames array is a ClassFrameInfo[] array when only getting caller reference,
610 // and a StackFrameInfo[] array (or derivative) otherwise. It should never
611 // be null.
612 objArrayOop fa = objArrayOop(JNIHandles::resolve_non_null(frames));
613 objArrayHandle frames_array_h(THREAD, fa);
614
615 if (frames_array_h->length() < buffer_size) {
616 THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(), "not enough space in buffers");
617 }
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))
660 Handle obj(THREAD, JNIHandles::resolve_non_null(handle));
661 ObjectSynchronizer::notify(obj, CHECK);
662 JVM_END
663
664
665 JVM_ENTRY(void, JVM_MonitorNotifyAll(JNIEnv* env, jobject handle))
666 Handle obj(THREAD, JNIHandles::resolve_non_null(handle));
667 ObjectSynchronizer::notifyall(obj, CHECK);
668 JVM_END
669
670
671 JVM_ENTRY(jobject, JVM_Clone(JNIEnv* env, jobject handle))
672 Handle obj(THREAD, JNIHandles::resolve_non_null(handle));
673 Klass* klass = obj->klass();
674 JvmtiVMObjectAllocEventCollector oam;
675
676 #ifdef ASSERT
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() &&
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);
717 }
718
719 return JNIHandles::make_local(THREAD, new_obj());
720 JVM_END
721
722 // java.lang.ref.Finalizer ////////////////////////////////////////////////////
723
724 JVM_ENTRY(void, JVM_ReportFinalizationComplete(JNIEnv * env, jobject finalizee))
725 MANAGEMENT_ONLY(FinalizerService::on_complete(JNIHandles::resolve_non_null(finalizee), THREAD);)
726 JVM_END
727
728 JVM_LEAF(jboolean, JVM_IsFinalizationEnabled(JNIEnv * env))
729 return InstanceKlass::is_finalization_enabled();
730 JVM_END
731
732 // jdk.internal.vm.Continuation /////////////////////////////////////////////////////
733
734 JVM_ENTRY(void, JVM_RegisterContinuationMethods(JNIEnv *env, jclass cls))
735 CONT_RegisterNativeMethods(env, cls);
736 JVM_END
737
738 // java.io.File ///////////////////////////////////////////////////////////////
739
740 JVM_LEAF(char*, JVM_NativePath(char* path))
741 return os::native_path(path);
742 JVM_END
743
744
745 // Misc. class handling ///////////////////////////////////////////////////////////
746
747
748 JVM_ENTRY(jclass, JVM_GetCallerClass(JNIEnv* env))
749 // Getting the class of the caller frame.
750 //
751 // The call stack at this point looks something like this:
752 //
753 // [0] [ @CallerSensitive public jdk.internal.reflect.Reflection.getCallerClass ]
754 // [1] [ @CallerSensitive API.method ]
755 // [.] [ (skipped intermediate frames) ]
756 // [n] [ caller ]
757 vframeStream vfst(thread);
758 // Cf. LibraryCallKit::inline_native_Reflection_getCallerClass
759 for (int n = 0; !vfst.at_end(); vfst.security_next(), n++) {
760 Method* m = vfst.method();
761 assert(m != nullptr, "sanity");
762 switch (n) {
763 case 0:
764 // This must only be called from Reflection.getCallerClass
765 if (m->intrinsic_id() != vmIntrinsics::_getCallerClass) {
766 THROW_MSG_NULL(vmSymbols::java_lang_InternalError(), "JVM_GetCallerClass must only be called from Reflection.getCallerClass");
767 }
768 // fall-through
769 case 1:
770 // Frame 0 and 1 must be caller sensitive.
771 if (!m->caller_sensitive()) {
772 THROW_MSG_NULL(vmSymbols::java_lang_InternalError(), err_msg("CallerSensitive annotation expected at frame %d", n));
773 }
774 break;
775 default:
776 if (!m->is_ignored_by_security_stack_walk()) {
777 // We have reached the desired frame; return the holder class.
778 return (jclass) JNIHandles::make_local(THREAD, m->method_holder()->java_mirror());
779 }
780 break;
781 }
782 }
783 return nullptr;
784 JVM_END
785
786
787 JVM_ENTRY(jclass, JVM_FindPrimitiveClass(JNIEnv* env, const char* utf))
788 oop mirror = nullptr;
789 BasicType t = name2type(utf);
790 if (t != T_ILLEGAL && !is_reference_type(t)) {
791 mirror = Universe::java_mirror(t);
792 }
793 if (mirror == nullptr) {
794 THROW_MSG_NULL(vmSymbols::java_lang_ClassNotFoundException(), (char*) utf);
795 } else {
796 return (jclass) JNIHandles::make_local(THREAD, mirror);
797 }
798 JVM_END
799
800
801 // Returns a class loaded by the bootstrap class loader; or null
802 // if not found. ClassNotFoundException is not thrown.
803 // FindClassFromBootLoader is exported to the launcher for windows.
804 JVM_ENTRY(jclass, JVM_FindClassFromBootLoader(JNIEnv* env,
805 const char* name))
806 // Java libraries should ensure that name is never null or illegal.
807 if (name == nullptr || (int)strlen(name) > Symbol::max_length()) {
808 // It's impossible to create this class; the name cannot fit
809 // into the constant pool.
810 return nullptr;
811 }
812 assert(UTF8::is_legal_utf8((const unsigned char*)name, strlen(name), false), "illegal UTF name");
813
814 TempNewSymbol h_name = SymbolTable::new_symbol(name);
815 Klass* k = SystemDictionary::resolve_or_null(h_name, CHECK_NULL);
816 if (k == nullptr) {
817 return nullptr;
818 }
819
820 if (log_is_enabled(Debug, class, resolve)) {
821 trace_class_resolution(k);
822 }
823 return (jclass) JNIHandles::make_local(THREAD, k->java_mirror());
824 JVM_END
825
826 // Find a class with this name in this loader, using the caller's protection domain.
827 JVM_ENTRY(jclass, JVM_FindClassFromCaller(JNIEnv* env, const char* name,
828 jboolean init, jobject loader,
829 jclass caller))
830 TempNewSymbol h_name =
831 SystemDictionary::class_name_symbol(name, vmSymbols::java_lang_ClassNotFoundException(),
832 CHECK_NULL);
833
834 oop loader_oop = JNIHandles::resolve(loader);
835 oop from_class = JNIHandles::resolve(caller);
836 oop protection_domain = nullptr;
837 // If loader is null, shouldn't call ClassLoader.checkPackageAccess; otherwise get
838 // NPE. Put it in another way, the bootstrap class loader has all permission and
839 // thus no checkPackageAccess equivalence in the VM class loader.
840 // The caller is also passed as null by the java code if there is no security
841 // manager to avoid the performance cost of getting the calling class.
842 if (from_class != nullptr && loader_oop != nullptr) {
843 protection_domain = java_lang_Class::as_Klass(from_class)->protection_domain();
844 }
845
846 Handle h_loader(THREAD, loader_oop);
847 Handle h_prot(THREAD, protection_domain);
848 jclass result = find_class_from_class_loader(env, h_name, init, h_loader,
849 h_prot, false, THREAD);
850
851 if (log_is_enabled(Debug, class, resolve) && result != nullptr) {
852 trace_class_resolution(java_lang_Class::as_Klass(JNIHandles::resolve_non_null(result)));
853 }
854 return result;
855 JVM_END
856
857 // Currently only called from the old verifier.
858 JVM_ENTRY(jclass, JVM_FindClassFromClass(JNIEnv *env, const char *name,
859 jboolean init, jclass from))
860 TempNewSymbol h_name =
861 SystemDictionary::class_name_symbol(name, vmSymbols::java_lang_ClassNotFoundException(),
862 CHECK_NULL);
863 oop from_class_oop = JNIHandles::resolve(from);
864 Klass* from_class = (from_class_oop == nullptr)
865 ? (Klass*)nullptr
866 : java_lang_Class::as_Klass(from_class_oop);
867 oop class_loader = nullptr;
868 oop protection_domain = nullptr;
869 if (from_class != nullptr) {
870 class_loader = from_class->class_loader();
871 protection_domain = from_class->protection_domain();
872 }
873 Handle h_loader(THREAD, class_loader);
874 Handle h_prot (THREAD, protection_domain);
875 jclass result = find_class_from_class_loader(env, h_name, init, h_loader,
876 h_prot, true, thread);
877
878 if (log_is_enabled(Debug, class, resolve) && result != nullptr) {
879 // this function is generally only used for class loading during verification.
1042 ik->external_name(),
1043 host_class->external_name(),
1044 ik->is_hidden() ? "is hidden" : "is not hidden");
1045 }
1046 }
1047
1048 if ((!is_hidden || is_nestmate) && !Reflection::is_same_class_package(lookup_k, ik)) {
1049 // non-hidden class or nestmate class must be in the same package as the Lookup class
1050 THROW_MSG_NULL(vmSymbols::java_lang_IllegalArgumentException(), "Lookup class and defined class are in different packages");
1051 }
1052
1053 if (init) {
1054 ik->initialize(CHECK_NULL);
1055 } else {
1056 ik->link_class(CHECK_NULL);
1057 }
1058
1059 return (jclass) JNIHandles::make_local(THREAD, ik->java_mirror());
1060 }
1061
1062 JVM_ENTRY(jclass, JVM_DefineClass(JNIEnv *env, const char *name, jobject loader, const jbyte *buf, jsize len, jobject pd))
1063 return jvm_define_class_common(name, loader, buf, len, pd, nullptr, THREAD);
1064 JVM_END
1065
1066 /*
1067 * Define a class with the specified lookup class.
1068 * lookup: Lookup class
1069 * name: the name of the class
1070 * buf: class bytes
1071 * len: length of class bytes
1072 * pd: protection domain
1073 * init: initialize the class
1074 * flags: properties of the class
1075 * classData: private static pre-initialized field
1076 */
1077 JVM_ENTRY(jclass, JVM_LookupDefineClass(JNIEnv *env, jclass lookup, const char *name, const jbyte *buf,
1078 jsize len, jobject pd, jboolean initialize, int flags, jobject classData))
1079
1080 if (lookup == nullptr) {
1081 THROW_MSG_NULL(vmSymbols::java_lang_IllegalArgumentException(), "Lookup class is null");
1082 }
1083
1084 assert(buf != nullptr, "buf must not be null");
1085
1086 return jvm_lookup_define_class(lookup, name, buf, len, pd, initialize, flags, classData, THREAD);
1087 JVM_END
1088
1089 JVM_ENTRY(jclass, JVM_DefineClassWithSource(JNIEnv *env, const char *name, jobject loader, const jbyte *buf, jsize len, jobject pd, const char *source))
1090
1091 return jvm_define_class_common(name, loader, buf, len, pd, source, THREAD);
1092 JVM_END
1093
1094 JVM_ENTRY(jclass, JVM_FindLoadedClass(JNIEnv *env, jobject loader, jstring name))
1095 ResourceMark rm(THREAD);
1096
1097 Handle h_name (THREAD, JNIHandles::resolve_non_null(name));
1098 char* str = java_lang_String::as_utf8_string(h_name());
1099
1100 // Sanity check, don't expect null
1101 if (str == nullptr) return nullptr;
1102
1103 // Internalize the string, converting '.' to '/' in string.
1104 char* p = (char*)str;
1105 while (*p != '\0') {
1106 if (*p == '.') {
1107 *p = '/';
1108 }
1109 p++;
1110 }
1111
1112 const int str_len = (int)(p - str);
1113 if (str_len > Symbol::max_length()) {
1114 // It's impossible to create this class; the name cannot fit
1120 // Security Note:
1121 // The Java level wrapper will perform the necessary security check allowing
1122 // us to pass the null as the initiating class loader.
1123 Handle h_loader(THREAD, JNIHandles::resolve(loader));
1124 Klass* k = SystemDictionary::find_instance_or_array_klass(THREAD, klass_name,
1125 h_loader,
1126 Handle());
1127 #if INCLUDE_CDS
1128 if (k == nullptr) {
1129 // If the class is not already loaded, try to see if it's in the shared
1130 // archive for the current classloader (h_loader).
1131 k = SystemDictionaryShared::find_or_load_shared_class(klass_name, h_loader, CHECK_NULL);
1132 }
1133 #endif
1134 return (k == nullptr) ? nullptr :
1135 (jclass) JNIHandles::make_local(THREAD, k->java_mirror());
1136 JVM_END
1137
1138 // Module support //////////////////////////////////////////////////////////////////////////////
1139
1140 JVM_ENTRY(void, JVM_DefineModule(JNIEnv *env, jobject module, jboolean is_open, jstring version,
1141 jstring location, jobjectArray packages))
1142 Handle h_module (THREAD, JNIHandles::resolve(module));
1143 Modules::define_module(h_module, is_open, version, location, packages, CHECK);
1144 JVM_END
1145
1146 JVM_ENTRY(void, JVM_SetBootLoaderUnnamedModule(JNIEnv *env, jobject module))
1147 Handle h_module (THREAD, JNIHandles::resolve(module));
1148 Modules::set_bootloader_unnamed_module(h_module, CHECK);
1149 JVM_END
1150
1151 JVM_ENTRY(void, JVM_AddModuleExports(JNIEnv *env, jobject from_module, jstring package, jobject to_module))
1152 Handle h_from_module (THREAD, JNIHandles::resolve(from_module));
1153 Handle h_to_module (THREAD, JNIHandles::resolve(to_module));
1154 Modules::add_module_exports_qualified(h_from_module, package, h_to_module, CHECK);
1155 JVM_END
1156
1157 JVM_ENTRY(void, JVM_AddModuleExportsToAllUnnamed(JNIEnv *env, jobject from_module, jstring package))
1158 Handle h_from_module (THREAD, JNIHandles::resolve(from_module));
1159 Modules::add_module_exports_to_all_unnamed(h_from_module, package, CHECK);
1160 JVM_END
1161
1162 JVM_ENTRY(void, JVM_AddModuleExportsToAll(JNIEnv *env, jobject from_module, jstring package))
1163 Handle h_from_module (THREAD, JNIHandles::resolve(from_module));
1164 Modules::add_module_exports(h_from_module, package, Handle(), CHECK);
1165 JVM_END
1166
1167 JVM_ENTRY (void, JVM_AddReadsModule(JNIEnv *env, jobject from_module, jobject source_module))
1168 Handle h_from_module (THREAD, JNIHandles::resolve(from_module));
1169 Handle h_source_module (THREAD, JNIHandles::resolve(source_module));
1170 Modules::add_reads_module(h_from_module, h_source_module, CHECK);
1171 JVM_END
1172
1173 JVM_ENTRY(void, JVM_DefineArchivedModules(JNIEnv *env, jobject platform_loader, jobject system_loader))
1174 Handle h_platform_loader (THREAD, JNIHandles::resolve(platform_loader));
1175 Handle h_system_loader (THREAD, JNIHandles::resolve(system_loader));
1176 Modules::define_archived_modules(h_platform_loader, h_system_loader, CHECK);
1177 JVM_END
1178
1179 // Reflection support //////////////////////////////////////////////////////////////////////////////
1180
1181 JVM_ENTRY(jstring, JVM_InitClassName(JNIEnv *env, jclass cls))
1182 assert (cls != nullptr, "illegal class");
1183 JvmtiVMObjectAllocEventCollector oam;
1184 ResourceMark rm(THREAD);
1185 HandleMark hm(THREAD);
1186 Handle java_class(THREAD, JNIHandles::resolve(cls));
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());
1275 return JNIHandles::make_local(THREAD, result);
1276 JVM_END
1277
1278 JVM_ENTRY(jobject, JVM_GetStackAccessControlContext(JNIEnv *env, jclass cls))
1279 if (!UsePrivilegedStack) return nullptr;
1280
1281 ResourceMark rm(THREAD);
1282 GrowableArray<Handle>* local_array = new GrowableArray<Handle>(12);
1283 JvmtiVMObjectAllocEventCollector oam;
1284
1285 // count the protection domains on the execution stack. We collapse
1286 // duplicate consecutive protection domains into a single one, as
1287 // well as stopping when we hit a privileged frame.
1288
1289 oop previous_protection_domain = nullptr;
1290 Handle privileged_context(thread, nullptr);
1291 bool is_privileged = false;
1292 oop protection_domain = nullptr;
1293
1294 // Iterate through Java frames
1295 vframeStream vfst(thread);
1296 for(; !vfst.at_end(); vfst.next()) {
1297 // get method of frame
1298 Method* method = vfst.method();
1342 local_array->length(), CHECK_NULL);
1343 objArrayHandle h_context(thread, context);
1344 for (int index = 0; index < local_array->length(); index++) {
1345 h_context->obj_at_put(index, local_array->at(index)());
1346 }
1347
1348 oop result = java_security_AccessControlContext::create(h_context, is_privileged, privileged_context, CHECK_NULL);
1349
1350 return JNIHandles::make_local(THREAD, result);
1351 JVM_END
1352
1353 class ScopedValueBindingsResolver {
1354 public:
1355 InstanceKlass* Carrier_klass;
1356 ScopedValueBindingsResolver(JavaThread* THREAD) {
1357 Klass *k = SystemDictionary::resolve_or_fail(vmSymbols::java_lang_ScopedValue_Carrier(), true, THREAD);
1358 Carrier_klass = InstanceKlass::cast(k);
1359 }
1360 };
1361
1362 JVM_ENTRY(jobject, JVM_FindScopedValueBindings(JNIEnv *env, jclass cls))
1363 ResourceMark rm(THREAD);
1364 GrowableArray<Handle>* local_array = new GrowableArray<Handle>(12);
1365 JvmtiVMObjectAllocEventCollector oam;
1366
1367 static ScopedValueBindingsResolver resolver(THREAD);
1368
1369 // Iterate through Java frames
1370 vframeStream vfst(thread);
1371 for(; !vfst.at_end(); vfst.next()) {
1372 int loc = -1;
1373 // get method of frame
1374 Method* method = vfst.method();
1375
1376 Symbol *name = method->name();
1377
1378 InstanceKlass* holder = method->method_holder();
1379 if (name == vmSymbols::runWith_method_name()) {
1380 if (holder == vmClasses::Thread_klass()
1381 || holder == resolver.Carrier_klass) {
1382 loc = 1;
1383 }
1384 }
1385
1386 if (loc != -1) {
1387 javaVFrame *frame = vfst.asJavaVFrame();
1388 StackValueCollection* locals = frame->locals();
1389 StackValue* head_sv = locals->at(loc); // java/lang/ScopedValue$Snapshot
1390 Handle result = head_sv->get_obj();
1391 assert(!head_sv->obj_is_scalar_replaced(), "found scalar-replaced object");
1392 if (result() != nullptr) {
1393 return JNIHandles::make_local(THREAD, result());
1394 }
1395 }
1396 }
1397
1398 return nullptr;
1399 JVM_END
1400
1401 JVM_ENTRY(jboolean, JVM_IsArrayClass(JNIEnv *env, jclass cls))
1402 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls));
1403 return (k != nullptr) && k->is_array_klass() ? true : false;
1404 JVM_END
1405
1406
1407 JVM_ENTRY(jboolean, JVM_IsPrimitiveClass(JNIEnv *env, jclass cls))
1408 oop mirror = JNIHandles::resolve_non_null(cls);
1409 return (jboolean) java_lang_Class::is_primitive(mirror);
1410 JVM_END
1411
1412
1413 JVM_ENTRY(jint, JVM_GetClassModifiers(JNIEnv *env, jclass cls))
1414 oop mirror = JNIHandles::resolve_non_null(cls);
1415 if (java_lang_Class::is_primitive(mirror)) {
1416 // Primitive type
1417 return JVM_ACC_ABSTRACT | JVM_ACC_FINAL | JVM_ACC_PUBLIC;
1418 }
1419
1420 Klass* k = java_lang_Class::as_Klass(mirror);
1421 debug_only(int computed_modifiers = k->compute_modifier_flags());
1422 assert(k->modifier_flags() == computed_modifiers, "modifiers cache is OK");
1423 return k->modifier_flags();
1424 JVM_END
1425
1426
1427 // Inner class reflection ///////////////////////////////////////////////////////////////////////////////
1428
1429 JVM_ENTRY(jobjectArray, JVM_GetDeclaredClasses(JNIEnv *env, jclass ofClass))
1430 JvmtiVMObjectAllocEventCollector oam;
1431 // ofClass is a reference to a java_lang_Class object. The mirror object
1432 // of an InstanceKlass
1433 oop ofMirror = JNIHandles::resolve_non_null(ofClass);
1434 if (java_lang_Class::is_primitive(ofMirror) ||
1435 ! java_lang_Class::as_Klass(ofMirror)->is_instance_klass()) {
1436 oop result = oopFactory::new_objArray(vmClasses::Class_klass(), 0, CHECK_NULL);
1437 return (jobjectArray)JNIHandles::make_local(THREAD, result);
1438 }
1439
1440 InstanceKlass* k = InstanceKlass::cast(java_lang_Class::as_Klass(ofMirror));
1441 InnerClassesIterator iter(k);
1442
1443 if (iter.length() == 0) {
1444 // Neither an inner nor outer class
1445 oop result = oopFactory::new_objArray(vmClasses::Class_klass(), 0, CHECK_NULL);
1446 return (jobjectArray)JNIHandles::make_local(THREAD, result);
1447 }
1448
1449 // find inner class info
1475 result->obj_at_put(members, inner_klass->java_mirror());
1476 members++;
1477 }
1478 }
1479 }
1480 }
1481
1482 if (members != length) {
1483 // Return array of right length
1484 objArrayOop res = oopFactory::new_objArray(vmClasses::Class_klass(), members, CHECK_NULL);
1485 for(int i = 0; i < members; i++) {
1486 res->obj_at_put(i, result->obj_at(i));
1487 }
1488 return (jobjectArray)JNIHandles::make_local(THREAD, res);
1489 }
1490
1491 return (jobjectArray)JNIHandles::make_local(THREAD, result());
1492 JVM_END
1493
1494
1495 JVM_ENTRY(jclass, JVM_GetDeclaringClass(JNIEnv *env, jclass ofClass))
1496 {
1497 // ofClass is a reference to a java_lang_Class object.
1498 oop ofMirror = JNIHandles::resolve_non_null(ofClass);
1499 if (java_lang_Class::is_primitive(ofMirror)) {
1500 return nullptr;
1501 }
1502 Klass* klass = java_lang_Class::as_Klass(ofMirror);
1503 if (!klass->is_instance_klass()) {
1504 return nullptr;
1505 }
1506
1507 bool inner_is_member = false;
1508 Klass* outer_klass
1509 = InstanceKlass::cast(klass)->compute_enclosing_class(&inner_is_member, CHECK_NULL);
1510 if (outer_klass == nullptr) return nullptr; // already a top-level class
1511 if (!inner_is_member) return nullptr; // a hidden class (inside a method)
1512 return (jclass) JNIHandles::make_local(THREAD, outer_klass->java_mirror());
1513 }
1514 JVM_END
1515
1516 JVM_ENTRY(jstring, JVM_GetSimpleBinaryName(JNIEnv *env, jclass cls))
1517 {
1518 oop mirror = JNIHandles::resolve_non_null(cls);
1519 if (java_lang_Class::is_primitive(mirror)) {
1520 return nullptr;
1521 }
1522 Klass* klass = java_lang_Class::as_Klass(mirror);
1523 if (!klass->is_instance_klass()) {
1524 return nullptr;
1525 }
1526 InstanceKlass* k = InstanceKlass::cast(klass);
1527 int ooff = 0, noff = 0;
1528 if (k->find_inner_classes_attr(&ooff, &noff, THREAD)) {
1529 if (noff != 0) {
1530 constantPoolHandle i_cp(thread, k->constants());
1531 Symbol* name = i_cp->symbol_at(noff);
1532 Handle str = java_lang_String::create_from_symbol(name, CHECK_NULL);
1533 return (jstring) JNIHandles::make_local(THREAD, str());
1534 }
1535 }
1536 return nullptr;
1537 }
1538 JVM_END
1539
1540 JVM_ENTRY(jstring, JVM_GetClassSignature(JNIEnv *env, jclass cls))
1541 assert (cls != nullptr, "illegal class");
1542 JvmtiVMObjectAllocEventCollector oam;
1543 ResourceMark rm(THREAD);
1544 oop mirror = JNIHandles::resolve_non_null(cls);
1545 // Return null for arrays and primitives
1546 if (!java_lang_Class::is_primitive(mirror)) {
1547 Klass* k = java_lang_Class::as_Klass(mirror);
1548 if (k->is_instance_klass()) {
1549 Symbol* sym = InstanceKlass::cast(k)->generic_signature();
1550 if (sym == nullptr) return nullptr;
1551 Handle str = java_lang_String::create_from_symbol(sym, CHECK_NULL);
1552 return (jstring) JNIHandles::make_local(THREAD, str());
1553 }
1554 }
1555 return nullptr;
1556 JVM_END
1557
1558
1559 JVM_ENTRY(jbyteArray, JVM_GetClassAnnotations(JNIEnv *env, jclass cls))
1560 assert (cls != nullptr, "illegal class");
1561 oop mirror = JNIHandles::resolve_non_null(cls);
1562 // Return null for arrays and primitives
1563 if (!java_lang_Class::is_primitive(mirror)) {
1564 Klass* k = java_lang_Class::as_Klass(mirror);
1565 if (k->is_instance_klass()) {
1566 typeArrayOop a = Annotations::make_java_array(InstanceKlass::cast(k)->class_annotations(), CHECK_NULL);
1567 return (jbyteArray) JNIHandles::make_local(THREAD, a);
1568 }
1569 }
1570 return nullptr;
1571 JVM_END
1572
1573
1574 static bool jvm_get_field_common(jobject field, fieldDescriptor& fd) {
1575 // some of this code was adapted from from jni_FromReflectedField
1576
1577 oop reflected = JNIHandles::resolve_non_null(field);
1578 oop mirror = java_lang_reflect_Field::clazz(reflected);
1579 Klass* k = java_lang_Class::as_Klass(mirror);
1608 int slot = 0;
1609
1610 if (reflected->klass() == vmClasses::reflect_Constructor_klass()) {
1611 mirror = java_lang_reflect_Constructor::clazz(reflected);
1612 slot = java_lang_reflect_Constructor::slot(reflected);
1613 } else {
1614 assert(reflected->klass() == vmClasses::reflect_Method_klass(),
1615 "wrong type");
1616 mirror = java_lang_reflect_Method::clazz(reflected);
1617 slot = java_lang_reflect_Method::slot(reflected);
1618 }
1619 Klass* k = java_lang_Class::as_Klass(mirror);
1620
1621 Method* m = InstanceKlass::cast(k)->method_with_idnum(slot);
1622 assert(m != nullptr, "cannot find method");
1623 return m; // caller has to deal with null in product mode
1624 }
1625
1626 /* Type use annotations support (JDK 1.8) */
1627
1628 JVM_ENTRY(jbyteArray, JVM_GetClassTypeAnnotations(JNIEnv *env, jclass cls))
1629 assert (cls != nullptr, "illegal class");
1630 ResourceMark rm(THREAD);
1631 // Return null for arrays and primitives
1632 if (!java_lang_Class::is_primitive(JNIHandles::resolve(cls))) {
1633 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve(cls));
1634 if (k->is_instance_klass()) {
1635 AnnotationArray* type_annotations = InstanceKlass::cast(k)->class_type_annotations();
1636 if (type_annotations != nullptr) {
1637 typeArrayOop a = Annotations::make_java_array(type_annotations, CHECK_NULL);
1638 return (jbyteArray) JNIHandles::make_local(THREAD, a);
1639 }
1640 }
1641 }
1642 return nullptr;
1643 JVM_END
1644
1645 JVM_ENTRY(jbyteArray, JVM_GetMethodTypeAnnotations(JNIEnv *env, jobject method))
1646 assert (method != nullptr, "illegal method");
1647 // method is a handle to a java.lang.reflect.Method object
1648 Method* m = jvm_get_method_common(method);
1649 if (m == nullptr) {
1650 return nullptr;
1651 }
1652
1653 AnnotationArray* type_annotations = m->type_annotations();
1654 if (type_annotations != nullptr) {
1655 typeArrayOop a = Annotations::make_java_array(type_annotations, CHECK_NULL);
1656 return (jbyteArray) JNIHandles::make_local(THREAD, a);
1657 }
1658
1659 return nullptr;
1660 JVM_END
1661
1662 JVM_ENTRY(jbyteArray, JVM_GetFieldTypeAnnotations(JNIEnv *env, jobject field))
1663 assert (field != nullptr, "illegal field");
1664 fieldDescriptor fd;
1665 bool gotFd = jvm_get_field_common(field, fd);
1666 if (!gotFd) {
1667 return nullptr;
1668 }
1669
1670 return (jbyteArray) JNIHandles::make_local(THREAD, Annotations::make_java_array(fd.type_annotations(), THREAD));
1671 JVM_END
1672
1673 static void bounds_check(const constantPoolHandle& cp, jint index, TRAPS) {
1674 if (!cp->is_within_bounds(index)) {
1675 THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), "Constant pool index out of bounds");
1676 }
1677 }
1678
1679 JVM_ENTRY(jobjectArray, JVM_GetMethodParameters(JNIEnv *env, jobject method))
1680 {
1681 // method is a handle to a java.lang.reflect.Method object
1682 Method* method_ptr = jvm_get_method_common(method);
1683 methodHandle mh (THREAD, method_ptr);
1684 Handle reflected_method (THREAD, JNIHandles::resolve_non_null(method));
1685 const int num_params = mh->method_parameters_length();
1686
1687 if (num_params < 0) {
1688 // A -1 return value from method_parameters_length means there is no
1689 // parameter data. Return null to indicate this to the reflection
1690 // API.
1691 assert(num_params == -1, "num_params should be -1 if it is less than zero");
1692 return (jobjectArray)nullptr;
1693 } else {
1694 // Otherwise, we return something up to reflection, even if it is
1695 // a zero-length array. Why? Because in some cases this can
1696 // trigger a MalformedParametersException.
1697
1698 // make sure all the symbols are properly formatted
1699 for (int i = 0; i < num_params; i++) {
1712 objArrayOop result_oop = oopFactory::new_objArray(vmClasses::reflect_Parameter_klass(), num_params, CHECK_NULL);
1713 objArrayHandle result (THREAD, result_oop);
1714
1715 for (int i = 0; i < num_params; i++) {
1716 MethodParametersElement* params = mh->method_parameters_start();
1717 // For a 0 index, give a null symbol
1718 Symbol* sym = 0 != params[i].name_cp_index ?
1719 mh->constants()->symbol_at(params[i].name_cp_index) : nullptr;
1720 int flags = params[i].flags;
1721 oop param = Reflection::new_parameter(reflected_method, i, sym,
1722 flags, CHECK_NULL);
1723 result->obj_at_put(i, param);
1724 }
1725 return (jobjectArray)JNIHandles::make_local(THREAD, result());
1726 }
1727 }
1728 JVM_END
1729
1730 // New (JDK 1.4) reflection implementation /////////////////////////////////////
1731
1732 JVM_ENTRY(jobjectArray, JVM_GetClassDeclaredFields(JNIEnv *env, jclass ofClass, jboolean publicOnly))
1733 {
1734 JvmtiVMObjectAllocEventCollector oam;
1735
1736 oop ofMirror = JNIHandles::resolve_non_null(ofClass);
1737 // Exclude primitive types and array types
1738 if (java_lang_Class::is_primitive(ofMirror) ||
1739 java_lang_Class::as_Klass(ofMirror)->is_array_klass()) {
1740 // Return empty array
1741 oop res = oopFactory::new_objArray(vmClasses::reflect_Field_klass(), 0, CHECK_NULL);
1742 return (jobjectArray) JNIHandles::make_local(THREAD, res);
1743 }
1744
1745 InstanceKlass* k = InstanceKlass::cast(java_lang_Class::as_Klass(ofMirror));
1746 constantPoolHandle cp(THREAD, k->constants());
1747
1748 // Ensure class is linked
1749 k->link_class(CHECK_NULL);
1750
1751 // Allocate result
1752 int num_fields;
1763 objArrayOop r = oopFactory::new_objArray(vmClasses::reflect_Field_klass(), num_fields, CHECK_NULL);
1764 objArrayHandle result (THREAD, r);
1765
1766 int out_idx = 0;
1767 fieldDescriptor fd;
1768 for (JavaFieldStream fs(k); !fs.done(); fs.next()) {
1769 if (!publicOnly || fs.access_flags().is_public()) {
1770 fd.reinitialize(k, fs.index());
1771 oop field = Reflection::new_field(&fd, CHECK_NULL);
1772 result->obj_at_put(out_idx, field);
1773 ++out_idx;
1774 }
1775 }
1776 assert(out_idx == num_fields, "just checking");
1777 return (jobjectArray) JNIHandles::make_local(THREAD, result());
1778 }
1779 JVM_END
1780
1781 // A class is a record if and only if it is final and a direct subclass of
1782 // java.lang.Record and has a Record attribute; otherwise, it is not a record.
1783 JVM_ENTRY(jboolean, JVM_IsRecord(JNIEnv *env, jclass cls))
1784 {
1785 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls));
1786 if (k != nullptr && k->is_instance_klass()) {
1787 InstanceKlass* ik = InstanceKlass::cast(k);
1788 return ik->is_record();
1789 } else {
1790 return false;
1791 }
1792 }
1793 JVM_END
1794
1795 // Returns an array containing the components of the Record attribute,
1796 // or null if the attribute is not present.
1797 //
1798 // Note that this function returns the components of the Record attribute
1799 // even if the class is not a record.
1800 JVM_ENTRY(jobjectArray, JVM_GetRecordComponents(JNIEnv* env, jclass ofClass))
1801 {
1802 Klass* c = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(ofClass));
1803 assert(c->is_instance_klass(), "must be");
1804 InstanceKlass* ik = InstanceKlass::cast(c);
1805
1806 Array<RecordComponent*>* components = ik->record_components();
1807 if (components != nullptr) {
1808 JvmtiVMObjectAllocEventCollector oam;
1809 constantPoolHandle cp(THREAD, ik->constants());
1810 int length = components->length();
1811 assert(length >= 0, "unexpected record_components length");
1812 objArrayOop record_components =
1813 oopFactory::new_objArray(vmClasses::RecordComponent_klass(), length, CHECK_NULL);
1814 objArrayHandle components_h (THREAD, record_components);
1815
1816 for (int x = 0; x < length; x++) {
1817 RecordComponent* component = components->at(x);
1818 assert(component != nullptr, "unexpected null record component");
1819 oop component_oop = java_lang_reflect_RecordComponent::create(ik, component, CHECK_NULL);
1820 components_h->obj_at_put(x, component_oop);
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))
1916 {
1917 return get_class_declared_methods_helper(env, ofClass, publicOnly,
1918 /*want_constructor*/ true,
1919 vmClasses::reflect_Constructor_klass(), THREAD);
1920 }
1921 JVM_END
1922
1923 JVM_ENTRY(jint, JVM_GetClassAccessFlags(JNIEnv *env, jclass cls))
1924 {
1925 oop mirror = JNIHandles::resolve_non_null(cls);
1926 if (java_lang_Class::is_primitive(mirror)) {
1927 // Primitive type
1928 return JVM_ACC_ABSTRACT | JVM_ACC_FINAL | JVM_ACC_PUBLIC;
1929 }
1930
1931 Klass* k = java_lang_Class::as_Klass(mirror);
1932 return k->access_flags().as_int() & JVM_ACC_WRITTEN_FLAGS;
1933 }
1934 JVM_END
1935
1936 JVM_ENTRY(jboolean, JVM_AreNestMates(JNIEnv *env, jclass current, jclass member))
1937 {
1938 Klass* c = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(current));
1939 assert(c->is_instance_klass(), "must be");
1940 InstanceKlass* ck = InstanceKlass::cast(c);
1941 Klass* m = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(member));
1942 assert(m->is_instance_klass(), "must be");
1943 InstanceKlass* mk = InstanceKlass::cast(m);
1944 return ck->has_nestmate_access_to(mk, THREAD);
1945 }
1946 JVM_END
1947
1948 JVM_ENTRY(jclass, JVM_GetNestHost(JNIEnv* env, jclass current))
1949 {
1950 // current is not a primitive or array class
1951 Klass* c = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(current));
1952 assert(c->is_instance_klass(), "must be");
1953 InstanceKlass* ck = InstanceKlass::cast(c);
1954 InstanceKlass* host = ck->nest_host(THREAD);
1955 return (jclass) (host == nullptr ? nullptr :
1956 JNIHandles::make_local(THREAD, host->java_mirror()));
1957 }
1958 JVM_END
1959
1960 JVM_ENTRY(jobjectArray, JVM_GetNestMembers(JNIEnv* env, jclass current))
1961 {
1962 // current is not a primitive or array class
1963 ResourceMark rm(THREAD);
1964 Klass* c = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(current));
1965 assert(c->is_instance_klass(), "must be");
1966 InstanceKlass* ck = InstanceKlass::cast(c);
1967 InstanceKlass* host = ck->nest_host(THREAD);
1968
1969 log_trace(class, nestmates)("Calling GetNestMembers for type %s with nest-host %s",
1970 ck->external_name(), host->external_name());
1971 {
1972 JvmtiVMObjectAllocEventCollector oam;
1973 Array<u2>* members = host->nest_members();
1974 int length = members == nullptr ? 0 : members->length();
1975
1976 log_trace(class, nestmates)(" - host has %d listed nest members", length);
1977
1978 // nest host is first in the array so make it one bigger
1979 objArrayOop r = oopFactory::new_objArray(vmClasses::Class_klass(),
1980 length + 1, CHECK_NULL);
2020 log_trace(class, nestmates)(" - compacting array from length %d to %d",
2021 length + 1, count + 1);
2022
2023 objArrayOop r2 = oopFactory::new_objArray(vmClasses::Class_klass(),
2024 count + 1, CHECK_NULL);
2025 objArrayHandle result2(THREAD, r2);
2026 for (int i = 0; i < count + 1; i++) {
2027 result2->obj_at_put(i, result->obj_at(i));
2028 }
2029 return (jobjectArray)JNIHandles::make_local(THREAD, result2());
2030 }
2031 }
2032 else {
2033 assert(host == ck || ck->is_hidden(), "must be singleton nest or dynamic nestmate");
2034 }
2035 return (jobjectArray)JNIHandles::make_local(THREAD, result());
2036 }
2037 }
2038 JVM_END
2039
2040 JVM_ENTRY(jobjectArray, JVM_GetPermittedSubclasses(JNIEnv* env, jclass current))
2041 {
2042 oop mirror = JNIHandles::resolve_non_null(current);
2043 assert(!java_lang_Class::is_primitive(mirror), "should not be");
2044 Klass* c = java_lang_Class::as_Klass(mirror);
2045 assert(c->is_instance_klass(), "must be");
2046 InstanceKlass* ik = InstanceKlass::cast(c);
2047 ResourceMark rm(THREAD);
2048 log_trace(class, sealed)("Calling GetPermittedSubclasses for %s type %s",
2049 ik->is_sealed() ? "sealed" : "non-sealed", ik->external_name());
2050 if (ik->is_sealed()) {
2051 JvmtiVMObjectAllocEventCollector oam;
2052 Array<u2>* subclasses = ik->permitted_subclasses();
2053 int length = subclasses->length();
2054
2055 log_trace(class, sealed)(" - sealed class has %d permitted subclasses", length);
2056
2057 objArrayOop r = oopFactory::new_objArray(vmClasses::Class_klass(),
2058 length, CHECK_NULL);
2059 objArrayHandle result(THREAD, r);
2060 int count = 0;
2083 }
2084 if (count < length) {
2085 // we had invalid entries so we need to compact the array
2086 objArrayOop r2 = oopFactory::new_objArray(vmClasses::Class_klass(),
2087 count, CHECK_NULL);
2088 objArrayHandle result2(THREAD, r2);
2089 for (int i = 0; i < count; i++) {
2090 result2->obj_at_put(i, result->obj_at(i));
2091 }
2092 return (jobjectArray)JNIHandles::make_local(THREAD, result2());
2093 }
2094 return (jobjectArray)JNIHandles::make_local(THREAD, result());
2095 } else {
2096 return nullptr;
2097 }
2098 }
2099 JVM_END
2100
2101 // Constant pool access //////////////////////////////////////////////////////////
2102
2103 JVM_ENTRY(jobject, JVM_GetClassConstantPool(JNIEnv *env, jclass cls))
2104 {
2105 JvmtiVMObjectAllocEventCollector oam;
2106 oop mirror = JNIHandles::resolve_non_null(cls);
2107 // Return null for primitives and arrays
2108 if (!java_lang_Class::is_primitive(mirror)) {
2109 Klass* k = java_lang_Class::as_Klass(mirror);
2110 if (k->is_instance_klass()) {
2111 InstanceKlass* k_h = InstanceKlass::cast(k);
2112 Handle jcp = reflect_ConstantPool::create(CHECK_NULL);
2113 reflect_ConstantPool::set_cp(jcp(), k_h->constants());
2114 return JNIHandles::make_local(THREAD, jcp());
2115 }
2116 }
2117 return nullptr;
2118 }
2119 JVM_END
2120
2121
2122 JVM_ENTRY(jint, JVM_ConstantPoolGetSize(JNIEnv *env, jobject obj, jobject unused))
2123 {
2124 constantPoolHandle cp = constantPoolHandle(THREAD, reflect_ConstantPool::get_cp(JNIHandles::resolve_non_null(obj)));
2125 return cp->length();
2126 }
2127 JVM_END
2128
2129
2130 JVM_ENTRY(jclass, JVM_ConstantPoolGetClassAt(JNIEnv *env, jobject obj, jobject unused, jint index))
2131 {
2132 constantPoolHandle cp = constantPoolHandle(THREAD, reflect_ConstantPool::get_cp(JNIHandles::resolve_non_null(obj)));
2133 bounds_check(cp, index, CHECK_NULL);
2134 constantTag tag = cp->tag_at(index);
2135 if (!tag.is_klass() && !tag.is_unresolved_klass()) {
2136 THROW_MSG_NULL(vmSymbols::java_lang_IllegalArgumentException(), "Wrong type at constant pool index");
2137 }
2138 Klass* k = cp->klass_at(index, CHECK_NULL);
2139 return (jclass) JNIHandles::make_local(THREAD, k->java_mirror());
2140 }
2141 JVM_END
2142
2143 JVM_ENTRY(jclass, JVM_ConstantPoolGetClassAtIfLoaded(JNIEnv *env, jobject obj, jobject unused, jint index))
2144 {
2145 constantPoolHandle cp = constantPoolHandle(THREAD, reflect_ConstantPool::get_cp(JNIHandles::resolve_non_null(obj)));
2146 bounds_check(cp, index, CHECK_NULL);
2147 constantTag tag = cp->tag_at(index);
2148 if (!tag.is_klass() && !tag.is_unresolved_klass()) {
2149 THROW_MSG_NULL(vmSymbols::java_lang_IllegalArgumentException(), "Wrong type at constant pool index");
2150 }
2151 Klass* k = ConstantPool::klass_at_if_loaded(cp, index);
2152 if (k == nullptr) return nullptr;
2153 return (jclass) JNIHandles::make_local(THREAD, k->java_mirror());
2154 }
2155 JVM_END
2156
2157 static jobject get_method_at_helper(const constantPoolHandle& cp, jint index, bool force_resolution, TRAPS) {
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;
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);
2202 jobject res = get_method_at_helper(cp, index, false, CHECK_NULL);
2203 return res;
2204 }
2205 JVM_END
2206
2207 static jobject get_field_at_helper(constantPoolHandle cp, jint index, bool force_resolution, TRAPS) {
2208 constantTag tag = cp->tag_at(index);
2209 if (!tag.is_field()) {
2210 THROW_MSG_NULL(vmSymbols::java_lang_IllegalArgumentException(), "Wrong type at constant pool index");
2211 }
2212 int klass_ref = cp->uncached_klass_ref_index_at(index);
2213 Klass* k_o;
2214 if (force_resolution) {
2215 k_o = cp->klass_at(klass_ref, CHECK_NULL);
2216 } else {
2217 k_o = ConstantPool::klass_at_if_loaded(cp, klass_ref);
2218 if (k_o == nullptr) return nullptr;
2219 }
2220 InstanceKlass* k = InstanceKlass::cast(k_o);
2221 Symbol* name = cp->uncached_name_ref_at(index);
2222 Symbol* sig = cp->uncached_signature_ref_at(index);
2223 fieldDescriptor fd;
2224 Klass* target_klass = k->find_field(name, sig, &fd);
2225 if (target_klass == nullptr) {
2226 THROW_MSG_NULL(vmSymbols::java_lang_RuntimeException(), "Unable to look up field in target class");
2227 }
2228 oop field = Reflection::new_field(&fd, CHECK_NULL);
2229 return JNIHandles::make_local(THREAD, field);
2230 }
2231
2232 JVM_ENTRY(jobject, JVM_ConstantPoolGetFieldAt(JNIEnv *env, jobject obj, jobject unusedl, jint index))
2233 {
2234 JvmtiVMObjectAllocEventCollector oam;
2235 constantPoolHandle cp = constantPoolHandle(THREAD, reflect_ConstantPool::get_cp(JNIHandles::resolve_non_null(obj)));
2236 bounds_check(cp, index, CHECK_NULL);
2237 jobject res = get_field_at_helper(cp, index, true, CHECK_NULL);
2238 return res;
2239 }
2240 JVM_END
2241
2242 JVM_ENTRY(jobject, JVM_ConstantPoolGetFieldAtIfLoaded(JNIEnv *env, jobject obj, jobject unused, jint index))
2243 {
2244 JvmtiVMObjectAllocEventCollector oam;
2245 constantPoolHandle cp = constantPoolHandle(THREAD, reflect_ConstantPool::get_cp(JNIHandles::resolve_non_null(obj)));
2246 bounds_check(cp, index, CHECK_NULL);
2247 jobject res = get_field_at_helper(cp, index, false, CHECK_NULL);
2248 return res;
2249 }
2250 JVM_END
2251
2252 JVM_ENTRY(jobjectArray, JVM_ConstantPoolGetMemberRefInfoAt(JNIEnv *env, jobject obj, jobject unused, jint index))
2253 {
2254 JvmtiVMObjectAllocEventCollector oam;
2255 constantPoolHandle cp = constantPoolHandle(THREAD, reflect_ConstantPool::get_cp(JNIHandles::resolve_non_null(obj)));
2256 bounds_check(cp, index, CHECK_NULL);
2257 constantTag tag = cp->tag_at(index);
2258 if (!tag.is_field_or_method()) {
2259 THROW_MSG_NULL(vmSymbols::java_lang_IllegalArgumentException(), "Wrong type at constant pool index");
2260 }
2261 int klass_ref = cp->uncached_klass_ref_index_at(index);
2262 Symbol* klass_name = cp->klass_name_at(klass_ref);
2263 Symbol* member_name = cp->uncached_name_ref_at(index);
2264 Symbol* member_sig = cp->uncached_signature_ref_at(index);
2265 objArrayOop dest_o = oopFactory::new_objArray(vmClasses::String_klass(), 3, CHECK_NULL);
2266 objArrayHandle dest(THREAD, dest_o);
2267 Handle str = java_lang_String::create_from_symbol(klass_name, CHECK_NULL);
2268 dest->obj_at_put(0, str());
2269 str = java_lang_String::create_from_symbol(member_name, CHECK_NULL);
2270 dest->obj_at_put(1, str());
2271 str = java_lang_String::create_from_symbol(member_sig, CHECK_NULL);
2272 dest->obj_at_put(2, str());
2273 return (jobjectArray) JNIHandles::make_local(THREAD, dest());
2274 }
2275 JVM_END
2276
2277 JVM_ENTRY(jint, JVM_ConstantPoolGetClassRefIndexAt(JNIEnv *env, jobject obj, jobject unused, jint index))
2278 {
2279 JvmtiVMObjectAllocEventCollector oam;
2280 constantPoolHandle cp(THREAD, reflect_ConstantPool::get_cp(JNIHandles::resolve_non_null(obj)));
2281 bounds_check(cp, index, CHECK_0);
2282 constantTag tag = cp->tag_at(index);
2283 if (!tag.is_field_or_method()) {
2284 THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(), "Wrong type at constant pool index");
2285 }
2286 return (jint) cp->uncached_klass_ref_index_at(index);
2287 }
2288 JVM_END
2289
2290 JVM_ENTRY(jint, JVM_ConstantPoolGetNameAndTypeRefIndexAt(JNIEnv *env, jobject obj, jobject unused, jint index))
2291 {
2292 JvmtiVMObjectAllocEventCollector oam;
2293 constantPoolHandle cp(THREAD, reflect_ConstantPool::get_cp(JNIHandles::resolve_non_null(obj)));
2294 bounds_check(cp, index, CHECK_0);
2295 constantTag tag = cp->tag_at(index);
2296 if (!tag.is_invoke_dynamic() && !tag.is_field_or_method()) {
2297 THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(), "Wrong type at constant pool index");
2298 }
2299 return (jint) cp->uncached_name_and_type_ref_index_at(index);
2300 }
2301 JVM_END
2302
2303 JVM_ENTRY(jobjectArray, JVM_ConstantPoolGetNameAndTypeRefInfoAt(JNIEnv *env, jobject obj, jobject unused, jint index))
2304 {
2305 JvmtiVMObjectAllocEventCollector oam;
2306 constantPoolHandle cp(THREAD, reflect_ConstantPool::get_cp(JNIHandles::resolve_non_null(obj)));
2307 bounds_check(cp, index, CHECK_NULL);
2308 constantTag tag = cp->tag_at(index);
2309 if (!tag.is_name_and_type()) {
2310 THROW_MSG_NULL(vmSymbols::java_lang_IllegalArgumentException(), "Wrong type at constant pool index");
2311 }
2312 Symbol* member_name = cp->symbol_at(cp->name_ref_index_at(index));
2313 Symbol* member_sig = cp->symbol_at(cp->signature_ref_index_at(index));
2314 objArrayOop dest_o = oopFactory::new_objArray(vmClasses::String_klass(), 2, CHECK_NULL);
2315 objArrayHandle dest(THREAD, dest_o);
2316 Handle str = java_lang_String::create_from_symbol(member_name, CHECK_NULL);
2317 dest->obj_at_put(0, str());
2318 str = java_lang_String::create_from_symbol(member_sig, CHECK_NULL);
2319 dest->obj_at_put(1, str());
2320 return (jobjectArray) JNIHandles::make_local(THREAD, dest());
2321 }
2322 JVM_END
2323
2324 JVM_ENTRY(jint, JVM_ConstantPoolGetIntAt(JNIEnv *env, jobject obj, jobject unused, jint index))
2325 {
2326 constantPoolHandle cp = constantPoolHandle(THREAD, reflect_ConstantPool::get_cp(JNIHandles::resolve_non_null(obj)));
2327 bounds_check(cp, index, CHECK_0);
2328 constantTag tag = cp->tag_at(index);
2329 if (!tag.is_int()) {
2330 THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(), "Wrong type at constant pool index");
2331 }
2332 return cp->int_at(index);
2333 }
2334 JVM_END
2335
2336 JVM_ENTRY(jlong, JVM_ConstantPoolGetLongAt(JNIEnv *env, jobject obj, jobject unused, jint index))
2337 {
2338 constantPoolHandle cp = constantPoolHandle(THREAD, reflect_ConstantPool::get_cp(JNIHandles::resolve_non_null(obj)));
2339 bounds_check(cp, index, CHECK_(0L));
2340 constantTag tag = cp->tag_at(index);
2341 if (!tag.is_long()) {
2342 THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(), "Wrong type at constant pool index");
2343 }
2344 return cp->long_at(index);
2345 }
2346 JVM_END
2347
2348 JVM_ENTRY(jfloat, JVM_ConstantPoolGetFloatAt(JNIEnv *env, jobject obj, jobject unused, jint index))
2349 {
2350 constantPoolHandle cp = constantPoolHandle(THREAD, reflect_ConstantPool::get_cp(JNIHandles::resolve_non_null(obj)));
2351 bounds_check(cp, index, CHECK_(0.0f));
2352 constantTag tag = cp->tag_at(index);
2353 if (!tag.is_float()) {
2354 THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(), "Wrong type at constant pool index");
2355 }
2356 return cp->float_at(index);
2357 }
2358 JVM_END
2359
2360 JVM_ENTRY(jdouble, JVM_ConstantPoolGetDoubleAt(JNIEnv *env, jobject obj, jobject unused, jint index))
2361 {
2362 constantPoolHandle cp = constantPoolHandle(THREAD, reflect_ConstantPool::get_cp(JNIHandles::resolve_non_null(obj)));
2363 bounds_check(cp, index, CHECK_(0.0));
2364 constantTag tag = cp->tag_at(index);
2365 if (!tag.is_double()) {
2366 THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(), "Wrong type at constant pool index");
2367 }
2368 return cp->double_at(index);
2369 }
2370 JVM_END
2371
2372 JVM_ENTRY(jstring, JVM_ConstantPoolGetStringAt(JNIEnv *env, jobject obj, jobject unused, jint index))
2373 {
2374 constantPoolHandle cp = constantPoolHandle(THREAD, reflect_ConstantPool::get_cp(JNIHandles::resolve_non_null(obj)));
2375 bounds_check(cp, index, CHECK_NULL);
2376 constantTag tag = cp->tag_at(index);
2377 if (!tag.is_string()) {
2378 THROW_MSG_NULL(vmSymbols::java_lang_IllegalArgumentException(), "Wrong type at constant pool index");
2379 }
2380 oop str = cp->string_at(index, CHECK_NULL);
2381 return (jstring) JNIHandles::make_local(THREAD, str);
2382 }
2383 JVM_END
2384
2385 JVM_ENTRY(jstring, JVM_ConstantPoolGetUTF8At(JNIEnv *env, jobject obj, jobject unused, jint index))
2386 {
2387 JvmtiVMObjectAllocEventCollector oam;
2388 constantPoolHandle cp = constantPoolHandle(THREAD, reflect_ConstantPool::get_cp(JNIHandles::resolve_non_null(obj)));
2389 bounds_check(cp, index, CHECK_NULL);
2390 constantTag tag = cp->tag_at(index);
2391 if (!tag.is_symbol()) {
2392 THROW_MSG_NULL(vmSymbols::java_lang_IllegalArgumentException(), "Wrong type at constant pool index");
2393 }
2394 Symbol* sym = cp->symbol_at(index);
2395 Handle str = java_lang_String::create_from_symbol(sym, CHECK_NULL);
2396 return (jstring) JNIHandles::make_local(THREAD, str());
2397 }
2398 JVM_END
2399
2400 JVM_ENTRY(jbyte, JVM_ConstantPoolGetTagAt(JNIEnv *env, jobject obj, jobject unused, jint index))
2401 {
2402 constantPoolHandle cp = constantPoolHandle(THREAD, reflect_ConstantPool::get_cp(JNIHandles::resolve_non_null(obj)));
2403 bounds_check(cp, index, CHECK_0);
2404 constantTag tag = cp->tag_at(index);
2405 jbyte result = tag.value();
2406 // If returned tag values are not from the JVM spec, e.g. tags from 100 to 105,
2407 // they are changed to the corresponding tags from the JVM spec, so that java code in
2408 // sun.reflect.ConstantPool will return only tags from the JVM spec, not internal ones.
2409 if (tag.is_klass_or_reference()) {
2410 result = JVM_CONSTANT_Class;
2411 } else if (tag.is_string_index()) {
2412 result = JVM_CONSTANT_String;
2413 } else if (tag.is_method_type_in_error()) {
2414 result = JVM_CONSTANT_MethodType;
2415 } else if (tag.is_method_handle_in_error()) {
2416 result = JVM_CONSTANT_MethodHandle;
2417 } else if (tag.is_dynamic_constant_in_error()) {
2418 result = JVM_CONSTANT_Dynamic;
2419 }
2420 return result;
2421 }
2422 JVM_END
2423
2424 // Assertion support. //////////////////////////////////////////////////////////
2425
2426 JVM_ENTRY(jboolean, JVM_DesiredAssertionStatus(JNIEnv *env, jclass unused, jclass cls))
2427 assert(cls != nullptr, "bad class");
2428
2429 oop r = JNIHandles::resolve(cls);
2430 assert(! java_lang_Class::is_primitive(r), "primitive classes not allowed");
2431 if (java_lang_Class::is_primitive(r)) return false;
2432
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))
2473 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls));
2474 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
2475 // types will have length zero if this is not an InstanceKlass
2476 // (length is determined by call to JVM_GetClassCPEntriesCount)
2477 if (k->is_instance_klass()) {
2478 ConstantPool* cp = InstanceKlass::cast(k)->constants();
2479 for (int index = cp->length() - 1; index >= 0; index--) {
2480 constantTag tag = cp->tag_at(index);
2481 types[index] = (tag.is_unresolved_klass()) ? (unsigned char) JVM_CONSTANT_Class : tag.value();
2482 }
2483 }
2484 JVM_END
2485
2486
2487 JVM_ENTRY(jint, JVM_GetClassCPEntriesCount(JNIEnv *env, jclass cls))
2488 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls));
2489 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
2490 return (!k->is_instance_klass()) ? 0 : InstanceKlass::cast(k)->constants()->length();
2491 JVM_END
2492
2493
2494 JVM_ENTRY(jint, JVM_GetClassFieldsCount(JNIEnv *env, jclass cls))
2495 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls));
2496 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
2497 return (!k->is_instance_klass()) ? 0 : InstanceKlass::cast(k)->java_fields_count();
2498 JVM_END
2499
2500
2501 JVM_ENTRY(jint, JVM_GetClassMethodsCount(JNIEnv *env, jclass cls))
2502 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls));
2503 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
2504 return (!k->is_instance_klass()) ? 0 : InstanceKlass::cast(k)->methods()->length();
2505 JVM_END
2506
2507
2508 // The following methods, used for the verifier, are never called with
2509 // array klasses, so a direct cast to InstanceKlass is safe.
2510 // Typically, these methods are called in a loop with bounds determined
2511 // by the results of JVM_GetClass{Fields,Methods}Count, which return
2512 // zero for arrays.
2513 JVM_ENTRY(void, JVM_GetMethodIxExceptionIndexes(JNIEnv *env, jclass cls, jint method_index, unsigned short *exceptions))
2514 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls));
2515 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
2516 Method* method = InstanceKlass::cast(k)->methods()->at(method_index);
2517 int length = method->checked_exceptions_length();
2518 if (length > 0) {
2519 CheckedExceptionElement* table= method->checked_exceptions_start();
2520 for (int i = 0; i < length; i++) {
2521 exceptions[i] = table[i].class_cp_index;
2522 }
2523 }
2524 JVM_END
2525
2526
2527 JVM_ENTRY(jint, JVM_GetMethodIxExceptionsCount(JNIEnv *env, jclass cls, jint method_index))
2528 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls));
2529 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
2530 Method* method = InstanceKlass::cast(k)->methods()->at(method_index);
2531 return method->checked_exceptions_length();
2532 JVM_END
2533
2534
2535 JVM_ENTRY(void, JVM_GetMethodIxByteCode(JNIEnv *env, jclass cls, jint method_index, unsigned char *code))
2536 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls));
2537 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
2538 Method* method = InstanceKlass::cast(k)->methods()->at(method_index);
2539 memcpy(code, method->code_base(), method->code_size());
2540 JVM_END
2541
2542
2543 JVM_ENTRY(jint, JVM_GetMethodIxByteCodeLength(JNIEnv *env, jclass cls, jint method_index))
2544 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls));
2545 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
2546 Method* method = InstanceKlass::cast(k)->methods()->at(method_index);
2547 return method->code_size();
2548 JVM_END
2549
2550
2551 JVM_ENTRY(void, JVM_GetMethodIxExceptionTableEntry(JNIEnv *env, jclass cls, jint method_index, jint entry_index, JVM_ExceptionTableEntryType *entry))
2552 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls));
2553 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
2554 Method* method = InstanceKlass::cast(k)->methods()->at(method_index);
2555 ExceptionTable extable(method);
2556 entry->start_pc = extable.start_pc(entry_index);
2557 entry->end_pc = extable.end_pc(entry_index);
2558 entry->handler_pc = extable.handler_pc(entry_index);
2559 entry->catchType = extable.catch_type_index(entry_index);
2560 JVM_END
2561
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))
2636 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls));
2637 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
2638 Method* method = InstanceKlass::cast(k)->methods()->at(method_index);
2639 return method->signature()->as_utf8();
2640 JVM_END
2641
2642 /**
2643 * All of these JVM_GetCP-xxx methods are used by the old verifier to
2644 * read entries in the constant pool. Since the old verifier always
2645 * works on a copy of the code, it will not see any rewriting that
2646 * may possibly occur in the middle of verification. So it is important
2647 * that nothing it calls tries to use the cpCache instead of the raw
2648 * constant pool, so we must use cp->uncached_x methods when appropriate.
2649 */
2650 JVM_ENTRY(const char*, JVM_GetCPFieldNameUTF(JNIEnv *env, jclass cls, jint cp_index))
2651 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls));
2652 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
2653 ConstantPool* cp = InstanceKlass::cast(k)->constants();
2654 switch (cp->tag_at(cp_index).value()) {
2655 case JVM_CONSTANT_Fieldref:
2656 return cp->uncached_name_ref_at(cp_index)->as_utf8();
2657 default:
2658 fatal("JVM_GetCPFieldNameUTF: illegal constant");
2659 }
2660 ShouldNotReachHere();
2661 return nullptr;
2662 JVM_END
2663
2664
2665 JVM_ENTRY(const char*, JVM_GetCPMethodNameUTF(JNIEnv *env, jclass cls, jint cp_index))
2666 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls));
2667 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
2668 ConstantPool* cp = InstanceKlass::cast(k)->constants();
2669 switch (cp->tag_at(cp_index).value()) {
2670 case JVM_CONSTANT_InterfaceMethodref:
2671 case JVM_CONSTANT_Methodref:
2672 return cp->uncached_name_ref_at(cp_index)->as_utf8();
2673 default:
2674 fatal("JVM_GetCPMethodNameUTF: illegal constant");
2675 }
2676 ShouldNotReachHere();
2677 return nullptr;
2678 JVM_END
2679
2680
2681 JVM_ENTRY(const char*, JVM_GetCPMethodSignatureUTF(JNIEnv *env, jclass cls, jint cp_index))
2682 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls));
2683 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
2684 ConstantPool* cp = InstanceKlass::cast(k)->constants();
2685 switch (cp->tag_at(cp_index).value()) {
2686 case JVM_CONSTANT_InterfaceMethodref:
2687 case JVM_CONSTANT_Methodref:
2688 return cp->uncached_signature_ref_at(cp_index)->as_utf8();
2689 default:
2690 fatal("JVM_GetCPMethodSignatureUTF: illegal constant");
2691 }
2692 ShouldNotReachHere();
2693 return nullptr;
2694 JVM_END
2695
2696
2697 JVM_ENTRY(const char*, JVM_GetCPFieldSignatureUTF(JNIEnv *env, jclass cls, jint cp_index))
2698 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls));
2699 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
2700 ConstantPool* cp = InstanceKlass::cast(k)->constants();
2701 switch (cp->tag_at(cp_index).value()) {
2702 case JVM_CONSTANT_Fieldref:
2703 return cp->uncached_signature_ref_at(cp_index)->as_utf8();
2704 default:
2705 fatal("JVM_GetCPFieldSignatureUTF: illegal constant");
2706 }
2707 ShouldNotReachHere();
2708 return nullptr;
2709 JVM_END
2710
2711
2712 JVM_ENTRY(const char*, JVM_GetCPClassNameUTF(JNIEnv *env, jclass cls, jint cp_index))
2713 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls));
2714 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
2715 ConstantPool* cp = InstanceKlass::cast(k)->constants();
2716 Symbol* classname = cp->klass_name_at(cp_index);
2717 return classname->as_utf8();
2718 JVM_END
2719
2720
2721 JVM_ENTRY(const char*, JVM_GetCPFieldClassNameUTF(JNIEnv *env, jclass cls, jint cp_index))
2722 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls));
2723 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
2724 ConstantPool* cp = InstanceKlass::cast(k)->constants();
2725 switch (cp->tag_at(cp_index).value()) {
2726 case JVM_CONSTANT_Fieldref: {
2727 int class_index = cp->uncached_klass_ref_index_at(cp_index);
2728 Symbol* classname = cp->klass_name_at(class_index);
2729 return classname->as_utf8();
2730 }
2731 default:
2732 fatal("JVM_GetCPFieldClassNameUTF: illegal constant");
2733 }
2734 ShouldNotReachHere();
2735 return nullptr;
2736 JVM_END
2737
2738
2739 JVM_ENTRY(const char*, JVM_GetCPMethodClassNameUTF(JNIEnv *env, jclass cls, jint cp_index))
2740 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls));
2741 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
2742 ConstantPool* cp = InstanceKlass::cast(k)->constants();
2743 switch (cp->tag_at(cp_index).value()) {
2744 case JVM_CONSTANT_Methodref:
2745 case JVM_CONSTANT_InterfaceMethodref: {
2746 int class_index = cp->uncached_klass_ref_index_at(cp_index);
2747 Symbol* classname = cp->klass_name_at(class_index);
2748 return classname->as_utf8();
2749 }
2750 default:
2751 fatal("JVM_GetCPMethodClassNameUTF: illegal constant");
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:
2793 case JVM_CONSTANT_InterfaceMethodref: {
2794 Symbol* name = cp->uncached_name_ref_at(cp_index);
2795 Symbol* signature = cp->uncached_signature_ref_at(cp_index);
2796 Array<Method*>* methods = InstanceKlass::cast(k_called)->methods();
2797 int methods_count = methods->length();
2798 for (int i = 0; i < methods_count; i++) {
2799 Method* method = methods->at(i);
2800 if (method->name() == name && method->signature() == signature) {
2801 return method->access_flags().as_int() & JVM_RECOGNIZED_METHOD_MODIFIERS;
2802 }
2803 }
2804 return -1;
2805 }
2806 default:
2807 fatal("JVM_GetCPMethodModifiers: illegal constant");
2808 }
2809 ShouldNotReachHere();
2810 return 0;
2811 JVM_END
2812
2813
2814 // Misc //////////////////////////////////////////////////////////////////////////////////////////////
2815
2816 JVM_LEAF(void, JVM_ReleaseUTF(const char *utf))
2817 // So long as UTF8::convert_to_utf8 returns resource strings, we don't have to do anything
2818 JVM_END
2819
2820
2821 JVM_ENTRY(jboolean, JVM_IsSameClassPackage(JNIEnv *env, jclass class1, jclass class2))
2822 oop class1_mirror = JNIHandles::resolve_non_null(class1);
2823 oop class2_mirror = JNIHandles::resolve_non_null(class2);
2824 Klass* klass1 = java_lang_Class::as_Klass(class1_mirror);
2825 Klass* klass2 = java_lang_Class::as_Klass(class2_mirror);
2826 return (jboolean) Reflection::is_same_class_package(klass1, klass2);
2827 JVM_END
2828
2829 // Printing support //////////////////////////////////////////////////
2830 extern "C" {
2831
2832 ATTRIBUTE_PRINTF(3, 0)
2833 int jio_vsnprintf(char *str, size_t count, const char *fmt, va_list args) {
2834 // Reject count values that are negative signed values converted to
2835 // unsigned; see bug 4399518, 4417214
2836 if ((intptr_t)count <= 0) return -1;
2837
2838 int result = os::vsnprintf(str, count, fmt, args);
2839 if (result > 0 && (size_t)result >= count) {
2840 result = -1;
2841 }
2900 // In most of the JVM thread support functions we need to access the
2901 // thread through a ThreadsListHandle to prevent it from exiting and
2902 // being reclaimed while we try to operate on it. The exceptions to this
2903 // rule are when operating on the current thread, or if the monitor of
2904 // the target java.lang.Thread is locked at the Java level - in both
2905 // cases the target cannot exit.
2906
2907 static void thread_entry(JavaThread* thread, TRAPS) {
2908 HandleMark hm(THREAD);
2909 Handle obj(THREAD, thread->threadObj());
2910 JavaValue result(T_VOID);
2911 JavaCalls::call_virtual(&result,
2912 obj,
2913 vmClasses::Thread_klass(),
2914 vmSymbols::run_method_name(),
2915 vmSymbols::void_method_signature(),
2916 THREAD);
2917 }
2918
2919
2920 JVM_ENTRY(void, JVM_StartThread(JNIEnv* env, jobject jthread))
2921 #if INCLUDE_CDS
2922 if (CDSConfig::is_dumping_static_archive()) {
2923 // During java -Xshare:dump, if we allow multiple Java threads to
2924 // execute in parallel, symbols and classes may be loaded in
2925 // random orders which will make the resulting CDS archive
2926 // non-deterministic.
2927 //
2928 // Lucikly, during java -Xshare:dump, it's important to run only
2929 // the code in the main Java thread (which is NOT started here) that
2930 // creates the module graph, etc. It's safe to not start the other
2931 // threads which are launched by class static initializers
2932 // (ReferenceHandler, FinalizerThread and CleanerImpl).
2933 if (log_is_enabled(Info, cds)) {
2934 ResourceMark rm;
2935 oop t = JNIHandles::resolve_non_null(jthread);
2936 log_info(cds)("JVM_StartThread() ignored: %s", t->klass()->external_name());
2937 }
2938 return;
2939 }
2940 #endif
2941 JavaThread *native_thread = nullptr;
2942
2996 log_warning(os, thread)("Failed to start the native thread for java.lang.Thread \"%s\"",
2997 JavaThread::name_for(JNIHandles::resolve_non_null(jthread)));
2998 // No one should hold a reference to the 'native_thread'.
2999 native_thread->smr_delete();
3000 if (JvmtiExport::should_post_resource_exhausted()) {
3001 JvmtiExport::post_resource_exhausted(
3002 JVMTI_RESOURCE_EXHAUSTED_OOM_ERROR | JVMTI_RESOURCE_EXHAUSTED_THREADS,
3003 os::native_thread_creation_failed_msg());
3004 }
3005 THROW_MSG(vmSymbols::java_lang_OutOfMemoryError(),
3006 os::native_thread_creation_failed_msg());
3007 }
3008
3009 JFR_ONLY(Jfr::on_java_thread_start(thread, native_thread);)
3010
3011 Thread::start(native_thread);
3012
3013 JVM_END
3014
3015
3016 JVM_ENTRY(void, JVM_SetThreadPriority(JNIEnv* env, jobject jthread, jint prio))
3017 ThreadsListHandle tlh(thread);
3018 oop java_thread = nullptr;
3019 JavaThread* receiver = nullptr;
3020 bool is_alive = tlh.cv_internal_thread_to_JavaThread(jthread, &receiver, &java_thread);
3021 java_lang_Thread::set_priority(java_thread, (ThreadPriority)prio);
3022
3023 if (is_alive) {
3024 // jthread refers to a live JavaThread.
3025 Thread::set_priority(receiver, (ThreadPriority)prio);
3026 }
3027 // Implied else: If the JavaThread hasn't started yet, then the
3028 // priority set in the java.lang.Thread object above will be pushed
3029 // down when it does start.
3030 JVM_END
3031
3032
3033 JVM_LEAF(void, JVM_Yield(JNIEnv *env, jclass threadClass))
3034 if (os::dont_yield()) return;
3035 HOTSPOT_THREAD_YIELD();
3036 os::naked_yield();
3037 JVM_END
3038
3039 JVM_ENTRY(void, JVM_SleepNanos(JNIEnv* env, jclass threadClass, jlong nanos))
3040 if (nanos < 0) {
3041 THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), "nanosecond timeout value out of range");
3042 }
3043
3044 if (thread->is_interrupted(true) && !HAS_PENDING_EXCEPTION) {
3045 THROW_MSG(vmSymbols::java_lang_InterruptedException(), "sleep interrupted");
3046 }
3047
3048 // Save current thread state and restore it at the end of this block.
3049 // And set new thread state to SLEEPING.
3050 JavaThreadSleepState jtss(thread);
3051
3052 HOTSPOT_THREAD_SLEEP_BEGIN(nanos / NANOSECS_PER_MILLISEC);
3053
3054 if (nanos == 0) {
3055 os::naked_yield();
3056 } else {
3057 ThreadState old_state = thread->osthread()->get_state();
3058 thread->osthread()->set_state(SLEEPING);
3059 if (!thread->sleep_nanos(nanos)) { // interrupted
3060 // An asynchronous exception could have been thrown on
3061 // us while we were sleeping. We do not overwrite those.
3062 if (!HAS_PENDING_EXCEPTION) {
3063 HOTSPOT_THREAD_SLEEP_END(1);
3064
3065 // TODO-FIXME: THROW_MSG returns which means we will not call set_state()
3066 // to properly restore the thread state. That's likely wrong.
3067 THROW_MSG(vmSymbols::java_lang_InterruptedException(), "sleep interrupted");
3068 }
3069 }
3070 thread->osthread()->set_state(old_state);
3071 }
3072 HOTSPOT_THREAD_SLEEP_END(0);
3073 JVM_END
3074
3075 JVM_ENTRY(jobject, JVM_CurrentCarrierThread(JNIEnv* env, jclass threadClass))
3076 oop jthread = thread->threadObj();
3077 assert(jthread != nullptr, "no current carrier thread!");
3078 return JNIHandles::make_local(THREAD, jthread);
3079 JVM_END
3080
3081 JVM_ENTRY(jobject, JVM_CurrentThread(JNIEnv* env, jclass threadClass))
3082 oop theThread = thread->vthread();
3083 assert(theThread != (oop)nullptr, "no current thread!");
3084 return JNIHandles::make_local(THREAD, theThread);
3085 JVM_END
3086
3087 JVM_ENTRY(void, JVM_SetCurrentThread(JNIEnv* env, jobject thisThread,
3088 jobject theThread))
3089 oop threadObj = JNIHandles::resolve(theThread);
3090 thread->set_vthread(threadObj);
3091 JFR_ONLY(Jfr::on_set_current_thread(thread, threadObj);)
3092 JVM_END
3093
3094 JVM_ENTRY(jlong, JVM_GetNextThreadIdOffset(JNIEnv* env, jclass threadClass))
3095 return ThreadIdentifier::unsafe_offset();
3096 JVM_END
3097
3098 JVM_ENTRY(void, JVM_Interrupt(JNIEnv* env, jobject jthread))
3099 ThreadsListHandle tlh(thread);
3100 JavaThread* receiver = nullptr;
3101 bool is_alive = tlh.cv_internal_thread_to_JavaThread(jthread, &receiver, nullptr);
3102 if (is_alive) {
3103 // jthread refers to a live JavaThread.
3104 receiver->interrupt();
3105 }
3106 JVM_END
3107
3108 // Return true iff the current thread has locked the object passed in
3109
3110 JVM_ENTRY(jboolean, JVM_HoldsLock(JNIEnv* env, jclass threadClass, jobject obj))
3111 if (obj == nullptr) {
3112 THROW_(vmSymbols::java_lang_NullPointerException(), JNI_FALSE);
3113 }
3114 Handle h_obj(THREAD, JNIHandles::resolve(obj));
3115 return ObjectSynchronizer::current_thread_holds_lock(thread, h_obj);
3116 JVM_END
3117
3118 JVM_ENTRY(jobject, JVM_GetStackTrace(JNIEnv *env, jobject jthread))
3119 oop trace = java_lang_Thread::async_get_stack_trace(JNIHandles::resolve(jthread), THREAD);
3120 return JNIHandles::make_local(THREAD, trace);
3121 JVM_END
3122
3123 JVM_ENTRY(void, JVM_SetNativeThreadName(JNIEnv* env, jobject jthread, jstring name))
3124 // We don't use a ThreadsListHandle here because the current thread
3125 // must be alive.
3126 oop java_thread = JNIHandles::resolve_non_null(jthread);
3127 JavaThread* thr = java_lang_Thread::thread(java_thread);
3128 if (thread == thr && !thr->has_attached_via_jni()) {
3129 // Thread naming is only supported for the current thread and
3130 // we don't set the name of an attached thread to avoid stepping
3131 // on other programs.
3132 ResourceMark rm(thread);
3133 const char *thread_name = java_lang_String::as_utf8_string(JNIHandles::resolve_non_null(name));
3134 os::set_native_thread_name(thread_name);
3135 }
3136 JVM_END
3137
3138 JVM_ENTRY(jobject, JVM_ScopedValueCache(JNIEnv* env, jclass threadClass))
3139 oop theCache = thread->scopedValueCache();
3140 return JNIHandles::make_local(THREAD, theCache);
3141 JVM_END
3142
3143 JVM_ENTRY(void, JVM_SetScopedValueCache(JNIEnv* env, jclass threadClass,
3144 jobject theCache))
3145 arrayOop objs = arrayOop(JNIHandles::resolve(theCache));
3146 thread->set_scopedValueCache(objs);
3147 JVM_END
3148
3149 // java.lang.SecurityManager ///////////////////////////////////////////////////////////////////////
3150
3151 JVM_ENTRY(jobjectArray, JVM_GetClassContext(JNIEnv *env))
3152 ResourceMark rm(THREAD);
3153 JvmtiVMObjectAllocEventCollector oam;
3154 vframeStream vfst(thread);
3155
3156 if (vmClasses::reflect_CallerSensitive_klass() != nullptr) {
3157 // This must only be called from SecurityManager.getClassContext
3158 Method* m = vfst.method();
3159 if (!(m->method_holder() == vmClasses::SecurityManager_klass() &&
3160 m->name() == vmSymbols::getClassContext_name() &&
3161 m->signature() == vmSymbols::void_class_array_signature())) {
3162 THROW_MSG_NULL(vmSymbols::java_lang_InternalError(), "JVM_GetClassContext must only be called from SecurityManager.getClassContext");
3163 }
3164 }
3165
3166 // Collect method holders
3167 GrowableArray<Klass*>* klass_array = new GrowableArray<Klass*>();
3168 for (; !vfst.at_end(); vfst.security_next()) {
3169 Method* m = vfst.method();
3170 // Native frames are not returned
3171 if (!m->is_ignored_by_security_stack_walk() && !m->is_native()) {
3172 Klass* holder = m->method_holder();
3173 assert(holder->is_klass(), "just checking");
3174 klass_array->append(holder);
3175 }
3176 }
3177
3178 // Create result array of type [Ljava/lang/Class;
3179 objArrayOop result = oopFactory::new_objArray(vmClasses::Class_klass(), klass_array->length(), CHECK_NULL);
3180 // Fill in mirrors corresponding to method holders
3181 for (int i = 0; i < klass_array->length(); i++) {
3182 result->obj_at_put(i, klass_array->at(i)->java_mirror());
3183 }
3184
3185 return (jobjectArray) JNIHandles::make_local(THREAD, result);
3186 JVM_END
3187
3188
3189 // java.lang.Package ////////////////////////////////////////////////////////////////
3190
3191
3192 JVM_ENTRY(jstring, JVM_GetSystemPackage(JNIEnv *env, jstring name))
3193 ResourceMark rm(THREAD);
3194 JvmtiVMObjectAllocEventCollector oam;
3195 char* str = java_lang_String::as_utf8_string(JNIHandles::resolve_non_null(name));
3196 oop result = ClassLoader::get_system_package(str, CHECK_NULL);
3197 return (jstring) JNIHandles::make_local(THREAD, result);
3198 JVM_END
3199
3200
3201 JVM_ENTRY(jobjectArray, JVM_GetSystemPackages(JNIEnv *env))
3202 JvmtiVMObjectAllocEventCollector oam;
3203 objArrayOop result = ClassLoader::get_system_packages(CHECK_NULL);
3204 return (jobjectArray) JNIHandles::make_local(THREAD, result);
3205 JVM_END
3206
3207
3208 // java.lang.ref.Reference ///////////////////////////////////////////////////////////////
3209
3210
3211 JVM_ENTRY(jobject, JVM_GetAndClearReferencePendingList(JNIEnv* env))
3212 MonitorLocker ml(Heap_lock);
3213 oop ref = Universe::reference_pending_list();
3214 if (ref != nullptr) {
3215 Universe::clear_reference_pending_list();
3216 }
3217 return JNIHandles::make_local(THREAD, ref);
3218 JVM_END
3219
3220 JVM_ENTRY(jboolean, JVM_HasReferencePendingList(JNIEnv* env))
3221 MonitorLocker ml(Heap_lock);
3222 return Universe::has_reference_pending_list();
3223 JVM_END
3224
3225 JVM_ENTRY(void, JVM_WaitForReferencePendingList(JNIEnv* env))
3226 MonitorLocker ml(Heap_lock);
3227 while (!Universe::has_reference_pending_list()) {
3228 ml.wait();
3229 }
3230 JVM_END
3231
3232 JVM_ENTRY(jboolean, JVM_ReferenceRefersTo(JNIEnv* env, jobject ref, jobject o))
3233 oop ref_oop = JNIHandles::resolve_non_null(ref);
3234 // PhantomReference has it's own implementation of refersTo().
3235 // See: JVM_PhantomReferenceRefersTo
3236 assert(!java_lang_ref_Reference::is_phantom(ref_oop), "precondition");
3237 oop referent = java_lang_ref_Reference::weak_referent_no_keepalive(ref_oop);
3238 return referent == JNIHandles::resolve(o);
3239 JVM_END
3240
3241 JVM_ENTRY(void, JVM_ReferenceClear(JNIEnv* env, jobject ref))
3242 oop ref_oop = JNIHandles::resolve_non_null(ref);
3243 // FinalReference has it's own implementation of clear().
3244 assert(!java_lang_ref_Reference::is_final(ref_oop), "precondition");
3245 if (java_lang_ref_Reference::unknown_referent_no_keepalive(ref_oop) == nullptr) {
3246 // If the referent has already been cleared then done.
3247 // However, if the referent is dead but has not yet been cleared by
3248 // concurrent reference processing, it should NOT be cleared here.
3249 // Instead, clearing should be left to the GC. Clearing it here could
3250 // detectably lose an expected notification, which is impossible with
3251 // STW reference processing. The clearing in enqueue() doesn't have
3252 // this problem, since the enqueue covers the notification, but it's not
3253 // worth the effort to handle that case specially.
3254 return;
3255 }
3256 java_lang_ref_Reference::clear_referent(ref_oop);
3257 JVM_END
3258
3259
3260 // java.lang.ref.PhantomReference //////////////////////////////////////////////////
3261
3262
3263 JVM_ENTRY(jboolean, JVM_PhantomReferenceRefersTo(JNIEnv* env, jobject ref, jobject o))
3264 oop ref_oop = JNIHandles::resolve_non_null(ref);
3265 oop referent = java_lang_ref_Reference::phantom_referent_no_keepalive(ref_oop);
3266 return referent == JNIHandles::resolve(o);
3267 JVM_END
3268
3269
3270 // ObjectInputStream ///////////////////////////////////////////////////////////////
3271
3272 // Return the first user-defined class loader up the execution stack, or null
3273 // if only code from the bootstrap or platform class loader is on the stack.
3274
3275 JVM_ENTRY(jobject, JVM_LatestUserDefinedLoader(JNIEnv *env))
3276 for (vframeStream vfst(thread); !vfst.at_end(); vfst.next()) {
3277 InstanceKlass* ik = vfst.method()->method_holder();
3278 oop loader = ik->class_loader();
3279 if (loader != nullptr && !SystemDictionary::is_platform_class_loader(loader)) {
3280 // Skip reflection related frames
3281 if (!ik->is_subclass_of(vmClasses::reflect_SerializationConstructorAccessorImpl_klass())) {
3282 return JNIHandles::make_local(THREAD, loader);
3283 }
3284 }
3285 }
3286 return nullptr;
3287 JVM_END
3288
3289
3290 // Array ///////////////////////////////////////////////////////////////////////////////////////////
3291
3292
3293 // resolve array handle and check arguments
3294 static inline arrayOop check_array(JNIEnv *env, jobject arr, bool type_array_only, TRAPS) {
3295 if (arr == nullptr) {
3296 THROW_NULL(vmSymbols::java_lang_NullPointerException());
3297 }
3298 oop a = JNIHandles::resolve_non_null(arr);
3299 if (!a->is_array()) {
3300 THROW_MSG_NULL(vmSymbols::java_lang_IllegalArgumentException(), "Argument is not an array");
3301 } else if (type_array_only && !a->is_typeArray()) {
3302 THROW_MSG_NULL(vmSymbols::java_lang_IllegalArgumentException(), "Argument is not an array of primitive type");
3303 }
3304 return arrayOop(a);
3305 }
3306
3307
3308 JVM_ENTRY(jint, JVM_GetArrayLength(JNIEnv *env, jobject arr))
3309 arrayOop a = check_array(env, arr, false, CHECK_0);
3310 return a->length();
3311 JVM_END
3312
3313
3314 JVM_ENTRY(jobject, JVM_GetArrayElement(JNIEnv *env, jobject arr, jint index))
3315 JvmtiVMObjectAllocEventCollector oam;
3316 arrayOop a = check_array(env, arr, false, CHECK_NULL);
3317 jvalue value;
3318 BasicType type = Reflection::array_get(&value, a, index, CHECK_NULL);
3319 oop box = Reflection::box(&value, type, CHECK_NULL);
3320 return JNIHandles::make_local(THREAD, box);
3321 JVM_END
3322
3323
3324 JVM_ENTRY(jvalue, JVM_GetPrimitiveArrayElement(JNIEnv *env, jobject arr, jint index, jint wCode))
3325 jvalue value;
3326 value.i = 0; // to initialize value before getting used in CHECK
3327 arrayOop a = check_array(env, arr, true, CHECK_(value));
3328 assert(a->is_typeArray(), "just checking");
3329 BasicType type = Reflection::array_get(&value, a, index, CHECK_(value));
3330 BasicType wide_type = (BasicType) wCode;
3331 if (type != wide_type) {
3332 Reflection::widen(&value, type, wide_type, CHECK_(value));
3333 }
3334 return value;
3335 JVM_END
3336
3337
3338 JVM_ENTRY(void, JVM_SetArrayElement(JNIEnv *env, jobject arr, jint index, jobject val))
3339 arrayOop a = check_array(env, arr, false, CHECK);
3340 oop box = JNIHandles::resolve(val);
3341 jvalue value;
3342 value.i = 0; // to initialize value before getting used in CHECK
3343 BasicType value_type;
3344 if (a->is_objArray()) {
3345 // Make sure we do no unbox e.g. java/lang/Integer instances when storing into an object array
3346 value_type = Reflection::unbox_for_regular_object(box, &value);
3347 } else {
3348 value_type = Reflection::unbox_for_primitive(box, &value, CHECK);
3349 }
3350 Reflection::array_set(&value, a, index, value_type, CHECK);
3351 JVM_END
3352
3353
3354 JVM_ENTRY(void, JVM_SetPrimitiveArrayElement(JNIEnv *env, jobject arr, jint index, jvalue v, unsigned char vCode))
3355 arrayOop a = check_array(env, arr, true, CHECK);
3356 assert(a->is_typeArray(), "just checking");
3357 BasicType value_type = (BasicType) vCode;
3358 Reflection::array_set(&v, a, index, value_type, CHECK);
3359 JVM_END
3360
3361
3362 JVM_ENTRY(jobject, JVM_NewArray(JNIEnv *env, jclass eltClass, jint length))
3363 JvmtiVMObjectAllocEventCollector oam;
3364 oop element_mirror = JNIHandles::resolve(eltClass);
3365 oop result = Reflection::reflect_new_array(element_mirror, length, CHECK_NULL);
3366 return JNIHandles::make_local(THREAD, result);
3367 JVM_END
3368
3369
3370 JVM_ENTRY(jobject, JVM_NewMultiArray(JNIEnv *env, jclass eltClass, jintArray dim))
3371 JvmtiVMObjectAllocEventCollector oam;
3372 arrayOop dim_array = check_array(env, dim, true, CHECK_NULL);
3373 oop element_mirror = JNIHandles::resolve(eltClass);
3374 assert(dim_array->is_typeArray(), "just checking");
3375 oop result = Reflection::reflect_new_multi_array(element_mirror, typeArrayOop(dim_array), CHECK_NULL);
3376 return JNIHandles::make_local(THREAD, result);
3377 JVM_END
3378
3379
3380 // Library support ///////////////////////////////////////////////////////////////////////////
3381
3382 JVM_LEAF(void*, JVM_LoadZipLibrary())
3383 return ZipLibrary::handle();
3384 JVM_END
3385
3386 JVM_ENTRY_NO_ENV(void*, JVM_LoadLibrary(const char* name, jboolean throwException))
3387 //%note jvm_ct
3388 char ebuf[1024];
3389 void *load_result;
3390 {
3391 ThreadToNativeFromVM ttnfvm(thread);
3392 load_result = os::dll_load(name, ebuf, sizeof ebuf);
3393 }
3394 if (load_result == nullptr) {
3395 if (throwException) {
3396 char msg[1024];
3397 jio_snprintf(msg, sizeof msg, "%s: %s", name, ebuf);
3398 // Since 'ebuf' may contain a string encoded using
3399 // platform encoding scheme, we need to pass
3400 // Exceptions::unsafe_to_utf8 to the new_exception method
3401 // as the last argument. See bug 6367357.
3402 Handle h_exception =
3403 Exceptions::new_exception(thread,
3404 vmSymbols::java_lang_UnsatisfiedLinkError(),
3405 msg, Exceptions::unsafe_to_utf8);
3406
3407 THROW_HANDLE_NULL(h_exception);
3408 } else {
3409 log_info(library)("Failed to load library %s", name);
3410 return load_result;
3411 }
3412 }
3413 log_info(library)("Loaded library %s, handle " INTPTR_FORMAT, name, p2i(load_result));
3414 return load_result;
3415 JVM_END
3416
3417
3418 JVM_LEAF(void, JVM_UnloadLibrary(void* handle))
3419 os::dll_unload(handle);
3420 log_info(library)("Unloaded library with handle " INTPTR_FORMAT, p2i(handle));
3421 JVM_END
3422
3423
3424 JVM_LEAF(void*, JVM_FindLibraryEntry(void* handle, const char* name))
3425 void* find_result = os::dll_lookup(handle, name);
3426 log_info(library)("%s %s in library with handle " INTPTR_FORMAT,
3427 find_result != nullptr ? "Found" : "Failed to find",
3428 name, p2i(handle));
3429 return find_result;
3430 JVM_END
3431
3432
3433 // JNI version ///////////////////////////////////////////////////////////////////////////////
3434
3435 JVM_LEAF(jboolean, JVM_IsSupportedJNIVersion(jint version))
3436 return Threads::is_supported_jni_version_including_1_1(version);
3437 JVM_END
3438
3439
3440 JVM_LEAF(jboolean, JVM_IsPreviewEnabled(void))
3441 return Arguments::enable_preview() ? JNI_TRUE : JNI_FALSE;
3442 JVM_END
3443
3444 JVM_LEAF(jboolean, JVM_IsContinuationsSupported(void))
3445 return VMContinuations ? JNI_TRUE : JNI_FALSE;
3446 JVM_END
3447
3448 JVM_LEAF(jboolean, JVM_IsForeignLinkerSupported(void))
3449 return ForeignGlobals::is_foreign_linker_supported() ? JNI_TRUE : JNI_FALSE;
3450 JVM_END
3451
3452 JVM_ENTRY_NO_ENV(jboolean, JVM_IsStaticallyLinked(void))
3453 return is_vm_statically_linked() ? JNI_TRUE : JNI_FALSE;
3454 JVM_END
3455
3456 // String support ///////////////////////////////////////////////////////////////////////////
3457
3458 JVM_ENTRY(jstring, JVM_InternString(JNIEnv *env, jstring str))
3459 JvmtiVMObjectAllocEventCollector oam;
3460 if (str == nullptr) return nullptr;
3461 oop string = JNIHandles::resolve_non_null(str);
3462 oop result = StringTable::intern(string, CHECK_NULL);
3463 return (jstring) JNIHandles::make_local(THREAD, result);
3464 JVM_END
3465
3466
3467 // VM Raw monitor support //////////////////////////////////////////////////////////////////////
3468
3469 // VM Raw monitors (not to be confused with JvmtiRawMonitors) are a simple mutual exclusion
3470 // lock (not actually monitors: no wait/notify) that is exported by the VM for use by JDK
3471 // library code. They may be used by JavaThreads and non-JavaThreads and do not participate
3472 // in the safepoint protocol, thread suspension, thread interruption, or anything of that
3473 // nature. JavaThreads will be "in native" when using this API from JDK code.
3474
3475
3476 JNIEXPORT void* JNICALL JVM_RawMonitorCreate(void) {
3477 VM_Exit::block_if_vm_exited();
3478 return new PlatformMutex();
3504 Handle loader, Handle protection_domain,
3505 jboolean throwError, TRAPS) {
3506 // Security Note:
3507 // The Java level wrapper will perform the necessary security check allowing
3508 // us to pass the null as the initiating class loader. The VM is responsible for
3509 // the checkPackageAccess relative to the initiating class loader via the
3510 // protection_domain. The protection_domain is passed as null by the java code
3511 // if there is no security manager in 3-arg Class.forName().
3512 Klass* klass = SystemDictionary::resolve_or_fail(name, loader, protection_domain, throwError != 0, CHECK_NULL);
3513
3514 // Check if we should initialize the class
3515 if (init && klass->is_instance_klass()) {
3516 klass->initialize(CHECK_NULL);
3517 }
3518 return (jclass) JNIHandles::make_local(THREAD, klass->java_mirror());
3519 }
3520
3521
3522 // Method ///////////////////////////////////////////////////////////////////////////////////////////
3523
3524 JVM_ENTRY(jobject, JVM_InvokeMethod(JNIEnv *env, jobject method, jobject obj, jobjectArray args0))
3525 Handle method_handle;
3526 if (thread->stack_overflow_state()->stack_available((address) &method_handle) >= JVMInvokeMethodSlack) {
3527 method_handle = Handle(THREAD, JNIHandles::resolve(method));
3528 Handle receiver(THREAD, JNIHandles::resolve(obj));
3529 objArrayHandle args(THREAD, objArrayOop(JNIHandles::resolve(args0)));
3530 oop result = Reflection::invoke_method(method_handle(), receiver, args, CHECK_NULL);
3531 jobject res = JNIHandles::make_local(THREAD, result);
3532 if (JvmtiExport::should_post_vm_object_alloc()) {
3533 oop ret_type = java_lang_reflect_Method::return_type(method_handle());
3534 assert(ret_type != nullptr, "sanity check: ret_type oop must not be null!");
3535 if (java_lang_Class::is_primitive(ret_type)) {
3536 // Only for primitive type vm allocates memory for java object.
3537 // See box() method.
3538 JvmtiExport::post_vm_object_alloc(thread, result);
3539 }
3540 }
3541 return res;
3542 } else {
3543 THROW_NULL(vmSymbols::java_lang_StackOverflowError());
3544 }
3545 JVM_END
3546
3547
3548 JVM_ENTRY(jobject, JVM_NewInstanceFromConstructor(JNIEnv *env, jobject c, jobjectArray args0))
3549 oop constructor_mirror = JNIHandles::resolve(c);
3550 objArrayHandle args(THREAD, objArrayOop(JNIHandles::resolve(args0)));
3551 oop result = Reflection::invoke_constructor(constructor_mirror, args, CHECK_NULL);
3552 jobject res = JNIHandles::make_local(THREAD, result);
3553 if (JvmtiExport::should_post_vm_object_alloc()) {
3554 JvmtiExport::post_vm_object_alloc(thread, result);
3555 }
3556 return res;
3557 JVM_END
3558
3559 JVM_ENTRY(void, JVM_InitializeFromArchive(JNIEnv* env, jclass cls))
3560 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve(cls));
3561 assert(k->is_klass(), "just checking");
3562 HeapShared::initialize_from_archived_subgraph(THREAD, k);
3563 JVM_END
3564
3565 JVM_ENTRY(void, JVM_RegisterLambdaProxyClassForArchiving(JNIEnv* env,
3566 jclass caller,
3567 jstring interfaceMethodName,
3568 jobject factoryType,
3569 jobject interfaceMethodType,
3570 jobject implementationMember,
3571 jobject dynamicMethodType,
3572 jclass lambdaProxyClass))
3573 #if INCLUDE_CDS
3574 if (!CDSConfig::is_dumping_archive()) {
3575 return;
3576 }
3577
3578 Klass* caller_k = java_lang_Class::as_Klass(JNIHandles::resolve(caller));
3579 InstanceKlass* caller_ik = InstanceKlass::cast(caller_k);
3580 if (caller_ik->is_hidden()) {
3581 // Hidden classes not of type lambda proxy classes are currently not being archived.
3582 // If the caller_ik is of one of the above types, the corresponding lambda proxy class won't be
3583 // registered for archiving.
3584 return;
3585 }
3593 interface_method_name = java_lang_String::as_symbol(JNIHandles::resolve_non_null(interfaceMethodName));
3594 }
3595 Handle factory_type_oop(THREAD, JNIHandles::resolve_non_null(factoryType));
3596 Symbol* factory_type = java_lang_invoke_MethodType::as_signature(factory_type_oop(), true);
3597
3598 Handle interface_method_type_oop(THREAD, JNIHandles::resolve_non_null(interfaceMethodType));
3599 Symbol* interface_method_type = java_lang_invoke_MethodType::as_signature(interface_method_type_oop(), true);
3600
3601 Handle implementation_member_oop(THREAD, JNIHandles::resolve_non_null(implementationMember));
3602 assert(java_lang_invoke_MemberName::is_method(implementation_member_oop()), "must be");
3603 Method* m = java_lang_invoke_MemberName::vmtarget(implementation_member_oop());
3604
3605 Handle dynamic_method_type_oop(THREAD, JNIHandles::resolve_non_null(dynamicMethodType));
3606 Symbol* dynamic_method_type = java_lang_invoke_MethodType::as_signature(dynamic_method_type_oop(), true);
3607
3608 SystemDictionaryShared::add_lambda_proxy_class(caller_ik, lambda_ik, interface_method_name, factory_type,
3609 interface_method_type, m, dynamic_method_type, THREAD);
3610 #endif // INCLUDE_CDS
3611 JVM_END
3612
3613 JVM_ENTRY(jclass, JVM_LookupLambdaProxyClassFromArchive(JNIEnv* env,
3614 jclass caller,
3615 jstring interfaceMethodName,
3616 jobject factoryType,
3617 jobject interfaceMethodType,
3618 jobject implementationMember,
3619 jobject dynamicMethodType))
3620 #if INCLUDE_CDS
3621
3622 if (interfaceMethodName == nullptr || factoryType == nullptr || interfaceMethodType == nullptr ||
3623 implementationMember == nullptr || dynamicMethodType == nullptr) {
3624 THROW_(vmSymbols::java_lang_NullPointerException(), nullptr);
3625 }
3626
3627 Klass* caller_k = java_lang_Class::as_Klass(JNIHandles::resolve(caller));
3628 InstanceKlass* caller_ik = InstanceKlass::cast(caller_k);
3629 if (!caller_ik->is_shared()) {
3630 // there won't be a shared lambda class if the caller_ik is not in the shared archive.
3631 return nullptr;
3632 }
3633
3641 Handle implementation_member_oop(THREAD, JNIHandles::resolve_non_null(implementationMember));
3642 assert(java_lang_invoke_MemberName::is_method(implementation_member_oop()), "must be");
3643 Method* m = java_lang_invoke_MemberName::vmtarget(implementation_member_oop());
3644
3645 Handle dynamic_method_type_oop(THREAD, JNIHandles::resolve_non_null(dynamicMethodType));
3646 Symbol* dynamic_method_type = java_lang_invoke_MethodType::as_signature(dynamic_method_type_oop(), true);
3647
3648 InstanceKlass* lambda_ik = SystemDictionaryShared::get_shared_lambda_proxy_class(caller_ik, interface_method_name, factory_type,
3649 interface_method_type, m, dynamic_method_type);
3650 jclass jcls = nullptr;
3651 if (lambda_ik != nullptr) {
3652 InstanceKlass* loaded_lambda = SystemDictionaryShared::prepare_shared_lambda_proxy_class(lambda_ik, caller_ik, THREAD);
3653 jcls = loaded_lambda == nullptr ? nullptr : (jclass) JNIHandles::make_local(THREAD, loaded_lambda->java_mirror());
3654 }
3655 return jcls;
3656 #else
3657 return nullptr;
3658 #endif // INCLUDE_CDS
3659 JVM_END
3660
3661 JVM_ENTRY_NO_ENV(jlong, JVM_GetRandomSeedForDumping())
3662 if (CDSConfig::is_dumping_static_archive()) {
3663 // We do this so that the default CDS archive can be deterministic.
3664 const char* release = VM_Version::vm_release();
3665 const char* dbg_level = VM_Version::jdk_debug_level();
3666 const char* version = VM_Version::internal_vm_info_string();
3667 jlong seed = (jlong)(java_lang_String::hash_code((const jbyte*)release, (int)strlen(release)) ^
3668 java_lang_String::hash_code((const jbyte*)dbg_level, (int)strlen(dbg_level)) ^
3669 java_lang_String::hash_code((const jbyte*)version, (int)strlen(version)));
3670 seed += (jlong)VM_Version::vm_major_version();
3671 seed += (jlong)VM_Version::vm_minor_version();
3672 seed += (jlong)VM_Version::vm_security_version();
3673 seed += (jlong)VM_Version::vm_patch_version();
3674 if (seed == 0) { // don't let this ever be zero.
3675 seed = 0x87654321;
3676 }
3677 log_debug(cds)("JVM_GetRandomSeedForDumping() = " JLONG_FORMAT, seed);
3678 return seed;
3679 } else {
3680 return 0;
3681 }
3682 JVM_END
3683
3684 JVM_ENTRY_NO_ENV(jint, JVM_GetCDSConfigStatus())
3685 return CDSConfig::get_status();
3686 JVM_END
3687
3688 JVM_ENTRY(void, JVM_LogLambdaFormInvoker(JNIEnv *env, jstring line))
3689 #if INCLUDE_CDS
3690 assert(CDSConfig::is_logging_lambda_form_invokers(), "sanity");
3691 if (line != nullptr) {
3692 ResourceMark rm(THREAD);
3693 Handle h_line (THREAD, JNIHandles::resolve_non_null(line));
3694 char* c_line = java_lang_String::as_utf8_string(h_line());
3695 if (CDSConfig::is_dumping_dynamic_archive()) {
3696 // Note: LambdaFormInvokers::append take same format which is not
3697 // same as below the print format. The line does not include LAMBDA_FORM_TAG.
3698 LambdaFormInvokers::append(os::strdup((const char*)c_line, mtInternal));
3699 }
3700 if (ClassListWriter::is_enabled()) {
3701 ClassListWriter w;
3702 w.stream()->print_cr("%s %s", ClassListParser::lambda_form_tag(), c_line);
3703 }
3704 }
3705 #endif // INCLUDE_CDS
3706 JVM_END
3707
3708 JVM_ENTRY(void, JVM_DumpClassListToFile(JNIEnv *env, jstring listFileName))
3709 #if INCLUDE_CDS
3710 ResourceMark rm(THREAD);
3711 Handle file_handle(THREAD, JNIHandles::resolve_non_null(listFileName));
3712 char* file_name = java_lang_String::as_utf8_string(file_handle());
3713 MetaspaceShared::dump_loaded_classes(file_name, THREAD);
3714 #endif // INCLUDE_CDS
3715 JVM_END
3716
3717 JVM_ENTRY(void, JVM_DumpDynamicArchive(JNIEnv *env, jstring archiveName))
3718 #if INCLUDE_CDS
3719 ResourceMark rm(THREAD);
3720 Handle file_handle(THREAD, JNIHandles::resolve_non_null(archiveName));
3721 char* archive_name = java_lang_String::as_utf8_string(file_handle());
3722 DynamicArchive::dump_for_jcmd(archive_name, CHECK);
3723 #endif // INCLUDE_CDS
3724 JVM_END
3725
3726 // Returns an array of all live Thread objects (VM internal JavaThreads,
3727 // jvmti agent threads, and JNI attaching threads are skipped)
3728 // See CR 6404306 regarding JNI attaching threads
3729 JVM_ENTRY(jobjectArray, JVM_GetAllThreads(JNIEnv *env, jclass dummy))
3730 ResourceMark rm(THREAD);
3731 ThreadsListEnumerator tle(THREAD, false, false);
3732 JvmtiVMObjectAllocEventCollector oam;
3733
3734 int num_threads = tle.num_threads();
3735 objArrayOop r = oopFactory::new_objArray(vmClasses::Thread_klass(), num_threads, CHECK_NULL);
3736 objArrayHandle threads_ah(THREAD, r);
3737
3738 for (int i = 0; i < num_threads; i++) {
3739 Handle h = tle.get_threadObj(i);
3740 threads_ah->obj_at_put(i, h());
3741 }
3742
3743 return (jobjectArray) JNIHandles::make_local(THREAD, threads_ah());
3744 JVM_END
3745
3746
3747 // Support for java.lang.Thread.getStackTrace() and getAllStackTraces() methods
3748 // Return StackTraceElement[][], each element is the stack trace of a thread in
3749 // the corresponding entry in the given threads array
3750 JVM_ENTRY(jobjectArray, JVM_DumpThreads(JNIEnv *env, jclass threadClass, jobjectArray threads))
3751 JvmtiVMObjectAllocEventCollector oam;
3752
3753 // Check if threads is null
3754 if (threads == nullptr) {
3755 THROW_NULL(vmSymbols::java_lang_NullPointerException());
3756 }
3757
3758 objArrayOop a = objArrayOop(JNIHandles::resolve_non_null(threads));
3759 objArrayHandle ah(THREAD, a);
3760 int num_threads = ah->length();
3761 // check if threads is non-empty array
3762 if (num_threads == 0) {
3763 THROW_NULL(vmSymbols::java_lang_IllegalArgumentException());
3764 }
3765
3766 // check if threads is not an array of objects of Thread class
3767 Klass* k = ObjArrayKlass::cast(ah->klass())->element_klass();
3768 if (k != vmClasses::Thread_klass()) {
3769 THROW_NULL(vmSymbols::java_lang_IllegalArgumentException());
3770 }
3771
3772 ResourceMark rm(THREAD);
3773
3774 GrowableArray<instanceHandle>* thread_handle_array = new GrowableArray<instanceHandle>(num_threads);
3775 for (int i = 0; i < num_threads; i++) {
3776 oop thread_obj = ah->obj_at(i);
3777 instanceHandle h(THREAD, (instanceOop) thread_obj);
3778 thread_handle_array->append(h);
3779 }
3780
3781 // The JavaThread references in thread_handle_array are validated
3782 // in VM_ThreadDump::doit().
3783 Handle stacktraces = ThreadService::dump_stack_traces(thread_handle_array, num_threads, CHECK_NULL);
3784 return (jobjectArray)JNIHandles::make_local(THREAD, stacktraces());
3785
3786 JVM_END
3787
3788 // JVM monitoring and management support
3789 JVM_LEAF(void*, JVM_GetManagement(jint version))
3790 return Management::get_jmm_interface(version);
3791 JVM_END
3792
3793 // com.sun.tools.attach.VirtualMachine agent properties support
3794 //
3795 // Initialize the agent properties with the properties maintained in the VM
3796 JVM_ENTRY(jobject, JVM_InitAgentProperties(JNIEnv *env, jobject properties))
3797 ResourceMark rm;
3798
3799 Handle props(THREAD, JNIHandles::resolve_non_null(properties));
3800
3801 PUTPROP(props, "sun.java.command", Arguments::java_command());
3802 PUTPROP(props, "sun.jvm.flags", Arguments::jvm_flags());
3803 PUTPROP(props, "sun.jvm.args", Arguments::jvm_args());
3804 return properties;
3805 JVM_END
3806
3807 JVM_ENTRY(jobjectArray, JVM_GetEnclosingMethodInfo(JNIEnv *env, jclass ofClass))
3808 {
3809 JvmtiVMObjectAllocEventCollector oam;
3810
3811 if (ofClass == nullptr) {
3812 return nullptr;
3813 }
3814 Handle mirror(THREAD, JNIHandles::resolve_non_null(ofClass));
3815 // Special handling for primitive objects
3816 if (java_lang_Class::is_primitive(mirror())) {
3817 return nullptr;
3818 }
3819 Klass* k = java_lang_Class::as_Klass(mirror());
3820 if (!k->is_instance_klass()) {
3821 return nullptr;
3822 }
3823 InstanceKlass* ik = InstanceKlass::cast(k);
3824 int encl_method_class_idx = ik->enclosing_method_class_index();
3825 if (encl_method_class_idx == 0) {
3826 return nullptr;
3827 }
3830 Klass* enc_k = ik->constants()->klass_at(encl_method_class_idx, CHECK_NULL);
3831 dest->obj_at_put(0, enc_k->java_mirror());
3832 int encl_method_method_idx = ik->enclosing_method_method_index();
3833 if (encl_method_method_idx != 0) {
3834 Symbol* sym = ik->constants()->symbol_at(
3835 extract_low_short_from_int(
3836 ik->constants()->name_and_type_at(encl_method_method_idx)));
3837 Handle str = java_lang_String::create_from_symbol(sym, CHECK_NULL);
3838 dest->obj_at_put(1, str());
3839 sym = ik->constants()->symbol_at(
3840 extract_high_short_from_int(
3841 ik->constants()->name_and_type_at(encl_method_method_idx)));
3842 str = java_lang_String::create_from_symbol(sym, CHECK_NULL);
3843 dest->obj_at_put(2, str());
3844 }
3845 return (jobjectArray) JNIHandles::make_local(THREAD, dest());
3846 }
3847 JVM_END
3848
3849 // Returns an array of java.lang.String objects containing the input arguments to the VM.
3850 JVM_ENTRY(jobjectArray, JVM_GetVmArguments(JNIEnv *env))
3851 ResourceMark rm(THREAD);
3852
3853 if (Arguments::num_jvm_args() == 0 && Arguments::num_jvm_flags() == 0) {
3854 return nullptr;
3855 }
3856
3857 char** vm_flags = Arguments::jvm_flags_array();
3858 char** vm_args = Arguments::jvm_args_array();
3859 int num_flags = Arguments::num_jvm_flags();
3860 int num_args = Arguments::num_jvm_args();
3861
3862 InstanceKlass* ik = vmClasses::String_klass();
3863 objArrayOop r = oopFactory::new_objArray(ik, num_args + num_flags, CHECK_NULL);
3864 objArrayHandle result_h(THREAD, r);
3865
3866 int index = 0;
3867 for (int j = 0; j < num_flags; j++, index++) {
3868 Handle h = java_lang_String::create_from_platform_dependent_str(vm_flags[j], CHECK_NULL);
3869 result_h->obj_at_put(index, h());
3870 }
3871 for (int i = 0; i < num_args; i++, index++) {
3872 Handle h = java_lang_String::create_from_platform_dependent_str(vm_args[i], CHECK_NULL);
3873 result_h->obj_at_put(index, h());
3874 }
3875 return (jobjectArray) JNIHandles::make_local(THREAD, result_h());
3876 JVM_END
3877
3878 JVM_LEAF(jint, JVM_FindSignal(const char *name))
3879 return os::get_signal_number(name);
3880 JVM_END
3881
3882 JVM_ENTRY(void, JVM_VirtualThreadStart(JNIEnv* env, jobject vthread))
3883 #if INCLUDE_JVMTI
3884 if (!DoJVMTIVirtualThreadTransitions) {
3885 assert(!JvmtiExport::can_support_virtual_threads(), "sanity check");
3886 return;
3887 }
3888 if (JvmtiVTMSTransitionDisabler::VTMS_notify_jvmti_events()) {
3889 JvmtiVTMSTransitionDisabler::VTMS_vthread_start(vthread);
3890 } else {
3891 // set VTMS transition bit value in JavaThread and java.lang.VirtualThread object
3892 JvmtiVTMSTransitionDisabler::set_is_in_VTMS_transition(thread, vthread, false);
3893 }
3894 #endif
3895 JVM_END
3896
3897 JVM_ENTRY(void, JVM_VirtualThreadEnd(JNIEnv* env, jobject vthread))
3898 #if INCLUDE_JVMTI
3899 if (!DoJVMTIVirtualThreadTransitions) {
3900 assert(!JvmtiExport::can_support_virtual_threads(), "sanity check");
3901 return;
3902 }
3903 if (JvmtiVTMSTransitionDisabler::VTMS_notify_jvmti_events()) {
3904 JvmtiVTMSTransitionDisabler::VTMS_vthread_end(vthread);
3905 } else {
3906 // set VTMS transition bit value in JavaThread and java.lang.VirtualThread object
3907 JvmtiVTMSTransitionDisabler::set_is_in_VTMS_transition(thread, vthread, true);
3908 }
3909 #endif
3910 JVM_END
3911
3912 // If notifications are disabled then just update the VTMS transition bit and return.
3913 // Otherwise, the bit is updated in the given jvmtiVTMSTransitionDisabler function call.
3914 JVM_ENTRY(void, JVM_VirtualThreadMount(JNIEnv* env, jobject vthread, jboolean hide))
3915 #if INCLUDE_JVMTI
3916 if (!DoJVMTIVirtualThreadTransitions) {
3917 assert(!JvmtiExport::can_support_virtual_threads(), "sanity check");
3918 return;
3919 }
3920 if (JvmtiVTMSTransitionDisabler::VTMS_notify_jvmti_events()) {
3921 JvmtiVTMSTransitionDisabler::VTMS_vthread_mount(vthread, hide);
3922 } else {
3923 // set VTMS transition bit value in JavaThread and java.lang.VirtualThread object
3924 JvmtiVTMSTransitionDisabler::set_is_in_VTMS_transition(thread, vthread, hide);
3925 }
3926 #endif
3927 JVM_END
3928
3929 // If notifications are disabled then just update the VTMS transition bit and return.
3930 // Otherwise, the bit is updated in the given jvmtiVTMSTransitionDisabler function call below.
3931 JVM_ENTRY(void, JVM_VirtualThreadUnmount(JNIEnv* env, jobject vthread, jboolean hide))
3932 #if INCLUDE_JVMTI
3933 if (!DoJVMTIVirtualThreadTransitions) {
3934 assert(!JvmtiExport::can_support_virtual_threads(), "sanity check");
3935 return;
3936 }
3937 if (JvmtiVTMSTransitionDisabler::VTMS_notify_jvmti_events()) {
3938 JvmtiVTMSTransitionDisabler::VTMS_vthread_unmount(vthread, hide);
3939 } else {
3940 // set VTMS transition bit value in JavaThread and java.lang.VirtualThread object
3941 JvmtiVTMSTransitionDisabler::set_is_in_VTMS_transition(thread, vthread, hide);
3942 }
3943 #endif
3944 JVM_END
3945
3946 // Always update the temporary VTMS transition bit.
3947 JVM_ENTRY(void, JVM_VirtualThreadHideFrames(JNIEnv* env, jclass clazz, jboolean hide))
3948 #if INCLUDE_JVMTI
3949 if (!DoJVMTIVirtualThreadTransitions) {
3950 assert(!JvmtiExport::can_support_virtual_threads(), "sanity check");
3951 return;
3952 }
3953 assert(!thread->is_in_VTMS_transition(), "sanity check");
3954 assert(thread->is_in_tmp_VTMS_transition() != (bool)hide, "sanity check");
3955 thread->toggle_is_in_tmp_VTMS_transition();
3956 #endif
3957 JVM_END
3958
3959 // Notification from VirtualThread about disabling JVMTI Suspend in a sync critical section.
3960 // Needed to avoid deadlocks with JVMTI suspend mechanism.
3961 JVM_ENTRY(void, JVM_VirtualThreadDisableSuspend(JNIEnv* env, jclass clazz, jboolean enter))
3962 #if INCLUDE_JVMTI
3963 if (!DoJVMTIVirtualThreadTransitions) {
3964 assert(!JvmtiExport::can_support_virtual_threads(), "sanity check");
3965 return;
3966 }
3967 assert(thread->is_disable_suspend() != (bool)enter,
3968 "nested or unbalanced monitor enter/exit is not allowed");
3969 thread->toggle_is_disable_suspend();
3970 #endif
3971 JVM_END
3972
3973 /*
3974 * Return the current class's class file version. The low order 16 bits of the
3975 * returned jint contain the class's major version. The high order 16 bits
3976 * contain the class's minor version.
3977 */
3978 JVM_ENTRY(jint, JVM_GetClassFileVersion(JNIEnv* env, jclass current))
3979 oop mirror = JNIHandles::resolve_non_null(current);
3980 if (java_lang_Class::is_primitive(mirror)) {
3981 // return latest major version and minor version of 0.
3982 return JVM_CLASSFILE_MAJOR_VERSION;
3983 }
3984 assert(!java_lang_Class::as_Klass(mirror)->is_array_klass(), "unexpected array class");
3985
3986 Klass* c = java_lang_Class::as_Klass(mirror);
3987 assert(c->is_instance_klass(), "must be");
3988 InstanceKlass* ik = InstanceKlass::cast(c);
3989 return (ik->minor_version() << 16) | ik->major_version();
3990 JVM_END
3991
3992 /*
3993 * Ensure that code doing a stackwalk and using javaVFrame::locals() to
3994 * get the value will see a materialized value and not a scalar-replaced
3995 * null value.
3996 */
3997 JVM_ENTRY(void, JVM_EnsureMaterializedForStackWalk_func(JNIEnv* env, jobject vthread, jobject value))
3998 JVM_EnsureMaterializedForStackWalk(env, value);
3999 JVM_END
4000
4001 /*
4002 * Return JNI_TRUE if warnings are printed when agents are dynamically loaded.
4003 */
4004 JVM_LEAF(jboolean, JVM_PrintWarningAtDynamicAgentLoad(void))
4005 return (EnableDynamicAgentLoading && !FLAG_IS_CMDLINE(EnableDynamicAgentLoading)) ? JNI_TRUE : JNI_FALSE;
4006 JVM_END
|
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 *
23 */
24
25 #include "precompiled.hpp"
26 #include "cds/aotConstantPoolResolver.hpp"
27 #include "cds/cdsConfig.hpp"
28 #include "cds/classListParser.hpp"
29 #include "cds/classListWriter.hpp"
30 #include "cds/dynamicArchive.hpp"
31 #include "cds/heapShared.hpp"
32 #include "cds/lambdaFormInvokers.hpp"
33 #include "classfile/classFileStream.hpp"
34 #include "classfile/classLoader.inline.hpp"
35 #include "classfile/classLoaderData.hpp"
36 #include "classfile/classLoaderData.inline.hpp"
37 #include "classfile/classLoadInfo.hpp"
38 #include "classfile/javaAssertions.hpp"
39 #include "classfile/javaClasses.inline.hpp"
40 #include "classfile/moduleEntry.hpp"
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"
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"
81 #include "runtime/handshake.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/os.inline.hpp"
88 #include "runtime/osThread.hpp"
89 #include "runtime/perfData.inline.hpp"
90 #include "runtime/reflection.hpp"
91 #include "runtime/synchronizer.hpp"
92 #include "runtime/threadIdentifier.hpp"
93 #include "runtime/threadSMR.hpp"
94 #include "runtime/vframe.inline.hpp"
95 #include "runtime/vmOperations.hpp"
96 #include "runtime/vm_version.hpp"
97 #include "services/attachListener.hpp"
98 #include "services/management.hpp"
99 #include "services/threadService.hpp"
100 #include "utilities/checkedCast.hpp"
101 #include "utilities/copy.hpp"
102 #include "utilities/defaultStream.hpp"
103 #include "utilities/dtrace.hpp"
104 #include "utilities/events.hpp"
105 #include "utilities/macros.hpp"
106 #include "utilities/utf8.hpp"
107 #include "utilities/zipLibrary.hpp"
108 #if INCLUDE_CDS
109 #include "classfile/systemDictionaryShared.hpp"
227 if (source_file != nullptr) {
228 log_debug(class, resolve)("%s %s %s:%d (%s)", from, to, source_file, line_number, trace);
229 } else {
230 log_debug(class, resolve)("%s %s (%s)", from, to, trace);
231 }
232 }
233 }
234 }
235
236 void trace_class_resolution(Klass* to_class) {
237 EXCEPTION_MARK;
238 trace_class_resolution_impl(to_class, THREAD);
239 if (HAS_PENDING_EXCEPTION) {
240 CLEAR_PENDING_EXCEPTION;
241 }
242 }
243
244 // java.lang.System //////////////////////////////////////////////////////////////////////
245
246
247 JVM_LEAF_PROF(jlong, JVM_CurrentTimeMillis, JVM_CurrentTimeMillis(JNIEnv *env, jclass ignored))
248 return os::javaTimeMillis();
249 JVM_END
250
251 JVM_LEAF_PROF(jlong, JVM_NanoTime, JVM_NanoTime(JNIEnv *env, jclass ignored))
252 return os::javaTimeNanos();
253 JVM_END
254
255 // The function below is actually exposed by jdk.internal.misc.VM and not
256 // java.lang.System, but we choose to keep it here so that it stays next
257 // to JVM_CurrentTimeMillis and JVM_NanoTime
258
259 const jlong MAX_DIFF_SECS = CONST64(0x0100000000); // 2^32
260 const jlong MIN_DIFF_SECS = -MAX_DIFF_SECS; // -2^32
261
262 JVM_LEAF_PROF(jlong, JVM_GetNanoTimeAdjustment, JVM_GetNanoTimeAdjustment(JNIEnv *env, jclass ignored, jlong offset_secs))
263 jlong seconds;
264 jlong nanos;
265
266 os::javaTimeSystemUTC(seconds, nanos);
267
268 // We're going to verify that the result can fit in a long.
269 // For that we need the difference in seconds between 'seconds'
270 // and 'offset_secs' to be such that:
271 // |seconds - offset_secs| < (2^63/10^9)
272 // We're going to approximate 10^9 ~< 2^30 (1000^3 ~< 1024^3)
273 // which makes |seconds - offset_secs| < 2^33
274 // and we will prefer +/- 2^32 as the maximum acceptable diff
275 // as 2^32 has a more natural feel than 2^33...
276 //
277 // So if |seconds - offset_secs| >= 2^32 - we return a special
278 // sentinel value (-1) which the caller should take as an
279 // exception value indicating that the offset given to us is
280 // too far from range of the current time - leading to too big
281 // a nano adjustment. The caller is expected to recover by
282 // computing a more accurate offset and calling this method
283 // again. (For the record 2^32 secs is ~136 years, so that
284 // should rarely happen)
285 //
286 jlong diff = seconds - offset_secs;
287 if (diff >= MAX_DIFF_SECS || diff <= MIN_DIFF_SECS) {
288 return -1; // sentinel value: the offset is too far off the target
289 }
290
291 // return the adjustment. If you compute a time by adding
292 // this number of nanoseconds along with the number of seconds
293 // in the offset you should get the current UTC time.
294 return (diff * (jlong)1000000000) + nanos;
295 JVM_END
296
297 JVM_ENTRY_PROF(void, JVM_ArrayCopy, JVM_ArrayCopy(JNIEnv *env, jclass ignored, jobject src, jint src_pos,
298 jobject dst, jint dst_pos, jint length))
299 // Check if we have null pointers
300 if (src == nullptr || dst == nullptr) {
301 THROW(vmSymbols::java_lang_NullPointerException());
302 }
303 arrayOop s = arrayOop(JNIHandles::resolve_non_null(src));
304 arrayOop d = arrayOop(JNIHandles::resolve_non_null(dst));
305 assert(oopDesc::is_oop(s), "JVM_ArrayCopy: src not an oop");
306 assert(oopDesc::is_oop(d), "JVM_ArrayCopy: dst not an oop");
307 // Do copy
308 s->klass()->copy_array(s, src_pos, d, dst_pos, length, thread);
309 JVM_END
310
311
312 static void set_property(Handle props, const char* key, const char* value, TRAPS) {
313 JavaValue r(T_OBJECT);
314 // public synchronized Object put(Object key, Object value);
315 HandleMark hm(THREAD);
316 Handle key_str = java_lang_String::create_from_platform_dependent_str(key, CHECK);
317 Handle value_str = java_lang_String::create_from_platform_dependent_str((value != nullptr ? value : ""), CHECK);
318 JavaCalls::call_virtual(&r,
319 props,
320 vmClasses::Properties_klass(),
321 vmSymbols::put_name(),
322 vmSymbols::object_object_object_signature(),
323 key_str,
324 value_str,
325 THREAD);
326 }
327
328
329 #define PUTPROP(props, name, value) set_property((props), (name), (value), CHECK_(properties));
330
331 /*
332 * Return all of the system properties in a Java String array with alternating
333 * names and values from the jvm SystemProperty.
334 * Which includes some internal and all commandline -D defined properties.
335 */
336 JVM_ENTRY_PROF(jobjectArray, JVM_GetProperties, JVM_GetProperties(JNIEnv *env))
337 ResourceMark rm(THREAD);
338 HandleMark hm(THREAD);
339 int ndx = 0;
340 int fixedCount = 2;
341
342 SystemProperty* p = Arguments::system_properties();
343 int count = Arguments::PropertyList_count(p);
344
345 // Allocate result String array
346 InstanceKlass* ik = vmClasses::String_klass();
347 objArrayOop r = oopFactory::new_objArray(ik, (count + fixedCount) * 2, CHECK_NULL);
348 objArrayHandle result_h(THREAD, r);
349
350 while (p != nullptr) {
351 const char * key = p->key();
352 if (strcmp(key, "sun.nio.MaxDirectMemorySize") != 0) {
353 const char * value = p->value();
354 Handle key_str = java_lang_String::create_from_platform_dependent_str(key, CHECK_NULL);
355 Handle value_str = java_lang_String::create_from_platform_dependent_str((value != nullptr ? value : ""), CHECK_NULL);
356 result_h->obj_at_put(ndx * 2, key_str());
404 Handle key_str = java_lang_String::create_from_platform_dependent_str("sun.management.compiler", CHECK_NULL);
405 Handle value_str = java_lang_String::create_from_platform_dependent_str(compiler_name, CHECK_NULL);
406 result_h->obj_at_put(ndx * 2, key_str());
407 result_h->obj_at_put(ndx * 2 + 1, value_str());
408 ndx++;
409 }
410 }
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_PROF(jstring, JVM_GetTemporaryDirectory, 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
432 // java.lang.Runtime /////////////////////////////////////////////////////////////////////////
433
434 extern volatile jint vm_created;
435
436 JVM_ENTRY_NO_ENV_PROF(void, JVM_BeforeHalt, JVM_BeforeHalt())
437 EventShutdown event;
438 if (event.should_commit()) {
439 event.set_reason("Shutdown requested from Java");
440 event.commit();
441 }
442 JVM_END
443
444
445 JVM_ENTRY_NO_ENV_PROF(void, JVM_Halt, JVM_Halt(jint code))
446 before_exit(thread, true);
447 vm_exit(code);
448 JVM_END
449
450
451 JVM_ENTRY_NO_ENV_PROF(void, JVM_GC, JVM_GC(void))
452 if (!DisableExplicitGC) {
453 EventSystemGC event;
454 event.set_invokedConcurrent(ExplicitGCInvokesConcurrent);
455 Universe::heap()->collect(GCCause::_java_lang_system_gc);
456 event.commit();
457 }
458 JVM_END
459
460
461 JVM_LEAF_PROF(jlong, JVM_MaxObjectInspectionAge, JVM_MaxObjectInspectionAge(void))
462 return Universe::heap()->millis_since_last_whole_heap_examined();
463 JVM_END
464
465
466 static inline jlong convert_size_t_to_jlong(size_t val) {
467 // In the 64-bit vm, a size_t can overflow a jlong (which is signed).
468 NOT_LP64 (return (jlong)val;)
469 LP64_ONLY(return (jlong)MIN2(val, (size_t)max_jlong);)
470 }
471
472 JVM_ENTRY_NO_ENV_PROF(jlong, JVM_TotalMemory, JVM_TotalMemory(void))
473 size_t n = Universe::heap()->capacity();
474 return convert_size_t_to_jlong(n);
475 JVM_END
476
477
478 JVM_ENTRY_NO_ENV_PROF(jlong, JVM_FreeMemory, JVM_FreeMemory(void))
479 size_t n = Universe::heap()->unused();
480 return convert_size_t_to_jlong(n);
481 JVM_END
482
483
484 JVM_ENTRY_NO_ENV_PROF(jlong, JVM_MaxMemory, JVM_MaxMemory(void))
485 size_t n = Universe::heap()->max_capacity();
486 return convert_size_t_to_jlong(n);
487 JVM_END
488
489
490 JVM_ENTRY_NO_ENV_PROF(jint, JVM_ActiveProcessorCount, JVM_ActiveProcessorCount(void))
491 return os::active_processor_count();
492 JVM_END
493
494 JVM_LEAF_PROF(jboolean, JVM_IsUseContainerSupport, JVM_IsUseContainerSupport(void))
495 #ifdef LINUX
496 if (UseContainerSupport) {
497 return JNI_TRUE;
498 }
499 #endif
500 return JNI_FALSE;
501 JVM_END
502
503 JVM_LEAF(jboolean, JVM_IsContainerized(void))
504 #ifdef LINUX
505 if (OSContainer::is_containerized()) {
506 return JNI_TRUE;
507 }
508 #endif
509 return JNI_FALSE;
510 JVM_END
511
512 // java.lang.Throwable //////////////////////////////////////////////////////
513
514 JVM_ENTRY_PROF(void, JVM_FillInStackTrace, JVM_FillInStackTrace(JNIEnv *env, jobject receiver))
515 Handle exception(thread, JNIHandles::resolve_non_null(receiver));
516 java_lang_Throwable::fill_in_stack_trace(exception);
517 JVM_END
518
519 // java.lang.NullPointerException ///////////////////////////////////////////
520
521 JVM_ENTRY_PROF(jstring, JVM_GetExtendedNPEMessage, JVM_GetExtendedNPEMessage(JNIEnv *env, jthrowable throwable))
522 if (!ShowCodeDetailsInExceptionMessages) return nullptr;
523
524 oop exc = JNIHandles::resolve_non_null(throwable);
525
526 Method* method;
527 int bci;
528 if (!java_lang_Throwable::get_top_method_and_bci(exc, &method, &bci)) {
529 return nullptr;
530 }
531 if (method->is_native()) {
532 return nullptr;
533 }
534
535 stringStream ss;
536 bool ok = BytecodeUtils::get_NPE_message_at(&ss, method, bci);
537 if (ok) {
538 oop result = java_lang_String::create_oop_from_str(ss.base(), CHECK_NULL);
539 return (jstring) JNIHandles::make_local(THREAD, result);
540 } else {
541 return nullptr;
542 }
543 JVM_END
544
545 // java.lang.StackTraceElement //////////////////////////////////////////////
546
547
548 JVM_ENTRY_PROF(void, JVM_InitStackTraceElementArray, JVM_InitStackTraceElementArray(JNIEnv *env, jobjectArray elements, jobject backtrace, jint depth))
549 Handle backtraceh(THREAD, JNIHandles::resolve(backtrace));
550 objArrayOop st = objArrayOop(JNIHandles::resolve(elements));
551 objArrayHandle stack_trace(THREAD, st);
552 // Fill in the allocated stack trace
553 java_lang_Throwable::get_stack_trace_elements(depth, backtraceh, stack_trace, CHECK);
554 JVM_END
555
556
557 JVM_ENTRY_PROF(void, JVM_InitStackTraceElement, JVM_InitStackTraceElement(JNIEnv* env, jobject element, jobject stackFrameInfo))
558 Handle stack_frame_info(THREAD, JNIHandles::resolve_non_null(stackFrameInfo));
559 Handle stack_trace_element(THREAD, JNIHandles::resolve_non_null(element));
560 java_lang_StackFrameInfo::to_stack_trace_element(stack_frame_info, stack_trace_element, CHECK);
561 JVM_END
562
563
564 // java.lang.StackWalker //////////////////////////////////////////////////////
565 JVM_ENTRY_PROF(void, JVM_ExpandStackFrameInfo, JVM_ExpandStackFrameInfo(JNIEnv *env, jobject obj))
566 Handle stack_frame_info(THREAD, JNIHandles::resolve_non_null(obj));
567
568 bool have_name = (java_lang_StackFrameInfo::name(stack_frame_info()) != nullptr);
569 bool have_type = (java_lang_StackFrameInfo::type(stack_frame_info()) != nullptr);
570 Method* method = java_lang_StackFrameInfo::get_method(stack_frame_info());
571 if (!have_name) {
572 oop name = StringTable::intern(method->name(), CHECK);
573 java_lang_StackFrameInfo::set_name(stack_frame_info(), name);
574 }
575 if (!have_type) {
576 Handle type = java_lang_String::create_from_symbol(method->signature(), CHECK);
577 java_lang_StackFrameInfo::set_type(stack_frame_info(), type());
578 }
579 JVM_END
580
581 JVM_ENTRY_PROF(jobject, JVM_CallStackWalk, JVM_CallStackWalk(JNIEnv *env, jobject stackStream, jint mode,
582 jint skip_frames, jobject contScope, jobject cont,
583 jint buffer_size, jint start_index, jobjectArray frames))
584 if (!thread->has_last_Java_frame()) {
585 THROW_MSG_(vmSymbols::java_lang_InternalError(), "doStackWalk: no stack trace", nullptr);
586 }
587
588 Handle stackStream_h(THREAD, JNIHandles::resolve_non_null(stackStream));
589 Handle contScope_h(THREAD, JNIHandles::resolve(contScope));
590 Handle cont_h(THREAD, JNIHandles::resolve(cont));
591 // frames array is a ClassFrameInfo[] array when only getting caller reference,
592 // and a StackFrameInfo[] array (or derivative) otherwise. It should never
593 // be null.
594 objArrayOop fa = objArrayOop(JNIHandles::resolve_non_null(frames));
595 objArrayHandle frames_array_h(THREAD, fa);
596
597 if (frames_array_h->length() < buffer_size) {
598 THROW_MSG_(vmSymbols::java_lang_IllegalArgumentException(), "not enough space in buffers", nullptr);
599 }
600
601 oop result = StackWalk::walk(stackStream_h, mode, skip_frames, contScope_h, cont_h,
602 buffer_size, start_index, frames_array_h, CHECK_NULL);
603 return JNIHandles::make_local(THREAD, result);
604 JVM_END
605
606
607 JVM_ENTRY_PROF(jint, JVM_MoreStackWalk, JVM_MoreStackWalk(JNIEnv *env, jobject stackStream, jint mode, jlong anchor,
608 jint last_batch_count, jint buffer_size, jint start_index,
609 jobjectArray frames))
610 // frames array is a ClassFrameInfo[] array when only getting caller reference,
611 // and a StackFrameInfo[] array (or derivative) otherwise. It should never
612 // be null.
613 objArrayOop fa = objArrayOop(JNIHandles::resolve_non_null(frames));
614 objArrayHandle frames_array_h(THREAD, fa);
615
616 if (frames_array_h->length() < buffer_size) {
617 THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(), "not enough space in buffers");
618 }
619
620 Handle stackStream_h(THREAD, JNIHandles::resolve_non_null(stackStream));
621 return StackWalk::fetchNextBatch(stackStream_h, mode, anchor, last_batch_count, buffer_size,
622 start_index, frames_array_h, THREAD);
623 JVM_END
624
625 JVM_ENTRY_PROF(void, JVM_SetStackWalkContinuation, JVM_SetStackWalkContinuation(JNIEnv *env, jobject stackStream, jlong anchor, jobjectArray frames, jobject cont))
626 objArrayOop fa = objArrayOop(JNIHandles::resolve_non_null(frames));
627 objArrayHandle frames_array_h(THREAD, fa);
628 Handle stackStream_h(THREAD, JNIHandles::resolve_non_null(stackStream));
629 Handle cont_h(THREAD, JNIHandles::resolve_non_null(cont));
630
631 StackWalk::setContinuation(stackStream_h, anchor, frames_array_h, cont_h, THREAD);
632 JVM_END
633
634 // java.lang.Object ///////////////////////////////////////////////
635
636
637 JVM_ENTRY_PROF(jint, JVM_IHashCode, JVM_IHashCode(JNIEnv* env, jobject handle))
638 // as implemented in the classic virtual machine; return 0 if object is null
639 return handle == nullptr ? 0 :
640 checked_cast<jint>(ObjectSynchronizer::FastHashCode (THREAD, JNIHandles::resolve_non_null(handle)));
641 JVM_END
642
643
644 JVM_ENTRY_PROF(void, JVM_MonitorWait, JVM_MonitorWait(JNIEnv* env, jobject handle, jlong ms))
645 Handle obj(THREAD, JNIHandles::resolve_non_null(handle));
646 JavaThreadInObjectWaitState jtiows(thread, ms != 0);
647 if (JvmtiExport::should_post_monitor_wait()) {
648 JvmtiExport::post_monitor_wait(thread, obj(), ms);
649
650 // The current thread already owns the monitor and it has not yet
651 // been added to the wait queue so the current thread cannot be
652 // made the successor. This means that the JVMTI_EVENT_MONITOR_WAIT
653 // event handler cannot accidentally consume an unpark() meant for
654 // the ParkEvent associated with this ObjectMonitor.
655 }
656 ObjectSynchronizer::wait(obj, ms, CHECK);
657 JVM_END
658
659
660 JVM_ENTRY_PROF(void, JVM_MonitorNotify, JVM_MonitorNotify(JNIEnv* env, jobject handle))
661 Handle obj(THREAD, JNIHandles::resolve_non_null(handle));
662 ObjectSynchronizer::notify(obj, CHECK);
663 JVM_END
664
665
666 JVM_ENTRY_PROF(void, JVM_MonitorNotifyAll, JVM_MonitorNotifyAll(JNIEnv* env, jobject handle))
667 Handle obj(THREAD, JNIHandles::resolve_non_null(handle));
668 ObjectSynchronizer::notifyall(obj, CHECK);
669 JVM_END
670
671
672 JVM_ENTRY_PROF(jobject, JVM_Clone, JVM_Clone(JNIEnv* env, jobject handle))
673 Handle obj(THREAD, JNIHandles::resolve_non_null(handle));
674 Klass* klass = obj->klass();
675 JvmtiVMObjectAllocEventCollector oam;
676
677 #ifdef ASSERT
678 // Just checking that the cloneable flag is set correct
679 if (obj->is_array()) {
680 guarantee(klass->is_cloneable(), "all arrays are cloneable");
681 } else {
682 guarantee(obj->is_instance(), "should be instanceOop");
683 bool cloneable = klass->is_subtype_of(vmClasses::Cloneable_klass());
684 guarantee(cloneable == klass->is_cloneable(), "incorrect cloneable flag");
685 }
686 #endif
687
688 // Check if class of obj supports the Cloneable interface.
689 // All arrays are considered to be cloneable (See JLS 20.1.5).
690 // All j.l.r.Reference classes are considered non-cloneable.
691 if (!klass->is_cloneable() ||
692 (klass->is_instance_klass() &&
705 } else {
706 new_obj_oop = Universe::heap()->obj_allocate(klass, size, CHECK_NULL);
707 }
708
709 HeapAccess<>::clone(obj(), new_obj_oop, size);
710
711 Handle new_obj(THREAD, new_obj_oop);
712 // Caution: this involves a java upcall, so the clone should be
713 // "gc-robust" by this stage.
714 if (klass->has_finalizer()) {
715 assert(obj->is_instance(), "should be instanceOop");
716 new_obj_oop = InstanceKlass::register_finalizer(instanceOop(new_obj()), CHECK_NULL);
717 new_obj = Handle(THREAD, new_obj_oop);
718 }
719
720 return JNIHandles::make_local(THREAD, new_obj());
721 JVM_END
722
723 // java.lang.ref.Finalizer ////////////////////////////////////////////////////
724
725 JVM_ENTRY_PROF(void, JVM_ReportFinalizationComplete, JVM_ReportFinalizationComplete(JNIEnv * env, jobject finalizee))
726 MANAGEMENT_ONLY(FinalizerService::on_complete(JNIHandles::resolve_non_null(finalizee), THREAD);)
727 JVM_END
728
729 JVM_LEAF_PROF(jboolean, JVM_IsFinalizationEnabled, JVM_IsFinalizationEnabled(JNIEnv * env))
730 return InstanceKlass::is_finalization_enabled();
731 JVM_END
732
733 // jdk.internal.vm.Continuation /////////////////////////////////////////////////////
734
735 JVM_ENTRY_PROF(void, JVM_RegisterContinuationMethods, JVM_RegisterContinuationMethods(JNIEnv *env, jclass cls))
736 CONT_RegisterNativeMethods(env, cls);
737 JVM_END
738
739 // java.io.File ///////////////////////////////////////////////////////////////
740
741 JVM_LEAF_PROF(char*, JVM_NativePath, JVM_NativePath(char* path))
742 return os::native_path(path);
743 JVM_END
744
745
746 // Misc. class handling ///////////////////////////////////////////////////////////
747
748
749 JVM_ENTRY_PROF(jclass, JVM_GetCallerClass, JVM_GetCallerClass(JNIEnv* env))
750 // Getting the class of the caller frame.
751 //
752 // The call stack at this point looks something like this:
753 //
754 // [0] [ @CallerSensitive public jdk.internal.reflect.Reflection.getCallerClass ]
755 // [1] [ @CallerSensitive API.method ]
756 // [.] [ (skipped intermediate frames) ]
757 // [n] [ caller ]
758 vframeStream vfst(thread);
759 // Cf. LibraryCallKit::inline_native_Reflection_getCallerClass
760 for (int n = 0; !vfst.at_end(); vfst.security_next(), n++) {
761 Method* m = vfst.method();
762 assert(m != nullptr, "sanity");
763 switch (n) {
764 case 0:
765 // This must only be called from Reflection.getCallerClass
766 if (m->intrinsic_id() != vmIntrinsics::_getCallerClass) {
767 THROW_MSG_NULL(vmSymbols::java_lang_InternalError(), "JVM_GetCallerClass must only be called from Reflection.getCallerClass");
768 }
769 // fall-through
770 case 1:
771 // Frame 0 and 1 must be caller sensitive.
772 if (!m->caller_sensitive()) {
773 THROW_MSG_NULL(vmSymbols::java_lang_InternalError(), err_msg("CallerSensitive annotation expected at frame %d", n));
774 }
775 break;
776 default:
777 if (!m->is_ignored_by_security_stack_walk()) {
778 // We have reached the desired frame; return the holder class.
779 return (jclass) JNIHandles::make_local(THREAD, m->method_holder()->java_mirror());
780 }
781 break;
782 }
783 }
784 return nullptr;
785 JVM_END
786
787
788 JVM_ENTRY_PROF(jclass, JVM_FindPrimitiveClass, JVM_FindPrimitiveClass(JNIEnv* env, const char* utf))
789 oop mirror = nullptr;
790 BasicType t = name2type(utf);
791 if (t != T_ILLEGAL && !is_reference_type(t)) {
792 mirror = Universe::java_mirror(t);
793 }
794 if (mirror == nullptr) {
795 THROW_MSG_NULL(vmSymbols::java_lang_ClassNotFoundException(), (char*) utf);
796 } else {
797 return (jclass) JNIHandles::make_local(THREAD, mirror);
798 }
799 JVM_END
800
801
802 // Returns a class loaded by the bootstrap class loader; or null
803 // if not found. ClassNotFoundException is not thrown.
804 // FindClassFromBootLoader is exported to the launcher for windows.
805 JVM_ENTRY_PROF(jclass, JVM_FindClassFromBootLoader, JVM_FindClassFromBootLoader(JNIEnv* env, const char* name))
806 // Java libraries should ensure that name is never null or illegal.
807 if (name == nullptr || (int)strlen(name) > Symbol::max_length()) {
808 // It's impossible to create this class; the name cannot fit
809 // into the constant pool.
810 return nullptr;
811 }
812 assert(UTF8::is_legal_utf8((const unsigned char*)name, strlen(name), false), "illegal UTF name");
813
814 TempNewSymbol h_name = SymbolTable::new_symbol(name);
815 Klass* k = SystemDictionary::resolve_or_null(h_name, CHECK_NULL);
816 if (k == nullptr) {
817 return nullptr;
818 }
819
820 if (log_is_enabled(Debug, class, resolve)) {
821 trace_class_resolution(k);
822 }
823 return (jclass) JNIHandles::make_local(THREAD, k->java_mirror());
824 JVM_END
825
826 // Find a class with this name in this loader, using the caller's protection domain.
827 JVM_ENTRY_PROF(jclass, JVM_FindClassFromCaller, JVM_FindClassFromCaller(JNIEnv* env, const char* name,
828 jboolean init, jobject loader,
829 jclass caller))
830 TempNewSymbol h_name =
831 SystemDictionary::class_name_symbol(name, vmSymbols::java_lang_ClassNotFoundException(),
832 CHECK_NULL);
833
834 oop loader_oop = JNIHandles::resolve(loader);
835 oop from_class = JNIHandles::resolve(caller);
836 oop protection_domain = nullptr;
837 // If loader is null, shouldn't call ClassLoader.checkPackageAccess; otherwise get
838 // NPE. Put it in another way, the bootstrap class loader has all permission and
839 // thus no checkPackageAccess equivalence in the VM class loader.
840 // The caller is also passed as null by the java code if there is no security
841 // manager to avoid the performance cost of getting the calling class.
842 if (from_class != nullptr && loader_oop != nullptr) {
843 protection_domain = java_lang_Class::as_Klass(from_class)->protection_domain();
844 }
845
846 Handle h_loader(THREAD, loader_oop);
847 Handle h_prot(THREAD, protection_domain);
848 jclass result = find_class_from_class_loader(env, h_name, init, h_loader,
849 h_prot, false, THREAD);
850
851 if (log_is_enabled(Debug, class, resolve) && result != nullptr) {
852 trace_class_resolution(java_lang_Class::as_Klass(JNIHandles::resolve_non_null(result)));
853 }
854 return result;
855 JVM_END
856
857 // Currently only called from the old verifier.
858 JVM_ENTRY_PROF(jclass, JVM_FindClassFromClass, JVM_FindClassFromClass(JNIEnv *env, const char *name,
859 jboolean init, jclass from))
860 TempNewSymbol h_name =
861 SystemDictionary::class_name_symbol(name, vmSymbols::java_lang_ClassNotFoundException(),
862 CHECK_NULL);
863 oop from_class_oop = JNIHandles::resolve(from);
864 Klass* from_class = (from_class_oop == nullptr)
865 ? (Klass*)nullptr
866 : java_lang_Class::as_Klass(from_class_oop);
867 oop class_loader = nullptr;
868 oop protection_domain = nullptr;
869 if (from_class != nullptr) {
870 class_loader = from_class->class_loader();
871 protection_domain = from_class->protection_domain();
872 }
873 Handle h_loader(THREAD, class_loader);
874 Handle h_prot (THREAD, protection_domain);
875 jclass result = find_class_from_class_loader(env, h_name, init, h_loader,
876 h_prot, true, thread);
877
878 if (log_is_enabled(Debug, class, resolve) && result != nullptr) {
879 // this function is generally only used for class loading during verification.
1042 ik->external_name(),
1043 host_class->external_name(),
1044 ik->is_hidden() ? "is hidden" : "is not hidden");
1045 }
1046 }
1047
1048 if ((!is_hidden || is_nestmate) && !Reflection::is_same_class_package(lookup_k, ik)) {
1049 // non-hidden class or nestmate class must be in the same package as the Lookup class
1050 THROW_MSG_NULL(vmSymbols::java_lang_IllegalArgumentException(), "Lookup class and defined class are in different packages");
1051 }
1052
1053 if (init) {
1054 ik->initialize(CHECK_NULL);
1055 } else {
1056 ik->link_class(CHECK_NULL);
1057 }
1058
1059 return (jclass) JNIHandles::make_local(THREAD, ik->java_mirror());
1060 }
1061
1062 JVM_ENTRY_PROF(jclass, JVM_DefineClass, JVM_DefineClass(JNIEnv *env, const char *name, jobject loader, const jbyte *buf, jsize len, jobject pd))
1063 return jvm_define_class_common(name, loader, buf, len, pd, nullptr, THREAD);
1064 JVM_END
1065
1066 /*
1067 * Define a class with the specified lookup class.
1068 * lookup: Lookup class
1069 * name: the name of the class
1070 * buf: class bytes
1071 * len: length of class bytes
1072 * pd: protection domain
1073 * init: initialize the class
1074 * flags: properties of the class
1075 * classData: private static pre-initialized field
1076 */
1077 JVM_ENTRY_PROF(jclass, JVM_LookupDefineClass, JVM_LookupDefineClass(JNIEnv *env, jclass lookup, const char *name, const jbyte *buf,
1078 jsize len, jobject pd, jboolean initialize, int flags, jobject classData))
1079
1080 if (lookup == nullptr) {
1081 THROW_MSG_NULL(vmSymbols::java_lang_IllegalArgumentException(), "Lookup class is null");
1082 }
1083
1084 assert(buf != nullptr, "buf must not be null");
1085
1086 return jvm_lookup_define_class(lookup, name, buf, len, pd, initialize, flags, classData, THREAD);
1087 JVM_END
1088
1089 JVM_ENTRY_PROF(jclass, JVM_DefineClassWithSource, JVM_DefineClassWithSource(JNIEnv *env, const char *name, jobject loader, const jbyte *buf, jsize len, jobject pd, const char *source))
1090 return jvm_define_class_common(name, loader, buf, len, pd, source, THREAD);
1091 JVM_END
1092
1093 JVM_ENTRY_PROF(jclass, JVM_FindLoadedClass, JVM_FindLoadedClass(JNIEnv *env, jobject loader, jstring name))
1094 ResourceMark rm(THREAD);
1095
1096 Handle h_name (THREAD, JNIHandles::resolve_non_null(name));
1097 char* str = java_lang_String::as_utf8_string(h_name());
1098
1099 // Sanity check, don't expect null
1100 if (str == nullptr) return nullptr;
1101
1102 // Internalize the string, converting '.' to '/' in string.
1103 char* p = (char*)str;
1104 while (*p != '\0') {
1105 if (*p == '.') {
1106 *p = '/';
1107 }
1108 p++;
1109 }
1110
1111 const int str_len = (int)(p - str);
1112 if (str_len > Symbol::max_length()) {
1113 // It's impossible to create this class; the name cannot fit
1119 // Security Note:
1120 // The Java level wrapper will perform the necessary security check allowing
1121 // us to pass the null as the initiating class loader.
1122 Handle h_loader(THREAD, JNIHandles::resolve(loader));
1123 Klass* k = SystemDictionary::find_instance_or_array_klass(THREAD, klass_name,
1124 h_loader,
1125 Handle());
1126 #if INCLUDE_CDS
1127 if (k == nullptr) {
1128 // If the class is not already loaded, try to see if it's in the shared
1129 // archive for the current classloader (h_loader).
1130 k = SystemDictionaryShared::find_or_load_shared_class(klass_name, h_loader, CHECK_NULL);
1131 }
1132 #endif
1133 return (k == nullptr) ? nullptr :
1134 (jclass) JNIHandles::make_local(THREAD, k->java_mirror());
1135 JVM_END
1136
1137 // Module support //////////////////////////////////////////////////////////////////////////////
1138
1139 JVM_ENTRY_PROF(void, JVM_DefineModule, JVM_DefineModule(JNIEnv *env, jobject module, jboolean is_open, jstring version,
1140 jstring location, jobjectArray packages))
1141 Handle h_module (THREAD, JNIHandles::resolve(module));
1142 Modules::define_module(h_module, is_open, version, location, packages, CHECK);
1143 JVM_END
1144
1145 JVM_ENTRY_PROF(void, JVM_SetBootLoaderUnnamedModule, JVM_SetBootLoaderUnnamedModule(JNIEnv *env, jobject module))
1146 Handle h_module (THREAD, JNIHandles::resolve(module));
1147 Modules::set_bootloader_unnamed_module(h_module, CHECK);
1148 JVM_END
1149
1150 JVM_ENTRY_PROF(void, JVM_AddModuleExports, JVM_AddModuleExports(JNIEnv *env, jobject from_module, jstring package, jobject to_module))
1151 Handle h_from_module (THREAD, JNIHandles::resolve(from_module));
1152 Handle h_to_module (THREAD, JNIHandles::resolve(to_module));
1153 Modules::add_module_exports_qualified(h_from_module, package, h_to_module, CHECK);
1154 JVM_END
1155
1156 JVM_ENTRY_PROF(void, JVM_AddModuleExportsToAllUnnamed, JVM_AddModuleExportsToAllUnnamed(JNIEnv *env, jobject from_module, jstring package))
1157 Handle h_from_module (THREAD, JNIHandles::resolve(from_module));
1158 Modules::add_module_exports_to_all_unnamed(h_from_module, package, CHECK);
1159 JVM_END
1160
1161 JVM_ENTRY_PROF(void, JVM_AddModuleExportsToAll, JVM_AddModuleExportsToAll(JNIEnv *env, jobject from_module, jstring package))
1162 Handle h_from_module (THREAD, JNIHandles::resolve(from_module));
1163 Modules::add_module_exports(h_from_module, package, Handle(), CHECK);
1164 JVM_END
1165
1166 JVM_ENTRY_PROF(void, JVM_AddReadsModule, JVM_AddReadsModule(JNIEnv *env, jobject from_module, jobject source_module))
1167 Handle h_from_module (THREAD, JNIHandles::resolve(from_module));
1168 Handle h_source_module (THREAD, JNIHandles::resolve(source_module));
1169 Modules::add_reads_module(h_from_module, h_source_module, CHECK);
1170 JVM_END
1171
1172 JVM_ENTRY_PROF(void, JVM_DefineArchivedModules, JVM_DefineArchivedModules(JNIEnv *env, jobject platform_loader, jobject system_loader))
1173 Handle h_platform_loader (THREAD, JNIHandles::resolve(platform_loader));
1174 Handle h_system_loader (THREAD, JNIHandles::resolve(system_loader));
1175 Modules::define_archived_modules(h_platform_loader, h_system_loader, CHECK);
1176 JVM_END
1177
1178 // Reflection support //////////////////////////////////////////////////////////////////////////////
1179
1180 JVM_ENTRY_PROF(jstring, JVM_InitClassName, JVM_InitClassName(JNIEnv *env, jclass cls))
1181 assert (cls != nullptr, "illegal class");
1182 JvmtiVMObjectAllocEventCollector oam;
1183 ResourceMark rm(THREAD);
1184 HandleMark hm(THREAD);
1185 Handle java_class(THREAD, JNIHandles::resolve(cls));
1186 oop result = java_lang_Class::name(java_class, CHECK_NULL);
1187 return (jstring) JNIHandles::make_local(THREAD, result);
1188 JVM_END
1189
1190
1191 JVM_ENTRY_PROF(jobjectArray, JVM_GetClassInterfaces, JVM_GetClassInterfaces(JNIEnv *env, jclass cls))
1192 JvmtiVMObjectAllocEventCollector oam;
1193 oop mirror = JNIHandles::resolve_non_null(cls);
1194
1195 // Special handling for primitive objects
1196 if (java_lang_Class::is_primitive(mirror)) {
1197 // Primitive objects does not have any interfaces
1198 objArrayOop r = oopFactory::new_objArray(vmClasses::Class_klass(), 0, CHECK_NULL);
1199 return (jobjectArray) JNIHandles::make_local(THREAD, r);
1200 }
1201
1202 Klass* klass = java_lang_Class::as_Klass(mirror);
1203 // Figure size of result array
1204 int size;
1205 if (klass->is_instance_klass()) {
1206 size = InstanceKlass::cast(klass)->local_interfaces()->length();
1207 } else {
1208 assert(klass->is_objArray_klass() || klass->is_typeArray_klass(), "Illegal mirror klass");
1209 size = 2;
1210 }
1211
1212 // Allocate result array
1213 objArrayOop r = oopFactory::new_objArray(vmClasses::Class_klass(), size, CHECK_NULL);
1214 objArrayHandle result (THREAD, r);
1215 // Fill in result
1216 if (klass->is_instance_klass()) {
1217 // Regular instance klass, fill in all local interfaces
1218 for (int index = 0; index < size; index++) {
1219 Klass* k = InstanceKlass::cast(klass)->local_interfaces()->at(index);
1220 result->obj_at_put(index, k->java_mirror());
1221 }
1222 } else {
1223 // All arrays implement java.lang.Cloneable and java.io.Serializable
1224 result->obj_at_put(0, vmClasses::Cloneable_klass()->java_mirror());
1225 result->obj_at_put(1, vmClasses::Serializable_klass()->java_mirror());
1226 }
1227 return (jobjectArray) JNIHandles::make_local(THREAD, result());
1228 JVM_END
1229
1230
1231 JVM_ENTRY_PROF(jboolean, JVM_IsInterface, JVM_IsInterface(JNIEnv *env, jclass cls))
1232 oop mirror = JNIHandles::resolve_non_null(cls);
1233 if (java_lang_Class::is_primitive(mirror)) {
1234 return JNI_FALSE;
1235 }
1236 Klass* k = java_lang_Class::as_Klass(mirror);
1237 jboolean result = k->is_interface();
1238 assert(!result || k->is_instance_klass(),
1239 "all interfaces are instance types");
1240 // The compiler intrinsic for isInterface tests the
1241 // Klass::_access_flags bits in the same way.
1242 return result;
1243 JVM_END
1244
1245 JVM_ENTRY_PROF(jboolean, JVM_IsHiddenClass, JVM_IsHiddenClass(JNIEnv *env, jclass cls))
1246 oop mirror = JNIHandles::resolve_non_null(cls);
1247 if (java_lang_Class::is_primitive(mirror)) {
1248 return JNI_FALSE;
1249 }
1250 Klass* k = java_lang_Class::as_Klass(mirror);
1251 return k->is_hidden();
1252 JVM_END
1253
1254 JVM_ENTRY_PROF(jobject, JVM_GetProtectionDomain, JVM_GetProtectionDomain(JNIEnv *env, jclass cls))
1255 oop mirror = JNIHandles::resolve_non_null(cls);
1256 if (mirror == nullptr) {
1257 THROW_(vmSymbols::java_lang_NullPointerException(), nullptr);
1258 }
1259
1260 if (java_lang_Class::is_primitive(mirror)) {
1261 // Primitive types does not have a protection domain.
1262 return nullptr;
1263 }
1264
1265 oop pd = java_lang_Class::protection_domain(mirror);
1266 return (jobject) JNIHandles::make_local(THREAD, pd);
1267 JVM_END
1268
1269
1270 // Returns the inherited_access_control_context field of the running thread.
1271 JVM_ENTRY_PROF(jobject, JVM_GetInheritedAccessControlContext, JVM_GetInheritedAccessControlContext(JNIEnv *env, jclass cls))
1272 oop result = java_lang_Thread::inherited_access_control_context(thread->threadObj());
1273 return JNIHandles::make_local(THREAD, result);
1274 JVM_END
1275
1276 JVM_ENTRY_PROF(jobject, JVM_GetStackAccessControlContext, JVM_GetStackAccessControlContext(JNIEnv *env, jclass cls))
1277 if (!UsePrivilegedStack) return nullptr;
1278
1279 ResourceMark rm(THREAD);
1280 GrowableArray<Handle>* local_array = new GrowableArray<Handle>(12);
1281 JvmtiVMObjectAllocEventCollector oam;
1282
1283 // count the protection domains on the execution stack. We collapse
1284 // duplicate consecutive protection domains into a single one, as
1285 // well as stopping when we hit a privileged frame.
1286
1287 oop previous_protection_domain = nullptr;
1288 Handle privileged_context(thread, nullptr);
1289 bool is_privileged = false;
1290 oop protection_domain = nullptr;
1291
1292 // Iterate through Java frames
1293 vframeStream vfst(thread);
1294 for(; !vfst.at_end(); vfst.next()) {
1295 // get method of frame
1296 Method* method = vfst.method();
1340 local_array->length(), CHECK_NULL);
1341 objArrayHandle h_context(thread, context);
1342 for (int index = 0; index < local_array->length(); index++) {
1343 h_context->obj_at_put(index, local_array->at(index)());
1344 }
1345
1346 oop result = java_security_AccessControlContext::create(h_context, is_privileged, privileged_context, CHECK_NULL);
1347
1348 return JNIHandles::make_local(THREAD, result);
1349 JVM_END
1350
1351 class ScopedValueBindingsResolver {
1352 public:
1353 InstanceKlass* Carrier_klass;
1354 ScopedValueBindingsResolver(JavaThread* THREAD) {
1355 Klass *k = SystemDictionary::resolve_or_fail(vmSymbols::java_lang_ScopedValue_Carrier(), true, THREAD);
1356 Carrier_klass = InstanceKlass::cast(k);
1357 }
1358 };
1359
1360 JVM_ENTRY_PROF(jobject, JVM_FindScopedValueBindings, JVM_FindScopedValueBindings(JNIEnv *env, jclass cls))
1361 ResourceMark rm(THREAD);
1362 GrowableArray<Handle>* local_array = new GrowableArray<Handle>(12);
1363 JvmtiVMObjectAllocEventCollector oam;
1364
1365 static ScopedValueBindingsResolver resolver(THREAD);
1366
1367 // Iterate through Java frames
1368 vframeStream vfst(thread);
1369 for(; !vfst.at_end(); vfst.next()) {
1370 int loc = -1;
1371 // get method of frame
1372 Method* method = vfst.method();
1373
1374 Symbol *name = method->name();
1375
1376 InstanceKlass* holder = method->method_holder();
1377 if (name == vmSymbols::runWith_method_name()) {
1378 if (holder == vmClasses::Thread_klass()
1379 || holder == resolver.Carrier_klass) {
1380 loc = 1;
1381 }
1382 }
1383
1384 if (loc != -1) {
1385 javaVFrame *frame = vfst.asJavaVFrame();
1386 StackValueCollection* locals = frame->locals();
1387 StackValue* head_sv = locals->at(loc); // java/lang/ScopedValue$Snapshot
1388 Handle result = head_sv->get_obj();
1389 assert(!head_sv->obj_is_scalar_replaced(), "found scalar-replaced object");
1390 if (result() != nullptr) {
1391 return JNIHandles::make_local(THREAD, result());
1392 }
1393 }
1394 }
1395
1396 return nullptr;
1397 JVM_END
1398
1399 JVM_ENTRY_PROF(jboolean, JVM_IsArrayClass, JVM_IsArrayClass(JNIEnv *env, jclass cls))
1400 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls));
1401 return (k != nullptr) && k->is_array_klass() ? true : false;
1402 JVM_END
1403
1404
1405 JVM_ENTRY_PROF(jboolean, JVM_IsPrimitiveClass, JVM_IsPrimitiveClass(JNIEnv *env, jclass cls))
1406 oop mirror = JNIHandles::resolve_non_null(cls);
1407 return (jboolean) java_lang_Class::is_primitive(mirror);
1408 JVM_END
1409
1410
1411 JVM_ENTRY_PROF(jint, JVM_GetClassModifiers, JVM_GetClassModifiers(JNIEnv *env, jclass cls))
1412 oop mirror = JNIHandles::resolve_non_null(cls);
1413 if (java_lang_Class::is_primitive(mirror)) {
1414 // Primitive type
1415 return JVM_ACC_ABSTRACT | JVM_ACC_FINAL | JVM_ACC_PUBLIC;
1416 }
1417
1418 Klass* k = java_lang_Class::as_Klass(mirror);
1419 debug_only(int computed_modifiers = k->compute_modifier_flags());
1420 assert(k->modifier_flags() == computed_modifiers, "modifiers cache is OK");
1421 return k->modifier_flags();
1422 JVM_END
1423
1424
1425 // Inner class reflection ///////////////////////////////////////////////////////////////////////////////
1426
1427 JVM_ENTRY_PROF(jobjectArray, JVM_GetDeclaredClasses, JVM_GetDeclaredClasses(JNIEnv *env, jclass ofClass))
1428 JvmtiVMObjectAllocEventCollector oam;
1429 // ofClass is a reference to a java_lang_Class object. The mirror object
1430 // of an InstanceKlass
1431 oop ofMirror = JNIHandles::resolve_non_null(ofClass);
1432 if (java_lang_Class::is_primitive(ofMirror) ||
1433 ! java_lang_Class::as_Klass(ofMirror)->is_instance_klass()) {
1434 oop result = oopFactory::new_objArray(vmClasses::Class_klass(), 0, CHECK_NULL);
1435 return (jobjectArray)JNIHandles::make_local(THREAD, result);
1436 }
1437
1438 InstanceKlass* k = InstanceKlass::cast(java_lang_Class::as_Klass(ofMirror));
1439 InnerClassesIterator iter(k);
1440
1441 if (iter.length() == 0) {
1442 // Neither an inner nor outer class
1443 oop result = oopFactory::new_objArray(vmClasses::Class_klass(), 0, CHECK_NULL);
1444 return (jobjectArray)JNIHandles::make_local(THREAD, result);
1445 }
1446
1447 // find inner class info
1473 result->obj_at_put(members, inner_klass->java_mirror());
1474 members++;
1475 }
1476 }
1477 }
1478 }
1479
1480 if (members != length) {
1481 // Return array of right length
1482 objArrayOop res = oopFactory::new_objArray(vmClasses::Class_klass(), members, CHECK_NULL);
1483 for(int i = 0; i < members; i++) {
1484 res->obj_at_put(i, result->obj_at(i));
1485 }
1486 return (jobjectArray)JNIHandles::make_local(THREAD, res);
1487 }
1488
1489 return (jobjectArray)JNIHandles::make_local(THREAD, result());
1490 JVM_END
1491
1492
1493 JVM_ENTRY_PROF(jclass, JVM_GetDeclaringClass, JVM_GetDeclaringClass(JNIEnv *env, jclass ofClass))
1494 {
1495 // ofClass is a reference to a java_lang_Class object.
1496 oop ofMirror = JNIHandles::resolve_non_null(ofClass);
1497 if (java_lang_Class::is_primitive(ofMirror)) {
1498 return nullptr;
1499 }
1500 Klass* klass = java_lang_Class::as_Klass(ofMirror);
1501 if (!klass->is_instance_klass()) {
1502 return nullptr;
1503 }
1504
1505 bool inner_is_member = false;
1506 Klass* outer_klass
1507 = InstanceKlass::cast(klass)->compute_enclosing_class(&inner_is_member, CHECK_NULL);
1508 if (outer_klass == nullptr) return nullptr; // already a top-level class
1509 if (!inner_is_member) return nullptr; // a hidden class (inside a method)
1510 return (jclass) JNIHandles::make_local(THREAD, outer_klass->java_mirror());
1511 }
1512 JVM_END
1513
1514 JVM_ENTRY_PROF(jstring, JVM_GetSimpleBinaryName, JVM_GetSimpleBinaryName(JNIEnv *env, jclass cls))
1515 {
1516 oop mirror = JNIHandles::resolve_non_null(cls);
1517 if (java_lang_Class::is_primitive(mirror)) {
1518 return nullptr;
1519 }
1520 Klass* klass = java_lang_Class::as_Klass(mirror);
1521 if (!klass->is_instance_klass()) {
1522 return nullptr;
1523 }
1524 InstanceKlass* k = InstanceKlass::cast(klass);
1525 int ooff = 0, noff = 0;
1526 if (k->find_inner_classes_attr(&ooff, &noff, THREAD)) {
1527 if (noff != 0) {
1528 constantPoolHandle i_cp(thread, k->constants());
1529 Symbol* name = i_cp->symbol_at(noff);
1530 Handle str = java_lang_String::create_from_symbol(name, CHECK_NULL);
1531 return (jstring) JNIHandles::make_local(THREAD, str());
1532 }
1533 }
1534 return nullptr;
1535 }
1536 JVM_END
1537
1538 JVM_ENTRY_PROF(jstring, JVM_GetClassSignature, JVM_GetClassSignature(JNIEnv *env, jclass cls))
1539 assert (cls != nullptr, "illegal class");
1540 JvmtiVMObjectAllocEventCollector oam;
1541 ResourceMark rm(THREAD);
1542 oop mirror = JNIHandles::resolve_non_null(cls);
1543 // Return null for arrays and primitives
1544 if (!java_lang_Class::is_primitive(mirror)) {
1545 Klass* k = java_lang_Class::as_Klass(mirror);
1546 if (k->is_instance_klass()) {
1547 Symbol* sym = InstanceKlass::cast(k)->generic_signature();
1548 if (sym == nullptr) return nullptr;
1549 Handle str = java_lang_String::create_from_symbol(sym, CHECK_NULL);
1550 return (jstring) JNIHandles::make_local(THREAD, str());
1551 }
1552 }
1553 return nullptr;
1554 JVM_END
1555
1556
1557 JVM_ENTRY_PROF(jbyteArray, JVM_GetClassAnnotations, JVM_GetClassAnnotations(JNIEnv *env, jclass cls))
1558 assert (cls != nullptr, "illegal class");
1559 oop mirror = JNIHandles::resolve_non_null(cls);
1560 // Return null for arrays and primitives
1561 if (!java_lang_Class::is_primitive(mirror)) {
1562 Klass* k = java_lang_Class::as_Klass(mirror);
1563 if (k->is_instance_klass()) {
1564 typeArrayOop a = Annotations::make_java_array(InstanceKlass::cast(k)->class_annotations(), CHECK_NULL);
1565 return (jbyteArray) JNIHandles::make_local(THREAD, a);
1566 }
1567 }
1568 return nullptr;
1569 JVM_END
1570
1571
1572 static bool jvm_get_field_common(jobject field, fieldDescriptor& fd) {
1573 // some of this code was adapted from from jni_FromReflectedField
1574
1575 oop reflected = JNIHandles::resolve_non_null(field);
1576 oop mirror = java_lang_reflect_Field::clazz(reflected);
1577 Klass* k = java_lang_Class::as_Klass(mirror);
1606 int slot = 0;
1607
1608 if (reflected->klass() == vmClasses::reflect_Constructor_klass()) {
1609 mirror = java_lang_reflect_Constructor::clazz(reflected);
1610 slot = java_lang_reflect_Constructor::slot(reflected);
1611 } else {
1612 assert(reflected->klass() == vmClasses::reflect_Method_klass(),
1613 "wrong type");
1614 mirror = java_lang_reflect_Method::clazz(reflected);
1615 slot = java_lang_reflect_Method::slot(reflected);
1616 }
1617 Klass* k = java_lang_Class::as_Klass(mirror);
1618
1619 Method* m = InstanceKlass::cast(k)->method_with_idnum(slot);
1620 assert(m != nullptr, "cannot find method");
1621 return m; // caller has to deal with null in product mode
1622 }
1623
1624 /* Type use annotations support (JDK 1.8) */
1625
1626 JVM_ENTRY_PROF(jbyteArray, JVM_GetClassTypeAnnotations, JVM_GetClassTypeAnnotations(JNIEnv *env, jclass cls))
1627 assert (cls != nullptr, "illegal class");
1628 ResourceMark rm(THREAD);
1629 // Return null for arrays and primitives
1630 if (!java_lang_Class::is_primitive(JNIHandles::resolve(cls))) {
1631 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve(cls));
1632 if (k->is_instance_klass()) {
1633 AnnotationArray* type_annotations = InstanceKlass::cast(k)->class_type_annotations();
1634 if (type_annotations != nullptr) {
1635 typeArrayOop a = Annotations::make_java_array(type_annotations, CHECK_NULL);
1636 return (jbyteArray) JNIHandles::make_local(THREAD, a);
1637 }
1638 }
1639 }
1640 return nullptr;
1641 JVM_END
1642
1643 JVM_ENTRY_PROF(jbyteArray, JVM_GetMethodTypeAnnotations, JVM_GetMethodTypeAnnotations(JNIEnv *env, jobject method))
1644 assert (method != nullptr, "illegal method");
1645 // method is a handle to a java.lang.reflect.Method object
1646 Method* m = jvm_get_method_common(method);
1647 if (m == nullptr) {
1648 return nullptr;
1649 }
1650
1651 AnnotationArray* type_annotations = m->type_annotations();
1652 if (type_annotations != nullptr) {
1653 typeArrayOop a = Annotations::make_java_array(type_annotations, CHECK_NULL);
1654 return (jbyteArray) JNIHandles::make_local(THREAD, a);
1655 }
1656
1657 return nullptr;
1658 JVM_END
1659
1660 JVM_ENTRY_PROF(jbyteArray, JVM_GetFieldTypeAnnotations, JVM_GetFieldTypeAnnotations(JNIEnv *env, jobject field))
1661 assert (field != nullptr, "illegal field");
1662 fieldDescriptor fd;
1663 bool gotFd = jvm_get_field_common(field, fd);
1664 if (!gotFd) {
1665 return nullptr;
1666 }
1667
1668 return (jbyteArray) JNIHandles::make_local(THREAD, Annotations::make_java_array(fd.type_annotations(), THREAD));
1669 JVM_END
1670
1671 static void bounds_check(const constantPoolHandle& cp, jint index, TRAPS) {
1672 if (!cp->is_within_bounds(index)) {
1673 THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), "Constant pool index out of bounds");
1674 }
1675 }
1676
1677 JVM_ENTRY_PROF(jobjectArray, JVM_GetMethodParameters, JVM_GetMethodParameters(JNIEnv *env, jobject method))
1678 {
1679 // method is a handle to a java.lang.reflect.Method object
1680 Method* method_ptr = jvm_get_method_common(method);
1681 methodHandle mh (THREAD, method_ptr);
1682 Handle reflected_method (THREAD, JNIHandles::resolve_non_null(method));
1683 const int num_params = mh->method_parameters_length();
1684
1685 if (num_params < 0) {
1686 // A -1 return value from method_parameters_length means there is no
1687 // parameter data. Return null to indicate this to the reflection
1688 // API.
1689 assert(num_params == -1, "num_params should be -1 if it is less than zero");
1690 return (jobjectArray)nullptr;
1691 } else {
1692 // Otherwise, we return something up to reflection, even if it is
1693 // a zero-length array. Why? Because in some cases this can
1694 // trigger a MalformedParametersException.
1695
1696 // make sure all the symbols are properly formatted
1697 for (int i = 0; i < num_params; i++) {
1710 objArrayOop result_oop = oopFactory::new_objArray(vmClasses::reflect_Parameter_klass(), num_params, CHECK_NULL);
1711 objArrayHandle result (THREAD, result_oop);
1712
1713 for (int i = 0; i < num_params; i++) {
1714 MethodParametersElement* params = mh->method_parameters_start();
1715 // For a 0 index, give a null symbol
1716 Symbol* sym = 0 != params[i].name_cp_index ?
1717 mh->constants()->symbol_at(params[i].name_cp_index) : nullptr;
1718 int flags = params[i].flags;
1719 oop param = Reflection::new_parameter(reflected_method, i, sym,
1720 flags, CHECK_NULL);
1721 result->obj_at_put(i, param);
1722 }
1723 return (jobjectArray)JNIHandles::make_local(THREAD, result());
1724 }
1725 }
1726 JVM_END
1727
1728 // New (JDK 1.4) reflection implementation /////////////////////////////////////
1729
1730 JVM_ENTRY_PROF(jobjectArray, JVM_GetClassDeclaredFields, JVM_GetClassDeclaredFields(JNIEnv *env, jclass ofClass, jboolean publicOnly))
1731 {
1732 JvmtiVMObjectAllocEventCollector oam;
1733
1734 oop ofMirror = JNIHandles::resolve_non_null(ofClass);
1735 // Exclude primitive types and array types
1736 if (java_lang_Class::is_primitive(ofMirror) ||
1737 java_lang_Class::as_Klass(ofMirror)->is_array_klass()) {
1738 // Return empty array
1739 oop res = oopFactory::new_objArray(vmClasses::reflect_Field_klass(), 0, CHECK_NULL);
1740 return (jobjectArray) JNIHandles::make_local(THREAD, res);
1741 }
1742
1743 InstanceKlass* k = InstanceKlass::cast(java_lang_Class::as_Klass(ofMirror));
1744 constantPoolHandle cp(THREAD, k->constants());
1745
1746 // Ensure class is linked
1747 k->link_class(CHECK_NULL);
1748
1749 // Allocate result
1750 int num_fields;
1761 objArrayOop r = oopFactory::new_objArray(vmClasses::reflect_Field_klass(), num_fields, CHECK_NULL);
1762 objArrayHandle result (THREAD, r);
1763
1764 int out_idx = 0;
1765 fieldDescriptor fd;
1766 for (JavaFieldStream fs(k); !fs.done(); fs.next()) {
1767 if (!publicOnly || fs.access_flags().is_public()) {
1768 fd.reinitialize(k, fs.index());
1769 oop field = Reflection::new_field(&fd, CHECK_NULL);
1770 result->obj_at_put(out_idx, field);
1771 ++out_idx;
1772 }
1773 }
1774 assert(out_idx == num_fields, "just checking");
1775 return (jobjectArray) JNIHandles::make_local(THREAD, result());
1776 }
1777 JVM_END
1778
1779 // A class is a record if and only if it is final and a direct subclass of
1780 // java.lang.Record and has a Record attribute; otherwise, it is not a record.
1781 JVM_ENTRY_PROF(jboolean, JVM_IsRecord, JVM_IsRecord(JNIEnv *env, jclass cls))
1782 {
1783 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls));
1784 if (k != nullptr && k->is_instance_klass()) {
1785 InstanceKlass* ik = InstanceKlass::cast(k);
1786 return ik->is_record();
1787 } else {
1788 return false;
1789 }
1790 }
1791 JVM_END
1792
1793 // Returns an array containing the components of the Record attribute,
1794 // or null if the attribute is not present.
1795 //
1796 // Note that this function returns the components of the Record attribute
1797 // even if the class is not a record.
1798 JVM_ENTRY_PROF(jobjectArray, JVM_GetRecordComponents, JVM_GetRecordComponents(JNIEnv* env, jclass ofClass))
1799 {
1800 Klass* c = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(ofClass));
1801 assert(c->is_instance_klass(), "must be");
1802 InstanceKlass* ik = InstanceKlass::cast(c);
1803
1804 Array<RecordComponent*>* components = ik->record_components();
1805 if (components != nullptr) {
1806 JvmtiVMObjectAllocEventCollector oam;
1807 constantPoolHandle cp(THREAD, ik->constants());
1808 int length = components->length();
1809 assert(length >= 0, "unexpected record_components length");
1810 objArrayOop record_components =
1811 oopFactory::new_objArray(vmClasses::RecordComponent_klass(), length, CHECK_NULL);
1812 objArrayHandle components_h (THREAD, record_components);
1813
1814 for (int x = 0; x < length; x++) {
1815 RecordComponent* component = components->at(x);
1816 assert(component != nullptr, "unexpected null record component");
1817 oop component_oop = java_lang_reflect_RecordComponent::create(ik, component, CHECK_NULL);
1818 components_h->obj_at_put(x, component_oop);
1885 for (int i = 0; i < num_methods; i++) {
1886 methodHandle method(THREAD, k->method_with_idnum(idnums->at(i)));
1887 if (method.is_null()) {
1888 // Method may have been deleted and seems this API can handle null
1889 // Otherwise should probably put a method that throws NSME
1890 result->obj_at_put(i, nullptr);
1891 } else {
1892 oop m;
1893 if (want_constructor) {
1894 m = Reflection::new_constructor(method, CHECK_NULL);
1895 } else {
1896 m = Reflection::new_method(method, false, CHECK_NULL);
1897 }
1898 result->obj_at_put(i, m);
1899 }
1900 }
1901
1902 return (jobjectArray) JNIHandles::make_local(THREAD, result());
1903 }
1904
1905 JVM_ENTRY_PROF(jobjectArray, JVM_GetClassDeclaredMethods, JVM_GetClassDeclaredMethods(JNIEnv *env, jclass ofClass, jboolean publicOnly))
1906 {
1907 return get_class_declared_methods_helper(env, ofClass, publicOnly,
1908 /*want_constructor*/ false,
1909 vmClasses::reflect_Method_klass(), THREAD);
1910 }
1911 JVM_END
1912
1913 JVM_ENTRY_PROF(jobjectArray, JVM_GetClassDeclaredConstructors, JVM_GetClassDeclaredConstructors(JNIEnv *env, jclass ofClass, jboolean publicOnly))
1914 {
1915 return get_class_declared_methods_helper(env, ofClass, publicOnly,
1916 /*want_constructor*/ true,
1917 vmClasses::reflect_Constructor_klass(), THREAD);
1918 }
1919 JVM_END
1920
1921 JVM_ENTRY_PROF(jint, JVM_GetClassAccessFlags, JVM_GetClassAccessFlags(JNIEnv *env, jclass cls))
1922 {
1923 oop mirror = JNIHandles::resolve_non_null(cls);
1924 if (java_lang_Class::is_primitive(mirror)) {
1925 // Primitive type
1926 return JVM_ACC_ABSTRACT | JVM_ACC_FINAL | JVM_ACC_PUBLIC;
1927 }
1928
1929 Klass* k = java_lang_Class::as_Klass(mirror);
1930 return k->access_flags().as_int() & JVM_ACC_WRITTEN_FLAGS;
1931 }
1932 JVM_END
1933
1934 JVM_ENTRY_PROF(jboolean, JVM_AreNestMates, JVM_AreNestMates(JNIEnv *env, jclass current, jclass member))
1935 {
1936 Klass* c = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(current));
1937 assert(c->is_instance_klass(), "must be");
1938 InstanceKlass* ck = InstanceKlass::cast(c);
1939 Klass* m = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(member));
1940 assert(m->is_instance_klass(), "must be");
1941 InstanceKlass* mk = InstanceKlass::cast(m);
1942 return ck->has_nestmate_access_to(mk, THREAD);
1943 }
1944 JVM_END
1945
1946 JVM_ENTRY_PROF(jclass, JVM_GetNestHost, JVM_GetNestHost(JNIEnv* env, jclass current))
1947 {
1948 // current is not a primitive or array class
1949 Klass* c = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(current));
1950 assert(c->is_instance_klass(), "must be");
1951 InstanceKlass* ck = InstanceKlass::cast(c);
1952 InstanceKlass* host = ck->nest_host(THREAD);
1953 return (jclass) (host == nullptr ? nullptr :
1954 JNIHandles::make_local(THREAD, host->java_mirror()));
1955 }
1956 JVM_END
1957
1958 JVM_ENTRY_PROF(jobjectArray, JVM_GetNestMembers, JVM_GetNestMembers(JNIEnv* env, jclass current))
1959 {
1960 // current is not a primitive or array class
1961 ResourceMark rm(THREAD);
1962 Klass* c = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(current));
1963 assert(c->is_instance_klass(), "must be");
1964 InstanceKlass* ck = InstanceKlass::cast(c);
1965 InstanceKlass* host = ck->nest_host(THREAD);
1966
1967 log_trace(class, nestmates)("Calling GetNestMembers for type %s with nest-host %s",
1968 ck->external_name(), host->external_name());
1969 {
1970 JvmtiVMObjectAllocEventCollector oam;
1971 Array<u2>* members = host->nest_members();
1972 int length = members == nullptr ? 0 : members->length();
1973
1974 log_trace(class, nestmates)(" - host has %d listed nest members", length);
1975
1976 // nest host is first in the array so make it one bigger
1977 objArrayOop r = oopFactory::new_objArray(vmClasses::Class_klass(),
1978 length + 1, CHECK_NULL);
2018 log_trace(class, nestmates)(" - compacting array from length %d to %d",
2019 length + 1, count + 1);
2020
2021 objArrayOop r2 = oopFactory::new_objArray(vmClasses::Class_klass(),
2022 count + 1, CHECK_NULL);
2023 objArrayHandle result2(THREAD, r2);
2024 for (int i = 0; i < count + 1; i++) {
2025 result2->obj_at_put(i, result->obj_at(i));
2026 }
2027 return (jobjectArray)JNIHandles::make_local(THREAD, result2());
2028 }
2029 }
2030 else {
2031 assert(host == ck || ck->is_hidden(), "must be singleton nest or dynamic nestmate");
2032 }
2033 return (jobjectArray)JNIHandles::make_local(THREAD, result());
2034 }
2035 }
2036 JVM_END
2037
2038 JVM_ENTRY_PROF(jobjectArray, JVM_GetPermittedSubclasses, JVM_GetPermittedSubclasses(JNIEnv* env, jclass current))
2039 {
2040 oop mirror = JNIHandles::resolve_non_null(current);
2041 assert(!java_lang_Class::is_primitive(mirror), "should not be");
2042 Klass* c = java_lang_Class::as_Klass(mirror);
2043 assert(c->is_instance_klass(), "must be");
2044 InstanceKlass* ik = InstanceKlass::cast(c);
2045 ResourceMark rm(THREAD);
2046 log_trace(class, sealed)("Calling GetPermittedSubclasses for %s type %s",
2047 ik->is_sealed() ? "sealed" : "non-sealed", ik->external_name());
2048 if (ik->is_sealed()) {
2049 JvmtiVMObjectAllocEventCollector oam;
2050 Array<u2>* subclasses = ik->permitted_subclasses();
2051 int length = subclasses->length();
2052
2053 log_trace(class, sealed)(" - sealed class has %d permitted subclasses", length);
2054
2055 objArrayOop r = oopFactory::new_objArray(vmClasses::Class_klass(),
2056 length, CHECK_NULL);
2057 objArrayHandle result(THREAD, r);
2058 int count = 0;
2081 }
2082 if (count < length) {
2083 // we had invalid entries so we need to compact the array
2084 objArrayOop r2 = oopFactory::new_objArray(vmClasses::Class_klass(),
2085 count, CHECK_NULL);
2086 objArrayHandle result2(THREAD, r2);
2087 for (int i = 0; i < count; i++) {
2088 result2->obj_at_put(i, result->obj_at(i));
2089 }
2090 return (jobjectArray)JNIHandles::make_local(THREAD, result2());
2091 }
2092 return (jobjectArray)JNIHandles::make_local(THREAD, result());
2093 } else {
2094 return nullptr;
2095 }
2096 }
2097 JVM_END
2098
2099 // Constant pool access //////////////////////////////////////////////////////////
2100
2101 JVM_ENTRY_PROF(jobject, JVM_GetClassConstantPool, JVM_GetClassConstantPool(JNIEnv *env, jclass cls))
2102 {
2103 JvmtiVMObjectAllocEventCollector oam;
2104 oop mirror = JNIHandles::resolve_non_null(cls);
2105 // Return null for primitives and arrays
2106 if (!java_lang_Class::is_primitive(mirror)) {
2107 Klass* k = java_lang_Class::as_Klass(mirror);
2108 if (k->is_instance_klass()) {
2109 InstanceKlass* k_h = InstanceKlass::cast(k);
2110 Handle jcp = reflect_ConstantPool::create(CHECK_NULL);
2111 reflect_ConstantPool::set_cp(jcp(), k_h->constants());
2112 return JNIHandles::make_local(THREAD, jcp());
2113 }
2114 }
2115 return nullptr;
2116 }
2117 JVM_END
2118
2119
2120 JVM_ENTRY_PROF(jint, JVM_ConstantPoolGetSize, JVM_ConstantPoolGetSize(JNIEnv *env, jobject obj, jobject unused))
2121 {
2122 constantPoolHandle cp = constantPoolHandle(THREAD, reflect_ConstantPool::get_cp(JNIHandles::resolve_non_null(obj)));
2123 return cp->length();
2124 }
2125 JVM_END
2126
2127
2128 JVM_ENTRY_PROF(jclass, JVM_ConstantPoolGetClassAt, JVM_ConstantPoolGetClassAt(JNIEnv *env, jobject obj, jobject unused, jint index))
2129 {
2130 constantPoolHandle cp = constantPoolHandle(THREAD, reflect_ConstantPool::get_cp(JNIHandles::resolve_non_null(obj)));
2131 bounds_check(cp, index, CHECK_NULL);
2132 constantTag tag = cp->tag_at(index);
2133 if (!tag.is_klass() && !tag.is_unresolved_klass()) {
2134 THROW_MSG_NULL(vmSymbols::java_lang_IllegalArgumentException(), "Wrong type at constant pool index");
2135 }
2136 Klass* k = cp->klass_at(index, CHECK_NULL);
2137 return (jclass) JNIHandles::make_local(THREAD, k->java_mirror());
2138 }
2139 JVM_END
2140
2141 JVM_ENTRY_PROF(jclass, JVM_ConstantPoolGetClassAtIfLoaded, JVM_ConstantPoolGetClassAtIfLoaded(JNIEnv *env, jobject obj, jobject unused, jint index))
2142 {
2143 constantPoolHandle cp = constantPoolHandle(THREAD, reflect_ConstantPool::get_cp(JNIHandles::resolve_non_null(obj)));
2144 bounds_check(cp, index, CHECK_NULL);
2145 constantTag tag = cp->tag_at(index);
2146 if (!tag.is_klass() && !tag.is_unresolved_klass()) {
2147 THROW_MSG_NULL(vmSymbols::java_lang_IllegalArgumentException(), "Wrong type at constant pool index");
2148 }
2149 Klass* k = ConstantPool::klass_at_if_loaded(cp, index);
2150 if (k == nullptr) return nullptr;
2151 return (jclass) JNIHandles::make_local(THREAD, k->java_mirror());
2152 }
2153 JVM_END
2154
2155 static jobject get_method_at_helper(const constantPoolHandle& cp, jint index, bool force_resolution, TRAPS) {
2156 constantTag tag = cp->tag_at(index);
2157 if (!tag.is_method() && !tag.is_interface_method()) {
2158 THROW_MSG_NULL(vmSymbols::java_lang_IllegalArgumentException(), "Wrong type at constant pool index");
2159 }
2160 int klass_ref = cp->uncached_klass_ref_index_at(index);
2161 Klass* k_o;
2165 k_o = ConstantPool::klass_at_if_loaded(cp, klass_ref);
2166 if (k_o == nullptr) return nullptr;
2167 }
2168 InstanceKlass* k = InstanceKlass::cast(k_o);
2169 Symbol* name = cp->uncached_name_ref_at(index);
2170 Symbol* sig = cp->uncached_signature_ref_at(index);
2171 methodHandle m (THREAD, k->find_method(name, sig));
2172 if (m.is_null()) {
2173 THROW_MSG_NULL(vmSymbols::java_lang_RuntimeException(), "Unable to look up method in target class");
2174 }
2175 oop method;
2176 if (m->is_object_initializer()) {
2177 method = Reflection::new_constructor(m, CHECK_NULL);
2178 } else {
2179 // new_method accepts <clinit> as Method here
2180 method = Reflection::new_method(m, true, CHECK_NULL);
2181 }
2182 return JNIHandles::make_local(THREAD, method);
2183 }
2184
2185 JVM_ENTRY_PROF(jobject, JVM_ConstantPoolGetMethodAt, JVM_ConstantPoolGetMethodAt(JNIEnv *env, jobject obj, jobject unused, jint index))
2186 {
2187 JvmtiVMObjectAllocEventCollector oam;
2188 constantPoolHandle cp = constantPoolHandle(THREAD, reflect_ConstantPool::get_cp(JNIHandles::resolve_non_null(obj)));
2189 bounds_check(cp, index, CHECK_NULL);
2190 jobject res = get_method_at_helper(cp, index, true, CHECK_NULL);
2191 return res;
2192 }
2193 JVM_END
2194
2195 JVM_ENTRY_PROF(jobject, JVM_ConstantPoolGetMethodAtIfLoaded, JVM_ConstantPoolGetMethodAtIfLoaded(JNIEnv *env, jobject obj, jobject unused, jint index))
2196 {
2197 JvmtiVMObjectAllocEventCollector oam;
2198 constantPoolHandle cp = constantPoolHandle(THREAD, reflect_ConstantPool::get_cp(JNIHandles::resolve_non_null(obj)));
2199 bounds_check(cp, index, CHECK_NULL);
2200 jobject res = get_method_at_helper(cp, index, false, CHECK_NULL);
2201 return res;
2202 }
2203 JVM_END
2204
2205 static jobject get_field_at_helper(constantPoolHandle cp, jint index, bool force_resolution, TRAPS) {
2206 constantTag tag = cp->tag_at(index);
2207 if (!tag.is_field()) {
2208 THROW_MSG_NULL(vmSymbols::java_lang_IllegalArgumentException(), "Wrong type at constant pool index");
2209 }
2210 int klass_ref = cp->uncached_klass_ref_index_at(index);
2211 Klass* k_o;
2212 if (force_resolution) {
2213 k_o = cp->klass_at(klass_ref, CHECK_NULL);
2214 } else {
2215 k_o = ConstantPool::klass_at_if_loaded(cp, klass_ref);
2216 if (k_o == nullptr) return nullptr;
2217 }
2218 InstanceKlass* k = InstanceKlass::cast(k_o);
2219 Symbol* name = cp->uncached_name_ref_at(index);
2220 Symbol* sig = cp->uncached_signature_ref_at(index);
2221 fieldDescriptor fd;
2222 Klass* target_klass = k->find_field(name, sig, &fd);
2223 if (target_klass == nullptr) {
2224 THROW_MSG_NULL(vmSymbols::java_lang_RuntimeException(), "Unable to look up field in target class");
2225 }
2226 oop field = Reflection::new_field(&fd, CHECK_NULL);
2227 return JNIHandles::make_local(THREAD, field);
2228 }
2229
2230 JVM_ENTRY_PROF(jobject, JVM_ConstantPoolGetFieldAt, JVM_ConstantPoolGetFieldAt(JNIEnv *env, jobject obj, jobject unusedl, jint index))
2231 {
2232 JvmtiVMObjectAllocEventCollector oam;
2233 constantPoolHandle cp = constantPoolHandle(THREAD, reflect_ConstantPool::get_cp(JNIHandles::resolve_non_null(obj)));
2234 bounds_check(cp, index, CHECK_NULL);
2235 jobject res = get_field_at_helper(cp, index, true, CHECK_NULL);
2236 return res;
2237 }
2238 JVM_END
2239
2240 JVM_ENTRY_PROF(jobject, JVM_ConstantPoolGetFieldAtIfLoaded, JVM_ConstantPoolGetFieldAtIfLoaded(JNIEnv *env, jobject obj, jobject unused, jint index))
2241 {
2242 JvmtiVMObjectAllocEventCollector oam;
2243 constantPoolHandle cp = constantPoolHandle(THREAD, reflect_ConstantPool::get_cp(JNIHandles::resolve_non_null(obj)));
2244 bounds_check(cp, index, CHECK_NULL);
2245 jobject res = get_field_at_helper(cp, index, false, CHECK_NULL);
2246 return res;
2247 }
2248 JVM_END
2249
2250 JVM_ENTRY_PROF(jobjectArray, JVM_ConstantPoolGetMemberRefInfoAt, JVM_ConstantPoolGetMemberRefInfoAt(JNIEnv *env, jobject obj, jobject unused, jint index))
2251 {
2252 JvmtiVMObjectAllocEventCollector oam;
2253 constantPoolHandle cp = constantPoolHandle(THREAD, reflect_ConstantPool::get_cp(JNIHandles::resolve_non_null(obj)));
2254 bounds_check(cp, index, CHECK_NULL);
2255 constantTag tag = cp->tag_at(index);
2256 if (!tag.is_field_or_method()) {
2257 THROW_MSG_NULL(vmSymbols::java_lang_IllegalArgumentException(), "Wrong type at constant pool index");
2258 }
2259 int klass_ref = cp->uncached_klass_ref_index_at(index);
2260 Symbol* klass_name = cp->klass_name_at(klass_ref);
2261 Symbol* member_name = cp->uncached_name_ref_at(index);
2262 Symbol* member_sig = cp->uncached_signature_ref_at(index);
2263 objArrayOop dest_o = oopFactory::new_objArray(vmClasses::String_klass(), 3, CHECK_NULL);
2264 objArrayHandle dest(THREAD, dest_o);
2265 Handle str = java_lang_String::create_from_symbol(klass_name, CHECK_NULL);
2266 dest->obj_at_put(0, str());
2267 str = java_lang_String::create_from_symbol(member_name, CHECK_NULL);
2268 dest->obj_at_put(1, str());
2269 str = java_lang_String::create_from_symbol(member_sig, CHECK_NULL);
2270 dest->obj_at_put(2, str());
2271 return (jobjectArray) JNIHandles::make_local(THREAD, dest());
2272 }
2273 JVM_END
2274
2275 JVM_ENTRY_PROF(jint, JVM_ConstantPoolGetClassRefIndexAt, JVM_ConstantPoolGetClassRefIndexAt(JNIEnv *env, jobject obj, jobject unused, jint index))
2276 {
2277 JvmtiVMObjectAllocEventCollector oam;
2278 constantPoolHandle cp(THREAD, reflect_ConstantPool::get_cp(JNIHandles::resolve_non_null(obj)));
2279 bounds_check(cp, index, CHECK_0);
2280 constantTag tag = cp->tag_at(index);
2281 if (!tag.is_field_or_method()) {
2282 THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(), "Wrong type at constant pool index");
2283 }
2284 return (jint) cp->uncached_klass_ref_index_at(index);
2285 }
2286 JVM_END
2287
2288 JVM_ENTRY_PROF(jint, JVM_ConstantPoolGetNameAndTypeRefIndexAt, JVM_ConstantPoolGetNameAndTypeRefIndexAt(JNIEnv *env, jobject obj, jobject unused, jint index))
2289 {
2290 JvmtiVMObjectAllocEventCollector oam;
2291 constantPoolHandle cp(THREAD, reflect_ConstantPool::get_cp(JNIHandles::resolve_non_null(obj)));
2292 bounds_check(cp, index, CHECK_0);
2293 constantTag tag = cp->tag_at(index);
2294 if (!tag.is_invoke_dynamic() && !tag.is_field_or_method()) {
2295 THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(), "Wrong type at constant pool index");
2296 }
2297 return (jint) cp->uncached_name_and_type_ref_index_at(index);
2298 }
2299 JVM_END
2300
2301 JVM_ENTRY_PROF(jobjectArray, JVM_ConstantPoolGetNameAndTypeRefInfoAt, JVM_ConstantPoolGetNameAndTypeRefInfoAt(JNIEnv *env, jobject obj, jobject unused, jint index))
2302 {
2303 JvmtiVMObjectAllocEventCollector oam;
2304 constantPoolHandle cp(THREAD, reflect_ConstantPool::get_cp(JNIHandles::resolve_non_null(obj)));
2305 bounds_check(cp, index, CHECK_NULL);
2306 constantTag tag = cp->tag_at(index);
2307 if (!tag.is_name_and_type()) {
2308 THROW_MSG_NULL(vmSymbols::java_lang_IllegalArgumentException(), "Wrong type at constant pool index");
2309 }
2310 Symbol* member_name = cp->symbol_at(cp->name_ref_index_at(index));
2311 Symbol* member_sig = cp->symbol_at(cp->signature_ref_index_at(index));
2312 objArrayOop dest_o = oopFactory::new_objArray(vmClasses::String_klass(), 2, CHECK_NULL);
2313 objArrayHandle dest(THREAD, dest_o);
2314 Handle str = java_lang_String::create_from_symbol(member_name, CHECK_NULL);
2315 dest->obj_at_put(0, str());
2316 str = java_lang_String::create_from_symbol(member_sig, CHECK_NULL);
2317 dest->obj_at_put(1, str());
2318 return (jobjectArray) JNIHandles::make_local(THREAD, dest());
2319 }
2320 JVM_END
2321
2322 JVM_ENTRY_PROF(jint, JVM_ConstantPoolGetIntAt, JVM_ConstantPoolGetIntAt(JNIEnv *env, jobject obj, jobject unused, jint index))
2323 {
2324 constantPoolHandle cp = constantPoolHandle(THREAD, reflect_ConstantPool::get_cp(JNIHandles::resolve_non_null(obj)));
2325 bounds_check(cp, index, CHECK_0);
2326 constantTag tag = cp->tag_at(index);
2327 if (!tag.is_int()) {
2328 THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(), "Wrong type at constant pool index");
2329 }
2330 return cp->int_at(index);
2331 }
2332 JVM_END
2333
2334 JVM_ENTRY_PROF(jlong, JVM_ConstantPoolGetLongAt, JVM_ConstantPoolGetLongAt(JNIEnv *env, jobject obj, jobject unused, jint index))
2335 {
2336 constantPoolHandle cp = constantPoolHandle(THREAD, reflect_ConstantPool::get_cp(JNIHandles::resolve_non_null(obj)));
2337 bounds_check(cp, index, CHECK_(0L));
2338 constantTag tag = cp->tag_at(index);
2339 if (!tag.is_long()) {
2340 THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(), "Wrong type at constant pool index");
2341 }
2342 return cp->long_at(index);
2343 }
2344 JVM_END
2345
2346 JVM_ENTRY_PROF(jfloat, JVM_ConstantPoolGetFloatAt, JVM_ConstantPoolGetFloatAt(JNIEnv *env, jobject obj, jobject unused, jint index))
2347 {
2348 constantPoolHandle cp = constantPoolHandle(THREAD, reflect_ConstantPool::get_cp(JNIHandles::resolve_non_null(obj)));
2349 bounds_check(cp, index, CHECK_(0.0f));
2350 constantTag tag = cp->tag_at(index);
2351 if (!tag.is_float()) {
2352 THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(), "Wrong type at constant pool index");
2353 }
2354 return cp->float_at(index);
2355 }
2356 JVM_END
2357
2358 JVM_ENTRY_PROF(jdouble, JVM_ConstantPoolGetDoubleAt, JVM_ConstantPoolGetDoubleAt(JNIEnv *env, jobject obj, jobject unused, jint index))
2359 {
2360 constantPoolHandle cp = constantPoolHandle(THREAD, reflect_ConstantPool::get_cp(JNIHandles::resolve_non_null(obj)));
2361 bounds_check(cp, index, CHECK_(0.0));
2362 constantTag tag = cp->tag_at(index);
2363 if (!tag.is_double()) {
2364 THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(), "Wrong type at constant pool index");
2365 }
2366 return cp->double_at(index);
2367 }
2368 JVM_END
2369
2370 JVM_ENTRY_PROF(jstring, JVM_ConstantPoolGetStringAt, JVM_ConstantPoolGetStringAt(JNIEnv *env, jobject obj, jobject unused, jint index))
2371 {
2372 constantPoolHandle cp = constantPoolHandle(THREAD, reflect_ConstantPool::get_cp(JNIHandles::resolve_non_null(obj)));
2373 bounds_check(cp, index, CHECK_NULL);
2374 constantTag tag = cp->tag_at(index);
2375 if (!tag.is_string()) {
2376 THROW_MSG_NULL(vmSymbols::java_lang_IllegalArgumentException(), "Wrong type at constant pool index");
2377 }
2378 oop str = cp->string_at(index, CHECK_NULL);
2379 return (jstring) JNIHandles::make_local(THREAD, str);
2380 }
2381 JVM_END
2382
2383 JVM_ENTRY_PROF(jstring, JVM_ConstantPoolGetUTF8At, JVM_ConstantPoolGetUTF8At(JNIEnv *env, jobject obj, jobject unused, jint index))
2384 {
2385 JvmtiVMObjectAllocEventCollector oam;
2386 constantPoolHandle cp = constantPoolHandle(THREAD, reflect_ConstantPool::get_cp(JNIHandles::resolve_non_null(obj)));
2387 bounds_check(cp, index, CHECK_NULL);
2388 constantTag tag = cp->tag_at(index);
2389 if (!tag.is_symbol()) {
2390 THROW_MSG_NULL(vmSymbols::java_lang_IllegalArgumentException(), "Wrong type at constant pool index");
2391 }
2392 Symbol* sym = cp->symbol_at(index);
2393 Handle str = java_lang_String::create_from_symbol(sym, CHECK_NULL);
2394 return (jstring) JNIHandles::make_local(THREAD, str());
2395 }
2396 JVM_END
2397
2398 JVM_ENTRY_PROF(jbyte, JVM_ConstantPoolGetTagAt, JVM_ConstantPoolGetTagAt(JNIEnv *env, jobject obj, jobject unused, jint index))
2399 {
2400 constantPoolHandle cp = constantPoolHandle(THREAD, reflect_ConstantPool::get_cp(JNIHandles::resolve_non_null(obj)));
2401 bounds_check(cp, index, CHECK_0);
2402 constantTag tag = cp->tag_at(index);
2403 jbyte result = tag.value();
2404 // If returned tag values are not from the JVM spec, e.g. tags from 100 to 105,
2405 // they are changed to the corresponding tags from the JVM spec, so that java code in
2406 // sun.reflect.ConstantPool will return only tags from the JVM spec, not internal ones.
2407 if (tag.is_klass_or_reference()) {
2408 result = JVM_CONSTANT_Class;
2409 } else if (tag.is_string_index()) {
2410 result = JVM_CONSTANT_String;
2411 } else if (tag.is_method_type_in_error()) {
2412 result = JVM_CONSTANT_MethodType;
2413 } else if (tag.is_method_handle_in_error()) {
2414 result = JVM_CONSTANT_MethodHandle;
2415 } else if (tag.is_dynamic_constant_in_error()) {
2416 result = JVM_CONSTANT_Dynamic;
2417 }
2418 return result;
2419 }
2420 JVM_END
2421
2422 // Assertion support. //////////////////////////////////////////////////////////
2423
2424 JVM_ENTRY_PROF(jboolean, JVM_DesiredAssertionStatus, JVM_DesiredAssertionStatus(JNIEnv *env, jclass unused, jclass cls))
2425 assert(cls != nullptr, "bad class");
2426
2427 oop r = JNIHandles::resolve(cls);
2428 assert(! java_lang_Class::is_primitive(r), "primitive classes not allowed");
2429 if (java_lang_Class::is_primitive(r)) return false;
2430
2431 Klass* k = java_lang_Class::as_Klass(r);
2432 assert(k->is_instance_klass(), "must be an instance klass");
2433 if (!k->is_instance_klass()) return false;
2434
2435 ResourceMark rm(THREAD);
2436 const char* name = k->name()->as_C_string();
2437 bool system_class = k->class_loader() == nullptr;
2438 return JavaAssertions::enabled(name, system_class);
2439
2440 JVM_END
2441
2442
2443 // Return a new AssertionStatusDirectives object with the fields filled in with
2444 // command-line assertion arguments (i.e., -ea, -da).
2445 JVM_ENTRY_PROF(jobject, JVM_AssertionStatusDirectives, JVM_AssertionStatusDirectives(JNIEnv *env, jclass unused))
2446 JvmtiVMObjectAllocEventCollector oam;
2447 oop asd = JavaAssertions::createAssertionStatusDirectives(CHECK_NULL);
2448 return JNIHandles::make_local(THREAD, asd);
2449 JVM_END
2450
2451 // Verification ////////////////////////////////////////////////////////////////////////////////
2452
2453 // Reflection for the verifier /////////////////////////////////////////////////////////////////
2454
2455 // RedefineClasses support: bug 6214132 caused verification to fail.
2456 // All functions from this section should call the jvmtiThreadSate function:
2457 // Klass* class_to_verify_considering_redefinition(Klass* klass).
2458 // The function returns a Klass* of the _scratch_class if the verifier
2459 // was invoked in the middle of the class redefinition.
2460 // Otherwise it returns its argument value which is the _the_class Klass*.
2461 // Please, refer to the description in the jvmtiThreadSate.hpp.
2462
2463 JVM_ENTRY_PROF(const char*, JVM_GetClassNameUTF, JVM_GetClassNameUTF(JNIEnv *env, jclass cls))
2464 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls));
2465 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
2466 return k->name()->as_utf8();
2467 JVM_END
2468
2469
2470 JVM_ENTRY_PROF(void, JVM_GetClassCPTypes, JVM_GetClassCPTypes(JNIEnv *env, jclass cls, unsigned char *types))
2471 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls));
2472 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
2473 // types will have length zero if this is not an InstanceKlass
2474 // (length is determined by call to JVM_GetClassCPEntriesCount)
2475 if (k->is_instance_klass()) {
2476 ConstantPool* cp = InstanceKlass::cast(k)->constants();
2477 for (int index = cp->length() - 1; index >= 0; index--) {
2478 constantTag tag = cp->tag_at(index);
2479 types[index] = (tag.is_unresolved_klass()) ? (unsigned char) JVM_CONSTANT_Class : tag.value();
2480 }
2481 }
2482 JVM_END
2483
2484
2485 JVM_ENTRY_PROF(jint, JVM_GetClassCPEntriesCount, JVM_GetClassCPEntriesCount(JNIEnv *env, jclass cls))
2486 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls));
2487 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
2488 return (!k->is_instance_klass()) ? 0 : InstanceKlass::cast(k)->constants()->length();
2489 JVM_END
2490
2491
2492 JVM_ENTRY_PROF(jint, JVM_GetClassFieldsCount, JVM_GetClassFieldsCount(JNIEnv *env, jclass cls))
2493 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls));
2494 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
2495 return (!k->is_instance_klass()) ? 0 : InstanceKlass::cast(k)->java_fields_count();
2496 JVM_END
2497
2498
2499 JVM_ENTRY_PROF(jint, JVM_GetClassMethodsCount, JVM_GetClassMethodsCount(JNIEnv *env, jclass cls))
2500 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls));
2501 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
2502 return (!k->is_instance_klass()) ? 0 : InstanceKlass::cast(k)->methods()->length();
2503 JVM_END
2504
2505
2506 // The following methods, used for the verifier, are never called with
2507 // array klasses, so a direct cast to InstanceKlass is safe.
2508 // Typically, these methods are called in a loop with bounds determined
2509 // by the results of JVM_GetClass{Fields,Methods}Count, which return
2510 // zero for arrays.
2511 JVM_ENTRY_PROF(void, JVM_GetMethodIxExceptionIndexes, JVM_GetMethodIxExceptionIndexes(JNIEnv *env, jclass cls, jint method_index, unsigned short *exceptions))
2512 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls));
2513 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
2514 Method* method = InstanceKlass::cast(k)->methods()->at(method_index);
2515 int length = method->checked_exceptions_length();
2516 if (length > 0) {
2517 CheckedExceptionElement* table= method->checked_exceptions_start();
2518 for (int i = 0; i < length; i++) {
2519 exceptions[i] = table[i].class_cp_index;
2520 }
2521 }
2522 JVM_END
2523
2524
2525 JVM_ENTRY_PROF(jint, JVM_GetMethodIxExceptionsCount, JVM_GetMethodIxExceptionsCount(JNIEnv *env, jclass cls, jint method_index))
2526 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls));
2527 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
2528 Method* method = InstanceKlass::cast(k)->methods()->at(method_index);
2529 return method->checked_exceptions_length();
2530 JVM_END
2531
2532
2533 JVM_ENTRY_PROF(void, JVM_GetMethodIxByteCode, JVM_GetMethodIxByteCode(JNIEnv *env, jclass cls, jint method_index, unsigned char *code))
2534 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls));
2535 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
2536 Method* method = InstanceKlass::cast(k)->methods()->at(method_index);
2537 memcpy(code, method->code_base(), method->code_size());
2538 JVM_END
2539
2540
2541 JVM_ENTRY_PROF(jint, JVM_GetMethodIxByteCodeLength, JVM_GetMethodIxByteCodeLength(JNIEnv *env, jclass cls, jint method_index))
2542 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls));
2543 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
2544 Method* method = InstanceKlass::cast(k)->methods()->at(method_index);
2545 return method->code_size();
2546 JVM_END
2547
2548
2549 JVM_ENTRY_PROF(void, JVM_GetMethodIxExceptionTableEntry, JVM_GetMethodIxExceptionTableEntry(JNIEnv *env, jclass cls, jint method_index, jint entry_index, JVM_ExceptionTableEntryType *entry))
2550 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls));
2551 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
2552 Method* method = InstanceKlass::cast(k)->methods()->at(method_index);
2553 ExceptionTable extable(method);
2554 entry->start_pc = extable.start_pc(entry_index);
2555 entry->end_pc = extable.end_pc(entry_index);
2556 entry->handler_pc = extable.handler_pc(entry_index);
2557 entry->catchType = extable.catch_type_index(entry_index);
2558 JVM_END
2559
2560
2561 JVM_ENTRY_PROF(jint, JVM_GetMethodIxExceptionTableLength, JVM_GetMethodIxExceptionTableLength(JNIEnv *env, jclass cls, int method_index))
2562 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls));
2563 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
2564 Method* method = InstanceKlass::cast(k)->methods()->at(method_index);
2565 return method->exception_table_length();
2566 JVM_END
2567
2568
2569 JVM_ENTRY_PROF(jint, JVM_GetMethodIxModifiers, JVM_GetMethodIxModifiers(JNIEnv *env, jclass cls, int method_index))
2570 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls));
2571 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
2572 Method* method = InstanceKlass::cast(k)->methods()->at(method_index);
2573 return method->access_flags().as_int() & JVM_RECOGNIZED_METHOD_MODIFIERS;
2574 JVM_END
2575
2576
2577 JVM_ENTRY_PROF(jint, JVM_GetFieldIxModifiers, JVM_GetFieldIxModifiers(JNIEnv *env, jclass cls, int field_index))
2578 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls));
2579 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
2580 return InstanceKlass::cast(k)->field_access_flags(field_index) & JVM_RECOGNIZED_FIELD_MODIFIERS;
2581 JVM_END
2582
2583
2584 JVM_ENTRY_PROF(jint, JVM_GetMethodIxLocalsCount, JVM_GetMethodIxLocalsCount(JNIEnv *env, jclass cls, int method_index))
2585 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls));
2586 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
2587 Method* method = InstanceKlass::cast(k)->methods()->at(method_index);
2588 return method->max_locals();
2589 JVM_END
2590
2591
2592 JVM_ENTRY_PROF(jint, JVM_GetMethodIxArgsSize, JVM_GetMethodIxArgsSize(JNIEnv *env, jclass cls, int method_index))
2593 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls));
2594 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
2595 Method* method = InstanceKlass::cast(k)->methods()->at(method_index);
2596 return method->size_of_parameters();
2597 JVM_END
2598
2599
2600 JVM_ENTRY_PROF(jint, JVM_GetMethodIxMaxStack, JVM_GetMethodIxMaxStack(JNIEnv *env, jclass cls, int method_index))
2601 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls));
2602 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
2603 Method* method = InstanceKlass::cast(k)->methods()->at(method_index);
2604 return method->verifier_max_stack();
2605 JVM_END
2606
2607
2608 JVM_ENTRY_PROF(jboolean, JVM_IsConstructorIx, JVM_IsConstructorIx(JNIEnv *env, jclass cls, int method_index))
2609 ResourceMark rm(THREAD);
2610 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls));
2611 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
2612 Method* method = InstanceKlass::cast(k)->methods()->at(method_index);
2613 return method->name() == vmSymbols::object_initializer_name();
2614 JVM_END
2615
2616
2617 JVM_ENTRY_PROF(jboolean, JVM_IsVMGeneratedMethodIx, JVM_IsVMGeneratedMethodIx(JNIEnv *env, jclass cls, int method_index))
2618 ResourceMark rm(THREAD);
2619 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls));
2620 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
2621 Method* method = InstanceKlass::cast(k)->methods()->at(method_index);
2622 return method->is_overpass();
2623 JVM_END
2624
2625 JVM_ENTRY_PROF(const char*, JVM_GetMethodIxNameUTF, JVM_GetMethodIxNameUTF(JNIEnv *env, jclass cls, jint method_index))
2626 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls));
2627 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
2628 Method* method = InstanceKlass::cast(k)->methods()->at(method_index);
2629 return method->name()->as_utf8();
2630 JVM_END
2631
2632
2633 JVM_ENTRY_PROF(const char*, JVM_GetMethodIxSignatureUTF, JVM_GetMethodIxSignatureUTF(JNIEnv *env, jclass cls, jint method_index))
2634 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls));
2635 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
2636 Method* method = InstanceKlass::cast(k)->methods()->at(method_index);
2637 return method->signature()->as_utf8();
2638 JVM_END
2639
2640 /**
2641 * All of these JVM_GetCP-xxx methods are used by the old verifier to
2642 * read entries in the constant pool. Since the old verifier always
2643 * works on a copy of the code, it will not see any rewriting that
2644 * may possibly occur in the middle of verification. So it is important
2645 * that nothing it calls tries to use the cpCache instead of the raw
2646 * constant pool, so we must use cp->uncached_x methods when appropriate.
2647 */
2648 JVM_ENTRY_PROF(const char*, JVM_GetCPFieldNameUTF, JVM_GetCPFieldNameUTF(JNIEnv *env, jclass cls, jint cp_index))
2649 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls));
2650 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
2651 ConstantPool* cp = InstanceKlass::cast(k)->constants();
2652 switch (cp->tag_at(cp_index).value()) {
2653 case JVM_CONSTANT_Fieldref:
2654 return cp->uncached_name_ref_at(cp_index)->as_utf8();
2655 default:
2656 fatal("JVM_GetCPFieldNameUTF: illegal constant");
2657 }
2658 ShouldNotReachHere();
2659 return nullptr;
2660 JVM_END
2661
2662
2663 JVM_ENTRY_PROF(const char*, JVM_GetCPMethodNameUTF, JVM_GetCPMethodNameUTF(JNIEnv *env, jclass cls, jint cp_index))
2664 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls));
2665 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
2666 ConstantPool* cp = InstanceKlass::cast(k)->constants();
2667 switch (cp->tag_at(cp_index).value()) {
2668 case JVM_CONSTANT_InterfaceMethodref:
2669 case JVM_CONSTANT_Methodref:
2670 return cp->uncached_name_ref_at(cp_index)->as_utf8();
2671 default:
2672 fatal("JVM_GetCPMethodNameUTF: illegal constant");
2673 }
2674 ShouldNotReachHere();
2675 return nullptr;
2676 JVM_END
2677
2678
2679 JVM_ENTRY_PROF(const char*, JVM_GetCPMethodSignatureUTF, JVM_GetCPMethodSignatureUTF(JNIEnv *env, jclass cls, jint cp_index))
2680 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls));
2681 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
2682 ConstantPool* cp = InstanceKlass::cast(k)->constants();
2683 switch (cp->tag_at(cp_index).value()) {
2684 case JVM_CONSTANT_InterfaceMethodref:
2685 case JVM_CONSTANT_Methodref:
2686 return cp->uncached_signature_ref_at(cp_index)->as_utf8();
2687 default:
2688 fatal("JVM_GetCPMethodSignatureUTF: illegal constant");
2689 }
2690 ShouldNotReachHere();
2691 return nullptr;
2692 JVM_END
2693
2694
2695 JVM_ENTRY_PROF(const char*, JVM_GetCPFieldSignatureUTF, JVM_GetCPFieldSignatureUTF(JNIEnv *env, jclass cls, jint cp_index))
2696 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls));
2697 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
2698 ConstantPool* cp = InstanceKlass::cast(k)->constants();
2699 switch (cp->tag_at(cp_index).value()) {
2700 case JVM_CONSTANT_Fieldref:
2701 return cp->uncached_signature_ref_at(cp_index)->as_utf8();
2702 default:
2703 fatal("JVM_GetCPFieldSignatureUTF: illegal constant");
2704 }
2705 ShouldNotReachHere();
2706 return nullptr;
2707 JVM_END
2708
2709
2710 JVM_ENTRY_PROF(const char*, JVM_GetCPClassNameUTF, JVM_GetCPClassNameUTF(JNIEnv *env, jclass cls, jint cp_index))
2711 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls));
2712 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
2713 ConstantPool* cp = InstanceKlass::cast(k)->constants();
2714 Symbol* classname = cp->klass_name_at(cp_index);
2715 return classname->as_utf8();
2716 JVM_END
2717
2718
2719 JVM_ENTRY_PROF(const char*, JVM_GetCPFieldClassNameUTF, JVM_GetCPFieldClassNameUTF(JNIEnv *env, jclass cls, jint cp_index))
2720 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls));
2721 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
2722 ConstantPool* cp = InstanceKlass::cast(k)->constants();
2723 switch (cp->tag_at(cp_index).value()) {
2724 case JVM_CONSTANT_Fieldref: {
2725 int class_index = cp->uncached_klass_ref_index_at(cp_index);
2726 Symbol* classname = cp->klass_name_at(class_index);
2727 return classname->as_utf8();
2728 }
2729 default:
2730 fatal("JVM_GetCPFieldClassNameUTF: illegal constant");
2731 }
2732 ShouldNotReachHere();
2733 return nullptr;
2734 JVM_END
2735
2736
2737 JVM_ENTRY_PROF(const char*, JVM_GetCPMethodClassNameUTF, JVM_GetCPMethodClassNameUTF(JNIEnv *env, jclass cls, jint cp_index))
2738 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls));
2739 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
2740 ConstantPool* cp = InstanceKlass::cast(k)->constants();
2741 switch (cp->tag_at(cp_index).value()) {
2742 case JVM_CONSTANT_Methodref:
2743 case JVM_CONSTANT_InterfaceMethodref: {
2744 int class_index = cp->uncached_klass_ref_index_at(cp_index);
2745 Symbol* classname = cp->klass_name_at(class_index);
2746 return classname->as_utf8();
2747 }
2748 default:
2749 fatal("JVM_GetCPMethodClassNameUTF: illegal constant");
2750 }
2751 ShouldNotReachHere();
2752 return nullptr;
2753 JVM_END
2754
2755
2756 JVM_ENTRY_PROF(jint, JVM_GetCPFieldModifiers, JVM_GetCPFieldModifiers(JNIEnv *env, jclass cls, int cp_index, jclass called_cls))
2757 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls));
2758 Klass* k_called = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(called_cls));
2759 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
2760 k_called = JvmtiThreadState::class_to_verify_considering_redefinition(k_called, thread);
2761 ConstantPool* cp = InstanceKlass::cast(k)->constants();
2762 ConstantPool* cp_called = InstanceKlass::cast(k_called)->constants();
2763 switch (cp->tag_at(cp_index).value()) {
2764 case JVM_CONSTANT_Fieldref: {
2765 Symbol* name = cp->uncached_name_ref_at(cp_index);
2766 Symbol* signature = cp->uncached_signature_ref_at(cp_index);
2767 InstanceKlass* ik = InstanceKlass::cast(k_called);
2768 for (JavaFieldStream fs(ik); !fs.done(); fs.next()) {
2769 if (fs.name() == name && fs.signature() == signature) {
2770 return fs.access_flags().as_short() & JVM_RECOGNIZED_FIELD_MODIFIERS;
2771 }
2772 }
2773 return -1;
2774 }
2775 default:
2776 fatal("JVM_GetCPFieldModifiers: illegal constant");
2777 }
2778 ShouldNotReachHere();
2779 return 0;
2780 JVM_END
2781
2782
2783 JVM_ENTRY_PROF(jint, JVM_GetCPMethodModifiers, JVM_GetCPMethodModifiers(JNIEnv *env, jclass cls, int cp_index, jclass called_cls))
2784 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls));
2785 Klass* k_called = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(called_cls));
2786 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
2787 k_called = JvmtiThreadState::class_to_verify_considering_redefinition(k_called, thread);
2788 ConstantPool* cp = InstanceKlass::cast(k)->constants();
2789 switch (cp->tag_at(cp_index).value()) {
2790 case JVM_CONSTANT_Methodref:
2791 case JVM_CONSTANT_InterfaceMethodref: {
2792 Symbol* name = cp->uncached_name_ref_at(cp_index);
2793 Symbol* signature = cp->uncached_signature_ref_at(cp_index);
2794 Array<Method*>* methods = InstanceKlass::cast(k_called)->methods();
2795 int methods_count = methods->length();
2796 for (int i = 0; i < methods_count; i++) {
2797 Method* method = methods->at(i);
2798 if (method->name() == name && method->signature() == signature) {
2799 return method->access_flags().as_int() & JVM_RECOGNIZED_METHOD_MODIFIERS;
2800 }
2801 }
2802 return -1;
2803 }
2804 default:
2805 fatal("JVM_GetCPMethodModifiers: illegal constant");
2806 }
2807 ShouldNotReachHere();
2808 return 0;
2809 JVM_END
2810
2811
2812 // Misc //////////////////////////////////////////////////////////////////////////////////////////////
2813
2814 JVM_LEAF_PROF(void, JVM_ReleaseUTF, JVM_ReleaseUTF(const char *utf))
2815 // So long as UTF8::convert_to_utf8 returns resource strings, we don't have to do anything
2816 JVM_END
2817
2818
2819 JVM_ENTRY_PROF(jboolean, JVM_IsSameClassPackage, JVM_IsSameClassPackage(JNIEnv *env, jclass class1, jclass class2))
2820 oop class1_mirror = JNIHandles::resolve_non_null(class1);
2821 oop class2_mirror = JNIHandles::resolve_non_null(class2);
2822 Klass* klass1 = java_lang_Class::as_Klass(class1_mirror);
2823 Klass* klass2 = java_lang_Class::as_Klass(class2_mirror);
2824 return (jboolean) Reflection::is_same_class_package(klass1, klass2);
2825 JVM_END
2826
2827 // Printing support //////////////////////////////////////////////////
2828 extern "C" {
2829
2830 ATTRIBUTE_PRINTF(3, 0)
2831 int jio_vsnprintf(char *str, size_t count, const char *fmt, va_list args) {
2832 // Reject count values that are negative signed values converted to
2833 // unsigned; see bug 4399518, 4417214
2834 if ((intptr_t)count <= 0) return -1;
2835
2836 int result = os::vsnprintf(str, count, fmt, args);
2837 if (result > 0 && (size_t)result >= count) {
2838 result = -1;
2839 }
2898 // In most of the JVM thread support functions we need to access the
2899 // thread through a ThreadsListHandle to prevent it from exiting and
2900 // being reclaimed while we try to operate on it. The exceptions to this
2901 // rule are when operating on the current thread, or if the monitor of
2902 // the target java.lang.Thread is locked at the Java level - in both
2903 // cases the target cannot exit.
2904
2905 static void thread_entry(JavaThread* thread, TRAPS) {
2906 HandleMark hm(THREAD);
2907 Handle obj(THREAD, thread->threadObj());
2908 JavaValue result(T_VOID);
2909 JavaCalls::call_virtual(&result,
2910 obj,
2911 vmClasses::Thread_klass(),
2912 vmSymbols::run_method_name(),
2913 vmSymbols::void_method_signature(),
2914 THREAD);
2915 }
2916
2917
2918 JVM_ENTRY_PROF(void, JVM_StartThread, JVM_StartThread(JNIEnv* env, jobject jthread))
2919 #if INCLUDE_CDS
2920 if (CDSConfig::allow_only_single_java_thread()) {
2921 // During java -Xshare:dump, if we allow multiple Java threads to
2922 // execute in parallel, symbols and classes may be loaded in
2923 // random orders which will make the resulting CDS archive
2924 // non-deterministic.
2925 //
2926 // Lucikly, during java -Xshare:dump, it's important to run only
2927 // the code in the main Java thread (which is NOT started here) that
2928 // creates the module graph, etc. It's safe to not start the other
2929 // threads which are launched by class static initializers
2930 // (ReferenceHandler, FinalizerThread and CleanerImpl).
2931 if (log_is_enabled(Info, cds)) {
2932 ResourceMark rm;
2933 oop t = JNIHandles::resolve_non_null(jthread);
2934 log_info(cds)("JVM_StartThread() ignored: %s", t->klass()->external_name());
2935 }
2936 return;
2937 }
2938 #endif
2939 JavaThread *native_thread = nullptr;
2940
2994 log_warning(os, thread)("Failed to start the native thread for java.lang.Thread \"%s\"",
2995 JavaThread::name_for(JNIHandles::resolve_non_null(jthread)));
2996 // No one should hold a reference to the 'native_thread'.
2997 native_thread->smr_delete();
2998 if (JvmtiExport::should_post_resource_exhausted()) {
2999 JvmtiExport::post_resource_exhausted(
3000 JVMTI_RESOURCE_EXHAUSTED_OOM_ERROR | JVMTI_RESOURCE_EXHAUSTED_THREADS,
3001 os::native_thread_creation_failed_msg());
3002 }
3003 THROW_MSG(vmSymbols::java_lang_OutOfMemoryError(),
3004 os::native_thread_creation_failed_msg());
3005 }
3006
3007 JFR_ONLY(Jfr::on_java_thread_start(thread, native_thread);)
3008
3009 Thread::start(native_thread);
3010
3011 JVM_END
3012
3013
3014 JVM_ENTRY_PROF(void, JVM_SetThreadPriority, JVM_SetThreadPriority(JNIEnv* env, jobject jthread, jint prio))
3015 ThreadsListHandle tlh(thread);
3016 oop java_thread = nullptr;
3017 JavaThread* receiver = nullptr;
3018 bool is_alive = tlh.cv_internal_thread_to_JavaThread(jthread, &receiver, &java_thread);
3019 java_lang_Thread::set_priority(java_thread, (ThreadPriority)prio);
3020
3021 if (is_alive) {
3022 // jthread refers to a live JavaThread.
3023 Thread::set_priority(receiver, (ThreadPriority)prio);
3024 }
3025 // Implied else: If the JavaThread hasn't started yet, then the
3026 // priority set in the java.lang.Thread object above will be pushed
3027 // down when it does start.
3028 JVM_END
3029
3030
3031 JVM_LEAF_PROF(void, JVM_Yield, JVM_Yield(JNIEnv *env, jclass threadClass))
3032 if (os::dont_yield()) return;
3033 HOTSPOT_THREAD_YIELD();
3034 os::naked_yield();
3035 JVM_END
3036
3037 JVM_ENTRY_PROF(void, JVM_SleepNanos, JVM_SleepNanos(JNIEnv* env, jclass threadClass, jlong nanos))
3038 if (nanos < 0) {
3039 THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), "nanosecond timeout value out of range");
3040 }
3041
3042 if (thread->is_interrupted(true) && !HAS_PENDING_EXCEPTION) {
3043 THROW_MSG(vmSymbols::java_lang_InterruptedException(), "sleep interrupted");
3044 }
3045
3046 // Save current thread state and restore it at the end of this block.
3047 // And set new thread state to SLEEPING.
3048 JavaThreadSleepState jtss(thread);
3049
3050 HOTSPOT_THREAD_SLEEP_BEGIN(nanos / NANOSECS_PER_MILLISEC);
3051
3052 if (nanos == 0) {
3053 os::naked_yield();
3054 } else {
3055 ThreadState old_state = thread->osthread()->get_state();
3056 thread->osthread()->set_state(SLEEPING);
3057 if (!thread->sleep_nanos(nanos)) { // interrupted
3058 // An asynchronous exception could have been thrown on
3059 // us while we were sleeping. We do not overwrite those.
3060 if (!HAS_PENDING_EXCEPTION) {
3061 HOTSPOT_THREAD_SLEEP_END(1);
3062
3063 // TODO-FIXME: THROW_MSG returns which means we will not call set_state()
3064 // to properly restore the thread state. That's likely wrong.
3065 THROW_MSG(vmSymbols::java_lang_InterruptedException(), "sleep interrupted");
3066 }
3067 }
3068 thread->osthread()->set_state(old_state);
3069 }
3070 HOTSPOT_THREAD_SLEEP_END(0);
3071 JVM_END
3072
3073 JVM_ENTRY_PROF(jobject, JVM_CurrentCarrierThread, JVM_CurrentCarrierThread(JNIEnv* env, jclass threadClass))
3074 oop jthread = thread->threadObj();
3075 assert(jthread != nullptr, "no current carrier thread!");
3076 return JNIHandles::make_local(THREAD, jthread);
3077 JVM_END
3078
3079 JVM_ENTRY_PROF(jobject, JVM_CurrentThread, JVM_CurrentThread(JNIEnv* env, jclass threadClass))
3080 oop theThread = thread->vthread();
3081 assert(theThread != (oop)nullptr, "no current thread!");
3082 return JNIHandles::make_local(THREAD, theThread);
3083 JVM_END
3084
3085 JVM_ENTRY_PROF(void, JVM_SetCurrentThread, JVM_SetCurrentThread(JNIEnv* env, jobject thisThread,
3086 jobject theThread))
3087 oop threadObj = JNIHandles::resolve(theThread);
3088 thread->set_vthread(threadObj);
3089 JFR_ONLY(Jfr::on_set_current_thread(thread, threadObj);)
3090 JVM_END
3091
3092 JVM_ENTRY_PROF(jlong, JVM_GetNextThreadIdOffset, JVM_GetNextThreadIdOffset(JNIEnv* env, jclass threadClass))
3093 return ThreadIdentifier::unsafe_offset();
3094 JVM_END
3095
3096 JVM_ENTRY_PROF(void, JVM_Interrupt, JVM_Interrupt(JNIEnv* env, jobject jthread))
3097 ThreadsListHandle tlh(thread);
3098 JavaThread* receiver = nullptr;
3099 bool is_alive = tlh.cv_internal_thread_to_JavaThread(jthread, &receiver, nullptr);
3100 if (is_alive) {
3101 // jthread refers to a live JavaThread.
3102 receiver->interrupt();
3103 }
3104 JVM_END
3105
3106 // Return true iff the current thread has locked the object passed in
3107
3108 JVM_ENTRY_PROF(jboolean, JVM_HoldsLock, JVM_HoldsLock(JNIEnv* env, jclass threadClass, jobject obj))
3109 if (obj == nullptr) {
3110 THROW_(vmSymbols::java_lang_NullPointerException(), JNI_FALSE);
3111 }
3112 Handle h_obj(THREAD, JNIHandles::resolve(obj));
3113 return ObjectSynchronizer::current_thread_holds_lock(thread, h_obj);
3114 JVM_END
3115
3116 JVM_ENTRY_PROF(jobject, JVM_GetStackTrace, JVM_GetStackTrace(JNIEnv *env, jobject jthread))
3117 oop trace = java_lang_Thread::async_get_stack_trace(JNIHandles::resolve(jthread), THREAD);
3118 return JNIHandles::make_local(THREAD, trace);
3119 JVM_END
3120
3121
3122 JVM_ENTRY_PROF(void, JVM_SetNativeThreadName, JVM_SetNativeThreadName(JNIEnv* env, jobject jthread, jstring name))
3123 // We don't use a ThreadsListHandle here because the current thread
3124 // must be alive.
3125 oop java_thread = JNIHandles::resolve_non_null(jthread);
3126 JavaThread* thr = java_lang_Thread::thread(java_thread);
3127 if (thread == thr && !thr->has_attached_via_jni()) {
3128 // Thread naming is only supported for the current thread and
3129 // we don't set the name of an attached thread to avoid stepping
3130 // on other programs.
3131 ResourceMark rm(thread);
3132 const char *thread_name = java_lang_String::as_utf8_string(JNIHandles::resolve_non_null(name));
3133 os::set_native_thread_name(thread_name);
3134 }
3135 JVM_END
3136
3137 JVM_ENTRY_PROF(jobject, JVM_ScopedValueCache, JVM_ScopedValueCache(JNIEnv* env, jclass threadClass))
3138 oop theCache = thread->scopedValueCache();
3139 return JNIHandles::make_local(THREAD, theCache);
3140 JVM_END
3141
3142 JVM_ENTRY_PROF(void, JVM_SetScopedValueCache, JVM_SetScopedValueCache(JNIEnv* env, jclass threadClass,
3143 jobject theCache))
3144 arrayOop objs = arrayOop(JNIHandles::resolve(theCache));
3145 thread->set_scopedValueCache(objs);
3146 JVM_END
3147
3148 // java.lang.SecurityManager ///////////////////////////////////////////////////////////////////////
3149
3150 JVM_ENTRY_PROF(jobjectArray, JVM_GetClassContext, JVM_GetClassContext(JNIEnv *env))
3151 ResourceMark rm(THREAD);
3152 JvmtiVMObjectAllocEventCollector oam;
3153 vframeStream vfst(thread);
3154
3155 if (vmClasses::reflect_CallerSensitive_klass() != nullptr) {
3156 // This must only be called from SecurityManager.getClassContext
3157 Method* m = vfst.method();
3158 if (!(m->method_holder() == vmClasses::SecurityManager_klass() &&
3159 m->name() == vmSymbols::getClassContext_name() &&
3160 m->signature() == vmSymbols::void_class_array_signature())) {
3161 THROW_MSG_NULL(vmSymbols::java_lang_InternalError(), "JVM_GetClassContext must only be called from SecurityManager.getClassContext");
3162 }
3163 }
3164
3165 // Collect method holders
3166 GrowableArray<Klass*>* klass_array = new GrowableArray<Klass*>();
3167 for (; !vfst.at_end(); vfst.security_next()) {
3168 Method* m = vfst.method();
3169 // Native frames are not returned
3170 if (!m->is_ignored_by_security_stack_walk() && !m->is_native()) {
3171 Klass* holder = m->method_holder();
3172 assert(holder->is_klass(), "just checking");
3173 klass_array->append(holder);
3174 }
3175 }
3176
3177 // Create result array of type [Ljava/lang/Class;
3178 objArrayOop result = oopFactory::new_objArray(vmClasses::Class_klass(), klass_array->length(), CHECK_NULL);
3179 // Fill in mirrors corresponding to method holders
3180 for (int i = 0; i < klass_array->length(); i++) {
3181 result->obj_at_put(i, klass_array->at(i)->java_mirror());
3182 }
3183
3184 return (jobjectArray) JNIHandles::make_local(THREAD, result);
3185 JVM_END
3186
3187
3188 // java.lang.Package ////////////////////////////////////////////////////////////////
3189
3190
3191 JVM_ENTRY_PROF(jstring, JVM_GetSystemPackage, JVM_GetSystemPackage(JNIEnv *env, jstring name))
3192 ResourceMark rm(THREAD);
3193 JvmtiVMObjectAllocEventCollector oam;
3194 char* str = java_lang_String::as_utf8_string(JNIHandles::resolve_non_null(name));
3195 oop result = ClassLoader::get_system_package(str, CHECK_NULL);
3196 return (jstring) JNIHandles::make_local(THREAD, result);
3197 JVM_END
3198
3199
3200 JVM_ENTRY_PROF(jobjectArray, JVM_GetSystemPackages, JVM_GetSystemPackages(JNIEnv *env))
3201 JvmtiVMObjectAllocEventCollector oam;
3202 objArrayOop result = ClassLoader::get_system_packages(CHECK_NULL);
3203 return (jobjectArray) JNIHandles::make_local(THREAD, result);
3204 JVM_END
3205
3206
3207 // java.lang.ref.Reference ///////////////////////////////////////////////////////////////
3208
3209
3210 JVM_ENTRY_PROF(jobject, JVM_GetAndClearReferencePendingList, JVM_GetAndClearReferencePendingList(JNIEnv* env))
3211 MonitorLocker ml(Heap_lock);
3212 oop ref = Universe::reference_pending_list();
3213 if (ref != nullptr) {
3214 Universe::clear_reference_pending_list();
3215 }
3216 return JNIHandles::make_local(THREAD, ref);
3217 JVM_END
3218
3219 JVM_ENTRY_PROF(jboolean, JVM_HasReferencePendingList, JVM_HasReferencePendingList(JNIEnv* env))
3220 MonitorLocker ml(Heap_lock);
3221 return Universe::has_reference_pending_list();
3222 JVM_END
3223
3224 JVM_ENTRY_PROF(void, JVM_WaitForReferencePendingList, JVM_WaitForReferencePendingList(JNIEnv* env))
3225 MonitorLocker ml(Heap_lock);
3226 while (!Universe::has_reference_pending_list()) {
3227 ml.wait();
3228 }
3229 JVM_END
3230
3231 JVM_ENTRY_PROF(jboolean, JVM_ReferenceRefersTo, JVM_ReferenceRefersTo(JNIEnv* env, jobject ref, jobject o))
3232 oop ref_oop = JNIHandles::resolve_non_null(ref);
3233 // PhantomReference has it's own implementation of refersTo().
3234 // See: JVM_PhantomReferenceRefersTo
3235 assert(!java_lang_ref_Reference::is_phantom(ref_oop), "precondition");
3236 oop referent = java_lang_ref_Reference::weak_referent_no_keepalive(ref_oop);
3237 return referent == JNIHandles::resolve(o);
3238 JVM_END
3239
3240 JVM_ENTRY_PROF(void, JVM_ReferenceClear, JVM_ReferenceClear(JNIEnv* env, jobject ref))
3241 oop ref_oop = JNIHandles::resolve_non_null(ref);
3242 // FinalReference has it's own implementation of clear().
3243 assert(!java_lang_ref_Reference::is_final(ref_oop), "precondition");
3244 if (java_lang_ref_Reference::unknown_referent_no_keepalive(ref_oop) == nullptr) {
3245 // If the referent has already been cleared then done.
3246 // However, if the referent is dead but has not yet been cleared by
3247 // concurrent reference processing, it should NOT be cleared here.
3248 // Instead, clearing should be left to the GC. Clearing it here could
3249 // detectably lose an expected notification, which is impossible with
3250 // STW reference processing. The clearing in enqueue() doesn't have
3251 // this problem, since the enqueue covers the notification, but it's not
3252 // worth the effort to handle that case specially.
3253 return;
3254 }
3255 java_lang_ref_Reference::clear_referent(ref_oop);
3256 JVM_END
3257
3258
3259 // java.lang.ref.PhantomReference //////////////////////////////////////////////////
3260
3261
3262 JVM_ENTRY_PROF(jboolean, JVM_PhantomReferenceRefersTo, JVM_PhantomReferenceRefersTo(JNIEnv* env, jobject ref, jobject o))
3263 oop ref_oop = JNIHandles::resolve_non_null(ref);
3264 oop referent = java_lang_ref_Reference::phantom_referent_no_keepalive(ref_oop);
3265 return referent == JNIHandles::resolve(o);
3266 JVM_END
3267
3268
3269 // ObjectInputStream ///////////////////////////////////////////////////////////////
3270
3271 // Return the first user-defined class loader up the execution stack, or null
3272 // if only code from the bootstrap or platform class loader is on the stack.
3273
3274 JVM_ENTRY_PROF(jobject, JVM_LatestUserDefinedLoader, JVM_LatestUserDefinedLoader(JNIEnv *env))
3275 for (vframeStream vfst(thread); !vfst.at_end(); vfst.next()) {
3276 InstanceKlass* ik = vfst.method()->method_holder();
3277 oop loader = ik->class_loader();
3278 if (loader != nullptr && !SystemDictionary::is_platform_class_loader(loader)) {
3279 // Skip reflection related frames
3280 if (!ik->is_subclass_of(vmClasses::reflect_SerializationConstructorAccessorImpl_klass())) {
3281 return JNIHandles::make_local(THREAD, loader);
3282 }
3283 }
3284 }
3285 return nullptr;
3286 JVM_END
3287
3288
3289 // Array ///////////////////////////////////////////////////////////////////////////////////////////
3290
3291
3292 // resolve array handle and check arguments
3293 static inline arrayOop check_array(JNIEnv *env, jobject arr, bool type_array_only, TRAPS) {
3294 if (arr == nullptr) {
3295 THROW_NULL(vmSymbols::java_lang_NullPointerException());
3296 }
3297 oop a = JNIHandles::resolve_non_null(arr);
3298 if (!a->is_array()) {
3299 THROW_MSG_NULL(vmSymbols::java_lang_IllegalArgumentException(), "Argument is not an array");
3300 } else if (type_array_only && !a->is_typeArray()) {
3301 THROW_MSG_NULL(vmSymbols::java_lang_IllegalArgumentException(), "Argument is not an array of primitive type");
3302 }
3303 return arrayOop(a);
3304 }
3305
3306
3307 JVM_ENTRY_PROF(jint, JVM_GetArrayLength, JVM_GetArrayLength(JNIEnv *env, jobject arr))
3308 arrayOop a = check_array(env, arr, false, CHECK_0);
3309 return a->length();
3310 JVM_END
3311
3312
3313 JVM_ENTRY_PROF(jobject, JVM_GetArrayElement, JVM_GetArrayElement(JNIEnv *env, jobject arr, jint index))
3314 JvmtiVMObjectAllocEventCollector oam;
3315 arrayOop a = check_array(env, arr, false, CHECK_NULL);
3316 jvalue value;
3317 BasicType type = Reflection::array_get(&value, a, index, CHECK_NULL);
3318 oop box = Reflection::box(&value, type, CHECK_NULL);
3319 return JNIHandles::make_local(THREAD, box);
3320 JVM_END
3321
3322
3323 JVM_ENTRY_PROF(jvalue, JVM_GetPrimitiveArrayElement, JVM_GetPrimitiveArrayElement(JNIEnv *env, jobject arr, jint index, jint wCode))
3324 jvalue value;
3325 value.i = 0; // to initialize value before getting used in CHECK
3326 arrayOop a = check_array(env, arr, true, CHECK_(value));
3327 assert(a->is_typeArray(), "just checking");
3328 BasicType type = Reflection::array_get(&value, a, index, CHECK_(value));
3329 BasicType wide_type = (BasicType) wCode;
3330 if (type != wide_type) {
3331 Reflection::widen(&value, type, wide_type, CHECK_(value));
3332 }
3333 return value;
3334 JVM_END
3335
3336
3337 JVM_ENTRY_PROF(void, JVM_SetArrayElement, JVM_SetArrayElement(JNIEnv *env, jobject arr, jint index, jobject val))
3338 arrayOop a = check_array(env, arr, false, CHECK);
3339 oop box = JNIHandles::resolve(val);
3340 jvalue value;
3341 value.i = 0; // to initialize value before getting used in CHECK
3342 BasicType value_type;
3343 if (a->is_objArray()) {
3344 // Make sure we do no unbox e.g. java/lang/Integer instances when storing into an object array
3345 value_type = Reflection::unbox_for_regular_object(box, &value);
3346 } else {
3347 value_type = Reflection::unbox_for_primitive(box, &value, CHECK);
3348 }
3349 Reflection::array_set(&value, a, index, value_type, CHECK);
3350 JVM_END
3351
3352
3353 JVM_ENTRY_PROF(void, JVM_SetPrimitiveArrayElement, JVM_SetPrimitiveArrayElement(JNIEnv *env, jobject arr, jint index, jvalue v, unsigned char vCode))
3354 arrayOop a = check_array(env, arr, true, CHECK);
3355 assert(a->is_typeArray(), "just checking");
3356 BasicType value_type = (BasicType) vCode;
3357 Reflection::array_set(&v, a, index, value_type, CHECK);
3358 JVM_END
3359
3360
3361 JVM_ENTRY_PROF(jobject, JVM_NewArray, JVM_NewArray(JNIEnv *env, jclass eltClass, jint length))
3362 JvmtiVMObjectAllocEventCollector oam;
3363 oop element_mirror = JNIHandles::resolve(eltClass);
3364 oop result = Reflection::reflect_new_array(element_mirror, length, CHECK_NULL);
3365 return JNIHandles::make_local(THREAD, result);
3366 JVM_END
3367
3368
3369 JVM_ENTRY_PROF(jobject, JVM_NewMultiArray, JVM_NewMultiArray(JNIEnv *env, jclass eltClass, jintArray dim))
3370 JvmtiVMObjectAllocEventCollector oam;
3371 arrayOop dim_array = check_array(env, dim, true, CHECK_NULL);
3372 oop element_mirror = JNIHandles::resolve(eltClass);
3373 assert(dim_array->is_typeArray(), "just checking");
3374 oop result = Reflection::reflect_new_multi_array(element_mirror, typeArrayOop(dim_array), CHECK_NULL);
3375 return JNIHandles::make_local(THREAD, result);
3376 JVM_END
3377
3378
3379 // Library support ///////////////////////////////////////////////////////////////////////////
3380
3381 JVM_LEAF_PROF(void*, JVM_LoadZipLibrary, JVM_LoadZipLibrary())
3382 return ZipLibrary::handle();
3383 JVM_END
3384
3385 JVM_ENTRY_NO_ENV_PROF(void*, JVM_LoadLibrary, JVM_LoadLibrary(const char* name, jboolean throwException))
3386 //%note jvm_ct
3387 char ebuf[1024];
3388 void *load_result;
3389 {
3390 ThreadToNativeFromVM ttnfvm(thread);
3391 load_result = os::dll_load(name, ebuf, sizeof ebuf);
3392 }
3393 if (load_result == nullptr) {
3394 if (throwException) {
3395 char msg[1024];
3396 jio_snprintf(msg, sizeof msg, "%s: %s", name, ebuf);
3397 // Since 'ebuf' may contain a string encoded using
3398 // platform encoding scheme, we need to pass
3399 // Exceptions::unsafe_to_utf8 to the new_exception method
3400 // as the last argument. See bug 6367357.
3401 Handle h_exception =
3402 Exceptions::new_exception(thread,
3403 vmSymbols::java_lang_UnsatisfiedLinkError(),
3404 msg, Exceptions::unsafe_to_utf8);
3405
3406 THROW_HANDLE_NULL(h_exception);
3407 } else {
3408 log_info(library)("Failed to load library %s", name);
3409 return load_result;
3410 }
3411 }
3412 log_info(library)("Loaded library %s, handle " INTPTR_FORMAT, name, p2i(load_result));
3413 return load_result;
3414 JVM_END
3415
3416
3417 JVM_LEAF_PROF(void, JVM_UnloadLibrary, JVM_UnloadLibrary(void* handle))
3418 os::dll_unload(handle);
3419 log_info(library)("Unloaded library with handle " INTPTR_FORMAT, p2i(handle));
3420 JVM_END
3421
3422
3423 JVM_LEAF_PROF(void*, JVM_FindLibraryEntry, JVM_FindLibraryEntry(void* handle, const char* name))
3424 void* find_result = os::dll_lookup(handle, name);
3425 log_info(library)("%s %s in library with handle " INTPTR_FORMAT,
3426 find_result != nullptr ? "Found" : "Failed to find",
3427 name, p2i(handle));
3428 return find_result;
3429 JVM_END
3430
3431
3432 // JNI version ///////////////////////////////////////////////////////////////////////////////
3433
3434 JVM_LEAF_PROF(jboolean, JVM_IsSupportedJNIVersion, JVM_IsSupportedJNIVersion(jint version))
3435 return Threads::is_supported_jni_version_including_1_1(version);
3436 JVM_END
3437
3438
3439 JVM_LEAF_PROF(jboolean, JVM_IsPreviewEnabled, JVM_IsPreviewEnabled(void))
3440 return Arguments::enable_preview() ? JNI_TRUE : JNI_FALSE;
3441 JVM_END
3442
3443 JVM_LEAF_PROF(jboolean, JVM_IsContinuationsSupported, JVM_IsContinuationsSupported(void))
3444 return VMContinuations ? JNI_TRUE : JNI_FALSE;
3445 JVM_END
3446
3447 JVM_LEAF_PROF(jboolean, JVM_IsForeignLinkerSupported, JVM_IsForeignLinkerSupported(void))
3448 return ForeignGlobals::is_foreign_linker_supported() ? JNI_TRUE : JNI_FALSE;
3449 JVM_END
3450
3451 JVM_ENTRY_NO_ENV(jboolean, JVM_IsStaticallyLinked(void))
3452 return is_vm_statically_linked() ? JNI_TRUE : JNI_FALSE;
3453 JVM_END
3454
3455 // String support ///////////////////////////////////////////////////////////////////////////
3456
3457 JVM_ENTRY_PROF(jstring, JVM_InternString, JVM_InternString(JNIEnv *env, jstring str))
3458 JvmtiVMObjectAllocEventCollector oam;
3459 if (str == nullptr) return nullptr;
3460 oop string = JNIHandles::resolve_non_null(str);
3461 oop result = StringTable::intern(string, CHECK_NULL);
3462 return (jstring) JNIHandles::make_local(THREAD, result);
3463 JVM_END
3464
3465
3466 // VM Raw monitor support //////////////////////////////////////////////////////////////////////
3467
3468 // VM Raw monitors (not to be confused with JvmtiRawMonitors) are a simple mutual exclusion
3469 // lock (not actually monitors: no wait/notify) that is exported by the VM for use by JDK
3470 // library code. They may be used by JavaThreads and non-JavaThreads and do not participate
3471 // in the safepoint protocol, thread suspension, thread interruption, or anything of that
3472 // nature. JavaThreads will be "in native" when using this API from JDK code.
3473
3474
3475 JNIEXPORT void* JNICALL JVM_RawMonitorCreate(void) {
3476 VM_Exit::block_if_vm_exited();
3477 return new PlatformMutex();
3503 Handle loader, Handle protection_domain,
3504 jboolean throwError, TRAPS) {
3505 // Security Note:
3506 // The Java level wrapper will perform the necessary security check allowing
3507 // us to pass the null as the initiating class loader. The VM is responsible for
3508 // the checkPackageAccess relative to the initiating class loader via the
3509 // protection_domain. The protection_domain is passed as null by the java code
3510 // if there is no security manager in 3-arg Class.forName().
3511 Klass* klass = SystemDictionary::resolve_or_fail(name, loader, protection_domain, throwError != 0, CHECK_NULL);
3512
3513 // Check if we should initialize the class
3514 if (init && klass->is_instance_klass()) {
3515 klass->initialize(CHECK_NULL);
3516 }
3517 return (jclass) JNIHandles::make_local(THREAD, klass->java_mirror());
3518 }
3519
3520
3521 // Method ///////////////////////////////////////////////////////////////////////////////////////////
3522
3523 JVM_ENTRY_PROF(jobject, JVM_InvokeMethod, JVM_InvokeMethod(JNIEnv *env, jobject method, jobject obj, jobjectArray args0))
3524 Handle method_handle;
3525 if (thread->stack_overflow_state()->stack_available((address) &method_handle) >= JVMInvokeMethodSlack) {
3526 method_handle = Handle(THREAD, JNIHandles::resolve(method));
3527 Handle receiver(THREAD, JNIHandles::resolve(obj));
3528 objArrayHandle args(THREAD, objArrayOop(JNIHandles::resolve(args0)));
3529 oop result = Reflection::invoke_method(method_handle(), receiver, args, CHECK_NULL);
3530 jobject res = JNIHandles::make_local(THREAD, result);
3531 if (JvmtiExport::should_post_vm_object_alloc()) {
3532 oop ret_type = java_lang_reflect_Method::return_type(method_handle());
3533 assert(ret_type != nullptr, "sanity check: ret_type oop must not be null!");
3534 if (java_lang_Class::is_primitive(ret_type)) {
3535 // Only for primitive type vm allocates memory for java object.
3536 // See box() method.
3537 JvmtiExport::post_vm_object_alloc(thread, result);
3538 }
3539 }
3540 return res;
3541 } else {
3542 THROW_NULL(vmSymbols::java_lang_StackOverflowError());
3543 }
3544 JVM_END
3545
3546
3547 JVM_ENTRY_PROF(jobject, JVM_NewInstanceFromConstructor, JVM_NewInstanceFromConstructor(JNIEnv *env, jobject c, jobjectArray args0))
3548 oop constructor_mirror = JNIHandles::resolve(c);
3549 objArrayHandle args(THREAD, objArrayOop(JNIHandles::resolve(args0)));
3550 oop result = Reflection::invoke_constructor(constructor_mirror, args, CHECK_NULL);
3551 jobject res = JNIHandles::make_local(THREAD, result);
3552 if (JvmtiExport::should_post_vm_object_alloc()) {
3553 JvmtiExport::post_vm_object_alloc(thread, result);
3554 }
3555 return res;
3556 JVM_END
3557
3558 JVM_ENTRY_PROF(void, JVM_InitializeFromArchive, JVM_InitializeFromArchive(JNIEnv* env, jclass cls))
3559 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve(cls));
3560 assert(k->is_klass(), "just checking");
3561 HeapShared::initialize_from_archived_subgraph(THREAD, k);
3562 JVM_END
3563
3564 JVM_ENTRY_PROF(void, JVM_RegisterLambdaProxyClassForArchiving, JVM_RegisterLambdaProxyClassForArchiving(JNIEnv* env,
3565 jclass caller,
3566 jstring interfaceMethodName,
3567 jobject factoryType,
3568 jobject interfaceMethodType,
3569 jobject implementationMember,
3570 jobject dynamicMethodType,
3571 jclass lambdaProxyClass))
3572 #if INCLUDE_CDS
3573 if (!CDSConfig::is_dumping_archive()) {
3574 return;
3575 }
3576
3577 Klass* caller_k = java_lang_Class::as_Klass(JNIHandles::resolve(caller));
3578 InstanceKlass* caller_ik = InstanceKlass::cast(caller_k);
3579 if (caller_ik->is_hidden()) {
3580 // Hidden classes not of type lambda proxy classes are currently not being archived.
3581 // If the caller_ik is of one of the above types, the corresponding lambda proxy class won't be
3582 // registered for archiving.
3583 return;
3584 }
3592 interface_method_name = java_lang_String::as_symbol(JNIHandles::resolve_non_null(interfaceMethodName));
3593 }
3594 Handle factory_type_oop(THREAD, JNIHandles::resolve_non_null(factoryType));
3595 Symbol* factory_type = java_lang_invoke_MethodType::as_signature(factory_type_oop(), true);
3596
3597 Handle interface_method_type_oop(THREAD, JNIHandles::resolve_non_null(interfaceMethodType));
3598 Symbol* interface_method_type = java_lang_invoke_MethodType::as_signature(interface_method_type_oop(), true);
3599
3600 Handle implementation_member_oop(THREAD, JNIHandles::resolve_non_null(implementationMember));
3601 assert(java_lang_invoke_MemberName::is_method(implementation_member_oop()), "must be");
3602 Method* m = java_lang_invoke_MemberName::vmtarget(implementation_member_oop());
3603
3604 Handle dynamic_method_type_oop(THREAD, JNIHandles::resolve_non_null(dynamicMethodType));
3605 Symbol* dynamic_method_type = java_lang_invoke_MethodType::as_signature(dynamic_method_type_oop(), true);
3606
3607 SystemDictionaryShared::add_lambda_proxy_class(caller_ik, lambda_ik, interface_method_name, factory_type,
3608 interface_method_type, m, dynamic_method_type, THREAD);
3609 #endif // INCLUDE_CDS
3610 JVM_END
3611
3612 JVM_ENTRY_PROF(jclass, JVM_LookupLambdaProxyClassFromArchive, JVM_LookupLambdaProxyClassFromArchive(JNIEnv* env,
3613 jclass caller,
3614 jstring interfaceMethodName,
3615 jobject factoryType,
3616 jobject interfaceMethodType,
3617 jobject implementationMember,
3618 jobject dynamicMethodType))
3619 #if INCLUDE_CDS
3620
3621 if (interfaceMethodName == nullptr || factoryType == nullptr || interfaceMethodType == nullptr ||
3622 implementationMember == nullptr || dynamicMethodType == nullptr) {
3623 THROW_(vmSymbols::java_lang_NullPointerException(), nullptr);
3624 }
3625
3626 Klass* caller_k = java_lang_Class::as_Klass(JNIHandles::resolve(caller));
3627 InstanceKlass* caller_ik = InstanceKlass::cast(caller_k);
3628 if (!caller_ik->is_shared()) {
3629 // there won't be a shared lambda class if the caller_ik is not in the shared archive.
3630 return nullptr;
3631 }
3632
3640 Handle implementation_member_oop(THREAD, JNIHandles::resolve_non_null(implementationMember));
3641 assert(java_lang_invoke_MemberName::is_method(implementation_member_oop()), "must be");
3642 Method* m = java_lang_invoke_MemberName::vmtarget(implementation_member_oop());
3643
3644 Handle dynamic_method_type_oop(THREAD, JNIHandles::resolve_non_null(dynamicMethodType));
3645 Symbol* dynamic_method_type = java_lang_invoke_MethodType::as_signature(dynamic_method_type_oop(), true);
3646
3647 InstanceKlass* lambda_ik = SystemDictionaryShared::get_shared_lambda_proxy_class(caller_ik, interface_method_name, factory_type,
3648 interface_method_type, m, dynamic_method_type);
3649 jclass jcls = nullptr;
3650 if (lambda_ik != nullptr) {
3651 InstanceKlass* loaded_lambda = SystemDictionaryShared::prepare_shared_lambda_proxy_class(lambda_ik, caller_ik, THREAD);
3652 jcls = loaded_lambda == nullptr ? nullptr : (jclass) JNIHandles::make_local(THREAD, loaded_lambda->java_mirror());
3653 }
3654 return jcls;
3655 #else
3656 return nullptr;
3657 #endif // INCLUDE_CDS
3658 JVM_END
3659
3660 JVM_ENTRY_NO_ENV_PROF(jlong, JVM_GetRandomSeedForDumping, JVM_GetRandomSeedForDumping())
3661 if (CDSConfig::is_dumping_static_archive()) {
3662 // We do this so that the default CDS archive can be deterministic.
3663 const char* release = VM_Version::vm_release();
3664 const char* dbg_level = VM_Version::jdk_debug_level();
3665 const char* version = VM_Version::internal_vm_info_string();
3666 jlong seed = (jlong)(java_lang_String::hash_code((const jbyte*)release, (int)strlen(release)) ^
3667 java_lang_String::hash_code((const jbyte*)dbg_level, (int)strlen(dbg_level)) ^
3668 java_lang_String::hash_code((const jbyte*)version, (int)strlen(version)));
3669 seed += (jlong)VM_Version::vm_major_version();
3670 seed += (jlong)VM_Version::vm_minor_version();
3671 seed += (jlong)VM_Version::vm_security_version();
3672 seed += (jlong)VM_Version::vm_patch_version();
3673 if (seed == 0) { // don't let this ever be zero.
3674 seed = 0x87654321;
3675 }
3676 log_debug(cds)("JVM_GetRandomSeedForDumping() = " JLONG_FORMAT, seed);
3677 return seed;
3678 } else {
3679 return 0;
3680 }
3681 JVM_END
3682
3683 JVM_ENTRY_NO_ENV_PROF(jint, JVM_GetCDSConfigStatus, JVM_GetCDSConfigStatus())
3684 return CDSConfig::get_status();
3685 JVM_END
3686
3687 JVM_ENTRY_PROF(void, JVM_LogLambdaFormInvoker, JVM_LogLambdaFormInvoker(JNIEnv *env, jstring line))
3688 #if INCLUDE_CDS
3689 assert(CDSConfig::is_logging_lambda_form_invokers(), "sanity");
3690 if (line != nullptr) {
3691 ResourceMark rm(THREAD);
3692 Handle h_line (THREAD, JNIHandles::resolve_non_null(line));
3693 char* c_line = java_lang_String::as_utf8_string(h_line());
3694 if (CDSConfig::is_dumping_dynamic_archive() || CDSConfig::is_dumping_preimage_static_archive()) {
3695 // Note: LambdaFormInvokers::append take same format which is not
3696 // same as below the print format. The line does not include LAMBDA_FORM_TAG.
3697 LambdaFormInvokers::append(os::strdup((const char*)c_line, mtInternal));
3698 }
3699 if (ClassListWriter::is_enabled()) {
3700 ClassListWriter w;
3701 w.stream()->print_cr("%s %s", ClassListParser::lambda_form_tag(), c_line);
3702 }
3703 }
3704 #endif // INCLUDE_CDS
3705 JVM_END
3706
3707 JVM_ENTRY_PROF(void, JVM_LogDynamicProxy, JVM_LogDynamicProxy(JNIEnv *env, jobject loader, jstring proxy_name, jobjectArray interfaces, jint access_flags))
3708 #if INCLUDE_CDS
3709 assert(CDSConfig::is_logging_dynamic_proxies(), "sanity");
3710
3711 ResourceMark rm(THREAD);
3712 oop proxy_name_oop = JNIHandles::resolve_non_null(proxy_name);
3713 char* proxy_name_str = java_lang_String::as_utf8_string(proxy_name_oop);
3714 oop loader_oop = JNIHandles::resolve(loader);
3715 objArrayOop interfaces_oop = objArrayOop(JNIHandles::resolve_non_null(interfaces));
3716
3717 AOTConstantPoolResolver::trace_dynamic_proxy_class(loader_oop, proxy_name_str, interfaces_oop, access_flags);
3718 #endif // INCLUDE_CDS
3719 JVM_END
3720
3721 JVM_ENTRY_PROF(void, JVM_DumpClassListToFile, JVM_DumpClassListToFile(JNIEnv *env, jstring listFileName))
3722 #if INCLUDE_CDS
3723 ResourceMark rm(THREAD);
3724 Handle file_handle(THREAD, JNIHandles::resolve_non_null(listFileName));
3725 char* file_name = java_lang_String::as_utf8_string(file_handle());
3726 MetaspaceShared::dump_loaded_classes(file_name, THREAD);
3727 #endif // INCLUDE_CDS
3728 JVM_END
3729
3730 JVM_ENTRY_PROF(void, JVM_DumpDynamicArchive, JVM_DumpDynamicArchive(JNIEnv *env, jstring archiveName))
3731 #if INCLUDE_CDS
3732 ResourceMark rm(THREAD);
3733 Handle file_handle(THREAD, JNIHandles::resolve_non_null(archiveName));
3734 char* archive_name = java_lang_String::as_utf8_string(file_handle());
3735 DynamicArchive::dump_for_jcmd(archive_name, CHECK);
3736 #endif // INCLUDE_CDS
3737 JVM_END
3738
3739 // Returns an array of all live Thread objects (VM internal JavaThreads,
3740 // jvmti agent threads, and JNI attaching threads are skipped)
3741 // See CR 6404306 regarding JNI attaching threads
3742 JVM_ENTRY_PROF(jobjectArray, JVM_GetAllThreads, JVM_GetAllThreads(JNIEnv *env, jclass dummy))
3743 ResourceMark rm(THREAD);
3744 ThreadsListEnumerator tle(THREAD, false, false);
3745 JvmtiVMObjectAllocEventCollector oam;
3746
3747 int num_threads = tle.num_threads();
3748 objArrayOop r = oopFactory::new_objArray(vmClasses::Thread_klass(), num_threads, CHECK_NULL);
3749 objArrayHandle threads_ah(THREAD, r);
3750
3751 for (int i = 0; i < num_threads; i++) {
3752 Handle h = tle.get_threadObj(i);
3753 threads_ah->obj_at_put(i, h());
3754 }
3755
3756 return (jobjectArray) JNIHandles::make_local(THREAD, threads_ah());
3757 JVM_END
3758
3759
3760 // Support for java.lang.Thread.getStackTrace() and getAllStackTraces() methods
3761 // Return StackTraceElement[][], each element is the stack trace of a thread in
3762 // the corresponding entry in the given threads array
3763 JVM_ENTRY_PROF(jobjectArray, JVM_DumpThreads, JVM_DumpThreads(JNIEnv *env, jclass threadClass, jobjectArray threads))
3764 JvmtiVMObjectAllocEventCollector oam;
3765
3766 // Check if threads is null
3767 if (threads == nullptr) {
3768 THROW_NULL(vmSymbols::java_lang_NullPointerException());
3769 }
3770
3771 objArrayOop a = objArrayOop(JNIHandles::resolve_non_null(threads));
3772 objArrayHandle ah(THREAD, a);
3773 int num_threads = ah->length();
3774 // check if threads is non-empty array
3775 if (num_threads == 0) {
3776 THROW_NULL(vmSymbols::java_lang_IllegalArgumentException());
3777 }
3778
3779 // check if threads is not an array of objects of Thread class
3780 Klass* k = ObjArrayKlass::cast(ah->klass())->element_klass();
3781 if (k != vmClasses::Thread_klass()) {
3782 THROW_NULL(vmSymbols::java_lang_IllegalArgumentException());
3783 }
3784
3785 ResourceMark rm(THREAD);
3786
3787 GrowableArray<instanceHandle>* thread_handle_array = new GrowableArray<instanceHandle>(num_threads);
3788 for (int i = 0; i < num_threads; i++) {
3789 oop thread_obj = ah->obj_at(i);
3790 instanceHandle h(THREAD, (instanceOop) thread_obj);
3791 thread_handle_array->append(h);
3792 }
3793
3794 // The JavaThread references in thread_handle_array are validated
3795 // in VM_ThreadDump::doit().
3796 Handle stacktraces = ThreadService::dump_stack_traces(thread_handle_array, num_threads, CHECK_NULL);
3797 return (jobjectArray)JNIHandles::make_local(THREAD, stacktraces());
3798
3799 JVM_END
3800
3801 // JVM monitoring and management support
3802 JVM_LEAF_PROF(void*, JVM_GetManagement, JVM_GetManagement(jint version))
3803 return Management::get_jmm_interface(version);
3804 JVM_END
3805
3806 // com.sun.tools.attach.VirtualMachine agent properties support
3807 //
3808 // Initialize the agent properties with the properties maintained in the VM
3809 JVM_ENTRY_PROF(jobject, JVM_InitAgentProperties, JVM_InitAgentProperties(JNIEnv *env, jobject properties))
3810 ResourceMark rm;
3811
3812 Handle props(THREAD, JNIHandles::resolve_non_null(properties));
3813
3814 PUTPROP(props, "sun.java.command", Arguments::java_command());
3815 PUTPROP(props, "sun.jvm.flags", Arguments::jvm_flags());
3816 PUTPROP(props, "sun.jvm.args", Arguments::jvm_args());
3817 return properties;
3818 JVM_END
3819
3820 JVM_ENTRY_PROF(jobjectArray, JVM_GetEnclosingMethodInfo, JVM_GetEnclosingMethodInfo(JNIEnv *env, jclass ofClass))
3821 {
3822 JvmtiVMObjectAllocEventCollector oam;
3823
3824 if (ofClass == nullptr) {
3825 return nullptr;
3826 }
3827 Handle mirror(THREAD, JNIHandles::resolve_non_null(ofClass));
3828 // Special handling for primitive objects
3829 if (java_lang_Class::is_primitive(mirror())) {
3830 return nullptr;
3831 }
3832 Klass* k = java_lang_Class::as_Klass(mirror());
3833 if (!k->is_instance_klass()) {
3834 return nullptr;
3835 }
3836 InstanceKlass* ik = InstanceKlass::cast(k);
3837 int encl_method_class_idx = ik->enclosing_method_class_index();
3838 if (encl_method_class_idx == 0) {
3839 return nullptr;
3840 }
3843 Klass* enc_k = ik->constants()->klass_at(encl_method_class_idx, CHECK_NULL);
3844 dest->obj_at_put(0, enc_k->java_mirror());
3845 int encl_method_method_idx = ik->enclosing_method_method_index();
3846 if (encl_method_method_idx != 0) {
3847 Symbol* sym = ik->constants()->symbol_at(
3848 extract_low_short_from_int(
3849 ik->constants()->name_and_type_at(encl_method_method_idx)));
3850 Handle str = java_lang_String::create_from_symbol(sym, CHECK_NULL);
3851 dest->obj_at_put(1, str());
3852 sym = ik->constants()->symbol_at(
3853 extract_high_short_from_int(
3854 ik->constants()->name_and_type_at(encl_method_method_idx)));
3855 str = java_lang_String::create_from_symbol(sym, CHECK_NULL);
3856 dest->obj_at_put(2, str());
3857 }
3858 return (jobjectArray) JNIHandles::make_local(THREAD, dest());
3859 }
3860 JVM_END
3861
3862 // Returns an array of java.lang.String objects containing the input arguments to the VM.
3863 JVM_ENTRY_PROF(jobjectArray, JVM_GetVmArguments, JVM_GetVmArguments(JNIEnv *env))
3864 ResourceMark rm(THREAD);
3865
3866 if (Arguments::num_jvm_args() == 0 && Arguments::num_jvm_flags() == 0) {
3867 return nullptr;
3868 }
3869
3870 char** vm_flags = Arguments::jvm_flags_array();
3871 char** vm_args = Arguments::jvm_args_array();
3872 int num_flags = Arguments::num_jvm_flags();
3873 int num_args = Arguments::num_jvm_args();
3874
3875 InstanceKlass* ik = vmClasses::String_klass();
3876 objArrayOop r = oopFactory::new_objArray(ik, num_args + num_flags, CHECK_NULL);
3877 objArrayHandle result_h(THREAD, r);
3878
3879 int index = 0;
3880 for (int j = 0; j < num_flags; j++, index++) {
3881 Handle h = java_lang_String::create_from_platform_dependent_str(vm_flags[j], CHECK_NULL);
3882 result_h->obj_at_put(index, h());
3883 }
3884 for (int i = 0; i < num_args; i++, index++) {
3885 Handle h = java_lang_String::create_from_platform_dependent_str(vm_args[i], CHECK_NULL);
3886 result_h->obj_at_put(index, h());
3887 }
3888 return (jobjectArray) JNIHandles::make_local(THREAD, result_h());
3889 JVM_END
3890
3891 JVM_LEAF_PROF(jint, JVM_FindSignal, JVM_FindSignal(const char *name))
3892 return os::get_signal_number(name);
3893 JVM_END
3894
3895 JVM_ENTRY_PROF(void, JVM_VirtualThreadStart, JVM_VirtualThreadStart(JNIEnv* env, jobject vthread))
3896 #if INCLUDE_JVMTI
3897 if (!DoJVMTIVirtualThreadTransitions) {
3898 assert(!JvmtiExport::can_support_virtual_threads(), "sanity check");
3899 return;
3900 }
3901 if (JvmtiVTMSTransitionDisabler::VTMS_notify_jvmti_events()) {
3902 JvmtiVTMSTransitionDisabler::VTMS_vthread_start(vthread);
3903 } else {
3904 // set VTMS transition bit value in JavaThread and java.lang.VirtualThread object
3905 JvmtiVTMSTransitionDisabler::set_is_in_VTMS_transition(thread, vthread, false);
3906 }
3907 #endif
3908 JVM_END
3909
3910 JVM_ENTRY_PROF(void, JVM_VirtualThreadEnd, JVM_VirtualThreadEnd(JNIEnv* env, jobject vthread))
3911 #if INCLUDE_JVMTI
3912 if (!DoJVMTIVirtualThreadTransitions) {
3913 assert(!JvmtiExport::can_support_virtual_threads(), "sanity check");
3914 return;
3915 }
3916 if (JvmtiVTMSTransitionDisabler::VTMS_notify_jvmti_events()) {
3917 JvmtiVTMSTransitionDisabler::VTMS_vthread_end(vthread);
3918 } else {
3919 // set VTMS transition bit value in JavaThread and java.lang.VirtualThread object
3920 JvmtiVTMSTransitionDisabler::set_is_in_VTMS_transition(thread, vthread, true);
3921 }
3922 #endif
3923 JVM_END
3924
3925 // If notifications are disabled then just update the VTMS transition bit and return.
3926 // Otherwise, the bit is updated in the given jvmtiVTMSTransitionDisabler function call.
3927 JVM_ENTRY_PROF(void, JVM_VirtualThreadMount, JVM_VirtualThreadMount(JNIEnv* env, jobject vthread, jboolean hide))
3928 #if INCLUDE_JVMTI
3929 if (!DoJVMTIVirtualThreadTransitions) {
3930 assert(!JvmtiExport::can_support_virtual_threads(), "sanity check");
3931 return;
3932 }
3933 if (JvmtiVTMSTransitionDisabler::VTMS_notify_jvmti_events()) {
3934 JvmtiVTMSTransitionDisabler::VTMS_vthread_mount(vthread, hide);
3935 } else {
3936 // set VTMS transition bit value in JavaThread and java.lang.VirtualThread object
3937 JvmtiVTMSTransitionDisabler::set_is_in_VTMS_transition(thread, vthread, hide);
3938 }
3939 #endif
3940 JVM_END
3941
3942 // If notifications are disabled then just update the VTMS transition bit and return.
3943 // Otherwise, the bit is updated in the given jvmtiVTMSTransitionDisabler function call below.
3944 JVM_ENTRY_PROF(void, JVM_VirtualThreadUnmount, JVM_VirtualThreadUnmount(JNIEnv* env, jobject vthread, jboolean hide))
3945 #if INCLUDE_JVMTI
3946 if (!DoJVMTIVirtualThreadTransitions) {
3947 assert(!JvmtiExport::can_support_virtual_threads(), "sanity check");
3948 return;
3949 }
3950 if (JvmtiVTMSTransitionDisabler::VTMS_notify_jvmti_events()) {
3951 JvmtiVTMSTransitionDisabler::VTMS_vthread_unmount(vthread, hide);
3952 } else {
3953 // set VTMS transition bit value in JavaThread and java.lang.VirtualThread object
3954 JvmtiVTMSTransitionDisabler::set_is_in_VTMS_transition(thread, vthread, hide);
3955 }
3956 #endif
3957 JVM_END
3958
3959 // Always update the temporary VTMS transition bit.
3960 JVM_ENTRY_PROF(void, JVM_VirtualThreadHideFrames, JVM_VirtualThreadHideFrames(JNIEnv* env, jclass clazz, jboolean hide))
3961 #if INCLUDE_JVMTI
3962 if (!DoJVMTIVirtualThreadTransitions) {
3963 assert(!JvmtiExport::can_support_virtual_threads(), "sanity check");
3964 return;
3965 }
3966 assert(!thread->is_in_VTMS_transition(), "sanity check");
3967 assert(thread->is_in_tmp_VTMS_transition() != (bool)hide, "sanity check");
3968 thread->toggle_is_in_tmp_VTMS_transition();
3969 #endif
3970 JVM_END
3971
3972 // Notification from VirtualThread about disabling JVMTI Suspend in a sync critical section.
3973 // Needed to avoid deadlocks with JVMTI suspend mechanism.
3974 JVM_ENTRY(void, JVM_VirtualThreadDisableSuspend(JNIEnv* env, jclass clazz, jboolean enter))
3975 #if INCLUDE_JVMTI
3976 if (!DoJVMTIVirtualThreadTransitions) {
3977 assert(!JvmtiExport::can_support_virtual_threads(), "sanity check");
3978 return;
3979 }
3980 assert(thread->is_disable_suspend() != (bool)enter,
3981 "nested or unbalanced monitor enter/exit is not allowed");
3982 thread->toggle_is_disable_suspend();
3983 #endif
3984 JVM_END
3985
3986 /*
3987 * Return the current class's class file version. The low order 16 bits of the
3988 * returned jint contain the class's major version. The high order 16 bits
3989 * contain the class's minor version.
3990 */
3991 JVM_ENTRY_PROF(jint, JVM_GetClassFileVersion, JVM_GetClassFileVersion(JNIEnv* env, jclass current))
3992 oop mirror = JNIHandles::resolve_non_null(current);
3993 if (java_lang_Class::is_primitive(mirror)) {
3994 // return latest major version and minor version of 0.
3995 return JVM_CLASSFILE_MAJOR_VERSION;
3996 }
3997 assert(!java_lang_Class::as_Klass(mirror)->is_array_klass(), "unexpected array class");
3998
3999 Klass* c = java_lang_Class::as_Klass(mirror);
4000 assert(c->is_instance_klass(), "must be");
4001 InstanceKlass* ik = InstanceKlass::cast(c);
4002 return (ik->minor_version() << 16) | ik->major_version();
4003 JVM_END
4004
4005 /*
4006 * Ensure that code doing a stackwalk and using javaVFrame::locals() to
4007 * get the value will see a materialized value and not a scalar-replaced
4008 * null value.
4009 */
4010 JVM_ENTRY_PROF(void, JVM_EnsureMaterializedForStackWalk_func, JVM_EnsureMaterializedForStackWalk_func(JNIEnv* env, jobject vthread, jobject value))
4011 JVM_EnsureMaterializedForStackWalk(env, value);
4012 JVM_END
4013
4014 /*
4015 * Return JNI_TRUE if warnings are printed when agents are dynamically loaded.
4016 */
4017 JVM_LEAF_PROF(jboolean, JVM_PrintWarningAtDynamicAgentLoad, JVM_PrintWarningAtDynamicAgentLoad(void))
4018 return (EnableDynamicAgentLoading && !FLAG_IS_CMDLINE(EnableDynamicAgentLoading)) ? JNI_TRUE : JNI_FALSE;
4019 JVM_END
4020
4021 #define DO_COUNTERS(macro) \
4022 macro(JVM_CurrentTimeMillis) \
4023 macro(JVM_NanoTime) \
4024 macro(JVM_GetNanoTimeAdjustment) \
4025 macro(JVM_ArrayCopy) \
4026 macro(JVM_GetProperties) \
4027 macro(JVM_GetTemporaryDirectory) \
4028 macro(JVM_BeforeHalt) \
4029 macro(JVM_Halt) \
4030 macro(JVM_GC) \
4031 macro(JVM_MaxObjectInspectionAge) \
4032 macro(JVM_TotalMemory) \
4033 macro(JVM_FreeMemory) \
4034 macro(JVM_MaxMemory) \
4035 macro(JVM_ActiveProcessorCount) \
4036 macro(JVM_IsUseContainerSupport) \
4037 macro(JVM_FillInStackTrace) \
4038 macro(JVM_GetExtendedNPEMessage) \
4039 macro(JVM_InitStackTraceElementArray) \
4040 macro(JVM_InitStackTraceElement) \
4041 macro(JVM_ExpandStackFrameInfo) \
4042 macro(JVM_CallStackWalk) \
4043 macro(JVM_MoreStackWalk) \
4044 macro(JVM_SetStackWalkContinuation) \
4045 macro(JVM_IHashCode) \
4046 macro(JVM_MonitorWait) \
4047 macro(JVM_MonitorNotify) \
4048 macro(JVM_MonitorNotifyAll) \
4049 macro(JVM_Clone) \
4050 macro(JVM_ReportFinalizationComplete) \
4051 macro(JVM_IsFinalizationEnabled) \
4052 macro(JVM_RegisterContinuationMethods) \
4053 macro(JVM_NativePath) \
4054 macro(JVM_GetCallerClass) \
4055 macro(JVM_FindPrimitiveClass) \
4056 macro(JVM_FindClassFromBootLoader) \
4057 macro(JVM_FindClassFromCaller) \
4058 macro(JVM_FindClassFromClass) \
4059 macro(JVM_DefineClass) \
4060 macro(JVM_LookupDefineClass) \
4061 macro(JVM_DefineClassWithSource) \
4062 macro(JVM_FindLoadedClass) \
4063 macro(JVM_DefineModule) \
4064 macro(JVM_SetBootLoaderUnnamedModule) \
4065 macro(JVM_AddModuleExports) \
4066 macro(JVM_AddModuleExportsToAllUnnamed) \
4067 macro(JVM_AddModuleExportsToAll) \
4068 macro(JVM_AddReadsModule) \
4069 macro(JVM_DefineArchivedModules) \
4070 macro(JVM_InitClassName) \
4071 macro(JVM_GetClassInterfaces) \
4072 macro(JVM_IsInterface) \
4073 macro(JVM_IsHiddenClass) \
4074 macro(JVM_GetProtectionDomain) \
4075 macro(JVM_GetInheritedAccessControlContext) \
4076 macro(JVM_GetStackAccessControlContext) \
4077 macro(JVM_FindScopedValueBindings) \
4078 macro(JVM_IsArrayClass) \
4079 macro(JVM_IsPrimitiveClass) \
4080 macro(JVM_GetClassModifiers) \
4081 macro(JVM_GetDeclaredClasses) \
4082 macro(JVM_GetDeclaringClass) \
4083 macro(JVM_GetSimpleBinaryName) \
4084 macro(JVM_GetClassSignature) \
4085 macro(JVM_GetClassAnnotations) \
4086 macro(JVM_GetClassTypeAnnotations) \
4087 macro(JVM_GetMethodTypeAnnotations) \
4088 macro(JVM_GetFieldTypeAnnotations) \
4089 macro(JVM_GetMethodParameters) \
4090 macro(JVM_GetClassDeclaredFields) \
4091 macro(JVM_IsRecord) \
4092 macro(JVM_GetRecordComponents) \
4093 macro(JVM_GetClassDeclaredMethods) \
4094 macro(JVM_GetClassDeclaredConstructors) \
4095 macro(JVM_GetClassAccessFlags) \
4096 macro(JVM_AreNestMates) \
4097 macro(JVM_GetNestHost) \
4098 macro(JVM_GetNestMembers) \
4099 macro(JVM_GetPermittedSubclasses) \
4100 macro(JVM_GetClassConstantPool) \
4101 macro(JVM_ConstantPoolGetSize) \
4102 macro(JVM_ConstantPoolGetClassAt) \
4103 macro(JVM_ConstantPoolGetClassAtIfLoaded) \
4104 macro(JVM_ConstantPoolGetMethodAt) \
4105 macro(JVM_ConstantPoolGetMethodAtIfLoaded) \
4106 macro(JVM_ConstantPoolGetFieldAt) \
4107 macro(JVM_ConstantPoolGetFieldAtIfLoaded) \
4108 macro(JVM_ConstantPoolGetMemberRefInfoAt) \
4109 macro(JVM_ConstantPoolGetClassRefIndexAt) \
4110 macro(JVM_ConstantPoolGetNameAndTypeRefIndexAt) \
4111 macro(JVM_ConstantPoolGetNameAndTypeRefInfoAt) \
4112 macro(JVM_ConstantPoolGetIntAt) \
4113 macro(JVM_ConstantPoolGetLongAt) \
4114 macro(JVM_ConstantPoolGetFloatAt) \
4115 macro(JVM_ConstantPoolGetDoubleAt) \
4116 macro(JVM_ConstantPoolGetStringAt) \
4117 macro(JVM_ConstantPoolGetUTF8At) \
4118 macro(JVM_ConstantPoolGetTagAt) \
4119 macro(JVM_DesiredAssertionStatus) \
4120 macro(JVM_AssertionStatusDirectives) \
4121 macro(JVM_GetClassNameUTF) \
4122 macro(JVM_GetClassCPTypes) \
4123 macro(JVM_GetClassCPEntriesCount) \
4124 macro(JVM_GetClassFieldsCount) \
4125 macro(JVM_GetClassMethodsCount) \
4126 macro(JVM_GetMethodIxExceptionIndexes) \
4127 macro(JVM_GetMethodIxExceptionsCount) \
4128 macro(JVM_GetMethodIxByteCode) \
4129 macro(JVM_GetMethodIxByteCodeLength) \
4130 macro(JVM_GetMethodIxExceptionTableEntry) \
4131 macro(JVM_GetMethodIxExceptionTableLength) \
4132 macro(JVM_GetMethodIxModifiers) \
4133 macro(JVM_GetFieldIxModifiers) \
4134 macro(JVM_GetMethodIxLocalsCount) \
4135 macro(JVM_GetMethodIxArgsSize) \
4136 macro(JVM_GetMethodIxMaxStack) \
4137 macro(JVM_IsConstructorIx) \
4138 macro(JVM_IsVMGeneratedMethodIx) \
4139 macro(JVM_GetMethodIxNameUTF) \
4140 macro(JVM_GetMethodIxSignatureUTF) \
4141 macro(JVM_GetCPFieldNameUTF) \
4142 macro(JVM_GetCPMethodNameUTF) \
4143 macro(JVM_GetCPMethodSignatureUTF) \
4144 macro(JVM_GetCPFieldSignatureUTF) \
4145 macro(JVM_GetCPClassNameUTF) \
4146 macro(JVM_GetCPFieldClassNameUTF) \
4147 macro(JVM_GetCPMethodClassNameUTF) \
4148 macro(JVM_GetCPFieldModifiers) \
4149 macro(JVM_GetCPMethodModifiers) \
4150 macro(JVM_ReleaseUTF) \
4151 macro(JVM_IsSameClassPackage) \
4152 macro(JVM_StartThread) \
4153 macro(JVM_SetThreadPriority) \
4154 macro(JVM_Yield) \
4155 macro(JVM_SleepNanos) \
4156 macro(JVM_CurrentCarrierThread) \
4157 macro(JVM_CurrentThread) \
4158 macro(JVM_SetCurrentThread) \
4159 macro(JVM_GetNextThreadIdOffset) \
4160 macro(JVM_Interrupt) \
4161 macro(JVM_HoldsLock) \
4162 macro(JVM_GetStackTrace) \
4163 macro(JVM_SetNativeThreadName) \
4164 macro(JVM_ScopedValueCache) \
4165 macro(JVM_SetScopedValueCache) \
4166 macro(JVM_GetClassContext) \
4167 macro(JVM_GetSystemPackage) \
4168 macro(JVM_GetSystemPackages) \
4169 macro(JVM_GetAndClearReferencePendingList) \
4170 macro(JVM_HasReferencePendingList) \
4171 macro(JVM_WaitForReferencePendingList) \
4172 macro(JVM_ReferenceRefersTo) \
4173 macro(JVM_ReferenceClear) \
4174 macro(JVM_PhantomReferenceRefersTo) \
4175 macro(JVM_LatestUserDefinedLoader) \
4176 macro(JVM_GetArrayLength) \
4177 macro(JVM_GetArrayElement) \
4178 macro(JVM_GetPrimitiveArrayElement) \
4179 macro(JVM_SetArrayElement) \
4180 macro(JVM_SetPrimitiveArrayElement) \
4181 macro(JVM_NewArray) \
4182 macro(JVM_NewMultiArray) \
4183 macro(JVM_LoadZipLibrary) \
4184 macro(JVM_LoadLibrary) \
4185 macro(JVM_UnloadLibrary) \
4186 macro(JVM_FindLibraryEntry) \
4187 macro(JVM_IsSupportedJNIVersion) \
4188 macro(JVM_IsPreviewEnabled) \
4189 macro(JVM_IsContinuationsSupported) \
4190 macro(JVM_IsForeignLinkerSupported) \
4191 macro(JVM_InternString) \
4192 macro(JVM_InvokeMethod) \
4193 macro(JVM_NewInstanceFromConstructor) \
4194 macro(JVM_InitializeFromArchive) \
4195 macro(JVM_RegisterLambdaProxyClassForArchiving) \
4196 macro(JVM_LookupLambdaProxyClassFromArchive) \
4197 macro(JVM_GetRandomSeedForDumping) \
4198 macro(JVM_LogLambdaFormInvoker) \
4199 macro(JVM_LogDynamicProxy) \
4200 macro(JVM_DumpClassListToFile) \
4201 macro(JVM_DumpDynamicArchive) \
4202 macro(JVM_GetAllThreads) \
4203 macro(JVM_DumpThreads) \
4204 macro(JVM_GetManagement) \
4205 macro(JVM_InitAgentProperties) \
4206 macro(JVM_GetEnclosingMethodInfo) \
4207 macro(JVM_GetVmArguments) \
4208 macro(JVM_FindSignal) \
4209 macro(JVM_VirtualThreadStart) \
4210 macro(JVM_VirtualThreadEnd) \
4211 macro(JVM_VirtualThreadMount) \
4212 macro(JVM_VirtualThreadUnmount) \
4213 macro(JVM_VirtualThreadHideFrames) \
4214 macro(JVM_GetClassFileVersion) \
4215 macro(JVM_EnsureMaterializedForStackWalk_func) \
4216 macro(JVM_PrintWarningAtDynamicAgentLoad)
4217
4218 #define INIT_COUNTER(name) \
4219 NEWPERFTICKCOUNTERS(_perf_##name##_timer, SUN_RT, #name); \
4220 NEWPERFEVENTCOUNTER(_perf_##name##_count, SUN_RT, #name "_count"); \
4221
4222 void perf_jvm_init() {
4223 if (ProfileVMCalls && UsePerfData) {
4224 EXCEPTION_MARK;
4225
4226 DO_COUNTERS(INIT_COUNTER)
4227
4228 if (HAS_PENDING_EXCEPTION) {
4229 vm_exit_during_initialization("jvm_perf_init failed unexpectedly");
4230 }
4231 }
4232 }
4233
4234 #undef INIT_COUNTER
4235
4236 #define PRINT_COUNTER(name) {\
4237 jlong count = _perf_##name##_count->get_value(); \
4238 if (count > 0) { \
4239 st->print_cr(" %-40s = " JLONG_FORMAT_W(6) "us (elapsed) " JLONG_FORMAT_W(6) "us (thread) (" JLONG_FORMAT_W(5) " events)", \
4240 #name, \
4241 _perf_##name##_timer->elapsed_counter_value_us(), \
4242 _perf_##name##_timer->thread_counter_value_us(), count); \
4243 }}
4244
4245 void perf_jvm_print_on(outputStream* st) {
4246 if (ProfileVMCalls && UsePerfData) {
4247 DO_COUNTERS(PRINT_COUNTER)
4248 } else {
4249 st->print_cr(" JVM: no info (%s is disabled)", (UsePerfData ? "ProfileVMCalls" : "UsePerfData"));
4250 }
4251 }
4252
4253 #undef PRINT_COUNTER
|