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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
|
use binaryninja::architecture::Register;
use binaryninja::binary_view::BinaryViewExt;
use binaryninja::headless::Session;
use binaryninja::low_level_il::expression::{
ExpressionHandler, LowLevelExpressionIndex, LowLevelILExpressionKind,
};
use binaryninja::low_level_il::instruction::{
InstructionHandler, LowLevelILInstructionKind, LowLevelInstructionIndex,
};
use binaryninja::low_level_il::{LowLevelILRegisterKind, LowLevelILSSARegisterKind, VisitorAction};
use std::path::PathBuf;
#[test]
fn test_llil_info() {
let _session = Session::new().expect("Failed to initialize session");
let out_dir = env!("OUT_DIR").parse::<PathBuf>().unwrap();
let view = binaryninja::load(out_dir.join("atox.obj")).expect("Failed to create view");
let image_base = view.original_image_base();
let entry_function = view.entry_point_function().unwrap();
let llil_function = entry_function.low_level_il().unwrap();
let llil_basic_blocks = llil_function.basic_blocks();
let mut llil_basic_block_iter = llil_basic_blocks.iter();
let first_basic_block = llil_basic_block_iter.next().unwrap();
let mut llil_instr_iter = first_basic_block.iter();
// 0 @ 00025f10 (LLIL_SET_REG.d edi = (LLIL_REG.d edi))
let instr_0 = llil_instr_iter.next().unwrap();
assert_eq!(instr_0.index, LowLevelInstructionIndex(0));
assert_eq!(instr_0.address(), image_base + 0x00025f10);
println!("{:?}", instr_0);
println!("{:?}", instr_0.kind());
match instr_0.kind() {
LowLevelILInstructionKind::SetReg(op) => {
assert_eq!(op.size(), 4);
match op.dest_reg() {
LowLevelILRegisterKind::Arch(reg) => assert_eq!(reg.name(), "edi"),
_ => panic!("Expected Register::ArchReg"),
}
assert_eq!(op.source_expr().index, LowLevelExpressionIndex(0));
}
_ => panic!("Expected SetReg"),
}
// 1 @ 00025f12 (LLIL_PUSH.d push((LLIL_REG.d ebp)))
let instr_1 = llil_instr_iter.next().unwrap();
assert_eq!(instr_1.index, LowLevelInstructionIndex(1));
assert_eq!(instr_1.address(), image_base + 0x00025f12);
println!("{:?}", instr_1.kind());
match instr_1.kind() {
LowLevelILInstructionKind::Push(op) => {
assert_eq!(op.size(), 4);
assert_eq!(op.operand().index, LowLevelExpressionIndex(2));
println!("{:?}", op.operand().kind());
match op.operand().kind() {
LowLevelILExpressionKind::Reg(op) => {
assert_eq!(op.size(), 4);
match op.source_reg() {
LowLevelILRegisterKind::Arch(reg) => assert_eq!(reg.name(), "ebp"),
_ => panic!("Expected Register::ArchReg"),
}
}
_ => panic!("Expected Reg"),
}
}
_ => panic!("Expected Push"),
}
// 2 @ 00025f13 (LLIL_SET_REG.d ebp = (LLIL_REG.d esp) {__saved_ebp})
let instr_2 = llil_instr_iter.next().unwrap();
assert_eq!(instr_2.index, LowLevelInstructionIndex(2));
assert_eq!(instr_2.address(), image_base + 0x00025f13);
println!("{:?}", instr_2.kind());
match instr_2.kind() {
LowLevelILInstructionKind::SetReg(op) => {
assert_eq!(op.size(), 4);
match op.dest_reg() {
LowLevelILRegisterKind::Arch(reg) => assert_eq!(reg.name(), "ebp"),
_ => panic!("Expected Register::ArchReg"),
}
assert_eq!(op.source_expr().index, LowLevelExpressionIndex(4));
}
_ => panic!("Expected SetReg"),
}
// 3 @ 00025f15 (LLIL_SET_REG.d eax = (LLIL_LOAD.d [(LLIL_ADD.d (LLIL_REG.d ebp) + (LLIL_CONST.d 8)) {arg1}].d))
let instr_3 = llil_instr_iter.next().unwrap();
assert_eq!(instr_3.index, LowLevelInstructionIndex(3));
assert_eq!(instr_3.address(), image_base + 0x00025f15);
println!("{:?}", instr_3.kind());
match instr_3.kind() {
LowLevelILInstructionKind::SetReg(op) => {
assert_eq!(op.size(), 4);
match op.dest_reg() {
LowLevelILRegisterKind::Arch(reg) => assert_eq!(reg.name(), "eax"),
_ => panic!("Expected Register::ArchReg"),
}
assert_eq!(op.source_expr().index, LowLevelExpressionIndex(9));
}
_ => panic!("Expected SetReg"),
}
// 4 @ 00025f18 (LLIL_PUSH.d push((LLIL_REG.d eax)))
let instr_4 = llil_instr_iter.next().unwrap();
assert_eq!(instr_4.index, LowLevelInstructionIndex(4));
assert_eq!(instr_4.address(), image_base + 0x00025f18);
println!("{:?}", instr_4.kind());
match instr_4.kind() {
LowLevelILInstructionKind::Push(op) => {
assert_eq!(op.size(), 4);
assert_eq!(op.operand().index, LowLevelExpressionIndex(11));
}
_ => panic!("Expected Push"),
}
// 5 @ 00025f19 (LLIL_CALL call((LLIL_CONST_PTR.d __crt_interlocked_read_32)))
let instr_5 = llil_instr_iter.next().unwrap();
assert_eq!(instr_5.index, LowLevelInstructionIndex(5));
assert_eq!(instr_5.address(), image_base + 0x00025f19);
println!("{:?}", instr_5.kind());
match instr_5.kind() {
LowLevelILInstructionKind::Call(op) => {
assert_eq!(op.target().index, LowLevelExpressionIndex(13));
}
_ => panic!("Expected Call"),
}
// 6 @ 00025f1e (LLIL_SET_REG.d esp = (LLIL_ADD.d (LLIL_REG.d esp) + (LLIL_CONST.d 4)))
let instr_6 = llil_instr_iter.next().unwrap();
assert_eq!(instr_6.index, LowLevelInstructionIndex(6));
assert_eq!(instr_6.address(), image_base + 0x00025f1e);
println!("{:?}", instr_6.kind());
match instr_6.kind() {
LowLevelILInstructionKind::SetReg(op) => {
assert_eq!(op.size(), 4);
match op.dest_reg() {
LowLevelILRegisterKind::Arch(reg) => assert_eq!(reg.name(), "esp"),
_ => panic!("Expected Register::ArchReg"),
}
assert_eq!(op.source_expr().index, LowLevelExpressionIndex(17));
}
_ => panic!("Expected SetReg"),
}
// 7 @ 00025f21 (LLIL_SET_REG.d ebp = (LLIL_POP.d pop))
let instr_7 = llil_instr_iter.next().unwrap();
assert_eq!(instr_7.index, LowLevelInstructionIndex(7));
assert_eq!(instr_7.address(), image_base + 0x00025f21);
println!("{:?}", instr_7.kind());
match instr_7.kind() {
LowLevelILInstructionKind::SetReg(op) => {
assert_eq!(op.size(), 4);
match op.dest_reg() {
LowLevelILRegisterKind::Arch(reg) => assert_eq!(reg.name(), "ebp"),
_ => panic!("Expected Register::ArchReg"),
}
assert_eq!(op.source_expr().index, LowLevelExpressionIndex(19));
}
_ => panic!("Expected SetReg"),
}
// 8 @ 00025f22 (LLIL_RET <return> jump((LLIL_POP.d pop)))
let instr_8 = llil_instr_iter.next().unwrap();
assert_eq!(instr_8.index, LowLevelInstructionIndex(8));
assert_eq!(instr_8.address(), image_base + 0x00025f22);
println!("{:?}", instr_8.kind());
match instr_8.kind() {
LowLevelILInstructionKind::Ret(op) => {
assert_eq!(op.target().index, LowLevelExpressionIndex(21));
}
_ => panic!("Expected Ret"),
}
}
#[test]
fn test_llil_visitor() {
let _session = Session::new().expect("Failed to initialize session");
let out_dir = env!("OUT_DIR").parse::<PathBuf>().unwrap();
let view = binaryninja::load(out_dir.join("atox.obj")).expect("Failed to create view");
let image_base = view.original_image_base();
let platform = view.default_platform().unwrap();
// Sample function: __crt_strtox::c_string_character_source<char>::validate
let sample_function = view.function_at(&platform, image_base + 0x2bd80).unwrap();
let llil_function = sample_function.low_level_il().unwrap();
let llil_basic_blocks = llil_function.basic_blocks();
let llil_basic_block_iter = llil_basic_blocks.iter();
let mut basic_blocks_visited = 0;
let mut instructions_visited: Vec<LowLevelInstructionIndex> = vec![];
let mut expressions_visited: Vec<LowLevelExpressionIndex> = vec![];
for basic_block in llil_basic_block_iter {
basic_blocks_visited += 1;
for instr in basic_block.iter() {
instructions_visited.push(instr.index);
expressions_visited.push(instr.expr_idx());
instr.visit_tree(&mut |expr| {
expressions_visited.push(expr.index);
VisitorAction::Descend
});
}
}
assert_eq!(basic_blocks_visited, 10);
// This is a flag instruction removed in LLIL.
instructions_visited.push(LowLevelInstructionIndex(38));
for instr_idx in 0..41 {
if instructions_visited
.iter()
.find(|x| x.0 == instr_idx)
.is_none()
{
panic!("Instruction with index {:?} not visited", instr_idx);
};
}
// These are NOP's
expressions_visited.push(LowLevelExpressionIndex(24));
expressions_visited.push(LowLevelExpressionIndex(54));
expressions_visited.push(LowLevelExpressionIndex(62));
expressions_visited.push(LowLevelExpressionIndex(87));
// These are some flag things
expressions_visited.push(LowLevelExpressionIndex(114));
expressions_visited.push(LowLevelExpressionIndex(115));
expressions_visited.push(LowLevelExpressionIndex(116));
expressions_visited.push(LowLevelExpressionIndex(121));
for expr_idx in 0..127 {
if expressions_visited
.iter()
.find(|x| x.0 == expr_idx)
.is_none()
{
panic!("Expression with index {:?} not visited", expr_idx);
};
}
}
#[test]
fn test_llil_ssa() {
let _session = Session::new().expect("Failed to initialize session");
let out_dir = env!("OUT_DIR").parse::<PathBuf>().unwrap();
let view = binaryninja::load(out_dir.join("atox.obj")).expect("Failed to create view");
let image_base = view.original_image_base();
let platform = view.default_platform().unwrap();
// Sample function: __crt_strtox::c_string_character_source<char>::validate
let sample_function = view.function_at(&platform, image_base + 0x2bd80).unwrap();
let llil_function = sample_function.low_level_il().unwrap();
let llil_ssa_function = llil_function.ssa_form().expect("Valid SSA form");
let llil_ssa_basic_blocks = llil_ssa_function.basic_blocks();
let mut llil_ssa_basic_block_iter = llil_ssa_basic_blocks.iter();
let first_basic_block = llil_ssa_basic_block_iter.next().unwrap();
let mut llil_instr_iter = first_basic_block.iter();
// 0 @ 0002bd80 (LLIL_SET_REG_SSA.d edi#1 = (LLIL_REG_SSA.d edi#0))
let ssa_instr_0 = llil_instr_iter.next().unwrap();
assert_eq!(ssa_instr_0.index, LowLevelInstructionIndex(0));
assert_eq!(ssa_instr_0.address(), image_base + 0x0002bd80);
println!("{:?}", ssa_instr_0);
println!("{:?}", ssa_instr_0.kind());
match ssa_instr_0.kind() {
LowLevelILInstructionKind::SetRegSsa(op) => {
assert_eq!(op.size(), 4);
match op.dest_reg() {
LowLevelILSSARegisterKind::Full { kind, version } => {
assert_eq!(kind.name(), "edi");
assert_eq!(version, 1);
}
_ => panic!("Expected LowLevelILSSARegisterKind::Full"),
}
assert_eq!(op.source_expr().index, LowLevelExpressionIndex(0));
}
_ => panic!("Expected SetRegSsa"),
}
// 1 @ 0002bd82 (LLIL_STORE_SSA.d [(LLIL_SUB.d (LLIL_REG_SSA.d esp#0) - (LLIL_CONST.d 4)) {__saved_ebp}].d = (LLIL_REG_SSA.d ebp#0) @ mem#0 -> mem#1)
let ssa_instr_1 = llil_instr_iter.next().unwrap();
assert_eq!(ssa_instr_1.index, LowLevelInstructionIndex(1));
assert_eq!(ssa_instr_1.address(), image_base + 0x0002bd82);
println!("{:?}", ssa_instr_1);
println!("{:?}", ssa_instr_1.kind());
match ssa_instr_1.kind() {
LowLevelILInstructionKind::StoreSsa(op) => {
assert_eq!(op.size(), 4);
let source_expr = op.source_expr();
let source_memory_version = op.source_memory_version();
assert_eq!(source_memory_version, 0);
assert_eq!(source_expr.index, LowLevelExpressionIndex(5));
let dest_expr = op.dest_expr();
let dest_memory_version = op.dest_memory_version();
assert_eq!(dest_memory_version, 1);
assert_eq!(dest_expr.index, LowLevelExpressionIndex(4));
}
_ => panic!("Expected StoreSsa"),
}
// 34 @ 0002bdc7 (LLIL_CALL_SSA eax#8, edx#5, ecx#6, mem#23 = call((LLIL_EXTERN_PTR.d __CrtDbgReportW), stack = esp#18 @ mem#22))
let ssa_instr_34 = llil_ssa_function
.instruction_from_index(LowLevelInstructionIndex(34))
.expect("Valid instruction");
assert_eq!(ssa_instr_34.index, LowLevelInstructionIndex(34));
assert_eq!(ssa_instr_34.address(), image_base + 0x0002bdc7);
println!("{:?}", ssa_instr_34);
println!("{:?}", ssa_instr_34.kind());
match ssa_instr_34.kind() {
LowLevelILInstructionKind::CallSsa(op) => match op.target().kind() {
LowLevelILExpressionKind::ExternPtr(extern_ptr) => {
assert_eq!(extern_ptr.size(), 4);
let extern_sym = view
.symbol_by_address(extern_ptr.value())
.expect("Valid symbol");
assert_eq!(extern_sym.short_name(), "__CrtDbgReportW".into())
}
_ => panic!("Expected ExternPtr"),
},
_ => panic!("Expected CallSsa"),
}
}
|