fix(libc): store the std::__sce_v2 _Throw_Cpp_error value in the first code word #15667

Merged
Danigamer05 merged 1 commit from upstream-pr/15667 into main 2026-11-06 22:27:25 +00:00
Danigamer05 commented 2026-11-04 01:31:55 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Danigamer05 on 2026-11-04T01:31:55Z.


What

std::__sce_v2::_Throw_Cpp_error (NID ShHKOkWgZRM) stored the error value in the second 32-bit word of the thrown system_error's code. The versioned type keeps it in the first word. XairRrR raised this in the review of #15552, and the libc.prx bundled with PPSA03671 v01.001.005 (J03848509, SHA-256 483961e086b8a1fffa8138b3c0badf798987fb5f35ec908d828946408be2d8a6) confirms it:

  • The versioned _Throw_Cpp_error at 0x10AB60 builds the object through the _System_error constructor at 0xCD5B0, which stores the value with mov DWORD PTR [r14+0x10],r15d and the category with mov QWORD PTR [r14+0x18],rbx (0xCD72D, 0xCD731).
  • The versioned future_error::what() reads the same first word.
  • The unversioned _Throw_Cpp_error at 0xB95A0 passes the code as value << 32, so the unversioned layout keeps the value in the second word, as main already does for it.

ThrowCppError now takes which word to use. The versioned export asks for the first word, with the second word 0 (the module leaves stack contents there). The unversioned export and ThrowCppError_nid_no_patch are unchanged. With the old layout, guest code reading code().value() on a std::__sce_v2::system_error from _Throw_Cpp_error got 0.

Tested

Windows x86-64, WinLibs GCC 15.2.0, Release, BUILD_TESTING=ON, main 1c8c0efc8c4. Exact tested commit: 70fdc24e8cb.

  • Built libc, libkernel, their NID-patched production libraries and the related tests. guest_sce_v2_exceptions and guest_filesystem_error pass.
  • guest_sce_v2_exceptions now throws _Throw_Cpp_error(1) through its catch frame and checks the copied code: the first word is 22 (EINVAL), the second word is 0, and the category is generic_category(). With main's StandardExceptions.cpp the same test fails on the first two checks, and with this change it passes.
  • The layout was read from the module's code at the addresses above; the thrower was not run side by side, because it ends in __cxa_throw.
  • Code conventions and git diff --check pass.
  • tools/pr_overlap.py read 1,827 open PRs and reports no overlap. It keeps exit 2 for 29 branches that already conflict with main. None of them names _Throw_Cpp_error or ThrowCppError. Merging each with this branch and with main using git merge-tree gives the same conflict blocks. The branch also merges cleanly with #15336, #15471, #15484, #15490, #15501, #15519, #15552, #15561, #15602 and #15614.

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:5147292824 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/15667). Originally posted by [Danigamer05](https://github.com/Danigamer05) on 2026-11-04T01:31:55Z. --- ### What `std::__sce_v2::_Throw_Cpp_error` (NID `ShHKOkWgZRM`) stored the error value in the second 32-bit word of the thrown `system_error`'s code. The versioned type keeps it in the first word. XairRrR raised this in the review of #15552, and the `libc.prx` bundled with PPSA03671 v01.001.005 (J03848509, SHA-256 `483961e086b8a1fffa8138b3c0badf798987fb5f35ec908d828946408be2d8a6`) confirms it: - The versioned `_Throw_Cpp_error` at `0x10AB60` builds the object through the `_System_error` constructor at `0xCD5B0`, which stores the value with `mov DWORD PTR [r14+0x10],r15d` and the category with `mov QWORD PTR [r14+0x18],rbx` (`0xCD72D`, `0xCD731`). - The versioned `future_error::what()` reads the same first word. - The unversioned `_Throw_Cpp_error` at `0xB95A0` passes the code as `value << 32`, so the unversioned layout keeps the value in the second word, as main already does for it. `ThrowCppError` now takes which word to use. The versioned export asks for the first word, with the second word 0 (the module leaves stack contents there). The unversioned export and `ThrowCppError_nid_no_patch` are unchanged. With the old layout, guest code reading `code().value()` on a `std::__sce_v2::system_error` from `_Throw_Cpp_error` got 0. ### 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 `1c8c0efc8c4`. Exact tested commit: `70fdc24e8cb`. - Built libc, libkernel, their NID-patched production libraries and the related tests. `guest_sce_v2_exceptions` and `guest_filesystem_error` pass. - `guest_sce_v2_exceptions` now throws `_Throw_Cpp_error(1)` through its catch frame and checks the copied code: the first word is 22 (`EINVAL`), the second word is 0, and the category is `generic_category()`. With main's `StandardExceptions.cpp` the same test fails on the first two checks, and with this change it passes. - The layout was read from the module's code at the addresses above; the thrower was not run side by side, because it ends in `__cxa_throw`. - Code conventions and `git diff --check` pass. - `tools/pr_overlap.py` read 1,827 open PRs and reports no overlap. It keeps exit 2 for 29 branches that already conflict with main. None of them names `_Throw_Cpp_error` or `ThrowCppError`. Merging each with this branch and with main using `git merge-tree` gives the same conflict blocks. The branch also merges cleanly with #15336, #15471, #15484, #15490, #15501, #15519, #15552, #15561, #15602 and #15614. 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)
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!15667
No description provided.