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 ecx.memory_mut().extra.rng = config.seed.map(StdRng::seed_from_u64);
90 let main_instance = ty::Instance::mono(ecx.tcx.tcx, main_id);
91 let main_mir = ecx.load_mir(main_instance.def)?;
93 if !main_mir.return_ty().is_unit() || main_mir.arg_count != 0 {
94 return err!(Unimplemented(
95 "miri does not support main functions without `fn()` type signatures"
100 let start_id = tcx.lang_items().start_fn().unwrap();
101 let main_ret_ty = tcx.fn_sig(main_id).output();
102 let main_ret_ty = main_ret_ty.no_bound_vars().unwrap();
103 let start_instance = ty::Instance::resolve(
105 ty::ParamEnv::reveal_all(),
108 ::std::iter::once(ty::subst::Kind::from(main_ret_ty)))
110 let start_mir = ecx.load_mir(start_instance.def)?;
112 if start_mir.arg_count != 3 {
113 return err!(AbiViolation(format!(
114 "'start' lang item should have three arguments, but has {}",
119 // Return value (in static memory so that it does not count as leak).
120 let ret = ecx.layout_of(start_mir.return_ty())?;
121 let ret_ptr = ecx.allocate(ret, MiriMemoryKind::Static.into());
123 // Push our stack frame.
124 ecx.push_stack_frame(
126 // There is no call site.
129 Some(ret_ptr.into()),
130 StackPopCleanup::None { cleanup: true },
133 let mut args = ecx.frame().body.args_iter();
135 // First argument: pointer to `main()`.
136 let main_ptr = ecx.memory_mut().create_fn_alloc(main_instance);
137 let dest = ecx.eval_place(&mir::Place::Base(mir::PlaceBase::Local(args.next().unwrap())))?;
138 ecx.write_scalar(Scalar::Ptr(main_ptr), dest)?;
140 // Second argument (argc): `1`.
141 let dest = ecx.eval_place(&mir::Place::Base(mir::PlaceBase::Local(args.next().unwrap())))?;
142 let argc = Scalar::from_uint(config.args.len() as u128, dest.layout.size);
143 ecx.write_scalar(argc, dest)?;
144 // Store argc for macOS's `_NSGetArgc`.
146 let argc_place = ecx.allocate(dest.layout, MiriMemoryKind::Env.into());
147 ecx.write_scalar(argc, argc_place.into())?;
148 ecx.machine.argc = Some(argc_place.ptr.to_ptr()?);
151 // FIXME: extract main source file path.
152 // Third argument (`argv`): created from `config.args`.
153 let dest = ecx.eval_place(&mir::Place::Base(mir::PlaceBase::Local(args.next().unwrap())))?;
154 // For Windows, construct a command string with all the aguments.
155 let mut cmd = String::new();
156 for arg in config.args.iter() {
160 cmd.push_str(&*shell_escape::windows::escape(arg.as_str().into()));
162 // Don't forget `0` terminator.
163 cmd.push(std::char::from_u32(0).unwrap());
164 // Collect the pointers to the individual strings.
165 let mut argvs = Vec::<Pointer<Tag>>::new();
166 for arg in config.args {
167 // Add `0` terminator.
168 let mut arg = arg.into_bytes();
170 argvs.push(ecx.memory_mut().allocate_static_bytes(arg.as_slice(), MiriMemoryKind::Static.into()));
172 // Make an array with all these pointers, in the Miri memory.
173 let argvs_layout = ecx.layout_of(ecx.tcx.mk_array(ecx.tcx.mk_imm_ptr(ecx.tcx.types.u8), argvs.len() as u64))?;
174 let argvs_place = ecx.allocate(argvs_layout, MiriMemoryKind::Env.into());
175 for (idx, arg) in argvs.into_iter().enumerate() {
176 let place = ecx.mplace_field(argvs_place, idx as u64)?;
177 ecx.write_scalar(Scalar::Ptr(arg), place.into())?;
179 ecx.memory_mut().mark_immutable(argvs_place.to_ptr()?.alloc_id)?;
180 // Write a pointer to that place as the argument.
181 let argv = argvs_place.ptr;
182 ecx.write_scalar(argv, dest)?;
183 // Store `argv` for macOS `_NSGetArgv`.
185 let argv_place = ecx.allocate(dest.layout, MiriMemoryKind::Env.into());
186 ecx.write_scalar(argv, argv_place.into())?;
187 ecx.machine.argv = Some(argv_place.ptr.to_ptr()?);
189 // Store command line as UTF-16 for Windows `GetCommandLineW`.
191 let tcx = &{ecx.tcx.tcx};
192 let cmd_utf16: Vec<u16> = cmd.encode_utf16().collect();
193 let cmd_ptr = ecx.memory_mut().allocate(
194 Size::from_bytes(cmd_utf16.len() as u64 * 2),
195 Align::from_bytes(2).unwrap(),
196 MiriMemoryKind::Env.into(),
198 ecx.machine.cmd_line = Some(cmd_ptr);
199 // Store the UTF-16 string.
200 let char_size = Size::from_bytes(2);
201 let cmd_alloc = ecx.memory_mut().get_mut(cmd_ptr.alloc_id)?;
202 let mut cur_ptr = cmd_ptr;
203 for &c in cmd_utf16.iter() {
204 cmd_alloc.write_scalar(
207 Scalar::from_uint(c, char_size).into(),
210 cur_ptr = cur_ptr.offset(char_size, tcx)?;
214 assert!(args.next().is_none(), "start lang item has more arguments than expected");
219 pub fn eval_main<'tcx>(
224 let mut ecx = match create_ecx(tcx, main_id, config) {
227 err.print_backtrace();
228 panic!("Miri initialziation error: {}", err.kind)
232 // Perform the main execution.
233 let res: InterpResult = (|| {
238 // Process the result.
241 let leaks = ecx.memory().leak_report();
242 // Disable the leak test on some platforms where we do not
243 // correctly implement TLS destructors.
244 let target_os = ecx.tcx.tcx.sess.target.target.target_os.to_lowercase();
245 let ignore_leaks = target_os == "windows" || target_os == "macos";
246 if !ignore_leaks && leaks != 0 {
247 tcx.sess.err("the evaluated program leaked memory");
251 // Special treatment for some error kinds
252 let msg = match e.kind {
253 InterpError::Exit(code) => std::process::exit(code),
254 InterpError::NoMirFor(..) =>
255 format!("{}. Did you set `MIRI_SYSROOT` to a Miri-enabled sysroot? You can prepare one with `cargo miri setup`.", e),
259 if let Some(frame) = ecx.stack().last() {
260 let block = &frame.body.basic_blocks()[frame.block];
261 let span = if frame.stmt < block.statements.len() {
262 block.statements[frame.stmt].source_info.span
264 block.terminator().source_info.span
267 let msg = format!("Miri evaluation error: {}", msg);
268 let mut err = struct_error(ecx.tcx.tcx.at(span), msg.as_str());
269 let frames = ecx.generate_stacktrace(None);
270 err.span_label(span, msg);
271 // We iterate with indices because we need to look at the next frame (the caller).
272 for idx in 0..frames.len() {
273 let frame_info = &frames[idx];
274 let call_site_is_local = frames.get(idx+1).map_or(false,
275 |caller_info| caller_info.instance.def_id().is_local());
276 if call_site_is_local {
277 err.span_note(frame_info.call_site, &frame_info.to_string());
279 err.note(&frame_info.to_string());
284 ecx.tcx.sess.err(&msg);
287 for (i, frame) in ecx.stack().iter().enumerate() {
288 trace!("-------------------");
289 trace!("Frame {}", i);
290 trace!(" return: {:?}", frame.return_place.map(|p| *p));
291 for (i, local) in frame.locals.iter().enumerate() {
292 trace!(" local {}: {:?}", i, local.value);
299 #[derive(Debug, Copy, Clone, PartialEq, Eq)]
300 pub enum MiriMemoryKind {
301 /// `__rust_alloc` memory.
305 /// Part of env var emulation.
311 impl Into<MemoryKind<MiriMemoryKind>> for MiriMemoryKind {
313 fn into(self) -> MemoryKind<MiriMemoryKind> {
314 MemoryKind::Machine(self)
318 impl MayLeak for MiriMemoryKind {
320 fn may_leak(self) -> bool {
321 use self::MiriMemoryKind::*;
324 Env | Static => true,
329 pub struct Evaluator<'tcx> {
330 /// Environment variables set by `setenv`.
331 /// Miri does not expose env vars from the host to the emulated program.
332 pub(crate) env_vars: HashMap<Vec<u8>, Pointer<Tag>>,
334 /// Program arguments (`Option` because we can only initialize them after creating the ecx).
335 /// These are *pointers* to argc/argv because macOS.
336 /// We also need the full command line as one string because of Windows.
337 pub(crate) argc: Option<Pointer<Tag>>,
338 pub(crate) argv: Option<Pointer<Tag>>,
339 pub(crate) cmd_line: Option<Pointer<Tag>>,
342 pub(crate) last_error: u32,
345 pub(crate) tls: TlsData<'tcx>,
347 /// Whether to enforce the validity invariant.
348 pub(crate) validate: bool,
351 impl<'tcx> Evaluator<'tcx> {
352 fn new(validate: bool) -> Self {
354 env_vars: HashMap::default(),
359 tls: TlsData::default(),
365 // FIXME: rustc issue <https://github.com/rust-lang/rust/issues/47131>.
367 type MiriEvalContext<'mir, 'tcx> = InterpretCx<'mir, 'tcx, Evaluator<'tcx>>;
369 // A little trait that's useful to be inherited by extension traits.
370 pub trait MiriEvalContextExt<'mir, 'tcx> {
371 fn eval_context_ref(&self) -> &MiriEvalContext<'mir, 'tcx>;
372 fn eval_context_mut(&mut self) -> &mut MiriEvalContext<'mir, 'tcx>;
374 impl<'mir, 'tcx> MiriEvalContextExt<'mir, 'tcx> for MiriEvalContext<'mir, 'tcx> {
376 fn eval_context_ref(&self) -> &MiriEvalContext<'mir, 'tcx> {
380 fn eval_context_mut(&mut self) -> &mut MiriEvalContext<'mir, 'tcx> {
385 impl<'mir, 'tcx> Machine<'mir, 'tcx> for Evaluator<'tcx> {
386 type MemoryKinds = MiriMemoryKind;
388 type FrameExtra = stacked_borrows::CallId;
389 type MemoryExtra = memory::MemoryExtra;
390 type AllocExtra = memory::AllocExtra;
391 type PointerTag = Tag;
393 type MemoryMap = MonoHashMap<AllocId, (MemoryKind<MiriMemoryKind>, Allocation<Tag, Self::AllocExtra>)>;
395 const STATIC_KIND: Option<MiriMemoryKind> = Some(MiriMemoryKind::Static);
398 fn enforce_validity(ecx: &InterpretCx<'mir, 'tcx, Self>) -> bool {
402 /// Returns `Ok()` when the function was handled; fail otherwise.
405 ecx: &mut InterpretCx<'mir, 'tcx, Self>,
406 instance: ty::Instance<'tcx>,
407 args: &[OpTy<'tcx, Tag>],
408 dest: Option<PlaceTy<'tcx, Tag>>,
409 ret: Option<mir::BasicBlock>,
410 ) -> InterpResult<'tcx, Option<&'mir mir::Body<'tcx>>> {
411 ecx.find_fn(instance, args, dest, ret)
416 ecx: &mut rustc_mir::interpret::InterpretCx<'mir, 'tcx, Self>,
417 instance: ty::Instance<'tcx>,
418 args: &[OpTy<'tcx, Tag>],
419 dest: PlaceTy<'tcx, Tag>,
420 ) -> InterpResult<'tcx> {
421 ecx.call_intrinsic(instance, args, dest)
426 ecx: &rustc_mir::interpret::InterpretCx<'mir, 'tcx, Self>,
428 left: ImmTy<'tcx, Tag>,
429 right: ImmTy<'tcx, Tag>,
430 ) -> InterpResult<'tcx, (Scalar<Tag>, bool)> {
431 ecx.ptr_op(bin_op, left, right)
435 ecx: &mut InterpretCx<'mir, 'tcx, Self>,
436 dest: PlaceTy<'tcx, Tag>,
437 ) -> InterpResult<'tcx> {
438 trace!("box_alloc for {:?}", dest.layout.ty);
439 // Call the `exchange_malloc` lang item.
440 let malloc = ecx.tcx.lang_items().exchange_malloc_fn().unwrap();
441 let malloc = ty::Instance::mono(ecx.tcx.tcx, malloc);
442 let malloc_mir = ecx.load_mir(malloc.def)?;
443 ecx.push_stack_frame(
448 // Don't do anything when we are done. The `statement()` function will increment
449 // the old stack frame's stmt counter to the next statement, which means that when
450 // `exchange_malloc` returns, we go on evaluating exactly where we want to be.
451 StackPopCleanup::None { cleanup: true },
454 let mut args = ecx.frame().body.args_iter();
455 let layout = ecx.layout_of(dest.layout.ty.builtin_deref(false).unwrap().ty)?;
457 // First argument: `size`.
458 // (`0` is allowed here -- this is expected to be handled by the lang item).
459 let arg = ecx.eval_place(&mir::Place::Base(mir::PlaceBase::Local(args.next().unwrap())))?;
460 let size = layout.size.bytes();
461 ecx.write_scalar(Scalar::from_uint(size, arg.layout.size), arg)?;
463 // Second argument: `align`.
464 let arg = ecx.eval_place(&mir::Place::Base(mir::PlaceBase::Local(args.next().unwrap())))?;
465 let align = layout.align.abi.bytes();
466 ecx.write_scalar(Scalar::from_uint(align, arg.layout.size), arg)?;
468 // No more arguments.
470 args.next().is_none(),
471 "`exchange_malloc` lang item has more arguments than expected"
476 fn find_foreign_static(
479 ) -> InterpResult<'tcx, Cow<'tcx, Allocation>> {
480 let attrs = tcx.get_attrs(def_id);
481 let link_name = match attr::first_attr_value_str_by_name(&attrs, sym::link_name) {
482 Some(name) => name.as_str(),
483 None => tcx.item_name(def_id).as_str(),
486 let alloc = match link_name.get() {
487 "__cxa_thread_atexit_impl" => {
488 // This should be all-zero, pointer-sized.
489 let size = tcx.data_layout.pointer_size;
490 let data = vec![0; size.bytes() as usize];
491 Allocation::from_bytes(&data, tcx.data_layout.pointer_align.abi)
493 _ => return err!(Unimplemented(
494 format!("can't access foreign static: {}", link_name),
497 Ok(Cow::Owned(alloc))
501 fn before_terminator(_ecx: &mut InterpretCx<'mir, 'tcx, Self>) -> InterpResult<'tcx>
503 // We are not interested in detecting loops.
507 fn tag_allocation<'b>(
509 alloc: Cow<'b, Allocation>,
510 kind: Option<MemoryKind<Self::MemoryKinds>>,
511 memory: &Memory<'mir, 'tcx, Self>,
512 ) -> (Cow<'b, Allocation<Self::PointerTag, Self::AllocExtra>>, Self::PointerTag) {
513 let kind = kind.expect("we set our STATIC_KIND so this cannot be None");
514 let alloc = alloc.into_owned();
515 let (stacks, base_tag) = Stacks::new_allocation(
517 Size::from_bytes(alloc.bytes.len() as u64),
518 Rc::clone(&memory.extra.stacked_borrows),
521 if kind != MiriMemoryKind::Static.into() {
522 assert!(alloc.relocations.is_empty(), "Only statics can come initialized with inner pointers");
523 // Now we can rely on the inner pointers being static, too.
525 let mut memory_extra = memory.extra.stacked_borrows.borrow_mut();
526 let alloc: Allocation<Tag, Self::AllocExtra> = Allocation {
528 relocations: Relocations::from_presorted(
529 alloc.relocations.iter()
530 // The allocations in the relocations (pointers stored *inside* this allocation)
531 // all get the base pointer tag.
532 .map(|&(offset, ((), alloc))| (offset, (memory_extra.static_base_ptr(alloc), alloc)))
535 undef_mask: alloc.undef_mask,
537 mutability: alloc.mutability,
539 stacked_borrows: stacks,
540 intptrcast: Default::default(),
543 (Cow::Owned(alloc), base_tag)
547 fn tag_static_base_pointer(
549 memory: &Memory<'mir, 'tcx, Self>,
550 ) -> Self::PointerTag {
551 memory.extra.stacked_borrows.borrow_mut().static_base_ptr(id)
556 ecx: &mut InterpretCx<'mir, 'tcx, Self>,
557 kind: mir::RetagKind,
558 place: PlaceTy<'tcx, Tag>,
559 ) -> InterpResult<'tcx> {
560 if !ecx.tcx.sess.opts.debugging_opts.mir_emit_retag || !Self::enforce_validity(ecx) {
561 // No tracking, or no retagging. The latter is possible because a dependency of ours
562 // might be called with different flags than we are, so there are `Retag`
563 // statements but we do not want to execute them.
564 // Also, honor the whitelist in `enforce_validity` because otherwise we might retag
565 // uninitialized data.
568 ecx.retag(kind, place)
574 ecx: &mut InterpretCx<'mir, 'tcx, Self>,
575 ) -> InterpResult<'tcx, stacked_borrows::CallId> {
576 Ok(ecx.memory().extra.stacked_borrows.borrow_mut().new_call())
581 ecx: &mut InterpretCx<'mir, 'tcx, Self>,
582 extra: stacked_borrows::CallId,
583 ) -> InterpResult<'tcx> {
584 Ok(ecx.memory().extra.stacked_borrows.borrow_mut().end_call(extra))
589 memory: &Memory<'mir, 'tcx, Self>,
590 ) -> InterpResult<'tcx, Pointer<Self::PointerTag>> {
592 err!(InvalidNullPointerUsage)
593 } else if memory.extra.rng.is_none() {
594 err!(ReadBytesAsPointer)
596 intptrcast::GlobalState::int_to_ptr(int, memory)
601 ptr: Pointer<Self::PointerTag>,
602 memory: &Memory<'mir, 'tcx, Self>,
603 ) -> InterpResult<'tcx, u64> {
604 if memory.extra.rng.is_none() {
605 err!(ReadPointerAsBytes)
607 intptrcast::GlobalState::ptr_to_int(ptr, memory)