feat(libSceLibcInternal): implement filebuf locking #20834

Merged
fogydes merged 1 commit from upstream-pr/20834 into main 2026-11-17 08:39:21 +00:00
fogydes commented 2026-11-15 16:32:13 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by fogydes on 2026-11-15T16:32:13Z.


What

Implement the narrow and wide basic_filebuf::_Lock exports, which currently throw. Each reads the file pointer at this + 0xa8, leaves an empty file buffer unchanged, and acquires one level of the existing recursive guest file lock. The existing _Unlock pair shares the guarded pointer reader.

Source: SDK 10.00 libc.prx at 0x84560 (char) and 0xb4ae0 (wchar), calling _Lockfilelock at 0x35f0; target/include/fstream:304-307 confirms the void member signature. These contracts have not been measured on a console or verified on newer SDKs. Invalid or wrapping object addresses still throw as a host-safety exception.

NIDs: 9biiDDejX3Q, GYTma8zq0NU. Both match the pinned name database and are present after NID patching.

Coverage on base 2faa10f332b9: libSceLibcInternal remains 509 implemented / 622 TODO (1,131 total). Two throwing export bodies gain runtime behavior, but the conservative counter still follows the pointer-reader throwing paths; no numerical coverage increase is claimed.

Overlap audit: checked 1,549 open PRs. The tool returned exit 2 because five older PRs conflict with main; their actual diffs and unchanged heads were then reviewed manually. No other PR supplies working filebuf Lock behavior. #17300 renames these same aliases to parameterless throwing stubs: this is a real symbol/declaration collision, and a future merge must keep these typed void(void*) implementations rather than both providers. #13778 (trigonometric kernels), #14834 (exception constructors), #18135 (moneypunct), #20059 (Dinkum exception tests), and #19434 (forwarders/test registration) are functionally independent. Shared CMake append sites need ordinary integration; the existing libc _Lockfilelock backend means #19434 is not a prerequisite. Depends on: none describes the functional dependency, not an absence of textual overlap.

Tested

Latest Windows verification (2026-11-16): head 5259b362f5d7, base fc66bec8cdc5, includes upstream #21466. The preceding CI run passed every selected test except patched_libraries: PNG encoding introduced imports of compress2/compressBound from AgcDriver into VideoOut that were absent after patching; GnmDriver failed transitively. The upstream revert removes those imports. This PR adds no PNG/GPU workaround. Five focused local tests pass on this head (including the previously failing ICU executable, the PNG decoder, and filebuf neighbors), with clean generated build provenance. A new full patched-library CI check remains pending. The earlier validation and coverage figures below are identified by their historical base revisions.

Windows CI follow-up (2026-11-16): rebased onto 2faa10f332b9. The previous Windows job failed in the unrelated guest_icu_recovered_signatures_tests link because ICU test files defined duplicate GuestPointers::Run() symbols. Upstream #20844, commit 47d5b922208, gives those namespaces distinct names. The rebased branch builds that previously failing executable and passes it alongside the feature/neighbor tests (4/4). Generated build revision matches 0eb2cf6dc75c, MODIFIED=0; headers and conventions pass. The feature patch is preserved; no ICU workaround was added to this PR. Fresh GitHub checks run on the pushed revision.

Windows x64, WinLibs GCC 15.2, Ninja Release, BUILD_TESTING=ON. Built and passed guest_libc_internal_filebuf_lock, guest_libc_internal_filebuf_unlock, and guest_standard_streams (3/3), including a repeat after rebasing onto the stated main commit.

The new test checks null-file behavior and errno preservation, object/neighbor isolation, two recursive acquisitions, exclusion of another thread, and release through the existing Unlock exports. A no-op Lock negative control failed at the first call excludes another thread; restoring the implementation passed all three tests. Public-header audit, conventions, debt folding, whitespace checks and patched-NID verification passed. No title or console run; no performance claim.

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:5324772433 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/20834). Originally posted by [fogydes](https://github.com/fogydes) on 2026-11-15T16:32:13Z. --- ### What Implement the narrow and wide `basic_filebuf::_Lock` exports, which currently throw. Each reads the file pointer at `this + 0xa8`, leaves an empty file buffer unchanged, and acquires one level of the existing recursive guest file lock. The existing `_Unlock` pair shares the guarded pointer reader. Source: SDK 10.00 `libc.prx` at `0x84560` (char) and `0xb4ae0` (wchar), calling `_Lockfilelock` at `0x35f0`; `target/include/fstream:304-307` confirms the void member signature. These contracts have not been measured on a console or verified on newer SDKs. Invalid or wrapping object addresses still throw as a host-safety exception. NIDs: `9biiDDejX3Q`, `GYTma8zq0NU`. Both match the pinned name database and are present after NID patching. Coverage on base `2faa10f332b9`: `libSceLibcInternal` remains 509 implemented / 622 TODO (1,131 total). Two throwing export bodies gain runtime behavior, but the conservative counter still follows the pointer-reader throwing paths; no numerical coverage increase is claimed. Overlap audit: checked 1,549 open PRs. The tool returned exit 2 because five older PRs conflict with main; their actual diffs and unchanged heads were then reviewed manually. No other PR supplies working filebuf Lock behavior. #17300 renames these same aliases to parameterless throwing stubs: this is a real symbol/declaration collision, and a future merge must keep these typed void(void*) implementations rather than both providers. #13778 (trigonometric kernels), #14834 (exception constructors), #18135 (moneypunct), #20059 (Dinkum exception tests), and #19434 (forwarders/test registration) are functionally independent. Shared CMake append sites need ordinary integration; the existing libc `_Lockfilelock` backend means #19434 is not a prerequisite. `Depends on: none` describes the functional dependency, not an absence of textual overlap. ### Tested Latest Windows verification (2026-11-16): head `5259b362f5d7`, base `fc66bec8cdc5`, includes upstream [#21466](https://github.com/boykopovar/AnyPS5/pull/21466). The preceding CI run passed every selected test except `patched_libraries`: PNG encoding introduced imports of `compress2`/`compressBound` from AgcDriver into VideoOut that were absent after patching; GnmDriver failed transitively. The upstream revert removes those imports. This PR adds no PNG/GPU workaround. Five focused local tests pass on this head (including the previously failing ICU executable, the PNG decoder, and filebuf neighbors), with clean generated build provenance. A new full patched-library CI check remains pending. The earlier validation and coverage figures below are identified by their historical base revisions. Windows CI follow-up (2026-11-16): rebased onto `2faa10f332b9`. The previous Windows job failed in the unrelated `guest_icu_recovered_signatures_tests` link because ICU test files defined duplicate `GuestPointers::Run()` symbols. Upstream [#20844](https://github.com/boykopovar/AnyPS5/pull/20844), commit `47d5b922208`, gives those namespaces distinct names. The rebased branch builds that previously failing executable and passes it alongside the feature/neighbor tests (4/4). Generated build revision matches `0eb2cf6dc75c`, MODIFIED=0; headers and conventions pass. The feature patch is preserved; no ICU workaround was added to this PR. Fresh GitHub checks run on the pushed revision. <!-- OS, title or homebrew, what was checked. "Not tested" is a valid answer. A performance change: the summary lines of main and of this PR from the same machine and scene, with the traces attached, captured as the Performance report issue form describes. --> Windows x64, WinLibs GCC 15.2, Ninja Release, BUILD_TESTING=ON. Built and passed `guest_libc_internal_filebuf_lock`, `guest_libc_internal_filebuf_unlock`, and `guest_standard_streams` (3/3), including a repeat after rebasing onto the stated main commit. The new test checks null-file behavior and errno preservation, object/neighbor isolation, two recursive acquisitions, exclusion of another thread, and release through the existing Unlock exports. A no-op Lock negative control failed at `the first call excludes another thread`; restoring the implementation passed all three tests. Public-header audit, conventions, debt folding, whitespace checks and patched-NID verification passed. No title or console run; no performance claim. ### 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 <!-- #PR, or none --> - [x] Fixes: none <!-- issue it closes as Fixes #N, or none --> - [x] Unblocks: none <!-- title ID and Game bring-up discussion, e.g. PPSA01325 #3232, or none --> - [x] GPUs tested: n/a <!-- for AGC, shader recompiler or VideoOut changes: vendor, model, driver and OS of each GPU the GPU tests ran on; otherwise n/a --> - [x] AI-assisted: yes <!-- yes / no -->
PrivacyOnion commented 2026-11-16 02:06:11 +00:00 (Migrated from github.com)

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

Review: APPROVED


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

  • Conventions: tools/check_conventions.py passed; git diff --check clean.
  • Build: guest_libc_internal_filebuf_lock_tests built cleanly.
  • CTest (guest_libc_internal_filebuf_lock):
    1/1 Test #4029: guest_libc_internal_filebuf_lock ...   Passed    0.04 sec
    100% tests passed, 0 tests failed out of 1
    
  • Negative control: with core/libs/prx/libSceLibcInternal/Filebuf.cpp, core/libs/prx/libSceLibcInternal/InventoryExports.cpp, core/libs/prx/libSceLibcInternal/include/InventoryExports.hpp reverted to main, the new tests do not build/link without the change; restoring it makes them build and pass.
  • Code reading:
    • Verified FilebufStream_nid_no_patch in core/libs/prx/libSceLibcInternal/Filebuf.cpp:11-22 checks for address + 0xa8 overflow, preserves errno across GuestCopyIn_nid_no_patch, throws NotImplemented_nid_no_patch on unreadable memory, and returns the FileStream* at offset 0xa8.
    • Verified _ZNSt13basic_filebufIcSt11char_traitsIcEE5_LockEv_nid_postfix and _ZNSt13basic_filebufIwSt11char_traitsIwEE5_LockEv_nid_postfix in core/libs/prx/libSceLibcInternal/Filebuf.cpp:28-34 acquire the recursive file lock via _Lockfilelock_nid_postfix when the stream is non-null and are no-ops when the stream pointer is null.
    • Verified placeholder removal in core/libs/prx/libSceLibcInternal/InventoryExports.cpp:119-123 and 164-168 (NIDs 9biiDDejX3Q and GYTma8zq0NU) and updated declarations in core/libs/prx/libSceLibcInternal/include/InventoryExports.hpp:36,45.
    • Verified technical debt fragment in docs/dev/debt/changes/feat-libc-internal-filebuf-lock.md properly documents the unverified SDK 10.00 contract and updates the callable export inventory description.
    • Verified unit tests in core/libs/tests/GuestLibcInternalFilebufLock.cpp:46-86 test both narrow and wide _Lock/_Unlock pairs for null stream no-op behavior, errno preservation, offset 0xa8 isolation from adjacent pointers, cross-thread exclusion via ftrylockfile_nid_postfix, and recursive lock counting.

Looks Good To Me

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5914258304 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/20834#pullrequestreview-5914258304). Originally posted by [PrivacyOnion](https://github.com/PrivacyOnion) on 2026-11-16T02:06:11Z. **Review:** APPROVED --- Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against `7ea3602e31`: - **Conventions**: `tools/check_conventions.py` passed; `git diff --check` clean. - **Build**: `guest_libc_internal_filebuf_lock_tests` built cleanly. - **CTest** (`guest_libc_internal_filebuf_lock`): ``` 1/1 Test #4029: guest_libc_internal_filebuf_lock ... Passed 0.04 sec 100% tests passed, 0 tests failed out of 1 ``` - **Negative control**: with `core/libs/prx/libSceLibcInternal/Filebuf.cpp`, `core/libs/prx/libSceLibcInternal/InventoryExports.cpp`, `core/libs/prx/libSceLibcInternal/include/InventoryExports.hpp` reverted to `main`, the new tests do not build/link without the change; restoring it makes them build and pass. - **Code reading**: - Verified FilebufStream_nid_no_patch in core/libs/prx/libSceLibcInternal/Filebuf.cpp:11-22 checks for address + 0xa8 overflow, preserves errno across GuestCopyIn_nid_no_patch, throws NotImplemented_nid_no_patch on unreadable memory, and returns the FileStream* at offset 0xa8. - Verified _ZNSt13basic_filebufIcSt11char_traitsIcEE5_LockEv_nid_postfix and _ZNSt13basic_filebufIwSt11char_traitsIwEE5_LockEv_nid_postfix in core/libs/prx/libSceLibcInternal/Filebuf.cpp:28-34 acquire the recursive file lock via _Lockfilelock_nid_postfix when the stream is non-null and are no-ops when the stream pointer is null. - Verified placeholder removal in core/libs/prx/libSceLibcInternal/InventoryExports.cpp:119-123 and 164-168 (NIDs 9biiDDejX3Q and GYTma8zq0NU) and updated declarations in core/libs/prx/libSceLibcInternal/include/InventoryExports.hpp:36,45. - Verified technical debt fragment in docs/dev/debt/changes/feat-libc-internal-filebuf-lock.md properly documents the unverified SDK 10.00 contract and updates the callable export inventory description. - Verified unit tests in core/libs/tests/GuestLibcInternalFilebufLock.cpp:46-86 test both narrow and wide _Lock/_Unlock pairs for null stream no-op behavior, errno preservation, offset 0xa8 isolation from adjacent pointers, cross-thread exclusion via ftrylockfile_nid_postfix, and recursive lock counting. 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!20834
No description provided.