Skip to content

Commit 809f97f

Browse files
committed
Rename MediumLevelILInstruction to Instruction in Rust API
1 parent 20ef71e commit 809f97f

File tree

6 files changed

+45
-54
lines changed

6 files changed

+45
-54
lines changed

rust/src/function.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2128,7 +2128,7 @@ impl Function {
21282128

21292129
/// Splits a variable at the definition site. The given `var` must be the
21302130
/// variable unique to the definition and should be obtained by using
2131-
/// [crate::medium_level_il::MediumLevelILInstruction::get_split_var_for_definition] at the definition site.
2131+
/// [crate::medium_level_il::Instruction::get_split_var_for_definition] at the definition site.
21322132
///
21332133
/// This function is not meant to split variables that have been previously merged. Use
21342134
/// [Function::unmerge_variables] to split previously merged variables.
@@ -2151,7 +2151,7 @@ impl Function {
21512151

21522152
/// Undoes variable splitting performed with [Function::split_variable]. The given `var`
21532153
/// must be the variable unique to the definition and should be obtained by using
2154-
/// [crate::medium_level_il::MediumLevelILInstruction::get_split_var_for_definition] at the definition site.
2154+
/// [crate::medium_level_il::Instruction::get_split_var_for_definition] at the definition site.
21552155
///
21562156
/// * `var` - variable to unsplit
21572157
pub fn unsplit_variable(&self, var: &Variable) {

rust/src/medium_level_il/block.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,18 @@ use crate::basic_block::{BasicBlock, BlockContext};
22
use crate::rc::Ref;
33
use std::ops::Range;
44

5-
use super::{MediumLevelILFunction, MediumLevelILInstruction, MediumLevelInstructionIndex};
5+
use super::{Instruction, MediumLevelILFunction, MediumLevelInstructionIndex};
66

77
pub struct MediumLevelILBlock {
88
pub(crate) function: Ref<MediumLevelILFunction>,
99
}
1010

1111
impl BlockContext for MediumLevelILBlock {
12-
type Instruction = MediumLevelILInstruction;
12+
type Instruction = Instruction;
1313
type InstructionIndex = MediumLevelInstructionIndex;
1414
type Iter = MediumLevelILBlockIter;
1515

16-
fn start(&self, block: &BasicBlock<Self>) -> MediumLevelILInstruction {
16+
fn start(&self, block: &BasicBlock<Self>) -> Instruction {
1717
// TODO: instruction_from_index says that it is not mapped and will do the call
1818
// TODO: What if this IS already MAPPED!?!?!?
1919
self.function
@@ -51,7 +51,7 @@ pub struct MediumLevelILBlockIter {
5151
}
5252

5353
impl Iterator for MediumLevelILBlockIter {
54-
type Item = MediumLevelILInstruction;
54+
type Item = Instruction;
5555

5656
fn next(&mut self) -> Option<Self::Item> {
5757
self.range

rust/src/medium_level_il/function.rs

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::ffi::c_char;
33
use std::fmt::{Debug, Formatter};
44
use std::hash::{Hash, Hasher};
55

6-
use super::{MediumLevelILBlock, MediumLevelILInstruction, MediumLevelInstructionIndex};
6+
use super::{Instruction, MediumLevelILBlock, MediumLevelInstructionIndex};
77
use crate::architecture::CoreArchitecture;
88
use crate::basic_block::BasicBlock;
99
use crate::confidence::Conf;
@@ -33,8 +33,8 @@ impl MediumLevelILFunction {
3333
Ref::new(Self::from_raw(handle))
3434
}
3535

36-
pub fn instruction_at<L: Into<Location>>(&self, loc: L) -> Option<MediumLevelILInstruction> {
37-
Some(MediumLevelILInstruction::new(
36+
pub fn instruction_at<L: Into<Location>>(&self, loc: L) -> Option<Instruction> {
37+
Some(Instruction::new(
3838
self.to_owned(),
3939
self.instruction_index_at(loc)?,
4040
))
@@ -61,25 +61,22 @@ impl MediumLevelILFunction {
6161
pub fn instruction_from_index(
6262
&self,
6363
index: MediumLevelInstructionIndex,
64-
) -> Option<MediumLevelILInstruction> {
64+
) -> Option<Instruction> {
6565
if index.0 >= self.instruction_count() {
6666
None
6767
} else {
68-
Some(MediumLevelILInstruction::new(self.to_owned(), index))
68+
Some(Instruction::new(self.to_owned(), index))
6969
}
7070
}
7171

7272
pub fn instruction_from_expr_index(
7373
&self,
7474
expr_index: MediumLevelInstructionIndex,
75-
) -> Option<MediumLevelILInstruction> {
75+
) -> Option<Instruction> {
7676
if expr_index.0 >= self.expression_count() {
7777
None
7878
} else {
79-
Some(MediumLevelILInstruction::new_expr(
80-
self.to_owned(),
81-
expr_index,
82-
))
79+
Some(Instruction::new_expr(self.to_owned(), expr_index))
8380
}
8481
}
8582

@@ -113,7 +110,7 @@ impl MediumLevelILFunction {
113110
unsafe { Array::new(blocks, count, context) }
114111
}
115112

116-
pub fn var_definitions(&self, var: &Variable) -> Array<MediumLevelILInstruction> {
113+
pub fn var_definitions(&self, var: &Variable) -> Array<Instruction> {
117114
let mut count = 0;
118115
let raw_var = BNVariable::from(var);
119116
let raw_instr_idxs =
@@ -407,7 +404,7 @@ impl MediumLevelILFunction {
407404

408405
/// Returns the [`BasicBlock`] at the given instruction `index`.
409406
///
410-
/// You can also retrieve this using [`MediumLevelILInstruction::basic_block`].
407+
/// You can also retrieve this using [`Instruction::basic_block`].
411408
pub fn basic_block_containing_index(
412409
&self,
413410
index: MediumLevelInstructionIndex,
@@ -466,10 +463,7 @@ impl MediumLevelILFunction {
466463
///
467464
/// Since SSA variables can only be defined once, this will return the single instruction where that occurs.
468465
/// For SSA variable version 0s, which don't have definitions, this will return `None` instead.
469-
pub fn ssa_variable_definition(
470-
&self,
471-
ssa_variable: &SSAVariable,
472-
) -> Option<MediumLevelILInstruction> {
466+
pub fn ssa_variable_definition(&self, ssa_variable: &SSAVariable) -> Option<Instruction> {
473467
let raw_var = BNVariable::from(ssa_variable.variable);
474468
let result = unsafe {
475469
BNGetMediumLevelILSSAVarDefinition(self.handle, &raw_var, ssa_variable.version)
@@ -478,14 +472,14 @@ impl MediumLevelILFunction {
478472
self.instruction_from_index(MediumLevelInstructionIndex(result))
479473
}
480474

481-
pub fn ssa_memory_definition(&self, version: usize) -> Option<MediumLevelILInstruction> {
475+
pub fn ssa_memory_definition(&self, version: usize) -> Option<Instruction> {
482476
let result = unsafe { BNGetMediumLevelILSSAMemoryDefinition(self.handle, version) };
483477
// TODO: Does this return the expression or instruction index? Also we dont diff and this prob doesnt work.
484478
self.instruction_from_index(MediumLevelInstructionIndex(result))
485479
}
486480

487481
/// Gets all the instructions that use the given SSA variable.
488-
pub fn ssa_variable_uses(&self, ssa_variable: &SSAVariable) -> Array<MediumLevelILInstruction> {
482+
pub fn ssa_variable_uses(&self, ssa_variable: &SSAVariable) -> Array<Instruction> {
489483
let mut count = 0;
490484
let raw_var = BNVariable::from(ssa_variable.variable);
491485
let uses = unsafe {
@@ -495,7 +489,7 @@ impl MediumLevelILFunction {
495489
unsafe { Array::new(uses, count, self.to_owned()) }
496490
}
497491

498-
pub fn ssa_memory_uses(&self, version: usize) -> Array<MediumLevelILInstruction> {
492+
pub fn ssa_memory_uses(&self, version: usize) -> Array<Instruction> {
499493
let mut count = 0;
500494
let uses = unsafe { BNGetMediumLevelILSSAMemoryUses(self.handle, version, &mut count) };
501495
assert!(!uses.is_null());
@@ -508,15 +502,15 @@ impl MediumLevelILFunction {
508502
unsafe { BNIsMediumLevelILSSAVarLive(self.handle, &raw_var, ssa_variable.version) }
509503
}
510504

511-
pub fn variable_definitions(&self, variable: &Variable) -> Array<MediumLevelILInstruction> {
505+
pub fn variable_definitions(&self, variable: &Variable) -> Array<Instruction> {
512506
let mut count = 0;
513507
let raw_var = BNVariable::from(variable);
514508
let defs =
515509
unsafe { BNGetMediumLevelILVariableDefinitions(self.handle, &raw_var, &mut count) };
516510
unsafe { Array::new(defs, count, self.to_owned()) }
517511
}
518512

519-
pub fn variable_uses(&self, variable: &Variable) -> Array<MediumLevelILInstruction> {
513+
pub fn variable_uses(&self, variable: &Variable) -> Array<Instruction> {
520514
let mut count = 0;
521515
let raw_var = BNVariable::from(variable);
522516
let uses = unsafe { BNGetMediumLevelILVariableUses(self.handle, &raw_var, &mut count) };
@@ -534,7 +528,7 @@ impl MediumLevelILFunction {
534528
&self,
535529
variable: &Variable,
536530
include_last_user: bool,
537-
) -> Array<MediumLevelILInstruction> {
531+
) -> Array<Instruction> {
538532
let mut count = 0;
539533
let raw_var = BNVariable::from(variable);
540534
let uses = unsafe {

rust/src/medium_level_il/instruction.rs

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -42,16 +42,16 @@ impl Display for MediumLevelInstructionIndex {
4242
}
4343

4444
#[derive(Clone)]
45-
pub struct MediumLevelILInstruction {
45+
pub struct Instruction {
4646
pub function: Ref<MediumLevelILFunction>,
4747
pub address: u64,
4848
// TODO; Because this structure is incorrectly named instruction, we want to make it clear that we actually have the expression index.
4949
pub expr_index: MediumLevelInstructionIndex,
5050
pub size: usize,
51-
pub kind: MediumLevelILInstructionKind,
51+
pub kind: InstructionKind,
5252
}
5353

54-
impl MediumLevelILInstruction {
54+
impl Instruction {
5555
pub(crate) fn new(
5656
function: Ref<MediumLevelILFunction>,
5757
index: MediumLevelInstructionIndex,
@@ -69,7 +69,7 @@ impl MediumLevelILInstruction {
6969
// TODO: If op.sourceOperation == BN_INVALID_OPERAND && op.operation == MLIL_NOP return None
7070
let op = unsafe { BNGetMediumLevelILByIndex(function.handle, expr_index.0) };
7171
use BNMediumLevelILOperation::*;
72-
use MediumLevelILInstructionKind as Op;
72+
use InstructionKind as Op;
7373
let kind = match op.operation {
7474
MLIL_NOP => Op::Nop,
7575
MLIL_NORET => Op::Noret,
@@ -624,7 +624,7 @@ impl MediumLevelILInstruction {
624624
}
625625

626626
pub fn lift(&self) -> MediumLevelILLiftedInstruction {
627-
use MediumLevelILInstructionKind::*;
627+
use InstructionKind::*;
628628
use MediumLevelILLiftedInstructionKind as Lifted;
629629

630630
let kind = match self.kind {
@@ -970,7 +970,7 @@ impl MediumLevelILInstruction {
970970
unsafe { BNGetMediumLevelILExprValue(self.function.handle, self.expr_index.0) }.into()
971971
}
972972

973-
/// Returns the [`BasicBlock`] containing the given [`MediumLevelILInstruction`].
973+
/// Returns the [`BasicBlock`] containing the given [`Instruction`].
974974
pub fn basic_block(&self) -> Option<Ref<BasicBlock<MediumLevelILBlock>>> {
975975
// TODO: We might be able to .expect this if we guarantee that self.index is valid.
976976
self.function.basic_block_containing_index(self.expr_index)
@@ -1057,7 +1057,7 @@ impl MediumLevelILInstruction {
10571057
unsafe { Array::new(deps, count, self.function.clone()) }
10581058
}
10591059

1060-
pub fn branch_dependence_at(&self, instruction: MediumLevelILInstruction) -> BranchDependence {
1060+
pub fn branch_dependence_at(&self, instruction: Instruction) -> BranchDependence {
10611061
let deps = unsafe {
10621062
BNGetMediumLevelILBranchDependence(
10631063
self.function.handle,
@@ -1377,7 +1377,7 @@ impl MediumLevelILInstruction {
13771377
Variable::new(var.ty, index, var.storage)
13781378
}
13791379

1380-
/// alias for [MediumLevelILInstruction::split_var_for_definition]
1380+
/// alias for [Instruction::split_var_for_definition]
13811381
#[inline]
13821382
pub fn get_split_var_for_definition(&self, var: &Variable) -> Variable {
13831383
self.split_var_for_definition(var)
@@ -1466,7 +1466,7 @@ impl MediumLevelILInstruction {
14661466
}
14671467
}
14681468

1469-
impl Debug for MediumLevelILInstruction {
1469+
impl Debug for Instruction {
14701470
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
14711471
f.debug_struct("MediumLevelILInstruction")
14721472
.field("address", &self.address)
@@ -1477,13 +1477,13 @@ impl Debug for MediumLevelILInstruction {
14771477
}
14781478
}
14791479

1480-
impl CoreArrayProvider for MediumLevelILInstruction {
1480+
impl CoreArrayProvider for Instruction {
14811481
type Raw = usize;
14821482
type Context = Ref<MediumLevelILFunction>;
14831483
type Wrapped<'a> = Self;
14841484
}
14851485

1486-
unsafe impl CoreArrayProviderInner for MediumLevelILInstruction {
1486+
unsafe impl CoreArrayProviderInner for Instruction {
14871487
unsafe fn free(raw: *mut Self::Raw, _count: usize, _context: &Self::Context) {
14881488
BNFreeILInstructionList(raw)
14891489
}
@@ -1498,7 +1498,7 @@ unsafe impl CoreArrayProviderInner for MediumLevelILInstruction {
14981498
}
14991499

15001500
#[derive(Debug, Copy, Clone)]
1501-
pub enum MediumLevelILInstructionKind {
1501+
pub enum InstructionKind {
15021502
Nop,
15031503
Noret,
15041504
Bp,
@@ -1661,7 +1661,7 @@ fn get_call_output(function: &MediumLevelILFunction, idx: usize) -> impl Iterato
16611661
fn get_call_params(
16621662
function: &MediumLevelILFunction,
16631663
idx: usize,
1664-
) -> impl Iterator<Item = MediumLevelILInstruction> {
1664+
) -> impl Iterator<Item = Instruction> {
16651665
let op = get_raw_operation(function, idx);
16661666
assert_eq!(op.operation, BNMediumLevelILOperation::MLIL_CALL_PARAM);
16671667
OperandIter::new(function, op.operands[1] as usize, op.operands[0] as usize).exprs()
@@ -1679,15 +1679,15 @@ fn get_call_output_ssa(
16791679
fn get_call_params_ssa(
16801680
function: &MediumLevelILFunction,
16811681
idx: usize,
1682-
) -> impl Iterator<Item = MediumLevelILInstruction> {
1682+
) -> impl Iterator<Item = Instruction> {
16831683
let op = get_raw_operation(function, idx);
16841684
assert_eq!(op.operation, BNMediumLevelILOperation::MLIL_CALL_PARAM_SSA);
16851685
OperandIter::new(function, op.operands[2] as usize, op.operands[1] as usize).exprs()
16861686
}
16871687

16881688
/// Conditional branching instruction and an expected conditional result
16891689
pub struct BranchDependence {
1690-
pub instruction: MediumLevelILInstruction,
1690+
pub instruction: Instruction,
16911691
pub dependence: ILBranchDependence,
16921692
}
16931693

@@ -1704,10 +1704,7 @@ unsafe impl CoreArrayProviderInner for BranchDependence {
17041704

17051705
unsafe fn wrap_raw<'a>(raw: &'a Self::Raw, context: &'a Self::Context) -> Self::Wrapped<'a> {
17061706
Self {
1707-
instruction: MediumLevelILInstruction::new(
1708-
context.clone(),
1709-
MediumLevelInstructionIndex(raw.branch),
1710-
),
1707+
instruction: Instruction::new(context.clone(), MediumLevelInstructionIndex(raw.branch)),
17111708
dependence: raw.dependence,
17121709
}
17131710
}

rust/src/operand_iter.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use crate::high_level_il::{
77
HighLevelILFunction, HighLevelInstructionIndex, Instruction as HighLevelILInstruction,
88
};
99
use crate::medium_level_il::{
10-
MediumLevelILFunction, MediumLevelILInstruction, MediumLevelInstructionIndex,
10+
Instruction as MediumLevelILInstruction, MediumLevelILFunction, MediumLevelInstructionIndex,
1111
};
1212
use crate::rc::{Ref, RefCountable};
1313
use crate::variable::{SSAVariable, Variable};

rust/tests/medium_level_il.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use binaryninja::binary_view::BinaryViewExt;
22
use binaryninja::headless::Session;
3-
use binaryninja::medium_level_il::{MediumLevelILInstructionKind, MediumLevelInstructionIndex};
3+
use binaryninja::medium_level_il::{InstructionKind, MediumLevelInstructionIndex};
44
use std::path::PathBuf;
55

66
#[test]
@@ -23,7 +23,7 @@ fn test_mlil_info() {
2323
assert_eq!(instr_0.address, image_base + 0x00025f10);
2424
println!("{:?}", instr_0.kind);
2525
match instr_0.kind {
26-
MediumLevelILInstructionKind::SetVar(op) => {
26+
InstructionKind::SetVar(op) => {
2727
assert_eq!(op.dest.index, 524288);
2828
assert_eq!(op.src, 0);
2929
}
@@ -35,7 +35,7 @@ fn test_mlil_info() {
3535
assert_eq!(instr_1.address, image_base + 0x00025f15);
3636
println!("{:?}", instr_1.kind);
3737
match instr_1.kind {
38-
MediumLevelILInstructionKind::SetVar(op) => {
38+
InstructionKind::SetVar(op) => {
3939
assert_eq!(op.dest.index, 5);
4040
assert_eq!(op.src, 2);
4141
}
@@ -47,7 +47,7 @@ fn test_mlil_info() {
4747
assert_eq!(instr_2.address, image_base + 0x00025f18);
4848
println!("{:?}", instr_2.kind);
4949
match instr_2.kind {
50-
MediumLevelILInstructionKind::SetVar(op) => {
50+
InstructionKind::SetVar(op) => {
5151
assert_eq!(op.dest.index, 8);
5252
assert_eq!(op.src, 4);
5353
}
@@ -59,7 +59,7 @@ fn test_mlil_info() {
5959
assert_eq!(instr_3.address, image_base + 0x00025f19);
6060
println!("{:?}", instr_3.kind);
6161
match instr_3.kind {
62-
MediumLevelILInstructionKind::Call(op) => {
62+
InstructionKind::Call(op) => {
6363
assert_eq!(op.first_output, 8);
6464
assert_eq!(op.num_outputs, 1);
6565
assert_eq!(op.dest, 7);
@@ -74,7 +74,7 @@ fn test_mlil_info() {
7474
assert_eq!(instr_4.address, image_base + 0x00025f22);
7575
println!("{:?}", instr_4.kind);
7676
match instr_4.kind {
77-
MediumLevelILInstructionKind::Ret(op) => {
77+
InstructionKind::Ret(op) => {
7878
assert_eq!(op.first_operand, 12);
7979
assert_eq!(op.num_operands, 1);
8080
}

0 commit comments

Comments
 (0)