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
use architecture::Architecture;
use error::*;
use executor::State;
use executor::successor::*;
use il;
use RC;
#[derive(Debug, Clone)]
pub struct Driver {
program: RC<il::Program>,
location: il::ProgramLocation,
state: State,
architecture: RC<Architecture>,
}
impl Driver {
pub fn new(
program: RC<il::Program>,
location: il::ProgramLocation,
state: State,
architecture: RC<Architecture>,
) -> Driver {
Driver {
program: program,
location: location,
state: state,
architecture: architecture
}
}
pub fn step(self) -> Result<Driver> {
let location =
self.location
.apply(&self.program)
.ok_or("Failed to apply program location")?;
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 {
for location in locations {
if let il::RefFunctionLocation::Edge(edge) = *location.function_location() {
if successor.state()
.symbolize_and_eval(
edge.condition()
.ok_or("Failed to get edge condition")?)?
.is_one() {
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::ProgramLocation =
il::RefProgramLocation::from_address(&program,
address)
.ok_or("Failed to get location for newly lifted function")?
.into();
return Ok(Driver::new(
program.clone(),
location,
state,
self.architecture
));
}
}
},
SuccessorType::Intrinsic(ref intrinsic) => {
bail!(format!("Intrinsic is unimplemented, {}",
intrinsic.instruction_str()));
}
}
},
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()
.ok_or("Failed to get edge condition")?)?
.is_one() {
return Ok(Driver::new(
self.program.clone(),
location.clone().into(),
self.state,
self.architecture
));
}
}
}
}
bail!("No valid location out of empty block");
}
}
}
pub fn program(&self) -> &il::Program {
&self.program
}
pub fn address(&self) -> Option<u64> {
self.location
.apply(&self.program)
.expect("Failed to apply program location")
.address()
}
pub fn location(&self) -> &il::ProgramLocation {
&self.location
}
pub fn state(&self) -> &State {
&self.state
}
pub fn state_mut(&mut self) -> &mut State {
&mut self.state
}
}