2020-05-24 00:55:44 +02:00
|
|
|
//! Validates the MIR to ensure that invariants are upheld.
|
|
|
|
|
|
|
|
use super::{MirPass, MirSource};
|
|
|
|
use rustc_middle::mir::visit::Visitor;
|
|
|
|
use rustc_middle::{
|
2020-05-31 09:52:51 +02:00
|
|
|
mir::{
|
|
|
|
BasicBlock, Body, Location, Operand, Rvalue, Statement, StatementKind, Terminator,
|
|
|
|
TerminatorKind,
|
|
|
|
},
|
2020-06-21 13:49:53 +02:00
|
|
|
ty::{
|
|
|
|
self,
|
|
|
|
relate::{Relate, RelateResult, TypeRelation},
|
|
|
|
ParamEnv, Ty, TyCtxt,
|
|
|
|
},
|
2020-05-24 00:55:44 +02:00
|
|
|
};
|
|
|
|
|
2020-06-09 11:21:36 +01:00
|
|
|
#[derive(Copy, Clone, Debug)]
|
2020-06-08 13:54:20 +01:00
|
|
|
enum EdgeKind {
|
|
|
|
Unwind,
|
2020-06-08 16:04:41 +01:00
|
|
|
Normal,
|
2020-06-08 13:54:20 +01:00
|
|
|
}
|
|
|
|
|
2020-05-24 00:55:44 +02:00
|
|
|
pub struct Validator {
|
|
|
|
/// Describes at which point in the pipeline this validation is happening.
|
|
|
|
pub when: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'tcx> MirPass<'tcx> for Validator {
|
|
|
|
fn run_pass(&self, tcx: TyCtxt<'tcx>, source: MirSource<'tcx>, body: &mut Body<'tcx>) {
|
2020-06-14 20:22:13 +02:00
|
|
|
let param_env = tcx.param_env(source.def_id());
|
|
|
|
TypeChecker { when: &self.when, source, body, tcx, param_env }.visit_body(body);
|
2020-05-24 00:55:44 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct TypeChecker<'a, 'tcx> {
|
|
|
|
when: &'a str,
|
2020-06-14 20:22:13 +02:00
|
|
|
source: MirSource<'tcx>,
|
2020-05-24 00:55:44 +02:00
|
|
|
body: &'a Body<'tcx>,
|
|
|
|
tcx: TyCtxt<'tcx>,
|
|
|
|
param_env: ParamEnv<'tcx>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
|
2020-05-31 15:21:09 +02:00
|
|
|
fn fail(&self, location: Location, msg: impl AsRef<str>) {
|
|
|
|
let span = self.body.source_info(location).span;
|
2020-05-24 00:55:44 +02:00
|
|
|
// We use `delay_span_bug` as we might see broken MIR when other errors have already
|
|
|
|
// occurred.
|
|
|
|
self.tcx.sess.diagnostic().delay_span_bug(
|
|
|
|
span,
|
2020-05-31 15:21:09 +02:00
|
|
|
&format!(
|
|
|
|
"broken MIR in {:?} ({}) at {:?}:\n{}",
|
2020-06-14 20:22:13 +02:00
|
|
|
self.source.instance,
|
2020-05-31 15:21:09 +02:00
|
|
|
self.when,
|
|
|
|
location,
|
|
|
|
msg.as_ref()
|
|
|
|
),
|
2020-05-24 00:55:44 +02:00
|
|
|
);
|
|
|
|
}
|
2020-05-31 09:52:51 +02:00
|
|
|
|
2020-06-09 11:21:36 +01:00
|
|
|
fn check_edge(&self, location: Location, bb: BasicBlock, edge_kind: EdgeKind) {
|
2020-06-08 13:54:20 +01:00
|
|
|
if let Some(bb) = self.body.basic_blocks().get(bb) {
|
|
|
|
let src = self.body.basic_blocks().get(location.block).unwrap();
|
|
|
|
match (src.is_cleanup, bb.is_cleanup, edge_kind) {
|
|
|
|
// Non-cleanup blocks can jump to non-cleanup blocks along non-unwind edges
|
2020-06-08 16:04:41 +01:00
|
|
|
(false, false, EdgeKind::Normal)
|
2020-06-08 13:54:20 +01:00
|
|
|
// Non-cleanup blocks can jump to cleanup blocks along unwind edges
|
|
|
|
| (false, true, EdgeKind::Unwind)
|
2020-06-08 16:04:41 +01:00
|
|
|
// Cleanup blocks can jump to cleanup blocks along non-unwind edges
|
|
|
|
| (true, true, EdgeKind::Normal) => {}
|
2020-06-08 13:54:20 +01:00
|
|
|
// All other jumps are invalid
|
|
|
|
_ => {
|
|
|
|
self.fail(
|
|
|
|
location,
|
2020-06-09 11:21:36 +01:00
|
|
|
format!(
|
|
|
|
"{:?} edge to {:?} violates unwind invariants (cleanup {:?} -> {:?})",
|
|
|
|
edge_kind,
|
|
|
|
bb,
|
|
|
|
src.is_cleanup,
|
|
|
|
bb.is_cleanup,
|
|
|
|
)
|
2020-06-08 13:54:20 +01:00
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2020-05-31 15:21:09 +02:00
|
|
|
self.fail(location, format!("encountered jump to invalid basic block {:?}", bb))
|
2020-05-31 09:52:51 +02:00
|
|
|
}
|
|
|
|
}
|
2020-05-24 00:55:44 +02:00
|
|
|
|
2020-05-31 15:02:33 +02:00
|
|
|
/// Check if src can be assigned into dest.
|
|
|
|
/// This is not precise, it will accept some incorrect assignments.
|
|
|
|
fn mir_assign_valid_types(&self, src: Ty<'tcx>, dest: Ty<'tcx>) -> bool {
|
|
|
|
if src == dest {
|
|
|
|
// Equal types, all is good.
|
|
|
|
return true;
|
|
|
|
}
|
2020-06-01 21:04:11 +02:00
|
|
|
// Normalize projections and things like that.
|
2020-06-06 11:45:19 +02:00
|
|
|
// FIXME: We need to reveal_all, as some optimizations change types in ways
|
2020-06-22 09:00:40 +02:00
|
|
|
// that require unfolding opaque types.
|
2020-06-06 11:45:19 +02:00
|
|
|
let param_env = self.param_env.with_reveal_all();
|
|
|
|
let src = self.tcx.normalize_erasing_regions(param_env, src);
|
|
|
|
let dest = self.tcx.normalize_erasing_regions(param_env, dest);
|
2020-06-01 21:04:11 +02:00
|
|
|
// It's worth checking equality again.
|
|
|
|
if src == dest {
|
|
|
|
return true;
|
|
|
|
}
|
2020-05-30 23:46:21 +02:00
|
|
|
|
2020-06-21 10:04:12 +02:00
|
|
|
// Type-changing assignments can happen when subtyping is used. While
|
|
|
|
// all normal lifetimes are erased, higher-ranked types with their
|
|
|
|
// late-bound lifetimes are still around and can lead to type
|
2020-06-21 13:49:53 +02:00
|
|
|
// differences. So we compare ignoring lifetimes.
|
|
|
|
struct LifetimeIgnoreRelation<'tcx> {
|
|
|
|
tcx: TyCtxt<'tcx>,
|
|
|
|
param_env: ty::ParamEnv<'tcx>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl TypeRelation<'tcx> for LifetimeIgnoreRelation<'tcx> {
|
|
|
|
fn tcx(&self) -> TyCtxt<'tcx> {
|
|
|
|
self.tcx
|
|
|
|
}
|
|
|
|
|
|
|
|
fn param_env(&self) -> ty::ParamEnv<'tcx> {
|
|
|
|
self.param_env
|
|
|
|
}
|
|
|
|
|
|
|
|
fn tag(&self) -> &'static str {
|
|
|
|
"librustc_mir::transform::validate"
|
|
|
|
}
|
|
|
|
|
|
|
|
fn a_is_expected(&self) -> bool {
|
|
|
|
true
|
|
|
|
}
|
|
|
|
|
|
|
|
fn relate_with_variance<T: Relate<'tcx>>(
|
|
|
|
&mut self,
|
|
|
|
_: ty::Variance,
|
|
|
|
a: &T,
|
|
|
|
b: &T,
|
|
|
|
) -> RelateResult<'tcx, T> {
|
|
|
|
// Ignore variance, require types to be exactly the same.
|
|
|
|
self.relate(a, b)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn tys(&mut self, a: Ty<'tcx>, b: Ty<'tcx>) -> RelateResult<'tcx, Ty<'tcx>> {
|
|
|
|
if a == b {
|
|
|
|
// Short-circuit.
|
|
|
|
return Ok(a);
|
|
|
|
}
|
|
|
|
ty::relate::super_relate_tys(self, a, b)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn regions(
|
|
|
|
&mut self,
|
|
|
|
a: ty::Region<'tcx>,
|
|
|
|
_b: ty::Region<'tcx>,
|
|
|
|
) -> RelateResult<'tcx, ty::Region<'tcx>> {
|
|
|
|
// Ignore regions.
|
|
|
|
Ok(a)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn consts(
|
|
|
|
&mut self,
|
|
|
|
a: &'tcx ty::Const<'tcx>,
|
|
|
|
b: &'tcx ty::Const<'tcx>,
|
|
|
|
) -> RelateResult<'tcx, &'tcx ty::Const<'tcx>> {
|
|
|
|
ty::relate::super_relate_consts(self, a, b)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn binders<T>(
|
|
|
|
&mut self,
|
|
|
|
a: &ty::Binder<T>,
|
|
|
|
b: &ty::Binder<T>,
|
|
|
|
) -> RelateResult<'tcx, ty::Binder<T>>
|
|
|
|
where
|
|
|
|
T: Relate<'tcx>,
|
|
|
|
{
|
|
|
|
self.relate(a.skip_binder(), b.skip_binder())?;
|
|
|
|
Ok(a.clone())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Instantiate and run relation.
|
|
|
|
let mut relator: LifetimeIgnoreRelation<'tcx> =
|
|
|
|
LifetimeIgnoreRelation { tcx: self.tcx, param_env };
|
|
|
|
relator.relate(&src, &dest).is_ok()
|
2020-05-31 15:02:33 +02:00
|
|
|
}
|
2020-05-30 23:46:21 +02:00
|
|
|
}
|
|
|
|
|
2020-05-24 00:55:44 +02:00
|
|
|
impl<'a, 'tcx> Visitor<'tcx> for TypeChecker<'a, 'tcx> {
|
|
|
|
fn visit_operand(&mut self, operand: &Operand<'tcx>, location: Location) {
|
|
|
|
// `Operand::Copy` is only supposed to be used with `Copy` types.
|
|
|
|
if let Operand::Copy(place) = operand {
|
|
|
|
let ty = place.ty(&self.body.local_decls, self.tcx).ty;
|
2020-05-31 14:26:41 +02:00
|
|
|
let span = self.body.source_info(location).span;
|
2020-05-24 00:55:44 +02:00
|
|
|
|
2020-05-31 14:26:41 +02:00
|
|
|
if !ty.is_copy_modulo_regions(self.tcx, self.param_env, span) {
|
2020-05-31 15:21:09 +02:00
|
|
|
self.fail(location, format!("`Operand::Copy` with non-`Copy` type {}", ty));
|
2020-05-24 00:55:44 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
self.super_operand(operand, location);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_statement(&mut self, statement: &Statement<'tcx>, location: Location) {
|
2020-05-31 09:54:25 +02:00
|
|
|
match &statement.kind {
|
|
|
|
StatementKind::Assign(box (dest, rvalue)) => {
|
|
|
|
// LHS and RHS of the assignment must have the same type.
|
|
|
|
let left_ty = dest.ty(&self.body.local_decls, self.tcx).ty;
|
|
|
|
let right_ty = rvalue.ty(&self.body.local_decls, self.tcx);
|
2020-05-31 15:02:33 +02:00
|
|
|
if !self.mir_assign_valid_types(right_ty, left_ty) {
|
2020-05-31 09:54:25 +02:00
|
|
|
self.fail(
|
|
|
|
location,
|
|
|
|
format!(
|
|
|
|
"encountered `Assign` statement with incompatible types:\n\
|
|
|
|
left-hand side has type: {}\n\
|
|
|
|
right-hand side has type: {}",
|
|
|
|
left_ty, right_ty,
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
// The sides of an assignment must not alias. Currently this just checks whether the places
|
|
|
|
// are identical.
|
|
|
|
match rvalue {
|
|
|
|
Rvalue::Use(Operand::Copy(src) | Operand::Move(src)) => {
|
|
|
|
if dest == src {
|
|
|
|
self.fail(
|
|
|
|
location,
|
|
|
|
"encountered `Assign` statement with overlapping memory",
|
|
|
|
);
|
|
|
|
}
|
2020-05-24 00:55:44 +02:00
|
|
|
}
|
2020-05-31 09:54:25 +02:00
|
|
|
_ => {}
|
2020-05-24 00:55:44 +02:00
|
|
|
}
|
|
|
|
}
|
2020-05-31 09:54:25 +02:00
|
|
|
_ => {}
|
2020-05-24 00:55:44 +02:00
|
|
|
}
|
|
|
|
}
|
2020-05-31 09:52:51 +02:00
|
|
|
|
2020-05-31 15:21:09 +02:00
|
|
|
fn visit_terminator(&mut self, terminator: &Terminator<'tcx>, location: Location) {
|
2020-05-31 09:52:51 +02:00
|
|
|
match &terminator.kind {
|
|
|
|
TerminatorKind::Goto { target } => {
|
2020-06-09 11:21:36 +01:00
|
|
|
self.check_edge(location, *target, EdgeKind::Normal);
|
2020-05-31 09:52:51 +02:00
|
|
|
}
|
2020-05-31 15:07:16 +02:00
|
|
|
TerminatorKind::SwitchInt { targets, values, .. } => {
|
|
|
|
if targets.len() != values.len() + 1 {
|
2020-05-31 09:52:51 +02:00
|
|
|
self.fail(
|
2020-05-31 15:21:09 +02:00
|
|
|
location,
|
2020-05-31 15:07:16 +02:00
|
|
|
format!(
|
|
|
|
"encountered `SwitchInt` terminator with {} values, but {} targets (should be values+1)",
|
|
|
|
values.len(),
|
|
|
|
targets.len(),
|
|
|
|
),
|
2020-05-31 09:52:51 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
for target in targets {
|
2020-06-09 11:21:36 +01:00
|
|
|
self.check_edge(location, *target, EdgeKind::Normal);
|
2020-05-31 09:52:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
TerminatorKind::Drop { target, unwind, .. } => {
|
2020-06-09 11:21:36 +01:00
|
|
|
self.check_edge(location, *target, EdgeKind::Normal);
|
2020-05-31 09:52:51 +02:00
|
|
|
if let Some(unwind) = unwind {
|
2020-06-09 11:21:36 +01:00
|
|
|
self.check_edge(location, *unwind, EdgeKind::Unwind);
|
2020-05-31 09:52:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
TerminatorKind::DropAndReplace { target, unwind, .. } => {
|
2020-06-09 11:21:36 +01:00
|
|
|
self.check_edge(location, *target, EdgeKind::Normal);
|
2020-05-31 09:52:51 +02:00
|
|
|
if let Some(unwind) = unwind {
|
2020-06-09 11:21:36 +01:00
|
|
|
self.check_edge(location, *unwind, EdgeKind::Unwind);
|
2020-05-31 09:52:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
TerminatorKind::Call { func, destination, cleanup, .. } => {
|
|
|
|
let func_ty = func.ty(&self.body.local_decls, self.tcx);
|
|
|
|
match func_ty.kind {
|
|
|
|
ty::FnPtr(..) | ty::FnDef(..) => {}
|
|
|
|
_ => self.fail(
|
2020-05-31 15:21:09 +02:00
|
|
|
location,
|
2020-05-31 09:52:51 +02:00
|
|
|
format!("encountered non-callable type {} in `Call` terminator", func_ty),
|
|
|
|
),
|
|
|
|
}
|
|
|
|
if let Some((_, target)) = destination {
|
2020-06-09 11:21:36 +01:00
|
|
|
self.check_edge(location, *target, EdgeKind::Normal);
|
2020-05-31 09:52:51 +02:00
|
|
|
}
|
|
|
|
if let Some(cleanup) = cleanup {
|
2020-06-09 11:21:36 +01:00
|
|
|
self.check_edge(location, *cleanup, EdgeKind::Unwind);
|
2020-05-31 09:52:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
TerminatorKind::Assert { cond, target, cleanup, .. } => {
|
|
|
|
let cond_ty = cond.ty(&self.body.local_decls, self.tcx);
|
|
|
|
if cond_ty != self.tcx.types.bool {
|
|
|
|
self.fail(
|
2020-05-31 15:21:09 +02:00
|
|
|
location,
|
2020-05-31 09:52:51 +02:00
|
|
|
format!(
|
|
|
|
"encountered non-boolean condition of type {} in `Assert` terminator",
|
|
|
|
cond_ty
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
2020-06-09 11:21:36 +01:00
|
|
|
self.check_edge(location, *target, EdgeKind::Normal);
|
2020-05-31 09:52:51 +02:00
|
|
|
if let Some(cleanup) = cleanup {
|
2020-06-09 11:21:36 +01:00
|
|
|
self.check_edge(location, *cleanup, EdgeKind::Unwind);
|
2020-05-31 09:52:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
TerminatorKind::Yield { resume, drop, .. } => {
|
2020-06-09 11:21:36 +01:00
|
|
|
self.check_edge(location, *resume, EdgeKind::Normal);
|
2020-05-31 09:52:51 +02:00
|
|
|
if let Some(drop) = drop {
|
2020-06-09 11:21:36 +01:00
|
|
|
self.check_edge(location, *drop, EdgeKind::Normal);
|
2020-05-31 09:52:51 +02:00
|
|
|
}
|
|
|
|
}
|
2020-06-02 09:15:24 +02:00
|
|
|
TerminatorKind::FalseEdge { real_target, imaginary_target } => {
|
2020-06-09 11:21:36 +01:00
|
|
|
self.check_edge(location, *real_target, EdgeKind::Normal);
|
|
|
|
self.check_edge(location, *imaginary_target, EdgeKind::Normal);
|
2020-05-31 09:52:51 +02:00
|
|
|
}
|
|
|
|
TerminatorKind::FalseUnwind { real_target, unwind } => {
|
2020-06-09 11:21:36 +01:00
|
|
|
self.check_edge(location, *real_target, EdgeKind::Normal);
|
2020-05-31 09:52:51 +02:00
|
|
|
if let Some(unwind) = unwind {
|
2020-06-09 11:21:36 +01:00
|
|
|
self.check_edge(location, *unwind, EdgeKind::Unwind);
|
2020-05-31 09:52:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
TerminatorKind::InlineAsm { destination, .. } => {
|
|
|
|
if let Some(destination) = destination {
|
2020-06-09 11:21:36 +01:00
|
|
|
self.check_edge(location, *destination, EdgeKind::Normal);
|
2020-05-31 09:52:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// Nothing to validate for these.
|
|
|
|
TerminatorKind::Resume
|
|
|
|
| TerminatorKind::Abort
|
|
|
|
| TerminatorKind::Return
|
|
|
|
| TerminatorKind::Unreachable
|
|
|
|
| TerminatorKind::GeneratorDrop => {}
|
|
|
|
}
|
|
|
|
}
|
2020-05-24 00:55:44 +02:00
|
|
|
}
|