fix(libSceSystemService): run guest exit handlers first on LoadExec exit #4872

Merged
David-cmrg merged 1 commit from upstream-pr/4872 into main 2026-10-13 11:09:39 +00:00
David-cmrg commented 2026-10-13 01:08:48 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by David-cmrg on 2026-10-13T01:08:48Z.


What

Fixes #4846.

sceSystemServiceLoadExec("exit") ran LibcRunShutdown_nid_postfix() and then std::exit(0). The title's own exit handlers (__cxa_atexit, run by CxaFinalize_nid_no_patch from the host atexit entry registered at the first guest registration) then ran after the host exit handlers registered later, among them the destructors of host statics such as the GuestAllocations registry. A guest destructor that frees heap memory then reads the destroyed registry. In ProsperoEden the freed entry reads as pinned, so the free waits APS5_PIN_WAIT_MS and the process aborts.

It now exits through LibcExit_nid_no_patch(0), like exit, a return from main and sceSystemServiceKillApp: shutdown, then the guest exit handlers, then std::exit, and it waits when another thread already started exiting. That is the order guest_exit_order requires ("guest exit handlers must run before the host exit handlers").

guest_system_exit now registers a guest exit handler before its host atexit handler and requires that the guest one ran first.

Tested

Fedora 44, GCC 16.2.1, Release.

  • main 510a2188c: with the test change alone, guest_system_exit fails 3 of 3; with this change it passes 3 of 3. guest_exit_order, guest_system_exit*, guest_quick_exit, guest_immediate_exit, guest_return_from_main, guest_system_kill_app* and guest_system_service* (11 tests) pass.
  • ProsperoEden (PPSA99008 v1.000.110) on llvmpipe, set up as in #4846, on d53960612 (the files this PR touches are unchanged since then), with data already in the run folder. The title quits from its launcher after about 19 s.
    • Without this change: exit code 134 after 82 s.
    • With it: exit code 0 after 19 to 20 s, 2 of 2. That run also carried #4690, which only creates data.
  • Not tested on Windows.

#4786 changes the same function (a relaunch for the title's own eboot.bin) and keeps LibcRunShutdown_nid_postfix(); std::exit(0); there, so the two PRs touch the same lines.

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
  • Unblocks: none
  • AI-assisted: yes (Claude Opus 5.5)
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4836910775 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4872). Originally posted by [David-cmrg](https://github.com/David-cmrg) on 2026-10-13T01:08:48Z. --- ### What Fixes #4846. `sceSystemServiceLoadExec("exit")` ran `LibcRunShutdown_nid_postfix()` and then `std::exit(0)`. The title's own exit handlers (`__cxa_atexit`, run by `CxaFinalize_nid_no_patch` from the host `atexit` entry registered at the first guest registration) then ran after the host exit handlers registered later, among them the destructors of host statics such as the `GuestAllocations` registry. A guest destructor that frees heap memory then reads the destroyed registry. In ProsperoEden the freed entry reads as pinned, so the free waits `APS5_PIN_WAIT_MS` and the process aborts. It now exits through `LibcExit_nid_no_patch(0)`, like `exit`, a return from `main` and `sceSystemServiceKillApp`: shutdown, then the guest exit handlers, then `std::exit`, and it waits when another thread already started exiting. That is the order `guest_exit_order` requires ("guest exit handlers must run before the host exit handlers"). `guest_system_exit` now registers a guest exit handler before its host `atexit` handler and requires that the guest one ran first. ### Tested Fedora 44, GCC 16.2.1, Release. - `main` `510a2188c`: with the test change alone, `guest_system_exit` fails 3 of 3; with this change it passes 3 of 3. `guest_exit_order`, `guest_system_exit*`, `guest_quick_exit`, `guest_immediate_exit`, `guest_return_from_main`, `guest_system_kill_app*` and `guest_system_service*` (11 tests) pass. - ProsperoEden (PPSA99008 v1.000.110) on llvmpipe, set up as in #4846, on `d53960612` (the files this PR touches are unchanged since then), with `data` already in the run folder. The title quits from its launcher after about 19 s. - Without this change: exit code 134 after 82 s. - With it: exit code 0 after 19 to 20 s, 2 of 2. That run also carried #4690, which only creates `data`. - Not tested on Windows. #4786 changes the same function (a relaunch for the title's own `eboot.bin`) and keeps `LibcRunShutdown_nid_postfix(); std::exit(0);` there, so the two PRs touch the same lines. ### 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] Unblocks: none - [x] AI-assisted: yes (Claude Opus 5.5)
oneandonlydean commented 2026-10-13 08:37:55 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-13T08:37:55Z.


Needs a rebase: core/libs/prx/libSceSystemService/Export.cpp conflicts with main now. It gets a full build once it is clean.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6141664805 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4872#issuecomment-6141664805). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-13T08:37:55Z. --- Needs a rebase: `core/libs/prx/libSceSystemService/Export.cpp` conflicts with main now. It gets a full build once it is clean.
David-cmrg commented 2026-10-13 10:05:42 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by David-cmrg on 2026-10-13T10:05:42Z.


Rebased onto 77b9f7270. The conflict was main's new launch code in sceSystemServiceLoadExec. The change now only replaces the "exit" branch's LibcRunShutdown_nid_postfix(); std::exit(0); with LibcExit_nid_no_patch(0). The relaunch and other-executable paths are unchanged.

On Linux (GCC 16.2.1, Release), the six guest_system_* exit tests pass, including guest_system_relaunch. guest_system_exit fails 3/3 with main's Export.cpp and passes 3/3 with the change.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6143158357 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4872#issuecomment-6143158357). Originally posted by [David-cmrg](https://github.com/David-cmrg) on 2026-10-13T10:05:42Z. --- Rebased onto `77b9f7270`. The conflict was main's new launch code in `sceSystemServiceLoadExec`. The change now only replaces the `"exit"` branch's `LibcRunShutdown_nid_postfix(); std::exit(0);` with `LibcExit_nid_no_patch(0)`. The relaunch and other-executable paths are unchanged. On Linux (GCC 16.2.1, Release), the six `guest_system_*` exit tests pass, including `guest_system_relaunch`. `guest_system_exit` fails 3/3 with main's `Export.cpp` and passes 3/3 with the change.
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!4872
No description provided.