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