From 3e31ba1b90dbb2bbfce6babae4e01c02016bc064 Mon Sep 17 00:00:00 2001 From: Alex Saveau Date: Sun, 30 Jun 2024 22:47:33 -0700 Subject: [PATCH] Fix search segfaulting on empty bucket files Signed-off-by: Alex Saveau --- client-sdk/src/ring_reader.rs | 2 +- ringboard/src/ring.rs | 42 ++++++++++++++++------------------- 2 files changed, 20 insertions(+), 24 deletions(-) diff --git a/client-sdk/src/ring_reader.rs b/client-sdk/src/ring_reader.rs index 44f7cf6..9471a32 100644 --- a/client-sdk/src/ring_reader.rs +++ b/client-sdk/src/ring_reader.rs @@ -477,7 +477,7 @@ impl EntryReader { let mut maps = ArrayVec::new_const(); for (i, fd) in buckets.into_iter().enumerate() { maps.push( - Mmap::new(fd, usize::try_from(lengths[i]).unwrap().max(4096)) + Mmap::new(fd, usize::try_from(lengths[i]).unwrap()) .map_io_err(|| "Failed to map memory.")?, ); } diff --git a/ringboard/src/ring.rs b/ringboard/src/ring.rs index 330a28e..40853ca 100644 --- a/ringboard/src/ring.rs +++ b/ringboard/src/ring.rs @@ -114,7 +114,8 @@ impl BucketEntry { #[derive(Debug)] pub struct Mmap { ptr: NonNull, - len: usize, + requested_len: usize, + backing_len: usize, } unsafe impl Send for Mmap {} @@ -127,18 +128,12 @@ impl Mmap { } pub fn new(fd: Fd, len: usize) -> rustix::io::Result { - if len == 0 { - return Ok(Self { - ptr: NonNull::dangling(), - len, - }); - } - + let backing_len = len.max(4096); Ok(Self { ptr: unsafe { NonNull::new_unchecked(mmap( ptr::null_mut(), - len, + backing_len, ProtFlags::READ, MapFlags::SHARED_VALIDATE, fd, @@ -146,18 +141,12 @@ impl Mmap { )?) } .cast(), - len, + requested_len: len, + backing_len, }) } pub fn new_anon(len: usize) -> rustix::io::Result { - if len == 0 { - return Ok(Self { - ptr: NonNull::dangling(), - len, - }); - } - Ok(Self { ptr: unsafe { NonNull::new_unchecked(mmap_anonymous( @@ -168,23 +157,30 @@ impl Mmap { )?) } .cast(), - len, + requested_len: len, + backing_len: len, }) } pub fn remap(&mut self, len: usize) -> rustix::io::Result<()> { + if len >= self.requested_len && len <= self.backing_len { + self.requested_len = len; + return Ok(()); + } + self.ptr = unsafe { NonNull::new_unchecked( mremap( self.ptr.as_ptr().cast(), - self.len, + self.backing_len, len, MremapFlags::MAYMOVE, )? .cast(), ) }; - self.len = len; + self.requested_len = len; + self.backing_len = len; Ok(()) } @@ -195,12 +191,12 @@ impl Mmap { #[must_use] pub const fn len(&self) -> usize { - self.len + self.requested_len } #[must_use] pub const fn is_empty(&self) -> bool { - self.len == 0 + self.requested_len == 0 } } @@ -220,7 +216,7 @@ impl AsRef<[u8]> for Mmap { impl Drop for Mmap { fn drop(&mut self) { - let _ = unsafe { munmap(self.ptr.as_ptr().cast(), self.len) }; + let _ = unsafe { munmap(self.ptr.as_ptr().cast(), self.backing_len) }; } }