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

@ -596,7 +596,7 @@ ape_stack_offset = 0;
ape_stack_vaddr = DEFINED(ape_stack_vaddr) ? ape_stack_vaddr : 0x700000000000; ape_stack_vaddr = DEFINED(ape_stack_vaddr) ? ape_stack_vaddr : 0x700000000000;
ape_stack_paddr = ape_ram_paddr + ape_ram_filesz; ape_stack_paddr = ape_ram_paddr + ape_ram_filesz;
ape_stack_filesz = 0; ape_stack_filesz = 0;
ape_stack_memsz = DEFINED(ape_stack_memsz) ? ape_stack_memsz : 8 * 1024 * 1024; ape_stack_memsz = DEFINED(ape_stack_memsz) ? ape_stack_memsz : 4 * 1024 * 1024;
ape_note_offset = ape_cod_offset + (ape_note - ape_cod_vaddr); ape_note_offset = ape_cod_offset + (ape_note - ape_cod_vaddr);
ape_note_filesz = ape_note_end - ape_note; ape_note_filesz = ape_note_end - ape_note;

View file

@ -67,10 +67,9 @@ textstartup void InitializeMetalFile(void) {
size_t size = ROUNDUP(_ezip - __executable_start, 4096); size_t size = ROUNDUP(_ezip - __executable_start, 4096);
// TODO(jart): Restore support for ZIPOS on metal. // TODO(jart): Restore support for ZIPOS on metal.
void *copied_base; void *copied_base;
struct DirectMap dm; void *addr = sys_mmap_metal(NULL, size, PROT_READ | PROT_WRITE,
dm = sys_mmap_metal(NULL, size, PROT_READ | PROT_WRITE,
MAP_SHARED_linux | MAP_ANONYMOUS_linux, -1, 0); MAP_SHARED_linux | MAP_ANONYMOUS_linux, -1, 0);
copied_base = dm.addr; copied_base = addr;
npassert(copied_base != (void *)-1); npassert(copied_base != (void *)-1);
memcpy(copied_base, (void *)(BANE + IMAGE_BASE_PHYSICAL), size); memcpy(copied_base, (void *)(BANE + IMAGE_BASE_PHYSICAL), size);
__ape_com_base = copied_base; __ape_com_base = copied_base;

View file

@ -49,11 +49,9 @@ int sys_openat_metal(int dirfd, const char *file, int flags, unsigned mode) {
if ((fd = __reservefd(-1)) == -1) if ((fd = __reservefd(-1)) == -1)
return -1; return -1;
if (!_weaken(calloc) || !_weaken(free)) { if (!_weaken(calloc) || !_weaken(free)) {
struct DirectMap dm; state = sys_mmap_metal(NULL, ROUNDUP(sizeof(struct MetalFile), 4096),
dm = sys_mmap_metal(NULL, ROUNDUP(sizeof(struct MetalFile), 4096), PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS,
PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, -1, -1, 0);
0);
state = dm.addr;
if (state == (void *)-1) if (state == (void *)-1)
return -1; return -1;
} else { } else {

View file

@ -2,6 +2,9 @@
#define COSMOPOLITAN_LIBC_CALLS_SYSCALL_NT_INTERNAL_H_ #define COSMOPOLITAN_LIBC_CALLS_SYSCALL_NT_INTERNAL_H_
COSMOPOLITAN_C_START_ COSMOPOLITAN_C_START_
extern int sys_getppid_nt_cosmo;
extern int sys_getppid_nt_win32;
bool32 sys_isatty(int); bool32 sys_isatty(int);
int sys_chdir_nt(const char *); int sys_chdir_nt(const char *);
int sys_dup_nt(int, int, int, int); int sys_dup_nt(int, int, int, int);
@ -37,6 +40,7 @@ int sys_unlinkat_nt(int, const char *, int);
int64_t sys_lseek_nt(int, int64_t, int); int64_t sys_lseek_nt(int, int64_t, int);
ssize_t sys_read_nt_impl(int, void *, size_t, int64_t); ssize_t sys_read_nt_impl(int, void *, size_t, int64_t);
ssize_t sys_readlinkat_nt(int, const char *, char *, size_t); ssize_t sys_readlinkat_nt(int, const char *, char *, size_t);
void sys_getppid_nt_wipe(int, int);
COSMOPOLITAN_C_END_ COSMOPOLITAN_C_END_
#endif /* COSMOPOLITAN_LIBC_CALLS_SYSCALL_NT_INTERNAL_H_ */ #endif /* COSMOPOLITAN_LIBC_CALLS_SYSCALL_NT_INTERNAL_H_ */

View file

@ -59,7 +59,7 @@ textwindows int sys_clock_gettime_nt(int clock, struct timespec *ts) {
// —Quoth MSDN § Windows Time // —Quoth MSDN § Windows Time
// //
QueryUnbiasedInterruptTimePrecise(&hectons); QueryUnbiasedInterruptTimePrecise(&hectons);
*ts = timespec_fromnanos(hectons * 100); *ts = WindowsDurationToTimeSpec(hectons);
return 0; return 0;
case _CLOCK_MONOTONIC_COARSE: case _CLOCK_MONOTONIC_COARSE:
// //
@ -83,7 +83,7 @@ textwindows int sys_clock_gettime_nt(int clock, struct timespec *ts) {
// —Quoth MSDN § QueryUnbiasedInterruptTimePrecise // —Quoth MSDN § QueryUnbiasedInterruptTimePrecise
// //
QueryUnbiasedInterruptTime(&hectons); QueryUnbiasedInterruptTime(&hectons);
*ts = timespec_fromnanos(hectons * 100); *ts = WindowsDurationToTimeSpec(hectons);
return 0; return 0;
case _CLOCK_BOOTTIME: case _CLOCK_BOOTTIME:
// //
@ -95,7 +95,7 @@ textwindows int sys_clock_gettime_nt(int clock, struct timespec *ts) {
// —Quoth MSDN § Interrupt Time // —Quoth MSDN § Interrupt Time
// //
QueryInterruptTimePrecise(&hectons); QueryInterruptTimePrecise(&hectons);
*ts = timespec_fromnanos(hectons * 100); *ts = WindowsDurationToTimeSpec(hectons);
return 0; return 0;
case _CLOCK_PROCESS_CPUTIME_ID: case _CLOCK_PROCESS_CPUTIME_ID:
GetProcessTimes(GetCurrentProcess(), &ftCreation, &ftExit, &ftKernel, GetProcessTimes(GetCurrentProcess(), &ftCreation, &ftExit, &ftKernel,

View file

@ -0,0 +1,32 @@
/*-*- 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 2024 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/intrin/describeflags.h"
#include "libc/macros.h"
#include "libc/nt/enum/memflags.h"
static const struct DescribeFlags kNtAllocationTypeFlags[] = {
{kNtMemCommit, "Commit"}, //
{kNtMemReserve, "Reserve"}, //
{kNtMemReset, "Reset"}, //
};
const char *_DescribeNtAllocationType(char buf[48], uint32_t x) {
return _DescribeFlags(buf, 48, kNtAllocationTypeFlags,
ARRAYLEN(kNtAllocationTypeFlags), "kNtMem", x);
}

View file

@ -29,6 +29,7 @@ const char *_DescribeMapping(char[8], int, int) libcesque;
const char *_DescribeMremapFlags(char[30], int) libcesque; const char *_DescribeMremapFlags(char[30], int) libcesque;
const char *_DescribeMsg(char[16], int) libcesque; const char *_DescribeMsg(char[16], int) libcesque;
const char *_DescribeMsyncFlags(char[48], int) libcesque; const char *_DescribeMsyncFlags(char[48], int) libcesque;
const char *_DescribeNtAllocationType(char[48], uint32_t);
const char *_DescribeNtConsoleInFlags(char[256], uint32_t) libcesque; const char *_DescribeNtConsoleInFlags(char[256], uint32_t) libcesque;
const char *_DescribeNtConsoleOutFlags(char[128], uint32_t) libcesque; const char *_DescribeNtConsoleOutFlags(char[128], uint32_t) libcesque;
const char *_DescribeNtCreationDisposition(uint32_t) libcesque; const char *_DescribeNtCreationDisposition(uint32_t) libcesque;
@ -87,6 +88,7 @@ const char *_DescribeWhichPrio(char[12], int) libcesque;
#define DescribeMremapFlags(x) _DescribeMremapFlags(alloca(30), x) #define DescribeMremapFlags(x) _DescribeMremapFlags(alloca(30), x)
#define DescribeMsg(x) _DescribeMsg(alloca(16), x) #define DescribeMsg(x) _DescribeMsg(alloca(16), x)
#define DescribeMsyncFlags(x) _DescribeMsyncFlags(alloca(48), x) #define DescribeMsyncFlags(x) _DescribeMsyncFlags(alloca(48), x)
#define DescribeNtAllocationType(x) _DescribeNtAllocationType(alloca(48), x)
#define DescribeNtConsoleInFlags(x) _DescribeNtConsoleInFlags(alloca(256), x) #define DescribeNtConsoleInFlags(x) _DescribeNtConsoleInFlags(alloca(256), x)
#define DescribeNtConsoleOutFlags(x) _DescribeNtConsoleOutFlags(alloca(128), x) #define DescribeNtConsoleOutFlags(x) _DescribeNtConsoleOutFlags(alloca(128), x)
#define DescribeNtFileAccessFlags(x) _DescribeNtFileAccessFlags(alloca(512), x) #define DescribeNtFileAccessFlags(x) _DescribeNtFileAccessFlags(alloca(512), x)

View file

@ -19,7 +19,6 @@
#include "libc/calls/calls.h" #include "libc/calls/calls.h"
#include "libc/calls/internal.h" #include "libc/calls/internal.h"
#include "libc/calls/metalfile.internal.h" #include "libc/calls/metalfile.internal.h"
#include "libc/intrin/directmap.h"
#include "libc/macros.h" #include "libc/macros.h"
#include "libc/runtime/pc.internal.h" #include "libc/runtime/pc.internal.h"
#include "libc/str/str.h" #include "libc/str/str.h"
@ -32,19 +31,11 @@
static uint64_t sys_mmap_metal_break; static uint64_t sys_mmap_metal_break;
static struct DirectMap bad_mmap(void) { void *sys_mmap_metal(void *vaddr, size_t size, int prot, int flags, int fd,
struct DirectMap res; int64_t off) {
res.addr = (void *)-1;
res.maphandle = -1;
return res;
}
struct DirectMap sys_mmap_metal(void *vaddr, size_t size, int prot, int flags,
int fd, int64_t off) {
/* asan runtime depends on this function */ /* asan runtime depends on this function */
size_t i; size_t i;
struct mman *mm; struct mman *mm;
struct DirectMap res;
uint64_t addr, faddr = 0, page, e, *pte, *fdpte, *pml4t; uint64_t addr, faddr = 0, page, e, *pte, *fdpte, *pml4t;
mm = __get_mm(); mm = __get_mm();
pml4t = __get_pml4t(); pml4t = __get_pml4t();
@ -54,18 +45,18 @@ struct DirectMap sys_mmap_metal(void *vaddr, size_t size, int prot, int flags,
struct Fd *sfd; struct Fd *sfd;
struct MetalFile *file; struct MetalFile *file;
if (off < 0 || fd < 0 || fd >= g_fds.n) if (off < 0 || fd < 0 || fd >= g_fds.n)
return bad_mmap(); return MAP_FAILED;
sfd = &g_fds.p[fd]; sfd = &g_fds.p[fd];
if (sfd->kind != kFdFile) if (sfd->kind != kFdFile)
return bad_mmap(); return MAP_FAILED;
file = (struct MetalFile *)sfd->handle; file = (struct MetalFile *)sfd->handle;
/* TODO: allow mapping partial page at end of file, if file size not /* TODO: allow mapping partial page at end of file, if file size not
* multiple of page size */ * multiple of page size */
if (off > file->size || size > file->size - off) if (off > file->size || size > file->size - off)
return bad_mmap(); return MAP_FAILED;
faddr = (uint64_t)file->base + off; faddr = (uint64_t)file->base + off;
if (faddr % 4096 != 0) if (faddr % 4096 != 0)
return bad_mmap(); return MAP_FAILED;
} }
if (!(flags & MAP_FIXED_linux)) { if (!(flags & MAP_FIXED_linux)) {
if (!addr) { if (!addr) {
@ -88,7 +79,7 @@ struct DirectMap sys_mmap_metal(void *vaddr, size_t size, int prot, int flags,
if ((flags & MAP_ANONYMOUS_linux)) { if ((flags & MAP_ANONYMOUS_linux)) {
page = __new_page(mm); page = __new_page(mm);
if (!page) if (!page)
return bad_mmap(); return MAP_FAILED;
__clear_page(BANE + page); __clear_page(BANE + page);
e = page | PAGE_RSRV | PAGE_U; e = page | PAGE_RSRV | PAGE_U;
if ((prot & PROT_WRITE)) if ((prot & PROT_WRITE))
@ -114,9 +105,7 @@ struct DirectMap sys_mmap_metal(void *vaddr, size_t size, int prot, int flags,
break; break;
} }
} }
res.addr = (void *)addr; return (void *)addr;
res.maphandle = -1;
return res;
} }
#endif /* __x86_64__ */ #endif /* __x86_64__ */

View file

@ -1,122 +0,0 @@
/*-*- 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 2020 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/assert.h"
#include "libc/calls/internal.h"
#include "libc/calls/state.internal.h"
#include "libc/errno.h"
#include "libc/intrin/directmap.h"
#include "libc/nt/enum/filemapflags.h"
#include "libc/nt/enum/pageflags.h"
#include "libc/nt/errors.h"
#include "libc/nt/memory.h"
#include "libc/nt/runtime.h"
#include "libc/nt/struct/processmemorycounters.h"
#include "libc/nt/struct/securityattributes.h"
#include "libc/sysv/consts/map.h"
#include "libc/sysv/consts/o.h"
#include "libc/sysv/consts/prot.h"
textwindows struct DirectMap sys_mmap_nt(void *addr, size_t size, int prot,
int flags, int fd, int64_t off) {
int64_t handle;
if (flags & MAP_ANONYMOUS) {
handle = kNtInvalidHandleValue;
} else {
handle = g_fds.p[fd].handle;
}
// mark map handle as inheritable if fork might need it
const struct NtSecurityAttributes *mapsec;
if ((flags & MAP_TYPE) == MAP_SHARED) {
mapsec = &kNtIsInheritable;
} else {
mapsec = 0;
}
// nt will whine under many circumstances if we change the execute bit
// later using mprotect(). the workaround is to always request execute
// and then virtualprotect() it away until we actually need it. please
// note that open-nt.c always requests an kNtGenericExecute accessmask
int iscow = false;
struct ProtectNt fl;
if (handle != -1) {
if ((flags & MAP_TYPE) != MAP_SHARED) {
// windows has cow pages but they can't propagate across fork()
// that means we only get copy-on-write for the root process :(
fl = (struct ProtectNt){kNtPageExecuteWritecopy,
kNtFileMapCopy | kNtFileMapExecute};
iscow = true;
} else {
if ((g_fds.p[fd].flags & O_ACCMODE) == O_RDONLY) {
fl = (struct ProtectNt){kNtPageExecuteRead,
kNtFileMapRead | kNtFileMapExecute};
} else {
fl = (struct ProtectNt){kNtPageExecuteReadwrite,
kNtFileMapWrite | kNtFileMapExecute};
}
}
} else {
unassert(flags & MAP_ANONYMOUS);
fl = (struct ProtectNt){kNtPageExecuteReadwrite,
kNtFileMapWrite | kNtFileMapExecute};
}
int e = errno;
struct DirectMap dm;
TryAgain:
if ((dm.maphandle = CreateFileMapping(handle, mapsec, fl.flags1,
(size + off) >> 32, (size + off), 0))) {
if ((dm.addr = MapViewOfFileEx(dm.maphandle, fl.flags2, off >> 32, off,
size, addr))) {
uint32_t oldprot;
if (VirtualProtect(dm.addr, size, __prot2nt(prot, iscow), &oldprot))
return dm;
UnmapViewOfFile(dm.addr);
}
CloseHandle(dm.maphandle);
} else if (!(prot & PROT_EXEC) && //
(fl.flags2 & kNtFileMapExecute) && //
GetLastError() == kNtErrorAccessDenied) {
// your file needs to have been O_CREAT'd with exec `mode` bits in
// order to be mapped with executable permission. we always try to
// get execute permission if the kernel will give it to us because
// win32 would otherwise forbid mprotect() from elevating later on
fl.flags2 &= ~kNtFileMapExecute;
switch (fl.flags1) {
case kNtPageExecuteWritecopy:
fl.flags1 = kNtPageWritecopy;
break;
case kNtPageExecuteReadwrite:
fl.flags1 = kNtPageReadwrite;
break;
case kNtPageExecuteRead:
fl.flags1 = kNtPageReadonly;
break;
default:
__builtin_unreachable();
}
errno = e;
goto TryAgain;
}
dm.maphandle = kNtInvalidHandleValue;
dm.addr = (void *)(intptr_t)-1;
return dm;
}

View file

@ -1,67 +0,0 @@
/*-*- 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 2020 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/intrin/directmap.h"
#include "libc/calls/calls.h"
#include "libc/calls/syscall-sysv.internal.h"
#include "libc/dce.h"
#include "libc/errno.h"
#include "libc/intrin/describebacktrace.h"
#include "libc/intrin/describeflags.h"
#include "libc/intrin/strace.h"
#include "libc/nt/runtime.h"
#include "libc/runtime/memtrack.internal.h"
#include "libc/runtime/runtime.h"
#include "libc/runtime/syslib.internal.h"
#include "libc/sysv/errfuns.h"
/**
* Obtains memory mapping directly from system.
*
* The mmap() function needs to track memory mappings in order to
* support Windows NT and Address Sanitizer. That memory tracking can be
* bypassed by calling this function. However the caller is responsible
* for passing the magic memory handle on Windows NT to CloseHandle().
*
* @asyncsignalsafe
*/
struct DirectMap sys_mmap(void *addr, size_t size, int prot, int flags, int fd,
int64_t off) {
struct DirectMap d;
if ((__virtualsize += size) >= __virtualmax) {
d.maphandle = kNtInvalidHandleValue;
d.addr = (void *)enomem();
} else if (IsXnuSilicon()) {
long p = _sysret(__syslib->__mmap(addr, size, prot, flags, fd, off));
d.maphandle = kNtInvalidHandleValue;
d.addr = (void *)p;
} else if (!IsWindows() && !IsMetal()) {
d.addr = __sys_mmap(addr, size, prot, flags, fd, off, off);
d.maphandle = kNtInvalidHandleValue;
} else if (IsMetal()) {
d = sys_mmap_metal(addr, size, prot, flags, fd, off);
} else {
d = sys_mmap_nt(addr, size, prot, flags, fd, off);
}
if (d.addr == MAP_FAILED)
__virtualsize -= size;
KERNTRACE("sys_mmap(%.12p, %'zu, %s, %s, %d, %'ld) → {%.12p, %p}% m", addr,
size, DescribeProtFlags(prot), DescribeMapFlags(flags), fd, off,
d.addr, d.maphandle);
return d;
}

View file

@ -2,19 +2,7 @@
#define COSMOPOLITAN_LIBC_INTRIN_DIRECTMAP_H_ #define COSMOPOLITAN_LIBC_INTRIN_DIRECTMAP_H_
COSMOPOLITAN_C_START_ COSMOPOLITAN_C_START_
struct ProtectNt { void *sys_mmap_metal(void *, size_t, int, int, int, int64_t) libcesque;
uint32_t flags1;
uint32_t flags2;
};
struct DirectMap {
void *addr;
int64_t maphandle;
};
struct DirectMap sys_mmap(void *, size_t, int, int, int, int64_t);
struct DirectMap sys_mmap_nt(void *, size_t, int, int, int, int64_t);
struct DirectMap sys_mmap_metal(void *, size_t, int, int, int, int64_t);
int sys_munmap_metal(void *, size_t) libcesque; int sys_munmap_metal(void *, size_t) libcesque;
int __prot2nt(int, int) libcesque; int __prot2nt(int, int) libcesque;

View file

@ -18,6 +18,7 @@
*/ */
#include "libc/calls/calls.h" #include "libc/calls/calls.h"
#include "libc/calls/internal.h" #include "libc/calls/internal.h"
#include "libc/calls/state.internal.h"
#include "libc/calls/syscall-sysv.internal.h" #include "libc/calls/syscall-sysv.internal.h"
#include "libc/calls/syscall_support-nt.internal.h" #include "libc/calls/syscall_support-nt.internal.h"
#include "libc/dce.h" #include "libc/dce.h"
@ -33,10 +34,14 @@
#include "libc/intrin/weaken.h" #include "libc/intrin/weaken.h"
#include "libc/limits.h" #include "libc/limits.h"
#include "libc/macros.h" #include "libc/macros.h"
#include "libc/nt/enum/filemapflags.h"
#include "libc/nt/enum/memflags.h" #include "libc/nt/enum/memflags.h"
#include "libc/nt/enum/pageflags.h"
#include "libc/nt/errors.h"
#include "libc/nt/memory.h" #include "libc/nt/memory.h"
#include "libc/nt/runtime.h" #include "libc/nt/runtime.h"
#include "libc/runtime/runtime.h" #include "libc/runtime/runtime.h"
#include "libc/runtime/syslib.internal.h"
#include "libc/runtime/zipos.internal.h" #include "libc/runtime/zipos.internal.h"
#include "libc/stdckdint.h" #include "libc/stdckdint.h"
#include "libc/stdio/sysparam.h" #include "libc/stdio/sysparam.h"
@ -80,6 +85,11 @@
} while (0) } while (0)
#endif #endif
struct DirectMap {
void *addr;
int64_t hand;
};
int __maps_compare(const struct Tree *ra, const struct Tree *rb) { int __maps_compare(const struct Tree *ra, const struct Tree *rb) {
const struct Map *a = (const struct Map *)MAP_TREE_CONTAINER(ra); const struct Map *a = (const struct Map *)MAP_TREE_CONTAINER(ra);
const struct Map *b = (const struct Map *)MAP_TREE_CONTAINER(rb); const struct Map *b = (const struct Map *)MAP_TREE_CONTAINER(rb);
@ -421,7 +431,7 @@ void __maps_insert(struct Map *map) {
__maps_check(); __maps_check();
} }
// adds interval to rbtree (no sys_mmap) // adds interval to rbtree
bool __maps_track(char *addr, size_t size, int prot, int flags) { bool __maps_track(char *addr, size_t size, int prot, int flags) {
struct Map *map; struct Map *map;
if (!(map = __maps_alloc())) if (!(map = __maps_alloc()))
@ -447,6 +457,125 @@ int __maps_untrack(char *addr, size_t size) {
return rc; return rc;
} }
textwindows dontinline static struct DirectMap sys_mmap_nt(
void *addr, size_t size, int prot, int flags, int fd, int64_t off) {
struct DirectMap dm;
// it's 5x faster
if (IsWindows() && (flags & MAP_ANONYMOUS) &&
(flags & MAP_TYPE) != MAP_SHARED) {
if (!(dm.addr = VirtualAlloc(addr, size, kNtMemReserve | kNtMemCommit,
__prot2nt(prot, false)))) {
dm.addr = MAP_FAILED;
}
dm.hand = MAPS_VIRTUAL;
return dm;
}
int64_t file_handle;
if (flags & MAP_ANONYMOUS) {
file_handle = kNtInvalidHandleValue;
} else {
file_handle = g_fds.p[fd].handle;
}
// mark map handle as inheritable if fork might need it
const struct NtSecurityAttributes *mapsec;
if ((flags & MAP_TYPE) == MAP_SHARED) {
mapsec = &kNtIsInheritable;
} else {
mapsec = 0;
}
// nt will whine under many circumstances if we change the execute bit
// later using mprotect(). the workaround is to always request execute
// and then virtualprotect() it away until we actually need it. please
// note that open-nt.c always requests an kNtGenericExecute accessmask
int iscow = 0;
int page_flags;
int file_flags;
if (file_handle != -1) {
if ((flags & MAP_TYPE) != MAP_SHARED) {
// windows has cow pages but they can't propagate across fork()
// that means we only get copy-on-write for the root process :(
page_flags = kNtPageExecuteWritecopy;
file_flags = kNtFileMapCopy | kNtFileMapExecute;
iscow = 1;
} else {
if ((g_fds.p[fd].flags & O_ACCMODE) == O_RDONLY) {
page_flags = kNtPageExecuteRead;
file_flags = kNtFileMapRead | kNtFileMapExecute;
} else {
page_flags = kNtPageExecuteReadwrite;
file_flags = kNtFileMapWrite | kNtFileMapExecute;
}
}
} else {
page_flags = kNtPageExecuteReadwrite;
file_flags = kNtFileMapWrite | kNtFileMapExecute;
}
int e = errno;
TryAgain:
if ((dm.hand = CreateFileMapping(file_handle, mapsec, page_flags,
(size + off) >> 32, (size + off), 0))) {
if ((dm.addr = MapViewOfFileEx(dm.hand, file_flags, off >> 32, off, size,
addr))) {
uint32_t oldprot;
if (VirtualProtect(dm.addr, size, __prot2nt(prot, iscow), &oldprot))
return dm;
UnmapViewOfFile(dm.addr);
}
CloseHandle(dm.hand);
} else if (!(prot & PROT_EXEC) && //
(file_flags & kNtFileMapExecute) && //
GetLastError() == kNtErrorAccessDenied) {
// your file needs to have been O_CREAT'd with exec `mode` bits in
// order to be mapped with executable permission. we always try to
// get execute permission if the kernel will give it to us because
// win32 would otherwise forbid mprotect() from elevating later on
file_flags &= ~kNtFileMapExecute;
switch (page_flags) {
case kNtPageExecuteWritecopy:
page_flags = kNtPageWritecopy;
break;
case kNtPageExecuteReadwrite:
page_flags = kNtPageReadwrite;
break;
case kNtPageExecuteRead:
page_flags = kNtPageReadonly;
break;
default:
__builtin_unreachable();
}
errno = e;
goto TryAgain;
}
dm.hand = kNtInvalidHandleValue;
dm.addr = (void *)(intptr_t)-1;
return dm;
}
static struct DirectMap sys_mmap(void *addr, size_t size, int prot, int flags,
int fd, int64_t off) {
struct DirectMap d;
if (IsXnuSilicon()) {
long p = _sysret(__syslib->__mmap(addr, size, prot, flags, fd, off));
d.hand = kNtInvalidHandleValue;
d.addr = (void *)p;
} else if (IsWindows()) {
d = sys_mmap_nt(addr, size, prot, flags, fd, off);
} else if (IsMetal()) {
d.addr = sys_mmap_metal(addr, size, prot, flags, fd, off);
d.hand = kNtInvalidHandleValue;
} else {
d.addr = __sys_mmap(addr, size, prot, flags, fd, off, off);
d.hand = kNtInvalidHandleValue;
}
return d;
}
struct Map *__maps_alloc(void) { struct Map *__maps_alloc(void) {
struct Map *map; struct Map *map;
uintptr_t tip = atomic_load_explicit(&__maps.freed, memory_order_relaxed); uintptr_t tip = atomic_load_explicit(&__maps.freed, memory_order_relaxed);
@ -467,7 +596,7 @@ struct Map *__maps_alloc(void) {
if (sys.addr == MAP_FAILED) if (sys.addr == MAP_FAILED)
return 0; return 0;
if (IsWindows()) if (IsWindows())
CloseHandle(sys.maphandle); CloseHandle(sys.hand);
struct MapSlab *slab = sys.addr; struct MapSlab *slab = sys.addr;
while (!atomic_compare_exchange_weak(&__maps.slabs, &slab->next, slab)) { while (!atomic_compare_exchange_weak(&__maps.slabs, &slab->next, slab)) {
} }
@ -717,7 +846,7 @@ static void *__mmap_impl(char *addr, size_t size, int prot, int flags, int fd,
map->off = off; map->off = off;
map->prot = prot; map->prot = prot;
map->flags = flags; map->flags = flags;
map->hand = res.maphandle; map->hand = res.hand;
if (IsWindows()) { if (IsWindows()) {
map->iscow = (flags & MAP_TYPE) != MAP_SHARED && fd != -1; map->iscow = (flags & MAP_TYPE) != MAP_SHARED && fd != -1;
map->readonlyfile = (flags & MAP_TYPE) == MAP_SHARED && fd != -1 && map->readonlyfile = (flags & MAP_TYPE) == MAP_SHARED && fd != -1 &&

View file

@ -41,8 +41,6 @@ int sys_munmap(void *p, size_t n) {
} else { } else {
rc = __sys_munmap(p, n); rc = __sys_munmap(p, n);
} }
if (!rc)
__virtualsize -= n;
KERNTRACE("sys_munmap(%p, %'zu) → %d", p, n, rc); KERNTRACE("sys_munmap(%p, %'zu) → %d", p, n, rc);
return rc; return rc;
} }

View file

@ -83,7 +83,7 @@ struct SignalFrame {
}; };
__msabi extern typeof(GetStdHandle) *const __imp_GetStdHandle; __msabi extern typeof(GetStdHandle) *const __imp_GetStdHandle;
__msabi extern typeof(VirtualProtect) *const __imp_VirtualProtect; __msabi extern typeof(VirtualProtectEx) *const __imp_VirtualProtectEx;
__msabi extern typeof(VirtualQuery) *const __imp_VirtualQuery; __msabi extern typeof(VirtualQuery) *const __imp_VirtualQuery;
__msabi extern typeof(WriteFile) *const __imp_WriteFile; __msabi extern typeof(WriteFile) *const __imp_WriteFile;
@ -566,7 +566,8 @@ textwindows wontreturn static void __sig_death(int sig, const char *thing) {
// //
forceinline void __sig_reguard(void *page) { forceinline void __sig_reguard(void *page) {
uint32_t old_protect; uint32_t old_protect;
__imp_VirtualProtect((void *)((uintptr_t)page & -__pagesize), __pagesize, __imp_VirtualProtectEx(GetCurrentProcess(),
(void *)((uintptr_t)page & -__pagesize), __pagesize,
kNtPageReadwrite | kNtPageGuard, &old_protect); kNtPageReadwrite | kNtPageGuard, &old_protect);
} }

View file

@ -1,7 +1,7 @@
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│ /*-*- 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 vi: set et ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi
Copyright 2021 Justine Alexandra Roberts Tunney Copyright 2024 Justine Alexandra Roberts Tunney
Permission to use, copy, modify, and/or distribute this software for Permission to use, copy, modify, and/or distribute this software for
any purpose with or without fee is hereby granted, provided that the any purpose with or without fee is hereby granted, provided that the
@ -16,22 +16,14 @@
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE. PERFORMANCE OF THIS SOFTWARE.
*/ */
#include "libc/calls/syscall-nt.internal.h" #include "libc/nt/memory.h"
#include "libc/nt/enum/status.h"
#include "libc/nt/nt/process.h"
#include "libc/nt/process.h"
#include "libc/nt/runtime.h" #include "libc/nt/runtime.h"
#include "libc/nt/struct/processbasicinformation.h"
textwindows int sys_getppid_nt(void) { /**
struct NtProcessBasicInformation ProcessInformation; * Allocates memory on The New Technology.
uint32_t gotsize = 0; */
if (!NtError( textwindows void *VirtualAlloc(void *lpAddress, uint64_t dwSize,
NtQueryInformationProcess(GetCurrentProcess(), 0, &ProcessInformation, uint32_t flAllocationType, uint32_t flProtect) {
sizeof(ProcessInformation), &gotsize)) && return VirtualAllocEx(GetCurrentProcess(), lpAddress, dwSize,
gotsize >= sizeof(ProcessInformation) && flAllocationType, flProtect);
ProcessInformation.InheritedFromUniqueProcessId) {
return ProcessInformation.InheritedFromUniqueProcessId;
}
return GetCurrentProcessId();
} }

View file

@ -19,32 +19,23 @@
#include "libc/calls/syscall_support-nt.internal.h" #include "libc/calls/syscall_support-nt.internal.h"
#include "libc/intrin/describeflags.h" #include "libc/intrin/describeflags.h"
#include "libc/intrin/strace.h" #include "libc/intrin/strace.h"
#include "libc/macros.h"
#include "libc/mem/alloca.h"
#include "libc/nt/enum/memflags.h"
#include "libc/nt/memory.h" #include "libc/nt/memory.h"
#include "libc/nt/thunk/msabi.h" #include "libc/nt/thunk/msabi.h"
__msabi extern typeof(VirtualAllocEx) *const __imp_VirtualAllocEx; __msabi extern typeof(VirtualAllocEx) *const __imp_VirtualAllocEx;
static const char *DescribeAllocationType(char buf[48], uint32_t x) { /**
const struct DescribeFlags kAllocationTypeFlags[] = { * Allocates memory on The New Technology.
{kNtMemCommit, "Commit"}, // */
{kNtMemReserve, "Reserve"}, // textwindows void *VirtualAllocEx(int64_t hProcess, void *lpAddress,
{kNtMemReset, "Reset"}, // uint64_t dwSize, uint32_t flAllocationType,
}; uint32_t flProtect) {
return _DescribeFlags(buf, 48, kAllocationTypeFlags,
ARRAYLEN(kAllocationTypeFlags), "kNtMem", x);
}
void *VirtualAllocEx(int64_t hProcess, void *lpAddress, uint64_t dwSize,
uint32_t flAllocationType, uint32_t flProtect) {
void *res = __imp_VirtualAllocEx(hProcess, lpAddress, dwSize, void *res = __imp_VirtualAllocEx(hProcess, lpAddress, dwSize,
flAllocationType, flProtect); flAllocationType, flProtect);
if (!res) if (!res)
__winerr(); __winerr();
NTTRACE("VirtualAllocEx(%ld, %p, %'lu, %s, %s) → %p% m", hProcess, lpAddress, NTTRACE("VirtualAllocEx(%ld, %p, %'lu, %s, %s) → %p% m", hProcess, lpAddress,
dwSize, DescribeAllocationType(alloca(48), flAllocationType), dwSize, DescribeNtAllocationType(flAllocationType),
DescribeNtPageFlags(flProtect), res); DescribeNtPageFlags(flProtect), res);
return res; return res;
} }

View file

@ -19,4 +19,3 @@
#include "libc/runtime/runtime.h" #include "libc/runtime/runtime.h"
size_t __virtualmax = -1; size_t __virtualmax = -1;
size_t __virtualsize = 0;

View file

@ -58,9 +58,8 @@ textstartup void *_AcpiOsMapUncachedMemory(uintptr_t phy, size_t n) {
} }
textstartup static void *_AcpiOsAllocatePages(size_t n) { textstartup static void *_AcpiOsAllocatePages(size_t n) {
struct DirectMap dm = sys_mmap_metal(NULL, n, PROT_READ | PROT_WRITE, void *addr = sys_mmap_metal(NULL, n, PROT_READ | PROT_WRITE,
MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
void *addr = dm.addr;
if (addr == (void *)-1) if (addr == (void *)-1)
addr = NULL; addr = NULL;
return addr; return addr;

View file

@ -1,18 +0,0 @@
#include "libc/nt/codegen.h"
.imp kernel32,__imp_VirtualAlloc,VirtualAlloc
.text.windows
.ftrace1
VirtualAlloc:
.ftrace2
#ifdef __x86_64__
push %rbp
mov %rsp,%rbp
mov __imp_VirtualAlloc(%rip),%rax
jmp __sysv2nt
#elif defined(__aarch64__)
mov x0,#0
ret
#endif
.endfn VirtualAlloc,globl
.previous

View file

@ -9,7 +9,6 @@
# KERNEL32.DLL # KERNEL32.DLL
# #
# Name Actual DLL Arity # Name Actual DLL Arity
imp '' CreateDirectoryW kernel32 2 imp '' CreateDirectoryW kernel32 2
imp '' CreateFileA kernel32 7 imp '' CreateFileA kernel32 7
imp '' CreateFileMappingNumaW kernel32 7 imp '' CreateFileMappingNumaW kernel32 7
@ -303,7 +302,6 @@ imp 'UnlockFile' UnlockFile kernel32 5
imp 'UnmapViewOfFile2' UnmapViewOfFile2 kernel32 2 imp 'UnmapViewOfFile2' UnmapViewOfFile2 kernel32 2
imp 'UnmapViewOfFileEx' UnmapViewOfFileEx kernel32 3 imp 'UnmapViewOfFileEx' UnmapViewOfFileEx kernel32 3
imp 'UpdateProcThreadAttribute' UpdateProcThreadAttribute kernel32 7 imp 'UpdateProcThreadAttribute' UpdateProcThreadAttribute kernel32 7
imp 'VirtualAlloc' VirtualAlloc kernel32 4
imp 'VirtualFree' VirtualFree kernel32 3 imp 'VirtualFree' VirtualFree kernel32 3
imp 'VirtualLock' VirtualLock kernel32 2 imp 'VirtualLock' VirtualLock kernel32 2
imp 'VirtualQuery' VirtualQuery kernel32 3 imp 'VirtualQuery' VirtualQuery kernel32 3

View file

@ -30,6 +30,7 @@ LIBC_PROC_A_DIRECTDEPS = \
LIBC_MEM \ LIBC_MEM \
LIBC_NEXGEN32E \ LIBC_NEXGEN32E \
LIBC_NT_KERNEL32 \ LIBC_NT_KERNEL32 \
LIBC_NT_NTDLL \
LIBC_NT_PSAPI \ LIBC_NT_PSAPI \
LIBC_RUNTIME \ LIBC_RUNTIME \
LIBC_STR \ LIBC_STR \

View file

@ -24,16 +24,23 @@
#include "libc/calls/syscall-nt.internal.h" #include "libc/calls/syscall-nt.internal.h"
#include "libc/errno.h" #include "libc/errno.h"
#include "libc/fmt/itoa.h" #include "libc/fmt/itoa.h"
#include "libc/intrin/dll.h"
#include "libc/intrin/fds.h" #include "libc/intrin/fds.h"
#include "libc/intrin/kprintf.h"
#include "libc/intrin/strace.h"
#include "libc/mem/mem.h" #include "libc/mem/mem.h"
#include "libc/nt/accounting.h"
#include "libc/nt/enum/processaccess.h" #include "libc/nt/enum/processaccess.h"
#include "libc/nt/enum/startf.h" #include "libc/nt/enum/startf.h"
#include "libc/nt/enum/status.h"
#include "libc/nt/errors.h" #include "libc/nt/errors.h"
#include "libc/nt/files.h" #include "libc/nt/files.h"
#include "libc/nt/process.h" #include "libc/nt/process.h"
#include "libc/nt/runtime.h" #include "libc/nt/runtime.h"
#include "libc/nt/struct/processinformation.h" #include "libc/nt/struct/processinformation.h"
#include "libc/nt/struct/startupinfo.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/nt/thunk/msabi.h"
#include "libc/proc/describefds.internal.h" #include "libc/proc/describefds.internal.h"
#include "libc/proc/ntspawn.h" #include "libc/proc/ntspawn.h"
@ -41,6 +48,7 @@
#include "libc/str/str.h" #include "libc/str/str.h"
#include "libc/sysv/consts/at.h" #include "libc/sysv/consts/at.h"
#include "libc/sysv/consts/o.h" #include "libc/sysv/consts/o.h"
#include "libc/sysv/consts/sig.h"
#include "libc/sysv/errfuns.h" #include "libc/sysv/errfuns.h"
#include "libc/thread/posixthread.internal.h" #include "libc/thread/posixthread.internal.h"
#include "libc/thread/thread.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 _pthread_lock(); // order matters
// new process should be a child of our parent // new process should be a child of our parent
int64_t hParentProcess; int64_t hParentProcess =
int ppid = sys_getppid_nt(); sys_getppid_nt_win32
if (!(hParentProcess = OpenProcess( ? OpenProcess(kNtProcessDupHandle | kNtProcessCreateProcess, false,
kNtProcessDupHandle | kNtProcessCreateProcess, false, ppid))) { sys_getppid_nt_win32)
sys_execve_nt_abort(sigmask); : 0;
return -1;
}
// inherit pid // inherit pid
char pidvar[11 + 21]; char pidvar[11 + 21];
@ -81,6 +87,16 @@ textwindows int sys_execve_nt(const char *program, char *const argv[],
char maskvar[6 + 21]; char maskvar[6 + 21];
FormatUint64(stpcpy(maskvar, "_MASK="), sigmask); 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 // define stdio handles for the spawned subprocess
struct NtStartupInfo si = { struct NtStartupInfo si = {
.cb = sizeof(struct NtStartupInfo), .cb = sizeof(struct NtStartupInfo),
@ -94,12 +110,21 @@ 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 // pass serialized file descriptor table in environment
char *fdspec; char *fdspec;
int64_t *lpExplicitHandles; int64_t *lpExplicitHandles;
uint32_t dwExplicitHandleCount; 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))) { &lpExplicitHandles, &dwExplicitHandleCount))) {
if (hParentProcess)
CloseHandle(hParentProcess); CloseHandle(hParentProcess);
sys_execve_nt_abort(sigmask); sys_execve_nt_abort(sigmask);
return -1; return -1;
@ -114,11 +139,13 @@ textwindows int sys_execve_nt(const char *program, char *const argv[],
// launch the process // launch the process
struct NtProcessInformation pi; struct NtProcessInformation pi;
int rc = ntspawn(&(struct NtSpawnArgs){ int rc = ntspawn(&(struct NtSpawnArgs){
AT_FDCWD, program, argv, envp, (char *[]){fdspec, maskvar, pidvar, 0}, 0, AT_FDCWD, program, argv, envp,
0, hParentProcess, lpExplicitHandles, dwExplicitHandleCount, &si, &pi}); (char *[]){fdspec, maskvar, pidvar, ppidvar, 0}, 0, 0, hCreatorProcess,
__undescribe_fds(hParentProcess, lpExplicitHandles, dwExplicitHandleCount); lpExplicitHandles, dwExplicitHandleCount, &si, &pi});
__undescribe_fds(hCreatorProcess, lpExplicitHandles, dwExplicitHandleCount);
if (rc == -1) { if (rc == -1) {
free(fdspec); free(fdspec);
if (hParentProcess)
CloseHandle(hParentProcess); CloseHandle(hParentProcess);
sys_execve_nt_abort(sigmask); sys_execve_nt_abort(sigmask);
if (GetLastError() == kNtErrorSharingViolation) { if (GetLastError() == kNtErrorSharingViolation) {
@ -128,10 +155,12 @@ textwindows int sys_execve_nt(const char *program, char *const argv[],
} }
} }
// check if parent spoofing worked
if (hParentProcess) {
// give child to libc/proc/proc.c worker thread in parent // give child to libc/proc/proc.c worker thread in parent
int64_t handle; int64_t handle;
if (DuplicateHandle(GetCurrentProcess(), pi.hProcess, hParentProcess, &handle, if (DuplicateHandle(GetCurrentProcess(), pi.hProcess, hParentProcess,
0, false, kNtDuplicateSameAccess)) { &handle, 0, false, kNtDuplicateSameAccess)) {
unassert(!(handle & 0xFFFFFFFFFF000000)); unassert(!(handle & 0xFFFFFFFFFF000000));
__imp_TerminateProcess(-1, 0x23000000u | handle); __imp_TerminateProcess(-1, 0x23000000u | handle);
} else { } else {
@ -142,4 +171,39 @@ textwindows int sys_execve_nt(const char *program, char *const argv[],
__builtin_unreachable(); __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);
}
}
}
}
#endif /* __x86_64__ */ #endif /* __x86_64__ */

View file

@ -57,11 +57,6 @@
* compiled by MSVC or Cygwin is launched instead, then only the stdio * compiled by MSVC or Cygwin is launched instead, then only the stdio
* file descriptors can be passed along. * file descriptors can be passed along.
* *
* On Windows, the parent process must be a cosmo program. If you're
* calling execve() from a program that wasn't launched by cosmopolitan
* bash, or some similar program, then ask yourself if what you really
* want is to either (a) call fork() first, or (b) use posix_spawn().
*
* On Windows, `argv` and `envp` can't contain binary strings. They need * On Windows, `argv` and `envp` can't contain binary strings. They need
* to be valid UTF-8 in order to round-trip the WIN32 API, without being * to be valid UTF-8 in order to round-trip the WIN32 API, without being
* corrupted. * corrupted.

View file

@ -46,6 +46,7 @@
#include "libc/nt/winsock.h" #include "libc/nt/winsock.h"
#include "libc/proc/proc.h" #include "libc/proc/proc.h"
#include "libc/runtime/internal.h" #include "libc/runtime/internal.h"
#include "libc/runtime/runtime.h"
#include "libc/runtime/symbols.internal.h" #include "libc/runtime/symbols.internal.h"
#include "libc/sysv/consts/map.h" #include "libc/sysv/consts/map.h"
#include "libc/sysv/consts/prot.h" #include "libc/sysv/consts/prot.h"
@ -211,8 +212,6 @@ textwindows static int sys_fork_nt_parent(uint32_t dwCreationFlags) {
// let's go // let's go
bool ok = true; bool ok = true;
uint32_t child_old_protect;
uint32_t parent_old_protect;
// copy memory manager maps // copy memory manager maps
for (struct MapSlab *slab = for (struct MapSlab *slab =
@ -225,11 +224,12 @@ textwindows static int sys_fork_nt_parent(uint32_t dwCreationFlags) {
} }
// copy private memory maps // copy private memory maps
int alloc_prot = -1;
for (struct Map *map = __maps_first(); map; map = __maps_next(map)) { for (struct Map *map = __maps_first(); map; map = __maps_next(map)) {
if ((map->flags & MAP_TYPE) == MAP_SHARED) if ((map->flags & MAP_TYPE) == MAP_SHARED)
continue; continue; // shared memory doesn't need to be copied to subprocess
if ((map->flags & MAP_NOFORK) && (map->flags & MAP_TYPE) != MAP_FILE) if ((map->flags & MAP_NOFORK) && (map->flags & MAP_TYPE) != MAP_FILE)
continue; continue; // ignore things like signal worker stack memory
if (__maps_isalloc(map)) { if (__maps_isalloc(map)) {
size_t allocsize = map->size; size_t allocsize = map->size;
for (struct Map *m2 = __maps_next(map); m2; m2 = __maps_next(m2)) { for (struct Map *m2 = __maps_next(map); m2; m2 = __maps_next(m2)) {
@ -240,22 +240,41 @@ textwindows static int sys_fork_nt_parent(uint32_t dwCreationFlags) {
} }
} }
if ((map->flags & MAP_NOFORK) && (map->flags & MAP_TYPE) == MAP_FILE) { if ((map->flags & MAP_NOFORK) && (map->flags & MAP_TYPE) == MAP_FILE) {
// portable executable segment
if (!(map->prot & PROT_WRITE)) {
uint32_t child_old_protect;
ok = ok && !!VirtualProtectEx(procinfo.hProcess, map->addr, allocsize, ok = ok && !!VirtualProtectEx(procinfo.hProcess, map->addr, allocsize,
kNtPageReadwrite, &child_old_protect); kNtPageReadwrite, &child_old_protect);
alloc_prot = PROT_READ | PROT_WRITE;
} else { } else {
alloc_prot = map->prot;
}
} else {
// private mapping
uint32_t page_flags;
if (!(alloc_prot & PROT_WRITE)) {
page_flags = kNtPageReadwrite;
alloc_prot = PROT_READ | PROT_WRITE;
} else {
page_flags = __prot2nt(alloc_prot, false);
}
ok = ok && !!VirtualAllocEx(procinfo.hProcess, map->addr, allocsize, ok = ok && !!VirtualAllocEx(procinfo.hProcess, map->addr, allocsize,
kNtMemReserve | kNtMemCommit, kNtMemReserve | kNtMemCommit, page_flags);
kNtPageExecuteReadwrite);
} }
} }
uint32_t parent_old_protect;
if (!(map->prot & PROT_READ)) if (!(map->prot & PROT_READ))
ok = ok && !!VirtualProtect(map->addr, map->size, kNtPageReadwrite, ok = ok && !!VirtualProtect(map->addr, map->size, kNtPageReadwrite,
&parent_old_protect); &parent_old_protect);
ok = ok && !!WriteProcessMemory(procinfo.hProcess, map->addr, map->addr, ok = ok &&
map->size, 0); !!WriteProcessMemory(procinfo.hProcess, map->addr, map->addr,
(map->size + __pagesize - 1) & -__pagesize, 0);
if (map->prot != alloc_prot) {
uint32_t child_old_protect;
ok = ok && ok = ok &&
!!VirtualProtectEx(procinfo.hProcess, map->addr, map->size, !!VirtualProtectEx(procinfo.hProcess, map->addr, map->size,
__prot2nt(map->prot, false), &child_old_protect); __prot2nt(map->prot, false), &child_old_protect);
}
if (!(map->prot & PROT_READ)) if (!(map->prot & PROT_READ))
ok = ok && !!VirtualProtect(map->addr, map->size, parent_old_protect, ok = ok && !!VirtualProtect(map->addr, map->size, parent_old_protect,
&parent_old_protect); &parent_old_protect);

View file

@ -20,6 +20,7 @@
#include "libc/calls/internal.h" #include "libc/calls/internal.h"
#include "libc/calls/sig.internal.h" #include "libc/calls/sig.internal.h"
#include "libc/calls/state.internal.h" #include "libc/calls/state.internal.h"
#include "libc/calls/struct/metasigaltstack.h"
#include "libc/calls/struct/sigset.internal.h" #include "libc/calls/struct/sigset.internal.h"
#include "libc/calls/struct/timespec.h" #include "libc/calls/struct/timespec.h"
#include "libc/calls/syscall-nt.internal.h" #include "libc/calls/syscall-nt.internal.h"
@ -43,6 +44,7 @@
#include "libc/runtime/syslib.internal.h" #include "libc/runtime/syslib.internal.h"
#include "libc/stdio/internal.h" #include "libc/stdio/internal.h"
#include "libc/str/str.h" #include "libc/str/str.h"
#include "libc/sysv/consts/ss.h"
#include "libc/thread/itimer.h" #include "libc/thread/itimer.h"
#include "libc/thread/posixthread.internal.h" #include "libc/thread/posixthread.internal.h"
#include "libc/thread/thread.h" #include "libc/thread/thread.h"
@ -120,8 +122,7 @@ static void fork_prepare(void) {
if (_weaken(__dlopen_lock)) if (_weaken(__dlopen_lock))
_weaken(__dlopen_lock)(); _weaken(__dlopen_lock)();
if (IsWindows()) if (IsWindows())
if (_weaken(__proc_lock)) __proc_lock();
_weaken(__proc_lock)();
if (_weaken(cosmo_stack_lock)) if (_weaken(cosmo_stack_lock))
_weaken(cosmo_stack_lock)(); _weaken(cosmo_stack_lock)();
__cxa_lock(); __cxa_lock();
@ -155,8 +156,7 @@ static void fork_parent(void) {
if (_weaken(cosmo_stack_unlock)) if (_weaken(cosmo_stack_unlock))
_weaken(cosmo_stack_unlock)(); _weaken(cosmo_stack_unlock)();
if (IsWindows()) if (IsWindows())
if (_weaken(__proc_unlock)) __proc_unlock();
_weaken(__proc_unlock)();
if (_weaken(__dlopen_unlock)) if (_weaken(__dlopen_unlock))
_weaken(__dlopen_unlock)(); _weaken(__dlopen_unlock)();
if (_weaken(__localtime_unlock)) if (_weaken(__localtime_unlock))
@ -167,7 +167,7 @@ static void fork_parent(void) {
_pthread_mutex_unlock(&supreme_lock); _pthread_mutex_unlock(&supreme_lock);
} }
static void fork_child(void) { static void fork_child(int ppid_win32, int ppid_cosmo) {
if (_weaken(__rand64_wipe)) if (_weaken(__rand64_wipe))
_weaken(__rand64_wipe)(); _weaken(__rand64_wipe)();
_pthread_mutex_wipe_np(&__fds_lock_obj); _pthread_mutex_wipe_np(&__fds_lock_obj);
@ -194,6 +194,8 @@ static void fork_child(void) {
_pthread_mutex_wipe_np(&__sig_worker_lock); _pthread_mutex_wipe_np(&__sig_worker_lock);
if (_weaken(__sig_init)) if (_weaken(__sig_init))
_weaken(__sig_init)(); _weaken(__sig_init)();
if (_weaken(sys_getppid_nt_wipe))
_weaken(sys_getppid_nt_wipe)(ppid_win32, ppid_cosmo);
} }
if (_weaken(_pthread_onfork_child)) if (_weaken(_pthread_onfork_child))
_weaken(_pthread_onfork_child)(); _weaken(_pthread_onfork_child)();
@ -202,8 +204,9 @@ static void fork_child(void) {
int _fork(uint32_t dwCreationFlags) { int _fork(uint32_t dwCreationFlags) {
struct Dll *e; struct Dll *e;
int ax, dx, tid, parent; int ax, dx, tid, ppid_win32, ppid_cosmo;
parent = __pid; ppid_win32 = IsWindows() ? GetCurrentProcessId() : 0;
ppid_cosmo = __pid;
BLOCK_SIGNALS; BLOCK_SIGNALS;
fork_prepare(); fork_prepare();
if (!IsWindows()) { if (!IsWindows()) {
@ -223,7 +226,7 @@ int _fork(uint32_t dwCreationFlags) {
// get new thread id // get new thread id
struct CosmoTib *tib = __get_tls(); struct CosmoTib *tib = __get_tls();
struct PosixThread *pt = (struct PosixThread *)tib->tib_pthread; struct PosixThread *me = (struct PosixThread *)tib->tib_pthread;
tid = IsLinux() || IsXnuSilicon() ? dx : sys_gettid(); tid = IsLinux() || IsXnuSilicon() ? dx : sys_gettid();
atomic_init(&tib->tib_ctid, tid); atomic_init(&tib->tib_ctid, tid);
atomic_init(&tib->tib_ptid, tid); atomic_init(&tib->tib_ptid, tid);
@ -243,10 +246,10 @@ int _fork(uint32_t dwCreationFlags) {
// turn other threads into zombies // turn other threads into zombies
// we can't free() them since we're monopolizing all locks // we can't free() them since we're monopolizing all locks
// we assume the operating system already reclaimed system handles // we assume the operating system already reclaimed system handles
dll_remove(&_pthread_list, &pt->list); dll_remove(&_pthread_list, &me->list);
struct Dll *old_threads = _pthread_list; struct Dll *old_threads = _pthread_list;
_pthread_list = 0; _pthread_list = 0;
dll_make_first(&_pthread_list, &pt->list); dll_make_first(&_pthread_list, &me->list);
atomic_init(&_pthread_count, 1); atomic_init(&_pthread_count, 1);
// get new system thread handle // get new system thread handle
@ -264,25 +267,38 @@ int _fork(uint32_t dwCreationFlags) {
atomic_init(&tib->tib_sigpending, 0); atomic_init(&tib->tib_sigpending, 0);
// we can't be canceled if the canceler no longer exists // we can't be canceled if the canceler no longer exists
atomic_init(&pt->pt_canceled, false); atomic_init(&me->pt_canceled, false);
// forget locks // forget locks
bzero(tib->tib_locks, sizeof(tib->tib_locks)); bzero(tib->tib_locks, sizeof(tib->tib_locks));
// xnu fork() doesn't preserve sigaltstack()
if (IsXnu() && me->tib->tib_sigstack_addr) {
struct sigaltstack_bsd ss;
ss.ss_sp = me->tib->tib_sigstack_addr;
ss.ss_size = me->tib->tib_sigstack_size;
ss.ss_flags = me->tib->tib_sigstack_flags;
if (IsXnuSilicon()) {
__syslib->__sigaltstack(&ss, 0);
} else {
sys_sigaltstack(&ss, 0);
}
}
// run user fork callbacks // run user fork callbacks
fork_child(); fork_child(ppid_win32, ppid_cosmo);
// free threads // free threads
if (_weaken(_pthread_free)) { if (_weaken(_pthread_free)) {
while ((e = dll_first(old_threads))) { while ((e = dll_first(old_threads))) {
pt = POSIXTHREAD_CONTAINER(e); struct PosixThread *pt = POSIXTHREAD_CONTAINER(e);
atomic_init(&pt->tib->tib_syshand, 0); atomic_init(&pt->tib->tib_syshand, 0);
dll_remove(&old_threads, e); dll_remove(&old_threads, e);
_weaken(_pthread_free)(pt); _weaken(_pthread_free)(pt);
} }
} }
STRACE("fork() → 0 (child of %d)", parent); STRACE("fork() → 0 (child of %d)", ppid_cosmo);
} else { } else {
// this is the parent process // this is the parent process
fork_parent(); fork_parent();

93
libc/proc/getppid-nt.c Normal file
View file

@ -0,0 +1,93 @@
/*-*- 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 2021 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/syscall-nt.internal.h"
#include "libc/cosmo.h"
#include "libc/dce.h"
#include "libc/fmt/itoa.h"
#include "libc/nt/enum/status.h"
#include "libc/nt/nt/process.h"
#include "libc/nt/process.h"
#include "libc/nt/runtime.h"
#include "libc/nt/struct/processbasicinformation.h"
#include "libc/runtime/internal.h"
#include "libc/runtime/runtime.h"
int sys_getppid_nt_win32;
int sys_getppid_nt_cosmo;
textwindows static int sys_getppid_nt_ntdll(void) {
struct NtProcessBasicInformation ProcessInformation;
uint32_t gotsize = 0;
if (!NtError(
NtQueryInformationProcess(GetCurrentProcess(), 0, &ProcessInformation,
sizeof(ProcessInformation), &gotsize)) &&
gotsize >= sizeof(ProcessInformation) &&
ProcessInformation.InheritedFromUniqueProcessId) {
return ProcessInformation.InheritedFromUniqueProcessId;
}
return 0;
}
static void sys_getppid_nt_extract(const char *str) {
int c;
int win32 = 0;
int cosmo = 0;
if (str) {
for (;;) {
c = *str;
if (!('0' <= c && c <= '9'))
break;
win32 *= 10;
win32 += c - '0';
++str;
}
if (win32 && *str++ == ':') {
for (;;) {
c = *str;
if (!('0' <= c && c <= '9'))
break;
cosmo *= 10;
cosmo += c - '0';
++str;
}
if (win32 == sys_getppid_nt_ntdll()) {
sys_getppid_nt_win32 = win32;
sys_getppid_nt_cosmo = cosmo;
}
}
}
}
__attribute__((__constructor__(90))) static void init(void) {
if (!IsWindows())
return;
sys_getppid_nt_extract(getenv("_COSMO_PPID"));
}
textwindows int sys_getppid_nt(void) {
if (sys_getppid_nt_cosmo)
return sys_getppid_nt_cosmo;
return sys_getppid_nt_ntdll();
}
textwindows void sys_getppid_nt_wipe(int win32, int cosmo) {
sys_getppid_nt_win32 = win32;
sys_getppid_nt_cosmo = cosmo;
}

View file

@ -51,6 +51,7 @@
#include "libc/nt/enum/processcreationflags.h" #include "libc/nt/enum/processcreationflags.h"
#include "libc/nt/enum/startf.h" #include "libc/nt/enum/startf.h"
#include "libc/nt/files.h" #include "libc/nt/files.h"
#include "libc/nt/process.h"
#include "libc/nt/runtime.h" #include "libc/nt/runtime.h"
#include "libc/nt/struct/processinformation.h" #include "libc/nt/struct/processinformation.h"
#include "libc/nt/struct/startupinfo.h" #include "libc/nt/struct/startupinfo.h"
@ -59,6 +60,7 @@
#include "libc/proc/posix_spawn.h" #include "libc/proc/posix_spawn.h"
#include "libc/proc/posix_spawn.internal.h" #include "libc/proc/posix_spawn.internal.h"
#include "libc/proc/proc.h" #include "libc/proc/proc.h"
#include "libc/runtime/internal.h"
#include "libc/runtime/runtime.h" #include "libc/runtime/runtime.h"
#include "libc/sock/sock.h" #include "libc/sock/sock.h"
#include "libc/stdio/stdio.h" #include "libc/stdio/stdio.h"
@ -396,6 +398,14 @@ static textwindows errno_t posix_spawn_nt_impl(
} }
FormatUint64(stpcpy(maskvar, "_MASK="), childmask); FormatUint64(stpcpy(maskvar, "_MASK="), childmask);
// inherit parent process id
char ppidvar[12 + 21 + 1 + 21 + 1], *p = ppidvar;
p = stpcpy(p, "_COSMO_PPID=");
p = FormatUint64(p, GetCurrentProcessId());
*p++ = ':';
p = FormatUint64(p, __pid);
setenv("_COSMO_PPID", ppidvar, true);
// launch process // launch process
int rc = -1; int rc = -1;
struct NtProcessInformation procinfo; struct NtProcessInformation procinfo;

View file

@ -24,12 +24,13 @@
#include "libc/intrin/kprintf.h" #include "libc/intrin/kprintf.h"
#include "libc/nt/enum/pageflags.h" #include "libc/nt/enum/pageflags.h"
#include "libc/nt/memory.h" #include "libc/nt/memory.h"
#include "libc/nt/runtime.h"
#include "libc/nt/thunk/msabi.h" #include "libc/nt/thunk/msabi.h"
#include "libc/runtime/runtime.h" #include "libc/runtime/runtime.h"
#include "libc/sysv/consts/nr.h" #include "libc/sysv/consts/nr.h"
#include "libc/sysv/consts/prot.h" #include "libc/sysv/consts/prot.h"
__msabi extern typeof(VirtualProtect) *const __imp_VirtualProtect; __msabi extern typeof(VirtualProtectEx) *const __imp_VirtualProtectEx;
__funline void __morph_mprotect(void *addr, size_t size, int prot, int ntprot) { __funline void __morph_mprotect(void *addr, size_t size, int prot, int ntprot) {
#ifdef __x86_64__ #ifdef __x86_64__
@ -54,7 +55,7 @@ __funline void __morph_mprotect(void *addr, size_t size, int prot, int ntprot) {
} }
#endif #endif
} else { } else {
__imp_VirtualProtect(addr, size, ntprot, &op); __imp_VirtualProtectEx(GetCurrentProcess(), addr, size, ntprot, &op);
} }
#elif defined(__aarch64__) #elif defined(__aarch64__)
register long r0 asm("x0") = (long)addr; register long r0 asm("x0") = (long)addr;

View file

@ -83,7 +83,6 @@ extern uint64_t kStartTsc;
extern const char kNtSystemDirectory[]; extern const char kNtSystemDirectory[];
extern const char kNtWindowsDirectory[]; extern const char kNtWindowsDirectory[];
extern size_t __virtualmax; extern size_t __virtualmax;
extern size_t __virtualsize;
extern size_t __stackmax; extern size_t __stackmax;
extern bool32 __isworker; extern bool32 __isworker;
/* utilities */ /* utilities */

View file

@ -79,7 +79,7 @@ __msabi extern typeof(SetConsoleMode) *const __imp_SetConsoleMode;
__msabi extern typeof(SetConsoleOutputCP) *const __imp_SetConsoleOutputCP; __msabi extern typeof(SetConsoleOutputCP) *const __imp_SetConsoleOutputCP;
__msabi extern typeof(SetEnvironmentVariable) *const __imp_SetEnvironmentVariableW; __msabi extern typeof(SetEnvironmentVariable) *const __imp_SetEnvironmentVariableW;
__msabi extern typeof(SetStdHandle) *const __imp_SetStdHandle; __msabi extern typeof(SetStdHandle) *const __imp_SetStdHandle;
__msabi extern typeof(VirtualProtect) *const __imp_VirtualProtect; __msabi extern typeof(VirtualProtectEx) *const __imp_VirtualProtectEx;
__msabi extern typeof(WriteFile) *const __imp_WriteFile; __msabi extern typeof(WriteFile) *const __imp_WriteFile;
// clang-format on // clang-format on
@ -206,10 +206,11 @@ abi wontreturn static void WinInit(const char16_t *cmdline) {
int stackprot = (intptr_t)ape_stack_prot; int stackprot = (intptr_t)ape_stack_prot;
if (~stackprot & PROT_EXEC) { if (~stackprot & PROT_EXEC) {
uint32_t old; uint32_t old;
__imp_VirtualProtect(stackaddr, stacksize, kNtPageReadwrite, &old); __imp_VirtualProtectEx(GetCurrentProcess(), stackaddr, stacksize,
kNtPageReadwrite, &old);
} }
uint32_t oldattr; uint32_t oldattr;
__imp_VirtualProtect(stackaddr, GetGuardSize(), __imp_VirtualProtectEx(GetCurrentProcess(), stackaddr, GetGuardSize(),
kNtPageReadwrite | kNtPageGuard, &oldattr); kNtPageReadwrite | kNtPageGuard, &oldattr);
if (_weaken(__maps_stack)) { if (_weaken(__maps_stack)) {
struct NtSystemInfo si; struct NtSystemInfo si;

View file

@ -23,17 +23,36 @@
#include "libc/runtime/runtime.h" #include "libc/runtime/runtime.h"
#include "libc/sysv/consts/auxv.h" #include "libc/sysv/consts/auxv.h"
#include "libc/sysv/consts/sig.h" #include "libc/sysv/consts/sig.h"
#include "libc/thread/thread.h"
/** /**
* Returns true if signal is most likely a stack overflow. * Returns true if signal is caused by stack overflow.
*/ */
char __is_stack_overflow(siginfo_t *si, void *arg) { char __is_stack_overflow(siginfo_t *si, void *arg) {
// sanity check
ucontext_t *uc = arg; ucontext_t *uc = arg;
if (!si || !uc) if (!si || !uc)
return false; return false;
if (si->si_signo != SIGSEGV && si->si_signo != SIGBUS) if (si->si_signo != SIGSEGV && //
si->si_signo != SIGBUS)
return false; return false;
intptr_t sp = uc->uc_mcontext.SP;
intptr_t fp = (intptr_t)si->si_addr; // get stack information
return ABS(fp - sp) < __pagesize; pthread_attr_t attr;
if (pthread_getattr_np(pthread_self(), &attr))
return false;
size_t guardsize;
if (pthread_attr_getguardsize(&attr, &guardsize))
return false;
void *stackaddr;
size_t stacksize;
if (pthread_attr_getstack(&attr, &stackaddr, &stacksize))
return false;
// determine if faulting address is inside guard region
char *x = (char *)si->si_addr;
char *lo = (char *)stackaddr - guardsize;
char *hi = (char *)stackaddr;
return lo <= x && x < hi;
} }

View file

@ -167,7 +167,6 @@ void _StartTty(struct Tty *tty, unsigned char type, unsigned short yp,
unsigned short startx, unsigned char yc, unsigned char xc, unsigned short startx, unsigned char yc, unsigned char xc,
void *fb, unsigned init_flags) { void *fb, unsigned init_flags) {
unsigned short yn, xn, xs = xp * sizeof(TtyCanvasColor); unsigned short yn, xn, xs = xp * sizeof(TtyCanvasColor);
struct DirectMap dm;
bzero(tty, sizeof(struct Tty)); bzero(tty, sizeof(struct Tty));
SetYp(tty, yp); SetYp(tty, yp);
SetXp(tty, xp); SetXp(tty, xp);
@ -183,9 +182,9 @@ void _StartTty(struct Tty *tty, unsigned char type, unsigned short yp,
tty->canvas = fb; tty->canvas = fb;
xs = xsfb; xs = xsfb;
} else { } else {
dm = sys_mmap_metal(NULL, (size_t)yp * xs, PROT_READ | PROT_WRITE, void *addr = sys_mmap_metal(NULL, (size_t)yp * xs, PROT_READ | PROT_WRITE,
MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
if (dm.addr == (void *)-1) { if (addr == (void *)-1) {
/* /*
* We are a bit low on memory. Try to go on anyway, & initialize * We are a bit low on memory. Try to go on anyway, & initialize
* our tty as an emergency console. * our tty as an emergency console.
@ -194,7 +193,7 @@ void _StartTty(struct Tty *tty, unsigned char type, unsigned short yp,
tty->canvas = fb; tty->canvas = fb;
xs = xsfb; xs = xsfb;
} else } else
tty->canvas = dm.addr; tty->canvas = addr;
} }
} }
SetYn(tty, yn); SetYn(tty, yn);

View file

@ -1,242 +0,0 @@
/*-*- 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 2021 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 "dsp/core/core.h"
#include "libc/calls/calls.h"
#include "libc/calls/struct/rlimit.h"
#include "libc/calls/struct/timespec.h"
#include "libc/dce.h"
#include "libc/errno.h"
#include "libc/intrin/directmap.h"
#include "libc/intrin/safemacros.h"
#include "libc/limits.h"
#include "libc/runtime/runtime.h"
#include "libc/stdio/rand.h"
#include "libc/stdio/stdio.h"
#include "libc/sysv/consts/auxv.h"
#include "libc/sysv/consts/map.h"
#include "libc/sysv/consts/o.h"
#include "libc/sysv/consts/prot.h"
#include "libc/sysv/consts/rlimit.h"
#include "libc/sysv/consts/sig.h"
#include "libc/testlib/testlib.h"
#include "libc/time.h"
#include "libc/x/xsigaction.h"
#include "libc/x/xspawn.h"
#define MEM (64 * 1024 * 1024)
static char tmpname[PATH_MAX];
void OnSigxcpu(int sig) {
ASSERT_EQ(SIGXCPU, sig);
_Exit(0);
}
void OnSigxfsz(int sig) {
unlink(tmpname);
ASSERT_EQ(SIGXFSZ, sig);
_Exit(0);
}
TEST(setrlimit, testCpuLimit) {
int wstatus;
struct rlimit rlim;
struct timespec start;
double matrices[3][3][3];
if (IsWindows())
return; // of course it doesn't work on windows
if (IsXnu())
return; // TODO(jart): it worked before
if (IsOpenbsd())
return; // TODO(jart): fix flake
ASSERT_NE(-1, (wstatus = xspawn(0)));
if (wstatus == -2) {
ASSERT_EQ(0, xsigaction(SIGXCPU, OnSigxcpu, 0, 0, 0));
ASSERT_EQ(0, getrlimit(RLIMIT_CPU, &rlim));
rlim.rlim_cur = 1; // set soft limit to one second
ASSERT_EQ(0, setrlimit(RLIMIT_CPU, &rlim));
start = timespec_real();
do {
matmul3(matrices[0], matrices[1], matrices[2]);
matmul3(matrices[0], matrices[1], matrices[2]);
matmul3(matrices[0], matrices[1], matrices[2]);
matmul3(matrices[0], matrices[1], matrices[2]);
} while (timespec_sub(timespec_real(), start).tv_sec < 5);
_Exit(1);
}
EXPECT_TRUE(WIFEXITED(wstatus));
EXPECT_FALSE(WIFSIGNALED(wstatus));
EXPECT_EQ(0, WEXITSTATUS(wstatus));
EXPECT_EQ(0, WTERMSIG(wstatus));
}
TEST(setrlimit, testFileSizeLimit) {
char junkdata[512];
int i, fd, wstatus;
struct rlimit rlim;
if (IsWindows())
return; /* of course it doesn't work on windows */
ASSERT_NE(-1, (wstatus = xspawn(0)));
if (wstatus == -2) {
ASSERT_EQ(0, xsigaction(SIGXFSZ, OnSigxfsz, 0, 0, 0));
ASSERT_EQ(0, getrlimit(RLIMIT_FSIZE, &rlim));
rlim.rlim_cur = 1024 * 1024; /* set soft limit to one megabyte */
ASSERT_EQ(0, setrlimit(RLIMIT_FSIZE, &rlim));
snprintf(tmpname, sizeof(tmpname), "%s/%s.%d",
firstnonnull(getenv("TMPDIR"), "/tmp"),
firstnonnull(program_invocation_short_name, "unknown"), getpid());
ASSERT_NE(-1, (fd = open(tmpname, O_RDWR | O_CREAT | O_TRUNC, 0644)));
rngset(junkdata, 512, lemur64, -1);
for (i = 0; i < 5 * 1024 * 1024 / 512; ++i) {
ASSERT_EQ(512, write(fd, junkdata, 512));
}
close(fd);
unlink(tmpname);
_Exit(1);
}
EXPECT_TRUE(WIFEXITED(wstatus));
EXPECT_FALSE(WIFSIGNALED(wstatus));
EXPECT_EQ(0, WEXITSTATUS(wstatus));
EXPECT_EQ(0, WTERMSIG(wstatus));
}
int SetMemoryLimit(size_t n) {
struct rlimit rlim = {0};
getrlimit(RLIMIT_AS, &rlim);
rlim.rlim_cur = n;
rlim.rlim_max = n;
return setrlimit(RLIMIT_AS, &rlim);
}
TEST(setrlimit, testMemoryLimit) {
char *p;
bool gotsome;
int i, wstatus;
ASSERT_NE(-1, (wstatus = xspawn(0)));
if (wstatus == -2) {
ASSERT_EQ(0, SetMemoryLimit(MEM));
for (gotsome = false, i = 0; i < (MEM * 2) / getpagesize(); ++i) {
p = mmap(0, getpagesize(), PROT_READ | PROT_WRITE,
MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
if (p != MAP_FAILED) {
gotsome = true;
} else {
ASSERT_TRUE(gotsome);
ASSERT_EQ(ENOMEM, errno);
_Exit(0);
}
rngset(p, getpagesize(), lemur64, -1);
}
_Exit(1);
}
EXPECT_TRUE(WIFEXITED(wstatus));
EXPECT_FALSE(WIFSIGNALED(wstatus));
EXPECT_EQ(0, WEXITSTATUS(wstatus));
EXPECT_EQ(0, WTERMSIG(wstatus));
}
TEST(setrlimit, testVirtualMemoryLimit) {
char *p;
int i, wstatus;
ASSERT_NE(-1, (wstatus = xspawn(0)));
if (wstatus == -2) {
ASSERT_EQ(0, setrlimit(RLIMIT_AS, &(struct rlimit){MEM, MEM}));
for (i = 0; i < (MEM * 2) / getpagesize(); ++i) {
if ((p = mmap(0, getpagesize(), PROT_READ | PROT_WRITE,
MAP_ANONYMOUS | MAP_PRIVATE | MAP_POPULATE, -1, 0)) ==
MAP_FAILED) {
ASSERT_EQ(ENOMEM, errno);
_Exit(0);
}
rngset(p, getpagesize(), lemur64, -1);
}
_Exit(1);
}
EXPECT_TRUE(WIFEXITED(wstatus));
EXPECT_FALSE(WIFSIGNALED(wstatus));
EXPECT_EQ(0, WEXITSTATUS(wstatus));
EXPECT_EQ(0, WTERMSIG(wstatus));
}
TEST(setrlimit, testDataMemoryLimit) {
char *p;
int i, wstatus;
if (IsXnu())
return; /* doesn't work on darwin */
if (IsNetbsd())
return; /* doesn't work on netbsd */
if (IsFreebsd())
return; /* doesn't work on freebsd */
if (IsLinux())
return; /* doesn't work on gnu/systemd */
if (IsWindows())
return; /* of course it doesn't work on windows */
ASSERT_NE(-1, (wstatus = xspawn(0)));
if (wstatus == -2) {
ASSERT_EQ(0, setrlimit(RLIMIT_DATA, &(struct rlimit){MEM, MEM}));
for (i = 0; i < (MEM * 2) / getpagesize(); ++i) {
p = sys_mmap(0, getpagesize(), PROT_READ | PROT_WRITE,
MAP_ANONYMOUS | MAP_PRIVATE | MAP_POPULATE, -1, 0)
.addr;
if (p == MAP_FAILED) {
ASSERT_EQ(ENOMEM, errno);
_Exit(0);
}
rngset(p, getpagesize(), lemur64, -1);
}
_Exit(1);
}
EXPECT_TRUE(WIFEXITED(wstatus));
EXPECT_FALSE(WIFSIGNALED(wstatus));
EXPECT_EQ(0, WEXITSTATUS(wstatus));
EXPECT_EQ(0, WTERMSIG(wstatus));
}
TEST(setrlimit, testPhysicalMemoryLimit) {
/* RLIMIT_RSS doesn't work on gnu/systemd */
/* RLIMIT_RSS doesn't work on darwin */
/* RLIMIT_RSS doesn't work on freebsd */
/* RLIMIT_RSS doesn't work on netbsd */
/* RLIMIT_RSS doesn't work on openbsd */
/* of course it doesn't work on windows */
}
wontreturn void OnVfork(void *ctx) {
struct rlimit *rlim;
rlim = ctx;
rlim->rlim_cur -= 1;
ASSERT_EQ(0, getrlimit(RLIMIT_CPU, rlim));
_Exit(0);
}
TEST(setrlimit, isVforkSafe) {
int ws;
struct rlimit rlim[2];
if (IsWindows())
return; /* of course it doesn't work on windows */
ASSERT_EQ(0, getrlimit(RLIMIT_CPU, rlim));
ASSERT_NE(-1, (ws = xvspawn(OnVfork, rlim, 0)));
EXPECT_TRUE(WIFEXITED(ws));
EXPECT_FALSE(WIFSIGNALED(ws));
EXPECT_EQ(0, WEXITSTATUS(ws));
EXPECT_EQ(0, WTERMSIG(ws));
ASSERT_EQ(0, getrlimit(RLIMIT_CPU, rlim + 1));
EXPECT_EQ(rlim[0].rlim_cur, rlim[1].rlim_cur);
EXPECT_EQ(rlim[0].rlim_max, rlim[1].rlim_max);
}

View file

@ -59,7 +59,7 @@ void CrashHandler(int sig, siginfo_t *si, void *ctx) {
kprintf("kprintf avoids overflowing %G si_addr=%lx sp=%lx\n", si->si_signo, kprintf("kprintf avoids overflowing %G si_addr=%lx sp=%lx\n", si->si_signo,
si->si_addr, ((ucontext_t *)ctx)->uc_mcontext.SP); si->si_addr, ((ucontext_t *)ctx)->uc_mcontext.SP);
smashed_stack = true; smashed_stack = true;
unassert(__is_stack_overflow(si, ctx)); // unassert(__is_stack_overflow(si, ctx)); // fuzzy with main thread
longjmp(recover, 123); longjmp(recover, 123);
} }

View file

@ -16,6 +16,7 @@
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE. PERFORMANCE OF THIS SOFTWARE.
*/ */
#include "libc/assert.h"
#include "libc/calls/struct/sigaction.h" #include "libc/calls/struct/sigaction.h"
#include "libc/calls/struct/sigaltstack.h" #include "libc/calls/struct/sigaltstack.h"
#include "libc/calls/struct/siginfo.h" #include "libc/calls/struct/siginfo.h"
@ -40,8 +41,9 @@
volatile bool smashed_stack; volatile bool smashed_stack;
void CrashHandler(int sig) { void CrashHandler(int sig, siginfo_t *si, void *ctx) {
smashed_stack = true; smashed_stack = true;
unassert(__is_stack_overflow(si, ctx));
pthread_exit((void *)123L); pthread_exit((void *)123L);
} }
@ -63,7 +65,7 @@ void *MyPosixThread(void *arg) {
ASSERT_SYS(0, 0, sigaltstack(&ss, 0)); ASSERT_SYS(0, 0, sigaltstack(&ss, 0));
sa.sa_flags = SA_SIGINFO | SA_ONSTACK; // <-- important sa.sa_flags = SA_SIGINFO | SA_ONSTACK; // <-- important
sigemptyset(&sa.sa_mask); sigemptyset(&sa.sa_mask);
sa.sa_handler = CrashHandler; sa.sa_sigaction = CrashHandler;
sigaction(SIGBUS, &sa, 0); sigaction(SIGBUS, &sa, 0);
sigaction(SIGSEGV, &sa, 0); sigaction(SIGSEGV, &sa, 0);
exit(StackOverflow(1)); exit(StackOverflow(1));

View file

@ -16,22 +16,28 @@
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE. PERFORMANCE OF THIS SOFTWARE.
*/ */
#include <cosmo.h> #include "libc/assert.h"
#include <limits.h> #include "libc/calls/calls.h"
#include <pthread.h> #include "libc/calls/struct/sigaction.h"
#include <signal.h> #include "libc/calls/struct/siginfo.h"
#include <unistd.h> #include "libc/runtime/runtime.h"
#include "libc/sysv/consts/sa.h"
#include "libc/sysv/consts/sig.h"
#include "libc/sysv/consts/ss.h"
#include "libc/thread/thread.h"
#include "libc/thread/tls.h"
/** /**
* stack overflow recovery technique #5 * stack overflow test #5
* use the cosmo posix threads extensions * - make sure fork() preserves sigaltstack()
* - make sure fork() preserves guard page status
*/ */
sig_atomic_t smashed_stack; jmp_buf recover;
void CrashHandler(int sig) { void CrashHandler(int sig, siginfo_t *si, void *ctx) {
smashed_stack = true; unassert(__is_stack_overflow(si, ctx));
pthread_exit(0); longjmp(recover, 123);
} }
int StackOverflow(int d) { int StackOverflow(int d) {
@ -44,42 +50,40 @@ int StackOverflow(int d) {
} }
void *MyPosixThread(void *arg) { void *MyPosixThread(void *arg) {
exit(StackOverflow(1)); int pid;
unassert(__get_tls()->tib_sigstack_addr);
unassert((pid = fork()) != -1);
if (!pid) {
int jumpcode;
if (!(jumpcode = setjmp(recover))) {
StackOverflow(1);
_Exit(1);
}
unassert(123 == jumpcode);
} else {
int ws;
unassert(wait(&ws) != -1);
unassert(!ws);
pthread_exit(0);
}
return 0; return 0;
} }
int main() { int main() {
// choose the most dangerously small size possible
size_t sigstacksize = sysconf(_SC_MINSIGSTKSZ) + 2048;
// setup signal handler
struct sigaction sa; struct sigaction sa;
sa.sa_flags = SA_SIGINFO | SA_ONSTACK;
sigemptyset(&sa.sa_mask); sigemptyset(&sa.sa_mask);
sa.sa_flags = SA_ONSTACK; sa.sa_sigaction = CrashHandler;
sa.sa_handler = CrashHandler; unassert(!sigaction(SIGBUS, &sa, 0));
if (sigaction(SIGBUS, &sa, 0)) unassert(!sigaction(SIGSEGV, &sa, 0));
return 1;
if (sigaction(SIGSEGV, &sa, 0))
return 2;
// create thread with signal stack pthread_t th;
pthread_t id;
pthread_attr_t attr; pthread_attr_t attr;
if (pthread_attr_init(&attr)) unassert(!pthread_attr_init(&attr));
return 3; unassert(!pthread_attr_setguardsize(&attr, getpagesize()));
if (pthread_attr_setguardsize(&attr, getpagesize())) unassert(!pthread_attr_setsigaltstacksize_np(&attr, SIGSTKSZ));
return 4; unassert(!pthread_create(&th, &attr, MyPosixThread, 0));
if (pthread_attr_setsigaltstacksize_np(&attr, sigstacksize)) unassert(!pthread_attr_destroy(&attr));
return 5; unassert(!pthread_join(th, 0));
if (pthread_create(&id, &attr, MyPosixThread, 0))
return 6;
if (pthread_attr_destroy(&attr))
return 7;
if (pthread_join(id, 0))
return 8;
if (!smashed_stack)
return 9;
CheckForMemoryLeaks();
} }

View file

@ -116,6 +116,42 @@ TEST(mmap, fixedTaken) {
EXPECT_SYS(0, 0, munmap(p, 1)); EXPECT_SYS(0, 0, munmap(p, 1));
} }
TEST(mmap, anon_rw_to_rx) {
char *p;
ASSERT_NE(MAP_FAILED, (p = mmap(0, 1, PROT_READ | PROT_WRITE,
MAP_ANONYMOUS | MAP_PRIVATE, -1, 0)));
ASSERT_SYS(0, 0, mprotect(p, 1, PROT_READ | PROT_EXEC));
ASSERT_SYS(0, 0, munmap(p, 1));
}
TEST(mmap, anon_rw_fork_to_rx) {
char *p;
ASSERT_NE(MAP_FAILED, (p = mmap(0, 1, PROT_READ | PROT_WRITE,
MAP_ANONYMOUS | MAP_PRIVATE, -1, 0)));
SPAWN(fork);
ASSERT_SYS(0, 0, mprotect(p, 1, PROT_READ | PROT_EXEC));
EXITS(0);
ASSERT_SYS(0, 0, munmap(p, 1));
}
TEST(mmap, anon_r_to_rw) {
char *p;
ASSERT_NE(MAP_FAILED,
(p = mmap(0, 1, PROT_READ, MAP_ANONYMOUS | MAP_PRIVATE, -1, 0)));
ASSERT_SYS(0, 0, mprotect(p, 1, PROT_READ | PROT_WRITE));
ASSERT_SYS(0, 0, munmap(p, 1));
}
TEST(mmap, anon_r_fork_to_rw) {
char *p;
ASSERT_NE(MAP_FAILED,
(p = mmap(0, 1, PROT_READ, MAP_ANONYMOUS | MAP_PRIVATE, -1, 0)));
SPAWN(fork);
ASSERT_SYS(0, 0, mprotect(p, 1, PROT_READ | PROT_WRITE));
EXITS(0);
ASSERT_SYS(0, 0, munmap(p, 1));
}
TEST(mmap, hint) { TEST(mmap, hint) {
char *p; char *p;

View file

@ -151,6 +151,32 @@ TEST(fork, preservesTlsMemory) {
EXITS(0); EXITS(0);
} }
TEST(fork, privateExtraPageData_getsCopiedByFork) {
char *p;
ASSERT_NE(MAP_FAILED, (p = mmap(0, 1, PROT_WRITE | PROT_READ,
MAP_ANONYMOUS | MAP_PRIVATE, -1, 0)));
p[0] = 1;
p[1] = 2;
SPAWN(fork);
ASSERT_EQ(1, p[0]);
ASSERT_EQ(2, p[1]);
EXITS(0);
ASSERT_SYS(0, 0, munmap(p, 1));
}
TEST(fork, sharedExtraPageData_getsResurrectedByFork) {
char *p;
ASSERT_NE(MAP_FAILED, (p = mmap(0, 1, PROT_WRITE | PROT_READ,
MAP_ANONYMOUS | MAP_SHARED, -1, 0)));
p[0] = 1;
p[1] = 2;
SPAWN(fork);
ASSERT_EQ(1, p[0]);
ASSERT_EQ(2, p[1]);
EXITS(0);
ASSERT_SYS(0, 0, munmap(p, 1));
}
#define CHECK_TERMSIG \ #define CHECK_TERMSIG \
if (WIFSIGNALED(ws)) { \ if (WIFSIGNALED(ws)) { \
kprintf("%s:%d: error: forked life subprocess terminated with %G\n", \ kprintf("%s:%d: error: forked life subprocess terminated with %G\n", \

View file

@ -3273,6 +3273,7 @@ static char *ServeIndex(const char *path, size_t pathlen) {
p = RoutePath(q, n); p = RoutePath(q, n);
free(q); free(q);
} }
__print_maps(30);
return p; return p;
} }