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,
|
|
|
|
},
|
|
|
|
ty::{self, ParamEnv, TyCtxt},
|
2020-05-24 00:55:44 +02:00
|
|
|
};
|
2020-05-31 15:21:09 +02:00
|
|
|
use rustc_span::def_id::DefId;
|
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>) {
|
|
|
|
let def_id = source.def_id();
|
|
|
|
let param_env = tcx.param_env(def_id);
|
|
|
|
TypeChecker { when: &self.when, def_id, body, tcx, param_env }.visit_body(body);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct TypeChecker<'a, 'tcx> {
|
|
|
|
when: &'a str,
|
|
|
|
def_id: DefId,
|
|
|
|
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{}",
|
|
|
|
self.def_id,
|
|
|
|
self.when,
|
|
|
|
location,
|
|
|
|
msg.as_ref()
|
|
|
|
),
|
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 check_bb(&self, location: Location, bb: BasicBlock) {
|
2020-05-31 09:52:51 +02:00
|
|
|
if self.body.basic_blocks().get(bb).is_none() {
|
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
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
|
|
|
// The sides of an assignment must not alias. Currently this just checks whether the places
|
|
|
|
// are identical.
|
|
|
|
if let StatementKind::Assign(box (dest, rvalue)) = &statement.kind {
|
|
|
|
match rvalue {
|
|
|
|
Rvalue::Use(Operand::Copy(src) | Operand::Move(src)) => {
|
|
|
|
if dest == src {
|
|
|
|
self.fail(
|
2020-05-31 15:21:09 +02:00
|
|
|
location,
|
|
|
|
"encountered `Assign` statement with overlapping memory",
|
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-05-31 15:21:09 +02:00
|
|
|
self.check_bb(location, *target);
|
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-05-31 15:21:09 +02:00
|
|
|
self.check_bb(location, *target);
|
2020-05-31 09:52:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
TerminatorKind::Drop { target, unwind, .. } => {
|
2020-05-31 15:21:09 +02:00
|
|
|
self.check_bb(location, *target);
|
2020-05-31 09:52:51 +02:00
|
|
|
if let Some(unwind) = unwind {
|
2020-05-31 15:21:09 +02:00
|
|
|
self.check_bb(location, *unwind);
|
2020-05-31 09:52:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
TerminatorKind::DropAndReplace { target, unwind, .. } => {
|
2020-05-31 15:21:09 +02:00
|
|
|
self.check_bb(location, *target);
|
2020-05-31 09:52:51 +02:00
|
|
|
if let Some(unwind) = unwind {
|
2020-05-31 15:21:09 +02:00
|
|
|
self.check_bb(location, *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-05-31 15:21:09 +02:00
|
|
|
self.check_bb(location, *target);
|
2020-05-31 09:52:51 +02:00
|
|
|
}
|
|
|
|
if let Some(cleanup) = cleanup {
|
2020-05-31 15:21:09 +02:00
|
|
|
self.check_bb(location, *cleanup);
|
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-05-31 15:21:09 +02:00
|
|
|
self.check_bb(location, *target);
|
2020-05-31 09:52:51 +02:00
|
|
|
if let Some(cleanup) = cleanup {
|
2020-05-31 15:21:09 +02:00
|
|
|
self.check_bb(location, *cleanup);
|
2020-05-31 09:52:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
TerminatorKind::Yield { resume, drop, .. } => {
|
2020-05-31 15:21:09 +02:00
|
|
|
self.check_bb(location, *resume);
|
2020-05-31 09:52:51 +02:00
|
|
|
if let Some(drop) = drop {
|
2020-05-31 15:21:09 +02:00
|
|
|
self.check_bb(location, *drop);
|
2020-05-31 09:52:51 +02:00
|
|
|
}
|
|
|
|
}
|
2020-06-02 09:15:24 +02:00
|
|
|
TerminatorKind::FalseEdge { real_target, imaginary_target } => {
|
2020-05-31 15:21:09 +02:00
|
|
|
self.check_bb(location, *real_target);
|
|
|
|
self.check_bb(location, *imaginary_target);
|
2020-05-31 09:52:51 +02:00
|
|
|
}
|
|
|
|
TerminatorKind::FalseUnwind { real_target, unwind } => {
|
2020-05-31 15:21:09 +02:00
|
|
|
self.check_bb(location, *real_target);
|
2020-05-31 09:52:51 +02:00
|
|
|
if let Some(unwind) = unwind {
|
2020-05-31 15:21:09 +02:00
|
|
|
self.check_bb(location, *unwind);
|
2020-05-31 09:52:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
TerminatorKind::InlineAsm { destination, .. } => {
|
|
|
|
if let Some(destination) = destination {
|
2020-05-31 15:21:09 +02:00
|
|
|
self.check_bb(location, *destination);
|
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
|
|
|
}
|