Skip to content

Commit 92c474a

Browse files
authored
Rollup merge of rust-lang#72796 - RalfJung:mir-assign-sanity, r=matthewjasper
MIR sanity check: validate types on assignment This expands the MIR validation added by @jonas-schievink in rust-lang#72093 to also check that on an assignment, the types of both sides match. Cc @eddyb @oli-obk
2 parents a973c5d + 35911ee commit 92c474a

File tree

4 files changed

+176
-51
lines changed

4 files changed

+176
-51
lines changed

src/librustc_mir/interpret/eval_context.rs

+23-36
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use rustc_middle::mir::interpret::{
1515
};
1616
use rustc_middle::ty::layout::{self, TyAndLayout};
1717
use rustc_middle::ty::{
18-
self, fold::BottomUpFolder, query::TyCtxtAt, subst::SubstsRef, Ty, TyCtxt, TypeFoldable,
18+
self, query::TyCtxtAt, subst::SubstsRef, ParamEnv, Ty, TyCtxt, TypeFoldable,
1919
};
2020
use rustc_span::{source_map::DUMMY_SP, Span};
2121
use rustc_target::abi::{Align, HasDataLayout, LayoutOf, Size, TargetDataLayout};
@@ -24,6 +24,7 @@ use super::{
2424
Immediate, MPlaceTy, Machine, MemPlace, MemPlaceMeta, Memory, OpTy, Operand, Place, PlaceTy,
2525
ScalarMaybeUninit, StackPopJump,
2626
};
27+
use crate::transform::validate::equal_up_to_regions;
2728
use crate::util::storage::AlwaysLiveLocals;
2829

2930
pub struct InterpCx<'mir, 'tcx, M: Machine<'mir, 'tcx>> {
@@ -220,49 +221,35 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> LayoutOf for InterpCx<'mir, 'tcx,
220221
/// This test should be symmetric, as it is primarily about layout compatibility.
221222
pub(super) fn mir_assign_valid_types<'tcx>(
222223
tcx: TyCtxt<'tcx>,
224+
param_env: ParamEnv<'tcx>,
223225
src: TyAndLayout<'tcx>,
224226
dest: TyAndLayout<'tcx>,
225227
) -> bool {
226-
if src.ty == dest.ty {
227-
// Equal types, all is good.
228-
return true;
229-
}
230-
if src.layout != dest.layout {
231-
// Layout differs, definitely not equal.
232-
// We do this here because Miri would *do the wrong thing* if we allowed layout-changing
233-
// assignments.
234-
return false;
235-
}
236-
237-
// Type-changing assignments can happen for (at least) two reasons:
238-
// 1. `&mut T` -> `&T` gets optimized from a reborrow to a mere assignment.
239-
// 2. Subtyping is used. While all normal lifetimes are erased, higher-ranked types
240-
// with their late-bound lifetimes are still around and can lead to type differences.
241-
// Normalize both of them away.
242-
let normalize = |ty: Ty<'tcx>| {
243-
ty.fold_with(&mut BottomUpFolder {
244-
tcx,
245-
// Normalize all references to immutable.
246-
ty_op: |ty| match ty.kind {
247-
ty::Ref(_, pointee, _) => tcx.mk_imm_ref(tcx.lifetimes.re_erased, pointee),
248-
_ => ty,
249-
},
250-
// We just erase all late-bound lifetimes, but this is not fully correct (FIXME):
251-
// lifetimes in invariant positions could matter (e.g. through associated types).
252-
// We rely on the fact that layout was confirmed to be equal above.
253-
lt_op: |_| tcx.lifetimes.re_erased,
254-
// Leave consts unchanged.
255-
ct_op: |ct| ct,
256-
})
257-
};
258-
normalize(src.ty) == normalize(dest.ty)
228+
// Type-changing assignments can happen when subtyping is used. While
229+
// all normal lifetimes are erased, higher-ranked types with their
230+
// late-bound lifetimes are still around and can lead to type
231+
// differences. So we compare ignoring lifetimes.
232+
if equal_up_to_regions(tcx, param_env, src.ty, dest.ty) {
233+
// Make sure the layout is equal, too -- just to be safe. Miri really
234+
// needs layout equality. For performance reason we skip this check when
235+
// the types are equal. Equal types *can* have different layouts when
236+
// enum downcast is involved (as enum variants carry the type of the
237+
// enum), but those should never occur in assignments.
238+
if cfg!(debug_assertions) || src.ty != dest.ty {
239+
assert_eq!(src.layout, dest.layout);
240+
}
241+
true
242+
} else {
243+
false
244+
}
259245
}
260246

261247
/// Use the already known layout if given (but sanity check in debug mode),
262248
/// or compute the layout.
263249
#[cfg_attr(not(debug_assertions), inline(always))]
264250
pub(super) fn from_known_layout<'tcx>(
265251
tcx: TyCtxtAt<'tcx>,
252+
param_env: ParamEnv<'tcx>,
266253
known_layout: Option<TyAndLayout<'tcx>>,
267254
compute: impl FnOnce() -> InterpResult<'tcx, TyAndLayout<'tcx>>,
268255
) -> InterpResult<'tcx, TyAndLayout<'tcx>> {
@@ -271,7 +258,7 @@ pub(super) fn from_known_layout<'tcx>(
271258
Some(known_layout) => {
272259
if cfg!(debug_assertions) {
273260
let check_layout = compute()?;
274-
if !mir_assign_valid_types(tcx.tcx, check_layout, known_layout) {
261+
if !mir_assign_valid_types(tcx.tcx, param_env, check_layout, known_layout) {
275262
span_bug!(
276263
tcx.span,
277264
"expected type differs from actual type.\nexpected: {:?}\nactual: {:?}",
@@ -475,7 +462,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
475462
// have to support that case (mostly by skipping all caching).
476463
match frame.locals.get(local).and_then(|state| state.layout.get()) {
477464
None => {
478-
let layout = from_known_layout(self.tcx, layout, || {
465+
let layout = from_known_layout(self.tcx, self.param_env, layout, || {
479466
let local_ty = frame.body.local_decls[local].ty;
480467
let local_ty =
481468
self.subst_from_frame_and_normalize_erasing_regions(frame, local_ty);

src/librustc_mir/interpret/operand.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -488,6 +488,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
488488
// Sanity-check the type we ended up with.
489489
debug_assert!(mir_assign_valid_types(
490490
*self.tcx,
491+
self.param_env,
491492
self.layout_of(self.subst_from_current_frame_and_normalize_erasing_regions(
492493
place.ty(&self.frame().body.local_decls, *self.tcx).ty
493494
))?,
@@ -570,7 +571,8 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
570571
// documentation).
571572
let val_val = M::adjust_global_const(self, val_val)?;
572573
// Other cases need layout.
573-
let layout = from_known_layout(self.tcx, layout, || self.layout_of(val.ty))?;
574+
let layout =
575+
from_known_layout(self.tcx, self.param_env, layout, || self.layout_of(val.ty))?;
574576
let op = match val_val {
575577
ConstValue::ByRef { alloc, offset } => {
576578
let id = self.tcx.create_memory_alloc(alloc);

src/librustc_mir/interpret/place.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -652,6 +652,7 @@ where
652652
// Sanity-check the type we ended up with.
653653
debug_assert!(mir_assign_valid_types(
654654
*self.tcx,
655+
self.param_env,
655656
self.layout_of(self.subst_from_current_frame_and_normalize_erasing_regions(
656657
place.ty(&self.frame().body.local_decls, *self.tcx).ty
657658
))?,
@@ -855,7 +856,7 @@ where
855856
) -> InterpResult<'tcx> {
856857
// We do NOT compare the types for equality, because well-typed code can
857858
// actually "transmute" `&mut T` to `&T` in an assignment without a cast.
858-
if !mir_assign_valid_types(*self.tcx, src.layout, dest.layout) {
859+
if !mir_assign_valid_types(*self.tcx, self.param_env, src.layout, dest.layout) {
859860
span_bug!(
860861
self.cur_span(),
861862
"type mismatch when copying!\nsrc: {:?},\ndest: {:?}",
@@ -912,7 +913,7 @@ where
912913
src: OpTy<'tcx, M::PointerTag>,
913914
dest: PlaceTy<'tcx, M::PointerTag>,
914915
) -> InterpResult<'tcx> {
915-
if mir_assign_valid_types(*self.tcx, src.layout, dest.layout) {
916+
if mir_assign_valid_types(*self.tcx, self.param_env, src.layout, dest.layout) {
916917
// Fast path: Just use normal `copy_op`
917918
return self.copy_op(src, dest);
918919
}

src/librustc_mir/transform/validate.rs

+147-12
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,11 @@ use rustc_middle::{
77
BasicBlock, Body, Location, Operand, Rvalue, Statement, StatementKind, Terminator,
88
TerminatorKind,
99
},
10-
ty::{self, ParamEnv, TyCtxt},
10+
ty::{
11+
self,
12+
relate::{Relate, RelateResult, TypeRelation},
13+
ParamEnv, Ty, TyCtxt,
14+
},
1115
};
1216

1317
#[derive(Copy, Clone, Debug)]
@@ -28,6 +32,98 @@ impl<'tcx> MirPass<'tcx> for Validator {
2832
}
2933
}
3034

35+
/// Returns whether the two types are equal up to lifetimes.
36+
/// All lifetimes, including higher-ranked ones, get ignored for this comparison.
37+
/// (This is unlike the `erasing_regions` methods, which keep higher-ranked lifetimes for soundness reasons.)
38+
///
39+
/// The point of this function is to approximate "equal up to subtyping". However,
40+
/// the approximation is incorrect as variance is ignored.
41+
pub fn equal_up_to_regions(
42+
tcx: TyCtxt<'tcx>,
43+
param_env: ParamEnv<'tcx>,
44+
src: Ty<'tcx>,
45+
dest: Ty<'tcx>,
46+
) -> bool {
47+
// Fast path.
48+
if src == dest {
49+
return true;
50+
}
51+
52+
struct LifetimeIgnoreRelation<'tcx> {
53+
tcx: TyCtxt<'tcx>,
54+
param_env: ty::ParamEnv<'tcx>,
55+
}
56+
57+
impl TypeRelation<'tcx> for LifetimeIgnoreRelation<'tcx> {
58+
fn tcx(&self) -> TyCtxt<'tcx> {
59+
self.tcx
60+
}
61+
62+
fn param_env(&self) -> ty::ParamEnv<'tcx> {
63+
self.param_env
64+
}
65+
66+
fn tag(&self) -> &'static str {
67+
"librustc_mir::transform::validate"
68+
}
69+
70+
fn a_is_expected(&self) -> bool {
71+
true
72+
}
73+
74+
fn relate_with_variance<T: Relate<'tcx>>(
75+
&mut self,
76+
_: ty::Variance,
77+
a: &T,
78+
b: &T,
79+
) -> RelateResult<'tcx, T> {
80+
// Ignore variance, require types to be exactly the same.
81+
self.relate(a, b)
82+
}
83+
84+
fn tys(&mut self, a: Ty<'tcx>, b: Ty<'tcx>) -> RelateResult<'tcx, Ty<'tcx>> {
85+
if a == b {
86+
// Short-circuit.
87+
return Ok(a);
88+
}
89+
ty::relate::super_relate_tys(self, a, b)
90+
}
91+
92+
fn regions(
93+
&mut self,
94+
a: ty::Region<'tcx>,
95+
_b: ty::Region<'tcx>,
96+
) -> RelateResult<'tcx, ty::Region<'tcx>> {
97+
// Ignore regions.
98+
Ok(a)
99+
}
100+
101+
fn consts(
102+
&mut self,
103+
a: &'tcx ty::Const<'tcx>,
104+
b: &'tcx ty::Const<'tcx>,
105+
) -> RelateResult<'tcx, &'tcx ty::Const<'tcx>> {
106+
ty::relate::super_relate_consts(self, a, b)
107+
}
108+
109+
fn binders<T>(
110+
&mut self,
111+
a: &ty::Binder<T>,
112+
b: &ty::Binder<T>,
113+
) -> RelateResult<'tcx, ty::Binder<T>>
114+
where
115+
T: Relate<'tcx>,
116+
{
117+
self.relate(a.skip_binder(), b.skip_binder())?;
118+
Ok(a.clone())
119+
}
120+
}
121+
122+
// Instantiate and run relation.
123+
let mut relator: LifetimeIgnoreRelation<'tcx> = LifetimeIgnoreRelation { tcx: tcx, param_env };
124+
relator.relate(&src, &dest).is_ok()
125+
}
126+
31127
struct TypeChecker<'a, 'tcx> {
32128
when: &'a str,
33129
source: MirSource<'tcx>,
@@ -81,6 +177,28 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
81177
self.fail(location, format!("encountered jump to invalid basic block {:?}", bb))
82178
}
83179
}
180+
181+
/// Check if src can be assigned into dest.
182+
/// This is not precise, it will accept some incorrect assignments.
183+
fn mir_assign_valid_types(&self, src: Ty<'tcx>, dest: Ty<'tcx>) -> bool {
184+
// Fast path before we normalize.
185+
if src == dest {
186+
// Equal types, all is good.
187+
return true;
188+
}
189+
// Normalize projections and things like that.
190+
// FIXME: We need to reveal_all, as some optimizations change types in ways
191+
// that require unfolding opaque types.
192+
let param_env = self.param_env.with_reveal_all();
193+
let src = self.tcx.normalize_erasing_regions(param_env, src);
194+
let dest = self.tcx.normalize_erasing_regions(param_env, dest);
195+
196+
// Type-changing assignments can happen when subtyping is used. While
197+
// all normal lifetimes are erased, higher-ranked types with their
198+
// late-bound lifetimes are still around and can lead to type
199+
// differences. So we compare ignoring lifetimes.
200+
equal_up_to_regions(self.tcx, param_env, src, dest)
201+
}
84202
}
85203

86204
impl<'a, 'tcx> Visitor<'tcx> for TypeChecker<'a, 'tcx> {
@@ -99,20 +217,37 @@ impl<'a, 'tcx> Visitor<'tcx> for TypeChecker<'a, 'tcx> {
99217
}
100218

101219
fn visit_statement(&mut self, statement: &Statement<'tcx>, location: Location) {
102-
// The sides of an assignment must not alias. Currently this just checks whether the places
103-
// are identical.
104-
if let StatementKind::Assign(box (dest, rvalue)) = &statement.kind {
105-
match rvalue {
106-
Rvalue::Use(Operand::Copy(src) | Operand::Move(src)) => {
107-
if dest == src {
108-
self.fail(
109-
location,
110-
"encountered `Assign` statement with overlapping memory",
111-
);
220+
match &statement.kind {
221+
StatementKind::Assign(box (dest, rvalue)) => {
222+
// LHS and RHS of the assignment must have the same type.
223+
let left_ty = dest.ty(&self.body.local_decls, self.tcx).ty;
224+
let right_ty = rvalue.ty(&self.body.local_decls, self.tcx);
225+
if !self.mir_assign_valid_types(right_ty, left_ty) {
226+
self.fail(
227+
location,
228+
format!(
229+
"encountered `Assign` statement with incompatible types:\n\
230+
left-hand side has type: {}\n\
231+
right-hand side has type: {}",
232+
left_ty, right_ty,
233+
),
234+
);
235+
}
236+
// The sides of an assignment must not alias. Currently this just checks whether the places
237+
// are identical.
238+
match rvalue {
239+
Rvalue::Use(Operand::Copy(src) | Operand::Move(src)) => {
240+
if dest == src {
241+
self.fail(
242+
location,
243+
"encountered `Assign` statement with overlapping memory",
244+
);
245+
}
112246
}
247+
_ => {}
113248
}
114-
_ => {}
115249
}
250+
_ => {}
116251
}
117252
}
118253

0 commit comments

Comments
 (0)