Skip to main content

Crate binbun_disasm

Crate binbun_disasm 

Source
Expand description

Architecture-independent instruction decoding and inspection for x86 and AArch64.

decode and Decoder take an Arch chosen by the caller (nothing is guessed from the bytes) and give an Insn: its address, the bytes it was decoded from (borrowed from the caller’s buffer), its length, its mnemonic, its text, its control flow and the registers it reads and writes, whichever the architecture. Everything else stays with the native decoders, binbun_x86 and binbun_aarch64 (re-exported as x86 and aarch64), whose instruction Insn::as_x86 and Insn::as_aarch64 return: operands, flags, conditions, encodings, text options and tokens.

The crate is no_std and allocates nothing: an Insn is the native instruction behind an enum, and its facts are computed when asked. Features x86 and aarch64 (both default) choose the architectures: without one, its Arch variant, Insn’s as_ method and its decoder crate are left out. Flow and Target are re-exported, so that no other crate is needed.

  • Insn::mnemonic is the mnemonic the text writes, lower case: on x86 the word after any prefix words (movsb of rep movsb, not XED’s iclass REP_MOVSB), on AArch64 after alias selection, with its suffix (mov, b.eq).
  • The text (Display) is each decoder’s own default: Intel syntax as XED writes it on x86, LLVM’s on AArch64 (a tab after the mnemonic); branch targets as addresses on both. Nothing is normalised.
  • Insn::registers_read and Insn::registers_written are what the decoders report an instruction may read and write; see Registers.
use binbun_disasm::{Arch, Decoder, Flow, Target, x86};

let code = [0x55, 0xe8, 0x00, 0x00, 0x00, 0x00, 0xc3]; // push rbp; call; ret
let mut decoder = Decoder::new(Arch::X86(x86::Mode::Bits64), &code, 0x1000);
let (address, push) = decoder.next().unwrap();
let push = push?;
assert_eq!((address, push.mnemonic(), push.to_string()), (0x1000, "push", "push rbp".into()));
assert_eq!(push.bytes(), [0x55]);
let names = |registers: &binbun_disasm::Registers| {
    registers.iter().map(ToString::to_string).collect::<Vec<_>>()
};
assert_eq!(names(&push.registers_read()), ["rbp", "rsp"]);
assert_eq!(names(&push.registers_written()), ["rsp"]);
let (_, call) = decoder.next().unwrap();
assert_eq!(call?.flow(), Flow::Call(Target::Address(0x1006)));

let a64 = [0xe0, 0x03, 0x01, 0xaa]; // orr x0, xzr, x1
let mov = binbun_disasm::decode(Arch::AArch64, &a64, 0x2000)?;
assert_eq!((mov.mnemonic(), mov.to_string()), ("mov", "mov\tx0, x1".into()));
assert_eq!(mov.as_aarch64().unwrap().encoding().mnemonic(), "ORR");

Re-exports§

pub use binbun_aarch64 as aarch64;
pub use binbun_x86 as x86;

Structs§

Decoder
Decodes the instructions of a buffer one after another (a linear sweep), for an architecture.
Insn
A decoded instruction at its address, with the bytes it was decoded from.
Registers
What an instruction may read or write: each register once, in the order of the instruction’s operands (implied ones included), then AArch64’s implicit registers.

Enums§

AArch64Register
An AArch64 register, as the text names it.
Arch
The architecture to decode for, with x86’s processor mode.
DecodeError
Why bytes are no instruction: the native decoder’s error.
Flow
How an instruction transfers control.
Register
A register, as its architecture names it.
Target
Where a jump or call goes, as far as the instruction alone tells.

Functions§

decode
Decodes the instruction at the start of bytes, which is at address, for arch.