TestAfterDivZeroChecker.cpp 8.2 KB

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