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