]> git.lizzy.rs Git - rust.git/blob - src/machine.rs
add option to track call IDs
[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
14 use rustc_ast::attr;
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, extern statics 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 }
66
67 impl Into<MemoryKind<MiriMemoryKind>> for MiriMemoryKind {
68     #[inline(always)]
69     fn into(self) -> MemoryKind<MiriMemoryKind> {
70         MemoryKind::Machine(self)
71     }
72 }
73
74 impl MayLeak for MiriMemoryKind {
75     #[inline(always)]
76     fn may_leak(self) -> bool {
77         use self::MiriMemoryKind::*;
78         match self {
79             Rust | C | WinHeap | Env => false,
80             Machine | Global => true,
81         }
82     }
83 }
84
85 impl fmt::Display for MiriMemoryKind {
86     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
87         use self::MiriMemoryKind::*;
88         match self {
89             Rust => write!(f, "Rust heap"),
90             C => write!(f, "C heap"),
91             WinHeap => write!(f, "Windows heap"),
92             Machine => write!(f, "machine-managed memory"),
93             Env => write!(f, "environment variable"),
94             Global => write!(f, "global"),
95         }
96     }
97 }
98
99 /// Extra per-allocation data
100 #[derive(Debug, Clone)]
101 pub struct AllocExtra {
102     /// Stacked Borrows state is only added if it is enabled.
103     pub stacked_borrows: Option<stacked_borrows::AllocExtra>,
104 }
105
106 /// Extra global memory data
107 #[derive(Clone, Debug)]
108 pub struct MemoryExtra {
109     pub stacked_borrows: Option<stacked_borrows::MemoryExtra>,
110     pub intptrcast: intptrcast::MemoryExtra,
111
112     /// Mapping extern static names to their canonical allocation.
113     extern_statics: FxHashMap<Symbol, AllocId>,
114
115     /// The random number generator used for resolving non-determinism.
116     /// Needs to be queried by ptr_to_int, hence needs interior mutability.
117     pub(crate) rng: RefCell<StdRng>,
118
119     /// An allocation ID to report when it is being allocated
120     /// (helps for debugging memory leaks and use after free bugs).
121     tracked_alloc_id: Option<AllocId>,
122
123     /// Controls whether alignment of memory accesses is being checked.
124     check_alignment: bool,
125 }
126
127 impl MemoryExtra {
128     pub fn new(
129         rng: StdRng,
130         stacked_borrows: bool,
131         tracked_pointer_tag: Option<PtrId>,
132         tracked_call_id: Option<CallId>,
133         tracked_alloc_id: Option<AllocId>,
134         check_alignment: bool,
135     ) -> Self {
136         let stacked_borrows = if stacked_borrows {
137             Some(Rc::new(RefCell::new(stacked_borrows::GlobalState::new(tracked_pointer_tag, tracked_call_id))))
138         } else {
139             None
140         };
141         MemoryExtra {
142             stacked_borrows,
143             intptrcast: Default::default(),
144             extern_statics: FxHashMap::default(),
145             rng: RefCell::new(rng),
146             tracked_alloc_id,
147             check_alignment,
148         }
149     }
150
151     fn add_extern_static<'tcx, 'mir>(
152         this: &mut MiriEvalContext<'mir, 'tcx>,
153         name: &str,
154         ptr: Scalar<Tag>,
155     ) {
156         let ptr = ptr.assert_ptr();
157         assert_eq!(ptr.offset, Size::ZERO);
158         this.memory
159             .extra
160             .extern_statics
161             .insert(Symbol::intern(name), ptr.alloc_id)
162             .unwrap_none();
163     }
164
165     /// Sets up the "extern statics" for this machine.
166     pub fn init_extern_statics<'tcx, 'mir>(
167         this: &mut MiriEvalContext<'mir, 'tcx>,
168     ) -> InterpResult<'tcx> {
169         match this.tcx.sess.target.target.target_os.as_str() {
170             "linux" => {
171                 // "__cxa_thread_atexit_impl"
172                 // This should be all-zero, pointer-sized.
173                 let layout = this.machine.layouts.usize;
174                 let place = this.allocate(layout, MiriMemoryKind::Machine.into());
175                 this.write_scalar(Scalar::from_machine_usize(0, this), place.into())?;
176                 Self::add_extern_static(this, "__cxa_thread_atexit_impl", place.ptr);
177                 // "environ"
178                 Self::add_extern_static(this, "environ", this.machine.env_vars.environ.unwrap().ptr);
179             }
180             "windows" => {
181                 // "_tls_used"
182                 // This is some obscure hack that is part of the Windows TLS story. It's a `u8`.
183                 let layout = this.machine.layouts.u8;
184                 let place = this.allocate(layout, MiriMemoryKind::Machine.into());
185                 this.write_scalar(Scalar::from_u8(0), place.into())?;
186                 Self::add_extern_static(this, "_tls_used", place.ptr);
187             }
188             _ => {} // No "extern statics" supported on this target
189         }
190         Ok(())
191     }
192 }
193
194 /// Precomputed layouts of primitive types
195 pub struct PrimitiveLayouts<'tcx> {
196     pub unit: TyAndLayout<'tcx>,
197     pub i8: TyAndLayout<'tcx>,
198     pub i32: TyAndLayout<'tcx>,
199     pub isize: TyAndLayout<'tcx>,
200     pub u8: TyAndLayout<'tcx>,
201     pub u32: TyAndLayout<'tcx>,
202     pub usize: TyAndLayout<'tcx>,
203 }
204
205 impl<'mir, 'tcx: 'mir> PrimitiveLayouts<'tcx> {
206     fn new(layout_cx: LayoutCx<'tcx, TyCtxt<'tcx>>) -> Result<Self, LayoutError<'tcx>> {
207         Ok(Self {
208             unit: layout_cx.layout_of(layout_cx.tcx.mk_unit())?,
209             i8: layout_cx.layout_of(layout_cx.tcx.types.i8)?,
210             i32: layout_cx.layout_of(layout_cx.tcx.types.i32)?,
211             isize: layout_cx.layout_of(layout_cx.tcx.types.isize)?,
212             u8: layout_cx.layout_of(layout_cx.tcx.types.u8)?,
213             u32: layout_cx.layout_of(layout_cx.tcx.types.u32)?,
214             usize: layout_cx.layout_of(layout_cx.tcx.types.usize)?,
215         })
216     }
217 }
218
219 /// The machine itself.
220 pub struct Evaluator<'mir, 'tcx> {
221     /// Environment variables set by `setenv`.
222     /// Miri does not expose env vars from the host to the emulated program.
223     pub(crate) env_vars: EnvVars<'tcx>,
224
225     /// Program arguments (`Option` because we can only initialize them after creating the ecx).
226     /// These are *pointers* to argc/argv because macOS.
227     /// We also need the full command line as one string because of Windows.
228     pub(crate) argc: Option<Scalar<Tag>>,
229     pub(crate) argv: Option<Scalar<Tag>>,
230     pub(crate) cmd_line: Option<Scalar<Tag>>,
231
232     /// Last OS error location in memory. It is a 32-bit integer.
233     pub(crate) last_error: Option<MPlaceTy<'tcx, Tag>>,
234
235     /// TLS state.
236     pub(crate) tls: TlsData<'tcx>,
237
238     /// If enabled, the `env_vars` field is populated with the host env vars during initialization
239     /// and random number generation is delegated to the host.
240     pub(crate) communicate: bool,
241
242     /// Whether to enforce the validity invariant.
243     pub(crate) validate: bool,
244
245     pub(crate) file_handler: shims::posix::FileHandler,
246     pub(crate) dir_handler: shims::posix::DirHandler,
247
248     /// The temporary used for storing the argument of
249     /// the call to `miri_start_panic` (the panic payload) when unwinding.
250     /// This is pointer-sized, and matches the `Payload` type in `src/libpanic_unwind/miri.rs`.
251     pub(crate) panic_payload: Option<Scalar<Tag>>,
252
253     /// The "time anchor" for this machine's monotone clock (for `Instant` simulation).
254     pub(crate) time_anchor: Instant,
255
256     /// The set of threads.
257     pub(crate) threads: ThreadManager<'mir, 'tcx>,
258
259     /// Precomputed `TyLayout`s for primitive data types that are commonly used inside Miri.
260     pub(crate) layouts: PrimitiveLayouts<'tcx>,
261 }
262
263 impl<'mir, 'tcx> Evaluator<'mir, 'tcx> {
264     pub(crate) fn new(
265         communicate: bool,
266         validate: bool,
267         layout_cx: LayoutCx<'tcx, TyCtxt<'tcx>>,
268     ) -> Self {
269         let layouts = PrimitiveLayouts::new(layout_cx)
270             .expect("Couldn't get layouts of primitive types");
271         Evaluator {
272             // `env_vars` could be initialized properly here if `Memory` were available before
273             // calling this method.
274             env_vars: EnvVars::default(),
275             argc: None,
276             argv: None,
277             cmd_line: None,
278             last_error: None,
279             tls: TlsData::default(),
280             communicate,
281             validate,
282             file_handler: Default::default(),
283             dir_handler: Default::default(),
284             panic_payload: None,
285             time_anchor: Instant::now(),
286             layouts,
287             threads: ThreadManager::default(),
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
329     }
330
331     #[inline(always)]
332     fn enforce_validity(ecx: &InterpCx<'mir, 'tcx, Self>) -> bool {
333         ecx.machine.validate
334     }
335
336     #[inline(always)]
337     fn find_mir_or_eval_fn(
338         ecx: &mut InterpCx<'mir, 'tcx, Self>,
339         instance: ty::Instance<'tcx>,
340         args: &[OpTy<'tcx, Tag>],
341         ret: Option<(PlaceTy<'tcx, Tag>, mir::BasicBlock)>,
342         unwind: Option<mir::BasicBlock>,
343     ) -> InterpResult<'tcx, Option<&'mir mir::Body<'tcx>>> {
344         ecx.find_mir_or_eval_fn(instance, args, ret, unwind)
345     }
346
347     #[inline(always)]
348     fn call_extra_fn(
349         ecx: &mut InterpCx<'mir, 'tcx, Self>,
350         fn_val: Dlsym,
351         args: &[OpTy<'tcx, Tag>],
352         ret: Option<(PlaceTy<'tcx, Tag>, mir::BasicBlock)>,
353         _unwind: Option<mir::BasicBlock>,
354     ) -> InterpResult<'tcx> {
355         ecx.call_dlsym(fn_val, args, ret)
356     }
357
358     #[inline(always)]
359     fn call_intrinsic(
360         ecx: &mut rustc_mir::interpret::InterpCx<'mir, 'tcx, Self>,
361         instance: ty::Instance<'tcx>,
362         args: &[OpTy<'tcx, Tag>],
363         ret: Option<(PlaceTy<'tcx, Tag>, mir::BasicBlock)>,
364         unwind: Option<mir::BasicBlock>,
365     ) -> InterpResult<'tcx> {
366         ecx.call_intrinsic(instance, args, ret, unwind)
367     }
368
369     #[inline(always)]
370     fn assert_panic(
371         ecx: &mut InterpCx<'mir, 'tcx, Self>,
372         msg: &mir::AssertMessage<'tcx>,
373         unwind: Option<mir::BasicBlock>,
374     ) -> InterpResult<'tcx> {
375         ecx.assert_panic(msg, unwind)
376     }
377
378     #[inline(always)]
379     fn abort(_ecx: &mut InterpCx<'mir, 'tcx, Self>) -> InterpResult<'tcx, !> {
380         throw_machine_stop!(TerminationInfo::Abort(None))
381     }
382
383     #[inline(always)]
384     fn binary_ptr_op(
385         ecx: &rustc_mir::interpret::InterpCx<'mir, 'tcx, Self>,
386         bin_op: mir::BinOp,
387         left: ImmTy<'tcx, Tag>,
388         right: ImmTy<'tcx, Tag>,
389     ) -> InterpResult<'tcx, (Scalar<Tag>, bool, ty::Ty<'tcx>)> {
390         ecx.binary_ptr_op(bin_op, left, right)
391     }
392
393     fn box_alloc(
394         ecx: &mut InterpCx<'mir, 'tcx, Self>,
395         dest: PlaceTy<'tcx, Tag>,
396     ) -> InterpResult<'tcx> {
397         trace!("box_alloc for {:?}", dest.layout.ty);
398         let layout = ecx.layout_of(dest.layout.ty.builtin_deref(false).unwrap().ty)?;
399         // First argument: `size`.
400         // (`0` is allowed here -- this is expected to be handled by the lang item).
401         let size = Scalar::from_machine_usize(layout.size.bytes(), ecx);
402
403         // Second argument: `align`.
404         let align = Scalar::from_machine_usize(layout.align.abi.bytes(), ecx);
405
406         // Call the `exchange_malloc` lang item.
407         let malloc = ecx.tcx.lang_items().exchange_malloc_fn().unwrap();
408         let malloc = ty::Instance::mono(ecx.tcx.tcx, malloc);
409         ecx.call_function(
410             malloc,
411             &[size.into(), align.into()],
412             Some(dest),
413             // Don't do anything when we are done. The `statement()` function will increment
414             // the old stack frame's stmt counter to the next statement, which means that when
415             // `exchange_malloc` returns, we go on evaluating exactly where we want to be.
416             StackPopCleanup::None { cleanup: true },
417         )?;
418         Ok(())
419     }
420
421     fn thread_local_alloc_id(
422         ecx: &mut InterpCx<'mir, 'tcx, Self>,
423         def_id: DefId,
424     ) -> InterpResult<'tcx, AllocId> {
425         ecx.get_or_create_thread_local_alloc_id(def_id)
426     }
427
428     fn adjust_global_const(
429         ecx: &InterpCx<'mir, 'tcx, Self>,
430         mut val: mir::interpret::ConstValue<'tcx>,
431     ) -> InterpResult<'tcx, mir::interpret::ConstValue<'tcx>> {
432         // FIXME: Remove this, do The Right Thing in `thread_local_alloc_id` instead.
433         ecx.remap_thread_local_alloc_ids(&mut val)?;
434         Ok(val)
435     }
436
437     fn canonical_alloc_id(mem: &Memory<'mir, 'tcx, Self>, id: AllocId) -> AllocId {
438         let tcx = mem.tcx;
439         // Figure out if this is an extern static, and if yes, which one.
440         let def_id = match tcx.get_global_alloc(id) {
441             Some(GlobalAlloc::Static(def_id)) if tcx.is_foreign_item(def_id) => def_id,
442             _ => {
443                 // No need to canonicalize anything.
444                 return id;
445             }
446         };
447         let attrs = tcx.get_attrs(def_id);
448         let link_name = match attr::first_attr_value_str_by_name(&attrs, sym::link_name) {
449             Some(name) => name,
450             None => tcx.item_name(def_id),
451         };
452         // Check if we know this one.
453         if let Some(canonical_id) = mem.extra.extern_statics.get(&link_name) {
454             trace!("canonical_alloc_id: {:?} ({}) -> {:?}", id, link_name, canonical_id);
455             *canonical_id
456         } else {
457             // Return original id; `Memory::get_static_alloc` will throw an error.
458             id
459         }
460     }
461
462     fn init_allocation_extra<'b>(
463         memory_extra: &MemoryExtra,
464         id: AllocId,
465         alloc: Cow<'b, Allocation>,
466         kind: Option<MemoryKind<Self::MemoryKind>>,
467     ) -> (Cow<'b, Allocation<Self::PointerTag, Self::AllocExtra>>, Self::PointerTag) {
468         if Some(id) == memory_extra.tracked_alloc_id {
469             register_diagnostic(NonHaltingDiagnostic::CreatedAlloc(id));
470         }
471
472         let kind = kind.expect("we set our STATIC_KIND so this cannot be None");
473         let alloc = alloc.into_owned();
474         let (stacks, base_tag) =
475             if let Some(stacked_borrows) = &memory_extra.stacked_borrows {
476                 let (stacks, base_tag) =
477                     Stacks::new_allocation(id, alloc.size, Rc::clone(stacked_borrows), kind);
478                 (Some(stacks), base_tag)
479             } else {
480                 // No stacks, no tag.
481                 (None, Tag::Untagged)
482             };
483         let mut stacked_borrows = memory_extra.stacked_borrows.as_ref().map(|sb| sb.borrow_mut());
484         let alloc: Allocation<Tag, Self::AllocExtra> = alloc.with_tags_and_extra(
485             |alloc| {
486                 if let Some(stacked_borrows) = &mut stacked_borrows {
487                     // Only globals may already contain pointers at this point
488                     assert_eq!(kind, MiriMemoryKind::Global.into());
489                     stacked_borrows.global_base_ptr(alloc)
490                 } else {
491                     Tag::Untagged
492                 }
493             },
494             AllocExtra { stacked_borrows: stacks },
495         );
496         (Cow::Owned(alloc), base_tag)
497     }
498
499     #[inline(always)]
500     fn before_deallocation(
501         memory_extra: &mut Self::MemoryExtra,
502         id: AllocId,
503     ) -> InterpResult<'tcx> {
504         if Some(id) == memory_extra.tracked_alloc_id {
505             register_diagnostic(NonHaltingDiagnostic::FreedAlloc(id));
506         }
507
508         Ok(())
509     }
510
511     #[inline(always)]
512     fn tag_global_base_pointer(memory_extra: &MemoryExtra, id: AllocId) -> Self::PointerTag {
513         if let Some(stacked_borrows) = &memory_extra.stacked_borrows {
514             stacked_borrows.borrow_mut().global_base_ptr(id)
515         } else {
516             Tag::Untagged
517         }
518     }
519
520     #[inline(always)]
521     fn retag(
522         ecx: &mut InterpCx<'mir, 'tcx, Self>,
523         kind: mir::RetagKind,
524         place: PlaceTy<'tcx, Tag>,
525     ) -> InterpResult<'tcx> {
526         if ecx.memory.extra.stacked_borrows.is_some() {
527             ecx.retag(kind, place)
528         } else {
529             Ok(())
530         }
531     }
532
533     #[inline(always)]
534     fn init_frame_extra(
535         ecx: &mut InterpCx<'mir, 'tcx, Self>,
536         frame: Frame<'mir, 'tcx, Tag>,
537     ) -> InterpResult<'tcx, Frame<'mir, 'tcx, Tag, FrameData<'tcx>>> {
538         let stacked_borrows = ecx.memory.extra.stacked_borrows.as_ref();
539         let call_id = stacked_borrows.map_or(NonZeroU64::new(1).unwrap(), |stacked_borrows| {
540             stacked_borrows.borrow_mut().new_call()
541         });
542         let extra = FrameData { call_id, catch_unwind: None };
543         Ok(frame.with_extra(extra))
544     }
545
546     fn stack<'a>(
547         ecx: &'a InterpCx<'mir, 'tcx, Self>
548     ) -> &'a [Frame<'mir, 'tcx, Self::PointerTag, Self::FrameExtra>] {
549         ecx.active_thread_stack()
550     }
551
552     fn stack_mut<'a>(
553         ecx: &'a mut InterpCx<'mir, 'tcx, Self>
554     ) -> &'a mut Vec<Frame<'mir, 'tcx, Self::PointerTag, Self::FrameExtra>> {
555         ecx.active_thread_stack_mut()
556     }
557
558     #[inline(always)]
559     fn after_stack_push(ecx: &mut InterpCx<'mir, 'tcx, Self>) -> InterpResult<'tcx> {
560         if ecx.memory.extra.stacked_borrows.is_some() {
561             ecx.retag_return_place()
562         } else {
563             Ok(())
564         }
565     }
566
567     #[inline(always)]
568     fn after_stack_pop(
569         ecx: &mut InterpCx<'mir, 'tcx, Self>,
570         frame: Frame<'mir, 'tcx, Tag, FrameData<'tcx>>,
571         unwinding: bool,
572     ) -> InterpResult<'tcx, StackPopJump> {
573         ecx.handle_stack_pop(frame.extra, unwinding)
574     }
575
576     #[inline(always)]
577     fn int_to_ptr(
578         memory: &Memory<'mir, 'tcx, Self>,
579         int: u64,
580     ) -> InterpResult<'tcx, Pointer<Self::PointerTag>> {
581         intptrcast::GlobalState::int_to_ptr(int, memory)
582     }
583
584     #[inline(always)]
585     fn ptr_to_int(
586         memory: &Memory<'mir, 'tcx, Self>,
587         ptr: Pointer<Self::PointerTag>,
588     ) -> InterpResult<'tcx, u64> {
589         intptrcast::GlobalState::ptr_to_int(ptr, memory)
590     }
591 }
592
593 impl AllocationExtra<Tag> for AllocExtra {
594     #[inline(always)]
595     fn memory_read<'tcx>(
596         alloc: &Allocation<Tag, AllocExtra>,
597         ptr: Pointer<Tag>,
598         size: Size,
599     ) -> InterpResult<'tcx> {
600         if let Some(stacked_borrows) = &alloc.extra.stacked_borrows {
601             stacked_borrows.memory_read(ptr, size)
602         } else {
603             Ok(())
604         }
605     }
606
607     #[inline(always)]
608     fn memory_written<'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_written(ptr, size)
615         } else {
616             Ok(())
617         }
618     }
619
620     #[inline(always)]
621     fn memory_deallocated<'tcx>(
622         alloc: &mut Allocation<Tag, AllocExtra>,
623         ptr: Pointer<Tag>,
624         size: Size,
625     ) -> InterpResult<'tcx> {
626         if let Some(stacked_borrows) = &mut alloc.extra.stacked_borrows {
627             stacked_borrows.memory_deallocated(ptr, size)
628         } else {
629             Ok(())
630         }
631     }
632 }