Skip to content

Commit a73c86d

Browse files
committed
fix return place protection when the place is given as a local
1 parent 3d9e8f1 commit a73c86d

File tree

3 files changed

+71
-1
lines changed

3 files changed

+71
-1
lines changed

src/tools/miri/src/machine.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1219,7 +1219,8 @@ impl<'mir, 'tcx> Machine<'mir, 'tcx> for MiriMachine<'mir, 'tcx> {
12191219
// If we have a borrow tracker, we also have it set up protection so that all reads *and
12201220
// writes* during this call are insta-UB.
12211221
if ecx.machine.borrow_tracker.is_some() {
1222-
if let Either::Left(place) = place.as_mplace_or_local() {
1222+
// Have to do `to_op` first because a `Place::Local` doesn't imply the local doesn't have an address.
1223+
if let Either::Left(place) = ecx.place_to_op(place)?.as_mplace_or_imm() {
12231224
ecx.protect_place(&place)?;
12241225
} else {
12251226
// Locals that don't have their address taken are as protected as they can ever be.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
//@compile-flags: -Zmiri-tree-borrows
2+
#![feature(raw_ref_op)]
3+
#![feature(core_intrinsics)]
4+
#![feature(custom_mir)]
5+
6+
use std::intrinsics::mir::*;
7+
8+
#[custom_mir(dialect = "runtime", phase = "optimized")]
9+
pub fn main() {
10+
mir! {
11+
{
12+
let x = 0;
13+
let ptr = &raw mut x;
14+
// We arrange for `myfun` to have a pointer that aliases
15+
// its return place. Even just reading from that pointer is UB.
16+
Call(x, after_call, myfun(ptr))
17+
}
18+
19+
after_call = {
20+
Return()
21+
}
22+
}
23+
}
24+
25+
fn myfun(ptr: *mut i32) -> i32 {
26+
// This overwrites the return place, which shouldn't be possible through another pointer.
27+
unsafe { ptr.write(0) };
28+
//~^ ERROR: /write access .* forbidden/
29+
13
30+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
error: Undefined Behavior: write access through <TAG> (root of the allocation) is forbidden
2+
--> $DIR/return_pointer_aliasing2.rs:LL:CC
3+
|
4+
LL | unsafe { ptr.write(0) };
5+
| ^^^^^^^^^^^^ write access through <TAG> (root of the allocation) is forbidden
6+
|
7+
= help: this indicates a potential bug in the program: it performed an invalid operation, but the Tree Borrows rules it violated are still experimental
8+
= help: the accessed tag <TAG> (root of the allocation) is foreign to the protected tag <TAG> (i.e., it is not a child)
9+
= help: this foreign write access would cause the protected tag <TAG> (currently Active) to become Disabled
10+
= help: protected tags must never be Disabled
11+
help: the accessed tag <TAG> was created here
12+
--> $DIR/return_pointer_aliasing2.rs:LL:CC
13+
|
14+
LL | / mir! {
15+
LL | | {
16+
LL | | let x = 0;
17+
LL | | let ptr = &raw mut x;
18+
... |
19+
LL | | }
20+
LL | | }
21+
| |_____^
22+
help: the protected tag <TAG> was created here, in the initial state Active
23+
--> $DIR/return_pointer_aliasing2.rs:LL:CC
24+
|
25+
LL | unsafe { ptr.write(0) };
26+
| ^^^^^^^^^^^^^^^^^^^^^^^
27+
= note: BACKTRACE (of the first span):
28+
= note: inside `myfun` at $DIR/return_pointer_aliasing2.rs:LL:CC
29+
note: inside `main`
30+
--> $DIR/return_pointer_aliasing2.rs:LL:CC
31+
|
32+
LL | Call(x, after_call, myfun(ptr))
33+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
34+
= note: this error originates in the macro `::core::intrinsics::mir::__internal_remove_let` which comes from the expansion of the macro `mir` (in Nightly builds, run with -Z macro-backtrace for more info)
35+
36+
note: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace
37+
38+
error: aborting due to previous error
39+

0 commit comments

Comments
 (0)