blob: 77501bbba226fa94858620fbe9df5886d1d1a0e6 [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 }
192 else
193 return NULL;
194
195 if (const VarRegion *VR = dyn_cast<VarRegion>(R)) {
196 os << '\'' << VR->getDecl()->getNameAsString() << '\'';
197 }
198 else
199 return NULL;
200 }
201
202 // FIXME: Refactor this into BugReporterContext.
Ted Kremenek5f85e172009-07-22 22:35:28 +0000203 const Stmt *S = 0;
Ted Kremenek53500662009-07-22 17:55:28 +0000204 ProgramPoint P = N->getLocation();
205
206 if (BlockEdge *BE = dyn_cast<BlockEdge>(&P)) {
207 CFGBlock *BSrc = BE->getSrc();
208 S = BSrc->getTerminatorCondition();
209 }
210 else if (PostStmt *PS = dyn_cast<PostStmt>(&P)) {
211 S = PS->getStmt();
212 }
213
214 if (!S)
215 return NULL;
216
217 // Construct a new PathDiagnosticPiece.
218 PathDiagnosticLocation L(S, BRC.getSourceManager());
219 return new PathDiagnosticEventPiece(L, os.str());
220 }
221};
222
223
224static void registerFindLastStore(BugReporterContext& BRC, const MemRegion *R,
225 SVal V) {
226 BRC.addVisitor(new FindLastStoreBRVisitor(V, R));
227}
228
229class VISIBILITY_HIDDEN TrackConstraintBRVisitor : public BugReporterVisitor {
230 SVal Constraint;
231 const bool Assumption;
232 bool isSatisfied;
233public:
234 TrackConstraintBRVisitor(SVal constraint, bool assumption)
235 : Constraint(constraint), Assumption(assumption), isSatisfied(false) {}
236
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000237 PathDiagnosticPiece* VisitNode(const ExplodedNode *N,
238 const ExplodedNode *PrevN,
Ted Kremenek53500662009-07-22 17:55:28 +0000239 BugReporterContext& BRC) {
240 if (isSatisfied)
241 return NULL;
242
243 // Check if in the previous state it was feasible for this constraint
244 // to *not* be true.
245 if (PrevN->getState()->assume(Constraint, !Assumption)) {
246
247 isSatisfied = true;
248
249 // As a sanity check, make sure that the negation of the constraint
250 // was infeasible in the current state. If it is feasible, we somehow
251 // missed the transition point.
252 if (N->getState()->assume(Constraint, !Assumption))
253 return NULL;
254
255 // We found the transition point for the constraint. We now need to
256 // pretty-print the constraint. (work-in-progress)
257 std::string sbuf;
258 llvm::raw_string_ostream os(sbuf);
259
260 if (isa<Loc>(Constraint)) {
261 os << "Assuming pointer value is ";
262 os << (Assumption ? "non-null" : "null");
263 }
264
265 if (os.str().empty())
266 return NULL;
267
268 // FIXME: Refactor this into BugReporterContext.
Ted Kremenek5f85e172009-07-22 22:35:28 +0000269 const Stmt *S = 0;
Ted Kremenek53500662009-07-22 17:55:28 +0000270 ProgramPoint P = N->getLocation();
271
272 if (BlockEdge *BE = dyn_cast<BlockEdge>(&P)) {
273 CFGBlock *BSrc = BE->getSrc();
274 S = BSrc->getTerminatorCondition();
275 }
276 else if (PostStmt *PS = dyn_cast<PostStmt>(&P)) {
277 S = PS->getStmt();
278 }
279
280 if (!S)
281 return NULL;
282
283 // Construct a new PathDiagnosticPiece.
284 PathDiagnosticLocation L(S, BRC.getSourceManager());
285 return new PathDiagnosticEventPiece(L, os.str());
286 }
287
288 return NULL;
289 }
290};
291} // end anonymous namespace
292
293static void registerTrackConstraint(BugReporterContext& BRC, SVal Constraint,
294 bool Assumption) {
295 BRC.addVisitor(new TrackConstraintBRVisitor(Constraint, Assumption));
296}
297
298void clang::bugreporter::registerTrackNullOrUndefValue(BugReporterContext& BRC,
299 const Stmt *S,
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000300 const ExplodedNode* N) {
Ted Kremenek53500662009-07-22 17:55:28 +0000301
302 if (!S)
303 return;
304
305 GRStateManager &StateMgr = BRC.getStateManager();
306 const GRState *state = N->getState();
307
308 if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(S)) {
309 if (const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl())) {
310 const VarRegion *R =
311 StateMgr.getRegionManager().getVarRegion(VD);
312
313 // What did we load?
314 SVal V = state->getSVal(S);
315
316 if (isa<loc::ConcreteInt>(V) || isa<nonloc::ConcreteInt>(V)
317 || V.isUndef()) {
318 registerFindLastStore(BRC, R, V);
319 }
320 }
321 }
322
323 SVal V = state->getSValAsScalarOrLoc(S);
324
325 // Uncomment this to find cases where we aren't properly getting the
326 // base value that was dereferenced.
327 // assert(!V.isUnknownOrUndef());
328
329 // Is it a symbolic value?
330 if (loc::MemRegionVal *L = dyn_cast<loc::MemRegionVal>(&V)) {
331 const SubRegion *R = cast<SubRegion>(L->getRegion());
332 while (R && !isa<SymbolicRegion>(R)) {
333 R = dyn_cast<SubRegion>(R->getSuperRegion());
334 }
335
336 if (R) {
337 assert(isa<SymbolicRegion>(R));
338 registerTrackConstraint(BRC, loc::MemRegionVal(R), false);
339 }
340 }
341}