|
| 1 | +//! Regression test for <https://github.com/rust-lang/miri/issues/3341>: |
| 2 | +//! If `Box` has a local allocator, then it can't be `noalias` as the allocator |
| 3 | +//! may want to access allocator state based on the data pointer. |
| 4 | +
|
| 5 | +//@revisions: stack tree |
| 6 | +//@[tree]compile-flags: -Zmiri-tree-borrows |
| 7 | +#![feature(allocator_api)] |
| 8 | +#![feature(strict_provenance)] |
| 9 | + |
| 10 | +use std::{ |
| 11 | + alloc::{AllocError, Allocator, Layout}, |
| 12 | + cell::{Cell, UnsafeCell}, |
| 13 | + ptr::{self, addr_of, NonNull}, |
| 14 | + thread::{self, ThreadId}, |
| 15 | + mem, |
| 16 | +}; |
| 17 | + |
| 18 | +const BIN_SIZE: usize = 8; |
| 19 | + |
| 20 | +// A bin represents a collection of blocks of a specific layout. |
| 21 | +#[repr(align(128))] |
| 22 | +struct MyBin { |
| 23 | + top: Cell<usize>, |
| 24 | + thread_id: ThreadId, |
| 25 | + memory: UnsafeCell<[usize; BIN_SIZE]>, |
| 26 | +} |
| 27 | + |
| 28 | +impl MyBin { |
| 29 | + fn pop(&self) -> Option<NonNull<u8>> { |
| 30 | + let top = self.top.get(); |
| 31 | + if top == BIN_SIZE { |
| 32 | + return None; |
| 33 | + } |
| 34 | + // Cast the *entire* thing to a raw pointer to not restrict its provenance. |
| 35 | + let bin = self as *const MyBin; |
| 36 | + let base_ptr = UnsafeCell::raw_get(unsafe{ addr_of!((*bin).memory )}).cast::<usize>(); |
| 37 | + let ptr = unsafe { NonNull::new_unchecked(base_ptr.add(top)) }; |
| 38 | + self.top.set(top + 1); |
| 39 | + Some(ptr.cast()) |
| 40 | + } |
| 41 | + |
| 42 | + // Pretends to not be a throwaway allocation method like this. A more realistic |
| 43 | + // substitute is using intrusive linked lists, which requires access to the |
| 44 | + // metadata of this bin as well. |
| 45 | + unsafe fn push(&self, ptr: NonNull<u8>) { |
| 46 | + // For now just check that this really is in this bin. |
| 47 | + let start = self.memory.get().addr(); |
| 48 | + let end = start + BIN_SIZE * mem::size_of::<usize>(); |
| 49 | + let addr = ptr.addr().get(); |
| 50 | + assert!((start..end).contains(&addr)); |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +// A collection of bins. |
| 55 | +struct MyAllocator { |
| 56 | + thread_id: ThreadId, |
| 57 | + // Pretends to be some complex collection of bins, such as an array of linked lists. |
| 58 | + bins: Box<[MyBin; 1]>, |
| 59 | +} |
| 60 | + |
| 61 | +impl MyAllocator { |
| 62 | + fn new() -> Self { |
| 63 | + let thread_id = thread::current().id(); |
| 64 | + MyAllocator { |
| 65 | + thread_id, |
| 66 | + bins: Box::new( |
| 67 | + [MyBin { |
| 68 | + top: Cell::new(0), |
| 69 | + thread_id, |
| 70 | + memory: UnsafeCell::default(), |
| 71 | + }; 1], |
| 72 | + ), |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + // Pretends to be expensive finding a suitable bin for the layout. |
| 77 | + fn find_bin(&self, layout: Layout) -> Option<&MyBin> { |
| 78 | + if layout == Layout::new::<usize>() { |
| 79 | + Some(&self.bins[0]) |
| 80 | + } else { |
| 81 | + None |
| 82 | + } |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +unsafe impl Allocator for MyAllocator { |
| 87 | + fn allocate(&self, layout: Layout) -> Result<NonNull<[u8]>, AllocError> { |
| 88 | + // Expensive bin search. |
| 89 | + let bin = self.find_bin(layout).ok_or(AllocError)?; |
| 90 | + let ptr = bin.pop().ok_or(AllocError)?; |
| 91 | + Ok(NonNull::slice_from_raw_parts(ptr, layout.size())) |
| 92 | + } |
| 93 | + |
| 94 | + unsafe fn deallocate(&self, ptr: NonNull<u8>, _layout: Layout) { |
| 95 | + // Since manually finding the corresponding bin of `ptr` is very expensive, |
| 96 | + // doing pointer arithmetics is preferred. |
| 97 | + // But this means we access `top` via `ptr` rather than `self`! |
| 98 | + // That is fundamentally the source of the aliasing trouble in this example. |
| 99 | + let their_bin = ptr.as_ptr().map_addr(|addr| addr & !127).cast::<MyBin>(); |
| 100 | + let thread_id = ptr::read(ptr::addr_of!((*their_bin).thread_id)); |
| 101 | + if self.thread_id == thread_id { |
| 102 | + unsafe { (*their_bin).push(ptr) }; |
| 103 | + } else { |
| 104 | + todo!("Deallocating from another thread") |
| 105 | + } |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +// Make sure to involve `Box` in allocating these, |
| 110 | +// as that's where `noalias` may come from. |
| 111 | +fn v<T, A: Allocator>(t: T, a: A) -> Vec<T, A> { |
| 112 | + (Box::new_in([t], a) as Box<[T], A>).into_vec() |
| 113 | +} |
| 114 | + |
| 115 | +fn main() { |
| 116 | + assert!(mem::size_of::<MyBin>() <= 128); // if it grows bigger, the trick to access the "header" no longer works |
| 117 | + let my_alloc = MyAllocator::new(); |
| 118 | + let a = v(1usize, &my_alloc); |
| 119 | + let b = v(2usize, &my_alloc); |
| 120 | + assert_eq!(a[0] + 1, b[0]); |
| 121 | + assert_eq!(addr_of!(a[0]).wrapping_add(1), addr_of!(b[0])); |
| 122 | + drop((a, b)); |
| 123 | +} |
0 commit comments