feat(libc): throw std::bad_cast and the std::__sce_v2 bad_any_cast, format_error and future_error #15552

Merged
Danigamer05 merged 2 commits from upstream-pr/15552 into main 2026-11-07 10:40:23 +00:00
Danigamer05 commented 2026-11-03 18:43:07 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Danigamer05 on 2026-11-03T18:43:07Z.


What

Add four exception throwers to libc:

Export NID Original RVA
_ZSt6_ThrowRKSt8bad_cast sJ41UnZr6dQ 0xCC310
_ZNSt8__sce_v26_ThrowERKNS_12bad_any_castE m+y2TtGJp54 0xCC710
_ZNSt8__sce_v26_ThrowERKNS_12format_errorE HfF9zbvHXXE 0xCC6A0
_ZNSt8__sce_v26_ThrowERKNS_12future_errorE ZdF3BhEh5sk 0xCC620

The behavior comes from the libc.prx bundled with PPSA03671 v01.001.005 (J03848509, SHA-256 483961e086b8a1fffa8138b3c0badf798987fb5f35ec908d828946408be2d8a6). Each follows the module's thrower and the type information it throws, read from that module with its relocations applied:

  • _Throw(const bad_cast&) ignores its argument and throws a new 8-byte std::bad_cast, using the _ZTVSt8bad_cast already in main. The module's what() returns std::bad_cast, as main's does.
  • The versioned bad_any_cast also ignores its argument and throws a new 8-byte object. Its type NSt8__sce_v212bad_any_castE derives from std::bad_cast, and what() returns Bad any_cast.
  • The versioned format_error (NSt8__sce_v212format_errorE, derived from the versioned runtime_error) copies the message object, through the existing SceV2::ThrowCopy.
  • The versioned future_error (NSt8__sce_v212future_errorE, derived from the versioned logic_error) copies the message and the 16-byte error code into a 32-byte object, through the existing SceV2::ThrowSystemCopy. Its what() is the module's own: it reads the first 32-bit word of the code and returns broken promise, future already retrieved, promise already satisfied or no state for 1 to 4, and a null pointer otherwise. It reuses main's FutureErrorMessage, which holds the same four texts.

The two other free throwers of that module, _Throw_bad_any_cast(const char*) and _Throw_bad_node_access(const char*), build an object holding a std::string and are not part of this change.

Tested

Windows x86-64, WinLibs GCC 15.2.0, Release, BUILD_TESTING=ON, main 1e658521a0a. Exact tested commit: e92455a5f4b.

  • Built libc, libkernel, their NID-patched production libraries and the related tests. guest_sce_v2_exceptions passes with the new cases, along with guest_filesystem_error, exception_personality, windows_exceptions, guest_libc_internal_exception_destructors, public_headers and the other guest_sce_v2_* CTests.
  • guest_sce_v2_exceptions now throws each new export through its assembly catch frame and checks the landing selector, the caught type name and what():
    • bad_any_cast is caught as std::bad_cast, not as _Error_exception;
    • bad_cast is caught as std::exception;
    • format_error is caught as the versioned runtime_error and keeps the copied message;
    • future_error is caught as the versioned logic_error, answers promise already satisfied for code word 3, and keeps both code words and the category.
  • The type names, base classes, what() strings, object sizes and copy layouts were read from the module's typeinfo, vtables, .rodata and throwers. The throwers were not run side by side: they end in __cxa_throw, which a host harness cannot catch for the module's frames.
  • Negative control: four lines were altered one at a time: the bad_any_cast text, the format_error base class, the word that future_error::what() reads, and the vtable that _Throw(bad_cast) throws. guest_sce_v2_exceptions failed on each. The source was then restored byte for byte (SHA-256 checked) and rebuilt, and the test passed again.
  • Code conventions and git diff --check pass.
  • tools/pr_overlap.py read 1,757 open PRs and reports no overlap. It keeps exit 2 for 29 branches that already conflict with main. Merging each of the 29 with this branch and with main using git merge-tree gives the same conflict blocks. Two of them name a related symbol: #13449 adds the unversioned future_error type-name string as data, and #13486 lists unversioned future_error destructors in a table. The branch also merges cleanly with #15336, #15471, #15484, #15490, #15501 and #15519.

Not tested: Linux, macOS, another SDK, a game, GPU or console. No game compatibility claim. No original module, extracted bytes, harness or log is committed or uploaded.

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

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5142672341 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/15552). Originally posted by [Danigamer05](https://github.com/Danigamer05) on 2026-11-03T18:43:07Z. --- ### What Add four exception throwers to libc: | Export | NID | Original RVA | | --- | --- | --- | | `_ZSt6_ThrowRKSt8bad_cast` | `sJ41UnZr6dQ` | `0xCC310` | | `_ZNSt8__sce_v26_ThrowERKNS_12bad_any_castE` | `m+y2TtGJp54` | `0xCC710` | | `_ZNSt8__sce_v26_ThrowERKNS_12format_errorE` | `HfF9zbvHXXE` | `0xCC6A0` | | `_ZNSt8__sce_v26_ThrowERKNS_12future_errorE` | `ZdF3BhEh5sk` | `0xCC620` | The behavior comes from the `libc.prx` bundled with PPSA03671 v01.001.005 (J03848509, SHA-256 `483961e086b8a1fffa8138b3c0badf798987fb5f35ec908d828946408be2d8a6`). Each follows the module's thrower and the type information it throws, read from that module with its relocations applied: - `_Throw(const bad_cast&)` ignores its argument and throws a new 8-byte `std::bad_cast`, using the `_ZTVSt8bad_cast` already in main. The module's `what()` returns `std::bad_cast`, as main's does. - The versioned `bad_any_cast` also ignores its argument and throws a new 8-byte object. Its type `NSt8__sce_v212bad_any_castE` derives from `std::bad_cast`, and `what()` returns `Bad any_cast`. - The versioned `format_error` (`NSt8__sce_v212format_errorE`, derived from the versioned `runtime_error`) copies the message object, through the existing `SceV2::ThrowCopy`. - The versioned `future_error` (`NSt8__sce_v212future_errorE`, derived from the versioned `logic_error`) copies the message and the 16-byte error code into a 32-byte object, through the existing `SceV2::ThrowSystemCopy`. Its `what()` is the module's own: it reads the first 32-bit word of the code and returns `broken promise`, `future already retrieved`, `promise already satisfied` or `no state` for 1 to 4, and a null pointer otherwise. It reuses main's `FutureErrorMessage`, which holds the same four texts. The two other free throwers of that module, `_Throw_bad_any_cast(const char*)` and `_Throw_bad_node_access(const char*)`, build an object holding a `std::string` and are not part of this change. ### 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. --> Windows x86-64, WinLibs GCC 15.2.0, Release, `BUILD_TESTING=ON`, main `1e658521a0a`. Exact tested commit: `e92455a5f4b`. - Built libc, libkernel, their NID-patched production libraries and the related tests. `guest_sce_v2_exceptions` passes with the new cases, along with `guest_filesystem_error`, `exception_personality`, `windows_exceptions`, `guest_libc_internal_exception_destructors`, `public_headers` and the other `guest_sce_v2_*` CTests. - `guest_sce_v2_exceptions` now throws each new export through its assembly catch frame and checks the landing selector, the caught type name and `what()`: - `bad_any_cast` is caught as `std::bad_cast`, not as `_Error_exception`; - `bad_cast` is caught as `std::exception`; - `format_error` is caught as the versioned `runtime_error` and keeps the copied message; - `future_error` is caught as the versioned `logic_error`, answers `promise already satisfied` for code word 3, and keeps both code words and the category. - The type names, base classes, `what()` strings, object sizes and copy layouts were read from the module's typeinfo, vtables, `.rodata` and throwers. The throwers were not run side by side: they end in `__cxa_throw`, which a host harness cannot catch for the module's frames. - Negative control: four lines were altered one at a time: the `bad_any_cast` text, the `format_error` base class, the word that `future_error::what()` reads, and the vtable that `_Throw(bad_cast)` throws. `guest_sce_v2_exceptions` failed on each. The source was then restored byte for byte (SHA-256 checked) and rebuilt, and the test passed again. - Code conventions and `git diff --check` pass. - `tools/pr_overlap.py` read 1,757 open PRs and reports no overlap. It keeps exit 2 for 29 branches that already conflict with main. Merging each of the 29 with this branch and with main using `git merge-tree` gives the same conflict blocks. Two of them name a related symbol: #13449 adds the unversioned `future_error` type-name string as data, and #13486 lists unversioned `future_error` destructors in a table. The branch also merges cleanly with #15336, #15471, #15484, #15490, #15501 and #15519. Not tested: Linux, macOS, another SDK, a game, GPU or console. No game compatibility claim. No original module, extracted bytes, harness or log is committed or uploaded. ### 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 🤖 Generated with [Claude Code](https://claude.com/claude-code)
XairRrR commented 2026-11-03 20:23:27 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by XairRrR on 2026-11-03T20:23:27Z.

Review: APPROVED


Reviewed — approved

The four new throwers (std::_Throw(const bad_cast&) and the std::__sce_v2 _Throw for bad_any_cast, format_error and future_error) merge cleanly, build, and pass the extended guest_sce_v2_exceptions. The negative control and a targeted mutation both make that test fail. I have two non-blocking notes.

What I ran

Head e92455a5f4bc902b32775437bdf5231e782f9313, main 086c0686c8adff3ee648fdbe34e0b0b647e1d511, merged tree f3b27126c6910ae6eaa4b22b96aad52e054af9c3. Debian 13 VM, GCC 14.2.0, Release, Ninja, BUILD_TESTING=ON.

git diff --check 086c0686c8adff3ee648fdbe34e0b0b647e1d511...e92455a5f4bc902b32775437bdf5231e782f9313
python3 tools/check_conventions.py --base 086c0686c8adff3ee648fdbe34e0b0b647e1d511
ctest --test-dir build -j8 --test-load 32 --output-on-failure -R "^guest_sce_v2_exceptions$|^public_headers$"
# negative control
git show 086c0686c8adff3ee648fdbe34e0b0b647e1d511:core/libs/prx/libc/include/Api.hpp > core/libs/prx/libc/include/Api.hpp
git show 086c0686c8adff3ee648fdbe34e0b0b647e1d511:core/libs/prx/libc/src/exception/StandardExceptions.cpp > core/libs/prx/libc/src/exception/StandardExceptions.cpp
ninja -C build -j32 -l32 guest_sce_v2_exceptions_tests
# mutation: versioned future_error::what() reads code.value instead of the first word
ninja -C build -j32 -l32 guest_sce_v2_exceptions_tests
build/tests/guest_sce_v2_exceptions_tests
# restore merged files, rebuild, rerun
ninja -C build -j32 -l32 guest_sce_v2_exceptions_tests
ctest --test-dir build --output-on-failure -R "^guest_sce_v2_exceptions$"
git status --porcelain

Results

Check Result
git diff --check clean
check_conventions.py All conventions checks passed
Configure / build / libs (script) ok
Selected tests (script): 3729 run 0 new failures, 0 timeouts; the 9 known failures also fail on main
check_skips.py (script) guest_net_ctl skipped, which also happens on main; not caused by this PR
guest_sce_v2_exceptions passed (0.01 s)
public_headers passed (62 s on this shared VM; it is not a new test, and it also runs long on the AMD CI)
Vulkan validation not applicable (no AGC driver, shader or VideoOut change)

Negative control

  • Production files reverted: I restored Api.hpp and StandardExceptions.cpp from main. The test no longer compiles because the four throwers are not declared, so the test does depend on the new exports.
  • Targeted mutation: I changed the versioned FutureErrorWhat_nid_no_patch to read code.value, the second word, which is what the unversioned future_error reads. The build succeeded and the test failed:
output
  _Throw(future_error): got 'NSt8__sce_v212future_errorE|(null)', want 'NSt8__sce_v212future_errorE|promise already satisfied'

After that I restored the merged files and rebuilt; the test passed again, and git status --porcelain was empty.

Findings

Blocking

None.

Non-blocking

  1. Unclear read of the first code word. At core/libs/prx/libc/src/exception/StandardExceptions.cpp:586, *reinterpret_cast<const int*>(&...->code) reads ErrorCode::reserved, the first member of a standard-layout struct (Api.hpp:99). The extra cast hides that. Writing reinterpret_cast<const SystemErrorObject*>(self)->code.reserved says the same thing directly, and the test already checks that field by name (inspectedSystem.code.reserved == 3).
  2. Possible layout mismatch with __sce_v2::_Throw_Cpp_error (pre-existing, outside this PR). The description reports that the module's versioned future_error::what() takes the error value from the first 32-bit word of the code. That matches libc++, where error_code is {int value; const error_category* category;}. On main, though, ThrowCppError fills the versioned system_error with {0, value, &GenericCategory}, putting the value in the second word (StandardExceptions.cpp:317). If the versioned error_code really keeps its value at offset 0, guest code calling code().value() on a __sce_v2::system_error from _Throw_Cpp_error would read 0. I did not check this against the module. A separate check and a debt note would help. It does not block this PR, which copies the code unchanged.

Not verified

  • Module data: I had no access to the PPSA03671 libc.prx. The type names, base classes, what() texts, object sizes and the first-word read are taken from the description and the debt entry.
  • Platforms: Windows/MinGW, macOS and console execution.
  • Other tests: the related tests named in the description, other than guest_sce_v2_exceptions and public_headers, beyond the script's selected run.
<!-- greyfinch-sync:boykopovar/AnyPS5:review:5758961728 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/15552#pullrequestreview-5758961728). Originally posted by [XairRrR](https://github.com/XairRrR) on 2026-11-03T20:23:27Z. **Review:** APPROVED --- # Reviewed — approved The four new throwers (`std::_Throw(const bad_cast&)` and the `std::__sce_v2` `_Throw` for `bad_any_cast`, `format_error` and `future_error`) merge cleanly, build, and pass the extended `guest_sce_v2_exceptions`. The negative control and a targeted mutation both make that test fail. I have two non-blocking notes. ## What I ran Head `e92455a5f4bc902b32775437bdf5231e782f9313`, `main` `086c0686c8adff3ee648fdbe34e0b0b647e1d511`, merged tree `f3b27126c6910ae6eaa4b22b96aad52e054af9c3`. Debian 13 VM, GCC 14.2.0, Release, Ninja, `BUILD_TESTING=ON`. ``` git diff --check 086c0686c8adff3ee648fdbe34e0b0b647e1d511...e92455a5f4bc902b32775437bdf5231e782f9313 python3 tools/check_conventions.py --base 086c0686c8adff3ee648fdbe34e0b0b647e1d511 ctest --test-dir build -j8 --test-load 32 --output-on-failure -R "^guest_sce_v2_exceptions$|^public_headers$" # negative control git show 086c0686c8adff3ee648fdbe34e0b0b647e1d511:core/libs/prx/libc/include/Api.hpp > core/libs/prx/libc/include/Api.hpp git show 086c0686c8adff3ee648fdbe34e0b0b647e1d511:core/libs/prx/libc/src/exception/StandardExceptions.cpp > core/libs/prx/libc/src/exception/StandardExceptions.cpp ninja -C build -j32 -l32 guest_sce_v2_exceptions_tests # mutation: versioned future_error::what() reads code.value instead of the first word ninja -C build -j32 -l32 guest_sce_v2_exceptions_tests build/tests/guest_sce_v2_exceptions_tests # restore merged files, rebuild, rerun ninja -C build -j32 -l32 guest_sce_v2_exceptions_tests ctest --test-dir build --output-on-failure -R "^guest_sce_v2_exceptions$" git status --porcelain ``` ## Results | Check | Result | |---|---| | `git diff --check` | clean | | `check_conventions.py` | All conventions checks passed | | Configure / build / `libs` (script) | ok | | Selected tests (script): 3729 run | 0 new failures, 0 timeouts; the 9 known failures also fail on `main` | | `check_skips.py` (script) | `guest_net_ctl` skipped, which also happens on `main`; not caused by this PR | | `guest_sce_v2_exceptions` | passed (0.01 s) | | `public_headers` | passed (62 s on this shared VM; it is not a new test, and it also runs long on the AMD CI) | | Vulkan validation | not applicable (no AGC driver, shader or VideoOut change) | ## Negative control - **Production files reverted:** I restored `Api.hpp` and `StandardExceptions.cpp` from `main`. The test no longer compiles because the four throwers are not declared, so the test does depend on the new exports. - **Targeted mutation:** I changed the versioned `FutureErrorWhat_nid_no_patch` to read `code.value`, the second word, which is what the unversioned `future_error` reads. The build succeeded and the test failed: <details><summary>output</summary> ``` _Throw(future_error): got 'NSt8__sce_v212future_errorE|(null)', want 'NSt8__sce_v212future_errorE|promise already satisfied' ``` </details> After that I restored the merged files and rebuilt; the test passed again, and `git status --porcelain` was empty. ## Findings ### Blocking None. ### Non-blocking 1. **Unclear read of the first code word.** At `core/libs/prx/libc/src/exception/StandardExceptions.cpp:586`, `*reinterpret_cast<const int*>(&...->code)` reads `ErrorCode::reserved`, the first member of a standard-layout struct ([Api.hpp:99](https://github.com/boykopovar/AnyPS5/blob/e92455a5f4bc902b32775437bdf5231e782f9313/core/libs/prx/libc/include/Api.hpp#L99-L101)). The extra cast hides that. Writing `reinterpret_cast<const SystemErrorObject*>(self)->code.reserved` says the same thing directly, and the test already checks that field by name (`inspectedSystem.code.reserved == 3`). 2. **Possible layout mismatch with `__sce_v2::_Throw_Cpp_error` (pre-existing, outside this PR).** The description reports that the module's versioned `future_error::what()` takes the error value from the *first* 32-bit word of the code. That matches libc++, where `error_code` is `{int value; const error_category* category;}`. On `main`, though, `ThrowCppError` fills the versioned `system_error` with `{0, value, &GenericCategory}`, putting the value in the *second* word ([StandardExceptions.cpp:317](https://github.com/boykopovar/AnyPS5/blob/e92455a5f4bc902b32775437bdf5231e782f9313/core/libs/prx/libc/src/exception/StandardExceptions.cpp#L314-L318)). If the versioned `error_code` really keeps its value at offset 0, guest code calling `code().value()` on a `__sce_v2::system_error` from `_Throw_Cpp_error` would read 0. I did not check this against the module. A separate check and a debt note would help. It does not block this PR, which copies the code unchanged. ## Not verified - **Module data:** I had no access to the PPSA03671 `libc.prx`. The type names, base classes, `what()` texts, object sizes and the first-word read are taken from the description and the debt entry. - **Platforms:** Windows/MinGW, macOS and console execution. - **Other tests:** the related tests named in the description, other than `guest_sce_v2_exceptions` and `public_headers`, beyond the script's selected run. <!-- anyps5-bots job=3535 head=e92455a5f4bc902b32775437bdf5231e782f9313 -->
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!15552
No description provided.