From e0d5bd7c68d862206318e5b277feea9cb11b3995 Mon Sep 17 00:00:00 2001 From: dzwdz Date: Wed, 22 Sep 2021 20:25:09 +0200 Subject: kfree: check for a magic value before freeing to detect invalid frees --- src/kernel/mem/alloc.c | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/src/kernel/mem/alloc.c b/src/kernel/mem/alloc.c index 51576bb..59898cb 100644 --- a/src/kernel/mem/alloc.c +++ b/src/kernel/mem/alloc.c @@ -3,6 +3,8 @@ #include #include +#define MALLOC_MAGIC 0xACAB1312 + static void *highest_page; static int malloc_balance = 0; @@ -36,12 +38,22 @@ void page_free(void *first, size_t pages) { void *kmalloc(size_t len) { - malloc_balance++; + void *addr; + len += sizeof(uint32_t); // add space for MALLOC_MAGIC // extremely inefficient, but this is only temporary anyways - return page_alloc(len / PAGE_SIZE + 1); + addr = page_alloc(len / PAGE_SIZE + 1); + *(uint32_t*)addr = MALLOC_MAGIC; + malloc_balance++; + return addr + sizeof(uint32_t); } void kfree(void *ptr) { if (ptr == NULL) return; + if (((uint32_t*)ptr)[-1] != MALLOC_MAGIC) { + // TODO add some kind of separate system log + tty_const("WARNING kfree() didn't find MALLOC_MAGIC, ptr == "); + _tty_var(ptr); + tty_const(" "); + } malloc_balance--; } -- cgit v1.2.3