mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-07-03 17:58:30 +00:00
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:
parent
c97a858470
commit
42a3bb729a
40 changed files with 612 additions and 656 deletions
|
@ -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,
|
||||
|
|
32
libc/intrin/describeallocationtype.c
Normal file
32
libc/intrin/describeallocationtype.c
Normal 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);
|
||||
}
|
|
@ -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)
|
||||
|
|
|
@ -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__ */
|
||||
|
|
|
@ -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;
|
||||
}
|
|
@ -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;
|
||||
}
|
|
@ -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;
|
||||
|
||||
|
|
|
@ -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 &&
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
|
29
libc/intrin/virtualalloc.c
Normal file
29
libc/intrin/virtualalloc.c
Normal file
|
@ -0,0 +1,29 @@
|
|||
/*-*- 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/nt/memory.h"
|
||||
#include "libc/nt/runtime.h"
|
||||
|
||||
/**
|
||||
* 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);
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -19,4 +19,3 @@
|
|||
#include "libc/runtime/runtime.h"
|
||||
|
||||
size_t __virtualmax = -1;
|
||||
size_t __virtualsize = 0;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue