/*
* Copyright (c) 1997, 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*
*/
#include "precompiled.hpp"
#include "cds/classListParser.hpp"
#include "cds/classListWriter.hpp"
#include "cds/dynamicArchive.hpp"
#include "cds/heapShared.hpp"
#include "cds/lambdaFormInvokers.hpp"
#include "classfile/classFileStream.hpp"
#include "classfile/classLoader.inline.hpp"
#include "classfile/classLoaderData.hpp"
#include "classfile/classLoaderData.inline.hpp"
#include "classfile/classLoadInfo.hpp"
#include "classfile/javaAssertions.hpp"
#include "classfile/javaClasses.inline.hpp"
#include "classfile/moduleEntry.hpp"
#include "classfile/modules.hpp"
#include "classfile/packageEntry.hpp"
#include "classfile/stringTable.hpp"
#include "classfile/symbolTable.hpp"
#include "classfile/systemDictionary.hpp"
#include "classfile/vmClasses.hpp"
#include "classfile/vmSymbols.hpp"
#include "gc/shared/collectedHeap.inline.hpp"
#include "interpreter/bytecode.hpp"
#include "interpreter/bytecodeUtils.hpp"
#include "jfr/jfrEvents.hpp"
#include "jvm.h"
#include "logging/log.hpp"
#include "memory/oopFactory.hpp"
#include "memory/referenceType.hpp"
#include "memory/resourceArea.hpp"
#include "memory/universe.hpp"
#include "oops/access.inline.hpp"
#include "oops/constantPool.hpp"
#include "oops/fieldStreams.inline.hpp"
#include "oops/instanceKlass.hpp"
#include "oops/klass.inline.hpp"
#include "oops/method.hpp"
#include "oops/recordComponent.hpp"
#include "oops/objArrayKlass.hpp"
#include "oops/objArrayOop.inline.hpp"
#include "oops/oop.inline.hpp"
#include "prims/jvm_misc.hpp"
#include "prims/jvmtiExport.hpp"
#include "prims/jvmtiThreadState.inline.hpp"
#include "prims/stackwalk.hpp"
#include "runtime/arguments.hpp"
#include "runtime/atomic.hpp"
#include "runtime/continuation.hpp"
#include "runtime/globals_extension.hpp"
#include "runtime/handles.inline.hpp"
#include "runtime/init.hpp"
#include "runtime/interfaceSupport.inline.hpp"
#include "runtime/deoptimization.hpp"
#include "runtime/handshake.hpp"
#include "runtime/java.hpp"
#include "runtime/javaCalls.hpp"
#include "runtime/javaThread.hpp"
#include "runtime/jfieldIDWorkaround.hpp"
#include "runtime/jniHandles.inline.hpp"
#include "runtime/os.inline.hpp"
#include "runtime/osThread.hpp"
#include "runtime/perfData.hpp"
#include "runtime/reflection.hpp"
#include "runtime/synchronizer.hpp"
#include "runtime/threadIdentifier.hpp"
#include "runtime/threadSMR.hpp"
#include "runtime/vframe.inline.hpp"
#include "runtime/vmOperations.hpp"
#include "runtime/vm_version.hpp"
#include "services/attachListener.hpp"
#include "services/management.hpp"
#include "services/threadService.hpp"
#include "utilities/copy.hpp"
#include "utilities/defaultStream.hpp"
#include "utilities/dtrace.hpp"
#include "utilities/events.hpp"
#include "utilities/macros.hpp"
#include "utilities/utf8.hpp"
#if INCLUDE_CDS
#include "classfile/systemDictionaryShared.hpp"
#endif
#if INCLUDE_JFR
#include "jfr/jfr.hpp"
#endif
#if INCLUDE_MANAGEMENT
#include "services/finalizerService.hpp"
#endif
#include <errno.h>
/*
NOTE about use of any ctor or function call that can trigger a safepoint/GC:
such ctors and calls MUST NOT come between an oop declaration/init and its
usage because if objects are move this may cause various memory stomps, bus
errors and segfaults. Here is a cookbook for causing so called "naked oop
failures":
JVM_ENTRY(jobjectArray, JVM_GetClassDeclaredFields<etc> {
// Object address to be held directly in mirror & not visible to GC
oop mirror = JNIHandles::resolve_non_null(ofClass);
// If this ctor can hit a safepoint, moving objects around, then
ComplexConstructor foo;
// Boom! mirror may point to JUNK instead of the intended object
(some dereference of mirror)
// Here's another call that may block for GC, making mirror stale
MutexLocker ml(some_lock);
// And here's an initializer that can result in a stale oop
// all in one step.
oop o = call_that_can_throw_exception(TRAPS);
The solution is to keep the oop declaration BELOW the ctor or function
call that might cause a GC, do another resolve to reassign the oop, or
consider use of a Handle instead of an oop so there is immunity from object
motion. But note that the "QUICK" entries below do not have a handlemark
and thus can only support use of handles passed in.
*/
static void trace_class_resolution_impl(Klass* to_class, TRAPS) {
ResourceMark rm;
int line_number = -1;
const char * source_file = NULL;
const char * trace = "explicit";
InstanceKlass* caller = NULL;
JavaThread* jthread = THREAD;
if (jthread->has_last_Java_frame()) {
vframeStream vfst(jthread);
// scan up the stack skipping ClassLoader, AccessController and PrivilegedAction frames
TempNewSymbol access_controller = SymbolTable::new_symbol("java/security/AccessController");
Klass* access_controller_klass = SystemDictionary::resolve_or_fail(access_controller, false, CHECK);
TempNewSymbol privileged_action = SymbolTable::new_symbol("java/security/PrivilegedAction");
Klass* privileged_action_klass = SystemDictionary::resolve_or_fail(privileged_action, false, CHECK);
Method* last_caller = NULL;
while (!vfst.at_end()) {
Method* m = vfst.method();
if (!vfst.method()->method_holder()->is_subclass_of(vmClasses::ClassLoader_klass())&&
!vfst.method()->method_holder()->is_subclass_of(access_controller_klass) &&
!vfst.method()->method_holder()->is_subclass_of(privileged_action_klass)) {
break;
}
last_caller = m;
vfst.next();
}
// if this is called from Class.forName0 and that is called from Class.forName,
// then print the caller of Class.forName. If this is Class.loadClass, then print
// that caller, otherwise keep quiet since this should be picked up elsewhere.
bool found_it = false;
if (!vfst.at_end() &&
vfst.method()->method_holder()->name() == vmSymbols::java_lang_Class() &&
vfst.method()->name() == vmSymbols::forName0_name()) {
vfst.next();
if (!vfst.at_end() &&
vfst.method()->method_holder()->name() == vmSymbols::java_lang_Class() &&
vfst.method()->name() == vmSymbols::forName_name()) {
vfst.next();
found_it = true;
}
} else if (last_caller != NULL &&
last_caller->method_holder()->name() ==
vmSymbols::java_lang_ClassLoader() &&
last_caller->name() == vmSymbols::loadClass_name()) {
found_it = true;
} else if (!vfst.at_end()) {
if (vfst.method()->is_native()) {
// JNI call
found_it = true;
}
}
if (found_it && !vfst.at_end()) {
// found the caller
caller = vfst.method()->method_holder();
line_number = vfst.method()->line_number_from_bci(vfst.bci());
if (line_number == -1) {
// show method name if it's a native method
trace = vfst.method()->name_and_sig_as_C_string();
}
Symbol* s = caller->source_file_name();
if (s != NULL) {
source_file = s->as_C_string();
}
}
}
if (caller != NULL) {
if (to_class != caller) {
const char * from = caller->external_name();
const char * to = to_class->external_name();
// print in a single call to reduce interleaving between threads
if (source_file != NULL) {
log_debug(class, resolve)("%s %s %s:%d (%s)", from, to, source_file, line_number, trace);
} else {
log_debug(class, resolve)("%s %s (%s)", from, to, trace);
}
}
}
}
void trace_class_resolution(Klass* to_class) {
EXCEPTION_MARK;
trace_class_resolution_impl(to_class, THREAD);
if (HAS_PENDING_EXCEPTION) {
CLEAR_PENDING_EXCEPTION;
}
}
// java.lang.System //////////////////////////////////////////////////////////////////////
JVM_LEAF(jlong, JVM_CurrentTimeMillis(JNIEnv *env, jclass ignored))
return os::javaTimeMillis();
JVM_END
JVM_LEAF(jlong, JVM_NanoTime(JNIEnv *env, jclass ignored))
return os::javaTimeNanos();
JVM_END
// The function below is actually exposed by jdk.internal.misc.VM and not
// java.lang.System, but we choose to keep it here so that it stays next
// to JVM_CurrentTimeMillis and JVM_NanoTime
const jlong MAX_DIFF_SECS = CONST64(0x0100000000); // 2^32
const jlong MIN_DIFF_SECS = -MAX_DIFF_SECS; // -2^32
JVM_LEAF(jlong, JVM_GetNanoTimeAdjustment(JNIEnv *env, jclass ignored, jlong offset_secs))
jlong seconds;
jlong nanos;
os::javaTimeSystemUTC(seconds, nanos);
// We're going to verify that the result can fit in a long.
// For that we need the difference in seconds between 'seconds'
// and 'offset_secs' to be such that:
// |seconds - offset_secs| < (2^63/10^9)
// We're going to approximate 10^9 ~< 2^30 (1000^3 ~< 1024^3)
// which makes |seconds - offset_secs| < 2^33
// and we will prefer +/- 2^32 as the maximum acceptable diff
// as 2^32 has a more natural feel than 2^33...
//
// So if |seconds - offset_secs| >= 2^32 - we return a special
// sentinel value (-1) which the caller should take as an
// exception value indicating that the offset given to us is
// too far from range of the current time - leading to too big
// a nano adjustment. The caller is expected to recover by
// computing a more accurate offset and calling this method
// again. (For the record 2^32 secs is ~136 years, so that
// should rarely happen)
//
jlong diff = seconds - offset_secs;
if (diff >= MAX_DIFF_SECS || diff <= MIN_DIFF_SECS) {
return -1; // sentinel value: the offset is too far off the target
}
// return the adjustment. If you compute a time by adding
// this number of nanoseconds along with the number of seconds
// in the offset you should get the current UTC time.
return (diff * (jlong)1000000000) + nanos;
JVM_END
JVM_ENTRY(void, JVM_ArrayCopy(JNIEnv *env, jclass ignored, jobject src, jint src_pos,
jobject dst, jint dst_pos, jint length))
// Check if we have null pointers
if (src == NULL || dst == NULL) {
THROW(vmSymbols::java_lang_NullPointerException());
}
arrayOop s = arrayOop(JNIHandles::resolve_non_null(src));
arrayOop d = arrayOop(JNIHandles::resolve_non_null(dst));
assert(oopDesc::is_oop(s), "JVM_ArrayCopy: src not an oop");
assert(oopDesc::is_oop(d), "JVM_ArrayCopy: dst not an oop");
// Do copy
s->klass()->copy_array(s, src_pos, d, dst_pos, length, thread);
JVM_END
static void set_property(Handle props, const char* key, const char* value, TRAPS) {
JavaValue r(T_OBJECT);
// public synchronized Object put(Object key, Object value);
HandleMark hm(THREAD);
Handle key_str = java_lang_String::create_from_platform_dependent_str(key, CHECK);
Handle value_str = java_lang_String::create_from_platform_dependent_str((value != NULL ? value : ""), CHECK);
JavaCalls::call_virtual(&r,
props,
vmClasses::Properties_klass(),
vmSymbols::put_name(),
vmSymbols::object_object_object_signature(),
key_str,
value_str,
THREAD);
}
#define PUTPROP(props, name, value) set_property((props), (name), (value), CHECK_(properties));
/*
* Return all of the system properties in a Java String array with alternating
* names and values from the jvm SystemProperty.
* Which includes some internal and all commandline -D defined properties.
*/
JVM_ENTRY(jobjectArray, JVM_GetProperties(JNIEnv *env))
ResourceMark rm(THREAD);
HandleMark hm(THREAD);
int ndx = 0;
int fixedCount = 2;
SystemProperty* p = Arguments::system_properties();
int count = Arguments::PropertyList_count(p);
// Allocate result String array
InstanceKlass* ik = vmClasses::String_klass();
objArrayOop r = oopFactory::new_objArray(ik, (count + fixedCount) * 2, CHECK_NULL);
objArrayHandle result_h(THREAD, r);
while (p != NULL) {
const char * key = p->key();
if (strcmp(key, "sun.nio.MaxDirectMemorySize") != 0) {
const char * value = p->value();
Handle key_str = java_lang_String::create_from_platform_dependent_str(key, CHECK_NULL);
Handle value_str = java_lang_String::create_from_platform_dependent_str((value != NULL ? value : ""), CHECK_NULL);
result_h->obj_at_put(ndx * 2, key_str());
result_h->obj_at_put(ndx * 2 + 1, value_str());
ndx++;
}
p = p->next();
}
// Convert the -XX:MaxDirectMemorySize= command line flag
// to the sun.nio.MaxDirectMemorySize property.
// Do this after setting user properties to prevent people
// from setting the value with a -D option, as requested.
// Leave empty if not supplied
if (!FLAG_IS_DEFAULT(MaxDirectMemorySize)) {
char as_chars[256];
jio_snprintf(as_chars, sizeof(as_chars), JULONG_FORMAT, MaxDirectMemorySize);
Handle key_str = java_lang_String::create_from_platform_dependent_str("sun.nio.MaxDirectMemorySize", CHECK_NULL);
Handle value_str = java_lang_String::create_from_platform_dependent_str(as_chars, CHECK_NULL);
result_h->obj_at_put(ndx * 2, key_str());
result_h->obj_at_put(ndx * 2 + 1, value_str());
ndx++;
}
// JVM monitoring and management support
// Add the sun.management.compiler property for the compiler's name
{
#undef CSIZE
#if defined(_LP64) || defined(_WIN64)
#define CSIZE "64-Bit "
#else
#define CSIZE
#endif // 64bit
#if COMPILER1_AND_COMPILER2
const char* compiler_name = "HotSpot " CSIZE "Tiered Compilers";
#else
#if defined(COMPILER1)
const char* compiler_name = "HotSpot " CSIZE "Client Compiler";
#elif defined(COMPILER2)
const char* compiler_name = "HotSpot " CSIZE "Server Compiler";
#elif INCLUDE_JVMCI
#error "INCLUDE_JVMCI should imply COMPILER1_OR_COMPILER2"
#else
const char* compiler_name = "";
#endif // compilers
#endif // COMPILER1_AND_COMPILER2
if (*compiler_name != '\0' &&
(Arguments::mode() != Arguments::_int)) {
Handle key_str = java_lang_String::create_from_platform_dependent_str("sun.management.compiler", CHECK_NULL);
Handle value_str = java_lang_String::create_from_platform_dependent_str(compiler_name, CHECK_NULL);
result_h->obj_at_put(ndx * 2, key_str());
result_h->obj_at_put(ndx * 2 + 1, value_str());
ndx++;
}
}
return (jobjectArray) JNIHandles::make_local(THREAD, result_h());
JVM_END
/*
* Return the temporary directory that the VM uses for the attach
* and perf data files.
*
* It is important that this directory is well-known and the
* same for all VM instances. It cannot be affected by configuration
* variables such as java.io.tmpdir.
*/
JVM_ENTRY(jstring, JVM_GetTemporaryDirectory(JNIEnv *env))
HandleMark hm(THREAD);
const char* temp_dir = os::get_temp_directory();
Handle h = java_lang_String::create_from_platform_dependent_str(temp_dir, CHECK_NULL);
return (jstring) JNIHandles::make_local(THREAD, h());
JVM_END
// java.lang.Runtime /////////////////////////////////////////////////////////////////////////
extern volatile jint vm_created;
JVM_ENTRY_NO_ENV(void, JVM_BeforeHalt())
#if INCLUDE_CDS
// Link all classes for dynamic CDS dumping before vm exit.
if (DynamicArchive::should_dump_at_vm_exit()) {
DynamicArchive::prepare_for_dump_at_exit();
}
#endif
EventShutdown event;
if (event.should_commit()) {
event.set_reason("Shutdown requested from Java");
event.commit();
}
JVM_END
JVM_ENTRY_NO_ENV(void, JVM_Halt(jint code))
before_exit(thread, true);
vm_exit(code);
JVM_END
JVM_ENTRY_NO_ENV(void, JVM_GC(void))
if (!DisableExplicitGC) {
EventSystemGC event;
event.set_invokedConcurrent(ExplicitGCInvokesConcurrent);
Universe::heap()->collect(GCCause::_java_lang_system_gc);
event.commit();
}
JVM_END
JVM_LEAF(jlong, JVM_MaxObjectInspectionAge(void))
return Universe::heap()->millis_since_last_whole_heap_examined();
JVM_END
static inline jlong convert_size_t_to_jlong(size_t val) {
// In the 64-bit vm, a size_t can overflow a jlong (which is signed).
NOT_LP64 (return (jlong)val;)
LP64_ONLY(return (jlong)MIN2(val, (size_t)max_jlong);)
}
JVM_ENTRY_NO_ENV(jlong, JVM_TotalMemory(void))
size_t n = Universe::heap()->capacity();
return convert_size_t_to_jlong(n);
JVM_END
JVM_ENTRY_NO_ENV(jlong, JVM_FreeMemory(void))
size_t n = Universe::heap()->unused();
return convert_size_t_to_jlong(n);
JVM_END
JVM_ENTRY_NO_ENV(jlong, JVM_MaxMemory(void))
size_t n = Universe::heap()->max_capacity();
return convert_size_t_to_jlong(n);
JVM_END
JVM_ENTRY_NO_ENV(jint, JVM_ActiveProcessorCount(void))
return os::active_processor_count();
JVM_END
JVM_LEAF(jboolean, JVM_IsUseContainerSupport(void))
#ifdef LINUX
if (UseContainerSupport) {
return JNI_TRUE;
}
#endif
return JNI_FALSE;
JVM_END
// java.lang.Throwable //////////////////////////////////////////////////////
JVM_ENTRY(void, JVM_FillInStackTrace(JNIEnv *env, jobject receiver))
Handle exception(thread, JNIHandles::resolve_non_null(receiver));
java_lang_Throwable::fill_in_stack_trace(exception);
JVM_END
// java.lang.NullPointerException ///////////////////////////////////////////
JVM_ENTRY(jstring, JVM_GetExtendedNPEMessage(JNIEnv *env, jthrowable throwable))
if (!ShowCodeDetailsInExceptionMessages) return NULL;
oop exc = JNIHandles::resolve_non_null(throwable);
Method* method;
int bci;
if (!java_lang_Throwable::get_top_method_and_bci(exc, &method, &bci)) {
return NULL;
}
if (method->is_native()) {
return NULL;
}
stringStream ss;
bool ok = BytecodeUtils::get_NPE_message_at(&ss, method, bci);
if (ok) {
oop result = java_lang_String::create_oop_from_str(ss.base(), CHECK_NULL);
return (jstring) JNIHandles::make_local(THREAD, result);
} else {
return NULL;
}
JVM_END
// java.lang.StackTraceElement //////////////////////////////////////////////
JVM_ENTRY(void, JVM_InitStackTraceElementArray(JNIEnv *env, jobjectArray elements, jobject backtrace, jint depth))
Handle backtraceh(THREAD, JNIHandles::resolve(backtrace));
objArrayOop st = objArrayOop(JNIHandles::resolve(elements));
objArrayHandle stack_trace(THREAD, st);
// Fill in the allocated stack trace
java_lang_Throwable::get_stack_trace_elements(depth, backtraceh, stack_trace, CHECK);
JVM_END
JVM_ENTRY(void, JVM_InitStackTraceElement(JNIEnv* env, jobject element, jobject stackFrameInfo))
Handle stack_frame_info(THREAD, JNIHandles::resolve_non_null(stackFrameInfo));
Handle stack_trace_element(THREAD, JNIHandles::resolve_non_null(element));
java_lang_StackFrameInfo::to_stack_trace_element(stack_frame_info, stack_trace_element, THREAD);
JVM_END
// java.lang.StackWalker //////////////////////////////////////////////////////
JVM_ENTRY(jobject, JVM_CallStackWalk(JNIEnv *env, jobject stackStream, jlong mode,
jint skip_frames, jobject contScope, jobject cont,
jint frame_count, jint start_index, jobjectArray frames))
if (!thread->has_last_Java_frame()) {
THROW_MSG_(vmSymbols::java_lang_InternalError(), "doStackWalk: no stack trace", NULL);
}
Handle stackStream_h(THREAD, JNIHandles::resolve_non_null(stackStream));
Handle contScope_h(THREAD, JNIHandles::resolve(contScope));
Handle cont_h(THREAD, JNIHandles::resolve(cont));
// frames array is a Class<?>[] array when only getting caller reference,
// and a StackFrameInfo[] array (or derivative) otherwise. It should never
// be null.
objArrayOop fa = objArrayOop(JNIHandles::resolve_non_null(frames));
objArrayHandle frames_array_h(THREAD, fa);
int limit = start_index + frame_count;
if (frames_array_h->length() < limit) {
THROW_MSG_(vmSymbols::java_lang_IllegalArgumentException(), "not enough space in buffers", NULL);
}
oop result = StackWalk::walk(stackStream_h, mode, skip_frames, contScope_h, cont_h,
frame_count, start_index, frames_array_h, CHECK_NULL);
return JNIHandles::make_local(THREAD, result);
JVM_END
JVM_ENTRY(jint, JVM_MoreStackWalk(JNIEnv *env, jobject stackStream, jlong mode, jlong anchor,
jint frame_count, jint start_index,
jobjectArray frames))
// frames array is a Class<?>[] array when only getting caller reference,
// and a StackFrameInfo[] array (or derivative) otherwise. It should never
// be null.
objArrayOop fa = objArrayOop(JNIHandles::resolve_non_null(frames));
objArrayHandle frames_array_h(THREAD, fa);
int limit = start_index+frame_count;
if (frames_array_h->length() < limit) {
THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(), "not enough space in buffers");
}
Handle stackStream_h(THREAD, JNIHandles::resolve_non_null(stackStream));
return StackWalk::fetchNextBatch(stackStream_h, mode, anchor, frame_count,
start_index, frames_array_h, THREAD);
JVM_END
JVM_ENTRY(void, JVM_SetStackWalkContinuation(JNIEnv *env, jobject stackStream, jlong anchor, jobjectArray frames, jobject cont))
objArrayOop fa = objArrayOop(JNIHandles::resolve_non_null(frames));
objArrayHandle frames_array_h(THREAD, fa);
Handle stackStream_h(THREAD, JNIHandles::resolve_non_null(stackStream));
Handle cont_h(THREAD, JNIHandles::resolve_non_null(cont));
StackWalk::setContinuation(stackStream_h, anchor, frames_array_h, cont_h, THREAD);
JVM_END
// java.lang.Object ///////////////////////////////////////////////
JVM_ENTRY(jint, JVM_IHashCode(JNIEnv* env, jobject handle))
// as implemented in the classic virtual machine; return 0 if object is NULL
return handle == NULL ? 0 : ObjectSynchronizer::FastHashCode (THREAD, JNIHandles::resolve_non_null(handle)) ;
JVM_END
JVM_ENTRY(void, JVM_MonitorWait(JNIEnv* env, jobject handle, jlong ms))
Handle obj(THREAD, JNIHandles::resolve_non_null(handle));
JavaThreadInObjectWaitState jtiows(thread, ms != 0);
if (JvmtiExport::should_post_monitor_wait()) {
JvmtiExport::post_monitor_wait(thread, obj(), ms);
// The current thread already owns the monitor and it has not yet
// been added to the wait queue so the current thread cannot be
// made the successor. This means that the JVMTI_EVENT_MONITOR_WAIT
// event handler cannot accidentally consume an unpark() meant for
// the ParkEvent associated with this ObjectMonitor.
}
ObjectSynchronizer::wait(obj, ms, CHECK);
JVM_END
JVM_ENTRY(void, JVM_MonitorNotify(JNIEnv* env, jobject handle))
Handle obj(THREAD, JNIHandles::resolve_non_null(handle));
ObjectSynchronizer::notify(obj, CHECK);
JVM_END
JVM_ENTRY(void, JVM_MonitorNotifyAll(JNIEnv* env, jobject handle))
Handle obj(THREAD, JNIHandles::resolve_non_null(handle));
ObjectSynchronizer::notifyall(obj, CHECK);
JVM_END
JVM_ENTRY(jobject, JVM_Clone(JNIEnv* env, jobject handle))
Handle obj(THREAD, JNIHandles::resolve_non_null(handle));
Klass* klass = obj->klass();
JvmtiVMObjectAllocEventCollector oam;
#ifdef ASSERT
// Just checking that the cloneable flag is set correct
if (obj->is_array()) {
guarantee(klass->is_cloneable(), "all arrays are cloneable");
} else {
guarantee(obj->is_instance(), "should be instanceOop");
bool cloneable = klass->is_subtype_of(vmClasses::Cloneable_klass());
guarantee(cloneable == klass->is_cloneable(), "incorrect cloneable flag");
}
#endif
// Check if class of obj supports the Cloneable interface.
// All arrays are considered to be cloneable (See JLS 20.1.5).
// All j.l.r.Reference classes are considered non-cloneable.
if (!klass->is_cloneable() ||
(klass->is_instance_klass() &&
InstanceKlass::cast(klass)->reference_type() != REF_NONE)) {
ResourceMark rm(THREAD);
THROW_MSG_0(vmSymbols::java_lang_CloneNotSupportedException(), klass->external_name());
}
// Make shallow object copy
const size_t size = obj->size();
oop new_obj_oop = NULL;
if (obj->is_array()) {
const int length = ((arrayOop)obj())->length();
new_obj_oop = Universe::heap()->array_allocate(klass, size, length,
/* do_zero */ true, CHECK_NULL);
} else {
new_obj_oop = Universe::heap()->obj_allocate(klass, size, CHECK_NULL);
}
HeapAccess<>::clone(obj(), new_obj_oop, size);
Handle new_obj(THREAD, new_obj_oop);
// Caution: this involves a java upcall, so the clone should be
// "gc-robust" by this stage.
if (klass->has_finalizer()) {
assert(obj->is_instance(), "should be instanceOop");
new_obj_oop = InstanceKlass::register_finalizer(instanceOop(new_obj()), CHECK_NULL);
new_obj = Handle(THREAD, new_obj_oop);
}
return JNIHandles::make_local(THREAD, new_obj());
JVM_END
// java.lang.ref.Finalizer ////////////////////////////////////////////////////
JVM_ENTRY(void, JVM_ReportFinalizationComplete(JNIEnv * env, jobject finalizee))
MANAGEMENT_ONLY(FinalizerService::on_complete(JNIHandles::resolve_non_null(finalizee), THREAD);)
JVM_END
JVM_LEAF(jboolean, JVM_IsFinalizationEnabled(JNIEnv * env))
return InstanceKlass::is_finalization_enabled();
JVM_END
// jdk.internal.vm.Continuation /////////////////////////////////////////////////////
JVM_ENTRY(void, JVM_RegisterContinuationMethods(JNIEnv *env, jclass cls))
CONT_RegisterNativeMethods(env, cls);
JVM_END
// java.io.File ///////////////////////////////////////////////////////////////
JVM_LEAF(char*, JVM_NativePath(char* path))
return os::native_path(path);
JVM_END
// Misc. class handling ///////////////////////////////////////////////////////////
JVM_ENTRY(jclass, JVM_GetCallerClass(JNIEnv* env))
// Getting the class of the caller frame.
//
// The call stack at this point looks something like this:
//
// [0] [ @CallerSensitive public sun.reflect.Reflection.getCallerClass ]
// [1] [ @CallerSensitive API.method ]
// [.] [ (skipped intermediate frames) ]
// [n] [ caller ]
vframeStream vfst(thread);
// Cf. LibraryCallKit::inline_native_Reflection_getCallerClass
for (int n = 0; !vfst.at_end(); vfst.security_next(), n++) {
Method* m = vfst.method();
assert(m != NULL, "sanity");
switch (n) {
case 0:
// This must only be called from Reflection.getCallerClass
if (m->intrinsic_id() != vmIntrinsics::_getCallerClass) {
THROW_MSG_NULL(vmSymbols::java_lang_InternalError(), "JVM_GetCallerClass must only be called from Reflection.getCallerClass");
}
// fall-through
case 1:
// Frame 0 and 1 must be caller sensitive.
if (!m->caller_sensitive()) {
THROW_MSG_NULL(vmSymbols::java_lang_InternalError(), err_msg("CallerSensitive annotation expected at frame %d", n));
}
break;
default:
if (!m->is_ignored_by_security_stack_walk()) {
// We have reached the desired frame; return the holder class.
return (jclass) JNIHandles::make_local(THREAD, m->method_holder()->java_mirror());
}
break;
}
}
return NULL;
JVM_END
JVM_ENTRY(jclass, JVM_FindPrimitiveClass(JNIEnv* env, const char* utf))
oop mirror = NULL;
BasicType t = name2type(utf);
if (t != T_ILLEGAL && !is_reference_type(t)) {
mirror = Universe::java_mirror(t);
}
if (mirror == NULL) {
THROW_MSG_0(vmSymbols::java_lang_ClassNotFoundException(), (char*) utf);
} else {
return (jclass) JNIHandles::make_local(THREAD, mirror);
}
JVM_END
// Returns a class loaded by the bootstrap class loader; or null
// if not found. ClassNotFoundException is not thrown.
// FindClassFromBootLoader is exported to the launcher for windows.
JVM_ENTRY(jclass, JVM_FindClassFromBootLoader(JNIEnv* env,
const char* name))
// Java libraries should ensure that name is never null or illegal.
if (name == NULL || (int)strlen(name) > Symbol::max_length()) {
// It's impossible to create this class; the name cannot fit
// into the constant pool.
return NULL;
}
assert(UTF8::is_legal_utf8((const unsigned char*)name, (int)strlen(name), false), "illegal UTF name");
TempNewSymbol h_name = SymbolTable::new_symbol(name);
Klass* k = SystemDictionary::resolve_or_null(h_name, CHECK_NULL);
if (k == NULL) {
return NULL;
}
if (log_is_enabled(Debug, class, resolve)) {
trace_class_resolution(k);
}
return (jclass) JNIHandles::make_local(THREAD, k->java_mirror());
JVM_END
// Find a class with this name in this loader, using the caller's protection domain.
JVM_ENTRY(jclass, JVM_FindClassFromCaller(JNIEnv* env, const char* name,
jboolean init, jobject loader,
jclass caller))
TempNewSymbol h_name =
SystemDictionary::class_name_symbol(name, vmSymbols::java_lang_ClassNotFoundException(),
CHECK_NULL);
oop loader_oop = JNIHandles::resolve(loader);
oop from_class = JNIHandles::resolve(caller);
oop protection_domain = NULL;
// If loader is null, shouldn't call ClassLoader.checkPackageAccess; otherwise get
// NPE. Put it in another way, the bootstrap class loader has all permission and
// thus no checkPackageAccess equivalence in the VM class loader.
// The caller is also passed as NULL by the java code if there is no security
// manager to avoid the performance cost of getting the calling class.
if (from_class != NULL && loader_oop != NULL) {
protection_domain = java_lang_Class::as_Klass(from_class)->protection_domain();
}
Handle h_loader(THREAD, loader_oop);
Handle h_prot(THREAD, protection_domain);
jclass result = find_class_from_class_loader(env, h_name, init, h_loader,
h_prot, false, THREAD);
if (log_is_enabled(Debug, class, resolve) && result != NULL) {
trace_class_resolution(java_lang_Class::as_Klass(JNIHandles::resolve_non_null(result)));
}
return result;
JVM_END
// Currently only called from the old verifier.
JVM_ENTRY(jclass, JVM_FindClassFromClass(JNIEnv *env, const char *name,
jboolean init, jclass from))
TempNewSymbol h_name =
SystemDictionary::class_name_symbol(name, vmSymbols::java_lang_ClassNotFoundException(),
CHECK_NULL);
oop from_class_oop = JNIHandles::resolve(from);
Klass* from_class = (from_class_oop == NULL)
? (Klass*)NULL
: java_lang_Class::as_Klass(from_class_oop);
oop class_loader = NULL;
oop protection_domain = NULL;
if (from_class != NULL) {
class_loader = from_class->class_loader();
protection_domain = from_class->protection_domain();
}
Handle h_loader(THREAD, class_loader);
Handle h_prot (THREAD, protection_domain);
jclass result = find_class_from_class_loader(env, h_name, init, h_loader,
h_prot, true, thread);
if (log_is_enabled(Debug, class, resolve) && result != NULL) {
// this function is generally only used for class loading during verification.
ResourceMark rm;
oop from_mirror = JNIHandles::resolve_non_null(from);
Klass* from_class = java_lang_Class::as_Klass(from_mirror);
const char * from_name = from_class->external_name();
oop mirror = JNIHandles::resolve_non_null(result);
Klass* to_class = java_lang_Class::as_Klass(mirror);
const char * to = to_class->external_name();
log_debug(class, resolve)("%s %s (verification)", from_name, to);
}
return result;
JVM_END
// common code for JVM_DefineClass() and JVM_DefineClassWithSource()
static jclass jvm_define_class_common(const char *name,
jobject loader, const jbyte *buf,
jsize len, jobject pd, const char *source,
TRAPS) {
if (source == NULL) source = "__JVM_DefineClass__";
JavaThread* jt = THREAD;
PerfClassTraceTime vmtimer(ClassLoader::perf_define_appclass_time(),
ClassLoader::perf_define_appclass_selftime(),
ClassLoader::perf_define_appclasses(),
jt->get_thread_stat()->perf_recursion_counts_addr(),
jt->get_thread_stat()->perf_timers_addr(),
PerfClassTraceTime::DEFINE_CLASS);
if (UsePerfData) {
ClassLoader::perf_app_classfile_bytes_read()->inc(len);
}
// Class resolution will get the class name from the .class stream if the name is null.
TempNewSymbol class_name = name == NULL ? NULL :
SystemDictionary::class_name_symbol(name, vmSymbols::java_lang_NoClassDefFoundError(),
CHECK_NULL);
ResourceMark rm(THREAD);
ClassFileStream st((u1*)buf, len, source, ClassFileStream::verify);
Handle class_loader (THREAD, JNIHandles::resolve(loader));
Handle protection_domain (THREAD, JNIHandles::resolve(pd));
ClassLoadInfo cl_info(protection_domain);
Klass* k = SystemDictionary::resolve_from_stream(&st, class_name,
class_loader,
cl_info,
CHECK_NULL);
if (log_is_enabled(Debug, class, resolve)) {
trace_class_resolution(k);
}
return (jclass) JNIHandles::make_local(THREAD, k->java_mirror());
}
enum {
NESTMATE = java_lang_invoke_MemberName::MN_NESTMATE_CLASS,
HIDDEN_CLASS = java_lang_invoke_MemberName::MN_HIDDEN_CLASS,
STRONG_LOADER_LINK = java_lang_invoke_MemberName::MN_STRONG_LOADER_LINK,
ACCESS_VM_ANNOTATIONS = java_lang_invoke_MemberName::MN_ACCESS_VM_ANNOTATIONS
};
/*
* Define a class with the specified flags that indicates if it's a nestmate,
* hidden, or strongly referenced from class loader.
*/
static jclass jvm_lookup_define_class(jclass lookup, const char *name,
const jbyte *buf, jsize len, jobject pd,
jboolean init, int flags, jobject classData, TRAPS) {
ResourceMark rm(THREAD);
Klass* lookup_k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(lookup));
// Lookup class must be a non-null instance
if (lookup_k == NULL) {
THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(), "Lookup class is null");
}
assert(lookup_k->is_instance_klass(), "Lookup class must be an instance klass");
Handle class_loader (THREAD, lookup_k->class_loader());
bool is_nestmate = (flags & NESTMATE) == NESTMATE;
bool is_hidden = (flags & HIDDEN_CLASS) == HIDDEN_CLASS;
bool is_strong = (flags & STRONG_LOADER_LINK) == STRONG_LOADER_LINK;
bool vm_annotations = (flags & ACCESS_VM_ANNOTATIONS) == ACCESS_VM_ANNOTATIONS;
InstanceKlass* host_class = NULL;
if (is_nestmate) {
host_class = InstanceKlass::cast(lookup_k)->nest_host(CHECK_NULL);
}
log_info(class, nestmates)("LookupDefineClass: %s - %s%s, %s, %s, %s",
name,
is_nestmate ? "with dynamic nest-host " : "non-nestmate",
is_nestmate ? host_class->external_name() : "",
is_hidden ? "hidden" : "not hidden",
is_strong ? "strong" : "weak",
vm_annotations ? "with vm annotations" : "without vm annotation");
if (!is_hidden) {
// classData is only applicable for hidden classes
if (classData != NULL) {
THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(), "classData is only applicable for hidden classes");
}
if (is_nestmate) {
THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(), "dynamic nestmate is only applicable for hidden classes");
}
if (!is_strong) {
THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(), "an ordinary class must be strongly referenced by its defining loader");
}
if (vm_annotations) {
THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(), "vm annotations only allowed for hidden classes");
}
if (flags != STRONG_LOADER_LINK) {
THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(),
err_msg("invalid flag 0x%x", flags));
}
}
// Class resolution will get the class name from the .class stream if the name is null.
TempNewSymbol class_name = name == NULL ? NULL :
SystemDictionary::class_name_symbol(name, vmSymbols::java_lang_NoClassDefFoundError(),
CHECK_NULL);
Handle protection_domain (THREAD, JNIHandles::resolve(pd));
const char* source = is_nestmate ? host_class->external_name() : "__JVM_LookupDefineClass__";
ClassFileStream st((u1*)buf, len, source, ClassFileStream::verify);
InstanceKlass* ik = NULL;
if (!is_hidden) {
ClassLoadInfo cl_info(protection_domain);
ik = SystemDictionary::resolve_from_stream(&st, class_name,
class_loader,
cl_info,
CHECK_NULL);
if (log_is_enabled(Debug, class, resolve)) {
trace_class_resolution(ik);
}
} else { // hidden
Handle classData_h(THREAD, JNIHandles::resolve(classData));
ClassLoadInfo cl_info(protection_domain,
host_class,
classData_h,
is_hidden,
is_strong,
vm_annotations);
ik = SystemDictionary::resolve_from_stream(&st, class_name,
class_loader,
cl_info,
CHECK_NULL);
// The hidden class loader data has been artificially been kept alive to
// this point. The mirror and any instances of this class have to keep
// it alive afterwards.
ik->class_loader_data()->dec_keep_alive();
if (is_nestmate && log_is_enabled(Debug, class, nestmates)) {
ModuleEntry* module = ik->module();
const char * module_name = module->is_named() ? module->name()->as_C_string() : UNNAMED_MODULE;
log_debug(class, nestmates)("Dynamic nestmate: %s/%s, nest_host %s, %s",
module_name,
ik->external_name(),
host_class->external_name(),
ik->is_hidden() ? "is hidden" : "is not hidden");
}
}
assert(Reflection::is_same_class_package(lookup_k, ik),
"lookup class and defined class are in different packages");
if (init) {
ik->initialize(CHECK_NULL);
} else {
ik->link_class(CHECK_NULL);
}
return (jclass) JNIHandles::make_local(THREAD, ik->java_mirror());
}
JVM_ENTRY(jclass, JVM_DefineClass(JNIEnv *env, const char *name, jobject loader, const jbyte *buf, jsize len, jobject pd))
return jvm_define_class_common(name, loader, buf, len, pd, NULL, THREAD);
JVM_END
/*
* Define a class with the specified lookup class.
* lookup: Lookup class
* name: the name of the class
* buf: class bytes
* len: length of class bytes
* pd: protection domain
* init: initialize the class
* flags: properties of the class
* classData: private static pre-initialized field
*/
JVM_ENTRY(jclass, JVM_LookupDefineClass(JNIEnv *env, jclass lookup, const char *name, const jbyte *buf,
jsize len, jobject pd, jboolean initialize, int flags, jobject classData))
if (lookup == NULL) {
THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(), "Lookup class is null");
}
assert(buf != NULL, "buf must not be NULL");
return jvm_lookup_define_class(lookup, name, buf, len, pd, initialize, flags, classData, THREAD);
JVM_END
JVM_ENTRY(jclass, JVM_DefineClassWithSource(JNIEnv *env, const char *name, jobject loader, const jbyte *buf, jsize len, jobject pd, const char *source))
return jvm_define_class_common(name, loader, buf, len, pd, source, THREAD);
JVM_END
JVM_ENTRY(jclass, JVM_FindLoadedClass(JNIEnv *env, jobject loader, jstring name))
ResourceMark rm(THREAD);
Handle h_name (THREAD, JNIHandles::resolve_non_null(name));
char* str = java_lang_String::as_utf8_string(h_name());
// Sanity check, don't expect null
if (str == NULL) return NULL;
// Internalize the string, converting '.' to '/' in string.
char* p = (char*)str;
while (*p != '\0') {
if (*p == '.') {
*p = '/';
}
p++;
}
const int str_len = (int)(p - str);
if (str_len > Symbol::max_length()) {
// It's impossible to create this class; the name cannot fit
// into the constant pool.
return NULL;
}
TempNewSymbol klass_name = SymbolTable::new_symbol(str, str_len);
// Security Note:
// The Java level wrapper will perform the necessary security check allowing
// us to pass the NULL as the initiating class loader.
Handle h_loader(THREAD, JNIHandles::resolve(loader));
Klass* k = SystemDictionary::find_instance_or_array_klass(THREAD, klass_name,
h_loader,
Handle());
#if INCLUDE_CDS
if (k == NULL) {
// If the class is not already loaded, try to see if it's in the shared
// archive for the current classloader (h_loader).
k = SystemDictionaryShared::find_or_load_shared_class(klass_name, h_loader, CHECK_NULL);
}
#endif
return (k == NULL) ? NULL :
(jclass) JNIHandles::make_local(THREAD, k->java_mirror());
JVM_END
// Module support //////////////////////////////////////////////////////////////////////////////
JVM_ENTRY(void, JVM_DefineModule(JNIEnv *env, jobject module, jboolean is_open, jstring version,
jstring location, jobjectArray packages))
Handle h_module (THREAD, JNIHandles::resolve(module));
Modules::define_module(h_module, is_open, version, location, packages, CHECK);
JVM_END
JVM_ENTRY(void, JVM_SetBootLoaderUnnamedModule(JNIEnv *env, jobject module))
Handle h_module (THREAD, JNIHandles::resolve(module));
Modules::set_bootloader_unnamed_module(h_module, CHECK);
JVM_END
JVM_ENTRY(void, JVM_AddModuleExports(JNIEnv *env, jobject from_module, jstring package, jobject to_module))
Handle h_from_module (THREAD, JNIHandles::resolve(from_module));
Handle h_to_module (THREAD, JNIHandles::resolve(to_module));
Modules::add_module_exports_qualified(h_from_module, package, h_to_module, CHECK);
JVM_END
JVM_ENTRY(void, JVM_AddModuleExportsToAllUnnamed(JNIEnv *env, jobject from_module, jstring package))
Handle h_from_module (THREAD, JNIHandles::resolve(from_module));
Modules::add_module_exports_to_all_unnamed(h_from_module, package, CHECK);
JVM_END
JVM_ENTRY(void, JVM_AddModuleExportsToAll(JNIEnv *env, jobject from_module, jstring package))
Handle h_from_module (THREAD, JNIHandles::resolve(from_module));
Modules::add_module_exports(h_from_module, package, Handle(), CHECK);
JVM_END
JVM_ENTRY (void, JVM_AddReadsModule(JNIEnv *env, jobject from_module, jobject source_module))
Handle h_from_module (THREAD, JNIHandles::resolve(from_module));
Handle h_source_module (THREAD, JNIHandles::resolve(source_module));
Modules::add_reads_module(h_from_module, h_source_module, CHECK);
JVM_END
JVM_ENTRY(void, JVM_DefineArchivedModules(JNIEnv *env, jobject platform_loader, jobject system_loader))
Handle h_platform_loader (THREAD, JNIHandles::resolve(platform_loader));
Handle h_system_loader (THREAD, JNIHandles::resolve(system_loader));
Modules::define_archived_modules(h_platform_loader, h_system_loader, CHECK);
JVM_END
// Reflection support //////////////////////////////////////////////////////////////////////////////
JVM_ENTRY(jstring, JVM_InitClassName(JNIEnv *env, jclass cls))
assert (cls != NULL, "illegal class");
JvmtiVMObjectAllocEventCollector oam;
ResourceMark rm(THREAD);
HandleMark hm(THREAD);
Handle java_class(THREAD, JNIHandles::resolve(cls));
oop result = java_lang_Class::name(java_class, CHECK_NULL);
return (jstring) JNIHandles::make_local(THREAD, result);
JVM_END
JVM_ENTRY(jobjectArray, JVM_GetClassInterfaces(JNIEnv *env, jclass cls))
JvmtiVMObjectAllocEventCollector oam;
oop mirror = JNIHandles::resolve_non_null(cls);
// Special handling for primitive objects
if (java_lang_Class::is_primitive(mirror)) {
// Primitive objects does not have any interfaces
objArrayOop r = oopFactory::new_objArray(vmClasses::Class_klass(), 0, CHECK_NULL);
return (jobjectArray) JNIHandles::make_local(THREAD, r);
}
Klass* klass = java_lang_Class::as_Klass(mirror);
// Figure size of result array
int size;
if (klass->is_instance_klass()) {
size = InstanceKlass::cast(klass)->local_interfaces()->length();
} else {
assert(klass->is_objArray_klass() || klass->is_typeArray_klass(), "Illegal mirror klass");
size = 2;
}
// Allocate result array
objArrayOop r = oopFactory::new_objArray(vmClasses::Class_klass(), size, CHECK_NULL);
objArrayHandle result (THREAD, r);
// Fill in result
if (klass->is_instance_klass()) {
// Regular instance klass, fill in all local interfaces
for (int index = 0; index < size; index++) {
Klass* k = InstanceKlass::cast(klass)->local_interfaces()->at(index);
result->obj_at_put(index, k->java_mirror());
}
} else {
// All arrays implement java.lang.Cloneable and java.io.Serializable
result->obj_at_put(0, vmClasses::Cloneable_klass()->java_mirror());
result->obj_at_put(1, vmClasses::Serializable_klass()->java_mirror());
}
return (jobjectArray) JNIHandles::make_local(THREAD, result());
JVM_END
JVM_ENTRY(jboolean, JVM_IsInterface(JNIEnv *env, jclass cls))
oop mirror = JNIHandles::resolve_non_null(cls);
if (java_lang_Class::is_primitive(mirror)) {
return JNI_FALSE;
}
Klass* k = java_lang_Class::as_Klass(mirror);
jboolean result = k->is_interface();
assert(!result || k->is_instance_klass(),
"all interfaces are instance types");
// The compiler intrinsic for isInterface tests the
// Klass::_access_flags bits in the same way.
return result;
JVM_END
JVM_ENTRY(jboolean, JVM_IsHiddenClass(JNIEnv *env, jclass cls))
oop mirror = JNIHandles::resolve_non_null(cls);
if (java_lang_Class::is_primitive(mirror)) {
return JNI_FALSE;
}
Klass* k = java_lang_Class::as_Klass(mirror);
return k->is_hidden();
JVM_END
JVM_ENTRY(jobjectArray, JVM_GetClassSigners(JNIEnv *env, jclass cls))
JvmtiVMObjectAllocEventCollector oam;
oop mirror = JNIHandles::resolve_non_null(cls);
if (java_lang_Class::is_primitive(mirror)) {
// There are no signers for primitive types
return NULL;
}
objArrayHandle signers(THREAD, java_lang_Class::signers(mirror));
// If there are no signers set in the class, or if the class
// is an array, return NULL.
if (signers == NULL) return NULL;
// copy of the signers array
Klass* element = ObjArrayKlass::cast(signers->klass())->element_klass();
objArrayOop signers_copy = oopFactory::new_objArray(element, signers->length(), CHECK_NULL);
for (int index = 0; index < signers->length(); index++) {
signers_copy->obj_at_put(index, signers->obj_at(index));
}
// return the copy
return (jobjectArray) JNIHandles::make_local(THREAD, signers_copy);
JVM_END
JVM_ENTRY(void, JVM_SetClassSigners(JNIEnv *env, jclass cls, jobjectArray signers))
oop mirror = JNIHandles::resolve_non_null(cls);
if (!java_lang_Class::is_primitive(mirror)) {
// This call is ignored for primitive types and arrays.
// Signers are only set once, ClassLoader.java, and thus shouldn't
// be called with an array. Only the bootstrap loader creates arrays.
Klass* k = java_lang_Class::as_Klass(mirror);
if (k->is_instance_klass()) {
java_lang_Class::set_signers(k->java_mirror(), objArrayOop(JNIHandles::resolve(signers)));
}
}
JVM_END
JVM_ENTRY(jobject, JVM_GetProtectionDomain(JNIEnv *env, jclass cls))
oop mirror = JNIHandles::resolve_non_null(cls);
if (mirror == NULL) {
THROW_(vmSymbols::java_lang_NullPointerException(), NULL);
}
if (java_lang_Class::is_primitive(mirror)) {
// Primitive types does not have a protection domain.
return NULL;
}
oop pd = java_lang_Class::protection_domain(mirror);
return (jobject) JNIHandles::make_local(THREAD, pd);
JVM_END
// Returns the inherited_access_control_context field of the running thread.
JVM_ENTRY(jobject, JVM_GetInheritedAccessControlContext(JNIEnv *env, jclass cls))
oop result = java_lang_Thread::inherited_access_control_context(thread->threadObj());
return JNIHandles::make_local(THREAD, result);
JVM_END
JVM_ENTRY(jobject, JVM_GetStackAccessControlContext(JNIEnv *env, jclass cls))
if (!UsePrivilegedStack) return NULL;
ResourceMark rm(THREAD);
GrowableArray<Handle>* local_array = new GrowableArray<Handle>(12);
JvmtiVMObjectAllocEventCollector oam;
// count the protection domains on the execution stack. We collapse
// duplicate consecutive protection domains into a single one, as
// well as stopping when we hit a privileged frame.
oop previous_protection_domain = NULL;
Handle privileged_context(thread, NULL);
bool is_privileged = false;
oop protection_domain = NULL;
// Iterate through Java frames
vframeStream vfst(thread);
for(; !vfst.at_end(); vfst.next()) {
// get method of frame
Method* method = vfst.method();
// stop at the first privileged frame
if (method->method_holder() == vmClasses::AccessController_klass() &&
method->name() == vmSymbols::executePrivileged_name())
{
// this frame is privileged
is_privileged = true;
javaVFrame *priv = vfst.asJavaVFrame(); // executePrivileged
StackValueCollection* locals = priv->locals();
StackValue* ctx_sv = locals->at(1); // AccessControlContext context
StackValue* clr_sv = locals->at(2); // Class<?> caller
assert(!ctx_sv->obj_is_scalar_replaced(), "found scalar-replaced object");
assert(!clr_sv->obj_is_scalar_replaced(), "found scalar-replaced object");
privileged_context = ctx_sv->get_obj();
Handle caller = clr_sv->get_obj();
Klass *caller_klass = java_lang_Class::as_Klass(caller());
protection_domain = caller_klass->protection_domain();
} else {
protection_domain = method->method_holder()->protection_domain();
}
if ((previous_protection_domain != protection_domain) && (protection_domain != NULL)) {
local_array->push(Handle(thread, protection_domain));
previous_protection_domain = protection_domain;
}
if (is_privileged) break;
}
// either all the domains on the stack were system domains, or
// we had a privileged system domain
if (local_array->is_empty()) {
if (is_privileged && privileged_context.is_null()) return NULL;
oop result = java_security_AccessControlContext::create(objArrayHandle(), is_privileged, privileged_context, CHECK_NULL);
return JNIHandles::make_local(THREAD, result);
}
objArrayOop context = oopFactory::new_objArray(vmClasses::ProtectionDomain_klass(),
local_array->length(), CHECK_NULL);
objArrayHandle h_context(thread, context);
for (int index = 0; index < local_array->length(); index++) {
h_context->obj_at_put(index, local_array->at(index)());
}
oop result = java_security_AccessControlContext::create(h_context, is_privileged, privileged_context, CHECK_NULL);
return JNIHandles::make_local(THREAD, result);
JVM_END
class ScopedValueBindingsResolver {
public:
InstanceKlass* Carrier_klass;
ScopedValueBindingsResolver(JavaThread* THREAD) {
Klass *k = SystemDictionary::resolve_or_fail(vmSymbols::jdk_incubator_concurrent_ScopedValue_Carrier(), true, THREAD);
Carrier_klass = InstanceKlass::cast(k);
}
};
JVM_ENTRY(jobject, JVM_FindScopedValueBindings(JNIEnv *env, jclass cls))
ResourceMark rm(THREAD);
GrowableArray<Handle>* local_array = new GrowableArray<Handle>(12);
JvmtiVMObjectAllocEventCollector oam;
static ScopedValueBindingsResolver resolver(THREAD);
// Iterate through Java frames
vframeStream vfst(thread);
for(; !vfst.at_end(); vfst.next()) {
int loc = -1;
// get method of frame
Method* method = vfst.method();
Symbol *name = method->name();
InstanceKlass* holder = method->method_holder();
if (name == vmSymbols::runWith_method_name()) {
if ((holder == resolver.Carrier_klass
|| holder == vmClasses::VirtualThread_klass()
|| holder == vmClasses::Thread_klass())) {
loc = 1;
}
}
if (loc != -1) {
javaVFrame *frame = vfst.asJavaVFrame();
StackValueCollection* locals = frame->locals();
StackValue* head_sv = locals->at(loc); // jdk/incubator/concurrent/ScopedValue$Snapshot
Handle result = head_sv->get_obj();
assert(!head_sv->obj_is_scalar_replaced(), "found scalar-replaced object");
if (result() != NULL) {
return JNIHandles::make_local(THREAD, result());
}
}
}
return NULL;
JVM_END
JVM_ENTRY(jboolean, JVM_IsArrayClass(JNIEnv *env, jclass cls))
Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls));
return (k != NULL) && k->is_array_klass() ? true : false;
JVM_END
JVM_ENTRY(jboolean, JVM_IsPrimitiveClass(JNIEnv *env, jclass cls))
oop mirror = JNIHandles::resolve_non_null(cls);
return (jboolean) java_lang_Class::is_primitive(mirror);
JVM_END
JVM_ENTRY(jint, JVM_GetClassModifiers(JNIEnv *env, jclass cls))
oop mirror = JNIHandles::resolve_non_null(cls);
if (java_lang_Class::is_primitive(mirror)) {
// Primitive type
return JVM_ACC_ABSTRACT | JVM_ACC_FINAL | JVM_ACC_PUBLIC;
}
Klass* k = java_lang_Class::as_Klass(mirror);
debug_only(int computed_modifiers = k->compute_modifier_flags());
assert(k->modifier_flags() == computed_modifiers, "modifiers cache is OK");
return k->modifier_flags();
JVM_END
// Inner class reflection ///////////////////////////////////////////////////////////////////////////////
JVM_ENTRY(jobjectArray, JVM_GetDeclaredClasses(JNIEnv *env, jclass ofClass))
JvmtiVMObjectAllocEventCollector oam;
// ofClass is a reference to a java_lang_Class object. The mirror object
// of an InstanceKlass
oop ofMirror = JNIHandles::resolve_non_null(ofClass);
if (java_lang_Class::is_primitive(ofMirror) ||
! java_lang_Class::as_Klass(ofMirror)->is_instance_klass()) {
oop result = oopFactory::new_objArray(vmClasses::Class_klass(), 0, CHECK_NULL);
return (jobjectArray)JNIHandles::make_local(THREAD, result);
}
InstanceKlass* k = InstanceKlass::cast(java_lang_Class::as_Klass(ofMirror));
InnerClassesIterator iter(k);
if (iter.length() == 0) {
// Neither an inner nor outer class
oop result = oopFactory::new_objArray(vmClasses::Class_klass(), 0, CHECK_NULL);
return (jobjectArray)JNIHandles::make_local(THREAD, result);
}
// find inner class info
constantPoolHandle cp(thread, k->constants());
int length = iter.length();
// Allocate temp. result array
objArrayOop r = oopFactory::new_objArray(vmClasses::Class_klass(), length/4, CHECK_NULL);
objArrayHandle result (THREAD, r);
int members = 0;
for (; !iter.done(); iter.next()) {
int ioff = iter.inner_class_info_index();
int ooff = iter.outer_class_info_index();
if (ioff != 0 && ooff != 0) {
// Check to see if the name matches the class we're looking for
// before attempting to find the class.
if (cp->klass_name_at_matches(k, ooff)) {
Klass* outer_klass = cp->klass_at(ooff, CHECK_NULL);
if (outer_klass == k) {
Klass* ik = cp->klass_at(ioff, CHECK_NULL);
InstanceKlass* inner_klass = InstanceKlass::cast(ik);
// Throws an exception if outer klass has not declared k as
// an inner klass
Reflection::check_for_inner_class(k, inner_klass, true, CHECK_NULL);
result->obj_at_put(members, inner_klass->java_mirror());
members++;
}
}
}
}
if (members != length) {
// Return array of right length
objArrayOop res = oopFactory::new_objArray(vmClasses::Class_klass(), members, CHECK_NULL);
for(int i = 0; i < members; i++) {
res->obj_at_put(i, result->obj_at(i));
}
return (jobjectArray)JNIHandles::make_local(THREAD, res);
}
return (jobjectArray)JNIHandles::make_local(THREAD, result());
JVM_END
JVM_ENTRY(jclass, JVM_GetDeclaringClass(JNIEnv *env, jclass ofClass))
{
// ofClass is a reference to a java_lang_Class object.
oop ofMirror = JNIHandles::resolve_non_null(ofClass);
if (java_lang_Class::is_primitive(ofMirror)) {
return NULL;
}
Klass* klass = java_lang_Class::as_Klass(ofMirror);
if (!klass->is_instance_klass()) {
return NULL;
}
bool inner_is_member = false;
Klass* outer_klass
= InstanceKlass::cast(klass)->compute_enclosing_class(&inner_is_member, CHECK_NULL);
if (outer_klass == NULL) return NULL; // already a top-level class
if (!inner_is_member) return NULL; // a hidden class (inside a method)
return (jclass) JNIHandles::make_local(THREAD, outer_klass->java_mirror());
}
JVM_END
JVM_ENTRY(jstring, JVM_GetSimpleBinaryName(JNIEnv *env, jclass cls))
{
oop mirror = JNIHandles::resolve_non_null(cls);
if (java_lang_Class::is_primitive(mirror)) {
return NULL;
}
Klass* klass = java_lang_Class::as_Klass(mirror);
if (!klass->is_instance_klass()) {
return NULL;
}
InstanceKlass* k = InstanceKlass::cast(klass);
int ooff = 0, noff = 0;
if (k->find_inner_classes_attr(&ooff, &noff, THREAD)) {
if (noff != 0) {
constantPoolHandle i_cp(thread, k->constants());
Symbol* name = i_cp->symbol_at(noff);
Handle str = java_lang_String::create_from_symbol(name, CHECK_NULL);
return (jstring) JNIHandles::make_local(THREAD, str());
}
}
return NULL;
}
JVM_END
JVM_ENTRY(jstring, JVM_GetClassSignature(JNIEnv *env, jclass cls))
assert (cls != NULL, "illegal class");
JvmtiVMObjectAllocEventCollector oam;
ResourceMark rm(THREAD);
oop mirror = JNIHandles::resolve_non_null(cls);
// Return null for arrays and primitives
if (!java_lang_Class::is_primitive(mirror)) {
Klass* k = java_lang_Class::as_Klass(mirror);
if (k->is_instance_klass()) {
Symbol* sym = InstanceKlass::cast(k)->generic_signature();
if (sym == NULL) return NULL;
Handle str = java_lang_String::create_from_symbol(sym, CHECK_NULL);
return (jstring) JNIHandles::make_local(THREAD, str());
}
}
return NULL;
JVM_END
JVM_ENTRY(jbyteArray, JVM_GetClassAnnotations(JNIEnv *env, jclass cls))
assert (cls != NULL, "illegal class");
oop mirror = JNIHandles::resolve_non_null(cls);
// Return null for arrays and primitives
if (!java_lang_Class::is_primitive(mirror)) {
Klass* k = java_lang_Class::as_Klass(mirror);
if (k->is_instance_klass()) {
typeArrayOop a = Annotations::make_java_array(InstanceKlass::cast(k)->class_annotations(), CHECK_NULL);
return (jbyteArray) JNIHandles::make_local(THREAD, a);
}
}
return NULL;
JVM_END
static bool jvm_get_field_common(jobject field, fieldDescriptor& fd) {
// some of this code was adapted from from jni_FromReflectedField
oop reflected = JNIHandles::resolve_non_null(field);
oop mirror = java_lang_reflect_Field::clazz(reflected);
Klass* k = java_lang_Class::as_Klass(mirror);
int slot = java_lang_reflect_Field::slot(reflected);
int modifiers = java_lang_reflect_Field::modifiers(reflected);
InstanceKlass* ik = InstanceKlass::cast(k);
intptr_t offset = ik->field_offset(slot);
if (modifiers & JVM_ACC_STATIC) {
// for static fields we only look in the current class
if (!ik->find_local_field_from_offset(offset, true, &fd)) {
assert(false, "cannot find static field");
return false;
}
} else {
// for instance fields we start with the current class and work
// our way up through the superclass chain
if (!ik->find_field_from_offset(offset, false, &fd)) {
assert(false, "cannot find instance field");
return false;
}
}
return true;
}
static Method* jvm_get_method_common(jobject method) {
// some of this code was adapted from from jni_FromReflectedMethod
oop reflected = JNIHandles::resolve_non_null(method);
oop mirror = NULL;
int slot = 0;
if (reflected->klass() == vmClasses::reflect_Constructor_klass()) {
mirror = java_lang_reflect_Constructor::clazz(reflected);
slot = java_lang_reflect_Constructor::slot(reflected);
} else {
assert(reflected->klass() == vmClasses::reflect_Method_klass(),
"wrong type");
mirror = java_lang_reflect_Method::clazz(reflected);
slot = java_lang_reflect_Method::slot(reflected);
}
Klass* k = java_lang_Class::as_Klass(mirror);
Method* m = InstanceKlass::cast(k)->method_with_idnum(slot);
assert(m != NULL, "cannot find method");
return m; // caller has to deal with NULL in product mode
}
/* Type use annotations support (JDK 1.8) */
JVM_ENTRY(jbyteArray, JVM_GetClassTypeAnnotations(JNIEnv *env, jclass cls))
assert (cls != NULL, "illegal class");
ResourceMark rm(THREAD);
// Return null for arrays and primitives
if (!java_lang_Class::is_primitive(JNIHandles::resolve(cls))) {
Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve(cls));
if (k->is_instance_klass()) {
AnnotationArray* type_annotations = InstanceKlass::cast(k)->class_type_annotations();
if (type_annotations != NULL) {
typeArrayOop a = Annotations::make_java_array(type_annotations, CHECK_NULL);
return (jbyteArray) JNIHandles::make_local(THREAD, a);
}
}
}
return NULL;
JVM_END
JVM_ENTRY(jbyteArray, JVM_GetMethodTypeAnnotations(JNIEnv *env, jobject method))
assert (method != NULL, "illegal method");
// method is a handle to a java.lang.reflect.Method object
Method* m = jvm_get_method_common(method);
if (m == NULL) {
return NULL;
}
AnnotationArray* type_annotations = m->type_annotations();
if (type_annotations != NULL) {
typeArrayOop a = Annotations::make_java_array(type_annotations, CHECK_NULL);
return (jbyteArray) JNIHandles::make_local(THREAD, a);
}
return NULL;
JVM_END
JVM_ENTRY(jbyteArray, JVM_GetFieldTypeAnnotations(JNIEnv *env, jobject field))
assert (field != NULL, "illegal field");
fieldDescriptor fd;
bool gotFd = jvm_get_field_common(field, fd);
if (!gotFd) {
return NULL;
}
return (jbyteArray) JNIHandles::make_local(THREAD, Annotations::make_java_array(fd.type_annotations(), THREAD));
JVM_END
static void bounds_check(const constantPoolHandle& cp, jint index, TRAPS) {
if (!cp->is_within_bounds(index)) {
THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), "Constant pool index out of bounds");
}
}
JVM_ENTRY(jobjectArray, JVM_GetMethodParameters(JNIEnv *env, jobject method))
{
// method is a handle to a java.lang.reflect.Method object
Method* method_ptr = jvm_get_method_common(method);
methodHandle mh (THREAD, method_ptr);
Handle reflected_method (THREAD, JNIHandles::resolve_non_null(method));
const int num_params = mh->method_parameters_length();
if (num_params < 0) {
// A -1 return value from method_parameters_length means there is no
// parameter data. Return null to indicate this to the reflection
// API.
assert(num_params == -1, "num_params should be -1 if it is less than zero");
return (jobjectArray)NULL;
} else {
// Otherwise, we return something up to reflection, even if it is
// a zero-length array. Why? Because in some cases this can
// trigger a MalformedParametersException.
// make sure all the symbols are properly formatted
for (int i = 0; i < num_params; i++) {
MethodParametersElement* params = mh->method_parameters_start();
int index = params[i].name_cp_index;
--> --------------------
--> maximum size reached
--> --------------------
¤ Dauer der Verarbeitung: 0.93 Sekunden
(vorverarbeitet)
¤
|
Laden
Fehler beim Verzeichnis:
in der Quellcodebibliothek suchen
Die farbliche Syntaxdarstellung ist noch experimentell.
|