Skip to content

Commit f9b6d4b

Browse files
committed
miri: detect passing the same local twice as an in-place argument
1 parent 4aa1112 commit f9b6d4b

File tree

3 files changed

+92
-0
lines changed

3 files changed

+92
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
//@revisions: stack tree
2+
//@[tree]compile-flags: -Zmiri-tree-borrows
3+
// Validation forces more things into memory, which we can't have here.
4+
//@compile-flags: -Zmiri-disable-validation
5+
#![feature(custom_mir, core_intrinsics)]
6+
use std::intrinsics::mir::*;
7+
8+
pub struct S(i32);
9+
10+
#[custom_mir(dialect = "runtime", phase = "optimized")]
11+
fn main() {
12+
mir! {
13+
let _unit: ();
14+
{
15+
let staging = S(42); // This forces `staging` into memory...
16+
let non_copy = staging; // ... so we move it to a non-inmemory local here.
17+
// This specifically uses a type with scalar representation to tempt Miri to use the
18+
// efficient way of storing local variables (outside adressable memory).
19+
Call(_unit = callee(Move(non_copy), Move(non_copy)), ReturnTo(after_call), UnwindContinue())
20+
//~[stack]^ ERROR: not granting access
21+
//~[tree]| ERROR: /read access .* forbidden/
22+
}
23+
after_call = {
24+
Return()
25+
}
26+
}
27+
}
28+
29+
pub fn callee(x: S, mut y: S) {
30+
// With the setup above, if `x` and `y` are both moved,
31+
// then writing to `y` will change the value stored in `x`!
32+
y.0 = 0;
33+
assert_eq!(x.0, 42);
34+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
error: Undefined Behavior: not granting access to tag <TAG> because that would remove [Unique for <TAG>] which is strongly protected
2+
--> tests/fail/function_calls/arg_inplace_locals_alias.rs:LL:CC
3+
|
4+
LL | Call(_unit = callee(Move(non_copy), Move(non_copy)), ReturnTo(after_call), UnwindContinue())
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Undefined Behavior occurred here
6+
|
7+
= help: this indicates a potential bug in the program: it performed an invalid operation, but the Stacked Borrows rules it violated are still experimental
8+
= help: see https://github.com/rust-lang/unsafe-code-guidelines/blob/master/wip/stacked-borrows.md for further information
9+
help: <TAG> was created here, as the root tag for ALLOC
10+
--> tests/fail/function_calls/arg_inplace_locals_alias.rs:LL:CC
11+
|
12+
LL | Call(_unit = callee(Move(non_copy), Move(non_copy)), ReturnTo(after_call), UnwindContinue())
13+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
14+
help: <TAG> is this argument
15+
--> tests/fail/function_calls/arg_inplace_locals_alias.rs:LL:CC
16+
|
17+
LL | y.0 = 0;
18+
| ^^^^^^^
19+
= note: BACKTRACE (of the first span):
20+
= note: inside `main` at tests/fail/function_calls/arg_inplace_locals_alias.rs:LL:CC
21+
22+
note: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace
23+
24+
error: aborting due to 1 previous error
25+
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
error: Undefined Behavior: read access through <TAG> (root of the allocation) at ALLOC[0x0] is forbidden
2+
--> tests/fail/function_calls/arg_inplace_locals_alias.rs:LL:CC
3+
|
4+
LL | Call(_unit = callee(Move(non_copy), Move(non_copy)), ReturnTo(after_call), UnwindContinue())
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Undefined Behavior occurred here
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 read 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+
--> tests/fail/function_calls/arg_inplace_locals_alias.rs:LL:CC
13+
|
14+
LL | Call(_unit = callee(Move(non_copy), Move(non_copy)), ReturnTo(after_call), UnwindContinue())
15+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
16+
help: the protected tag <TAG> was created here, in the initial state Reserved
17+
--> tests/fail/function_calls/arg_inplace_locals_alias.rs:LL:CC
18+
|
19+
LL | y.0 = 0;
20+
| ^^^^^^^
21+
help: the protected tag <TAG> later transitioned to Active due to a child write access at offsets [0x0..0x4]
22+
--> tests/fail/function_calls/arg_inplace_locals_alias.rs:LL:CC
23+
|
24+
LL | y.0 = 0;
25+
| ^^^^^^^
26+
= help: this transition corresponds to the first write to a 2-phase borrowed mutable reference
27+
= note: BACKTRACE (of the first span):
28+
= note: inside `main` at tests/fail/function_calls/arg_inplace_locals_alias.rs:LL:CC
29+
30+
note: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace
31+
32+
error: aborting due to 1 previous error
33+

0 commit comments

Comments
 (0)