1 //! Global machine state as well as implementation of the interpreter engine
5 use std::cell::RefCell;
6 use std::num::NonZeroU64;
9 use rand::rngs::StdRng;
14 layout::{LayoutOf, Size},
18 use rustc_hir::def_id::DefId;
19 use rustc_span::{source_map::Span, symbol::sym};
23 // Some global facts about the emulated machine.
24 pub const PAGE_SIZE: u64 = 4 * 1024; // FIXME: adjust to target architecture
25 pub const STACK_ADDR: u64 = 32 * PAGE_SIZE; // not really about the "stack", but where we start assigning integer addresses to allocations
26 pub const STACK_SIZE: u64 = 16 * PAGE_SIZE; // whatever
27 pub const NUM_CPUS: u64 = 1;
29 /// Extra data stored with each stack frame
31 pub struct FrameData<'tcx> {
32 /// Extra data for Stacked Borrows.
33 pub call_id: stacked_borrows::CallId,
35 /// If this is Some(), then this is a special "catch unwind" frame (the frame of the closure
36 /// called by `__rustc_maybe_catch_panic`). When this frame is popped during unwinding a panic,
37 /// we stop unwinding, use the `CatchUnwindData` to
38 /// store the panic payload, and continue execution in the parent frame.
39 pub catch_panic: Option<CatchUnwindData<'tcx>>,
42 /// Extra memory kinds
43 #[derive(Debug, Copy, Clone, PartialEq, Eq)]
44 pub enum MiriMemoryKind {
45 /// `__rust_alloc` memory.
49 /// Windows `HeapAlloc` memory.
51 /// Memory for env vars and args, errno, extern statics and other parts of the machine-managed environment.
57 impl Into<MemoryKind<MiriMemoryKind>> for MiriMemoryKind {
59 fn into(self) -> MemoryKind<MiriMemoryKind> {
60 MemoryKind::Machine(self)
64 /// Extra per-allocation data
65 #[derive(Debug, Clone)]
66 pub struct AllocExtra {
67 /// Stacked Borrows state is only added if it is enabled.
68 pub stacked_borrows: Option<stacked_borrows::AllocExtra>,
71 /// Extra global memory data
72 #[derive(Clone, Debug)]
73 pub struct MemoryExtra {
74 pub stacked_borrows: Option<stacked_borrows::MemoryExtra>,
75 pub intptrcast: intptrcast::MemoryExtra,
77 /// The random number generator used for resolving non-determinism.
78 pub(crate) rng: RefCell<StdRng>,
82 pub fn new(rng: StdRng, stacked_borrows: bool, tracked_pointer_tag: Option<PtrId>) -> Self {
83 let stacked_borrows = if stacked_borrows {
84 Some(Rc::new(RefCell::new(stacked_borrows::GlobalState::new(tracked_pointer_tag))))
88 MemoryExtra { stacked_borrows, intptrcast: Default::default(), rng: RefCell::new(rng) }
92 /// The machine itself.
93 pub struct Evaluator<'tcx> {
94 /// Environment variables set by `setenv`.
95 /// Miri does not expose env vars from the host to the emulated program.
96 pub(crate) env_vars: EnvVars,
98 /// Program arguments (`Option` because we can only initialize them after creating the ecx).
99 /// These are *pointers* to argc/argv because macOS.
100 /// We also need the full command line as one string because of Windows.
101 pub(crate) argc: Option<Scalar<Tag>>,
102 pub(crate) argv: Option<Scalar<Tag>>,
103 pub(crate) cmd_line: Option<Scalar<Tag>>,
105 /// Last OS error location in memory. It is a 32-bit integer.
106 pub(crate) last_error: Option<MPlaceTy<'tcx, Tag>>,
109 pub(crate) tls: TlsData<'tcx>,
111 /// If enabled, the `env_vars` field is populated with the host env vars during initialization
112 /// and random number generation is delegated to the host.
113 pub(crate) communicate: bool,
115 /// Whether to enforce the validity invariant.
116 pub(crate) validate: bool,
118 pub(crate) file_handler: FileHandler,
119 pub(crate) dir_handler: DirHandler,
121 /// The temporary used for storing the argument of
122 /// the call to `miri_start_panic` (the panic payload) when unwinding.
123 pub(crate) panic_payload: Option<ImmTy<'tcx, Tag>>,
126 impl<'tcx> Evaluator<'tcx> {
127 pub(crate) fn new(communicate: bool, validate: bool) -> Self {
129 // `env_vars` could be initialized properly here if `Memory` were available before
130 // calling this method.
131 env_vars: EnvVars::default(),
136 tls: TlsData::default(),
139 file_handler: Default::default(),
140 dir_handler: Default::default(),
146 /// A rustc InterpCx for Miri.
147 pub type MiriEvalContext<'mir, 'tcx> = InterpCx<'mir, 'tcx, Evaluator<'tcx>>;
149 /// A little trait that's useful to be inherited by extension traits.
150 pub trait MiriEvalContextExt<'mir, 'tcx> {
151 fn eval_context_ref<'a>(&'a self) -> &'a MiriEvalContext<'mir, 'tcx>;
152 fn eval_context_mut<'a>(&'a mut self) -> &'a mut MiriEvalContext<'mir, 'tcx>;
154 impl<'mir, 'tcx> MiriEvalContextExt<'mir, 'tcx> for MiriEvalContext<'mir, 'tcx> {
156 fn eval_context_ref(&self) -> &MiriEvalContext<'mir, 'tcx> {
160 fn eval_context_mut(&mut self) -> &mut MiriEvalContext<'mir, 'tcx> {
165 /// Machine hook implementations.
166 impl<'mir, 'tcx> Machine<'mir, 'tcx> for Evaluator<'tcx> {
167 type MemoryKinds = MiriMemoryKind;
169 type FrameExtra = FrameData<'tcx>;
170 type MemoryExtra = MemoryExtra;
171 type AllocExtra = AllocExtra;
172 type PointerTag = Tag;
173 type ExtraFnVal = Dlsym;
176 MonoHashMap<AllocId, (MemoryKind<MiriMemoryKind>, Allocation<Tag, Self::AllocExtra>)>;
178 const STATIC_KIND: Option<MiriMemoryKind> = Some(MiriMemoryKind::Static);
180 const CHECK_ALIGN: bool = true;
183 fn enforce_validity(ecx: &InterpCx<'mir, 'tcx, Self>) -> bool {
188 fn find_mir_or_eval_fn(
189 ecx: &mut InterpCx<'mir, 'tcx, Self>,
191 instance: ty::Instance<'tcx>,
192 args: &[OpTy<'tcx, Tag>],
193 ret: Option<(PlaceTy<'tcx, Tag>, mir::BasicBlock)>,
194 unwind: Option<mir::BasicBlock>,
195 ) -> InterpResult<'tcx, Option<&'mir mir::Body<'tcx>>> {
196 ecx.find_mir_or_eval_fn(instance, args, ret, unwind)
201 ecx: &mut InterpCx<'mir, 'tcx, Self>,
203 args: &[OpTy<'tcx, Tag>],
204 ret: Option<(PlaceTy<'tcx, Tag>, mir::BasicBlock)>,
205 _unwind: Option<mir::BasicBlock>,
206 ) -> InterpResult<'tcx> {
207 ecx.call_dlsym(fn_val, args, ret)
212 ecx: &mut rustc_mir::interpret::InterpCx<'mir, 'tcx, Self>,
214 instance: ty::Instance<'tcx>,
215 args: &[OpTy<'tcx, Tag>],
216 ret: Option<(PlaceTy<'tcx, Tag>, mir::BasicBlock)>,
217 unwind: Option<mir::BasicBlock>,
218 ) -> InterpResult<'tcx> {
219 ecx.call_intrinsic(span, instance, args, ret, unwind)
224 ecx: &mut InterpCx<'mir, 'tcx, Self>,
226 msg: &mir::AssertMessage<'tcx>,
227 unwind: Option<mir::BasicBlock>,
228 ) -> InterpResult<'tcx> {
229 ecx.assert_panic(span, msg, unwind)
234 ecx: &rustc_mir::interpret::InterpCx<'mir, 'tcx, Self>,
236 left: ImmTy<'tcx, Tag>,
237 right: ImmTy<'tcx, Tag>,
238 ) -> InterpResult<'tcx, (Scalar<Tag>, bool, Ty<'tcx>)> {
239 ecx.binary_ptr_op(bin_op, left, right)
243 ecx: &mut InterpCx<'mir, 'tcx, Self>,
244 dest: PlaceTy<'tcx, Tag>,
245 ) -> InterpResult<'tcx> {
246 trace!("box_alloc for {:?}", dest.layout.ty);
247 let layout = ecx.layout_of(dest.layout.ty.builtin_deref(false).unwrap().ty)?;
248 // First argument: `size`.
249 // (`0` is allowed here -- this is expected to be handled by the lang item).
250 let size = Scalar::from_uint(layout.size.bytes(), ecx.pointer_size());
252 // Second argument: `align`.
253 let align = Scalar::from_uint(layout.align.abi.bytes(), ecx.pointer_size());
255 // Call the `exchange_malloc` lang item.
256 let malloc = ecx.tcx.lang_items().exchange_malloc_fn().unwrap();
257 let malloc = ty::Instance::mono(ecx.tcx.tcx, malloc);
260 &[size.into(), align.into()],
262 // Don't do anything when we are done. The `statement()` function will increment
263 // the old stack frame's stmt counter to the next statement, which means that when
264 // `exchange_malloc` returns, we go on evaluating exactly where we want to be.
265 StackPopCleanup::None { cleanup: true },
270 fn find_foreign_static(
273 ) -> InterpResult<'tcx, Cow<'tcx, Allocation>> {
274 let attrs = tcx.get_attrs(def_id);
275 let link_name = match attr::first_attr_value_str_by_name(&attrs, sym::link_name) {
276 Some(name) => name.as_str(),
277 None => tcx.item_name(def_id).as_str(),
280 let alloc = match &*link_name {
281 "__cxa_thread_atexit_impl" => {
282 // This should be all-zero, pointer-sized.
283 let size = tcx.data_layout.pointer_size;
284 let data = vec![0; size.bytes() as usize];
285 Allocation::from_bytes(&data, tcx.data_layout.pointer_align.abi)
287 _ => throw_unsup_format!("can't access foreign static: {}", link_name),
289 Ok(Cow::Owned(alloc))
293 fn before_terminator(_ecx: &mut InterpCx<'mir, 'tcx, Self>) -> InterpResult<'tcx> {
294 // We are not interested in detecting loops.
298 fn init_allocation_extra<'b>(
299 memory_extra: &MemoryExtra,
301 alloc: Cow<'b, Allocation>,
302 kind: Option<MemoryKind<Self::MemoryKinds>>,
303 ) -> (Cow<'b, Allocation<Self::PointerTag, Self::AllocExtra>>, Self::PointerTag) {
304 let kind = kind.expect("we set our STATIC_KIND so this cannot be None");
305 let alloc = alloc.into_owned();
306 let (stacks, base_tag) =
307 if let Some(stacked_borrows) = memory_extra.stacked_borrows.as_ref() {
308 let (stacks, base_tag) =
309 Stacks::new_allocation(id, alloc.size, Rc::clone(stacked_borrows), kind);
310 (Some(stacks), base_tag)
312 // No stacks, no tag.
313 (None, Tag::Untagged)
315 let mut stacked_borrows = memory_extra.stacked_borrows.as_ref().map(|sb| sb.borrow_mut());
316 let alloc: Allocation<Tag, Self::AllocExtra> = alloc.with_tags_and_extra(
318 if let Some(stacked_borrows) = stacked_borrows.as_mut() {
319 // Only statics may already contain pointers at this point
320 assert_eq!(kind, MiriMemoryKind::Static.into());
321 stacked_borrows.static_base_ptr(alloc)
326 AllocExtra { stacked_borrows: stacks },
328 (Cow::Owned(alloc), base_tag)
332 fn tag_static_base_pointer(memory_extra: &MemoryExtra, id: AllocId) -> Self::PointerTag {
333 if let Some(stacked_borrows) = memory_extra.stacked_borrows.as_ref() {
334 stacked_borrows.borrow_mut().static_base_ptr(id)
342 ecx: &mut InterpCx<'mir, 'tcx, Self>,
343 kind: mir::RetagKind,
344 place: PlaceTy<'tcx, Tag>,
345 ) -> InterpResult<'tcx> {
346 if ecx.memory.extra.stacked_borrows.is_none() {
350 ecx.retag(kind, place)
355 fn stack_push(ecx: &mut InterpCx<'mir, 'tcx, Self>) -> InterpResult<'tcx, FrameData<'tcx>> {
356 let stacked_borrows = ecx.memory.extra.stacked_borrows.as_ref();
357 let call_id = stacked_borrows.map_or(NonZeroU64::new(1).unwrap(), |stacked_borrows| {
358 stacked_borrows.borrow_mut().new_call()
360 Ok(FrameData { call_id, catch_panic: None })
365 ecx: &mut InterpCx<'mir, 'tcx, Self>,
366 extra: FrameData<'tcx>,
368 ) -> InterpResult<'tcx, StackPopInfo> {
369 ecx.handle_stack_pop(extra, unwinding)
374 memory: &Memory<'mir, 'tcx, Self>,
376 ) -> InterpResult<'tcx, Pointer<Self::PointerTag>> {
377 intptrcast::GlobalState::int_to_ptr(int, memory)
382 memory: &Memory<'mir, 'tcx, Self>,
383 ptr: Pointer<Self::PointerTag>,
384 ) -> InterpResult<'tcx, u64> {
385 intptrcast::GlobalState::ptr_to_int(ptr, memory)
389 impl AllocationExtra<Tag> for AllocExtra {
391 fn memory_read<'tcx>(
392 alloc: &Allocation<Tag, AllocExtra>,
395 ) -> InterpResult<'tcx> {
396 if let Some(ref stacked_borrows) = alloc.extra.stacked_borrows {
397 stacked_borrows.memory_read(ptr, size)
404 fn memory_written<'tcx>(
405 alloc: &mut Allocation<Tag, AllocExtra>,
408 ) -> InterpResult<'tcx> {
409 if let Some(ref mut stacked_borrows) = alloc.extra.stacked_borrows {
410 stacked_borrows.memory_written(ptr, size)
417 fn memory_deallocated<'tcx>(
418 alloc: &mut Allocation<Tag, AllocExtra>,
421 ) -> InterpResult<'tcx> {
422 if let Some(ref mut stacked_borrows) = alloc.extra.stacked_borrows {
423 stacked_borrows.memory_deallocated(ptr, size)
430 impl MayLeak for MiriMemoryKind {
432 fn may_leak(self) -> bool {
433 use self::MiriMemoryKind::*;
435 Rust | C | WinHeap => false,
436 Machine | Static => true,