-
Notifications
You must be signed in to change notification settings - Fork 100
/
mir_interpreter.rs
282 lines (247 loc) · 9.74 KB
/
mir_interpreter.rs
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
// © 2019, ETH Zurich
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
use prusti_common::vir;
use rustc_middle::mir;
use std::collections::HashMap;
use std::fmt::{self, Debug, Display};
use std::iter::FromIterator;
use std::marker::Sized;
use log::trace;
/// Backward interpreter for a loop-less MIR
pub trait BackwardMirInterpreter<'tcx> {
type State: Sized;
fn apply_terminator(
&self,
bb: mir::BasicBlock,
terminator: &mir::Terminator<'tcx>,
states: HashMap<mir::BasicBlock, &Self::State>,
) -> Self::State;
fn apply_statement(
&self,
bb: mir::BasicBlock,
stmt_index: usize,
stmt: &mir::Statement<'tcx>,
state: &mut Self::State,
);
}
/// Interpret a loop-less MIR starting from the end and return the **initial** state.
/// The result is None if the CFG contains a loop.
pub fn run_backward_interpretation<'tcx, S: Debug, I: BackwardMirInterpreter<'tcx, State = S>>(
mir: &mir::Body<'tcx>,
interpreter: &I,
) -> Option<S> {
let basic_blocks = mir.basic_blocks();
let mut heads: HashMap<mir::BasicBlock, S> = HashMap::new();
// Find the final basic blocks
let mut pending_blocks: Vec<mir::BasicBlock> = basic_blocks
.iter_enumerated()
.filter(|(_, bb_data)| match bb_data.terminator {
Some(ref term) => term.successors().next().is_none(),
_ => false,
})
.map(|(bb, _)| bb)
.collect();
// Interpret all the blocks in `pending_blocks`
while !pending_blocks.is_empty() {
let curr_bb = pending_blocks.pop().unwrap();
let bb_data = &basic_blocks[curr_bb];
// Apply the terminator
let terminator = bb_data.terminator();
let states = HashMap::from_iter(terminator.successors().map(|bb| (*bb, &heads[bb])));
trace!("States before: {:?}", states);
trace!("Apply terminator {:?}", terminator);
let mut curr_state = interpreter.apply_terminator(curr_bb, terminator, states);
trace!("State after: {:?}", curr_state);
// Apply each statement, from the last
for (stmt_index, stmt) in bb_data.statements.iter().enumerate().rev() {
trace!("State before: {:?}", curr_state);
trace!("Apply statement {:?}", stmt);
interpreter.apply_statement(curr_bb, stmt_index, stmt, &mut curr_state);
trace!("State after: {:?}", curr_state);
}
// Store the state at the beginning of block `curr_bb`
heads.insert(curr_bb, curr_state);
// Put the preceding basic blocks
for &pred_bb in mir.predecessors()[curr_bb].iter() {
if let Some(ref term) = basic_blocks[pred_bb].terminator {
if term.successors().all(|succ_bb| heads.contains_key(succ_bb)) {
pending_blocks.push(pred_bb);
}
}
}
}
let result = heads.remove(&basic_blocks.indices().next().unwrap());
if result.is_none() {
trace!("heads: {:?}", heads);
}
result
}
/// Interpret a loop-less MIR starting from the end and return the **initial** state.
/// The result is None if the CFG contains a loop.
pub fn run_backward_interpretation_point_to_point<
'tcx,
S: Debug + Clone,
I: BackwardMirInterpreter<'tcx, State = S>,
>(
mir: &mir::Body<'tcx>,
interpreter: &I,
initial_bbi: mir::BasicBlock,
final_bbi: mir::BasicBlock,
final_stmt_index: usize,
final_state: S,
empty_state: S,
) -> Option<S> {
let basic_blocks = mir.basic_blocks();
let mut heads: HashMap<mir::BasicBlock, S> = HashMap::new();
trace!(
"[start] run_backward_interpretation_point_to_point:\n - from final block {:?}, statement {}\n - and state {:?}\n - to initial block {:?}\n - using empty state {:?}",
final_bbi,
final_stmt_index,
final_state,
initial_bbi,
empty_state
);
// Find the final basic blocks
let mut pending_blocks: Vec<mir::BasicBlock> = vec![final_bbi];
// Interpret all the blocks in `pending_blocks`
while !pending_blocks.is_empty() {
let curr_bb = pending_blocks.pop().unwrap();
let bb_data = &basic_blocks[curr_bb];
trace!("curr_bb: {:?}", curr_bb);
// Apply the terminator
let terminator = bb_data.terminator();
let terminator_index = bb_data.statements.len();
let states = {
// HACK: define the state even if only one successor is defined
let default_state = terminator
.successors()
.flat_map(|bb| heads.get(bb))
.next()
.unwrap_or(&empty_state);
HashMap::from_iter(
terminator
.successors()
.map(|bb| (*bb, heads.get(bb).unwrap_or(&default_state))),
)
};
trace!("States before: {:?}", states);
trace!("Apply terminator {:?}", terminator);
let mut curr_state = interpreter.apply_terminator(curr_bb, terminator, states);
trace!("State after: {:?}", curr_state);
if curr_bb == final_bbi && final_stmt_index == terminator_index {
trace!("Final location reached in terminator");
curr_state = final_state.clone();
trace!("State after: {:?}", curr_state);
}
// Apply each statement, from the last
for (stmt_index, stmt) in bb_data.statements.iter().enumerate().rev() {
trace!("State before: {:?}", curr_state);
trace!("Apply statement {:?}", stmt);
interpreter.apply_statement(curr_bb, stmt_index, stmt, &mut curr_state);
trace!("State after: {:?}", curr_state);
if curr_bb == final_bbi && final_stmt_index == stmt_index {
trace!("Final location reached in statement");
curr_state = final_state.clone();
trace!("State after: {:?}", curr_state);
}
}
// Store the state at the beginning of block `curr_bb`
heads.insert(curr_bb, curr_state);
if curr_bb != initial_bbi {
// Put the preceding basic blocks
for &pred_bb in mir.predecessors()[curr_bb].iter() {
// Note: here we don't check that all the successors of `pred_bb` has been visited.
// It's a known limitation, because this is the point-to-point interpretation.
// Use `run_backward_interpretation` if the check is important.
pending_blocks.push(pred_bb);
}
}
}
let result = heads.remove(&initial_bbi);
if result.is_none() {
trace!("heads: {:?}", heads);
}
trace!(
"[end] run_backward_interpretation_point_to_point:\n - from final final block {:?}, statement {}\n - and state {:?}\n - to initial block {:?}\n - using empty state {:?}\n - resulted in state {:?}",
final_bbi,
final_stmt_index,
final_state,
initial_bbi,
empty_state,
result
);
result
}
/// Forward interpreter for a loop-less MIR
pub trait ForwardMirInterpreter<'tcx> {
type State: Sized;
fn initial_state(&self) -> Self::State;
fn apply_statement(&self, stmt: &mir::Statement<'tcx>, state: &mut Self::State);
fn apply_terminator(
&self,
terminator: &mir::Terminator<'tcx>,
state: &Self::State,
) -> (HashMap<mir::BasicBlock, Self::State>, Option<Self::State>);
fn join(&self, states: &[&Self::State]) -> Self::State;
}
#[derive(Clone, Debug)]
pub struct MultiExprBackwardInterpreterState {
exprs: Vec<vir::Expr>,
}
impl Display for MultiExprBackwardInterpreterState {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(
f,
"exprs={}",
self.exprs
.iter()
.map(|e| format!("{},", e))
.collect::<String>()
)
}
}
impl MultiExprBackwardInterpreterState {
pub fn new(exprs: Vec<vir::Expr>) -> Self {
MultiExprBackwardInterpreterState { exprs }
}
pub fn new_single(expr: vir::Expr) -> Self {
MultiExprBackwardInterpreterState { exprs: vec![expr] }
}
pub fn exprs(&self) -> &Vec<vir::Expr> {
&self.exprs
}
pub fn into_expressions(self) -> Vec<vir::Expr> {
self.exprs
}
pub fn substitute_place(&mut self, sub_target: &vir::Expr, replacement: vir::Expr) {
trace!("substitute_place {:?} --> {:?}", sub_target, replacement);
// If `replacement` is a reference, simplify also its dereferentiations
if let vir::Expr::AddrOf(box ref base_replacement, ref _dereferenced_type, ref pos) =
replacement
{
trace!("Substitution of a reference. Simplify its dereferentiations.");
let deref_field = vir::Field::new("val_ref", base_replacement.get_type().clone());
let deref_target = sub_target
.clone()
.field(deref_field.clone())
.set_pos(*pos);
self.substitute_place(&deref_target, base_replacement.clone());
}
for expr in &mut self.exprs {
*expr = expr.clone().replace_place(&sub_target, &replacement);
}
}
pub fn substitute_value(&mut self, exact_target: &vir::Expr, replacement: vir::Expr) {
trace!("substitute_value {:?} --> {:?}", exact_target, replacement);
for expr in &mut self.exprs {
*expr = expr.clone().replace_place(exact_target, &replacement);
}
}
pub fn use_place(&self, sub_target: &vir::Expr) -> bool {
trace!("use_place {:?}", sub_target);
self.exprs.iter().any(|expr| expr.find(sub_target))
}
}