]> Repositorios git - scryer-prolog.git/commitdiff
fix some more miri errors
authorBennet Bleßmann <[email protected]>
Sat, 6 Jul 2024 01:41:12 +0000 (03:41 +0200)
committerBennet Bleßmann <[email protected]>
Sat, 6 Jul 2024 01:52:46 +0000 (03:52 +0200)
probably relevant to mthom/scryer-prolog#2438

src/arena.rs
src/heap_print.rs
src/machine/copier.rs
src/machine/lib_machine.rs

index 07a144ebdc9e23dccfaa42f12eb3b0ebdaa08ccc..9cf74ea8aafeb48593bb9913d3242ee31c97fe23 100644 (file)
@@ -20,6 +20,8 @@ use std::mem;
 use std::net::TcpListener;
 use std::ops::{Deref, DerefMut};
 use std::ptr;
+use std::ptr::addr_of_mut;
+use std::ptr::NonNull;
 use std::sync::RwLock;
 
 #[macro_export]
@@ -345,7 +347,7 @@ pub trait ArenaAllocated: Sized {
     #[allow(clippy::missing_safety_doc)]
     fn alloc(arena: &mut Arena, value: Self) -> TypedArenaPtr<Self> {
         let size = mem::size_of::<TypedAllocSlab<Self>>();
-        let mut slab = Box::new(TypedAllocSlab {
+        let slab = Box::new(TypedAllocSlab {
             slab: AllocSlab {
                 next: arena.base.take(),
                 #[cfg(target_pointer_width = "32")]
@@ -357,10 +359,10 @@ pub trait ArenaAllocated: Sized {
             payload: value,
         });
 
-        let allocated_ptr = slab.to_typed_arena_ptr();
-        let untyped_slab = unsafe { Box::from_raw(Box::into_raw(slab) as *mut AllocSlab) };
+        let raw_box = Box::into_raw(slab);
+        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
     }
@@ -568,7 +570,7 @@ impl ArenaAllocated for IndexPtr {
         });
 
         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
     }
 }
@@ -603,7 +605,7 @@ impl Clone for HeaderOrIdxPtr {
 #[repr(C)]
 #[derive(Clone, Debug)]
 pub struct AllocSlab {
-    next: Option<Box<AllocSlab>>,
+    next: Option<NonNull<AllocSlab>>,
     #[cfg(target_pointer_width = "32")]
     _padding: u32,
     header: HeaderOrIdxPtr,
@@ -618,16 +620,16 @@ pub struct TypedAllocSlab<Payload> {
 
 impl<Payload: ArenaAllocated> TypedAllocSlab<Payload> {
     #[inline]
-    pub fn to_typed_arena_ptr(&mut self) -> TypedArenaPtr<Payload> {
+    pub fn to_typed_arena_ptr(ptr: *mut Self) -> TypedArenaPtr<Payload> {
         // safety:
         // - this is the arena allocation of corresponding type
-        unsafe { TypedArenaPtr::new(&mut self.payload) }
+        unsafe { TypedArenaPtr::new(addr_of_mut!((*ptr).payload)) }
     }
 }
 
 #[derive(Debug)]
 pub struct Arena {
-    base: Option<Box<AllocSlab>>,
+    base: Option<NonNull<AllocSlab>>,
     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 {
         ($payload: ty, $value: expr) => {
-            let slab: &mut TypedAllocSlab<$payload> = mem::transmute($value);
-            ptr::drop_in_place(&mut slab.payload);
+            drop(Box::from_raw(
+                $value.as_ptr().cast::<TypedAllocSlab<$payload>>(),
+            ));
         };
     }
 
-    match value.header.header.tag() {
+    match (unsafe { value.as_ref() }).header.header.tag() {
         ArenaHeaderTag::Integer => {
             drop_typed_slab_in_place!(Integer, value);
         }
@@ -723,10 +726,10 @@ impl Drop for Arena {
     fn drop(&mut self) {
         let mut ptr = self.base.take();
 
-        while let Some(mut slab) = ptr {
+        while let Some(slab) = ptr {
             unsafe {
-                drop_slab_in_place(&mut slab);
-                ptr = slab.next;
+                ptr = slab.as_ref().next;
+                drop_slab_in_place(slab);
             }
         }
     }
@@ -814,7 +817,6 @@ mod tests {
     }
 
     #[test]
-    #[cfg_attr(miri, ignore = "blocked on arena.rs UB")]
     fn heap_put_literal_tests() {
         let mut wam = MockWAM::new();
 
index c844aedea3a814a46965255d35107694b98542dd..4982b2dea4b95e364726ac70b8787000928f6755 100644 (file)
@@ -1841,7 +1841,7 @@ mod tests {
     use crate::machine::mock_wam::*;
 
     #[test]
-    #[cfg_attr(miri, ignore = "blocked on streams.rs UB")]
+    #[cfg_attr(miri, ignore = "it takes too long to run")]
     fn term_printing_tests() {
         let mut wam = MockWAM::new();
 
index b64be4a080451a6ff68d753362d7c91dab398957..c02854ffdcb24212eee874b65720915b8cde5737 100644 (file)
@@ -398,7 +398,6 @@ mod tests {
     use crate::machine::mock_wam::*;
 
     #[test]
-    #[cfg_attr(miri, ignore = "blocked on atom_table.rs UB")]
     fn copier_tests() {
         let mut wam = MockWAM::new();
 
index d65fc53930bfceaaf8b7a436aa7e1282b5535a2b..f05b26c230f97cbcf29bd2121a6beac32ace0096 100644 (file)
@@ -292,7 +292,7 @@ mod tests {
     }
 
     #[test]
-    #[cfg_attr(miri, ignore)]
+    #[cfg_attr(miri, ignore = "blocked on streams.rs UB")]
     fn complex_results() {
         let mut machine = Machine::new_lib();
         machine.load_module_string(