From 62961fe31686bb1b4c071e128047a49f321492e9 Mon Sep 17 00:00:00 2001 From: Jakob Kaivo Date: Tue, 30 Jun 2020 15:21:49 -0400 Subject: add a signal handler to eventually differentiate between overflow, underflow, use-after-free, and double-free --- src/mapalloc.c | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/mapalloc.c b/src/mapalloc.c index 6d49f9d..4848f94 100644 --- a/src/mapalloc.c +++ b/src/mapalloc.c @@ -1,6 +1,7 @@ #define _POSIX_C_SOURCE 200809L #include #include +#include #include #include #include @@ -49,6 +50,23 @@ static void *page_alloc(size_t npages) return pages; } +static void map_signal_action(int sig, siginfo_t *si, void *addr) +{ + (void)sig; + fprintf(stderr, "error accessing %p\n", si->si_addr); + _exit(127 + SIGSEGV); +} + +static void set_signal_handler(void) +{ + struct sigaction sa = { + .sa_flags = SA_SIGINFO, + .sa_sigaction = map_signal_action, + }; + sigemptyset(&sa.sa_mask); + sigaction(SIGSEGV, &sa, NULL); +} + static struct bucket *get_bucket(void *ptr, int allocate) { static uintptr_t *trie_top = NULL; @@ -57,14 +75,14 @@ static struct bucket *get_bucket(void *ptr, int allocate) memset(trie_top, 0, PAGESIZE); } - printf("- finding bucket %p (%d)\n", ptr, allocate); + set_signal_handler(); + uintptr_t *trie = trie_top; uintptr_t addr = (uintptr_t)ptr; for (size_t i = 0; i < sizeof(addr); i++) { uintptr_t next = (addr >> ((sizeof(addr) - i) * CHAR_BIT)) & UCHAR_MAX; - printf("-- %02zx\n", next); if (trie[next] == 0) { if (allocate) { uintptr_t *newtrie = page_alloc(1); -- cgit v1.2.1