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 void RangeSet::IntersectInRange(BasicValueFactory &BV, Factory &F, 27 const llvm::APSInt &Lower, const llvm::APSInt &Upper, 28 PrimRangeSet &newRanges, PrimRangeSet::iterator &i, 29 PrimRangeSet::iterator &e) const { 30 // There are six cases for each range R in the set: 31 // 1. R is entirely before the intersection range. 32 // 2. R is entirely after the intersection range. 33 // 3. R contains the entire intersection range. 34 // 4. R starts before the intersection range and ends in the middle. 35 // 5. R starts in the middle of the intersection range and ends after it. 36 // 6. R is entirely contained in the intersection range. 37 // These correspond to each of the conditions below. 38 for (/* i = begin(), e = end() */; i != e; ++i) { 39 if (i->To() < Lower) { 40 continue; 41 } 42 if (i->From() > Upper) { 43 break; 44 } 45 46 if (i->Includes(Lower)) { 47 if (i->Includes(Upper)) { 48 newRanges = 49 F.add(newRanges, Range(BV.getValue(Lower), BV.getValue(Upper))); 50 break; 51 } else 52 newRanges = F.add(newRanges, Range(BV.getValue(Lower), i->To())); 53 } else { 54 if (i->Includes(Upper)) { 55 newRanges = F.add(newRanges, Range(i->From(), BV.getValue(Upper))); 56 break; 57 } else 58 newRanges = F.add(newRanges, *i); 59 } 60 } 61 } 62 63 const llvm::APSInt &RangeSet::getMinValue() const { 64 assert(!isEmpty()); 65 return ranges.begin()->From(); 66 } 67 68 bool RangeSet::pin(llvm::APSInt &Lower, llvm::APSInt &Upper) const { 69 // This function has nine cases, the cartesian product of range-testing 70 // both the upper and lower bounds against the symbol's type. 71 // Each case requires a different pinning operation. 72 // The function returns false if the described range is entirely outside 73 // the range of values for the associated symbol. 74 APSIntType Type(getMinValue()); 75 APSIntType::RangeTestResultKind LowerTest = Type.testInRange(Lower, true); 76 APSIntType::RangeTestResultKind UpperTest = Type.testInRange(Upper, true); 77 78 switch (LowerTest) { 79 case APSIntType::RTR_Below: 80 switch (UpperTest) { 81 case APSIntType::RTR_Below: 82 // The entire range is outside the symbol's set of possible values. 83 // If this is a conventionally-ordered range, the state is infeasible. 84 if (Lower <= Upper) 85 return false; 86 87 // However, if the range wraps around, it spans all possible values. 88 Lower = Type.getMinValue(); 89 Upper = Type.getMaxValue(); 90 break; 91 case APSIntType::RTR_Within: 92 // The range starts below what's possible but ends within it. Pin. 93 Lower = Type.getMinValue(); 94 Type.apply(Upper); 95 break; 96 case APSIntType::RTR_Above: 97 // The range spans all possible values for the symbol. Pin. 98 Lower = Type.getMinValue(); 99 Upper = Type.getMaxValue(); 100 break; 101 } 102 break; 103 case APSIntType::RTR_Within: 104 switch (UpperTest) { 105 case APSIntType::RTR_Below: 106 // The range wraps around, but all lower values are not possible. 107 Type.apply(Lower); 108 Upper = Type.getMaxValue(); 109 break; 110 case APSIntType::RTR_Within: 111 // The range may or may not wrap around, but both limits are valid. 112 Type.apply(Lower); 113 Type.apply(Upper); 114 break; 115 case APSIntType::RTR_Above: 116 // The range starts within what's possible but ends above it. Pin. 117 Type.apply(Lower); 118 Upper = Type.getMaxValue(); 119 break; 120 } 121 break; 122 case APSIntType::RTR_Above: 123 switch (UpperTest) { 124 case APSIntType::RTR_Below: 125 // The range wraps but is outside the symbol's set of possible values. 126 return false; 127 case APSIntType::RTR_Within: 128 // The range starts above what's possible but ends within it (wrap). 129 Lower = Type.getMinValue(); 130 Type.apply(Upper); 131 break; 132 case APSIntType::RTR_Above: 133 // The entire range is outside the symbol's set of possible values. 134 // If this is a conventionally-ordered range, the state is infeasible. 135 if (Lower <= Upper) 136 return false; 137 138 // However, if the range wraps around, it spans all possible values. 139 Lower = Type.getMinValue(); 140 Upper = Type.getMaxValue(); 141 break; 142 } 143 break; 144 } 145 146 return true; 147 } 148 149 // Returns a set containing the values in the receiving set, intersected with 150 // the closed range [Lower, Upper]. Unlike the Range type, this range uses 151 // modular arithmetic, corresponding to the common treatment of C integer 152 // overflow. Thus, if the Lower bound is greater than the Upper bound, the 153 // range is taken to wrap around. This is equivalent to taking the 154 // intersection with the two ranges [Min, Upper] and [Lower, Max], 155 // or, alternatively, /removing/ all integers between Upper and Lower. 156 RangeSet RangeSet::Intersect(BasicValueFactory &BV, Factory &F, 157 llvm::APSInt Lower, llvm::APSInt Upper) const { 158 if (!pin(Lower, Upper)) 159 return F.getEmptySet(); 160 161 PrimRangeSet newRanges = F.getEmptySet(); 162 163 PrimRangeSet::iterator i = begin(), e = end(); 164 if (Lower <= Upper) 165 IntersectInRange(BV, F, Lower, Upper, newRanges, i, e); 166 else { 167 // The order of the next two statements is important! 168 // IntersectInRange() does not reset the iteration state for i and e. 169 // Therefore, the lower range most be handled first. 170 IntersectInRange(BV, F, BV.getMinValue(Upper), Upper, newRanges, i, e); 171 IntersectInRange(BV, F, Lower, BV.getMaxValue(Lower), newRanges, i, e); 172 } 173 174 return newRanges; 175 } 176 177 void RangeSet::print(raw_ostream &os) const { 178 bool isFirst = true; 179 os << "{ "; 180 for (iterator i = begin(), e = end(); i != e; ++i) { 181 if (isFirst) 182 isFirst = false; 183 else 184 os << ", "; 185 186 os << '[' << i->From().toString(10) << ", " << i->To().toString(10) 187 << ']'; 188 } 189 os << " }"; 190 } 191 192 namespace { 193 class RangeConstraintManager : public RangedConstraintManager { 194 public: 195 RangeConstraintManager(SubEngine *SE, SValBuilder &SVB) 196 : RangedConstraintManager(SE, SVB) {} 197 198 //===------------------------------------------------------------------===// 199 // Implementation for interface from ConstraintManager. 200 //===------------------------------------------------------------------===// 201 202 bool canReasonAbout(SVal X) const override; 203 204 ConditionTruthVal checkNull(ProgramStateRef State, SymbolRef Sym) override; 205 206 const llvm::APSInt *getSymVal(ProgramStateRef State, 207 SymbolRef Sym) const override; 208 209 ProgramStateRef removeDeadBindings(ProgramStateRef State, 210 SymbolReaper &SymReaper) override; 211 212 void print(ProgramStateRef State, raw_ostream &Out, const char *nl, 213 const char *sep) override; 214 215 //===------------------------------------------------------------------===// 216 // Implementation for interface from RangedConstraintManager. 217 //===------------------------------------------------------------------===// 218 219 ProgramStateRef assumeSymNE(ProgramStateRef State, SymbolRef Sym, 220 const llvm::APSInt &V, 221 const llvm::APSInt &Adjustment) override; 222 223 ProgramStateRef assumeSymEQ(ProgramStateRef State, SymbolRef Sym, 224 const llvm::APSInt &V, 225 const llvm::APSInt &Adjustment) override; 226 227 ProgramStateRef assumeSymLT(ProgramStateRef State, SymbolRef Sym, 228 const llvm::APSInt &V, 229 const llvm::APSInt &Adjustment) override; 230 231 ProgramStateRef assumeSymGT(ProgramStateRef State, SymbolRef Sym, 232 const llvm::APSInt &V, 233 const llvm::APSInt &Adjustment) override; 234 235 ProgramStateRef assumeSymLE(ProgramStateRef State, SymbolRef Sym, 236 const llvm::APSInt &V, 237 const llvm::APSInt &Adjustment) override; 238 239 ProgramStateRef assumeSymGE(ProgramStateRef State, SymbolRef Sym, 240 const llvm::APSInt &V, 241 const llvm::APSInt &Adjustment) override; 242 243 ProgramStateRef assumeSymWithinInclusiveRange( 244 ProgramStateRef State, SymbolRef Sym, const llvm::APSInt &From, 245 const llvm::APSInt &To, const llvm::APSInt &Adjustment) override; 246 247 ProgramStateRef assumeSymOutsideInclusiveRange( 248 ProgramStateRef State, SymbolRef Sym, const llvm::APSInt &From, 249 const llvm::APSInt &To, const llvm::APSInt &Adjustment) override; 250 251 private: 252 RangeSet::Factory F; 253 254 RangeSet getRange(ProgramStateRef State, SymbolRef Sym); 255 256 RangeSet getSymLTRange(ProgramStateRef St, SymbolRef Sym, 257 const llvm::APSInt &Int, 258 const llvm::APSInt &Adjustment); 259 RangeSet getSymGTRange(ProgramStateRef St, SymbolRef Sym, 260 const llvm::APSInt &Int, 261 const llvm::APSInt &Adjustment); 262 RangeSet getSymLERange(ProgramStateRef St, SymbolRef Sym, 263 const llvm::APSInt &Int, 264 const llvm::APSInt &Adjustment); 265 RangeSet getSymLERange(llvm::function_ref<RangeSet()> RS, 266 const llvm::APSInt &Int, 267 const llvm::APSInt &Adjustment); 268 RangeSet getSymGERange(ProgramStateRef St, SymbolRef Sym, 269 const llvm::APSInt &Int, 270 const llvm::APSInt &Adjustment); 271 }; 272 273 } // end anonymous namespace 274 275 std::unique_ptr<ConstraintManager> 276 ento::CreateRangeConstraintManager(ProgramStateManager &StMgr, SubEngine *Eng) { 277 return llvm::make_unique<RangeConstraintManager>(Eng, StMgr.getSValBuilder()); 278 } 279 280 bool RangeConstraintManager::canReasonAbout(SVal X) const { 281 Optional<nonloc::SymbolVal> SymVal = X.getAs<nonloc::SymbolVal>(); 282 if (SymVal && SymVal->isExpression()) { 283 const SymExpr *SE = SymVal->getSymbol(); 284 285 if (const SymIntExpr *SIE = dyn_cast<SymIntExpr>(SE)) { 286 switch (SIE->getOpcode()) { 287 // We don't reason yet about bitwise-constraints on symbolic values. 288 case BO_And: 289 case BO_Or: 290 case BO_Xor: 291 return false; 292 // We don't reason yet about these arithmetic constraints on 293 // symbolic values. 294 case BO_Mul: 295 case BO_Div: 296 case BO_Rem: 297 case BO_Shl: 298 case BO_Shr: 299 return false; 300 // All other cases. 301 default: 302 return true; 303 } 304 } 305 306 if (const SymSymExpr *SSE = dyn_cast<SymSymExpr>(SE)) { 307 // FIXME: Handle <=> here. 308 if (BinaryOperator::isEqualityOp(SSE->getOpcode()) || 309 BinaryOperator::isRelationalOp(SSE->getOpcode())) { 310 // We handle Loc <> Loc comparisons, but not (yet) NonLoc <> NonLoc. 311 if (Loc::isLocType(SSE->getLHS()->getType())) { 312 assert(Loc::isLocType(SSE->getRHS()->getType())); 313 return true; 314 } 315 } 316 } 317 318 return false; 319 } 320 321 return true; 322 } 323 324 ConditionTruthVal RangeConstraintManager::checkNull(ProgramStateRef State, 325 SymbolRef Sym) { 326 const RangeSet *Ranges = State->get<ConstraintRange>(Sym); 327 328 // If we don't have any information about this symbol, it's underconstrained. 329 if (!Ranges) 330 return ConditionTruthVal(); 331 332 // If we have a concrete value, see if it's zero. 333 if (const llvm::APSInt *Value = Ranges->getConcreteValue()) 334 return *Value == 0; 335 336 BasicValueFactory &BV = getBasicVals(); 337 APSIntType IntType = BV.getAPSIntType(Sym->getType()); 338 llvm::APSInt Zero = IntType.getZeroValue(); 339 340 // Check if zero is in the set of possible values. 341 if (Ranges->Intersect(BV, F, Zero, Zero).isEmpty()) 342 return false; 343 344 // Zero is a possible value, but it is not the /only/ possible value. 345 return ConditionTruthVal(); 346 } 347 348 const llvm::APSInt *RangeConstraintManager::getSymVal(ProgramStateRef St, 349 SymbolRef Sym) const { 350 const ConstraintRangeTy::data_type *T = St->get<ConstraintRange>(Sym); 351 return T ? T->getConcreteValue() : nullptr; 352 } 353 354 /// Scan all symbols referenced by the constraints. If the symbol is not alive 355 /// as marked in LSymbols, mark it as dead in DSymbols. 356 ProgramStateRef 357 RangeConstraintManager::removeDeadBindings(ProgramStateRef State, 358 SymbolReaper &SymReaper) { 359 bool Changed = false; 360 ConstraintRangeTy CR = State->get<ConstraintRange>(); 361 ConstraintRangeTy::Factory &CRFactory = State->get_context<ConstraintRange>(); 362 363 for (ConstraintRangeTy::iterator I = CR.begin(), E = CR.end(); I != E; ++I) { 364 SymbolRef Sym = I.getKey(); 365 if (SymReaper.maybeDead(Sym)) { 366 Changed = true; 367 CR = CRFactory.remove(CR, Sym); 368 } 369 } 370 371 return Changed ? State->set<ConstraintRange>(CR) : State; 372 } 373 374 /// Return a range set subtracting zero from \p Domain. 375 static RangeSet assumeNonZero( 376 BasicValueFactory &BV, 377 RangeSet::Factory &F, 378 SymbolRef Sym, 379 RangeSet Domain) { 380 APSIntType IntType = BV.getAPSIntType(Sym->getType()); 381 return Domain.Intersect(BV, F, ++IntType.getZeroValue(), 382 --IntType.getZeroValue()); 383 } 384 385 /// Apply implicit constraints for bitwise OR- and AND-. 386 /// For unsigned types, bitwise OR with a constant always returns 387 /// a value greater-or-equal than the constant, and bitwise AND 388 /// returns a value less-or-equal then the constant. 389 /// 390 /// Pattern matches the expression \p Sym against those rule, 391 /// and applies the required constraints. 392 /// \p Input Previously established expression range set 393 static RangeSet applyBitwiseConstraints( 394 BasicValueFactory &BV, 395 RangeSet::Factory &F, 396 RangeSet Input, 397 const SymIntExpr* SIE) { 398 QualType T = SIE->getType(); 399 bool IsUnsigned = T->isUnsignedIntegerType(); 400 const llvm::APSInt &RHS = SIE->getRHS(); 401 const llvm::APSInt &Zero = BV.getAPSIntType(T).getZeroValue(); 402 BinaryOperator::Opcode Operator = SIE->getOpcode(); 403 404 // For unsigned types, the output of bitwise-or is bigger-or-equal than RHS. 405 if (Operator == BO_Or && IsUnsigned) 406 return Input.Intersect(BV, F, RHS, BV.getMaxValue(T)); 407 408 // Bitwise-or with a non-zero constant is always non-zero. 409 if (Operator == BO_Or && RHS != Zero) 410 return assumeNonZero(BV, F, SIE, Input); 411 412 // For unsigned types, or positive RHS, 413 // bitwise-and output is always smaller-or-equal than RHS (assuming two's 414 // complement representation of signed types). 415 if (Operator == BO_And && (IsUnsigned || RHS >= Zero)) 416 return Input.Intersect(BV, F, BV.getMinValue(T), RHS); 417 418 return Input; 419 } 420 421 RangeSet RangeConstraintManager::getRange(ProgramStateRef State, 422 SymbolRef Sym) { 423 if (ConstraintRangeTy::data_type *V = State->get<ConstraintRange>(Sym)) 424 return *V; 425 426 // Lazily generate a new RangeSet representing all possible values for the 427 // given symbol type. 428 BasicValueFactory &BV = getBasicVals(); 429 QualType T = Sym->getType(); 430 431 RangeSet Result(F, BV.getMinValue(T), BV.getMaxValue(T)); 432 433 // References are known to be non-zero. 434 if (T->isReferenceType()) 435 return assumeNonZero(BV, F, Sym, Result); 436 437 // Known constraints on ranges of bitwise expressions. 438 if (const SymIntExpr* SIE = dyn_cast<SymIntExpr>(Sym)) 439 return applyBitwiseConstraints(BV, F, Result, SIE); 440 441 return Result; 442 } 443 444 //===------------------------------------------------------------------------=== 445 // assumeSymX methods: protected interface for RangeConstraintManager. 446 //===------------------------------------------------------------------------===/ 447 448 // The syntax for ranges below is mathematical, using [x, y] for closed ranges 449 // and (x, y) for open ranges. These ranges are modular, corresponding with 450 // a common treatment of C integer overflow. This means that these methods 451 // do not have to worry about overflow; RangeSet::Intersect can handle such a 452 // "wraparound" range. 453 // As an example, the range [UINT_MAX-1, 3) contains five values: UINT_MAX-1, 454 // UINT_MAX, 0, 1, and 2. 455 456 ProgramStateRef 457 RangeConstraintManager::assumeSymNE(ProgramStateRef St, SymbolRef Sym, 458 const llvm::APSInt &Int, 459 const llvm::APSInt &Adjustment) { 460 // Before we do any real work, see if the value can even show up. 461 APSIntType AdjustmentType(Adjustment); 462 if (AdjustmentType.testInRange(Int, true) != APSIntType::RTR_Within) 463 return St; 464 465 llvm::APSInt Lower = AdjustmentType.convert(Int) - Adjustment; 466 llvm::APSInt Upper = Lower; 467 --Lower; 468 ++Upper; 469 470 // [Int-Adjustment+1, Int-Adjustment-1] 471 // Notice that the lower bound is greater than the upper bound. 472 RangeSet New = getRange(St, Sym).Intersect(getBasicVals(), F, Upper, Lower); 473 return New.isEmpty() ? nullptr : St->set<ConstraintRange>(Sym, New); 474 } 475 476 ProgramStateRef 477 RangeConstraintManager::assumeSymEQ(ProgramStateRef St, SymbolRef Sym, 478 const llvm::APSInt &Int, 479 const llvm::APSInt &Adjustment) { 480 // Before we do any real work, see if the value can even show up. 481 APSIntType AdjustmentType(Adjustment); 482 if (AdjustmentType.testInRange(Int, true) != APSIntType::RTR_Within) 483 return nullptr; 484 485 // [Int-Adjustment, Int-Adjustment] 486 llvm::APSInt AdjInt = AdjustmentType.convert(Int) - Adjustment; 487 RangeSet New = getRange(St, Sym).Intersect(getBasicVals(), F, AdjInt, AdjInt); 488 return New.isEmpty() ? nullptr : St->set<ConstraintRange>(Sym, New); 489 } 490 491 RangeSet RangeConstraintManager::getSymLTRange(ProgramStateRef St, 492 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, true)) { 498 case APSIntType::RTR_Below: 499 return F.getEmptySet(); 500 case APSIntType::RTR_Within: 501 break; 502 case APSIntType::RTR_Above: 503 return getRange(St, Sym); 504 } 505 506 // Special case for Int == Min. This is always false. 507 llvm::APSInt ComparisonVal = AdjustmentType.convert(Int); 508 llvm::APSInt Min = AdjustmentType.getMinValue(); 509 if (ComparisonVal == Min) 510 return F.getEmptySet(); 511 512 llvm::APSInt Lower = Min - Adjustment; 513 llvm::APSInt Upper = ComparisonVal - Adjustment; 514 --Upper; 515 516 return getRange(St, Sym).Intersect(getBasicVals(), F, Lower, Upper); 517 } 518 519 ProgramStateRef 520 RangeConstraintManager::assumeSymLT(ProgramStateRef St, SymbolRef Sym, 521 const llvm::APSInt &Int, 522 const llvm::APSInt &Adjustment) { 523 RangeSet New = getSymLTRange(St, Sym, Int, Adjustment); 524 return New.isEmpty() ? nullptr : St->set<ConstraintRange>(Sym, New); 525 } 526 527 RangeSet RangeConstraintManager::getSymGTRange(ProgramStateRef St, 528 SymbolRef Sym, 529 const llvm::APSInt &Int, 530 const llvm::APSInt &Adjustment) { 531 // Before we do any real work, see if the value can even show up. 532 APSIntType AdjustmentType(Adjustment); 533 switch (AdjustmentType.testInRange(Int, true)) { 534 case APSIntType::RTR_Below: 535 return getRange(St, Sym); 536 case APSIntType::RTR_Within: 537 break; 538 case APSIntType::RTR_Above: 539 return F.getEmptySet(); 540 } 541 542 // Special case for Int == Max. This is always false. 543 llvm::APSInt ComparisonVal = AdjustmentType.convert(Int); 544 llvm::APSInt Max = AdjustmentType.getMaxValue(); 545 if (ComparisonVal == Max) 546 return F.getEmptySet(); 547 548 llvm::APSInt Lower = ComparisonVal - Adjustment; 549 llvm::APSInt Upper = Max - Adjustment; 550 ++Lower; 551 552 return getRange(St, Sym).Intersect(getBasicVals(), F, Lower, Upper); 553 } 554 555 ProgramStateRef 556 RangeConstraintManager::assumeSymGT(ProgramStateRef St, SymbolRef Sym, 557 const llvm::APSInt &Int, 558 const llvm::APSInt &Adjustment) { 559 RangeSet New = getSymGTRange(St, Sym, Int, Adjustment); 560 return New.isEmpty() ? nullptr : St->set<ConstraintRange>(Sym, New); 561 } 562 563 RangeSet RangeConstraintManager::getSymGERange(ProgramStateRef St, 564 SymbolRef Sym, 565 const llvm::APSInt &Int, 566 const llvm::APSInt &Adjustment) { 567 // Before we do any real work, see if the value can even show up. 568 APSIntType AdjustmentType(Adjustment); 569 switch (AdjustmentType.testInRange(Int, true)) { 570 case APSIntType::RTR_Below: 571 return getRange(St, Sym); 572 case APSIntType::RTR_Within: 573 break; 574 case APSIntType::RTR_Above: 575 return F.getEmptySet(); 576 } 577 578 // Special case for Int == Min. This is always feasible. 579 llvm::APSInt ComparisonVal = AdjustmentType.convert(Int); 580 llvm::APSInt Min = AdjustmentType.getMinValue(); 581 if (ComparisonVal == Min) 582 return getRange(St, Sym); 583 584 llvm::APSInt Max = AdjustmentType.getMaxValue(); 585 llvm::APSInt Lower = ComparisonVal - Adjustment; 586 llvm::APSInt Upper = Max - Adjustment; 587 588 return getRange(St, Sym).Intersect(getBasicVals(), F, Lower, Upper); 589 } 590 591 ProgramStateRef 592 RangeConstraintManager::assumeSymGE(ProgramStateRef St, SymbolRef Sym, 593 const llvm::APSInt &Int, 594 const llvm::APSInt &Adjustment) { 595 RangeSet New = getSymGERange(St, Sym, Int, Adjustment); 596 return New.isEmpty() ? nullptr : St->set<ConstraintRange>(Sym, New); 597 } 598 599 RangeSet RangeConstraintManager::getSymLERange( 600 llvm::function_ref<RangeSet()> RS, 601 const llvm::APSInt &Int, 602 const llvm::APSInt &Adjustment) { 603 // Before we do any real work, see if the value can even show up. 604 APSIntType AdjustmentType(Adjustment); 605 switch (AdjustmentType.testInRange(Int, true)) { 606 case APSIntType::RTR_Below: 607 return F.getEmptySet(); 608 case APSIntType::RTR_Within: 609 break; 610 case APSIntType::RTR_Above: 611 return RS(); 612 } 613 614 // Special case for Int == Max. This is always feasible. 615 llvm::APSInt ComparisonVal = AdjustmentType.convert(Int); 616 llvm::APSInt Max = AdjustmentType.getMaxValue(); 617 if (ComparisonVal == Max) 618 return RS(); 619 620 llvm::APSInt Min = AdjustmentType.getMinValue(); 621 llvm::APSInt Lower = Min - Adjustment; 622 llvm::APSInt Upper = ComparisonVal - Adjustment; 623 624 return RS().Intersect(getBasicVals(), F, Lower, Upper); 625 } 626 627 RangeSet RangeConstraintManager::getSymLERange(ProgramStateRef St, 628 SymbolRef Sym, 629 const llvm::APSInt &Int, 630 const llvm::APSInt &Adjustment) { 631 return getSymLERange([&] { return getRange(St, Sym); }, Int, Adjustment); 632 } 633 634 ProgramStateRef 635 RangeConstraintManager::assumeSymLE(ProgramStateRef St, SymbolRef Sym, 636 const llvm::APSInt &Int, 637 const llvm::APSInt &Adjustment) { 638 RangeSet New = getSymLERange(St, Sym, Int, Adjustment); 639 return New.isEmpty() ? nullptr : St->set<ConstraintRange>(Sym, New); 640 } 641 642 ProgramStateRef RangeConstraintManager::assumeSymWithinInclusiveRange( 643 ProgramStateRef State, SymbolRef Sym, const llvm::APSInt &From, 644 const llvm::APSInt &To, const llvm::APSInt &Adjustment) { 645 RangeSet New = getSymGERange(State, Sym, From, Adjustment); 646 if (New.isEmpty()) 647 return nullptr; 648 RangeSet Out = getSymLERange([&] { return New; }, To, Adjustment); 649 return Out.isEmpty() ? nullptr : State->set<ConstraintRange>(Sym, Out); 650 } 651 652 ProgramStateRef RangeConstraintManager::assumeSymOutsideInclusiveRange( 653 ProgramStateRef State, SymbolRef Sym, const llvm::APSInt &From, 654 const llvm::APSInt &To, const llvm::APSInt &Adjustment) { 655 RangeSet RangeLT = getSymLTRange(State, Sym, From, Adjustment); 656 RangeSet RangeGT = getSymGTRange(State, Sym, To, Adjustment); 657 RangeSet New(RangeLT.addRange(F, RangeGT)); 658 return New.isEmpty() ? nullptr : State->set<ConstraintRange>(Sym, New); 659 } 660 661 //===------------------------------------------------------------------------=== 662 // Pretty-printing. 663 //===------------------------------------------------------------------------===/ 664 665 void RangeConstraintManager::print(ProgramStateRef St, raw_ostream &Out, 666 const char *nl, const char *sep) { 667 668 ConstraintRangeTy Ranges = St->get<ConstraintRange>(); 669 670 if (Ranges.isEmpty()) { 671 Out << nl << sep << "Ranges are empty." << nl; 672 return; 673 } 674 675 Out << nl << sep << "Ranges of symbol values:"; 676 for (ConstraintRangeTy::iterator I = Ranges.begin(), E = Ranges.end(); I != E; 677 ++I) { 678 Out << nl << ' ' << I.getKey() << " : "; 679 I.getData().print(Out); 680 } 681 Out << nl; 682 } 683