54 : goto_model(goto_model),
55 message_handler(message_handler),
58 spec_functions(spec_functions),
59 contract_clauses_codegen(contract_clauses_codegen),
65 contract_clauses_codegen),
66 ns(goto_model.symbol_table)
102 std::set<irep_idt> &dest)
const
241 std::set<irep_idt> &function_pointer_contracts)
251 "Function '" +
id2string(function_id) +
"' must exist in the model.");
257 const auto &function_symbol =
263 "__write_set_to_check",
264 function_symbol.location);
276 function_pointer_contracts);
278 auto &body = goto_function.
body;
298 std::set<symbol_exprt>
301 std::set<symbol_exprt> local_statics;
304 const auto &sym = sym_pair.second;
305 if(sym.is_static_lifetime)
307 const auto &loc = sym.location;
308 if(!loc.get_function().empty() && loc.get_function() == function_id)
310 local_statics.insert(sym.symbol_expr());
314 return local_statics;
320 std::set<irep_idt> &function_pointer_contracts)
330 "Function '" +
id2string(function_id) +
"' must exist in the model.");
335 "__write_set_to_check",
349 function_pointer_contracts);
353 const irep_idt &wrapped_function_id,
354 const irep_idt &initial_function_id,
356 std::set<irep_idt> &function_pointer_contracts)
367 "Function '" +
id2string(wrapped_function_id) +
368 "' must exist in the model.");
373 "__write_set_to_check",
387 function_pointer_contracts);
393 const exprt &write_set,
394 std::set<irep_idt> &function_pointer_contracts)
418 function_pointer_contracts);
421 goto_program.
clear();
430 const exprt &write_set,
431 const std::set<symbol_exprt> &local_statics,
433 std::set<irep_idt> &function_pointer_contracts)
438 std::string options =
"assert-false-assume-false";
447 auto &body = goto_function.
body;
463 body.instructions.
begin(),
464 body.instructions.end(),
466 function_pointer_contracts);
479 function_pointer_contracts);
483 auto begin = body.instructions.begin();
484 auto end = std::prev(body.instructions.end());
487 for(
const auto &local_static : local_statics)
501 for(
const auto ¶m : code_type.
parameters())
503 const irep_idt ¶m_id = param.get_identifier();
504 if(top_level_tracked.find(param_id) != top_level_tracked.end())
524 std::set<irep_idt> &function_pointer_contracts)
529 goto_program, first_instruction, last_instruction, cfg_info);
534 goto_program, first_instruction, last_instruction, cfg_info);
539 goto_program, first_instruction, last_instruction, cfg_info);
548 function_pointer_contracts);
551 auto &target = first_instruction;
554 while(target != last_instruction)
556 if(target->is_decl())
560 if(target->is_dead())
564 else if(target->is_assign())
568 else if(target->is_function_call())
572 else if(target->is_other())
583 const exprt &write_set,
589 const auto &target_location = target->source_location();
598 auto label_instruction =
600 goto_instruction->complete_goto(label_instruction);
619 const auto &decl_symbol = target->decl_symbol();
624 function_id, write_set, decl_symbol, target, goto_program);
630 const exprt &write_set,
636 const auto &target_location = target->source_location();
645 auto label_instruction =
648 goto_instruction->complete_goto(label_instruction);
668 const auto &decl_symbol = target->dead_symbol();
671 function_id, write_set, decl_symbol, target, goto_program);
686 const auto &lhs_source_location = target->source_location();
713 "__check_lhs_assignment",
714 lhs_source_location);
725 lhs_source_location),
726 lhs_source_location));
745 auto label_instruction =
747 goto_instruction->complete_goto(label_instruction);
759 lhs.
id() == ID_symbol &&
767 return if_expr.true_case();
781 const auto &lhs = target->assign_lhs();
782 const auto &rhs = target->assign_rhs();
783 const auto &target_location = target->source_location();
793 if(dead_ptr.has_value())
813 write_set, dead_ptr.value(), target_location),
816 auto label_instruction =
818 goto_instruction->complete_goto(label_instruction);
827 if(rhs.id() == ID_side_effect && rhs.get(ID_statement) == ID_allocate)
848 auto label_instruction =
850 goto_instruction->complete_goto(label_instruction);
860 const exprt &write_set,
880 const auto &target_location = target->source_location();
881 const auto &callf = target->call_function();
892 target->call_lhs(), target->call_function(), target->call_arguments());
893 call_inst.
arguments().push_back(write_set);
895 auto goto_end_inst = payload.
add(
898 goto_no_inst->complete_goto(no_inst_label);
900 target->call_lhs(), target->call_function(), target->call_arguments());
903 goto_end_inst->complete_goto(end_label);
905 target->turn_into_skip();
910 const exprt &write_set,
914 if(target->is_function_call())
916 if(target->call_function().id() == ID_symbol)
923 target->call_arguments().push_back(write_set);
932 write_set, target, goto_program);
939 const exprt &write_set,
943 INVARIANT(target->is_function_call(),
"target must be a function call");
945 target->call_function().id() == ID_symbol &&
950 auto &target_location = target->source_location();
971 "__check_deallocate",
976 const auto &ptr = target->call_arguments().at(0);
980 check_var, write_set, ptr, target_location),
999 auto label_instruction =
1001 goto_instruction->complete_goto(label_instruction);
1013 target->is_function_call(),
1014 "the target must be a function call instruction");
1019 if(target->call_lhs().is_not_nil())
1022 function_id, target, target->call_lhs(), goto_program, cfg_info);
1025 const auto &call_function = target->call_function();
1027 call_function.id() == ID_symbol &&
1046 const auto &target_location = target->source_location();
1047 auto &statement = target->get_other().get_statement();
1052 if(statement == ID_array_set || statement == ID_array_copy)
1054 const bool is_array_set = statement == ID_array_set;
1074 is_array_set ?
"__check_array_set" :
"__check_array_copy",
1079 const auto &dest = target->get_other().operands().at(0);
1083 check_var, write_set, dest, target_location)
1085 check_var, write_set, dest, target_location),
1092 std::string fun_name = is_array_set ?
"array_set" :
"array_copy";
1094 std::string
comment =
"Check that " + fun_name +
"(" +
1096 ", ...) is allowed by the assigns clause";
1102 auto label_instruction =
1104 goto_instruction->complete_goto(label_instruction);
1108 else if(statement == ID_array_replace)
1129 "__check_array_replace",
1134 const auto &dest = target->get_other().operands().at(0);
1135 const auto &src = target->get_other().operands().at(1);
1139 check_var, write_set, dest, src, target_location),
1145 std::string
comment =
"Check that array_replace(" +
1147 ", ...) is allowed by the assigns clause";
1153 auto label_instruction =
1155 goto_instruction->complete_goto(label_instruction);
1159 else if(statement == ID_havoc_object)
1178 "__check_havoc_object",
1183 const auto &ptr = target->get_other().operands().at(0);
1187 check_var, write_set, ptr, target_location),
1193 std::string
comment =
"Check that havoc_object(" +
1195 ") is allowed by the assigns clause";
1201 auto label_instruction =
1203 goto_instruction->complete_goto(label_instruction);
1207 else if(statement == ID_expression)
1232 log.
warning() <<
"dfcc_instrument::instrument_other: statement type '"
1233 << statement <<
"' is not supported, analysis may be unsound"
1243 const std::set<symbol_exprt> &local_statics,
1244 std::set<irep_idt> &function_pointer_contracts)
1255 if(loop.invariant.is_nil() && loop.decreases.empty())
1258 log.
warning() <<
"loop " << function_id <<
"." << loop.cbmc_loop_id
1259 <<
" does not have a contract, skipping instrumentation"
1270 function_pointer_contracts);
API to expression classes that are internal to the C frontend.
pointer_typet pointer_type(const typet &subtype)
unsignedbv_typet unsigned_char_type()
Operator to return the address of an object.
goto_instruction_codet representation of a function call statement.
const parameterst & parameters() const
Computes natural loops, enforces normal form conditions, computes the nesting graph,...
const std::vector< std::size_t > & get_loops_toposorted() const
const std::unordered_set< irep_idt > & get_top_level_tracked()
Returns the set of top level symbols that must be tracked explicitly in the top level write set of th...
const dfcc_loop_infot & get_loop_info(const std::size_t loop_id) const
Returns the loop info for that loop_id.
bool must_check_lhs(goto_programt::const_targett target) const
True iff the lhs of an assignment must be checked against the ambient write set.
const exprt & get_write_set(goto_programt::const_targett target) const
Returns the write set variable for that instruction.
bool must_track_decl_or_dead(goto_programt::const_targett target) const
True iff a DECL ident must be tracked in the write set of the loop that contains the DECL.
Translates assigns and frees clauses of a function contract or loop contract into GOTO programs that ...
std::size_t get_max_assigns_clause_size() const
Maximum number of assigns clause targets.
void instrument_goto_program(const irep_idt &function_id, goto_programt &goto_program, const exprt &write_set, std::set< irep_idt > &function_pointer_contracts)
Instruments a GOTO program against a given write set variable.
void instrument_call_instruction(const exprt &write_set, goto_programt::targett &target, goto_programt &goto_program)
Adds the write_set as extra argument to a function of function pointer call instruction.
void apply_loop_contracts(const irep_idt &function_id, goto_functiont &goto_function, dfcc_cfg_infot &cfg_info, const dfcc_loop_contract_modet loop_contract_mode, const std::set< symbol_exprt > &local_statics, std::set< irep_idt > &function_pointer_contracts)
Applies loop contracts transformations to the given GOTO function, using the given cfg_info instance ...
void instrument_lhs(const irep_idt &function_id, goto_programt::targett &target, const exprt &lhs, goto_programt &goto_program, dfcc_cfg_infot &cfg_info)
Instruments the LHS of an assignment instruction instruction by adding an inclusion check of lhs in w...
void instrument_other(const irep_idt &function_id, goto_programt::targett &target, goto_programt &goto_program, dfcc_cfg_infot &cfg_info)
Instruments a OTHER statement; instruction.
void instrument_assign(const irep_idt &function_id, goto_programt::targett &target, goto_programt &goto_program, dfcc_cfg_infot &cfg_info)
Instrument the lhs of an ASSIGN lhs := rhs instruction by adding an inclusion check of lhs in write_s...
void instrument_deallocate_call(const irep_idt &function_id, const exprt &write_set, goto_programt::targett &target, goto_programt &goto_program)
Inserts deallocation checks and a write set update before a call to the __CPROVER_deallocate function...
std::set< irep_idt > internal_symbols
Set of internal symbols which implementation is generated and inlined into the model at conversion or...
dfcc_instrument_loopt instrument_loop
void get_instrumented_functions(std::set< irep_idt > &dest) const
Adds the names of instrumented functions to dest.
message_handlert & message_handler
void instrument_goto_function(const irep_idt &function_id, goto_functiont &goto_function, const exprt &write_set, const std::set< symbol_exprt > &local_statics, const dfcc_loop_contract_modet loop_contract_mode, std::set< irep_idt > &function_pointer_contracts)
Instruments the body of a GOTO function against a given write set.
static std::set< irep_idt > function_cache
Keeps track of instrumented functions, so that no function gets instrumented more than once.
void instrument_function_call(const irep_idt &function_id, goto_programt::targett &target, goto_programt &goto_program, dfcc_cfg_infot &cfg_info)
Instruments a CALL lhs := function(params) instruction by adding an inclusion check of lhs in write_s...
void instrument_instructions(const irep_idt &function_id, goto_programt &goto_program, goto_programt::targett first_instruction, const goto_programt::targett &last_instruction, dfcc_cfg_infot &cfg_info, std::set< irep_idt > &function_pointer_contracts)
Instruments the instructions found between first_instruction and last_instruction in the instructions...
std::optional< exprt > is_dead_object_update(const exprt &lhs, const exprt &rhs)
Checks if lhs is the dead_object, and if rhs is an if_exprt(nondet, ptr, dead_object) expression.
void instrument_wrapped_function(const irep_idt &wrapped_function_id, const irep_idt &initial_function_id, const dfcc_loop_contract_modet loop_contract_mode, std::set< irep_idt > &function_pointer_contracts)
Instruments a GOTO function by adding an extra write set parameter and inserting frame condition chec...
dfcc_instrumentt(goto_modelt &goto_model, message_handlert &message_handler, dfcc_libraryt &library, dfcc_spec_functionst &spec_functions, dfcc_contract_clauses_codegent &contract_clauses_codegen)
void instrument_decl(const irep_idt &function_id, goto_programt::targett &target, goto_programt &goto_program, dfcc_cfg_infot &cfg_info)
Instruments a DECL x instruction.
std::size_t get_max_assigns_clause_size() const
void instrument_harness_function(const irep_idt &function_id, const dfcc_loop_contract_modet loop_contract_mode, std::set< irep_idt > &function_pointer_contracts)
Instruments a GOTO function used as a proof harness.
bool do_not_instrument(const irep_idt &id) const
True iff the symbol must not be instrumented because it is an internal symbol or a CPROVER symbol.
void instrument_dead(const irep_idt &function_id, goto_programt::targett &target, goto_programt &goto_program, dfcc_cfg_infot &cfg_info)
Instruments a DEAD x instruction.
void instrument_function(const irep_idt &function_id, const dfcc_loop_contract_modet loop_contract_mode, std::set< irep_idt > &function_pointer_contracts)
Instruments a GOTO function by adding an extra write set parameter and instrumenting its body instruc...
void insert_record_dead_call(const irep_idt &function_id, const exprt &write_set, const symbol_exprt &symbol_expr, goto_programt::targett &target, goto_programt &goto_program)
Generates a guarded call to record the death of a local symbol and inserts it in the goto_program at ...
std::set< symbol_exprt > get_local_statics(const irep_idt &function_id)
Returns the set of names from the symbol table that have the static flag set to true and have a sourc...
bool is_internal_symbol(const irep_idt &id) const
True iff the symbol an internal symbol.
void insert_add_decl_call(const irep_idt &function_id, const exprt &write_set, const symbol_exprt &symbol_expr, goto_programt::targett &target, goto_programt &goto_program)
Generates a guarded call to record a locally allocated symbol and inserts it in the goto_program at t...
void instrument_fptr_call_instruction_dynamic_lookup(const exprt &write_set, goto_programt::targett &target, goto_programt &goto_program)
Before calling a function pointer, performs a dynamic lookup into the map of instrumented function pr...
Rewrites calls to is_freeable and was_freed predicates in goto programs encoding pre and post conditi...
void rewrite_calls(goto_programt &program, dfcc_cfg_infot &cfg_info)
Rewrites calls to is_freeable and was_freed predicates into calls to the library implementation in th...
Rewrites calls to is_fresh predicates into calls to the library implementation.
void rewrite_calls(goto_programt &program, dfcc_cfg_infot &cfg_info)
Rewrites calls to is_fresh predicates into calls to the library implementation in the given program,...
Class interface to library types and functions defined in cprover_contracts.c.
const code_function_callt write_set_record_dead_call(const exprt &write_set_ptr, const exprt &ptr, const source_locationt &source_location)
Builds call to __CPROVER_contracts_write_set_record_dead.
const code_function_callt write_set_check_assignment_call(const exprt &check_var, const exprt &write_set_ptr, const exprt &ptr, const exprt &size, const source_locationt &source_location)
Builds call to __CPROVER_contracts_write_set_check_assignment.
const code_function_callt write_set_record_deallocated_call(const exprt &write_set_ptr, const exprt &ptr, const source_locationt &source_location)
Builds call to __CPROVER_contracts_write_set_record_deallocated.
const code_function_callt write_set_add_decl_call(const exprt &write_set_ptr, const exprt &ptr, const source_locationt &source_location)
Builds call to __CPROVER_contracts_write_set_add_decl.
const code_function_callt write_set_check_array_copy_call(const exprt &check_var, const exprt &write_set_ptr, const exprt &dest, const source_locationt &source_location)
Builds call to __CPROVER_contracts_write_set_check_array_copy.
const code_function_callt write_set_add_allocated_call(const exprt &write_set_ptr, const exprt &ptr, const source_locationt &source_location)
Builds call to __CPROVER_contracts_write_set_add_allocated.
const symbolt & get_instrumented_functions_map_symbol()
Returns the "__dfcc_instrumented_functions" symbol or creates it if it does not exist already.
const code_function_callt write_set_check_array_set_call(const exprt &check_var, const exprt &write_set_ptr, const exprt &dest, const source_locationt &source_location)
Builds call to __CPROVER_contracts_write_set_check_array_set.
const code_function_callt write_set_check_array_replace_call(const exprt &check_var, const exprt &write_set_ptr, const exprt &dest, const exprt &src, const source_locationt &source_location)
Builds call to __CPROVER_contracts_write_set_check_array_replace.
const code_function_callt write_set_check_deallocate_call(const exprt &check_var, const exprt &write_set_ptr, const exprt &ptr, const source_locationt &source_location)
Builds call to __CPROVER_contracts_write_set_check_deallocate.
std::map< dfcc_typet, typet > dfcc_type
Maps enum values to the actual types (dynamically loaded)
const code_function_callt write_set_check_havoc_object_call(const exprt &check_var, const exprt &write_set_ptr, const exprt &ptr, const source_locationt &source_location)
Builds call to __CPROVER_contracts_write_set_check_havoc_object.
Rewrites calls to obeys_contract predicates into calls to the library implementation.
void rewrite_calls(goto_programt &program, dfcc_cfg_infot &cfg_info, std::set< irep_idt > &function_pointer_contracts)
Rewrites calls to obeys_contract predicates into calls to the library implementation in the given pro...
Rewrites calls to pointer_in_range predicates into calls to the library implementation.
void rewrite_calls(goto_programt &program, dfcc_cfg_infot cfg_info)
Rewrites calls to pointer_in_range predicates into calls to the library implementation in the given p...
This class rewrites GOTO functions that use the built-ins:
dstringt has one field, an unsigned integer no which is an index into a static table of strings.
std::string::const_iterator begin() const
Base class for all expressions.
const source_locationt & source_location() const
function_mapt function_map
A goto function, consisting of function body (see body) and parameter identifiers (see parameter_iden...
bool body_available() const
symbol_tablet symbol_table
Symbol table.
goto_functionst goto_functions
GOTO functions.
A generic container class for the GOTO intermediate representation of one function.
instructionst instructions
The list of instructions in the goto program.
void copy_from(const goto_programt &src)
Copy a full goto program, preserving targets.
void clear()
Clear the goto program.
static instructiont make_dead(const symbol_exprt &symbol, const source_locationt &l=source_locationt::nil())
instructionst::iterator targett
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_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
source_locationt source_location
mstreamt & warning() const
message_handlert & get_message_handler()
A namespacet is essentially one or two symbol tables bound together, to allow for symbol lookups in t...
The null pointer constant.
void set_comment(const irep_idt &comment)
void set_property_class(const irep_idt &property_class)
Expression to hold a symbol (variable)
const irep_idt & get_identifier() const
class symbol_exprt symbol_expr() const
Produces a symbol_exprt for a symbol.
typet type
Type of symbol.
irep_idt mode
Language mode.
The Boolean constant true.
static exprt conditional_cast(const exprt &expr, const typet &type)
bool has_prefix(const std::string &s, const std::string &prefix)
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_is_cprover_function_symbol(const irep_idt &id)
Returns true iff id is one of the known CPROVER functions or starts with __VERIFIER or nondet.
Instruments occurrences of is_freeable predicates in programs encoding requires and ensures clauses o...
Instruments occurrences of is_fresh predicates in programs encoding requires and ensures clauses of c...
static const std::map< dfcc_funt, int > to_unwind
set of functions that need to be unwound to assigns clause size with corresponding loop identifiers.
Dynamic frame condition checking library loading.
@ WRITE_SET_PTR
type of pointers to descriptors of assignable/freeable sets of locations
dfcc_loop_contract_modet
Enumeration representing the instrumentation mode for loop contracts.
@ NONE
Do not apply loop contracts.
@ APPLY_UNWIND
Apply loop contracts and unwind the resulting base + step encoding.
Instruments occurrences of obeys_contract predicates in programs encoding requires and ensures clause...
Instruments occurrences of pointer_in_range predicates in programs encoding requires and ensures clau...
Translate functions that specify assignable and freeable targets declaratively into active functions ...
Dynamic frame condition checking utility functions.
Fresh auxiliary symbol creation.
std::unique_ptr< generate_function_bodiest > generate_function_bodies_factory(const std::string &options, const c_object_factory_parameterst &object_factory_parameters, const symbol_tablet &symbol_table, message_handlert &message_handler)
Create the type that actually generates the functions.
void generate_function_bodies(const std::regex &functions_regex, const generate_function_bodiest &generate_function_body, goto_modelt &model, message_handlert &message_handler)
Generate function bodies with some default behavior: assert-false, assume-false, assert-false-assume-...
const std::string & id2string(const irep_idt &d)
std::string from_expr_using_mode(const namespacet &ns, const irep_idt &mode, const exprt &expr)
Formats an expression using the given namespace, using the given mode to retrieve the language printe...
API to expression classes for Pointers.
const dereference_exprt & to_dereference_expr(const exprt &expr)
Cast an exprt to a dereference_exprt.
const pointer_typet & to_pointer_type(const typet &type)
Cast a typet to a pointer_typet.
exprt pointer_object(const exprt &p)
Various predicates over pointers in programs.
static std::string comment(const rw_set_baset::entryt &entry, bool write)
void remove_skip(goto_programt &goto_program, goto_programt::targett begin, goto_programt::targett end)
remove unnecessary skip statements
#define PRECONDITION_WITH_DIAGNOSTICS(CONDITION,...)
#define PRECONDITION(CONDITION)
bool can_cast_expr< side_effect_expr_nondett >(const exprt &base)
const if_exprt & to_if_expr(const exprt &expr)
Cast an exprt to an if_exprt.
const symbol_exprt & to_symbol_expr(const exprt &expr)
Cast an exprt to a symbol_exprt.
const code_typet & to_code_type(const typet &type)
Cast a typet to a code_typet.
std::string to_string(const string_not_contains_constraintt &expr)
Used for debug printing.
static symbolt & get_function_symbol(symbol_table_baset &, const irep_idt &function_id)
Returns the symbolt for function_id.
static const exprt make_null_check_expr(const exprt &ptr)
Returns the expression expr == NULL.
static exprt make_sizeof_expr(const exprt &expr, const namespacet &)
Returns the expression sizeof(expr).
static void add_parameter(goto_modelt &, const symbolt &symbol, const irep_idt &function_id)
Adds the given symbol as parameter to the function symbol's code_type.
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 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.