Skip to content

Commit

Permalink
Miri engine: stronger type-based sanity check for assignments
Browse files Browse the repository at this point in the history
  • Loading branch information
RalfJung committed Apr 2, 2020
1 parent 537ccdf commit f1ea273
Showing 1 changed file with 17 additions and 5 deletions.
22 changes: 17 additions & 5 deletions src/librustc_mir/interpret/place.rs
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,18 @@ impl<'tcx, Tag: ::std::fmt::Debug> PlaceTy<'tcx, Tag> {
}
}

/// Test if it is valid for a MIR assignment to assign `src`-typed place to `dest`-typed value.
fn mir_assign_valid_types<'tcx>(src: Ty<'tcx>, dest: Ty<'tcx>) -> bool {
src == dest
|| match (&src.kind, &dest.kind) {
// After MIR optimizations, there can be assignments that change reference mutability.
(ty::Ref(_, src_pointee, _), ty::Ref(_, dest_pointee, _)) => {
src_pointee == dest_pointee
}
_ => false,
}
}

// separating the pointer tag for `impl Trait`, see https://github.com/rust-lang/rust/issues/54385
impl<'mir, 'tcx, Tag, M> InterpCx<'mir, 'tcx, M>
where
Expand Down Expand Up @@ -869,10 +881,10 @@ where
// We do NOT compare the types for equality, because well-typed code can
// actually "transmute" `&mut T` to `&T` in an assignment without a cast.
assert!(
src.layout.layout == dest.layout.layout,
"Layout mismatch when copying!\nsrc: {:#?}\ndest: {:#?}",
src,
dest
mir_assign_valid_types(src.layout.ty, dest.layout.ty),
"type mismatch when copying!\nsrc: {:?}, dest: {:?}",
src.layout.ty,
dest.layout.ty,
);

// Let us see if the layout is simple so we take a shortcut, avoid force_allocation.
Expand Down Expand Up @@ -923,7 +935,7 @@ where
src: OpTy<'tcx, M::PointerTag>,
dest: PlaceTy<'tcx, M::PointerTag>,
) -> InterpResult<'tcx> {
if src.layout.layout == dest.layout.layout {
if mir_assign_valid_types(src.layout.ty, dest.layout.ty) {
// Fast path: Just use normal `copy_op`
return self.copy_op(src, dest);
}
Expand Down

0 comments on commit f1ea273

Please sign in to comment.