mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-05-23 22:02:27 +00:00
Improve stack overflow recovery
It's now possible to use sigaltstack() to recover from stack overflows on Windows. Several bugs in sigaltstack() have been fixed, for all our supported platforms. There's a newer better example showing how to use this, along with three independent unit tests just to further showcase the various techniques.
This commit is contained in:
parent
1694edf85c
commit
4631d34d0d
24 changed files with 590 additions and 274 deletions
|
@ -255,58 +255,6 @@ TEST(pthread_cleanup, pthread_normal) {
|
|||
ASSERT_TRUE(g_cleanup2);
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
jmp_buf recover;
|
||||
volatile bool smashed_stack;
|
||||
|
||||
void CrashHandler(int sig, siginfo_t *si, void *ctx) {
|
||||
kprintf("kprintf avoids overflowing %G %p\n", si->si_signo, si->si_addr);
|
||||
smashed_stack = true;
|
||||
ASSERT_TRUE(__is_stack_overflow(si, ctx));
|
||||
longjmp(recover, 123);
|
||||
}
|
||||
|
||||
int StackOverflow(int f(), int n) {
|
||||
if (n < INT_MAX) {
|
||||
return f(f, n + 1) - 1;
|
||||
} else {
|
||||
return INT_MAX;
|
||||
}
|
||||
}
|
||||
|
||||
int (*pStackOverflow)(int (*)(), int) = StackOverflow;
|
||||
|
||||
void *MyPosixThread(void *arg) {
|
||||
int jumpcode;
|
||||
struct sigaction sa, o1, o2;
|
||||
struct sigaltstack ss;
|
||||
ss.ss_flags = 0;
|
||||
ss.ss_size = sysconf(_SC_MINSIGSTKSZ) + 4096;
|
||||
ss.ss_sp = gc(malloc(ss.ss_size));
|
||||
ASSERT_SYS(0, 0, sigaltstack(&ss, 0));
|
||||
sa.sa_flags = SA_SIGINFO | SA_ONSTACK; // <-- important
|
||||
sigemptyset(&sa.sa_mask);
|
||||
sa.sa_sigaction = CrashHandler;
|
||||
sigaction(SIGBUS, &sa, &o1);
|
||||
sigaction(SIGSEGV, &sa, &o2);
|
||||
if (!(jumpcode = setjmp(recover))) {
|
||||
exit(pStackOverflow(pStackOverflow, 0));
|
||||
}
|
||||
ASSERT_EQ(123, jumpcode);
|
||||
sigaction(SIGSEGV, &o2, 0);
|
||||
sigaction(SIGBUS, &o1, 0);
|
||||
return 0;
|
||||
}
|
||||
|
||||
TEST(cosmo, altstack_thread) {
|
||||
pthread_t th;
|
||||
if (IsWindows()) return;
|
||||
pthread_create(&th, 0, MyPosixThread, 0);
|
||||
pthread_join(th, 0);
|
||||
ASSERT_TRUE(smashed_stack);
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// BENCHMARKS
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue