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-11-09 17:03:30 +00:00
|
|
|
|
//! This framework is currently experimental. Originally, it supported shared references and enum
|
|
|
|
|
//! variants. However, it was discovered that both of these were unsound, and especially references
|
|
|
|
|
//! had subtle but serious issues. In the future, they could be added back in, but we should clarify
|
|
|
|
|
//! the rules for optimizations that rely on the aliasing model first.
|
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-11-09 17:03:30 +00:00
|
|
|
|
//! - Currently, places that have their reference taken cannot be tracked. Although this would be
|
|
|
|
|
//! possible, it has to rely on some aliasing model, which we are not ready to commit to yet.
|
|
|
|
|
//! Because of that, we can assume that the only way to change the value behind a tracked place is
|
|
|
|
|
//! by direct assignment.
|
2022-08-25 16:43:46 +00:00
|
|
|
|
|
|
|
|
|
use std::fmt::{Debug, Formatter};
|
|
|
|
|
|
2022-11-12 13:57:14 +00:00
|
|
|
|
use rustc_data_structures::fx::FxHashMap;
|
2023-01-21 21:53:26 +00:00
|
|
|
|
use rustc_index::bit_set::BitSet;
|
2022-08-25 16:43:46 +00:00
|
|
|
|
use rustc_index::vec::IndexVec;
|
2022-11-09 17:03:30 +00:00
|
|
|
|
use rustc_middle::mir::visit::{MutatingUseContext, PlaceContext, Visitor};
|
2022-08-25 16:43:46 +00:00
|
|
|
|
use rustc_middle::mir::*;
|
|
|
|
|
use rustc_middle::ty::{self, Ty, TyCtxt};
|
|
|
|
|
use rustc_target::abi::VariantIdx;
|
|
|
|
|
|
2022-10-24 22:56:51 +00:00
|
|
|
|
use crate::lattice::{HasBottom, HasTop};
|
2022-08-25 16:43:46 +00:00
|
|
|
|
use crate::{
|
2022-10-24 22:56:51 +00:00
|
|
|
|
fmt::DebugWithContext, Analysis, AnalysisDomain, CallReturnPlaces, JoinSemiLattice,
|
|
|
|
|
SwitchIntEdgeEffects,
|
2022-08-25 16:43:46 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
}
|
2023-01-21 22:28:54 +00:00
|
|
|
|
StatementKind::SetDiscriminant { box ref place, .. } => {
|
|
|
|
|
state.flood_discr(place.as_ref(), self.map());
|
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(..) => {
|
2022-11-09 17:03:30 +00:00
|
|
|
|
// We don't track references.
|
2022-10-12 21:46:31 +00:00
|
|
|
|
}
|
2022-12-20 00:51:17 +00:00
|
|
|
|
StatementKind::ConstEvalCounter
|
|
|
|
|
| StatementKind::Nop
|
2022-08-25 16:43:46 +00:00
|
|
|
|
| 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-11-09 17:03:30 +00:00
|
|
|
|
) -> ValueOrPlace<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-11-09 17:03:30 +00:00
|
|
|
|
) -> ValueOrPlace<Self::Value> {
|
2022-08-25 16:43:46 +00:00
|
|
|
|
match rvalue {
|
2022-11-09 17:03:30 +00:00
|
|
|
|
Rvalue::Use(operand) => self.handle_operand(operand, state),
|
2022-11-09 17:21:42 +00:00
|
|
|
|
Rvalue::CopyForDeref(place) => self.handle_operand(&Operand::Copy(*place), state),
|
2022-11-09 17:03:30 +00:00
|
|
|
|
Rvalue::Ref(..) | Rvalue::AddressOf(..) => {
|
|
|
|
|
// We don't track such places.
|
|
|
|
|
ValueOrPlace::top()
|
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.
|
2022-11-09 17:03:30 +00:00
|
|
|
|
ValueOrPlace::top()
|
2022-10-19 13:56:58 +00:00
|
|
|
|
}
|
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-11-09 17:03:30 +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-11-09 17:03:30 +00:00
|
|
|
|
TerminatorKind::Drop { .. } => {
|
|
|
|
|
// We don't track dropped places.
|
2022-09-04 10:02:38 +00:00
|
|
|
|
}
|
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-10-25 19:54:39 +00:00
|
|
|
|
#[derive(PartialEq, Eq, Debug)]
|
2022-09-02 12:41:27 +00:00
|
|
|
|
enum StateData<V> {
|
|
|
|
|
Reachable(IndexVec<ValueIndex, V>),
|
|
|
|
|
Unreachable,
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-25 19:54:39 +00:00
|
|
|
|
impl<V: Clone> Clone for StateData<V> {
|
|
|
|
|
fn clone(&self) -> Self {
|
|
|
|
|
match self {
|
|
|
|
|
Self::Reachable(x) => Self::Reachable(x.clone()),
|
|
|
|
|
Self::Unreachable => Self::Unreachable,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn clone_from(&mut self, source: &Self) {
|
|
|
|
|
match (&mut *self, source) {
|
|
|
|
|
(Self::Reachable(x), Self::Reachable(y)) => {
|
|
|
|
|
// We go through `raw` here, because `IndexVec` currently has a naive `clone_from`.
|
|
|
|
|
x.raw.clone_from(&y.raw);
|
|
|
|
|
}
|
|
|
|
|
_ => *self = source.clone(),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
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
|
2022-12-05 08:42:36 +00:00
|
|
|
|
/// place. If we call this lattice `V` and set of tracked places `P`, then a [`State`] is an
|
2022-09-02 22:26:15 +00:00
|
|
|
|
/// 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-10-25 19:54:39 +00:00
|
|
|
|
#[derive(PartialEq, Eq, Debug)]
|
2022-09-02 12:41:27 +00:00
|
|
|
|
pub struct State<V>(StateData<V>);
|
2022-08-25 16:43:46 +00:00
|
|
|
|
|
2022-10-25 19:54:39 +00:00
|
|
|
|
impl<V: Clone> Clone for State<V> {
|
|
|
|
|
fn clone(&self) -> Self {
|
|
|
|
|
Self(self.0.clone())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn clone_from(&mut self, source: &Self) {
|
|
|
|
|
self.0.clone_from(&source.0);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
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) {
|
2023-01-21 22:28:54 +00:00
|
|
|
|
let StateData::Reachable(values) = &mut self.0 else { return };
|
|
|
|
|
map.for_each_aliasing_place(place, None, &mut |place| {
|
|
|
|
|
if let Some(vi) = map.places[place].value_index {
|
|
|
|
|
values[vi] = value.clone();
|
|
|
|
|
}
|
|
|
|
|
});
|
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())
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-21 22:28:54 +00:00
|
|
|
|
pub fn flood_discr_with(&mut self, place: PlaceRef<'_>, map: &Map, value: V) {
|
2022-09-02 12:41:27 +00:00
|
|
|
|
let StateData::Reachable(values) = &mut self.0 else { return };
|
2023-01-21 22:28:54 +00:00
|
|
|
|
map.for_each_aliasing_place(place, Some(TrackElem::Discriminant), &mut |place| {
|
2022-08-25 16:43:46 +00:00
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-21 22:28:54 +00:00
|
|
|
|
pub fn flood_discr(&mut self, place: PlaceRef<'_>, map: &Map) {
|
|
|
|
|
self.flood_discr_with(place, map, V::top())
|
2022-09-01 12:17:15 +00:00
|
|
|
|
}
|
|
|
|
|
|
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.
|
2023-01-21 22:28:54 +00:00
|
|
|
|
///
|
|
|
|
|
/// The target place must have been flooded before calling this method.
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-09 17:03:30 +00:00
|
|
|
|
pub fn assign(&mut self, target: PlaceRef<'_>, result: ValueOrPlace<V>, map: &Map) {
|
2023-01-21 22:28:54 +00:00
|
|
|
|
self.flood(target, map);
|
2022-08-25 16:43:46 +00:00
|
|
|
|
if let Some(target) = map.find(target) {
|
|
|
|
|
self.assign_idx(target, result, map);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-21 22:28:54 +00:00
|
|
|
|
pub fn assign_discr(&mut self, target: PlaceRef<'_>, result: ValueOrPlace<V>, map: &Map) {
|
|
|
|
|
self.flood_discr(target, map);
|
|
|
|
|
if let Some(target) = map.find_discr(target) {
|
|
|
|
|
self.assign_idx(target, result, map);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// The target place must have been flooded before calling this method.
|
2022-11-09 17:03:30 +00:00
|
|
|
|
pub fn assign_idx(&mut self, target: PlaceIndex, result: ValueOrPlace<V>, map: &Map) {
|
2022-08-25 16:43:46 +00:00
|
|
|
|
match result {
|
2022-11-09 17:03:30 +00:00
|
|
|
|
ValueOrPlace::Value(value) => {
|
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-11-09 17:03:30 +00:00
|
|
|
|
ValueOrPlace::Place(source) => self.assign_place_idx(target, source, map),
|
2022-08-25 16:43:46 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-07 09:33:28 +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())
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-21 22:28:54 +00:00
|
|
|
|
/// Retrieve the value stored for a place, or ⊤ if it is not tracked.
|
|
|
|
|
pub fn get_discr(&self, place: PlaceRef<'_>, map: &Map) -> V {
|
|
|
|
|
match map.find_discr(place) {
|
|
|
|
|
Some(place) => self.get_idx(place, map),
|
|
|
|
|
None => V::top(),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-07 09:33:28 +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-11-10 18:12:10 +00:00
|
|
|
|
/// Partial mapping from [`Place`] to [`PlaceIndex`], where some places also have a [`ValueIndex`].
|
2022-10-19 13:56:58 +00:00
|
|
|
|
///
|
2022-11-10 18:12:10 +00:00
|
|
|
|
/// This data structure essentially maintains a tree of places and their projections. Some
|
|
|
|
|
/// additional bookkeeping is done, to speed up traversal over this tree:
|
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-11-10 18:12:10 +00:00
|
|
|
|
/// chosen is an implementation detail and may not be relied upon (other than that their type
|
|
|
|
|
/// passes the filter).
|
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,
|
2023-01-28 11:23:18 +00:00
|
|
|
|
place_limit: Option<usize>,
|
2022-10-06 22:09:36 +00:00
|
|
|
|
) -> Self {
|
|
|
|
|
let mut map = Self::new();
|
2022-11-12 13:57:14 +00:00
|
|
|
|
let exclude = excluded_locals(body);
|
2023-01-28 11:23:18 +00:00
|
|
|
|
map.register_with_filter(tcx, body, filter, exclude, place_limit);
|
2022-10-25 19:54:39 +00:00
|
|
|
|
debug!("registered {} places ({} nodes in total)", map.value_count, map.places.len());
|
2022-10-06 22:09:36 +00:00
|
|
|
|
map
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-09 17:03:30 +00:00
|
|
|
|
/// Register all non-excluded places that pass the filter.
|
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
|
|
|
|
mut filter: impl FnMut(Ty<'tcx>) -> bool,
|
2023-01-21 21:53:26 +00:00
|
|
|
|
exclude: BitSet<Local>,
|
2023-01-28 11:23:18 +00:00
|
|
|
|
place_limit: Option<usize>,
|
2022-08-25 16:43:46 +00:00
|
|
|
|
) {
|
2022-11-10 18:12:10 +00:00
|
|
|
|
// We use this vector as stack, pushing and popping projections.
|
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() {
|
2023-01-21 21:53:26 +00:00
|
|
|
|
if !exclude.contains(local) {
|
2023-01-28 11:23:18 +00:00
|
|
|
|
self.register_with_filter_rec(
|
|
|
|
|
tcx,
|
|
|
|
|
local,
|
|
|
|
|
&mut projection,
|
|
|
|
|
decl.ty,
|
|
|
|
|
&mut filter,
|
|
|
|
|
place_limit,
|
|
|
|
|
);
|
2022-11-12 13:57:14 +00:00
|
|
|
|
}
|
2022-08-25 16:43:46 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-11 10:24:31 +00:00
|
|
|
|
/// Potentially register the (local, projection) place and its fields, recursively.
|
2022-11-10 18:12:10 +00:00
|
|
|
|
///
|
2023-01-21 22:28:54 +00:00
|
|
|
|
/// Invariant: The projection must only contain trackable elements.
|
2022-08-25 16:43:46 +00:00
|
|
|
|
fn register_with_filter_rec<'tcx>(
|
|
|
|
|
&mut self,
|
|
|
|
|
tcx: TyCtxt<'tcx>,
|
|
|
|
|
local: Local,
|
|
|
|
|
projection: &mut Vec<PlaceElem<'tcx>>,
|
|
|
|
|
ty: Ty<'tcx>,
|
|
|
|
|
filter: &mut impl FnMut(Ty<'tcx>) -> bool,
|
2023-01-28 11:23:18 +00:00
|
|
|
|
place_limit: Option<usize>,
|
2022-08-25 16:43:46 +00:00
|
|
|
|
) {
|
2023-01-28 11:23:18 +00:00
|
|
|
|
if let Some(place_limit) = place_limit && self.value_count >= place_limit {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-21 22:28:54 +00:00
|
|
|
|
// We know that the projection only contains trackable elements.
|
|
|
|
|
let place = self.make_place(local, projection).unwrap();
|
2022-11-10 18:12:10 +00:00
|
|
|
|
|
2023-01-21 22:28:54 +00:00
|
|
|
|
// Allocate a value slot if it doesn't have one, and the user requested one.
|
|
|
|
|
if self.places[place].value_index.is_none() && filter(ty) {
|
|
|
|
|
self.places[place].value_index = Some(self.value_count.into());
|
|
|
|
|
self.value_count += 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ty.is_enum() {
|
|
|
|
|
let discr_ty = ty.discriminant_ty(tcx);
|
|
|
|
|
if filter(discr_ty) {
|
|
|
|
|
let discr = *self
|
|
|
|
|
.projections
|
|
|
|
|
.entry((place, TrackElem::Discriminant))
|
|
|
|
|
.or_insert_with(|| {
|
|
|
|
|
// Prepend new child to the linked list.
|
|
|
|
|
let next = self.places.push(PlaceInfo::new(Some(TrackElem::Discriminant)));
|
|
|
|
|
self.places[next].next_sibling = self.places[place].first_child;
|
|
|
|
|
self.places[place].first_child = Some(next);
|
|
|
|
|
next
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Allocate a value slot if it doesn't have one.
|
|
|
|
|
if self.places[discr].value_index.is_none() {
|
|
|
|
|
self.places[discr].value_index = Some(self.value_count.into());
|
|
|
|
|
self.value_count += 1;
|
|
|
|
|
}
|
2022-11-10 18:12:10 +00:00
|
|
|
|
}
|
2022-08-25 16:43:46 +00:00
|
|
|
|
}
|
2022-11-10 18:12:10 +00:00
|
|
|
|
|
|
|
|
|
// Recurse with all fields of this place.
|
2022-08-25 16:43:46 +00:00
|
|
|
|
iter_fields(ty, tcx, |variant, field, ty| {
|
2023-01-21 22:28:54 +00:00
|
|
|
|
if let Some(variant) = variant {
|
|
|
|
|
projection.push(PlaceElem::Downcast(None, variant));
|
|
|
|
|
let _ = self.make_place(local, projection);
|
|
|
|
|
projection.push(PlaceElem::Field(field, ty));
|
2023-01-28 11:23:18 +00:00
|
|
|
|
self.register_with_filter_rec(tcx, local, projection, ty, filter, place_limit);
|
2023-01-21 22:28:54 +00:00
|
|
|
|
projection.pop();
|
|
|
|
|
projection.pop();
|
2022-09-27 14:16:09 +00:00
|
|
|
|
return;
|
2022-08-25 16:43:46 +00:00
|
|
|
|
}
|
|
|
|
|
projection.push(PlaceElem::Field(field, ty));
|
2023-01-28 11:23:18 +00:00
|
|
|
|
self.register_with_filter_rec(tcx, local, projection, ty, filter, place_limit);
|
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-11-10 18:12:10 +00:00
|
|
|
|
/// Returns the number of tracked places, i.e., those for which a value can be stored.
|
2022-10-25 19:54:39 +00:00
|
|
|
|
pub fn tracked_places(&self) -> usize {
|
|
|
|
|
self.value_count
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-10 18:12:10 +00:00
|
|
|
|
/// Applies a single projection element, yielding the corresponding child.
|
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()
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-10 18:12:10 +00:00
|
|
|
|
/// Locates the given place, if it exists in the tree.
|
2023-01-29 14:20:45 +00:00
|
|
|
|
pub fn find_extra(
|
|
|
|
|
&self,
|
|
|
|
|
place: PlaceRef<'_>,
|
|
|
|
|
extra: impl IntoIterator<Item = TrackElem>,
|
|
|
|
|
) -> Option<PlaceIndex> {
|
2022-08-25 16:43:46 +00:00
|
|
|
|
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
|
|
|
|
}
|
2023-01-29 14:20:45 +00:00
|
|
|
|
for elem in extra {
|
|
|
|
|
index = self.apply(index, elem)?;
|
|
|
|
|
}
|
2022-08-25 16:43:46 +00:00
|
|
|
|
|
|
|
|
|
Some(index)
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-21 22:28:54 +00:00
|
|
|
|
/// Locates the given place, if it exists in the tree.
|
2023-01-29 14:20:45 +00:00
|
|
|
|
pub fn find(&self, place: PlaceRef<'_>) -> Option<PlaceIndex> {
|
|
|
|
|
self.find_extra(place, [])
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Locates the given place and applies `Discriminant`, if it exists in the tree.
|
2023-01-21 22:28:54 +00:00
|
|
|
|
pub fn find_discr(&self, place: PlaceRef<'_>) -> Option<PlaceIndex> {
|
2023-01-29 14:20:45 +00:00
|
|
|
|
self.find_extra(place, [TrackElem::Discriminant])
|
2023-01-21 22:28:54 +00:00
|
|
|
|
}
|
|
|
|
|
|
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)
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-21 22:28:54 +00:00
|
|
|
|
/// Invoke a function on the given place and all places that may alias it.
|
|
|
|
|
///
|
|
|
|
|
/// In particular, when the given place has a variant downcast, we invoke the function on all
|
|
|
|
|
/// the other variants.
|
|
|
|
|
///
|
|
|
|
|
/// `tail_elem` allows to support discriminants that are not a place in MIR, but that we track
|
|
|
|
|
/// as such.
|
2023-01-29 14:20:45 +00:00
|
|
|
|
pub fn for_each_aliasing_place(
|
2023-01-21 22:28:54 +00:00
|
|
|
|
&self,
|
|
|
|
|
place: PlaceRef<'_>,
|
|
|
|
|
tail_elem: Option<TrackElem>,
|
|
|
|
|
f: &mut impl FnMut(PlaceIndex),
|
|
|
|
|
) {
|
|
|
|
|
let Some(&Some(mut index)) = self.locals.get(place.local) else {
|
2023-01-29 14:20:45 +00:00
|
|
|
|
// The local is not tracked at all, so it does not alias anything.
|
2023-01-21 22:28:54 +00:00
|
|
|
|
return;
|
|
|
|
|
};
|
|
|
|
|
let elems = place
|
|
|
|
|
.projection
|
|
|
|
|
.iter()
|
|
|
|
|
.map(|&elem| elem.try_into())
|
|
|
|
|
.chain(tail_elem.map(Ok).into_iter());
|
|
|
|
|
for elem in elems {
|
|
|
|
|
let Ok(elem) = elem else { return };
|
|
|
|
|
let sub = self.apply(index, elem);
|
|
|
|
|
if let TrackElem::Variant(..) | TrackElem::Discriminant = elem {
|
2023-01-29 14:20:45 +00:00
|
|
|
|
// Enum variant fields and enum discriminants alias each another.
|
2023-01-21 22:28:54 +00:00
|
|
|
|
self.for_each_variant_sibling(index, sub, f);
|
|
|
|
|
}
|
|
|
|
|
if let Some(sub) = sub {
|
|
|
|
|
index = sub
|
|
|
|
|
} else {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
self.preorder_invoke(index, f);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Invoke the given function on all the descendants of the given place, except one branch.
|
2023-01-29 14:20:45 +00:00
|
|
|
|
fn for_each_variant_sibling(
|
2023-01-21 22:28:54 +00:00
|
|
|
|
&self,
|
|
|
|
|
parent: PlaceIndex,
|
|
|
|
|
preserved_child: Option<PlaceIndex>,
|
|
|
|
|
f: &mut impl FnMut(PlaceIndex),
|
|
|
|
|
) {
|
|
|
|
|
for sibling in self.children(parent) {
|
|
|
|
|
let elem = self.places[sibling].proj_elem;
|
|
|
|
|
// Only invalidate variants and discriminant. Fields (for generators) are not
|
|
|
|
|
// invalidated by assignment to a variant.
|
|
|
|
|
if let Some(TrackElem::Variant(..) | TrackElem::Discriminant) = elem
|
|
|
|
|
// Only invalidate the other variants, the current one is fine.
|
|
|
|
|
&& Some(sibling) != preserved_child
|
|
|
|
|
{
|
|
|
|
|
self.preorder_invoke(sibling, f);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-10 18:12:10 +00:00
|
|
|
|
/// Invoke a function on the given place and all descendants.
|
2023-01-21 22:28:54 +00:00
|
|
|
|
fn preorder_invoke(&self, root: PlaceIndex, f: &mut impl FnMut(PlaceIndex)) {
|
2022-08-25 16:43:46 +00:00
|
|
|
|
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
|
|
|
|
|
2022-11-09 17:03:30 +00:00
|
|
|
|
/// Used as the result of an operand or r-value.
|
2023-01-21 22:28:54 +00:00
|
|
|
|
#[derive(Debug)]
|
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-24 22:56:51 +00:00
|
|
|
|
impl<V: HasTop> ValueOrPlace<V> {
|
|
|
|
|
pub 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
|
|
|
|
/// The set of projection elements that can be used by a tracked place.
|
2022-09-27 14:16:09 +00:00
|
|
|
|
///
|
2022-11-09 17:03:30 +00:00
|
|
|
|
/// Although only field projections are currently allowed, this could change in the future.
|
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
|
|
|
|
Field(Field),
|
2023-01-21 22:28:54 +00:00
|
|
|
|
Variant(VariantIdx),
|
|
|
|
|
Discriminant,
|
2022-08-25 16:43:46 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-12-19 15:30:43 +00:00
|
|
|
|
impl<V, T> TryFrom<ProjectionElem<V, T>> for TrackElem {
|
2022-08-25 16:43:46 +00:00
|
|
|
|
type Error = ();
|
|
|
|
|
|
2022-12-19 15:30:43 +00:00
|
|
|
|
fn try_from(value: ProjectionElem<V, T>) -> Result<Self, Self::Error> {
|
2022-08-25 16:43:46 +00:00
|
|
|
|
match value {
|
2022-10-19 13:56:58 +00:00
|
|
|
|
ProjectionElem::Field(field, _) => Ok(TrackElem::Field(field)),
|
2023-01-21 22:28:54 +00:00
|
|
|
|
ProjectionElem::Downcast(_, idx) => Ok(TrackElem::Variant(idx)),
|
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`.
|
2023-02-05 11:37:44 +00:00
|
|
|
|
pub fn iter_fields<'tcx>(
|
2022-08-25 16:43:46 +00:00
|
|
|
|
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) => {
|
2022-11-09 17:21:42 +00:00
|
|
|
|
if def.is_union() {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2022-08-25 16:43:46 +00:00
|
|
|
|
for (v_index, v_def) in def.variants().iter_enumerated() {
|
2022-11-09 17:21:42 +00:00
|
|
|
|
let variant = if def.is_struct() { None } else { Some(v_index) };
|
2022-08-25 16:43:46 +00:00
|
|
|
|
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-11-09 17:21:42 +00:00
|
|
|
|
f(variant, f_index.into(), field_ty);
|
2022-08-25 16:43:46 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
ty::Closure(_, substs) => {
|
|
|
|
|
iter_fields(substs.as_closure().tupled_upvars_ty(), tcx, f);
|
|
|
|
|
}
|
|
|
|
|
_ => (),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-12 13:57:14 +00:00
|
|
|
|
/// Returns all locals with projections that have their reference or address taken.
|
2023-01-21 21:53:26 +00:00
|
|
|
|
pub fn excluded_locals(body: &Body<'_>) -> BitSet<Local> {
|
2022-11-12 13:57:14 +00:00
|
|
|
|
struct Collector {
|
2023-01-21 21:53:26 +00:00
|
|
|
|
result: BitSet<Local>,
|
2022-10-19 13:56:58 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-11-12 13:57:14 +00:00
|
|
|
|
impl<'tcx> Visitor<'tcx> for Collector {
|
2022-10-19 13:56:58 +00:00
|
|
|
|
fn visit_place(&mut self, place: &Place<'tcx>, context: PlaceContext, _location: Location) {
|
2023-01-21 21:53:26 +00:00
|
|
|
|
if (context.is_borrow()
|
2022-11-09 17:03:30 +00:00
|
|
|
|
|| context.is_address_of()
|
|
|
|
|
|| context.is_drop()
|
2023-01-21 21:53:26 +00:00
|
|
|
|
|| context == PlaceContext::MutatingUse(MutatingUseContext::AsmOutput))
|
|
|
|
|
&& !place.is_indirect()
|
2022-11-09 17:03:30 +00:00
|
|
|
|
{
|
2022-11-12 13:57:14 +00:00
|
|
|
|
// A pointer to a place could be used to access other places with the same local,
|
|
|
|
|
// hence we have to exclude the local completely.
|
2023-01-21 21:53:26 +00:00
|
|
|
|
self.result.insert(place.local);
|
2022-10-19 13:56:58 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-21 21:53:26 +00:00
|
|
|
|
let mut collector = Collector { result: BitSet::new_empty(body.local_decls.len()) };
|
2022-10-19 13:56:58 +00:00
|
|
|
|
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 {
|
2023-01-21 22:28:54 +00:00
|
|
|
|
TrackElem::Discriminant => {
|
|
|
|
|
format!("discriminant({})", place_str)
|
|
|
|
|
}
|
|
|
|
|
TrackElem::Variant(idx) => {
|
|
|
|
|
format!("({} as {:?})", place_str, idx)
|
|
|
|
|
}
|
2022-10-19 13:56:58 +00:00
|
|
|
|
TrackElem::Field(field) => {
|
2022-11-18 09:30:47 +00:00
|
|
|
|
if place_str.starts_with('*') {
|
2022-08-25 16:43:46 +00:00
|
|
|
|
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 {
|
2022-12-19 09:31:55 +00:00
|
|
|
|
debug_with_context_rec(*place, &format!("{local:?}"), new, old, map, f)?;
|
2022-08-25 16:43:46 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|