mirror of
https://github.com/boykopovar/AnyPS5.git
synced 2026-10-10 01:28:44 +02:00
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 <noreply@anthropic.com>
This commit is contained in:
@@ -216,6 +216,16 @@ if(BUILD_TESTING)
|
||||
set_tests_properties(guest_pthread_rwlock PROPERTIES ENVIRONMENT_MODIFICATION
|
||||
"PATH=path_list_prepend:$<TARGET_FILE_DIR:libkernel>")
|
||||
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:$<TARGET_FILE_DIR:libkernel>")
|
||||
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)
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
#include "../include/Pthread.hpp"
|
||||
#include "prx/libc/include/General.hpp"
|
||||
#include <array>
|
||||
#include <atomic>
|
||||
#include <cstdint>
|
||||
#include <mutex>
|
||||
#include <stdexcept>
|
||||
|
||||
@@ -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<std::uint64_t> 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<KeySlot, MAX_KEYS> g_keys;
|
||||
static std::uint64_t g_lastSequence = FREE_KEY;
|
||||
|
||||
struct ThreadValues {
|
||||
std::array<void*, MAX_KEYS> values{};
|
||||
std::array<ThreadValue, MAX_KEYS> 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<GuestKeyDestructor>(destructor)};
|
||||
if (g_keys[index].sequence.load(std::memory_order_relaxed) == FREE_KEY) {
|
||||
g_keys[index].destructor = reinterpret_cast<GuestKeyDestructor>(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;
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,100 @@
|
||||
#include "SceTypes.hpp"
|
||||
#include <atomic>
|
||||
#include <cstdlib>
|
||||
#include <thread>
|
||||
|
||||
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<int>(0x80020016);
|
||||
|
||||
static void Require(bool value) { if (!value) std::abort(); }
|
||||
|
||||
static std::atomic<int> deletedCalls{0}, reusedCalls{0}, liveCalls{0};
|
||||
static std::atomic<void*> liveValue{nullptr};
|
||||
static std::atomic<int> 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<pthread_key_destructor_func_t>(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);
|
||||
}
|
||||
Reference in New Issue
Block a user