Skip to content

Commit 8468029

Browse files
committed
Merge branch 'fix/ub-pass-miri-tests' of https://github.com/xgroleau/bbqueue into main
2 parents 443e78f + 12a61e1 commit 8468029

File tree

2 files changed

+38
-27
lines changed

2 files changed

+38
-27
lines changed

core/src/bbbuffer.rs

+34-23
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use core::{
1010
ops::{Deref, DerefMut},
1111
ptr::NonNull,
1212
result::Result as CoreResult,
13-
slice::from_raw_parts_mut,
13+
slice::{from_raw_parts, from_raw_parts_mut},
1414
sync::atomic::{
1515
AtomicBool, AtomicUsize,
1616
Ordering::{AcqRel, Acquire, Release},
@@ -404,9 +404,10 @@ impl<'a, const N: usize> Producer<'a, N> {
404404
let grant_slice = unsafe { from_raw_parts_mut(start_of_buf_ptr.add(start), sz) };
405405

406406
Ok(GrantW {
407-
buf: grant_slice,
407+
buf: grant_slice.into(),
408408
bbq: self.bbq,
409409
to_commit: 0,
410+
phatom: PhantomData,
410411
})
411412
}
412413

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

509510
Ok(GrantW {
510-
buf: grant_slice,
511+
buf: grant_slice.into(),
511512
bbq: self.bbq,
512513
to_commit: 0,
514+
phatom: PhantomData,
513515
})
514516
}
515517
}
@@ -598,9 +600,10 @@ impl<'a, const N: usize> Consumer<'a, N> {
598600
let grant_slice = unsafe { from_raw_parts_mut(start_of_buf_ptr.add(read), sz) };
599601

600602
Ok(GrantR {
601-
buf: grant_slice,
603+
buf: grant_slice.into(),
602604
bbq: self.bbq,
603605
to_release: 0,
606+
phatom: PhantomData,
604607
})
605608
}
606609

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

653656
Ok(SplitGrantR {
654-
buf1: grant_slice1,
655-
buf2: grant_slice2,
657+
buf1: grant_slice1.into(),
658+
buf2: grant_slice2.into(),
656659
bbq: self.bbq,
657660
to_release: 0,
661+
phatom: PhantomData,
658662
})
659663
}
660664
}
@@ -697,9 +701,10 @@ impl<const N: usize> BBBuffer<N> {
697701
/// without committing it takes a short critical section,
698702
#[derive(Debug, PartialEq)]
699703
pub struct GrantW<'a, const N: usize> {
700-
pub(crate) buf: &'a mut [u8],
704+
pub(crate) buf: NonNull<[u8]>,
701705
bbq: NonNull<BBBuffer<N>>,
702706
pub(crate) to_commit: usize,
707+
phatom: PhantomData<&'a mut [u8]>,
703708
}
704709

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

726732
/// A structure representing up to two contiguous regions of memory that
727733
/// may be read from, and potentially "released" (or cleared)
728734
/// from the queue
729735
#[derive(Debug, PartialEq)]
730736
pub struct SplitGrantR<'a, const N: usize> {
731-
pub(crate) buf1: &'a mut [u8],
732-
pub(crate) buf2: &'a mut [u8],
737+
pub(crate) buf1: NonNull<[u8]>,
738+
pub(crate) buf2: NonNull<[u8]>,
733739
bbq: NonNull<BBBuffer<N>>,
734740
pub(crate) to_release: usize,
741+
phatom: PhantomData<&'a mut [u8]>,
735742
}
736743

737744
unsafe impl<'a, const N: usize> Send for GrantR<'a, N> {}
@@ -777,7 +784,7 @@ impl<'a, const N: usize> GrantW<'a, N> {
777784
/// # }
778785
/// ```
779786
pub fn buf(&mut self) -> &mut [u8] {
780-
self.buf
787+
unsafe { from_raw_parts_mut(self.buf.as_ptr() as *mut u8, self.buf.len()) }
781788
}
782789

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

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

876883
pub(crate) fn shrink(&mut self, len: usize) {
877884
let mut new_buf: &mut [u8] = &mut [];
878-
core::mem::swap(&mut self.buf, &mut new_buf);
885+
core::mem::swap(&mut self.buf_mut(), &mut new_buf);
879886
let (new, _) = new_buf.split_at_mut(len);
880-
self.buf = new;
887+
self.buf = new.into();
881888
}
882889

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

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

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

941948
#[inline(always)]
@@ -1022,15 +1029,19 @@ impl<'a, const N: usize> SplitGrantR<'a, N> {
10221029
/// # }
10231030
/// ```
10241031
pub fn bufs(&self) -> (&[u8], &[u8]) {
1025-
(self.buf1, self.buf2)
1032+
let buf1 = unsafe { from_raw_parts(self.buf1.as_ptr() as *const u8, self.buf1.len()) };
1033+
let buf2 = unsafe { from_raw_parts(self.buf2.as_ptr() as *const u8, self.buf2.len()) };
1034+
(buf1, buf2)
10261035
}
10271036

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

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

10931104
fn deref(&self) -> &Self::Target {
1094-
self.buf
1105+
unsafe { from_raw_parts_mut(self.buf.as_ptr() as *mut u8, self.buf.len()) }
10951106
}
10961107
}
10971108

10981109
impl<'a, const N: usize> DerefMut for GrantW<'a, N> {
10991110
fn deref_mut(&mut self) -> &mut [u8] {
1100-
self.buf
1111+
self.buf()
11011112
}
11021113
}
11031114

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

11071118
fn deref(&self) -> &Self::Target {
1108-
self.buf
1119+
self.buf()
11091120
}
11101121
}
11111122

11121123
impl<'a, const N: usize> DerefMut for GrantR<'a, N> {
11131124
fn deref_mut(&mut self) -> &mut [u8] {
1114-
self.buf
1125+
self.buf_mut()
11151126
}
11161127
}
11171128

core/src/framed.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -159,27 +159,27 @@ impl<'a, const N: usize> Deref for FrameGrantW<'a, N> {
159159
type Target = [u8];
160160

161161
fn deref(&self) -> &Self::Target {
162-
&self.grant_w.buf[self.hdr_len.into()..]
162+
&self.grant_w[self.hdr_len.into()..]
163163
}
164164
}
165165

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

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

175175
fn deref(&self) -> &Self::Target {
176-
&self.grant_r.buf[self.hdr_len.into()..]
176+
&self.grant_r[self.hdr_len.into()..]
177177
}
178178
}
179179

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

0 commit comments

Comments
 (0)