fix(libc): call guest unwind callbacks with the System V ABI on Windows #6851
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/6851"
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 GitamarT045T3R on 2026-10-16T14:16:20Z.
What
On Windows, AnyPS5 called three kinds of guest callbacks with the Microsoft x64 convention instead of System V:
_Unwind_Backtrace,_Unwind_ForcedUnwind,exception_cleanupof an exception deleted by_Unwind_DeleteException, including the foreign exceptions that__cxa_end_catchdeletes.Their types in
UnwindAbi.hpp(_Unwind_Trace_Fn,_Unwind_Stop_Fnand_Unwind_Exception::exception_cleanup) had noAPS5_VABI. Sotrace(&context, argument)inUnwind.cppput the context and the argument inrcxandrdx, while the guest function readsrdiandrsi; the stop function and the cleanup got wrong arguments the same way. The relinker passes guest function pointers to libc unchanged, and the other guest callbacks in libc already haveAPS5_VABI, e.g. the constructor and destructor of__cxa_vec_ctor_nid_postfix. vkQuake (PPSA99010) imports_Unwind_Backtraceand reads each frame in its callback (db3ec02c5), so on Windows its callback got a wrong context.Changes:
APS5_VABI.Cleanup, theexception_cleanupof the exceptions libc throws, has it too. With the new types the compiler rejects any other host function stored in them on Windows, andCleanupwas the only one._Unwind_DeleteExceptionalso deletes exceptions thrown insidelibstdc++-6.dllwhen code that takes__cxa_begin_catchand__cxa_end_catchfromlibc.prxcatches them. Their class isGNUCC++and their cleanup is a host function, so_Unwind_DeleteException_nid_postfixpasses them to libgcc's_Unwind_DeleteException, which calls the cleanup as before. With every cleanup called as System V, a small program built with SEH unwind tables that catchesstd::stoi("x")crashed in__gxx_exception_cleanup; with this change it catches the exception 3 of 3 times, as onmain. The AnyPS5 modules themselves are built with-fno-asynchronous-unwind-tables, so they cannot catch such an exception, onmaineither:windows_exception_testsreports the samestd::stoiexception as undeliverable.-O3inlines_Unwind_DeleteException_nid_postfixinto__cxa_end_catch_nid_postfixand merges the two indirect calls, which differ only in their calling convention, into the System V one (-fno-tree-tail-mergekeeps both). The call to libgcc is a direct call, so it cannot be merged.APS5_VABIis empty there.New test
core/libs/tests/UnwindCallbackAbi.cpp, registered on every platform as five ctest tests. Each passes callbacks declaredAPS5_VABIlike a guest does:unwind_callback_abi_backtrace: the trace function gets the argument given to_Unwind_Backtraceand a context whose region start is the function that called_Unwind_Backtrace.unwind_callback_abi_forced_unwind: the stop function gets version 1,_UA_CLEANUP_PHASE | _UA_FORCE_UNWIND, the exception class, the exception, the argument given to_Unwind_ForcedUnwindand the context of its caller.unwind_callback_abi_delete_exception:_Unwind_DeleteExceptioncallsexception_cleanuponce with_URC_FOREIGN_EXCEPTION_CAUGHTand the exception.unwind_callback_abi_end_catch: the same after__cxa_begin_catchand__cxa_end_catchon a foreign exception.unwind_callback_abi_end_catch_gnu: the same with aGNUCC++exception whose cleanup is a host function.ExceptionRuntime.cppstill passes host lambdas as these types. It is built only on Linux and macOS, where the types are unchanged.My open PR #6819 adds an
unwind_cursortest whoseTraceFirstFrameis a host function passed to_Unwind_Backtrace_nid_postfix. The two branches merge without conflicts, but once both are in, that test no longer compiles on Windows untilTraceFirstFrameis markedAPS5_VABI. I'll add that to whichever of the two is merged second.Tested
Windows 10 Pro 22H2 (19045), MinGW-w64 GCC 15.2.0 (WinLibs r7), Ninja, Release,
BUILD_TESTING=ON, onmain82ddae75b. The new test binary was run 20 times per test against three builds oflibc.prx:mainbacktracethe trace callback gets the argument given to _Unwind_Backtraceforced_unwindthe stop function gets version 1delete_exceptionexception_cleanup gets _URC_FOREIGN_EXCEPTION_CAUGHTend_catchexception_cleanup gets _URC_FOREIGN_EXCEPTION_CAUGHTend_catch_gnuexception_cleanup gets _URC_FOREIGN_EXCEPTION_CAUGHTctest -R "exception|unwind|backtrace|personality|terminate|signal"pass, includingwindows_exceptions,windows_backtrace_*,guest_exception_*,guest_raise_exception,guest_unwind_module_info,guest_mutex_raise_exception,unwind_record_bounds,undeliverable_host_exceptionandguest_signal*.ctest -j 12: 1324 of 1326 pass.agc_shader_disk_cachefails withAccess is deniedas onmainon Windows 10 (#6415), andagc_driver_gpu_indirect_arguments_misaligned_writer_ahead_testsdoes not build with MinGW onmain(alignment 65536 above the object file maximum of 8192, #6452).python tools/check_conventions.py --base origin/mainpasses.Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file.mdfiles or images added to the repository (attach them to this PR)Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-16T14:17:06Z.
Progress report (
82ddae7→3168727)➖ System libraries: 88.83% (+0.0%, +1 implemented functions)
✅ 1 implemented
CleanupImported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-16T14:40:04Z.
Some comparisons could not be completed because a pull request conflicts with the base branch. Review these shared files manually before concluding that there is no overlap.
core/libs/prx/libc/CMakeLists.txtcore/libs/prx/libc/CMakeLists.txtcore/libs/prx/libc/CMakeLists.txtcore/libs/prx/libc/CMakeLists.txtcore/libs/prx/libc/CMakeLists.txtcore/libs/prx/libc/CMakeLists.txtcore/libs/prx/libc/src/exception/Runtime.cppcore/libs/prx/libc/src/exception/Unwind.cppcore/libs/prx/libc/CMakeLists.txtcore/libs/prx/libc/src/exception/Unwind.cppcore/libs/prx/libc/CMakeLists.txtcore/libs/prx/libc/CMakeLists.txtcore/libs/prx/libc/CMakeLists.txtImported from boykopovar/AnyPS5. Originally posted by DavidSamir on 2026-10-16T19:46:44Z.
Title run on Windows with vkQuake (PPSA99010, catalog release 1.0) and the free Quake shareware data, so the crash this PR describes can be reproduced without anyone's own game files.
Windows 11 Pro build 26300, Intel i7-11700F, NVIDIA GeForce RTX 3070, WinLibs GCC 15.2, Release, relinked with
--windows. PR head merged ontomainc842491865(mergebf79bc0920, no conflicts). Data: the sharewareid1/pak0.pakfrom id Software'squake106.zip(18,689,235 bytes, MD55906e5998fc3d896ddaf5e6a62e03abb), placed at/app0/id1/pak0.pak(the title'sBasedir is: /app0). Anid1/autoexec.cfgwith three lines makes the title reach its multiplayer search without any controller input:(
r_rtshadows 0keeps the ray-traced shadow compute shader, which takes tens of seconds to build here, out of the way: #4977.)mainc842491865FATAL: unhandled exception 0xc0000005,rip app.exe+0x7a5ed3, read of 0x0mainc842491865+ this PRNo Quake servers found., a 10-frameSTACK TRACE,Host_Error: CL_Connect: connect failed(expected: nothing listens on 127.0.0.1) and goes back to its demo loopWhat the crash is, from the disassembly of the relinked
app.exeand the traced library calls (--trace-imports,APS5_TRACE_CALLS=all):mainstops in the title's own stack-trace printer. Its last traced calls are_Unwind_Backtrace,vsnprintf,vprintf(the=== STACK TRACE ===header),pthread_once,strlen, and thenstrtok_ris entered with a NULL string atapp.exe+0x7a5ed3, because the backtrace gave it no frames. With this PR the callback gets its arguments and the same printer lists 10 frames ([no symbols], as the title has none).Controller-only runs (the fixed pad sequence, 150 s): on
main82ddae75b6the same crash at the same address in 4 of 4 runs (it takes 85 to 100 s to reach the multiplayer search); onmainc842491865neither of 2 runs reached the search screen (no crash, no stack trace), and with this PR both of 2 runs reached it and neither crashed. The menu timing depends on frame pacing (the title runs at about 6 fps here), so only theautoexec.cfgtable above is a like-for-like comparison.AI-assisted: yes
Imported from boykopovar/AnyPS5. Originally posted by GitamarT045T3R on 2026-10-17T12:24:15Z.
@DavidSamir thank you, that is the reproduction this PR needed: a public title with free data, where
maincrashes in the title's own stack-trace printer 3 of 3 times and this PR runs the full minute 3 of 3 with a 10-frame backtrace. I've set the checklist toUnblocks: PPSA99010; there is no Game bring-up discussion for vkQuake yet.Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-19T12:10:33Z.
Checked the diff against the description: the three callback types and
CleanupgetAPS5_VABI,GNUCC++exceptions go through libgcc's_Unwind_DeleteExceptionon Windows, and the five new tests are registered in ctest and pin the arguments each callback receives. The vkQuake reproduction by @DavidSamir (Windows, PPSA99010) covers the title side. Merged.