From a112dfe6fea99c835d7f165033a18ad01ba549f8 Mon Sep 17 00:00:00 2001 From: Shane Snyder Date: Thu, 13 Apr 2023 17:45:27 -0500 Subject: [PATCH 1/2] fix potential segfault with app exclusions+fork Darshan fork handlers expect Darshan library state to be set, but this doesn't happen when an app is excluded from instrumentation. To fix, don't set Darshan fork handlers until it's known that library initialization succeeded. --- darshan-runtime/lib/darshan-core.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/darshan-runtime/lib/darshan-core.c b/darshan-runtime/lib/darshan-core.c index 4f16f447d..28a006003 100644 --- a/darshan-runtime/lib/darshan-core.c +++ b/darshan-runtime/lib/darshan-core.c @@ -232,12 +232,6 @@ void darshan_core_initialize(int argc, char **argv) /* set PID that initialized Darshan runtime */ init_core->pid = getpid(); - /* setup fork handlers if not using MPI */ - if(!using_mpi && !orig_parent_pid) - { - pthread_atfork(NULL, NULL, &darshan_core_fork_child_cb); - } - /* parse any user-supplied runtime configuration of Darshan */ /* NOTE: as the ordering implies, environment variables override any * config file parameters @@ -352,6 +346,12 @@ void darshan_core_initialize(int argc, char **argv) init_core->config.mod_disabled = ~(init_core->config.mod_disabled & 0); } + /* setup fork handlers if not using MPI */ + if(!using_mpi && !orig_parent_pid) + { + pthread_atfork(NULL, NULL, &darshan_core_fork_child_cb); + } + /* if darshan was successfully initialized, set the global pointer * and record absolute start time so that we can later generate * relative times with this as a reference point. From c3605f73e344033b246c4ca383fe6ca21f95a39c Mon Sep 17 00:00:00 2001 From: Shane Snyder Date: Thu, 13 Apr 2023 18:02:36 -0500 Subject: [PATCH 2/2] additional safety check inside fork handler don't do anything if darshan_core is NULL --- darshan-runtime/lib/darshan-core.c | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/darshan-runtime/lib/darshan-core.c b/darshan-runtime/lib/darshan-core.c index 28a006003..0bd7c5554 100644 --- a/darshan-runtime/lib/darshan-core.c +++ b/darshan-runtime/lib/darshan-core.c @@ -2182,16 +2182,19 @@ static void darshan_core_cleanup(struct darshan_core_runtime* core) static void darshan_core_fork_child_cb(void) { - /* hold onto the original parent PID, which we will use as jobid if the user didn't - * provide a jobid env variable - */ - parent_pid = __darshan_core->pid; - if(!orig_parent_pid) - orig_parent_pid = parent_pid; + if(__darshan_core) + { + /* hold onto the original parent PID, which we will use as jobid if the user didn't + * provide a jobid env variable + */ + parent_pid = __darshan_core->pid; + if(!orig_parent_pid) + orig_parent_pid = parent_pid; - /* shutdown and re-init darshan, making sure to not write out a log file */ - darshan_core_shutdown(0); - darshan_core_initialize(0, NULL); + /* shutdown and re-init darshan, making sure to not write out a log file */ + darshan_core_shutdown(0); + darshan_core_initialize(0, NULL); + } return; }