-
Notifications
You must be signed in to change notification settings - Fork 273
/
Copy pathcode_contracts.cpp
491 lines (397 loc) · 13.2 KB
/
code_contracts.cpp
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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
/*******************************************************************\
Module: Verify and use annotated invariants and pre/post-conditions
Author: Michael Tautschnig
Date: February 2016
\*******************************************************************/
/// \file
/// Verify and use annotated invariants and pre/post-conditions
#include "code_contracts.h"
#include <util/expr_util.h>
#include <util/fresh_symbol.h>
#include <util/message.h>
#include <util/replace_symbol.h>
#include <goto-programs/goto_convert.h>
#include <goto-programs/remove_skip.h>
#include <analyses/local_may_alias.h>
#include <linking/static_lifetime_init.h>
#include "loop_utils.h"
class code_contractst
{
public:
code_contractst(
symbol_tablet &_symbol_table,
goto_functionst &_goto_functions,
std::list<std::string> _functions_to_assert_ensures)
: ns(_symbol_table),
symbol_table(_symbol_table),
goto_functions(_goto_functions),
functions_to_assert_ensures(_functions_to_assert_ensures),
temporary_counter(0)
{
if(functions_to_assert_ensures.empty())
mode_assert_ensures = false;
else
mode_assert_ensures = true;
}
void operator()();
protected:
namespacet ns;
symbol_tablet &symbol_table;
goto_functionst &goto_functions;
std::list<std::string> functions_to_assert_ensures;
unsigned temporary_counter;
bool mode_assert_ensures;
std::unordered_set<irep_idt> summarized;
void code_contracts(goto_functionst::goto_functiont &goto_function);
void apply_contract(
goto_programt &goto_program,
goto_programt::targett target);
void
assert_ensures_after_calls(goto_functionst::goto_functiont &goto_function);
void
assert_ensures(goto_programt &goto_program, goto_programt::targett target);
void add_contract_check(
const irep_idt &function,
goto_programt &dest);
const symbolt &new_tmp_symbol(
const typet &type,
const source_locationt &source_location,
const irep_idt &function_id,
const irep_idt &mode);
};
static void check_apply_invariants(
goto_functionst::goto_functiont &goto_function,
const local_may_aliast &local_may_alias,
const goto_programt::targett loop_head,
const loopt &loop)
{
assert(!loop.empty());
// find the last back edge
goto_programt::targett loop_end=loop_head;
for(loopt::const_iterator
it=loop.begin();
it!=loop.end();
++it)
if((*it)->is_goto() &&
(*it)->get_target()==loop_head &&
(*it)->location_number>loop_end->location_number)
loop_end=*it;
// see whether we have an invariant
exprt invariant = static_cast<const exprt &>(
loop_end->get_condition().find(ID_C_spec_loop_invariant));
if(invariant.is_nil())
return;
// change H: loop; E: ...
// to
// H: assert(invariant);
// havoc;
// assume(invariant);
// if(guard) goto E:
// loop;
// assert(invariant);
// assume(false);
// E: ...
// find out what can get changed in the loop
modifiest modifies;
get_modifies(local_may_alias, loop, modifies);
// build the havocking code
goto_programt havoc_code;
// assert the invariant
{
goto_programt::targett a = havoc_code.add(
goto_programt::make_assertion(invariant, loop_head->source_location));
a->source_location.set_comment("Loop invariant violated before entry");
}
// havoc variables being written to
build_havoc_code(loop_head, modifies, havoc_code);
// assume the invariant
havoc_code.add(
goto_programt::make_assumption(invariant, loop_head->source_location));
// non-deterministically skip the loop if it is a do-while loop
if(!loop_head->is_goto())
{
havoc_code.add(goto_programt::make_goto(
loop_end,
side_effect_expr_nondett(bool_typet(), loop_head->source_location)));
}
// Now havoc at the loop head. Use insert_swap to
// preserve jumps to loop head.
goto_function.body.insert_before_swap(loop_head, havoc_code);
// assert the invariant at the end of the loop body
{
goto_programt::instructiont a =
goto_programt::make_assertion(invariant, loop_end->source_location);
a.source_location.set_comment("Loop invariant not preserved");
goto_function.body.insert_before_swap(loop_end, a);
++loop_end;
}
// change the back edge into assume(false) or assume(guard)
loop_end->targets.clear();
loop_end->type=ASSUME;
if(loop_head->is_goto())
loop_end->set_condition(false_exprt());
else
loop_end->set_condition(boolean_negate(loop_end->get_condition()));
}
void code_contractst::apply_contract(
goto_programt &goto_program,
goto_programt::targett target)
{
const code_function_callt &call = target->get_function_call();
// we don't handle function pointers
if(call.function().id()!=ID_symbol)
return;
const irep_idt &function=
to_symbol_expr(call.function()).get_identifier();
const symbolt &f_sym=ns.lookup(function);
const code_typet &type=to_code_type(f_sym.type);
exprt requires=
static_cast<const exprt&>(type.find(ID_C_spec_requires));
exprt ensures=
static_cast<const exprt&>(type.find(ID_C_spec_ensures));
// is there a contract?
if(ensures.is_nil())
return;
// replace formal parameters by arguments, replace return
replace_symbolt replace;
// TODO: return value could be nil
if(type.return_type()!=empty_typet())
{
symbol_exprt ret_val(CPROVER_PREFIX "return_value", call.lhs().type());
replace.insert(ret_val, call.lhs());
}
// formal parameters
code_function_callt::argumentst::const_iterator a_it=
call.arguments().begin();
for(code_typet::parameterst::const_iterator
p_it=type.parameters().begin();
p_it!=type.parameters().end() &&
a_it!=call.arguments().end();
++p_it, ++a_it)
if(!p_it->get_identifier().empty())
{
symbol_exprt p(p_it->get_identifier(), p_it->type());
replace.insert(p, *a_it);
}
replace(requires);
replace(ensures);
if(requires.is_not_nil())
{
goto_programt::instructiont a =
goto_programt::make_assertion(requires, target->source_location);
goto_program.insert_before_swap(target, a);
++target;
}
// overwrite the function call
*target = goto_programt::make_assumption(ensures, target->source_location);
summarized.insert(function);
}
void code_contractst::code_contracts(
goto_functionst::goto_functiont &goto_function)
{
local_may_aliast local_may_alias(goto_function);
natural_loops_mutablet natural_loops(goto_function.body);
// iterate over the (natural) loops in the function
for(natural_loops_mutablet::loop_mapt::const_iterator
l_it=natural_loops.loop_map.begin();
l_it!=natural_loops.loop_map.end();
l_it++)
check_apply_invariants(
goto_function,
local_may_alias,
l_it->first,
l_it->second);
// look at all function calls
Forall_goto_program_instructions(it, goto_function.body)
if(it->is_function_call())
apply_contract(goto_function.body, it);
}
void code_contractst::assert_ensures(
goto_programt &goto_program,
goto_programt::targett target)
{
const code_function_callt &call = target->get_function_call();
// we don't handle function pointers
if(call.function().id() != ID_symbol)
return;
const irep_idt &function = to_symbol_expr(call.function()).get_identifier();
const symbolt &f_sym = ns.lookup(function);
const code_typet &type = to_code_type(f_sym.type);
exprt ensures = static_cast<const exprt &>(type.find(ID_C_spec_ensures));
// is there a contract?
if(ensures.is_nil())
return;
// replace formal parameters by arguments, replace return
replace_symbolt replace;
// TODO: return value could be nil
if(type.return_type() != empty_typet())
{
symbol_exprt ret_val(CPROVER_PREFIX "return_value", call.lhs().type());
replace.insert(ret_val, call.lhs());
}
// formal parameters
code_function_callt::argumentst::const_iterator a_it =
call.arguments().begin();
for(code_typet::parameterst::const_iterator p_it = type.parameters().begin();
p_it != type.parameters().end() && a_it != call.arguments().end();
++p_it, ++a_it)
if(!p_it->get_identifier().empty())
{
symbol_exprt p(p_it->get_identifier(), p_it->type());
replace.insert(p, *a_it);
}
replace(ensures);
// assert the ensures part of the contract after the call
++target;
code_assertt a(ensures);
goto_programt new_goto_program;
null_message_handlert message_handler;
// We first need to convert the expression to a goto_program
goto_convert(a, symbol_table, new_goto_program, message_handler, ID_C);
goto_program.insert_before_swap(target, new_goto_program);
}
void code_contractst::assert_ensures_after_calls(
goto_functionst::goto_functiont &goto_function)
{
// look at all function calls
Forall_goto_program_instructions(it, goto_function.body)
if(it->is_function_call())
assert_ensures(goto_function.body, it);
}
const symbolt &code_contractst::new_tmp_symbol(
const typet &type,
const source_locationt &source_location,
const irep_idt &function_id,
const irep_idt &mode)
{
return get_fresh_aux_symbol(
type,
id2string(function_id) + "::tmp_cc",
"tmp_cc",
source_location,
mode,
symbol_table);
}
void code_contractst::add_contract_check(
const irep_idt &function,
goto_programt &dest)
{
assert(!dest.instructions.empty());
goto_functionst::function_mapt::iterator f_it=
goto_functions.function_map.find(function);
assert(f_it!=goto_functions.function_map.end());
const goto_functionst::goto_functiont &gf=f_it->second;
const exprt &requires=
static_cast<const exprt&>(gf.type.find(ID_C_spec_requires));
const exprt &ensures=
static_cast<const exprt&>(gf.type.find(ID_C_spec_ensures));
assert(ensures.is_not_nil());
// build:
// if(nondet)
// decl ret
// decl parameter1 ...
// assume(requires) [optional]
// ret=function(parameter1, ...)
// assert(ensures)
// assume(false)
// skip: ...
// build skip so that if(nondet) can refer to it
goto_programt tmp_skip;
goto_programt::targett skip =
tmp_skip.add(goto_programt::make_skip(ensures.source_location()));
goto_programt check;
// if(nondet)
check.add(goto_programt::make_goto(
skip,
side_effect_expr_nondett(bool_typet(), skip->source_location),
skip->source_location));
// prepare function call including all declarations
const symbolt &function_symbol = ns.lookup(function);
code_function_callt call(function_symbol.symbol_expr());
replace_symbolt replace;
// decl ret
if(gf.type.return_type()!=empty_typet())
{
symbol_exprt r = new_tmp_symbol(
gf.type.return_type(),
skip->source_location,
function,
function_symbol.mode)
.symbol_expr();
check.add(goto_programt::make_decl(r, skip->source_location));
call.lhs()=r;
symbol_exprt ret_val(CPROVER_PREFIX "return_value", call.lhs().type());
replace.insert(ret_val, r);
}
// decl parameter1 ...
for(const auto ¶meter : gf.parameter_identifiers)
{
PRECONDITION(!parameter.empty());
const symbolt ¶meter_symbol = ns.lookup(parameter);
symbol_exprt p = new_tmp_symbol(
parameter_symbol.type,
skip->source_location,
function,
parameter_symbol.mode)
.symbol_expr();
check.add(goto_programt::make_decl(p, skip->source_location));
call.arguments().push_back(p);
replace.insert(parameter_symbol.symbol_expr(), p);
}
// assume(requires)
if(requires.is_not_nil())
{
// rewrite any use of parameters
exprt requires_cond = requires;
replace(requires_cond);
check.add(goto_programt::make_assumption(
requires_cond, requires.source_location()));
}
// ret=function(parameter1, ...)
check.add(goto_programt::make_function_call(call, skip->source_location));
// rewrite any use of __CPROVER_return_value
exprt ensures_cond = ensures;
replace(ensures_cond);
// assert(ensures)
check.add(
goto_programt::make_assertion(ensures_cond, ensures.source_location()));
// assume(false)
check.add(
goto_programt::make_assumption(false_exprt(), ensures.source_location()));
// prepend the new code to dest
check.destructive_append(tmp_skip);
dest.destructive_insert(dest.instructions.begin(), check);
}
void code_contractst::operator()()
{
if(mode_assert_ensures)
{
Forall_goto_functions(it, goto_functions)
assert_ensures_after_calls(it->second);
}
else
{
Forall_goto_functions(it, goto_functions)
code_contracts(it->second);
goto_functionst::function_mapt::iterator i_it =
goto_functions.function_map.find(INITIALIZE_FUNCTION);
INVARIANT(
i_it != goto_functions.function_map.end(),
"There must exist an INITIALIZE_FUNCTION");
for(const auto &contract : summarized)
add_contract_check(contract, i_it->second.body);
// remove skips
remove_skip(i_it->second.body);
}
goto_functions.update();
}
void assert_ensures(goto_modelt &goto_model, std::list<std::string> functions)
{
code_contractst(
goto_model.symbol_table, goto_model.goto_functions, functions)();
}
void code_contracts(goto_modelt &goto_model)
{
std::list<std::string> empty;
code_contractst(goto_model.symbol_table, goto_model.goto_functions, empty)();
}