From d3e4f175cd0c91aaea1c5dc8f6d466a3fe480163 Mon Sep 17 00:00:00 2001 From: Zhang Junyang Date: Thu, 8 May 2025 16:02:15 +0800 Subject: [PATCH] Fix the memory leak on large heap slot dealloc --- ostd/src/mm/frame/segment.rs | 2 ++ ostd/src/mm/heap/slot.rs | 3 +-- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/ostd/src/mm/frame/segment.rs b/ostd/src/mm/frame/segment.rs index d718f1846..915c0e623 100644 --- a/ostd/src/mm/frame/segment.rs +++ b/ostd/src/mm/frame/segment.rs @@ -116,6 +116,8 @@ impl Segment { /// It could be manually forgotten by [`core::mem::forget`], /// [`ManuallyDrop`], or [`Self::into_raw`]. pub(crate) unsafe fn from_raw(range: Range) -> Self { + debug_assert_eq!(range.start % PAGE_SIZE, 0); + debug_assert_eq!(range.end % PAGE_SIZE, 0); Self { range, _marker: core::marker::PhantomData, diff --git a/ostd/src/mm/heap/slot.rs b/ostd/src/mm/heap/slot.rs index 1893ee9cd..fee85412a 100644 --- a/ostd/src/mm/heap/slot.rs +++ b/ostd/src/mm/heap/slot.rs @@ -116,8 +116,7 @@ impl HeapSlot { debug_assert_eq!(size % PAGE_SIZE, 0); debug_assert_eq!(self.paddr() % PAGE_SIZE, 0); - let nframes = size / PAGE_SIZE; - let range = self.paddr()..self.paddr() + nframes; + let range = self.paddr()..self.paddr() + size; // SAFETY: The segment was once forgotten when allocated. drop(unsafe { Segment::::from_raw(range) });