fix some more miri errors

probably relevant to mthom/scryer-prolog#2438
This commit is contained in:
Bennet Bleßmann
2024-07-06 03:41:12 +02:00
parent cba81b4dc7
commit 6575b1b573
4 changed files with 21 additions and 20 deletions

View File

@@ -20,6 +20,8 @@ use std::mem;
use std::net::TcpListener; use std::net::TcpListener;
use std::ops::{Deref, DerefMut}; use std::ops::{Deref, DerefMut};
use std::ptr; use std::ptr;
use std::ptr::addr_of_mut;
use std::ptr::NonNull;
use std::sync::RwLock; use std::sync::RwLock;
#[macro_export] #[macro_export]
@@ -345,7 +347,7 @@ pub trait ArenaAllocated: Sized {
#[allow(clippy::missing_safety_doc)] #[allow(clippy::missing_safety_doc)]
fn alloc(arena: &mut Arena, value: Self) -> TypedArenaPtr<Self> { fn alloc(arena: &mut Arena, value: Self) -> TypedArenaPtr<Self> {
let size = mem::size_of::<TypedAllocSlab<Self>>(); let size = mem::size_of::<TypedAllocSlab<Self>>();
let mut slab = Box::new(TypedAllocSlab { let slab = Box::new(TypedAllocSlab {
slab: AllocSlab { slab: AllocSlab {
next: arena.base.take(), next: arena.base.take(),
#[cfg(target_pointer_width = "32")] #[cfg(target_pointer_width = "32")]
@@ -357,10 +359,10 @@ pub trait ArenaAllocated: Sized {
payload: value, payload: value,
}); });
let allocated_ptr = slab.to_typed_arena_ptr(); let raw_box = Box::into_raw(slab);
let untyped_slab = unsafe { Box::from_raw(Box::into_raw(slab) as *mut AllocSlab) }; let allocated_ptr = TypedAllocSlab::to_typed_arena_ptr(raw_box);
arena.base = Some(untyped_slab); arena.base = Some(NonNull::new(raw_box.cast::<AllocSlab>()).unwrap());
allocated_ptr allocated_ptr
} }
@@ -568,7 +570,7 @@ impl ArenaAllocated for IndexPtr {
}); });
let allocated_ptr = unsafe { TypedArenaPtr::new(ptr::addr_of_mut!(slab.header.idx_ptr)) }; let allocated_ptr = unsafe { TypedArenaPtr::new(ptr::addr_of_mut!(slab.header.idx_ptr)) };
arena.base = Some(slab); arena.base = Some(NonNull::new(Box::into_raw(slab)).unwrap());
allocated_ptr allocated_ptr
} }
} }
@@ -603,7 +605,7 @@ impl Clone for HeaderOrIdxPtr {
#[repr(C)] #[repr(C)]
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
pub struct AllocSlab { pub struct AllocSlab {
next: Option<Box<AllocSlab>>, next: Option<NonNull<AllocSlab>>,
#[cfg(target_pointer_width = "32")] #[cfg(target_pointer_width = "32")]
_padding: u32, _padding: u32,
header: HeaderOrIdxPtr, header: HeaderOrIdxPtr,
@@ -618,16 +620,16 @@ pub struct TypedAllocSlab<Payload> {
impl<Payload: ArenaAllocated> TypedAllocSlab<Payload> { impl<Payload: ArenaAllocated> TypedAllocSlab<Payload> {
#[inline] #[inline]
pub fn to_typed_arena_ptr(&mut self) -> TypedArenaPtr<Payload> { pub fn to_typed_arena_ptr(ptr: *mut Self) -> TypedArenaPtr<Payload> {
// safety: // safety:
// - this is the arena allocation of corresponding type // - this is the arena allocation of corresponding type
unsafe { TypedArenaPtr::new(&mut self.payload) } unsafe { TypedArenaPtr::new(addr_of_mut!((*ptr).payload)) }
} }
} }
#[derive(Debug)] #[derive(Debug)]
pub struct Arena { pub struct Arena {
base: Option<Box<AllocSlab>>, base: Option<NonNull<AllocSlab>>,
pub f64_tbl: Arc<F64Table>, pub f64_tbl: Arc<F64Table>,
} }
@@ -645,15 +647,16 @@ impl Arena {
} }
} }
unsafe fn drop_slab_in_place(value: &mut AllocSlab) { unsafe fn drop_slab_in_place(value: NonNull<AllocSlab>) {
macro_rules! drop_typed_slab_in_place { macro_rules! drop_typed_slab_in_place {
($payload: ty, $value: expr) => { ($payload: ty, $value: expr) => {
let slab: &mut TypedAllocSlab<$payload> = mem::transmute($value); drop(Box::from_raw(
ptr::drop_in_place(&mut slab.payload); $value.as_ptr().cast::<TypedAllocSlab<$payload>>(),
));
}; };
} }
match value.header.header.tag() { match (unsafe { value.as_ref() }).header.header.tag() {
ArenaHeaderTag::Integer => { ArenaHeaderTag::Integer => {
drop_typed_slab_in_place!(Integer, value); drop_typed_slab_in_place!(Integer, value);
} }
@@ -723,10 +726,10 @@ impl Drop for Arena {
fn drop(&mut self) { fn drop(&mut self) {
let mut ptr = self.base.take(); let mut ptr = self.base.take();
while let Some(mut slab) = ptr { while let Some(slab) = ptr {
unsafe { unsafe {
drop_slab_in_place(&mut slab); ptr = slab.as_ref().next;
ptr = slab.next; drop_slab_in_place(slab);
} }
} }
} }
@@ -814,7 +817,6 @@ mod tests {
} }
#[test] #[test]
#[cfg_attr(miri, ignore = "blocked on arena.rs UB")]
fn heap_put_literal_tests() { fn heap_put_literal_tests() {
let mut wam = MockWAM::new(); let mut wam = MockWAM::new();

View File

@@ -1841,7 +1841,7 @@ mod tests {
use crate::machine::mock_wam::*; use crate::machine::mock_wam::*;
#[test] #[test]
#[cfg_attr(miri, ignore = "blocked on streams.rs UB")] #[cfg_attr(miri, ignore = "it takes too long to run")]
fn term_printing_tests() { fn term_printing_tests() {
let mut wam = MockWAM::new(); let mut wam = MockWAM::new();

View File

@@ -398,7 +398,6 @@ mod tests {
use crate::machine::mock_wam::*; use crate::machine::mock_wam::*;
#[test] #[test]
#[cfg_attr(miri, ignore = "blocked on atom_table.rs UB")]
fn copier_tests() { fn copier_tests() {
let mut wam = MockWAM::new(); let mut wam = MockWAM::new();

View File

@@ -292,7 +292,7 @@ mod tests {
} }
#[test] #[test]
#[cfg_attr(miri, ignore)] #[cfg_attr(miri, ignore = "blocked on streams.rs UB")]
fn complex_results() { fn complex_results() {
let mut machine = Machine::new_lib(); let mut machine = Machine::new_lib();
machine.load_module_string( machine.load_module_string(