Skip to content

Commit

Permalink
Fix heap allocator bug
Browse files Browse the repository at this point in the history
The implementation of `GlobalAlloc` for `Heap` assumed that all memory
under `INITIAL_HEAP_END_ADDR` was allocated using the initial allocator,
but this isn't true.

`MultipleHeaps` allocates large objects using mapped pages leading to
objects allocated in the lower half of memory. When deallocating these
objects, `Heap` tried to deallocate them using the initial allocator
rather than `MultipleHeaps`.

Signed-off-by: Klimenty Tsoutsman <klim@tsoutsman.com>
  • Loading branch information
tsoutsman committed Oct 30, 2023
1 parent c025bd1 commit 57a4c50
Showing 1 changed file with 1 addition and 1 deletion.
2 changes: 1 addition & 1 deletion kernel/heap/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ unsafe impl GlobalAlloc for Heap {
}

unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) {
if (ptr as usize) < INITIAL_HEAP_END_ADDR {
if KERNEL_HEAP_START <= (ptr as usize) && (ptr as usize) < INITIAL_HEAP_END_ADDR {
self.initial_allocator.lock().deallocate(ptr, layout);
}
else {
Expand Down

0 comments on commit 57a4c50

Please sign in to comment.