From 7137fc03b2258fcefca13fdfe04cdcf08d9fe7fb Mon Sep 17 00:00:00 2001 From: Augustin Cavalier Date: Tue, 7 Jan 2025 16:27:24 -0500 Subject: [PATCH] bootloader heap: Actually implement DEBUG_ALLOCATIONS. The old define was really "check heap integrity", so it's split off. In the userland test harness, this takes the total time spent in the allocator from around 1ms to 14ms for all the allocations used during a boot with "nightly" profile, so it seems acceptable to enable it under KDEBUG. Tested with both BIOS and EFI bootloaders, still works. --- headers/private/kernel/util/SimpleAllocator.h | 9 ++++++++- src/system/boot/loader/heap.cpp | 7 ++++++- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/headers/private/kernel/util/SimpleAllocator.h b/headers/private/kernel/util/SimpleAllocator.h index df2a5be6ac..1221c56974 100644 --- a/headers/private/kernel/util/SimpleAllocator.h +++ b/headers/private/kernel/util/SimpleAllocator.h @@ -266,6 +266,9 @@ public: #ifdef DEBUG_MAX_HEAP_USAGE fMaxHeapUsage = std::max(fMaxHeapUsage, fMaxHeapSize - fAvailable); #endif +#ifdef DEBUG_ALLOCATIONS + memset(allocated, 0xcc, chunk->Size()); +#endif return allocated; } @@ -306,7 +309,7 @@ public: FreeChunk* freedChunk = FreeChunk::SetToAllocated(allocated); -#ifdef DEBUG_ALLOCATIONS +#ifdef DEBUG_VALIDATE_HEAP_ON_FREE if (freedChunk->Size() > (fMaxHeapSize - fAvailable)) { panic("freed chunk %p clobbered (%#zx)!\n", freedChunk, freedChunk->Size()); @@ -321,6 +324,10 @@ public: } } #endif +#ifdef DEBUG_ALLOCATIONS + for (uint32 i = 0; i < (freedChunk->Size() / 4); i++) + ((uint32*)allocated)[i] = 0xdeadbeef; +#endif // try to join the new free chunk with an existing one // it may be joined with up to two chunks diff --git a/src/system/boot/loader/heap.cpp b/src/system/boot/loader/heap.cpp index 9fbd1e8436..043be1395e 100644 --- a/src/system/boot/loader/heap.cpp +++ b/src/system/boot/loader/heap.cpp @@ -16,11 +16,16 @@ #include +#if KDEBUG #define DEBUG_ALLOCATIONS - // if defined, freed memory is filled with 0xcc + // if defined, allocated memory is filled with 0xcc and freed memory with 0xdeadbeef #define DEBUG_MAX_HEAP_USAGE // if defined, the maximum heap usage is determined and printed before // entering the kernel +#define DEBUG_VALIDATE_HEAP_ON_FREE + // if defined, the heap integrity is checked on every free + // (must have DEBUG_MAX_HEAP_USAGE defined also) +#endif #include