-
Notifications
You must be signed in to change notification settings - Fork 273
/
Copy pathdfcc.cpp
562 lines (485 loc) · 18.3 KB
/
dfcc.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
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
/*******************************************************************\
Module: Dynamic frame condition checking
Author: Remi Delmas, [email protected]
\*******************************************************************/
#include "dfcc.h"
#include <util/config.h>
#include <util/expr_util.h>
#include <util/format_expr.h>
#include <util/format_type.h>
#include <util/fresh_symbol.h>
#include <util/mathematical_expr.h>
#include <util/mathematical_types.h>
#include <util/namespace.h>
#include <util/pointer_expr.h>
#include <util/pointer_offset_size.h>
#include <util/pointer_predicates.h>
#include <util/prefix.h>
#include <util/std_expr.h>
#include <util/string_utils.h>
#include <goto-programs/goto_functions.h>
#include <goto-programs/goto_inline.h>
#include <goto-programs/goto_model.h>
#include <goto-programs/initialize_goto_model.h>
#include <goto-programs/remove_skip.h>
#include <goto-programs/remove_unused_functions.h>
#include <ansi-c/ansi_c_entry_point.h>
#include <ansi-c/c_expr.h>
#include <ansi-c/c_object_factory_parameters.h>
#include <ansi-c/cprover_library.h>
#include <ansi-c/goto-conversion/goto_convert_functions.h>
#include <ansi-c/goto-conversion/link_to_library.h>
#include <goto-instrument/contracts/cfg_info.h>
#include <goto-instrument/contracts/utils.h>
#include <goto-instrument/nondet_static.h>
#include <langapi/language.h>
#include <langapi/language_file.h>
#include <langapi/mode.h>
#include <linking/static_lifetime_init.h>
#include "dfcc_lift_memory_predicates.h"
invalid_function_contract_pair_exceptiont::
invalid_function_contract_pair_exceptiont(
std::string reason,
std::string correct_format)
: cprover_exception_baset(std::move(reason)),
correct_format(std::move(correct_format))
{
}
std::string invalid_function_contract_pair_exceptiont::what() const
{
std::string res;
res += "Invalid function-contract mapping";
res += "\nReason: " + reason;
if(!correct_format.empty())
{
res += "\nFormat: " + correct_format;
}
return res;
}
static std::pair<irep_idt, irep_idt>
parse_function_contract_pair(const irep_idt &cli_flag)
{
auto const correct_format_message =
"the format for function and contract pairs is "
"`<function_name>[/<contract_name>]`";
std::string cli_flag_str = id2string(cli_flag);
auto split = split_string(cli_flag_str, '/', true, false);
if(split.size() == 1)
{
return std::make_pair(cli_flag, cli_flag);
}
else if(split.size() == 2)
{
auto function_name = split[0];
if(function_name.empty())
{
throw invalid_function_contract_pair_exceptiont{
"couldn't find function name before '/' in '" + cli_flag_str + "'",
correct_format_message};
}
auto contract_name = split[1];
if(contract_name.empty())
{
throw invalid_function_contract_pair_exceptiont{
"couldn't find contract name after '/' in '" + cli_flag_str + "'",
correct_format_message};
}
return std::make_pair(function_name, contract_name);
}
else
{
throw invalid_function_contract_pair_exceptiont{
"couldn't parse '" + cli_flag_str + "'", correct_format_message};
}
}
void dfcc(
const optionst &options,
goto_modelt &goto_model,
const irep_idt &harness_id,
const std::optional<irep_idt> &to_check,
const bool allow_recursive_calls,
const std::set<irep_idt> &to_replace,
const loop_contract_configt loop_contract_config,
const std::set<std::string> &to_exclude_from_nondet_static,
message_handlert &message_handler)
{
std::map<irep_idt, irep_idt> to_replace_map;
for(const auto &cli_flag : to_replace)
to_replace_map.insert(parse_function_contract_pair(cli_flag));
dfcct(
options,
goto_model,
harness_id,
to_check.has_value() ? parse_function_contract_pair(to_check.value())
: std::optional<std::pair<irep_idt, irep_idt>>{},
allow_recursive_calls,
to_replace_map,
loop_contract_config,
message_handler,
to_exclude_from_nondet_static);
}
dfcct::dfcct(
const optionst &options,
goto_modelt &goto_model,
const irep_idt &harness_id,
const std::optional<std::pair<irep_idt, irep_idt>> &to_check,
const bool allow_recursive_calls,
const std::map<irep_idt, irep_idt> &to_replace,
const loop_contract_configt loop_contract_config,
message_handlert &message_handler,
const std::set<std::string> &to_exclude_from_nondet_static)
: options(options),
goto_model(goto_model),
harness_id(harness_id),
to_check(to_check),
allow_recursive_calls(allow_recursive_calls),
to_replace(to_replace),
loop_contract_config(loop_contract_config),
to_exclude_from_nondet_static(to_exclude_from_nondet_static),
message_handler(message_handler),
log(message_handler),
library(goto_model, message_handler),
ns(goto_model.symbol_table),
spec_functions(goto_model, message_handler, library),
contract_clauses_codegen(goto_model, message_handler, library),
instrument(
goto_model,
message_handler,
library,
spec_functions,
contract_clauses_codegen),
memory_predicates(goto_model, library, instrument, message_handler),
contract_handler(
goto_model,
message_handler,
library,
instrument,
memory_predicates,
spec_functions,
contract_clauses_codegen),
swap_and_wrap(
goto_model,
message_handler,
library,
instrument,
spec_functions,
contract_handler),
max_assigns_clause_size(0)
{
transform_goto_model();
}
void dfcct::check_transform_goto_model_preconditions()
{
// check that harness function exists
PRECONDITION_WITH_DIAGNOSTICS(
dfcc_utilst::function_symbol_with_body_exists(goto_model, harness_id),
"Harness function '" + id2string(harness_id) +
"' either not found or has no body");
if(to_check.has_value())
{
auto pair = to_check.value();
PRECONDITION_WITH_DIAGNOSTICS(
dfcc_utilst::function_symbol_with_body_exists(goto_model, pair.first),
"Function to check '" + id2string(pair.first) +
"' either not found or has no body");
// triggers signature compatibility checking
contract_handler.get_pure_contract_symbol(pair.second, pair.first);
PRECONDITION_WITH_DIAGNOSTICS(
pair.first != harness_id,
"Function '" + id2string(pair.first) +
"' cannot be both be checked against a contract and be the harness");
PRECONDITION_WITH_DIAGNOSTICS(
pair.second != harness_id,
"Function '" + id2string(pair.first) +
"' cannot be both the contract to check and be the harness");
PRECONDITION_WITH_DIAGNOSTICS(
to_replace.find(pair.first) == to_replace.end(),
"Function '" + id2string(pair.first) +
"' cannot be both checked against contract and replaced by a contract");
PRECONDITION_WITH_DIAGNOSTICS(
!instrument.do_not_instrument(pair.first),
"CPROVER function or builtin '" + id2string(pair.first) +
"' cannot be checked against a contract");
}
for(const auto &pair : to_replace)
{
// for functions to replace with contracts we don't require the replaced
// function to have a body because only the contract is actually used
PRECONDITION_WITH_DIAGNOSTICS(
dfcc_utilst::function_symbol_exists(goto_model, pair.first),
"Function to replace '" + id2string(pair.first) + "' not found");
// triggers signature compatibility checking
contract_handler.get_pure_contract_symbol(pair.second, pair.first);
PRECONDITION_WITH_DIAGNOSTICS(
pair.first != harness_id,
"Function '" + id2string(pair.first) +
"' cannot both be replaced with a contract and be the harness");
PRECONDITION_WITH_DIAGNOSTICS(
pair.second != harness_id,
"Function '" + id2string(pair.first) +
"' cannot both be the contract to use for replacement and be the "
"harness");
}
}
void dfcct::partition_function_symbols(
std::set<irep_idt> &contract_symbols,
std::set<irep_idt> &other_symbols)
{
std::set<irep_idt> called_functions;
find_used_functions(
goto_functionst::entry_point(),
goto_model.goto_functions,
called_functions);
// collect contract and other symbols
for(auto &entry : goto_model.symbol_table)
{
const symbolt &symbol = entry.second;
// not a function symbol
if(symbol.type.id() != ID_code || symbol.is_macro)
continue;
// is it a pure contract ?
const irep_idt &sym_name = symbol.name;
if(symbol.is_property && has_prefix(id2string(sym_name), "contract::"))
{
contract_symbols.insert(sym_name);
}
else if(called_functions.find(sym_name) != called_functions.end())
{
// it is not a contract
other_symbols.insert(sym_name);
}
}
}
void dfcct::link_model_and_load_dfcc_library()
{
// load the cprover library to make sure the model is complete
log.status() << "Loading CPROVER C library (" << config.ansi_c.arch << ")"
<< messaget::eom;
link_to_library(
goto_model, log.get_message_handler(), cprover_c_library_factory);
// this must be done before loading the dfcc lib
partition_function_symbols(pure_contract_symbols, other_symbols);
// load the dfcc library before instrumentation starts
library.load(other_symbols);
// disable checks on all library functions
library.disable_checks();
// add C prover lib again to fetch any dependencies of the dfcc functions
link_to_library(
goto_model, log.get_message_handler(), cprover_c_library_factory);
}
void dfcct::instrument_harness_function()
{
// instrument the harness function
// load the cprover library to make sure the model is complete
log.status() << "Instrumenting harness function '" << harness_id << "'"
<< messaget::eom;
instrument.instrument_harness_function(
harness_id, loop_contract_config, function_pointer_contracts);
other_symbols.erase(harness_id);
}
void dfcct::lift_memory_predicates()
{
std::set<irep_idt> predicates =
memory_predicates.lift_predicates(function_pointer_contracts);
for(const auto &predicate : predicates)
{
log.debug() << "Memory predicate" << predicate << messaget::eom;
if(other_symbols.find(predicate) != other_symbols.end())
other_symbols.erase(predicate);
}
}
void dfcct::wrap_checked_function()
{
// swap-and-wrap checked functions with contracts
if(to_check.has_value())
{
const auto &pair = to_check.value();
const auto &wrapper_id = pair.first;
const auto &contract_id = pair.second;
log.status() << "Wrapping '" << wrapper_id << "' with contract '"
<< contract_id << "' in CHECK mode" << messaget::eom;
swap_and_wrap.swap_and_wrap_check(
loop_contract_config,
wrapper_id,
contract_id,
function_pointer_contracts,
allow_recursive_calls);
if(other_symbols.find(wrapper_id) != other_symbols.end())
other_symbols.erase(wrapper_id);
// update max contract size
const std::size_t assigns_clause_size =
contract_handler.get_assigns_clause_size(contract_id);
if(assigns_clause_size > max_assigns_clause_size)
max_assigns_clause_size = assigns_clause_size;
}
}
void dfcct::wrap_replaced_functions()
{
// swap-and-wrap replaced functions with contracts
for(const auto &pair : to_replace)
{
const auto &wrapper_id = pair.first;
const auto &contract_id = pair.second;
log.status() << "Wrapping '" << wrapper_id << "' with contract '"
<< contract_id << "' in REPLACE mode" << messaget::eom;
swap_and_wrap.swap_and_wrap_replace(
wrapper_id, contract_id, function_pointer_contracts);
if(other_symbols.find(wrapper_id) != other_symbols.end())
other_symbols.erase(wrapper_id);
}
}
void dfcct::wrap_discovered_function_pointer_contracts()
{
std::set<irep_idt> swapped;
while(!function_pointer_contracts.empty())
{
std::set<irep_idt> new_contracts;
// swap-and-wrap function pointer contracts with themselves
for(const auto &fp_contract : function_pointer_contracts)
{
if(swapped.find(fp_contract) != swapped.end())
continue;
// contracts for function pointers must be replaced with themselves
// so we need to check that:
// - the symbol exists as a function symbol
// - the symbol exists as a pure contract symbol
// - the function symbol is not already swapped for contract checking
// - the function symbol is not already swapped with another contract for
// replacement
const auto str = id2string(fp_contract);
// Is it already swapped with another function for contract checking ?
PRECONDITION_WITH_DIAGNOSTICS(
!to_check.has_value() || to_check.value().first != str,
"Function '" + str +
"' used as contract for function pointer cannot be itself the object "
"of a contract check.");
// Is it already swapped with another function for contract checking ?
auto found = to_replace.find(str);
if(found != to_replace.end())
{
PRECONDITION_WITH_DIAGNOSTICS(
found->first == found->second,
"Function '" + str +
"' used as contract for function pointer already the object of a "
"contract replacement with '" +
id2string(found->second) + "'");
log.status() << "Function pointer contract '" << fp_contract
<< "' already wrapped with itself in REPLACE mode"
<< messaget::eom;
}
else
{
// we need to swap it with itself
PRECONDITION_WITH_DIAGNOSTICS(
dfcc_utilst::function_symbol_exists(goto_model, str),
"Function pointer contract '" + str + "' not found.");
// triggers signature compatibility checking
contract_handler.get_pure_contract_symbol(str);
log.status() << "Wrapping function pointer contract '" << fp_contract
<< "' with itself in REPLACE mode" << messaget::eom;
swap_and_wrap.swap_and_wrap_replace(
fp_contract, fp_contract, new_contracts);
swapped.insert(fp_contract);
// remove it from the set of symbols to process
if(other_symbols.find(fp_contract) != other_symbols.end())
other_symbols.erase(fp_contract);
}
}
// process newly discovered contracts
function_pointer_contracts = new_contracts;
}
}
void dfcct::instrument_other_functions()
{
// instrument all other remaining functions
for(const auto &function_id : other_symbols)
{
// Don't instrument CPROVER and internal functions
if(instrument.do_not_instrument(function_id))
{
continue;
}
log.status() << "Instrumenting '" << function_id << "'" << messaget::eom;
instrument.instrument_function(
function_id, loop_contract_config, function_pointer_contracts);
}
goto_model.goto_functions.update();
}
void dfcct::transform_goto_model()
{
check_transform_goto_model_preconditions();
link_model_and_load_dfcc_library();
lift_memory_predicates();
instrument_harness_function();
wrap_checked_function();
wrap_replaced_functions();
wrap_discovered_function_pointer_contracts();
instrument_other_functions();
// take the max of function of loop contracts assigns clauses
auto assigns_clause_size = instrument.get_max_assigns_clause_size();
if(assigns_clause_size > max_assigns_clause_size)
max_assigns_clause_size = assigns_clause_size;
log.status() << "Specializing cprover_contracts functions for assigns "
"clauses of at most "
<< max_assigns_clause_size << " targets" << messaget::eom;
library.specialize(max_assigns_clause_size);
library.inhibit_front_end_builtins();
// TODO implement a means to inhibit unreachable functions (possibly via the
// code that implements drop-unused-functions followed by
// generate-function-bodies):
// Traverse the call tree from the given entry point to identify
// functions symbols that are effectively called in the model,
// Then goes over all functions of the model and turns the bodies of all
// functions that are not in the used function set into:
// ```c
// assert(false, "function identified as unreachable");
// assume(false);
// ```
// That way, if the analysis mistakenly pruned some functions, assertions
// will be violated and the analysis will fail.
// TODO: add a command line flag to tell the instrumentation to not prune
// a function.
goto_model.goto_functions.update();
remove_skip(goto_model);
goto_model.goto_functions.update();
log.status() << "Removing unused functions" << messaget::eom;
// This can prune too many functions if function pointers have not been
// yet been removed or if the entry point is not defined.
// Another solution would be to rewrite the bodies of functions that seem to
// be unreachable into assert(false);assume(false)
remove_unused_functions(goto_model, message_handler);
goto_model.goto_functions.update();
reinitialize_model();
}
void dfcct::reinitialize_model()
{
// collect set of functions which are now instrumented
std::set<irep_idt> instrumented_functions;
instrument.get_instrumented_functions(instrumented_functions);
swap_and_wrap.get_swapped_functions(instrumented_functions);
log.status() << "Updating init function" << messaget::eom;
if(goto_model.can_produce_function(INITIALIZE_FUNCTION))
recreate_initialize_function(goto_model, message_handler);
nondet_static(goto_model, to_exclude_from_nondet_static);
// Initialize the map of instrumented functions by adding extra instructions
// to the harness function
auto &init_function = goto_model.goto_functions.function_map[harness_id];
auto &body = init_function.body;
auto begin = body.instructions.begin();
goto_programt payload;
library.add_instrumented_functions_map_init_instructions(
instrumented_functions, begin->source_location(), payload);
body.destructive_insert(begin, payload);
// Define harness as the entry point, overriding any preexisting one.
log.status() << "Setting entry point to " << harness_id << messaget::eom;
// remove the CPROVER start function
goto_model.symbol_table.erase(
goto_model.symbol_table.symbols.find(goto_functionst::entry_point()));
// regenerate the CPROVER start function
generate_ansi_c_start_function(
dfcc_utilst::get_function_symbol(goto_model.symbol_table, harness_id),
goto_model.symbol_table,
message_handler,
c_object_factory_parameterst(options));
goto_model.goto_functions.update();
}