Skip to content

fix(libc): count wide collate string elements - #6517

Merged
oneandonlydean merged 1 commit into
boykopovar:mainfrom
wildbadgerdev:fix/libc-wide-collate-layout
Oct 17, 2026
Merged

oneandonlydean merged 1 commit into
boykopovar:mainfrom
wildbadgerdev:fix/libc-wide-collate-layout

Conversation

@wildbadgerdev

@wildbadgerdev wildbadgerdev commented Oct 15, 2026 •

Copy link
Copy Markdown
Contributor

What

Fix the string result returned by the existing collate<wchar_t> transform. For u"abc", main writes size=6, capacity=14 into inline storage. Under the Dinkumware string layout already modeled by GuestLocale::String, a 16-bit string stores element counts: its inline capacity is 7, and capacity 14 selects heap storage, interpreting those inline characters as a pointer.

Record size/capacity in 16-bit elements and allocate (capacity + 1) * 2 bytes for heap results. Keep the existing allocation/alignment helper. This follows VC12 xstring at a fixed revision and addresses the string-layout question raised in the review of #5662. The implementation of _Getcat from that merged PR is retained.

The existing tests now expect element counts and the correct allocation size at 7/8 characters. Added 2039/2040-character cases check the transition to the existing 4096-byte aligned allocation, the stored base pointer, contents and terminator. Hash, comparison and embedded-NUL behavior remain covered by the existing tests.

Tested

  • Linux x64, Ubuntu 24.04 under WSL2, GCC 13.3, Release, bundled dependencies, BUILD_TESTING=ON: full build passed, including the incremental build after rebasing to the base below. guest_collate and nine neighbouring locale/wide-string tests passed three times each (30 runs, no skips).
  • Windows x64, WinLibs MinGW-w64 GCC 15.2.0 r7 UCRT/posix/seh, Release: libs and the same ten test targets built; all ten CTests passed three times each (30 runs, no skips).
  • Negative control on Linux: relinked a separate copy of libc.prx using the base branch's LocaleSupport.cpp and the same remaining objects. This PR's guest_collate aborts against that copy and passes against the corrected library. No production source or build artifact was replaced for this check.
  • Windows full-build limitation: the full default build stops in the unchanged GpuIndirectArgumentsMisalignedWriterAhead.cpp:25: MinGW rejects alignas(65536) because the object format's maximum is 8192. Compiling the base branch's identical file separately reproduces the same error. The library build and relevant tests above pass.
  • python tools/check_conventions.py --base origin/main and git diff --check pass. Scanned the filenames of 608 open PRs, including pagination for PRs with over 100 files: none changes this wide-transform implementation. feat(libc): export locale::classic (PPSA34015) #6087 adds locale::classic in another region of the same source file.

Based on main 03c4cb5e7; the three changed files are unchanged since the original full-build base fa9365ca7. No console or title run; the wide string ABI is derived from the existing Dinkumware model and the source above. Full upstream Linux/Windows CI requires a maintainer's manual dispatch.

Checklist

  • Based on current main; no other open PR implements the same correction
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt
  • Unimplemented paths throw; no stubs added or changed
  • No notes, investigation files or images added to the repository
  • No new third-party code
  • Generic behaviour, not specific to one title
  • Depends on: none (feat(libc): implement collate<wchar_t>::_Getcat #5662 is merged)
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes

@github-actions

github-actions Bot commented Oct 15, 2026 •

Copy link
Copy Markdown

Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in Depends on.

Pull request Conflicting files (lines on main) Same exports Same new files
PR 6483 docs/dev/debt/libc.md L70-71
PR 6731 docs/dev/debt/libc.md L71-72
PR 6732 docs/dev/debt/libc.md L71-72

Some comparisons could not be completed because a pull request conflicts with the base branch. Review these shared files manually before concluding that there is no overlap.

Pull request Shared files not compared
PR 5021 docs/dev/debt/libc.md
PR 5303 docs/dev/debt/libc.md
PR 6044 core/libs/tests/GuestCollate.cpp
PR 6222 docs/dev/debt/libc.md
PR 6334 docs/dev/debt/libc.md
PR 6565 docs/dev/debt/libc.md
PR 6975 core/libs/prx/libc/src/LocaleSupport.cpp
docs/dev/debt/libc.md
PR 7081 docs/dev/debt/libc.md

@Dextroid17

Copy link
Copy Markdown
Contributor

I checked the size and capacity rules against the VC12 source you link. Your negative control relinked a separate copy of libc.prx; I ran the same check a different way, by reverting LocaleSupport.cpp in the tree, and also ran the test on a merge with main at 9bf01db66.

Setup: head 2b567cda3 (merge-base with main 03c4cb5e7), Release, BUILD_TESTING=ON, Ninja, CachyOS (kernel 7.2.9), i9-11900K, GCC 16.2.1. The checks ran under env -i with only HOME and PATH set. Each ran ctest -R '^guest_collate$' 3 times.

  • Head: passes in each of the 3 runs.
  • Fix reverted: with LocaleSupport.cpp as on 03c4cb5e7 and your test kept, guest_collate aborts 3 of 3. The first failure is GuestCollate.cpp:150, the result.size == 3 && result.capacity == 7 check for u"abc". That is the first size check on the result, which fits your note that main writes size 6 and capacity 14 there. (I found the line by having require print std::source_location::current().line() before aborting. That edit adds two lines at the top, so its line 152 is your line 150. With the fix, the same edited test passes.)
  • Merged with main at 9bf01db66: clean merge, passes in each of the 3 runs. tools/check_conventions.py passes.

Source check (xstring at your pinned revision): _BUF_SIZE is 16 / sizeof(value_type), so 8 slots (7 characters plus the terminator) for a 2-byte type. _ALLOC_MASK is 7 for that size. _Mysize and _Myres count elements, not bytes. _Copy (L2200-L2231) computes the capacity as _Newsize | _ALLOC_MASK when the string starts in the inline buffer (_Myres 7, so the 1.5x growth branch is not taken), and allocates _Newres + 1 elements. These match your fix: GuestWideInlineCapacity is 7, the heap capacity is length | GuestWideInlineCapacity (the same 7), and the allocation is (capacity + 1) * 2 bytes. Your 7 and 8 character cases (capacity 7 inline, then 15 on the heap with a 32-byte allocation) are right at that boundary. The 2039/2040-character cases are not covered by this file: xstring has no 4096-byte threshold or aligned allocation, so I did not check that path against VC12.

Overlap: this PR merges cleanly with #6087, which also changes LocaleSupport.cpp (checked with git merge-tree only, no build or tests).

Not checked: Windows and macOS, and PS5 hardware. I compared against the VC12 source only, not a console libc.prx.

Written with AI assistance (Claude Code). I ran the builds and tests above on my machine.

AI-assisted: yes

@oneandonlydean
oneandonlydean merged commit 0f0d400 into boykopovar:main Oct 17, 2026
16 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants