Skip to content

Commit 0ee4358

Browse files
Rollup merge of #145744 - RalfJung:miri-inplace-aliasing, r=compiler-errors
miri: also detect aliasing of in-place argument and return place This is a follow-up to rust-lang/rust#145585 where I forgot to deal with the case of the return place aliasing an in-place argument -- as ``@Amanieu`` mentioned in rust-lang/rust#71117 (comment), that case must also be forbidden. r? ``@compiler-errors``
2 parents 9c044db + b19df5c commit 0ee4358

File tree

4 files changed

+95
-0
lines changed

4 files changed

+95
-0
lines changed

tests/fail/function_calls/arg_inplace_locals_alias.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
//! Ensure we detect aliasing of two in-place arguments for the tricky case where they do not
2+
//! live in memory.
13
//@revisions: stack tree
24
//@[tree]compile-flags: -Zmiri-tree-borrows
35
// Validation forces more things into memory, which we can't have here.
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
//! Ensure we detect aliasing of a in-place argument with the return place for the tricky case where
2+
//! they do not live in memory.
3+
//@revisions: stack tree
4+
//@[tree]compile-flags: -Zmiri-tree-borrows
5+
// Validation forces more things into memory, which we can't have here.
6+
//@compile-flags: -Zmiri-disable-validation
7+
#![feature(custom_mir, core_intrinsics)]
8+
use std::intrinsics::mir::*;
9+
10+
#[allow(unused)]
11+
pub struct S(i32);
12+
13+
#[custom_mir(dialect = "runtime", phase = "optimized")]
14+
fn main() {
15+
mir! {
16+
let _unit: ();
17+
{
18+
let staging = S(42); // This forces `staging` into memory...
19+
let _non_copy = staging; // ... so we move it to a non-inmemory local here.
20+
// This specifically uses a type with scalar representation to tempt Miri to use the
21+
// efficient way of storing local variables (outside adressable memory).
22+
Call(_non_copy = callee(Move(_non_copy)), ReturnTo(after_call), UnwindContinue())
23+
//~[stack]^ ERROR: not granting access
24+
//~[tree]| ERROR: /reborrow .* forbidden/
25+
}
26+
after_call = {
27+
Return()
28+
}
29+
}
30+
}
31+
32+
pub fn callee(x: S) -> S {
33+
x
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_ret.rs:LL:CC
3+
|
4+
LL | Call(_non_copy = callee(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_ret.rs:LL:CC
11+
|
12+
LL | Call(_non_copy = callee(Move(_non_copy)), ReturnTo(after_call), UnwindContinue())
13+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
14+
help: <TAG> is this argument
15+
--> tests/fail/function_calls/arg_inplace_locals_alias_ret.rs:LL:CC
16+
|
17+
LL | x
18+
| ^
19+
= note: BACKTRACE (of the first span):
20+
= note: inside `main` at tests/fail/function_calls/arg_inplace_locals_alias_ret.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: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
error: Undefined Behavior: reborrow through <TAG> (root of the allocation) at ALLOC[0x0] is forbidden
2+
--> tests/fail/function_calls/arg_inplace_locals_alias_ret.rs:LL:CC
3+
|
4+
LL | Call(_non_copy = callee(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: see https://github.com/rust-lang/unsafe-code-guidelines/blob/master/wip/tree-borrows.md for further information
9+
= help: the accessed tag <TAG> (root of the allocation) is foreign to the protected tag <TAG> (i.e., it is not a child)
10+
= help: this reborrow (acting as a foreign read access) would cause the protected tag <TAG> (currently Active) to become Disabled
11+
= help: protected tags must never be Disabled
12+
help: the accessed tag <TAG> was created here
13+
--> tests/fail/function_calls/arg_inplace_locals_alias_ret.rs:LL:CC
14+
|
15+
LL | Call(_non_copy = callee(Move(_non_copy)), ReturnTo(after_call), UnwindContinue())
16+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
17+
help: the protected tag <TAG> was created here, in the initial state Reserved
18+
--> tests/fail/function_calls/arg_inplace_locals_alias_ret.rs:LL:CC
19+
|
20+
LL | x
21+
| ^
22+
help: the protected tag <TAG> later transitioned to Active due to a child write access at offsets [0x0..0x4]
23+
--> tests/fail/function_calls/arg_inplace_locals_alias_ret.rs:LL:CC
24+
|
25+
LL | x
26+
| ^
27+
= help: this transition corresponds to the first write to a 2-phase borrowed mutable reference
28+
= note: BACKTRACE (of the first span):
29+
= note: inside `main` at tests/fail/function_calls/arg_inplace_locals_alias_ret.rs:LL:CC
30+
31+
note: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace
32+
33+
error: aborting due to 1 previous error
34+

0 commit comments

Comments
 (0)