1 //== TestAfterDivZeroChecker.cpp - Test after division by zero checker --*--==//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This defines TestAfterDivZeroChecker, a builtin check that performs checks
11 // for division by zero where the division occurs before comparison with zero.
13 //===----------------------------------------------------------------------===//
15 #include "ClangSACheckers.h"
16 #include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
17 #include "clang/StaticAnalyzer/Core/Checker.h"
18 #include "clang/StaticAnalyzer/Core/PathSensitive/CallEvent.h"
19 #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
20 #include "llvm/ADT/FoldingSet.h"
22 using namespace clang;
31 const StackFrameContext *SFC;
34 ZeroState(SymbolRef S, unsigned B, const StackFrameContext *SFC)
35 : ZeroSymbol(S), BlockID(B), SFC(SFC) {}
37 const StackFrameContext *getStackFrameContext() const { return SFC; }
39 bool operator==(const ZeroState &X) const {
40 return BlockID == X.BlockID && SFC == X.SFC && ZeroSymbol == X.ZeroSymbol;
43 bool operator<(const ZeroState &X) const {
44 if (BlockID != X.BlockID)
45 return BlockID < X.BlockID;
48 return ZeroSymbol < X.ZeroSymbol;
51 void Profile(llvm::FoldingSetNodeID &ID) const {
52 ID.AddInteger(BlockID);
54 ID.AddPointer(ZeroSymbol);
58 class DivisionBRVisitor : public BugReporterVisitorImpl<DivisionBRVisitor> {
61 const StackFrameContext *SFC;
65 DivisionBRVisitor(SymbolRef ZeroSymbol, const StackFrameContext *SFC)
66 : ZeroSymbol(ZeroSymbol), SFC(SFC), Satisfied(false) {}
68 void Profile(llvm::FoldingSetNodeID &ID) const override {
73 PathDiagnosticPiece *VisitNode(const ExplodedNode *Succ,
74 const ExplodedNode *Pred,
75 BugReporterContext &BRC,
76 BugReport &BR) override;
79 class TestAfterDivZeroChecker
80 : public Checker<check::PreStmt<BinaryOperator>, check::BranchCondition,
82 mutable std::unique_ptr<BuiltinBug> DivZeroBug;
83 void reportBug(SVal Val, CheckerContext &C) const;
86 void checkPreStmt(const BinaryOperator *B, CheckerContext &C) const;
87 void checkBranchCondition(const Stmt *Condition, CheckerContext &C) const;
88 void checkEndFunction(CheckerContext &C) const;
89 void setDivZeroMap(SVal Var, CheckerContext &C) const;
90 bool hasDivZeroMap(SVal Var, const CheckerContext &C) const;
91 bool isZero(SVal S, CheckerContext &C) const;
93 } // end anonymous namespace
95 REGISTER_SET_WITH_PROGRAMSTATE(DivZeroMap, ZeroState)
97 PathDiagnosticPiece *DivisionBRVisitor::VisitNode(const ExplodedNode *Succ,
98 const ExplodedNode *Pred,
99 BugReporterContext &BRC,
104 const Expr *E = nullptr;
106 if (Optional<PostStmt> P = Succ->getLocationAs<PostStmt>())
107 if (const BinaryOperator *BO = P->getStmtAs<BinaryOperator>()) {
108 BinaryOperator::Opcode Op = BO->getOpcode();
109 if (Op == BO_Div || Op == BO_Rem || Op == BO_DivAssign ||
110 Op == BO_RemAssign) {
118 ProgramStateRef State = Succ->getState();
119 SVal S = State->getSVal(E, Succ->getLocationContext());
120 if (ZeroSymbol == S.getAsSymbol() && SFC == Succ->getStackFrame()) {
123 // Construct a new PathDiagnosticPiece.
124 ProgramPoint P = Succ->getLocation();
125 PathDiagnosticLocation L =
126 PathDiagnosticLocation::create(P, BRC.getSourceManager());
128 if (!L.isValid() || !L.asLocation().isValid())
131 return new PathDiagnosticEventPiece(
132 L, "Division with compared value made here");
138 bool TestAfterDivZeroChecker::isZero(SVal S, CheckerContext &C) const {
139 Optional<DefinedSVal> DSV = S.getAs<DefinedSVal>();
144 ConstraintManager &CM = C.getConstraintManager();
145 return !CM.assume(C.getState(), *DSV, true);
148 void TestAfterDivZeroChecker::setDivZeroMap(SVal Var, CheckerContext &C) const {
149 SymbolRef SR = Var.getAsSymbol();
153 ProgramStateRef State = C.getState();
155 State->add<DivZeroMap>(ZeroState(SR, C.getBlockID(), C.getStackFrame()));
156 C.addTransition(State);
159 bool TestAfterDivZeroChecker::hasDivZeroMap(SVal Var,
160 const CheckerContext &C) const {
161 SymbolRef SR = Var.getAsSymbol();
165 ZeroState ZS(SR, C.getBlockID(), C.getStackFrame());
166 return C.getState()->contains<DivZeroMap>(ZS);
169 void TestAfterDivZeroChecker::reportBug(SVal Val, CheckerContext &C) const {
170 if (ExplodedNode *N = C.generateErrorNode(C.getState())) {
172 DivZeroBug.reset(new BuiltinBug(this, "Division by zero"));
174 auto R = llvm::make_unique<BugReport>(
175 *DivZeroBug, "Value being compared against zero has already been used "
179 R->addVisitor(llvm::make_unique<DivisionBRVisitor>(Val.getAsSymbol(),
181 C.emitReport(std::move(R));
185 void TestAfterDivZeroChecker::checkEndFunction(CheckerContext &C) const {
186 ProgramStateRef State = C.getState();
188 DivZeroMapTy DivZeroes = State->get<DivZeroMap>();
189 if (DivZeroes.isEmpty())
192 DivZeroMapTy::Factory &F = State->get_context<DivZeroMap>();
193 for (llvm::ImmutableSet<ZeroState>::iterator I = DivZeroes.begin(),
197 if (ZS.getStackFrameContext() == C.getStackFrame())
198 DivZeroes = F.remove(DivZeroes, ZS);
200 C.addTransition(State->set<DivZeroMap>(DivZeroes));
203 void TestAfterDivZeroChecker::checkPreStmt(const BinaryOperator *B,
204 CheckerContext &C) const {
205 BinaryOperator::Opcode Op = B->getOpcode();
206 if (Op == BO_Div || Op == BO_Rem || Op == BO_DivAssign ||
207 Op == BO_RemAssign) {
208 SVal S = C.getSVal(B->getRHS());
215 void TestAfterDivZeroChecker::checkBranchCondition(const Stmt *Condition,
216 CheckerContext &C) const {
217 if (const BinaryOperator *B = dyn_cast<BinaryOperator>(Condition)) {
218 if (B->isComparisonOp()) {
219 const IntegerLiteral *IntLiteral = dyn_cast<IntegerLiteral>(B->getRHS());
222 IntLiteral = dyn_cast<IntegerLiteral>(B->getLHS());
226 if (!IntLiteral || IntLiteral->getValue() != 0)
229 SVal Val = C.getSVal(LRHS ? B->getLHS() : B->getRHS());
230 if (hasDivZeroMap(Val, C))
233 } else if (const UnaryOperator *U = dyn_cast<UnaryOperator>(Condition)) {
234 if (U->getOpcode() == UO_LNot) {
236 if (const ImplicitCastExpr *I =
237 dyn_cast<ImplicitCastExpr>(U->getSubExpr()))
238 Val = C.getSVal(I->getSubExpr());
240 if (hasDivZeroMap(Val, C))
243 Val = C.getSVal(U->getSubExpr());
244 if (hasDivZeroMap(Val, C))
248 } else if (const ImplicitCastExpr *IE =
249 dyn_cast<ImplicitCastExpr>(Condition)) {
250 SVal Val = C.getSVal(IE->getSubExpr());
252 if (hasDivZeroMap(Val, C))
255 SVal Val = C.getSVal(Condition);
257 if (hasDivZeroMap(Val, C))
263 void ento::registerTestAfterDivZeroChecker(CheckerManager &mgr) {
264 mgr.registerChecker<TestAfterDivZeroChecker>();