Skip to content

Commit d91834a

Browse files
committed
Add missing instruction types to MLIL Rust API
1 parent 910fb8f commit d91834a

File tree

3 files changed

+164
-9
lines changed

3 files changed

+164
-9
lines changed

rust/src/medium_level_il/instruction.rs

Lines changed: 54 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@ use crate::architecture::{CoreIntrinsic, FlagId, IntrinsicId, RegisterId};
55
use crate::basic_block::BasicBlock;
66
use crate::confidence::Conf;
77
use crate::disassembly::InstructionTextToken;
8-
use crate::operand_iter::OperandIter;
98
use crate::rc::{Array, CoreArrayProvider, CoreArrayProviderInner, Ref};
109
use crate::types::Type;
1110
use crate::variable::{ConstantData, PossibleValueSet, RegisterValue, SSAVariable, Variable};
1211
use crate::{DataFlowQueryOption, ILBranchDependence};
1312
use binaryninjacore_sys::*;
13+
use std::collections::BTreeMap;
1414
use std::fmt;
1515
use std::fmt::{Debug, Display, Formatter};
1616

@@ -411,6 +411,24 @@ impl MediumLevelILInstruction {
411411
num_params: op.operands[3] as usize,
412412
first_param: op.operands[4] as usize,
413413
}),
414+
MLIL_CALL_OUTPUT => Op::CallOutput(CallOutput {
415+
first_output: op.operands[0] as usize,
416+
num_outputs: op.operands[1] as usize,
417+
}),
418+
MLIL_CALL_PARAM => Op::CallParam(CallParam {
419+
first_param: op.operands[0] as usize,
420+
num_params: op.operands[1] as usize,
421+
}),
422+
MLIL_CALL_OUTPUT_SSA => Op::CallOutputSsa(CallOutputSsa {
423+
dest_memory: op.operands[0],
424+
num_outputs: op.operands[1] as usize,
425+
first_output: op.operands[2] as usize,
426+
}),
427+
MLIL_CALL_PARAM_SSA => Op::CallParamSsa(CallParamSsa {
428+
src_memory: op.operands[0],
429+
num_params: op.operands[1] as usize,
430+
first_param: op.operands[2] as usize,
431+
}),
414432
MLIL_TAILCALL => Op::Tailcall(Call {
415433
num_outputs: op.operands[0] as usize,
416434
first_output: op.operands[1] as usize,
@@ -438,6 +456,20 @@ impl MediumLevelILInstruction {
438456
num_params: op.operands[3] as usize,
439457
first_param: op.operands[4] as usize,
440458
}),
459+
MLIL_MEMORY_INTRINSIC_SSA => Op::MemoryIntrinsicSsa(MemoryIntrinsicSsa {
460+
output: op.operands[0] as usize,
461+
intrinsic: op.operands[1] as u32,
462+
num_params: op.operands[2] as usize,
463+
first_param: op.operands[3] as usize,
464+
src_memory: op.operands[4],
465+
}),
466+
MLIL_MEMORY_INTRINSIC_OUTPUT_SSA => {
467+
Op::MemoryIntrinsicOutputSsa(MemoryIntrinsicOutputSsa {
468+
dest_memory: op.operands[0],
469+
first_output: op.operands[1] as usize,
470+
num_outputs: op.operands[2] as usize,
471+
})
472+
}
441473
MLIL_CALL_SSA => Op::CallSsa(CallSsa {
442474
output: op.operands[0] as usize,
443475
dest: op.operands[1] as usize,
@@ -602,14 +634,6 @@ impl MediumLevelILInstruction {
602634
MLIL_TRAP => Op::Trap(Trap {
603635
vector: op.operands[0],
604636
}),
605-
// translated directly into a list for Expression or Variables
606-
// TODO MLIL_MEMORY_INTRINSIC_SSA needs to be handled properly
607-
MLIL_CALL_OUTPUT
608-
| MLIL_CALL_PARAM
609-
| MLIL_CALL_PARAM_SSA
610-
| MLIL_CALL_OUTPUT_SSA
611-
| MLIL_MEMORY_INTRINSIC_OUTPUT_SSA
612-
| MLIL_MEMORY_INTRINSIC_SSA => Op::NotYetImplemented,
613637
};
614638

615639
Self {
@@ -847,6 +871,21 @@ impl MediumLevelILInstruction {
847871
Rrc(op) => Lifted::Rrc(self.lift_binary_op_carry(op)),
848872

849873
Call(op) => Lifted::Call(self.lift_call(op)),
874+
CallOutput(_op) => Lifted::CallOutput(LiftedCallOutput {
875+
output: self.get_var_list(0),
876+
}),
877+
CallParam(_op) => Lifted::CallParam(LiftedCallParam {
878+
params: self.get_expr_list(0).iter().map(|i| i.lift()).collect(),
879+
}),
880+
CallOutputSsa(op) => Lifted::CallOutputSsa(LiftedCallOutputSsa {
881+
dest_memory: op.dest_memory,
882+
output: self.get_ssa_var_list(1),
883+
}),
884+
CallParamSsa(op) => Lifted::CallParamSsa(LiftedCallParamSsa {
885+
src_memory: op.src_memory,
886+
params: self.get_expr_list(1).iter().map(|i| i.lift()).collect(),
887+
}),
888+
850889
Tailcall(op) => Lifted::Tailcall(self.lift_call(op)),
851890

852891
Intrinsic(op) => Lifted::Intrinsic(LiftedIntrinsic {
@@ -1672,10 +1711,16 @@ pub enum MediumLevelILInstructionKind {
16721711
Rlc(BinaryOpCarry),
16731712
Rrc(BinaryOpCarry),
16741713
Call(Call),
1714+
CallOutput(CallOutput),
1715+
CallParam(CallParam),
1716+
CallOutputSsa(CallOutputSsa),
1717+
CallParamSsa(CallParamSsa),
16751718
Tailcall(Call),
16761719
Syscall(Syscall),
16771720
Intrinsic(Intrinsic),
16781721
IntrinsicSsa(IntrinsicSsa),
1722+
MemoryIntrinsicSsa(MemoryIntrinsicSsa),
1723+
MemoryIntrinsicOutputSsa(MemoryIntrinsicOutputSsa),
16791724
CallSsa(CallSsa),
16801725
TailcallSsa(CallSsa),
16811726
CallUntypedSsa(CallUntypedSsa),

rust/src/medium_level_il/lift.rs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,10 +129,16 @@ pub enum MediumLevelILLiftedInstructionKind {
129129
Rlc(LiftedBinaryOpCarry),
130130
Rrc(LiftedBinaryOpCarry),
131131
Call(LiftedCall),
132+
CallOutput(LiftedCallOutput),
133+
CallParam(LiftedCallParam),
134+
CallOutputSsa(LiftedCallOutputSsa),
135+
CallParamSsa(LiftedCallParamSsa),
132136
Tailcall(LiftedCall),
133137
Intrinsic(LiftedIntrinsic),
134138
Syscall(LiftedSyscallCall),
135139
IntrinsicSsa(LiftedIntrinsicSsa),
140+
MemoryIntrinsicSsa(LiftedMemoryIntrinsicSsa),
141+
MemoryIntrinsicOutputSsa(LiftedMemoryIntrinsicOutputSsa),
136142
CallSsa(LiftedCallSsa),
137143
TailcallSsa(LiftedCallSsa),
138144
CallUntypedSsa(LiftedCallUntypedSsa),
@@ -269,10 +275,16 @@ impl MediumLevelILLiftedInstruction {
269275
Rlc(_) => "Rlc",
270276
Rrc(_) => "Rrc",
271277
Call(_) => "Call",
278+
CallOutput(_) => "CallOutput",
279+
CallParam(_) => "CallParam",
280+
CallOutputSsa(_) => "CallOutputSsa",
281+
CallParamSsa(_) => "CallParamSsa",
272282
Tailcall(_) => "Tailcall",
273283
Syscall(_) => "Syscall",
274284
Intrinsic(_) => "Intrinsic",
275285
IntrinsicSsa(_) => "IntrinsicSsa",
286+
MemoryIntrinsicSsa(_) => "MemoryIntrinsicSsa",
287+
MemoryIntrinsicOutputSsa(_) => "MemoryIntrinsicOutputSsa",
276288
CallSsa(_) => "CallSsa",
277289
TailcallSsa(_) => "TailcallSsa",
278290
CallUntypedSsa(_) => "CallUntypedSsa",
@@ -441,6 +453,16 @@ impl MediumLevelILLiftedInstruction {
441453
("dest", Operand::Expr(*op.dest.clone())),
442454
("params", Operand::ExprList(op.params.clone())),
443455
],
456+
CallOutput(op) => vec![("output", Operand::VarList(op.output.clone()))],
457+
CallParam(op) => vec![("params", Operand::ExprList(op.params.clone()))],
458+
CallOutputSsa(op) => vec![
459+
("output", Operand::VarSsaList(op.output.clone())),
460+
("dest_memory", Operand::Int(op.dest_memory)),
461+
],
462+
CallParamSsa(op) => vec![
463+
("params", Operand::ExprList(op.params.clone())),
464+
("src_memory", Operand::Int(op.src_memory)),
465+
],
444466
Syscall(op) => vec![
445467
("output", Operand::VarList(op.output.clone())),
446468
("params", Operand::ExprList(op.params.clone())),
@@ -455,6 +477,16 @@ impl MediumLevelILLiftedInstruction {
455477
("intrinsic", Operand::Intrinsic(op.intrinsic)),
456478
("params", Operand::ExprList(op.params.clone())),
457479
],
480+
MemoryIntrinsicSsa(op) => vec![
481+
("output", Operand::Expr(*op.output.clone())),
482+
("intrinsic", Operand::Intrinsic(op.intrinsic)),
483+
("params", Operand::ExprList(op.params.clone())),
484+
("src_memory", Operand::Int(op.src_memory)),
485+
],
486+
MemoryIntrinsicOutputSsa(op) => vec![
487+
("dest_memory", Operand::Int(op.dest_memory)),
488+
("output", Operand::VarSsaList(op.output.clone())),
489+
],
458490
CallSsa(op) | TailcallSsa(op) => vec![
459491
("output", Operand::VarSsaList(op.output.clone())),
460492
("dest", Operand::Expr(*op.dest.clone())),

rust/src/medium_level_il/operation.rs

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,54 @@ pub struct LiftedCall {
329329
pub params: Vec<MediumLevelILLiftedInstruction>,
330330
}
331331

332+
// CALL_OUTPUT
333+
#[derive(Debug, Copy, Clone)]
334+
pub struct CallOutput {
335+
pub first_output: usize,
336+
pub num_outputs: usize,
337+
}
338+
#[derive(Clone, Debug, PartialEq)]
339+
pub struct LiftedCallOutput {
340+
pub output: Vec<Variable>,
341+
}
342+
343+
// CALL_PARAM_SSA
344+
#[derive(Debug, Copy, Clone)]
345+
pub struct CallParam {
346+
pub first_param: usize,
347+
pub num_params: usize,
348+
}
349+
#[derive(Clone, Debug, PartialEq)]
350+
pub struct LiftedCallParam {
351+
pub params: Vec<MediumLevelILLiftedInstruction>,
352+
}
353+
354+
// CALL_OUTPUT_SSA
355+
#[derive(Debug, Copy, Clone)]
356+
pub struct CallOutputSsa {
357+
pub dest_memory: u64,
358+
pub first_output: usize,
359+
pub num_outputs: usize,
360+
}
361+
#[derive(Clone, Debug, PartialEq)]
362+
pub struct LiftedCallOutputSsa {
363+
pub dest_memory: u64,
364+
pub output: Vec<SSAVariable>,
365+
}
366+
367+
// CALL_PARAM_SSA
368+
#[derive(Debug, Copy, Clone)]
369+
pub struct CallParamSsa {
370+
pub src_memory: u64,
371+
pub first_param: usize,
372+
pub num_params: usize,
373+
}
374+
#[derive(Clone, Debug, PartialEq)]
375+
pub struct LiftedCallParamSsa {
376+
pub src_memory: u64,
377+
pub params: Vec<MediumLevelILLiftedInstruction>,
378+
}
379+
332380
// SYSCALL
333381
#[derive(Debug, Copy, Clone)]
334382
pub struct Syscall {
@@ -375,6 +423,36 @@ pub struct LiftedIntrinsicSsa {
375423
pub params: Vec<MediumLevelILLiftedInstruction>,
376424
}
377425

426+
// MEMORY_INTRINSIC_SSA
427+
#[derive(Debug, Copy, Clone)]
428+
pub struct MemoryIntrinsicSsa {
429+
pub output: usize,
430+
pub intrinsic: u32,
431+
pub first_param: usize,
432+
pub num_params: usize,
433+
pub src_memory: u64,
434+
}
435+
#[derive(Clone, Debug, PartialEq)]
436+
pub struct LiftedMemoryIntrinsicSsa {
437+
pub output: Box<MediumLevelILLiftedInstruction>,
438+
pub intrinsic: CoreIntrinsic,
439+
pub params: Vec<MediumLevelILLiftedInstruction>,
440+
pub src_memory: u64,
441+
}
442+
443+
// MEMORY_INTRINSIC_OUPUT_SSA
444+
#[derive(Debug, Copy, Clone)]
445+
pub struct MemoryIntrinsicOutputSsa {
446+
pub dest_memory: u64,
447+
pub first_output: usize,
448+
pub num_outputs: usize,
449+
}
450+
#[derive(Clone, Debug, PartialEq)]
451+
pub struct LiftedMemoryIntrinsicOutputSsa {
452+
pub dest_memory: u64,
453+
pub output: Vec<SSAVariable>,
454+
}
455+
378456
// CALL_SSA, TAILCALL_SSA
379457
#[derive(Debug, Copy, Clone)]
380458
pub struct CallSsa {

0 commit comments

Comments
 (0)