Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix MIRI under tree borrows #266

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
not(debug_assertions),
deny(missing_docs, clippy::missing_docs_in_private_items)
)]
#![cfg_attr(miri, feature(strict_provenance))]
#![deny(unconditional_recursion)]
#![allow(
clippy::declare_interior_mutable_const,
Expand Down
31 changes: 23 additions & 8 deletions src/ptr/span.rs
Original file line number Diff line number Diff line change
Expand Up @@ -273,10 +273,17 @@ where
/// [`::new`]: Self::new
#[cfg(feature = "alloc")]
pub(crate) unsafe fn set_address(&mut self, addr: Address<M, T>) {
let mut addr_value = addr.to_const() as usize;
addr_value &= Self::PTR_ADDR_MASK;
addr_value |= self.ptr.as_ptr() as usize & Self::PTR_HEAD_MASK;
self.ptr = NonNull::new_unchecked(addr_value as *mut ())
let map = |mut addr_value| {
addr_value &= Self::PTR_ADDR_MASK;
addr_value |= self.ptr.as_ptr() as usize & Self::PTR_HEAD_MASK;
addr_value
};

#[cfg(miri)]
let new_addr = addr.to_const().map_addr(map).cast::<()>().cast_mut();
#[cfg(not(miri))]
let new_addr = map(addr.to_const() as usize) as *mut ();
self.ptr = NonNull::new_unchecked(new_addr);
}

/// Gets the starting bit index of the referent region.
Expand Down Expand Up @@ -313,11 +320,19 @@ where
#[cfg(feature = "alloc")]
pub(crate) unsafe fn set_head(&mut self, head: BitIdx<T::Mem>) {
let head = head.into_inner() as usize;
let mut ptr = self.ptr.as_ptr() as usize;
let map = |mut ptr_value| {
ptr_value &= Self::PTR_ADDR_MASK;
ptr_value |= head >> Self::LEN_HEAD_BITS;
ptr_value
};

let ptr = self.ptr.as_ptr();

ptr &= Self::PTR_ADDR_MASK;
ptr |= head >> Self::LEN_HEAD_BITS;
self.ptr = NonNull::new_unchecked(ptr as *mut ());
#[cfg(miri)]
let new_ptr = ptr.map_addr(map);
#[cfg(not(miri))]
let new_ptr = map(ptr as usize) as *mut ();
self.ptr = NonNull::new_unchecked(new_ptr);

self.len &= !Self::LEN_HEAD_MASK;
self.len |= head & Self::LEN_HEAD_MASK;
Expand Down
7 changes: 5 additions & 2 deletions src/ptr/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,14 @@ fn free_functions() {

let one = BitPtr::<Mut, u8, Lsb0>::from_slice_mut(&mut a[..]);
let two = one.wrapping_add(8);
let three = BitPtr::<Mut, u16, Msb0>::from_mut(&mut b);
let four = three.wrapping_add(8);

unsafe {
bv_ptr::copy(two.to_const(), one, 8);
}
assert_eq!(a[0], !0);

let one = BitPtr::<Mut, u8, Lsb0>::from_slice_mut(&mut a[..]);
let three = BitPtr::<Mut, u16, Msb0>::from_mut(&mut b);
unsafe {
bv_ptr::copy(three.to_const(), one, 8);
}
Expand All @@ -43,6 +44,8 @@ fn free_functions() {
assert_eq!(a[1], 0);
assert_eq!(b, !0);

let three = BitPtr::<Mut, u16, Msb0>::from_mut(&mut b);
let four = three.wrapping_add(8);
unsafe {
bv_ptr::write_bits(four, false, 8);
}
Expand Down
3 changes: 2 additions & 1 deletion src/slice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#[cfg(feature = "alloc")]
use alloc::vec::Vec;
use core::{
cell::UnsafeCell,
marker::PhantomData,
ops::RangeBounds,
};
Expand Down Expand Up @@ -82,7 +83,7 @@ where
///
/// See `ptr::span` for more information on the encoding scheme used in
/// references to `BitSlice`.
_mem: [()],
_mem: [UnsafeCell<()>],
}

/// Constructors.
Expand Down
8 changes: 8 additions & 0 deletions src/slice/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ use core::{
Hash,
Hasher,
},
panic::RefUnwindSafe,
str,
};

Expand Down Expand Up @@ -541,6 +542,13 @@ where
}
}

impl<T, O> RefUnwindSafe for BitSlice<T, O>
where
T: BitStore + RefUnwindSafe,
O: BitOrder,
{
}

#[doc = include_str!("../../doc/slice/threadsafe.md")]
unsafe impl<T, O> Send for BitSlice<T, O>
where
Expand Down