Skip to content

Commit

Permalink
Merge pull request #16878 from babsingh/fix_getthreadstate_jvmti_037
Browse files Browse the repository at this point in the history
(0.37) Pass valid JNI refs to getVirtualThreadState
  • Loading branch information
gacholio committed Mar 10, 2023
2 parents 72fb959 + eedceed commit 095fe80
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 9 deletions.
2 changes: 2 additions & 0 deletions runtime/jvmti/jvmtiHelpers.c
Expand Up @@ -845,6 +845,8 @@ getVirtualThreadState(J9VMThread *currentThread, jthread thread)
rc = JVMTI_JAVA_LANG_THREAD_STATE_RUNNABLE;
}
vm->internalVMFunctions->internalEnterVMFromJNI(currentThread);
/* Re-fetch object to correctly set the isSuspendedByJVMTI field. */
vThreadObject = J9_JNI_UNWRAP_REFERENCE(thread);
break;
}
case JVMTI_VTHREAD_STATE_RUNNABLE:
Expand Down
29 changes: 20 additions & 9 deletions runtime/jvmti/jvmtiThread.c
Expand Up @@ -53,8 +53,9 @@ jvmtiGetThreadState(jvmtiEnv *env,
J9VMThread *targetThread = NULL;
j9object_t threadObject = NULL;
jboolean threadStartedFlag = JNI_FALSE;
J9InternalVMFunctions *vmFuncs = vm->internalVMFunctions;

vm->internalVMFunctions->internalEnterVMFromJNI(currentThread);
vmFuncs->internalEnterVMFromJNI(currentThread);

ENSURE_PHASE_LIVE(env);
ENSURE_NON_NULL(thread_state_ptr);
Expand All @@ -71,21 +72,31 @@ jvmtiGetThreadState(jvmtiEnv *env,
/* If thread is NULL, the current thread is used which cannot be a virtual thread.
* There is an assertion inside getVirtualThreadState() that thread is not NULL.
*/
rv_thread_state = getVirtualThreadState(currentThread, (jthread)&threadObject);
if (NULL != thread) {
rv_thread_state = getVirtualThreadState(currentThread, thread);
} else {
/* Create a local ref to pass the currentThread's threadObject since
* getVirtualThreadState releases and re-acquires VM access.
*/
JNIEnv *jniEnv = (JNIEnv *)currentThread;
jobject virtualThreadRef = vmFuncs->j9jni_createLocalRef(jniEnv, threadObject);
rv_thread_state = getVirtualThreadState(currentThread, (jthread)virtualThreadRef);
vmFuncs->j9jni_deleteLocalRef(jniEnv, virtualThreadRef);
}
} else
#endif /* JAVA_SPEC_VERSION >= 19 */
{
rc = getVMThread(currentThread, thread, &targetThread, JVMTI_ERROR_NONE, 0);
if (JVMTI_ERROR_NONE == rc) {
/* Get the lock for the object. */
j9object_t threadObjectLock = J9VMJAVALANGTHREAD_LOCK(currentThread,threadObject);
j9object_t threadObjectLock = J9VMJAVALANGTHREAD_LOCK(currentThread, threadObject);
if (NULL != threadObjectLock) {
threadStartedFlag = (jboolean)J9VMJAVALANGTHREAD_STARTED(currentThread, threadObject);
} else {
/* In this case, we must be early in the thread creation. We still need to call getVMThread so that
* the inspection count etc. are handled correctly, however, we just want to fall into the case were
* we return that the thread is NEW so we set the targetThread and threadStartedFlag to false.
*/
* the inspection count etc. are handled correctly, however, we just want to fall into the case were
* we return that the thread is NEW so we set the targetThread and threadStartedFlag to false.
*/
targetThread = NULL;
threadStartedFlag = JNI_FALSE;
}
Expand All @@ -107,20 +118,20 @@ jvmtiGetThreadState(jvmtiEnv *env,
rv_thread_state = 0;
}
} else {
vm->internalVMFunctions->haltThreadForInspection(currentThread, targetThread);
vmFuncs->haltThreadForInspection(currentThread, targetThread);
/* The VM thread can't be recycled because getVMThread() prevented it from exiting. */
#if JAVA_SPEC_VERSION >= 19
rv_thread_state = getThreadState(currentThread, targetThread->carrierThreadObject);
#else /* JAVA_SPEC_VERSION >= 19 */
rv_thread_state = getThreadState(currentThread, targetThread->threadObject);
#endif /* JAVA_SPEC_VERSION >= 19 */
vm->internalVMFunctions->resumeThreadForInspection(currentThread, targetThread);
vmFuncs->resumeThreadForInspection(currentThread, targetThread);
}
releaseVMThread(currentThread, targetThread, thread);
}
}
done:
vm->internalVMFunctions->internalExitVMToJNI(currentThread);
vmFuncs->internalExitVMToJNI(currentThread);
}

if (NULL != thread_state_ptr) {
Expand Down

0 comments on commit 095fe80

Please sign in to comment.