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

@ -25,10 +25,12 @@
#include "libc/intrin/weaken.h"
#include "libc/log/backtrace.internal.h"
#include "libc/macros.h"
#include "libc/mem/alloca.h"
#include "libc/nexgen32e/gc.internal.h"
#include "libc/nexgen32e/stackframe.h"
#include "libc/runtime/memtrack.internal.h"
#include "libc/runtime/runtime.h"
#include "libc/runtime/stack.h"
#include "libc/runtime/symbols.internal.h"
#include "libc/str/str.h"
#include "libc/thread/thread.h"
@ -50,9 +52,10 @@ dontinstrument int PrintBacktraceUsingSymbols(int fd,
const struct StackFrame *bp,
struct SymbolTable *st) {
size_t gi;
char *cxxbuf;
intptr_t addr;
const char *name;
char cxxbuf[3000];
int cxxbufsize = 0;
int i, symbol, addend;
struct Garbages *garbage;
const struct StackFrame *frame;
@ -91,14 +94,25 @@ dontinstrument int PrintBacktraceUsingSymbols(int fd,
symbol = 0;
addend = 0;
}
if ((name = __get_symbol_name(st, symbol)) &&
(_weaken(__is_mangled) && _weaken(__is_mangled)(name))) {
_weaken(__demangle)(cxxbuf, name, sizeof(cxxbuf));
kprintf("%012lx %lx %s%+d\n", frame, addr, cxxbuf, addend);
name = cxxbuf;
} else {
kprintf("%012lx %lx %s%+d\n", frame, addr, name, addend);
name = __get_symbol_name(st, symbol);
#pragma GCC push_options
#pragma GCC diagnostic ignored "-Walloca-larger-than="
// decipher c++ symbols if there's enough stack memory
// stack size requirement assumes max_depth's still 20
if (_weaken(__demangle) && //
_weaken(__is_mangled) && //
_weaken(__is_mangled)(name)) {
if (!cxxbufsize)
if ((cxxbufsize = __get_safe_size(8192, 8192)) >= 512) {
cxxbuf = alloca(cxxbufsize);
CheckLargeStackAllocation(cxxbuf, sizeof(cxxbufsize));
}
if (cxxbufsize >= 512)
if (_weaken(__demangle)(cxxbuf, name, cxxbufsize) != -1)
name = cxxbuf;
}
#pragma GCC pop_options
kprintf("%012lx %lx %s%+d\n", frame, addr, name, addend);
}
return 0;
}

View file

@ -396,12 +396,6 @@ relegated void __oncrash(int sig, siginfo_t *si, void *arg) {
SpinLock(&lock);
__oncrash_impl(sig, si, arg);
// unlike amd64, the instruction pointer on arm64 isn't advanced past
// the debugger breakpoint instruction automatically. we need this so
// execution can resume after __builtin_trap().
if (arg && sig == SIGTRAP)
((ucontext_t *)arg)->uc_mcontext.PC += 4;
// ensure execution doesn't resume for anything but SIGTRAP / SIGQUIT
if (arg && sig != SIGTRAP && sig != SIGQUIT) {
if (!IsXnu()) {