about summary refs log tree commit diff
diff options
context:
space:
mode:
authorRalf Jung <post@ralfj.de>2024-03-10 18:04:03 +0100
committerRalf Jung <post@ralfj.de>2024-03-10 18:07:34 +0100
commita7443f554215b76ef1f911b1d6d07b4b7ebbb23c (patch)
tree1fe53d59747d4705d16f04fa97cca704cb0434ce
parent81ebaf27cb4301a833f5a4ba1f4a6e63cfcc32b3 (diff)
downloadrust-a7443f554215b76ef1f911b1d6d07b4b7ebbb23c.tar.gz
rust-a7443f554215b76ef1f911b1d6d07b4b7ebbb23c.zip
test into_boxed_slice with custom allocator in Miri
-rw-r--r--src/tools/miri/tests/pass/box-custom-alloc-aliasing.rs18
1 files changed, 14 insertions, 4 deletions
diff --git a/src/tools/miri/tests/pass/box-custom-alloc-aliasing.rs b/src/tools/miri/tests/pass/box-custom-alloc-aliasing.rs
index 541e5f244db..ada9cf45375 100644
--- a/src/tools/miri/tests/pass/box-custom-alloc-aliasing.rs
+++ b/src/tools/miri/tests/pass/box-custom-alloc-aliasing.rs
@@ -92,6 +92,8 @@ unsafe impl Allocator for MyAllocator {
     }
 
     unsafe fn deallocate(&self, ptr: NonNull<u8>, _layout: Layout) {
+        // Make sure accesses via `self` don't disturb anything.
+        let _val = self.bins[0].top.get();
         // Since manually finding the corresponding bin of `ptr` is very expensive,
         // doing pointer arithmetics is preferred.
         // But this means we access `top` via `ptr` rather than `self`!
@@ -101,22 +103,30 @@ unsafe impl Allocator for MyAllocator {
         if self.thread_id == thread_id {
             unsafe { (*their_bin).push(ptr) };
         } else {
-            todo!("Deallocating from another thread")
+            todo!("Deallocating from another thread");
         }
+        // Make sure we can also still access this via `self` after the rest is done.
+        let _val = self.bins[0].top.get();
     }
 }
 
 // Make sure to involve `Box` in allocating these,
 // as that's where `noalias` may come from.
-fn v<T, A: Allocator>(t: T, a: A) -> Vec<T, A> {
+fn v1<T, A: Allocator>(t: T, a: A) -> Vec<T, A> {
     (Box::new_in([t], a) as Box<[T], A>).into_vec()
 }
+fn v2<T, A: Allocator>(t: T, a: A) -> Vec<T, A> {
+    let v = v1(t, a);
+    // There was a bug in `into_boxed_slice` that caused aliasing issues,
+    // so round-trip through that as well.
+    v.into_boxed_slice().into_vec()
+}
 
 fn main() {
     assert!(mem::size_of::<MyBin>() <= 128); // if it grows bigger, the trick to access the "header" no longer works
     let my_alloc = MyAllocator::new();
-    let a = v(1usize, &my_alloc);
-    let b = v(2usize, &my_alloc);
+    let a = v1(1usize, &my_alloc);
+    let b = v2(2usize, &my_alloc);
     assert_eq!(a[0] + 1, b[0]);
     assert_eq!(addr_of!(a[0]).wrapping_add(1), addr_of!(b[0]));
     drop((a, b));