#![no_std]
#![forbid(unsafe_code)]
mod registers;
#[cfg(not(any(feature = "x86", feature = "aarch64")))]
compile_error!("binbun-disasm needs feature `x86` or `aarch64` (or both)");
use core::fmt;
use core::iter::FusedIterator;
#[cfg(feature = "aarch64")]
pub use binbun_aarch64 as aarch64;
pub use binbun_isa::{Flow, Target};
#[cfg(feature = "x86")]
pub use binbun_x86 as x86;
#[cfg(feature = "aarch64")]
pub use registers::AArch64Register;
pub use registers::{Register, Registers};
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum Arch {
#[cfg(feature = "x86")]
X86(x86::Mode),
#[cfg(feature = "aarch64")]
AArch64,
}
impl Arch {
pub const fn max_length(self) -> usize {
match self {
#[cfg(feature = "x86")]
Self::X86(_) => x86::MAX_LENGTH,
#[cfg(feature = "aarch64")]
Self::AArch64 => 4,
}
}
pub const fn invalid_step(self) -> usize {
match self {
#[cfg(feature = "x86")]
Self::X86(_) => 1,
#[cfg(feature = "aarch64")]
Self::AArch64 => 4,
}
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum DecodeError {
#[cfg(feature = "x86")]
X86(x86::DecodeError),
#[cfg(feature = "aarch64")]
AArch64(aarch64::Error),
}
impl DecodeError {
pub const fn is_truncated(self) -> bool {
match self {
#[cfg(feature = "x86")]
Self::X86(error) => matches!(error, x86::DecodeError::TooShort),
#[cfg(feature = "aarch64")]
Self::AArch64(error) => matches!(error, aarch64::Error::TooShort),
}
}
}
impl fmt::Display for DecodeError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
#[cfg(feature = "x86")]
Self::X86(error) => error.fmt(f),
#[cfg(feature = "aarch64")]
Self::AArch64(error) => error.fmt(f),
}
}
}
impl core::error::Error for DecodeError {}
#[cfg(feature = "x86")]
impl From<x86::DecodeError> for DecodeError {
fn from(error: x86::DecodeError) -> Self {
Self::X86(error)
}
}
#[cfg(feature = "aarch64")]
impl From<aarch64::Error> for DecodeError {
fn from(error: aarch64::Error) -> Self {
Self::AArch64(error)
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
enum Native {
#[cfg(feature = "x86")]
X86(x86::Instruction, x86::Mode),
#[cfg(feature = "aarch64")]
AArch64(aarch64::Instruction),
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Insn<'a> {
address: u64,
bytes: &'a [u8],
native: Native,
}
impl<'a> Insn<'a> {
#[cfg(feature = "x86")]
pub fn from_x86(
instruction: x86::Instruction,
mode: x86::Mode,
bytes: &'a [u8],
address: u64,
) -> Self {
Self {
address,
bytes: bytes.get(..instruction.len()).unwrap_or(bytes),
native: Native::X86(instruction, mode),
}
}
#[cfg(feature = "aarch64")]
pub fn from_aarch64(instruction: aarch64::Instruction, bytes: &'a [u8], address: u64) -> Self {
Self {
address,
bytes: bytes.get(..4).unwrap_or(bytes),
native: Native::AArch64(instruction),
}
}
pub const fn arch(&self) -> Arch {
match self.native {
#[cfg(feature = "x86")]
Native::X86(_, mode) => Arch::X86(mode),
#[cfg(feature = "aarch64")]
Native::AArch64(_) => Arch::AArch64,
}
}
pub const fn address(&self) -> u64 {
self.address
}
#[allow(clippy::len_without_is_empty, reason = "an instruction is never empty")]
pub const fn len(&self) -> usize {
self.bytes.len()
}
pub const fn bytes(&self) -> &'a [u8] {
self.bytes
}
pub fn mnemonic(&self) -> &'static str {
match &self.native {
#[cfg(feature = "x86")]
Native::X86(instruction, _) => instruction.intel_mnemonic(),
#[cfg(feature = "aarch64")]
Native::AArch64(instruction) => instruction.mnemonic(),
}
}
pub fn flow(&self) -> Flow {
match &self.native {
#[cfg(feature = "x86")]
Native::X86(instruction, _) => binbun_isa::Instruction::flow(instruction, self.address),
#[cfg(feature = "aarch64")]
Native::AArch64(instruction) => instruction.flow(self.address),
}
}
pub fn registers_read(&self) -> Registers {
match &self.native {
#[cfg(feature = "x86")]
Native::X86(instruction, _) => registers::x86(instruction, registers::Side::Read),
#[cfg(feature = "aarch64")]
Native::AArch64(instruction) => registers::aarch64(*instruction, registers::Side::Read),
}
}
pub fn registers_written(&self) -> Registers {
match &self.native {
#[cfg(feature = "x86")]
Native::X86(instruction, _) => registers::x86(instruction, registers::Side::Write),
#[cfg(feature = "aarch64")]
Native::AArch64(instruction) => {
registers::aarch64(*instruction, registers::Side::Write)
}
}
}
#[cfg(feature = "x86")]
pub const fn as_x86(&self) -> Option<&x86::Instruction> {
match &self.native {
Native::X86(instruction, _) => Some(instruction),
#[cfg(feature = "aarch64")]
Native::AArch64(_) => None,
}
}
#[cfg(feature = "aarch64")]
pub const fn as_aarch64(&self) -> Option<&aarch64::Instruction> {
match &self.native {
#[cfg(feature = "x86")]
Native::X86(..) => None,
Native::AArch64(instruction) => Some(instruction),
}
}
}
impl fmt::Display for Insn<'_> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match &self.native {
#[cfg(feature = "x86")]
Native::X86(instruction, _) => instruction.intel(self.address).fmt(f),
#[cfg(feature = "aarch64")]
Native::AArch64(instruction) => instruction.display(self.address).fmt(f),
}
}
}
pub fn decode(arch: Arch, bytes: &[u8], address: u64) -> Result<Insn<'_>, DecodeError> {
let native = match arch {
#[cfg(feature = "x86")]
Arch::X86(mode) => Native::X86(x86::decode(bytes, mode)?, mode),
#[cfg(feature = "aarch64")]
Arch::AArch64 => Native::AArch64(aarch64::decode_bytes(bytes)?),
};
let len = match &native {
#[cfg(feature = "x86")]
Native::X86(instruction, _) => instruction.len(),
#[cfg(feature = "aarch64")]
Native::AArch64(_) => 4,
};
Ok(Insn {
address,
bytes: bytes.get(..len).unwrap_or(bytes),
native,
})
}
#[derive(Debug, Clone)]
pub struct Decoder<'a> {
arch: Arch,
bytes: &'a [u8],
base: u64,
offset: usize,
}
impl<'a> Decoder<'a> {
pub const fn new(arch: Arch, bytes: &'a [u8], address: u64) -> Self {
Self {
arch,
bytes,
base: address,
offset: 0,
}
}
pub const fn arch(&self) -> Arch {
self.arch
}
pub const fn offset(&self) -> usize {
self.offset
}
pub const fn address(&self) -> u64 {
self.base.wrapping_add(self.offset as u64)
}
pub const fn set_offset(&mut self, offset: usize) {
self.offset = offset;
}
pub fn remaining(&self) -> &'a [u8] {
self.bytes.get(self.offset..).unwrap_or_default()
}
}
impl<'a> Iterator for Decoder<'a> {
type Item = (u64, Result<Insn<'a>, DecodeError>);
fn next(&mut self) -> Option<Self::Item> {
let rest = self.remaining();
if rest.is_empty() {
return None;
}
let address = self.address();
let result = decode(self.arch, rest, address);
let step = result.as_ref().map_or(self.arch.invalid_step(), Insn::len);
self.offset = self.offset.saturating_add(step);
Some((address, result))
}
fn size_hint(&self) -> (usize, Option<usize>) {
let rest = self.remaining().len();
match self.arch {
#[cfg(feature = "x86")]
Arch::X86(_) => (rest.div_ceil(x86::MAX_LENGTH), Some(rest)),
#[cfg(feature = "aarch64")]
Arch::AArch64 => (rest.div_ceil(4), Some(rest.div_ceil(4))),
}
}
}
impl FusedIterator for Decoder<'_> {}