feat(libc): export the std::__sce_v2 _Locimp_ctor and _Locimp_dtor with facet reference counting #16165

Merged
Danigamer05 merged 4 commits from upstream-pr/16165 into main 2026-11-06 06:10:18 +00:00
Danigamer05 commented 2026-11-05 19:01:04 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Danigamer05 on 2026-11-05T19:01:04Z.


What

Builds on #16113: this branch contains its commit, so until it merges only the two commits after it are this PR's change.

Export the versioned std::__sce_v2 locale::_Locimp::_Locimp_ctor and _Locimp_dtor from libc, and make the versioned _Locimp family count facet references the way the console does.

Export NID Original address
_ZNSt8__sce_v26locale7_Locimp12_Locimp_ctorEPS1_RKS1_ XBQPgR5196I 0xF25E0
_ZNSt8__sce_v26locale7_Locimp12_Locimp_dtorEPS1_ L2pyahLSGFk 0xD1030

Changed exports: _ZNSt8__sce_v26locale7_Locimp11_New_LocimpERKS1_ (ynwqogpIbL0, 0xD0F60) and _ZNSt8__sce_v26locale7_Locimp14_Locimp_AddfacEPS1_PNS0_5facetEm (7KPYZ-jSAto, 0xF2680).

The behavior comes from the libc.prx bundled with PPSA03671 v01.001.005 (J03848509, SHA-256 483961e086b8a1fffa8138b3c0badf798987fb5f35ec908d828946408be2d8a6). As in the module:

  • _Locimp_ctor(self, source) builds only the facet vector. The caller has already copied the count. A zero count leaves the vector alone. Otherwise it calls malloc(count * 8), throws bad_alloc on null, and copies the facets from the last to the first, retaining each non-null one.
  • _Locimp_dtor(self) releases each non-null facet from the last to the first, calls deleteObject on the ones that reach zero, and frees the vector unless it is the classic one. It does not free the _Locimp.
  • _New_Locimp(const _Locimp&) allocates 0x30 bytes and sets the _Locimp vtable, one reference and a null vector. It copies the count, category, transparency and name pointer, then calls _Locimp_ctor. If that throws, it frees the object.
  • _Locimp_Addfac grows the vector with realloc to max(id + 1, 40) slots and clears the new ones. It retains the new facet, releases the one it replaces (deleting it at zero) and stores the new one.
  • The _Locimp objects built by _New_Locimp(const&), _New_Locimp(bool) and empty() get the module's _Locimp vtable. The last release returns the object, and deleteObject runs _Locimp_dtor and frees it. Before this PR they used the copy vtable and were never freed.

Before this PR, the versioned _New_Locimp(const&) and _Locimp_Addfac called the unversioned copy and insertion, which neither retain nor release. The unversioned functions are unchanged.

guest_sce_v2_locale passed facets with a null vtable to _Locimp_Addfac and read the copy after its last release. It now uses a counting facet and checks that the last release deletes the copy and its facet.

A new debt entry lists what still differs: the lock, null arguments, size overflow, the one-slot classic vector, and copies of locales built by unversioned calls. The versioned locale entry drops its sentence about the reused unversioned copy and insertion, and #16113's entry drops the one about the copy vtable.

Tested

Windows x86-64, WinLibs GCC 15.2.0, Release, BUILD_TESTING=ON, on top of #16113 (d18e1c71325, main d893997f1a6); the branch merges cleanly with main c4469b60daf. Exact tested commit: b24626696c2. It adds one commit to 4e7016e6926 that renames two internal helpers. The build, the tests, the side-by-side run and the conventions check ran on it; the negative control and the overlap checks below ran on 4e7016e6926.

  • Built libc, libkernel, their NID-patched production libraries and the related tests. The new guest_sce_v2_locimp_references passes, along with guest_sce_v2_locale, guest_sce_v2_locale_basics, guest_locale and guest_sce_v2_exceptions; public_headers passed on 4e7016e6926.

  • The NID-patched libc exports both new NIDs.

  • The new test uses facets that count retains, releases and deletes. It checks:

    • the fields _New_Locimp(const&) copies, and that the copy owns its vector;
    • growth to 40 and to id + 1, the cleared slots, replacing a facet and re-adding the same one;
    • the references a nested copy takes, and that the last release deletes the copy and releases its facets;
    • that _Locimp_ctor leaves the vector alone for a zero count and copies only count facets, and that _Locimp_dtor frees the vector and deletes facets at zero;
    • that a _Locimp sharing the classic vector keeps it on destruction and gets a copy of it on growth;
    • that a failed allocation in each call throws the guest bad_alloc, retains nothing, keeps the vector and frees the half-built _New_Locimp object, and that another thread can still take the lock afterwards;
    • that _New_Locimp(bool) and empty() objects are freed with their facets on the last release.
  • 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. Both sides' _Xbad_alloc were redirected to one recorder. The recorder exits by longjmp, so after a failed _New_Locimp(const&) the harness frees the object as the module's landing pad does.

  • Side by side with the patched production library, over two seeds with 200,000 random steps each: about 64,000 _New_Locimp(const&), 16,000 _New_Locimp(bool), 88,000 _Locimp_Addfac, 100,000 _Locimp_ctor/_Locimp_dtor pairs on caller-owned objects, and 131,000 retains, releases and destroy calls through the vtable. One step in six made its allocation fail (about 12,000 throws per seed), and about 13,000 _Locimps per seed reached zero and were deleted. Each step compared:

    • every _Locimp's references, count, category, transparency, name and slots;
    • each of 24 facets' references, retains, releases, deletes and destroys;
    • the sequence and sizes of malloc, realloc and free calls.

    The only difference is that the module also calls free(NULL), which the harness ignores. No comparison differed. #16113's side-by-side run also still passes with two seeds on 4e7016e6926.

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

    • _Locimp_ctor did not retain;
    • _Locimp_dtor did not free the vector;
    • _Locimp_Addfac kept the replaced facet;
    • _New_Locimp(const&) did not copy the count;
    • _New_Locimp(bool) kept the copy vtable.

    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 tests and side-by-side run passed again.

  • Code conventions and git diff --check pass.

  • tools/pr_overlap.py read 1,871 open PRs at 4e7016e6926 and reports no overlap. It keeps exit 2 for 36 branches that already conflict with main. Merging each of the 36 with this branch and with main using git merge-tree gives the same conflict blocks, apart from main's own movement since #16113's base, except in docs/dev/debt/libc.md: for #5021, #9371, #9447 and #9526 the edited _Locimp line and the new entry land inside the conflict block they already have there, as #16113's lines do, and #14212 gets no conflict block beyond the ones it has with #16113. The debt edit adds no conflict over #16113 with any of the 142 open PRs that touch that file. No open PR adds these names or NIDs. The branch also merges cleanly with #14595, #15336, #15471, #15484, #15490, #15501, #15519, #15552, #15561, #15602, #15614, #15667, #15683, #15791, #15796, #15809, #15834, #15879, #15923, #16083 and #16113.

Not tested: Linux, macOS, another SDK, a game, GPU or console. 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: #16113
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5177078384 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/16165). Originally posted by [Danigamer05](https://github.com/Danigamer05) on 2026-11-05T19:01:04Z. --- ### What Builds on #16113: this branch contains its commit, so until it merges only the two commits after it are this PR's change. Export the versioned `std::__sce_v2` `locale::_Locimp::_Locimp_ctor` and `_Locimp_dtor` from libc, and make the versioned `_Locimp` family count facet references the way the console does. | Export | NID | Original address | | --- | --- | --- | | `_ZNSt8__sce_v26locale7_Locimp12_Locimp_ctorEPS1_RKS1_` | `XBQPgR5196I` | `0xF25E0` | | `_ZNSt8__sce_v26locale7_Locimp12_Locimp_dtorEPS1_` | `L2pyahLSGFk` | `0xD1030` | Changed exports: `_ZNSt8__sce_v26locale7_Locimp11_New_LocimpERKS1_` (`ynwqogpIbL0`, `0xD0F60`) and `_ZNSt8__sce_v26locale7_Locimp14_Locimp_AddfacEPS1_PNS0_5facetEm` (`7KPYZ-jSAto`, `0xF2680`). The behavior comes from the `libc.prx` bundled with PPSA03671 v01.001.005 (J03848509, SHA-256 `483961e086b8a1fffa8138b3c0badf798987fb5f35ec908d828946408be2d8a6`). As in the module: - `_Locimp_ctor(self, source)` builds only the facet vector. The caller has already copied the count. A zero count leaves the vector alone. Otherwise it calls `malloc(count * 8)`, throws `bad_alloc` on null, and copies the facets from the last to the first, retaining each non-null one. - `_Locimp_dtor(self)` releases each non-null facet from the last to the first, calls `deleteObject` on the ones that reach zero, and frees the vector unless it is the classic one. It does not free the `_Locimp`. - `_New_Locimp(const _Locimp&)` allocates `0x30` bytes and sets the `_Locimp` vtable, one reference and a null vector. It copies the count, category, transparency and name pointer, then calls `_Locimp_ctor`. If that throws, it frees the object. - `_Locimp_Addfac` grows the vector with `realloc` to `max(id + 1, 40)` slots and clears the new ones. It retains the new facet, releases the one it replaces (deleting it at zero) and stores the new one. - The `_Locimp` objects built by `_New_Locimp(const&)`, `_New_Locimp(bool)` and `empty()` get the module's `_Locimp` vtable. The last release returns the object, and `deleteObject` runs `_Locimp_dtor` and frees it. Before this PR they used the copy vtable and were never freed. Before this PR, the versioned `_New_Locimp(const&)` and `_Locimp_Addfac` called the unversioned copy and insertion, which neither retain nor release. The unversioned functions are unchanged. `guest_sce_v2_locale` passed facets with a null vtable to `_Locimp_Addfac` and read the copy after its last release. It now uses a counting facet and checks that the last release deletes the copy and its facet. A new debt entry lists what still differs: the lock, null arguments, size overflow, the one-slot classic vector, and copies of locales built by unversioned calls. The versioned locale entry drops its sentence about the reused unversioned copy and insertion, and #16113's entry drops the one about the copy vtable. ### 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 #16113 (`d18e1c71325`, main `d893997f1a6`); the branch merges cleanly with main `c4469b60daf`. Exact tested commit: `b24626696c2`. It adds one commit to `4e7016e6926` that renames two internal helpers. The build, the tests, the side-by-side run and the conventions check ran on it; the negative control and the overlap checks below ran on `4e7016e6926`. - Built libc, libkernel, their NID-patched production libraries and the related tests. The new `guest_sce_v2_locimp_references` passes, along with `guest_sce_v2_locale`, `guest_sce_v2_locale_basics`, `guest_locale` and `guest_sce_v2_exceptions`; `public_headers` passed on `4e7016e6926`. - The NID-patched libc exports both new NIDs. - The new test uses facets that count retains, releases and deletes. It checks: - the fields `_New_Locimp(const&)` copies, and that the copy owns its vector; - growth to 40 and to `id + 1`, the cleared slots, replacing a facet and re-adding the same one; - the references a nested copy takes, and that the last release deletes the copy and releases its facets; - that `_Locimp_ctor` leaves the vector alone for a zero count and copies only `count` facets, and that `_Locimp_dtor` frees the vector and deletes facets at zero; - that a `_Locimp` sharing the classic vector keeps it on destruction and gets a copy of it on growth; - that a failed allocation in each call throws the guest `bad_alloc`, retains nothing, keeps the vector and frees the half-built `_New_Locimp` object, and that another thread can still take the lock afterwards; - that `_New_Locimp(bool)` and `empty()` objects are freed with their facets on the last release. - 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. Both sides' `_Xbad_alloc` were redirected to one recorder. The recorder exits by `longjmp`, so after a failed `_New_Locimp(const&)` the harness frees the object as the module's landing pad does. - Side by side with the patched production library, over two seeds with 200,000 random steps each: about 64,000 `_New_Locimp(const&)`, 16,000 `_New_Locimp(bool)`, 88,000 `_Locimp_Addfac`, 100,000 `_Locimp_ctor`/`_Locimp_dtor` pairs on caller-owned objects, and 131,000 retains, releases and `destroy` calls through the vtable. One step in six made its allocation fail (about 12,000 throws per seed), and about 13,000 `_Locimp`s per seed reached zero and were deleted. Each step compared: - every `_Locimp`'s references, count, category, transparency, name and slots; - each of 24 facets' references, retains, releases, deletes and destroys; - the sequence and sizes of `malloc`, `realloc` and `free` calls. The only difference is that the module also calls `free(NULL)`, which the harness ignores. No comparison differed. #16113's side-by-side run also still passes with two seeds on `4e7016e6926`. - Negative control: five lines were altered one at a time: - `_Locimp_ctor` did not retain; - `_Locimp_dtor` did not free the vector; - `_Locimp_Addfac` kept the replaced facet; - `_New_Locimp(const&)` did not copy the count; - `_New_Locimp(bool)` kept the copy vtable. 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 tests and side-by-side run passed again. - Code conventions and `git diff --check` pass. - `tools/pr_overlap.py` read 1,871 open PRs at `4e7016e6926` and reports no overlap. It keeps exit 2 for 36 branches that already conflict with main. Merging each of the 36 with this branch and with main using `git merge-tree` gives the same conflict blocks, apart from main's own movement since #16113's base, except in `docs/dev/debt/libc.md`: for #5021, #9371, #9447 and #9526 the edited `_Locimp` line and the new entry land inside the conflict block they already have there, as #16113's lines do, and #14212 gets no conflict block beyond the ones it has with #16113. The debt edit adds no conflict over #16113 with any of the 142 open PRs that touch that file. No open PR adds these names or NIDs. The branch also merges cleanly with #14595, #15336, #15471, #15484, #15490, #15501, #15519, #15552, #15561, #15602, #15614, #15667, #15683, #15791, #15796, #15809, #15834, #15879, #15923, #16083 and #16113. Not tested: Linux, macOS, another SDK, a game, GPU or console. 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: #16113 - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes 🤖 Generated with [Claude Code](https://claude.com/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!16165
No description provided.