You've already forked linux-packaging-mono
acceptance-tests
data
debian
docs
external
Newtonsoft.Json
api-doc-tools
api-snapshot
aspnetwebstack
bdwgc
binary-reference-assemblies
bockbuild
boringssl
cecil
cecil-legacy
corefx
corert
helix-binaries
ikdasm
ikvm
illinker-test-assets
linker
llvm-project
clang
INPUTS
bindings
cmake
docs
examples
include
lib
ARCMigrate
AST
ASTMatchers
Analysis
Basic
CodeGen
CrossTU
Driver
Edit
Format
Frontend
FrontendTool
Headers
Index
Lex
Parse
Rewrite
Sema
Serialization
StaticAnalyzer
Checkers
MPI-Checker
AllocationDiagnostics.cpp
AllocationDiagnostics.h
AnalysisOrderChecker.cpp
AnalyzerStatsChecker.cpp
ArrayBoundChecker.cpp
ArrayBoundCheckerV2.cpp
BasicObjCFoundationChecks.cpp
BlockInCriticalSectionChecker.cpp
BoolAssignmentChecker.cpp
BuiltinFunctionChecker.cpp
CMakeLists.txt
CStringChecker.cpp
CStringSyntaxChecker.cpp
CXXSelfAssignmentChecker.cpp
CallAndMessageChecker.cpp
CastSizeChecker.cpp
CastToStructChecker.cpp
CheckObjCDealloc.cpp
CheckObjCInstMethSignature.cpp
CheckSecuritySyntaxOnly.cpp
CheckSizeofPointer.cpp
CheckerDocumentation.cpp
ChrootChecker.cpp
ClangCheckers.cpp
ClangSACheckers.h
CloneChecker.cpp
ConversionChecker.cpp
DeadStoresChecker.cpp
DebugCheckers.cpp
DeleteWithNonVirtualDtorChecker.cpp
DereferenceChecker.cpp
DirectIvarAssignment.cpp
DivZeroChecker.cpp
DynamicTypeChecker.cpp
DynamicTypePropagation.cpp
ExprInspectionChecker.cpp
FixedAddressChecker.cpp
GTestChecker.cpp
GenericTaintChecker.cpp
IdenticalExprChecker.cpp
InterCheckerAPI.h
IteratorChecker.cpp
IvarInvalidationChecker.cpp
LLVMConventionsChecker.cpp
LocalizationChecker.cpp
MacOSKeychainAPIChecker.cpp
MacOSXAPIChecker.cpp
MallocChecker.cpp.REMOVED.git-id
MallocOverflowSecurityChecker.cpp
MallocSizeofChecker.cpp
MisusedMovedObjectChecker.cpp
NSAutoreleasePoolChecker.cpp
NSErrorChecker.cpp
NoReturnFunctionChecker.cpp
NonNullParamChecker.cpp
NonnullGlobalConstantsChecker.cpp
NullabilityChecker.cpp
NumberObjectConversionChecker.cpp
ObjCAtSyncChecker.cpp
ObjCContainersASTChecker.cpp
ObjCContainersChecker.cpp
ObjCMissingSuperCallChecker.cpp
ObjCPropertyChecker.cpp
ObjCSelfInitChecker.cpp
ObjCSuperDeallocChecker.cpp
ObjCUnusedIVarsChecker.cpp
PaddingChecker.cpp
PointerArithChecker.cpp
PointerSubChecker.cpp
PthreadLockChecker.cpp
RetainCountChecker.cpp.REMOVED.git-id
ReturnPointerRangeChecker.cpp
ReturnUndefChecker.cpp
SelectorExtras.h
SimpleStreamChecker.cpp
StackAddrEscapeChecker.cpp
StdLibraryFunctionsChecker.cpp
StreamChecker.cpp
TaintTesterChecker.cpp
TestAfterDivZeroChecker.cpp
TraversalChecker.cpp
UndefBranchChecker.cpp
UndefCapturedBlockVarChecker.cpp
UndefResultChecker.cpp
UndefinedArraySubscriptChecker.cpp
UndefinedAssignmentChecker.cpp
UnixAPIChecker.cpp
UnreachableCodeChecker.cpp
VLASizeChecker.cpp
ValistChecker.cpp
VforkChecker.cpp
VirtualCallChecker.cpp
Core
Frontend
CMakeLists.txt
README.txt
Tooling
CMakeLists.txt
runtime
tools
unittests
utils
www
.arcconfig
.clang-format
.clang-tidy
.gitignore
CMakeLists.txt
CODE_OWNERS.TXT
INSTALL.txt
LICENSE.TXT
ModuleInfo.txt
NOTES.txt
README.txt
clang-tools-extra
compiler-rt
eng
libcxx
libcxxabi
libunwind
lld
lldb
llvm
nuget
openmp
polly
Directory.Build.props
Directory.Build.targets
NuGet.config
azure-pipelines.yml
build.cmd
build.sh
dir.common.props
global.json
llvm.proj
mxe-Win64.cmake.in
nuget-buildtasks
nunit-lite
roslyn-binaries
rx
xunit-binaries
how-to-bump-roslyn-binaries.md
ikvm-native
llvm
m4
man
mcs
mono
msvc
netcore
po
runtime
samples
scripts
support
tools
COPYING.LIB
LICENSE
Makefile.am
Makefile.in
NEWS
README.md
acinclude.m4
aclocal.m4
autogen.sh
code_of_conduct.md
compile
config.guess
config.h.in
config.rpath
config.sub
configure.REMOVED.git-id
configure.ac.REMOVED.git-id
depcomp
install-sh
ltmain.sh.REMOVED.git-id
missing
mkinstalldirs
mono-uninstalled.pc.in
test-driver
winconfig.h
265 lines
8.2 KiB
C++
265 lines
8.2 KiB
C++
![]() |
//== TestAfterDivZeroChecker.cpp - Test after division by zero checker --*--==//
|
||
|
//
|
||
|
// The LLVM Compiler Infrastructure
|
||
|
//
|
||
|
// This file is distributed under the University of Illinois Open Source
|
||
|
// License. See LICENSE.TXT for details.
|
||
|
//
|
||
|
//===----------------------------------------------------------------------===//
|
||
|
//
|
||
|
// This defines TestAfterDivZeroChecker, a builtin check that performs checks
|
||
|
// for division by zero where the division occurs before comparison with zero.
|
||
|
//
|
||
|
//===----------------------------------------------------------------------===//
|
||
|
|
||
|
#include "ClangSACheckers.h"
|
||
|
#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);
|
||
|
}
|
||
|
};
|
||
|
|
||
|
class DivisionBRVisitor : public BugReporterVisitorImpl<DivisionBRVisitor> {
|
||
|
private:
|
||
|
SymbolRef ZeroSymbol;
|
||
|
const StackFrameContext *SFC;
|
||
|
bool Satisfied;
|
||
|
|
||
|
public:
|
||
|
DivisionBRVisitor(SymbolRef ZeroSymbol, const StackFrameContext *SFC)
|
||
|
: ZeroSymbol(ZeroSymbol), SFC(SFC), Satisfied(false) {}
|
||
|
|
||
|
void Profile(llvm::FoldingSetNodeID &ID) const override {
|
||
|
ID.Add(ZeroSymbol);
|
||
|
ID.Add(SFC);
|
||
|
}
|
||
|
|
||
|
std::shared_ptr<PathDiagnosticPiece> VisitNode(const ExplodedNode *Succ,
|
||
|
const ExplodedNode *Pred,
|
||
|
BugReporterContext &BRC,
|
||
|
BugReport &BR) override;
|
||
|
};
|
||
|
|
||
|
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;
|
||
|
void checkEndFunction(CheckerContext &C) const;
|
||
|
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)
|
||
|
|
||
|
std::shared_ptr<PathDiagnosticPiece>
|
||
|
DivisionBRVisitor::VisitNode(const ExplodedNode *Succ, const ExplodedNode *Pred,
|
||
|
BugReporterContext &BRC, BugReport &BR) {
|
||
|
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;
|
||
|
|
||
|
ProgramStateRef State = Succ->getState();
|
||
|
SVal S = State->getSVal(E, Succ->getLocationContext());
|
||
|
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;
|
||
|
|
||
|
return std::make_shared<PathDiagnosticEventPiece>(
|
||
|
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 {
|
||
|
if (ExplodedNode *N = C.generateErrorNode(C.getState())) {
|
||
|
if (!DivZeroBug)
|
||
|
DivZeroBug.reset(new BuiltinBug(this, "Division by zero"));
|
||
|
|
||
|
auto R = llvm::make_unique<BugReport>(
|
||
|
*DivZeroBug, "Value being compared against zero has already been used "
|
||
|
"for division",
|
||
|
N);
|
||
|
|
||
|
R->addVisitor(llvm::make_unique<DivisionBRVisitor>(Val.getAsSymbol(),
|
||
|
C.getStackFrame()));
|
||
|
C.emitReport(std::move(R));
|
||
|
}
|
||
|
}
|
||
|
|
||
|
void TestAfterDivZeroChecker::checkEndFunction(CheckerContext &C) const {
|
||
|
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>();
|
||
|
}
|