2009-10-31 18:02:37 +08:00
|
|
|
//== DivZeroChecker.cpp - Division by zero checker --------------*- C++ -*--==//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2010-12-23 02:53:44 +08:00
|
|
|
// This defines DivZeroChecker, a builtin check in ExprEngine that performs
|
2009-10-31 18:02:37 +08:00
|
|
|
// checks for division by zeros.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2011-02-28 09:27:50 +08:00
|
|
|
#include "ClangSACheckers.h"
|
2012-12-04 17:13:33 +08:00
|
|
|
#include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
|
2011-03-01 09:16:21 +08:00
|
|
|
#include "clang/StaticAnalyzer/Core/Checker.h"
|
2011-02-28 09:27:50 +08:00
|
|
|
#include "clang/StaticAnalyzer/Core/CheckerManager.h"
|
|
|
|
#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
|
2009-10-31 18:02:37 +08:00
|
|
|
|
|
|
|
using namespace clang;
|
2010-12-23 15:20:52 +08:00
|
|
|
using namespace ento;
|
2009-10-31 18:02:37 +08:00
|
|
|
|
2009-11-07 04:47:51 +08:00
|
|
|
namespace {
|
2011-03-01 09:16:21 +08:00
|
|
|
class DivZeroChecker : public Checker< check::PreStmt<BinaryOperator> > {
|
2012-02-05 10:12:40 +08:00
|
|
|
mutable OwningPtr<BuiltinBug> BT;
|
2012-01-21 04:28:31 +08:00
|
|
|
void reportBug(const char *Msg,
|
2012-01-27 05:29:00 +08:00
|
|
|
ProgramStateRef StateZero,
|
2012-01-21 04:28:31 +08:00
|
|
|
CheckerContext &C) const ;
|
2009-11-07 04:47:51 +08:00
|
|
|
public:
|
2011-02-28 09:27:50 +08:00
|
|
|
void checkPreStmt(const BinaryOperator *B, CheckerContext &C) const;
|
2009-11-07 04:47:51 +08:00
|
|
|
};
|
|
|
|
} // end anonymous namespace
|
|
|
|
|
2012-01-21 04:28:31 +08:00
|
|
|
void DivZeroChecker::reportBug(const char *Msg,
|
2012-01-27 05:29:00 +08:00
|
|
|
ProgramStateRef StateZero,
|
2012-01-21 04:28:31 +08:00
|
|
|
CheckerContext &C) const {
|
|
|
|
if (ExplodedNode *N = C.generateSink(StateZero)) {
|
|
|
|
if (!BT)
|
2014-02-12 05:49:21 +08:00
|
|
|
BT.reset(new BuiltinBug(this, "Division by zero"));
|
2012-01-21 04:28:31 +08:00
|
|
|
|
2012-08-28 08:50:51 +08:00
|
|
|
BugReport *R = new BugReport(*BT, Msg, N);
|
|
|
|
bugreporter::trackNullOrUndefValue(N, bugreporter::GetDenomExpr(N), *R);
|
2012-11-02 09:53:40 +08:00
|
|
|
C.emitReport(R);
|
2012-01-21 04:28:31 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-02-28 09:27:50 +08:00
|
|
|
void DivZeroChecker::checkPreStmt(const BinaryOperator *B,
|
|
|
|
CheckerContext &C) const {
|
2009-10-31 18:02:37 +08:00
|
|
|
BinaryOperator::Opcode Op = B->getOpcode();
|
2010-08-25 19:45:40 +08:00
|
|
|
if (Op != BO_Div &&
|
|
|
|
Op != BO_Rem &&
|
|
|
|
Op != BO_DivAssign &&
|
|
|
|
Op != BO_RemAssign)
|
2009-10-31 18:02:37 +08:00
|
|
|
return;
|
|
|
|
|
2012-07-11 00:27:55 +08:00
|
|
|
if (!B->getRHS()->getType()->isScalarType())
|
2009-10-31 18:02:37 +08:00
|
|
|
return;
|
|
|
|
|
2012-01-07 06:09:28 +08:00
|
|
|
SVal Denom = C.getState()->getSVal(B->getRHS(), C.getLocationContext());
|
2013-02-21 06:23:23 +08:00
|
|
|
Optional<DefinedSVal> DV = Denom.getAs<DefinedSVal>();
|
2009-10-31 18:02:37 +08:00
|
|
|
|
|
|
|
// Divide-by-undefined handled in the generic checking for uses of
|
|
|
|
// undefined values.
|
|
|
|
if (!DV)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Check for divide by zero.
|
|
|
|
ConstraintManager &CM = C.getConstraintManager();
|
2012-01-27 05:29:00 +08:00
|
|
|
ProgramStateRef stateNotZero, stateZero;
|
2014-03-02 21:01:17 +08:00
|
|
|
std::tie(stateNotZero, stateZero) = CM.assumeDual(C.getState(), *DV);
|
2009-10-31 18:02:37 +08:00
|
|
|
|
2011-11-09 03:56:35 +08:00
|
|
|
if (!stateNotZero) {
|
|
|
|
assert(stateZero);
|
2012-01-21 04:28:31 +08:00
|
|
|
reportBug("Division by zero", stateZero, C);
|
|
|
|
return;
|
|
|
|
}
|
2009-10-31 18:02:37 +08:00
|
|
|
|
2012-01-21 04:28:31 +08:00
|
|
|
bool TaintedD = C.getState()->isTainted(*DV);
|
|
|
|
if ((stateNotZero && stateZero && TaintedD)) {
|
|
|
|
reportBug("Division by a tainted value, possibly zero", stateZero, C);
|
2009-10-31 18:02:37 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we get here, then the denom should not be zero. We abandon the implicit
|
|
|
|
// zero denom case for now.
|
2011-10-27 05:06:34 +08:00
|
|
|
C.addTransition(stateNotZero);
|
2009-10-31 18:02:37 +08:00
|
|
|
}
|
2011-02-28 09:27:50 +08:00
|
|
|
|
|
|
|
void ento::registerDivZeroChecker(CheckerManager &mgr) {
|
|
|
|
mgr.registerChecker<DivZeroChecker>();
|
|
|
|
}
|