diff --git a/benches/helpers/miri_helper.rs b/benches/helpers/miri_helper.rs index 2483f2de3a8..b478a7d23eb 100644 --- a/benches/helpers/miri_helper.rs +++ b/benches/helpers/miri_helper.rs @@ -29,16 +29,7 @@ fn after_analysis<'tcx>( tcx.entry_fn(LOCAL_CRATE).expect("no main or start function found"); self.bencher.iter(|| { - let config = miri::MiriConfig { - validate: true, - stacked_borrows: true, - communicate: false, - ignore_leaks: false, - excluded_env_vars: vec![], - args: vec![], - seed: None, - tracked_pointer_tag: None, - }; + let config = miri::MiriConfig::default(); eval_main(tcx, entry_def_id, config); }); }); diff --git a/src/bin/miri-rustc-tests.rs b/src/bin/miri-rustc-tests.rs index 9cc9901a913..509a1592152 100644 --- a/src/bin/miri-rustc-tests.rs +++ b/src/bin/miri-rustc-tests.rs @@ -46,16 +46,7 @@ fn visit_item(&mut self, i: &'hir hir::Item) { .iter() .any(|attr| attr.check_name(rustc_span::symbol::sym::test)) { - let config = MiriConfig { - validate: true, - stacked_borrows: true, - communicate: false, - ignore_leaks: false, - excluded_env_vars: vec![], - args: vec![], - seed: None, - tracked_pointer_tag: None, - }; + let config = MiriConfig::default(); let did = self.0.hir().body_owner_def_id(body_id); println!("running test: {}", self.0.def_path_debug_str(did)); miri::eval_main(self.0, did, config); @@ -68,16 +59,7 @@ fn visit_impl_item(&mut self, _impl_item: &'hir hir::ImplItem) {} } tcx.hir().krate().visit_all_item_likes(&mut Visitor(tcx)); } else if let Some((entry_def_id, _)) = tcx.entry_fn(LOCAL_CRATE) { - let config = MiriConfig { - validate: true, - stacked_borrows: true, - communicate: false, - ignore_leaks: false, - excluded_env_vars: vec![], - args: vec![], - seed: None, - tracked_pointer_tag: None, - }; + let config = MiriConfig::default(); miri::eval_main(tcx, entry_def_id, config); compiler.session().abort_if_errors(); diff --git a/src/bin/miri.rs b/src/bin/miri.rs index e69e7f7b6f5..d2709643237 100644 --- a/src/bin/miri.rs +++ b/src/bin/miri.rs @@ -135,6 +135,7 @@ fn main() { let mut ignore_leaks = false; let mut seed: Option = None; let mut tracked_pointer_tag: Option = None; + let mut tracked_alloc_id: Option = None; let mut rustc_args = vec![]; let mut miri_args = vec![]; let mut after_dashdash = false; @@ -206,6 +207,17 @@ fn main() { panic!("-Zmiri-track-pointer-tag must be a nonzero id"); } } + arg if arg.starts_with("-Zmiri-track-alloc-id=") => { + let id: u64 = match arg.trim_start_matches("-Zmiri-track-alloc-id=").parse() + { + Ok(id) => id, + Err(err) => panic!( + "-Zmiri-track-alloc-id requires a valid `u64` as the argument: {}", + err + ), + }; + tracked_alloc_id = Some(miri::AllocId(id)); + } _ => { rustc_args.push(arg); } @@ -240,6 +252,7 @@ fn main() { seed, args: miri_args, tracked_pointer_tag, + tracked_alloc_id, }; rustc_driver::install_ice_hook(); let result = rustc_driver::catch_fatal_errors(move || { diff --git a/src/eval.rs b/src/eval.rs index 267b79d0eba..1981a8d1e03 100644 --- a/src/eval.rs +++ b/src/eval.rs @@ -30,6 +30,24 @@ pub struct MiriConfig { pub seed: Option, /// The stacked borrow id to report about pub tracked_pointer_tag: Option, + /// The allocation id to report about. + pub tracked_alloc_id: Option, +} + +impl Default for MiriConfig { + fn default() -> MiriConfig { + MiriConfig { + validate: true, + stacked_borrows: true, + communicate: false, + ignore_leaks: false, + excluded_env_vars: vec![], + args: vec![], + seed: None, + tracked_pointer_tag: None, + tracked_alloc_id: None, + } + } } /// Details of premature program termination. @@ -55,6 +73,7 @@ pub fn create_ecx<'mir, 'tcx: 'mir>( StdRng::seed_from_u64(config.seed.unwrap_or(0)), config.stacked_borrows, config.tracked_pointer_tag, + config.tracked_alloc_id, ), ); // Complete initialization. diff --git a/src/machine.rs b/src/machine.rs index 0df567a2fa5..07885148e1f 100644 --- a/src/machine.rs +++ b/src/machine.rs @@ -75,15 +75,19 @@ pub struct MemoryExtra { pub intptrcast: intptrcast::MemoryExtra, /// Mapping extern static names to their canonical allocation. - pub(crate) extern_statics: FxHashMap, + extern_statics: FxHashMap, /// The random number generator used for resolving non-determinism. /// Needs to be queried by ptr_to_int, hence needs interior mutability. pub(crate) rng: RefCell, + + /// An allocation ID to report when it is being allocated + /// (helps for debugging memory leaks). + tracked_alloc_id: Option, } impl MemoryExtra { - pub fn new(rng: StdRng, stacked_borrows: bool, tracked_pointer_tag: Option) -> Self { + pub fn new(rng: StdRng, stacked_borrows: bool, tracked_pointer_tag: Option, tracked_alloc_id: Option) -> Self { let stacked_borrows = if stacked_borrows { Some(Rc::new(RefCell::new(stacked_borrows::GlobalState::new(tracked_pointer_tag)))) } else { @@ -94,6 +98,7 @@ pub fn new(rng: StdRng, stacked_borrows: bool, tracked_pointer_tag: Option