Skip to content

Commit

Permalink
Merge branch 'fix/ub-pass-miri-tests' of https://github.com/xgroleau/…
Browse files Browse the repository at this point in the history
…bbqueue into main
  • Loading branch information
Sympatron committed Dec 10, 2024
2 parents 443e78f + 12a61e1 commit 8468029
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 27 deletions.
57 changes: 34 additions & 23 deletions core/src/bbbuffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use core::{
ops::{Deref, DerefMut},
ptr::NonNull,
result::Result as CoreResult,
slice::from_raw_parts_mut,
slice::{from_raw_parts, from_raw_parts_mut},
sync::atomic::{
AtomicBool, AtomicUsize,
Ordering::{AcqRel, Acquire, Release},
Expand Down Expand Up @@ -404,9 +404,10 @@ impl<'a, const N: usize> Producer<'a, N> {
let grant_slice = unsafe { from_raw_parts_mut(start_of_buf_ptr.add(start), sz) };

Ok(GrantW {
buf: grant_slice,
buf: grant_slice.into(),
bbq: self.bbq,
to_commit: 0,
phatom: PhantomData,
})
}

Expand Down Expand Up @@ -507,9 +508,10 @@ impl<'a, const N: usize> Producer<'a, N> {
let grant_slice = unsafe { from_raw_parts_mut(start_of_buf_ptr.add(start), sz) };

Ok(GrantW {
buf: grant_slice,
buf: grant_slice.into(),
bbq: self.bbq,
to_commit: 0,
phatom: PhantomData,
})
}
}
Expand Down Expand Up @@ -598,9 +600,10 @@ impl<'a, const N: usize> Consumer<'a, N> {
let grant_slice = unsafe { from_raw_parts_mut(start_of_buf_ptr.add(read), sz) };

Ok(GrantR {
buf: grant_slice,
buf: grant_slice.into(),
bbq: self.bbq,
to_release: 0,
phatom: PhantomData,
})
}

Expand Down Expand Up @@ -651,10 +654,11 @@ impl<'a, const N: usize> Consumer<'a, N> {
let grant_slice2 = unsafe { from_raw_parts_mut(start_of_buf_ptr, sz2) };

Ok(SplitGrantR {
buf1: grant_slice1,
buf2: grant_slice2,
buf1: grant_slice1.into(),
buf2: grant_slice2.into(),
bbq: self.bbq,
to_release: 0,
phatom: PhantomData,
})
}
}
Expand Down Expand Up @@ -697,9 +701,10 @@ impl<const N: usize> BBBuffer<N> {
/// without committing it takes a short critical section,
#[derive(Debug, PartialEq)]
pub struct GrantW<'a, const N: usize> {
pub(crate) buf: &'a mut [u8],
pub(crate) buf: NonNull<[u8]>,
bbq: NonNull<BBBuffer<N>>,
pub(crate) to_commit: usize,
phatom: PhantomData<&'a mut [u8]>,
}

unsafe impl<'a, const N: usize> Send for GrantW<'a, N> {}
Expand All @@ -718,20 +723,22 @@ unsafe impl<'a, const N: usize> Send for GrantW<'a, N> {}
/// without releasing it takes a short critical section,
#[derive(Debug, PartialEq)]
pub struct GrantR<'a, const N: usize> {
pub(crate) buf: &'a mut [u8],
pub(crate) buf: NonNull<[u8]>,
bbq: NonNull<BBBuffer<N>>,
pub(crate) to_release: usize,
phatom: PhantomData<&'a mut [u8]>,
}

/// A structure representing up to two contiguous regions of memory that
/// may be read from, and potentially "released" (or cleared)
/// from the queue
#[derive(Debug, PartialEq)]
pub struct SplitGrantR<'a, const N: usize> {
pub(crate) buf1: &'a mut [u8],
pub(crate) buf2: &'a mut [u8],
pub(crate) buf1: NonNull<[u8]>,
pub(crate) buf2: NonNull<[u8]>,
bbq: NonNull<BBBuffer<N>>,
pub(crate) to_release: usize,
phatom: PhantomData<&'a mut [u8]>,
}

unsafe impl<'a, const N: usize> Send for GrantR<'a, N> {}
Expand Down Expand Up @@ -777,7 +784,7 @@ impl<'a, const N: usize> GrantW<'a, N> {
/// # }
/// ```
pub fn buf(&mut self) -> &mut [u8] {
self.buf
unsafe { from_raw_parts_mut(self.buf.as_ptr() as *mut u8, self.buf.len()) }
}

/// Sometimes, it's not possible for the lifetimes to check out. For example,
Expand All @@ -794,7 +801,7 @@ impl<'a, const N: usize> GrantW<'a, N> {
/// Additionally, you must ensure that a separate reference to this data is not created
/// to this data, e.g. using `DerefMut` or the `buf()` method of this grant.
pub unsafe fn as_static_mut_buf(&mut self) -> &'static mut [u8] {
transmute::<&mut [u8], &'static mut [u8]>(self.buf)
transmute::<&mut [u8], &'static mut [u8]>(self.buf())
}

#[inline(always)]
Expand Down Expand Up @@ -875,9 +882,9 @@ impl<'a, const N: usize> GrantR<'a, N> {

pub(crate) fn shrink(&mut self, len: usize) {
let mut new_buf: &mut [u8] = &mut [];
core::mem::swap(&mut self.buf, &mut new_buf);
core::mem::swap(&mut self.buf_mut(), &mut new_buf);
let (new, _) = new_buf.split_at_mut(len);
self.buf = new;
self.buf = new.into();
}

/// Obtain access to the inner buffer for reading
Expand Down Expand Up @@ -910,15 +917,15 @@ impl<'a, const N: usize> GrantR<'a, N> {
/// # }
/// ```
pub fn buf(&self) -> &[u8] {
self.buf
unsafe { from_raw_parts(self.buf.as_ptr() as *const u8, self.buf.len()) }
}

/// Obtain mutable access to the read grant
///
/// This is useful if you are performing in-place operations
/// on an incoming packet, such as decryption
pub fn buf_mut(&mut self) -> &mut [u8] {
self.buf
unsafe { from_raw_parts_mut(self.buf.as_ptr() as *mut u8, self.buf.len()) }
}

/// Sometimes, it's not possible for the lifetimes to check out. For example,
Expand All @@ -935,7 +942,7 @@ impl<'a, const N: usize> GrantR<'a, N> {
/// Additionally, you must ensure that a separate reference to this data is not created
/// to this data, e.g. using `Deref` or the `buf()` method of this grant.
pub unsafe fn as_static_buf(&self) -> &'static [u8] {
transmute::<&[u8], &'static [u8]>(self.buf)
transmute::<&[u8], &'static [u8]>(self.buf())
}

#[inline(always)]
Expand Down Expand Up @@ -1022,15 +1029,19 @@ impl<'a, const N: usize> SplitGrantR<'a, N> {
/// # }
/// ```
pub fn bufs(&self) -> (&[u8], &[u8]) {
(self.buf1, self.buf2)
let buf1 = unsafe { from_raw_parts(self.buf1.as_ptr() as *const u8, self.buf1.len()) };
let buf2 = unsafe { from_raw_parts(self.buf2.as_ptr() as *const u8, self.buf2.len()) };
(buf1, buf2)
}

/// Obtain mutable access to both parts of the read grant
///
/// This is useful if you are performing in-place operations
/// on an incoming packet, such as decryption
pub fn bufs_mut(&mut self) -> (&mut [u8], &mut [u8]) {
(self.buf1, self.buf2)
let buf1 = unsafe { from_raw_parts_mut(self.buf1.as_ptr() as *mut u8, self.buf1.len()) };
let buf2 = unsafe { from_raw_parts_mut(self.buf2.as_ptr() as *mut u8, self.buf2.len()) };
(buf1, buf2)
}

#[inline(always)]
Expand Down Expand Up @@ -1091,27 +1102,27 @@ impl<'a, const N: usize> Deref for GrantW<'a, N> {
type Target = [u8];

fn deref(&self) -> &Self::Target {
self.buf
unsafe { from_raw_parts_mut(self.buf.as_ptr() as *mut u8, self.buf.len()) }
}
}

impl<'a, const N: usize> DerefMut for GrantW<'a, N> {
fn deref_mut(&mut self) -> &mut [u8] {
self.buf
self.buf()
}
}

impl<'a, const N: usize> Deref for GrantR<'a, N> {
type Target = [u8];

fn deref(&self) -> &Self::Target {
self.buf
self.buf()
}
}

impl<'a, const N: usize> DerefMut for GrantR<'a, N> {
fn deref_mut(&mut self) -> &mut [u8] {
self.buf
self.buf_mut()
}
}

Expand Down
8 changes: 4 additions & 4 deletions core/src/framed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,27 +159,27 @@ impl<'a, const N: usize> Deref for FrameGrantW<'a, N> {
type Target = [u8];

fn deref(&self) -> &Self::Target {
&self.grant_w.buf[self.hdr_len.into()..]
&self.grant_w[self.hdr_len.into()..]
}
}

impl<'a, const N: usize> DerefMut for FrameGrantW<'a, N> {
fn deref_mut(&mut self) -> &mut [u8] {
&mut self.grant_w.buf[self.hdr_len.into()..]
&mut self.grant_w[self.hdr_len.into()..]
}
}

impl<'a, const N: usize> Deref for FrameGrantR<'a, N> {
type Target = [u8];

fn deref(&self) -> &Self::Target {
&self.grant_r.buf[self.hdr_len.into()..]
&self.grant_r[self.hdr_len.into()..]
}
}

impl<'a, const N: usize> DerefMut for FrameGrantR<'a, N> {
fn deref_mut(&mut self) -> &mut [u8] {
&mut self.grant_r.buf[self.hdr_len.into()..]
&mut self.grant_r[self.hdr_len.into()..]
}
}

Expand Down

0 comments on commit 8468029

Please sign in to comment.