dotscope 0.6.0

A high-performance, cross-platform framework for analyzing and reverse engineering .NET PE executables
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
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
//! Memory SSA (MSSA) for tracking versioned memory locations.
//!
//! This module extends SSA to track state stored in fields, arrays, and heap locations.
//! Memory SSA is essential for precise analysis of obfuscated code that stores state
//! in memory rather than local variables.
//!
//! # Architecture
//!
//! Memory SSA builds on top of traditional SSA by:
//!
//! 1. **Memory Locations**: Abstract representation of memory (fields, arrays, pointers)
//! 2. **Memory Versioning**: Each store creates a new version, each load reads a version
//! 3. **Memory Phi Nodes**: At control flow merges, memory versions are merged
//!
//! ```text
//! Traditional SSA:           Memory SSA:
//!
//!   v1 = x                     v1 = x
//!   obj.field = v1             mem[obj.field]₁ = v1
//!   ...                        ...
//!   v2 = obj.field             v2 = mem[obj.field]₁
//! ```
//!
//! # Memory Location Hierarchy
//!
//! Memory locations form a hierarchy for alias analysis:
//!
//! ```text
//! Unknown (may alias anything)
//!   ├── StaticField(token)      - Specific static field
//!   ├── InstanceField(obj, token) - Specific instance field
//!   ├── ArrayElement(arr, idx)   - Specific array element
//!   │     ├── ArrayElement(arr, Constant(i)) - Known index
//!   │     └── ArrayElement(arr, Variable(v)) - Unknown index (may alias)
//!   └── Indirect(addr)          - Pointer dereference
//! ```
//!
//! # Usage
//!
//! ```rust,ignore
//! use dotscope::analysis::{MemorySsa, SsaFunction, SsaCfg};
//!
//! let cfg = SsaCfg::from_ssa(&ssa);
//! let mem_ssa = MemorySsa::build(&ssa, &cfg);
//!
//! // Query memory version at a specific point
//! let loc = MemoryLocation::StaticField(field_token);
//! if let Some(version) = mem_ssa.version_at_block(&loc, block_idx) {
//!     println!("Memory version: {}", version);
//! }
//! ```
//!
//! # References
//!
//! - Chow et al., "Effective Representation of Aliases and Indirect Memory
//!   Operations in SSA Form", CC 1996

use std::collections::{HashMap, HashSet, VecDeque};

use crate::analysis::ssa::{FieldRef, SsaCfg, SsaFunction, SsaOp, SsaVarId};
use crate::utils::graph::{
    algorithms::{compute_dominance_frontiers, compute_dominators},
    GraphBase, NodeId, RootedGraph, Successors,
};

/// Represents an abstract memory location.
///
/// Memory locations are used to track which memory is being accessed by
/// load/store operations. The granularity varies by location type:
///
/// - Static fields are precise (one location per field)
/// - Instance fields depend on object identity (may alias if objects may alias)
/// - Array elements depend on both array identity and index
#[derive(Debug, Clone, Hash, PartialEq, Eq)]
pub enum MemoryLocation {
    /// Instance field access: `object.field`
    ///
    /// The `SsaVarId` identifies the object, and `FieldRef` identifies the field.
    /// Two instance field locations may alias if the objects may alias.
    InstanceField(SsaVarId, FieldRef),

    /// Static field access: `ClassName.field`
    ///
    /// Static fields are uniquely identified by their token. Two static field
    /// locations alias iff they have the same token.
    StaticField(FieldRef),

    /// Array element access: `array[index]`
    ///
    /// The `SsaVarId` identifies the array, and `ArrayIndex` identifies the index.
    /// Array element locations may alias based on array identity and index overlap.
    ArrayElement(SsaVarId, ArrayIndex),

    /// Indirect memory access through a pointer: `*ptr`
    ///
    /// The `SsaVarId` is the pointer variable. Indirect accesses are the most
    /// conservative - they may alias anything the pointer could point to.
    Indirect(SsaVarId),

    /// Unknown/escaped memory.
    ///
    /// Used when we can't determine the exact location (e.g., after a call
    /// that may modify memory, or for volatile accesses).
    Unknown,
}

impl MemoryLocation {
    /// Returns the base object variable, if any.
    ///
    /// For instance fields and arrays, this is the object/array variable.
    /// For static fields and unknown locations, returns `None`.
    #[must_use]
    pub fn base_object(&self) -> Option<SsaVarId> {
        match self {
            Self::InstanceField(obj, _) => Some(*obj),
            Self::ArrayElement(arr, _) => Some(*arr),
            Self::Indirect(ptr) => Some(*ptr),
            Self::StaticField(_) | Self::Unknown => None,
        }
    }

    /// Returns `true` if this location may alias the other location.
    ///
    /// This is a conservative analysis - if we can't prove non-aliasing,
    /// we assume aliasing is possible.
    #[must_use]
    pub fn may_alias(&self, other: &Self) -> bool {
        match (self, other) {
            // Unknown aliases everything; Indirect may alias any concrete location
            (Self::Unknown, _)
            | (_, Self::Unknown)
            | (
                Self::Indirect(_),
                Self::InstanceField(..) | Self::ArrayElement(..) | Self::StaticField(_),
            )
            | (
                Self::InstanceField(..) | Self::ArrayElement(..) | Self::StaticField(_),
                Self::Indirect(_),
            ) => true,

            // Static fields alias iff same field
            (Self::StaticField(f1), Self::StaticField(f2)) => f1 == f2,

            // Static fields don't alias instance fields or arrays;
            // Instance fields don't alias array elements (different memory types)
            (Self::StaticField(_), Self::InstanceField(..) | Self::ArrayElement(..))
            | (Self::InstanceField(..) | Self::ArrayElement(..), Self::StaticField(_))
            | (Self::InstanceField(..), Self::ArrayElement(..))
            | (Self::ArrayElement(..), Self::InstanceField(..)) => false,

            // Instance fields alias if same object AND same field
            // Conservative: different objects assumed to not alias
            (Self::InstanceField(obj1, f1), Self::InstanceField(obj2, f2)) => {
                obj1 == obj2 && f1 == f2
            }

            // Array elements alias if same array AND indices may overlap
            (Self::ArrayElement(arr1, idx1), Self::ArrayElement(arr2, idx2)) => {
                arr1 == arr2 && idx1.may_overlap(idx2)
            }

            // Indirect access may alias anything with same pointer
            (Self::Indirect(p1), Self::Indirect(p2)) => p1 == p2,
        }
    }

    /// Returns `true` if this location must alias the other location.
    ///
    /// This is a more precise analysis - returns `true` only if we can
    /// prove the locations definitely refer to the same memory.
    #[must_use]
    pub fn must_alias(&self, other: &Self) -> bool {
        match (self, other) {
            // Static fields must-alias iff same field
            (Self::StaticField(f1), Self::StaticField(f2)) => f1 == f2,

            // Instance fields must-alias iff same object AND same field
            (Self::InstanceField(obj1, f1), Self::InstanceField(obj2, f2)) => {
                obj1 == obj2 && f1 == f2
            }

            // Array elements must-alias iff same array AND same constant index
            (Self::ArrayElement(arr1, idx1), Self::ArrayElement(arr2, idx2)) => {
                arr1 == arr2 && idx1.must_equal(idx2)
            }

            // Indirect must-alias iff same pointer
            (Self::Indirect(p1), Self::Indirect(p2)) => p1 == p2,

            // Unknown never must-aliases (not precise enough)
            _ => false,
        }
    }
}

/// Represents an array index for array element locations.
#[derive(Debug, Clone, Hash, PartialEq, Eq)]
pub enum ArrayIndex {
    /// A constant index value.
    Constant(i64),
    /// A variable index.
    Variable(SsaVarId),
    /// Unknown index (could be any value).
    Unknown,
}

impl ArrayIndex {
    /// Returns `true` if these indices may refer to the same element.
    #[must_use]
    pub fn may_overlap(&self, other: &Self) -> bool {
        match (self, other) {
            // Unknown overlaps everything; Variable indices may overlap (conservative)
            (Self::Unknown | Self::Variable(_), _) | (_, Self::Unknown | Self::Variable(_)) => true,
            // Constants overlap iff equal
            (Self::Constant(i1), Self::Constant(i2)) => i1 == i2,
        }
    }

    /// Returns `true` if these indices must refer to the same element.
    #[must_use]
    pub fn must_equal(&self, other: &Self) -> bool {
        match (self, other) {
            (Self::Constant(i1), Self::Constant(i2)) => i1 == i2,
            (Self::Variable(v1), Self::Variable(v2)) => v1 == v2,
            _ => false,
        }
    }
}

/// A memory operation (load or store).
#[derive(Debug, Clone)]
pub enum MemoryOp {
    /// A memory load operation.
    Load {
        /// The memory location being loaded.
        location: MemoryLocation,
        /// The SSA variable receiving the loaded value.
        dest: SsaVarId,
        /// Block containing this operation.
        block: usize,
        /// Instruction index within the block.
        instr: usize,
    },
    /// A memory store operation.
    Store {
        /// The memory location being stored to.
        location: MemoryLocation,
        /// The SSA variable being stored.
        value: SsaVarId,
        /// Block containing this operation.
        block: usize,
        /// Instruction index within the block.
        instr: usize,
    },
}

impl MemoryOp {
    /// Returns the memory location accessed by this operation.
    #[must_use]
    pub fn location(&self) -> &MemoryLocation {
        match self {
            Self::Load { location, .. } | Self::Store { location, .. } => location,
        }
    }

    /// Returns the block index containing this operation.
    #[must_use]
    pub fn block(&self) -> usize {
        match self {
            Self::Load { block, .. } | Self::Store { block, .. } => *block,
        }
    }

    /// Returns the instruction index within the block.
    #[must_use]
    pub fn instr(&self) -> usize {
        match self {
            Self::Load { instr, .. } | Self::Store { instr, .. } => *instr,
        }
    }

    /// Returns `true` if this is a store operation.
    #[must_use]
    pub fn is_store(&self) -> bool {
        matches!(self, Self::Store { .. })
    }

    /// Returns `true` if this is a load operation.
    #[must_use]
    pub fn is_load(&self) -> bool {
        matches!(self, Self::Load { .. })
    }
}

/// A phi node for memory locations.
///
/// Memory phi nodes are placed at control flow merge points where different
/// memory versions from different predecessors need to be merged.
#[derive(Debug, Clone)]
pub struct MemoryPhi {
    /// The memory location this phi node is for.
    pub location: MemoryLocation,
    /// The result version number produced by this phi.
    pub result_version: u32,
    /// The operands from each predecessor.
    pub operands: Vec<MemoryPhiOperand>,
}

impl MemoryPhi {
    /// Creates a new memory phi node.
    #[must_use]
    pub fn new(location: MemoryLocation, result_version: u32) -> Self {
        Self {
            location,
            result_version,
            operands: Vec::new(),
        }
    }

    /// Adds an operand from a predecessor block.
    pub fn add_operand(&mut self, predecessor: usize, version: u32) {
        self.operands.push(MemoryPhiOperand {
            predecessor,
            version,
        });
    }

    /// Returns the operand from a specific predecessor, if present.
    #[must_use]
    pub fn operand_from(&self, predecessor: usize) -> Option<&MemoryPhiOperand> {
        self.operands
            .iter()
            .find(|op| op.predecessor == predecessor)
    }
}

/// An operand of a memory phi node.
#[derive(Debug, Clone)]
pub struct MemoryPhiOperand {
    /// The predecessor block this operand comes from.
    pub predecessor: usize,
    /// The memory version from that predecessor.
    pub version: u32,
}

/// Memory version identifier.
///
/// Combines a memory location with a version number to uniquely identify
/// a specific "value" of that memory location.
#[derive(Debug, Clone, Hash, PartialEq, Eq)]
pub struct MemoryVersion {
    /// The memory location.
    pub location: MemoryLocation,
    /// The version number.
    pub version: u32,
}

impl MemoryVersion {
    /// Creates a new memory version.
    #[must_use]
    pub fn new(location: MemoryLocation, version: u32) -> Self {
        Self { location, version }
    }
}

/// Definition site for a memory version.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum MemoryDefSite {
    /// Defined at entry (initial version).
    Entry,
    /// Defined by a store instruction.
    Store {
        /// The block containing the store.
        block: usize,
        /// The instruction index within the block.
        instr: usize,
    },
    /// Defined by a memory phi node.
    Phi {
        /// The block containing the phi node.
        block: usize,
    },
}

/// Memory SSA representation.
///
/// This structure tracks versioned memory locations throughout a function,
/// enabling precise tracking of memory state for analysis.
#[derive(Debug)]
pub struct MemorySsa {
    /// Next version number for each memory location.
    next_version: HashMap<MemoryLocation, u32>,

    /// Memory phi nodes at each block.
    /// Key is block index, value is list of memory phi nodes.
    memory_phis: HashMap<usize, Vec<MemoryPhi>>,

    /// Definition sites for each memory version.
    definitions: HashMap<MemoryVersion, MemoryDefSite>,

    /// Memory version at block entry for each location.
    /// Key is (location, block), value is version.
    entry_versions: HashMap<(MemoryLocation, usize), u32>,

    /// Memory version at block exit for each location.
    /// Key is (location, block), value is version.
    exit_versions: HashMap<(MemoryLocation, usize), u32>,

    /// All identified memory operations.
    operations: Vec<MemoryOp>,

    /// All unique memory locations in the function.
    locations: HashSet<MemoryLocation>,
}

impl MemorySsa {
    /// Creates an empty Memory SSA structure.
    #[must_use]
    pub fn new() -> Self {
        Self {
            next_version: HashMap::new(),
            memory_phis: HashMap::new(),
            definitions: HashMap::new(),
            entry_versions: HashMap::new(),
            exit_versions: HashMap::new(),
            operations: Vec::new(),
            locations: HashSet::new(),
        }
    }

    /// Builds Memory SSA from an SSA function.
    ///
    /// This performs the full Memory SSA construction:
    /// 1. Identify all memory operations
    /// 2. Place memory phi nodes at dominance frontiers
    /// 3. Rename memory versions using dominator tree traversal
    ///
    /// # Arguments
    ///
    /// * `ssa` - The SSA function to analyze.
    /// * `cfg` - The control flow graph of the function.
    ///
    /// # Returns
    ///
    /// A complete Memory SSA representation.
    #[must_use]
    pub fn build(ssa: &SsaFunction, cfg: &SsaCfg<'_>) -> Self {
        let mut mem_ssa = Self::new();

        // Phase 1: Identify all memory operations
        mem_ssa.identify_memory_operations(ssa);

        // Phase 2: Place memory phi nodes
        mem_ssa.place_memory_phis(cfg);

        // Phase 3: Rename memory versions
        mem_ssa.rename_memory_versions(ssa, cfg);

        mem_ssa
    }

    /// Returns the memory phi nodes at a block.
    #[must_use]
    pub fn memory_phis(&self, block: usize) -> &[MemoryPhi] {
        self.memory_phis.get(&block).map_or(&[], Vec::as_slice)
    }

    /// Returns all memory operations.
    #[must_use]
    pub fn operations(&self) -> &[MemoryOp] {
        &self.operations
    }

    /// Returns all unique memory locations.
    #[must_use]
    pub fn locations(&self) -> &HashSet<MemoryLocation> {
        &self.locations
    }

    /// Returns the memory version at block entry for a location.
    #[must_use]
    pub fn version_at_entry(&self, location: &MemoryLocation, block: usize) -> Option<u32> {
        self.entry_versions.get(&(location.clone(), block)).copied()
    }

    /// Returns the memory version at block exit for a location.
    #[must_use]
    pub fn version_at_exit(&self, location: &MemoryLocation, block: usize) -> Option<u32> {
        self.exit_versions.get(&(location.clone(), block)).copied()
    }

    /// Returns the definition site for a memory version.
    #[must_use]
    pub fn definition(&self, version: &MemoryVersion) -> Option<MemoryDefSite> {
        self.definitions.get(version).copied()
    }

    /// Returns the next version number for a location (and increments it).
    fn allocate_version(&mut self, location: &MemoryLocation) -> u32 {
        let version = self.next_version.entry(location.clone()).or_insert(0);
        let result = *version;
        *version += 1;
        result
    }

    /// Returns the current version number for a location without incrementing.
    fn current_version(&self, location: &MemoryLocation) -> u32 {
        self.next_version
            .get(location)
            .copied()
            .unwrap_or(0)
            .saturating_sub(1)
    }

    /// Phase 1: Identify all memory operations in the SSA function.
    fn identify_memory_operations(&mut self, ssa: &SsaFunction) {
        for (block_idx, instr_idx, instr) in ssa.iter_instructions() {
            if let Some(mem_op) = Self::classify_memory_operation(instr.op(), block_idx, instr_idx)
            {
                self.locations.insert(mem_op.location().clone());
                self.operations.push(mem_op);
            }
        }
    }

    /// Classifies an SSA operation as a memory operation, if applicable.
    fn classify_memory_operation(op: &SsaOp, block: usize, instr: usize) -> Option<MemoryOp> {
        match op {
            SsaOp::LoadField {
                dest,
                object,
                field,
            } => {
                let location = MemoryLocation::InstanceField(*object, *field);
                Some(MemoryOp::Load {
                    location,
                    dest: *dest,
                    block,
                    instr,
                })
            }
            SsaOp::StoreField {
                object,
                field,
                value,
            } => {
                let location = MemoryLocation::InstanceField(*object, *field);
                Some(MemoryOp::Store {
                    location,
                    value: *value,
                    block,
                    instr,
                })
            }
            SsaOp::LoadStaticField { dest, field } => {
                let location = MemoryLocation::StaticField(*field);
                Some(MemoryOp::Load {
                    location,
                    dest: *dest,
                    block,
                    instr,
                })
            }
            SsaOp::StoreStaticField { field, value } => {
                let location = MemoryLocation::StaticField(*field);
                Some(MemoryOp::Store {
                    location,
                    value: *value,
                    block,
                    instr,
                })
            }
            SsaOp::LoadElement {
                dest, array, index, ..
            } => {
                let idx = Self::resolve_array_index(*index);
                let location = MemoryLocation::ArrayElement(*array, idx);
                Some(MemoryOp::Load {
                    location,
                    dest: *dest,
                    block,
                    instr,
                })
            }
            SsaOp::StoreElement {
                array,
                index,
                value,
                ..
            } => {
                let idx = Self::resolve_array_index(*index);
                let location = MemoryLocation::ArrayElement(*array, idx);
                Some(MemoryOp::Store {
                    location,
                    value: *value,
                    block,
                    instr,
                })
            }
            SsaOp::LoadIndirect { dest, addr, .. } => {
                let location = MemoryLocation::Indirect(*addr);
                Some(MemoryOp::Load {
                    location,
                    dest: *dest,
                    block,
                    instr,
                })
            }
            SsaOp::StoreIndirect { addr, value, .. } => {
                let location = MemoryLocation::Indirect(*addr);
                Some(MemoryOp::Store {
                    location,
                    value: *value,
                    block,
                    instr,
                })
            }
            _ => None,
        }
    }

    /// Resolves an array index to an `ArrayIndex` abstraction.
    fn resolve_array_index(index_var: SsaVarId) -> ArrayIndex {
        // For now, treat all variable indices as unknown
        // Could be improved with constant propagation
        ArrayIndex::Variable(index_var)
    }

    /// Phase 2: Place memory phi nodes at dominance frontiers.
    fn place_memory_phis(&mut self, cfg: &SsaCfg<'_>) {
        let block_count = cfg.node_count();
        if block_count == 0 {
            return;
        }

        // Compute dominators and dominance frontiers
        let dom_tree = compute_dominators(cfg, cfg.entry());
        let frontiers = compute_dominance_frontiers(cfg, &dom_tree);

        // For each memory location, find blocks that define it (stores)
        let mut def_blocks: HashMap<MemoryLocation, HashSet<usize>> = HashMap::new();
        for op in &self.operations {
            if op.is_store() {
                def_blocks
                    .entry(op.location().clone())
                    .or_default()
                    .insert(op.block());
            }
        }

        // Standard phi placement algorithm (iterated dominance frontier)
        for (location, defs) in def_blocks {
            let mut phi_blocks: HashSet<usize> = HashSet::new();
            let mut worklist: VecDeque<usize> = defs.iter().copied().collect();
            let mut processed: HashSet<usize> = HashSet::new();

            while let Some(block) = worklist.pop_front() {
                if !processed.insert(block) {
                    continue;
                }

                let node_id = NodeId::new(block);
                if node_id.index() >= frontiers.len() {
                    continue;
                }

                for &frontier_node in &frontiers[node_id.index()] {
                    let frontier_block = frontier_node.index();
                    if phi_blocks.insert(frontier_block) {
                        // Add phi at frontier
                        let version = self.allocate_version(&location);
                        let phi = MemoryPhi::new(location.clone(), version);
                        self.memory_phis
                            .entry(frontier_block)
                            .or_default()
                            .push(phi);
                        self.definitions.insert(
                            MemoryVersion::new(location.clone(), version),
                            MemoryDefSite::Phi {
                                block: frontier_block,
                            },
                        );
                        worklist.push_back(frontier_block);
                    }
                }
            }
        }
    }

    /// Phase 3: Rename memory versions using dominator tree traversal.
    fn rename_memory_versions(&mut self, ssa: &SsaFunction, cfg: &SsaCfg<'_>) {
        let block_count = cfg.node_count();
        if block_count == 0 {
            return;
        }

        // Compute dominators for traversal order
        let dom_tree = compute_dominators(cfg, cfg.entry());

        // Stack of versions for each location
        let mut version_stacks: HashMap<MemoryLocation, Vec<u32>> = HashMap::new();

        // Initialize all locations with version 0 (entry version)
        let locations: Vec<_> = self.locations.iter().cloned().collect();
        for location in locations {
            let entry_version = self.allocate_version(&location);
            version_stacks
                .entry(location.clone())
                .or_default()
                .push(entry_version);
            self.definitions.insert(
                MemoryVersion::new(location, entry_version),
                MemoryDefSite::Entry,
            );
        }

        // Rename in dominator tree order (preorder)
        let mut visited = vec![false; block_count];
        let mut worklist = vec![cfg.entry().index()];

        while let Some(block_idx) = worklist.pop() {
            if visited[block_idx] {
                continue;
            }
            visited[block_idx] = true;

            self.rename_block(block_idx, ssa, cfg, &mut version_stacks);

            // Add dominated blocks to worklist
            for child in dom_tree.children(NodeId::new(block_idx)) {
                if !visited[child.index()] {
                    worklist.push(child.index());
                }
            }
        }
    }

    /// Renames memory versions within a single block.
    fn rename_block(
        &mut self,
        block_idx: usize,
        ssa: &SsaFunction,
        cfg: &SsaCfg<'_>,
        version_stacks: &mut HashMap<MemoryLocation, Vec<u32>>,
    ) {
        // Record entry versions
        for location in self.locations.clone() {
            if let Some(&version) = version_stacks.get(&location).and_then(|s| s.last()) {
                self.entry_versions
                    .insert((location.clone(), block_idx), version);
            }
        }

        // Process memory phi nodes - they define new versions
        if let Some(phis) = self.memory_phis.get(&block_idx).cloned() {
            for phi in phis {
                version_stacks
                    .entry(phi.location.clone())
                    .or_default()
                    .push(phi.result_version);
            }
        }

        // Process instructions in the block
        let Some(block) = ssa.block(block_idx) else {
            return;
        };

        for (instr_idx, instr) in block.instructions().iter().enumerate() {
            // Handle stores - create new version
            if let Some(mem_op) = Self::classify_memory_operation(instr.op(), block_idx, instr_idx)
            {
                if mem_op.is_store() {
                    let location = mem_op.location().clone();
                    let new_version = self.allocate_version(&location);
                    version_stacks
                        .entry(location.clone())
                        .or_default()
                        .push(new_version);
                    self.definitions.insert(
                        MemoryVersion::new(location, new_version),
                        MemoryDefSite::Store {
                            block: block_idx,
                            instr: instr_idx,
                        },
                    );
                }
            }
        }

        // Record exit versions
        for location in self.locations.clone() {
            if let Some(&version) = version_stacks.get(&location).and_then(|s| s.last()) {
                self.exit_versions
                    .insert((location.clone(), block_idx), version);
            }
        }

        // Fill in phi operands for successors
        for succ_id in cfg.successors(NodeId::new(block_idx)) {
            let succ_idx = succ_id.index();
            if let Some(phis) = self.memory_phis.get_mut(&succ_idx) {
                for phi in phis {
                    if let Some(&version) = version_stacks.get(&phi.location).and_then(|s| s.last())
                    {
                        phi.add_operand(block_idx, version);
                    }
                }
            }
        }
    }

    /// Returns statistics about the Memory SSA.
    #[must_use]
    pub fn stats(&self) -> MemorySsaStats {
        let total_phis = self.memory_phis.values().map(Vec::len).sum();
        let store_count = self.operations.iter().filter(|op| op.is_store()).count();
        let load_count = self.operations.iter().filter(|op| op.is_load()).count();

        MemorySsaStats {
            location_count: self.locations.len(),
            memory_phi_count: total_phis,
            store_count,
            load_count,
            version_count: self.definitions.len(),
        }
    }
}

impl Default for MemorySsa {
    fn default() -> Self {
        Self::new()
    }
}

/// Statistics about Memory SSA.
#[derive(Debug, Clone, Copy)]
pub struct MemorySsaStats {
    /// Number of unique memory locations tracked.
    pub location_count: usize,
    /// Number of memory phi nodes placed.
    pub memory_phi_count: usize,
    /// Number of store operations.
    pub store_count: usize,
    /// Number of load operations.
    pub load_count: usize,
    /// Total number of memory versions.
    pub version_count: usize,
}

/// Memory state tracker for path-aware evaluation.
///
/// This tracks the memory values along a specific execution path, enabling
/// precise tracking of memory contents during symbolic or concrete evaluation.
#[derive(Debug, Clone)]
pub struct MemoryState {
    /// Current memory values: location -> (version, value as SSA variable).
    values: HashMap<MemoryLocation, (u32, SsaVarId)>,
    /// Reference to the Memory SSA for version lookups.
    mem_ssa: Option<std::sync::Arc<MemorySsa>>,
}

impl MemoryState {
    /// Creates a new empty memory state.
    #[must_use]
    pub fn new() -> Self {
        Self {
            values: HashMap::new(),
            mem_ssa: None,
        }
    }

    /// Creates a memory state with a reference to Memory SSA.
    #[must_use]
    pub fn with_mem_ssa(mem_ssa: std::sync::Arc<MemorySsa>) -> Self {
        Self {
            values: HashMap::new(),
            mem_ssa: Some(mem_ssa),
        }
    }

    /// Records a memory store.
    pub fn store(&mut self, location: MemoryLocation, value: SsaVarId, version: u32) {
        self.values.insert(location, (version, value));
    }

    /// Loads from a memory location.
    ///
    /// Returns the SSA variable holding the value, if known.
    #[must_use]
    pub fn load(&self, location: &MemoryLocation) -> Option<SsaVarId> {
        // Direct match
        if let Some((_, value)) = self.values.get(location) {
            return Some(*value);
        }

        // Check for aliasing locations
        for (loc, (_, value)) in &self.values {
            if location.must_alias(loc) {
                return Some(*value);
            }
        }

        None
    }

    /// Returns the current version for a location, if known.
    #[must_use]
    pub fn version(&self, location: &MemoryLocation) -> Option<u32> {
        self.values.get(location).map(|(v, _)| *v)
    }

    /// Checks if any stored location may alias the given location.
    #[must_use]
    pub fn has_may_alias(&self, location: &MemoryLocation) -> bool {
        self.values.keys().any(|loc| loc.may_alias(location))
    }

    /// Clears all memory state.
    pub fn clear(&mut self) {
        self.values.clear();
    }

    /// Returns the number of tracked locations.
    #[must_use]
    pub fn len(&self) -> usize {
        self.values.len()
    }

    /// Returns `true` if no memory is being tracked.
    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.values.is_empty()
    }
}

impl Default for MemoryState {
    fn default() -> Self {
        Self::new()
    }
}

/// Alias analysis result for a pair of memory locations.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum AliasResult {
    /// The locations definitely do not alias.
    NoAlias,
    /// The locations may alias (conservative).
    MayAlias,
    /// The locations definitely alias (same memory).
    MustAlias,
}

/// Performs alias analysis between two memory locations.
#[must_use]
pub fn analyze_alias(loc1: &MemoryLocation, loc2: &MemoryLocation) -> AliasResult {
    if loc1.must_alias(loc2) {
        AliasResult::MustAlias
    } else if loc1.may_alias(loc2) {
        AliasResult::MayAlias
    } else {
        AliasResult::NoAlias
    }
}

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

    #[test]
    fn test_memory_location_static_field_alias() {
        let field1 = FieldRef::new(crate::metadata::token::Token::new(0x04000001));
        let field2 = FieldRef::new(crate::metadata::token::Token::new(0x04000002));

        let loc1 = MemoryLocation::StaticField(field1);
        let loc2 = MemoryLocation::StaticField(field1);
        let loc3 = MemoryLocation::StaticField(field2);

        assert!(loc1.must_alias(&loc2));
        assert!(loc1.may_alias(&loc2));
        assert!(!loc1.may_alias(&loc3));
    }

    #[test]
    fn test_memory_location_instance_field_alias() {
        let field = FieldRef::new(crate::metadata::token::Token::new(0x04000001));
        let obj1 = SsaVarId::new();
        let obj2 = SsaVarId::new();

        let loc1 = MemoryLocation::InstanceField(obj1, field);
        let loc2 = MemoryLocation::InstanceField(obj1, field);
        let loc3 = MemoryLocation::InstanceField(obj2, field);

        assert!(loc1.must_alias(&loc2));
        assert!(loc1.may_alias(&loc2));
        assert!(!loc1.may_alias(&loc3)); // Different objects
    }

    #[test]
    fn test_array_index_overlap() {
        let idx1 = ArrayIndex::Constant(5);
        let idx2 = ArrayIndex::Constant(5);
        let idx3 = ArrayIndex::Constant(10);
        let idx4 = ArrayIndex::Unknown;

        assert!(idx1.may_overlap(&idx2));
        assert!(idx1.must_equal(&idx2));
        assert!(!idx1.may_overlap(&idx3));
        assert!(idx1.may_overlap(&idx4)); // Unknown overlaps everything
    }

    #[test]
    fn test_memory_location_array_element_alias() {
        let arr = SsaVarId::new();
        let idx1 = ArrayIndex::Constant(5);
        let idx2 = ArrayIndex::Constant(5);
        let idx3 = ArrayIndex::Constant(10);

        let loc1 = MemoryLocation::ArrayElement(arr, idx1);
        let loc2 = MemoryLocation::ArrayElement(arr, idx2);
        let loc3 = MemoryLocation::ArrayElement(arr, idx3);

        assert!(loc1.must_alias(&loc2));
        assert!(!loc1.may_alias(&loc3));
    }

    #[test]
    fn test_memory_location_unknown_alias() {
        let field = FieldRef::new(crate::metadata::token::Token::new(0x04000001));
        let loc1 = MemoryLocation::Unknown;
        let loc2 = MemoryLocation::StaticField(field);

        assert!(loc1.may_alias(&loc2)); // Unknown aliases everything
        assert!(!loc1.must_alias(&loc2)); // But doesn't must-alias
    }

    #[test]
    fn test_alias_result() {
        let field = FieldRef::new(crate::metadata::token::Token::new(0x04000001));
        let loc1 = MemoryLocation::StaticField(field);
        let loc2 = MemoryLocation::StaticField(field);

        assert_eq!(analyze_alias(&loc1, &loc2), AliasResult::MustAlias);

        let arr1 = SsaVarId::new();
        let arr2 = SsaVarId::new();
        let loc3 = MemoryLocation::ArrayElement(arr1, ArrayIndex::Constant(0));
        let loc4 = MemoryLocation::ArrayElement(arr2, ArrayIndex::Constant(0));

        assert_eq!(analyze_alias(&loc3, &loc4), AliasResult::NoAlias);
    }

    #[test]
    fn test_memory_state() {
        let mut state = MemoryState::new();
        let field = FieldRef::new(crate::metadata::token::Token::new(0x04000001));
        let loc = MemoryLocation::StaticField(field);
        let value = SsaVarId::new();

        state.store(loc.clone(), value, 1);
        assert_eq!(state.load(&loc), Some(value));
        assert_eq!(state.version(&loc), Some(1));
        assert_eq!(state.len(), 1);

        state.clear();
        assert!(state.is_empty());
    }

    #[test]
    fn test_memory_phi() {
        let field = FieldRef::new(crate::metadata::token::Token::new(0x04000001));
        let loc = MemoryLocation::StaticField(field);

        let mut phi = MemoryPhi::new(loc.clone(), 2);
        phi.add_operand(0, 0);
        phi.add_operand(1, 1);

        assert_eq!(phi.result_version, 2);
        assert_eq!(phi.operands.len(), 2);
        assert_eq!(phi.operand_from(0).unwrap().version, 0);
        assert_eq!(phi.operand_from(1).unwrap().version, 1);
        assert!(phi.operand_from(2).is_none());
    }

    #[test]
    fn test_memory_op() {
        let field = FieldRef::new(crate::metadata::token::Token::new(0x04000001));
        let loc = MemoryLocation::StaticField(field);
        let dest = SsaVarId::new();
        let value = SsaVarId::new();

        let load = MemoryOp::Load {
            location: loc.clone(),
            dest,
            block: 0,
            instr: 5,
        };
        assert!(load.is_load());
        assert!(!load.is_store());
        assert_eq!(load.block(), 0);
        assert_eq!(load.instr(), 5);

        let store = MemoryOp::Store {
            location: loc,
            value,
            block: 1,
            instr: 3,
        };
        assert!(!store.is_load());
        assert!(store.is_store());
    }
}