cprover
Loading...
Searching...
No Matches
dfcc_instrument_loop.cpp
Go to the documentation of this file.
1/*******************************************************************\
2
3Module: Dynamic frame condition checking for loop contracts
4
5Author: Qinheping Hu, qinhh@amazon.com
6
7Author: Remi Delmas, delmasrd@amazon.com
8
9Date: April 2023
10
11\*******************************************************************/
12
15
16#include <util/format_expr.h>
17#include <util/fresh_symbol.h>
18
20
21#include "dfcc_cfg_info.h"
23#include "dfcc_instrument.h"
24#include "dfcc_loop_tags.h"
25#include "dfcc_spec_functions.h"
26
28 goto_modelt &goto_model,
29 message_handlert &message_handler,
30 dfcc_libraryt &library,
31 dfcc_spec_functionst &spec_functions,
32 dfcc_contract_clauses_codegent &contract_clauses_codegen)
33 : goto_model(goto_model),
34 log(message_handler),
35 library(library),
36 spec_functions(spec_functions),
37 contract_clauses_codegen(contract_clauses_codegen),
38 ns(goto_model.symbol_table)
39{
40}
41
43 const std::size_t loop_id,
44 const irep_idt &function_id,
45 goto_functiont &goto_function,
46 dfcc_cfg_infot &cfg_info,
47 const std::set<symbol_exprt> &local_statics,
48 std::set<irep_idt> &function_pointer_contracts)
49{
50 const dfcc_loop_infot &loop = cfg_info.get_loop_info(loop_id);
51 const std::size_t cbmc_loop_id = loop.cbmc_loop_id;
52 const exprt &outer_write_set = cfg_info.get_outer_write_set(loop_id);
53 goto_programt::targett head = loop.find_head(goto_function.body).value();
54 auto head_location(head->source_location());
55
56 auto &symbol_table = goto_model.symbol_table;
57
58 // Temporary variables:
59 // Create a temporary to track if we entered the loop,
60 // i.e., the loop guard was satisfied.
61 const auto entered_loop = dfcc_utilst::create_symbol(
62 symbol_table,
63 bool_typet(),
64 function_id,
65 std::string(ENTERED_LOOP) + "__" + std::to_string(cbmc_loop_id),
66 head_location);
67
68 // Create a snapshot of the invariant so that we can check the base case,
69 // if the loop is not vacuous and must be abstracted with contracts.
70 const auto initial_invariant = dfcc_utilst::create_symbol(
71 symbol_table, bool_typet(), function_id, INIT_INVARIANT, head_location);
72
73 // Create a temporary variable to track base case vs inductive case
74 // instrumentation of the loop.
75 const auto in_base_case = dfcc_utilst::create_symbol(
76 symbol_table, bool_typet(), function_id, IN_BASE_CASE, head_location);
77
78 // Temporary variables for storing the multidimensional decreases clause
79 // at the start of and end of a loop body.
80 exprt::operandst decreases_clauses = loop.decreases;
81 std::vector<symbol_exprt> old_decreases_vars, new_decreases_vars;
82 for(const auto &clause : decreases_clauses)
83 {
84 // old
85 const auto old_decreases_var = dfcc_utilst::create_symbol(
86 symbol_table, clause.type(), function_id, "tmp_cc", head_location);
87 old_decreases_vars.push_back(old_decreases_var);
88 // new
89 const auto new_decreases_var = dfcc_utilst::create_symbol(
90 symbol_table, clause.type(), function_id, "tmp_cc", head_location);
91 new_decreases_vars.push_back(new_decreases_var);
92 }
93
94 // Convert the assigns clause to the required type.
95 exprt::operandst assigns(loop.assigns.begin(), loop.assigns.end());
96
97 // Add local statics to the assigns clause.
98 for(auto &local_static : local_statics)
99 {
100 assigns.push_back(local_static);
101 }
102
103 auto nof_targets = assigns.size();
104 max_assigns_clause_size = std::max(nof_targets, max_assigns_clause_size);
105
106 // populate(w_loop, <loop_assigns>);
107 // Construct the write set from loop assigns target. That is, contract_assigns
108 // in the result __CPROVER_contracts_write_set_t should be the set of CAR
109 // of the loop assign targets.
110 goto_programt write_set_populate_instrs;
111 const irep_idt &language_mode =
112 dfcc_utilst::get_function_symbol(symbol_table, function_id).mode;
114 language_mode, assigns, write_set_populate_instrs);
115
116 // havoc(w_loop);
117 // The generated GOTO instructions havoc the write set of the loop.
118 goto_programt havoc_instrs;
119
121 function_id,
122 write_set_populate_instrs,
124 havoc_instrs,
125 nof_targets);
128 language_mode,
129 write_set_populate_instrs,
130 nof_targets);
131
132 // ---------- Add instrumented instructions ----------
133 goto_programt::targett loop_latch =
134 loop.find_latch(goto_function.body).value();
135 exprt invariant(loop.invariant);
136 const auto history_var_map = add_prehead_instructions(
137 loop_id,
138 goto_function,
139 symbol_table,
140 head,
141 loop_latch,
142 write_set_populate_instrs,
143 invariant,
144 assigns,
145 loop.write_set_var,
147 entered_loop,
148 initial_invariant,
149 in_base_case,
150 language_mode);
151
152 const auto step_target = add_step_instructions(
153 loop_id,
154 cbmc_loop_id,
155 function_id,
156 goto_function,
157 symbol_table,
158 head,
159 loop_latch,
160 havoc_instrs,
161 invariant,
162 decreases_clauses,
164 outer_write_set,
165 initial_invariant,
166 in_base_case,
167 old_decreases_vars);
168
170 loop_id,
171 cbmc_loop_id,
172 goto_function,
173 symbol_table,
174 head,
175 loop_latch,
176 invariant,
177 decreases_clauses,
178 entered_loop,
179 in_base_case,
180 old_decreases_vars,
181 new_decreases_vars,
182 step_target,
183 language_mode);
184
186 loop_id,
187 cbmc_loop_id,
188 goto_function,
189 head,
190 loop.write_set_var,
192 history_var_map,
193 entered_loop,
194 initial_invariant,
195 in_base_case,
196 old_decreases_vars,
197 new_decreases_vars);
198
199 goto_function.body.update();
200}
201
202std::unordered_map<exprt, symbol_exprt, irep_hash>
204 const std::size_t loop_id,
205 goto_functionst::goto_functiont &goto_function,
206 symbol_table_baset &symbol_table,
207 goto_programt::targett loop_head,
208 goto_programt::targett loop_latch,
209 goto_programt &assigns_instrs,
210 exprt &invariant,
211 const exprt::operandst &assigns,
212 const symbol_exprt &loop_write_set,
213 const symbol_exprt &addr_of_loop_write_set,
214 const symbol_exprt &entered_loop,
215 const symbol_exprt &initial_invariant,
216 const symbol_exprt &in_base_case,
217 const irep_idt &language_mode)
218{
219 auto loop_head_location(loop_head->source_location());
220 dfcc_remove_loop_tags(loop_head_location);
221
222 // ```
223 // ... preamble ...
224 //
225 // // Prehead block: Declare & initialize instrumentation variables
226 // snapshot loop_entry history vars;
227 // entered_loop = false
228 // initial_invariant = loop_invariant;
229 // in_base_case = true;
230 // __ws_loop;
231 // ws_loop := address_of(__ws_loop);
232 // __write_set_create(ws_loop);
233 // __write_set_add(ws_loop, loop_assigns);
234 // __write_set_add(ws_loop, local_statics);
235 // GOTO HEAD;
236 // ```
237
238 // Replace bound variables by fresh instances in quantified formulas.
239 if(has_subexpr(invariant, ID_exists) || has_subexpr(invariant, ID_forall))
240 add_quantified_variable(symbol_table, invariant, language_mode);
241 // initialize loop_entry history vars;
242 auto replace_history_result = replace_history_loop_entry(
243 symbol_table, invariant, loop_head_location, language_mode);
244 invariant.swap(replace_history_result.expression_after_replacement);
245 goto_programt &pre_loop_head_instrs =
246 replace_history_result.history_construction;
247
248 // entered_loop = false
249 {
250 pre_loop_head_instrs.add(
251 goto_programt::make_decl(entered_loop, loop_head_location));
252 pre_loop_head_instrs.add(goto_programt::make_assignment(
253 entered_loop, false_exprt{}, loop_head_location));
254 }
255
256 // initial_invariant = <loop_invariant>;
257 {
258 // Create a snapshot of the invariant so that we can check the base case,
259 // if the loop is not vacuous and must be abstracted with contracts.
260 pre_loop_head_instrs.add(
261 goto_programt::make_decl(initial_invariant, loop_head_location));
262
263 // Although the invariant at this point will not have side effects,
264 // it is still a C expression, and needs to be "goto_convert"ed.
265 // Note that this conversion may emit many GOTO instructions.
266 code_frontend_assignt initial_invariant_assignment{
267 initial_invariant, invariant, loop_head_location};
268 goto_convertt converter(symbol_table, log.get_message_handler());
269 converter.goto_convert(
270 initial_invariant_assignment, pre_loop_head_instrs, language_mode);
271 }
272
273 {
274 // Create a temporary variable to track base case vs inductive case
275 // instrumentation of the loop.
276 // in_base_case = true;
277 pre_loop_head_instrs.add(
278 goto_programt::make_decl(in_base_case, loop_head_location));
279 pre_loop_head_instrs.add(goto_programt::make_assignment(
280 in_base_case, true_exprt{}, loop_head_location));
281 }
282
283 {
284 // Create and populate the write set.
285 // DECL loop_write_set
286 // DECL addr_of_loop_write_set
287 // ASSIGN write_set_ptr := address_of(write_set)
288 // CALL __CPROVER_contracts_write_set_create(write_set_ptr,
289 // contracts_assigns_size, contracts_assigns_frees_size,
290 // assume_require_ctx, assert_require_ctx, assume_ensures_ctx,
291 // assert_ensures_ctx, allow_allocate, allow_deallocate);
292 pre_loop_head_instrs.add(
293 goto_programt::make_decl(loop_write_set, loop_head_location));
294 pre_loop_head_instrs.add(
295 goto_programt::make_decl(addr_of_loop_write_set, loop_head_location));
296 pre_loop_head_instrs.add(goto_programt::make_assignment(
297 addr_of_loop_write_set,
298 address_of_exprt(loop_write_set),
299 loop_head_location));
300
302 addr_of_loop_write_set,
303 from_integer(assigns.size(), size_type()),
304 loop_head_location);
305 pre_loop_head_instrs.add(
306 goto_programt::make_function_call(call, loop_head_location));
307
308 pre_loop_head_instrs.destructive_append(assigns_instrs);
309 }
310
311 // goto HEAD;
312 pre_loop_head_instrs.add(
313 goto_programt::make_goto(loop_head, true_exprt{}, loop_head_location));
314
315 goto_function.body.destructive_insert(loop_head, pre_loop_head_instrs);
316 return replace_history_result.parameter_to_history;
317}
318
321 const std::size_t loop_id,
322 const std::size_t cbmc_loop_id,
323 const irep_idt &function_id,
324 goto_functionst::goto_functiont &goto_function,
325 symbol_table_baset &symbol_table,
326 goto_programt::targett loop_head,
327 goto_programt::targett loop_latch,
328 goto_programt &havoc_instrs,
329 exprt &invariant,
330 const exprt::operandst &decreases_clauses,
331 const symbol_exprt &addr_of_loop_write_set,
332 const exprt &outer_write_set,
333 const symbol_exprt &initial_invariant,
334 const symbol_exprt &in_base_case,
335 const std::vector<symbol_exprt> &old_decreases_vars)
336{
337 auto loop_head_location(loop_head->source_location());
338 dfcc_remove_loop_tags(loop_head_location);
339
340 // ```
341 // STEP: // Loop step block: havoc the loop state
342 // ASSERT(initial_invariant);
343 // __write_set_check_inclusion(ws_loop, ws_parent);
344 // in_base_case = false;
345 // in_loop_havoc_block = true;
346 // havoc(assigns_clause_targets);
347 // in_loop_havoc_block = false;
348 // ASSUME(loop_invariant);
349 // old_variant = loop_decreases;
350 // ```
351
352 goto_programt step_instrs;
353
354 // We skip past it initially, because of the unconditional jump above,
355 // but jump back here if we get past the loop guard while in_base_case.
356 // `in_base_case = false;`
357 goto_programt::instructiont::targett step_case_target =
359 in_base_case, false_exprt{}, loop_head_location));
360
361 {
362 // If we jump here, then the loop runs at least once,
363 // so add the base case assertion: `assert(initial_invariant)`.
364 source_locationt check_location(loop_head_location);
365 check_location.set_property_class("loop_invariant_base");
366 check_location.set_comment(
367 "Check invariant before entry for loop " +
368 id2string(check_location.get_function()) + "." +
369 std::to_string(cbmc_loop_id));
370 step_instrs.add(
371 goto_programt::make_assertion(initial_invariant, check_location));
372 }
373
374 {
375 // Check assigns clause inclusion with parent write set
376 // skip the check when if w_parent is NULL.
377 auto goto_instruction = step_instrs.add(goto_programt::make_incomplete_goto(
379 outer_write_set,
380 null_pointer_exprt(to_pointer_type(outer_write_set.type()))),
381 loop_head_location));
382
383 const auto check_var = dfcc_utilst::create_symbol(
384 symbol_table,
385 bool_typet(),
386 function_id,
387 "__check_assigns_clause_incl_loop_" + std::to_string(cbmc_loop_id),
388 loop_head_location);
389
390 step_instrs.add(goto_programt::make_decl(check_var, loop_head_location));
393 check_var, outer_write_set, addr_of_loop_write_set, loop_head_location),
394 loop_head_location));
395
396 source_locationt check_location(loop_head_location);
397 check_location.set_property_class("loop_assigns");
398 check_location.set_comment(
399 "Check assigns clause inclusion for loop " +
400 id2string(check_location.get_function()) + "." +
401 std::to_string(cbmc_loop_id));
402 step_instrs.add(goto_programt::make_assertion(check_var, check_location));
403 step_instrs.add(goto_programt::make_dead(check_var, loop_head_location));
404
405 auto label_instruction =
406 step_instrs.add(goto_programt::make_skip(loop_head_location));
407 goto_instruction->complete_goto(label_instruction);
408 }
409
410 {
411 // Generate havocing code for assigns targets.
412 const auto in_loop_havoc_block = dfcc_utilst::create_symbol(
413 symbol_table,
414 bool_typet(),
415 function_id,
416 std::string(IN_LOOP_HAVOC_BLOCK) + +"__" + std::to_string(cbmc_loop_id),
417 loop_head_location);
418 step_instrs.add(
419 goto_programt::make_decl(in_loop_havoc_block, loop_head_location));
421 in_loop_havoc_block, true_exprt{}, loop_head_location));
422 step_instrs.destructive_append(havoc_instrs);
424 in_loop_havoc_block, false_exprt{}, loop_head_location));
425 }
426
427 goto_convertt converter(symbol_table, log.get_message_handler());
428 const irep_idt &language_mode =
429 dfcc_utilst::get_function_symbol(symbol_table, function_id).mode;
430 {
431 // Assume the loop invariant after havocing the state.
432 // Replace bound variables by fresh instances in quantified formulas.
433 if(has_subexpr(invariant, ID_exists) || has_subexpr(invariant, ID_forall))
434 add_quantified_variable(symbol_table, invariant, language_mode);
435 code_assumet assumption{invariant};
436 assumption.add_source_location() = loop_head_location;
437 converter.goto_convert(assumption, step_instrs, language_mode);
438 }
439
440 {
441 // Generate assignments to store the multidimensional decreases clause's
442 // value just before the loop_head.
443 for(size_t i = 0; i < old_decreases_vars.size(); i++)
444 {
445 code_frontend_assignt old_decreases_assignment{
446 old_decreases_vars[i], decreases_clauses[i], loop_head_location};
447 converter.goto_convert(
448 old_decreases_assignment, step_instrs, language_mode);
449 }
450 }
451
452 goto_function.body.destructive_insert(loop_head, step_instrs);
453
454 return step_case_target;
455}
456
458 const std::size_t loop_id,
459 const std::size_t cbmc_loop_id,
460 goto_functionst::goto_functiont &goto_function,
461 symbol_table_baset &symbol_table,
462 goto_programt::targett loop_head,
463 goto_programt::targett loop_latch,
464 exprt &invariant,
465 const exprt::operandst &decreases_clauses,
466 const symbol_exprt &entered_loop,
467 const symbol_exprt &in_base_case,
468 const std::vector<symbol_exprt> &old_decreases_vars,
469 const std::vector<symbol_exprt> &new_decreases_vars,
470 const goto_programt::instructiont::targett &step_case_target,
471 const irep_idt &language_mode)
472{
473 auto loop_head_location(loop_head->source_location());
474 dfcc_remove_loop_tags(loop_head_location);
475
476 // HEAD: // Loop body block
477 // ... eval guard ...
478 // IF (!guard) GOTO EXIT;
479 // ... loop body ...
480 // // instrumentation
481 // entered_loop = true
482 // // Jump back to the step case if the loop can run at least once
483 // IF (in_base_case) GOTO STEP;
484 // ASSERT(<loop_invariant>);
485 // new_variant = <loop_decreases>;
486 // ASSERT(new_variant < old_variant);
487 // ASSUME(false);
488
489 goto_programt pre_loop_latch_instrs;
490
491 {
492 // Record that we entered the loop with `entered_loop = true`.
493 pre_loop_latch_instrs.add(goto_programt::make_assignment(
494 entered_loop, true_exprt{}, loop_head_location));
495 }
496
497 {
498 // Jump back to the step case to havoc the write set, assume the invariant,
499 // and execute an arbitrary iteration.
500 pre_loop_latch_instrs.add(goto_programt::make_goto(
501 step_case_target, in_base_case, loop_head_location));
502 }
503
504 goto_convertt converter(symbol_table, log.get_message_handler());
505 {
506 // Because of the unconditional jump above the following code is only
507 // reachable in the step case. Generate the inductive invariant check
508 // `ASSERT(invariant)`.
509 source_locationt check_location(loop_head_location);
510 check_location.set_property_class("loop_invariant_step");
511 check_location.set_comment(
512 "Check invariant after step for loop " +
513 id2string(check_location.get_function()) + "." +
514 std::to_string(cbmc_loop_id));
515 // Assume the loop invariant after havocing the state.
516 // Replace bound variables by fresh instances in quantified formulas.
517 if(has_subexpr(invariant, ID_exists) || has_subexpr(invariant, ID_forall))
518 add_quantified_variable(symbol_table, invariant, language_mode);
519 code_assertt assertion{invariant};
520 assertion.add_source_location() = check_location;
521 converter.goto_convert(assertion, pre_loop_latch_instrs, language_mode);
522 }
523
524 {
525 // Generate assignments to store the multidimensional decreases clause's
526 // value after one iteration of the loop.
527 if(!decreases_clauses.empty())
528 {
529 for(size_t i = 0; i < new_decreases_vars.size(); i++)
530 {
531 code_frontend_assignt new_decreases_assignment{
532 new_decreases_vars[i], decreases_clauses[i], loop_head_location};
533 converter.goto_convert(
534 new_decreases_assignment, pre_loop_latch_instrs, language_mode);
535 }
536
537 // Generate assertion that the multidimensional decreases clause's value
538 // after the loop is lexicographically smaller than its initial value.
539 source_locationt check_location(loop_head_location);
540 check_location.set_property_class("loop_decreases");
541 check_location.set_comment(
542 "Check variant decreases after step for loop " +
543 id2string(check_location.get_function()) + "." +
544 std::to_string(cbmc_loop_id));
545 pre_loop_latch_instrs.add(goto_programt::make_assertion(
547 new_decreases_vars, old_decreases_vars),
548 check_location));
549
550 // Discard the temporary variables that store decreases clause's value.
551 for(size_t i = 0; i < old_decreases_vars.size(); i++)
552 {
553 pre_loop_latch_instrs.add(
554 goto_programt::make_dead(old_decreases_vars[i], loop_head_location));
555 pre_loop_latch_instrs.add(
556 goto_programt::make_dead(new_decreases_vars[i], loop_head_location));
557 }
558 }
559 }
560
562 goto_function.body, loop_latch, pre_loop_latch_instrs);
563
564 {
565 // Change the back edge into assume(false) or assume(!guard).
566 loop_latch->turn_into_assume();
567 loop_latch->condition_nonconst() = boolean_negate(loop_latch->condition());
568 }
569}
570
572 const std::size_t loop_id,
573 const std::size_t cbmc_loop_id,
574 goto_functionst::goto_functiont &goto_function,
575 goto_programt::targett loop_head,
576 const symbol_exprt &loop_write_set,
577 const symbol_exprt &addr_of_loop_write_set,
578 const std::unordered_map<exprt, symbol_exprt, irep_hash> &history_var_map,
579 const symbol_exprt &entered_loop,
580 const symbol_exprt &initial_invariant,
581 const symbol_exprt &in_base_case,
582 const std::vector<symbol_exprt> &old_decreases_vars,
583 const std::vector<symbol_exprt> &new_decreases_vars)
584{
585 // Collect all exit targets of the loop.
586 std::set<goto_programt::targett, goto_programt::target_less_than>
587 exit_targets;
588
590 goto_function.body.instructions.begin();
591 target != goto_function.body.instructions.end();
592 target++)
593 {
594 if(!dfcc_is_loop_exiting(target) || !dfcc_has_loop_id(target, loop_id))
595 continue;
596 INVARIANT(target->is_goto(), "Exiting instructions must be GOTOs");
597 auto exit_target = target->get_target();
598 auto exit_loop_id_opt = dfcc_get_loop_id(exit_target);
599 INVARIANT(
600 exit_loop_id_opt.has_value() && exit_loop_id_opt.value() != loop_id,
601 "Exiting instructions must jump out of the loop");
602 exit_targets.insert(exit_target);
603 }
604
605 // For each exit target of the loop, insert a code block:
606 // ```
607 // EXIT:
608 // // check that step case was checked if loop can run once
609 // ASSUME (entered_loop ==> !in_base_case);
610 // DEAD loop_entry history vars, in_base_case;
611 // DEAD initial_invariant, entered_loop;
612 // DEAD old_variant, in_loop_havoc_block;
613 // __write_set_release(w_loop);
614 // DEAD __ws_loop, ws_loop;
615 // ```
616
617 for(auto exit_target : exit_targets)
618 {
619 goto_programt loop_exit_program;
620
621 // Use the head location for this check as well so that all checks related
622 // to a given loop are presented as coming from the loop head.
623 source_locationt check_location = loop_head->source_location();
624 check_location.set_property_class("loop_step_unwinding");
625 check_location.set_comment(
626 "Check step was unwound for loop " +
627 id2string(check_location.get_function()) + "." +
628 std::to_string(cbmc_loop_id));
629 loop_exit_program.add(goto_programt::make_assertion(
630 or_exprt{not_exprt{entered_loop}, not_exprt{in_base_case}},
631 check_location));
632
633 // Mark instrumentation variables as going out of scope.
634 const source_locationt &exit_location = exit_target->source_location();
635 loop_exit_program.add(
636 goto_programt::make_dead(in_base_case, exit_location));
637 loop_exit_program.add(
638 goto_programt::make_dead(entered_loop, exit_location));
639 loop_exit_program.add(
640 goto_programt::make_dead(initial_invariant, exit_location));
641
642 // Release the write set resources.
643 loop_exit_program.add(goto_programt::make_function_call(
644 library.write_set_release_call(addr_of_loop_write_set, exit_location),
645 exit_location));
646
647 // Mark write set as going out of scope.
648 loop_exit_program.add(
649 goto_programt::make_dead(to_symbol_expr(loop_write_set), exit_location));
650 loop_exit_program.add(goto_programt::make_dead(
651 to_symbol_expr(addr_of_loop_write_set), exit_location));
652
653 // Mark history variables as going out of scope.
654 for(const auto &v : history_var_map)
655 loop_exit_program.add(goto_programt::make_dead(v.second, exit_location));
656
657 // Mark decreases clause snapshots as gong out of scope.
658 for(size_t i = 0; i < old_decreases_vars.size(); i++)
659 {
660 loop_exit_program.add(
661 goto_programt::make_dead(old_decreases_vars[i], exit_location));
662 loop_exit_program.add(
663 goto_programt::make_dead(new_decreases_vars[i], exit_location));
664 }
665
666 // Insert the exit block, preserving the loop end target.
668 goto_function.body, exit_target, loop_exit_program);
669 }
670}
constant_exprt from_integer(const mp_integer &int_value, const typet &type)
unsignedbv_typet size_type()
Definition c_types.cpp:50
Operator to return the address of an object.
The Boolean type.
Definition std_types.h:36
A non-fatal assertion, which checks a condition then permits execution to continue.
Definition std_code.h:270
An assumption, which must hold in subsequent code.
Definition std_code.h:217
A codet representing an assignment in the program.
Definition std_code.h:24
goto_instruction_codet representation of a function call statement.
Computes natural loops, enforces normal form conditions, computes the nesting graph,...
const dfcc_loop_infot & get_loop_info(const std::size_t loop_id) const
Returns the loop info for that loop_id.
const exprt & get_outer_write_set(std::size_t loop_id) const
Returns the write set of the outer loop of that loop or the top level write set if that loop has no o...
Translates assigns and frees clauses of a function contract or loop contract into GOTO programs that ...
void gen_spec_assigns_instructions(const irep_idt &language_mode, const exprt::operandst &assigns_clause, goto_programt &dest)
Generates instructions encoding the assigns_clause targets and adds them to dest.
void add_exit_instructions(const std::size_t loop_id, const std::size_t cbmc_loop_id, goto_functionst::goto_functiont &goto_function, goto_programt::targett loop_head, const symbol_exprt &loop_write_set, const symbol_exprt &addr_of_loop_write_set, const std::unordered_map< exprt, symbol_exprt, irep_hash > &history_var_map, const symbol_exprt &entered_loop, const symbol_exprt &initial_invariant, const symbol_exprt &in_base_case, const std::vector< symbol_exprt > &old_decreases_vars, const std::vector< symbol_exprt > &new_decreases_vars)
Adds instructions of the exit block.
dfcc_spec_functionst & spec_functions
dfcc_instrument_loopt(goto_modelt &goto_model, message_handlert &message_handler, dfcc_libraryt &library, dfcc_spec_functionst &spec_functions, dfcc_contract_clauses_codegent &contract_clauses_codegen)
Constructor for the loop contract instrumentation class.
void add_body_instructions(const std::size_t loop_id, const std::size_t cbmc_loop_id, goto_functionst::goto_functiont &goto_function, symbol_table_baset &symbol_table, goto_programt::targett loop_head, goto_programt::targett loop_latch, exprt &invariant, const exprt::operandst &decreases_clauses, const symbol_exprt &entered_loop, const symbol_exprt &in_base_case, const std::vector< symbol_exprt > &old_decreases_vars, const std::vector< symbol_exprt > &new_decreases_vars, const goto_programt::instructiont::targett &step_case_target, const irep_idt &symbol_mode)
Adds instructions of the body block.
std::unordered_map< exprt, symbol_exprt, irep_hash > add_prehead_instructions(const std::size_t loop_id, goto_functionst::goto_functiont &goto_function, symbol_table_baset &symbol_table, goto_programt::targett loop_head, goto_programt::targett loop_latch, goto_programt &assigns_instrs, exprt &invariant, const exprt::operandst &assigns, const symbol_exprt &loop_write_set, const symbol_exprt &addr_of_loop_write_set, const symbol_exprt &entered_loop, const symbol_exprt &initial_invariant, const symbol_exprt &in_base_case, const irep_idt &symbol_mode)
Adds instructions of prehead block, and return history variables.
void operator()(const std::size_t loop_id, const irep_idt &function_id, goto_functiont &goto_function, dfcc_cfg_infot &cfg_info, const std::set< symbol_exprt > &local_statics, std::set< irep_idt > &function_pointer_contracts)
Replaces a loop by a base + step abstraction generated from its contract.
goto_programt::instructiont::targett add_step_instructions(const std::size_t loop_id, const std::size_t cbmc_loop_id, const irep_idt &function_id, goto_functionst::goto_functiont &goto_function, symbol_table_baset &symbol_table, goto_programt::targett loop_head, goto_programt::targett loop_latch, goto_programt &havoc_instrs, exprt &invariant, const exprt::operandst &decreases_clauses, const symbol_exprt &loop_write_set, const exprt &outer_write_set, const symbol_exprt &initial_invariant, const symbol_exprt &in_base_case, const std::vector< symbol_exprt > &old_decreases_vars)
Adds instructions of the step block, and returns the STEP jump target so that it can be used to jump ...
dfcc_contract_clauses_codegent & contract_clauses_codegen
Class interface to library types and functions defined in cprover_contracts.c.
const code_function_callt write_set_release_call(const exprt &write_set_ptr, const source_locationt &source_location)
Builds call to __CPROVER_contracts_write_set_release.
const code_function_callt write_set_check_assigns_clause_inclusion_call(const exprt &check_var, const exprt &reference_write_set_ptr, const exprt &candidate_write_set_ptr, const source_locationt &source_location)
Builds call to __CPROVER_contracts_write_set_check_assigns_clause_inclusion.
const code_function_callt write_set_create_call(const exprt &write_set_ptr, const exprt &contract_assigns_size, const exprt &contract_frees_size, const exprt &assume_requires_ctx, const exprt &assert_requires_ctx, const exprt &assume_ensures_ctx, const exprt &assert_ensures_ctx, const exprt &allow_allocate, const exprt &allow_deallocate, const source_locationt &source_location)
Builds call to __CPROVER_contracts_write_set_create.
Describes a single loop for the purpose of DFCC loop contract instrumentation.
const symbol_exprt addr_of_write_set_var
Symbol representing pointer to the stack allocated write set object for this loop.
const exprt::operandst decreases
Decreases clause expression.
const std::set< exprt > assigns
Set of targets assigned by the loop, either user-provided or inferred.
const std::size_t cbmc_loop_id
Loop identifier assigned to this loop by traditional CBMC loop numbering.
std::optional< goto_programt::targett > find_latch(goto_programt &goto_program) const
const exprt invariant
Loop invariant expression.
std::optional< goto_programt::targett > find_head(goto_programt &goto_program) const
Finds the first instruction tagged as loop head and having the same loop identifier as this struct in...
const symbol_exprt write_set_var
Symbol representing the stack-allocated write set object for this loop.
This class rewrites GOTO functions that use the built-ins:
void generate_havoc_instructions(const irep_idt &function_id, const goto_programt &original_program, const exprt &write_set_to_havoc, goto_programt &havoc_program, std::size_t &nof_targets)
Translates original_program that specifies assignable targets into a program that havocs the targets.
void to_spec_assigns_instructions(const exprt &write_set_to_fill, const irep_idt &language_mode, goto_programt &program, std::size_t &nof_targets)
Rewrites in place program expressed in terms of built-ins specifying assignable targets declaratively...
dstringt has one field, an unsigned integer no which is an index into a static table of strings.
Definition dstring.h:38
Equality.
Definition std_expr.h:1361
Base class for all expressions.
Definition expr.h:56
std::vector< exprt > operandst
Definition expr.h:58
typet & type()
Return the type of the expression.
Definition expr.h:84
source_locationt & add_source_location()
Definition expr.h:236
The Boolean constant false.
Definition std_expr.h:3064
void goto_convert(const codet &code, goto_programt &dest, const irep_idt &mode)
::goto_functiont goto_functiont
A goto function, consisting of function body (see body) and parameter identifiers (see parameter_iden...
goto_programt body
symbol_tablet symbol_table
Symbol table.
Definition goto_model.h:31
std::list< instructiont >::iterator targett
The target for gotos and for start_thread nodes.
A generic container class for the GOTO intermediate representation of one function.
static instructiont make_dead(const symbol_exprt &symbol, const source_locationt &l=source_locationt::nil())
void update()
Update all indices.
void destructive_append(goto_programt &p)
Appends the given program p to *this. p is destroyed.
static instructiont make_assignment(const code_assignt &_code, const source_locationt &l=source_locationt::nil())
Create an assignment instruction.
instructionst::iterator targett
static instructiont make_skip(const source_locationt &l=source_locationt::nil())
static instructiont make_function_call(const code_function_callt &_code, const source_locationt &l=source_locationt::nil())
Create a function call instruction.
targett add(instructiont &&instruction)
Adds a given instruction at the end.
static instructiont make_goto(targett _target, const source_locationt &l=source_locationt::nil())
static instructiont make_decl(const symbol_exprt &symbol, const source_locationt &l=source_locationt::nil())
static instructiont make_assertion(const exprt &g, const source_locationt &l=source_locationt::nil())
static instructiont make_incomplete_goto(const exprt &_cond, const source_locationt &l=source_locationt::nil())
void swap(irept &irep)
Definition irep.h:434
message_handlert & get_message_handler()
Definition message.h:183
Boolean negation.
Definition std_expr.h:2327
The null pointer constant.
Boolean OR.
Definition std_expr.h:2228
void set_comment(const irep_idt &comment)
void set_property_class(const irep_idt &property_class)
const irep_idt & get_function() const
Expression to hold a symbol (variable)
Definition std_expr.h:131
The symbol table base class interface.
irep_idt mode
Language mode.
Definition symbol.h:49
The Boolean constant true.
Definition std_expr.h:3055
Class that computes CFG information about the loop structure of a GOTO function for the purpose of dy...
Translates assigns and frees clauses of a function contract or loop contract into goto programs that ...
Add instrumentation to a goto program to perform frame condition checks.
This class applies the loop contract transformation to a loop in a goto function.
bool dfcc_has_loop_id(const goto_programt::instructiont::const_targett &target, std::size_t loop_id)
void dfcc_remove_loop_tags(source_locationt &source_location)
std::optional< std::size_t > dfcc_get_loop_id(const goto_programt::instructiont::const_targett &target)
bool dfcc_is_loop_exiting(const goto_programt::instructiont::const_targett &target)
Functions that allow to tag GOTO instructions with loop identifiers and loop instruction type: head,...
Translate functions that specify assignable and freeable targets declaratively into active functions ...
bool has_subexpr(const exprt &expr, const std::function< bool(const exprt &)> &pred)
returns true if the expression has a subexpression that satisfies pred
exprt boolean_negate(const exprt &src)
negate a Boolean expression, possibly removing a not_exprt, and swapping false and true
Fresh auxiliary symbol creation.
Program Transformation.
const std::string & id2string(const irep_idt &d)
Definition irep.h:44
const pointer_typet & to_pointer_type(const typet &type)
Cast a typet to a pointer_typet.
#define INVARIANT(CONDITION, REASON)
This macro uses the wrapper function 'invariant_violated_string'.
Definition invariant.h:423
const symbol_exprt & to_symbol_expr(const exprt &expr)
Cast an exprt to a symbol_exprt.
Definition std_expr.h:272
static symbolt & get_function_symbol(symbol_table_baset &, const irep_idt &function_id)
Returns the symbolt for function_id.
static symbol_exprt create_symbol(symbol_table_baset &, const typet &type, const irep_idt &function_id, const std::string &base_name, const source_locationt &source_location)
Adds a new symbol named function_id::base_name of type type with given attributes in the symbol table...
void add_quantified_variable(symbol_table_baset &symbol_table, exprt &expression, const irep_idt &mode)
This function recursively searches expression to find nested or non-nested quantified expressions.
Definition utils.cpp:384
replace_history_parametert replace_history_loop_entry(symbol_table_baset &symbol_table, const exprt &expr, const source_locationt &location, const irep_idt &mode)
This function recursively identifies the "loop_entry" expressions within expr and replaces them with ...
Definition utils.cpp:515
void insert_before_swap_and_advance(goto_programt &destination, goto_programt::targett &target, goto_programt &payload)
Insert a goto program before a target instruction iterator and advance the iterator.
Definition utils.cpp:235
exprt generate_lexicographic_less_than_check(const std::vector< symbol_exprt > &lhs, const std::vector< symbol_exprt > &rhs)
Generate a lexicographic less-than comparison over ordered tuples.
Definition utils.cpp:191
#define ENTERED_LOOP
Definition utils.h:26
#define INIT_INVARIANT
Definition utils.h:28
#define IN_BASE_CASE
Definition utils.h:25
#define IN_LOOP_HAVOC_BLOCK
Definition utils.h:27