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 std::shared_ptr<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 std::shared_ptr<PathDiagnosticPiece>
98 DivisionBRVisitor::VisitNode(const ExplodedNode *Succ, const ExplodedNode *Pred,
99 BugReporterContext &BRC, BugReport &BR) {
103 const Expr *E = nullptr;
105 if (Optional<PostStmt> P = Succ->getLocationAs<PostStmt>())
106 if (const BinaryOperator *BO = P->getStmtAs<BinaryOperator>()) {
107 BinaryOperator::Opcode Op = BO->getOpcode();
108 if (Op == BO_Div || Op == BO_Rem || Op == BO_DivAssign ||
109 Op == BO_RemAssign) {
117 ProgramStateRef State = Succ->getState();
118 SVal S = State->getSVal(E, Succ->getLocationContext());
119 if (ZeroSymbol == S.getAsSymbol() && SFC == Succ->getStackFrame()) {
122 // Construct a new PathDiagnosticPiece.
123 ProgramPoint P = Succ->getLocation();
124 PathDiagnosticLocation L =
125 PathDiagnosticLocation::create(P, BRC.getSourceManager());
127 if (!L.isValid() || !L.asLocation().isValid())
130 return std::make_shared<PathDiagnosticEventPiece>(
131 L, "Division with compared value made here");
137 bool TestAfterDivZeroChecker::isZero(SVal S, CheckerContext &C) const {
138 Optional<DefinedSVal> DSV = S.getAs<DefinedSVal>();
143 ConstraintManager &CM = C.getConstraintManager();
144 return !CM.assume(C.getState(), *DSV, true);
147 void TestAfterDivZeroChecker::setDivZeroMap(SVal Var, CheckerContext &C) const {
148 SymbolRef SR = Var.getAsSymbol();
152 ProgramStateRef State = C.getState();
154 State->add<DivZeroMap>(ZeroState(SR, C.getBlockID(), C.getStackFrame()));
155 C.addTransition(State);
158 bool TestAfterDivZeroChecker::hasDivZeroMap(SVal Var,
159 const CheckerContext &C) const {
160 SymbolRef SR = Var.getAsSymbol();
164 ZeroState ZS(SR, C.getBlockID(), C.getStackFrame());
165 return C.getState()->contains<DivZeroMap>(ZS);
168 void TestAfterDivZeroChecker::reportBug(SVal Val, CheckerContext &C) const {
169 if (ExplodedNode *N = C.generateErrorNode(C.getState())) {
171 DivZeroBug.reset(new BuiltinBug(this, "Division by zero"));
173 auto R = llvm::make_unique<BugReport>(
174 *DivZeroBug, "Value being compared against zero has already been used "
178 R->addVisitor(llvm::make_unique<DivisionBRVisitor>(Val.getAsSymbol(),
180 C.emitReport(std::move(R));
184 void TestAfterDivZeroChecker::checkEndFunction(CheckerContext &C) const {
185 ProgramStateRef State = C.getState();
187 DivZeroMapTy DivZeroes = State->get<DivZeroMap>();
188 if (DivZeroes.isEmpty())
191 DivZeroMapTy::Factory &F = State->get_context<DivZeroMap>();
192 for (llvm::ImmutableSet<ZeroState>::iterator I = DivZeroes.begin(),
196 if (ZS.getStackFrameContext() == C.getStackFrame())
197 DivZeroes = F.remove(DivZeroes, ZS);
199 C.addTransition(State->set<DivZeroMap>(DivZeroes));
202 void TestAfterDivZeroChecker::checkPreStmt(const BinaryOperator *B,
203 CheckerContext &C) const {
204 BinaryOperator::Opcode Op = B->getOpcode();
205 if (Op == BO_Div || Op == BO_Rem || Op == BO_DivAssign ||
206 Op == BO_RemAssign) {
207 SVal S = C.getSVal(B->getRHS());
214 void TestAfterDivZeroChecker::checkBranchCondition(const Stmt *Condition,
215 CheckerContext &C) const {
216 if (const BinaryOperator *B = dyn_cast<BinaryOperator>(Condition)) {
217 if (B->isComparisonOp()) {
218 const IntegerLiteral *IntLiteral = dyn_cast<IntegerLiteral>(B->getRHS());
221 IntLiteral = dyn_cast<IntegerLiteral>(B->getLHS());
225 if (!IntLiteral || IntLiteral->getValue() != 0)
228 SVal Val = C.getSVal(LRHS ? B->getLHS() : B->getRHS());
229 if (hasDivZeroMap(Val, C))
232 } else if (const UnaryOperator *U = dyn_cast<UnaryOperator>(Condition)) {
233 if (U->getOpcode() == UO_LNot) {
235 if (const ImplicitCastExpr *I =
236 dyn_cast<ImplicitCastExpr>(U->getSubExpr()))
237 Val = C.getSVal(I->getSubExpr());
239 if (hasDivZeroMap(Val, C))
242 Val = C.getSVal(U->getSubExpr());
243 if (hasDivZeroMap(Val, C))
247 } else if (const ImplicitCastExpr *IE =
248 dyn_cast<ImplicitCastExpr>(Condition)) {
249 SVal Val = C.getSVal(IE->getSubExpr());
251 if (hasDivZeroMap(Val, C))
254 SVal Val = C.getSVal(Condition);
256 if (hasDivZeroMap(Val, C))
262 void ento::registerTestAfterDivZeroChecker(CheckerManager &mgr) {
263 mgr.registerChecker<TestAfterDivZeroChecker>();