vlk-analysis 0.3.0

Lifting, SSA and simplification passes of the vlk decompiler
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
//! Indirect calls whose callee the binary fixes. A forward pass over the
//! lifted blocks follows what each register and each word of memory the
//! function stores holds: a constant, or an unknown value plus an offset,
//! named for where it came from. A call whose address comes out as a known
//! function's becomes a direct call to it, so the callee's summary gives
//! the arguments, and keeps the table it read in a comment. One that loads
//! the address from a slot of the table the object starts with is marked
//! with the slot, and the functions the C++ tables hold there.

use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet};

use vlk_ir::ir::{BinOp, Block, Expr, Stmt, Table, Via};
use vlk_loader::loader::Image;
use vlk_program::program::Program;
use vlk_types::types::Convention;

/// Where an unknown value comes from.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum Base {
    /// A register as the function received it.
    Entry(u16),
    /// A register on entry to a block whose predecessors leave different
    /// values in it.
    Join(u32, u16),
    /// What a statement loads, by block, statement and which of its
    /// loads, or what a call returns.
    Def(u32, u32, u32),
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub enum Value {
    Unknown,
    /// A constant, with the table and offset it was read from when
    /// constant data holds it.
    Const(u64, Option<(u64, u64)>),
    Sym(Base, u64),
    /// The function an import's slot holds, by the slot.
    Import(u64),
}

impl Value {
    /// The address as a key, whatever it was read from.
    fn key(self) -> Value {
        match self {
            Value::Const(value, _) => Value::Const(value, None),
            other => other,
        }
    }
}

/// What the registers the state does not list hold.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum Rest {
    Entry,
    Join(u32),
    Unknown,
}

#[derive(Debug, Clone, PartialEq)]
struct State {
    regs: BTreeMap<u16, Value>,
    rest: Rest,
    /// The words the function stored, by address and width in bytes.
    memory: BTreeMap<(Value, u8), Value>,
}

impl State {
    fn entry() -> Self {
        State {
            regs: BTreeMap::new(),
            rest: Rest::Entry,
            memory: BTreeMap::new(),
        }
    }

    fn reg(&self, index: u16) -> Value {
        if let Some(value) = self.regs.get(&index) {
            return *value;
        }
        match self.rest {
            Rest::Entry => Value::Sym(Base::Entry(index), 0),
            Rest::Join(block) => Value::Sym(Base::Join(block, index), 0),
            Rest::Unknown => Value::Unknown,
        }
    }

    /// The state on entry to `block` from two predecessors: what both
    /// leave the same, and a name of its own for each register they do not.
    fn meet(&self, other: &State, block: u32) -> State {
        let rest = match (self.rest, other.rest) {
            (a, b) if a == b && a != Rest::Unknown => a,
            _ => Rest::Join(block),
        };
        let mut merged = State {
            regs: BTreeMap::new(),
            rest,
            memory: BTreeMap::new(),
        };
        let indices: BTreeSet<u16> = self.regs.keys().chain(other.regs.keys()).copied().collect();
        for index in indices {
            let value = match (self.reg(index), other.reg(index)) {
                (Value::Const(a, from_a), Value::Const(b, from_b)) if a == b => {
                    Value::Const(a, if from_a == from_b { from_a } else { None })
                }
                (a, b) if a == b && a != Value::Unknown => a,
                _ => Value::Sym(Base::Join(block, index), 0),
            };
            if value != merged.reg(index) {
                merged.regs.insert(index, value);
            }
        }
        for (key, value) in &self.memory {
            if other.memory.get(key) == Some(value) {
                merged.memory.insert(*key, *value);
            }
        }
        merged
    }
}

/// What the pass decides for one call.
enum Decision {
    Direct(u64, Via),
    /// A call to the import whose slot the address was loaded from.
    Import(u64),
    Slot(Via),
}

struct Tracker<'a> {
    program: &'a Program,
    sp: u16,
    /// The address each named load reads.
    loads: std::cell::RefCell<HashMap<Base, Value>>,
    /// Calls to constructors leave their table in the object.
    constructors: bool,
    returns: Option<std::cell::RefCell<HashMap<u64, Option<u64>>>>,
}

impl Tracker<'_> {
    fn pointer_value(&self, value: Value) -> Value {
        if vlk_arch::arch::pointer_bytes(self.program.image.isa) != 4 {
            return value;
        }
        match value {
            Value::Const(value, from) => Value::Const(u64::from(value as u32), from),
            Value::Sym(base, offset) => Value::Sym(base, u64::from(offset as u32)),
            other => other,
        }
    }

    fn returned(&self, target: u64, convention: Convention) -> Option<u64> {
        let Some(returns) = self.returns.as_ref() else {
            return None;
        };
        if crate::lift::convention_of(&self.program.image, target) != convention {
            return None;
        }
        if let Some(known) = returns.borrow().get(&target) {
            return *known;
        }
        let found = returned_by(self.program, target);
        returns.borrow_mut().insert(target, found);
        found
    }

    /// Whether a value is the address of a table of code, which the C++
    /// object model lets no call or store through another pointer change
    /// once the object holds it.
    fn durable(&self, value: &Value) -> bool {
        matches!(value, Value::Const(table, _)
            if read_only_word(&self.program.image, *table)
                .is_some_and(|word| self.program.image.functions.contains_key(&word)))
    }

    fn on_stack(&self, value: Value) -> bool {
        matches!(value, Value::Sym(Base::Entry(index), _) if index == self.sp)
    }

    /// Whether a store of `width` bytes at `addr` may change the stored
    /// `stored_width` bytes at `stored`.
    fn may_alias(&self, stored: Value, stored_width: u8, addr: Value, width: u8) -> bool {
        let overlap = |a: u64, b: u64| {
            let distance = if vlk_arch::arch::pointer_bytes(self.program.image.isa) == 4 {
                i64::from((a as u32).wrapping_sub(b as u32) as i32)
            } else {
                a.wrapping_sub(b) as i64
            };
            distance < i64::from(width) && distance > -i64::from(stored_width)
        };
        match (stored, addr) {
            (Value::Sym(a, x), Value::Sym(b, y)) if a == b => overlap(x, y),
            (Value::Const(x, _), Value::Const(y, _)) => overlap(x, y),
            (_, Value::Unknown) => true,
            // The frame is the function's own: no pointer it was given
            // reaches it.
            (a, b) => self.on_stack(a) == self.on_stack(b),
        }
    }

    fn narrow(&self, value: Value, width: u8) -> Value {
        match (value, width) {
            (value, 64) => value,
            (Value::Const(value, _), width) => {
                Value::Const(value & vlk_ir::ir::semantics::mask(u32::from(width)), None)
            }
            (value @ Value::Sym(..), 32)
                if vlk_arch::arch::pointer_bytes(self.program.image.isa) == 4 =>
            {
                self.pointer_value(value)
            }
            _ => Value::Unknown,
        }
    }

    fn eval(&self, state: &State, expr: &Expr, site: (u32, u32), loads: &mut u32) -> Value {
        match expr {
            Expr::Reg { index, width } => self.narrow(state.reg(*index), *width),
            Expr::Const(value) => Value::Const(*value, None),
            Expr::Bin { op, lhs, rhs } => {
                let a = self.eval(state, lhs, site, loads);
                let b = self.eval(state, rhs, site, loads);
                match (op, a, b) {
                    (BinOp::Add, Value::Sym(base, offset), Value::Const(value, _))
                    | (BinOp::Add, Value::Const(value, _), Value::Sym(base, offset)) => {
                        Value::Sym(base, offset.wrapping_add(value))
                    }
                    (BinOp::Sub, Value::Sym(base, offset), Value::Const(value, _)) => {
                        Value::Sym(base, offset.wrapping_sub(value))
                    }
                    (BinOp::And, value, Value::Const(0xffff_ffff, _))
                    | (BinOp::And, Value::Const(0xffff_ffff, _), value)
                        if vlk_arch::arch::pointer_bytes(self.program.image.isa) == 4 =>
                    {
                        self.pointer_value(value)
                    }
                    (_, Value::Const(a, _), Value::Const(b, _)) => {
                        match Expr::bin(*op, Expr::Const(a), Expr::Const(b)) {
                            Expr::Const(value) => Value::Const(value, None),
                            _ => Value::Unknown,
                        }
                    }
                    _ => Value::Unknown,
                }
            }
            Expr::Select {
                cond,
                lhs,
                rhs,
                then,
                els,
            } if self.program.image.isa == vlk_base::target::Isa::Arm32 => {
                match (
                    self.eval(state, lhs, site, loads),
                    self.eval(state, rhs, site, loads),
                ) {
                    (Value::Const(a, _), Value::Const(b, _)) => {
                        let bits = vlk_ir::ir::semantics::operand_width(lhs, rhs);
                        let value = if vlk_ir::ir::semantics::holds(*cond, a, b, bits) {
                            then
                        } else {
                            els
                        };
                        self.eval(state, value, site, loads)
                    }
                    _ => Value::Unknown,
                }
            }
            Expr::Load { addr, width, .. } => {
                let (table, offset) = match addr.as_ref() {
                    Expr::Bin {
                        op: BinOp::Add,
                        lhs,
                        rhs,
                    } if let Expr::Const(offset) = rhs.as_ref() => {
                        (self.eval(state, lhs, site, loads), *offset)
                    }
                    other => (self.eval(state, other, site, loads), 0),
                };
                let addr = self.pointer_value(match table {
                    Value::Const(table, _) => Value::Const(table.wrapping_add(offset), None),
                    Value::Sym(base, at) => Value::Sym(base, at.wrapping_add(offset)),
                    Value::Unknown | Value::Import(_) => Value::Unknown,
                });
                let name = Base::Def(site.0, site.1, *loads);
                *loads += 1;
                self.loads.borrow_mut().insert(name, addr);
                if *width == vlk_arch::arch::pointer_bytes(self.program.image.isa) {
                    if let Value::Const(slot, _) = addr
                        && self.program.image.imports.contains_key(&slot)
                        && self
                            .program
                            .image
                            .import_addends
                            .get(&slot)
                            .is_none_or(|addend| *addend == 0)
                    {
                        return Value::Import(slot);
                    }
                    if let Some(value) = state.memory.get(&(addr.key(), *width)) {
                        return *value;
                    }
                    if let (Value::Const(at, _), Value::Const(table, _)) = (addr, table)
                        && let Some(word) = read_only_word(&self.program.image, at)
                    {
                        return Value::Const(word, Some((table, offset)));
                    }
                }
                Value::Sym(name, 0)
            }
            _ => Value::Unknown,
        }
    }

    /// Keeps of memory what a store of `width` bytes at `addr` leaves as
    /// it was; with no address, what a call or an unmodeled instruction
    /// leaves.
    fn forget(&self, state: &mut State, addr: Option<(Value, u8)>) {
        state
            .memory
            .retain(|(stored, stored_width), value| match addr {
                Some((addr, width)) => {
                    let same_object = match (stored, addr) {
                        (Value::Sym(a, _), Value::Sym(b, _)) => *a == b,
                        (Value::Const(..), Value::Const(..)) => true,
                        _ => false,
                    };
                    !self.may_alias(*stored, *stored_width, addr, width)
                        || (!same_object && self.durable(value))
                }
                None => !self.on_stack(*stored) && self.durable(value),
            });
    }

    fn step(&self, state: &mut State, stmt: &Stmt, site: (u32, u32)) {
        let mut loads = 0;
        match stmt {
            Stmt::Machine {
                instruction,
                outputs,
                ..
            } => {
                if matches!(
                    instruction,
                    vlk_ir::ir::MachineInstruction::Atomic { .. }
                        | vlk_ir::ir::MachineInstruction::ArmBarrier { .. }
                        | vlk_ir::ir::MachineInstruction::ArmLoadAcquire { .. }
                        | vlk_ir::ir::MachineInstruction::ArmExclusive { .. }
                        | vlk_ir::ir::MachineInstruction::ArmCacheZero
                ) {
                    state.memory.clear();
                }
                for output in outputs {
                    state.regs.insert(output.index, Value::Unknown);
                }
            }
            Stmt::Assign { dest, value, .. } => {
                let value = self.narrow(self.eval(state, value, site, &mut loads), dest.width);
                let value = if dest.index < 16 {
                    self.pointer_value(value)
                } else {
                    value
                };
                state.regs.insert(dest.index, value);
            }
            Stmt::Store {
                addr, value, width, ..
            } => {
                let addr = self.pointer_value(self.eval(state, addr, site, &mut loads));
                let value = self.eval(state, value, site, &mut loads);
                let value = if *width == 4 {
                    self.pointer_value(value)
                } else {
                    value
                };
                self.forget(state, Some((addr, *width)));
                if addr != Value::Unknown && value != Value::Unknown {
                    state.memory.insert((addr.key(), *width), value);
                }
            }
            Stmt::Call {
                target, convention, ..
            } => {
                let first = convention
                    .arguments()
                    .first()
                    .map(|register| state.reg(*register));
                let physical = stmt.call_clobbers();
                let primary = convention.results().first();
                let returned = target.and_then(|target| {
                    stmt.primary_call_output()
                        .filter(|output| {
                            output.dest.width == 64 && physical.contains(&output.register)
                        })
                        .and_then(|_| self.returned(target, *convention))
                });
                let outputs: Vec<_> = stmt
                    .call_outputs()
                    .iter()
                    .map(|output| {
                        let value = if !physical.contains(&output.register) {
                            state.reg(output.register)
                        } else if Some(&output.register) == primary
                            && output.dest.width == 64
                            && let Some(value) = returned
                        {
                            Value::Const(value, None)
                        } else {
                            Value::Sym(
                                Base::Def(site.0, site.1, u32::MAX - u32::from(output.register)),
                                0,
                            )
                        };
                        let value = match (value, output.dest.width) {
                            (value, 64) => value,
                            (Value::Const(value, _), width) => Value::Const(
                                value & vlk_ir::ir::semantics::mask(u32::from(width)),
                                None,
                            ),
                            _ => Value::Unknown,
                        };
                        (output.dest.index, value)
                    })
                    .collect();
                self.forget(state, None);
                for register in vlk_ir::ir::visit::statement_registers(stmt).0 {
                    state.regs.insert(register, Value::Unknown);
                }
                state.regs.extend(outputs);
                if self.constructors
                    && let (Some(target), Some(object)) = (target, first)
                    && object != Value::Unknown
                    && let Some(table) = installed_table(self.program, *target)
                {
                    state
                        .memory
                        .insert((object.key(), 8), Value::Const(table, None));
                }
            }
            Stmt::Unsupported { writes, .. } => {
                self.forget(state, None);
                match writes {
                    Some(writes) => {
                        for register in writes {
                            state.regs.insert(*register, Value::Unknown);
                        }
                    }
                    None => {
                        state.regs.clear();
                        state.rest = Rest::Unknown;
                    }
                }
            }
            _ => {}
        }
    }

    /// The state on entry to each block, `None` for one no path reaches.
    /// A block ending in a call that unwinds into a landing pad goes there
    /// too.
    fn solve(&self, blocks: &[Block], pads: &[(u64, Vec<(u64, u64)>)]) -> Vec<Option<State>> {
        let mut entry: Vec<Option<State>> = vec![None; blocks.len()];
        if blocks.is_empty() {
            return entry;
        }
        entry[0] = Some(State::entry());
        let mut pending = vec![0];
        // Blocks only an exception or an unresolved jump reaches start
        // knowing nothing, once the rest has settled.
        let mut unreached = (1..blocks.len()).rev();
        loop {
            while let Some(index) = pending.pop() {
                let mut state = entry[index].clone().expect("a reached block");
                for (position, stmt) in blocks[index].stmts.iter().enumerate() {
                    self.step(&mut state, stmt, (index as u32, position as u32));
                }
                for next in successors(blocks, pads, index) {
                    let merged = match &entry[next] {
                        Some(old) => old.meet(&state, next as u32),
                        None => state.clone(),
                    };
                    if entry[next].as_ref() != Some(&merged) {
                        entry[next] = Some(merged);
                        pending.push(next);
                    }
                }
            }
            let Some(index) = unreached.find(|index| entry[*index].is_none()) else {
                return entry;
            };
            entry[index] = Some(State {
                regs: BTreeMap::new(),
                rest: Rest::Join(index as u32),
                memory: BTreeMap::new(),
            });
            pending.push(index);
        }
    }
}

fn successors(blocks: &[Block], pads: &[(u64, Vec<(u64, u64)>)], index: usize) -> Vec<usize> {
    let mut out = vlk_ir::ir::cfg::block_successors(blocks, index);
    let start = blocks[index].start;
    let end = blocks.get(index + 1).map_or(u64::MAX, |next| next.start);
    for (pad, sites) in pads {
        if sites
            .iter()
            .any(|(from, to)| start < *to && *to <= end && *from < end)
            && let Some(position) = blocks.iter().position(|block| block.start == *pad)
            && !out.contains(&position)
        {
            out.push(position);
        }
    }
    out
}

/// Constant indirect destinations proved from statements in the same block.
/// Unknown entry values prevent assumptions about unresolved incoming edges.
pub(crate) fn constant_branch_targets(blocks: &[Block], program: &Program) -> HashMap<u64, u64> {
    let image = &program.image;
    let tracker = Tracker {
        program,
        sp: vlk_arch::arch::stack_pointer(image.isa),
        loads: Default::default(),
        constructors: false,
        returns: None,
    };
    let mut targets = HashMap::new();
    for (index, block) in blocks.iter().enumerate() {
        let mut state = State {
            regs: BTreeMap::new(),
            rest: Rest::Unknown,
            memory: BTreeMap::new(),
        };
        for (position, stmt) in block.stmts.iter().enumerate() {
            let site = (index as u32, position as u32);
            if let Stmt::BrIndirect { target, .. } = stmt
                && let Value::Const(target, _) = tracker.eval(&state, target, site, &mut 0)
            {
                targets.insert(block.start, target);
            }
            tracker.step(&mut state, stmt, site);
        }
    }
    targets
}

/// The word at `addr` in constant data.
fn read_only_word(image: &Image, addr: u64) -> Option<u64> {
    let Some(section) = image.section_at(addr) else {
        return None;
    };
    if !section.read_only {
        if image.isa == vlk_base::target::Isa::Arm32
            && let Some(Expr::Const(value)) =
                crate::lift::arm32_literal(image, &Expr::Const(addr), 4, false)
        {
            return Some(value);
        }
        return None;
    }
    match image.read_pointer(addr) {
        Some(vlk_loader::loader::Pointer::Address(address)) => Some(address),
        _ => None,
    }
}

fn is_code(image: &Image, word: Option<u64>) -> bool {
    word.is_some_and(|word| image.functions.contains_key(&word))
}

/// The address points of the C++ virtual tables: where the symbols say
/// they are, else where code takes the address of words of code after
/// the offset to the object's top and the type information.
fn vtables(program: &Program) -> &BTreeSet<u64> {
    let image = &program.image;
    image.vtables.get_or_init(|| {
        let mut found: BTreeSet<u64> = image
            .data_symbols
            .iter()
            .filter(|(_, name)| name.trim_start_matches('_').starts_with("ZTV"))
            .filter_map(|(addr, _)| addr.checked_add(16))
            .filter(|point| read_only_word(image, *point).is_some())
            .collect();
        for class in image.classes() {
            for table in &class.vtables {
                found.insert(table.address);
            }
        }
        if found.is_empty() && !image.go {
            let addressed = program
                .addressed
                .get_or_init(|| super::jump_table::referenced_addresses(image));
            found.extend(addressed.iter().copied().filter(|point| {
                let word = |offset: u64| read_only_word(image, point.wrapping_sub(offset));
                is_code(image, read_only_word(image, *point))
                    && word(16).is_some_and(|top| (-0x1_0000..=0).contains(&(top as i64)))
                    && word(8).is_some_and(|info| {
                        info == 0 || image.section_at(info).is_some_and(|s| !s.executable)
                    })
            }));
        }
        found
    })
}

/// The functions slot `slot` holds in the C++ tables long enough to have
/// it, but the handler a pure virtual slot holds, which does not return.
fn candidates(program: &Program, slot: u64) -> Vec<u64> {
    let image = &program.image;
    let mut found: Vec<u64> = vtables(program)
        .iter()
        .filter(|point| {
            (0..slot).all(|earlier| {
                read_only_word(image, *point + 8 * earlier)
                    .is_some_and(|word| word == 0 || image.functions.contains_key(&word))
            })
        })
        .filter_map(|point| read_only_word(image, point + 8 * slot))
        .filter(|word| image.functions.contains_key(word))
        .filter(|word| !super::summary(program, *word).noreturn)
        .collect();
    found.sort_unstable();
    found.dedup();
    found
}

/// Candidate sets larger than this are left out of the comment.
const MAX_CANDIDATES: usize = 4;

/// What kind of table the word at `table + offset` belongs to.
fn via(program: &Program, from: Option<(u64, u64)>) -> Via {
    let image = &program.image;
    let make = |table, slot| Via {
        table,
        slot,
        candidates: Vec::new(),
    };
    let Some((table, offset)) = from else {
        return make(Table::Register, 0);
    };
    let word = |at: u64| read_only_word(image, table.wrapping_add(at));
    let data = |word: Option<u64>| {
        word.is_some_and(|word| image.section_at(word).is_some_and(|s| !s.executable))
    };
    if vtables(program).contains(&table) && offset % 8 == 0 {
        return make(Table::Vtable, offset / 8);
    }
    let method = offset >= 0x18 && offset % 8 == 0;
    if image.go {
        if offset == 0 {
            return make(Table::FuncValue, 0);
        }
        if method && data(word(0)) && data(word(8)) {
            return make(Table::Itab, (offset - 0x18) / 8);
        }
    } else if method
        && word(0).is_some_and(|drop| drop == 0 || image.functions.contains_key(&drop))
        && word(8).is_some_and(|size| size < 1 << 32 && !image.functions.contains_key(&size))
        && word(16).is_some_and(|align| align.is_power_of_two() && align <= 1 << 16)
    {
        return make(Table::TraitObject, (offset - 0x18) / 8);
    }
    make(Table::Pointer, table.wrapping_add(offset))
}

/// The table the function at `addr` leaves at offset 0 of the object its
/// first argument points to, on every path that returns.
fn installed_table(program: &Program, addr: u64) -> Option<u64> {
    if let Some(known) = program.installs.lock().expect("installs lock").get(&addr) {
        return *known;
    }
    let found = find_installed_table(program, addr);
    program
        .installs
        .lock()
        .expect("installs lock")
        .insert(addr, found);
    found
}

/// Constructors are short; longer functions are not looked into.
const MAX_ANALYZED_FUNCTION_SIZE: u64 = 512;

fn find_installed_table(program: &Program, addr: u64) -> Option<u64> {
    let image = &program.image;
    let Some(function) = image.functions.get(&addr) else {
        return None;
    };
    let Some(size) = function
        .size
        .filter(|size| *size <= MAX_ANALYZED_FUNCTION_SIZE)
    else {
        return None;
    };
    let Some(blocks) = crate::lift::lift(program, addr, size, &HashSet::new()).ok() else {
        return None;
    };
    let Some(value) = crate::lift::convention_of(image, addr).arguments().first() else {
        return None;
    };
    let first = *value;
    let tracker = Tracker {
        program,
        sp: vlk_arch::arch::stack_pointer(image.isa),
        loads: Default::default(),
        constructors: false,
        returns: None,
    };
    let entry = tracker.solve(&blocks, &[]);
    let mut table = None;
    for (index, block) in blocks.iter().enumerate() {
        let Some(mut state) = entry[index].clone() else {
            continue;
        };
        for (position, stmt) in block.stmts.iter().enumerate() {
            if let Stmt::Ret { .. } = stmt {
                let left = state.memory.get(&(Value::Sym(Base::Entry(first), 0), 8));
                match (left, table) {
                    (Some(Value::Const(left, _)), None)
                        if tracker.durable(&Value::Const(*left, None)) =>
                    {
                        table = Some(*left)
                    }
                    (Some(Value::Const(left, _)), Some(table)) if *left == table => {}
                    _ => return None,
                }
            }
            tracker.step(&mut state, stmt, (index as u32, position as u32));
        }
    }
    table
}

fn returned_by(program: &Program, addr: u64) -> Option<u64> {
    let image = &program.image;
    let Some(function) = image.functions.get(&addr) else {
        return None;
    };
    if function.size_estimated {
        return None;
    }
    let Some(size) = function
        .size
        .filter(|size| *size != 0 && *size <= MAX_ANALYZED_FUNCTION_SIZE)
    else {
        return None;
    };
    let Some(end) = addr.checked_add(size) else {
        return None;
    };
    let convention = crate::lift::convention_of(image, addr);
    if !matches!(
        convention,
        Convention::SysV
            | Convention::Win64
            | Convention::Aapcs64
            | Convention::Lp64
            | Convention::GoAmd64
            | Convention::GoArm64
    ) {
        return None;
    }
    let Some(value) = convention.results().first() else {
        return None;
    };
    let primary = *value;
    let Some(blocks) = crate::lift::lift_shared(program, addr, size, &|_| Default::default()).ok()
    else {
        return None;
    };
    returned_from(program, addr, end, primary, &blocks)
}

fn returned_from(
    program: &Program,
    addr: u64,
    end: u64,
    primary: u16,
    blocks: &[Block],
) -> Option<u64> {
    let image = &program.image;
    let Some(first) = blocks.first() else {
        return None;
    };
    if first.start != addr
        || !closed_normal_flow(blocks, Edges::Forward)
        || blocks.iter().any(|block| {
            block.start < addr
                || block.start >= end
                || block.stmts.iter().any(|stmt| {
                    matches!(
                        stmt,
                        Stmt::Call { .. }
                            | Stmt::Store { .. }
                            | Stmt::Machine { .. }
                            | Stmt::Unsupported { .. }
                    )
                })
        })
    {
        return None;
    }
    let tracker = Tracker {
        program,
        sp: vlk_arch::arch::stack_pointer(image.isa),
        loads: Default::default(),
        constructors: false,
        returns: None,
    };
    let entry = tracker.solve(blocks, &[]);
    let mut returned = None;
    for (index, block) in blocks.iter().enumerate() {
        let Some(mut state) = entry[index].clone() else {
            return None;
        };
        for (position, stmt) in block.stmts.iter().enumerate() {
            if matches!(stmt, Stmt::Ret { .. }) {
                match (state.reg(primary), returned) {
                    (Value::Const(value, _), None)
                        if image.functions.contains_key(&value)
                            || image
                                .section_at(value)
                                .is_some_and(|section| section.read_only) =>
                    {
                        returned = Some(value);
                    }
                    (Value::Const(value, _), Some(known)) if value == known => {}
                    _ => return None,
                }
            }
            tracker.step(&mut state, stmt, (index as u32, position as u32));
        }
    }
    returned
}

#[derive(PartialEq)]
enum Edges {
    Forward,
    Any,
}

fn closed_normal_flow(blocks: &[Block], edges: Edges) -> bool {
    !blocks.is_empty()
        && blocks.iter().enumerate().all(|(index, block)| {
            if index != 0 && blocks[index - 1].start >= block.start {
                return false;
            }
            let Some((last, prefix)) = block.stmts.split_last() else {
                return false;
            };
            if prefix.iter().any(|stmt| {
                matches!(
                    stmt,
                    Stmt::Brif { .. }
                        | Stmt::BrifFlags { .. }
                        | Stmt::Br { .. }
                        | Stmt::BrIndirect { .. }
                        | Stmt::Switch { .. }
                        | Stmt::Ret { .. }
                        | Stmt::Trap { .. }
                )
            }) {
                return false;
            }
            let known = |target| {
                blocks.iter().enumerate().any(|(next, block)| {
                    (edges == Edges::Any || next > index) && block.start == target
                })
            };
            match last {
                Stmt::Ret { .. } | Stmt::Trap { .. } => true,
                Stmt::Br {
                    target: Some(target),
                    ..
                } => known(*target),
                Stmt::Brif { target, .. } | Stmt::BrifFlags { target, .. } => {
                    known(*target) && index + 1 < blocks.len()
                }
                Stmt::Switch {
                    cases,
                    default: Some(default),
                    ..
                } => known(*default) && cases.iter().all(|(_, target)| known(*target)),
                Stmt::Assign { .. }
                | Stmt::Store { .. }
                | Stmt::Call { .. }
                | Stmt::Machine { .. }
                | Stmt::Unsupported { .. } => index + 1 < blocks.len(),
                _ => false,
            }
        })
}

/// Turns the indirect calls whose callee the code fixes into direct calls,
/// and marks those through a slot of an object's table with the slot.
pub(crate) fn resolve(blocks: &mut [Block], pads: &[(u64, Vec<(u64, u64)>)], program: &Program) {
    let image = &program.image;
    let indirect = |stmt: &Stmt| {
        matches!(stmt, Stmt::Call { target: None, through: Some(through), convention, .. }
            if !stmt.has_declared_call()
                && !matches!(convention, Convention::SyscallI386 | Convention::SyscallX86 | Convention::SyscallArm | Convention::SyscallRiscv | Convention::SyscallMips)
                && vlk_prototypes::prototypes::import_name(image, through).is_none())
    };
    if !blocks.iter().flat_map(|block| &block.stmts).any(indirect) {
        return;
    }
    let tracker = Tracker {
        program,
        sp: vlk_arch::arch::stack_pointer(image.isa),
        loads: Default::default(),
        constructors: !vtables(program).is_empty(),
        returns: (pads.is_empty() && closed_normal_flow(blocks, Edges::Any)).then(Default::default),
    };
    let types = if image.classes().is_empty() {
        None
    } else {
        blocks.first().and_then(|block| {
            vlk_loader::loader::dwarf::virtual_calls::function_types(image, block.start)
        })
    };
    let entry = tracker.solve(blocks, pads);
    let mut decisions = Vec::new();
    for (index, block) in blocks.iter().enumerate() {
        let Some(mut state) = entry[index].clone() else {
            continue;
        };
        for (position, stmt) in block.stmts.iter().enumerate() {
            let site = (index as u32, position as u32);
            if indirect(stmt)
                && let Stmt::Call {
                    through: Some(through),
                    ..
                } = stmt
                && let Some(decision) = decide(&tracker, &state, through, site, types.as_ref())
            {
                decisions.push((index, position, decision));
            }
            tracker.step(&mut state, stmt, site);
        }
    }
    let summaries = |target| super::summary(program, target);
    for (index, position, decision) in decisions.into_iter().rev() {
        let stmts = &mut blocks[index].stmts;
        let Stmt::Call {
            kind,
            through: Some(through),
            ..
        } = &stmts[position]
        else {
            continue;
        };
        let kind = *kind;
        let through = through.clone();
        match decision {
            Decision::Direct(target, found) => {
                let mut call = crate::lift::call(kind, image, &summaries, Some(target));
                if image.isa == vlk_base::target::Isa::Mips32
                    && kind == vlk_types::types::CallKind::Invoke
                {
                    crate::lift::mips32::count_from_block_format(
                        image,
                        &summaries,
                        &stmts[..position],
                        &mut call,
                    );
                }
                if let Stmt::Call { via, .. } = &mut call {
                    *via = Some(Box::new(found));
                }
                stmts[position] = call;
                // A tail call's results go to this function's caller.
                if kind == vlk_types::types::CallKind::Invoke {
                    let results = crate::lift::stack_results(image, &summaries, target);
                    stmts.splice(position + 1..position + 1, results);
                    // What follows a call that does not return is not run.
                    if summaries(target).noreturn {
                        stmts.truncate(position + 1);
                        stmts.push(Stmt::Trap { at: None });
                    }
                }
                remove_call_target_assignments(stmts, position, &through);
            }
            Decision::Import(slot) => {
                let imported = Expr::Load {
                    addr: Box::new(Expr::Const(slot)),
                    width: vlk_arch::arch::pointer_bytes(image.isa),
                    signed: false,
                };
                stmts[position] = crate::lift::call_through(kind, image, imported);
                remove_call_target_assignments(stmts, position, &through);
            }
            Decision::Slot(found) => {
                let arity = common_arity(program, &found.candidates);
                if let Stmt::Call {
                    via,
                    args,
                    convention,
                    ..
                } = &mut stmts[position]
                {
                    if args.is_none() && arity.is_some() {
                        *args = crate::lift::arguments(kind, *convention, image.isa, arity, 0);
                    }
                    *via = Some(Box::new(found));
                }
            }
        }
    }
}

/// The number of register arguments every candidate takes, when they agree.
fn common_arity(program: &Program, candidates: &[u64]) -> Option<usize> {
    let mut arity = None;
    for candidate in candidates {
        let summary = super::summary(program, *candidate);
        if summary.stack != 0 || summary.floats.is_some_and(|floats| floats != 0) {
            return None;
        }
        match (arity, summary.arity) {
            (_, None) => return None,
            (None, found) => arity = found,
            (Some(known), Some(found)) if known != found => return None,
            _ => {}
        }
    }
    arity
}

/// Drops the statements that only computed the address an indirect call,
/// now direct, went through: each assignment of a register the address
/// read, last before the call, that nothing reads before the call or the
/// block's code after it writes the register again or returns.
fn remove_call_target_assignments(stmts: &mut Vec<Stmt>, mut position: usize, through: &Expr) {
    let mut pending = HashSet::new();
    vlk_ir::ir::visit::collect_reads(through, &mut pending);
    while let Some(register) = pending.iter().next().copied() {
        pending.remove(&register);
        let overwritten = match stmts[position + 1..]
            .iter()
            .map(vlk_ir::ir::visit::statement_registers)
            .find(|(writes, reads)| writes.contains(&register) || reads.contains(&register))
        {
            Some((_, reads)) => !reads.contains(&register),
            None => matches!(stmts.last(), Some(Stmt::Ret { .. } | Stmt::Trap { .. })),
        };
        if !vlk_ir::ir::visit::statement_registers(&stmts[position])
            .0
            .contains(&register)
            && !overwritten
        {
            continue;
        }
        let Some(at) = stmts[..position].iter().rposition(|stmt| {
            vlk_ir::ir::visit::statement_registers(stmt)
                .0
                .contains(&register)
        }) else {
            continue;
        };
        let Stmt::Assign { value, .. } = &stmts[at] else {
            continue;
        };
        let read_later = stmts[at + 1..=position].iter().any(|stmt| {
            vlk_ir::ir::visit::statement_registers(stmt)
                .1
                .contains(&register)
        });
        if read_later {
            continue;
        }
        vlk_ir::ir::visit::collect_reads(value, &mut pending);
        stmts.remove(at);
        position -= 1;
    }
}

fn decide(
    tracker: &Tracker<'_>,
    state: &State,
    through: &Expr,
    site: (u32, u32),
    types: Option<&vlk_loader::loader::dwarf::FunctionTypes>,
) -> Option<Decision> {
    let program = tracker.program;
    let image = &program.image;
    let mut loads = 0;
    match tracker.eval(state, through, site, &mut loads) {
        Value::Const(target, from) if image.functions.contains_key(&image.code_address(target)) => {
            Some(Decision::Direct(
                image.code_address(target),
                via(program, from),
            ))
        }
        Value::Import(slot) => Some(Decision::Import(slot)),
        // A slot of the table the object starts with.
        Value::Sym(name @ Base::Def(..), 0) if !vtables(program).is_empty() => {
            let loaded = tracker.loads.borrow();
            let Some(Value::Sym(table @ Base::Def(..), offset)) = loaded.get(&name).copied() else {
                return None;
            };
            if offset % 8 != 0 || !matches!(loaded.get(&table), Some(Value::Sym(_, 0))) {
                return None;
            }
            let slot = offset / 8;
            let mut candidates = candidates(program, slot);
            let mut typed = false;
            if let Some(Value::Sym(Base::Entry(register), 0)) = loaded.get(&table)
                && let Some(types) = types
                && !types.variadic
                && types.ret.as_ref().is_some_and(|ty| {
                    ty.is_pointer()
                        || ty.integer().is_some_and(|(bits, _)| bits <= 64)
                        || *ty == vlk_types::types::CType::Void
                })
                && types.params.iter().all(|(_, ty)| {
                    ty.as_ref().is_some_and(|ty| {
                        ty.is_pointer() || ty.integer().is_some_and(|(bits, _)| bits <= 64)
                    })
                })
                && let Ok(profile) = types.abi_profile
                && let Some(position) = super::abi::convention(profile)
                    .arguments()
                    .iter()
                    .position(|arg| arg == register)
                && let Some((_, Some(ty))) = types.params.get(position)
                && let Some(name) = ty.pointee_struct()
                && image.classes().iter().any(|class| class.name == name)
            {
                let targets =
                    vlk_loader::loader::classes::virtual_targets(image.classes(), name, slot);
                candidates.retain(|candidate| targets.contains(candidate));
                typed = true;
            }
            if !typed && candidates.len() > MAX_CANDIDATES {
                candidates.clear();
            }
            Some(Decision::Slot(Via {
                table: Table::Vtable,
                slot,
                candidates,
            }))
        }
        _ => None,
    }
}

#[cfg(test)]
mod tests;