1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
//! The representation of the PDG.
use std::fmt;
use internment::Intern;
use serde::{Deserialize, Serialize};
use crate::rustc_portable::*;
#[cfg(feature = "rustc")]
use crate::rustc_proxies;
/// Extends a MIR body's `Location` with `Start` (before the first instruction) and `End` (after all returns).
#[derive(PartialEq, Eq, Hash, Clone, Copy, Debug, Serialize, Deserialize)]
pub enum RichLocation {
/// The point *after* a location in a body.
#[cfg_attr(feature = "rustc", serde(with = "rustc_proxies::Location"))]
Location(Location),
/// The start of the body.
///
/// Note that [`Location::START`] is different from [`RichLocation::Start`]!
/// The latter is *before* the former in time.
Start,
/// The end of the body, after all possible return statements.
End,
}
impl RichLocation {
/// Returns true if this is a `Start` location.
pub fn is_start(self) -> bool {
matches!(self, RichLocation::Start)
}
/// Returns true if this is an `End` location.
pub fn is_end(self) -> bool {
matches!(self, RichLocation::End)
}
pub fn is_real(self) -> bool {
matches!(self, RichLocation::Location(_))
}
/// Returns the [`Location`] in `self`, panicking otherwise.
pub fn unwrap_location(self) -> Location {
self.as_location()
.expect("RichLocation was unexpectedly Start")
}
/// Returns the [`Location`] in `self`, returning `None` otherwise.
pub fn as_location(self) -> Option<Location> {
match self {
RichLocation::Location(location) => Some(location),
RichLocation::Start | RichLocation::End => None,
}
}
}
impl fmt::Display for RichLocation {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
RichLocation::Location(loc) => write!(f, "{loc:?}"),
RichLocation::Start => write!(f, "start"),
RichLocation::End => write!(f, "end"),
}
}
}
impl From<Location> for RichLocation {
fn from(value: Location) -> Self {
RichLocation::Location(value)
}
}
/// A [`RichLocation`] within a specific point in a codebase.
#[derive(PartialEq, Eq, Hash, Clone, Copy, Debug, Serialize, Deserialize)]
pub struct GlobalLocation {
/// The function containing the location.
#[cfg_attr(feature = "rustc", serde(with = "rustc_proxies::DefId"))]
pub function: DefId,
/// The location of an instruction in the function, or the function's start.
pub location: RichLocation,
}
#[cfg(not(feature = "rustc"))]
impl fmt::Display for GlobalLocation {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{:?}::{}", self.function, self.location)
}
}
/// A location within the global call-graph.
///
/// The first location is the root of the call-graph.
/// The last location is the currently-called function.
///
/// Invariant: a call string should never be empty, i.e.,
/// there should always be at least one [`GlobalLocation`] in a call-string.
///
/// Note: This type is copyable due to interning.
#[derive(PartialEq, Eq, Hash, Copy, Clone, Debug, Serialize, Deserialize)]
pub struct CallString(Intern<CallStringInner>);
type CallStringInner = Box<[GlobalLocation]>;
impl CallString {
/// Create a new call string from a list of global locations.
fn new(locs: CallStringInner) -> Self {
CallString(Intern::new(locs))
}
/// Split the leaf (the current instruction) from the caller for the
/// function (if any) and return both. Same as `(self.leaf(), self.caller())`.
pub fn pop(self) -> (GlobalLocation, Option<CallString>) {
let (last, rest) = self
.0
.split_last()
.expect("Invariant broken, call strings must have at least length 1");
(
*last,
(!rest.is_empty()).then(|| CallString::new(rest.into())),
)
}
/// Create an initial call string for the single location `loc`.
pub fn single(loc: GlobalLocation) -> Self {
Self::new(Box::new([loc]))
}
/// Returns the leaf of the call string (the currently-called function).
pub fn leaf(self) -> GlobalLocation {
*self.0.last().unwrap()
}
/// Returns the call string minus the leaf. Returns `None` if this location
/// is at the root.
pub fn caller(self) -> Option<Self> {
self.pop().1
}
/// Returns an iterator over the locations in the call string, starting at
/// the leaf and going to the root.
pub fn iter(&self) -> impl DoubleEndedIterator<Item = GlobalLocation> + '_ {
self.0.iter().rev().copied()
}
/// Adds a new call site to the end of the call string.
pub fn push(self, loc: GlobalLocation) -> Self {
let string = self.0.iter().copied().chain(Some(loc)).collect();
CallString::new(string)
}
pub fn push_front(self, loc: GlobalLocation) -> Self {
CallString::new([loc].into_iter().chain(self.0.iter().copied()).collect())
}
pub fn is_at_root(self) -> bool {
self.0.len() == 1
}
pub fn root(self) -> GlobalLocation {
*self.0.first().unwrap()
}
pub fn stable_id(self) -> usize {
let r: &'static CallStringInner = self.0.as_ref();
r as *const CallStringInner as usize
}
/// Returns an iterator over the locations in the call string, starting at
/// the root and going to the leaf.
pub fn iter_from_root(&self) -> impl DoubleEndedIterator<Item = GlobalLocation> + '_ {
self.0.iter().copied()
}
pub fn len(self) -> usize {
self.0.len()
}
pub fn is_empty(self) -> bool {
self.0.is_empty()
}
}
impl fmt::Display for CallString {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
for (i, loc) in self.0.iter().enumerate() {
if i > 0 {
write!(f, "←")?;
}
loc.fmt(f)?;
}
Ok(())
}
}
/// Additional information about the source of data.
///
/// If the operation is a function call this contains the argument index
#[derive(
PartialEq, Eq, PartialOrd, Ord, Hash, Clone, Copy, Debug, Serialize, Deserialize, strum::EnumIs,
)]
pub enum SourceUse {
Operand,
Argument(u8),
}
/// Additional information about this mutation.
#[derive(PartialEq, Eq, Hash, Clone, Copy, Debug, Serialize, Deserialize, strum::EnumIs)]
pub enum TargetUse {
/// A function returned, assigning to it's return destination
Return,
/// This mutation is a non-function assign
Assign,
/// A mutable argument was modified by a function call
MutArg(u8),
}