blob: 604542b2c1394f0d9239d2fddb155927467b4b7f [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();
31
32 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 }
44
45 return NULL;
46}
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) {
Ted Kremenek53500662009-07-22 17:55:28 +000058 const Stmt *S = N->getLocationAs<PostStmt>()->getStmt();
59 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) {
Ted Kremenek53500662009-07-22 17:55:28 +000066 const Stmt *S = N->getLocationAs<PostStmt>()->getStmt();
67 if (const CallExpr *CE = dyn_cast<CallExpr>(S))
68 return CE->getCallee();
69 return NULL;
70}
71
72const Stmt*
Zhongxing Xuc5619d92009-08-06 01:32:16 +000073clang::bugreporter::GetRetValExpr(const ExplodedNode *N) {
Ted Kremenek53500662009-07-22 17:55:28 +000074 const Stmt *S = N->getLocationAs<PostStmt>()->getStmt();
75 if (const ReturnStmt *RS = dyn_cast<ReturnStmt>(S))
76 return RS->getRetValue();
77 return NULL;
78}
79
80//===----------------------------------------------------------------------===//
81// Definitions for bug reporter visitors.
82//===----------------------------------------------------------------------===//
83
84namespace {
85class VISIBILITY_HIDDEN FindLastStoreBRVisitor : public BugReporterVisitor {
86 const MemRegion *R;
87 SVal V;
88 bool satisfied;
Zhongxing Xuc5619d92009-08-06 01:32:16 +000089 const ExplodedNode *StoreSite;
Ted Kremenek53500662009-07-22 17:55:28 +000090public:
91 FindLastStoreBRVisitor(SVal v, const MemRegion *r)
92 : R(r), V(v), satisfied(false), StoreSite(0) {}
93
Zhongxing Xuc5619d92009-08-06 01:32:16 +000094 PathDiagnosticPiece* VisitNode(const ExplodedNode *N,
95 const ExplodedNode *PrevN,
Ted Kremenek53500662009-07-22 17:55:28 +000096 BugReporterContext& BRC) {
97
98 if (satisfied)
99 return NULL;
100
101 if (!StoreSite) {
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000102 const ExplodedNode *Node = N, *Last = NULL;
Ted Kremenek53500662009-07-22 17:55:28 +0000103
104 for ( ; Node ; Last = Node, Node = Node->getFirstPred()) {
105
106 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 }
114
115 if (Node->getState()->getSVal(R) != V)
116 break;
117 }
118
119 if (!Node || !Last) {
120 satisfied = true;
121 return NULL;
122 }
123
124 StoreSite = Last;
125 }
126
127 if (StoreSite != N)
128 return NULL;
129
130 satisfied = true;
131 std::string sbuf;
132 llvm::raw_string_ostream os(sbuf);
133
134 if (const PostStmt *PS = N->getLocationAs<PostStmt>()) {
135 if (const DeclStmt *DS = PS->getStmtAs<DeclStmt>()) {
136
137 if (const VarRegion *VR = dyn_cast<VarRegion>(R)) {
138 os << "Variable '" << VR->getDecl()->getNameAsString() << "' ";
139 }
140 else
141 return NULL;
142
143 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 }
154
155 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
167 os << "declared without an initial value";
168 }
169 }
170 }
171 }
172
173 if (os.str().empty()) {
174 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 }
185
186 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;
198
199 if (const VarRegion *VR = dyn_cast<VarRegion>(R)) {
200 os << '\'' << VR->getDecl()->getNameAsString() << '\'';
201 }
202 else
203 return NULL;
204 }
205
206 // FIXME: Refactor this into BugReporterContext.
Ted Kremenek5f85e172009-07-22 22:35:28 +0000207 const Stmt *S = 0;
Ted Kremenek53500662009-07-22 17:55:28 +0000208 ProgramPoint P = N->getLocation();
209
210 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 }
217
218 if (!S)
219 return NULL;
220
221 // 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
233class VISIBILITY_HIDDEN TrackConstraintBRVisitor : public BugReporterVisitor {
234 SVal Constraint;
235 const bool Assumption;
236 bool isSatisfied;
237public:
238 TrackConstraintBRVisitor(SVal constraint, bool assumption)
239 : Constraint(constraint), Assumption(assumption), isSatisfied(false) {}
240
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;
246
247 // Check if in the previous state it was feasible for this constraint
248 // to *not* be true.
249 if (PrevN->getState()->assume(Constraint, !Assumption)) {
250
251 isSatisfied = true;
252
253 // As a sanity check, make sure that the negation of the constraint
254 // was infeasible in the current state. If it is feasible, we somehow
255 // missed the transition point.
256 if (N->getState()->assume(Constraint, !Assumption))
257 return NULL;
258
259 // We found the transition point for the constraint. We now need to
260 // pretty-print the constraint. (work-in-progress)
261 std::string sbuf;
262 llvm::raw_string_ostream os(sbuf);
263
264 if (isa<Loc>(Constraint)) {
265 os << "Assuming pointer value is ";
266 os << (Assumption ? "non-null" : "null");
267 }
268
269 if (os.str().empty())
270 return NULL;
271
272 // FIXME: Refactor this into BugReporterContext.
Ted Kremenek5f85e172009-07-22 22:35:28 +0000273 const Stmt *S = 0;
Ted Kremenek53500662009-07-22 17:55:28 +0000274 ProgramPoint P = N->getLocation();
275
276 if (BlockEdge *BE = dyn_cast<BlockEdge>(&P)) {
277 CFGBlock *BSrc = BE->getSrc();
278 S = BSrc->getTerminatorCondition();
279 }
280 else if (PostStmt *PS = dyn_cast<PostStmt>(&P)) {
281 S = PS->getStmt();
282 }
283
284 if (!S)
285 return NULL;
286
287 // Construct a new PathDiagnosticPiece.
288 PathDiagnosticLocation L(S, BRC.getSourceManager());
289 return new PathDiagnosticEventPiece(L, os.str());
290 }
291
292 return NULL;
293 }
294};
295} // end anonymous namespace
296
297static void registerTrackConstraint(BugReporterContext& BRC, SVal Constraint,
298 bool Assumption) {
299 BRC.addVisitor(new TrackConstraintBRVisitor(Constraint, Assumption));
300}
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) {
Ted Kremenek53500662009-07-22 17:55:28 +0000305
Ted Kremenek592362b2009-08-18 01:05:30 +0000306 const Stmt *S = static_cast<const Stmt*>(data);
307
Ted Kremenek53500662009-07-22 17:55:28 +0000308 if (!S)
309 return;
310
311 GRStateManager &StateMgr = BRC.getStateManager();
312 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())) {
316 const VarRegion *R =
317 StateMgr.getRegionManager().getVarRegion(VD);
318
319 // What did we load?
320 SVal V = state->getSVal(S);
321
322 if (isa<loc::ConcreteInt>(V) || isa<nonloc::ConcreteInt>(V)
323 || V.isUndef()) {
324 registerFindLastStore(BRC, R, V);
325 }
326 }
327 }
328
329 SVal V = state->getSValAsScalarOrLoc(S);
330
331 // Uncomment this to find cases where we aren't properly getting the
332 // base value that was dereferenced.
333 // assert(!V.isUnknownOrUndef());
334
335 // 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 }
341
342 if (R) {
343 assert(isa<SymbolicRegion>(R));
344 registerTrackConstraint(BRC, loc::MemRegionVal(R), false);
345 }
346 }
347}