Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add JVMTI synchronization in JVM_VirtualThreadHideFrames #18453

Merged
merged 1 commit into from
Nov 19, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 29 additions & 10 deletions runtime/j9vm/javanextvmi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -310,8 +310,6 @@ virtualThreadMountBegin(JNIEnv *env, jobject thread)
{
J9VMThread *currentThread = (J9VMThread *)env;

VM_VMHelpers::virtualThreadHideFrames(currentThread, JNI_TRUE);

j9object_t threadObj = J9_JNI_UNWRAP_REFERENCE(thread);
Assert_SC_true(IS_JAVA_LANG_VIRTUALTHREAD(currentThread, threadObj));

Expand Down Expand Up @@ -351,6 +349,8 @@ virtualThreadMountBegin(JNIEnv *env, jobject thread)
enterVThreadTransitionCritical(currentThread, thread);
threadObj = J9_JNI_UNWRAP_REFERENCE(thread);
}

VM_VMHelpers::virtualThreadHideFrames(currentThread, JNI_TRUE);
babsingh marked this conversation as resolved.
Show resolved Hide resolved
}

/* Caller must have VMAccess. */
Expand All @@ -361,8 +361,6 @@ virtualThreadMountEnd(JNIEnv *env, jobject thread)
J9JavaVM *vm = currentThread->javaVM;
j9object_t threadObj = J9_JNI_UNWRAP_REFERENCE(thread);

VM_VMHelpers::virtualThreadHideFrames(currentThread, JNI_FALSE);

Assert_SC_true(IS_JAVA_LANG_VIRTUALTHREAD(currentThread, threadObj));

if (TrcEnabled_Trc_SC_VirtualThread_Info) {
Expand All @@ -377,6 +375,8 @@ virtualThreadMountEnd(JNIEnv *env, jobject thread)
J9VMJDKINTERNALVMCONTINUATION_VMREF(currentThread, continuationObj));
}

VM_VMHelpers::virtualThreadHideFrames(currentThread, JNI_FALSE);
babsingh marked this conversation as resolved.
Show resolved Hide resolved

/* Allow thread to be inspected again. */
exitVThreadTransitionCritical(currentThread, threadObj);

Expand Down Expand Up @@ -409,7 +409,6 @@ virtualThreadUnmountBegin(JNIEnv *env, jobject thread)
TRIGGER_J9HOOK_VM_VIRTUAL_THREAD_UNMOUNT(vm->hookInterface, currentThread);

enterVThreadTransitionCritical(currentThread, thread);
VM_VMHelpers::virtualThreadHideFrames(currentThread, JNI_TRUE);

J9InternalVMFunctions *vmFuncs = vm->internalVMFunctions;
j9object_t carrierThreadObject = currentThread->carrierThreadObject;
Expand All @@ -432,6 +431,8 @@ virtualThreadUnmountBegin(JNIEnv *env, jobject thread)
carrierThreadObject = currentThread->carrierThreadObject;
threadObj = J9_JNI_UNWRAP_REFERENCE(thread);
}

VM_VMHelpers::virtualThreadHideFrames(currentThread, JNI_TRUE);
}

/* Caller must have VMAccess. */
Expand All @@ -442,8 +443,6 @@ virtualThreadUnmountEnd(JNIEnv *env, jobject thread)
J9JavaVM *vm = currentThread->javaVM;
J9InternalVMFunctions *vmFuncs = vm->internalVMFunctions;

VM_VMHelpers::virtualThreadHideFrames(currentThread, JNI_FALSE);

j9object_t threadObj = J9_JNI_UNWRAP_REFERENCE(thread);
j9object_t continuationObj = J9VMJAVALANGVIRTUALTHREAD_CONT(currentThread, threadObj);
ContinuationState continuationState = *VM_ContinuationHelpers::getContinuationStateAddress(currentThread, continuationObj);
Expand All @@ -465,6 +464,8 @@ virtualThreadUnmountEnd(JNIEnv *env, jobject thread)
vmFuncs->freeTLS(currentThread, threadObj);
}

VM_VMHelpers::virtualThreadHideFrames(currentThread, JNI_FALSE);

/* Allow thread to be inspected again. */
exitVThreadTransitionCritical(currentThread, threadObj);
}
Expand Down Expand Up @@ -577,13 +578,31 @@ JNIEXPORT void JNICALL
JVM_VirtualThreadHideFrames(JNIEnv *env, jobject vthread, jboolean hide)
{
J9VMThread *currentThread = (J9VMThread *)env;
J9InternalVMFunctions const * const vmFuncs = currentThread->javaVM->internalVMFunctions;

vmFuncs->internalEnterVMFromJNI(currentThread);

j9object_t vThreadObj = currentThread->threadObject;
Assert_SC_true(IS_JAVA_LANG_VIRTUALTHREAD(currentThread, vThreadObj));
/* Do not allow JVMTI operations because J9VMThread->threadObject is modified
* between the first invocation with hide=true and the second invocation with
* hide=false. Otherwise, JVMTI functions will see an unstable
* J9VMThread->threadObject.
*/
bool hiddenFrames = J9_ARE_ALL_BITS_SET(currentThread->privateFlags, J9_PRIVATE_FLAGS_VIRTUAL_THREAD_HIDDEN_FRAMES);
if (hide) {
Assert_SC_true(J9_ARE_NO_BITS_SET(currentThread->privateFlags, J9_PRIVATE_FLAGS_VIRTUAL_THREAD_HIDDEN_FRAMES));
} else {
Assert_SC_true(J9_ARE_ALL_BITS_SET(currentThread->privateFlags, J9_PRIVATE_FLAGS_VIRTUAL_THREAD_HIDDEN_FRAMES));
Assert_SC_true(!hiddenFrames && (vThreadObj == J9_JNI_UNWRAP_REFERENCE(vthread)));
enterVThreadTransitionCritical(currentThread, vthread);
}

VM_VMHelpers::virtualThreadHideFrames(currentThread, hide);

if (!hide) {
babsingh marked this conversation as resolved.
Show resolved Hide resolved
Assert_SC_true(hiddenFrames);
exitVThreadTransitionCritical(currentThread, vThreadObj);
}

vmFuncs->internalExitVMToJNI(currentThread);
}
#endif /* JAVA_SPEC_VERSION >= 20 */

Expand Down