Lifted code
A disassembler or a lifter turns machine code into an IR; bitwright::lift reads three of them
into expressions, so that what a block computes can be simplified, compared or proved:
- p-code, Ghidra’s IR, one operation per line, varnodes
(space, offset, size)as Ghidra prints them, or with a register name for the offset ((register, RAX, 8)); - VEX, the IR of Valgrind and angr, as pyvex prints an IRSB;
- LLVM IR functions (feature
prove), with loads, stores andgetelementptrover one flat memory.
A block becomes a Block: the registers it reads before writing them (symbols named after
them), the final value of each register it writes, its stores, its conditional exits, and
where it continues. Registers are read and written by byte range. On x86-64, eax, ax,
al and ah are parts of rax. On AArch64, w0 through w30 and wsp name the low
four bytes of their respective X register or stack pointer. A raw four-byte write preserves the
other bytes. A lifted instruction that clears them expresses the full-width write in its IR.
Memory is a memory, so a value stored and loaded
back is the value. One block is read straight through: an exit is recorded, and the code after
it runs as if the exit was not taken.
#![allow(unused)]
fn main() {
use bitwright::engine::Engine;
use bitwright::lift::{pcode, vex};
use bitwright::Context;
let mut cx = Context::new();
// An MBA, as pyvex prints the IRSB of `(x & y) * (x | y) + (x & ~y) * (~x & y)`.
let block = vex(
&mut cx,
"t0 = GET:I64(rdi)
t1 = GET:I64(rsi)
t2 = And64(t0,t1)
t3 = Or64(t0,t1)
t4 = Mul64(t2,t3)
t5 = Not64(t1)
t6 = And64(t0,t5)
t7 = Not64(t0)
t8 = And64(t7,t1)
t9 = Mul64(t6,t8)
t10 = Add64(t4,t9)
PUT(rax) = t10",
)?;
let engine = bitwright::engine::Engine::builder()
.builtin()
.strategy(bitwright::engine::Strategy::deobfuscate().with_mba(Default::default()))
.build()?;
let rax = engine.simplify(&mut cx, block.register("rax").unwrap())?;
assert_eq!(cx.display(rax.expr).to_string(), "rdi * rsi");
// A stack spill in p-code, Ghidra's register offsets: the reload is the spilled value.
let block = pcode(
&mut cx,
"(register, 0x20, 8) = INT_SUB (register, 0x20, 8) , (const, 0x8, 8)
STORE (const, 0x1b1, 4) , (register, 0x20, 8) , (register, 0x38, 8)
(register, 0x0, 8) = LOAD (const, 0x1b1, 4) , (register, 0x20, 8)
(register, 0x0, 8) = INT_ADD (register, 0x0, 8) , (const, 0x1, 8)",
)?;
let rax = Engine::standard().simplify(&mut cx, block.register("reg_0x0").unwrap())?;
assert_eq!(cx.display(rax.expr).to_string(), "reg_0x38 + 1");
Ok::<(), Box<dyn std::error::Error>>(())
}
bitwright lift file.vex (or .pcode, .ll, or --from) prints each register a block writes,
each store and each exit, deobfuscated; --synth also searches for a smaller equal expression,
each proof within --synth-conflicts and --synth-propagations, and a note on standard error
names an output whose candidate was left unproved (see Synthesis).
A call, a helper VEX leaves opaque (an unknown value, for a helper call such as a flag
computation), a loop or an operation outside the front end’s subset is reported, not guessed.
tools/plugins/ has scripts
for Ghidra (p-code), Binary Ninja (its low-level IL) and IDA Pro with Hex-Rays (the ctree) that
hand the code at the cursor to the Python package. They are written against each tool’s
documented API and are not tested, as none of the tools is available where bitwright is
developed.
Choosing the host request
For smaller IR, pass the block’s live outputs to Engine::run. For “is this address a
constant on this path?”, ask batch::Ask::constant(address).under(&facts): a nonconstant
result from the simplifier alone doesn’t answer that question. Build the expressions for
one analysis boundary in one Context, preserving symbol identities, and keep a Session
per proof configuration across boundaries.
Compare Split::none() (prove the whole expression) with Split::shared() (prove common
subexpressions first) on the host’s exported requests. High node overlap alone is not
enough: if the whole expressions fold cheaply, discovering and proving shared parts can
take longer without deciding another address. Compare answers, tasks solved and boundary
p95 at the same budgets, with deep retries included. See the
walk replay.