dotscope 0.6.0

A high-performance, cross-platform framework for analyzing and reverse engineering .NET PE executables
Documentation
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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
//! Control flow graph view of SSA functions.
//!
//! This module provides [`SsaCfg`], a lightweight CFG view that can be constructed
//! directly from an [`SsaFunction`]. Unlike the CIL-based [`ControlFlowGraph`] which
//! is built from basic blocks, this CFG is derived from SSA block terminators.
//!
//! # Purpose
//!
//! The primary use case is to enable dataflow analyses (like SCCP) that require
//! a CFG to work on SSA functions during deobfuscation passes. Since passes only
//! receive `SsaFunction` (not the original CFG), this module bridges that gap.
//!
//! # Design
//!
//! `SsaCfg` implements the standard graph traits:
//! - [`GraphBase`] - Node count and iteration
//! - [`Successors`] - Forward edge traversal (from terminators)
//! - [`Predecessors`] - Backward edge traversal (computed from successors)
//! - [`RootedGraph`] - Entry node (block 0)
//!
//! This allows it to be used with the existing dataflow analysis infrastructure,
//! particularly the SCCP algorithm in [`crate::analysis::dataflow::sccp`].
//!
//! # Construction
//!
//! The CFG is constructed on-demand from the SSA function:
//!
//! ```rust,ignore
//! use dotscope::analysis::{SsaCfg, SsaFunction};
//!
//! let ssa: SsaFunction = /* ... */;
//! let cfg = SsaCfg::from_ssa(&ssa);
//!
//! // Use with SCCP
//! let mut sccp = ConstantPropagation::new(PointerSize::Bit64);
//! let results = sccp.analyze(&ssa, &cfg);
//! ```
//!
//! [`ControlFlowGraph`]: crate::analysis::ControlFlowGraph

use crate::{
    analysis::ssa::SsaFunction,
    utils::graph::{
        algorithms::{postorder, reverse_postorder},
        GraphBase, NodeId, Predecessors, RootedGraph, Successors,
    },
};

/// A lightweight control flow graph view of an SSA function.
///
/// This struct provides a CFG interface over an existing [`SsaFunction`],
/// extracting control flow edges from block terminators. It's designed to
/// enable dataflow analyses that require a CFG without duplicating the
/// underlying SSA structure.
///
/// # Performance
///
/// The CFG computes and caches predecessor lists on construction. This is
/// an O(E) operation where E is the number of edges (typically similar to
/// the number of blocks). Once constructed, all queries are O(1) or O(k)
/// where k is the number of adjacent nodes.
///
/// # Lifetime
///
/// The CFG holds a reference to the SSA function it was created from.
/// The CFG must not outlive the SSA function.
#[derive(Debug)]
pub struct SsaCfg<'a> {
    /// Reference to the SSA function.
    ssa: &'a SsaFunction,
    /// Precomputed predecessor lists for each block.
    /// predecessors[block_id] = list of blocks that can jump to block_id.
    predecessors: Vec<Vec<usize>>,
}

impl<'a> SsaCfg<'a> {
    /// Creates a CFG view from an SSA function.
    ///
    /// This extracts control flow edges by examining the terminator of each
    /// SSA block. Predecessors are computed and cached for efficient backward
    /// traversal.
    ///
    /// # Arguments
    ///
    /// * `ssa` - The SSA function to create a CFG view of.
    ///
    /// # Returns
    ///
    /// A new `SsaCfg` view of the given function.
    ///
    /// # Example
    ///
    /// ```rust,ignore
    /// let cfg = SsaCfg::from_ssa(&ssa_function);
    /// assert_eq!(cfg.node_count(), ssa_function.block_count());
    /// ```
    #[must_use]
    pub fn from_ssa(ssa: &'a SsaFunction) -> Self {
        let block_count = ssa.block_count();
        let mut predecessors = vec![Vec::new(); block_count];

        // Build predecessor lists by iterating over all blocks and their terminators
        for block_idx in 0..block_count {
            if let Some(block) = ssa.block(block_idx) {
                // Find the terminator instruction (last instruction that is a terminator)
                let successors = block
                    .instructions()
                    .iter()
                    .rev()
                    .find_map(|instr| {
                        let op = instr.op();
                        if op.is_terminator() {
                            Some(op.successors())
                        } else {
                            None
                        }
                    })
                    .unwrap_or_default();

                // Add this block as a predecessor of each successor
                for succ in successors {
                    if succ < block_count {
                        predecessors[succ].push(block_idx);
                    }
                }
            }
        }

        Self { ssa, predecessors }
    }

    /// Returns the underlying SSA function.
    ///
    /// This can be used to access block and instruction data while
    /// traversing the CFG.
    #[must_use]
    pub const fn ssa(&self) -> &'a SsaFunction {
        self.ssa
    }

    /// Returns the number of blocks in the CFG.
    #[must_use]
    pub fn block_count(&self) -> usize {
        self.ssa.block_count()
    }

    /// Returns true if the CFG has no blocks.
    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.ssa.is_empty()
    }

    /// Returns the successor block indices for a given block.
    ///
    /// Successors are determined by the block's terminator instruction.
    ///
    /// # Arguments
    ///
    /// * `block_idx` - The block index to query.
    ///
    /// # Returns
    ///
    /// A vector of successor block indices. Empty if the block has no
    /// successors (e.g., return, throw) or doesn't exist.
    #[must_use]
    pub fn block_successors(&self, block_idx: usize) -> Vec<usize> {
        self.ssa
            .block(block_idx)
            .and_then(|block| {
                block.instructions().iter().rev().find_map(|instr| {
                    let op = instr.op();
                    if op.is_terminator() {
                        Some(op.successors())
                    } else {
                        None
                    }
                })
            })
            .unwrap_or_default()
    }

    /// Returns the predecessor block indices for a given block.
    ///
    /// # Arguments
    ///
    /// * `block_idx` - The block index to query.
    ///
    /// # Returns
    ///
    /// A slice of predecessor block indices.
    #[must_use]
    pub fn block_predecessors(&self, block_idx: usize) -> &[usize] {
        self.predecessors.get(block_idx).map_or(&[], Vec::as_slice)
    }

    /// Returns the exit nodes of the CFG.
    ///
    /// Exit nodes are blocks with no successors (blocks that end in return,
    /// throw, or other terminating instructions).
    ///
    /// # Returns
    ///
    /// A vector of exit node IDs.
    #[must_use]
    pub fn exits(&self) -> Vec<NodeId> {
        let mut exits = Vec::new();
        for idx in 0..self.ssa.block_count() {
            if self.block_successors(idx).is_empty() {
                exits.push(NodeId::new(idx));
            }
        }
        exits
    }

    /// Returns blocks in postorder traversal.
    ///
    /// Postorder is useful for backward data flow analysis.
    ///
    /// # Returns
    ///
    /// A vector of node IDs in postorder.
    #[must_use]
    pub fn postorder(&self) -> Vec<NodeId> {
        postorder(self, self.entry())
    }

    /// Returns blocks in reverse postorder traversal.
    ///
    /// Reverse postorder is useful for forward data flow analysis.
    ///
    /// # Returns
    ///
    /// A vector of node IDs in reverse postorder.
    #[must_use]
    pub fn reverse_postorder(&self) -> Vec<NodeId> {
        reverse_postorder(self, self.entry())
    }
}

impl GraphBase for SsaCfg<'_> {
    fn node_count(&self) -> usize {
        self.ssa.block_count()
    }

    fn node_ids(&self) -> impl Iterator<Item = NodeId> {
        (0..self.ssa.block_count()).map(NodeId::new)
    }
}

impl Successors for SsaCfg<'_> {
    fn successors(&self, node: NodeId) -> impl Iterator<Item = NodeId> {
        self.block_successors(node.index())
            .into_iter()
            .map(NodeId::new)
    }
}

impl Predecessors for SsaCfg<'_> {
    fn predecessors(&self, node: NodeId) -> impl Iterator<Item = NodeId> {
        self.block_predecessors(node.index())
            .iter()
            .copied()
            .map(NodeId::new)
    }
}

impl RootedGraph for SsaCfg<'_> {
    fn entry(&self) -> NodeId {
        NodeId::new(0)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::analysis::ssa::{SsaBlock, SsaInstruction, SsaOp, SsaVarId};

    /// Creates a simple test SSA function with the given block structure.
    fn create_test_ssa(terminators: Vec<SsaOp>) -> SsaFunction {
        let mut ssa = SsaFunction::new(0, 0);

        for (idx, terminator) in terminators.into_iter().enumerate() {
            let mut block = SsaBlock::new(idx);
            block.add_instruction(SsaInstruction::synthetic(terminator));
            ssa.add_block(block);
        }

        ssa
    }

    #[test]
    fn test_empty_ssa() {
        let ssa = SsaFunction::new(0, 0);
        let cfg = SsaCfg::from_ssa(&ssa);

        assert!(cfg.is_empty());
        assert_eq!(cfg.node_count(), 0);
    }

    #[test]
    fn test_single_block() {
        let ssa = create_test_ssa(vec![SsaOp::Return { value: None }]);
        let cfg = SsaCfg::from_ssa(&ssa);

        assert_eq!(cfg.node_count(), 1);
        assert_eq!(cfg.entry(), NodeId::new(0));
        assert!(cfg.block_successors(0).is_empty());
        assert!(cfg.block_predecessors(0).is_empty());
    }

    #[test]
    fn test_linear_blocks() {
        // B0 -> B1 -> B2 (return)
        let ssa = create_test_ssa(vec![
            SsaOp::Jump { target: 1 },
            SsaOp::Jump { target: 2 },
            SsaOp::Return { value: None },
        ]);
        let cfg = SsaCfg::from_ssa(&ssa);

        assert_eq!(cfg.node_count(), 3);

        // Check successors
        assert_eq!(cfg.block_successors(0), vec![1]);
        assert_eq!(cfg.block_successors(1), vec![2]);
        assert!(cfg.block_successors(2).is_empty());

        // Check predecessors
        assert!(cfg.block_predecessors(0).is_empty());
        assert_eq!(cfg.block_predecessors(1), vec![0]);
        assert_eq!(cfg.block_predecessors(2), vec![1]);
    }

    #[test]
    fn test_diamond_cfg() {
        // B0 (branch) -> B1, B2 -> B3 (return)
        let cond = SsaVarId::new();
        let ssa = create_test_ssa(vec![
            SsaOp::Branch {
                condition: cond,
                true_target: 1,
                false_target: 2,
            },
            SsaOp::Jump { target: 3 },
            SsaOp::Jump { target: 3 },
            SsaOp::Return { value: None },
        ]);
        let cfg = SsaCfg::from_ssa(&ssa);

        assert_eq!(cfg.node_count(), 4);

        // B0 has two successors
        let b0_succs = cfg.block_successors(0);
        assert_eq!(b0_succs.len(), 2);
        assert!(b0_succs.contains(&1));
        assert!(b0_succs.contains(&2));

        // B3 has two predecessors
        let b3_preds = cfg.block_predecessors(3);
        assert_eq!(b3_preds.len(), 2);
        assert!(b3_preds.contains(&1));
        assert!(b3_preds.contains(&2));
    }

    #[test]
    fn test_loop_cfg() {
        // B0 -> B1 (loop) -> B1 (back edge) or B2 (exit)
        let cond = SsaVarId::new();
        let ssa = create_test_ssa(vec![
            SsaOp::Jump { target: 1 },
            SsaOp::Branch {
                condition: cond,
                true_target: 1, // back edge
                false_target: 2,
            },
            SsaOp::Return { value: None },
        ]);
        let cfg = SsaCfg::from_ssa(&ssa);

        assert_eq!(cfg.node_count(), 3);

        // B1 has itself as a predecessor (back edge)
        let b1_preds = cfg.block_predecessors(1);
        assert_eq!(b1_preds.len(), 2);
        assert!(b1_preds.contains(&0));
        assert!(b1_preds.contains(&1)); // self-loop
    }

    #[test]
    fn test_switch_cfg() {
        // B0 (switch) -> B1, B2, B3 (cases), B4 (default)
        let val = SsaVarId::new();
        let ssa = create_test_ssa(vec![
            SsaOp::Switch {
                value: val,
                targets: vec![1, 2, 3],
                default: 4,
            },
            SsaOp::Return { value: None },
            SsaOp::Return { value: None },
            SsaOp::Return { value: None },
            SsaOp::Return { value: None },
        ]);
        let cfg = SsaCfg::from_ssa(&ssa);

        assert_eq!(cfg.node_count(), 5);

        // B0 has 4 successors (3 cases + default)
        let b0_succs = cfg.block_successors(0);
        assert_eq!(b0_succs.len(), 4);
        assert!(b0_succs.contains(&1));
        assert!(b0_succs.contains(&2));
        assert!(b0_succs.contains(&3));
        assert!(b0_succs.contains(&4));
    }

    #[test]
    fn test_graph_traits() {
        let ssa = create_test_ssa(vec![
            SsaOp::Jump { target: 1 },
            SsaOp::Return { value: None },
        ]);
        let cfg = SsaCfg::from_ssa(&ssa);

        // Test GraphBase
        assert_eq!(GraphBase::node_count(&cfg), 2);
        let node_ids: Vec<_> = GraphBase::node_ids(&cfg).collect();
        assert_eq!(node_ids, vec![NodeId::new(0), NodeId::new(1)]);

        // Test Successors
        let succs: Vec<_> = Successors::successors(&cfg, NodeId::new(0)).collect();
        assert_eq!(succs, vec![NodeId::new(1)]);

        // Test Predecessors
        let preds: Vec<_> = Predecessors::predecessors(&cfg, NodeId::new(1)).collect();
        assert_eq!(preds, vec![NodeId::new(0)]);

        // Test RootedGraph
        assert_eq!(RootedGraph::entry(&cfg), NodeId::new(0));
    }
}