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  ssa_exprt ssa_expr,
25  bool write) const
26 {
27  if(!run_apply || write)
28  return std::move(ssa_expr);
29  else
30  return get_fields(ns, state, ssa_expr);
31 }
32 
34  const namespacet &ns,
35  goto_symex_statet &state,
36  exprt expr,
37  bool write) const
38 {
39  if(!run_apply)
40  return expr;
41 
42  if(expr.id() != ID_address_of)
43  {
44  Forall_operands(it, expr)
45  *it = apply(ns, state, std::move(*it), write);
46  }
47 
48  if(!write && is_ssa_expr(expr))
49  {
50  return apply(ns, state, to_ssa_expr(expr), write);
51  }
52  else if(
53  !write && expr.id() == ID_member &&
54  to_member_expr(expr).struct_op().id() == ID_struct)
55  {
56  return simplify_expr(std::move(expr), ns);
57  }
58 #ifdef ENABLE_ARRAY_FIELD_SENSITIVITY
59  else if(
60  !write && expr.id() == ID_index &&
61  to_index_expr(expr).array().id() == ID_array)
62  {
63  return simplify_expr(std::move(expr), ns);
64  }
65 #endif // ENABLE_ARRAY_FIELD_SENSITIVITY
66  else if(expr.id() == ID_member)
67  {
68  // turn a member-of-an-SSA-expression into a single SSA expression, thus
69  // encoding the member as an individual symbol rather than only the full
70  // struct
71  member_exprt &member = to_member_expr(expr);
72 
73  if(
74  is_ssa_expr(member.struct_op()) &&
75  (member.struct_op().type().id() == ID_struct ||
76  member.struct_op().type().id() == ID_struct_tag))
77  {
78  // place the entire member expression, not just the struct operand, in an
79  // SSA expression
80  ssa_exprt tmp = to_ssa_expr(member.struct_op());
81  bool was_l2 = !tmp.get_level_2().empty();
82 
83  tmp.remove_level_2();
84  member.struct_op() = tmp.get_original_expr();
85  tmp.set_expression(member);
86  if(was_l2)
87  return state.rename(std::move(tmp), ns).get();
88  else
89  return std::move(tmp);
90  }
91  }
92 #ifdef ENABLE_ARRAY_FIELD_SENSITIVITY
93  else if(expr.id() == ID_index)
94  {
95  // turn a index-of-an-SSA-expression into a single SSA expression, thus
96  // encoding the index access into an array as an individual symbol rather
97  // than only the full array
98  index_exprt &index = to_index_expr(expr);
99  simplify(index.index(), ns);
100 
101  if(
102  is_ssa_expr(index.array()) && index.array().type().id() == ID_array &&
103  index.index().id() == ID_constant)
104  {
105  // place the entire index expression, not just the array operand, in an
106  // SSA expression
107  ssa_exprt tmp = to_ssa_expr(index.array());
108  auto l2_index = state.rename(index.index(), ns);
109  l2_index.simplify(ns);
110  bool was_l2 = !tmp.get_level_2().empty();
111  exprt l2_size =
112  state.rename(to_array_type(index.array().type()).size(), ns).get();
113  if(l2_size.is_nil() && index.array().id() == ID_symbol)
114  {
115  // In case the array type was incomplete, attempt to retrieve it from
116  // the symbol table.
117  const symbolt *array_from_symbol_table = ns.get_symbol_table().lookup(
118  to_symbol_expr(index.array()).get_identifier());
119  if(array_from_symbol_table != nullptr)
120  l2_size = to_array_type(array_from_symbol_table->type).size();
121  }
122 
123  if(
124  l2_size.id() == ID_constant &&
125  numeric_cast_v<mp_integer>(to_constant_expr(l2_size)) <=
127  {
128  if(l2_index.get().id() == ID_constant)
129  {
130  // place the entire index expression, not just the array operand,
131  // in an SSA expression
132  ssa_exprt ssa_array = to_ssa_expr(index.array());
133  ssa_array.remove_level_2();
134  index.array() = ssa_array.get_original_expr();
135  index.index() = l2_index.get();
136  tmp.set_expression(index);
137  if(was_l2)
138  return state.rename(std::move(tmp), ns).get();
139  else
140  return std::move(tmp);
141  }
142  else if(!write)
143  {
144  // Expand the array and return `{array[0]; array[1]; ...}[index]`
145  exprt expanded_array =
146  get_fields(ns, state, to_ssa_expr(index.array()));
147  return index_exprt{std::move(expanded_array), index.index()};
148  }
149  }
150  }
151  }
152 #endif // ENABLE_ARRAY_FIELD_SENSITIVITY
153  return expr;
154 }
155 
157  const namespacet &ns,
158  goto_symex_statet &state,
159  const ssa_exprt &ssa_expr) const
160 {
161  if(ssa_expr.type().id() == ID_struct || ssa_expr.type().id() == ID_struct_tag)
162  {
163  const struct_typet &type = to_struct_type(ns.follow(ssa_expr.type()));
164  const struct_union_typet::componentst &components = type.components();
165 
167  fields.reserve(components.size());
168 
169  const exprt &struct_op = ssa_expr.get_original_expr();
170 
171  for(const auto &comp : components)
172  {
173  ssa_exprt tmp = ssa_expr;
174  bool was_l2 = !tmp.get_level_2().empty();
175  tmp.remove_level_2();
176  tmp.set_expression(member_exprt{struct_op, comp.get_name(), comp.type()});
177  if(was_l2)
178  {
179  fields.push_back(state.rename(get_fields(ns, state, tmp), ns).get());
180  }
181  else
182  fields.push_back(get_fields(ns, state, tmp));
183  }
184 
185  return struct_exprt(std::move(fields), ssa_expr.type());
186  }
187 #ifdef ENABLE_ARRAY_FIELD_SENSITIVITY
188  else if(
189  ssa_expr.type().id() == ID_array &&
190  to_array_type(ssa_expr.type()).size().id() == ID_constant)
191  {
192  const mp_integer mp_array_size = numeric_cast_v<mp_integer>(
193  to_constant_expr(to_array_type(ssa_expr.type()).size()));
194  if(mp_array_size < 0 || mp_array_size > max_field_sensitivity_array_size)
195  return ssa_expr;
196 
197  const array_typet &type = to_array_type(ssa_expr.type());
198  const std::size_t array_size = numeric_cast_v<std::size_t>(mp_array_size);
199 
200  array_exprt::operandst elements;
201  elements.reserve(array_size);
202 
203  const exprt &array = ssa_expr.get_original_expr();
204 
205  for(std::size_t i = 0; i < array_size; ++i)
206  {
207  const index_exprt index(array, from_integer(i, index_type()));
208  ssa_exprt tmp = ssa_expr;
209  bool was_l2 = !tmp.get_level_2().empty();
210  tmp.remove_level_2();
211  tmp.set_expression(index);
212  if(was_l2)
213  {
214  elements.push_back(state.rename(get_fields(ns, state, tmp), ns).get());
215  }
216  else
217  elements.push_back(get_fields(ns, state, tmp));
218  }
219 
220  return array_exprt(std::move(elements), type);
221  }
222 #endif // ENABLE_ARRAY_FIELD_SENSITIVITY
223  else
224  return ssa_expr;
225 }
226 
228  const namespacet &ns,
229  goto_symex_statet &state,
230  const ssa_exprt &lhs,
231  symex_targett &target,
232  bool allow_pointer_unsoundness)
233 {
234  const exprt lhs_fs = apply(ns, state, lhs, false);
235 
236  bool run_apply_bak = run_apply;
237  run_apply = false;
239  ns, state, lhs_fs, lhs, target, allow_pointer_unsoundness);
240  run_apply = run_apply_bak;
241 }
242 
254  const namespacet &ns,
255  goto_symex_statet &state,
256  const exprt &lhs_fs,
257  const exprt &lhs,
258  symex_targett &target,
259  bool allow_pointer_unsoundness)
260 {
261  if(lhs == lhs_fs)
262  return;
263  else if(is_ssa_expr(lhs_fs))
264  {
265  exprt ssa_rhs = state.rename(lhs, ns).get();
266  simplify(ssa_rhs, ns);
267 
268  const ssa_exprt ssa_lhs = state
269  .assignment(
270  to_ssa_expr(lhs_fs),
271  ssa_rhs,
272  ns,
273  true,
274  true,
275  allow_pointer_unsoundness)
276  .get();
277 
278  // do the assignment
279  target.assignment(
280  state.guard.as_expr(),
281  ssa_lhs,
282  ssa_lhs,
283  ssa_lhs.get_original_expr(),
284  ssa_rhs,
285  state.source,
287  }
288  else if(lhs.type().id() == ID_struct || lhs.type().id() == ID_struct_tag)
289  {
290  const struct_typet &type = to_struct_type(ns.follow(lhs.type()));
291  const struct_union_typet::componentst &components = type.components();
292 
293  PRECONDITION(
294  components.empty() ||
295  (lhs_fs.has_operands() && lhs_fs.operands().size() == components.size()));
296 
297  exprt::operandst::const_iterator fs_it = lhs_fs.operands().begin();
298  for(const auto &comp : components)
299  {
300  const member_exprt member_rhs(lhs, comp.get_name(), comp.type());
301  const exprt &member_lhs = *fs_it;
302 
304  ns, state, member_lhs, member_rhs, target, allow_pointer_unsoundness);
305  ++fs_it;
306  }
307  }
308 #ifdef ENABLE_ARRAY_FIELD_SENSITIVITY
309  else if(const auto &type = type_try_dynamic_cast<array_typet>(lhs.type()))
310  {
311  const std::size_t array_size =
312  numeric_cast_v<std::size_t>(to_constant_expr(type->size()));
313  PRECONDITION(lhs_fs.operands().size() == array_size);
314 
315  if(array_size > max_field_sensitivity_array_size)
316  return;
317 
318  exprt::operandst::const_iterator fs_it = lhs_fs.operands().begin();
319  for(std::size_t i = 0; i < array_size; ++i)
320  {
321  const index_exprt index_rhs(lhs, from_integer(i, index_type()));
322  const exprt &index_lhs = *fs_it;
323 
325  ns, state, index_lhs, index_rhs, target, allow_pointer_unsoundness);
326  ++fs_it;
327  }
328  }
329 #endif // ENABLE_ARRAY_FIELD_SENSITIVITY
330  else if(lhs_fs.has_operands())
331  {
332  PRECONDITION(
333  lhs.has_operands() && lhs_fs.operands().size() == lhs.operands().size());
334 
335  exprt::operandst::const_iterator fs_it = lhs_fs.operands().begin();
336  for(const exprt &op : lhs.operands())
337  {
339  ns, state, *fs_it, op, target, allow_pointer_unsoundness);
340  ++fs_it;
341  }
342  }
343  else
344  {
345  UNREACHABLE;
346  }
347 }
348 
350 {
351  if(expr.type().id() == ID_struct || expr.type().id() == ID_struct_tag)
352  return true;
353 
354 #ifdef ENABLE_ARRAY_FIELD_SENSITIVITY
355  if(
356  expr.type().id() == ID_array &&
357  to_array_type(expr.type()).size().id() == ID_constant &&
358  numeric_cast_v<mp_integer>(to_constant_expr(
360  {
361  return true;
362  }
363 #endif
364 
365  return false;
366 }
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:141
Forall_operands
#define Forall_operands(it, expr)
Definition: expr.h:25
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:77
to_struct_type
const struct_typet & to_struct_type(const typet &type)
Cast a typet to a struct_typet.
Definition: std_types.h:302
to_index_expr
const index_exprt & to_index_expr(const exprt &expr)
Cast an exprt to an index_exprt.
Definition: std_expr.h:1296
symbolt::type
typet type
Type of symbol.
Definition: symbol.h:31
goto_symex_statet
Central data structure: state.
Definition: goto_symex_state.h:46
mp_integer
BigInt mp_integer
Definition: mp_arith.h:19
exprt
Base class for all expressions.
Definition: expr.h:54
struct_union_typet::componentst
std::vector< componentt > componentst
Definition: std_types.h:134
goto_symex_statet::source
symex_targett::sourcet source
Definition: goto_symex_state.h:64
field_sensitivityt::apply
NODISCARD 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:33
field_sensitivityt::max_field_sensitivity_array_size
const std::size_t max_field_sensitivity_array_size
Definition: field_sensitivity.h:157
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:155
symex_targett::assignment_typet::STATE
@ STATE
struct_exprt
Struct constructor from list of elements.
Definition: std_expr.h:1582
ssa_exprt
Expression providing an SSA-renamed symbol of expressions.
Definition: ssa_expr.h:17
array_typet::size
const exprt & size() const
Definition: std_types.h:765
namespacet
A namespacet is essentially one or two symbol tables bound together, to allow for symbol lookups in t...
Definition: namespace.h:92
exprt::type
typet & type()
Return the type of the expression.
Definition: expr.h:82
is_ssa_expr
bool is_ssa_expr(const exprt &expr)
Definition: ssa_expr.h:125
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:93
to_ssa_expr
const ssa_exprt & to_ssa_expr(const exprt &expr)
Cast a generic exprt to an ssa_exprt.
Definition: ssa_expr.h:145
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:2557
PRECONDITION
#define PRECONDITION(CONDITION)
Definition: invariant.h:464
symbol_exprt::get_identifier
const irep_idt & get_identifier() const
Definition: std_expr.h:109
goto_statet::guard
guardt guard
Definition: goto_state.h:58
ssa_exprt::set_expression
void set_expression(exprt expr)
Replace the underlying, original expression by expr while maintaining SSA indices.
simplify
bool simplify(exprt &expr, const namespacet &ns)
Definition: simplify_expr.cpp:2552
simplify_expr
exprt simplify_expr(exprt src, const namespacet &ns)
Definition: simplify_expr.cpp:2557
index_exprt::index
exprt & index()
Definition: std_expr.h:1268
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:1258
to_symbol_expr
const symbol_exprt & to_symbol_expr(const exprt &expr)
Cast an exprt to a symbol_exprt.
Definition: std_expr.h:189
irept::is_nil
bool is_nil() const
Definition: irep.h:387
irept::id
const irep_idt & id() const
Definition: irep.h:407
exprt::operandst
std::vector< exprt > operandst
Definition: expr.h:56
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:227
member_exprt
Extract member of struct or union.
Definition: std_expr.h:2527
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:757
goto_symex_state.h
Symbolic Execution.
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:349
namespace_baset::follow
const typet & follow(const typet &) const
Resolve type symbol to the type it points to.
Definition: namespace.cpp:52
irept::get
const irep_idt & get(const irep_namet &name) const
Definition: irep.cpp:51
symbolt
Symbol table entry.
Definition: symbol.h:28
from_integer
constant_exprt from_integer(const mp_integer &int_value, const typet &type)
Definition: arith_tools.cpp:100
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:253
to_array_type
const array_typet & to_array_type(const typet &type)
Cast a typet to an array_typet.
Definition: std_types.h:807
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:2611
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:156
exprt::operands
operandst & operands()
Definition: expr.h:96
field_sensitivity.h
index_exprt
Array index operator.
Definition: std_expr.h:1242
std_expr.h
API to expression classes.
array_exprt
Array constructor from list of elements.
Definition: std_expr.h:1381
c_types.h
symex_target.h
Generate Equation using Symbolic Execution.
symex_targett
The interface of the target container for symbolic execution to record its symbolic steps into.
Definition: symex_target.h:26
to_constant_expr
const constant_exprt & to_constant_expr(const exprt &expr)
Cast an exprt to a constant_exprt.
Definition: std_expr.h:2700