blob: b83dea772b340dc3b0588a38a826b864ed2be701 [file] [log] [blame]
Jordan Rosedc352bb2014-07-10 16:10:52 +00001//== TestAfterDivZeroChecker.cpp - Test after division by zero checker --*--==//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This defines TestAfterDivZeroChecker, a builtin check that performs checks
11// for division by zero where the division occurs before comparison with zero.
12//
13//===----------------------------------------------------------------------===//
14
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"
21
22using namespace clang;
23using namespace ento;
24
25namespace {
26
27class ZeroState {
28private:
29 SymbolRef ZeroSymbol;
30 unsigned BlockID;
31 const StackFrameContext *SFC;
32
33public:
34 ZeroState(SymbolRef S, unsigned B, const StackFrameContext *SFC)
35 : ZeroSymbol(S), BlockID(B), SFC(SFC) {}
36
37 const StackFrameContext *getStackFrameContext() const { return SFC; }
38
39 bool operator==(const ZeroState &X) const {
40 return BlockID == X.BlockID && SFC == X.SFC && ZeroSymbol == X.ZeroSymbol;
41 }
42
43 bool operator<(const ZeroState &X) const {
44 if (BlockID != X.BlockID)
45 return BlockID < X.BlockID;
46 if (SFC != X.SFC)
47 return SFC < X.SFC;
48 return ZeroSymbol < X.ZeroSymbol;
49 }
50
51 void Profile(llvm::FoldingSetNodeID &ID) const {
52 ID.AddInteger(BlockID);
53 ID.AddPointer(SFC);
54 ID.AddPointer(ZeroSymbol);
55 }
56};
57
58class DivisionBRVisitor : public BugReporterVisitorImpl<DivisionBRVisitor> {
59private:
60 SymbolRef ZeroSymbol;
61 const StackFrameContext *SFC;
NAKAMURA Takumi5787cc22014-07-11 00:32:35 +000062 bool Satisfied;
Jordan Rosedc352bb2014-07-10 16:10:52 +000063
64public:
65 DivisionBRVisitor(SymbolRef ZeroSymbol, const StackFrameContext *SFC)
NAKAMURA Takumi5787cc22014-07-11 00:32:35 +000066 : ZeroSymbol(ZeroSymbol), SFC(SFC), Satisfied(false) {}
Jordan Rosedc352bb2014-07-10 16:10:52 +000067
68 void Profile(llvm::FoldingSetNodeID &ID) const override {
69 ID.Add(ZeroSymbol);
70 ID.Add(SFC);
71 }
72
David Blaikie0a0c2752017-01-05 17:26:53 +000073 std::shared_ptr<PathDiagnosticPiece> VisitNode(const ExplodedNode *Succ,
74 const ExplodedNode *Pred,
75 BugReporterContext &BRC,
76 BugReport &BR) override;
Jordan Rosedc352bb2014-07-10 16:10:52 +000077};
78
79class TestAfterDivZeroChecker
80 : public Checker<check::PreStmt<BinaryOperator>, check::BranchCondition,
81 check::EndFunction> {
82 mutable std::unique_ptr<BuiltinBug> DivZeroBug;
83 void reportBug(SVal Val, CheckerContext &C) const;
84
85public:
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;
92};
93} // end anonymous namespace
94
95REGISTER_SET_WITH_PROGRAMSTATE(DivZeroMap, ZeroState)
96
David Blaikie0a0c2752017-01-05 17:26:53 +000097std::shared_ptr<PathDiagnosticPiece>
98DivisionBRVisitor::VisitNode(const ExplodedNode *Succ, const ExplodedNode *Pred,
99 BugReporterContext &BRC, BugReport &BR) {
Jordan Rosedc352bb2014-07-10 16:10:52 +0000100 if (Satisfied)
101 return nullptr;
102
103 const Expr *E = nullptr;
104
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) {
110 E = BO->getRHS();
111 }
112 }
113
114 if (!E)
115 return nullptr;
116
George Karpenkovd703ec92018-01-17 20:27:29 +0000117 SVal S = Succ->getSVal(E);
Jordan Rosedc352bb2014-07-10 16:10:52 +0000118 if (ZeroSymbol == S.getAsSymbol() && SFC == Succ->getStackFrame()) {
119 Satisfied = true;
120
121 // Construct a new PathDiagnosticPiece.
122 ProgramPoint P = Succ->getLocation();
123 PathDiagnosticLocation L =
124 PathDiagnosticLocation::create(P, BRC.getSourceManager());
125
126 if (!L.isValid() || !L.asLocation().isValid())
127 return nullptr;
128
David Blaikie0a0c2752017-01-05 17:26:53 +0000129 return std::make_shared<PathDiagnosticEventPiece>(
Jordan Rosedc352bb2014-07-10 16:10:52 +0000130 L, "Division with compared value made here");
131 }
132
133 return nullptr;
134}
135
136bool TestAfterDivZeroChecker::isZero(SVal S, CheckerContext &C) const {
137 Optional<DefinedSVal> DSV = S.getAs<DefinedSVal>();
138
139 if (!DSV)
140 return false;
141
142 ConstraintManager &CM = C.getConstraintManager();
143 return !CM.assume(C.getState(), *DSV, true);
144}
145
146void TestAfterDivZeroChecker::setDivZeroMap(SVal Var, CheckerContext &C) const {
147 SymbolRef SR = Var.getAsSymbol();
148 if (!SR)
149 return;
150
151 ProgramStateRef State = C.getState();
152 State =
153 State->add<DivZeroMap>(ZeroState(SR, C.getBlockID(), C.getStackFrame()));
154 C.addTransition(State);
155}
156
157bool TestAfterDivZeroChecker::hasDivZeroMap(SVal Var,
158 const CheckerContext &C) const {
159 SymbolRef SR = Var.getAsSymbol();
160 if (!SR)
161 return false;
162
163 ZeroState ZS(SR, C.getBlockID(), C.getStackFrame());
164 return C.getState()->contains<DivZeroMap>(ZS);
165}
166
167void TestAfterDivZeroChecker::reportBug(SVal Val, CheckerContext &C) const {
Devin Coughline39bd402015-09-16 22:03:05 +0000168 if (ExplodedNode *N = C.generateErrorNode(C.getState())) {
Jordan Rosedc352bb2014-07-10 16:10:52 +0000169 if (!DivZeroBug)
170 DivZeroBug.reset(new BuiltinBug(this, "Division by zero"));
171
Aaron Ballman8d3a7a52015-06-23 13:15:32 +0000172 auto R = llvm::make_unique<BugReport>(
173 *DivZeroBug, "Value being compared against zero has already been used "
174 "for division",
175 N);
Jordan Rosedc352bb2014-07-10 16:10:52 +0000176
David Blaikie91e79022014-09-04 23:54:33 +0000177 R->addVisitor(llvm::make_unique<DivisionBRVisitor>(Val.getAsSymbol(),
178 C.getStackFrame()));
Aaron Ballman8d3a7a52015-06-23 13:15:32 +0000179 C.emitReport(std::move(R));
Jordan Rosedc352bb2014-07-10 16:10:52 +0000180 }
181}
182
183void TestAfterDivZeroChecker::checkEndFunction(CheckerContext &C) const {
184 ProgramStateRef State = C.getState();
185
186 DivZeroMapTy DivZeroes = State->get<DivZeroMap>();
187 if (DivZeroes.isEmpty())
188 return;
189
190 DivZeroMapTy::Factory &F = State->get_context<DivZeroMap>();
191 for (llvm::ImmutableSet<ZeroState>::iterator I = DivZeroes.begin(),
192 E = DivZeroes.end();
193 I != E; ++I) {
194 ZeroState ZS = *I;
195 if (ZS.getStackFrameContext() == C.getStackFrame())
196 DivZeroes = F.remove(DivZeroes, ZS);
197 }
198 C.addTransition(State->set<DivZeroMap>(DivZeroes));
199}
200
201void TestAfterDivZeroChecker::checkPreStmt(const BinaryOperator *B,
202 CheckerContext &C) const {
203 BinaryOperator::Opcode Op = B->getOpcode();
204 if (Op == BO_Div || Op == BO_Rem || Op == BO_DivAssign ||
205 Op == BO_RemAssign) {
206 SVal S = C.getSVal(B->getRHS());
207
208 if (!isZero(S, C))
209 setDivZeroMap(S, C);
210 }
211}
212
213void TestAfterDivZeroChecker::checkBranchCondition(const Stmt *Condition,
214 CheckerContext &C) const {
215 if (const BinaryOperator *B = dyn_cast<BinaryOperator>(Condition)) {
216 if (B->isComparisonOp()) {
217 const IntegerLiteral *IntLiteral = dyn_cast<IntegerLiteral>(B->getRHS());
218 bool LRHS = true;
219 if (!IntLiteral) {
220 IntLiteral = dyn_cast<IntegerLiteral>(B->getLHS());
221 LRHS = false;
222 }
223
224 if (!IntLiteral || IntLiteral->getValue() != 0)
225 return;
226
227 SVal Val = C.getSVal(LRHS ? B->getLHS() : B->getRHS());
228 if (hasDivZeroMap(Val, C))
229 reportBug(Val, C);
230 }
231 } else if (const UnaryOperator *U = dyn_cast<UnaryOperator>(Condition)) {
232 if (U->getOpcode() == UO_LNot) {
233 SVal Val;
234 if (const ImplicitCastExpr *I =
235 dyn_cast<ImplicitCastExpr>(U->getSubExpr()))
236 Val = C.getSVal(I->getSubExpr());
237
238 if (hasDivZeroMap(Val, C))
239 reportBug(Val, C);
240 else {
241 Val = C.getSVal(U->getSubExpr());
242 if (hasDivZeroMap(Val, C))
243 reportBug(Val, C);
244 }
245 }
246 } else if (const ImplicitCastExpr *IE =
247 dyn_cast<ImplicitCastExpr>(Condition)) {
248 SVal Val = C.getSVal(IE->getSubExpr());
249
250 if (hasDivZeroMap(Val, C))
251 reportBug(Val, C);
252 else {
253 SVal Val = C.getSVal(Condition);
254
255 if (hasDivZeroMap(Val, C))
256 reportBug(Val, C);
257 }
258 }
259}
260
261void ento::registerTestAfterDivZeroChecker(CheckerManager &mgr) {
262 mgr.registerChecker<TestAfterDivZeroChecker>();
263}