feat(libSceLibcInternal): implement posix_spawn process creation and reaping #9133

Merged
fogydes merged 7 commits from upstream-pr/9133 into main 2026-10-25 16:38:42 +00:00
fogydes commented 2026-10-21 12:57:48 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by fogydes on 2026-10-21T12:57:48Z.


What

posix_spawn and posix_spawnp (libSceLibcInternal) create a real child process, and waitpid/wait (libkernel) reap it. The child runs the already-converted image of the requested guest program: the path must be under /app0/ (/app0/eboot.bin maps to the running image itself; anything else is found beside the running image under its flattened name), posix_spawnp searches PATH as FreeBSD 11's _execvpe does (_PATH_DEFPATH when unset, ENOENT otherwise), file actions and attributes come from the FreeBSD 11.4 objects, a null argv returns EFAULT (14) and a null envp gives the child the parent's environment. Guest process IDs are the host's on both hosts. On Linux the spawn delegates to the host posix_spawn with full file-action fidelity and the POSIX_SPAWN_RESETIDS/POSIX_SPAWN_SETPGROUP flags translated. On Windows the child keeps the parent's stdin/stdout/stderr through duplicated inheritable handles with a STARTF_USESTDHANDLES handle list, file actions deliver only descriptors 0-2 (addopen/adddup2/addclose; a target beyond that throws), and a nonzero attribute flag set throws where FreeBSD would apply signal attributes. waitpid reaps these children: on Linux straight from the host call with the stopped child's signal, from bits 8-15, and a killed child's signal, from bits 0-6, translated to the guest's BSD numbering (7, 10, 12, 17, 18, 19, 20, 23, 29, 31 -> 10, 30, 31, 20, 19, 17, 18, 16, 23, 12; Linux 16 and 30 throw), a continued status reported as 0x13, and the guest WCONTINUED option (4) translated to Linux's 8; on Windows through a child-handle registry in libc (internal glue NIDs RegisterChildProcess_nid_no_patch / WaitChildProcess_nid_no_patch, needed because libSceLibcInternal imports libc but not libkernel) with an exit-status decode: host exit codes keep the guest exit byte layout, access violation/stack overflow -> SIGSEGV, illegal instruction -> SIGILL, breakpoint -> SIGTRAP, division -> SIGFPE, fastfail/heap corruption -> SIGABRT, and any other code, including a guest exit(-1) (0xFFFFFFFF), keeps the guest exit byte layout; no stop/continue states are ever produced on Windows. Unknown options bits keep EINVAL; calls without a posix_spawn child keep ECHILD. Limitations are recorded in the debt notes: the child executes the conversion made at relink time, and the console spawn sandbox is unverified.

Tested

Windows 11 x64, MinGW-w64 GCC 15.2: guest_posix_spawn passes, including the new real-execution coverage (two staged child copies: immediate exit 42 reaped, sleeping child reporting WNOHANG and reaped with 43, then ECHILD with untouched errno) and the waitpid rows: a natural exit(-1) reading as 255, and TerminateProcess codes 0xC0000005 -> 11, 0xC0000096 -> 4, 0x80000003 -> 5, 0xC0000094 -> 8 and 0xC0000409 -> 6 through the decoder; all pre-existing FreeBSD error-path locks still pass. guest_waitpid, guest_kernel_sys, guest_system_exit, guest_system_exit_parameters and loadexec_arguments pass with the rebuilt libc, libkernel and libSceSystemService. Linux x64 Debian (gcc 14.2, WSL2 native ext4): guest_posix_spawn (same real-child coverage plus the waitpid rows: WUNTRACED reports a stopped child as guest 17 keeping the 0x7f marker, SIGCONT continues it and WCONTINUED reports 0x13, then exit 44; killed rows SIGBUS 7 -> 10 and SIGSYS 31 -> 12), guest_waitpid, guest_kernel_sys, guest_system_exit, guest_system_exit_parameters and loadexec_arguments all pass. guest_kernel_sys fails only when run from a /mnt/d drvfs working directory (the directory rename/replacement marker behind it), reproduced before this change as well and unrelated to the spawn/waitpid diff. PPSA99305 (Dusklight) not run end to end; the spawn path it reaches is covered by guest_posix_spawn.

Checklist

  • Based on current main; no other open PR implements the same functions
  • One topic per PR; follow-ups go in a new PR
  • 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
  • Fixes: none
  • Unblocks: PPSA99305 #7971 (SDL searches for zenity through posix_spawn); other spawn/waitpid titles from discussion #7971 (a fork follow-up stays separate)
  • GPUs tested: n/a
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4951939837 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9133). Originally posted by [fogydes](https://github.com/fogydes) on 2026-10-21T12:57:48Z. --- ### What `posix_spawn` and `posix_spawnp` (libSceLibcInternal) create a real child process, and `waitpid`/`wait` (libkernel) reap it. The child runs the already-converted image of the requested guest program: the path must be under `/app0/` (`/app0/eboot.bin` maps to the running image itself; anything else is found beside the running image under its flattened name), `posix_spawnp` searches `PATH` as FreeBSD 11's `_execvpe` does (`_PATH_DEFPATH` when unset, `ENOENT` otherwise), file actions and attributes come from the FreeBSD 11.4 objects, a null `argv` returns `EFAULT` (14) and a null `envp` gives the child the parent's environment. Guest process IDs are the host's on both hosts. On Linux the spawn delegates to the host `posix_spawn` with full file-action fidelity and the `POSIX_SPAWN_RESETIDS`/`POSIX_SPAWN_SETPGROUP` flags translated. On Windows the child keeps the parent's stdin/stdout/stderr through duplicated inheritable handles with a `STARTF_USESTDHANDLES` handle list, file actions deliver only descriptors 0-2 (addopen/adddup2/addclose; a target beyond that throws), and a nonzero attribute flag set throws where FreeBSD would apply signal attributes. `waitpid` reaps these children: on Linux straight from the host call with the stopped child's signal, from bits 8-15, and a killed child's signal, from bits 0-6, translated to the guest's BSD numbering (7, 10, 12, 17, 18, 19, 20, 23, 29, 31 -> 10, 30, 31, 20, 19, 17, 18, 16, 23, 12; Linux 16 and 30 throw), a continued status reported as 0x13, and the guest `WCONTINUED` option (4) translated to Linux's 8; on Windows through a child-handle registry in libc (internal glue NIDs `RegisterChildProcess_nid_no_patch` / `WaitChildProcess_nid_no_patch`, needed because libSceLibcInternal imports libc but not libkernel) with an exit-status decode: host exit codes keep the guest `exit` byte layout, access violation/stack overflow -> `SIGSEGV`, illegal instruction -> `SIGILL`, breakpoint -> `SIGTRAP`, division -> `SIGFPE`, fastfail/heap corruption -> `SIGABRT`, and any other code, including a guest `exit(-1)` (0xFFFFFFFF), keeps the guest `exit` byte layout; no stop/continue states are ever produced on Windows. Unknown `options` bits keep `EINVAL`; calls without a `posix_spawn` child keep `ECHILD`. Limitations are recorded in the debt notes: the child executes the conversion made at relink time, and the console spawn sandbox is unverified. ### Tested <!-- OS, title or homebrew, what was checked. "Not tested" is a valid answer. A performance change: the summary lines of main and of this PR from the same machine and scene, with the traces attached, captured as the Performance report issue form describes. --> Windows 11 x64, MinGW-w64 GCC 15.2: `guest_posix_spawn` passes, including the new real-execution coverage (two staged child copies: immediate exit 42 reaped, sleeping child reporting `WNOHANG` and reaped with 43, then `ECHILD` with untouched `errno`) and the waitpid rows: a natural `exit(-1)` reading as 255, and `TerminateProcess` codes 0xC0000005 -> 11, 0xC0000096 -> 4, 0x80000003 -> 5, 0xC0000094 -> 8 and 0xC0000409 -> 6 through the decoder; all pre-existing FreeBSD error-path locks still pass. `guest_waitpid`, `guest_kernel_sys`, `guest_system_exit`, `guest_system_exit_parameters` and `loadexec_arguments` pass with the rebuilt `libc`, `libkernel` and `libSceSystemService`. Linux x64 Debian (gcc 14.2, WSL2 native ext4): `guest_posix_spawn` (same real-child coverage plus the waitpid rows: `WUNTRACED` reports a stopped child as guest 17 keeping the 0x7f marker, `SIGCONT` continues it and `WCONTINUED` reports 0x13, then exit 44; killed rows `SIGBUS` 7 -> 10 and `SIGSYS` 31 -> 12), `guest_waitpid`, `guest_kernel_sys`, `guest_system_exit`, `guest_system_exit_parameters` and `loadexec_arguments` all pass. `guest_kernel_sys` fails only when run from a `/mnt/d` drvfs working directory (the directory rename/replacement marker behind it), reproduced before this change as well and unrelated to the spawn/waitpid diff. PPSA99305 (Dusklight) not run end to end; the spawn path it reaches is covered by `guest_posix_spawn`. ### 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 - [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 - [x] Fixes: none - [x] Unblocks: PPSA99305 #7971 (SDL searches for `zenity` through `posix_spawn`); other spawn/waitpid titles from discussion #7971 (a `fork` follow-up stays separate) - [x] GPUs tested: n/a - [x] AI-assisted: yes
fogydes commented 2026-10-22 07:51:31 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by fogydes on 2026-10-22T07:51:31Z.


The reverted-function flag is the progress bot's stub classifier, not an actual revert. tools/progress.py counts a function as implemented only when its body — directly or through any helper — no longer reaches NotImplemented_nid_no_patch (the STUB_WRAPPER closure feeding scan_library). The waitpid change could not avoid that: its Linux arm throws for child-death signals 16/29/30 that have no mapping on the console's BSD axis, which the project convention requires (unrepresentable behaviour throws and is recorded in docs/dev/debt/libkernel.md). Everything else in waitpid is implemented and covered by guest_waitpid; the old code simply failed those cases silently, which is why the base counted it as done.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6335654223 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9133#issuecomment-6335654223). Originally posted by [fogydes](https://github.com/fogydes) on 2026-10-22T07:51:31Z. --- The reverted-function flag is the progress bot's stub classifier, not an actual revert. `tools/progress.py` counts a function as implemented only when its body — directly or through any helper — no longer reaches `NotImplemented_nid_no_patch` (the `STUB_WRAPPER` closure feeding `scan_library`). The `waitpid` change could not avoid that: its Linux arm throws for child-death signals 16/29/30 that have no mapping on the console's BSD axis, which the project convention requires (unrepresentable behaviour throws and is recorded in `docs/dev/debt/libkernel.md`). Everything else in `waitpid` is implemented and covered by `guest_waitpid`; the old code simply failed those cases silently, which is why the base counted it as done.
adriadam10 commented 2026-10-23 20:15:21 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-23T20:15:21Z.

Review: CHANGES_REQUESTED


Thanks, the structure is good (Linux host posix_spawn, Windows child registry in libc, debt notes). A few things to fix before merge:

  • core/libs/prx/libkernel/System/src/Process.cpp:466-479 (Linux waitpid): a stopped status is (signal << 8) | 0x7f, but (encoded & ~0x7f) | 10 writes the BSD number into the low bits and destroys the 0x7f marker. Translate the signal in bits 8-15 for WIFSTOPPED, and in bits 0-6 only for WIFSIGNALED.
  • Same block: the Linux→FreeBSD signal map is partial. FreeBSD numbers differ for more than 7/10/12: SIGCHLD 17→20, SIGCONT 18→19, SIGSTOP 19→17, SIGTSTP 20→18, SIGURG 23→16, SIGIO 29→23, SIGSYS 31→12 (and 16, 30 have no equivalent). With WUNTRACED, a stopped child reports the wrong signal today (SIGSTOP would read as SIGCHLD...). Please map the whole table (FreeBSD sys/sys/signal.h) and throw only for the unmapped ones, with a test using a child that stops/is killed.
  • Same function: guest WCONTINUED is 4 on FreeBSD but 8 on Linux, so options & (WNOHANG | WUNTRACED | WCONTINUED) silently drops it; translate the bit, and the continued status (0xffff on Linux, SIGCONT << 8 | 0x7f-style 0x13 on FreeBSD).
  • core/libs/prx/libc/src/Process.cpp:295-312 (Windows): any exit code >= 0x80000000 throws as "not representable", including a guest exit(-1)/exit(255) seen as 0xFFFFFFFF. Please show how the guest exit status reaches ExitProcess and keep that layout for it (only NTSTATUS-looking codes with the 0xC/0x8 severity should hit the signal mapping), with a test row for exit(-1).
  • core/libs/prx/libc/src/Process.cpp:357-365: WaitForMultipleObjects(..., 250) runs with childProcessMutex held, so every RegisterChildProcess/other waitpid stalls up to 250 ms per 64 children. Copy the handles under the lock and wait outside it (and drop the hang name: that flag is WNOHANG).
  • Tests: the Linux signal translation and the Windows structured-exception mapping have no coverage; only exit 42/43 are pinned.
<!-- greyfinch-sync:boykopovar/AnyPS5:review:5633925988 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9133#pullrequestreview-5633925988). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-23T20:15:21Z. **Review:** CHANGES_REQUESTED --- Thanks, the structure is good (Linux host `posix_spawn`, Windows child registry in libc, debt notes). A few things to fix before merge: - `core/libs/prx/libkernel/System/src/Process.cpp:466-479` (Linux `waitpid`): a stopped status is `(signal << 8) | 0x7f`, but `(encoded & ~0x7f) | 10` writes the BSD number into the low bits and destroys the `0x7f` marker. Translate the signal in bits 8-15 for `WIFSTOPPED`, and in bits 0-6 only for `WIFSIGNALED`. - Same block: the Linux→FreeBSD signal map is partial. FreeBSD numbers differ for more than 7/10/12: SIGCHLD 17→20, SIGCONT 18→19, SIGSTOP 19→17, SIGTSTP 20→18, SIGURG 23→16, SIGIO 29→23, SIGSYS 31→12 (and 16, 30 have no equivalent). With `WUNTRACED`, a stopped child reports the wrong signal today (SIGSTOP would read as SIGCHLD...). Please map the whole table (FreeBSD `sys/sys/signal.h`) and throw only for the unmapped ones, with a test using a child that stops/is killed. - Same function: guest `WCONTINUED` is 4 on FreeBSD but 8 on Linux, so `options & (WNOHANG | WUNTRACED | WCONTINUED)` silently drops it; translate the bit, and the continued status (`0xffff` on Linux, `SIGCONT << 8 | 0x7f`-style 0x13 on FreeBSD). - `core/libs/prx/libc/src/Process.cpp:295-312` (Windows): any exit code >= 0x80000000 throws as "not representable", including a guest `exit(-1)`/`exit(255)` seen as 0xFFFFFFFF. Please show how the guest `exit` status reaches `ExitProcess` and keep that layout for it (only NTSTATUS-looking codes with the 0xC/0x8 severity should hit the signal mapping), with a test row for `exit(-1)`. - `core/libs/prx/libc/src/Process.cpp:357-365`: `WaitForMultipleObjects(..., 250)` runs with `childProcessMutex` held, so every `RegisterChildProcess`/other `waitpid` stalls up to 250 ms per 64 children. Copy the handles under the lock and wait outside it (and drop the `hang` name: that flag is `WNOHANG`). - Tests: the Linux signal translation and the Windows structured-exception mapping have no coverage; only exit 42/43 are pinned.
adriadam10 commented 2026-10-24 06:30:46 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-24T06:30:46Z.

Review: CHANGES_REQUESTED


Thanks for the update. The new head only changes docs and the Apple _NSGetExecutablePath declaration; the points from the previous review are still open:

  • core/libs/prx/libkernel/System/src/Process.cpp:466-479 (Linux waitpid): (encoded & ~0x7f) | 10 still overwrites the low 7 bits, so a stopped status (sig << 8) | 0x7f loses its marker. Translate the signal in bits 8-15 for WIFSTOPPED, bits 0-6 for WIFSIGNALED.
  • Same block: the Linux→FreeBSD map only covers 7/10/12. Map the whole table from FreeBSD sys/sys/signal.h (SIGCHLD 17→20, SIGCONT 18→19, SIGSTOP 19→17, SIGTSTP 20→18, SIGURG 23→16, SIGIO 29→23, SIGSYS 31→12) and throw only for unmapped ones.
  • Same function: guest WCONTINUED is 4, Linux's is 8, so options & (WNOHANG | WUNTRACED | WCONTINUED) drops it. Translate the bit and the continued status.
  • core/libs/prx/libc/src/Process.cpp:295-312 (Windows): every code >= 0x80000000 throws, including a guest exit(-1) (0xFFFFFFFF). Keep the guest exit layout and only map NTSTATUS-looking codes (0xC/0x8 severity) as signals; add a test row for exit(-1).
  • core/libs/prx/libc/src/Process.cpp:357-365: WaitForMultipleObjects(..., 250) still runs under childProcessMutex. Copy the handles under the lock and wait outside it (and rename hang, that flag is WNOHANG).
  • Tests: add coverage for the Linux signal translation (a child that stops / is killed) and the Windows exception mapping; only exit 42/43 are pinned.
<!-- greyfinch-sync:boykopovar/AnyPS5:review:5637164825 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9133#pullrequestreview-5637164825). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-24T06:30:46Z. **Review:** CHANGES_REQUESTED --- Thanks for the update. The new head only changes docs and the Apple `_NSGetExecutablePath` declaration; the points from the previous review are still open: - `core/libs/prx/libkernel/System/src/Process.cpp:466-479` (Linux `waitpid`): `(encoded & ~0x7f) | 10` still overwrites the low 7 bits, so a stopped status `(sig << 8) | 0x7f` loses its marker. Translate the signal in bits 8-15 for `WIFSTOPPED`, bits 0-6 for `WIFSIGNALED`. - Same block: the Linux→FreeBSD map only covers 7/10/12. Map the whole table from FreeBSD `sys/sys/signal.h` (SIGCHLD 17→20, SIGCONT 18→19, SIGSTOP 19→17, SIGTSTP 20→18, SIGURG 23→16, SIGIO 29→23, SIGSYS 31→12) and throw only for unmapped ones. - Same function: guest `WCONTINUED` is 4, Linux's is 8, so `options & (WNOHANG | WUNTRACED | WCONTINUED)` drops it. Translate the bit and the continued status. - `core/libs/prx/libc/src/Process.cpp:295-312` (Windows): every code >= 0x80000000 throws, including a guest `exit(-1)` (0xFFFFFFFF). Keep the guest exit layout and only map NTSTATUS-looking codes (0xC/0x8 severity) as signals; add a test row for `exit(-1)`. - `core/libs/prx/libc/src/Process.cpp:357-365`: `WaitForMultipleObjects(..., 250)` still runs under `childProcessMutex`. Copy the handles under the lock and wait outside it (and rename `hang`, that flag is `WNOHANG`). - Tests: add coverage for the Linux signal translation (a child that stops / is killed) and the Windows exception mapping; only exit 42/43 are pinned.
adriadam10 commented 2026-10-24 14:44:27 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-24T14:44:27Z.

Review: CHANGES_REQUESTED


Re-checked on 21b467c6c9: the stopped/killed status marker and the signal table (FreeBSD 11.4 numbering) are now right, and the earlier points are met.

One point left:

  • core/libs/prx/libc/src/Process.cpp:352-353 (Windows WaitChildProcess_nid_no_patch): if another thread reaps the child this call is waiting for, the loop never returns. With no children left, continue spins forever; with other children left, it waits on them instead of failing. POSIX returns ECHILD in that case. Build handles only from the children matching processId, and return -2 when none is left.

Please also add a test that two waiters on one child don't both block (or a note in the debt if it can't be tested on Windows).

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5638642070 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9133#pullrequestreview-5638642070). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-24T14:44:27Z. **Review:** CHANGES_REQUESTED --- Re-checked on 21b467c6c9: the stopped/killed status marker and the signal table (FreeBSD 11.4 numbering) are now right, and the earlier points are met. One point left: - `core/libs/prx/libc/src/Process.cpp:352-353` (Windows `WaitChildProcess_nid_no_patch`): if another thread reaps the child this call is waiting for, the loop never returns. With no children left, `continue` spins forever; with other children left, it waits on them instead of failing. POSIX returns `ECHILD` in that case. Build `handles` only from the children matching `processId`, and return -2 when none is left. Please also add a test that two waiters on one child don't both block (or a note in the debt if it can't be tested on Windows).
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!9133
No description provided.