feat(libSceLibcInternal): implement exception what and _Doraise members #16024

Merged
B-Howell merged 5 commits from upstream-pr/16024 into main 2026-11-09 05:49:44 +00:00
B-Howell commented 2026-11-05 11:12:15 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by B-Howell on 2026-11-05T11:12:15Z.


What

Implements seven libSceLibcInternal virtual members of the standard exception classes. Each one now runs the member that libc already puts in the guest vtable of the same class, so a direct call and a virtual call give the same result. Before this change all seven threw through NotImplemented_nid_no_patch.

Export NID Implementation
_ZNKSt9bad_alloc8_DoraiseEv pS-t9AJblSM throws a new std::bad_alloc through libc _ZSt11_Xbad_allocv (ThrowPlain(_ZTVSt9bad_alloc))
_ZNKSt10bad_typeid8_DoraiseEv WMw8eIs0kjM throws a new std::bad_typeid through libc __cxa_bad_typeid (ThrowPlain(_ZTVSt10bad_typeid))
_ZNKSt12system_error8_DoraiseEv uWZBRxLAvEg copies *this into a new exception with libc _ZNSt12system_errorC1ERKS_ and throws it as _ZTISt12system_error; the thrown copy is destroyed with _ZNSt12system_errorD1Ev
_ZNKSt12future_error4whatEv 5sfbtNAdt-M _ZTVSt12future_error slot what (FutureErrorWhat)
_ZNKSt12future_error8_DoraiseEv -syPONaWjqw _ZTVSt12future_error slot _Doraise (RaiseFutureError: copies the message and code, then throws)
_ZNKSt18bad_variant_access4whatEv +5IOQncui3c _ZTVSt18bad_variant_access slot what (GuestPlainWhat)
_ZNKSt22_Future_error_category7messageEi nWfZplDjbxQ _ZTVSt22_Future_error_category slot message (FutureCategoryMessage)

References:

  • Dinkumware _Doraise throws a copy of *this (virtual void _Doraise() const { _RAISE(*this); }, where _RAISE(x) is throw x). The bad_alloc and bad_typeid versions follow the existing libc _ZNKSt8bad_cast8_DoraiseEv, which runs if (!self) throw std::invalid_argument("exception object is null"); LibcException::ThrowPlain(_ZTVSt8bad_cast_nid_postfix);.
  • A null this is handled the way libc/src/exception/StandardExceptions.cpp handles it in each family. Every libc _Doraise and _Raise there (exception, bad_cast, logic_error) starts with if (!self) throw std::invalid_argument("exception object is null");, so all four _Doraise members here (bad_alloc, bad_typeid, system_error and future_error) start with the same line. The libc what members (_ZNKSt9exception4whatEv, _ZNKSt12system_error4whatEv and the others) have no null check and read the object, so the two what members here call the vtable slot without one. _Future_error_category::message does not read the category object, as in libc.
  • The what, _Doraise and message bodies are the functions libc/src/exception/StandardExceptions.cpp already places in _ZTVSt12future_error_nid_postfix {0, &_ZTISt12future_error_nid_postfix, DestroyFutureError_nid_no_patch, DeleteFutureError_nid_no_patch, FutureErrorWhat_nid_no_patch, RaiseFutureError_nid_no_patch}, in _ZTVSt18bad_variant_access_nid_postfix (GuestPlainWhat_nid_no_patch) and in _ZTVSt22_Future_error_category_nid_postfix (FutureCategoryMessage_nid_no_patch).
  • Layouts are the libc ones: ExceptionObject, SystemErrorObject (sizeof == 32, code at 16), and FutureErrorObject (sizeof == 0x40, message at 0x8, code at 0x30).

The code is in a new ExceptionVirtuals.cpp, declared in include/ExceptionVirtuals.hpp, which libSceLibcInternal.hpp includes. StandardExceptions.cpp is not touched, so this does not conflict with #15552, which edits it. The seven stubs are removed from Export.cpp and InventoryExports2.cpp, and their debt entries are removed or cut in docs/dev/debt/libSceLibcInternal.md.

tools/progress.py collect_libraries(): origin/main b11c7bf84f has 9921 done of 37146 and libSceLibcInternal 92 done, 1045 todo. This branch has 9928 done of 37146 and libSceLibcInternal 99 done, 1038 todo: +7 done, -7 todo, no new todo. libc is unchanged at 1628 done, 19 todo.

Tested

Only the focused test targets and their libraries were built, on head 0e0f234240 rebased onto origin/main b11c7bf84f. No title was run.

New guest test guest_libc_internal_exception_virtuals (core/libs/tests/GuestLibcInternalExceptionVirtuals.cpp) checks:

  • bad_alloc and bad_typeid _Doraise throw a new object carrying the libc vtable, type name and what(). They are caught as std::bad_alloc and std::bad_typeid, and a null this throws std::invalid_argument.
  • bad_variant_access::what returns the same pointer as the vtable what.
  • future_error::what returns the four future messages for codes 1-4, matching the vtable, and nullptr for codes 0, 5, 22 and -1.
  • future_error::_Doraise throws a distinct std::future_error copy. A heap message gets new storage and keeps its size and capacity, an inline message is copied inline, the code is copied, and the source is left intact. A null this throws std::invalid_argument.
  • _Future_error_category::message gives the four future messages and the generic _Syserror_map text, or unknown error, for 0, 5, 22 and 99.
  • system_error::_Doraise rethrows a _Throw_Cpp_error object and a hand-built one as a distinct std::system_error that shares the reference-counted message and copies the code. It can be caught as std::runtime_error. A null this throws std::invalid_argument.

Results (ctest -R '^(guest_libc_internal_exception_virtuals|guest_libc_internal_exception_destructors|guest_future_category)$'; the last two are the existing tests of the neighbouring destructors and of the libc future category):

  • Windows 11, WinLibs GCC 15.2.0, Release: 3 runs, each 3 of 3 passed.
  • Linux, WSL Ubuntu 22.04, GCC 11.4, Release, same head: 3 runs, each 3 of 3 passed.
  • Negative control (Windows): with only the new future_error::_Doraise null check removed, the test fails 1 of 1 (SEGFAULT, 0xc0000005 in the null this case). With the check restored it passes again.
  • MSYS_NO_PATHCONV=1 python3 tools/check_conventions.py --base origin/main --title "<title>" --body <this body>: "All conventions checks passed."
  • core/libs/tests/test_public_headers.py --audit-only (Windows): "406 libraries, 28350 exports: unique declarations and umbrella coverage verified".
  • python3 tools/pr_overlap.py --branch feat/r2-exception-virtuals exited 2 and printed "pr_overlap: overlap check failed closed; manually compare every reported PR branch before opening this one". It found no PR with the same exports or the same new files. It reported conflicting lines with #15678, #15692, #15695, #15696 and #15714, and could not compare #5021, #13486, #13778, #13782, #13881, #14212, #14401, #14837 and #14936. I compared every one of them by hand: for each I diffed the PR head against its merge base, searched the diff for the seven NIDs, the seven symbol names and ExceptionVirtuals, and ran git merge-tree of the PR head against main and against this branch.
    • #15678 (names the round 6 LibcInternal stubs): no hits; text conflict in the libSceLibcInternal.hpp include list.
    • #15692 (basic_string members): its only hit is a rewrapped debt-file line that still lists _ZNKSt10bad_typeid8_DoraiseEv (WMw8eIs0kjM) as unimplemented; text conflicts in InventoryExports2.cpp and the debt file.
    • #15695 (C++ ABI type_info destructors): no hits; text conflict on the neighbouring CMakeLists.txt line.
    • #15696 (error category names and codecvt_base queries): its only hits are rewrapped debt-file lines that still list future_error::what, future_error::_Doraise, system_error::_Doraise, bad_variant_access::what and _Future_error_category::message as unimplemented; it does not implement them. Text conflicts in CMakeLists.txt, InventoryExports2.cpp and the debt file.
    • #15714 (ctype<char> widen and narrow): no hits; it touches the same debt-file block (lines 372-377) but git merge-tree merges it with this branch cleanly.
    • #5021, #13486, #13778, #13782, #13881, #14212, #14401, #14837 and #14936: no hits. Each one's git merge-tree conflicts are the same against this branch as against main, so this branch adds none.
    • #15552 (also edits StandardExceptions.cpp) was not reported; it has no hits and merges with this branch with no new conflict.
      These are text conflicts only; whichever merges second rebases.

Independent test evidence

  • Independent Windows check on 0e0f234240: guest_libc_internal_exception_virtuals passed 3/3.
  • Independent AMD run on the same PR head merged onto main 4639eff: the build passed with no new failures or Vulkan validation IDs relative to main.

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: none
  • GPUs tested: n/a
  • AI-assisted: yes

Supersedes #15722, which the open-PR limit closed; same branch and head.

🤖 Generated with Claude Code

🤖 Updated with Codex

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5170566231 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/16024). Originally posted by [B-Howell](https://github.com/B-Howell) on 2026-11-05T11:12:15Z. --- ### What Implements seven `libSceLibcInternal` virtual members of the standard exception classes. Each one now runs the member that `libc` already puts in the guest vtable of the same class, so a direct call and a virtual call give the same result. Before this change all seven threw through `NotImplemented_nid_no_patch`. | Export | NID | Implementation | |---|---|---| | `_ZNKSt9bad_alloc8_DoraiseEv` | `pS-t9AJblSM` | throws a new `std::bad_alloc` through `libc` `_ZSt11_Xbad_allocv` (`ThrowPlain(_ZTVSt9bad_alloc)`) | | `_ZNKSt10bad_typeid8_DoraiseEv` | `WMw8eIs0kjM` | throws a new `std::bad_typeid` through `libc` `__cxa_bad_typeid` (`ThrowPlain(_ZTVSt10bad_typeid)`) | | `_ZNKSt12system_error8_DoraiseEv` | `uWZBRxLAvEg` | copies `*this` into a new exception with `libc` `_ZNSt12system_errorC1ERKS_` and throws it as `_ZTISt12system_error`; the thrown copy is destroyed with `_ZNSt12system_errorD1Ev` | | `_ZNKSt12future_error4whatEv` | `5sfbtNAdt-M` | `_ZTVSt12future_error` slot `what` (`FutureErrorWhat`) | | `_ZNKSt12future_error8_DoraiseEv` | `-syPONaWjqw` | `_ZTVSt12future_error` slot `_Doraise` (`RaiseFutureError`: copies the message and code, then throws) | | `_ZNKSt18bad_variant_access4whatEv` | `+5IOQncui3c` | `_ZTVSt18bad_variant_access` slot `what` (`GuestPlainWhat`) | | `_ZNKSt22_Future_error_category7messageEi` | `nWfZplDjbxQ` | `_ZTVSt22_Future_error_category` slot `message` (`FutureCategoryMessage`) | References: - Dinkumware `_Doraise` throws a copy of `*this` (`virtual void _Doraise() const { _RAISE(*this); }`, where `_RAISE(x)` is `throw x`). The `bad_alloc` and `bad_typeid` versions follow the existing `libc` `_ZNKSt8bad_cast8_DoraiseEv`, which runs `if (!self) throw std::invalid_argument("exception object is null"); LibcException::ThrowPlain(_ZTVSt8bad_cast_nid_postfix);`. - A null `this` is handled the way `libc/src/exception/StandardExceptions.cpp` handles it in each family. Every `libc` `_Doraise` and `_Raise` there (`exception`, `bad_cast`, `logic_error`) starts with `if (!self) throw std::invalid_argument("exception object is null");`, so all four `_Doraise` members here (`bad_alloc`, `bad_typeid`, `system_error` and `future_error`) start with the same line. The `libc` `what` members (`_ZNKSt9exception4whatEv`, `_ZNKSt12system_error4whatEv` and the others) have no null check and read the object, so the two `what` members here call the vtable slot without one. `_Future_error_category::message` does not read the category object, as in `libc`. - The `what`, `_Doraise` and `message` bodies are the functions `libc/src/exception/StandardExceptions.cpp` already places in `_ZTVSt12future_error_nid_postfix {0, &_ZTISt12future_error_nid_postfix, DestroyFutureError_nid_no_patch, DeleteFutureError_nid_no_patch, FutureErrorWhat_nid_no_patch, RaiseFutureError_nid_no_patch}`, in `_ZTVSt18bad_variant_access_nid_postfix` (`GuestPlainWhat_nid_no_patch`) and in `_ZTVSt22_Future_error_category_nid_postfix` (`FutureCategoryMessage_nid_no_patch`). - Layouts are the `libc` ones: `ExceptionObject`, `SystemErrorObject` (`sizeof == 32`, `code` at 16), and `FutureErrorObject` (`sizeof == 0x40`, `message` at 0x8, `code` at 0x30). The code is in a new `ExceptionVirtuals.cpp`, declared in `include/ExceptionVirtuals.hpp`, which `libSceLibcInternal.hpp` includes. `StandardExceptions.cpp` is not touched, so this does not conflict with #15552, which edits it. The seven stubs are removed from `Export.cpp` and `InventoryExports2.cpp`, and their debt entries are removed or cut in `docs/dev/debt/libSceLibcInternal.md`. `tools/progress.py` `collect_libraries()`: `origin/main` `b11c7bf84f` has 9921 done of 37146 and `libSceLibcInternal` 92 done, 1045 todo. This branch has 9928 done of 37146 and `libSceLibcInternal` 99 done, 1038 todo: +7 done, -7 todo, no new todo. `libc` is unchanged at 1628 done, 19 todo. ### Tested <!-- OS, title or homebrew, what was checked. "Not tested" is a valid answer. A performance change: the summary lines of main and of this PR from the same machine and scene, with the traces attached, captured as the Performance report issue form describes. --> Only the focused test targets and their libraries were built, on head `0e0f234240` rebased onto `origin/main` `b11c7bf84f`. No title was run. New guest test `guest_libc_internal_exception_virtuals` (`core/libs/tests/GuestLibcInternalExceptionVirtuals.cpp`) checks: - `bad_alloc` and `bad_typeid` `_Doraise` throw a new object carrying the `libc` vtable, type name and `what()`. They are caught as `std::bad_alloc` and `std::bad_typeid`, and a null `this` throws `std::invalid_argument`. - `bad_variant_access::what` returns the same pointer as the vtable `what`. - `future_error::what` returns the four future messages for codes 1-4, matching the vtable, and `nullptr` for codes 0, 5, 22 and -1. - `future_error::_Doraise` throws a distinct `std::future_error` copy. A heap message gets new storage and keeps its size and capacity, an inline message is copied inline, the code is copied, and the source is left intact. A null `this` throws `std::invalid_argument`. - `_Future_error_category::message` gives the four future messages and the generic `_Syserror_map` text, or `unknown error`, for 0, 5, 22 and 99. - `system_error::_Doraise` rethrows a `_Throw_Cpp_error` object and a hand-built one as a distinct `std::system_error` that shares the reference-counted message and copies the code. It can be caught as `std::runtime_error`. A null `this` throws `std::invalid_argument`. Results (`ctest -R '^(guest_libc_internal_exception_virtuals|guest_libc_internal_exception_destructors|guest_future_category)$'`; the last two are the existing tests of the neighbouring destructors and of the `libc` future category): - Windows 11, WinLibs GCC 15.2.0, Release: 3 runs, each 3 of 3 passed. - Linux, WSL Ubuntu 22.04, GCC 11.4, Release, same head: 3 runs, each 3 of 3 passed. - Negative control (Windows): with only the new `future_error::_Doraise` null check removed, the test fails 1 of 1 (`SEGFAULT`, `0xc0000005` in the null `this` case). With the check restored it passes again. - `MSYS_NO_PATHCONV=1 python3 tools/check_conventions.py --base origin/main --title "<title>" --body <this body>`: "All conventions checks passed." - `core/libs/tests/test_public_headers.py --audit-only` (Windows): "406 libraries, 28350 exports: unique declarations and umbrella coverage verified". - `python3 tools/pr_overlap.py --branch feat/r2-exception-virtuals` exited 2 and printed "pr_overlap: overlap check failed closed; manually compare every reported PR branch before opening this one". It found no PR with the same exports or the same new files. It reported conflicting lines with #15678, #15692, #15695, #15696 and #15714, and could not compare #5021, #13486, #13778, #13782, #13881, #14212, #14401, #14837 and #14936. I compared every one of them by hand: for each I diffed the PR head against its merge base, searched the diff for the seven NIDs, the seven symbol names and `ExceptionVirtuals`, and ran `git merge-tree` of the PR head against `main` and against this branch. - #15678 (names the round 6 LibcInternal stubs): no hits; text conflict in the `libSceLibcInternal.hpp` include list. - #15692 (`basic_string` members): its only hit is a rewrapped debt-file line that still lists `_ZNKSt10bad_typeid8_DoraiseEv` (`WMw8eIs0kjM`) as unimplemented; text conflicts in `InventoryExports2.cpp` and the debt file. - #15695 (C++ ABI `type_info` destructors): no hits; text conflict on the neighbouring `CMakeLists.txt` line. - #15696 (error category names and `codecvt_base` queries): its only hits are rewrapped debt-file lines that still list `future_error::what`, `future_error::_Doraise`, `system_error::_Doraise`, `bad_variant_access::what` and `_Future_error_category::message` as unimplemented; it does not implement them. Text conflicts in `CMakeLists.txt`, `InventoryExports2.cpp` and the debt file. - #15714 (`ctype<char>` widen and narrow): no hits; it touches the same debt-file block (lines 372-377) but `git merge-tree` merges it with this branch cleanly. - #5021, #13486, #13778, #13782, #13881, #14212, #14401, #14837 and #14936: no hits. Each one's `git merge-tree` conflicts are the same against this branch as against `main`, so this branch adds none. - #15552 (also edits `StandardExceptions.cpp`) was not reported; it has no hits and merges with this branch with no new conflict. These are text conflicts only; whichever merges second rebases. #### Independent test evidence - [Independent Windows check](https://github.com/boykopovar/AnyPS5/pull/16024#issuecomment-6657279799) on `0e0f234240`: `guest_libc_internal_exception_virtuals` passed 3/3. - [Independent AMD run](https://github.com/boykopovar/AnyPS5/pull/16024#issuecomment-6657898404) on the same PR head merged onto main `4639eff`: the build passed with no new failures or Vulkan validation IDs relative to main. ### Checklist <!-- Rules behind each item: https://github.com/boykopovar/AnyPS5/blob/main/CONTRIBUTING.md --> - [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 under Silent stubs in their library's [technical debt](https://github.com/boykopovar/AnyPS5/tree/main/docs/dev/debt) file - [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] Fixes: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes Supersedes #15722, which the open-PR limit closed; same branch and head. 🤖 Generated with [Claude Code](https://claude.com/claude-code) 🤖 Updated with [Codex](https://openai.com/codex)
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!16024
No description provided.