blob: 776e12bd2ae4fb3f0a8298b4e56548820f37f7e6 [file] [log] [blame]
Ted Kremenek53500662009-07-22 17:55:28 +00001// BugReporterVisitors.cpp - Helpers for reporting bugs -----------*- C++ -*--//
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 file defines a set of BugReporter "visitors" which can be used to
11// enhance the diagnostics reported for a bug.
12//
13//===----------------------------------------------------------------------===//
14
15#include "clang/AST/Expr.h"
16#include "clang/AST/ExprObjC.h"
Ted Kremenek6b676302010-01-25 17:10:22 +000017#include "clang/Checker/BugReporter/BugReporter.h"
18#include "clang/Checker/BugReporter/PathDiagnostic.h"
Benjamin Kramer5e2d2c22010-03-27 21:19:47 +000019#include "clang/Checker/PathSensitive/ExplodedGraph.h"
Ted Kremenek1309f9a2010-01-25 04:41:41 +000020#include "clang/Checker/PathSensitive/GRState.h"
Ted Kremenek53500662009-07-22 17:55:28 +000021
22using namespace clang;
23
24//===----------------------------------------------------------------------===//
25// Utility functions.
26//===----------------------------------------------------------------------===//
27
Zhongxing Xuc5619d92009-08-06 01:32:16 +000028const Stmt *clang::bugreporter::GetDerefExpr(const ExplodedNode *N) {
Ted Kremenek53500662009-07-22 17:55:28 +000029 // Pattern match for a few useful cases (do something smarter later):
30 // a[0], p->f, *p
31 const Stmt *S = N->getLocationAs<PostStmt>()->getStmt();
Mike Stump1eb44332009-09-09 15:08:12 +000032
Ted Kremenek53500662009-07-22 17:55:28 +000033 if (const UnaryOperator *U = dyn_cast<UnaryOperator>(S)) {
34 if (U->getOpcode() == UnaryOperator::Deref)
35 return U->getSubExpr()->IgnoreParenCasts();
36 }
37 else if (const MemberExpr *ME = dyn_cast<MemberExpr>(S)) {
38 return ME->getBase()->IgnoreParenCasts();
39 }
40 else if (const ArraySubscriptExpr *AE = dyn_cast<ArraySubscriptExpr>(S)) {
41 // Retrieve the base for arrays since BasicStoreManager doesn't know how
42 // to reason about them.
43 return AE->getBase();
44 }
Mike Stump1eb44332009-09-09 15:08:12 +000045
46 return NULL;
Ted Kremenek53500662009-07-22 17:55:28 +000047}
48
49const Stmt*
Zhongxing Xuc5619d92009-08-06 01:32:16 +000050clang::bugreporter::GetDenomExpr(const ExplodedNode *N) {
Zhongxing Xu6403b572009-09-02 13:26:26 +000051 const Stmt *S = N->getLocationAs<PreStmt>()->getStmt();
Ted Kremenek53500662009-07-22 17:55:28 +000052 if (const BinaryOperator *BE = dyn_cast<BinaryOperator>(S))
53 return BE->getRHS();
54 return NULL;
55}
56
57const Stmt*
Zhongxing Xuc5619d92009-08-06 01:32:16 +000058clang::bugreporter::GetCalleeExpr(const ExplodedNode *N) {
Zhongxing Xud99f3612009-09-02 08:10:35 +000059 // Callee is checked as a PreVisit to the CallExpr.
60 const Stmt *S = N->getLocationAs<PreStmt>()->getStmt();
Ted Kremenek53500662009-07-22 17:55:28 +000061 if (const CallExpr *CE = dyn_cast<CallExpr>(S))
62 return CE->getCallee();
63 return NULL;
64}
65
66const Stmt*
Zhongxing Xuc5619d92009-08-06 01:32:16 +000067clang::bugreporter::GetRetValExpr(const ExplodedNode *N) {
Ted Kremenek53500662009-07-22 17:55:28 +000068 const Stmt *S = N->getLocationAs<PostStmt>()->getStmt();
69 if (const ReturnStmt *RS = dyn_cast<ReturnStmt>(S))
70 return RS->getRetValue();
71 return NULL;
72}
73
74//===----------------------------------------------------------------------===//
75// Definitions for bug reporter visitors.
76//===----------------------------------------------------------------------===//
77
78namespace {
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +000079class FindLastStoreBRVisitor : public BugReporterVisitor {
Ted Kremenek53500662009-07-22 17:55:28 +000080 const MemRegion *R;
81 SVal V;
82 bool satisfied;
Zhongxing Xuc5619d92009-08-06 01:32:16 +000083 const ExplodedNode *StoreSite;
Ted Kremenek53500662009-07-22 17:55:28 +000084public:
85 FindLastStoreBRVisitor(SVal v, const MemRegion *r)
86 : R(r), V(v), satisfied(false), StoreSite(0) {}
Mike Stump1eb44332009-09-09 15:08:12 +000087
Ted Kremenek1b431022010-03-20 18:01:57 +000088 virtual void Profile(llvm::FoldingSetNodeID &ID) const {
89 static int tag = 0;
90 ID.AddPointer(&tag);
91 ID.AddPointer(R);
92 ID.Add(V);
93 }
94
Zhongxing Xuc5619d92009-08-06 01:32:16 +000095 PathDiagnosticPiece* VisitNode(const ExplodedNode *N,
96 const ExplodedNode *PrevN,
Ted Kremenek53500662009-07-22 17:55:28 +000097 BugReporterContext& BRC) {
Mike Stump1eb44332009-09-09 15:08:12 +000098
Ted Kremenek53500662009-07-22 17:55:28 +000099 if (satisfied)
100 return NULL;
Mike Stump1eb44332009-09-09 15:08:12 +0000101
102 if (!StoreSite) {
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000103 const ExplodedNode *Node = N, *Last = NULL;
Mike Stump1eb44332009-09-09 15:08:12 +0000104
Ted Kremenek53500662009-07-22 17:55:28 +0000105 for ( ; Node ; Last = Node, Node = Node->getFirstPred()) {
Daniel Dunbar0f2c9072010-03-20 04:28:39 +0000106
Ted Kremenek53500662009-07-22 17:55:28 +0000107 if (const VarRegion *VR = dyn_cast<VarRegion>(R)) {
108 if (const PostStmt *P = Node->getLocationAs<PostStmt>())
109 if (const DeclStmt *DS = P->getStmtAs<DeclStmt>())
110 if (DS->getSingleDecl() == VR->getDecl()) {
111 Last = Node;
112 break;
113 }
114 }
Mike Stump1eb44332009-09-09 15:08:12 +0000115
Ted Kremenek13976632010-02-08 16:18:51 +0000116 if (Node->getState()->getSVal(R) != V)
Ted Kremenek53500662009-07-22 17:55:28 +0000117 break;
118 }
Mike Stump1eb44332009-09-09 15:08:12 +0000119
Ted Kremenek53500662009-07-22 17:55:28 +0000120 if (!Node || !Last) {
121 satisfied = true;
122 return NULL;
123 }
Mike Stump1eb44332009-09-09 15:08:12 +0000124
Ted Kremenek53500662009-07-22 17:55:28 +0000125 StoreSite = Last;
126 }
Mike Stump1eb44332009-09-09 15:08:12 +0000127
Ted Kremenek53500662009-07-22 17:55:28 +0000128 if (StoreSite != N)
129 return NULL;
Mike Stump1eb44332009-09-09 15:08:12 +0000130
Ted Kremenek53500662009-07-22 17:55:28 +0000131 satisfied = true;
Ted Kremenek1b431022010-03-20 18:01:57 +0000132 llvm::SmallString<256> sbuf;
133 llvm::raw_svector_ostream os(sbuf);
Mike Stump1eb44332009-09-09 15:08:12 +0000134
Ted Kremenek53500662009-07-22 17:55:28 +0000135 if (const PostStmt *PS = N->getLocationAs<PostStmt>()) {
136 if (const DeclStmt *DS = PS->getStmtAs<DeclStmt>()) {
Mike Stump1eb44332009-09-09 15:08:12 +0000137
Ted Kremenek53500662009-07-22 17:55:28 +0000138 if (const VarRegion *VR = dyn_cast<VarRegion>(R)) {
Benjamin Kramer900fc632010-04-17 09:33:03 +0000139 os << "Variable '" << VR->getDecl() << "' ";
Ted Kremenek53500662009-07-22 17:55:28 +0000140 }
141 else
142 return NULL;
Mike Stump1eb44332009-09-09 15:08:12 +0000143
Ted Kremenek53500662009-07-22 17:55:28 +0000144 if (isa<loc::ConcreteInt>(V)) {
145 bool b = false;
146 ASTContext &C = BRC.getASTContext();
147 if (R->isBoundable()) {
148 if (const TypedRegion *TR = dyn_cast<TypedRegion>(R)) {
149 if (TR->getValueType(C)->isObjCObjectPointerType()) {
150 os << "initialized to nil";
151 b = true;
152 }
153 }
154 }
Mike Stump1eb44332009-09-09 15:08:12 +0000155
Ted Kremenek53500662009-07-22 17:55:28 +0000156 if (!b)
157 os << "initialized to a null pointer value";
158 }
159 else if (isa<nonloc::ConcreteInt>(V)) {
160 os << "initialized to " << cast<nonloc::ConcreteInt>(V).getValue();
161 }
162 else if (V.isUndef()) {
163 if (isa<VarRegion>(R)) {
164 const VarDecl *VD = cast<VarDecl>(DS->getSingleDecl());
165 if (VD->getInit())
166 os << "initialized to a garbage value";
167 else
Mike Stump1eb44332009-09-09 15:08:12 +0000168 os << "declared without an initial value";
169 }
Ted Kremenek53500662009-07-22 17:55:28 +0000170 }
171 }
172 }
Mike Stump1eb44332009-09-09 15:08:12 +0000173
174 if (os.str().empty()) {
Ted Kremenek53500662009-07-22 17:55:28 +0000175 if (isa<loc::ConcreteInt>(V)) {
176 bool b = false;
177 ASTContext &C = BRC.getASTContext();
178 if (R->isBoundable()) {
179 if (const TypedRegion *TR = dyn_cast<TypedRegion>(R)) {
180 if (TR->getValueType(C)->isObjCObjectPointerType()) {
181 os << "nil object reference stored to ";
182 b = true;
183 }
184 }
185 }
Mike Stump1eb44332009-09-09 15:08:12 +0000186
Ted Kremenek53500662009-07-22 17:55:28 +0000187 if (!b)
188 os << "Null pointer value stored to ";
189 }
190 else if (V.isUndef()) {
191 os << "Uninitialized value stored to ";
192 }
Ted Kremenek592362b2009-08-18 01:05:30 +0000193 else if (isa<nonloc::ConcreteInt>(V)) {
194 os << "The value " << cast<nonloc::ConcreteInt>(V).getValue()
195 << " is assigned to ";
196 }
Ted Kremenek53500662009-07-22 17:55:28 +0000197 else
198 return NULL;
Mike Stump1eb44332009-09-09 15:08:12 +0000199
Ted Kremenek53500662009-07-22 17:55:28 +0000200 if (const VarRegion *VR = dyn_cast<VarRegion>(R)) {
Benjamin Kramer900fc632010-04-17 09:33:03 +0000201 os << '\'' << VR->getDecl() << '\'';
Ted Kremenek53500662009-07-22 17:55:28 +0000202 }
203 else
204 return NULL;
205 }
Mike Stump1eb44332009-09-09 15:08:12 +0000206
Ted Kremenek53500662009-07-22 17:55:28 +0000207 // FIXME: Refactor this into BugReporterContext.
Mike Stump1eb44332009-09-09 15:08:12 +0000208 const Stmt *S = 0;
Ted Kremenek53500662009-07-22 17:55:28 +0000209 ProgramPoint P = N->getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +0000210
Ted Kremenek53500662009-07-22 17:55:28 +0000211 if (BlockEdge *BE = dyn_cast<BlockEdge>(&P)) {
212 CFGBlock *BSrc = BE->getSrc();
213 S = BSrc->getTerminatorCondition();
214 }
215 else if (PostStmt *PS = dyn_cast<PostStmt>(&P)) {
216 S = PS->getStmt();
217 }
Mike Stump1eb44332009-09-09 15:08:12 +0000218
Ted Kremenek53500662009-07-22 17:55:28 +0000219 if (!S)
220 return NULL;
Mike Stump1eb44332009-09-09 15:08:12 +0000221
Ted Kremenek53500662009-07-22 17:55:28 +0000222 // Construct a new PathDiagnosticPiece.
223 PathDiagnosticLocation L(S, BRC.getSourceManager());
224 return new PathDiagnosticEventPiece(L, os.str());
225 }
226};
227
228
229static void registerFindLastStore(BugReporterContext& BRC, const MemRegion *R,
230 SVal V) {
231 BRC.addVisitor(new FindLastStoreBRVisitor(V, R));
232}
233
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +0000234class TrackConstraintBRVisitor : public BugReporterVisitor {
Ted Kremenek5b9bd212009-09-11 22:07:28 +0000235 DefinedSVal Constraint;
Ted Kremenek53500662009-07-22 17:55:28 +0000236 const bool Assumption;
237 bool isSatisfied;
238public:
Ted Kremenek5b9bd212009-09-11 22:07:28 +0000239 TrackConstraintBRVisitor(DefinedSVal constraint, bool assumption)
Ted Kremenek53500662009-07-22 17:55:28 +0000240 : Constraint(constraint), Assumption(assumption), isSatisfied(false) {}
Mike Stump1eb44332009-09-09 15:08:12 +0000241
Ted Kremenek1b431022010-03-20 18:01:57 +0000242 void Profile(llvm::FoldingSetNodeID &ID) const {
243 static int tag = 0;
244 ID.AddPointer(&tag);
245 ID.AddBoolean(Assumption);
246 ID.Add(Constraint);
247 }
248
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000249 PathDiagnosticPiece* VisitNode(const ExplodedNode *N,
250 const ExplodedNode *PrevN,
Ted Kremenek53500662009-07-22 17:55:28 +0000251 BugReporterContext& BRC) {
252 if (isSatisfied)
253 return NULL;
Mike Stump1eb44332009-09-09 15:08:12 +0000254
Ted Kremenek53500662009-07-22 17:55:28 +0000255 // Check if in the previous state it was feasible for this constraint
256 // to *not* be true.
Ted Kremenek5b9bd212009-09-11 22:07:28 +0000257 if (PrevN->getState()->Assume(Constraint, !Assumption)) {
Daniel Dunbar0f2c9072010-03-20 04:28:39 +0000258
Ted Kremenek53500662009-07-22 17:55:28 +0000259 isSatisfied = true;
Mike Stump1eb44332009-09-09 15:08:12 +0000260
Ted Kremenek53500662009-07-22 17:55:28 +0000261 // As a sanity check, make sure that the negation of the constraint
262 // was infeasible in the current state. If it is feasible, we somehow
263 // missed the transition point.
Ted Kremenek5b9bd212009-09-11 22:07:28 +0000264 if (N->getState()->Assume(Constraint, !Assumption))
Ted Kremenek53500662009-07-22 17:55:28 +0000265 return NULL;
Mike Stump1eb44332009-09-09 15:08:12 +0000266
Ted Kremenek53500662009-07-22 17:55:28 +0000267 // We found the transition point for the constraint. We now need to
Mike Stump1eb44332009-09-09 15:08:12 +0000268 // pretty-print the constraint. (work-in-progress)
Daniel Dunbar0f2c9072010-03-20 04:28:39 +0000269 std::string sbuf;
270 llvm::raw_string_ostream os(sbuf);
Mike Stump1eb44332009-09-09 15:08:12 +0000271
Ted Kremenek53500662009-07-22 17:55:28 +0000272 if (isa<Loc>(Constraint)) {
273 os << "Assuming pointer value is ";
274 os << (Assumption ? "non-null" : "null");
275 }
Mike Stump1eb44332009-09-09 15:08:12 +0000276
Ted Kremenek53500662009-07-22 17:55:28 +0000277 if (os.str().empty())
278 return NULL;
Mike Stump1eb44332009-09-09 15:08:12 +0000279
Ted Kremenek53500662009-07-22 17:55:28 +0000280 // FIXME: Refactor this into BugReporterContext.
Mike Stump1eb44332009-09-09 15:08:12 +0000281 const Stmt *S = 0;
Ted Kremenek53500662009-07-22 17:55:28 +0000282 ProgramPoint P = N->getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +0000283
Ted Kremenek53500662009-07-22 17:55:28 +0000284 if (BlockEdge *BE = dyn_cast<BlockEdge>(&P)) {
285 CFGBlock *BSrc = BE->getSrc();
286 S = BSrc->getTerminatorCondition();
287 }
288 else if (PostStmt *PS = dyn_cast<PostStmt>(&P)) {
289 S = PS->getStmt();
290 }
Mike Stump1eb44332009-09-09 15:08:12 +0000291
Ted Kremenek53500662009-07-22 17:55:28 +0000292 if (!S)
293 return NULL;
Mike Stump1eb44332009-09-09 15:08:12 +0000294
Ted Kremenek53500662009-07-22 17:55:28 +0000295 // Construct a new PathDiagnosticPiece.
296 PathDiagnosticLocation L(S, BRC.getSourceManager());
297 return new PathDiagnosticEventPiece(L, os.str());
298 }
Mike Stump1eb44332009-09-09 15:08:12 +0000299
Ted Kremenek53500662009-07-22 17:55:28 +0000300 return NULL;
Mike Stump1eb44332009-09-09 15:08:12 +0000301 }
Ted Kremenek53500662009-07-22 17:55:28 +0000302};
303} // end anonymous namespace
304
Ted Kremenek5b9bd212009-09-11 22:07:28 +0000305static void registerTrackConstraint(BugReporterContext& BRC,
306 DefinedSVal Constraint,
Ted Kremenek53500662009-07-22 17:55:28 +0000307 bool Assumption) {
Mike Stump1eb44332009-09-09 15:08:12 +0000308 BRC.addVisitor(new TrackConstraintBRVisitor(Constraint, Assumption));
Ted Kremenek53500662009-07-22 17:55:28 +0000309}
310
311void clang::bugreporter::registerTrackNullOrUndefValue(BugReporterContext& BRC,
Ted Kremenek592362b2009-08-18 01:05:30 +0000312 const void *data,
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000313 const ExplodedNode* N) {
Mike Stump1eb44332009-09-09 15:08:12 +0000314
Ted Kremenek592362b2009-08-18 01:05:30 +0000315 const Stmt *S = static_cast<const Stmt*>(data);
Mike Stump1eb44332009-09-09 15:08:12 +0000316
Ted Kremenek53500662009-07-22 17:55:28 +0000317 if (!S)
318 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000319
Ted Kremenek53500662009-07-22 17:55:28 +0000320 GRStateManager &StateMgr = BRC.getStateManager();
Mike Stump1eb44332009-09-09 15:08:12 +0000321 const GRState *state = N->getState();
322
323 if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(S)) {
324 if (const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl())) {
Ted Kremenek53500662009-07-22 17:55:28 +0000325 const VarRegion *R =
Ted Kremenekd17da2b2009-08-21 22:28:32 +0000326 StateMgr.getRegionManager().getVarRegion(VD, N->getLocationContext());
Mike Stump1eb44332009-09-09 15:08:12 +0000327
Ted Kremenek53500662009-07-22 17:55:28 +0000328 // What did we load?
Ted Kremenek13976632010-02-08 16:18:51 +0000329 SVal V = state->getSVal(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000330
331 if (isa<loc::ConcreteInt>(V) || isa<nonloc::ConcreteInt>(V)
Ted Kremenek53500662009-07-22 17:55:28 +0000332 || V.isUndef()) {
Ted Kremenek94fd0b82010-02-16 08:33:59 +0000333 ::registerFindLastStore(BRC, R, V);
Ted Kremenek53500662009-07-22 17:55:28 +0000334 }
335 }
336 }
Mike Stump1eb44332009-09-09 15:08:12 +0000337
Ted Kremenek13976632010-02-08 16:18:51 +0000338 SVal V = state->getSValAsScalarOrLoc(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000339
Ted Kremenek53500662009-07-22 17:55:28 +0000340 // Uncomment this to find cases where we aren't properly getting the
341 // base value that was dereferenced.
342 // assert(!V.isUnknownOrUndef());
Mike Stump1eb44332009-09-09 15:08:12 +0000343
Ted Kremenek53500662009-07-22 17:55:28 +0000344 // Is it a symbolic value?
345 if (loc::MemRegionVal *L = dyn_cast<loc::MemRegionVal>(&V)) {
346 const SubRegion *R = cast<SubRegion>(L->getRegion());
347 while (R && !isa<SymbolicRegion>(R)) {
348 R = dyn_cast<SubRegion>(R->getSuperRegion());
349 }
Mike Stump1eb44332009-09-09 15:08:12 +0000350
Ted Kremenek53500662009-07-22 17:55:28 +0000351 if (R) {
352 assert(isa<SymbolicRegion>(R));
353 registerTrackConstraint(BRC, loc::MemRegionVal(R), false);
354 }
355 }
356}
Ted Kremenek94fd0b82010-02-16 08:33:59 +0000357
358void clang::bugreporter::registerFindLastStore(BugReporterContext& BRC,
359 const void *data,
360 const ExplodedNode* N) {
361
362 const MemRegion *R = static_cast<const MemRegion*>(data);
363
364 if (!R)
365 return;
366
367 const GRState *state = N->getState();
368 SVal V = state->getSVal(R);
369
370 if (V.isUnknown())
371 return;
372
373 BRC.addVisitor(new FindLastStoreBRVisitor(V, R));
374}
Ted Kremenekff7f7362010-03-20 18:02:01 +0000375
376
377namespace {
378class NilReceiverVisitor : public BugReporterVisitor {
379public:
380 NilReceiverVisitor() {}
381
382 void Profile(llvm::FoldingSetNodeID &ID) const {
383 static int x = 0;
384 ID.AddPointer(&x);
385 }
386
387 PathDiagnosticPiece* VisitNode(const ExplodedNode *N,
388 const ExplodedNode *PrevN,
389 BugReporterContext& BRC) {
390
391 const PostStmt *P = N->getLocationAs<PostStmt>();
392 if (!P)
393 return 0;
394 const ObjCMessageExpr *ME = P->getStmtAs<ObjCMessageExpr>();
395 if (!ME)
396 return 0;
Douglas Gregor04badcf2010-04-21 00:45:42 +0000397 const Expr *Receiver = ME->getInstanceReceiver();
Ted Kremenekff7f7362010-03-20 18:02:01 +0000398 if (!Receiver)
399 return 0;
400 const GRState *state = N->getState();
401 const SVal &V = state->getSVal(Receiver);
402 const DefinedOrUnknownSVal *DV = dyn_cast<DefinedOrUnknownSVal>(&V);
403 if (!DV)
404 return 0;
405 state = state->Assume(*DV, true);
406 if (state)
407 return 0;
408
409 // The receiver was nil, and hence the method was skipped.
410 // Register a BugReporterVisitor to issue a message telling us how
411 // the receiver was null.
412 bugreporter::registerTrackNullOrUndefValue(BRC, Receiver, N);
413 // Issue a message saying that the method was skipped.
414 PathDiagnosticLocation L(Receiver, BRC.getSourceManager());
415 return new PathDiagnosticEventPiece(L, "No method actually called "
416 "because the receiver is nil");
417 }
418};
419} // end anonymous namespace
420
421void clang::bugreporter::registerNilReceiverVisitor(BugReporterContext &BRC) {
422 BRC.addVisitor(new NilReceiverVisitor());
423}