fix(libc): release future results' exceptions through libc on Windows #2570

Merged
oneandonlydean merged 1 commit from upstream-pr/2570 into main 2026-10-10 06:51:09 +00:00
oneandonlydean commented 2026-10-10 06:30:08 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-10T06:30:08Z.


Carrying @AlxcBTW's fix for #2540 while PR intake is paused. The change is theirs, unchanged (from AlxcBTW/AnyPS5#6).

What and why

libc implements the C++ exception runtime and already defines
exception_ptr's out-of-line members, so host modules that link libc
reference-count libc's exceptions. Code inside libstdc++-6.dll does not
see those definitions on Windows: std::__future_base::_Result_base's
destructor is one of them, and it releases the stored exception with
libstdc++'s own exception_ptr::_M_release. That decrements a count 0xa0
bytes before the object, while libc keeps it 0x80 bytes before, so the
decrement lands in the heap memory in front of libc's allocation and
the exception is never freed.

VideoOutDriver reports the presentation thread's start-up failure
through a std::promise, so video_out_flip_lifetime sometimes ended with
0xc0000374 when SDL's offscreen driver refused the window.

libc now defines the base destructor, with a _nid_postfix twin so that
the NID-patched libc keeps its name, and modules bind it there.

Fixes #2540.

Tested

  • See #2540 for the reproduction and the author's runs.
  • tools/check_conventions.py passes.

Checklist

  • Based on current main; no other open PR implements the same functions
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt
  • Unimplemented paths throw (NotImplemented_nid_no_patch); silent stubs are listed in TechnicalDebt
  • No notes, investigation .md files or images added to the repository (attach them to this PR)
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: none
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4809273722 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2570). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-10T06:30:08Z. --- Carrying @AlxcBTW's fix for #2540 while PR intake is paused. The change is theirs, unchanged (from AlxcBTW/AnyPS5#6). ### What and why libc implements the C++ exception runtime and already defines exception_ptr's out-of-line members, so host modules that link libc reference-count libc's exceptions. Code inside libstdc++-6.dll does not see those definitions on Windows: std::__future_base::_Result_base's destructor is one of them, and it releases the stored exception with libstdc++'s own exception_ptr::_M_release. That decrements a count 0xa0 bytes before the object, while libc keeps it 0x80 bytes before, so the decrement lands in the heap memory in front of libc's allocation and the exception is never freed. VideoOutDriver reports the presentation thread's start-up failure through a std::promise, so video_out_flip_lifetime sometimes ended with 0xc0000374 when SDL's offscreen driver refused the window. libc now defines the base destructor, with a _nid_postfix twin so that the NID-patched libc keeps its name, and modules bind it there. Fixes #2540. ### Tested - See #2540 for the reproduction and the author's runs. - `tools/check_conventions.py` passes. ### Checklist - [x] Based on current `main`; no other open PR implements the same functions - [x] One topic per PR; follow-ups go in a new PR - [x] No comments in code except [technical debt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/CONVENTIONS.md) - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed in [TechnicalDebt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/TechnicalDebt.md#silent-stubs) - [x] No notes, investigation `.md` files or images added to the repository (attach them to this PR) - [x] New third-party code is a submodule built from source - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] AI-assisted: yes
github-actions[bot] commented 2026-10-10 06:30:46 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-10T06:30:46Z.


Progress report (2e19774 → cc56748)

📈 System libraries: 85.69% (+0.01%, +1 implemented functions)

✅ 1 implemented
Library Name
libc _ZNSt13__future_base12_Result_baseD2Ev_nid_postfix
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6094671341 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2570#issuecomment-6094671341). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-10T06:30:46Z. --- <!-- progress-report --> ### Progress report (2e19774 → cc56748) 📈 **System libraries**: 85.69% (+0.01%, +1 implemented functions) <details> <summary>✅ 1 implemented</summary> | Library | Name | | - | - | | libc | `_ZNSt13__future_base12_Result_baseD2Ev_nid_postfix` | </details>
AlxcBTW commented 2026-10-10 06:49:12 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by AlxcBTW on 2026-10-10T06:49:12Z.


Thanks for carrying it. The conventions failure is only the template check: it wants a ### What heading, and this body has ### What and why.

Measurements behind the change, Windows 11, WinLibs GCC 15.2.0 r7, video_out_flip_tests.exe with SDL_VIDEODRIVER=offscreen:

0xc0000374
main c963432d 25 / 300, and 47 / 200 with SDL_JOYSTICK_WGI=0
with this change 0 / 300, and 0 / 200 with SDL_JOYSTICK_WGI=0

The same corruption also hits guest_video_out_resolution_status, which takes the same failed sceVideoOutOpen path. It failed with 0xc0000374 in an unrelated fork run without this change (windows job).

A related gap I found while checking for other cases: on Windows libc doesn't export __cxa_init_primary_exception or std::uncaught_exception(s) natively either, so modules bind libstdc++'s versions. std::uncaught_exceptions() then returns 0 during unwinding, which affects recordDeferredLabels' failure guard. The fix is AlxcBTW/AnyPS5#7, and I'll open an issue for it.

AI-assisted: yes (Claude Code).

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6094814465 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2570#issuecomment-6094814465). Originally posted by [AlxcBTW](https://github.com/AlxcBTW) on 2026-10-10T06:49:12Z. --- Thanks for carrying it. The `conventions` failure is only the template check: it wants a `### What` heading, and this body has `### What and why`. Measurements behind the change, Windows 11, WinLibs GCC 15.2.0 r7, `video_out_flip_tests.exe` with `SDL_VIDEODRIVER=offscreen`: | | `0xc0000374` | |---|---| | `main` c963432d | 25 / 300, and 47 / 200 with `SDL_JOYSTICK_WGI=0` | | with this change | 0 / 300, and 0 / 200 with `SDL_JOYSTICK_WGI=0` | The same corruption also hits `guest_video_out_resolution_status`, which takes the same failed `sceVideoOutOpen` path. It failed with `0xc0000374` in an unrelated fork run without this change ([windows job](https://github.com/AlxcBTW/AnyPS5/actions/runs/38028591087/job/114144607078)). A related gap I found while checking for other cases: on Windows libc doesn't export `__cxa_init_primary_exception` or `std::uncaught_exception(s)` natively either, so modules bind libstdc++'s versions. `std::uncaught_exceptions()` then returns 0 during unwinding, which affects `recordDeferredLabels`' failure guard. The fix is [AlxcBTW/AnyPS5#7](https://github.com/AlxcBTW/AnyPS5/pull/7), and I'll open an issue for it. AI-assisted: yes (Claude Code).
Sign in to join this conversation.
No reviewers
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!2570
No description provided.