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
use super::*;
use vlk_smt::smt::{self, Proofs};

#[test]
fn decoded_extend_add_matches_symbolic_rotation_and_addition() {
    if !Proofs::available() {
        return;
    }
    let mut proofs = Proofs::default();
    let image = Image::from_code(Isa::Arm32, 0, vec![0; 4]);
    for mode in [ArmMode::Arm, ArmMode::Thumb] {
        for width in [1, 2] {
            for signed in [false, true] {
                for rotation in [0, 8, 16, 24] {
                    let word: u32 = if mode == ArmMode::Arm {
                        0xe680_0072
                            | (if signed { 0 } else { 1 << 22 })
                            | (if width == 1 { 2 << 20 } else { 3 << 20 })
                            | (1 << 16)
                            | ((rotation / 8) << 10)
                    } else {
                        0xfa01_f082
                            | (if signed { 0 } else { 1 << 20 })
                            | (if width == 1 { 1 << 22 } else { 0 })
                            | ((rotation / 8) << 4)
                    };
                    let bytes = if mode == ArmMode::Arm {
                        word.to_le_bytes()
                    } else {
                        let high = ((word >> 16) as u16).to_le_bytes();
                        let low = (word as u16).to_le_bytes();
                        [high[0], high[1], low[0], low[1]]
                    };
                    let instruction = instruction_fallback::decode(&bytes, mode)
                        .expect("extend-and-add encoding");
                    let mut lifter = Lifter {
                        image: &image,
                        out: crate::lift::Emitter::pending(None),
                        float_comparison: false,
                    };
                    instruction.execute(&mut lifter, 0, mode == ArmMode::Thumb);
                    let rotated = format!(
                        "((_ zero_extend 32) ((_ rotate_right {rotation}) ((_ extract 31 0) r2)))"
                    );
                    let extended = if signed {
                        smt::signed(&rotated, width * 8)
                    } else {
                        smt::low(&rotated, width * 8)
                    };
                    let expected = smt::low(
                        &format!("(bvadd {} {extended})", smt::term(&Expr::reg(1, 32))),
                        32,
                    );
                    let [Stmt::Assign { dest, value, .. }] = lifter.out.stmts.as_slice() else {
                        panic!("expected one assignment");
                    };
                    assert_eq!(dest.index, 0);
                    proofs.predicate(
                        format!("{mode:?}/{width}/{signed}/{rotation}"),
                        &expected,
                        &smt::term(&low(value.clone())),
                    );
                }
            }
        }
    }
    proofs.check();
}

#[test]
fn carry_arithmetic_is_symbolic() {
    if !Proofs::available() {
        return;
    }
    let mut proofs = Proofs::default();
    let image = Image::from_code(Isa::Arm32, 0, vec![0; 4]);
    for opcode in [Opcode::ADC, Opcode::SBC, Opcode::RSC] {
        let mut lifter = Lifter {
            image: &image,
            out: crate::lift::Emitter::pending(Some(Flags::difference(
                Expr::reg(3, 32),
                Expr::constant(1),
            ))),
            float_comparison: false,
        };
        lifter.out.flags.pending.as_mut().unwrap().width = Some(32);
        let instruction = Instruction {
            opcode,
            s: true,
            operands: [
                Operand::Reg(armv7::Reg::from_u8(2)),
                Operand::Reg(armv7::Reg::from_u8(0)),
                Operand::Reg(armv7::Reg::from_u8(1)),
                Operand::Nothing,
            ],
            ..Instruction::default()
        };
        assert_eq!(lifter.execute(&instruction, 0, 4, None), Some(false));
        let a = smt::term(&Expr::reg(if opcode == Opcode::RSC { 1 } else { 0 }, 32));
        let b = smt::term(&Expr::reg(if opcode == Opcode::RSC { 0 } else { 1 }, 32));
        let carry = format!(
            "(ite (= {} {}) {} {})",
            smt::term(&Expr::reg(3, 32)),
            smt::constant(0),
            smt::constant(0),
            smt::constant(1)
        );
        let incoming = Expr::Select {
            cond: Cond::Eq,
            lhs: Box::new(Expr::reg(3, 32)),
            rhs: Box::new(Expr::constant(0)),
            then: Box::new(Expr::constant(0)),
            els: Box::new(Expr::constant(1)),
        };
        let without_carry = smt::raw_bin(
            if opcode == Opcode::ADC {
                BinOp::Add
            } else {
                BinOp::Sub
            },
            Expr::reg(if opcode == Opcode::RSC { 1 } else { 0 }, 32),
            Expr::reg(if opcode == Opcode::RSC { 0 } else { 1 }, 32),
        );
        let adjustment = if opcode == Opcode::ADC {
            incoming.clone()
        } else {
            smt::raw_bin(BinOp::Xor, incoming.clone(), Expr::Const(1))
        };
        let exact_value = smt::raw_bin(
            if opcode == Opcode::ADC {
                BinOp::Add
            } else {
                BinOp::Sub
            },
            without_carry.clone(),
            adjustment,
        );
        let expected = smt::raw_bin(BinOp::And, exact_value.clone(), Expr::Const(0xffff_ffff));
        let mut statements = lifter.out.stmts.clone();
        statements.push(Stmt::Ret {
            value: Expr::reg(2, 64),
            at: None,
        });
        proofs.program(
            format!("carry arithmetic value/{opcode:?}"),
            &[Stmt::Ret {
                value: expected.clone(),
                at: None,
            }],
            &statements,
        );
        let wrong = smt::raw_bin(BinOp::And, without_carry, Expr::Const(0xffff_ffff));
        proofs.counterexample(
            format!("missing incoming carry or borrow/{opcode:?}"),
            &smt::term(&expected),
            &smt::term(&wrong),
        );
        proofs.counterexample(
            format!("missing result truncation/{opcode:?}"),
            &smt::term(&expected),
            &smt::term(&exact_value),
        );
        let exact = if opcode == Opcode::ADC {
            format!("(bvadd (bvadd {a} {b}) {carry})")
        } else {
            format!(
                "(bvsub (bvsub {a} {b}) (bvxor {carry} {}))",
                smt::constant(1)
            )
        };
        let signed_a = smt::signed(&a, 32);
        let signed_b = smt::signed(&b, 32);
        let signed_exact = if opcode == Opcode::ADC {
            format!("(bvadd (bvadd {signed_a} {signed_b}) {carry})")
        } else {
            format!(
                "(bvsub (bvsub {signed_a} {signed_b}) (bvxor {carry} {}))",
                smt::constant(1)
            )
        };
        let overflow = format!(
            "(or (bvslt {signed_exact} {}) (bvsgt {signed_exact} {}))",
            smt::constant(0xffff_ffff_8000_0000),
            smt::constant(0x7fff_ffff)
        );
        let flags = lifter.out.flags.pending.as_ref().unwrap();
        for (cond, expected) in [
            (
                Cond::Eq,
                format!("(= {} {})", smt::low(&exact, 32), smt::constant(0)),
            ),
            (
                Cond::Mi,
                format!("(bvslt {} {})", smt::signed(&exact, 32), smt::constant(0)),
            ),
            (
                Cond::Cs,
                if opcode == Opcode::ADC {
                    format!("(bvugt {exact} {})", smt::constant(0xffff_ffff))
                } else {
                    format!(
                        "(bvuge {a} (bvadd {b} (bvxor {carry} {})))",
                        smt::constant(1)
                    )
                },
            ),
            (Cond::Vs, overflow.clone()),
            (Cond::Vc, format!("(not {overflow})")),
        ] {
            let actual = flags
                .test(cond, Isa::Arm32)
                .select(Expr::constant(1), Expr::constant(0))
                .unwrap();
            // Capture statements freeze the incoming carry before setting the output flags.
            let mut statements = lifter.out.stmts.clone();
            statements.push(Stmt::Ret {
                value: actual,
                at: None,
            });
            // Substitute the captured carry so the predicate remains a pure expression.
            vlk_ir::ir::visit::replace_reads(
                statements.last_mut().unwrap(),
                &[(vlk_ir::ir::EXCHANGE, incoming.clone())],
            );
            let Stmt::Ret { value, .. } = statements.last().unwrap() else {
                unreachable!()
            };
            proofs.predicate(
                format!("{opcode:?}/{cond:?}"),
                &format!("(ite {expected} {} {})", smt::constant(1), smt::constant(0)),
                &smt::term(value),
            );
            proofs.counterexample(
                format!("inverted carry-arithmetic flag/{opcode:?}/{cond:?}"),
                &format!("(ite {expected} {} {})", smt::constant(1), smt::constant(0)),
                &format!("(bvxor {} {})", smt::term(value), smt::constant(1)),
            );
        }
    }
    proofs.check();
}

#[test]
fn rotate_through_carry_preserves_value_and_flags() {
    if !Proofs::available() {
        return;
    }
    let mut proofs = Proofs::default();
    let image = Image::from_code(Isa::Arm32, 0, vec![0; 4]);
    for set_flags in [false, true] {
        let mut flags = Flags::stored(Expr::reg(3, 8), Some(Expr::reg(4, 1)));
        flags.width = Some(32);
        let mut lifter = Lifter {
            image: &image,
            out: crate::lift::Emitter::pending(Some(flags.clone())),
            float_comparison: false,
        };
        let instruction = Instruction {
            opcode: Opcode::RRX,
            s: set_flags,
            operands: [
                Operand::Reg(armv7::Reg::from_u8(2)),
                Operand::Reg(armv7::Reg::from_u8(0)),
                Operand::Nothing,
                Operand::Nothing,
            ],
            ..Instruction::default()
        };
        assert_eq!(lifter.execute(&instruction, 0, 4, None), Some(false));
        let carry = flags
            .test(Cond::Cs, Isa::Arm32)
            .select(Expr::constant(1), Expr::constant(0))
            .unwrap();
        let x = smt::term(&Expr::reg(0, 32));
        let expected = format!(
            "(concat ((_ extract 0 0) {}) ((_ extract 31 1) r0))",
            smt::term(&carry)
        );
        let [Stmt::Assign { dest, value, .. }] = lifter.out.stmts.as_slice() else {
            panic!("RRX assigns its destination once");
        };
        assert_eq!(dest.index, 2);
        let actual = smt::term(value);
        proofs.predicate(
            format!("RRX value/{set_flags}"),
            &format!("((_ zero_extend 32) {expected})"),
            &actual,
        );
        proofs.counterexample(
            format!("RRX missing carry/{set_flags}"),
            &actual,
            &format!("(bvlshr {x} {})", smt::constant(1)),
        );
        for cond in [Cond::Eq, Cond::Mi, Cond::Cs, Cond::Vs] {
            let expected_flag = if !set_flags || cond == Cond::Vs {
                let value = flags
                    .test(cond, Isa::Arm32)
                    .select(Expr::constant(1), Expr::constant(0))
                    .unwrap();
                smt::term(&value)
            } else {
                let condition = match cond {
                    Cond::Eq => format!("(= {expected} (_ bv0 32))"),
                    Cond::Mi => format!("(bvslt {expected} (_ bv0 32))"),
                    Cond::Cs => {
                        format!("(= (bvand {x} {}) {})", smt::constant(1), smt::constant(1))
                    }
                    _ => unreachable!(),
                };
                format!(
                    "(ite {condition} {} {})",
                    smt::constant(1),
                    smt::constant(0)
                )
            };
            let actual = lifter
                .out
                .flags
                .pending
                .as_ref()
                .unwrap()
                .test(cond, Isa::Arm32)
                .select(Expr::constant(1), Expr::constant(0))
                .unwrap();
            proofs.predicate(
                format!("RRX flag/{set_flags}/{cond:?}"),
                &expected_flag,
                &smt::term(&actual),
            );
            proofs.counterexample(
                format!("RRX inverted flag/{set_flags}/{cond:?}"),
                &expected_flag,
                &format!("(bvxor {} {})", smt::term(&actual), smt::constant(1)),
            );
        }
        lifter.out.flags.pending = None;
        assert_eq!(lifter.execute(&instruction, 0, 4, None), None);
    }
    proofs.check();
}

#[test]
fn shifted_carry_matches_extracted_bits() {
    if !Proofs::available() {
        return;
    }
    let mut proofs = Proofs::default();
    for style in [
        ShiftStyle::LSL,
        ShiftStyle::LSR,
        ShiftStyle::ASR,
        ShiftStyle::ROR,
        ShiftStyle::RRX,
    ] {
        for amount in [0, 1, 7, 31, 32, 33, 63, 255] {
            let x = Expr::reg(0, 32);
            let old = Expr::bin(BinOp::And, Expr::reg(1, 32), Expr::constant(1));
            let actual =
                shifted_carry(x.clone(), style, &Expr::constant(amount), Some(old.clone()))
                    .unwrap();
            let bit = match (style, amount) {
                (ShiftStyle::RRX, _) => Some(0),
                (_, 0) => None,
                (ShiftStyle::LSL, 1..=32) => Some(32 - amount),
                (ShiftStyle::LSR, 1..=32) | (ShiftStyle::ASR, 1..=31) => Some(amount - 1),
                (ShiftStyle::ASR, _) => Some(31),
                (ShiftStyle::ROR, _) => Some((amount - 1) % 32),
                _ => Some(64),
            };
            let expected = match bit {
                None => smt::term(&old),
                Some(64) => smt::constant(0),
                Some(bit) => format!(
                    "((_ zero_extend 63) ((_ extract {bit} {bit}) {}))",
                    smt::term(&x)
                ),
            };
            proofs.predicate(
                format!("shift carry/{style:?}/{amount}"),
                &expected,
                &smt::term(&actual),
            );
        }
    }
    proofs.check();
}

#[test]
fn high_multiply_is_the_top_word_of_the_signed_product() {
    if !Proofs::available() {
        return;
    }
    let mut proofs = Proofs::default();
    let image = Image::from_code(Isa::Arm32, 0, vec![0; 4]);
    for round in [false, true] {
        // smmul r3, r4, r5 and smmulr r3, r4, r5
        let word: u32 = 0xe753_f514 | (u32::from(round) << 5);
        let instruction = instruction_fallback::decode(&word.to_le_bytes(), ArmMode::Arm)
            .expect("high multiply encoding");
        let mut lifter = Lifter {
            image: &image,
            out: crate::lift::Emitter::pending(None),
            float_comparison: false,
        };
        instruction.execute(&mut lifter, 0, false);
        let [Stmt::Assign { dest, value, .. }] = lifter.out.stmts.as_slice() else {
            panic!("expected one assignment");
        };
        assert_eq!(dest.index, 3);
        let word = |name: &str| format!("((_ sign_extend 32) ((_ extract 31 0) {name}))");
        let product = format!("(bvmul {} {})", word("r4"), word("r5"));
        let rounded = if round {
            format!("(bvadd {product} #x0000000080000000)")
        } else {
            product
        };
        let expected = format!("((_ zero_extend 32) ((_ extract 63 32) {rounded}))");
        proofs.predicate(
            format!("high multiply/{round}"),
            &expected,
            &smt::term(&low(value.clone())),
        );
    }
    proofs.check();
}

#[test]
fn long_multiply_matches_a_wide_product() {
    if !Proofs::available() {
        return;
    }
    let mut proofs = Proofs::default();
    let image = Image::from_code(Isa::Arm32, 0, vec![0; 4]);
    for opcode in [Opcode::UMULL, Opcode::SMULL, Opcode::UMLAL, Opcode::SMLAL] {
        let mut lifter = Lifter {
            image: &image,
            out: crate::lift::Emitter::pending(None),
            float_comparison: false,
        };
        let instruction = Instruction {
            opcode,
            operands: [0, 1, 2, 3].map(|index| Operand::Reg(armv7::Reg::from_u8(index))),
            ..Instruction::default()
        };
        assert_eq!(lifter.execute(&instruction, 0, 4, None), Some(false));
        lifter.out.stmts.push(Stmt::Ret {
            value: super::super::pairs32::join(Expr::reg(0, 32), Expr::reg(1, 32)),
            at: None,
        });
        let signed = matches!(opcode, Opcode::SMULL | Opcode::SMLAL);
        let extend = |index| {
            if signed {
                Expr::SignExtend {
                    value: Box::new(Expr::reg(index, 32)),
                    width: 4,
                }
            } else {
                Expr::reg(index, 32)
            }
        };
        let mut expected = Expr::Bin {
            op: BinOp::Mul,
            lhs: Box::new(extend(2)),
            rhs: Box::new(extend(3)),
        };
        if matches!(opcode, Opcode::UMLAL | Opcode::SMLAL) {
            expected = Expr::Bin {
                op: BinOp::Add,
                lhs: Box::new(expected),
                rhs: Box::new(super::super::pairs32::join(
                    Expr::reg(0, 32),
                    Expr::reg(1, 32),
                )),
            };
        }
        // Multiplication is compared through its actual 64-bit modular IR semantics.
        proofs.program(
            format!("long multiply/{opcode:?}"),
            &[Stmt::Ret {
                value: expected,
                at: None,
            }],
            &lifter.out.stmts,
        );
    }
    proofs.check();
}

#[test]
fn rotated_extensions_and_shifted_operands_are_equivalent() {
    if !Proofs::available() {
        return;
    }
    let mut proofs = Proofs::default();
    let x = Expr::reg(0, 32);
    let input = "((_ extract 31 0) r0)";
    for bytes in [1, 2] {
        for signed in [false, true] {
            for rotation in [0, 8, 16, 24] {
                let rotated = format!("((_ zero_extend 32) ((_ rotate_right {rotation}) {input}))");
                let expected = if signed {
                    smt::signed(&rotated, u32::from(bytes) * 8)
                } else {
                    smt::low(&rotated, u32::from(bytes) * 8)
                };
                proofs.predicate(
                    format!("rotated extension/{bytes}/{signed}/{rotation}"),
                    &expected,
                    &smt::term(&extend(x.clone(), bytes, signed, rotation)),
                );
            }
        }
    }
    // An amount held in a register, and immediates on both sides of 32.
    let amounts = std::iter::once(Expr::reg(1, 8)).chain([0, 1, 31, 32, 33, 255].map(Expr::Const));
    for amount in amounts {
        let count = smt::term(&amount);
        for style in [
            ShiftStyle::LSL,
            ShiftStyle::LSR,
            ShiftStyle::ASR,
            ShiftStyle::ROR,
        ] {
            let expected = match style {
                ShiftStyle::ROR => format!(
                    "((_ zero_extend 32) (ext_rotate_right {input} ((_ extract 31 0) {count})))"
                ),
                ShiftStyle::ASR => smt::low(
                    &format!("(bvashr {} {count})", smt::signed(&smt::term(&x), 32)),
                    32,
                ),
                _ => smt::low(
                    &format!(
                        "({} {} {count})",
                        if style == ShiftStyle::LSL {
                            "bvshl"
                        } else {
                            "bvlshr"
                        },
                        smt::term(&x)
                    ),
                    32,
                ),
            };
            let actual = shift_value(x.clone(), style, amount.clone()).expect("supported shift");
            proofs.predicate(
                format!("A32 shift/{style:?}/{amount:?}"),
                &expected,
                &smt::low(&smt::term(&actual), 32),
            );
        }
    }
    proofs.check();
}