CBMC
label_function_pointer_call_sites.cpp
Go to the documentation of this file.
1 /*******************************************************************\
2 Module: Label function pointer call sites
3 Author: Diffblue Ltd.
4 \*******************************************************************/
5 
8 
10 
11 #include <util/pointer_expr.h>
12 
13 #include "goto_model.h"
14 
16 {
17  for(auto &goto_function : goto_model.goto_functions.function_map)
18  {
19  std::size_t function_pointer_call_counter = 0;
20 
22  goto_function.second,
23  [](const goto_programt::targett it) {
24  return it->is_function_call() &&
25  can_cast_expr<dereference_exprt>(it->call_function());
26  },
27  [&](goto_programt::targett &it) {
28  auto const &function_pointer_dereference =
29  to_dereference_expr(it->call_function());
30  auto const &source_location = it->source_location();
31  auto const &goto_function_symbol_mode =
32  goto_model.symbol_table.lookup_ref(goto_function.first).mode;
33 
34  auto const call_site_symbol_name =
35  irep_idt{id2string(goto_function.first) + ".function_pointer_call." +
36  std::to_string(++function_pointer_call_counter)};
37 
38  // insert new function pointer variable into the symbol table
39  goto_model.symbol_table.insert([&] {
40  symbolt function_call_site_symbol{};
41  function_call_site_symbol.name = function_call_site_symbol.base_name =
42  function_call_site_symbol.pretty_name = call_site_symbol_name;
43  function_call_site_symbol.type =
44  function_pointer_dereference.pointer().type();
45  function_call_site_symbol.location = it->source_location();
46  function_call_site_symbol.is_lvalue = true;
47  function_call_site_symbol.mode = goto_function_symbol_mode;
48  return function_call_site_symbol;
49  }());
50 
51  auto const new_function_pointer =
52  goto_model.symbol_table.lookup_ref(call_site_symbol_name)
53  .symbol_expr();
54 
55  // add a DECL instruction for the function pointer variable
56  auto decl_instruction =
57  goto_programt::make_decl(new_function_pointer, source_location);
58 
59  goto_function.second.body.insert_before_swap(it, decl_instruction);
60  ++it;
61 
62  // add assignment to the new variable
63  auto assign_instruction = goto_programt::make_assignment(
64  code_assignt{new_function_pointer,
65  function_pointer_dereference.pointer()},
66  source_location);
67 
68  goto_function.second.body.insert_before_swap(it, assign_instruction);
69  ++it;
70 
71  // transform original call into a call to the new variable
72  it->call_function() = dereference_exprt{new_function_pointer};
73  ++it;
74 
75  // add a DEAD instruction for the new variable
76  auto dead_instruction =
77  goto_programt::make_dead(new_function_pointer, source_location);
78  goto_function.second.body.insert_before_swap(it, dead_instruction);
79  // the iterator now points to the DEAD instruction and will be
80  // incremented by the outer loop
81  });
82  }
83 }
A goto_instruction_codet representing an assignment in the program.
Operator to dereference a pointer.
Definition: pointer_expr.h:834
function_mapt function_map
symbol_tablet symbol_table
Symbol table.
Definition: goto_model.h:31
goto_functionst goto_functions
GOTO functions.
Definition: goto_model.h:34
const exprt & call_function() const
Get the function that is called for FUNCTION_CALL.
Definition: goto_program.h:272
static instructiont make_dead(const symbol_exprt &symbol, const source_locationt &l=source_locationt::nil())
Definition: goto_program.h:971
instructionst::iterator targett
Definition: goto_program.h:614
static instructiont make_assignment(const code_assignt &_code, const source_locationt &l=source_locationt::nil())
Create an assignment instruction.
static instructiont make_decl(const symbol_exprt &symbol, const source_locationt &l=source_locationt::nil())
Definition: goto_program.h:964
const symbolt & lookup_ref(const irep_idt &name) const
Find a symbol in the symbol table for read-only access.
virtual std::pair< symbolt &, bool > insert(symbolt symbol) override
Author: Diffblue Ltd.
class symbol_exprt symbol_expr() const
Produces a symbol_exprt for a symbol.
Definition: symbol.cpp:121
Symbol Table + CFG.
void for_each_instruction_if(GotoFunctionT &&goto_function, PredicateT predicate, HandlerT handler)
void label_function_pointer_call_sites(goto_modelt &goto_model)
This ensures that call instructions can be only one of two things:
Label function pointer call sites across a goto model.
API to expression classes for Pointers.