fix(libc): run guest exit handlers before the host exit handlers #1658
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/1658"
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 morten-holm on 2026-10-08T15:54:21Z.
What
LibcExit_nid_no_patchcalledstd::exitafter the shutdown callbacks.std::exitruns the static destructors of the host libraries (singletons that hold astd::mutex) before the guest exit handlers, because the guest handlers are run by anatexitrunner that is registered at the first guest__cxa_atexit. A guest handler that freed memory then locked a destroyedstd::mutex. On macOSstd::mutex::lockthen throwsstd::system_error("Invalid argument"), and thenoexceptdestructor of the guest ended instd::terminate:terminate called without an active exception, exit code 134, on a window close and on SIGTERM.LibcExit_nid_no_patchnow callsCxaFinalize_nid_no_patch(nullptr)beforestd::exit. The guest handlers run first, while the library objects are alive.CxaFinalizeremoves each entry as it runs it, so the lateratexitrunner finds the list empty and nothing runs twice. A guest handler that callsexitagain behaves as before (LibcExitalready guards that).Tested
guest_exit_order(UNIX only): a forked child registers a guest handler with__cxa_atexit, then a hoststd::atexithandler, then callsLibcExit; the parent reads the order through a pipe. It fails without the change (host handler first) and passes with it, 3 of 3 runs.maindoes not build libc on macOS yet.std::exitto just before it; I did not check other titles or platforms.Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed in TechnicalDebt.mdfiles or images added to the repository (attach them to this PR)🤖 Generated with Claude Code
Imported from boykopovar/AnyPS5. Originally posted by morten-holm on 2026-10-08T16:27:19Z.
Additional result on mugurc's
macos-portbranch (4dc40455, plus local changes needed to run the title: fixed-base load address, registry and maxprot for the no-access segment, conditionalVK_KHR_portability_enumeration, and the one-line change from this PR).Setup: macOS 27, Apple M2, x86-64 under Rosetta, MoltenVK 1.4.2, one retail title (not named). The title runs for 120 s without a crash.
Window close:
libc++abi: terminating due to uncaught exception of type std::__1::system_error: mutex lock failed: Invalid argument.This is one run on macOS. SIGTERM, Linux and Windows are not tested on this tree; the unit test (
guest_exit_order) covers the order on UNIX.Imported from boykopovar/AnyPS5. Originally posted by hikazey on 2026-10-08T18:01:11Z.
Linux and Windows runs, since the description lists both as untested. This commit merged onto
main17350cf5, which merges cleanly.Linux (Ubuntu 24.04 container, GCC 14.2, Release, Ryzen 9 5900X host):
guest_exit_orderCxaFinalize_nid_no_patch(nullptr)removed, test keptguest exit handlers must run before the host exit handlers)import_audithit my 120 s ctest timeout in the full run; alone it passes in 236 s, which is slow I/O through the container's bind mount, not this changeWindows (Windows 11 Pro 26200, MinGW-w64 GCC 15.2.0 / WinLibs r7, Release, configured like CI).
guest_exit_orderisUNIXonly because offork, so I checked the order with a small local test withoutfork. It registers a guest handler with__cxa_atexit, then a hoststd::atexithandler, callsLibcExit_nid_no_patch(0), and the handlers writeG/Hto the stdout handle:GH, 5/5CxaFinalize_nid_no_patch(nullptr)removedHG, 5/5So the reorder takes effect on Windows as well. If you want Windows coverage in the PR, this is the test I used (registered under
if(WIN32)next to the other libc guest tests, withPASS_REGULAR_EXPRESSION "GH",FAIL_REGULAR_EXPRESSION "HG"and the usualPATH=path_list_prepend:$<TARGET_FILE_DIR:libc>):AI-assisted: yes (Claude Code).
Imported from boykopovar/AnyPS5. Originally posted by fisherjoey on 2026-10-08T18:16:53Z.
Linux test with a retail title, since the PR hasn't been tested there.
Demon's Souls (PPSA01341), Nobara 44 (Linux 7.2), GCC 16.2.1, Release, RTX 5070 Ti (595.91.07). Our stack is
maindf16c4c2plus #1071, #1201/#1202, #1184 and some local performance commits. This PR was cherry-picked onto it; only the test list inlibc/CMakeLists.txtconflicted, andguest_exit_order_testspasses. The window was closed at the attract screen (t+150 s) withkdotool windowclose, 3 runs per arm, interleaved:AJM: decoding codec 4026830656 is not implemented,AJM: decoding codec 27 is not implemented; the SIGSEGV is a guest job-worker fiber jumping to address 0APR: cannot open <garbage path>,double free or corruption (out)None of the six runs exited cleanly. Before this test, every window close on our stack since #1071 ended in -11 or -6, mostly from level-length runs, with
NCA DecoderManager: sceAjmBatchWait() failed with error code 0x80930004 ... halting nowand then one of a fault on a guest job-worker thread,double free or corruption, orAJM: decoding codec <garbage> is not implemented. With the PR the failures look the same: guest code still reads freed or garbage state during teardown (codec ids, file paths). So on Linux this PR doesn't fix that teardown fault, which must be a different one. I didn't see any new failure caused by the PR either; the one run that ignored the close is new, but with three runs per arm I can't tell it apart from noise.Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-10T04:18:06Z.
Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in Depends on.
main)core/libs/tests/GuestExitOrder.cpp