nyx-scanner 0.6.1

A multi-language static analysis tool for detecting security vulnerabilities
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
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
#![allow(clippy::collapsible_if)]

use std::collections::HashMap;

use super::ir::*;
use crate::cfg::Cfg;

/// Run copy propagation on an SSA body.
///
/// Identifies `Assign([single_use])` instructions where the CFG node has no
/// labels (i.e., no semantic significance like sanitizer/source), then rewrites
/// all uses of the destination value to use the source value directly.
///
/// Returns `(copies_eliminated, resolved_replacement_map)`. The replacement map
/// maps each eliminated destination SsaValue to its transitive root source
/// SsaValue, used downstream by alias analysis to recover base-variable
/// aliasing relationships.
pub fn copy_propagate(body: &mut SsaBody, cfg: &Cfg) -> (usize, HashMap<SsaValue, SsaValue>) {
    // 1. Identify copies: Assign with single operand and no labels on CFG node
    let mut replace_map: HashMap<SsaValue, SsaValue> = HashMap::new();

    for block in &body.blocks {
        for inst in &block.body {
            if let SsaOp::Assign(uses) = &inst.op {
                if uses.len() == 1 {
                    let src = uses[0];
                    let info = &cfg[inst.cfg_node];
                    // Skip if the node has labels, sanitizers, sources, sinks
                    // have semantic meaning that must be preserved.
                    if !info.taint.labels.is_empty() {
                        continue;
                    }
                    // Skip numeric-length reads (`arr.length`, `map.size`, etc.):
                    // the destination is Int-typed (a derived property of the
                    // source) while the source is typically String/Object/
                    // Unknown.  Copy-propagating through this Assign would
                    // erase the Int type fact and defeat HTML_ESCAPE / SQL /
                    // FILE_IO / SHELL sink suppression.
                    if info.is_numeric_length_access {
                        continue;
                    }
                    // Skip Assigns whose CFG node carries a `string_prefix`
                    // (template literals or `"lit" + var` RHS recognised by
                    // `extract_template_prefix`).  The abstract-interpretation
                    // `transfer_abstract` consumes that prefix to seed a
                    // StringFact on the Assign's SSA value, which downstream
                    // SSRF suppression reads.  Propagating past this Assign
                    // erases the prefix-bearing SSA value: the Call's args get
                    // rewritten to the bare upstream variable (no prefix), and
                    // `is_call_abstract_safe` falls through to a tainted-flow
                    // emission even on safe fixed-host URLs.
                    if info.string_prefix.is_some() {
                        continue;
                    }
                    replace_map.insert(inst.value, src);
                }
            }
        }
    }

    if replace_map.is_empty() {
        return (0, HashMap::new());
    }

    // 2. Build transitive replacement map: chase chains (SSA is acyclic)
    let mut resolved: HashMap<SsaValue, SsaValue> = HashMap::new();
    for &dst in replace_map.keys() {
        let root = resolve_root(dst, &replace_map);
        resolved.insert(dst, root);
    }

    // 3. Rewrite all uses
    let mut count = 0;
    for block in &mut body.blocks {
        // Rewrite phi operands
        for phi in &mut block.phis {
            if let SsaOp::Phi(operands) = &mut phi.op {
                for (_bid, val) in operands.iter_mut() {
                    if let Some(&root) = resolved.get(val) {
                        *val = root;
                    }
                }
            }
        }

        // Rewrite body instructions
        for inst in &mut block.body {
            match &mut inst.op {
                SsaOp::Assign(uses) => {
                    for val in uses.iter_mut() {
                        if let Some(&root) = resolved.get(val) {
                            *val = root;
                        }
                    }
                }
                SsaOp::Call { args, receiver, .. } => {
                    if let Some(rv) = receiver {
                        if let Some(&root) = resolved.get(rv) {
                            *rv = root;
                        }
                    }
                    for arg in args.iter_mut() {
                        for val in arg.iter_mut() {
                            if let Some(&root) = resolved.get(val) {
                                *val = root;
                            }
                        }
                    }
                }
                _ => {}
            }
        }
    }

    // 4. Convert copy instructions to Nop (DCE will clean up)
    for block in &mut body.blocks {
        for inst in &mut block.body {
            if resolved.contains_key(&inst.value) {
                inst.op = SsaOp::Nop;
                count += 1;
            }
        }
    }

    (count, resolved)
}

/// Chase the replacement chain to find the root value.
fn resolve_root(val: SsaValue, map: &HashMap<SsaValue, SsaValue>) -> SsaValue {
    let mut current = val;
    // Safety: SSA is acyclic, but cap iterations to be safe
    for _ in 0..1000 {
        match map.get(&current) {
            Some(&next) if next != current => current = next,
            _ => break,
        }
    }
    current
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::cfg::{NodeInfo, StmtKind};
    use petgraph::Graph;
    use smallvec::SmallVec;

    fn make_cfg_node(kind: StmtKind) -> NodeInfo {
        NodeInfo {
            kind,
            ..Default::default()
        }
    }

    #[test]
    fn simple_copy_eliminated() {
        // v0 = const("42"), v1 = assign(v0), v2 = assign(v1)
        let mut cfg: Cfg = Graph::new();
        let n0 = cfg.add_node(make_cfg_node(StmtKind::Seq));
        let n1 = cfg.add_node(make_cfg_node(StmtKind::Seq));
        let n2 = cfg.add_node(make_cfg_node(StmtKind::Seq));

        let mut body = SsaBody {
            blocks: vec![SsaBlock {
                id: BlockId(0),
                phis: vec![],
                body: vec![
                    SsaInst {
                        value: SsaValue(0),
                        op: SsaOp::Const(Some("42".into())),
                        cfg_node: n0,
                        var_name: Some("x".into()),
                        span: (0, 2),
                    },
                    SsaInst {
                        value: SsaValue(1),
                        op: SsaOp::Assign(SmallVec::from_elem(SsaValue(0), 1)),
                        cfg_node: n1,
                        var_name: Some("y".into()),
                        span: (3, 5),
                    },
                    SsaInst {
                        value: SsaValue(2),
                        op: SsaOp::Assign(SmallVec::from_elem(SsaValue(1), 1)),
                        cfg_node: n2,
                        var_name: Some("z".into()),
                        span: (6, 8),
                    },
                ],
                terminator: Terminator::Return(None),
                preds: SmallVec::new(),
                succs: SmallVec::new(),
            }],
            entry: BlockId(0),
            value_defs: vec![
                ValueDef {
                    var_name: Some("x".into()),
                    cfg_node: n0,
                    block: BlockId(0),
                },
                ValueDef {
                    var_name: Some("y".into()),
                    cfg_node: n1,
                    block: BlockId(0),
                },
                ValueDef {
                    var_name: Some("z".into()),
                    cfg_node: n2,
                    block: BlockId(0),
                },
            ],
            cfg_node_map: [(n0, SsaValue(0)), (n1, SsaValue(1)), (n2, SsaValue(2))]
                .into_iter()
                .collect(),
            exception_edges: vec![],
            field_interner: crate::ssa::ir::FieldInterner::default(),
            field_writes: std::collections::HashMap::new(),

            synthetic_externals: std::collections::HashSet::new(),
        };

        let (eliminated, copy_map) = copy_propagate(&mut body, &cfg);
        assert_eq!(eliminated, 2);
        // Both v1 and v2 should map to v0 (the root)
        assert_eq!(copy_map.get(&SsaValue(1)), Some(&SsaValue(0)));
        assert_eq!(copy_map.get(&SsaValue(2)), Some(&SsaValue(0)));

        // v1 and v2 should be Nop now
        assert!(matches!(body.blocks[0].body[1].op, SsaOp::Nop));
        assert!(matches!(body.blocks[0].body[2].op, SsaOp::Nop));
    }

    /// `resolve_root` has a 1000-iteration safety cap to avoid livelock if
    /// a malformed copy map ever contains a cycle (SSA itself is acyclic,
    /// but defensively we want this guarantee on the helper). Confirm the
    /// cap actually fires by feeding a hand-crafted cycle a → b → a.
    #[test]
    fn resolve_root_terminates_on_cyclic_copy_map() {
        let mut map: std::collections::HashMap<SsaValue, SsaValue> =
            std::collections::HashMap::new();
        map.insert(SsaValue(0), SsaValue(1));
        map.insert(SsaValue(1), SsaValue(0));
        // Must terminate; the exact returned value isn't a correctness
        // guarantee under malformed input, but no infinite loop is.
        let _root = resolve_root(SsaValue(0), &map);
    }

    /// A four-deep copy chain v3 = v2 = v1 = v0 must collapse to v0
    /// in a single `copy_propagate` pass, the resolved replacement
    /// map drives downstream alias recovery, so the *transitive*
    /// closure must be exposed, not just the immediate parent.
    #[test]
    fn deep_copy_chain_collapses_to_root() {
        let mut cfg: Cfg = Graph::new();
        let nodes: Vec<_> = (0..4)
            .map(|_| cfg.add_node(make_cfg_node(StmtKind::Seq)))
            .collect();

        let mut block_body = vec![SsaInst {
            value: SsaValue(0),
            op: SsaOp::Const(Some("\"x\"".into())),
            cfg_node: nodes[0],
            var_name: Some("a".into()),
            span: (0, 1),
        }];
        for (i, node) in nodes.iter().enumerate().take(4).skip(1) {
            block_body.push(SsaInst {
                value: SsaValue(i as u32),
                op: SsaOp::Assign(SmallVec::from_elem(SsaValue((i - 1) as u32), 1)),
                cfg_node: *node,
                var_name: Some(format!("v{i}")),
                span: (i, i + 1),
            });
        }

        let mut body = SsaBody {
            blocks: vec![SsaBlock {
                id: BlockId(0),
                phis: vec![],
                body: block_body,
                terminator: Terminator::Return(None),
                preds: SmallVec::new(),
                succs: SmallVec::new(),
            }],
            entry: BlockId(0),
            value_defs: (0..4)
                .map(|i| ValueDef {
                    var_name: Some(format!("v{i}")),
                    cfg_node: nodes[i],
                    block: BlockId(0),
                })
                .collect(),
            cfg_node_map: nodes
                .iter()
                .enumerate()
                .map(|(i, n)| (*n, SsaValue(i as u32)))
                .collect(),
            exception_edges: vec![],
            field_interner: crate::ssa::ir::FieldInterner::default(),
            field_writes: std::collections::HashMap::new(),

            synthetic_externals: std::collections::HashSet::new(),
        };

        let (eliminated, copy_map) = copy_propagate(&mut body, &cfg);
        assert_eq!(eliminated, 3, "v1, v2, v3 must all be eliminated");
        for i in 1..4 {
            assert_eq!(
                copy_map.get(&SsaValue(i)),
                Some(&SsaValue(0)),
                "v{i} must resolve transitively to v0"
            );
        }
    }

    // ─────────────────────────────────────────────────────────────────
    // Skip-conditions: copy-prop must NOT erase semantic info attached
    // to a copy's CFG node. These guard the three early-exits in
    // `copy_propagate`: labels, numeric-length, and string_prefix.
    // ─────────────────────────────────────────────────────────────────

    /// Build a single-block SSA body containing
    ///   v0 = Const, v1 = Assign(v0)
    /// with `node1_decorator` applied to v1's CFG node so individual
    /// skip-conditions can be exercised.
    fn build_two_inst_body(decorate: impl FnOnce(&mut NodeInfo)) -> (Cfg, SsaBody) {
        let mut cfg: Cfg = Graph::new();
        let n0 = cfg.add_node(make_cfg_node(StmtKind::Seq));
        let mut n1_info = make_cfg_node(StmtKind::Seq);
        decorate(&mut n1_info);
        let n1 = cfg.add_node(n1_info);
        let body = SsaBody {
            blocks: vec![SsaBlock {
                id: BlockId(0),
                phis: vec![],
                body: vec![
                    SsaInst {
                        value: SsaValue(0),
                        op: SsaOp::Const(Some("42".into())),
                        cfg_node: n0,
                        var_name: Some("x".into()),
                        span: (0, 2),
                    },
                    SsaInst {
                        value: SsaValue(1),
                        op: SsaOp::Assign(SmallVec::from_elem(SsaValue(0), 1)),
                        cfg_node: n1,
                        var_name: Some("y".into()),
                        span: (3, 5),
                    },
                ],
                terminator: Terminator::Return(None),
                preds: SmallVec::new(),
                succs: SmallVec::new(),
            }],
            entry: BlockId(0),
            value_defs: vec![
                ValueDef {
                    var_name: Some("x".into()),
                    cfg_node: n0,
                    block: BlockId(0),
                },
                ValueDef {
                    var_name: Some("y".into()),
                    cfg_node: n1,
                    block: BlockId(0),
                },
            ],
            cfg_node_map: [(n0, SsaValue(0)), (n1, SsaValue(1))].into_iter().collect(),
            exception_edges: vec![],
            field_interner: crate::ssa::ir::FieldInterner::default(),
            field_writes: std::collections::HashMap::new(),

            synthetic_externals: std::collections::HashSet::new(),
        };
        (cfg, body)
    }

    /// Skip path 1: an Assign whose CFG node carries a label
    /// (sanitizer/source/sink) must NOT be propagated through. Erasing
    /// that label would silently drop a sanitization step from the
    /// taint path.
    #[test]
    fn copy_with_label_on_cfg_node_is_not_propagated() {
        use crate::labels::{Cap, DataLabel};
        use smallvec::smallvec;
        let (cfg, mut body) = build_two_inst_body(|info| {
            info.taint.labels = smallvec![DataLabel::Sanitizer(Cap::SHELL_ESCAPE)];
        });
        let (eliminated, _map) = copy_propagate(&mut body, &cfg);
        assert_eq!(eliminated, 0, "copy through a labeled node must be skipped");
        assert!(
            matches!(body.blocks[0].body[1].op, SsaOp::Assign(_)),
            "labeled copy must remain an Assign, not be Nop'd"
        );
    }

    /// Skip path 2: numeric-length reads (`arr.length`, `map.size`)
    /// have a different type from their source, propagating through
    /// would erase the Int type fact.
    #[test]
    fn copy_through_numeric_length_access_is_not_propagated() {
        let (cfg, mut body) = build_two_inst_body(|info| {
            info.is_numeric_length_access = true;
        });
        let (eliminated, _map) = copy_propagate(&mut body, &cfg);
        assert_eq!(
            eliminated, 0,
            "copy through numeric-length access must be skipped"
        );
    }

    /// Skip path 3: an Assign carrying a `string_prefix` (template
    /// literal or `"lit" + var` RHS) seeds a StringFact on its SSA
    /// value. Propagating past it erases the prefix-bearing value and
    /// breaks SSRF prefix-lock suppression downstream.
    #[test]
    fn copy_through_string_prefix_node_is_not_propagated() {
        let (cfg, mut body) = build_two_inst_body(|info| {
            info.string_prefix = Some("https://api.example.com/".into());
        });
        let (eliminated, _map) = copy_propagate(&mut body, &cfg);
        assert_eq!(
            eliminated, 0,
            "copy through string_prefix-bearing node must be skipped"
        );
    }

    /// Multi-operand Assigns (e.g. `v2 = v0 + v1`) are NOT copies and
    /// must be left alone. Only single-operand Assigns are copies.
    #[test]
    fn multi_operand_assign_is_not_a_copy() {
        let mut cfg: Cfg = Graph::new();
        let n0 = cfg.add_node(make_cfg_node(StmtKind::Seq));
        let n1 = cfg.add_node(make_cfg_node(StmtKind::Seq));
        let n2 = cfg.add_node(make_cfg_node(StmtKind::Seq));
        let mut body = SsaBody {
            blocks: vec![SsaBlock {
                id: BlockId(0),
                phis: vec![],
                body: vec![
                    SsaInst {
                        value: SsaValue(0),
                        op: SsaOp::Const(Some("1".into())),
                        cfg_node: n0,
                        var_name: Some("x".into()),
                        span: (0, 1),
                    },
                    SsaInst {
                        value: SsaValue(1),
                        op: SsaOp::Const(Some("2".into())),
                        cfg_node: n1,
                        var_name: Some("y".into()),
                        span: (2, 3),
                    },
                    SsaInst {
                        value: SsaValue(2),
                        op: SsaOp::Assign({
                            let mut v: SmallVec<[SsaValue; 4]> = SmallVec::new();
                            v.push(SsaValue(0));
                            v.push(SsaValue(1));
                            v
                        }),
                        cfg_node: n2,
                        var_name: Some("z".into()),
                        span: (4, 5),
                    },
                ],
                terminator: Terminator::Return(None),
                preds: SmallVec::new(),
                succs: SmallVec::new(),
            }],
            entry: BlockId(0),
            value_defs: vec![
                ValueDef {
                    var_name: Some("x".into()),
                    cfg_node: n0,
                    block: BlockId(0),
                },
                ValueDef {
                    var_name: Some("y".into()),
                    cfg_node: n1,
                    block: BlockId(0),
                },
                ValueDef {
                    var_name: Some("z".into()),
                    cfg_node: n2,
                    block: BlockId(0),
                },
            ],
            cfg_node_map: [(n0, SsaValue(0)), (n1, SsaValue(1)), (n2, SsaValue(2))]
                .into_iter()
                .collect(),
            exception_edges: vec![],
            field_interner: crate::ssa::ir::FieldInterner::default(),
            field_writes: std::collections::HashMap::new(),

            synthetic_externals: std::collections::HashSet::new(),
        };
        let (eliminated, _map) = copy_propagate(&mut body, &cfg);
        assert_eq!(eliminated, 0, "two-operand Assign is not a copy");
        assert!(
            matches!(body.blocks[0].body[2].op, SsaOp::Assign(_)),
            "multi-operand Assign must be preserved"
        );
    }

    /// A Call's argument and receiver slots that reference a
    /// copy-eliminated value must be rewritten to the root.
    #[test]
    fn call_args_and_receiver_rewritten_through_copy() {
        let mut cfg: Cfg = Graph::new();
        let n0 = cfg.add_node(make_cfg_node(StmtKind::Seq));
        let n1 = cfg.add_node(make_cfg_node(StmtKind::Seq));
        let n2 = cfg.add_node(make_cfg_node(StmtKind::Call));
        let mut arg_vec: SmallVec<[SsaValue; 2]> = SmallVec::new();
        arg_vec.push(SsaValue(1));
        let mut body = SsaBody {
            blocks: vec![SsaBlock {
                id: BlockId(0),
                phis: vec![],
                body: vec![
                    SsaInst {
                        value: SsaValue(0),
                        op: SsaOp::Const(Some("\"x\"".into())),
                        cfg_node: n0,
                        var_name: Some("a".into()),
                        span: (0, 1),
                    },
                    SsaInst {
                        value: SsaValue(1),
                        op: SsaOp::Assign(SmallVec::from_elem(SsaValue(0), 1)),
                        cfg_node: n1,
                        var_name: Some("b".into()),
                        span: (2, 3),
                    },
                    SsaInst {
                        value: SsaValue(2),
                        op: SsaOp::Call {
                            callee: "f".into(),
                            callee_text: None,
                            args: vec![arg_vec],
                            receiver: Some(SsaValue(1)),
                        },
                        cfg_node: n2,
                        var_name: None,
                        span: (4, 7),
                    },
                ],
                terminator: Terminator::Return(None),
                preds: SmallVec::new(),
                succs: SmallVec::new(),
            }],
            entry: BlockId(0),
            value_defs: vec![
                ValueDef {
                    var_name: Some("a".into()),
                    cfg_node: n0,
                    block: BlockId(0),
                },
                ValueDef {
                    var_name: Some("b".into()),
                    cfg_node: n1,
                    block: BlockId(0),
                },
                ValueDef {
                    var_name: None,
                    cfg_node: n2,
                    block: BlockId(0),
                },
            ],
            cfg_node_map: [(n0, SsaValue(0)), (n1, SsaValue(1)), (n2, SsaValue(2))]
                .into_iter()
                .collect(),
            exception_edges: vec![],
            field_interner: crate::ssa::ir::FieldInterner::default(),
            field_writes: std::collections::HashMap::new(),

            synthetic_externals: std::collections::HashSet::new(),
        };
        let (eliminated, _) = copy_propagate(&mut body, &cfg);
        assert_eq!(eliminated, 1, "v1 should be eliminated");
        let call_inst = &body.blocks[0].body[2];
        match &call_inst.op {
            SsaOp::Call { args, receiver, .. } => {
                assert_eq!(receiver, &Some(SsaValue(0)), "receiver rewritten to root");
                assert_eq!(args[0][0], SsaValue(0), "call arg rewritten to root");
            }
            other => panic!("expected Call op, got {:?}", other),
        }
    }

    /// Phi operand referencing a copy-eliminated value must be
    /// rewritten to the root.
    #[test]
    fn phi_operand_rewritten_through_copy() {
        let mut cfg: Cfg = Graph::new();
        let n0 = cfg.add_node(make_cfg_node(StmtKind::Seq));
        let n1 = cfg.add_node(make_cfg_node(StmtKind::Seq));
        let n2 = cfg.add_node(make_cfg_node(StmtKind::Seq));
        // Block 0: v0=const, v1=assign(v0)
        // Block 1: v2 = phi(B0: v1)
        let mut phi_ops: smallvec::SmallVec<[(BlockId, SsaValue); 2]> = smallvec::SmallVec::new();
        phi_ops.push((BlockId(0), SsaValue(1)));
        let mut body = SsaBody {
            blocks: vec![
                SsaBlock {
                    id: BlockId(0),
                    phis: vec![],
                    body: vec![
                        SsaInst {
                            value: SsaValue(0),
                            op: SsaOp::Const(Some("\"v0\"".into())),
                            cfg_node: n0,
                            var_name: Some("a".into()),
                            span: (0, 1),
                        },
                        SsaInst {
                            value: SsaValue(1),
                            op: SsaOp::Assign(SmallVec::from_elem(SsaValue(0), 1)),
                            cfg_node: n1,
                            var_name: Some("b".into()),
                            span: (2, 3),
                        },
                    ],
                    terminator: Terminator::Goto(BlockId(1)),
                    preds: SmallVec::new(),
                    succs: {
                        let mut s = SmallVec::new();
                        s.push(BlockId(1));
                        s
                    },
                },
                SsaBlock {
                    id: BlockId(1),
                    phis: vec![SsaInst {
                        value: SsaValue(2),
                        op: SsaOp::Phi(phi_ops),
                        cfg_node: n2,
                        var_name: Some("b".into()),
                        span: (4, 5),
                    }],
                    body: vec![],
                    terminator: Terminator::Return(Some(SsaValue(2))),
                    preds: {
                        let mut p = SmallVec::new();
                        p.push(BlockId(0));
                        p
                    },
                    succs: SmallVec::new(),
                },
            ],
            entry: BlockId(0),
            value_defs: vec![
                ValueDef {
                    var_name: Some("a".into()),
                    cfg_node: n0,
                    block: BlockId(0),
                },
                ValueDef {
                    var_name: Some("b".into()),
                    cfg_node: n1,
                    block: BlockId(0),
                },
                ValueDef {
                    var_name: Some("b".into()),
                    cfg_node: n2,
                    block: BlockId(1),
                },
            ],
            cfg_node_map: [(n0, SsaValue(0)), (n1, SsaValue(1)), (n2, SsaValue(2))]
                .into_iter()
                .collect(),
            exception_edges: vec![],
            field_interner: crate::ssa::ir::FieldInterner::default(),
            field_writes: std::collections::HashMap::new(),

            synthetic_externals: std::collections::HashSet::new(),
        };
        let (eliminated, _map) = copy_propagate(&mut body, &cfg);
        assert_eq!(eliminated, 1);
        // The phi in block 1 should now reference v0, not v1.
        let phi = &body.blocks[1].phis[0];
        match &phi.op {
            SsaOp::Phi(ops) => {
                assert_eq!(
                    ops[0].1,
                    SsaValue(0),
                    "phi operand should be rewritten to root v0"
                );
            }
            other => panic!("expected Phi op, got {:?}", other),
        }
    }

    /// `copy_propagate` on a body with no Assign instructions returns
    /// `(0, empty_map)` and leaves the body untouched.
    #[test]
    fn no_op_when_no_copies_exist() {
        let mut cfg: Cfg = Graph::new();
        let n0 = cfg.add_node(make_cfg_node(StmtKind::Seq));
        let mut body = SsaBody {
            blocks: vec![SsaBlock {
                id: BlockId(0),
                phis: vec![],
                body: vec![SsaInst {
                    value: SsaValue(0),
                    op: SsaOp::Const(Some("42".into())),
                    cfg_node: n0,
                    var_name: Some("x".into()),
                    span: (0, 2),
                }],
                terminator: Terminator::Return(None),
                preds: SmallVec::new(),
                succs: SmallVec::new(),
            }],
            entry: BlockId(0),
            value_defs: vec![ValueDef {
                var_name: Some("x".into()),
                cfg_node: n0,
                block: BlockId(0),
            }],
            cfg_node_map: [(n0, SsaValue(0))].into_iter().collect(),
            exception_edges: vec![],
            field_interner: crate::ssa::ir::FieldInterner::default(),
            field_writes: std::collections::HashMap::new(),

            synthetic_externals: std::collections::HashSet::new(),
        };
        let (eliminated, map) = copy_propagate(&mut body, &cfg);
        assert_eq!(eliminated, 0);
        assert!(map.is_empty());
    }
}