Skip to content

Commit

Permalink
Fix fork waiter leak in nsync
Browse files Browse the repository at this point in the history
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
  • Loading branch information
jart committed Dec 31, 2024
1 parent fd7da58 commit 98c5847
Show file tree
Hide file tree
Showing 35 changed files with 299 additions and 173 deletions.
2 changes: 1 addition & 1 deletion libc/intrin/gettid.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion libc/intrin/kprintf.greg.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
6 changes: 3 additions & 3 deletions libc/intrin/maps.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions libc/intrin/pthread_mutex_lock.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down
4 changes: 2 additions & 2 deletions libc/intrin/pthread_mutex_unlock.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
18 changes: 17 additions & 1 deletion libc/intrin/pthread_tid.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
5 changes: 4 additions & 1 deletion libc/intrin/wintlsinit.c
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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);
}

Expand Down
2 changes: 1 addition & 1 deletion libc/mem/leaks.c
Original file line number Diff line number Diff line change
Expand Up @@ -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");

Expand Down
3 changes: 0 additions & 3 deletions libc/proc/fork-nt.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
11 changes: 7 additions & 4 deletions libc/proc/fork.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand All @@ -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
Expand Down
51 changes: 32 additions & 19 deletions libc/runtime/clone.c
Original file line number Diff line number Diff line change
Expand Up @@ -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().
Expand All @@ -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;
Expand All @@ -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);
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand All @@ -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)
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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);
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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);
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}

Expand All @@ -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;
}
Expand All @@ -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;
Expand All @@ -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);
Expand Down Expand Up @@ -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,
Expand All @@ -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);
*
Expand Down
3 changes: 2 additions & 1 deletion libc/runtime/cosmo2.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
Loading

0 comments on commit 98c5847

Please sign in to comment.