diff options
| author | Rusty Wagner <rusty.wagner@gmail.com> | 2023-12-19 22:39:11 -0700 |
|---|---|---|
| committer | Rusty Wagner <rusty.wagner@gmail.com> | 2024-01-04 11:02:14 -0700 |
| commit | 0748dfe65398f17064ed744a3d198f144d0aa2c5 (patch) | |
| tree | adde2cf25a4b9812c8322ed326f361b23c1681e5 /rust/src/llil | |
| parent | e9e5c3e3568351e99c5034e49ae21f0b8d12a4c2 (diff) | |
Support intrinsics in Rust architecture plugins
Diffstat (limited to 'rust/src/llil')
| -rw-r--r-- | rust/src/llil/expression.rs | 99 | ||||
| -rw-r--r-- | rust/src/llil/instruction.rs | 8 | ||||
| -rw-r--r-- | rust/src/llil/lifting.rs | 97 | ||||
| -rw-r--r-- | rust/src/llil/operation.rs | 17 |
4 files changed, 205 insertions, 16 deletions
diff --git a/rust/src/llil/expression.rs b/rust/src/llil/expression.rs index 2898d89f..60d13521 100644 --- a/rust/src/llil/expression.rs +++ b/rust/src/llil/expression.rs @@ -149,6 +149,31 @@ where LLIL_BOOL_TO_INT => ExprInfo::BoolToInt(Operation::new(function, op)), + LLIL_FADD => ExprInfo::Fadd(Operation::new(function, op)), + LLIL_FSUB => ExprInfo::Fsub(Operation::new(function, op)), + LLIL_FMUL => ExprInfo::Fmul(Operation::new(function, op)), + LLIL_FDIV => ExprInfo::Fdiv(Operation::new(function, op)), + + LLIL_FSQRT => ExprInfo::Fsqrt(Operation::new(function, op)), + LLIL_FNEG => ExprInfo::Fneg(Operation::new(function, op)), + LLIL_FABS => ExprInfo::Fabs(Operation::new(function, op)), + LLIL_FLOAT_TO_INT => ExprInfo::FloatToInt(Operation::new(function, op)), + LLIL_INT_TO_FLOAT => ExprInfo::IntToFloat(Operation::new(function, op)), + LLIL_FLOAT_CONV => ExprInfo::FloatConv(Operation::new(function, op)), + LLIL_ROUND_TO_INT => ExprInfo::RoundToInt(Operation::new(function, op)), + LLIL_FLOOR => ExprInfo::Floor(Operation::new(function, op)), + LLIL_CEIL => ExprInfo::Ceil(Operation::new(function, op)), + LLIL_FTRUNC => ExprInfo::Ftrunc(Operation::new(function, op)), + + LLIL_FCMP_E => ExprInfo::FcmpE(Operation::new(function, op)), + LLIL_FCMP_NE => ExprInfo::FcmpNE(Operation::new(function, op)), + LLIL_FCMP_LT => ExprInfo::FcmpLT(Operation::new(function, op)), + LLIL_FCMP_LE => ExprInfo::FcmpLE(Operation::new(function, op)), + LLIL_FCMP_GT => ExprInfo::FcmpGT(Operation::new(function, op)), + LLIL_FCMP_GE => ExprInfo::FcmpGE(Operation::new(function, op)), + LLIL_FCMP_O => ExprInfo::FcmpO(Operation::new(function, op)), + LLIL_FCMP_UO => ExprInfo::FcmpUO(Operation::new(function, op)), + LLIL_UNIMPL => ExprInfo::Unimpl(Operation::new(function, op)), LLIL_UNIMPL_MEM => ExprInfo::UnimplMem(Operation::new(function, op)), @@ -188,7 +213,9 @@ where match *info { CmpE(ref op) | CmpNe(ref op) | CmpSlt(ref op) | CmpUlt(ref op) | CmpSle(ref op) - | CmpUle(ref op) | CmpSge(ref op) | CmpUge(ref op) | CmpSgt(ref op) | CmpUgt(ref op) => { + | CmpUle(ref op) | CmpSge(ref op) | CmpUge(ref op) | CmpSgt(ref op) | CmpUgt(ref op) + | FcmpE(ref op) | FcmpNE(ref op) | FcmpLT(ref op) | FcmpLE(ref op) | FcmpGE(ref op) + | FcmpGT(ref op) | FcmpO(ref op) | FcmpUO(ref op) => { visit!(f, &op.left()); visit!(f, &op.right()); } @@ -201,7 +228,8 @@ where Add(ref op) | Sub(ref op) | And(ref op) | Or(ref op) | Xor(ref op) | Lsl(ref op) | Lsr(ref op) | Asr(ref op) | Rol(ref op) | Ror(ref op) | Mul(ref op) | MulsDp(ref op) - | MuluDp(ref op) | Divu(ref op) | Divs(ref op) | Modu(ref op) | Mods(ref op) => { + | MuluDp(ref op) | Divu(ref op) | Divs(ref op) | Modu(ref op) | Mods(ref op) + | Fadd(ref op) | Fsub(ref op) | Fmul(ref op) | Fdiv(ref op) => { visit!(f, &op.left()); visit!(f, &op.right()); } @@ -213,7 +241,9 @@ where } Neg(ref op) | Not(ref op) | Sx(ref op) | Zx(ref op) | LowPart(ref op) - | BoolToInt(ref op) => { + | BoolToInt(ref op) | Fsqrt(ref op) | Fneg(ref op) | Fabs(ref op) | FloatToInt(ref op) + | IntToFloat(ref op) | FloatConv(ref op) | RoundToInt(ref op) | Floor(ref op) + | Ceil(ref op) | Ftrunc(ref op) => { visit!(f, &op.operand()); } @@ -412,6 +442,30 @@ where //TestBit(Operation<'func, A, M, F, operation::TestBit>), // TODO BoolToInt(Operation<'func, A, M, F, operation::UnaryOp>), + Fadd(Operation<'func, A, M, F, operation::BinaryOp>), + Fsub(Operation<'func, A, M, F, operation::BinaryOp>), + Fmul(Operation<'func, A, M, F, operation::BinaryOp>), + Fdiv(Operation<'func, A, M, F, operation::BinaryOp>), + Fsqrt(Operation<'func, A, M, F, operation::UnaryOp>), + Fneg(Operation<'func, A, M, F, operation::UnaryOp>), + Fabs(Operation<'func, A, M, F, operation::UnaryOp>), + FloatToInt(Operation<'func, A, M, F, operation::UnaryOp>), + IntToFloat(Operation<'func, A, M, F, operation::UnaryOp>), + FloatConv(Operation<'func, A, M, F, operation::UnaryOp>), + RoundToInt(Operation<'func, A, M, F, operation::UnaryOp>), + Floor(Operation<'func, A, M, F, operation::UnaryOp>), + Ceil(Operation<'func, A, M, F, operation::UnaryOp>), + Ftrunc(Operation<'func, A, M, F, operation::UnaryOp>), + + FcmpE(Operation<'func, A, M, F, operation::Condition>), + FcmpNE(Operation<'func, A, M, F, operation::Condition>), + FcmpLT(Operation<'func, A, M, F, operation::Condition>), + FcmpLE(Operation<'func, A, M, F, operation::Condition>), + FcmpGE(Operation<'func, A, M, F, operation::Condition>), + FcmpGT(Operation<'func, A, M, F, operation::Condition>), + FcmpO(Operation<'func, A, M, F, operation::Condition>), + FcmpUO(Operation<'func, A, M, F, operation::Condition>), + // TODO ADD_OVERFLOW Unimpl(Operation<'func, A, M, F, operation::NoArgs>), UnimplMem(Operation<'func, A, M, F, operation::UnimplMem>), @@ -467,7 +521,8 @@ where match *self { CmpE(ref op) | CmpNe(ref op) | CmpSlt(ref op) | CmpUlt(ref op) | CmpSle(ref op) | CmpUle(ref op) | CmpSge(ref op) | CmpUge(ref op) | CmpSgt(ref op) - | CmpUgt(ref op) => Some(op), + | CmpUgt(ref op) | FcmpE(ref op) | FcmpNE(ref op) | FcmpLT(ref op) | FcmpLE(ref op) + | FcmpGE(ref op) | FcmpGT(ref op) | FcmpO(ref op) | FcmpUO(ref op) => Some(op), _ => None, } } @@ -479,7 +534,7 @@ where Add(ref op) | Sub(ref op) | And(ref op) | Or(ref op) | Xor(ref op) | Lsl(ref op) | Lsr(ref op) | Asr(ref op) | Rol(ref op) | Ror(ref op) | Mul(ref op) | MulsDp(ref op) | MuluDp(ref op) | Divu(ref op) | Divs(ref op) | Modu(ref op) - | Mods(ref op) => Some(op), + | Mods(ref op) | Fadd(ref op) | Fsub(ref op) | Fmul(ref op) | Fdiv(ref op) => Some(op), _ => None, } } @@ -511,7 +566,9 @@ where match *self { Neg(ref op) | Not(ref op) | Sx(ref op) | Zx(ref op) | LowPart(ref op) - | BoolToInt(ref op) => Some(op), + | BoolToInt(ref op) | Fsqrt(ref op) | Fneg(ref op) | Fabs(ref op) + | FloatToInt(ref op) | IntToFloat(ref op) | FloatConv(ref op) | RoundToInt(ref op) + | Floor(ref op) | Ceil(ref op) | Ftrunc(ref op) => Some(op), _ => None, } } @@ -529,7 +586,8 @@ where CmpE(ref op) | CmpNe(ref op) | CmpSlt(ref op) | CmpUlt(ref op) | CmpSle(ref op) | CmpUle(ref op) | CmpSge(ref op) | CmpUge(ref op) | CmpSgt(ref op) - | CmpUgt(ref op) => &op.op, + | CmpUgt(ref op) | FcmpE(ref op) | FcmpNE(ref op) | FcmpLT(ref op) | FcmpLE(ref op) + | FcmpGE(ref op) | FcmpGT(ref op) | FcmpO(ref op) | FcmpUO(ref op) => &op.op, Load(ref op) => &op.op, @@ -548,12 +606,14 @@ where Add(ref op) | Sub(ref op) | And(ref op) | Or(ref op) | Xor(ref op) | Lsl(ref op) | Lsr(ref op) | Asr(ref op) | Rol(ref op) | Ror(ref op) | Mul(ref op) | MulsDp(ref op) | MuluDp(ref op) | Divu(ref op) | Divs(ref op) | Modu(ref op) - | Mods(ref op) => &op.op, + | Mods(ref op) | Fadd(ref op) | Fsub(ref op) | Fmul(ref op) | Fdiv(ref op) => &op.op, DivuDp(ref op) | DivsDp(ref op) | ModuDp(ref op) | ModsDp(ref op) => &op.op, Neg(ref op) | Not(ref op) | Sx(ref op) | Zx(ref op) | LowPart(ref op) - | BoolToInt(ref op) => &op.op, + | BoolToInt(ref op) | Fsqrt(ref op) | Fneg(ref op) | Fabs(ref op) + | FloatToInt(ref op) | IntToFloat(ref op) | FloatConv(ref op) | RoundToInt(ref op) + | Floor(ref op) | Ceil(ref op) | Ftrunc(ref op) => &op.op, UnimplMem(ref op) => &op.op, //TestBit(Operation<'func, A, M, F, operation::TestBit>), // TODO @@ -578,7 +638,9 @@ where CmpE(ref _op) | CmpNe(ref _op) | CmpSlt(ref _op) | CmpUlt(ref _op) | CmpSle(ref _op) | CmpUle(ref _op) | CmpSge(ref _op) | CmpUge(ref _op) - | CmpSgt(ref _op) | CmpUgt(ref _op) => None, + | CmpSgt(ref _op) | CmpUgt(ref _op) | FcmpE(ref _op) | FcmpNE(ref _op) + | FcmpLT(ref _op) | FcmpLE(ref _op) | FcmpGE(ref _op) | FcmpGT(ref _op) + | FcmpO(ref _op) | FcmpUO(ref _op) => None, Load(ref op) => op.flag_write(), @@ -597,12 +659,16 @@ where Add(ref op) | Sub(ref op) | And(ref op) | Or(ref op) | Xor(ref op) | Lsl(ref op) | Lsr(ref op) | Asr(ref op) | Rol(ref op) | Ror(ref op) | Mul(ref op) | MulsDp(ref op) | MuluDp(ref op) | Divu(ref op) | Divs(ref op) | Modu(ref op) - | Mods(ref op) => op.flag_write(), + | Mods(ref op) | Fadd(ref op) | Fsub(ref op) | Fmul(ref op) | Fdiv(ref op) => { + op.flag_write() + } DivuDp(ref op) | DivsDp(ref op) | ModuDp(ref op) | ModsDp(ref op) => op.flag_write(), Neg(ref op) | Not(ref op) | Sx(ref op) | Zx(ref op) | LowPart(ref op) - | BoolToInt(ref op) => op.flag_write(), + | BoolToInt(ref op) | Fsqrt(ref op) | Fneg(ref op) | Fabs(ref op) + | FloatToInt(ref op) | IntToFloat(ref op) | FloatConv(ref op) | RoundToInt(ref op) + | Floor(ref op) | Ceil(ref op) | Ftrunc(ref op) => op.flag_write(), UnimplMem(ref op) => op.flag_write(), //TestBit(Operation<'func, A, M, F, operation::TestBit>), // TODO @@ -629,7 +695,8 @@ where CmpE(ref op) | CmpNe(ref op) | CmpSlt(ref op) | CmpUlt(ref op) | CmpSle(ref op) | CmpUle(ref op) | CmpSge(ref op) | CmpUge(ref op) | CmpSgt(ref op) - | CmpUgt(ref op) => { + | CmpUgt(ref op) | FcmpE(ref op) | FcmpNE(ref op) | FcmpLT(ref op) | FcmpLE(ref op) + | FcmpGE(ref op) | FcmpGT(ref op) | FcmpO(ref op) | FcmpUO(ref op) => { let left = op.left(); let right = op.right(); @@ -693,7 +760,7 @@ where Add(ref op) | Sub(ref op) | And(ref op) | Or(ref op) | Xor(ref op) | Lsl(ref op) | Lsr(ref op) | Asr(ref op) | Rol(ref op) | Ror(ref op) | Mul(ref op) | MulsDp(ref op) | MuluDp(ref op) | Divu(ref op) | Divs(ref op) | Modu(ref op) - | Mods(ref op) => { + | Mods(ref op) | Fadd(ref op) | Fsub(ref op) | Fmul(ref op) | Fdiv(ref op) => { let left = op.left(); let right = op.right(); @@ -724,7 +791,9 @@ where } Neg(ref op) | Not(ref op) | Sx(ref op) | Zx(ref op) | LowPart(ref op) - | BoolToInt(ref op) => write!( + | BoolToInt(ref op) | Fsqrt(ref op) | Fneg(ref op) | Fabs(ref op) + | FloatToInt(ref op) | IntToFloat(ref op) | FloatConv(ref op) | RoundToInt(ref op) + | Floor(ref op) | Ceil(ref op) | Ftrunc(ref op) => write!( f, "{:?}({}, {:?})", op.op.operation, diff --git a/rust/src/llil/instruction.rs b/rust/src/llil/instruction.rs index e5ae508b..c9817dbf 100644 --- a/rust/src/llil/instruction.rs +++ b/rust/src/llil/instruction.rs @@ -112,6 +112,7 @@ where InstrInfo::Call(Operation::new(self.function, op)) } LLIL_SYSCALL => InstrInfo::Syscall(Operation::new(self.function, op)), + LLIL_INTRINSIC => InstrInfo::Intrinsic(Operation::new(self.function, op)), _ => { common_info(self.function, op).unwrap_or({ // Hopefully this is a bare value. If it isn't (expression @@ -149,6 +150,12 @@ where } Push(ref op) => visit!(fb, &op.operand()), Call(ref op) => visit!(fb, &op.target()), + Intrinsic(ref _op) => { + // TODO: Use this when we support expression lists + // for expr in op.source_exprs() { + // visit!(fb, expr); + // } + } _ => visit!(common_visit, &info, fb), } @@ -181,6 +188,7 @@ where Goto(Operation<'func, A, M, F, operation::Goto>), Syscall(Operation<'func, A, M, F, operation::Syscall>), + Intrinsic(Operation<'func, A, M, F, operation::Intrinsic>), Bp(Operation<'func, A, M, F, operation::NoArgs>), Trap(Operation<'func, A, M, F, operation::Trap>), Undef(Operation<'func, A, M, F, operation::NoArgs>), diff --git a/rust/src/llil/lifting.rs b/rust/src/llil/lifting.rs index eb7b36fd..bd023933 100644 --- a/rust/src/llil/lifting.rs +++ b/rust/src/llil/lifting.rs @@ -17,7 +17,9 @@ use std::mem; use crate::architecture::Architecture; use crate::architecture::Register as ArchReg; -use crate::architecture::{Flag, FlagClass, FlagCondition, FlagGroup, FlagRole, FlagWrite}; +use crate::architecture::{ + Flag, FlagClass, FlagCondition, FlagGroup, FlagRole, FlagWrite, Intrinsic, +}; use super::*; @@ -866,6 +868,9 @@ impl<A> Function<A, Mutable, NonSSA<LiftedNonSSA>> where A: Architecture, { + pub const NO_INPUTS: [ExpressionBuilder<'static, A, ValueExpr>; 0] = []; + pub const NO_OUTPUTS: [Register<A::Register>; 0] = []; + pub fn expression<'a, E: Liftable<'a, A>>( &'a self, expr: E, @@ -1215,6 +1220,73 @@ where } } + pub fn intrinsic<'a, O, OL, I, P, PL>( + &'a self, + outputs: OL, + intrinsic: I, + inputs: PL, + ) -> ExpressionBuilder<'a, A, VoidExpr> + where + O: Into<Register<A::Register>>, + OL: IntoIterator<Item = O>, + I: Into<A::Intrinsic>, + P: Liftable<'a, A, Result = ValueExpr>, + PL: IntoIterator<Item = P>, + { + use binaryninjacore_sys::BNLowLevelILOperation::{LLIL_CALL_PARAM, LLIL_INTRINSIC}; + use binaryninjacore_sys::{BNLowLevelILAddExpr, BNLowLevelILAddOperandList}; + + let mut outputs: Vec<u64> = outputs + .into_iter() + .map(|output| { + // TODO verify valid id + let output = match output.into() { + Register::ArchReg(r) => r.id(), + Register::Temp(r) => 0x8000_0000 | r, + }; + output as u64 + }) + .collect(); + let output_expr_idx = + unsafe { BNLowLevelILAddOperandList(self.handle, outputs.as_mut_ptr(), outputs.len()) }; + + let intrinsic: A::Intrinsic = intrinsic.into(); + + let mut inputs: Vec<u64> = inputs + .into_iter() + .map(|input| { + let input = P::lift(self, input); + input.expr_idx as u64 + }) + .collect(); + let input_list_expr_idx = + unsafe { BNLowLevelILAddOperandList(self.handle, inputs.as_mut_ptr(), inputs.len()) }; + let input_expr_idx = unsafe { + BNLowLevelILAddExpr( + self.handle, + LLIL_CALL_PARAM, + 0, + 0, + inputs.len() as u64, + input_list_expr_idx as u64, + 0, + 0, + ) + }; + + ExpressionBuilder { + function: self, + op: LLIL_INTRINSIC, + size: 0, + flags: 0, + op1: outputs.len() as u64, + op2: output_expr_idx as u64, + op3: intrinsic.id() as u64, + op4: input_expr_idx as u64, + _ty: PhantomData, + } + } + sized_unary_op_lifter!(push, LLIL_PUSH, VoidExpr); sized_no_arg_lifter!(pop, LLIL_POP, ValueExpr); @@ -1276,6 +1348,29 @@ where // TODO no flags size_changing_unary_op_lifter!(bool_to_int, LLIL_BOOL_TO_INT, ValueExpr); + binary_op_lifter!(fadd, LLIL_FADD); + binary_op_lifter!(fsub, LLIL_FSUB); + binary_op_lifter!(fmul, LLIL_FMUL); + binary_op_lifter!(fdiv, LLIL_FDIV); + sized_unary_op_lifter!(fsqrt, LLIL_FSQRT, ValueExpr); + sized_unary_op_lifter!(fneg, LLIL_FNEG, ValueExpr); + sized_unary_op_lifter!(fabs, LLIL_FABS, ValueExpr); + sized_unary_op_lifter!(float_to_int, LLIL_FLOAT_TO_INT, ValueExpr); + sized_unary_op_lifter!(int_to_float, LLIL_INT_TO_FLOAT, ValueExpr); + sized_unary_op_lifter!(float_conv, LLIL_FLOAT_CONV, ValueExpr); + sized_unary_op_lifter!(round_to_int, LLIL_ROUND_TO_INT, ValueExpr); + sized_unary_op_lifter!(floor, LLIL_FLOOR, ValueExpr); + sized_unary_op_lifter!(ceil, LLIL_CEIL, ValueExpr); + sized_unary_op_lifter!(ftrunc, LLIL_FTRUNC, ValueExpr); + binary_op_lifter!(fcmp_e, LLIL_FCMP_E); + binary_op_lifter!(fcmp_ne, LLIL_FCMP_NE); + binary_op_lifter!(fcmp_lt, LLIL_FCMP_LT); + binary_op_lifter!(fcmp_le, LLIL_FCMP_LE); + binary_op_lifter!(fcmp_ge, LLIL_FCMP_GE); + binary_op_lifter!(fcmp_gt, LLIL_FCMP_GT); + binary_op_lifter!(fcmp_o, LLIL_FCMP_O); + binary_op_lifter!(fcmp_uo, LLIL_FCMP_UO); + pub fn current_address(&self) -> u64 { use binaryninjacore_sys::BNLowLevelILGetCurrentAddress; unsafe { BNLowLevelILGetCurrentAddress(self.handle) } diff --git a/rust/src/llil/operation.rs b/rust/src/llil/operation.rs index b998cce3..4daa7cb2 100644 --- a/rust/src/llil/operation.rs +++ b/rust/src/llil/operation.rs @@ -85,6 +85,22 @@ where // LLIL_SYSCALL, LLIL_SYSCALL_SSA pub struct Syscall; +// LLIL_INTRINSIC, LLIL_INTRINSIC_SSA +pub struct Intrinsic; + +impl<'func, A, M, V> Operation<'func, A, M, NonSSA<V>, Intrinsic> + where + A: 'func + Architecture, + M: FunctionMutability, + V: NonSSAVariant, +{ + // TODO: Support register and expression lists + pub fn intrinsic(&self) -> Option<A::Intrinsic> { + let raw_id = self.op.operands[2] as u32; + self.function.arch().intrinsic_from_id(raw_id) + } +} + // LLIL_SET_REG, LLIL_SET_REG_SSA, LLIL_SET_REG_PARTIAL_SSA pub struct SetReg; @@ -617,6 +633,7 @@ pub trait OperationArguments: 'static {} impl OperationArguments for NoArgs {} impl OperationArguments for Pop {} impl OperationArguments for Syscall {} +impl OperationArguments for Intrinsic {} impl OperationArguments for SetReg {} impl OperationArguments for SetRegSplit {} impl OperationArguments for SetFlag {} |
