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
//! x86-64 instructions that read and write only general registers and
//! flags, run by a small program built for x86-64 (`x86_64.c`). On an
//! `AArch64` Mac the system runs that program by translation.

use std::io::Write as _;
use std::path::PathBuf;
use std::process::{Command, Stdio};

use iced_x86::{
    Decoder, DecoderOptions, FlowControl, Instruction, InstructionInfoFactory, Mnemonic, OpKind,
    Register, RflagsBits,
};

use super::{
    Findings, branches_on_unknown_flags, condition_differs, float_bits, lifted,
    register_differences, same_float_bits, unsupported, value,
};
use vlk_base::target::Isa;
use vlk_eval::eval::{self, hash};

const RSP: u16 = 4;
const R15: u16 = 15;

/// The general registers a case may use: the runner keeps the stack in rsp
/// and its state pointer in r15.
fn usable_register_index(index: u16) -> bool {
    index != RSP && index != R15
}

fn register_name(index: u16) -> String {
    if index >= vlk_arch::arch::x86::XMM0 {
        return vlk_ir::ir::reg_name(vlk_base::target::Isa::X86_64, index, 64);
    }
    NAMES[usize::from(index)].to_owned()
}

const NAMES: [&str; 16] = [
    "rax", "rcx", "rdx", "rbx", "rsp", "rbp", "rsi", "rdi", "r8", "r9", "r10", "r11", "r12", "r13",
    "r14", "r15",
];

/// The buffer the runner maps for cases that address memory, and the flag
/// on a case's length that sends its contents.
const BUFFER: u64 = 0x6000_0000_0000;
const BUFFER_SIZE: usize = 8192;
const WITH_MEMORY: u32 = 0x8000_0000;

/// The flags a case starts from: carry, parity, adjust, zero, sign and
/// overflow, with the bit that is always set.
const ARITHMETIC_FLAGS: u64 = 0x1 | 0x4 | 0x10 | 0x40 | 0x80 | 0x800;

struct Case {
    code: Vec<u8>,
    registers: [u64; 16],
    flags: u64,
    /// xmm0 to xmm7, as halves, low first, then the upper halves of ymm0
    /// to ymm7 the same way.
    vectors: [u64; 32],
    /// The buffer's contents, for a case that reads or writes memory.
    memory: Option<Vec<u8>>,
}

struct Outcome {
    registers: [u64; 16],
    flags: u64,
    vectors: [u64; 32],
    memory: Option<Vec<u8>>,
    /// The signal the code raised, or zero.
    signal: u8,
}

/// Where a vector slot of the IR lies among the case's vector words: the
/// xmm halves first, then the ymm upper halves.
fn vector_word(index: u16) -> Option<usize> {
    use vlk_arch::arch::x86::{XMM0, YMM_UPPER};
    match index {
        _ if (XMM0..XMM0 + 16).contains(&index) => Some(usize::from(index - XMM0)),
        _ if (YMM_UPPER..YMM_UPPER + 16).contains(&index) => {
            Some(16 + usize::from(index - YMM_UPPER))
        }
        _ => None,
    }
}

/// The vector slots a case sets and compares: xmm0 to xmm7 and the upper
/// halves of ymm0 to ymm7.
fn vector_slots() -> impl Iterator<Item = u16> {
    use vlk_arch::arch::x86::{XMM0, YMM_UPPER};
    (XMM0..XMM0 + 16).chain(YMM_UPPER..YMM_UPPER + 16)
}

/// Builds the runner once for every test, or None when this machine has
/// no compiler for it. Test processes running at once each compile to a
/// file of their own and rename it into place, so none runs a runner
/// another is still writing.
fn runner() -> Option<Command> {
    static BUILT: std::sync::OnceLock<Option<PathBuf>> = std::sync::OnceLock::new();
    let translated = cfg!(target_arch = "aarch64");
    let built = BUILT.get_or_init(|| {
        let dir =
            PathBuf::from(concat!(env!("CARGO_MANIFEST_DIR"), "/../..")).join("target/hardware");
        if std::fs::create_dir_all(&dir).is_err() {
            return None;
        }
        let source = PathBuf::from(concat!(env!("CARGO_MANIFEST_DIR"), "/../.."))
            .join("crates/analysis/src/lift/hardware/x86_64.c");
        let binary = dir.join("x86_64_runner");
        let partial = dir.join(format!("x86_64_runner.{}", std::process::id()));
        let mut compile = Command::new(if translated { "clang" } else { "cc" });
        if translated {
            compile.args(["-arch", "x86_64"]);
        }
        let compiled = compile
            .arg("-O1")
            .arg("-o")
            .arg(&partial)
            .arg(&source)
            .status()
            .is_ok_and(|status| status.success());
        if !compiled || std::fs::rename(&partial, &binary).is_err() {
            std::fs::remove_file(&partial).ok();
            return None;
        }
        Some(binary)
    });
    let Some(binary) = built.clone() else {
        return None;
    };
    Some(if translated {
        let mut command = Command::new("arch");
        command.arg("-x86_64").arg(binary);
        command
    } else {
        Command::new(binary)
    })
}

fn run(mut runner: Command, cases: &[Case]) -> Vec<Outcome> {
    let mut input = Vec::new();
    for case in cases {
        let flag = if case.memory.is_some() {
            WITH_MEMORY
        } else {
            0
        };
        input.extend((case.code.len() as u32 | flag).to_le_bytes());
        input.extend(&case.code);
        for register in case.registers {
            input.extend(register.to_le_bytes());
        }
        input.extend(case.flags.to_le_bytes());
        for half in case.vectors {
            input.extend(half.to_le_bytes());
        }
        if let Some(memory) = &case.memory {
            input.extend(memory);
        }
    }
    let mut child = runner
        .stdin(Stdio::piped())
        .stdout(Stdio::piped())
        .spawn()
        .expect("starting the x86-64 runner");
    let mut stdin = child.stdin.take().expect("runner input");
    let writer = std::thread::spawn(move || stdin.write_all(&input));
    let output = child.wait_with_output().expect("running the x86-64 runner");
    writer.join().expect("writer").expect("writing cases");
    assert!(output.status.success(), "the x86-64 runner failed");
    let mut outcomes = Vec::new();
    let mut rest = output.stdout.as_slice();
    for case in cases {
        let state = 49 * 8;
        let size = state + case.memory.as_ref().map_or(0, Vec::len) + 1;
        let (chunk, tail) = rest.split_at(size);
        rest = tail;
        let word = |index: usize| {
            u64::from_le_bytes(chunk[index * 8..index * 8 + 8].try_into().expect("8 bytes"))
        };
        outcomes.push(Outcome {
            registers: std::array::from_fn(word),
            flags: word(16),
            vectors: std::array::from_fn(|half| word(17 + half)),
            memory: case
                .memory
                .as_ref()
                .map(|memory| chunk[state..state + memory.len()].to_vec()),
            signal: chunk[size - 1],
        });
    }
    assert!(rest.is_empty(), "the x86-64 runner wrote too much");
    outcomes
}

fn decode(code: &[u8]) -> Option<Instruction> {
    let mut decoder = Decoder::with_ip(64, code, 0, DecoderOptions::NONE);
    let instruction = decoder.decode();
    (!instruction.is_invalid()).then_some(instruction)
}

/// Whether `instruction` touches only usable general registers and
/// immediates, carries on to the next instruction, and needs no privilege.
fn register_only(
    instruction: &Instruction,
    instruction_info_factory: &mut InstructionInfoFactory,
) -> bool {
    // `movsxd` without REX.W is a plain move on the processor; the
    // translation that runs x86-64 code on AArch64 sign-extends it.
    if instruction.mnemonic() == Mnemonic::Movsxd && instruction.op0_register().size() != 8 {
        return false;
    }
    if instruction.flow_control() != FlowControl::Next
        || instruction.is_privileged()
        || matches!(
            instruction.mnemonic(),
            Mnemonic::Cpuid
                | Mnemonic::Rdtsc
                | Mnemonic::Rdtscp
                | Mnemonic::Rdrand
                | Mnemonic::Rdseed
                | Mnemonic::Rdpid
                | Mnemonic::Rdpmc
                | Mnemonic::Xgetbv
        )
    {
        return false;
    }
    let operands_plain = (0..instruction.op_count()).all(|index| {
        matches!(
            instruction.op_kind(index),
            OpKind::Register
                | OpKind::Immediate8
                | OpKind::Immediate8_2nd
                | OpKind::Immediate16
                | OpKind::Immediate32
                | OpKind::Immediate64
                | OpKind::Immediate8to16
                | OpKind::Immediate8to32
                | OpKind::Immediate8to64
                | OpKind::Immediate32to64
        )
    });
    let info = instruction_info_factory.info(instruction);
    operands_plain
        && info.used_memory().is_empty()
        && info
            .used_registers()
            .iter()
            .all(|used| usable_register(used.register()))
}

fn usable_register(register: Register) -> bool {
    (register.is_gpr() && !matches!(register.full_register(), Register::RSP | Register::R15))
        || ((register.is_xmm() || register.is_ymm()) && register.number() < 8)
}

/// A random instruction of up to 15 bytes, decoded from random bytes with an
/// operand-size or REX prefix now and then.
fn random_instruction(
    seed: u64,
    info: &mut InstructionInfoFactory,
) -> Option<(Vec<u8>, Instruction)> {
    // Scalar SSE: a mandatory prefix, an opcode of the floating-point
    // groups and a register-to-register operand byte.
    const SSE: [u8; 18] = [
        0x10, 0x11, 0x28, 0x2a, 0x2c, 0x2d, 0x2e, 0x2f, 0x51, 0x54, 0x57, 0x58, 0x59, 0x5a, 0x5c,
        0x5d, 0x5e, 0x5f,
    ];
    let mut bytes: Vec<u8> = (0..15)
        .map(|index| hash(seed, 0x40 + index) as u8)
        .collect();
    match hash(seed, 0x30) % 9 {
        // Integer and packed SSE: 66 0F with an opcode of those groups, a
        // register operand byte and an immediate for the ones that take one.
        // AVX on registers: VEX moves, bitwise and lane operations of 128
        // or 256 bits, the byte mask, the broadcast, and clearing the upper
        // halves.
        8 if hash(seed, 0x3f).is_multiple_of(3) => {
            const OPCODES: [(u8, u8); 18] = [
                (0x6f, 2),
                (0x6f, 1),
                (0xef, 1),
                (0xdb, 1),
                (0xeb, 1),
                (0xdf, 1),
                (0x74, 1),
                (0x75, 1),
                (0x76, 1),
                (0xfc, 1),
                (0xfe, 1),
                (0xd4, 1),
                (0xf8, 1),
                (0xfb, 1),
                (0xda, 1),
                (0xde, 1),
                (0xd7, 1),
                (0x77, 0),
            ];
            let (opcode, prefix) = OPCODES[(hash(seed, 0x3b) % OPCODES.len() as u64) as usize];
            let wide = !hash(seed, 0x3c).is_multiple_of(3);
            let source = (hash(seed, 0x3d) % 8) as u8;
            let modrm = 0xc0 | (hash(seed, 0x34) as u8 & 0x3f);
            let code: Vec<u8> = match (opcode, prefix) {
                // vzeroupper.
                (0x77, _) => vec![0xc5, 0xf8, 0x77],
                // vpmovmskb names no second source.
                (0xd7, _) => vec![0xc5, 0xf9 | if wide { 0x04 } else { 0 }, 0xd7, modrm],
                _ if hash(seed, 0x3e).is_multiple_of(8) => {
                    // vpbroadcastb from an xmm register.
                    vec![0xc4, 0xe2, 0x79 | if wide { 0x04 } else { 0 }, 0x78, modrm]
                }
                _ => {
                    let unused = if opcode == 0x6f { 0xf } else { !source & 0xf };
                    vec![
                        0xc5,
                        0x80 | (unused << 3) | if wide { 0x04 } else { 0 } | prefix,
                        opcode,
                        modrm,
                    ]
                }
            };
            bytes.splice(0..code.len(), code);
        }
        // AES rounds: 66 0F 38 DC to DF on two xmm registers.
        7 if hash(seed, 0x3e).is_multiple_of(4) => {
            let code = [
                0x66,
                0x0f,
                0x38,
                0xdc + (hash(seed, 0x3b) % 4) as u8,
                0xc0 | (hash(seed, 0x34) as u8 & 0x3f),
            ];
            bytes.splice(0..code.len(), code);
        }
        7 | 8 => {
            let opcode = match hash(seed, 0x3a) % 4 {
                0 => 0x60 + (hash(seed, 0x3b) % 0x20) as u8,
                1 => 0xd0 + (hash(seed, 0x3b) % 0x30) as u8,
                2 => 0x50 + (hash(seed, 0x3b) % 0x10) as u8,
                // pinsrw and pextrw.
                _ => 0xc4 + (hash(seed, 0x3b) % 2) as u8,
            };
            let prefix = if hash(seed, 0x3c).is_multiple_of(4) {
                0xf3
            } else {
                0x66
            };
            let code = [
                prefix,
                0x0f,
                opcode,
                0xc0 | (hash(seed, 0x34) as u8 & 0x3f),
                hash(seed, 0x3d) as u8,
            ];
            bytes.splice(0..code.len(), code);
        }
        // Bit scans and counts: bsf, bsr, and with F3 popcnt, tzcnt, lzcnt.
        6 => {
            let (prefix, opcode) = [
                (None, 0xbc),
                (None, 0xbd),
                (Some(0xf3), 0xb8),
                (Some(0xf3), 0xbc),
                (Some(0xf3), 0xbd),
                // bt, bts, btr and btc with a register bit number.
                (None, 0xa3),
                (None, 0xab),
                (None, 0xb3),
                (None, 0xbb),
            ][(hash(seed, 0x35) % 9) as usize];
            let mut code: Vec<u8> = prefix.into_iter().collect();
            if hash(seed, 0x36) & 1 == 1 {
                code.push(0x48 | (hash(seed, 0x37) as u8 & 0x05));
            }
            code.extend([0x0f, opcode, 0xc0 | (hash(seed, 0x34) as u8 & 0x3f)]);
            bytes.splice(0..code.len(), code);
        }
        0 => bytes[0] = 0x48 | (bytes[0] & 0x07),
        1 => bytes[0] = 0x66,
        // roundsd and roundss, with an immediate rounding.
        5 => {
            let opcode = [0x0a, 0x0b][(hash(seed, 0x38) & 1) as usize];
            let code = [
                0x66,
                0x0f,
                0x3a,
                opcode,
                0xc0 | (hash(seed, 0x34) as u8 & 0x3f),
                hash(seed, 0x39) as u8 & 0x07,
            ];
            bytes.splice(0..code.len(), code);
        }
        2 | 3 => {
            let prefix = [0xf2, 0xf3, 0x66, 0xf2][(hash(seed, 0x31) % 4) as usize];
            let opcode = SSE[(hash(seed, 0x32) % SSE.len() as u64) as usize];
            let rex = hash(seed, 0x33).is_multiple_of(3);
            let mut sse = vec![prefix];
            if rex {
                // A 64-bit general operand for the integer conversions.
                sse.push(0x48);
            }
            sse.extend([0x0f, opcode, 0xc0 | (hash(seed, 0x34) as u8 & 0x3f)]);
            bytes.splice(0..sse.len(), sse);
        }
        _ => {}
    }
    let decoded = decode(&bytes)?;
    if !register_only(&decoded, info) {
        return None;
    }
    bytes.truncate(decoded.len());
    Some((bytes, decoded))
}

/// `cmp a, b` on two usable registers at 32 or 64 bits.
fn compare(seed: u64) -> Vec<u8> {
    let register = |key| {
        let index = (hash(seed, key) % 16) as u8;
        if usable_register_index(u16::from(index)) {
            index
        } else {
            0
        }
    };
    let (a, b) = (register(0x10), register(0x11));
    let wide = hash(seed, 0x12) & 1 == 1;
    let rex = 0x40 | u8::from(wide) << 3 | (b >> 3) << 2 | a >> 3;
    vec![rex, 0x39, 0xc0 | (b & 7) << 3 | (a & 7)]
}

/// The flags each condition code reads, and whether it holds for `flags`.
fn condition(code: u8, flags: u64) -> (u32, bool) {
    let bit = |shift: u32| flags >> shift & 1 == 1;
    let (cf, pf, zf, sf, of) = (bit(0), bit(2), bit(6), bit(7), bit(11));
    let (reads, base) = match code >> 1 {
        0 => (RflagsBits::OF, of),
        1 => (RflagsBits::CF, cf),
        2 => (RflagsBits::ZF, zf),
        3 => (RflagsBits::CF | RflagsBits::ZF, cf || zf),
        4 => (RflagsBits::SF, sf),
        5 => (RflagsBits::PF, pf),
        6 => (RflagsBits::SF | RflagsBits::OF, sf != of),
        _ => (
            RflagsBits::ZF | RflagsBits::SF | RflagsBits::OF,
            zf || sf != of,
        ),
    };
    (reads, if code & 1 == 1 { !base } else { base })
}

struct Prepared {
    seed: u64,
    body: Vec<u8>,
    instructions: Vec<Instruction>,
    /// Flags the processor leaves undefined after the body.
    undefined: u32,
    registers: Vec<(u16, u64)>,
}

fn prepare_case(seed: u64, info: &mut InstructionInfoFactory) -> Option<Prepared> {
    let mut body = compare(seed);
    let Some(decoded) = decode(&body) else {
        return None;
    };
    let mut instructions = vec![decoded];
    for (index, salt) in [0, 0x5eed].into_iter().enumerate() {
        if index == 1 && hash(seed, 7) & 1 == 0 {
            break;
        }
        let Some((bytes, decoded)) = random_instruction(seed ^ salt, info) else {
            return None;
        };
        body.extend(bytes);
        instructions.push(decoded);
    }
    let undefined = instructions.iter().fold(0, |undefined, instruction| {
        let defined =
            instruction.rflags_written() | instruction.rflags_cleared() | instruction.rflags_set();
        (undefined & !defined) | instruction.rflags_undefined()
    });
    let registers = (0..16u16)
        .filter(|index| usable_register_index(*index))
        .map(|index| (index, value(seed, 0x100 + u64::from(index))))
        .chain(vector_slots().map(|slot| (slot, float_bits(seed, 0x200 + u64::from(slot)))))
        .collect();
    Some(Prepared {
        seed,
        body,
        instructions,
        undefined,
        registers,
    })
}

#[test]
fn lifted_instructions_match_the_processor() {
    let Some(runner) = runner() else {
        eprintln!("no compiler for x86-64 here; the comparison did not run");
        return;
    };
    let mut info = InstructionInfoFactory::new();
    let mut findings = Findings::default();
    let mut prepared = Vec::new();
    for seed in 0..60_000 {
        let Some(case) = prepare_case(seed, &mut info) else {
            continue;
        };
        let Some(ir) = lifted(Isa::X86_64, [case.body.as_slice(), &[0xc3]].concat()) else {
            continue;
        };
        if let Some(name) = unsupported(&ir) {
            *findings.unsupported.entry(name).or_default() += 1;
            continue;
        }
        prepared.push(case);
    }
    let cases: Vec<Case> = prepared
        .iter()
        .map(|case| {
            let (mut registers, mut vectors) = ([0; 16], [0; 32]);
            for (index, value) in &case.registers {
                match vector_word(*index) {
                    Some(word) => vectors[word] = *value,
                    None => registers[usize::from(*index)] = *value,
                }
            }
            Case {
                code: case.body.clone(),
                registers,
                flags: 0x2 | (hash(case.seed, 6) & ARITHMETIC_FLAGS),
                vectors,
                memory: None,
            }
        })
        .collect();
    let outcomes = run(runner, &cases);
    for (case, outcome) in prepared.iter().zip(outcomes) {
        if outcome.signal != 0 {
            findings.undefined += 1;
            continue;
        }
        findings.tried += 1;
        let kind = case.instructions[1..]
            .iter()
            .map(|instruction| format!("{:?}", instruction.mnemonic()))
            .collect::<Vec<_>>()
            .join(" then ");
        let describe = || {
            let code: Vec<String> = case.instructions.iter().map(|i| i.to_string()).collect();
            let inputs: Vec<String> = case
                .registers
                .iter()
                .map(|(index, value)| format!("{}={value:#x}", register_name(*index)))
                .collect();
            format!("{}\n  in: {}", code.join(" ; "), inputs.join(" "))
        };
        let ir = lifted(Isa::X86_64, [case.body.as_slice(), &[0xc3]].concat()).expect("lifted");
        if let Some(differ) = register_differences(
            &ir,
            &case.registers,
            |index| match vector_word(index) {
                Some(word) => outcome.vectors[word],
                None => outcome.registers[usize::from(index)],
            },
            register_name,
            |index, a, b| {
                if index >= vlk_arch::arch::x86::XMM0 {
                    same_float_bits(a, b)
                } else {
                    a == b
                }
            },
        ) {
            Findings::note(&mut findings.registers, kind, || {
                format!("{}\n{}\n  {differ}", describe(), ir.render())
            });
            continue;
        }
        // j<cc> +6; mov eax, 0; ret; mov eax, 1; ret
        for code in 0..16u8 {
            let (reads, expected) = condition(code, outcome.flags);
            if reads & case.undefined != 0 {
                continue;
            }
            let tail = [
                0x70 | code,
                6,
                0xb8,
                0,
                0,
                0,
                0,
                0xc3,
                0xb8,
                1,
                0,
                0,
                0,
                0xc3,
            ];
            let Some(ir) = lifted(Isa::X86_64, [case.body.as_slice(), &tail].concat()) else {
                continue;
            };
            if branches_on_unknown_flags(&ir) {
                continue;
            }
            if let Some(differ) = condition_differs(&ir, &case.registers, expected) {
                Findings::note(
                    &mut findings.conditions,
                    format!("{kind}, condition {code}"),
                    || {
                        format!(
                            "{}\n  condition {code}: {differ}\n{}",
                            describe(),
                            ir.render()
                        )
                    },
                );
            }
        }
    }
    findings.assert_agreement();
}

/// The registers memory cases address through, pointing into the middle
/// of the buffer; every other register holds a small value.
const POINTERS: [u16; 4] = [3, 6, 7, 12];

/// One of the instructions that take a memory operand, addressed through
/// a pointer register with a small index and displacement: the opcode
/// bytes, then `modrm` with a SIB byte, a byte displacement and room for
/// an immediate.
fn memory_instruction(
    seed: u64,
    info: &mut InstructionInfoFactory,
) -> Option<(Vec<u8>, Instruction)> {
    const OPCODES: &[&[u8]] = &[
        &[0x00],
        &[0x01],
        &[0x02],
        &[0x03],
        &[0x08],
        &[0x09],
        &[0x0a],
        &[0x0b],
        &[0x10],
        &[0x11],
        &[0x13],
        &[0x19],
        &[0x1b],
        &[0x20],
        &[0x21],
        &[0x23],
        &[0x28],
        &[0x29],
        &[0x2b],
        &[0x30],
        &[0x31],
        &[0x33],
        &[0x38],
        &[0x39],
        &[0x3b],
        &[0x80],
        &[0x81],
        &[0x83],
        &[0x84],
        &[0x85],
        &[0x86],
        &[0x87],
        &[0x88],
        &[0x89],
        &[0x8a],
        &[0x8b],
        &[0x8d],
        &[0xc0],
        &[0xc1],
        &[0xc6],
        &[0xc7],
        &[0xd0],
        &[0xd1],
        &[0xd3],
        &[0xf6],
        &[0xf7],
        &[0xfe],
        &[0xff],
        &[0x0f, 0xaf],
        &[0x0f, 0xb6],
        &[0x0f, 0xb7],
        &[0x0f, 0xbe],
        &[0x0f, 0xbf],
        &[0x0f, 0x44],
        &[0x0f, 0x4c],
        &[0x0f, 0x94],
        &[0x0f, 0x9f],
        &[0x0f, 0xb1],
        &[0x0f, 0xc1],
        &[0x63],
        &[0xf2, 0x0f, 0x10],
        &[0xf2, 0x0f, 0x11],
        &[0xf3, 0x0f, 0x10],
        &[0xf3, 0x0f, 0x11],
        &[0x66, 0x0f, 0x6f],
        &[0x66, 0x0f, 0x7f],
        &[0xf3, 0x0f, 0x6f],
        &[0xf3, 0x0f, 0x7f],
        &[0x0f, 0x10],
        &[0x0f, 0x11],
        &[0x66, 0x0f, 0xd6],
        &[0x66, 0x0f, 0x6e],
        &[0x66, 0x0f, 0x7e],
        &[0xf2, 0x0f, 0x58],
        &[0xf2, 0x0f, 0x59],
        &[0xf3, 0x0f, 0x5c],
        &[0x66, 0x0f, 0xfe],
        &[0x66, 0x0f, 0xef],
        &[0x66, 0x0f, 0x74],
    ];
    let pick = |key: u64, count: u64| hash(seed, key) % count;
    let opcode = OPCODES[pick(0x50, OPCODES.len() as u64) as usize];
    let base = POINTERS[pick(0x51, 4) as usize] as u8;
    let index = pick(0x52, 16) as u8;
    let (wide, operand_high) = (pick(0x53, 2) == 1, pick(0x54, 2) as u8);
    let rex = 0x40 | u8::from(wide) << 3 | operand_high << 2 | (index >> 3) << 1 | base >> 3;
    // A mandatory prefix comes before REX, the escape after it.
    let (prefix, rest) = match opcode {
        [first @ (0xf2 | 0xf3 | 0x66), rest @ ..] => (Some(*first), rest),
        rest => (None, rest),
    };
    let mut bytes: Vec<u8> = prefix.into_iter().collect();
    if pick(0x55, 8) == 0 && prefix.is_none() {
        bytes.push(0x66);
    }
    bytes.push(rex);
    bytes.extend(rest);
    let modrm = 0x40 | (pick(0x56, 8) as u8) << 3 | 0b100;
    let sib = (pick(0x57, 4) as u8) << 6 | (index & 7) << 3 | (base & 7);
    bytes.extend([modrm, sib, hash(seed, 0x58) as u8]);
    bytes.extend((0..4).map(|key| hash(seed, 0x59 + key) as u8));
    let decoded = decode(&bytes)?;
    bytes.truncate(decoded.len());
    // movsxd without REX.W runs differently under translation (see
    // `register_only`).
    if decoded.flow_control() != FlowControl::Next
        || decoded.is_privileged()
        || matches!(
            decoded.mnemonic(),
            Mnemonic::Bt | Mnemonic::Bts | Mnemonic::Btr | Mnemonic::Btc
        )
        || decoded.mnemonic() == Mnemonic::Movsxd && decoded.op0_register().size() != 8
    {
        return None;
    }
    let details = info.info(&decoded);
    let addressed = details.used_memory().iter().all(|memory| {
        POINTERS.contains(&gpr(memory.base()).map_or(u16::MAX, |(index, _)| index))
            && (memory.index() == Register::None
                || gpr(memory.index()).is_some_and(|(index, _)| {
                    !POINTERS.contains(&index) && usable_register_index(index)
                }))
            && (memory.displacement() as i64).unsigned_abs() <= 1024
            && memory.segment() == Register::DS
    });
    let registers = details
        .used_registers()
        .iter()
        .all(|used| usable_register(used.register()));
    (addressed && registers && !details.used_memory().is_empty()).then_some((bytes, decoded))
}

fn gpr(register: Register) -> Option<(u16, u8)> {
    (register.is_gpr()).then(|| {
        (
            register.full_register().number() as u16,
            (register.size() * 8) as u8,
        )
    })
}

#[test]
fn lifted_memory_operands_match_the_processor() {
    let Some(runner) = runner() else {
        eprintln!("no compiler for x86-64 here; the comparison did not run");
        return;
    };
    let mut info = InstructionInfoFactory::new();
    let mut findings = Findings::default();
    let mut cases = Vec::new();
    let mut prepared = Vec::new();
    for seed in 0..40_000u64 {
        let Some((instruction, decoded)) = memory_instruction(seed, &mut info) else {
            continue;
        };
        // After a compare, so an instruction that reads the flags reads
        // known ones.
        let code = [compare(seed), instruction].concat();
        let Some(ir) = lifted(Isa::X86_64, [code.as_slice(), &[0xc3]].concat()) else {
            continue;
        };
        if let Some(name) = unsupported(&ir) {
            *findings.unsupported.entry(name).or_default() += 1;
            continue;
        }
        let mut registers = [0u64; 16];
        for (index, slot) in registers.iter_mut().enumerate() {
            *slot = if POINTERS.contains(&(index as u16)) {
                BUFFER + (BUFFER_SIZE / 2) as u64 + hash(seed, 0x300 + index as u64) % 256
            } else {
                hash(seed, 0x100 + index as u64) % 64
            };
        }
        let vectors: [u64; 32] = std::array::from_fn(|word| float_bits(seed, 0x200 + word as u64));
        let memory: Vec<u8> = (0..BUFFER_SIZE)
            .map(|index| hash(seed, 0x400 + index as u64) as u8)
            .collect();
        cases.push(Case {
            code: code.clone(),
            registers,
            flags: 0x2 | (hash(seed, 6) & ARITHMETIC_FLAGS),
            vectors,
            memory: Some(memory),
        });
        prepared.push((code, decoded, ir));
    }
    let outcomes = run(runner, &cases);
    for ((code, decoded, ir), (case, outcome)) in prepared.iter().zip(cases.iter().zip(outcomes)) {
        if outcome.signal != 0 {
            findings.undefined += 1;
            continue;
        }
        findings.tried += 1;
        let registers: Vec<(u16, u64)> = (0..16u16)
            .filter(|index| usable_register_index(*index))
            .map(|index| (index, case.registers[usize::from(index)]))
            .chain(vector_slots().map(|slot| {
                (
                    slot,
                    case.vectors[vector_word(slot).expect("a vector slot")],
                )
            }))
            .collect();
        let before = case.memory.as_ref().expect("memory");
        let words: Vec<(u64, u64)> = before
            .chunks(8)
            .enumerate()
            .map(|(index, chunk)| {
                (
                    BUFFER + index as u64 * 8,
                    u64::from_le_bytes(chunk.try_into().expect("8 bytes")),
                )
            })
            .collect();
        let setup = eval::Setup {
            seed: 0,
            convention: eval::Convention {
                arguments: &[],
                clobbered: &[],
            },
            registers: &registers,
            memory: &words,
            step_limit: 100,
        };
        let kind = format!("{:?}", decoded.mnemonic());
        let describe = || format!("{decoded} ({code:02x?})\n{}", ir.render());
        let (after, memory) = match eval::state_after(ir, &setup) {
            Ok(state) => state,
            Err("division by zero") => {
                findings.undefined += 1;
                continue;
            }
            Err(reason) => {
                Findings::note(&mut findings.registers, kind, || {
                    format!("{}\n  {reason}", describe())
                });
                continue;
            }
        };
        let real = |index: u16| match vector_word(index) {
            Some(word) => outcome.vectors[word],
            None => outcome.registers[usize::from(index)],
        };
        let mut differ: Vec<String> = registers
            .iter()
            .filter_map(|(index, start)| {
                let lifted = after.get(index).copied().unwrap_or(*start);
                let same = if *index >= vlk_arch::arch::x86::XMM0 {
                    same_float_bits(lifted, real(*index))
                } else {
                    lifted == real(*index)
                };
                (!same).then(|| {
                    format!(
                        "{}: lifted {lifted:#x}, processor {:#x}",
                        register_name(*index),
                        real(*index)
                    )
                })
            })
            .collect();
        let after_memory = outcome.memory.as_ref().expect("memory back");
        for (offset, real) in after_memory.iter().enumerate() {
            let lifted = memory
                .get(&(BUFFER + offset as u64))
                .copied()
                .unwrap_or(before[offset]);
            if lifted != *real {
                differ.push(format!(
                    "byte {offset:#x}: lifted {lifted:#x}, processor {real:#x}"
                ));
            }
        }
        if !differ.is_empty() {
            Findings::note(&mut findings.registers, kind, || {
                format!("{}\n  {}", describe(), differ.join("\n  "))
            });
        }
    }
    findings.assert_agreement();
}

#[test]
fn fixture_simd_flags_and_reverse_strings_match_the_processor() {
    let runner = runner().expect("an x86-64 compiler is required");
    let mut codes = vec![
        vec![0x66, 0x0f, 0x38, 0x00, 0xc1], // pshufb xmm0, xmm1
        vec![0x66, 0x0f, 0x38, 0x00, 0x03], // pshufb xmm0, [rbx]
        vec![0x66, 0x0f, 0x38, 0x17, 0xc1], // ptest xmm0, xmm1
        vec![0xc4, 0xe2, 0x79, 0x17, 0xc1], // vptest xmm0, xmm1
        vec![0xc4, 0xe2, 0x7d, 0x17, 0xc1], // vptest ymm0, ymm1
        vec![0xc4, 0xe2, 0x7d, 0x17, 0x03], // vptest ymm0, [rbx]
        vec![0x66, 0x0f, 0x38, 0x00, 0xc0], // pshufb xmm0, xmm0
    ];
    for lane in 0..4 {
        for source in [0xc1, 0x03] {
            codes.push(vec![0x66, 0x0f, 0x3a, 0x22, source, 0xfc | lane]);
        }
    }
    // Record the condition flags in registers for comparison.
    for (kind, code) in codes.iter_mut().enumerate() {
        code.splice(0..0, [0x48, 0x39, 0xd0]); // cmp rax, rdx
        if cfg!(target_arch = "aarch64") && (2..=5).contains(&kind) {
            // Translation leaves some flags that PTEST specifies as zero
            // unchanged. Compare CF and ZF here; native x86 checks all six.
            code.extend([0x0f, 0x92, 0xc0, 0x0f, 0x94, 0xc1]);
        } else {
            code.extend([0x9f, 0x0f, 0x90, 0xc1]); // lahf; seto cl
        }
    }
    codes.extend([
        // Different-width compares meet at sete al.
        vec![
            0x48, 0x85, 0xd2, 0x74, 5, 0x48, 0x39, 0xd8, 0xeb, 2, 0x39, 0xd8, 0x0f, 0x94, 0xc0,
        ],
        // A variable shift preserves ZF for an effective count of zero.
        vec![0x48, 0x39, 0xd8, 0x48, 0xd3, 0xe0, 0x0f, 0x94, 0xc2],
        vec![0x48, 0x39, 0xd8, 0xd3, 0xe0, 0x0f, 0x94, 0xc2],
        vec![0x48, 0x39, 0xd8, 0x66, 0xd3, 0xe0, 0x0f, 0x94, 0xc2],
        vec![0x48, 0x39, 0xd8, 0xd2, 0xe0, 0x0f, 0x94, 0xc2],
        // Direction set in another block, then restored after the copy.
        vec![0xfd, 0xeb, 0, 0xf3, 0xa4, 0xfc],
        vec![0xfd, 0xeb, 0, 0xf3, 0x48, 0xa5, 0xfc],
        vec![0xfd, 0xeb, 0, 0xf3, 0xaa, 0xfc],
        vec![0xfd, 0xeb, 0, 0xf3, 0xac, 0xfc],
    ]);
    for backward in [false, true] {
        for (wide, prefix) in [
            (false, vec![]),
            (true, vec![0x66]),
            (true, vec![]),
            (true, vec![0x48]),
        ] {
            for opcode in [0xa4, 0xaa, 0xac, 0xa6, 0xae] {
                for repeat in [None, Some(0xf3), Some(0xf2)] {
                    let mut code = vec![0x48, 0x39, 0xd0, if backward { 0xfd } else { 0xfc }];
                    code.extend(repeat);
                    code.extend(&prefix);
                    code.push(opcode + u8::from(wide));
                    code.extend([0xfc, 0x9f, 0x0f, 0x90, 0xc2]);
                    codes.push(code);
                }
            }
        }
    }
    let mut cases = Vec::new();
    let mut kinds = Vec::new();
    for (kind, code) in codes.iter().enumerate() {
        for seed in 0..256 {
            let mut registers = std::array::from_fn(|index| value(seed, index as u64));
            registers[3] = BUFFER + 4096;
            registers[6] = BUFFER + 5000;
            registers[7] = BUFFER + 6000;
            registers[1] = seed % 80;
            registers[2] = seed % 3;
            let mut vectors = std::array::from_fn(|index| hash(seed, index as u64));
            if seed % 4 == 0 {
                vectors.fill(0);
            }
            if seed % 4 == 1 {
                vectors.fill(u64::MAX);
            }
            let memory = (0..BUFFER_SIZE)
                .map(|index| hash(seed, index as u64) as u8)
                .collect();
            kinds.push(kind);
            cases.push(Case {
                code: code.clone(),
                registers,
                vectors,
                flags: 0x202 | (hash(seed, 42) & ARITHMETIC_FLAGS),
                memory: Some(memory),
            });
        }
    }
    let outcomes = run(runner, &cases);
    for ((kind, case), outcome) in kinds.iter().zip(&cases).zip(outcomes) {
        assert_eq!(outcome.signal, 0, "instruction case {kind}");
        let ir = lifted(Isa::X86_64, [case.code.as_slice(), &[0xc3]].concat()).expect("lifted");
        assert!(unsupported(&ir).is_none(), "{}", ir.render());
        let registers: Vec<_> = (0..16u16)
            .filter(|index| usable_register_index(*index))
            .map(|index| (index, case.registers[usize::from(index)]))
            .chain(vector_slots().map(|index| (index, case.vectors[vector_word(index).unwrap()])))
            .collect();
        let before = case.memory.as_ref().unwrap();
        let words: Vec<_> = before
            .chunks(8)
            .enumerate()
            .map(|(index, bytes)| {
                (
                    BUFFER + index as u64 * 8,
                    u64::from_le_bytes(bytes.try_into().unwrap()),
                )
            })
            .collect();
        let setup = eval::Setup {
            seed: 0,
            convention: eval::Convention {
                arguments: &[],
                clobbered: &[],
            },
            registers: &registers,
            memory: &words,
            step_limit: 3000,
        };
        let (after, memory) = eval::state_after(&ir, &setup).expect("interpreted");
        for (index, initial) in &registers {
            let actual = match vector_word(*index) {
                Some(word) => outcome.vectors[word],
                None => outcome.registers[usize::from(*index)],
            };
            assert_eq!(
                after.get(index).unwrap_or(initial),
                &actual,
                "case {kind}, register {}\n{}",
                register_name(*index),
                ir.render()
            );
        }
        for (offset, actual) in outcome.memory.as_ref().unwrap().iter().enumerate() {
            assert_eq!(
                *memory
                    .get(&(BUFFER + offset as u64))
                    .unwrap_or(&before[offset]),
                *actual,
                "case {kind}, byte {offset}"
            );
        }
    }
}

#[test]
fn explicit_breakpoint_interrupt_matches_the_processor() {
    let runner = runner().expect("an x86-64 compiler is required");
    let cases: Vec<_> = [vec![0xcc], vec![0xcd, 3]]
        .into_iter()
        .map(|code| Case {
            code,
            registers: [0; 16],
            flags: 0x202,
            vectors: [0; 32],
            memory: None,
        })
        .collect();
    for (case, outcome) in cases.iter().zip(run(runner, &cases)) {
        assert_eq!(outcome.signal, 5);
        let ir = lifted(Isa::X86_64, case.code.clone()).expect("lifted");
        assert!(unsupported(&ir).is_none());
        assert!(matches!(
            eval::run(&ir, &super::setup(&[])),
            eval::Outcome::Trap { .. }
        ));
    }
}