]> git.lizzy.rs Git - rust.git/blob - src/machine.rs
rustup
[rust.git] / src / machine.rs
1 //! Global machine state as well as implementation of the interpreter engine
2 //! `Machine` trait.
3
4 use std::borrow::Cow;
5 use std::cell::RefCell;
6 use std::num::NonZeroU64;
7 use std::rc::Rc;
8 use std::time::Instant;
9 use std::fmt;
10
11 use log::trace;
12 use rand::rngs::StdRng;
13 use rand::SeedableRng;
14
15 use rustc_data_structures::fx::FxHashMap;
16 use rustc_middle::{
17     mir,
18     ty::{
19         self,
20         layout::{LayoutCx, LayoutError, TyAndLayout},
21         TyCtxt,
22     },
23 };
24 use rustc_span::symbol::{sym, Symbol};
25 use rustc_span::def_id::DefId;
26 use rustc_target::abi::{LayoutOf, Size};
27
28 use crate::*;
29
30 // Some global facts about the emulated machine.
31 pub const PAGE_SIZE: u64 = 4 * 1024; // FIXME: adjust to target architecture
32 pub const STACK_ADDR: u64 = 32 * PAGE_SIZE; // not really about the "stack", but where we start assigning integer addresses to allocations
33 pub const STACK_SIZE: u64 = 16 * PAGE_SIZE; // whatever
34 pub const NUM_CPUS: u64 = 1;
35
36 /// Extra data stored with each stack frame
37 #[derive(Debug)]
38 pub struct FrameData<'tcx> {
39     /// Extra data for Stacked Borrows.
40     pub call_id: stacked_borrows::CallId,
41
42     /// If this is Some(), then this is a special "catch unwind" frame (the frame of `try_fn`
43     /// called by `try`). When this frame is popped during unwinding a panic,
44     /// we stop unwinding, use the `CatchUnwindData` to handle catching.
45     pub catch_unwind: Option<CatchUnwindData<'tcx>>,
46 }
47
48 /// Extra memory kinds
49 #[derive(Debug, Copy, Clone, PartialEq, Eq)]
50 pub enum MiriMemoryKind {
51     /// `__rust_alloc` memory.
52     Rust,
53     /// `malloc` memory.
54     C,
55     /// Windows `HeapAlloc` memory.
56     WinHeap,
57     /// Memory for args, errno, and other parts of the machine-managed environment.
58     /// This memory may leak.
59     Machine,
60     /// Memory for env vars. Separate from `Machine` because we clean it up and leak-check it.
61     Env,
62     /// Globals copied from `tcx`.
63     /// This memory may leak.
64     Global,
65     /// Memory for extern statics.
66     /// This memory may leak.
67     ExternStatic,
68     /// Memory for thread-local statics.
69     /// This memory may leak.
70     Tls,
71 }
72
73 impl Into<MemoryKind<MiriMemoryKind>> for MiriMemoryKind {
74     #[inline(always)]
75     fn into(self) -> MemoryKind<MiriMemoryKind> {
76         MemoryKind::Machine(self)
77     }
78 }
79
80 impl MayLeak for MiriMemoryKind {
81     #[inline(always)]
82     fn may_leak(self) -> bool {
83         use self::MiriMemoryKind::*;
84         match self {
85             Rust | C | WinHeap | Env => false,
86             Machine | Global | ExternStatic | Tls => true,
87         }
88     }
89 }
90
91 impl fmt::Display for MiriMemoryKind {
92     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
93         use self::MiriMemoryKind::*;
94         match self {
95             Rust => write!(f, "Rust heap"),
96             C => write!(f, "C heap"),
97             WinHeap => write!(f, "Windows heap"),
98             Machine => write!(f, "machine-managed memory"),
99             Env => write!(f, "environment variable"),
100             Global => write!(f, "global (static or const)"),
101             ExternStatic => write!(f, "extern static"),
102             Tls =>  write!(f, "thread-local static"),
103         }
104     }
105 }
106
107 /// Extra per-allocation data
108 #[derive(Debug, Clone)]
109 pub struct AllocExtra {
110     /// Stacked Borrows state is only added if it is enabled.
111     pub stacked_borrows: Option<stacked_borrows::AllocExtra>,
112 }
113
114 /// Extra global memory data
115 #[derive(Clone, Debug)]
116 pub struct MemoryExtra {
117     pub stacked_borrows: Option<stacked_borrows::MemoryExtra>,
118     pub intptrcast: intptrcast::MemoryExtra,
119
120     /// Mapping extern static names to their canonical allocation.
121     extern_statics: FxHashMap<Symbol, AllocId>,
122
123     /// The random number generator used for resolving non-determinism.
124     /// Needs to be queried by ptr_to_int, hence needs interior mutability.
125     pub(crate) rng: RefCell<StdRng>,
126
127     /// An allocation ID to report when it is being allocated
128     /// (helps for debugging memory leaks and use after free bugs).
129     tracked_alloc_id: Option<AllocId>,
130
131     /// Controls whether alignment of memory accesses is being checked.
132     pub(crate) check_alignment: AlignmentCheck,
133 }
134
135 impl MemoryExtra {
136     pub fn new(config: &MiriConfig) -> Self {
137         let rng = StdRng::seed_from_u64(config.seed.unwrap_or(0));
138         let stacked_borrows = if config.stacked_borrows {
139             Some(Rc::new(RefCell::new(stacked_borrows::GlobalState::new(
140                 config.tracked_pointer_tag,
141                 config.tracked_call_id,
142                 config.track_raw,
143             ))))
144         } else {
145             None
146         };
147         MemoryExtra {
148             stacked_borrows,
149             intptrcast: Default::default(),
150             extern_statics: FxHashMap::default(),
151             rng: RefCell::new(rng),
152             tracked_alloc_id: config.tracked_alloc_id,
153             check_alignment: config.check_alignment,
154         }
155     }
156
157     fn add_extern_static<'tcx, 'mir>(
158         this: &mut MiriEvalContext<'mir, 'tcx>,
159         name: &str,
160         ptr: Scalar<Tag>,
161     ) {
162         let ptr = ptr.assert_ptr();
163         assert_eq!(ptr.offset, Size::ZERO);
164         this.memory
165             .extra
166             .extern_statics
167             .insert(Symbol::intern(name), ptr.alloc_id)
168             .unwrap_none();
169     }
170
171     /// Sets up the "extern statics" for this machine.
172     pub fn init_extern_statics<'tcx, 'mir>(
173         this: &mut MiriEvalContext<'mir, 'tcx>,
174     ) -> InterpResult<'tcx> {
175         match this.tcx.sess.target.os.as_str() {
176             "linux" => {
177                 // "__cxa_thread_atexit_impl"
178                 // This should be all-zero, pointer-sized.
179                 let layout = this.machine.layouts.usize;
180                 let place = this.allocate(layout, MiriMemoryKind::ExternStatic.into());
181                 this.write_scalar(Scalar::from_machine_usize(0, this), place.into())?;
182                 Self::add_extern_static(this, "__cxa_thread_atexit_impl", place.ptr);
183                 // "environ"
184                 Self::add_extern_static(this, "environ", this.machine.env_vars.environ.unwrap().ptr);
185             }
186             "windows" => {
187                 // "_tls_used"
188                 // This is some obscure hack that is part of the Windows TLS story. It's a `u8`.
189                 let layout = this.machine.layouts.u8;
190                 let place = this.allocate(layout, MiriMemoryKind::ExternStatic.into());
191                 this.write_scalar(Scalar::from_u8(0), place.into())?;
192                 Self::add_extern_static(this, "_tls_used", place.ptr);
193             }
194             _ => {} // No "extern statics" supported on this target
195         }
196         Ok(())
197     }
198 }
199
200 /// Precomputed layouts of primitive types
201 pub struct PrimitiveLayouts<'tcx> {
202     pub unit: TyAndLayout<'tcx>,
203     pub i8: TyAndLayout<'tcx>,
204     pub i32: TyAndLayout<'tcx>,
205     pub isize: TyAndLayout<'tcx>,
206     pub u8: TyAndLayout<'tcx>,
207     pub u32: TyAndLayout<'tcx>,
208     pub usize: TyAndLayout<'tcx>,
209 }
210
211 impl<'mir, 'tcx: 'mir> PrimitiveLayouts<'tcx> {
212     fn new(layout_cx: LayoutCx<'tcx, TyCtxt<'tcx>>) -> Result<Self, LayoutError<'tcx>> {
213         Ok(Self {
214             unit: layout_cx.layout_of(layout_cx.tcx.mk_unit())?,
215             i8: layout_cx.layout_of(layout_cx.tcx.types.i8)?,
216             i32: layout_cx.layout_of(layout_cx.tcx.types.i32)?,
217             isize: layout_cx.layout_of(layout_cx.tcx.types.isize)?,
218             u8: layout_cx.layout_of(layout_cx.tcx.types.u8)?,
219             u32: layout_cx.layout_of(layout_cx.tcx.types.u32)?,
220             usize: layout_cx.layout_of(layout_cx.tcx.types.usize)?,
221         })
222     }
223 }
224
225 /// The machine itself.
226 pub struct Evaluator<'mir, 'tcx> {
227     /// Environment variables set by `setenv`.
228     /// Miri does not expose env vars from the host to the emulated program.
229     pub(crate) env_vars: EnvVars<'tcx>,
230
231     /// Program arguments (`Option` because we can only initialize them after creating the ecx).
232     /// These are *pointers* to argc/argv because macOS.
233     /// We also need the full command line as one string because of Windows.
234     pub(crate) argc: Option<Scalar<Tag>>,
235     pub(crate) argv: Option<Scalar<Tag>>,
236     pub(crate) cmd_line: Option<Scalar<Tag>>,
237
238     /// TLS state.
239     pub(crate) tls: TlsData<'tcx>,
240
241     /// If enabled, the `env_vars` field is populated with the host env vars during initialization
242     /// and random number generation is delegated to the host.
243     pub(crate) communicate: bool,
244
245     /// Whether to enforce the validity invariant.
246     pub(crate) validate: bool,
247
248     pub(crate) file_handler: shims::posix::FileHandler,
249     pub(crate) dir_handler: shims::posix::DirHandler,
250
251     /// The "time anchor" for this machine's monotone clock (for `Instant` simulation).
252     pub(crate) time_anchor: Instant,
253
254     /// The set of threads.
255     pub(crate) threads: ThreadManager<'mir, 'tcx>,
256
257     /// Precomputed `TyLayout`s for primitive data types that are commonly used inside Miri.
258     pub(crate) layouts: PrimitiveLayouts<'tcx>,
259
260     /// Allocations that are considered roots of static memory (that may leak).
261     pub(crate) static_roots: Vec<AllocId>,
262 }
263
264 impl<'mir, 'tcx> Evaluator<'mir, 'tcx> {
265     pub(crate) fn new(
266         communicate: bool,
267         validate: bool,
268         layout_cx: LayoutCx<'tcx, TyCtxt<'tcx>>,
269     ) -> Self {
270         let layouts = PrimitiveLayouts::new(layout_cx)
271             .expect("Couldn't get layouts of primitive types");
272         Evaluator {
273             // `env_vars` could be initialized properly here if `Memory` were available before
274             // calling this method.
275             env_vars: EnvVars::default(),
276             argc: None,
277             argv: None,
278             cmd_line: None,
279             tls: TlsData::default(),
280             communicate,
281             validate,
282             file_handler: Default::default(),
283             dir_handler: Default::default(),
284             time_anchor: Instant::now(),
285             layouts,
286             threads: ThreadManager::default(),
287             static_roots: Vec::new(),
288         }
289     }
290 }
291
292 /// A rustc InterpCx for Miri.
293 pub type MiriEvalContext<'mir, 'tcx> = InterpCx<'mir, 'tcx, Evaluator<'mir, 'tcx>>;
294
295 /// A little trait that's useful to be inherited by extension traits.
296 pub trait MiriEvalContextExt<'mir, 'tcx> {
297     fn eval_context_ref<'a>(&'a self) -> &'a MiriEvalContext<'mir, 'tcx>;
298     fn eval_context_mut<'a>(&'a mut self) -> &'a mut MiriEvalContext<'mir, 'tcx>;
299 }
300 impl<'mir, 'tcx> MiriEvalContextExt<'mir, 'tcx> for MiriEvalContext<'mir, 'tcx> {
301     #[inline(always)]
302     fn eval_context_ref(&self) -> &MiriEvalContext<'mir, 'tcx> {
303         self
304     }
305     #[inline(always)]
306     fn eval_context_mut(&mut self) -> &mut MiriEvalContext<'mir, 'tcx> {
307         self
308     }
309 }
310
311 /// Machine hook implementations.
312 impl<'mir, 'tcx> Machine<'mir, 'tcx> for Evaluator<'mir, 'tcx> {
313     type MemoryKind = MiriMemoryKind;
314
315     type FrameExtra = FrameData<'tcx>;
316     type MemoryExtra = MemoryExtra;
317     type AllocExtra = AllocExtra;
318     type PointerTag = Tag;
319     type ExtraFnVal = Dlsym;
320
321     type MemoryMap =
322         MonoHashMap<AllocId, (MemoryKind<MiriMemoryKind>, Allocation<Tag, Self::AllocExtra>)>;
323
324     const GLOBAL_KIND: Option<MiriMemoryKind> = Some(MiriMemoryKind::Global);
325
326     #[inline(always)]
327     fn enforce_alignment(memory_extra: &MemoryExtra) -> bool {
328         memory_extra.check_alignment != AlignmentCheck::None
329     }
330
331     #[inline(always)]
332     fn force_int_for_alignment_check(memory_extra: &Self::MemoryExtra) -> bool {
333         memory_extra.check_alignment == AlignmentCheck::Int
334     }
335
336     #[inline(always)]
337     fn enforce_validity(ecx: &InterpCx<'mir, 'tcx, Self>) -> bool {
338         ecx.machine.validate
339     }
340
341     #[inline(always)]
342     fn find_mir_or_eval_fn(
343         ecx: &mut InterpCx<'mir, 'tcx, Self>,
344         instance: ty::Instance<'tcx>,
345         args: &[OpTy<'tcx, Tag>],
346         ret: Option<(PlaceTy<'tcx, Tag>, mir::BasicBlock)>,
347         unwind: Option<mir::BasicBlock>,
348     ) -> InterpResult<'tcx, Option<&'mir mir::Body<'tcx>>> {
349         ecx.find_mir_or_eval_fn(instance, args, ret, unwind)
350     }
351
352     #[inline(always)]
353     fn call_extra_fn(
354         ecx: &mut InterpCx<'mir, 'tcx, Self>,
355         fn_val: Dlsym,
356         args: &[OpTy<'tcx, Tag>],
357         ret: Option<(PlaceTy<'tcx, Tag>, mir::BasicBlock)>,
358         _unwind: Option<mir::BasicBlock>,
359     ) -> InterpResult<'tcx> {
360         ecx.call_dlsym(fn_val, args, ret)
361     }
362
363     #[inline(always)]
364     fn call_intrinsic(
365         ecx: &mut rustc_mir::interpret::InterpCx<'mir, 'tcx, Self>,
366         instance: ty::Instance<'tcx>,
367         args: &[OpTy<'tcx, Tag>],
368         ret: Option<(PlaceTy<'tcx, Tag>, mir::BasicBlock)>,
369         unwind: Option<mir::BasicBlock>,
370     ) -> InterpResult<'tcx> {
371         ecx.call_intrinsic(instance, args, ret, unwind)
372     }
373
374     #[inline(always)]
375     fn assert_panic(
376         ecx: &mut InterpCx<'mir, 'tcx, Self>,
377         msg: &mir::AssertMessage<'tcx>,
378         unwind: Option<mir::BasicBlock>,
379     ) -> InterpResult<'tcx> {
380         ecx.assert_panic(msg, unwind)
381     }
382
383     #[inline(always)]
384     fn abort(_ecx: &mut InterpCx<'mir, 'tcx, Self>) -> InterpResult<'tcx, !> {
385         throw_machine_stop!(TerminationInfo::Abort(None))
386     }
387
388     #[inline(always)]
389     fn binary_ptr_op(
390         ecx: &rustc_mir::interpret::InterpCx<'mir, 'tcx, Self>,
391         bin_op: mir::BinOp,
392         left: ImmTy<'tcx, Tag>,
393         right: ImmTy<'tcx, Tag>,
394     ) -> InterpResult<'tcx, (Scalar<Tag>, bool, ty::Ty<'tcx>)> {
395         ecx.binary_ptr_op(bin_op, left, right)
396     }
397
398     fn box_alloc(
399         ecx: &mut InterpCx<'mir, 'tcx, Self>,
400         dest: PlaceTy<'tcx, Tag>,
401     ) -> InterpResult<'tcx> {
402         trace!("box_alloc for {:?}", dest.layout.ty);
403         let layout = ecx.layout_of(dest.layout.ty.builtin_deref(false).unwrap().ty)?;
404         // First argument: `size`.
405         // (`0` is allowed here -- this is expected to be handled by the lang item).
406         let size = Scalar::from_machine_usize(layout.size.bytes(), ecx);
407
408         // Second argument: `align`.
409         let align = Scalar::from_machine_usize(layout.align.abi.bytes(), ecx);
410
411         // Call the `exchange_malloc` lang item.
412         let malloc = ecx.tcx.lang_items().exchange_malloc_fn().unwrap();
413         let malloc = ty::Instance::mono(ecx.tcx.tcx, malloc);
414         ecx.call_function(
415             malloc,
416             &[size.into(), align.into()],
417             Some(dest),
418             // Don't do anything when we are done. The `statement()` function will increment
419             // the old stack frame's stmt counter to the next statement, which means that when
420             // `exchange_malloc` returns, we go on evaluating exactly where we want to be.
421             StackPopCleanup::None { cleanup: true },
422         )?;
423         Ok(())
424     }
425
426     fn thread_local_static_alloc_id(
427         ecx: &mut InterpCx<'mir, 'tcx, Self>,
428         def_id: DefId,
429     ) -> InterpResult<'tcx, AllocId> {
430         ecx.get_or_create_thread_local_alloc_id(def_id)
431     }
432
433     fn extern_static_alloc_id(
434         memory: &Memory<'mir, 'tcx, Self>,
435         def_id: DefId,
436     ) -> InterpResult<'tcx, AllocId> {
437         let attrs = memory.tcx.get_attrs(def_id);
438         let link_name = match memory.tcx.sess.first_attr_value_str_by_name(&attrs, sym::link_name) {
439             Some(name) => name,
440             None => memory.tcx.item_name(def_id),
441         };
442         if let Some(&id) = memory.extra.extern_statics.get(&link_name) {
443             Ok(id)
444         } else {
445             throw_unsup_format!("`extern` static {:?} is not supported by Miri", def_id)
446         }
447     }
448
449     fn init_allocation_extra<'b>(
450         memory_extra: &MemoryExtra,
451         id: AllocId,
452         alloc: Cow<'b, Allocation>,
453         kind: Option<MemoryKind<Self::MemoryKind>>,
454     ) -> (Cow<'b, Allocation<Self::PointerTag, Self::AllocExtra>>, Self::PointerTag) {
455         if Some(id) == memory_extra.tracked_alloc_id {
456             register_diagnostic(NonHaltingDiagnostic::CreatedAlloc(id));
457         }
458
459         let kind = kind.expect("we set our STATIC_KIND so this cannot be None");
460         let alloc = alloc.into_owned();
461         let (stacks, base_tag) =
462             if let Some(stacked_borrows) = &memory_extra.stacked_borrows {
463                 let (stacks, base_tag) =
464                     Stacks::new_allocation(id, alloc.size, Rc::clone(stacked_borrows), kind);
465                 (Some(stacks), base_tag)
466             } else {
467                 // No stacks, no tag.
468                 (None, Tag::Untagged)
469             };
470         let mut stacked_borrows = memory_extra.stacked_borrows.as_ref().map(|sb| sb.borrow_mut());
471         let alloc: Allocation<Tag, Self::AllocExtra> = alloc.with_tags_and_extra(
472             |alloc| {
473                 if let Some(stacked_borrows) = &mut stacked_borrows {
474                     // Only globals may already contain pointers at this point
475                     assert_eq!(kind, MiriMemoryKind::Global.into());
476                     stacked_borrows.global_base_ptr(alloc)
477                 } else {
478                     Tag::Untagged
479                 }
480             },
481             AllocExtra { stacked_borrows: stacks },
482         );
483         (Cow::Owned(alloc), base_tag)
484     }
485
486     #[inline(always)]
487     fn before_deallocation(
488         memory_extra: &mut Self::MemoryExtra,
489         id: AllocId,
490     ) -> InterpResult<'tcx> {
491         if Some(id) == memory_extra.tracked_alloc_id {
492             register_diagnostic(NonHaltingDiagnostic::FreedAlloc(id));
493         }
494
495         Ok(())
496     }
497
498     #[inline(always)]
499     fn tag_global_base_pointer(memory_extra: &MemoryExtra, id: AllocId) -> Self::PointerTag {
500         if let Some(stacked_borrows) = &memory_extra.stacked_borrows {
501             stacked_borrows.borrow_mut().global_base_ptr(id)
502         } else {
503             Tag::Untagged
504         }
505     }
506
507     #[inline(always)]
508     fn retag(
509         ecx: &mut InterpCx<'mir, 'tcx, Self>,
510         kind: mir::RetagKind,
511         place: PlaceTy<'tcx, Tag>,
512     ) -> InterpResult<'tcx> {
513         if ecx.memory.extra.stacked_borrows.is_some() {
514             ecx.retag(kind, place)
515         } else {
516             Ok(())
517         }
518     }
519
520     #[inline(always)]
521     fn init_frame_extra(
522         ecx: &mut InterpCx<'mir, 'tcx, Self>,
523         frame: Frame<'mir, 'tcx, Tag>,
524     ) -> InterpResult<'tcx, Frame<'mir, 'tcx, Tag, FrameData<'tcx>>> {
525         let stacked_borrows = ecx.memory.extra.stacked_borrows.as_ref();
526         let call_id = stacked_borrows.map_or(NonZeroU64::new(1).unwrap(), |stacked_borrows| {
527             stacked_borrows.borrow_mut().new_call()
528         });
529         let extra = FrameData { call_id, catch_unwind: None };
530         Ok(frame.with_extra(extra))
531     }
532
533     fn stack<'a>(
534         ecx: &'a InterpCx<'mir, 'tcx, Self>
535     ) -> &'a [Frame<'mir, 'tcx, Self::PointerTag, Self::FrameExtra>] {
536         ecx.active_thread_stack()
537     }
538
539     fn stack_mut<'a>(
540         ecx: &'a mut InterpCx<'mir, 'tcx, Self>
541     ) -> &'a mut Vec<Frame<'mir, 'tcx, Self::PointerTag, Self::FrameExtra>> {
542         ecx.active_thread_stack_mut()
543     }
544
545     #[inline(always)]
546     fn after_stack_push(ecx: &mut InterpCx<'mir, 'tcx, Self>) -> InterpResult<'tcx> {
547         if ecx.memory.extra.stacked_borrows.is_some() {
548             ecx.retag_return_place()
549         } else {
550             Ok(())
551         }
552     }
553
554     #[inline(always)]
555     fn after_stack_pop(
556         ecx: &mut InterpCx<'mir, 'tcx, Self>,
557         frame: Frame<'mir, 'tcx, Tag, FrameData<'tcx>>,
558         unwinding: bool,
559     ) -> InterpResult<'tcx, StackPopJump> {
560         ecx.handle_stack_pop(frame.extra, unwinding)
561     }
562
563     #[inline(always)]
564     fn int_to_ptr(
565         memory: &Memory<'mir, 'tcx, Self>,
566         int: u64,
567     ) -> InterpResult<'tcx, Pointer<Self::PointerTag>> {
568         intptrcast::GlobalState::int_to_ptr(int, memory)
569     }
570
571     #[inline(always)]
572     fn ptr_to_int(
573         memory: &Memory<'mir, 'tcx, Self>,
574         ptr: Pointer<Self::PointerTag>,
575     ) -> InterpResult<'tcx, u64> {
576         intptrcast::GlobalState::ptr_to_int(ptr, memory)
577     }
578 }
579
580 impl AllocationExtra<Tag> for AllocExtra {
581     #[inline(always)]
582     fn memory_read<'tcx>(
583         alloc: &Allocation<Tag, AllocExtra>,
584         ptr: Pointer<Tag>,
585         size: Size,
586     ) -> InterpResult<'tcx> {
587         if let Some(stacked_borrows) = &alloc.extra.stacked_borrows {
588             stacked_borrows.memory_read(ptr, size)
589         } else {
590             Ok(())
591         }
592     }
593
594     #[inline(always)]
595     fn memory_written<'tcx>(
596         alloc: &mut Allocation<Tag, AllocExtra>,
597         ptr: Pointer<Tag>,
598         size: Size,
599     ) -> InterpResult<'tcx> {
600         if let Some(stacked_borrows) = &mut alloc.extra.stacked_borrows {
601             stacked_borrows.memory_written(ptr, size)
602         } else {
603             Ok(())
604         }
605     }
606
607     #[inline(always)]
608     fn memory_deallocated<'tcx>(
609         alloc: &mut Allocation<Tag, AllocExtra>,
610         ptr: Pointer<Tag>,
611         size: Size,
612     ) -> InterpResult<'tcx> {
613         if let Some(stacked_borrows) = &mut alloc.extra.stacked_borrows {
614             stacked_borrows.memory_deallocated(ptr, size)
615         } else {
616             Ok(())
617         }
618     }
619 }