2019-12-22 16:42:04 -06:00
|
|
|
use rustc_target::spec::abi::Abi;
|
2016-05-11 15:03:57 -05:00
|
|
|
use syntax::ast;
|
2019-05-07 22:21:18 -05:00
|
|
|
use syntax::symbol::sym;
|
2016-06-21 17:08:13 -05:00
|
|
|
use syntax_pos::Span;
|
2016-05-11 15:03:57 -05:00
|
|
|
|
2019-12-22 16:42:04 -06:00
|
|
|
use crate::transform::{MirPass, MirSource};
|
2019-03-17 05:36:10 -05:00
|
|
|
use rustc::hir::def_id::DefId;
|
2019-12-22 16:42:04 -06:00
|
|
|
use rustc::mir::{self, Body, BodyAndCache, Local, Location};
|
|
|
|
use rustc::ty::{self, Ty, TyCtxt};
|
2019-09-26 00:30:10 -05:00
|
|
|
use rustc_index::bit_set::BitSet;
|
2016-05-11 15:03:57 -05:00
|
|
|
|
2019-12-22 16:42:04 -06:00
|
|
|
use crate::dataflow::move_paths::{HasMoveData, MoveData};
|
|
|
|
use crate::dataflow::move_paths::{LookupResult, MovePathIndex};
|
2019-02-07 15:28:15 -06:00
|
|
|
use crate::dataflow::BitDenotation;
|
|
|
|
use crate::dataflow::DataflowResults;
|
2019-07-05 20:18:38 -05:00
|
|
|
use crate::dataflow::DataflowResultsCursor;
|
2019-12-22 16:42:04 -06:00
|
|
|
use crate::dataflow::IndirectlyMutableLocals;
|
|
|
|
use crate::dataflow::MoveDataParamEnv;
|
|
|
|
use crate::dataflow::{do_dataflow, DebugFormatted};
|
2019-02-07 15:28:15 -06:00
|
|
|
use crate::dataflow::{
|
2019-12-22 16:42:04 -06:00
|
|
|
DefinitelyInitializedPlaces, MaybeInitializedPlaces, MaybeUninitializedPlaces,
|
2019-02-07 15:28:15 -06:00
|
|
|
};
|
2017-06-26 07:57:26 -05:00
|
|
|
|
2019-02-07 15:28:15 -06:00
|
|
|
use crate::dataflow::has_rustc_mir_with;
|
2017-06-26 07:57:26 -05:00
|
|
|
|
|
|
|
pub struct SanityCheck;
|
|
|
|
|
2019-08-04 15:20:00 -05:00
|
|
|
impl<'tcx> MirPass<'tcx> for SanityCheck {
|
2019-12-03 10:51:58 -06:00
|
|
|
fn run_pass(&self, tcx: TyCtxt<'tcx>, src: MirSource<'tcx>, body: &mut BodyAndCache<'tcx>) {
|
2019-02-03 04:51:07 -06:00
|
|
|
let def_id = src.def_id();
|
2019-05-07 22:21:18 -05:00
|
|
|
if !tcx.has_attr(def_id, sym::rustc_mir) {
|
2018-12-19 04:31:35 -06:00
|
|
|
debug!("skipping rustc_peek::SanityCheck on {}", tcx.def_path_str(def_id));
|
2017-06-26 07:57:26 -05:00
|
|
|
return;
|
|
|
|
} else {
|
2018-12-19 04:31:35 -06:00
|
|
|
debug!("running rustc_peek::SanityCheck on {}", tcx.def_path_str(def_id));
|
2017-06-26 07:57:26 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
let attributes = tcx.get_attrs(def_id);
|
|
|
|
let param_env = tcx.param_env(def_id);
|
2019-11-22 16:03:25 -06:00
|
|
|
let move_data = MoveData::gather_moves(body, tcx, param_env).unwrap();
|
2017-06-26 07:57:26 -05:00
|
|
|
let mdpe = MoveDataParamEnv { move_data: move_data, param_env: param_env };
|
2019-11-06 11:00:46 -06:00
|
|
|
let dead_unwinds = BitSet::new_empty(body.basic_blocks().len());
|
2019-12-22 16:42:04 -06:00
|
|
|
let flow_inits = do_dataflow(
|
|
|
|
tcx,
|
|
|
|
body,
|
|
|
|
def_id,
|
|
|
|
&attributes,
|
|
|
|
&dead_unwinds,
|
|
|
|
MaybeInitializedPlaces::new(tcx, body, &mdpe),
|
|
|
|
|bd, i| DebugFormatted::new(&bd.move_data().move_paths[i]),
|
|
|
|
);
|
|
|
|
let flow_uninits = do_dataflow(
|
|
|
|
tcx,
|
|
|
|
body,
|
|
|
|
def_id,
|
|
|
|
&attributes,
|
|
|
|
&dead_unwinds,
|
|
|
|
MaybeUninitializedPlaces::new(tcx, body, &mdpe),
|
|
|
|
|bd, i| DebugFormatted::new(&bd.move_data().move_paths[i]),
|
|
|
|
);
|
|
|
|
let flow_def_inits = do_dataflow(
|
|
|
|
tcx,
|
|
|
|
body,
|
|
|
|
def_id,
|
|
|
|
&attributes,
|
|
|
|
&dead_unwinds,
|
|
|
|
DefinitelyInitializedPlaces::new(tcx, body, &mdpe),
|
|
|
|
|bd, i| DebugFormatted::new(&bd.move_data().move_paths[i]),
|
|
|
|
);
|
|
|
|
let flow_indirectly_mut = do_dataflow(
|
|
|
|
tcx,
|
|
|
|
body,
|
|
|
|
def_id,
|
|
|
|
&attributes,
|
|
|
|
&dead_unwinds,
|
|
|
|
IndirectlyMutableLocals::new(tcx, body, param_env),
|
|
|
|
|_, i| DebugFormatted::new(&i),
|
|
|
|
);
|
2017-06-26 07:57:26 -05:00
|
|
|
|
2019-05-07 22:21:18 -05:00
|
|
|
if has_rustc_mir_with(&attributes, sym::rustc_peek_maybe_init).is_some() {
|
2019-11-06 11:00:46 -06:00
|
|
|
sanity_check_via_rustc_peek(tcx, body, def_id, &attributes, &flow_inits);
|
2017-06-26 07:57:26 -05:00
|
|
|
}
|
2019-05-07 22:21:18 -05:00
|
|
|
if has_rustc_mir_with(&attributes, sym::rustc_peek_maybe_uninit).is_some() {
|
2019-11-06 11:00:46 -06:00
|
|
|
sanity_check_via_rustc_peek(tcx, body, def_id, &attributes, &flow_uninits);
|
2017-06-26 07:57:26 -05:00
|
|
|
}
|
2019-05-07 22:21:18 -05:00
|
|
|
if has_rustc_mir_with(&attributes, sym::rustc_peek_definite_init).is_some() {
|
2019-12-22 16:42:04 -06:00
|
|
|
sanity_check_via_rustc_peek(tcx, body, def_id, &attributes, &flow_def_inits);
|
2017-06-26 07:57:26 -05:00
|
|
|
}
|
2019-10-01 22:14:01 -05:00
|
|
|
if has_rustc_mir_with(&attributes, sym::rustc_peek_indirectly_mutable).is_some() {
|
2019-12-22 16:42:04 -06:00
|
|
|
sanity_check_via_rustc_peek(tcx, body, def_id, &attributes, &flow_indirectly_mut);
|
2019-10-01 22:14:01 -05:00
|
|
|
}
|
2019-05-07 22:21:18 -05:00
|
|
|
if has_rustc_mir_with(&attributes, sym::stop_after_dataflow).is_some() {
|
2017-06-26 07:57:26 -05:00
|
|
|
tcx.sess.fatal("stop_after_dataflow ended compilation");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-05-11 15:03:57 -05:00
|
|
|
|
2016-05-17 01:37:36 -05:00
|
|
|
/// This function scans `mir` for all calls to the intrinsic
|
|
|
|
/// `rustc_peek` that have the expression form `rustc_peek(&expr)`.
|
|
|
|
///
|
|
|
|
/// For each such call, determines what the dataflow bit-state is for
|
|
|
|
/// the L-value corresponding to `expr`; if the bit-state is a 1, then
|
|
|
|
/// that call to `rustc_peek` is ignored by the sanity check. If the
|
|
|
|
/// bit-state is a 0, then this pass emits a error message saying
|
|
|
|
/// "rustc_peek: bit not set".
|
|
|
|
///
|
|
|
|
/// The intention is that one can write unit tests for dataflow by
|
|
|
|
/// putting code into a compile-fail test and using `rustc_peek` to
|
|
|
|
/// make observations about the results of dataflow static analyses.
|
|
|
|
///
|
|
|
|
/// (If there are any calls to `rustc_peek` that do not match the
|
|
|
|
/// expression form above, then that emits an error as well, but those
|
|
|
|
/// errors are not intended to be used for unit tests.)
|
2019-06-11 16:11:55 -05:00
|
|
|
pub fn sanity_check_via_rustc_peek<'tcx, O>(
|
2019-06-13 16:48:52 -05:00
|
|
|
tcx: TyCtxt<'tcx>,
|
2019-06-11 16:11:55 -05:00
|
|
|
body: &Body<'tcx>,
|
|
|
|
def_id: DefId,
|
|
|
|
_attributes: &[ast::Attribute],
|
|
|
|
results: &DataflowResults<'tcx, O>,
|
2019-12-22 16:42:04 -06:00
|
|
|
) where
|
|
|
|
O: RustcPeekAt<'tcx>,
|
|
|
|
{
|
2019-03-17 05:36:10 -05:00
|
|
|
debug!("sanity_check_via_rustc_peek def_id: {:?}", def_id);
|
2016-05-11 15:03:57 -05:00
|
|
|
|
2019-07-05 20:18:38 -05:00
|
|
|
let mut cursor = DataflowResultsCursor::new(results, body);
|
2016-05-11 15:03:57 -05:00
|
|
|
|
2019-12-22 16:42:04 -06:00
|
|
|
let peek_calls = body.basic_blocks().iter_enumerated().filter_map(|(bb, block_data)| {
|
|
|
|
PeekCall::from_terminator(tcx, block_data.terminator()).map(|call| (bb, block_data, call))
|
|
|
|
});
|
2019-07-05 20:18:38 -05:00
|
|
|
|
|
|
|
for (bb, block_data, call) in peek_calls {
|
|
|
|
// Look for a sequence like the following to indicate that we should be peeking at `_1`:
|
|
|
|
// _2 = &_1;
|
|
|
|
// rustc_peek(_2);
|
|
|
|
//
|
|
|
|
// /* or */
|
|
|
|
//
|
|
|
|
// _2 = _1;
|
|
|
|
// rustc_peek(_2);
|
|
|
|
let (statement_index, peek_rval) = block_data
|
|
|
|
.statements
|
|
|
|
.iter()
|
|
|
|
.enumerate()
|
|
|
|
.filter_map(|(i, stmt)| value_assigned_to_local(stmt, call.arg).map(|rval| (i, rval)))
|
|
|
|
.next()
|
2019-12-22 16:42:04 -06:00
|
|
|
.expect(
|
|
|
|
"call to rustc_peek should be preceded by \
|
|
|
|
assignment to temporary holding its argument",
|
|
|
|
);
|
2019-07-05 20:18:38 -05:00
|
|
|
|
|
|
|
match (call.kind, peek_rval) {
|
2019-12-22 16:42:04 -06:00
|
|
|
(PeekCallKind::ByRef, mir::Rvalue::Ref(_, _, place))
|
2019-07-05 20:18:38 -05:00
|
|
|
| (PeekCallKind::ByVal, mir::Rvalue::Use(mir::Operand::Move(place)))
|
2019-12-22 16:42:04 -06:00
|
|
|
| (PeekCallKind::ByVal, mir::Rvalue::Use(mir::Operand::Copy(place))) => {
|
2019-07-05 20:18:38 -05:00
|
|
|
let loc = Location { block: bb, statement_index };
|
|
|
|
cursor.seek(loc);
|
|
|
|
let state = cursor.get();
|
|
|
|
results.operator().peek_at(tcx, place, state, call);
|
2016-05-24 05:56:02 -05:00
|
|
|
}
|
2019-07-05 20:18:38 -05:00
|
|
|
|
|
|
|
_ => {
|
2016-06-11 15:47:28 -05:00
|
|
|
let msg = "rustc_peek: argument expression \
|
2019-07-05 20:18:38 -05:00
|
|
|
must be either `place` or `&place`";
|
|
|
|
tcx.sess.span_err(call.span, msg);
|
2016-05-24 05:56:02 -05:00
|
|
|
}
|
2016-05-11 15:03:57 -05:00
|
|
|
}
|
2019-07-05 20:18:38 -05:00
|
|
|
}
|
|
|
|
}
|
2016-05-24 16:03:52 -05:00
|
|
|
|
2019-07-05 20:18:38 -05:00
|
|
|
/// If `stmt` is an assignment where the LHS is the given local (with no projections), returns the
|
|
|
|
/// RHS of the assignment.
|
|
|
|
fn value_assigned_to_local<'a, 'tcx>(
|
|
|
|
stmt: &'a mir::Statement<'tcx>,
|
|
|
|
local: Local,
|
|
|
|
) -> Option<&'a mir::Rvalue<'tcx>> {
|
|
|
|
if let mir::StatementKind::Assign(box (place, rvalue)) = &stmt.kind {
|
2019-10-20 15:09:36 -05:00
|
|
|
if let Some(l) = place.as_local() {
|
|
|
|
if local == l {
|
2019-07-05 20:18:38 -05:00
|
|
|
return Some(&*rvalue);
|
|
|
|
}
|
|
|
|
}
|
2016-05-11 15:03:57 -05:00
|
|
|
}
|
2016-05-20 06:18:03 -05:00
|
|
|
|
2019-07-05 20:18:38 -05:00
|
|
|
None
|
|
|
|
}
|
2017-12-23 16:45:53 -06:00
|
|
|
|
2019-07-05 20:18:38 -05:00
|
|
|
#[derive(Clone, Copy, Debug)]
|
|
|
|
enum PeekCallKind {
|
|
|
|
ByVal,
|
|
|
|
ByRef,
|
2016-05-20 06:18:03 -05:00
|
|
|
}
|
|
|
|
|
2019-07-05 20:18:38 -05:00
|
|
|
impl PeekCallKind {
|
|
|
|
fn from_arg_ty(arg: Ty<'_>) -> Self {
|
|
|
|
match arg.kind {
|
|
|
|
ty::Ref(_, _, _) => PeekCallKind::ByRef,
|
|
|
|
_ => PeekCallKind::ByVal,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone, Copy, Debug)]
|
|
|
|
pub struct PeekCall {
|
|
|
|
arg: Local,
|
|
|
|
kind: PeekCallKind,
|
|
|
|
span: Span,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl PeekCall {
|
|
|
|
fn from_terminator<'tcx>(
|
|
|
|
tcx: TyCtxt<'tcx>,
|
|
|
|
terminator: &mir::Terminator<'tcx>,
|
|
|
|
) -> Option<Self> {
|
2019-10-20 15:09:36 -05:00
|
|
|
use mir::Operand;
|
2019-07-05 20:18:38 -05:00
|
|
|
|
|
|
|
let span = terminator.source_info.span;
|
|
|
|
if let mir::TerminatorKind::Call { func: Operand::Constant(func), args, .. } =
|
|
|
|
&terminator.kind
|
|
|
|
{
|
|
|
|
if let ty::FnDef(def_id, substs) = func.literal.ty.kind {
|
|
|
|
let sig = tcx.fn_sig(def_id);
|
|
|
|
let name = tcx.item_name(def_id);
|
|
|
|
if sig.abi() != Abi::RustIntrinsic || name != sym::rustc_peek {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
|
|
|
assert_eq!(args.len(), 1);
|
|
|
|
let kind = PeekCallKind::from_arg_ty(substs.type_at(0));
|
2019-10-20 15:09:36 -05:00
|
|
|
let arg = match &args[0] {
|
|
|
|
Operand::Copy(place) | Operand::Move(place) => {
|
|
|
|
if let Some(local) = place.as_local() {
|
|
|
|
local
|
|
|
|
} else {
|
|
|
|
tcx.sess.diagnostic().span_err(
|
|
|
|
span,
|
|
|
|
"dataflow::sanity_check cannot feed a non-temp to rustc_peek.",
|
|
|
|
);
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
}
|
2019-07-05 20:18:38 -05:00
|
|
|
_ => {
|
|
|
|
tcx.sess.diagnostic().span_err(
|
2019-10-20 15:09:36 -05:00
|
|
|
span,
|
|
|
|
"dataflow::sanity_check cannot feed a non-temp to rustc_peek.",
|
|
|
|
);
|
2019-07-05 20:18:38 -05:00
|
|
|
return None;
|
2016-05-20 06:18:03 -05:00
|
|
|
}
|
2019-07-05 20:18:38 -05:00
|
|
|
};
|
|
|
|
|
2019-12-22 16:42:04 -06:00
|
|
|
return Some(PeekCall { arg, kind, span });
|
2019-07-05 20:18:38 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub trait RustcPeekAt<'tcx>: BitDenotation<'tcx> {
|
|
|
|
fn peek_at(
|
|
|
|
&self,
|
|
|
|
tcx: TyCtxt<'tcx>,
|
|
|
|
place: &mir::Place<'tcx>,
|
|
|
|
flow_state: &BitSet<Self::Idx>,
|
|
|
|
call: PeekCall,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'tcx, O> RustcPeekAt<'tcx> for O
|
2019-12-22 16:42:04 -06:00
|
|
|
where
|
|
|
|
O: BitDenotation<'tcx, Idx = MovePathIndex> + HasMoveData<'tcx>,
|
2019-07-05 20:18:38 -05:00
|
|
|
{
|
|
|
|
fn peek_at(
|
|
|
|
&self,
|
|
|
|
tcx: TyCtxt<'tcx>,
|
|
|
|
place: &mir::Place<'tcx>,
|
|
|
|
flow_state: &BitSet<Self::Idx>,
|
|
|
|
call: PeekCall,
|
|
|
|
) {
|
|
|
|
match self.move_data().rev_lookup.find(place.as_ref()) {
|
|
|
|
LookupResult::Exact(peek_mpi) => {
|
|
|
|
let bit_state = flow_state.contains(peek_mpi);
|
2019-12-22 16:42:04 -06:00
|
|
|
debug!("rustc_peek({:?} = &{:?}) bit_state: {}", call.arg, place, bit_state);
|
2019-07-05 20:18:38 -05:00
|
|
|
if !bit_state {
|
|
|
|
tcx.sess.span_err(call.span, "rustc_peek: bit not set");
|
2016-05-20 06:18:03 -05:00
|
|
|
}
|
|
|
|
}
|
2019-10-01 22:14:01 -05:00
|
|
|
|
2019-07-05 20:18:38 -05:00
|
|
|
LookupResult::Parent(..) => {
|
|
|
|
tcx.sess.span_err(call.span, "rustc_peek: argument untracked");
|
|
|
|
}
|
2016-05-20 06:18:03 -05:00
|
|
|
}
|
|
|
|
}
|
2016-05-11 15:03:57 -05:00
|
|
|
}
|
2019-10-01 22:14:01 -05:00
|
|
|
|
|
|
|
impl<'tcx> RustcPeekAt<'tcx> for IndirectlyMutableLocals<'_, 'tcx> {
|
|
|
|
fn peek_at(
|
|
|
|
&self,
|
|
|
|
tcx: TyCtxt<'tcx>,
|
|
|
|
place: &mir::Place<'tcx>,
|
|
|
|
flow_state: &BitSet<Local>,
|
|
|
|
call: PeekCall,
|
|
|
|
) {
|
|
|
|
warn!("peek_at: place={:?}", place);
|
2019-10-20 15:09:36 -05:00
|
|
|
let local = if let Some(l) = place.as_local() {
|
|
|
|
l
|
|
|
|
} else {
|
|
|
|
tcx.sess.span_err(call.span, "rustc_peek: argument was not a local");
|
|
|
|
return;
|
2019-10-01 22:14:01 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
if !flow_state.contains(local) {
|
|
|
|
tcx.sess.span_err(call.span, "rustc_peek: bit not set");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|