2014-07-11 00:10:52 +08:00
|
|
|
//== TestAfterDivZeroChecker.cpp - Test after division by zero checker --*--==//
|
|
|
|
//
|
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
|
2014-07-11 00:10:52 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This defines TestAfterDivZeroChecker, a builtin check that performs checks
|
|
|
|
// for division by zero where the division occurs before comparison with zero.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
[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"
|
2014-07-11 00:10:52 +08:00
|
|
|
#include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
|
|
|
|
#include "clang/StaticAnalyzer/Core/Checker.h"
|
|
|
|
#include "clang/StaticAnalyzer/Core/PathSensitive/CallEvent.h"
|
|
|
|
#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
|
|
|
|
#include "llvm/ADT/FoldingSet.h"
|
|
|
|
|
|
|
|
using namespace clang;
|
|
|
|
using namespace ento;
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
class ZeroState {
|
|
|
|
private:
|
|
|
|
SymbolRef ZeroSymbol;
|
|
|
|
unsigned BlockID;
|
|
|
|
const StackFrameContext *SFC;
|
|
|
|
|
|
|
|
public:
|
|
|
|
ZeroState(SymbolRef S, unsigned B, const StackFrameContext *SFC)
|
|
|
|
: ZeroSymbol(S), BlockID(B), SFC(SFC) {}
|
|
|
|
|
|
|
|
const StackFrameContext *getStackFrameContext() const { return SFC; }
|
|
|
|
|
|
|
|
bool operator==(const ZeroState &X) const {
|
|
|
|
return BlockID == X.BlockID && SFC == X.SFC && ZeroSymbol == X.ZeroSymbol;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool operator<(const ZeroState &X) const {
|
|
|
|
if (BlockID != X.BlockID)
|
|
|
|
return BlockID < X.BlockID;
|
|
|
|
if (SFC != X.SFC)
|
|
|
|
return SFC < X.SFC;
|
|
|
|
return ZeroSymbol < X.ZeroSymbol;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Profile(llvm::FoldingSetNodeID &ID) const {
|
|
|
|
ID.AddInteger(BlockID);
|
|
|
|
ID.AddPointer(SFC);
|
|
|
|
ID.AddPointer(ZeroSymbol);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
[analyzer] Do not run visitors until the fixpoint, run only once.
In the current implementation, we run visitors until the fixed point is
reached.
That is, if a visitor adds another visitor, the currently processed path
is destroyed, all diagnostics is discarded, and it is regenerated again,
until it's no longer modified.
This pattern has a few negative implications:
- This loop does not even guarantee to terminate.
E.g. just imagine two visitors bouncing a diagnostics around.
- Performance-wise, e.g. for sqlite3 all visitors are being re-run at
least 10 times for some bugs.
We have already seen a few reports where it leads to timeouts.
- If we want to add more computationally intense visitors, this will
become worse.
- From architectural standpoint, the current layout requires copying
visitors, which is conceptually wrong, and can be annoying (e.g. no
unique_ptr on visitors allowed).
The proposed change is a much simpler architecture: the outer loop
processes nodes upwards, and whenever the visitor is added it only
processes current nodes and above, thus guaranteeing termination.
Differential Revision: https://reviews.llvm.org/D47856
llvm-svn: 335666
2018-06-27 05:12:08 +08:00
|
|
|
class DivisionBRVisitor : public BugReporterVisitor {
|
2014-07-11 00:10:52 +08:00
|
|
|
private:
|
|
|
|
SymbolRef ZeroSymbol;
|
|
|
|
const StackFrameContext *SFC;
|
2014-07-11 08:32:35 +08:00
|
|
|
bool Satisfied;
|
2014-07-11 00:10:52 +08:00
|
|
|
|
|
|
|
public:
|
|
|
|
DivisionBRVisitor(SymbolRef ZeroSymbol, const StackFrameContext *SFC)
|
2014-07-11 08:32:35 +08:00
|
|
|
: ZeroSymbol(ZeroSymbol), SFC(SFC), Satisfied(false) {}
|
2014-07-11 00:10:52 +08:00
|
|
|
|
|
|
|
void Profile(llvm::FoldingSetNodeID &ID) const override {
|
|
|
|
ID.Add(ZeroSymbol);
|
|
|
|
ID.Add(SFC);
|
|
|
|
}
|
|
|
|
|
2019-08-14 00:45:48 +08:00
|
|
|
PathDiagnosticPieceRef VisitNode(const ExplodedNode *Succ,
|
|
|
|
BugReporterContext &BRC,
|
2019-09-10 04:34:40 +08:00
|
|
|
PathSensitiveBugReport &BR) override;
|
2014-07-11 00:10:52 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
class TestAfterDivZeroChecker
|
|
|
|
: public Checker<check::PreStmt<BinaryOperator>, check::BranchCondition,
|
|
|
|
check::EndFunction> {
|
|
|
|
mutable std::unique_ptr<BuiltinBug> DivZeroBug;
|
|
|
|
void reportBug(SVal Val, CheckerContext &C) const;
|
|
|
|
|
|
|
|
public:
|
|
|
|
void checkPreStmt(const BinaryOperator *B, CheckerContext &C) const;
|
|
|
|
void checkBranchCondition(const Stmt *Condition, CheckerContext &C) const;
|
2018-07-17 04:47:45 +08:00
|
|
|
void checkEndFunction(const ReturnStmt *RS, CheckerContext &C) const;
|
2014-07-11 00:10:52 +08:00
|
|
|
void setDivZeroMap(SVal Var, CheckerContext &C) const;
|
|
|
|
bool hasDivZeroMap(SVal Var, const CheckerContext &C) const;
|
|
|
|
bool isZero(SVal S, CheckerContext &C) const;
|
|
|
|
};
|
|
|
|
} // end anonymous namespace
|
|
|
|
|
|
|
|
REGISTER_SET_WITH_PROGRAMSTATE(DivZeroMap, ZeroState)
|
|
|
|
|
2019-09-10 04:34:40 +08:00
|
|
|
PathDiagnosticPieceRef
|
|
|
|
DivisionBRVisitor::VisitNode(const ExplodedNode *Succ, BugReporterContext &BRC,
|
|
|
|
PathSensitiveBugReport &BR) {
|
2014-07-11 00:10:52 +08:00
|
|
|
if (Satisfied)
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
const Expr *E = nullptr;
|
|
|
|
|
|
|
|
if (Optional<PostStmt> P = Succ->getLocationAs<PostStmt>())
|
|
|
|
if (const BinaryOperator *BO = P->getStmtAs<BinaryOperator>()) {
|
|
|
|
BinaryOperator::Opcode Op = BO->getOpcode();
|
|
|
|
if (Op == BO_Div || Op == BO_Rem || Op == BO_DivAssign ||
|
|
|
|
Op == BO_RemAssign) {
|
|
|
|
E = BO->getRHS();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!E)
|
|
|
|
return nullptr;
|
|
|
|
|
2018-01-18 04:27:29 +08:00
|
|
|
SVal S = Succ->getSVal(E);
|
2014-07-11 00:10:52 +08:00
|
|
|
if (ZeroSymbol == S.getAsSymbol() && SFC == Succ->getStackFrame()) {
|
|
|
|
Satisfied = true;
|
|
|
|
|
|
|
|
// Construct a new PathDiagnosticPiece.
|
|
|
|
ProgramPoint P = Succ->getLocation();
|
|
|
|
PathDiagnosticLocation L =
|
|
|
|
PathDiagnosticLocation::create(P, BRC.getSourceManager());
|
|
|
|
|
|
|
|
if (!L.isValid() || !L.asLocation().isValid())
|
|
|
|
return nullptr;
|
|
|
|
|
2017-01-06 01:26:53 +08:00
|
|
|
return std::make_shared<PathDiagnosticEventPiece>(
|
2014-07-11 00:10:52 +08:00
|
|
|
L, "Division with compared value made here");
|
|
|
|
}
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool TestAfterDivZeroChecker::isZero(SVal S, CheckerContext &C) const {
|
|
|
|
Optional<DefinedSVal> DSV = S.getAs<DefinedSVal>();
|
|
|
|
|
|
|
|
if (!DSV)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
ConstraintManager &CM = C.getConstraintManager();
|
|
|
|
return !CM.assume(C.getState(), *DSV, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
void TestAfterDivZeroChecker::setDivZeroMap(SVal Var, CheckerContext &C) const {
|
|
|
|
SymbolRef SR = Var.getAsSymbol();
|
|
|
|
if (!SR)
|
|
|
|
return;
|
|
|
|
|
|
|
|
ProgramStateRef State = C.getState();
|
|
|
|
State =
|
|
|
|
State->add<DivZeroMap>(ZeroState(SR, C.getBlockID(), C.getStackFrame()));
|
|
|
|
C.addTransition(State);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool TestAfterDivZeroChecker::hasDivZeroMap(SVal Var,
|
|
|
|
const CheckerContext &C) const {
|
|
|
|
SymbolRef SR = Var.getAsSymbol();
|
|
|
|
if (!SR)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
ZeroState ZS(SR, C.getBlockID(), C.getStackFrame());
|
|
|
|
return C.getState()->contains<DivZeroMap>(ZS);
|
|
|
|
}
|
|
|
|
|
|
|
|
void TestAfterDivZeroChecker::reportBug(SVal Val, CheckerContext &C) const {
|
2015-09-17 06:03:05 +08:00
|
|
|
if (ExplodedNode *N = C.generateErrorNode(C.getState())) {
|
2014-07-11 00:10:52 +08:00
|
|
|
if (!DivZeroBug)
|
|
|
|
DivZeroBug.reset(new BuiltinBug(this, "Division by zero"));
|
|
|
|
|
2019-09-10 04:34:40 +08:00
|
|
|
auto R = std::make_unique<PathSensitiveBugReport>(
|
2015-06-23 21:15:32 +08:00
|
|
|
*DivZeroBug, "Value being compared against zero has already been used "
|
|
|
|
"for division",
|
|
|
|
N);
|
2014-07-11 00:10:52 +08:00
|
|
|
|
2019-08-15 07:04:18 +08:00
|
|
|
R->addVisitor(std::make_unique<DivisionBRVisitor>(Val.getAsSymbol(),
|
2014-09-05 07:54:33 +08:00
|
|
|
C.getStackFrame()));
|
2015-06-23 21:15:32 +08:00
|
|
|
C.emitReport(std::move(R));
|
2014-07-11 00:10:52 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-29 02:49:41 +08:00
|
|
|
void TestAfterDivZeroChecker::checkEndFunction(const ReturnStmt *,
|
2018-07-17 04:47:45 +08:00
|
|
|
CheckerContext &C) const {
|
2014-07-11 00:10:52 +08:00
|
|
|
ProgramStateRef State = C.getState();
|
|
|
|
|
|
|
|
DivZeroMapTy DivZeroes = State->get<DivZeroMap>();
|
|
|
|
if (DivZeroes.isEmpty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
DivZeroMapTy::Factory &F = State->get_context<DivZeroMap>();
|
|
|
|
for (llvm::ImmutableSet<ZeroState>::iterator I = DivZeroes.begin(),
|
|
|
|
E = DivZeroes.end();
|
|
|
|
I != E; ++I) {
|
|
|
|
ZeroState ZS = *I;
|
|
|
|
if (ZS.getStackFrameContext() == C.getStackFrame())
|
|
|
|
DivZeroes = F.remove(DivZeroes, ZS);
|
|
|
|
}
|
|
|
|
C.addTransition(State->set<DivZeroMap>(DivZeroes));
|
|
|
|
}
|
|
|
|
|
|
|
|
void TestAfterDivZeroChecker::checkPreStmt(const BinaryOperator *B,
|
|
|
|
CheckerContext &C) const {
|
|
|
|
BinaryOperator::Opcode Op = B->getOpcode();
|
|
|
|
if (Op == BO_Div || Op == BO_Rem || Op == BO_DivAssign ||
|
|
|
|
Op == BO_RemAssign) {
|
|
|
|
SVal S = C.getSVal(B->getRHS());
|
|
|
|
|
|
|
|
if (!isZero(S, C))
|
|
|
|
setDivZeroMap(S, C);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void TestAfterDivZeroChecker::checkBranchCondition(const Stmt *Condition,
|
|
|
|
CheckerContext &C) const {
|
|
|
|
if (const BinaryOperator *B = dyn_cast<BinaryOperator>(Condition)) {
|
|
|
|
if (B->isComparisonOp()) {
|
|
|
|
const IntegerLiteral *IntLiteral = dyn_cast<IntegerLiteral>(B->getRHS());
|
|
|
|
bool LRHS = true;
|
|
|
|
if (!IntLiteral) {
|
|
|
|
IntLiteral = dyn_cast<IntegerLiteral>(B->getLHS());
|
|
|
|
LRHS = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!IntLiteral || IntLiteral->getValue() != 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
SVal Val = C.getSVal(LRHS ? B->getLHS() : B->getRHS());
|
|
|
|
if (hasDivZeroMap(Val, C))
|
|
|
|
reportBug(Val, C);
|
|
|
|
}
|
|
|
|
} else if (const UnaryOperator *U = dyn_cast<UnaryOperator>(Condition)) {
|
|
|
|
if (U->getOpcode() == UO_LNot) {
|
|
|
|
SVal Val;
|
|
|
|
if (const ImplicitCastExpr *I =
|
|
|
|
dyn_cast<ImplicitCastExpr>(U->getSubExpr()))
|
|
|
|
Val = C.getSVal(I->getSubExpr());
|
|
|
|
|
|
|
|
if (hasDivZeroMap(Val, C))
|
|
|
|
reportBug(Val, C);
|
|
|
|
else {
|
|
|
|
Val = C.getSVal(U->getSubExpr());
|
|
|
|
if (hasDivZeroMap(Val, C))
|
|
|
|
reportBug(Val, C);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (const ImplicitCastExpr *IE =
|
|
|
|
dyn_cast<ImplicitCastExpr>(Condition)) {
|
|
|
|
SVal Val = C.getSVal(IE->getSubExpr());
|
|
|
|
|
|
|
|
if (hasDivZeroMap(Val, C))
|
|
|
|
reportBug(Val, C);
|
|
|
|
else {
|
|
|
|
SVal Val = C.getSVal(Condition);
|
|
|
|
|
|
|
|
if (hasDivZeroMap(Val, C))
|
|
|
|
reportBug(Val, C);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ento::registerTestAfterDivZeroChecker(CheckerManager &mgr) {
|
|
|
|
mgr.registerChecker<TestAfterDivZeroChecker>();
|
|
|
|
}
|
2019-01-26 22:23:08 +08:00
|
|
|
|
2020-03-27 21:29:31 +08:00
|
|
|
bool ento::shouldRegisterTestAfterDivZeroChecker(const CheckerManager &mgr) {
|
2019-01-26 22:23:08 +08:00
|
|
|
return true;
|
|
|
|
}
|