SimpleConstraintManager.cpp 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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/APSIntType.h"
  16. #include "clang/StaticAnalyzer/Core/PathSensitive/ExprEngine.h"
  17. #include "clang/StaticAnalyzer/Core/PathSensitive/ProgramState.h"
  18. namespace clang {
  19. namespace ento {
  20. SimpleConstraintManager::~SimpleConstraintManager() {}
  21. bool SimpleConstraintManager::canReasonAbout(SVal X) const {
  22. nonloc::SymbolVal *SymVal = dyn_cast<nonloc::SymbolVal>(&X);
  23. if (SymVal && SymVal->isExpression()) {
  24. const SymExpr *SE = SymVal->getSymbol();
  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. ProgramStateRef SimpleConstraintManager::assume(ProgramStateRef 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. ProgramStateRef SimpleConstraintManager::assume(ProgramStateRef state, Loc cond,
  58. bool assumption) {
  59. state = assumeAux(state, cond, assumption);
  60. if (NotifyAssumeClients)
  61. return SU.processAssume(state, cond, assumption);
  62. return state;
  63. }
  64. ProgramStateRef SimpleConstraintManager::assumeAux(ProgramStateRef state,
  65. Loc Cond, bool Assumption) {
  66. switch (Cond.getSubKind()) {
  67. default:
  68. assert (false && "'Assume' not implemented for this Loc.");
  69. return state;
  70. case loc::MemRegionKind: {
  71. // FIXME: Should this go into the storemanager?
  72. const MemRegion *R = cast<loc::MemRegionVal>(Cond).getRegion();
  73. const SubRegion *SubR = dyn_cast<SubRegion>(R);
  74. while (SubR) {
  75. // FIXME: now we only find the first symbolic region.
  76. if (const SymbolicRegion *SymR = dyn_cast<SymbolicRegion>(SubR)) {
  77. const llvm::APSInt &zero = getBasicVals().getZeroWithPtrWidth();
  78. if (Assumption)
  79. return assumeSymNE(state, SymR->getSymbol(), zero, zero);
  80. else
  81. return assumeSymEQ(state, SymR->getSymbol(), zero, zero);
  82. }
  83. SubR = dyn_cast<SubRegion>(SubR->getSuperRegion());
  84. }
  85. // FALL-THROUGH.
  86. }
  87. case loc::GotoLabelKind:
  88. return Assumption ? state : NULL;
  89. case loc::ConcreteIntKind: {
  90. bool b = cast<loc::ConcreteInt>(Cond).getValue() != 0;
  91. bool isFeasible = b ? Assumption : !Assumption;
  92. return isFeasible ? state : NULL;
  93. }
  94. } // end switch
  95. }
  96. ProgramStateRef SimpleConstraintManager::assume(ProgramStateRef state,
  97. NonLoc cond,
  98. bool assumption) {
  99. state = assumeAux(state, cond, assumption);
  100. if (NotifyAssumeClients)
  101. return SU.processAssume(state, cond, assumption);
  102. return state;
  103. }
  104. static BinaryOperator::Opcode NegateComparison(BinaryOperator::Opcode op) {
  105. // FIXME: This should probably be part of BinaryOperator, since this isn't
  106. // the only place it's used. (This code was copied from SimpleSValBuilder.cpp.)
  107. switch (op) {
  108. default:
  109. llvm_unreachable("Invalid opcode.");
  110. case BO_LT: return BO_GE;
  111. case BO_GT: return BO_LE;
  112. case BO_LE: return BO_GT;
  113. case BO_GE: return BO_LT;
  114. case BO_EQ: return BO_NE;
  115. case BO_NE: return BO_EQ;
  116. }
  117. }
  118. ProgramStateRef
  119. SimpleConstraintManager::assumeAuxForSymbol(ProgramStateRef State,
  120. SymbolRef Sym, bool Assumption) {
  121. BasicValueFactory &BVF = getBasicVals();
  122. QualType T = Sym->getType();
  123. // None of the constraint solvers currently support non-integer types.
  124. if (!T->isIntegerType())
  125. return State;
  126. const llvm::APSInt &zero = BVF.getValue(0, T);
  127. if (Assumption)
  128. return assumeSymNE(State, Sym, zero, zero);
  129. else
  130. return assumeSymEQ(State, Sym, zero, zero);
  131. }
  132. ProgramStateRef SimpleConstraintManager::assumeAux(ProgramStateRef state,
  133. NonLoc Cond,
  134. bool Assumption) {
  135. // We cannot reason about SymSymExprs, and can only reason about some
  136. // SymIntExprs.
  137. if (!canReasonAbout(Cond)) {
  138. // Just add the constraint to the expression without trying to simplify.
  139. SymbolRef sym = Cond.getAsSymExpr();
  140. return assumeAuxForSymbol(state, sym, Assumption);
  141. }
  142. BasicValueFactory &BasicVals = getBasicVals();
  143. switch (Cond.getSubKind()) {
  144. default:
  145. llvm_unreachable("'Assume' not implemented for this NonLoc");
  146. case nonloc::SymbolValKind: {
  147. nonloc::SymbolVal& SV = cast<nonloc::SymbolVal>(Cond);
  148. SymbolRef sym = SV.getSymbol();
  149. assert(sym);
  150. // Handle SymbolData.
  151. if (!SV.isExpression()) {
  152. return assumeAuxForSymbol(state, sym, Assumption);
  153. // Handle symbolic expression.
  154. } else {
  155. // We can only simplify expressions whose RHS is an integer.
  156. const SymIntExpr *SE = dyn_cast<SymIntExpr>(sym);
  157. if (!SE)
  158. return assumeAuxForSymbol(state, sym, Assumption);
  159. BinaryOperator::Opcode op = SE->getOpcode();
  160. // Implicitly compare non-comparison expressions to 0.
  161. if (!BinaryOperator::isComparisonOp(op)) {
  162. QualType T = SE->getType();
  163. const llvm::APSInt &zero = BasicVals.getValue(0, T);
  164. op = (Assumption ? BO_NE : BO_EQ);
  165. return assumeSymRel(state, SE, op, zero);
  166. }
  167. // From here on out, op is the real comparison we'll be testing.
  168. if (!Assumption)
  169. op = NegateComparison(op);
  170. return assumeSymRel(state, SE->getLHS(), op, SE->getRHS());
  171. }
  172. }
  173. case nonloc::ConcreteIntKind: {
  174. bool b = cast<nonloc::ConcreteInt>(Cond).getValue() != 0;
  175. bool isFeasible = b ? Assumption : !Assumption;
  176. return isFeasible ? state : NULL;
  177. }
  178. case nonloc::LocAsIntegerKind:
  179. return assumeAux(state, cast<nonloc::LocAsInteger>(Cond).getLoc(),
  180. Assumption);
  181. } // end switch
  182. }
  183. static void computeAdjustment(SymbolRef &Sym, llvm::APSInt &Adjustment) {
  184. // Is it a "($sym+constant1)" expression?
  185. if (const SymIntExpr *SE = dyn_cast<SymIntExpr>(Sym)) {
  186. BinaryOperator::Opcode Op = SE->getOpcode();
  187. if (Op == BO_Add || Op == BO_Sub) {
  188. Sym = SE->getLHS();
  189. Adjustment = APSIntType(Adjustment).convert(SE->getRHS());
  190. // Don't forget to negate the adjustment if it's being subtracted.
  191. // This should happen /after/ promotion, in case the value being
  192. // subtracted is, say, CHAR_MIN, and the promoted type is 'int'.
  193. if (Op == BO_Sub)
  194. Adjustment = -Adjustment;
  195. }
  196. }
  197. }
  198. ProgramStateRef SimpleConstraintManager::assumeSymRel(ProgramStateRef state,
  199. const SymExpr *LHS,
  200. BinaryOperator::Opcode op,
  201. const llvm::APSInt& Int) {
  202. assert(BinaryOperator::isComparisonOp(op) &&
  203. "Non-comparison ops should be rewritten as comparisons to zero.");
  204. // Get the type used for calculating wraparound.
  205. BasicValueFactory &BVF = getBasicVals();
  206. APSIntType WraparoundType = BVF.getAPSIntType(LHS->getType());
  207. // We only handle simple comparisons of the form "$sym == constant"
  208. // or "($sym+constant1) == constant2".
  209. // The adjustment is "constant1" in the above expression. It's used to
  210. // "slide" the solution range around for modular arithmetic. For example,
  211. // x < 4 has the solution [0, 3]. x+2 < 4 has the solution [0-2, 3-2], which
  212. // in modular arithmetic is [0, 1] U [UINT_MAX-1, UINT_MAX]. It's up to
  213. // the subclasses of SimpleConstraintManager to handle the adjustment.
  214. SymbolRef Sym = LHS;
  215. llvm::APSInt Adjustment = WraparoundType.getZeroValue();
  216. computeAdjustment(Sym, Adjustment);
  217. // Convert the right-hand side integer as necessary.
  218. APSIntType ComparisonType = std::max(WraparoundType, APSIntType(Int));
  219. llvm::APSInt ConvertedInt = ComparisonType.convert(Int);
  220. switch (op) {
  221. default:
  222. // No logic yet for other operators. assume the constraint is feasible.
  223. return state;
  224. case BO_EQ:
  225. return assumeSymEQ(state, Sym, ConvertedInt, Adjustment);
  226. case BO_NE:
  227. return assumeSymNE(state, Sym, ConvertedInt, Adjustment);
  228. case BO_GT:
  229. return assumeSymGT(state, Sym, ConvertedInt, Adjustment);
  230. case BO_GE:
  231. return assumeSymGE(state, Sym, ConvertedInt, Adjustment);
  232. case BO_LT:
  233. return assumeSymLT(state, Sym, ConvertedInt, Adjustment);
  234. case BO_LE:
  235. return assumeSymLE(state, Sym, ConvertedInt, Adjustment);
  236. } // end switch
  237. }
  238. } // end of namespace ento
  239. } // end of namespace clang