CBMC
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
dfcc_pointer_equals.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: Jan 2025
7
8\*******************************************************************/
9
10#include "dfcc_pointer_equals.h"
11
12#include <util/c_types.h>
13#include <util/cprover_prefix.h>
14#include <util/expr_iterator.h>
15#include <util/pointer_expr.h>
16#include <util/prefix.h>
17#include <util/replace_expr.h>
18#include <util/std_code.h>
19#include <util/std_expr.h>
20#include <util/suffix.h>
21#include <util/symbol.h>
22
23#include "dfcc_cfg_info.h"
24#include "dfcc_library.h"
25
27 dfcc_libraryt &library,
28 message_handlert &message_handler)
29 : library(library), message_handler(message_handler), log(message_handler)
30{
31}
32
34 goto_programt &program,
35 dfcc_cfg_infot cfg_info)
36{
38 program,
39 program.instructions.begin(),
40 program.instructions.end(),
41 cfg_info);
42}
43
45 goto_programt &program,
48 dfcc_cfg_infot cfg_info)
49{
50 auto &target = first_instruction;
51 while(target != last_instruction)
52 {
53 if(target->is_function_call())
54 {
55 const auto &function = target->call_function();
56
57 if(function.id() == ID_symbol)
58 {
59 const irep_idt &fun_name = to_symbol_expr(function).get_identifier();
60
61 if(has_prefix(id2string(fun_name), CPROVER_PREFIX "pointer_equals"))
62 {
63 // add address on first operand
64 target->call_arguments()[0] =
65 address_of_exprt(target->call_arguments()[0]);
66
67 // fix the function name.
68 to_symbol_expr(target->call_function())
69 .set_identifier(
71
72 // pass the may_fail flag
73 if(function.source_location().get_bool("no_fail"))
74 target->call_arguments().push_back(false_exprt());
75 else
76 target->call_arguments().push_back(true_exprt());
77
78 // pass the write_set
79 target->call_arguments().push_back(cfg_info.get_write_set(target));
80 }
81 }
82 }
83 target++;
84 }
85}
86
88{
89private:
90 std::vector<exprt *> equality_exprs_to_transform;
91
92public:
93 void visit_expr(exprt &expr)
94 {
95 if(expr.id() == ID_equal)
96 {
98
99 // Check if both operands are pointers
100 if(
101 equal_expr.lhs().type().id() == ID_pointer &&
102 equal_expr.rhs().type().id() == ID_pointer)
103 {
104 equality_exprs_to_transform.push_back(&expr);
105 }
106 }
107 }
108
109 // Apply the transformations after visiting
111 {
115 bool_typet());
116
118 CPROVER_PREFIX "pointer_equals", pointer_equals_type);
119
121 {
123
124 // Create the function call to __CPROVER_pointer_equals
125 // Add the original equality operands as arguments
128 {equal_expr.lhs(), equal_expr.rhs()},
129 bool_typet(),
130 equal_expr.source_location());
131
132 result.arguments().push_back(equal_expr.lhs());
133 result.arguments().push_back(equal_expr.rhs());
134
135 // Set the type of the function call
136 result.type() = bool_typet();
137
138 // Replace the original expression
139 *expr_ptr = result;
140 }
141 }
142};
143
144// Usage function
146{
148
149 // First collect all equality expressions
150 expr.visit(visitor);
151
152 // Then transform them
153 visitor.transform();
154}
empty_typet void_type()
Definition c_types.cpp:245
pointer_typet pointer_type(const typet &subtype)
Definition c_types.cpp:235
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
The Boolean type.
Definition std_types.h:36
Base type of functions.
Definition std_types.h:583
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_pointer_equalst(dfcc_libraryt &library, message_handlert &message_handler)
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...
dstringt has one field, an unsigned integer no which is an index into a static table of strings.
Definition dstring.h:38
Equality.
Definition std_expr.h:1366
Base class for all expressions.
Definition expr.h:56
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...
Definition expr.cpp:237
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
std::vector< exprt * > equality_exprs_to_transform
A side_effect_exprt representation of a function call side effect.
Definition std_code.h:1692
exprt::operandst & arguments()
Definition std_code.h:1718
Expression to hold a symbol (variable)
Definition std_expr.h:131
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.
void rewrite_equal_exprt_to_pointer_equals(exprt &expr)
Rewrites equal_exprt over pointers into calls to the front-end function __CPROVER_pointer_equals,...
Instruments occurrences of pointer_equals predicates in programs encoding requires and ensures clause...
Forward depth-first search iterators These iterators' copy operations are expensive,...
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.
API to expression classes.
const symbol_exprt & to_symbol_expr(const exprt &expr)
Cast an exprt to a symbol_exprt.
Definition std_expr.h:272
const equal_exprt & to_equal_expr(const exprt &expr)
Cast an exprt to an equal_exprt.
Definition std_expr.h:1407
Symbol table entry.