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
//! A driver concretely executes a Falcon IL programs.
use error::*;
use executor::State;
use executor::successor::*;
use il;
use RC;
use types::Architecture;
/// A driver for a concrete executor over Falcon IL.
#[derive(Debug, Clone)]
pub struct Driver<'d> {
program: RC<il::Program>,
location: il::ProgramLocation,
state: State<'d>,
architecture: Architecture,
}
impl<'d> Driver<'d> {
/// Create a new driver for concrete execution over Falcon IL.
pub fn new(
program: RC<il::Program>,
location: il::ProgramLocation,
state: State<'d>,
architecture: Architecture,
) -> Driver {
Driver {
program: program,
location: location,
state: state,
architecture: architecture
}
}
/// Step forward over Falcon IL.
pub fn step(self) -> Result<Driver<'d>> {
let location = self.location.apply(&self.program).unwrap();
match *location.function_location() {
il::RefFunctionLocation::Instruction(_, instruction) => {
let successor = self.state.execute(instruction.operation())?;
match successor.type_().clone() {
SuccessorType::FallThrough => {
let locations = location.forward()?;
if locations.len() == 1 {
return Ok(Driver::new(
self.program.clone(),
locations[0].clone().into(),
successor.into(),
self.architecture
));
}
else {
// every location should be an edge, and only one
// edge should be satisfiable
for location in locations {
if let il::RefFunctionLocation::Edge(edge) = *location.function_location() {
if successor.state()
.symbolize_and_eval(&edge.condition().clone().unwrap())?
.value() == 1 {
return Ok(Driver::new(
self.program.clone(),
location.clone().into(),
successor.into(),
self.architecture
));
}
}
}
bail!("No valid successor location found on fall through");
}
},
SuccessorType::Branch(address) => {
match il::RefProgramLocation::from_address(&self.program, address) {
Some(location) => return Ok(Driver::new(
self.program.clone(),
location.into(),
successor.into(),
self.architecture
)),
None => {
let state: State = successor.into();
let function = self.architecture
.translator()
.translate_function(state.memory(), address)
.expect(&format!("Failed to lift function at 0x{:x}", address));
let mut program = self.program.clone();
RC::make_mut(&mut program).add_function(function);
let location = il::RefProgramLocation::from_address(
&program,
address
);
return Ok(Driver::new(
program.clone(),
location.unwrap().into(),
state,
self.architecture
));
}
}
},
SuccessorType::Raise(ref expression) => {
bail!(format!("Raise is unimplemented, {}", expression));
}
}
},
il::RefFunctionLocation::Edge(_) => {
let locations = location.forward()?;
return Ok(Driver::new(
self.program.clone(),
locations[0].clone().into(),
self.state,
self.architecture
));
},
il::RefFunctionLocation::EmptyBlock(_) => {
let locations = location.forward()?;
if locations.len() == 1 {
return Ok(Driver::new(
self.program.clone(),
locations[0].clone().into(),
self.state,
self.architecture
));
}
else {
for location in locations {
if let il::RefFunctionLocation::Edge(edge) = *location.function_location() {
if self.state
.symbolize_and_eval(&edge.condition().clone().unwrap())?
.value() == 1 {
return Ok(Driver::new(
self.program.clone(),
location.clone().into(),
self.state,
self.architecture
));
}
}
}
}
bail!("No valid location out of empty block");
}
}
}
/// Retrieve the Falcon IL program associated with this driver.
pub fn program(&self) -> &il::Program {
&self.program
}
/// Retrieve the `il::ProgramLocation` associated with this driver.
pub fn location(&self) -> &il::ProgramLocation {
&self.location
}
/// Retrieve the concrete `State` associated with this driver.
pub fn state(&self) -> &State {
&self.state
}
}