DivZeroChecker.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. //== DivZeroChecker.cpp - Division by zero checker --------------*- C++ -*--==//
  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 DivZeroChecker, a builtin check in ExprEngine that performs
  10. // checks for division by zeros.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "clang/StaticAnalyzer/Checkers/BuiltinCheckerRegistration.h"
  14. #include "clang/StaticAnalyzer/Checkers/Taint.h"
  15. #include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
  16. #include "clang/StaticAnalyzer/Core/Checker.h"
  17. #include "clang/StaticAnalyzer/Core/CheckerManager.h"
  18. #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
  19. #include <optional>
  20. using namespace clang;
  21. using namespace ento;
  22. using namespace taint;
  23. namespace {
  24. class DivZeroChecker : public Checker< check::PreStmt<BinaryOperator> > {
  25. mutable std::unique_ptr<BuiltinBug> BT;
  26. void reportBug(const char *Msg, ProgramStateRef StateZero, CheckerContext &C,
  27. std::unique_ptr<BugReporterVisitor> Visitor = nullptr) const;
  28. public:
  29. void checkPreStmt(const BinaryOperator *B, CheckerContext &C) const;
  30. };
  31. } // end anonymous namespace
  32. static const Expr *getDenomExpr(const ExplodedNode *N) {
  33. const Stmt *S = N->getLocationAs<PreStmt>()->getStmt();
  34. if (const auto *BE = dyn_cast<BinaryOperator>(S))
  35. return BE->getRHS();
  36. return nullptr;
  37. }
  38. void DivZeroChecker::reportBug(
  39. const char *Msg, ProgramStateRef StateZero, CheckerContext &C,
  40. std::unique_ptr<BugReporterVisitor> Visitor) const {
  41. if (ExplodedNode *N = C.generateErrorNode(StateZero)) {
  42. if (!BT)
  43. BT.reset(new BuiltinBug(this, "Division by zero"));
  44. auto R = std::make_unique<PathSensitiveBugReport>(*BT, Msg, N);
  45. R->addVisitor(std::move(Visitor));
  46. bugreporter::trackExpressionValue(N, getDenomExpr(N), *R);
  47. C.emitReport(std::move(R));
  48. }
  49. }
  50. void DivZeroChecker::checkPreStmt(const BinaryOperator *B,
  51. CheckerContext &C) const {
  52. BinaryOperator::Opcode Op = B->getOpcode();
  53. if (Op != BO_Div &&
  54. Op != BO_Rem &&
  55. Op != BO_DivAssign &&
  56. Op != BO_RemAssign)
  57. return;
  58. if (!B->getRHS()->getType()->isScalarType())
  59. return;
  60. SVal Denom = C.getSVal(B->getRHS());
  61. std::optional<DefinedSVal> DV = Denom.getAs<DefinedSVal>();
  62. // Divide-by-undefined handled in the generic checking for uses of
  63. // undefined values.
  64. if (!DV)
  65. return;
  66. // Check for divide by zero.
  67. ConstraintManager &CM = C.getConstraintManager();
  68. ProgramStateRef stateNotZero, stateZero;
  69. std::tie(stateNotZero, stateZero) = CM.assumeDual(C.getState(), *DV);
  70. if (!stateNotZero) {
  71. assert(stateZero);
  72. reportBug("Division by zero", stateZero, C);
  73. return;
  74. }
  75. bool TaintedD = isTainted(C.getState(), *DV);
  76. if ((stateNotZero && stateZero && TaintedD)) {
  77. reportBug("Division by a tainted value, possibly zero", stateZero, C,
  78. std::make_unique<taint::TaintBugVisitor>(*DV));
  79. return;
  80. }
  81. // If we get here, then the denom should not be zero. We abandon the implicit
  82. // zero denom case for now.
  83. C.addTransition(stateNotZero);
  84. }
  85. void ento::registerDivZeroChecker(CheckerManager &mgr) {
  86. mgr.registerChecker<DivZeroChecker>();
  87. }
  88. bool ento::shouldRegisterDivZeroChecker(const CheckerManager &mgr) {
  89. return true;
  90. }