1
0
Fork 0
mirror of https://gitlab.com/sortix/sortix.git synced 2023-02-13 20:55:38 -05:00

Add PARANOIA level 3 and better malloc/free error reporting.

This commit is contained in:
Jonas 'Sortie' Termansen 2012-12-07 13:11:26 +01:00
parent 6afca10c25
commit 0c7d1486b5

View file

@ -29,6 +29,7 @@
#endif #endif
#ifndef SORTIX_KERNEL #ifndef SORTIX_KERNEL
#include <error.h>
#include <stdio.h> #include <stdio.h>
#include <unistd.h> #include <unistd.h>
#endif #endif
@ -536,7 +537,7 @@ extern "C" void* malloc(size_t size)
chunk->magic = MAGIC; chunk->magic = MAGIC;
chunk->GetTrailer()->magic = MAGIC; chunk->GetTrailer()->magic = MAGIC;
#if 2 <= PARANOIA #if 3 <= PARANOIA
assert(ValidateHeap()); assert(ValidateHeap());
#endif #endif
@ -570,7 +571,7 @@ extern "C" void* malloc(size_t size)
chunk->magic = MAGIC; chunk->magic = MAGIC;
trailer->magic = MAGIC; trailer->magic = MAGIC;
#if 2 <= PARANOIA #if 3 <= PARANOIA
assert(ValidateHeap()); assert(ValidateHeap());
#endif #endif
@ -667,6 +668,20 @@ extern "C" void free(void* addr)
if ( !addr) { return; } if ( !addr) { return; }
Chunk* chunk = (Chunk*) ((addr_t) addr - sizeof(Chunk)); 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->IsUsed());
assert(chunk->IsSane()); assert(chunk->IsSane());
@ -688,7 +703,7 @@ extern "C" void free(void* addr)
InsertChunk(chunk); InsertChunk(chunk);
#if 2 <= PARANOIA #if 3 <= PARANOIA
assert(ValidateHeap()); assert(ValidateHeap());
#endif #endif
} }