|
| 1 | +use std::alloc; |
| 2 | +use std::alloc::Layout; |
| 3 | +use std::borrow::Cow; |
| 4 | +use std::slice; |
| 5 | + |
| 6 | +use rustc_middle::mir::interpret::AllocBytes; |
| 7 | +use rustc_target::abi::{Align, Size}; |
| 8 | + |
| 9 | +/// Allocation bytes that explicitly handle the layout of the data they're storing. |
| 10 | +/// This is necessary to interface with native code that accesses the program store in Miri. |
| 11 | +#[derive(Debug)] |
| 12 | +pub struct MiriAllocBytes { |
| 13 | + /// Stored layout information about the allocation. |
| 14 | + layout: alloc::Layout, |
| 15 | + /// Pointer to the allocation contents. |
| 16 | + /// Invariant: |
| 17 | + /// * If `self.layout.size() == 0`, then `self.ptr` is some suitably aligned pointer |
| 18 | + /// without provenance (and no actual memory was allocated). |
| 19 | + /// * Otherwise, `self.ptr` points to memory allocated with `self.layout`. |
| 20 | + ptr: *mut u8, |
| 21 | +} |
| 22 | + |
| 23 | +impl Clone for MiriAllocBytes { |
| 24 | + fn clone(&self) -> Self { |
| 25 | + let bytes: Cow<'_, [u8]> = Cow::Borrowed(self); |
| 26 | + let align = Align::from_bytes(self.layout.align().try_into().unwrap()).unwrap(); |
| 27 | + MiriAllocBytes::from_bytes(bytes, align) |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +impl Drop for MiriAllocBytes { |
| 32 | + fn drop(&mut self) { |
| 33 | + if self.layout.size() != 0 { |
| 34 | + // SAFETY: Invariant, `self.ptr` points to memory allocated with `self.layout`. |
| 35 | + unsafe { alloc::dealloc(self.ptr, self.layout) } |
| 36 | + } |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +impl std::ops::Deref for MiriAllocBytes { |
| 41 | + type Target = [u8]; |
| 42 | + |
| 43 | + fn deref(&self) -> &Self::Target { |
| 44 | + // SAFETY: `ptr` is non-null, properly aligned, and valid for reading out `self.layout.size()`-many bytes. |
| 45 | + // Note that due to the invariant this is true even if `self.layout.size() == 0`. |
| 46 | + unsafe { slice::from_raw_parts(self.ptr, self.layout.size()) } |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +impl std::ops::DerefMut for MiriAllocBytes { |
| 51 | + fn deref_mut(&mut self) -> &mut Self::Target { |
| 52 | + // SAFETY: `ptr` is non-null, properly aligned, and valid for reading out `self.layout.size()`-many bytes. |
| 53 | + // Note that due to the invariant this is true even if `self.layout.size() == 0`. |
| 54 | + unsafe { slice::from_raw_parts_mut(self.ptr, self.layout.size()) } |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +impl MiriAllocBytes { |
| 59 | + /// This method factors out how a `MiriAllocBytes` object is allocated, |
| 60 | + /// specifically given an allocation function `alloc_fn`. |
| 61 | + /// `alloc_fn` is only used if `size != 0`. |
| 62 | + /// Returns `Err(layout)` if the allocation function returns a `ptr` that is `ptr.is_null()`. |
| 63 | + fn alloc_with( |
| 64 | + size: usize, |
| 65 | + align: usize, |
| 66 | + alloc_fn: impl FnOnce(Layout) -> *mut u8, |
| 67 | + ) -> Result<MiriAllocBytes, Layout> { |
| 68 | + let layout = Layout::from_size_align(size, align).unwrap(); |
| 69 | + let ptr = if size == 0 { |
| 70 | + std::ptr::without_provenance_mut(align) |
| 71 | + } else { |
| 72 | + let ptr = alloc_fn(layout); |
| 73 | + if ptr.is_null() { |
| 74 | + return Err(layout); |
| 75 | + } |
| 76 | + ptr |
| 77 | + }; |
| 78 | + // SAFETY: All `MiriAllocBytes` invariants are fulfilled. |
| 79 | + Ok(Self { ptr, layout }) |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +impl AllocBytes for MiriAllocBytes { |
| 84 | + fn from_bytes<'a>(slice: impl Into<Cow<'a, [u8]>>, align: Align) -> Self { |
| 85 | + let slice = slice.into(); |
| 86 | + let size = slice.len(); |
| 87 | + let align = align.bytes_usize(); |
| 88 | + // SAFETY: `alloc_fn` will only be used if `size != 0`. |
| 89 | + let alloc_fn = |layout| unsafe { alloc::alloc(layout) }; |
| 90 | + let alloc_bytes = MiriAllocBytes::alloc_with(size, align, alloc_fn) |
| 91 | + .unwrap_or_else(|layout| alloc::handle_alloc_error(layout)); |
| 92 | + // SAFETY: `alloc_bytes.ptr` and `slice.as_ptr()` are non-null, properly aligned |
| 93 | + // and valid for the `size`-many bytes to be copied. |
| 94 | + unsafe { alloc_bytes.ptr.copy_from(slice.as_ptr(), size) }; |
| 95 | + alloc_bytes |
| 96 | + } |
| 97 | + |
| 98 | + fn zeroed(size: Size, align: Align) -> Option<Self> { |
| 99 | + let size = size.bytes_usize(); |
| 100 | + let align = align.bytes_usize(); |
| 101 | + // SAFETY: `alloc_fn` will only be used if `size != 0`. |
| 102 | + let alloc_fn = |layout| unsafe { alloc::alloc_zeroed(layout) }; |
| 103 | + MiriAllocBytes::alloc_with(size, align, alloc_fn).ok() |
| 104 | + } |
| 105 | + |
| 106 | + fn as_mut_ptr(&mut self) -> *mut u8 { |
| 107 | + self.ptr |
| 108 | + } |
| 109 | +} |
0 commit comments