feat(libSceLibcInternal): implement basic_string erase, append, assign, _Xran and _Xlen #15692

Merged
B-Howell merged 9 commits from upstream-pr/15692 into main 2026-11-07 20:15:22 +00:00
B-Howell commented 2026-11-04 06:41:41 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by B-Howell on 2026-11-04T06:41:41Z.


What

Implements eight libSceLibcInternal exports of the unversioned std::string (std::basic_string<char>, mangled Ss) that were stubs throwing through NotImplemented_nid_no_patch. Each mangled name was checked against its NID with tools/nid_names.py's compute_nid:

Export NID Old stub
_ZNSs5eraseEmm 0Ir3jiT4V6Q sceLibcInternalPending4_110
_ZNSs6appendERKSsmm qiR-4jx1abE sceLibcInternalPending4_111
_ZNSs6appendEmc QqBWUNEfIAo sceLibcInternalPending4_112
_ZNSs6assignEPKcm xSxPHmpcNzY sceLibcInternalPending4_113
_ZNSs6assignERKSsmm pGxNI4JKfmY sceLibcInternalPending4_114
_ZNSs6assignEmc ikjnoeemspQ sceLibcInternalPending4_115
_ZNKSs5_XranEv AzBnOt1DouU sceLibcInternalUnknown_AzBnOt1DouU
_ZNKSs5_XlenEv pDtTdJ2sIz0 sceLibcInternalUnknown_pDtTdJ2sIz0

Where the behaviour comes from:

  • ISO C++ [string.erase] (erase(pos, n): out_of_range if pos > size(), removes min(n, size() - pos) characters), [string.append] (append(str, pos, n) and append(n, c): out_of_range if pos > str.size(), length_error past max_size()) and [string.assign] (assign(s, n), assign(str, pos, n), assign(n, c)). Self-referencing calls (assign from a pointer into *this, append(*this, ...), assign(*this, pos, n)) keep their source valid across a reallocation.
  • Layout and allocation reuse libc's existing guest string: GuestLocale::String (static_assert(sizeof(String) == 0x28) in libc/include/GuestLocale.hpp; buffer at 0x8, size at 0x18, capacity at 0x20, inline while capacity is 15) and GuestLocale::AllocateStringStorage from libc/src/LocaleSupport.cpp (32-byte aligned with the raw pointer at [-1] from 0x1000 bytes). A grown string gets capacity size | 15, the policy libc's CollateTransform and AssignCString use. To share them, StringInlineCapacity and StringCapacityMask move from LocaleSupport.cpp into GuestLocale.hpp, and the release half of ReleaseStringStorage becomes GuestLocale::DeallocateStringStorage, which TakeString now also uses. Behaviour of the libc code is unchanged.
  • _Xran and _Xlen forward to libc's _ZSt14_Xout_of_rangePKc and _ZSt14_Xlength_errorPKc. The messages are microsoft/STL's at e1ed302683e5e822f663fbb0f44cee7e9ddc6af0, stl/inc/xstring L481-L482 _Xout_of_range("invalid string position"); and L573-L574 _Xlength_error("string too long");.

_ZNSs5_CopyEmm (9HILqEoh24E, sceLibcInternalPending4_109) and _ZNSs6insertEmmc keep throwing: _Copy is the console's internal growth step and was not confirmed by disassembly.

The stubs are removed from Export.cpp and InventoryExports2.cpp, and the new functions are declared once in include/Api.hpp. The debt file loses these NIDs from its stub entries and gains one entry for what is still unverified: the console's libSceLibcInternal.sprx was not read, so its growth factor may differ from size | 15, and max_size() is taken as SIZE_MAX - 1.

Who calls them: guest C++ code built against the unversioned std::string calls these out-of-line modifiers; the rest of basic_string is inlined into the title.

tools/progress.py collect_libraries() against origin/main c2eafe4461: done goes from 9863 to 9871 (+8). For libSceLibcInternal, done goes from 84 to 92 and todo from 1045 to 1037; no other library changes and no todo is added.

Tested

  • New guest test guest_libc_internal_basic_string (core/libs/tests/GuestLibcInternalBasicString.cpp), using the existing GuestSceV2Exceptions.S guest catch harness. It checks every function's result and returned *this, inline and heap storage, capacity after growth, storage reuse on shrink and on assignment from inside *this, self-append across a reallocation, a 5005-character string in 32-byte aligned large storage, embedded nulls, the untouched first 8 bytes, the out_of_range/length_error type and message thrown by each range or length failure, and that every allocation is released.
  • Windows 11, WinLibs GCC 15.2.0, Release: guest_libc_internal_basic_string passed 3/3 runs. guest_collate, guest_filesystem_error, guest_future_category, guest_generic_category, guest_libc_internal_exception_destructors, guest_locale and guest_sce_v2_locale (the users of the moved libc string helpers) passed 7/7. public_headers (the single-declaration check) passed 1/1.
  • Linux, WSL Ubuntu 22.04, GCC 11.4.0, Release: guest_libc_internal_basic_string passed 3/3 runs. The same seven libc and libSceLibcInternal tests passed 7/7.
  • Negative control: main's production files (Export.cpp, InventoryExports2.cpp, include/Api.hpp, GuestLocale.hpp, LocaleSupport.cpp, without BasicString.cpp) plus the new test fail to build on both Windows and Linux: StringInlineCapacity is not a member of GuestLocale and the _ZNSs/_ZNKSs functions are not declared.
  • python3 tools/check_conventions.py --base origin/main printed "All conventions checks passed." The conventions_* and debt_split ctest entries are self-tests of the checker scripts and were not run separately.
  • python3 tools/pr_overlap.py --branch feat/lci-string found no open PR with the same exports or new files. It failed closed for PRs it could not compare automatically (5021, 13486, 13778, 13782, 13881, 14085, 14212, 14401, 14837, 14936). The added lines of each were checked by hand: none contains _ZNSs, _ZNKSs or any of these NIDs. PR 14158 touches neighbouring lines in libSceLibcInternal/CMakeLists.txt and the debt file, and PRs 14373 and 14834 touch neighbouring lines in include/Api.hpp.
  • No title run.

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
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5150103983 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/15692). Originally posted by [B-Howell](https://github.com/B-Howell) on 2026-11-04T06:41:41Z. --- ### What Implements eight `libSceLibcInternal` exports of the unversioned `std::string` (`std::basic_string<char>`, mangled `Ss`) that were stubs throwing through `NotImplemented_nid_no_patch`. Each mangled name was checked against its NID with `tools/nid_names.py`'s `compute_nid`: | Export | NID | Old stub | | --- | --- | --- | | `_ZNSs5eraseEmm` | `0Ir3jiT4V6Q` | `sceLibcInternalPending4_110` | | `_ZNSs6appendERKSsmm` | `qiR-4jx1abE` | `sceLibcInternalPending4_111` | | `_ZNSs6appendEmc` | `QqBWUNEfIAo` | `sceLibcInternalPending4_112` | | `_ZNSs6assignEPKcm` | `xSxPHmpcNzY` | `sceLibcInternalPending4_113` | | `_ZNSs6assignERKSsmm` | `pGxNI4JKfmY` | `sceLibcInternalPending4_114` | | `_ZNSs6assignEmc` | `ikjnoeemspQ` | `sceLibcInternalPending4_115` | | `_ZNKSs5_XranEv` | `AzBnOt1DouU` | `sceLibcInternalUnknown_AzBnOt1DouU` | | `_ZNKSs5_XlenEv` | `pDtTdJ2sIz0` | `sceLibcInternalUnknown_pDtTdJ2sIz0` | Where the behaviour comes from: - ISO C++ [string.erase] (`erase(pos, n)`: `out_of_range` if `pos > size()`, removes `min(n, size() - pos)` characters), [string.append] (`append(str, pos, n)` and `append(n, c)`: `out_of_range` if `pos > str.size()`, `length_error` past `max_size()`) and [string.assign] (`assign(s, n)`, `assign(str, pos, n)`, `assign(n, c)`). Self-referencing calls (`assign` from a pointer into `*this`, `append(*this, ...)`, `assign(*this, pos, n)`) keep their source valid across a reallocation. - Layout and allocation reuse libc's existing guest string: `GuestLocale::String` (`static_assert(sizeof(String) == 0x28)` in `libc/include/GuestLocale.hpp`; buffer at 0x8, size at 0x18, capacity at 0x20, inline while capacity is 15) and `GuestLocale::AllocateStringStorage` from `libc/src/LocaleSupport.cpp` (32-byte aligned with the raw pointer at `[-1]` from 0x1000 bytes). A grown string gets capacity `size | 15`, the policy libc's `CollateTransform` and `AssignCString` use. To share them, `StringInlineCapacity` and `StringCapacityMask` move from `LocaleSupport.cpp` into `GuestLocale.hpp`, and the release half of `ReleaseStringStorage` becomes `GuestLocale::DeallocateStringStorage`, which `TakeString` now also uses. Behaviour of the libc code is unchanged. - `_Xran` and `_Xlen` forward to libc's `_ZSt14_Xout_of_rangePKc` and `_ZSt14_Xlength_errorPKc`. The messages are microsoft/STL's at `e1ed302683e5e822f663fbb0f44cee7e9ddc6af0`, `stl/inc/xstring` L481-L482 `_Xout_of_range("invalid string position");` and L573-L574 `_Xlength_error("string too long");`. `_ZNSs5_CopyEmm` (`9HILqEoh24E`, `sceLibcInternalPending4_109`) and `_ZNSs6insertEmmc` keep throwing: `_Copy` is the console's internal growth step and was not confirmed by disassembly. The stubs are removed from `Export.cpp` and `InventoryExports2.cpp`, and the new functions are declared once in `include/Api.hpp`. The debt file loses these NIDs from its stub entries and gains one entry for what is still unverified: the console's `libSceLibcInternal.sprx` was not read, so its growth factor may differ from `size | 15`, and `max_size()` is taken as `SIZE_MAX - 1`. Who calls them: guest C++ code built against the unversioned `std::string` calls these out-of-line modifiers; the rest of `basic_string` is inlined into the title. `tools/progress.py` `collect_libraries()` against `origin/main` `c2eafe4461`: `done` goes from 9863 to 9871 (+8). For `libSceLibcInternal`, `done` goes from 84 to 92 and `todo` from 1045 to 1037; no other library changes and no todo is added. ### Tested - New guest test `guest_libc_internal_basic_string` (`core/libs/tests/GuestLibcInternalBasicString.cpp`), using the existing `GuestSceV2Exceptions.S` guest catch harness. It checks every function's result and returned `*this`, inline and heap storage, capacity after growth, storage reuse on shrink and on assignment from inside `*this`, self-append across a reallocation, a 5005-character string in 32-byte aligned large storage, embedded nulls, the untouched first 8 bytes, the `out_of_range`/`length_error` type and message thrown by each range or length failure, and that every allocation is released. - Windows 11, WinLibs GCC 15.2.0, Release: `guest_libc_internal_basic_string` passed 3/3 runs. `guest_collate`, `guest_filesystem_error`, `guest_future_category`, `guest_generic_category`, `guest_libc_internal_exception_destructors`, `guest_locale` and `guest_sce_v2_locale` (the users of the moved libc string helpers) passed 7/7. `public_headers` (the single-declaration check) passed 1/1. - Linux, WSL Ubuntu 22.04, GCC 11.4.0, Release: `guest_libc_internal_basic_string` passed 3/3 runs. The same seven libc and libSceLibcInternal tests passed 7/7. - Negative control: `main`'s production files (`Export.cpp`, `InventoryExports2.cpp`, `include/Api.hpp`, `GuestLocale.hpp`, `LocaleSupport.cpp`, without `BasicString.cpp`) plus the new test fail to build on both Windows and Linux: `StringInlineCapacity` is not a member of `GuestLocale` and the `_ZNSs`/`_ZNKSs` functions are not declared. - `python3 tools/check_conventions.py --base origin/main` printed "All conventions checks passed." The `conventions_*` and `debt_split` ctest entries are self-tests of the checker scripts and were not run separately. - `python3 tools/pr_overlap.py --branch feat/lci-string` found no open PR with the same exports or new files. It failed closed for PRs it could not compare automatically (5021, 13486, 13778, 13782, 13881, 14085, 14212, 14401, 14837, 14936). The added lines of each were checked by hand: none contains `_ZNSs`, `_ZNKSs` or any of these NIDs. PR 14158 touches neighbouring lines in `libSceLibcInternal/CMakeLists.txt` and the debt file, and PRs 14373 and 14834 touch neighbouring lines in `include/Api.hpp`. - No title run. ### 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] Fixes: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes 🤖 Generated with [Claude Code](https://claude.com/claude-code)
ahmadmuj commented 2026-11-06 08:03:10 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by ahmadmuj on 2026-11-06T08:03:10Z.

Review: COMMENTED


Checked head d887251dd09150e78aa57a56e6be070b5b27a409, PR base ad6cb554308afba4bfc3664ebb961f9a22885b4b, on Linux/WSL with GCC 13.3. The existing guest_libc_internal_basic_string test and conventions check pass. A separate probe calls the actual assignment export through the built libSceLibcInternal/libc/libkernel modules.

The pointer/count overload at BasicString.cpp:114-116 loses a character when an alias range includes the existing terminator. For "abc", assign(data()+1, 3) returns size 2, with bytes 62 63. It should return size 3, with bytes 62 63 00, plus its final terminator. The delegated substring overload clamps the count to size - position. The cited STL pointer/count implementation preserves the full count.

The probe also gets size 15 instead of 16 for an inline 15-byte string with offset 0/count 16, and 19 instead of 20 for a heap string with offset 1/count 20. All ranges use initialized bytes plus the existing terminator. Six controls pass. A temporary correction that snapshots the source before assignment makes all nine cases pass; restoring the head restores the three failures.

Could you preserve the pointer/count range across overlap and growth, and add these cases? No console or allocation-policy claim here.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5797353943 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/15692#pullrequestreview-5797353943). Originally posted by [ahmadmuj](https://github.com/ahmadmuj) on 2026-11-06T08:03:10Z. **Review:** COMMENTED --- Checked head `d887251dd09150e78aa57a56e6be070b5b27a409`, PR base `ad6cb554308afba4bfc3664ebb961f9a22885b4b`, on Linux/WSL with GCC 13.3. The existing `guest_libc_internal_basic_string` test and conventions check pass. A separate probe calls the actual assignment export through the built libSceLibcInternal/libc/libkernel modules. The pointer/count overload at `BasicString.cpp:114-116` loses a character when an alias range includes the existing terminator. For `"abc"`, `assign(data()+1, 3)` returns size 2, with bytes `62 63`. It should return size 3, with bytes `62 63 00`, plus its final terminator. The delegated substring overload clamps the count to `size - position`. The [cited STL pointer/count implementation](https://github.com/microsoft/STL/blob/e1ed302683e5e822f663fbb0f44cee7e9ddc6af0/stl/inc/xstring#L3075-L3100) preserves the full count. The probe also gets size 15 instead of 16 for an inline 15-byte string with offset 0/count 16, and 19 instead of 20 for a heap string with offset 1/count 20. All ranges use initialized bytes plus the existing terminator. Six controls pass. A temporary correction that snapshots the source before assignment makes all nine cases pass; restoring the head restores the three failures. Could you preserve the pointer/count range across overlap and growth, and add these cases? No console or allocation-policy claim here.
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!15692
No description provided.