cprover
Loading...
Searching...
No Matches
dfcc_spec_functions.cpp
Go to the documentation of this file.
1/*******************************************************************\
2
3Module: Dynamic frame condition checking
4
5Author: Remi Delmas, delmarsd@amazon.com
6
7\*******************************************************************/
8
11
12#include <util/format_expr.h>
13#include <util/namespace.h>
14
16
18
19#include "dfcc_library.h"
21
23 goto_modelt &goto_model,
24 message_handlert &message_handler,
25 dfcc_libraryt &library)
26 : goto_model(goto_model),
27 message_handler(message_handler),
28 log(message_handler),
29 library(library),
30 ns(goto_model.symbol_table)
31{
32}
33
35{
37 expr.id() == ID_typecast && expr.type().id() == ID_pointer &&
38 expr.operands().at(0).id() == ID_address_of,
39 "target expression must be of the form `cast(address_of(target), empty*)`");
40
41 return expr.operands().at(0).operands().at(0).type();
42}
43
45 const irep_idt &function_id,
47 std::size_t &nof_targets)
48{
51 "DFCC: havoc function id '" + id2string(havoc_function_id) +
52 "' already exists");
53
54 const auto &function_symbol =
56
57 // create the code type that goes on the function symbol
62
63 // create the havoc function symbol
71 havoc_function_symbol.location = function_symbol.location;
72 havoc_function_symbol.set_compiled();
76 "DFCC: could not insert havoc function '" + id2string(havoc_function_id) +
77 "' in the symbol table");
78
79 // create the write_set symbol used as input by the havoc function
83 "__write_set_to_havoc",
87 .parameters()[0]
88 .set_identifier(write_set_symbol.name);
89
90 // create new goto_function
92 dummy_havoc_function.parameter_identifiers = {write_set_symbol.name};
95
96 // body will be filled with instructions
97 auto &havoc_program =
99
101 goto_model.goto_functions.function_map.at(function_id).body;
102
106 write_set_symbol.symbol_expr(),
109
111
113
114 std::set<irep_idt> no_body;
115 std::set<irep_idt> missing_function;
116 std::set<irep_idt> recursive_call;
117 std::set<irep_idt> not_enough_arguments;
121 no_body,
126 INVARIANT(
127 no_body.empty(),
128 "no body warnings when inlining " + id2string(havoc_function_id));
129 INVARIANT(
130 missing_function.empty(),
131 "missing function warnings when inlining " + id2string(havoc_function_id));
132 INVARIANT(
133 recursive_call.empty(),
134 "recursive calls when inlining " + id2string(havoc_function_id));
135 INVARIANT(
136 not_enough_arguments.empty(),
137 "not enough arguments when inlining " + id2string(havoc_function_id));
138
140
142}
143
145 const irep_idt &function_id,
149 std::size_t &nof_targets)
150{
151 // index of the CAR to havoc in the write set
152 std::size_t next_idx = 0;
153
154 // iterate on the body of the original function and emit one havoc instruction
155 // per target
157 {
158 if(ins_it->is_function_call())
159 {
160 if(ins_it->call_function().id() != ID_symbol)
161 {
163 "Function pointer calls are not supported in assigns clauses: '" +
164 from_expr(ns, function_id, ins_it->call_function()) +
165 "' called in function '" + id2string(function_id) + "'",
166 ins_it->source_location());
167 }
168
169 const irep_idt &callee_id =
170 to_symbol_expr(ins_it->call_function()).get_identifier();
171
172 // Only process built-in functions that represent assigns clause targets,
173 // and error-out on any other function call
174
175 // Find the corresponding instrumentation hook
177 INVARIANT(
178 hook_opt.has_value(),
179 "dfcc_spec_functionst::generate_havoc_instructions: function calls "
180 "must be inlined before calling this function");
181
182 // Use same source location as original call
183 source_locationt location(ins_it->source_location());
184 auto hook = hook_opt.value();
186 library.dfcc_fun_symbol.at(hook).symbol_expr(),
187 {write_set_to_havoc, from_integer(next_idx, size_type())});
188
190 {
191 // ```
192 // DECL __havoc_target;
193 // CALL __havoc_target = havoc_hook(set, next_idx);
194 // IF !__havoc_target GOTO label;
195 // ASSIGN *__havoc_target = nondet(target_type);
196 // label: DEAD __havoc_target;
197 // ```
198 // declare a local var to store targets havoced via nondet assignment
199 auto &target_type = get_target_type(ins_it->call_arguments().at(0));
200
203 pointer_type(target_type),
204 function_id,
205 "__havoc_target",
206 location);
207
209
210 call.lhs() = target_expr;
212
213 auto goto_instruction =
216
217 // create nondet assignment to the target
218 side_effect_expr_nondett nondet(target_type, location);
221 target_expr, pointer_type(target_type))},
222 nondet,
223 location));
224 auto label =
226 goto_instruction->complete_goto(label);
227 }
228 else if(
231 {
232 // ```
233 // CALL havoc_hook(set, next_idx);
234 // ```
236 }
237 else
238 {
240 }
241 ++next_idx;
242 }
243 }
245}
246
248 const irep_idt &function_id,
249 std::size_t &nof_targets)
250{
251 auto &goto_function = goto_model.goto_functions.function_map.at(function_id);
252
253 // add write_set parameter
257 function_id,
258 "__write_set_to_fill",
260 .symbol_expr();
261
265 goto_function.body,
267
269
270 goto_model.goto_functions.function_map.at(function_id).make_hidden();
271}
272
275 const irep_idt &language_mode,
276 goto_programt &program,
277 std::size_t &nof_targets)
278{
279 // counts the number of calls to built-ins to get an over approximation
280 // of the size of the set
281 std::size_t next_idx = 0;
282
283 // rewrite calls
285 {
286 if(ins_it->is_function_call())
287 {
288 if(ins_it->call_function().id() != ID_symbol)
289 {
291 "Function pointer calls are not supported in assigns clauses '" +
292 from_expr_using_mode(ns, language_mode, ins_it->call_function()) +
293 "'",
294 ins_it->source_location());
295 }
296
297 const irep_idt &callee_id =
298 to_symbol_expr(ins_it->call_function()).get_identifier();
299
300 // Only process built-in functions that specify assignable targets
301 // and error-out on any other function call
302
303 // Find the corresponding instrumentation hook
304 INVARIANT(
306 "dfcc_spec_functionst::to_spec_assigns_function: function calls must "
307 "be inlined before calling this function");
308
310 // redirect the call to the hook
311 ins_it->call_function() = library.dfcc_fun_symbol.at(hook).symbol_expr();
312 // insert insertion index argument
313 ins_it->call_arguments().insert(
314 ins_it->call_arguments().begin(), from_integer(next_idx, size_type()));
315 // insert write set argument
316 ins_it->call_arguments().insert(
317 ins_it->call_arguments().begin(), write_set_to_fill);
318
319 // remove the is_pointer_to_pointer argument which is not used in the
320 // hook for insert assignable
322 ins_it->call_arguments().pop_back();
323
324 ++next_idx;
325 }
326 }
328}
329
331 const irep_idt &function_id,
332 std::size_t &nof_targets)
333{
334 auto &goto_function = goto_model.goto_functions.function_map.at(function_id);
335
336 // add __dfcc_set parameter
337 const exprt &write_set_to_fill =
340 function_id,
341 "__write_set_to_fill",
343 .symbol_expr();
344
348 goto_function.body,
350
352
353 goto_model.goto_functions.function_map.at(function_id).make_hidden();
354}
355
358 const irep_idt &language_mode,
359 goto_programt &program,
360 std::size_t &nof_targets)
361{
362 // counts the number of calls to the `freeable` builtin
363 std::size_t next_idx = 0;
365 {
366 if(ins_it->is_function_call())
367 {
368 if(ins_it->call_function().id() != ID_symbol)
369 {
371 "Function pointer calls are not supported in frees clauses: '" +
372 from_expr_using_mode(ns, language_mode, ins_it->call_function()) +
373 "'",
374 ins_it->source_location());
375 }
376
377 const irep_idt &callee_id =
378 to_symbol_expr(ins_it->call_function()).get_identifier();
379
380 // only process the built-in `freeable` function
381 // error out on any other function call
382 INVARIANT(
383 callee_id == CPROVER_PREFIX "freeable",
384 "dfcc_spec_functionst::to_spec_frees_function: function calls must "
385 "be inlined before calling this function");
386
387 ins_it->call_function() =
389 .symbol_expr();
390 ins_it->call_arguments().insert(
391 ins_it->call_arguments().begin(), write_set_to_fill);
392 ++next_idx;
393 }
394 }
395
397}
constant_exprt from_integer(const mp_integer &int_value, const typet &type)
unsignedbv_typet size_type()
Definition c_types.cpp:55
pointer_typet pointer_type(const typet &subtype)
Definition c_types.cpp:240
ait supplies three of the four components needed: an abstract interpreter (in this case handling func...
Definition ai.h:563
goto_instruction_codet representation of a function call statement.
Base type of functions.
Definition std_types.h:539
Operator to dereference a pointer.
Class interface to library types and functions defined in cprover_contracts.c.
bool is_front_end_builtin(const irep_idt &function_id) const
Returns true iff the given function_id is one of __CPROVER_assignable, __CPROVER_object_whole,...
dfcc_funt get_hook(const irep_idt &function_id) const
Returns the library instrumentation hook for the given front-end function.
std::map< dfcc_typet, typet > dfcc_type
Maps enum values to the actual types (dynamically loaded)
optionalt< dfcc_funt > get_havoc_hook(const irep_idt &function_id) const
Returns the library instrumentation hook for the given built-in.
std::map< dfcc_funt, symbolt > dfcc_fun_symbol
Maps enum values to the actual function symbols (dynamically loaded)
void to_spec_assigns_function(const irep_idt &function_id, std::size_t &nof_targets)
Transforms (in place) a function.
void to_spec_frees_instructions(const exprt &write_set_to_fill, const irep_idt &language_mode, goto_programt &program, std::size_t &nof_targets)
Rewrites in place program expressed in terms of built-ins specifying freeable targets declaratively u...
void generate_havoc_function(const irep_idt &function_id, const irep_idt &havoc_function_id, std::size_t &nof_targets)
From a function:
void generate_havoc_instructions(const irep_idt &function_id, const goto_programt &original_program, const exprt &write_set_to_havoc, goto_programt &havoc_program, std::size_t &nof_targets)
Translates original_program that specifies assignable targets into a program that havocs the targets.
message_handlert & message_handler
void to_spec_assigns_instructions(const exprt &write_set_to_fill, const irep_idt &language_mode, goto_programt &program, std::size_t &nof_targets)
Rewrites in place program expressed in terms of built-ins specifying assignable targets declaratively...
const typet & get_target_type(const exprt &expr)
Extracts the type of an assigns clause target expression The expression must be of the form: expr = c...
dfcc_spec_functionst(goto_modelt &goto_model, message_handlert &message_handler, dfcc_libraryt &library)
void to_spec_frees_function(const irep_idt &function_id, std::size_t &nof_targets)
Transforms (in place) a function.
dstringt has one field, an unsigned integer no which is an index into a static table of strings.
Definition dstring.h:39
The empty type.
Definition std_types.h:51
Base class for all expressions.
Definition expr.h:56
typet & type()
Return the type of the expression.
Definition expr.h:84
operandst & operands()
Definition expr.h:94
function_mapt function_map
A goto function, consisting of function body (see body) and parameter identifiers (see parameter_iden...
symbol_tablet symbol_table
Symbol table.
Definition goto_model.h:31
goto_functionst goto_functions
GOTO functions.
Definition goto_model.h:34
A generic container class for the GOTO intermediate representation of one function.
static instructiont make_dead(const symbol_exprt &symbol, const source_locationt &l=source_locationt::nil())
static instructiont make_end_function(const source_locationt &l=source_locationt::nil())
static instructiont make_assignment(const code_assignt &_code, const source_locationt &l=source_locationt::nil())
Create an assignment instruction.
static instructiont make_function_call(const code_function_callt &_code, const source_locationt &l=source_locationt::nil())
Create a function call instruction.
static instructiont make_decl(const symbol_exprt &symbol, const source_locationt &l=source_locationt::nil())
static instructiont make_incomplete_goto(const exprt &_cond, const source_locationt &l=source_locationt::nil())
Thrown when we can't handle something in an input source file.
const irep_idt & id() const
Definition irep.h:396
A side_effect_exprt that returns a non-deterministically chosen value.
Definition std_code.h:1520
symbolt & get_writeable_ref(const irep_idt &name)
Find a symbol in the symbol table for read-write access.
bool has_symbol(const irep_idt &name) const
Check whether a symbol exists in the symbol table.
bool add(const symbolt &symbol)
Add a new symbol to the symbol table.
Symbol table entry.
Definition symbol.h:28
typet type
Type of symbol.
Definition symbol.h:31
static exprt conditional_cast(const exprt &expr, const typet &type)
Definition std_expr.h:2025
The type of an expression, extends irept.
Definition type.h:29
#define CPROVER_PREFIX
Dynamic frame condition checking library loading.
@ WRITE_SET_INSERT_ASSIGNABLE
@ WRITE_SET_HAVOC_SLICE
@ WRITE_SET_HAVOC_GET_ASSIGNABLE_TARGET
@ WRITE_SET_ADD_FREEABLE
@ WRITE_SET_HAVOC_OBJECT_WHOLE
@ CAR_SET_PTR
type of pointers to sets of CAR
@ WRITE_SET_PTR
type of pointers to descriptors of assignable/freeable sets of locations
Enumeration representing the instrumentation mode for loop contracts.
Translate functions that specify assignable and freeable targets declaratively into active functions ...
Program Transformation.
Symbol Table + CFG.
#define forall_goto_program_instructions(it, program)
#define Forall_goto_program_instructions(it, program)
const std::string & id2string(const irep_idt &d)
Definition irep.h:47
std::string from_expr(const namespacet &ns, const irep_idt &identifier, const exprt &expr)
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...
#define UNREACHABLE
This should be used to mark dead code.
Definition invariant.h:525
#define INVARIANT(CONDITION, REASON)
This macro uses the wrapper function 'invariant_violated_string'.
Definition invariant.h:423
const symbol_exprt & to_symbol_expr(const exprt &expr)
Cast an exprt to a symbol_exprt.
Definition std_expr.h:222
const code_typet & to_code_type(const typet &type)
Cast a typet to a code_typet.
Definition std_types.h:744
static symbolt & get_function_symbol(symbol_table_baset &, const irep_idt &function_id)
Returns the symbolt for function_id.
static void inline_function(goto_modelt &goto_model, const irep_idt &function_id, message_handlert &message_handler)
Inlines the given function, aborts on recursive calls during inlining.
static const exprt make_null_check_expr(const exprt &ptr)
Returns the expression expr == NULL.
static const symbolt & create_new_parameter_symbol(symbol_table_baset &, const irep_idt &function_id, const std::string &base_name, const typet &type)
Creates a new parameter symbol for the given function_id.
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...