From 44acdd7873a624eb7cdcc2ba3a5965c870d631e7 Mon Sep 17 00:00:00 2001 From: Janis Date: Fri, 21 Feb 2025 23:55:05 +0100 Subject: [PATCH] aligned function pointers --- src/lib.rs | 1 + src/praetor/mod.rs | 16 ++++++++++++++-- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 1250cd7..5bea4f0 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -4,6 +4,7 @@ debug_closure_helpers, cell_update, cold_path, + fn_align, let_chains )] diff --git a/src/praetor/mod.rs b/src/praetor/mod.rs index 0a06d4c..fb59fe7 100644 --- a/src/praetor/mod.rs +++ b/src/praetor/mod.rs @@ -66,6 +66,10 @@ mod util { } } + // Miri doesn't like tagging pointers that it doesn't know the alignment of. + // This includes function pointers, which aren't guaranteed to be aligned to + // anything, but generally have an alignment of 8, and can be specified to + // be aligned to `n` with `#[repr(align(n))]`. #[repr(transparent)] pub struct TaggedAtomicPtr(AtomicPtr<()>, PhantomData); @@ -85,7 +89,12 @@ mod util { pub fn ptr(&self, order: Ordering) -> NonNull { unsafe { - NonNull::new_unchecked(self.0.load(order).map_addr(|addr| addr & !Self::mask()) as _) + NonNull::new_unchecked( + self.0 + .load(order) + .map_addr(|addr| addr & !Self::mask()) + .cast(), + ) } } @@ -180,7 +189,7 @@ mod util { let mask = Self::mask(); loop { let ptr = self.0.load(failure); - let new = ptr.with_addr((ptr.addr() & !mask) | (tag & mask)); + let new = ptr.map_addr(|addr| (addr & !mask) | (tag & mask)); if self .0 @@ -823,6 +832,7 @@ mod job { F: FnOnce(&super::Scope) -> T + Send, T: Send, { + #[repr(align(8))] unsafe fn harness(this: *const (), job: *const Job<()>, scope: &super::Scope) where F: FnOnce(&super::Scope) -> T + Send, @@ -864,6 +874,7 @@ mod job { F: FnOnce(&super::Scope) -> T + Send, T: Send, { + #[repr(align(8))] unsafe fn harness(this: *const (), job: *const Job<()>, scope: &super::Scope) where F: FnOnce(&super::Scope) -> T + Send, @@ -1117,6 +1128,7 @@ impl Scope { let this = SendPtr::new(&raw const *self as *mut Self).unwrap(); let schedule = move |runnable: Runnable| { + #[repr(align(8))] unsafe fn harness(this: *const (), job: *const Job, _: &Scope) { let runnable = Runnable::<()>::from_raw(NonNull::new_unchecked(this.cast_mut())); runnable.run();