Skip to content

Commit f361080

Browse files
committed
add_retag: ensure box-to-raw-ptr casts are preserved for Miri
1 parent 38ca6d6 commit f361080

File tree

3 files changed

+12
-12
lines changed

3 files changed

+12
-12
lines changed

src/borrow_tracker/stacked_borrows/mod.rs

Lines changed: 10 additions & 10 deletions
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.

tests/fail/both_borrows/newtype_pair_retagging.stack.stderr

Lines changed: 1 addition & 1 deletion
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));

tests/fail/both_borrows/newtype_retagging.stack.stderr

Lines changed: 1 addition & 1 deletion
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));

0 commit comments

Comments
 (0)