diff --git a/core/libs/prx/libkernel/Pthread/Posix/Rwlock.cpp b/core/libs/prx/libkernel/Pthread/Posix/Rwlock.cpp index 26987b3a0589..08199fd5089d 100644 --- a/core/libs/prx/libkernel/Pthread/Posix/Rwlock.cpp +++ b/core/libs/prx/libkernel/Pthread/Posix/Rwlock.cpp @@ -24,14 +24,15 @@ int toPosix(int result) { return static_cast(error & 0xffffu); } -void initializeStatic(PthreadRwlock* rwlock, const char* funcName) { - if (!rwlock) throw std::runtime_error(std::string(funcName) + ": null rwlock"); +static bool initializeStatic(PthreadRwlock* rwlock) { + if (!rwlock) return false; std::atomic_ref slot(*rwlock); PthreadRwlock current = slot.load(std::memory_order_acquire); - if (current != nullptr) return; + if (current != nullptr) return true; auto* created = new PthreadRwlockPrivate(); if (!slot.compare_exchange_strong(current, created, std::memory_order_acq_rel, std::memory_order_acquire)) delete created; + return true; } } @@ -39,21 +40,21 @@ void initializeStatic(PthreadRwlock* rwlock, const char* funcName) { extern "C" { int APS5_VABI pthread_rwlock_destroy_nid_postfix(PthreadRwlock* rwlock) { - if (!rwlock) throw std::runtime_error("pthread_rwlock_destroy: null rwlock"); + if (!rwlock) return PosixThread::GUEST_EINVAL; if (*rwlock == nullptr) return 0; return toPosix(scePthreadRwlockDestroy(rwlock)); } int APS5_VABI pthread_rwlock_init_nid_postfix(PthreadRwlock* rwlock, const PthreadRwlockattr* attr) { + if (!rwlock) return PosixThread::GUEST_EINVAL; return toPosix(scePthreadRwlockInit(rwlock, attr, nullptr)); } int APS5_VABI pthread_rwlockattr_destroy_nid_postfix(PthreadRwlockattr* attr) { - if (attr) { - PthreadRwlockattr handle = nullptr; - if (!RwlockOperations::LoadAttribute(attr, handle)) return PosixThread::GUEST_EFAULT; - if (handle == DESTROYED_RWLOCKATTR) throw std::runtime_error("pthread_rwlockattr_destroy: destroyed attr"); - } + if (!attr) return PosixThread::GUEST_EINVAL; + PthreadRwlockattr handle = nullptr; + if (!RwlockOperations::LoadAttribute(attr, handle)) return PosixThread::GUEST_EFAULT; + if (handle == DESTROYED_RWLOCKATTR) throw std::runtime_error("pthread_rwlockattr_destroy: destroyed attr"); const int result = toPosix(scePthreadRwlockattrDestroy(attr)); if (result != 0) return result; const PthreadRwlockattr destroyed = DESTROYED_RWLOCKATTR; @@ -62,6 +63,7 @@ int APS5_VABI pthread_rwlockattr_destroy_nid_postfix(PthreadRwlockattr* attr) { } int APS5_VABI pthread_rwlockattr_init_nid_postfix(PthreadRwlockattr* attr) { + if (!attr) return PosixThread::GUEST_EINVAL; return toPosix(scePthreadRwlockattrInit(attr)); } @@ -111,13 +113,14 @@ int APS5_VABI scePthreadRwlockattrGetpshared(const PthreadRwlockattr* attr, int* } int APS5_VABI pthread_rwlock_rdlock_nid_postfix(PthreadRwlock* rwlock) { - initializeStatic(rwlock, __func__); + if (!initializeStatic(rwlock)) return PosixThread::GUEST_EINVAL; return toPosix(scePthreadRwlockRdlock(rwlock)); } int APS5_VABI pthread_rwlock_timedrdlock_nid_postfix(PthreadRwlock* rwlock, const KernelTimespec* abstime) { + if (!rwlock) return PosixThread::GUEST_EINVAL; if (!abstime) return pthread_rwlock_rdlock_nid_postfix(rwlock); - initializeStatic(rwlock, __func__); + initializeStatic(rwlock); if (scePthreadRwlockTryrdlock(rwlock) == 0) return 0; KernelUseconds usec = 0; if (!PosixThread::RelativeMicroseconds(GUEST_REALTIME_CLOCK, abstime, &usec)) return PosixThread::GUEST_EINVAL; @@ -125,8 +128,9 @@ int APS5_VABI pthread_rwlock_timedrdlock_nid_postfix(PthreadRwlock* rwlock, cons } int APS5_VABI pthread_rwlock_timedwrlock_nid_postfix(PthreadRwlock* rwlock, const KernelTimespec* abstime) { + if (!rwlock) return PosixThread::GUEST_EINVAL; if (!abstime) return pthread_rwlock_wrlock_nid_postfix(rwlock); - initializeStatic(rwlock, __func__); + initializeStatic(rwlock); if (scePthreadRwlockTrywrlock(rwlock) == 0) return 0; KernelUseconds usec = 0; if (!PosixThread::RelativeMicroseconds(GUEST_REALTIME_CLOCK, abstime, &usec)) return PosixThread::GUEST_EINVAL; @@ -134,32 +138,32 @@ int APS5_VABI pthread_rwlock_timedwrlock_nid_postfix(PthreadRwlock* rwlock, cons } int APS5_VABI pthread_rwlock_reltimedrdlock_np_nid_postfix(PthreadRwlock* rwlock, KernelUseconds usec) { - initializeStatic(rwlock, __func__); + if (!initializeStatic(rwlock)) return PosixThread::GUEST_EINVAL; return toPosix(scePthreadRwlockTimedrdlock(rwlock, usec)); } int APS5_VABI pthread_rwlock_reltimedwrlock_np_nid_postfix(PthreadRwlock* rwlock, KernelUseconds usec) { - initializeStatic(rwlock, __func__); + if (!initializeStatic(rwlock)) return PosixThread::GUEST_EINVAL; return toPosix(scePthreadRwlockTimedwrlock(rwlock, usec)); } int APS5_VABI pthread_rwlock_tryrdlock_nid_postfix(PthreadRwlock* rwlock) { - initializeStatic(rwlock, __func__); + if (!initializeStatic(rwlock)) return PosixThread::GUEST_EINVAL; return toPosix(scePthreadRwlockTryrdlock(rwlock)); } int APS5_VABI pthread_rwlock_trywrlock_nid_postfix(PthreadRwlock* rwlock) { - initializeStatic(rwlock, __func__); + if (!initializeStatic(rwlock)) return PosixThread::GUEST_EINVAL; return toPosix(scePthreadRwlockTrywrlock(rwlock)); } int APS5_VABI pthread_rwlock_unlock_nid_postfix(PthreadRwlock* rwlock) { - initializeStatic(rwlock, __func__); + if (!initializeStatic(rwlock)) return PosixThread::GUEST_EINVAL; return toPosix(scePthreadRwlockUnlock(rwlock)); } int APS5_VABI pthread_rwlock_wrlock_nid_postfix(PthreadRwlock* rwlock) { - initializeStatic(rwlock, __func__); + if (!initializeStatic(rwlock)) return PosixThread::GUEST_EINVAL; return toPosix(scePthreadRwlockWrlock(rwlock)); } diff --git a/core/libs/tests/GuestPthreadRwlock.cpp b/core/libs/tests/GuestPthreadRwlock.cpp index a3de6b982398..3562bc4faadf 100644 --- a/core/libs/tests/GuestPthreadRwlock.cpp +++ b/core/libs/tests/GuestPthreadRwlock.cpp @@ -542,6 +542,26 @@ static void CheckSceTryLocks() { Require(scePthreadRwlockDestroy(&lock) == SCE_OK); } +static void CheckNullPointerValidation() { + Require(pthread_rwlock_init_nid_postfix(nullptr, nullptr) == GUEST_EINVAL); + Require(pthread_rwlock_destroy_nid_postfix(nullptr) == GUEST_EINVAL); + Require(pthread_rwlock_rdlock_nid_postfix(nullptr) == GUEST_EINVAL); + Require(pthread_rwlock_wrlock_nid_postfix(nullptr) == GUEST_EINVAL); + Require(pthread_rwlock_tryrdlock_nid_postfix(nullptr) == GUEST_EINVAL); + Require(pthread_rwlock_trywrlock_nid_postfix(nullptr) == GUEST_EINVAL); + Require(pthread_rwlock_unlock_nid_postfix(nullptr) == GUEST_EINVAL); + const KernelTimespec soon = After(20); + Require(pthread_rwlock_timedrdlock_nid_postfix(nullptr, &soon) == GUEST_EINVAL); + Require(pthread_rwlock_timedrdlock_nid_postfix(nullptr, nullptr) == GUEST_EINVAL); + Require(pthread_rwlock_timedwrlock_nid_postfix(nullptr, &soon) == GUEST_EINVAL); + Require(pthread_rwlock_timedwrlock_nid_postfix(nullptr, nullptr) == GUEST_EINVAL); + Require(pthread_rwlock_reltimedrdlock_np_nid_postfix(nullptr, 1000) == GUEST_EINVAL); + Require(pthread_rwlock_reltimedwrlock_np_nid_postfix(nullptr, 1000) == GUEST_EINVAL); + + Require(pthread_rwlockattr_init_nid_postfix(nullptr) == GUEST_EINVAL); + Require(pthread_rwlockattr_destroy_nid_postfix(nullptr) == GUEST_EINVAL); +} + int main() { CheckBadPointers(); CheckSceAttrLifecycle(); @@ -670,4 +690,5 @@ int main() { CheckWriterHandoff(); CheckFailedLockAttempts(); CheckDestroyLifecycle(); + CheckNullPointerValidation(); } diff --git a/docs/dev/debt/libkernel.md b/docs/dev/debt/libkernel.md index 996a98f0494c..2dc6a76b5b1a 100644 --- a/docs/dev/debt/libkernel.md +++ b/docs/dev/debt/libkernel.md @@ -374,6 +374,7 @@ - [scePthreadMutexGetspinloops and scePthreadMutexGetyieldloops](../../../core/libs/prx/libkernel/Pthread/src/Mutex.cpp), also reached through `pthread_mutex_getspinloops_np` and `pthread_mutex_getyieldloops_np`, and [scePthreadSemInit and scePthreadSemGetvalue](../../../core/libs/prx/libkernel/Pthread/src/Sem.cpp) (libkernel) - an unreadable mutex or semaphore pointer, and a null or unwritable output pointer, fail with `SCE_KERNEL_ERROR_EFAULT` (`EFAULT` from the POSIX wrappers), where a null output used to throw; `scePthreadSemInit` also gives it for an unwritable semaphore slot. A null mutex or semaphore pointer still throws and a destroyed mutex still returns `EINVAL`, in the earlier order. These are host-safety fallbacks with unmeasured console behaviour. A read-only slot that holds a static initializer is still written by the lazy initialisation and is not checked. `scePthreadMutexUnlock` and the other lock calls are unchanged because a copy helper would cost them on every lock. - [pthread_mutexattr_setkind_np](../../../core/libs/prx/libkernel/Pthread/Posix/Mutex.cpp) (libkernel) keeps the low 16 bits of the kind as a signed value; `pthread_mutexattr_gettype` fails with `EINVAL` only for a kind above 4, so a negative kind is returned with success, and `pthread_mutex_init` and `scePthreadMutexInit` fail with `EINVAL` for a kind outside 1 to 4 without touching the mutex. Measured in #10725 by running the firmware 13.42 `libkernel.sprx` and `libkernel_sys.sprx` in an emulator, not on a console or in a title process - [pthread_cond_signalto_np, scePthreadCondSignalto](../../../core/libs/prx/libkernel/Pthread/src/Cond.cpp) (libkernel) wake every waiter of the condition variable, so the other waiters see a spurious wakeup, where a payload process on PS5 firmware 13.60 woke only the target and the other waiter of two timed out; waking one thread would need a wait per waiter in the condition variable every wait shares. As on that console, a handle that is not a thread (`0x12345` there) returns `ESRCH`; a finished, unjoined thread (untried there) gets `ESRCH` here too, as with `pthread_getprio`. Otherwise they follow that console: a null thread returns `EINVAL` and a thread that is not waiting on the condition variable `EPERM`, which a registry of each condition variable's waiting threads answers. A thread joins it before it releases the mutex, so a caller holding that mutex wakes it; a caller that does not can find it registered before it blocks and lose the wakeup, as `scePthreadCondSignal` can +- [pthread_rwlock_init, pthread_rwlock_destroy, pthread_rwlock_rdlock, pthread_rwlock_wrlock, pthread_rwlock_tryrdlock, pthread_rwlock_trywrlock, pthread_rwlock_timedrdlock, pthread_rwlock_timedwrlock, pthread_rwlock_reltimedrdlock_np, pthread_rwlock_reltimedwrlock_np, pthread_rwlock_unlock, pthread_rwlockattr_init, pthread_rwlockattr_destroy](../../../core/libs/prx/libkernel/Pthread/Posix/Rwlock.cpp) (libkernel) - passing a null rwlock or attribute pointer returns `EINVAL` (22) as a defensive host-safety fallback; console behavior is unmeasured, and FreeBSD 11.4 `thr_rwlock.c` faults on null dereference. Only null pointers are checked; non-null invalid pointers are not validated. `pthread_rwlockattr_getpshared`, `pthread_rwlockattr_setpshared` and `pthread_rwlockattr_gettype_np` throw for a null attribute to preserve existing test expectations. - [pthread_rwlockattr_gettype_np, scePthreadRwlockattrGettype](../../../core/libs/prx/libkernel/Pthread/Posix/Rwlock.cpp) (libkernel) report the type stored by `scePthreadRwlockattrSettype`, which accepts any value and which read-write locks do not apply; the console's valid types and their effect are unverified. A null or uninitialized attribute throws, as for `pthread_rwlockattr_getpshared` - [pthread_rwlockattr_getpshared, setpshared, gettype_np, settype_np and the scePthreadRwlockattr forms](../../../core/libs/prx/libkernel/Pthread/Posix/Rwlock.cpp) (libkernel) - a non-null attribute pointer that is unreadable or unwritable, and an output pointer that is null or unusable, fail with `EFAULT` (`SCE_KERNEL_ERROR_EFAULT` in the SCE forms), where a null output used to throw. A null attribute pointer or handle keeps its earlier answer (a throw, or `EINVAL` for `settype`), checked before the output pointer, and `setpshared` still checks the `pshared` value (`EINVAL`) before it reads the attribute. FreeBSD 11.4 [`thr_rwlockattr.c`](https://github.com/freebsd/freebsd-src/blob/release/11.4.0/lib/libthr/thread/thr_rwlockattr.c) has no `_np` type calls and dereferences these pointers, so `EFAULT` is a host-safety fallback and not measured on a console. `scePthreadRwlockattrInit` and `Destroy` give `EFAULT` the same way. - [pthread_mutex_setname_np](../../../core/libs/prx/libkernel/Pthread/src/Mutex.cpp) (libkernel) - the console passes `name` to the kernel's `namedobj_create` and stores the returned id instead of the name; here the name is copied and never read back. Once named, a mutex returns `EAGAIN` (35) for every later call and keeps its name, as [firmware 13.42's module run on a PC](https://github.com/boykopovar/AnyPS5/pull/10385#issuecomment-6415603047) does. The kernel's handling of a null `name` is unmeasured: here it returns 0 and leaves the mutex unnamed. A null mutex pointer faults in the module and throws here; a statically initialized or destroyed mutex also throws, and the module's result for those is unmeasured. The module's separate branch for a field holding `0x5eaf00d`, which copies the name into the object, is known only from disassembly and is not reproduced