4 inclusive_range_fields,
5 inclusive_range_methods,
8 #![cfg_attr(feature = "cargo-clippy", allow(cast_lossless))]
16 extern crate rustc_data_structures;
17 extern crate rustc_mir;
18 extern crate rustc_target;
22 extern crate lazy_static;
24 use rustc::ty::{self, TyCtxt};
25 use rustc::ty::layout::{TyLayout, LayoutOf, Size};
26 use rustc::ty::subst::Subst;
27 use rustc::hir::def_id::DefId;
30 use syntax::ast::Mutability;
31 use syntax::codemap::Span;
33 use std::collections::{HashMap, BTreeMap};
35 pub use rustc::mir::interpret::*;
36 pub use rustc_mir::interpret::*;
48 use fn_call::EvalContextExt as MissingFnsEvalContextExt;
49 use operator::EvalContextExt as OperatorEvalContextExt;
50 use intrinsic::EvalContextExt as IntrinsicEvalContextExt;
51 use tls::EvalContextExt as TlsEvalContextExt;
53 use locks::MemoryExt as LockMemoryExt;
54 use validation::EvalContextExt as ValidationEvalContextExt;
55 use range_map::RangeMap;
56 use validation::{ValidationQuery, AbsPlace};
60 fn from_i32(i: i32) -> Self;
61 fn from_u128(i: u128) -> Self;
62 fn from_i128(i: i128) -> Self;
63 fn from_usize(i: u64, ptr_size: Size) -> Self;
64 fn from_isize(i: i64, ptr_size: Size) -> Self;
65 fn from_f32(f: f32) -> Self;
66 fn from_f64(f: f64) -> Self;
67 fn to_usize<'a, 'mir, 'tcx>(self, ecx: &rustc_mir::interpret::EvalContext<'a, 'mir, 'tcx, Evaluator<'tcx>>) -> EvalResult<'static, u64>;
68 fn is_null(self) -> EvalResult<'static, bool>;
69 /// HACK: this function just extracts all bits if `defined != 0`
70 /// Mainly used for args of C-functions and we should totally correctly fetch the size
71 /// of their arguments
72 fn to_bytes(self) -> EvalResult<'static, u128>;
75 impl ScalarExt for Scalar {
77 Scalar::Bits { bits: 0, defined: 128 }
80 fn from_i32(i: i32) -> Self {
81 Scalar::Bits { bits: i as u32 as u128, defined: 32 }
84 fn from_u128(i: u128) -> Self {
85 Scalar::Bits { bits: i, defined: 128 }
88 fn from_i128(i: i128) -> Self {
89 Scalar::Bits { bits: i as u128, defined: 128 }
92 fn from_usize(i: u64, ptr_size: Size) -> Self {
93 Scalar::Bits { bits: i as u128, defined: ptr_size.bits() as u8 }
96 fn from_isize(i: i64, ptr_size: Size) -> Self {
97 Scalar::Bits { bits: i as i128 as u128, defined: ptr_size.bits() as u8 }
100 fn from_f32(f: f32) -> Self {
101 Scalar::Bits { bits: f.to_bits() as u128, defined: 32 }
104 fn from_f64(f: f64) -> Self {
105 Scalar::Bits { bits: f.to_bits() as u128, defined: 64 }
108 fn to_usize<'a, 'mir, 'tcx>(self, ecx: &rustc_mir::interpret::EvalContext<'a, 'mir, 'tcx, Evaluator<'tcx>>) -> EvalResult<'static, u64> {
109 let b = self.to_bits(ecx.memory.pointer_size())?;
110 assert_eq!(b as u64 as u128, b);
114 fn is_null(self) -> EvalResult<'static, bool> {
116 Scalar::Bits { bits, defined } => {
123 Scalar::Ptr(_) => Ok(false)
127 fn to_bytes(self) -> EvalResult<'static, u128> {
129 Scalar::Bits { defined: 0, .. } => err!(ReadUndefBytes),
130 Scalar::Bits { bits, .. } => Ok(bits),
131 Scalar::Ptr(_) => err!(ReadPointerAsBytes),
136 pub fn create_ecx<'a, 'mir: 'a, 'tcx: 'mir>(
137 tcx: TyCtxt<'a, 'tcx, 'tcx>,
139 start_wrapper: Option<DefId>,
140 ) -> EvalResult<'tcx, (EvalContext<'a, 'mir, 'tcx, Evaluator<'tcx>>, Option<Pointer>)> {
141 let mut ecx = EvalContext::new(tcx.at(syntax::codemap::DUMMY_SP), ty::ParamEnv::reveal_all(), Default::default(), Default::default());
143 let main_instance = ty::Instance::mono(ecx.tcx.tcx, main_id);
144 let main_mir = ecx.load_mir(main_instance.def)?;
145 let mut cleanup_ptr = None; // Scalar to be deallocated when we are done
147 if !main_mir.return_ty().is_nil() || main_mir.arg_count != 0 {
148 return err!(Unimplemented(
149 "miri does not support main functions without `fn()` type signatures"
154 if let Some(start_id) = start_wrapper {
155 let main_ret_ty = ecx.tcx.fn_sig(main_id).output();
156 let main_ret_ty = main_ret_ty.no_late_bound_regions().unwrap();
157 let start_instance = ty::Instance::resolve(
159 ty::ParamEnv::reveal_all(),
162 ::std::iter::once(ty::subst::Kind::from(main_ret_ty)))
164 let start_mir = ecx.load_mir(start_instance.def)?;
166 if start_mir.arg_count != 3 {
167 return err!(AbiViolation(format!(
168 "'start' lang item should have three arguments, but has {}",
174 let size = ecx.tcx.data_layout.pointer_size;
175 let align = ecx.tcx.data_layout.pointer_align;
176 let ret_ptr = ecx.memory_mut().allocate(size, align, MemoryKind::Stack)?;
177 cleanup_ptr = Some(ret_ptr);
179 // Push our stack frame
180 ecx.push_stack_frame(
184 Place::from_ptr(ret_ptr, align),
185 StackPopCleanup::None,
188 let mut args = ecx.frame().mir.args_iter();
190 // First argument: pointer to main()
191 let main_ptr = ecx.memory_mut().create_fn_alloc(main_instance);
192 let dest = ecx.eval_place(&mir::Place::Local(args.next().unwrap()))?;
193 let main_ty = main_instance.ty(ecx.tcx.tcx);
194 let main_ptr_ty = ecx.tcx.mk_fn_ptr(main_ty.fn_sig(ecx.tcx.tcx));
197 value: Value::Scalar(Scalar::Ptr(main_ptr)),
203 // Second argument (argc): 1
204 let dest = ecx.eval_place(&mir::Place::Local(args.next().unwrap()))?;
205 let ty = ecx.tcx.types.isize;
206 ecx.write_scalar(dest, Scalar::from_u128(1), ty)?;
208 // FIXME: extract main source file path
209 // Third argument (argv): &[b"foo"]
210 let dest = ecx.eval_place(&mir::Place::Local(args.next().unwrap()))?;
211 let ty = ecx.tcx.mk_imm_ptr(ecx.tcx.mk_imm_ptr(ecx.tcx.types.u8));
212 let foo = ecx.memory.allocate_bytes(b"foo\0");
213 let ptr_size = ecx.memory.pointer_size();
214 let ptr_align = ecx.tcx.data_layout.pointer_align;
215 let foo_ptr = ecx.memory.allocate(ptr_size, ptr_align, MemoryKind::Stack)?;
216 ecx.memory.write_scalar(foo_ptr.into(), ptr_align, Scalar::Ptr(foo), ptr_size, false)?;
217 ecx.memory.mark_static_initialized(foo_ptr.alloc_id, Mutability::Immutable)?;
218 ecx.write_ptr(dest, foo_ptr.into(), ty)?;
220 assert!(args.next().is_none(), "start lang item has more arguments than expected");
222 ecx.push_stack_frame(
226 Place::from_scalar_ptr(Scalar::from_u128(1), ty::layout::Align::from_bytes(1, 1).unwrap()),
227 StackPopCleanup::None,
231 let mut args = ecx.frame().mir.args_iter();
232 assert!(args.next().is_none(), "main function must not have arguments");
235 Ok((ecx, cleanup_ptr))
238 pub fn eval_main<'a, 'tcx: 'a>(
239 tcx: TyCtxt<'a, 'tcx, 'tcx>,
241 start_wrapper: Option<DefId>,
243 let (mut ecx, cleanup_ptr) = create_ecx(tcx, main_id, start_wrapper).expect("Couldn't create ecx");
245 let res: EvalResult = do catch {
247 ecx.run_tls_dtors()?;
248 if let Some(cleanup_ptr) = cleanup_ptr {
249 ecx.memory_mut().deallocate(
259 let leaks = ecx.memory().leak_report();
261 // TODO: Prevent leaks which aren't supposed to be there
262 //tcx.sess.err("the evaluated program leaked memory");
266 if let Some(frame) = ecx.stack().last() {
267 let block = &frame.mir.basic_blocks()[frame.block];
268 let span = if frame.stmt < block.statements.len() {
269 block.statements[frame.stmt].source_info.span
271 block.terminator().source_info.span
274 let mut err = struct_error(ecx.tcx.tcx.at(span), "constant evaluation error");
275 let (frames, span) = ecx.generate_stacktrace(None);
276 err.span_label(span, e.to_string());
277 for FrameInfo { span, location, .. } in frames {
278 err.span_note(span, &format!("inside call to `{}`", location));
282 ecx.tcx.sess.err(&e.to_string());
285 for (i, frame) in ecx.stack().iter().enumerate() {
286 trace!("-------------------");
287 trace!("Frame {}", i);
288 trace!(" return: {:#?}", frame.return_place);
289 for (i, local) in frame.locals.iter().enumerate() {
290 if let Some(local) = local {
291 trace!(" local {}: {:?}", i, local);
300 pub struct Evaluator<'tcx> {
301 /// Environment variables set by `setenv`
302 /// Miri does not expose env vars from the host to the emulated program
303 pub(crate) env_vars: HashMap<Vec<u8>, Pointer>,
305 /// Places that were suspended by the validation subsystem, and will be recovered later
306 pub(crate) suspended: HashMap<DynamicLifetime, Vec<ValidationQuery<'tcx>>>,
309 pub type TlsKey = u128;
311 #[derive(Copy, Clone, Debug)]
312 pub struct TlsEntry<'tcx> {
313 data: Scalar, // Will eventually become a map from thread IDs to `Scalar`s, if we ever support more than one thread.
314 dtor: Option<ty::Instance<'tcx>>,
318 pub struct MemoryData<'tcx> {
319 /// The Key to use for the next thread-local allocation.
320 next_thread_local: TlsKey,
322 /// pthreads-style thread-local storage.
323 thread_local: BTreeMap<TlsKey, TlsEntry<'tcx>>,
325 /// Memory regions that are locked by some function
327 /// Only mutable (static mut, heap, stack) allocations have an entry in this map.
328 /// The entry is created when allocating the memory and deleted after deallocation.
329 locks: HashMap<AllocId, RangeMap<LockInfo<'tcx>>>,
331 statics: HashMap<GlobalId<'tcx>, AllocId>,
334 impl<'mir, 'tcx: 'mir> Machine<'mir, 'tcx> for Evaluator<'tcx> {
335 type MemoryData = MemoryData<'tcx>;
336 type MemoryKinds = memory::MemoryKind;
338 /// Returns Ok() when the function was handled, fail otherwise
340 ecx: &mut EvalContext<'a, 'mir, 'tcx, Self>,
341 instance: ty::Instance<'tcx>,
342 destination: Option<(Place, mir::BasicBlock)>,
343 args: &[ValTy<'tcx>],
345 sig: ty::FnSig<'tcx>,
346 ) -> EvalResult<'tcx, bool> {
347 ecx.eval_fn_call(instance, destination, args, span, sig)
350 fn call_intrinsic<'a>(
351 ecx: &mut rustc_mir::interpret::EvalContext<'a, 'mir, 'tcx, Self>,
352 instance: ty::Instance<'tcx>,
353 args: &[ValTy<'tcx>],
355 dest_layout: TyLayout<'tcx>,
356 target: mir::BasicBlock,
357 ) -> EvalResult<'tcx> {
358 ecx.call_intrinsic(instance, args, dest, dest_layout, target)
362 ecx: &rustc_mir::interpret::EvalContext<'a, 'mir, 'tcx, Self>,
365 left_ty: ty::Ty<'tcx>,
367 right_ty: ty::Ty<'tcx>,
368 ) -> EvalResult<'tcx, Option<(Scalar, bool)>> {
369 ecx.ptr_op(bin_op, left, left_ty, right, right_ty)
372 fn mark_static_initialized<'a>(
373 mem: &mut Memory<'a, 'mir, 'tcx, Self>,
375 _mutability: Mutability,
376 ) -> EvalResult<'tcx, bool> {
377 use memory::MemoryKind::*;
378 match mem.get_alloc_kind(id) {
379 // FIXME: This could be allowed, but not for env vars set during miri execution
380 Some(MemoryKind::Machine(Env)) => err!(Unimplemented("statics can't refer to env vars".to_owned())),
381 _ => Ok(false), // TODO: What does the bool mean?
386 ecx: &mut EvalContext<'a, 'mir, 'tcx, Self>,
388 ) -> EvalResult<'tcx, AllocId> {
389 // Step 1: If the static has already been evaluated return the cached version
390 if let Some(alloc_id) = ecx.memory.data.statics.get(&cid) {
391 return Ok(*alloc_id);
394 let tcx = ecx.tcx.tcx;
397 let mut mir = ecx.load_mir(cid.instance.def)?;
398 if let Some(index) = cid.promoted {
399 mir = &mir.promoted[index];
401 assert!(mir.arg_count == 0);
403 // Step 3: Allocate storage
404 let layout = ecx.layout_of(mir.return_ty().subst(tcx, cid.instance.substs))?;
405 assert!(!layout.is_unsized());
406 let ptr = ecx.memory.allocate(
412 // Step 4: Cache allocation id for recursive statics
413 assert!(ecx.memory.data.statics.insert(cid, ptr.alloc_id).is_none());
415 // Step 5: Push stackframe to evaluate static
416 let cleanup = StackPopCleanup::None;
417 ecx.push_stack_frame(
421 Place::from_ptr(ptr, layout.align),
425 // Step 6: Step until static has been initialized
426 let call_stackframe = ecx.stack().len();
427 while ecx.step()? && ecx.stack().len() >= call_stackframe {
428 if ecx.stack().len() == call_stackframe {
429 let frame = ecx.frame_mut();
430 let bb = &frame.mir.basic_blocks()[frame.block];
431 if bb.statements.len() == frame.stmt && !bb.is_cleanup {
432 if let ::rustc::mir::TerminatorKind::Return = bb.terminator().kind {
433 for (local, _local_decl) in mir.local_decls.iter_enumerated().skip(1) {
434 // Don't deallocate locals, because the return value might reference them
435 frame.storage_dead(local);
442 // TODO: Freeze immutable statics without copying them to the global static cache
444 // Step 7: Return the alloc
449 ecx: &mut EvalContext<'a, 'mir, 'tcx, Self>,
452 ) -> EvalResult<'tcx> {
453 let layout = ecx.layout_of(ty)?;
455 // Call the `exchange_malloc` lang item
456 let malloc = ecx.tcx.lang_items().exchange_malloc_fn().unwrap();
457 let malloc = ty::Instance::mono(ecx.tcx.tcx, malloc);
458 let malloc_mir = ecx.load_mir(malloc.def)?;
459 ecx.push_stack_frame(
464 // Don't do anything when we are done. The statement() function will increment
465 // the old stack frame's stmt counter to the next statement, which means that when
466 // exchange_malloc returns, we go on evaluating exactly where we want to be.
467 StackPopCleanup::None,
470 let mut args = ecx.frame().mir.args_iter();
471 let usize = ecx.tcx.types.usize;
473 // First argument: size
474 let dest = ecx.eval_place(&mir::Place::Local(args.next().unwrap()))?;
477 value: Value::Scalar(Scalar::from_u128(match layout.size.bytes() {
479 size => size as u128,
486 // Second argument: align
487 let dest = ecx.eval_place(&mir::Place::Local(args.next().unwrap()))?;
490 value: Value::Scalar(Scalar::from_u128(layout.align.abi().into())),
497 assert!(args.next().is_none(), "exchange_malloc lang item has more arguments than expected");
501 fn global_item_with_linkage<'a>(
502 _ecx: &mut EvalContext<'a, 'mir, 'tcx, Self>,
503 _instance: ty::Instance<'tcx>,
504 _mutability: Mutability,
505 ) -> EvalResult<'tcx> {
506 panic!("remove this function from rustc");
510 mem: &Memory<'a, 'mir, 'tcx, Self>,
514 ) -> EvalResult<'tcx> {
515 mem.check_locks(ptr, size.bytes(), access)
519 mem: &mut Memory<'a, 'mir, 'tcx, Self>,
522 mem.data.locks.insert(id, RangeMap::new());
526 mem: &mut Memory<'a, 'mir, 'tcx, Self>,
529 ) -> EvalResult<'tcx> {
532 .expect("allocation has no corresponding locks")
540 EvalErrorKind::DeallocatedLockedMemory {
543 alloc_id: AllocId(0),
544 offset: Size::from_bytes(0),
552 ecx: &mut EvalContext<'a, 'mir, 'tcx, Self>,
553 reg: Option<::rustc::middle::region::Scope>,
554 ) -> EvalResult<'tcx> {
558 fn validation_op<'a>(
559 _ecx: &mut EvalContext<'a, 'mir, 'tcx, Self>,
560 _op: ::rustc::mir::ValidationOp,
561 _operand: &::rustc::mir::ValidationOperand<'tcx, ::rustc::mir::Place<'tcx>>,
562 ) -> EvalResult<'tcx> {
563 // FIXME: prevent this from ICEing
564 //ecx.validation_op(op, operand)