fix(libSceLibcInternal): copy _Mtxinit's name in through checked guest copies #23161

Merged
milescoviello merged 1 commit from upstream-pr/23161 into main 2026-11-27 15:28:24 +00:00
milescoviello commented 2026-11-18 19:32:39 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by milescoviello on 2026-11-18T19:32:39Z.


What

_Mtxinit labels its mutex SceLibcI_<name> with snprintf("%s", name), which read the caller's name through the guest pointer, so an unmapped name crashed the host.

Changes:

  • The name is copied in first, at most MutexNameSize (32) bytes, all the label can use.
    • A name with a terminator in those bytes is copied with CopyGuestString_nid_no_patch.
    • A longer name has its first 32 bytes copied with one GuestCopyIn_nid_no_patch, so it is still truncated as before.
  • The label is formatted from the copy.
  • An unreadable name returns 1, as the function's other failures do, before any attribute or mutex is created.

Labels for readable names, including truncated long ones, are unchanged.

Tested

Linux (Ubuntu 26.04.1, kernel 7.0), GCC 15.2, Ninja, Release, -DBUILD_TESTING=ON:

  • guest_libc_internal_mtx gains an unmapped (0x10) name: _Mtxinit returns 1 and leaves the mutex handle null.
  • guest_libc_internal_mtx passes, 3 runs, on d3d109bb43, the pre-squash commit with this tree.
  • Negative control: the same tree with main's Mutex.cpp ends guest_libc_internal_mtx in fatal SIGSEGV (code 1) at address 0x0000000000000010.

Windows 11 VM (GeForce GT 1030, WinLibs GCC 16.2.0 POSIX UCRT, Ninja, Release):

  • guest_libc_internal_mtx passes, 3 runs, on d3d109bb43.
  • Negative control: main with only the new test (70360b8278) ends guest_libc_internal_mtx in an access violation (0xc0000005).

Both:

  • git merge-tree against main 92231f2168 is clean. No open PR edits Mutex.cpp or the test file.
  • tools/check_conventions.py passes on the branch and this body. tools/debt_fold.py --check reports nothing for this fragment.
  • ycbuzi's CI runs of 2b04e8b990 merged into main (the whole ctest suite, WinLibs GCC 15.2.0, GitHub-hosted runners): on Windows Server 2025 all 6282 tests pass in both runs; on Windows Server 2022 the two runs have 4 and 3 failures out of 6282, and ycbuzi's report says 3 of the failing tests also fail on main there.
  • Romannachwievor's macOS run of 2b04e8b990 on an Apple M1 (macOS 26.5, Apple clang 21.0.0, x86_64 Release under Rosetta 2, main 245210b07e with #17425 and #11049 for the macOS build): guest_libc_internal_mtx passes 3 of 3 with this PR, and 3 of 3 on main with main's tests.

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:5382729474 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/23161). Originally posted by [milescoviello](https://github.com/milescoviello) on 2026-11-18T19:32:39Z. --- ### What `_Mtxinit` labels its mutex `SceLibcI_<name>` with `snprintf("%s", name)`, which read the caller's name through the guest pointer, so an unmapped name crashed the host. Changes: - The name is copied in first, at most `MutexNameSize` (32) bytes, all the label can use. - A name with a terminator in those bytes is copied with `CopyGuestString_nid_no_patch`. - A longer name has its first 32 bytes copied with one `GuestCopyIn_nid_no_patch`, so it is still truncated as before. - The label is formatted from the copy. - An unreadable name returns 1, as the function's other failures do, before any attribute or mutex is created. Labels for readable names, including truncated long ones, are unchanged. ### Tested Linux (Ubuntu 26.04.1, kernel 7.0), GCC 15.2, Ninja, Release, `-DBUILD_TESTING=ON`: - `guest_libc_internal_mtx` gains an unmapped (`0x10`) name: `_Mtxinit` returns 1 and leaves the mutex handle null. - `guest_libc_internal_mtx` passes, 3 runs, on `d3d109bb43`, the pre-squash commit with this tree. - Negative control: the same tree with `main`'s `Mutex.cpp` ends `guest_libc_internal_mtx` in `fatal SIGSEGV (code 1) at address 0x0000000000000010`. Windows 11 VM (GeForce GT 1030, WinLibs GCC 16.2.0 POSIX UCRT, Ninja, Release): - `guest_libc_internal_mtx` passes, 3 runs, on `d3d109bb43`. - Negative control: `main` with only the new test (`70360b8278`) ends `guest_libc_internal_mtx` in an access violation (`0xc0000005`). Both: - `git merge-tree` against `main` `92231f2168` is clean. No open PR edits `Mutex.cpp` or the test file. - `tools/check_conventions.py` passes on the branch and this body. `tools/debt_fold.py --check` reports nothing for this fragment. - ycbuzi's [CI runs](https://github.com/boykopovar/AnyPS5/pull/23161#issuecomment-6999528335) of `2b04e8b990` merged into `main` (the whole ctest suite, WinLibs GCC 15.2.0, GitHub-hosted runners): on Windows Server 2025 all 6282 tests pass in both runs; on Windows Server 2022 the two runs have 4 and 3 failures out of 6282, and ycbuzi's report says 3 of the failing tests also fail on `main` there. - Romannachwievor's [macOS run](https://github.com/boykopovar/AnyPS5/pull/23161#issuecomment-7004265262) of `2b04e8b990` on an Apple M1 (macOS 26.5, Apple clang 21.0.0, x86_64 Release under Rosetta 2, `main` `245210b07e` with #17425 and #11049 for the macOS build): `guest_libc_internal_mtx` passes 3 of 3 with this PR, and 3 of 3 on `main` with `main`'s tests. ### 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-18 19:43:58 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by PrivacyOnion on 2026-11-18T19:43:58Z.

Review: APPROVED


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

  • Conventions: tools/check_conventions.py passed.
  • Build: guest_libc_internal_mtx_tests built cleanly.
  • CTest (guest_libc_internal_mtx):
    1/1 Test #4225: guest_libc_internal_mtx ..........   Passed    0.01 sec
    100% tests passed, 0 tests failed out of 1
    
  • Negative control: with core/libs/prx/libSceLibcInternal/Mutex.cpp reverted to main and the new tests kept, the suite fails:
    1/1 Test #4225: guest_libc_internal_mtx ..........***Exception: SegFault  0.40 sec
    0% tests passed, 1 tests failed out of 1
    
    Restoring the change makes it pass again.
  • Code reading:
    • Verified _Mtxinit_nid_postfix correctly validates name != nullptr before calling CopyGuestString_nid_no_patch and handles truncation error (ENAMETOOLONG / 63) via GuestCopyIn_nid_no_patch in core/libs/prx/libSceLibcInternal/Mutex.cpp
    • Verified that invalid guest pointers return 1 immediately before any pthread attribute or mutex allocation, preventing host segmentation faults
    • Verified snprintf format buffer boundaries (MutexNameSize = 32) and null termination of copied string
    • Verified proper lifecycle cleanup in error branches (scePthreadMutexattrDestroy on settype failure and release after mutex init)
    • Verified strict AGENTS.md compliance, including debt tracking fragment docs/dev/debt/changes/fix-libcinternal-mtxinit-name-guest-copy.md and absence of illegal comments
    • Verified test coverage in core/libs/tests/GuestLibcInternalMtx.cpp exercising unreadable pointer (0x10) and long name truncation

Looks Good To Me

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5963810356 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/23161#pullrequestreview-5963810356). Originally posted by [PrivacyOnion](https://github.com/PrivacyOnion) on 2026-11-18T19:43:58Z. **Review:** APPROVED --- Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against `2b04e8b990`: - **Conventions**: `tools/check_conventions.py` passed. - **Build**: `guest_libc_internal_mtx_tests` built cleanly. - **CTest** (`guest_libc_internal_mtx`): ``` 1/1 Test #4225: guest_libc_internal_mtx .......... Passed 0.01 sec 100% tests passed, 0 tests failed out of 1 ``` - **Negative control**: with `core/libs/prx/libSceLibcInternal/Mutex.cpp` reverted to `main` and the new tests kept, the suite fails: ``` 1/1 Test #4225: guest_libc_internal_mtx ..........***Exception: SegFault 0.40 sec 0% tests passed, 1 tests failed out of 1 ``` Restoring the change makes it pass again. - **Code reading**: - Verified _Mtxinit_nid_postfix correctly validates name != nullptr before calling CopyGuestString_nid_no_patch and handles truncation error (ENAMETOOLONG / 63) via GuestCopyIn_nid_no_patch in core/libs/prx/libSceLibcInternal/Mutex.cpp - Verified that invalid guest pointers return 1 immediately before any pthread attribute or mutex allocation, preventing host segmentation faults - Verified snprintf format buffer boundaries (MutexNameSize = 32) and null termination of copied string - Verified proper lifecycle cleanup in error branches (scePthreadMutexattrDestroy on settype failure and release after mutex init) - Verified strict AGENTS.md compliance, including debt tracking fragment docs/dev/debt/changes/fix-libcinternal-mtxinit-name-guest-copy.md and absence of illegal comments - Verified test coverage in core/libs/tests/GuestLibcInternalMtx.cpp exercising unreadable pointer (0x10) and long name truncation Looks Good To Me
MarcZX10 commented 2026-11-18 23:49:32 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-18T23:49:32Z.

Review: APPROVED


Reviewed — looks good

The checked copy handles unreadable names before mutex creation, and the 32-byte fallback preserves the previous truncation behavior at the reviewed head.

What I ran

Reviewed head 2b04e8b990de7dc693d5009ccff860c1e732d0f7. No commands or tests were run; the reported Linux/Windows runs and negative controls are author-reported only. The changed files listed in the manifest were all read: core/libs/prx/libSceLibcInternal/Mutex.cpp, core/libs/tests/GuestLibcInternalMtx.cpp, and docs/dev/debt/changes/fix-libcinternal-mtxinit-name-guest-copy.md. Also read the relevant helper implementation in core/libs/prx/libc/src/General.cpp. No excluded-file inventory was available in the prepared review materials.

Results

Check Result Note
Changed-file review passed All three changed files read
Test execution not run Runs and negative controls reported by author
Console behavior not run Inaccessible-name result is explicitly unmeasured

Findings

Blocking

None.

Non-blocking

None.

Not verified

Builds, test execution, negative controls, merge preview, and physical-console behavior were not verified in this review. The author reports Linux and Windows test runs; those are not runs performed here.

To get this to a good state

Nothing is required.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5967085009 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/23161#pullrequestreview-5967085009). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-11-18T23:49:32Z. **Review:** APPROVED --- # Reviewed — looks good The checked copy handles unreadable names before mutex creation, and the 32-byte fallback preserves the previous truncation behavior at the reviewed head. ## What I ran Reviewed head `2b04e8b990de7dc693d5009ccff860c1e732d0f7`. No commands or tests were run; the reported Linux/Windows runs and negative controls are author-reported only. The changed files listed in the manifest were all read: `core/libs/prx/libSceLibcInternal/Mutex.cpp`, `core/libs/tests/GuestLibcInternalMtx.cpp`, and `docs/dev/debt/changes/fix-libcinternal-mtxinit-name-guest-copy.md`. Also read the relevant helper implementation in `core/libs/prx/libc/src/General.cpp`. No excluded-file inventory was available in the prepared review materials. ## Results | Check | Result | Note | |---|---|---| | Changed-file review | passed | All three changed files read | | Test execution | not run | Runs and negative controls reported by author | | Console behavior | not run | Inaccessible-name result is explicitly unmeasured | ## Findings ### Blocking None. ### Non-blocking None. ## Not verified Builds, test execution, negative controls, merge preview, and physical-console behavior were not verified in this review. The author reports Linux and Windows test runs; those are not runs performed here. ## To get this to a good state Nothing is required.
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!23161
No description provided.