Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/tools/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
187
static const Expr *getDenomExpr(const ExplodedNode *N) {
37
187
  const Stmt *S = N->getLocationAs<PreStmt>()->getStmt();
38
187
  if (const auto *BE = dyn_cast<BinaryOperator>(S))
39
187
    return BE->getRHS();
40
0
  return nullptr;
41
0
}
42
43
void DivZeroChecker::reportBug(
44
    const char *Msg, ProgramStateRef StateZero, CheckerContext &C,
45
187
    std::unique_ptr<BugReporterVisitor> Visitor) const {
46
187
  if (ExplodedNode *N = C.generateErrorNode(StateZero)) {
47
187
    if (!BT)
48
75
      BT.reset(new BuiltinBug(this, "Division by zero"));
49
187
50
187
    auto R = llvm::make_unique<BugReport>(*BT, Msg, N);
51
187
    R->addVisitor(std::move(Visitor));
52
187
    bugreporter::trackExpressionValue(N, getDenomExpr(N), *R);
53
187
    C.emitReport(std::move(R));
54
187
  }
55
187
}
56
57
void DivZeroChecker::checkPreStmt(const BinaryOperator *B,
58
68.5k
                                  CheckerContext &C) const {
59
68.5k
  BinaryOperator::Opcode Op = B->getOpcode();
60
68.5k
  if (Op != BO_Div &&
61
68.5k
      
Op != BO_Rem68.0k
&&
62
68.5k
      
Op != BO_DivAssign67.9k
&&
63
68.5k
      
Op != BO_RemAssign67.9k
)
64
67.9k
    return;
65
602
66
602
  if (!B->getRHS()->getType()->isScalarType())
67
0
    return;
68
602
69
602
  SVal Denom = C.getSVal(B->getRHS());
70
602
  Optional<DefinedSVal> DV = Denom.getAs<DefinedSVal>();
71
602
72
602
  // Divide-by-undefined handled in the generic checking for uses of
73
602
  // undefined values.
74
602
  if (!DV)
75
111
    return;
76
491
77
491
  // Check for divide by zero.
78
491
  ConstraintManager &CM = C.getConstraintManager();
79
491
  ProgramStateRef stateNotZero, stateZero;
80
491
  std::tie(stateNotZero, stateZero) = CM.assumeDual(C.getState(), *DV);
81
491
82
491
  if (!stateNotZero) {
83
180
    assert(stateZero);
84
180
    reportBug("Division by zero", stateZero, C);
85
180
    return;
86
180
  }
87
311
88
311
  bool TaintedD = isTainted(C.getState(), *DV);
89
311
  if ((stateNotZero && stateZero && 
TaintedD105
)) {
90
7
    reportBug("Division by a tainted value, possibly zero", stateZero, C,
91
7
              llvm::make_unique<taint::TaintBugVisitor>(*DV));
92
7
    return;
93
7
  }
94
304
95
304
  // If we get here, then the denom should not be zero. We abandon the implicit
96
304
  // zero denom case for now.
97
304
  C.addTransition(stateNotZero);
98
304
}
99
100
682
void ento::registerDivZeroChecker(CheckerManager &mgr) {
101
682
  mgr.registerChecker<DivZeroChecker>();
102
682
}
103
104
683
bool ento::shouldRegisterDivZeroChecker(const LangOptions &LO) {
105
683
  return true;
106
683
}