Make execve() linger when it can't spoof parent

It's now possible to use execve() when the parent process isn't built by
cosmo. In such cases, the current process will kill all threads and then
linger around, waiting for the newly created process to die, and then we
propagate its exit code to the parent. This should help bazel and others

Allocating private anonymous memory is now 5x faster on Windows. This is
thanks to VirtualAlloc() which is faster than the file mapping APIs. The
fork() function also now goes 30% faster, since we are able to avoid the
VirtualProtect() calls on mappings in most cases now.

Fixes #1253
This commit is contained in:
Justine Tunney 2025-01-04 21:11:53 -08:00
parent c97a858470
commit 42a3bb729a
No known key found for this signature in database
GPG key ID: BE714B4575D6E328
40 changed files with 612 additions and 656 deletions

View file

@ -24,16 +24,23 @@
#include "libc/calls/syscall-nt.internal.h"
#include "libc/errno.h"
#include "libc/fmt/itoa.h"
#include "libc/intrin/dll.h"
#include "libc/intrin/fds.h"
#include "libc/intrin/kprintf.h"
#include "libc/intrin/strace.h"
#include "libc/mem/mem.h"
#include "libc/nt/accounting.h"
#include "libc/nt/enum/processaccess.h"
#include "libc/nt/enum/startf.h"
#include "libc/nt/enum/status.h"
#include "libc/nt/errors.h"
#include "libc/nt/files.h"
#include "libc/nt/process.h"
#include "libc/nt/runtime.h"
#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/proc/describefds.internal.h"
#include "libc/proc/ntspawn.h"
@ -41,6 +48,7 @@
#include "libc/str/str.h"
#include "libc/sysv/consts/at.h"
#include "libc/sysv/consts/o.h"
#include "libc/sysv/consts/sig.h"
#include "libc/sysv/errfuns.h"
#include "libc/thread/posixthread.internal.h"
#include "libc/thread/thread.h"
@ -65,13 +73,11 @@ textwindows int sys_execve_nt(const char *program, char *const argv[],
_pthread_lock(); // order matters
// new process should be a child of our parent
int64_t hParentProcess;
int ppid = sys_getppid_nt();
if (!(hParentProcess = OpenProcess(
kNtProcessDupHandle | kNtProcessCreateProcess, false, ppid))) {
sys_execve_nt_abort(sigmask);
return -1;
}
int64_t hParentProcess =
sys_getppid_nt_win32
? OpenProcess(kNtProcessDupHandle | kNtProcessCreateProcess, false,
sys_getppid_nt_win32)
: 0;
// inherit pid
char pidvar[11 + 21];
@ -81,6 +87,16 @@ textwindows int sys_execve_nt(const char *program, char *const argv[],
char maskvar[6 + 21];
FormatUint64(stpcpy(maskvar, "_MASK="), sigmask);
// inherit parent process id
char ppidvar[12 + 21 + 1 + 21 + 1], *p = ppidvar;
p = stpcpy(p, "_COSMO_PPID=");
if (hParentProcess) {
p = FormatUint64(p, sys_getppid_nt_win32);
*p++ = ':';
p = FormatUint64(p, __pid);
setenv("_COSMO_PPID", ppidvar, true);
}
// define stdio handles for the spawned subprocess
struct NtStartupInfo si = {
.cb = sizeof(struct NtStartupInfo),
@ -94,13 +110,22 @@ textwindows int sys_execve_nt(const char *program, char *const argv[],
}
}
// which process is responsible for spawning the child?
int64_t hCreatorProcess;
if (hParentProcess) {
hCreatorProcess = hParentProcess;
} else {
hCreatorProcess = GetCurrentProcess();
}
// pass serialized file descriptor table in environment
char *fdspec;
int64_t *lpExplicitHandles;
uint32_t dwExplicitHandleCount;
if (!(fdspec = __describe_fds(g_fds.p, g_fds.n, &si, hParentProcess,
if (!(fdspec = __describe_fds(g_fds.p, g_fds.n, &si, hCreatorProcess,
&lpExplicitHandles, &dwExplicitHandleCount))) {
CloseHandle(hParentProcess);
if (hParentProcess)
CloseHandle(hParentProcess);
sys_execve_nt_abort(sigmask);
return -1;
}
@ -114,12 +139,14 @@ textwindows int sys_execve_nt(const char *program, char *const argv[],
// launch the process
struct NtProcessInformation pi;
int rc = ntspawn(&(struct NtSpawnArgs){
AT_FDCWD, program, argv, envp, (char *[]){fdspec, maskvar, pidvar, 0}, 0,
0, hParentProcess, lpExplicitHandles, dwExplicitHandleCount, &si, &pi});
__undescribe_fds(hParentProcess, lpExplicitHandles, dwExplicitHandleCount);
AT_FDCWD, program, argv, envp,
(char *[]){fdspec, maskvar, pidvar, ppidvar, 0}, 0, 0, hCreatorProcess,
lpExplicitHandles, dwExplicitHandleCount, &si, &pi});
__undescribe_fds(hCreatorProcess, lpExplicitHandles, dwExplicitHandleCount);
if (rc == -1) {
free(fdspec);
CloseHandle(hParentProcess);
if (hParentProcess)
CloseHandle(hParentProcess);
sys_execve_nt_abort(sigmask);
if (GetLastError() == kNtErrorSharingViolation) {
return etxtbsy();
@ -128,18 +155,55 @@ textwindows int sys_execve_nt(const char *program, char *const argv[],
}
}
// give child to libc/proc/proc.c worker thread in parent
int64_t handle;
if (DuplicateHandle(GetCurrentProcess(), pi.hProcess, hParentProcess, &handle,
0, false, kNtDuplicateSameAccess)) {
unassert(!(handle & 0xFFFFFFFFFF000000));
__imp_TerminateProcess(-1, 0x23000000u | handle);
} else {
// TODO(jart): Why does `make loc` print this?
// kprintf("DuplicateHandle failed w/ %d\n", GetLastError());
__imp_TerminateProcess(-1, ECHILD);
// check if parent spoofing worked
if (hParentProcess) {
// give child to libc/proc/proc.c worker thread in parent
int64_t handle;
if (DuplicateHandle(GetCurrentProcess(), pi.hProcess, hParentProcess,
&handle, 0, false, kNtDuplicateSameAccess)) {
unassert(!(handle & 0xFFFFFFFFFF000000));
__imp_TerminateProcess(-1, 0x23000000u | handle);
} else {
// TODO(jart): Why does `make loc` print this?
// kprintf("DuplicateHandle failed w/ %d\n", GetLastError());
__imp_TerminateProcess(-1, ECHILD);
}
__builtin_unreachable();
}
// we couldn't reparent the new process
STRACE("warning: execve() lingering due to non-cosmo parent process");
// terminate other threads
struct Dll *e;
struct PosixThread *me = _pthread_self();
for (e = dll_first(_pthread_list); e; e = dll_next(_pthread_list, e)) {
struct PosixThread *pt = POSIXTHREAD_CONTAINER(e);
if (pt == me)
continue;
TerminateThread(
atomic_load_explicit(&pt->tib->tib_syshand, memory_order_relaxed),
SIGKILL);
}
// wait for child to terminate and propagate exit code
for (;;) {
uint32_t status;
WaitForSingleObject(pi.hProcess, -1u);
GetExitCodeProcess(pi.hProcess, &status);
if (status != kNtStillActive) {
if ((status & 0xFF000000u) == 0x23000000u) {
// handle child execve()
CloseHandle(pi.hProcess);
pi.hProcess = status & 0x00FFFFFF;
} else {
// handle child _Exit()
if (status == 0xc9af3d51u)
status = kNtStillActive;
TerminateThisProcess(status);
}
}
}
__builtin_unreachable();
}
#endif /* __x86_64__ */