fix(libkernel): report a child's stop and kill signals correctly on macOS hosts #11801

Merged
jotitadev merged 2 commits from upstream-pr/11801 into main 2026-10-28 16:35:56 +00:00
jotitadev commented 2026-10-27 08:17:49 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by jotitadev on 2026-10-27T08:17:49Z.


What

guest_posix_spawn fails on macOS for two reasons. Each one is a separate commit.

1. waitpid translates signals that are already in the FreeBSD numbering (libkernel, Process.cpp). waitpid_nid_postfix maps the stop and termination signal of a child from the Linux numbering to the FreeBSD one on every non-Windows host. Darwin already uses the BSD numbering, which matches FreeBSD for signals 1 to 31, so on macOS the table moves signals that were already correct:

child Darwin / FreeBSD reported on macOS before
stopped by SIGSTOP 17 20
killed by SIGBUS 10 30
killed by SIGSYS 12 31
killed by SIGUSR1 30 NotImplemented ("without a guest equivalent")

The table now applies only off macOS, so macOS passes the host signal through. Linux is unchanged.

2. The macOS write watch swallows a SIGBUS or SIGSEGV sent with raise or kill (libc, GuestWriteWatch.cpp). The write watch installs a process-wide handler for both signals at startup. When a signal is not a write to a watched page and no handler was there before, the handler restores the default action and returns. For a real fault that is enough: the instruction runs again and the process dies. A sent signal does not run again, so the process just keeps going. The spawn test's child-kill-bus child calls std::raise(SIGBUS) and, instead of dying, falls through to std::_Exit(99). With APS5_NO_WRITE_WATCH=1 the child dies with SIGBUS as expected.

The handler now raises the signal again after restoring the default action. The signal stays blocked until the handler returns, so a real fault still ends with the same signal, and a sent one now does too. If SIG_IGN was inherited, the behaviour stays as before. Linux has no handler here, since its write watch uses userfaultfd.

Tests:

  • GuestPosixSpawn.cpp finds its own executable with _NSGetExecutablePath on macOS instead of /proc/self/exe, using the same pattern as Spawn.cpp. It also adds a child killed by SIGUSR1, expected as guest 30. That number differs between Linux (10) and FreeBSD (30), and macOS used to reject it.
  • write_watch_raised is a new mode of write_watch_tests. It forks children that raise(SIGBUS) and raise(SIGSEGV) and requires that each one is terminated by that signal. The write watch test builds on its own from main on macOS, so this one runs without the port PRs.

Tested

Apple M2 Max, macOS 27.2, Apple clang 21, x86-64 Release under Rosetta 2. main alone does not build libkernel's tests on macOS, so guest_posix_spawn ran on main 2f0a0071f5 plus the open macOS build PRs (#7631, #11249, #11358, #10669, #10552, #7804, #11447, #11437, #11554), with both commits cherry-picked on top.

  • Before: guest_posix_spawn stops at GuestPosixSpawn.cpp:225, the copy of its own executable, because there is no /proc/self/exe.
  • With the test fix only:
    • Without the waitpid commit, it fails at :319 (SIGSTOP reported as 20), 3 out of 3.
    • Without the write watch commit, it fails at :332 (the SIGBUS child exits with status 99), 3 out of 3.
  • With both commits: guest_posix_spawn and the 11 write_watch_* tests pass 3 runs out of 3. No stopped children are left behind.
  • write_watch_raised on main alone (standalone core/libs/tests/write_watch build): it fails with a raised fault signal does not terminate the process without the GuestWriteWatch.cpp change, and the 11 tests pass 3 out of 3 with it.
  • Other tests: on the same tree, 52 signal, exception, fault, spawn, kill and write-watch tests give the same results with and without this PR, apart from guest_posix_spawn. The ones that still fail are known Rosetta FP failures (guest_raise_except*) or tests that do not build there (#4630).
  • Linux (Ubuntu, kernel 7.0, GCC 15.2): clean clone of this branch, Release. guest_posix_spawn (including the new SIGUSR1 child, 10 → 30) and guest_waitpid pass 3 out of 3.
  • Not run on Windows: the waitpid change is in the non-Windows branch, and the spawn test's new child is under #ifndef _WIN32.

check_conventions.py --base origin/main passes.

Checklist

  • Based on current main; no other open PR implements the same functions
  • One topic per PR; follow-ups go in a new PR. Both commits fix how a child's death by a signal reaches waitpid on macOS, and guest_posix_spawn needs both. I can split them if you prefer.
  • No comments in code except technical debt
  • Unimplemented paths throw (NotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file
  • No notes, investigation .md files or images added to the repository (attach them to this PR)
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: none for the change. Building guest_posix_spawn on macOS also needs #11358 (environ) and the open macOS build PRs listed above.
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes (Claude Code)

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5030729888 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/11801). Originally posted by [jotitadev](https://github.com/jotitadev) on 2026-10-27T08:17:49Z. --- ### What `guest_posix_spawn` fails on macOS for two reasons. Each one is a separate commit. **1. `waitpid` translates signals that are already in the FreeBSD numbering** (`libkernel`, `Process.cpp`). `waitpid_nid_postfix` maps the stop and termination signal of a child from the Linux numbering to the FreeBSD one on every non-Windows host. Darwin already uses the BSD numbering, which matches FreeBSD for signals 1 to 31, so on macOS the table moves signals that were already correct: | child | Darwin / FreeBSD | reported on macOS before | |---|---|---| | stopped by `SIGSTOP` | 17 | 20 | | killed by `SIGBUS` | 10 | 30 | | killed by `SIGSYS` | 12 | 31 | | killed by `SIGUSR1` | 30 | `NotImplemented` ("without a guest equivalent") | The table now applies only off macOS, so macOS passes the host signal through. Linux is unchanged. **2. The macOS write watch swallows a `SIGBUS` or `SIGSEGV` sent with `raise` or `kill`** (`libc`, `GuestWriteWatch.cpp`). The write watch installs a process-wide handler for both signals at startup. When a signal is not a write to a watched page and no handler was there before, the handler restores the default action and returns. For a real fault that is enough: the instruction runs again and the process dies. A sent signal does not run again, so the process just keeps going. The spawn test's `child-kill-bus` child calls `std::raise(SIGBUS)` and, instead of dying, falls through to `std::_Exit(99)`. With `APS5_NO_WRITE_WATCH=1` the child dies with `SIGBUS` as expected. The handler now raises the signal again after restoring the default action. The signal stays blocked until the handler returns, so a real fault still ends with the same signal, and a sent one now does too. If `SIG_IGN` was inherited, the behaviour stays as before. Linux has no handler here, since its write watch uses userfaultfd. **Tests:** - `GuestPosixSpawn.cpp` finds its own executable with `_NSGetExecutablePath` on macOS instead of `/proc/self/exe`, using the same pattern as `Spawn.cpp`. It also adds a child killed by `SIGUSR1`, expected as guest 30. That number differs between Linux (10) and FreeBSD (30), and macOS used to reject it. - `write_watch_raised` is a new mode of `write_watch_tests`. It forks children that `raise(SIGBUS)` and `raise(SIGSEGV)` and requires that each one is terminated by that signal. The write watch test builds on its own from `main` on macOS, so this one runs without the port PRs. ### Tested Apple M2 Max, macOS 27.2, Apple clang 21, x86-64 Release under Rosetta 2. `main` alone does not build libkernel's tests on macOS, so `guest_posix_spawn` ran on `main` 2f0a0071f5 plus the open macOS build PRs (#7631, #11249, #11358, #10669, #10552, #7804, #11447, #11437, #11554), with both commits cherry-picked on top. - **Before:** `guest_posix_spawn` stops at `GuestPosixSpawn.cpp:225`, the copy of its own executable, because there is no `/proc/self/exe`. - **With the test fix only:** - Without the `waitpid` commit, it fails at :319 (`SIGSTOP` reported as 20), 3 out of 3. - Without the write watch commit, it fails at :332 (the `SIGBUS` child exits with status 99), 3 out of 3. - **With both commits:** `guest_posix_spawn` and the 11 `write_watch_*` tests pass 3 runs out of 3. No stopped children are left behind. - **`write_watch_raised` on `main` alone** (standalone `core/libs/tests/write_watch` build): it fails with `a raised fault signal does not terminate the process` without the `GuestWriteWatch.cpp` change, and the 11 tests pass 3 out of 3 with it. - **Other tests:** on the same tree, 52 signal, exception, fault, spawn, kill and write-watch tests give the same results with and without this PR, apart from `guest_posix_spawn`. The ones that still fail are known Rosetta FP failures (`guest_raise_except*`) or tests that do not build there (#4630). - **Linux** (Ubuntu, kernel 7.0, GCC 15.2): clean clone of this branch, Release. `guest_posix_spawn` (including the new `SIGUSR1` child, 10 → 30) and `guest_waitpid` pass 3 out of 3. - Not run on Windows: the `waitpid` change is in the non-Windows branch, and the spawn test's new child is under `#ifndef _WIN32`. `check_conventions.py --base origin/main` passes. ### Checklist <!-- Rules behind each item: https://github.com/boykopovar/AnyPS5/blob/main/CONTRIBUTING.md --> - [x] Based on current `main`; no other open PR implements the same functions - [x] One topic per PR; follow-ups go in a new PR. Both commits fix how a child's death by a signal reaches `waitpid` on macOS, and `guest_posix_spawn` needs both. I can split them if you prefer. - [x] No comments in code except [technical debt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/CONVENTIONS.md) - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed under Silent stubs in their library's [technical debt](https://github.com/boykopovar/AnyPS5/tree/main/docs/dev/debt) file - [x] No notes, investigation `.md` files or images added to the repository (attach them to this PR) - [x] New third-party code is a submodule built from source - [x] Generic behaviour, not specific to one title - [x] Depends on: none for the change. Building `guest_posix_spawn` on macOS also needs #11358 (`environ`) and the open macOS build PRs listed above. - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes (Claude Code) 🤖 Generated with [Claude Code](https://claude.com/claude-code)
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
boykopovar/AnyPS5!11801
No description provided.