From bc136caef26bc1399cb03b9bee03b648f871166e Mon Sep 17 00:00:00 2001 From: John Hodge Date: Sun, 9 Feb 2014 09:36:19 +0800 Subject: [PATCH] Kernel/heap - Enable validation on every malloc --- KernelLand/Kernel/heap.c | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/KernelLand/Kernel/heap.c b/KernelLand/Kernel/heap.c index ccb5db1b..20729d8a 100755 --- a/KernelLand/Kernel/heap.c +++ b/KernelLand/Kernel/heap.c @@ -13,6 +13,7 @@ #define WARNINGS 1 // Warn and dump on heap errors #define DEBUG_TRACE 0 // Enable tracing of allocations #define VERBOSE_DUMP 0 // Set to 1 to enable a verbose dump when heap errors are encountered +#define VALIDATE_ON_ALLOC 1 // Set to 1 to enable validation of the heap on all malloc() calls // === CONSTANTS === #define HEAP_INIT_SIZE 0x8000 // 32 KiB @@ -178,6 +179,10 @@ void *Heap_Allocate(const char *File, int Line, size_t __Bytes) return NULL; // TODO: Return a known un-mapped range. // return INVLPTR; } + + #if VALIDATE_ON_ALLOC + Heap_Dump(0); + #endif // Get required size #if POW2_SIZES @@ -358,6 +363,7 @@ void Heap_Deallocate(const char *File, int Line, void *Ptr) Log_Warning("Heap", "free - Passed a freed block (%p) by %s:%i (was freed by %s:%i)", head, File, Line, head->File, head->Line); + Proc_PrintBacktrace(); return; } if(head->Magic != MAGIC_USED) { @@ -626,7 +632,7 @@ void Heap_Dump(int bVerbose) in_heap_dump = 0; return ; } - + // If not verbose, we need to dump the failing block if( !bVerbose ) { @@ -640,8 +646,7 @@ void Heap_Dump(int bVerbose) } Log_Log("Heap", ""); } - - + badHead = head; // Work backwards @@ -690,7 +695,7 @@ void Heap_Dump(int bVerbose) Log_Debug("Heap", "head=%p", head); } - Panic("Heap_Dump - Heap is corrupted, kernel panic!"); + Panic("Heap_Dump - Heap is corrupted, kernel panic! (%p)", badHead); } void Heap_Stats(void) -- 2.20.1