Skip to content

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

Merged
oneandonlydean merged 1 commit into
boykopovar:mainfrom
GiannisG1:feat/libcinternal-streambuf-xsgetn-xsputn
Nov 20, 2026
Merged

oneandonlydean merged 1 commit into
boykopovar:mainfrom
GiannisG1:feat/libcinternal-streambuf-xsgetn-xsputn

Conversation

@GiannisG1

@GiannisG1 GiannisG1 commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

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.

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

@PrivacyOnion PrivacyOnion left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

basic_streambuf<char> and <wchar_t>::xsgetn and xsputn were
parameterless inventory placeholders that threw, so a guest stream
buffer that inherits them failed on its first bulk read or write
(istream::read, ostream::write). They now follow Microsoft STL's
basic_streambuf (stl/inc/streambuf): copy what the get or put area
holds, then move one element at a time through the virtual uflow or
overflow until it returns end-of-file, and return the count moved. The
areas are the indirect next and end pointers time_get and time_put
already use. Their mangled names hash to the inventory NIDs, so the
exports are named.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@github-actions

github-actions Bot commented Nov 19, 2026 •

Copy link
Copy Markdown

Progress report (aa698cd → c511705)

📈 System libraries: 44.52% (+0.01%, +4 implemented, -4 removed functions)

✅ 4 implemented
Library Name
libSceLibcInternal _ZNSt15basic_streambufIcSt11char_traitsIcEE6xsgetnEPci_nid_postfix
libSceLibcInternal _ZNSt15basic_streambufIcSt11char_traitsIcEE6xsputnEPKci_nid_postfix
libSceLibcInternal _ZNSt15basic_streambufIwSt11char_traitsIwEE6xsgetnEPwi_nid_postfix
libSceLibcInternal _ZNSt15basic_streambufIwSt11char_traitsIwEE6xsputnEPKwi_nid_postfix
🗑️ 4 removed
Library Name
libSceLibcInternal libSceLibcInternalInventory_045_nid_no_patch
libSceLibcInternal libSceLibcInternalInventory_046_nid_no_patch
libSceLibcInternal libSceLibcInternalInventory_048_nid_no_patch
libSceLibcInternal libSceLibcInternalInventory_049_nid_no_patch

Changed libraries (implemented exports)

Library Before After Change
libSceLibcInternal 586/1131 (51.81%) 590/1131 (52.17%) +0.35%

@PrivacyOnion PrivacyOnion left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants