Skip to main content

rustc_middle/mir/interpret/
allocation.rs

1//! The virtual memory representation of the MIR interpreter.
2
3mod init_mask;
4mod provenance_map;
5
6use std::alloc::{self, Layout};
7use std::borrow::Cow;
8use std::hash::Hash;
9use std::ops::{Deref, DerefMut, Range};
10use std::{fmt, hash, ptr};
11
12use either::{Left, Right};
13use init_mask::*;
14pub use init_mask::{InitChunk, InitChunkIter};
15use provenance_map::*;
16use rustc_abi::{Align, HasDataLayout, Size};
17use rustc_ast::Mutability;
18use rustc_data_structures::intern::Interned;
19use rustc_macros::StableHash;
20use rustc_serialize::{Decodable, Decoder, Encodable, Encoder};
21
22use super::{
23    AllocId, BadBytesAccess, CtfeProvenance, InterpErrorKind, InterpResult, Pointer, Provenance,
24    ResourceExhaustionInfo, Scalar, UndefinedBehaviorInfo, UnsupportedOpInfo, interp_ok,
25    read_target_uint, write_target_uint,
26};
27use crate::ty;
28
29/// Functionality required for the bytes of an `Allocation`.
30pub trait AllocBytes: Clone + fmt::Debug + Deref<Target = [u8]> + DerefMut<Target = [u8]> {
31    /// The type of extra parameters passed in when creating an allocation.
32    /// Can be used by `interpret::Machine` instances to make runtime-configuration-dependent
33    /// decisions about the allocation strategy.
34    type AllocParams;
35
36    /// Create an `AllocBytes` from a slice of `u8`.
37    fn from_bytes<'a>(
38        slice: impl Into<Cow<'a, [u8]>>,
39        _align: Align,
40        _params: Self::AllocParams,
41    ) -> Self;
42
43    /// Create a zeroed `AllocBytes` of the specified size and alignment.
44    /// Returns `None` if we ran out of memory on the host.
45    fn zeroed(size: Size, _align: Align, _params: Self::AllocParams) -> Option<Self>;
46
47    /// Gives direct access to the raw underlying storage.
48    ///
49    /// Crucially this pointer is compatible with:
50    /// - other pointers returned by this method, and
51    /// - references returned from `deref()`, as long as there was no write.
52    fn as_mut_ptr(&mut self) -> *mut u8;
53
54    /// Gives direct access to the raw underlying storage.
55    ///
56    /// Crucially this pointer is compatible with:
57    /// - other pointers returned by this method, and
58    /// - references returned from `deref()`, as long as there was no write.
59    fn as_ptr(&self) -> *const u8;
60}
61
62/// Default `bytes` for `Allocation` is a `Box<u8>`.
63impl AllocBytes for Box<[u8]> {
64    type AllocParams = ();
65
66    fn from_bytes<'a>(slice: impl Into<Cow<'a, [u8]>>, _align: Align, _params: ()) -> Self {
67        Box::<[u8]>::from(slice.into())
68    }
69
70    fn zeroed(size: Size, _align: Align, _params: ()) -> Option<Self> {
71        let bytes = Box::<[u8]>::try_new_zeroed_slice(size.bytes().try_into().ok()?).ok()?;
72        // SAFETY: the box was zero-allocated, which is a valid initial value for Box<[u8]>
73        let bytes = unsafe { bytes.assume_init() };
74        Some(bytes)
75    }
76
77    fn as_mut_ptr(&mut self) -> *mut u8 {
78        Box::as_mut_ptr(self).cast()
79    }
80
81    fn as_ptr(&self) -> *const u8 {
82        Box::as_ptr(self).cast()
83    }
84}
85
86/// This type represents an Allocation in the Miri/CTFE core engine.
87///
88/// Its public API is rather low-level, working directly with allocation offsets and a custom error
89/// type to account for the lack of an AllocId on this level. The Miri/CTFE core engine `memory`
90/// module provides higher-level access.
91// Note: for performance reasons when interning, some of the `Allocation` fields can be partially
92// hashed. (see the `Hash` impl below for more details), so the impl is not derived.
93#[derive(#[automatically_derived]impl<Prov: ::core::clone::Clone + Provenance,
    Extra: ::core::clone::Clone, Bytes: ::core::clone::Clone>
    ::core::clone::Clone for Allocation<Prov, Extra, Bytes> {
    #[inline]
    #[rustc_trivial_field_reads]
    fn clone(&self) -> Self {
        Allocation {
            bytes: ::core::clone::Clone::clone(&self.bytes),
            provenance: ::core::clone::Clone::clone(&self.provenance),
            init_mask: ::core::clone::Clone::clone(&self.init_mask),
            align: ::core::clone::Clone::clone(&self.align),
            mutability: ::core::clone::Clone::clone(&self.mutability),
            extra: ::core::clone::Clone::clone(&self.extra),
        }
    }
}Clone, #[automatically_derived]impl<Prov: ::core::cmp::Eq + Provenance,
    Extra: ::core::cmp::Eq, Bytes: ::core::cmp::Eq> ::core::cmp::Eq for
    Allocation<Prov, Extra, Bytes> {
    #[inline]
    #[doc(hidden)]
    #[coverage(off)]
    fn assert_fields_are_eq(&self) {
        let _: ::core::cmp::AssertParamIsEq<Bytes>;
        let _: ::core::cmp::AssertParamIsEq<ProvenanceMap<Prov>>;
        let _: ::core::cmp::AssertParamIsEq<InitMask>;
        let _: ::core::cmp::AssertParamIsEq<Align>;
        let _: ::core::cmp::AssertParamIsEq<Mutability>;
        let _: ::core::cmp::AssertParamIsEq<Extra>;
    }
}Eq, #[automatically_derived]impl<Prov: ::core::cmp::PartialEq + Provenance,
    Extra: ::core::cmp::PartialEq, Bytes: ::core::cmp::PartialEq>
    ::core::marker::StructuralPartialEq for Allocation<Prov, Extra, Bytes> {
}
#[automatically_derived]impl<Prov: ::core::cmp::PartialEq + Provenance,
    Extra: ::core::cmp::PartialEq, Bytes: ::core::cmp::PartialEq>
    ::core::cmp::PartialEq for Allocation<Prov, Extra, Bytes> {
    #[inline]
    fn eq(&self, other: &Self) -> bool {
        self.bytes == other.bytes && self.provenance == other.provenance &&
                        self.init_mask == other.init_mask &&
                    self.align == other.align &&
                self.mutability == other.mutability &&
            self.extra == other.extra
    }
}PartialEq)]
94#[derive(const _: () =
    {
        impl<Prov: Provenance, Extra, Bytes>
            ::rustc_data_structures::stable_hash::StableHash for
            Allocation<Prov, Extra, Bytes> where
            Bytes: ::rustc_data_structures::stable_hash::StableHash,
            Prov: ::rustc_data_structures::stable_hash::StableHash,
            Extra: ::rustc_data_structures::stable_hash::StableHash {
            #[inline]
            fn stable_hash<__Hcx: ::rustc_data_structures::stable_hash::StableHashCtxt>(&self,
                __hcx: &mut __Hcx,
                __hasher:
                    &mut ::rustc_data_structures::stable_hash::StableHasher) {
                match *self {
                    Allocation {
                        bytes: ref __binding_0,
                        provenance: ref __binding_1,
                        init_mask: ref __binding_2,
                        align: ref __binding_3,
                        mutability: ref __binding_4,
                        extra: ref __binding_5 } => {
                        { __binding_0.stable_hash(__hcx, __hasher); }
                        { __binding_1.stable_hash(__hcx, __hasher); }
                        { __binding_2.stable_hash(__hcx, __hasher); }
                        { __binding_3.stable_hash(__hcx, __hasher); }
                        { __binding_4.stable_hash(__hcx, __hasher); }
                        { __binding_5.stable_hash(__hcx, __hasher); }
                    }
                }
            }
        }
    };StableHash)]
95pub struct Allocation<Prov: Provenance = CtfeProvenance, Extra = (), Bytes = Box<[u8]>> {
96    /// The actual bytes of the allocation.
97    /// Note that the bytes of a pointer represent the offset of the pointer.
98    bytes: Bytes,
99    /// Maps from byte addresses to extra provenance data for each pointer.
100    /// Only the first byte of a pointer is inserted into the map; i.e.,
101    /// every entry in this map applies to `pointer_size` consecutive bytes starting
102    /// at the given offset.
103    provenance: ProvenanceMap<Prov>,
104    /// Denotes which part of this allocation is initialized.
105    ///
106    /// Invariant: the uninitialized parts have no provenance.
107    init_mask: InitMask,
108    /// The alignment of the allocation to detect unaligned reads.
109    /// (`Align` guarantees that this is a power of two.)
110    pub align: Align,
111    /// `true` if the allocation is mutable.
112    /// Also used by codegen to determine if a static should be put into mutable memory,
113    /// which happens for `static mut` and `static` with interior mutability.
114    pub mutability: Mutability,
115    /// Extra state for the machine.
116    pub extra: Extra,
117}
118
119/// Helper struct that packs an alignment, mutability, and "all bytes are zero" flag together.
120///
121/// Alignment values always have 2 free high bits, and we check for this in our [`Encodable`] impl.
122struct AllocFlags {
123    align: Align,
124    mutability: Mutability,
125    all_zero: bool,
126}
127
128impl<E: Encoder> Encodable<E> for AllocFlags {
129    fn encode(&self, encoder: &mut E) {
130        // Make sure Align::MAX can be stored with the high 2 bits unset.
131        const {
132            let max_supported_align_repr = u8::MAX >> 2;
133            let max_supported_align = 1 << max_supported_align_repr;
134            if !(Align::MAX.bytes() <= max_supported_align) {
    ::core::panicking::panic("assertion failed: Align::MAX.bytes() <= max_supported_align")
}assert!(Align::MAX.bytes() <= max_supported_align)
135        }
136
137        let mut flags = self.align.bytes().trailing_zeros() as u8;
138        flags |= match self.mutability {
139            Mutability::Not => 0,
140            Mutability::Mut => 1 << 6,
141        };
142        flags |= (self.all_zero as u8) << 7;
143        flags.encode(encoder);
144    }
145}
146
147impl<D: Decoder> Decodable<D> for AllocFlags {
148    fn decode(decoder: &mut D) -> Self {
149        let flags: u8 = Decodable::decode(decoder);
150        let align = flags & 0b0011_1111;
151        let mutability = flags & 0b0100_0000;
152        let all_zero = flags & 0b1000_0000;
153
154        let align = Align::from_bytes(1 << align).unwrap();
155        let mutability = match mutability {
156            0 => Mutability::Not,
157            _ => Mutability::Mut,
158        };
159        let all_zero = all_zero > 0;
160
161        AllocFlags { align, mutability, all_zero }
162    }
163}
164
165/// Efficiently detect whether a slice of `u8` is all zero.
166///
167/// This is used in encoding of [`Allocation`] to special-case all-zero allocations. It is only
168/// optimized a little, because for many allocations the encoding of the actual bytes does not
169/// dominate runtime.
170#[inline]
171fn all_zero(buf: &[u8]) -> bool {
172    // In the empty case we wouldn't encode any contents even without this system where we
173    // special-case allocations whose contents are all 0. We can return anything in the empty case.
174    if buf.is_empty() {
175        return true;
176    }
177    // Just fast-rejecting based on the first element significantly reduces the amount that we end
178    // up walking the whole array.
179    if buf[0] != 0 {
180        return false;
181    }
182
183    // This strategy of combining all slice elements with & or | is unbeatable for the large
184    // all-zero case because it is so well-understood by autovectorization.
185    buf.iter().fold(true, |acc, b| acc & (*b == 0))
186}
187
188/// Custom encoder for [`Allocation`] to more efficiently represent the case where all bytes are 0.
189impl<Prov: Provenance, Extra, E: Encoder> Encodable<E> for Allocation<Prov, Extra, Box<[u8]>>
190where
191    ProvenanceMap<Prov>: Encodable<E>,
192    Extra: Encodable<E>,
193{
194    fn encode(&self, encoder: &mut E) {
195        let all_zero = all_zero(&self.bytes);
196        AllocFlags { align: self.align, mutability: self.mutability, all_zero }.encode(encoder);
197
198        encoder.emit_usize(self.bytes.len());
199        if !all_zero {
200            encoder.emit_raw_bytes(&self.bytes);
201        }
202        self.provenance.encode(encoder);
203        self.init_mask.encode(encoder);
204        self.extra.encode(encoder);
205    }
206}
207
208impl<Prov: Provenance, Extra, D: Decoder> Decodable<D> for Allocation<Prov, Extra, Box<[u8]>>
209where
210    ProvenanceMap<Prov>: Decodable<D>,
211    Extra: Decodable<D>,
212{
213    fn decode(decoder: &mut D) -> Self {
214        let AllocFlags { align, mutability, all_zero } = Decodable::decode(decoder);
215
216        let len = decoder.read_usize();
217        let bytes = if all_zero { ::alloc::vec::from_elem_global(0u8, len)vec![0u8; len] } else { decoder.read_raw_bytes(len).to_vec() };
218        let bytes = <Box<[u8]> as AllocBytes>::from_bytes(bytes, align, ());
219
220        let provenance = Decodable::decode(decoder);
221        let init_mask = Decodable::decode(decoder);
222        let extra = Decodable::decode(decoder);
223
224        Self { bytes, provenance, init_mask, align, mutability, extra }
225    }
226}
227
228/// This is the maximum size we will hash at a time, when interning an `Allocation` and its
229/// `InitMask`. Note, we hash that amount of bytes twice: at the start, and at the end of a buffer.
230/// Used when these two structures are large: we only partially hash the larger fields in that
231/// situation. See the comment at the top of their respective `Hash` impl for more details.
232const MAX_BYTES_TO_HASH: usize = 64;
233
234/// This is the maximum size (in bytes) for which a buffer will be fully hashed, when interning.
235/// Otherwise, it will be partially hashed in 2 slices, requiring at least 2 `MAX_BYTES_TO_HASH`
236/// bytes.
237const MAX_HASHED_BUFFER_LEN: usize = 2 * MAX_BYTES_TO_HASH;
238
239// Const allocations are only hashed for interning. However, they can be large, making the hashing
240// expensive especially since it uses `FxHash`: it's better suited to short keys, not potentially
241// big buffers like the actual bytes of allocation. We can partially hash some fields when they're
242// large.
243impl hash::Hash for Allocation {
244    fn hash<H: hash::Hasher>(&self, state: &mut H) {
245        let Self {
246            bytes,
247            provenance,
248            init_mask,
249            align,
250            mutability,
251            extra: (), // don't bother hashing ()
252        } = self;
253
254        // Partially hash the `bytes` buffer when it is large. To limit collisions with common
255        // prefixes and suffixes, we hash the length and some slices of the buffer.
256        let byte_count = bytes.len();
257        if byte_count > MAX_HASHED_BUFFER_LEN {
258            // Hash the buffer's length.
259            byte_count.hash(state);
260
261            // And its head and tail.
262            bytes[..MAX_BYTES_TO_HASH].hash(state);
263            bytes[byte_count - MAX_BYTES_TO_HASH..].hash(state);
264        } else {
265            bytes.hash(state);
266        }
267
268        // Hash the other fields as usual.
269        provenance.hash(state);
270        init_mask.hash(state);
271        align.hash(state);
272        mutability.hash(state);
273    }
274}
275
276/// Interned types generally have an `Outer` type and an `Inner` type, where
277/// `Outer` is a newtype around `Interned<Inner>`, and all the operations are
278/// done on `Outer`, because all occurrences are interned. E.g. `Ty` is an
279/// outer type and `TyKind` is its inner type.
280///
281/// Here things are different because only const allocations are interned. This
282/// means that both the inner type (`Allocation`) and the outer type
283/// (`ConstAllocation`) are used quite a bit.
284#[derive(#[automatically_derived]impl<'tcx> ::core::marker::Copy for
    ConstAllocation<'tcx> {
}Copy, #[automatically_derived]#[doc(hidden)]unsafe impl<'tcx>
    ::core::clone::TrivialClone for ConstAllocation<'tcx> {
}
#[automatically_derived]impl<'tcx> ::core::clone::Clone for
    ConstAllocation<'tcx> {
    #[inline]
    #[rustc_trivial_field_reads]
    fn clone(&self) -> Self {
        let _: ::core::clone::AssertParamIsClone<Interned<'tcx, Allocation>>;
        *self
    }
}Clone, #[automatically_derived]impl<'tcx> ::core::marker::StructuralPartialEq for
    ConstAllocation<'tcx> {
}
#[automatically_derived]impl<'tcx> ::core::cmp::PartialEq for
    ConstAllocation<'tcx> {
    #[inline]
    fn eq(&self, other: &Self) -> bool { self.0 == other.0 }
}PartialEq, #[automatically_derived]impl<'tcx> ::core::cmp::Eq for ConstAllocation<'tcx> {
    #[inline]
    #[doc(hidden)]
    #[coverage(off)]
    fn assert_fields_are_eq(&self) {
        let _: ::core::cmp::AssertParamIsEq<Interned<'tcx, Allocation>>;
    }
}Eq, #[automatically_derived]impl<'tcx> ::core::hash::Hash for
    ConstAllocation<'tcx> {
    #[inline]
    fn hash<__H: ::core::hash::Hasher>(&self, state: &mut __H) {
        ::core::hash::Hash::hash(&self.0, state)
    }
}Hash, const _: () =
    {
        impl<'tcx> ::rustc_data_structures::stable_hash::StableHash for
            ConstAllocation<'tcx> {
            #[inline]
            fn stable_hash<__Hcx: ::rustc_data_structures::stable_hash::StableHashCtxt>(&self,
                __hcx: &mut __Hcx,
                __hasher:
                    &mut ::rustc_data_structures::stable_hash::StableHasher) {
                match *self {
                    ConstAllocation(ref __binding_0) => {
                        { __binding_0.stable_hash(__hcx, __hasher); }
                    }
                }
            }
        }
    };StableHash)]
285#[cfg_attr(bootstrap, rustc_pass_by_value)]
286#[cfg_attr(not(bootstrap), lint::pass_by_value)]
287pub struct ConstAllocation<'tcx>(pub Interned<'tcx, Allocation>);
288
289impl<'tcx> fmt::Debug for ConstAllocation<'tcx> {
290    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
291        // The debug representation of this is very verbose and basically useless,
292        // so don't print it.
293        f.write_fmt(format_args!("ConstAllocation {{ .. }}"))write!(f, "ConstAllocation {{ .. }}")
294    }
295}
296
297impl<'tcx> ConstAllocation<'tcx> {
298    pub fn inner(self) -> &'tcx Allocation {
299        self.0.0
300    }
301}
302
303/// We have our own error type that does not know about the `AllocId`; that information
304/// is added when converting to `InterpError`.
305#[derive(#[automatically_derived]impl ::core::fmt::Debug for AllocError {
    #[inline]
    #[rustc_trivial_field_reads]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        match self {
            Self::ReadPointerAsInt(__self_0) =>
                ::core::fmt::Formatter::debug_struct_field1_finish(f,
                    "ReadPointerAsInt", &__self_0),
            Self::ReadPartialPointer(__self_0) =>
                ::core::fmt::Formatter::debug_struct_field1_finish(f,
                    "ReadPartialPointer", &__self_0),
            Self::InvalidUninitBytes(__self_0) =>
                ::core::fmt::Formatter::debug_struct_field1_finish(f,
                    "InvalidUninitBytes", &__self_0),
        }
    }
}Debug)]
306pub enum AllocError {
307    /// Encountered a pointer where we needed raw bytes.
308    ReadPointerAsInt(Option<BadBytesAccess>),
309    /// Partially copying a pointer.
310    ReadPartialPointer(Size),
311    /// Using uninitialized data where it is not allowed.
312    InvalidUninitBytes(Option<BadBytesAccess>),
313}
314pub type AllocResult<T = ()> = Result<T, AllocError>;
315
316impl AllocError {
317    pub fn to_interp_error<'tcx>(self, alloc_id: AllocId) -> InterpErrorKind<'tcx> {
318        use AllocError::*;
319        match self {
320            ReadPointerAsInt(info) => InterpErrorKind::Unsupported(
321                UnsupportedOpInfo::ReadPointerAsInt(info.map(|b| (alloc_id, b))),
322            ),
323            ReadPartialPointer(offset) => InterpErrorKind::Unsupported(
324                UnsupportedOpInfo::ReadPartialPointer(Pointer::new(alloc_id, offset)),
325            ),
326            InvalidUninitBytes(info) => InterpErrorKind::UndefinedBehavior(
327                UndefinedBehaviorInfo::InvalidUninitBytes(info.map(|b| (alloc_id, b))),
328            ),
329        }
330    }
331}
332
333/// The information that makes up a memory access: offset and size.
334#[derive(#[automatically_derived]impl ::core::fmt::Debug for AllocRange {
    #[inline]
    #[rustc_trivial_field_reads]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        ::core::fmt::Formatter::debug_struct_field2_finish(f,
            "AllocRange start size", &self.start, &&self.size)
    }
}Debug, #[automatically_derived]impl ::core::marker::Copy for AllocRange { }Copy, #[automatically_derived]#[doc(hidden)]unsafe impl ::core::clone::TrivialClone
    for AllocRange {
}
#[automatically_derived]impl ::core::clone::Clone for AllocRange {
    #[inline]
    #[rustc_trivial_field_reads]
    fn clone(&self) -> Self {
        let _: ::core::clone::AssertParamIsClone<Size>;
        *self
    }
}Clone, #[automatically_derived]impl ::core::marker::StructuralPartialEq for
    AllocRange {
}
#[automatically_derived]impl ::core::cmp::PartialEq for AllocRange {
    #[inline]
    fn eq(&self, other: &Self) -> bool {
        self.start == other.start && self.size == other.size
    }
}PartialEq, #[automatically_derived]impl ::core::cmp::Eq for AllocRange {
    #[inline]
    #[doc(hidden)]
    #[coverage(off)]
    fn assert_fields_are_eq(&self) {
        let _: ::core::cmp::AssertParamIsEq<Size>;
    }
}Eq)]
335pub struct AllocRange {
336    pub start: Size,
337    pub size: Size,
338}
339
340impl fmt::Display for AllocRange {
341    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
342        f.write_fmt(format_args!("[{0:#x}..{1:#x}]", self.start.bytes(),
        self.end().bytes()))write!(f, "[{:#x}..{:#x}]", self.start.bytes(), self.end().bytes())
343    }
344}
345
346/// Free-starting constructor for less syntactic overhead.
347#[inline(always)]
348pub fn alloc_range(start: Size, size: Size) -> AllocRange {
349    AllocRange { start, size }
350}
351
352impl From<Range<Size>> for AllocRange {
353    #[inline]
354    fn from(r: Range<Size>) -> Self {
355        alloc_range(r.start, r.end - r.start) // `Size` subtraction (overflow-checked)
356    }
357}
358
359impl From<Range<usize>> for AllocRange {
360    #[inline]
361    fn from(r: Range<usize>) -> Self {
362        AllocRange::from(Size::from_bytes(r.start)..Size::from_bytes(r.end))
363    }
364}
365
366impl AllocRange {
367    #[inline(always)]
368    pub fn end(self) -> Size {
369        self.start + self.size // This does overflow checking.
370    }
371
372    /// Returns the `subrange` within this range; panics if it is not a subrange.
373    #[inline]
374    pub fn subrange(self, subrange: AllocRange) -> AllocRange {
375        let sub_start = self.start + subrange.start;
376        let range = alloc_range(sub_start, subrange.size);
377        if !(range.end() <= self.end()) {
    {
        ::core::panicking::panic_fmt(format_args!("access outside the bounds for given AllocRange"));
    }
};assert!(range.end() <= self.end(), "access outside the bounds for given AllocRange");
378        range
379    }
380}
381
382/// Whether a new allocation should be initialized with zero-bytes.
383pub enum AllocInit {
384    Uninit,
385    Zero,
386}
387
388// The constructors are all without extra; the extra gets added by a machine hook later.
389impl<Prov: Provenance, Bytes: AllocBytes> Allocation<Prov, (), Bytes> {
390    /// Creates an allocation initialized by the given bytes
391    pub fn from_bytes<'a>(
392        slice: impl Into<Cow<'a, [u8]>>,
393        align: Align,
394        mutability: Mutability,
395        params: <Bytes as AllocBytes>::AllocParams,
396    ) -> Self {
397        let bytes = Bytes::from_bytes(slice, align, params);
398        let size = Size::from_bytes(bytes.len());
399        Self {
400            bytes,
401            provenance: ProvenanceMap::new(),
402            init_mask: InitMask::new(size, true),
403            align,
404            mutability,
405            extra: (),
406        }
407    }
408
409    pub fn from_bytes_byte_aligned_immutable<'a>(
410        slice: impl Into<Cow<'a, [u8]>>,
411        params: <Bytes as AllocBytes>::AllocParams,
412    ) -> Self {
413        Allocation::from_bytes(slice, Align::ONE, Mutability::Not, params)
414    }
415
416    fn new_inner<R>(
417        size: Size,
418        align: Align,
419        init: AllocInit,
420        params: <Bytes as AllocBytes>::AllocParams,
421        fail: impl FnOnce() -> R,
422    ) -> Result<Self, R> {
423        // We raise an error if we cannot create the allocation on the host.
424        // This results in an error that can happen non-deterministically, since the memory
425        // available to the compiler can change between runs. Normally queries are always
426        // deterministic. However, we can be non-deterministic here because all uses of const
427        // evaluation (including ConstProp!) will make compilation fail (via hard error
428        // or OOM) upon encountering a `MemoryExhausted` error.
429        let bytes = Bytes::zeroed(size, align, params).ok_or_else(fail)?;
430
431        Ok(Allocation {
432            bytes,
433            provenance: ProvenanceMap::new(),
434            init_mask: InitMask::new(
435                size,
436                match init {
437                    AllocInit::Uninit => false,
438                    AllocInit::Zero => true,
439                },
440            ),
441            align,
442            mutability: Mutability::Mut,
443            extra: (),
444        })
445    }
446
447    /// Try to create an Allocation of `size` bytes, failing if there is not enough memory
448    /// available to the compiler to do so.
449    pub fn try_new<'tcx>(
450        size: Size,
451        align: Align,
452        init: AllocInit,
453        params: <Bytes as AllocBytes>::AllocParams,
454    ) -> InterpResult<'tcx, Self> {
455        Self::new_inner(size, align, init, params, || {
456            ty::tls::with(|tcx| tcx.dcx().delayed_bug("exhausted memory during interpretation"));
457            InterpErrorKind::ResourceExhaustion(ResourceExhaustionInfo::MemoryExhausted)
458        })
459        .into()
460    }
461
462    /// Try to create an Allocation of `size` bytes. Aborts if there is not enough memory
463    /// available to the compiler to do so.
464    ///
465    /// Example use case: To obtain an Allocation filled with specific data,
466    /// first call this function and then call write_scalar to fill in the right data.
467    pub fn new(
468        size: Size,
469        align: Align,
470        init: AllocInit,
471        params: <Bytes as AllocBytes>::AllocParams,
472    ) -> Self {
473        match Self::new_inner(size, align, init, params, || {
474            // `size` may actually be bigger than isize::MAX since it is a *target* size.
475            // Clamp it to isize::MAX to still give a somewhat reasonable error message.
476            alloc::handle_alloc_error(
477                Layout::from_size_align(
478                    size.bytes().min(isize::MAX as u64) as usize,
479                    align.bytes_usize(),
480                )
481                .unwrap(),
482            )
483        }) {
484            Ok(x) => x,
485            Err(x) => x,
486        }
487    }
488
489    /// Add the extra.
490    pub fn with_extra<Extra>(self, extra: Extra) -> Allocation<Prov, Extra, Bytes> {
491        Allocation {
492            bytes: self.bytes,
493            provenance: self.provenance,
494            init_mask: self.init_mask,
495            align: self.align,
496            mutability: self.mutability,
497            extra,
498        }
499    }
500}
501
502impl Allocation {
503    /// Adjust allocation from the ones in `tcx` to a custom Machine instance
504    /// with a different `Provenance` and `Byte` type.
505    pub fn adjust_from_tcx<'tcx, Prov: Provenance, Bytes: AllocBytes>(
506        &self,
507        cx: &impl HasDataLayout,
508        alloc_bytes: impl FnOnce(&[u8], Align) -> InterpResult<'tcx, Bytes>,
509        mut adjust_ptr: impl FnMut(Pointer<CtfeProvenance>) -> InterpResult<'tcx, Pointer<Prov>>,
510    ) -> InterpResult<'tcx, Allocation<Prov, (), Bytes>> {
511        // Copy the data.
512        let mut bytes = alloc_bytes(&*self.bytes, self.align)?;
513        // Adjust provenance of pointers stored in this allocation.
514        let mut new_provenance = Vec::with_capacity(self.provenance.ptrs().len());
515        let ptr_size = cx.data_layout().pointer_size().bytes_usize();
516        let endian = cx.data_layout().endian;
517        for &(offset, alloc_id) in self.provenance.ptrs().iter() {
518            let idx = offset.bytes_usize();
519            let ptr_bytes = &mut bytes[idx..idx + ptr_size];
520            let bits = read_target_uint(endian, ptr_bytes).unwrap();
521            let (ptr_prov, ptr_offset) =
522                adjust_ptr(Pointer::new(alloc_id, Size::from_bytes(bits)))?.into_raw_parts();
523            write_target_uint(endian, ptr_bytes, ptr_offset.bytes().into()).unwrap();
524            new_provenance.push((offset, ptr_prov));
525        }
526        // Create allocation.
527        interp_ok(Allocation {
528            bytes,
529            provenance: ProvenanceMap::from_presorted_ptrs(new_provenance),
530            init_mask: self.init_mask.clone(),
531            align: self.align,
532            mutability: self.mutability,
533            extra: self.extra,
534        })
535    }
536}
537
538/// Raw accessors. Provide access to otherwise private bytes.
539impl<Prov: Provenance, Extra, Bytes: AllocBytes> Allocation<Prov, Extra, Bytes> {
540    pub fn len(&self) -> usize {
541        self.bytes.len()
542    }
543
544    pub fn size(&self) -> Size {
545        Size::from_bytes(self.len())
546    }
547
548    /// Looks at a slice which may contain uninitialized bytes or provenance. This differs
549    /// from `get_bytes_with_uninit_and_ptr` in that it does no provenance checks (even on the
550    /// edges) at all.
551    /// This must not be used for reads affecting the interpreter execution.
552    pub fn inspect_with_uninit_and_ptr_outside_interpreter(&self, range: Range<usize>) -> &[u8] {
553        &self.bytes[range]
554    }
555
556    /// Returns the mask indicating which bytes are initialized.
557    pub fn init_mask(&self) -> &InitMask {
558        &self.init_mask
559    }
560
561    /// Returns the provenance map.
562    pub fn provenance(&self) -> &ProvenanceMap<Prov> {
563        &self.provenance
564    }
565}
566
567/// Byte accessors.
568impl<Prov: Provenance, Extra, Bytes: AllocBytes> Allocation<Prov, Extra, Bytes> {
569    /// This is the entirely abstraction-violating way to just grab the raw bytes without
570    /// caring about provenance or initialization.
571    ///
572    /// This function also guarantees that the resulting pointer will remain stable
573    /// even when new allocations are pushed to the `HashMap`. `mem_copy_repeatedly` relies
574    /// on that.
575    #[inline]
576    pub fn get_bytes_unchecked(&self, range: AllocRange) -> &[u8] {
577        &self.bytes[range.start.bytes_usize()..range.end().bytes_usize()]
578    }
579
580    /// Checks that these bytes are initialized, and then strip provenance (if possible) and return
581    /// them.
582    ///
583    /// It is the caller's responsibility to check bounds and alignment beforehand.
584    /// Most likely, you want to use the `PlaceTy` and `OperandTy`-based methods
585    /// on `InterpCx` instead.
586    #[inline]
587    pub fn get_bytes_strip_provenance(
588        &self,
589        cx: &impl HasDataLayout,
590        range: AllocRange,
591    ) -> AllocResult<&[u8]> {
592        self.init_mask.is_range_initialized(range).map_err(|uninit_range| {
593            AllocError::InvalidUninitBytes(Some(BadBytesAccess {
594                access: range,
595                bad: uninit_range,
596            }))
597        })?;
598        if !Prov::OFFSET_IS_ADDR && !self.provenance.range_empty(range, cx) {
599            // Find the provenance.
600            let (prov_range, _prov) = self
601                .provenance
602                .get_range(range, cx)
603                .next()
604                .expect("there must be provenance somewhere here");
605            let start = prov_range.start.max(range.start); // the pointer might begin before `range`!
606            let end = prov_range.end().min(range.end()); // the pointer might end after `range`!
607            return Err(AllocError::ReadPointerAsInt(Some(BadBytesAccess {
608                access: range,
609                bad: AllocRange::from(start..end),
610            })));
611        }
612        Ok(self.get_bytes_unchecked(range))
613    }
614
615    /// This is the entirely abstraction-violating way to just get mutable access to the raw bytes.
616    /// Just calling this already marks everything as defined and removes provenance, so be sure to
617    /// actually overwrite all the data there!
618    ///
619    /// It is the caller's responsibility to check bounds and alignment beforehand.
620    /// Most likely, you want to use the `PlaceTy` and `OperandTy`-based methods
621    /// on `InterpCx` instead.
622    pub fn get_bytes_unchecked_for_overwrite(
623        &mut self,
624        cx: &impl HasDataLayout,
625        range: AllocRange,
626    ) -> &mut [u8] {
627        self.mark_init(range, true);
628        self.provenance.clear(range, &self.bytes, cx);
629
630        &mut self.bytes[range.start.bytes_usize()..range.end().bytes_usize()]
631    }
632
633    /// A raw pointer variant of `get_bytes_unchecked_for_overwrite` that avoids invalidating existing immutable aliases
634    /// into this memory.
635    pub fn get_bytes_unchecked_for_overwrite_ptr(
636        &mut self,
637        cx: &impl HasDataLayout,
638        range: AllocRange,
639    ) -> *mut [u8] {
640        self.mark_init(range, true);
641        self.provenance.clear(range, &self.bytes, cx);
642
643        if !(range.end().bytes_usize() <= self.bytes.len()) {
    ::core::panicking::panic("assertion failed: range.end().bytes_usize() <= self.bytes.len()")
};assert!(range.end().bytes_usize() <= self.bytes.len()); // need to do our own bounds-check
644        // Crucially, we go via `AllocBytes::as_mut_ptr`, not `AllocBytes::deref_mut`.
645        let begin_ptr = self.bytes.as_mut_ptr().wrapping_add(range.start.bytes_usize());
646        let len = range.end().bytes_usize() - range.start.bytes_usize();
647        ptr::slice_from_raw_parts_mut(begin_ptr, len)
648    }
649
650    /// This gives direct mutable access to the entire buffer, just exposing their internal state
651    /// without resetting anything. Directly exposes `AllocBytes::as_mut_ptr`. Only works if
652    /// `OFFSET_IS_ADDR` is true.
653    pub fn get_bytes_unchecked_raw_mut(&mut self) -> *mut u8 {
654        if !Prov::OFFSET_IS_ADDR {
    ::core::panicking::panic("assertion failed: Prov::OFFSET_IS_ADDR")
};assert!(Prov::OFFSET_IS_ADDR);
655        self.bytes.as_mut_ptr()
656    }
657
658    /// This gives direct immutable access to the entire buffer, just exposing their internal state
659    /// without resetting anything. Directly exposes `AllocBytes::as_ptr`. Only works if
660    /// `OFFSET_IS_ADDR` is true.
661    pub fn get_bytes_unchecked_raw(&self) -> *const u8 {
662        if !Prov::OFFSET_IS_ADDR {
    ::core::panicking::panic("assertion failed: Prov::OFFSET_IS_ADDR")
};assert!(Prov::OFFSET_IS_ADDR);
663        self.bytes.as_ptr()
664    }
665}
666
667/// Reading and writing.
668impl<Prov: Provenance, Extra, Bytes: AllocBytes> Allocation<Prov, Extra, Bytes> {
669    /// Sets the init bit for the given range.
670    fn mark_init(&mut self, range: AllocRange, is_init: bool) {
671        if range.size.bytes() == 0 {
672            return;
673        }
674        if !(self.mutability == Mutability::Mut) {
    ::core::panicking::panic("assertion failed: self.mutability == Mutability::Mut")
};assert!(self.mutability == Mutability::Mut);
675        self.init_mask.set_range(range, is_init);
676    }
677
678    /// Reads a *non-ZST* scalar.
679    ///
680    /// If `read_provenance` is `true`, this will also read provenance; otherwise (if the machine
681    /// supports that) provenance is entirely ignored.
682    ///
683    /// ZSTs can't be read because in order to obtain a `Pointer`, we need to check
684    /// for ZSTness anyway due to integer pointers being valid for ZSTs.
685    ///
686    /// It is the caller's responsibility to check bounds and alignment beforehand.
687    /// Most likely, you want to call `InterpCx::read_scalar` instead of this method.
688    pub fn read_scalar(
689        &self,
690        cx: &impl HasDataLayout,
691        range: AllocRange,
692        read_provenance: bool,
693    ) -> AllocResult<Scalar<Prov>> {
694        // First and foremost, if anything is uninit, bail.
695        if let Err(bad) = self.init_mask.is_range_initialized(range) {
696            return Err(AllocError::InvalidUninitBytes(Some(BadBytesAccess {
697                access: range,
698                bad,
699            })));
700        }
701
702        // Get the integer part of the result. We HAVE TO check provenance before returning this!
703        let bytes = self.get_bytes_unchecked(range);
704        let bits = read_target_uint(cx.data_layout().endian, bytes).unwrap();
705
706        if read_provenance {
707            {
    match (&range.size, &cx.data_layout().pointer_size()) {
        (left_val, right_val) => {
            if !(*left_val == *right_val) {
                let kind = ::core::panicking::AssertKind::Eq;
                ::core::panicking::assert_failed(kind, &*left_val,
                    &*right_val, ::core::option::Option::None);
            }
        }
    }
};assert_eq!(range.size, cx.data_layout().pointer_size());
708
709            if let Some(prov) = self.provenance.read_ptr(range.start, cx)? {
710                // Assemble the bits with their provenance.
711                let ptr = Pointer::new(prov, Size::from_bytes(bits));
712                Ok(Scalar::from_pointer(ptr, cx))
713            } else {
714                // Return raw bits without provenance.
715                Ok(Scalar::from_uint(bits, range.size))
716            }
717        } else {
718            // We are *not* reading a pointer.
719            // If we can just ignore provenance or there is none, that's easy.
720            if Prov::OFFSET_IS_ADDR || self.provenance.range_empty(range, cx) {
721                // We just strip provenance.
722                return Ok(Scalar::from_uint(bits, range.size));
723            }
724            // There is some provenance and we don't have OFFSET_IS_ADDR. This doesn't work.
725            return Err(AllocError::ReadPointerAsInt(None));
726        }
727    }
728
729    /// Writes a *non-ZST* scalar.
730    ///
731    /// ZSTs can't be read because in order to obtain a `Pointer`, we need to check
732    /// for ZSTness anyway due to integer pointers being valid for ZSTs.
733    ///
734    /// It is the caller's responsibility to check bounds and alignment beforehand.
735    /// Most likely, you want to call `InterpCx::write_scalar` instead of this method.
736    pub fn write_scalar(
737        &mut self,
738        cx: &impl HasDataLayout,
739        range: AllocRange,
740        val: Scalar<Prov>,
741    ) -> AllocResult {
742        if !(self.mutability == Mutability::Mut) {
    ::core::panicking::panic("assertion failed: self.mutability == Mutability::Mut")
};assert!(self.mutability == Mutability::Mut);
743
744        // `to_bits_or_ptr_internal` is the right method because we just want to store this data
745        // as-is into memory. This also double-checks that `val.size()` matches `range.size`.
746        let (bytes, provenance) = match val.to_bits_or_ptr_internal(range.size) {
747            Right(ptr) => {
748                let (provenance, offset) = ptr.into_raw_parts();
749                (u128::from(offset.bytes()), Some(provenance))
750            }
751            Left(data) => (data, None),
752        };
753
754        let endian = cx.data_layout().endian;
755        // Yes we do overwrite all the bytes in `dst`.
756        let dst = self.get_bytes_unchecked_for_overwrite(cx, range);
757        write_target_uint(endian, dst, bytes).unwrap();
758
759        // See if we have to also store some provenance.
760        if let Some(provenance) = provenance {
761            {
    match (&range.size, &cx.data_layout().pointer_size()) {
        (left_val, right_val) => {
            if !(*left_val == *right_val) {
                let kind = ::core::panicking::AssertKind::Eq;
                ::core::panicking::assert_failed(kind, &*left_val,
                    &*right_val, ::core::option::Option::None);
            }
        }
    }
};assert_eq!(range.size, cx.data_layout().pointer_size());
762            self.provenance.insert_ptr(range.start, provenance, cx);
763        }
764
765        Ok(())
766    }
767
768    /// Write "uninit" to the given memory range.
769    pub fn write_uninit(&mut self, cx: &impl HasDataLayout, range: AllocRange) {
770        self.mark_init(range, false);
771        self.provenance.clear(range, &self.bytes, cx);
772    }
773
774    /// Mark all bytes in the given range as initialised and reset the provenance
775    /// to wildcards. This entirely breaks the normal mechanisms for tracking
776    /// initialisation and is only provided for Miri operating in native-lib
777    /// mode. UB will be missed if the underlying bytes were not actually written to.
778    ///
779    /// If `range` is `None`, defaults to performing this on the whole allocation.
780    pub fn process_native_write(&mut self, cx: &impl HasDataLayout, range: Option<AllocRange>) {
781        let range = range.unwrap_or_else(|| AllocRange {
782            start: Size::ZERO,
783            size: Size::from_bytes(self.len()),
784        });
785        self.mark_init(range, true);
786        self.provenance.write_wildcards(cx, &self.bytes, range);
787    }
788
789    /// Remove all provenance in the given memory range.
790    pub fn clear_provenance(&mut self, cx: &impl HasDataLayout, range: AllocRange) {
791        self.provenance.clear(range, &self.bytes, cx);
792    }
793
794    pub fn provenance_merge_bytes(&mut self, cx: &impl HasDataLayout) -> bool {
795        self.provenance.merge_bytes(cx)
796    }
797
798    pub fn provenance_prepare_copy(
799        &self,
800        range: AllocRange,
801        cx: &impl HasDataLayout,
802    ) -> ProvenanceCopy<Prov> {
803        self.provenance.prepare_copy(range, &self.bytes, cx)
804    }
805
806    /// Applies a previously prepared provenance copy.
807    /// The affected range is expected to be clear of provenance.
808    ///
809    /// This is dangerous to use as it can violate internal `Allocation` invariants!
810    /// It only exists to support an efficient implementation of `mem_copy_repeatedly`.
811    pub fn provenance_apply_copy(
812        &mut self,
813        copy: ProvenanceCopy<Prov>,
814        range: AllocRange,
815        repeat: u64,
816    ) {
817        self.provenance.apply_copy(copy, range, repeat)
818    }
819
820    /// Applies a previously prepared copy of the init mask.
821    ///
822    /// This is dangerous to use as it can violate internal `Allocation` invariants!
823    /// It only exists to support an efficient implementation of `mem_copy_repeatedly`.
824    pub fn init_mask_apply_copy(&mut self, copy: InitCopy, range: AllocRange, repeat: u64) {
825        self.init_mask.apply_copy(copy, range, repeat)
826    }
827}