SimpleConstraintManager.cpp 9.5 KB

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