1 //! Global machine state as well as implementation of the interpreter engine
5 use std::cell::RefCell;
6 use std::num::NonZeroU64;
8 use std::time::Instant;
12 use rand::rngs::StdRng;
15 use rustc_data_structures::fx::FxHashMap;
17 middle::codegen_fn_attrs::CodegenFnAttrFlags,
21 layout::{LayoutCx, LayoutError, TyAndLayout},
25 use rustc_span::{def_id::DefId, symbol::{sym, Symbol}};
26 use rustc_target::abi::{LayoutOf, Size};
30 pub use crate::threads::{ThreadId, ThreadSet, ThreadLocalStorage};
32 // Some global facts about the emulated machine.
33 pub const PAGE_SIZE: u64 = 4 * 1024; // FIXME: adjust to target architecture
34 pub const STACK_ADDR: u64 = 32 * PAGE_SIZE; // not really about the "stack", but where we start assigning integer addresses to allocations
35 pub const STACK_SIZE: u64 = 16 * PAGE_SIZE; // whatever
36 pub const NUM_CPUS: u64 = 1;
38 /// Extra data stored with each stack frame
40 pub struct FrameData<'tcx> {
41 /// Extra data for Stacked Borrows.
42 pub call_id: stacked_borrows::CallId,
44 /// If this is Some(), then this is a special "catch unwind" frame (the frame of `try_fn`
45 /// called by `try`). When this frame is popped during unwinding a panic,
46 /// we stop unwinding, use the `CatchUnwindData` to handle catching.
47 pub catch_unwind: Option<CatchUnwindData<'tcx>>,
50 /// Extra memory kinds
51 #[derive(Debug, Copy, Clone, PartialEq, Eq)]
52 pub enum MiriMemoryKind {
53 /// `__rust_alloc` memory.
57 /// Windows `HeapAlloc` memory.
59 /// Memory for args, errno, extern statics and other parts of the machine-managed environment.
60 /// This memory may leak.
62 /// Memory for env vars. Separate from `Machine` because we clean it up and leak-check it.
64 /// Globals copied from `tcx`.
65 /// This memory may leak.
69 impl Into<MemoryKind<MiriMemoryKind>> for MiriMemoryKind {
71 fn into(self) -> MemoryKind<MiriMemoryKind> {
72 MemoryKind::Machine(self)
76 impl MayLeak for MiriMemoryKind {
78 fn may_leak(self) -> bool {
79 use self::MiriMemoryKind::*;
81 Rust | C | WinHeap | Env => false,
82 Machine | Global => true,
87 impl fmt::Display for MiriMemoryKind {
88 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
89 use self::MiriMemoryKind::*;
91 Rust => write!(f, "Rust heap"),
92 C => write!(f, "C heap"),
93 WinHeap => write!(f, "Windows heap"),
94 Machine => write!(f, "machine-managed memory"),
95 Env => write!(f, "environment variable"),
96 Global => write!(f, "global"),
101 /// Extra per-allocation data
102 #[derive(Debug, Clone)]
103 pub struct AllocExtra {
104 /// Stacked Borrows state is only added if it is enabled.
105 pub stacked_borrows: Option<stacked_borrows::AllocExtra>,
108 /// Extra global memory data
109 #[derive(Clone, Debug)]
110 pub struct MemoryExtra {
111 pub stacked_borrows: Option<stacked_borrows::MemoryExtra>,
112 pub intptrcast: intptrcast::MemoryExtra,
113 pub tls: ThreadLocalStorage,
115 /// Mapping extern static names to their canonical allocation.
116 extern_statics: FxHashMap<Symbol, AllocId>,
118 /// The random number generator used for resolving non-determinism.
119 /// Needs to be queried by ptr_to_int, hence needs interior mutability.
120 pub(crate) rng: RefCell<StdRng>,
122 /// An allocation ID to report when it is being allocated
123 /// (helps for debugging memory leaks and use after free bugs).
124 tracked_alloc_id: Option<AllocId>,
126 /// Controls whether alignment of memory accesses is being checked.
127 check_alignment: bool,
133 stacked_borrows: bool,
134 tracked_pointer_tag: Option<PtrId>,
135 tracked_alloc_id: Option<AllocId>,
136 check_alignment: bool,
138 let stacked_borrows = if stacked_borrows {
139 Some(Rc::new(RefCell::new(stacked_borrows::GlobalState::new(tracked_pointer_tag))))
145 intptrcast: Default::default(),
146 extern_statics: FxHashMap::default(),
147 rng: RefCell::new(rng),
150 tls: Default::default(),
154 fn add_extern_static<'tcx, 'mir>(
155 this: &mut MiriEvalContext<'mir, 'tcx>,
159 let ptr = ptr.assert_ptr();
160 assert_eq!(ptr.offset, Size::ZERO);
164 .insert(Symbol::intern(name), ptr.alloc_id)
168 /// Sets up the "extern statics" for this machine.
169 pub fn init_extern_statics<'tcx, 'mir>(
170 this: &mut MiriEvalContext<'mir, 'tcx>,
171 ) -> InterpResult<'tcx> {
172 match this.tcx.sess.target.target.target_os.as_str() {
174 // "__cxa_thread_atexit_impl"
175 // This should be all-zero, pointer-sized.
176 let layout = this.machine.layouts.usize;
177 let place = this.allocate(layout, MiriMemoryKind::Machine.into());
178 this.write_scalar(Scalar::from_machine_usize(0, this), place.into())?;
179 Self::add_extern_static(this, "__cxa_thread_atexit_impl", place.ptr);
181 Self::add_extern_static(this, "environ", this.machine.env_vars.environ.unwrap().ptr);
185 // This is some obscure hack that is part of the Windows TLS story. It's a `u8`.
186 let layout = this.machine.layouts.u8;
187 let place = this.allocate(layout, MiriMemoryKind::Machine.into());
188 this.write_scalar(Scalar::from_u8(0), place.into())?;
189 Self::add_extern_static(this, "_tls_used", place.ptr);
191 _ => {} // No "extern statics" supported on this target
197 /// Precomputed layouts of primitive types
198 pub struct PrimitiveLayouts<'tcx> {
199 pub unit: TyAndLayout<'tcx>,
200 pub i8: TyAndLayout<'tcx>,
201 pub i32: TyAndLayout<'tcx>,
202 pub isize: TyAndLayout<'tcx>,
203 pub u8: TyAndLayout<'tcx>,
204 pub u32: TyAndLayout<'tcx>,
205 pub usize: TyAndLayout<'tcx>,
208 impl<'mir, 'tcx: 'mir> PrimitiveLayouts<'tcx> {
209 fn new(layout_cx: LayoutCx<'tcx, TyCtxt<'tcx>>) -> Result<Self, LayoutError<'tcx>> {
211 unit: layout_cx.layout_of(layout_cx.tcx.mk_unit())?,
212 i8: layout_cx.layout_of(layout_cx.tcx.types.i8)?,
213 i32: layout_cx.layout_of(layout_cx.tcx.types.i32)?,
214 isize: layout_cx.layout_of(layout_cx.tcx.types.isize)?,
215 u8: layout_cx.layout_of(layout_cx.tcx.types.u8)?,
216 u32: layout_cx.layout_of(layout_cx.tcx.types.u32)?,
217 usize: layout_cx.layout_of(layout_cx.tcx.types.usize)?,
222 /// The machine itself.
223 pub struct Evaluator<'mir, 'tcx> {
224 /// Environment variables set by `setenv`.
225 /// Miri does not expose env vars from the host to the emulated program.
226 pub(crate) env_vars: EnvVars<'tcx>,
228 /// Program arguments (`Option` because we can only initialize them after creating the ecx).
229 /// These are *pointers* to argc/argv because macOS.
230 /// We also need the full command line as one string because of Windows.
231 pub(crate) argc: Option<Scalar<Tag>>,
232 pub(crate) argv: Option<Scalar<Tag>>,
233 pub(crate) cmd_line: Option<Scalar<Tag>>,
235 /// Last OS error location in memory. It is a 32-bit integer.
236 pub(crate) last_error: Option<MPlaceTy<'tcx, Tag>>,
239 pub(crate) tls: TlsData<'tcx>,
241 /// If enabled, the `env_vars` field is populated with the host env vars during initialization
242 /// and random number generation is delegated to the host.
243 pub(crate) communicate: bool,
245 /// Whether to enforce the validity invariant.
246 pub(crate) validate: bool,
248 pub(crate) file_handler: FileHandler,
249 pub(crate) dir_handler: DirHandler,
251 /// The temporary used for storing the argument of
252 /// the call to `miri_start_panic` (the panic payload) when unwinding.
253 /// This is pointer-sized, and matches the `Payload` type in `src/libpanic_unwind/miri.rs`.
254 pub(crate) panic_payload: Option<Scalar<Tag>>,
256 /// The "time anchor" for this machine's monotone clock (for `Instant` simulation).
257 pub(crate) time_anchor: Instant,
259 /// The set of threads.
260 pub(crate) threads: ThreadSet<'mir, 'tcx>,
262 /// Precomputed `TyLayout`s for primitive data types that are commonly used inside Miri.
263 pub(crate) layouts: PrimitiveLayouts<'tcx>,
266 impl<'mir, 'tcx> Evaluator<'mir, 'tcx> {
270 layout_cx: LayoutCx<'tcx, TyCtxt<'tcx>>,
272 let layouts = PrimitiveLayouts::new(layout_cx)
273 .expect("Couldn't get layouts of primitive types");
275 // `env_vars` could be initialized properly here if `Memory` were available before
276 // calling this method.
277 env_vars: EnvVars::default(),
282 tls: TlsData::default(),
285 file_handler: Default::default(),
286 dir_handler: Default::default(),
288 time_anchor: Instant::now(),
290 threads: Default::default(),
295 /// A rustc InterpCx for Miri.
296 pub type MiriEvalContext<'mir, 'tcx> = InterpCx<'mir, 'tcx, Evaluator<'mir, 'tcx>>;
298 /// A little trait that's useful to be inherited by extension traits.
299 pub trait MiriEvalContextExt<'mir, 'tcx> {
300 fn eval_context_ref<'a>(&'a self) -> &'a MiriEvalContext<'mir, 'tcx>;
301 fn eval_context_mut<'a>(&'a mut self) -> &'a mut MiriEvalContext<'mir, 'tcx>;
303 impl<'mir, 'tcx> MiriEvalContextExt<'mir, 'tcx> for MiriEvalContext<'mir, 'tcx> {
305 fn eval_context_ref(&self) -> &MiriEvalContext<'mir, 'tcx> {
309 fn eval_context_mut(&mut self) -> &mut MiriEvalContext<'mir, 'tcx> {
314 /// Machine hook implementations.
315 impl<'mir, 'tcx> Machine<'mir, 'tcx> for Evaluator<'mir, 'tcx> {
316 type MemoryKind = MiriMemoryKind;
318 type FrameExtra = FrameData<'tcx>;
319 type MemoryExtra = MemoryExtra;
320 type AllocExtra = AllocExtra;
321 type PointerTag = Tag;
322 type ExtraFnVal = Dlsym;
325 MonoHashMap<AllocId, (MemoryKind<MiriMemoryKind>, Allocation<Tag, Self::AllocExtra>)>;
327 const GLOBAL_KIND: Option<MiriMemoryKind> = Some(MiriMemoryKind::Global);
330 fn enforce_alignment(memory_extra: &MemoryExtra) -> bool {
331 memory_extra.check_alignment
336 ecx: &'a InterpCx<'mir, 'tcx, Self>
337 ) -> &'a [Frame<'mir, 'tcx, Self::PointerTag, Self::FrameExtra>] {
338 ecx.active_thread_stack()
342 ecx: &'a mut InterpCx<'mir, 'tcx, Self>
343 ) -> &'a mut Vec<Frame<'mir, 'tcx, Self::PointerTag, Self::FrameExtra>> {
344 ecx.active_thread_stack_mut()
348 fn enforce_validity(ecx: &InterpCx<'mir, 'tcx, Self>) -> bool {
353 fn find_mir_or_eval_fn(
354 ecx: &mut InterpCx<'mir, 'tcx, Self>,
355 instance: ty::Instance<'tcx>,
356 args: &[OpTy<'tcx, Tag>],
357 ret: Option<(PlaceTy<'tcx, Tag>, mir::BasicBlock)>,
358 unwind: Option<mir::BasicBlock>,
359 ) -> InterpResult<'tcx, Option<&'mir mir::Body<'tcx>>> {
360 ecx.find_mir_or_eval_fn(instance, args, ret, unwind)
365 ecx: &mut InterpCx<'mir, 'tcx, Self>,
367 args: &[OpTy<'tcx, Tag>],
368 ret: Option<(PlaceTy<'tcx, Tag>, mir::BasicBlock)>,
369 _unwind: Option<mir::BasicBlock>,
370 ) -> InterpResult<'tcx> {
371 ecx.call_dlsym(fn_val, args, ret)
376 ecx: &mut rustc_mir::interpret::InterpCx<'mir, 'tcx, Self>,
377 instance: ty::Instance<'tcx>,
378 args: &[OpTy<'tcx, Tag>],
379 ret: Option<(PlaceTy<'tcx, Tag>, mir::BasicBlock)>,
380 unwind: Option<mir::BasicBlock>,
381 ) -> InterpResult<'tcx> {
382 ecx.call_intrinsic(instance, args, ret, unwind)
387 ecx: &mut InterpCx<'mir, 'tcx, Self>,
388 msg: &mir::AssertMessage<'tcx>,
389 unwind: Option<mir::BasicBlock>,
390 ) -> InterpResult<'tcx> {
391 ecx.assert_panic(msg, unwind)
395 fn abort(_ecx: &mut InterpCx<'mir, 'tcx, Self>) -> InterpResult<'tcx, !> {
396 throw_machine_stop!(TerminationInfo::Abort(None))
401 ecx: &rustc_mir::interpret::InterpCx<'mir, 'tcx, Self>,
403 left: ImmTy<'tcx, Tag>,
404 right: ImmTy<'tcx, Tag>,
405 ) -> InterpResult<'tcx, (Scalar<Tag>, bool, ty::Ty<'tcx>)> {
406 ecx.binary_ptr_op(bin_op, left, right)
410 ecx: &mut InterpCx<'mir, 'tcx, Self>,
411 dest: PlaceTy<'tcx, Tag>,
412 ) -> InterpResult<'tcx> {
413 trace!("box_alloc for {:?}", dest.layout.ty);
414 let layout = ecx.layout_of(dest.layout.ty.builtin_deref(false).unwrap().ty)?;
415 // First argument: `size`.
416 // (`0` is allowed here -- this is expected to be handled by the lang item).
417 let size = Scalar::from_machine_usize(layout.size.bytes(), ecx);
419 // Second argument: `align`.
420 let align = Scalar::from_machine_usize(layout.align.abi.bytes(), ecx);
422 // Call the `exchange_malloc` lang item.
423 let malloc = ecx.tcx.lang_items().exchange_malloc_fn().unwrap();
424 let malloc = ty::Instance::mono(ecx.tcx.tcx, malloc);
427 &[size.into(), align.into()],
429 // Don't do anything when we are done. The `statement()` function will increment
430 // the old stack frame's stmt counter to the next statement, which means that when
431 // `exchange_malloc` returns, we go on evaluating exactly where we want to be.
432 StackPopCleanup::None { cleanup: true },
438 ecx: &InterpCx<'mir, 'tcx, Self>,
439 frame: &Frame<'mir, 'tcx, Self::PointerTag, Self::FrameExtra>,
441 ) -> InterpResult<'tcx, Operand<Self::PointerTag>> {
442 match frame.body.local_decls[local].local_info {
443 mir::LocalInfo::StaticRef { def_id, is_thread_local: true } => {
444 let static_alloc_id = ecx.tcx.alloc_map.lock().create_static_alloc(def_id);
445 let alloc_id = ecx.memory.extra.tls.get_or_register_allocation(*ecx.memory.tcx, static_alloc_id);
446 let tag = Self::tag_global_base_pointer(&ecx.memory.extra, alloc_id);
447 let pointer: Pointer = alloc_id.into();
448 let pointer = pointer.with_tag(tag);
449 let scalar: Scalar<_> = pointer.into();
450 let scalar: ScalarMaybeUndef<_> = scalar.into();
451 let immediate: Immediate<_> = scalar.into();
453 Operand::Immediate(immediate)
456 _ => frame.locals[local].access(),
460 fn canonical_alloc_id(mem: &Memory<'mir, 'tcx, Self>, id: AllocId) -> AllocId {
462 let alloc = tcx.alloc_map.lock().get(id);
463 fn is_thread_local<'tcx>(tcx: TyCtxt<'tcx>, def_id: DefId) -> bool {
464 tcx.codegen_fn_attrs(def_id).flags.contains(CodegenFnAttrFlags::THREAD_LOCAL)
467 Some(GlobalAlloc::Static(def_id)) if tcx.is_foreign_item(def_id) => {
468 if is_thread_local(*tcx, def_id) {
469 unimplemented!("Foreign thread local statics are not supported yet.");
471 // Figure out if this is an extern static, and if yes, which one.
472 let attrs = tcx.get_attrs(def_id);
473 let link_name = match attr::first_attr_value_str_by_name(&attrs, sym::link_name) {
475 None => tcx.item_name(def_id),
477 // Check if we know this one.
478 if let Some(canonical_id) = mem.extra.extern_statics.get(&link_name) {
479 trace!("canonical_alloc_id: {:?} ({}) -> {:?}", id, link_name, canonical_id);
482 // Return original id; `Memory::get_static_alloc` will throw an error.
486 Some(GlobalAlloc::Static(def_id)) if is_thread_local(*tcx, def_id) => {
487 // We have a thread local, so we need to get a unique allocation id for it.
488 mem.extra.tls.get_or_register_allocation(*tcx, id)
491 // No need to canonicalize anything.
498 fn resolve_maybe_global_alloc(
499 tcx: ty::query::TyCtxtAt<'tcx>,
500 extra: &Self::MemoryExtra,
502 ) -> Option<mir::interpret::GlobalAlloc<'tcx>> {
503 extra.tls.resolve_allocation(*tcx, id)
506 fn init_allocation_extra<'b>(
507 memory_extra: &MemoryExtra,
509 alloc: Cow<'b, Allocation>,
510 kind: Option<MemoryKind<Self::MemoryKind>>,
511 ) -> (Cow<'b, Allocation<Self::PointerTag, Self::AllocExtra>>, Self::PointerTag) {
512 if Some(id) == memory_extra.tracked_alloc_id {
513 register_diagnostic(NonHaltingDiagnostic::CreatedAlloc(id));
516 let kind = kind.expect("we set our STATIC_KIND so this cannot be None");
517 let alloc = alloc.into_owned();
518 let (stacks, base_tag) =
519 if let Some(stacked_borrows) = &memory_extra.stacked_borrows {
520 let (stacks, base_tag) =
521 Stacks::new_allocation(id, alloc.size, Rc::clone(stacked_borrows), kind);
522 (Some(stacks), base_tag)
524 // No stacks, no tag.
525 (None, Tag::Untagged)
527 let mut stacked_borrows = memory_extra.stacked_borrows.as_ref().map(|sb| sb.borrow_mut());
528 let alloc: Allocation<Tag, Self::AllocExtra> = alloc.with_tags_and_extra(
530 if let Some(stacked_borrows) = &mut stacked_borrows {
531 // Only globals may already contain pointers at this point
532 assert_eq!(kind, MiriMemoryKind::Global.into());
533 stacked_borrows.global_base_ptr(alloc)
538 AllocExtra { stacked_borrows: stacks },
540 (Cow::Owned(alloc), base_tag)
544 fn before_deallocation(
545 memory_extra: &mut Self::MemoryExtra,
547 ) -> InterpResult<'tcx> {
548 if Some(id) == memory_extra.tracked_alloc_id {
549 register_diagnostic(NonHaltingDiagnostic::FreedAlloc(id));
556 fn tag_global_base_pointer(memory_extra: &MemoryExtra, id: AllocId) -> Self::PointerTag {
557 if let Some(stacked_borrows) = &memory_extra.stacked_borrows {
558 stacked_borrows.borrow_mut().global_base_ptr(id)
566 ecx: &mut InterpCx<'mir, 'tcx, Self>,
567 kind: mir::RetagKind,
568 place: PlaceTy<'tcx, Tag>,
569 ) -> InterpResult<'tcx> {
570 if ecx.memory.extra.stacked_borrows.is_some() {
571 ecx.retag(kind, place)
579 ecx: &mut InterpCx<'mir, 'tcx, Self>,
580 frame: Frame<'mir, 'tcx, Tag>,
581 ) -> InterpResult<'tcx, Frame<'mir, 'tcx, Tag, FrameData<'tcx>>> {
582 let stacked_borrows = ecx.memory.extra.stacked_borrows.as_ref();
583 let call_id = stacked_borrows.map_or(NonZeroU64::new(1).unwrap(), |stacked_borrows| {
584 stacked_borrows.borrow_mut().new_call()
586 let extra = FrameData { call_id, catch_unwind: None };
587 Ok(frame.with_extra(extra))
592 ecx: &'a InterpCx<'mir, 'tcx, Self>,
593 ) -> &'a [Frame<'mir, 'tcx, Self::PointerTag, Self::FrameExtra>] {
599 ecx: &'a mut InterpCx<'mir, 'tcx, Self>,
600 ) -> &'a mut Vec<Frame<'mir, 'tcx, Self::PointerTag, Self::FrameExtra>> {
601 &mut ecx.machine.stack
605 fn after_stack_push(ecx: &mut InterpCx<'mir, 'tcx, Self>) -> InterpResult<'tcx> {
606 if ecx.memory.extra.stacked_borrows.is_some() {
607 ecx.retag_return_place()
615 ecx: &mut InterpCx<'mir, 'tcx, Self>,
616 frame: Frame<'mir, 'tcx, Tag, FrameData<'tcx>>,
618 ) -> InterpResult<'tcx, StackPopJump> {
619 ecx.handle_stack_pop(frame.extra, unwinding)
624 memory: &Memory<'mir, 'tcx, Self>,
626 ) -> InterpResult<'tcx, Pointer<Self::PointerTag>> {
627 intptrcast::GlobalState::int_to_ptr(int, memory)
632 memory: &Memory<'mir, 'tcx, Self>,
633 ptr: Pointer<Self::PointerTag>,
634 ) -> InterpResult<'tcx, u64> {
635 intptrcast::GlobalState::ptr_to_int(ptr, memory)
639 impl AllocationExtra<Tag> for AllocExtra {
641 fn memory_read<'tcx>(
642 alloc: &Allocation<Tag, AllocExtra>,
645 ) -> InterpResult<'tcx> {
646 if let Some(stacked_borrows) = &alloc.extra.stacked_borrows {
647 stacked_borrows.memory_read(ptr, size)
654 fn memory_written<'tcx>(
655 alloc: &mut Allocation<Tag, AllocExtra>,
658 ) -> InterpResult<'tcx> {
659 if let Some(stacked_borrows) = &mut alloc.extra.stacked_borrows {
660 stacked_borrows.memory_written(ptr, size)
667 fn memory_deallocated<'tcx>(
668 alloc: &mut Allocation<Tag, AllocExtra>,
671 ) -> InterpResult<'tcx> {
672 if let Some(stacked_borrows) = &mut alloc.extra.stacked_borrows {
673 stacked_borrows.memory_deallocated(ptr, size)