Go to the documentation of this file.
15 #include <unordered_set>
53 if(exp.
id() != ID_symbol)
77 if((*it)->is_goto() &&
78 (*it)->get_target()==loop_head &&
79 (*it)->location_number>loop_end->location_number)
84 loop_end->get_condition().find(ID_C_spec_loop_invariant));
110 a->source_location.set_comment(
"Loop invariant violated before entry");
121 if(!loop_head->is_goto())
130 goto_function.body.insert_before_swap(loop_head, havoc_code);
137 goto_function.body.insert_before_swap(loop_end, a);
142 loop_end->targets.clear();
144 if(loop_head->is_goto())
147 loop_end->set_condition(
boolean_negate(loop_end->get_condition()));
180 exprt assigns =
static_cast<const exprt &
>(type.
find(ID_C_spec_assigns));
181 exprt requires =
static_cast<const exprt &
>(type.
find(ID_C_spec_requires));
182 exprt ensures =
static_cast<const exprt &
>(type.
find(ID_C_spec_ensures));
217 "ignored_return_value",
229 code_function_callt::argumentst::const_iterator a_it=
231 for(code_typet::parameterst::const_iterator p_it = type.
parameters().begin();
235 if(!p_it->get_identifier().empty())
264 for(
const exprt &curr_op : targets)
266 if(curr_op.id() == ID_symbol || curr_op.id() == ID_dereference)
268 assigns_tgts.insert(curr_op);
272 log.
error() <<
"Unable to apply assigns clause for expression of type '"
273 << curr_op.id() <<
"'; not enforcing assigns clause."
282 std::advance(target, assigns_tgts.size());
309 for(natural_loops_mutablet::loop_mapt::const_iterator l_it =
311 l_it != natural_loops.
loop_map.end();
314 goto_function, local_may_alias, l_it->first, l_it->second);
318 if(ins->is_function_call())
338 const exprt &assigns,
340 std::vector<exprt> &aliasable_references)
342 bool first_iter =
true;
344 for(
auto aliasable : aliasable_references)
347 exprt right_ptr = aliasable;
358 disjuncts.push_back(same);
359 disjuncts.push_back(running);
368 const symbolt &function_symbol,
371 std::vector<exprt> &created_references)
374 const exprt &assigns =
375 static_cast<const exprt &
>(type.
find(ID_C_spec_assigns));
378 for(
const exprt &curr_op : targets)
385 function_symbol.
mode)
396 created_references.push_back(standin);
401 goto_programt::instructionst::iterator &instruction_iterator,
404 std::vector<exprt> &assigns_references,
405 std::set<exprt> &freely_assignable_exprs)
408 instruction_iterator->is_assign(),
409 "The first argument of instrument_assigns_statement should always be"
411 const exprt &lhs = instruction_iterator->get_assign().lhs();
412 if(freely_assignable_exprs.find(lhs) != freely_assignable_exprs.end())
422 ++instruction_iterator;
426 goto_programt::instructionst::iterator &instruction_iterator,
429 std::vector<exprt> &aliasable_references,
430 std::set<exprt> &freely_assignable_exprs)
433 instruction_iterator->is_function_call(),
434 "The first argument of instrument_call_statement should always be "
443 if(called_name ==
"malloc")
445 aliasable_references.push_back(call.
lhs());
448 goto_programt::instructionst::iterator local_instruction_iterator =
449 instruction_iterator;
450 local_instruction_iterator++;
452 local_instruction_iterator->is_assign() &&
453 local_instruction_iterator->get_assign().lhs().is_not_nil())
455 freely_assignable_exprs.insert(
456 local_instruction_iterator->get_assign().lhs());
470 ++instruction_iterator;
482 log.
error() <<
"Could not find function '" << called_name
483 <<
"' in goto-program; not enforcing assigns clause."
488 exprt called_assigns =
489 static_cast<const exprt &
>(called_sym.
type.
find(ID_C_spec_assigns));
490 if(called_assigns.
is_nil())
493 log.
error() <<
"No assigns specification found for function '"
495 <<
"' in goto-program; not enforcing assigns clause."
504 ++instruction_iterator;
512 code_function_callt::argumentst::const_iterator a_it =
514 for(code_typet::parameterst::const_iterator p_it =
520 if(!p_it->get_identifier().empty())
528 for(exprt::operandst::const_iterator called_op_it =
530 called_op_it != called_assigns.
operands().end();
534 freely_assignable_exprs.find(*called_op_it) !=
535 freely_assignable_exprs.end())
546 ++instruction_iterator;
555 std::vector<goto_programt::instructiont> back_gotos;
556 std::vector<goto_programt::instructiont> malloc_calls;
563 back_gotos.push_back(instruction);
571 if(called_name ==
"malloc")
573 malloc_calls.push_back(instruction);
580 for(
auto goto_entry : back_gotos)
582 for(
const auto &target : goto_entry.targets)
584 for(
auto malloc_entry : malloc_calls)
587 malloc_entry.location_number >= target->location_number &&
588 malloc_entry.location_number < goto_entry.location_number)
598 log.
error() <<
"Call to malloc at location "
599 << malloc_entry.location_number <<
" falls between goto "
600 <<
"source location " << goto_entry.location_number
601 <<
" and it's destination at location "
602 << target->location_number <<
". "
603 <<
"Unable to complete instrumentation, as this malloc "
619 log.
error() <<
"Could not find function '" << function_name
620 <<
"' in goto-program; not enforcing contracts."
630 const irep_idt function_id(function_name);
634 exprt assigns =
static_cast<const exprt &
>(type.
find(ID_C_spec_assigns));
640 goto_programt::instructionst::iterator instruction_iterator =
646 std::vector<exprt> original_references;
648 function_symbol, function_id, standin_decls, original_references);
651 std::set<exprt> freely_assignable_exprs;
654 freely_assignable_exprs.insert(param);
657 int lines_to_iterate = standin_decls.
instructions.size();
659 std::advance(instruction_iterator, lines_to_iterate);
667 for(; instruction_iterator != program.
instructions.end();
668 ++instruction_iterator)
670 if(instruction_iterator->is_decl())
672 freely_assignable_exprs.insert(instruction_iterator->decl_symbol());
674 else if(instruction_iterator->is_assign())
677 instruction_iterator,
681 freely_assignable_exprs);
683 else if(instruction_iterator->is_function_call())
686 instruction_iterator,
690 freely_assignable_exprs);
703 std::stringstream ss;
706 const irep_idt original(fun_to_enforce);
711 log.
error() <<
"Could not find function '" << fun_to_enforce
712 <<
"' in goto-program; not enforcing contracts."
722 sl.
set_file(
"instrumented for code contracts");
726 mangled_sym = *original_sym;
727 mangled_sym.
name = mangled;
732 mangled_found.second,
733 "There should be no existing function called " + ss.str() +
734 " in the symbol table because that name is a mangled name");
740 "There should be no function called " + fun_to_enforce +
741 " in the function map because that function should have had its"
747 "There should be a function called " + ss.str() +
748 " in the function map because we inserted a fresh goto-program"
749 " with that mangled name");
768 const exprt &assigns =
769 static_cast<const exprt &
>(code_type.
find(ID_C_spec_assigns));
770 const exprt &requires =
771 static_cast<const exprt &
>(code_type.
find(ID_C_spec_requires));
772 const exprt &ensures =
773 static_cast<const exprt &
>(code_type.
find(ID_C_spec_ensures));
776 "Code contract enforcement is trivial without an ensures or assigns "
812 function_symbol.
mode)
823 goto_functionst::function_mapt::iterator f_it =
828 for(
const auto ¶meter : gf.parameter_identifiers)
834 parameter_symbol.
type,
837 parameter_symbol.
mode)
850 exprt requires_cond = requires;
861 exprt ensures_cond = ensures;
877 const std::set<std::string> &funs_to_replace)
880 for(
const auto &fun : funs_to_replace)
885 <<
"' does not have a contract; "
886 "not replacing calls with contract."
898 if(ins->is_function_call())
908 auto found = std::find(
909 funs_to_replace.begin(), funs_to_replace.end(),
id2string(fun_name));
910 if(found == funs_to_replace.end())
931 std::set<std::string> funs_to_replace;
935 funs_to_replace.insert(
id2string(goto_function.first));
942 std::set<std::string> funs_to_enforce;
946 funs_to_enforce.insert(
id2string(goto_function.first));
952 const std::set<std::string> &funs_to_enforce)
955 for(
const auto &fun : funs_to_enforce)
960 log.
error() <<
"Could not find function '" << fun
961 <<
"' in goto-program; not enforcing contracts."
#define Forall_goto_program_instructions(it, program)
dstringt has one field, an unsigned integer no which is an index into a static table of strings.
void code_contracts(goto_functionst::goto_functiont &goto_function)
const symbolt & lookup_ref(const irep_idt &name) const
Find a symbol in the symbol table for read-only access.
source_locationt source_location
The location of the instruction in the source file.
void add_contract_check(const irep_idt &, const irep_idt &, goto_programt &dest)
void set_comment(const irep_idt &comment)
void populate_assigns_references(const symbolt &f_sym, const irep_idt &func_id, goto_programt &created_decls, std::vector< exprt > &created_references)
Creates a local variable declaration for each expression in the assigns clause (of the function given...
void instrument_call_statement(goto_programt::instructionst::iterator &ins_it, goto_programt &program, exprt &assigns, std::vector< exprt > &assigns_references, std::set< exprt > &freely_assignable_exprs)
Inserts an assertion statement into program before the function call at ins_it, to ensure that any me...
The type of an expression, extends irept.
Fresh auxiliary symbol creation.
typet type
Type of symbol.
void remove_skip(goto_programt &goto_program, goto_programt::targett begin, goto_programt::targett end)
remove unnecessary skip statements
Various predicates over pointers in programs.
const irept & find(const irep_namet &name) const
void replace(const union_find_replacet &replace_map, string_not_contains_constraintt &constraint)
Predicate to be used with the exprt::visit() function.
static instructiont make_end_function(const source_locationt &l=source_locationt::nil())
targett add(instructiont &&instruction)
Adds a given instruction at the end.
const_iterator end() const
Iterator over this loop's instructions.
Base class for all expressions.
irep_idt base_name
Base (non-scoped) name.
function_mapt function_map
Expression to hold a symbol (variable)
static instructiont make_decl(const symbol_exprt &symbol, const source_locationt &l=source_locationt::nil())
void visit(class expr_visitort &visitor)
These are pre-order traversal visitors, i.e., the visitor is executed on a node before its children h...
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)
static instructiont make_assignment(const code_assignt &_code, const source_locationt &l=source_locationt::nil())
Create an assignment instruction.
static instructiont make_goto(targett _target, const source_locationt &l=source_locationt::nil())
void set_line(const irep_idt &line)
static instructiont make_function_call(const code_function_callt &_code, const source_locationt &l=source_locationt::nil())
Create a function call instruction.
typet & type()
Return the type of the expression.
bool lookup(const irep_idt &name, const symbolt *&symbol) const override
See documentation for namespace_baset::lookup().
codet representation of a function call statement.
const_iterator begin() const
Iterator over this loop's instructions.
const code_typet & to_code_type(const typet &type)
Cast a typet to a code_typet.
exprt disjunction(const exprt::operandst &op)
1) generates a disjunction for two or more operands 2) for one operand, returns the operand 3) return...
std::set< exprt > modifiest
static instructiont make_assertion(const exprt &g, const source_locationt &l=source_locationt::nil())
irep_idt mode
Language mode.
loop_instructionst::const_iterator const_iterator
void destructive_insert(const_targett target, goto_programt &p)
Inserts the given program p before target.
Field-insensitive, location-sensitive may-alias analysis.
const std::string & id2string(const irep_idt &d)
static instructiont make_skip(const source_locationt &l=source_locationt::nil())
bool has_contract(const irep_idt)
Does the named function have a contract?
#define PRECONDITION(CONDITION)
const source_locationt & source_location() const
const irep_idt & get_identifier() const
exprt boolean_negate(const exprt &src)
negate a Boolean expression, possibly removing a not_exprt, and swapping false and true
void instrument_assigns_statement(goto_programt::instructionst::iterator &ins_it, goto_programt &program, exprt &assigns, std::vector< exprt > &original_references, std::set< exprt > &freely_assignable_exprs)
Inserts an assertion statement into program before the assignment ins_it, to ensure that the left-han...
bool is_backwards_goto() const
Returns true if the instruction is a backwards branch.
void set_file(const irep_idt &file)
class symbol_exprt symbol_expr() const
Produces a symbol_exprt for a symbol.
API to expression classes for Pointers.
const code_function_callt & get_function_call() const
Get the function call for FUNCTION_CALL.
bool check_for_looped_mallocs(const goto_programt &)
Check if there are any malloc statements which may be repeated because of a goto statement that jumps...
bool apply_contract(goto_programt &goto_program, goto_programt::targett target)
bool add_pointer_checks(const std::string &)
Insert assertion statements into the goto program to ensure that assigned memory is within the assign...
A loop, specified as a set of instructions.
virtual std::pair< symbolt &, bool > insert(symbolt symbol) override
Author: Diffblue Ltd.
pointer_typet pointer_type(const typet &subtype)
const symbol_exprt & to_symbol_expr(const exprt &expr)
Cast an exprt to a symbol_exprt.
const irep_idt & id() const
A goto function, consisting of function body (see body) and parameter identifiers (see parameter_iden...
std::vector< exprt > operandst
The Boolean constant false.
const parameterst & parameters() const
Helper functions for k-induction and loop invariants.
void destructive_append(goto_programt &p)
Appends the given program p to *this. p is destroyed.
void operator()(const exprt &exp) override
A side_effect_exprt that returns a non-deterministically chosen value.
::goto_functiont goto_functiont
const symbolt & new_tmp_symbol(const typet &type, const source_locationt &source_location, const irep_idt &function_id, const irep_idt &mode)
instructionst instructions
The list of instructions in the goto program.
Deprecated expression utility functions.
bool enforce_contracts()
Call enforce_contracts() on all functions that have a contract.
goto_functionst & goto_functions
source_locationt location
Source code location of definition of symbol.
void get_modifies(const local_may_aliast &local_may_alias, const loopt &loop, modifiest &modifies)
exprt same_object(const exprt &p1, const exprt &p2)
A generic container class for the GOTO intermediate representation of one function.
static exprt create_alias_expression(const exprt &assigns, const exprt &lhs, std::vector< exprt > &aliasable_references)
bool replace_calls()
Call replace_calls() on all calls to any function that has a contract.
std::unordered_set< irep_idt > summarized
bool empty() const
Returns true if this loop contains no instructions.
const symbolt * lookup(const irep_idt &name) const
Find a symbol in the symbol table for read-only access.
const typet & return_type() const
parameter_identifierst parameter_identifiers
The identifiers of the parameters of this function.
static instructiont make_assumption(const exprt &g, const source_locationt &l=source_locationt::nil())
bool enforce_contract(const std::string &)
Enforce contract of a single function.
Operator to return the address of an object.
#define INVARIANT(CONDITION, REASON)
This macro uses the wrapper function 'invariant_violated_string'.
void insert_before_swap(targett target)
Insertion that preserves jumps to "target".
bool found_return_value()
A codet representing an assignment in the program.
void build_havoc_code(const goto_programt::targett loop_head, const modifiest &modifies, goto_programt &dest)
bool is_function_call() const
This class represents an instruction in the GOTO intermediate representation.
const source_locationt & source_location() const
symbolt & get_fresh_aux_symbol(const typet &type, const std::string &name_prefix, const std::string &basename_prefix, const source_locationt &source_location, const irep_idt &symbol_mode, const namespacet &ns, symbol_table_baset &symbol_table)
Installs a fresh-named symbol with respect to the given namespace ns with the requested name pattern ...
irep_idt name
The unique identifier.
instructionst::iterator targett
symbol_tablet & symbol_table
Verify and use annotated invariants and pre/post-conditions.
Replace expression or type symbols by an expression or type, respectively.