fix(libc): size collate<char>::do_transform strings as the console does #4733

Merged
fdfawef324 merged 1 commit from upstream-pr/4733 into main 2026-10-13 04:50:56 +00:00
fdfawef324 commented 2026-10-12 20:01:05 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by fdfawef324 on 2026-10-12T20:01:05Z.


What

collate<char>::do_transform gave the result std::string a capacity of exactly its length and threw for results of 4095 characters or more.

The console's libc.prx grows the string from its inline capacity of 15 to length | 15, so a 26-character result has capacity 31 and a 32-byte allocation. Once capacity + 1 reaches 0x1000 bytes it takes the over-aligned large allocation that the guest's inlined deallocation expects: operator new(bytes + 0x27), the data aligned to 32 bytes and the raw pointer stored just before it. This is the scheme of Dinkumware's basic_string growth (_Newsize | _ALLOC_MASK) and of its _Allocate in <xmemory0> (_BIG_ALLOCATION_THRESHOLD 4096, _BIG_ALLOCATION_ALIGNMENT 32, _NON_USER_SIZE = sizeof(void*) + 31 in release builds). As in Dinkumware, the large path throws bad_alloc if bytes + 0x27 would wrap.

do_transform now uses that capacity and allocation, and the throw for long results is removed. The collate<char> entry in docs/dev/debt/libc.md now says that the capacity and the large-allocation layout come from the console's libc.prx code and have not been checked on hardware.

This replaces fork branch fix/libc-collate-char-capacity, which was never opened as a PR. That branch was stacked on feat/libc-collate-wchar to share an allocation helper. This version is based directly on main and changes only the narrow facet, because #4409 implements collate<wchar_t>::_Getcat separately. Both PRs touch LocaleSupport.cpp but change different functions. Once this one is merged, the wide transform in #4409 could use AllocateStringStorage for results of 2047 code units or more, which #4409 currently rejects with a throw.

Tested

Windows Server 2025, RTX 3060, GCC 15.2 (WinLibs UCRT), Release -O3, based on main 1acfa7f78.

  • guest_collate checks the capacity and allocation size of a 26-character result, the last small result (4079 characters, a 4080-byte block) and the first large one (4080 characters, capacity 4095, a 4096 + 0x27-byte block with the data aligned to 32 and the raw pointer before it).
  • Red on main: with this test and main's LocaleSupport.cpp, guest_collate aborts (exit 0xc0000409) at the allocation-size check of the 26-character result: the allocation is 27 bytes, not 32. With the fix it passes (0.24 s).
  • All 82 libc tests registered by core/libs/prx/libc/CMakeLists.txt in this Windows build (55 executables) were run with ctest -j 4: 81 pass; log_macros fails ("a short line differs") the same way on unchanged main.
  • Not tested with a title.

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
  • Unblocks: none
  • AI-assisted: yes

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4834374718 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4733). Originally posted by [fdfawef324](https://github.com/fdfawef324) on 2026-10-12T20:01:05Z. --- ### What `collate<char>::do_transform` gave the result `std::string` a capacity of exactly its length and threw for results of 4095 characters or more. The console's `libc.prx` grows the string from its inline capacity of 15 to `length | 15`, so a 26-character result has capacity 31 and a 32-byte allocation. Once `capacity + 1` reaches 0x1000 bytes it takes the over-aligned large allocation that the guest's inlined deallocation expects: `operator new(bytes + 0x27)`, the data aligned to 32 bytes and the raw pointer stored just before it. This is the scheme of Dinkumware's `basic_string` growth (`_Newsize | _ALLOC_MASK`) and of its `_Allocate` in `<xmemory0>` (`_BIG_ALLOCATION_THRESHOLD` 4096, `_BIG_ALLOCATION_ALIGNMENT` 32, `_NON_USER_SIZE` = `sizeof(void*) + 31` in release builds). As in Dinkumware, the large path throws `bad_alloc` if `bytes + 0x27` would wrap. `do_transform` now uses that capacity and allocation, and the throw for long results is removed. The `collate<char>` entry in `docs/dev/debt/libc.md` now says that the capacity and the large-allocation layout come from the console's `libc.prx` code and have not been checked on hardware. This replaces fork branch `fix/libc-collate-char-capacity`, which was never opened as a PR. That branch was stacked on `feat/libc-collate-wchar` to share an allocation helper. This version is based directly on `main` and changes only the narrow facet, because #4409 implements `collate<wchar_t>::_Getcat` separately. Both PRs touch `LocaleSupport.cpp` but change different functions. Once this one is merged, the wide transform in #4409 could use `AllocateStringStorage` for results of 2047 code units or more, which #4409 currently rejects with a throw. ### Tested Windows Server 2025, RTX 3060, GCC 15.2 (WinLibs UCRT), Release `-O3`, based on `main` 1acfa7f78. - `guest_collate` checks the capacity and allocation size of a 26-character result, the last small result (4079 characters, a 4080-byte block) and the first large one (4080 characters, capacity 4095, a 4096 + 0x27-byte block with the data aligned to 32 and the raw pointer before it). - Red on main: with this test and `main`'s `LocaleSupport.cpp`, `guest_collate` aborts (exit 0xc0000409) at the allocation-size check of the 26-character result: the allocation is 27 bytes, not 32. With the fix it passes (0.24 s). - All 82 libc tests registered by `core/libs/prx/libc/CMakeLists.txt` in this Windows build (55 executables) were run with `ctest -j 4`: 81 pass; `log_macros` fails ("a short line differs") the same way on unchanged `main`. - Not tested with a title. ### 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] Unblocks: none - [x] AI-assisted: yes 🤖 Generated with [Claude Code](https://claude.com/claude-code)
xXPBSXx commented 2026-10-13 01:09:29 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by xXPBSXx on 2026-10-13T01:09:29Z.


Windows test: head 0ca047e60 merged onto current main 1c5e62878 (clean merge, no conflicts). Windows 10, MinGW GCC (WinLibs), Ninja Release, -j2: full build 0 errors; ctest with the filter guest_collate: guest_collate passed (1 test(s), 0 failed); python tools/check_conventions.py --base upstream/main passed. I did not run the new tests against main without the change.

AI-assisted (Claude Code).

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6135877119 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4733#issuecomment-6135877119). Originally posted by [xXPBSXx](https://github.com/xXPBSXx) on 2026-10-13T01:09:29Z. --- Windows test: head `0ca047e60` merged onto current `main` `1c5e62878` (clean merge, no conflicts). Windows 10, MinGW GCC (WinLibs), Ninja Release, `-j2`: full build 0 errors; ctest with the filter `guest_collate`: guest_collate passed (1 test(s), 0 failed); `python tools/check_conventions.py --base upstream/main` passed. I did not run the new tests against `main` without the change. AI-assisted (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!4733
No description provided.