1 //== RangeConstraintManager.cpp - Manage range constraints.------*- C++ -*--==//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines RangeConstraintManager, a class that tracks simple
11 // equality and inequality constraints on symbolic values of ProgramState.
13 //===----------------------------------------------------------------------===//
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"
24 using namespace clang;
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.
31 class Range : public std::pair<const llvm::APSInt*,
32 const llvm::APSInt*> {
34 Range(const llvm::APSInt &from, const llvm::APSInt &to)
35 : std::pair<const llvm::APSInt*, const llvm::APSInt*>(&from, &to) {
38 bool Includes(const llvm::APSInt &v) const {
39 return *first <= v && v <= *second;
41 const llvm::APSInt &From() const {
44 const llvm::APSInt &To() const {
47 const llvm::APSInt *getConcreteValue() const {
48 return &From() == &To() ? &From() : nullptr;
51 void Profile(llvm::FoldingSetNodeID &ID) const {
52 ID.AddPointer(&From());
58 class RangeTrait : public llvm::ImutContainerInfo<Range> {
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);
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.
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=
79 typedef PrimRangeSet::Factory Factory;
80 typedef PrimRangeSet::iterator iterator;
82 RangeSet(PrimRangeSet RS) : ranges(RS) {}
84 iterator begin() const { return ranges.begin(); }
85 iterator end() const { return ranges.end(); }
87 bool isEmpty() const { return ranges.isEmpty(); }
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))) {}
93 /// Profile - Generates a hash profile of this RangeSet for use
95 void Profile(llvm::FoldingSetNodeID &ID) const { ranges.Profile(ID); }
97 /// getConcreteValue - If a symbol is contrained to equal a specific integer
98 /// constant then this method returns that value. Otherwise, it returns
100 const llvm::APSInt* getConcreteValue() const {
101 return ranges.isSingleton() ? ranges.begin()->getConcreteValue() : nullptr;
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) {
123 if (i->From() > Upper) {
127 if (i->Includes(Lower)) {
128 if (i->Includes(Upper)) {
129 newRanges = F.add(newRanges, Range(BV.getValue(Lower),
130 BV.getValue(Upper)));
133 newRanges = F.add(newRanges, Range(BV.getValue(Lower), i->To()));
135 if (i->Includes(Upper)) {
136 newRanges = F.add(newRanges, Range(i->From(), BV.getValue(Upper)));
139 newRanges = F.add(newRanges, *i);
144 const llvm::APSInt &getMinValue() const {
146 return ranges.begin()->From();
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);
160 case APSIntType::RTR_Below:
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.
168 // However, if the range wraps around, it spans all possible values.
169 Lower = Type.getMinValue();
170 Upper = Type.getMaxValue();
172 case APSIntType::RTR_Within:
173 // The range starts below what's possible but ends within it. Pin.
174 Lower = Type.getMinValue();
177 case APSIntType::RTR_Above:
178 // The range spans all possible values for the symbol. Pin.
179 Lower = Type.getMinValue();
180 Upper = Type.getMaxValue();
184 case APSIntType::RTR_Within:
186 case APSIntType::RTR_Below:
187 // The range wraps around, but all lower values are not possible.
189 Upper = Type.getMaxValue();
191 case APSIntType::RTR_Within:
192 // The range may or may not wrap around, but both limits are valid.
196 case APSIntType::RTR_Above:
197 // The range starts within what's possible but ends above it. Pin.
199 Upper = Type.getMaxValue();
203 case APSIntType::RTR_Above:
205 case APSIntType::RTR_Below:
206 // The range wraps but is outside the symbol's set of possible values.
208 case APSIntType::RTR_Within:
209 // The range starts above what's possible but ends within it (wrap).
210 Lower = Type.getMinValue();
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.
219 // However, if the range wraps around, it spans all possible values.
220 Lower = Type.getMinValue();
221 Upper = Type.getMaxValue();
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();
243 PrimRangeSet newRanges = F.getEmptySet();
245 PrimRangeSet::iterator i = begin(), e = end();
247 IntersectInRange(BV, F, Lower, Upper, newRanges, i, e);
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);
259 void print(raw_ostream &os) const {
262 for (iterator i = begin(), e = end(); i != e; ++i) {
268 os << '[' << i->From().toString(10) << ", " << i->To().toString(10)
274 bool operator==(const RangeSet &other) const {
275 return ranges == other.ranges;
278 } // end anonymous namespace
280 REGISTER_TRAIT_WITH_PROGRAMSTATE(ConstraintRange,
281 CLANG_ENTO_PROGRAMSTATE_MAP(SymbolRef,
285 class RangeConstraintManager : public SimpleConstraintManager{
286 RangeSet GetRange(ProgramStateRef state, SymbolRef sym);
288 RangeConstraintManager(SubEngine *subengine, SValBuilder &SVB)
289 : SimpleConstraintManager(subengine, SVB) {}
291 ProgramStateRef assumeSymNE(ProgramStateRef state, SymbolRef sym,
292 const llvm::APSInt& Int,
293 const llvm::APSInt& Adjustment) override;
295 ProgramStateRef assumeSymEQ(ProgramStateRef state, SymbolRef sym,
296 const llvm::APSInt& Int,
297 const llvm::APSInt& Adjustment) override;
299 ProgramStateRef assumeSymLT(ProgramStateRef state, SymbolRef sym,
300 const llvm::APSInt& Int,
301 const llvm::APSInt& Adjustment) override;
303 ProgramStateRef assumeSymGT(ProgramStateRef state, SymbolRef sym,
304 const llvm::APSInt& Int,
305 const llvm::APSInt& Adjustment) override;
307 ProgramStateRef assumeSymGE(ProgramStateRef state, SymbolRef sym,
308 const llvm::APSInt& Int,
309 const llvm::APSInt& Adjustment) override;
311 ProgramStateRef assumeSymLE(ProgramStateRef state, SymbolRef sym,
312 const llvm::APSInt& Int,
313 const llvm::APSInt& Adjustment) override;
315 const llvm::APSInt* getSymVal(ProgramStateRef St,
316 SymbolRef sym) const override;
317 ConditionTruthVal checkNull(ProgramStateRef State, SymbolRef Sym) override;
319 ProgramStateRef removeDeadBindings(ProgramStateRef St,
320 SymbolReaper& SymReaper) override;
322 void print(ProgramStateRef St, raw_ostream &Out,
323 const char* nl, const char *sep) override;
329 } // end anonymous namespace
331 std::unique_ptr<ConstraintManager>
332 ento::CreateRangeConstraintManager(ProgramStateManager &StMgr, SubEngine *Eng) {
333 return llvm::make_unique<RangeConstraintManager>(Eng, StMgr.getSValBuilder());
336 const 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() : nullptr;
342 ConditionTruthVal RangeConstraintManager::checkNull(ProgramStateRef State,
344 const RangeSet *Ranges = State->get<ConstraintRange>(Sym);
346 // If we don't have any information about this symbol, it's underconstrained.
348 return ConditionTruthVal();
350 // If we have a concrete value, see if it's zero.
351 if (const llvm::APSInt *Value = Ranges->getConcreteValue())
354 BasicValueFactory &BV = getBasicVals();
355 APSIntType IntType = BV.getAPSIntType(Sym->getType());
356 llvm::APSInt Zero = IntType.getZeroValue();
358 // Check if zero is in the set of possible values.
359 if (Ranges->Intersect(BV, F, Zero, Zero).isEmpty())
362 // Zero is a possible value, but it is not the /only/ possible value.
363 return ConditionTruthVal();
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.
369 RangeConstraintManager::removeDeadBindings(ProgramStateRef state,
370 SymbolReaper& SymReaper) {
372 ConstraintRangeTy CR = state->get<ConstraintRange>();
373 ConstraintRangeTy::Factory& CRFactory = state->get_context<ConstraintRange>();
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);
381 return state->set<ConstraintRange>(CR);
385 RangeConstraintManager::GetRange(ProgramStateRef state, SymbolRef sym) {
386 if (ConstraintRangeTy::data_type* V = state->get<ConstraintRange>(sym))
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();
394 RangeSet Result(F, BV.getMinValue(T), BV.getMaxValue(T));
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());
406 //===------------------------------------------------------------------------===
407 // assumeSymX methods: public interface for RangeConstraintManager.
408 //===------------------------------------------------------------------------===/
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.
419 RangeConstraintManager::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)
427 llvm::APSInt Lower = AdjustmentType.convert(Int) - Adjustment;
428 llvm::APSInt Upper = Lower;
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() ? nullptr : St->set<ConstraintRange>(Sym, New);
439 RangeConstraintManager::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)
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() ? nullptr : St->set<ConstraintRange>(Sym, New);
454 RangeConstraintManager::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:
462 case APSIntType::RTR_Within:
464 case APSIntType::RTR_Above:
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)
474 llvm::APSInt Lower = Min-Adjustment;
475 llvm::APSInt Upper = ComparisonVal-Adjustment;
478 RangeSet New = GetRange(St, Sym).Intersect(getBasicVals(), F, Lower, Upper);
479 return New.isEmpty() ? nullptr : St->set<ConstraintRange>(Sym, New);
483 RangeConstraintManager::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:
491 case APSIntType::RTR_Within:
493 case APSIntType::RTR_Above:
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)
503 llvm::APSInt Lower = ComparisonVal-Adjustment;
504 llvm::APSInt Upper = Max-Adjustment;
507 RangeSet New = GetRange(St, Sym).Intersect(getBasicVals(), F, Lower, Upper);
508 return New.isEmpty() ? nullptr : St->set<ConstraintRange>(Sym, New);
512 RangeConstraintManager::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:
520 case APSIntType::RTR_Within:
522 case APSIntType::RTR_Above:
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)
532 llvm::APSInt Max = AdjustmentType.getMaxValue();
533 llvm::APSInt Lower = ComparisonVal-Adjustment;
534 llvm::APSInt Upper = Max-Adjustment;
536 RangeSet New = GetRange(St, Sym).Intersect(getBasicVals(), F, Lower, Upper);
537 return New.isEmpty() ? nullptr : St->set<ConstraintRange>(Sym, New);
541 RangeConstraintManager::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:
549 case APSIntType::RTR_Within:
551 case APSIntType::RTR_Above:
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)
561 llvm::APSInt Min = AdjustmentType.getMinValue();
562 llvm::APSInt Lower = Min-Adjustment;
563 llvm::APSInt Upper = ComparisonVal-Adjustment;
565 RangeSet New = GetRange(St, Sym).Intersect(getBasicVals(), F, Lower, Upper);
566 return New.isEmpty() ? nullptr : St->set<ConstraintRange>(Sym, New);
569 //===------------------------------------------------------------------------===
571 //===------------------------------------------------------------------------===/
573 void RangeConstraintManager::print(ProgramStateRef St, raw_ostream &Out,
574 const char* nl, const char *sep) {
576 ConstraintRangeTy Ranges = St->get<ConstraintRange>();
578 if (Ranges.isEmpty()) {
579 Out << nl << sep << "Ranges are empty." << nl;
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);