From 0bdd6f29a7a6033043755f005d54d55a0b071951 Mon Sep 17 00:00:00 2001 From: Babneet Singh Date: Mon, 14 Nov 2022 08:10:11 -0500 Subject: [PATCH] Fix jvmtiGetThreadInfo Set J9JavaVM->vthreadGroup in stdinit.c::standardInit to remove the recursive dependency with JVMTI event callbacks, which causes an infinite loop. Closes: #16229 Signed-off-by: Babneet Singh --- runtime/jcl/common/stdinit.c | 19 ++++++++++++++++ runtime/jvmti/jvmtiThread.c | 43 +----------------------------------- runtime/oti/j9nonbuilder.h | 3 +-- 3 files changed, 21 insertions(+), 44 deletions(-) diff --git a/runtime/jcl/common/stdinit.c b/runtime/jcl/common/stdinit.c index 81b10fae29a..142b2179766 100644 --- a/runtime/jcl/common/stdinit.c +++ b/runtime/jcl/common/stdinit.c @@ -301,6 +301,25 @@ standardInit(J9JavaVM *vm, char *dllName) (void)(*(JNIEnv*)vmThread)->GetStaticFieldID((JNIEnv*)vmThread, clazz, "helpers", "Lcom/ibm/jit/JITHelpers;"); (*(JNIEnv*)vmThread)->DeleteLocalRef((JNIEnv*)vmThread, clazz); } + +#if JAVA_SPEC_VERSION >= 19 + clazz = (*(JNIEnv*)vmThread)->FindClass((JNIEnv*)vmThread, "java/lang/Thread$Constants"); + if (NULL != clazz) { + jfieldID id = (*(JNIEnv*)vmThread)->GetStaticFieldID((JNIEnv*)vmThread, clazz, "VTHREAD_GROUP", "Ljava/lang/ThreadGroup;"); + if (NULL != id) { + jobject tempGroup = (*(JNIEnv*)vmThread)->GetStaticObjectField((JNIEnv*)vmThread, clazz, id); + if (NULL != tempGroup) { + vm->vthreadGroup = (jobject)(*(JNIEnv*)vmThread)->NewGlobalRef((JNIEnv*)vmThread, tempGroup); + } + } + (*(JNIEnv*)vmThread)->DeleteLocalRef((JNIEnv*)vmThread, clazz); + if (NULL == vm->vthreadGroup) { + goto _fail; + } + } else { + goto _fail; + } +#endif /* JAVA_SPEC_VERSION >= 19 */ #endif /* !J9VM_IVE_RAW_BUILD */ } #endif diff --git a/runtime/jvmti/jvmtiThread.c b/runtime/jvmti/jvmtiThread.c index 327233010b0..554d1f89b0f 100644 --- a/runtime/jvmti/jvmtiThread.c +++ b/runtime/jvmti/jvmtiThread.c @@ -522,48 +522,7 @@ jvmtiGetThreadInfo(jvmtiEnv *env, /* For virtual threads, check its state. */ if (JVMTI_VTHREAD_STATE_TERMINATED != J9VMJAVALANGVIRTUALTHREAD_STATE(currentThread, threadObject)) { /* The thread group of a virtual thread is always j.l.Thread$Constants.VTHREAD_GROUP. */ - JNIEnv *jniEnv = (JNIEnv *)currentThread; - jclass constants = vm->jlThreadConstants; - jfieldID id = vm->vthreadGroupID; - jobject tempGroup = NULL; - - vm->internalVMFunctions->internalExitVMToJNI(currentThread); - if (NULL == constants) { - /* Cache the class. */ - jclass localRef = (*jniEnv)->FindClass(jniEnv, "java/lang/Thread$Constants"); - Assert_JVMTI_notNull(localRef); - constants = (jclass)((*jniEnv)->NewGlobalRef(jniEnv, localRef)); - if (NULL != constants) { - vm->jlThreadConstants = constants; - } else { - rc = JVMTI_ERROR_OUT_OF_MEMORY; - } - } - - if ((NULL != constants) && (NULL == id)) { - /* Cache the field id. */ - id = (*jniEnv)->GetStaticFieldID(jniEnv, constants, "VTHREAD_GROUP", "Ljava/lang/ThreadGroup;"); - if (NULL != id) { - vm->vthreadGroupID = id; - } else { - rc = JVMTI_ERROR_OUT_OF_MEMORY; - } - } - - if ((NULL != constants) && (NULL != id)) { - tempGroup = (*jniEnv)->GetStaticObjectField(jniEnv, constants, id); - Assert_JVMTI_notNull(tempGroup); - } - - /* VM access is required before invoking releaseVMThread. */ - vm->internalVMFunctions->internalEnterVMFromJNI(currentThread); - if (JVMTI_ERROR_OUT_OF_MEMORY == rc) { - j9mem_free_memory(name); - goto release; - } - - group = J9_JNI_UNWRAP_REFERENCE(tempGroup); - threadObject = (NULL == thread) ? targetThread->threadObject : J9_JNI_UNWRAP_REFERENCE(thread); + group = J9_JNI_UNWRAP_REFERENCE(vm->vthreadGroup); } } else { /* For platform threads, a NULL vmthread indicates that a thread is terminated. */ diff --git a/runtime/oti/j9nonbuilder.h b/runtime/oti/j9nonbuilder.h index e874e01a9bd..5f0a61251da 100644 --- a/runtime/oti/j9nonbuilder.h +++ b/runtime/oti/j9nonbuilder.h @@ -5799,8 +5799,7 @@ typedef struct J9JavaVM { omrthread_monitor_t tlsFinalizersMutex; struct J9Pool *tlsPool; omrthread_monitor_t tlsPoolMutex; - jclass jlThreadConstants; - jfieldID vthreadGroupID; + jobject vthreadGroup; #endif /* JAVA_SPEC_VERSION >= 19 */ } J9JavaVM;