Fix ordering of pthread_create(pthread_t *thread)

This change fixes a bug where signal_latency_async_test would flake less
than 1/1000 of the time. What was happening was pthread_kill(sender_thr)
would return EFAULT. This was because pthread_create() was not returning
the thread object pointer until after clone() had been called. So it was
actually possible for the main thread to stall after calling clone() and
during that time the receiver would launch and receive a signal from the
sender thread, and then fail when it tried to send a pong. I thought I'd
use a barrier at first, in the test, to synchronize thread creation, but
I firmly believe that pthread_create() was to blame and now that's fixed
This commit is contained in:
Justine Tunney 2025-01-03 17:28:39 -08:00
parent ed6d133a27
commit e939659b70
No known key found for this signature in database
GPG key ID: BE714B4575D6E328
6 changed files with 57 additions and 63 deletions

View file

@ -492,7 +492,7 @@ relegated bool TellOpenbsdThisIsStackMemory(void *addr, size_t size) {
// OpenBSD only permits RSP to occupy memory that's been explicitly
// defined as stack memory, i.e. `lo <= %rsp < hi` must be the case
relegated errno_t FixupCustomStackOnOpenbsd(pthread_attr_t *attr) {
relegated bool FixupCustomStackOnOpenbsd(pthread_attr_t *attr) {
// get interval
uintptr_t lo = (uintptr_t)attr->__stackaddr;
@ -503,15 +503,11 @@ relegated errno_t FixupCustomStackOnOpenbsd(pthread_attr_t *attr) {
hi = hi & -__pagesize;
// tell os it's stack memory
errno_t olderr = errno;
if (!TellOpenbsdThisIsStackMemory((void *)lo, hi - lo)) {
errno_t err = errno;
errno = olderr;
return err;
}
if (!TellOpenbsdThisIsStackMemory((void *)lo, hi - lo))
return false;
// update attributes with usable stack address
attr->__stackaddr = (void *)lo;
attr->__stacksize = hi - lo;
return 0;
return true;
}

View file

@ -8,7 +8,7 @@ void cosmo_stack_unlock(void);
void cosmo_stack_wipe(void);
bool TellOpenbsdThisIsStackMemory(void *, size_t);
errno_t FixupCustomStackOnOpenbsd(pthread_attr_t *);
bool FixupCustomStackOnOpenbsd(pthread_attr_t *);
COSMOPOLITAN_C_END_
#endif /* COSMOPOLITAN_LIBC_STACK_H_ */

View file

@ -128,7 +128,7 @@ forceinline pureconst struct PosixThread *_pthread_self(void) {
}
forceinline void _pthread_ref(struct PosixThread *pt) {
atomic_fetch_add_explicit(&pt->pt_refs, 1, memory_order_acq_rel);
atomic_fetch_add_explicit(&pt->pt_refs, 1, memory_order_relaxed);
}
forceinline void _pthread_unref(struct PosixThread *pt) {

View file

@ -199,14 +199,12 @@ static errno_t pthread_create_impl(pthread_t *thread,
const pthread_attr_t *attr,
void *(*start_routine)(void *), void *arg,
sigset_t oldsigs) {
int rc, e = errno;
errno_t err;
struct PosixThread *pt;
// create posix thread object
if (!(pt = calloc(1, sizeof(struct PosixThread)))) {
errno = e;
if (!(pt = calloc(1, sizeof(struct PosixThread))))
return EAGAIN;
}
dll_init(&pt->list);
pt->pt_locale = &__global_locale;
pt->pt_start = start_routine;
@ -215,7 +213,6 @@ static errno_t pthread_create_impl(pthread_t *thread,
// create thread local storage memory
if (!(pt->pt_tls = _mktls(&pt->tib))) {
free(pt);
errno = e;
return EAGAIN;
}
@ -232,9 +229,9 @@ static errno_t pthread_create_impl(pthread_t *thread,
// caller supplied their own stack
// assume they know what they're doing as much as possible
if (IsOpenbsd()) {
if ((rc = FixupCustomStackOnOpenbsd(&pt->pt_attr))) {
if (!FixupCustomStackOnOpenbsd(&pt->pt_attr)) {
_pthread_free(pt);
return rc;
return EPERM;
}
}
} else {
@ -259,7 +256,7 @@ static errno_t pthread_create_impl(pthread_t *thread,
if (!(pt->pt_attr.__sigaltstackaddr =
malloc(pt->pt_attr.__sigaltstacksize))) {
_pthread_free(pt);
return errno;
return EAGAIN;
}
pt->pt_flags |= PT_OWNSIGALTSTACK;
}
@ -282,35 +279,41 @@ static errno_t pthread_create_impl(pthread_t *thread,
memory_order_relaxed);
break;
default:
_pthread_free(pt);
return EINVAL;
// pthread_attr_setdetachstate() makes this impossible
__builtin_unreachable();
}
// if pthread_attr_setdetachstate() was used then it's possible for
// the `pt` object to be freed before this clone call has returned!
atomic_store_explicit(&pt->pt_refs, 1, memory_order_relaxed);
// add thread to global list
// we add it to the beginning since zombies go at the end
_pthread_lock();
dll_make_first(&_pthread_list, &pt->list);
_pthread_unlock();
// if pthread_attr_setdetachstate() was used then it's possible for
// the `pt` object to be freed before this clone call has returned!
_pthread_ref(pt);
// we don't normally do this, but it's important to write the result
// memory before spawning the thread, so it's visible to the threads
*thread = (pthread_t)pt;
// launch PosixThread(pt) in new thread
if ((rc = clone(
if ((err = 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))) {
*thread = 0; // posix doesn't require we do this
_pthread_lock();
dll_remove(&_pthread_list, &pt->list);
_pthread_unlock();
_pthread_free(pt);
return rc;
if (err == ENOMEM)
err = EAGAIN;
return err;
}
*thread = (pthread_t)pt;
return 0;
}
@ -359,7 +362,7 @@ static const char *DescribeHandle(char buf[12], errno_t err, pthread_t *th) {
*
*
* @param thread is used to output the thread id upon success, which
* must be non-null
* must be non-null; upon failure, its value is undefined
* @param attr points to launch configuration, or may be null
* to use sensible defaults; it must be initialized using
* pthread_attr_init()
@ -375,6 +378,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;
errno_t olderr = errno;
_pthread_decimate(kPosixThreadZombie);
BLOCK_SIGNALS;
err = pthread_create_impl(thread, attr, start_routine, arg, _SigMask);
@ -382,7 +386,10 @@ errno_t pthread_create(pthread_t *thread, const pthread_attr_t *attr,
STRACE("pthread_create([%s], %p, %t, %p) → %s",
DescribeHandle(alloca(12), err, thread), attr, start_routine, arg,
DescribeErrno(err));
if (!err)
if (!err) {
_pthread_unref(*(struct PosixThread **)thread);
} else {
errno = olderr;
}
return err;
}