blob: fce31e70f0b2b41e5fed83fe862bfb2daee2a2b8 [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) {
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) {}
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) {
98
99 if (satisfied)
100 return NULL;
101
102 if (!StoreSite) {
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000103 const ExplodedNode *Node = N, *Last = NULL;
Ted Kremenek53500662009-07-22 17:55:28 +0000104
105 for ( ; Node ; Last = Node, Node = Node->getFirstPred()) {
106
107 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 }
115
116 if (Node->getState()->getSVal(R) != V)
117 break;
118 }
119
120 if (!Node || !Last) {
121 satisfied = true;
122 return NULL;
123 }
124
125 StoreSite = Last;
126 }
127
128 if (StoreSite != N)
129 return NULL;
130
131 satisfied = true;
132 std::string sbuf;
133 llvm::raw_string_ostream os(sbuf);
134
135 if (const PostStmt *PS = N->getLocationAs<PostStmt>()) {
136 if (const DeclStmt *DS = PS->getStmtAs<DeclStmt>()) {
137
138 if (const VarRegion *VR = dyn_cast<VarRegion>(R)) {
139 os << "Variable '" << VR->getDecl()->getNameAsString() << "' ";
140 }
141 else
142 return NULL;
143
144 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 }
155
156 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
168 os << "declared without an initial value";
169 }
170 }
171 }
172 }
173
174 if (os.str().empty()) {
175 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 }
186
187 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;
199
200 if (const VarRegion *VR = dyn_cast<VarRegion>(R)) {
201 os << '\'' << VR->getDecl()->getNameAsString() << '\'';
202 }
203 else
204 return NULL;
205 }
206
207 // FIXME: Refactor this into BugReporterContext.
Ted Kremenek5f85e172009-07-22 22:35:28 +0000208 const Stmt *S = 0;
Ted Kremenek53500662009-07-22 17:55:28 +0000209 ProgramPoint P = N->getLocation();
210
211 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 }
218
219 if (!S)
220 return NULL;
221
222 // 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) {}
241
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;
247
248 // Check if in the previous state it was feasible for this constraint
249 // to *not* be true.
250 if (PrevN->getState()->assume(Constraint, !Assumption)) {
251
252 isSatisfied = true;
253
254 // 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;
259
260 // We found the transition point for the constraint. We now need to
261 // pretty-print the constraint. (work-in-progress)
262 std::string sbuf;
263 llvm::raw_string_ostream os(sbuf);
264
265 if (isa<Loc>(Constraint)) {
266 os << "Assuming pointer value is ";
267 os << (Assumption ? "non-null" : "null");
268 }
269
270 if (os.str().empty())
271 return NULL;
272
273 // FIXME: Refactor this into BugReporterContext.
Ted Kremenek5f85e172009-07-22 22:35:28 +0000274 const Stmt *S = 0;
Ted Kremenek53500662009-07-22 17:55:28 +0000275 ProgramPoint P = N->getLocation();
276
277 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 }
284
285 if (!S)
286 return NULL;
287
288 // Construct a new PathDiagnosticPiece.
289 PathDiagnosticLocation L(S, BRC.getSourceManager());
290 return new PathDiagnosticEventPiece(L, os.str());
291 }
292
293 return NULL;
294 }
295};
296} // end anonymous namespace
297
298static void registerTrackConstraint(BugReporterContext& BRC, SVal Constraint,
299 bool Assumption) {
300 BRC.addVisitor(new TrackConstraintBRVisitor(Constraint, Assumption));
301}
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) {
Ted Kremenek53500662009-07-22 17:55:28 +0000306
Ted Kremenek592362b2009-08-18 01:05:30 +0000307 const Stmt *S = static_cast<const Stmt*>(data);
308
Ted Kremenek53500662009-07-22 17:55:28 +0000309 if (!S)
310 return;
311
312 GRStateManager &StateMgr = BRC.getStateManager();
313 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())) {
317 const VarRegion *R =
Ted Kremenekd17da2b2009-08-21 22:28:32 +0000318 StateMgr.getRegionManager().getVarRegion(VD, N->getLocationContext());
Ted Kremenek53500662009-07-22 17:55:28 +0000319
320 // What did we load?
321 SVal V = state->getSVal(S);
322
323 if (isa<loc::ConcreteInt>(V) || isa<nonloc::ConcreteInt>(V)
324 || V.isUndef()) {
325 registerFindLastStore(BRC, R, V);
326 }
327 }
328 }
329
330 SVal V = state->getSValAsScalarOrLoc(S);
331
332 // Uncomment this to find cases where we aren't properly getting the
333 // base value that was dereferenced.
334 // assert(!V.isUnknownOrUndef());
335
336 // 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 }
342
343 if (R) {
344 assert(isa<SymbolicRegion>(R));
345 registerTrackConstraint(BRC, loc::MemRegionVal(R), false);
346 }
347 }
348}