fix(libSceSystemService): end LoadExec exit without running guest exit handlers #5236

Merged
David-cmrg merged 1 commit from upstream-pr/5236 into main 2026-10-14 20:54:41 +00:00
David-cmrg commented 2026-10-13 15:22:52 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by David-cmrg on 2026-10-13T15:22:52Z.


What

Fixes #5235.

#4872 made sceSystemServiceLoadExec("exit") run the title's __cxa_atexit handlers before the host's, which fixed #4846. This change stops running the title's handlers on "exit" at all, because the console doesn't run them: the shell closes the title asynchronously, and titles are written for that. PS5_SwitchWaker sleeps forever after the call (catchReturnFromMain), and VLC for PS5 calls _exit(0) right after it (plat_quit). Wind Waker (PPSA99309) segfaulted in one of its own destructors at this point.

"exit" now calls a new libc helper, LibcExitWithoutHandlers_nid_no_patch, next to LibcExit_nid_no_patch. Both first mark the exit as requested and started and run LibcRunShutdown, or park if another exit is already running; that part is now shared. The new helper then flushes the host streams and calls LibcTerminate, without CxaFinalize or std::exit. Marking the exit first keeps the title's other threads as they are on exit(): a thread that gets ProcessShutdown during the shutdown, for example in AgcDriver::Submit, parks in LibcAwaitExit_nid_postfix, and a thread that calls exit() meanwhile waits instead of running the title's handlers. #4846 stays fixed, since no guest code, and so no guest free, runs during the exit.

guest_system_exit registers a guest __cxa_atexit handler and a host atexit handler that both print "Guest exit handler ran" and fail. Its shutdown callback also starts a thread that calls LibcAwaitExit_nid_postfix; the test fails with "Guest thread resumed during exit" if that call throws ProcessShutdown instead of parking the thread. The test passes on "Guest shutdown completed", printed by the shutdown callback. The technical debt entry for sceSystemServiceLoadExec describes the "exit" ending. It also notes that a line a title forwards through its own thread just before may be lost: Wind Waker's last line, "exit status 14: asking the shell to close the title", was missing in 1 of 3 runs with the change.

#5083 (open) changes the executable-launch branch of the same function and lists this PR in its merge order. This PR leaves that branch alone and only changes "exit".

Tested

Linux, Fedora 44, GCC 16.2.1, Release, main a3aeaf884 with and without this change:

  • With the change, 17 exit tests pass in 4 of 4 runs: the seven guest_system_exit_tests modes (guest_system_exit, guest_system_exit_parameters, guest_quick_exit, guest_immediate_exit, guest_return_from_main, guest_system_relaunch, guest_nested_exit), guest_system_kill_app, guest_system_kill_app_status, guest_system_kill_app_parameters, guest_system_service, guest_system_service_language, guest_exit_order, application_heap_exit_order, guest_thread_atexit, guest_user_context and guest_user_context_exit.
  • Negative controls for guest_system_exit: with the first version of this PR (LibcRunShutdown, a flush and LibcTerminate, without marking the exit), it fails in 3 of 3 runs with "Guest thread resumed during exit"; with main's Export.cpp, it fails in 3 of 3 with "Guest exit handler ran".
  • Wind Waker (PPSA99309, catalog 1.0.1, no disc image), llvmpipe: on main it ends with a segmentation fault (exit code 139) in 3 of 3 runs; with the change it exits with code 0 in 3 of 3.
  • Earlier, on main 9510e71d6 with the first version of this change: ProsperoEden (PPSA99008, the title from #4846), which also ends with sceSystemServiceLoadExec("exit"), exited with code 0 in 7 of 10 runs both on main and with the change; in the other 3 on each side it segfaulted before calling sceSystemServiceLoadExec, in the AGC driver thread. From an empty shader cache, its first run wrote 7 shader cache entries (68 KiB) and its second run wrote none, both on main and with the change.
  • python3 tools/check_conventions.py --base origin/main passes, and git diff --check is clean.

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:4845494202 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/5236). Originally posted by [David-cmrg](https://github.com/David-cmrg) on 2026-10-13T15:22:52Z. --- ### What Fixes #5235. #4872 made `sceSystemServiceLoadExec("exit")` run the title's `__cxa_atexit` handlers before the host's, which fixed #4846. This change stops running the title's handlers on `"exit"` at all, because the console doesn't run them: the shell closes the title asynchronously, and titles are written for that. PS5_SwitchWaker sleeps forever after the call ([`catchReturnFromMain`](https://github.com/griches/PS5_SwitchWaker/blob/3c0f213f2d4c17606ac8637eda2c5da6cbcc73b0/ps5/src/platform.c#L77-L89)), and VLC for PS5 calls `_exit(0)` right after it ([`plat_quit`](https://github.com/ZiZc3/VLC/blob/d46be6a68f4244466b041c2dc1b640915ac1a983/src/platform_ps5.c#L555-L560)). Wind Waker (PPSA99309) segfaulted in one of its own destructors at this point. `"exit"` now calls a new libc helper, `LibcExitWithoutHandlers_nid_no_patch`, next to `LibcExit_nid_no_patch`. Both first mark the exit as requested and started and run `LibcRunShutdown`, or park if another exit is already running; that part is now shared. The new helper then flushes the host streams and calls `LibcTerminate`, without `CxaFinalize` or `std::exit`. Marking the exit first keeps the title's other threads as they are on `exit()`: a thread that gets `ProcessShutdown` during the shutdown, for example in `AgcDriver::Submit`, parks in `LibcAwaitExit_nid_postfix`, and a thread that calls `exit()` meanwhile waits instead of running the title's handlers. #4846 stays fixed, since no guest code, and so no guest free, runs during the exit. `guest_system_exit` registers a guest `__cxa_atexit` handler and a host `atexit` handler that both print "Guest exit handler ran" and fail. Its shutdown callback also starts a thread that calls `LibcAwaitExit_nid_postfix`; the test fails with "Guest thread resumed during exit" if that call throws `ProcessShutdown` instead of parking the thread. The test passes on "Guest shutdown completed", printed by the shutdown callback. The technical debt entry for `sceSystemServiceLoadExec` describes the `"exit"` ending. It also notes that a line a title forwards through its own thread just before may be lost: Wind Waker's last line, "exit status 14: asking the shell to close the title", was missing in 1 of 3 runs with the change. #5083 (open) changes the executable-launch branch of the same function and lists this PR in its merge order. This PR leaves that branch alone and only changes `"exit"`. ### Tested Linux, Fedora 44, GCC 16.2.1, Release, `main` `a3aeaf884` with and without this change: - With the change, 17 exit tests pass in 4 of 4 runs: the seven `guest_system_exit_tests` modes (`guest_system_exit`, `guest_system_exit_parameters`, `guest_quick_exit`, `guest_immediate_exit`, `guest_return_from_main`, `guest_system_relaunch`, `guest_nested_exit`), `guest_system_kill_app`, `guest_system_kill_app_status`, `guest_system_kill_app_parameters`, `guest_system_service`, `guest_system_service_language`, `guest_exit_order`, `application_heap_exit_order`, `guest_thread_atexit`, `guest_user_context` and `guest_user_context_exit`. - Negative controls for `guest_system_exit`: with the first version of this PR (`LibcRunShutdown`, a flush and `LibcTerminate`, without marking the exit), it fails in 3 of 3 runs with "Guest thread resumed during exit"; with `main`'s `Export.cpp`, it fails in 3 of 3 with "Guest exit handler ran". - Wind Waker (PPSA99309, catalog 1.0.1, no disc image), llvmpipe: on `main` it ends with a segmentation fault (exit code 139) in 3 of 3 runs; with the change it exits with code 0 in 3 of 3. - Earlier, on `main` `9510e71d6` with the first version of this change: ProsperoEden (PPSA99008, the title from #4846), which also ends with `sceSystemServiceLoadExec("exit")`, exited with code 0 in 7 of 10 runs both on `main` and with the change; in the other 3 on each side it segfaulted before calling `sceSystemServiceLoadExec`, in the AGC driver thread. From an empty shader cache, its first run wrote 7 shader cache entries (68 KiB) and its second run wrote none, both on `main` and with the change. - `python3 tools/check_conventions.py --base origin/main` passes, and `git diff --check` is clean. ### 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)
AlexOfTheForest commented 2026-10-14 05:58:48 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by AlexOfTheForest on 2026-10-14T05:58:48Z.


The "exit" path no longer goes through LibcExit_nid_no_patch, so exitRequested and exitStarted (core/libs/prx/libc/src/Process.cpp) stay false while LibcRunShutdown_nid_postfix runs.

LibcRunShutdown_nid_postfix stops the shutdown token. A guest thread that is inside AgcDriver::Submit (or the other entry points in Execution/src/Driver/EntryPoints.cpp, or libSceVideoOut/src/Event.cpp) at that moment gets ProcessShutdown and calls LibcAwaitExit_nid_postfix. With exitRequested == false, that throws ProcessShutdown{} again instead of parking the thread, so the exception goes back into guest code during shutdown. On main, LibcExit_nid_no_patch sets exitRequested first, so these threads park.

Same if another guest thread calls exit() meanwhile: exitStarted is still false, so it ends up in CxaFinalize_nid_no_patch and runs the title's handlers that this PR wants to skip, racing LibcTerminate_nid_no_patch.

A small libc helper next to LibcExit_nid_no_patch that sets exitRequested/exitStarted, then shuts down, flushes and terminates without CxaFinalize would keep both cases as on main. The relaunch path has the same pattern, but that can be a separate PR.

Removing VerifyExit loses no coverage: normal exit() handlers are still tested by guest_exit_order and guest_system_kill_app.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6161966791 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/5236#issuecomment-6161966791). Originally posted by [AlexOfTheForest](https://github.com/AlexOfTheForest) on 2026-10-14T05:58:48Z. --- The "exit" path no longer goes through LibcExit_nid_no_patch, so exitRequested and exitStarted (core/libs/prx/libc/src/Process.cpp) stay false while LibcRunShutdown_nid_postfix runs. LibcRunShutdown_nid_postfix stops the shutdown token. A guest thread that is inside AgcDriver::Submit (or the other entry points in Execution/src/Driver/EntryPoints.cpp, or libSceVideoOut/src/Event.cpp) at that moment gets ProcessShutdown and calls LibcAwaitExit_nid_postfix. With exitRequested == false, that throws ProcessShutdown{} again instead of parking the thread, so the exception goes back into guest code during shutdown. On main, LibcExit_nid_no_patch sets exitRequested first, so these threads park. Same if another guest thread calls exit() meanwhile: exitStarted is still false, so it ends up in CxaFinalize_nid_no_patch and runs the title's handlers that this PR wants to skip, racing LibcTerminate_nid_no_patch. A small libc helper next to LibcExit_nid_no_patch that sets exitRequested/exitStarted, then shuts down, flushes and terminates without CxaFinalize would keep both cases as on main. The relaunch path has the same pattern, but that can be a separate PR. Removing VerifyExit loses no coverage: normal exit() handlers are still tested by guest_exit_order and guest_system_kill_app.
David-cmrg commented 2026-10-14 07:52:06 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by David-cmrg on 2026-10-14T07:52:06Z.


Updated to b3ef07ba8, rebased onto main a3aeaf884:

  • "exit" now calls a new LibcExitWithoutHandlers_nid_no_patch next to LibcExit_nid_no_patch. Both start with the same shared part, which sets exitRequested and exitStarted and runs the shutdown, or parks if another exit is already running. The new helper then flushes and calls LibcTerminate instead of CxaFinalize and std::exit; LibcExit_nid_no_patch behaves as before.
  • guest_system_exit's shutdown callback now also starts a thread that calls LibcAwaitExit_nid_postfix, and the test fails if that thread gets ProcessShutdown back. It fails in 3 of 3 runs with the previous head's Export.cpp and passes with the helper.
  • A thread that calls exit() during the shutdown has no test, since that race can't fail reliably. The same change covers it: exitStarted is set before the shutdown starts, so such a thread parks in LibcAwaitExit_nid_postfix.
  • The relaunch path is unchanged, as you suggested.

On the new base, 17 exit tests pass in 4 of 4 runs, including guest_exit_order, guest_system_kill_app and guest_user_context_exit, and Wind Waker still exits with code 0 in 3 of 3 runs (139 on main). The description has the details. Thanks for checking that removing VerifyExit loses no coverage.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6163606643 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/5236#issuecomment-6163606643). Originally posted by [David-cmrg](https://github.com/David-cmrg) on 2026-10-14T07:52:06Z. --- Updated to `b3ef07ba8`, rebased onto `main` `a3aeaf884`: - `"exit"` now calls a new `LibcExitWithoutHandlers_nid_no_patch` next to `LibcExit_nid_no_patch`. Both start with the same shared part, which sets `exitRequested` and `exitStarted` and runs the shutdown, or parks if another exit is already running. The new helper then flushes and calls `LibcTerminate` instead of `CxaFinalize` and `std::exit`; `LibcExit_nid_no_patch` behaves as before. - `guest_system_exit`'s shutdown callback now also starts a thread that calls `LibcAwaitExit_nid_postfix`, and the test fails if that thread gets `ProcessShutdown` back. It fails in 3 of 3 runs with the previous head's `Export.cpp` and passes with the helper. - A thread that calls `exit()` during the shutdown has no test, since that race can't fail reliably. The same change covers it: `exitStarted` is set before the shutdown starts, so such a thread parks in `LibcAwaitExit_nid_postfix`. - The relaunch path is unchanged, as you suggested. On the new base, 17 exit tests pass in 4 of 4 runs, including `guest_exit_order`, `guest_system_kill_app` and `guest_user_context_exit`, and Wind Waker still exits with code 0 in 3 of 3 runs (139 on `main`). The description has the details. Thanks for checking that removing `VerifyExit` loses no coverage.
babyhuey commented 2026-10-14 12:35:45 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by babyhuey on 2026-10-14T12:35:45Z.


Windows run, the platform where Wind Waker's exit shows up as an access violation. Windows 11 Pro 25H2, RTX 3080 (NVIDIA 591.86), WinLibs GCC 15.2.0, Ninja Release. Head b3ef07ba8 merged onto main 2aad30682 with no conflicts (Export.cpp auto-merged).

  • Tests: the seven tests in guest_system_exit_tests (guest_system_exit, parameters, quick_exit, immediate, return_from_main, relaunch, nested_exit) pass 5 of 5 each, on main and on the merge. The merge build has 0 warnings.
  • Wind Waker on main: PPSA99309, catalog release, relinked with --windows, no disc image, 3 runs. All 3 end with FATAL: unhandled exception 0xc0000005 at app.exe+0xddabd2, reading 0x98 after exit status 14: asking the shell to close the title. A Linux gdb run of the same title shows the cause: LoadExec("exit") reaches CxaFinalize, and one of the title's static destructors loads 0x98 through a null pointer.
  • Wind Waker with this PR: all 3 runs exit with code 0 after about 2.2 s. The exit status 14 line is missing in 1 of the 3, as the debt entry describes.

AI-assisted: yes

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6168476411 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/5236#issuecomment-6168476411). Originally posted by [babyhuey](https://github.com/babyhuey) on 2026-10-14T12:35:45Z. --- Windows run, the platform where Wind Waker's exit shows up as an access violation. Windows 11 Pro 25H2, RTX 3080 (NVIDIA 591.86), WinLibs GCC 15.2.0, Ninja Release. Head `b3ef07ba8` merged onto `main` `2aad30682` with no conflicts (`Export.cpp` auto-merged). - **Tests:** the seven tests in `guest_system_exit_tests` (`guest_system_exit`, `parameters`, `quick_exit`, `immediate`, `return_from_main`, `relaunch`, `nested_exit`) pass 5 of 5 each, on `main` and on the merge. The merge build has 0 warnings. - **Wind Waker on `main`:** PPSA99309, catalog release, relinked with `--windows`, no disc image, 3 runs. All 3 end with `FATAL: unhandled exception 0xc0000005` at `app.exe+0xddabd2`, reading `0x98` after `exit status 14: asking the shell to close the title`. A Linux gdb run of the same title shows the cause: `LoadExec("exit")` reaches `CxaFinalize`, and one of the title's static destructors loads `0x98` through a null pointer. - **Wind Waker with this PR:** all 3 runs exit with code 0 after about 2.2 s. The `exit status 14` line is missing in 1 of the 3, as the debt entry describes. AI-assisted: yes
adriadam10 commented 2026-10-14 18:10:24 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-14T18:10:24Z.


Checked on Linux with the PR merged onto current main: build and scoped ctest green (116/116), including the seven guest_system_exit_tests modes. With the non-test changes reverted, guest_system_exit fails, so the test covers the change (no guest/host exit handler runs, and a guest thread parks in LibcAwaitExit_nid_postfix instead of getting ProcessShutdown). Windows run by @babyhuey also confirms; merging once the full Build is green.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6175085952 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/5236#issuecomment-6175085952). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-14T18:10:24Z. --- Checked on Linux with the PR merged onto current `main`: build and scoped ctest green (116/116), including the seven `guest_system_exit_tests` modes. With the non-test changes reverted, `guest_system_exit` fails, so the test covers the change (no guest/host exit handler runs, and a guest thread parks in `LibcAwaitExit_nid_postfix` instead of getting `ProcessShutdown`). Windows run by @babyhuey also confirms; merging once the full Build is green.
adriadam10 commented 2026-10-14 19:45:10 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-14T19:45:10Z.


Rebased onto current main (only conflict: the sceSystemServiceLoadExec line in docs/dev/debt/libSceSystemService.md after #5083; kept main's text and the "exit" sentence from this PR). Build and scoped ctest green on the rebased head (117/117, including the seven guest_system_exit_tests modes). The code diff is unchanged, so the earlier checks (negative control, Windows run by @babyhuey) still apply; merging once the full Build is green.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6176885624 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/5236#issuecomment-6176885624). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-14T19:45:10Z. --- Rebased onto current `main` (only conflict: the `sceSystemServiceLoadExec` line in `docs/dev/debt/libSceSystemService.md` after #5083; kept main's text and the `"exit"` sentence from this PR). Build and scoped ctest green on the rebased head (117/117, including the seven `guest_system_exit_tests` modes). The code diff is unchanged, so the earlier checks (negative control, Windows run by @babyhuey) still apply; merging once the full Build is green.
adriadam10 commented 2026-10-14 20:41:03 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-14T20:41:03Z.


Rebased onto current main (only conflict: the sceSystemServiceLoadExec line in docs/dev/debt/libSceSystemService.md after #5083; kept main's text and the "exit" sentence from this PR). Build and scoped ctest green on the rebased head (117/117, including the seven guest_system_exit_tests modes). The code diff is unchanged, so the earlier checks (negative control, Windows run by @babyhuey) still apply; the one red in the first full Build was agc_interpolant_mapping timing out on Windows, in code this PR doesn't touch, and the job was rerun. Merging once the full Build is green.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6177982305 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/5236#issuecomment-6177982305). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-14T20:41:03Z. --- Rebased onto current `main` (only conflict: the `sceSystemServiceLoadExec` line in `docs/dev/debt/libSceSystemService.md` after #5083; kept main's text and the `"exit"` sentence from this PR). Build and scoped ctest green on the rebased head (117/117, including the seven `guest_system_exit_tests` modes). The code diff is unchanged, so the earlier checks (negative control, Windows run by @babyhuey) still apply; the one red in the first full Build was `agc_interpolant_mapping` timing out on Windows, in code this PR doesn't touch, and the job was rerun. Merging once the full Build is green.
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!5236
No description provided.