Skip to content

Commit d363a47

Browse files
committed
Add a scheme to find the place where an id was destroyed
1 parent 048af40 commit d363a47

File tree

8 files changed

+55
-9
lines changed

8 files changed

+55
-9
lines changed

benches/helpers/miri_helper.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ impl rustc_driver::Callbacks for MiriCompilerCalls<'_> {
3232
excluded_env_vars: vec![],
3333
args: vec![],
3434
seed: None,
35+
tracked_id: None,
3536
};
3637
eval_main(tcx, entry_def_id, config);
3738
});

src/bin/miri-rustc-tests.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ impl rustc_driver::Callbacks for MiriCompilerCalls {
4545
excluded_env_vars: vec![],
4646
args: vec![],
4747
seed: None,
48+
tracked_id: None,
4849
};
4950
let did = self.0.hir().body_owner_def_id(body_id);
5051
println!("running test: {}", self.0.def_path_debug_str(did));
@@ -64,7 +65,8 @@ impl rustc_driver::Callbacks for MiriCompilerCalls {
6465
ignore_leaks: false,
6566
excluded_env_vars: vec![],
6667
args: vec![],
67-
seed: None
68+
seed: None,
69+
tracked_id: None,
6870
};
6971
miri::eval_main(tcx, entry_def_id, config);
7072

src/bin/miri.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ fn main() {
126126
let mut communicate = false;
127127
let mut ignore_leaks = false;
128128
let mut seed: Option<u64> = None;
129+
let mut tracked_id: Option<miri::PtrId> = None;
129130
let mut rustc_args = vec![];
130131
let mut miri_args = vec![];
131132
let mut after_dashdash = false;
@@ -176,6 +177,17 @@ fn main() {
176177
arg if arg.starts_with("-Zmiri-env-exclude=") => {
177178
excluded_env_vars.push(arg.trim_start_matches("-Zmiri-env-exclude=").to_owned());
178179
},
180+
arg if arg.starts_with("-Zmiri-track-id=") => {
181+
let id: u64 = match arg.trim_start_matches("-Zmiri-track-id=").parse() {
182+
Ok(id) => id,
183+
Err(err) => panic!("-Zmiri-track-id requires a valid `u64` as the argument: {}", err),
184+
};
185+
if let Some(id) = miri::PtrId::new(id) {
186+
tracked_id = Some(id);
187+
} else {
188+
panic!("-Zmiri-track-id must be a nonzero id");
189+
}
190+
},
179191
_ => {
180192
rustc_args.push(arg);
181193
}
@@ -208,6 +220,7 @@ fn main() {
208220
excluded_env_vars,
209221
seed,
210222
args: miri_args,
223+
tracked_id,
211224
};
212225
rustc_driver::install_ice_hook();
213226
let result = rustc_driver::catch_fatal_errors(move || {

src/eval.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ pub struct MiriConfig {
2626
pub args: Vec<String>,
2727
/// The seed to use when non-determinism or randomness are required (e.g. ptr-to-int cast, `getrandom()`).
2828
pub seed: Option<u64>,
29+
/// The stacked borrow id to report about
30+
pub tracked_id: Option<PtrId>,
2931
}
3032

3133
/// Details of premature program termination.
@@ -47,7 +49,7 @@ pub fn create_ecx<'mir, 'tcx: 'mir>(
4749
tcx.at(syntax::source_map::DUMMY_SP),
4850
ty::ParamEnv::reveal_all(),
4951
Evaluator::new(config.communicate),
50-
MemoryExtra::new(StdRng::seed_from_u64(config.seed.unwrap_or(0)), config.validate),
52+
MemoryExtra::new(StdRng::seed_from_u64(config.seed.unwrap_or(0)), config.validate, config.tracked_id),
5153
);
5254
// Complete initialization.
5355
EnvVars::init(&mut ecx, config.excluded_env_vars);

src/lib.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,10 @@ pub use crate::operator::EvalContextExt as OperatorEvalContextExt;
4343
pub use crate::range_map::RangeMap;
4444
pub use crate::helpers::{EvalContextExt as HelpersEvalContextExt};
4545
pub use crate::mono_hash_map::MonoHashMap;
46-
pub use crate::stacked_borrows::{EvalContextExt as StackedBorEvalContextExt, Tag, Permission, Stack, Stacks, Item};
46+
pub use crate::stacked_borrows::{
47+
EvalContextExt as StackedBorEvalContextExt, Tag, Permission, Stack, Stacks, Item, PtrId,
48+
GlobalState,
49+
};
4750
pub use crate::machine::{
4851
PAGE_SIZE, STACK_ADDR, STACK_SIZE, NUM_CPUS,
4952
MemoryExtra, AllocExtra, FrameData, MiriMemoryKind, Evaluator, MiriEvalContext, MiriEvalContextExt,

src/machine.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,9 @@ pub struct MemoryExtra {
7777
}
7878

7979
impl MemoryExtra {
80-
pub fn new(rng: StdRng, validate: bool) -> Self {
80+
pub fn new(rng: StdRng, validate: bool, tracked_id: Option<PtrId>) -> Self {
8181
MemoryExtra {
82-
stacked_borrows: Default::default(),
82+
stacked_borrows: Rc::new(RefCell::new(GlobalState::new(tracked_id))),
8383
intptrcast: Default::default(),
8484
rng: RefCell::new(rng),
8585
validate,

src/stacked_borrows.rs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,8 @@ pub struct GlobalState {
105105
next_call_id: CallId,
106106
/// Those call IDs corresponding to functions that are still running.
107107
active_calls: HashSet<CallId>,
108+
/// The id to trace in this execution run
109+
tracked_id: Option<PtrId>,
108110
}
109111
/// Memory extra state gives us interior mutable access to the global state.
110112
pub type MemoryExtra = Rc<RefCell<GlobalState>>;
@@ -151,18 +153,17 @@ impl fmt::Display for RefKind {
151153
}
152154

153155
/// Utilities for initialization and ID generation
154-
impl Default for GlobalState {
155-
fn default() -> Self {
156+
impl GlobalState {
157+
pub fn new(tracked_id: Option<PtrId>) -> Self {
156158
GlobalState {
157159
next_ptr_id: NonZeroU64::new(1).unwrap(),
158160
base_ptr_ids: HashMap::default(),
159161
next_call_id: NonZeroU64::new(1).unwrap(),
160162
active_calls: HashSet::default(),
163+
tracked_id,
161164
}
162165
}
163-
}
164166

165-
impl GlobalState {
166167
fn new_ptr(&mut self) -> PtrId {
167168
let id = self.next_ptr_id;
168169
self.next_ptr_id = NonZeroU64::new(id.get() + 1).unwrap();
@@ -312,6 +313,11 @@ impl<'tcx> Stack {
312313
let first_incompatible_idx = self.find_first_write_incompatible(granting_idx);
313314
for item in self.borrows.drain(first_incompatible_idx..).rev() {
314315
trace!("access: popping item {:?}", item);
316+
if let Tag::Tagged(id) = item.tag {
317+
if Some(id) == global.tracked_id {
318+
throw_unsup!(Unsupported(format!("popped id {}", id)));
319+
}
320+
}
315321
Stack::check_protector(&item, Some(tag), global)?;
316322
}
317323
} else {
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// compile-flags: -Zmiri-track-id=1372
2+
// do not run on anything but x86_64 linux, because minute changes can change the borrow stack ids
3+
// only-x86_64
4+
// only-linux
5+
6+
use std::mem;
7+
8+
fn main() {
9+
let mut target = 42;
10+
// Make sure we cannot use a raw-tagged `&mut` pointing to a frozen location.
11+
// Even just creating it unfreezes.
12+
let raw = &mut target as *mut _; // let this leak to raw
13+
let reference = unsafe { &*raw }; // freeze
14+
let ptr = reference as *const _ as *mut i32; // raw ptr, with raw tag
15+
let _mut_ref: &mut i32 = unsafe { mem::transmute(ptr) }; // &mut, with raw tag
16+
//~^ ERROR popped id 1372
17+
// Now we retag, making our ref top-of-stack -- and, in particular, unfreezing.
18+
let _val = *reference;
19+
}

0 commit comments

Comments
 (0)