1 //== TestAfterDivZeroChecker.cpp - Test after division by zero checker --*--==//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // This defines TestAfterDivZeroChecker, a builtin check that performs checks
10 // for division by zero where the division occurs before comparison with zero.
12 //===----------------------------------------------------------------------===//
14 #include "clang/StaticAnalyzer/Checkers/BuiltinCheckerRegistration.h"
15 #include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
16 #include "clang/StaticAnalyzer/Core/Checker.h"
17 #include "clang/StaticAnalyzer/Core/PathSensitive/CallEvent.h"
18 #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
19 #include "llvm/ADT/FoldingSet.h"
21 using namespace clang;
30 const StackFrameContext *SFC;
33 ZeroState(SymbolRef S, unsigned B, const StackFrameContext *SFC)
34 : ZeroSymbol(S), BlockID(B), SFC(SFC) {}
36 const StackFrameContext *getStackFrameContext() const { return SFC; }
38 bool operator==(const ZeroState &X) const {
39 return BlockID == X.BlockID && SFC == X.SFC && ZeroSymbol == X.ZeroSymbol;
42 bool operator<(const ZeroState &X) const {
43 if (BlockID != X.BlockID)
44 return BlockID < X.BlockID;
47 return ZeroSymbol < X.ZeroSymbol;
50 void Profile(llvm::FoldingSetNodeID &ID) const {
51 ID.AddInteger(BlockID);
53 ID.AddPointer(ZeroSymbol);
57 class DivisionBRVisitor : public BugReporterVisitor {
60 const StackFrameContext *SFC;
64 DivisionBRVisitor(SymbolRef ZeroSymbol, const StackFrameContext *SFC)
65 : ZeroSymbol(ZeroSymbol), SFC(SFC), Satisfied(false) {}
67 void Profile(llvm::FoldingSetNodeID &ID) const override {
72 std::shared_ptr<PathDiagnosticPiece> VisitNode(const ExplodedNode *Succ,
73 BugReporterContext &BRC,
74 BugReport &BR) override;
77 class TestAfterDivZeroChecker
78 : public Checker<check::PreStmt<BinaryOperator>, check::BranchCondition,
80 mutable std::unique_ptr<BuiltinBug> DivZeroBug;
81 void reportBug(SVal Val, CheckerContext &C) const;
84 void checkPreStmt(const BinaryOperator *B, CheckerContext &C) const;
85 void checkBranchCondition(const Stmt *Condition, CheckerContext &C) const;
86 void checkEndFunction(const ReturnStmt *RS, CheckerContext &C) const;
87 void setDivZeroMap(SVal Var, CheckerContext &C) const;
88 bool hasDivZeroMap(SVal Var, const CheckerContext &C) const;
89 bool isZero(SVal S, CheckerContext &C) const;
91 } // end anonymous namespace
93 REGISTER_SET_WITH_PROGRAMSTATE(DivZeroMap, ZeroState)
95 std::shared_ptr<PathDiagnosticPiece>
96 DivisionBRVisitor::VisitNode(const ExplodedNode *Succ,
97 BugReporterContext &BRC, BugReport &BR) {
101 const Expr *E = nullptr;
103 if (Optional<PostStmt> P = Succ->getLocationAs<PostStmt>())
104 if (const BinaryOperator *BO = P->getStmtAs<BinaryOperator>()) {
105 BinaryOperator::Opcode Op = BO->getOpcode();
106 if (Op == BO_Div || Op == BO_Rem || Op == BO_DivAssign ||
107 Op == BO_RemAssign) {
115 SVal S = Succ->getSVal(E);
116 if (ZeroSymbol == S.getAsSymbol() && SFC == Succ->getStackFrame()) {
119 // Construct a new PathDiagnosticPiece.
120 ProgramPoint P = Succ->getLocation();
121 PathDiagnosticLocation L =
122 PathDiagnosticLocation::create(P, BRC.getSourceManager());
124 if (!L.isValid() || !L.asLocation().isValid())
127 return std::make_shared<PathDiagnosticEventPiece>(
128 L, "Division with compared value made here");
134 bool TestAfterDivZeroChecker::isZero(SVal S, CheckerContext &C) const {
135 Optional<DefinedSVal> DSV = S.getAs<DefinedSVal>();
140 ConstraintManager &CM = C.getConstraintManager();
141 return !CM.assume(C.getState(), *DSV, true);
144 void TestAfterDivZeroChecker::setDivZeroMap(SVal Var, CheckerContext &C) const {
145 SymbolRef SR = Var.getAsSymbol();
149 ProgramStateRef State = C.getState();
151 State->add<DivZeroMap>(ZeroState(SR, C.getBlockID(), C.getStackFrame()));
152 C.addTransition(State);
155 bool TestAfterDivZeroChecker::hasDivZeroMap(SVal Var,
156 const CheckerContext &C) const {
157 SymbolRef SR = Var.getAsSymbol();
161 ZeroState ZS(SR, C.getBlockID(), C.getStackFrame());
162 return C.getState()->contains<DivZeroMap>(ZS);
165 void TestAfterDivZeroChecker::reportBug(SVal Val, CheckerContext &C) const {
166 if (ExplodedNode *N = C.generateErrorNode(C.getState())) {
168 DivZeroBug.reset(new BuiltinBug(this, "Division by zero"));
170 auto R = llvm::make_unique<BugReport>(
171 *DivZeroBug, "Value being compared against zero has already been used "
175 R->addVisitor(llvm::make_unique<DivisionBRVisitor>(Val.getAsSymbol(),
177 C.emitReport(std::move(R));
181 void TestAfterDivZeroChecker::checkEndFunction(const ReturnStmt *,
182 CheckerContext &C) const {
183 ProgramStateRef State = C.getState();
185 DivZeroMapTy DivZeroes = State->get<DivZeroMap>();
186 if (DivZeroes.isEmpty())
189 DivZeroMapTy::Factory &F = State->get_context<DivZeroMap>();
190 for (llvm::ImmutableSet<ZeroState>::iterator I = DivZeroes.begin(),
194 if (ZS.getStackFrameContext() == C.getStackFrame())
195 DivZeroes = F.remove(DivZeroes, ZS);
197 C.addTransition(State->set<DivZeroMap>(DivZeroes));
200 void TestAfterDivZeroChecker::checkPreStmt(const BinaryOperator *B,
201 CheckerContext &C) const {
202 BinaryOperator::Opcode Op = B->getOpcode();
203 if (Op == BO_Div || Op == BO_Rem || Op == BO_DivAssign ||
204 Op == BO_RemAssign) {
205 SVal S = C.getSVal(B->getRHS());
212 void TestAfterDivZeroChecker::checkBranchCondition(const Stmt *Condition,
213 CheckerContext &C) const {
214 if (const BinaryOperator *B = dyn_cast<BinaryOperator>(Condition)) {
215 if (B->isComparisonOp()) {
216 const IntegerLiteral *IntLiteral = dyn_cast<IntegerLiteral>(B->getRHS());
219 IntLiteral = dyn_cast<IntegerLiteral>(B->getLHS());
223 if (!IntLiteral || IntLiteral->getValue() != 0)
226 SVal Val = C.getSVal(LRHS ? B->getLHS() : B->getRHS());
227 if (hasDivZeroMap(Val, C))
230 } else if (const UnaryOperator *U = dyn_cast<UnaryOperator>(Condition)) {
231 if (U->getOpcode() == UO_LNot) {
233 if (const ImplicitCastExpr *I =
234 dyn_cast<ImplicitCastExpr>(U->getSubExpr()))
235 Val = C.getSVal(I->getSubExpr());
237 if (hasDivZeroMap(Val, C))
240 Val = C.getSVal(U->getSubExpr());
241 if (hasDivZeroMap(Val, C))
245 } else if (const ImplicitCastExpr *IE =
246 dyn_cast<ImplicitCastExpr>(Condition)) {
247 SVal Val = C.getSVal(IE->getSubExpr());
249 if (hasDivZeroMap(Val, C))
252 SVal Val = C.getSVal(Condition);
254 if (hasDivZeroMap(Val, C))
260 void ento::registerTestAfterDivZeroChecker(CheckerManager &mgr) {
261 mgr.registerChecker<TestAfterDivZeroChecker>();
264 bool ento::shouldRegisterTestAfterDivZeroChecker(const LangOptions &LO) {