From 42a3bb729aecc1e45d2e560d8a26712dc3bf7dc5 Mon Sep 17 00:00:00 2001 From: Justine Tunney <jtunney@gmail.com> Date: Sat, 4 Jan 2025 21:11:53 -0800 Subject: [PATCH] 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 --- ape/ape.lds | 2 +- libc/calls/metalfile.c | 7 +- libc/calls/openat-metal.c | 8 +- libc/calls/syscall-nt.internal.h | 4 + libc/intrin/clock_gettime-nt.c | 6 +- libc/intrin/describeallocationtype.c | 32 +++ libc/intrin/describeflags.h | 2 + libc/intrin/directmap-metal.c | 27 +- libc/intrin/directmap-nt.c | 122 --------- libc/intrin/directmap.c | 67 ----- libc/intrin/directmap.h | 14 +- libc/intrin/mmap.c | 135 +++++++++- libc/intrin/munmap-sysv.c | 2 - libc/intrin/sig.c | 7 +- .../getppid-nt.c => intrin/virtualalloc.c} | 26 +- libc/intrin/virtualallocex.c | 23 +- libc/intrin/virtualmax.c | 1 - libc/irq/acpi-xsdt.c | 5 +- libc/nt/kernel32/VirtualAlloc.S | 18 -- libc/nt/master.sh | 2 - libc/proc/BUILD.mk | 1 + libc/proc/execve-nt.greg.c | 112 ++++++-- libc/proc/execve.c | 5 - libc/proc/fork-nt.c | 43 +++- libc/proc/fork.c | 44 +++- libc/proc/getppid-nt.c | 93 +++++++ libc/{calls => proc}/getppid.c | 0 libc/proc/posix_spawn.c | 10 + libc/runtime/morph.c | 5 +- libc/runtime/runtime.h | 1 - libc/runtime/winmain.greg.c | 9 +- libc/{runtime => thread}/isstackoverflow.c | 29 ++- libc/vga/tty.greg.c | 9 +- test/libc/calls/setrlimit_test.c | 242 ------------------ test/libc/calls/stackoverflow1_test.c | 2 +- test/libc/calls/stackoverflow4_test.c | 6 +- test/libc/calls/stackoverflow5_test.c | 84 +++--- test/libc/intrin/mmap_test.c | 36 +++ test/libc/proc/fork_test.c | 26 ++ tool/net/redbean.c | 1 + 40 files changed, 612 insertions(+), 656 deletions(-) create mode 100644 libc/intrin/describeallocationtype.c delete mode 100644 libc/intrin/directmap-nt.c delete mode 100644 libc/intrin/directmap.c rename libc/{calls/getppid-nt.c => intrin/virtualalloc.c} (70%) delete mode 100644 libc/nt/kernel32/VirtualAlloc.S create mode 100644 libc/proc/getppid-nt.c rename libc/{calls => proc}/getppid.c (100%) rename libc/{runtime => thread}/isstackoverflow.c (76%) delete mode 100644 test/libc/calls/setrlimit_test.c diff --git a/ape/ape.lds b/ape/ape.lds index ec63ae7d5..155b0aad9 100644 --- a/ape/ape.lds +++ b/ape/ape.lds @@ -596,7 +596,7 @@ ape_stack_offset = 0; ape_stack_vaddr = DEFINED(ape_stack_vaddr) ? ape_stack_vaddr : 0x700000000000; ape_stack_paddr = ape_ram_paddr + ape_ram_filesz; 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_filesz = ape_note_end - ape_note; diff --git a/libc/calls/metalfile.c b/libc/calls/metalfile.c index d20736e35..5d2c57540 100644 --- a/libc/calls/metalfile.c +++ b/libc/calls/metalfile.c @@ -67,10 +67,9 @@ textstartup void InitializeMetalFile(void) { size_t size = ROUNDUP(_ezip - __executable_start, 4096); // TODO(jart): Restore support for ZIPOS on metal. void *copied_base; - struct DirectMap dm; - dm = sys_mmap_metal(NULL, size, PROT_READ | PROT_WRITE, - MAP_SHARED_linux | MAP_ANONYMOUS_linux, -1, 0); - copied_base = dm.addr; + void *addr = sys_mmap_metal(NULL, size, PROT_READ | PROT_WRITE, + MAP_SHARED_linux | MAP_ANONYMOUS_linux, -1, 0); + copied_base = addr; npassert(copied_base != (void *)-1); memcpy(copied_base, (void *)(BANE + IMAGE_BASE_PHYSICAL), size); __ape_com_base = copied_base; diff --git a/libc/calls/openat-metal.c b/libc/calls/openat-metal.c index 16650f4b3..647af1360 100644 --- a/libc/calls/openat-metal.c +++ b/libc/calls/openat-metal.c @@ -49,11 +49,9 @@ int sys_openat_metal(int dirfd, const char *file, int flags, unsigned mode) { if ((fd = __reservefd(-1)) == -1) return -1; if (!_weaken(calloc) || !_weaken(free)) { - struct DirectMap dm; - dm = sys_mmap_metal(NULL, ROUNDUP(sizeof(struct MetalFile), 4096), - PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, -1, - 0); - state = dm.addr; + state = sys_mmap_metal(NULL, ROUNDUP(sizeof(struct MetalFile), 4096), + PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, + -1, 0); if (state == (void *)-1) return -1; } else { diff --git a/libc/calls/syscall-nt.internal.h b/libc/calls/syscall-nt.internal.h index 2c4e7dbbf..dafbf18ea 100644 --- a/libc/calls/syscall-nt.internal.h +++ b/libc/calls/syscall-nt.internal.h @@ -2,6 +2,9 @@ #define COSMOPOLITAN_LIBC_CALLS_SYSCALL_NT_INTERNAL_H_ COSMOPOLITAN_C_START_ +extern int sys_getppid_nt_cosmo; +extern int sys_getppid_nt_win32; + bool32 sys_isatty(int); int sys_chdir_nt(const char *); 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); ssize_t sys_read_nt_impl(int, void *, size_t, int64_t); ssize_t sys_readlinkat_nt(int, const char *, char *, size_t); +void sys_getppid_nt_wipe(int, int); COSMOPOLITAN_C_END_ #endif /* COSMOPOLITAN_LIBC_CALLS_SYSCALL_NT_INTERNAL_H_ */ diff --git a/libc/intrin/clock_gettime-nt.c b/libc/intrin/clock_gettime-nt.c index 911223cb7..9020e9cfd 100644 --- a/libc/intrin/clock_gettime-nt.c +++ b/libc/intrin/clock_gettime-nt.c @@ -59,7 +59,7 @@ textwindows int sys_clock_gettime_nt(int clock, struct timespec *ts) { // —Quoth MSDN § Windows Time // QueryUnbiasedInterruptTimePrecise(&hectons); - *ts = timespec_fromnanos(hectons * 100); + *ts = WindowsDurationToTimeSpec(hectons); return 0; case _CLOCK_MONOTONIC_COARSE: // @@ -83,7 +83,7 @@ textwindows int sys_clock_gettime_nt(int clock, struct timespec *ts) { // —Quoth MSDN § QueryUnbiasedInterruptTimePrecise // QueryUnbiasedInterruptTime(&hectons); - *ts = timespec_fromnanos(hectons * 100); + *ts = WindowsDurationToTimeSpec(hectons); return 0; case _CLOCK_BOOTTIME: // @@ -95,7 +95,7 @@ textwindows int sys_clock_gettime_nt(int clock, struct timespec *ts) { // —Quoth MSDN § Interrupt Time // QueryInterruptTimePrecise(&hectons); - *ts = timespec_fromnanos(hectons * 100); + *ts = WindowsDurationToTimeSpec(hectons); return 0; case _CLOCK_PROCESS_CPUTIME_ID: GetProcessTimes(GetCurrentProcess(), &ftCreation, &ftExit, &ftKernel, diff --git a/libc/intrin/describeallocationtype.c b/libc/intrin/describeallocationtype.c new file mode 100644 index 000000000..4dd69e733 --- /dev/null +++ b/libc/intrin/describeallocationtype.c @@ -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); +} diff --git a/libc/intrin/describeflags.h b/libc/intrin/describeflags.h index 85814c78d..e63059f0e 100644 --- a/libc/intrin/describeflags.h +++ b/libc/intrin/describeflags.h @@ -29,6 +29,7 @@ const char *_DescribeMapping(char[8], int, int) libcesque; const char *_DescribeMremapFlags(char[30], int) libcesque; const char *_DescribeMsg(char[16], 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 *_DescribeNtConsoleOutFlags(char[128], 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 DescribeMsg(x) _DescribeMsg(alloca(16), x) #define DescribeMsyncFlags(x) _DescribeMsyncFlags(alloca(48), x) +#define DescribeNtAllocationType(x) _DescribeNtAllocationType(alloca(48), x) #define DescribeNtConsoleInFlags(x) _DescribeNtConsoleInFlags(alloca(256), x) #define DescribeNtConsoleOutFlags(x) _DescribeNtConsoleOutFlags(alloca(128), x) #define DescribeNtFileAccessFlags(x) _DescribeNtFileAccessFlags(alloca(512), x) diff --git a/libc/intrin/directmap-metal.c b/libc/intrin/directmap-metal.c index 8ed352fef..30e377da9 100644 --- a/libc/intrin/directmap-metal.c +++ b/libc/intrin/directmap-metal.c @@ -19,7 +19,6 @@ #include "libc/calls/calls.h" #include "libc/calls/internal.h" #include "libc/calls/metalfile.internal.h" -#include "libc/intrin/directmap.h" #include "libc/macros.h" #include "libc/runtime/pc.internal.h" #include "libc/str/str.h" @@ -32,19 +31,11 @@ static uint64_t sys_mmap_metal_break; -static struct DirectMap bad_mmap(void) { - struct DirectMap res; - 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) { +void *sys_mmap_metal(void *vaddr, size_t size, int prot, int flags, int fd, + int64_t off) { /* asan runtime depends on this function */ size_t i; struct mman *mm; - struct DirectMap res; uint64_t addr, faddr = 0, page, e, *pte, *fdpte, *pml4t; mm = __get_mm(); 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 MetalFile *file; if (off < 0 || fd < 0 || fd >= g_fds.n) - return bad_mmap(); + return MAP_FAILED; sfd = &g_fds.p[fd]; if (sfd->kind != kFdFile) - return bad_mmap(); + return MAP_FAILED; file = (struct MetalFile *)sfd->handle; /* TODO: allow mapping partial page at end of file, if file size not * multiple of page size */ if (off > file->size || size > file->size - off) - return bad_mmap(); + return MAP_FAILED; faddr = (uint64_t)file->base + off; if (faddr % 4096 != 0) - return bad_mmap(); + return MAP_FAILED; } if (!(flags & MAP_FIXED_linux)) { 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)) { page = __new_page(mm); if (!page) - return bad_mmap(); + return MAP_FAILED; __clear_page(BANE + page); e = page | PAGE_RSRV | PAGE_U; if ((prot & PROT_WRITE)) @@ -114,9 +105,7 @@ struct DirectMap sys_mmap_metal(void *vaddr, size_t size, int prot, int flags, break; } } - res.addr = (void *)addr; - res.maphandle = -1; - return res; + return (void *)addr; } #endif /* __x86_64__ */ diff --git a/libc/intrin/directmap-nt.c b/libc/intrin/directmap-nt.c deleted file mode 100644 index 3cd19da78..000000000 --- a/libc/intrin/directmap-nt.c +++ /dev/null @@ -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; -} diff --git a/libc/intrin/directmap.c b/libc/intrin/directmap.c deleted file mode 100644 index aa1e4e76c..000000000 --- a/libc/intrin/directmap.c +++ /dev/null @@ -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; -} diff --git a/libc/intrin/directmap.h b/libc/intrin/directmap.h index a3eefc30a..389336a91 100644 --- a/libc/intrin/directmap.h +++ b/libc/intrin/directmap.h @@ -2,19 +2,7 @@ #define COSMOPOLITAN_LIBC_INTRIN_DIRECTMAP_H_ COSMOPOLITAN_C_START_ -struct ProtectNt { - 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); +void *sys_mmap_metal(void *, size_t, int, int, int, int64_t) libcesque; int sys_munmap_metal(void *, size_t) libcesque; int __prot2nt(int, int) libcesque; diff --git a/libc/intrin/mmap.c b/libc/intrin/mmap.c index bd87d3899..adcde3d0c 100644 --- a/libc/intrin/mmap.c +++ b/libc/intrin/mmap.c @@ -18,6 +18,7 @@ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "libc/calls/calls.h" #include "libc/calls/internal.h" +#include "libc/calls/state.internal.h" #include "libc/calls/syscall-sysv.internal.h" #include "libc/calls/syscall_support-nt.internal.h" #include "libc/dce.h" @@ -33,10 +34,14 @@ #include "libc/intrin/weaken.h" #include "libc/limits.h" #include "libc/macros.h" +#include "libc/nt/enum/filemapflags.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/runtime.h" #include "libc/runtime/runtime.h" +#include "libc/runtime/syslib.internal.h" #include "libc/runtime/zipos.internal.h" #include "libc/stdckdint.h" #include "libc/stdio/sysparam.h" @@ -80,6 +85,11 @@ } while (0) #endif +struct DirectMap { + void *addr; + int64_t hand; +}; + 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 *b = (const struct Map *)MAP_TREE_CONTAINER(rb); @@ -421,7 +431,7 @@ void __maps_insert(struct Map *map) { __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) { struct Map *map; if (!(map = __maps_alloc())) @@ -447,6 +457,125 @@ int __maps_untrack(char *addr, size_t size) { 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 *map; 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) return 0; if (IsWindows()) - CloseHandle(sys.maphandle); + CloseHandle(sys.hand); struct MapSlab *slab = sys.addr; 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->prot = prot; map->flags = flags; - map->hand = res.maphandle; + map->hand = res.hand; if (IsWindows()) { map->iscow = (flags & MAP_TYPE) != MAP_SHARED && fd != -1; map->readonlyfile = (flags & MAP_TYPE) == MAP_SHARED && fd != -1 && diff --git a/libc/intrin/munmap-sysv.c b/libc/intrin/munmap-sysv.c index 0f00ddc5c..3d4b0c6ae 100644 --- a/libc/intrin/munmap-sysv.c +++ b/libc/intrin/munmap-sysv.c @@ -41,8 +41,6 @@ int sys_munmap(void *p, size_t n) { } else { rc = __sys_munmap(p, n); } - if (!rc) - __virtualsize -= n; KERNTRACE("sys_munmap(%p, %'zu) → %d", p, n, rc); return rc; } diff --git a/libc/intrin/sig.c b/libc/intrin/sig.c index bfb5cc740..4f622a819 100644 --- a/libc/intrin/sig.c +++ b/libc/intrin/sig.c @@ -83,7 +83,7 @@ struct SignalFrame { }; __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(WriteFile) *const __imp_WriteFile; @@ -566,8 +566,9 @@ textwindows wontreturn static void __sig_death(int sig, const char *thing) { // forceinline void __sig_reguard(void *page) { uint32_t old_protect; - __imp_VirtualProtect((void *)((uintptr_t)page & -__pagesize), __pagesize, - kNtPageReadwrite | kNtPageGuard, &old_protect); + __imp_VirtualProtectEx(GetCurrentProcess(), + (void *)((uintptr_t)page & -__pagesize), __pagesize, + kNtPageReadwrite | kNtPageGuard, &old_protect); } // trampoline for calling signal handler when system reports crash diff --git a/libc/calls/getppid-nt.c b/libc/intrin/virtualalloc.c similarity index 70% rename from libc/calls/getppid-nt.c rename to libc/intrin/virtualalloc.c index 438cafc61..6993d8154 100644 --- a/libc/calls/getppid-nt.c +++ b/libc/intrin/virtualalloc.c @@ -1,7 +1,7 @@ /*-*- 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 │ +│ 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 │ @@ -16,22 +16,14 @@ │ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ -#include "libc/calls/syscall-nt.internal.h" -#include "libc/nt/enum/status.h" -#include "libc/nt/nt/process.h" -#include "libc/nt/process.h" +#include "libc/nt/memory.h" #include "libc/nt/runtime.h" -#include "libc/nt/struct/processbasicinformation.h" -textwindows int sys_getppid_nt(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 GetCurrentProcessId(); +/** + * Allocates memory on The New Technology. + */ +textwindows void *VirtualAlloc(void *lpAddress, uint64_t dwSize, + uint32_t flAllocationType, uint32_t flProtect) { + return VirtualAllocEx(GetCurrentProcess(), lpAddress, dwSize, + flAllocationType, flProtect); } diff --git a/libc/intrin/virtualallocex.c b/libc/intrin/virtualallocex.c index b55caf9aa..77e938819 100644 --- a/libc/intrin/virtualallocex.c +++ b/libc/intrin/virtualallocex.c @@ -19,32 +19,23 @@ #include "libc/calls/syscall_support-nt.internal.h" #include "libc/intrin/describeflags.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/thunk/msabi.h" __msabi extern typeof(VirtualAllocEx) *const __imp_VirtualAllocEx; -static const char *DescribeAllocationType(char buf[48], uint32_t x) { - const struct DescribeFlags kAllocationTypeFlags[] = { - {kNtMemCommit, "Commit"}, // - {kNtMemReserve, "Reserve"}, // - {kNtMemReset, "Reset"}, // - }; - 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) { +/** + * Allocates memory on The New Technology. + */ +textwindows void *VirtualAllocEx(int64_t hProcess, void *lpAddress, + uint64_t dwSize, uint32_t flAllocationType, + uint32_t flProtect) { void *res = __imp_VirtualAllocEx(hProcess, lpAddress, dwSize, flAllocationType, flProtect); if (!res) __winerr(); NTTRACE("VirtualAllocEx(%ld, %p, %'lu, %s, %s) → %p% m", hProcess, lpAddress, - dwSize, DescribeAllocationType(alloca(48), flAllocationType), + dwSize, DescribeNtAllocationType(flAllocationType), DescribeNtPageFlags(flProtect), res); return res; } diff --git a/libc/intrin/virtualmax.c b/libc/intrin/virtualmax.c index 4f24070e2..e6b5b1888 100644 --- a/libc/intrin/virtualmax.c +++ b/libc/intrin/virtualmax.c @@ -19,4 +19,3 @@ #include "libc/runtime/runtime.h" size_t __virtualmax = -1; -size_t __virtualsize = 0; diff --git a/libc/irq/acpi-xsdt.c b/libc/irq/acpi-xsdt.c index 44c2a6da5..83b71ffd1 100644 --- a/libc/irq/acpi-xsdt.c +++ b/libc/irq/acpi-xsdt.c @@ -58,9 +58,8 @@ textstartup void *_AcpiOsMapUncachedMemory(uintptr_t phy, size_t n) { } textstartup static void *_AcpiOsAllocatePages(size_t n) { - struct DirectMap dm = sys_mmap_metal(NULL, n, PROT_READ | PROT_WRITE, - MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); - void *addr = dm.addr; + void *addr = sys_mmap_metal(NULL, n, PROT_READ | PROT_WRITE, + MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); if (addr == (void *)-1) addr = NULL; return addr; diff --git a/libc/nt/kernel32/VirtualAlloc.S b/libc/nt/kernel32/VirtualAlloc.S deleted file mode 100644 index f8e5f815a..000000000 --- a/libc/nt/kernel32/VirtualAlloc.S +++ /dev/null @@ -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 diff --git a/libc/nt/master.sh b/libc/nt/master.sh index d13447f2d..9d3ae3d3b 100755 --- a/libc/nt/master.sh +++ b/libc/nt/master.sh @@ -9,7 +9,6 @@ # KERNEL32.DLL # # Name Actual DLL Arity - imp '' CreateDirectoryW kernel32 2 imp '' CreateFileA kernel32 7 imp '' CreateFileMappingNumaW kernel32 7 @@ -303,7 +302,6 @@ imp 'UnlockFile' UnlockFile kernel32 5 imp 'UnmapViewOfFile2' UnmapViewOfFile2 kernel32 2 imp 'UnmapViewOfFileEx' UnmapViewOfFileEx kernel32 3 imp 'UpdateProcThreadAttribute' UpdateProcThreadAttribute kernel32 7 -imp 'VirtualAlloc' VirtualAlloc kernel32 4 imp 'VirtualFree' VirtualFree kernel32 3 imp 'VirtualLock' VirtualLock kernel32 2 imp 'VirtualQuery' VirtualQuery kernel32 3 diff --git a/libc/proc/BUILD.mk b/libc/proc/BUILD.mk index 3e0e0c894..8491e5635 100644 --- a/libc/proc/BUILD.mk +++ b/libc/proc/BUILD.mk @@ -30,6 +30,7 @@ LIBC_PROC_A_DIRECTDEPS = \ LIBC_MEM \ LIBC_NEXGEN32E \ LIBC_NT_KERNEL32 \ + LIBC_NT_NTDLL \ LIBC_NT_PSAPI \ LIBC_RUNTIME \ LIBC_STR \ diff --git a/libc/proc/execve-nt.greg.c b/libc/proc/execve-nt.greg.c index c09988018..cfb0ab1fc 100644 --- a/libc/proc/execve-nt.greg.c +++ b/libc/proc/execve-nt.greg.c @@ -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__ */ diff --git a/libc/proc/execve.c b/libc/proc/execve.c index a88ed55b4..b610f8b29 100644 --- a/libc/proc/execve.c +++ b/libc/proc/execve.c @@ -57,11 +57,6 @@ * compiled by MSVC or Cygwin is launched instead, then only the stdio * 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 * to be valid UTF-8 in order to round-trip the WIN32 API, without being * corrupted. diff --git a/libc/proc/fork-nt.c b/libc/proc/fork-nt.c index 20cef986c..4e0679b23 100644 --- a/libc/proc/fork-nt.c +++ b/libc/proc/fork-nt.c @@ -46,6 +46,7 @@ #include "libc/nt/winsock.h" #include "libc/proc/proc.h" #include "libc/runtime/internal.h" +#include "libc/runtime/runtime.h" #include "libc/runtime/symbols.internal.h" #include "libc/sysv/consts/map.h" #include "libc/sysv/consts/prot.h" @@ -211,8 +212,6 @@ textwindows static int sys_fork_nt_parent(uint32_t dwCreationFlags) { // let's go bool ok = true; - uint32_t child_old_protect; - uint32_t parent_old_protect; // copy memory manager maps for (struct MapSlab *slab = @@ -225,11 +224,12 @@ textwindows static int sys_fork_nt_parent(uint32_t dwCreationFlags) { } // copy private memory maps + int alloc_prot = -1; for (struct Map *map = __maps_first(); map; map = __maps_next(map)) { 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) - continue; + continue; // ignore things like signal worker stack memory if (__maps_isalloc(map)) { size_t allocsize = map->size; 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) { - ok = ok && !!VirtualProtectEx(procinfo.hProcess, map->addr, allocsize, - kNtPageReadwrite, &child_old_protect); + // portable executable segment + if (!(map->prot & PROT_WRITE)) { + uint32_t child_old_protect; + ok = ok && !!VirtualProtectEx(procinfo.hProcess, map->addr, allocsize, + kNtPageReadwrite, &child_old_protect); + alloc_prot = PROT_READ | PROT_WRITE; + } 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, - kNtMemReserve | kNtMemCommit, - kNtPageExecuteReadwrite); + kNtMemReserve | kNtMemCommit, page_flags); } } + uint32_t parent_old_protect; if (!(map->prot & PROT_READ)) ok = ok && !!VirtualProtect(map->addr, map->size, kNtPageReadwrite, &parent_old_protect); - ok = ok && !!WriteProcessMemory(procinfo.hProcess, map->addr, map->addr, - map->size, 0); ok = ok && - !!VirtualProtectEx(procinfo.hProcess, map->addr, map->size, - __prot2nt(map->prot, false), &child_old_protect); + !!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 && + !!VirtualProtectEx(procinfo.hProcess, map->addr, map->size, + __prot2nt(map->prot, false), &child_old_protect); + } if (!(map->prot & PROT_READ)) ok = ok && !!VirtualProtect(map->addr, map->size, parent_old_protect, &parent_old_protect); diff --git a/libc/proc/fork.c b/libc/proc/fork.c index 046b7c983..eab5cfb09 100644 --- a/libc/proc/fork.c +++ b/libc/proc/fork.c @@ -20,6 +20,7 @@ #include "libc/calls/internal.h" #include "libc/calls/sig.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/timespec.h" #include "libc/calls/syscall-nt.internal.h" @@ -43,6 +44,7 @@ #include "libc/runtime/syslib.internal.h" #include "libc/stdio/internal.h" #include "libc/str/str.h" +#include "libc/sysv/consts/ss.h" #include "libc/thread/itimer.h" #include "libc/thread/posixthread.internal.h" #include "libc/thread/thread.h" @@ -120,8 +122,7 @@ static void fork_prepare(void) { if (_weaken(__dlopen_lock)) _weaken(__dlopen_lock)(); if (IsWindows()) - if (_weaken(__proc_lock)) - _weaken(__proc_lock)(); + __proc_lock(); if (_weaken(cosmo_stack_lock)) _weaken(cosmo_stack_lock)(); __cxa_lock(); @@ -155,8 +156,7 @@ static void fork_parent(void) { if (_weaken(cosmo_stack_unlock)) _weaken(cosmo_stack_unlock)(); if (IsWindows()) - if (_weaken(__proc_unlock)) - _weaken(__proc_unlock)(); + __proc_unlock(); if (_weaken(__dlopen_unlock)) _weaken(__dlopen_unlock)(); if (_weaken(__localtime_unlock)) @@ -167,7 +167,7 @@ static void fork_parent(void) { _pthread_mutex_unlock(&supreme_lock); } -static void fork_child(void) { +static void fork_child(int ppid_win32, int ppid_cosmo) { if (_weaken(__rand64_wipe)) _weaken(__rand64_wipe)(); _pthread_mutex_wipe_np(&__fds_lock_obj); @@ -194,6 +194,8 @@ static void fork_child(void) { _pthread_mutex_wipe_np(&__sig_worker_lock); if (_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)) _weaken(_pthread_onfork_child)(); @@ -202,8 +204,9 @@ static void fork_child(void) { int _fork(uint32_t dwCreationFlags) { struct Dll *e; - int ax, dx, tid, parent; - parent = __pid; + int ax, dx, tid, ppid_win32, ppid_cosmo; + ppid_win32 = IsWindows() ? GetCurrentProcessId() : 0; + ppid_cosmo = __pid; BLOCK_SIGNALS; fork_prepare(); if (!IsWindows()) { @@ -223,7 +226,7 @@ int _fork(uint32_t dwCreationFlags) { // get new thread id 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(); atomic_init(&tib->tib_ctid, tid); atomic_init(&tib->tib_ptid, tid); @@ -243,10 +246,10 @@ int _fork(uint32_t dwCreationFlags) { // turn other threads into zombies // we can't free() them since we're monopolizing all locks // 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; _pthread_list = 0; - dll_make_first(&_pthread_list, &pt->list); + dll_make_first(&_pthread_list, &me->list); atomic_init(&_pthread_count, 1); // get new system thread handle @@ -264,25 +267,38 @@ int _fork(uint32_t dwCreationFlags) { atomic_init(&tib->tib_sigpending, 0); // 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 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 - fork_child(); + fork_child(ppid_win32, ppid_cosmo); // free threads if (_weaken(_pthread_free)) { while ((e = dll_first(old_threads))) { - pt = POSIXTHREAD_CONTAINER(e); + struct PosixThread *pt = POSIXTHREAD_CONTAINER(e); atomic_init(&pt->tib->tib_syshand, 0); dll_remove(&old_threads, e); _weaken(_pthread_free)(pt); } } - STRACE("fork() → 0 (child of %d)", parent); + STRACE("fork() → 0 (child of %d)", ppid_cosmo); } else { // this is the parent process fork_parent(); diff --git a/libc/proc/getppid-nt.c b/libc/proc/getppid-nt.c new file mode 100644 index 000000000..c602042e6 --- /dev/null +++ b/libc/proc/getppid-nt.c @@ -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; +} diff --git a/libc/calls/getppid.c b/libc/proc/getppid.c similarity index 100% rename from libc/calls/getppid.c rename to libc/proc/getppid.c diff --git a/libc/proc/posix_spawn.c b/libc/proc/posix_spawn.c index d2dcf7f41..4dbbdcea9 100644 --- a/libc/proc/posix_spawn.c +++ b/libc/proc/posix_spawn.c @@ -51,6 +51,7 @@ #include "libc/nt/enum/processcreationflags.h" #include "libc/nt/enum/startf.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" @@ -59,6 +60,7 @@ #include "libc/proc/posix_spawn.h" #include "libc/proc/posix_spawn.internal.h" #include "libc/proc/proc.h" +#include "libc/runtime/internal.h" #include "libc/runtime/runtime.h" #include "libc/sock/sock.h" #include "libc/stdio/stdio.h" @@ -396,6 +398,14 @@ static textwindows errno_t posix_spawn_nt_impl( } 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 int rc = -1; struct NtProcessInformation procinfo; diff --git a/libc/runtime/morph.c b/libc/runtime/morph.c index 08abcc410..c3bcc4ae3 100644 --- a/libc/runtime/morph.c +++ b/libc/runtime/morph.c @@ -24,12 +24,13 @@ #include "libc/intrin/kprintf.h" #include "libc/nt/enum/pageflags.h" #include "libc/nt/memory.h" +#include "libc/nt/runtime.h" #include "libc/nt/thunk/msabi.h" #include "libc/runtime/runtime.h" #include "libc/sysv/consts/nr.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) { #ifdef __x86_64__ @@ -54,7 +55,7 @@ __funline void __morph_mprotect(void *addr, size_t size, int prot, int ntprot) { } #endif } else { - __imp_VirtualProtect(addr, size, ntprot, &op); + __imp_VirtualProtectEx(GetCurrentProcess(), addr, size, ntprot, &op); } #elif defined(__aarch64__) register long r0 asm("x0") = (long)addr; diff --git a/libc/runtime/runtime.h b/libc/runtime/runtime.h index 8a0dc5fc3..4ea96a3cc 100644 --- a/libc/runtime/runtime.h +++ b/libc/runtime/runtime.h @@ -83,7 +83,6 @@ extern uint64_t kStartTsc; extern const char kNtSystemDirectory[]; extern const char kNtWindowsDirectory[]; extern size_t __virtualmax; -extern size_t __virtualsize; extern size_t __stackmax; extern bool32 __isworker; /* utilities */ diff --git a/libc/runtime/winmain.greg.c b/libc/runtime/winmain.greg.c index 3e85b6860..640314f93 100644 --- a/libc/runtime/winmain.greg.c +++ b/libc/runtime/winmain.greg.c @@ -79,7 +79,7 @@ __msabi extern typeof(SetConsoleMode) *const __imp_SetConsoleMode; __msabi extern typeof(SetConsoleOutputCP) *const __imp_SetConsoleOutputCP; __msabi extern typeof(SetEnvironmentVariable) *const __imp_SetEnvironmentVariableW; __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; // clang-format on @@ -206,11 +206,12 @@ abi wontreturn static void WinInit(const char16_t *cmdline) { int stackprot = (intptr_t)ape_stack_prot; if (~stackprot & PROT_EXEC) { uint32_t old; - __imp_VirtualProtect(stackaddr, stacksize, kNtPageReadwrite, &old); + __imp_VirtualProtectEx(GetCurrentProcess(), stackaddr, stacksize, + kNtPageReadwrite, &old); } uint32_t oldattr; - __imp_VirtualProtect(stackaddr, GetGuardSize(), - kNtPageReadwrite | kNtPageGuard, &oldattr); + __imp_VirtualProtectEx(GetCurrentProcess(), stackaddr, GetGuardSize(), + kNtPageReadwrite | kNtPageGuard, &oldattr); if (_weaken(__maps_stack)) { struct NtSystemInfo si; __imp_GetSystemInfo(&si); diff --git a/libc/runtime/isstackoverflow.c b/libc/thread/isstackoverflow.c similarity index 76% rename from libc/runtime/isstackoverflow.c rename to libc/thread/isstackoverflow.c index 35c646dd9..850eb5a60 100644 --- a/libc/runtime/isstackoverflow.c +++ b/libc/thread/isstackoverflow.c @@ -23,17 +23,36 @@ #include "libc/runtime/runtime.h" #include "libc/sysv/consts/auxv.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) { + + // sanity check ucontext_t *uc = arg; if (!si || !uc) return false; - if (si->si_signo != SIGSEGV && si->si_signo != SIGBUS) + if (si->si_signo != SIGSEGV && // + si->si_signo != SIGBUS) return false; - intptr_t sp = uc->uc_mcontext.SP; - intptr_t fp = (intptr_t)si->si_addr; - return ABS(fp - sp) < __pagesize; + + // get stack information + 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; } diff --git a/libc/vga/tty.greg.c b/libc/vga/tty.greg.c index ad1f009d7..7b2738a3b 100644 --- a/libc/vga/tty.greg.c +++ b/libc/vga/tty.greg.c @@ -167,7 +167,6 @@ void _StartTty(struct Tty *tty, unsigned char type, unsigned short yp, unsigned short startx, unsigned char yc, unsigned char xc, void *fb, unsigned init_flags) { unsigned short yn, xn, xs = xp * sizeof(TtyCanvasColor); - struct DirectMap dm; bzero(tty, sizeof(struct Tty)); SetYp(tty, yp); SetXp(tty, xp); @@ -183,9 +182,9 @@ void _StartTty(struct Tty *tty, unsigned char type, unsigned short yp, tty->canvas = fb; xs = xsfb; } else { - dm = sys_mmap_metal(NULL, (size_t)yp * xs, PROT_READ | PROT_WRITE, - MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); - if (dm.addr == (void *)-1) { + void *addr = sys_mmap_metal(NULL, (size_t)yp * xs, PROT_READ | PROT_WRITE, + MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); + if (addr == (void *)-1) { /* * We are a bit low on memory. Try to go on anyway, & initialize * our tty as an emergency console. @@ -194,7 +193,7 @@ void _StartTty(struct Tty *tty, unsigned char type, unsigned short yp, tty->canvas = fb; xs = xsfb; } else - tty->canvas = dm.addr; + tty->canvas = addr; } } SetYn(tty, yn); diff --git a/test/libc/calls/setrlimit_test.c b/test/libc/calls/setrlimit_test.c deleted file mode 100644 index eb1e75cd7..000000000 --- a/test/libc/calls/setrlimit_test.c +++ /dev/null @@ -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); -} diff --git a/test/libc/calls/stackoverflow1_test.c b/test/libc/calls/stackoverflow1_test.c index 6f1e2a32b..c9397cbba 100644 --- a/test/libc/calls/stackoverflow1_test.c +++ b/test/libc/calls/stackoverflow1_test.c @@ -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, si->si_addr, ((ucontext_t *)ctx)->uc_mcontext.SP); smashed_stack = true; - unassert(__is_stack_overflow(si, ctx)); + // unassert(__is_stack_overflow(si, ctx)); // fuzzy with main thread longjmp(recover, 123); } diff --git a/test/libc/calls/stackoverflow4_test.c b/test/libc/calls/stackoverflow4_test.c index 54d8e240b..a9b1eab2f 100644 --- a/test/libc/calls/stackoverflow4_test.c +++ b/test/libc/calls/stackoverflow4_test.c @@ -16,6 +16,7 @@ │ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ +#include "libc/assert.h" #include "libc/calls/struct/sigaction.h" #include "libc/calls/struct/sigaltstack.h" #include "libc/calls/struct/siginfo.h" @@ -40,8 +41,9 @@ volatile bool smashed_stack; -void CrashHandler(int sig) { +void CrashHandler(int sig, siginfo_t *si, void *ctx) { smashed_stack = true; + unassert(__is_stack_overflow(si, ctx)); pthread_exit((void *)123L); } @@ -63,7 +65,7 @@ void *MyPosixThread(void *arg) { ASSERT_SYS(0, 0, sigaltstack(&ss, 0)); sa.sa_flags = SA_SIGINFO | SA_ONSTACK; // <-- important sigemptyset(&sa.sa_mask); - sa.sa_handler = CrashHandler; + sa.sa_sigaction = CrashHandler; sigaction(SIGBUS, &sa, 0); sigaction(SIGSEGV, &sa, 0); exit(StackOverflow(1)); diff --git a/test/libc/calls/stackoverflow5_test.c b/test/libc/calls/stackoverflow5_test.c index 2d15845a8..29a4097d1 100644 --- a/test/libc/calls/stackoverflow5_test.c +++ b/test/libc/calls/stackoverflow5_test.c @@ -16,22 +16,28 @@ │ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ -#include <cosmo.h> -#include <limits.h> -#include <pthread.h> -#include <signal.h> -#include <unistd.h> +#include "libc/assert.h" +#include "libc/calls/calls.h" +#include "libc/calls/struct/sigaction.h" +#include "libc/calls/struct/siginfo.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 - * use the cosmo posix threads extensions + * stack overflow test #5 + * - make sure fork() preserves sigaltstack() + * - make sure fork() preserves guard page status */ -sig_atomic_t smashed_stack; +jmp_buf recover; -void CrashHandler(int sig) { - smashed_stack = true; - pthread_exit(0); +void CrashHandler(int sig, siginfo_t *si, void *ctx) { + unassert(__is_stack_overflow(si, ctx)); + longjmp(recover, 123); } int StackOverflow(int d) { @@ -44,42 +50,40 @@ int StackOverflow(int d) { } 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; } int main() { - // choose the most dangerously small size possible - size_t sigstacksize = sysconf(_SC_MINSIGSTKSZ) + 2048; - - // setup signal handler struct sigaction sa; + sa.sa_flags = SA_SIGINFO | SA_ONSTACK; sigemptyset(&sa.sa_mask); - sa.sa_flags = SA_ONSTACK; - sa.sa_handler = CrashHandler; - if (sigaction(SIGBUS, &sa, 0)) - return 1; - if (sigaction(SIGSEGV, &sa, 0)) - return 2; + sa.sa_sigaction = CrashHandler; + unassert(!sigaction(SIGBUS, &sa, 0)); + unassert(!sigaction(SIGSEGV, &sa, 0)); - // create thread with signal stack - pthread_t id; + pthread_t th; pthread_attr_t attr; - if (pthread_attr_init(&attr)) - return 3; - if (pthread_attr_setguardsize(&attr, getpagesize())) - return 4; - if (pthread_attr_setsigaltstacksize_np(&attr, sigstacksize)) - return 5; - 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(); + unassert(!pthread_attr_init(&attr)); + unassert(!pthread_attr_setguardsize(&attr, getpagesize())); + unassert(!pthread_attr_setsigaltstacksize_np(&attr, SIGSTKSZ)); + unassert(!pthread_create(&th, &attr, MyPosixThread, 0)); + unassert(!pthread_attr_destroy(&attr)); + unassert(!pthread_join(th, 0)); } diff --git a/test/libc/intrin/mmap_test.c b/test/libc/intrin/mmap_test.c index 7d186e6bd..e44ee223e 100644 --- a/test/libc/intrin/mmap_test.c +++ b/test/libc/intrin/mmap_test.c @@ -116,6 +116,42 @@ TEST(mmap, fixedTaken) { 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) { char *p; diff --git a/test/libc/proc/fork_test.c b/test/libc/proc/fork_test.c index 65b12baf8..264f226d3 100644 --- a/test/libc/proc/fork_test.c +++ b/test/libc/proc/fork_test.c @@ -151,6 +151,32 @@ TEST(fork, preservesTlsMemory) { 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 \ if (WIFSIGNALED(ws)) { \ kprintf("%s:%d: error: forked life subprocess terminated with %G\n", \ diff --git a/tool/net/redbean.c b/tool/net/redbean.c index 93816d1aa..5a2be6864 100644 --- a/tool/net/redbean.c +++ b/tool/net/redbean.c @@ -3273,6 +3273,7 @@ static char *ServeIndex(const char *path, size_t pathlen) { p = RoutePath(q, n); free(q); } + __print_maps(30); return p; }