Skip to content

Commit

Permalink
Don't invoke shutdown signal handler until JVM init completes
Browse files Browse the repository at this point in the history
JVM init path: J9_CreateJavaVM.
JVM exit paths: protectedDestroyJavaVM and exitJavaVM.

A segfault or other side-effects can happen if the JVM init and
exit paths execute concurrently.

The exit path can be taken if a shutdown signal is raised and the
shutdown handler is invoked. JVM shutdown signals are SIGINT, SIGTERM
and SIGHUP.

Preventing invocation of the exit path from the shutdown signal handler
until the JVM initialization completes resolves the above side-effects.

Related:
- #17101
- #17438

Signed-off-by: Babneet Singh <[email protected]>
  • Loading branch information
babsingh committed Sep 8, 2023
1 parent 87d042a commit 4676e0b
Showing 1 changed file with 10 additions and 7 deletions.
17 changes: 10 additions & 7 deletions runtime/vm/jvminit.c
Original file line number Diff line number Diff line change
Expand Up @@ -7898,8 +7898,9 @@ predefinedHandlerWrapper(struct J9PortLibrary *portLibrary, U_32 gpType, void *g
J9JavaVMAttachArgs attachArgs = {0};
J9VMThread *vmThread = NULL;
IDATA result = JNI_ERR;
BOOLEAN shutdownStarted = FALSE;
BOOLEAN invokeHandler = TRUE;
I_32 signal = 0;
U_32 runtimeFlags = 0;
PORT_ACCESS_FROM_JAVAVM(vm);

signal = j9sig_map_portlib_signal_to_os_signal(gpType);
Expand All @@ -7908,14 +7909,16 @@ predefinedHandlerWrapper(struct J9PortLibrary *portLibrary, U_32 gpType, void *g
return 1;
}

/* Don't invoke handler if JVM exit has started. */
omrthread_monitor_enter(vm->runtimeFlagsMutex);
if (J9_ARE_ANY_BITS_SET(vm->runtimeFlags, J9_RUNTIME_EXIT_STARTED)) {
shutdownStarted = TRUE;
/* Don't invoke handler if JVM hasn't initialized or JVM exit has started. */
issueReadBarrier();
runtimeFlags = vm->runtimeFlags;
if (J9_ARE_NO_BITS_SET(runtimeFlags, J9_RUNTIME_INITIALIZED)
|| J9_ARE_ANY_BITS_SET(runtimeFlags, J9_RUNTIME_EXIT_STARTED)
) {
invokeHandler = FALSE;
}
omrthread_monitor_exit(vm->runtimeFlagsMutex);

if (shutdownStarted) {
if (!invokeHandler) {
return 1;
}

Expand Down

0 comments on commit 4676e0b

Please sign in to comment.