From db49c933fe34a54947dbc7df381fa1700dc1e74f Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Fri, 20 Mar 2026 23:35:38 +0100 Subject: First commit MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mattias Andrée --- stackoverflow-recovery.c | 75 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 stackoverflow-recovery.c (limited to 'stackoverflow-recovery.c') diff --git a/stackoverflow-recovery.c b/stackoverflow-recovery.c new file mode 100644 index 0000000..436c8c7 --- /dev/null +++ b/stackoverflow-recovery.c @@ -0,0 +1,75 @@ +/* See LICENSE file for copyright and license details. */ +#include +#include +#include +#include +#include +#include +#include + + +unsigned volatile just_one = 1; + +static jmp_buf jmpenv; + + +static void +sigsegv(int signo) +{ + (void) signo; + + siglongjmp(jmpenv, 1); +} + + +static unsigned +overflow2(size_t depth) +{ + if (!depth) + return just_one; + depth -= 1u; + return overflow2(depth) + overflow2(depth); +} + + +static unsigned +overflow(void) +{ + return overflow2(SIZE_MAX); +} + + +int +main(void) +{ + volatile unsigned sum = 0; + volatile int i; + struct sigaction sa; + stack_t ss; + + ss.ss_sp = malloc((size_t)SIGSTKSZ); + if (!ss.ss_sp) + return 1; + ss.ss_size = (size_t)SIGSTKSZ; + ss.ss_flags = 0; + if (sigaltstack(&ss, NULL)) + return 2; + + memset(&sa, 0, sizeof(sa)); + sa.sa_flags = SA_ONSTACK; + sa.sa_handler = &sigsegv; + sigfillset(&sa.sa_mask); + sigaction(SIGSEGV, &sa, NULL); + + for (i = 1; i <= 10; i++) { + if (!sigsetjmp(jmpenv, 1)) { + printf("%i: before overflow dereference\n", i); + sum += overflow(); + } else { + printf("%i: after overflow dereference\n", i); + } + } + + fflush(stdout); + return (int)sum; +} -- cgit v1.2.3-70-g09d2