feat(libc): implement the std::__sce_v2 ctype, numpunct and codecvt virtuals for char #16214

Open
Danigamer05 wants to merge 8 commits from upstream-pr/16214 into main
Danigamer05 commented 2026-11-05 20:53:25 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Danigamer05 on 2026-11-05T20:53:25Z.


What

Builds on #16178: this branch contains its commits, and through it #16165, #16113 and #15923. Until they merge, only the last commit is this PR's change.

Implement the virtuals of the versioned std::__sce_v2 ctype<char>, numpunct<char> and codecvt<char> facets that _Makeloc builds. Before this PR, each of them threw NotImplemented_nid_no_patch. These slots are not exports: guest code reaches them through the facet's vtable, so this PR adds no NIDs.

The behavior comes from the libc.prx bundled with PPSA03671 v01.001.005 (J03848509, SHA-256 483961e086b8a1fffa8138b3c0badf798987fb5f35ec908d828946408be2d8a6). Vtable offsets are from the start of the module's vtable:

Facet Vtable Offset Virtual Behavior
ctype<char> 0x18C520 0x20, 0x30 do_tolower, do_toupper (character) _Tolower or _Toupper of the unsigned byte
0x28, 0x38 do_tolower, do_toupper (range) the same in place; returns the end
0x40, 0x50 do_widen, do_narrow (character) returns the character; do_narrow ignores the fallback
0x48, 0x58 do_widen, do_narrow (range) copies the range; returns the end
numpunct<char> 0x18C690 0x20, 0x28 do_decimal_point, do_thousands_sep reads the facet's field
0x30, 0x38, 0x40 do_grouping, do_falsename, do_truename builds a versioned string from the field
codecvt<char> 0x18C788 0x20 do_always_noconv true
0x28, 0x30 do_max_length, do_encoding 1
0x38, 0x40 do_in, do_out consumes nothing, returns noconv (3)
0x48 do_unshift writes next, returns noconv (3)
0x50 do_length the distance capped at INT_MAX, then at the count, compared unsigned, so a reversed range returns the count

The versioned string is 0x20 bytes: a 16-byte inline buffer or pointer, the size and the capacity. The string slots zero it first, keep up to 15 characters inline, and otherwise allocate (length | 15) + 1 bytes through the existing GuestLocale::AllocateStringStorage, which adds the module's aligned header from 0x1000 bytes up.

The unversioned do_unshift and do_length differ from these: do_unshift returns 0 and do_length has no cap. They are unchanged, so the versioned facet has its own slots.

The other virtuals still throw, so stream output through a locale built here still stops at num_put, and the classic _Locimp stays without facets. The debt entry for _Makeloc now names the three facets whose virtuals work.

Tested

Windows x86-64, WinLibs GCC 15.2.0, Release, BUILD_TESTING=ON, on top of #16178 (5a8306b705d). The branch merges cleanly with main 151a2cb6e27. Exact tested commit: 37825601e62.

  • Built libc, libkernel, their NID-patched production libraries and the related tests. The new guest_sce_v2_char_facet_virtuals passes, along with guest_sce_v2_makeloc, guest_sce_v2_locimp_references, guest_sce_v2_locale, guest_sce_v2_locale_basics, guest_sce_v2_facet_ids, guest_locale, guest_sce_v2_exceptions and public_headers.

  • The new test gets the facets from _Makeloc and calls each slot through the vtable. It checks:

    • every byte through the character slots, and that only ASCII letters change case;
    • the case ranges in place and the widen and narrow ranges into a buffer, with the byte after the range left alone;
    • that the punctuation slots follow the facet's fields;
    • each string slot at lengths around the inline limit and the 0x1000 allocation limit: size, capacity, text, and zeroed inline bytes;
    • the codecvt constants and noconv results, with buffers and state left alone, and do_length at the count, the range, the cap and a reversed range.
  • The original functions were loaded from the bundled module with their relocations applied. Their imports were bound by NID to this branch's libc and libkernel. The module's heap is not set up outside a console process, so its allocation calls were redirected to the host's.

  • Side by side with the patched production library, with facets built by each side's _Makeloc, over two seeds with 200,000 random steps each:

    • all 256 bytes through the character slots;
    • about 60,000 calls of the range slots with random bytes, comparing the returned end and every byte written;
    • about 60,000 calls of the string slots with random text of 0 to 0x2000 bytes, comparing size, capacity, text, inline bytes, large-block alignment and the sequence of allocation sizes;
    • about 20,000 calls each for the punctuation fields, do_in and do_out, do_unshift, and do_length with edge values around INT_MAX and reversed ranges.

    No comparison differed. #16178's side-by-side run also still passes with one seed.

  • The tests were also run once with the test heap filling each block with 0xDD before freeing it, to catch reads of freed memory on Windows. They passed. That change is not committed.

  • Negative control: six lines were altered one at a time:

    • do_tolower left its character unchanged;
    • the do_widen range returned its start;
    • do_truename read falsename;
    • the string capacity was not rounded up;
    • do_unshift returned 0;
    • do_length lost its cap.

    CTest failed on each, and the side-by-side run reported differences each time. The source was then restored byte for byte (SHA-256 checked) and rebuilt, and the test and side-by-side run passed again.

  • Code conventions and git diff --check pass.

  • tools/pr_overlap.py read 1,909 open PRs at the tested commit and reports no overlap. It keeps exit 2 for 31 branches that already conflict with main. Merging each of the 31 with this branch and with main using git merge-tree gives the same conflict blocks in the files this PR touches; the differences it lists for #10253, #11831 and #14212 are in other files and come from main moving since this branch's base. The debt edit adds no conflict over #16178 with any of the 142 open PRs that touch that file. Other open PRs add unversioned ctype, numpunct and codecvt names, in libc and libSceLibcInternal, and none adds these versioned slots. The branch also merges cleanly with all 23 open PRs of this author.

Not tested: Linux, macOS, another SDK, a game, GPU or console. The other facet virtuals still throw. 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: #16178
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5178555428 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/16214). Originally posted by [Danigamer05](https://github.com/Danigamer05) on 2026-11-05T20:53:25Z. --- ### What Builds on #16178: this branch contains its commits, and through it #16165, #16113 and #15923. Until they merge, only the last commit is this PR's change. Implement the virtuals of the versioned `std::__sce_v2` `ctype<char>`, `numpunct<char>` and `codecvt<char>` facets that `_Makeloc` builds. Before this PR, each of them threw `NotImplemented_nid_no_patch`. These slots are not exports: guest code reaches them through the facet's vtable, so this PR adds no NIDs. The behavior comes from the `libc.prx` bundled with PPSA03671 v01.001.005 (J03848509, SHA-256 `483961e086b8a1fffa8138b3c0badf798987fb5f35ec908d828946408be2d8a6`). Vtable offsets are from the start of the module's vtable: | Facet | Vtable | Offset | Virtual | Behavior | | --- | --- | --- | --- | --- | | `ctype<char>` | `0x18C520` | `0x20`, `0x30` | `do_tolower`, `do_toupper` (character) | `_Tolower` or `_Toupper` of the unsigned byte | | | | `0x28`, `0x38` | `do_tolower`, `do_toupper` (range) | the same in place; returns the end | | | | `0x40`, `0x50` | `do_widen`, `do_narrow` (character) | returns the character; `do_narrow` ignores the fallback | | | | `0x48`, `0x58` | `do_widen`, `do_narrow` (range) | copies the range; returns the end | | `numpunct<char>` | `0x18C690` | `0x20`, `0x28` | `do_decimal_point`, `do_thousands_sep` | reads the facet's field | | | | `0x30`, `0x38`, `0x40` | `do_grouping`, `do_falsename`, `do_truename` | builds a versioned string from the field | | `codecvt<char>` | `0x18C788` | `0x20` | `do_always_noconv` | `true` | | | | `0x28`, `0x30` | `do_max_length`, `do_encoding` | `1` | | | | `0x38`, `0x40` | `do_in`, `do_out` | consumes nothing, returns `noconv` (3) | | | | `0x48` | `do_unshift` | writes `next`, returns `noconv` (3) | | | | `0x50` | `do_length` | the distance capped at `INT_MAX`, then at the count, compared unsigned, so a reversed range returns the count | The versioned string is `0x20` bytes: a 16-byte inline buffer or pointer, the size and the capacity. The string slots zero it first, keep up to 15 characters inline, and otherwise allocate `(length | 15) + 1` bytes through the existing `GuestLocale::AllocateStringStorage`, which adds the module's aligned header from `0x1000` bytes up. The unversioned `do_unshift` and `do_length` differ from these: `do_unshift` returns 0 and `do_length` has no cap. They are unchanged, so the versioned facet has its own slots. The other virtuals still throw, so stream output through a locale built here still stops at `num_put`, and the classic `_Locimp` stays without facets. The debt entry for `_Makeloc` now names the three facets whose virtuals work. ### 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`, on top of #16178 (`5a8306b705d`). The branch merges cleanly with main `151a2cb6e27`. Exact tested commit: `37825601e62`. - Built libc, libkernel, their NID-patched production libraries and the related tests. The new `guest_sce_v2_char_facet_virtuals` passes, along with `guest_sce_v2_makeloc`, `guest_sce_v2_locimp_references`, `guest_sce_v2_locale`, `guest_sce_v2_locale_basics`, `guest_sce_v2_facet_ids`, `guest_locale`, `guest_sce_v2_exceptions` and `public_headers`. - The new test gets the facets from `_Makeloc` and calls each slot through the vtable. It checks: - every byte through the character slots, and that only ASCII letters change case; - the case ranges in place and the widen and narrow ranges into a buffer, with the byte after the range left alone; - that the punctuation slots follow the facet's fields; - each string slot at lengths around the inline limit and the `0x1000` allocation limit: size, capacity, text, and zeroed inline bytes; - the `codecvt` constants and `noconv` results, with buffers and state left alone, and `do_length` at the count, the range, the cap and a reversed range. - The original functions were loaded from the bundled module with their relocations applied. Their imports were bound by NID to this branch's libc and libkernel. The module's heap is not set up outside a console process, so its allocation calls were redirected to the host's. - Side by side with the patched production library, with facets built by each side's `_Makeloc`, over two seeds with 200,000 random steps each: - all 256 bytes through the character slots; - about 60,000 calls of the range slots with random bytes, comparing the returned end and every byte written; - about 60,000 calls of the string slots with random text of 0 to `0x2000` bytes, comparing size, capacity, text, inline bytes, large-block alignment and the sequence of allocation sizes; - about 20,000 calls each for the punctuation fields, `do_in` and `do_out`, `do_unshift`, and `do_length` with edge values around `INT_MAX` and reversed ranges. No comparison differed. #16178's side-by-side run also still passes with one seed. - The tests were also run once with the test heap filling each block with `0xDD` before freeing it, to catch reads of freed memory on Windows. They passed. That change is not committed. - Negative control: six lines were altered one at a time: - `do_tolower` left its character unchanged; - the `do_widen` range returned its start; - `do_truename` read `falsename`; - the string capacity was not rounded up; - `do_unshift` returned 0; - `do_length` lost its cap. CTest failed on each, and the side-by-side run reported differences each time. The source was then restored byte for byte (SHA-256 checked) and rebuilt, and the test and side-by-side run passed again. - Code conventions and `git diff --check` pass. - `tools/pr_overlap.py` read 1,909 open PRs at the tested commit and reports no overlap. It keeps exit 2 for 31 branches that already conflict with main. Merging each of the 31 with this branch and with main using `git merge-tree` gives the same conflict blocks in the files this PR touches; the differences it lists for #10253, #11831 and #14212 are in other files and come from main moving since this branch's base. The debt edit adds no conflict over #16178 with any of the 142 open PRs that touch that file. Other open PRs add unversioned `ctype`, `numpunct` and `codecvt` names, in `libc` and `libSceLibcInternal`, and none adds these versioned slots. The branch also merges cleanly with all 23 open PRs of this author. Not tested: Linux, macOS, another SDK, a game, GPU or console. The other facet virtuals still throw. 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: #16178 - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes
MarcZX10 commented 2026-11-20 11:08:06 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-20T11:08:06Z.

Review: APPROVED


Reviewed — looks good

The reviewed code implements the described versioned char facet virtuals; I found no concrete defect in the inspected changes.

What I ran

Read the prepared diff and changed files at head 9a765a75fc6cd489a6af4b6017176f4c78290427. I did not run build or tests; the checks in the PR description are author-reported.

Results

Check Result Note
Changed code review passed Four changed files read
Build and tests not run Only author-reported results available
Negative control not run Author reports controls; not independently run

Findings

Blocking

None.

Non-blocking

None.

Not verified

I read core/libs/prx/libc/CMakeLists.txt, core/libs/prx/libc/src/LocaleSupport.cpp, core/libs/tests/GuestSceV2CharFacetVirtuals.cpp, and docs/dev/debt/libc.md; all four changed files listed in the manifest were included and read. I did not independently verify the bundled module behavior, the reported test/build runs, or behavior on other platforms and consoles.

To get this to a good state

Nothing is required.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5991704830 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/16214#pullrequestreview-5991704830). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-11-20T11:08:06Z. **Review:** APPROVED --- # Reviewed — looks good The reviewed code implements the described versioned `char` facet virtuals; I found no concrete defect in the inspected changes. ## What I ran Read the prepared diff and changed files at head `9a765a75fc6cd489a6af4b6017176f4c78290427`. I did not run build or tests; the checks in the PR description are author-reported. ## Results | Check | Result | Note | |---|---|---| | Changed code review | passed | Four changed files read | | Build and tests | not run | Only author-reported results available | | Negative control | not run | Author reports controls; not independently run | ## Findings ### Blocking None. ### Non-blocking None. ## Not verified I read `core/libs/prx/libc/CMakeLists.txt`, `core/libs/prx/libc/src/LocaleSupport.cpp`, `core/libs/tests/GuestSceV2CharFacetVirtuals.cpp`, and `docs/dev/debt/libc.md`; all four changed files listed in the manifest were included and read. I did not independently verify the bundled module behavior, the reported test/build runs, or behavior on other platforms and consoles. ## To get this to a good state Nothing is required.
ligmaSec commented 2026-12-03 13:21:08 +00:00 (Migrated from github.com)

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

Review: COMMENTED


Built the branch locale implementation and matching headers against the existing Linux runtime. The focused character facet fixture passed.

AI-assisted: yes.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:6170594746 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/16214#pullrequestreview-6170594746). Originally posted by [ligmaSec](https://github.com/ligmaSec) on 2026-12-03T13:21:08Z. **Review:** COMMENTED --- Built the branch locale implementation and matching headers against the existing Linux runtime. The focused character facet 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/16214:upstream-pr/16214'
git switch 'upstream-pr/16214'

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