Skip to content

Commit fd959c9

Browse files
authored
Rollup merge of #163121 - Amanieu:inline-dest-rawptr, r=saethlin
Borrow the destination place with a raw pointer in MIR inlining Background: if the destination place in the caller involves a `Deref` or `Index` projection then the inliner will capture a mutable reference to the destination place before the call and write back the return value using that pointer at the end of the call. This is necessary because such projections are unstable and may cause the place to evaluate to a different location at the end of the call. However the use of `&mut` instead of `&raw mut` causes 2 problems: 1. Because call arguments are copied after the destination place is evaluated, if the same place is present in an argument and the destination then it would invalidate the destination reference, causing the later write through it to be UB. 2. Creating a mutable reference to an uninhabited type is immediate UB according to rust-lang/unsafe-code-guidelines#413, and this is enforced by Miri. While the first issue could be addressed by using two-phased borrows or by copying arguments beforehand, a raw pointer is the only way to handle the second. It's also not possible to special-case uninhabited types since it may not be possible to determine whether the destination is inhabited in generic code. r? wg-mir-opt
2 parents f4b7c67 + d3305f1 commit fd959c9

2 files changed

Lines changed: 58 additions & 18 deletions

File tree

‎compiler/rustc_mir_transform/src/inline.rs‎

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -886,24 +886,13 @@ fn inline_call<'tcx, I: Inliner<'tcx>>(
886886
// Place could result in two different locations if `f`
887887
// writes to `i`. To prevent this we need to create a temporary
888888
// borrow of the place and pass the destination as `*temp` instead.
889-
fn dest_needs_borrow(place: Place<'_>) -> bool {
890-
for elem in place.projection.iter() {
891-
match elem {
892-
ProjectionElem::Deref | ProjectionElem::Index(_) => return true,
893-
_ => {}
894-
}
895-
}
896-
897-
false
898-
}
899-
900-
let dest = if dest_needs_borrow(destination) {
889+
//
890+
// This must be a raw pointer: a mutable reference could be invalidated by
891+
// reading the arguments later, and would be invalid if the destination type
892+
// is uninhabited.
893+
let dest = if !destination.is_stable_offset() {
901894
trace!("creating temp for return destination");
902-
let dest = Rvalue::Ref(
903-
tcx.lifetimes.re_erased,
904-
BorrowKind::Mut { kind: MutBorrowKind::Default },
905-
destination,
906-
);
895+
let dest = Rvalue::RawPtr(RawPtrKind::Mut, destination);
907896
let dest_ty = dest.ty(caller_body, tcx);
908897
let temp = Place::from(new_call_temp(caller_body, callsite, dest_ty, return_block));
909898
caller_body[callsite.block].statements.push(Statement::new(

‎tests/mir-opt/inline/indirect_destination.rs‎

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use core::intrinsics::mir::*;
1111
// CHECK-LABEL: fn f(
1212
// CHECK: bb1: {
1313
// CHECK-NEXT: StorageLive([[A:.*]]);
14-
// CHECK-NEXT: [[A]] = &mut (*_1);
14+
// CHECK-NEXT: [[A]] = &raw mut (*_1);
1515
// CHECK-NEXT: StorageLive([[B:.*]]);
1616
// CHECK-NEXT: [[B]] = const 42_u8;
1717
// CHECK-NEXT: (*[[A]]) = move [[B]];
@@ -40,3 +40,54 @@ fn g() -> u8 {
4040
}
4141
}
4242
}
43+
44+
// Reading the argument must not invalidate the saved destination pointer.
45+
#[custom_mir(dialect = "runtime", phase = "initial")]
46+
// CHECK-LABEL: fn indexed(
47+
// CHECK: [[DEST:_[0-9]+]] = &raw mut _2[_1];
48+
// CHECK: [[ARG:_[0-9]+]] = copy _2[_1];
49+
// CHECK-NEXT: [[RET:_[0-9]+]] = copy [[ARG]];
50+
// CHECK-NEXT: (*[[DEST]]) = move [[RET]];
51+
pub fn indexed(index: usize) -> u32 {
52+
mir! {
53+
let values: [u32; 2];
54+
{
55+
values = [41, 99];
56+
Call(values[index] = identity(values[index]), ReturnTo(done), UnwindContinue())
57+
}
58+
done = {
59+
RET = values[index];
60+
Return()
61+
}
62+
}
63+
}
64+
65+
#[inline(always)]
66+
fn identity(value: u32) -> u32 {
67+
value
68+
}
69+
70+
// Saving an uninhabited destination must not construct an invalid reference.
71+
#[custom_mir(dialect = "runtime", phase = "initial")]
72+
// CHECK-LABEL: fn uninhabited(
73+
// CHECK: [[PTR:_[0-9]+]] = &raw mut _1;
74+
// CHECK: [[DEST:_[0-9]+]] = &raw mut (*[[PTR]]);
75+
// CHECK: panic_fmt
76+
pub fn uninhabited() {
77+
mir! {
78+
let slot: !;
79+
let ptr: *mut !;
80+
{
81+
ptr = &raw mut slot;
82+
Call(*ptr = fail(), ReturnTo(done), UnwindContinue())
83+
}
84+
done = {
85+
Return()
86+
}
87+
}
88+
}
89+
90+
#[inline(always)]
91+
fn fail() -> ! {
92+
panic!("expected panic")
93+
}

0 commit comments

Comments
 (0)