fix(libSceUlt): sceUltReaderWriterLockDestroy unlocks the state's mutex after the state was freed (ThreadSanitizer: heap-use-after-free) #9146

Closed
opened 2026-10-21 13:32:00 +00:00 by xXPBSXx · 0 comments
xXPBSXx commented 2026-10-21 13:32:00 +00:00 (Migrated from github.com)
Contributor

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


sceUltReaderWriterLockDestroy (core/libs/prx/libSceUlt/src/ReaderWriterLock.cpp:105-118) takes auto& state = *it->second;, locks state._mutex with a lock_guard, and then calls gLocks.erase(it). When the map held the last shared_ptr, erase frees the state, and the lock_guard then unlocks state._mutex in freed memory at the end of the function.

Found with ThreadSanitizer (Ubuntu 24.04 WSL2, GCC 13.3, RelWithDebInfo, -fsanitize=thread, run with setarch -R, main 27fabb6b7): the existing guest_ult_sync_rwlock-* tests report heap-use-after-free and unlock of an unlocked mutex at ReaderWriterLock.cpp:118 (8 reports each), and 4 of the 5 tests fail under the sanitizer. The other sceUlt*Destroy functions (mutex, semaphore, condition, queue, pool) keep a shared_ptr copy in a local and are not affected.

AI-assisted (Claude Code).

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5961528962 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/9146). Originally posted by [xXPBSXx](https://github.com/xXPBSXx) on 2026-10-21T13:32:00Z. --- `sceUltReaderWriterLockDestroy` (core/libs/prx/libSceUlt/src/ReaderWriterLock.cpp:105-118) takes `auto& state = *it->second;`, locks `state._mutex` with a `lock_guard`, and then calls `gLocks.erase(it)`. When the map held the last `shared_ptr`, `erase` frees the state, and the `lock_guard` then unlocks `state._mutex` in freed memory at the end of the function. Found with ThreadSanitizer (Ubuntu 24.04 WSL2, GCC 13.3, RelWithDebInfo, `-fsanitize=thread`, run with `setarch -R`, `main` 27fabb6b7): the existing `guest_ult_sync_rwlock-*` tests report `heap-use-after-free` and `unlock of an unlocked mutex` at ReaderWriterLock.cpp:118 (8 reports each), and 4 of the 5 tests fail under the sanitizer. The other `sceUlt*Destroy` functions (mutex, semaphore, condition, queue, pool) keep a `shared_ptr` copy in a local and are not affected. AI-assisted (Claude Code).
Sign in to join this conversation.
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#9146
No description provided.