feat(libSceLibcInternal): implement locale::facet _Incref and _Decref #23858

Merged
GiannisG1 merged 1 commit from upstream-pr/23858 into main 2026-11-19 21:56:11 +00:00
GiannisG1 commented 2026-11-19 19:49:53 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by GiannisG1 on 2026-11-19T19:49:53Z.


What

std::locale::facet::_Incref and _Decref (inventory 076 and 075, NIDs -EgSegeAKl4 and AgxEl+HeWRQ) were parameterless placeholders that threw. In the Dinkumware/Microsoft STL design they override _Facet_base's virtuals, so the vtable of a facet a title defines itself (a custom codecvt or numpunct, say) points at these exports. Installing such a facet in a locale, or copying and destroying that locale, called a throwing stub.

They now follow Microsoft STL's locale::facet (stl/inc/xlocale):

  • _Incref atomically adds one to the reference count;
  • _Decref atomically subtracts one and returns the facet when the count reaches zero (the caller deletes it), otherwise null.

The count is the 32-bit field at 0x08 that the retain and release entries of the facet vtables in libc (RetainFacet/ReleaseFacet in LocaleSupport.cpp) already use the same way. A null facet throws std::invalid_argument. The mangled names hash to the inventory NIDs (tools/nid_names.py), so the exports are named and the two placeholders are gone. The new header is included by the library umbrella. The debt fragment removes the two table rows and adds a Functional entry: not verified on a console.

Tested

Linux Mint 22.3, GCC 13.3.0, Release, head on main e4cee4952b.

  • New test guest_libc_internal_facet_references:
    • two _Incref calls make the count 2 and leave the vtable pointer and the next field alone;
    • _Decref returns null at 2→1 and the facet at 1→0;
    • 8 threads × 20000 _Incref calls reach 160000, and the same number of concurrent _Decref calls brings it back to 0 with exactly one of them returning the facet;
    • a null facet throws std::invalid_argument.
  • main: the test does not link, because neither export exists there (the NIDs reach the throwing placeholders). This PR: passes 3/3.
  • Split suite (tools/suite.py) on this branch: GPU tests on a GTX 1050 Ti (NVIDIA 580.178.04), an HD 630 (Mesa 25.2.8 ANV) and llvmpipe 25.2.8, everything else once. The results are identical to the main e4cee4952b baseline: GTX none failed, HD 630 the same 10, llvmpipe the same 3. guest_libc_internal_facet_references and public_headers pass. progress.py --module libSceLibcInternal lists neither export as pending.
  • check_conventions.py, check_test_registration.py, check_main_sync.py (merges cleanly), check_compile.py and debt_fold.py --check are clean.
  • pr_overlap.py --branch: #17300 (rename-only) names the same two aliases. #13778, #14834, #17300, #18135 and #21704 could not be compared because they conflict with main. A trial merge with each adds no conflicting file beyond those it already has. It merges cleanly with #23809, #23837 and #23846.

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 and the licence notice of code adapted from upstream (see AGENTS.md)
  • 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
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5401854973 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/23858). Originally posted by [GiannisG1](https://github.com/GiannisG1) on 2026-11-19T19:49:53Z. --- ### What `std::locale::facet::_Incref` and `_Decref` (inventory 076 and 075, NIDs `-EgSegeAKl4` and `AgxEl+HeWRQ`) were parameterless placeholders that threw. In the Dinkumware/Microsoft STL design they override `_Facet_base`'s virtuals, so the vtable of a facet a title defines itself (a custom `codecvt` or `numpunct`, say) points at these exports. Installing such a facet in a locale, or copying and destroying that locale, called a throwing stub. They now follow Microsoft STL's [`locale::facet`](https://github.com/microsoft/STL/blob/770da5aa457af11f0a796600e1aa6f7f6fce14a6/stl/inc/xlocale) (`stl/inc/xlocale`): - `_Incref` atomically adds one to the reference count; - `_Decref` atomically subtracts one and returns the facet when the count reaches zero (the caller deletes it), otherwise null. The count is the 32-bit field at 0x08 that the retain and release entries of the facet vtables in libc (`RetainFacet`/`ReleaseFacet` in `LocaleSupport.cpp`) already use the same way. A null facet throws `std::invalid_argument`. The mangled names hash to the inventory NIDs (`tools/nid_names.py`), so the exports are named and the two placeholders are gone. The new header is included by the library umbrella. The debt fragment removes the two table rows and adds a Functional entry: not verified on a console. ### Tested Linux Mint 22.3, GCC 13.3.0, Release, head on `main` `e4cee4952b`. - New test `guest_libc_internal_facet_references`: - two `_Incref` calls make the count 2 and leave the vtable pointer and the next field alone; - `_Decref` returns null at 2→1 and the facet at 1→0; - 8 threads × 20000 `_Incref` calls reach 160000, and the same number of concurrent `_Decref` calls brings it back to 0 with exactly one of them returning the facet; - a null facet throws `std::invalid_argument`. - `main`: the test does not link, because neither export exists there (the NIDs reach the throwing placeholders). This PR: passes 3/3. - Split suite (`tools/suite.py`) on this branch: GPU tests on a GTX 1050 Ti (NVIDIA 580.178.04), an HD 630 (Mesa 25.2.8 ANV) and llvmpipe 25.2.8, everything else once. The results are identical to the `main` `e4cee4952b` baseline: GTX none failed, HD 630 the same 10, llvmpipe the same 3. `guest_libc_internal_facet_references` and `public_headers` pass. `progress.py --module libSceLibcInternal` lists neither export as pending. - `check_conventions.py`, `check_test_registration.py`, `check_main_sync.py` (merges cleanly), `check_compile.py` and `debt_fold.py --check` are clean. - `pr_overlap.py --branch`: #17300 (rename-only) names the same two aliases. #13778, #14834, #17300, #18135 and #21704 could not be compared because they conflict with `main`. A trial merge with each adds no conflicting file beyond those it already has. It merges cleanly with #23809, #23837 and #23846. ### 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) and the licence notice of code adapted from upstream (see AGENTS.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] Fixes: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes 🤖 Generated with [Claude Code](https://claude.com/claude-code)
PrivacyOnion commented 2026-11-19 20:04:08 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by PrivacyOnion on 2026-11-19T20:04:08Z.

Review: APPROVED


Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against 1215eadb2c:

  • Conventions: tools/check_conventions.py passed.
  • Build: guest_libc_internal_facet_references_tests built cleanly.
  • CTest (guest_libc_internal_facet_references):
    1/1 Test #4424: guest_libc_internal_facet_references ...   Passed    0.04 sec
    100% tests passed, 0 tests failed out of 1
    
  • Negative control: with core/libs/prx/libSceLibcInternal/InventoryExports.cpp, core/libs/prx/libSceLibcInternal/include/InventoryExports.hpp, core/libs/prx/libSceLibcInternal/libSceLibcInternal.hpp, core/libs/prx/libSceLibcInternal/FacetReferences.cpp reverted to main, the new tests do not build/link without the change; restoring it makes them build and pass.
  • Code reading:
    • Validated null-pointer handling: both _Incref and _Decref verify self == nullptr and throw std::invalid_argument before any dereference.
    • Verified atomic operations and memory ordering: std::atomic_refstd::uint32_t(self->references) correctly issues fetch_add(1) and fetch_sub(1) == 1 to return self on reaching 0 and nullptr otherwise.
    • Confirmed ABI and symbol compatibility: mangled names _ZNSt6locale5facet7_IncrefEv_nid_postfix and _ZNSt6locale5facet7_DecrefEv_nid_postfix correctly replace inventory 076 (-EgSegeAKl4) and 075 (AgxEl+HeWRQ) stubs with APS5_VABI.
    • Verified AGENTS.md compliance: conventions passed, no illegal comments, debt fragment added under docs/dev/debt/changes/feat-libcinternal-facet-incref-decref.md removing the stubs and adding Functional entry.
    • Evaluated concurrency and stress tests in GuestLibcInternalFacetReferences.cpp: multithreaded increment and decrement loops verify atomicity and race-free terminal decrement.

Looks Good To Me

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5982413367 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/23858#pullrequestreview-5982413367). Originally posted by [PrivacyOnion](https://github.com/PrivacyOnion) on 2026-11-19T20:04:08Z. **Review:** APPROVED --- Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against `1215eadb2c`: - **Conventions**: `tools/check_conventions.py` passed. - **Build**: `guest_libc_internal_facet_references_tests` built cleanly. - **CTest** (`guest_libc_internal_facet_references`): ``` 1/1 Test #4424: guest_libc_internal_facet_references ... Passed 0.04 sec 100% tests passed, 0 tests failed out of 1 ``` - **Negative control**: with `core/libs/prx/libSceLibcInternal/InventoryExports.cpp`, `core/libs/prx/libSceLibcInternal/include/InventoryExports.hpp`, `core/libs/prx/libSceLibcInternal/libSceLibcInternal.hpp`, `core/libs/prx/libSceLibcInternal/FacetReferences.cpp` reverted to `main`, the new tests do not build/link without the change; restoring it makes them build and pass. - **Code reading**: - Validated null-pointer handling: both _Incref and _Decref verify self == nullptr and throw std::invalid_argument before any dereference. - Verified atomic operations and memory ordering: std::atomic_ref<std::uint32_t>(self->references) correctly issues fetch_add(1) and fetch_sub(1) == 1 to return self on reaching 0 and nullptr otherwise. - Confirmed ABI and symbol compatibility: mangled names _ZNSt6locale5facet7_IncrefEv_nid_postfix and _ZNSt6locale5facet7_DecrefEv_nid_postfix correctly replace inventory 076 (-EgSegeAKl4) and 075 (AgxEl+HeWRQ) stubs with APS5_VABI. - Verified AGENTS.md compliance: conventions passed, no illegal comments, debt fragment added under docs/dev/debt/changes/feat-libcinternal-facet-incref-decref.md removing the stubs and adding Functional entry. - Evaluated concurrency and stress tests in GuestLibcInternalFacetReferences.cpp: multithreaded increment and decrement loops verify atomicity and race-free terminal decrement. Looks Good To Me
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!23858
No description provided.