Windows: libc exports __cxa_init_primary_exception and std::uncaught_exception(s) only as _nid_postfix, so modules bind libstdc++'s #2597

Closed
opened 2026-10-10 08:06:34 +00:00 by AlxcBTW · 1 comment
AlxcBTW commented 2026-10-10 08:06:34 +00:00 (Migrated from github.com)
Contributor

Imported from boykopovar/AnyPS5. Originally posted by AlxcBTW on 2026-10-10T08:06:34Z.


On Windows, libc exports __cxa_init_primary_exception, std::uncaught_exception and std::uncaught_exceptions only as _nid_postfix symbols, because GCC_GLOBAL_ALIAS is empty there. The #ifdef _WIN32 block in Exports.cpp gives __cxa_throw, __cxa_begin_catch and the rest native names, but not these three. Host modules therefore bind libstdc++-6.dll's versions instead (objdump -p on main c72d5ce1: libSceAgcDriver.prx, libSceVideoOut.prx, libkernel.prx). This is the same class of problem as #2540, fixed in #2570.

  • std::uncaught_exceptions() reads libstdc++'s thread state, which libc's throws never touch, so it returns 0 while an exception unwinds. recordDeferredLabels' guard in DeferredLabels.cpp uses it to tell a failed record from a finished one. On Windows a failed record therefore closes the label group with the tracker generation instead of 0, and the dropped ... queued labels line is never printed.
  • std::make_exception_ptr (used by std::promise's destructor for broken_promise) allocates with libc and initializes with libstdc++'s __cxa_init_primary_exception, which lays out its header differently. A test that makes such an exception and rethrows it dies with 0xc0000005.
  • On every platform, libc's __cxa_init_primary_exception leaves the reference count at 1. The Itanium ABI sets it to 0, and libstdc++ and libc++abi both do. The exception_ptr created next adds its own reference, so a made exception is never destroyed. This affects guest code using make_exception_ptr too.

Fix

AlxcBTW:fix/libc-native-exception-exports-v2 (400fce30, on main d5c8e42d). The same change before the rebase (4876aecf) passed full CI on c72d5ce1. In the run on d5c8e42d, Linux and macOS pass. The only Windows failure is agc_driver_prepared_shaders_deferred_undecodable, which fails on every main run at the moment (fix: AlxcBTW/AnyPS5#13). I can't open it as a PR while intake is limited, so it's here if a collaborator wants to cherry-pick it.

The three are now exported natively, next to the others, and __cxa_init_primary_exception sets the reference count to 0.

Tested

Windows 11, WinLibs GCC 15.2.0 r7.

  • windows_exceptions gains two cases. A made, rethrown and released exception must have its argument and its stored copy destroyed once each, and std::uncaught_exceptions() must be 1 in a destructor that runs during unwinding. Without the change the test dies with 0xc0000005. With only the exports, it fails with made exception destroyed an incorrect number of times. With both, it passes.
  • exception_runtime (Linux/macOS) gains the same made-exception check, and it passes in CI.
  • After the change all three modules import the symbols from libc.prx, and patched_libraries loads every patched library.

AI-assisted: yes (Claude Code).

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5789437913 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/2597). Originally posted by [AlxcBTW](https://github.com/AlxcBTW) on 2026-10-10T08:06:34Z. --- On Windows, libc exports `__cxa_init_primary_exception`, `std::uncaught_exception` and `std::uncaught_exceptions` only as `_nid_postfix` symbols, because `GCC_GLOBAL_ALIAS` is empty there. The `#ifdef _WIN32` block in `Exports.cpp` gives `__cxa_throw`, `__cxa_begin_catch` and the rest native names, but not these three. Host modules therefore bind libstdc++-6.dll's versions instead (`objdump -p` on `main` c72d5ce1: `libSceAgcDriver.prx`, `libSceVideoOut.prx`, `libkernel.prx`). This is the same class of problem as #2540, fixed in #2570. - `std::uncaught_exceptions()` reads libstdc++'s thread state, which libc's throws never touch, so it returns 0 while an exception unwinds. `recordDeferredLabels`' guard in `DeferredLabels.cpp` uses it to tell a failed record from a finished one. On Windows a failed record therefore closes the label group with the tracker generation instead of 0, and the `dropped ... queued labels` line is never printed. - `std::make_exception_ptr` (used by `std::promise`'s destructor for `broken_promise`) allocates with libc and initializes with libstdc++'s `__cxa_init_primary_exception`, which lays out its header differently. A test that makes such an exception and rethrows it dies with `0xc0000005`. - On every platform, libc's `__cxa_init_primary_exception` leaves the reference count at 1. The Itanium ABI sets it to 0, and libstdc++ and libc++abi both do. The `exception_ptr` created next adds its own reference, so a made exception is never destroyed. This affects guest code using `make_exception_ptr` too. ### Fix [`AlxcBTW:fix/libc-native-exception-exports-v2`](https://github.com/AlxcBTW/AnyPS5/pull/8) (`400fce30`, on `main` d5c8e42d). The same change before the rebase (`4876aecf`) passed [full CI](https://github.com/AlxcBTW/AnyPS5/actions/runs/38028591087) on c72d5ce1. In the [run on d5c8e42d](https://github.com/AlxcBTW/AnyPS5/actions/runs/38034621830), Linux and macOS pass. The only Windows failure is `agc_driver_prepared_shaders_deferred_undecodable`, which fails on every `main` run at the moment (fix: AlxcBTW/AnyPS5#13). I can't open it as a PR while intake is limited, so it's here if a collaborator wants to cherry-pick it. The three are now exported natively, next to the others, and `__cxa_init_primary_exception` sets the reference count to 0. ### Tested Windows 11, WinLibs GCC 15.2.0 r7. - `windows_exceptions` gains two cases. A made, rethrown and released exception must have its argument and its stored copy destroyed once each, and `std::uncaught_exceptions()` must be 1 in a destructor that runs during unwinding. Without the change the test dies with `0xc0000005`. With only the exports, it fails with `made exception destroyed an incorrect number of times`. With both, it passes. - `exception_runtime` (Linux/macOS) gains the same made-exception check, and it passes in CI. - After the change all three modules import the symbols from libc.prx, and `patched_libraries` loads every patched library. AI-assisted: yes (Claude Code).
Yuva1024 commented 2026-10-10 08:22:27 +00:00 (Migrated from github.com)
Author
Contributor

Imported from boykopovar/AnyPS5. Originally posted by Yuva1024 on 2026-10-10T08:22:27Z.


Tested AlxcBTW/AnyPS5#8 (400fce30) merged onto current main 2c4828a6 (--no-ff; it merges cleanly). Windows 11 (26200), WinLibs GCC 15.2.0 r7, Release, RTX 4060 (driver 617.42). This adds a run with a real GPU, which Windows CI can't do.

  • Full ctest: 658/658, 5 expected skips (video-out window tests); windows_exceptions passes.
  • libSceVideoOut.prx and libkernel.prx now import __cxa_init_primary_exception from libc.prx (it was libstdc++-6.dll), and libSceAgcDriver.prx imports std::uncaught_exceptions from libc.prx.
  • A standalone program linked like the libraries that does std::make_exception_ptr(std::runtime_error(...)) and rethrows it: 1000 round trips and 50 further runs without a failure. Before the fix (on #2570 alone) the same program faulted on its first iteration in libc.prx.
  • VideoOut tests (video_out_flip_tests, guest_video_out_resolution_status, guest_video_out_wqhd_detection), 300 runs each with SDL_VIDEODRIVER=offscreen: 0 aborts in 900, so the #2570 fix still holds with this on top.
  • std::future probe: the word at obj-0xA0 is unchanged and the libc.prx reference count goes 2 → 1.

Not tested on a title.

AI-assisted: yes (Claude Code).

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6095636203 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/2597#issuecomment-6095636203). Originally posted by [Yuva1024](https://github.com/Yuva1024) on 2026-10-10T08:22:27Z. --- Tested [AlxcBTW/AnyPS5#8](https://github.com/AlxcBTW/AnyPS5/pull/8) (`400fce30`) merged onto current `main` `2c4828a6` (`--no-ff`; it merges cleanly). Windows 11 (26200), WinLibs GCC 15.2.0 r7, Release, RTX 4060 (driver 617.42). This adds a run with a real GPU, which Windows CI can't do. - Full `ctest`: 658/658, 5 expected skips (video-out window tests); `windows_exceptions` passes. - `libSceVideoOut.prx` and `libkernel.prx` now import `__cxa_init_primary_exception` from `libc.prx` (it was `libstdc++-6.dll`), and `libSceAgcDriver.prx` imports `std::uncaught_exceptions` from `libc.prx`. - A standalone program linked like the libraries that does `std::make_exception_ptr(std::runtime_error(...))` and rethrows it: 1000 round trips and 50 further runs without a failure. Before the fix (on #2570 alone) the same program faulted on its first iteration in `libc.prx`. - VideoOut tests (`video_out_flip_tests`, `guest_video_out_resolution_status`, `guest_video_out_wqhd_detection`), 300 runs each with `SDL_VIDEODRIVER=offscreen`: 0 aborts in 900, so the #2570 fix still holds with this on top. - `std::future` probe: the word at `obj-0xA0` is unchanged and the `libc.prx` reference count goes 2 → 1. Not tested on a title. AI-assisted: yes (Claude Code).
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#2597
No description provided.