From 98f050f879a9bf4842bc387d20f2e66ba0818b2d Mon Sep 17 00:00:00 2001 From: Pacuka <69674426+Pacuka@users.noreply.github.com> Date: Tue, 29 Sep 2026 21:33:15 +0200 Subject: [PATCH] fix(libkernel): default mutexes to error checking and add the adaptive type The PS5 libthr (FreeBSD) creates error-checking mutexes by default and has PTHREAD_MUTEX_ADAPTIVE_NP (type 4); relocking an error-checking or adaptive mutex returns EDEADLK. Default mutexes were Normal and type 4 was mapped to Normal, so a relock threw instead of returning EDEADLK, which aborted an Unreal Engine title's recursive directory scan. Co-Authored-By: Claude Opus 5.5 --- .../prx/libkernel/Pthread/include/Pthread.hpp | 3 ++- core/libs/prx/libkernel/Pthread/src/Mutex.cpp | 6 ++--- core/libs/tests/GuestKernelErrors.cpp | 27 +++++++++++++++++++ 3 files changed, 32 insertions(+), 4 deletions(-) diff --git a/core/libs/prx/libkernel/Pthread/include/Pthread.hpp b/core/libs/prx/libkernel/Pthread/include/Pthread.hpp index c1d319502..b95ca4f87 100644 --- a/core/libs/prx/libkernel/Pthread/include/Pthread.hpp +++ b/core/libs/prx/libkernel/Pthread/include/Pthread.hpp @@ -17,6 +17,7 @@ enum class MutexType : std::uint32_t { ErrorCheck = 1, Recursive = 2, Normal = 3, + Adaptive = 4, }; struct PthreadMutexattrPrivate { @@ -30,7 +31,7 @@ struct PthreadMutexPrivate { std::atomic _owner; int _count; - PthreadMutexPrivate() : _type(MutexType::Normal), _owner(std::thread::id{}), _count(0) {} + PthreadMutexPrivate() : _type(MutexType::ErrorCheck), _owner(std::thread::id{}), _count(0) {} }; struct PthreadRwlockattrPrivate { diff --git a/core/libs/prx/libkernel/Pthread/src/Mutex.cpp b/core/libs/prx/libkernel/Pthread/src/Mutex.cpp index 598b914d1..5bd606db6 100644 --- a/core/libs/prx/libkernel/Pthread/src/Mutex.cpp +++ b/core/libs/prx/libkernel/Pthread/src/Mutex.cpp @@ -52,7 +52,7 @@ int acquireMutex(PthreadMutex mutex, TAcquire acquire, int unavailable, bool try if (owned && mutex->_type != MutexType::Recursive) { if (tryOnly) return sceBusy; - if (mutex->_type == MutexType::ErrorCheck) + if (mutex->_type == MutexType::ErrorCheck || mutex->_type == MutexType::Adaptive) return sceDeadlock; throw std::runtime_error("Mutex is already owned by the current thread"); } @@ -90,7 +90,7 @@ extern "C" { int APS5_VABI scePthreadMutexattrInit(PthreadMutexattr* attr) { if (!attr) throw std::invalid_argument("Mutex attribute pointer is null"); - *attr = new PthreadMutexattrPrivate{MutexType::Normal}; + *attr = new PthreadMutexattrPrivate{MutexType::ErrorCheck}; return 0; } @@ -109,7 +109,7 @@ int APS5_VABI scePthreadMutexattrSettype(PthreadMutexattr* attr, int type) { case 1: (*attr)->type = MutexType::ErrorCheck; break; case 2: (*attr)->type = MutexType::Recursive; break; case 3: (*attr)->type = MutexType::Normal; break; - case 4: (*attr)->type = MutexType::Normal; break; + case 4: (*attr)->type = MutexType::Adaptive; break; default: throw std::invalid_argument("Invalid mutex type"); } return 0; diff --git a/core/libs/tests/GuestKernelErrors.cpp b/core/libs/tests/GuestKernelErrors.cpp index 045ced418..fe085f0f7 100644 --- a/core/libs/tests/GuestKernelErrors.cpp +++ b/core/libs/tests/GuestKernelErrors.cpp @@ -34,6 +34,7 @@ static constexpr int MUTEX_TYPE_ERRORCHECK = 1; static constexpr int PRIO_NONE = 0; static constexpr int PRIO_INHERIT = 1; static constexpr int PRIO_PROTECT = 2; +static constexpr int MUTEX_TYPE_ADAPTIVE = 4; static void Require(bool value) { if (!value) std::abort(); } @@ -88,4 +89,30 @@ int main() { Require(scePthreadMutexLock(&mutex) == SCE_KERNEL_ERROR_EDEADLK); Require(scePthreadMutexUnlock(&mutex) == SCE_OK); Require(scePthreadMutexDestroy(&mutex) == SCE_OK); + + Require(scePthreadMutexattrInit(&attr) == SCE_OK); + Require(scePthreadMutexattrSettype(&attr, MUTEX_TYPE_ADAPTIVE) == SCE_OK); + PthreadMutex adaptive = nullptr; + Require(scePthreadMutexInit(&adaptive, &attr, nullptr) == SCE_OK); + Require(scePthreadMutexattrDestroy(&attr) == SCE_OK); + Require(scePthreadMutexLock(&adaptive) == SCE_OK); + Require(scePthreadMutexLock(&adaptive) == SCE_KERNEL_ERROR_EDEADLK); + Require(scePthreadMutexUnlock(&adaptive) == SCE_OK); + Require(scePthreadMutexDestroy(&adaptive) == SCE_OK); + + Require(scePthreadMutexattrInit(&attr) == SCE_OK); + PthreadMutex defaulted = nullptr; + Require(scePthreadMutexInit(&defaulted, &attr, nullptr) == SCE_OK); + Require(scePthreadMutexattrDestroy(&attr) == SCE_OK); + Require(scePthreadMutexLock(&defaulted) == SCE_OK); + Require(scePthreadMutexLock(&defaulted) == SCE_KERNEL_ERROR_EDEADLK); + Require(scePthreadMutexUnlock(&defaulted) == SCE_OK); + Require(scePthreadMutexDestroy(&defaulted) == SCE_OK); + + PthreadMutex unattributed = nullptr; + Require(scePthreadMutexInit(&unattributed, nullptr, nullptr) == SCE_OK); + Require(scePthreadMutexLock(&unattributed) == SCE_OK); + Require(scePthreadMutexLock(&unattributed) == SCE_KERNEL_ERROR_EDEADLK); + Require(scePthreadMutexUnlock(&unattributed) == SCE_OK); + Require(scePthreadMutexDestroy(&unattributed) == SCE_OK); }