Skip to content

Commit 4d72e4c

Browse files
committed
[Rust] WIP: LLIL expression builder related changes
1 parent 931e338 commit 4d72e4c

File tree

7 files changed

+33
-22
lines changed

7 files changed

+33
-22
lines changed

arch/riscv/src/lib.rs

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@ use binaryninja::low_level_il::lifting::{
4444
};
4545
use binaryninja::low_level_il::{
4646
expression::ExpressionHandler, instruction::InstructionHandler, LowLevelILMutableExpression,
47-
LowLevelILMutableFunction, LowLevelILRegisterKind, LowLevelILRegularFunction,
47+
LowLevelILMutableFunction, LowLevelILOperandIndex, LowLevelILRegisterKind,
48+
LowLevelILRegularFunction,
4849
};
4950
use riscv_dis::{
5051
FloatReg, FloatRegType, Instr, IntRegType, Op, RegFile, Register as RiscVRegister,
@@ -1117,12 +1118,12 @@ impl<D: RiscVDisassembler> Architecture for RiscVArch<D> {
11171118

11181119
let src_expr = il.add(max_width, rs1, l.imm());
11191120
let load_expr = il.load(size, src_expr)
1120-
.with_source_operand(1);
1121+
.with_source_operand(LowLevelILOperandIndex(1));
11211122

11221123
match (size < max_width, l.zx()) {
11231124
(false, _) => load_expr,
1124-
(true, true) => il.zx(max_width, load_expr).build(),
1125-
(true, false) => il.sx(max_width, load_expr).build(),
1125+
(true, true) => il.zx(max_width, load_expr),
1126+
(true, false) => il.sx(max_width, load_expr),
11261127
}
11271128
}),
11281129
Op::Store(s) => {
@@ -1136,7 +1137,9 @@ impl<D: RiscVDisassembler> Architecture for RiscVArch<D> {
11361137
src = il.low_part(size, src).build();
11371138
}
11381139

1139-
il.store(size, dest, src).with_source_operand(1).append();
1140+
il.store(size, dest, src)
1141+
.with_source_operand(LowLevelILOperandIndex(1))
1142+
.append();
11401143
}
11411144

11421145
Op::AddI(i) => simple_i!(i, |rs1, imm| il.add(max_width, rs1, imm)),
@@ -1459,11 +1462,11 @@ impl<D: RiscVDisassembler> Architecture for RiscVArch<D> {
14591462
Op::Lr(a) => simple_op!(a, no_discard {
14601463
let size = a.width();
14611464
let load_expr = il.load(size, Register::from(a.rs1()))
1462-
.with_source_operand(1);
1465+
.with_source_operand(LowLevelILOperandIndex(1));
14631466

14641467
match size == max_width {
14651468
true => load_expr,
1466-
false => il.sx(max_width, load_expr).build(),
1469+
false => il.sx(max_width, load_expr),
14671470
}
14681471
}),
14691472
Op::Sc(a) => {
@@ -1497,7 +1500,7 @@ impl<D: RiscVDisassembler> Architecture for RiscVArch<D> {
14971500

14981501
il.mark_label(&mut t);
14991502
il.store(size, Register::from(a.rs1()), Register::from(a.rs2()))
1500-
.with_source_operand(2)
1503+
.with_source_operand(LowLevelILOperandIndex(2))
15011504
.append();
15021505

15031506
if new_false {
@@ -1539,10 +1542,12 @@ impl<D: RiscVDisassembler> Architecture for RiscVArch<D> {
15391542
let reg_with_address = alloc_reg(rs1);
15401543
let reg_with_val = alloc_reg(rs2);
15411544

1542-
let mut load_expr = il.load(size, Register::from(rs1)).with_source_operand(2);
1545+
let mut load_expr = il
1546+
.load(size, Register::from(rs1))
1547+
.with_source_operand(LowLevelILOperandIndex(2));
15431548

15441549
if size < max_width {
1545-
load_expr = il.sx(max_width, load_expr).build();
1550+
load_expr = il.sx(max_width, load_expr);
15461551
}
15471552

15481553
il.set_reg(max_width, dest_reg, load_expr).append();
@@ -1571,7 +1576,7 @@ impl<D: RiscVDisassembler> Architecture for RiscVArch<D> {
15711576

15721577
let load_expr = il
15731578
.load(m.width(), il.add(max_width, rs1, m.imm()))
1574-
.with_source_operand(1);
1579+
.with_source_operand(LowLevelILOperandIndex(1));
15751580

15761581
il.set_reg(m.width(), rd, load_expr).append();
15771582
}
@@ -1582,7 +1587,7 @@ impl<D: RiscVDisassembler> Architecture for RiscVArch<D> {
15821587
let dest_expr = il.add(max_width, rs1, m.imm());
15831588

15841589
il.store(m.width(), dest_expr, il.reg(m.width(), rs2))
1585-
.with_source_operand(1)
1590+
.with_source_operand(LowLevelILOperandIndex(1))
15861591
.append();
15871592
}
15881593
Op::Fmadd(f) | Op::Fmsub(f) | Op::Fnmadd(f) | Op::Fnmsub(f) => {

plugins/warp/src/plugin/workflow.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,9 +110,7 @@ pub fn run_matcher(view: &BinaryView) {
110110
// generated, we should alert them and ask if they would like to reanalyze.
111111
// NOTE: We only alert if we actually have the GUID activity enabled.
112112
if let Some(sample_function) = view.functions().iter().next() {
113-
let function_workflow = sample_function
114-
.workflow()
115-
.expect("Function has no workflow");
113+
let function_workflow = sample_function.workflow();
116114
if function_workflow.contains(GUID_ACTIVITY_NAME) {
117115
log::error!("No function guids in database, please reanalyze the database.");
118116
} else {

rust/examples/workflow.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ fn example_activity(analysis_context: &AnalysisContext) {
2121
"Activity `{}` called in function {} with workflow {:?}!",
2222
RUST_ACTIVITY_NAME,
2323
func.start(),
24-
func.workflow().map(|wf| wf.name())
24+
func.workflow().name()
2525
);
2626
// If we have llil available, replace that as well.
2727
if let Some(llil) = unsafe { analysis_context.llil_function() } {

rust/src/architecture.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,13 @@ macro_rules! new_id_type {
9292
}
9393
}
9494

95+
// To satisfy type deduction.
96+
impl From<i32> for $name {
97+
fn from(value: i32) -> Self {
98+
Self(value as $inner_type)
99+
}
100+
}
101+
95102
impl std::fmt::Display for $name {
96103
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
97104
write!(f, "{}", self.0)

rust/src/binary_view.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ pub trait BinaryViewExt: BinaryViewBase {
242242
}
243243

244244
/// Search the view using the query options.
245-
///
245+
///
246246
/// In the `on_match` callback return `false` to stop searching.
247247
fn search_with_progress<P: ProgressCallback, C: FnMut(u64, &DataBuffer) -> bool>(
248248
&self,

rust/src/function.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -371,10 +371,11 @@ impl Function {
371371
}
372372
}
373373

374-
pub fn workflow(&self) -> Option<Ref<Workflow>> {
374+
pub fn workflow(&self) -> Ref<Workflow> {
375375
unsafe {
376-
let workflow = NonNull::new(BNGetWorkflowForFunction(self.handle))?;
377-
Some(Workflow::ref_from_raw(workflow))
376+
let workflow = NonNull::new(BNGetWorkflowForFunction(self.handle))
377+
.expect("Function has no workflow");
378+
Workflow::ref_from_raw(workflow)
378379
}
379380
}
380381

rust/src/progress.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ use std::ffi::c_void;
22

33
pub trait ProgressCallback: Sized {
44
type SplitProgressType: SplitProgressBuilder;
5-
5+
66
/// Caller function will call this to report progress.
7-
///
7+
///
88
/// Return `false` to tell the caller to stop.
99
fn progress(&mut self, progress: usize, total: usize) -> bool;
1010

0 commit comments

Comments
 (0)