Skip to content

Commit

Permalink
more rework
Browse files Browse the repository at this point in the history
  • Loading branch information
xorpse committed Jun 11, 2024
1 parent 525950b commit 8dcbc41
Show file tree
Hide file tree
Showing 7 changed files with 251 additions and 145 deletions.
1 change: 1 addition & 0 deletions fugue-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ ouroboros = "0.18"
regex = "1"
rkyv = "0.7"
rustc-hash = "1.1"
sealed = "0.5"
serde = { version = "1", features = ["derive"] }
serde_yaml = "0.9"
static_init = "1"
Expand Down
42 changes: 17 additions & 25 deletions fugue-core/src/arch/aarch64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,21 @@ use std::cell::{Cell, RefCell};
use fugue_ir::disassembly::IRBuilderArena;
use fugue_ir::Address;

use thiserror::Error;

use yaxpeax_arch::*;
use yaxpeax_arm::armv8::a64::Opcode;

pub use yaxpeax_arm::armv8::a64::{
DecodeError as AArch64DecoderError, InstDecoder as AArch64InstDecoder, Instruction as AArch64Instruction,
DecodeError as AArch64DecoderError, InstDecoder as AArch64InstDecoder,
Instruction as AArch64Instruction,
};

use crate::ir::PCode;
use crate::lifter::{InsnLifter, LiftedInsn, LiftedInsnProperties, Lifter};
use crate::lifter::{InsnLifter, LiftedInsn, LiftedInsnProperties, Lifter, LifterError};

pub struct AArch64InsnLifter {
decoder: AArch64InstDecoder,
}

#[derive(Debug, Error)]
pub enum AArch64LifterError {
#[error(transparent)]
Decoder(#[from] AArch64DecoderError),
#[error(transparent)]
Lifter(#[from] fugue_ir::error::Error),
}

impl AArch64InsnLifter {
pub fn new() -> Self {
Self::new_with(AArch64InstDecoder::default())
Expand All @@ -35,31 +26,32 @@ impl AArch64InsnLifter {
pub fn new_with(decoder: AArch64InstDecoder) -> Self {
Self { decoder }
}

pub fn boxed<'a>(self) -> Box<dyn InsnLifter<'a>> {
Box::new(self)
}
}

fn should_lift(insn: &AArch64Instruction) -> bool {
match insn.opcode {
Opcode::B
| Opcode::BL
| Opcode::CBZ
| Opcode::CBNZ
| Opcode::SVC => true,
Opcode::B | Opcode::BL | Opcode::CBZ | Opcode::CBNZ | Opcode::SVC => true,
_ => false,
}
}

impl<'a> InsnLifter<'a, AArch64Instruction> for AArch64InsnLifter {
type Error = AArch64LifterError;

impl<'a> InsnLifter<'a> for AArch64InsnLifter {
fn properties<'b>(
&mut self,
lifter: &mut Lifter,
irb: &'a IRBuilderArena,
address: Address,
bytes: &'b [u8],
) -> Result<LiftedInsn<'a, 'b, AArch64Instruction>, Self::Error> {
) -> Result<LiftedInsn<'a, 'b>, LifterError> {
let mut reader = yaxpeax_arch::U8Reader::new(bytes);
let insn = self.decoder.decode(&mut reader)?;
let insn = self
.decoder
.decode(&mut reader)
.map_err(LifterError::decode)?;
let size = insn.len().to_const() as u8;

if should_lift(&insn) {
Expand All @@ -68,7 +60,9 @@ impl<'a> InsnLifter<'a, AArch64Instruction> for AArch64InsnLifter {
operations,
delay_slots,
length,
} = lifter.lift(irb, address, bytes)?;
} = lifter
.lift(irb, address, bytes)
.map_err(LifterError::lift)?;

Ok(LiftedInsn {
address,
Expand All @@ -77,7 +71,6 @@ impl<'a> InsnLifter<'a, AArch64Instruction> for AArch64InsnLifter {
operations: RefCell::new(Some(operations)),
delay_slots,
length,
data: insn,
})
} else {
Ok(LiftedInsn {
Expand All @@ -87,7 +80,6 @@ impl<'a> InsnLifter<'a, AArch64Instruction> for AArch64InsnLifter {
operations: RefCell::new(None),
delay_slots: 0,
length: size,
data: insn,
})
}
}
Expand Down
33 changes: 14 additions & 19 deletions fugue-core/src/arch/arm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ use std::cell::{Cell, RefCell};
use fugue_ir::disassembly::IRBuilderArena;
use fugue_ir::Address;

use thiserror::Error;

use yaxpeax_arch::*;
use yaxpeax_arm::armv7::{Opcode, Operand, Reg};

Expand All @@ -13,20 +11,12 @@ pub use yaxpeax_arm::armv7::{
};

use crate::ir::PCode;
use crate::lifter::{InsnLifter, LiftedInsn, LiftedInsnProperties, Lifter};
use crate::lifter::{InsnLifter, LiftedInsn, LiftedInsnProperties, Lifter, LifterError};

pub struct ARMInsnLifter {
decoder: ARMInstDecoder,
}

#[derive(Debug, Error)]
pub enum ARMLifterError {
#[error(transparent)]
Decoder(#[from] ARMDecoderError),
#[error(transparent)]
Lifter(#[from] fugue_ir::error::Error),
}

impl ARMInsnLifter {
pub fn new() -> Self {
Self::new_with(ARMInstDecoder::armv7())
Expand All @@ -35,6 +25,10 @@ impl ARMInsnLifter {
pub fn new_with(decoder: ARMInstDecoder) -> Self {
Self { decoder }
}

pub fn boxed<'a>(self) -> Box<dyn InsnLifter<'a>> {
Box::new(self)
}
}

fn should_lift(insn: &ARMInstruction) -> bool {
Expand All @@ -53,18 +47,19 @@ fn should_lift(insn: &ARMInstruction) -> bool {
}
}

impl<'a> InsnLifter<'a, ARMInstruction> for ARMInsnLifter {
type Error = ARMLifterError;

impl<'a> InsnLifter<'a> for ARMInsnLifter {
fn properties<'b>(
&mut self,
lifter: &mut Lifter,
irb: &'a IRBuilderArena,
address: Address,
bytes: &'b [u8],
) -> Result<LiftedInsn<'a, 'b, ARMInstruction>, Self::Error> {
) -> Result<LiftedInsn<'a, 'b>, LifterError> {
let mut reader = yaxpeax_arch::U8Reader::new(bytes);
let insn = self.decoder.decode(&mut reader)?;
let insn = self
.decoder
.decode(&mut reader)
.map_err(LifterError::decode)?;
let size = insn.len().to_const() as u8;

if should_lift(&insn) {
Expand All @@ -73,7 +68,9 @@ impl<'a> InsnLifter<'a, ARMInstruction> for ARMInsnLifter {
operations,
delay_slots,
length,
} = lifter.lift(irb, address, bytes)?;
} = lifter
.lift(irb, address, bytes)
.map_err(LifterError::lift)?;

Ok(LiftedInsn {
address,
Expand All @@ -82,7 +79,6 @@ impl<'a> InsnLifter<'a, ARMInstruction> for ARMInsnLifter {
operations: RefCell::new(Some(operations)),
delay_slots,
length,
data: insn,
})
} else {
Ok(LiftedInsn {
Expand All @@ -92,7 +88,6 @@ impl<'a> InsnLifter<'a, ARMInstruction> for ARMInsnLifter {
operations: RefCell::new(None),
delay_slots: 0,
length: size,
data: insn,
})
}
}
Expand Down
Loading

0 comments on commit 8dcbc41

Please sign in to comment.