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/Support/Debug.h"
20 #include "llvm/ADT/FoldingSet.h"
21 #include "llvm/ADT/ImmutableSet.h"
22 #include "llvm/Support/raw_ostream.h"
23 
24 using namespace clang;
25 using namespace ento;
26 
27 namespace { class ConstraintRange {}; }
28 static int ConstraintRangeIndex = 0;
29 
30 /// A Range represents the closed range [from, to].  The caller must
31 /// guarantee that from <= to.  Note that Range is immutable, so as not
32 /// to subvert RangeSet's immutability.
33 namespace {
34 class Range : public std::pair<const llvm::APSInt*,
35                                                 const llvm::APSInt*> {
36 public:
37   Range(const llvm::APSInt &from, const llvm::APSInt &to)
38     : std::pair<const llvm::APSInt*, const llvm::APSInt*>(&from, &to) {
39     assert(from <= to);
40   }
41   bool Includes(const llvm::APSInt &v) const {
42     return *first <= v && v <= *second;
43   }
44   const llvm::APSInt &From() const {
45     return *first;
46   }
47   const llvm::APSInt &To() const {
48     return *second;
49   }
50   const llvm::APSInt *getConcreteValue() const {
51     return &From() == &To() ? &From() : NULL;
52   }
53 
54   void Profile(llvm::FoldingSetNodeID &ID) const {
55     ID.AddPointer(&From());
56     ID.AddPointer(&To());
57   }
58 };
59 
60 
61 class RangeTrait : public llvm::ImutContainerInfo<Range> {
62 public:
63   // When comparing if one Range is less than another, we should compare
64   // the actual APSInt values instead of their pointers.  This keeps the order
65   // consistent (instead of comparing by pointer values) and can potentially
66   // be used to speed up some of the operations in RangeSet.
67   static inline bool isLess(key_type_ref lhs, key_type_ref rhs) {
68     return *lhs.first < *rhs.first || (!(*rhs.first < *lhs.first) &&
69                                        *lhs.second < *rhs.second);
70   }
71 };
72 
73 /// RangeSet contains a set of ranges. If the set is empty, then
74 ///  there the value of a symbol is overly constrained and there are no
75 ///  possible values for that symbol.
76 class RangeSet {
77   typedef llvm::ImmutableSet<Range, RangeTrait> PrimRangeSet;
78   PrimRangeSet ranges; // no need to make const, since it is an
79                        // ImmutableSet - this allows default operator=
80                        // to work.
81 public:
82   typedef PrimRangeSet::Factory Factory;
83   typedef PrimRangeSet::iterator iterator;
84 
85   RangeSet(PrimRangeSet RS) : ranges(RS) {}
86 
87   iterator begin() const { return ranges.begin(); }
88   iterator end() const { return ranges.end(); }
89 
90   bool isEmpty() const { return ranges.isEmpty(); }
91 
92   /// Construct a new RangeSet representing '{ [from, to] }'.
93   RangeSet(Factory &F, const llvm::APSInt &from, const llvm::APSInt &to)
94     : ranges(F.add(F.getEmptySet(), Range(from, to))) {}
95 
96   /// Profile - Generates a hash profile of this RangeSet for use
97   ///  by FoldingSet.
98   void Profile(llvm::FoldingSetNodeID &ID) const { ranges.Profile(ID); }
99 
100   /// getConcreteValue - If a symbol is contrained to equal a specific integer
101   ///  constant then this method returns that value.  Otherwise, it returns
102   ///  NULL.
103   const llvm::APSInt* getConcreteValue() const {
104     return ranges.isSingleton() ? ranges.begin()->getConcreteValue() : 0;
105   }
106 
107 private:
108   void IntersectInRange(BasicValueFactory &BV, Factory &F,
109                         const llvm::APSInt &Lower,
110                         const llvm::APSInt &Upper,
111                         PrimRangeSet &newRanges,
112                         PrimRangeSet::iterator &i,
113                         PrimRangeSet::iterator &e) const {
114     // There are six cases for each range R in the set:
115     //   1. R is entirely before the intersection range.
116     //   2. R is entirely after the intersection range.
117     //   3. R contains the entire intersection range.
118     //   4. R starts before the intersection range and ends in the middle.
119     //   5. R starts in the middle of the intersection range and ends after it.
120     //   6. R is entirely contained in the intersection range.
121     // These correspond to each of the conditions below.
122     for (/* i = begin(), e = end() */; i != e; ++i) {
123       if (i->To() < Lower) {
124         continue;
125       }
126       if (i->From() > Upper) {
127         break;
128       }
129 
130       if (i->Includes(Lower)) {
131         if (i->Includes(Upper)) {
132           newRanges = F.add(newRanges, Range(BV.getValue(Lower),
133                                              BV.getValue(Upper)));
134           break;
135         } else
136           newRanges = F.add(newRanges, Range(BV.getValue(Lower), i->To()));
137       } else {
138         if (i->Includes(Upper)) {
139           newRanges = F.add(newRanges, Range(i->From(), BV.getValue(Upper)));
140           break;
141         } else
142           newRanges = F.add(newRanges, *i);
143       }
144     }
145   }
146 
147   const llvm::APSInt &getMinValue() const {
148     assert(!isEmpty());
149     return ranges.begin()->From();
150   }
151 
152   bool pin(llvm::APSInt &Lower, llvm::APSInt &Upper) const {
153     // This function has nine cases, the cartesian product of range-testing
154     // both the upper and lower bounds against the symbol's type.
155     // Each case requires a different pinning operation.
156     // The function returns false if the described range is entirely outside
157     // the range of values for the associated symbol.
158     APSIntType Type(getMinValue());
159     APSIntType::RangeTestResultKind LowerTest = Type.testInRange(Lower);
160     APSIntType::RangeTestResultKind UpperTest = Type.testInRange(Upper);
161 
162     switch (LowerTest) {
163     case APSIntType::RTR_Below:
164       switch (UpperTest) {
165       case APSIntType::RTR_Below:
166         // The entire range is outside the symbol's set of possible values.
167         // If this is a conventionally-ordered range, the state is infeasible.
168         if (Lower < Upper)
169           return false;
170 
171         // However, if the range wraps around, it spans all possible values.
172         Lower = Type.getMinValue();
173         Upper = Type.getMaxValue();
174         break;
175       case APSIntType::RTR_Within:
176         // The range starts below what's possible but ends within it. Pin.
177         Lower = Type.getMinValue();
178         Type.apply(Upper);
179         break;
180       case APSIntType::RTR_Above:
181         // The range spans all possible values for the symbol. Pin.
182         Lower = Type.getMinValue();
183         Upper = Type.getMaxValue();
184         break;
185       }
186       break;
187     case APSIntType::RTR_Within:
188       switch (UpperTest) {
189       case APSIntType::RTR_Below:
190         // The range wraps around, but all lower values are not possible.
191         Type.apply(Lower);
192         Upper = Type.getMaxValue();
193         break;
194       case APSIntType::RTR_Within:
195         // The range may or may not wrap around, but both limits are valid.
196         Type.apply(Lower);
197         Type.apply(Upper);
198         break;
199       case APSIntType::RTR_Above:
200         // The range starts within what's possible but ends above it. Pin.
201         Type.apply(Lower);
202         Upper = Type.getMaxValue();
203         break;
204       }
205       break;
206     case APSIntType::RTR_Above:
207       switch (UpperTest) {
208       case APSIntType::RTR_Below:
209         // The range wraps but is outside the symbol's set of possible values.
210         return false;
211       case APSIntType::RTR_Within:
212         // The range starts above what's possible but ends within it (wrap).
213         Lower = Type.getMinValue();
214         Type.apply(Upper);
215         break;
216       case APSIntType::RTR_Above:
217         // The entire range is outside the symbol's set of possible values.
218         // If this is a conventionally-ordered range, the state is infeasible.
219         if (Lower < Upper)
220           return false;
221 
222         // However, if the range wraps around, it spans all possible values.
223         Lower = Type.getMinValue();
224         Upper = Type.getMaxValue();
225         break;
226       }
227       break;
228     }
229 
230     return true;
231   }
232 
233 public:
234   // Returns a set containing the values in the receiving set, intersected with
235   // the closed range [Lower, Upper]. Unlike the Range type, this range uses
236   // modular arithmetic, corresponding to the common treatment of C integer
237   // overflow. Thus, if the Lower bound is greater than the Upper bound, the
238   // range is taken to wrap around. This is equivalent to taking the
239   // intersection with the two ranges [Min, Upper] and [Lower, Max],
240   // or, alternatively, /removing/ all integers between Upper and Lower.
241   RangeSet Intersect(BasicValueFactory &BV, Factory &F,
242                      llvm::APSInt Lower, llvm::APSInt Upper) const {
243     if (!pin(Lower, Upper))
244       return F.getEmptySet();
245 
246     PrimRangeSet newRanges = F.getEmptySet();
247 
248     PrimRangeSet::iterator i = begin(), e = end();
249     if (Lower <= Upper)
250       IntersectInRange(BV, F, Lower, Upper, newRanges, i, e);
251     else {
252       // The order of the next two statements is important!
253       // IntersectInRange() does not reset the iteration state for i and e.
254       // Therefore, the lower range most be handled first.
255       IntersectInRange(BV, F, BV.getMinValue(Upper), Upper, newRanges, i, e);
256       IntersectInRange(BV, F, Lower, BV.getMaxValue(Lower), newRanges, i, e);
257     }
258 
259     return newRanges;
260   }
261 
262   void print(raw_ostream &os) const {
263     bool isFirst = true;
264     os << "{ ";
265     for (iterator i = begin(), e = end(); i != e; ++i) {
266       if (isFirst)
267         isFirst = false;
268       else
269         os << ", ";
270 
271       os << '[' << i->From().toString(10) << ", " << i->To().toString(10)
272          << ']';
273     }
274     os << " }";
275   }
276 
277   bool operator==(const RangeSet &other) const {
278     return ranges == other.ranges;
279   }
280 };
281 } // end anonymous namespace
282 
283 typedef llvm::ImmutableMap<SymbolRef,RangeSet> ConstraintRangeTy;
284 
285 namespace clang {
286 namespace ento {
287 template<>
288 struct ProgramStateTrait<ConstraintRange>
289   : public ProgramStatePartialTrait<ConstraintRangeTy> {
290   static inline void *GDMIndex() { return &ConstraintRangeIndex; }
291 };
292 }
293 }
294 
295 namespace {
296 class RangeConstraintManager : public SimpleConstraintManager{
297   RangeSet GetRange(ProgramStateRef state, SymbolRef sym);
298 public:
299   RangeConstraintManager(SubEngine &subengine, BasicValueFactory &BVF)
300     : SimpleConstraintManager(subengine, BVF) {}
301 
302   ProgramStateRef assumeSymNE(ProgramStateRef state, SymbolRef sym,
303                              const llvm::APSInt& Int,
304                              const llvm::APSInt& Adjustment);
305 
306   ProgramStateRef assumeSymEQ(ProgramStateRef state, SymbolRef sym,
307                              const llvm::APSInt& Int,
308                              const llvm::APSInt& Adjustment);
309 
310   ProgramStateRef assumeSymLT(ProgramStateRef state, SymbolRef sym,
311                              const llvm::APSInt& Int,
312                              const llvm::APSInt& Adjustment);
313 
314   ProgramStateRef assumeSymGT(ProgramStateRef state, SymbolRef sym,
315                              const llvm::APSInt& Int,
316                              const llvm::APSInt& Adjustment);
317 
318   ProgramStateRef assumeSymGE(ProgramStateRef state, SymbolRef sym,
319                              const llvm::APSInt& Int,
320                              const llvm::APSInt& Adjustment);
321 
322   ProgramStateRef assumeSymLE(ProgramStateRef state, SymbolRef sym,
323                              const llvm::APSInt& Int,
324                              const llvm::APSInt& Adjustment);
325 
326   const llvm::APSInt* getSymVal(ProgramStateRef St, SymbolRef sym) const;
327 
328   // FIXME: Refactor into SimpleConstraintManager?
329   bool isEqual(ProgramStateRef St, SymbolRef sym, const llvm::APSInt& V) const {
330     const llvm::APSInt *i = getSymVal(St, sym);
331     return i ? *i == V : false;
332   }
333 
334   ProgramStateRef removeDeadBindings(ProgramStateRef St, SymbolReaper& SymReaper);
335 
336   void print(ProgramStateRef St, raw_ostream &Out,
337              const char* nl, const char *sep);
338 
339 private:
340   RangeSet::Factory F;
341 };
342 
343 } // end anonymous namespace
344 
345 ConstraintManager *
346 ento::CreateRangeConstraintManager(ProgramStateManager &StMgr, SubEngine &Eng) {
347   return new RangeConstraintManager(Eng, StMgr.getBasicVals());
348 }
349 
350 const llvm::APSInt* RangeConstraintManager::getSymVal(ProgramStateRef St,
351                                                       SymbolRef sym) const {
352   const ConstraintRangeTy::data_type *T = St->get<ConstraintRange>(sym);
353   return T ? T->getConcreteValue() : NULL;
354 }
355 
356 /// Scan all symbols referenced by the constraints. If the symbol is not alive
357 /// as marked in LSymbols, mark it as dead in DSymbols.
358 ProgramStateRef
359 RangeConstraintManager::removeDeadBindings(ProgramStateRef state,
360                                            SymbolReaper& SymReaper) {
361 
362   ConstraintRangeTy CR = state->get<ConstraintRange>();
363   ConstraintRangeTy::Factory& CRFactory = state->get_context<ConstraintRange>();
364 
365   for (ConstraintRangeTy::iterator I = CR.begin(), E = CR.end(); I != E; ++I) {
366     SymbolRef sym = I.getKey();
367     if (SymReaper.maybeDead(sym))
368       CR = CRFactory.remove(CR, sym);
369   }
370 
371   return state->set<ConstraintRange>(CR);
372 }
373 
374 RangeSet
375 RangeConstraintManager::GetRange(ProgramStateRef state, SymbolRef sym) {
376   if (ConstraintRangeTy::data_type* V = state->get<ConstraintRange>(sym))
377     return *V;
378 
379   // Lazily generate a new RangeSet representing all possible values for the
380   // given symbol type.
381   BasicValueFactory &BV = getBasicVals();
382   QualType T = sym->getType(BV.getContext());
383   return RangeSet(F, BV.getMinValue(T), BV.getMaxValue(T));
384 }
385 
386 //===------------------------------------------------------------------------===
387 // assumeSymX methods: public interface for RangeConstraintManager.
388 //===------------------------------------------------------------------------===/
389 
390 // The syntax for ranges below is mathematical, using [x, y] for closed ranges
391 // and (x, y) for open ranges. These ranges are modular, corresponding with
392 // a common treatment of C integer overflow. This means that these methods
393 // do not have to worry about overflow; RangeSet::Intersect can handle such a
394 // "wraparound" range.
395 // As an example, the range [UINT_MAX-1, 3) contains five values: UINT_MAX-1,
396 // UINT_MAX, 0, 1, and 2.
397 
398 ProgramStateRef
399 RangeConstraintManager::assumeSymNE(ProgramStateRef St, SymbolRef Sym,
400                                     const llvm::APSInt &Int,
401                                     const llvm::APSInt &Adjustment) {
402   // Before we do any real work, see if the value can even show up.
403   APSIntType AdjustmentType(Adjustment);
404   if (AdjustmentType.testInRange(Int) != APSIntType::RTR_Within)
405     return St;
406 
407   llvm::APSInt Lower = AdjustmentType.convert(Int) - Adjustment;
408   llvm::APSInt Upper = Lower;
409   --Lower;
410   ++Upper;
411 
412   // [Int-Adjustment+1, Int-Adjustment-1]
413   // Notice that the lower bound is greater than the upper bound.
414   RangeSet New = GetRange(St, Sym).Intersect(getBasicVals(), F, Upper, Lower);
415   return New.isEmpty() ? NULL : St->set<ConstraintRange>(Sym, New);
416 }
417 
418 ProgramStateRef
419 RangeConstraintManager::assumeSymEQ(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) != APSIntType::RTR_Within)
425     return NULL;
426 
427   // [Int-Adjustment, Int-Adjustment]
428   llvm::APSInt AdjInt = AdjustmentType.convert(Int) - Adjustment;
429   RangeSet New = GetRange(St, Sym).Intersect(getBasicVals(), F, AdjInt, AdjInt);
430   return New.isEmpty() ? NULL : St->set<ConstraintRange>(Sym, New);
431 }
432 
433 ProgramStateRef
434 RangeConstraintManager::assumeSymLT(ProgramStateRef St, SymbolRef Sym,
435                                     const llvm::APSInt &Int,
436                                     const llvm::APSInt &Adjustment) {
437   // Before we do any real work, see if the value can even show up.
438   APSIntType AdjustmentType(Adjustment);
439   switch (AdjustmentType.testInRange(Int)) {
440   case APSIntType::RTR_Below:
441     return NULL;
442   case APSIntType::RTR_Within:
443     break;
444   case APSIntType::RTR_Above:
445     return St;
446   }
447 
448   // Special case for Int == Min. This is always false.
449   llvm::APSInt ComparisonVal = AdjustmentType.convert(Int);
450   llvm::APSInt Min = AdjustmentType.getMinValue();
451   if (ComparisonVal == Min)
452     return NULL;
453 
454   llvm::APSInt Lower = Min-Adjustment;
455   llvm::APSInt Upper = ComparisonVal-Adjustment;
456   --Upper;
457 
458   RangeSet New = GetRange(St, Sym).Intersect(getBasicVals(), F, Lower, Upper);
459   return New.isEmpty() ? NULL : St->set<ConstraintRange>(Sym, New);
460 }
461 
462 ProgramStateRef
463 RangeConstraintManager::assumeSymGT(ProgramStateRef St, SymbolRef Sym,
464                                     const llvm::APSInt &Int,
465                                     const llvm::APSInt &Adjustment) {
466   // Before we do any real work, see if the value can even show up.
467   APSIntType AdjustmentType(Adjustment);
468   switch (AdjustmentType.testInRange(Int)) {
469   case APSIntType::RTR_Below:
470     return St;
471   case APSIntType::RTR_Within:
472     break;
473   case APSIntType::RTR_Above:
474     return NULL;
475   }
476 
477   // Special case for Int == Max. This is always false.
478   llvm::APSInt ComparisonVal = AdjustmentType.convert(Int);
479   llvm::APSInt Max = AdjustmentType.getMaxValue();
480   if (ComparisonVal == Max)
481     return NULL;
482 
483   llvm::APSInt Lower = ComparisonVal-Adjustment;
484   llvm::APSInt Upper = Max-Adjustment;
485   ++Lower;
486 
487   RangeSet New = GetRange(St, Sym).Intersect(getBasicVals(), F, Lower, Upper);
488   return New.isEmpty() ? NULL : St->set<ConstraintRange>(Sym, New);
489 }
490 
491 ProgramStateRef
492 RangeConstraintManager::assumeSymGE(ProgramStateRef St, SymbolRef Sym,
493                                     const llvm::APSInt &Int,
494                                     const llvm::APSInt &Adjustment) {
495   // Before we do any real work, see if the value can even show up.
496   APSIntType AdjustmentType(Adjustment);
497   switch (AdjustmentType.testInRange(Int)) {
498   case APSIntType::RTR_Below:
499     return St;
500   case APSIntType::RTR_Within:
501     break;
502   case APSIntType::RTR_Above:
503     return NULL;
504   }
505 
506   // Special case for Int == Min. This is always feasible.
507   llvm::APSInt ComparisonVal = AdjustmentType.convert(Int);
508   llvm::APSInt Min = AdjustmentType.getMinValue();
509   if (ComparisonVal == Min)
510     return St;
511 
512   llvm::APSInt Max = AdjustmentType.getMaxValue();
513   llvm::APSInt Lower = ComparisonVal-Adjustment;
514   llvm::APSInt Upper = Max-Adjustment;
515 
516   RangeSet New = GetRange(St, Sym).Intersect(getBasicVals(), F, Lower, Upper);
517   return New.isEmpty() ? NULL : St->set<ConstraintRange>(Sym, New);
518 }
519 
520 ProgramStateRef
521 RangeConstraintManager::assumeSymLE(ProgramStateRef St, SymbolRef Sym,
522                                     const llvm::APSInt &Int,
523                                     const llvm::APSInt &Adjustment) {
524   // Before we do any real work, see if the value can even show up.
525   APSIntType AdjustmentType(Adjustment);
526   switch (AdjustmentType.testInRange(Int)) {
527   case APSIntType::RTR_Below:
528     return NULL;
529   case APSIntType::RTR_Within:
530     break;
531   case APSIntType::RTR_Above:
532     return St;
533   }
534 
535   // Special case for Int == Max. This is always feasible.
536   llvm::APSInt ComparisonVal = AdjustmentType.convert(Int);
537   llvm::APSInt Max = AdjustmentType.getMaxValue();
538   if (ComparisonVal == Max)
539     return St;
540 
541   llvm::APSInt Min = AdjustmentType.getMinValue();
542   llvm::APSInt Lower = Min-Adjustment;
543   llvm::APSInt Upper = ComparisonVal-Adjustment;
544 
545   RangeSet New = GetRange(St, Sym).Intersect(getBasicVals(), F, Lower, Upper);
546   return New.isEmpty() ? NULL : St->set<ConstraintRange>(Sym, New);
547 }
548 
549 //===------------------------------------------------------------------------===
550 // Pretty-printing.
551 //===------------------------------------------------------------------------===/
552 
553 void RangeConstraintManager::print(ProgramStateRef St, raw_ostream &Out,
554                                    const char* nl, const char *sep) {
555 
556   ConstraintRangeTy Ranges = St->get<ConstraintRange>();
557 
558   if (Ranges.isEmpty()) {
559     Out << nl << sep << "Ranges are empty." << nl;
560     return;
561   }
562 
563   Out << nl << sep << "Ranges of symbol values:";
564   for (ConstraintRangeTy::iterator I=Ranges.begin(), E=Ranges.end(); I!=E; ++I){
565     Out << nl << ' ' << I.getKey() << " : ";
566     I.getData().print(Out);
567   }
568   Out << nl;
569 }
570