CBMC
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
dfcc_obeys_contract.cpp
Go to the documentation of this file.
1/*******************************************************************\
2
3Module: Dynamic frame condition checking for function contracts
4
5Author: Remi Delmas, delmasrd@amazon.com
6Date: August 2022
7
8\*******************************************************************/
9
10#include "dfcc_obeys_contract.h"
11
12#include <util/cprover_prefix.h>
13#include <util/pointer_expr.h>
14#include <util/prefix.h>
15#include <util/suffix.h>
16#include <util/symbol.h>
17
19
20#include "dfcc_cfg_info.h"
21#include "dfcc_library.h"
22
24 dfcc_libraryt &library,
25 message_handlert &message_handler)
26 : library(library), message_handler(message_handler), log(message_handler)
27{
28}
29
31 goto_programt &program,
32 dfcc_cfg_infot &cfg_info,
33 std::set<irep_idt> &function_pointer_contracts)
34{
36 program,
37 program.instructions.begin(),
38 program.instructions.end(),
39 cfg_info,
40 function_pointer_contracts);
41}
42
44 const exprt &expr,
45 std::set<irep_idt> &function_pointer_contracts)
46{
47 PRECONDITION(expr.id() == ID_typecast || expr.id() == ID_address_of);
48
49 if(expr.id() == ID_typecast)
50 {
51 get_contract_name(to_typecast_expr(expr).op(), function_pointer_contracts);
52 }
53 else
54 {
56 to_address_of_expr(expr).object().id() == ID_symbol,
57 "symbol expression expected");
58 function_pointer_contracts.insert(
59 to_symbol_expr(to_address_of_expr(expr).object()).get_identifier());
60 }
61}
62
64 goto_programt &program,
67 dfcc_cfg_infot &cfg_info,
68 std::set<irep_idt> &function_pointer_contracts)
69{
70 for(auto &target = first_instruction; target != last_instruction; target++)
71 {
72 if(target->is_function_call())
73 {
74 const auto &function = target->call_function();
75
76 if(function.id() == ID_symbol)
77 {
78 const irep_idt &fun_name = to_symbol_expr(function).get_identifier();
79
80 if(has_prefix(id2string(fun_name), CPROVER_PREFIX "obeys_contract"))
81 {
82 // add address_of on first operand
83 target->call_arguments()[0] =
84 address_of_exprt(target->call_arguments()[0]);
85
86 // fix the function name.
87 to_symbol_expr(target->call_function())
88 .set_identifier(
90
91 // pass the may_fail flag
92 if(function.source_location().get_bool("no_fail"))
93 target->call_arguments().push_back(false_exprt());
94 else
95 target->call_arguments().push_back(true_exprt());
96
97 // pass the write_set
98 target->call_arguments().push_back(cfg_info.get_write_set(target));
99
100 // record discovered function contract
102 target->call_arguments()[1], function_pointer_contracts);
103 }
104 }
105 }
106 }
107}
Operator to return the address of an object.
ait supplies three of the four components needed: an abstract interpreter (in this case handling func...
Definition ai.h:562
Computes natural loops, enforces normal form conditions, computes the nesting graph,...
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,...
Class interface to library types and functions defined in cprover_contracts.c.
std::map< dfcc_funt, symbolt > dfcc_fun_symbol
Maps enum values to the actual function symbols (dynamically loaded)
dfcc_obeys_contractt(dfcc_libraryt &library, message_handlert &message_handler)
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...
void get_contract_name(const exprt &expr, std::set< irep_idt > &function_pointer_contracts)
Extracts the name from the second argument of a call to obeys_contract (modulo any intermediate typec...
dstringt has one field, an unsigned integer no which is an index into a static table of strings.
Definition dstring.h:38
Base class for all expressions.
Definition expr.h:56
The Boolean constant false.
Definition std_expr.h:3199
A generic container class for the GOTO intermediate representation of one function.
instructionst instructions
The list of instructions in the goto program.
instructionst::iterator targett
const irep_idt & id() const
Definition irep.h:388
The Boolean constant true.
Definition std_expr.h:3190
bool has_prefix(const std::string &s, const std::string &prefix)
Definition converter.cpp:13
#define CPROVER_PREFIX
Class that computes CFG information about the loop structure of a GOTO function for the purpose of dy...
Dynamic frame condition checking library loading.
Instruments occurrences of obeys_contract predicates in programs encoding requires and ensures clause...
const std::string & id2string(const irep_idt &d)
Definition irep.h:44
double log(double x)
Definition math.c:2449
API to expression classes for Pointers.
const address_of_exprt & to_address_of_expr(const exprt &expr)
Cast an exprt to an address_of_exprt.
#define PRECONDITION_WITH_DIAGNOSTICS(CONDITION,...)
Definition invariant.h:464
#define PRECONDITION(CONDITION)
Definition invariant.h:463
const typecast_exprt & to_typecast_expr(const exprt &expr)
Cast an exprt to a typecast_exprt.
Definition std_expr.h:2107
const symbol_exprt & to_symbol_expr(const exprt &expr)
Cast an exprt to a symbol_exprt.
Definition std_expr.h:272
Symbol table entry.