feat(libSceLibcInternal): implement basic_streambuf xsgetn and xsputn #23837

Merged
GiannisG1 merged 1 commit from upstream-pr/23837 into main 2026-11-20 10:01:11 +00:00
GiannisG1 commented 2026-11-19 18:35:50 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by GiannisG1 on 2026-11-19T18:35:50Z.


What

basic_streambuf<char> and basic_streambuf<wchar_t> xsgetn and xsputn (inventory 045, 046, 048 and 049, NIDs VpwymQiS4ck, sXaxl1QGorg, lZVehk7yFok, 041c37QfoUc) were parameterless placeholders that threw. A guest stream buffer that does not override them inherits these exports through its vtable (slots 9 and 10), so its first bulk transfer failed: istream::read, ostream::write, sgetn, sputn.

They now follow Microsoft STL's basic_streambuf (stl/inc/streambuf) and C++ [streambuf.virt.get]/[streambuf.virt.put]:

  • copy what the get or put area holds;
  • then move one element at a time through the virtual uflow (slot 8) or overflow (slot 4) until it returns end-of-file;
  • return the count moved. A count of 0 or less moves nothing.

overflow gets the element as a non-negative int_type (0xff and 0xffff are not end-of-file), as to_int_type gives.

Each mangled name hashes to its inventory NID (tools/nid_names.py), so the exports are named and the four placeholders are gone. The count is an int, as the mangled names give.

The get and put areas are the indirect next and end pointers that time_get (TimeGetFields.cpp) and time_put (LocaleSupport.cpp, read from the SDK libc.prx) already use: 0x38 and 0x58, 0x40 and 0x60. Microsoft STL keeps remaining counts there instead, so only the next pointer advances, as in those functions. End-of-file is -1 for the 16-bit wide form too, as in the library's other wide stream code. The new header is included by the library umbrella. The debt fragment removes the four 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_streambuf_transfer, with a fake guest stream buffer, for char and wchar_t:
    • xsgetn of 10 with a 4-element get area and 3 more from uflow returns 7 in order, writes nothing after them, empties the get area and calls uflow 4 times;
    • a count smaller than the area takes only that count and leaves uflow alone;
    • with no get area, everything comes from uflow;
    • xsputn of 8 with room for 4 and an overflow that takes 2 returns 6, fills the put area first and stops at the third overflow. 0xff (0xffff wide) reaches overflow as 255 (65535);
    • counts of 0 and -1, and a null range with count 0, return 0 and change nothing;
    • a null stream buffer, or a null range with a positive count, throws std::invalid_argument.
  • main: the test does not link, because none of the four exports exists there (their NIDs reach the throwing placeholders). This PR: passes 3/3.
  • Split suite (tools/suite.py): 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.
    • guest_libc_internal_streambuf_transfer and guest_libc_internal_time_get_fields pass.
    • The failures are those of the main baseline (HD 630 10, llvmpipe 3) plus two more:
      • agc_driver_pm4_order_signal on the GTX and llvmpipe. It checks for a median below 0.1 ms and does not load libSceLibcInternal. Interleaved with a main e4cee4952b build, 5 runs per GPU, both builds failed 7 of 15 (measured for #23809).
      • public_headers: the new header was missing from the library umbrella. The include was added, and public_headers, its 64 compile tests, every guest_libc_internal_* test and the conventions and checker tests (344) then passed on the rebuilt tree.
  • After the suite, the null checks changed from NotImplemented_nid_no_patch to std::invalid_argument (head c511705d5d). They are invalid arguments, not unimplemented paths, and tools/progress.py counts a reachable NotImplemented as a stub. On that tree, public_headers and its compile tests, every guest_libc_internal_* test and the conventions and checker tests (344) pass. progress.py --module libSceLibcInternal lists none of the four exports 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 four 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.

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:5400764732 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/23837). Originally posted by [GiannisG1](https://github.com/GiannisG1) on 2026-11-19T18:35:50Z. --- ### What `basic_streambuf<char>` and `basic_streambuf<wchar_t>` `xsgetn` and `xsputn` (inventory 045, 046, 048 and 049, NIDs `VpwymQiS4ck`, `sXaxl1QGorg`, `lZVehk7yFok`, `041c37QfoUc`) were parameterless placeholders that threw. A guest stream buffer that does not override them inherits these exports through its vtable (slots 9 and 10), so its first bulk transfer failed: `istream::read`, `ostream::write`, `sgetn`, `sputn`. They now follow Microsoft STL's [`basic_streambuf`](https://github.com/microsoft/STL/blob/770da5aa457af11f0a796600e1aa6f7f6fce14a6/stl/inc/streambuf) (`stl/inc/streambuf`) and C++ [streambuf.virt.get]/[streambuf.virt.put]: - copy what the get or put area holds; - then move one element at a time through the virtual `uflow` (slot 8) or `overflow` (slot 4) until it returns end-of-file; - return the count moved. A count of 0 or less moves nothing. `overflow` gets the element as a non-negative `int_type` (0xff and 0xffff are not end-of-file), as `to_int_type` gives. Each mangled name hashes to its inventory NID (`tools/nid_names.py`), so the exports are named and the four placeholders are gone. The count is an `int`, as the mangled names give. The get and put areas are the indirect next and end pointers that `time_get` (`TimeGetFields.cpp`) and `time_put` (`LocaleSupport.cpp`, read from the SDK `libc.prx`) already use: 0x38 and 0x58, 0x40 and 0x60. Microsoft STL keeps remaining counts there instead, so only the next pointer advances, as in those functions. End-of-file is -1 for the 16-bit wide form too, as in the library's other wide stream code. The new header is included by the library umbrella. The debt fragment removes the four 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_streambuf_transfer`, with a fake guest stream buffer, for `char` and `wchar_t`: - `xsgetn` of 10 with a 4-element get area and 3 more from `uflow` returns 7 in order, writes nothing after them, empties the get area and calls `uflow` 4 times; - a count smaller than the area takes only that count and leaves `uflow` alone; - with no get area, everything comes from `uflow`; - `xsputn` of 8 with room for 4 and an `overflow` that takes 2 returns 6, fills the put area first and stops at the third `overflow`. 0xff (0xffff wide) reaches `overflow` as 255 (65535); - counts of 0 and -1, and a null range with count 0, return 0 and change nothing; - a null stream buffer, or a null range with a positive count, throws `std::invalid_argument`. - `main`: the test does not link, because none of the four exports exists there (their NIDs reach the throwing placeholders). This PR: passes 3/3. - Split suite (`tools/suite.py`): 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. - `guest_libc_internal_streambuf_transfer` and `guest_libc_internal_time_get_fields` pass. - The failures are those of the `main` baseline (HD 630 10, llvmpipe 3) plus two more: - `agc_driver_pm4_order_signal` on the GTX and llvmpipe. It checks for a median below 0.1 ms and does not load libSceLibcInternal. Interleaved with a `main` `e4cee4952b` build, 5 runs per GPU, both builds failed 7 of 15 (measured for #23809). - `public_headers`: the new header was missing from the library umbrella. The include was added, and `public_headers`, its 64 compile tests, every `guest_libc_internal_*` test and the conventions and checker tests (344) then passed on the rebuilt tree. - After the suite, the null checks changed from `NotImplemented_nid_no_patch` to `std::invalid_argument` (head `c511705d5d`). They are invalid arguments, not unimplemented paths, and `tools/progress.py` counts a reachable `NotImplemented` as a stub. On that tree, `public_headers` and its compile tests, every `guest_libc_internal_*` test and the conventions and checker tests (344) pass. `progress.py --module libSceLibcInternal` lists none of the four exports 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 four 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. ### 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 18:47:36 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by PrivacyOnion on 2026-11-19T18:47:36Z.

Review: APPROVED


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

  • Conventions: tools/check_conventions.py passed.
  • Build: guest_libc_internal_streambuf_transfer_tests built cleanly.
  • CTest (guest_libc_internal_streambuf_transfer):
    1/1 Test #4432: guest_libc_internal_streambuf_transfer ...   Passed    0.02 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/StreambufTransfer.cpp reverted to main, the new tests do not build/link without the change; restoring it makes them build and pass.
  • Code reading:
    • Validated StreambufTransfer.cpp GetElements and PutElements for negative and zero count handling (returns 0 immediately without dereferencing pointers or mutating state)
    • Verified Available calculation in StreambufTransfer.cpp: properly handles nullptr next/end, end <= next, pointer difference, and element byte scaling
    • Audited StreambufTransfer.cpp pointer arithmetic and std::memcpy bounds for both char and uint16_t (wchar_t) specializations
    • Audited virtual slot lookups and calling conventions (uflow slot 8, overflow slot 4 with APS5_VABI) matching Microsoft STL and AnyPS5 vtable conventions
    • Verified docs/dev/debt/changes/ fragment formatting and removal of placeholder InventoryExports 045, 046, 048, 049 from InventoryExports.cpp and InventoryExports.hpp
    • Checked CMakeLists.txt and GuestLibcInternalStreambufTransfer.cpp unit tests across edge cases (full buffer, partial buffer, EOF on uflow/overflow, zero/negative count, null stream/buffer exceptions)

Looks Good To Me

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5981198495 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/23837#pullrequestreview-5981198495). Originally posted by [PrivacyOnion](https://github.com/PrivacyOnion) on 2026-11-19T18:47:36Z. **Review:** APPROVED --- Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against `e85e771696`: - **Conventions**: `tools/check_conventions.py` passed. - **Build**: `guest_libc_internal_streambuf_transfer_tests` built cleanly. - **CTest** (`guest_libc_internal_streambuf_transfer`): ``` 1/1 Test #4432: guest_libc_internal_streambuf_transfer ... Passed 0.02 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/StreambufTransfer.cpp` reverted to `main`, the new tests do not build/link without the change; restoring it makes them build and pass. - **Code reading**: - Validated StreambufTransfer.cpp GetElements and PutElements for negative and zero count handling (returns 0 immediately without dereferencing pointers or mutating state) - Verified Available calculation in StreambufTransfer.cpp: properly handles nullptr next/end, end <= next, pointer difference, and element byte scaling - Audited StreambufTransfer.cpp pointer arithmetic and std::memcpy bounds for both char and uint16_t (wchar_t) specializations - Audited virtual slot lookups and calling conventions (uflow slot 8, overflow slot 4 with APS5_VABI) matching Microsoft STL and AnyPS5 vtable conventions - Verified docs/dev/debt/changes/ fragment formatting and removal of placeholder InventoryExports 045, 046, 048, 049 from InventoryExports.cpp and InventoryExports.hpp - Checked CMakeLists.txt and GuestLibcInternalStreambufTransfer.cpp unit tests across edge cases (full buffer, partial buffer, EOF on uflow/overflow, zero/negative count, null stream/buffer exceptions) Looks Good To Me
PrivacyOnion commented 2026-11-19 19:48:16 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by PrivacyOnion on 2026-11-19T19:48:16Z.

Review: APPROVED


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

  • Conventions: tools/check_conventions.py passed.
  • Build: guest_libc_internal_streambuf_transfer_tests built cleanly.
  • CTest (guest_libc_internal_streambuf_transfer):
    1/1 Test #4435: guest_libc_internal_streambuf_transfer ...   Passed    0.01 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/StreambufTransfer.cpp reverted to main, the new tests do not build/link without the change; restoring it makes them build and pass.
  • Code reading:
    • StreambufTransfer.cpp: verified Available() safely handles nullptr, *end <= *next, and computes integer division by element size without overflow or signed pointer subtraction issues
    • StreambufTransfer.cpp: verified GetElements and PutElements for count <= 0 immediately returning 0 without dereferencing pointers or modifying stream state
    • StreambufTransfer.cpp: checked vtable dispatch for CallUflow (slot 8) and CallOverflow (slot 4) matching Microsoft STL streambuf layout and APS5_VABI calling convention
    • StreambufTransfer.cpp: verified argument checking and throwing std::invalid_argument when self, self->vtable, or (count > 0 && destination/source == nullptr) are null
    • StreambufTransfer.cpp: verified wchar_t / std::uint16_t signed/unsigned promotion in CallOverflow and unsigned conversions in GetElements/PutElements
    • InventoryExports.cpp & include/InventoryExports.hpp: verified removal of old placeholders 045, 046, 048, 049 and correct replacement with exported mangled symbols
    • GuestLibcInternalStreambufTransfer.cpp: verified test coverage across bulk transfers, partial buffer reads/writes, uflow/overflow fallbacks, EOF termination, and null argument exceptions

Looks Good To Me

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5982176988 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/23837#pullrequestreview-5982176988). Originally posted by [PrivacyOnion](https://github.com/PrivacyOnion) on 2026-11-19T19:48:16Z. **Review:** APPROVED --- Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against `c511705d5d`: - **Conventions**: `tools/check_conventions.py` passed. - **Build**: `guest_libc_internal_streambuf_transfer_tests` built cleanly. - **CTest** (`guest_libc_internal_streambuf_transfer`): ``` 1/1 Test #4435: guest_libc_internal_streambuf_transfer ... Passed 0.01 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/StreambufTransfer.cpp` reverted to `main`, the new tests do not build/link without the change; restoring it makes them build and pass. - **Code reading**: - StreambufTransfer.cpp: verified Available() safely handles nullptr, *end <= *next, and computes integer division by element size without overflow or signed pointer subtraction issues - StreambufTransfer.cpp: verified GetElements and PutElements for count <= 0 immediately returning 0 without dereferencing pointers or modifying stream state - StreambufTransfer.cpp: checked vtable dispatch for CallUflow (slot 8) and CallOverflow (slot 4) matching Microsoft STL streambuf layout and APS5_VABI calling convention - StreambufTransfer.cpp: verified argument checking and throwing std::invalid_argument when self, self->vtable, or (count > 0 && destination/source == nullptr) are null - StreambufTransfer.cpp: verified wchar_t / std::uint16_t signed/unsigned promotion in CallOverflow and unsigned conversions in GetElements/PutElements - InventoryExports.cpp & include/InventoryExports.hpp: verified removal of old placeholders 045, 046, 048, 049 and correct replacement with exported mangled symbols - GuestLibcInternalStreambufTransfer.cpp: verified test coverage across bulk transfers, partial buffer reads/writes, uflow/overflow fallbacks, EOF termination, and null argument exceptions 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!23837
No description provided.