flowistry_pdg_construction/
graph.rs

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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
//! The representation of the PDG.

use std::{
    fmt::{self},
    hash::Hash,
    path::Path,
};

use flowistry_pdg::{CallString, GlobalLocation, RichLocation};
use internment::Intern;
use petgraph::{dot, graph::DiGraph};

use rustc_hash::FxHashSet;
use rustc_hir::def_id::DefId;
use rustc_index::IndexVec;
use rustc_middle::{
    mir::{Body, HasLocalDecls, Local, LocalDecl, LocalDecls, Place},
    ty::{GenericArgsRef, TyCtxt},
};
use rustc_span::Span;
use rustc_utils::PlaceExt;

pub use flowistry_pdg::{SourceUse, TargetUse};

use super::utils::Captures;

/// A node in the program dependency graph.
///
/// Represents a place at a particular call-string.
/// The place is in the body of the root of the call-string.
#[derive(Clone, Copy, Debug)]
pub struct DepNode<'tcx> {
    /// A place in memory in a particular body.
    pub place: Place<'tcx>,

    /// The point in the execution of the program.
    pub at: CallString,

    /// Pretty representation of the place.
    /// This is cached as an interned string on [`DepNode`] because to compute it later,
    /// we would have to regenerate the entire monomorphized body for a given place.
    pub(crate) place_pretty: Option<Intern<String>>,
    /// Does the PDG track subplaces of this place?
    pub is_split: bool,
    pub span: Span,
}

impl PartialEq for DepNode<'_> {
    fn eq(&self, other: &Self) -> bool {
        // Using an explicit match here with all fields, so that should new
        // fields be added we remember to check whether they need to be included
        // here.
        let Self {
            place,
            at,
            place_pretty: _,
            span,
            is_split,
        } = *self;
        let eq = (place, at).eq(&(other.place, other.at));
        if eq {
            debug_assert_eq!(span, other.span);
            debug_assert_eq!(is_split, other.is_split);
        }
        eq
    }
}

impl Eq for DepNode<'_> {}

impl Hash for DepNode<'_> {
    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
        // Using an explicit match here with all fields, so that should new
        // fields be added we remember to check whether they need to be included
        // here.
        let Self {
            place,
            at,
            place_pretty: _,
            span: _,
            is_split: _,
        } = self;
        (place, at).hash(state)
    }
}

impl<'tcx> DepNode<'tcx> {
    /// Constructs a new [`DepNode`].
    ///
    /// The `tcx` and `body` arguments are used to precompute a pretty string
    /// representation of the [`DepNode`].
    pub fn new(
        place: Place<'tcx>,
        at: CallString,
        tcx: TyCtxt<'tcx>,
        body: &Body<'tcx>,
        is_split: bool,
    ) -> Self {
        let i = at.leaf();
        let span = match i.location {
            RichLocation::Location(loc) => {
                let expanded_span = body
                    .stmt_at(loc)
                    .either(|s| s.source_info.span, |t| t.source_info.span);
                tcx.sess.source_map().stmt_span(expanded_span, body.span)
            }
            RichLocation::Start | RichLocation::End => tcx.def_span(i.function),
        };
        DepNode {
            place,
            at,
            place_pretty: place.to_string(tcx, body).map(Intern::new),
            span,
            is_split,
        }
    }
}

impl DepNode<'_> {
    /// Returns a pretty string representation of the place, if one exists.
    pub fn place_pretty(&self) -> Option<&str> {
        self.place_pretty.map(|s| s.as_ref().as_str())
    }
}

impl fmt::Display for DepNode<'_> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self.place_pretty() {
            Some(s) => s.fmt(f)?,
            None => write!(f, "{:?}", self.place)?,
        };
        write!(f, " @ {}", self.at)
    }
}

/// A kind of edge in the program dependence graph.
#[derive(PartialEq, Eq, Hash, Clone, Copy, Debug)]
pub enum DepEdgeKind {
    /// X is control-dependent on Y if the value of Y influences the execution
    /// of statements that affect the value of X.
    Control,

    /// X is data-dependent on Y if the value of Y is an input to statements that affect
    /// the value of X.
    Data,
}

/// An edge in the program dependence graph.
///
/// Represents an operation that induces a dependency between places.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct DepEdge {
    /// Either data or control.
    pub kind: DepEdgeKind,

    /// The location of the operation.
    pub at: CallString,

    pub source_use: SourceUse,

    pub target_use: TargetUse,
}

impl DepEdge {
    /// Constructs a data edge.
    pub fn data(at: CallString, source_use: SourceUse, target_use: TargetUse) -> Self {
        DepEdge {
            kind: DepEdgeKind::Data,
            at,
            source_use,
            target_use,
        }
    }

    /// Constructs a control edge.
    pub fn control(at: CallString, source_use: SourceUse, target_use: TargetUse) -> Self {
        DepEdge {
            kind: DepEdgeKind::Control,
            at,
            source_use,
            target_use,
        }
    }
}

impl fmt::Display for DepEdge {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{:?}\n@ {}", self.kind, self.at)
    }
}

/// The top-level PDG.
#[derive(Debug)]
pub struct DepGraph<'tcx> {
    /// The petgraph representation of the PDG.
    pub graph: DiGraph<DepNode<'tcx>, DepEdge>,
}

impl Clone for DepGraph<'_> {
    fn clone(&self) -> Self {
        DepGraph {
            graph: self.graph.clone(),
        }
    }

    fn clone_from(&mut self, source: &Self) {
        self.graph.clone_from(&source.graph);
    }
}

impl<'tcx> DepGraph<'tcx> {
    /// Constructs a new [`DepGraph`].
    pub fn new(graph: DiGraph<DepNode<'tcx>, DepEdge>) -> Self {
        Self { graph }
    }
}

impl DepGraph<'_> {
    /// Generates a graphviz visualization of the PDG and saves it to `path`.
    pub fn generate_graphviz(&self, path: impl AsRef<Path>) -> anyhow::Result<()> {
        let graph_dot = format!(
            "{}",
            dot::Dot::with_attr_getters(
                &self.graph,
                &[],
                &|_, _| "fontname=\"Courier New\"".to_string(),
                &|_, (_, _)| "fontname=\"Courier New\",shape=box".to_string(),
            )
        );
        rustc_utils::mir::body::run_dot(path.as_ref(), &graph_dot.into_bytes())
    }
}

#[derive(Debug, Clone)]
pub struct PartialGraph<'tcx> {
    pub(crate) nodes: FxHashSet<DepNode<'tcx>>,
    pub(crate) edges: FxHashSet<(DepNode<'tcx>, DepNode<'tcx>, DepEdge)>,
    pub(crate) generics: GenericArgsRef<'tcx>,
    def_id: DefId,
    arg_count: usize,
    local_decls: IndexVec<Local, LocalDecl<'tcx>>,
}

impl<'tcx> HasLocalDecls<'tcx> for PartialGraph<'tcx> {
    fn local_decls(&self) -> &LocalDecls<'tcx> {
        &self.local_decls
    }
}

impl<'tcx> PartialGraph<'tcx> {
    pub fn mentioned_call_string<'a>(
        &'a self,
    ) -> impl Iterator<Item = CallString> + Captures<'tcx> + 'a {
        self.nodes
            .iter()
            .map(|n| &n.at)
            .chain(self.edges.iter().map(|e| &e.2.at))
            .copied()
    }

    pub fn new(
        generics: GenericArgsRef<'tcx>,
        def_id: DefId,
        arg_count: usize,
        local_decls: &LocalDecls<'tcx>,
    ) -> Self {
        Self {
            nodes: Default::default(),
            edges: Default::default(),
            generics,
            def_id,
            arg_count,
            local_decls: local_decls.to_owned(),
        }
    }

    /// Returns the set of source places that the parent can access (write to)
    ///
    /// Parameterized by a `is_at_root` function which returns whether a given
    /// call string refers to a location in the outermost function. This is
    /// necessary, because consumers of [`PartialGraph`] manipulate the call
    /// string and as such we cannot assume that `.len() == 1` necessarily refers
    /// to a root location. (TODO we probably should maintain that invariant)
    pub(crate) fn parentable_srcs<'a>(
        &'a self,
        is_at_root: impl Fn(CallString) -> bool,
    ) -> FxHashSet<(DepNode<'tcx>, Option<u8>)> {
        self.edges
            .iter()
            .map(|(src, _, _)| *src)
            .filter(|n| is_at_root(n.at) && n.at.leaf().location.is_start())
            .filter_map(move |a| Some((a, as_arg(&a, self.def_id, self.arg_count)?)))
            .collect()
    }

    /// Returns the set of destination places that the parent can access (read
    /// from)
    ///
    /// Parameterized by a `is_at_root` function which returns whether a given
    /// call string refers to a location in the outermost function. This is
    /// necessary, because consumers of [`PartialGraph`] manipulate the call
    /// string and as such we cannot assume that `.len() == 1` necessarily refers
    /// to a root location. (TODO we probably should maintain that invariant)
    pub(crate) fn parentable_dsts<'a>(
        &'a self,
        is_at_root: impl Fn(CallString) -> bool,
    ) -> FxHashSet<(DepNode<'tcx>, Option<u8>)> {
        self.edges
            .iter()
            .map(|(_, dst, _)| *dst)
            .filter(|n| is_at_root(n.at) && n.at.leaf().location.is_end())
            .filter_map(move |a| Some((a, as_arg(&a, self.def_id, self.arg_count)?)))
            .collect()
    }
}

fn as_arg(node: &DepNode<'_>, def_id: DefId, arg_count: usize) -> Option<Option<u8>> {
    if node.at.leaf().function != def_id {
        return None;
    }
    let local = node.place.local.as_usize();
    if node.place.local == rustc_middle::mir::RETURN_PLACE {
        Some(None)
    } else if local > 0 && (local - 1) < arg_count {
        Some(Some(node.place.local.as_u32() as u8 - 1))
    } else {
        None
    }
}

impl<'tcx> TransformCallString for PartialGraph<'tcx> {
    fn transform_call_string(&self, f: impl Fn(CallString) -> CallString) -> Self {
        let recurse_node = |n: &DepNode<'tcx>| n.transform_call_string(&f);
        Self {
            generics: self.generics,
            nodes: self.nodes.iter().map(recurse_node).collect(),
            edges: self
                .edges
                .iter()
                .map(|(from, to, e)| {
                    (
                        recurse_node(from),
                        recurse_node(to),
                        e.transform_call_string(&f),
                    )
                })
                .collect(),
            def_id: self.def_id,
            arg_count: self.arg_count,
            local_decls: self.local_decls.to_owned(),
        }
    }
}

pub(crate) trait TransformCallString {
    fn transform_call_string(&self, f: impl Fn(CallString) -> CallString) -> Self;
}

impl TransformCallString for CallString {
    fn transform_call_string(&self, f: impl Fn(CallString) -> CallString) -> Self {
        f(*self)
    }
}

impl TransformCallString for DepNode<'_> {
    fn transform_call_string(&self, f: impl Fn(CallString) -> CallString) -> Self {
        Self {
            at: f(self.at),
            ..*self
        }
    }
}

impl TransformCallString for DepEdge {
    fn transform_call_string(&self, f: impl Fn(CallString) -> CallString) -> Self {
        Self {
            at: f(self.at),
            ..*self
        }
    }
}

pub(crate) fn push_call_string_root<T: TransformCallString>(
    old: &T,
    new_root: GlobalLocation,
) -> T {
    old.transform_call_string(|c| c.push_front(new_root))
}