Skip to content

Commit ca8ca0e

Browse files
authored
Unrolled build for #145744
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 #145585 where I forgot to deal with the case of the return place aliasing an in-place argument -- as ``@Amanieu`` mentioned in #71117 (comment), that case must also be forbidden. r? ``@compiler-errors``
2 parents 69b76df + 78bdd86 commit ca8ca0e

File tree

5 files changed

+111
-7
lines changed

5 files changed

+111
-7
lines changed

compiler/rustc_const_eval/src/interpret/step.rs

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
//!
33
//! The main entry point is the `step` method.
44
5+
use std::iter;
6+
57
use either::Either;
68
use rustc_abi::{FIRST_VARIANT, FieldIdx};
79
use rustc_data_structures::fx::FxHashSet;
@@ -426,6 +428,7 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
426428
terminator: &mir::Terminator<'tcx>,
427429
func: &mir::Operand<'tcx>,
428430
args: &[Spanned<mir::Operand<'tcx>>],
431+
dest: &mir::Place<'tcx>,
429432
) -> InterpResult<'tcx, EvaluatedCalleeAndArgs<'tcx, M>> {
430433
let func = self.eval_operand(func, None)?;
431434

@@ -435,14 +438,20 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
435438
// protection, but then we'd force *a lot* of arguments into memory. So we do some syntactic
436439
// pre-processing here where if all `move` arguments are syntactically distinct local
437440
// variables (and none is indirect), we can skip the in-memory forcing.
441+
// We have to include `dest` in that list so that we can detect aliasing of an in-place
442+
// argument with the return place.
438443
let move_definitely_disjoint = 'move_definitely_disjoint: {
439444
let mut previous_locals = FxHashSet::<mir::Local>::default();
440-
for arg in args {
441-
let mir::Operand::Move(place) = arg.node else {
442-
continue; // we can skip non-`Move` arguments.
443-
};
445+
for place in args
446+
.iter()
447+
.filter_map(|a| {
448+
// We only have to care about `Move` arguments.
449+
if let mir::Operand::Move(place) = &a.node { Some(place) } else { None }
450+
})
451+
.chain(iter::once(dest))
452+
{
444453
if place.is_indirect_first_projection() {
445-
// An indirect `Move` argument could alias with anything else...
454+
// An indirect in-place argument could alias with anything else...
446455
break 'move_definitely_disjoint false;
447456
}
448457
if !previous_locals.insert(place.local) {
@@ -544,7 +553,7 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
544553
let old_loc = self.frame().loc;
545554

546555
let EvaluatedCalleeAndArgs { callee, args, fn_sig, fn_abi, with_caller_location } =
547-
self.eval_callee_and_args(terminator, func, args)?;
556+
self.eval_callee_and_args(terminator, func, args, &destination)?;
548557

549558
let destination = self.eval_place(destination)?;
550559
self.init_fn_call(
@@ -567,7 +576,7 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
567576
let old_frame_idx = self.frame_idx();
568577

569578
let EvaluatedCalleeAndArgs { callee, args, fn_sig, fn_abi, with_caller_location } =
570-
self.eval_callee_and_args(terminator, func, args)?;
579+
self.eval_callee_and_args(terminator, func, args, &mir::Place::return_place())?;
571580

572581
self.init_fn_tail_call(callee, (fn_sig.abi, fn_abi), &args, with_caller_location)?;
573582

src/tools/miri/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)