fix(libkernel): do not expose stale thread-specific values through a reused pthread key #367

Merged
DotDebian merged 1 commit from upstream-pr/367 into main 2026-10-05 01:19:33 +00:00
DotDebian commented 2026-10-02 15:24:21 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by DotDebian on 2026-10-02T15:24:21Z.


What

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 then read the previous key's value instead of NULL, and at thread exit the new key's destructor was called on that stale pointer.

POSIX requires the value NULL to 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 to scePthreadGetspecific and to the exit destructors while its sequence matches the slot's.

  • scePthreadGetspecific returns NULL for a deleted key and for a value stored under a previous incarnation of the key. It stays lock-free (one atomic load).
  • Exit destructors skip values that belong to a deleted or recreated key.
  • scePthreadSetspecific reads the slot sequence without taking the key lock.
  • pthread_getspecific, pthread_setspecific, pthread_key_create and pthread_key_delete forward 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). On main the read-after-delete, both reuse reads and the destructor check fail; the live-key destructor check passes.

Checklist

  • Based on current main; no other open PR implements the same functions
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt
  • Unimplemented paths throw (NotImplemented_nid_no_patch); silent stubs are listed in TechnicalDebt
  • No notes, investigation .md files or images added to the repository (attach them to this PR)
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: none
  • AI-assisted: yes

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4718222391 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/367). Originally posted by [DotDebian](https://github.com/DotDebian) on 2026-10-02T15:24:21Z. --- ### What `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 then read the previous key's value instead of `NULL`, and at thread exit the new key's destructor was called on that stale pointer. POSIX requires the value `NULL` to 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 to `scePthreadGetspecific` and to the exit destructors while its sequence matches the slot's. - `scePthreadGetspecific` returns `NULL` for a deleted key and for a value stored under a previous incarnation of the key. It stays lock-free (one atomic load). - Exit destructors skip values that belong to a deleted or recreated key. - `scePthreadSetspecific` reads the slot sequence without taking the key lock. - `pthread_getspecific`, `pthread_setspecific`, `pthread_key_create` and `pthread_key_delete` forward 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). On `main` the read-after-delete, both reuse reads and the destructor check fail; the live-key destructor check passes. ### Checklist - [x] Based on current `main`; no other open PR implements the same functions - [x] One topic per PR; follow-ups go in a new PR - [x] No comments in code except [technical debt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/CONVENTIONS.md) - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed in [TechnicalDebt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/TechnicalDebt.md#silent-stubs) - [x] No notes, investigation `.md` files or images added to the repository (attach them to this PR) - [x] New third-party code is a submodule built from source - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] AI-assisted: yes 🤖 Generated with [Claude Code](https://claude.com/claude-code)
oneandonlydean commented 2026-10-04 05:43:11 +00:00 (Migrated from github.com)

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.txt conflicts with tests that landed since. Could you rebase onto main? Once CI is green again it looks ready to merge from my side.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:5977023439 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/367#issuecomment-5977023439). Originally posted by [oneandonlydean](https://github.com/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.txt` conflicts with tests that landed since. Could you rebase onto main? Once CI is green again it looks ready to merge from my side.
oneandonlydean commented 2026-10-04 22:37:57 +00:00 (Migrated from github.com)

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_key passes here on current main. Ready to merge from my side. #344 registers its test in the same spot of libkernel/CMakeLists.txt, so whichever lands second needs a one-line rebase there.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:5985218685 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/367#issuecomment-5985218685). Originally posted by [oneandonlydean](https://github.com/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_key` passes here on current main. Ready to merge from my side. #344 registers its test in the same spot of `libkernel/CMakeLists.txt`, so whichever lands second needs a one-line rebase there.
oneandonlydean commented 2026-10-05 01:18:04 +00:00 (Migrated from github.com)

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_key passes here (it aborts on main's code), and CI is green. Ready to merge from my side.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:5986478852 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/367#issuecomment-5986478852). Originally posted by [oneandonlydean](https://github.com/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_key` passes here (it aborts on main's code), and CI is green. Ready to merge from my side.
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
boykopovar/AnyPS5!367
No description provided.