TestAfterDivZeroChecker.cpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. //== TestAfterDivZeroChecker.cpp - Test after division by zero checker --*--==//
  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 defines TestAfterDivZeroChecker, a builtin check that performs checks
  11. // for division by zero where the division occurs before comparison with zero.
  12. //
  13. //===----------------------------------------------------------------------===//
  14. #include "ClangSACheckers.h"
  15. #include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
  16. #include "clang/StaticAnalyzer/Core/Checker.h"
  17. #include "clang/StaticAnalyzer/Core/PathSensitive/CallEvent.h"
  18. #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
  19. #include "llvm/ADT/FoldingSet.h"
  20. using namespace clang;
  21. using namespace ento;
  22. namespace {
  23. class ZeroState {
  24. private:
  25. SymbolRef ZeroSymbol;
  26. unsigned BlockID;
  27. const StackFrameContext *SFC;
  28. public:
  29. ZeroState(SymbolRef S, unsigned B, const StackFrameContext *SFC)
  30. : ZeroSymbol(S), BlockID(B), SFC(SFC) {}
  31. const StackFrameContext *getStackFrameContext() const { return SFC; }
  32. bool operator==(const ZeroState &X) const {
  33. return BlockID == X.BlockID && SFC == X.SFC && ZeroSymbol == X.ZeroSymbol;
  34. }
  35. bool operator<(const ZeroState &X) const {
  36. if (BlockID != X.BlockID)
  37. return BlockID < X.BlockID;
  38. if (SFC != X.SFC)
  39. return SFC < X.SFC;
  40. return ZeroSymbol < X.ZeroSymbol;
  41. }
  42. void Profile(llvm::FoldingSetNodeID &ID) const {
  43. ID.AddInteger(BlockID);
  44. ID.AddPointer(SFC);
  45. ID.AddPointer(ZeroSymbol);
  46. }
  47. };
  48. class DivisionBRVisitor : public BugReporterVisitorImpl<DivisionBRVisitor> {
  49. private:
  50. SymbolRef ZeroSymbol;
  51. const StackFrameContext *SFC;
  52. bool Satisfied;
  53. public:
  54. DivisionBRVisitor(SymbolRef ZeroSymbol, const StackFrameContext *SFC)
  55. : ZeroSymbol(ZeroSymbol), SFC(SFC), Satisfied(false) {}
  56. void Profile(llvm::FoldingSetNodeID &ID) const override {
  57. ID.Add(ZeroSymbol);
  58. ID.Add(SFC);
  59. }
  60. std::shared_ptr<PathDiagnosticPiece> VisitNode(const ExplodedNode *Succ,
  61. const ExplodedNode *Pred,
  62. BugReporterContext &BRC,
  63. BugReport &BR) override;
  64. };
  65. class TestAfterDivZeroChecker
  66. : public Checker<check::PreStmt<BinaryOperator>, check::BranchCondition,
  67. check::EndFunction> {
  68. mutable std::unique_ptr<BuiltinBug> DivZeroBug;
  69. void reportBug(SVal Val, CheckerContext &C) const;
  70. public:
  71. void checkPreStmt(const BinaryOperator *B, CheckerContext &C) const;
  72. void checkBranchCondition(const Stmt *Condition, CheckerContext &C) const;
  73. void checkEndFunction(CheckerContext &C) const;
  74. void setDivZeroMap(SVal Var, CheckerContext &C) const;
  75. bool hasDivZeroMap(SVal Var, const CheckerContext &C) const;
  76. bool isZero(SVal S, CheckerContext &C) const;
  77. };
  78. } // end anonymous namespace
  79. REGISTER_SET_WITH_PROGRAMSTATE(DivZeroMap, ZeroState)
  80. std::shared_ptr<PathDiagnosticPiece>
  81. DivisionBRVisitor::VisitNode(const ExplodedNode *Succ, const ExplodedNode *Pred,
  82. BugReporterContext &BRC, BugReport &BR) {
  83. if (Satisfied)
  84. return nullptr;
  85. const Expr *E = nullptr;
  86. if (Optional<PostStmt> P = Succ->getLocationAs<PostStmt>())
  87. if (const BinaryOperator *BO = P->getStmtAs<BinaryOperator>()) {
  88. BinaryOperator::Opcode Op = BO->getOpcode();
  89. if (Op == BO_Div || Op == BO_Rem || Op == BO_DivAssign ||
  90. Op == BO_RemAssign) {
  91. E = BO->getRHS();
  92. }
  93. }
  94. if (!E)
  95. return nullptr;
  96. SVal S = Succ->getSVal(E);
  97. if (ZeroSymbol == S.getAsSymbol() && SFC == Succ->getStackFrame()) {
  98. Satisfied = true;
  99. // Construct a new PathDiagnosticPiece.
  100. ProgramPoint P = Succ->getLocation();
  101. PathDiagnosticLocation L =
  102. PathDiagnosticLocation::create(P, BRC.getSourceManager());
  103. if (!L.isValid() || !L.asLocation().isValid())
  104. return nullptr;
  105. return std::make_shared<PathDiagnosticEventPiece>(
  106. L, "Division with compared value made here");
  107. }
  108. return nullptr;
  109. }
  110. bool TestAfterDivZeroChecker::isZero(SVal S, CheckerContext &C) const {
  111. Optional<DefinedSVal> DSV = S.getAs<DefinedSVal>();
  112. if (!DSV)
  113. return false;
  114. ConstraintManager &CM = C.getConstraintManager();
  115. return !CM.assume(C.getState(), *DSV, true);
  116. }
  117. void TestAfterDivZeroChecker::setDivZeroMap(SVal Var, CheckerContext &C) const {
  118. SymbolRef SR = Var.getAsSymbol();
  119. if (!SR)
  120. return;
  121. ProgramStateRef State = C.getState();
  122. State =
  123. State->add<DivZeroMap>(ZeroState(SR, C.getBlockID(), C.getStackFrame()));
  124. C.addTransition(State);
  125. }
  126. bool TestAfterDivZeroChecker::hasDivZeroMap(SVal Var,
  127. const CheckerContext &C) const {
  128. SymbolRef SR = Var.getAsSymbol();
  129. if (!SR)
  130. return false;
  131. ZeroState ZS(SR, C.getBlockID(), C.getStackFrame());
  132. return C.getState()->contains<DivZeroMap>(ZS);
  133. }
  134. void TestAfterDivZeroChecker::reportBug(SVal Val, CheckerContext &C) const {
  135. if (ExplodedNode *N = C.generateErrorNode(C.getState())) {
  136. if (!DivZeroBug)
  137. DivZeroBug.reset(new BuiltinBug(this, "Division by zero"));
  138. auto R = llvm::make_unique<BugReport>(
  139. *DivZeroBug, "Value being compared against zero has already been used "
  140. "for division",
  141. N);
  142. R->addVisitor(llvm::make_unique<DivisionBRVisitor>(Val.getAsSymbol(),
  143. C.getStackFrame()));
  144. C.emitReport(std::move(R));
  145. }
  146. }
  147. void TestAfterDivZeroChecker::checkEndFunction(CheckerContext &C) const {
  148. ProgramStateRef State = C.getState();
  149. DivZeroMapTy DivZeroes = State->get<DivZeroMap>();
  150. if (DivZeroes.isEmpty())
  151. return;
  152. DivZeroMapTy::Factory &F = State->get_context<DivZeroMap>();
  153. for (llvm::ImmutableSet<ZeroState>::iterator I = DivZeroes.begin(),
  154. E = DivZeroes.end();
  155. I != E; ++I) {
  156. ZeroState ZS = *I;
  157. if (ZS.getStackFrameContext() == C.getStackFrame())
  158. DivZeroes = F.remove(DivZeroes, ZS);
  159. }
  160. C.addTransition(State->set<DivZeroMap>(DivZeroes));
  161. }
  162. void TestAfterDivZeroChecker::checkPreStmt(const BinaryOperator *B,
  163. CheckerContext &C) const {
  164. BinaryOperator::Opcode Op = B->getOpcode();
  165. if (Op == BO_Div || Op == BO_Rem || Op == BO_DivAssign ||
  166. Op == BO_RemAssign) {
  167. SVal S = C.getSVal(B->getRHS());
  168. if (!isZero(S, C))
  169. setDivZeroMap(S, C);
  170. }
  171. }
  172. void TestAfterDivZeroChecker::checkBranchCondition(const Stmt *Condition,
  173. CheckerContext &C) const {
  174. if (const BinaryOperator *B = dyn_cast<BinaryOperator>(Condition)) {
  175. if (B->isComparisonOp()) {
  176. const IntegerLiteral *IntLiteral = dyn_cast<IntegerLiteral>(B->getRHS());
  177. bool LRHS = true;
  178. if (!IntLiteral) {
  179. IntLiteral = dyn_cast<IntegerLiteral>(B->getLHS());
  180. LRHS = false;
  181. }
  182. if (!IntLiteral || IntLiteral->getValue() != 0)
  183. return;
  184. SVal Val = C.getSVal(LRHS ? B->getLHS() : B->getRHS());
  185. if (hasDivZeroMap(Val, C))
  186. reportBug(Val, C);
  187. }
  188. } else if (const UnaryOperator *U = dyn_cast<UnaryOperator>(Condition)) {
  189. if (U->getOpcode() == UO_LNot) {
  190. SVal Val;
  191. if (const ImplicitCastExpr *I =
  192. dyn_cast<ImplicitCastExpr>(U->getSubExpr()))
  193. Val = C.getSVal(I->getSubExpr());
  194. if (hasDivZeroMap(Val, C))
  195. reportBug(Val, C);
  196. else {
  197. Val = C.getSVal(U->getSubExpr());
  198. if (hasDivZeroMap(Val, C))
  199. reportBug(Val, C);
  200. }
  201. }
  202. } else if (const ImplicitCastExpr *IE =
  203. dyn_cast<ImplicitCastExpr>(Condition)) {
  204. SVal Val = C.getSVal(IE->getSubExpr());
  205. if (hasDivZeroMap(Val, C))
  206. reportBug(Val, C);
  207. else {
  208. SVal Val = C.getSVal(Condition);
  209. if (hasDivZeroMap(Val, C))
  210. reportBug(Val, C);
  211. }
  212. }
  213. }
  214. void ento::registerTestAfterDivZeroChecker(CheckerManager &mgr) {
  215. mgr.registerChecker<TestAfterDivZeroChecker>();
  216. }