From eb725ec57f9389834802f3d51fa58cd722f8bf79 Mon Sep 17 00:00:00 2001 From: Sean Date: Thu, 3 Sep 2020 13:20:36 +0800 Subject: [PATCH] xous: carton: leak memory when converting into_message() When converting into a message, the associated memory should be leaked and not freed when the Carton is dropped. This is because the Message can outlive the MemoryMessage, and the Carton is then immediately dropped. Signed-off-by: Sean --- xous-rs/src/carton.rs | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/xous-rs/src/carton.rs b/xous-rs/src/carton.rs index a00b21371..7d2b2696c 100644 --- a/xous-rs/src/carton.rs +++ b/xous-rs/src/carton.rs @@ -8,6 +8,7 @@ pub struct Carton<'a> { range: MemoryRange, valid: MemoryRange, slice: &'a [u8], + should_drop: bool, } impl<'a> Carton<'a> { @@ -36,10 +37,14 @@ impl<'a> Carton<'a> { range: new_mem, slice: unsafe { core::slice::from_raw_parts_mut(new_mem.as_mut_ptr(), bytes.len()) }, valid, + should_drop: true, } } - pub fn into_message(self, id: usize) -> MemoryMessage { + pub fn into_message(mut self, id: usize) -> MemoryMessage { + // Leak the memory buffer, since it will be taken care of + // when the MemoryMessage is dropped. + self.should_drop = false; MemoryMessage { id, buf: self.valid, @@ -86,6 +91,8 @@ impl<'a> AsRef<[u8]> for Carton<'a> { impl<'a> Drop for Carton<'a> { fn drop(&mut self) { - crate::unmap_memory(self.range).unwrap(); + if self.should_drop { + crate::unmap_memory(self.range).unwrap(); + } } }