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

@ -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 */