Make improvements

- We now serialize the file descriptor table when spawning / executing
  processes on Windows. This means you can now inherit more stuff than
  just standard i/o. It's needed by bash, which duplicates the console
  to file descriptor #255. We also now do a better job serializing the
  environment variables, so you're less likely to encounter E2BIG when
  using your bash shell. We also no longer coerce environ to uppercase

- execve() on Windows now remotely controls its parent process to make
  them spawn a replacement for itself. Then it'll be able to terminate
  immediately once the spawn succeeds, without having to linger around
  for the lifetime as a shell process for proxying the exit code. When
  process worker thread running in the parent sees the child die, it's
  given a handle to the new child, to replace it in the process table.

- execve() and posix_spawn() on Windows will now provide CreateProcess
  an explicit handle list. This allows us to remove handle locks which
  enables better fork/spawn concurrency, with seriously correct thread
  safety. Other codebases like Go use the same technique. On the other
  hand fork() still favors the conventional WIN32 inheritence approach
  which can be a little bit messy, but is *controlled* by guaranteeing
  perfectly clean slates at both the spawning and execution boundaries

- sigset_t is now 64 bits. Having it be 128 bits was a mistake because
  there's no reason to use that and it's only supported by FreeBSD. By
  using the system word size, signal mask manipulation on Windows goes
  very fast. Furthermore @asyncsignalsafe funcs have been rewritten on
  Windows to take advantage of signal masking, now that it's much more
  pleasant to use.

- All the overlapped i/o code on Windows has been rewritten for pretty
  good signal and cancelation safety. We're now able to ensure overlap
  data structures are cleaned up so long as you don't longjmp() out of
  out of a signal handler that interrupted an i/o operation. Latencies
  are also improved thanks to the removal of lots of "busy wait" code.
  Waits should be optimal for everything except poll(), which shall be
  the last and final demon we slay in the win32 i/o horror show.

- getrusage() on Windows is now able to report RUSAGE_CHILDREN as well
  as RUSAGE_SELF, thanks to aggregation in the process manager thread.
This commit is contained in:
Justine Tunney 2023-10-08 05:36:18 -07:00
parent af7cb3c82f
commit 791f79fcb3
No known key found for this signature in database
GPG key ID: BE714B4575D6E328
382 changed files with 4008 additions and 4511 deletions

View file

@ -18,9 +18,9 @@
*/
#include "libc/assert.h"
#include "libc/atomic.h"
#include "libc/calls/blocksigs.internal.h"
#include "libc/calls/calls.h"
#include "libc/calls/struct/sigset.h"
#include "libc/calls/struct/sigset.internal.h"
#include "libc/calls/syscall-sysv.internal.h"
#include "libc/dce.h"
#include "libc/errno.h"
@ -31,7 +31,6 @@
#include "libc/intrin/bsr.h"
#include "libc/intrin/describeflags.internal.h"
#include "libc/intrin/dll.h"
#include "libc/intrin/kprintf.h"
#include "libc/intrin/strace.internal.h"
#include "libc/intrin/weaken.h"
#include "libc/log/internal.h"
@ -70,7 +69,7 @@ __static_yoink("_pthread_atfork");
void _pthread_free(struct PosixThread *pt, bool isfork) {
if (pt->pt_flags & PT_STATIC) return;
if (pt->pt_flags & PT_OWNSTACK) {
unassert(!munmap(pt->attr.__stackaddr, pt->attr.__stacksize));
unassert(!munmap(pt->pt_attr.__stackaddr, pt->pt_attr.__stacksize));
}
if (!isfork) {
if (IsWindows()) {
@ -83,28 +82,28 @@ void _pthread_free(struct PosixThread *pt, bool isfork) {
}
}
}
free(pt->tls);
free(pt->pt_tls);
free(pt);
}
static int PosixThread(void *arg, int tid) {
void *rc;
struct PosixThread *pt = arg;
if (pt->attr.__inheritsched == PTHREAD_EXPLICIT_SCHED) {
if (pt->pt_attr.__inheritsched == PTHREAD_EXPLICIT_SCHED) {
unassert(_weaken(_pthread_reschedule));
_weaken(_pthread_reschedule)(pt); // yoinked by attribute builder
}
// set long jump handler so pthread_exit can bring control back here
if (!setjmp(pt->exiter)) {
pthread_sigmask(SIG_SETMASK, (sigset_t *)pt->attr.__sigmask, 0);
rc = pt->start(pt->arg);
if (!setjmp(pt->pt_exiter)) {
pthread_sigmask(SIG_SETMASK, &pt->pt_attr.__sigmask, 0);
rc = pt->pt_start(pt->pt_arg);
// ensure pthread_cleanup_pop(), and pthread_exit() popped cleanup
unassert(!pt->cleanup);
unassert(!pt->pt_cleanup);
// calling pthread_exit() will either jump back here, or call exit
pthread_exit(rc);
}
// avoid signal handler being triggered after we trash our own stack
_sigblockall();
__sig_block();
// return to clone polyfill which clears tid, wakes futex, and exits
return 0;
}
@ -152,16 +151,11 @@ static errno_t pthread_create_impl(pthread_t *thread,
errno = e;
return EAGAIN;
}
pt->start = start_routine;
pt->arg = arg;
if (IsWindows()) {
if (!(pt->semaphore = CreateSemaphore(0, 0, 1, 0))) {
notpossible;
}
}
pt->pt_start = start_routine;
pt->pt_arg = arg;
// create thread local storage memory
if (!(pt->tls = _mktls(&pt->tib))) {
if (!(pt->pt_tls = _mktls(&pt->tib))) {
free(pt);
errno = e;
return EAGAIN;
@ -169,18 +163,18 @@ static errno_t pthread_create_impl(pthread_t *thread,
// setup attributes
if (attr) {
pt->attr = *attr;
pt->pt_attr = *attr;
attr = 0;
} else {
pthread_attr_init(&pt->attr);
pthread_attr_init(&pt->pt_attr);
}
// setup stack
if (pt->attr.__stackaddr) {
if (pt->pt_attr.__stackaddr) {
// caller supplied their own stack
// assume they know what they're doing as much as possible
if (IsOpenbsd()) {
if ((rc = FixupCustomStackOnOpenbsd(&pt->attr))) {
if ((rc = FixupCustomStackOnOpenbsd(&pt->pt_attr))) {
_pthread_free(pt, false);
return rc;
}
@ -191,38 +185,39 @@ static errno_t pthread_create_impl(pthread_t *thread,
// 2. in public world optimize to *work* regardless of memory
int granularity = FRAMESIZE;
int pagesize = getauxval(AT_PAGESZ);
pt->attr.__guardsize = ROUNDUP(pt->attr.__guardsize, pagesize);
pt->attr.__stacksize = ROUNDUP(pt->attr.__stacksize, granularity);
if (pt->attr.__guardsize + pagesize > pt->attr.__stacksize) {
pt->pt_attr.__guardsize = ROUNDUP(pt->pt_attr.__guardsize, pagesize);
pt->pt_attr.__stacksize = ROUNDUP(pt->pt_attr.__stacksize, granularity);
if (pt->pt_attr.__guardsize + pagesize > pt->pt_attr.__stacksize) {
_pthread_free(pt, false);
return EINVAL;
}
if (pt->attr.__guardsize == pagesize) {
pt->attr.__stackaddr =
mmap(0, pt->attr.__stacksize, PROT_READ | PROT_WRITE,
if (pt->pt_attr.__guardsize == pagesize) {
pt->pt_attr.__stackaddr =
mmap(0, pt->pt_attr.__stacksize, PROT_READ | PROT_WRITE,
MAP_STACK | MAP_ANONYMOUS, -1, 0);
} else {
pt->attr.__stackaddr =
mmap(0, pt->attr.__stacksize, PROT_READ | PROT_WRITE,
pt->pt_attr.__stackaddr =
mmap(0, pt->pt_attr.__stacksize, PROT_READ | PROT_WRITE,
MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
if (pt->attr.__stackaddr != MAP_FAILED) {
if (pt->pt_attr.__stackaddr != MAP_FAILED) {
if (IsOpenbsd() &&
__sys_mmap(
pt->attr.__stackaddr, pt->attr.__stacksize,
pt->pt_attr.__stackaddr, pt->pt_attr.__stacksize,
PROT_READ | PROT_WRITE,
MAP_PRIVATE | MAP_FIXED | MAP_ANON_OPENBSD | MAP_STACK_OPENBSD,
-1, 0, 0) != pt->attr.__stackaddr) {
-1, 0, 0) != pt->pt_attr.__stackaddr) {
notpossible;
}
if (pt->attr.__guardsize) {
if (pt->pt_attr.__guardsize) {
if (!IsWindows()) {
if (mprotect(pt->attr.__stackaddr, pt->attr.__guardsize,
if (mprotect(pt->pt_attr.__stackaddr, pt->pt_attr.__guardsize,
PROT_NONE)) {
notpossible;
}
} else {
uint32_t oldattr;
if (!VirtualProtect(pt->attr.__stackaddr, pt->attr.__guardsize,
if (!VirtualProtect(pt->pt_attr.__stackaddr,
pt->pt_attr.__guardsize,
kNtPageReadwrite | kNtPageGuard, &oldattr)) {
notpossible;
}
@ -230,7 +225,7 @@ static errno_t pthread_create_impl(pthread_t *thread,
}
}
}
if (!pt->attr.__stackaddr || pt->attr.__stackaddr == MAP_FAILED) {
if (!pt->pt_attr.__stackaddr || pt->pt_attr.__stackaddr == MAP_FAILED) {
rc = errno;
_pthread_free(pt, false);
errno = e;
@ -241,8 +236,8 @@ static errno_t pthread_create_impl(pthread_t *thread,
}
}
pt->pt_flags |= PT_OWNSTACK;
if (IsAsan() && !IsWindows() && pt->attr.__guardsize) {
__asan_poison(pt->attr.__stackaddr, pt->attr.__guardsize,
if (IsAsan() && !IsWindows() && pt->pt_attr.__guardsize) {
__asan_poison(pt->pt_attr.__stackaddr, pt->pt_attr.__guardsize,
kAsanStackOverflow);
}
}
@ -250,17 +245,17 @@ static errno_t pthread_create_impl(pthread_t *thread,
// set initial status
pt->tib->tib_pthread = (pthread_t)pt;
atomic_store_explicit(&pt->tib->tib_sigmask, -1, memory_order_relaxed);
if (!pt->attr.__havesigmask) {
pt->attr.__havesigmask = true;
memcpy(pt->attr.__sigmask, &oldsigs, sizeof(oldsigs));
if (!pt->pt_attr.__havesigmask) {
pt->pt_attr.__havesigmask = true;
pt->pt_attr.__sigmask = oldsigs;
}
switch (pt->attr.__detachstate) {
switch (pt->pt_attr.__detachstate) {
case PTHREAD_CREATE_JOINABLE:
atomic_store_explicit(&pt->status, kPosixThreadJoinable,
atomic_store_explicit(&pt->pt_status, kPosixThreadJoinable,
memory_order_relaxed);
break;
case PTHREAD_CREATE_DETACHED:
atomic_store_explicit(&pt->status, kPosixThreadDetached,
atomic_store_explicit(&pt->pt_status, kPosixThreadDetached,
memory_order_relaxed);
break;
default:
@ -271,21 +266,21 @@ static errno_t pthread_create_impl(pthread_t *thread,
// add thread to global list
// we add it to the beginning since zombies go at the end
dll_init(&pt->list);
pthread_spin_lock(&_pthread_lock);
_pthread_lock();
dll_make_first(&_pthread_list, &pt->list);
pthread_spin_unlock(&_pthread_lock);
_pthread_unlock();
// launch PosixThread(pt) in new thread
if ((rc = clone(PosixThread, pt->attr.__stackaddr,
pt->attr.__stacksize - (IsOpenbsd() ? 16 : 0),
if ((rc = clone(PosixThread, pt->pt_attr.__stackaddr,
pt->pt_attr.__stacksize - (IsOpenbsd() ? 16 : 0),
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))) {
pthread_spin_lock(&_pthread_lock);
_pthread_lock();
dll_remove(&_pthread_list, &pt->list);
pthread_spin_unlock(&_pthread_lock);
_pthread_unlock();
_pthread_free(pt, false);
return rc;
}