diff options
author | Jakob Kaivo <jkk@ung.org> | 2020-06-30 15:21:49 -0400 |
---|---|---|
committer | Jakob Kaivo <jkk@ung.org> | 2020-06-30 15:21:49 -0400 |
commit | 62961fe31686bb1b4c071e128047a49f321492e9 (patch) | |
tree | 6abe58cf3eaf77ec800fc541f2660b54df09558a | |
parent | c4a294e498cd456c5421b3ffebdd64b1e15a062d (diff) |
add a signal handler to eventually differentiate between overflow, underflow, use-after-free, and double-free
-rw-r--r-- | src/mapalloc.c | 22 |
1 files changed, 20 insertions, 2 deletions
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 <fcntl.h> #include <limits.h> +#include <signal.h> #include <stdint.h> #include <stdio.h> #include <stdlib.h> @@ -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); |