Fix fork waiter leak in nsync

This change fixes a bug where nsync waiter objects would leak. It'd mean
that long-running programs like runitd would run out of file descriptors
on NetBSD where waiter objects have ksem file descriptors. On other OSes
this bug is mostly harmless since the worst that can happen with a futex
is to leak a little bit of ram. The bug was caused because tib_nsync was
sneaking back in after the finalization code had cleared it. This change
refactors the thread exiting code to handle nsync teardown appropriately
and in making this change I found another issue, which is that user code
which is buggy, and tries to exit without joining joinable threads which
haven't been detached, would result in a deadlock. That doesn't sound so
bad, except the main thread is a joinable thread. So this deadlock would
be triggered in ways that put libc at fault. So we now auto-join threads
and libc will log a warning to --strace when that happens for any thread
This commit is contained in:
Justine Tunney 2024-12-31 00:55:15 -08:00
parent fd7da586b5
commit 98c5847727
No known key found for this signature in database
GPG key ID: BE714B4575D6E328
35 changed files with 299 additions and 173 deletions

View file

@ -39,7 +39,7 @@
int gettid(void) {
int tid;
if (VERY_LIKELY(__tls_enabled && !__vforked)) {
tid = atomic_load_explicit(&__get_tls()->tib_tid, memory_order_relaxed);
tid = atomic_load_explicit(&__get_tls()->tib_ptid, memory_order_relaxed);
if (VERY_LIKELY(tid > 0))
return tid;
}

View file

@ -561,7 +561,7 @@ ABI static size_t kformat(char *b, size_t n, const char *fmt, va_list va) {
tib = __tls_enabled ? __get_tls_privileged() : 0;
if (!(tib && (tib->tib_flags & TIB_FLAG_VFORKED))) {
if (tib) {
x = atomic_load_explicit(&tib->tib_tid, memory_order_relaxed);
x = atomic_load_explicit(&tib->tib_ptid, memory_order_relaxed);
} else {
x = __pid;
}

View file

@ -129,7 +129,7 @@ bool __maps_held(void) {
return __tls_enabled && !(__get_tls()->tib_flags & TIB_FLAG_VFORKED) &&
MUTEX_OWNER(
atomic_load_explicit(&__maps.lock.word, memory_order_relaxed)) ==
atomic_load_explicit(&__get_tls()->tib_tid, memory_order_relaxed);
atomic_load_explicit(&__get_tls()->tib_ptid, memory_order_relaxed);
}
ABI void __maps_lock(void) {
@ -142,7 +142,7 @@ ABI void __maps_lock(void) {
return;
if (tib->tib_flags & TIB_FLAG_VFORKED)
return;
me = atomic_load_explicit(&tib->tib_tid, memory_order_relaxed);
me = atomic_load_explicit(&tib->tib_ptid, memory_order_relaxed);
if (me <= 0)
return;
word = atomic_load_explicit(&__maps.lock.word, memory_order_relaxed);
@ -192,7 +192,7 @@ ABI void __maps_unlock(void) {
return;
if (tib->tib_flags & TIB_FLAG_VFORKED)
return;
me = atomic_load_explicit(&tib->tib_tid, memory_order_relaxed);
me = atomic_load_explicit(&tib->tib_ptid, memory_order_relaxed);
if (me <= 0)
return;
word = atomic_load_explicit(&__maps.lock.word, memory_order_relaxed);

View file

@ -69,7 +69,7 @@ static errno_t pthread_mutex_lock_recursive(pthread_mutex_t *mutex,
uint64_t word, bool is_trylock) {
uint64_t lock;
int backoff = 0;
int me = atomic_load_explicit(&__get_tls()->tib_tid, memory_order_relaxed);
int me = atomic_load_explicit(&__get_tls()->tib_ptid, memory_order_relaxed);
bool once = false;
for (;;) {
if (MUTEX_OWNER(word) == me) {
@ -119,7 +119,7 @@ static errno_t pthread_mutex_lock_recursive(pthread_mutex_t *mutex,
static errno_t pthread_mutex_lock_recursive_nsync(pthread_mutex_t *mutex,
uint64_t word,
bool is_trylock) {
int me = atomic_load_explicit(&__get_tls()->tib_tid, memory_order_relaxed);
int me = atomic_load_explicit(&__get_tls()->tib_ptid, memory_order_relaxed);
for (;;) {
if (MUTEX_OWNER(word) == me) {
if (MUTEX_DEPTH(word) < MUTEX_DEPTH_MAX) {

View file

@ -44,7 +44,7 @@ static void pthread_mutex_unlock_drepper(atomic_int *futex, char pshare) {
static errno_t pthread_mutex_unlock_recursive(pthread_mutex_t *mutex,
uint64_t word) {
int me = atomic_load_explicit(&__get_tls()->tib_tid, memory_order_relaxed);
int me = atomic_load_explicit(&__get_tls()->tib_ptid, memory_order_relaxed);
for (;;) {
// we allow unlocking an initialized lock that wasn't locked, but we
@ -76,7 +76,7 @@ static errno_t pthread_mutex_unlock_recursive(pthread_mutex_t *mutex,
#if PTHREAD_USE_NSYNC
static errno_t pthread_mutex_unlock_recursive_nsync(pthread_mutex_t *mutex,
uint64_t word) {
int me = atomic_load_explicit(&__get_tls()->tib_tid, memory_order_relaxed);
int me = atomic_load_explicit(&__get_tls()->tib_ptid, memory_order_relaxed);
for (;;) {
// we allow unlocking an initialized lock that wasn't locked, but we

View file

@ -21,9 +21,25 @@
#include "libc/thread/posixthread.internal.h"
#include "libc/thread/thread.h"
//
// - tib_ptid: always guaranteed to be non-zero in thread itself. on
// some platforms (e.g. xnu) the parent thread and other
// threads may need to wait for this value to be set. this
// is generally the value you want to read to get the tid.
//
// - tib_ctid: starts off as -1. once thread starts, it's set to the
// thread's tid before calling the thread callback. when
// thread is done executing, this is set to zero, and then
// this address is futex woken, in case the parent thread or
// any other thread is waiting on its completion. when a
// thread wants to read its own tid, it shouldn't use this,
// because the thread might need to do things after clearing
// its own tib_ctid (see pthread_exit() for static thread).
//
int _pthread_tid(struct PosixThread *pt) {
int tid = 0;
while (pt && !(tid = atomic_load_explicit(&pt->ptid, memory_order_acquire)))
while (pt && !(tid = atomic_load_explicit(&pt->tib->tib_ptid,
memory_order_acquire)))
pthread_yield_np();
return tid;
}

View file

@ -16,6 +16,7 @@
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/intrin/atomic.h"
#include "libc/log/libfatal.internal.h"
#include "libc/nt/thread.h"
#include "libc/nt/thunk/msabi.h"
@ -38,7 +39,9 @@ textwindows dontinstrument void __bootstrap_tls(struct CosmoTib *tib,
tib->tib_ftrace = __ftrace;
tib->tib_sigstack_size = 57344;
tib->tib_sigstack_addr = bp - 57344;
tib->tib_tid = __imp_GetCurrentThreadId();
int tid = __imp_GetCurrentThreadId();
atomic_init(&tib->tib_ptid, tid);
atomic_init(&tib->tib_ctid, tid);
__set_tls_win32(tib);
}

View file

@ -79,7 +79,7 @@ void CheckForMemoryLeaks(void) {
// validate usage of this api
if (_weaken(_pthread_decimate))
_weaken(_pthread_decimate)();
_weaken(_pthread_decimate)(kPosixThreadZombie);
if (!pthread_orphan_np())
kprintf("warning: called CheckForMemoryLeaks() from non-orphaned thread\n");

View file

@ -465,9 +465,6 @@ textwindows int sys_fork_nt(uint32_t dwCreationFlags) {
// re-apply code morphing for function tracing
if (ftrace_stackdigs)
_weaken(__hook)(_weaken(ftrace_hook), _weaken(GetSymbolTable)());
// notify pthread join
atomic_store_explicit(&_pthread_static.ptid, GetCurrentThreadId(),
memory_order_release);
}
if (rc == -1)
dll_make_first(&__proc.free, &proc->elem);

View file

@ -59,7 +59,6 @@ extern pthread_mutex_t __sig_worker_lock;
void __dlopen_lock(void);
void __dlopen_unlock(void);
void nsync_mu_semaphore_sem_fork_child(void);
// first and last and always
// it is the lord of all locks
@ -147,7 +146,6 @@ static void fork_parent(void) {
}
static void fork_child(void) {
nsync_mu_semaphore_sem_fork_child();
_pthread_mutex_wipe_np(&__dlopen_lock_obj);
_pthread_mutex_wipe_np(&__rand64_lock_obj);
_pthread_mutex_wipe_np(&__fds_lock_obj);
@ -204,8 +202,8 @@ int _fork(uint32_t dwCreationFlags) {
struct CosmoTib *tib = __get_tls();
struct PosixThread *pt = (struct PosixThread *)tib->tib_pthread;
tid = IsLinux() || IsXnuSilicon() ? dx : sys_gettid();
atomic_init(&tib->tib_tid, tid);
atomic_init(&pt->ptid, tid);
atomic_init(&tib->tib_ctid, tid);
atomic_init(&tib->tib_ptid, tid);
// tracing and kisdangerous need this lock wiped a little earlier
atomic_init(&__maps.lock.word, 0);
@ -214,6 +212,11 @@ int _fork(uint32_t dwCreationFlags) {
* it's now safe to call normal functions again
*/
// this wipe must happen fast
void nsync_waiter_wipe_(void);
if (_weaken(nsync_waiter_wipe_))
_weaken(nsync_waiter_wipe_)();
// turn other threads into zombies
// we can't free() them since we're monopolizing all locks
// we assume the operating system already reclaimed system handles

View file

@ -120,11 +120,13 @@ WinThreadEntry(int rdi, // rcx
int rc;
if (wt->tls)
__set_tls_win32(wt->tls);
*wt->ctid = __imp_GetCurrentThreadId();
int tid = __imp_GetCurrentThreadId();
atomic_init(wt->ptid, tid);
atomic_init(wt->ctid, tid);
rc = __stack_call(wt->arg, wt->tid, 0, 0, wt->func, wt->sp);
// we can now clear ctid directly since we're no longer using our own
// stack memory, which can now be safely free'd by the parent thread.
*wt->ztid = 0;
atomic_store_explicit(wt->ztid, 0, memory_order_release);
__imp_WakeByAddressAll(wt->ztid);
// since we didn't indirect this function through NT2SYSV() it's not
// safe to simply return, and as such, we need ExitThread().
@ -146,6 +148,7 @@ static textwindows errno_t CloneWindows(int (*func)(void *, int), char *stk,
sp &= -alignof(struct CloneArgs);
wt = (struct CloneArgs *)sp;
wt->ctid = flags & CLONE_CHILD_SETTID ? ctid : &wt->tid;
wt->ptid = flags & CLONE_PARENT_SETTID ? ptid : &wt->tid;
wt->ztid = flags & CLONE_CHILD_CLEARTID ? ctid : &wt->tid;
wt->func = func;
wt->arg = arg;
@ -154,7 +157,7 @@ static textwindows errno_t CloneWindows(int (*func)(void *, int), char *stk,
if ((h = CreateThread(&kNtIsInheritable, 65536, (void *)WinThreadEntry, wt,
kNtStackSizeParamIsAReservation, &utid))) {
if (flags & CLONE_PARENT_SETTID)
*ptid = utid;
atomic_init(ptid, utid);
if (flags & CLONE_SETTLS) {
struct CosmoTib *tib = tls;
atomic_store_explicit(&tib->tib_syshand, h, memory_order_release);
@ -192,8 +195,8 @@ XnuThreadMain(void *pthread, // rdi
int ax;
wt->tid = tid;
*wt->ctid = tid;
*wt->ptid = tid;
atomic_init(wt->ctid, tid);
atomic_init(wt->ptid, tid);
if (wt->tls) {
// XNU uses the same 0x30 offset as the WIN32 TIB x64. They told the
@ -250,8 +253,8 @@ static errno_t CloneXnu(int (*fn)(void *), char *stk, size_t stksz, int flags,
wt = (struct CloneArgs *)sp;
// pass parameters to new thread via xnu
wt->ptid = flags & CLONE_PARENT_SETTID ? ptid : &wt->tid;
wt->ctid = flags & CLONE_CHILD_SETTID ? ctid : &wt->tid;
wt->ptid = flags & CLONE_PARENT_SETTID ? ptid : &wt->tid;
wt->ztid = flags & CLONE_CHILD_CLEARTID ? ctid : &wt->tid;
wt->tls = flags & CLONE_SETTLS ? tls : 0;
return sys_clone_xnu(fn, arg, wt, 0, PTHREAD_START_CUSTOM_XNU);
@ -264,7 +267,8 @@ static errno_t CloneXnu(int (*fn)(void *), char *stk, size_t stksz, int flags,
// 1. __asan_handle_no_return wipes stack [todo?]
relegated static wontreturn void OpenbsdThreadMain(void *p) {
struct CloneArgs *wt = p;
*wt->ctid = wt->tid;
atomic_init(wt->ptid, wt->tid);
atomic_init(wt->ctid, wt->tid);
wt->func(wt->arg, wt->tid);
asm volatile("mov\t%2,%%rsp\n\t" // so syscall can validate stack exists
"movl\t$0,(%%rdi)\n\t" // *wt->ztid = 0 (old stack now free'd)
@ -295,6 +299,7 @@ relegated errno_t CloneOpenbsd(int (*func)(void *, int), char *stk,
wt = (struct CloneArgs *)sp;
sp = AlignStack(sp, stk, stksz, 16);
wt->ctid = flags & CLONE_CHILD_SETTID ? ctid : &wt->tid;
wt->ptid = flags & CLONE_PARENT_SETTID ? ptid : &wt->tid;
wt->ztid = flags & CLONE_CHILD_CLEARTID ? ctid : &wt->tid;
wt->arg = arg;
wt->func = func;
@ -303,7 +308,7 @@ relegated errno_t CloneOpenbsd(int (*func)(void *, int), char *stk,
tf->tf_tid = &wt->tid;
if ((rc = __tfork_thread(tf, sizeof(*tf), OpenbsdThreadMain, wt)) >= 0) {
if (flags & CLONE_PARENT_SETTID)
*ptid = rc;
atomic_init(ptid, rc);
return 0;
} else {
return -rc;
@ -316,13 +321,16 @@ relegated errno_t CloneOpenbsd(int (*func)(void *, int), char *stk,
static wontreturn void NetbsdThreadMain(void *arg, // rdi
int (*func)(void *, int), // rsi
int flags, // rdx
atomic_int *ctid) { // rcx
atomic_int *ctid, // rcx
atomic_int *ptid) { // r8
int ax, dx;
static atomic_int clobber;
atomic_int *ztid = &clobber;
ax = sys_gettid();
if (flags & CLONE_CHILD_SETTID)
atomic_store_explicit(ctid, ax, memory_order_release);
atomic_init(ctid, ax);
if (flags & CLONE_PARENT_SETTID)
atomic_init(ptid, ax);
if (flags & CLONE_CHILD_CLEARTID)
ztid = ctid;
func(arg, ax);
@ -381,6 +389,7 @@ static int CloneNetbsd(int (*func)(void *, int), char *stk, size_t stksz,
ctx->uc_mcontext.rsi = (intptr_t)func;
ctx->uc_mcontext.rdx = flags;
ctx->uc_mcontext.rcx = (intptr_t)ctid;
ctx->uc_mcontext.r8 = (intptr_t)ptid;
ctx->uc_flags |= _UC_STACK;
ctx->uc_stack.ss_sp = stk;
ctx->uc_stack.ss_size = stksz;
@ -399,7 +408,7 @@ static int CloneNetbsd(int (*func)(void *, int), char *stk, size_t stksz,
if (!failed) {
unassert(tid);
if (flags & CLONE_PARENT_SETTID)
*ptid = tid;
atomic_init(ptid, tid);
return 0;
} else {
return ax;
@ -418,7 +427,8 @@ static wontreturn void FreebsdThreadMain(void *p) {
#elif defined(__x86_64__)
sys_set_tls(AMD64_SET_GSBASE, wt->tls);
#endif
*wt->ctid = wt->tid;
atomic_init(wt->ctid, wt->tid);
atomic_init(wt->ptid, wt->tid);
wt->func(wt->arg, wt->tid);
// we no longer use the stack after this point
// void thr_exit(%rdi = long *state);
@ -465,6 +475,7 @@ static errno_t CloneFreebsd(int (*func)(void *, int), char *stk, size_t stksz,
wt = (struct CloneArgs *)sp;
sp = AlignStack(sp, stk, stksz, 16);
wt->ctid = flags & CLONE_CHILD_SETTID ? ctid : &wt->tid;
wt->ptid = flags & CLONE_PARENT_SETTID ? ptid : &wt->tid;
wt->ztid = flags & CLONE_CHILD_CLEARTID ? ctid : &wt->tid;
wt->tls = tls;
wt->func = func;
@ -499,7 +510,7 @@ static errno_t CloneFreebsd(int (*func)(void *, int), char *stk, size_t stksz,
#error "unsupported architecture"
#endif
if (flags & CLONE_PARENT_SETTID)
*ptid = tid;
atomic_init(ptid, tid);
return 0;
}
@ -511,9 +522,10 @@ static errno_t CloneFreebsd(int (*func)(void *, int), char *stk, size_t stksz,
static void *SiliconThreadMain(void *arg) {
struct CloneArgs *wt = arg;
asm volatile("mov\tx28,%0" : /* no outputs */ : "r"(wt->tls));
*wt->ctid = wt->this;
atomic_init(wt->ctid, wt->this);
atomic_init(wt->ptid, wt->this);
__stack_call(wt->arg, wt->this, 0, 0, wt->func, wt->sp);
*wt->ztid = 0;
atomic_store_explicit(wt->ztid, 0, memory_order_release);
ulock_wake(UL_COMPARE_AND_WAIT | ULF_WAKE_ALL, wt->ztid, 0);
return 0;
}
@ -537,6 +549,7 @@ static errno_t CloneSilicon(int (*fn)(void *, int), char *stk, size_t stksz,
tid = atomic_fetch_add_explicit(&tids, 1, memory_order_acq_rel);
wt->this = tid = (tid % kMaxThreadIds) + kMinThreadId;
wt->ctid = flags & CLONE_CHILD_SETTID ? ctid : &wt->tid;
wt->ptid = flags & CLONE_PARENT_SETTID ? ptid : &wt->tid;
wt->ztid = flags & CLONE_CHILD_CLEARTID ? ctid : &wt->tid;
wt->tls = flags & CLONE_SETTLS ? tls : 0;
wt->func = fn;
@ -552,7 +565,7 @@ static errno_t CloneSilicon(int (*fn)(void *, int), char *stk, size_t stksz,
unassert(!__syslib->__pthread_attr_setstacksize(attr, babystack));
if (!(res = __syslib->__pthread_create(&th, attr, SiliconThreadMain, wt))) {
if (flags & CLONE_PARENT_SETTID)
*ptid = tid;
atomic_init(ptid, tid);
if (flags & CLONE_SETTLS) {
struct CosmoTib *tib = tls;
atomic_store_explicit(&tib[-1].tib_syshand, th, memory_order_release);
@ -637,7 +650,7 @@ static int CloneLinux(int (*func)(void *arg, int rc), char *stk, size_t stksz,
* If you use clone() you're on your own. Example:
*
* int worker(void *arg) { return 0; }
* struct CosmoTib tib = {.tib_self = &tib, .tib_tid = -1};
* struct CosmoTib tib = {.tib_self = &tib, .tib_ctid = -1};
* atomic_int tid;
* char *stk = NewCosmoStack();
* clone(worker, stk, GetStackSize() - 16,
@ -647,9 +660,9 @@ static int CloneLinux(int (*func)(void *arg, int rc), char *stk, size_t stksz,
* arg, &tid, &tib, &tib.tib_tid);
* while (atomic_load(&tid) == 0) sched_yield();
* // thread is known
* while (atomic_load(&tib.tib_tid) < 0) sched_yield();
* while (atomic_load(&tib.tib_ctid) < 0) sched_yield();
* // thread is running
* while (atomic_load(&tib.tib_tid) > 0) sched_yield();
* while (atomic_load(&tib.tib_ctid) > 0) sched_yield();
* // thread has terminated
* FreeCosmoStack(stk);
*

View file

@ -93,7 +93,8 @@ wontreturn textstartup void cosmo(long *sp, struct Syslib *m1, char *exename,
.tib_sigmask = -1,
.tib_sigstack_size = 57344,
.tib_sigstack_addr = (char *)__builtin_frame_address(0) - 57344,
.tib_tid = 1,
.tib_ptid = 1,
.tib_ctid = 1,
};
__set_tls(&tib);

View file

@ -23,7 +23,6 @@
#include "libc/nexgen32e/gc.internal.h"
#include "libc/thread/posixthread.internal.h"
#include "libc/thread/tls.h"
#include "third_party/nsync/wait_s.internal.h"
struct Dtor {
void *fun;
@ -89,10 +88,7 @@ void __cxa_thread_finalize(void) {
// thread has any thread-specific data, appropriate destructor
// functions shall be called in an unspecified order."
// ──Quoth POSIX.1-2017
if (tib->tib_nsync)
_weaken(nsync_waiter_destroy)(tib->tib_nsync);
_pthread_unkey(tib);
_pthread_ungarbage(tib);
while ((dtor = tib->tib_atexit)) {

View file

@ -233,7 +233,8 @@ textstartup void __enable_tls(void) {
} else {
tid = sys_gettid();
}
atomic_init(&tib->tib_tid, tid);
atomic_init(&tib->tib_ptid, tid);
atomic_init(&tib->tib_ctid, tid);
// TODO(jart): set_tid_address?
// inherit signal mask
@ -248,7 +249,6 @@ textstartup void __enable_tls(void) {
_pthread_static.pt_attr.__stacksize = __maps.stack.size;
dll_init(&_pthread_static.list);
_pthread_list = &_pthread_static.list;
atomic_init(&_pthread_static.ptid, tid);
// ask the operating system to change the x86 segment register
if (IsWindows())

View file

@ -169,7 +169,7 @@ int main(int argc, char *argv[]) {
// make sure threads are in a good state
if (_weaken(_pthread_decimate))
_weaken(_pthread_decimate)();
_weaken(_pthread_decimate)(kPosixThreadZombie);
if (_weaken(pthread_orphan_np) && !_weaken(pthread_orphan_np)()) {
tinyprint(2, "error: tests ended with threads still active\n", NULL);
_Exit(1);

View file

@ -40,10 +40,9 @@ static char *_mktls_finish(struct CosmoTib **out_tib, char *mem,
tib->tib_ftrace = old->tib_ftrace;
tib->tib_strace = old->tib_strace;
tib->tib_sigmask = old->tib_sigmask;
atomic_store_explicit(&tib->tib_tid, -1, memory_order_relaxed);
if (out_tib) {
atomic_init(&tib->tib_ctid, -1);
if (out_tib)
*out_tib = tib;
}
return mem;
}

View file

@ -75,7 +75,6 @@ struct PosixThread {
atomic_int pt_canceled; // 0x04: thread has bad beliefs
_Atomic(enum PosixThreadStatus) pt_status;
_Atomic(atomic_int *) pt_blocker;
atomic_int ptid; // transitions 0 → tid
atomic_int pt_refs; // prevents decimation
void *(*pt_start)(void *); // creation callback
void *pt_val; // start param / return val
@ -108,7 +107,7 @@ int _pthread_setschedparam_freebsd(int, int, const struct sched_param *);
int _pthread_tid(struct PosixThread *) libcesque;
intptr_t _pthread_syshand(struct PosixThread *) libcesque;
long _pthread_cancel_ack(void) libcesque;
void _pthread_decimate(void) libcesque;
void _pthread_decimate(enum PosixThreadStatus) libcesque;
void _pthread_free(struct PosixThread *) libcesque paramsnonnull();
void _pthread_lock(void) libcesque;
void _pthread_onfork_child(void) libcesque;

View file

@ -57,6 +57,7 @@
#include "libc/thread/posixthread.internal.h"
#include "libc/thread/thread.h"
#include "libc/thread/tls.h"
#include "third_party/nsync/wait_s.internal.h"
__static_yoink("nsync_mu_lock");
__static_yoink("nsync_mu_unlock");
@ -81,6 +82,10 @@ void _pthread_free(struct PosixThread *pt) {
cosmo_stack_free(pt->pt_attr.__stackaddr, pt->pt_attr.__stacksize,
pt->pt_attr.__guardsize);
// reclaim thread's cached nsync waiter object
if (pt->tib->tib_nsync)
nsync_waiter_destroy_(pt->tib->tib_nsync);
// free any additional upstream system resources
// our fork implementation wipes this handle in child automatically
uint64_t syshand =
@ -102,7 +107,7 @@ void _pthread_free(struct PosixThread *pt) {
3);
}
void _pthread_decimate(void) {
void _pthread_decimate(enum PosixThreadStatus threshold) {
struct PosixThread *pt;
struct Dll *e, *e2, *list = 0;
enum PosixThreadStatus status;
@ -117,11 +122,18 @@ void _pthread_decimate(void) {
pt = POSIXTHREAD_CONTAINER(e);
if (atomic_load_explicit(&pt->pt_refs, memory_order_acquire) > 0)
continue; // pthread_kill() has a lease on this thread
if (atomic_load_explicit(&pt->tib->tib_ctid, memory_order_acquire))
continue; // thread is still using stack so leave alone
status = atomic_load_explicit(&pt->pt_status, memory_order_acquire);
if (status != kPosixThreadZombie)
break; // zombies only exist at the end of the linked list
if (atomic_load_explicit(&pt->tib->tib_tid, memory_order_acquire))
continue; // undead thread that should stop existing soon
if (status < threshold) {
if (threshold == kPosixThreadZombie)
break; // zombies only exist at the end of the linked list
continue;
}
if (status == kPosixThreadTerminated)
if (!(pt->pt_flags & PT_STATIC))
STRACE("warning: you forgot to join or detach thread id %d",
atomic_load_explicit(&pt->tib->tib_ptid, memory_order_acquire));
dll_remove(&_pthread_list, e);
dll_make_first(&list, e);
}
@ -139,7 +151,7 @@ void _pthread_decimate(void) {
}
}
static int PosixThread(void *arg, int tid) {
dontinstrument static int PosixThread(void *arg, int tid) {
struct PosixThread *pt = arg;
// setup scheduling
@ -285,12 +297,12 @@ static errno_t pthread_create_impl(pthread_t *thread,
_pthread_ref(pt);
// launch PosixThread(pt) in new thread
if ((rc = clone(PosixThread, pt->pt_attr.__stackaddr, pt->pt_attr.__stacksize,
CLONE_VM | CLONE_THREAD | CLONE_FS | CLONE_FILES |
CLONE_SIGHAND | CLONE_SYSVSEM | CLONE_SETTLS |
CLONE_PARENT_SETTID | CLONE_CHILD_SETTID |
CLONE_CHILD_CLEARTID,
pt, &pt->ptid, __adj_tls(pt->tib), &pt->tib->tib_tid))) {
if ((rc = clone(
PosixThread, pt->pt_attr.__stackaddr, pt->pt_attr.__stacksize,
CLONE_VM | CLONE_THREAD | CLONE_FS | CLONE_FILES | CLONE_SIGHAND |
CLONE_SYSVSEM | CLONE_SETTLS | CLONE_PARENT_SETTID |
CLONE_CHILD_SETTID | CLONE_CHILD_CLEARTID,
pt, &pt->tib->tib_ptid, __adj_tls(pt->tib), &pt->tib->tib_ctid))) {
_pthread_lock();
dll_remove(&_pthread_list, &pt->list);
_pthread_unlock();
@ -363,7 +375,7 @@ static const char *DescribeHandle(char buf[12], errno_t err, pthread_t *th) {
errno_t pthread_create(pthread_t *thread, const pthread_attr_t *attr,
void *(*start_routine)(void *), void *arg) {
errno_t err;
_pthread_decimate();
_pthread_decimate(kPosixThreadZombie);
BLOCK_SIGNALS;
err = pthread_create_impl(thread, attr, start_routine, arg, _SigMask);
ALLOW_SIGNALS;

View file

@ -41,7 +41,7 @@
* @return 0 on success, or errno on error
*/
int pthread_decimate_np(void) {
_pthread_decimate();
_pthread_decimate(kPosixThreadZombie);
cosmo_stack_clear();
return 0;
}

View file

@ -18,11 +18,13 @@
*/
#include "libc/assert.h"
#include "libc/atomic.h"
#include "libc/calls/calls.h"
#include "libc/cosmo.h"
#include "libc/cxxabi.h"
#include "libc/dce.h"
#include "libc/intrin/atomic.h"
#include "libc/intrin/cxaatexit.h"
#include "libc/intrin/describebacktrace.h"
#include "libc/intrin/strace.h"
#include "libc/intrin/weaken.h"
#include "libc/limits.h"
@ -97,13 +99,15 @@ wontreturn void pthread_exit(void *rc) {
// notice how we avoid acquiring the pthread gil
if (!(population = atomic_fetch_sub(&_pthread_count, 1) - 1)) {
// we know for certain we're an orphan. any other threads that
// exist, will terminate and clear their tid very soon. but...
// some goofball could spawn more threads from atexit handlers
// exist, will terminate and clear their tid very soon. but some
// goofball could spawn more threads from atexit() handlers. we'd
// also like to avoid looping forever here, by auto-joining threads
// that leaked, because the user forgot to join them or detach them
for (;;) {
_pthread_decimate();
if (_weaken(__cxa_finalize))
_weaken(__cxa_finalize)(NULL);
_pthread_decimate(kPosixThreadTerminated);
if (pthread_orphan_np()) {
if (_weaken(__cxa_finalize))
_weaken(__cxa_finalize)(NULL);
population = atomic_load(&_pthread_count);
break;
}
@ -147,8 +151,8 @@ wontreturn void pthread_exit(void *rc) {
// check if the main thread has died whilst children live
// note that the main thread is joinable by child threads
if (pt->pt_flags & PT_STATIC) {
atomic_store_explicit(&tib->tib_tid, 0, memory_order_release);
cosmo_futex_wake((atomic_int *)&tib->tib_tid, INT_MAX,
atomic_store_explicit(&tib->tib_ctid, 0, memory_order_release);
cosmo_futex_wake((atomic_int *)&tib->tib_ctid, INT_MAX,
!IsWindows() && !IsXnu());
_Exit1(0);
}

View file

@ -67,7 +67,7 @@ static errno_t _pthread_wait(atomic_int *ctid, struct timespec *abstime) {
// thread argument to pthread_join() refers to the calling thread,
// it is recommended that the function should fail and report an
// [EDEADLK] error." ──Quoth POSIX.1-2017
if (ctid == &__get_tls()->tib_tid)
if (ctid == &__get_tls()->tib_ctid)
return EDEADLK;
// "If the thread calling pthread_join() is canceled, then the target
@ -134,7 +134,7 @@ errno_t pthread_timedjoin_np(pthread_t thread, void **value_ptr,
// "The results of multiple simultaneous calls to pthread_join()
// specifying the same target thread are undefined."
// ──Quoth POSIX.1-2017
if (!(err = _pthread_wait(&pt->tib->tib_tid, abstime))) {
if (!(err = _pthread_wait(&pt->tib->tib_ctid, abstime))) {
if (value_ptr)
*value_ptr = pt->pt_val;
if (atomic_load_explicit(&pt->pt_refs, memory_order_acquire)) {

View file

@ -23,10 +23,10 @@ struct CosmoTib {
struct CosmoTib *tib_self; /* 0x00 */
struct CosmoFtrace tib_ftracer; /* 0x08 */
void *tib_garbages; /* 0x18 */
intptr_t __unused; /* 0x20 */
_Atomic(int32_t) tib_ptid; /* 0x20 transitions 0 → tid */
intptr_t tib_pthread; /* 0x28 */
struct CosmoTib *tib_self2; /* 0x30 */
_Atomic(int32_t) tib_tid; /* 0x38 transitions -1 → tid → 0 */
_Atomic(int32_t) tib_ctid; /* 0x38 transitions -1 → tid → 0 */
int32_t tib_errno; /* 0x3c */
uint64_t tib_flags; /* 0x40 */
int tib_ftrace; /* inherited */