fix(libSceLibcInternal): take the end iterator of _Getloctxt by reference #23988

Merged
ToRaNek merged 1 commit from upstream-pr/23988 into main 2026-11-20 05:53:46 +00:00
ToRaNek commented 2026-11-20 00:12:38 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by ToRaNek on 2026-11-20T00:12:38Z.


What

std::_Getloctxt<char, istreambuf_iterator<char>> (bsohl1ZrRXE) was declared with its end iterator by value:

int _ZSt10_GetloctxtIcSt19istreambuf_iteratorIcSt11char_traitsIcEEEiRT0_S5_mPKT_(InputIterator* first, InputIterator last, std::size_t numFields, const char* ptr);

The symbol says otherwise. RT0_S5_ is two references, and it demangles to int std::_Getloctxt<char, std::istreambuf_iterator<char>>(std::istreambuf_iterator<char>&, std::istreambuf_iterator<char>&, unsigned long, char const*). The reading of firmware 13.42's libSceLibcInternal.sprx in #22452 says the same: "rdi the first iterator by reference, rsi the last iterator by reference, rdx a size_t field count, rcx the const char* field table".

GuestLocale::InputIterator is 16 bytes and trivially copyable, so by value it travels in two registers. A real caller puts &last in rsi, the count in rdx and the table in rcx; the export read the end iterator from rsi:rdx, the field count from rcx (the table's address) and the table from r8, which the caller never set. Every call from guest code was therefore wrong (this library's own time_get code calls the internal matcher directly and was not affected). The tests did not see it because they called the export through the same by-value prototype.

This PR takes the end iterator by pointer, as _Getint in the same file already does for both of its iterators, and returns -2 for a null one like the other null arguments. The matching itself is untouched.

Tested

Windows 11 IoT Enterprise LTSC 2024 (10.0.26100), WinLibs GCC 15.2.0 r7, Release, BUILD_TESTING=ON.

  • CheckGroup6Getloctxt in core/libs/tests/GuestLibcInternalTimeGetFields.cpp now calls the export with &endIter in all 29 calls, and a null end iterator is checked.
  • Red: on main 5b6fb5480c, with the test's function pointer cast to the prototype of the symbol (InputIterator*, InputIterator*, size_t, const char*), guest_libc_internal_time_get_fields dies with an access violation (0xc0000005) in the first call.
  • Green: with the fix, the same 29 calls through the real prototype give the results the test already expected, and ctest -R "^guest_libc_internal_(time|locale|facet|num|ctype|collate|messages|money)" passes 13 of 13 after a full rebuild.
  • python tools/check_conventions.py --base origin/main passes. No debt change: no entry describes the prototype.
  • python tools/pr_overlap.py --branch fix/libscelibcinternal-getloctxt-end-by-reference (2017 open) reports two textual neighbours, neither of which changes the prototype: #22462 rewrites the body line that passes the end iterator on (it copies the guest table and iterator safely and keeps &last), and #23809 adds two declarations right after this one in TimeGetFields.hpp. Whichever lands second needs a small rebase.

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
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5405254564 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/23988). Originally posted by [ToRaNek](https://github.com/ToRaNek) on 2026-11-20T00:12:38Z. --- ### What `std::_Getloctxt<char, istreambuf_iterator<char>>` (`bsohl1ZrRXE`) was declared with its end iterator **by value**: ```cpp int _ZSt10_GetloctxtIcSt19istreambuf_iteratorIcSt11char_traitsIcEEEiRT0_S5_mPKT_(InputIterator* first, InputIterator last, std::size_t numFields, const char* ptr); ``` The symbol says otherwise. `RT0_S5_` is two references, and it demangles to `int std::_Getloctxt<char, std::istreambuf_iterator<char>>(std::istreambuf_iterator<char>&, std::istreambuf_iterator<char>&, unsigned long, char const*)`. The reading of firmware 13.42's `libSceLibcInternal.sprx` in [#22452](https://github.com/boykopovar/AnyPS5/issues/22452#issuecomment-6971241155) says the same: "`rdi` the first iterator by reference, `rsi` the last iterator by reference, `rdx` a `size_t` field count, `rcx` the `const char*` field table". `GuestLocale::InputIterator` is 16 bytes and trivially copyable, so by value it travels in two registers. A real caller puts `&last` in `rsi`, the count in `rdx` and the table in `rcx`; the export read the end iterator from `rsi:rdx`, the field count from `rcx` (the table's address) and the table from `r8`, which the caller never set. Every call from guest code was therefore wrong (this library's own `time_get` code calls the internal matcher directly and was not affected). The tests did not see it because they called the export through the same by-value prototype. This PR takes the end iterator by pointer, as `_Getint` in the same file already does for both of its iterators, and returns -2 for a null one like the other null arguments. The matching itself is untouched. ### Tested Windows 11 IoT Enterprise LTSC 2024 (10.0.26100), WinLibs GCC 15.2.0 r7, Release, `BUILD_TESTING=ON`. - `CheckGroup6Getloctxt` in `core/libs/tests/GuestLibcInternalTimeGetFields.cpp` now calls the export with `&endIter` in all 29 calls, and a null end iterator is checked. - Red: on `main` `5b6fb5480c`, with the test's function pointer cast to the prototype of the symbol (`InputIterator*, InputIterator*, size_t, const char*`), `guest_libc_internal_time_get_fields` dies with an access violation (0xc0000005) in the first call. - Green: with the fix, the same 29 calls through the real prototype give the results the test already expected, and `ctest -R "^guest_libc_internal_(time|locale|facet|num|ctype|collate|messages|money)"` passes 13 of 13 after a full rebuild. - `python tools/check_conventions.py --base origin/main` passes. No debt change: no entry describes the prototype. - `python tools/pr_overlap.py --branch fix/libscelibcinternal-getloctxt-end-by-reference` (2017 open) reports two textual neighbours, neither of which changes the prototype: #22462 rewrites the body line that passes the end iterator on (it copies the guest table and iterator safely and keeps `&last`), and #23809 adds two declarations right after this one in `TimeGetFields.hpp`. Whichever lands second needs a small rebase. ### Checklist - [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
PrivacyOnion commented 2026-11-20 00:22:35 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by PrivacyOnion on 2026-11-20T00:22:35Z.

Review: APPROVED


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

  • Conventions: tools/check_conventions.py passed.
  • Build: guest_libc_internal_time_get_fields_tests built cleanly.
  • CTest (guest_libc_internal_time_get_fields):
    1/1 Test #4449: guest_libc_internal_time_get_fields ...   Passed    0.05 sec
    100% tests passed, 0 tests failed out of 1
    
  • Negative control: with core/libs/prx/libSceLibcInternal/TimeGetFields.cpp, core/libs/prx/libSceLibcInternal/include/TimeGetFields.hpp reverted to main, the new tests do not build/link without the change; restoring it makes them build and pass.
  • Code reading:
    • Verified demangled symbol and firmware calling convention: ZSt10_GetloctxtIcSt19istreambuf_iteratorIcSt11char_traitsIcEEEiRT0_S5_mPKT demangles to reference types for both iterators (first and last), matching GuestLocale::InputIterator* under APS5_VABI.
    • Checked null pointer validation: TimeGetFields.cpp:984 checks first == nullptr || last == nullptr || ptr == nullptr and returns -2 as expected.
    • Verified GetLocaleText call and implementation: GetLocaleText(first, last, numFields, ptr) accepts const GuestLocale::InputIterator last and handles iterator end/equality checking correctly without dangling pointer or copying issues.
    • Verified test coverage: GuestLibcInternalTimeGetFields.cpp exercises the updated function signature across normal matches, partial matches, mismatches, null pointers (including fn(&first, nullptr, 0, monthsTable) returning -2), and SIZE_MAX overflow.
    • Verified conventions and AGENTS.md rules: no code comments added, no unhandled inputs or title-specific workarounds introduced, and build/tests/negative control passed cleanly.

Looks Good To Me

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5985597602 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/23988#pullrequestreview-5985597602). Originally posted by [PrivacyOnion](https://github.com/PrivacyOnion) on 2026-11-20T00:22:35Z. **Review:** APPROVED --- Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against `ff85b6ccac`: - **Conventions**: `tools/check_conventions.py` passed. - **Build**: `guest_libc_internal_time_get_fields_tests` built cleanly. - **CTest** (`guest_libc_internal_time_get_fields`): ``` 1/1 Test #4449: guest_libc_internal_time_get_fields ... Passed 0.05 sec 100% tests passed, 0 tests failed out of 1 ``` - **Negative control**: with `core/libs/prx/libSceLibcInternal/TimeGetFields.cpp`, `core/libs/prx/libSceLibcInternal/include/TimeGetFields.hpp` reverted to `main`, the new tests do not build/link without the change; restoring it makes them build and pass. - **Code reading**: - Verified demangled symbol and firmware calling convention: _ZSt10_GetloctxtIcSt19istreambuf_iteratorIcSt11char_traitsIcEEEiRT0_S5_mPKT_ demangles to reference types for both iterators (first and last), matching GuestLocale::InputIterator* under APS5_VABI. - Checked null pointer validation: TimeGetFields.cpp:984 checks first == nullptr || last == nullptr || ptr == nullptr and returns -2 as expected. - Verified GetLocaleText call and implementation: GetLocaleText<char>(*first, last, numFields, ptr) accepts const GuestLocale::InputIterator* last and handles iterator end/equality checking correctly without dangling pointer or copying issues. - Verified test coverage: GuestLibcInternalTimeGetFields.cpp exercises the updated function signature across normal matches, partial matches, mismatches, null pointers (including fn(&first, nullptr, 0, monthsTable) returning -2), and SIZE_MAX overflow. - Verified conventions and AGENTS.md rules: no code comments added, no unhandled inputs or title-specific workarounds introduced, and build/tests/negative control passed cleanly. 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!23988
No description provided.