diff options
author | dzwdz | 2021-09-22 20:25:09 +0200 |
---|---|---|
committer | dzwdz | 2021-09-22 20:25:09 +0200 |
commit | e0d5bd7c68d862206318e5b277feea9cb11b3995 (patch) | |
tree | 73f625f28d5a327058e8435b9205ca67f53ac02d /src/kernel | |
parent | 521e2bcedb8eb5d13f4c527275c81d3a0f31f2b6 (diff) |
kfree: check for a magic value before freeing to detect invalid frees
Diffstat (limited to 'src/kernel')
-rw-r--r-- | src/kernel/mem/alloc.c | 16 |
1 files 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 <kernel/util.h> #include <stdint.h> +#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--; } |