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