CBMC
contracts.h
Go to the documentation of this file.
1 /*******************************************************************\
2 
3 Module: Verify and use annotated invariants and pre/post-conditions
4 
5 Author: Michael Tautschnig
6 
7 Date: February 2016
8 
9 \*******************************************************************/
10 
13 
14 #ifndef CPROVER_GOTO_INSTRUMENT_CONTRACTS_CONTRACTS_H
15 #define CPROVER_GOTO_INSTRUMENT_CONTRACTS_CONTRACTS_H
16 
18 
19 #include <util/message.h>
20 #include <util/namespace.h>
21 #include <util/optional.h>
22 #include <util/pointer_expr.h>
23 
27 
29 
30 #include <list>
31 #include <map>
32 #include <set>
33 #include <string>
34 #include <unordered_set>
35 
36 #define FLAG_LOOP_CONTRACTS "apply-loop-contracts"
37 #define HELP_LOOP_CONTRACTS \
38  " --apply-loop-contracts\n" \
39  " check and use loop contracts when provided\n"
40 
41 #define FLAG_REPLACE_CALL "replace-call-with-contract"
42 #define HELP_REPLACE_CALL \
43  " --replace-call-with-contract <fun>\n" \
44  " replace calls to fun with fun's contract\n"
45 
46 #define FLAG_ENFORCE_CONTRACT "enforce-contract"
47 #define HELP_ENFORCE_CONTRACT \
48  " --enforce-contract <fun> wrap fun with an assertion of its contract\n"
49 
50 class local_may_aliast;
52 class cfg_infot;
54 
56 {
57 public:
63  log(log),
64  converter(symbol_table, log.get_message_handler())
65  {
66  }
67 
69  void check_all_functions_found(const std::set<std::string> &functions) const;
70 
84  void replace_calls(const std::set<std::string> &to_replace);
85 
106  void enforce_contracts(
107  const std::set<std::string> &to_enforce,
108  const std::set<std::string> &to_exclude_from_nondet_init = {});
109 
114  const std::set<std::string> &to_exclude_from_nondet_init = {});
115 
117  const irep_idt &function_name,
118  goto_functionst::goto_functiont &goto_function,
119  const local_may_aliast &local_may_alias,
120  goto_programt::targett loop_head,
121  goto_programt::targett loop_end,
122  const loopt &loop,
123  exprt assigns_clause,
124  exprt invariant,
125  exprt decreases_clause,
126  const irep_idt &mode);
127 
128  // for "helper" classes to update symbol table.
131 
133 
134 protected:
138 
141 
142  std::unordered_set<irep_idt> summarized;
143 
144 public:
155  const irep_idt &property_class,
156  const irep_idt &mode,
157  goto_programt &dest);
158 
168  const irep_idt &mode,
169  goto_programt &dest);
170 
172  void enforce_contract(const irep_idt &function);
173 
175  void check_frame_conditions_function(const irep_idt &function);
176 
179  void apply_loop_contract(
180  const irep_idt &function,
181  goto_functionst::goto_functiont &goto_function);
182 
187  const irep_idt &function,
188  const source_locationt &location,
189  goto_programt &function_body,
190  goto_programt::targett &target);
191 
194  void add_contract_check(
195  const irep_idt &wrapper_function,
196  const irep_idt &mangled_function,
197  goto_programt &dest);
198 
203  void add_quantified_variable(exprt &expression, const irep_idt &mode);
204 
208  exprt &expr,
209  std::map<exprt, exprt> &parameter2history,
210  source_locationt location,
211  const irep_idt &mode,
212  goto_programt &history,
213  const irep_idt &id);
214 
218  std::pair<goto_programt, goto_programt> create_ensures_instruction(
219  codet &expression,
220  source_locationt location,
221  const irep_idt &mode);
222 };
223 
224 #endif // CPROVER_GOTO_INSTRUMENT_CONTRACTS_CONTRACTS_H
messaget
Class that provides messages with a built-in verbosity 'level'.
Definition: message.h:154
dstringt
dstringt has one field, an unsigned integer no which is an index into a static table of strings.
Definition: dstring.h:36
symbol_tablet
The symbol table.
Definition: symbol_table.h:13
code_contractst::check_apply_loop_contracts
void check_apply_loop_contracts(const irep_idt &function_name, goto_functionst::goto_functiont &goto_function, const local_may_aliast &local_may_alias, goto_programt::targett loop_head, goto_programt::targett loop_end, const loopt &loop, exprt assigns_clause, exprt invariant, exprt decreases_clause, const irep_idt &mode)
Definition: contracts.cpp:51
code_contractst::enforce_contract
void enforce_contract(const irep_idt &function)
Enforce contract of a single function.
Definition: contracts.cpp:1277
code_contractst::check_frame_conditions_function
void check_frame_conditions_function(const irep_idt &function)
Instrument functions to check frame conditions.
Definition: contracts.cpp:1186
cfg_infot
Stores information about a goto function computed from its CFG.
Definition: cfg_info.h:38
code_contractst::apply_loop_contracts
void apply_loop_contracts(const std::set< std::string > &to_exclude_from_nondet_init={})
Applies loop contract transformations.
Definition: contracts.cpp:1607
code_contractst::add_contract_check
void add_contract_check(const irep_idt &wrapper_function, const irep_idt &mangled_function, goto_programt &dest)
Instruments wrapper_function adding assumptions based on requires clauses and assertions based on ens...
Definition: contracts.cpp:1369
code_contractst::replace_calls
void replace_calls(const std::set< std::string > &to_replace)
Replace all calls to each function in the to_replace set with that function's contract.
Definition: contracts.cpp:1567
optional.h
instrument_preconditions.h
goto_convert_class.h
goto_model.h
exprt
Base class for all expressions.
Definition: expr.h:55
goto_modelt
Definition: goto_model.h:25
code_contractst::log
messaget & log
Definition: contracts.h:139
code_contractst::assert_function_pointer_obeys_contract
void assert_function_pointer_obeys_contract(const function_pointer_obeys_contract_exprt &expr, const irep_idt &property_class, const irep_idt &mode, goto_programt &dest)
Translates a function_pointer_obeys_contract_exprt into an assertion.
Definition: contracts.cpp:1331
namespace.h
instrument_spec_assignst
A class that generates instrumentation for assigns clause checking.
Definition: instrument_spec_assigns.h:189
local_may_aliast
Definition: local_may_alias.h:25
code_contractst::apply_function_contract
void apply_function_contract(const irep_idt &function, const source_locationt &location, goto_programt &function_body, goto_programt::targett &target)
Replaces function calls with assertions based on requires clauses, non-deterministic assignments for ...
Definition: contracts.cpp:680
namespacet
A namespacet is essentially one or two symbol tables bound together, to allow for symbol lookups in t...
Definition: namespace.h:90
code_contractst::code_contractst
code_contractst(goto_modelt &goto_model, messaget &log)
Definition: contracts.h:58
code_contractst::converter
goto_convertt converter
Definition: contracts.h:140
goto_convertt
Definition: goto_convert_class.h:30
code_contractst::add_quantified_variable
void add_quantified_variable(exprt &expression, const irep_idt &mode)
This function recursively searches expression to find nested or non-nested quantified expressions.
Definition: contracts.cpp:489
pointer_expr.h
code_contractst::assume_function_pointer_obeys_contract
void assume_function_pointer_obeys_contract(const function_pointer_obeys_contract_exprt &expr, const irep_idt &mode, goto_programt &dest)
Translates a function_pointer_obeys_contract_exprt into an assignment.
Definition: contracts.cpp:1353
code_contractst
Definition: contracts.h:55
loop_templatet
A loop, specified as a set of instructions.
Definition: loop_analysis.h:23
code_contractst::apply_loop_contract
void apply_loop_contract(const irep_idt &function, goto_functionst::goto_functiont &goto_function)
Apply loop contracts, whenever available, to all loops in function.
Definition: contracts.cpp:929
code_contractst::goto_model
goto_modelt & goto_model
Definition: contracts.h:135
code_contractst::ns
namespacet ns
Definition: contracts.h:132
function_pointer_obeys_contract_exprt
A class for expressions representing a requires_contract(fptr, contract) clause or an ensures_contrac...
Definition: c_expr.h:327
loop_utils.h
source_locationt
Definition: source_location.h:18
goto_functionst::goto_functiont
::goto_functiont goto_functiont
Definition: goto_functions.h:27
code_contractst::check_all_functions_found
void check_all_functions_found(const std::set< std::string > &functions) const
Throws an exception if some function functions is found in the program.
Definition: contracts.cpp:1552
goto_functionst
A collection of goto functions.
Definition: goto_functions.h:24
code_contractst::create_ensures_instruction
std::pair< goto_programt, goto_programt > create_ensures_instruction(codet &expression, source_locationt location, const irep_idt &mode)
This function creates and returns an instruction that corresponds to the ensures clause.
Definition: contracts.cpp:635
code_contractst::goto_functions
goto_functionst & goto_functions
Definition: contracts.h:137
code_contractst::get_symbol_table
symbol_tablet & get_symbol_table()
Definition: contracts.cpp:1176
goto_functions.h
goto_programt
A generic container class for the GOTO intermediate representation of one function.
Definition: goto_program.h:72
code_contractst::summarized
std::unordered_set< irep_idt > summarized
Definition: contracts.h:142
code_contractst::replace_history_parameter
void replace_history_parameter(exprt &expr, std::map< exprt, exprt > &parameter2history, source_locationt location, const irep_idt &mode, goto_programt &history, const irep_idt &id)
This function recursively identifies the "old" expressions within expr and replaces them with corresp...
Definition: contracts.cpp:560
code_contractst::enforce_contracts
void enforce_contracts(const std::set< std::string > &to_enforce, const std::set< std::string > &to_exclude_from_nondet_init={})
Turn requires & ensures into assumptions and assertions for each of the named functions.
Definition: contracts.cpp:1619
message.h
code_contractst::get_goto_functions
goto_functionst & get_goto_functions()
Definition: contracts.cpp:1181
goto_programt::targett
instructionst::iterator targett
Definition: goto_program.h:586
code_contractst::symbol_table
symbol_tablet & symbol_table
Definition: contracts.h:136
codet
Data structure for representing an arbitrary statement in a program.
Definition: std_code_base.h:28