feat(libc): implement the std::__sce_v2 codecvt virtuals for wchar_t #16975

Open
Danigamer05 wants to merge 28 commits from upstream-pr/16975 into main
Danigamer05 commented 2026-11-08 03:18:37 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Danigamer05 on 2026-11-08T03:18:37Z.


What

Builds on #16949: this branch contains its commits, and through them #16944, #16924, #16919, #16560, #16516, #16214, #16178 and #15923, with current main. It needs none of their code, only their place in LocaleSupport.cpp, so it stacks to avoid conflicts. Until they merge, only the last commit is this PR's change.

Implement the seven virtuals of std::__sce_v2::codecvt<wchar_t, char, mbstate_t>, which _Makewloc builds and every wide stream uses to convert between wchar_t and multibyte text. Before this PR, each of them threw NotImplemented_nid_no_patch. These slots are not exports, so this PR adds no NID.

The behavior comes from the libc.prx bundled with PPSA03671 v01.001.005 (J03848509, SHA-256 483961e086b8a1fffa8138b3c0badf798987fb5f35ec908d828946408be2d8a6), vtable 0x18D3D0:

  • do_always_noconv is false. do_max_length returns the first word of the facet's conversion data, and do_encoding is 1 when that word is 1 and 0 otherwise.
  • do_in converts with mbrtowc from a clean state of its own and leaves the given state untouched. A null byte counts as one byte. It returns partial when the output is full or a sequence is incomplete, and error on an invalid sequence.
  • do_out converts with wcrtomb on the given state, directly into the output while at least 6 bytes are left. Closer to the end it converts into a buffer and, when the sequence does not fit, restores the state and stops. It returns partial while input is left.
  • do_unshift converts a null character and writes the bytes before the terminator. When they do not fit it restores the state and returns partial.
  • do_length converts at most the given count of characters on the given state, counts a null byte as one, stops at an invalid or incomplete sequence, and returns the bytes read capped at INT_MAX.

The module calls its _Mbrtowc and _Wcrtomb, which wrap mbrtowc and wcrtomb. This library has no such exports, so it calls mbrtowc and wcrtomb as its wide ctype already does. A new debt entry records that, and the _Makeloc entry lists the facet among the working ones.

Tested

Windows 11, MinGW GCC 15.2, Release, head on top of #16949. Not run on Linux or macOS, not run in a title.

  • New test guest_sce_v2_codecvt_wide: every slot; two- and three-byte sequences; incomplete and invalid input; full output; null bytes; exact and short output room; and a state holding a pending sequence, which do_in ignores and do_length continues. It passes, as do guest_sce_v2_num_get_wide, guest_sce_v2_num_get_char, guest_sce_v2_num_put_char, guest_sce_v2_num_put_wide, guest_sce_v2_char_facet_virtuals, guest_sce_v2_makeloc, guest_sce_v2_locale, guest_sce_v2_locale_basics, guest_sce_v2_locimp_references, guest_sce_v2_facet_ids, guest_locale, guest_sce_v2_exceptions, guest_sce_v2_string_to_integer and public_headers (15 of 15).
  • Differential harness against the module's own code, loaded and relocated, with the facet from each side's _Makeloc. Inputs: random multibyte text with ASCII, null bytes, valid, truncated, overlong and surrogate sequences; random 16-bit text with surrogates; output rooms from 0 to 63; counts up to 2^64; and initial states that are clean, have one random byte, or are random. It compares the result, both next pointers, the output buffers with two bytes past their end, the state's 32 bytes and errno.
    • With the module's mbrtowc and wcrtomb redirected to this library's: 400,000 calls over 2 seeds, 0 differences.
    • With the module's own: 120 of 200,000 calls differ, all in do_length from a nonzero initial state, where the two libraries' mbrtowc read a state they did not write differently.
    • Chained calls, which carry one state through up to 24 calls of do_in, do_out, do_unshift and do_length with output rooms of 0 to 6, as a stream converting piece by piece does: 300,000 chains over 3 more seeds with the redirected functions and 20,000 with the module's own, 0 differences.
    • 1,500,000 more single calls over those 3 seeds, 0 differences.
  • Regression on the harnesses of #16560 and #16949: 20,000 calls each, 0 differences.
  • The whole ctest suite built from this head, without the tests that drive the GPU, audio or controllers: every libc test passes. The only failures are select_tests_MissingCommandTests, which cannot create a symbolic link without Windows' privilege, and vulkan_validation_gate_commands, a script test this PR does not touch.
  • Negative control: 15 single-change mutations. The harness catches 11 and the final test 10. The first test run missed 2 of the state cases, so the test was strengthened and those were rerun; the harness alone still catches the do_unshift terminator. The other 4 make no difference with this library's conversions: do_max_length is always 1, wcrtomb leaves the state unchanged so restoring it has no effect, no 16-bit character needs more than 3 bytes, and do_unshift from an initial state never runs out of room.
  • check_conventions, git diff --check: clean. Merges cleanly with main. pr_overlap reports no open PR with the same functions or conflicting code; it could not compare 37 PRs that conflict with main on shared files, and of the 4 that touch LocaleSupport.cpp (#9526, #11172, #12518, #12704) none adds these virtuals. #11172 wires the exported vtable of the unversioned std::codecvt<wchar_t, char, _Mbstatet>, a different facet from this one.

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: #16949
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5208228291 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/16975). Originally posted by [Danigamer05](https://github.com/Danigamer05) on 2026-11-08T03:18:37Z. --- ### What Builds on #16949: this branch contains its commits, and through them #16944, #16924, #16919, #16560, #16516, #16214, #16178 and #15923, with current `main`. It needs none of their code, only their place in `LocaleSupport.cpp`, so it stacks to avoid conflicts. Until they merge, only the last commit is this PR's change. Implement the seven virtuals of `std::__sce_v2::codecvt<wchar_t, char, mbstate_t>`, which `_Makewloc` builds and every wide stream uses to convert between `wchar_t` and multibyte text. Before this PR, each of them threw `NotImplemented_nid_no_patch`. These slots are not exports, so this PR adds no NID. The behavior comes from the `libc.prx` bundled with PPSA03671 v01.001.005 (J03848509, SHA-256 `483961e086b8a1fffa8138b3c0badf798987fb5f35ec908d828946408be2d8a6`), vtable `0x18D3D0`: - `do_always_noconv` is false. `do_max_length` returns the first word of the facet's conversion data, and `do_encoding` is 1 when that word is 1 and 0 otherwise. - `do_in` converts with `mbrtowc` from a clean state of its own and leaves the given state untouched. A null byte counts as one byte. It returns `partial` when the output is full or a sequence is incomplete, and `error` on an invalid sequence. - `do_out` converts with `wcrtomb` on the given state, directly into the output while at least 6 bytes are left. Closer to the end it converts into a buffer and, when the sequence does not fit, restores the state and stops. It returns `partial` while input is left. - `do_unshift` converts a null character and writes the bytes before the terminator. When they do not fit it restores the state and returns `partial`. - `do_length` converts at most the given count of characters on the given state, counts a null byte as one, stops at an invalid or incomplete sequence, and returns the bytes read capped at `INT_MAX`. The module calls its `_Mbrtowc` and `_Wcrtomb`, which wrap `mbrtowc` and `wcrtomb`. This library has no such exports, so it calls `mbrtowc` and `wcrtomb` as its wide `ctype` already does. A new debt entry records that, and the `_Makeloc` entry lists the facet among the working ones. ### 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 11, MinGW GCC 15.2, Release, head on top of #16949. Not run on Linux or macOS, not run in a title. - New test `guest_sce_v2_codecvt_wide`: every slot; two- and three-byte sequences; incomplete and invalid input; full output; null bytes; exact and short output room; and a state holding a pending sequence, which `do_in` ignores and `do_length` continues. It passes, as do `guest_sce_v2_num_get_wide`, `guest_sce_v2_num_get_char`, `guest_sce_v2_num_put_char`, `guest_sce_v2_num_put_wide`, `guest_sce_v2_char_facet_virtuals`, `guest_sce_v2_makeloc`, `guest_sce_v2_locale`, `guest_sce_v2_locale_basics`, `guest_sce_v2_locimp_references`, `guest_sce_v2_facet_ids`, `guest_locale`, `guest_sce_v2_exceptions`, `guest_sce_v2_string_to_integer` and `public_headers` (15 of 15). - Differential harness against the module's own code, loaded and relocated, with the facet from each side's `_Makeloc`. Inputs: random multibyte text with ASCII, null bytes, valid, truncated, overlong and surrogate sequences; random 16-bit text with surrogates; output rooms from 0 to 63; counts up to 2^64; and initial states that are clean, have one random byte, or are random. It compares the result, both next pointers, the output buffers with two bytes past their end, the state's 32 bytes and `errno`. - With the module's `mbrtowc` and `wcrtomb` redirected to this library's: 400,000 calls over 2 seeds, 0 differences. - With the module's own: 120 of 200,000 calls differ, all in `do_length` from a nonzero initial state, where the two libraries' `mbrtowc` read a state they did not write differently. - Chained calls, which carry one state through up to 24 calls of `do_in`, `do_out`, `do_unshift` and `do_length` with output rooms of 0 to 6, as a stream converting piece by piece does: 300,000 chains over 3 more seeds with the redirected functions and 20,000 with the module's own, 0 differences. - 1,500,000 more single calls over those 3 seeds, 0 differences. - Regression on the harnesses of #16560 and #16949: 20,000 calls each, 0 differences. - The whole ctest suite built from this head, without the tests that drive the GPU, audio or controllers: every libc test passes. The only failures are `select_tests_MissingCommandTests`, which cannot create a symbolic link without Windows' privilege, and `vulkan_validation_gate_commands`, a script test this PR does not touch. - Negative control: 15 single-change mutations. The harness catches 11 and the final test 10. The first test run missed 2 of the state cases, so the test was strengthened and those were rerun; the harness alone still catches the `do_unshift` terminator. The other 4 make no difference with this library's conversions: `do_max_length` is always 1, `wcrtomb` leaves the state unchanged so restoring it has no effect, no 16-bit character needs more than 3 bytes, and `do_unshift` from an initial state never runs out of room. - `check_conventions`, `git diff --check`: clean. Merges cleanly with `main`. `pr_overlap` reports no open PR with the same functions or conflicting code; it could not compare 37 PRs that conflict with `main` on shared files, and of the 4 that touch `LocaleSupport.cpp` (#9526, #11172, #12518, #12704) none adds these virtuals. #11172 wires the exported vtable of the unversioned `std::codecvt<wchar_t, char, _Mbstatet>`, a different facet from this one. ### 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: #16949 - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes
MarcZX10 commented 2026-11-21 07:18:51 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-21T07:18:51Z.

Review: COMMENTED


Reviewed — no blocking findings

The codecvt implementation and its focused test were inspected at head dd05a6107ba07214a6e84ed6f16e68b7533c7839; the review is incomplete, so a formal approval is not justified.

What I ran

No commands or tests were run. I read the prepared description, commit list, diff, manifest, GuestLocale.hpp, LocaleSupport.cpp around the new codecvt implementation, and GuestSceV2CodecvtWide.cpp.

Results

Check Result Note
Source inspection passed Codecvt implementation and focused test read
Tests not run Author-reported results were not independently verified
Full review not run Only 3 of 9 changed source files were read

Findings

Blocking

None verified in the inspected codecvt implementation.

Non-blocking

None.

Not verified

The remaining six changed files were not read: CMakeLists.txt, the other five test files, and docs/dev/debt/libc.md. The submitted Windows test, differential harness, full suite, negative-control mutations, and console behavior were not independently run or confirmed. The prepared description reports that Linux, macOS, and title execution were not tested.

To get this to a good state

Nothing is required. Optional: complete review of the six unread changed files and independently run the relevant checks.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:6003765869 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/16975#pullrequestreview-6003765869). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-11-21T07:18:51Z. **Review:** COMMENTED --- # Reviewed — no blocking findings The codecvt implementation and its focused test were inspected at head `dd05a6107ba07214a6e84ed6f16e68b7533c7839`; the review is incomplete, so a formal approval is not justified. ## What I ran No commands or tests were run. I read the prepared description, commit list, diff, manifest, `GuestLocale.hpp`, `LocaleSupport.cpp` around the new codecvt implementation, and `GuestSceV2CodecvtWide.cpp`. ## Results | Check | Result | Note | |---|---|---| | Source inspection | passed | Codecvt implementation and focused test read | | Tests | not run | Author-reported results were not independently verified | | Full review | not run | Only 3 of 9 changed source files were read | ## Findings ### Blocking None verified in the inspected codecvt implementation. ### Non-blocking None. ## Not verified The remaining six changed files were not read: `CMakeLists.txt`, the other five test files, and `docs/dev/debt/libc.md`. The submitted Windows test, differential harness, full suite, negative-control mutations, and console behavior were not independently run or confirmed. The prepared description reports that Linux, macOS, and title execution were not tested. ## To get this to a good state Nothing is required. Optional: complete review of the six unread changed files and independently run the relevant checks.
ligmaSec commented 2026-12-03 13:12:38 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by ligmaSec on 2026-12-03T13:12:38Z.

Review: COMMENTED


Built the branch’s locale implementation and matching headers against the existing Linux runtime. The focused GuestSceV2CodecvtWide fixture passed.

AI-assisted: yes.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:6170471580 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/16975#pullrequestreview-6170471580). Originally posted by [ligmaSec](https://github.com/ligmaSec) on 2026-12-03T13:12:38Z. **Review:** COMMENTED --- Built the branch’s locale implementation and matching headers against the existing Linux runtime. The focused `GuestSceV2CodecvtWide` fixture passed. AI-assisted: yes.
This pull request cannot be merged automatically due to conflicts.
Merge manually to resolve the conflicts.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin 'upstream-pr/16975:upstream-pr/16975'
git switch 'upstream-pr/16975'

Merge

Merge the changes and update on Forgejo.

Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.

git switch 'main'
git merge --no-ff 'upstream-pr/16975'
git switch 'upstream-pr/16975'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/16975'
git switch 'upstream-pr/16975'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/16975'
git switch 'main'
git merge --squash 'upstream-pr/16975'
git switch 'main'
git merge --ff-only 'upstream-pr/16975'
git switch 'main'
git merge 'upstream-pr/16975'
git push origin 'main'
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!16975
No description provided.