aboutsummaryrefslogtreecommitdiff
path: root/arch/x86_64/src/memory
diff options
context:
space:
mode:
authorMatteo Gmür <matteo.gmuer1@ost.ch>2024-12-02 11:14:43 +0000
committerMatteo Gmür <matteo.gmuer1@ost.ch>2024-12-02 11:14:43 +0000
commitaa4de534ec7bf0b609aff032c4649484aa49823c (patch)
tree45a53137b0f7f67fe4db05091b9d2a54f1728bb7 /arch/x86_64/src/memory
parenta5e5eabd32872f81a7190589aa648dc0e1963888 (diff)
downloadteachos-aa4de534ec7bf0b609aff032c4649484aa49823c.tar.xz
teachos-aa4de534ec7bf0b609aff032c4649484aa49823c.zip
Add check to detect double free in linked list allocator
Diffstat (limited to 'arch/x86_64/src/memory')
-rw-r--r--arch/x86_64/src/memory/heap/linked_list_allocator.cpp7
1 files changed, 7 insertions, 0 deletions
diff --git a/arch/x86_64/src/memory/heap/linked_list_allocator.cpp b/arch/x86_64/src/memory/heap/linked_list_allocator.cpp
index 706f43e..f596f27 100644
--- a/arch/x86_64/src/memory/heap/linked_list_allocator.cpp
+++ b/arch/x86_64/src/memory/heap/linked_list_allocator.cpp
@@ -126,6 +126,13 @@ namespace teachos::arch::memory::heap
return;
}
+ // Check if the block we want to deallocate is contained in the previous block, because if it is it can only mean
+ // that the block has already been deallocated and we therefore attempted a double free.
+ exception_handling::assert(previous_block == nullptr ||
+ start_address >=
+ (reinterpret_cast<std::size_t>(previous_block) + previous_block->size),
+ "[Linked List Allocator] Attempted double free detected");
+
auto const new_block = new (pointer) memory_block(block_size, next_block);
// If we want to deallocate the first block that is before any other free block, then there exists no previous free
// block (nullptr). Therefore we have to overwrite the first block instead of overwriting its