jingle 0.6.8

SMT Modeling for Ghidra's PCODE
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
//! This is a placeholder structuring algorithm, written with agent assistance given existing graph structuring literature,
//! the CPA traits in this repo, and the StructuredCfg type as a goal. It has not been evaluated except on simple
//! test-cases and is subject to change.
use crate::analysis::cfg::{CfgNode, PcodeCfg};
use crate::analysis::dominators::{DominatorTree, PostDominatorTree};
use jingle_sleigh::{JingleDisplay, SleighArchInfo};
use std::collections::HashSet;
use std::fmt;

pub trait CbranchInfo {
    fn cbranch_input0(&self) -> Option<&jingle_sleigh::VarNode>;
}

impl CbranchInfo for () {
    fn cbranch_input0(&self) -> Option<&jingle_sleigh::VarNode> {
        None
    }
}

impl CbranchInfo for jingle_sleigh::PcodeOperation {
    fn cbranch_input0(&self) -> Option<&jingle_sleigh::VarNode> {
        if let jingle_sleigh::PcodeOperation::CBranch { input0, .. } = self {
            Some(input0)
        } else {
            None
        }
    }
}

impl<'a> CbranchInfo for jingle_sleigh::PcodeOpRef<'a> {
    fn cbranch_input0(&self) -> Option<&jingle_sleigh::VarNode> {
        use std::ops::Deref;
        self.deref().cbranch_input0()
    }
}

impl CbranchInfo for Vec<jingle_sleigh::PcodeOperation> {
    fn cbranch_input0(&self) -> Option<&jingle_sleigh::VarNode> {
        self.last()?.cbranch_input0()
    }
}

#[derive(Debug, Clone)]
pub enum StructuredCfg<N> {
    Block(N),
    Sequence(Vec<StructuredCfg<N>>),
    IfElse {
        header: N,
        then_branch: Box<StructuredCfg<N>>,
        else_branch: Box<StructuredCfg<N>>,
    },
    Loop {
        header: N,
        body: Box<StructuredCfg<N>>,
    },
}

fn fmt_indent<N: fmt::Display>(
    cfg: &StructuredCfg<N>,
    f: &mut fmt::Formatter<'_>,
    depth: usize,
) -> fmt::Result {
    let pad = "  ".repeat(depth);
    match cfg {
        StructuredCfg::Block(n) => writeln!(f, "{pad}block({n})"),
        StructuredCfg::Sequence(items) => {
            for item in items {
                fmt_indent(item, f, depth)?;
            }
            Ok(())
        }
        StructuredCfg::IfElse {
            header,
            then_branch,
            else_branch,
        } => {
            writeln!(f, "{pad}if {header} {{")?;
            fmt_indent(then_branch, f, depth + 1)?;
            writeln!(f, "{pad}}} else {{")?;
            fmt_indent(else_branch, f, depth + 1)?;
            writeln!(f, "{pad}}}")
        }
        StructuredCfg::Loop { header, body } => {
            writeln!(f, "{pad}loop {header} {{")?;
            fmt_indent(body, f, depth + 1)?;
            writeln!(f, "{pad}}}")
        }
    }
}

impl<N: fmt::Display> fmt::Display for StructuredCfg<N> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        fmt_indent(self, f, 0)
    }
}

fn fmt_jingle_indent<N: JingleDisplay>(
    cfg: &StructuredCfg<N>,
    f: &mut fmt::Formatter<'_>,
    ctx: &SleighArchInfo,
    depth: usize,
) -> fmt::Result {
    let pad = "  ".repeat(depth);
    match cfg {
        StructuredCfg::Block(n) => writeln!(f, "{pad}block({})", n.display(ctx)),
        StructuredCfg::Sequence(items) => {
            for item in items {
                fmt_jingle_indent(item, f, ctx, depth)?;
            }
            Ok(())
        }
        StructuredCfg::IfElse {
            header,
            then_branch,
            else_branch,
        } => {
            writeln!(f, "{pad}if {} {{", header.display(ctx))?;
            fmt_jingle_indent(then_branch, f, ctx, depth + 1)?;
            writeln!(f, "{pad}}} else {{")?;
            fmt_jingle_indent(else_branch, f, ctx, depth + 1)?;
            writeln!(f, "{pad}}}")
        }
        StructuredCfg::Loop { header, body } => {
            writeln!(f, "{pad}loop {} {{", header.display(ctx))?;
            fmt_jingle_indent(body, f, ctx, depth + 1)?;
            writeln!(f, "{pad}}}")
        }
    }
}

impl<N: JingleDisplay> JingleDisplay for StructuredCfg<N> {
    fn fmt_jingle(&self, f: &mut fmt::Formatter<'_>, ctx: &SleighArchInfo) -> fmt::Result {
        fmt_jingle_indent(self, f, ctx, 0)
    }
}

#[derive(Debug, thiserror::Error)]
pub enum StructuringError {
    #[error("irreducible control flow")]
    Irreducible,
    #[error("empty CFG")]
    Empty,
}

pub fn structure<N: CfgNode, D: Clone + CbranchInfo>(
    mut cfg: PcodeCfg<N, D>,
) -> Result<StructuredCfg<N>, StructuringError> {
    cfg.trim_symbolic_leaves();
    let entries: Vec<&N> = cfg.entry_nodes().collect();
    match entries.len() {
        0 => return Err(StructuringError::Empty),
        1 => {}
        _ => return Err(StructuringError::Irreducible),
    }
    let entry = entries[0].clone();
    let dom = cfg.dominator_tree();
    let pdom = cfg.post_dominator_tree();
    check_reducibility(&cfg)?;
    structure_from(&entry, None, &cfg, &dom, &pdom, &HashSet::new())
}

/// Computes all strongly connected components using Kosaraju's algorithm.
fn compute_sccs<N: CfgNode, D: Clone>(cfg: &PcodeCfg<N, D>) -> Vec<HashSet<N>> {
    let all_nodes: Vec<N> = cfg.nodes().cloned().collect();

    // Phase 1: forward DFS, record post-order finish times.
    let mut visited: HashSet<N> = HashSet::new();
    let mut post_order: Vec<N> = Vec::new();

    for start in &all_nodes {
        if visited.contains(start) {
            continue;
        }
        visited.insert(start.clone());
        let start_succs: Vec<N> = cfg
            .successors(start)
            .unwrap_or_default()
            .into_iter()
            .cloned()
            .collect();
        let mut dfs_stack: Vec<(N, usize, Vec<N>)> = vec![(start.clone(), 0, start_succs)];
        loop {
            let next = {
                let Some(frame) = dfs_stack.last_mut() else {
                    break;
                };
                if frame.1 < frame.2.len() {
                    let succ = frame.2[frame.1].clone();
                    frame.1 += 1;
                    Some(succ)
                } else {
                    None
                }
            };
            match next {
                Some(succ) => {
                    if visited.insert(succ.clone()) {
                        let succs: Vec<N> = cfg
                            .successors(&succ)
                            .unwrap_or_default()
                            .into_iter()
                            .cloned()
                            .collect();
                        dfs_stack.push((succ, 0, succs));
                    }
                }
                None => {
                    let Some((node, _, _)) = dfs_stack.pop() else {
                        break;
                    };
                    post_order.push(node);
                }
            }
        }
    }

    // Phase 2: reverse DFS in reverse post-order assigns each node to an SCC.
    let mut visited2: HashSet<N> = HashSet::new();
    let mut sccs: Vec<HashSet<N>> = Vec::new();

    for start in post_order.iter().rev() {
        if visited2.contains(start) {
            continue;
        }
        let mut scc: HashSet<N> = HashSet::new();
        let mut stack: Vec<N> = vec![start.clone()];
        while let Some(node) = stack.pop() {
            if !visited2.insert(node.clone()) {
                continue;
            }
            scc.insert(node.clone());
            for pred in cfg.predecessors(&node).unwrap_or_default() {
                if !visited2.contains(pred) {
                    stack.push(pred.clone());
                }
            }
        }
        sccs.push(scc);
    }

    sccs
}

fn check_reducibility<N: CfgNode, D: Clone>(cfg: &PcodeCfg<N, D>) -> Result<(), StructuringError> {
    // A CFG is irreducible iff any strongly connected component (SCC) with more
    // than one node has multiple external entry points — nodes inside the SCC
    // that have at least one predecessor outside the SCC.  Reducible natural
    // loops always have exactly one such header.
    for scc in compute_sccs(cfg) {
        if scc.len() <= 1 {
            continue;
        }
        let external_entries = scc
            .iter()
            .filter(|node| {
                cfg.predecessors(*node)
                    .unwrap_or_default()
                    .into_iter()
                    .any(|pred| !scc.contains(pred))
            })
            .count();
        if external_entries > 1 {
            return Err(StructuringError::Irreducible);
        }
    }
    Ok(())
}

fn natural_loop_body<N: CfgNode, D: Clone>(
    header: &N,
    latches: &[N],
    cfg: &PcodeCfg<N, D>,
    dom: &DominatorTree<N>,
) -> HashSet<N> {
    let mut body: HashSet<N> = HashSet::new();
    body.insert(header.clone());
    let mut worklist: Vec<N> = latches.to_vec();
    while let Some(node) = worklist.pop() {
        if body.contains(&node) {
            continue;
        }
        body.insert(node.clone());
        for pred in cfg.predecessors(&node).unwrap_or_default() {
            if !body.contains(pred) && dom.dominates(header, pred) {
                worklist.push(pred.clone());
            }
        }
    }
    body
}

fn flatten<N: CfgNode>(items: Vec<StructuredCfg<N>>) -> StructuredCfg<N> {
    let mut result: Vec<StructuredCfg<N>> = Vec::new();
    for item in items {
        match item {
            StructuredCfg::Sequence(inner) => result.extend(inner),
            other => result.push(other),
        }
    }
    match result.len() {
        0 => StructuredCfg::Sequence(vec![]),
        1 => result.remove(0),
        _ => StructuredCfg::Sequence(result),
    }
}

fn structure_from<N: CfgNode, D: Clone + CbranchInfo>(
    current: &N,
    stop_at: Option<&N>,
    cfg: &PcodeCfg<N, D>,
    dom: &DominatorTree<N>,
    pdom: &PostDominatorTree<N>,
    back_edges: &HashSet<(N, N)>,
) -> Result<StructuredCfg<N>, StructuringError> {
    if stop_at == Some(current) {
        return Ok(StructuredCfg::Sequence(vec![]));
    }

    let fwd_succs: Vec<N> = cfg
        .successors(current)
        .unwrap_or_default()
        .into_iter()
        .filter(|s| !back_edges.contains(&(current.clone(), (*s).clone())))
        .cloned()
        .collect();

    let latches: Vec<N> = cfg
        .predecessors(current)
        .unwrap_or_default()
        .into_iter()
        .filter(|p| dom.dominates(current, p))
        .cloned()
        .collect();

    if !latches.is_empty() {
        return structure_loop(current, stop_at, cfg, dom, pdom, back_edges, &latches);
    }

    match fwd_succs.len() {
        0 => Ok(StructuredCfg::Block(current.clone())),
        1 => {
            let rest = structure_from(&fwd_succs[0], stop_at, cfg, dom, pdom, back_edges)?;
            Ok(flatten(vec![StructuredCfg::Block(current.clone()), rest]))
        }
        2 => {
            let merge = pdom.immediate_dominator(current);
            let taken_idx = cfg
                .get_op_at(current)
                .and_then(|op| op.cbranch_input0())
                .and_then(|input0| {
                    current.concrete_location().and_then(|addr| {
                        let target =
                            crate::modeling::machine::cpu::concrete::ConcretePcodeAddress
                                ::resolve_from_varnode(input0, addr);
                        fwd_succs
                            .iter()
                            .position(|s| s.concrete_location() == Some(target))
                    })
                })
                .unwrap_or(0);
            let else_idx = 1 - taken_idx;
            let then_branch =
                structure_from(&fwd_succs[taken_idx], merge, cfg, dom, pdom, back_edges)?;
            let else_branch =
                structure_from(&fwd_succs[else_idx], merge, cfg, dom, pdom, back_edges)?;
            let rest = match merge {
                Some(m) => structure_from(m, stop_at, cfg, dom, pdom, back_edges)?,
                None => StructuredCfg::Sequence(vec![]),
            };
            Ok(flatten(vec![
                StructuredCfg::IfElse {
                    header: current.clone(),
                    then_branch: Box::new(then_branch),
                    else_branch: Box::new(else_branch),
                },
                rest,
            ]))
        }
        _ => Err(StructuringError::Irreducible),
    }
}

fn structure_loop<N: CfgNode, D: Clone + CbranchInfo>(
    header: &N,
    stop_at: Option<&N>,
    cfg: &PcodeCfg<N, D>,
    dom: &DominatorTree<N>,
    pdom: &PostDominatorTree<N>,
    outer_back_edges: &HashSet<(N, N)>,
    latches: &[N],
) -> Result<StructuredCfg<N>, StructuringError> {
    let body_nodes = natural_loop_body(header, latches, cfg, dom);

    let mut back_edges = outer_back_edges.clone();
    for latch in latches {
        back_edges.insert((latch.clone(), header.clone()));
    }

    // Collect all exits from the loop body: successors of any body node that are
    // outside the body. This handles loops where the exit edge comes from a latch
    // node rather than the header (e.g. A→B, B→C, C→B, C→D: exit is C→D).
    let mut loop_exit_set: HashSet<N> = HashSet::new();
    for body_node in &body_nodes {
        for s in cfg.successors(body_node).unwrap_or_default() {
            if !body_nodes.contains(s) {
                loop_exit_set.insert(s.clone());
            }
        }
    }
    if loop_exit_set.len() > 1 {
        return Err(StructuringError::Irreducible);
    }
    let loop_exit = loop_exit_set.into_iter().next();

    // Find which forward successor of the header enters the loop body.
    let header_fwd_succs: Vec<N> = cfg
        .successors(header)
        .unwrap_or_default()
        .into_iter()
        .filter(|s| !back_edges.contains(&(header.clone(), (*s).clone())))
        .cloned()
        .collect();

    let in_loop: Vec<N> = header_fwd_succs
        .iter()
        .filter(|s| body_nodes.contains(*s) && *s != header)
        .cloned()
        .collect();

    // For body structuring, stop at the loop exit node so that post-loop nodes
    // are not pulled into the body. Fall back to the header so the back-edge
    // target also acts as a natural stop (it's already filtered by back_edges).
    let body_stop: Option<&N> = loop_exit.as_ref().or(Some(header));

    let body_struct = match in_loop.len() {
        0 => StructuredCfg::Sequence(vec![]),
        1 => structure_from(&in_loop[0], body_stop, cfg, dom, pdom, &back_edges)?,
        _ => return Err(StructuringError::Irreducible),
    };

    let rest = match loop_exit {
        Some(ref exit) => structure_from(exit, stop_at, cfg, dom, pdom, outer_back_edges)?,
        None => StructuredCfg::Sequence(vec![]),
    };

    Ok(flatten(vec![
        StructuredCfg::Loop {
            header: header.clone(),
            body: Box::new(body_struct),
        },
        rest,
    ]))
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::analysis::cfg::PcodeCfg;
    use crate::analysis::cpa::lattice::pcode::PcodeAddressLattice;
    use crate::analysis::cpa::state::PcodeLocation;
    use crate::modeling::machine::cpu::concrete::ConcretePcodeAddress;

    fn n(m: u64) -> ConcretePcodeAddress {
        ConcretePcodeAddress::new(m, 0)
    }

    fn make_cfg(edges: &[(u64, u64)]) -> PcodeCfg<ConcretePcodeAddress, ()> {
        let mut cfg = PcodeCfg::new();
        for &(from, to) in edges {
            cfg.add_edge(n(from), n(to), ());
        }
        cfg
    }

    #[test]
    fn empty_returns_error() {
        let cfg: PcodeCfg<ConcretePcodeAddress, ()> = PcodeCfg::new();
        assert!(matches!(structure(cfg), Err(StructuringError::Empty)));
    }

    #[test]
    fn single_block() {
        let mut cfg: PcodeCfg<ConcretePcodeAddress, ()> = PcodeCfg::new();
        cfg.add_node(n(0));
        let result = structure(cfg).unwrap();
        assert!(matches!(result, StructuredCfg::Block(a) if a == n(0)));
    }

    #[test]
    fn linear_sequence() {
        // A→B→C
        let cfg = make_cfg(&[(0, 1), (1, 2)]);
        let result = structure(cfg).unwrap();
        let StructuredCfg::Sequence(items) = result else {
            panic!("expected Sequence, got {result:?}");
        };
        assert_eq!(items.len(), 3);
        assert!(matches!(&items[0], StructuredCfg::Block(a) if *a == n(0)));
        assert!(matches!(&items[1], StructuredCfg::Block(a) if *a == n(1)));
        assert!(matches!(&items[2], StructuredCfg::Block(a) if *a == n(2)));
    }

    #[test]
    fn diamond_if_else() {
        // A→B, A→C, B→D, C→D
        let cfg = make_cfg(&[(0, 1), (0, 2), (1, 3), (2, 3)]);
        let result = structure(cfg).unwrap();
        let StructuredCfg::Sequence(items) = result else {
            panic!("expected Sequence, got {result:?}");
        };
        assert_eq!(items.len(), 2);
        assert!(matches!(&items[0], StructuredCfg::IfElse { header, .. } if *header == n(0)));
        assert!(matches!(&items[1], StructuredCfg::Block(a) if *a == n(3)));
    }

    #[test]
    fn if_without_else() {
        // A→B, A→C, B→C  (B is the then-branch, C is the merge/else)
        let cfg = make_cfg(&[(0, 1), (0, 2), (1, 2)]);
        let result = structure(cfg).unwrap();
        let StructuredCfg::Sequence(items) = result else {
            panic!("expected Sequence, got {result:?}");
        };
        assert_eq!(items.len(), 2);
        assert!(matches!(&items[0], StructuredCfg::IfElse { header, .. } if *header == n(0)));
        assert!(matches!(&items[1], StructuredCfg::Block(a) if *a == n(2)));
    }

    #[test]
    fn natural_loop() {
        // A→B, B→C, C→B, C→D
        let cfg = make_cfg(&[(0, 1), (1, 2), (2, 1), (2, 3)]);
        let result = structure(cfg).unwrap();
        let StructuredCfg::Sequence(items) = result else {
            panic!("expected Sequence, got {result:?}");
        };
        assert_eq!(items.len(), 3);
        assert!(matches!(&items[0], StructuredCfg::Block(a) if *a == n(0)));
        assert!(matches!(&items[1], StructuredCfg::Loop { header, .. } if *header == n(1)));
        assert!(matches!(&items[2], StructuredCfg::Block(a) if *a == n(3)));

        let StructuredCfg::Loop { body, .. } = &items[1] else {
            panic!()
        };
        assert!(matches!(body.as_ref(), StructuredCfg::Block(a) if *a == n(2)));
    }

    #[test]
    fn while_loop() {
        // A→B, B→C, B→D, C→B
        let cfg = make_cfg(&[(0, 1), (1, 2), (1, 3), (2, 1)]);
        let result = structure(cfg).unwrap();
        let StructuredCfg::Sequence(items) = result else {
            panic!("expected Sequence, got {result:?}");
        };
        assert_eq!(items.len(), 3);
        assert!(matches!(&items[0], StructuredCfg::Block(a) if *a == n(0)));
        assert!(matches!(&items[1], StructuredCfg::Loop { header, .. } if *header == n(1)));
        assert!(matches!(&items[2], StructuredCfg::Block(a) if *a == n(3)));

        let StructuredCfg::Loop { body, .. } = &items[1] else {
            panic!()
        };
        assert!(matches!(body.as_ref(), StructuredCfg::Block(a) if *a == n(2)));
    }

    #[test]
    fn irreducible_fails() {
        // A→B, A→C, B→C, C→B
        let cfg = make_cfg(&[(0, 1), (0, 2), (1, 2), (2, 1)]);
        assert!(matches!(structure(cfg), Err(StructuringError::Irreducible)));
    }

    #[test]
    fn display_sequence() {
        let cfg = make_cfg(&[(0, 1)]);
        let result = structure(cfg).unwrap();
        assert_eq!(result.to_string(), "block(0:0)\nblock(1:0)\n");
    }

    #[test]
    fn cbranch_then_is_taken_branch() {
        // CBRANCH at n(0): input0 = absolute VarNode pointing to n(2) (taken),
        // fallthrough = n(1). Edges added in CPA order: taken first, then fallthrough.
        // With petgraph head-insertion, successors() returns [n(1), n(2)] (reversed),
        // so without the fix, then=n(1) (fallthrough, WRONG). With the fix, then=n(2).
        use jingle_sleigh::{PcodeOperation, VarNode};
        let cbranch_op = PcodeOperation::CBranch {
            input0: VarNode::new(2u64, 8u32, 1u32), // absolute addr 2 → n(2)
            input1: VarNode::new_const(1u64, 1u32),
        };
        let mut cfg: PcodeCfg<ConcretePcodeAddress, PcodeOperation> = PcodeCfg::new();
        cfg.add_edge(n(0), n(2), cbranch_op.clone()); // taken (added first)
        cfg.add_edge(n(0), n(1), cbranch_op.clone()); // fallthrough (added second)
        cfg.add_edge(n(1), n(3), cbranch_op.clone());
        cfg.add_edge(n(2), n(3), cbranch_op.clone());
        let result = structure(cfg).unwrap();
        let StructuredCfg::Sequence(items) = result else {
            panic!("{result:?}")
        };
        let StructuredCfg::IfElse {
            then_branch,
            else_branch,
            ..
        } = &items[0]
        else {
            panic!("expected IfElse, got {:?}", items[0]);
        };
        assert!(
            matches!(then_branch.as_ref(), StructuredCfg::Block(a) if *a == n(2)),
            "then_branch should be taken (n(2)), got {then_branch:?}"
        );
        assert!(
            matches!(else_branch.as_ref(), StructuredCfg::Block(a) if *a == n(1)),
            "else_branch should be fallthrough (n(1)), got {else_branch:?}"
        );
    }

    #[test]
    fn display_if_else() {
        // A→B, A→C, B→D, C→D
        let cfg = make_cfg(&[(0, 1), (0, 2), (1, 3), (2, 3)]);
        let result = structure(cfg).unwrap();
        let s = result.to_string();
        assert!(s.contains("if 0:0 {"), "missing if header: {s}");
        assert!(s.contains("  block(1:0)"), "missing then-branch: {s}");
        assert!(s.contains("} else {"), "missing else: {s}");
        assert!(s.contains("  block(2:0)"), "missing else-branch: {s}");
        assert!(s.contains("block(3:0)"), "missing merge: {s}");
    }

    #[test]
    fn display_loop() {
        // A→B, B→C, C→B, C→D
        let cfg = make_cfg(&[(0, 1), (1, 2), (2, 1), (2, 3)]);
        let result = structure(cfg).unwrap();
        let s = result.to_string();
        assert!(s.contains("loop 1:0 {"), "missing loop header: {s}");
        assert!(s.contains("  block(2:0)"), "missing loop body: {s}");
        assert!(s.contains("block(3:0)"), "missing post-loop: {s}");
    }

    /// A node type that can represent either a concrete pcode location or a symbolic one.
    /// Used to test that `structure()` prunes symbolic leaf nodes before running.
    #[derive(Clone, Debug, Hash, PartialEq, Eq)]
    enum MaybeSymbolicNode {
        Concrete(ConcretePcodeAddress),
        Symbolic,
    }

    impl PcodeLocation for MaybeSymbolicNode {
        fn location(&self) -> PcodeAddressLattice {
            match self {
                MaybeSymbolicNode::Concrete(addr) => PcodeAddressLattice::Const(*addr),
                MaybeSymbolicNode::Symbolic => PcodeAddressLattice::Top,
            }
        }
    }

    #[test]
    fn symbolic_leaf_is_pruned() {
        // Concrete node A with one successor: a symbolic leaf.
        // Without pruning the structurer sees a Sequence [A, leaf].
        // With pruning it should see just Block(A).
        let concrete = MaybeSymbolicNode::Concrete(ConcretePcodeAddress::new(0, 0));
        let symbolic = MaybeSymbolicNode::Symbolic;
        let mut cfg: PcodeCfg<MaybeSymbolicNode, ()> = PcodeCfg::new();
        cfg.add_edge(concrete.clone(), symbolic, ());
        let result = structure(cfg).unwrap();
        assert!(
            matches!(result, StructuredCfg::Block(ref n) if *n == concrete),
            "expected Block(concrete), got {result:?}"
        );
    }

    #[test]
    fn if_inside_loop() {
        // 0→1 (entry), 1→2 (loop header to if-cond), 1→6 (loop exit),
        // 2→3, 2→4 (if-else branches), 3→5, 4→5 (merge), 5→1 (latch)
        let cfg = make_cfg(&[
            (0, 1),
            (1, 2),
            (1, 6),
            (2, 3),
            (2, 4),
            (3, 5),
            (4, 5),
            (5, 1),
        ]);
        let result = structure(cfg).unwrap();

        let StructuredCfg::Sequence(items) = result else {
            panic!("expected Sequence, got {result:?}");
        };
        assert_eq!(items.len(), 3);
        assert!(matches!(&items[0], StructuredCfg::Block(a) if *a == n(0)));
        assert!(matches!(&items[2], StructuredCfg::Block(a) if *a == n(6)));

        let StructuredCfg::Loop { header, body } = &items[1] else {
            panic!("expected Loop at items[1], got {:?}", items[1]);
        };
        assert_eq!(*header, n(1));

        let StructuredCfg::Sequence(body_items) = body.as_ref() else {
            panic!("expected Sequence in loop body, got {body:?}");
        };
        assert_eq!(body_items.len(), 2);
        assert!(matches!(&body_items[0], StructuredCfg::IfElse { header, .. } if *header == n(2)));
        assert!(matches!(&body_items[1], StructuredCfg::Block(a) if *a == n(5)));
    }
}