From 1a4ad2bb9fe3216010f33b47c6b05debaef7d604 Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Thu, 23 Aug 2018 19:28:48 +0200 Subject: [PATCH] update for miri engine: new function handling, new static handling, fixed leaks --- src/fn_call.rs | 179 +++++++++--------- src/helpers.rs | 52 +---- src/intrinsic.rs | 87 +-------- src/lib.rs | 163 +++------------- src/memory.rs | 18 +- src/tls.rs | 2 +- tests/compile-fail/memleak.rs | 1 - tests/compile-fail/memleak_rc.rs | 1 - tests/compile-fail/modifying_constants.rs | 1 - .../static_memory_modification.rs | 1 - tests/run-pass/pthreads.rs | 14 ++ 11 files changed, 149 insertions(+), 370 deletions(-) create mode 100644 tests/run-pass/pthreads.rs diff --git a/src/fn_call.rs b/src/fn_call.rs index d8afe087899..6e3fcf7d448 100644 --- a/src/fn_call.rs +++ b/src/fn_call.rs @@ -3,7 +3,6 @@ use rustc::ty::layout::{Align, LayoutOf, Size}; use rustc::hir::def_id::{DefId, CRATE_DEF_INDEX}; use rustc::mir; use syntax::attr; -use syntax::source_map::Span; use std::mem; @@ -13,122 +12,138 @@ use tls::MemoryExt; use super::memory::MemoryKind; -pub trait EvalContextExt<'tcx> { - fn call_foreign_item( +pub trait EvalContextExt<'tcx, 'mir> { + /// Emulate calling a foreign item, fail if the item is not supported. + /// This function will handle `goto_block` if needed. + fn emulate_foreign_item( &mut self, def_id: DefId, args: &[OpTy<'tcx>], dest: PlaceTy<'tcx>, - dest_block: mir::BasicBlock, + ret: mir::BasicBlock, ) -> EvalResult<'tcx>; fn resolve_path(&self, path: &[&str]) -> EvalResult<'tcx, ty::Instance<'tcx>>; - fn call_missing_fn( + /// Emulate a function that should have MIR but does not. + /// This is solely to support execution without full MIR. + /// Fail if emulating this function is not supported. + /// This function will handle `goto_block` if needed. + fn emulate_missing_fn( &mut self, - instance: ty::Instance<'tcx>, - destination: Option<(PlaceTy<'tcx>, mir::BasicBlock)>, - args: &[OpTy<'tcx>], path: String, + args: &[OpTy<'tcx>], + dest: Option>, + ret: Option, ) -> EvalResult<'tcx>; - fn eval_fn_call( + fn find_fn( &mut self, instance: ty::Instance<'tcx>, - destination: Option<(PlaceTy<'tcx>, mir::BasicBlock)>, args: &[OpTy<'tcx>], - span: Span, - ) -> EvalResult<'tcx, bool>; + dest: Option>, + ret: Option, + ) -> EvalResult<'tcx, Option<&'mir mir::Mir<'tcx>>>; fn write_null(&mut self, dest: PlaceTy<'tcx>) -> EvalResult<'tcx>; } -impl<'a, 'mir, 'tcx: 'mir + 'a> EvalContextExt<'tcx> for EvalContext<'a, 'mir, 'tcx, super::Evaluator<'tcx>> { - fn eval_fn_call( +impl<'a, 'mir, 'tcx: 'mir + 'a> EvalContextExt<'tcx, 'mir> for EvalContext<'a, 'mir, 'tcx, super::Evaluator<'tcx>> { + fn find_fn( &mut self, instance: ty::Instance<'tcx>, - destination: Option<(PlaceTy<'tcx>, mir::BasicBlock)>, args: &[OpTy<'tcx>], - span: Span, - ) -> EvalResult<'tcx, bool> { - trace!("eval_fn_call: {:#?}, {:?}", instance, destination.map(|(place, bb)| (*place, bb))); + dest: Option>, + ret: Option, + ) -> EvalResult<'tcx, Option<&'mir mir::Mir<'tcx>>> { + trace!("eval_fn_call: {:#?}, {:?}", instance, dest.map(|place| *place)); + // first run the common hooks also supported by CTFE + if self.hook_fn(instance, args, dest)? { + self.goto_block(ret)?; + return Ok(None); + } + // there are some more lang items we want to hook that CTFE does not hook (yet) + if self.tcx.lang_items().align_offset_fn() == Some(instance.def.def_id()) { + // FIXME: return a real value in case the target allocation has an + // alignment bigger than the one requested + let n = u128::max_value(); + let dest = dest.unwrap(); + let n = self.truncate(n, dest.layout); + self.write_scalar(Scalar::from_uint(n, dest.layout.size), dest)?; + self.goto_block(ret)?; + return Ok(None); + } + + // FIXME: Why are these hooked here, not in `emulate_missing_fn` or so? let def_id = instance.def_id(); let item_path = self.tcx.absolute_item_path_str(def_id); match &*item_path { "std::sys::unix::thread::guard::init" | "std::sys::unix::thread::guard::current" => { // Return None, as it doesn't make sense to return Some, because miri detects stack overflow itself. - let (return_place, return_to_block) = destination.unwrap(); - match return_place.layout.ty.sty { + let dest = dest.unwrap(); + match dest.layout.ty.sty { ty::Adt(ref adt_def, _) => { assert!(adt_def.is_enum(), "Unexpected return type for {}", item_path); let none_variant_index = adt_def.variants.iter().position(|def| { def.name.as_str() == "None" }).expect("No None variant"); - self.write_discriminant_value(none_variant_index, return_place)?; - self.goto_block(return_to_block); - return Ok(true); + self.write_discriminant_value(none_variant_index, dest)?; + self.goto_block(ret)?; + return Ok(None); } _ => panic!("Unexpected return type for {}", item_path) } } "std::sys::unix::fast_thread_local::register_dtor" => { // TODO: register the dtor - let (_return_place, return_to_block) = destination.unwrap(); - self.goto_block(return_to_block); - return Ok(true); + self.goto_block(ret)?; + return Ok(None); } _ => {} } - if self.tcx.lang_items().align_offset_fn() == Some(instance.def.def_id()) { - // FIXME: return a real value in case the target allocation has an - // alignment bigger than the one requested - let n = u128::max_value(); - let (dest, return_to_block) = destination.unwrap(); - let n = self.truncate(n, dest.layout); - self.write_scalar(Scalar::from_uint(n, dest.layout.size), dest)?; - self.goto_block(return_to_block); - return Ok(true); + // Try to see if we can do something about foreign items + if self.tcx.is_foreign_item(instance.def_id()) { + // An external function that we cannot find MIR for, but we can still run enough + // of them to make miri viable. + self.emulate_foreign_item( + instance.def_id(), + args, + dest.unwrap(), + ret.unwrap(), + )?; + // `goto_block` already handled + return Ok(None); } + // Otherwise we really want to see the MIR -- but if we do not have it, maybe we can + // emulate something. This is a HACK to support running without a full-MIR libstd. let mir = match self.load_mir(instance.def) { Ok(mir) => mir, Err(EvalError { kind: EvalErrorKind::NoMirFor(path), .. }) => { - self.call_missing_fn( - instance, - destination, - args, + self.emulate_missing_fn( path, + args, + dest, + ret, )?; - return Ok(true); + // `goto_block` already handled + return Ok(None); } Err(other) => return Err(other), }; - let (return_place, return_to_block) = match destination { - Some((place, block)) => (*place, StackPopCleanup::Goto(block)), - None => (Place::null(&self), StackPopCleanup::None), - }; - - self.push_stack_frame( - instance, - span, - mir, - return_place, - return_to_block, - )?; - - Ok(false) + Ok(Some(mir)) } - fn call_foreign_item( + fn emulate_foreign_item( &mut self, def_id: DefId, args: &[OpTy<'tcx>], dest: PlaceTy<'tcx>, - dest_block: mir::BasicBlock, + ret: mir::BasicBlock, ) -> EvalResult<'tcx> { let attrs = self.tcx.get_attrs(def_id); let link_name = match attr::first_attr_value_str_by_name(&attrs, "link_name") { @@ -269,13 +284,13 @@ impl<'a, 'mir, 'tcx: 'mir + 'a> EvalContextExt<'tcx> for EvalContext<'a, 'mir, ' // Now we make a function call. TODO: Consider making this re-usable? EvalContext::step does sth. similar for the TLS dtors, // and of course eval_main. let mir = self.load_mir(f_instance.def)?; - let ret = Place::null(&self); + let closure_dest = Place::null(&self); self.push_stack_frame( f_instance, mir.span, mir, - ret, - StackPopCleanup::Goto(dest_block), + closure_dest, + StackPopCleanup::Goto(Some(ret)), // directly return to caller )?; let mut args = self.frame().mir.args_iter(); @@ -290,16 +305,15 @@ impl<'a, 'mir, 'tcx: 'mir + 'a> EvalContextExt<'tcx> for EvalContext<'a, 'mir, ' assert!(args.next().is_none(), "__rust_maybe_catch_panic argument has more arguments than expected"); - // We ourselves return 0 + // We ourselves will return 0, eventually (because we will not return if we paniced) self.write_null(dest)?; - // Don't fall through + // Don't fall through, we do NOT want to `goto_block`! return Ok(()); } - "__rust_start_panic" => { - return err!(Panic); - } + "__rust_start_panic" => + return err!(MachineError("the evaluated program panicked".to_string())), "memcmp" => { let left = self.read_scalar(args[0])?.not_undef()?; @@ -624,11 +638,8 @@ impl<'a, 'mir, 'tcx: 'mir + 'a> EvalContextExt<'tcx> for EvalContext<'a, 'mir, ' } } - // Since we pushed no stack frame, the main loop will act - // as if the call just completed and it's returning to the - // current frame. + self.goto_block(Some(ret))?; self.dump_place(*dest); - self.goto_block(dest_block); Ok(()) } @@ -666,38 +677,27 @@ impl<'a, 'mir, 'tcx: 'mir + 'a> EvalContextExt<'tcx> for EvalContext<'a, 'mir, ' }) } - fn call_missing_fn( + fn emulate_missing_fn( &mut self, - instance: ty::Instance<'tcx>, - destination: Option<(PlaceTy<'tcx>, mir::BasicBlock)>, - args: &[OpTy<'tcx>], path: String, + _args: &[OpTy<'tcx>], + dest: Option>, + ret: Option, ) -> EvalResult<'tcx> { // In some cases in non-MIR libstd-mode, not having a destination is legit. Handle these early. match &path[..] { "std::panicking::rust_panic_with_hook" | "core::panicking::panic_fmt::::panic_impl" | - "std::rt::begin_panic_fmt" => return err!(Panic), + "std::rt::begin_panic_fmt" => + return err!(MachineError("the evaluated program panicked".to_string())), _ => {} } - let (dest, dest_block) = destination.ok_or_else( + let dest = dest.ok_or_else( + // Must be some function we do not support || EvalErrorKind::NoMirFor(path.clone()), )?; - if self.tcx.is_foreign_item(instance.def_id()) { - // An external function - // TODO: That functions actually has a similar preamble to what follows here. May make sense to - // unify these two mechanisms for "hooking into missing functions". - self.call_foreign_item( - instance.def_id(), - args, - dest, - dest_block, - )?; - return Ok(()); - } - match &path[..] { // A Rust function is missing, which means we are running with MIR missing for libstd (or other dependencies). // Still, we can make many things mostly work by "emulating" or ignoring some functions. @@ -724,11 +724,8 @@ impl<'a, 'mir, 'tcx: 'mir + 'a> EvalContextExt<'tcx> for EvalContext<'a, 'mir, ' _ => return err!(NoMirFor(path)), } - // Since we pushed no stack frame, the main loop will act - // as if the call just completed and it's returning to the - // current frame. + self.goto_block(ret)?; self.dump_place(*dest); - self.goto_block(dest_block); Ok(()) } diff --git a/src/helpers.rs b/src/helpers.rs index 88dd351513a..24285fa4a6e 100644 --- a/src/helpers.rs +++ b/src/helpers.rs @@ -1,5 +1,4 @@ -use rustc::ty::layout::{Size, HasDataLayout}; -use rustc::mir::interpret::sign_extend; +use rustc::ty::layout::Size; use super::{Scalar, ScalarMaybeUndef, EvalResult}; @@ -14,11 +13,6 @@ pub trait ScalarExt { } pub trait FalibleScalarExt { - fn to_usize(self, cx: impl HasDataLayout) -> EvalResult<'static, u64>; - fn to_isize(self, cx: impl HasDataLayout) -> EvalResult<'static, i64>; - fn to_i32(self) -> EvalResult<'static, i32>; - fn to_u8(self) -> EvalResult<'static, u8>; - /// HACK: this function just extracts all bits if `defined != 0` /// Mainly used for args of C-functions and we should totally correctly fetch the size /// of their arguments @@ -59,34 +53,6 @@ impl ScalarExt for Scalar { } impl FalibleScalarExt for Scalar { - fn to_usize(self, cx: impl HasDataLayout) -> EvalResult<'static, u64> { - let b = self.to_bits(cx.data_layout().pointer_size)?; - assert_eq!(b as u64 as u128, b); - Ok(b as u64) - } - - fn to_u8(self) -> EvalResult<'static, u8> { - let sz = Size::from_bits(8); - let b = self.to_bits(sz)?; - assert_eq!(b as u8 as u128, b); - Ok(b as u8) - } - - fn to_isize(self, cx: impl HasDataLayout) -> EvalResult<'static, i64> { - let b = self.to_bits(cx.data_layout().pointer_size)?; - let b = sign_extend(b, cx.data_layout().pointer_size) as i128; - assert_eq!(b as i64 as i128, b); - Ok(b as i64) - } - - fn to_i32(self) -> EvalResult<'static, i32> { - let sz = Size::from_bits(32); - let b = self.to_bits(sz)?; - let b = sign_extend(b, sz) as i128; - assert_eq!(b as i32 as i128, b); - Ok(b as i32) - } - fn to_bytes(self) -> EvalResult<'static, u128> { match self { Scalar::Bits { bits, size } => { @@ -99,22 +65,6 @@ impl FalibleScalarExt for Scalar { } impl FalibleScalarExt for ScalarMaybeUndef { - fn to_usize(self, cx: impl HasDataLayout) -> EvalResult<'static, u64> { - self.not_undef()?.to_usize(cx) - } - - fn to_u8(self) -> EvalResult<'static, u8> { - self.not_undef()?.to_u8() - } - - fn to_isize(self, cx: impl HasDataLayout) -> EvalResult<'static, i64> { - self.not_undef()?.to_isize(cx) - } - - fn to_i32(self) -> EvalResult<'static, i32> { - self.not_undef()?.to_i32() - } - fn to_bytes(self) -> EvalResult<'static, u128> { self.not_undef()?.to_bytes() } diff --git a/src/intrinsic.rs b/src/intrinsic.rs index 47399c39079..62a1938d0fc 100644 --- a/src/intrinsic.rs +++ b/src/intrinsic.rs @@ -1,5 +1,5 @@ use rustc::mir; -use rustc::ty::layout::{self, LayoutOf, Size, Primitive, Integer::*}; +use rustc::ty::layout::{self, LayoutOf, Size}; use rustc::ty; use rustc::mir::interpret::{EvalResult, Scalar, ScalarMaybeUndef}; @@ -15,7 +15,6 @@ pub trait EvalContextExt<'tcx> { instance: ty::Instance<'tcx>, args: &[OpTy<'tcx>], dest: PlaceTy<'tcx>, - target: mir::BasicBlock, ) -> EvalResult<'tcx>; } @@ -25,8 +24,11 @@ impl<'a, 'mir, 'tcx> EvalContextExt<'tcx> for EvalContext<'a, 'mir, 'tcx, super: instance: ty::Instance<'tcx>, args: &[OpTy<'tcx>], dest: PlaceTy<'tcx>, - target: mir::BasicBlock, ) -> EvalResult<'tcx> { + if self.emulate_intrinsic(instance, args, dest)? { + return Ok(()); + } + let substs = instance.substs; let intrinsic_name = &self.tcx.item_name(instance.def_id()).as_str()[..]; @@ -194,24 +196,6 @@ impl<'a, 'mir, 'tcx> EvalContextExt<'tcx> for EvalContext<'a, 'mir, 'tcx, super: } } - "ctpop" | "cttz" | "cttz_nonzero" | "ctlz" | "ctlz_nonzero" | "bswap" => { - let ty = substs.type_at(0); - let num = self.read_scalar(args[0])?.to_bytes()?; - let kind = match self.layout_of(ty)?.abi { - ty::layout::Abi::Scalar(ref scalar) => scalar.value, - _ => Err(::rustc::mir::interpret::EvalErrorKind::TypeNotPrimitive(ty))?, - }; - let num = if intrinsic_name.ends_with("_nonzero") { - if num == 0 { - return err!(Intrinsic(format!("{} called on 0", intrinsic_name))); - } - numeric_intrinsic(intrinsic_name.trim_right_matches("_nonzero"), num, kind)? - } else { - numeric_intrinsic(intrinsic_name, num, kind)? - }; - self.write_scalar(num, dest)?; - } - "discriminant_value" => { let place = self.ref_to_mplace(self.read_value(args[0])?)?; let discr_val = self.read_discriminant_value(place.into())?; @@ -316,14 +300,6 @@ impl<'a, 'mir, 'tcx> EvalContextExt<'tcx> for EvalContext<'a, 'mir, 'tcx, super: } } - "min_align_of" => { - let elem_ty = substs.type_at(0); - let elem_align = self.layout_of(elem_ty)?.align.abi(); - let ptr_size = self.memory.pointer_size(); - let align_val = Scalar::from_uint(elem_align as u128, ptr_size); - self.write_scalar(align_val, dest)?; - } - "pref_align_of" => { let ty = substs.type_at(0); let layout = self.layout_of(ty)?; @@ -456,13 +432,6 @@ impl<'a, 'mir, 'tcx> EvalContextExt<'tcx> for EvalContext<'a, 'mir, 'tcx, super: )?; } - "size_of" => { - let ty = substs.type_at(0); - let size = self.layout_of(ty)?.size.bytes(); - let ptr_size = self.memory.pointer_size(); - self.write_scalar(Scalar::from_uint(size, ptr_size), dest)?; - } - "size_of_val" => { let mplace = self.ref_to_mplace(self.read_value(args[0])?)?; let (size, _) = self.size_and_align_of_mplace(mplace)?; @@ -490,11 +459,6 @@ impl<'a, 'mir, 'tcx> EvalContextExt<'tcx> for EvalContext<'a, 'mir, 'tcx, super: let value = self.str_to_value(&ty_name)?; self.write_value(value, dest)?; } - "type_id" => { - let ty = substs.type_at(0); - let n = self.tcx.type_id_hash(ty); - self.write_scalar(Scalar::Bits { bits: n as u128, size: 8 }, dest)?; - } "transmute" => { // Go through an allocation, to make sure the completely different layouts @@ -610,47 +574,6 @@ impl<'a, 'mir, 'tcx> EvalContextExt<'tcx> for EvalContext<'a, 'mir, 'tcx, super: name => return err!(Unimplemented(format!("unimplemented intrinsic: {}", name))), } - self.goto_block(target); - - // Since we pushed no stack frame, the main loop will act - // as if the call just completed and it's returning to the - // current frame. Ok(()) } } - -fn numeric_intrinsic<'tcx>( - name: &str, - bytes: u128, - kind: Primitive, -) -> EvalResult<'tcx, Scalar> { - macro_rules! integer_intrinsic { - ($method:ident) => ({ - let (result_bytes, size) = match kind { - Primitive::Int(I8, true) => ((bytes as i8).$method() as u128, 1), - Primitive::Int(I8, false) => ((bytes as u8).$method() as u128, 1), - Primitive::Int(I16, true) => ((bytes as i16).$method() as u128, 2), - Primitive::Int(I16, false) => ((bytes as u16).$method() as u128, 2), - Primitive::Int(I32, true) => ((bytes as i32).$method() as u128, 4), - Primitive::Int(I32, false) => ((bytes as u32).$method() as u128, 4), - Primitive::Int(I64, true) => ((bytes as i64).$method() as u128, 8), - Primitive::Int(I64, false) => ((bytes as u64).$method() as u128, 8), - Primitive::Int(I128, true) => ((bytes as i128).$method() as u128, 16), - Primitive::Int(I128, false) => (bytes.$method() as u128, 16), - _ => bug!("invalid `{}` argument: {:?}", name, bytes), - }; - - Scalar::from_uint(result_bytes, Size::from_bytes(size)) - }); - } - - let result_val = match name { - "bswap" => integer_intrinsic!(swap_bytes), - "ctlz" => integer_intrinsic!(leading_zeros), - "ctpop" => integer_intrinsic!(count_ones), - "cttz" => integer_intrinsic!(trailing_zeros), - _ => bug!("not a numeric intrinsic: {}", name), - }; - - Ok(result_val) -} diff --git a/src/lib.rs b/src/lib.rs index 044ce28b301..81cee6fbe42 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -18,14 +18,12 @@ extern crate syntax; use rustc::ty::{self, TyCtxt}; use rustc::ty::layout::{TyLayout, LayoutOf, Size}; -use rustc::ty::subst::Subst; use rustc::hir::def_id::DefId; use rustc::mir; use rustc_data_structures::fx::FxHasher; use syntax::ast::Mutability; -use syntax::source_map::Span; use std::marker::PhantomData; use std::collections::{HashMap, BTreeMap}; @@ -47,6 +45,7 @@ use fn_call::EvalContextExt as MissingFnsEvalContextExt; use operator::EvalContextExt as OperatorEvalContextExt; use intrinsic::EvalContextExt as IntrinsicEvalContextExt; use tls::EvalContextExt as TlsEvalContextExt; +use memory::MemoryKind as MiriMemoryKind; use locks::LockInfo; use range_map::RangeMap; use helpers::{ScalarExt, FalibleScalarExt}; @@ -55,7 +54,7 @@ pub fn create_ecx<'a, 'mir: 'a, 'tcx: 'mir>( tcx: TyCtxt<'a, 'tcx, 'tcx>, main_id: DefId, start_wrapper: Option, -) -> EvalResult<'tcx, (EvalContext<'a, 'mir, 'tcx, Evaluator<'tcx>>, Option)> { +) -> EvalResult<'tcx, EvalContext<'a, 'mir, 'tcx, Evaluator<'tcx>>> { let mut ecx = EvalContext::new( tcx.at(syntax::source_map::DUMMY_SP), ty::ParamEnv::reveal_all(), @@ -65,7 +64,6 @@ pub fn create_ecx<'a, 'mir: 'a, 'tcx: 'mir>( let main_instance = ty::Instance::mono(ecx.tcx.tcx, main_id); let main_mir = ecx.load_mir(main_instance.def)?; - let mut cleanup_ptr = None; // Scalar to be deallocated when we are done if !main_mir.return_ty().is_nil() || main_mir.arg_count != 0 { return err!(Unimplemented( @@ -94,11 +92,10 @@ pub fn create_ecx<'a, 'mir: 'a, 'tcx: 'mir>( ))); } - // Return value + // Return value (in static memory so that it does not count as leak) let size = ecx.tcx.data_layout.pointer_size; let align = ecx.tcx.data_layout.pointer_align; - let ret_ptr = ecx.memory_mut().allocate(size, align, MemoryKind::Stack)?; - cleanup_ptr = Some(ret_ptr); + let ret_ptr = ecx.memory_mut().allocate(size, align, MiriMemoryKind::MutStatic.into())?; // Push our stack frame ecx.push_stack_frame( @@ -123,12 +120,12 @@ pub fn create_ecx<'a, 'mir: 'a, 'tcx: 'mir>( // FIXME: extract main source file path // Third argument (argv): &[b"foo"] let dest = ecx.eval_place(&mir::Place::Local(args.next().unwrap()))?; - let foo = ecx.memory.allocate_bytes(b"foo\0"); + let foo = ecx.memory.allocate_static_bytes(b"foo\0"); let foo_ty = ecx.tcx.mk_imm_ptr(ecx.tcx.types.u8); let foo_layout = ecx.layout_of(foo_ty)?; - let foo_place = ecx.allocate(foo_layout, MemoryKind::Stack)?; + let foo_place = ecx.allocate(foo_layout, MemoryKind::Stack)?; // will be marked as static in just a second ecx.write_scalar(Scalar::Ptr(foo), foo_place.into())?; - ecx.memory.mark_static_initialized(foo_place.to_ptr()?.alloc_id, Mutability::Immutable)?; + ecx.memory.mark_static_initialized(foo_place.to_ptr()?.alloc_id, Mutability::Immutable)?; // marked as static ecx.write_scalar(foo_place.ptr, dest)?; assert!(args.next().is_none(), "start lang item has more arguments than expected"); @@ -146,7 +143,7 @@ pub fn create_ecx<'a, 'mir: 'a, 'tcx: 'mir>( assert!(args.next().is_none(), "main function must not have arguments"); } - Ok((ecx, cleanup_ptr)) + Ok(ecx) } pub fn eval_main<'a, 'tcx: 'a>( @@ -154,26 +151,18 @@ pub fn eval_main<'a, 'tcx: 'a>( main_id: DefId, start_wrapper: Option, ) { - let (mut ecx, cleanup_ptr) = create_ecx(tcx, main_id, start_wrapper).expect("Couldn't create ecx"); + let mut ecx = create_ecx(tcx, main_id, start_wrapper).expect("Couldn't create ecx"); let res: EvalResult = do catch { - while ecx.step()? {} + ecx.run()?; ecx.run_tls_dtors()?; - if let Some(cleanup_ptr) = cleanup_ptr { - ecx.memory_mut().deallocate( - cleanup_ptr, - None, - MemoryKind::Stack, - )?; - } }; match res { Ok(()) => { let leaks = ecx.memory().leak_report(); if leaks != 0 { - // TODO: Prevent leaks which aren't supposed to be there - //tcx.sess.err("the evaluated program leaked memory"); + tcx.sess.err("the evaluated program leaked memory"); } } Err(e) => { @@ -198,6 +187,7 @@ pub fn eval_main<'a, 'tcx: 'a>( ecx.tcx.sess.err(&e.to_string()); } + /* Nice try, but with MIRI_BACKTRACE this shows 100s of backtraces. for (i, frame) in ecx.stack().iter().enumerate() { trace!("-------------------"); trace!("Frame {}", i); @@ -207,7 +197,7 @@ pub fn eval_main<'a, 'tcx: 'a>( trace!(" local {}: {:?}", i, local); } } - } + }*/ } } } @@ -262,8 +252,6 @@ pub struct MemoryData<'tcx> { /// Only mutable (static mut, heap, stack) allocations have an entry in this map. /// The entry is created when allocating the memory and deleted after deallocation. locks: HashMap>>, - - statics: HashMap, AllocId>, } impl<'tcx> MemoryData<'tcx> { @@ -272,7 +260,6 @@ impl<'tcx> MemoryData<'tcx> { next_thread_local: 1, // start with 1 as we must not use 0 on Windows thread_local: BTreeMap::new(), locks: HashMap::new(), - statics: HashMap::new(), } } } @@ -283,7 +270,6 @@ impl<'tcx> Hash for MemoryData<'tcx> { next_thread_local: _, thread_local, locks: _, - statics: _, } = self; thread_local.hash(state); @@ -295,14 +281,14 @@ impl<'mir, 'tcx: 'mir> Machine<'mir, 'tcx> for Evaluator<'tcx> { type MemoryKinds = memory::MemoryKind; /// Returns Ok() when the function was handled, fail otherwise - fn eval_fn_call<'a>( + fn find_fn<'a>( ecx: &mut EvalContext<'a, 'mir, 'tcx, Self>, instance: ty::Instance<'tcx>, - destination: Option<(PlaceTy<'tcx>, mir::BasicBlock)>, args: &[OpTy<'tcx>], - span: Span, - ) -> EvalResult<'tcx, bool> { - ecx.eval_fn_call(instance, destination, args, span) + dest: Option>, + ret: Option, + ) -> EvalResult<'tcx, Option<&'mir mir::Mir<'tcx>>> { + ecx.find_fn(instance, args, dest, ret) } fn call_intrinsic<'a>( @@ -310,9 +296,8 @@ impl<'mir, 'tcx: 'mir> Machine<'mir, 'tcx> for Evaluator<'tcx> { instance: ty::Instance<'tcx>, args: &[OpTy<'tcx>], dest: PlaceTy<'tcx>, - target: mir::BasicBlock, ) -> EvalResult<'tcx> { - ecx.call_intrinsic(instance, args, dest, target) + ecx.call_intrinsic(instance, args, dest) } fn try_ptr_op<'a>( @@ -326,82 +311,13 @@ impl<'mir, 'tcx: 'mir> Machine<'mir, 'tcx> for Evaluator<'tcx> { ecx.ptr_op(bin_op, left, left_layout, right, right_layout) } - fn mark_static_initialized<'a>( - mem: &mut Memory<'a, 'mir, 'tcx, Self>, + fn access_static_mut<'a, 'm>( + mem: &'m mut Memory<'a, 'mir, 'tcx, Self>, id: AllocId, - _mutability: Mutability, - ) -> EvalResult<'tcx, bool> { - use memory::MemoryKind::*; - match mem.get_alloc_kind(id) { - // FIXME: This could be allowed, but not for env vars set during miri execution - Some(MemoryKind::Machine(Env)) => err!(Unimplemented("statics can't refer to env vars".to_owned())), - _ => Ok(false), // TODO: What does the bool mean? - } - } - - fn init_static<'a>( - ecx: &mut EvalContext<'a, 'mir, 'tcx, Self>, - cid: GlobalId<'tcx>, - ) -> EvalResult<'tcx, AllocId> { - // Step 1: If the static has already been evaluated return the cached version - if let Some(alloc_id) = ecx.memory.data.statics.get(&cid) { - return Ok(*alloc_id); - } - - let tcx = ecx.tcx.tcx; - - // Step 2: Load mir - let mut mir = ecx.load_mir(cid.instance.def)?; - if let Some(index) = cid.promoted { - mir = &mir.promoted[index]; - } - assert!(mir.arg_count == 0); - - // Step 3: Allocate storage - let layout = ecx.layout_of(mir.return_ty().subst(tcx, cid.instance.substs))?; - assert!(!layout.is_unsized()); - let ptr = ecx.memory.allocate( - layout.size, - layout.align, - MemoryKind::Stack, - )?; - - // Step 4: Cache allocation id for recursive statics - assert!(ecx.memory.data.statics.insert(cid, ptr.alloc_id).is_none()); - - // Step 5: Push stackframe to evaluate static - let cleanup = StackPopCleanup::None; - ecx.push_stack_frame( - cid.instance, - mir.span, - mir, - Place::from_ptr(ptr, layout.align), - cleanup, - )?; - - // Step 6: Step until static has been initialized - let call_stackframe = ecx.stack().len(); - while ecx.step()? && ecx.stack().len() >= call_stackframe { - if ecx.stack().len() == call_stackframe { - let cleanup = { - let frame = ecx.frame(); - let bb = &frame.mir.basic_blocks()[frame.block]; - bb.statements.len() == frame.stmt && !bb.is_cleanup && - if let ::rustc::mir::TerminatorKind::Return = bb.terminator().kind { true } else { false } - }; - if cleanup { - for (local, _local_decl) in mir.local_decls.iter_enumerated().skip(1) { - // Don't deallocate locals, because the return value might reference them - ecx.storage_dead(local); - } - } - } - } - - // TODO: Freeze immutable statics without copying them to the global static cache - - // Step 7: Return the alloc - Ok(ptr.alloc_id) + ) -> EvalResult<'tcx, &'m mut Allocation> { + // Make a copy, use that. + mem.deep_copy_static(id, MiriMemoryKind::MutStatic.into())?; + mem.get_mut(id) // this is recursive, but now we know that `id` is in `alloc_map` } fn box_alloc<'a>( @@ -451,35 +367,6 @@ impl<'mir, 'tcx: 'mir> Machine<'mir, 'tcx> for Evaluator<'tcx> { panic!("remove this function from rustc"); } - fn check_locks<'a>( - _mem: &Memory<'a, 'mir, 'tcx, Self>, - _ptr: Pointer, - _size: Size, - _access: AccessKind, - ) -> EvalResult<'tcx> { - Ok(()) - } - - fn add_lock<'a>( - _mem: &mut Memory<'a, 'mir, 'tcx, Self>, - _id: AllocId, - ) { } - - fn free_lock<'a>( - _mem: &mut Memory<'a, 'mir, 'tcx, Self>, - _id: AllocId, - _len: u64, - ) -> EvalResult<'tcx> { - Ok(()) - } - - fn end_region<'a>( - _ecx: &mut EvalContext<'a, 'mir, 'tcx, Self>, - _reg: Option<::rustc::middle::region::Scope>, - ) -> EvalResult<'tcx> { - Ok(()) - } - fn validation_op<'a>( _ecx: &mut EvalContext<'a, 'mir, 'tcx, Self>, _op: ::rustc::mir::ValidationOp, diff --git a/src/memory.rs b/src/memory.rs index bed0eb28c25..bc4c5b70ece 100644 --- a/src/memory.rs +++ b/src/memory.rs @@ -1,12 +1,24 @@ +use rustc_mir::interpret::IsStatic; -#[derive(Debug, PartialEq, Copy, Clone)] +#[derive(Debug, PartialEq, Copy, Clone, Hash, Eq)] pub enum MemoryKind { - /// Error if deallocated any other way than `rust_deallocate` + /// `__rust_alloc` memory Rust, - /// Error if deallocated any other way than `free` + /// `malloc` memory C, /// Part of env var emulation Env, + // mutable statics + MutStatic, +} + +impl IsStatic for MemoryKind { + fn is_static(self) -> bool { + match self { + MemoryKind::MutStatic => true, + _ => false, + } + } } impl Into<::rustc_mir::interpret::MemoryKind> for MemoryKind { diff --git a/src/tls.rs b/src/tls.rs index 878884065bb..955ecb22568 100644 --- a/src/tls.rs +++ b/src/tls.rs @@ -134,7 +134,7 @@ impl<'a, 'mir, 'tcx: 'mir + 'a> EvalContextExt<'tcx> for EvalContext<'a, 'mir, ' self.write_scalar(ptr, dest)?; // step until out of stackframes - while self.step()? {} + self.run()?; dtor = match self.memory.fetch_tls_dtor(Some(key)) { dtor @ Some(_) => dtor, diff --git a/tests/compile-fail/memleak.rs b/tests/compile-fail/memleak.rs index c03cf50eb27..71b4e2f442f 100644 --- a/tests/compile-fail/memleak.rs +++ b/tests/compile-fail/memleak.rs @@ -1,4 +1,3 @@ -// ignore-test FIXME: leak detection is disabled //error-pattern: the evaluated program leaked memory fn main() { diff --git a/tests/compile-fail/memleak_rc.rs b/tests/compile-fail/memleak_rc.rs index da3a58118a2..b2bc6722afb 100644 --- a/tests/compile-fail/memleak_rc.rs +++ b/tests/compile-fail/memleak_rc.rs @@ -1,4 +1,3 @@ -// ignore-test FIXME: leak detection is disabled //error-pattern: the evaluated program leaked memory use std::rc::Rc; diff --git a/tests/compile-fail/modifying_constants.rs b/tests/compile-fail/modifying_constants.rs index 6ffba89fa5e..ba46651f58e 100644 --- a/tests/compile-fail/modifying_constants.rs +++ b/tests/compile-fail/modifying_constants.rs @@ -1,4 +1,3 @@ -// ignore-test FIXME: we are not making these statics read-only any more? fn main() { let x = &1; // the `&1` is promoted to a constant, but it used to be that only the pointer is marked static, not the pointee diff --git a/tests/compile-fail/static_memory_modification.rs b/tests/compile-fail/static_memory_modification.rs index e28bcb37fb7..9e39c2c01c2 100644 --- a/tests/compile-fail/static_memory_modification.rs +++ b/tests/compile-fail/static_memory_modification.rs @@ -1,4 +1,3 @@ -// ignore-test FIXME: we are not making these statics read-only any more? static X: usize = 5; #[allow(mutable_transmutes)] diff --git a/tests/run-pass/pthreads.rs b/tests/run-pass/pthreads.rs new file mode 100644 index 00000000000..ec7e082a5d5 --- /dev/null +++ b/tests/run-pass/pthreads.rs @@ -0,0 +1,14 @@ +// Just instantiate some data structures to make sure we got all their foreign items covered + +use std::sync; + +fn main() { + let m = sync::Mutex::new(0); + let _ = m.lock(); + drop(m); + + let rw = sync::RwLock::new(0); + let _ = rw.read(); + let _ = rw.write(); + drop(rw); +}