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 
12 
14 
15 #include <util/c_types.h>
16 #include <util/fresh_symbol.h>
17 #include <util/message.h>
18 #include <util/namespace.h>
19 #include <util/pointer_expr.h>
20 #include <util/std_code.h>
21 
22 #ifdef USE_STD_STRING
23 # include <util/dstring.h>
24 #endif
25 
27  code_function_callt &function_call,
28  const namespacet &ns)
29 {
30  const code_typet &code_type =
31  to_code_type(ns.follow(function_call.function().type()));
32 
33  const code_typet::parameterst &function_parameters = code_type.parameters();
34 
35  code_function_callt::argumentst &call_arguments = function_call.arguments();
36 
37  for(std::size_t i = 0; i < function_parameters.size(); i++)
38  {
39  // casting pointers might result in more arguments than function parameters
40  if(i < call_arguments.size())
41  {
42  call_arguments[i] = typecast_exprt::conditional_cast(
43  call_arguments[i], function_parameters[i].type());
44  }
45  }
46 }
47 
49  code_function_callt &function_call,
50  goto_programt &dest,
51  goto_modelt &goto_model)
52 {
53  // are we returning anything at all?
54  if(function_call.lhs().is_nil())
55  return;
56 
57  const namespacet ns(goto_model.symbol_table);
58 
59  const code_typet &code_type =
60  to_code_type(ns.follow(function_call.function().type()));
61 
62  // type already ok?
63  if(function_call.lhs().type() == code_type.return_type())
64  return;
65 
66  const symbolt &function_symbol =
67  ns.lookup(to_symbol_expr(function_call.function()).get_identifier());
68 
69  symbolt &tmp_symbol = get_fresh_aux_symbol(
70  code_type.return_type(),
71  id2string(function_call.source_location().get_function()),
72  "tmp_return_val_" + id2string(function_symbol.base_name),
73  function_call.source_location(),
74  function_symbol.mode,
75  goto_model.symbol_table);
76 
77  const symbol_exprt tmp_symbol_expr = tmp_symbol.symbol_expr();
78 
79  exprt old_lhs = function_call.lhs();
80  function_call.lhs() = tmp_symbol_expr;
81 
83  code_assignt(old_lhs, typecast_exprt(tmp_symbol_expr, old_lhs.type()))));
84 }
85 
87  goto_programt &goto_program,
89  const std::set<symbol_exprt> &functions,
90  goto_modelt &goto_model)
91 {
92  const exprt &function = as_const(*target).call_function();
93  PRECONDITION(function.id() == ID_dereference);
94 
95  // this better have the right type
96  code_typet call_type = to_code_type(function.type());
97 
98  // refine the type in case the forward declaration was incomplete
99  if(call_type.has_ellipsis() && call_type.parameters().empty())
100  {
101  call_type.remove_ellipsis();
102  for(const auto &argument : as_const(*target).call_arguments())
103  call_type.parameters().push_back(code_typet::parametert(argument.type()));
104  }
105 
106  const exprt &pointer = to_dereference_expr(function).pointer();
107 
108  // the final target is a skip
109  goto_programt final_skip;
110  goto_programt::targett t_final = final_skip.add(goto_programt::make_skip());
111 
112  // build the calls and gotos
113 
114  goto_programt new_code_calls;
115  goto_programt new_code_gotos;
116 
117  for(const auto &fun : functions)
118  {
119  // call function
121  new_code_calls.add(goto_programt::make_function_call(
122  as_const(*target).call_lhs(),
123  fun,
124  as_const(*target).call_arguments(),
125  target->source_location));
126 
127  // the signature of the function might not match precisely
128  const namespacet ns(goto_model.symbol_table);
129  fix_argument_types(to_code_function_call(t1->code_nonconst()), ns);
131  to_code_function_call(t1->code_nonconst()), new_code_calls, goto_model);
132 
133  // goto final
134  new_code_calls.add(goto_programt::make_goto(t_final, true_exprt()));
135 
136  // goto to call
137  address_of_exprt address_of(fun, pointer_type(fun.type()));
138 
139  new_code_gotos.add(goto_programt::make_goto(
140  t1,
141  equal_exprt(
142  pointer,
143  typecast_exprt::conditional_cast(address_of, pointer.type()))));
144  }
145 
147  new_code_gotos.add(goto_programt::make_assertion(false_exprt()));
148  t->source_location.set_property_class("pointer dereference");
149  t->source_location.set_comment("invalid function pointer");
150 
151  goto_programt new_code;
152 
153  // patch them all together
154  new_code.destructive_append(new_code_gotos);
155  new_code.destructive_append(new_code_calls);
156  new_code.destructive_append(final_skip);
157 
158  // set locations
159  for(auto &instruction : new_code.instructions)
160  {
161  irep_idt property_class = instruction.source_location.get_property_class();
162  irep_idt comment = instruction.source_location.get_comment();
163  instruction.source_location = target->source_location;
164  if(!property_class.empty())
165  instruction.source_location.set_property_class(property_class);
166  if(!comment.empty())
167  instruction.source_location.set_comment(comment);
168  }
169 
170  goto_programt::targett next_target = target;
171  next_target++;
172 
173  goto_program.destructive_insert(next_target, new_code);
174 
175  // We preserve the original dereferencing to possibly catch
176  // further pointer-related errors.
177  code_expressiont code_expression(function);
178  code_expression.add_source_location() = function.source_location();
179  target->code_nonconst().swap(code_expression);
180  target->type = OTHER;
181 }
182 
184  goto_modelt &goto_model,
185  message_handlert &message_handler)
186 {
187  messaget message(message_handler);
188  message.status() << "Doing FI value set analysis" << messaget::eom;
189 
190  const namespacet ns(goto_model.symbol_table);
191  value_set_analysis_fit value_sets(ns);
192  value_sets(goto_model.goto_functions);
193 
194  message.status() << "Instrumenting" << messaget::eom;
195 
196  // now replace aliases by addresses
197  for(auto &f : goto_model.goto_functions.function_map)
198  {
199  for(auto target = f.second.body.instructions.begin();
200  target != f.second.body.instructions.end();
201  target++)
202  {
203  if(target->is_function_call())
204  {
205  const auto &function = as_const(*target).call_function();
206  if(function.id() == ID_dereference)
207  {
208  message.status() << "CALL at " << target->source_location << ":"
209  << messaget::eom;
210 
211  const auto &pointer = to_dereference_expr(function).pointer();
212  auto addresses = value_sets.get_values(f.first, target, pointer);
213 
214  std::set<symbol_exprt> functions;
215 
216  for(const auto &address : addresses)
217  {
218  // is this a plain function address?
219  // strip leading '&'
220  if(address.id() == ID_object_descriptor)
221  {
222  const auto &od = to_object_descriptor_expr(address);
223  const auto &object = od.object();
224  if(object.type().id() == ID_code && object.id() == ID_symbol)
225  functions.insert(to_symbol_expr(object));
226  }
227  }
228 
229  for(const auto &f : functions)
230  message.status()
231  << " function: " << f.get_identifier() << messaget::eom;
232 
233  if(functions.size() > 0)
235  f.second.body, target, functions, goto_model);
236  }
237  }
238  }
239  }
240  goto_model.goto_functions.update();
241 }
const T & as_const(T &value)
Return a reference to the same object but ensures the type is const.
Definition: as_const.h:14
pointer_typet pointer_type(const typet &subtype)
Definition: c_types.cpp:243
Operator to return the address of an object.
Definition: pointer_expr.h:341
A codet representing an assignment in the program.
Definition: std_code.h:293
codet representation of an expression statement.
Definition: std_code.h:1840
codet representation of a function call statement.
Definition: std_code.h:1213
exprt & function()
Definition: std_code.h:1248
argumentst & arguments()
Definition: std_code.h:1258
exprt::operandst argumentst
Definition: std_code.h:1222
Base type of functions.
Definition: std_types.h:539
std::vector< parametert > parameterst
Definition: std_types.h:541
const typet & return_type() const
Definition: std_types.h:645
void remove_ellipsis()
Definition: std_types.h:640
bool has_ellipsis() const
Definition: std_types.h:611
const parameterst & parameters() const
Definition: std_types.h:655
dstringt has one field, an unsigned integer no which is an index into a static table of strings.
Definition: dstring.h:37
bool empty() const
Definition: dstring.h:88
Equality.
Definition: std_expr.h:1225
Base class for all expressions.
Definition: expr.h:54
source_locationt & add_source_location()
Definition: expr.h:235
const source_locationt & source_location() const
Definition: expr.h:230
typet & type()
Return the type of the expression.
Definition: expr.h:82
The Boolean constant false.
Definition: std_expr.h:2811
function_mapt function_map
symbol_tablet symbol_table
Symbol table.
Definition: goto_model.h:30
goto_functionst goto_functions
GOTO functions.
Definition: goto_model.h:33
A generic container class for the GOTO intermediate representation of one function.
Definition: goto_program.h:71
instructionst instructions
The list of instructions in the goto program.
Definition: goto_program.h:652
void destructive_insert(const_targett target, goto_programt &p)
Inserts the given program p before target.
Definition: goto_program.h:744
instructionst::iterator targett
Definition: goto_program.h:646
void destructive_append(goto_programt &p)
Appends the given program p to *this. p is destroyed.
Definition: goto_program.h:736
static instructiont make_assignment(const code_assignt &_code, const source_locationt &l=source_locationt::nil())
Create an assignment instruction.
static instructiont make_skip(const source_locationt &l=source_locationt::nil())
Definition: goto_program.h:909
static instructiont make_function_call(const code_function_callt &_code, const source_locationt &l=source_locationt::nil())
Create a function call instruction.
targett add(instructiont &&instruction)
Adds a given instruction at the end.
Definition: goto_program.h:753
static instructiont make_goto(targett _target, const source_locationt &l=source_locationt::nil())
static instructiont make_assertion(const exprt &g, const source_locationt &l=source_locationt::nil())
Definition: goto_program.h:951
void swap(irept &irep)
Definition: irep.h:453
bool is_nil() const
Definition: irep.h:387
source_locationt source_location
Definition: message.h:247
Class that provides messages with a built-in verbosity 'level'.
Definition: message.h:155
mstreamt & status() const
Definition: message.h:414
static eomt eom
Definition: message.h:297
const typet & follow(const typet &) const
Resolve type symbol to the type it points to.
Definition: namespace.cpp:49
A namespacet is essentially one or two symbol tables bound together, to allow for symbol lookups in t...
Definition: namespace.h:91
bool lookup(const irep_idt &name, const symbolt *&symbol) const override
See documentation for namespace_baset::lookup().
Definition: namespace.cpp:138
const irep_idt & get_function() const
Expression to hold a symbol (variable)
Definition: std_expr.h:80
const irep_idt & get_identifier() const
Definition: std_expr.h:109
Symbol table entry.
Definition: symbol.h:28
irep_idt base_name
Base (non-scoped) name.
Definition: symbol.h:46
class symbol_exprt symbol_expr() const
Produces a symbol_exprt for a symbol.
Definition: symbol.cpp:121
irep_idt mode
Language mode.
Definition: symbol.h:49
The Boolean constant true.
Definition: std_expr.h:2802
Semantic type conversion.
Definition: std_expr.h:1866
static exprt conditional_cast(const exprt &expr, const typet &type)
Definition: std_expr.h:1874
std::vector< exprt > get_values(const irep_idt &function_id, locationt l, const exprt &expr) override
Container for C-Strings.
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 ...
Fresh auxiliary symbol creation.
Symbol Table + CFG.
@ OTHER
Definition: goto_program.h:35
const std::string & id2string(const irep_idt &d)
Definition: irep.h:49
API to expression classes for Pointers.
const dereference_exprt & to_dereference_expr(const exprt &expr)
Cast an exprt to a dereference_exprt.
Definition: pointer_expr.h:684
const object_descriptor_exprt & to_object_descriptor_expr(const exprt &expr)
Cast an exprt to an object_descriptor_exprt.
Definition: pointer_expr.h:218
static std::string comment(const rw_set_baset::entryt &entry, bool write)
Definition: race_check.cpp:109
#define PRECONDITION(CONDITION)
Definition: invariant.h:463
const code_function_callt & to_code_function_call(const codet &code)
Definition: std_code.h:1324
const symbol_exprt & to_symbol_expr(const exprt &expr)
Cast an exprt to a symbol_exprt.
Definition: std_expr.h:189
const code_typet & to_code_type(const typet &type)
Cast a typet to a code_typet.
Definition: std_types.h:744
Value Set Propagation (flow insensitive)
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...
void remove_function_pointer(goto_programt &goto_program, goto_programt::targett target, const std::set< symbol_exprt > &functions, goto_modelt &goto_model)
void fix_return_type(code_function_callt &function_call, goto_programt &dest, goto_modelt &goto_model)
void fix_argument_types(code_function_callt &function_call, const namespacet &ns)
flow insensitive value set function pointer removal