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
//! The optimization passes must not change what a function does: every fixture
//! function runs through the interpreter before and after them, on the same inputs.
//! Current mismatches are listed in tests/golden/semantics.txt; a new one fails,
//! and a fixed one must be removed with `VLK_BLESS=1 cargo test -p vlk-analysis semantics`.

use std::collections::BTreeMap;
use std::fmt::Write as _;
use std::path::{Path, PathBuf};
use std::sync::atomic::{AtomicUsize, Ordering};

use super::{lift_function, optimize};
use vlk_eval::eval::trials::{TRIALS, initial_memory, registers, return_bits, setup};
use vlk_eval::eval::{self, Outcome};
use vlk_loader::loader::Function;
use vlk_types::types::CType;

#[test]
fn passes_preserve_behavior() {
    let root = PathBuf::from(concat!(env!("CARGO_MANIFEST_DIR"), "/../.."));
    let mut fixtures = Vec::new();
    collect(&root.join("tests/fixtures"), Path::new(""), &mut fixtures);
    fixtures.sort();
    let images: Vec<vlk_program::program::Program> = fixtures
        .iter()
        .map(|fixture| open(&root.join("tests/fixtures").join(fixture)))
        .collect();
    let work: Vec<(usize, &Function)> = images
        .iter()
        .enumerate()
        .flat_map(|(index, image)| {
            image
                .image
                .functions
                .values()
                .map(move |function| (index, function))
        })
        .collect();

    let next = AtomicUsize::new(0);
    let workers = std::thread::available_parallelism().map_or(4, |count| count.get());
    let checks: Vec<Check> = std::thread::scope(|scope| {
        let handles: Vec<_> = (0..workers)
            .map(|_| {
                scope.spawn(|| {
                    let mut done = Vec::new();
                    while let Some((index, function)) =
                        work.get(next.fetch_add(1, Ordering::Relaxed))
                    {
                        done.push(check(&fixtures[*index], &images[*index], function));
                    }
                    done
                })
            })
            .collect();
        handles
            .into_iter()
            .flat_map(|handle| {
                handle
                    .join()
                    .unwrap_or_else(|panic| std::panic::resume_unwind(panic))
            })
            .collect()
    });

    let (mut checked, mut runs) = (0, 0);
    let mut unknown: BTreeMap<String, usize> = BTreeMap::new();
    let mut mismatches: Vec<String> = Vec::new();
    for result in checks {
        checked += usize::from(result.lifted);
        runs += result.runs;
        for reason in result.unknown {
            *unknown.entry(reason).or_default() += 1;
        }
        mismatches.extend(result.mismatch);
    }
    mismatches.sort();
    let mut actual =
        format!("# {checked} functions, {runs} compared runs\n# runs left unfinished:\n");
    for (reason, count) in &unknown {
        writeln!(actual, "#   {reason}: {count}").expect("writing to a String");
    }
    for line in mismatches {
        actual.push_str(&line);
        actual.push('\n');
    }

    let path = root.join("tests/golden/semantics.txt");
    if std::env::var_os("VLK_BLESS").is_some_and(|value| value == "1") {
        std::fs::write(&path, &actual)
            .unwrap_or_else(|error| panic!("{}: {error}", path.display()));
        return;
    }
    let expected = std::fs::read_to_string(&path).unwrap_or_default();
    if expected != actual {
        panic!(
            "behavior changes differ from {}:\n{}",
            path.display(),
            changes(&expected, &actual)
        );
    }
}

/// The functions that newly differ and the ones no longer listed.
fn changes(expected: &str, actual: &str) -> String {
    let functions = |text: &str| -> BTreeMap<String, String> {
        text.lines()
            .filter(|line| !line.starts_with('#'))
            .map(|line| {
                (
                    line.split_whitespace()
                        .take(2)
                        .collect::<Vec<_>>()
                        .join(" "),
                    line.to_owned(),
                )
            })
            .collect()
    };
    let (before, after) = (functions(expected), functions(actual));
    let mut out = String::new();
    for (key, line) in &after {
        if !before.contains_key(key) {
            writeln!(out, "  new: {line}").expect("writing to a String");
        }
    }
    for (key, line) in &before {
        if !after.contains_key(key) {
            writeln!(out, "  no longer differs: {line}").expect("writing to a String");
        }
    }
    if out.is_empty() {
        out.push_str("  the same functions differ; the counts or descriptions changed\n");
    }
    out
}

/// Go and Rust executables are mostly their runtimes, so they are counted apart.
fn group(fixture: &Path) -> &'static str {
    let name = fixture.to_string_lossy();
    if name.starts_with("go_") {
        "Go"
    } else if name.starts_with("rust_") {
        "Rust"
    } else {
        "C and C++"
    }
}

struct Check {
    lifted: bool,
    runs: usize,
    unknown: Vec<String>,
    mismatch: Option<String>,
}

/// The lifted function with each switch a jump table became turned back
/// into the indirect jump it was, as a switch on the address itself over
/// every block, or `None` without tables.
fn indirect_jumps(
    program: &vlk_program::program::Program,
    function: &Function,
    lifted: &vlk_ir::ir::FunctionIr,
) -> Option<(vlk_ir::ir::FunctionIr, Vec<(u64, u64)>)> {
    use vlk_ir::ir::Stmt;
    if !lifted
        .blocks
        .iter()
        .any(|block| matches!(block.stmts.last(), Some(Stmt::Switch { .. })))
    {
        return None;
    }
    let size = function.size.unwrap_or(super::UNKNOWN_SIZE);
    let targets: std::collections::HashSet<u64> =
        lifted.blocks.iter().map(|block| block.start).collect();
    let Ok(raw) = crate::lift::lift(program, function.addr, size, &targets) else {
        return None;
    };
    let extent = function.addr..function.addr.saturating_add(size);
    let tables = super::jump_table::recover(&raw, program, extent)
        .into_iter()
        .map(|table| table.table)
        .collect();
    let mut indirect = lifted.clone();
    for block in &mut indirect.blocks {
        if !matches!(block.stmts.last(), Some(Stmt::Switch { .. })) {
            continue;
        }
        let Some(original) = raw.iter().find(|other| other.start == block.start) else {
            return None;
        };
        let Some(Stmt::BrIndirect { target, .. }) = original.stmts.last() else {
            return None;
        };
        let mut stmts = original.stmts.clone();
        *stmts.last_mut().expect("a jump") = Stmt::Switch {
            index: target.clone(),
            cases: targets
                .iter()
                .map(|start| (*start as i64, *start))
                .collect(),
            default: None,
            at: None,
        };
        block.stmts = stmts;
    }
    Some((indirect, tables))
}

fn check(fixture: &Path, program: &vlk_program::program::Program, function: &Function) -> Check {
    let mut result = Check {
        lifted: false,
        runs: 0,
        unknown: Vec::new(),
        mismatch: None,
    };
    let Ok(lifted) = lift_function(program, function) else {
        return result;
    };
    result.lifted = true;
    let before = lifted.ir.clone();
    let after = optimize(program, lifted, None).unwrap().ir;
    // Both versions go into SSA form and back unchanged.
    for ir in [&before, &after] {
        let Some(ssa) = crate::ssa::build(ir) else {
            continue;
        };
        let broken = crate::ssa::verify(&ssa).err().or_else(|| {
            (crate::ssa::lower(&ssa).render() != ir.render())
                .then(|| "lowering changes the function".to_owned())
        });
        if let Some(reason) = broken {
            result.mismatch = Some(format!("ssa: {reason}"));
            return result;
        }
    }
    let memory = initial_memory(&program.image);
    // A switch a jump table became goes where the indirect jump went, with
    // the tables' bytes as the program holds them.
    let indirect = indirect_jumps(program, function, &before);
    if let Some((indirect, tables)) = &indirect {
        let mut seeded = memory.clone();
        for (start, length) in tables {
            for word in (0..length.div_ceil(8)).map(|word| start + word * 8) {
                if let Some(bytes) = program.image.read_vaddr(word, 8) {
                    seeded.push((word, u64::from_le_bytes(bytes.try_into().expect("8 bytes"))));
                }
            }
        }
        for trial in 0..TRIALS {
            let registers = registers(&program.image, trial);
            let setup = setup(&program.image, trial, &registers, &seeded);
            let jumped = eval::run(indirect, &setup);
            let switched = eval::run(&before, &setup);
            if !matches!(jumped, Outcome::Unknown(_))
                && !matches!(switched, Outcome::Unknown(_))
                && !same(&jumped, &switched, &before.ret_type)
            {
                result.mismatch = Some(format!(
                    "{} {} {}: trial {trial}: jump table: {}",
                    fixture.display(),
                    vlk_base::addr::format(function.addr),
                    function.name.as_deref().unwrap_or("-"),
                    describe(&jumped, &switched, &before.ret_type)
                ));
                return result;
            }
        }
    }
    // The structured code, as blocks again, behaves as the optimized IR does.
    let structured = vlk_render::render::structure::lower::lower(
        &after,
        &vlk_render::render::structure::structure_best(&after),
    );
    for trial in 0..TRIALS {
        let registers = registers(&program.image, trial);
        let setup = setup(&program.image, trial, &registers, &memory);
        let old = eval::run(&before, &setup);
        let new = eval::run(&after, &setup);
        let shaped = eval::run(&structured, &setup);
        if !matches!(new, Outcome::Unknown(_))
            && !matches!(shaped, Outcome::Unknown(_))
            && !same(&new, &shaped, &after.ret_type)
        {
            result.mismatch = Some(format!(
                "{} {} {}: trial {trial}: structuring: {}",
                fixture.display(),
                vlk_base::addr::format(function.addr),
                function.name.as_deref().unwrap_or("-"),
                describe(&new, &shaped, &after.ret_type)
            ));
            break;
        }
        if let (Outcome::Unknown(reason), _) | (_, Outcome::Unknown(reason)) = (&old, &new) {
            result
                .unknown
                .push(format!("{reason} ({})", group(fixture)));
            continue;
        }
        result.runs += 1;
        if !same(&old, &new, &after.ret_type) {
            result.mismatch = Some(format!(
                "{} {} {}: trial {trial}: {}",
                fixture.display(),
                vlk_base::addr::format(function.addr),
                function.name.as_deref().unwrap_or("-"),
                describe(&old, &new, &after.ret_type)
            ));
            break;
        }
    }
    result
}

fn collect(dir: &Path, relative: &Path, found: &mut Vec<PathBuf>) {
    for entry in std::fs::read_dir(dir).unwrap_or_else(|error| panic!("{}: {error}", dir.display()))
    {
        let path = entry.unwrap_or_else(|error| panic!("{error}")).path();
        let name = relative.join(path.file_name().expect("a directory entry has a name"));
        if path.is_dir() {
            collect(&path, &name, found);
        } else if path.extension().is_none_or(|extension| {
            !matches!(
                extension.to_str(),
                Some("txt" | "pdb" | "vlk-cache" | "tmp")
            )
        }) {
            found.push(name);
        }
    }
}

/// An image as the service opens it, with jump table cases folded back.
fn open(path: &std::path::Path) -> vlk_program::program::Program {
    let mut program = vlk_program::program::Program::new(
        crate::lift::load(path).unwrap_or_else(|error| panic!("{error}")),
    );
    super::absorb_jump_table_cases(&mut program);
    program
}

/// Prints both versions of one function and their runs:
/// `VLK_FIXTURE=arm64.o VLK_FUNCTION=0x0 cargo test --release -p vlk-analysis explain_function -- --ignored --nocapture`
#[test]
#[ignore]
fn explain_function() {
    let root = PathBuf::from(concat!(env!("CARGO_MANIFEST_DIR"), "/../.."));
    let fixture = std::env::var("VLK_FIXTURE").expect("set VLK_FIXTURE");
    let addr = std::env::var("VLK_FUNCTION").expect("set VLK_FUNCTION");
    let addr = vlk_base::addr::parse(&addr).expect("VLK_FUNCTION is an address");
    let program = open(&root.join("tests/fixtures").join(fixture));
    let function = program
        .image
        .functions
        .get(&addr)
        .expect("a function at that address");
    let lifted = lift_function(&program, function).unwrap_or_else(|error| panic!("{error}"));
    let before = lifted.ir.clone();
    let after = optimize(&program, lifted, None).unwrap().ir;
    let structured = vlk_render::render::structure::lower::lower(
        &after,
        &vlk_render::render::structure::structure_best(&after),
    );
    println!(
        "== before\n{}\n== after\n{}\n== structured\n{}",
        before.render(),
        after.render(),
        structured.render()
    );
    let memory = initial_memory(&program.image);
    for trial in 0..TRIALS {
        let registers = registers(&program.image, trial);
        let setup = setup(&program.image, trial, &registers, &memory);
        let old = eval::run(&before, &setup);
        let new = eval::run(&after, &setup);
        println!("  structured {:x?}", eval::run(&structured, &setup));
        let verdict = if matches!(old, Outcome::Unknown(_)) || matches!(new, Outcome::Unknown(_)) {
            "skipped"
        } else if same(&old, &new, &after.ret_type) {
            "same"
        } else {
            "DIFFERENT"
        };
        println!(
            "trial {trial} {verdict} registers {registers:x?}\n  before {old:x?}\n  after  {new:x?}"
        );
    }
}

/// The same calls in the same order, passing the same arguments as far as
/// both name them: a call whose callee's arity is unknown passes every
/// argument register, and one the analysis counted passes just those.
fn calls_match(old: &[eval::Call], new: &[eval::Call]) -> bool {
    old.len() == new.len()
        && old.iter().zip(new).all(|(a, b)| {
            a.target == b.target && a.arguments.iter().zip(&b.arguments).all(|(x, y)| x == y)
        })
}

fn same(old: &Outcome, new: &Outcome, ret: &CType) -> bool {
    match (old, new) {
        (
            Outcome::Return {
                value: a,
                calls: ca,
                memory: ma,
            },
            Outcome::Return {
                value: b,
                calls: cb,
                memory: mb,
            },
        ) => {
            let values_match = return_bits(ret).is_none_or(|bits| {
                let mask = if bits >= 64 {
                    u64::MAX
                } else {
                    (1 << bits) - 1
                };
                a & mask == b & mask
            });
            values_match && calls_match(ca, cb) && ma == mb
        }
        (
            Outcome::Trap {
                calls: ca,
                memory: ma,
            },
            Outcome::Trap {
                calls: cb,
                memory: mb,
            },
        ) => calls_match(ca, cb) && ma == mb,
        _ => false,
    }
}

fn describe(old: &Outcome, new: &Outcome, ret: &CType) -> String {
    match (old, new) {
        (
            Outcome::Return {
                value: a,
                calls: ca,
                memory: ma,
            },
            Outcome::Return {
                value: b,
                calls: cb,
                memory: mb,
            },
        ) => {
            if !calls_match(ca, cb) {
                format!("calls differ ({} before, {} after)", ca.len(), cb.len())
            } else if ma != mb {
                "memory differs".to_owned()
            } else {
                format!("returns 0x{b:x} instead of 0x{a:x} ({ret})")
            }
        }
        (Outcome::Trap { .. }, Outcome::Trap { .. }) => {
            "memory or calls differ before the trap".to_owned()
        }
        _ => "one run returns and the other traps".to_owned(),
    }
}