mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-06-26 22:38: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
|
@ -1,39 +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 2023 Justine Alexandra Roberts Tunney │
|
||||
│ │
|
||||
│ Permission to use, copy, modify, and/or distribute this software for │
|
||||
│ any purpose with or without fee is hereby granted, provided that the │
|
||||
│ 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/calls/struct/siginfo.h"
|
||||
#include "libc/calls/struct/ucontext.internal.h"
|
||||
#include "libc/calls/ucontext.h"
|
||||
#include "libc/macros.h"
|
||||
#include "libc/runtime/runtime.h"
|
||||
#include "libc/sysv/consts/auxv.h"
|
||||
#include "libc/sysv/consts/sig.h"
|
||||
|
||||
/**
|
||||
* Returns true if signal is most likely a stack overflow.
|
||||
*/
|
||||
char __is_stack_overflow(siginfo_t *si, void *arg) {
|
||||
ucontext_t *uc = arg;
|
||||
if (!si || !uc)
|
||||
return false;
|
||||
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;
|
||||
}
|
|
@ -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;
|
||||
|
|
|
@ -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 */
|
||||
|
|
|
@ -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);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue