libc: calling exit() from an atexit handler throws and crashes the process instead of running the remaining handlers; fix branch ready #5277

Closed
opened 2026-10-13 16:18:34 +00:00 by husseinkad · 1 comment
husseinkad commented 2026-10-13 16:18:34 +00:00 (Migrated from github.com)
Contributor

Imported from boykopovar/AnyPS5. Originally posted by husseinkad on 2026-10-13T16:18:34Z.


If a guest atexit or __cxa_atexit handler calls exit(), the nested call throws std::runtime_error ("exit thread cannot wait for itself") and the process crashes. FreeBSD 11.4 exit() runs __cxa_finalize(NULL), then the stdio flush, then _exit(status); __cxa_finalize marks each handler as called and drops its lock before it runs the handler, so a nested exit() runs the remaining handlers, flushes stdio once, and its status replaces the original. A guest whose cleanup code ends with exit(code) (a common pattern in error paths) crashes here instead.

Fix branch: https://github.com/husseinkad/AnyPS5/tree/fix/libc-exit-nested (one commit on a recent main: libc/src/Process.cpp, tests/GuestSystemExit.cpp, and a guest_nested_exit ctest entry in the libSceSystemService CMakeLists.txt). LibcExit_nid_no_patch marks the exiting thread with a thread_local flag; a nested exit on that thread runs the remaining handlers and calls std::exit(code), while an exit from another thread still waits as before. The test is a new driver mode (--nested-exit-driver): the child registers a handler that records a marker and another that calls exit(7), and the driver asserts exit code 7 and that the marker was written.

Checks:

  • The new guest_nested_exit fails on main ("exit thread cannot wait for itself", exit code 0xc0000409) and passes with the change; guest_system_relaunch, guest_system_exit and the other guest exit tests pass.
  • A probe with child processes and a 12 s timeout: exit(7) from a handler, exit chains up to 200 deep (the deepest nested exit wins), atexit handlers calling exit, exit from a worker thread, LibcRequestExit, return from main, a handler registering new handlers, a throw followed by another exit, _Exit from a handler, and exit racing another thread (25 to 60 repetitions each). Every scenario gave the expected exit code with each handler's output exactly once, to a file and to a pipe; main fails nearly all of them (0xc0000005).
  • Two independent reviews found nothing wrong. check_conventions.py is clean and pr_overlap.py finds no overlap (#5235 is about sceSystemServiceLoadExec, a different path).

Not covered, and the same as main: a handler that starts a thread that calls exit and joins it hangs; handlers do not run when the host calls std::exit directly. One minor new difference: exit(7) from a host shutdown callback (not an atexit handler) skips the remaining shutdown callbacks and runs the atexit handlers early, where main threw. In the test, the Windows child wait is INFINITE; a hung child would be killed by the ctest timeout but left orphaned. Only Windows was built and run.

AI-assisted: yes

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5833818632 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/5277). Originally posted by [husseinkad](https://github.com/husseinkad) on 2026-10-13T16:18:34Z. --- If a guest `atexit` or `__cxa_atexit` handler calls `exit()`, the nested call throws `std::runtime_error` ("exit thread cannot wait for itself") and the process crashes. FreeBSD 11.4 `exit()` runs `__cxa_finalize(NULL)`, then the stdio flush, then `_exit(status)`; `__cxa_finalize` marks each handler as called and drops its lock before it runs the handler, so a nested `exit()` runs the remaining handlers, flushes stdio once, and its status replaces the original. A guest whose cleanup code ends with `exit(code)` (a common pattern in error paths) crashes here instead. Fix branch: https://github.com/husseinkad/AnyPS5/tree/fix/libc-exit-nested (one commit on a recent main: `libc/src/Process.cpp`, `tests/GuestSystemExit.cpp`, and a `guest_nested_exit` ctest entry in the `libSceSystemService` `CMakeLists.txt`). `LibcExit_nid_no_patch` marks the exiting thread with a `thread_local` flag; a nested `exit` on that thread runs the remaining handlers and calls `std::exit(code)`, while an `exit` from another thread still waits as before. The test is a new driver mode (`--nested-exit-driver`): the child registers a handler that records a marker and another that calls `exit(7)`, and the driver asserts exit code 7 and that the marker was written. Checks: - The new `guest_nested_exit` fails on main ("exit thread cannot wait for itself", exit code 0xc0000409) and passes with the change; `guest_system_relaunch`, `guest_system_exit` and the other guest exit tests pass. - A probe with child processes and a 12 s timeout: `exit(7)` from a handler, exit chains up to 200 deep (the deepest nested exit wins), `atexit` handlers calling `exit`, exit from a worker thread, `LibcRequestExit`, return from `main`, a handler registering new handlers, a throw followed by another exit, `_Exit` from a handler, and exit racing another thread (25 to 60 repetitions each). Every scenario gave the expected exit code with each handler's output exactly once, to a file and to a pipe; main fails nearly all of them (0xc0000005). - Two independent reviews found nothing wrong. `check_conventions.py` is clean and `pr_overlap.py` finds no overlap (#5235 is about `sceSystemServiceLoadExec`, a different path). Not covered, and the same as main: a handler that starts a thread that calls `exit` and joins it hangs; handlers do not run when the host calls `std::exit` directly. One minor new difference: `exit(7)` from a host shutdown callback (not an `atexit` handler) skips the remaining shutdown callbacks and runs the `atexit` handlers early, where main threw. In the test, the Windows child wait is `INFINITE`; a hung child would be killed by the ctest timeout but left orphaned. Only Windows was built and run. AI-assisted: yes
MarcZX10 commented 2026-10-13 16:29:52 +00:00 (Migrated from github.com)
Author
Contributor

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-13T16:29:52Z.


Carried the implementation into PR #5284: https://github.com/boykopovar/AnyPS5/pull/5284. On Linux, guest_nested_exit, guest_system_exit and guest_system_relaunch pass (3/3), and the conventions check passes. The reported Windows failure was not rerun locally.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6150253504 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/5277#issuecomment-6150253504). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-13T16:29:52Z. --- Carried the implementation into PR #5284: https://github.com/boykopovar/AnyPS5/pull/5284. On Linux, `guest_nested_exit`, `guest_system_exit` and `guest_system_relaunch` pass (3/3), and the conventions check passes. The reported Windows failure was not rerun locally.
Sign in to join this conversation.
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#5277
No description provided.