Coverage Report

Created: 2022-01-25 06:29

/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/lib/StaticAnalyzer/Checkers/DivZeroChecker.cpp
Line
Count
Source (jump to first uncovered line)
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
14
#include "Taint.h"
15
#include "clang/StaticAnalyzer/Checkers/BuiltinCheckerRegistration.h"
16
#include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
17
#include "clang/StaticAnalyzer/Core/Checker.h"
18
#include "clang/StaticAnalyzer/Core/CheckerManager.h"
19
#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
20
21
using namespace clang;
22
using namespace ento;
23
using namespace taint;
24
25
namespace {
26
class DivZeroChecker : public Checker< check::PreStmt<BinaryOperator> > {
27
  mutable std::unique_ptr<BuiltinBug> BT;
28
  void reportBug(const char *Msg, ProgramStateRef StateZero, CheckerContext &C,
29
                 std::unique_ptr<BugReporterVisitor> Visitor = nullptr) const;
30
31
public:
32
  void checkPreStmt(const BinaryOperator *B, CheckerContext &C) const;
33
};
34
} // end anonymous namespace
35
36
228
static const Expr *getDenomExpr(const ExplodedNode *N) {
37
228
  const Stmt *S = N->getLocationAs<PreStmt>()->getStmt();
38
228
  if (const auto *BE = dyn_cast<BinaryOperator>(S))
39
228
    return BE->getRHS();
40
0
  return nullptr;
41
228
}
42
43
void DivZeroChecker::reportBug(
44
    const char *Msg, ProgramStateRef StateZero, CheckerContext &C,
45
228
    std::unique_ptr<BugReporterVisitor> Visitor) const {
46
228
  if (ExplodedNode *N = C.generateErrorNode(StateZero)) {
47
228
    if (!BT)
48
94
      BT.reset(new BuiltinBug(this, "Division by zero"));
49
50
228
    auto R = std::make_unique<PathSensitiveBugReport>(*BT, Msg, N);
51
228
    R->addVisitor(std::move(Visitor));
52
228
    bugreporter::trackExpressionValue(N, getDenomExpr(N), *R);
53
228
    C.emitReport(std::move(R));
54
228
  }
55
228
}
56
57
void DivZeroChecker::checkPreStmt(const BinaryOperator *B,
58
71.4k
                                  CheckerContext &C) const {
59
71.4k
  BinaryOperator::Opcode Op = B->getOpcode();
60
71.4k
  if (Op != BO_Div &&
61
71.4k
      
Op != BO_Rem70.8k
&&
62
71.4k
      
Op != BO_DivAssign70.6k
&&
63
71.4k
      
Op != BO_RemAssign70.6k
)
64
70.6k
    return;
65
66
803
  if (!B->getRHS()->getType()->isScalarType())
67
0
    return;
68
69
803
  SVal Denom = C.getSVal(B->getRHS());
70
803
  Optional<DefinedSVal> DV = Denom.getAs<DefinedSVal>();
71
72
  // Divide-by-undefined handled in the generic checking for uses of
73
  // undefined values.
74
803
  if (!DV)
75
112
    return;
76
77
  // Check for divide by zero.
78
691
  ConstraintManager &CM = C.getConstraintManager();
79
691
  ProgramStateRef stateNotZero, stateZero;
80
691
  std::tie(stateNotZero, stateZero) = CM.assumeDual(C.getState(), *DV);
81
82
691
  if (!stateNotZero) {
83
219
    assert(stateZero);
84
0
    reportBug("Division by zero", stateZero, C);
85
219
    return;
86
219
  }
87
88
472
  bool TaintedD = isTainted(C.getState(), *DV);
89
472
  if ((stateNotZero && stateZero && 
TaintedD142
)) {
90
9
    reportBug("Division by a tainted value, possibly zero", stateZero, C,
91
9
              std::make_unique<taint::TaintBugVisitor>(*DV));
92
9
    return;
93
9
  }
94
95
  // If we get here, then the denom should not be zero. We abandon the implicit
96
  // zero denom case for now.
97
463
  C.addTransition(stateNotZero);
98
463
}
99
100
1.16k
void ento::registerDivZeroChecker(CheckerManager &mgr) {
101
1.16k
  mgr.registerChecker<DivZeroChecker>();
102
1.16k
}
103
104
2.32k
bool ento::shouldRegisterDivZeroChecker(const CheckerManager &mgr) {
105
2.32k
  return true;
106
2.32k
}