cprover
field_sensitivity.cpp
Go to the documentation of this file.
1 /*******************************************************************\
2 
3 Module: Field-sensitive SSA
4 
5 Author: Michael Tautschnig
6 
7 \*******************************************************************/
8 
9 #include "field_sensitivity.h"
10 
11 #include <util/arith_tools.h>
12 #include <util/c_types.h>
13 #include <util/simplify_expr.h>
14 #include <util/std_expr.h>
15 
16 #include "goto_symex_state.h"
17 #include "symex_target.h"
18 
19 #define ENABLE_ARRAY_FIELD_SENSITIVITY
20 
22  const namespacet &ns,
23  goto_symex_statet &state,
24  exprt expr,
25  bool write) const
26 {
27  if(!run_apply)
28  return expr;
29 
30  if(expr.id() != ID_address_of)
31  {
32  Forall_operands(it, expr)
33  *it = apply(ns, state, std::move(*it), write);
34  }
35 
36  if(expr.id() == ID_symbol && expr.get_bool(ID_C_SSA_symbol) && !write)
37  {
38  return get_fields(ns, state, to_ssa_expr(expr));
39  }
40  else if(
41  !write && expr.id() == ID_member &&
42  to_member_expr(expr).struct_op().id() == ID_struct)
43  {
44  return simplify_expr(std::move(expr), ns);
45  }
46 #ifdef ENABLE_ARRAY_FIELD_SENSITIVITY
47  else if(
48  !write && expr.id() == ID_index &&
49  to_index_expr(expr).array().id() == ID_array)
50  {
51  return simplify_expr(std::move(expr), ns);
52  }
53 #endif // ENABLE_ARRAY_FIELD_SENSITIVITY
54  else if(expr.id() == ID_member)
55  {
56  // turn a member-of-an-SSA-expression into a single SSA expression, thus
57  // encoding the member as an individual symbol rather than only the full
58  // struct
59  member_exprt &member = to_member_expr(expr);
60 
61  if(
62  member.struct_op().id() == ID_symbol &&
63  member.struct_op().get_bool(ID_C_SSA_symbol) &&
64  (member.struct_op().type().id() == ID_struct ||
65  member.struct_op().type().id() == ID_struct_tag))
66  {
67  // place the entire member expression, not just the struct operand, in an
68  // SSA expression
69  ssa_exprt tmp = to_ssa_expr(member.struct_op());
70  bool was_l2 = !tmp.get_level_2().empty();
71 
72  tmp.remove_level_2();
73  member.struct_op() = tmp.get_original_expr();
74  tmp.set_expression(member);
75  if(was_l2)
76  return state.rename(std::move(tmp), ns).get();
77  else
78  return std::move(tmp);
79  }
80  }
81 #ifdef ENABLE_ARRAY_FIELD_SENSITIVITY
82  else if(expr.id() == ID_index)
83  {
84  // turn a index-of-an-SSA-expression into a single SSA expression, thus
85  // encoding the index access into an array as an individual symbol rather
86  // than only the full array
87  index_exprt &index = to_index_expr(expr);
88  simplify(index.index(), ns);
89 
90  if(
91  index.array().id() == ID_symbol &&
92  index.array().get_bool(ID_C_SSA_symbol) &&
93  index.array().type().id() == ID_array &&
94  index.index().id() == ID_constant)
95  {
96  // place the entire index expression, not just the array operand, in an
97  // SSA expression
98  ssa_exprt tmp = to_ssa_expr(index.array());
99  auto l2_index = state.rename(index.index(), ns);
100  l2_index.simplify(ns);
101  bool was_l2 = !tmp.get_level_2().empty();
102  exprt l2_size =
103  state.rename(to_array_type(index.array().type()).size(), ns).get();
104  if(l2_size.is_nil() && index.array().id() == ID_symbol)
105  {
106  // In case the array type was incomplete, attempt to retrieve it from
107  // the symbol table.
108  const symbolt *array_from_symbol_table = ns.get_symbol_table().lookup(
109  to_symbol_expr(index.array()).get_identifier());
110  if(array_from_symbol_table != nullptr)
111  l2_size = to_array_type(array_from_symbol_table->type).size();
112  }
113 
114  if(
115  l2_size.id() == ID_constant &&
116  numeric_cast_v<mp_integer>(to_constant_expr(l2_size)) <=
118  {
119  if(l2_index.get().id() == ID_constant)
120  {
121  // place the entire index expression, not just the array operand,
122  // in an SSA expression
123  ssa_exprt ssa_array = to_ssa_expr(index.array());
124  ssa_array.remove_level_2();
125  index.array() = ssa_array.get_original_expr();
126  index.index() = l2_index.get();
127  tmp.set_expression(index);
128  if(was_l2)
129  return state.rename(std::move(tmp), ns).get();
130  else
131  return std::move(tmp);
132  }
133  else if(!write)
134  {
135  // Expand the array and return `{array[0]; array[1]; ...}[index]`
136  exprt expanded_array =
137  get_fields(ns, state, to_ssa_expr(index.array()));
138  return index_exprt{std::move(expanded_array), index.index()};
139  }
140  }
141  }
142  }
143 #endif // ENABLE_ARRAY_FIELD_SENSITIVITY
144  return expr;
145 }
146 
148  const namespacet &ns,
149  goto_symex_statet &state,
150  const ssa_exprt &ssa_expr) const
151 {
152  if(ssa_expr.type().id() == ID_struct || ssa_expr.type().id() == ID_struct_tag)
153  {
154  const struct_typet &type = to_struct_type(ns.follow(ssa_expr.type()));
155  const struct_union_typet::componentst &components = type.components();
156 
158  fields.reserve(components.size());
159 
160  const exprt &struct_op = ssa_expr.get_original_expr();
161 
162  for(const auto &comp : components)
163  {
164  const member_exprt member(struct_op, comp.get_name(), comp.type());
165  ssa_exprt tmp = ssa_expr;
166  bool was_l2 = !tmp.get_level_2().empty();
167  tmp.remove_level_2();
168  tmp.set_expression(member);
169  if(was_l2)
170  {
171  fields.push_back(state.rename(get_fields(ns, state, tmp), ns).get());
172  }
173  else
174  fields.push_back(get_fields(ns, state, tmp));
175  }
176 
177  return struct_exprt(std::move(fields), ssa_expr.type());
178  }
179 #ifdef ENABLE_ARRAY_FIELD_SENSITIVITY
180  else if(
181  ssa_expr.type().id() == ID_array &&
182  to_array_type(ssa_expr.type()).size().id() == ID_constant)
183  {
184  const mp_integer mp_array_size = numeric_cast_v<mp_integer>(
185  to_constant_expr(to_array_type(ssa_expr.type()).size()));
186  if(mp_array_size < 0 || mp_array_size > max_field_sensitivity_array_size)
187  return ssa_expr;
188 
189  const array_typet &type = to_array_type(ssa_expr.type());
190  const std::size_t array_size = numeric_cast_v<std::size_t>(mp_array_size);
191 
192  array_exprt::operandst elements;
193  elements.reserve(array_size);
194 
195  const exprt &array = ssa_expr.get_original_expr();
196 
197  for(std::size_t i = 0; i < array_size; ++i)
198  {
199  const index_exprt index(array, from_integer(i, index_type()));
200  ssa_exprt tmp = ssa_expr;
201  bool was_l2 = !tmp.get_level_2().empty();
202  tmp.remove_level_2();
203  tmp.set_expression(index);
204  if(was_l2)
205  {
206  elements.push_back(state.rename(get_fields(ns, state, tmp), ns).get());
207  }
208  else
209  elements.push_back(get_fields(ns, state, tmp));
210  }
211 
212  return array_exprt(std::move(elements), type);
213  }
214 #endif // ENABLE_ARRAY_FIELD_SENSITIVITY
215  else
216  return ssa_expr;
217 }
218 
220  const namespacet &ns,
221  goto_symex_statet &state,
222  const ssa_exprt &lhs,
223  symex_targett &target,
224  bool allow_pointer_unsoundness)
225 {
226  const exprt lhs_fs = apply(ns, state, lhs, false);
227 
228  bool run_apply_bak = run_apply;
229  run_apply = false;
231  ns, state, lhs_fs, lhs, target, allow_pointer_unsoundness);
232  run_apply = run_apply_bak;
233 }
234 
246  const namespacet &ns,
247  goto_symex_statet &state,
248  const exprt &lhs_fs,
249  const exprt &lhs,
250  symex_targett &target,
251  bool allow_pointer_unsoundness)
252 {
253  if(lhs == lhs_fs)
254  return;
255  else if(lhs_fs.id() == ID_symbol && lhs_fs.get_bool(ID_C_SSA_symbol))
256  {
257  exprt ssa_rhs = state.rename(lhs, ns).get();
258  simplify(ssa_rhs, ns);
259 
260  const ssa_exprt ssa_lhs = state
261  .assignment(
262  to_ssa_expr(lhs_fs),
263  ssa_rhs,
264  ns,
265  true,
266  true,
267  allow_pointer_unsoundness)
268  .get();
269 
270  // do the assignment
271  target.assignment(
272  state.guard.as_expr(),
273  ssa_lhs,
274  ssa_lhs,
275  ssa_lhs.get_original_expr(),
276  ssa_rhs,
277  state.source,
279  }
280  else if(lhs.type().id() == ID_struct || lhs.type().id() == ID_struct_tag)
281  {
282  const struct_typet &type = to_struct_type(ns.follow(lhs.type()));
283  const struct_union_typet::componentst &components = type.components();
284 
285  PRECONDITION(
286  components.empty() ||
287  (lhs_fs.has_operands() && lhs_fs.operands().size() == components.size()));
288 
289  exprt::operandst::const_iterator fs_it = lhs_fs.operands().begin();
290  for(const auto &comp : components)
291  {
292  const member_exprt member_rhs(lhs, comp.get_name(), comp.type());
293  const exprt &member_lhs = *fs_it;
294 
296  ns, state, member_lhs, member_rhs, target, allow_pointer_unsoundness);
297  ++fs_it;
298  }
299  }
300 #ifdef ENABLE_ARRAY_FIELD_SENSITIVITY
301  else if(const auto &type = type_try_dynamic_cast<array_typet>(lhs.type()))
302  {
303  const std::size_t array_size =
304  numeric_cast_v<std::size_t>(to_constant_expr(type->size()));
305  PRECONDITION(lhs_fs.operands().size() == array_size);
306 
307  if(array_size > max_field_sensitivity_array_size)
308  return;
309 
310  exprt::operandst::const_iterator fs_it = lhs_fs.operands().begin();
311  for(std::size_t i = 0; i < array_size; ++i)
312  {
313  const index_exprt index_rhs(lhs, from_integer(i, index_type()));
314  const exprt &index_lhs = *fs_it;
315 
317  ns, state, index_lhs, index_rhs, target, allow_pointer_unsoundness);
318  ++fs_it;
319  }
320  }
321 #endif // ENABLE_ARRAY_FIELD_SENSITIVITY
322  else if(lhs_fs.has_operands())
323  {
324  PRECONDITION(
325  lhs.has_operands() && lhs_fs.operands().size() == lhs.operands().size());
326 
327  exprt::operandst::const_iterator fs_it = lhs_fs.operands().begin();
328  for(const exprt &op : lhs.operands())
329  {
331  ns, state, *fs_it, op, target, allow_pointer_unsoundness);
332  ++fs_it;
333  }
334  }
335  else
336  {
337  UNREACHABLE;
338  }
339 }
340 
342 {
343  if(expr.type().id() == ID_struct || expr.type().id() == ID_struct_tag)
344  return true;
345 
346 #ifdef ENABLE_ARRAY_FIELD_SENSITIVITY
347  if(
348  expr.type().id() == ID_array &&
349  to_array_type(expr.type()).size().id() == ID_constant &&
350  numeric_cast_v<mp_integer>(to_constant_expr(
352  {
353  return true;
354  }
355 #endif
356 
357  return false;
358 }
UNREACHABLE
#define UNREACHABLE
This should be used to mark dead code.
Definition: invariant.h:504
struct_union_typet::components
const componentst & components() const
Definition: std_types.h:142
Forall_operands
#define Forall_operands(it, expr)
Definition: expr.h:24
arith_tools.h
ssa_exprt::remove_level_2
void remove_level_2()
goto_symex_statet::assignment
NODISCARD renamedt< ssa_exprt, L2 > assignment(ssa_exprt lhs, const exprt &rhs, const namespacet &ns, bool rhs_is_simplified, bool record_value, bool allow_pointer_unsoundness=false)
Definition: goto_symex_state.cpp:76
to_struct_type
const struct_typet & to_struct_type(const typet &type)
Cast a typet to a struct_typet.
Definition: std_types.h:303
to_index_expr
const index_exprt & to_index_expr(const exprt &expr)
Cast an exprt to an index_exprt.
Definition: std_expr.h:1347
symbolt::type
typet type
Type of symbol.
Definition: symbol.h:31
goto_symex_statet
Central data structure: state.
Definition: goto_symex_state.h:45
mp_integer
BigInt mp_integer
Definition: mp_arith.h:19
exprt
Base class for all expressions.
Definition: expr.h:52
struct_union_typet::componentst
std::vector< componentt > componentst
Definition: std_types.h:135
goto_symex_statet::source
symex_targett::sourcet source
Definition: goto_symex_state.h:64
field_sensitivityt::apply
exprt apply(const namespacet &ns, goto_symex_statet &state, exprt expr, bool write) const
Turn an expression expr into a field-sensitive SSA expression.
Definition: field_sensitivity.cpp:21
field_sensitivityt::max_field_sensitivity_array_size
const std::size_t max_field_sensitivity_array_size
Definition: field_sensitivity.h:149
index_type
bitvector_typet index_type()
Definition: c_types.cpp:16
ssa_exprt::get_original_expr
const exprt & get_original_expr() const
Definition: ssa_expr.h:33
field_sensitivityt::run_apply
bool run_apply
whether or not to invoke field_sensitivityt::apply
Definition: field_sensitivity.h:147
symex_targett::assignment_typet::STATE
struct_exprt
Struct constructor from list of elements.
Definition: std_expr.h:1632
ssa_exprt
Expression providing an SSA-renamed symbol of expressions.
Definition: ssa_expr.h:16
array_typet::size
const exprt & size() const
Definition: std_types.h:973
namespacet
A namespacet is essentially one or two symbol tables bound together, to allow for symbol lookups in t...
Definition: namespace.h:91
exprt::type
typet & type()
Return the type of the expression.
Definition: expr.h:81
irept::get_bool
bool get_bool(const irep_namet &name) const
Definition: irep.cpp:64
symex_targett::assignment
virtual void assignment(const exprt &guard, const ssa_exprt &ssa_lhs, const exprt &ssa_full_lhs, const exprt &original_full_lhs, const exprt &ssa_rhs, const sourcet &source, assignment_typet assignment_type)=0
Write to a local variable.
exprt::has_operands
bool has_operands() const
Return true if there is at least one operand.
Definition: expr.h:92
to_ssa_expr
const ssa_exprt & to_ssa_expr(const exprt &expr)
Cast a generic exprt to an ssa_exprt.
Definition: ssa_expr.h:148
guard_exprt::as_expr
exprt as_expr() const
Definition: guard_expr.h:49
member_exprt::struct_op
const exprt & struct_op() const
Definition: std_expr.h:3435
PRECONDITION
#define PRECONDITION(CONDITION)
Definition: invariant.h:464
goto_statet::guard
guardt guard
Definition: goto_state.h:50
simplify
bool simplify(exprt &expr, const namespacet &ns)
Definition: simplify_expr.cpp:2693
simplify_expr
exprt simplify_expr(exprt src, const namespacet &ns)
Definition: simplify_expr.cpp:2698
index_exprt::index
exprt & index()
Definition: std_expr.h:1319
ssa_exprt::get_level_2
const irep_idt get_level_2() const
Definition: ssa_expr.h:73
index_exprt::array
exprt & array()
Definition: std_expr.h:1309
to_symbol_expr
const symbol_exprt & to_symbol_expr(const exprt &expr)
Cast an exprt to a symbol_exprt.
Definition: std_expr.h:177
irept::is_nil
bool is_nil() const
Definition: irep.h:398
irept::id
const irep_idt & id() const
Definition: irep.h:418
exprt::operandst
std::vector< exprt > operandst
Definition: expr.h:55
dstringt::empty
bool empty() const
Definition: dstring.h:88
simplify_expr.h
field_sensitivityt::field_assignments
void field_assignments(const namespacet &ns, goto_symex_statet &state, const ssa_exprt &lhs, symex_targett &target, bool allow_pointer_unsoundness)
Assign to the individual fields of a non-expanded symbol lhs.
Definition: field_sensitivity.cpp:219
member_exprt
Extract member of struct or union.
Definition: std_expr.h:3404
namespacet::get_symbol_table
const symbol_table_baset & get_symbol_table() const
Return first symbol table registered with the namespace.
Definition: namespace.h:124
struct_typet
Structure type, corresponds to C style structs.
Definition: std_types.h:225
array_typet
Arrays with given size.
Definition: std_types.h:964
ssa_exprt::set_expression
void set_expression(const exprt &expr)
Replace the underlying, original expression by expr while maintaining SSA indices.
goto_symex_state.h
field_sensitivityt::is_divisible
bool is_divisible(const ssa_exprt &expr) const
Determine whether expr would translate to an atomic SSA expression (returns false) or a composite obj...
Definition: field_sensitivity.cpp:341
namespace_baset::follow
const typet & follow(const typet &) const
Resolve type symbol to the type it points to.
Definition: namespace.cpp:51
irept::get
const irep_idt & get(const irep_namet &name) const
Definition: irep.cpp:51
symbolt
Symbol table entry.
Definition: symbol.h:27
from_integer
constant_exprt from_integer(const mp_integer &int_value, const typet &type)
Definition: arith_tools.cpp:99
field_sensitivityt::field_assignments_rec
void field_assignments_rec(const namespacet &ns, goto_symex_statet &state, const exprt &lhs_fs, const exprt &lhs, symex_targett &target, bool allow_pointer_unsoundness)
Assign to the individual fields lhs_fs of a non-expanded symbol lhs.
Definition: field_sensitivity.cpp:245
to_array_type
const array_typet & to_array_type(const typet &type)
Cast a typet to an array_typet.
Definition: std_types.h:1011
symbol_table_baset::lookup
const symbolt * lookup(const irep_idt &name) const
Find a symbol in the symbol table for read-only access.
Definition: symbol_table_base.h:95
to_member_expr
const member_exprt & to_member_expr(const exprt &expr)
Cast an exprt to a member_exprt.
Definition: std_expr.h:3489
goto_symex_statet::rename
NODISCARD renamedt< exprt, level > rename(exprt expr, const namespacet &ns)
Rewrites symbol expressions in exprt, applying a suffix to each symbol reflecting its most recent ver...
field_sensitivityt::get_fields
exprt get_fields(const namespacet &ns, goto_symex_statet &state, const ssa_exprt &ssa_expr) const
Compute an expression representing the individual components of a field-sensitive SSA representation ...
Definition: field_sensitivity.cpp:147
exprt::operands
operandst & operands()
Definition: expr.h:95
field_sensitivity.h
index_exprt
Array index operator.
Definition: std_expr.h:1292
std_expr.h
array_exprt
Array constructor from list of elements.
Definition: std_expr.h:1431
c_types.h
symex_target.h
symex_targett
The interface of the target container for symbolic execution to record its symbolic steps into.
Definition: symex_target.h:25
to_constant_expr
const constant_exprt & to_constant_expr(const exprt &expr)
Cast an exprt to a constant_exprt.
Definition: std_expr.h:3939