clang  6.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,
28  ProgramStateRef StateZero,
29  CheckerContext &C) const ;
30 public:
31  void checkPreStmt(const BinaryOperator *B, CheckerContext &C) const;
32 };
33 } // end anonymous namespace
34 
35 void DivZeroChecker::reportBug(const char *Msg,
36  ProgramStateRef StateZero,
37  CheckerContext &C) 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);
44  C.emitReport(std::move(R));
45  }
46 }
47 
48 void DivZeroChecker::checkPreStmt(const BinaryOperator *B,
49  CheckerContext &C) const {
51  if (Op != BO_Div &&
52  Op != BO_Rem &&
53  Op != BO_DivAssign &&
54  Op != BO_RemAssign)
55  return;
56 
57  if (!B->getRHS()->getType()->isScalarType())
58  return;
59 
60  SVal Denom = C.getState()->getSVal(B->getRHS(), C.getLocationContext());
62 
63  // Divide-by-undefined handled in the generic checking for uses of
64  // undefined values.
65  if (!DV)
66  return;
67 
68  // Check for divide by zero.
70  ProgramStateRef stateNotZero, stateZero;
71  std::tie(stateNotZero, stateZero) = CM.assumeDual(C.getState(), *DV);
72 
73  if (!stateNotZero) {
74  assert(stateZero);
75  reportBug("Division by zero", stateZero, C);
76  return;
77  }
78 
79  bool TaintedD = C.getState()->isTainted(*DV);
80  if ((stateNotZero && stateZero && TaintedD)) {
81  reportBug("Division by a tainted value, possibly zero", stateZero, C);
82  return;
83  }
84 
85  // If we get here, then the denom should not be zero. We abandon the implicit
86  // zero denom case for now.
87  C.addTransition(stateNotZero);
88 }
89 
90 void ento::registerDivZeroChecker(CheckerManager &mgr) {
91  mgr.registerChecker<DivZeroChecker>();
92 }
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:3026
const Stmt * GetDenomExpr(const ExplodedNode *N)
BinaryOperatorKind
A builtin binary operation expression such as "x + y" or "x <= y".
Definition: Expr.h:2985
bool isScalarType() const
Definition: Type.h:6206
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:100
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:63
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:3031
const LocationContext * getLocationContext() const