Skip to content

fix(libc): _Mtx_trylock returns 4 for a destroyed handle and locks a pointer to a null handle - #473

Open
Thimo188 wants to merge 14 commits into
mainfrom
upstream-pr/18347
Open

Thimo188 wants to merge 14 commits into
mainfrom
upstream-pr/18347

Conversation

@Thimo188

@Thimo188 Thimo188 commented Dec 2, 2026 •

Copy link
Copy Markdown
Owner

Imported from boykopovar/AnyPS5#18347, originally opened by xXPBSXx.

Source commit: 378dd39ab94171c3a616e8625daaf294d98e34d3. The import branch preserves this fetched upstream PR snapshot. The description and validation below are copied from upstream; no new code changes or test runs were made for this import. Original reviews, discussions, and attachments remain at the linked upstream PR.


What

_Mtx_trylock threw for the handle of a destroyed mutex and for a pointer to a null handle. On a PS5 it returns 4 (_Thrd_error) for the destroyed mutex's stale handle and 0 for a pointer to a null handle (cases 1.9 and 4.2, lines 11 and 22 of the raw output). A null pointer makes the console fault (case 4.1, line 21). _Mtx_destroy now leaves the address of a static marker in the handle, and _Mtx_trylock returns 4 for the marker and for a handle whose tag is not a live mutex's, which is what the console returned for the destroyed one. For a pointer to a null handle in writable memory it creates a plain mutex, stores it and locks it, returning 0; a null pointer and a null handle slot that cannot be written still throw, as on main. The handle is read with __atomic_load_n next to the compare-exchange that publishes the new mutex (Threads.cpp, ResolveMutex and _Mtx_trylock), and the slot is opened for a guest write with GuestArena::HostWrite before the writability check, as GuestCopyOut does for a write-watched guest page. The tests pin both answers, a null handle in a read-only slot and a null handle in a write-watched page, and the entry for _Mtx_trylock is in the fragment docs/dev/debt/changes/fix-libc-mtx-trylock-null-and-destroyed-handle.md (the entry is 964 characters with its list marker); docs/dev/debt/libc.md is not changed.

That a pointer to a null handle gets a mutex made on demand is a guess: the probe measured only the return value 0, not what the console does with the handle. The tests pin the real sequence _Mtx_init, _Mtx_destroy, _Mtx_trylock on one handle and, with zeroed storage, a handle that is not a live mutex; a handle that points to freed memory is not tested. That _Mtx_destroy leaves a marker is an implementation choice that gives the measured return value 4: what the console leaves in the handle after the destroy is not measured. PPSA06465 imports _Mtx_trylock from the system library at one call site (a registry read, not a run); 14 more titles import it but ship their own libc.prx. Whether PPSA06465 reaches the call with a destroyed or null handle is not shown. A title process was not measured.

Tested

Measured on a PS5 with firmware 11.60 (kernel_get_fw_version 0x11600005), from a homebrew payload process (ps5-payload-sdk v0.45, elfldr), on 2026-11-11 at 06:00 UTC. A title process may differ. The cases are 1.2 to 4.2, lines 4 to 22 of the raw output in the first comment.

Call PS5, payload process, firmware 11.60 AnyPS5 main before here
_Mtx_trylock(&handle) of a fresh plain mutex 0 (line 4) 0 unchanged
_Mtx_trylock(&handle) of a plain mutex the caller owns 3 (_Thrd_busy) (line 5) 3 unchanged
_Mtx_trylock(&handle) of a plain mutex held by another thread 3 (line 8) 3 unchanged
_Mtx_trylock(&handle) after _Mtx_destroy (stale handle) 4 (line 11) throws 4 (after _Mtx_init and _Mtx_destroy of the same handle, and with zeroed storage)
_Mtx_trylock(&handle) with handle null 0 (line 22) throws 0, creates and locks a plain mutex in writable memory (a guess); a read-only slot throws
_Mtx_trylock(NULL) SIGSEGV (line 21) throws throws

On main ce111157b8b3, Linux (WSL, Release), built for the targets guest_libc_static_imports and guest_libc_threads with ninja returning 0 for each commit: with only the test commit guest_libc_static_imports aborts (1 of the 4 mutex tests fails), and with the fix and with the documentation commit all four pass in 3 of 3 runs each. tools/check_conventions.py passes on each of the three commits with no warning (the changed entry was 976 characters). The probe source and its raw output are in the first comment. On Windows 10 Pro 10.0.19045 (WinLibs GCC, Ninja, Release), on 2026-11-16 between 20:23 and 20:33 UTC, the build of the test commit 4a3d0cab1f1c alone finished with ninja returning 0 and 3 of the 9 mutex, condition variable, pad and forwarding tests failed (guest_libc_threads, guest_libc_static_imports, guest_libc_mutex_init_with_name), and the build of 82402f2dc3b2 finished with ninja returning 0 and all 9 passed. The Linux runs above are from before the destroyed-handle commits. The three review follow-ups were built and run on Windows (WinLibs MinGW-W64, Ninja, Release; one machine, the logs are kept locally and are not attached) on 2026-12-02: the atomic loads (ae3502724c3, 10 lines added and 5 removed in Threads.cpp) have no red arm, because on x86 the missing atomics cannot be seen without a thread sanitizer; guest_libc_threads_tests, guest_libc_static_imports_tests and guest_libc_mutex_init_with_name_tests were built (ninja returning 0) and each returned 0 in one run, at about 03:11 UTC. For the read-only slot, the test commit 0372622ede6 alone on a575ba6a9ad5 made guest_libc_threads_tests crash with an unhandled exception 0xc0000005 (a write) at about 05:00 UTC, and with the fix commit ac188a9baf0 it returned 0 in 3 of 3 runs, and the other two tests returned 0, at about 05:01 UTC. For the write-watched page, the test commit 77d67f778d8 alone on ac188a9baf0 ended in std::invalid_argument _Mtx_trylock: null mutex handle, then terminate and abort, at about 06:31 UTC, and with the fix commit 378dd39ab94, the head of this branch, guest_libc_threads_tests returned 0 in 3 of 3 runs and the other two tests returned 0, at about 06:32 UTC. Against a fresh main fc294cb (fetched on 2026-12-02): tools/debt_fold.py --check exits with 0; tools/check_conventions.py --base upstream/main reports no findings; git merge-tree of the head with that main is clean. Not tested: Linux for the destroyed-handle commits and the follow-ups, macOS, the CI runner, a title process, any other firmware, the recursive and timed mutex types, a handle that points to freed memory, and what the console does with a null handle or leaves in a destroyed handle after the call.

Overlap: counted on 2026-12-02 from the 2357 open pull requests (file lists from the GitHub API, the pull requests with 100 or more files read in full through the files API; the 1386 pull requests that change core/libs/prx/libc/src/Threads.cpp, core/libs/tests/GuestLibcMutexInitWithName.cpp, core/libs/tests/GuestLibcStaticImports.cpp, core/libs/tests/GuestLibcThreads.cpp, a file under docs/dev/debt/ or the fragment path, or have mtx or mutex in the title or branch name, had their patches of the files under docs/dev/debt/ and of the files with libc, Threads or GuestLibc in the path searched for _Mtx_trylock, _Mtx_destroy, destroyedMutexMarker and the fragment name, from 08:08 to 09:17 UTC, main at fc294cbbb539 at the start). No other open pull request changes the fragment docs/dev/debt/changes/fix-libc-mtx-trylock-null-and-destroyed-handle.md, and no patch of a debt file contains a searched name, so none changes the entry for _Mtx_trylock. Of the pull requests that share a file, boykopovar#27937 (feat(libs): general system-library fixes from PS4 title bring-up, Threads.cpp) and boykopovar#26252 and boykopovar#26224 (GuestLibcStaticImports.cpp) merge cleanly with this head, and boykopovar#19434 (feat(libSceLibcInternal): forward the firmware libc-internal export surface) declares _Mtx_destroy_nid_disambig1_nid_postfix in Forwards.cpp and merges cleanly; boykopovar#12315 and boykopovar#1835 change Threads.cpp and already conflict with main alone in as many files, so not because of this change. The merges of this head with each of the 52 pull requests that change one of the files named or have a searched name were made with git merge-tree from 08:08 to 09:17 UTC: 36 are clean; the other 16 already conflict with main alone, in at least as many files as with this head, so none of them conflicts because of this change. The count was repeated for the 147 pull requests that were opened or updated since 08:08 UTC (of the 2359 open pull requests, from 09:36 to 09:41 UTC; 99 of them were patch-scanned for the same names): the only one that shares a file is boykopovar#12849 (libc.md), which already conflicts with main alone in 2 files; no new hit. Two commits have been merged to main since 68d65e6b14a (the main that was merged into this branch; main at fc294cbbb539); neither changed one of the files named, and a merge of this head into main at fc294cbbb539 was checked at 09:42 UTC and is clean. This pull request does not depend on any of them (Depends on: none).

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 (no graphics change)
  • AI-assisted: yes

Summary by CodeRabbit

  • Bug Fixes
    • Improved reliability of mutex operations when handles are uninitialized, invalid, or already destroyed.
    • Added support for initializing mutexes when first used, with clearer handling of inaccessible memory and allocation failures.
    • Prevented stale mutex references from remaining in guest pad state after cleanup.
  • Documentation
    • Added guidance on observed mutex behavior in edge cases.

xXPBSXx and others added 14 commits November 11, 2026 10:28
…a null handle

A payload process on firmware 11.60 returns 4 for the handle of a destroyed mutex and 0
for a pointer to a null handle; both threw here. The test expects both, so it fails until
the next commit.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
… a pointer to a null handle

Both threw here. A pointer to a null handle gets a plain mutex that the call locks; a
handle whose tag is not a live mutex's returns _Thrd_error.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
…process

The entry now says what a payload process on firmware 11.60 returned for a destroyed
handle, a pointer to a null handle and a null pointer, and what this does for each.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
…same handle

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
…_Mtx_trylock returns 4

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
…andle

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
…e-exchange

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
… _Mtx_trylock

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
…t page

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
… publishes a mutex

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@coderabbitai

coderabbitai Bot commented Dec 2, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

Changes: Mutex Handle Behavior

Layer / File(s) Summary
Destroyed mutex handle lifecycle
core/libs/prx/libc/src/Threads.cpp, core/libs/tests/GuestLibcMutexInitWithName.cpp, core/libs/tests/GuestLibcThreads.cpp
Mutex resolution rejects the destroyed marker. Destruction records the marker, while GuestPad cleanup clears its mutex pointer. Tests check try-lock results after destruction and repeated destruction.
Try-lock validation and null-handle initialization
core/libs/prx/libc/src/Threads.cpp, core/libs/tests/GuestLibcStaticImports.cpp, core/libs/tests/GuestLibcThreads.cpp, docs/dev/debt/changes/fix-libc-mtx-trylock-null-and-destroyed-handle.md
Try-lock returns errors for destroyed or invalid handles and initializes a mutex when a null handle slot is writable. Tests cover stale and read-only handles, lazy initialization, and Windows guest-arena write watching. The debt note records observed and inferred behavior.

Suggested reviewers: edubox, al3w0f205

Change: Bug fix

Merge Risk: Minimal · up to 378dd

This change makes try-lock return an error for destroyed or invalid mutex handles and lazily creates a mutex in writable null handle slots. No concrete merge-blocking issue was identified. The behavior for null handles is an implementation guess rather than a measured console result.

Finishing Touches

Generate unit tests (beta)

  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Usage-based review receipt

  • Mode: Continue automatically
  • Reviewed files: 5
  • Waived: $1.25 (charged $0.00)
  • View usage details

Note

This review exceeded your plan’s limits and used usage-based reviews—free during trial, billed after paid activation unless disabled. Manage usage-based reviews.

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 664898b2-8124-4c4d-b26e-819f26c77bd2
Commits

Reviewing files that changed from the base of the PR and between 4131f51 and 378dd39.

Files selected for processing: 5

  • core/libs/prx/libc/src/Threads.cpp
  • core/libs/tests/GuestLibcMutexInitWithName.cpp
  • core/libs/tests/GuestLibcStaticImports.cpp
  • core/libs/tests/GuestLibcThreads.cpp
  • docs/dev/debt/changes/fix-libc-mtx-trylock-null-and-destroyed-handle.md

Limit details: You’ve used all 10 included reviews currently available.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants