feat(libc): implement collate<char>::_Getcat #885

Merged
adriadam10 merged 1 commit from upstream-pr/885 into main 2026-10-07 02:29:49 +00:00
adriadam10 commented 2026-10-06 12:54:31 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-06T12:54:31Z.


What

Implements std::collate<char>::_Getcat (libc, BSVJqITGCyI), which was a NotImplemented stub. Astro's Playroom imports it. collate<wchar_t>::_Getcat still throws.

Everything below comes from the disassembly of the console's libc.prx (the copy in Astro's Playroom's sce_module/). No emulator implements this function:

  • _Getcat(const facet** ppf, const locale* ploc) returns 1 (_X_COLLATE). It builds a facet only when ppf is non-null and *ppf is null. The facet is a 0x20-byte operator new allocation: vtable, reference count 0, then two pointers to libc's collation state at 0x10 and 0x18.
  • Facet vtable: the complete destructor (does nothing), the deleting destructor (operator delete), _Incref (lock xadd +1), _Decref (lock xadd -1, which returns this when the old count was 1), then do_compare, do_transform and do_hash.
  • do_compare compares the two ranges one null-terminated segment at a time with the locale's strcoll and returns -1, 0 or 1. In the C locale strcoll is strcmp, so the result is the lexicographic order of the unsigned bytes, with the shorter range first on a tie. The tests cover embedded nulls, 0x80, and a prefix of the other range.
  • do_hash is 64-bit FNV-1a over the bytes, the same as MS STL's collate::do_hash, which comes from the same Dinkumware line. The tests check it against the FNV reference vectors ("", "a", "foobar").
  • do_transform returns a guest std::string: 0x28 bytes, with a 16-byte inline buffer at 0x8, the size at 0x18 and the capacity at 0x20. Results longer than 15 characters are allocated with operator new(capacity + 1). In the C locale the result is a copy of the input.

What still throws, listed in TechnicalDebt: locales other than "C"; do_transform on input with embedded nulls, which the console drops depending on the output buffer size; and do_transform results of 4095 characters or more. For those, the guest's string deallocation expects the over-aligned large-allocation header (it reads the real pointer from p[-1] when capacity + 1 >= 0x1000). The two collation-state pointers are left null, because only the facet's own functions read them.

Tested

Linux, new guest_collate ctest (core/libs/tests/GuestCollate.cpp). It installs an allocator through ApplicationHeapRegister and calls through the facet's vtable the way guest code does. It covers _Getcat (null ppf, an existing facet left alone, a non-C locale rejected, the allocation size), compare, hash, and transform (inline, exactly 15 characters, heap, 4094 characters, rejected inputs). It also covers the Incref/Decref pair down to the deleting destructor. The test fails on main, where the stub throws. All 17 mutations of the implementation make it fail. Full Release build (GCC) and ctest -R 'guest_|collate|locale|application_heap' pass.

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 in TechnicalDebt
  • 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
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4759009061 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/885). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-06T12:54:31Z. --- ### What Implements `std::collate<char>::_Getcat` (libc, `BSVJqITGCyI`), which was a `NotImplemented` stub. Astro's Playroom imports it. `collate<wchar_t>::_Getcat` still throws. Everything below comes from the disassembly of the console's `libc.prx` (the copy in Astro's Playroom's `sce_module/`). No emulator implements this function: - `_Getcat(const facet** ppf, const locale* ploc)` returns 1 (`_X_COLLATE`). It builds a facet only when `ppf` is non-null and `*ppf` is null. The facet is a 0x20-byte `operator new` allocation: vtable, reference count 0, then two pointers to libc's collation state at 0x10 and 0x18. - Facet vtable: the complete destructor (does nothing), the deleting destructor (`operator delete`), `_Incref` (`lock xadd +1`), `_Decref` (`lock xadd -1`, which returns `this` when the old count was 1), then `do_compare`, `do_transform` and `do_hash`. - `do_compare` compares the two ranges one null-terminated segment at a time with the locale's `strcoll` and returns -1, 0 or 1. In the C locale `strcoll` is `strcmp`, so the result is the lexicographic order of the unsigned bytes, with the shorter range first on a tie. The tests cover embedded nulls, `0x80`, and a prefix of the other range. - `do_hash` is 64-bit FNV-1a over the bytes, the same as MS STL's `collate::do_hash`, which comes from the same Dinkumware line. The tests check it against the FNV reference vectors (`""`, `"a"`, `"foobar"`). - `do_transform` returns a guest `std::string`: 0x28 bytes, with a 16-byte inline buffer at 0x8, the size at 0x18 and the capacity at 0x20. Results longer than 15 characters are allocated with `operator new(capacity + 1)`. In the C locale the result is a copy of the input. What still throws, listed in TechnicalDebt: locales other than "C"; `do_transform` on input with embedded nulls, which the console drops depending on the output buffer size; and `do_transform` results of 4095 characters or more. For those, the guest's string deallocation expects the over-aligned large-allocation header (it reads the real pointer from `p[-1]` when `capacity + 1 >= 0x1000`). The two collation-state pointers are left null, because only the facet's own functions read them. ### Tested Linux, new `guest_collate` ctest (`core/libs/tests/GuestCollate.cpp`). It installs an allocator through `ApplicationHeapRegister` and calls through the facet's vtable the way guest code does. It covers `_Getcat` (null `ppf`, an existing facet left alone, a non-C locale rejected, the allocation size), compare, hash, and transform (inline, exactly 15 characters, heap, 4094 characters, rejected inputs). It also covers the Incref/Decref pair down to the deleting destructor. The test fails on `main`, where the stub throws. All 17 mutations of the implementation make it fail. Full Release build (GCC) and `ctest -R 'guest_|collate|locale|application_heap'` pass. ### 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 in [TechnicalDebt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/TechnicalDebt.md#silent-stubs) - [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] AI-assisted: yes
github-actions[bot] commented 2026-10-06 12:54:56 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-06T12:54:56Z.


Progress report (5895ea7 → a546bbe)

📈 System libraries: 87.07% (+0.07%, +8 implemented functions)

✅ 8 implemented
Library Name
libc CollateCompare
libc CollateHash
libc CollateTransform
libc DeleteCollate
libc DestroyCollate
libc ReleaseFacet
libc RetainFacet
libc _ZNSt7collateIcE7_GetcatEPPKNSt6locale5facetEPKS1__nid_postfix
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6016711973 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/885#issuecomment-6016711973). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-06T12:54:56Z. --- <!-- progress-report --> ### Progress report (5895ea7 → a546bbe) 📈 **System libraries**: 87.07% (+0.07%, +8 implemented functions) <details> <summary>✅ 8 implemented</summary> | Library | Name | | - | - | | libc | `CollateCompare` | | libc | `CollateHash` | | libc | `CollateTransform` | | libc | `DeleteCollate` | | libc | `DestroyCollate` | | libc | `ReleaseFacet` | | libc | `RetainFacet` | | libc | `_ZNSt7collateIcE7_GetcatEPPKNSt6locale5facetEPKS1__nid_postfix` | </details>
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!885
No description provided.