cprover
value_set_fi_fp_removal.cpp
Go to the documentation of this file.
1 /*******************************************************************\
2 
3 Module: value_set_fi_Fp_removal
4 
5 Author: Daniel Kroening, kroening@kroening.com
6 
7 \*******************************************************************/
8 
10 
13 
15 
16 #include <util/base_type.h>
17 #include <util/c_types.h>
18 #include <util/expanding_vector.h>
19 #include <util/fresh_symbol.h>
20 #include <util/message.h>
21 #include <util/namespace.h>
22 #include <util/std_code.h>
23 #include <util/union_find.h>
24 
25 #ifdef USE_STD_STRING
26 # include <util/dstring.h>
27 #endif
28 
30  code_function_callt &function_call,
31  const namespacet &ns)
32 {
33  const code_typet &code_type =
34  to_code_type(ns.follow(function_call.function().type()));
35 
36  const code_typet::parameterst &function_parameters = code_type.parameters();
37 
38  code_function_callt::argumentst &call_arguments = function_call.arguments();
39 
40  for(std::size_t i = 0; i < function_parameters.size(); i++)
41  {
42  // casting pointers might result in more arguments than function parameters
43  if(i < call_arguments.size())
44  {
45  call_arguments[i] = typecast_exprt::conditional_cast(
46  call_arguments[i], function_parameters[i].type());
47  }
48  }
49 }
50 
52  code_function_callt &function_call,
53  goto_programt &dest,
54  goto_modelt &goto_model)
55 {
56  // are we returning anything at all?
57  if(function_call.lhs().is_nil())
58  return;
59 
60  const namespacet ns(goto_model.symbol_table);
61 
62  const code_typet &code_type =
63  to_code_type(ns.follow(function_call.function().type()));
64 
65  // type already ok?
66  if(function_call.lhs().type() == code_type.return_type())
67  return;
68 
69  const symbolt &function_symbol =
70  ns.lookup(to_symbol_expr(function_call.function()).get_identifier());
71 
72  symbolt &tmp_symbol = get_fresh_aux_symbol(
73  code_type.return_type(),
74  id2string(function_call.source_location().get_function()),
75  "tmp_return_val_" + id2string(function_symbol.base_name),
76  function_call.source_location(),
77  function_symbol.mode,
78  goto_model.symbol_table);
79 
80  const symbol_exprt tmp_symbol_expr = tmp_symbol.symbol_expr();
81 
82  exprt old_lhs = function_call.lhs();
83  function_call.lhs() = tmp_symbol_expr;
84 
86  code_assignt(old_lhs, typecast_exprt(tmp_symbol_expr, old_lhs.type()))));
87 }
88 
90  goto_programt &goto_program,
92  const std::set<symbol_exprt> &functions,
93  goto_modelt &goto_model)
94 {
95  const code_function_callt &code = to_code_function_call(target->code);
96 
97  const exprt &function = code.function();
98  PRECONDITION(function.id() == ID_dereference);
99 
100  // this better have the right type
101  code_typet call_type = to_code_type(function.type());
102 
103  // refine the type in case the forward declaration was incomplete
104  if(call_type.has_ellipsis() && call_type.parameters().empty())
105  {
106  call_type.remove_ellipsis();
107  forall_expr(it, code.arguments())
108  call_type.parameters().push_back(code_typet::parametert(it->type()));
109  }
110 
111  const exprt &pointer = to_dereference_expr(function).pointer();
112 
113  // the final target is a skip
114  goto_programt final_skip;
115 
116  goto_programt::targett t_final = final_skip.add_instruction();
117  t_final->make_skip();
118 
119  // build the calls and gotos
120 
121  goto_programt new_code_calls;
122  goto_programt new_code_gotos;
123 
124  for(const auto &fun : functions)
125  {
126  // call function
127  goto_programt::targett t1 = new_code_calls.add_instruction();
128  t1->make_function_call(code);
129  to_code_function_call(t1->code).function() = fun;
130 
131  // the signature of the function might not match precisely
132  const namespacet ns(goto_model.symbol_table);
135  to_code_function_call(t1->code), new_code_calls, goto_model);
136 
137  // goto final
138  goto_programt::targett t3 = new_code_calls.add_instruction();
139  t3->make_goto(t_final, true_exprt());
140 
141  // goto to call
142  address_of_exprt address_of(fun, pointer_type(fun.type()));
143 
144  goto_programt::targett t4 = new_code_gotos.add_instruction();
145  t4->make_goto(
146  t1,
147  equal_exprt(
148  pointer, typecast_exprt::conditional_cast(address_of, pointer.type())));
149  }
150 
151  goto_programt::targett t = new_code_gotos.add_instruction();
152  t->make_assertion(false_exprt());
153  t->source_location.set_property_class("pointer dereference");
154  t->source_location.set_comment("invalid function pointer");
155 
156  goto_programt new_code;
157 
158  // patch them all together
159  new_code.destructive_append(new_code_gotos);
160  new_code.destructive_append(new_code_calls);
161  new_code.destructive_append(final_skip);
162 
163  // set locations
164  for(auto &instruction : new_code.instructions)
165  {
166  irep_idt property_class = instruction.source_location.get_property_class();
167  irep_idt comment = instruction.source_location.get_comment();
168  instruction.source_location = target->source_location;
169  if(!property_class.empty())
170  instruction.source_location.set_property_class(property_class);
171  if(!comment.empty())
172  instruction.source_location.set_comment(comment);
173  }
174 
175  goto_programt::targett next_target = target;
176  next_target++;
177 
178  goto_program.destructive_insert(next_target, new_code);
179 
180  // We preserve the original dereferencing to possibly catch
181  // further pointer-related errors.
182  code_expressiont code_expression(function);
183  code_expression.add_source_location() = function.source_location();
184  target->code.swap(code_expression);
185  target->type = OTHER;
186 }
187 
189  goto_modelt &goto_model,
190  message_handlert &message_handler)
191 {
192  messaget message(message_handler);
193  message.status() << "Doing FI value set analysis" << messaget::eom;
194 
195  const namespacet ns(goto_model.symbol_table);
196  value_set_analysis_fit value_sets(ns);
197  value_sets(goto_model.goto_functions);
198 
199  message.status() << "Instrumenting" << messaget::eom;
200 
201  // now replace aliases by addresses
202  for(auto &f : goto_model.goto_functions.function_map)
203  {
204  for(auto target = f.second.body.instructions.begin();
205  target != f.second.body.instructions.end();
206  target++)
207  {
208  if(target->is_function_call())
209  {
210  const auto &call = to_code_function_call(target->code);
211  if(call.function().id() == ID_dereference)
212  {
213  message.status() << "CALL at " << target->source_location << ":"
214  << messaget::eom;
215 
216  const auto &pointer = to_dereference_expr(call.function()).pointer();
217  std::list<exprt> addresses;
218  value_sets.get_values(f.first, target, pointer, addresses);
219 
220  std::set<symbol_exprt> functions;
221 
222  for(const auto &address : addresses)
223  {
224  // is this a plain function address?
225  // strip leading '&'
226  if(address.id() == ID_object_descriptor)
227  {
228  const auto &od = to_object_descriptor_expr(address);
229  const auto &object = od.object();
230  if(object.type().id() == ID_code && object.id() == ID_symbol)
231  functions.insert(to_symbol_expr(object));
232  }
233  }
234 
235  for(const auto &f : functions)
236  message.status()
237  << " function: " << f.get_identifier() << messaget::eom;
238 
239  if(functions.size() > 0)
241  f.second.body, target, functions, goto_model);
242  }
243  }
244  }
245  }
246  goto_model.goto_functions.update();
247 }
messaget
Class that provides messages with a built-in verbosity 'level'.
Definition: message.h:155
value_set_fi_fp_removal
void value_set_fi_fp_removal(goto_modelt &goto_model, message_handlert &message_handler)
Builds the flow-insensitive value set for all function pointers and replaces function pointers with a...
Definition: value_set_fi_fp_removal.cpp:188
dstringt
dstringt has one field, an unsigned integer no which is an index into a static table of strings.
Definition: dstring.h:37
source_locationt::get_function
const irep_idt & get_function() const
Definition: source_location.h:56
code_typet::has_ellipsis
bool has_ellipsis() const
Definition: std_types.h:813
typecast_exprt::conditional_cast
static exprt conditional_cast(const exprt &expr, const typet &type)
Definition: std_expr.h:2021
union_find.h
code_typet::parameterst
std::vector< parametert > parameterst
Definition: std_types.h:738
fresh_symbol.h
Fresh auxiliary symbol creation.
goto_programt::add
targett add(instructiont &&instruction)
Adds a given instruction at the end.
Definition: goto_program.h:686
goto_model.h
Symbol Table + CFG.
exprt
Base class for all expressions.
Definition: expr.h:53
goto_modelt
Definition: goto_model.h:26
symbolt::base_name
irep_idt base_name
Base (non-scoped) name.
Definition: symbol.h:46
messaget::eom
static eomt eom
Definition: message.h:297
goto_functionst::function_map
function_mapt function_map
Definition: goto_functions.h:27
symbol_exprt
Expression to hold a symbol (variable)
Definition: std_expr.h:82
namespace.h
equal_exprt
Equality.
Definition: std_expr.h:1190
code_typet::remove_ellipsis
void remove_ellipsis()
Definition: std_types.h:842
code_function_callt::lhs
exprt & lhs()
Definition: std_code.h:1208
goto_programt::make_assignment
static instructiont make_assignment(const code_assignt &_code, const source_locationt &l=source_locationt::nil())
Create an assignment instruction.
Definition: goto_program.h:1024
value_set_analysis_fit
Definition: value_set_analysis_fi.h:23
fix_return_type
void fix_return_type(code_function_callt &function_call, goto_programt &dest, goto_modelt &goto_model)
Definition: value_set_fi_fp_removal.cpp:51
namespacet
A namespacet is essentially one or two symbol tables bound together, to allow for symbol lookups in t...
Definition: namespace.h:92
message
static const char * message(const static_verifier_resultt::statust &status)
Makes a status message string from a status.
Definition: static_verifier.cpp:74
goto_programt::add_instruction
targett add_instruction()
Adds an instruction at the end.
Definition: goto_program.h:694
exprt::type
typet & type()
Return the type of the expression.
Definition: expr.h:81
namespacet::lookup
bool lookup(const irep_idt &name, const symbolt *&symbol) const override
See documentation for namespace_baset::lookup().
Definition: namespace.cpp:140
code_function_callt
codet representation of a function call statement.
Definition: std_code.h:1183
to_code_type
const code_typet & to_code_type(const typet &type)
Cast a typet to a code_typet.
Definition: std_types.h:946
symbolt::mode
irep_idt mode
Language mode.
Definition: symbol.h:49
base_type.h
Base Type Computation.
goto_programt::destructive_insert
void destructive_insert(const_targett target, goto_programt &p)
Inserts the given program p before target.
Definition: goto_program.h:677
id2string
const std::string & id2string(const irep_idt &d)
Definition: irep.h:44
PRECONDITION
#define PRECONDITION(CONDITION)
Definition: invariant.h:464
symbol_exprt::get_identifier
const irep_idt & get_identifier() const
Definition: std_expr.h:111
dereference_exprt::pointer
exprt & pointer()
Definition: std_expr.h:2901
symbolt::symbol_expr
class symbol_exprt symbol_expr() const
Produces a symbol_exprt for a symbol.
Definition: symbol.cpp:122
to_object_descriptor_expr
const object_descriptor_exprt & to_object_descriptor_expr(const exprt &expr)
Cast an exprt to an object_descriptor_exprt.
Definition: std_expr.h:1898
value_set_fi_fp_removal.h
flow insensitive value set function pointer removal
pointer_type
pointer_typet pointer_type(const typet &subtype)
Definition: c_types.cpp:243
irept::swap
void swap(irept &irep)
Definition: irep.h:463
to_symbol_expr
const symbol_exprt & to_symbol_expr(const exprt &expr)
Cast an exprt to a symbol_exprt.
Definition: std_expr.h:177
code_typet
Base type of functions.
Definition: std_types.h:736
OTHER
@ OTHER
Definition: goto_program.h:37
irept::is_nil
bool is_nil() const
Definition: irep.h:398
message_handlert
Definition: message.h:28
to_code_function_call
const code_function_callt & to_code_function_call(const codet &code)
Definition: std_code.h:1294
code_function_callt::argumentst
exprt::operandst argumentst
Definition: std_code.h:1192
dstringt::empty
bool empty() const
Definition: dstring.h:88
false_exprt
The Boolean constant false.
Definition: std_expr.h:3964
value_set_analysis_fi.h
Value Set Propagation (flow insensitive)
code_typet::parameters
const parameterst & parameters() const
Definition: std_types.h:857
expanding_vector.h
std_code.h
code_function_callt::arguments
argumentst & arguments()
Definition: std_code.h:1228
goto_programt::destructive_append
void destructive_append(goto_programt &p)
Appends the given program p to *this. p is destroyed.
Definition: goto_program.h:669
goto_programt::instructions
instructionst instructions
The list of instructions in the goto program.
Definition: goto_program.h:585
dstring.h
Container for C-Strings.
to_dereference_expr
const dereference_exprt & to_dereference_expr(const exprt &expr)
Cast an exprt to a dereference_exprt.
Definition: std_expr.h:2944
goto_modelt::goto_functions
goto_functionst goto_functions
GOTO functions.
Definition: goto_model.h:33
namespace_baset::follow
const typet & follow(const typet &) const
Resolve type symbol to the type it points to.
Definition: namespace.cpp:51
symbolt
Symbol table entry.
Definition: symbol.h:28
goto_functionst::update
void update()
Definition: goto_functions.h:81
code_typet::parametert
Definition: std_types.h:753
goto_programt
A generic container class for the GOTO intermediate representation of one function.
Definition: goto_program.h:73
remove_function_pointer
void remove_function_pointer(goto_programt &goto_program, goto_programt::targett target, const std::set< symbol_exprt > &functions, goto_modelt &goto_model)
Definition: value_set_fi_fp_removal.cpp:89
code_typet::return_type
const typet & return_type() const
Definition: std_types.h:847
forall_expr
#define forall_expr(it, expr)
Definition: expr.h:29
address_of_exprt
Operator to return the address of an object.
Definition: std_expr.h:2786
exprt::add_source_location
source_locationt & add_source_location()
Definition: expr.h:259
value_set_analysis_fit::get_values
void get_values(const irep_idt &function_id, locationt l, const exprt &expr, std::list< exprt > &dest) override
Definition: value_set_analysis_fi.h:62
typecast_exprt
Semantic type conversion.
Definition: std_expr.h:2013
code_assignt
A codet representing an assignment in the program.
Definition: std_code.h:295
message.h
true_exprt
The Boolean constant true.
Definition: std_expr.h:3955
comment
static std::string comment(const rw_set_baset::entryt &entry, bool write)
Definition: race_check.cpp:109
remove_function_pointers.h
Remove Indirect Function Calls.
exprt::source_location
const source_locationt & source_location() const
Definition: expr.h:254
goto_modelt::symbol_table
symbol_tablet symbol_table
Symbol table.
Definition: goto_model.h:30
c_types.h
fix_argument_types
void fix_argument_types(code_function_callt &function_call, const namespacet &ns)
Definition: value_set_fi_fp_removal.cpp:29
get_fresh_aux_symbol
symbolt & get_fresh_aux_symbol(const typet &type, const std::string &name_prefix, const std::string &basename_prefix, const source_locationt &source_location, const irep_idt &symbol_mode, const namespacet &ns, symbol_table_baset &symbol_table)
Installs a fresh-named symbol with respect to the given namespace ns with the requested name pattern ...
Definition: fresh_symbol.cpp:32
goto_programt::targett
instructionst::iterator targett
Definition: goto_program.h:579
code_function_callt::function
exprt & function()
Definition: std_code.h:1218
code_expressiont
codet representation of an expression statement.
Definition: std_code.h:1810