2022-08-25 16:43:46 +00:00
|
|
|
|
//! This module provides a framework on top of the normal MIR dataflow framework to simplify the
|
2022-10-21 15:02:03 +00:00
|
|
|
|
//! implementation of analyses that track information about the values stored in certain places.
|
|
|
|
|
//! We are using the term "place" here to refer to a `mir::Place` (a place expression) instead of
|
|
|
|
|
//! an `interpret::Place` (a memory location).
|
2022-08-25 16:43:46 +00:00
|
|
|
|
//!
|
|
|
|
|
//! The default methods of [`ValueAnalysis`] (prefixed with `super_` instead of `handle_`)
|
|
|
|
|
//! provide some behavior that should be valid for all abstract domains that are based only on the
|
|
|
|
|
//! value stored in a certain place. On top of these default rules, an implementation should
|
|
|
|
|
//! override some of the `handle_` methods. For an example, see `ConstAnalysis`.
|
|
|
|
|
//!
|
2022-10-21 15:02:03 +00:00
|
|
|
|
//! An implementation must also provide a [`Map`]. Before the analysis begins, all places that
|
|
|
|
|
//! should be tracked during the analysis must be registered. During the analysis, no new places
|
|
|
|
|
//! can be registered. The [`State`] can be queried to retrieve the abstract value stored for a
|
|
|
|
|
//! certain place by passing the map.
|
2022-08-30 22:56:39 +00:00
|
|
|
|
//!
|
2022-10-21 15:02:03 +00:00
|
|
|
|
//! This framework is currently experimental. In particular, the features related to references are
|
|
|
|
|
//! currently guarded behind `-Zunsound-mir-opts`, because their correctness relies on Stacked
|
|
|
|
|
//! Borrows. Also, only places with scalar types can be tracked currently. This is because scalar
|
|
|
|
|
//! types are indivisible, which simplifies the current implementation. But this limitation could be
|
|
|
|
|
//! lifted in the future.
|
2022-08-31 15:43:53 +00:00
|
|
|
|
//!
|
|
|
|
|
//!
|
2022-10-21 15:02:03 +00:00
|
|
|
|
//! # Notes
|
2022-08-31 15:43:53 +00:00
|
|
|
|
//!
|
2022-10-21 15:02:03 +00:00
|
|
|
|
//! - The bottom state denotes uninitialized memory. Because we are only doing a sound approximation
|
|
|
|
|
//! of the actual execution, we can also use this state for places where access would be UB.
|
2022-08-31 15:43:53 +00:00
|
|
|
|
//!
|
2022-10-21 15:02:03 +00:00
|
|
|
|
//! - The assignment logic in `State::assign_place_idx` assumes that the places are non-overlapping,
|
|
|
|
|
//! or identical. Note that this refers to place expressions, not memory locations.
|
2022-08-31 15:43:53 +00:00
|
|
|
|
//!
|
2022-10-21 15:02:03 +00:00
|
|
|
|
//! - Since pointers (and mutable references) are not tracked, but can be used to change the
|
|
|
|
|
//! underlying values, we are conservative and immediately flood the referenced place upon creation
|
|
|
|
|
//! of the pointer. Also, we have to uphold the invariant that the place must stay that way as long
|
|
|
|
|
//! as this mutable access could exist. However...
|
2022-08-31 15:43:53 +00:00
|
|
|
|
//!
|
2022-10-21 15:02:03 +00:00
|
|
|
|
//! - Without an aliasing model like Stacked Borrows (i.e., `-Zunsound-mir-opts` is not given),
|
|
|
|
|
//! such mutable access is never revoked. And even shared references could be used to obtain the
|
|
|
|
|
//! address of a value an modify it. When not assuming Stacked Borrows, we prevent such places from
|
|
|
|
|
//! being tracked at all. This means that the analysis itself can assume that writes to a *tracked*
|
|
|
|
|
//! place always invalidate all other means of mutable access, regardless of the aliasing model.
|
2022-10-05 19:30:43 +00:00
|
|
|
|
//!
|
2022-10-21 15:02:03 +00:00
|
|
|
|
//! - Likewise, the analysis itself assumes that if the value of a *tracked* place behind a shared
|
|
|
|
|
//! reference is changed, the reference may not be used to access that value anymore. This is true
|
|
|
|
|
//! for all places if the referenced type is `Freeze` and we assume Stacked Borrows. If we are not
|
|
|
|
|
//! assuming Stacking Borrows (or if the referenced type could be `!Freeze`), we again prevent such
|
|
|
|
|
//! places from being tracked at all, making this assertion trivially true.
|
2022-08-25 16:43:46 +00:00
|
|
|
|
|
|
|
|
|
use std::fmt::{Debug, Formatter};
|
|
|
|
|
|
|
|
|
|
use rustc_data_structures::fx::FxHashMap;
|
|
|
|
|
use rustc_index::vec::IndexVec;
|
2022-09-27 14:16:09 +00:00
|
|
|
|
use rustc_middle::mir::tcx::PlaceTy;
|
2022-10-06 23:10:10 +00:00
|
|
|
|
use rustc_middle::mir::visit::{PlaceContext, Visitor};
|
2022-08-25 16:43:46 +00:00
|
|
|
|
use rustc_middle::mir::*;
|
|
|
|
|
use rustc_middle::ty::{self, Ty, TyCtxt};
|
2022-10-06 22:09:36 +00:00
|
|
|
|
use rustc_span::DUMMY_SP;
|
2022-08-25 16:43:46 +00:00
|
|
|
|
use rustc_target::abi::VariantIdx;
|
|
|
|
|
|
|
|
|
|
use crate::{
|
|
|
|
|
fmt::DebugWithContext, lattice::FlatSet, Analysis, AnalysisDomain, CallReturnPlaces,
|
|
|
|
|
JoinSemiLattice, SwitchIntEdgeEffects,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
pub trait ValueAnalysis<'tcx> {
|
|
|
|
|
/// For each place of interest, the analysis tracks a value of the given type.
|
|
|
|
|
type Value: Clone + JoinSemiLattice + HasBottom + HasTop;
|
|
|
|
|
|
|
|
|
|
const NAME: &'static str;
|
|
|
|
|
|
|
|
|
|
fn map(&self) -> ⤅
|
|
|
|
|
|
|
|
|
|
fn handle_statement(&self, statement: &Statement<'tcx>, state: &mut State<Self::Value>) {
|
|
|
|
|
self.super_statement(statement, state)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn super_statement(&self, statement: &Statement<'tcx>, state: &mut State<Self::Value>) {
|
|
|
|
|
match &statement.kind {
|
|
|
|
|
StatementKind::Assign(box (place, rvalue)) => {
|
|
|
|
|
self.handle_assign(*place, rvalue, state);
|
|
|
|
|
}
|
|
|
|
|
StatementKind::SetDiscriminant { .. } => {
|
2022-08-29 21:03:30 +00:00
|
|
|
|
// Could treat this as writing a constant to a pseudo-place.
|
2022-09-09 09:56:10 +00:00
|
|
|
|
// But discriminants are currently not tracked, so we do nothing.
|
|
|
|
|
// Related: https://github.com/rust-lang/unsafe-code-guidelines/issues/84
|
2022-08-25 16:43:46 +00:00
|
|
|
|
}
|
2022-10-04 23:24:33 +00:00
|
|
|
|
StatementKind::Intrinsic(box intrinsic) => {
|
|
|
|
|
self.handle_intrinsic(intrinsic, state);
|
2022-08-25 16:43:46 +00:00
|
|
|
|
}
|
2022-09-09 09:56:10 +00:00
|
|
|
|
StatementKind::StorageLive(local) | StatementKind::StorageDead(local) => {
|
2022-10-21 15:02:03 +00:00
|
|
|
|
// StorageLive leaves the local in an uninitialized state.
|
|
|
|
|
// StorageDead makes it UB to access the local afterwards.
|
2022-10-05 18:46:39 +00:00
|
|
|
|
state.flood_with(Place::from(*local).as_ref(), self.map(), Self::Value::bottom());
|
2022-09-04 10:29:19 +00:00
|
|
|
|
}
|
|
|
|
|
StatementKind::Deinit(box place) => {
|
2022-10-21 15:02:03 +00:00
|
|
|
|
// Deinit makes the place uninitialized.
|
2022-10-05 18:46:39 +00:00
|
|
|
|
state.flood_with(place.as_ref(), self.map(), Self::Value::bottom());
|
2022-08-25 16:43:46 +00:00
|
|
|
|
}
|
2022-10-12 21:46:31 +00:00
|
|
|
|
StatementKind::Retag(..) => {
|
|
|
|
|
// A retag modifies the provenance of references. Currently references are only
|
|
|
|
|
// tracked if `-Zunsound-mir-opts` is given, but this might change in the future.
|
|
|
|
|
// However, it is still unclear how retags should be handled:
|
|
|
|
|
// https://github.com/rust-lang/rust/pull/101168#discussion_r985304895
|
|
|
|
|
}
|
2022-08-25 16:43:46 +00:00
|
|
|
|
StatementKind::Nop
|
|
|
|
|
| StatementKind::FakeRead(..)
|
|
|
|
|
| StatementKind::Coverage(..)
|
|
|
|
|
| StatementKind::AscribeUserType(..) => (),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-04 23:24:33 +00:00
|
|
|
|
fn handle_intrinsic(
|
|
|
|
|
&self,
|
|
|
|
|
intrinsic: &NonDivergingIntrinsic<'tcx>,
|
|
|
|
|
state: &mut State<Self::Value>,
|
|
|
|
|
) {
|
|
|
|
|
self.super_intrinsic(intrinsic, state);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn super_intrinsic(
|
|
|
|
|
&self,
|
2022-10-05 18:19:11 +00:00
|
|
|
|
intrinsic: &NonDivergingIntrinsic<'tcx>,
|
|
|
|
|
state: &mut State<Self::Value>,
|
2022-10-04 23:24:33 +00:00
|
|
|
|
) {
|
2022-10-05 18:19:11 +00:00
|
|
|
|
match intrinsic {
|
|
|
|
|
NonDivergingIntrinsic::Assume(..) => {
|
|
|
|
|
// Could use this, but ignoring it is sound.
|
|
|
|
|
}
|
|
|
|
|
NonDivergingIntrinsic::CopyNonOverlapping(CopyNonOverlapping { dst, .. }) => {
|
|
|
|
|
if let Some(place) = dst.place() {
|
|
|
|
|
state.flood(place.as_ref(), self.map());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-10-04 23:24:33 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-08-25 16:43:46 +00:00
|
|
|
|
fn handle_assign(
|
|
|
|
|
&self,
|
|
|
|
|
target: Place<'tcx>,
|
|
|
|
|
rvalue: &Rvalue<'tcx>,
|
|
|
|
|
state: &mut State<Self::Value>,
|
|
|
|
|
) {
|
|
|
|
|
self.super_assign(target, rvalue, state)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn super_assign(
|
|
|
|
|
&self,
|
|
|
|
|
target: Place<'tcx>,
|
|
|
|
|
rvalue: &Rvalue<'tcx>,
|
|
|
|
|
state: &mut State<Self::Value>,
|
|
|
|
|
) {
|
2022-09-01 12:17:15 +00:00
|
|
|
|
let result = self.handle_rvalue(rvalue, state);
|
|
|
|
|
state.assign(target.as_ref(), result, self.map());
|
2022-08-25 16:43:46 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn handle_rvalue(
|
|
|
|
|
&self,
|
|
|
|
|
rvalue: &Rvalue<'tcx>,
|
|
|
|
|
state: &mut State<Self::Value>,
|
2022-09-01 12:17:15 +00:00
|
|
|
|
) -> ValueOrPlaceOrRef<Self::Value> {
|
2022-08-25 16:43:46 +00:00
|
|
|
|
self.super_rvalue(rvalue, state)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn super_rvalue(
|
|
|
|
|
&self,
|
|
|
|
|
rvalue: &Rvalue<'tcx>,
|
|
|
|
|
state: &mut State<Self::Value>,
|
2022-09-01 12:17:15 +00:00
|
|
|
|
) -> ValueOrPlaceOrRef<Self::Value> {
|
2022-08-25 16:43:46 +00:00
|
|
|
|
match rvalue {
|
2022-09-01 12:17:15 +00:00
|
|
|
|
Rvalue::Use(operand) => self.handle_operand(operand, state).into(),
|
|
|
|
|
Rvalue::Ref(_, BorrowKind::Shared, place) => self
|
|
|
|
|
.map()
|
|
|
|
|
.find(place.as_ref())
|
|
|
|
|
.map(ValueOrPlaceOrRef::Ref)
|
2022-10-05 20:01:33 +00:00
|
|
|
|
.unwrap_or(ValueOrPlaceOrRef::top()),
|
2022-09-01 12:17:15 +00:00
|
|
|
|
Rvalue::Ref(_, _, place) | Rvalue::AddressOf(_, place) => {
|
2022-10-19 13:56:58 +00:00
|
|
|
|
// This is not a `&x` reference and could be used for modification.
|
2022-09-01 12:17:15 +00:00
|
|
|
|
state.flood(place.as_ref(), self.map());
|
2022-10-05 20:01:33 +00:00
|
|
|
|
ValueOrPlaceOrRef::top()
|
2022-09-01 12:17:15 +00:00
|
|
|
|
}
|
|
|
|
|
Rvalue::CopyForDeref(place) => {
|
|
|
|
|
self.handle_operand(&Operand::Copy(*place), state).into()
|
2022-08-25 16:43:46 +00:00
|
|
|
|
}
|
2022-10-19 13:56:58 +00:00
|
|
|
|
Rvalue::Repeat(..)
|
|
|
|
|
| Rvalue::ThreadLocalRef(..)
|
|
|
|
|
| Rvalue::Len(..)
|
|
|
|
|
| Rvalue::Cast(..)
|
|
|
|
|
| Rvalue::BinaryOp(..)
|
|
|
|
|
| Rvalue::CheckedBinaryOp(..)
|
|
|
|
|
| Rvalue::NullaryOp(..)
|
|
|
|
|
| Rvalue::UnaryOp(..)
|
|
|
|
|
| Rvalue::Discriminant(..)
|
|
|
|
|
| Rvalue::Aggregate(..)
|
|
|
|
|
| Rvalue::ShallowInitBox(..) => {
|
|
|
|
|
// No modification is possible through these r-values.
|
|
|
|
|
ValueOrPlaceOrRef::top()
|
|
|
|
|
}
|
2022-08-25 16:43:46 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn handle_operand(
|
|
|
|
|
&self,
|
|
|
|
|
operand: &Operand<'tcx>,
|
|
|
|
|
state: &mut State<Self::Value>,
|
|
|
|
|
) -> ValueOrPlace<Self::Value> {
|
|
|
|
|
self.super_operand(operand, state)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn super_operand(
|
|
|
|
|
&self,
|
|
|
|
|
operand: &Operand<'tcx>,
|
|
|
|
|
state: &mut State<Self::Value>,
|
|
|
|
|
) -> ValueOrPlace<Self::Value> {
|
|
|
|
|
match operand {
|
|
|
|
|
Operand::Constant(box constant) => {
|
|
|
|
|
ValueOrPlace::Value(self.handle_constant(constant, state))
|
|
|
|
|
}
|
|
|
|
|
Operand::Copy(place) | Operand::Move(place) => {
|
2022-10-05 19:30:43 +00:00
|
|
|
|
// On move, we would ideally flood the place with bottom. But with the current
|
|
|
|
|
// framework this is not possible (similar to `InterpCx::eval_operand`).
|
2022-08-25 16:43:46 +00:00
|
|
|
|
self.map()
|
|
|
|
|
.find(place.as_ref())
|
|
|
|
|
.map(ValueOrPlace::Place)
|
2022-10-05 20:01:33 +00:00
|
|
|
|
.unwrap_or(ValueOrPlace::top())
|
2022-08-25 16:43:46 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn handle_constant(
|
|
|
|
|
&self,
|
|
|
|
|
constant: &Constant<'tcx>,
|
|
|
|
|
state: &mut State<Self::Value>,
|
|
|
|
|
) -> Self::Value {
|
|
|
|
|
self.super_constant(constant, state)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn super_constant(
|
|
|
|
|
&self,
|
|
|
|
|
_constant: &Constant<'tcx>,
|
|
|
|
|
_state: &mut State<Self::Value>,
|
|
|
|
|
) -> Self::Value {
|
|
|
|
|
Self::Value::top()
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-02 22:26:15 +00:00
|
|
|
|
/// The effect of a successful function call return should not be
|
|
|
|
|
/// applied here, see [`Analysis::apply_terminator_effect`].
|
2022-08-25 16:43:46 +00:00
|
|
|
|
fn handle_terminator(&self, terminator: &Terminator<'tcx>, state: &mut State<Self::Value>) {
|
|
|
|
|
self.super_terminator(terminator, state)
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-04 10:02:38 +00:00
|
|
|
|
fn super_terminator(&self, terminator: &Terminator<'tcx>, state: &mut State<Self::Value>) {
|
2022-09-01 22:37:38 +00:00
|
|
|
|
match &terminator.kind {
|
|
|
|
|
TerminatorKind::Call { .. } | TerminatorKind::InlineAsm { .. } => {
|
|
|
|
|
// Effect is applied by `handle_call_return`.
|
|
|
|
|
}
|
2022-09-04 10:02:38 +00:00
|
|
|
|
TerminatorKind::Drop { place, .. } => {
|
|
|
|
|
// Place can still be accessed after drop, and drop has mutable access to it.
|
|
|
|
|
state.flood(place.as_ref(), self.map());
|
|
|
|
|
}
|
2022-09-01 22:37:38 +00:00
|
|
|
|
TerminatorKind::DropAndReplace { .. } | TerminatorKind::Yield { .. } => {
|
|
|
|
|
// They would have an effect, but are not allowed in this phase.
|
|
|
|
|
bug!("encountered disallowed terminator");
|
|
|
|
|
}
|
2022-10-23 13:05:03 +00:00
|
|
|
|
TerminatorKind::Goto { .. }
|
|
|
|
|
| TerminatorKind::SwitchInt { .. }
|
|
|
|
|
| TerminatorKind::Resume
|
|
|
|
|
| TerminatorKind::Abort
|
|
|
|
|
| TerminatorKind::Return
|
|
|
|
|
| TerminatorKind::Unreachable
|
|
|
|
|
| TerminatorKind::Assert { .. }
|
|
|
|
|
| TerminatorKind::GeneratorDrop
|
|
|
|
|
| TerminatorKind::FalseEdge { .. }
|
|
|
|
|
| TerminatorKind::FalseUnwind { .. } => {
|
|
|
|
|
// These terminators have no effect on the analysis.
|
2022-09-01 22:37:38 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-08-25 16:43:46 +00:00
|
|
|
|
|
|
|
|
|
fn handle_call_return(
|
|
|
|
|
&self,
|
|
|
|
|
return_places: CallReturnPlaces<'_, 'tcx>,
|
|
|
|
|
state: &mut State<Self::Value>,
|
|
|
|
|
) {
|
|
|
|
|
self.super_call_return(return_places, state)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn super_call_return(
|
|
|
|
|
&self,
|
|
|
|
|
return_places: CallReturnPlaces<'_, 'tcx>,
|
|
|
|
|
state: &mut State<Self::Value>,
|
|
|
|
|
) {
|
|
|
|
|
return_places.for_each(|place| {
|
2022-09-01 12:17:15 +00:00
|
|
|
|
state.flood(place.as_ref(), self.map());
|
2022-08-25 16:43:46 +00:00
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn handle_switch_int(
|
|
|
|
|
&self,
|
|
|
|
|
discr: &Operand<'tcx>,
|
|
|
|
|
apply_edge_effects: &mut impl SwitchIntEdgeEffects<State<Self::Value>>,
|
|
|
|
|
) {
|
|
|
|
|
self.super_switch_int(discr, apply_edge_effects)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn super_switch_int(
|
|
|
|
|
&self,
|
|
|
|
|
_discr: &Operand<'tcx>,
|
|
|
|
|
_apply_edge_effects: &mut impl SwitchIntEdgeEffects<State<Self::Value>>,
|
|
|
|
|
) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn wrap(self) -> ValueAnalysisWrapper<Self>
|
|
|
|
|
where
|
|
|
|
|
Self: Sized,
|
|
|
|
|
{
|
|
|
|
|
ValueAnalysisWrapper(self)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub struct ValueAnalysisWrapper<T>(pub T);
|
|
|
|
|
|
|
|
|
|
impl<'tcx, T: ValueAnalysis<'tcx>> AnalysisDomain<'tcx> for ValueAnalysisWrapper<T> {
|
|
|
|
|
type Domain = State<T::Value>;
|
|
|
|
|
|
|
|
|
|
type Direction = crate::Forward;
|
|
|
|
|
|
|
|
|
|
const NAME: &'static str = T::NAME;
|
|
|
|
|
|
|
|
|
|
fn bottom_value(&self, _body: &Body<'tcx>) -> Self::Domain {
|
2022-09-02 12:41:27 +00:00
|
|
|
|
State(StateData::Unreachable)
|
2022-08-25 16:43:46 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn initialize_start_block(&self, body: &Body<'tcx>, state: &mut Self::Domain) {
|
2022-09-02 22:26:15 +00:00
|
|
|
|
// The initial state maps all tracked places of argument projections to ⊤ and the rest to ⊥.
|
2022-09-02 12:41:27 +00:00
|
|
|
|
assert!(matches!(state.0, StateData::Unreachable));
|
|
|
|
|
let values = IndexVec::from_elem_n(T::Value::bottom(), self.0.map().value_count);
|
|
|
|
|
*state = State(StateData::Reachable(values));
|
2022-08-25 16:43:46 +00:00
|
|
|
|
for arg in body.args_iter() {
|
2022-09-01 12:17:15 +00:00
|
|
|
|
state.flood(PlaceRef { local: arg, projection: &[] }, self.0.map());
|
2022-08-25 16:43:46 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<'tcx, T> Analysis<'tcx> for ValueAnalysisWrapper<T>
|
|
|
|
|
where
|
|
|
|
|
T: ValueAnalysis<'tcx>,
|
|
|
|
|
{
|
|
|
|
|
fn apply_statement_effect(
|
|
|
|
|
&self,
|
|
|
|
|
state: &mut Self::Domain,
|
|
|
|
|
statement: &Statement<'tcx>,
|
|
|
|
|
_location: Location,
|
|
|
|
|
) {
|
2022-09-02 12:41:27 +00:00
|
|
|
|
if state.is_reachable() {
|
|
|
|
|
self.0.handle_statement(statement, state);
|
|
|
|
|
}
|
2022-08-25 16:43:46 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn apply_terminator_effect(
|
|
|
|
|
&self,
|
|
|
|
|
state: &mut Self::Domain,
|
|
|
|
|
terminator: &Terminator<'tcx>,
|
|
|
|
|
_location: Location,
|
|
|
|
|
) {
|
2022-09-02 12:41:27 +00:00
|
|
|
|
if state.is_reachable() {
|
|
|
|
|
self.0.handle_terminator(terminator, state);
|
|
|
|
|
}
|
2022-08-25 16:43:46 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn apply_call_return_effect(
|
|
|
|
|
&self,
|
|
|
|
|
state: &mut Self::Domain,
|
|
|
|
|
_block: BasicBlock,
|
|
|
|
|
return_places: crate::CallReturnPlaces<'_, 'tcx>,
|
|
|
|
|
) {
|
2022-09-02 12:41:27 +00:00
|
|
|
|
if state.is_reachable() {
|
|
|
|
|
self.0.handle_call_return(return_places, state)
|
|
|
|
|
}
|
2022-08-25 16:43:46 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn apply_switch_int_edge_effects(
|
|
|
|
|
&self,
|
|
|
|
|
_block: BasicBlock,
|
|
|
|
|
discr: &Operand<'tcx>,
|
|
|
|
|
apply_edge_effects: &mut impl SwitchIntEdgeEffects<Self::Domain>,
|
|
|
|
|
) {
|
2022-09-02 12:41:27 +00:00
|
|
|
|
// FIXME: Dataflow framework provides no access to current state here.
|
2022-08-25 16:43:46 +00:00
|
|
|
|
self.0.handle_switch_int(discr, apply_edge_effects)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
rustc_index::newtype_index!(
|
2022-09-02 22:26:15 +00:00
|
|
|
|
/// This index uniquely identifies a place.
|
|
|
|
|
///
|
|
|
|
|
/// Not every place has a `PlaceIndex`, and not every `PlaceIndex` correspondends to a tracked
|
|
|
|
|
/// place. However, every tracked place and all places along its projection have a `PlaceIndex`.
|
2022-08-25 16:43:46 +00:00
|
|
|
|
pub struct PlaceIndex {}
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
rustc_index::newtype_index!(
|
2022-09-02 22:26:15 +00:00
|
|
|
|
/// This index uniquely identifies a tracked place and therefore a slot in [`State`].
|
|
|
|
|
///
|
|
|
|
|
/// It is an implementation detail of this module.
|
2022-08-25 16:43:46 +00:00
|
|
|
|
struct ValueIndex {}
|
|
|
|
|
);
|
|
|
|
|
|
2022-09-02 22:26:15 +00:00
|
|
|
|
/// See [`State`].
|
2022-08-25 16:43:46 +00:00
|
|
|
|
#[derive(PartialEq, Eq, Clone, Debug)]
|
2022-09-02 12:41:27 +00:00
|
|
|
|
enum StateData<V> {
|
|
|
|
|
Reachable(IndexVec<ValueIndex, V>),
|
|
|
|
|
Unreachable,
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-02 22:26:15 +00:00
|
|
|
|
/// The dataflow state for an instance of [`ValueAnalysis`].
|
|
|
|
|
///
|
|
|
|
|
/// Every instance specifies a lattice that represents the possible values of a single tracked
|
|
|
|
|
/// place. If we call this lattice `V` and set set of tracked places `P`, then a [`State`] is an
|
|
|
|
|
/// element of `{unreachable} ∪ (P -> V)`. This again forms a lattice, where the bottom element is
|
|
|
|
|
/// `unreachable` and the top element is the mapping `p ↦ ⊤`. Note that the mapping `p ↦ ⊥` is not
|
|
|
|
|
/// the bottom element (because joining an unreachable and any other reachable state yields a
|
|
|
|
|
/// reachable state). All operations on unreachable states are ignored.
|
|
|
|
|
///
|
|
|
|
|
/// Flooding means assigning a value (by default `⊤`) to all tracked projections of a given place.
|
2022-09-02 12:41:27 +00:00
|
|
|
|
#[derive(PartialEq, Eq, Clone, Debug)]
|
|
|
|
|
pub struct State<V>(StateData<V>);
|
2022-08-25 16:43:46 +00:00
|
|
|
|
|
2022-10-19 13:56:58 +00:00
|
|
|
|
impl<V: Clone + HasTop + HasBottom> State<V> {
|
2022-09-02 12:41:27 +00:00
|
|
|
|
pub fn is_reachable(&self) -> bool {
|
|
|
|
|
matches!(&self.0, StateData::Reachable(_))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn mark_unreachable(&mut self) {
|
|
|
|
|
self.0 = StateData::Unreachable;
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-01 12:17:15 +00:00
|
|
|
|
pub fn flood_all(&mut self) {
|
|
|
|
|
self.flood_all_with(V::top())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn flood_all_with(&mut self, value: V) {
|
2022-09-02 12:41:27 +00:00
|
|
|
|
let StateData::Reachable(values) = &mut self.0 else { return };
|
|
|
|
|
values.raw.fill(value);
|
2022-08-25 16:43:46 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-09-01 12:17:15 +00:00
|
|
|
|
pub fn flood_with(&mut self, place: PlaceRef<'_>, map: &Map, value: V) {
|
2022-08-25 16:43:46 +00:00
|
|
|
|
if let Some(root) = map.find(place) {
|
2022-09-01 12:17:15 +00:00
|
|
|
|
self.flood_idx_with(root, map, value);
|
2022-08-25 16:43:46 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-01 12:17:15 +00:00
|
|
|
|
pub fn flood(&mut self, place: PlaceRef<'_>, map: &Map) {
|
|
|
|
|
self.flood_with(place, map, V::top())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn flood_idx_with(&mut self, place: PlaceIndex, map: &Map, value: V) {
|
2022-09-02 12:41:27 +00:00
|
|
|
|
let StateData::Reachable(values) = &mut self.0 else { return };
|
2022-08-25 16:43:46 +00:00
|
|
|
|
map.preorder_invoke(place, &mut |place| {
|
|
|
|
|
if let Some(vi) = map.places[place].value_index {
|
2022-09-02 12:41:27 +00:00
|
|
|
|
values[vi] = value.clone();
|
2022-08-25 16:43:46 +00:00
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-01 12:17:15 +00:00
|
|
|
|
pub fn flood_idx(&mut self, place: PlaceIndex, map: &Map) {
|
|
|
|
|
self.flood_idx_with(place, map, V::top())
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-19 13:56:58 +00:00
|
|
|
|
/// Copies `source` to `target`, including all tracked places beneath.
|
|
|
|
|
///
|
|
|
|
|
/// If `target` contains a place that is not contained in `source`, it will be overwritten with
|
2022-10-21 15:02:03 +00:00
|
|
|
|
/// Top. Also, because this will copy all entries one after another, it may only be used for
|
|
|
|
|
/// places that are non-overlapping or identical.
|
2022-08-25 16:43:46 +00:00
|
|
|
|
pub fn assign_place_idx(&mut self, target: PlaceIndex, source: PlaceIndex, map: &Map) {
|
2022-09-02 12:41:27 +00:00
|
|
|
|
let StateData::Reachable(values) = &mut self.0 else { return };
|
2022-10-05 19:30:43 +00:00
|
|
|
|
|
2022-09-02 22:26:15 +00:00
|
|
|
|
// If both places are tracked, we copy the value to the target. If the target is tracked,
|
|
|
|
|
// but the source is not, we have to invalidate the value in target. If the target is not
|
|
|
|
|
// tracked, then we don't have to do anything.
|
2022-08-25 16:43:46 +00:00
|
|
|
|
if let Some(target_value) = map.places[target].value_index {
|
|
|
|
|
if let Some(source_value) = map.places[source].value_index {
|
2022-09-02 12:41:27 +00:00
|
|
|
|
values[target_value] = values[source_value].clone();
|
2022-08-25 16:43:46 +00:00
|
|
|
|
} else {
|
2022-09-02 12:41:27 +00:00
|
|
|
|
values[target_value] = V::top();
|
2022-08-25 16:43:46 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
for target_child in map.children(target) {
|
2022-09-02 22:26:15 +00:00
|
|
|
|
// Try to find corresponding child and recurse. Reasoning is similar as above.
|
2022-08-25 16:43:46 +00:00
|
|
|
|
let projection = map.places[target_child].proj_elem.unwrap();
|
|
|
|
|
if let Some(source_child) = map.projections.get(&(source, projection)) {
|
|
|
|
|
self.assign_place_idx(target_child, *source_child, map);
|
|
|
|
|
} else {
|
2022-09-01 12:17:15 +00:00
|
|
|
|
self.flood_idx(target_child, map);
|
2022-08-25 16:43:46 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-01 12:17:15 +00:00
|
|
|
|
pub fn assign(&mut self, target: PlaceRef<'_>, result: ValueOrPlaceOrRef<V>, map: &Map) {
|
2022-08-25 16:43:46 +00:00
|
|
|
|
if let Some(target) = map.find(target) {
|
|
|
|
|
self.assign_idx(target, result, map);
|
2022-09-01 12:17:15 +00:00
|
|
|
|
} else {
|
|
|
|
|
// We don't track this place nor any projections, assignment can be ignored.
|
2022-08-25 16:43:46 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-01 12:17:15 +00:00
|
|
|
|
pub fn assign_idx(&mut self, target: PlaceIndex, result: ValueOrPlaceOrRef<V>, map: &Map) {
|
2022-08-25 16:43:46 +00:00
|
|
|
|
match result {
|
2022-09-01 12:17:15 +00:00
|
|
|
|
ValueOrPlaceOrRef::Value(value) => {
|
2022-08-31 14:01:37 +00:00
|
|
|
|
// First flood the target place in case we also track any projections (although
|
2022-09-01 12:17:15 +00:00
|
|
|
|
// this scenario is currently not well-supported by the API).
|
|
|
|
|
self.flood_idx(target, map);
|
2022-09-02 12:41:27 +00:00
|
|
|
|
let StateData::Reachable(values) = &mut self.0 else { return };
|
2022-08-25 16:43:46 +00:00
|
|
|
|
if let Some(value_index) = map.places[target].value_index {
|
2022-09-02 12:41:27 +00:00
|
|
|
|
values[value_index] = value;
|
2022-08-25 16:43:46 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2022-09-01 12:17:15 +00:00
|
|
|
|
ValueOrPlaceOrRef::Place(source) => self.assign_place_idx(target, source, map),
|
|
|
|
|
ValueOrPlaceOrRef::Ref(source) => {
|
2022-09-02 12:41:27 +00:00
|
|
|
|
let StateData::Reachable(values) = &mut self.0 else { return };
|
2022-09-01 12:17:15 +00:00
|
|
|
|
if let Some(value_index) = map.places[target].value_index {
|
2022-09-02 12:41:27 +00:00
|
|
|
|
values[value_index] = V::top();
|
2022-09-01 12:17:15 +00:00
|
|
|
|
}
|
2022-10-21 15:02:03 +00:00
|
|
|
|
// Instead of tracking of *where* a reference points to (as in, which memory
|
|
|
|
|
// location), we track *what* it points to (as in, what do we know about the
|
|
|
|
|
// target). For an assignment `x = &y`, we thus copy the info of `y` to `*x`.
|
2022-10-19 13:56:58 +00:00
|
|
|
|
if let Some(target_deref) = map.apply(target, TrackElem::Deref) {
|
2022-10-21 15:02:03 +00:00
|
|
|
|
// We know here that `*x` is `Freeze`, because we only track through
|
|
|
|
|
// dereferences if the target type is `Freeze`.
|
2022-09-01 12:17:15 +00:00
|
|
|
|
self.assign_place_idx(target_deref, source, map);
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-08-25 16:43:46 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-21 15:02:03 +00:00
|
|
|
|
/// Retrieve the value stored for a place, or ⊥ if it is not tracked.
|
2022-08-25 16:43:46 +00:00
|
|
|
|
pub fn get(&self, place: PlaceRef<'_>, map: &Map) -> V {
|
|
|
|
|
map.find(place).map(|place| self.get_idx(place, map)).unwrap_or(V::top())
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-21 15:02:03 +00:00
|
|
|
|
/// Retrieve the value stored for a place index, or ⊥ if it is not tracked.
|
2022-08-25 16:43:46 +00:00
|
|
|
|
pub fn get_idx(&self, place: PlaceIndex, map: &Map) -> V {
|
2022-09-02 12:41:27 +00:00
|
|
|
|
match &self.0 {
|
|
|
|
|
StateData::Reachable(values) => {
|
|
|
|
|
map.places[place].value_index.map(|v| values[v].clone()).unwrap_or(V::top())
|
|
|
|
|
}
|
2022-10-19 13:56:58 +00:00
|
|
|
|
StateData::Unreachable => {
|
|
|
|
|
// Because this is unreachable, we can return any value we want.
|
|
|
|
|
V::bottom()
|
|
|
|
|
}
|
2022-09-02 12:41:27 +00:00
|
|
|
|
}
|
2022-08-25 16:43:46 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-02 12:41:27 +00:00
|
|
|
|
impl<V: JoinSemiLattice + Clone> JoinSemiLattice for State<V> {
|
2022-08-25 16:43:46 +00:00
|
|
|
|
fn join(&mut self, other: &Self) -> bool {
|
2022-09-02 12:41:27 +00:00
|
|
|
|
match (&mut self.0, &other.0) {
|
|
|
|
|
(_, StateData::Unreachable) => false,
|
|
|
|
|
(StateData::Unreachable, _) => {
|
|
|
|
|
*self = other.clone();
|
|
|
|
|
true
|
|
|
|
|
}
|
|
|
|
|
(StateData::Reachable(this), StateData::Reachable(other)) => this.join(other),
|
|
|
|
|
}
|
2022-08-25 16:43:46 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-21 15:02:03 +00:00
|
|
|
|
/// A partial mapping from `Place` to `PlaceIndex`, where some place indices have value indices.
|
2022-10-19 13:56:58 +00:00
|
|
|
|
///
|
2022-10-21 15:02:03 +00:00
|
|
|
|
/// Some additional bookkeeping is done to speed up traversal:
|
2022-10-19 13:56:58 +00:00
|
|
|
|
/// - For iteration, every [`PlaceInfo`] contains an intrusive linked list of its children.
|
2022-10-21 15:02:03 +00:00
|
|
|
|
/// - To directly get the child for a specific projection, there is a `projections` map.
|
2022-08-25 16:43:46 +00:00
|
|
|
|
#[derive(Debug)]
|
|
|
|
|
pub struct Map {
|
|
|
|
|
locals: IndexVec<Local, Option<PlaceIndex>>,
|
2022-10-19 13:56:58 +00:00
|
|
|
|
projections: FxHashMap<(PlaceIndex, TrackElem), PlaceIndex>,
|
2022-08-25 16:43:46 +00:00
|
|
|
|
places: IndexVec<PlaceIndex, PlaceInfo>,
|
|
|
|
|
value_count: usize,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Map {
|
2022-10-06 22:09:36 +00:00
|
|
|
|
fn new() -> Self {
|
2022-08-25 16:43:46 +00:00
|
|
|
|
Self {
|
|
|
|
|
locals: IndexVec::new(),
|
|
|
|
|
projections: FxHashMap::default(),
|
|
|
|
|
places: IndexVec::new(),
|
|
|
|
|
value_count: 0,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-19 13:56:58 +00:00
|
|
|
|
/// Returns a map that only tracks places whose type passes the filter.
|
|
|
|
|
///
|
|
|
|
|
/// This is currently the only way to create a [`Map`]. The way in which the tracked places are
|
2022-10-21 15:02:03 +00:00
|
|
|
|
/// chosen is an implementation detail and may not be relied upon.
|
2022-10-06 22:09:36 +00:00
|
|
|
|
pub fn from_filter<'tcx>(
|
|
|
|
|
tcx: TyCtxt<'tcx>,
|
|
|
|
|
body: &Body<'tcx>,
|
|
|
|
|
filter: impl FnMut(Ty<'tcx>) -> bool,
|
|
|
|
|
) -> Self {
|
|
|
|
|
let mut map = Self::new();
|
2022-10-06 23:10:10 +00:00
|
|
|
|
|
|
|
|
|
// If `-Zunsound-mir-opts` is given, tracking through references, and tracking of places
|
|
|
|
|
// that have their reference taken is allowed. This would be "unsound" in the sense that
|
|
|
|
|
// the correctness relies on an aliasing model similar to Stacked Borrows (which is
|
|
|
|
|
// not yet guaranteed).
|
|
|
|
|
if tcx.sess.opts.unstable_opts.unsound_mir_opts {
|
2022-10-19 13:56:58 +00:00
|
|
|
|
// We might want to add additional limitations. If a struct has 10 boxed fields of
|
2022-10-21 15:02:03 +00:00
|
|
|
|
// itself, there will currently be `10.pow(max_derefs)` tracked places.
|
2022-10-19 13:56:58 +00:00
|
|
|
|
map.register_with_filter(tcx, body, 2, filter, &[]);
|
2022-10-06 23:10:10 +00:00
|
|
|
|
} else {
|
|
|
|
|
map.register_with_filter(tcx, body, 0, filter, &escaped_places(body));
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-06 22:09:36 +00:00
|
|
|
|
map
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-19 13:56:58 +00:00
|
|
|
|
/// Register all non-excluded places that pass the filter, up to a certain dereference depth.
|
2022-10-06 22:09:36 +00:00
|
|
|
|
fn register_with_filter<'tcx>(
|
2022-08-25 16:43:46 +00:00
|
|
|
|
&mut self,
|
|
|
|
|
tcx: TyCtxt<'tcx>,
|
2022-10-06 22:09:36 +00:00
|
|
|
|
body: &Body<'tcx>,
|
2022-08-25 16:43:46 +00:00
|
|
|
|
max_derefs: u32,
|
|
|
|
|
mut filter: impl FnMut(Ty<'tcx>) -> bool,
|
2022-10-06 23:10:10 +00:00
|
|
|
|
exclude: &[Place<'tcx>],
|
2022-08-25 16:43:46 +00:00
|
|
|
|
) {
|
2022-10-19 13:56:58 +00:00
|
|
|
|
// This is used to tell whether a type is `!Freeze`.
|
2022-10-06 22:09:36 +00:00
|
|
|
|
let param_env = tcx.param_env_reveal_all_normalized(body.source.def_id());
|
2022-10-19 13:56:58 +00:00
|
|
|
|
|
2022-08-25 16:43:46 +00:00
|
|
|
|
let mut projection = Vec::new();
|
2022-10-06 22:09:36 +00:00
|
|
|
|
for (local, decl) in body.local_decls.iter_enumerated() {
|
2022-08-25 16:43:46 +00:00
|
|
|
|
self.register_with_filter_rec(
|
|
|
|
|
tcx,
|
|
|
|
|
max_derefs,
|
|
|
|
|
local,
|
|
|
|
|
&mut projection,
|
|
|
|
|
decl.ty,
|
|
|
|
|
&mut filter,
|
2022-10-06 22:09:36 +00:00
|
|
|
|
param_env,
|
2022-10-06 23:10:10 +00:00
|
|
|
|
exclude,
|
2022-08-25 16:43:46 +00:00
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn register_with_filter_rec<'tcx>(
|
|
|
|
|
&mut self,
|
|
|
|
|
tcx: TyCtxt<'tcx>,
|
|
|
|
|
max_derefs: u32,
|
|
|
|
|
local: Local,
|
|
|
|
|
projection: &mut Vec<PlaceElem<'tcx>>,
|
|
|
|
|
ty: Ty<'tcx>,
|
|
|
|
|
filter: &mut impl FnMut(Ty<'tcx>) -> bool,
|
2022-10-06 22:09:36 +00:00
|
|
|
|
param_env: ty::ParamEnv<'tcx>,
|
2022-10-06 23:10:10 +00:00
|
|
|
|
exclude: &[Place<'tcx>],
|
2022-08-25 16:43:46 +00:00
|
|
|
|
) {
|
2022-10-19 13:56:58 +00:00
|
|
|
|
// This currently does a linear scan, could be improved.
|
2022-10-06 23:10:10 +00:00
|
|
|
|
if exclude.contains(&Place { local, projection: tcx.intern_place_elems(projection) }) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-25 16:43:46 +00:00
|
|
|
|
if filter(ty) {
|
2022-09-27 14:16:09 +00:00
|
|
|
|
// This might fail if `ty` is not scalar.
|
|
|
|
|
let _ = self.register_with_ty(local, projection, ty);
|
2022-08-25 16:43:46 +00:00
|
|
|
|
}
|
2022-10-06 23:10:10 +00:00
|
|
|
|
|
2022-08-25 16:43:46 +00:00
|
|
|
|
if max_derefs > 0 {
|
2022-10-06 22:09:36 +00:00
|
|
|
|
if let Some(ty::TypeAndMut { ty: deref_ty, .. }) = ty.builtin_deref(false) {
|
2022-10-21 15:02:03 +00:00
|
|
|
|
// Values behind references can only be tracked if the target is `Freeze`.
|
2022-10-06 22:09:36 +00:00
|
|
|
|
if deref_ty.is_freeze(tcx.at(DUMMY_SP), param_env) {
|
|
|
|
|
projection.push(PlaceElem::Deref);
|
|
|
|
|
self.register_with_filter_rec(
|
|
|
|
|
tcx,
|
|
|
|
|
max_derefs - 1,
|
|
|
|
|
local,
|
|
|
|
|
projection,
|
|
|
|
|
deref_ty,
|
|
|
|
|
filter,
|
|
|
|
|
param_env,
|
2022-10-06 23:10:10 +00:00
|
|
|
|
exclude,
|
2022-10-06 22:09:36 +00:00
|
|
|
|
);
|
|
|
|
|
projection.pop();
|
|
|
|
|
}
|
2022-08-25 16:43:46 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
iter_fields(ty, tcx, |variant, field, ty| {
|
2022-09-27 14:16:09 +00:00
|
|
|
|
if variant.is_some() {
|
|
|
|
|
// Downcasts are currently not supported.
|
|
|
|
|
return;
|
2022-08-25 16:43:46 +00:00
|
|
|
|
}
|
|
|
|
|
projection.push(PlaceElem::Field(field, ty));
|
2022-10-06 22:09:36 +00:00
|
|
|
|
self.register_with_filter_rec(
|
2022-10-06 23:10:10 +00:00
|
|
|
|
tcx, max_derefs, local, projection, ty, filter, param_env, exclude,
|
2022-10-06 22:09:36 +00:00
|
|
|
|
);
|
2022-08-25 16:43:46 +00:00
|
|
|
|
projection.pop();
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-19 13:56:58 +00:00
|
|
|
|
/// Tries to add the place to the map, without allocating a value slot.
|
|
|
|
|
///
|
|
|
|
|
/// Can fail if the projection contains non-trackable elements.
|
2022-09-27 14:16:09 +00:00
|
|
|
|
fn make_place<'tcx>(
|
2022-08-25 16:43:46 +00:00
|
|
|
|
&mut self,
|
|
|
|
|
local: Local,
|
|
|
|
|
projection: &[PlaceElem<'tcx>],
|
2022-09-27 14:16:09 +00:00
|
|
|
|
) -> Result<PlaceIndex, ()> {
|
2022-08-25 16:43:46 +00:00
|
|
|
|
// Get the base index of the local.
|
|
|
|
|
let mut index =
|
|
|
|
|
*self.locals.get_or_insert_with(local, || self.places.push(PlaceInfo::new(None)));
|
|
|
|
|
|
|
|
|
|
// Apply the projection.
|
|
|
|
|
for &elem in projection {
|
|
|
|
|
let elem = elem.try_into()?;
|
|
|
|
|
index = *self.projections.entry((index, elem)).or_insert_with(|| {
|
|
|
|
|
// Prepend new child to the linked list.
|
|
|
|
|
let next = self.places.push(PlaceInfo::new(Some(elem)));
|
|
|
|
|
self.places[next].next_sibling = self.places[index].first_child;
|
|
|
|
|
self.places[index].first_child = Some(next);
|
|
|
|
|
next
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-27 14:16:09 +00:00
|
|
|
|
Ok(index)
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-06 22:09:36 +00:00
|
|
|
|
#[allow(unused)]
|
|
|
|
|
fn register<'tcx>(
|
2022-09-27 14:16:09 +00:00
|
|
|
|
&mut self,
|
|
|
|
|
local: Local,
|
|
|
|
|
projection: &[PlaceElem<'tcx>],
|
|
|
|
|
decls: &impl HasLocalDecls<'tcx>,
|
|
|
|
|
tcx: TyCtxt<'tcx>,
|
|
|
|
|
) -> Result<(), ()> {
|
|
|
|
|
projection
|
|
|
|
|
.iter()
|
|
|
|
|
.fold(PlaceTy::from_ty(decls.local_decls()[local].ty), |place_ty, &elem| {
|
|
|
|
|
place_ty.projection_ty(tcx, elem)
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
let place_ty = Place::ty_from(local, projection, decls, tcx);
|
|
|
|
|
if place_ty.variant_index.is_some() {
|
|
|
|
|
return Err(());
|
|
|
|
|
}
|
|
|
|
|
self.register_with_ty(local, projection, place_ty.ty)
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-19 13:56:58 +00:00
|
|
|
|
/// Tries to track the given place. Fails if type is non-scalar or projection is not trackable.
|
2022-09-27 14:16:09 +00:00
|
|
|
|
fn register_with_ty<'tcx>(
|
|
|
|
|
&mut self,
|
|
|
|
|
local: Local,
|
|
|
|
|
projection: &[PlaceElem<'tcx>],
|
|
|
|
|
ty: Ty<'tcx>,
|
|
|
|
|
) -> Result<(), ()> {
|
|
|
|
|
if !ty.is_scalar() {
|
|
|
|
|
// Currently, only scalar types are allowed, because they are atomic
|
|
|
|
|
// and therefore do not require invalidation of parent places.
|
|
|
|
|
return Err(());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let place = self.make_place(local, projection)?;
|
|
|
|
|
|
2022-08-25 16:43:46 +00:00
|
|
|
|
// Allocate a value slot if it doesn't have one.
|
2022-09-27 14:16:09 +00:00
|
|
|
|
if self.places[place].value_index.is_none() {
|
|
|
|
|
self.places[place].value_index = Some(self.value_count.into());
|
2022-08-25 16:43:46 +00:00
|
|
|
|
self.value_count += 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-19 13:56:58 +00:00
|
|
|
|
pub fn apply(&self, place: PlaceIndex, elem: TrackElem) -> Option<PlaceIndex> {
|
2022-08-25 16:43:46 +00:00
|
|
|
|
self.projections.get(&(place, elem)).copied()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn find(&self, place: PlaceRef<'_>) -> Option<PlaceIndex> {
|
|
|
|
|
let mut index = *self.locals.get(place.local)?.as_ref()?;
|
|
|
|
|
|
|
|
|
|
for &elem in place.projection {
|
2022-10-19 13:56:58 +00:00
|
|
|
|
index = self.apply(index, elem.try_into().ok()?)?;
|
2022-08-25 16:43:46 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Some(index)
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-19 13:56:58 +00:00
|
|
|
|
/// Iterate over all direct children.
|
2022-08-25 16:43:46 +00:00
|
|
|
|
pub fn children(&self, parent: PlaceIndex) -> impl Iterator<Item = PlaceIndex> + '_ {
|
|
|
|
|
Children::new(self, parent)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn preorder_invoke(&self, root: PlaceIndex, f: &mut impl FnMut(PlaceIndex)) {
|
|
|
|
|
f(root);
|
|
|
|
|
for child in self.children(root) {
|
|
|
|
|
self.preorder_invoke(child, f);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-19 13:56:58 +00:00
|
|
|
|
/// This is the information tracked for every [`PlaceIndex`] and is stored by [`Map`].
|
|
|
|
|
///
|
|
|
|
|
/// Together, `first_child` and `next_sibling` form an intrusive linked list, which is used to
|
|
|
|
|
/// model a tree structure (a replacement for a member like `children: Vec<PlaceIndex>`).
|
2022-08-25 16:43:46 +00:00
|
|
|
|
#[derive(Debug)]
|
|
|
|
|
struct PlaceInfo {
|
2022-10-19 13:56:58 +00:00
|
|
|
|
/// We store a [`ValueIndex`] if and only if the placed is tracked by the analysis.
|
2022-08-25 16:43:46 +00:00
|
|
|
|
value_index: Option<ValueIndex>,
|
2022-10-19 13:56:58 +00:00
|
|
|
|
|
|
|
|
|
/// The projection used to go from parent to this node (only None for root).
|
|
|
|
|
proj_elem: Option<TrackElem>,
|
|
|
|
|
|
|
|
|
|
/// The left-most child.
|
|
|
|
|
first_child: Option<PlaceIndex>,
|
|
|
|
|
|
|
|
|
|
/// Index of the sibling to the right of this node.
|
|
|
|
|
next_sibling: Option<PlaceIndex>,
|
2022-08-25 16:43:46 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl PlaceInfo {
|
2022-10-19 13:56:58 +00:00
|
|
|
|
fn new(proj_elem: Option<TrackElem>) -> Self {
|
2022-08-25 16:43:46 +00:00
|
|
|
|
Self { next_sibling: None, first_child: None, proj_elem, value_index: None }
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct Children<'a> {
|
|
|
|
|
map: &'a Map,
|
|
|
|
|
next: Option<PlaceIndex>,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<'a> Children<'a> {
|
|
|
|
|
fn new(map: &'a Map, parent: PlaceIndex) -> Self {
|
|
|
|
|
Self { map, next: map.places[parent].first_child }
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<'a> Iterator for Children<'a> {
|
|
|
|
|
type Item = PlaceIndex;
|
|
|
|
|
|
|
|
|
|
fn next(&mut self) -> Option<Self::Item> {
|
|
|
|
|
match self.next {
|
|
|
|
|
Some(child) => {
|
|
|
|
|
self.next = self.map.places[child].next_sibling;
|
|
|
|
|
Some(child)
|
|
|
|
|
}
|
|
|
|
|
None => None,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-10-19 13:56:58 +00:00
|
|
|
|
|
|
|
|
|
/// Used as the result of an operand.
|
2022-08-25 16:43:46 +00:00
|
|
|
|
pub enum ValueOrPlace<V> {
|
|
|
|
|
Value(V),
|
|
|
|
|
Place(PlaceIndex),
|
2022-10-05 20:01:33 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-10-19 13:56:58 +00:00
|
|
|
|
impl<V: HasTop> HasTop for ValueOrPlace<V> {
|
|
|
|
|
fn top() -> Self {
|
2022-10-05 20:01:33 +00:00
|
|
|
|
ValueOrPlace::Value(V::top())
|
|
|
|
|
}
|
2022-08-25 16:43:46 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-10-19 13:56:58 +00:00
|
|
|
|
/// Used as the result of an r-value.
|
2022-09-01 12:17:15 +00:00
|
|
|
|
pub enum ValueOrPlaceOrRef<V> {
|
|
|
|
|
Value(V),
|
|
|
|
|
Place(PlaceIndex),
|
2022-10-05 20:23:57 +00:00
|
|
|
|
Ref(PlaceIndex),
|
2022-09-01 12:17:15 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-10-19 13:56:58 +00:00
|
|
|
|
impl<V: HasTop> HasTop for ValueOrPlaceOrRef<V> {
|
|
|
|
|
fn top() -> Self {
|
2022-10-05 20:01:33 +00:00
|
|
|
|
ValueOrPlaceOrRef::Value(V::top())
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-01 12:17:15 +00:00
|
|
|
|
impl<V> From<ValueOrPlace<V>> for ValueOrPlaceOrRef<V> {
|
|
|
|
|
fn from(x: ValueOrPlace<V>) -> Self {
|
|
|
|
|
match x {
|
|
|
|
|
ValueOrPlace::Value(value) => ValueOrPlaceOrRef::Value(value),
|
|
|
|
|
ValueOrPlace::Place(place) => ValueOrPlaceOrRef::Place(place),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-25 16:43:46 +00:00
|
|
|
|
pub trait HasBottom {
|
|
|
|
|
fn bottom() -> Self;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub trait HasTop {
|
|
|
|
|
fn top() -> Self;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<V> HasBottom for FlatSet<V> {
|
|
|
|
|
fn bottom() -> Self {
|
|
|
|
|
Self::Bottom
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<V> HasTop for FlatSet<V> {
|
|
|
|
|
fn top() -> Self {
|
|
|
|
|
Self::Top
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-19 13:56:58 +00:00
|
|
|
|
/// The set of projection elements that can be used by a tracked place.
|
2022-09-27 14:16:09 +00:00
|
|
|
|
///
|
|
|
|
|
/// For now, downcast is not allowed due to aliasing between variants (see #101168).
|
2022-08-25 16:43:46 +00:00
|
|
|
|
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
|
2022-10-19 13:56:58 +00:00
|
|
|
|
pub enum TrackElem {
|
2022-08-25 16:43:46 +00:00
|
|
|
|
Deref,
|
|
|
|
|
Field(Field),
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-19 13:56:58 +00:00
|
|
|
|
impl<V, T> TryFrom<ProjectionElem<V, T>> for TrackElem {
|
2022-08-25 16:43:46 +00:00
|
|
|
|
type Error = ();
|
|
|
|
|
|
|
|
|
|
fn try_from(value: ProjectionElem<V, T>) -> Result<Self, Self::Error> {
|
|
|
|
|
match value {
|
2022-10-19 13:56:58 +00:00
|
|
|
|
ProjectionElem::Deref => Ok(TrackElem::Deref),
|
|
|
|
|
ProjectionElem::Field(field, _) => Ok(TrackElem::Field(field)),
|
2022-08-25 16:43:46 +00:00
|
|
|
|
_ => Err(()),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-19 13:56:58 +00:00
|
|
|
|
/// Invokes `f` on all direct fields of `ty`.
|
2022-08-25 16:43:46 +00:00
|
|
|
|
fn iter_fields<'tcx>(
|
|
|
|
|
ty: Ty<'tcx>,
|
|
|
|
|
tcx: TyCtxt<'tcx>,
|
|
|
|
|
mut f: impl FnMut(Option<VariantIdx>, Field, Ty<'tcx>),
|
|
|
|
|
) {
|
|
|
|
|
match ty.kind() {
|
|
|
|
|
ty::Tuple(list) => {
|
|
|
|
|
for (field, ty) in list.iter().enumerate() {
|
|
|
|
|
f(None, field.into(), ty);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
ty::Adt(def, substs) => {
|
|
|
|
|
for (v_index, v_def) in def.variants().iter_enumerated() {
|
|
|
|
|
for (f_index, f_def) in v_def.fields.iter().enumerate() {
|
2022-08-29 20:29:46 +00:00
|
|
|
|
let field_ty = f_def.ty(tcx, substs);
|
|
|
|
|
let field_ty = tcx
|
|
|
|
|
.try_normalize_erasing_regions(ty::ParamEnv::reveal_all(), field_ty)
|
|
|
|
|
.unwrap_or(field_ty);
|
2022-08-25 16:43:46 +00:00
|
|
|
|
f(Some(v_index), f_index.into(), field_ty);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
ty::Closure(_, substs) => {
|
|
|
|
|
iter_fields(substs.as_closure().tupled_upvars_ty(), tcx, f);
|
|
|
|
|
}
|
|
|
|
|
_ => (),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-19 13:56:58 +00:00
|
|
|
|
/// Returns all places, that have their reference or address taken.
|
2022-10-21 15:02:03 +00:00
|
|
|
|
///
|
|
|
|
|
/// This includes shared references.
|
2022-10-19 13:56:58 +00:00
|
|
|
|
fn escaped_places<'tcx>(body: &Body<'tcx>) -> Vec<Place<'tcx>> {
|
|
|
|
|
struct Collector<'tcx> {
|
|
|
|
|
result: Vec<Place<'tcx>>,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<'tcx> Visitor<'tcx> for Collector<'tcx> {
|
|
|
|
|
fn visit_place(&mut self, place: &Place<'tcx>, context: PlaceContext, _location: Location) {
|
|
|
|
|
if context.is_borrow() || context.is_address_of() {
|
|
|
|
|
self.result.push(*place);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let mut collector = Collector { result: Vec::new() };
|
|
|
|
|
collector.visit_body(body);
|
|
|
|
|
collector.result
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// This is used to visualize the dataflow analysis.
|
|
|
|
|
impl<'tcx, T> DebugWithContext<ValueAnalysisWrapper<T>> for State<T::Value>
|
|
|
|
|
where
|
|
|
|
|
T: ValueAnalysis<'tcx>,
|
|
|
|
|
T::Value: Debug,
|
|
|
|
|
{
|
|
|
|
|
fn fmt_with(&self, ctxt: &ValueAnalysisWrapper<T>, f: &mut Formatter<'_>) -> std::fmt::Result {
|
|
|
|
|
match &self.0 {
|
|
|
|
|
StateData::Reachable(values) => debug_with_context(values, None, ctxt.0.map(), f),
|
|
|
|
|
StateData::Unreachable => write!(f, "unreachable"),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn fmt_diff_with(
|
|
|
|
|
&self,
|
|
|
|
|
old: &Self,
|
|
|
|
|
ctxt: &ValueAnalysisWrapper<T>,
|
|
|
|
|
f: &mut Formatter<'_>,
|
|
|
|
|
) -> std::fmt::Result {
|
|
|
|
|
match (&self.0, &old.0) {
|
|
|
|
|
(StateData::Reachable(this), StateData::Reachable(old)) => {
|
|
|
|
|
debug_with_context(this, Some(old), ctxt.0.map(), f)
|
|
|
|
|
}
|
|
|
|
|
_ => Ok(()), // Consider printing something here.
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-25 16:43:46 +00:00
|
|
|
|
fn debug_with_context_rec<V: Debug + Eq>(
|
|
|
|
|
place: PlaceIndex,
|
|
|
|
|
place_str: &str,
|
2022-09-02 12:41:27 +00:00
|
|
|
|
new: &IndexVec<ValueIndex, V>,
|
|
|
|
|
old: Option<&IndexVec<ValueIndex, V>>,
|
2022-08-25 16:43:46 +00:00
|
|
|
|
map: &Map,
|
|
|
|
|
f: &mut Formatter<'_>,
|
|
|
|
|
) -> std::fmt::Result {
|
|
|
|
|
if let Some(value) = map.places[place].value_index {
|
|
|
|
|
match old {
|
2022-09-02 12:41:27 +00:00
|
|
|
|
None => writeln!(f, "{}: {:?}", place_str, new[value])?,
|
2022-08-25 16:43:46 +00:00
|
|
|
|
Some(old) => {
|
2022-09-02 12:41:27 +00:00
|
|
|
|
if new[value] != old[value] {
|
|
|
|
|
writeln!(f, "\u{001f}-{}: {:?}", place_str, old[value])?;
|
|
|
|
|
writeln!(f, "\u{001f}+{}: {:?}", place_str, new[value])?;
|
2022-08-25 16:43:46 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for child in map.children(place) {
|
|
|
|
|
let info_elem = map.places[child].proj_elem.unwrap();
|
|
|
|
|
let child_place_str = match info_elem {
|
2022-10-19 13:56:58 +00:00
|
|
|
|
TrackElem::Deref => format!("*{}", place_str),
|
|
|
|
|
TrackElem::Field(field) => {
|
2022-08-25 16:43:46 +00:00
|
|
|
|
if place_str.starts_with("*") {
|
|
|
|
|
format!("({}).{}", place_str, field.index())
|
|
|
|
|
} else {
|
|
|
|
|
format!("{}.{}", place_str, field.index())
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
debug_with_context_rec(child, &child_place_str, new, old, map, f)?;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn debug_with_context<V: Debug + Eq>(
|
2022-09-02 12:41:27 +00:00
|
|
|
|
new: &IndexVec<ValueIndex, V>,
|
|
|
|
|
old: Option<&IndexVec<ValueIndex, V>>,
|
2022-08-25 16:43:46 +00:00
|
|
|
|
map: &Map,
|
|
|
|
|
f: &mut Formatter<'_>,
|
|
|
|
|
) -> std::fmt::Result {
|
|
|
|
|
for (local, place) in map.locals.iter_enumerated() {
|
|
|
|
|
if let Some(place) = place {
|
|
|
|
|
debug_with_context_rec(*place, &format!("{:?}", local), new, old, map, f)?;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|