From eae05782afa6be74497b14381670a607376684db Mon Sep 17 00:00:00 2001 From: RomainROCH Date: Fri, 2 Oct 2026 07:03:36 +0200 Subject: [PATCH] fix(libkernel): enforce POSIX semaphore count limits --- core/libs/prx/libkernel/CMakeLists.txt | 10 +++ core/libs/prx/libkernel/Pthread/Posix/Sem.cpp | 5 +- core/libs/tests/GuestPosixSem.cpp | 61 +++++++++++++++++++ 3 files changed, 75 insertions(+), 1 deletion(-) create mode 100644 core/libs/tests/GuestPosixSem.cpp diff --git a/core/libs/prx/libkernel/CMakeLists.txt b/core/libs/prx/libkernel/CMakeLists.txt index c151a4f41..28ab69eea 100644 --- a/core/libs/prx/libkernel/CMakeLists.txt +++ b/core/libs/prx/libkernel/CMakeLists.txt @@ -70,6 +70,16 @@ if(MINGW) endif() if(BUILD_TESTING) + add_test_executable(guest_posix_sem_tests ${LIBS_INCLUDE_DIR}/tests/GuestPosixSem.cpp) + target_include_directories(guest_posix_sem_tests PRIVATE ${LIBS_INCLUDE_DIR}) + target_link_libraries(guest_posix_sem_tests PRIVATE libkernel libc) + configure_windows_unwind(guest_posix_sem_tests) + add_test(NAME guest_posix_sem_init COMMAND guest_posix_sem_tests init) + add_test(NAME guest_posix_sem_post COMMAND guest_posix_sem_tests post) + if(WIN32) + set_tests_properties(guest_posix_sem_init guest_posix_sem_post PROPERTIES ENVIRONMENT_MODIFICATION + "PATH=path_list_prepend:$") + endif() add_library(guest_module_fixture SHARED ${LIBS_INCLUDE_DIR}/tests/GuestModuleFixture.cpp) target_include_directories(guest_module_fixture PRIVATE ${LIBS_INCLUDE_DIR}) add_custom_command(TARGET guest_module_fixture POST_BUILD diff --git a/core/libs/prx/libkernel/Pthread/Posix/Sem.cpp b/core/libs/prx/libkernel/Pthread/Posix/Sem.cpp index 8bbe3735e..2672b045b 100644 --- a/core/libs/prx/libkernel/Pthread/Posix/Sem.cpp +++ b/core/libs/prx/libkernel/Pthread/Posix/Sem.cpp @@ -17,6 +17,8 @@ struct PosixSemaphore { }; static constexpr int GUEST_EAGAIN = 35; +static constexpr int GUEST_EOVERFLOW = 84; +static constexpr unsigned int GUEST_SEM_VALUE_MAX = 0x7fffffffu; static constexpr int GUEST_REALTIME_CLOCK = 0; static int Fail(int error) { @@ -47,7 +49,7 @@ extern "C" { int APS5_VABI sem_init_nid_postfix(void* sem, int pshared, unsigned int value) { (void)pshared; - if (!sem) return Fail(PosixThread::GUEST_EINVAL); + if (!sem || value > GUEST_SEM_VALUE_MAX) return Fail(PosixThread::GUEST_EINVAL); auto* semaphore = new PosixSemaphore(); semaphore->count = value; std::memcpy(sem, &semaphore, sizeof(semaphore)); @@ -97,6 +99,7 @@ int APS5_VABI sem_post_nid_postfix(void* sem) { if (!semaphore) return Fail(PosixThread::GUEST_EINVAL); { std::lock_guard lock(semaphore->lock); + if (semaphore->count == GUEST_SEM_VALUE_MAX) return Fail(GUEST_EOVERFLOW); ++semaphore->count; } semaphore->available.NotifyOne(); diff --git a/core/libs/tests/GuestPosixSem.cpp b/core/libs/tests/GuestPosixSem.cpp new file mode 100644 index 000000000..aae90c1ae --- /dev/null +++ b/core/libs/tests/GuestPosixSem.cpp @@ -0,0 +1,61 @@ +#include "SceTypes.hpp" +#include "prx/libc/include/general/VabiMacros.hpp" +#include +#include +#include +#include +#include + +extern "C" { +int APS5_VABI sem_init_nid_postfix(void*, int, unsigned int); +int APS5_VABI sem_destroy_nid_postfix(void*); +int APS5_VABI sem_post_nid_postfix(void*); +int APS5_VABI sem_trywait_nid_postfix(void*); +int APS5_VABI sem_getvalue_nid_postfix(void*, int*); +int* APS5_VABI __error_nid_postfix(); +} + +static void Require(bool condition, const char* message) { + if (!condition) { + std::fprintf(stderr, "%s\n", message); + std::abort(); + } +} + +int main(int argc, char** argv) { + constexpr unsigned int maximum = 0x7fffffffu; + std::uintptr_t sem = 0; + int value = -1; + if (argc > 1 && std::strcmp(argv[1], "init") == 0) { + for (const unsigned int initial : {maximum + 1, 0xffffffffu}) { + sem = 0x1234; + *__error_nid_postfix() = 0; + const int result = sem_init_nid_postfix(&sem, 0, initial); + std::fprintf(stderr, "sem_init(%u): result=%d errno=%d\n", initial, result, *__error_nid_postfix()); + Require(result == -1 && *__error_nid_postfix() == 22, "initial count above maximum must fail with EINVAL"); + Require(sem == 0x1234, "failed initialization changed semaphore storage"); + } + return 0; + } + Require(sem_init_nid_postfix(&sem, 0, maximum) == 0, "maximum count must initialize"); + Require(sem_getvalue_nid_postfix(&sem, &value) == 0 && value == static_cast(maximum), "maximum count must be readable"); + *__error_nid_postfix() = 0; + const int result = sem_post_nid_postfix(&sem); + std::fprintf(stderr, "sem_post(maximum): result=%d errno=%d\n", result, *__error_nid_postfix()); + Require(result == -1 && *__error_nid_postfix() == 84, "post at maximum must fail with guest EOVERFLOW"); + Require(sem_getvalue_nid_postfix(&sem, &value) == 0 && value == static_cast(maximum), "overflow changed count"); + Require(sem_trywait_nid_postfix(&sem) == 0, "wait at maximum must succeed"); + Require(sem_getvalue_nid_postfix(&sem, &value) == 0 && value == static_cast(maximum - 1), "wait must decrement"); + Require(sem_post_nid_postfix(&sem) == 0, "post below maximum must succeed"); + Require(sem_getvalue_nid_postfix(&sem, &value) == 0 && value == static_cast(maximum), "post must reach maximum"); + Require(sem_post_nid_postfix(&sem) == -1 && *__error_nid_postfix() == 84, "repeated overflow must fail"); + Require(sem_getvalue_nid_postfix(&sem, &value) == 0 && value == static_cast(maximum), "repeated overflow changed count"); + Require(sem_destroy_nid_postfix(&sem) == 0, "destroy must succeed"); + Require(sem_init_nid_postfix(&sem, 0, 0) == 0, "zero count must initialize"); + Require(sem_trywait_nid_postfix(&sem) == -1 && *__error_nid_postfix() == 35, "empty semaphore must report EAGAIN"); + Require(sem_post_nid_postfix(&sem) == 0, "normal post must succeed"); + Require(sem_getvalue_nid_postfix(&sem, &value) == 0 && value == 1, "normal post must increment"); + Require(sem_trywait_nid_postfix(&sem) == 0, "normal wait must succeed"); + Require(sem_getvalue_nid_postfix(&sem, &value) == 0 && value == 0, "normal wait must decrement"); + Require(sem_destroy_nid_postfix(&sem) == 0, "normal destroy must succeed"); +}