-
Notifications
You must be signed in to change notification settings - Fork 280
Expand file tree
/
Copy pathfunction.rs
More file actions
155 lines (129 loc) · 4.64 KB
/
function.rs
File metadata and controls
155 lines (129 loc) · 4.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
use core::hash::{Hash, Hasher};
use binaryninjacore_sys::BNFreeMediumLevelILFunction;
use binaryninjacore_sys::BNGetMediumLevelILBasicBlockList;
use binaryninjacore_sys::BNGetMediumLevelILIndexForInstruction;
use binaryninjacore_sys::BNGetMediumLevelILInstructionCount;
use binaryninjacore_sys::BNGetMediumLevelILOwnerFunction;
use binaryninjacore_sys::BNGetMediumLevelILSSAForm;
use binaryninjacore_sys::BNMediumLevelILFunction;
use binaryninjacore_sys::BNMediumLevelILGetInstructionStart;
use binaryninjacore_sys::BNNewMediumLevelILFunctionReference;
use crate::basicblock::BasicBlock;
use crate::function::Function;
use crate::function::Location;
use crate::rc::{Array, Ref, RefCountable};
use super::{
Form, MediumLevelILBlock, MediumLevelILInstruction, MediumLevelILLiftedInstruction, NonSSA, SSA,
};
pub struct MediumLevelILFunction<I> {
pub(crate) handle: *mut BNMediumLevelILFunction,
_form: std::marker::PhantomData<I>,
}
unsafe impl<I> Send for MediumLevelILFunction<I> {}
unsafe impl<I> Sync for MediumLevelILFunction<I> {}
impl<I> Eq for MediumLevelILFunction<I> {}
impl<I> PartialEq for MediumLevelILFunction<I> {
fn eq(&self, rhs: &Self) -> bool {
self.get_function().eq(&rhs.get_function())
}
}
impl<I> Hash for MediumLevelILFunction<I> {
fn hash<H: Hasher>(&self, state: &mut H) {
self.get_function().hash(state)
}
}
impl<I> MediumLevelILFunction<I> {
pub(crate) unsafe fn ref_from_raw(handle: *mut BNMediumLevelILFunction) -> Ref<Self> {
debug_assert!(!handle.is_null());
Self {
handle,
_form: std::marker::PhantomData,
}
.to_owned()
}
pub fn instruction_count(&self) -> usize {
unsafe { BNGetMediumLevelILInstructionCount(self.handle) }
}
pub fn get_function(&self) -> Ref<Function> {
unsafe {
let func = BNGetMediumLevelILOwnerFunction(self.handle);
Function::from_raw(func)
}
}
}
impl<I: Form> MediumLevelILFunction<I> {
pub fn instruction_at<L: Into<Location>>(&self, loc: L) -> Option<MediumLevelILInstruction<I>> {
let loc: Location = loc.into();
let arch_handle = loc.arch.unwrap();
let expr_idx =
unsafe { BNMediumLevelILGetInstructionStart(self.handle, arch_handle.0, loc.addr) };
if expr_idx >= self.instruction_count() {
None
} else {
Some(MediumLevelILInstruction::new(self.to_owned(), expr_idx))
}
}
pub fn instruction_from_idx(&self, expr_idx: usize) -> MediumLevelILInstruction<I> {
MediumLevelILInstruction::new(self.to_owned(), expr_idx)
}
pub fn lifted_instruction_from_idx(
&self,
expr_idx: usize,
) -> MediumLevelILLiftedInstruction<I> {
self.instruction_from_idx(expr_idx).lift()
}
pub fn instruction_from_instruction_idx(
&self,
instr_idx: usize,
) -> MediumLevelILInstruction<I> {
MediumLevelILInstruction::new(self.to_owned(), unsafe {
BNGetMediumLevelILIndexForInstruction(self.handle, instr_idx)
})
}
pub fn lifted_instruction_from_instruction_idx(
&self,
instr_idx: usize,
) -> MediumLevelILLiftedInstruction<I> {
self.instruction_from_instruction_idx(instr_idx).lift()
}
pub fn basic_blocks(&self) -> Array<BasicBlock<MediumLevelILBlock<I>>> {
let mut count = 0;
let blocks = unsafe { BNGetMediumLevelILBasicBlockList(self.handle, &mut count) };
let context = MediumLevelILBlock {
function: self.to_owned(),
};
unsafe { Array::new(blocks, count, context) }
}
}
impl MediumLevelILFunction<NonSSA> {
pub fn ssa_form(&self) -> MediumLevelILFunction<SSA> {
let ssa = unsafe { BNGetMediumLevelILSSAForm(self.handle) };
assert!(!ssa.is_null());
MediumLevelILFunction {
handle: ssa,
_form: std::marker::PhantomData,
}
}
}
impl<I> ToOwned for MediumLevelILFunction<I> {
type Owned = Ref<Self>;
fn to_owned(&self) -> Self::Owned {
unsafe { RefCountable::inc_ref(self) }
}
}
unsafe impl<I> RefCountable for MediumLevelILFunction<I> {
unsafe fn inc_ref(handle: &Self) -> Ref<Self> {
Ref::new(Self {
handle: BNNewMediumLevelILFunctionReference(handle.handle),
_form: std::marker::PhantomData,
})
}
unsafe fn dec_ref(handle: &Self) {
BNFreeMediumLevelILFunction(handle.handle);
}
}
impl<I> core::fmt::Debug for MediumLevelILFunction<I> {
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
write!(f, "<mlil func handle {:p}>", self.handle)
}
}