Improve memory manager and signal handling

On Windows, mmap() now chooses addresses transactionally. It reduces the
risk of badness when interacting with the WIN32 memory manager. We don't
throw darts anymore. There is also no more retry limit, since we recover
from mystery maps more gracefully. The subroutine for combining adjacent
maps has been rewritten for clarity. The print maps subroutine is better

This change goes to great lengths to perfect the stack overflow code. On
Windows you can now longjmp() out of a crash signal handler. Guard pages
previously weren't being restored properly by the signal handler. That's
fixed, so on Windows you can now handle a stack overflow multiple times.
Great thought has been put into selecting the perfect SIGSTKSZ constants
so you can save sigaltstack() memory. You can now use kprintf() with 512
bytes of stack available. The guard pages beneath the main stack are now
recorded in the memory manager.

This change fixes getcontext() so it works right with the %rax register.
This commit is contained in:
Justine Tunney 2024-12-27 01:03:11 -08:00
parent 36e5861b0c
commit 379cd77078
No known key found for this signature in database
GPG key ID: BE714B4575D6E328
48 changed files with 834 additions and 570 deletions

View file

@ -220,7 +220,7 @@ textstartup void __enable_tls(void) {
DuplicateHandle(GetCurrentProcess(), GetCurrentThread(),
GetCurrentProcess(), &hThread, 0, false,
kNtDuplicateSameAccess);
atomic_store_explicit(&tib->tib_syshand, hThread, memory_order_relaxed);
atomic_init(&tib->tib_syshand, hThread);
} else if (IsXnuSilicon()) {
tib->tib_syshand = __syslib->__pthread_self();
}
@ -233,23 +233,22 @@ textstartup void __enable_tls(void) {
} else {
tid = sys_gettid();
}
atomic_store_explicit(&tib->tib_tid, tid, memory_order_relaxed);
atomic_init(&tib->tib_tid, tid);
// TODO(jart): set_tid_address?
// inherit signal mask
if (IsWindows()) {
atomic_store_explicit(&tib->tib_sigmask,
ParseMask(__getenv(environ, "_MASK").s),
memory_order_relaxed);
}
if (IsWindows())
atomic_init(&tib->tib_sigmask, ParseMask(__getenv(environ, "_MASK").s));
// initialize posix threads
_pthread_static.tib = tib;
_pthread_static.pt_flags = PT_STATIC;
_pthread_static.pt_locale = &__global_locale;
_pthread_static.pt_attr.__stackaddr = __maps.stack.addr;
_pthread_static.pt_attr.__stacksize = __maps.stack.size;
dll_init(&_pthread_static.list);
_pthread_list = &_pthread_static.list;
atomic_store_explicit(&_pthread_static.ptid, tid, memory_order_release);
atomic_init(&_pthread_static.ptid, tid);
// ask the operating system to change the x86 segment register
if (IsWindows())

View file

@ -130,7 +130,8 @@ static struct SymbolTable *OpenSymbolTableImpl(const char *filename) {
++j;
}
t->count = j;
munmap(stp, sizeof(const Elf64_Sym *) * n);
if (!IsWindows())
munmap(stp, sizeof(const Elf64_Sym *) * n);
munmap(map, filesize);
close(fd);
return t;
@ -144,9 +145,8 @@ RaiseEnoexec:
errno = ENOEXEC;
SystemError:
STRACE("OpenSymbolTable()% m");
if (map != MAP_FAILED) {
if (map != MAP_FAILED)
munmap(map, filesize);
}
close(fd);
return 0;
}

View file

@ -29,7 +29,7 @@
// Saves caller CPU state and signal mask.
//
// @param rdi points to jmp_buf
// @param rdi points to sigjmp_buf
// @param esi if non-zero will cause mask to be saved
// @return eax 0 when set and !0 when longjmp'd
// @returnstwice

View file

@ -45,8 +45,8 @@
* - `_SC_GRANSIZE` returns addr alignment for mmap()
* - `_SC_CLK_TCK` returns number of clock ticks per second
* - `_SC_ARG_MAX` will perform expensive rlimit calculations
* - `_SC_SIGSTKSZ` returns host platform's preferred SIGSTKSZ
* - `_SC_MINSIGSTKSZ` returns host platform's required MINSIGSTKSZ
* - `_SC_SIGSTKSZ` returns recommended `SIGSTKSZ` for platform
* - `_SC_MINSIGSTKSZ` returns size of kernel pushed signal frame
* - `_SC_AVPHYS_PAGES` returns average physical memory pages
* - `_SC_PHYS_PAGES` returns physical memory pages available
* - `_SC_NPROCESSORS_ONLN` returns number of effective CPUs
@ -67,7 +67,7 @@ long sysconf(int name) {
case _SC_ARG_MAX:
return __get_arg_max();
case _SC_SIGSTKSZ:
return _SIGSTKSZ;
return __get_minsigstksz() + SIGSTKSZ;
case _SC_MINSIGSTKSZ:
return __get_minsigstksz();
case _SC_CHILD_MAX:

View file

@ -63,11 +63,9 @@ static void __zipos_dismiss(uint8_t *map, const uint8_t *cdir, long pg) {
}
// unmap the executable portion beneath the local files
if (!IsWindows()) {
mo = ROUNDDOWN(lo, __gransize);
if (mo)
munmap(map, mo);
}
mo = ROUNDDOWN(lo, __gransize);
if (mo && !IsWindows())
munmap(map, mo);
}
static int __zipos_compare_names(const void *a, const void *b, void *c) {