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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
#![doc(html_root_url = "https://docs.rs/haybale/0.6.3")]
use llvm_ir::Type;
use std::collections::HashSet;
mod project;
pub use project::Project;
mod symex;
pub use symex::*;
pub mod config;
pub use config::Config;
mod error;
pub use error::*;
mod return_value;
pub use return_value::ReturnValue;
mod alloc;
pub mod alloc_utils;
pub mod backend;
pub mod callbacks;
pub mod cell_memory;
mod demangling;
mod double_keyed_map;
pub mod function_hooks;
mod global_allocations;
pub mod hook_utils;
mod hooks;
pub mod simple_memory;
pub mod solver_utils;
mod state;
mod varmap;
pub mod watchpoints;
use backend::*;
use itertools::Itertools;
use solver_utils::PossibleSolutions;
#[cfg(test)]
mod test_utils;
#[derive(PartialEq, Eq, Clone, Copy, Debug)]
pub enum SolutionValue {
I8(i8),
I16(i16),
I32(i32),
I64(i64),
Ptr(u64),
}
impl SolutionValue {
pub fn unwrap_to_i8(self) -> i8 {
match self {
SolutionValue::I8(i) => i,
_ => panic!("unwrap_to_i8 on {:?}", self),
}
}
pub fn unwrap_to_i16(self) -> i16 {
match self {
SolutionValue::I16(i) => i,
_ => panic!("unwrap_to_i16 on {:?}", self),
}
}
pub fn unwrap_to_i32(self) -> i32 {
match self {
SolutionValue::I32(i) => i,
_ => panic!("unwrap_to_i32 on {:?}", self),
}
}
pub fn unwrap_to_i64(self) -> i64 {
match self {
SolutionValue::I64(i) => i,
_ => panic!("unwrap_to_i64 on {:?}", self),
}
}
pub fn unwrap_to_ptr(self) -> u64 {
match self {
SolutionValue::Ptr(u) => u,
_ => panic!("unwrap_to_ptr on {:?}", self),
}
}
}
pub fn find_zero_of_func<'p>(
funcname: &str,
project: &'p Project,
config: Config<'p, DefaultBackend>,
) -> std::result::Result<Option<Vec<SolutionValue>>, String> {
let mut em: ExecutionManager<DefaultBackend> = symex_function(funcname, project, config);
let (func, _) = project
.get_func_by_name(funcname)
.unwrap_or_else(|| panic!("Failed to find function named {:?}", funcname));
for (param, bv) in func.parameters.iter().zip(em.param_bvs()) {
if let Type::PointerType { .. } = em.state().type_of(param).as_ref() {
bv._ne(&em.state().zero(bv.get_width())).assert();
}
}
let returnwidth = match func.return_type.as_ref() {
Type::VoidType => {
return Err("find_zero_of_func: function has void type".into());
},
ty => {
let width = project
.size_in_bits(&ty)
.expect("Function return type shouldn't be an opaque struct type");
assert_ne!(width, 0, "Function return type has width 0 bits but isn't void type");
width
},
};
let zero = em.state().zero(returnwidth);
let mut found = false;
while let Some(bvretval) = em.next() {
match bvretval {
Ok(ReturnValue::ReturnVoid) => panic!("Function shouldn't return void"),
Ok(ReturnValue::Throw(_)) => continue,
Ok(ReturnValue::Abort) => continue,
Ok(ReturnValue::Return(bvretval)) => {
let state = em.mut_state();
bvretval._eq(&zero).assert();
if state.sat()? {
found = true;
break;
}
},
Err(Error::LoopBoundExceeded(_)) => continue,
Err(e) => return Err(em.state().full_error_message_with_context(e)),
}
}
let param_bvs: Vec<_> = em.param_bvs().clone();
let state = em.mut_state();
if found {
Ok(Some(
func.parameters
.iter()
.zip_eq(param_bvs.iter())
.map(|(p, bv)| {
let param_as_u64 = state
.get_a_solution_for_bv(bv)?
.expect("since state.sat() passed, expected a solution for each var")
.as_u64()
.expect("parameter more than 64 bits wide");
Ok(match p.ty.as_ref() {
Type::IntegerType { bits: 8 } => SolutionValue::I8(param_as_u64 as i8),
Type::IntegerType { bits: 16 } => SolutionValue::I16(param_as_u64 as i16),
Type::IntegerType { bits: 32 } => SolutionValue::I32(param_as_u64 as i32),
Type::IntegerType { bits: 64 } => SolutionValue::I64(param_as_u64 as i64),
Type::PointerType { .. } => SolutionValue::Ptr(param_as_u64),
ty => unimplemented!("Function parameter with type {:?}", ty),
})
})
.collect::<Result<_>>()?,
))
} else {
Ok(None)
}
}
pub fn get_possible_return_values_of_func<'p>(
funcname: &str,
args: impl IntoIterator<Item = Option<u64>>,
project: &'p Project,
config: Config<'p, DefaultBackend>,
thrown_size: Option<u32>,
n: usize,
) -> PossibleSolutions<ReturnValue<u64>> {
let mut em: ExecutionManager<DefaultBackend> = symex_function(funcname, project, config);
let (func, _) = project
.get_func_by_name(funcname)
.expect("Failed to find function");
for (param, arg) in func.parameters.iter().zip(args.into_iter()) {
if let Some(val) = arg {
let param_size_bits = project
.size_in_bits(¶m.ty)
.expect("Parameter type shouldn't be opaque struct type");
assert_ne!(param_size_bits, 0, "Parameter {} shouldn't have size 0 bits", ¶m.name);
let val = em.state().bv_from_u64(val, param_size_bits);
em.mut_state()
.overwrite_latest_version_of_bv(¶m.name, val);
}
}
let return_width = project
.size_in_bits(&func.return_type)
.expect("Function return type shouldn't be opaque struct type");
let mut candidate_values = HashSet::<ReturnValue<u64>>::new();
let mut have_throw = false;
while let Some(bvretval) = em.next() {
match bvretval {
Err(e) => panic!("{}", em.state().full_error_message_with_context(e)),
Ok(ReturnValue::ReturnVoid) => {
candidate_values.insert(ReturnValue::ReturnVoid);
if candidate_values.len() > n {
break;
}
},
Ok(ReturnValue::Abort) => {
candidate_values.insert(ReturnValue::Abort);
if candidate_values.len() > n {
break;
}
},
Ok(ReturnValue::Return(bvretval)) => {
assert_eq!(bvretval.get_width(), return_width);
let state = em.mut_state();
for candidate in candidate_values.iter() {
if let ReturnValue::Return(candidate) = candidate {
bvretval
._ne(&state.bv_from_u64(*candidate, return_width))
.assert();
}
}
match state.get_possible_solutions_for_bv(&bvretval, n).unwrap() {
PossibleSolutions::Exactly(v) => {
candidate_values.extend(
v.iter()
.map(|bvsol| ReturnValue::Return(bvsol.as_u64().unwrap())),
);
if candidate_values.len() > n {
break;
}
},
PossibleSolutions::AtLeast(v) => {
candidate_values.extend(
v.iter()
.map(|bvsol| ReturnValue::Return(bvsol.as_u64().unwrap())),
);
break;
},
};
},
Ok(ReturnValue::Throw(bvptr)) => {
let state = em.mut_state();
match thrown_size {
None => {
if !have_throw {
candidate_values.insert(ReturnValue::Throw(bvptr.as_u64().unwrap()));
have_throw = true;
if candidate_values.len() > n {
break;
}
}
},
Some(thrown_size) => {
let thrown_value = state.read(&bvptr, thrown_size).unwrap();
for candidate in candidate_values.iter() {
if let ReturnValue::Throw(candidate) = candidate {
thrown_value
._ne(&state.bv_from_u64(*candidate, thrown_size))
.assert();
}
}
match state
.get_possible_solutions_for_bv(&thrown_value, n)
.unwrap()
{
PossibleSolutions::Exactly(v) => {
candidate_values.extend(
v.iter()
.map(|bvsol| ReturnValue::Throw(bvsol.as_u64().unwrap())),
);
if candidate_values.len() > n {
break;
}
},
PossibleSolutions::AtLeast(v) => {
candidate_values.extend(
v.iter()
.map(|bvsol| ReturnValue::Throw(bvsol.as_u64().unwrap())),
);
break;
},
}
},
}
},
}
}
if candidate_values.len() > n {
PossibleSolutions::AtLeast(candidate_values)
} else {
PossibleSolutions::Exactly(candidate_values)
}
}