2023-12-12 00:00:00 +00:00
|
|
|
//! This pass statically detects code which has undefined behaviour or is likely to be erroneous.
|
|
|
|
//! It can be used to locate problems in MIR building or optimizations. It assumes that all code
|
|
|
|
//! can be executed, so it has false positives.
|
2024-06-19 19:04:30 +00:00
|
|
|
|
2023-12-12 00:00:00 +00:00
|
|
|
use std::borrow::Cow;
|
2024-07-28 22:13:50 +00:00
|
|
|
|
2024-01-04 13:24:41 +00:00
|
|
|
use rustc_data_structures::fx::FxHashSet;
|
2023-12-12 00:00:00 +00:00
|
|
|
use rustc_index::bit_set::BitSet;
|
|
|
|
use rustc_middle::mir::visit::{PlaceContext, Visitor};
|
|
|
|
use rustc_middle::mir::*;
|
|
|
|
use rustc_middle::ty::TyCtxt;
|
2023-12-17 00:00:00 +00:00
|
|
|
use rustc_mir_dataflow::impls::{MaybeStorageDead, MaybeStorageLive};
|
2023-12-12 00:00:00 +00:00
|
|
|
use rustc_mir_dataflow::storage::always_storage_live_locals;
|
|
|
|
use rustc_mir_dataflow::{Analysis, ResultsCursor};
|
|
|
|
|
2024-08-27 23:39:59 +00:00
|
|
|
pub(super) fn lint_body<'tcx>(tcx: TyCtxt<'tcx>, body: &Body<'tcx>, when: String) {
|
2023-12-12 00:00:00 +00:00
|
|
|
let always_live_locals = &always_storage_live_locals(body);
|
2023-12-17 00:00:00 +00:00
|
|
|
|
|
|
|
let maybe_storage_live = MaybeStorageLive::new(Cow::Borrowed(always_live_locals))
|
|
|
|
.into_engine(tcx, body)
|
|
|
|
.iterate_to_fixpoint()
|
|
|
|
.into_results_cursor(body);
|
|
|
|
|
|
|
|
let maybe_storage_dead = MaybeStorageDead::new(Cow::Borrowed(always_live_locals))
|
2023-12-12 00:00:00 +00:00
|
|
|
.into_engine(tcx, body)
|
|
|
|
.iterate_to_fixpoint()
|
|
|
|
.into_results_cursor(body);
|
|
|
|
|
2023-12-30 00:00:00 +00:00
|
|
|
let mut lint = Lint {
|
2023-12-16 00:00:00 +00:00
|
|
|
tcx,
|
|
|
|
when,
|
|
|
|
body,
|
|
|
|
is_fn_like: tcx.def_kind(body.source.def_id()).is_fn_like(),
|
|
|
|
always_live_locals,
|
2023-12-17 00:00:00 +00:00
|
|
|
maybe_storage_live,
|
|
|
|
maybe_storage_dead,
|
2024-01-04 13:24:41 +00:00
|
|
|
places: Default::default(),
|
2023-12-30 00:00:00 +00:00
|
|
|
};
|
|
|
|
for (bb, data) in traversal::reachable(body) {
|
|
|
|
lint.visit_basic_block_data(bb, data);
|
2023-12-16 00:00:00 +00:00
|
|
|
}
|
2023-12-12 00:00:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
struct Lint<'a, 'tcx> {
|
|
|
|
tcx: TyCtxt<'tcx>,
|
|
|
|
when: String,
|
|
|
|
body: &'a Body<'tcx>,
|
2023-12-16 00:00:00 +00:00
|
|
|
is_fn_like: bool,
|
|
|
|
always_live_locals: &'a BitSet<Local>,
|
2023-12-17 00:00:00 +00:00
|
|
|
maybe_storage_live: ResultsCursor<'a, 'tcx, MaybeStorageLive<'a>>,
|
|
|
|
maybe_storage_dead: ResultsCursor<'a, 'tcx, MaybeStorageDead<'a>>,
|
2024-01-04 13:24:41 +00:00
|
|
|
places: FxHashSet<PlaceRef<'tcx>>,
|
2023-12-12 00:00:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, 'tcx> Lint<'a, 'tcx> {
|
|
|
|
#[track_caller]
|
|
|
|
fn fail(&self, location: Location, msg: impl AsRef<str>) {
|
|
|
|
let span = self.body.source_info(location).span;
|
|
|
|
self.tcx.sess.dcx().span_delayed_bug(
|
|
|
|
span,
|
|
|
|
format!(
|
|
|
|
"broken MIR in {:?} ({}) at {:?}:\n{}",
|
|
|
|
self.body.source.instance,
|
|
|
|
self.when,
|
|
|
|
location,
|
|
|
|
msg.as_ref()
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, 'tcx> Visitor<'tcx> for Lint<'a, 'tcx> {
|
|
|
|
fn visit_local(&mut self, local: Local, context: PlaceContext, location: Location) {
|
2023-12-30 00:00:00 +00:00
|
|
|
if context.is_use() {
|
2023-12-17 00:00:00 +00:00
|
|
|
self.maybe_storage_dead.seek_after_primary_effect(location);
|
|
|
|
if self.maybe_storage_dead.get().contains(local) {
|
2023-12-12 00:00:00 +00:00
|
|
|
self.fail(location, format!("use of local {local:?}, which has no storage here"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_statement(&mut self, statement: &Statement<'tcx>, location: Location) {
|
2024-01-04 13:24:41 +00:00
|
|
|
match &statement.kind {
|
|
|
|
StatementKind::Assign(box (dest, rvalue)) => {
|
|
|
|
if let Rvalue::Use(Operand::Copy(src) | Operand::Move(src)) = rvalue {
|
|
|
|
// The sides of an assignment must not alias. Currently this just checks whether
|
|
|
|
// the places are identical.
|
|
|
|
if dest == src {
|
|
|
|
self.fail(
|
|
|
|
location,
|
|
|
|
"encountered `Assign` statement with overlapping memory",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-12-12 00:00:00 +00:00
|
|
|
StatementKind::StorageLive(local) => {
|
2023-12-30 00:00:00 +00:00
|
|
|
self.maybe_storage_live.seek_before_primary_effect(location);
|
2024-01-04 13:24:41 +00:00
|
|
|
if self.maybe_storage_live.get().contains(*local) {
|
2023-12-30 00:00:00 +00:00
|
|
|
self.fail(
|
|
|
|
location,
|
|
|
|
format!("StorageLive({local:?}) which already has storage here"),
|
|
|
|
);
|
2023-12-12 00:00:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
|
|
|
|
self.super_statement(statement, location);
|
|
|
|
}
|
2023-12-16 00:00:00 +00:00
|
|
|
|
|
|
|
fn visit_terminator(&mut self, terminator: &Terminator<'tcx>, location: Location) {
|
2024-01-04 13:24:41 +00:00
|
|
|
match &terminator.kind {
|
2023-12-16 00:00:00 +00:00
|
|
|
TerminatorKind::Return => {
|
2023-12-30 00:00:00 +00:00
|
|
|
if self.is_fn_like {
|
2023-12-17 00:00:00 +00:00
|
|
|
self.maybe_storage_live.seek_after_primary_effect(location);
|
|
|
|
for local in self.maybe_storage_live.get().iter() {
|
2023-12-16 00:00:00 +00:00
|
|
|
if !self.always_live_locals.contains(local) {
|
|
|
|
self.fail(
|
|
|
|
location,
|
|
|
|
format!(
|
|
|
|
"local {local:?} still has storage when returning from function"
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-01-04 13:24:41 +00:00
|
|
|
TerminatorKind::Call { args, destination, .. } => {
|
|
|
|
// The call destination place and Operand::Move place used as an argument might be
|
|
|
|
// passed by a reference to the callee. Consequently they must be non-overlapping.
|
|
|
|
// Currently this simply checks for duplicate places.
|
|
|
|
self.places.clear();
|
|
|
|
self.places.insert(destination.as_ref());
|
|
|
|
let mut has_duplicates = false;
|
|
|
|
for arg in args {
|
2024-01-12 07:21:42 +00:00
|
|
|
if let Operand::Move(place) = &arg.node {
|
2024-01-04 13:24:41 +00:00
|
|
|
has_duplicates |= !self.places.insert(place.as_ref());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if has_duplicates {
|
|
|
|
self.fail(
|
|
|
|
location,
|
|
|
|
format!(
|
|
|
|
"encountered overlapping memory in `Move` arguments to `Call` terminator: {:?}",
|
|
|
|
terminator.kind,
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2023-12-16 00:00:00 +00:00
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
|
|
|
|
self.super_terminator(terminator, location);
|
|
|
|
}
|
2023-12-12 00:00:00 +00:00
|
|
|
}
|