CBMC
interval_abstract_value.cpp
Go to the documentation of this file.
1 /*******************************************************************\
2 
3  Module: analyses variable-sensitivity intervals
4 
5  Author: Diffblue Ltd.
6 
7 \*******************************************************************/
8 
9 #include <ostream>
10 
11 #include <util/arith_tools.h>
12 #include <util/bitvector_types.h>
13 #include <util/expr_util.h>
14 #include <util/invariant.h>
15 #include <util/simplify_expr.h>
16 
17 #include "abstract_environment.h"
20 #include "widened_range.h"
21 
24  const namespacet &n);
25 
27 {
28 public:
30  const constant_interval_exprt &interval_,
31  const namespacet &n)
32  : interval(interval_),
33  index(nil_exprt()),
34  next(interval.get_lower()),
35  upper(interval.get_upper()),
36  ns(n)
37  {
38  }
39 
40  const exprt &current() const override
41  {
42  return index;
43  }
44  bool advance_to_next() override
45  {
46  index = next;
49  .is_true();
50  }
51 
53  {
55  }
56 
57 private:
58  static exprt next_element(const exprt &cur, const namespacet &ns)
59  {
60  return simplify_expr(plus_exprt(cur, from_integer(1, cur.type())), ns);
61  }
62 
67  const namespacet &ns;
68 };
69 
72  const namespacet &n)
73 {
74  return std::make_unique<interval_index_ranget>(interval, n);
75 }
76 
77 static inline bool
78 bvint_value_is_max(const typet &type, const mp_integer &value)
79 {
80  PRECONDITION(type.id() == ID_signedbv || type.id() == ID_unsignedbv);
81  if(type.id() == ID_signedbv)
82  {
83  return to_signedbv_type(type).largest() == value;
84  }
85  else
86  {
87  return to_unsignedbv_type(type).largest() == value;
88  }
89 }
90 
91 static inline bool
92 bvint_value_is_min(const typet &type, const mp_integer &value)
93 {
94  PRECONDITION(type.id() == ID_signedbv || type.id() == ID_unsignedbv);
95  if(type.id() == ID_signedbv)
96  {
97  return to_signedbv_type(type).smallest() == value;
98  }
99  else
100  {
101  return to_unsignedbv_type(type).smallest() == value;
102  }
103 }
104 
105 static inline constant_interval_exprt
107 {
108  return constant_interval_exprt(min_value_exprt(value.type()), value);
109 }
110 
111 static inline constant_interval_exprt
113 {
114  return constant_interval_exprt(value, max_value_exprt(value.type()));
115 }
116 
117 static inline mp_integer force_value_from_expr(const exprt &value)
118 {
120  std::optional<mp_integer> maybe_integer_value =
121  numeric_cast<mp_integer>(value);
122  INVARIANT(maybe_integer_value.has_value(), "Input has to have a value");
123  return maybe_integer_value.value();
124 }
125 
126 static inline constant_interval_exprt
128 {
129  mp_integer integer_value = force_value_from_expr(value);
130  if(!bvint_value_is_min(value.type(), integer_value))
132  min_value_exprt(value.type()),
133  from_integer(integer_value - 1, value.type()));
134  else
135  return constant_interval_exprt::bottom(value.type());
136 }
137 
138 static inline constant_interval_exprt
140 {
141  mp_integer integer_value = force_value_from_expr(value);
142  if(!bvint_value_is_max(value.type(), integer_value))
144  from_integer(integer_value + 1, value.type()),
145  max_value_exprt(value.type()));
146  else
147  return constant_interval_exprt::bottom(value.type());
148 }
149 
150 static inline bool represents_interval(const exprt &expr)
151 {
152  const exprt &e = skip_typecast(expr);
153  return (e.id() == ID_constant_interval || e.is_constant());
154 }
155 
157 {
158  const exprt &e = skip_typecast(expr);
159 
160  if(e.id() == ID_constant_interval)
161  {
162  return to_constant_interval_expr(e);
163  }
164  else if(e.is_constant())
165  {
166  return constant_interval_exprt(e, e);
167  }
168  else
169  {
170  // not directly representable, so just return TOP
171  return constant_interval_exprt(e.type());
172  }
173 }
174 
175 static inline irep_idt invert_relation(const irep_idt &relation)
176 {
177  PRECONDITION(
178  relation == ID_le || relation == ID_lt || relation == ID_ge ||
179  relation == ID_gt || relation == ID_equal);
180  if(relation == ID_le)
181  return ID_ge;
182  if(relation == ID_ge)
183  return ID_le;
184  if(relation == ID_lt)
185  return ID_gt;
186  if(relation == ID_gt)
187  return ID_lt;
188  return relation;
189 }
190 
197 {
198  PRECONDITION(e.operands().size() == 2);
199  const auto &relation = e.id();
200  const auto &binary_e = to_binary_expr(e);
201  const auto &lhs = binary_e.lhs();
202  const auto &rhs = binary_e.rhs();
203  PRECONDITION(
204  relation == ID_le || relation == ID_lt || relation == ID_ge ||
205  relation == ID_gt || relation == ID_equal);
206  PRECONDITION(lhs.is_constant() || lhs.id() == ID_symbol);
207  PRECONDITION(rhs.is_constant() || rhs.id() == ID_symbol);
208  PRECONDITION(lhs.id() != rhs.id());
209 
210  const auto the_constant_part_of_the_relation =
211  (rhs.id() == ID_symbol ? lhs : rhs);
212  const auto maybe_inverted_relation =
213  (rhs.id() == ID_symbol ? invert_relation(relation) : relation);
214 
215  if(maybe_inverted_relation == ID_le)
216  return interval_from_x_le_value(the_constant_part_of_the_relation);
217  if(maybe_inverted_relation == ID_lt)
218  return interval_from_x_lt_value(the_constant_part_of_the_relation);
219  if(maybe_inverted_relation == ID_ge)
220  return interval_from_x_ge_value(the_constant_part_of_the_relation);
221  if(maybe_inverted_relation == ID_gt)
222  return interval_from_x_gt_value(the_constant_part_of_the_relation);
223  INVARIANT(
224  maybe_inverted_relation == ID_equal, "We excluded other cases above");
226  the_constant_part_of_the_relation, the_constant_part_of_the_relation);
227 }
228 
230  const typet &t,
231  bool tp,
232  bool bttm)
233  : abstract_value_objectt(t, tp, bttm), interval(t)
234 {
235 }
236 
238 {
239  if(e.is_top())
240  return true;
241 
242  if(e.get_lower().is_false() && e.get_upper().is_true())
243  return true;
244  if(
245  e.type().id() == ID_c_bool && e.get_lower().is_zero() &&
246  e.get_upper().is_one())
247  return true;
248 
249  return false;
250 }
251 
253  const constant_interval_exprt &e)
254  : abstract_value_objectt(e.type(), new_interval_is_top(e), e.is_bottom()),
255  interval(e)
256 {
257 }
258 
260  const exprt &lower,
261  const exprt &upper)
263 {
264 }
265 
267  const exprt &e,
268  const abstract_environmentt &environment,
269  const namespacet &ns)
272  ? make_interval_expr(e)
273  : (e.operands().size() == 2 ? interval_from_relation(e)
274  : constant_interval_exprt(e.type())))
275 {
276 }
277 
279 {
280  // Attempt to reduce this interval to a constant expression
281  if(!is_top() && !is_bottom() && interval.is_single_value_interval())
282  {
283  // Interval is the equivalent of a constant, so reduce it to a constant
284  return to_constant_expr(interval.get_lower());
285  }
287 #if 0
288  if(!is_top() && !is_bottom())
289  {
290  return this->interval;
291  }
292  else
293  {
295  }
296 #endif
297 }
298 
300 {
302 }
303 
305 {
306  return std::hash<std::string>{}(interval.pretty());
307 }
308 
310  const abstract_object_pointert &other) const
311 {
312  auto cast_other =
313  std::dynamic_pointer_cast<const interval_abstract_valuet>(other);
314  return cast_other && interval == cast_other->interval;
315 }
316 
318  std::ostream &out,
319  const ai_baset &ai,
320  const namespacet &ns) const
321 {
322  if(!is_top() && !is_bottom())
323  {
324  std::string lower_string;
325  std::string upper_string;
326 
327  if(interval.get_lower().id() == ID_min_value)
328  {
329  lower_string = "-INF";
330  }
331  else
332  {
333  INVARIANT(
334  interval.get_lower().is_constant(), "We only support constant limits");
335  lower_string =
337  }
338 
339  if(interval.get_upper().id() == ID_max_value)
340  {
341  upper_string = "+INF";
342  }
343  else
344  {
345  INVARIANT(
346  interval.get_upper().is_constant(), "We only support constant limits");
347  upper_string =
349  }
350 
351  out << "[" << lower_string << ", " << upper_string << "]";
352  }
353  else
354  {
355  abstract_objectt::output(out, ai, ns);
356  }
357 }
358 
360  const constant_interval_exprt &lhs,
361  const constant_interval_exprt &rhs)
362 {
363  auto widened = widened_ranget(lhs, rhs);
364 
365  // new interval ...
366  auto new_interval = constant_interval_exprt(
367  widened.widened_lower_bound, widened.widened_upper_bound);
368  return interval_abstract_valuet::make_interval(new_interval);
369 }
370 
372  const abstract_value_pointert &other,
373  const widen_modet &widen_mode) const
374 {
375  if(other->is_bottom())
376  return shared_from_this();
377 
378  auto other_interval = other->to_interval();
379 
380  if(is_bottom())
381  return make_interval(other_interval);
382 
383  if(interval.contains(other_interval))
384  return shared_from_this();
385 
386  if(widen_mode == widen_modet::could_widen)
387  return widening_merge(interval, other_interval);
388 
389  auto lower_bound = constant_interval_exprt::get_min(
390  interval.get_lower(), other_interval.get_lower());
391  auto upper_bound = constant_interval_exprt::get_max(
392  interval.get_upper(), other_interval.get_upper());
393 
394  return make_interval(lower_bound, upper_bound);
395 }
396 
398  const abstract_value_pointert &other) const
399 {
400  auto other_interval = other->to_interval();
401 
402  if(other_interval.contains(interval))
403  return shared_from_this();
404 
405  if(interval.contains(other_interval))
406  return make_interval(other_interval);
407 
408  auto lower_bound = constant_interval_exprt::get_max(
409  interval.get_lower(), other_interval.get_lower());
410  auto upper_bound = constant_interval_exprt::get_min(
411  interval.get_upper(), other_interval.get_upper());
412 
413  // if the interval is valid, we have a meet!
414  if(constant_interval_exprt::less_than_or_equal(lower_bound, upper_bound))
415  return make_interval(constant_interval_exprt(lower_bound, upper_bound));
416 
417  return make_interval(interval.type(), false, true);
418 }
419 
422 {
423  if(is_top() || is_bottom() || interval.is_top() || interval.is_bottom())
424  return make_empty_index_range();
425  if(
426  interval.get_lower().id() == ID_min_value ||
427  interval.get_upper().id() == ID_max_value)
428  {
429  return make_empty_index_range();
430  }
431 
433 }
434 
437 {
438  return make_single_value_range(shared_from_this());
439 }
440 
442  const exprt &lower,
443  const exprt &upper) const
444 {
445  auto lower_bound =
446  constant_interval_exprt::get_max(lower, interval.get_lower());
447  auto upper_bound =
448  constant_interval_exprt::get_min(upper, interval.get_upper());
449 
450  if(constant_interval_exprt::greater_than(lower_bound, upper_bound))
451  return as_value(mutable_clone());
452 
453  auto constrained_interval = constant_interval_exprt(lower_bound, upper_bound);
454  return as_value(
455  make_interval(constant_interval_exprt(lower_bound, upper_bound)));
456 }
457 
459 {
460  if(interval.is_single_value_interval())
461  return equal_exprt(name, interval.get_lower());
462 
463  auto lower_bound = binary_relation_exprt(interval.get_lower(), ID_le, name);
464  auto upper_bound = binary_relation_exprt(name, ID_le, interval.get_upper());
465 
466  return and_exprt(lower_bound, upper_bound);
467 }
468 
470  abstract_object_statisticst &statistics,
471  abstract_object_visitedt &visited,
472  const abstract_environmentt &env,
473  const namespacet &ns) const
474 {
475  abstract_objectt::get_statistics(statistics, visited, env, ns);
477  if(interval.is_single_value_interval())
478  {
480  }
481  statistics.objects_memory_usage += memory_sizet::from_bytes(sizeof(*this));
482 }
An abstract version of a program environment.
std::set< abstract_object_pointert > abstract_object_visitedt
sharing_ptrt< class abstract_objectt > abstract_object_pointert
Statistics gathering for the variable senstivity domain.
index_range_implementation_ptrt make_empty_index_range()
value_range_implementation_ptrt make_single_value_range(const abstract_object_pointert &value)
std::unique_ptr< index_range_implementationt > index_range_implementation_ptrt
std::unique_ptr< value_range_implementationt > value_range_implementation_ptrt
constant_exprt from_integer(const mp_integer &int_value, const typet &type)
Definition: arith_tools.cpp:99
Pre-defined bitvector types.
const signedbv_typet & to_signedbv_type(const typet &type)
Cast a typet to a signedbv_typet.
const unsignedbv_typet & to_unsignedbv_type(const typet &type)
Cast a typet to an unsignedbv_typet.
virtual exprt to_constant() const
Converts to a constant expression if possible.
virtual bool is_top() const
Find out if the abstract object is top.
virtual bool is_bottom() const
Find out if the abstract object is bottom.
virtual void output(std::ostream &out, const class ai_baset &ai, const namespacet &ns) const
Print the value of the abstract object.
virtual void get_statistics(abstract_object_statisticst &statistics, abstract_object_visitedt &visited, const abstract_environmentt &env, const namespacet &ns) const
virtual const typet & type() const
Get the real type of the variable this abstract object is representing.
sharing_ptrt< const abstract_value_objectt > as_value(const abstract_object_pointert &obj) const
sharing_ptrt< const abstract_value_objectt > abstract_value_pointert
This is the basic interface of the abstract interpreter with default implementations of the core func...
Definition: ai.h:117
Boolean AND.
Definition: std_expr.h:2125
A base class for expressions that are predicates, i.e., Boolean-typed, and that take exactly two argu...
Definition: std_expr.h:731
A base class for relations, i.e., binary predicates whose two operands have the same type.
Definition: std_expr.h:762
const irep_idt & get_value() const
Definition: std_expr.h:3008
Represents an interval of values.
Definition: interval.h:52
constant_interval_exprt bottom() const
Definition: interval.cpp:1058
tvt greater_than(const constant_interval_exprt &o) const
Definition: interval.cpp:397
tvt less_than_or_equal(const constant_interval_exprt &o) const
Definition: interval.cpp:403
static bool is_top(const constant_interval_exprt &a)
Definition: interval.cpp:1810
static exprt get_max(const exprt &a, const exprt &b)
Definition: interval.cpp:960
static exprt get_min(const exprt &a, const exprt &b)
Definition: interval.cpp:965
const exprt & get_lower() const
Definition: interval.cpp:27
const exprt & get_upper() const
Definition: interval.cpp:32
dstringt has one field, an unsigned integer no which is an index into a static table of strings.
Definition: dstring.h:38
Equality.
Definition: std_expr.h:1366
Base class for all expressions.
Definition: expr.h:56
bool is_one() const
Return whether the expression is a constant representing 1.
Definition: expr.cpp:96
bool is_true() const
Return whether the expression is a constant representing true.
Definition: expr.cpp:27
bool is_false() const
Return whether the expression is a constant representing false.
Definition: expr.cpp:34
bool is_zero() const
Return whether the expression is a constant representing 0.
Definition: expr.cpp:47
typet & type()
Return the type of the expression.
Definition: expr.h:84
bool is_constant() const
Return whether the expression is a constant.
Definition: expr.h:212
operandst & operands()
Definition: expr.h:94
mp_integer largest() const
Return the largest value that can be represented using this type.
mp_integer smallest() const
Return the smallest value that can be represented using this type.
abstract_object_pointert merge_with_value(const abstract_value_pointert &other, const widen_modet &widen_mode) const override
Merge another interval abstract object with this one.
interval_abstract_valuet(const typet &t, bool tp, bool bttm)
internal_abstract_object_pointert mutable_clone() const override
bool internal_equality(const abstract_object_pointert &other) const override
void output(std::ostream &out, const class ai_baset &ai, const class namespacet &ns) const override
void get_statistics(abstract_object_statisticst &statistics, abstract_object_visitedt &visited, const abstract_environmentt &env, const namespacet &ns) const override
index_range_implementation_ptrt index_range_implementation(const namespacet &ns) const override
exprt to_constant() const override
Converts to a constant expression if possible.
abstract_value_pointert constrain(const exprt &lower, const exprt &upper) const override
exprt to_predicate_internal(const exprt &name) const override
to_predicate implementation - derived classes will override
abstract_object_pointert meet_with_value(const abstract_value_pointert &other) const override
Meet another interval abstract object with this one.
value_range_implementation_ptrt value_range_implementation() const override
size_t internal_hash() const override
static std::shared_ptr< interval_abstract_valuet > make_interval(Args &&... args)
constant_interval_exprt interval
const exprt & current() const override
index_range_implementation_ptrt reset() const override
static exprt next_element(const exprt &cur, const namespacet &ns)
interval_index_ranget(const constant_interval_exprt &interval_, const namespacet &n)
const constant_interval_exprt & interval
const irep_idt & id() const
Definition: irep.h:388
+∞ upper bound for intervals
Definition: interval.h:18
static memory_sizet from_bytes(std::size_t bytes)
-∞ upper bound for intervals
Definition: interval.h:33
A namespacet is essentially one or two symbol tables bound together, to allow for symbol lookups in t...
Definition: namespace.h:94
The NIL expression.
Definition: std_expr.h:3091
The plus expression Associativity is not specified.
Definition: std_expr.h:1002
The type of an expression, extends irept.
Definition: type.h:29
const exprt & skip_typecast(const exprt &expr)
find the expression nested inside typecasts, if any
Definition: expr_util.cpp:193
Deprecated expression utility functions.
const constant_interval_exprt & to_constant_interval_expr(const exprt &expr)
Definition: interval.h:462
bool new_interval_is_top(const constant_interval_exprt &e)
static constant_interval_exprt interval_from_relation(const exprt &e)
Builds an interval representing all values satisfying the input expression.
static bool represents_interval(const exprt &expr)
static bool bvint_value_is_min(const typet &type, const mp_integer &value)
static mp_integer force_value_from_expr(const exprt &value)
abstract_object_pointert widening_merge(const constant_interval_exprt &lhs, const constant_interval_exprt &rhs)
static index_range_implementation_ptrt make_interval_index_range(const constant_interval_exprt &interval, const namespacet &n)
static constant_interval_exprt interval_from_x_gt_value(const exprt &value)
static constant_interval_exprt make_interval_expr(const exprt &expr)
static bool bvint_value_is_max(const typet &type, const mp_integer &value)
static irep_idt invert_relation(const irep_idt &relation)
static constant_interval_exprt interval_from_x_ge_value(const exprt &value)
static constant_interval_exprt interval_from_x_lt_value(const exprt &value)
static constant_interval_exprt interval_from_x_le_value(const exprt &value)
An interval to represent a set of possible values.
const std::string & id2string(const irep_idt &d)
Definition: irep.h:44
exprt simplify_expr(exprt src, const namespacet &ns)
BigInt mp_integer
Definition: smt_terms.h:17
#define PRECONDITION(CONDITION)
Definition: invariant.h:463
#define INVARIANT(CONDITION, REASON)
This macro uses the wrapper function 'invariant_violated_string'.
Definition: invariant.h:423
const constant_exprt & to_constant_expr(const exprt &expr)
Cast an exprt to a constant_exprt.
Definition: std_expr.h:3055
const binary_exprt & to_binary_expr(const exprt &expr)
Cast an exprt to a binary_exprt.
Definition: std_expr.h:715
memory_sizet objects_memory_usage
An underestimation of the memory usage of the abstract objects.