From 0c7d1486b5ff7150e3c29378c7e14f6f8e2ebd66 Mon Sep 17 00:00:00 2001 From: Jonas 'Sortie' Termansen Date: Fri, 7 Dec 2012 13:11:26 +0100 Subject: [PATCH] Add PARANOIA level 3 and better malloc/free error reporting. --- libc/heap.cpp | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/libc/heap.cpp b/libc/heap.cpp index 37191807..5a2de125 100644 --- a/libc/heap.cpp +++ b/libc/heap.cpp @@ -29,6 +29,7 @@ #endif #ifndef SORTIX_KERNEL +#include #include #include #endif @@ -536,7 +537,7 @@ extern "C" void* malloc(size_t size) chunk->magic = MAGIC; chunk->GetTrailer()->magic = MAGIC; - #if 2 <= PARANOIA + #if 3 <= PARANOIA assert(ValidateHeap()); #endif @@ -570,7 +571,7 @@ extern "C" void* malloc(size_t size) chunk->magic = MAGIC; trailer->magic = MAGIC; - #if 2 <= PARANOIA + #if 3 <= PARANOIA assert(ValidateHeap()); #endif @@ -667,6 +668,20 @@ extern "C" void free(void* addr) if ( !addr) { return; } Chunk* chunk = (Chunk*) ((addr_t) addr - sizeof(Chunk)); +#ifndef SORTIX_KERNEL + if ( !IsGoodHeapPointer(addr, 1) || + !IsGoodHeapPointer(chunk, sizeof(*chunk)) ) + { + error(0, 0, "attempted to free(3) non-heap pointer: 0x%zx", addr); + abort(); + } + if ( !chunk->IsUsed() ) + { + error(0, 0, "attempted to free(3) area that doesn't appear to be " + "allocated: 0x%zx + 0x%zx", addr); + abort(); + } +#endif assert(chunk->IsUsed()); assert(chunk->IsSane()); @@ -688,7 +703,7 @@ extern "C" void free(void* addr) InsertChunk(chunk); - #if 2 <= PARANOIA + #if 3 <= PARANOIA assert(ValidateHeap()); #endif }