8346306: Unattached thread can cause crash during VM exit if it calls wait_if_vm_exited

Reviewed-by: coleenp, ccheung
This commit is contained in:
David Holmes 2024-12-19 02:48:06 +00:00
parent b0c40aadd2
commit 484229e04b
2 changed files with 13 additions and 8 deletions

View File

@ -3319,8 +3319,9 @@ JVM_END
// VM Raw monitors (not to be confused with JvmtiRawMonitors) are a simple mutual exclusion
// lock (not actually monitors: no wait/notify) that is exported by the VM for use by JDK
// library code. They may be used by JavaThreads and non-JavaThreads and do not participate
// in the safepoint protocol, thread suspension, thread interruption, or anything of that
// nature. JavaThreads will be "in native" when using this API from JDK code.
// in the safepoint protocol, thread suspension, thread interruption, or most things of that
// nature, except JavaThreads will be blocked by VM_Exit::block_if_vm_exited if the VM has
// shutdown. JavaThreads will be "in native" when using this API from JDK code.
JNIEXPORT void* JNICALL JVM_RawMonitorCreate(void) {

View File

@ -611,12 +611,16 @@ void VM_Exit::doit() {
void VM_Exit::wait_if_vm_exited() {
if (_vm_exited &&
Thread::current_or_null() != _shutdown_thread) {
// _vm_exited is set at safepoint, and the Threads_lock is never released
// so we will block here until the process dies.
Threads_lock->lock();
ShouldNotReachHere();
if (_vm_exited) {
// Need to check for an unattached thread as only attached threads
// can acquire the lock.
Thread* current = Thread::current_or_null();
if (current != nullptr && current != _shutdown_thread) {
// _vm_exited is set at safepoint, and the Threads_lock is never released
// so we will block here until the process dies.
Threads_lock->lock();
ShouldNotReachHere();
}
}
}