xref: /freebsd-src/contrib/llvm-project/clang/lib/StaticAnalyzer/Checkers/TestAfterDivZeroChecker.cpp (revision 647cbc5de815c5651677bf8582797f716ec7b48d)
10b57cec5SDimitry Andric //== TestAfterDivZeroChecker.cpp - Test after division by zero checker --*--==//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric //
90b57cec5SDimitry Andric // This defines TestAfterDivZeroChecker, a builtin check that performs checks
100b57cec5SDimitry Andric //  for division by zero where the division occurs before comparison with zero.
110b57cec5SDimitry Andric //
120b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
130b57cec5SDimitry Andric 
140b57cec5SDimitry Andric #include "clang/StaticAnalyzer/Checkers/BuiltinCheckerRegistration.h"
150b57cec5SDimitry Andric #include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
160b57cec5SDimitry Andric #include "clang/StaticAnalyzer/Core/Checker.h"
170b57cec5SDimitry Andric #include "clang/StaticAnalyzer/Core/PathSensitive/CallEvent.h"
180b57cec5SDimitry Andric #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
190b57cec5SDimitry Andric #include "llvm/ADT/FoldingSet.h"
20bdd1243dSDimitry Andric #include <optional>
210b57cec5SDimitry Andric 
220b57cec5SDimitry Andric using namespace clang;
230b57cec5SDimitry Andric using namespace ento;
240b57cec5SDimitry Andric 
250b57cec5SDimitry Andric namespace {
260b57cec5SDimitry Andric 
270b57cec5SDimitry Andric class ZeroState {
280b57cec5SDimitry Andric private:
290b57cec5SDimitry Andric   SymbolRef ZeroSymbol;
300b57cec5SDimitry Andric   unsigned BlockID;
310b57cec5SDimitry Andric   const StackFrameContext *SFC;
320b57cec5SDimitry Andric 
330b57cec5SDimitry Andric public:
ZeroState(SymbolRef S,unsigned B,const StackFrameContext * SFC)340b57cec5SDimitry Andric   ZeroState(SymbolRef S, unsigned B, const StackFrameContext *SFC)
350b57cec5SDimitry Andric       : ZeroSymbol(S), BlockID(B), SFC(SFC) {}
360b57cec5SDimitry Andric 
getStackFrameContext() const370b57cec5SDimitry Andric   const StackFrameContext *getStackFrameContext() const { return SFC; }
380b57cec5SDimitry Andric 
operator ==(const ZeroState & X) const390b57cec5SDimitry Andric   bool operator==(const ZeroState &X) const {
400b57cec5SDimitry Andric     return BlockID == X.BlockID && SFC == X.SFC && ZeroSymbol == X.ZeroSymbol;
410b57cec5SDimitry Andric   }
420b57cec5SDimitry Andric 
operator <(const ZeroState & X) const430b57cec5SDimitry Andric   bool operator<(const ZeroState &X) const {
440b57cec5SDimitry Andric     if (BlockID != X.BlockID)
450b57cec5SDimitry Andric       return BlockID < X.BlockID;
460b57cec5SDimitry Andric     if (SFC != X.SFC)
470b57cec5SDimitry Andric       return SFC < X.SFC;
480b57cec5SDimitry Andric     return ZeroSymbol < X.ZeroSymbol;
490b57cec5SDimitry Andric   }
500b57cec5SDimitry Andric 
Profile(llvm::FoldingSetNodeID & ID) const510b57cec5SDimitry Andric   void Profile(llvm::FoldingSetNodeID &ID) const {
520b57cec5SDimitry Andric     ID.AddInteger(BlockID);
530b57cec5SDimitry Andric     ID.AddPointer(SFC);
540b57cec5SDimitry Andric     ID.AddPointer(ZeroSymbol);
550b57cec5SDimitry Andric   }
560b57cec5SDimitry Andric };
570b57cec5SDimitry Andric 
580b57cec5SDimitry Andric class DivisionBRVisitor : public BugReporterVisitor {
590b57cec5SDimitry Andric private:
600b57cec5SDimitry Andric   SymbolRef ZeroSymbol;
610b57cec5SDimitry Andric   const StackFrameContext *SFC;
620b57cec5SDimitry Andric   bool Satisfied;
630b57cec5SDimitry Andric 
640b57cec5SDimitry Andric public:
DivisionBRVisitor(SymbolRef ZeroSymbol,const StackFrameContext * SFC)650b57cec5SDimitry Andric   DivisionBRVisitor(SymbolRef ZeroSymbol, const StackFrameContext *SFC)
660b57cec5SDimitry Andric       : ZeroSymbol(ZeroSymbol), SFC(SFC), Satisfied(false) {}
670b57cec5SDimitry Andric 
Profile(llvm::FoldingSetNodeID & ID) const680b57cec5SDimitry Andric   void Profile(llvm::FoldingSetNodeID &ID) const override {
690b57cec5SDimitry Andric     ID.Add(ZeroSymbol);
700b57cec5SDimitry Andric     ID.Add(SFC);
710b57cec5SDimitry Andric   }
720b57cec5SDimitry Andric 
73a7dea167SDimitry Andric   PathDiagnosticPieceRef VisitNode(const ExplodedNode *Succ,
740b57cec5SDimitry Andric                                    BugReporterContext &BRC,
75a7dea167SDimitry Andric                                    PathSensitiveBugReport &BR) override;
760b57cec5SDimitry Andric };
770b57cec5SDimitry Andric 
780b57cec5SDimitry Andric class TestAfterDivZeroChecker
790b57cec5SDimitry Andric     : public Checker<check::PreStmt<BinaryOperator>, check::BranchCondition,
800b57cec5SDimitry Andric                      check::EndFunction> {
81*647cbc5dSDimitry Andric   const BugType DivZeroBug{this, "Division by zero"};
820b57cec5SDimitry Andric   void reportBug(SVal Val, CheckerContext &C) const;
830b57cec5SDimitry Andric 
840b57cec5SDimitry Andric public:
850b57cec5SDimitry Andric   void checkPreStmt(const BinaryOperator *B, CheckerContext &C) const;
860b57cec5SDimitry Andric   void checkBranchCondition(const Stmt *Condition, CheckerContext &C) const;
870b57cec5SDimitry Andric   void checkEndFunction(const ReturnStmt *RS, CheckerContext &C) const;
880b57cec5SDimitry Andric   void setDivZeroMap(SVal Var, CheckerContext &C) const;
890b57cec5SDimitry Andric   bool hasDivZeroMap(SVal Var, const CheckerContext &C) const;
900b57cec5SDimitry Andric   bool isZero(SVal S, CheckerContext &C) const;
910b57cec5SDimitry Andric };
920b57cec5SDimitry Andric } // end anonymous namespace
930b57cec5SDimitry Andric 
REGISTER_SET_WITH_PROGRAMSTATE(DivZeroMap,ZeroState)940b57cec5SDimitry Andric REGISTER_SET_WITH_PROGRAMSTATE(DivZeroMap, ZeroState)
950b57cec5SDimitry Andric 
96a7dea167SDimitry Andric PathDiagnosticPieceRef
97a7dea167SDimitry Andric DivisionBRVisitor::VisitNode(const ExplodedNode *Succ, BugReporterContext &BRC,
98a7dea167SDimitry Andric                              PathSensitiveBugReport &BR) {
990b57cec5SDimitry Andric   if (Satisfied)
1000b57cec5SDimitry Andric     return nullptr;
1010b57cec5SDimitry Andric 
1020b57cec5SDimitry Andric   const Expr *E = nullptr;
1030b57cec5SDimitry Andric 
104bdd1243dSDimitry Andric   if (std::optional<PostStmt> P = Succ->getLocationAs<PostStmt>())
1050b57cec5SDimitry Andric     if (const BinaryOperator *BO = P->getStmtAs<BinaryOperator>()) {
1060b57cec5SDimitry Andric       BinaryOperator::Opcode Op = BO->getOpcode();
1070b57cec5SDimitry Andric       if (Op == BO_Div || Op == BO_Rem || Op == BO_DivAssign ||
1080b57cec5SDimitry Andric           Op == BO_RemAssign) {
1090b57cec5SDimitry Andric         E = BO->getRHS();
1100b57cec5SDimitry Andric       }
1110b57cec5SDimitry Andric     }
1120b57cec5SDimitry Andric 
1130b57cec5SDimitry Andric   if (!E)
1140b57cec5SDimitry Andric     return nullptr;
1150b57cec5SDimitry Andric 
1160b57cec5SDimitry Andric   SVal S = Succ->getSVal(E);
1170b57cec5SDimitry Andric   if (ZeroSymbol == S.getAsSymbol() && SFC == Succ->getStackFrame()) {
1180b57cec5SDimitry Andric     Satisfied = true;
1190b57cec5SDimitry Andric 
1200b57cec5SDimitry Andric     // Construct a new PathDiagnosticPiece.
1210b57cec5SDimitry Andric     ProgramPoint P = Succ->getLocation();
1220b57cec5SDimitry Andric     PathDiagnosticLocation L =
1230b57cec5SDimitry Andric         PathDiagnosticLocation::create(P, BRC.getSourceManager());
1240b57cec5SDimitry Andric 
1250b57cec5SDimitry Andric     if (!L.isValid() || !L.asLocation().isValid())
1260b57cec5SDimitry Andric       return nullptr;
1270b57cec5SDimitry Andric 
1280b57cec5SDimitry Andric     return std::make_shared<PathDiagnosticEventPiece>(
1290b57cec5SDimitry Andric         L, "Division with compared value made here");
1300b57cec5SDimitry Andric   }
1310b57cec5SDimitry Andric 
1320b57cec5SDimitry Andric   return nullptr;
1330b57cec5SDimitry Andric }
1340b57cec5SDimitry Andric 
isZero(SVal S,CheckerContext & C) const1350b57cec5SDimitry Andric bool TestAfterDivZeroChecker::isZero(SVal S, CheckerContext &C) const {
136bdd1243dSDimitry Andric   std::optional<DefinedSVal> DSV = S.getAs<DefinedSVal>();
1370b57cec5SDimitry Andric 
1380b57cec5SDimitry Andric   if (!DSV)
1390b57cec5SDimitry Andric     return false;
1400b57cec5SDimitry Andric 
1410b57cec5SDimitry Andric   ConstraintManager &CM = C.getConstraintManager();
1420b57cec5SDimitry Andric   return !CM.assume(C.getState(), *DSV, true);
1430b57cec5SDimitry Andric }
1440b57cec5SDimitry Andric 
setDivZeroMap(SVal Var,CheckerContext & C) const1450b57cec5SDimitry Andric void TestAfterDivZeroChecker::setDivZeroMap(SVal Var, CheckerContext &C) const {
1460b57cec5SDimitry Andric   SymbolRef SR = Var.getAsSymbol();
1470b57cec5SDimitry Andric   if (!SR)
1480b57cec5SDimitry Andric     return;
1490b57cec5SDimitry Andric 
1500b57cec5SDimitry Andric   ProgramStateRef State = C.getState();
1510b57cec5SDimitry Andric   State =
1520b57cec5SDimitry Andric       State->add<DivZeroMap>(ZeroState(SR, C.getBlockID(), C.getStackFrame()));
1530b57cec5SDimitry Andric   C.addTransition(State);
1540b57cec5SDimitry Andric }
1550b57cec5SDimitry Andric 
hasDivZeroMap(SVal Var,const CheckerContext & C) const1560b57cec5SDimitry Andric bool TestAfterDivZeroChecker::hasDivZeroMap(SVal Var,
1570b57cec5SDimitry Andric                                             const CheckerContext &C) const {
1580b57cec5SDimitry Andric   SymbolRef SR = Var.getAsSymbol();
1590b57cec5SDimitry Andric   if (!SR)
1600b57cec5SDimitry Andric     return false;
1610b57cec5SDimitry Andric 
1620b57cec5SDimitry Andric   ZeroState ZS(SR, C.getBlockID(), C.getStackFrame());
1630b57cec5SDimitry Andric   return C.getState()->contains<DivZeroMap>(ZS);
1640b57cec5SDimitry Andric }
1650b57cec5SDimitry Andric 
reportBug(SVal Val,CheckerContext & C) const1660b57cec5SDimitry Andric void TestAfterDivZeroChecker::reportBug(SVal Val, CheckerContext &C) const {
1670b57cec5SDimitry Andric   if (ExplodedNode *N = C.generateErrorNode(C.getState())) {
168a7dea167SDimitry Andric     auto R = std::make_unique<PathSensitiveBugReport>(
169*647cbc5dSDimitry Andric         DivZeroBug,
170*647cbc5dSDimitry Andric         "Value being compared against zero has already been used "
1710b57cec5SDimitry Andric         "for division",
1720b57cec5SDimitry Andric         N);
1730b57cec5SDimitry Andric 
174a7dea167SDimitry Andric     R->addVisitor(std::make_unique<DivisionBRVisitor>(Val.getAsSymbol(),
1750b57cec5SDimitry Andric                                                        C.getStackFrame()));
1760b57cec5SDimitry Andric     C.emitReport(std::move(R));
1770b57cec5SDimitry Andric   }
1780b57cec5SDimitry Andric }
1790b57cec5SDimitry Andric 
checkEndFunction(const ReturnStmt *,CheckerContext & C) const1800b57cec5SDimitry Andric void TestAfterDivZeroChecker::checkEndFunction(const ReturnStmt *,
1810b57cec5SDimitry Andric                                                CheckerContext &C) const {
1820b57cec5SDimitry Andric   ProgramStateRef State = C.getState();
1830b57cec5SDimitry Andric 
1840b57cec5SDimitry Andric   DivZeroMapTy DivZeroes = State->get<DivZeroMap>();
1850b57cec5SDimitry Andric   if (DivZeroes.isEmpty())
1860b57cec5SDimitry Andric     return;
1870b57cec5SDimitry Andric 
1880b57cec5SDimitry Andric   DivZeroMapTy::Factory &F = State->get_context<DivZeroMap>();
18906c3fb27SDimitry Andric   for (const ZeroState &ZS : DivZeroes) {
1900b57cec5SDimitry Andric     if (ZS.getStackFrameContext() == C.getStackFrame())
1910b57cec5SDimitry Andric       DivZeroes = F.remove(DivZeroes, ZS);
1920b57cec5SDimitry Andric   }
1930b57cec5SDimitry Andric   C.addTransition(State->set<DivZeroMap>(DivZeroes));
1940b57cec5SDimitry Andric }
1950b57cec5SDimitry Andric 
checkPreStmt(const BinaryOperator * B,CheckerContext & C) const1960b57cec5SDimitry Andric void TestAfterDivZeroChecker::checkPreStmt(const BinaryOperator *B,
1970b57cec5SDimitry Andric                                            CheckerContext &C) const {
1980b57cec5SDimitry Andric   BinaryOperator::Opcode Op = B->getOpcode();
1990b57cec5SDimitry Andric   if (Op == BO_Div || Op == BO_Rem || Op == BO_DivAssign ||
2000b57cec5SDimitry Andric       Op == BO_RemAssign) {
2010b57cec5SDimitry Andric     SVal S = C.getSVal(B->getRHS());
2020b57cec5SDimitry Andric 
2030b57cec5SDimitry Andric     if (!isZero(S, C))
2040b57cec5SDimitry Andric       setDivZeroMap(S, C);
2050b57cec5SDimitry Andric   }
2060b57cec5SDimitry Andric }
2070b57cec5SDimitry Andric 
checkBranchCondition(const Stmt * Condition,CheckerContext & C) const2080b57cec5SDimitry Andric void TestAfterDivZeroChecker::checkBranchCondition(const Stmt *Condition,
2090b57cec5SDimitry Andric                                                    CheckerContext &C) const {
2100b57cec5SDimitry Andric   if (const BinaryOperator *B = dyn_cast<BinaryOperator>(Condition)) {
2110b57cec5SDimitry Andric     if (B->isComparisonOp()) {
2120b57cec5SDimitry Andric       const IntegerLiteral *IntLiteral = dyn_cast<IntegerLiteral>(B->getRHS());
2130b57cec5SDimitry Andric       bool LRHS = true;
2140b57cec5SDimitry Andric       if (!IntLiteral) {
2150b57cec5SDimitry Andric         IntLiteral = dyn_cast<IntegerLiteral>(B->getLHS());
2160b57cec5SDimitry Andric         LRHS = false;
2170b57cec5SDimitry Andric       }
2180b57cec5SDimitry Andric 
2190b57cec5SDimitry Andric       if (!IntLiteral || IntLiteral->getValue() != 0)
2200b57cec5SDimitry Andric         return;
2210b57cec5SDimitry Andric 
2220b57cec5SDimitry Andric       SVal Val = C.getSVal(LRHS ? B->getLHS() : B->getRHS());
2230b57cec5SDimitry Andric       if (hasDivZeroMap(Val, C))
2240b57cec5SDimitry Andric         reportBug(Val, C);
2250b57cec5SDimitry Andric     }
2260b57cec5SDimitry Andric   } else if (const UnaryOperator *U = dyn_cast<UnaryOperator>(Condition)) {
2270b57cec5SDimitry Andric     if (U->getOpcode() == UO_LNot) {
2280b57cec5SDimitry Andric       SVal Val;
2290b57cec5SDimitry Andric       if (const ImplicitCastExpr *I =
2300b57cec5SDimitry Andric               dyn_cast<ImplicitCastExpr>(U->getSubExpr()))
2310b57cec5SDimitry Andric         Val = C.getSVal(I->getSubExpr());
2320b57cec5SDimitry Andric 
2330b57cec5SDimitry Andric       if (hasDivZeroMap(Val, C))
2340b57cec5SDimitry Andric         reportBug(Val, C);
2350b57cec5SDimitry Andric       else {
2360b57cec5SDimitry Andric         Val = C.getSVal(U->getSubExpr());
2370b57cec5SDimitry Andric         if (hasDivZeroMap(Val, C))
2380b57cec5SDimitry Andric           reportBug(Val, C);
2390b57cec5SDimitry Andric       }
2400b57cec5SDimitry Andric     }
2410b57cec5SDimitry Andric   } else if (const ImplicitCastExpr *IE =
2420b57cec5SDimitry Andric                  dyn_cast<ImplicitCastExpr>(Condition)) {
2430b57cec5SDimitry Andric     SVal Val = C.getSVal(IE->getSubExpr());
2440b57cec5SDimitry Andric 
2450b57cec5SDimitry Andric     if (hasDivZeroMap(Val, C))
2460b57cec5SDimitry Andric       reportBug(Val, C);
2470b57cec5SDimitry Andric     else {
2480b57cec5SDimitry Andric       SVal Val = C.getSVal(Condition);
2490b57cec5SDimitry Andric 
2500b57cec5SDimitry Andric       if (hasDivZeroMap(Val, C))
2510b57cec5SDimitry Andric         reportBug(Val, C);
2520b57cec5SDimitry Andric     }
2530b57cec5SDimitry Andric   }
2540b57cec5SDimitry Andric }
2550b57cec5SDimitry Andric 
registerTestAfterDivZeroChecker(CheckerManager & mgr)2560b57cec5SDimitry Andric void ento::registerTestAfterDivZeroChecker(CheckerManager &mgr) {
2570b57cec5SDimitry Andric   mgr.registerChecker<TestAfterDivZeroChecker>();
2580b57cec5SDimitry Andric }
2590b57cec5SDimitry Andric 
shouldRegisterTestAfterDivZeroChecker(const CheckerManager & mgr)2605ffd83dbSDimitry Andric bool ento::shouldRegisterTestAfterDivZeroChecker(const CheckerManager &mgr) {
2610b57cec5SDimitry Andric   return true;
2620b57cec5SDimitry Andric }
263