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;
13 use rand::SeedableRng;
15 use rustc_data_structures::fx::FxHashMap;
20 layout::{LayoutCx, LayoutError, TyAndLayout},
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;
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;
37 /// Extra data stored with each stack frame
39 pub struct FrameData<'tcx> {
40 /// Extra data for Stacked Borrows.
41 pub call_id: stacked_borrows::CallId,
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>>,
49 /// Extra memory kinds
50 #[derive(Debug, Copy, Clone, PartialEq, Eq)]
51 pub enum MiriMemoryKind {
52 /// `__rust_alloc` memory.
56 /// Windows `HeapAlloc` memory.
58 /// Memory for args, errno, and other parts of the machine-managed environment.
59 /// This memory may leak.
61 /// Memory for env vars. Separate from `Machine` because we clean it up and leak-check it.
63 /// Globals copied from `tcx`.
64 /// This memory may leak.
66 /// Memory for extern statics.
67 /// This memory may leak.
69 /// Memory for thread-local statics.
70 /// This memory may leak.
74 impl Into<MemoryKind<MiriMemoryKind>> for MiriMemoryKind {
76 fn into(self) -> MemoryKind<MiriMemoryKind> {
77 MemoryKind::Machine(self)
81 impl MayLeak for MiriMemoryKind {
83 fn may_leak(self) -> bool {
84 use self::MiriMemoryKind::*;
86 Rust | C | WinHeap | Env => false,
87 Machine | Global | ExternStatic | Tls => true,
92 impl fmt::Display for MiriMemoryKind {
93 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
94 use self::MiriMemoryKind::*;
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"),
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>,
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,
125 /// Mapping extern static names to their canonical allocation.
126 extern_statics: FxHashMap<Symbol, AllocId>,
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>,
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>,
136 /// Controls whether alignment of memory accesses is being checked.
137 pub(crate) check_alignment: AlignmentCheck,
141 pub fn new(config: &MiriConfig) -> Self {
142 let rng = StdRng::seed_from_u64(config.seed.unwrap_or(0));
143 let stacked_borrows = if config.stacked_borrows {
144 Some(Rc::new(RefCell::new(stacked_borrows::GlobalState::new(
145 config.tracked_pointer_tag,
146 config.tracked_call_id,
152 let data_race = if config.data_race_detector {
153 Some(Rc::new(data_race::GlobalState::new()))
160 intptrcast: Default::default(),
161 extern_statics: FxHashMap::default(),
162 rng: RefCell::new(rng),
163 tracked_alloc_id: config.tracked_alloc_id,
164 check_alignment: config.check_alignment,
168 fn add_extern_static<'tcx, 'mir>(
169 this: &mut MiriEvalContext<'mir, 'tcx>,
173 let ptr = ptr.assert_ptr();
174 assert_eq!(ptr.offset, Size::ZERO);
178 .insert(Symbol::intern(name), ptr.alloc_id)
182 /// Sets up the "extern statics" for this machine.
183 pub fn init_extern_statics<'tcx, 'mir>(
184 this: &mut MiriEvalContext<'mir, 'tcx>,
185 ) -> InterpResult<'tcx> {
186 match this.tcx.sess.target.os.as_str() {
188 // "__cxa_thread_atexit_impl"
189 // This should be all-zero, pointer-sized.
190 let layout = this.machine.layouts.usize;
191 let place = this.allocate(layout, MiriMemoryKind::ExternStatic.into());
192 this.write_scalar(Scalar::from_machine_usize(0, this), place.into())?;
193 Self::add_extern_static(this, "__cxa_thread_atexit_impl", place.ptr);
195 Self::add_extern_static(this, "environ", this.machine.env_vars.environ.unwrap().ptr);
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);
205 _ => {} // No "extern statics" supported on this target
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>,
222 impl<'mir, 'tcx: 'mir> PrimitiveLayouts<'tcx> {
223 fn new(layout_cx: LayoutCx<'tcx, TyCtxt<'tcx>>) -> Result<Self, LayoutError<'tcx>> {
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)?,
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>,
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>>,
250 pub(crate) tls: TlsData<'tcx>,
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,
256 /// Whether to enforce the validity invariant.
257 pub(crate) validate: bool,
259 pub(crate) file_handler: shims::posix::FileHandler,
260 pub(crate) dir_handler: shims::posix::DirHandler,
262 /// The "time anchor" for this machine's monotone clock (for `Instant` simulation).
263 pub(crate) time_anchor: Instant,
265 /// The set of threads.
266 pub(crate) threads: ThreadManager<'mir, 'tcx>,
268 /// Precomputed `TyLayout`s for primitive data types that are commonly used inside Miri.
269 pub(crate) layouts: PrimitiveLayouts<'tcx>,
271 /// Allocations that are considered roots of static memory (that may leak).
272 pub(crate) static_roots: Vec<AllocId>,
275 impl<'mir, 'tcx> Evaluator<'mir, 'tcx> {
279 layout_cx: LayoutCx<'tcx, TyCtxt<'tcx>>,
281 let layouts = PrimitiveLayouts::new(layout_cx)
282 .expect("Couldn't get layouts of primitive types");
284 // `env_vars` could be initialized properly here if `Memory` were available before
285 // calling this method.
286 env_vars: EnvVars::default(),
290 tls: TlsData::default(),
293 file_handler: Default::default(),
294 dir_handler: Default::default(),
295 time_anchor: Instant::now(),
297 threads: ThreadManager::default(),
298 static_roots: Vec::new(),
303 /// A rustc InterpCx for Miri.
304 pub type MiriEvalContext<'mir, 'tcx> = InterpCx<'mir, 'tcx, Evaluator<'mir, 'tcx>>;
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>;
311 impl<'mir, 'tcx> MiriEvalContextExt<'mir, 'tcx> for MiriEvalContext<'mir, 'tcx> {
313 fn eval_context_ref(&self) -> &MiriEvalContext<'mir, 'tcx> {
317 fn eval_context_mut(&mut self) -> &mut MiriEvalContext<'mir, 'tcx> {
322 /// Machine hook implementations.
323 impl<'mir, 'tcx> Machine<'mir, 'tcx> for Evaluator<'mir, 'tcx> {
324 type MemoryKind = MiriMemoryKind;
326 type FrameExtra = FrameData<'tcx>;
327 type MemoryExtra = MemoryExtra;
328 type AllocExtra = AllocExtra;
329 type PointerTag = Tag;
330 type ExtraFnVal = Dlsym;
333 MonoHashMap<AllocId, (MemoryKind<MiriMemoryKind>, Allocation<Tag, Self::AllocExtra>)>;
335 const GLOBAL_KIND: Option<MiriMemoryKind> = Some(MiriMemoryKind::Global);
338 fn enforce_alignment(memory_extra: &MemoryExtra) -> bool {
339 memory_extra.check_alignment != AlignmentCheck::None
343 fn force_int_for_alignment_check(memory_extra: &Self::MemoryExtra) -> bool {
344 memory_extra.check_alignment == AlignmentCheck::Int
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>,
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, args, ret, unwind)
366 ecx: &mut InterpCx<'mir, 'tcx, Self>,
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, args, ret)
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)
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)
397 fn abort(_ecx: &mut InterpCx<'mir, 'tcx, Self>, msg: String) -> InterpResult<'tcx, !> {
398 throw_machine_stop!(TerminationInfo::Abort(msg))
403 ecx: &rustc_mir::interpret::InterpCx<'mir, 'tcx, Self>,
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)
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);
421 // Second argument: `align`.
422 let align = Scalar::from_machine_usize(layout.align.abi.bytes(), ecx);
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);
429 &[size.into(), align.into()],
431 // Don't do anything when we are done. The `statement()` function will increment
432 // the old stack frame's stmt counter to the next statement, which means that when
433 // `exchange_malloc` returns, we go on evaluating exactly where we want to be.
434 StackPopCleanup::None { cleanup: true },
439 fn thread_local_static_alloc_id(
440 ecx: &mut InterpCx<'mir, 'tcx, Self>,
442 ) -> InterpResult<'tcx, AllocId> {
443 ecx.get_or_create_thread_local_alloc_id(def_id)
446 fn extern_static_alloc_id(
447 memory: &Memory<'mir, 'tcx, Self>,
449 ) -> InterpResult<'tcx, AllocId> {
450 let attrs = memory.tcx.get_attrs(def_id);
451 let link_name = match memory.tcx.sess.first_attr_value_str_by_name(&attrs, sym::link_name) {
453 None => memory.tcx.item_name(def_id),
455 if let Some(&id) = memory.extra.extern_statics.get(&link_name) {
458 throw_unsup_format!("`extern` static {:?} is not supported by Miri", def_id)
462 fn init_allocation_extra<'b>(
463 memory_extra: &MemoryExtra,
465 alloc: Cow<'b, Allocation>,
466 kind: Option<MemoryKind<Self::MemoryKind>>,
467 ) -> (Cow<'b, Allocation<Self::PointerTag, Self::AllocExtra>>, Self::PointerTag) {
468 if Some(id) == memory_extra.tracked_alloc_id {
469 register_diagnostic(NonHaltingDiagnostic::CreatedAlloc(id));
472 let kind = kind.expect("we set our STATIC_KIND so this cannot be None");
473 let alloc = alloc.into_owned();
474 let (stacks, base_tag) =
475 if let Some(stacked_borrows) = &memory_extra.stacked_borrows {
476 let (stacks, base_tag) =
477 Stacks::new_allocation(id, alloc.size, Rc::clone(stacked_borrows), kind);
478 (Some(stacks), base_tag)
480 // No stacks, no tag.
481 (None, Tag::Untagged)
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))
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(
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)
499 AllocExtra { stacked_borrows: stacks, data_race: race_alloc },
501 (Cow::Owned(alloc), base_tag)
505 fn before_deallocation(
506 memory_extra: &mut Self::MemoryExtra,
508 ) -> InterpResult<'tcx> {
509 if Some(id) == memory_extra.tracked_alloc_id {
510 register_diagnostic(NonHaltingDiagnostic::FreedAlloc(id));
517 fn after_static_mem_initialized(
518 ecx: &mut InterpCx<'mir, 'tcx, Self>,
519 ptr: Pointer<Self::PointerTag>,
521 ) -> InterpResult<'tcx> {
522 if ecx.memory.extra.data_race.is_some() {
523 ecx.reset_vector_clocks(ptr, size)?;
529 fn tag_global_base_pointer(memory_extra: &MemoryExtra, id: AllocId) -> Self::PointerTag {
530 if let Some(stacked_borrows) = &memory_extra.stacked_borrows {
531 stacked_borrows.borrow_mut().global_base_ptr(id)
539 ecx: &mut InterpCx<'mir, 'tcx, Self>,
540 kind: mir::RetagKind,
541 place: PlaceTy<'tcx, Tag>,
542 ) -> InterpResult<'tcx> {
543 if ecx.memory.extra.stacked_borrows.is_some() {
544 ecx.retag(kind, place)
552 ecx: &mut InterpCx<'mir, 'tcx, Self>,
553 frame: Frame<'mir, 'tcx, Tag>,
554 ) -> InterpResult<'tcx, Frame<'mir, 'tcx, Tag, FrameData<'tcx>>> {
555 let stacked_borrows = ecx.memory.extra.stacked_borrows.as_ref();
556 let call_id = stacked_borrows.map_or(NonZeroU64::new(1).unwrap(), |stacked_borrows| {
557 stacked_borrows.borrow_mut().new_call()
559 let extra = FrameData { call_id, catch_unwind: None };
560 Ok(frame.with_extra(extra))
564 ecx: &'a InterpCx<'mir, 'tcx, Self>
565 ) -> &'a [Frame<'mir, 'tcx, Self::PointerTag, Self::FrameExtra>] {
566 ecx.active_thread_stack()
570 ecx: &'a mut InterpCx<'mir, 'tcx, Self>
571 ) -> &'a mut Vec<Frame<'mir, 'tcx, Self::PointerTag, Self::FrameExtra>> {
572 ecx.active_thread_stack_mut()
576 fn after_stack_push(ecx: &mut InterpCx<'mir, 'tcx, Self>) -> InterpResult<'tcx> {
577 if ecx.memory.extra.stacked_borrows.is_some() {
578 ecx.retag_return_place()
586 ecx: &mut InterpCx<'mir, 'tcx, Self>,
587 frame: Frame<'mir, 'tcx, Tag, FrameData<'tcx>>,
589 ) -> InterpResult<'tcx, StackPopJump> {
590 ecx.handle_stack_pop(frame.extra, unwinding)
595 memory: &Memory<'mir, 'tcx, Self>,
597 ) -> InterpResult<'tcx, Pointer<Self::PointerTag>> {
598 intptrcast::GlobalState::int_to_ptr(int, memory)
603 memory: &Memory<'mir, 'tcx, Self>,
604 ptr: Pointer<Self::PointerTag>,
605 ) -> InterpResult<'tcx, u64> {
606 intptrcast::GlobalState::ptr_to_int(ptr, memory)
610 impl AllocationExtra<Tag> for AllocExtra {
612 fn memory_read<'tcx>(
613 alloc: &Allocation<Tag, AllocExtra>,
616 ) -> InterpResult<'tcx> {
617 if let Some(data_race) = &alloc.extra.data_race {
618 data_race.read(ptr, size)?;
620 if let Some(stacked_borrows) = &alloc.extra.stacked_borrows {
621 stacked_borrows.memory_read(ptr, size)
628 fn memory_written<'tcx>(
629 alloc: &mut Allocation<Tag, AllocExtra>,
632 ) -> InterpResult<'tcx> {
633 if let Some(data_race) = &mut alloc.extra.data_race {
634 data_race.write(ptr, size)?;
636 if let Some(stacked_borrows) = &mut alloc.extra.stacked_borrows {
637 stacked_borrows.memory_written(ptr, size)
644 fn memory_deallocated<'tcx>(
645 alloc: &mut Allocation<Tag, AllocExtra>,
648 ) -> InterpResult<'tcx> {
649 if let Some(data_race) = &mut alloc.extra.data_race {
650 data_race.deallocate(ptr, size)?;
652 if let Some(stacked_borrows) = &mut alloc.extra.stacked_borrows {
653 stacked_borrows.memory_deallocated(ptr, size)