2009-10-31 18:02:37 +08:00
|
|
|
//== DivZeroChecker.cpp - Division by zero checker --------------*- C++ -*--==//
|
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2009-10-31 18:02:37 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
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.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2019-03-30 06:49:30 +08:00
|
|
|
#include "Taint.h"
|
[analyzer][NFC] Move CheckerRegistry from the Core directory to Frontend
ClangCheckerRegistry is a very non-obvious, poorly documented, weird concept.
It derives from CheckerRegistry, and is placed in lib/StaticAnalyzer/Frontend,
whereas it's base is located in lib/StaticAnalyzer/Core. It was, from what I can
imagine, used to circumvent the problem that the registry functions of the
checkers are located in the clangStaticAnalyzerCheckers library, but that
library depends on clangStaticAnalyzerCore. However, clangStaticAnalyzerFrontend
depends on both of those libraries.
One can make the observation however, that CheckerRegistry has no place in Core,
it isn't used there at all! The only place where it is used is Frontend, which
is where it ultimately belongs.
This move implies that since
include/clang/StaticAnalyzer/Checkers/ClangCheckers.h only contained a single function:
class CheckerRegistry;
void registerBuiltinCheckers(CheckerRegistry ®istry);
it had to re purposed, as CheckerRegistry is no longer available to
clangStaticAnalyzerCheckers. It was renamed to BuiltinCheckerRegistration.h,
which actually describes it a lot better -- it does not contain the registration
functions for checkers, but only those generated by the tblgen files.
Differential Revision: https://reviews.llvm.org/D54436
llvm-svn: 349275
2018-12-16 00:23:51 +08:00
|
|
|
#include "clang/StaticAnalyzer/Checkers/BuiltinCheckerRegistration.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;
|
2019-03-30 06:49:30 +08:00
|
|
|
using namespace taint;
|
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> > {
|
2014-03-08 04:03:18 +08:00
|
|
|
mutable std::unique_ptr<BuiltinBug> BT;
|
2018-05-02 20:11:22 +08:00
|
|
|
void reportBug(const char *Msg, ProgramStateRef StateZero, CheckerContext &C,
|
|
|
|
std::unique_ptr<BugReporterVisitor> Visitor = nullptr) 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;
|
2015-09-08 11:50:52 +08:00
|
|
|
};
|
2009-11-07 04:47:51 +08:00
|
|
|
} // end anonymous namespace
|
|
|
|
|
2018-10-24 02:24:53 +08:00
|
|
|
static const Expr *getDenomExpr(const ExplodedNode *N) {
|
|
|
|
const Stmt *S = N->getLocationAs<PreStmt>()->getStmt();
|
|
|
|
if (const auto *BE = dyn_cast<BinaryOperator>(S))
|
|
|
|
return BE->getRHS();
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2018-05-02 20:11:22 +08:00
|
|
|
void DivZeroChecker::reportBug(
|
|
|
|
const char *Msg, ProgramStateRef StateZero, CheckerContext &C,
|
|
|
|
std::unique_ptr<BugReporterVisitor> Visitor) const {
|
2015-09-17 06:03:05 +08:00
|
|
|
if (ExplodedNode *N = C.generateErrorNode(StateZero)) {
|
2012-01-21 04:28:31 +08:00
|
|
|
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
|
|
|
|
2015-06-23 21:15:32 +08:00
|
|
|
auto R = llvm::make_unique<BugReport>(*BT, Msg, N);
|
2018-05-02 20:11:22 +08:00
|
|
|
R->addVisitor(std::move(Visitor));
|
2018-10-24 02:24:53 +08:00
|
|
|
bugreporter::trackExpressionValue(N, getDenomExpr(N), *R);
|
2015-06-23 21:15:32 +08:00
|
|
|
C.emitReport(std::move(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;
|
|
|
|
|
2018-01-18 04:27:29 +08:00
|
|
|
SVal Denom = C.getSVal(B->getRHS());
|
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
|
|
|
|
2019-03-30 06:49:30 +08:00
|
|
|
bool TaintedD = isTainted(C.getState(), *DV);
|
2012-01-21 04:28:31 +08:00
|
|
|
if ((stateNotZero && stateZero && TaintedD)) {
|
2018-05-02 20:11:22 +08:00
|
|
|
reportBug("Division by a tainted value, possibly zero", stateZero, C,
|
2019-03-30 06:49:30 +08:00
|
|
|
llvm::make_unique<taint::TaintBugVisitor>(*DV));
|
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>();
|
|
|
|
}
|
2019-01-26 22:23:08 +08:00
|
|
|
|
|
|
|
bool ento::shouldRegisterDivZeroChecker(const LangOptions &LO) {
|
|
|
|
return true;
|
|
|
|
}
|