Skip to content

Add option to print when an AllocId gets created #1209

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,8 @@ Several `-Z` flags are relevant for Miri:
is popped from a borrow stack (which is where the tag becomes invalid and any
future use of it will error). This helps you in finding out why UB is
happening and where in your code would be a good place to look for it.
* `-Zmiri-track-alloc-id=<id>` shows a backtrace when the given allocation is
being allocated. This helps in debugging memory leaks.

Moreover, Miri recognizes some environment variables:

Expand Down
11 changes: 1 addition & 10 deletions benches/helpers/miri_helper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,7 @@ impl rustc_driver::Callbacks for MiriCompilerCalls<'_> {
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);
});
});
Expand Down
22 changes: 2 additions & 20 deletions src/bin/miri-rustc-tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,7 @@ impl rustc_driver::Callbacks for MiriCompilerCalls {
.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);
Expand All @@ -68,16 +59,7 @@ impl rustc_driver::Callbacks for MiriCompilerCalls {
}
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();
Expand Down
13 changes: 13 additions & 0 deletions src/bin/miri.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ fn main() {
let mut ignore_leaks = false;
let mut seed: Option<u64> = None;
let mut tracked_pointer_tag: Option<miri::PtrId> = None;
let mut tracked_alloc_id: Option<miri::AllocId> = None;
let mut rustc_args = vec![];
let mut miri_args = vec![];
let mut after_dashdash = false;
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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 || {
Expand Down
6 changes: 5 additions & 1 deletion src/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use crate::*;
/// Miri specific diagnostics
pub enum NonHaltingDiagnostic {
PoppedTrackedPointerTag(Item),
CreatedAlloc(AllocId),
}

/// Emit a custom diagnostic without going through the miri-engine machinery
Expand Down Expand Up @@ -97,9 +98,12 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
let this = self.eval_context_ref();
DIAGNOSTICS.with(|diagnostics| {
for e in diagnostics.borrow_mut().drain(..) {
use NonHaltingDiagnostic::*;
let msg = match e {
NonHaltingDiagnostic::PoppedTrackedPointerTag(item) =>
PoppedTrackedPointerTag(item) =>
format!("popped tracked tag for item {:?}", item),
CreatedAlloc(AllocId(id)) =>
format!("created allocation with id {}", id),
};
report_msg(this, msg, false);
}
Expand Down
19 changes: 19 additions & 0 deletions src/eval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,24 @@ pub struct MiriConfig {
pub seed: Option<u64>,
/// The stacked borrow id to report about
pub tracked_pointer_tag: Option<PtrId>,
/// The allocation id to report about.
pub tracked_alloc_id: Option<AllocId>,
}

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.
Expand All @@ -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.
Expand Down
13 changes: 11 additions & 2 deletions src/machine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,15 +75,19 @@ pub struct MemoryExtra {
pub intptrcast: intptrcast::MemoryExtra,

/// Mapping extern static names to their canonical allocation.
pub(crate) extern_statics: FxHashMap<Symbol, AllocId>,
extern_statics: FxHashMap<Symbol, AllocId>,

/// 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<StdRng>,

/// An allocation ID to report when it is being allocated
/// (helps for debugging memory leaks).
tracked_alloc_id: Option<AllocId>,
}

impl MemoryExtra {
pub fn new(rng: StdRng, stacked_borrows: bool, tracked_pointer_tag: Option<PtrId>) -> Self {
pub fn new(rng: StdRng, stacked_borrows: bool, tracked_pointer_tag: Option<PtrId>, tracked_alloc_id: Option<AllocId>) -> Self {
let stacked_borrows = if stacked_borrows {
Some(Rc::new(RefCell::new(stacked_borrows::GlobalState::new(tracked_pointer_tag))))
} else {
Expand All @@ -94,6 +98,7 @@ impl MemoryExtra {
intptrcast: Default::default(),
extern_statics: FxHashMap::default(),
rng: RefCell::new(rng),
tracked_alloc_id,
}
}

Expand Down Expand Up @@ -329,6 +334,10 @@ impl<'mir, 'tcx> Machine<'mir, 'tcx> for Evaluator<'tcx> {
alloc: Cow<'b, Allocation>,
kind: Option<MemoryKind<Self::MemoryKinds>>,
) -> (Cow<'b, Allocation<Self::PointerTag, Self::AllocExtra>>, Self::PointerTag) {
if Some(id) == memory_extra.tracked_alloc_id {
register_diagnostic(NonHaltingDiagnostic::CreatedAlloc(id));
}

let kind = kind.expect("we set our STATIC_KIND so this cannot be None");
let alloc = alloc.into_owned();
let (stacks, base_tag) =
Expand Down