From 0cb4afef6da5488a128e5aaece435e9aa5f5797e Mon Sep 17 00:00:00 2001 From: Anton Kling Date: Fri, 10 Nov 2023 15:19:49 +0100 Subject: Kernel/Memory: Fill new allocations with random data. This should make it easier to spot uninitalized memory being used. --- kernel/kmalloc.c | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-) (limited to 'kernel/kmalloc.c') diff --git a/kernel/kmalloc.c b/kernel/kmalloc.c index 34c466d..75bc74b 100644 --- a/kernel/kmalloc.c +++ b/kernel/kmalloc.c @@ -2,7 +2,8 @@ #include #include #include -#define NEW_ALLOC_SIZE 0x30000 +#include +#define NEW_ALLOC_SIZE 0x20000 #define IS_FREE (1 << 0) #define IS_FINAL (1 << 1) @@ -142,6 +143,7 @@ void *kmalloc(size_t s) { free_entry->flags = 0; free_entry->n = new_entry; free_entry->magic = 0xdde51ab9410268b1; + get_random((void *)rc, s); return rc; } @@ -215,16 +217,15 @@ void *kcalloc(size_t nelem, size_t elsize) { } void kfree(void *p) { - /* -if (!p) -return; -// FIXME: This assumes that p is at the start of a allocated area. -// Could this be avoided in a simple way? -MallocHeader *h = (MallocHeader *)((uintptr_t)p - sizeof(MallocHeader)); -assert(h->magic == 0xdde51ab9410268b1); -if (h->flags & IS_FREE) -return; - -h->flags |= IS_FREE; -merge_headers(h);*/ + if (!p) + return; + // FIXME: This assumes that p is at the start of a allocated area. + // Could this be avoided in a simple way? + MallocHeader *h = (MallocHeader *)((uintptr_t)p - sizeof(MallocHeader)); + assert(h->magic == 0xdde51ab9410268b1); + if (h->flags & IS_FREE) + return; + + h->flags |= IS_FREE; + merge_headers(h); } -- cgit v1.2.3