From a2e338032390fc2e3649e04fafb4cad287ed4a9e Mon Sep 17 00:00:00 2001 From: QRMOE Date: Tue, 13 Oct 2026 08:05:35 +0200 Subject: [PATCH 1/3] fix(libkernel): reject unsupported POSIX condition clocks --- core/libs/prx/libkernel/Pthread/Posix/Cond.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/core/libs/prx/libkernel/Pthread/Posix/Cond.cpp b/core/libs/prx/libkernel/Pthread/Posix/Cond.cpp index 5c5e64d18cf9..693e236bd57c 100644 --- a/core/libs/prx/libkernel/Pthread/Posix/Cond.cpp +++ b/core/libs/prx/libkernel/Pthread/Posix/Cond.cpp @@ -61,6 +61,7 @@ int APS5_VABI pthread_condattr_init_nid_postfix(PthreadCondattr* attr) { int APS5_VABI pthread_condattr_setclock_nid_postfix(PthreadCondattr* attr, KernelClockid clock_id) { if (!attr || !*attr) return PosixThread::GUEST_EINVAL; + if (clock_id != 0 && clock_id != 1 && clock_id != 2 && clock_id != 4) return PosixThread::GUEST_EINVAL; return toPosix(scePthreadCondattrSetclock(attr, clock_id)); } From 7d156be34c90567e64beb7e143a0ed3ffb5a7ef3 Mon Sep 17 00:00:00 2001 From: QRMOE Date: Tue, 13 Oct 2026 08:06:52 +0200 Subject: [PATCH 2/3] test(libkernel): cover accepted and rejected condition clocks --- core/libs/tests/GuestCondTimedwait.cpp | 44 ++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/core/libs/tests/GuestCondTimedwait.cpp b/core/libs/tests/GuestCondTimedwait.cpp index 5a9190e1ec66..e9764bcd42f2 100644 --- a/core/libs/tests/GuestCondTimedwait.cpp +++ b/core/libs/tests/GuestCondTimedwait.cpp @@ -1,8 +1,11 @@ #include "SceTypes.hpp" +#include "prx/libkernel/Pthread/include/Pthread.hpp" #include #include #include +#include #include +#include extern "C" { int APS5_VABI scePthreadCreate(Pthread* thread, const PthreadAttr* attr, PthreadEntry entry, void* arg, const char* name); @@ -17,6 +20,11 @@ int APS5_VABI scePthreadCondDestroy(PthreadCond* cond); int APS5_VABI scePthreadCondTimedwait(PthreadCond* cond, PthreadMutex* mutex, unsigned int usec); int APS5_VABI scePthreadCondSignal(PthreadCond* cond); int APS5_VABI pthread_cond_reltimedwait_np_nid_postfix(PthreadCond* cond, PthreadMutex* mutex, unsigned int usec); +int APS5_VABI pthread_condattr_init_nid_postfix(PthreadCondattr* attr); +int APS5_VABI pthread_condattr_destroy_nid_postfix(PthreadCondattr* attr); +int APS5_VABI pthread_condattr_setclock_nid_postfix(PthreadCondattr* attr, KernelClockid clockId); +int APS5_VABI pthread_cond_timedwait_nid_postfix(PthreadCond* cond, PthreadMutex* mutex, const KernelTimespec* abstime); +int* APS5_VABI __error_nid_postfix(); int APS5_VABI __cxa_atexit_nid_postfix(void (APS5_VABI *)(void*), void*, void*); void APS5_VABI __pthread_cxa_finalize_nid_postfix(void*); unsigned int APS5_VABI sceKernelSleep(unsigned int seconds); @@ -29,6 +37,7 @@ static constexpr int SCE_OK = 0; static constexpr int SCE_KERNEL_ERROR_ETIMEDOUT = static_cast(0x8002003C); static constexpr int SCE_KERNEL_ERROR_EBUSY = static_cast(0x80020010); static constexpr int POSIX_EPERM = 1; +static constexpr int POSIX_EINVAL = 22; static constexpr int POSIX_ETIMEDOUT = 60; static void Require(bool value) { if (!value) std::abort(); } @@ -104,7 +113,42 @@ static void RelativeWait() { Require(scePthreadMutexDestroy(&signalled.mutex) == SCE_OK); } +static void AttributeClockValidation() { + PthreadCondattr empty = nullptr; + Require(pthread_condattr_setclock_nid_postfix(nullptr, 0) == POSIX_EINVAL); + Require(pthread_condattr_setclock_nid_postfix(&empty, 0) == POSIX_EINVAL); + for (const KernelClockid accepted : {0, 1, 2, 4}) { + PthreadCondattr attr = nullptr; + Require(pthread_condattr_init_nid_postfix(&attr) == 0); + *__error_nid_postfix() = 12345; + Require(pthread_condattr_setclock_nid_postfix(&attr, accepted) == 0); + Require(*__error_nid_postfix() == 12345); + for (const KernelClockid rejected : {std::numeric_limits::min(), -1, 3, 5, 6, 7, 8, + 9, 10, 11, 12, 13, 14, 15, std::numeric_limits::max()}) { + const int result = pthread_condattr_setclock_nid_postfix(&attr, rejected); + if (result != POSIX_EINVAL) { + std::fprintf(stderr, "pthread_condattr_setclock accepted unsupported clock %d: result %d\n", rejected, result); + std::abort(); + } + Require(*__error_nid_postfix() == 12345); + Require(attr->_clockid == accepted); + } + Signalled signalled; + Require(scePthreadCondInit(&signalled.cond, &attr, nullptr) == 0); + Require(pthread_condattr_destroy_nid_postfix(&attr) == 0 && attr == nullptr); + Require(scePthreadMutexInit(&signalled.mutex, nullptr, nullptr) == 0); + Require(scePthreadMutexLock(&signalled.mutex) == 0); + const KernelTimespec expired{-1, 0}; + Require(pthread_cond_timedwait_nid_postfix(&signalled.cond, &signalled.mutex, &expired) == POSIX_ETIMEDOUT); + RequireHeldByCaller(signalled); + Require(scePthreadMutexUnlock(&signalled.mutex) == 0); + Require(scePthreadMutexDestroy(&signalled.mutex) == 0); + Require(scePthreadCondDestroy(&signalled.cond) == 0); + } +} + int main() { + AttributeClockValidation(); RelativeWait(); Context context; From a98e67c1765a1d2555d99002d45e926fe488ff0a Mon Sep 17 00:00:00 2001 From: QRMOE Date: Tue, 13 Oct 2026 08:07:55 +0200 Subject: [PATCH 3/3] docs(libkernel): record condition-clock reference behavior --- docs/dev/debt/libkernel.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/docs/dev/debt/libkernel.md b/docs/dev/debt/libkernel.md index 111da3fceb34..507543ceff43 100644 --- a/docs/dev/debt/libkernel.md +++ b/docs/dev/debt/libkernel.md @@ -10,6 +10,8 @@ ### Unknown function info +- [pthread_condattr_setclock](../../../core/libs/prx/libkernel/Pthread/Posix/Cond.cpp) (libkernel) accepts guest clock IDs 0, 1, 2 and 4 (`CLOCK_REALTIME`, `CLOCK_VIRTUAL`, `CLOCK_PROF`, `CLOCK_MONOTONIC`) and returns `EINVAL` without changing the attribute for any other ID, following FreeBSD 11 [thr_condattr.c](https://github.com/freebsd/freebsd-src/blob/releng/11.4/lib/libthr/thread/thr_condattr.c). The console's accepted clock IDs have not been measured. + - [getlogin_r](../../../core/libs/prx/libkernel/System/src/Login.cpp) (libkernel) returns the host effective account name: `GetUserNameW` as UTF-8 on Windows and `getpwuid_r(geteuid())` on POSIX. A valid account returns a name even without a login session; host `getlogin_r`, `USER` and `LOGNAME` are not consulted. The console's login name and behaviour without a session are unknown. Account lookup or encoding failures, a missing or empty account name, and POSIX account records exceeding the 16 KiB lookup buffer throw; there is no replacement name or retry. A short guest buffer returns `ERANGE` without modifying it, and a null output returns `EFAULT`. - [sceCoredumpGetThreadContextInfo](../../../core/libs/prx/libkernel/System/src/Coredump.cpp) (libkernel, NID `ShChva57wIM`) - declared as a throwing stub. The pointer and size arguments and integer return type follow [SharpProspero's PS5 bindings](https://github.com/SvenGDK/SharpProspero/blob/5dfdb4b0b166e83dc9ae275001afb695f12c7174/src/SharpProspero/Interop/Kernel/KernelSystem.cs). The context structure and console behavior are unverified; the stub does not read or write caller memory. PPSA01736 imports it - [sceKernelGetSystemSwVersion](../../../core/libs/prx/libkernel/System/src/SystemConfiguration.cpp) (libkernel) reports baseline system software version `0x01000000` ("01.000.000") for ProsperoStore (PPSA99000). The 0x28-byte layout (`uint64_t size` at 0, `versionString` at 8 and `version` at 0x24) matches [ProsperoStore's caller](https://github.com/blackbearreloaded/ProsperoStore/blob/20584011/src/diag/trace.cpp#L20-L26), [shadPS4](https://github.com/shadps4-emu/shadPS4/blob/811bfcd7d1/src/core/libraries/kernel/kernel.h#L45-L49), [fpPS4](https://github.com/red-prig/fpPS4/blob/04cefd43e6/kernel/ps4_libkernel.pas#L502-L506) and [OpenOrbis](https://github.com/OpenOrbis/OpenOrbis-PS4-Toolchain/blob/0a1aaf9dd4/include/orbis/_types/kernel.h#L256-L260). These sources support the structure layout, but not the reported version value: the console firmware version remains unverified. The checks are unverified too: a null pointer returns `SCE_KERNEL_ERROR_EFAULT` and a `size` other than 0x28 returns `SCE_KERNEL_ERROR_EINVAL`, while shadPS4 returns success for null and never reads `size`.