Make posix_spawn faster on Windows

This commit is contained in:
Justine Tunney 2023-08-20 02:13:02 -07:00
parent d6f72aa4a6
commit ec957491ea
No known key found for this signature in database
GPG key ID: BE714B4575D6E328
15 changed files with 514 additions and 123 deletions

View file

@ -205,6 +205,8 @@ o/$(MODE)/libc/calls/swapcontext.o: libc/calls/swapcontext.S
@$(COMPILE) -AOBJECTIFY.S $(OBJECTIFY.S) $(OUTPUT_OPTION) -c $<
o/$(MODE)/libc/calls/tailcontext.o: libc/calls/tailcontext.S
@$(COMPILE) -AOBJECTIFY.S $(OBJECTIFY.S) $(OUTPUT_OPTION) -c $<
o/$(MODE)/libc/calls/switchstacks.o: libc/calls/switchstacks.S
@$(COMPILE) -AOBJECTIFY.S $(OBJECTIFY.S) $(OUTPUT_OPTION) -c $<
LIBC_CALLS_LIBS = $(foreach x,$(LIBC_CALLS_ARTIFACTS),$($(x)))
LIBC_CALLS_SRCS = $(foreach x,$(LIBC_CALLS_ARTIFACTS),$($(x)_SRCS))

View file

@ -16,6 +16,7 @@
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/atomic.h"
#include "libc/calls/blockcancel.internal.h"
#include "libc/calls/calls.h"
#include "libc/calls/cp.internal.h"
@ -33,7 +34,7 @@
#include "libc/sysv/errfuns.h"
static struct CopyFileRange {
_Atomic(uint32_t) once;
atomic_uint once;
bool ok;
} g_copy_file_range;

View file

@ -22,8 +22,10 @@
#include "libc/calls/ntspawn.h"
#include "libc/calls/syscall-nt.internal.h"
#include "libc/fmt/itoa.h"
#include "libc/intrin/kprintf.h"
#include "libc/intrin/strace.internal.h"
#include "libc/intrin/weaken.h"
#include "libc/macros.internal.h"
#include "libc/mem/alloca.h"
#include "libc/nt/accounting.h"
#include "libc/nt/console.h"
@ -34,16 +36,22 @@
#include "libc/nt/struct/processinformation.h"
#include "libc/nt/struct/startupinfo.h"
#include "libc/nt/synchronization.h"
#include "libc/nt/thread.h"
#include "libc/nt/thunk/msabi.h"
#include "libc/runtime/memtrack.internal.h"
#include "libc/runtime/runtime.h"
#include "libc/runtime/stack.h"
#include "libc/sock/sock.h"
#include "libc/str/str.h"
#include "libc/sysv/consts/at.h"
#include "libc/sysv/consts/map.h"
#include "libc/sysv/consts/o.h"
#include "libc/sysv/consts/ok.h"
#include "libc/sysv/consts/sig.h"
#include "libc/sysv/errfuns.h"
#include "libc/thread/thread.h"
#define keywords textwindows dontasan dontubsan dontinstrument
extern long __klog_handle;
@ -51,18 +59,58 @@ __msabi extern typeof(CloseHandle) *const __imp_CloseHandle;
__msabi extern typeof(WaitForSingleObject) *const __imp_WaitForSingleObject;
__msabi extern typeof(GetExitCodeProcess) *const __imp_GetExitCodeProcess;
__msabi extern typeof(UnmapViewOfFile) *const __imp_UnmapViewOfFile;
__msabi extern typeof(TerminateThread) *const __imp_TerminateThread;
static dontinstrument __msabi bool32
BlockExecveConsoleEvent(uint32_t dwCtrlType) {
// block SIGINT and SIGQUIT in execve() parent process
return true;
wontreturn void __switch_stacks(intptr_t, long, long, long,
void (*)(intptr_t, intptr_t, long, long),
intptr_t);
__msabi static keywords bool32 sys_execve_nt_event(uint32_t dwCtrlType) {
return true; // block sigint and sigquit in execve() parent process
}
textwindows int sys_execve_nt(const char *program, char *const argv[],
char *const envp[]) {
static keywords void PurgeHandle(intptr_t h) {
if (h && h != -1) {
__imp_CloseHandle(h);
}
}
static keywords void PurgeThread(intptr_t h) {
if (h && h != -1) {
__imp_TerminateThread(h, SIGKILL);
__imp_CloseHandle(h);
}
}
// this function runs on the original tiny stack that windows gave us.
// we need to keep the original process alive simply to pass an int32.
// so we unmap all memory to avoid getting a double whammy after fork.
static keywords void sys_execve_nt_relay(intptr_t h, long b, long c, long d) {
uint32_t i, dwExitCode;
__imp_SetConsoleCtrlHandler((void *)sys_execve_nt_event, 1);
PurgeThread(g_fds.stdin.thread);
PurgeHandle(g_fds.stdin.reader);
PurgeHandle(g_fds.stdin.writer);
PurgeHandle(g_fds.p[0].handle);
PurgeHandle(g_fds.p[1].handle);
PurgeHandle(g_fds.p[2].handle);
for (i = 0; i < _mmi.i; ++i) {
__imp_UnmapViewOfFile((void *)((uintptr_t)_mmi.p[i].x << 16));
PurgeHandle(_mmi.p[i].h);
}
do {
__imp_WaitForSingleObject(h, -1);
dwExitCode = kNtStillActive;
__imp_GetExitCodeProcess(h, &dwExitCode);
} while (dwExitCode == kNtStillActive);
__imp_ExitProcess(dwExitCode);
__builtin_unreachable();
}
keywords int sys_execve_nt(const char *program, char *const argv[],
char *const envp[]) {
int rc;
size_t i;
uint32_t dwExitCode;
char progbuf[PATH_MAX];
struct NtStartupInfo startinfo;
struct NtProcessInformation procinfo;
@ -89,16 +137,26 @@ textwindows int sys_execve_nt(const char *program, char *const argv[],
//////////////////////////////////////////////////////////////////////////////
// execve operation is unrecoverable from this point
if (_weaken(pthread_kill_siblings_np)) {
_weaken(pthread_kill_siblings_np)();
}
// close non-stdio and cloexec handles
for (i = 0; i < g_fds.n; ++i) {
if (g_fds.p[i].kind == kFdEmpty) {
g_fds.p[i].handle = -1;
} else if (i > 2 || (g_fds.p[i].flags & O_CLOEXEC)) {
__imp_CloseHandle(g_fds.p[i].handle);
PurgeHandle(g_fds.p[i].handle);
g_fds.p[i].handle = -1;
}
}
if (_weaken(__klog_handle) && //
*_weaken(__klog_handle) != 0 && //
*_weaken(__klog_handle) != -1) {
PurgeHandle(*_weaken(__klog_handle));
}
int bits;
char buf[32], *v = 0;
if (_weaken(socket)) {
@ -117,12 +175,6 @@ textwindows int sys_execve_nt(const char *program, char *const argv[],
startinfo.hStdOutput = g_fds.p[1].handle;
startinfo.hStdError = g_fds.p[2].handle;
if (_weaken(__klog_handle) && //
*_weaken(__klog_handle) != 0 && //
*_weaken(__klog_handle) != -1) {
__imp_CloseHandle(*_weaken(__klog_handle));
}
// spawn the process
rc = ntspawn(program, argv, envp, v, 0, 0, true, 0, 0, &startinfo, &procinfo);
if (rc == -1) {
@ -131,19 +183,8 @@ textwindows int sys_execve_nt(const char *program, char *const argv[],
}
//////////////////////////////////////////////////////////////////////////////
// zombie shell process remains, to wait for child and propagate its exit
// code
// zombify this process which lingers on to relay the status code
__imp_CloseHandle(g_fds.p[0].handle);
__imp_CloseHandle(g_fds.p[1].handle);
__imp_CloseHandle(procinfo.hThread);
__imp_SetConsoleCtrlHandler((void *)BlockExecveConsoleEvent, 1);
do {
__imp_WaitForSingleObject(procinfo.hProcess, -1);
dwExitCode = kNtStillActive;
__imp_GetExitCodeProcess(procinfo.hProcess, &dwExitCode);
} while (dwExitCode == kNtStillActive);
__imp_CloseHandle(procinfo.hProcess);
__imp_ExitProcess(dwExitCode);
notpossible;
PurgeHandle(procinfo.hThread);
__switch_stacks(procinfo.hProcess, 0, 0, 0, sys_execve_nt_relay, __oldstack);
}

View file

@ -21,6 +21,8 @@
#include "libc/calls/syscall-sysv.internal.h"
#include "libc/dce.h"
#include "libc/intrin/strace.internal.h"
#include "libc/intrin/weaken.h"
#include "libc/nt/runtime.h"
#include "libc/runtime/internal.h"
#include "libc/sysv/consts/sicode.h"
#include "libc/sysv/consts/sig.h"
@ -62,10 +64,20 @@ int raise(int sig) {
RaiseSigFpe();
rc = 0;
#endif
} else if (!IsWindows() && !IsMetal()) {
} else if (IsLinux() || IsXnu() || IsFreebsd() || IsOpenbsd() || IsNetbsd()) {
rc = sys_tkill(gettid(), sig, 0);
} else if (IsWindows() || IsMetal()) {
if (IsWindows() && sig == SIGKILL) {
// TODO(jart): Isn't this implemented by __sig_raise()?
if (_weaken(__restore_console_win32)) {
_weaken(__restore_console_win32)();
}
ExitProcess(sig);
} else {
rc = __sig_raise(sig, SI_TKILL);
}
} else {
rc = __sig_raise(sig, SI_TKILL);
__builtin_unreachable();
}
STRACE("...raise(%G) → %d% m", sig, rc);
return rc;

View file

@ -41,6 +41,7 @@ struct StdinRelay {
int64_t handle; /* should == g_fds.p[0].handle */
int64_t reader; /* ReadFile() use this instead */
int64_t writer; /* only used by WinStdinThread */
int64_t thread; /* handle for the stdio thread */
};
struct Fds {

View file

@ -1,7 +1,7 @@
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi
/*-*- mode:unix-assembly; indent-tabs-mode:t; tab-width:8; coding:utf-8 -*-│
vi: set et ft=asm ts=8 tw=8 fenc=utf-8 :vi
Copyright 2022 Justine Alexandra Roberts Tunney
Copyright 2023 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
@ -16,38 +16,19 @@
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/calls/calls.h"
#include "libc/calls/sig.internal.h"
#include "libc/calls/syscall-sysv.internal.h"
#include "libc/calls/syscall_support-sysv.internal.h"
#include "libc/dce.h"
#include "libc/intrin/strace.internal.h"
#include "libc/sysv/consts/sicode.h"
#include "libc/macros.internal.h"
// OpenBSD has an optional `tib` parameter for extra safety
int __tkill(int tid, int sig, void *tib) {
int rc;
if (!IsWindows() && !IsMetal()) {
rc = sys_tkill(tid, sig, tib);
} else {
rc = __sig_add(tid, sig, SI_TKILL);
}
STRACE("tkill(%d, %G) %d% m", tid, sig, rc);
return rc;
}
/**
* Kills thread.
*
* @param tid is thread id
* @param sig does nothing on xnu
* @return 0 on success, or -1 w/ errno
* @raise EAGAIN if `RLIMIT_SIGPENDING` was exceeded
* @raise EINVAL if `tid` or `sig` were invalid
* @raise ESRCH if no such `tid` existed
* @raise EPERM if permission was denied
* @asyncsignalsafe
*/
int tkill(int tid, int sig) {
return __tkill(tid, sig, 0);
}
__switch_stacks:
#ifdef __x86_64__
mov %r9,%rsp
and $-16,%rsp
xor %rbp,%rbp
call *%r8
#elif defined(__aarch64__)
and sp,x5,#-16
mov x29,0
blr x4
#else
#error "unsupported architecture"
#endif
.endfn __switch_stacks,globl

View file

@ -135,8 +135,8 @@ dontasan dontubsan dontinstrument textwindows void WinMainStdin(void) {
NTTRACE("<stdin> CreateThread failed");
return;
}
__imp_CloseHandle(hThread);
g_fds.stdin.handle = hStdin;
g_fds.stdin.thread = hStdin;
g_fds.stdin.handle = hThread;
g_fds.stdin.reader = hReader;
g_fds.stdin.writer = hWriter;
g_fds.stdin.inisem = hSemaphore;