TestAfterDivZeroChecker.cpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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. 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. PathDiagnosticPiece *DivisionBRVisitor::VisitNode(const ExplodedNode *Succ,
  81. const ExplodedNode *Pred,
  82. BugReporterContext &BRC,
  83. BugReport &BR) {
  84. if (Satisfied)
  85. return nullptr;
  86. const Expr *E = nullptr;
  87. if (Optional<PostStmt> P = Succ->getLocationAs<PostStmt>())
  88. if (const BinaryOperator *BO = P->getStmtAs<BinaryOperator>()) {
  89. BinaryOperator::Opcode Op = BO->getOpcode();
  90. if (Op == BO_Div || Op == BO_Rem || Op == BO_DivAssign ||
  91. Op == BO_RemAssign) {
  92. E = BO->getRHS();
  93. }
  94. }
  95. if (!E)
  96. return nullptr;
  97. ProgramStateRef State = Succ->getState();
  98. SVal S = State->getSVal(E, Succ->getLocationContext());
  99. if (ZeroSymbol == S.getAsSymbol() && SFC == Succ->getStackFrame()) {
  100. Satisfied = true;
  101. // Construct a new PathDiagnosticPiece.
  102. ProgramPoint P = Succ->getLocation();
  103. PathDiagnosticLocation L =
  104. PathDiagnosticLocation::create(P, BRC.getSourceManager());
  105. if (!L.isValid() || !L.asLocation().isValid())
  106. return nullptr;
  107. return new PathDiagnosticEventPiece(
  108. L, "Division with compared value made here");
  109. }
  110. return nullptr;
  111. }
  112. bool TestAfterDivZeroChecker::isZero(SVal S, CheckerContext &C) const {
  113. Optional<DefinedSVal> DSV = S.getAs<DefinedSVal>();
  114. if (!DSV)
  115. return false;
  116. ConstraintManager &CM = C.getConstraintManager();
  117. return !CM.assume(C.getState(), *DSV, true);
  118. }
  119. void TestAfterDivZeroChecker::setDivZeroMap(SVal Var, CheckerContext &C) const {
  120. SymbolRef SR = Var.getAsSymbol();
  121. if (!SR)
  122. return;
  123. ProgramStateRef State = C.getState();
  124. State =
  125. State->add<DivZeroMap>(ZeroState(SR, C.getBlockID(), C.getStackFrame()));
  126. C.addTransition(State);
  127. }
  128. bool TestAfterDivZeroChecker::hasDivZeroMap(SVal Var,
  129. const CheckerContext &C) const {
  130. SymbolRef SR = Var.getAsSymbol();
  131. if (!SR)
  132. return false;
  133. ZeroState ZS(SR, C.getBlockID(), C.getStackFrame());
  134. return C.getState()->contains<DivZeroMap>(ZS);
  135. }
  136. void TestAfterDivZeroChecker::reportBug(SVal Val, CheckerContext &C) const {
  137. if (ExplodedNode *N = C.generateSink(C.getState())) {
  138. if (!DivZeroBug)
  139. DivZeroBug.reset(new BuiltinBug(this, "Division by zero"));
  140. auto R = llvm::make_unique<BugReport>(
  141. *DivZeroBug, "Value being compared against zero has already been used "
  142. "for division",
  143. N);
  144. R->addVisitor(llvm::make_unique<DivisionBRVisitor>(Val.getAsSymbol(),
  145. C.getStackFrame()));
  146. C.emitReport(std::move(R));
  147. }
  148. }
  149. void TestAfterDivZeroChecker::checkEndFunction(CheckerContext &C) const {
  150. ProgramStateRef State = C.getState();
  151. DivZeroMapTy DivZeroes = State->get<DivZeroMap>();
  152. if (DivZeroes.isEmpty())
  153. return;
  154. DivZeroMapTy::Factory &F = State->get_context<DivZeroMap>();
  155. for (llvm::ImmutableSet<ZeroState>::iterator I = DivZeroes.begin(),
  156. E = DivZeroes.end();
  157. I != E; ++I) {
  158. ZeroState ZS = *I;
  159. if (ZS.getStackFrameContext() == C.getStackFrame())
  160. DivZeroes = F.remove(DivZeroes, ZS);
  161. }
  162. C.addTransition(State->set<DivZeroMap>(DivZeroes));
  163. }
  164. void TestAfterDivZeroChecker::checkPreStmt(const BinaryOperator *B,
  165. CheckerContext &C) const {
  166. BinaryOperator::Opcode Op = B->getOpcode();
  167. if (Op == BO_Div || Op == BO_Rem || Op == BO_DivAssign ||
  168. Op == BO_RemAssign) {
  169. SVal S = C.getSVal(B->getRHS());
  170. if (!isZero(S, C))
  171. setDivZeroMap(S, C);
  172. }
  173. }
  174. void TestAfterDivZeroChecker::checkBranchCondition(const Stmt *Condition,
  175. CheckerContext &C) const {
  176. if (const BinaryOperator *B = dyn_cast<BinaryOperator>(Condition)) {
  177. if (B->isComparisonOp()) {
  178. const IntegerLiteral *IntLiteral = dyn_cast<IntegerLiteral>(B->getRHS());
  179. bool LRHS = true;
  180. if (!IntLiteral) {
  181. IntLiteral = dyn_cast<IntegerLiteral>(B->getLHS());
  182. LRHS = false;
  183. }
  184. if (!IntLiteral || IntLiteral->getValue() != 0)
  185. return;
  186. SVal Val = C.getSVal(LRHS ? B->getLHS() : B->getRHS());
  187. if (hasDivZeroMap(Val, C))
  188. reportBug(Val, C);
  189. }
  190. } else if (const UnaryOperator *U = dyn_cast<UnaryOperator>(Condition)) {
  191. if (U->getOpcode() == UO_LNot) {
  192. SVal Val;
  193. if (const ImplicitCastExpr *I =
  194. dyn_cast<ImplicitCastExpr>(U->getSubExpr()))
  195. Val = C.getSVal(I->getSubExpr());
  196. if (hasDivZeroMap(Val, C))
  197. reportBug(Val, C);
  198. else {
  199. Val = C.getSVal(U->getSubExpr());
  200. if (hasDivZeroMap(Val, C))
  201. reportBug(Val, C);
  202. }
  203. }
  204. } else if (const ImplicitCastExpr *IE =
  205. dyn_cast<ImplicitCastExpr>(Condition)) {
  206. SVal Val = C.getSVal(IE->getSubExpr());
  207. if (hasDivZeroMap(Val, C))
  208. reportBug(Val, C);
  209. else {
  210. SVal Val = C.getSVal(Condition);
  211. if (hasDivZeroMap(Val, C))
  212. reportBug(Val, C);
  213. }
  214. }
  215. }
  216. void ento::registerTestAfterDivZeroChecker(CheckerManager &mgr) {
  217. mgr.registerChecker<TestAfterDivZeroChecker>();
  218. }