Skip to content

Commit f0cbc7d

Browse files
committed
ensure we allow zero-sized references to functions and vtables
1 parent e55f494 commit f0cbc7d

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

Diff for: src/tools/miri/src/borrow_tracker/tree_borrows/mod.rs

+7
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,13 @@ trait EvalContextPrivExt<'mir: 'ecx, 'tcx: 'mir, 'ecx>: crate::MiriInterpCxExt<'
248248
.insert(new_tag, protect);
249249
}
250250

251+
let alloc_kind = this.get_alloc_info(alloc_id).2;
252+
if !matches!(alloc_kind, AllocKind::LiveData) {
253+
// There's not actually any bytes here where accesses could even be tracked.
254+
// Just produce the new provenance, nothing else to do.
255+
return Ok(Some(Provenance::Concrete { alloc_id, tag: new_tag }));
256+
}
257+
251258
let span = this.machine.current_span();
252259
let alloc_extra = this.get_alloc_extra(alloc_id)?;
253260
let range = alloc_range(base_offset, ptr_size);

Diff for: src/tools/miri/tests/pass/strange_references.rs

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
//@revisions: stack tree
2+
//@[tree]compile-flags: -Zmiri-tree-borrows
3+
4+
// Create zero-sized references to vtables and function data.
5+
// Just make sure nothing explodes.
6+
7+
use std::{mem, ptr};
8+
9+
fn check_ref(x: &()) {
10+
let _ptr = ptr::addr_of!(*x);
11+
}
12+
13+
fn main() {
14+
check_ref({
15+
// Create reference to a function.
16+
let fnptr: fn(&()) = check_ref;
17+
unsafe { mem::transmute(fnptr) }
18+
});
19+
check_ref({
20+
// Create reference to a vtable.
21+
let wideptr: &dyn Send = &0;
22+
let fields: (&i32, &()) = unsafe { mem::transmute(wideptr) };
23+
fields.1
24+
})
25+
}

0 commit comments

Comments
 (0)