RangedConstraintManager.cpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. //== RangedConstraintManager.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 RangedConstraintManager, a class that provides a
  11. // range-based constraint manager interface.
  12. //
  13. //===----------------------------------------------------------------------===//
  14. #include "RangedConstraintManager.h"
  15. #include "clang/StaticAnalyzer/Core/PathSensitive/ProgramState.h"
  16. namespace clang {
  17. namespace ento {
  18. RangedConstraintManager::~RangedConstraintManager() {}
  19. ProgramStateRef RangedConstraintManager::assumeSym(ProgramStateRef State,
  20. SymbolRef Sym,
  21. bool Assumption) {
  22. // Handle SymbolData.
  23. if (isa<SymbolData>(Sym)) {
  24. return assumeSymUnsupported(State, Sym, Assumption);
  25. // Handle symbolic expression.
  26. } else if (const SymIntExpr *SIE = dyn_cast<SymIntExpr>(Sym)) {
  27. // We can only simplify expressions whose RHS is an integer.
  28. BinaryOperator::Opcode op = SIE->getOpcode();
  29. if (BinaryOperator::isComparisonOp(op) && op != BO_Cmp) {
  30. if (!Assumption)
  31. op = BinaryOperator::negateComparisonOp(op);
  32. return assumeSymRel(State, SIE->getLHS(), op, SIE->getRHS());
  33. }
  34. } else if (const SymSymExpr *SSE = dyn_cast<SymSymExpr>(Sym)) {
  35. // Translate "a != b" to "(b - a) != 0".
  36. // We invert the order of the operands as a heuristic for how loop
  37. // conditions are usually written ("begin != end") as compared to length
  38. // calculations ("end - begin"). The more correct thing to do would be to
  39. // canonicalize "a - b" and "b - a", which would allow us to treat
  40. // "a != b" and "b != a" the same.
  41. SymbolManager &SymMgr = getSymbolManager();
  42. BinaryOperator::Opcode Op = SSE->getOpcode();
  43. assert(BinaryOperator::isComparisonOp(Op));
  44. // For now, we only support comparing pointers.
  45. assert(Loc::isLocType(SSE->getLHS()->getType()));
  46. assert(Loc::isLocType(SSE->getRHS()->getType()));
  47. QualType DiffTy = SymMgr.getContext().getPointerDiffType();
  48. SymbolRef Subtraction =
  49. SymMgr.getSymSymExpr(SSE->getRHS(), BO_Sub, SSE->getLHS(), DiffTy);
  50. const llvm::APSInt &Zero = getBasicVals().getValue(0, DiffTy);
  51. Op = BinaryOperator::reverseComparisonOp(Op);
  52. if (!Assumption)
  53. Op = BinaryOperator::negateComparisonOp(Op);
  54. return assumeSymRel(State, Subtraction, Op, Zero);
  55. }
  56. // If we get here, there's nothing else we can do but treat the symbol as
  57. // opaque.
  58. return assumeSymUnsupported(State, Sym, Assumption);
  59. }
  60. ProgramStateRef RangedConstraintManager::assumeSymInclusiveRange(
  61. ProgramStateRef State, SymbolRef Sym, const llvm::APSInt &From,
  62. const llvm::APSInt &To, bool InRange) {
  63. // Get the type used for calculating wraparound.
  64. BasicValueFactory &BVF = getBasicVals();
  65. APSIntType WraparoundType = BVF.getAPSIntType(Sym->getType());
  66. llvm::APSInt Adjustment = WraparoundType.getZeroValue();
  67. SymbolRef AdjustedSym = Sym;
  68. computeAdjustment(AdjustedSym, Adjustment);
  69. // Convert the right-hand side integer as necessary.
  70. APSIntType ComparisonType = std::max(WraparoundType, APSIntType(From));
  71. llvm::APSInt ConvertedFrom = ComparisonType.convert(From);
  72. llvm::APSInt ConvertedTo = ComparisonType.convert(To);
  73. // Prefer unsigned comparisons.
  74. if (ComparisonType.getBitWidth() == WraparoundType.getBitWidth() &&
  75. ComparisonType.isUnsigned() && !WraparoundType.isUnsigned())
  76. Adjustment.setIsSigned(false);
  77. if (InRange)
  78. return assumeSymWithinInclusiveRange(State, AdjustedSym, ConvertedFrom,
  79. ConvertedTo, Adjustment);
  80. return assumeSymOutsideInclusiveRange(State, AdjustedSym, ConvertedFrom,
  81. ConvertedTo, Adjustment);
  82. }
  83. ProgramStateRef
  84. RangedConstraintManager::assumeSymUnsupported(ProgramStateRef State,
  85. SymbolRef Sym, bool Assumption) {
  86. BasicValueFactory &BVF = getBasicVals();
  87. QualType T = Sym->getType();
  88. // Non-integer types are not supported.
  89. if (!T->isIntegralOrEnumerationType())
  90. return State;
  91. // Reverse the operation and add directly to state.
  92. const llvm::APSInt &Zero = BVF.getValue(0, T);
  93. if (Assumption)
  94. return assumeSymNE(State, Sym, Zero, Zero);
  95. else
  96. return assumeSymEQ(State, Sym, Zero, Zero);
  97. }
  98. ProgramStateRef RangedConstraintManager::assumeSymRel(ProgramStateRef State,
  99. SymbolRef Sym,
  100. BinaryOperator::Opcode Op,
  101. const llvm::APSInt &Int) {
  102. assert(BinaryOperator::isComparisonOp(Op) &&
  103. "Non-comparison ops should be rewritten as comparisons to zero.");
  104. // Simplification: translate an assume of a constraint of the form
  105. // "(exp comparison_op expr) != 0" to true into an assume of
  106. // "exp comparison_op expr" to true. (And similarly, an assume of the form
  107. // "(exp comparison_op expr) == 0" to true into an assume of
  108. // "exp comparison_op expr" to false.)
  109. if (Int == 0 && (Op == BO_EQ || Op == BO_NE)) {
  110. if (const BinarySymExpr *SE = dyn_cast<BinarySymExpr>(Sym))
  111. if (BinaryOperator::isComparisonOp(SE->getOpcode()))
  112. return assumeSym(State, Sym, (Op == BO_NE ? true : false));
  113. }
  114. // Get the type used for calculating wraparound.
  115. BasicValueFactory &BVF = getBasicVals();
  116. APSIntType WraparoundType = BVF.getAPSIntType(Sym->getType());
  117. // We only handle simple comparisons of the form "$sym == constant"
  118. // or "($sym+constant1) == constant2".
  119. // The adjustment is "constant1" in the above expression. It's used to
  120. // "slide" the solution range around for modular arithmetic. For example,
  121. // x < 4 has the solution [0, 3]. x+2 < 4 has the solution [0-2, 3-2], which
  122. // in modular arithmetic is [0, 1] U [UINT_MAX-1, UINT_MAX]. It's up to
  123. // the subclasses of SimpleConstraintManager to handle the adjustment.
  124. llvm::APSInt Adjustment = WraparoundType.getZeroValue();
  125. computeAdjustment(Sym, Adjustment);
  126. // Convert the right-hand side integer as necessary.
  127. APSIntType ComparisonType = std::max(WraparoundType, APSIntType(Int));
  128. llvm::APSInt ConvertedInt = ComparisonType.convert(Int);
  129. // Prefer unsigned comparisons.
  130. if (ComparisonType.getBitWidth() == WraparoundType.getBitWidth() &&
  131. ComparisonType.isUnsigned() && !WraparoundType.isUnsigned())
  132. Adjustment.setIsSigned(false);
  133. switch (Op) {
  134. default:
  135. llvm_unreachable("invalid operation not caught by assertion above");
  136. case BO_EQ:
  137. return assumeSymEQ(State, Sym, ConvertedInt, Adjustment);
  138. case BO_NE:
  139. return assumeSymNE(State, Sym, ConvertedInt, Adjustment);
  140. case BO_GT:
  141. return assumeSymGT(State, Sym, ConvertedInt, Adjustment);
  142. case BO_GE:
  143. return assumeSymGE(State, Sym, ConvertedInt, Adjustment);
  144. case BO_LT:
  145. return assumeSymLT(State, Sym, ConvertedInt, Adjustment);
  146. case BO_LE:
  147. return assumeSymLE(State, Sym, ConvertedInt, Adjustment);
  148. } // end switch
  149. }
  150. void RangedConstraintManager::computeAdjustment(SymbolRef &Sym,
  151. llvm::APSInt &Adjustment) {
  152. // Is it a "($sym+constant1)" expression?
  153. if (const SymIntExpr *SE = dyn_cast<SymIntExpr>(Sym)) {
  154. BinaryOperator::Opcode Op = SE->getOpcode();
  155. if (Op == BO_Add || Op == BO_Sub) {
  156. Sym = SE->getLHS();
  157. Adjustment = APSIntType(Adjustment).convert(SE->getRHS());
  158. // Don't forget to negate the adjustment if it's being subtracted.
  159. // This should happen /after/ promotion, in case the value being
  160. // subtracted is, say, CHAR_MIN, and the promoted type is 'int'.
  161. if (Op == BO_Sub)
  162. Adjustment = -Adjustment;
  163. }
  164. }
  165. }
  166. } // end of namespace ento
  167. } // end of namespace clang