Skip to content

Commit e04318e

Browse files
committedMay 25, 2020
Add a small MIR validation pass
1 parent 4ca6262 commit e04318e

File tree

4 files changed

+96
-1
lines changed

4 files changed

+96
-1
lines changed
 

‎src/librustc_interface/tests.rs

+1
Original file line numberDiff line numberDiff line change
@@ -511,6 +511,7 @@ fn test_debugging_options_tracking_hash() {
511511
untracked!(ui_testing, true);
512512
untracked!(unpretty, Some("expanded".to_string()));
513513
untracked!(unstable_options, true);
514+
untracked!(validate_mir, true);
514515
untracked!(verbose, true);
515516

516517
macro_rules! tracked {

‎src/librustc_mir/transform/mod.rs

+13-1
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ pub mod simplify_branches;
3939
pub mod simplify_try;
4040
pub mod uninhabited_enum_branching;
4141
pub mod unreachable_prop;
42+
pub mod validate;
4243

4344
pub(crate) fn provide(providers: &mut Providers<'_>) {
4445
self::check_unsafety::provide(providers);
@@ -147,12 +148,18 @@ pub fn run_passes(
147148
passes: &[&[&dyn MirPass<'tcx>]],
148149
) {
149150
let phase_index = mir_phase.phase_index();
151+
let source = MirSource { instance, promoted };
152+
let validate = tcx.sess.opts.debugging_opts.validate_mir;
150153

151154
if body.phase >= mir_phase {
152155
return;
153156
}
154157

155-
let source = MirSource { instance, promoted };
158+
if validate {
159+
validate::Validator { when: format!("input to phase {:?}", mir_phase) }
160+
.run_pass(tcx, source, body);
161+
}
162+
156163
let mut index = 0;
157164
let mut run_pass = |pass: &dyn MirPass<'tcx>| {
158165
let run_hooks = |body: &_, index, is_after| {
@@ -169,6 +176,11 @@ pub fn run_passes(
169176
pass.run_pass(tcx, source, body);
170177
run_hooks(body, index, true);
171178

179+
if validate {
180+
validate::Validator { when: format!("after {} in phase {:?}", pass.name(), mir_phase) }
181+
.run_pass(tcx, source, body);
182+
}
183+
172184
index += 1;
173185
};
174186

+80
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
//! Validates the MIR to ensure that invariants are upheld.
2+
3+
use super::{MirPass, MirSource};
4+
use rustc_middle::mir::visit::Visitor;
5+
use rustc_middle::{
6+
mir::{Body, Location, Operand, Rvalue, Statement, StatementKind},
7+
ty::{ParamEnv, TyCtxt},
8+
};
9+
use rustc_span::{def_id::DefId, Span, DUMMY_SP};
10+
11+
pub struct Validator {
12+
/// Describes at which point in the pipeline this validation is happening.
13+
pub when: String,
14+
}
15+
16+
impl<'tcx> MirPass<'tcx> for Validator {
17+
fn run_pass(&self, tcx: TyCtxt<'tcx>, source: MirSource<'tcx>, body: &mut Body<'tcx>) {
18+
let def_id = source.def_id();
19+
let param_env = tcx.param_env(def_id);
20+
TypeChecker { when: &self.when, def_id, body, tcx, param_env }.visit_body(body);
21+
}
22+
}
23+
24+
struct TypeChecker<'a, 'tcx> {
25+
when: &'a str,
26+
def_id: DefId,
27+
body: &'a Body<'tcx>,
28+
tcx: TyCtxt<'tcx>,
29+
param_env: ParamEnv<'tcx>,
30+
}
31+
32+
impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
33+
fn fail(&self, span: Span, msg: impl AsRef<str>) {
34+
// We use `delay_span_bug` as we might see broken MIR when other errors have already
35+
// occurred.
36+
self.tcx.sess.diagnostic().delay_span_bug(
37+
span,
38+
&format!("broken MIR in {:?} ({}): {}", self.def_id, self.when, msg.as_ref()),
39+
);
40+
}
41+
}
42+
43+
impl<'a, 'tcx> Visitor<'tcx> for TypeChecker<'a, 'tcx> {
44+
fn visit_operand(&mut self, operand: &Operand<'tcx>, location: Location) {
45+
// `Operand::Copy` is only supposed to be used with `Copy` types.
46+
if let Operand::Copy(place) = operand {
47+
let ty = place.ty(&self.body.local_decls, self.tcx).ty;
48+
49+
if !ty.is_copy_modulo_regions(self.tcx, self.param_env, DUMMY_SP) {
50+
self.fail(
51+
DUMMY_SP,
52+
format!("`Operand::Copy` with non-`Copy` type {} at {:?}", ty, location),
53+
);
54+
}
55+
}
56+
57+
self.super_operand(operand, location);
58+
}
59+
60+
fn visit_statement(&mut self, statement: &Statement<'tcx>, location: Location) {
61+
// The sides of an assignment must not alias. Currently this just checks whether the places
62+
// are identical.
63+
if let StatementKind::Assign(box (dest, rvalue)) = &statement.kind {
64+
match rvalue {
65+
Rvalue::Use(Operand::Copy(src) | Operand::Move(src)) => {
66+
if dest == src {
67+
self.fail(
68+
DUMMY_SP,
69+
format!(
70+
"encountered `Assign` statement with overlapping memory at {:?}",
71+
location
72+
),
73+
);
74+
}
75+
}
76+
_ => {}
77+
}
78+
}
79+
}
80+
}

‎src/librustc_session/options.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1045,6 +1045,8 @@ options! {DebuggingOptions, DebuggingSetter, basic_debugging_options,
10451045
"adds unstable command line options to rustc interface (default: no)"),
10461046
use_ctors_section: Option<bool> = (None, parse_opt_bool, [TRACKED],
10471047
"use legacy .ctors section for initializers rather than .init_array"),
1048+
validate_mir: bool = (false, parse_bool, [UNTRACKED],
1049+
"validate MIR after each transformation"),
10481050
verbose: bool = (false, parse_bool, [UNTRACKED],
10491051
"in general, enable more debug printouts (default: no)"),
10501052
verify_llvm_ir: bool = (false, parse_bool, [TRACKED],

0 commit comments

Comments
 (0)
Please sign in to comment.