From 9a47495227a03400fa2549b07fe8af131f21e759 Mon Sep 17 00:00:00 2001
From: Tamir Duberstein <tamird@gmail.com>
Date: Sun, 2 Mar 2025 07:56:57 -0500
Subject: [PATCH] aya,aya-obj: preserve pointer provenance

---
 aya-obj/src/obj.rs               |  2 +-
 aya/src/maps/mod.rs              |  6 +++---
 aya/src/maps/perf/perf_buffer.rs | 33 ++------------------------------
 3 files changed, 6 insertions(+), 35 deletions(-)

diff --git a/aya-obj/src/obj.rs b/aya-obj/src/obj.rs
index 1c0eaad5..30801da2 100644
--- a/aya-obj/src/obj.rs
+++ b/aya-obj/src/obj.rs
@@ -1591,7 +1591,7 @@ mod tests {
             pinning: PinningType::ByName,
         };
         let mut buf = [0u8; 128];
-        unsafe { ptr::write_unaligned(buf.as_mut_ptr() as *mut _, def) };
+        unsafe { ptr::write_unaligned(buf.as_mut_ptr().cast(), def) };
 
         assert_eq!(parse_map_def("foo", &buf).unwrap(), def);
     }
diff --git a/aya/src/maps/mod.rs b/aya/src/maps/mod.rs
index a12b2cf3..ea854013 100644
--- a/aya/src/maps/mod.rs
+++ b/aya/src/maps/mod.rs
@@ -934,10 +934,10 @@ impl<T: Pod> PerCpuValues<T> {
 
     pub(crate) fn build_kernel_mem(&self) -> Result<PerCpuKernelMem, io::Error> {
         let mut mem = Self::alloc_kernel_mem()?;
-        let mem_ptr = mem.as_mut_ptr() as usize;
+        let mem_ptr = mem.as_mut_ptr();
         let value_size = (mem::size_of::<T>() + 7) & !7;
-        for i in 0..self.values.len() {
-            unsafe { ptr::write_unaligned((mem_ptr + i * value_size) as *mut _, self.values[i]) };
+        for (i, value) in self.values.iter().enumerate() {
+            unsafe { ptr::write_unaligned(mem_ptr.byte_add(i * value_size).cast(), *value) };
         }
 
         Ok(mem)
diff --git a/aya/src/maps/perf/perf_buffer.rs b/aya/src/maps/perf/perf_buffer.rs
index 39996764..aad7d448 100644
--- a/aya/src/maps/perf/perf_buffer.rs
+++ b/aya/src/maps/perf/perf_buffer.rs
@@ -354,10 +354,6 @@ mod tests {
     }
 
     #[test]
-    #[cfg_attr(
-        miri,
-        ignore = "`ptr::write_unaligned(dst, value)` is attempting a write access but no exposed tags have suitable permission in the borrow stack for this location"
-    )]
     fn test_read_first_lost() {
         let mut mmapped_buf = MMappedBuf {
             data: [0; PAGE_SIZE * 2],
@@ -397,10 +393,9 @@ mod tests {
     }
 
     fn write<T: Debug>(mmapped_buf: &mut MMappedBuf, offset: usize, value: T) -> usize {
-        let dst = (mmapped_buf as *const _ as usize + PAGE_SIZE + offset) as *const PerfSample<T>
-            as *mut T;
+        let dst: *mut _ = mmapped_buf;
         unsafe {
-            ptr::write_unaligned(dst, value);
+            ptr::write_unaligned(dst.byte_add(PAGE_SIZE + offset).cast(), value);
             mmapped_buf.mmap_page.data_head = (offset + mem::size_of::<T>()) as u64;
             mmapped_buf.mmap_page.data_head as usize
         }
@@ -430,10 +425,6 @@ mod tests {
     }
 
     #[test]
-    #[cfg_attr(
-        miri,
-        ignore = "`ptr::write_unaligned(dst, value)` is attempting a write access but no exposed tags have suitable permission in the borrow stack for this location"
-    )]
     fn test_read_first_sample() {
         let mut mmapped_buf = MMappedBuf {
             data: [0; PAGE_SIZE * 2],
@@ -451,10 +442,6 @@ mod tests {
     }
 
     #[test]
-    #[cfg_attr(
-        miri,
-        ignore = "`ptr::write_unaligned(dst, value)` is attempting a write access but no exposed tags have suitable permission in the borrow stack for this location"
-    )]
     fn test_read_many_with_many_reads() {
         let mut mmapped_buf = MMappedBuf {
             data: [0; PAGE_SIZE * 2],
@@ -477,10 +464,6 @@ mod tests {
     }
 
     #[test]
-    #[cfg_attr(
-        miri,
-        ignore = "`ptr::write_unaligned(dst, value)` is attempting a write access but no exposed tags have suitable permission in the borrow stack for this location"
-    )]
     fn test_read_many_with_one_read() {
         let mut mmapped_buf = MMappedBuf {
             data: [0; PAGE_SIZE * 2],
@@ -502,10 +485,6 @@ mod tests {
     }
 
     #[test]
-    #[cfg_attr(
-        miri,
-        ignore = "`ptr::write_unaligned(dst, value)` is attempting a write access but no exposed tags have suitable permission in the borrow stack for this location"
-    )]
     fn test_read_last_sample() {
         let mut mmapped_buf = MMappedBuf {
             data: [0; PAGE_SIZE * 2],
@@ -525,10 +504,6 @@ mod tests {
     }
 
     #[test]
-    #[cfg_attr(
-        miri,
-        ignore = "`ptr::write_unaligned(dst, value)` is attempting a write access but no exposed tags have suitable permission in the borrow stack for this location"
-    )]
     fn test_read_wrapping_sample_size() {
         let mut mmapped_buf = MMappedBuf {
             data: [0; PAGE_SIZE * 2],
@@ -566,10 +541,6 @@ mod tests {
     }
 
     #[test]
-    #[cfg_attr(
-        miri,
-        ignore = "`ptr::write_unaligned(dst, value)` is attempting a write access but no exposed tags have suitable permission in the borrow stack for this location"
-    )]
     fn test_read_wrapping_value() {
         let mut mmapped_buf = MMappedBuf {
             data: [0; PAGE_SIZE * 2],