]> git.lizzy.rs Git - rust.git/blob - src/machine.rs
rename memory kind: Env -> Machine
[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
9 use rand::rngs::StdRng;
10
11 use rustc::mir;
12 use rustc::ty::{
13     self,
14     layout::{LayoutOf, Size},
15     Ty, TyCtxt,
16 };
17 use rustc_ast::attr;
18 use rustc_hir::def_id::DefId;
19 use rustc_span::{source_map::Span, symbol::sym};
20
21 use crate::*;
22
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;
28
29 /// Extra data stored with each stack frame
30 #[derive(Debug)]
31 pub struct FrameData<'tcx> {
32     /// Extra data for Stacked Borrows.
33     pub call_id: stacked_borrows::CallId,
34
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>>,
40 }
41
42 /// Extra memory kinds
43 #[derive(Debug, Copy, Clone, PartialEq, Eq)]
44 pub enum MiriMemoryKind {
45     /// `__rust_alloc` memory.
46     Rust,
47     /// `malloc` memory.
48     C,
49     /// Windows `HeapAlloc` memory.
50     WinHeap,
51     /// Memory for env vars and args, errno, extern statics and other parts of the machine-managed environment.
52     Machine,
53     /// Rust statics.
54     Static,
55 }
56
57 impl Into<MemoryKind<MiriMemoryKind>> for MiriMemoryKind {
58     #[inline(always)]
59     fn into(self) -> MemoryKind<MiriMemoryKind> {
60         MemoryKind::Machine(self)
61     }
62 }
63
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>,
69 }
70
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,
76
77     /// The random number generator used for resolving non-determinism.
78     pub(crate) rng: RefCell<StdRng>,
79 }
80
81 impl MemoryExtra {
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))))
85         } else {
86             None
87         };
88         MemoryExtra { stacked_borrows, intptrcast: Default::default(), rng: RefCell::new(rng) }
89     }
90 }
91
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,
97
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>>,
104
105     /// Last OS error location in memory. It is a 32-bit integer.
106     pub(crate) last_error: Option<MPlaceTy<'tcx, Tag>>,
107
108     /// TLS state.
109     pub(crate) tls: TlsData<'tcx>,
110
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,
114
115     /// Whether to enforce the validity invariant.
116     pub(crate) validate: bool,
117
118     pub(crate) file_handler: FileHandler,
119     pub(crate) dir_handler: DirHandler,
120
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>>,
124 }
125
126 impl<'tcx> Evaluator<'tcx> {
127     pub(crate) fn new(communicate: bool, validate: bool) -> Self {
128         Evaluator {
129             // `env_vars` could be initialized properly here if `Memory` were available before
130             // calling this method.
131             env_vars: EnvVars::default(),
132             argc: None,
133             argv: None,
134             cmd_line: None,
135             last_error: None,
136             tls: TlsData::default(),
137             communicate,
138             validate,
139             file_handler: Default::default(),
140             dir_handler: Default::default(),
141             panic_payload: None,
142         }
143     }
144 }
145
146 /// A rustc InterpCx for Miri.
147 pub type MiriEvalContext<'mir, 'tcx> = InterpCx<'mir, 'tcx, Evaluator<'tcx>>;
148
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>;
153 }
154 impl<'mir, 'tcx> MiriEvalContextExt<'mir, 'tcx> for MiriEvalContext<'mir, 'tcx> {
155     #[inline(always)]
156     fn eval_context_ref(&self) -> &MiriEvalContext<'mir, 'tcx> {
157         self
158     }
159     #[inline(always)]
160     fn eval_context_mut(&mut self) -> &mut MiriEvalContext<'mir, 'tcx> {
161         self
162     }
163 }
164
165 /// Machine hook implementations.
166 impl<'mir, 'tcx> Machine<'mir, 'tcx> for Evaluator<'tcx> {
167     type MemoryKinds = MiriMemoryKind;
168
169     type FrameExtra = FrameData<'tcx>;
170     type MemoryExtra = MemoryExtra;
171     type AllocExtra = AllocExtra;
172     type PointerTag = Tag;
173     type ExtraFnVal = Dlsym;
174
175     type MemoryMap =
176         MonoHashMap<AllocId, (MemoryKind<MiriMemoryKind>, Allocation<Tag, Self::AllocExtra>)>;
177
178     const STATIC_KIND: Option<MiriMemoryKind> = Some(MiriMemoryKind::Static);
179
180     const CHECK_ALIGN: bool = true;
181
182     #[inline(always)]
183     fn enforce_validity(ecx: &InterpCx<'mir, 'tcx, Self>) -> bool {
184         ecx.machine.validate
185     }
186
187     #[inline(always)]
188     fn find_mir_or_eval_fn(
189         ecx: &mut InterpCx<'mir, 'tcx, Self>,
190         _span: Span,
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)
197     }
198
199     #[inline(always)]
200     fn call_extra_fn(
201         ecx: &mut InterpCx<'mir, 'tcx, Self>,
202         fn_val: Dlsym,
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)
208     }
209
210     #[inline(always)]
211     fn call_intrinsic(
212         ecx: &mut rustc_mir::interpret::InterpCx<'mir, 'tcx, Self>,
213         span: Span,
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)
220     }
221
222     #[inline(always)]
223     fn assert_panic(
224         ecx: &mut InterpCx<'mir, 'tcx, Self>,
225         span: Span,
226         msg: &mir::AssertMessage<'tcx>,
227         unwind: Option<mir::BasicBlock>,
228     ) -> InterpResult<'tcx> {
229         ecx.assert_panic(span, msg, unwind)
230     }
231
232     #[inline(always)]
233     fn binary_ptr_op(
234         ecx: &rustc_mir::interpret::InterpCx<'mir, 'tcx, Self>,
235         bin_op: mir::BinOp,
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)
240     }
241
242     fn box_alloc(
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());
251
252         // Second argument: `align`.
253         let align = Scalar::from_uint(layout.align.abi.bytes(), ecx.pointer_size());
254
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);
258         ecx.call_function(
259             malloc,
260             &[size.into(), align.into()],
261             Some(dest),
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 },
266         )?;
267         Ok(())
268     }
269
270     fn find_foreign_static(
271         tcx: TyCtxt<'tcx>,
272         def_id: DefId,
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(),
278         };
279
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)
286             }
287             _ => throw_unsup_format!("can't access foreign static: {}", link_name),
288         };
289         Ok(Cow::Owned(alloc))
290     }
291
292     #[inline(always)]
293     fn before_terminator(_ecx: &mut InterpCx<'mir, 'tcx, Self>) -> InterpResult<'tcx> {
294         // We are not interested in detecting loops.
295         Ok(())
296     }
297
298     fn init_allocation_extra<'b>(
299         memory_extra: &MemoryExtra,
300         id: AllocId,
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)
311             } else {
312                 // No stacks, no tag.
313                 (None, Tag::Untagged)
314             };
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(
317             |alloc| {
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)
322                 } else {
323                     Tag::Untagged
324                 }
325             },
326             AllocExtra { stacked_borrows: stacks },
327         );
328         (Cow::Owned(alloc), base_tag)
329     }
330
331     #[inline(always)]
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)
335         } else {
336             Tag::Untagged
337         }
338     }
339
340     #[inline(always)]
341     fn retag(
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() {
347             // No tracking.
348             Ok(())
349         } else {
350             ecx.retag(kind, place)
351         }
352     }
353
354     #[inline(always)]
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()
359         });
360         Ok(FrameData { call_id, catch_panic: None })
361     }
362
363     #[inline(always)]
364     fn stack_pop(
365         ecx: &mut InterpCx<'mir, 'tcx, Self>,
366         extra: FrameData<'tcx>,
367         unwinding: bool,
368     ) -> InterpResult<'tcx, StackPopInfo> {
369         ecx.handle_stack_pop(extra, unwinding)
370     }
371
372     #[inline(always)]
373     fn int_to_ptr(
374         memory: &Memory<'mir, 'tcx, Self>,
375         int: u64,
376     ) -> InterpResult<'tcx, Pointer<Self::PointerTag>> {
377         intptrcast::GlobalState::int_to_ptr(int, memory)
378     }
379
380     #[inline(always)]
381     fn ptr_to_int(
382         memory: &Memory<'mir, 'tcx, Self>,
383         ptr: Pointer<Self::PointerTag>,
384     ) -> InterpResult<'tcx, u64> {
385         intptrcast::GlobalState::ptr_to_int(ptr, memory)
386     }
387 }
388
389 impl AllocationExtra<Tag> for AllocExtra {
390     #[inline(always)]
391     fn memory_read<'tcx>(
392         alloc: &Allocation<Tag, AllocExtra>,
393         ptr: Pointer<Tag>,
394         size: Size,
395     ) -> InterpResult<'tcx> {
396         if let Some(ref stacked_borrows) = alloc.extra.stacked_borrows {
397             stacked_borrows.memory_read(ptr, size)
398         } else {
399             Ok(())
400         }
401     }
402
403     #[inline(always)]
404     fn memory_written<'tcx>(
405         alloc: &mut Allocation<Tag, AllocExtra>,
406         ptr: Pointer<Tag>,
407         size: Size,
408     ) -> InterpResult<'tcx> {
409         if let Some(ref mut stacked_borrows) = alloc.extra.stacked_borrows {
410             stacked_borrows.memory_written(ptr, size)
411         } else {
412             Ok(())
413         }
414     }
415
416     #[inline(always)]
417     fn memory_deallocated<'tcx>(
418         alloc: &mut Allocation<Tag, AllocExtra>,
419         ptr: Pointer<Tag>,
420         size: Size,
421     ) -> InterpResult<'tcx> {
422         if let Some(ref mut stacked_borrows) = alloc.extra.stacked_borrows {
423             stacked_borrows.memory_deallocated(ptr, size)
424         } else {
425             Ok(())
426         }
427     }
428 }
429
430 impl MayLeak for MiriMemoryKind {
431     #[inline(always)]
432     fn may_leak(self) -> bool {
433         use self::MiriMemoryKind::*;
434         match self {
435             Rust | C | WinHeap => false,
436             Machine | Static => true,
437         }
438     }
439 }