Skip to main content

rustc_const_eval/interpret/
memory.rs

1//! The memory subsystem.
2//!
3//! Generally, we use `Pointer` to denote memory addresses. However, some operations
4//! have a "size"-like parameter, and they take `Scalar` for the address because
5//! if the size is 0, then the pointer can also be a (properly aligned, non-null)
6//! integer. It is crucial that these operations call `check_align` *before*
7//! short-circuiting the empty case!
8
9use std::borrow::{Borrow, Cow};
10use std::cell::Cell;
11use std::collections::VecDeque;
12use std::{fmt, ptr};
13
14use rustc_abi::{Align, HasDataLayout, Size};
15use rustc_ast::Mutability;
16use rustc_data_structures::assert_matches;
17use rustc_data_structures::fx::{FxHashSet, FxIndexMap};
18use rustc_errors::msg;
19use rustc_middle::mir::display_allocation;
20use rustc_middle::ty::{self, Instance, Ty, TyCtxt};
21use rustc_middle::{bug, throw_ub_format};
22use tracing::{debug, instrument, trace};
23
24use super::{
25    AllocBytes, AllocId, AllocInit, AllocMap, AllocRange, Allocation, CheckAlignMsg,
26    CheckInAllocMsg, CtfeProvenance, GlobalAlloc, InterpCx, InterpResult, MPlaceTy, Machine,
27    MayLeak, Misalignment, Pointer, PointerArithmetic, Provenance, Scalar, alloc_range, err_ub,
28    err_ub_custom, interp_ok, throw_ub, throw_ub_custom, throw_unsup, throw_unsup_format,
29};
30use crate::const_eval::ConstEvalErrKind;
31
32#[derive(#[automatically_derived]
impl<T: ::core::fmt::Debug> ::core::fmt::Debug for MemoryKind<T> {
    #[inline]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        match self {
            MemoryKind::Stack =>
                ::core::fmt::Formatter::write_str(f, "Stack"),
            MemoryKind::CallerLocation =>
                ::core::fmt::Formatter::write_str(f, "CallerLocation"),
            MemoryKind::Machine(__self_0) =>
                ::core::fmt::Formatter::debug_tuple_field1_finish(f,
                    "Machine", &__self_0),
        }
    }
}Debug, #[automatically_derived]
impl<T: ::core::cmp::PartialEq> ::core::cmp::PartialEq for MemoryKind<T> {
    #[inline]
    fn eq(&self, other: &MemoryKind<T>) -> bool {
        let __self_discr = ::core::intrinsics::discriminant_value(self);
        let __arg1_discr = ::core::intrinsics::discriminant_value(other);
        __self_discr == __arg1_discr &&
            match (self, other) {
                (MemoryKind::Machine(__self_0), MemoryKind::Machine(__arg1_0))
                    => __self_0 == __arg1_0,
                _ => true,
            }
    }
}PartialEq, #[automatically_derived]
impl<T: ::core::marker::Copy> ::core::marker::Copy for MemoryKind<T> { }Copy, #[automatically_derived]
impl<T: ::core::clone::Clone> ::core::clone::Clone for MemoryKind<T> {
    #[inline]
    fn clone(&self) -> MemoryKind<T> {
        match self {
            MemoryKind::Stack => MemoryKind::Stack,
            MemoryKind::CallerLocation => MemoryKind::CallerLocation,
            MemoryKind::Machine(__self_0) =>
                MemoryKind::Machine(::core::clone::Clone::clone(__self_0)),
        }
    }
}Clone)]
33pub enum MemoryKind<T> {
34    /// Stack memory. Error if deallocated except during a stack pop.
35    Stack,
36    /// Memory allocated by `caller_location` intrinsic. Error if ever deallocated.
37    CallerLocation,
38    /// Additional memory kinds a machine wishes to distinguish from the builtin ones.
39    Machine(T),
40}
41
42impl<T: MayLeak> MayLeak for MemoryKind<T> {
43    #[inline]
44    fn may_leak(self) -> bool {
45        match self {
46            MemoryKind::Stack => false,
47            MemoryKind::CallerLocation => true,
48            MemoryKind::Machine(k) => k.may_leak(),
49        }
50    }
51}
52
53impl<T: fmt::Display> fmt::Display for MemoryKind<T> {
54    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
55        match self {
56            MemoryKind::Stack => f.write_fmt(format_args!("stack variable"))write!(f, "stack variable"),
57            MemoryKind::CallerLocation => f.write_fmt(format_args!("caller location"))write!(f, "caller location"),
58            MemoryKind::Machine(m) => f.write_fmt(format_args!("{0}", m))write!(f, "{m}"),
59        }
60    }
61}
62
63/// The return value of `get_alloc_info` indicates the "kind" of the allocation.
64#[derive(#[automatically_derived]
impl ::core::marker::Copy for AllocKind { }Copy, #[automatically_derived]
impl ::core::clone::Clone for AllocKind {
    #[inline]
    fn clone(&self) -> AllocKind { *self }
}Clone, #[automatically_derived]
impl ::core::cmp::PartialEq for AllocKind {
    #[inline]
    fn eq(&self, other: &AllocKind) -> bool {
        let __self_discr = ::core::intrinsics::discriminant_value(self);
        let __arg1_discr = ::core::intrinsics::discriminant_value(other);
        __self_discr == __arg1_discr
    }
}PartialEq, #[automatically_derived]
impl ::core::fmt::Debug for AllocKind {
    #[inline]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        ::core::fmt::Formatter::write_str(f,
            match self {
                AllocKind::LiveData => "LiveData",
                AllocKind::Function => "Function",
                AllocKind::VaList => "VaList",
                AllocKind::VTable => "VTable",
                AllocKind::TypeId => "TypeId",
                AllocKind::Dead => "Dead",
            })
    }
}Debug)]
65pub enum AllocKind {
66    /// A regular live data allocation.
67    LiveData,
68    /// A function allocation (that fn ptrs point to).
69    Function,
70    /// A variable argument list allocation (used by c-variadic functions).
71    VaList,
72    /// A vtable allocation.
73    VTable,
74    /// A TypeId allocation.
75    TypeId,
76    /// A dead allocation.
77    Dead,
78}
79
80/// Metadata about an `AllocId`.
81#[derive(#[automatically_derived]
impl ::core::marker::Copy for AllocInfo { }Copy, #[automatically_derived]
impl ::core::clone::Clone for AllocInfo {
    #[inline]
    fn clone(&self) -> AllocInfo {
        let _: ::core::clone::AssertParamIsClone<Size>;
        let _: ::core::clone::AssertParamIsClone<Align>;
        let _: ::core::clone::AssertParamIsClone<AllocKind>;
        let _: ::core::clone::AssertParamIsClone<Mutability>;
        *self
    }
}Clone, #[automatically_derived]
impl ::core::cmp::PartialEq for AllocInfo {
    #[inline]
    fn eq(&self, other: &AllocInfo) -> bool {
        self.size == other.size && self.align == other.align &&
                self.kind == other.kind && self.mutbl == other.mutbl
    }
}PartialEq, #[automatically_derived]
impl ::core::fmt::Debug for AllocInfo {
    #[inline]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        ::core::fmt::Formatter::debug_struct_field4_finish(f, "AllocInfo",
            "size", &self.size, "align", &self.align, "kind", &self.kind,
            "mutbl", &&self.mutbl)
    }
}Debug)]
82pub struct AllocInfo {
83    pub size: Size,
84    pub align: Align,
85    pub kind: AllocKind,
86    pub mutbl: Mutability,
87}
88
89impl AllocInfo {
90    fn new(size: Size, align: Align, kind: AllocKind, mutbl: Mutability) -> Self {
91        Self { size, align, kind, mutbl }
92    }
93}
94
95/// The value of a function pointer.
96#[derive(#[automatically_derived]
impl<'tcx, Other: ::core::fmt::Debug> ::core::fmt::Debug for
    FnVal<'tcx, Other> {
    #[inline]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        match self {
            FnVal::Instance(__self_0) =>
                ::core::fmt::Formatter::debug_tuple_field1_finish(f,
                    "Instance", &__self_0),
            FnVal::Other(__self_0) =>
                ::core::fmt::Formatter::debug_tuple_field1_finish(f, "Other",
                    &__self_0),
        }
    }
}Debug, #[automatically_derived]
impl<'tcx, Other: ::core::marker::Copy> ::core::marker::Copy for
    FnVal<'tcx, Other> {
}Copy, #[automatically_derived]
impl<'tcx, Other: ::core::clone::Clone> ::core::clone::Clone for
    FnVal<'tcx, Other> {
    #[inline]
    fn clone(&self) -> FnVal<'tcx, Other> {
        match self {
            FnVal::Instance(__self_0) =>
                FnVal::Instance(::core::clone::Clone::clone(__self_0)),
            FnVal::Other(__self_0) =>
                FnVal::Other(::core::clone::Clone::clone(__self_0)),
        }
    }
}Clone)]
97pub enum FnVal<'tcx, Other> {
98    Instance(Instance<'tcx>),
99    Other(Other),
100}
101
102impl<'tcx, Other> FnVal<'tcx, Other> {
103    pub fn as_instance(self) -> InterpResult<'tcx, Instance<'tcx>> {
104        match self {
105            FnVal::Instance(instance) => interp_ok(instance),
106            FnVal::Other(_) => {
107                do yeet ::rustc_middle::mir::interpret::InterpErrorKind::Unsupported(::rustc_middle::mir::interpret::UnsupportedOpInfo::Unsupported(::alloc::__export::must_use({
                    ::alloc::fmt::format(format_args!("\'foreign\' function pointers are not supported in this context"))
                })))throw_unsup_format!("'foreign' function pointers are not supported in this context")
108            }
109        }
110    }
111}
112
113// `Memory` has to depend on the `Machine` because some of its operations
114// (e.g., `get`) call a `Machine` hook.
115pub struct Memory<'tcx, M: Machine<'tcx>> {
116    /// Allocations local to this instance of the interpreter. The kind
117    /// helps ensure that the same mechanism is used for allocation and
118    /// deallocation. When an allocation is not found here, it is a
119    /// global and looked up in the `tcx` for read access. Some machines may
120    /// have to mutate this map even on a read-only access to a global (because
121    /// they do pointer provenance tracking and the allocations in `tcx` have
122    /// the wrong type), so we let the machine override this type.
123    /// Either way, if the machine allows writing to a global, doing so will
124    /// create a copy of the global allocation here.
125    // FIXME: this should not be public, but interning currently needs access to it
126    pub(super) alloc_map: M::MemoryMap,
127
128    /// Map for "extra" function pointers.
129    extra_fn_ptr_map: FxIndexMap<AllocId, M::ExtraFnVal>,
130
131    /// Map storing variable argument lists.
132    va_list_map: FxIndexMap<AllocId, VecDeque<MPlaceTy<'tcx, M::Provenance>>>,
133
134    /// To be able to compare pointers with null, and to check alignment for accesses
135    /// to ZSTs (where pointers may dangle), we keep track of the size even for allocations
136    /// that do not exist any more.
137    // FIXME: this should not be public, but interning currently needs access to it
138    pub(super) dead_alloc_map: FxIndexMap<AllocId, (Size, Align)>,
139
140    /// This stores whether we are currently doing reads purely for the purpose of validation.
141    /// Those reads do not trigger the machine's hooks for memory reads.
142    /// Needless to say, this must only be set with great care!
143    validation_in_progress: Cell<bool>,
144}
145
146/// A reference to some allocation that was already bounds-checked for the given region
147/// and had the on-access machine hooks run.
148#[derive(#[automatically_derived]
impl<'a, 'tcx, Prov: ::core::marker::Copy + Provenance,
    Extra: ::core::marker::Copy, Bytes: ::core::marker::Copy + AllocBytes>
    ::core::marker::Copy for AllocRef<'a, 'tcx, Prov, Extra, Bytes> {
}Copy, #[automatically_derived]
impl<'a, 'tcx, Prov: ::core::clone::Clone + Provenance,
    Extra: ::core::clone::Clone, Bytes: ::core::clone::Clone + AllocBytes>
    ::core::clone::Clone for AllocRef<'a, 'tcx, Prov, Extra, Bytes> {
    #[inline]
    fn clone(&self) -> AllocRef<'a, 'tcx, Prov, Extra, Bytes> {
        AllocRef {
            alloc: ::core::clone::Clone::clone(&self.alloc),
            range: ::core::clone::Clone::clone(&self.range),
            tcx: ::core::clone::Clone::clone(&self.tcx),
            alloc_id: ::core::clone::Clone::clone(&self.alloc_id),
        }
    }
}Clone)]
149pub struct AllocRef<'a, 'tcx, Prov: Provenance, Extra, Bytes: AllocBytes = Box<[u8]>> {
150    alloc: &'a Allocation<Prov, Extra, Bytes>,
151    range: AllocRange,
152    tcx: TyCtxt<'tcx>,
153    alloc_id: AllocId,
154}
155/// A reference to some allocation that was already bounds-checked for the given region
156/// and had the on-access machine hooks run.
157pub struct AllocRefMut<'a, 'tcx, Prov: Provenance, Extra, Bytes: AllocBytes = Box<[u8]>> {
158    alloc: &'a mut Allocation<Prov, Extra, Bytes>,
159    range: AllocRange,
160    tcx: TyCtxt<'tcx>,
161    alloc_id: AllocId,
162}
163
164impl<'tcx, M: Machine<'tcx>> Memory<'tcx, M> {
165    pub fn new() -> Self {
166        Memory {
167            alloc_map: M::MemoryMap::default(),
168            extra_fn_ptr_map: FxIndexMap::default(),
169            va_list_map: FxIndexMap::default(),
170            dead_alloc_map: FxIndexMap::default(),
171            validation_in_progress: Cell::new(false),
172        }
173    }
174
175    /// This is used by [priroda](https://github.com/oli-obk/priroda)
176    pub fn alloc_map(&self) -> &M::MemoryMap {
177        &self.alloc_map
178    }
179}
180
181impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
182    /// Call this to turn untagged "global" pointers (obtained via `tcx`) into
183    /// the machine pointer to the allocation. Must never be used
184    /// for any other pointers, nor for TLS statics.
185    ///
186    /// Using the resulting pointer represents a *direct* access to that memory
187    /// (e.g. by directly using a `static`),
188    /// as opposed to access through a pointer that was created by the program.
189    ///
190    /// This function can fail only if `ptr` points to an `extern static`.
191    #[inline]
192    pub fn global_root_pointer(
193        &self,
194        ptr: Pointer<CtfeProvenance>,
195    ) -> InterpResult<'tcx, Pointer<M::Provenance>> {
196        let alloc_id = ptr.provenance.alloc_id();
197        // We need to handle `extern static`.
198        match self.tcx.try_get_global_alloc(alloc_id) {
199            Some(GlobalAlloc::Static(def_id)) if self.tcx.is_thread_local_static(def_id) => {
200                // Thread-local statics do not have a constant address. They *must* be accessed via
201                // `ThreadLocalRef`; we can never have a pointer to them as a regular constant value.
202                ::rustc_middle::util::bug::bug_fmt(format_args!("global memory cannot point to thread-local static"))bug!("global memory cannot point to thread-local static")
203            }
204            Some(GlobalAlloc::Static(def_id)) if self.tcx.is_foreign_item(def_id) => {
205                return M::extern_static_pointer(self, def_id);
206            }
207            None => {
208                let is_fn_ptr = self.memory.extra_fn_ptr_map.contains_key(&alloc_id);
209                let is_va_list = self.memory.va_list_map.contains_key(&alloc_id);
210                if !(is_fn_ptr || is_va_list) {
    {
        ::core::panicking::panic_fmt(format_args!("{0:?} is neither global, va_list nor a function pointer",
                alloc_id));
    }
};assert!(
211                    is_fn_ptr || is_va_list,
212                    "{alloc_id:?} is neither global, va_list nor a function pointer"
213                );
214            }
215            _ => {}
216        }
217        // And we need to get the provenance.
218        M::adjust_alloc_root_pointer(self, ptr, M::GLOBAL_KIND.map(MemoryKind::Machine))
219    }
220
221    pub fn fn_ptr(&mut self, fn_val: FnVal<'tcx, M::ExtraFnVal>) -> Pointer<M::Provenance> {
222        let id = match fn_val {
223            FnVal::Instance(instance) => {
224                let salt = M::get_global_alloc_salt(self, Some(instance));
225                self.tcx.reserve_and_set_fn_alloc(instance, salt)
226            }
227            FnVal::Other(extra) => {
228                // FIXME(RalfJung): Should we have a cache here?
229                let id = self.tcx.reserve_alloc_id();
230                let old = self.memory.extra_fn_ptr_map.insert(id, extra);
231                if !old.is_none() {
    ::core::panicking::panic("assertion failed: old.is_none()")
};assert!(old.is_none());
232                id
233            }
234        };
235        // Functions are global allocations, so make sure we get the right root pointer.
236        // We know this is not an `extern static` so this cannot fail.
237        self.global_root_pointer(Pointer::from(id)).unwrap()
238    }
239
240    /// Insert a new variable argument list in the global map of variable argument lists.
241    pub fn va_list_ptr(
242        &mut self,
243        varargs: VecDeque<MPlaceTy<'tcx, M::Provenance>>,
244    ) -> Pointer<M::Provenance> {
245        let id = self.tcx.reserve_alloc_id();
246        let old = self.memory.va_list_map.insert(id, varargs);
247        if !old.is_none() {
    ::core::panicking::panic("assertion failed: old.is_none()")
};assert!(old.is_none());
248        // Variable argument lists are global allocations, so make sure we get the right root
249        // pointer. We know this is not an `extern static` so this cannot fail.
250        self.global_root_pointer(Pointer::from(id)).unwrap()
251    }
252
253    pub fn allocate_ptr(
254        &mut self,
255        size: Size,
256        align: Align,
257        kind: MemoryKind<M::MemoryKind>,
258        init: AllocInit,
259    ) -> InterpResult<'tcx, Pointer<M::Provenance>> {
260        let params = self.machine.get_default_alloc_params();
261        let alloc = if M::PANIC_ON_ALLOC_FAIL {
262            Allocation::new(size, align, init, params)
263        } else {
264            Allocation::try_new(size, align, init, params)?
265        };
266        self.insert_allocation(alloc, kind)
267    }
268
269    pub fn allocate_bytes_ptr(
270        &mut self,
271        bytes: &[u8],
272        align: Align,
273        kind: MemoryKind<M::MemoryKind>,
274        mutability: Mutability,
275    ) -> InterpResult<'tcx, Pointer<M::Provenance>> {
276        let params = self.machine.get_default_alloc_params();
277        let alloc = Allocation::from_bytes(bytes, align, mutability, params);
278        self.insert_allocation(alloc, kind)
279    }
280
281    pub fn insert_allocation(
282        &mut self,
283        alloc: Allocation<M::Provenance, (), M::Bytes>,
284        kind: MemoryKind<M::MemoryKind>,
285    ) -> InterpResult<'tcx, Pointer<M::Provenance>> {
286        if !(alloc.size() <= self.max_size_of_val()) {
    ::core::panicking::panic("assertion failed: alloc.size() <= self.max_size_of_val()")
};assert!(alloc.size() <= self.max_size_of_val());
287        let id = self.tcx.reserve_alloc_id();
288        if true {
    match (&(Some(kind)), &(M::GLOBAL_KIND.map(MemoryKind::Machine))) {
        (left_val, right_val) => {
            if *left_val == *right_val {
                let kind = ::core::panicking::AssertKind::Ne;
                ::core::panicking::assert_failed(kind, &*left_val,
                    &*right_val,
                    ::core::option::Option::Some(format_args!("dynamically allocating global memory")));
            }
        }
    };
};debug_assert_ne!(
289            Some(kind),
290            M::GLOBAL_KIND.map(MemoryKind::Machine),
291            "dynamically allocating global memory"
292        );
293        // This cannot be merged with the `adjust_global_allocation` code path
294        // since here we have an allocation that already uses `M::Bytes`.
295        let extra = M::init_local_allocation(self, id, kind, alloc.size(), alloc.align)?;
296        let alloc = alloc.with_extra(extra);
297        self.memory.alloc_map.insert(id, (kind, alloc));
298        M::adjust_alloc_root_pointer(self, Pointer::from(id), Some(kind))
299    }
300
301    /// If this grows the allocation, `init_growth` determines
302    /// whether the additional space will be initialized.
303    pub fn reallocate_ptr(
304        &mut self,
305        ptr: Pointer<Option<M::Provenance>>,
306        old_size_and_align: Option<(Size, Align)>,
307        new_size: Size,
308        new_align: Align,
309        kind: MemoryKind<M::MemoryKind>,
310        init_growth: AllocInit,
311    ) -> InterpResult<'tcx, Pointer<M::Provenance>> {
312        let (alloc_id, offset, _prov) = self.ptr_get_alloc_id(ptr, 0)?;
313        if offset.bytes() != 0 {
314            do yeet {
        let (ptr, kind) =
            (::alloc::__export::must_use({
                        ::alloc::fmt::format(format_args!("{0:?}", ptr))
                    }), "realloc");
        ::rustc_middle::mir::interpret::InterpErrorKind::UndefinedBehavior(::rustc_middle::mir::interpret::UndefinedBehaviorInfo::Custom(::rustc_middle::error::CustomSubdiagnostic {
                    msg: ||
                        rustc_errors::DiagMessage::Inline(std::borrow::Cow::Borrowed("{$kind ->\n                        [dealloc] deallocating\n                        [realloc] reallocating\n                        *[other] {\"\"}\n                    } {$ptr} which does not point to the beginning of an object")),
                    add_args: Box::new(move |mut set_arg|
                            {
                                set_arg("ptr".into(),
                                    rustc_errors::IntoDiagArg::into_diag_arg(ptr, &mut None));
                                set_arg("kind".into(),
                                    rustc_errors::IntoDiagArg::into_diag_arg(kind, &mut None));
                            }),
                }))
    };throw_ub_custom!(
315                msg!(
316                    "{$kind ->
317                        [dealloc] deallocating
318                        [realloc] reallocating
319                        *[other] {\"\"}
320                    } {$ptr} which does not point to the beginning of an object"
321                ),
322                ptr = format!("{ptr:?}"),
323                kind = "realloc"
324            );
325        }
326
327        // For simplicities' sake, we implement reallocate as "alloc, copy, dealloc".
328        // This happens so rarely, the perf advantage is outweighed by the maintenance cost.
329        // If requested, we zero-init the entire allocation, to ensure that a growing
330        // allocation has its new bytes properly set. For the part that is copied,
331        // `mem_copy` below will de-initialize things as necessary.
332        let new_ptr = self.allocate_ptr(new_size, new_align, kind, init_growth)?;
333        let old_size = match old_size_and_align {
334            Some((size, _align)) => size,
335            None => self.get_alloc_raw(alloc_id)?.size(),
336        };
337        // This will also call the access hooks.
338        self.mem_copy(ptr, new_ptr.into(), old_size.min(new_size), /*nonoverlapping*/ true)?;
339        self.deallocate_ptr(ptr, old_size_and_align, kind)?;
340
341        interp_ok(new_ptr)
342    }
343
344    /// Mark the `const_allocate`d allocation `ptr` points to as immutable so we can intern it.
345    pub fn make_const_heap_ptr_global(
346        &mut self,
347        ptr: Pointer<Option<CtfeProvenance>>,
348    ) -> InterpResult<'tcx>
349    where
350        M: Machine<'tcx, MemoryKind = crate::const_eval::MemoryKind, Provenance = CtfeProvenance>,
351    {
352        let (alloc_id, offset, _) = self.ptr_get_alloc_id(ptr, 0)?;
353        if offset.bytes() != 0 {
354            return Err(ConstEvalErrKind::ConstMakeGlobalWithOffset(ptr)).into();
355        }
356
357        if self.tcx.try_get_global_alloc(alloc_id).is_some() {
358            // This points to something outside the current interpreter.
359            return Err(ConstEvalErrKind::ConstMakeGlobalPtrIsNonHeap(ptr)).into();
360        }
361
362        // If we can't find it in `alloc_map` it must be dangling (because we don't use
363        // `extra_fn_ptr_map` in const-eval).
364        let (kind, alloc) = self
365            .memory
366            .alloc_map
367            .get_mut_or(alloc_id, || Err(ConstEvalErrKind::ConstMakeGlobalWithDanglingPtr(ptr)))?;
368
369        // Ensure this is actually a *heap* allocation, and record it as made-global.
370        match kind {
371            MemoryKind::Stack | MemoryKind::CallerLocation => {
372                return Err(ConstEvalErrKind::ConstMakeGlobalPtrIsNonHeap(ptr)).into();
373            }
374            MemoryKind::Machine(crate::const_eval::MemoryKind::Heap { was_made_global }) => {
375                if *was_made_global {
376                    return Err(ConstEvalErrKind::ConstMakeGlobalPtrAlreadyMadeGlobal(alloc_id))
377                        .into();
378                }
379                *was_made_global = true;
380            }
381        }
382
383        // Prevent further mutation, this is now an immutable global.
384        alloc.mutability = Mutability::Not;
385
386        interp_ok(())
387    }
388
389    #[allow(clippy :: suspicious_else_formatting)]
{
    let __tracing_attr_span;
    let __tracing_attr_guard;
    if ::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
                &&
                ::tracing::Level::DEBUG <=
                    ::tracing::level_filters::LevelFilter::current() ||
            { false } {
        __tracing_attr_span =
            {
                use ::tracing::__macro_support::Callsite as _;
                static __CALLSITE: ::tracing::callsite::DefaultCallsite =
                    {
                        static META: ::tracing::Metadata<'static> =
                            {
                                ::tracing_core::metadata::Metadata::new("deallocate_ptr",
                                    "rustc_const_eval::interpret::memory",
                                    ::tracing::Level::DEBUG,
                                    ::tracing_core::__macro_support::Option::Some("compiler/rustc_const_eval/src/interpret/memory.rs"),
                                    ::tracing_core::__macro_support::Option::Some(389u32),
                                    ::tracing_core::__macro_support::Option::Some("rustc_const_eval::interpret::memory"),
                                    ::tracing_core::field::FieldSet::new(&["ptr",
                                                    "old_size_and_align", "kind"],
                                        ::tracing_core::callsite::Identifier(&__CALLSITE)),
                                    ::tracing::metadata::Kind::SPAN)
                            };
                        ::tracing::callsite::DefaultCallsite::new(&META)
                    };
                let mut interest = ::tracing::subscriber::Interest::never();
                if ::tracing::Level::DEBUG <=
                                    ::tracing::level_filters::STATIC_MAX_LEVEL &&
                                ::tracing::Level::DEBUG <=
                                    ::tracing::level_filters::LevelFilter::current() &&
                            { interest = __CALLSITE.interest(); !interest.is_never() }
                        &&
                        ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                            interest) {
                    let meta = __CALLSITE.metadata();
                    ::tracing::Span::new(meta,
                        &{
                                #[allow(unused_imports)]
                                use ::tracing::field::{debug, display, Value};
                                let mut iter = meta.fields().iter();
                                meta.fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                                    ::tracing::__macro_support::Option::Some(&::tracing::field::debug(&ptr)
                                                            as &dyn Value)),
                                                (&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                                    ::tracing::__macro_support::Option::Some(&::tracing::field::debug(&old_size_and_align)
                                                            as &dyn Value)),
                                                (&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                                    ::tracing::__macro_support::Option::Some(&::tracing::field::debug(&kind)
                                                            as &dyn Value))])
                            })
                } else {
                    let span =
                        ::tracing::__macro_support::__disabled_span(__CALLSITE.metadata());
                    {};
                    span
                }
            };
        __tracing_attr_guard = __tracing_attr_span.enter();
    }

    #[warn(clippy :: suspicious_else_formatting)]
    {

        #[allow(unknown_lints, unreachable_code, clippy ::
        diverging_sub_expression, clippy :: empty_loop, clippy ::
        let_unit_value, clippy :: let_with_type_underscore, clippy ::
        needless_return, clippy :: unreachable)]
        if false {
            let __tracing_attr_fake_return: InterpResult<'tcx> = loop {};
            return __tracing_attr_fake_return;
        }
        {
            let (alloc_id, offset, prov) = self.ptr_get_alloc_id(ptr, 0)?;
            {
                use ::tracing::__macro_support::Callsite as _;
                static __CALLSITE: ::tracing::callsite::DefaultCallsite =
                    {
                        static META: ::tracing::Metadata<'static> =
                            {
                                ::tracing_core::metadata::Metadata::new("event compiler/rustc_const_eval/src/interpret/memory.rs:397",
                                    "rustc_const_eval::interpret::memory",
                                    ::tracing::Level::TRACE,
                                    ::tracing_core::__macro_support::Option::Some("compiler/rustc_const_eval/src/interpret/memory.rs"),
                                    ::tracing_core::__macro_support::Option::Some(397u32),
                                    ::tracing_core::__macro_support::Option::Some("rustc_const_eval::interpret::memory"),
                                    ::tracing_core::field::FieldSet::new(&["message"],
                                        ::tracing_core::callsite::Identifier(&__CALLSITE)),
                                    ::tracing::metadata::Kind::EVENT)
                            };
                        ::tracing::callsite::DefaultCallsite::new(&META)
                    };
                let enabled =
                    ::tracing::Level::TRACE <=
                                ::tracing::level_filters::STATIC_MAX_LEVEL &&
                            ::tracing::Level::TRACE <=
                                ::tracing::level_filters::LevelFilter::current() &&
                        {
                            let interest = __CALLSITE.interest();
                            !interest.is_never() &&
                                ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                                    interest)
                        };
                if enabled {
                    (|value_set: ::tracing::field::ValueSet|
                                {
                                    let meta = __CALLSITE.metadata();
                                    ::tracing::Event::dispatch(meta, &value_set);
                                    ;
                                })({
                            #[allow(unused_imports)]
                            use ::tracing::field::{debug, display, Value};
                            let mut iter = __CALLSITE.metadata().fields().iter();
                            __CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                                ::tracing::__macro_support::Option::Some(&format_args!("deallocating: {0:?}",
                                                                alloc_id) as &dyn Value))])
                        });
                } else { ; }
            };
            if offset.bytes() != 0 {
                do yeet {
                        let (ptr, kind) =
                            (::alloc::__export::must_use({
                                        ::alloc::fmt::format(format_args!("{0:?}", ptr))
                                    }), "dealloc");
                        ::rustc_middle::mir::interpret::InterpErrorKind::UndefinedBehavior(::rustc_middle::mir::interpret::UndefinedBehaviorInfo::Custom(::rustc_middle::error::CustomSubdiagnostic {
                                    msg: ||
                                        rustc_errors::DiagMessage::Inline(std::borrow::Cow::Borrowed("{$kind ->\n                        [dealloc] deallocating\n                        [realloc] reallocating\n                        *[other] {\"\"}\n                    } {$ptr} which does not point to the beginning of an object")),
                                    add_args: Box::new(move |mut set_arg|
                                            {
                                                set_arg("ptr".into(),
                                                    rustc_errors::IntoDiagArg::into_diag_arg(ptr, &mut None));
                                                set_arg("kind".into(),
                                                    rustc_errors::IntoDiagArg::into_diag_arg(kind, &mut None));
                                            }),
                                }))
                    };
            }
            let Some((alloc_kind, mut alloc)) =
                self.memory.alloc_map.remove(&alloc_id) else {
                    return Err(match self.tcx.try_get_global_alloc(alloc_id) {
                                    Some(GlobalAlloc::Function { .. }) => {
                                        {
                                            let (alloc_id, kind) = (alloc_id, "fn");
                                            ::rustc_middle::mir::interpret::InterpErrorKind::UndefinedBehavior(::rustc_middle::mir::interpret::UndefinedBehaviorInfo::Custom(::rustc_middle::error::CustomSubdiagnostic {
                                                        msg: ||
                                                            rustc_errors::DiagMessage::Inline(std::borrow::Cow::Borrowed("deallocating {$alloc_id}, which is {$kind ->\n                                [fn] a function\n                                [vtable] a vtable\n                                [static_mem] static memory\n                                *[other] {\"\"}\n                            }")),
                                                        add_args: Box::new(move |mut set_arg|
                                                                {
                                                                    set_arg("alloc_id".into(),
                                                                        rustc_errors::IntoDiagArg::into_diag_arg(alloc_id,
                                                                            &mut None));
                                                                    set_arg("kind".into(),
                                                                        rustc_errors::IntoDiagArg::into_diag_arg(kind, &mut None));
                                                                }),
                                                    }))
                                        }
                                    }
                                    Some(GlobalAlloc::VTable(..)) => {
                                        {
                                            let (alloc_id, kind) = (alloc_id, "vtable");
                                            ::rustc_middle::mir::interpret::InterpErrorKind::UndefinedBehavior(::rustc_middle::mir::interpret::UndefinedBehaviorInfo::Custom(::rustc_middle::error::CustomSubdiagnostic {
                                                        msg: ||
                                                            rustc_errors::DiagMessage::Inline(std::borrow::Cow::Borrowed("deallocating {$alloc_id}, which is {$kind ->\n                                [fn] a function\n                                [vtable] a vtable\n                                [static_mem] static memory\n                                *[other] {\"\"}\n                            }")),
                                                        add_args: Box::new(move |mut set_arg|
                                                                {
                                                                    set_arg("alloc_id".into(),
                                                                        rustc_errors::IntoDiagArg::into_diag_arg(alloc_id,
                                                                            &mut None));
                                                                    set_arg("kind".into(),
                                                                        rustc_errors::IntoDiagArg::into_diag_arg(kind, &mut None));
                                                                }),
                                                    }))
                                        }
                                    }
                                    Some(GlobalAlloc::TypeId { .. }) => {
                                        {
                                            let (alloc_id, kind) = (alloc_id, "typeid");
                                            ::rustc_middle::mir::interpret::InterpErrorKind::UndefinedBehavior(::rustc_middle::mir::interpret::UndefinedBehaviorInfo::Custom(::rustc_middle::error::CustomSubdiagnostic {
                                                        msg: ||
                                                            rustc_errors::DiagMessage::Inline(std::borrow::Cow::Borrowed("deallocating {$alloc_id}, which is {$kind ->\n                                [fn] a function\n                                [vtable] a vtable\n                                [static_mem] static memory\n                                *[other] {\"\"}\n                            }")),
                                                        add_args: Box::new(move |mut set_arg|
                                                                {
                                                                    set_arg("alloc_id".into(),
                                                                        rustc_errors::IntoDiagArg::into_diag_arg(alloc_id,
                                                                            &mut None));
                                                                    set_arg("kind".into(),
                                                                        rustc_errors::IntoDiagArg::into_diag_arg(kind, &mut None));
                                                                }),
                                                    }))
                                        }
                                    }
                                    Some(GlobalAlloc::Static(..) | GlobalAlloc::Memory(..)) => {
                                        {
                                            let (alloc_id, kind) = (alloc_id, "static_mem");
                                            ::rustc_middle::mir::interpret::InterpErrorKind::UndefinedBehavior(::rustc_middle::mir::interpret::UndefinedBehaviorInfo::Custom(::rustc_middle::error::CustomSubdiagnostic {
                                                        msg: ||
                                                            rustc_errors::DiagMessage::Inline(std::borrow::Cow::Borrowed("deallocating {$alloc_id}, which is {$kind ->\n                                [fn] a function\n                                [vtable] a vtable\n                                [static_mem] static memory\n                                *[other] {\"\"}\n                            }")),
                                                        add_args: Box::new(move |mut set_arg|
                                                                {
                                                                    set_arg("alloc_id".into(),
                                                                        rustc_errors::IntoDiagArg::into_diag_arg(alloc_id,
                                                                            &mut None));
                                                                    set_arg("kind".into(),
                                                                        rustc_errors::IntoDiagArg::into_diag_arg(kind, &mut None));
                                                                }),
                                                    }))
                                        }
                                    }
                                    None =>
                                        ::rustc_middle::mir::interpret::InterpErrorKind::UndefinedBehavior(::rustc_middle::mir::interpret::UndefinedBehaviorInfo::PointerUseAfterFree(alloc_id,
                                                CheckInAllocMsg::MemoryAccess)),
                                }).into();
                };
            if alloc.mutability.is_not() {
                do yeet {
                        let (alloc,) = (alloc_id,);
                        ::rustc_middle::mir::interpret::InterpErrorKind::UndefinedBehavior(::rustc_middle::mir::interpret::UndefinedBehaviorInfo::Custom(::rustc_middle::error::CustomSubdiagnostic {
                                    msg: ||
                                        rustc_errors::DiagMessage::Inline(std::borrow::Cow::Borrowed("deallocating immutable allocation {$alloc}")),
                                    add_args: Box::new(move |mut set_arg|
                                            {
                                                set_arg("alloc".into(),
                                                    rustc_errors::IntoDiagArg::into_diag_arg(alloc, &mut None));
                                            }),
                                }))
                    };
            }
            if alloc_kind != kind {
                do yeet {
                        let (alloc, alloc_kind, kind) =
                            (alloc_id,
                                ::alloc::__export::must_use({
                                        ::alloc::fmt::format(format_args!("{0}", alloc_kind))
                                    }),
                                ::alloc::__export::must_use({
                                        ::alloc::fmt::format(format_args!("{0}", kind))
                                    }));
                        ::rustc_middle::mir::interpret::InterpErrorKind::UndefinedBehavior(::rustc_middle::mir::interpret::UndefinedBehaviorInfo::Custom(::rustc_middle::error::CustomSubdiagnostic {
                                    msg: ||
                                        rustc_errors::DiagMessage::Inline(std::borrow::Cow::Borrowed("deallocating {$alloc}, which is {$alloc_kind} memory, using {$kind} deallocation operation")),
                                    add_args: Box::new(move |mut set_arg|
                                            {
                                                set_arg("alloc".into(),
                                                    rustc_errors::IntoDiagArg::into_diag_arg(alloc, &mut None));
                                                set_arg("alloc_kind".into(),
                                                    rustc_errors::IntoDiagArg::into_diag_arg(alloc_kind,
                                                        &mut None));
                                                set_arg("kind".into(),
                                                    rustc_errors::IntoDiagArg::into_diag_arg(kind, &mut None));
                                            }),
                                }))
                    };
            }
            if let Some((size, align)) = old_size_and_align {
                if size != alloc.size() || align != alloc.align {
                    do yeet {
                            let (alloc, size, align, size_found, align_found) =
                                (alloc_id, alloc.size().bytes(), alloc.align.bytes(),
                                    size.bytes(), align.bytes());
                            ::rustc_middle::mir::interpret::InterpErrorKind::UndefinedBehavior(::rustc_middle::mir::interpret::UndefinedBehaviorInfo::Custom(::rustc_middle::error::CustomSubdiagnostic {
                                        msg: ||
                                            rustc_errors::DiagMessage::Inline(std::borrow::Cow::Borrowed("incorrect layout on deallocation: {$alloc} has size {$size} and alignment {$align}, but gave size {$size_found} and alignment {$align_found}")),
                                        add_args: Box::new(move |mut set_arg|
                                                {
                                                    set_arg("alloc".into(),
                                                        rustc_errors::IntoDiagArg::into_diag_arg(alloc, &mut None));
                                                    set_arg("size".into(),
                                                        rustc_errors::IntoDiagArg::into_diag_arg(size, &mut None));
                                                    set_arg("align".into(),
                                                        rustc_errors::IntoDiagArg::into_diag_arg(align, &mut None));
                                                    set_arg("size_found".into(),
                                                        rustc_errors::IntoDiagArg::into_diag_arg(size_found,
                                                            &mut None));
                                                    set_arg("align_found".into(),
                                                        rustc_errors::IntoDiagArg::into_diag_arg(align_found,
                                                            &mut None));
                                                }),
                                    }))
                        }
                }
            }
            let size = alloc.size();
            M::before_memory_deallocation(self.tcx, &mut self.machine,
                    &mut alloc.extra, ptr, (alloc_id, prov), size, alloc.align,
                    kind)?;
            let old =
                self.memory.dead_alloc_map.insert(alloc_id,
                    (size, alloc.align));
            if old.is_some() {
                ::rustc_middle::util::bug::bug_fmt(format_args!("Nothing can be deallocated twice"));
            }
            interp_ok(())
        }
    }
}#[instrument(skip(self), level = "debug")]
390    pub fn deallocate_ptr(
391        &mut self,
392        ptr: Pointer<Option<M::Provenance>>,
393        old_size_and_align: Option<(Size, Align)>,
394        kind: MemoryKind<M::MemoryKind>,
395    ) -> InterpResult<'tcx> {
396        let (alloc_id, offset, prov) = self.ptr_get_alloc_id(ptr, 0)?;
397        trace!("deallocating: {alloc_id:?}");
398
399        if offset.bytes() != 0 {
400            throw_ub_custom!(
401                msg!(
402                    "{$kind ->
403                        [dealloc] deallocating
404                        [realloc] reallocating
405                        *[other] {\"\"}
406                    } {$ptr} which does not point to the beginning of an object"
407                ),
408                ptr = format!("{ptr:?}"),
409                kind = "dealloc",
410            );
411        }
412
413        let Some((alloc_kind, mut alloc)) = self.memory.alloc_map.remove(&alloc_id) else {
414            // Deallocating global memory -- always an error
415            return Err(match self.tcx.try_get_global_alloc(alloc_id) {
416                Some(GlobalAlloc::Function { .. }) => {
417                    err_ub_custom!(
418                        msg!(
419                            "deallocating {$alloc_id}, which is {$kind ->
420                                [fn] a function
421                                [vtable] a vtable
422                                [static_mem] static memory
423                                *[other] {\"\"}
424                            }"
425                        ),
426                        alloc_id = alloc_id,
427                        kind = "fn",
428                    )
429                }
430                Some(GlobalAlloc::VTable(..)) => {
431                    err_ub_custom!(
432                        msg!(
433                            "deallocating {$alloc_id}, which is {$kind ->
434                                [fn] a function
435                                [vtable] a vtable
436                                [static_mem] static memory
437                                *[other] {\"\"}
438                            }"
439                        ),
440                        alloc_id = alloc_id,
441                        kind = "vtable",
442                    )
443                }
444                Some(GlobalAlloc::TypeId { .. }) => {
445                    err_ub_custom!(
446                        msg!(
447                            "deallocating {$alloc_id}, which is {$kind ->
448                                [fn] a function
449                                [vtable] a vtable
450                                [static_mem] static memory
451                                *[other] {\"\"}
452                            }"
453                        ),
454                        alloc_id = alloc_id,
455                        kind = "typeid",
456                    )
457                }
458                Some(GlobalAlloc::Static(..) | GlobalAlloc::Memory(..)) => {
459                    err_ub_custom!(
460                        msg!(
461                            "deallocating {$alloc_id}, which is {$kind ->
462                                [fn] a function
463                                [vtable] a vtable
464                                [static_mem] static memory
465                                *[other] {\"\"}
466                            }"
467                        ),
468                        alloc_id = alloc_id,
469                        kind = "static_mem"
470                    )
471                }
472                None => err_ub!(PointerUseAfterFree(alloc_id, CheckInAllocMsg::MemoryAccess)),
473            })
474            .into();
475        };
476
477        if alloc.mutability.is_not() {
478            throw_ub_custom!(msg!("deallocating immutable allocation {$alloc}"), alloc = alloc_id,);
479        }
480        if alloc_kind != kind {
481            throw_ub_custom!(
482                msg!(
483                    "deallocating {$alloc}, which is {$alloc_kind} memory, using {$kind} deallocation operation"
484                ),
485                alloc = alloc_id,
486                alloc_kind = format!("{alloc_kind}"),
487                kind = format!("{kind}"),
488            );
489        }
490        if let Some((size, align)) = old_size_and_align {
491            if size != alloc.size() || align != alloc.align {
492                throw_ub_custom!(
493                    msg!(
494                        "incorrect layout on deallocation: {$alloc} has size {$size} and alignment {$align}, but gave size {$size_found} and alignment {$align_found}"
495                    ),
496                    alloc = alloc_id,
497                    size = alloc.size().bytes(),
498                    align = alloc.align.bytes(),
499                    size_found = size.bytes(),
500                    align_found = align.bytes(),
501                )
502            }
503        }
504
505        // Let the machine take some extra action
506        let size = alloc.size();
507        M::before_memory_deallocation(
508            self.tcx,
509            &mut self.machine,
510            &mut alloc.extra,
511            ptr,
512            (alloc_id, prov),
513            size,
514            alloc.align,
515            kind,
516        )?;
517
518        // Don't forget to remember size and align of this now-dead allocation
519        let old = self.memory.dead_alloc_map.insert(alloc_id, (size, alloc.align));
520        if old.is_some() {
521            bug!("Nothing can be deallocated twice");
522        }
523
524        interp_ok(())
525    }
526
527    /// Internal helper function to determine the allocation and offset of a pointer (if any).
528    #[inline(always)]
529    fn get_ptr_access(
530        &self,
531        ptr: Pointer<Option<M::Provenance>>,
532        size: Size,
533    ) -> InterpResult<'tcx, Option<(AllocId, Size, M::ProvenanceExtra)>> {
534        let size = i64::try_from(size.bytes()).unwrap(); // it would be an error to even ask for more than isize::MAX bytes
535        Self::check_and_deref_ptr(
536            self,
537            ptr,
538            size,
539            CheckInAllocMsg::MemoryAccess,
540            |this, alloc_id, offset, prov| {
541                let (size, align) =
542                    this.get_live_alloc_size_and_align(alloc_id, CheckInAllocMsg::MemoryAccess)?;
543                interp_ok((size, align, (alloc_id, offset, prov)))
544            },
545        )
546    }
547
548    /// Check if the given pointer points to live memory of the given `size`.
549    /// The caller can control the error message for the out-of-bounds case.
550    #[inline(always)]
551    pub fn check_ptr_access(
552        &self,
553        ptr: Pointer<Option<M::Provenance>>,
554        size: Size,
555        msg: CheckInAllocMsg,
556    ) -> InterpResult<'tcx> {
557        let size = i64::try_from(size.bytes()).unwrap(); // it would be an error to even ask for more than isize::MAX bytes
558        Self::check_and_deref_ptr(self, ptr, size, msg, |this, alloc_id, _, _| {
559            let (size, align) = this.get_live_alloc_size_and_align(alloc_id, msg)?;
560            interp_ok((size, align, ()))
561        })?;
562        interp_ok(())
563    }
564
565    /// Check whether the given pointer points to live memory for a signed amount of bytes.
566    /// A negative amounts means that the given range of memory to the left of the pointer
567    /// needs to be dereferenceable.
568    pub fn check_ptr_access_signed(
569        &self,
570        ptr: Pointer<Option<M::Provenance>>,
571        size: i64,
572        msg: CheckInAllocMsg,
573    ) -> InterpResult<'tcx> {
574        Self::check_and_deref_ptr(self, ptr, size, msg, |this, alloc_id, _, _| {
575            let (size, align) = this.get_live_alloc_size_and_align(alloc_id, msg)?;
576            interp_ok((size, align, ()))
577        })?;
578        interp_ok(())
579    }
580
581    /// Low-level helper function to check if a ptr is in-bounds and potentially return a reference
582    /// to the allocation it points to. Supports both shared and mutable references, as the actual
583    /// checking is offloaded to a helper closure. Supports signed sizes for checks "to the left" of
584    /// a pointer.
585    ///
586    /// `alloc_size` will only get called for non-zero-sized accesses.
587    ///
588    /// Returns `None` if and only if the size is 0.
589    fn check_and_deref_ptr<T, R: Borrow<Self>>(
590        this: R,
591        ptr: Pointer<Option<M::Provenance>>,
592        size: i64,
593        msg: CheckInAllocMsg,
594        alloc_size: impl FnOnce(
595            R,
596            AllocId,
597            Size,
598            M::ProvenanceExtra,
599        ) -> InterpResult<'tcx, (Size, Align, T)>,
600    ) -> InterpResult<'tcx, Option<T>> {
601        // Everything is okay with size 0.
602        if size == 0 {
603            return interp_ok(None);
604        }
605
606        interp_ok(match this.borrow().ptr_try_get_alloc_id(ptr, size) {
607            Err(addr) => {
608                // We couldn't get a proper allocation.
609                do yeet ::rustc_middle::mir::interpret::InterpErrorKind::UndefinedBehavior(::rustc_middle::mir::interpret::UndefinedBehaviorInfo::DanglingIntPointer {
            addr,
            inbounds_size: size,
            msg,
        });throw_ub!(DanglingIntPointer { addr, inbounds_size: size, msg });
610            }
611            Ok((alloc_id, offset, prov)) => {
612                let tcx = this.borrow().tcx;
613                let (alloc_size, _alloc_align, ret_val) = alloc_size(this, alloc_id, offset, prov)?;
614                let offset = offset.bytes();
615                // Compute absolute begin and end of the range.
616                let (begin, end) = if size >= 0 {
617                    (Some(offset), offset.checked_add(size as u64))
618                } else {
619                    (offset.checked_sub(size.unsigned_abs()), Some(offset))
620                };
621                // Ensure both are within bounds.
622                let in_bounds = begin.is_some() && end.is_some_and(|e| e <= alloc_size.bytes());
623                if !in_bounds {
624                    do yeet ::rustc_middle::mir::interpret::InterpErrorKind::UndefinedBehavior(::rustc_middle::mir::interpret::UndefinedBehaviorInfo::PointerOutOfBounds {
            alloc_id,
            alloc_size,
            ptr_offset: tcx.sign_extend_to_target_isize(offset),
            inbounds_size: size,
            msg,
        })throw_ub!(PointerOutOfBounds {
625                        alloc_id,
626                        alloc_size,
627                        ptr_offset: tcx.sign_extend_to_target_isize(offset),
628                        inbounds_size: size,
629                        msg,
630                    })
631                }
632
633                Some(ret_val)
634            }
635        })
636    }
637
638    pub(super) fn check_misalign(
639        &self,
640        misaligned: Option<Misalignment>,
641        msg: CheckAlignMsg,
642    ) -> InterpResult<'tcx> {
643        if let Some(misaligned) = misaligned {
644            do yeet ::rustc_middle::mir::interpret::InterpErrorKind::UndefinedBehavior(::rustc_middle::mir::interpret::UndefinedBehaviorInfo::AlignmentCheckFailed(misaligned,
            msg))throw_ub!(AlignmentCheckFailed(misaligned, msg))
645        }
646        interp_ok(())
647    }
648
649    pub(super) fn is_ptr_misaligned(
650        &self,
651        ptr: Pointer<Option<M::Provenance>>,
652        align: Align,
653    ) -> Option<Misalignment> {
654        if !M::enforce_alignment(self) || align.bytes() == 1 {
655            return None;
656        }
657
658        #[inline]
659        fn is_offset_misaligned(offset: u64, align: Align) -> Option<Misalignment> {
660            if offset.is_multiple_of(align.bytes()) {
661                None
662            } else {
663                // The biggest power of two through which `offset` is divisible.
664                let offset_pow2 = 1 << offset.trailing_zeros();
665                Some(Misalignment { has: Align::from_bytes(offset_pow2).unwrap(), required: align })
666            }
667        }
668
669        match self.ptr_try_get_alloc_id(ptr, 0) {
670            Err(addr) => is_offset_misaligned(addr, align),
671            Ok((alloc_id, offset, _prov)) => {
672                let alloc_info = self.get_alloc_info(alloc_id);
673                if let Some(misalign) = M::alignment_check(
674                    self,
675                    alloc_id,
676                    alloc_info.align,
677                    alloc_info.kind,
678                    offset,
679                    align,
680                ) {
681                    Some(misalign)
682                } else if M::Provenance::OFFSET_IS_ADDR {
683                    is_offset_misaligned(ptr.addr().bytes(), align)
684                } else {
685                    // Check allocation alignment and offset alignment.
686                    if alloc_info.align.bytes() < align.bytes() {
687                        Some(Misalignment { has: alloc_info.align, required: align })
688                    } else {
689                        is_offset_misaligned(offset.bytes(), align)
690                    }
691                }
692            }
693        }
694    }
695
696    /// Checks a pointer for misalignment.
697    ///
698    /// The error assumes this is checking the pointer used directly for an access.
699    pub fn check_ptr_align(
700        &self,
701        ptr: Pointer<Option<M::Provenance>>,
702        align: Align,
703    ) -> InterpResult<'tcx> {
704        self.check_misalign(self.is_ptr_misaligned(ptr, align), CheckAlignMsg::AccessedPtr)
705    }
706}
707
708impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
709    /// This function is used by Miri's provenance GC to remove unreachable entries from the dead_alloc_map.
710    pub fn remove_unreachable_allocs(&mut self, reachable_allocs: &FxHashSet<AllocId>) {
711        // Unlike all the other GC helpers where we check if an `AllocId` is found in the interpreter or
712        // is live, here all the IDs in the map are for dead allocations so we don't
713        // need to check for liveness.
714        #[allow(rustc::potential_query_instability)] // Only used from Miri, not queries.
715        self.memory.dead_alloc_map.retain(|id, _| reachable_allocs.contains(id));
716    }
717}
718
719/// Allocation accessors
720impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
721    /// Helper function to obtain a global (tcx) allocation.
722    /// This attempts to return a reference to an existing allocation if
723    /// one can be found in `tcx`. That, however, is only possible if `tcx` and
724    /// this machine use the same pointer provenance, so it is indirected through
725    /// `M::adjust_allocation`.
726    fn get_global_alloc(
727        &self,
728        id: AllocId,
729        is_write: bool,
730    ) -> InterpResult<'tcx, Cow<'tcx, Allocation<M::Provenance, M::AllocExtra, M::Bytes>>> {
731        let (alloc, def_id) = match self.tcx.try_get_global_alloc(id) {
732            Some(GlobalAlloc::Memory(mem)) => {
733                // Memory of a constant or promoted or anonymous memory referenced by a static.
734                (mem, None)
735            }
736            Some(GlobalAlloc::Function { .. }) => do yeet ::rustc_middle::mir::interpret::InterpErrorKind::UndefinedBehavior(::rustc_middle::mir::interpret::UndefinedBehaviorInfo::DerefFunctionPointer(id))throw_ub!(DerefFunctionPointer(id)),
737            Some(GlobalAlloc::VTable(..)) => do yeet ::rustc_middle::mir::interpret::InterpErrorKind::UndefinedBehavior(::rustc_middle::mir::interpret::UndefinedBehaviorInfo::DerefVTablePointer(id))throw_ub!(DerefVTablePointer(id)),
738            Some(GlobalAlloc::TypeId { .. }) => do yeet ::rustc_middle::mir::interpret::InterpErrorKind::UndefinedBehavior(::rustc_middle::mir::interpret::UndefinedBehaviorInfo::DerefTypeIdPointer(id))throw_ub!(DerefTypeIdPointer(id)),
739            None => do yeet ::rustc_middle::mir::interpret::InterpErrorKind::UndefinedBehavior(::rustc_middle::mir::interpret::UndefinedBehaviorInfo::PointerUseAfterFree(id,
            CheckInAllocMsg::MemoryAccess))throw_ub!(PointerUseAfterFree(id, CheckInAllocMsg::MemoryAccess)),
740            Some(GlobalAlloc::Static(def_id)) => {
741                if !self.tcx.is_static(def_id) {
    ::core::panicking::panic("assertion failed: self.tcx.is_static(def_id)")
};assert!(self.tcx.is_static(def_id));
742                // Thread-local statics do not have a constant address. They *must* be accessed via
743                // `ThreadLocalRef`; we can never have a pointer to them as a regular constant value.
744                if !!self.tcx.is_thread_local_static(def_id) {
    ::core::panicking::panic("assertion failed: !self.tcx.is_thread_local_static(def_id)")
};assert!(!self.tcx.is_thread_local_static(def_id));
745                // Notice that every static has two `AllocId` that will resolve to the same
746                // thing here: one maps to `GlobalAlloc::Static`, this is the "lazy" ID,
747                // and the other one is maps to `GlobalAlloc::Memory`, this is returned by
748                // `eval_static_initializer` and it is the "resolved" ID.
749                // The resolved ID is never used by the interpreted program, it is hidden.
750                // This is relied upon for soundness of const-patterns; a pointer to the resolved
751                // ID would "sidestep" the checks that make sure consts do not point to statics!
752                // The `GlobalAlloc::Memory` branch here is still reachable though; when a static
753                // contains a reference to memory that was created during its evaluation (i.e., not
754                // to another static), those inner references only exist in "resolved" form.
755                if self.tcx.is_foreign_item(def_id) {
756                    // This is unreachable in Miri, but can happen in CTFE where we actually *do* support
757                    // referencing arbitrary (declared) extern statics.
758                    do yeet ::rustc_middle::mir::interpret::InterpErrorKind::Unsupported(::rustc_middle::mir::interpret::UnsupportedOpInfo::ExternStatic(def_id));throw_unsup!(ExternStatic(def_id));
759                }
760
761                // We don't give a span -- statics don't need that, they cannot be generic or associated.
762                let val = self.ctfe_query(|tcx| tcx.eval_static_initializer(def_id))?;
763                (val, Some(def_id))
764            }
765        };
766        M::before_access_global(self.tcx, &self.machine, id, alloc, def_id, is_write)?;
767        // We got tcx memory. Let the machine initialize its "extra" stuff.
768        M::adjust_global_allocation(
769            self,
770            id, // always use the ID we got as input, not the "hidden" one.
771            alloc.inner(),
772        )
773    }
774
775    /// Gives raw access to the `Allocation`, without bounds or alignment checks.
776    /// The caller is responsible for calling the access hooks!
777    ///
778    /// You almost certainly want to use `get_ptr_alloc`/`get_ptr_alloc_mut` instead.
779    pub fn get_alloc_raw(
780        &self,
781        id: AllocId,
782    ) -> InterpResult<'tcx, &Allocation<M::Provenance, M::AllocExtra, M::Bytes>> {
783        // The error type of the inner closure here is somewhat funny. We have two
784        // ways of "erroring": An actual error, or because we got a reference from
785        // `get_global_alloc` that we can actually use directly without inserting anything anywhere.
786        // So the error type is `InterpResult<'tcx, &Allocation<M::Provenance>>`.
787        let a = self.memory.alloc_map.get_or(id, || {
788            // We have to funnel the `InterpErrorInfo` through a `Result` to match the `get_or` API,
789            // so we use `report_err` for that.
790            let alloc = self.get_global_alloc(id, /*is_write*/ false).report_err().map_err(Err)?;
791            match alloc {
792                Cow::Borrowed(alloc) => {
793                    // We got a ref, cheaply return that as an "error" so that the
794                    // map does not get mutated.
795                    Err(Ok(alloc))
796                }
797                Cow::Owned(alloc) => {
798                    // Need to put it into the map and return a ref to that
799                    let kind = M::GLOBAL_KIND.expect(
800                        "I got a global allocation that I have to copy but the machine does \
801                            not expect that to happen",
802                    );
803                    Ok((MemoryKind::Machine(kind), alloc))
804                }
805            }
806        });
807        // Now unpack that funny error type
808        match a {
809            Ok(a) => interp_ok(&a.1),
810            Err(a) => a.into(),
811        }
812    }
813
814    /// Gives raw, immutable access to the `Allocation` address, without bounds or alignment checks.
815    /// The caller is responsible for calling the access hooks!
816    pub fn get_alloc_bytes_unchecked_raw(&self, id: AllocId) -> InterpResult<'tcx, *const u8> {
817        let alloc = self.get_alloc_raw(id)?;
818        interp_ok(alloc.get_bytes_unchecked_raw())
819    }
820
821    /// Bounds-checked *but not align-checked* allocation access.
822    pub fn get_ptr_alloc<'a>(
823        &'a self,
824        ptr: Pointer<Option<M::Provenance>>,
825        size: Size,
826    ) -> InterpResult<'tcx, Option<AllocRef<'a, 'tcx, M::Provenance, M::AllocExtra, M::Bytes>>>
827    {
828        let size_i64 = i64::try_from(size.bytes()).unwrap(); // it would be an error to even ask for more than isize::MAX bytes
829        let ptr_and_alloc = Self::check_and_deref_ptr(
830            self,
831            ptr,
832            size_i64,
833            CheckInAllocMsg::MemoryAccess,
834            |this, alloc_id, offset, prov| {
835                let alloc = this.get_alloc_raw(alloc_id)?;
836                interp_ok((alloc.size(), alloc.align, (alloc_id, offset, prov, alloc)))
837            },
838        )?;
839        // We want to call the hook on *all* accesses that involve an AllocId, including zero-sized
840        // accesses. That means we cannot rely on the closure above or the `Some` branch below. We
841        // do this after `check_and_deref_ptr` to ensure some basic sanity has already been checked.
842        if !self.memory.validation_in_progress.get() {
843            if let Ok((alloc_id, ..)) = self.ptr_try_get_alloc_id(ptr, size_i64) {
844                M::before_alloc_access(self.tcx, &self.machine, alloc_id)?;
845            }
846        }
847
848        if let Some((alloc_id, offset, prov, alloc)) = ptr_and_alloc {
849            let range = alloc_range(offset, size);
850            if !self.memory.validation_in_progress.get() {
851                M::before_memory_read(
852                    self.tcx,
853                    &self.machine,
854                    &alloc.extra,
855                    ptr,
856                    (alloc_id, prov),
857                    range,
858                )?;
859            }
860            interp_ok(Some(AllocRef { alloc, range, tcx: *self.tcx, alloc_id }))
861        } else {
862            interp_ok(None)
863        }
864    }
865
866    /// Return the `extra` field of the given allocation.
867    pub fn get_alloc_extra<'a>(&'a self, id: AllocId) -> InterpResult<'tcx, &'a M::AllocExtra> {
868        interp_ok(&self.get_alloc_raw(id)?.extra)
869    }
870
871    /// Return the `mutability` field of the given allocation.
872    pub fn get_alloc_mutability<'a>(&'a self, id: AllocId) -> InterpResult<'tcx, Mutability> {
873        interp_ok(self.get_alloc_raw(id)?.mutability)
874    }
875
876    /// Gives raw mutable access to the `Allocation`, without bounds or alignment checks.
877    /// The caller is responsible for calling the access hooks!
878    ///
879    /// Also returns a ptr to `self.extra` so that the caller can use it in parallel with the
880    /// allocation.
881    ///
882    /// You almost certainly want to use `get_ptr_alloc`/`get_ptr_alloc_mut` instead.
883    pub fn get_alloc_raw_mut(
884        &mut self,
885        id: AllocId,
886    ) -> InterpResult<'tcx, (&mut Allocation<M::Provenance, M::AllocExtra, M::Bytes>, &mut M)> {
887        // We have "NLL problem case #3" here, which cannot be worked around without loss of
888        // efficiency even for the common case where the key is in the map.
889        // <https://rust-lang.github.io/rfcs/2094-nll.html#problem-case-3-conditional-control-flow-across-functions>
890        // (Cannot use `get_mut_or` since `get_global_alloc` needs `&self`, and that boils down to
891        // Miri's `adjust_alloc_root_pointer` needing to look up the size of the allocation.
892        // It could be avoided with a totally separate codepath in Miri for handling the absolute address
893        // of global allocations, but that's not worth it.)
894        if self.memory.alloc_map.get_mut(id).is_none() {
895            // Slow path.
896            // Allocation not found locally, go look global.
897            let alloc = self.get_global_alloc(id, /*is_write*/ true)?;
898            let kind = M::GLOBAL_KIND.expect(
899                "I got a global allocation that I have to copy but the machine does \
900                    not expect that to happen",
901            );
902            self.memory.alloc_map.insert(id, (MemoryKind::Machine(kind), alloc.into_owned()));
903        }
904
905        let (_kind, alloc) = self.memory.alloc_map.get_mut(id).unwrap();
906        if alloc.mutability.is_not() {
907            do yeet ::rustc_middle::mir::interpret::InterpErrorKind::UndefinedBehavior(::rustc_middle::mir::interpret::UndefinedBehaviorInfo::WriteToReadOnly(id))throw_ub!(WriteToReadOnly(id))
908        }
909        interp_ok((alloc, &mut self.machine))
910    }
911
912    /// Gives raw, mutable access to the `Allocation` address, without bounds or alignment checks.
913    /// The caller is responsible for calling the access hooks!
914    pub fn get_alloc_bytes_unchecked_raw_mut(
915        &mut self,
916        id: AllocId,
917    ) -> InterpResult<'tcx, *mut u8> {
918        let alloc = self.get_alloc_raw_mut(id)?.0;
919        interp_ok(alloc.get_bytes_unchecked_raw_mut())
920    }
921
922    /// Bounds-checked *but not align-checked* allocation access.
923    pub fn get_ptr_alloc_mut<'a>(
924        &'a mut self,
925        ptr: Pointer<Option<M::Provenance>>,
926        size: Size,
927    ) -> InterpResult<'tcx, Option<AllocRefMut<'a, 'tcx, M::Provenance, M::AllocExtra, M::Bytes>>>
928    {
929        let tcx = self.tcx;
930        let validation_in_progress = self.memory.validation_in_progress.get();
931
932        let size_i64 = i64::try_from(size.bytes()).unwrap(); // it would be an error to even ask for more than isize::MAX bytes
933        let ptr_and_alloc = Self::check_and_deref_ptr(
934            self,
935            ptr,
936            size_i64,
937            CheckInAllocMsg::MemoryAccess,
938            |this, alloc_id, offset, prov| {
939                let (alloc, machine) = this.get_alloc_raw_mut(alloc_id)?;
940                interp_ok((alloc.size(), alloc.align, (alloc_id, offset, prov, alloc, machine)))
941            },
942        )?;
943
944        if let Some((alloc_id, offset, prov, alloc, machine)) = ptr_and_alloc {
945            let range = alloc_range(offset, size);
946            if !validation_in_progress {
947                // For writes, it's okay to only call those when there actually is a non-zero
948                // amount of bytes to be written: a zero-sized write doesn't manifest anything.
949                M::before_alloc_access(tcx, machine, alloc_id)?;
950                M::before_memory_write(
951                    tcx,
952                    machine,
953                    &mut alloc.extra,
954                    ptr,
955                    (alloc_id, prov),
956                    range,
957                )?;
958            }
959            interp_ok(Some(AllocRefMut { alloc, range, tcx: *tcx, alloc_id }))
960        } else {
961            interp_ok(None)
962        }
963    }
964
965    /// Return the `extra` field of the given allocation.
966    pub fn get_alloc_extra_mut<'a>(
967        &'a mut self,
968        id: AllocId,
969    ) -> InterpResult<'tcx, (&'a mut M::AllocExtra, &'a mut M)> {
970        let (alloc, machine) = self.get_alloc_raw_mut(id)?;
971        interp_ok((&mut alloc.extra, machine))
972    }
973
974    /// Check whether an allocation is live. This is faster than calling
975    /// [`InterpCx::get_alloc_info`] if all you need to check is whether the kind is
976    /// [`AllocKind::Dead`] because it doesn't have to look up the type and layout of statics.
977    pub fn is_alloc_live(&self, id: AllocId) -> bool {
978        self.memory.alloc_map.contains_key_ref(&id)
979            || self.memory.extra_fn_ptr_map.contains_key(&id)
980            || self.memory.va_list_map.contains_key(&id)
981            // We check `tcx` last as that has to acquire a lock in `many-seeds` mode.
982            // This also matches the order in `get_alloc_info`.
983            || self.tcx.try_get_global_alloc(id).is_some()
984    }
985
986    /// Obtain the size and alignment of an allocation, even if that allocation has
987    /// been deallocated.
988    pub fn get_alloc_info(&self, id: AllocId) -> AllocInfo {
989        // # Regular allocations
990        // Don't use `self.get_raw` here as that will
991        // a) cause cycles in case `id` refers to a static
992        // b) duplicate a global's allocation in miri
993        if let Some((_, alloc)) = self.memory.alloc_map.get(id) {
994            return AllocInfo::new(
995                alloc.size(),
996                alloc.align,
997                AllocKind::LiveData,
998                alloc.mutability,
999            );
1000        }
1001
1002        // # Function pointers
1003        // (both global from `alloc_map` and local from `extra_fn_ptr_map`)
1004        if let Some(fn_val) = self.get_fn_alloc(id) {
1005            let align = match fn_val {
1006                FnVal::Instance(_instance) => {
1007                    // FIXME: Until we have a clear design for the effects of align(N) functions
1008                    // on the address of function pointers, we don't consider the align(N)
1009                    // attribute on functions in the interpreter.
1010                    // See <https://github.com/rust-lang/rust/issues/144661> for more context.
1011                    Align::ONE
1012                }
1013                // Machine-specific extra functions currently do not support alignment restrictions.
1014                FnVal::Other(_) => Align::ONE,
1015            };
1016
1017            return AllocInfo::new(Size::ZERO, align, AllocKind::Function, Mutability::Not);
1018        }
1019
1020        // # Variable argument lists
1021        if self.memory.va_list_map.contains_key(&id) {
1022            return AllocInfo::new(Size::ZERO, Align::ONE, AllocKind::VaList, Mutability::Not);
1023        }
1024
1025        // # Global allocations
1026        if let Some(global_alloc) = self.tcx.try_get_global_alloc(id) {
1027            // NOTE: `static` alignment from attributes has already been applied to the allocation.
1028            let (size, align) = global_alloc.size_and_align(*self.tcx, self.typing_env);
1029            let mutbl = global_alloc.mutability(*self.tcx, self.typing_env);
1030            let kind = match global_alloc {
1031                GlobalAlloc::Static { .. } | GlobalAlloc::Memory { .. } => AllocKind::LiveData,
1032                GlobalAlloc::Function { .. } => ::rustc_middle::util::bug::bug_fmt(format_args!("We already checked function pointers above"))bug!("We already checked function pointers above"),
1033                GlobalAlloc::VTable { .. } => AllocKind::VTable,
1034                GlobalAlloc::TypeId { .. } => AllocKind::TypeId,
1035            };
1036            return AllocInfo::new(size, align, kind, mutbl);
1037        }
1038
1039        // # Dead pointers
1040        let (size, align) = *self
1041            .memory
1042            .dead_alloc_map
1043            .get(&id)
1044            .expect("deallocated pointers should all be recorded in `dead_alloc_map`");
1045        AllocInfo::new(size, align, AllocKind::Dead, Mutability::Not)
1046    }
1047
1048    /// Obtain the size and alignment of a *live* allocation.
1049    fn get_live_alloc_size_and_align(
1050        &self,
1051        id: AllocId,
1052        msg: CheckInAllocMsg,
1053    ) -> InterpResult<'tcx, (Size, Align)> {
1054        let info = self.get_alloc_info(id);
1055        if info.kind == AllocKind::Dead {
1056            do yeet ::rustc_middle::mir::interpret::InterpErrorKind::UndefinedBehavior(::rustc_middle::mir::interpret::UndefinedBehaviorInfo::PointerUseAfterFree(id,
            msg))throw_ub!(PointerUseAfterFree(id, msg))
1057        }
1058        interp_ok((info.size, info.align))
1059    }
1060
1061    fn get_fn_alloc(&self, id: AllocId) -> Option<FnVal<'tcx, M::ExtraFnVal>> {
1062        if let Some(extra) = self.memory.extra_fn_ptr_map.get(&id) {
1063            Some(FnVal::Other(*extra))
1064        } else {
1065            match self.tcx.try_get_global_alloc(id) {
1066                Some(GlobalAlloc::Function { instance, .. }) => Some(FnVal::Instance(instance)),
1067                _ => None,
1068            }
1069        }
1070    }
1071
1072    /// Takes a pointer that is the first chunk of a `TypeId` and return the type that its
1073    /// provenance refers to, as well as the segment of the hash that this pointer covers.
1074    pub fn get_ptr_type_id(
1075        &self,
1076        ptr: Pointer<Option<M::Provenance>>,
1077    ) -> InterpResult<'tcx, (Ty<'tcx>, u64)> {
1078        let (alloc_id, offset, _meta) = self.ptr_get_alloc_id(ptr, 0)?;
1079        let Some(GlobalAlloc::TypeId { ty }) = self.tcx.try_get_global_alloc(alloc_id) else {
1080            do yeet ::rustc_middle::mir::interpret::InterpErrorKind::UndefinedBehavior(::rustc_middle::mir::interpret::UndefinedBehaviorInfo::Ub(::alloc::__export::must_use({
                    ::alloc::fmt::format(format_args!("invalid `TypeId` value: not all bytes carry type id metadata"))
                })))throw_ub_format!("invalid `TypeId` value: not all bytes carry type id metadata")
1081        };
1082        interp_ok((ty, offset.bytes()))
1083    }
1084
1085    pub fn get_ptr_fn(
1086        &self,
1087        ptr: Pointer<Option<M::Provenance>>,
1088    ) -> InterpResult<'tcx, FnVal<'tcx, M::ExtraFnVal>> {
1089        {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event compiler/rustc_const_eval/src/interpret/memory.rs:1089",
                        "rustc_const_eval::interpret::memory",
                        ::tracing::Level::TRACE,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_const_eval/src/interpret/memory.rs"),
                        ::tracing_core::__macro_support::Option::Some(1089u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_const_eval::interpret::memory"),
                        ::tracing_core::field::FieldSet::new(&["message"],
                            ::tracing_core::callsite::Identifier(&__CALLSITE)),
                        ::tracing::metadata::Kind::EVENT)
                };
            ::tracing::callsite::DefaultCallsite::new(&META)
        };
    let enabled =
        ::tracing::Level::TRACE <= ::tracing::level_filters::STATIC_MAX_LEVEL
                &&
                ::tracing::Level::TRACE <=
                    ::tracing::level_filters::LevelFilter::current() &&
            {
                let interest = __CALLSITE.interest();
                !interest.is_never() &&
                    ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                        interest)
            };
    if enabled {
        (|value_set: ::tracing::field::ValueSet|
                    {
                        let meta = __CALLSITE.metadata();
                        ::tracing::Event::dispatch(meta, &value_set);
                        ;
                    })({
                #[allow(unused_imports)]
                use ::tracing::field::{debug, display, Value};
                let mut iter = __CALLSITE.metadata().fields().iter();
                __CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                    ::tracing::__macro_support::Option::Some(&format_args!("get_ptr_fn({0:?})",
                                                    ptr) as &dyn Value))])
            });
    } else { ; }
};trace!("get_ptr_fn({:?})", ptr);
1090        let (alloc_id, offset, _prov) = self.ptr_get_alloc_id(ptr, 0)?;
1091        if offset.bytes() != 0 {
1092            do yeet ::rustc_middle::mir::interpret::InterpErrorKind::UndefinedBehavior(::rustc_middle::mir::interpret::UndefinedBehaviorInfo::InvalidFunctionPointer(Pointer::new(alloc_id,
                offset)))throw_ub!(InvalidFunctionPointer(Pointer::new(alloc_id, offset)))
1093        }
1094        self.get_fn_alloc(alloc_id)
1095            .ok_or_else(|| ::rustc_middle::mir::interpret::InterpErrorKind::UndefinedBehavior(::rustc_middle::mir::interpret::UndefinedBehaviorInfo::InvalidFunctionPointer(Pointer::new(alloc_id,
            offset)))err_ub!(InvalidFunctionPointer(Pointer::new(alloc_id, offset))))
1096            .into()
1097    }
1098
1099    pub fn get_ptr_va_list(
1100        &self,
1101        ptr: Pointer<Option<M::Provenance>>,
1102    ) -> InterpResult<'tcx, &VecDeque<MPlaceTy<'tcx, M::Provenance>>> {
1103        {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event compiler/rustc_const_eval/src/interpret/memory.rs:1103",
                        "rustc_const_eval::interpret::memory",
                        ::tracing::Level::TRACE,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_const_eval/src/interpret/memory.rs"),
                        ::tracing_core::__macro_support::Option::Some(1103u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_const_eval::interpret::memory"),
                        ::tracing_core::field::FieldSet::new(&["message"],
                            ::tracing_core::callsite::Identifier(&__CALLSITE)),
                        ::tracing::metadata::Kind::EVENT)
                };
            ::tracing::callsite::DefaultCallsite::new(&META)
        };
    let enabled =
        ::tracing::Level::TRACE <= ::tracing::level_filters::STATIC_MAX_LEVEL
                &&
                ::tracing::Level::TRACE <=
                    ::tracing::level_filters::LevelFilter::current() &&
            {
                let interest = __CALLSITE.interest();
                !interest.is_never() &&
                    ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                        interest)
            };
    if enabled {
        (|value_set: ::tracing::field::ValueSet|
                    {
                        let meta = __CALLSITE.metadata();
                        ::tracing::Event::dispatch(meta, &value_set);
                        ;
                    })({
                #[allow(unused_imports)]
                use ::tracing::field::{debug, display, Value};
                let mut iter = __CALLSITE.metadata().fields().iter();
                __CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                    ::tracing::__macro_support::Option::Some(&format_args!("get_ptr_va_list({0:?})",
                                                    ptr) as &dyn Value))])
            });
    } else { ; }
};trace!("get_ptr_va_list({:?})", ptr);
1104        let (alloc_id, offset, _prov) = self.ptr_get_alloc_id(ptr, 0)?;
1105        if offset.bytes() != 0 {
1106            do yeet ::rustc_middle::mir::interpret::InterpErrorKind::UndefinedBehavior(::rustc_middle::mir::interpret::UndefinedBehaviorInfo::InvalidVaListPointer(Pointer::new(alloc_id,
                offset)))throw_ub!(InvalidVaListPointer(Pointer::new(alloc_id, offset)))
1107        }
1108
1109        let Some(va_list) = self.memory.va_list_map.get(&alloc_id) else {
1110            do yeet ::rustc_middle::mir::interpret::InterpErrorKind::UndefinedBehavior(::rustc_middle::mir::interpret::UndefinedBehaviorInfo::InvalidVaListPointer(Pointer::new(alloc_id,
                offset)))throw_ub!(InvalidVaListPointer(Pointer::new(alloc_id, offset)))
1111        };
1112
1113        interp_ok(va_list)
1114    }
1115
1116    /// Removes this VaList from the global map of variable argument lists. This does not deallocate
1117    /// the VaList elements, that happens when the Frame is popped.
1118    pub fn deallocate_va_list(
1119        &mut self,
1120        ptr: Pointer<Option<M::Provenance>>,
1121    ) -> InterpResult<'tcx, VecDeque<MPlaceTy<'tcx, M::Provenance>>> {
1122        {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event compiler/rustc_const_eval/src/interpret/memory.rs:1122",
                        "rustc_const_eval::interpret::memory",
                        ::tracing::Level::TRACE,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_const_eval/src/interpret/memory.rs"),
                        ::tracing_core::__macro_support::Option::Some(1122u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_const_eval::interpret::memory"),
                        ::tracing_core::field::FieldSet::new(&["message"],
                            ::tracing_core::callsite::Identifier(&__CALLSITE)),
                        ::tracing::metadata::Kind::EVENT)
                };
            ::tracing::callsite::DefaultCallsite::new(&META)
        };
    let enabled =
        ::tracing::Level::TRACE <= ::tracing::level_filters::STATIC_MAX_LEVEL
                &&
                ::tracing::Level::TRACE <=
                    ::tracing::level_filters::LevelFilter::current() &&
            {
                let interest = __CALLSITE.interest();
                !interest.is_never() &&
                    ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                        interest)
            };
    if enabled {
        (|value_set: ::tracing::field::ValueSet|
                    {
                        let meta = __CALLSITE.metadata();
                        ::tracing::Event::dispatch(meta, &value_set);
                        ;
                    })({
                #[allow(unused_imports)]
                use ::tracing::field::{debug, display, Value};
                let mut iter = __CALLSITE.metadata().fields().iter();
                __CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                    ::tracing::__macro_support::Option::Some(&format_args!("deallocate_va_list({0:?})",
                                                    ptr) as &dyn Value))])
            });
    } else { ; }
};trace!("deallocate_va_list({:?})", ptr);
1123        let (alloc_id, offset, _prov) = self.ptr_get_alloc_id(ptr, 0)?;
1124        if offset.bytes() != 0 {
1125            do yeet ::rustc_middle::mir::interpret::InterpErrorKind::UndefinedBehavior(::rustc_middle::mir::interpret::UndefinedBehaviorInfo::InvalidVaListPointer(Pointer::new(alloc_id,
                offset)))throw_ub!(InvalidVaListPointer(Pointer::new(alloc_id, offset)))
1126        }
1127
1128        let Some(va_list) = self.memory.va_list_map.swap_remove(&alloc_id) else {
1129            do yeet ::rustc_middle::mir::interpret::InterpErrorKind::UndefinedBehavior(::rustc_middle::mir::interpret::UndefinedBehaviorInfo::InvalidVaListPointer(Pointer::new(alloc_id,
                offset)))throw_ub!(InvalidVaListPointer(Pointer::new(alloc_id, offset)))
1130        };
1131
1132        self.memory.dead_alloc_map.insert(alloc_id, (Size::ZERO, Align::ONE));
1133        interp_ok(va_list)
1134    }
1135
1136    /// Get the dynamic type of the given vtable pointer.
1137    /// If `expected_trait` is `Some`, it must be a vtable for the given trait.
1138    pub fn get_ptr_vtable_ty(
1139        &self,
1140        ptr: Pointer<Option<M::Provenance>>,
1141        expected_trait: Option<&'tcx ty::List<ty::PolyExistentialPredicate<'tcx>>>,
1142    ) -> InterpResult<'tcx, Ty<'tcx>> {
1143        {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event compiler/rustc_const_eval/src/interpret/memory.rs:1143",
                        "rustc_const_eval::interpret::memory",
                        ::tracing::Level::TRACE,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_const_eval/src/interpret/memory.rs"),
                        ::tracing_core::__macro_support::Option::Some(1143u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_const_eval::interpret::memory"),
                        ::tracing_core::field::FieldSet::new(&["message"],
                            ::tracing_core::callsite::Identifier(&__CALLSITE)),
                        ::tracing::metadata::Kind::EVENT)
                };
            ::tracing::callsite::DefaultCallsite::new(&META)
        };
    let enabled =
        ::tracing::Level::TRACE <= ::tracing::level_filters::STATIC_MAX_LEVEL
                &&
                ::tracing::Level::TRACE <=
                    ::tracing::level_filters::LevelFilter::current() &&
            {
                let interest = __CALLSITE.interest();
                !interest.is_never() &&
                    ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                        interest)
            };
    if enabled {
        (|value_set: ::tracing::field::ValueSet|
                    {
                        let meta = __CALLSITE.metadata();
                        ::tracing::Event::dispatch(meta, &value_set);
                        ;
                    })({
                #[allow(unused_imports)]
                use ::tracing::field::{debug, display, Value};
                let mut iter = __CALLSITE.metadata().fields().iter();
                __CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                    ::tracing::__macro_support::Option::Some(&format_args!("get_ptr_vtable({0:?})",
                                                    ptr) as &dyn Value))])
            });
    } else { ; }
};trace!("get_ptr_vtable({:?})", ptr);
1144        let (alloc_id, offset, _tag) = self.ptr_get_alloc_id(ptr, 0)?;
1145        if offset.bytes() != 0 {
1146            do yeet ::rustc_middle::mir::interpret::InterpErrorKind::UndefinedBehavior(::rustc_middle::mir::interpret::UndefinedBehaviorInfo::InvalidVTablePointer(Pointer::new(alloc_id,
                offset)))throw_ub!(InvalidVTablePointer(Pointer::new(alloc_id, offset)))
1147        }
1148        let Some(GlobalAlloc::VTable(ty, vtable_dyn_type)) =
1149            self.tcx.try_get_global_alloc(alloc_id)
1150        else {
1151            do yeet ::rustc_middle::mir::interpret::InterpErrorKind::UndefinedBehavior(::rustc_middle::mir::interpret::UndefinedBehaviorInfo::InvalidVTablePointer(Pointer::new(alloc_id,
                offset)))throw_ub!(InvalidVTablePointer(Pointer::new(alloc_id, offset)))
1152        };
1153        if let Some(expected_dyn_type) = expected_trait {
1154            self.check_vtable_for_type(vtable_dyn_type, expected_dyn_type)?;
1155        }
1156        interp_ok(ty)
1157    }
1158
1159    pub fn alloc_mark_immutable(&mut self, id: AllocId) -> InterpResult<'tcx> {
1160        self.get_alloc_raw_mut(id)?.0.mutability = Mutability::Not;
1161        interp_ok(())
1162    }
1163
1164    /// Visit all allocations reachable from the given start set, by recursively traversing the
1165    /// provenance information of those allocations.
1166    pub fn visit_reachable_allocs(
1167        &mut self,
1168        start: Vec<AllocId>,
1169        mut visit: impl FnMut(&mut Self, AllocId, &AllocInfo) -> InterpResult<'tcx>,
1170    ) -> InterpResult<'tcx> {
1171        let mut done = FxHashSet::default();
1172        let mut todo = start;
1173        while let Some(id) = todo.pop() {
1174            if !done.insert(id) {
1175                // We already saw this allocation before, don't process it again.
1176                continue;
1177            }
1178            let info = self.get_alloc_info(id);
1179
1180            // Recurse, if there is data here.
1181            // Do this *before* invoking the callback, as the callback might mutate the
1182            // allocation and e.g. replace all provenance by wildcards!
1183            if info.kind == AllocKind::LiveData {
1184                let alloc = self.get_alloc_raw(id)?;
1185                for prov in alloc.provenance().provenances() {
1186                    if let Some(id) = prov.get_alloc_id() {
1187                        todo.push(id);
1188                    }
1189                }
1190            }
1191
1192            // Call the callback.
1193            visit(self, id, &info)?;
1194        }
1195        interp_ok(())
1196    }
1197
1198    /// Create a lazy debug printer that prints the given allocation and all allocations it points
1199    /// to, recursively.
1200    #[must_use]
1201    pub fn dump_alloc<'a>(&'a self, id: AllocId) -> DumpAllocs<'a, 'tcx, M> {
1202        self.dump_allocs(::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
        [id]))vec![id])
1203    }
1204
1205    /// Create a lazy debug printer for a list of allocations and all allocations they point to,
1206    /// recursively.
1207    #[must_use]
1208    pub fn dump_allocs<'a>(&'a self, mut allocs: Vec<AllocId>) -> DumpAllocs<'a, 'tcx, M> {
1209        allocs.sort();
1210        allocs.dedup();
1211        DumpAllocs { ecx: self, allocs }
1212    }
1213
1214    /// Print the allocation's bytes, without any nested allocations.
1215    pub fn print_alloc_bytes_for_diagnostics(&self, id: AllocId) -> String {
1216        // Using the "raw" access to avoid the `before_alloc_read` hook, we specifically
1217        // want to be able to read all memory for diagnostics, even if that is cyclic.
1218        let alloc = self.get_alloc_raw(id).unwrap();
1219        let mut bytes = String::new();
1220        if alloc.size() != Size::ZERO {
1221            bytes = "\n".into();
1222            // FIXME(translation) there might be pieces that are translatable.
1223            rustc_middle::mir::pretty::write_allocation_bytes(*self.tcx, alloc, &mut bytes, "    ")
1224                .unwrap();
1225        }
1226        bytes
1227    }
1228
1229    /// Find leaked allocations, remove them from memory and return them. Allocations reachable from
1230    /// `static_roots` or a `Global` allocation are not considered leaked, as well as leaks whose
1231    /// kind's `may_leak()` returns true.
1232    ///
1233    /// This is highly destructive, no more execution can happen after this!
1234    pub fn take_leaked_allocations(
1235        &mut self,
1236        static_roots: impl FnOnce(&Self) -> &[AllocId],
1237    ) -> Vec<(AllocId, MemoryKind<M::MemoryKind>, Allocation<M::Provenance, M::AllocExtra, M::Bytes>)>
1238    {
1239        // Collect the set of allocations that are *reachable* from `Global` allocations.
1240        let reachable = {
1241            let mut reachable = FxHashSet::default();
1242            let global_kind = M::GLOBAL_KIND.map(MemoryKind::Machine);
1243            let mut todo: Vec<_> =
1244                self.memory.alloc_map.filter_map_collect(move |&id, &(kind, _)| {
1245                    if Some(kind) == global_kind { Some(id) } else { None }
1246                });
1247            todo.extend(static_roots(self));
1248            while let Some(id) = todo.pop() {
1249                if reachable.insert(id) {
1250                    // This is a new allocation, add the allocations it points to `todo`.
1251                    // We only need to care about `alloc_map` memory here, as entirely unchanged
1252                    // global memory cannot point to memory relevant for the leak check.
1253                    if let Some((_, alloc)) = self.memory.alloc_map.get(id) {
1254                        todo.extend(
1255                            alloc.provenance().provenances().filter_map(|prov| prov.get_alloc_id()),
1256                        );
1257                    }
1258                }
1259            }
1260            reachable
1261        };
1262
1263        // All allocations that are *not* `reachable` and *not* `may_leak` are considered leaking.
1264        let leaked: Vec<_> = self.memory.alloc_map.filter_map_collect(|&id, &(kind, _)| {
1265            if kind.may_leak() || reachable.contains(&id) { None } else { Some(id) }
1266        });
1267        let mut result = Vec::new();
1268        for &id in leaked.iter() {
1269            let (kind, alloc) = self.memory.alloc_map.remove(&id).unwrap();
1270            result.push((id, kind, alloc));
1271        }
1272        result
1273    }
1274
1275    /// Runs the closure in "validation" mode, which means the machine's memory read hooks will be
1276    /// suppressed. Needless to say, this must only be set with great care! Cannot be nested.
1277    ///
1278    /// We do this so Miri's allocation access tracking does not show the validation
1279    /// reads as spurious accesses.
1280    pub fn run_for_validation_mut<R>(&mut self, f: impl FnOnce(&mut Self) -> R) -> R {
1281        // This deliberately uses `==` on `bool` to follow the pattern
1282        // `assert!(val.replace(new) == old)`.
1283        if !(self.memory.validation_in_progress.replace(true) == false) {
    {
        ::core::panicking::panic_fmt(format_args!("`validation_in_progress` was already set"));
    }
};assert!(
1284            self.memory.validation_in_progress.replace(true) == false,
1285            "`validation_in_progress` was already set"
1286        );
1287        let res = f(self);
1288        if !(self.memory.validation_in_progress.replace(false) == true) {
    {
        ::core::panicking::panic_fmt(format_args!("`validation_in_progress` was unset by someone else"));
    }
};assert!(
1289            self.memory.validation_in_progress.replace(false) == true,
1290            "`validation_in_progress` was unset by someone else"
1291        );
1292        res
1293    }
1294
1295    /// Runs the closure in "validation" mode, which means the machine's memory read hooks will be
1296    /// suppressed. Needless to say, this must only be set with great care! Cannot be nested.
1297    ///
1298    /// We do this so Miri's allocation access tracking does not show the validation
1299    /// reads as spurious accesses.
1300    pub fn run_for_validation_ref<R>(&self, f: impl FnOnce(&Self) -> R) -> R {
1301        // This deliberately uses `==` on `bool` to follow the pattern
1302        // `assert!(val.replace(new) == old)`.
1303        if !(self.memory.validation_in_progress.replace(true) == false) {
    {
        ::core::panicking::panic_fmt(format_args!("`validation_in_progress` was already set"));
    }
};assert!(
1304            self.memory.validation_in_progress.replace(true) == false,
1305            "`validation_in_progress` was already set"
1306        );
1307        let res = f(self);
1308        if !(self.memory.validation_in_progress.replace(false) == true) {
    {
        ::core::panicking::panic_fmt(format_args!("`validation_in_progress` was unset by someone else"));
    }
};assert!(
1309            self.memory.validation_in_progress.replace(false) == true,
1310            "`validation_in_progress` was unset by someone else"
1311        );
1312        res
1313    }
1314
1315    pub(super) fn validation_in_progress(&self) -> bool {
1316        self.memory.validation_in_progress.get()
1317    }
1318}
1319
1320#[doc(hidden)]
1321/// There's no way to use this directly, it's just a helper struct for the `dump_alloc(s)` methods.
1322pub struct DumpAllocs<'a, 'tcx, M: Machine<'tcx>> {
1323    ecx: &'a InterpCx<'tcx, M>,
1324    allocs: Vec<AllocId>,
1325}
1326
1327impl<'a, 'tcx, M: Machine<'tcx>> std::fmt::Debug for DumpAllocs<'a, 'tcx, M> {
1328    fn fmt(&self, fmt: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
1329        // Cannot be a closure because it is generic in `Prov`, `Extra`.
1330        fn write_allocation_track_relocs<'tcx, Prov: Provenance, Extra, Bytes: AllocBytes>(
1331            fmt: &mut std::fmt::Formatter<'_>,
1332            tcx: TyCtxt<'tcx>,
1333            allocs_to_print: &mut VecDeque<AllocId>,
1334            alloc: &Allocation<Prov, Extra, Bytes>,
1335        ) -> std::fmt::Result {
1336            for alloc_id in alloc.provenance().provenances().filter_map(|prov| prov.get_alloc_id())
1337            {
1338                allocs_to_print.push_back(alloc_id);
1339            }
1340            fmt.write_fmt(format_args!("{0}", display_allocation(tcx, alloc)))write!(fmt, "{}", display_allocation(tcx, alloc))
1341        }
1342
1343        let mut allocs_to_print: VecDeque<_> = self.allocs.iter().copied().collect();
1344        // `allocs_printed` contains all allocations that we have already printed.
1345        let mut allocs_printed = FxHashSet::default();
1346
1347        while let Some(id) = allocs_to_print.pop_front() {
1348            if !allocs_printed.insert(id) {
1349                // Already printed, so skip this.
1350                continue;
1351            }
1352
1353            fmt.write_fmt(format_args!("{0:?}", id))write!(fmt, "{id:?}")?;
1354            match self.ecx.memory.alloc_map.get(id) {
1355                Some((kind, alloc)) => {
1356                    // normal alloc
1357                    fmt.write_fmt(format_args!(" ({0}, ", kind))write!(fmt, " ({kind}, ")?;
1358                    write_allocation_track_relocs(
1359                        &mut *fmt,
1360                        *self.ecx.tcx,
1361                        &mut allocs_to_print,
1362                        alloc,
1363                    )?;
1364                }
1365                None => {
1366                    // global alloc
1367                    match self.ecx.tcx.try_get_global_alloc(id) {
1368                        Some(GlobalAlloc::Memory(alloc)) => {
1369                            fmt.write_fmt(format_args!(" (unchanged global, "))write!(fmt, " (unchanged global, ")?;
1370                            write_allocation_track_relocs(
1371                                &mut *fmt,
1372                                *self.ecx.tcx,
1373                                &mut allocs_to_print,
1374                                alloc.inner(),
1375                            )?;
1376                        }
1377                        Some(GlobalAlloc::Function { instance, .. }) => {
1378                            fmt.write_fmt(format_args!(" (fn: {0})", instance))write!(fmt, " (fn: {instance})")?;
1379                        }
1380                        Some(GlobalAlloc::VTable(ty, dyn_ty)) => {
1381                            fmt.write_fmt(format_args!(" (vtable: impl {0} for {1})", dyn_ty, ty))write!(fmt, " (vtable: impl {dyn_ty} for {ty})")?;
1382                        }
1383                        Some(GlobalAlloc::TypeId { ty }) => {
1384                            fmt.write_fmt(format_args!(" (typeid for {0})", ty))write!(fmt, " (typeid for {ty})")?;
1385                        }
1386                        Some(GlobalAlloc::Static(did)) => {
1387                            fmt.write_fmt(format_args!(" (static: {0})", self.ecx.tcx.def_path_str(did)))write!(fmt, " (static: {})", self.ecx.tcx.def_path_str(did))?;
1388                        }
1389                        None => {
1390                            fmt.write_fmt(format_args!(" (deallocated)"))write!(fmt, " (deallocated)")?;
1391                        }
1392                    }
1393                }
1394            }
1395            fmt.write_fmt(format_args!("\n"))writeln!(fmt)?;
1396        }
1397        Ok(())
1398    }
1399}
1400
1401/// Reading and writing.
1402impl<'a, 'tcx, Prov: Provenance, Extra, Bytes: AllocBytes>
1403    AllocRefMut<'a, 'tcx, Prov, Extra, Bytes>
1404{
1405    pub fn as_ref<'b>(&'b self) -> AllocRef<'b, 'tcx, Prov, Extra, Bytes> {
1406        AllocRef { alloc: self.alloc, range: self.range, tcx: self.tcx, alloc_id: self.alloc_id }
1407    }
1408
1409    /// `range` is relative to this allocation reference, not the base of the allocation.
1410    pub fn write_scalar(&mut self, range: AllocRange, val: Scalar<Prov>) -> InterpResult<'tcx> {
1411        let range = self.range.subrange(range);
1412        {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event compiler/rustc_const_eval/src/interpret/memory.rs:1412",
                        "rustc_const_eval::interpret::memory",
                        ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_const_eval/src/interpret/memory.rs"),
                        ::tracing_core::__macro_support::Option::Some(1412u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_const_eval::interpret::memory"),
                        ::tracing_core::field::FieldSet::new(&["message"],
                            ::tracing_core::callsite::Identifier(&__CALLSITE)),
                        ::tracing::metadata::Kind::EVENT)
                };
            ::tracing::callsite::DefaultCallsite::new(&META)
        };
    let enabled =
        ::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
                &&
                ::tracing::Level::DEBUG <=
                    ::tracing::level_filters::LevelFilter::current() &&
            {
                let interest = __CALLSITE.interest();
                !interest.is_never() &&
                    ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                        interest)
            };
    if enabled {
        (|value_set: ::tracing::field::ValueSet|
                    {
                        let meta = __CALLSITE.metadata();
                        ::tracing::Event::dispatch(meta, &value_set);
                        ;
                    })({
                #[allow(unused_imports)]
                use ::tracing::field::{debug, display, Value};
                let mut iter = __CALLSITE.metadata().fields().iter();
                __CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                    ::tracing::__macro_support::Option::Some(&format_args!("write_scalar at {0:?}{1:?}: {2:?}",
                                                    self.alloc_id, range, val) as &dyn Value))])
            });
    } else { ; }
};debug!("write_scalar at {:?}{range:?}: {val:?}", self.alloc_id);
1413
1414        self.alloc
1415            .write_scalar(&self.tcx, range, val)
1416            .map_err(|e| e.to_interp_error(self.alloc_id))
1417            .into()
1418    }
1419
1420    /// `offset` is relative to this allocation reference, not the base of the allocation.
1421    pub fn write_ptr_sized(&mut self, offset: Size, val: Scalar<Prov>) -> InterpResult<'tcx> {
1422        self.write_scalar(alloc_range(offset, self.tcx.data_layout().pointer_size()), val)
1423    }
1424
1425    /// Mark the given sub-range (relative to this allocation reference) as uninitialized.
1426    pub fn write_uninit(&mut self, range: AllocRange) {
1427        let range = self.range.subrange(range);
1428
1429        self.alloc.write_uninit(&self.tcx, range);
1430    }
1431
1432    /// Mark the entire referenced range as uninitialized
1433    pub fn write_uninit_full(&mut self) {
1434        self.alloc.write_uninit(&self.tcx, self.range);
1435    }
1436
1437    /// Remove all provenance in the reference range.
1438    pub fn clear_provenance(&mut self) {
1439        self.alloc.clear_provenance(&self.tcx, self.range);
1440    }
1441}
1442
1443impl<'a, 'tcx, Prov: Provenance, Extra, Bytes: AllocBytes> AllocRef<'a, 'tcx, Prov, Extra, Bytes> {
1444    /// `range` is relative to this allocation reference, not the base of the allocation.
1445    pub fn read_scalar(
1446        &self,
1447        range: AllocRange,
1448        read_provenance: bool,
1449    ) -> InterpResult<'tcx, Scalar<Prov>> {
1450        let range = self.range.subrange(range);
1451        self.alloc
1452            .read_scalar(&self.tcx, range, read_provenance)
1453            .map_err(|e| e.to_interp_error(self.alloc_id))
1454            .into()
1455    }
1456
1457    /// `range` is relative to this allocation reference, not the base of the allocation.
1458    pub fn read_integer(&self, range: AllocRange) -> InterpResult<'tcx, Scalar<Prov>> {
1459        self.read_scalar(range, /*read_provenance*/ false)
1460    }
1461
1462    /// `offset` is relative to this allocation reference, not the base of the allocation.
1463    pub fn read_pointer(&self, offset: Size) -> InterpResult<'tcx, Scalar<Prov>> {
1464        self.read_scalar(
1465            alloc_range(offset, self.tcx.data_layout().pointer_size()),
1466            /*read_provenance*/ true,
1467        )
1468    }
1469
1470    /// `range` is relative to this allocation reference, not the base of the allocation.
1471    pub fn get_bytes_strip_provenance<'b>(&'b self) -> InterpResult<'tcx, &'a [u8]> {
1472        self.alloc
1473            .get_bytes_strip_provenance(&self.tcx, self.range)
1474            .map_err(|e| e.to_interp_error(self.alloc_id))
1475            .into()
1476    }
1477
1478    /// Returns whether the allocation has provenance anywhere in the range of the `AllocRef`.
1479    pub fn has_provenance(&self) -> bool {
1480        !self.alloc.provenance().range_empty(self.range, &self.tcx)
1481    }
1482}
1483
1484impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
1485    /// Reads the given number of bytes from memory, and strips their provenance if possible.
1486    /// Returns them as a slice.
1487    ///
1488    /// Performs appropriate bounds checks.
1489    pub fn read_bytes_ptr_strip_provenance(
1490        &self,
1491        ptr: Pointer<Option<M::Provenance>>,
1492        size: Size,
1493    ) -> InterpResult<'tcx, &[u8]> {
1494        let Some(alloc_ref) = self.get_ptr_alloc(ptr, size)? else {
1495            // zero-sized access
1496            return interp_ok(&[]);
1497        };
1498        // Side-step AllocRef and directly access the underlying bytes more efficiently.
1499        // (We are staying inside the bounds here so all is good.)
1500        interp_ok(
1501            alloc_ref
1502                .alloc
1503                .get_bytes_strip_provenance(&alloc_ref.tcx, alloc_ref.range)
1504                .map_err(|e| e.to_interp_error(alloc_ref.alloc_id))?,
1505        )
1506    }
1507
1508    /// Writes the given stream of bytes into memory.
1509    ///
1510    /// Performs appropriate bounds checks.
1511    pub fn write_bytes_ptr(
1512        &mut self,
1513        ptr: Pointer<Option<M::Provenance>>,
1514        src: impl IntoIterator<Item = u8>,
1515    ) -> InterpResult<'tcx> {
1516        let mut src = src.into_iter();
1517        let (lower, upper) = src.size_hint();
1518        let len = upper.expect("can only write bounded iterators");
1519        match (&lower, &len) {
    (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::Some(format_args!("can only write iterators with a precise length")));
        }
    }
};assert_eq!(lower, len, "can only write iterators with a precise length");
1520
1521        let size = Size::from_bytes(len);
1522        let Some(alloc_ref) = self.get_ptr_alloc_mut(ptr, size)? else {
1523            // zero-sized access
1524            match src.next() {
    None => {}
    ref left_val => {
        ::core::panicking::assert_matches_failed(left_val, "None",
            ::core::option::Option::Some(format_args!("iterator said it was empty but returned an element")));
    }
};assert_matches!(src.next(), None, "iterator said it was empty but returned an element");
1525            return interp_ok(());
1526        };
1527
1528        // Side-step AllocRef and directly access the underlying bytes more efficiently.
1529        // (We are staying inside the bounds here and all bytes do get overwritten so all is good.)
1530        let bytes =
1531            alloc_ref.alloc.get_bytes_unchecked_for_overwrite(&alloc_ref.tcx, alloc_ref.range);
1532        // `zip` would stop when the first iterator ends; we want to definitely
1533        // cover all of `bytes`.
1534        for dest in bytes {
1535            *dest = src.next().expect("iterator was shorter than it said it would be");
1536        }
1537        match src.next() {
    None => {}
    ref left_val => {
        ::core::panicking::assert_matches_failed(left_val, "None",
            ::core::option::Option::Some(format_args!("iterator was longer than it said it would be")));
    }
};assert_matches!(src.next(), None, "iterator was longer than it said it would be");
1538        interp_ok(())
1539    }
1540
1541    pub fn mem_copy(
1542        &mut self,
1543        src: Pointer<Option<M::Provenance>>,
1544        dest: Pointer<Option<M::Provenance>>,
1545        size: Size,
1546        nonoverlapping: bool,
1547    ) -> InterpResult<'tcx> {
1548        self.mem_copy_repeatedly(src, dest, size, 1, nonoverlapping)
1549    }
1550
1551    /// Performs `num_copies` many copies of `size` many bytes from `src` to `dest + i*size` (where
1552    /// `i` is the index of the copy).
1553    ///
1554    /// Either `nonoverlapping` must be true or `num_copies` must be 1; doing repeated copies that
1555    /// may overlap is not supported.
1556    pub fn mem_copy_repeatedly(
1557        &mut self,
1558        src: Pointer<Option<M::Provenance>>,
1559        dest: Pointer<Option<M::Provenance>>,
1560        size: Size,
1561        num_copies: u64,
1562        nonoverlapping: bool,
1563    ) -> InterpResult<'tcx> {
1564        let tcx = self.tcx;
1565        // We need to do our own bounds-checks.
1566        let src_parts = self.get_ptr_access(src, size)?;
1567        let dest_parts = self.get_ptr_access(dest, size * num_copies)?; // `Size` multiplication
1568
1569        // Similar to `get_ptr_alloc`, we need to call `before_alloc_access` even for zero-sized
1570        // reads. However, just like in `get_ptr_alloc_mut`, the write part is okay to skip for
1571        // zero-sized writes.
1572        if let Ok((alloc_id, ..)) = self.ptr_try_get_alloc_id(src, size.bytes().try_into().unwrap())
1573        {
1574            M::before_alloc_access(tcx, &self.machine, alloc_id)?;
1575        }
1576
1577        // FIXME: we look up both allocations twice here, once before for the `check_ptr_access`
1578        // and once below to get the underlying `&[mut] Allocation`.
1579
1580        // Source alloc preparations and access hooks.
1581        let Some((src_alloc_id, src_offset, src_prov)) = src_parts else {
1582            // Zero-sized *source*, that means dest is also zero-sized and we have nothing to do.
1583            return interp_ok(());
1584        };
1585        let src_alloc = self.get_alloc_raw(src_alloc_id)?;
1586        let src_range = alloc_range(src_offset, size);
1587        if !!self.memory.validation_in_progress.get() {
    {
        ::core::panicking::panic_fmt(format_args!("we can\'t be copying during validation"));
    }
};assert!(!self.memory.validation_in_progress.get(), "we can't be copying during validation");
1588
1589        // Trigger read hook.
1590        // For the overlapping case, it is crucial that we trigger the read hook
1591        // before the write hook -- the aliasing model cares about the order.
1592        M::before_memory_read(
1593            tcx,
1594            &self.machine,
1595            &src_alloc.extra,
1596            src,
1597            (src_alloc_id, src_prov),
1598            src_range,
1599        )?;
1600        // We need the `dest` ptr for the next operation, so we get it now.
1601        // We already did the source checks and called the hooks so we are good to return early.
1602        let Some((dest_alloc_id, dest_offset, dest_prov)) = dest_parts else {
1603            // Zero-sized *destination*.
1604            return interp_ok(());
1605        };
1606
1607        // Prepare getting source provenance.
1608        let src_bytes = src_alloc.get_bytes_unchecked(src_range).as_ptr(); // raw ptr, so we can also get a ptr to the destination allocation
1609        // First copy the provenance to a temporary buffer, because
1610        // `get_bytes_unchecked_for_overwrite_ptr` will clear the provenance (in preparation for
1611        // inserting the new provenance), and that can overlap with the source range.
1612        let provenance = src_alloc.provenance_prepare_copy(src_range, self);
1613        // Prepare a copy of the initialization mask.
1614        let init = src_alloc.init_mask().prepare_copy(src_range);
1615
1616        // Destination alloc preparations...
1617        let (dest_alloc, machine) = self.get_alloc_raw_mut(dest_alloc_id)?;
1618        let dest_range = alloc_range(dest_offset, size * num_copies);
1619        // ...and access hooks.
1620        M::before_alloc_access(tcx, machine, dest_alloc_id)?;
1621        M::before_memory_write(
1622            tcx,
1623            machine,
1624            &mut dest_alloc.extra,
1625            dest,
1626            (dest_alloc_id, dest_prov),
1627            dest_range,
1628        )?;
1629        // Yes we do overwrite all bytes in `dest_bytes`.
1630        let dest_bytes =
1631            dest_alloc.get_bytes_unchecked_for_overwrite_ptr(&tcx, dest_range).as_mut_ptr();
1632
1633        if init.no_bytes_init() {
1634            // Fast path: If all bytes are `uninit` then there is nothing to copy. The target range
1635            // is marked as uninitialized but we otherwise omit changing the byte representation which may
1636            // be arbitrary for uninitialized bytes.
1637            // This also avoids writing to the target bytes so that the backing allocation is never
1638            // touched if the bytes stay uninitialized for the whole interpreter execution. On contemporary
1639            // operating system this can avoid physically allocating the page.
1640            dest_alloc.write_uninit(&tcx, dest_range);
1641            // `write_uninit` also resets the provenance, so we are done.
1642            return interp_ok(());
1643        }
1644
1645        // SAFE: The above indexing would have panicked if there weren't at least `size` bytes
1646        // behind `src` and `dest`. Also, we use the overlapping-safe `ptr::copy` if `src` and
1647        // `dest` could possibly overlap.
1648        // The pointers above remain valid even if the `HashMap` table is moved around because they
1649        // point into the `Vec` storing the bytes.
1650        unsafe {
1651            if src_alloc_id == dest_alloc_id {
1652                if nonoverlapping {
1653                    // `Size` additions
1654                    if (src_offset <= dest_offset && src_offset + size > dest_offset)
1655                        || (dest_offset <= src_offset && dest_offset + size > src_offset)
1656                    {
1657                        do yeet {
        ::rustc_middle::mir::interpret::InterpErrorKind::UndefinedBehavior(::rustc_middle::mir::interpret::UndefinedBehaviorInfo::Custom(::rustc_middle::error::CustomSubdiagnostic {
                    msg: ||
                        rustc_errors::DiagMessage::Inline(std::borrow::Cow::Borrowed("`copy_nonoverlapping` called on overlapping ranges")),
                    add_args: Box::new(move |mut set_arg| {}),
                }))
    };throw_ub_custom!(msg!(
1658                            "`copy_nonoverlapping` called on overlapping ranges"
1659                        ));
1660                    }
1661                }
1662            }
1663            if num_copies > 1 {
1664                if !nonoverlapping {
    {
        ::core::panicking::panic_fmt(format_args!("multi-copy only supported in non-overlapping mode"));
    }
};assert!(nonoverlapping, "multi-copy only supported in non-overlapping mode");
1665            }
1666
1667            let size_in_bytes = size.bytes_usize();
1668            // For particularly large arrays (where this is perf-sensitive) it's common that
1669            // we're writing a single byte repeatedly. So, optimize that case to a memset.
1670            if size_in_bytes == 1 {
1671                if true {
    if !(num_copies >= 1) {
        ::core::panicking::panic("assertion failed: num_copies >= 1")
    };
};debug_assert!(num_copies >= 1); // we already handled the zero-sized cases above.
1672                // SAFETY: `src_bytes` would be read from anyway by `copy` below (num_copies >= 1).
1673                let value = *src_bytes;
1674                dest_bytes.write_bytes(value, (size * num_copies).bytes_usize());
1675            } else if src_alloc_id == dest_alloc_id {
1676                let mut dest_ptr = dest_bytes;
1677                for _ in 0..num_copies {
1678                    // Here we rely on `src` and `dest` being non-overlapping if there is more than
1679                    // one copy.
1680                    ptr::copy(src_bytes, dest_ptr, size_in_bytes);
1681                    dest_ptr = dest_ptr.add(size_in_bytes);
1682                }
1683            } else {
1684                let mut dest_ptr = dest_bytes;
1685                for _ in 0..num_copies {
1686                    ptr::copy_nonoverlapping(src_bytes, dest_ptr, size_in_bytes);
1687                    dest_ptr = dest_ptr.add(size_in_bytes);
1688                }
1689            }
1690        }
1691
1692        // now fill in all the "init" data
1693        dest_alloc.init_mask_apply_copy(
1694            init,
1695            alloc_range(dest_offset, size), // just a single copy (i.e., not full `dest_range`)
1696            num_copies,
1697        );
1698        // copy the provenance to the destination
1699        dest_alloc.provenance_apply_copy(provenance, alloc_range(dest_offset, size), num_copies);
1700
1701        interp_ok(())
1702    }
1703}
1704
1705/// Machine pointer introspection.
1706impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
1707    /// Test if this value might be null.
1708    /// If the machine does not support ptr-to-int casts, this is conservative.
1709    pub fn scalar_may_be_null(&self, scalar: Scalar<M::Provenance>) -> InterpResult<'tcx, bool> {
1710        match scalar.try_to_scalar_int() {
1711            Ok(int) => interp_ok(int.is_null()),
1712            Err(_) => {
1713                // We can't cast this pointer to an integer. Can only happen during CTFE.
1714                let ptr = scalar.to_pointer(self)?;
1715                match self.ptr_try_get_alloc_id(ptr, 0) {
1716                    Ok((alloc_id, offset, _)) => {
1717                        let info = self.get_alloc_info(alloc_id);
1718                        if info.kind == AllocKind::TypeId {
1719                            // We *could* actually precisely answer this question since here,
1720                            // the offset *is* the integer value. But the entire point of making
1721                            // this a pointer is not to leak the integer value, so we say everything
1722                            // might be null.
1723                            return interp_ok(true);
1724                        }
1725                        // If the pointer is in-bounds (including "at the end"), it is definitely not null.
1726                        if offset <= info.size {
1727                            return interp_ok(false);
1728                        }
1729                        // If the allocation is N-aligned, and the offset is not divisible by N,
1730                        // then `base + offset` has a non-zero remainder after division by `N`,
1731                        // which means `base + offset` cannot be null.
1732                        if !offset.bytes().is_multiple_of(info.align.bytes()) {
1733                            return interp_ok(false);
1734                        }
1735                        // We don't know enough, this might be null.
1736                        interp_ok(true)
1737                    }
1738                    Err(_offset) => ::rustc_middle::util::bug::bug_fmt(format_args!("a non-int scalar is always a pointer"))bug!("a non-int scalar is always a pointer"),
1739                }
1740            }
1741        }
1742    }
1743
1744    /// Turning a "maybe pointer" into a proper pointer (and some information
1745    /// about where it points), or an absolute address.
1746    ///
1747    /// `size` says how many bytes of memory are expected at that pointer. This is largely only used
1748    /// for error messages; however, the *sign* of `size` can be used to disambiguate situations
1749    /// where a wildcard pointer sits right in between two allocations.
1750    /// It is almost always okay to just set the size to 0; this will be treated like a positive size
1751    /// for handling wildcard pointers.
1752    ///
1753    /// The result must be used immediately; it is not allowed to convert
1754    /// the returned data back into a `Pointer` and store that in machine state.
1755    /// (In fact that's not even possible since `M::ProvenanceExtra` is generic and
1756    /// we don't have an operation to turn it back into `M::Provenance`.)
1757    pub fn ptr_try_get_alloc_id(
1758        &self,
1759        ptr: Pointer<Option<M::Provenance>>,
1760        size: i64,
1761    ) -> Result<(AllocId, Size, M::ProvenanceExtra), u64> {
1762        match ptr.into_pointer_or_addr() {
1763            Ok(ptr) => match M::ptr_get_alloc(self, ptr, size) {
1764                Some((alloc_id, offset, extra)) => Ok((alloc_id, offset, extra)),
1765                None => {
1766                    if !M::Provenance::OFFSET_IS_ADDR {
    ::core::panicking::panic("assertion failed: M::Provenance::OFFSET_IS_ADDR")
};assert!(M::Provenance::OFFSET_IS_ADDR);
1767                    // Offset is absolute, as we just asserted.
1768                    let (_, addr) = ptr.into_raw_parts();
1769                    Err(addr.bytes())
1770                }
1771            },
1772            Err(addr) => Err(addr.bytes()),
1773        }
1774    }
1775
1776    /// Turning a "maybe pointer" into a proper pointer (and some information about where it points).
1777    ///
1778    /// `size` says how many bytes of memory are expected at that pointer. This is largely only used
1779    /// for error messages; however, the *sign* of `size` can be used to disambiguate situations
1780    /// where a wildcard pointer sits right in between two allocations.
1781    /// It is almost always okay to just set the size to 0; this will be treated like a positive size
1782    /// for handling wildcard pointers.
1783    ///
1784    /// The result must be used immediately; it is not allowed to convert
1785    /// the returned data back into a `Pointer` and store that in machine state.
1786    /// (In fact that's not even possible since `M::ProvenanceExtra` is generic and
1787    /// we don't have an operation to turn it back into `M::Provenance`.)
1788    #[inline(always)]
1789    pub fn ptr_get_alloc_id(
1790        &self,
1791        ptr: Pointer<Option<M::Provenance>>,
1792        size: i64,
1793    ) -> InterpResult<'tcx, (AllocId, Size, M::ProvenanceExtra)> {
1794        self.ptr_try_get_alloc_id(ptr, size)
1795            .map_err(|offset| {
1796                ::rustc_middle::mir::interpret::InterpErrorKind::UndefinedBehavior(::rustc_middle::mir::interpret::UndefinedBehaviorInfo::DanglingIntPointer {
        addr: offset,
        inbounds_size: size,
        msg: CheckInAllocMsg::Dereferenceable,
    })err_ub!(DanglingIntPointer {
1797                    addr: offset,
1798                    inbounds_size: size,
1799                    msg: CheckInAllocMsg::Dereferenceable
1800                })
1801            })
1802            .into()
1803    }
1804}