macOS: spawn instead of fork+exec. * * fork() duplicates the parent's whole address space bookkeeping, and an * ASan-instrumented parent carries an enormous shadow mapping. Past a * footprint threshold the child is killed (jetsam) BEFORE exec replaces the * image, so the call fails with the child already gone (ESRCH on reap) — a * spawn failure that looks like the launched tool crashing. The
| 1548 | * because dup2 clears close-on-exec. |
| 1549 | * posix_spawnp keeps execvp's PATH semantics for a bare tool name. */ |
| 1550 | static int cbm_posix_spawn_apple(cbm_subprocess_t *process, int input, int output, int error, |
| 1551 | pid_t *pid_out) { |
| 1552 | /* posix_spawn is the PRIMARY path on macOS; fork+exec is only the |
| 1553 | * exec-class fallback. Injection therefore has to live here too, or a test |
| 1554 | * on macOS exercises nothing. */ |
| 1555 | #ifdef CBM_ENABLE_TEST_SEAMS |
| 1556 | if (cbm_spawn_eagain_injected()) { |
| 1557 | return CBM_SPAWN_RETRY; |
| 1558 | } |
| 1559 | #endif |
| 1560 | posix_spawn_file_actions_t actions; |
| 1561 | posix_spawnattr_t attr; |
| 1562 | if (posix_spawn_file_actions_init(&actions) != 0) { |
| 1563 | return -1; |
| 1564 | } |
| 1565 | if (posix_spawnattr_init(&attr) != 0) { |
| 1566 | (void)posix_spawn_file_actions_destroy(&actions); |
| 1567 | return -1; |
| 1568 | } |
| 1569 | sigset_t empty_mask; |
| 1570 | sigset_t all_signals; |
| 1571 | sigemptyset(&empty_mask); |
| 1572 | sigfillset(&all_signals); |
| 1573 | short flags = (short)(POSIX_SPAWN_SETPGROUP | POSIX_SPAWN_SETSIGDEF | POSIX_SPAWN_SETSIGMASK | |
| 1574 | POSIX_SPAWN_CLOEXEC_DEFAULT); |
| 1575 | bool configured = posix_spawnattr_setflags(&attr, flags) == 0 && |
| 1576 | posix_spawnattr_setpgroup(&attr, 0) == 0 && |
| 1577 | posix_spawnattr_setsigmask(&attr, &empty_mask) == 0 && |
| 1578 | posix_spawnattr_setsigdefault(&attr, &all_signals) == 0 && |
| 1579 | posix_spawn_file_actions_adddup2(&actions, input, STDIN_FILENO) == 0 && |
| 1580 | posix_spawn_file_actions_adddup2(&actions, output, STDOUT_FILENO) == 0 && |
| 1581 | posix_spawn_file_actions_adddup2(&actions, error, STDERR_FILENO) == 0; |
| 1582 | pid_t pid = -1; |
| 1583 | char **environment = process->environment; |
| 1584 | if (!environment) { |
| 1585 | environment = process->envp ? process->envp : environ; |
| 1586 | } |
| 1587 | int rc = configured |
| 1588 | ? posix_spawnp(&pid, process->bin, &actions, &attr, process->argv, environment) |
| 1589 | : -1; |
| 1590 | (void)posix_spawn_file_actions_destroy(&actions); |
| 1591 | (void)posix_spawnattr_destroy(&attr); |
| 1592 | if (configured && rc == 0 && pid > 0) { |
| 1593 | *pid_out = pid; |
| 1594 | return 0; |
| 1595 | } |
| 1596 | /* posix_spawn reports an unusable binary itself, where fork+exec instead |
| 1597 | * produces a child that exits 127. Callers (and tests) rely on the latter: |
| 1598 | * "spawn_failed" means the SPAWN mechanism failed, not that the tool was |
| 1599 | * missing. Fall back to fork+exec for exec-class errors so macOS and Linux |
| 1600 | * classify a bogus binary identically; the ASan-fork hazard does not apply |
| 1601 | * here, since this child exits immediately. */ |
| 1602 | if (configured && (rc == ENOENT || rc == EACCES || rc == ENOEXEC || rc == EISDIR || |
| 1603 | rc == ELOOP || rc == ENAMETOOLONG || rc == ENOTDIR)) { |
| 1604 | return 1; |
| 1605 | } |
| 1606 | /* EAGAIN/ENOMEM are the kernel saying "not right now", not "never": the |
| 1607 | * process table or a per-user limit is momentarily full. Reporting |
no test coverage detected