feat(libSceLibcInternal): count std::ios_base::Init and std::_Winit objects #21832

Merged
B-Howell merged 2 commits from upstream-pr/21832 into main 2026-11-16 21:08:20 +00:00
B-Howell commented 2026-11-16 20:05:56 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by B-Howell on 2026-11-16T20:05:56Z.


What

Implements four libSceLibcInternal exports that threw: the std::ios_base::Init and std::_Winit complete-object constructors and destructors (_ZNSt8ios_base4InitC1Ev sqWytnhYdEg, _ZNSt8ios_base4InitD1Ev kxXCvcat1cM, _ZNSt6_WinitC1Ev -Bl9-SZ2noc, _ZNSt6_WinitD1Ev Uw3OTZFPNt4).

  • Each class keeps a count that starts at -1. A construction sets it to 1 when it is negative and adds 1 otherwise. A destruction subtracts 1, and when the count reaches 0 it flushes the standard output and error streams. Both counts are updated under one lock and the flush runs outside it.
  • On the console the first construction also builds the library's own cin/cout/cerr/clog (or the wide ones), and the last destruction flushes them. This library has no such objects and exports none, so construction only counts and the last destruction flushes libc's _Stdout and _Stderr, the files those streams wrap. The debt fragment records this gap.
  • Sources: Dinkumware's iostream.cpp L9-L36 (int ios_base::Init::_Init_cnt = -1;, if (0 <= _Init_cnt) { ++_Init_cnt; } else { _Init_cnt = 1; }, if (--_Init_cnt == 0) { // flush standard streams) and wiostrea.cpp L9-L33, the same for _Winit. The libc.prx bundled with CUSA00900 exports the same four symbols. Its disassembly (read, not run) has the same count update under one library lock, an initial count of -1, construction of the streams over stdin, stdout and stderr on the first construction, and a flush of cout, cerr and clog (wide: wcout, wcerr, wclog) outside the lock on the last destruction.
  • The C2 and D2 aliases are not imported by this title and still throw.
  • The four functions keep their existing declarations. Their signatures change from int() to void(void*), which is the constructor and destructor ABI. _ZNSt6_WinitD1Ev moves from InventoryExports6.cpp to InventoryExports.cpp so that it shares its count with the constructor.
  • #17300 renames these four stubs without implementing them; whichever lands second takes over the other's lines. No other open PR touches these NIDs (checked with tools/pr_overlap.py --branch and the diffs of #13778, #14834, #18135 and #19602).

Tested

Windows 11 Pro 26300, WinLibs GCC 15.2.0 r7, Release, build-win.

  • New guest_libc_internal_standard_stream_init: guest stdout is reopened to a fully buffered file in the test working directory and given text. For each class the test checks that a destruction before any construction flushes nothing, that two constructions and one destruction flush nothing, that the last destruction flushes the text to the file, and that the object is left untouched.
    • This branch: passes, 3/3 (--repeat until-fail:3).
    • main's InventoryExports sources with the same test: fails with terminate called after throwing an instance of 'std::runtime_error' / libSceLibcInternalInventory_145_nid_no_patch not implemented.
  • fcd5d849b6e (after PrivacyOnion's Linux run): the test opened its file by a host absolute temp path, which the guest path resolver does not map on Linux, so freopen failed and the test aborted. It now uses a relative name, as GuestAppendStreams and GuestAtomicAssert do. WSL Ubuntu, GCC, Release, Ninja: at 6a8df00c3d4 it aborts with the guest standard output opens the file; at fcd5d849b6e guest_libc_internal_standard_stream_init and guest_libc_internal_ios_base pass 3/3. Windows build-win at fcd5d849b6e: both pass 3/3. No library code changed.
  • tools/progress.py --module libSceLibcInternal: 518 done / 613 to do on main and 522 / 609 here (+4).
  • python3 tools/check_conventions.py --base origin/main passes. tools/debt_fold.py --check passes for this fragment. On current main it reports docs-nkwebkit-drop-readded-count-lines.md:5, which this PR does not touch.
  • Import evidence: Bloodborne CUSA00900 01.09 (my own copy) was relinked with #17105 and #17711 merged into main. With tools/import_audit.py --modules on the eboot registry, its bundled libSceSmart.prx imports all four from libSceLibcInternal, and they were listed as stub. With this change they are implemented.

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: CUSA00900 Bloodborne (no Game bring-up discussion yet; PS4 work is tracked in #3093). Import audit: its libSceSmart.prx imports sqWytnhYdEg, kxXCvcat1cM, -Bl9-SZ2noc and Uw3OTZFPNt4, all stub on main.
  • GPUs tested: n/a
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5344187090 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/21832). Originally posted by [B-Howell](https://github.com/B-Howell) on 2026-11-16T20:05:56Z. --- ### What Implements four `libSceLibcInternal` exports that threw: the `std::ios_base::Init` and `std::_Winit` complete-object constructors and destructors (`_ZNSt8ios_base4InitC1Ev` `sqWytnhYdEg`, `_ZNSt8ios_base4InitD1Ev` `kxXCvcat1cM`, `_ZNSt6_WinitC1Ev` `-Bl9-SZ2noc`, `_ZNSt6_WinitD1Ev` `Uw3OTZFPNt4`). - Each class keeps a count that starts at -1. A construction sets it to 1 when it is negative and adds 1 otherwise. A destruction subtracts 1, and when the count reaches 0 it flushes the standard output and error streams. Both counts are updated under one lock and the flush runs outside it. - On the console the first construction also builds the library's own `cin`/`cout`/`cerr`/`clog` (or the wide ones), and the last destruction flushes them. This library has no such objects and exports none, so construction only counts and the last destruction flushes libc's `_Stdout` and `_Stderr`, the files those streams wrap. The debt fragment records this gap. - Sources: Dinkumware's [iostream.cpp L9-L36](https://github.com/microsoft/STL/blob/54d74d7a58e828857a73f979eccc2ba1c02e256d/stl/src/iostream.cpp#L9-L36) (`int ios_base::Init::_Init_cnt = -1;`, `if (0 <= _Init_cnt) { ++_Init_cnt; } else { _Init_cnt = 1; }`, `if (--_Init_cnt == 0) { // flush standard streams`) and [wiostrea.cpp L9-L33](https://github.com/microsoft/STL/blob/54d74d7a58e828857a73f979eccc2ba1c02e256d/stl/src/wiostrea.cpp#L9-L33), the same for `_Winit`. The `libc.prx` bundled with CUSA00900 exports the same four symbols. Its disassembly (read, not run) has the same count update under one library lock, an initial count of -1, construction of the streams over stdin, stdout and stderr on the first construction, and a flush of `cout`, `cerr` and `clog` (wide: `wcout`, `wcerr`, `wclog`) outside the lock on the last destruction. - The C2 and D2 aliases are not imported by this title and still throw. - The four functions keep their existing declarations. Their signatures change from `int()` to `void(void*)`, which is the constructor and destructor ABI. `_ZNSt6_WinitD1Ev` moves from `InventoryExports6.cpp` to `InventoryExports.cpp` so that it shares its count with the constructor. - #17300 renames these four stubs without implementing them; whichever lands second takes over the other's lines. No other open PR touches these NIDs (checked with `tools/pr_overlap.py --branch` and the diffs of #13778, #14834, #18135 and #19602). ### Tested Windows 11 Pro 26300, WinLibs GCC 15.2.0 r7, Release, `build-win`. - New `guest_libc_internal_standard_stream_init`: guest stdout is reopened to a fully buffered file in the test working directory and given text. For each class the test checks that a destruction before any construction flushes nothing, that two constructions and one destruction flush nothing, that the last destruction flushes the text to the file, and that the object is left untouched. - This branch: passes, 3/3 (`--repeat until-fail:3`). - `main`'s `InventoryExports` sources with the same test: fails with `terminate called after throwing an instance of 'std::runtime_error'` / `libSceLibcInternalInventory_145_nid_no_patch not implemented`. - `fcd5d849b6e` (after PrivacyOnion's Linux run): the test opened its file by a host absolute temp path, which the guest path resolver does not map on Linux, so `freopen` failed and the test aborted. It now uses a relative name, as `GuestAppendStreams` and `GuestAtomicAssert` do. WSL Ubuntu, GCC, Release, Ninja: at `6a8df00c3d4` it aborts with `the guest standard output opens the file`; at `fcd5d849b6e` `guest_libc_internal_standard_stream_init` and `guest_libc_internal_ios_base` pass 3/3. Windows `build-win` at `fcd5d849b6e`: both pass 3/3. No library code changed. - `tools/progress.py --module libSceLibcInternal`: 518 done / 613 to do on `main` and 522 / 609 here (+4). - `python3 tools/check_conventions.py --base origin/main` passes. `tools/debt_fold.py --check` passes for this fragment. On current `main` it reports `docs-nkwebkit-drop-readded-count-lines.md:5`, which this PR does not touch. - Import evidence: Bloodborne CUSA00900 01.09 (my own copy) was relinked with #17105 and #17711 merged into `main`. With `tools/import_audit.py --modules` on the eboot registry, its bundled `libSceSmart.prx` imports all four from `libSceLibcInternal`, and they were listed as `stub`. With this change they are implemented. ### 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: CUSA00900 Bloodborne (no Game bring-up discussion yet; PS4 work is tracked in #3093). Import audit: its `libSceSmart.prx` imports `sqWytnhYdEg`, `kxXCvcat1cM`, `-Bl9-SZ2noc` and `Uw3OTZFPNt4`, all `stub` on `main`. - [x] GPUs tested: n/a - [x] AI-assisted: yes
PrivacyOnion commented 2026-11-16 20:11:11 +00:00 (Migrated from github.com)

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

Review: CHANGES_REQUESTED


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

  • Conventions: tools/check_conventions.py passed.
  • Build: guest_libc_internal_standard_stream_init_tests built cleanly.
  • CTest (guest_libc_internal_standard_stream_init):
    1/1 Test #4040: guest_libc_internal_standard_stream_init ...Subprocess aborted***Exception:   0.22 sec
    0% tests passed, 1 tests failed out of 1
    
    The following tests FAILED:
    

Requesting changes for the points above.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5926787959 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/21832#pullrequestreview-5926787959). Originally posted by [PrivacyOnion](https://github.com/PrivacyOnion) on 2026-11-16T20:11:11Z. **Review:** CHANGES_REQUESTED --- Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against `6a8df00c3d`: - **Conventions**: `tools/check_conventions.py` passed. - **Build**: `guest_libc_internal_standard_stream_init_tests` built cleanly. - **CTest** (`guest_libc_internal_standard_stream_init`): ``` 1/1 Test #4040: guest_libc_internal_standard_stream_init ...Subprocess aborted***Exception: 0.22 sec 0% tests passed, 1 tests failed out of 1 ``` ``` The following tests FAILED: ``` Requesting changes for the points above.
PrivacyOnion commented 2026-11-16 20:37:37 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by PrivacyOnion on 2026-11-16T20:37:37Z.

Review: APPROVED


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

  • Conventions: tools/check_conventions.py passed.
  • Build: guest_libc_internal_standard_stream_init_tests built cleanly.
  • CTest (guest_libc_internal_standard_stream_init):
    1/1 Test #4040: guest_libc_internal_standard_stream_init ...   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/InventoryExports6.cpp, core/libs/prx/libSceLibcInternal/include/InventoryExports.hpp, core/libs/prx/libSceLibcInternal/include/InventoryExports6.hpp reverted to main, the new tests do not build/link without the change; restoring it makes them build and pass.
  • Code reading:
    • Verified InventoryExports.cpp CountStandardStreamInit and ReleaseStandardStreamInit correctly implement Dinkumware iostream Init count state machine (initially -1, sets to 1 if negative, increments otherwise; decrements and flushes when reaching 0).
    • Verified mutex synchronization: standardStreamInitMutex serializes updates to narrowStreamInitCount and wideStreamInitCount, and stream flushing is safely executed outside the lock.
    • Verified signature and NID mapping for _ZNSt6_WinitD1Ev (-Bl9-SZ2noc / Uw3OTZFPNt4) and std::ios_base::Init (sqWytnhYdEg / kxXCvcat1cM) with APS5_VABI calling convention and void(void*) signatures matching the ABI.
    • Verified debt fragment docs/dev/debt/changes/feat-libcinternal-standard-stream-init.md correctly removes previous stubs and adds functional documentation with upstream reference and known gap explanation.
    • Verified test GuestLibcInternalStandardStreamInit.cpp uses relative file path and tests multiple constructions, early destruction, and final flush verification with cleanup.

Looks Good To Me

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5927186142 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/21832#pullrequestreview-5927186142). Originally posted by [PrivacyOnion](https://github.com/PrivacyOnion) on 2026-11-16T20:37:37Z. **Review:** APPROVED --- Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against `fcd5d849b6`: - **Conventions**: `tools/check_conventions.py` passed. - **Build**: `guest_libc_internal_standard_stream_init_tests` built cleanly. - **CTest** (`guest_libc_internal_standard_stream_init`): ``` 1/1 Test #4040: guest_libc_internal_standard_stream_init ... 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/InventoryExports6.cpp`, `core/libs/prx/libSceLibcInternal/include/InventoryExports.hpp`, `core/libs/prx/libSceLibcInternal/include/InventoryExports6.hpp` reverted to `main`, the new tests do not build/link without the change; restoring it makes them build and pass. - **Code reading**: - Verified InventoryExports.cpp CountStandardStreamInit and ReleaseStandardStreamInit correctly implement Dinkumware iostream Init count state machine (initially -1, sets to 1 if negative, increments otherwise; decrements and flushes when reaching 0). - Verified mutex synchronization: standardStreamInitMutex serializes updates to narrowStreamInitCount and wideStreamInitCount, and stream flushing is safely executed outside the lock. - Verified signature and NID mapping for _ZNSt6_WinitD1Ev (-Bl9-SZ2noc / Uw3OTZFPNt4) and std::ios_base::Init (sqWytnhYdEg / kxXCvcat1cM) with APS5_VABI calling convention and void(void*) signatures matching the ABI. - Verified debt fragment docs/dev/debt/changes/feat-libcinternal-standard-stream-init.md correctly removes previous stubs and adds functional documentation with upstream reference and known gap explanation. - Verified test GuestLibcInternalStandardStreamInit.cpp uses relative file path and tests multiple constructions, early destruction, and final flush verification with cleanup. 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!21832
No description provided.