fix(libkernel): do not expose stale thread-specific values through a reused pthread key #367
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/367"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from boykopovar/AnyPS5. Originally posted by DotDebian on 2026-10-02T15:24:21Z.
What
scePthreadKeyDeletefrees a key slot and the nextscePthreadKeyCreatehands the same index out again, but every thread kept the value it had stored under that index. The new key then read the previous key's value instead ofNULL, and at thread exit the new key's destructor was called on that stale pointer.POSIX requires the value
NULLto be associated with a new key in all active threads. Each slot now carries a sequence number, as FreeBSD libthr does (thr_spec.c): a thread value is only visible toscePthreadGetspecificand to the exit destructors while its sequence matches the slot's.scePthreadGetspecificreturnsNULLfor a deleted key and for a value stored under a previous incarnation of the key. It stays lock-free (one atomic load).scePthreadSetspecificreads the slot sequence without taking the key lock.pthread_getspecific,pthread_setspecific,pthread_key_createandpthread_key_deleteforward to these functions and get the same behaviour.Tested
Windows 11, MinGW-w64 GCC 15.2.0, Release build with
-DBUILD_TESTING=ON,ctest: 109/109 pass. No title or homebrew was run. Linux was not built or run locally; the change has no platform-specific code and the new test is registered on both platforms, so CI is its first Linux run.New test
guest_pthread_key(core/libs/tests/GuestPthreadKey.cpp): value round trip, read after delete, read through a recreated key on the same thread and on another live thread, and destructors at thread exit (called for a live key, not called for a deleted key nor for the key that reused its index). Onmainthe read-after-delete, both reuse reads and the destructor check fail; the live-key destructor check passes.Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed in TechnicalDebt.mdfiles or images added to the repository (attach them to this PR)🤖 Generated with Claude Code
Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-04T05:43:11Z.
Thanks, this matches FreeBSD's libthr (
thr_spec.c: the key's seqno is bumped on create and getspecific compares it), and the ordering of the sequence store against the destructor looks right to me. It no longer merges into current main:core/libs/prx/libkernel/CMakeLists.txtconflicts with tests that landed since. Could you rebase onto main? Once CI is green again it looks ready to merge from my side.Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-04T22:37:57Z.
Thanks for the rebase; the code is unchanged apart from the CMake context, and CI is green.
guest_pthread_keypasses here on current main. Ready to merge from my side. #344 registers its test in the same spot oflibkernel/CMakeLists.txt, so whichever lands second needs a one-line rebase there.Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-05T01:18:04Z.
Thanks for the rebase. The commit is unchanged apart from the CMake context (the test now sits after #344's
guest_pthread_rwlock), it merges cleanly,guest_pthread_keypasses here (it aborts on main's code), and CI is green. Ready to merge from my side.