103 std::set<irep_idt> &dest)
const
175 static std::set<irep_idt> alloca_builtins = {"alloca", "__builtin_alloca"};
178 static std::set<std::string> builtins_with_cprover_impl = {
179 "__builtin_ia32_sfence",
180 "__builtin_ia32_lfence",
181 "__builtin_ia32_mfence",
185 "__builtin_ia32_vec_ext_v4hi",
186 "__builtin_ia32_vec_ext_v8hi",
187 "__builtin_ia32_vec_ext_v4si",
188 "__builtin_ia32_vec_ext_v2di",
189 "__builtin_ia32_vec_ext_v16qi",
190 "__builtin_ia32_vec_ext_v4sf",
191 "__builtin_ia32_psubsw128",
192 "__builtin_ia32_psubusw128",
193 "__builtin_ia32_paddsw",
194 "__builtin_ia32_psubsw",
195 "__builtin_ia32_vec_init_v4hi",
196 "__builtin_flt_rounds",
208 "__builtin_huge_valf",
209 "__builtin_huge_val",
210 "__builtin_huge_vall",
217 "__builtin___strcpy_chk",
218 "__builtin___strcat_chk",
219 "__builtin___strncat_chk",
220 "__builtin___strncpy_chk",
221 "__builtin___memcpy_chk",
223 "__builtin___memset_chk",
224 "__builtin___memmove_chk"};
242 std::set<irep_idt> &function_pointer_contracts)
249 auto found =
goto_model.goto_functions.function_map.find(function_id);
251 found !=
goto_model.goto_functions.function_map.end(),
252 "Function '" +
id2string(function_id) +
"' must exist in the model.");
258 const auto &function_symbol =
264 "__write_set_to_check",
265 function_symbol.location);
276 loop_contract_config,
277 function_pointer_contracts);
279 auto &body = goto_function.
body;
299std::set<symbol_exprt>
302 std::set<symbol_exprt> local_statics;
303 for(
const auto &sym_pair :
goto_model.symbol_table)
305 const auto &sym = sym_pair.second;
306 if(sym.is_static_lifetime)
308 const auto &loc = sym.location;
309 if(!loc.get_function().empty() && loc.get_function() == function_id)
311 local_statics.insert(sym.symbol_expr());
315 return local_statics;
321 std::set<irep_idt> &function_pointer_contracts)
328 auto found =
goto_model.goto_functions.function_map.find(function_id);
330 found !=
goto_model.goto_functions.function_map.end(),
331 "Function '" +
id2string(function_id) +
"' must exist in the model.");
336 "__write_set_to_check",
349 loop_contract_config,
350 function_pointer_contracts);
354 const irep_idt &wrapped_function_id,
355 const irep_idt &initial_function_id,
357 std::set<irep_idt> &function_pointer_contracts)
365 auto found =
goto_model.goto_functions.function_map.find(wrapped_function_id);
367 found !=
goto_model.goto_functions.function_map.end(),
368 "Function '" +
id2string(wrapped_function_id) +
369 "' must exist in the model.");
374 "__write_set_to_check",
387 loop_contract_config,
388 function_pointer_contracts);
394 const exprt &write_set,
395 std::set<irep_idt> &function_pointer_contracts)
417 body.instructions.
begin(),
418 body.instructions.end(),
420 function_pointer_contracts);
423 goto_program.
clear();
432 const exprt &write_set,
433 const std::set<symbol_exprt> &local_statics,
435 std::set<irep_idt> &function_pointer_contracts)
440 std::string options =
"assert-false-assume-false";
445 goto_function,
goto_model.symbol_table, function_id);
449 auto &body = goto_function.
body;
457 loop_contract_config,
466 body.instructions.
begin(),
467 body.instructions.end(),
469 function_pointer_contracts);
480 loop_contract_config,
482 function_pointer_contracts);
486 auto begin = body.instructions.begin();
487 auto end = std::prev(body.instructions.end());
490 for(
const auto &local_static : local_statics)
504 for(
const auto ¶m : code_type.
parameters())
506 const irep_idt ¶m_id = param.get_identifier();
507 if(top_level_tracked.find(param_id) != top_level_tracked.end())
509 symbol_exprt param_symbol{param.get_identifier(), param.type()};
527 std::set<irep_idt> &function_pointer_contracts)
532 goto_program, first_instruction, last_instruction, cfg_info);
537 goto_program, first_instruction, last_instruction, cfg_info);
542 goto_program, first_instruction, last_instruction, cfg_info);
547 goto_program, first_instruction, last_instruction, cfg_info);
556 function_pointer_contracts);
559 auto &target = first_instruction;
562 while(target != last_instruction)
564 if(target->is_decl())
568 if(target->is_dead())
572 else if(target->is_assign())
576 else if(target->is_function_call())
580 else if(target->is_other())
591 const exprt &write_set,
597 const auto &target_location = target->source_location();
602 library.write_set_add_decl_call(
606 auto label_instruction =
608 goto_instruction->complete_goto(label_instruction);
627 const auto &decl_symbol = target->decl_symbol();
632 function_id, write_set, decl_symbol, target, goto_program);
638 const exprt &write_set,
644 const auto &target_location = target->source_location();
649 library.write_set_record_dead_call(
653 auto label_instruction =
656 goto_instruction->complete_goto(label_instruction);
676 const auto &decl_symbol = target->dead_symbol();
679 function_id, write_set, decl_symbol, target, goto_program);
694 const auto &lhs_source_location = target->source_location();
719 "__check_lhs_assignment",
720 lhs_source_location);
725 library.write_set_check_assignment_call(
731 lhs_source_location),
732 lhs_source_location));
737 auto label_instruction =
739 goto_instruction->complete_goto(label_instruction);
750 const auto &lhs = target->assign_lhs();
751 const auto &rhs = target->assign_rhs();
752 const auto &target_location = target->source_location();
760 if(rhs.id() == ID_side_effect && rhs.get(ID_statement) == ID_allocate)
778 library.write_set_add_allocated_call(write_set, lhs, target_location),
781 auto label_instruction =
783 goto_instruction->complete_goto(label_instruction);
793 const exprt &write_set,
813 const auto &target_location = target->source_location();
814 const auto &callf = target->call_function();
819 library.get_instrumented_functions_map_symbol().symbol_expr(), object_id);
825 target->call_lhs(), target->call_function(), target->call_arguments());
826 call_inst.
arguments().push_back(write_set);
828 auto goto_end_inst = payload.
add(
831 goto_no_inst->complete_goto(no_inst_label);
833 target->call_lhs(), target->call_function(), target->call_arguments());
836 goto_end_inst->complete_goto(end_label);
838 target->turn_into_skip();
843 const exprt &write_set,
847 if(target->is_function_call())
849 if(target->call_function().id() == ID_symbol)
856 target->call_arguments().push_back(write_set);
865 write_set, target, goto_program);
872 const exprt &write_set,
876 INVARIANT(target->is_function_call(),
"target must be a function call");
878 target->call_function().id() == ID_symbol &&
883 auto &target_location = target->source_location();
904 "__check_deallocate",
909 const auto &ptr = target->call_arguments().at(0);
912 library.write_set_check_deallocate_call(
913 check_var, write_set, ptr, target_location),
929 library.write_set_record_deallocated_call(write_set, ptr, target_location),
932 auto label_instruction =
934 goto_instruction->complete_goto(label_instruction);
946 target->is_function_call(),
947 "the target must be a function call instruction");
952 if(target->call_lhs().is_not_nil() && cfg_info.
must_check_lhs(target))
955 function_id, target, target->call_lhs(), goto_program, cfg_info);
958 const auto &call_function = target->call_function();
960 call_function.id() == ID_symbol &&
979 const auto &target_location = target->source_location();
980 auto &statement = target->get_other().get_statement();
985 if(statement == ID_array_set || statement == ID_array_copy)
987 const bool is_array_set = statement == ID_array_set;
1007 is_array_set ?
"__check_array_set" :
"__check_array_copy",
1012 const auto &dest = target->get_other().operands().at(0);
1015 is_array_set ?
library.write_set_check_array_set_call(
1016 check_var, write_set, dest, target_location)
1017 :
library.write_set_check_array_copy_call(
1018 check_var, write_set, dest, target_location),
1025 std::string fun_name = is_array_set ?
"array_set" :
"array_copy";
1027 std::string
comment =
"Check that " + fun_name +
"(" +
1029 ", ...) is allowed by the assigns clause";
1035 auto label_instruction =
1037 goto_instruction->complete_goto(label_instruction);
1041 else if(statement == ID_array_replace)
1062 "__check_array_replace",
1067 const auto &dest = target->get_other().operands().at(0);
1068 const auto &src = target->get_other().operands().at(1);
1071 library.write_set_check_array_replace_call(
1072 check_var, write_set, dest, src, target_location),
1078 std::string
comment =
"Check that array_replace(" +
1080 ", ...) is allowed by the assigns clause";
1086 auto label_instruction =
1088 goto_instruction->complete_goto(label_instruction);
1092 else if(statement == ID_havoc_object)
1111 "__check_havoc_object",
1116 const auto &ptr = target->get_other().operands().at(0);
1119 library.write_set_check_havoc_object_call(
1120 check_var, write_set, ptr, target_location),
1126 std::string
comment =
"Check that havoc_object(" +
1128 ") is allowed by the assigns clause";
1134 auto label_instruction =
1136 goto_instruction->complete_goto(label_instruction);
1140 else if(statement == ID_expression)
1164 log.warning().source_location = target_location;
1165 log.warning() <<
"dfcc_instrument::instrument_other: statement type '"
1166 << statement <<
"' is not supported, analysis may be unsound"
1176 const std::set<symbol_exprt> &local_statics,
1177 std::set<irep_idt> &function_pointer_contracts)
1188 if(loop.must_skip())
1191 log.warning() <<
"loop " << function_id <<
"." << loop.cbmc_loop_id
1192 <<
" does not have a contract, skipping instrumentation"
1203 function_pointer_contracts);
1205 id2string(function_id) +
"." + std::to_string(loop.cbmc_loop_id) +
":2");
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::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.
const std::vector< std::size_t > & get_loops_toposorted() const
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 to use for the given instruction Returns the write set for the loop,...
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 ...
void instrument_function(const irep_idt &function_id, const loop_contract_configt &loop_contract_config, std::set< irep_idt > &function_pointer_contracts)
Instruments a GOTO function by adding an extra write set parameter and instrumenting its body instruc...
dfcc_contract_clauses_codegent & contract_clauses_codegen
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 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
static std::set< irep_idt > function_cache
Keeps track of instrumented functions, so that no function gets instrumented more than once.
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 loop_contract_configt &loop_contract_config, std::set< irep_idt > &function_pointer_contracts)
Instruments the body of a GOTO function against a given write set.
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_wrapped_function(const irep_idt &wrapped_function_id, const irep_idt &initial_function_id, const loop_contract_configt &loop_contract_config, std::set< irep_idt > &function_pointer_contracts)
Instruments a GOTO function by adding an extra write set parameter and inserting frame condition chec...
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...
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
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 apply_loop_contracts(const irep_idt &function_id, goto_functiont &goto_function, dfcc_cfg_infot &cfg_info, const loop_contract_configt &loop_contract_config, 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_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_harness_function(const irep_idt &function_id, const loop_contract_configt &loop_contract_config, std::set< irep_idt > &function_pointer_contracts)
Instruments a GOTO function used as a proof harness.
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.
dfcc_spec_functionst & spec_functions
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.
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_equals predicates into calls to the library implementation.
void rewrite_calls(goto_programt &program, dfcc_cfg_infot cfg_info)
Rewrites calls to pointer_equals 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:
std::string::const_iterator begin() const
Base class for all expressions.
const source_locationt & source_location() const
A goto function, consisting of function body (see body) and parameter identifiers (see parameter_iden...
bool body_available() const
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())
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
irep_idt mode
Language mode.
The Boolean constant true.
static exprt conditional_cast(const exprt &expr, const typet &type)
void parse_unwindset(const std::list< std::string > &unwindset, message_handlert &message_handler)
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
Instruments occurrences of obeys_contract predicates in programs encoding requires and ensures clause...
Instruments occurrences of pointer_equals 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 pointer_typet & to_pointer_type(const typet &type)
Cast a typet to a pointer_typet.
const dereference_exprt & to_dereference_expr(const exprt &expr)
Cast an exprt to a dereference_exprt.
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)
#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.
const code_typet & to_code_type(const typet &type)
Cast a typet to a code_typet.
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...
Loop contract configurations.
bool unwind_transformed_loops
bool apply_loop_contracts
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.