Skip to content

Commit da29c7a

Browse files
committed
Auto merge of #2322 - saethlin:stack-inspection-tools, r=oli-obk
Ideas on getting information about borrow stacks during execution From time to time people ask what some borrow stack looks like in some code. I just know that I am terrible at doing Stacked Borrows by hand, so I always toss together something like this. I know that Miri has logging, but I've never found it particularly useful because there's just too much output. Also I personally don't think about exactly what the state of a borrow stack is, but this seems to be something that newcomers to Stacked Borrows always want. Update: This has been sitting as S-waiting-on-author for a long time. I bring it out from time to time to explain Stacked Borrows to people, and just now `@JakobDegen` said > Can we please merge that btw? It's such a valuable teaching tool > Interfaces can be fixed later I'm inclined to trust Jake's judgement here.
2 parents d2d4f41 + 3d83f53 commit da29c7a

File tree

7 files changed

+79
-0
lines changed

7 files changed

+79
-0
lines changed

README.md

+11
Original file line numberDiff line numberDiff line change
@@ -531,6 +531,17 @@ extern "Rust" {
531531
/// This is internal and unstable and should not be used; we give it here
532532
/// just to be complete.
533533
fn miri_start_panic(payload: *mut u8) -> !;
534+
535+
/// Miri-provided extern function to get the internal unique identifier for the allocation that a pointer
536+
/// points to. This is only useful as an input to `miri_print_stacks`, and it is a separate call because
537+
/// getting a pointer to an allocation at runtime can change the borrow stacks in the allocation.
538+
fn miri_get_alloc_id(ptr: *const ()) -> u64;
539+
540+
/// Miri-provided extern function to print (from the interpreter, not the program) the contents of all
541+
/// borrow stacks in an allocation. The format of what this emits is unstable and may change at any time.
542+
/// In particular, users should be aware that Miri will periodically attempt to garbage collect the
543+
/// contents of all stacks. Callers of this function may wish to pass `-Zmiri-tag-gc=0` to disable the GC.
544+
fn miri_print_stacks(alloc_id: u64);
534545
}
535546
```
536547

src/range_map.rs

+4
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,10 @@ impl<T> RangeMap<T> {
9191
self.v.iter_mut().map(|elem| &mut elem.data)
9292
}
9393

94+
pub fn iter_all(&self) -> impl Iterator<Item = (ops::Range<u64>, &T)> {
95+
self.v.iter().map(|elem| (elem.range.clone(), &elem.data))
96+
}
97+
9498
// Splits the element situated at the given `index`, such that the 2nd one starts at offset
9599
// `split_offset`. Do nothing if the element already starts there.
96100
// Returns whether a split was necessary.

src/shims/foreign_items.rs

+13
Original file line numberDiff line numberDiff line change
@@ -417,6 +417,19 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
417417
// shim, add it to the corresponding submodule.
418418
match link_name.as_str() {
419419
// Miri-specific extern functions
420+
"miri_get_alloc_id" => {
421+
let [ptr] = this.check_shim(abi, Abi::Rust, link_name, args)?;
422+
let ptr = this.read_pointer(ptr)?;
423+
let (alloc_id, _, _) = this.ptr_get_alloc_id(ptr)?;
424+
this.write_scalar(Scalar::from_u64(alloc_id.0.get()), dest)?;
425+
}
426+
"miri_print_stacks" => {
427+
let [id] = this.check_shim(abi, Abi::Rust, link_name, args)?;
428+
let id = this.read_scalar(id)?.to_u64()?;
429+
if let Some(id) = std::num::NonZeroU64::new(id) {
430+
this.print_stacks(AllocId(id))?;
431+
}
432+
}
420433
"miri_static_root" => {
421434
let [ptr] = this.check_shim(abi, Abi::Rust, link_name, args)?;
422435
let ptr = this.read_pointer(ptr)?;

src/stacked_borrows/mod.rs

+15
Original file line numberDiff line numberDiff line change
@@ -1123,4 +1123,19 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
11231123
}
11241124
Ok(())
11251125
}
1126+
1127+
fn print_stacks(&mut self, alloc_id: AllocId) -> InterpResult<'tcx> {
1128+
let this = self.eval_context_mut();
1129+
let alloc_extra = this.get_alloc_extra(alloc_id)?;
1130+
let stacks = alloc_extra.stacked_borrows.as_ref().unwrap().borrow();
1131+
for (range, stack) in stacks.stacks.iter_all() {
1132+
print!("{:?}: [", range);
1133+
for i in 0..stack.len() {
1134+
let item = stack.get(i).unwrap();
1135+
print!(" {:?}{:?}", item.perm(), item.tag());
1136+
}
1137+
println!(" ]");
1138+
}
1139+
Ok(())
1140+
}
11261141
}

tests/compiletest.rs

+2
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,8 @@ regexes! {
139139
STDOUT:
140140
// Windows file paths
141141
r"\\" => "/",
142+
// erase Stacked Borrows tags
143+
"<[0-9]+>" => "<TAG>",
142144
}
143145

144146
regexes! {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
use std::{
2+
alloc::{self, Layout},
3+
mem::ManuallyDrop,
4+
};
5+
6+
extern "Rust" {
7+
fn miri_get_alloc_id(ptr: *const u8) -> u64;
8+
fn miri_print_stacks(alloc_id: u64);
9+
}
10+
11+
fn main() {
12+
let ptr = unsafe { alloc::alloc(Layout::new::<u8>()) };
13+
let alloc_id = unsafe { miri_get_alloc_id(ptr) };
14+
unsafe { miri_print_stacks(alloc_id) };
15+
16+
assert!(!ptr.is_null());
17+
unsafe { miri_print_stacks(alloc_id) };
18+
19+
unsafe { *ptr = 42 };
20+
unsafe { miri_print_stacks(alloc_id) };
21+
22+
let _b = unsafe { ManuallyDrop::new(Box::from_raw(ptr)) };
23+
unsafe { miri_print_stacks(alloc_id) };
24+
25+
let _ptr = unsafe { &*ptr };
26+
unsafe { miri_print_stacks(alloc_id) };
27+
28+
unsafe { alloc::dealloc(ptr, Layout::new::<u8>()) };
29+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
0..1: [ SharedReadWrite<TAG> ]
2+
0..1: [ SharedReadWrite<TAG> ]
3+
0..1: [ SharedReadWrite<TAG> ]
4+
0..1: [ SharedReadWrite<TAG> Unique<TAG> Unique<TAG> Unique<TAG> Unique<TAG> Unique<TAG> ]
5+
0..1: [ SharedReadWrite<TAG> Disabled<TAG> Disabled<TAG> Disabled<TAG> Disabled<TAG> Disabled<TAG> SharedReadOnly<TAG> ]

0 commit comments

Comments
 (0)