fix(libSceUlt): keep the reader-writer lock state alive until sceUltReaderWriterLockDestroy has unlocked it #9147

Merged
xXPBSXx merged 1 commit from upstream-pr/9147 into main 2026-10-21 19:48:12 +00:00
xXPBSXx commented 2026-10-21 13:32:16 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by xXPBSXx on 2026-10-21T13:32:16Z.


What

Copy the shared_ptr of the state before locking it in sceUltReaderWriterLockDestroy, so the state outlives the lock guard. The function took auto& state = *it->second, locked state._mutex and then called gLocks.erase(it); when the map held the last reference, erase freed the state and the guard unlocked a mutex in freed memory. No other behaviour changes; the other sceUlt*Destroy functions already keep a shared_ptr copy.

Fixes #9146

Tested

  • ThreadSanitizer (Ubuntu 24.04 on WSL2, GCC 13.3, -fsanitize=thread, setarch -R; a virtualised kernel, not bare-metal), ctest -R guest_ult_sync_rwlock: without the change 8 heap-use-after-free and 8 unlock of an unlocked mutex reports, 4 of 5 tests fail; with the change 0 reports, 5 of 5 pass. No new test: the plain build cannot observe the unlock of freed memory, and the existing guest_ult_sync_rwlock-* tests are the ones that fail under the sanitizer.
  • Windows 10, MinGW-w64 GCC 15.2, Release: guest_ult_sync passes.
  • python tools/check_conventions.py --base upstream/main passes; git merge-tree against main is clean.
  • Not tested: macOS; an AddressSanitizer run (ASAN does not instrument the libc mutex unlock, so it does not report it).

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
  • 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 #9146
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes

AI-assisted (Claude Code).

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4952438621 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9147). Originally posted by [xXPBSXx](https://github.com/xXPBSXx) on 2026-10-21T13:32:16Z. --- ### What Copy the `shared_ptr` of the state before locking it in `sceUltReaderWriterLockDestroy`, so the state outlives the lock guard. The function took `auto& state = *it->second`, locked `state._mutex` and then called `gLocks.erase(it)`; when the map held the last reference, `erase` freed the state and the guard unlocked a mutex in freed memory. No other behaviour changes; the other `sceUlt*Destroy` functions already keep a `shared_ptr` copy. Fixes #9146 ### Tested - ThreadSanitizer (Ubuntu 24.04 on WSL2, GCC 13.3, `-fsanitize=thread`, `setarch -R`; a virtualised kernel, not bare-metal), `ctest -R guest_ult_sync_rwlock`: without the change 8 `heap-use-after-free` and 8 `unlock of an unlocked mutex` reports, 4 of 5 tests fail; with the change 0 reports, 5 of 5 pass. No new test: the plain build cannot observe the unlock of freed memory, and the existing `guest_ult_sync_rwlock-*` tests are the ones that fail under the sanitizer. - Windows 10, MinGW-w64 GCC 15.2, Release: `guest_ult_sync` passes. - `python tools/check_conventions.py --base upstream/main` passes; `git merge-tree` against `main` is clean. - Not tested: macOS; an AddressSanitizer run (ASAN does not instrument the libc mutex unlock, so it does not report it). ### 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) - [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 #9146 - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes AI-assisted (Claude Code).
github-actions[bot] commented 2026-10-21 17:48:20 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-21T17:48:20Z.


Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in Depends on.

Pull request Conflicting files (lines on main) Same exports Same new files
PR 9278 core/libs/prx/libSceUlt/src/ReaderWriterLock.cpp L110-112
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6322684490 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9147#issuecomment-6322684490). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-21T17:48:20Z. --- <!-- pr-overlap --> Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in **Depends on**. | Pull request | Conflicting files (lines on `main`) | Same exports | Same new files | |---|---|---|---| | [PR 9278](https://www.github.com/boykopovar/AnyPS5/pull/9278) | `core/libs/prx/libSceUlt/src/ReaderWriterLock.cpp` [L110-112](https://github.com/boykopovar/AnyPS5/blob/99844c7eefb7e6c2c4d59fe475191e91c6ff185e/core/libs/prx/libSceUlt/src/ReaderWriterLock.cpp#L110-L112) | | |
adriadam10 commented 2026-10-21 19:48:48 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-21T19:48:48Z.


Checked the diff: the state shared_ptr is copied before the guard, so it outlives stateLock (destroyed first) when erase drops the last reference; same pattern as the other sceUlt*Destroy. No behaviour change; merged.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6325063294 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9147#issuecomment-6325063294). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-21T19:48:48Z. --- Checked the diff: the state `shared_ptr` is copied before the guard, so it outlives `stateLock` (destroyed first) when `erase` drops the last reference; same pattern as the other `sceUlt*Destroy`. No behaviour change; merged.
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!9147
No description provided.