clang  6.0.0svn
RangeConstraintManager.cpp
Go to the documentation of this file.
1 //== RangeConstraintManager.cpp - Manage range constraints.------*- C++ -*--==//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file defines RangeConstraintManager, a class that tracks simple
11 // equality and inequality constraints on symbolic values of ProgramState.
12 //
13 //===----------------------------------------------------------------------===//
14 
19 #include "llvm/ADT/FoldingSet.h"
20 #include "llvm/ADT/ImmutableSet.h"
21 #include "llvm/Support/raw_ostream.h"
22 
23 using namespace clang;
24 using namespace ento;
25 
26 /// A Range represents the closed range [from, to]. The caller must
27 /// guarantee that from <= to. Note that Range is immutable, so as not
28 /// to subvert RangeSet's immutability.
29 namespace {
30 class Range : public std::pair<const llvm::APSInt *, const llvm::APSInt *> {
31 public:
32  Range(const llvm::APSInt &from, const llvm::APSInt &to)
33  : std::pair<const llvm::APSInt *, const llvm::APSInt *>(&from, &to) {
34  assert(from <= to);
35  }
36  bool Includes(const llvm::APSInt &v) const {
37  return *first <= v && v <= *second;
38  }
39  const llvm::APSInt &From() const { return *first; }
40  const llvm::APSInt &To() const { return *second; }
41  const llvm::APSInt *getConcreteValue() const {
42  return &From() == &To() ? &From() : nullptr;
43  }
44 
45  void Profile(llvm::FoldingSetNodeID &ID) const {
46  ID.AddPointer(&From());
47  ID.AddPointer(&To());
48  }
49 };
50 
51 class RangeTrait : public llvm::ImutContainerInfo<Range> {
52 public:
53  // When comparing if one Range is less than another, we should compare
54  // the actual APSInt values instead of their pointers. This keeps the order
55  // consistent (instead of comparing by pointer values) and can potentially
56  // be used to speed up some of the operations in RangeSet.
57  static inline bool isLess(key_type_ref lhs, key_type_ref rhs) {
58  return *lhs.first < *rhs.first ||
59  (!(*rhs.first < *lhs.first) && *lhs.second < *rhs.second);
60  }
61 };
62 
63 /// RangeSet contains a set of ranges. If the set is empty, then
64 /// there the value of a symbol is overly constrained and there are no
65 /// possible values for that symbol.
66 class RangeSet {
67  typedef llvm::ImmutableSet<Range, RangeTrait> PrimRangeSet;
68  PrimRangeSet ranges; // no need to make const, since it is an
69  // ImmutableSet - this allows default operator=
70  // to work.
71 public:
72  typedef PrimRangeSet::Factory Factory;
73  typedef PrimRangeSet::iterator iterator;
74 
75  RangeSet(PrimRangeSet RS) : ranges(RS) {}
76 
77  /// Create a new set with all ranges of this set and RS.
78  /// Possible intersections are not checked here.
79  RangeSet addRange(Factory &F, const RangeSet &RS) {
80  PrimRangeSet Ranges(RS.ranges);
81  for (const auto &range : ranges)
82  Ranges = F.add(Ranges, range);
83  return RangeSet(Ranges);
84  }
85 
86  iterator begin() const { return ranges.begin(); }
87  iterator end() const { return ranges.end(); }
88 
89  bool isEmpty() const { return ranges.isEmpty(); }
90 
91  /// Construct a new RangeSet representing '{ [from, to] }'.
92  RangeSet(Factory &F, const llvm::APSInt &from, const llvm::APSInt &to)
93  : ranges(F.add(F.getEmptySet(), Range(from, to))) {}
94 
95  /// Profile - Generates a hash profile of this RangeSet for use
96  /// by FoldingSet.
97  void Profile(llvm::FoldingSetNodeID &ID) const { ranges.Profile(ID); }
98 
99  /// getConcreteValue - If a symbol is contrained to equal a specific integer
100  /// constant then this method returns that value. Otherwise, it returns
101  /// NULL.
102  const llvm::APSInt *getConcreteValue() const {
103  return ranges.isSingleton() ? ranges.begin()->getConcreteValue() : nullptr;
104  }
105 
106 private:
107  void IntersectInRange(BasicValueFactory &BV, Factory &F,
108  const llvm::APSInt &Lower, const llvm::APSInt &Upper,
109  PrimRangeSet &newRanges, PrimRangeSet::iterator &i,
110  PrimRangeSet::iterator &e) const {
111  // There are six cases for each range R in the set:
112  // 1. R is entirely before the intersection range.
113  // 2. R is entirely after the intersection range.
114  // 3. R contains the entire intersection range.
115  // 4. R starts before the intersection range and ends in the middle.
116  // 5. R starts in the middle of the intersection range and ends after it.
117  // 6. R is entirely contained in the intersection range.
118  // These correspond to each of the conditions below.
119  for (/* i = begin(), e = end() */; i != e; ++i) {
120  if (i->To() < Lower) {
121  continue;
122  }
123  if (i->From() > Upper) {
124  break;
125  }
126 
127  if (i->Includes(Lower)) {
128  if (i->Includes(Upper)) {
129  newRanges =
130  F.add(newRanges, Range(BV.getValue(Lower), BV.getValue(Upper)));
131  break;
132  } else
133  newRanges = F.add(newRanges, Range(BV.getValue(Lower), i->To()));
134  } else {
135  if (i->Includes(Upper)) {
136  newRanges = F.add(newRanges, Range(i->From(), BV.getValue(Upper)));
137  break;
138  } else
139  newRanges = F.add(newRanges, *i);
140  }
141  }
142  }
143 
144  const llvm::APSInt &getMinValue() const {
145  assert(!isEmpty());
146  return ranges.begin()->From();
147  }
148 
149  bool pin(llvm::APSInt &Lower, llvm::APSInt &Upper) const {
150  // This function has nine cases, the cartesian product of range-testing
151  // both the upper and lower bounds against the symbol's type.
152  // Each case requires a different pinning operation.
153  // The function returns false if the described range is entirely outside
154  // the range of values for the associated symbol.
155  APSIntType Type(getMinValue());
156  APSIntType::RangeTestResultKind LowerTest = Type.testInRange(Lower, true);
157  APSIntType::RangeTestResultKind UpperTest = Type.testInRange(Upper, true);
158 
159  switch (LowerTest) {
161  switch (UpperTest) {
163  // The entire range is outside the symbol's set of possible values.
164  // If this is a conventionally-ordered range, the state is infeasible.
165  if (Lower <= Upper)
166  return false;
167 
168  // However, if the range wraps around, it spans all possible values.
169  Lower = Type.getMinValue();
170  Upper = Type.getMaxValue();
171  break;
173  // The range starts below what's possible but ends within it. Pin.
174  Lower = Type.getMinValue();
175  Type.apply(Upper);
176  break;
178  // The range spans all possible values for the symbol. Pin.
179  Lower = Type.getMinValue();
180  Upper = Type.getMaxValue();
181  break;
182  }
183  break;
185  switch (UpperTest) {
187  // The range wraps around, but all lower values are not possible.
188  Type.apply(Lower);
189  Upper = Type.getMaxValue();
190  break;
192  // The range may or may not wrap around, but both limits are valid.
193  Type.apply(Lower);
194  Type.apply(Upper);
195  break;
197  // The range starts within what's possible but ends above it. Pin.
198  Type.apply(Lower);
199  Upper = Type.getMaxValue();
200  break;
201  }
202  break;
204  switch (UpperTest) {
206  // The range wraps but is outside the symbol's set of possible values.
207  return false;
209  // The range starts above what's possible but ends within it (wrap).
210  Lower = Type.getMinValue();
211  Type.apply(Upper);
212  break;
214  // The entire range is outside the symbol's set of possible values.
215  // If this is a conventionally-ordered range, the state is infeasible.
216  if (Lower <= Upper)
217  return false;
218 
219  // However, if the range wraps around, it spans all possible values.
220  Lower = Type.getMinValue();
221  Upper = Type.getMaxValue();
222  break;
223  }
224  break;
225  }
226 
227  return true;
228  }
229 
230 public:
231  // Returns a set containing the values in the receiving set, intersected with
232  // the closed range [Lower, Upper]. Unlike the Range type, this range uses
233  // modular arithmetic, corresponding to the common treatment of C integer
234  // overflow. Thus, if the Lower bound is greater than the Upper bound, the
235  // range is taken to wrap around. This is equivalent to taking the
236  // intersection with the two ranges [Min, Upper] and [Lower, Max],
237  // or, alternatively, /removing/ all integers between Upper and Lower.
238  RangeSet Intersect(BasicValueFactory &BV, Factory &F, llvm::APSInt Lower,
239  llvm::APSInt Upper) const {
240  if (!pin(Lower, Upper))
241  return F.getEmptySet();
242 
243  PrimRangeSet newRanges = F.getEmptySet();
244 
245  PrimRangeSet::iterator i = begin(), e = end();
246  if (Lower <= Upper)
247  IntersectInRange(BV, F, Lower, Upper, newRanges, i, e);
248  else {
249  // The order of the next two statements is important!
250  // IntersectInRange() does not reset the iteration state for i and e.
251  // Therefore, the lower range most be handled first.
252  IntersectInRange(BV, F, BV.getMinValue(Upper), Upper, newRanges, i, e);
253  IntersectInRange(BV, F, Lower, BV.getMaxValue(Lower), newRanges, i, e);
254  }
255 
256  return newRanges;
257  }
258 
259  void print(raw_ostream &os) const {
260  bool isFirst = true;
261  os << "{ ";
262  for (iterator i = begin(), e = end(); i != e; ++i) {
263  if (isFirst)
264  isFirst = false;
265  else
266  os << ", ";
267 
268  os << '[' << i->From().toString(10) << ", " << i->To().toString(10)
269  << ']';
270  }
271  os << " }";
272  }
273 
274  bool operator==(const RangeSet &other) const {
275  return ranges == other.ranges;
276  }
277 };
278 } // end anonymous namespace
279 
282  RangeSet))
283 
284 namespace {
285 class RangeConstraintManager : public RangedConstraintManager {
286 public:
287  RangeConstraintManager(SubEngine *SE, SValBuilder &SVB)
288  : RangedConstraintManager(SE, SVB) {}
289 
290  //===------------------------------------------------------------------===//
291  // Implementation for interface from ConstraintManager.
292  //===------------------------------------------------------------------===//
293 
294  bool canReasonAbout(SVal X) const override;
295 
296  ConditionTruthVal checkNull(ProgramStateRef State, SymbolRef Sym) override;
297 
298  const llvm::APSInt *getSymVal(ProgramStateRef State,
299  SymbolRef Sym) const override;
300 
301  ProgramStateRef removeDeadBindings(ProgramStateRef State,
302  SymbolReaper &SymReaper) override;
303 
304  void print(ProgramStateRef State, raw_ostream &Out, const char *nl,
305  const char *sep) override;
306 
307  //===------------------------------------------------------------------===//
308  // Implementation for interface from RangedConstraintManager.
309  //===------------------------------------------------------------------===//
310 
311  ProgramStateRef assumeSymNE(ProgramStateRef State, SymbolRef Sym,
312  const llvm::APSInt &V,
313  const llvm::APSInt &Adjustment) override;
314 
315  ProgramStateRef assumeSymEQ(ProgramStateRef State, SymbolRef Sym,
316  const llvm::APSInt &V,
317  const llvm::APSInt &Adjustment) override;
318 
319  ProgramStateRef assumeSymLT(ProgramStateRef State, SymbolRef Sym,
320  const llvm::APSInt &V,
321  const llvm::APSInt &Adjustment) override;
322 
323  ProgramStateRef assumeSymGT(ProgramStateRef State, SymbolRef Sym,
324  const llvm::APSInt &V,
325  const llvm::APSInt &Adjustment) override;
326 
327  ProgramStateRef assumeSymLE(ProgramStateRef State, SymbolRef Sym,
328  const llvm::APSInt &V,
329  const llvm::APSInt &Adjustment) override;
330 
331  ProgramStateRef assumeSymGE(ProgramStateRef State, SymbolRef Sym,
332  const llvm::APSInt &V,
333  const llvm::APSInt &Adjustment) override;
334 
335  ProgramStateRef assumeSymWithinInclusiveRange(
336  ProgramStateRef State, SymbolRef Sym, const llvm::APSInt &From,
337  const llvm::APSInt &To, const llvm::APSInt &Adjustment) override;
338 
339  ProgramStateRef assumeSymOutsideInclusiveRange(
340  ProgramStateRef State, SymbolRef Sym, const llvm::APSInt &From,
341  const llvm::APSInt &To, const llvm::APSInt &Adjustment) override;
342 
343 private:
344  RangeSet::Factory F;
345 
346  RangeSet getRange(ProgramStateRef State, SymbolRef Sym);
347 
348  RangeSet getSymLTRange(ProgramStateRef St, SymbolRef Sym,
349  const llvm::APSInt &Int,
350  const llvm::APSInt &Adjustment);
351  RangeSet getSymGTRange(ProgramStateRef St, SymbolRef Sym,
352  const llvm::APSInt &Int,
353  const llvm::APSInt &Adjustment);
354  RangeSet getSymLERange(ProgramStateRef St, SymbolRef Sym,
355  const llvm::APSInt &Int,
356  const llvm::APSInt &Adjustment);
357  RangeSet getSymLERange(llvm::function_ref<RangeSet()> RS,
358  const llvm::APSInt &Int,
359  const llvm::APSInt &Adjustment);
360  RangeSet getSymGERange(ProgramStateRef St, SymbolRef Sym,
361  const llvm::APSInt &Int,
362  const llvm::APSInt &Adjustment);
363 };
364 
365 } // end anonymous namespace
366 
367 std::unique_ptr<ConstraintManager>
369  return llvm::make_unique<RangeConstraintManager>(Eng, StMgr.getSValBuilder());
370 }
371 
372 bool RangeConstraintManager::canReasonAbout(SVal X) const {
374  if (SymVal && SymVal->isExpression()) {
375  const SymExpr *SE = SymVal->getSymbol();
376 
377  if (const SymIntExpr *SIE = dyn_cast<SymIntExpr>(SE)) {
378  switch (SIE->getOpcode()) {
379  // We don't reason yet about bitwise-constraints on symbolic values.
380  case BO_And:
381  case BO_Or:
382  case BO_Xor:
383  return false;
384  // We don't reason yet about these arithmetic constraints on
385  // symbolic values.
386  case BO_Mul:
387  case BO_Div:
388  case BO_Rem:
389  case BO_Shl:
390  case BO_Shr:
391  return false;
392  // All other cases.
393  default:
394  return true;
395  }
396  }
397 
398  if (const SymSymExpr *SSE = dyn_cast<SymSymExpr>(SE)) {
399  // FIXME: Handle <=> here.
400  if (BinaryOperator::isEqualityOp(SSE->getOpcode()) ||
401  BinaryOperator::isRelationalOp(SSE->getOpcode())) {
402  // We handle Loc <> Loc comparisons, but not (yet) NonLoc <> NonLoc.
403  if (Loc::isLocType(SSE->getLHS()->getType())) {
404  assert(Loc::isLocType(SSE->getRHS()->getType()));
405  return true;
406  }
407  }
408  }
409 
410  return false;
411  }
412 
413  return true;
414 }
415 
416 ConditionTruthVal RangeConstraintManager::checkNull(ProgramStateRef State,
417  SymbolRef Sym) {
418  const RangeSet *Ranges = State->get<ConstraintRange>(Sym);
419 
420  // If we don't have any information about this symbol, it's underconstrained.
421  if (!Ranges)
422  return ConditionTruthVal();
423 
424  // If we have a concrete value, see if it's zero.
425  if (const llvm::APSInt *Value = Ranges->getConcreteValue())
426  return *Value == 0;
427 
428  BasicValueFactory &BV = getBasicVals();
429  APSIntType IntType = BV.getAPSIntType(Sym->getType());
430  llvm::APSInt Zero = IntType.getZeroValue();
431 
432  // Check if zero is in the set of possible values.
433  if (Ranges->Intersect(BV, F, Zero, Zero).isEmpty())
434  return false;
435 
436  // Zero is a possible value, but it is not the /only/ possible value.
437  return ConditionTruthVal();
438 }
439 
440 const llvm::APSInt *RangeConstraintManager::getSymVal(ProgramStateRef St,
441  SymbolRef Sym) const {
442  const ConstraintRangeTy::data_type *T = St->get<ConstraintRange>(Sym);
443  return T ? T->getConcreteValue() : nullptr;
444 }
445 
446 /// Scan all symbols referenced by the constraints. If the symbol is not alive
447 /// as marked in LSymbols, mark it as dead in DSymbols.
449 RangeConstraintManager::removeDeadBindings(ProgramStateRef State,
450  SymbolReaper &SymReaper) {
451  bool Changed = false;
452  ConstraintRangeTy CR = State->get<ConstraintRange>();
453  ConstraintRangeTy::Factory &CRFactory = State->get_context<ConstraintRange>();
454 
455  for (ConstraintRangeTy::iterator I = CR.begin(), E = CR.end(); I != E; ++I) {
456  SymbolRef Sym = I.getKey();
457  if (SymReaper.maybeDead(Sym)) {
458  Changed = true;
459  CR = CRFactory.remove(CR, Sym);
460  }
461  }
462 
463  return Changed ? State->set<ConstraintRange>(CR) : State;
464 }
465 
466 /// Return a range set subtracting zero from \p Domain.
467 static RangeSet assumeNonZero(
468  BasicValueFactory &BV,
469  RangeSet::Factory &F,
470  SymbolRef Sym,
471  RangeSet Domain) {
472  APSIntType IntType = BV.getAPSIntType(Sym->getType());
473  return Domain.Intersect(BV, F, ++IntType.getZeroValue(),
474  --IntType.getZeroValue());
475 }
476 
477 /// \brief Apply implicit constraints for bitwise OR- and AND-.
478 /// For unsigned types, bitwise OR with a constant always returns
479 /// a value greater-or-equal than the constant, and bitwise AND
480 /// returns a value less-or-equal then the constant.
481 ///
482 /// Pattern matches the expression \p Sym against those rule,
483 /// and applies the required constraints.
484 /// \p Input Previously established expression range set
485 static RangeSet applyBitwiseConstraints(
486  BasicValueFactory &BV,
487  RangeSet::Factory &F,
488  RangeSet Input,
489  const SymIntExpr* SIE) {
490  QualType T = SIE->getType();
491  bool IsUnsigned = T->isUnsignedIntegerType();
492  const llvm::APSInt &RHS = SIE->getRHS();
493  const llvm::APSInt &Zero = BV.getAPSIntType(T).getZeroValue();
494  BinaryOperator::Opcode Operator = SIE->getOpcode();
495 
496  // For unsigned types, the output of bitwise-or is bigger-or-equal than RHS.
497  if (Operator == BO_Or && IsUnsigned)
498  return Input.Intersect(BV, F, RHS, BV.getMaxValue(T));
499 
500  // Bitwise-or with a non-zero constant is always non-zero.
501  if (Operator == BO_Or && RHS != Zero)
502  return assumeNonZero(BV, F, SIE, Input);
503 
504  // For unsigned types, or positive RHS,
505  // bitwise-and output is always smaller-or-equal than RHS (assuming two's
506  // complement representation of signed types).
507  if (Operator == BO_And && (IsUnsigned || RHS >= Zero))
508  return Input.Intersect(BV, F, BV.getMinValue(T), RHS);
509 
510  return Input;
511 }
512 
513 RangeSet RangeConstraintManager::getRange(ProgramStateRef State,
514  SymbolRef Sym) {
515  if (ConstraintRangeTy::data_type *V = State->get<ConstraintRange>(Sym))
516  return *V;
517 
518  // Lazily generate a new RangeSet representing all possible values for the
519  // given symbol type.
520  BasicValueFactory &BV = getBasicVals();
521  QualType T = Sym->getType();
522 
523  RangeSet Result(F, BV.getMinValue(T), BV.getMaxValue(T));
524 
525  // References are known to be non-zero.
526  if (T->isReferenceType())
527  return assumeNonZero(BV, F, Sym, Result);
528 
529  // Known constraints on ranges of bitwise expressions.
530  if (const SymIntExpr* SIE = dyn_cast<SymIntExpr>(Sym))
531  return applyBitwiseConstraints(BV, F, Result, SIE);
532 
533  return Result;
534 }
535 
536 //===------------------------------------------------------------------------===
537 // assumeSymX methods: protected interface for RangeConstraintManager.
538 //===------------------------------------------------------------------------===/
539 
540 // The syntax for ranges below is mathematical, using [x, y] for closed ranges
541 // and (x, y) for open ranges. These ranges are modular, corresponding with
542 // a common treatment of C integer overflow. This means that these methods
543 // do not have to worry about overflow; RangeSet::Intersect can handle such a
544 // "wraparound" range.
545 // As an example, the range [UINT_MAX-1, 3) contains five values: UINT_MAX-1,
546 // UINT_MAX, 0, 1, and 2.
547 
549 RangeConstraintManager::assumeSymNE(ProgramStateRef St, SymbolRef Sym,
550  const llvm::APSInt &Int,
551  const llvm::APSInt &Adjustment) {
552  // Before we do any real work, see if the value can even show up.
553  APSIntType AdjustmentType(Adjustment);
554  if (AdjustmentType.testInRange(Int, true) != APSIntType::RTR_Within)
555  return St;
556 
557  llvm::APSInt Lower = AdjustmentType.convert(Int) - Adjustment;
558  llvm::APSInt Upper = Lower;
559  --Lower;
560  ++Upper;
561 
562  // [Int-Adjustment+1, Int-Adjustment-1]
563  // Notice that the lower bound is greater than the upper bound.
564  RangeSet New = getRange(St, Sym).Intersect(getBasicVals(), F, Upper, Lower);
565  return New.isEmpty() ? nullptr : St->set<ConstraintRange>(Sym, New);
566 }
567 
569 RangeConstraintManager::assumeSymEQ(ProgramStateRef St, SymbolRef Sym,
570  const llvm::APSInt &Int,
571  const llvm::APSInt &Adjustment) {
572  // Before we do any real work, see if the value can even show up.
573  APSIntType AdjustmentType(Adjustment);
574  if (AdjustmentType.testInRange(Int, true) != APSIntType::RTR_Within)
575  return nullptr;
576 
577  // [Int-Adjustment, Int-Adjustment]
578  llvm::APSInt AdjInt = AdjustmentType.convert(Int) - Adjustment;
579  RangeSet New = getRange(St, Sym).Intersect(getBasicVals(), F, AdjInt, AdjInt);
580  return New.isEmpty() ? nullptr : St->set<ConstraintRange>(Sym, New);
581 }
582 
583 RangeSet RangeConstraintManager::getSymLTRange(ProgramStateRef St,
584  SymbolRef Sym,
585  const llvm::APSInt &Int,
586  const llvm::APSInt &Adjustment) {
587  // Before we do any real work, see if the value can even show up.
588  APSIntType AdjustmentType(Adjustment);
589  switch (AdjustmentType.testInRange(Int, true)) {
591  return F.getEmptySet();
593  break;
595  return getRange(St, Sym);
596  }
597 
598  // Special case for Int == Min. This is always false.
599  llvm::APSInt ComparisonVal = AdjustmentType.convert(Int);
600  llvm::APSInt Min = AdjustmentType.getMinValue();
601  if (ComparisonVal == Min)
602  return F.getEmptySet();
603 
604  llvm::APSInt Lower = Min - Adjustment;
605  llvm::APSInt Upper = ComparisonVal - Adjustment;
606  --Upper;
607 
608  return getRange(St, Sym).Intersect(getBasicVals(), F, Lower, Upper);
609 }
610 
612 RangeConstraintManager::assumeSymLT(ProgramStateRef St, SymbolRef Sym,
613  const llvm::APSInt &Int,
614  const llvm::APSInt &Adjustment) {
615  RangeSet New = getSymLTRange(St, Sym, Int, Adjustment);
616  return New.isEmpty() ? nullptr : St->set<ConstraintRange>(Sym, New);
617 }
618 
619 RangeSet RangeConstraintManager::getSymGTRange(ProgramStateRef St,
620  SymbolRef Sym,
621  const llvm::APSInt &Int,
622  const llvm::APSInt &Adjustment) {
623  // Before we do any real work, see if the value can even show up.
624  APSIntType AdjustmentType(Adjustment);
625  switch (AdjustmentType.testInRange(Int, true)) {
627  return getRange(St, Sym);
629  break;
631  return F.getEmptySet();
632  }
633 
634  // Special case for Int == Max. This is always false.
635  llvm::APSInt ComparisonVal = AdjustmentType.convert(Int);
636  llvm::APSInt Max = AdjustmentType.getMaxValue();
637  if (ComparisonVal == Max)
638  return F.getEmptySet();
639 
640  llvm::APSInt Lower = ComparisonVal - Adjustment;
641  llvm::APSInt Upper = Max - Adjustment;
642  ++Lower;
643 
644  return getRange(St, Sym).Intersect(getBasicVals(), F, Lower, Upper);
645 }
646 
648 RangeConstraintManager::assumeSymGT(ProgramStateRef St, SymbolRef Sym,
649  const llvm::APSInt &Int,
650  const llvm::APSInt &Adjustment) {
651  RangeSet New = getSymGTRange(St, Sym, Int, Adjustment);
652  return New.isEmpty() ? nullptr : St->set<ConstraintRange>(Sym, New);
653 }
654 
655 RangeSet RangeConstraintManager::getSymGERange(ProgramStateRef St,
656  SymbolRef Sym,
657  const llvm::APSInt &Int,
658  const llvm::APSInt &Adjustment) {
659  // Before we do any real work, see if the value can even show up.
660  APSIntType AdjustmentType(Adjustment);
661  switch (AdjustmentType.testInRange(Int, true)) {
663  return getRange(St, Sym);
665  break;
667  return F.getEmptySet();
668  }
669 
670  // Special case for Int == Min. This is always feasible.
671  llvm::APSInt ComparisonVal = AdjustmentType.convert(Int);
672  llvm::APSInt Min = AdjustmentType.getMinValue();
673  if (ComparisonVal == Min)
674  return getRange(St, Sym);
675 
676  llvm::APSInt Max = AdjustmentType.getMaxValue();
677  llvm::APSInt Lower = ComparisonVal - Adjustment;
678  llvm::APSInt Upper = Max - Adjustment;
679 
680  return getRange(St, Sym).Intersect(getBasicVals(), F, Lower, Upper);
681 }
682 
684 RangeConstraintManager::assumeSymGE(ProgramStateRef St, SymbolRef Sym,
685  const llvm::APSInt &Int,
686  const llvm::APSInt &Adjustment) {
687  RangeSet New = getSymGERange(St, Sym, Int, Adjustment);
688  return New.isEmpty() ? nullptr : St->set<ConstraintRange>(Sym, New);
689 }
690 
691 RangeSet RangeConstraintManager::getSymLERange(
692  llvm::function_ref<RangeSet()> RS,
693  const llvm::APSInt &Int,
694  const llvm::APSInt &Adjustment) {
695  // Before we do any real work, see if the value can even show up.
696  APSIntType AdjustmentType(Adjustment);
697  switch (AdjustmentType.testInRange(Int, true)) {
699  return F.getEmptySet();
701  break;
703  return RS();
704  }
705 
706  // Special case for Int == Max. This is always feasible.
707  llvm::APSInt ComparisonVal = AdjustmentType.convert(Int);
708  llvm::APSInt Max = AdjustmentType.getMaxValue();
709  if (ComparisonVal == Max)
710  return RS();
711 
712  llvm::APSInt Min = AdjustmentType.getMinValue();
713  llvm::APSInt Lower = Min - Adjustment;
714  llvm::APSInt Upper = ComparisonVal - Adjustment;
715 
716  return RS().Intersect(getBasicVals(), F, Lower, Upper);
717 }
718 
719 RangeSet RangeConstraintManager::getSymLERange(ProgramStateRef St,
720  SymbolRef Sym,
721  const llvm::APSInt &Int,
722  const llvm::APSInt &Adjustment) {
723  return getSymLERange([&] { return getRange(St, Sym); }, Int, Adjustment);
724 }
725 
727 RangeConstraintManager::assumeSymLE(ProgramStateRef St, SymbolRef Sym,
728  const llvm::APSInt &Int,
729  const llvm::APSInt &Adjustment) {
730  RangeSet New = getSymLERange(St, Sym, Int, Adjustment);
731  return New.isEmpty() ? nullptr : St->set<ConstraintRange>(Sym, New);
732 }
733 
734 ProgramStateRef RangeConstraintManager::assumeSymWithinInclusiveRange(
735  ProgramStateRef State, SymbolRef Sym, const llvm::APSInt &From,
736  const llvm::APSInt &To, const llvm::APSInt &Adjustment) {
737  RangeSet New = getSymGERange(State, Sym, From, Adjustment);
738  if (New.isEmpty())
739  return nullptr;
740  RangeSet Out = getSymLERange([&] { return New; }, To, Adjustment);
741  return Out.isEmpty() ? nullptr : State->set<ConstraintRange>(Sym, Out);
742 }
743 
744 ProgramStateRef RangeConstraintManager::assumeSymOutsideInclusiveRange(
745  ProgramStateRef State, SymbolRef Sym, const llvm::APSInt &From,
746  const llvm::APSInt &To, const llvm::APSInt &Adjustment) {
747  RangeSet RangeLT = getSymLTRange(State, Sym, From, Adjustment);
748  RangeSet RangeGT = getSymGTRange(State, Sym, To, Adjustment);
749  RangeSet New(RangeLT.addRange(F, RangeGT));
750  return New.isEmpty() ? nullptr : State->set<ConstraintRange>(Sym, New);
751 }
752 
753 //===------------------------------------------------------------------------===
754 // Pretty-printing.
755 //===------------------------------------------------------------------------===/
756 
757 void RangeConstraintManager::print(ProgramStateRef St, raw_ostream &Out,
758  const char *nl, const char *sep) {
759 
760  ConstraintRangeTy Ranges = St->get<ConstraintRange>();
761 
762  if (Ranges.isEmpty()) {
763  Out << nl << sep << "Ranges are empty." << nl;
764  return;
765  }
766 
767  Out << nl << sep << "Ranges of symbol values:";
768  for (ConstraintRangeTy::iterator I = Ranges.begin(), E = Ranges.end(); I != E;
769  ++I) {
770  Out << nl << ' ' << I.getKey() << " : ";
771  I.getData().print(Out);
772  }
773  Out << nl;
774 }
A (possibly-)qualified type.
Definition: Type.h:653
Value is less than the minimum representable value.
Definition: APSIntType.h:78
bool operator==(CanQual< T > x, CanQual< U > y)
DominatorTree GraphTraits specialization so the DominatorTree can be iterable by generic graph iterat...
Definition: Dominators.h:26
bool maybeDead(SymbolRef sym)
If a symbol is known to be live, marks the symbol as live.
std::unique_ptr< ConstraintManager > CreateRangeConstraintManager(ProgramStateManager &statemgr, SubEngine *subengine)
The base class of the type hierarchy.
Definition: Type.h:1353
bool isUnsignedIntegerType() const
Return true if this is an integer type that is unsigned, according to C99 6.2.5p6 [which returns true...
Definition: Type.cpp:1840
bool isEqualityOp() const
Definition: Expr.h:3073
Symbolic value.
Definition: SymExpr.h:29
LineState State
Definition: Format.h:1900
bool isReferenceType() const
Definition: Type.h:5956
static bool isLocType(QualType T)
Definition: SVals.h:308
BinaryOperatorKind
Value is representable using this type.
Definition: APSIntType.h:79
A record of the "type" of an APSInt, used for conversions.
Definition: APSIntType.h:20
Represents a symbolic expression like &#39;x&#39; + 3.
bool isRelationalOp() const
Definition: Expr.h:3070
RangeTestResultKind testInRange(const llvm::APSInt &Val, bool AllowMixedSign) const LLVM_READONLY
Tests whether a given value is losslessly representable using this type.
Definition: APSIntType.cpp:16
llvm::APSInt getZeroValue() const LLVM_READONLY
Returns an all-zero value for this type.
Definition: APSIntType.h:56
virtual QualType getType() const =0
llvm::APSInt getMinValue() const LLVM_READONLY
Returns the minimum value for this type.
Definition: APSIntType.h:61
const FunctionProtoType * T
REGISTER_TRAIT_WITH_PROGRAMSTATE(ConstraintRange, CLANG_ENTO_PROGRAMSTATE_MAP(SymbolRef, RangeSet)) namespace
do v
Definition: arm_acle.h:78
static RangeSet assumeNonZero(BasicValueFactory &BV, RangeSet::Factory &F, SymbolRef Sym, RangeSet Domain)
Return a range set subtracting zero from Domain.
Optional< T > getAs() const
Convert to the specified SVal type, returning None if this SVal is not of the desired type...
Definition: SVals.h:100
llvm::APSInt getMaxValue() const LLVM_READONLY
Returns the maximum value for this type.
Definition: APSIntType.h:66
SVal - This represents a symbolic expression, which can be either an L-value or an R-value...
Definition: SVals.h:63
#define CLANG_ENTO_PROGRAMSTATE_MAP(Key, Value)
Helper for registering a map trait.
QualType getType() const override
A class responsible for cleaning up unused symbols.
Value is greater than the maximum representable value.
Definition: APSIntType.h:80
RangeTestResultKind
Used to classify whether a value is representable using this type.
Definition: APSIntType.h:77
llvm::APSInt convert(const llvm::APSInt &Value) const LLVM_READONLY
Convert and return a new APSInt with the given value, but this type&#39;s bit width and signedness...
Definition: APSIntType.h:49
Dataflow Directional Tag Classes.
const llvm::APSInt & getRHS() const
Represents symbolic expression.
Definition: SVals.h:327
const llvm::APSInt & getMinValue(const llvm::APSInt &v)
APSIntType getAPSIntType(QualType T) const
Returns the type of the APSInt used to store values of the given QualType.
static RangeSet applyBitwiseConstraints(BasicValueFactory &BV, RangeSet::Factory &F, RangeSet Input, const SymIntExpr *SIE)
Apply implicit constraints for bitwise OR- and AND-.
X
Add a minimal nested name specifier fixit hint to allow lookup of a tag name from an outer enclosing ...
Definition: SemaDecl.cpp:13010
BinaryOperator::Opcode getOpcode() const
const llvm::APSInt & getMaxValue(const llvm::APSInt &v)
Represents a symbolic expression like &#39;x&#39; + &#39;y&#39;.