From 4dea441f01a9b2b72ef24a4ccb481125059dc85e Mon Sep 17 00:00:00 2001 From: DotDebian Date: Fri, 2 Oct 2026 17:24:12 +0200 Subject: [PATCH] fix(libkernel): do not expose stale thread-specific values through a reused pthread key scePthreadKeyDelete frees a key slot and the next scePthreadKeyCreate hands the same index out again, but every thread kept the value it had stored under that index. The new key read the previous key's value instead of NULL, and at thread exit its destructor was called on that stale pointer. Each slot now carries a sequence number, as in FreeBSD libthr. A thread value is visible to scePthreadGetspecific and to the exit destructors only while its sequence matches the slot's, so a new key starts as NULL in every thread, as POSIX requires, and a deleted key reads NULL. scePthreadGetspecific stays lock-free and scePthreadSetspecific no longer takes the key lock. Co-Authored-By: Claude Opus 5.5 --- core/libs/prx/libkernel/CMakeLists.txt | 10 ++ core/libs/prx/libkernel/Pthread/src/Tsd.cpp | 48 ++++++---- core/libs/tests/GuestPthreadKey.cpp | 100 ++++++++++++++++++++ 3 files changed, 138 insertions(+), 20 deletions(-) create mode 100644 core/libs/tests/GuestPthreadKey.cpp diff --git a/core/libs/prx/libkernel/CMakeLists.txt b/core/libs/prx/libkernel/CMakeLists.txt index 81792a6ad..9127d300d 100644 --- a/core/libs/prx/libkernel/CMakeLists.txt +++ b/core/libs/prx/libkernel/CMakeLists.txt @@ -216,6 +216,16 @@ if(BUILD_TESTING) set_tests_properties(guest_pthread_rwlock PROPERTIES ENVIRONMENT_MODIFICATION "PATH=path_list_prepend:$") endif() + add_test_executable(guest_pthread_key_tests ${LIBS_INCLUDE_DIR}/tests/GuestPthreadKey.cpp) + target_include_directories(guest_pthread_key_tests PRIVATE ${LIBS_INCLUDE_DIR}) + target_link_libraries(guest_pthread_key_tests PRIVATE libkernel libc) + configure_windows_unwind(guest_pthread_key_tests) + add_test(NAME guest_pthread_key COMMAND guest_pthread_key_tests) + set_tests_properties(guest_pthread_key PROPERTIES TIMEOUT 20) + if(WIN32) + set_tests_properties(guest_pthread_key PROPERTIES ENVIRONMENT_MODIFICATION + "PATH=path_list_prepend:$") + endif() add_test_executable(guest_signal_tests ${LIBS_INCLUDE_DIR}/tests/GuestSignals.cpp) target_include_directories(guest_signal_tests PRIVATE ${LIBS_INCLUDE_DIR}) target_link_libraries(guest_signal_tests PRIVATE libkernel libc) diff --git a/core/libs/prx/libkernel/Pthread/src/Tsd.cpp b/core/libs/prx/libkernel/Pthread/src/Tsd.cpp index 8f6442f8a..b737b2f16 100644 --- a/core/libs/prx/libkernel/Pthread/src/Tsd.cpp +++ b/core/libs/prx/libkernel/Pthread/src/Tsd.cpp @@ -1,6 +1,8 @@ #include "../include/Pthread.hpp" #include "prx/libc/include/General.hpp" #include +#include +#include #include #include @@ -9,34 +11,41 @@ static constexpr int SCE_KERNEL_ERROR_EINVAL = 0x80020016; static constexpr int SCE_KERNEL_ERROR_EAGAIN = 0x80020023; static constexpr int MAX_KEYS = 512; static constexpr int DESTRUCTOR_ITERATIONS = 4; +static constexpr std::uint64_t FREE_KEY = 0; using GuestKeyDestructor = void (APS5_VABI*)(void*); struct KeySlot { - bool used = false; + std::atomic sequence{FREE_KEY}; GuestKeyDestructor destructor = nullptr; }; +struct ThreadValue { + void* value = nullptr; + std::uint64_t sequence = FREE_KEY; +}; + static std::mutex g_keyLock; static std::array g_keys; +static std::uint64_t g_lastSequence = FREE_KEY; struct ThreadValues { - std::array values{}; + std::array values{}; ~ThreadValues() { for (int round = 0; round < DESTRUCTOR_ITERATIONS; ++round) { bool called = false; for (int key = 0; key < MAX_KEYS; ++key) { - void* value = values[key]; - if (!value) continue; + const ThreadValue entry = values[key]; + if (!entry.value) continue; GuestKeyDestructor destructor; { std::lock_guard lock(g_keyLock); - destructor = g_keys[key].used ? g_keys[key].destructor : nullptr; + destructor = g_keys[key].sequence.load(std::memory_order_relaxed) == entry.sequence ? g_keys[key].destructor : nullptr; } - values[key] = nullptr; + values[key] = {}; if (destructor) { - destructor(value); + destructor(entry.value); called = true; } } @@ -47,20 +56,15 @@ struct ThreadValues { static thread_local ThreadValues g_values; -static bool IsValidKey(PthreadKey key) { - if (key < 0 || key >= MAX_KEYS) return false; - std::lock_guard lock(g_keyLock); - return g_keys[key].used; -} - extern "C" { int APS5_VABI scePthreadKeyCreate(PthreadKey* key, pthread_key_destructor_func_t destructor) { if (!key) throw std::runtime_error("scePthreadKeyCreate: null key"); std::lock_guard lock(g_keyLock); for (int index = 0; index < MAX_KEYS; ++index) { - if (!g_keys[index].used) { - g_keys[index] = {true, reinterpret_cast(destructor)}; + if (g_keys[index].sequence.load(std::memory_order_relaxed) == FREE_KEY) { + g_keys[index].destructor = reinterpret_cast(destructor); + g_keys[index].sequence.store(++g_lastSequence, std::memory_order_release); *key = index; return SCE_OK; } @@ -71,19 +75,23 @@ int APS5_VABI scePthreadKeyCreate(PthreadKey* key, pthread_key_destructor_func_t int APS5_VABI scePthreadKeyDelete(PthreadKey key) { if (key < 0 || key >= MAX_KEYS) return SCE_KERNEL_ERROR_EINVAL; std::lock_guard lock(g_keyLock); - if (!g_keys[key].used) return SCE_KERNEL_ERROR_EINVAL; - g_keys[key] = {}; + if (g_keys[key].sequence.load(std::memory_order_relaxed) == FREE_KEY) return SCE_KERNEL_ERROR_EINVAL; + g_keys[key].sequence.store(FREE_KEY, std::memory_order_release); + g_keys[key].destructor = nullptr; return SCE_OK; } void* APS5_VABI scePthreadGetspecific(PthreadKey key) { if (key < 0 || key >= MAX_KEYS) return nullptr; - return g_values.values[key]; + const ThreadValue& entry = g_values.values[key]; + return entry.sequence == g_keys[key].sequence.load(std::memory_order_acquire) ? entry.value : nullptr; } int APS5_VABI scePthreadSetspecific(PthreadKey key, void* value) { - if (!IsValidKey(key)) return SCE_KERNEL_ERROR_EINVAL; - g_values.values[key] = value; + if (key < 0 || key >= MAX_KEYS) return SCE_KERNEL_ERROR_EINVAL; + const std::uint64_t sequence = g_keys[key].sequence.load(std::memory_order_acquire); + if (sequence == FREE_KEY) return SCE_KERNEL_ERROR_EINVAL; + g_values.values[key] = {value, sequence}; return SCE_OK; } diff --git a/core/libs/tests/GuestPthreadKey.cpp b/core/libs/tests/GuestPthreadKey.cpp new file mode 100644 index 000000000..e9bdd1653 --- /dev/null +++ b/core/libs/tests/GuestPthreadKey.cpp @@ -0,0 +1,100 @@ +#include "SceTypes.hpp" +#include +#include +#include + +extern "C" { +int APS5_VABI scePthreadCreate(Pthread* thread, const PthreadAttr* attr, PthreadEntry entry, void* arg, const char* name); +int APS5_VABI scePthreadJoin(Pthread thread, void** retval); +int APS5_VABI scePthreadKeyCreate(PthreadKey* key, pthread_key_destructor_func_t destructor); +int APS5_VABI scePthreadKeyDelete(PthreadKey key); +void* APS5_VABI scePthreadGetspecific(PthreadKey key); +int APS5_VABI scePthreadSetspecific(PthreadKey key, void* value); +} + +static constexpr int SCE_OK = 0; +static constexpr int SCE_KERNEL_ERROR_EINVAL = static_cast(0x80020016); + +static void Require(bool value) { if (!value) std::abort(); } + +static std::atomic deletedCalls{0}, reusedCalls{0}, liveCalls{0}; +static std::atomic liveValue{nullptr}; +static std::atomic stage{0}; +static PthreadKey sharedKey = -1; +static int first = 1, second = 2, third = 3; + +static void APS5_VABI Deleted(void*) { ++deletedCalls; } +static void APS5_VABI Reused(void*) { ++reusedCalls; } +static void APS5_VABI Live(void* value) { ++liveCalls; liveValue = value; } + +static pthread_key_destructor_func_t Destructor(void (APS5_VABI* function)(void*)) { + return reinterpret_cast(function); +} + +static void WaitFor(int value) { + while (stage.load() != value) std::this_thread::yield(); +} + +static void* APS5_VABI ReuseBeforeExit(void*) { + PthreadKey live = -1; + Require(scePthreadKeyCreate(&live, Destructor(Live)) == SCE_OK); + Require(scePthreadSetspecific(live, &third) == SCE_OK); + PthreadKey deleted = -1; + Require(scePthreadKeyCreate(&deleted, Destructor(Deleted)) == SCE_OK); + Require(scePthreadSetspecific(deleted, &first) == SCE_OK); + Require(scePthreadKeyDelete(deleted) == SCE_OK); + PthreadKey reused = -1; + Require(scePthreadKeyCreate(&reused, Destructor(Reused)) == SCE_OK); + Require(reused == deleted); + sharedKey = live; + return nullptr; +} + +static void* APS5_VABI HoldAcrossReuse(void*) { + Require(scePthreadSetspecific(sharedKey, &first) == SCE_OK); + Require(scePthreadGetspecific(sharedKey) == &first); + stage = 1; + WaitFor(2); + Require(scePthreadGetspecific(sharedKey) == nullptr); + Require(scePthreadSetspecific(sharedKey, &second) == SCE_OK); + Require(scePthreadGetspecific(sharedKey) == &second); + return nullptr; +} + +int main() { + PthreadKey key = -1; + Require(scePthreadKeyCreate(&key, nullptr) == SCE_OK); + Require(scePthreadGetspecific(key) == nullptr); + Require(scePthreadSetspecific(key, &first) == SCE_OK); + Require(scePthreadGetspecific(key) == &first); + Require(scePthreadKeyDelete(key) == SCE_OK); + Require(scePthreadGetspecific(key) == nullptr); + Require(scePthreadSetspecific(key, &second) == SCE_KERNEL_ERROR_EINVAL); + Require(scePthreadKeyDelete(key) == SCE_KERNEL_ERROR_EINVAL); + + PthreadKey reused = -1; + Require(scePthreadKeyCreate(&reused, nullptr) == SCE_OK); + Require(reused == key); + Require(scePthreadGetspecific(reused) == nullptr); + Require(scePthreadSetspecific(reused, &second) == SCE_OK); + Require(scePthreadGetspecific(reused) == &second); + Require(scePthreadKeyDelete(reused) == SCE_OK); + + Pthread thread = nullptr; + Require(scePthreadCreate(&thread, nullptr, ReuseBeforeExit, nullptr, nullptr) == SCE_OK); + Require(scePthreadJoin(thread, nullptr) == SCE_OK); + Require(liveCalls == 1 && liveValue == &third); + Require(deletedCalls == 0 && reusedCalls == 0); + Require(scePthreadKeyDelete(sharedKey) == SCE_OK); + + Require(scePthreadKeyCreate(&sharedKey, nullptr) == SCE_OK); + const PthreadKey held = sharedKey; + Require(scePthreadCreate(&thread, nullptr, HoldAcrossReuse, nullptr, nullptr) == SCE_OK); + WaitFor(1); + Require(scePthreadKeyDelete(sharedKey) == SCE_OK); + Require(scePthreadKeyCreate(&sharedKey, nullptr) == SCE_OK); + Require(sharedKey == held); + stage = 2; + Require(scePthreadJoin(thread, nullptr) == SCE_OK); + Require(scePthreadGetspecific(sharedKey) == nullptr); +}