/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/lib/StaticAnalyzer/Checkers/DivZeroChecker.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | //== DivZeroChecker.cpp - Division by zero checker --------------*- C++ -*--==// |
2 | | // |
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 |
6 | | // |
7 | | //===----------------------------------------------------------------------===// |
8 | | // |
9 | | // This defines DivZeroChecker, a builtin check in ExprEngine that performs |
10 | | // checks for division by zeros. |
11 | | // |
12 | | //===----------------------------------------------------------------------===// |
13 | | |
14 | | #include "clang/StaticAnalyzer/Checkers/BuiltinCheckerRegistration.h" |
15 | | #include "clang/StaticAnalyzer/Checkers/Taint.h" |
16 | | #include "clang/StaticAnalyzer/Core/BugReporter/BugType.h" |
17 | | #include "clang/StaticAnalyzer/Core/Checker.h" |
18 | | #include "clang/StaticAnalyzer/Core/CheckerManager.h" |
19 | | #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h" |
20 | | #include <optional> |
21 | | |
22 | | using namespace clang; |
23 | | using namespace ento; |
24 | | using namespace taint; |
25 | | |
26 | | namespace { |
27 | | class DivZeroChecker : public Checker< check::PreStmt<BinaryOperator> > { |
28 | | mutable std::unique_ptr<BugType> BT; |
29 | | mutable std::unique_ptr<BugType> TaintBT; |
30 | | void reportBug(StringRef Msg, ProgramStateRef StateZero, |
31 | | CheckerContext &C) const; |
32 | | void reportTaintBug(StringRef Msg, ProgramStateRef StateZero, |
33 | | CheckerContext &C, |
34 | | llvm::ArrayRef<SymbolRef> TaintedSyms) const; |
35 | | |
36 | | public: |
37 | | void checkPreStmt(const BinaryOperator *B, CheckerContext &C) const; |
38 | | }; |
39 | | } // end anonymous namespace |
40 | | |
41 | 322 | static const Expr *getDenomExpr(const ExplodedNode *N) { |
42 | 322 | const Stmt *S = N->getLocationAs<PreStmt>()->getStmt(); |
43 | 322 | if (const auto *BE = dyn_cast<BinaryOperator>(S)) |
44 | 322 | return BE->getRHS(); |
45 | 0 | return nullptr; |
46 | 322 | } |
47 | | |
48 | | void DivZeroChecker::reportBug(StringRef Msg, ProgramStateRef StateZero, |
49 | 297 | CheckerContext &C) const { |
50 | 297 | if (ExplodedNode *N = C.generateErrorNode(StateZero)) { |
51 | 297 | if (!BT) |
52 | 108 | BT.reset(new BugType(this, "Division by zero", categories::LogicError)); |
53 | | |
54 | 297 | auto R = std::make_unique<PathSensitiveBugReport>(*BT, Msg, N); |
55 | 297 | bugreporter::trackExpressionValue(N, getDenomExpr(N), *R); |
56 | 297 | C.emitReport(std::move(R)); |
57 | 297 | } |
58 | 297 | } |
59 | | |
60 | | void DivZeroChecker::reportTaintBug( |
61 | | StringRef Msg, ProgramStateRef StateZero, CheckerContext &C, |
62 | 25 | llvm::ArrayRef<SymbolRef> TaintedSyms) const { |
63 | 25 | if (ExplodedNode *N = C.generateErrorNode(StateZero)) { |
64 | 25 | if (!TaintBT) |
65 | 3 | TaintBT.reset( |
66 | 3 | new BugType(this, "Division by zero", categories::TaintedData)); |
67 | | |
68 | 25 | auto R = std::make_unique<PathSensitiveBugReport>(*TaintBT, Msg, N); |
69 | 25 | bugreporter::trackExpressionValue(N, getDenomExpr(N), *R); |
70 | 25 | for (auto Sym : TaintedSyms) |
71 | 31 | R->markInteresting(Sym); |
72 | 25 | C.emitReport(std::move(R)); |
73 | 25 | } |
74 | 25 | } |
75 | | |
76 | | void DivZeroChecker::checkPreStmt(const BinaryOperator *B, |
77 | 128k | CheckerContext &C) const { |
78 | 128k | BinaryOperator::Opcode Op = B->getOpcode(); |
79 | 128k | if (Op != BO_Div && |
80 | 128k | Op != BO_Rem124k && |
81 | 128k | Op != BO_DivAssign124k && |
82 | 128k | Op != BO_RemAssign124k ) |
83 | 124k | return; |
84 | | |
85 | 4.28k | if (!B->getRHS()->getType()->isScalarType()) |
86 | 0 | return; |
87 | | |
88 | 4.28k | SVal Denom = C.getSVal(B->getRHS()); |
89 | 4.28k | std::optional<DefinedSVal> DV = Denom.getAs<DefinedSVal>(); |
90 | | |
91 | | // Divide-by-undefined handled in the generic checking for uses of |
92 | | // undefined values. |
93 | 4.28k | if (!DV) |
94 | 112 | return; |
95 | | |
96 | | // Check for divide by zero. |
97 | 4.16k | ConstraintManager &CM = C.getConstraintManager(); |
98 | 4.16k | ProgramStateRef stateNotZero, stateZero; |
99 | 4.16k | std::tie(stateNotZero, stateZero) = CM.assumeDual(C.getState(), *DV); |
100 | | |
101 | 4.16k | if (!stateNotZero) { |
102 | 297 | assert(stateZero); |
103 | 297 | reportBug("Division by zero", stateZero, C); |
104 | 297 | return; |
105 | 297 | } |
106 | | |
107 | 3.87k | if ((stateNotZero && stateZero)) { |
108 | 1.71k | std::vector<SymbolRef> taintedSyms = getTaintedSymbols(C.getState(), *DV); |
109 | 1.71k | if (!taintedSyms.empty()) { |
110 | 25 | reportTaintBug("Division by a tainted value, possibly zero", stateZero, C, |
111 | 25 | taintedSyms); |
112 | 25 | return; |
113 | 25 | } |
114 | 1.71k | } |
115 | | |
116 | | // If we get here, then the denom should not be zero. We abandon the implicit |
117 | | // zero denom case for now. |
118 | 3.84k | C.addTransition(stateNotZero); |
119 | 3.84k | } |
120 | | |
121 | 1.28k | void ento::registerDivZeroChecker(CheckerManager &mgr) { |
122 | 1.28k | mgr.registerChecker<DivZeroChecker>(); |
123 | 1.28k | } |
124 | | |
125 | 2.56k | bool ento::shouldRegisterDivZeroChecker(const CheckerManager &mgr) { |
126 | 2.56k | return true; |
127 | 2.56k | } |