blob: b76ffb18a616381d78f81e4d5acdd44eaa9bf163 [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"
17#include "clang/Analysis/PathSensitive/BugReporter.h"
18#include "clang/Analysis/PathDiagnostic.h"
19#include "clang/Analysis/PathSensitive/GRState.h"
20
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 {
86class VISIBILITY_HIDDEN FindLastStoreBRVisitor : public BugReporterVisitor {
87 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()) {
Mike Stump1eb44332009-09-09 15:08:12 +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 Kremenek53500662009-07-22 17:55:28 +0000116 if (Node->getState()->getSVal(R) != V)
117 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;
132 std::string sbuf;
133 llvm::raw_string_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)) {
139 os << "Variable '" << VR->getDecl()->getNameAsString() << "' ";
140 }
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)) {
201 os << '\'' << VR->getDecl()->getNameAsString() << '\'';
202 }
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
234class VISIBILITY_HIDDEN TrackConstraintBRVisitor : public BugReporterVisitor {
235 SVal Constraint;
236 const bool Assumption;
237 bool isSatisfied;
238public:
239 TrackConstraintBRVisitor(SVal constraint, bool assumption)
240 : Constraint(constraint), Assumption(assumption), isSatisfied(false) {}
Mike Stump1eb44332009-09-09 15:08:12 +0000241
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000242 PathDiagnosticPiece* VisitNode(const ExplodedNode *N,
243 const ExplodedNode *PrevN,
Ted Kremenek53500662009-07-22 17:55:28 +0000244 BugReporterContext& BRC) {
245 if (isSatisfied)
246 return NULL;
Mike Stump1eb44332009-09-09 15:08:12 +0000247
Ted Kremenek53500662009-07-22 17:55:28 +0000248 // Check if in the previous state it was feasible for this constraint
249 // to *not* be true.
250 if (PrevN->getState()->assume(Constraint, !Assumption)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000251
Ted Kremenek53500662009-07-22 17:55:28 +0000252 isSatisfied = true;
Mike Stump1eb44332009-09-09 15:08:12 +0000253
Ted Kremenek53500662009-07-22 17:55:28 +0000254 // As a sanity check, make sure that the negation of the constraint
255 // was infeasible in the current state. If it is feasible, we somehow
256 // missed the transition point.
257 if (N->getState()->assume(Constraint, !Assumption))
258 return NULL;
Mike Stump1eb44332009-09-09 15:08:12 +0000259
Ted Kremenek53500662009-07-22 17:55:28 +0000260 // We found the transition point for the constraint. We now need to
Mike Stump1eb44332009-09-09 15:08:12 +0000261 // pretty-print the constraint. (work-in-progress)
Ted Kremenek53500662009-07-22 17:55:28 +0000262 std::string sbuf;
263 llvm::raw_string_ostream os(sbuf);
Mike Stump1eb44332009-09-09 15:08:12 +0000264
Ted Kremenek53500662009-07-22 17:55:28 +0000265 if (isa<Loc>(Constraint)) {
266 os << "Assuming pointer value is ";
267 os << (Assumption ? "non-null" : "null");
268 }
Mike Stump1eb44332009-09-09 15:08:12 +0000269
Ted Kremenek53500662009-07-22 17:55:28 +0000270 if (os.str().empty())
271 return NULL;
Mike Stump1eb44332009-09-09 15:08:12 +0000272
Ted Kremenek53500662009-07-22 17:55:28 +0000273 // FIXME: Refactor this into BugReporterContext.
Mike Stump1eb44332009-09-09 15:08:12 +0000274 const Stmt *S = 0;
Ted Kremenek53500662009-07-22 17:55:28 +0000275 ProgramPoint P = N->getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +0000276
Ted Kremenek53500662009-07-22 17:55:28 +0000277 if (BlockEdge *BE = dyn_cast<BlockEdge>(&P)) {
278 CFGBlock *BSrc = BE->getSrc();
279 S = BSrc->getTerminatorCondition();
280 }
281 else if (PostStmt *PS = dyn_cast<PostStmt>(&P)) {
282 S = PS->getStmt();
283 }
Mike Stump1eb44332009-09-09 15:08:12 +0000284
Ted Kremenek53500662009-07-22 17:55:28 +0000285 if (!S)
286 return NULL;
Mike Stump1eb44332009-09-09 15:08:12 +0000287
Ted Kremenek53500662009-07-22 17:55:28 +0000288 // Construct a new PathDiagnosticPiece.
289 PathDiagnosticLocation L(S, BRC.getSourceManager());
290 return new PathDiagnosticEventPiece(L, os.str());
291 }
Mike Stump1eb44332009-09-09 15:08:12 +0000292
Ted Kremenek53500662009-07-22 17:55:28 +0000293 return NULL;
Mike Stump1eb44332009-09-09 15:08:12 +0000294 }
Ted Kremenek53500662009-07-22 17:55:28 +0000295};
296} // end anonymous namespace
297
298static void registerTrackConstraint(BugReporterContext& BRC, SVal Constraint,
299 bool Assumption) {
Mike Stump1eb44332009-09-09 15:08:12 +0000300 BRC.addVisitor(new TrackConstraintBRVisitor(Constraint, Assumption));
Ted Kremenek53500662009-07-22 17:55:28 +0000301}
302
303void clang::bugreporter::registerTrackNullOrUndefValue(BugReporterContext& BRC,
Ted Kremenek592362b2009-08-18 01:05:30 +0000304 const void *data,
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000305 const ExplodedNode* N) {
Mike Stump1eb44332009-09-09 15:08:12 +0000306
Ted Kremenek592362b2009-08-18 01:05:30 +0000307 const Stmt *S = static_cast<const Stmt*>(data);
Mike Stump1eb44332009-09-09 15:08:12 +0000308
Ted Kremenek53500662009-07-22 17:55:28 +0000309 if (!S)
310 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000311
Ted Kremenek53500662009-07-22 17:55:28 +0000312 GRStateManager &StateMgr = BRC.getStateManager();
Mike Stump1eb44332009-09-09 15:08:12 +0000313 const GRState *state = N->getState();
314
315 if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(S)) {
316 if (const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl())) {
Ted Kremenek53500662009-07-22 17:55:28 +0000317 const VarRegion *R =
Ted Kremenekd17da2b2009-08-21 22:28:32 +0000318 StateMgr.getRegionManager().getVarRegion(VD, N->getLocationContext());
Mike Stump1eb44332009-09-09 15:08:12 +0000319
Ted Kremenek53500662009-07-22 17:55:28 +0000320 // What did we load?
321 SVal V = state->getSVal(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000322
323 if (isa<loc::ConcreteInt>(V) || isa<nonloc::ConcreteInt>(V)
Ted Kremenek53500662009-07-22 17:55:28 +0000324 || V.isUndef()) {
325 registerFindLastStore(BRC, R, V);
326 }
327 }
328 }
Mike Stump1eb44332009-09-09 15:08:12 +0000329
Ted Kremenek53500662009-07-22 17:55:28 +0000330 SVal V = state->getSValAsScalarOrLoc(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000331
Ted Kremenek53500662009-07-22 17:55:28 +0000332 // Uncomment this to find cases where we aren't properly getting the
333 // base value that was dereferenced.
334 // assert(!V.isUnknownOrUndef());
Mike Stump1eb44332009-09-09 15:08:12 +0000335
Ted Kremenek53500662009-07-22 17:55:28 +0000336 // Is it a symbolic value?
337 if (loc::MemRegionVal *L = dyn_cast<loc::MemRegionVal>(&V)) {
338 const SubRegion *R = cast<SubRegion>(L->getRegion());
339 while (R && !isa<SymbolicRegion>(R)) {
340 R = dyn_cast<SubRegion>(R->getSuperRegion());
341 }
Mike Stump1eb44332009-09-09 15:08:12 +0000342
Ted Kremenek53500662009-07-22 17:55:28 +0000343 if (R) {
344 assert(isa<SymbolicRegion>(R));
345 registerTrackConstraint(BRC, loc::MemRegionVal(R), false);
346 }
347 }
348}