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
use crate::{
function_data_builder::FunctionDataBuilder,
function_target::FunctionData,
function_target_pipeline::{FunctionTargetProcessor, FunctionTargetsHolder},
options::ProverOptions,
stackless_bytecode::{Bytecode, Operation, PropKind},
};
use move_model::{
ast,
ast::{ConditionKind, Exp, ExpData, QuantKind, TempIndex},
exp_generator::ExpGenerator,
model::{FunctionEnv, Loc, NodeId, StructEnv},
ty::Type,
};
const INVARIANT_FAILS_MESSAGE: &str = "data invariant does not hold";
pub struct DataInvariantInstrumentationProcessor {}
impl DataInvariantInstrumentationProcessor {
pub fn new() -> Box<Self> {
Box::new(Self {})
}
}
impl FunctionTargetProcessor for DataInvariantInstrumentationProcessor {
fn process(
&self,
targets: &mut FunctionTargetsHolder,
fun_env: &FunctionEnv<'_>,
data: FunctionData,
) -> FunctionData {
if fun_env.is_native() || fun_env.is_intrinsic() {
return data;
}
let options = ProverOptions::get(fun_env.module_env.env);
Instrumenter::run(&*options, targets, fun_env, data)
}
fn name(&self) -> String {
"data_invariant_instrumenter".to_string()
}
}
struct Instrumenter<'a> {
_options: &'a ProverOptions,
_targets: &'a mut FunctionTargetsHolder,
builder: FunctionDataBuilder<'a>,
for_verification: bool,
}
impl<'a> Instrumenter<'a> {
fn run(
options: &'a ProverOptions,
targets: &'a mut FunctionTargetsHolder,
fun_env: &FunctionEnv<'a>,
data: FunctionData,
) -> FunctionData {
let for_verification = data.variant.is_verified() || fun_env.has_friend();
let builder = FunctionDataBuilder::new(fun_env, data);
let mut instrumenter = Instrumenter {
_options: options,
_targets: targets,
builder,
for_verification,
};
instrumenter.instrument();
instrumenter.builder.data
}
fn instrument(&mut self) {
let old_code = std::mem::take(&mut self.builder.data.code);
for bc in old_code {
self.instrument_bytecode(bc.clone());
}
}
fn instrument_bytecode(&mut self, bc: Bytecode) {
use Bytecode::*;
use Operation::*;
match bc {
Call(_, _, UnpackRef, ..) | Call(_, _, UnpackRefDeep, ..) => {}
Call(id, dests, Pack(mid, sid, targs), srcs, aa) if self.for_verification => {
let struct_temp = dests[0];
self.builder
.emit(Call(id, dests, Pack(mid, sid, targs), srcs, aa));
self.emit_data_invariant_for_temp(false, PropKind::Assert, struct_temp);
}
Call(_, _, PackRef, srcs, _) if self.for_verification => {
self.emit_data_invariant_for_temp(false, PropKind::Assert, srcs[0]);
}
Call(_, _, PackRefDeep, srcs, _) if self.for_verification => {
self.emit_data_invariant_for_temp(true, PropKind::Assert, srcs[0]);
}
Prop(id, PropKind::Assume, exp) => {
let mut rewriter = |e: Exp| {
if let ExpData::Call(_, ast::Operation::WellFormed, args) = e.as_ref() {
let inv = self.builder.mk_join_bool(
ast::Operation::And,
self.translate_invariant(true, args[0].clone())
.into_iter()
.map(|(_, e)| e),
);
let e = self
.builder
.mk_join_opt_bool(ast::Operation::And, Some(e), inv)
.unwrap();
Ok(e)
} else {
Err(e)
}
};
let exp = ExpData::rewrite(exp, &mut rewriter);
self.builder.emit(Prop(id, PropKind::Assume, exp));
}
_ => self.builder.emit(bc),
}
}
fn emit_data_invariant_for_temp(&mut self, deep: bool, kind: PropKind, temp: TempIndex) {
let temp_exp = self.builder.mk_temporary(temp);
for (loc, inv) in self.translate_invariant(deep, temp_exp) {
self.builder.set_next_debug_comment(format!(
"data invariant {}",
loc.display(self.builder.global_env())
));
if kind == PropKind::Assert {
self.builder
.set_loc_and_vc_info(loc, INVARIANT_FAILS_MESSAGE);
}
self.builder.emit_with(|id| Bytecode::Prop(id, kind, inv));
}
}
fn translate_invariant(&self, deep: bool, value: Exp) -> Vec<(Loc, Exp)> {
let ty = self.builder.global_env().get_node_type(value.node_id());
match ty.skip_reference() {
Type::Struct(mid, sid, targs) => {
let struct_env = self.builder.global_env().get_module(*mid).into_struct(*sid);
self.translate_invariant_for_struct(deep, value, struct_env, targs)
}
Type::Vector(ety) => {
let mut loc = self.builder.global_env().unknown_loc();
let quant =
self.builder
.mk_vector_quant_opt(QuantKind::Forall, value, ety, &mut |elem| {
let invs = self.translate_invariant(deep, elem);
if !invs.is_empty() {
loc = invs[0].0.clone();
}
self.builder
.mk_join_bool(ast::Operation::And, invs.into_iter().map(|(_, e)| e))
});
if let Some(e) = quant {
vec![(loc, e)]
} else {
vec![]
}
}
_ => vec![],
}
}
fn translate_invariant_for_struct(
&self,
deep: bool,
value: Exp,
struct_env: StructEnv<'_>,
targs: &[Type],
) -> Vec<(Loc, Exp)> {
use ast::Operation::*;
use ExpData::*;
let mut result = vec![];
for cond in struct_env
.get_spec()
.filter_kind(ConditionKind::StructInvariant)
{
let exp_rewriter = &mut |e: Exp| match e.as_ref() {
Call(id, oper @ Select(..), args) if args.is_empty() => {
Ok(Call(*id, oper.to_owned(), vec![value.clone()]).into_exp())
}
_ => Err(e),
};
let env = self.builder.global_env();
let node_rewriter = &mut |id: NodeId| ExpData::instantiate_node(env, id, targs);
let exp =
ExpData::rewrite_exp_and_node_id(cond.exp.clone(), exp_rewriter, node_rewriter);
result.push((cond.loc.clone(), exp));
}
if deep {
for field_env in struct_env.get_fields() {
let field_exp = self
.builder
.mk_field_select(&field_env, targs, value.clone());
result.extend(self.translate_invariant(deep, field_exp));
}
}
result
}
}