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
Loading…
Reference in a new issue
No description provided.
Delete branch "%!s()"
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 husseinkad on 2026-10-14T00:18:02Z.
pthread_getaffinity_npandpthread_attr_get_npread 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.4thr_affinity.candthr_attr.creturnESRCHfor a dead thread (_thr_find_thread(..., include_dead = 0)fails whenpthread->state == PS_DEAD, whichthr_exit.csets before the thread is joined), andpthread_setaffinity_nphere 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 testtests/GuestPthreadDeadThread.cppregistered in the libkernelCMakeLists.txt, and a debt entry indocs/dev/debt/libkernel.md). A newGuestThreadIsSelfOrLivehelper returnsESRCHfor a dead thread (self short-circuits), and the output buffer is left untouched.Checks:
guest_pthread_dead_threadtest fails without the change (abort) and passes with it, 30 repeats; eight related thread tests pass.pthread_getaffinity_npandpthread_attr_get_npreturnESRCHwith 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 orESRCH, and no torn reads. Low and misaligned garbage pointers crash main in the first query; the branch returnsESRCHthere.pthread_getcpuclockidreturnESRCHfor a finished, unjoined thread, which FreeBSD does not (thr_getcpuclockid.creturns 0 and the clock id without a lookup); that part is removed in the second commit andpthread_getcpuclockidis identical to main in every state.check_conventions.pyis clean andpr_overlap.pyfinds 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_npandpthread_killhere have the same shape);scePthreadGetaffinityandscePthreadAttrGetstill return 0 with stale data for finished and joined threads, so they now disagree withpthread_attr_get_np. Only Windows was built and run.AI-assisted: yes
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.