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!
89use std::borrow::{Borrow, Cow};
10use std::cell::Cell;
11use std::collections::VecDeque;
12use std::{fmt, ptr};
1314use 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};
2324use super::{
25AllocBytes, AllocId, AllocInit, AllocMap, AllocRange, Allocation, CheckAlignMsg,
26CheckInAllocMsg, CtfeProvenance, GlobalAlloc, InterpCx, InterpResult, MPlaceTy, Machine,
27MayLeak, Misalignment, Pointer, PointerArithmetic, Provenance, Scalar, alloc_range, err_ub,
28err_ub_custom, interp_ok, throw_ub, throw_ub_custom, throw_unsup, throw_unsup_format,
29};
30use crate::const_eval::ConstEvalErrKind;
3132#[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.
35Stack,
36/// Memory allocated by `caller_location` intrinsic. Error if ever deallocated.
37CallerLocation,
38/// Additional memory kinds a machine wishes to distinguish from the builtin ones.
39Machine(T),
40}
4142impl<T: MayLeak> MayLeakfor MemoryKind<T> {
43#[inline]
44fn may_leak(self) -> bool {
45match self {
46 MemoryKind::Stack => false,
47 MemoryKind::CallerLocation => true,
48 MemoryKind::Machine(k) => k.may_leak(),
49 }
50 }
51}
5253impl<T: fmt::Display> fmt::Displayfor MemoryKind<T> {
54fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
55match 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}
6263/// 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.
67LiveData,
68/// A function allocation (that fn ptrs point to).
69Function,
70/// A variable argument list allocation (used by c-variadic functions).
71VaList,
72/// A vtable allocation.
73VTable,
74/// A TypeId allocation.
75TypeId,
76/// A dead allocation.
77Dead,
78}
7980/// 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 {
83pub size: Size,
84pub align: Align,
85pub kind: AllocKind,
86pub mutbl: Mutability,
87}
8889impl AllocInfo {
90fn new(size: Size, align: Align, kind: AllocKind, mutbl: Mutability) -> Self {
91Self { size, align, kind, mutbl }
92 }
93}
9495/// 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}
101102impl<'tcx, Other> FnVal<'tcx, Other> {
103pub fn as_instance(self) -> InterpResult<'tcx, Instance<'tcx>> {
104match self {
105 FnVal::Instance(instance) => interp_ok(instance),
106 FnVal::Other(_) => {
107do 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}
112113// `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
126pub(super) alloc_map: M::MemoryMap,
127128/// Map for "extra" function pointers.
129extra_fn_ptr_map: FxIndexMap<AllocId, M::ExtraFnVal>,
130131/// Map storing variable argument lists.
132va_list_map: FxIndexMap<AllocId, VecDeque<MPlaceTy<'tcx, M::Provenance>>>,
133134/// 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
138pub(super) dead_alloc_map: FxIndexMap<AllocId, (Size, Align)>,
139140/// 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!
143validation_in_progress: Cell<bool>,
144}
145146/// 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}
163164impl<'tcx, M: Machine<'tcx>> Memory<'tcx, M> {
165pub fn new() -> Self {
166Memory {
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 }
174175/// This is used by [priroda](https://github.com/oli-obk/priroda)
176pub fn alloc_map(&self) -> &M::MemoryMap {
177&self.alloc_map
178 }
179}
180181impl<'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]
192pub fn global_root_pointer(
193&self,
194 ptr: Pointer<CtfeProvenance>,
195 ) -> InterpResult<'tcx, Pointer<M::Provenance>> {
196let alloc_id = ptr.provenance.alloc_id();
197// We need to handle `extern static`.
198match self.tcx.try_get_global_alloc(alloc_id) {
199Some(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 }
204Some(GlobalAlloc::Static(def_id)) if self.tcx.is_foreign_item(def_id) => {
205return M::extern_static_pointer(self, def_id);
206 }
207None => {
208let is_fn_ptr = self.memory.extra_fn_ptr_map.contains_key(&alloc_id);
209let is_va_list = self.memory.va_list_map.contains_key(&alloc_id);
210if !(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.
218M::adjust_alloc_root_pointer(self, ptr, M::GLOBAL_KIND.map(MemoryKind::Machine))
219 }
220221pub fn fn_ptr(&mut self, fn_val: FnVal<'tcx, M::ExtraFnVal>) -> Pointer<M::Provenance> {
222let id = match fn_val {
223 FnVal::Instance(instance) => {
224let salt = M::get_global_alloc_salt(self, Some(instance));
225self.tcx.reserve_and_set_fn_alloc(instance, salt)
226 }
227 FnVal::Other(extra) => {
228// FIXME(RalfJung): Should we have a cache here?
229let id = self.tcx.reserve_alloc_id();
230let old = self.memory.extra_fn_ptr_map.insert(id, extra);
231if !old.is_none() {
::core::panicking::panic("assertion failed: old.is_none()")
};assert!(old.is_none());
232id233 }
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.
237self.global_root_pointer(Pointer::from(id)).unwrap()
238 }
239240/// Insert a new variable argument list in the global map of variable argument lists.
241pub fn va_list_ptr(
242&mut self,
243 varargs: VecDeque<MPlaceTy<'tcx, M::Provenance>>,
244 ) -> Pointer<M::Provenance> {
245let id = self.tcx.reserve_alloc_id();
246let old = self.memory.va_list_map.insert(id, varargs);
247if !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.
250self.global_root_pointer(Pointer::from(id)).unwrap()
251 }
252253pub 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>> {
260let params = self.machine.get_default_alloc_params();
261let alloc = if M::PANIC_ON_ALLOC_FAIL {
262Allocation::new(size, align, init, params)
263 } else {
264Allocation::try_new(size, align, init, params)?
265};
266self.insert_allocation(alloc, kind)
267 }
268269pub 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>> {
276let params = self.machine.get_default_alloc_params();
277let alloc = Allocation::from_bytes(bytes, align, mutability, params);
278self.insert_allocation(alloc, kind)
279 }
280281pub fn insert_allocation(
282&mut self,
283 alloc: Allocation<M::Provenance, (), M::Bytes>,
284 kind: MemoryKind<M::MemoryKind>,
285 ) -> InterpResult<'tcx, Pointer<M::Provenance>> {
286if !(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());
287let id = self.tcx.reserve_alloc_id();
288if 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!(
289Some(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`.
295let extra = M::init_local_allocation(self, id, kind, alloc.size(), alloc.align)?;
296let alloc = alloc.with_extra(extra);
297self.memory.alloc_map.insert(id, (kind, alloc));
298 M::adjust_alloc_root_pointer(self, Pointer::from(id), Some(kind))
299 }
300301/// If this grows the allocation, `init_growth` determines
302 /// whether the additional space will be initialized.
303pub 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>> {
312let (alloc_id, offset, _prov) = self.ptr_get_alloc_id(ptr, 0)?;
313if offset.bytes() != 0 {
314do 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!(
315msg!(
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 }
326327// 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.
332let new_ptr = self.allocate_ptr(new_size, new_align, kind, init_growth)?;
333let old_size = match old_size_and_align {
334Some((size, _align)) => size,
335None => self.get_alloc_raw(alloc_id)?.size(),
336 };
337// This will also call the access hooks.
338self.mem_copy(ptr, new_ptr.into(), old_size.min(new_size), /*nonoverlapping*/ true)?;
339self.deallocate_ptr(ptr, old_size_and_align, kind)?;
340341interp_ok(new_ptr)
342 }
343344/// Mark the `const_allocate`d allocation `ptr` points to as immutable so we can intern it.
345pub fn make_const_heap_ptr_global(
346&mut self,
347 ptr: Pointer<Option<CtfeProvenance>>,
348 ) -> InterpResult<'tcx>
349where
350M: Machine<'tcx, MemoryKind = crate::const_eval::MemoryKind, Provenance = CtfeProvenance>,
351 {
352let (alloc_id, offset, _) = self.ptr_get_alloc_id(ptr, 0)?;
353if offset.bytes() != 0 {
354return Err(ConstEvalErrKind::ConstMakeGlobalWithOffset(ptr)).into();
355 }
356357if self.tcx.try_get_global_alloc(alloc_id).is_some() {
358// This points to something outside the current interpreter.
359return Err(ConstEvalErrKind::ConstMakeGlobalPtrIsNonHeap(ptr)).into();
360 }
361362// 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).
364let (kind, alloc) = self365 .memory
366 .alloc_map
367 .get_mut_or(alloc_id, || Err(ConstEvalErrKind::ConstMakeGlobalWithDanglingPtr(ptr)))?;
368369// Ensure this is actually a *heap* allocation, and record it as made-global.
370match kind {
371 MemoryKind::Stack | MemoryKind::CallerLocation => {
372return Err(ConstEvalErrKind::ConstMakeGlobalPtrIsNonHeap(ptr)).into();
373 }
374 MemoryKind::Machine(crate::const_eval::MemoryKind::Heap { was_made_global }) => {
375if *was_made_global {
376return Err(ConstEvalErrKind::ConstMakeGlobalPtrAlreadyMadeGlobal(alloc_id))
377 .into();
378 }
379*was_made_global = true;
380 }
381 }
382383// Prevent further mutation, this is now an immutable global.
384alloc.mutability = Mutability::Not;
385386interp_ok(())
387 }
388389#[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")]390pub 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> {
396let (alloc_id, offset, prov) = self.ptr_get_alloc_id(ptr, 0)?;
397trace!("deallocating: {alloc_id:?}");
398399if offset.bytes() != 0 {
400throw_ub_custom!(
401msg!(
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 }
412413let Some((alloc_kind, mut alloc)) = self.memory.alloc_map.remove(&alloc_id) else {
414// Deallocating global memory -- always an error
415return Err(match self.tcx.try_get_global_alloc(alloc_id) {
416Some(GlobalAlloc::Function { .. }) => {
417err_ub_custom!(
418msg!(
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 }
430Some(GlobalAlloc::VTable(..)) => {
431err_ub_custom!(
432msg!(
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 }
444Some(GlobalAlloc::TypeId { .. }) => {
445err_ub_custom!(
446msg!(
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 }
458Some(GlobalAlloc::Static(..) | GlobalAlloc::Memory(..)) => {
459err_ub_custom!(
460msg!(
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 }
472None => err_ub!(PointerUseAfterFree(alloc_id, CheckInAllocMsg::MemoryAccess)),
473 })
474 .into();
475 };
476477if alloc.mutability.is_not() {
478throw_ub_custom!(msg!("deallocating immutable allocation {$alloc}"), alloc = alloc_id,);
479 }
480if alloc_kind != kind {
481throw_ub_custom!(
482msg!(
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 }
490if let Some((size, align)) = old_size_and_align {
491if size != alloc.size() || align != alloc.align {
492throw_ub_custom!(
493msg!(
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 }
504505// Let the machine take some extra action
506let size = alloc.size();
507 M::before_memory_deallocation(
508self.tcx,
509&mut self.machine,
510&mut alloc.extra,
511 ptr,
512 (alloc_id, prov),
513 size,
514 alloc.align,
515 kind,
516 )?;
517518// Don't forget to remember size and align of this now-dead allocation
519let old = self.memory.dead_alloc_map.insert(alloc_id, (size, alloc.align));
520if old.is_some() {
521bug!("Nothing can be deallocated twice");
522 }
523524 interp_ok(())
525 }
526527/// Internal helper function to determine the allocation and offset of a pointer (if any).
528#[inline(always)]
529fn get_ptr_access(
530&self,
531 ptr: Pointer<Option<M::Provenance>>,
532 size: Size,
533 ) -> InterpResult<'tcx, Option<(AllocId, Size, M::ProvenanceExtra)>> {
534let size = i64::try_from(size.bytes()).unwrap(); // it would be an error to even ask for more than isize::MAX bytes
535Self::check_and_deref_ptr(
536self,
537ptr,
538size,
539 CheckInAllocMsg::MemoryAccess,
540 |this, alloc_id, offset, prov| {
541let (size, align) =
542this.get_live_alloc_size_and_align(alloc_id, CheckInAllocMsg::MemoryAccess)?;
543interp_ok((size, align, (alloc_id, offset, prov)))
544 },
545 )
546 }
547548/// 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)]
551pub fn check_ptr_access(
552&self,
553 ptr: Pointer<Option<M::Provenance>>,
554 size: Size,
555 msg: CheckInAllocMsg,
556 ) -> InterpResult<'tcx> {
557let size = i64::try_from(size.bytes()).unwrap(); // it would be an error to even ask for more than isize::MAX bytes
558Self::check_and_deref_ptr(self, ptr, size, msg, |this, alloc_id, _, _| {
559let (size, align) = this.get_live_alloc_size_and_align(alloc_id, msg)?;
560interp_ok((size, align, ()))
561 })?;
562interp_ok(())
563 }
564565/// 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.
568pub fn check_ptr_access_signed(
569&self,
570 ptr: Pointer<Option<M::Provenance>>,
571 size: i64,
572 msg: CheckInAllocMsg,
573 ) -> InterpResult<'tcx> {
574Self::check_and_deref_ptr(self, ptr, size, msg, |this, alloc_id, _, _| {
575let (size, align) = this.get_live_alloc_size_and_align(alloc_id, msg)?;
576interp_ok((size, align, ()))
577 })?;
578interp_ok(())
579 }
580581/// 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.
589fn 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,
596AllocId,
597Size,
598 M::ProvenanceExtra,
599 ) -> InterpResult<'tcx, (Size, Align, T)>,
600 ) -> InterpResult<'tcx, Option<T>> {
601// Everything is okay with size 0.
602if size == 0 {
603return interp_ok(None);
604 }
605606interp_ok(match this.borrow().ptr_try_get_alloc_id(ptr, size) {
607Err(addr) => {
608// We couldn't get a proper allocation.
609do 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 }
611Ok((alloc_id, offset, prov)) => {
612let tcx = this.borrow().tcx;
613let (alloc_size, _alloc_align, ret_val) = alloc_size(this, alloc_id, offset, prov)?;
614let offset = offset.bytes();
615// Compute absolute begin and end of the range.
616let (begin, end) = if size >= 0 {
617 (Some(offset), offset.checked_add(sizeas u64))
618 } else {
619 (offset.checked_sub(size.unsigned_abs()), Some(offset))
620 };
621// Ensure both are within bounds.
622let in_bounds = begin.is_some() && end.is_some_and(|e| e <= alloc_size.bytes());
623if !in_bounds {
624do 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 }
632633Some(ret_val)
634 }
635 })
636 }
637638pub(super) fn check_misalign(
639&self,
640 misaligned: Option<Misalignment>,
641 msg: CheckAlignMsg,
642 ) -> InterpResult<'tcx> {
643if let Some(misaligned) = misaligned {
644do yeet ::rustc_middle::mir::interpret::InterpErrorKind::UndefinedBehavior(::rustc_middle::mir::interpret::UndefinedBehaviorInfo::AlignmentCheckFailed(misaligned,
msg))throw_ub!(AlignmentCheckFailed(misaligned, msg))645 }
646interp_ok(())
647 }
648649pub(super) fn is_ptr_misaligned(
650&self,
651 ptr: Pointer<Option<M::Provenance>>,
652 align: Align,
653 ) -> Option<Misalignment> {
654if !M::enforce_alignment(self) || align.bytes() == 1 {
655return None;
656 }
657658#[inline]
659fn is_offset_misaligned(offset: u64, align: Align) -> Option<Misalignment> {
660if offset.is_multiple_of(align.bytes()) {
661None662 } else {
663// The biggest power of two through which `offset` is divisible.
664let offset_pow2 = 1 << offset.trailing_zeros();
665Some(Misalignment { has: Align::from_bytes(offset_pow2).unwrap(), required: align })
666 }
667 }
668669match self.ptr_try_get_alloc_id(ptr, 0) {
670Err(addr) => is_offset_misaligned(addr, align),
671Ok((alloc_id, offset, _prov)) => {
672let alloc_info = self.get_alloc_info(alloc_id);
673if let Some(misalign) = M::alignment_check(
674self,
675alloc_id,
676alloc_info.align,
677alloc_info.kind,
678offset,
679align,
680 ) {
681Some(misalign)
682 } else if M::Provenance::OFFSET_IS_ADDR {
683is_offset_misaligned(ptr.addr().bytes(), align)
684 } else {
685// Check allocation alignment and offset alignment.
686if alloc_info.align.bytes() < align.bytes() {
687Some(Misalignment { has: alloc_info.align, required: align })
688 } else {
689is_offset_misaligned(offset.bytes(), align)
690 }
691 }
692 }
693 }
694 }
695696/// Checks a pointer for misalignment.
697 ///
698 /// The error assumes this is checking the pointer used directly for an access.
699pub fn check_ptr_align(
700&self,
701 ptr: Pointer<Option<M::Provenance>>,
702 align: Align,
703 ) -> InterpResult<'tcx> {
704self.check_misalign(self.is_ptr_misaligned(ptr, align), CheckAlignMsg::AccessedPtr)
705 }
706}
707708impl<'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.
710pub 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.
715self.memory.dead_alloc_map.retain(|id, _| reachable_allocs.contains(id));
716 }
717}
718719/// 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`.
726fn get_global_alloc(
727&self,
728 id: AllocId,
729 is_write: bool,
730 ) -> InterpResult<'tcx, Cow<'tcx, Allocation<M::Provenance, M::AllocExtra, M::Bytes>>> {
731let (alloc, def_id) = match self.tcx.try_get_global_alloc(id) {
732Some(GlobalAlloc::Memory(mem)) => {
733// Memory of a constant or promoted or anonymous memory referenced by a static.
734(mem, None)
735 }
736Some(GlobalAlloc::Function { .. }) => do yeet ::rustc_middle::mir::interpret::InterpErrorKind::UndefinedBehavior(::rustc_middle::mir::interpret::UndefinedBehaviorInfo::DerefFunctionPointer(id))throw_ub!(DerefFunctionPointer(id)),
737Some(GlobalAlloc::VTable(..)) => do yeet ::rustc_middle::mir::interpret::InterpErrorKind::UndefinedBehavior(::rustc_middle::mir::interpret::UndefinedBehaviorInfo::DerefVTablePointer(id))throw_ub!(DerefVTablePointer(id)),
738Some(GlobalAlloc::TypeId { .. }) => do yeet ::rustc_middle::mir::interpret::InterpErrorKind::UndefinedBehavior(::rustc_middle::mir::interpret::UndefinedBehaviorInfo::DerefTypeIdPointer(id))throw_ub!(DerefTypeIdPointer(id)),
739None => do yeet ::rustc_middle::mir::interpret::InterpErrorKind::UndefinedBehavior(::rustc_middle::mir::interpret::UndefinedBehaviorInfo::PointerUseAfterFree(id,
CheckInAllocMsg::MemoryAccess))throw_ub!(PointerUseAfterFree(id, CheckInAllocMsg::MemoryAccess)),
740Some(GlobalAlloc::Static(def_id)) => {
741if !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.
744if !!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.
755if 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.
758do yeet ::rustc_middle::mir::interpret::InterpErrorKind::Unsupported(::rustc_middle::mir::interpret::UnsupportedOpInfo::ExternStatic(def_id));throw_unsup!(ExternStatic(def_id));
759 }
760761// We don't give a span -- statics don't need that, they cannot be generic or associated.
762let 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.
768M::adjust_global_allocation(
769self,
770id, // always use the ID we got as input, not the "hidden" one.
771alloc.inner(),
772 )
773 }
774775/// 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.
779pub 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>>`.
787let 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.
790let alloc = self.get_global_alloc(id, /*is_write*/ false).report_err().map_err(Err)?;
791match 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.
795Err(Ok(alloc))
796 }
797 Cow::Owned(alloc) => {
798// Need to put it into the map and return a ref to that
799let 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 );
803Ok((MemoryKind::Machine(kind), alloc))
804 }
805 }
806 });
807// Now unpack that funny error type
808match a {
809Ok(a) => interp_ok(&a.1),
810Err(a) => a.into(),
811 }
812 }
813814/// Gives raw, immutable access to the `Allocation` address, without bounds or alignment checks.
815 /// The caller is responsible for calling the access hooks!
816pub fn get_alloc_bytes_unchecked_raw(&self, id: AllocId) -> InterpResult<'tcx, *const u8> {
817let alloc = self.get_alloc_raw(id)?;
818interp_ok(alloc.get_bytes_unchecked_raw())
819 }
820821/// Bounds-checked *but not align-checked* allocation access.
822pub 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 {
828let size_i64 = i64::try_from(size.bytes()).unwrap(); // it would be an error to even ask for more than isize::MAX bytes
829let ptr_and_alloc = Self::check_and_deref_ptr(
830self,
831ptr,
832size_i64,
833 CheckInAllocMsg::MemoryAccess,
834 |this, alloc_id, offset, prov| {
835let alloc = this.get_alloc_raw(alloc_id)?;
836interp_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.
842if !self.memory.validation_in_progress.get() {
843if 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 }
847848if let Some((alloc_id, offset, prov, alloc)) = ptr_and_alloc {
849let range = alloc_range(offset, size);
850if !self.memory.validation_in_progress.get() {
851 M::before_memory_read(
852self.tcx,
853&self.machine,
854&alloc.extra,
855ptr,
856 (alloc_id, prov),
857range,
858 )?;
859 }
860interp_ok(Some(AllocRef { alloc, range, tcx: *self.tcx, alloc_id }))
861 } else {
862interp_ok(None)
863 }
864 }
865866/// Return the `extra` field of the given allocation.
867pub fn get_alloc_extra<'a>(&'a self, id: AllocId) -> InterpResult<'tcx, &'a M::AllocExtra> {
868interp_ok(&self.get_alloc_raw(id)?.extra)
869 }
870871/// Return the `mutability` field of the given allocation.
872pub fn get_alloc_mutability<'a>(&'a self, id: AllocId) -> InterpResult<'tcx, Mutability> {
873interp_ok(self.get_alloc_raw(id)?.mutability)
874 }
875876/// 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.
883pub 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.)
894if self.memory.alloc_map.get_mut(id).is_none() {
895// Slow path.
896 // Allocation not found locally, go look global.
897let alloc = self.get_global_alloc(id, /*is_write*/ true)?;
898let 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 );
902self.memory.alloc_map.insert(id, (MemoryKind::Machine(kind), alloc.into_owned()));
903 }
904905let (_kind, alloc) = self.memory.alloc_map.get_mut(id).unwrap();
906if alloc.mutability.is_not() {
907do yeet ::rustc_middle::mir::interpret::InterpErrorKind::UndefinedBehavior(::rustc_middle::mir::interpret::UndefinedBehaviorInfo::WriteToReadOnly(id))throw_ub!(WriteToReadOnly(id))908 }
909interp_ok((alloc, &mut self.machine))
910 }
911912/// Gives raw, mutable access to the `Allocation` address, without bounds or alignment checks.
913 /// The caller is responsible for calling the access hooks!
914pub fn get_alloc_bytes_unchecked_raw_mut(
915&mut self,
916 id: AllocId,
917 ) -> InterpResult<'tcx, *mut u8> {
918let alloc = self.get_alloc_raw_mut(id)?.0;
919interp_ok(alloc.get_bytes_unchecked_raw_mut())
920 }
921922/// Bounds-checked *but not align-checked* allocation access.
923pub 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 {
929let tcx = self.tcx;
930let validation_in_progress = self.memory.validation_in_progress.get();
931932let size_i64 = i64::try_from(size.bytes()).unwrap(); // it would be an error to even ask for more than isize::MAX bytes
933let ptr_and_alloc = Self::check_and_deref_ptr(
934self,
935ptr,
936size_i64,
937 CheckInAllocMsg::MemoryAccess,
938 |this, alloc_id, offset, prov| {
939let (alloc, machine) = this.get_alloc_raw_mut(alloc_id)?;
940interp_ok((alloc.size(), alloc.align, (alloc_id, offset, prov, alloc, machine)))
941 },
942 )?;
943944if let Some((alloc_id, offset, prov, alloc, machine)) = ptr_and_alloc {
945let range = alloc_range(offset, size);
946if !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.
949M::before_alloc_access(tcx, machine, alloc_id)?;
950 M::before_memory_write(
951tcx,
952machine,
953&mut alloc.extra,
954ptr,
955 (alloc_id, prov),
956range,
957 )?;
958 }
959interp_ok(Some(AllocRefMut { alloc, range, tcx: *tcx, alloc_id }))
960 } else {
961interp_ok(None)
962 }
963 }
964965/// Return the `extra` field of the given allocation.
966pub fn get_alloc_extra_mut<'a>(
967&'a mut self,
968 id: AllocId,
969 ) -> InterpResult<'tcx, (&'a mut M::AllocExtra, &'a mut M)> {
970let (alloc, machine) = self.get_alloc_raw_mut(id)?;
971interp_ok((&mut alloc.extra, machine))
972 }
973974/// 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.
977pub fn is_alloc_live(&self, id: AllocId) -> bool {
978self.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 }
985986/// Obtain the size and alignment of an allocation, even if that allocation has
987 /// been deallocated.
988pub 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
993if let Some((_, alloc)) = self.memory.alloc_map.get(id) {
994return AllocInfo::new(
995alloc.size(),
996alloc.align,
997 AllocKind::LiveData,
998alloc.mutability,
999 );
1000 }
10011002// # Function pointers
1003 // (both global from `alloc_map` and local from `extra_fn_ptr_map`)
1004if let Some(fn_val) = self.get_fn_alloc(id) {
1005let 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.
1011Align::ONE1012 }
1013// Machine-specific extra functions currently do not support alignment restrictions.
1014FnVal::Other(_) => Align::ONE,
1015 };
10161017return AllocInfo::new(Size::ZERO, align, AllocKind::Function, Mutability::Not);
1018 }
10191020// # Variable argument lists
1021if self.memory.va_list_map.contains_key(&id) {
1022return AllocInfo::new(Size::ZERO, Align::ONE, AllocKind::VaList, Mutability::Not);
1023 }
10241025// # Global allocations
1026if let Some(global_alloc) = self.tcx.try_get_global_alloc(id) {
1027// NOTE: `static` alignment from attributes has already been applied to the allocation.
1028let (size, align) = global_alloc.size_and_align(*self.tcx, self.typing_env);
1029let mutbl = global_alloc.mutability(*self.tcx, self.typing_env);
1030let 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 };
1036return AllocInfo::new(size, align, kind, mutbl);
1037 }
10381039// # Dead pointers
1040let (size, align) = *self1041 .memory
1042 .dead_alloc_map
1043 .get(&id)
1044 .expect("deallocated pointers should all be recorded in `dead_alloc_map`");
1045AllocInfo::new(size, align, AllocKind::Dead, Mutability::Not)
1046 }
10471048/// Obtain the size and alignment of a *live* allocation.
1049fn get_live_alloc_size_and_align(
1050&self,
1051 id: AllocId,
1052 msg: CheckInAllocMsg,
1053 ) -> InterpResult<'tcx, (Size, Align)> {
1054let info = self.get_alloc_info(id);
1055if info.kind == AllocKind::Dead {
1056do yeet ::rustc_middle::mir::interpret::InterpErrorKind::UndefinedBehavior(::rustc_middle::mir::interpret::UndefinedBehaviorInfo::PointerUseAfterFree(id,
msg))throw_ub!(PointerUseAfterFree(id, msg))1057 }
1058interp_ok((info.size, info.align))
1059 }
10601061fn get_fn_alloc(&self, id: AllocId) -> Option<FnVal<'tcx, M::ExtraFnVal>> {
1062if let Some(extra) = self.memory.extra_fn_ptr_map.get(&id) {
1063Some(FnVal::Other(*extra))
1064 } else {
1065match self.tcx.try_get_global_alloc(id) {
1066Some(GlobalAlloc::Function { instance, .. }) => Some(FnVal::Instance(instance)),
1067_ => None,
1068 }
1069 }
1070 }
10711072/// 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.
1074pub fn get_ptr_type_id(
1075&self,
1076 ptr: Pointer<Option<M::Provenance>>,
1077 ) -> InterpResult<'tcx, (Ty<'tcx>, u64)> {
1078let (alloc_id, offset, _meta) = self.ptr_get_alloc_id(ptr, 0)?;
1079let Some(GlobalAlloc::TypeId { ty }) = self.tcx.try_get_global_alloc(alloc_id) else {
1080do 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 };
1082interp_ok((ty, offset.bytes()))
1083 }
10841085pub 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);
1090let (alloc_id, offset, _prov) = self.ptr_get_alloc_id(ptr, 0)?;
1091if offset.bytes() != 0 {
1092do 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 }
1094self.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 }
10981099pub 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);
1104let (alloc_id, offset, _prov) = self.ptr_get_alloc_id(ptr, 0)?;
1105if offset.bytes() != 0 {
1106do 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 }
11081109let Some(va_list) = self.memory.va_list_map.get(&alloc_id) else {
1110do 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 };
11121113interp_ok(va_list)
1114 }
11151116/// 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.
1118pub 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);
1123let (alloc_id, offset, _prov) = self.ptr_get_alloc_id(ptr, 0)?;
1124if offset.bytes() != 0 {
1125do 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 }
11271128let Some(va_list) = self.memory.va_list_map.swap_remove(&alloc_id) else {
1129do 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 };
11311132self.memory.dead_alloc_map.insert(alloc_id, (Size::ZERO, Align::ONE));
1133interp_ok(va_list)
1134 }
11351136/// Get the dynamic type of the given vtable pointer.
1137 /// If `expected_trait` is `Some`, it must be a vtable for the given trait.
1138pub 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);
1144let (alloc_id, offset, _tag) = self.ptr_get_alloc_id(ptr, 0)?;
1145if offset.bytes() != 0 {
1146do 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 }
1148let Some(GlobalAlloc::VTable(ty, vtable_dyn_type)) =
1149self.tcx.try_get_global_alloc(alloc_id)
1150else {
1151do 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 };
1153if let Some(expected_dyn_type) = expected_trait {
1154self.check_vtable_for_type(vtable_dyn_type, expected_dyn_type)?;
1155 }
1156interp_ok(ty)
1157 }
11581159pub fn alloc_mark_immutable(&mut self, id: AllocId) -> InterpResult<'tcx> {
1160self.get_alloc_raw_mut(id)?.0.mutability = Mutability::Not;
1161interp_ok(())
1162 }
11631164/// Visit all allocations reachable from the given start set, by recursively traversing the
1165 /// provenance information of those allocations.
1166pub fn visit_reachable_allocs(
1167&mut self,
1168 start: Vec<AllocId>,
1169mut visit: impl FnMut(&mut Self, AllocId, &AllocInfo) -> InterpResult<'tcx>,
1170 ) -> InterpResult<'tcx> {
1171let mut done = FxHashSet::default();
1172let mut todo = start;
1173while let Some(id) = todo.pop() {
1174if !done.insert(id) {
1175// We already saw this allocation before, don't process it again.
1176continue;
1177 }
1178let info = self.get_alloc_info(id);
11791180// 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!
1183if info.kind == AllocKind::LiveData {
1184let alloc = self.get_alloc_raw(id)?;
1185for prov in alloc.provenance().provenances() {
1186if let Some(id) = prov.get_alloc_id() {
1187 todo.push(id);
1188 }
1189 }
1190 }
11911192// Call the callback.
1193visit(self, id, &info)?;
1194 }
1195interp_ok(())
1196 }
11971198/// Create a lazy debug printer that prints the given allocation and all allocations it points
1199 /// to, recursively.
1200#[must_use]
1201pub fn dump_alloc<'a>(&'a self, id: AllocId) -> DumpAllocs<'a, 'tcx, M> {
1202self.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 }
12041205/// Create a lazy debug printer for a list of allocations and all allocations they point to,
1206 /// recursively.
1207#[must_use]
1208pub fn dump_allocs<'a>(&'a self, mut allocs: Vec<AllocId>) -> DumpAllocs<'a, 'tcx, M> {
1209allocs.sort();
1210allocs.dedup();
1211DumpAllocs { ecx: self, allocs }
1212 }
12131214/// Print the allocation's bytes, without any nested allocations.
1215pub 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.
1218let alloc = self.get_alloc_raw(id).unwrap();
1219let mut bytes = String::new();
1220if alloc.size() != Size::ZERO {
1221bytes = "\n".into();
1222// FIXME(translation) there might be pieces that are translatable.
1223rustc_middle::mir::pretty::write_allocation_bytes(*self.tcx, alloc, &mut bytes, " ")
1224 .unwrap();
1225 }
1226bytes1227 }
12281229/// 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!
1234pub 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.
1240let reachable = {
1241let mut reachable = FxHashSet::default();
1242let global_kind = M::GLOBAL_KIND.map(MemoryKind::Machine);
1243let mut todo: Vec<_> =
1244self.memory.alloc_map.filter_map_collect(move |&id, &(kind, _)| {
1245if Some(kind) == global_kind { Some(id) } else { None }
1246 });
1247todo.extend(static_roots(self));
1248while let Some(id) = todo.pop() {
1249if 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.
1253if 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 }
1260reachable1261 };
12621263// All allocations that are *not* `reachable` and *not* `may_leak` are considered leaking.
1264let leaked: Vec<_> = self.memory.alloc_map.filter_map_collect(|&id, &(kind, _)| {
1265if kind.may_leak() || reachable.contains(&id) { None } else { Some(id) }
1266 });
1267let mut result = Vec::new();
1268for &id in leaked.iter() {
1269let (kind, alloc) = self.memory.alloc_map.remove(&id).unwrap();
1270 result.push((id, kind, alloc));
1271 }
1272result1273 }
12741275/// 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.
1280pub 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)`.
1283if !(self.memory.validation_in_progress.replace(true) == false) {
{
::core::panicking::panic_fmt(format_args!("`validation_in_progress` was already set"));
}
};assert!(
1284self.memory.validation_in_progress.replace(true) == false,
1285"`validation_in_progress` was already set"
1286);
1287let res = f(self);
1288if !(self.memory.validation_in_progress.replace(false) == true) {
{
::core::panicking::panic_fmt(format_args!("`validation_in_progress` was unset by someone else"));
}
};assert!(
1289self.memory.validation_in_progress.replace(false) == true,
1290"`validation_in_progress` was unset by someone else"
1291);
1292res1293 }
12941295/// 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.
1300pub 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)`.
1303if !(self.memory.validation_in_progress.replace(true) == false) {
{
::core::panicking::panic_fmt(format_args!("`validation_in_progress` was already set"));
}
};assert!(
1304self.memory.validation_in_progress.replace(true) == false,
1305"`validation_in_progress` was already set"
1306);
1307let res = f(self);
1308if !(self.memory.validation_in_progress.replace(false) == true) {
{
::core::panicking::panic_fmt(format_args!("`validation_in_progress` was unset by someone else"));
}
};assert!(
1309self.memory.validation_in_progress.replace(false) == true,
1310"`validation_in_progress` was unset by someone else"
1311);
1312res1313 }
13141315pub(super) fn validation_in_progress(&self) -> bool {
1316self.memory.validation_in_progress.get()
1317 }
1318}
13191320#[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}
13261327impl<'a, 'tcx, M: Machine<'tcx>> std::fmt::Debugfor DumpAllocs<'a, 'tcx, M> {
1328fn fmt(&self, fmt: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
1329// Cannot be a closure because it is generic in `Prov`, `Extra`.
1330fn 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 {
1336for alloc_id in alloc.provenance().provenances().filter_map(|prov| prov.get_alloc_id())
1337 {
1338 allocs_to_print.push_back(alloc_id);
1339 }
1340fmt.write_fmt(format_args!("{0}", display_allocation(tcx, alloc)))write!(fmt, "{}", display_allocation(tcx, alloc))1341 }
13421343let mut allocs_to_print: VecDeque<_> = self.allocs.iter().copied().collect();
1344// `allocs_printed` contains all allocations that we have already printed.
1345let mut allocs_printed = FxHashSet::default();
13461347while let Some(id) = allocs_to_print.pop_front() {
1348if !allocs_printed.insert(id) {
1349// Already printed, so skip this.
1350continue;
1351 }
13521353fmt.write_fmt(format_args!("{0:?}", id))write!(fmt, "{id:?}")?;
1354match self.ecx.memory.alloc_map.get(id) {
1355Some((kind, alloc)) => {
1356// normal alloc
1357fmt.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 }
1365None => {
1366// global alloc
1367match self.ecx.tcx.try_get_global_alloc(id) {
1368Some(GlobalAlloc::Memory(alloc)) => {
1369fmt.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 }
1377Some(GlobalAlloc::Function { instance, .. }) => {
1378fmt.write_fmt(format_args!(" (fn: {0})", instance))write!(fmt, " (fn: {instance})")?;
1379 }
1380Some(GlobalAlloc::VTable(ty, dyn_ty)) => {
1381fmt.write_fmt(format_args!(" (vtable: impl {0} for {1})", dyn_ty, ty))write!(fmt, " (vtable: impl {dyn_ty} for {ty})")?;
1382 }
1383Some(GlobalAlloc::TypeId { ty }) => {
1384fmt.write_fmt(format_args!(" (typeid for {0})", ty))write!(fmt, " (typeid for {ty})")?;
1385 }
1386Some(GlobalAlloc::Static(did)) => {
1387fmt.write_fmt(format_args!(" (static: {0})", self.ecx.tcx.def_path_str(did)))write!(fmt, " (static: {})", self.ecx.tcx.def_path_str(did))?;
1388 }
1389None => {
1390fmt.write_fmt(format_args!(" (deallocated)"))write!(fmt, " (deallocated)")?;
1391 }
1392 }
1393 }
1394 }
1395fmt.write_fmt(format_args!("\n"))writeln!(fmt)?;
1396 }
1397Ok(())
1398 }
1399}
14001401/// Reading and writing.
1402impl<'a, 'tcx, Prov: Provenance, Extra, Bytes: AllocBytes>
1403AllocRefMut<'a, 'tcx, Prov, Extra, Bytes>
1404{
1405pub fn as_ref<'b>(&'b self) -> AllocRef<'b, 'tcx, Prov, Extra, Bytes> {
1406AllocRef { alloc: self.alloc, range: self.range, tcx: self.tcx, alloc_id: self.alloc_id }
1407 }
14081409/// `range` is relative to this allocation reference, not the base of the allocation.
1410pub fn write_scalar(&mut self, range: AllocRange, val: Scalar<Prov>) -> InterpResult<'tcx> {
1411let 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);
14131414self.alloc
1415 .write_scalar(&self.tcx, range, val)
1416 .map_err(|e| e.to_interp_error(self.alloc_id))
1417 .into()
1418 }
14191420/// `offset` is relative to this allocation reference, not the base of the allocation.
1421pub fn write_ptr_sized(&mut self, offset: Size, val: Scalar<Prov>) -> InterpResult<'tcx> {
1422self.write_scalar(alloc_range(offset, self.tcx.data_layout().pointer_size()), val)
1423 }
14241425/// Mark the given sub-range (relative to this allocation reference) as uninitialized.
1426pub fn write_uninit(&mut self, range: AllocRange) {
1427let range = self.range.subrange(range);
14281429self.alloc.write_uninit(&self.tcx, range);
1430 }
14311432/// Mark the entire referenced range as uninitialized
1433pub fn write_uninit_full(&mut self) {
1434self.alloc.write_uninit(&self.tcx, self.range);
1435 }
14361437/// Remove all provenance in the reference range.
1438pub fn clear_provenance(&mut self) {
1439self.alloc.clear_provenance(&self.tcx, self.range);
1440 }
1441}
14421443impl<'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.
1445pub fn read_scalar(
1446&self,
1447 range: AllocRange,
1448 read_provenance: bool,
1449 ) -> InterpResult<'tcx, Scalar<Prov>> {
1450let range = self.range.subrange(range);
1451self.alloc
1452 .read_scalar(&self.tcx, range, read_provenance)
1453 .map_err(|e| e.to_interp_error(self.alloc_id))
1454 .into()
1455 }
14561457/// `range` is relative to this allocation reference, not the base of the allocation.
1458pub fn read_integer(&self, range: AllocRange) -> InterpResult<'tcx, Scalar<Prov>> {
1459self.read_scalar(range, /*read_provenance*/ false)
1460 }
14611462/// `offset` is relative to this allocation reference, not the base of the allocation.
1463pub fn read_pointer(&self, offset: Size) -> InterpResult<'tcx, Scalar<Prov>> {
1464self.read_scalar(
1465alloc_range(offset, self.tcx.data_layout().pointer_size()),
1466/*read_provenance*/ true,
1467 )
1468 }
14691470/// `range` is relative to this allocation reference, not the base of the allocation.
1471pub fn get_bytes_strip_provenance<'b>(&'b self) -> InterpResult<'tcx, &'a [u8]> {
1472self.alloc
1473 .get_bytes_strip_provenance(&self.tcx, self.range)
1474 .map_err(|e| e.to_interp_error(self.alloc_id))
1475 .into()
1476 }
14771478/// Returns whether the allocation has provenance anywhere in the range of the `AllocRef`.
1479pub fn has_provenance(&self) -> bool {
1480 !self.alloc.provenance().range_empty(self.range, &self.tcx)
1481 }
1482}
14831484impl<'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.
1489pub fn read_bytes_ptr_strip_provenance(
1490&self,
1491 ptr: Pointer<Option<M::Provenance>>,
1492 size: Size,
1493 ) -> InterpResult<'tcx, &[u8]> {
1494let Some(alloc_ref) = self.get_ptr_alloc(ptr, size)? else {
1495// zero-sized access
1496return 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.)
1500interp_ok(
1501alloc_ref1502 .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 }
15071508/// Writes the given stream of bytes into memory.
1509 ///
1510 /// Performs appropriate bounds checks.
1511pub fn write_bytes_ptr(
1512&mut self,
1513 ptr: Pointer<Option<M::Provenance>>,
1514 src: impl IntoIterator<Item = u8>,
1515 ) -> InterpResult<'tcx> {
1516let mut src = src.into_iter();
1517let (lower, upper) = src.size_hint();
1518let len = upper.expect("can only write bounded iterators");
1519match (&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");
15201521let size = Size::from_bytes(len);
1522let Some(alloc_ref) = self.get_ptr_alloc_mut(ptr, size)? else {
1523// zero-sized access
1524match 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");
1525return interp_ok(());
1526 };
15271528// 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.)
1530let bytes =
1531alloc_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`.
1534for dest in bytes {
1535*dest = src.next().expect("iterator was shorter than it said it would be");
1536 }
1537match 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");
1538interp_ok(())
1539 }
15401541pub 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> {
1548self.mem_copy_repeatedly(src, dest, size, 1, nonoverlapping)
1549 }
15501551/// 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.
1556pub 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> {
1564let tcx = self.tcx;
1565// We need to do our own bounds-checks.
1566let src_parts = self.get_ptr_access(src, size)?;
1567let dest_parts = self.get_ptr_access(dest, size * num_copies)?; // `Size` multiplication
15681569 // 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.
1572if 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 }
15761577// 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`.
15791580 // Source alloc preparations and access hooks.
1581let Some((src_alloc_id, src_offset, src_prov)) = src_partselse {
1582// Zero-sized *source*, that means dest is also zero-sized and we have nothing to do.
1583return interp_ok(());
1584 };
1585let src_alloc = self.get_alloc_raw(src_alloc_id)?;
1586let src_range = alloc_range(src_offset, size);
1587if !!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");
15881589// 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.
1592M::before_memory_read(
1593tcx,
1594&self.machine,
1595&src_alloc.extra,
1596src,
1597 (src_alloc_id, src_prov),
1598src_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.
1602let Some((dest_alloc_id, dest_offset, dest_prov)) = dest_partselse {
1603// Zero-sized *destination*.
1604return interp_ok(());
1605 };
16061607// Prepare getting source provenance.
1608let 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.
1612let provenance = src_alloc.provenance_prepare_copy(src_range, self);
1613// Prepare a copy of the initialization mask.
1614let init = src_alloc.init_mask().prepare_copy(src_range);
16151616// Destination alloc preparations...
1617let (dest_alloc, machine) = self.get_alloc_raw_mut(dest_alloc_id)?;
1618let dest_range = alloc_range(dest_offset, size * num_copies);
1619// ...and access hooks.
1620M::before_alloc_access(tcx, machine, dest_alloc_id)?;
1621 M::before_memory_write(
1622tcx,
1623machine,
1624&mut dest_alloc.extra,
1625dest,
1626 (dest_alloc_id, dest_prov),
1627dest_range,
1628 )?;
1629// Yes we do overwrite all bytes in `dest_bytes`.
1630let dest_bytes =
1631dest_alloc.get_bytes_unchecked_for_overwrite_ptr(&tcx, dest_range).as_mut_ptr();
16321633if 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.
1640dest_alloc.write_uninit(&tcx, dest_range);
1641// `write_uninit` also resets the provenance, so we are done.
1642return interp_ok(());
1643 }
16441645// 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.
1650unsafe {
1651if src_alloc_id == dest_alloc_id {
1652if nonoverlapping {
1653// `Size` additions
1654if (src_offset <= dest_offset && src_offset + size > dest_offset)
1655 || (dest_offset <= src_offset && dest_offset + size > src_offset)
1656 {
1657do 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 }
1663if num_copies > 1 {
1664if !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 }
16661667let 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.
1670if size_in_bytes == 1 {
1671if 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).
1673let value = *src_bytes;
1674dest_bytes.write_bytes(value, (size * num_copies).bytes_usize());
1675 } else if src_alloc_id == dest_alloc_id {
1676let mut dest_ptr = dest_bytes;
1677for _ in 0..num_copies {
1678// Here we rely on `src` and `dest` being non-overlapping if there is more than
1679 // one copy.
1680ptr::copy(src_bytes, dest_ptr, size_in_bytes);
1681 dest_ptr = dest_ptr.add(size_in_bytes);
1682 }
1683 } else {
1684let mut dest_ptr = dest_bytes;
1685for _ 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 }
16911692// now fill in all the "init" data
1693dest_alloc.init_mask_apply_copy(
1694init,
1695alloc_range(dest_offset, size), // just a single copy (i.e., not full `dest_range`)
1696num_copies,
1697 );
1698// copy the provenance to the destination
1699dest_alloc.provenance_apply_copy(provenance, alloc_range(dest_offset, size), num_copies);
17001701interp_ok(())
1702 }
1703}
17041705/// 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.
1709pub fn scalar_may_be_null(&self, scalar: Scalar<M::Provenance>) -> InterpResult<'tcx, bool> {
1710match scalar.try_to_scalar_int() {
1711Ok(int) => interp_ok(int.is_null()),
1712Err(_) => {
1713// We can't cast this pointer to an integer. Can only happen during CTFE.
1714let ptr = scalar.to_pointer(self)?;
1715match self.ptr_try_get_alloc_id(ptr, 0) {
1716Ok((alloc_id, offset, _)) => {
1717let info = self.get_alloc_info(alloc_id);
1718if 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.
1723return interp_ok(true);
1724 }
1725// If the pointer is in-bounds (including "at the end"), it is definitely not null.
1726if offset <= info.size {
1727return 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.
1732if !offset.bytes().is_multiple_of(info.align.bytes()) {
1733return interp_ok(false);
1734 }
1735// We don't know enough, this might be null.
1736interp_ok(true)
1737 }
1738Err(_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 }
17431744/// 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`.)
1757pub fn ptr_try_get_alloc_id(
1758&self,
1759 ptr: Pointer<Option<M::Provenance>>,
1760 size: i64,
1761 ) -> Result<(AllocId, Size, M::ProvenanceExtra), u64> {
1762match ptr.into_pointer_or_addr() {
1763Ok(ptr) => match M::ptr_get_alloc(self, ptr, size) {
1764Some((alloc_id, offset, extra)) => Ok((alloc_id, offset, extra)),
1765None => {
1766if !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.
1768let (_, addr) = ptr.into_raw_parts();
1769Err(addr.bytes())
1770 }
1771 },
1772Err(addr) => Err(addr.bytes()),
1773 }
1774 }
17751776/// 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)]
1789pub fn ptr_get_alloc_id(
1790&self,
1791 ptr: Pointer<Option<M::Provenance>>,
1792 size: i64,
1793 ) -> InterpResult<'tcx, (AllocId, Size, M::ProvenanceExtra)> {
1794self.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}