RangeConstraintManager.cpp revision 651f13cea278ec967336033dd032faef0e9fc2ec
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
15#include "SimpleConstraintManager.h"
16#include "clang/StaticAnalyzer/Core/PathSensitive/APSIntType.h"
17#include "clang/StaticAnalyzer/Core/PathSensitive/ProgramState.h"
18#include "clang/StaticAnalyzer/Core/PathSensitive/ProgramStateTrait.h"
19#include "llvm/ADT/FoldingSet.h"
20#include "llvm/ADT/ImmutableSet.h"
21#include "llvm/Support/Debug.h"
22#include "llvm/Support/raw_ostream.h"
23
24using namespace clang;
25using namespace ento;
26
27/// A Range represents the closed range [from, to].  The caller must
28/// guarantee that from <= to.  Note that Range is immutable, so as not
29/// to subvert RangeSet's immutability.
30namespace {
31class Range : public std::pair<const llvm::APSInt*,
32                                                const llvm::APSInt*> {
33public:
34  Range(const llvm::APSInt &from, const llvm::APSInt &to)
35    : std::pair<const llvm::APSInt*, const llvm::APSInt*>(&from, &to) {
36    assert(from <= to);
37  }
38  bool Includes(const llvm::APSInt &v) const {
39    return *first <= v && v <= *second;
40  }
41  const llvm::APSInt &From() const {
42    return *first;
43  }
44  const llvm::APSInt &To() const {
45    return *second;
46  }
47  const llvm::APSInt *getConcreteValue() const {
48    return &From() == &To() ? &From() : NULL;
49  }
50
51  void Profile(llvm::FoldingSetNodeID &ID) const {
52    ID.AddPointer(&From());
53    ID.AddPointer(&To());
54  }
55};
56
57
58class RangeTrait : public llvm::ImutContainerInfo<Range> {
59public:
60  // When comparing if one Range is less than another, we should compare
61  // the actual APSInt values instead of their pointers.  This keeps the order
62  // consistent (instead of comparing by pointer values) and can potentially
63  // be used to speed up some of the operations in RangeSet.
64  static inline bool isLess(key_type_ref lhs, key_type_ref rhs) {
65    return *lhs.first < *rhs.first || (!(*rhs.first < *lhs.first) &&
66                                       *lhs.second < *rhs.second);
67  }
68};
69
70/// RangeSet contains a set of ranges. If the set is empty, then
71///  there the value of a symbol is overly constrained and there are no
72///  possible values for that symbol.
73class RangeSet {
74  typedef llvm::ImmutableSet<Range, RangeTrait> PrimRangeSet;
75  PrimRangeSet ranges; // no need to make const, since it is an
76                       // ImmutableSet - this allows default operator=
77                       // to work.
78public:
79  typedef PrimRangeSet::Factory Factory;
80  typedef PrimRangeSet::iterator iterator;
81
82  RangeSet(PrimRangeSet RS) : ranges(RS) {}
83
84  iterator begin() const { return ranges.begin(); }
85  iterator end() const { return ranges.end(); }
86
87  bool isEmpty() const { return ranges.isEmpty(); }
88
89  /// Construct a new RangeSet representing '{ [from, to] }'.
90  RangeSet(Factory &F, const llvm::APSInt &from, const llvm::APSInt &to)
91    : ranges(F.add(F.getEmptySet(), Range(from, to))) {}
92
93  /// Profile - Generates a hash profile of this RangeSet for use
94  ///  by FoldingSet.
95  void Profile(llvm::FoldingSetNodeID &ID) const { ranges.Profile(ID); }
96
97  /// getConcreteValue - If a symbol is contrained to equal a specific integer
98  ///  constant then this method returns that value.  Otherwise, it returns
99  ///  NULL.
100  const llvm::APSInt* getConcreteValue() const {
101    return ranges.isSingleton() ? ranges.begin()->getConcreteValue() : 0;
102  }
103
104private:
105  void IntersectInRange(BasicValueFactory &BV, Factory &F,
106                        const llvm::APSInt &Lower,
107                        const llvm::APSInt &Upper,
108                        PrimRangeSet &newRanges,
109                        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 = F.add(newRanges, Range(BV.getValue(Lower),
130                                             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) {
160    case APSIntType::RTR_Below:
161      switch (UpperTest) {
162      case APSIntType::RTR_Below:
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;
172      case APSIntType::RTR_Within:
173        // The range starts below what's possible but ends within it. Pin.
174        Lower = Type.getMinValue();
175        Type.apply(Upper);
176        break;
177      case APSIntType::RTR_Above:
178        // The range spans all possible values for the symbol. Pin.
179        Lower = Type.getMinValue();
180        Upper = Type.getMaxValue();
181        break;
182      }
183      break;
184    case APSIntType::RTR_Within:
185      switch (UpperTest) {
186      case APSIntType::RTR_Below:
187        // The range wraps around, but all lower values are not possible.
188        Type.apply(Lower);
189        Upper = Type.getMaxValue();
190        break;
191      case APSIntType::RTR_Within:
192        // The range may or may not wrap around, but both limits are valid.
193        Type.apply(Lower);
194        Type.apply(Upper);
195        break;
196      case APSIntType::RTR_Above:
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;
203    case APSIntType::RTR_Above:
204      switch (UpperTest) {
205      case APSIntType::RTR_Below:
206        // The range wraps but is outside the symbol's set of possible values.
207        return false;
208      case APSIntType::RTR_Within:
209        // The range starts above what's possible but ends within it (wrap).
210        Lower = Type.getMinValue();
211        Type.apply(Upper);
212        break;
213      case APSIntType::RTR_Above:
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
230public:
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,
239                     llvm::APSInt Lower, 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
280REGISTER_TRAIT_WITH_PROGRAMSTATE(ConstraintRange,
281                                 CLANG_ENTO_PROGRAMSTATE_MAP(SymbolRef,
282                                                             RangeSet))
283
284namespace {
285class RangeConstraintManager : public SimpleConstraintManager{
286  RangeSet GetRange(ProgramStateRef state, SymbolRef sym);
287public:
288  RangeConstraintManager(SubEngine *subengine, SValBuilder &SVB)
289    : SimpleConstraintManager(subengine, SVB) {}
290
291  ProgramStateRef assumeSymNE(ProgramStateRef state, SymbolRef sym,
292                             const llvm::APSInt& Int,
293                             const llvm::APSInt& Adjustment) override;
294
295  ProgramStateRef assumeSymEQ(ProgramStateRef state, SymbolRef sym,
296                             const llvm::APSInt& Int,
297                             const llvm::APSInt& Adjustment) override;
298
299  ProgramStateRef assumeSymLT(ProgramStateRef state, SymbolRef sym,
300                             const llvm::APSInt& Int,
301                             const llvm::APSInt& Adjustment) override;
302
303  ProgramStateRef assumeSymGT(ProgramStateRef state, SymbolRef sym,
304                             const llvm::APSInt& Int,
305                             const llvm::APSInt& Adjustment) override;
306
307  ProgramStateRef assumeSymGE(ProgramStateRef state, SymbolRef sym,
308                             const llvm::APSInt& Int,
309                             const llvm::APSInt& Adjustment) override;
310
311  ProgramStateRef assumeSymLE(ProgramStateRef state, SymbolRef sym,
312                             const llvm::APSInt& Int,
313                             const llvm::APSInt& Adjustment) override;
314
315  const llvm::APSInt* getSymVal(ProgramStateRef St,
316                                SymbolRef sym) const override;
317  ConditionTruthVal checkNull(ProgramStateRef State, SymbolRef Sym) override;
318
319  ProgramStateRef removeDeadBindings(ProgramStateRef St,
320                                     SymbolReaper& SymReaper) override;
321
322  void print(ProgramStateRef St, raw_ostream &Out,
323             const char* nl, const char *sep) override;
324
325private:
326  RangeSet::Factory F;
327};
328
329} // end anonymous namespace
330
331ConstraintManager *
332ento::CreateRangeConstraintManager(ProgramStateManager &StMgr, SubEngine *Eng) {
333  return new RangeConstraintManager(Eng, StMgr.getSValBuilder());
334}
335
336const llvm::APSInt* RangeConstraintManager::getSymVal(ProgramStateRef St,
337                                                      SymbolRef sym) const {
338  const ConstraintRangeTy::data_type *T = St->get<ConstraintRange>(sym);
339  return T ? T->getConcreteValue() : NULL;
340}
341
342ConditionTruthVal RangeConstraintManager::checkNull(ProgramStateRef State,
343                                                    SymbolRef Sym) {
344  const RangeSet *Ranges = State->get<ConstraintRange>(Sym);
345
346  // If we don't have any information about this symbol, it's underconstrained.
347  if (!Ranges)
348    return ConditionTruthVal();
349
350  // If we have a concrete value, see if it's zero.
351  if (const llvm::APSInt *Value = Ranges->getConcreteValue())
352    return *Value == 0;
353
354  BasicValueFactory &BV = getBasicVals();
355  APSIntType IntType = BV.getAPSIntType(Sym->getType());
356  llvm::APSInt Zero = IntType.getZeroValue();
357
358  // Check if zero is in the set of possible values.
359  if (Ranges->Intersect(BV, F, Zero, Zero).isEmpty())
360    return false;
361
362  // Zero is a possible value, but it is not the /only/ possible value.
363  return ConditionTruthVal();
364}
365
366/// Scan all symbols referenced by the constraints. If the symbol is not alive
367/// as marked in LSymbols, mark it as dead in DSymbols.
368ProgramStateRef
369RangeConstraintManager::removeDeadBindings(ProgramStateRef state,
370                                           SymbolReaper& SymReaper) {
371
372  ConstraintRangeTy CR = state->get<ConstraintRange>();
373  ConstraintRangeTy::Factory& CRFactory = state->get_context<ConstraintRange>();
374
375  for (ConstraintRangeTy::iterator I = CR.begin(), E = CR.end(); I != E; ++I) {
376    SymbolRef sym = I.getKey();
377    if (SymReaper.maybeDead(sym))
378      CR = CRFactory.remove(CR, sym);
379  }
380
381  return state->set<ConstraintRange>(CR);
382}
383
384RangeSet
385RangeConstraintManager::GetRange(ProgramStateRef state, SymbolRef sym) {
386  if (ConstraintRangeTy::data_type* V = state->get<ConstraintRange>(sym))
387    return *V;
388
389  // Lazily generate a new RangeSet representing all possible values for the
390  // given symbol type.
391  BasicValueFactory &BV = getBasicVals();
392  QualType T = sym->getType();
393
394  RangeSet Result(F, BV.getMinValue(T), BV.getMaxValue(T));
395
396  // Special case: references are known to be non-zero.
397  if (T->isReferenceType()) {
398    APSIntType IntType = BV.getAPSIntType(T);
399    Result = Result.Intersect(BV, F, ++IntType.getZeroValue(),
400                                     --IntType.getZeroValue());
401  }
402
403  return Result;
404}
405
406//===------------------------------------------------------------------------===
407// assumeSymX methods: public interface for RangeConstraintManager.
408//===------------------------------------------------------------------------===/
409
410// The syntax for ranges below is mathematical, using [x, y] for closed ranges
411// and (x, y) for open ranges. These ranges are modular, corresponding with
412// a common treatment of C integer overflow. This means that these methods
413// do not have to worry about overflow; RangeSet::Intersect can handle such a
414// "wraparound" range.
415// As an example, the range [UINT_MAX-1, 3) contains five values: UINT_MAX-1,
416// UINT_MAX, 0, 1, and 2.
417
418ProgramStateRef
419RangeConstraintManager::assumeSymNE(ProgramStateRef St, SymbolRef Sym,
420                                    const llvm::APSInt &Int,
421                                    const llvm::APSInt &Adjustment) {
422  // Before we do any real work, see if the value can even show up.
423  APSIntType AdjustmentType(Adjustment);
424  if (AdjustmentType.testInRange(Int, true) != APSIntType::RTR_Within)
425    return St;
426
427  llvm::APSInt Lower = AdjustmentType.convert(Int) - Adjustment;
428  llvm::APSInt Upper = Lower;
429  --Lower;
430  ++Upper;
431
432  // [Int-Adjustment+1, Int-Adjustment-1]
433  // Notice that the lower bound is greater than the upper bound.
434  RangeSet New = GetRange(St, Sym).Intersect(getBasicVals(), F, Upper, Lower);
435  return New.isEmpty() ? NULL : St->set<ConstraintRange>(Sym, New);
436}
437
438ProgramStateRef
439RangeConstraintManager::assumeSymEQ(ProgramStateRef St, SymbolRef Sym,
440                                    const llvm::APSInt &Int,
441                                    const llvm::APSInt &Adjustment) {
442  // Before we do any real work, see if the value can even show up.
443  APSIntType AdjustmentType(Adjustment);
444  if (AdjustmentType.testInRange(Int, true) != APSIntType::RTR_Within)
445    return NULL;
446
447  // [Int-Adjustment, Int-Adjustment]
448  llvm::APSInt AdjInt = AdjustmentType.convert(Int) - Adjustment;
449  RangeSet New = GetRange(St, Sym).Intersect(getBasicVals(), F, AdjInt, AdjInt);
450  return New.isEmpty() ? NULL : St->set<ConstraintRange>(Sym, New);
451}
452
453ProgramStateRef
454RangeConstraintManager::assumeSymLT(ProgramStateRef St, SymbolRef Sym,
455                                    const llvm::APSInt &Int,
456                                    const llvm::APSInt &Adjustment) {
457  // Before we do any real work, see if the value can even show up.
458  APSIntType AdjustmentType(Adjustment);
459  switch (AdjustmentType.testInRange(Int, true)) {
460  case APSIntType::RTR_Below:
461    return NULL;
462  case APSIntType::RTR_Within:
463    break;
464  case APSIntType::RTR_Above:
465    return St;
466  }
467
468  // Special case for Int == Min. This is always false.
469  llvm::APSInt ComparisonVal = AdjustmentType.convert(Int);
470  llvm::APSInt Min = AdjustmentType.getMinValue();
471  if (ComparisonVal == Min)
472    return NULL;
473
474  llvm::APSInt Lower = Min-Adjustment;
475  llvm::APSInt Upper = ComparisonVal-Adjustment;
476  --Upper;
477
478  RangeSet New = GetRange(St, Sym).Intersect(getBasicVals(), F, Lower, Upper);
479  return New.isEmpty() ? NULL : St->set<ConstraintRange>(Sym, New);
480}
481
482ProgramStateRef
483RangeConstraintManager::assumeSymGT(ProgramStateRef St, SymbolRef Sym,
484                                    const llvm::APSInt &Int,
485                                    const llvm::APSInt &Adjustment) {
486  // Before we do any real work, see if the value can even show up.
487  APSIntType AdjustmentType(Adjustment);
488  switch (AdjustmentType.testInRange(Int, true)) {
489  case APSIntType::RTR_Below:
490    return St;
491  case APSIntType::RTR_Within:
492    break;
493  case APSIntType::RTR_Above:
494    return NULL;
495  }
496
497  // Special case for Int == Max. This is always false.
498  llvm::APSInt ComparisonVal = AdjustmentType.convert(Int);
499  llvm::APSInt Max = AdjustmentType.getMaxValue();
500  if (ComparisonVal == Max)
501    return NULL;
502
503  llvm::APSInt Lower = ComparisonVal-Adjustment;
504  llvm::APSInt Upper = Max-Adjustment;
505  ++Lower;
506
507  RangeSet New = GetRange(St, Sym).Intersect(getBasicVals(), F, Lower, Upper);
508  return New.isEmpty() ? NULL : St->set<ConstraintRange>(Sym, New);
509}
510
511ProgramStateRef
512RangeConstraintManager::assumeSymGE(ProgramStateRef St, SymbolRef Sym,
513                                    const llvm::APSInt &Int,
514                                    const llvm::APSInt &Adjustment) {
515  // Before we do any real work, see if the value can even show up.
516  APSIntType AdjustmentType(Adjustment);
517  switch (AdjustmentType.testInRange(Int, true)) {
518  case APSIntType::RTR_Below:
519    return St;
520  case APSIntType::RTR_Within:
521    break;
522  case APSIntType::RTR_Above:
523    return NULL;
524  }
525
526  // Special case for Int == Min. This is always feasible.
527  llvm::APSInt ComparisonVal = AdjustmentType.convert(Int);
528  llvm::APSInt Min = AdjustmentType.getMinValue();
529  if (ComparisonVal == Min)
530    return St;
531
532  llvm::APSInt Max = AdjustmentType.getMaxValue();
533  llvm::APSInt Lower = ComparisonVal-Adjustment;
534  llvm::APSInt Upper = Max-Adjustment;
535
536  RangeSet New = GetRange(St, Sym).Intersect(getBasicVals(), F, Lower, Upper);
537  return New.isEmpty() ? NULL : St->set<ConstraintRange>(Sym, New);
538}
539
540ProgramStateRef
541RangeConstraintManager::assumeSymLE(ProgramStateRef St, SymbolRef Sym,
542                                    const llvm::APSInt &Int,
543                                    const llvm::APSInt &Adjustment) {
544  // Before we do any real work, see if the value can even show up.
545  APSIntType AdjustmentType(Adjustment);
546  switch (AdjustmentType.testInRange(Int, true)) {
547  case APSIntType::RTR_Below:
548    return NULL;
549  case APSIntType::RTR_Within:
550    break;
551  case APSIntType::RTR_Above:
552    return St;
553  }
554
555  // Special case for Int == Max. This is always feasible.
556  llvm::APSInt ComparisonVal = AdjustmentType.convert(Int);
557  llvm::APSInt Max = AdjustmentType.getMaxValue();
558  if (ComparisonVal == Max)
559    return St;
560
561  llvm::APSInt Min = AdjustmentType.getMinValue();
562  llvm::APSInt Lower = Min-Adjustment;
563  llvm::APSInt Upper = ComparisonVal-Adjustment;
564
565  RangeSet New = GetRange(St, Sym).Intersect(getBasicVals(), F, Lower, Upper);
566  return New.isEmpty() ? NULL : St->set<ConstraintRange>(Sym, New);
567}
568
569//===------------------------------------------------------------------------===
570// Pretty-printing.
571//===------------------------------------------------------------------------===/
572
573void RangeConstraintManager::print(ProgramStateRef St, raw_ostream &Out,
574                                   const char* nl, const char *sep) {
575
576  ConstraintRangeTy Ranges = St->get<ConstraintRange>();
577
578  if (Ranges.isEmpty()) {
579    Out << nl << sep << "Ranges are empty." << nl;
580    return;
581  }
582
583  Out << nl << sep << "Ranges of symbol values:";
584  for (ConstraintRangeTy::iterator I=Ranges.begin(), E=Ranges.end(); I!=E; ++I){
585    Out << nl << ' ' << I.getKey() << " : ";
586    I.getData().print(Out);
587  }
588  Out << nl;
589}
590