From c68ffcfd2157025fc734586432506fca2a752777 Mon Sep 17 00:00:00 2001 From: Lindamea1 <322051987+Lindamea1@users.noreply.github.com> Date: Tue, 20 Oct 2026 18:31:41 +0100 Subject: [PATCH] fix(libkernel): fail scePthreadMutexUnlock on a statically initialized mutex with EPERM scePthreadMutexUnlock resolved the slot with resolveMutex(mutex, false), which throws "Mutex is not initialized" while the slot still holds the static initializer (0) or the adaptive static initializer (1), so unlocking a statically initialized mutex that was never locked ended the process. FreeBSD 11.4's mutex_unlock_common returns EPERM for both without initializing the slot, and EINVAL for the destroyed marker. Load the slot once: the destroyed marker still returns 0x80020016, and the two initializers now return 0x80020001 and stay in the slot, so a later lock still creates the mutex. Co-Authored-By: Claude Opus 5.5 --- core/libs/prx/libkernel/Pthread/src/Mutex.cpp | 8 +++++--- core/libs/tests/GuestPthreadDestroyedObjects.cpp | 16 ++++++++++++++++ 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/core/libs/prx/libkernel/Pthread/src/Mutex.cpp b/core/libs/prx/libkernel/Pthread/src/Mutex.cpp index 876bd9ee2023..4691a0371b0f 100644 --- a/core/libs/prx/libkernel/Pthread/src/Mutex.cpp +++ b/core/libs/prx/libkernel/Pthread/src/Mutex.cpp @@ -189,10 +189,12 @@ int APS5_VABI scePthreadMutexLock(PthreadMutex* mutex) { } int APS5_VABI scePthreadMutexUnlock(PthreadMutex* mutex) { - auto* current = resolveMutex(mutex, false); - if (!current) + if (!mutex) + throw std::invalid_argument("Mutex pointer is null"); + auto* current = std::atomic_ref(*mutex).load(std::memory_order_acquire); + if (current == destroyedMutex()) return SCE_KERNEL_ERROR_EINVAL; - if (current->_owner.load(std::memory_order_acquire) != std::this_thread::get_id()) + if (!isInitializedMutex(current) || current->_owner.load(std::memory_order_acquire) != std::this_thread::get_id()) return sceNotPermitted; bool released = true; if (current->_type == MutexType::Recursive) { diff --git a/core/libs/tests/GuestPthreadDestroyedObjects.cpp b/core/libs/tests/GuestPthreadDestroyedObjects.cpp index a9a041cfc022..ca35d7b0998c 100644 --- a/core/libs/tests/GuestPthreadDestroyedObjects.cpp +++ b/core/libs/tests/GuestPthreadDestroyedObjects.cpp @@ -19,6 +19,7 @@ int APS5_VABI pthread_cond_destroy_nid_postfix(PthreadCond* cond); } static constexpr int SCE_OK = 0; +static constexpr int SCE_KERNEL_ERROR_EPERM = static_cast(0x80020001u); static constexpr int SCE_KERNEL_ERROR_EINVAL = static_cast(0x80020016u); static constexpr int POSIX_EINVAL = 22; @@ -34,6 +35,21 @@ int main() { Require(pthread_mutex_lock_nid_postfix(&mutex) == POSIX_EINVAL); Require(mutex == reinterpret_cast(std::uintptr_t{2})); + PthreadMutex uninitialized = nullptr; + Require(scePthreadMutexUnlock(&uninitialized) == SCE_KERNEL_ERROR_EPERM); + Require(uninitialized == nullptr); + Require(scePthreadMutexLock(&uninitialized) == SCE_OK); + Require(scePthreadMutexUnlock(&uninitialized) == SCE_OK); + Require(scePthreadMutexUnlock(&uninitialized) == SCE_KERNEL_ERROR_EPERM); + Require(scePthreadMutexDestroy(&uninitialized) == SCE_OK); + + PthreadMutex adaptive = reinterpret_cast(std::uintptr_t{1}); + Require(scePthreadMutexUnlock(&adaptive) == SCE_KERNEL_ERROR_EPERM); + Require(adaptive == reinterpret_cast(std::uintptr_t{1})); + Require(scePthreadMutexLock(&adaptive) == SCE_OK); + Require(scePthreadMutexUnlock(&adaptive) == SCE_OK); + Require(scePthreadMutexDestroy(&adaptive) == SCE_OK); + PthreadCond cond = nullptr; Require(scePthreadCondInit(&cond, nullptr, "destroyed") == SCE_OK); Require(scePthreadCondDestroy(&cond) == SCE_OK);