mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-07-07 19:58:30 +00:00
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:
parent
36e5861b0c
commit
379cd77078
48 changed files with 834 additions and 570 deletions
|
@ -113,7 +113,7 @@ static int sigaltstack_bsd(const struct sigaltstack *neu,
|
|||
* struct sigaction sa;
|
||||
* struct sigaltstack ss;
|
||||
* ss.ss_flags = 0;
|
||||
* ss.ss_size = sysconf(_SC_MINSIGSTKSZ) + 8192;
|
||||
* ss.ss_size = sysconf(_SC_SIGSTKSZ);
|
||||
* ss.ss_sp = malloc(ss.ss_size);
|
||||
* sigaltstack(&ss, 0);
|
||||
* sigemptyset(&sa.ss_mask);
|
||||
|
@ -121,11 +121,16 @@ static int sigaltstack_bsd(const struct sigaltstack *neu,
|
|||
* sa.sa_handler = OnStackOverflow;
|
||||
* sigaction(SIGSEGV, &sa, 0);
|
||||
*
|
||||
* Your stack size should be `sysconf(_SC_SIGSTKSZ)` which should be
|
||||
* somewhere in the ballpark of 32kb to 64kb. You should go no lower
|
||||
* than `sysconf(_SC_MINSIGSTKSZ) + 2048` which could be 4kb - 34kb.
|
||||
* Cosmo also defines `SIGSTKSZ` as 32kb, which should also be safe.
|
||||
*
|
||||
* @param neu if non-null will install new signal alt stack
|
||||
* @param old if non-null will receive current signal alt stack
|
||||
* @return 0 on success, or -1 w/ errno
|
||||
* @raise EFAULT if bad memory was supplied
|
||||
* @raise ENOMEM if `neu->ss_size` is less than `MINSIGSTKSZ`
|
||||
* @raise ENOMEM if `neu->ss_size` is beneath `sysconf(_SC_MINSIGSTKSZ)`
|
||||
*/
|
||||
int sigaltstack(const struct sigaltstack *neu, struct sigaltstack *old) {
|
||||
int rc;
|
||||
|
|
|
@ -33,6 +33,7 @@
|
|||
#include "libc/runtime/syslib.internal.h"
|
||||
#include "libc/str/str.h"
|
||||
#include "libc/sysv/consts/sa.h"
|
||||
#include "libc/sysv/consts/sig.h"
|
||||
|
||||
/**
|
||||
* @fileoverview XNU kernel callback normalization.
|
||||
|
@ -513,6 +514,7 @@ privileged void __sigenter_xnu(int sig, struct siginfo_xnu *xnuinfo,
|
|||
flags = __sighandflags[sig];
|
||||
|
||||
#ifdef __aarch64__
|
||||
|
||||
// xnu silicon claims to support sa_resethand but it does nothing
|
||||
// this can be tested, since it clears the bit from flags as well
|
||||
if (flags & SA_RESETHAND) {
|
||||
|
@ -521,6 +523,13 @@ privileged void __sigenter_xnu(int sig, struct siginfo_xnu *xnuinfo,
|
|||
__sighandflags[sig] = 0;
|
||||
__sighandrvas[sig] = 0;
|
||||
}
|
||||
|
||||
// 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 (xnuctx && sig == SIGTRAP)
|
||||
xnuctx->uc_mcontext->__ss.__pc += 4;
|
||||
|
||||
#endif
|
||||
|
||||
if (~flags & SA_SIGINFO) {
|
||||
|
|
|
@ -1,13 +1,14 @@
|
|||
#ifndef COSMOPOLITAN_LIBC_CALLS_STRUCT_UCONTEXT_INTERNAL_H_
|
||||
#define COSMOPOLITAN_LIBC_CALLS_STRUCT_UCONTEXT_INTERNAL_H_
|
||||
#include "libc/calls/ucontext.h"
|
||||
#include "libc/nt/struct/context.h"
|
||||
COSMOPOLITAN_C_START_
|
||||
|
||||
#ifdef __x86_64__
|
||||
#define PC rip
|
||||
#define SP rsp
|
||||
#define BP rbp
|
||||
#define RES0 rax
|
||||
#define RES1 rdx
|
||||
#define ARG0 rdi
|
||||
#define ARG1 rsi
|
||||
#define ARG2 rdx
|
||||
|
@ -18,6 +19,8 @@ COSMOPOLITAN_C_START_
|
|||
#define PC pc
|
||||
#define SP sp
|
||||
#define BP regs[29]
|
||||
#define RES0 regs[0]
|
||||
#define RES1 regs[1]
|
||||
#define ARG0 regs[0]
|
||||
#define ARG1 regs[1]
|
||||
#define ARG2 regs[2]
|
||||
|
@ -28,8 +31,5 @@ COSMOPOLITAN_C_START_
|
|||
#error "unsupported architecture"
|
||||
#endif
|
||||
|
||||
void _ntcontext2linux(struct ucontext *, const struct NtContext *);
|
||||
void _ntlinux2context(struct NtContext *, const ucontext_t *);
|
||||
|
||||
COSMOPOLITAN_C_END_
|
||||
#endif /* COSMOPOLITAN_LIBC_CALLS_STRUCT_UCONTEXT_INTERNAL_H_ */
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue