clang  7.0.0svn
DivZeroChecker.cpp
Go to the documentation of this file.
1 //== DivZeroChecker.cpp - Division by zero checker --------------*- C++ -*--==//
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 DivZeroChecker, a builtin check in ExprEngine that performs
11 // checks for division by zeros.
12 //
13 //===----------------------------------------------------------------------===//
14 
15 #include "ClangSACheckers.h"
20 
21 using namespace clang;
22 using namespace ento;
23 
24 namespace {
25 class DivZeroChecker : public Checker< check::PreStmt<BinaryOperator> > {
26  mutable std::unique_ptr<BuiltinBug> BT;
27  void reportBug(const char *Msg, ProgramStateRef StateZero, CheckerContext &C,
28  std::unique_ptr<BugReporterVisitor> Visitor = nullptr) const;
29 
30 public:
31  void checkPreStmt(const BinaryOperator *B, CheckerContext &C) const;
32 };
33 } // end anonymous namespace
34 
35 void DivZeroChecker::reportBug(
36  const char *Msg, ProgramStateRef StateZero, CheckerContext &C,
37  std::unique_ptr<BugReporterVisitor> Visitor) const {
38  if (ExplodedNode *N = C.generateErrorNode(StateZero)) {
39  if (!BT)
40  BT.reset(new BuiltinBug(this, "Division by zero"));
41 
42  auto R = llvm::make_unique<BugReport>(*BT, Msg, N);
43  R->addVisitor(std::move(Visitor));
45  C.emitReport(std::move(R));
46  }
47 }
48 
49 void DivZeroChecker::checkPreStmt(const BinaryOperator *B,
50  CheckerContext &C) const {
52  if (Op != BO_Div &&
53  Op != BO_Rem &&
54  Op != BO_DivAssign &&
55  Op != BO_RemAssign)
56  return;
57 
58  if (!B->getRHS()->getType()->isScalarType())
59  return;
60 
61  SVal Denom = C.getSVal(B->getRHS());
63 
64  // Divide-by-undefined handled in the generic checking for uses of
65  // undefined values.
66  if (!DV)
67  return;
68 
69  // Check for divide by zero.
71  ProgramStateRef stateNotZero, stateZero;
72  std::tie(stateNotZero, stateZero) = CM.assumeDual(C.getState(), *DV);
73 
74  if (!stateNotZero) {
75  assert(stateZero);
76  reportBug("Division by zero", stateZero, C);
77  return;
78  }
79 
80  bool TaintedD = C.getState()->isTainted(*DV);
81  if ((stateNotZero && stateZero && TaintedD)) {
82  reportBug("Division by a tainted value, possibly zero", stateZero, C,
83  llvm::make_unique<TaintBugVisitor>(*DV));
84  return;
85  }
86 
87  // If we get here, then the denom should not be zero. We abandon the implicit
88  // zero denom case for now.
89  C.addTransition(stateNotZero);
90 }
91 
92 void ento::registerDivZeroChecker(CheckerManager &mgr) {
93  mgr.registerChecker<DivZeroChecker>();
94 }
ExplodedNode * generateErrorNode(ProgramStateRef State=nullptr, const ProgramPointTag *Tag=nullptr)
Generate a transition to a node that will be used to report an error.
ExplodedNode * addTransition(ProgramStateRef State=nullptr, const ProgramPointTag *Tag=nullptr)
Generates a new transition in the program state graph (ExplodedGraph).
Opcode getOpcode() const
Definition: Expr.h:3106
const Stmt * GetDenomExpr(const ExplodedNode *N)
SVal getSVal(const Stmt *S) const
Get the value of arbitrary expressions at this point in the path.
BinaryOperatorKind
A builtin binary operation expression such as "x + y" or "x <= y".
Definition: Expr.h:3065
bool isScalarType() const
Definition: Type.h:6355
ProgramStatePair assumeDual(ProgramStateRef State, DefinedSVal Cond)
Returns a pair of states (StTrue, StFalse) where the given condition is assumed to be true or false...
QualType getType() const
Definition: Expr.h:128
Optional< T > getAs() const
Convert to the specified SVal type, returning None if this SVal is not of the desired type...
Definition: SVals.h:112
ConstraintManager & getConstraintManager()
void emitReport(std::unique_ptr< BugReport > R)
Emit the diagnostics report.
CHECKER * registerChecker()
Used to register checkers.
SVal - This represents a symbolic expression, which can be either an L-value or an R-value...
Definition: SVals.h:76
Dataflow Directional Tag Classes.
const ProgramStateRef & getState() const
bool trackNullOrUndefValue(const ExplodedNode *N, const Stmt *S, BugReport &R, bool IsArg=false, bool EnableNullFPSuppression=true)
Attempts to add visitors to trace a null or undefined value back to its point of origin, whether it is a symbol constrained to null or an explicit assignment.
Expr * getRHS() const
Definition: Expr.h:3111