Skip to content

Commit c96fa5e

Browse files
committed
add_retag: ensure box-to-raw-ptr casts are preserved for Miri
1 parent a42873e commit c96fa5e

File tree

10 files changed

+91
-33
lines changed

10 files changed

+91
-33
lines changed

compiler/rustc_const_eval/src/transform/validate.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,7 @@ impl<'a, 'tcx> Visitor<'tcx> for CfgChecker<'a, 'tcx> {
341341
// FIXME(JakobDegen) The validator should check that `self.mir_phase <
342342
// DropsLowered`. However, this causes ICEs with generation of drop shims, which
343343
// seem to fail to set their `MirPhase` correctly.
344-
if matches!(kind, RetagKind::Raw | RetagKind::TwoPhase) {
344+
if matches!(kind, RetagKind::TwoPhase) {
345345
self.fail(location, format!("explicit `{kind:?}` is forbidden"));
346346
}
347347
}
@@ -1272,7 +1272,7 @@ impl<'a, 'tcx> Visitor<'tcx> for TypeChecker<'a, 'tcx> {
12721272
// FIXME(JakobDegen) The validator should check that `self.mir_phase <
12731273
// DropsLowered`. However, this causes ICEs with generation of drop shims, which
12741274
// seem to fail to set their `MirPhase` correctly.
1275-
if matches!(kind, RetagKind::Raw | RetagKind::TwoPhase) {
1275+
if matches!(kind, RetagKind::TwoPhase) {
12761276
self.fail(location, format!("explicit `{kind:?}` is forbidden"));
12771277
}
12781278
}

compiler/rustc_mir_transform/src/add_retag.rs

+30-6
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ fn may_contain_reference<'tcx>(ty: Ty<'tcx>, depth: u32, tcx: TyCtxt<'tcx>) -> b
2424
| ty::Str
2525
| ty::FnDef(..)
2626
| ty::Never => false,
27-
// References
27+
// References and Boxes (`noalias` sources)
2828
ty::Ref(..) => true,
2929
ty::Adt(..) if ty.is_box() => true,
3030
ty::Adt(adt, _) if Some(adt.did()) == tcx.lang_items().ptr_unique() => true,
@@ -125,15 +125,39 @@ impl<'tcx> MirPass<'tcx> for AddRetag {
125125
for i in (0..block_data.statements.len()).rev() {
126126
let (retag_kind, place) = match block_data.statements[i].kind {
127127
// Retag after assignments of reference type.
128-
StatementKind::Assign(box (ref place, ref rvalue)) if needs_retag(place) => {
128+
StatementKind::Assign(box (ref place, ref rvalue)) => {
129129
let add_retag = match rvalue {
130130
// Ptr-creating operations already do their own internal retagging, no
131131
// need to also add a retag statement.
132-
Rvalue::Ref(..) | Rvalue::AddressOf(..) => false,
133-
_ => true,
132+
// *Except* if we are deref'ing a Box, because those get desugared to directly working
133+
// with the inner raw pointer! That's relevant for `AddressOf` as Miri otherwise makes it
134+
// a NOP when the original pointer is already raw.
135+
Rvalue::AddressOf(_mutbl, place) => {
136+
// Using `is_box_global` here is a bit sketchy: if this code is
137+
// generic over the allocator, we'll not add a retag! This is a hack
138+
// to make Stacked Borrows compatible with custom allocator code.
139+
// Long-term, we'll want to move to an aliasing model where "cast to
140+
// raw pointer" is a complete NOP, and then this will no longer be
141+
// an issue.
142+
if place.is_indirect_first_projection()
143+
&& body.local_decls[place.local].ty.is_box_global(tcx)
144+
{
145+
Some(RetagKind::Raw)
146+
} else {
147+
None
148+
}
149+
}
150+
Rvalue::Ref(..) => None,
151+
_ => {
152+
if needs_retag(place) {
153+
Some(RetagKind::Default)
154+
} else {
155+
None
156+
}
157+
}
134158
};
135-
if add_retag {
136-
(RetagKind::Default, *place)
159+
if let Some(kind) = add_retag {
160+
(kind, *place)
137161
} else {
138162
continue;
139163
}

compiler/rustc_mir_transform/src/lib.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -529,11 +529,11 @@ fn run_runtime_lowering_passes<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
529529
// AddMovesForPackedDrops needs to run after drop
530530
// elaboration.
531531
&add_moves_for_packed_drops::AddMovesForPackedDrops,
532-
// `AddRetag` needs to run after `ElaborateDrops`. Otherwise it should run fairly late,
532+
// `AddRetag` needs to run after `ElaborateDrops` but before `ElaborateBoxDerefs`. Otherwise it should run fairly late,
533533
// but before optimizations begin.
534+
&add_retag::AddRetag,
534535
&elaborate_box_derefs::ElaborateBoxDerefs,
535536
&coroutine::StateTransform,
536-
&add_retag::AddRetag,
537537
&Lint(known_panics_lint::KnownPanicsLint),
538538
];
539539
pm::run_passes_no_validate(tcx, body, passes, Some(MirPhase::Runtime(RuntimePhase::Initial)));

library/alloc/src/boxed.rs

+6-11
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,6 @@ use core::error::Error;
155155
use core::fmt;
156156
use core::future::Future;
157157
use core::hash::{Hash, Hasher};
158-
use core::intrinsics::retag_box_to_raw;
159158
use core::iter::FusedIterator;
160159
use core::marker::Tuple;
161160
use core::marker::Unsize;
@@ -165,7 +164,7 @@ use core::ops::{
165164
CoerceUnsized, Coroutine, CoroutineState, Deref, DerefMut, DispatchFromDyn, Receiver,
166165
};
167166
use core::pin::Pin;
168-
use core::ptr::{self, NonNull, Unique};
167+
use core::ptr::{self, addr_of_mut, NonNull, Unique};
169168
use core::task::{Context, Poll};
170169

171170
#[cfg(not(no_global_oom_handling))]
@@ -1111,16 +1110,12 @@ impl<T: ?Sized, A: Allocator> Box<T, A> {
11111110
#[unstable(feature = "allocator_api", issue = "32838")]
11121111
#[inline]
11131112
pub fn into_raw_with_allocator(b: Self) -> (*mut T, A) {
1114-
// This is the transition point from `Box` to raw pointers. For Stacked Borrows, these casts
1115-
// are relevant -- if this is a global allocator Box and we just get the pointer from `b.0`,
1116-
// it will have `Unique` permission, which is not what we want from a raw pointer. We could
1117-
// fix that by going through `&mut`, but then if this is *not* a global allocator Box, we'd
1118-
// be adding uniqueness assertions that we do not want. So for Miri's sake we pass this
1119-
// pointer through an intrinsic for box-to-raw casts, which can do the right thing wrt the
1120-
// aliasing model.
1121-
let b = mem::ManuallyDrop::new(b);
1113+
let mut b = mem::ManuallyDrop::new(b);
1114+
// We carefully get the raw pointer out in a way that Miri's aliasing model understands what
1115+
// is happening: using the primitive "deref" of `Box`.
1116+
let ptr = addr_of_mut!(**b);
11221117
let alloc = unsafe { ptr::read(&b.1) };
1123-
(unsafe { retag_box_to_raw::<T, A>(b.0.as_ptr()) }, alloc)
1118+
(ptr, alloc)
11241119
}
11251120

11261121
#[unstable(

src/tools/miri/src/borrow_tracker/stacked_borrows/mod.rs

+10-10
Original file line numberDiff line numberDiff line change
@@ -891,9 +891,9 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
891891
let this = self.eval_context_mut();
892892
let retag_fields = this.machine.borrow_tracker.as_mut().unwrap().get_mut().retag_fields;
893893
let retag_cause = match kind {
894-
RetagKind::Raw | RetagKind::TwoPhase { .. } => unreachable!(), // these can only happen in `retag_ptr_value`
894+
RetagKind::TwoPhase { .. } => unreachable!(), // can only happen in `retag_ptr_value`
895895
RetagKind::FnEntry => RetagCause::FnEntry,
896-
RetagKind::Default => RetagCause::Normal,
896+
RetagKind::Default | RetagKind::Raw => RetagCause::Normal,
897897
};
898898
let mut visitor =
899899
RetagVisitor { ecx: this, kind, retag_cause, retag_fields, in_field: false };
@@ -959,14 +959,14 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
959959

960960
// Check the type of this value to see what to do with it (retag, or recurse).
961961
match place.layout.ty.kind() {
962-
ty::Ref(..) => {
963-
let new_perm =
964-
NewPermission::from_ref_ty(place.layout.ty, self.kind, self.ecx);
965-
self.retag_ptr_inplace(place, new_perm)?;
966-
}
967-
ty::RawPtr(..) => {
968-
// We do *not* want to recurse into raw pointers -- wide raw pointers have
969-
// fields, and for dyn Trait pointees those can have reference type!
962+
ty::Ref(..) | ty::RawPtr(..) => {
963+
if matches!(place.layout.ty.kind(), ty::Ref(..))
964+
|| self.kind == RetagKind::Raw
965+
{
966+
let new_perm =
967+
NewPermission::from_ref_ty(place.layout.ty, self.kind, self.ecx);
968+
self.retag_ptr_inplace(place, new_perm)?;
969+
}
970970
}
971971
ty::Adt(adt, _) if adt.is_box() => {
972972
// Recurse for boxes, they require some tricky handling and will end up in `visit_box` above.

src/tools/miri/tests/fail/both_borrows/newtype_pair_retagging.stack.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ LL | Box(unsafe { Unique::new_unchecked(raw) }, alloc)
66
|
77
= help: this indicates a potential bug in the program: it performed an invalid operation, but the Stacked Borrows rules it violated are still experimental
88
= help: see https://github.com/rust-lang/unsafe-code-guidelines/blob/master/wip/stacked-borrows.md for further information
9-
help: <TAG> was created by a SharedReadWrite retag at offsets [0x0..0x4]
9+
help: <TAG> was created by a Unique retag at offsets [0x0..0x4]
1010
--> $DIR/newtype_pair_retagging.rs:LL:CC
1111
|
1212
LL | let ptr = Box::into_raw(Box::new(0i32));

src/tools/miri/tests/fail/both_borrows/newtype_retagging.stack.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ LL | Box(unsafe { Unique::new_unchecked(raw) }, alloc)
66
|
77
= help: this indicates a potential bug in the program: it performed an invalid operation, but the Stacked Borrows rules it violated are still experimental
88
= help: see https://github.com/rust-lang/unsafe-code-guidelines/blob/master/wip/stacked-borrows.md for further information
9-
help: <TAG> was created by a SharedReadWrite retag at offsets [0x0..0x4]
9+
help: <TAG> was created by a Unique retag at offsets [0x0..0x4]
1010
--> $DIR/newtype_retagging.rs:LL:CC
1111
|
1212
LL | let ptr = Box::into_raw(Box::new(0i32));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// MIR for `box_to_raw_mut` after SimplifyCfg-pre-optimizations
2+
3+
fn box_to_raw_mut(_1: &mut Box<i32>) -> *mut i32 {
4+
debug x => _1;
5+
let mut _0: *mut i32;
6+
let mut _2: std::boxed::Box<i32>;
7+
let mut _3: *const i32;
8+
9+
bb0: {
10+
Retag([fn entry] _1);
11+
_2 = deref_copy (*_1);
12+
_3 = (((_2.0: std::ptr::Unique<i32>).0: std::ptr::NonNull<i32>).0: *const i32);
13+
_0 = &raw mut (*_3);
14+
Retag([raw] _0);
15+
return;
16+
}
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// MIR for `box_to_raw_mut` after SimplifyCfg-pre-optimizations
2+
3+
fn box_to_raw_mut(_1: &mut Box<i32>) -> *mut i32 {
4+
debug x => _1;
5+
let mut _0: *mut i32;
6+
let mut _2: std::boxed::Box<i32>;
7+
let mut _3: *const i32;
8+
9+
bb0: {
10+
Retag([fn entry] _1);
11+
_2 = deref_copy (*_1);
12+
_3 = (((_2.0: std::ptr::Unique<i32>).0: std::ptr::NonNull<i32>).0: *const i32);
13+
_0 = &raw mut (*_3);
14+
Retag([raw] _0);
15+
return;
16+
}
17+
}

tests/mir-opt/retag.rs

+5
Original file line numberDiff line numberDiff line change
@@ -65,3 +65,8 @@ fn array_casts() {
6565
let p = &x as *const usize;
6666
assert_eq!(unsafe { *p.add(1) }, 1);
6767
}
68+
69+
// EMIT_MIR retag.box_to_raw_mut.SimplifyCfg-pre-optimizations.after.mir
70+
fn box_to_raw_mut(x: &mut Box<i32>) -> *mut i32 {
71+
std::ptr::addr_of_mut!(**x)
72+
}

0 commit comments

Comments
 (0)