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
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
use std::collections::{HashMap, HashSet, VecDeque};

use serde::{Deserialize, Serialize};

use super::ir::*;

/// Lattice value for constant propagation.
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub enum ConstLattice {
    /// Not yet analyzed (optimistic top).
    Top,
    /// Known string constant.
    Str(String),
    /// Known integer constant.
    Int(i64),
    /// Known boolean constant.
    Bool(bool),
    /// Null / nil / None.
    Null,
    /// Multiple possible values, not constant.
    Varying,
}

impl ConstLattice {
    /// Meet operation: combine two lattice values.
    fn meet(&self, other: &Self) -> Self {
        match (self, other) {
            (ConstLattice::Top, x) | (x, ConstLattice::Top) => x.clone(),
            (ConstLattice::Varying, _) | (_, ConstLattice::Varying) => ConstLattice::Varying,
            (a, b) if a == b => a.clone(),
            _ => ConstLattice::Varying,
        }
    }

    /// Parse a raw constant text into a typed lattice value.
    pub(crate) fn parse(text: &str) -> Self {
        let trimmed = text.trim();

        // Boolean
        if trimmed == "true" || trimmed == "True" || trimmed == "TRUE" {
            return ConstLattice::Bool(true);
        }
        if trimmed == "false" || trimmed == "False" || trimmed == "FALSE" {
            return ConstLattice::Bool(false);
        }

        // Null variants
        if trimmed == "null"
            || trimmed == "nil"
            || trimmed == "None"
            || trimmed == "NULL"
            || trimmed == "nullptr"
        {
            return ConstLattice::Null;
        }

        // Integer (including negative)
        if let Ok(i) = trimmed.parse::<i64>() {
            return ConstLattice::Int(i);
        }

        // String: strip surrounding quotes. Require len >= 2 so a lone `'`
        // or `"` (where starts_with and ends_with both match the same byte)
        // does not produce an empty `[1..0]` slice and panic.
        if trimmed.len() >= 2
            && ((trimmed.starts_with('"') && trimmed.ends_with('"'))
                || (trimmed.starts_with('\'') && trimmed.ends_with('\'')))
        {
            let inner = &trimmed[1..trimmed.len() - 1];
            return ConstLattice::Str(inner.to_string());
        }

        // Bare string (no quotes), treat as string constant
        ConstLattice::Str(trimmed.to_string())
    }

    /// Returns the boolean value if this is a known Bool.
    pub fn as_bool(&self) -> Option<bool> {
        match self {
            ConstLattice::Bool(b) => Some(*b),
            // Truthiness: null is false, 0 is false, empty string is false
            ConstLattice::Null => Some(false),
            ConstLattice::Int(0) => Some(false),
            ConstLattice::Str(s) if s.is_empty() => Some(false),
            _ => None,
        }
    }
}

/// Result of constant propagation analysis.
pub struct ConstPropResult {
    /// Per-SSA-value constant lattice.
    pub values: HashMap<SsaValue, ConstLattice>,
    /// Blocks that are statically unreachable.
    pub unreachable_blocks: HashSet<BlockId>,
}

/// Run Sparse Conditional Constant Propagation on an SSA body.
pub fn const_propagate(body: &SsaBody) -> ConstPropResult {
    let num_blocks = body.blocks.len();

    // Per-value lattice: starts at Top
    let mut values: HashMap<SsaValue, ConstLattice> = HashMap::new();

    // Executable flags per CFG edge (from_block, to_block)
    let mut executable_edges: HashSet<(BlockId, BlockId)> = HashSet::new();
    // Executable blocks
    let mut executable_blocks: HashSet<BlockId> = HashSet::new();

    // Two worklists
    let mut cfg_worklist: VecDeque<BlockId> = VecDeque::new();
    let mut ssa_worklist: VecDeque<SsaValue> = VecDeque::new();

    // Mark entry executable
    executable_blocks.insert(body.entry);
    cfg_worklist.push_back(body.entry);

    // Build use-map: SsaValue → list of (BlockId, instruction index in block)
    // so we can propagate SSA value changes efficiently.
    let mut use_sites: HashMap<SsaValue, Vec<BlockId>> = HashMap::new();
    for block in &body.blocks {
        for inst in block.phis.iter().chain(block.body.iter()) {
            for used_val in inst_uses(inst) {
                use_sites.entry(used_val).or_default().push(block.id);
            }
        }
    }

    // Initialize all values to Top
    for block in &body.blocks {
        for inst in block.phis.iter().chain(block.body.iter()) {
            values.insert(inst.value, ConstLattice::Top);
        }
    }

    // Process until both worklists are empty
    loop {
        let mut changed = false;

        // Process CFG worklist
        while let Some(block_id) = cfg_worklist.pop_front() {
            let block = body.block(block_id);

            // Evaluate phis
            for phi in &block.phis {
                if let SsaOp::Phi(operands) = &phi.op {
                    let old = values.get(&phi.value).cloned().unwrap_or(ConstLattice::Top);
                    let new_val = eval_phi(operands, &values, &executable_edges, block_id);
                    if new_val != old {
                        values.insert(phi.value, new_val);
                        ssa_worklist.push_back(phi.value);
                        changed = true;
                    }
                }
            }

            // Evaluate body instructions
            for inst in &block.body {
                let old = values
                    .get(&inst.value)
                    .cloned()
                    .unwrap_or(ConstLattice::Top);
                let new_val = eval_inst(inst, &values);
                if new_val != old {
                    values.insert(inst.value, new_val);
                    ssa_worklist.push_back(inst.value);
                    changed = true;
                }
            }

            // Process terminator: determine which successors are executable
            process_terminator(
                block,
                body,
                &values,
                &mut executable_edges,
                &mut executable_blocks,
                &mut cfg_worklist,
            );
        }

        // Process SSA worklist
        while let Some(val) = ssa_worklist.pop_front() {
            if let Some(blocks) = use_sites.get(&val) {
                for &block_id in blocks {
                    if !executable_blocks.contains(&block_id) {
                        continue;
                    }
                    let block = body.block(block_id);

                    // Re-evaluate phis using this value
                    for phi in &block.phis {
                        if let SsaOp::Phi(operands) = &phi.op
                            && operands.iter().any(|(_, v)| *v == val)
                        {
                            let old = values.get(&phi.value).cloned().unwrap_or(ConstLattice::Top);
                            let new_val = eval_phi(operands, &values, &executable_edges, block_id);
                            if new_val != old {
                                values.insert(phi.value, new_val);
                                ssa_worklist.push_back(phi.value);
                                changed = true;
                            }
                        }
                    }

                    // Re-evaluate body instructions using this value
                    for inst in &block.body {
                        if inst_uses(inst).contains(&val) {
                            let old = values
                                .get(&inst.value)
                                .cloned()
                                .unwrap_or(ConstLattice::Top);
                            let new_val = eval_inst(inst, &values);
                            if new_val != old {
                                values.insert(inst.value, new_val);
                                ssa_worklist.push_back(inst.value);
                                changed = true;
                            }
                        }
                    }

                    // Re-evaluate terminator if condition changed
                    process_terminator(
                        block,
                        body,
                        &values,
                        &mut executable_edges,
                        &mut executable_blocks,
                        &mut cfg_worklist,
                    );
                }
            }
        }

        if !changed {
            break;
        }
    }

    // Compute unreachable blocks
    let unreachable_blocks: HashSet<BlockId> = (0..num_blocks)
        .map(|i| BlockId(i as u32))
        .filter(|bid| !executable_blocks.contains(bid))
        .collect();

    ConstPropResult {
        values,
        unreachable_blocks,
    }
}

/// Evaluate a phi: meet of operands from executable predecessors.
fn eval_phi(
    operands: &[(BlockId, SsaValue)],
    values: &HashMap<SsaValue, ConstLattice>,
    executable_edges: &HashSet<(BlockId, BlockId)>,
    this_block: BlockId,
) -> ConstLattice {
    let mut result = ConstLattice::Top;
    for (pred_block, val) in operands {
        if !executable_edges.contains(&(*pred_block, this_block)) {
            continue; // skip non-executable predecessors
        }
        let operand_val = values.get(val).cloned().unwrap_or(ConstLattice::Top);
        result = result.meet(&operand_val);
    }
    result
}

/// Evaluate a single instruction.
fn eval_inst(inst: &SsaInst, values: &HashMap<SsaValue, ConstLattice>) -> ConstLattice {
    match &inst.op {
        SsaOp::Const(Some(text)) => ConstLattice::parse(text),
        SsaOp::Const(None) => ConstLattice::Varying, // unknown constant
        SsaOp::Assign(uses) if uses.len() == 1 => {
            // Copy: propagate the source's value
            values.get(&uses[0]).cloned().unwrap_or(ConstLattice::Top)
        }
        SsaOp::Assign(_) => ConstLattice::Varying, // expression with multiple uses
        SsaOp::Call { .. }
        | SsaOp::Source
        | SsaOp::Param { .. }
        | SsaOp::SelfParam
        | SsaOp::CatchParam => ConstLattice::Varying,
        // FieldProj: projecting a field is dynamic with respect to the
        // const-propagation lattice, there is no general way to fold
        // `obj.field` to a known scalar at this phase.  Returning Varying
        // matches Call: callers needing field-level constness will go
        // through the points-to / heap analysis.
        SsaOp::FieldProj { .. } => ConstLattice::Varying,
        SsaOp::Phi(_) => ConstLattice::Varying, // phis in body shouldn't happen
        SsaOp::Nop => ConstLattice::Varying,
        // Undef contributes no knowledge: `Top` is the lattice identity
        // for meet, so a phi operand of Undef leaves the joined value
        // to the other incoming operands.
        SsaOp::Undef => ConstLattice::Top,
    }
}

/// Collect SSA values used by an instruction (for use-map building).
fn inst_uses(inst: &SsaInst) -> Vec<SsaValue> {
    match &inst.op {
        SsaOp::Phi(operands) => operands.iter().map(|(_, v)| *v).collect(),
        SsaOp::Assign(uses) => uses.to_vec(),
        SsaOp::Call { args, receiver, .. } => {
            let mut vals = Vec::new();
            if let Some(rv) = receiver {
                vals.push(*rv);
            }
            for arg in args {
                vals.extend(arg.iter());
            }
            vals
        }
        SsaOp::FieldProj { receiver, .. } => vec![*receiver],
        SsaOp::Source
        | SsaOp::Const(_)
        | SsaOp::Param { .. }
        | SsaOp::SelfParam
        | SsaOp::CatchParam
        | SsaOp::Nop
        | SsaOp::Undef => Vec::new(),
    }
}

/// Process a block's terminator to determine successor executability.
fn process_terminator(
    block: &SsaBlock,
    body: &SsaBody,
    values: &HashMap<SsaValue, ConstLattice>,
    executable_edges: &mut HashSet<(BlockId, BlockId)>,
    executable_blocks: &mut HashSet<BlockId>,
    cfg_worklist: &mut VecDeque<BlockId>,
) {
    match &block.terminator {
        Terminator::Goto(_) => {
            // `block.succs` is authoritative. For collapsed ≥3-way fanouts
            // (see src/ssa/lower.rs `three_successor_collapse`) the terminator
            // only records the first successor; marking just that one would
            // leave the others unreachable for SCCP. Iterate succs so every
            // CFG successor is marked executable.
            for &target in &block.succs {
                mark_edge_executable(
                    block.id,
                    target,
                    executable_edges,
                    executable_blocks,
                    cfg_worklist,
                );
            }
        }
        Terminator::Branch {
            cond,
            true_blk,
            false_blk,
            condition: _,
        } => {
            // Try to resolve the condition to a known boolean
            let cond_val = body
                .cfg_node_map
                .get(cond)
                .and_then(|v| values.get(v))
                .and_then(|c| c.as_bool());

            match cond_val {
                Some(true) => {
                    mark_edge_executable(
                        block.id,
                        *true_blk,
                        executable_edges,
                        executable_blocks,
                        cfg_worklist,
                    );
                }
                Some(false) => {
                    mark_edge_executable(
                        block.id,
                        *false_blk,
                        executable_edges,
                        executable_blocks,
                        cfg_worklist,
                    );
                }
                None => {
                    // Unknown: both successors executable
                    mark_edge_executable(
                        block.id,
                        *true_blk,
                        executable_edges,
                        executable_blocks,
                        cfg_worklist,
                    );
                    mark_edge_executable(
                        block.id,
                        *false_blk,
                        executable_edges,
                        executable_blocks,
                        cfg_worklist,
                    );
                }
            }
        }
        Terminator::Switch {
            scrutinee,
            targets,
            default,
            case_values,
        } => {
            // Try to resolve scrutinee to a concrete integer literal; if
            // we can match it against one of the case literals (not
            // currently available on the SSA IR), mark just that target.
            // Until per-case literals are threaded through, fall back to
            // the sound "any successor executable" behavior, which mirrors
            // the pre-Switch cascade.
            let _ = (scrutinee, targets, default, case_values);
            for &target in &block.succs {
                mark_edge_executable(
                    block.id,
                    target,
                    executable_edges,
                    executable_blocks,
                    cfg_worklist,
                );
            }
        }
        Terminator::Return(_) | Terminator::Unreachable => {
            // `block.succs` is authoritative; the terminator is advisory.
            // Finally/cleanup continuation edges live on `succs` even when
            // the structured terminator is `Return`/`Unreachable`. Mark them
            // executable so SCCP reaches downstream (e.g. finally) blocks.
            for &target in &block.succs {
                mark_edge_executable(
                    block.id,
                    target,
                    executable_edges,
                    executable_blocks,
                    cfg_worklist,
                );
            }
        }
    }
}

fn mark_edge_executable(
    from: BlockId,
    to: BlockId,
    executable_edges: &mut HashSet<(BlockId, BlockId)>,
    executable_blocks: &mut HashSet<BlockId>,
    cfg_worklist: &mut VecDeque<BlockId>,
) {
    if executable_edges.insert((from, to)) {
        if executable_blocks.insert(to) {
            cfg_worklist.push_back(to);
        } else {
            // Block already executable but new edge, re-evaluate phis
            cfg_worklist.push_back(to);
        }
    }
}

/// Apply constant propagation results: prune branches where condition is known constant.
///
/// Returns the number of branches pruned.
pub fn apply_const_prop(body: &mut SsaBody, result: &ConstPropResult) -> usize {
    // Collect pruning decisions first to avoid borrow conflicts.
    // Each entry: (block_index, taken_block, untaken_block)
    let mut prune_ops: Vec<(usize, BlockId, BlockId)> = Vec::new();

    for (block_idx, block) in body.blocks.iter().enumerate() {
        if let Terminator::Branch {
            cond,
            true_blk,
            false_blk,
            condition: _,
        } = &block.terminator
        {
            let cond_val = body
                .cfg_node_map
                .get(cond)
                .and_then(|v| result.values.get(v))
                .and_then(|c| c.as_bool());

            match cond_val {
                Some(true) => {
                    prune_ops.push((block_idx, *true_blk, *false_blk));
                }
                Some(false) => {
                    prune_ops.push((block_idx, *false_blk, *true_blk));
                }
                None => {}
            }
        }
    }

    let pruned = prune_ops.len();

    // Apply pruning
    for (block_idx, taken, untaken) in prune_ops {
        let pred_id = body.blocks[block_idx].id;
        body.blocks[block_idx].terminator = Terminator::Goto(taken);

        // Remove pred from untaken's preds
        let untaken_idx = untaken.0 as usize;
        if untaken_idx < body.blocks.len() {
            body.blocks[untaken_idx].preds.retain(|p| *p != pred_id);
            // Remove phi operands referencing this pred
            for phi in &mut body.blocks[untaken_idx].phis {
                if let SsaOp::Phi(operands) = &mut phi.op {
                    operands.retain(|(bid, _)| *bid != pred_id);
                }
            }
        }

        // Remove untaken from pred's succs
        body.blocks[block_idx].succs.retain(|s| *s != untaken);
    }

    // Mark unreachable blocks
    for &bid in &result.unreachable_blocks {
        body.block_mut(bid).terminator = Terminator::Unreachable;
    }

    pruned
}

/// Collect module aliases from `require()` calls in the SSA body.
///
/// Detects patterns like `const http = require("http")` and propagates
/// aliases through phi nodes (e.g., `const lib = cond ? https : http`).
/// Returns a map from SSA value → set of possible module names.
///
/// Only tracks known HTTP-related modules to avoid false positives.
pub fn collect_module_aliases(
    body: &SsaBody,
    const_values: &HashMap<SsaValue, ConstLattice>,
) -> HashMap<SsaValue, smallvec::SmallVec<[String; 2]>> {
    use smallvec::SmallVec;

    // Known modules whose methods are security-relevant for alias tracking.
    const KNOWN_MODULES: &[&str] = &["http", "https", "child_process", "fs", "net", "dgram"];

    let mut aliases: HashMap<SsaValue, SmallVec<[String; 2]>> = HashMap::new();

    // Pass 1: detect `require("module")` calls.
    for block in &body.blocks {
        for inst in &block.body {
            if let SsaOp::Call { callee, args, .. } = &inst.op
                && (callee == "require" || callee.ends_with(".require"))
            {
                // Check if the first argument is a known module string constant.
                if let Some(first_arg) = args.first()
                    && let Some(&first_val) = first_arg.first()
                    && let Some(ConstLattice::Str(module_name)) = const_values.get(&first_val)
                    && KNOWN_MODULES.contains(&module_name.as_str())
                {
                    aliases
                        .entry(inst.value)
                        .or_default()
                        .push(module_name.clone());
                }
            }
        }
    }

    if aliases.is_empty() {
        return aliases;
    }

    // Pass 2: propagate through copies (single-use Assign) and phi nodes.
    let mut changed = true;
    let mut iterations = 0;
    while changed && iterations < 10 {
        changed = false;
        iterations += 1;
        for block in &body.blocks {
            // Phi nodes
            for phi in &block.phis {
                if let SsaOp::Phi(operands) = &phi.op {
                    let mut merged: SmallVec<[String; 2]> = SmallVec::new();
                    for (_, operand_val) in operands {
                        if let Some(operand_aliases) = aliases.get(operand_val) {
                            for a in operand_aliases {
                                if !merged.contains(a) {
                                    merged.push(a.clone());
                                }
                            }
                        }
                    }
                    if !merged.is_empty() {
                        let entry = aliases.entry(phi.value).or_default();
                        for a in &merged {
                            if !entry.contains(a) {
                                entry.push(a.clone());
                                changed = true;
                            }
                        }
                    }
                }
            }
            // Copy propagation through single-use Assign
            for inst in &block.body {
                if let SsaOp::Assign(uses) = &inst.op
                    && uses.len() == 1
                    && let Some(src_aliases) = aliases.get(&uses[0]).cloned()
                {
                    let entry = aliases.entry(inst.value).or_default();
                    for a in &src_aliases {
                        if !entry.contains(a) {
                            entry.push(a.clone());
                            changed = true;
                        }
                    }
                }
            }
        }
    }

    aliases
}

#[cfg(test)]
mod tests {
    use super::*;
    use petgraph::graph::NodeIndex;
    use smallvec::SmallVec;

    fn make_body(blocks: Vec<SsaBlock>, value_defs: Vec<ValueDef>) -> SsaBody {
        let cfg_node_map = value_defs
            .iter()
            .enumerate()
            .map(|(i, vd)| (vd.cfg_node, SsaValue(i as u32)))
            .collect();
        SsaBody {
            blocks,
            entry: BlockId(0),
            value_defs,
            cfg_node_map,
            exception_edges: Vec::new(),
            field_interner: crate::ssa::ir::FieldInterner::default(),
            field_writes: std::collections::HashMap::new(),

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

    #[test]
    fn const_literal_parsed() {
        assert_eq!(ConstLattice::parse("42"), ConstLattice::Int(42));
        assert_eq!(ConstLattice::parse("-1"), ConstLattice::Int(-1));
        assert_eq!(ConstLattice::parse("true"), ConstLattice::Bool(true));
        assert_eq!(ConstLattice::parse("false"), ConstLattice::Bool(false));
        assert_eq!(ConstLattice::parse("null"), ConstLattice::Null);
        assert_eq!(ConstLattice::parse("nil"), ConstLattice::Null);
        assert_eq!(
            ConstLattice::parse("\"hello\""),
            ConstLattice::Str("hello".into())
        );
        assert_eq!(
            ConstLattice::parse("'world'"),
            ConstLattice::Str("world".into())
        );
    }

    #[test]
    fn meet_lattice() {
        let a = ConstLattice::Int(42);
        let b = ConstLattice::Int(42);
        assert_eq!(a.meet(&b), ConstLattice::Int(42));

        let c = ConstLattice::Int(99);
        assert_eq!(a.meet(&c), ConstLattice::Varying);

        assert_eq!(ConstLattice::Top.meet(&a), ConstLattice::Int(42));
        assert_eq!(a.meet(&ConstLattice::Top), ConstLattice::Int(42));

        assert_eq!(ConstLattice::Varying.meet(&a), ConstLattice::Varying);
    }

    #[test]
    fn single_block_const() {
        // v0 = const("42")
        let n0 = NodeIndex::new(0);
        let block = 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(),
        };
        let body = make_body(
            vec![block],
            vec![ValueDef {
                var_name: Some("x".into()),
                cfg_node: n0,
                block: BlockId(0),
            }],
        );

        let result = const_propagate(&body);
        assert_eq!(
            result.values.get(&SsaValue(0)),
            Some(&ConstLattice::Int(42))
        );
        assert!(result.unreachable_blocks.is_empty());
    }

    #[test]
    fn copy_propagation_through_assign() {
        // v0 = const("true"), v1 = assign(v0)
        let n0 = NodeIndex::new(0);
        let n1 = NodeIndex::new(1);
        let block = SsaBlock {
            id: BlockId(0),
            phis: vec![],
            body: vec![
                SsaInst {
                    value: SsaValue(0),
                    op: SsaOp::Const(Some("true".into())),
                    cfg_node: n0,
                    var_name: Some("x".into()),
                    span: (0, 4),
                },
                SsaInst {
                    value: SsaValue(1),
                    op: SsaOp::Assign(SmallVec::from_elem(SsaValue(0), 1)),
                    cfg_node: n1,
                    var_name: Some("y".into()),
                    span: (5, 9),
                },
            ],
            terminator: Terminator::Return(None),
            preds: SmallVec::new(),
            succs: SmallVec::new(),
        };
        let body = make_body(
            vec![block],
            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),
                },
            ],
        );

        let result = const_propagate(&body);
        assert_eq!(
            result.values.get(&SsaValue(0)),
            Some(&ConstLattice::Bool(true))
        );
        assert_eq!(
            result.values.get(&SsaValue(1)),
            Some(&ConstLattice::Bool(true))
        );
    }

    /// Meet must be commutative: `a ⊓ b == b ⊓ a` for every pair of
    /// lattice values. Iterates a representative cross product; failure
    /// would indicate the implementation special-cased one operand.
    #[test]
    fn meet_lattice_is_commutative() {
        let vals = [
            ConstLattice::Top,
            ConstLattice::Varying,
            ConstLattice::Null,
            ConstLattice::Int(0),
            ConstLattice::Int(42),
            ConstLattice::Bool(true),
            ConstLattice::Bool(false),
            ConstLattice::Str("a".into()),
            ConstLattice::Str("b".into()),
        ];
        for a in &vals {
            for b in &vals {
                assert_eq!(
                    a.meet(b),
                    b.meet(a),
                    "meet should be commutative for ({a:?}, {b:?})"
                );
            }
        }
    }

    /// Meet must be associative: `(a ⊓ b) ⊓ c == a ⊓ (b ⊓ c)`.
    #[test]
    fn meet_lattice_is_associative() {
        let vals = [
            ConstLattice::Top,
            ConstLattice::Varying,
            ConstLattice::Null,
            ConstLattice::Int(0),
            ConstLattice::Int(42),
            ConstLattice::Bool(true),
            ConstLattice::Str("x".into()),
        ];
        for a in &vals {
            for b in &vals {
                for c in &vals {
                    let lhs = a.meet(b).meet(c);
                    let rhs = a.meet(&b.meet(c));
                    assert_eq!(lhs, rhs, "associativity broken on ({a:?},{b:?},{c:?})");
                }
            }
        }
    }

    /// Meet must be idempotent: `a ⊓ a == a` for every lattice value.
    #[test]
    fn meet_lattice_is_idempotent() {
        let vals = [
            ConstLattice::Top,
            ConstLattice::Varying,
            ConstLattice::Null,
            ConstLattice::Int(7),
            ConstLattice::Bool(false),
            ConstLattice::Str("y".into()),
        ];
        for a in &vals {
            assert_eq!(a.meet(a), a.clone(), "idempotence broken on {a:?}");
        }
    }

    /// Top is the meet identity: `Top ⊓ x == x` for every value.
    /// Varying is meet-absorbing: `Varying ⊓ x == Varying`.
    /// Two distinct concrete values meet to Varying.
    #[test]
    fn meet_lattice_extremes() {
        let xs = [
            ConstLattice::Null,
            ConstLattice::Int(1),
            ConstLattice::Bool(true),
            ConstLattice::Str("a".into()),
        ];
        for x in &xs {
            assert_eq!(ConstLattice::Top.meet(x), x.clone());
            assert_eq!(x.meet(&ConstLattice::Top), x.clone());
            assert_eq!(ConstLattice::Varying.meet(x), ConstLattice::Varying);
            assert_eq!(x.meet(&ConstLattice::Varying), ConstLattice::Varying);
        }
        assert_eq!(
            ConstLattice::Int(1).meet(&ConstLattice::Int(2)),
            ConstLattice::Varying
        );
        assert_eq!(
            ConstLattice::Bool(true).meet(&ConstLattice::Bool(false)),
            ConstLattice::Varying
        );
        assert_eq!(
            ConstLattice::Str("a".into()).meet(&ConstLattice::Str("b".into())),
            ConstLattice::Varying
        );
    }

    /// Const parsing must round-trip integer signs. i64::MIN/MAX must
    /// parse without overflow; arbitrary text falls back to a bare-string
    /// const (current contract, tested here so a future change is
    /// caught explicitly).
    #[test]
    fn const_parse_extremes_and_fallback() {
        assert_eq!(
            ConstLattice::parse(&i64::MAX.to_string()),
            ConstLattice::Int(i64::MAX)
        );
        assert_eq!(
            ConstLattice::parse(&i64::MIN.to_string()),
            ConstLattice::Int(i64::MIN)
        );
        // Larger than i64 falls back to bare-string.
        let huge = "99999999999999999999";
        assert_eq!(
            ConstLattice::parse(huge),
            ConstLattice::Str(huge.to_string())
        );
        // Empty string parses as empty Str (not panic).
        assert_eq!(ConstLattice::parse(""), ConstLattice::Str("".into()));
        // Lone quote characters must not panic in the quote-stripping path
        // (regression for fuzz crash-2f943c14: `'` triggered &s[1..0]).
        assert_eq!(ConstLattice::parse("'"), ConstLattice::Str("'".into()));
        assert_eq!(ConstLattice::parse("\""), ConstLattice::Str("\"".into()));
    }
}