nyx-scanner 0.6.0

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
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
//! Field-sensitive Steensgaard points-to analysis driver.
//!
//! Flow-insensitive union-find over SSA values; field sensitivity comes
//! from representing each `obj.f` access as a structural
//! [`AbsLoc::Field`] keyed by `(parent_loc, field)`.

use std::collections::HashMap;

use crate::cfg::BodyId;
use crate::ssa::ir::{FieldId, SsaBody, SsaInst, SsaOp, SsaValue};

use super::domain::{AbsLoc, LOC_TOP, LocId, LocInterner, PointsToSet, PtrProxyHint};

/// Maximum constraint-solver iterations before bailing.  Each pass
/// walks every instruction once; in practice the analysis converges
/// in a small number of passes for any well-formed body.
const MAX_FIXPOINT_ITERS: usize = 8;

/// Container-read callees that pull a single element out of a
/// collection without a key.  Cross-language; non-listed callees still
/// get fresh-alloc behaviour, so the list is conservative.
fn is_container_read_callee(callee: &str) -> bool {
    let bare = match callee.rsplit_once('.') {
        Some((_, m)) => m,
        None => callee,
    };
    matches!(
        bare,
        "shift"
            | "pop"
            | "peek"
            | "front"
            | "back"
            | "first"
            | "last"
            | "head"
            | "tail"
            | "dequeue"
            | "remove"
            | "popleft"
            // synthetic callee for subscript reads (`arr[i]`, `map[k]`)
            | "__index_get__"
    )
}

/// Container-write callees, mirror of `is_container_read_callee`.
pub fn is_container_write_callee(callee: &str) -> bool {
    let bare = match callee.rsplit_once('.') {
        Some((_, m)) => m,
        None => callee,
    };
    matches!(
        bare,
        "push"
            | "pushback"
            | "push_back"
            | "pushfront"
            | "push_front"
            | "append"
            | "add"
            | "insert"
            | "enqueue"
            | "unshift"
            // synthetic callee for subscript writes (`arr[i] = v`, `map[k] = v`)
            | "__index_set__"
    )
}

/// Public re-export of `is_container_read_callee` for the taint engine.
pub fn is_container_read_callee_pub(callee: &str) -> bool {
    is_container_read_callee(callee)
}

/// Derive a [`crate::summary::points_to::FieldPointsToSummary`] from
/// per-body points-to facts.
///
/// Records two channels:
///
/// 1. **Reads**, walks every [`SsaOp::FieldProj`] in the body; for
///    each `loc ∈ pt(receiver)` that resolves to a parameter
///    location ([`AbsLoc::Param`] / [`AbsLoc::SelfParam`]), records
///    the projected field name into the summary's
///    `param_field_reads`.
/// 2. **Writes**, walks the body's [`SsaBody::field_writes`] side-
///    table (populated by SSA lowering's W1 synth-Assign hook) and
///    records each `(receiver, FieldId)` pair against the receiver's
///    pt set the same way reads are recorded.
///
/// Field name resolution goes through the body's
/// [`SsaBody::field_interner`] because [`crate::ssa::ir::FieldId`]
/// is body-local, names are the only stable cross-file identity.
///
/// Receiver (`SelfParam`) reads/writes are recorded under the
/// [`u32::MAX`] sentinel parameter index, mirroring the convention in
/// `SsaFuncSummary::receiver_to_*` fields.
///
/// The container-element sentinel field [`FieldId::ELEM`] is recorded
/// under the special name `"<elem>"` so callers can recognise the
/// abstract-element flow without leaking the implementation detail
/// of the sentinel `u32::MAX` value across the wire.
pub fn extract_field_points_to(
    body: &SsaBody,
    facts: &PointsToFacts,
) -> crate::summary::points_to::FieldPointsToSummary {
    use crate::summary::points_to::FieldPointsToSummary;
    let mut out = FieldPointsToSummary::empty();
    if body.field_interner.is_empty() && body.field_writes.is_empty() {
        return out;
    }
    // Resolve a body-local FieldId to its cross-wire-stable name.
    // Returns `None` when the id is out of range (deserialised body
    // with a fresh interner) or doesn't correspond to a real field.
    let field_name = |field: FieldId| -> Option<String> {
        if field == FieldId::ELEM {
            Some("<elem>".to_string())
        } else if (field.0 as usize) < body.field_interner.len() {
            Some(body.field_interner.resolve(field).to_string())
        } else {
            None
        }
    };
    // Apply a single read or write to the summary, dispatching on
    // the abstract location's parameter / receiver shape.
    let record =
        |loc: LocId, name: &str, out: &mut FieldPointsToSummary, is_write: bool| match facts
            .interner
            .resolve(loc)
        {
            crate::pointer::AbsLoc::Param(_, idx) => {
                if is_write {
                    out.add_write(*idx as u32, name);
                } else {
                    out.add_read(*idx as u32, name);
                }
            }
            crate::pointer::AbsLoc::SelfParam(_) => {
                if is_write {
                    out.add_write(u32::MAX, name);
                } else {
                    out.add_read(u32::MAX, name);
                }
            }
            _ => {}
        };

    // Channel 1: reads from FieldProj.
    for block in &body.blocks {
        for inst in block.body.iter() {
            if let SsaOp::FieldProj {
                receiver, field, ..
            } = &inst.op
            {
                let pt = facts.pt(*receiver);
                if pt.is_empty() || pt.is_top() {
                    continue;
                }
                let Some(name) = field_name(*field) else {
                    continue;
                };
                for loc in pt.iter() {
                    record(loc, &name, &mut out, /* is_write */ false);
                }
            }
        }
    }

    // Channel 2: writes from the synth-Assign side-table.  Each
    // entry maps the synthetic Assign's defined value → (receiver
    // SsaValue, FieldId).  The receiver's pt set determines which
    // parameter index the write attributes to.
    for (receiver, field) in body.field_writes.values() {
        let pt = facts.pt(*receiver);
        if pt.is_empty() || pt.is_top() {
            continue;
        }
        let Some(name) = field_name(*field) else {
            continue;
        };
        for loc in pt.iter() {
            record(loc, &name, &mut out, /* is_write */ true);
        }
    }

    out
}

/// Per-body points-to result.
///
/// Owns the body-local [`LocInterner`] and a flat `SsaValue → PointsToSet`
/// table.  The table is dense, one slot per SSA value, so lookups
/// are O(1).
#[derive(Clone, Debug)]
pub struct PointsToFacts {
    /// Body the facts were computed for; used as the disambiguator
    /// inside [`crate::pointer::AbsLoc::Param`] / `Alloc` / `SelfParam`.
    pub body: BodyId,
    /// Interner for the [`super::domain::AbsLoc`] referenced by the
    /// per-value points-to sets.
    pub interner: LocInterner,
    /// `pt(v)` for every SSA value in the body.  Unreachable / unused
    /// slots are `PointsToSet::empty()`.
    by_value: Vec<PointsToSet>,
}

impl PointsToFacts {
    /// Empty result, every value points to nothing.  Used by callers
    /// that need a "no facts" placeholder when the analysis is
    /// disabled or the body could not be analysed.
    pub fn empty(body: BodyId) -> Self {
        Self {
            body,
            interner: LocInterner::new(),
            by_value: Vec::new(),
        }
    }

    /// Borrow the points-to set for `v`.  Returns an empty set when
    /// `v` is out of range (e.g. a value defined by an instruction
    /// the analysis didn't visit).
    pub fn pt(&self, v: SsaValue) -> &PointsToSet {
        let idx = v.0 as usize;
        static EMPTY: once_cell::sync::Lazy<PointsToSet> =
            once_cell::sync::Lazy::new(PointsToSet::empty);
        self.by_value.get(idx).unwrap_or(&EMPTY)
    }

    /// True when every value has an empty points-to set.  Used as a
    /// fast-path skip in callers that only care about non-trivial
    /// aliasing.
    pub fn is_trivial(&self) -> bool {
        self.by_value.iter().all(|s| s.is_empty())
    }

    /// Number of SSA values covered by the facts table.
    pub fn len(&self) -> usize {
        self.by_value.len()
    }

    /// True when no SSA values are covered by the facts table.
    pub fn is_empty(&self) -> bool {
        self.by_value.is_empty()
    }

    /// Classify a value's points-to set into a [`PtrProxyHint`] for
    /// consumers that only care about the "is this a sub-field alias"
    /// distinction.  Returns [`PtrProxyHint::Other`] for empty sets,
    /// `Top`, and any set containing a root location ([`AbsLoc::SelfParam`]
    /// / [`AbsLoc::Param`] / [`AbsLoc::Alloc`]).  Returns
    /// [`PtrProxyHint::FieldOnly`] iff every member is an
    /// [`AbsLoc::Field`].
    ///
    pub fn proxy_hint(&self, v: SsaValue) -> PtrProxyHint {
        let set = self.pt(v);
        if set.is_empty() || set.is_top() {
            return PtrProxyHint::Other;
        }
        for id in set.iter() {
            match self.interner.resolve(id) {
                AbsLoc::Field { .. } => {}
                _ => return PtrProxyHint::Other,
            }
        }
        PtrProxyHint::FieldOnly
    }

    /// Build a `var_name → PtrProxyHint` map by scanning the body's
    /// value defs for the latest definition of each named variable.
    /// Names that resolve to no variable, or whose latest definition is
    /// `Other`, are omitted, only `FieldOnly` entries appear.
    ///
    /// Iterates over [`SsaBody::value_defs`] in *reverse* order so the
    /// last (post-renaming) SSA definition for each name wins.  Used by
    /// the resource-lifecycle pass to look up `pt(receiver_text)` in
    /// `apply_call` without re-walking the SSA body.
    pub fn name_proxy_hints(
        &self,
        body: &SsaBody,
    ) -> std::collections::HashMap<String, PtrProxyHint> {
        let mut out = std::collections::HashMap::new();
        for (idx, def) in body.value_defs.iter().enumerate().rev() {
            let Some(name) = def.var_name.as_ref() else {
                continue;
            };
            if out.contains_key(name) {
                continue;
            }
            let hint = self.proxy_hint(SsaValue(idx as u32));
            if hint == PtrProxyHint::FieldOnly {
                out.insert(name.clone(), hint);
            }
        }
        out
    }
}

/// Analyse a single body and return its [`PointsToFacts`].
///
/// `body_id` is used as the disambiguator inside the abstract
/// locations, supplying a stable id (e.g. the file's
/// `BodyMeta.id`) lets callers compare facts emitted by different
/// bodies in the same file.
pub fn analyse_body(body: &SsaBody, body_id: BodyId) -> PointsToFacts {
    let mut state = AnalysisState::new(body_id, body.num_values());

    // Pass 1, emit constraints from ops that don't depend on
    // representative resolution (Param, SelfParam, Call result,
    // etc.).  These produce the "leaf" points-to sets.
    for block in &body.blocks {
        for inst in block.phis.iter().chain(block.body.iter()) {
            state.transfer_inst(body_id, inst);
        }
    }

    // Pass 2+, propagate through field projections, phis, and
    // assignments until a fixpoint.  Field projections need iteration
    // because a `FieldProj` whose receiver's representative changes
    // (via a later unification) must re-emit its constraint with the
    // new representative.
    for _ in 0..MAX_FIXPOINT_ITERS {
        let mut changed = false;
        for block in &body.blocks {
            for inst in block.phis.iter().chain(block.body.iter()) {
                changed |= state.propagate_inst(inst);
            }
        }
        if !changed {
            break;
        }
    }

    state.into_facts()
}

// ── Constraint solver internals ────────────────────────────────────

/// Mutable analysis state, the interner, points-to table, and
/// union-find arrays.  Lives inside `analyse_body` only.
struct AnalysisState {
    /// Body-id forwarded to [`PointsToFacts::body`] when the analysis
    /// completes.  Recorded here so `into_facts` can preserve the
    /// caller-supplied id instead of defaulting to `BodyId(0)`.
    body_id: BodyId,
    interner: LocInterner,
    pt: Vec<PointsToSet>,
    parent: Vec<u32>,
    rank: Vec<u8>,
}

impl AnalysisState {
    fn new(body_id: BodyId, num_values: usize) -> Self {
        Self {
            body_id,
            interner: LocInterner::new(),
            pt: vec![PointsToSet::empty(); num_values],
            parent: (0..num_values as u32).collect(),
            rank: vec![0; num_values],
        }
    }

    /// Union-find find with path compression.
    fn find(&mut self, mut v: u32) -> u32 {
        if v as usize >= self.parent.len() {
            return v;
        }
        // Walk to root.
        let mut root = v;
        while self.parent[root as usize] != root {
            root = self.parent[root as usize];
        }
        // Compress.
        while self.parent[v as usize] != root {
            let next = self.parent[v as usize];
            self.parent[v as usize] = root;
            v = next;
        }
        root
    }

    /// Union `a` and `b` by rank.  Returns the new representative.
    /// Merges the points-to sets of the two classes into the new
    /// representative's slot.
    fn union(&mut self, a: u32, b: u32) -> u32 {
        let ra = self.find(a);
        let rb = self.find(b);
        if ra == rb {
            return ra;
        }
        let (winner, loser) = match self.rank[ra as usize].cmp(&self.rank[rb as usize]) {
            std::cmp::Ordering::Less => (rb, ra),
            std::cmp::Ordering::Greater => (ra, rb),
            std::cmp::Ordering::Equal => {
                self.rank[ra as usize] += 1;
                (ra, rb)
            }
        };
        self.parent[loser as usize] = winner;
        // Move the loser's points-to set into the winner's slot.
        let loser_pt = std::mem::take(&mut self.pt[loser as usize]);
        let _ = self.pt[winner as usize].union_in_place(&loser_pt);
        winner
    }

    /// `pt(rep) ∪= {loc}`.
    fn add_loc(&mut self, ssa: u32, loc: LocId) -> bool {
        let rep = self.find(ssa) as usize;
        let mut delta = PointsToSet::singleton(loc);
        // Inline insert via union to keep the saturation logic in one place.
        let changed = self.pt[rep].union_in_place(&delta);
        // `delta` no longer needed.
        let _ = &mut delta;
        changed
    }

    /// `pt(rep_a) ∪= pt(rep_b)`.  Caller is responsible for passing
    /// already-resolved representatives if it wants Steensgaard
    /// unification, see `union` for that.
    fn copy_pt(&mut self, dst: u32, src: u32) -> bool {
        let dr = self.find(dst);
        let sr = self.find(src);
        if dr == sr {
            return false;
        }
        // Take a clone of the source set so we can mutate dst.
        let src_pt = self.pt[sr as usize].clone();
        self.pt[dr as usize].union_in_place(&src_pt)
    }

    /// First-pass transfer for an instruction.  Emits constraints
    /// that don't depend on representative-stable resolution.
    fn transfer_inst(&mut self, body_id: BodyId, inst: &SsaInst) {
        let v = inst.value.0;
        if (v as usize) >= self.pt.len() {
            return;
        }
        match &inst.op {
            SsaOp::Param { index } => {
                let loc = self.interner.intern_param(body_id, *index);
                self.add_loc(v, loc);
            }
            SsaOp::SelfParam => {
                let loc = self.interner.intern_self_param(body_id);
                self.add_loc(v, loc);
            }
            SsaOp::CatchParam => {
                // Exception bindings come from the runtime, model as
                // an opaque allocation-site keyed by the SSA value.
                let loc = self.interner.intern_alloc(body_id, v);
                self.add_loc(v, loc);
            }
            SsaOp::Call {
                callee, receiver, ..
            } => {
                // container element retrieval ops
                // (`shift`, `pop`, `peek`, `front`, …) project through
                // the abstract `Field(pt(receiver), ELEM)` cell so
                // per-element taint flows independently of the SSA
                // value referencing the container.  The receiver's
                // points-to set may not be fully resolved on this
                // pass, so we *also* add a fresh allocation site as a
                // fallback, the fixpoint pass below absorbs the
                // proper Field projection once the receiver's set
                // converges.
                let loc = self.interner.intern_alloc(body_id, v);
                self.add_loc(v, loc);
                if let Some(rcv) = receiver
                    && is_container_read_callee(callee)
                    && (rcv.0 as usize) < self.parent.len()
                {
                    let rcv_rep = self.find(rcv.0) as usize;
                    let rcv_pt = self.pt[rcv_rep].clone();
                    if !rcv_pt.is_empty() && !rcv_pt.is_top() {
                        for parent_loc in rcv_pt.iter() {
                            let proj = self.interner.intern_field(parent_loc, FieldId::ELEM);
                            self.add_loc(v, proj);
                        }
                    }
                }
            }
            SsaOp::Assign(uses) => {
                // Steensgaard unification: rep(v) ∪= rep(u_i).  We
                // unify here and then re-propagate during the
                // fixpoint pass to absorb later field projections.
                for &u in uses {
                    if (u.0 as usize) < self.parent.len() {
                        self.union(v, u.0);
                    }
                }
            }
            SsaOp::Phi(operands) => {
                for (_, u) in operands {
                    if (u.0 as usize) < self.parent.len() {
                        self.union(v, u.0);
                    }
                }
            }
            SsaOp::FieldProj { .. } => {
                // Resolved during the fixpoint pass, see
                // `propagate_inst`.
            }
            SsaOp::Source | SsaOp::Const(_) | SsaOp::Nop | SsaOp::Undef => {
                // Scalars / no-ops: empty points-to set.
            }
        }
    }

    /// Fixpoint-pass transfer.  Re-runs constraints whose result
    /// depends on the current set of representatives, i.e. field
    /// projections, phis, and assignments may need to absorb new
    /// members emitted after the first pass.  Returns `true` when
    /// any points-to set changed.
    fn propagate_inst(&mut self, inst: &SsaInst) -> bool {
        let v = inst.value.0;
        if (v as usize) >= self.pt.len() {
            return false;
        }
        match &inst.op {
            SsaOp::FieldProj {
                receiver, field, ..
            } => {
                if (receiver.0 as usize) >= self.parent.len() {
                    return false;
                }
                let rcv_rep = self.find(receiver.0) as usize;
                let mut new_pt = PointsToSet::empty();
                let rcv_pt = self.pt[rcv_rep].clone();
                if rcv_pt.is_top() {
                    new_pt.insert(LOC_TOP);
                } else if rcv_pt.is_empty() {
                    // Nothing to project from yet.
                    return false;
                } else {
                    for parent_loc in rcv_pt.iter() {
                        let proj = self.interner.intern_field(parent_loc, *field);
                        new_pt.insert(proj);
                    }
                }
                let v_rep = self.find(v) as usize;
                self.pt[v_rep].union_in_place(&new_pt)
            }
            SsaOp::Assign(uses) => {
                let mut changed = false;
                for &u in uses {
                    if (u.0 as usize) < self.parent.len() {
                        // Steensgaard unification already happened in
                        // pass 1; re-copying the points-to set
                        // absorbs any members added since.
                        changed |= self.copy_pt(v, u.0);
                    }
                }
                changed
            }
            SsaOp::Phi(operands) => {
                let mut changed = false;
                for (_, u) in operands {
                    if (u.0 as usize) < self.parent.len() {
                        changed |= self.copy_pt(v, u.0);
                    }
                }
                changed
            }
            // No re-propagation needed for leaf ops.
            _ => false,
        }
    }

    /// Materialise the dense `SsaValue → PointsToSet` table.  Each
    /// value's set is the set of its representative, values in the
    /// same Steensgaard class share the same set.
    fn into_facts(mut self) -> PointsToFacts {
        let mut by_value = Vec::with_capacity(self.pt.len());
        // Resolve every value through the union-find before returning
        // so consumers see the unified set without having to re-find.
        let mut rep_cache: HashMap<u32, PointsToSet> = HashMap::new();
        let n = self.pt.len();
        for v in 0..n as u32 {
            let rep = self.find(v);
            let set = rep_cache
                .entry(rep)
                .or_insert_with(|| self.pt[rep as usize].clone())
                .clone();
            by_value.push(set);
        }
        PointsToFacts {
            body: self.body_id,
            interner: self.interner,
            by_value,
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    use crate::cfg::Cfg;
    use crate::ssa::ir::{
        BlockId, FieldId, FieldInterner, SsaBlock, SsaBody, SsaInst, SsaOp, SsaValue, Terminator,
        ValueDef,
    };
    use petgraph::graph::NodeIndex;
    use smallvec::{SmallVec, smallvec};
    use std::collections::HashMap;

    fn body_id() -> BodyId {
        BodyId(0)
    }

    /// Helpers for building synthetic SSA bodies in tests.  We
    /// fabricate bodies directly rather than running the full lowering
    /// pipeline so the tests stay focused on the points-to behaviour.
    struct BodyBuilder {
        defs: Vec<ValueDef>,
        body_insts: Vec<SsaInst>,
        next_value: u32,
        field_interner: FieldInterner,
    }

    impl BodyBuilder {
        fn new() -> Self {
            Self {
                defs: Vec::new(),
                body_insts: Vec::new(),
                next_value: 0,
                field_interner: FieldInterner::new(),
            }
        }

        fn fresh(&mut self, name: Option<&str>) -> SsaValue {
            let v = SsaValue(self.next_value);
            self.next_value += 1;
            self.defs.push(ValueDef {
                var_name: name.map(|s| s.to_string()),
                cfg_node: NodeIndex::new(0),
                block: BlockId(0),
            });
            v
        }

        fn emit(&mut self, value: SsaValue, op: SsaOp, name: Option<&str>) {
            self.body_insts.push(SsaInst {
                value,
                op,
                cfg_node: NodeIndex::new(0),
                var_name: name.map(|s| s.to_string()),
                span: (0, 0),
            });
        }

        fn intern_field(&mut self, name: &str) -> FieldId {
            self.field_interner.intern(name)
        }

        fn build(self) -> SsaBody {
            SsaBody {
                blocks: vec![SsaBlock {
                    id: BlockId(0),
                    phis: vec![],
                    body: self.body_insts,
                    terminator: Terminator::Return(None),
                    preds: SmallVec::new(),
                    succs: SmallVec::new(),
                }],
                entry: BlockId(0),
                value_defs: self.defs,
                cfg_node_map: HashMap::new(),
                exception_edges: vec![],
                field_interner: self.field_interner,
                field_writes: std::collections::HashMap::new(),

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

    /// `let c = self; let m = c.mu;` , pt(m) must be `{Field(SelfParam, mu)}`,
    /// distinct from pt(c) = `{SelfParam}`.
    #[test]
    fn field_subobject_distinct_from_receiver() {
        let mut b = BodyBuilder::new();
        let c = b.fresh(Some("c"));
        b.emit(c, SsaOp::SelfParam, Some("c"));

        let mu_field = b.intern_field("mu");
        let m = b.fresh(Some("c.mu"));
        b.emit(
            m,
            SsaOp::FieldProj {
                receiver: c,
                field: mu_field,
                projected_type: None,
            },
            Some("c.mu"),
        );

        let body = b.build();
        let facts = analyse_body(&body, body_id());

        let pt_c = facts.pt(c);
        let pt_m = facts.pt(m);

        assert_eq!(pt_c.len(), 1, "pt(c) should be a singleton SelfParam");
        assert_eq!(pt_m.len(), 1, "pt(c.mu) should be a singleton Field");
        assert!(!pt_m.is_top());

        // The two sets must not overlap.
        for c_loc in pt_c.iter() {
            for m_loc in pt_m.iter() {
                assert_ne!(c_loc, m_loc, "field and receiver share a location");
            }
        }

        // And the field's parent must be the receiver's location.
        let m_loc = pt_m.iter().next().unwrap();
        match facts.interner.resolve(m_loc) {
            crate::pointer::AbsLoc::Field { parent, field } => {
                assert_eq!(*field, mu_field);
                assert_eq!(*parent, pt_c.iter().next().unwrap());
            }
            other => panic!("expected Field, got {other:?}"),
        }
    }

    /// `let y = x;` , y and x share the same points-to set.
    #[test]
    fn copy_propagation_unifies() {
        let mut b = BodyBuilder::new();
        let x = b.fresh(Some("x"));
        b.emit(x, SsaOp::Param { index: 0 }, Some("x"));

        let y = b.fresh(Some("y"));
        b.emit(y, SsaOp::Assign(smallvec![x]), Some("y"));

        let body = b.build();
        let facts = analyse_body(&body, body_id());

        assert_eq!(
            facts.pt(x),
            facts.pt(y),
            "Steensgaard unifies pt(y) with pt(x) via the copy"
        );
        assert!(!facts.pt(y).is_empty());
    }

    /// `if (cond) z = a; else z = b;` , phi at the merge unifies
    /// `pt(z)` with both `pt(a)` and `pt(b)`.
    #[test]
    fn phi_unifies_branches() {
        let mut b = BodyBuilder::new();
        let a = b.fresh(Some("a"));
        b.emit(a, SsaOp::Param { index: 0 }, Some("a"));
        let b_v = b.fresh(Some("b"));
        b.emit(b_v, SsaOp::Param { index: 1 }, Some("b"));

        // Phi(0: a, 0: b), predecessor block ids are placeholders.
        let z = b.fresh(Some("z"));
        b.emit(
            z,
            SsaOp::Phi(smallvec![(BlockId(0), a), (BlockId(0), b_v)]),
            Some("z"),
        );

        let body = b.build();
        let facts = analyse_body(&body, body_id());

        let pt_z = facts.pt(z);
        // Steensgaard unifies the three classes; pt(z) == pt(a) == pt(b)
        // and contains both Param locations.
        assert_eq!(pt_z, facts.pt(a));
        assert_eq!(pt_z, facts.pt(b_v));
        assert_eq!(pt_z.len(), 2);
    }

    /// `node = node.next;`, the `FieldProj` self-cycle must
    /// terminate via the union-find / depth bound, not loop.
    #[test]
    fn self_referential_field_chain_terminates() {
        let mut b = BodyBuilder::new();
        let node = b.fresh(Some("node"));
        b.emit(node, SsaOp::Param { index: 0 }, Some("node"));

        let next_field = b.intern_field("next");
        // Repeated pattern: `node = node.next` modeled as
        // fp = FieldProj(node, next); node' = Assign([fp])
        for _ in 0..6 {
            let fp = b.fresh(Some("node.next"));
            b.emit(
                fp,
                SsaOp::FieldProj {
                    receiver: node,
                    field: next_field,
                    projected_type: None,
                },
                Some("node.next"),
            );
            let new_node = b.fresh(Some("node"));
            b.emit(new_node, SsaOp::Assign(smallvec![fp]), Some("node"));
            // The original `node` and the new one are unified by Assign,
            // creating the self-cycle.  We don't update `node` here so
            // every iteration emits a fresh FieldProj on the original.
        }

        let body = b.build();
        // The bounded `MAX_FIELD_DEPTH` + union-find termination guarantees
        // analysis returns; this test would hang or panic on regression.
        let facts = analyse_body(&body, body_id());
        let pt_node = facts.pt(node);
        // Either we converge to a non-empty set including a Field chain,
        // or we saturate to Top, either is a valid termination outcome.
        assert!(!pt_node.is_empty());
    }

    /// `Source` introduces no points-to facts (taint is a separate
    /// lattice; points-to only models heap reach).
    #[test]
    fn source_op_has_empty_pt() {
        let mut b = BodyBuilder::new();
        let s = b.fresh(Some("s"));
        b.emit(s, SsaOp::Source, Some("s"));

        let body = b.build();
        let facts = analyse_body(&body, body_id());
        assert!(facts.pt(s).is_empty());
    }

    /// `Call` produces a fresh allocation-site location for its result ,
    /// distinct from its arguments.
    #[test]
    fn call_result_is_fresh_alloc() {
        let mut b = BodyBuilder::new();
        let arg = b.fresh(Some("x"));
        b.emit(arg, SsaOp::Param { index: 0 }, Some("x"));

        let result = b.fresh(Some("r"));
        b.emit(
            result,
            SsaOp::Call {
                callee: "make_thing".into(),
                callee_text: None,
                args: vec![smallvec![arg]],
                receiver: None,
            },
            Some("r"),
        );

        let body = b.build();
        let facts = analyse_body(&body, body_id());

        let pt_arg = facts.pt(arg);
        let pt_result = facts.pt(result);
        assert!(!pt_result.is_empty());
        assert!(!pt_arg.is_empty());
        // No member shared between the two sets.
        for ra in pt_arg.iter() {
            for rr in pt_result.iter() {
                assert_ne!(ra, rr);
            }
        }
    }

    /// Driver smoke-test: the analysis runs on an SsaBody produced by
    /// the real lowering pipeline without panicking.  This pins the
    /// "no behaviour change" gate, analysis runs to completion on
    /// representative input.
    #[test]
    fn smoke_runs_on_lowered_body() {
        // We don't exercise the real lowering here (that needs a full
        // CFG fixture); the synthetic builder above covers the IR
        // surface area.  Just confirm the entry point is callable
        // with an empty body.
        let body = SsaBody {
            blocks: vec![SsaBlock {
                id: BlockId(0),
                phis: vec![],
                body: vec![],
                terminator: Terminator::Return(None),
                preds: SmallVec::new(),
                succs: SmallVec::new(),
            }],
            entry: BlockId(0),
            value_defs: vec![],
            cfg_node_map: HashMap::new(),
            exception_edges: vec![],
            field_interner: FieldInterner::new(),
            field_writes: std::collections::HashMap::new(),

            synthetic_externals: std::collections::HashSet::new(),
        };
        let facts = analyse_body(&body, body_id());
        assert!(facts.is_trivial());
        assert_eq!(facts.len(), 0);

        let _ = std::marker::PhantomData::<Cfg>;
    }

    /// Contract pin: a value defined by a `FieldProj`
    /// classifies as [`PtrProxyHint::FieldOnly`].  Consumed by the
    /// resource-lifecycle pass to recognise field-aliased locals.
    #[test]
    fn proxy_hint_field_only_for_field_proj_value() {
        let mut b = BodyBuilder::new();
        let c = b.fresh(Some("c"));
        b.emit(c, SsaOp::SelfParam, Some("c"));
        let mu = b.intern_field("mu");
        let m = b.fresh(Some("m"));
        b.emit(
            m,
            SsaOp::FieldProj {
                receiver: c,
                field: mu,
                projected_type: None,
            },
            Some("m"),
        );

        let body = b.build();
        let facts = analyse_body(&body, BodyId(7));
        assert_eq!(
            facts.body,
            BodyId(7),
            "PointsToFacts must preserve caller-supplied BodyId"
        );
        assert_eq!(facts.proxy_hint(m), crate::pointer::PtrProxyHint::FieldOnly);
        assert_eq!(facts.proxy_hint(c), crate::pointer::PtrProxyHint::Other);
    }

    /// container-read callee classifier covers a
    /// representative sample across nyx's languages.  Pinned because
    /// the taint engine relies on the same classifier.
    #[test]
    fn container_read_callee_classifier_covers_common_methods() {
        for c in [
            "shift",
            "pop",
            "peek",
            "front",
            "back",
            "queue.shift",
            "list.pop",
            "deque.popleft",
            "stack.peek",
            "vec.first",
        ] {
            assert!(is_container_read_callee(c), "expected container read: {c}");
        }
        for c in ["push", "append", "insert", "myMethod", "process"] {
            assert!(
                !is_container_read_callee(c),
                "non-read should classify false: {c}"
            );
        }
    }

    /// container-write classifier (mirror).
    #[test]
    fn container_write_callee_classifier() {
        for c in [
            "push",
            "pushback",
            "push_back",
            "append",
            "insert",
            "enqueue",
            "list.append",
        ] {
            assert!(is_container_write_callee(c), "expected write: {c}");
        }
        for c in ["pop", "shift", "process", "lookup"] {
            assert!(
                !is_container_write_callee(c),
                "non-write should classify false: {c}"
            );
        }
    }

    /// a `Call("shift", receiver=container)` projects
    /// `Field(pt(container), ELEM)` into the result, alongside the
    /// fresh allocation site that fall-back paths still emit.
    #[test]
    fn container_read_call_projects_through_elem_field() {
        let mut b = BodyBuilder::new();
        // `arr` is the parameter container.
        let arr = b.fresh(Some("arr"));
        b.emit(arr, SsaOp::Param { index: 0 }, Some("arr"));
        // `e := arr.shift()`, container read.
        let e = b.fresh(Some("e"));
        b.emit(
            e,
            SsaOp::Call {
                callee: "shift".into(),
                callee_text: None,
                args: vec![],
                receiver: Some(arr),
            },
            Some("e"),
        );

        let body = b.build();
        let facts = analyse_body(&body, BodyId(0));
        let pt_e = facts.pt(e);
        // The result must include at least one Field(_, ELEM) member.
        let mut saw_elem = false;
        for loc in pt_e.iter() {
            if let crate::pointer::AbsLoc::Field { field, .. } = facts.interner.resolve(loc)
                && *field == FieldId::ELEM
            {
                saw_elem = true;
                break;
            }
        }
        assert!(
            saw_elem,
            "container read result should include Field(_, ELEM); got {pt_e:?}"
        );
    }

    /// `extract_field_points_to` records a field
    /// READ on the parameter index when a `FieldProj` traces back to
    /// an `AbsLoc::Param`.
    #[test]
    fn extract_field_points_to_records_param_reads() {
        let mut b = BodyBuilder::new();
        // `obj` is parameter 0.
        let obj = b.fresh(Some("obj"));
        b.emit(obj, SsaOp::Param { index: 0 }, Some("obj"));
        // `let n = obj.name;`, field projection from a param.
        let name_field = b.intern_field("name");
        let n = b.fresh(Some("n"));
        b.emit(
            n,
            SsaOp::FieldProj {
                receiver: obj,
                field: name_field,
                projected_type: None,
            },
            Some("n"),
        );

        let body = b.build();
        let facts = analyse_body(&body, BodyId(0));
        let summary = extract_field_points_to(&body, &facts);
        let entry = summary
            .param_field_reads
            .iter()
            .find(|(p, _)| *p == 0)
            .expect("param 0 read recorded");
        assert!(entry.1.iter().any(|s| s == "name"));
    }

    /// `extract_field_points_to` records field
    /// WRITES from the body's `field_writes` side-table populated by
    /// SSA lowering.  A synth Assign whose receiver traces back to
    /// `AbsLoc::Param` produces a `param_field_writes` entry.
    #[test]
    fn extract_field_points_to_records_param_writes() {
        let mut b = BodyBuilder::new();
        // `obj` is parameter 0.
        let obj = b.fresh(Some("obj"));
        b.emit(obj, SsaOp::Param { index: 0 }, Some("obj"));
        // Synth Assign mimicking `obj.cache = rhs`: define `cache`
        // field id and a synthetic value whose op is Assign.  The
        // side-table maps `synth_v -> (obj, cache_id)`.
        let cache_id = b.intern_field("cache");
        let rhs = b.fresh(Some("rhs"));
        b.emit(rhs, SsaOp::Source, Some("rhs"));
        let synth = b.fresh(Some("obj"));
        b.emit(synth, SsaOp::Assign(smallvec![rhs]), Some("obj"));

        let mut body = b.build();
        body.field_writes.insert(synth, (obj, cache_id));

        let facts = analyse_body(&body, BodyId(0));
        let summary = extract_field_points_to(&body, &facts);
        let entry = summary
            .param_field_writes
            .iter()
            .find(|(p, _)| *p == 0)
            .expect("param 0 write must be recorded from field_writes");
        assert!(
            entry.1.iter().any(|s| s == "cache"),
            "expected 'cache' in writes; got {:?}",
            entry.1,
        );
    }

    /// writes through the receiver (`this.f =
    /// rhs`) are recorded under the same `u32::MAX` sentinel as
    /// reads.
    #[test]
    fn extract_field_points_to_records_self_writes_under_sentinel() {
        let mut b = BodyBuilder::new();
        let this = b.fresh(Some("this"));
        b.emit(this, SsaOp::SelfParam, Some("this"));
        let cache_id = b.intern_field("cache");
        let rhs = b.fresh(Some("rhs"));
        b.emit(rhs, SsaOp::Source, Some("rhs"));
        let synth = b.fresh(Some("this"));
        b.emit(synth, SsaOp::Assign(smallvec![rhs]), Some("this"));

        let mut body = b.build();
        body.field_writes.insert(synth, (this, cache_id));

        let facts = analyse_body(&body, BodyId(0));
        let summary = extract_field_points_to(&body, &facts);
        let entry = summary
            .param_field_writes
            .iter()
            .find(|(p, _)| *p == u32::MAX)
            .expect("receiver write recorded under u32::MAX sentinel");
        assert!(entry.1.iter().any(|s| s == "cache"));
    }

    /// container-element writes (`<elem>`
    /// marker) flow through the same channel as named-field writes
    /// when the synth Assign carries `FieldId::ELEM`.
    #[test]
    fn extract_field_points_to_records_elem_writes() {
        let mut b = BodyBuilder::new();
        let arr = b.fresh(Some("arr"));
        b.emit(arr, SsaOp::Param { index: 0 }, Some("arr"));
        let rhs = b.fresh(Some("rhs"));
        b.emit(rhs, SsaOp::Source, Some("rhs"));
        let synth = b.fresh(Some("arr"));
        b.emit(synth, SsaOp::Assign(smallvec![rhs]), Some("arr"));

        let mut body = b.build();
        body.field_writes.insert(synth, (arr, FieldId::ELEM));

        let facts = analyse_body(&body, BodyId(0));
        let summary = extract_field_points_to(&body, &facts);
        let entry = summary
            .param_field_writes
            .iter()
            .find(|(p, _)| *p == 0)
            .expect("ELEM write on param 0 recorded");
        assert!(
            entry.1.iter().any(|s| s == "<elem>"),
            "ELEM marker '<elem>' must surface unchanged across the wire",
        );
    }

    /// receiver projections are recorded under the
    /// `u32::MAX` sentinel parameter index (mirror of
    /// `SsaFuncSummary::receiver_to_*`).
    #[test]
    fn extract_field_points_to_records_self_reads_under_sentinel() {
        let mut b = BodyBuilder::new();
        let this = b.fresh(Some("this"));
        b.emit(this, SsaOp::SelfParam, Some("this"));
        let cache = b.intern_field("cache");
        let c = b.fresh(Some("c"));
        b.emit(
            c,
            SsaOp::FieldProj {
                receiver: this,
                field: cache,
                projected_type: None,
            },
            Some("c"),
        );

        let body = b.build();
        let facts = analyse_body(&body, BodyId(0));
        let summary = extract_field_points_to(&body, &facts);
        let entry = summary
            .param_field_reads
            .iter()
            .find(|(p, _)| *p == u32::MAX)
            .expect("receiver read recorded under u32::MAX sentinel");
        assert!(entry.1.iter().any(|s| s == "cache"));
    }

    /// `name_proxy_hints` returns one entry per source-level variable
    /// whose latest SSA def has [`PtrProxyHint::FieldOnly`].  Names that
    /// don't qualify are omitted entirely so the consumer's lookup
    /// stays cheap.
    /// W5: subscript-read synthetic callee `__index_get__` must be
    /// recognised by the public container-read predicate so the W2/W4
    /// taint hooks fire on subscript reads (`arr[i]`, `cmds[0]`).
    #[test]
    fn subscript_get_classifies_as_container_read() {
        assert!(is_container_read_callee_pub("__index_get__"));
        assert!(is_container_read_callee_pub("arr.__index_get__"));
    }

    /// W5: subscript-write synthetic callee `__index_set__` must be
    /// recognised by the public container-write predicate so the W2
    /// taint hook fires on subscript writes (`arr[i] = v`).
    #[test]
    fn subscript_set_classifies_as_container_write() {
        assert!(is_container_write_callee("__index_set__"));
        assert!(is_container_write_callee("arr.__index_set__"));
    }

    /// W5: regression guard, neither synth name should match the
    /// opposite predicate, otherwise the W2 read/write hooks would
    /// double-fire on the same call.
    #[test]
    fn subscript_synth_callees_do_not_cross_classify() {
        assert!(!is_container_read_callee_pub("__index_set__"));
        assert!(!is_container_write_callee("__index_get__"));
    }

    #[test]
    fn name_proxy_hints_collects_field_only_locals() {
        let mut b = BodyBuilder::new();
        // `c` is the receiver, root location, hint=Other.
        let c = b.fresh(Some("c"));
        b.emit(c, SsaOp::SelfParam, Some("c"));
        // `m := c.mu`, field projection, hint=FieldOnly.
        let mu = b.intern_field("mu");
        let m = b.fresh(Some("m"));
        b.emit(
            m,
            SsaOp::FieldProj {
                receiver: c,
                field: mu,
                projected_type: None,
            },
            Some("m"),
        );

        let body = b.build();
        let facts = analyse_body(&body, BodyId(0));
        let hints = facts.name_proxy_hints(&body);
        assert_eq!(
            hints.get("m"),
            Some(&crate::pointer::PtrProxyHint::FieldOnly)
        );
        assert!(
            !hints.contains_key("c"),
            "root receiver must not appear in the FieldOnly map"
        );
    }
}