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