SimpleConstraintManager.cpp 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. //== SimpleConstraintManager.cpp --------------------------------*- 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 SimpleConstraintManager, a class that holds code shared
  11. // between BasicConstraintManager and RangeConstraintManager.
  12. //
  13. //===----------------------------------------------------------------------===//
  14. #include "SimpleConstraintManager.h"
  15. #include "clang/StaticAnalyzer/Core/PathSensitive/ExprEngine.h"
  16. #include "clang/StaticAnalyzer/Core/PathSensitive/GRState.h"
  17. namespace clang {
  18. namespace ento {
  19. SimpleConstraintManager::~SimpleConstraintManager() {}
  20. bool SimpleConstraintManager::canReasonAbout(SVal X) const {
  21. if (nonloc::SymExprVal *SymVal = dyn_cast<nonloc::SymExprVal>(&X)) {
  22. const SymExpr *SE = SymVal->getSymbolicExpression();
  23. if (isa<SymbolData>(SE))
  24. return true;
  25. if (const SymIntExpr *SIE = dyn_cast<SymIntExpr>(SE)) {
  26. switch (SIE->getOpcode()) {
  27. // We don't reason yet about bitwise-constraints on symbolic values.
  28. case BO_And:
  29. case BO_Or:
  30. case BO_Xor:
  31. return false;
  32. // We don't reason yet about these arithmetic constraints on
  33. // symbolic values.
  34. case BO_Mul:
  35. case BO_Div:
  36. case BO_Rem:
  37. case BO_Shl:
  38. case BO_Shr:
  39. return false;
  40. // All other cases.
  41. default:
  42. return true;
  43. }
  44. }
  45. return false;
  46. }
  47. return true;
  48. }
  49. const GRState *SimpleConstraintManager::assume(const GRState *state,
  50. DefinedSVal Cond,
  51. bool Assumption) {
  52. if (isa<NonLoc>(Cond))
  53. return assume(state, cast<NonLoc>(Cond), Assumption);
  54. else
  55. return assume(state, cast<Loc>(Cond), Assumption);
  56. }
  57. const GRState *SimpleConstraintManager::assume(const GRState *state, Loc cond,
  58. bool assumption) {
  59. state = assumeAux(state, cond, assumption);
  60. return SU.processAssume(state, cond, assumption);
  61. }
  62. const GRState *SimpleConstraintManager::assumeAux(const GRState *state,
  63. Loc Cond, bool Assumption) {
  64. BasicValueFactory &BasicVals = state->getBasicVals();
  65. switch (Cond.getSubKind()) {
  66. default:
  67. assert (false && "'Assume' not implemented for this Loc.");
  68. return state;
  69. case loc::MemRegionKind: {
  70. // FIXME: Should this go into the storemanager?
  71. const MemRegion *R = cast<loc::MemRegionVal>(Cond).getRegion();
  72. const SubRegion *SubR = dyn_cast<SubRegion>(R);
  73. while (SubR) {
  74. // FIXME: now we only find the first symbolic region.
  75. if (const SymbolicRegion *SymR = dyn_cast<SymbolicRegion>(SubR)) {
  76. const llvm::APSInt &zero = BasicVals.getZeroWithPtrWidth();
  77. if (Assumption)
  78. return assumeSymNE(state, SymR->getSymbol(), zero, zero);
  79. else
  80. return assumeSymEQ(state, SymR->getSymbol(), zero, zero);
  81. }
  82. SubR = dyn_cast<SubRegion>(SubR->getSuperRegion());
  83. }
  84. // FALL-THROUGH.
  85. }
  86. case loc::GotoLabelKind:
  87. return Assumption ? state : NULL;
  88. case loc::ConcreteIntKind: {
  89. bool b = cast<loc::ConcreteInt>(Cond).getValue() != 0;
  90. bool isFeasible = b ? Assumption : !Assumption;
  91. return isFeasible ? state : NULL;
  92. }
  93. } // end switch
  94. }
  95. const GRState *SimpleConstraintManager::assume(const GRState *state,
  96. NonLoc cond,
  97. bool assumption) {
  98. state = assumeAux(state, cond, assumption);
  99. return SU.processAssume(state, cond, assumption);
  100. }
  101. static BinaryOperator::Opcode NegateComparison(BinaryOperator::Opcode op) {
  102. // FIXME: This should probably be part of BinaryOperator, since this isn't
  103. // the only place it's used. (This code was copied from SimpleSValBuilder.cpp.)
  104. switch (op) {
  105. default:
  106. assert(false && "Invalid opcode.");
  107. case BO_LT: return BO_GE;
  108. case BO_GT: return BO_LE;
  109. case BO_LE: return BO_GT;
  110. case BO_GE: return BO_LT;
  111. case BO_EQ: return BO_NE;
  112. case BO_NE: return BO_EQ;
  113. }
  114. }
  115. const GRState *SimpleConstraintManager::assumeAux(const GRState *state,
  116. NonLoc Cond,
  117. bool Assumption) {
  118. // We cannot reason about SymSymExprs,
  119. // and can only reason about some SymIntExprs.
  120. if (!canReasonAbout(Cond)) {
  121. // Just return the current state indicating that the path is feasible.
  122. // This may be an over-approximation of what is possible.
  123. return state;
  124. }
  125. BasicValueFactory &BasicVals = state->getBasicVals();
  126. SymbolManager &SymMgr = state->getSymbolManager();
  127. switch (Cond.getSubKind()) {
  128. default:
  129. assert(false && "'Assume' not implemented for this NonLoc");
  130. case nonloc::SymbolValKind: {
  131. nonloc::SymbolVal& SV = cast<nonloc::SymbolVal>(Cond);
  132. SymbolRef sym = SV.getSymbol();
  133. QualType T = SymMgr.getType(sym);
  134. const llvm::APSInt &zero = BasicVals.getValue(0, T);
  135. if (Assumption)
  136. return assumeSymNE(state, sym, zero, zero);
  137. else
  138. return assumeSymEQ(state, sym, zero, zero);
  139. }
  140. case nonloc::SymExprValKind: {
  141. nonloc::SymExprVal V = cast<nonloc::SymExprVal>(Cond);
  142. // For now, we only handle expressions whose RHS is an integer.
  143. // All other expressions are assumed to be feasible.
  144. const SymIntExpr *SE = dyn_cast<SymIntExpr>(V.getSymbolicExpression());
  145. if (!SE)
  146. return state;
  147. BinaryOperator::Opcode op = SE->getOpcode();
  148. // Implicitly compare non-comparison expressions to 0.
  149. if (!BinaryOperator::isComparisonOp(op)) {
  150. QualType T = SymMgr.getType(SE);
  151. const llvm::APSInt &zero = BasicVals.getValue(0, T);
  152. op = (Assumption ? BO_NE : BO_EQ);
  153. return assumeSymRel(state, SE, op, zero);
  154. }
  155. // From here on out, op is the real comparison we'll be testing.
  156. if (!Assumption)
  157. op = NegateComparison(op);
  158. return assumeSymRel(state, SE->getLHS(), op, SE->getRHS());
  159. }
  160. case nonloc::ConcreteIntKind: {
  161. bool b = cast<nonloc::ConcreteInt>(Cond).getValue() != 0;
  162. bool isFeasible = b ? Assumption : !Assumption;
  163. return isFeasible ? state : NULL;
  164. }
  165. case nonloc::LocAsIntegerKind:
  166. return assumeAux(state, cast<nonloc::LocAsInteger>(Cond).getLoc(),
  167. Assumption);
  168. } // end switch
  169. }
  170. const GRState *SimpleConstraintManager::assumeSymRel(const GRState *state,
  171. const SymExpr *LHS,
  172. BinaryOperator::Opcode op,
  173. const llvm::APSInt& Int) {
  174. assert(BinaryOperator::isComparisonOp(op) &&
  175. "Non-comparison ops should be rewritten as comparisons to zero.");
  176. // We only handle simple comparisons of the form "$sym == constant"
  177. // or "($sym+constant1) == constant2".
  178. // The adjustment is "constant1" in the above expression. It's used to
  179. // "slide" the solution range around for modular arithmetic. For example,
  180. // x < 4 has the solution [0, 3]. x+2 < 4 has the solution [0-2, 3-2], which
  181. // in modular arithmetic is [0, 1] U [UINT_MAX-1, UINT_MAX]. It's up to
  182. // the subclasses of SimpleConstraintManager to handle the adjustment.
  183. llvm::APSInt Adjustment;
  184. // First check if the LHS is a simple symbol reference.
  185. SymbolRef Sym = dyn_cast<SymbolData>(LHS);
  186. if (Sym) {
  187. Adjustment = 0;
  188. } else {
  189. // Next, see if it's a "($sym+constant1)" expression.
  190. const SymIntExpr *SE = dyn_cast<SymIntExpr>(LHS);
  191. // We don't handle "($sym1+$sym2)".
  192. // Give up and assume the constraint is feasible.
  193. if (!SE)
  194. return state;
  195. // We don't handle "(<expr>+constant1)".
  196. // Give up and assume the constraint is feasible.
  197. Sym = dyn_cast<SymbolData>(SE->getLHS());
  198. if (!Sym)
  199. return state;
  200. // Get the constant out of the expression "($sym+constant1)".
  201. switch (SE->getOpcode()) {
  202. case BO_Add:
  203. Adjustment = SE->getRHS();
  204. break;
  205. case BO_Sub:
  206. Adjustment = -SE->getRHS();
  207. break;
  208. default:
  209. // We don't handle non-additive operators.
  210. // Give up and assume the constraint is feasible.
  211. return state;
  212. }
  213. }
  214. // FIXME: This next section is a hack. It silently converts the integers to
  215. // be of the same type as the symbol, which is not always correct. Really the
  216. // comparisons should be performed using the Int's type, then mapped back to
  217. // the symbol's range of values.
  218. GRStateManager &StateMgr = state->getStateManager();
  219. ASTContext &Ctx = StateMgr.getContext();
  220. QualType T = Sym->getType(Ctx);
  221. assert(T->isIntegerType() || Loc::isLocType(T));
  222. unsigned bitwidth = Ctx.getTypeSize(T);
  223. bool isSymUnsigned = T->isUnsignedIntegerType() || Loc::isLocType(T);
  224. // Convert the adjustment.
  225. Adjustment.setIsUnsigned(isSymUnsigned);
  226. Adjustment = Adjustment.extOrTrunc(bitwidth);
  227. // Convert the right-hand side integer.
  228. llvm::APSInt ConvertedInt(Int, isSymUnsigned);
  229. ConvertedInt = ConvertedInt.extOrTrunc(bitwidth);
  230. switch (op) {
  231. default:
  232. // No logic yet for other operators. assume the constraint is feasible.
  233. return state;
  234. case BO_EQ:
  235. return assumeSymEQ(state, Sym, ConvertedInt, Adjustment);
  236. case BO_NE:
  237. return assumeSymNE(state, Sym, ConvertedInt, Adjustment);
  238. case BO_GT:
  239. return assumeSymGT(state, Sym, ConvertedInt, Adjustment);
  240. case BO_GE:
  241. return assumeSymGE(state, Sym, ConvertedInt, Adjustment);
  242. case BO_LT:
  243. return assumeSymLT(state, Sym, ConvertedInt, Adjustment);
  244. case BO_LE:
  245. return assumeSymLE(state, Sym, ConvertedInt, Adjustment);
  246. } // end switch
  247. }
  248. } // end of namespace ento
  249. } // end of namespace clang