libc: calling exit() from an atexit handler throws and crashes the process instead of running the remaining handlers; fix branch ready #5277
Loading…
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from boykopovar/AnyPS5. Originally posted by husseinkad on 2026-10-13T16:18:34Z.
If a guest
atexitor__cxa_atexithandler callsexit(), the nested call throwsstd::runtime_error("exit thread cannot wait for itself") and the process crashes. FreeBSD 11.4exit()runs__cxa_finalize(NULL), then the stdio flush, then_exit(status);__cxa_finalizemarks each handler as called and drops its lock before it runs the handler, so a nestedexit()runs the remaining handlers, flushes stdio once, and its status replaces the original. A guest whose cleanup code ends withexit(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 aguest_nested_exitctest entry in thelibSceSystemServiceCMakeLists.txt).LibcExit_nid_no_patchmarks the exiting thread with athread_localflag; a nestedexiton that thread runs the remaining handlers and callsstd::exit(code), while anexitfrom 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 callsexit(7), and the driver asserts exit code 7 and that the marker was written.Checks:
guest_nested_exitfails on main ("exit thread cannot wait for itself", exit code 0xc0000409) and passes with the change;guest_system_relaunch,guest_system_exitand the other guest exit tests pass.exit(7)from a handler, exit chains up to 200 deep (the deepest nested exit wins),atexithandlers callingexit, exit from a worker thread,LibcRequestExit, return frommain, a handler registering new handlers, a throw followed by another exit,_Exitfrom 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).check_conventions.pyis clean andpr_overlap.pyfinds no overlap (#5235 is aboutsceSystemServiceLoadExec, a different path).Not covered, and the same as main: a handler that starts a thread that calls
exitand joins it hangs; handlers do not run when the host callsstd::exitdirectly. One minor new difference:exit(7)from a host shutdown callback (not anatexithandler) skips the remaining shutdown callbacks and runs theatexithandlers early, where main threw. In the test, the Windows child wait isINFINITE; a hung child would be killed by the ctest timeout but left orphaned. Only Windows was built and run.AI-assisted: yes
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_exitandguest_system_relaunchpass (3/3), and the conventions check passes. The reported Windows failure was not rerun locally.