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/ProgramState.h"
17 #include "clang/StaticAnalyzer/Core/PathSensitive/ProgramStateTrait.h"
18 #include "llvm/Support/Debug.h"
19 #include "llvm/ADT/FoldingSet.h"
20 #include "llvm/ADT/ImmutableSet.h"
21 #include "llvm/Support/raw_ostream.h"
23 using namespace clang;
26 namespace { class ConstraintRange {}; }
27 static int ConstraintRangeIndex = 0;
29 /// A Range represents the closed range [from, to]. The caller must
30 /// guarantee that from <= to. Note that Range is immutable, so as not
31 /// to subvert RangeSet's immutability.
33 class Range : public std::pair<const llvm::APSInt*,
34 const llvm::APSInt*> {
36 Range(const llvm::APSInt &from, const llvm::APSInt &to)
37 : std::pair<const llvm::APSInt*, const llvm::APSInt*>(&from, &to) {
40 bool Includes(const llvm::APSInt &v) const {
41 return *first <= v && v <= *second;
43 const llvm::APSInt &From() const {
46 const llvm::APSInt &To() const {
49 const llvm::APSInt *getConcreteValue() const {
50 return &From() == &To() ? &From() : NULL;
53 void Profile(llvm::FoldingSetNodeID &ID) const {
54 ID.AddPointer(&From());
60 class RangeTrait : public llvm::ImutContainerInfo<Range> {
62 // When comparing if one Range is less than another, we should compare
63 // the actual APSInt values instead of their pointers. This keeps the order
64 // consistent (instead of comparing by pointer values) and can potentially
65 // be used to speed up some of the operations in RangeSet.
66 static inline bool isLess(key_type_ref lhs, key_type_ref rhs) {
67 return *lhs.first < *rhs.first || (!(*rhs.first < *lhs.first) &&
68 *lhs.second < *rhs.second);
72 /// RangeSet contains a set of ranges. If the set is empty, then
73 /// there the value of a symbol is overly constrained and there are no
74 /// possible values for that symbol.
76 typedef llvm::ImmutableSet<Range, RangeTrait> PrimRangeSet;
77 PrimRangeSet ranges; // no need to make const, since it is an
78 // ImmutableSet - this allows default operator=
81 typedef PrimRangeSet::Factory Factory;
82 typedef PrimRangeSet::iterator iterator;
84 RangeSet(PrimRangeSet RS) : ranges(RS) {}
86 iterator begin() const { return ranges.begin(); }
87 iterator end() const { return ranges.end(); }
89 bool isEmpty() const { return ranges.isEmpty(); }
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))) {}
95 /// Profile - Generates a hash profile of this RangeSet for use
97 void Profile(llvm::FoldingSetNodeID &ID) const { ranges.Profile(ID); }
99 /// getConcreteValue - If a symbol is contrained to equal a specific integer
100 /// constant then this method returns that value. Otherwise, it returns
102 const llvm::APSInt* getConcreteValue() const {
103 return ranges.isSingleton() ? ranges.begin()->getConcreteValue() : 0;
107 void IntersectInRange(BasicValueFactory &BV, Factory &F,
108 const llvm::APSInt &Lower,
109 const llvm::APSInt &Upper,
110 PrimRangeSet &newRanges,
111 PrimRangeSet::iterator &i,
112 PrimRangeSet::iterator &e) const {
113 // There are six cases for each range R in the set:
114 // 1. R is entirely before the intersection range.
115 // 2. R is entirely after the intersection range.
116 // 3. R contains the entire intersection range.
117 // 4. R starts before the intersection range and ends in the middle.
118 // 5. R starts in the middle of the intersection range and ends after it.
119 // 6. R is entirely contained in the intersection range.
120 // These correspond to each of the conditions below.
121 for (/* i = begin(), e = end() */; i != e; ++i) {
122 if (i->To() < Lower) {
125 if (i->From() > Upper) {
129 if (i->Includes(Lower)) {
130 if (i->Includes(Upper)) {
131 newRanges = F.add(newRanges, Range(BV.getValue(Lower),
132 BV.getValue(Upper)));
135 newRanges = F.add(newRanges, Range(BV.getValue(Lower), i->To()));
137 if (i->Includes(Upper)) {
138 newRanges = F.add(newRanges, Range(i->From(), BV.getValue(Upper)));
141 newRanges = F.add(newRanges, *i);
147 // Returns a set containing the values in the receiving set, intersected with
148 // the closed range [Lower, Upper]. Unlike the Range type, this range uses
149 // modular arithmetic, corresponding to the common treatment of C integer
150 // overflow. Thus, if the Lower bound is greater than the Upper bound, the
151 // range is taken to wrap around. This is equivalent to taking the
152 // intersection with the two ranges [Min, Upper] and [Lower, Max],
153 // or, alternatively, /removing/ all integers between Upper and Lower.
154 RangeSet Intersect(BasicValueFactory &BV, Factory &F,
155 const llvm::APSInt &Lower,
156 const llvm::APSInt &Upper) const {
157 PrimRangeSet newRanges = F.getEmptySet();
159 PrimRangeSet::iterator i = begin(), e = end();
161 IntersectInRange(BV, F, Lower, Upper, newRanges, i, e);
163 // The order of the next two statements is important!
164 // IntersectInRange() does not reset the iteration state for i and e.
165 // Therefore, the lower range most be handled first.
166 IntersectInRange(BV, F, BV.getMinValue(Upper), Upper, newRanges, i, e);
167 IntersectInRange(BV, F, Lower, BV.getMaxValue(Lower), newRanges, i, e);
172 void print(raw_ostream &os) const {
175 for (iterator i = begin(), e = end(); i != e; ++i) {
181 os << '[' << i->From().toString(10) << ", " << i->To().toString(10)
187 bool operator==(const RangeSet &other) const {
188 return ranges == other.ranges;
191 } // end anonymous namespace
193 typedef llvm::ImmutableMap<SymbolRef,RangeSet> ConstraintRangeTy;
198 struct ProgramStateTrait<ConstraintRange>
199 : public ProgramStatePartialTrait<ConstraintRangeTy> {
200 static inline void *GDMIndex() { return &ConstraintRangeIndex; }
206 class RangeConstraintManager : public SimpleConstraintManager{
207 RangeSet GetRange(const ProgramState *state, SymbolRef sym);
209 RangeConstraintManager(SubEngine &subengine)
210 : SimpleConstraintManager(subengine) {}
212 const ProgramState *assumeSymNE(const ProgramState *state, SymbolRef sym,
213 const llvm::APSInt& Int,
214 const llvm::APSInt& Adjustment);
216 const ProgramState *assumeSymEQ(const ProgramState *state, SymbolRef sym,
217 const llvm::APSInt& Int,
218 const llvm::APSInt& Adjustment);
220 const ProgramState *assumeSymLT(const ProgramState *state, SymbolRef sym,
221 const llvm::APSInt& Int,
222 const llvm::APSInt& Adjustment);
224 const ProgramState *assumeSymGT(const ProgramState *state, SymbolRef sym,
225 const llvm::APSInt& Int,
226 const llvm::APSInt& Adjustment);
228 const ProgramState *assumeSymGE(const ProgramState *state, SymbolRef sym,
229 const llvm::APSInt& Int,
230 const llvm::APSInt& Adjustment);
232 const ProgramState *assumeSymLE(const ProgramState *state, SymbolRef sym,
233 const llvm::APSInt& Int,
234 const llvm::APSInt& Adjustment);
236 const llvm::APSInt* getSymVal(const ProgramState *St, SymbolRef sym) const;
238 // FIXME: Refactor into SimpleConstraintManager?
239 bool isEqual(const ProgramState *St, SymbolRef sym, const llvm::APSInt& V) const {
240 const llvm::APSInt *i = getSymVal(St, sym);
241 return i ? *i == V : false;
244 const ProgramState *removeDeadBindings(const ProgramState *St, SymbolReaper& SymReaper);
246 void print(const ProgramState *St, raw_ostream &Out,
247 const char* nl, const char *sep);
253 } // end anonymous namespace
255 ConstraintManager* ento::CreateRangeConstraintManager(ProgramStateManager&,
257 return new RangeConstraintManager(subeng);
260 const llvm::APSInt* RangeConstraintManager::getSymVal(const ProgramState *St,
261 SymbolRef sym) const {
262 const ConstraintRangeTy::data_type *T = St->get<ConstraintRange>(sym);
263 return T ? T->getConcreteValue() : NULL;
266 /// Scan all symbols referenced by the constraints. If the symbol is not alive
267 /// as marked in LSymbols, mark it as dead in DSymbols.
269 RangeConstraintManager::removeDeadBindings(const ProgramState *state,
270 SymbolReaper& SymReaper) {
272 ConstraintRangeTy CR = state->get<ConstraintRange>();
273 ConstraintRangeTy::Factory& CRFactory = state->get_context<ConstraintRange>();
275 for (ConstraintRangeTy::iterator I = CR.begin(), E = CR.end(); I != E; ++I) {
276 SymbolRef sym = I.getKey();
277 if (SymReaper.maybeDead(sym))
278 CR = CRFactory.remove(CR, sym);
281 return state->set<ConstraintRange>(CR);
285 RangeConstraintManager::GetRange(const ProgramState *state, SymbolRef sym) {
286 if (ConstraintRangeTy::data_type* V = state->get<ConstraintRange>(sym))
289 // Lazily generate a new RangeSet representing all possible values for the
290 // given symbol type.
291 QualType T = state->getSymbolManager().getType(sym);
292 BasicValueFactory& BV = state->getBasicVals();
293 return RangeSet(F, BV.getMinValue(T), BV.getMaxValue(T));
296 //===------------------------------------------------------------------------===
297 // assumeSymX methods: public interface for RangeConstraintManager.
298 //===------------------------------------------------------------------------===/
300 // The syntax for ranges below is mathematical, using [x, y] for closed ranges
301 // and (x, y) for open ranges. These ranges are modular, corresponding with
302 // a common treatment of C integer overflow. This means that these methods
303 // do not have to worry about overflow; RangeSet::Intersect can handle such a
304 // "wraparound" range.
305 // As an example, the range [UINT_MAX-1, 3) contains five values: UINT_MAX-1,
306 // UINT_MAX, 0, 1, and 2.
309 RangeConstraintManager::assumeSymNE(const ProgramState *state, SymbolRef sym,
310 const llvm::APSInt& Int,
311 const llvm::APSInt& Adjustment) {
312 BasicValueFactory &BV = state->getBasicVals();
314 llvm::APSInt Lower = Int-Adjustment;
315 llvm::APSInt Upper = Lower;
319 // [Int-Adjustment+1, Int-Adjustment-1]
320 // Notice that the lower bound is greater than the upper bound.
321 RangeSet New = GetRange(state, sym).Intersect(BV, F, Upper, Lower);
322 return New.isEmpty() ? NULL : state->set<ConstraintRange>(sym, New);
326 RangeConstraintManager::assumeSymEQ(const ProgramState *state, SymbolRef sym,
327 const llvm::APSInt& Int,
328 const llvm::APSInt& Adjustment) {
329 // [Int-Adjustment, Int-Adjustment]
330 BasicValueFactory &BV = state->getBasicVals();
331 llvm::APSInt AdjInt = Int-Adjustment;
332 RangeSet New = GetRange(state, sym).Intersect(BV, F, AdjInt, AdjInt);
333 return New.isEmpty() ? NULL : state->set<ConstraintRange>(sym, New);
337 RangeConstraintManager::assumeSymLT(const ProgramState *state, SymbolRef sym,
338 const llvm::APSInt& Int,
339 const llvm::APSInt& Adjustment) {
340 BasicValueFactory &BV = state->getBasicVals();
342 QualType T = state->getSymbolManager().getType(sym);
343 const llvm::APSInt &Min = BV.getMinValue(T);
345 // Special case for Int == Min. This is always false.
349 llvm::APSInt Lower = Min-Adjustment;
350 llvm::APSInt Upper = Int-Adjustment;
353 RangeSet New = GetRange(state, sym).Intersect(BV, F, Lower, Upper);
354 return New.isEmpty() ? NULL : state->set<ConstraintRange>(sym, New);
358 RangeConstraintManager::assumeSymGT(const ProgramState *state, SymbolRef sym,
359 const llvm::APSInt& Int,
360 const llvm::APSInt& Adjustment) {
361 BasicValueFactory &BV = state->getBasicVals();
363 QualType T = state->getSymbolManager().getType(sym);
364 const llvm::APSInt &Max = BV.getMaxValue(T);
366 // Special case for Int == Max. This is always false.
370 llvm::APSInt Lower = Int-Adjustment;
371 llvm::APSInt Upper = Max-Adjustment;
374 RangeSet New = GetRange(state, sym).Intersect(BV, F, Lower, Upper);
375 return New.isEmpty() ? NULL : state->set<ConstraintRange>(sym, New);
379 RangeConstraintManager::assumeSymGE(const ProgramState *state, SymbolRef sym,
380 const llvm::APSInt& Int,
381 const llvm::APSInt& Adjustment) {
382 BasicValueFactory &BV = state->getBasicVals();
384 QualType T = state->getSymbolManager().getType(sym);
385 const llvm::APSInt &Min = BV.getMinValue(T);
387 // Special case for Int == Min. This is always feasible.
391 const llvm::APSInt &Max = BV.getMaxValue(T);
393 llvm::APSInt Lower = Int-Adjustment;
394 llvm::APSInt Upper = Max-Adjustment;
396 RangeSet New = GetRange(state, sym).Intersect(BV, F, Lower, Upper);
397 return New.isEmpty() ? NULL : state->set<ConstraintRange>(sym, New);
401 RangeConstraintManager::assumeSymLE(const ProgramState *state, SymbolRef sym,
402 const llvm::APSInt& Int,
403 const llvm::APSInt& Adjustment) {
404 BasicValueFactory &BV = state->getBasicVals();
406 QualType T = state->getSymbolManager().getType(sym);
407 const llvm::APSInt &Max = BV.getMaxValue(T);
409 // Special case for Int == Max. This is always feasible.
413 const llvm::APSInt &Min = BV.getMinValue(T);
415 llvm::APSInt Lower = Min-Adjustment;
416 llvm::APSInt Upper = Int-Adjustment;
418 RangeSet New = GetRange(state, sym).Intersect(BV, F, Lower, Upper);
419 return New.isEmpty() ? NULL : state->set<ConstraintRange>(sym, New);
422 //===------------------------------------------------------------------------===
424 //===------------------------------------------------------------------------===/
426 void RangeConstraintManager::print(const ProgramState *St, raw_ostream &Out,
427 const char* nl, const char *sep) {
429 ConstraintRangeTy Ranges = St->get<ConstraintRange>();
431 if (Ranges.isEmpty())
434 Out << nl << sep << "ranges of symbol values:";
436 for (ConstraintRangeTy::iterator I=Ranges.begin(), E=Ranges.end(); I!=E; ++I){
437 Out << nl << ' ' << I.getKey() << " : ";
438 I.getData().print(Out);