libkernel: pthread_getaffinity_np and pthread_attr_get_np succeed on a dead thread and can read freed memory; FreeBSD returns ESRCH; fix branch ready #5584

Closed
opened 2026-10-14 00:18:02 +00:00 by husseinkad · 1 comment
husseinkad commented 2026-10-14 00:18:02 +00:00 (Migrated from github.com)
Contributor

Imported from boykopovar/AnyPS5. Originally posted by husseinkad on 2026-10-14T00:18:02Z.


pthread_getaffinity_np and pthread_attr_get_np read the thread object without checking that the thread is still alive. For a thread that has been joined, that reads freed memory and returns 0 with garbage; for a thread that has finished but is not yet joined, it returns success. FreeBSD 11.4 thr_affinity.c and thr_attr.c return ESRCH for a dead thread (_thr_find_thread(..., include_dead = 0) fails when pthread->state == PS_DEAD, which thr_exit.c sets before the thread is joined), and pthread_setaffinity_np here already did.

Fix branch: https://github.com/husseinkad/AnyPS5/tree/fix/pthread-dead-thread-esrch (two commits on a recent main: the change and a follow-up from review; libkernel/Pthread/Posix/Thread.cpp, Posix/Attr.cpp, src/Thread.cpp, include/Pthread.hpp, the new test tests/GuestPthreadDeadThread.cpp registered in the libkernel CMakeLists.txt, and a debt entry in docs/dev/debt/libkernel.md). A new GuestThreadIsSelfOrLive helper returns ESRCH for a dead thread (self short-circuits), and the output buffer is left untouched.

Checks:

  • The new guest_pthread_dead_thread test fails without the change (abort) and passes with it, 30 repeats; eight related thread tests pass.
  • Probed against main: pthread_getaffinity_np and pthread_attr_get_np return ESRCH with the buffer untouched for a finished, unjoined thread, a joined thread, a detached thread that finished, a thread detached after finishing, and zero-filled, 0xFF-filled and garbage handles; running, detached-running, self, the main thread from a worker, a worker's own handle and NULL (EINVAL) are unchanged. A 1,000-iteration join race (joined and detached, three querying threads) gave no crash, only 0 or ESRCH, and no torn reads. Low and misaligned garbage pointers crash main in the first query; the branch returns ESRCH there.
  • Three independent reviews. The first found that my first version also made pthread_getcpuclockid return ESRCH for a finished, unjoined thread, which FreeBSD does not (thr_getcpuclockid.c returns 0 and the clock id without a lookup); that part is removed in the second commit and pthread_getcpuclockid is identical to main in every state. check_conventions.py is clean and pr_overlap.py finds no overlap.

Not covered (listed in the debt entry): the liveness check and the later reads are not atomic, so a concurrent join can still free the thread in between (the window is narrowed; it never fired in the stress test, while main reads freed memory every time, and pthread_setaffinity_np and pthread_kill here have the same shape); scePthreadGetaffinity and scePthreadAttrGet still return 0 with stale data for finished and joined threads, so they now disagree with pthread_attr_get_np. Only Windows was built and run.

AI-assisted: yes

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5839734931 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/5584). Originally posted by [husseinkad](https://github.com/husseinkad) on 2026-10-14T00:18:02Z. --- `pthread_getaffinity_np` and `pthread_attr_get_np` read the thread object without checking that the thread is still alive. For a thread that has been joined, that reads freed memory and returns 0 with garbage; for a thread that has finished but is not yet joined, it returns success. FreeBSD 11.4 `thr_affinity.c` and `thr_attr.c` return `ESRCH` for a dead thread (`_thr_find_thread(..., include_dead = 0)` fails when `pthread->state == PS_DEAD`, which `thr_exit.c` sets before the thread is joined), and `pthread_setaffinity_np` here already did. Fix branch: https://github.com/husseinkad/AnyPS5/tree/fix/pthread-dead-thread-esrch (two commits on a recent main: the change and a follow-up from review; `libkernel/Pthread/Posix/Thread.cpp`, `Posix/Attr.cpp`, `src/Thread.cpp`, `include/Pthread.hpp`, the new test `tests/GuestPthreadDeadThread.cpp` registered in the libkernel `CMakeLists.txt`, and a debt entry in `docs/dev/debt/libkernel.md`). A new `GuestThreadIsSelfOrLive` helper returns `ESRCH` for a dead thread (self short-circuits), and the output buffer is left untouched. Checks: - The new `guest_pthread_dead_thread` test fails without the change (abort) and passes with it, 30 repeats; eight related thread tests pass. - Probed against main: `pthread_getaffinity_np` and `pthread_attr_get_np` return `ESRCH` with the buffer untouched for a finished, unjoined thread, a joined thread, a detached thread that finished, a thread detached after finishing, and zero-filled, 0xFF-filled and garbage handles; running, detached-running, self, the main thread from a worker, a worker's own handle and NULL (`EINVAL`) are unchanged. A 1,000-iteration join race (joined and detached, three querying threads) gave no crash, only 0 or `ESRCH`, and no torn reads. Low and misaligned garbage pointers crash main in the first query; the branch returns `ESRCH` there. - Three independent reviews. The first found that my first version also made `pthread_getcpuclockid` return `ESRCH` for a finished, unjoined thread, which FreeBSD does not (`thr_getcpuclockid.c` returns 0 and the clock id without a lookup); that part is removed in the second commit and `pthread_getcpuclockid` is identical to main in every state. `check_conventions.py` is clean and `pr_overlap.py` finds no overlap. Not covered (listed in the debt entry): the liveness check and the later reads are not atomic, so a concurrent join can still free the thread in between (the window is narrowed; it never fired in the stress test, while main reads freed memory every time, and `pthread_setaffinity_np` and `pthread_kill` here have the same shape); `scePthreadGetaffinity` and `scePthreadAttrGet` still return 0 with stale data for finished and joined threads, so they now disagree with `pthread_attr_get_np`. Only Windows was built and run. AI-assisted: yes
oneandonlydean commented 2026-10-14 01:05:58 +00:00 (Migrated from github.com)
Author
Contributor

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-14T01:05:58Z.


Closing: no title is known to reach this, and functions are only changed when a title needs them. If a title stops on or misbehaves because of it on current main, comment here with the title ID and the log line.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6158565077 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/5584#issuecomment-6158565077). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-14T01:05:58Z. --- Closing: no title is known to reach this, and functions are only changed when a title needs them. If a title stops on or misbehaves because of it on current main, comment here with the title ID and the log line.
Sign in to join this conversation.
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#5584
No description provided.