fix(libc): deliver exceptions raised inside libstdc++-6.dll on Windows #25935
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/25935"
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 methmoussa on 2026-11-22T22:45:19Z.
What
On Windows, a C++ exception thrown by out-of-line code inside
libstdc++-6.dllnever reached acatchin AnyPS5 code. This coversstd::regexerrors,std::stoi,std::filesystemcalls without anerror_code, andstd::locale. The process aborted withFATAL: C++ exception … cannot be delivered: <project frame> has no unwind data(#25728).Cause. libgcc raises such an exception through Windows SEH. With WinLibs GCC 15, project modules carry DWARF unwind data in
.ehframbut almost no.pdata, so dispatch stops at the first project frame. With MSYS2 GCC 16, project frames do have.pdata, so dispatch reaches thecatch. But the first project cleanup pad on the way calls libc's_Unwind_Resumein the middle of libgcc's unwind and aborts.The fix. It adds a handoff in libc's vectored handler (
src/specifics/windows/RuntimeThrowHandoff.cpp):STATUS_GCC_THROWwhose object has the GCC primary class, walk the runtime frames (KERNELBASE,libgcc_s_seh-1,libstdc++-6) withRtlVirtualUnwind, up to the first project frame. libc's own unwinder must be able to step that frame (LibcUnwindHasFrame_nid_no_patch): through its DWARF data (WinLibs GCC 15), or through its.pdatawith a.sehgccpersonality (MSYS2 GCC 16).noexcept, nothing changes: native dispatch handles it as before. A frame with a handler other than__gxx_personality_seh0also leaves it unchanged.RtlUnwindEx, targeting the last runtime frame, with the libgccprivate_fields set to the same target. Cleanups in the runtime frames run through__gxx_personality_seh0, including the collidedSTATUS_GCC_UNWINDand_Unwind_Resumepath._Unwind_Resumelands in the same place again. The frame is then unwound to the project frame.__cxa_allocate_exception, bytewise copy). Free the DLL's copy with its__cxa_free_exception, decrement the DLL's uncaught count, and throw again from the project frame through libc's unwinder.From there, typed and base-class catches,
catch (...),throw;andstd::current_exception/std::rethrow_exceptionwork exactly as for any AnyPS5 throw.Exceptions the handoff refuses still end in the existing report. Those are: objects over 512 bytes, the emergency pool, dependent exceptions, unknown handlers, or a header that fails the reference-count and type checks. They are recorded in
docs/dev/debt/libc.mdthrough the fragment.Tested
Windows 11 build 26300, WinLibs GCC 15.2.0 (
x86_64-ucrt-posix-sehr7), Release, full build withBUILD_TESTING=ON.New test
guest_libstdcxx_dll_throws. It has 6 modes; each also repeats the regex case 1000 times.regexbasestd::invalid_argumentcaught asstd::logic_errorrethrowthrow;plusstd::exception_ptrallcatch (...)rethrownstd::localewith a bad name: the DLL catches and rethrows internallyfilesystemstd::filesystem::canonical: cleanups in the DLL, including the last runtime frame'sEach mode also checks that a project frame between the throw and the catch is cleaned up. On
main, all 6 modes abort withexit 0x20474343and thecannot be deliveredreport; with this PR, all 6 pass.MSYS2 UCRT64 GCC 16.2.0 (Rev7). I built the branch separately with this toolchain (configure selects SEH exceptions), compiling only libc and its exception and unwind test programs.
guest_libstdcxx_dll_throws, 6 modes76a1bbe719(first commit)exit 0xc0000409(FATAL: abort()), the failure reported in reviewbe5796df6a(this head)The same 133 exception and unwind tests pass at this head on both WinLibs GCC 15.2.0 and MSYS2 GCC 16.2.0, from
exception_personalitythrough theunwind_*,guest_raise_exception,guest_libc_internal_*exception*andwindows_exceptiongroups.The original case from #25728. I merged #25548 (head
a2e45c2255) on top:nkwebkit_jscconfigfilemain+ #25548FATAL: C++ exception St11regex_error … libSceNKWebKit.prx+0x2c99de has no unwind datamain+ #25548 + this PRFull suite.
ctest -j 12onmain+ this PR passes 6,745 of 6,747 tests. The two failures areagc_driver_bda_deviceandagc_storage_write_back_host_retile, which also fail onmainon this machine (RTX 5060 Ti); #22774 and #23751 fix them.Repository checks.
python3 tools/check_conventions.py --base origin/mainpasses.tools/pr_overlap.py --branchreports only sharedCMakeLists.txtandUnwind.cppfiles. They merge cleanly; #16791's conflict is inQueueWorker.cpp, which this PR does not touch.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)🤖 Generated with Claude Code
Imported from boykopovar/AnyPS5. Originally posted by PrivacyOnion on 2026-11-22T22:53:15Z.
Review: APPROVED
Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against
76a1bbe719:tools/check_conventions.pypassed.guest_libstdcxx_dll_throws_testsbuilt cleanly.guest_libstdcxx_dll_throws_regex,guest_libstdcxx_dll_throws_base,guest_libstdcxx_dll_throws_rethrow,guest_libstdcxx_dll_throws_all,guest_libstdcxx_dll_throws_rethrown,guest_libstdcxx_dll_throws_filesystem):core/libs/prx/libc/include/exceptions/Unwind.hpp,core/libs/prx/libc/src/exception/Unwind.cpp,core/libs/prx/libc/src/specifics/windows/CrashReport.cpp,core/libs/prx/libc/src/specifics/windows/RuntimeThrowHandoff.cppreverted tomainand the new tests kept, the suite fails: Restoring the change makes it pass again.Looks Good To Me
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-22T23:05:20Z.
Review: COMMENTED
Reviewed — comments
This review covers head
76a1bbe7196ce09651821d6cab8f4372c3486909; the prepared bundle supports the intended change, but critical Windows unwind behavior was not independently validated. There are no verified blocking findings, but I cannot justify approval from the available evidence.What I ran
repo_read.pywas unavailable as an executable tool in this session.Results
Findings
Blocking
None verified.
Non-blocking
None.
Not verified
RtlUnwindExhandoff were not validated against the shipped libstdc++ runtime or by execution. These assumptions are documented indocs/dev/debt/changes/windows-libstdcxx-dll-throws.md.files.json; the manifest lists eight changed files and marks each as included, and I read those eight. No changed file was excluded according to the manifest.To get this to a good state
Nothing is required. Optional: provide independently reproducible Windows build and test results for the pinned head.
Imported from boykopovar/AnyPS5. Originally posted by malekaifa on 2026-11-23T00:29:39Z.
Review: COMMENTED
Independent Windows 11 / MSYS2 UCRT64 GCC 16.2.0 validation on current-main merge tree
ca278ec0cd1b5b865fd7a0ac13f8bc8302ad64d8(same tree as published head76a1bbe7196ce09651821d6cab8f4372c3486909merged withmain7a3206e9cd8c6f6f05e006f744e2412e65ca3449): theguest_libstdcxx_dll_throws_teststarget builds, but all six CTest modes abort with0xc0000409(FATAL: abort()), includingregex,base,rethrow,all,rethrownandfilesystem. This reproduces the MSYS2 GCC 16 failure mode from #25728; it is not the WinLibs GCC 15 result in the PR description.I narrowed the first failure further. A first-chance standalone
std::regexthrow under this MSYS2 runtime reportsExceptionCode=0x20474343, one exception parameter, and primary class0x474e5543432b2b00- both match the constants this patch expects. In the failing test executable,CompileBadRegexat RVA0x2b63has a PE.pdataruntime-function entry[0x2b30,0x2bf3), andRegexErrorReachesCatchat0x2c14has[0x2c00,0x2c5c).FindTargetcurrently rejects the first non-runtime module wheneverRtlLookupFunctionEntryreturns an entry (entry == nullptris required at its unknown-module branch), so it returns false before the handoff. That explains why the failure is the ordinary abort path rather thanHandoffFailed.Could you investigate handling the first project frame when it has PE unwind metadata as well as the renamed DWARF
.ehframdata, or clarify whether GCC 16's emitted.pdatais intentionally outside scope? The current tests do not pass on the MSYS2 GCC 16 toolchain used for this independent run. No source changes were made to your PR branch. AI-assisted: yes.Imported from boykopovar/AnyPS5. Originally posted by PrivacyOnion on 2026-11-23T02:00:46Z.
Review: APPROVED
Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against
be5796df6a:tools/check_conventions.pypassed.guest_libstdcxx_dll_throws_testsbuilt cleanly.guest_libstdcxx_dll_throws_regex,guest_libstdcxx_dll_throws_base,guest_libstdcxx_dll_throws_rethrow,guest_libstdcxx_dll_throws_all,guest_libstdcxx_dll_throws_rethrown,guest_libstdcxx_dll_throws_filesystem):core/libs/prx/libc/include/exceptions/Unwind.hpp,core/libs/prx/libc/src/exception/Unwind.cpp,core/libs/prx/libc/src/specifics/windows/CrashReport.cpp,core/libs/prx/libc/src/specifics/windows/RuntimeThrowHandoff.cppreverted tomainand the new tests kept, the suite fails: Restoring the change makes it pass again.Looks Good To Me
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-23T02:08:01Z.
Review: CHANGES_REQUESTED
Reviewed — changes requested
Changes are required before merge: the handoff misidentifies the GCC exception header’s allocation base and rejects valid exceptions, preventing the advertised runtime throws from being handed to libc.
What I ran
Reviewed head
be5796df6a799eb638296202d03107c62803abdf. I inspected the prepared diff and targeted source context usingrepo_read.py; I did not run build or tests. No merged-preview check was run.Results
Findings
Blocking
unwindpoints to the_Unwind_Exception, not the beginning of the runtime header. The GCC header'stypeis 80 bytes before that pointer and the allocation begins 160 bytes before the thrown object. The code instead subtracts 96 bytes fromunwindand treats that address as the allocation base, then reads the reference count and type at offsets relative to this incorrect base. For ordinary GCC primary exceptions this causes the validation or size check to reject the exception and leaves it undelivered; if the reads happen to pass, the allocation-size and object-size calculations are also wrong. Compute and validate the allocation from the known object/header layout before using_msize. Changed lines:core/libs/prx/libc/src/specifics/windows/RuntimeThrowHandoff.cpp:245,core/libs/prx/libc/src/specifics/windows/RuntimeThrowHandoff.cpp:249,core/libs/prx/libc/src/specifics/windows/RuntimeThrowHandoff.cpp:251.Non-blocking
None.
Not verified
The author reports Windows 11 runs on WinLibs GCC 15 and MSYS2 GCC 16, including the new tests and broad exception suites. Those runs were not performed during this review. I read all changed files listed as reviewed in the manifest:
core/libs/prx/libc/CMakeLists.txt,core/libs/prx/libc/include/RuntimeThrowHandoff.hpp,core/libs/prx/libc/include/exceptions/Unwind.hpp,core/libs/prx/libc/src/exception/Unwind.cpp,core/libs/prx/libc/src/specifics/windows/CrashReport.cpp,core/libs/prx/libc/src/specifics/windows/RuntimeThrowHandoff.cpp,core/libs/prx/libc/tests/cmake/guest_libstdcxx_dll_throws.cmake,core/libs/tests/GuestLibstdcxxDllThrows.cpp, anddocs/dev/debt/changes/windows-libstdcxx-dll-throws.md. The review bundle excluded no changed files.To get this to a good state
Correct the GCC allocation-base calculation and rerun the new Windows DLL-throw tests on the supported toolchains.
Imported from boykopovar/AnyPS5. Originally posted by PrivacyOnion on 2026-11-23T02:24:04Z.
Review: APPROVED
Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against
3bb8833107:tools/check_conventions.pypassed.guest_libstdcxx_dll_throws_testsbuilt cleanly.guest_libstdcxx_dll_throws_regex,guest_libstdcxx_dll_throws_base,guest_libstdcxx_dll_throws_rethrow,guest_libstdcxx_dll_throws_all,guest_libstdcxx_dll_throws_rethrown,guest_libstdcxx_dll_throws_filesystem):core/libs/prx/libc/include/exceptions/Unwind.hpp,core/libs/prx/libc/src/exception/Unwind.cpp,core/libs/prx/libc/src/specifics/windows/CrashReport.cpp,core/libs/prx/libc/src/specifics/windows/RuntimeThrowHandoff.cppreverted tomainand the new tests kept, the suite fails: Restoring the change makes it pass again.Looks Good To Me
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-23T02:27:56Z.
Review: APPROVED
Reviewed — looks good
Nothing blocks this change based on the inspected code; the Windows handoff and regression tests look consistent with the stated scope.
What I ran
Reviewed head
3bb8833107b0167ed72fa1220842036542a5b8edusing the prepared diff andrepo_read.pydirect path reads/searches. No build or tests were run.Results
Findings
Blocking
None.
Non-blocking
None.
Not verified
No programmatic checks were run. The reported Windows builds, regression suite, full suite, and repository checks were not independently verified.
review/files.jsonwas unavailable; the manifest lists 9 changed files and marks all as included. Prepared review inputs read:description.md,commits.md,manifest.json, anddiff.patch. Changed files read:core/libs/prx/libc/CMakeLists.txt,core/libs/prx/libc/include/RuntimeThrowHandoff.hpp,core/libs/prx/libc/include/exceptions/Unwind.hpp,core/libs/prx/libc/src/exception/Unwind.cpp,core/libs/prx/libc/src/specifics/windows/CrashReport.cpp,core/libs/prx/libc/src/specifics/windows/RuntimeThrowHandoff.cpp,core/libs/prx/libc/tests/cmake/guest_libstdcxx_dll_throws.cmake,core/libs/tests/GuestLibstdcxxDllThrows.cpp, anddocs/dev/debt/changes/windows-libstdcxx-dll-throws.md.To get this to a good state
Nothing is required.
Imported from boykopovar/AnyPS5. Originally posted by malekaifa on 2026-11-23T02:28:54Z.
Review: APPROVED
Reviewed exact head
3bb8833107b0167ed72fa1220842036542a5b8ed. The new commit is a layout-only refactor: the mirrored SEH structs deriveunwind - 96, typeunwind - 80, destructorunwind - 72, private words+16, and allocation-to-type+16with compile-time size/offset checks. Those values match GCC 16.2 libsupc++/SEH (_Unwind_Exception64 bytes,__cxa_exception144 bytes with unwind at 80,__cxa_refcounted_exception160 bytes with exception at 16), so it resolves the prior arithmetic-review concern without changing the handoff behavior. The preceding semantic head has independent MSYS2 GCC 16.2 and WinLibs execution evidence; hosted exact-head checks are still allowed to finish naturally. No blocking source finding from this review. AI-assisted: yesImported from boykopovar/AnyPS5. Originally posted by malekaifa on 2026-11-23T23:38:38Z.
Review: APPROVED
Re-reviewed exact head
eb0c55edea00c8278993887afdaa639106f7c2d2after the previously approved semantic/layout head3bb8833107b0167ed72fa1220842036542a5b8ed.The only new commit is the
badallocregression mode. I independently validated this exact commit on machinemainwith Windows/MSYS2 UCRT64 GCC 16.2: all sevenguest_libstdcxx_dll_throws_*modes pass,badallocpasses 20/20 repeated, and the caught dynamic type is exactlystd::bad_alloc. The test count is one pastPTRDIFF_MAX / sizeof(int)but still belowSIZE_MAX / sizeof(int), sostd::allocator<int>::allocatetakes libstdc++'sstd::__throw_bad_allocpath without relying on an actual huge allocation. 1Martin11 independently confirmed the seven-mode set passes with this fix and all seven fail when only the tests are applied without the fix.The earlier handoff implementation and mirrored GCC exception-header layout are unchanged from the exact head I already approved. Hosted Windows, relinker, ASan, register-context, write/edge-watch, conventions, eligibility, PR, labels, report and portability checks are now all terminal/successful (with the unrelated Linux/pipe-write matrix entries intentionally skipped).
No new blocker on this exact head.
AI-assisted: yes
Imported from boykopovar/AnyPS5. Originally posted by malekaifa on 2026-11-24T00:02:44Z.
Review: APPROVED
Re-reviewed exact head
eb0c55edea00c8278993887afdaa639106f7c2d2after the previously approved semantic/layout head3bb8833107b0167ed72fa1220842036542a5b8ed.The only new commit is the
badallocregression mode. I independently validated this exact commit on machinemainwith Windows/MSYS2 UCRT64 GCC 16.2: all sevenguest_libstdcxx_dll_throws_*modes pass,badallocpasses 20/20 repeated, and the caught dynamic type is exactlystd::bad_alloc. The test count is one pastPTRDIFF_MAX / sizeof(int)but still belowSIZE_MAX / sizeof(int), sostd::allocator<int>::allocatetakes libstdc++'sstd::__throw_bad_allocpath without relying on an actual huge allocation. 1Martin11 independently confirmed the seven-mode set passes with this fix and all seven fail when only the tests are applied without the fix.The earlier handoff implementation and mirrored GCC exception-header layout are unchanged from the exact head I already approved. Hosted Windows, relinker, ASan, register-context, write/edge-watch, conventions, eligibility, PR, labels, report and portability checks are now all terminal/successful (with the unrelated Linux/pipe-write matrix entries intentionally skipped).
No new blocker on this exact head.
AI-assisted: yes
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-24T23:54:53Z.
Review: DISMISSED
Reviewed — changes requested
The handoff stub’s SEH unwind metadata does not describe its stack prologue, so unwinding through it can restore the wrong stack pointer and prevent the handoff from reaching its target.
What I ran
eb0c55edea00c8278993887afdaa639106f7c2d2using the pinnedrepo_read.pyreader.Results
Negative control
Not run; no test or build environment was available through the reader.
Findings
Blocking
core/libs/prx/libc/src/specifics/windows/RuntimeThrowHandoff.cpp:176: The stub pushes%rax, then subtracts0x48, but its SEH metadata records only the0x48allocation. WhenRtlUnwindExunwinds through this stub, the metadata cannot account for the extra eight-byte push, so it can locate the wrong return address and stack pointer. Record the complete prologue with valid unwind operations (for example, allocate the full stack space with a singlesuband matching.seh_stackalloc).Non-blocking
None.
Sources
Not verified
To get this to a good state
Correct the stub’s prologue and matching SEH unwind metadata, then rerun the Windows build and handoff tests on the new head.