2424#include <errno.h>
2525#include <fcntl.h>
2626#include <signal.h>
27+ #ifdef __APPLE__
28+ #include <spawn.h>
29+ extern char * * environ ;
30+ #endif
2731#include <sys/stat.h>
2832#include <sys/wait.h>
2933#include <unistd.h>
@@ -877,6 +881,9 @@ static cbm_proc_poll_t cbm_subprocess_poll_win(cbm_subprocess_t *process, cbm_pr
877881
878882#else /* POSIX */
879883
884+ /* Used by the fork+exec child. posix_spawn performs the same reset
885+ * declaratively via SETSIGDEF + SETSIGMASK, but Apple still forks for the
886+ * exec-failure fallback below, so this stays compiled everywhere. */
880887static void cbm_posix_reset_child_signals (void ) {
881888 struct sigaction action = {0 };
882889 action .sa_handler = SIG_DFL ;
@@ -891,6 +898,9 @@ static void cbm_posix_reset_child_signals(void) {
891898 (void )sigprocmask (SIG_SETMASK , & empty , NULL );
892899}
893900
901+ /* fork+exec child setup. On Apple this runs ONLY for the exec-failure
902+ * fallback (see cbm_posix_spawn_apple), which preserves the documented
903+ * "bogus binary => child exits 127" contract across platforms. */
894904static void cbm_posix_child_exec (cbm_subprocess_t * process , int input , int output , long max_fd ) {
895905 if (setpgid (0 , 0 ) < 0 ) {
896906 _exit (127 );
@@ -935,6 +945,74 @@ static int cbm_posix_fd_at_least_three(int fd) {
935945 return duplicate ;
936946}
937947
948+ #ifdef __APPLE__
949+ /* macOS: spawn instead of fork+exec.
950+ *
951+ * fork() duplicates the parent's whole address space bookkeeping, and an
952+ * ASan-instrumented parent carries an enormous shadow mapping. Past a
953+ * footprint threshold the child is killed (jetsam) BEFORE exec replaces the
954+ * image, so the call fails with the child already gone (ESRCH on reap) — a
955+ * spawn failure that looks like the launched tool crashing. The test suite hit
956+ * exactly this: `git init` inside a fixture failed once enough suites had run
957+ * ahead of it, and the symptom was an unrelated-looking assertion. The same
958+ * hazard is already documented in tests/test_daemon_runtime.c, which switched
959+ * to posix_spawn for the same reason.
960+ *
961+ * posix_spawn never copies the parent address space, so the footprint is
962+ * irrelevant. Every guarantee of the fork path is preserved:
963+ * - own process group (SETPGROUP + setpgroup(0)) — the kill-tree contract
964+ * - default signal dispositions and an empty mask (SETSIGDEF/SETSIGMASK)
965+ * - stdin/stdout/stderr wired to the caller's fds (adddup2)
966+ * - every OTHER descriptor closed: CLOEXEC_DEFAULT is Apple's equivalent of
967+ * the child's close-everything loop, and the three dup2'd fds stay open
968+ * because dup2 clears close-on-exec.
969+ * posix_spawnp keeps execvp's PATH semantics for a bare tool name. */
970+ static int cbm_posix_spawn_apple (cbm_subprocess_t * process , int input , int output , pid_t * pid_out ) {
971+ posix_spawn_file_actions_t actions ;
972+ posix_spawnattr_t attr ;
973+ if (posix_spawn_file_actions_init (& actions ) != 0 ) {
974+ return -1 ;
975+ }
976+ if (posix_spawnattr_init (& attr ) != 0 ) {
977+ (void )posix_spawn_file_actions_destroy (& actions );
978+ return -1 ;
979+ }
980+ sigset_t empty_mask ;
981+ sigset_t all_signals ;
982+ sigemptyset (& empty_mask );
983+ sigfillset (& all_signals );
984+ short flags = (short )(POSIX_SPAWN_SETPGROUP | POSIX_SPAWN_SETSIGDEF | POSIX_SPAWN_SETSIGMASK |
985+ POSIX_SPAWN_CLOEXEC_DEFAULT );
986+ bool configured = posix_spawnattr_setflags (& attr , flags ) == 0 &&
987+ posix_spawnattr_setpgroup (& attr , 0 ) == 0 &&
988+ posix_spawnattr_setsigmask (& attr , & empty_mask ) == 0 &&
989+ posix_spawnattr_setsigdefault (& attr , & all_signals ) == 0 &&
990+ posix_spawn_file_actions_adddup2 (& actions , input , STDIN_FILENO ) == 0 &&
991+ posix_spawn_file_actions_adddup2 (& actions , output , STDOUT_FILENO ) == 0 &&
992+ posix_spawn_file_actions_adddup2 (& actions , output , STDERR_FILENO ) == 0 ;
993+ pid_t pid = -1 ;
994+ int rc =
995+ configured ? posix_spawnp (& pid , process -> bin , & actions , & attr , process -> argv , environ ) : -1 ;
996+ (void )posix_spawn_file_actions_destroy (& actions );
997+ (void )posix_spawnattr_destroy (& attr );
998+ if (configured && rc == 0 && pid > 0 ) {
999+ * pid_out = pid ;
1000+ return 0 ;
1001+ }
1002+ /* posix_spawn reports an unusable binary itself, where fork+exec instead
1003+ * produces a child that exits 127. Callers (and tests) rely on the latter:
1004+ * "spawn_failed" means the SPAWN mechanism failed, not that the tool was
1005+ * missing. Fall back to fork+exec for exec-class errors so macOS and Linux
1006+ * classify a bogus binary identically; the ASan-fork hazard does not apply
1007+ * here, since this child exits immediately. */
1008+ if (configured && (rc == ENOENT || rc == EACCES || rc == ENOEXEC || rc == EISDIR ||
1009+ rc == ELOOP || rc == ENAMETOOLONG || rc == ENOTDIR )) {
1010+ return 1 ;
1011+ }
1012+ return -1 ;
1013+ }
1014+ #endif
1015+
9381016static int cbm_subprocess_spawn_posix (cbm_subprocess_t * process ) {
9391017 int input_flags = O_RDONLY ;
9401018#ifdef O_CLOEXEC
@@ -977,7 +1055,27 @@ static int cbm_subprocess_spawn_posix(cbm_subprocess_t *process) {
9771055 max_fd = 65536L ;
9781056 }
9791057
980- pid_t pid = fork ();
1058+ pid_t pid = -1 ;
1059+ #ifdef __APPLE__
1060+ int spawn_rc = cbm_posix_spawn_apple (process , input , output , & pid );
1061+ if (spawn_rc < 0 ) {
1062+ (void )close (input );
1063+ (void )close (output );
1064+ return -1 ;
1065+ }
1066+ if (spawn_rc > 0 ) { /* exec-class failure: reproduce the fork+exec 127 */
1067+ pid = fork ();
1068+ if (pid < 0 ) {
1069+ (void )close (input );
1070+ (void )close (output );
1071+ return -1 ;
1072+ }
1073+ if (pid == 0 ) {
1074+ cbm_posix_child_exec (process , input , output , max_fd );
1075+ }
1076+ }
1077+ #else
1078+ pid = fork ();
9811079 if (pid < 0 ) {
9821080 (void )close (input );
9831081 (void )close (output );
@@ -986,6 +1084,7 @@ static int cbm_subprocess_spawn_posix(cbm_subprocess_t *process) {
9861084 if (pid == 0 ) {
9871085 cbm_posix_child_exec (process , input , output , max_fd );
9881086 }
1087+ #endif
9891088 (void )close (input );
9901089 (void )close (output );
9911090
0 commit comments