Skip to content

feat(libc): implement std::_Throw_bad_optional_access - #7872

Merged
oneandonlydean merged 2 commits into
boykopovar:mainfrom
ErenYeager1836:feat/libc-throw-bad-optional-access
Oct 19, 2026
Merged

oneandonlydean merged 2 commits into
boykopovar:mainfrom
ErenYeager1836:feat/libc-throw-bad-optional-access

Conversation

@ErenYeager1836

@ErenYeager1836 ErenYeager1836 commented Oct 19, 2026 •

Copy link
Copy Markdown
Contributor

What

Implements std::_Throw_bad_optional_access (_ZSt26_Throw_bad_optional_accessv, NID KwzEip7m6TM) and the std::bad_optional_access exception class plumbing in core/libs/prx/libc/src/exception/StandardExceptions.cpp:

Tested

  • guest_bad_optional_access unit test passes locally with CTest (100% tests passed out of 1).
  • ExceptionRuntime catch coverage tests throwing and catching by const std::bad_optional_access&.
  • python tools/check_conventions.py --base upstream/main passes 100%.

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 under Silent stubs in their library's technical debt file
  • 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
  • Fixes: part of PPSA26578: unresolved ELF imports from libc.prx (0xC0000139) | Windows, i7-13700K, RTX 5060, 32GB DDR5 #7624
  • Unblocks: PPSA26578, PPSA34015
  • GPUs tested: n/a
  • AI-assisted: yes

PPSA26578 (issue boykopovar#7624) imports _ZSt26_Throw_bad_optional_accessv (NID
KwzEip7m6TM) from libc, and its bundled libc.prx does not export it,
causing the process to exit with 0xC0000139 (GetLastError: 127).

Implement std::bad_optional_access type record and vtable deriving from
std::exception, thrown through ThrowPlain, with what() returning
"bad_optional_access", and add unit test coverage in ExceptionRuntime.
@github-actions

github-actions Bot commented Oct 19, 2026 •

Copy link
Copy Markdown

Progress report (d047690 → e775e45)

📈 System libraries: 79.77% (+0.03%, +7 implemented functions)

✅ 7 implemented
Library Name
libc _ZNKSt19bad_optional_access4whatEv_nid_postfix
libc _ZNSt19bad_optional_accessC1Ev_nid_postfix
libc _ZNSt19bad_optional_accessC2Ev_nid_postfix
libc _ZNSt19bad_optional_accessD0Ev_nid_postfix
libc _ZNSt19bad_optional_accessD1Ev_nid_postfix
libc _ZNSt19bad_optional_accessD2Ev_nid_postfix
libc _ZSt26_Throw_bad_optional_accessv_nid_postfix

@github-actions

github-actions Bot commented Oct 19, 2026 •

Copy link
Copy Markdown

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.

Pull request Conflicting files (lines on main) Same exports Same new files
PR 5303 docs/dev/debt/libc.md L100
PR 6079 _ZNKSt19bad_optional_access4whatEv_nid_postfix
_ZNSt19bad_optional_accessC1Ev_nid_postfix
_ZNSt19bad_optional_accessC2Ev_nid_postfix
_ZNSt19bad_optional_accessD0Ev_nid_postfix
_ZNSt19bad_optional_accessD1Ev_nid_postfix
_ZNSt19bad_optional_accessD2Ev_nid_postfix
_ZSt26_Throw_bad_optional_accessv_nid_postfix
core/libs/tests/GuestBadOptionalAccess.cpp
PR 7386 docs/dev/debt/libc.md L100-101
PR 7525 docs/dev/debt/libc.md L100-101

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.

Pull request Shared files not compared
PR 5021 core/libs/prx/libc/CMakeLists.txt
docs/dev/debt/libc.md
PR 6044 core/libs/prx/libc/CMakeLists.txt
core/libs/tests/ExceptionRuntime.cpp
PR 6079 core/libs/prx/libc/CMakeLists.txt
core/libs/prx/libc/src/exception/StandardExceptions.cpp
core/libs/tests/GuestBadOptionalAccess.cpp
PR 6226 docs/dev/debt/libc.md
PR 6469 docs/dev/debt/libc.md
PR 6819 core/libs/prx/libc/CMakeLists.txt
docs/dev/debt/libc.md
PR 7081 core/libs/prx/libc/CMakeLists.txt
docs/dev/debt/libc.md
PR 7201 core/libs/prx/libc/CMakeLists.txt
core/libs/tests/ExceptionRuntime.cpp
PR 7332 core/libs/prx/libc/CMakeLists.txt
core/libs/tests/ExceptionRuntime.cpp
docs/dev/debt/libc.md
PR 7417 core/libs/prx/libc/CMakeLists.txt
docs/dev/debt/libc.md
PR 7473 docs/dev/debt/libc.md
PR 7715 core/libs/prx/libc/CMakeLists.txt
docs/dev/debt/libc.md

@fogydes fogydes left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verified the implementation and test in PR #7872:

  • Implementation: _ZSt26_Throw_bad_optional_accessv_nid_postfix follows the exact pattern of neighboring exceptions (_throw_bad_variant_access, _Throw_bad_weak_ptr), properly establishing type info and throwing via ThrowPlain with what() returning "bad_optional_access".
  • Unit Test: core/libs/tests/ExceptionRuntime.cpp verifies that calling the export throws an exception caught as std::bad_optional_access with the expected what() string.
  • Technical Debt: docs/dev/debt/libc.md updated cleanly.
  • Conventions: No code comments added; code conventions check passed.

The CI conventions workflow is currently failing because the pull request description does not include the required template sections from .github/pull_request_template.md. Updating the PR body with ### Tested and ### Checklist will clear this check:

### What

PPSA26578 (issue #7624) imports `_ZSt26_Throw_bad_optional_accessv` (NID `KwzEip7m6TM`) from `libc`, and its bundled `libc.prx` does not export it, causing the process to exit at start-up on `main` with:

```text
0xC0000139 (GetLastError: 127)
unresolved ELF import KwzEip7m6TM from libc.prx

This implements std::_Throw_bad_optional_access following the neighboring bad_variant_access and bad_weak_ptr pattern:

  • _ZTISt19bad_optional_access_nid_postfix and _ZTVSt19bad_optional_access_nid_postfix type record and vtable deriving from std::exception.
  • _ZSt26_Throw_bad_optional_accessv_nid_postfix() throwing through ThrowPlain.
  • what() returning "bad_optional_access", matching Dinkumware / libc++ conventions.
  • Unit test coverage in ExceptionRuntime.cpp verifying the exception is caught as std::bad_optional_access.

Fixes part of #7624.

Tested

Windows 11, MinGW-w64 GCC 15.2.0 (Release). Verified build and test in ExceptionRuntime.cpp.

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 under Silent stubs in their library's technical debt file
  • 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
  • Fixes: none
  • Unblocks: PPSA26578 #7624
  • GPUs tested: n/a
  • AI-assisted: no
*(If AI assistance was used, adjust `AI-assisted` to `yes`)*.

@MarcZX10

Copy link
Copy Markdown
Contributor

Heads-up: open PR #6079 already adds the same _ZSt26_Throw_bad_optional_accessv export (NID KwzEip7m6TM) and exception type/vtable in core/libs/prx/libc/src/exception/StandardExceptions.cpp. This PR overlaps that implementation, although it adds a regression case for PPSA26578. Please coordinate with #6079’s author/maintainers on which implementation and tests should proceed; the two PRs currently also use different what() strings, which would need a single verified choice.

@github-actions github-actions Bot added the unblocks title Names a title ID on the Unblocks line label Oct 19, 2026
@ErenYeager1836

Copy link
Copy Markdown
Contributor Author

Thanks for the review and heads-up @MarcZX10!

Regarding the what() string: on PR #6079, @adriadam10 requested changes specifically asking to use libc++'s "bad_optional_access" (matching the neighboring bad_variant_access and bad_weak_ptr in AnyPS5) and to document under technical debt if unverified on hardware. This PR implements that exact libc++ string "bad_optional_access" and documents it in docs/dev/debt/libc.md.

Also updated the PR description with the required ### Checklist and ### Tested sections; all CI checks are now passing green. Happy to coordinate or adapt as maintainers prefer!

@oneandonlydean oneandonlydean self-assigned this Oct 19, 2026
@MarcZX10

MarcZX10 commented Oct 19, 2026 •

Copy link
Copy Markdown
Contributor

Thanks for explaining the libc++ string choice and updating the PR description. One remaining accuracy point: the new technical-debt entry says bad_optional_access matches Dinkumware/libc++ conventions, but it does not explicitly say the console libc.prx string is unverified. The review on #6079 asked for that limitation to be recorded when title evidence is unavailable. Please add that clarification.

The overlap with #6079 also remains: both PRs define _ZSt26_Throw_bad_optional_accessv_nid_postfix, while #6079 additionally adds the exception type, constructors, destructor and what() exports. Please coordinate with its author or maintainers on one source of truth before either version merges.

@ErenYeager1836

Copy link
Copy Markdown
Contributor Author

Thanks @MarcZX10!

  1. Clarified technical debt: Updated docs/dev/debt/libc.md to explicitly record that the console libc.prx string is unverified:

    ...returning the what() string bad_optional_access matching Dinkumware and libc++ conventions; the console libc.prx string is unverified.

  2. Consolidated with feat(libc): throw bad_optional_access like bad_weak_ptr #6079: Added the full exception type, constructors (_ZNSt19bad_optional_accessC1Ev, C2Ev), destructors (D1Ev, D2Ev, D0Ev), and what() exports in StandardExceptions.cpp, along with GuestBadOptionalAccess.cpp which validates the type record, vtable, construction, destruction, and pins what() to "bad_optional_access" (addressing the review feedback on feat(libc): throw bad_optional_access like bad_weak_ptr #6079). This unblocks both PPSA26578 (PPSA26578: unresolved ELF imports from libc.prx (0xC0000139) | Windows, i7-13700K, RTX 5060, 32GB DDR5 #7624) and PPSA34015 (feat(libc): throw bad_optional_access like bad_weak_ptr #6079), making this PR a single comprehensive implementation.

@github-actions github-actions Bot added the build label Oct 19, 2026
@oneandonlydean

Copy link
Copy Markdown
Collaborator

Thanks @ErenYeager1836. Merging this one: it carries the std::bad_optional_access exports and test from #6079 by @notaxeltv, plus the change @adriadam10 asked for there (what() returns "bad_optional_access", and the debt note says the PS5 string is unverified). Credit for the original implementation and test goes to @notaxeltv. Full CI is green on e775e45 (guest_bad_optional_access passes on Linux and Windows, exception_runtime on Linux).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

build docs libc unblocks title Names a title ID on the Unblocks line

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants