RangeConstraintManager.cpp 28 KB

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