feat(libSceSystemService): LoadExec on macOS hosts #6248

Merged
mugurc merged 1 commit from upstream-pr/6248 into main 2026-10-18 20:50:27 +00:00
mugurc commented 2026-10-15 07:07:42 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by mugurc on 2026-10-15T07:07:42Z.


What

sceSystemServiceLoadExec on macOS hosts. It found the running executable through /proc/self/exe and replaced the process with fexecve after close_range, none of which macOS has, so on macOS every call other than "exit" threw. Part of the macOS series announced on #1 (https://github.com/boykopovar/AnyPS5/issues/1#issuecomment-6124344298).

  • The running executable's path comes from _NSGetExecutablePath, resolved with std::filesystem::canonical, as libSceNpEntitlementAccess and libSceFont already do on macOS. The rest of PrepareLaunch is the Linux code.
  • The target must be an x86-64 Mach-O executable (MH_MAGIC_64, CPU_TYPE_X86_64, MH_EXECUTE) that the user may run, as Linux requires an x86-64 ELF.
  • macOS has no fexecve, so the process is replaced by path with posix_spawn and POSIX_SPAWN_SETEXEC. POSIX_SPAWN_CLOEXEC_DEFAULT closes every descriptor except the standard streams, which is what close_range does on Linux. The PID stays and the working directory becomes the executable's, as on Linux.
  • Before the replacement it starts the exit and runs the shutdown callbacks (LibcStartExit_nid_postfix, then LibcRunShutdown_nid_postfix), as the Linux and Windows Replace do since #6237.
  • Descriptor 2 gets the host standard error back before the replacement, as #5699 does on Linux, so the next executable writes to the host's standard error and not to where the title redirected descriptor 2. A launch with no argument list keeps the Linux execv path, which macOS has and which already does this.
  • guest_system_exit finds its own executable the same way on macOS, and guest_system_load_exec_standard_error from #5699 now runs on macOS too. In docs/dev/debt/libSceSystemService.md the sceSystemServiceLoadExec entry describes macOS instead of listing it as a host that throws, and the LoadExec descriptors entry says how macOS closes the descriptors.

Tested

  • macOS 26.5.1, Apple M5 Max, Apple clang 21, x86-64 build under Rosetta 2, main 385ed71f2 with the other macOS branches of the series and the open macOS fixes #6928, #6667, #6668, #6969, #6853, #6836, #6620 and #7524 (the libraries do not link on macOS without the series): guest_system_exit_parameters, guest_system_relaunch and guest_nested_exit, which failed before, pass, and so does guest_system_load_exec_standard_error; the four passed 10 runs in a row; guest_system_exit, guest_requested_exit, guest_quick_exit, guest_immediate_exit, guest_return_from_main, guest_exit_finalizers_before_shutdown, guest_system_service and guest_system_service_language pass. guest_system_relaunch replaces the process twice, into a copy named picker.picker.bin and back to the main executable through APS5_EBOOT, and checks the arguments and shutdown handlers of each step.
  • Linux and Windows: not run. Every change to the library is under __APPLE__ except OwnExecutable, which moves the Linux /proc/self/exe lookup into a function without changing it.

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 (none added)
  • Generic behaviour, not specific to one title
  • Depends on: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4869570017 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/6248). Originally posted by [mugurc](https://github.com/mugurc) on 2026-10-15T07:07:42Z. --- ### What `sceSystemServiceLoadExec` on macOS hosts. It found the running executable through `/proc/self/exe` and replaced the process with `fexecve` after `close_range`, none of which macOS has, so on macOS every call other than `"exit"` threw. Part of the macOS series announced on #1 (https://github.com/boykopovar/AnyPS5/issues/1#issuecomment-6124344298). - The running executable's path comes from `_NSGetExecutablePath`, resolved with `std::filesystem::canonical`, as `libSceNpEntitlementAccess` and `libSceFont` already do on macOS. The rest of `PrepareLaunch` is the Linux code. - The target must be an x86-64 Mach-O executable (`MH_MAGIC_64`, `CPU_TYPE_X86_64`, `MH_EXECUTE`) that the user may run, as Linux requires an x86-64 ELF. - macOS has no `fexecve`, so the process is replaced by path with `posix_spawn` and `POSIX_SPAWN_SETEXEC`. `POSIX_SPAWN_CLOEXEC_DEFAULT` closes every descriptor except the standard streams, which is what `close_range` does on Linux. The PID stays and the working directory becomes the executable's, as on Linux. - Before the replacement it starts the exit and runs the shutdown callbacks (`LibcStartExit_nid_postfix`, then `LibcRunShutdown_nid_postfix`), as the Linux and Windows `Replace` do since #6237. - Descriptor 2 gets the host standard error back before the replacement, as #5699 does on Linux, so the next executable writes to the host's standard error and not to where the title redirected descriptor 2. A launch with no argument list keeps the Linux `execv` path, which macOS has and which already does this. - `guest_system_exit` finds its own executable the same way on macOS, and `guest_system_load_exec_standard_error` from #5699 now runs on macOS too. In `docs/dev/debt/libSceSystemService.md` the `sceSystemServiceLoadExec` entry describes macOS instead of listing it as a host that throws, and the LoadExec descriptors entry says how macOS closes the descriptors. ### Tested - macOS 26.5.1, Apple M5 Max, Apple clang 21, x86-64 build under Rosetta 2, `main` 385ed71f2 with the other macOS branches of the series and the open macOS fixes #6928, #6667, #6668, #6969, #6853, #6836, #6620 and #7524 (the libraries do not link on macOS without the series): `guest_system_exit_parameters`, `guest_system_relaunch` and `guest_nested_exit`, which failed before, pass, and so does `guest_system_load_exec_standard_error`; the four passed 10 runs in a row; `guest_system_exit`, `guest_requested_exit`, `guest_quick_exit`, `guest_immediate_exit`, `guest_return_from_main`, `guest_exit_finalizers_before_shutdown`, `guest_system_service` and `guest_system_service_language` pass. `guest_system_relaunch` replaces the process twice, into a copy named `picker.picker.bin` and back to the main executable through `APS5_EBOOT`, and checks the arguments and shutdown handlers of each step. - Linux and Windows: not run. Every change to the library is under `__APPLE__` except `OwnExecutable`, which moves the Linux `/proc/self/exe` lookup into a function without changing it. ### Checklist - [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 (none added) - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes
github-actions[bot] commented 2026-10-15 07:33:14 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-15T07:33:14Z.


Some comparisons could not be completed because a pull request conflicts with the base branch. Review these shared files manually before concluding that there is no overlap.

Pull request Shared files not compared
PR 6044 core/libs/prx/libSceSystemService/CMakeLists.txt
core/libs/tests/GuestSystemExit.cpp
PR 6447 core/libs/prx/libSceSystemService/Export.cpp
PR 7201 core/libs/prx/libSceSystemService/Export.cpp
core/libs/prx/libSceSystemService/LoadExec.cpp
core/libs/tests/GuestSystemExit.cpp
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6186952827 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/6248#issuecomment-6186952827). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-15T07:33:14Z. --- <!-- pr-overlap --> Some comparisons could not be completed because a pull request conflicts with the base branch. Review these shared files manually before concluding that there is no overlap. | Pull request | Shared files not compared | |---|---| | [PR 6044](https://www.github.com/boykopovar/AnyPS5/pull/6044) | `core/libs/prx/libSceSystemService/CMakeLists.txt`<br>`core/libs/tests/GuestSystemExit.cpp` | | [PR 6447](https://www.github.com/boykopovar/AnyPS5/pull/6447) | `core/libs/prx/libSceSystemService/Export.cpp` | | [PR 7201](https://www.github.com/boykopovar/AnyPS5/pull/7201) | `core/libs/prx/libSceSystemService/Export.cpp`<br>`core/libs/prx/libSceSystemService/LoadExec.cpp`<br>`core/libs/tests/GuestSystemExit.cpp` |
milescoviello commented 2026-10-15 10:54:31 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by milescoviello on 2026-10-15T10:54:31Z.


A heads-up on ordering with #6237. It changes LoadExec's shutdown on Linux and Windows from LibcRunShutdown_nid_postfix() to a new LibcStartExit_nid_postfix() (what exit does), so a guest thread that calls into a stopped library during it waits for the process to be replaced instead of getting ProcessShutdown back in title code. On Linux, MGS4's launcher and VLC's self-restart abort on main from that.

The macOS Replace here also calls LibcRunShutdown_nid_postfix() just before the spawn. If #6237 lands first, that call would want to become LibcStartExit_nid_postfix() too. If this lands first, I'll change it when I rebase #6237. The two PRs also both edit the sceSystemServiceLoadExec line in docs/dev/debt/libSceSystemService.md.

AI-assisted: yes

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6190387775 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/6248#issuecomment-6190387775). Originally posted by [milescoviello](https://github.com/milescoviello) on 2026-10-15T10:54:31Z. --- A heads-up on ordering with #6237. It changes LoadExec's shutdown on Linux and Windows from `LibcRunShutdown_nid_postfix()` to a new `LibcStartExit_nid_postfix()` (what `exit` does), so a guest thread that calls into a stopped library during it waits for the process to be replaced instead of getting `ProcessShutdown` back in title code. On Linux, MGS4's launcher and VLC's self-restart abort on `main` from that. The macOS `Replace` here also calls `LibcRunShutdown_nid_postfix()` just before the spawn. If #6237 lands first, that call would want to become `LibcStartExit_nid_postfix()` too. If this lands first, I'll change it when I rebase #6237. The two PRs also both edit the `sceSystemServiceLoadExec` line in `docs/dev/debt/libSceSystemService.md`. AI-assisted: yes
mugurc commented 2026-10-15 11:42:49 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by mugurc on 2026-10-15T11:42:49Z.


Agreed. If #6237 lands first, I will rebase this and the macOS Replace will call LibcStartExit_nid_postfix(). If this lands first, the change is yours in the rebase of #6237. The two edits of the debt line are in different sentences, so either order resolves it by keeping both.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6191229202 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/6248#issuecomment-6191229202). Originally posted by [mugurc](https://github.com/mugurc) on 2026-10-15T11:42:49Z. --- Agreed. If #6237 lands first, I will rebase this and the macOS `Replace` will call `LibcStartExit_nid_postfix()`. If this lands first, the change is yours in the rebase of #6237. The two edits of the debt line are in different sentences, so either order resolves it by keeping both.
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!6248
No description provided.