45 const std::size_t loop_id,
49 const std::set<symbol_exprt> &local_statics,
50 std::set<irep_idt> &function_pointer_contracts)
56 auto head_location(head->source_location());
67 std::string(
ENTERED_LOOP) +
"__" + std::to_string(cbmc_loop_id),
83 std::vector<symbol_exprt> old_decreases_vars, new_decreases_vars;
84 for(
const auto &clause : decreases_clauses)
88 symbol_table, clause.type(), function_id,
"tmp_cc", head_location);
89 old_decreases_vars.push_back(old_decreases_var);
92 symbol_table, clause.type(), function_id,
"tmp_cc", head_location);
93 new_decreases_vars.push_back(new_decreases_var);
100 for(
auto &local_static : local_statics)
102 assigns.push_back(local_static);
105 auto nof_targets = assigns.size();
116 language_mode, assigns, write_set_populate_instrs);
124 write_set_populate_instrs,
132 write_set_populate_instrs,
145 write_set_populate_instrs,
205std::unordered_map<exprt, symbol_exprt, irep_hash>
207 const std::size_t loop_id,
222 auto loop_head_location(loop_head->source_location());
243 symbol_table, invariant, loop_head_location, language_mode);
244 invariant.
swap(replace_history_result.expression_after_replacement);
246 replace_history_result.history_construction;
250 pre_loop_head_instrs.
add(
260 pre_loop_head_instrs.
add(
267 initial_invariant, invariant, loop_head_location};
270 initial_invariant_assignment, pre_loop_head_instrs, language_mode);
277 pre_loop_head_instrs.
add(
280 in_base_case,
true_exprt{}, loop_head_location));
292 pre_loop_head_instrs.
add(
294 pre_loop_head_instrs.
add(
297 addr_of_loop_write_set,
299 loop_head_location));
302 addr_of_loop_write_set,
305 pre_loop_head_instrs.
add(
312 pre_loop_head_instrs.
add(
315 goto_function.body.destructive_insert(loop_head, pre_loop_head_instrs);
316 return replace_history_result.parameter_to_history;
321 const std::size_t loop_id,
322 const std::size_t cbmc_loop_id,
332 const exprt &outer_write_set,
335 const std::vector<symbol_exprt> &old_decreases_vars)
337 auto loop_head_location(loop_head->source_location());
367 "Check invariant before entry for loop " +
369 std::to_string(cbmc_loop_id));
381 loop_head_location));
387 "__check_assigns_clause_incl_loop_" + std::to_string(cbmc_loop_id),
392 library.write_set_check_assigns_clause_inclusion_call(
393 check_var, outer_write_set, addr_of_loop_write_set, loop_head_location),
394 loop_head_location));
399 "Check assigns clause inclusion for loop " +
401 std::to_string(cbmc_loop_id));
405 auto label_instruction =
407 goto_instruction->complete_goto(label_instruction);
421 in_loop_havoc_block,
true_exprt{}, loop_head_location));
424 in_loop_havoc_block,
false_exprt{}, loop_head_location));
434 if(invariant.
id() == ID_and)
436 for(
const auto &op : invariant.
operands())
440 converter.
goto_convert(assumption, step_instrs, language_mode);
447 converter.
goto_convert(assumption, step_instrs, language_mode);
454 for(
size_t i = 0; i < old_decreases_vars.size(); i++)
457 old_decreases_vars[i], decreases_clauses[i], loop_head_location};
459 old_decreases_assignment, step_instrs, language_mode);
463 goto_function.body.destructive_insert(loop_head, step_instrs);
465 return step_case_target;
469 const std::size_t loop_id,
470 const std::size_t cbmc_loop_id,
479 const std::vector<symbol_exprt> &old_decreases_vars,
480 const std::vector<symbol_exprt> &new_decreases_vars,
484 auto loop_head_location(loop_head->source_location());
505 entered_loop,
true_exprt{}, loop_head_location));
512 step_case_target, in_base_case, loop_head_location));
523 "Check invariant after step for loop " +
525 std::to_string(cbmc_loop_id));
529 if(invariant.
id() == ID_and)
531 for(
const auto &op : invariant.
operands())
535 converter.
goto_convert(assertion, pre_loop_latch_instrs, language_mode);
542 converter.
goto_convert(assertion, pre_loop_latch_instrs, language_mode);
549 if(!decreases_clauses.empty())
551 for(
size_t i = 0; i < new_decreases_vars.size(); i++)
554 new_decreases_vars[i], decreases_clauses[i], loop_head_location};
556 new_decreases_assignment, pre_loop_latch_instrs, language_mode);
564 "Check variant decreases after step for loop " +
566 std::to_string(cbmc_loop_id));
569 new_decreases_vars, old_decreases_vars),
573 for(
size_t i = 0; i < old_decreases_vars.size(); i++)
575 pre_loop_latch_instrs.
add(
577 pre_loop_latch_instrs.
add(
584 goto_function.body, loop_latch, pre_loop_latch_instrs);
588 loop_latch->turn_into_assume();
589 loop_latch->condition_nonconst() =
boolean_negate(loop_latch->condition());
594 const std::size_t loop_id,
595 const std::size_t cbmc_loop_id,
600 const std::unordered_map<exprt, symbol_exprt, irep_hash> &history_var_map,
604 const std::vector<symbol_exprt> &old_decreases_vars,
605 const std::vector<symbol_exprt> &new_decreases_vars)
608 std::set<goto_programt::targett, goto_programt::target_less_than>
612 goto_function.body.instructions.begin();
613 target != goto_function.body.instructions.end();
618 INVARIANT(target->is_goto(),
"Exiting instructions must be GOTOs");
619 auto exit_target = target->get_target();
622 exit_loop_id_opt.has_value() && exit_loop_id_opt.value() != loop_id,
623 "Exiting instructions must jump out of the loop");
624 exit_targets.insert(exit_target);
639 for(
auto exit_target : exit_targets)
648 "Check step was unwound for loop " +
650 std::to_string(cbmc_loop_id));
657 loop_exit_program.
add(
659 loop_exit_program.
add(
661 loop_exit_program.
add(
666 library.write_set_release_call(addr_of_loop_write_set, exit_location),
670 loop_exit_program.
add(
676 for(
const auto &v : history_var_map)
680 for(
size_t i = 0; i < old_decreases_vars.size(); i++)
682 loop_exit_program.
add(
684 loop_exit_program.
add(
690 goto_function.body, exit_target, loop_exit_program);
unsignedbv_typet size_type()
Operator to return the address of an object.
A non-fatal assertion, which checks a condition then permits execution to continue.
An assumption, which must hold in subsequent code.
A codet representing an assignment in the program.
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 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::size_t max_assigns_clause_size
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.
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:
Base class for all expressions.
std::vector< exprt > operandst
typet & type()
Return the type of the expression.
source_locationt & add_source_location()
The Boolean constant false.
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...
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.
instructionst::iterator targett
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.
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())
const irep_idt & id() const
The null pointer constant.
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).
The symbol table base class interface.
irep_idt mode
Language mode.
The Boolean constant true.
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 ...
This class applies the loop contract transformation to a loop in a goto function.
Dynamic frame condition checking library loading.
Translate functions that specify assignable and freeable targets declaratively into active functions ...
@ NONDET
havocs the pointer to an nondet pointer
Dynamic frame condition checking utility functions.
exprt boolean_negate(const exprt &src)
negate a Boolean expression, possibly removing a not_exprt, and swapping false and true
Deprecated expression utility functions.
const std::string & id2string(const irep_idt &d)
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'.
const symbol_exprt & to_symbol_expr(const exprt &expr)
Cast an exprt to a symbol_exprt.
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...
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 ...
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.
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.
#define IN_LOOP_HAVOC_BLOCK