blob: 6cf6d92efb844451cc4059f62822d928cf331fc8 [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"
Ted Kremenek1309f9a2010-01-25 04:41:41 +000019#include "clang/Checker/PathSensitive/GRState.h"
Ted Kremenek53500662009-07-22 17:55:28 +000020
21using namespace clang;
22
23//===----------------------------------------------------------------------===//
24// Utility functions.
25//===----------------------------------------------------------------------===//
26
Zhongxing Xuc5619d92009-08-06 01:32:16 +000027const Stmt *clang::bugreporter::GetDerefExpr(const ExplodedNode *N) {
Ted Kremenek53500662009-07-22 17:55:28 +000028 // Pattern match for a few useful cases (do something smarter later):
29 // a[0], p->f, *p
30 const Stmt *S = N->getLocationAs<PostStmt>()->getStmt();
Mike Stump1eb44332009-09-09 15:08:12 +000031
Ted Kremenek53500662009-07-22 17:55:28 +000032 if (const UnaryOperator *U = dyn_cast<UnaryOperator>(S)) {
33 if (U->getOpcode() == UnaryOperator::Deref)
34 return U->getSubExpr()->IgnoreParenCasts();
35 }
36 else if (const MemberExpr *ME = dyn_cast<MemberExpr>(S)) {
37 return ME->getBase()->IgnoreParenCasts();
38 }
39 else if (const ArraySubscriptExpr *AE = dyn_cast<ArraySubscriptExpr>(S)) {
40 // Retrieve the base for arrays since BasicStoreManager doesn't know how
41 // to reason about them.
42 return AE->getBase();
43 }
Mike Stump1eb44332009-09-09 15:08:12 +000044
45 return NULL;
Ted Kremenek53500662009-07-22 17:55:28 +000046}
47
48const Stmt*
Zhongxing Xuc5619d92009-08-06 01:32:16 +000049clang::bugreporter::GetReceiverExpr(const ExplodedNode *N){
Ted Kremenek53500662009-07-22 17:55:28 +000050 const Stmt *S = N->getLocationAs<PostStmt>()->getStmt();
51 if (const ObjCMessageExpr *ME = dyn_cast<ObjCMessageExpr>(S))
52 return ME->getReceiver();
53 return NULL;
54}
55
56const Stmt*
Zhongxing Xuc5619d92009-08-06 01:32:16 +000057clang::bugreporter::GetDenomExpr(const ExplodedNode *N) {
Zhongxing Xu6403b572009-09-02 13:26:26 +000058 const Stmt *S = N->getLocationAs<PreStmt>()->getStmt();
Ted Kremenek53500662009-07-22 17:55:28 +000059 if (const BinaryOperator *BE = dyn_cast<BinaryOperator>(S))
60 return BE->getRHS();
61 return NULL;
62}
63
64const Stmt*
Zhongxing Xuc5619d92009-08-06 01:32:16 +000065clang::bugreporter::GetCalleeExpr(const ExplodedNode *N) {
Zhongxing Xud99f3612009-09-02 08:10:35 +000066 // Callee is checked as a PreVisit to the CallExpr.
67 const Stmt *S = N->getLocationAs<PreStmt>()->getStmt();
Ted Kremenek53500662009-07-22 17:55:28 +000068 if (const CallExpr *CE = dyn_cast<CallExpr>(S))
69 return CE->getCallee();
70 return NULL;
71}
72
73const Stmt*
Zhongxing Xuc5619d92009-08-06 01:32:16 +000074clang::bugreporter::GetRetValExpr(const ExplodedNode *N) {
Ted Kremenek53500662009-07-22 17:55:28 +000075 const Stmt *S = N->getLocationAs<PostStmt>()->getStmt();
76 if (const ReturnStmt *RS = dyn_cast<ReturnStmt>(S))
77 return RS->getRetValue();
78 return NULL;
79}
80
81//===----------------------------------------------------------------------===//
82// Definitions for bug reporter visitors.
83//===----------------------------------------------------------------------===//
84
85namespace {
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +000086class FindLastStoreBRVisitor : public BugReporterVisitor {
Ted Kremenek53500662009-07-22 17:55:28 +000087 const MemRegion *R;
88 SVal V;
89 bool satisfied;
Zhongxing Xuc5619d92009-08-06 01:32:16 +000090 const ExplodedNode *StoreSite;
Ted Kremenek53500662009-07-22 17:55:28 +000091public:
92 FindLastStoreBRVisitor(SVal v, const MemRegion *r)
93 : R(r), V(v), satisfied(false), StoreSite(0) {}
Mike Stump1eb44332009-09-09 15:08:12 +000094
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()) {
Ted Kremenek53500662009-07-22 17:55:28 +0000106 if (const VarRegion *VR = dyn_cast<VarRegion>(R)) {
107 if (const PostStmt *P = Node->getLocationAs<PostStmt>())
108 if (const DeclStmt *DS = P->getStmtAs<DeclStmt>())
109 if (DS->getSingleDecl() == VR->getDecl()) {
110 Last = Node;
111 break;
112 }
113 }
Mike Stump1eb44332009-09-09 15:08:12 +0000114
Ted Kremenek13976632010-02-08 16:18:51 +0000115 if (Node->getState()->getSVal(R) != V)
Ted Kremenek53500662009-07-22 17:55:28 +0000116 break;
117 }
Mike Stump1eb44332009-09-09 15:08:12 +0000118
Ted Kremenek53500662009-07-22 17:55:28 +0000119 if (!Node || !Last) {
120 satisfied = true;
121 return NULL;
122 }
Mike Stump1eb44332009-09-09 15:08:12 +0000123
Ted Kremenek53500662009-07-22 17:55:28 +0000124 StoreSite = Last;
125 }
Mike Stump1eb44332009-09-09 15:08:12 +0000126
Ted Kremenek53500662009-07-22 17:55:28 +0000127 if (StoreSite != N)
128 return NULL;
Mike Stump1eb44332009-09-09 15:08:12 +0000129
Ted Kremenek53500662009-07-22 17:55:28 +0000130 satisfied = true;
131 std::string sbuf;
132 llvm::raw_string_ostream os(sbuf);
Mike Stump1eb44332009-09-09 15:08:12 +0000133
Ted Kremenek53500662009-07-22 17:55:28 +0000134 if (const PostStmt *PS = N->getLocationAs<PostStmt>()) {
135 if (const DeclStmt *DS = PS->getStmtAs<DeclStmt>()) {
Mike Stump1eb44332009-09-09 15:08:12 +0000136
Ted Kremenek53500662009-07-22 17:55:28 +0000137 if (const VarRegion *VR = dyn_cast<VarRegion>(R)) {
138 os << "Variable '" << VR->getDecl()->getNameAsString() << "' ";
139 }
140 else
141 return NULL;
Mike Stump1eb44332009-09-09 15:08:12 +0000142
Ted Kremenek53500662009-07-22 17:55:28 +0000143 if (isa<loc::ConcreteInt>(V)) {
144 bool b = false;
145 ASTContext &C = BRC.getASTContext();
146 if (R->isBoundable()) {
147 if (const TypedRegion *TR = dyn_cast<TypedRegion>(R)) {
148 if (TR->getValueType(C)->isObjCObjectPointerType()) {
149 os << "initialized to nil";
150 b = true;
151 }
152 }
153 }
Mike Stump1eb44332009-09-09 15:08:12 +0000154
Ted Kremenek53500662009-07-22 17:55:28 +0000155 if (!b)
156 os << "initialized to a null pointer value";
157 }
158 else if (isa<nonloc::ConcreteInt>(V)) {
159 os << "initialized to " << cast<nonloc::ConcreteInt>(V).getValue();
160 }
161 else if (V.isUndef()) {
162 if (isa<VarRegion>(R)) {
163 const VarDecl *VD = cast<VarDecl>(DS->getSingleDecl());
164 if (VD->getInit())
165 os << "initialized to a garbage value";
166 else
Mike Stump1eb44332009-09-09 15:08:12 +0000167 os << "declared without an initial value";
168 }
Ted Kremenek53500662009-07-22 17:55:28 +0000169 }
170 }
171 }
Mike Stump1eb44332009-09-09 15:08:12 +0000172
173 if (os.str().empty()) {
Ted Kremenek53500662009-07-22 17:55:28 +0000174 if (isa<loc::ConcreteInt>(V)) {
175 bool b = false;
176 ASTContext &C = BRC.getASTContext();
177 if (R->isBoundable()) {
178 if (const TypedRegion *TR = dyn_cast<TypedRegion>(R)) {
179 if (TR->getValueType(C)->isObjCObjectPointerType()) {
180 os << "nil object reference stored to ";
181 b = true;
182 }
183 }
184 }
Mike Stump1eb44332009-09-09 15:08:12 +0000185
Ted Kremenek53500662009-07-22 17:55:28 +0000186 if (!b)
187 os << "Null pointer value stored to ";
188 }
189 else if (V.isUndef()) {
190 os << "Uninitialized value stored to ";
191 }
Ted Kremenek592362b2009-08-18 01:05:30 +0000192 else if (isa<nonloc::ConcreteInt>(V)) {
193 os << "The value " << cast<nonloc::ConcreteInt>(V).getValue()
194 << " is assigned to ";
195 }
Ted Kremenek53500662009-07-22 17:55:28 +0000196 else
197 return NULL;
Mike Stump1eb44332009-09-09 15:08:12 +0000198
Ted Kremenek53500662009-07-22 17:55:28 +0000199 if (const VarRegion *VR = dyn_cast<VarRegion>(R)) {
200 os << '\'' << VR->getDecl()->getNameAsString() << '\'';
201 }
202 else
203 return NULL;
204 }
Mike Stump1eb44332009-09-09 15:08:12 +0000205
Ted Kremenek53500662009-07-22 17:55:28 +0000206 // FIXME: Refactor this into BugReporterContext.
Mike Stump1eb44332009-09-09 15:08:12 +0000207 const Stmt *S = 0;
Ted Kremenek53500662009-07-22 17:55:28 +0000208 ProgramPoint P = N->getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +0000209
Ted Kremenek53500662009-07-22 17:55:28 +0000210 if (BlockEdge *BE = dyn_cast<BlockEdge>(&P)) {
211 CFGBlock *BSrc = BE->getSrc();
212 S = BSrc->getTerminatorCondition();
213 }
214 else if (PostStmt *PS = dyn_cast<PostStmt>(&P)) {
215 S = PS->getStmt();
216 }
Mike Stump1eb44332009-09-09 15:08:12 +0000217
Ted Kremenek53500662009-07-22 17:55:28 +0000218 if (!S)
219 return NULL;
Mike Stump1eb44332009-09-09 15:08:12 +0000220
Ted Kremenek53500662009-07-22 17:55:28 +0000221 // Construct a new PathDiagnosticPiece.
222 PathDiagnosticLocation L(S, BRC.getSourceManager());
223 return new PathDiagnosticEventPiece(L, os.str());
224 }
225};
226
227
228static void registerFindLastStore(BugReporterContext& BRC, const MemRegion *R,
229 SVal V) {
230 BRC.addVisitor(new FindLastStoreBRVisitor(V, R));
231}
232
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +0000233class TrackConstraintBRVisitor : public BugReporterVisitor {
Ted Kremenek5b9bd212009-09-11 22:07:28 +0000234 DefinedSVal Constraint;
Ted Kremenek53500662009-07-22 17:55:28 +0000235 const bool Assumption;
236 bool isSatisfied;
237public:
Ted Kremenek5b9bd212009-09-11 22:07:28 +0000238 TrackConstraintBRVisitor(DefinedSVal constraint, bool assumption)
Ted Kremenek53500662009-07-22 17:55:28 +0000239 : Constraint(constraint), Assumption(assumption), isSatisfied(false) {}
Mike Stump1eb44332009-09-09 15:08:12 +0000240
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000241 PathDiagnosticPiece* VisitNode(const ExplodedNode *N,
242 const ExplodedNode *PrevN,
Ted Kremenek53500662009-07-22 17:55:28 +0000243 BugReporterContext& BRC) {
244 if (isSatisfied)
245 return NULL;
Mike Stump1eb44332009-09-09 15:08:12 +0000246
Ted Kremenek53500662009-07-22 17:55:28 +0000247 // Check if in the previous state it was feasible for this constraint
248 // to *not* be true.
Ted Kremenek5b9bd212009-09-11 22:07:28 +0000249 if (PrevN->getState()->Assume(Constraint, !Assumption)) {
Ted Kremenek53500662009-07-22 17:55:28 +0000250 isSatisfied = true;
Mike Stump1eb44332009-09-09 15:08:12 +0000251
Ted Kremenek53500662009-07-22 17:55:28 +0000252 // As a sanity check, make sure that the negation of the constraint
253 // was infeasible in the current state. If it is feasible, we somehow
254 // missed the transition point.
Ted Kremenek5b9bd212009-09-11 22:07:28 +0000255 if (N->getState()->Assume(Constraint, !Assumption))
Ted Kremenek53500662009-07-22 17:55:28 +0000256 return NULL;
Mike Stump1eb44332009-09-09 15:08:12 +0000257
Ted Kremenek53500662009-07-22 17:55:28 +0000258 // We found the transition point for the constraint. We now need to
Mike Stump1eb44332009-09-09 15:08:12 +0000259 // pretty-print the constraint. (work-in-progress)
Ted Kremenek2d46f4d2010-03-20 01:17:30 +0000260 llvm::SmallString<256> sbuf;
261 llvm::raw_svector_ostream os(sbuf);
Mike Stump1eb44332009-09-09 15:08:12 +0000262
Ted Kremenek53500662009-07-22 17:55:28 +0000263 if (isa<Loc>(Constraint)) {
264 os << "Assuming pointer value is ";
265 os << (Assumption ? "non-null" : "null");
266 }
Mike Stump1eb44332009-09-09 15:08:12 +0000267
Ted Kremenek53500662009-07-22 17:55:28 +0000268 if (os.str().empty())
269 return NULL;
Mike Stump1eb44332009-09-09 15:08:12 +0000270
Ted Kremenek53500662009-07-22 17:55:28 +0000271 // FIXME: Refactor this into BugReporterContext.
Mike Stump1eb44332009-09-09 15:08:12 +0000272 const Stmt *S = 0;
Ted Kremenek53500662009-07-22 17:55:28 +0000273 ProgramPoint P = N->getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +0000274
Ted Kremenek53500662009-07-22 17:55:28 +0000275 if (BlockEdge *BE = dyn_cast<BlockEdge>(&P)) {
276 CFGBlock *BSrc = BE->getSrc();
277 S = BSrc->getTerminatorCondition();
278 }
279 else if (PostStmt *PS = dyn_cast<PostStmt>(&P)) {
280 S = PS->getStmt();
281 }
Mike Stump1eb44332009-09-09 15:08:12 +0000282
Ted Kremenek53500662009-07-22 17:55:28 +0000283 if (!S)
284 return NULL;
Mike Stump1eb44332009-09-09 15:08:12 +0000285
Ted Kremenek53500662009-07-22 17:55:28 +0000286 // Construct a new PathDiagnosticPiece.
287 PathDiagnosticLocation L(S, BRC.getSourceManager());
288 return new PathDiagnosticEventPiece(L, os.str());
289 }
Mike Stump1eb44332009-09-09 15:08:12 +0000290
Ted Kremenek53500662009-07-22 17:55:28 +0000291 return NULL;
Mike Stump1eb44332009-09-09 15:08:12 +0000292 }
Ted Kremenek53500662009-07-22 17:55:28 +0000293};
294} // end anonymous namespace
295
Ted Kremenek5b9bd212009-09-11 22:07:28 +0000296static void registerTrackConstraint(BugReporterContext& BRC,
297 DefinedSVal Constraint,
Ted Kremenek53500662009-07-22 17:55:28 +0000298 bool Assumption) {
Mike Stump1eb44332009-09-09 15:08:12 +0000299 BRC.addVisitor(new TrackConstraintBRVisitor(Constraint, Assumption));
Ted Kremenek53500662009-07-22 17:55:28 +0000300}
301
302void clang::bugreporter::registerTrackNullOrUndefValue(BugReporterContext& BRC,
Ted Kremenek592362b2009-08-18 01:05:30 +0000303 const void *data,
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000304 const ExplodedNode* N) {
Mike Stump1eb44332009-09-09 15:08:12 +0000305
Ted Kremenek592362b2009-08-18 01:05:30 +0000306 const Stmt *S = static_cast<const Stmt*>(data);
Mike Stump1eb44332009-09-09 15:08:12 +0000307
Ted Kremenek53500662009-07-22 17:55:28 +0000308 if (!S)
309 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000310
Ted Kremenek53500662009-07-22 17:55:28 +0000311 GRStateManager &StateMgr = BRC.getStateManager();
Mike Stump1eb44332009-09-09 15:08:12 +0000312 const GRState *state = N->getState();
313
314 if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(S)) {
315 if (const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl())) {
Ted Kremenek53500662009-07-22 17:55:28 +0000316 const VarRegion *R =
Ted Kremenekd17da2b2009-08-21 22:28:32 +0000317 StateMgr.getRegionManager().getVarRegion(VD, N->getLocationContext());
Mike Stump1eb44332009-09-09 15:08:12 +0000318
Ted Kremenek53500662009-07-22 17:55:28 +0000319 // What did we load?
Ted Kremenek13976632010-02-08 16:18:51 +0000320 SVal V = state->getSVal(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000321
322 if (isa<loc::ConcreteInt>(V) || isa<nonloc::ConcreteInt>(V)
Ted Kremenek53500662009-07-22 17:55:28 +0000323 || V.isUndef()) {
Ted Kremenek94fd0b82010-02-16 08:33:59 +0000324 ::registerFindLastStore(BRC, R, V);
Ted Kremenek53500662009-07-22 17:55:28 +0000325 }
326 }
327 }
Mike Stump1eb44332009-09-09 15:08:12 +0000328
Ted Kremenek13976632010-02-08 16:18:51 +0000329 SVal V = state->getSValAsScalarOrLoc(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000330
Ted Kremenek53500662009-07-22 17:55:28 +0000331 // Uncomment this to find cases where we aren't properly getting the
332 // base value that was dereferenced.
333 // assert(!V.isUnknownOrUndef());
Mike Stump1eb44332009-09-09 15:08:12 +0000334
Ted Kremenek53500662009-07-22 17:55:28 +0000335 // Is it a symbolic value?
336 if (loc::MemRegionVal *L = dyn_cast<loc::MemRegionVal>(&V)) {
337 const SubRegion *R = cast<SubRegion>(L->getRegion());
338 while (R && !isa<SymbolicRegion>(R)) {
339 R = dyn_cast<SubRegion>(R->getSuperRegion());
340 }
Mike Stump1eb44332009-09-09 15:08:12 +0000341
Ted Kremenek53500662009-07-22 17:55:28 +0000342 if (R) {
343 assert(isa<SymbolicRegion>(R));
344 registerTrackConstraint(BRC, loc::MemRegionVal(R), false);
345 }
346 }
347}
Ted Kremenek94fd0b82010-02-16 08:33:59 +0000348
349void clang::bugreporter::registerFindLastStore(BugReporterContext& BRC,
350 const void *data,
351 const ExplodedNode* N) {
352
353 const MemRegion *R = static_cast<const MemRegion*>(data);
354
355 if (!R)
356 return;
357
358 const GRState *state = N->getState();
359 SVal V = state->getSVal(R);
360
361 if (V.isUnknown())
362 return;
363
364 BRC.addVisitor(new FindLastStoreBRVisitor(V, R));
365}
Ted Kremenek2d46f4d2010-03-20 01:17:30 +0000366
367namespace {
368class NilReceiverVisitor : public BugReporterVisitor {
369public:
370 NilReceiverVisitor() {}
371
372 PathDiagnosticPiece* VisitNode(const ExplodedNode *N,
373 const ExplodedNode *PrevN,
374 BugReporterContext& BRC) {
375
376 const PostStmt *P = N->getLocationAs<PostStmt>();
377 if (!P)
378 return 0;
379 const ObjCMessageExpr *ME = P->getStmtAs<ObjCMessageExpr>();
380 if (!ME)
381 return 0;
382 const Expr *Receiver = ME->getReceiver();
383 if (!Receiver)
384 return 0;
385 const GRState *state = N->getState();
386 const SVal &V = state->getSVal(Receiver);
387 const DefinedOrUnknownSVal *DV = dyn_cast<DefinedOrUnknownSVal>(&V);
388 if (!DV)
389 return 0;
390
391 state = state->Assume(*DV, true);
392 if (state)
393 return 0;
394
395 // The receiver was nil, and hence the method was skipped.
396 // Register a BugReporterVisitor to issue a message telling us how
397 // the receiver was null.
398 bugreporter::registerTrackNullOrUndefValue(BRC, Receiver, N);
399
400 //Issue a message saying that the method was skipped.
401 PathDiagnosticLocation L(Receiver, BRC.getSourceManager());
402 return new PathDiagnosticEventPiece(L, "No method actually called "
403 "because the receiver is nil");
404 }
405};
406} // end anonymous namespace
407
408void clang::bugreporter::registerNilReceiverVisitor(BugReporterContext &BRC) {
409 BRC.addVisitor(new NilReceiverVisitor());
410}