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