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 "RangedConstraintManager.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/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) { 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 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 280 REGISTER_TRAIT_WITH_PROGRAMSTATE(ConstraintRange, 281 CLANG_ENTO_PROGRAMSTATE_MAP(SymbolRef, 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(const RangeSet &RS, const llvm::APSInt &Int, 358 const llvm::APSInt &Adjustment); 359 RangeSet getSymGERange(ProgramStateRef St, SymbolRef Sym, 360 const llvm::APSInt &Int, 361 const llvm::APSInt &Adjustment); 362 }; 363 364 } // end anonymous namespace 365 366 std::unique_ptr<ConstraintManager> 367 ento::CreateRangeConstraintManager(ProgramStateManager &StMgr, SubEngine *Eng) { 368 return llvm::make_unique<RangeConstraintManager>(Eng, StMgr.getSValBuilder()); 369 } 370 371 bool RangeConstraintManager::canReasonAbout(SVal X) const { 372 Optional<nonloc::SymbolVal> SymVal = X.getAs<nonloc::SymbolVal>(); 373 if (SymVal && SymVal->isExpression()) { 374 const SymExpr *SE = SymVal->getSymbol(); 375 376 if (const SymIntExpr *SIE = dyn_cast<SymIntExpr>(SE)) { 377 switch (SIE->getOpcode()) { 378 // We don't reason yet about bitwise-constraints on symbolic values. 379 case BO_And: 380 case BO_Or: 381 case BO_Xor: 382 return false; 383 // We don't reason yet about these arithmetic constraints on 384 // symbolic values. 385 case BO_Mul: 386 case BO_Div: 387 case BO_Rem: 388 case BO_Shl: 389 case BO_Shr: 390 return false; 391 // All other cases. 392 default: 393 return true; 394 } 395 } 396 397 if (const SymSymExpr *SSE = dyn_cast<SymSymExpr>(SE)) { 398 if (BinaryOperator::isComparisonOp(SSE->getOpcode())) { 399 // We handle Loc <> Loc comparisons, but not (yet) NonLoc <> NonLoc. 400 if (Loc::isLocType(SSE->getLHS()->getType())) { 401 assert(Loc::isLocType(SSE->getRHS()->getType())); 402 return true; 403 } 404 } 405 } 406 407 return false; 408 } 409 410 return true; 411 } 412 413 ConditionTruthVal RangeConstraintManager::checkNull(ProgramStateRef State, 414 SymbolRef Sym) { 415 const RangeSet *Ranges = State->get<ConstraintRange>(Sym); 416 417 // If we don't have any information about this symbol, it's underconstrained. 418 if (!Ranges) 419 return ConditionTruthVal(); 420 421 // If we have a concrete value, see if it's zero. 422 if (const llvm::APSInt *Value = Ranges->getConcreteValue()) 423 return *Value == 0; 424 425 BasicValueFactory &BV = getBasicVals(); 426 APSIntType IntType = BV.getAPSIntType(Sym->getType()); 427 llvm::APSInt Zero = IntType.getZeroValue(); 428 429 // Check if zero is in the set of possible values. 430 if (Ranges->Intersect(BV, F, Zero, Zero).isEmpty()) 431 return false; 432 433 // Zero is a possible value, but it is not the /only/ possible value. 434 return ConditionTruthVal(); 435 } 436 437 const llvm::APSInt *RangeConstraintManager::getSymVal(ProgramStateRef St, 438 SymbolRef Sym) const { 439 const ConstraintRangeTy::data_type *T = St->get<ConstraintRange>(Sym); 440 return T ? T->getConcreteValue() : nullptr; 441 } 442 443 /// Scan all symbols referenced by the constraints. If the symbol is not alive 444 /// as marked in LSymbols, mark it as dead in DSymbols. 445 ProgramStateRef 446 RangeConstraintManager::removeDeadBindings(ProgramStateRef State, 447 SymbolReaper &SymReaper) { 448 bool Changed = false; 449 ConstraintRangeTy CR = State->get<ConstraintRange>(); 450 ConstraintRangeTy::Factory &CRFactory = State->get_context<ConstraintRange>(); 451 452 for (ConstraintRangeTy::iterator I = CR.begin(), E = CR.end(); I != E; ++I) { 453 SymbolRef Sym = I.getKey(); 454 if (SymReaper.maybeDead(Sym)) { 455 Changed = true; 456 CR = CRFactory.remove(CR, Sym); 457 } 458 } 459 460 return Changed ? State->set<ConstraintRange>(CR) : State; 461 } 462 463 RangeSet RangeConstraintManager::getRange(ProgramStateRef State, 464 SymbolRef Sym) { 465 if (ConstraintRangeTy::data_type *V = State->get<ConstraintRange>(Sym)) 466 return *V; 467 468 // Lazily generate a new RangeSet representing all possible values for the 469 // given symbol type. 470 BasicValueFactory &BV = getBasicVals(); 471 QualType T = Sym->getType(); 472 473 RangeSet Result(F, BV.getMinValue(T), BV.getMaxValue(T)); 474 475 // Special case: references are known to be non-zero. 476 if (T->isReferenceType()) { 477 APSIntType IntType = BV.getAPSIntType(T); 478 Result = Result.Intersect(BV, F, ++IntType.getZeroValue(), 479 --IntType.getZeroValue()); 480 } 481 482 return Result; 483 } 484 485 //===------------------------------------------------------------------------=== 486 // assumeSymX methods: protected interface for RangeConstraintManager. 487 //===------------------------------------------------------------------------===/ 488 489 // The syntax for ranges below is mathematical, using [x, y] for closed ranges 490 // and (x, y) for open ranges. These ranges are modular, corresponding with 491 // a common treatment of C integer overflow. This means that these methods 492 // do not have to worry about overflow; RangeSet::Intersect can handle such a 493 // "wraparound" range. 494 // As an example, the range [UINT_MAX-1, 3) contains five values: UINT_MAX-1, 495 // UINT_MAX, 0, 1, and 2. 496 497 ProgramStateRef 498 RangeConstraintManager::assumeSymNE(ProgramStateRef St, SymbolRef Sym, 499 const llvm::APSInt &Int, 500 const llvm::APSInt &Adjustment) { 501 // Before we do any real work, see if the value can even show up. 502 APSIntType AdjustmentType(Adjustment); 503 if (AdjustmentType.testInRange(Int, true) != APSIntType::RTR_Within) 504 return St; 505 506 llvm::APSInt Lower = AdjustmentType.convert(Int) - Adjustment; 507 llvm::APSInt Upper = Lower; 508 --Lower; 509 ++Upper; 510 511 // [Int-Adjustment+1, Int-Adjustment-1] 512 // Notice that the lower bound is greater than the upper bound. 513 RangeSet New = getRange(St, Sym).Intersect(getBasicVals(), F, Upper, Lower); 514 return New.isEmpty() ? nullptr : St->set<ConstraintRange>(Sym, New); 515 } 516 517 ProgramStateRef 518 RangeConstraintManager::assumeSymEQ(ProgramStateRef St, SymbolRef Sym, 519 const llvm::APSInt &Int, 520 const llvm::APSInt &Adjustment) { 521 // Before we do any real work, see if the value can even show up. 522 APSIntType AdjustmentType(Adjustment); 523 if (AdjustmentType.testInRange(Int, true) != APSIntType::RTR_Within) 524 return nullptr; 525 526 // [Int-Adjustment, Int-Adjustment] 527 llvm::APSInt AdjInt = AdjustmentType.convert(Int) - Adjustment; 528 RangeSet New = getRange(St, Sym).Intersect(getBasicVals(), F, AdjInt, AdjInt); 529 return New.isEmpty() ? nullptr : St->set<ConstraintRange>(Sym, New); 530 } 531 532 RangeSet RangeConstraintManager::getSymLTRange(ProgramStateRef St, 533 SymbolRef Sym, 534 const llvm::APSInt &Int, 535 const llvm::APSInt &Adjustment) { 536 // Before we do any real work, see if the value can even show up. 537 APSIntType AdjustmentType(Adjustment); 538 switch (AdjustmentType.testInRange(Int, true)) { 539 case APSIntType::RTR_Below: 540 return F.getEmptySet(); 541 case APSIntType::RTR_Within: 542 break; 543 case APSIntType::RTR_Above: 544 return getRange(St, Sym); 545 } 546 547 // Special case for Int == Min. This is always false. 548 llvm::APSInt ComparisonVal = AdjustmentType.convert(Int); 549 llvm::APSInt Min = AdjustmentType.getMinValue(); 550 if (ComparisonVal == Min) 551 return F.getEmptySet(); 552 553 llvm::APSInt Lower = Min - Adjustment; 554 llvm::APSInt Upper = ComparisonVal - Adjustment; 555 --Upper; 556 557 return getRange(St, Sym).Intersect(getBasicVals(), F, Lower, Upper); 558 } 559 560 ProgramStateRef 561 RangeConstraintManager::assumeSymLT(ProgramStateRef St, SymbolRef Sym, 562 const llvm::APSInt &Int, 563 const llvm::APSInt &Adjustment) { 564 RangeSet New = getSymLTRange(St, Sym, Int, Adjustment); 565 return New.isEmpty() ? nullptr : St->set<ConstraintRange>(Sym, New); 566 } 567 568 RangeSet RangeConstraintManager::getSymGTRange(ProgramStateRef St, 569 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 switch (AdjustmentType.testInRange(Int, true)) { 575 case APSIntType::RTR_Below: 576 return getRange(St, Sym); 577 case APSIntType::RTR_Within: 578 break; 579 case APSIntType::RTR_Above: 580 return F.getEmptySet(); 581 } 582 583 // Special case for Int == Max. This is always false. 584 llvm::APSInt ComparisonVal = AdjustmentType.convert(Int); 585 llvm::APSInt Max = AdjustmentType.getMaxValue(); 586 if (ComparisonVal == Max) 587 return F.getEmptySet(); 588 589 llvm::APSInt Lower = ComparisonVal - Adjustment; 590 llvm::APSInt Upper = Max - Adjustment; 591 ++Lower; 592 593 return getRange(St, Sym).Intersect(getBasicVals(), F, Lower, Upper); 594 } 595 596 ProgramStateRef 597 RangeConstraintManager::assumeSymGT(ProgramStateRef St, SymbolRef Sym, 598 const llvm::APSInt &Int, 599 const llvm::APSInt &Adjustment) { 600 RangeSet New = getSymGTRange(St, Sym, Int, Adjustment); 601 return New.isEmpty() ? nullptr : St->set<ConstraintRange>(Sym, New); 602 } 603 604 RangeSet RangeConstraintManager::getSymGERange(ProgramStateRef St, 605 SymbolRef Sym, 606 const llvm::APSInt &Int, 607 const llvm::APSInt &Adjustment) { 608 // Before we do any real work, see if the value can even show up. 609 APSIntType AdjustmentType(Adjustment); 610 switch (AdjustmentType.testInRange(Int, true)) { 611 case APSIntType::RTR_Below: 612 return getRange(St, Sym); 613 case APSIntType::RTR_Within: 614 break; 615 case APSIntType::RTR_Above: 616 return F.getEmptySet(); 617 } 618 619 // Special case for Int == Min. This is always feasible. 620 llvm::APSInt ComparisonVal = AdjustmentType.convert(Int); 621 llvm::APSInt Min = AdjustmentType.getMinValue(); 622 if (ComparisonVal == Min) 623 return getRange(St, Sym); 624 625 llvm::APSInt Max = AdjustmentType.getMaxValue(); 626 llvm::APSInt Lower = ComparisonVal - Adjustment; 627 llvm::APSInt Upper = Max - Adjustment; 628 629 return getRange(St, Sym).Intersect(getBasicVals(), F, Lower, Upper); 630 } 631 632 ProgramStateRef 633 RangeConstraintManager::assumeSymGE(ProgramStateRef St, SymbolRef Sym, 634 const llvm::APSInt &Int, 635 const llvm::APSInt &Adjustment) { 636 RangeSet New = getSymGERange(St, Sym, Int, Adjustment); 637 return New.isEmpty() ? nullptr : St->set<ConstraintRange>(Sym, New); 638 } 639 640 RangeSet RangeConstraintManager::getSymLERange(const RangeSet &RS, 641 const llvm::APSInt &Int, 642 const llvm::APSInt &Adjustment) { 643 // Before we do any real work, see if the value can even show up. 644 APSIntType AdjustmentType(Adjustment); 645 switch (AdjustmentType.testInRange(Int, true)) { 646 case APSIntType::RTR_Below: 647 return F.getEmptySet(); 648 case APSIntType::RTR_Within: 649 break; 650 case APSIntType::RTR_Above: 651 return RS; 652 } 653 654 // Special case for Int == Max. This is always feasible. 655 llvm::APSInt ComparisonVal = AdjustmentType.convert(Int); 656 llvm::APSInt Max = AdjustmentType.getMaxValue(); 657 if (ComparisonVal == Max) 658 return RS; 659 660 llvm::APSInt Min = AdjustmentType.getMinValue(); 661 llvm::APSInt Lower = Min - Adjustment; 662 llvm::APSInt Upper = ComparisonVal - Adjustment; 663 664 return RS.Intersect(getBasicVals(), F, Lower, Upper); 665 } 666 667 RangeSet RangeConstraintManager::getSymLERange(ProgramStateRef St, 668 SymbolRef Sym, 669 const llvm::APSInt &Int, 670 const llvm::APSInt &Adjustment) { 671 // Before we do any real work, see if the value can even show up. 672 APSIntType AdjustmentType(Adjustment); 673 switch (AdjustmentType.testInRange(Int, true)) { 674 case APSIntType::RTR_Below: 675 return F.getEmptySet(); 676 case APSIntType::RTR_Within: 677 break; 678 case APSIntType::RTR_Above: 679 return getRange(St, Sym); 680 } 681 682 // Special case for Int == Max. This is always feasible. 683 llvm::APSInt ComparisonVal = AdjustmentType.convert(Int); 684 llvm::APSInt Max = AdjustmentType.getMaxValue(); 685 if (ComparisonVal == Max) 686 return getRange(St, Sym); 687 688 llvm::APSInt Min = AdjustmentType.getMinValue(); 689 llvm::APSInt Lower = Min - Adjustment; 690 llvm::APSInt Upper = ComparisonVal - Adjustment; 691 692 return getRange(St, Sym).Intersect(getBasicVals(), F, Lower, Upper); 693 } 694 695 ProgramStateRef 696 RangeConstraintManager::assumeSymLE(ProgramStateRef St, SymbolRef Sym, 697 const llvm::APSInt &Int, 698 const llvm::APSInt &Adjustment) { 699 RangeSet New = getSymLERange(St, Sym, Int, Adjustment); 700 return New.isEmpty() ? nullptr : St->set<ConstraintRange>(Sym, New); 701 } 702 703 ProgramStateRef RangeConstraintManager::assumeSymWithinInclusiveRange( 704 ProgramStateRef State, SymbolRef Sym, const llvm::APSInt &From, 705 const llvm::APSInt &To, const llvm::APSInt &Adjustment) { 706 RangeSet New = getSymGERange(State, Sym, From, Adjustment); 707 if (New.isEmpty()) 708 return nullptr; 709 New = getSymLERange(New, To, Adjustment); 710 return New.isEmpty() ? nullptr : State->set<ConstraintRange>(Sym, New); 711 } 712 713 ProgramStateRef RangeConstraintManager::assumeSymOutsideInclusiveRange( 714 ProgramStateRef State, SymbolRef Sym, const llvm::APSInt &From, 715 const llvm::APSInt &To, const llvm::APSInt &Adjustment) { 716 RangeSet RangeLT = getSymLTRange(State, Sym, From, Adjustment); 717 RangeSet RangeGT = getSymGTRange(State, Sym, To, Adjustment); 718 RangeSet New(RangeLT.addRange(F, RangeGT)); 719 return New.isEmpty() ? nullptr : State->set<ConstraintRange>(Sym, New); 720 } 721 722 //===------------------------------------------------------------------------=== 723 // Pretty-printing. 724 //===------------------------------------------------------------------------===/ 725 726 void RangeConstraintManager::print(ProgramStateRef St, raw_ostream &Out, 727 const char *nl, const char *sep) { 728 729 ConstraintRangeTy Ranges = St->get<ConstraintRange>(); 730 731 if (Ranges.isEmpty()) { 732 Out << nl << sep << "Ranges are empty." << nl; 733 return; 734 } 735 736 Out << nl << sep << "Ranges of symbol values:"; 737 for (ConstraintRangeTy::iterator I = Ranges.begin(), E = Ranges.end(); I != E; 738 ++I) { 739 Out << nl << ' ' << I.getKey() << " : "; 740 I.getData().print(Out); 741 } 742 Out << nl; 743 } 744