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
311
312
313
314
315
316
317
318
319
use crate::error::*;
use crate::il::*;
use crate::memory::MemoryPermissions;
use std::collections::{BTreeMap, VecDeque};
pub mod mips;
pub mod ppc;
pub mod x86;
const DEFAULT_TRANSLATION_BLOCK_BYTES: usize = 64;
pub trait TranslationMemory {
fn permissions(&self, address: u64) -> Option<MemoryPermissions>;
fn get_u8(&self, address: u64) -> Option<u8>;
fn get_bytes(&self, address: u64, length: usize) -> Vec<u8> {
let mut bytes = Vec::new();
for i in 0..length {
match self.permissions(address) {
Some(permissions) => {
if !permissions.contains(MemoryPermissions::EXECUTE) {
break;
}
}
None => break,
}
match self.get_u8(address + i as u64) {
Some(u) => bytes.push(u),
None => break,
};
}
bytes
}
}
#[derive(Clone, Debug)]
pub struct BlockTranslationResult {
instructions: Vec<(u64, ControlFlowGraph)>,
address: u64,
length: usize,
successors: Vec<(u64, Option<Expression>)>,
}
impl BlockTranslationResult {
pub fn new(
instructions: Vec<(u64, ControlFlowGraph)>,
address: u64,
length: usize,
successors: Vec<(u64, Option<Expression>)>,
) -> BlockTranslationResult {
BlockTranslationResult {
instructions: instructions,
address: address,
length: length,
successors: successors,
}
}
pub fn instructions(&self) -> &Vec<(u64, ControlFlowGraph)> {
&self.instructions
}
pub fn address(&self) -> u64 {
self.address
}
pub fn length(&self) -> usize {
self.length
}
pub fn successors(&self) -> &Vec<(u64, Option<Expression>)> {
&self.successors
}
pub fn blockify(&self) -> Result<ControlFlowGraph> {
let mut control_flow_graph = ControlFlowGraph::new();
let block_index = {
let block = control_flow_graph.new_block()?;
block.index()
};
control_flow_graph.set_entry(block_index)?;
control_flow_graph.set_exit(block_index)?;
for &(_, ref cfg) in &self.instructions {
control_flow_graph.append(&cfg)?;
}
control_flow_graph.merge()?;
Ok(control_flow_graph)
}
}
pub trait Translator {
fn translate_block(&self, bytes: &[u8], address: u64) -> Result<BlockTranslationResult>;
fn translate_function(
&self,
memory: &dyn TranslationMemory,
function_address: u64,
) -> Result<Function> {
self.translate_function_extended(memory, function_address, vec![])
}
fn translate_function_extended(
&self,
memory: &dyn TranslationMemory,
function_address: u64,
manual_edges: Vec<(u64, u64, Option<Expression>)>,
) -> Result<Function> {
let mut translation_queue: VecDeque<u64> = VecDeque::new();
let mut translation_results: BTreeMap<u64, BlockTranslationResult> = BTreeMap::new();
translation_queue.push_front(function_address);
manual_edges
.iter()
.for_each(|(head_address, tail_address, _)| {
translation_queue.push_back(*head_address);
translation_queue.push_back(*tail_address);
});
while !translation_queue.is_empty() {
let block_address = translation_queue.pop_front().unwrap();
if translation_results.contains_key(&block_address) {
continue;
}
let block_bytes = memory.get_bytes(block_address, DEFAULT_TRANSLATION_BLOCK_BYTES);
if block_bytes.len() == 0 {
let mut control_flow_graph = ControlFlowGraph::new();
let block_index = control_flow_graph.new_block()?.index();
control_flow_graph.set_entry(block_index)?;
control_flow_graph.set_exit(block_index)?;
translation_results.insert(
block_address,
BlockTranslationResult::new(
vec![(block_address, control_flow_graph)],
block_address,
0,
Vec::new(),
),
);
continue;
}
let block_translation_result = self.translate_block(&block_bytes, block_address)?;
for successor in block_translation_result.successors().iter() {
if !translation_queue.contains(&successor.0) {
translation_queue.push_back(successor.0);
}
}
translation_results.insert(block_address, block_translation_result);
}
let mut instruction_indices: BTreeMap<u64, (usize, usize)> = BTreeMap::new();
let mut block_indices: BTreeMap<u64, (usize, usize)> = BTreeMap::new();
let mut control_flow_graph = ControlFlowGraph::new();
for result in &translation_results {
let block_translation_result = result.1;
let mut block_entry = 0;
let mut block_exit = 0;
let mut previous_exit = None;
for &(address, ref instruction_graph) in block_translation_result.instructions.iter() {
let (entry, exit) = if instruction_indices.get(&address).is_some() {
instruction_indices[&address]
} else {
let (entry, exit) = control_flow_graph.insert(instruction_graph)?;
instruction_indices.insert(address, (entry, exit));
(entry, exit)
};
if let Some(previous_exit) = previous_exit {
if control_flow_graph.edge(previous_exit, entry).is_err() {
control_flow_graph.unconditional_edge(previous_exit, entry)?;
}
}
else {
block_entry = entry;
}
block_exit = exit;
previous_exit = Some(exit);
}
block_indices.insert(*result.0, (block_entry, block_exit));
}
for (head_address, tail_address, condition) in manual_edges {
let (_, edge_head) = block_indices[&head_address];
let (edge_tail, _) = block_indices[&tail_address];
if control_flow_graph.edge(edge_head, edge_tail).is_ok() {
continue;
}
if let Some(condition) = condition {
control_flow_graph.conditional_edge(edge_head, edge_tail, condition)?;
} else {
control_flow_graph.unconditional_edge(edge_head, edge_tail)?;
}
}
for (address, block_translation_result) in translation_results {
let (_, block_exit) = block_indices[&address];
for (successor_address, successor_condition) in
block_translation_result.successors().iter()
{
let (block_entry, _) = block_indices[successor_address];
if control_flow_graph.edge(block_exit, block_entry).is_ok() {
continue;
}
match successor_condition {
Some(ref condition) => control_flow_graph.conditional_edge(
block_exit,
block_entry,
condition.clone(),
)?,
None => control_flow_graph.unconditional_edge(block_exit, block_entry)?,
}
}
}
control_flow_graph.set_entry(block_indices[&function_address].0)?;
control_flow_graph.merge()?;
Ok(Function::new(function_address, control_flow_graph))
}
}