mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-02-24 14:59:03 +00:00
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
244 lines
8.7 KiB
C
244 lines
8.7 KiB
C
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
|
|
│ vi: set et ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi │
|
|
╞══════════════════════════════════════════════════════════════════════════════╡
|
|
│ Copyright 2022 Justine Alexandra Roberts Tunney │
|
|
│ │
|
|
│ Permission to use, copy, modify, and/or distribute this software for │
|
|
│ any purpose with or without fee is hereby granted, provided that the │
|
|
│ above copyright notice and this permission notice appear in all copies. │
|
|
│ │
|
|
│ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │
|
|
│ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │
|
|
│ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │
|
|
│ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │
|
|
│ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │
|
|
│ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │
|
|
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
|
|
│ PERFORMANCE OF THIS SOFTWARE. │
|
|
╚─────────────────────────────────────────────────────────────────────────────*/
|
|
#include "libc/atomic.h"
|
|
#include "libc/calls/calls.h"
|
|
#include "libc/calls/struct/sigaction.h"
|
|
#include "libc/calls/struct/timespec.h"
|
|
#include "libc/errno.h"
|
|
#include "libc/fmt/itoa.h"
|
|
#include "libc/fmt/magnumstrs.internal.h"
|
|
#include "libc/intrin/atomic.h"
|
|
#include "libc/intrin/kprintf.h"
|
|
#include "libc/intrin/weaken.h"
|
|
#include "libc/runtime/internal.h"
|
|
#include "libc/runtime/runtime.h"
|
|
#include "libc/runtime/stack.h"
|
|
#include "libc/runtime/symbols.internal.h"
|
|
#include "libc/str/str.h"
|
|
#include "libc/sysv/consts/clone.h"
|
|
#include "libc/sysv/consts/sig.h"
|
|
#include "libc/thread/thread.h"
|
|
#include "libc/thread/tls.h"
|
|
#include "third_party/nsync/mu.h"
|
|
|
|
/**
|
|
* @fileoverview Austere Mutex Test
|
|
*
|
|
* 1. Tests what happens when *NSYNC isn't linked.
|
|
* 2. Tests what happens when TLS isn't enabled.
|
|
*
|
|
* If either:
|
|
*
|
|
* 1. malloc() isn't linked, or
|
|
* 2. we're in MODE=tiny
|
|
*
|
|
* Then we use Cosmopolitan's hand-rolled tiny locks. They're not
|
|
* scalable. They use a lot of CPU if you have lots of threads. But
|
|
* they're wicked fast and free of bloat if your app is tiny.
|
|
*/
|
|
|
|
atomic_int ready;
|
|
atomic_int counter;
|
|
atomic_int success;
|
|
pthread_mutex_t mu;
|
|
|
|
#define ASSERT_EQ(WANT, GOT) \
|
|
do { \
|
|
long _want = WANT, _got = GOT; \
|
|
if (_want != _got) \
|
|
__assert_eq_fail(__FILE__, __LINE__, #WANT, #GOT, _want, _got); \
|
|
} while (0)
|
|
|
|
void ignore_signal(int sig) {
|
|
}
|
|
|
|
void __assert_eq_fail(const char *file, int line, const char *wantstr,
|
|
const char *gotstr, long want, long got) {
|
|
kprintf("%s:%d: %s vs. %s was %ld vs. %ld (%s)\n", file, line, wantstr,
|
|
gotstr, want, got, !(got & ~255) ? _strerrno(got) : "n/a");
|
|
_Exit(1);
|
|
}
|
|
|
|
double time2dbl(struct timespec t) {
|
|
return (((double)t.tv_sec) + ((double)t.tv_nsec * 1e-9));
|
|
}
|
|
|
|
char *time2str(double s) {
|
|
static char buf[32];
|
|
static const struct {
|
|
const char *suffix;
|
|
double multiplier;
|
|
} scale[] = {
|
|
{"ns", 1.0e-9}, //
|
|
{"us", 1e-6}, //
|
|
{"ms", 1e-3}, //
|
|
{"s", 1.0}, //
|
|
{"hr", 3600.0}, //
|
|
};
|
|
int i = 0;
|
|
while (i + 1 != sizeof(scale) / sizeof(scale[0]) &&
|
|
scale[i + 1].multiplier <= s) {
|
|
i++;
|
|
}
|
|
stpcpy(FormatInt32(buf, s / scale[i].multiplier), scale[i].suffix);
|
|
return buf;
|
|
}
|
|
|
|
int Worker(void *arg) {
|
|
int i, x;
|
|
atomic_store(&ready, 1);
|
|
for (i = 0; i < 10000; ++i) {
|
|
ASSERT_EQ(0, pthread_mutex_lock(&mu));
|
|
x = atomic_load_explicit(&counter, memory_order_relaxed);
|
|
atomic_store_explicit(&counter, x + 1, memory_order_relaxed);
|
|
ASSERT_EQ(x + 1, atomic_load_explicit(&counter, memory_order_relaxed));
|
|
ASSERT_EQ(0, pthread_mutex_unlock(&mu));
|
|
}
|
|
atomic_store(&success, 1);
|
|
return 0;
|
|
}
|
|
|
|
void TestContendedLock(const char *name, int kind) {
|
|
char *stk;
|
|
double ns;
|
|
errno_t rc;
|
|
int x, i, n = 10000;
|
|
struct timespec t1, t2;
|
|
pthread_mutexattr_t attr;
|
|
struct CosmoTib tib = {
|
|
.tib_self = &tib,
|
|
.tib_self2 = &tib,
|
|
.tib_ctid = -1,
|
|
.tib_ptid = 0,
|
|
};
|
|
pthread_mutexattr_init(&attr);
|
|
pthread_mutexattr_settype(&attr, kind);
|
|
pthread_mutex_init(&mu, &attr);
|
|
pthread_mutexattr_destroy(&attr);
|
|
atomic_store(&ready, 0);
|
|
atomic_store(&success, 0);
|
|
stk = NewCosmoStack();
|
|
rc = clone(Worker, stk, GetStackSize() - 16 /* openbsd:stackbound */,
|
|
CLONE_VM | CLONE_THREAD | CLONE_FS | CLONE_FILES | CLONE_SIGHAND |
|
|
CLONE_SYSVSEM | CLONE_PARENT_SETTID | CLONE_CHILD_SETTID |
|
|
CLONE_CHILD_CLEARTID | CLONE_SETTLS,
|
|
0, &tib.tib_ptid, &tib, &tib.tib_ctid);
|
|
if (rc) {
|
|
kprintf("clone failed: %s\n", strerror(rc));
|
|
_Exit(1);
|
|
}
|
|
while (!atomic_load(&ready))
|
|
donothing;
|
|
t1 = timespec_real();
|
|
for (i = 0; i < n; ++i) {
|
|
ASSERT_EQ(0, pthread_mutex_lock(&mu));
|
|
x = atomic_load_explicit(&counter, memory_order_relaxed);
|
|
atomic_store_explicit(&counter, x - 1, memory_order_relaxed);
|
|
ASSERT_EQ(x - 1, atomic_load_explicit(&counter, memory_order_relaxed));
|
|
ASSERT_EQ(0, pthread_mutex_unlock(&mu));
|
|
}
|
|
t2 = timespec_real();
|
|
while (tib.tib_ctid)
|
|
donothing;
|
|
ASSERT_EQ(1, atomic_load(&success));
|
|
ASSERT_EQ(0, atomic_load(&counter));
|
|
FreeCosmoStack(stk);
|
|
ASSERT_EQ(0, pthread_mutex_destroy(&mu));
|
|
ns = time2dbl(timespec_sub(t2, t1)) / n;
|
|
kprintf("%s contended took %s\n", name, time2str(ns));
|
|
}
|
|
|
|
void TestUncontendedLock(const char *name, int kind) {
|
|
double ns;
|
|
long i, n = 10000;
|
|
struct timespec t1, t2;
|
|
pthread_mutex_t lock;
|
|
pthread_mutexattr_t attr;
|
|
pthread_mutexattr_init(&attr);
|
|
pthread_mutexattr_settype(&attr, kind);
|
|
pthread_mutex_init(&lock, &attr);
|
|
pthread_mutexattr_destroy(&attr);
|
|
t1 = timespec_real();
|
|
for (i = 0; i < n; ++i) {
|
|
pthread_mutex_lock(&lock);
|
|
pthread_mutex_unlock(&lock);
|
|
}
|
|
t2 = timespec_real();
|
|
pthread_mutex_destroy(&lock);
|
|
ns = time2dbl(timespec_sub(t2, t1)) / n;
|
|
kprintf("%s took %s\n", name, time2str(ns));
|
|
}
|
|
|
|
int main(int argc, char *argv[]) {
|
|
pthread_mutexattr_t attr;
|
|
|
|
#ifdef MODE_DBG
|
|
GetSymbolTable();
|
|
signal(SIGTRAP, ignore_signal);
|
|
kprintf("running %s\n", argv[0]);
|
|
#endif
|
|
|
|
#ifdef __aarch64__
|
|
// our usage of raw clone() is probably broken in aarch64
|
|
// we should just get rid of clone()
|
|
if (1)
|
|
return 0;
|
|
#endif
|
|
|
|
if (_weaken(nsync_mu_lock)) {
|
|
kprintf("*NSYNC should not be linked\n");
|
|
_Exit(1);
|
|
}
|
|
|
|
ASSERT_EQ(0, pthread_mutexattr_init(&attr));
|
|
ASSERT_EQ(0, pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_DEFAULT));
|
|
ASSERT_EQ(0, pthread_mutex_init(&mu, &attr));
|
|
ASSERT_EQ(0, pthread_mutexattr_destroy(&attr));
|
|
ASSERT_EQ(0, pthread_mutex_lock(&mu));
|
|
ASSERT_EQ(0, pthread_mutex_unlock(&mu));
|
|
ASSERT_EQ(0, pthread_mutex_destroy(&mu));
|
|
|
|
ASSERT_EQ(0, pthread_mutexattr_init(&attr));
|
|
ASSERT_EQ(0, pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE));
|
|
ASSERT_EQ(0, pthread_mutex_init(&mu, &attr));
|
|
ASSERT_EQ(0, pthread_mutexattr_destroy(&attr));
|
|
ASSERT_EQ(0, pthread_mutex_lock(&mu));
|
|
ASSERT_EQ(0, pthread_mutex_lock(&mu));
|
|
ASSERT_EQ(0, pthread_mutex_unlock(&mu));
|
|
ASSERT_EQ(0, pthread_mutex_unlock(&mu));
|
|
ASSERT_EQ(0, pthread_mutex_destroy(&mu));
|
|
|
|
ASSERT_EQ(0, pthread_mutexattr_init(&attr));
|
|
ASSERT_EQ(0, pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ERRORCHECK));
|
|
ASSERT_EQ(0, pthread_mutex_init(&mu, &attr));
|
|
ASSERT_EQ(0, pthread_mutexattr_destroy(&attr));
|
|
ASSERT_EQ(0, pthread_mutex_lock(&mu));
|
|
ASSERT_EQ(EDEADLK, pthread_mutex_lock(&mu));
|
|
ASSERT_EQ(0, pthread_mutex_unlock(&mu));
|
|
ASSERT_EQ(0, pthread_mutex_destroy(&mu));
|
|
|
|
TestUncontendedLock("PTHREAD_MUTEX_DEFAULT RAW TLS", PTHREAD_MUTEX_DEFAULT);
|
|
TestUncontendedLock("PTHREAD_MUTEX_RECURSIVE RAW TLS",
|
|
PTHREAD_MUTEX_RECURSIVE);
|
|
|
|
TestContendedLock("PTHREAD_MUTEX_DEFAULT RAW TLS", PTHREAD_MUTEX_DEFAULT);
|
|
TestContendedLock("PTHREAD_MUTEX_RECURSIVE RAW TLS", PTHREAD_MUTEX_RECURSIVE);
|
|
|
|
//
|
|
}
|