1 #![feature(rustc_private)]
3 #![allow(clippy::cast_lossless)]
9 extern crate rustc_apfloat;
10 #[macro_use] extern crate rustc;
11 extern crate rustc_data_structures;
12 extern crate rustc_mir;
13 extern crate rustc_target;
26 use std::collections::HashMap;
30 use rand::rngs::StdRng;
31 use rand::SeedableRng;
33 use rustc::ty::{self, TyCtxt, query::TyCtxtAt};
34 use rustc::ty::layout::{LayoutOf, Size, Align};
35 use rustc::hir::def_id::DefId;
37 pub use rustc_mir::interpret::*;
39 pub use rustc_mir::interpret::{self, AllocMap, PlaceTy};
41 use syntax::source_map::DUMMY_SP;
42 use syntax::symbol::sym;
44 pub use crate::fn_call::EvalContextExt as MissingFnsEvalContextExt;
45 pub use crate::operator::EvalContextExt as OperatorEvalContextExt;
46 pub use crate::intrinsic::EvalContextExt as IntrinsicEvalContextExt;
47 pub use crate::tls::{EvalContextExt as TlsEvalContextExt, TlsData};
48 use crate::range_map::RangeMap;
49 #[allow(unused_imports)] // FIXME: rustc bug, issue <https://github.com/rust-lang/rust/issues/53682>.
50 pub use crate::helpers::{EvalContextExt as HelpersEvalContextExt};
51 use crate::mono_hash_map::MonoHashMap;
52 pub use crate::stacked_borrows::{EvalContextExt as StackedBorEvalContextExt};
53 use crate::memory::AllocExtra;
56 pub use crate::stacked_borrows::{Tag, Permission, Stack, Stacks, Item};
58 /// Insert rustc arguments at the beginning of the argument list that Miri wants to be
59 /// set per default, for maximal validation power.
60 pub fn miri_default_args() -> &'static [&'static str] {
61 // The flags here should be kept in sync with what bootstrap adds when `test-miri` is
62 // set, which happens in `bootstrap/bin/rustc.rs` in the rustc sources.
63 &["-Zalways-encode-mir", "-Zmir-emit-retag", "-Zmir-opt-level=0", "--cfg=miri"]
66 /// Configuration needed to spawn a Miri instance.
68 pub struct MiriConfig {
70 pub args: Vec<String>,
72 // The seed to use when non-determinism is required (e.g. getrandom())
77 pub fn create_ecx<'mir, 'tcx: 'mir>(
81 ) -> InterpResult<'tcx, InterpretCx<'mir, 'tcx, Evaluator<'tcx>>> {
82 let mut ecx = InterpretCx::new(
83 tcx.at(syntax::source_map::DUMMY_SP),
84 ty::ParamEnv::reveal_all(),
85 Evaluator::new(config.validate),
88 // FIXME: InterpretCx::new should take an initial MemoryExtra
89 ecx.memory_mut().extra.rng = config.seed.map(StdRng::seed_from_u64);
91 let main_instance = ty::Instance::mono(ecx.tcx.tcx, main_id);
92 let main_mir = ecx.load_mir(main_instance.def)?;
94 if !main_mir.return_ty().is_unit() || main_mir.arg_count != 0 {
95 return err!(Unimplemented(
96 "miri does not support main functions without `fn()` type signatures"
101 let start_id = tcx.lang_items().start_fn().unwrap();
102 let main_ret_ty = tcx.fn_sig(main_id).output();
103 let main_ret_ty = main_ret_ty.no_bound_vars().unwrap();
104 let start_instance = ty::Instance::resolve(
106 ty::ParamEnv::reveal_all(),
109 ::std::iter::once(ty::subst::Kind::from(main_ret_ty)))
111 let start_mir = ecx.load_mir(start_instance.def)?;
113 if start_mir.arg_count != 3 {
114 return err!(AbiViolation(format!(
115 "'start' lang item should have three arguments, but has {}",
120 // Return value (in static memory so that it does not count as leak).
121 let ret = ecx.layout_of(start_mir.return_ty())?;
122 let ret_ptr = ecx.allocate(ret, MiriMemoryKind::Static.into());
124 // Push our stack frame.
125 ecx.push_stack_frame(
127 // There is no call site.
130 Some(ret_ptr.into()),
131 StackPopCleanup::None { cleanup: true },
134 let mut args = ecx.frame().body.args_iter();
136 // First argument: pointer to `main()`.
137 let main_ptr = ecx.memory_mut().create_fn_alloc(main_instance);
138 let dest = ecx.eval_place(&mir::Place::Base(mir::PlaceBase::Local(args.next().unwrap())))?;
139 ecx.write_scalar(Scalar::Ptr(main_ptr), dest)?;
141 // Second argument (argc): `1`.
142 let dest = ecx.eval_place(&mir::Place::Base(mir::PlaceBase::Local(args.next().unwrap())))?;
143 let argc = Scalar::from_uint(config.args.len() as u128, dest.layout.size);
144 ecx.write_scalar(argc, dest)?;
145 // Store argc for macOS's `_NSGetArgc`.
147 let argc_place = ecx.allocate(dest.layout, MiriMemoryKind::Env.into());
148 ecx.write_scalar(argc, argc_place.into())?;
149 ecx.machine.argc = Some(argc_place.ptr.to_ptr()?);
152 // FIXME: extract main source file path.
153 // Third argument (`argv`): created from `config.args`.
154 let dest = ecx.eval_place(&mir::Place::Base(mir::PlaceBase::Local(args.next().unwrap())))?;
155 // For Windows, construct a command string with all the aguments.
156 let mut cmd = String::new();
157 for arg in config.args.iter() {
161 cmd.push_str(&*shell_escape::windows::escape(arg.as_str().into()));
163 // Don't forget `0` terminator.
164 cmd.push(std::char::from_u32(0).unwrap());
165 // Collect the pointers to the individual strings.
166 let mut argvs = Vec::<Pointer<Tag>>::new();
167 for arg in config.args {
168 // Add `0` terminator.
169 let mut arg = arg.into_bytes();
171 argvs.push(ecx.memory_mut().allocate_static_bytes(arg.as_slice(), MiriMemoryKind::Static.into()));
173 // Make an array with all these pointers, in the Miri memory.
174 let argvs_layout = ecx.layout_of(ecx.tcx.mk_array(ecx.tcx.mk_imm_ptr(ecx.tcx.types.u8), argvs.len() as u64))?;
175 let argvs_place = ecx.allocate(argvs_layout, MiriMemoryKind::Env.into());
176 for (idx, arg) in argvs.into_iter().enumerate() {
177 let place = ecx.mplace_field(argvs_place, idx as u64)?;
178 ecx.write_scalar(Scalar::Ptr(arg), place.into())?;
180 ecx.memory_mut().mark_immutable(argvs_place.to_ptr()?.alloc_id)?;
181 // Write a pointer to that place as the argument.
182 let argv = argvs_place.ptr;
183 ecx.write_scalar(argv, dest)?;
184 // Store `argv` for macOS `_NSGetArgv`.
186 let argv_place = ecx.allocate(dest.layout, MiriMemoryKind::Env.into());
187 ecx.write_scalar(argv, argv_place.into())?;
188 ecx.machine.argv = Some(argv_place.ptr.to_ptr()?);
190 // Store command line as UTF-16 for Windows `GetCommandLineW`.
192 let tcx = &{ecx.tcx.tcx};
193 let cmd_utf16: Vec<u16> = cmd.encode_utf16().collect();
194 let cmd_ptr = ecx.memory_mut().allocate(
195 Size::from_bytes(cmd_utf16.len() as u64 * 2),
196 Align::from_bytes(2).unwrap(),
197 MiriMemoryKind::Env.into(),
199 ecx.machine.cmd_line = Some(cmd_ptr);
200 // Store the UTF-16 string.
201 let char_size = Size::from_bytes(2);
202 let cmd_alloc = ecx.memory_mut().get_mut(cmd_ptr.alloc_id)?;
203 let mut cur_ptr = cmd_ptr;
204 for &c in cmd_utf16.iter() {
205 cmd_alloc.write_scalar(
208 Scalar::from_uint(c, char_size).into(),
211 cur_ptr = cur_ptr.offset(char_size, tcx)?;
215 assert!(args.next().is_none(), "start lang item has more arguments than expected");
220 pub fn eval_main<'tcx>(
225 let mut ecx = match create_ecx(tcx, main_id, config) {
228 err.print_backtrace();
229 panic!("Miri initialziation error: {}", err.kind)
233 // Perform the main execution.
234 let res: InterpResult = (|| {
239 // Process the result.
242 let leaks = ecx.memory().leak_report();
243 // Disable the leak test on some platforms where we do not
244 // correctly implement TLS destructors.
245 let target_os = ecx.tcx.tcx.sess.target.target.target_os.to_lowercase();
246 let ignore_leaks = target_os == "windows" || target_os == "macos";
247 if !ignore_leaks && leaks != 0 {
248 tcx.sess.err("the evaluated program leaked memory");
252 // Special treatment for some error kinds
253 let msg = match e.kind {
254 InterpError::Exit(code) => std::process::exit(code),
255 InterpError::NoMirFor(..) =>
256 format!("{}. Did you set `MIRI_SYSROOT` to a Miri-enabled sysroot? You can prepare one with `cargo miri setup`.", e),
260 if let Some(frame) = ecx.stack().last() {
261 let block = &frame.body.basic_blocks()[frame.block];
262 let span = if frame.stmt < block.statements.len() {
263 block.statements[frame.stmt].source_info.span
265 block.terminator().source_info.span
268 let msg = format!("Miri evaluation error: {}", msg);
269 let mut err = struct_error(ecx.tcx.tcx.at(span), msg.as_str());
270 let frames = ecx.generate_stacktrace(None);
271 err.span_label(span, msg);
272 // We iterate with indices because we need to look at the next frame (the caller).
273 for idx in 0..frames.len() {
274 let frame_info = &frames[idx];
275 let call_site_is_local = frames.get(idx+1).map_or(false,
276 |caller_info| caller_info.instance.def_id().is_local());
277 if call_site_is_local {
278 err.span_note(frame_info.call_site, &frame_info.to_string());
280 err.note(&frame_info.to_string());
285 ecx.tcx.sess.err(&msg);
288 for (i, frame) in ecx.stack().iter().enumerate() {
289 trace!("-------------------");
290 trace!("Frame {}", i);
291 trace!(" return: {:?}", frame.return_place.map(|p| *p));
292 for (i, local) in frame.locals.iter().enumerate() {
293 trace!(" local {}: {:?}", i, local.value);
300 #[derive(Debug, Copy, Clone, PartialEq, Eq)]
301 pub enum MiriMemoryKind {
302 /// `__rust_alloc` memory.
306 /// Part of env var emulation.
312 impl Into<MemoryKind<MiriMemoryKind>> for MiriMemoryKind {
314 fn into(self) -> MemoryKind<MiriMemoryKind> {
315 MemoryKind::Machine(self)
319 impl MayLeak for MiriMemoryKind {
321 fn may_leak(self) -> bool {
322 use self::MiriMemoryKind::*;
325 Env | Static => true,
330 pub struct Evaluator<'tcx> {
331 /// Environment variables set by `setenv`.
332 /// Miri does not expose env vars from the host to the emulated program.
333 pub(crate) env_vars: HashMap<Vec<u8>, Pointer<Tag>>,
335 /// Program arguments (`Option` because we can only initialize them after creating the ecx).
336 /// These are *pointers* to argc/argv because macOS.
337 /// We also need the full command line as one string because of Windows.
338 pub(crate) argc: Option<Pointer<Tag>>,
339 pub(crate) argv: Option<Pointer<Tag>>,
340 pub(crate) cmd_line: Option<Pointer<Tag>>,
343 pub(crate) last_error: u32,
346 pub(crate) tls: TlsData<'tcx>,
348 /// Whether to enforce the validity invariant.
349 pub(crate) validate: bool,
352 impl<'tcx> Evaluator<'tcx> {
353 fn new(validate: bool) -> Self {
355 env_vars: HashMap::default(),
360 tls: TlsData::default(),
366 // FIXME: rustc issue <https://github.com/rust-lang/rust/issues/47131>.
368 type MiriEvalContext<'mir, 'tcx> = InterpretCx<'mir, 'tcx, Evaluator<'tcx>>;
370 // A little trait that's useful to be inherited by extension traits.
371 pub trait MiriEvalContextExt<'mir, 'tcx> {
372 fn eval_context_ref(&self) -> &MiriEvalContext<'mir, 'tcx>;
373 fn eval_context_mut(&mut self) -> &mut MiriEvalContext<'mir, 'tcx>;
375 impl<'mir, 'tcx> MiriEvalContextExt<'mir, 'tcx> for MiriEvalContext<'mir, 'tcx> {
377 fn eval_context_ref(&self) -> &MiriEvalContext<'mir, 'tcx> {
381 fn eval_context_mut(&mut self) -> &mut MiriEvalContext<'mir, 'tcx> {
386 impl<'mir, 'tcx> Machine<'mir, 'tcx> for Evaluator<'tcx> {
387 type MemoryKinds = MiriMemoryKind;
389 type FrameExtra = stacked_borrows::CallId;
390 type MemoryExtra = memory::MemoryExtra;
391 type AllocExtra = memory::AllocExtra;
392 type PointerTag = Tag;
394 type MemoryMap = MonoHashMap<AllocId, (MemoryKind<MiriMemoryKind>, Allocation<Tag, Self::AllocExtra>)>;
396 const STATIC_KIND: Option<MiriMemoryKind> = Some(MiriMemoryKind::Static);
399 fn enforce_validity(ecx: &InterpretCx<'mir, 'tcx, Self>) -> bool {
403 /// Returns `Ok()` when the function was handled; fail otherwise.
406 ecx: &mut InterpretCx<'mir, 'tcx, Self>,
407 instance: ty::Instance<'tcx>,
408 args: &[OpTy<'tcx, Tag>],
409 dest: Option<PlaceTy<'tcx, Tag>>,
410 ret: Option<mir::BasicBlock>,
411 ) -> InterpResult<'tcx, Option<&'mir mir::Body<'tcx>>> {
412 ecx.find_fn(instance, args, dest, ret)
417 ecx: &mut rustc_mir::interpret::InterpretCx<'mir, 'tcx, Self>,
418 instance: ty::Instance<'tcx>,
419 args: &[OpTy<'tcx, Tag>],
420 dest: PlaceTy<'tcx, Tag>,
421 ) -> InterpResult<'tcx> {
422 ecx.call_intrinsic(instance, args, dest)
427 ecx: &rustc_mir::interpret::InterpretCx<'mir, 'tcx, Self>,
429 left: ImmTy<'tcx, Tag>,
430 right: ImmTy<'tcx, Tag>,
431 ) -> InterpResult<'tcx, (Scalar<Tag>, bool)> {
432 ecx.ptr_op(bin_op, left, right)
436 ecx: &mut InterpretCx<'mir, 'tcx, Self>,
437 dest: PlaceTy<'tcx, Tag>,
438 ) -> InterpResult<'tcx> {
439 trace!("box_alloc for {:?}", dest.layout.ty);
440 // Call the `exchange_malloc` lang item.
441 let malloc = ecx.tcx.lang_items().exchange_malloc_fn().unwrap();
442 let malloc = ty::Instance::mono(ecx.tcx.tcx, malloc);
443 let malloc_mir = ecx.load_mir(malloc.def)?;
444 ecx.push_stack_frame(
449 // Don't do anything when we are done. The `statement()` function will increment
450 // the old stack frame's stmt counter to the next statement, which means that when
451 // `exchange_malloc` returns, we go on evaluating exactly where we want to be.
452 StackPopCleanup::None { cleanup: true },
455 let mut args = ecx.frame().body.args_iter();
456 let layout = ecx.layout_of(dest.layout.ty.builtin_deref(false).unwrap().ty)?;
458 // First argument: `size`.
459 // (`0` is allowed here -- this is expected to be handled by the lang item).
460 let arg = ecx.eval_place(&mir::Place::Base(mir::PlaceBase::Local(args.next().unwrap())))?;
461 let size = layout.size.bytes();
462 ecx.write_scalar(Scalar::from_uint(size, arg.layout.size), arg)?;
464 // Second argument: `align`.
465 let arg = ecx.eval_place(&mir::Place::Base(mir::PlaceBase::Local(args.next().unwrap())))?;
466 let align = layout.align.abi.bytes();
467 ecx.write_scalar(Scalar::from_uint(align, arg.layout.size), arg)?;
469 // No more arguments.
471 args.next().is_none(),
472 "`exchange_malloc` lang item has more arguments than expected"
477 fn find_foreign_static(
480 ) -> InterpResult<'tcx, Cow<'tcx, Allocation>> {
481 let attrs = tcx.get_attrs(def_id);
482 let link_name = match attr::first_attr_value_str_by_name(&attrs, sym::link_name) {
483 Some(name) => name.as_str(),
484 None => tcx.item_name(def_id).as_str(),
487 let alloc = match link_name.get() {
488 "__cxa_thread_atexit_impl" => {
489 // This should be all-zero, pointer-sized.
490 let size = tcx.data_layout.pointer_size;
491 let data = vec![0; size.bytes() as usize];
492 Allocation::from_bytes(&data, tcx.data_layout.pointer_align.abi)
494 _ => return err!(Unimplemented(
495 format!("can't access foreign static: {}", link_name),
498 Ok(Cow::Owned(alloc))
502 fn before_terminator(_ecx: &mut InterpretCx<'mir, 'tcx, Self>) -> InterpResult<'tcx>
504 // We are not interested in detecting loops.
508 fn tag_allocation<'b>(
510 alloc: Cow<'b, Allocation>,
511 kind: Option<MemoryKind<Self::MemoryKinds>>,
512 memory: &Memory<'mir, 'tcx, Self>,
513 ) -> (Cow<'b, Allocation<Self::PointerTag, Self::AllocExtra>>, Self::PointerTag) {
514 let kind = kind.expect("we set our STATIC_KIND so this cannot be None");
515 let alloc = alloc.into_owned();
516 let (stacks, base_tag) = Stacks::new_allocation(
518 Size::from_bytes(alloc.bytes.len() as u64),
519 Rc::clone(&memory.extra.stacked_borrows),
522 if kind != MiriMemoryKind::Static.into() {
523 assert!(alloc.relocations.is_empty(), "Only statics can come initialized with inner pointers");
524 // Now we can rely on the inner pointers being static, too.
526 let mut memory_extra = memory.extra.stacked_borrows.borrow_mut();
527 let alloc: Allocation<Tag, Self::AllocExtra> = Allocation {
529 relocations: Relocations::from_presorted(
530 alloc.relocations.iter()
531 // The allocations in the relocations (pointers stored *inside* this allocation)
532 // all get the base pointer tag.
533 .map(|&(offset, ((), alloc))| (offset, (memory_extra.static_base_ptr(alloc), alloc)))
536 undef_mask: alloc.undef_mask,
538 mutability: alloc.mutability,
540 stacked_borrows: stacks,
541 intptrcast: Default::default(),
544 (Cow::Owned(alloc), base_tag)
548 fn tag_static_base_pointer(
550 memory: &Memory<'mir, 'tcx, Self>,
551 ) -> Self::PointerTag {
552 memory.extra.stacked_borrows.borrow_mut().static_base_ptr(id)
557 ecx: &mut InterpretCx<'mir, 'tcx, Self>,
558 kind: mir::RetagKind,
559 place: PlaceTy<'tcx, Tag>,
560 ) -> InterpResult<'tcx> {
561 if !ecx.tcx.sess.opts.debugging_opts.mir_emit_retag || !Self::enforce_validity(ecx) {
562 // No tracking, or no retagging. The latter is possible because a dependency of ours
563 // might be called with different flags than we are, so there are `Retag`
564 // statements but we do not want to execute them.
565 // Also, honor the whitelist in `enforce_validity` because otherwise we might retag
566 // uninitialized data.
569 ecx.retag(kind, place)
575 ecx: &mut InterpretCx<'mir, 'tcx, Self>,
576 ) -> InterpResult<'tcx, stacked_borrows::CallId> {
577 Ok(ecx.memory().extra.stacked_borrows.borrow_mut().new_call())
582 ecx: &mut InterpretCx<'mir, 'tcx, Self>,
583 extra: stacked_borrows::CallId,
584 ) -> InterpResult<'tcx> {
585 Ok(ecx.memory().extra.stacked_borrows.borrow_mut().end_call(extra))
590 memory: &Memory<'mir, 'tcx, Self>,
591 ) -> InterpResult<'tcx, Pointer<Self::PointerTag>> {
593 err!(InvalidNullPointerUsage)
594 } else if memory.extra.rng.is_none() {
595 err!(ReadBytesAsPointer)
597 intptrcast::GlobalState::int_to_ptr(int, memory)
602 ptr: Pointer<Self::PointerTag>,
603 memory: &Memory<'mir, 'tcx, Self>,
604 ) -> InterpResult<'tcx, u64> {
605 if memory.extra.rng.is_none() {
606 err!(ReadPointerAsBytes)
608 intptrcast::GlobalState::ptr_to_int(ptr, memory)