blob: bb35274cb552bc8072ec4b34dc310726a9aa24e5 [file] [log] [blame]
Ted Kremenek78d46242008-07-22 16:21:24 +00001//=-- GRExprEngineInternalChecks.cpp - Builtin GRExprEngine Checks---*- 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 the BugType classes used by GRExprEngine to report
11// bugs derived from builtin checks in the path-sensitive engine.
12//
13//===----------------------------------------------------------------------===//
14
15#include "clang/Analysis/PathSensitive/BugReporter.h"
16#include "clang/Analysis/PathSensitive/GRExprEngine.h"
Ted Kremenekdd986cc2009-05-07 00:45:33 +000017#include "clang/Analysis/PathDiagnostic.h"
Ted Kremenek8aed8062008-10-31 00:13:20 +000018#include "clang/Basic/SourceManager.h"
Ted Kremenek78d46242008-07-22 16:21:24 +000019#include "llvm/Support/Compiler.h"
Ted Kremenekad51a602008-10-31 00:18:30 +000020#include "llvm/Support/raw_ostream.h"
Ted Kremenek78d46242008-07-22 16:21:24 +000021
22using namespace clang;
23
24//===----------------------------------------------------------------------===//
25// Utility functions.
26//===----------------------------------------------------------------------===//
27
28template <typename ITERATOR> inline
Ted Kremenek4adc81e2008-08-13 04:27:00 +000029ExplodedNode<GRState>* GetNode(ITERATOR I) {
Ted Kremenek78d46242008-07-22 16:21:24 +000030 return *I;
31}
32
33template <> inline
Ted Kremenek4adc81e2008-08-13 04:27:00 +000034ExplodedNode<GRState>* GetNode(GRExprEngine::undef_arg_iterator I) {
Ted Kremenek78d46242008-07-22 16:21:24 +000035 return I->first;
36}
37
38//===----------------------------------------------------------------------===//
Ted Kremenekdd986cc2009-05-07 00:45:33 +000039// Forward declarations for bug reporter visitors.
40//===----------------------------------------------------------------------===//
41
Ted Kremenek85ac9342009-05-15 05:25:09 +000042static const Stmt *GetDerefExpr(const ExplodedNode<GRState> *N);
43static const Stmt *GetReceiverExpr(const ExplodedNode<GRState> *N);
44static const Stmt *GetDenomExpr(const ExplodedNode<GRState> *N);
45static const Stmt *GetCalleeExpr(const ExplodedNode<GRState> *N);
46static const Stmt *GetRetValExpr(const ExplodedNode<GRState> *N);
47
Ted Kremenek0c313172009-05-13 19:16:35 +000048static void registerTrackNullOrUndefValue(BugReporterContext& BRC,
Ted Kremenek85ac9342009-05-15 05:25:09 +000049 const Stmt *ValExpr,
50 const ExplodedNode<GRState>* N);
Ted Kremenekdd986cc2009-05-07 00:45:33 +000051
52//===----------------------------------------------------------------------===//
Ted Kremenek78d46242008-07-22 16:21:24 +000053// Bug Descriptions.
54//===----------------------------------------------------------------------===//
55
56namespace {
Ted Kremenekdd986cc2009-05-07 00:45:33 +000057
Ted Kremenek0c313172009-05-13 19:16:35 +000058class VISIBILITY_HIDDEN BuiltinBugReport : public RangedBugReport {
Ted Kremenekdd986cc2009-05-07 00:45:33 +000059public:
60 BuiltinBugReport(BugType& bt, const char* desc,
Ted Kremenek0c313172009-05-13 19:16:35 +000061 ExplodedNode<GRState> *n)
62 : RangedBugReport(bt, desc, n) {}
Ted Kremenekdd986cc2009-05-07 00:45:33 +000063
Ted Kremenek85ac9342009-05-15 05:25:09 +000064 BuiltinBugReport(BugType& bt, const char *shortDesc, const char *desc,
65 ExplodedNode<GRState> *n)
66 : RangedBugReport(bt, shortDesc, desc, n) {}
67
Ted Kremenekdd986cc2009-05-07 00:45:33 +000068 void registerInitialVisitors(BugReporterContext& BRC,
69 const ExplodedNode<GRState>* N);
70};
71
Ted Kremenekcf118d42009-02-04 23:49:09 +000072class VISIBILITY_HIDDEN BuiltinBug : public BugType {
73 GRExprEngine &Eng;
Ted Kremenek159d2482008-12-09 00:44:16 +000074protected:
Ted Kremenekcf118d42009-02-04 23:49:09 +000075 const std::string desc;
Ted Kremenek78d46242008-07-22 16:21:24 +000076public:
Ted Kremenekcf118d42009-02-04 23:49:09 +000077 BuiltinBug(GRExprEngine *eng, const char* n, const char* d)
Ted Kremenek0c313172009-05-13 19:16:35 +000078 : BugType(n, "Logic errors"), Eng(*eng), desc(d) {}
Ted Kremenekcf118d42009-02-04 23:49:09 +000079
80 BuiltinBug(GRExprEngine *eng, const char* n)
Ted Kremenek0c313172009-05-13 19:16:35 +000081 : BugType(n, "Logic errors"), Eng(*eng), desc(n) {}
Ted Kremenek22bda882008-07-31 20:31:27 +000082
Ted Kremenekcf118d42009-02-04 23:49:09 +000083 virtual void FlushReportsImpl(BugReporter& BR, GRExprEngine& Eng) = 0;
84
85 void FlushReports(BugReporter& BR) { FlushReportsImpl(BR, Eng); }
Ted Kremenek78d46242008-07-22 16:21:24 +000086
Ted Kremenekdd986cc2009-05-07 00:45:33 +000087 virtual void registerInitialVisitors(BugReporterContext& BRC,
88 const ExplodedNode<GRState>* N,
89 BuiltinBugReport *R) {}
90
91 template <typename ITER> void Emit(BugReporter& BR, ITER I, ITER E);
Ted Kremenek78d46242008-07-22 16:21:24 +000092};
93
Ted Kremenekdd986cc2009-05-07 00:45:33 +000094
95template <typename ITER>
96void BuiltinBug::Emit(BugReporter& BR, ITER I, ITER E) {
97 for (; I != E; ++I) BR.EmitReport(new BuiltinBugReport(*this, desc.c_str(),
98 GetNode(I)));
99}
100
101void BuiltinBugReport::registerInitialVisitors(BugReporterContext& BRC,
102 const ExplodedNode<GRState>* N) {
103 static_cast<BuiltinBug&>(getBugType()).registerInitialVisitors(BRC, N, this);
104}
105
Ted Kremenek78d46242008-07-22 16:21:24 +0000106class VISIBILITY_HIDDEN NullDeref : public BuiltinBug {
107public:
Ted Kremenekcf118d42009-02-04 23:49:09 +0000108 NullDeref(GRExprEngine* eng)
Ted Kremenek0fa96542009-04-07 04:54:31 +0000109 : BuiltinBug(eng,"Null dereference", "Dereference of null pointer") {}
Ted Kremenek78d46242008-07-22 16:21:24 +0000110
Ted Kremenekcf118d42009-02-04 23:49:09 +0000111 void FlushReportsImpl(BugReporter& BR, GRExprEngine& Eng) {
Ted Kremenek78d46242008-07-22 16:21:24 +0000112 Emit(BR, Eng.null_derefs_begin(), Eng.null_derefs_end());
113 }
Ted Kremenekdd986cc2009-05-07 00:45:33 +0000114
115 void registerInitialVisitors(BugReporterContext& BRC,
116 const ExplodedNode<GRState>* N,
117 BuiltinBugReport *R) {
Ted Kremenek85ac9342009-05-15 05:25:09 +0000118 registerTrackNullOrUndefValue(BRC, GetDerefExpr(N), N);
Ted Kremenekdd986cc2009-05-07 00:45:33 +0000119 }
Ted Kremenek78d46242008-07-22 16:21:24 +0000120};
121
Ted Kremenek0c313172009-05-13 19:16:35 +0000122class VISIBILITY_HIDDEN NilReceiverStructRet : public BuiltinBug {
Ted Kremenek21fe8372009-02-19 04:06:22 +0000123public:
124 NilReceiverStructRet(GRExprEngine* eng) :
Ted Kremenek0c313172009-05-13 19:16:35 +0000125 BuiltinBug(eng, "'nil' receiver with struct return type") {}
Ted Kremenek21fe8372009-02-19 04:06:22 +0000126
Ted Kremenek0c313172009-05-13 19:16:35 +0000127 void FlushReportsImpl(BugReporter& BR, GRExprEngine& Eng) {
Ted Kremenek21fe8372009-02-19 04:06:22 +0000128 for (GRExprEngine::nil_receiver_struct_ret_iterator
129 I=Eng.nil_receiver_struct_ret_begin(),
130 E=Eng.nil_receiver_struct_ret_end(); I!=E; ++I) {
131
132 std::string sbuf;
133 llvm::raw_string_ostream os(sbuf);
134 PostStmt P = cast<PostStmt>((*I)->getLocation());
135 ObjCMessageExpr *ME = cast<ObjCMessageExpr>(P.getStmt());
136 os << "The receiver in the message expression is 'nil' and results in the"
137 " returned value (of type '"
138 << ME->getType().getAsString()
139 << "') to be garbage or otherwise undefined.";
140
Ted Kremenek0c313172009-05-13 19:16:35 +0000141 BuiltinBugReport *R = new BuiltinBugReport(*this, os.str().c_str(), *I);
Ted Kremenek21fe8372009-02-19 04:06:22 +0000142 R->addRange(ME->getReceiver()->getSourceRange());
143 BR.EmitReport(R);
144 }
145 }
Ted Kremenek0c313172009-05-13 19:16:35 +0000146
147 void registerInitialVisitors(BugReporterContext& BRC,
148 const ExplodedNode<GRState>* N,
149 BuiltinBugReport *R) {
Ted Kremenek85ac9342009-05-15 05:25:09 +0000150 registerTrackNullOrUndefValue(BRC, GetReceiverExpr(N), N);
Ted Kremenek0c313172009-05-13 19:16:35 +0000151 }
Ted Kremenek21fe8372009-02-19 04:06:22 +0000152};
Ted Kremenek899b3de2009-04-08 03:07:17 +0000153
Ted Kremenek0c313172009-05-13 19:16:35 +0000154class VISIBILITY_HIDDEN NilReceiverLargerThanVoidPtrRet : public BuiltinBug {
Ted Kremenek899b3de2009-04-08 03:07:17 +0000155public:
156 NilReceiverLargerThanVoidPtrRet(GRExprEngine* eng) :
Ted Kremenek0c313172009-05-13 19:16:35 +0000157 BuiltinBug(eng,
158 "'nil' receiver with return type larger than sizeof(void *)") {}
Ted Kremenek899b3de2009-04-08 03:07:17 +0000159
Ted Kremenek0c313172009-05-13 19:16:35 +0000160 void FlushReportsImpl(BugReporter& BR, GRExprEngine& Eng) {
Ted Kremenek899b3de2009-04-08 03:07:17 +0000161 for (GRExprEngine::nil_receiver_larger_than_voidptr_ret_iterator
162 I=Eng.nil_receiver_larger_than_voidptr_ret_begin(),
163 E=Eng.nil_receiver_larger_than_voidptr_ret_end(); I!=E; ++I) {
164
165 std::string sbuf;
166 llvm::raw_string_ostream os(sbuf);
167 PostStmt P = cast<PostStmt>((*I)->getLocation());
168 ObjCMessageExpr *ME = cast<ObjCMessageExpr>(P.getStmt());
169 os << "The receiver in the message expression is 'nil' and results in the"
170 " returned value (of type '"
171 << ME->getType().getAsString()
172 << "' and of size "
173 << Eng.getContext().getTypeSize(ME->getType()) / 8
174 << " bytes) to be garbage or otherwise undefined.";
175
Ted Kremenek0c313172009-05-13 19:16:35 +0000176 BuiltinBugReport *R = new BuiltinBugReport(*this, os.str().c_str(), *I);
Ted Kremenek899b3de2009-04-08 03:07:17 +0000177 R->addRange(ME->getReceiver()->getSourceRange());
178 BR.EmitReport(R);
179 }
Ted Kremenek0c313172009-05-13 19:16:35 +0000180 }
181 void registerInitialVisitors(BugReporterContext& BRC,
182 const ExplodedNode<GRState>* N,
183 BuiltinBugReport *R) {
Ted Kremenek85ac9342009-05-15 05:25:09 +0000184 registerTrackNullOrUndefValue(BRC, GetReceiverExpr(N), N);
Ted Kremenek899b3de2009-04-08 03:07:17 +0000185 }
186};
Ted Kremenek21fe8372009-02-19 04:06:22 +0000187
Ted Kremenek78d46242008-07-22 16:21:24 +0000188class VISIBILITY_HIDDEN UndefinedDeref : public BuiltinBug {
189public:
Ted Kremenekcf118d42009-02-04 23:49:09 +0000190 UndefinedDeref(GRExprEngine* eng)
Ted Kremenek17a8e072009-03-01 05:43:22 +0000191 : BuiltinBug(eng,"Dereference of undefined pointer value") {}
Ted Kremenek78d46242008-07-22 16:21:24 +0000192
Ted Kremenekcf118d42009-02-04 23:49:09 +0000193 void FlushReportsImpl(BugReporter& BR, GRExprEngine& Eng) {
Ted Kremenek78d46242008-07-22 16:21:24 +0000194 Emit(BR, Eng.undef_derefs_begin(), Eng.undef_derefs_end());
195 }
Ted Kremenek0c313172009-05-13 19:16:35 +0000196
197 void registerInitialVisitors(BugReporterContext& BRC,
198 const ExplodedNode<GRState>* N,
199 BuiltinBugReport *R) {
Ted Kremenek85ac9342009-05-15 05:25:09 +0000200 registerTrackNullOrUndefValue(BRC, GetDerefExpr(N), N);
Ted Kremenek0c313172009-05-13 19:16:35 +0000201 }
Ted Kremenek78d46242008-07-22 16:21:24 +0000202};
203
204class VISIBILITY_HIDDEN DivZero : public BuiltinBug {
205public:
Ted Kremenekcf118d42009-02-04 23:49:09 +0000206 DivZero(GRExprEngine* eng)
Ted Kremenek5d88ff82009-04-02 02:40:26 +0000207 : BuiltinBug(eng,"Division-by-zero",
208 "Division by zero or undefined value.") {}
Ted Kremenek78d46242008-07-22 16:21:24 +0000209
Ted Kremenekcf118d42009-02-04 23:49:09 +0000210 void FlushReportsImpl(BugReporter& BR, GRExprEngine& Eng) {
Ted Kremenek78d46242008-07-22 16:21:24 +0000211 Emit(BR, Eng.explicit_bad_divides_begin(), Eng.explicit_bad_divides_end());
212 }
Ted Kremenek85ac9342009-05-15 05:25:09 +0000213
214 void registerInitialVisitors(BugReporterContext& BRC,
215 const ExplodedNode<GRState>* N,
216 BuiltinBugReport *R) {
217 registerTrackNullOrUndefValue(BRC, GetDenomExpr(N), N);
218 }
Ted Kremenek78d46242008-07-22 16:21:24 +0000219};
220
221class VISIBILITY_HIDDEN UndefResult : public BuiltinBug {
222public:
Ted Kremenek5d88ff82009-04-02 02:40:26 +0000223 UndefResult(GRExprEngine* eng) : BuiltinBug(eng,"Undefined result",
Ted Kremenek78d46242008-07-22 16:21:24 +0000224 "Result of operation is undefined.") {}
225
Ted Kremenekcf118d42009-02-04 23:49:09 +0000226 void FlushReportsImpl(BugReporter& BR, GRExprEngine& Eng) {
Ted Kremenek78d46242008-07-22 16:21:24 +0000227 Emit(BR, Eng.undef_results_begin(), Eng.undef_results_end());
228 }
229};
230
231class VISIBILITY_HIDDEN BadCall : public BuiltinBug {
232public:
Ted Kremenekcf118d42009-02-04 23:49:09 +0000233 BadCall(GRExprEngine *eng)
Ted Kremenek5d88ff82009-04-02 02:40:26 +0000234 : BuiltinBug(eng, "Invalid function call",
Ted Kremenek17a8e072009-03-01 05:43:22 +0000235 "Called function pointer is a null or undefined pointer value") {}
Ted Kremenek78d46242008-07-22 16:21:24 +0000236
Ted Kremenekcf118d42009-02-04 23:49:09 +0000237 void FlushReportsImpl(BugReporter& BR, GRExprEngine& Eng) {
Ted Kremenek78d46242008-07-22 16:21:24 +0000238 Emit(BR, Eng.bad_calls_begin(), Eng.bad_calls_end());
239 }
Ted Kremenek85ac9342009-05-15 05:25:09 +0000240
241 void registerInitialVisitors(BugReporterContext& BRC,
242 const ExplodedNode<GRState>* N,
243 BuiltinBugReport *R) {
244 registerTrackNullOrUndefValue(BRC, GetCalleeExpr(N), N);
245 }
246};
247
248
249class VISIBILITY_HIDDEN ArgReport : public BuiltinBugReport {
250 const Stmt *Arg;
251public:
252 ArgReport(BugType& bt, const char* desc, ExplodedNode<GRState> *n,
253 const Stmt *arg)
254 : BuiltinBugReport(bt, desc, n), Arg(arg) {}
255
256 ArgReport(BugType& bt, const char *shortDesc, const char *desc,
257 ExplodedNode<GRState> *n, const Stmt *arg)
258 : BuiltinBugReport(bt, shortDesc, desc, n), Arg(arg) {}
259
260 const Stmt *getArg() const { return Arg; }
Ted Kremenek78d46242008-07-22 16:21:24 +0000261};
262
263class VISIBILITY_HIDDEN BadArg : public BuiltinBug {
Ted Kremenek85ac9342009-05-15 05:25:09 +0000264public:
Ted Kremenek5d88ff82009-04-02 02:40:26 +0000265 BadArg(GRExprEngine* eng) : BuiltinBug(eng,"Uninitialized argument",
Ted Kremenek17a8e072009-03-01 05:43:22 +0000266 "Pass-by-value argument in function call is undefined.") {}
Ted Kremenek78d46242008-07-22 16:21:24 +0000267
Ted Kremenekcf118d42009-02-04 23:49:09 +0000268 BadArg(GRExprEngine* eng, const char* d)
Ted Kremenek5d88ff82009-04-02 02:40:26 +0000269 : BuiltinBug(eng,"Uninitialized argument", d) {}
Ted Kremenek78d46242008-07-22 16:21:24 +0000270
Ted Kremenekcf118d42009-02-04 23:49:09 +0000271 void FlushReportsImpl(BugReporter& BR, GRExprEngine& Eng) {
Ted Kremenek78d46242008-07-22 16:21:24 +0000272 for (GRExprEngine::UndefArgsTy::iterator I = Eng.undef_arg_begin(),
273 E = Eng.undef_arg_end(); I!=E; ++I) {
Ted Kremenek78d46242008-07-22 16:21:24 +0000274 // Generate a report for this bug.
Ted Kremenek85ac9342009-05-15 05:25:09 +0000275 ArgReport *report = new ArgReport(*this, desc.c_str(), I->first,
276 I->second);
Ted Kremenekcf118d42009-02-04 23:49:09 +0000277 report->addRange(I->second->getSourceRange());
278 BR.EmitReport(report);
Ted Kremenek78d46242008-07-22 16:21:24 +0000279 }
280 }
Ted Kremenek85ac9342009-05-15 05:25:09 +0000281
282 void registerInitialVisitors(BugReporterContext& BRC,
283 const ExplodedNode<GRState>* N,
284 BuiltinBugReport *R) {
285 registerTrackNullOrUndefValue(BRC, static_cast<ArgReport*>(R)->getArg(),
286 N);
287 }
Ted Kremenek78d46242008-07-22 16:21:24 +0000288};
289
290class VISIBILITY_HIDDEN BadMsgExprArg : public BadArg {
291public:
Ted Kremenekcf118d42009-02-04 23:49:09 +0000292 BadMsgExprArg(GRExprEngine* eng)
Ted Kremenek5d88ff82009-04-02 02:40:26 +0000293 : BadArg(eng,"Pass-by-value argument in message expression is undefined"){}
Ted Kremenek78d46242008-07-22 16:21:24 +0000294
Ted Kremenekcf118d42009-02-04 23:49:09 +0000295 void FlushReportsImpl(BugReporter& BR, GRExprEngine& Eng) {
Ted Kremenek78d46242008-07-22 16:21:24 +0000296 for (GRExprEngine::UndefArgsTy::iterator I=Eng.msg_expr_undef_arg_begin(),
Ted Kremenekcf118d42009-02-04 23:49:09 +0000297 E = Eng.msg_expr_undef_arg_end(); I!=E; ++I) {
Ted Kremenek78d46242008-07-22 16:21:24 +0000298 // Generate a report for this bug.
Ted Kremenek85ac9342009-05-15 05:25:09 +0000299 ArgReport *report = new ArgReport(*this, desc.c_str(), I->first,
300 I->second);
Ted Kremenekcf118d42009-02-04 23:49:09 +0000301 report->addRange(I->second->getSourceRange());
302 BR.EmitReport(report);
Ted Kremenek78d46242008-07-22 16:21:24 +0000303 }
Ted Kremenek85ac9342009-05-15 05:25:09 +0000304 }
Ted Kremenek78d46242008-07-22 16:21:24 +0000305};
306
307class VISIBILITY_HIDDEN BadReceiver : public BuiltinBug {
308public:
Ted Kremenekcf118d42009-02-04 23:49:09 +0000309 BadReceiver(GRExprEngine* eng)
Ted Kremenek5d88ff82009-04-02 02:40:26 +0000310 : BuiltinBug(eng,"Uninitialized receiver",
311 "Receiver in message expression is an uninitialized value") {}
Ted Kremenek78d46242008-07-22 16:21:24 +0000312
Ted Kremenekcf118d42009-02-04 23:49:09 +0000313 void FlushReportsImpl(BugReporter& BR, GRExprEngine& Eng) {
Ted Kremenekefd59942008-12-08 22:47:34 +0000314 for (GRExprEngine::ErrorNodes::iterator I=Eng.undef_receivers_begin(),
Ted Kremenek78d46242008-07-22 16:21:24 +0000315 End = Eng.undef_receivers_end(); I!=End; ++I) {
316
317 // Generate a report for this bug.
Ted Kremenek0c313172009-05-13 19:16:35 +0000318 BuiltinBugReport *report = new BuiltinBugReport(*this, desc.c_str(), *I);
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000319 ExplodedNode<GRState>* N = *I;
Ted Kremenek78d46242008-07-22 16:21:24 +0000320 Stmt *S = cast<PostStmt>(N->getLocation()).getStmt();
321 Expr* E = cast<ObjCMessageExpr>(S)->getReceiver();
322 assert (E && "Receiver cannot be NULL");
Ted Kremenekcf118d42009-02-04 23:49:09 +0000323 report->addRange(E->getSourceRange());
324 BR.EmitReport(report);
Ted Kremenek0c313172009-05-13 19:16:35 +0000325 }
326 }
Ted Kremenek85ac9342009-05-15 05:25:09 +0000327
Ted Kremenek0c313172009-05-13 19:16:35 +0000328 void registerInitialVisitors(BugReporterContext& BRC,
329 const ExplodedNode<GRState>* N,
330 BuiltinBugReport *R) {
Ted Kremenek85ac9342009-05-15 05:25:09 +0000331 registerTrackNullOrUndefValue(BRC, GetReceiverExpr(N), N);
332 }
Ted Kremenek78d46242008-07-22 16:21:24 +0000333};
Ted Kremenek5917d782008-11-21 00:27:44 +0000334
Ted Kremenek78d46242008-07-22 16:21:24 +0000335class VISIBILITY_HIDDEN RetStack : public BuiltinBug {
336public:
Ted Kremenek17a8e072009-03-01 05:43:22 +0000337 RetStack(GRExprEngine* eng)
Ted Kremenek5d88ff82009-04-02 02:40:26 +0000338 : BuiltinBug(eng, "Return of address to stack-allocated memory") {}
Ted Kremenek78d46242008-07-22 16:21:24 +0000339
Ted Kremenekcf118d42009-02-04 23:49:09 +0000340 void FlushReportsImpl(BugReporter& BR, GRExprEngine& Eng) {
Ted Kremenekb7714b22008-07-30 17:49:12 +0000341 for (GRExprEngine::ret_stackaddr_iterator I=Eng.ret_stackaddr_begin(),
342 End = Eng.ret_stackaddr_end(); I!=End; ++I) {
Ted Kremenek22bda882008-07-31 20:31:27 +0000343
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000344 ExplodedNode<GRState>* N = *I;
Ted Kremenekb7714b22008-07-30 17:49:12 +0000345 Stmt *S = cast<PostStmt>(N->getLocation()).getStmt();
346 Expr* E = cast<ReturnStmt>(S)->getRetValue();
347 assert (E && "Return expression cannot be NULL");
Ted Kremenek22bda882008-07-31 20:31:27 +0000348
349 // Get the value associated with E.
Ted Kremenek23ec48c2009-06-18 23:58:37 +0000350 loc::MemRegionVal V = cast<loc::MemRegionVal>(N->getState()->getSVal(E));
Ted Kremenek22bda882008-07-31 20:31:27 +0000351
352 // Generate a report for this bug.
Ted Kremenekad51a602008-10-31 00:18:30 +0000353 std::string buf;
354 llvm::raw_string_ostream os(buf);
Ted Kremenek8aed8062008-10-31 00:13:20 +0000355 SourceRange R;
Ted Kremenek22bda882008-07-31 20:31:27 +0000356
Ted Kremenek8aed8062008-10-31 00:13:20 +0000357 // Check if the region is a compound literal.
358 if (const CompoundLiteralRegion* CR =
359 dyn_cast<CompoundLiteralRegion>(V.getRegion())) {
360
361 const CompoundLiteralExpr* CL = CR->getLiteralExpr();
362 os << "Address of stack memory associated with a compound literal "
363 "declared on line "
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000364 << BR.getSourceManager()
365 .getInstantiationLineNumber(CL->getLocStart())
Ted Kremenek8aed8062008-10-31 00:13:20 +0000366 << " returned.";
367
368 R = CL->getSourceRange();
369 }
Ted Kremenekde8cd192008-11-02 00:35:25 +0000370 else if (const AllocaRegion* AR = dyn_cast<AllocaRegion>(V.getRegion())) {
371 const Expr* ARE = AR->getExpr();
372 SourceLocation L = ARE->getLocStart();
373 R = ARE->getSourceRange();
374
375 os << "Address of stack memory allocated by call to alloca() on line "
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000376 << BR.getSourceManager().getInstantiationLineNumber(L)
Ted Kremenekde8cd192008-11-02 00:35:25 +0000377 << " returned.";
378 }
Ted Kremenek8aed8062008-10-31 00:13:20 +0000379 else {
380 os << "Address of stack memory associated with local variable '"
381 << V.getRegion()->getString() << "' returned.";
382 }
Ted Kremenek22bda882008-07-31 20:31:27 +0000383
Ted Kremenekcf118d42009-02-04 23:49:09 +0000384 RangedBugReport *report = new RangedBugReport(*this, os.str().c_str(), N);
385 report->addRange(E->getSourceRange());
386 if (R.isValid()) report->addRange(R);
387 BR.EmitReport(report);
Ted Kremenekb7714b22008-07-30 17:49:12 +0000388 }
Ted Kremenek78d46242008-07-22 16:21:24 +0000389 }
390};
Ted Kremenek5917d782008-11-21 00:27:44 +0000391
392class VISIBILITY_HIDDEN RetUndef : public BuiltinBug {
393public:
Ted Kremenek5d88ff82009-04-02 02:40:26 +0000394 RetUndef(GRExprEngine* eng) : BuiltinBug(eng, "Uninitialized return value",
Ted Kremenek0c313172009-05-13 19:16:35 +0000395 "Uninitialized or undefined value returned to caller.") {}
Ted Kremenek5917d782008-11-21 00:27:44 +0000396
Ted Kremenekcf118d42009-02-04 23:49:09 +0000397 void FlushReportsImpl(BugReporter& BR, GRExprEngine& Eng) {
Ted Kremenek5917d782008-11-21 00:27:44 +0000398 Emit(BR, Eng.ret_undef_begin(), Eng.ret_undef_end());
399 }
Ted Kremenek0c313172009-05-13 19:16:35 +0000400
401 void registerInitialVisitors(BugReporterContext& BRC,
402 const ExplodedNode<GRState>* N,
403 BuiltinBugReport *R) {
Ted Kremenek85ac9342009-05-15 05:25:09 +0000404 registerTrackNullOrUndefValue(BRC, GetRetValExpr(N), N);
405 }
Ted Kremenek5917d782008-11-21 00:27:44 +0000406};
Ted Kremenek78d46242008-07-22 16:21:24 +0000407
408class VISIBILITY_HIDDEN UndefBranch : public BuiltinBug {
409 struct VISIBILITY_HIDDEN FindUndefExpr {
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000410 GRStateManager& VM;
411 const GRState* St;
Ted Kremenek78d46242008-07-22 16:21:24 +0000412
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000413 FindUndefExpr(GRStateManager& V, const GRState* S) : VM(V), St(S) {}
Ted Kremenek78d46242008-07-22 16:21:24 +0000414
Ted Kremenekb7714b22008-07-30 17:49:12 +0000415 Expr* FindExpr(Expr* Ex) {
Ted Kremenek78d46242008-07-22 16:21:24 +0000416 if (!MatchesCriteria(Ex))
Ted Kremenekb7714b22008-07-30 17:49:12 +0000417 return 0;
Ted Kremenek78d46242008-07-22 16:21:24 +0000418
Ted Kremenekb7714b22008-07-30 17:49:12 +0000419 for (Stmt::child_iterator I=Ex->child_begin(), E=Ex->child_end();I!=E;++I)
Ted Kremenek78d46242008-07-22 16:21:24 +0000420 if (Expr* ExI = dyn_cast_or_null<Expr>(*I)) {
421 Expr* E2 = FindExpr(ExI);
422 if (E2) return E2;
423 }
424
425 return Ex;
426 }
427
Ted Kremenek23ec48c2009-06-18 23:58:37 +0000428 bool MatchesCriteria(Expr* Ex) { return St->getSVal(Ex).isUndef(); }
Ted Kremenek78d46242008-07-22 16:21:24 +0000429 };
430
431public:
Ted Kremenekcf118d42009-02-04 23:49:09 +0000432 UndefBranch(GRExprEngine *eng)
Ted Kremenek5d88ff82009-04-02 02:40:26 +0000433 : BuiltinBug(eng,"Use of uninitialized value",
Ted Kremenek78d46242008-07-22 16:21:24 +0000434 "Branch condition evaluates to an uninitialized value.") {}
435
Ted Kremenekcf118d42009-02-04 23:49:09 +0000436 void FlushReportsImpl(BugReporter& BR, GRExprEngine& Eng) {
Ted Kremenek78d46242008-07-22 16:21:24 +0000437 for (GRExprEngine::undef_branch_iterator I=Eng.undef_branches_begin(),
438 E=Eng.undef_branches_end(); I!=E; ++I) {
439
440 // What's going on here: we want to highlight the subexpression of the
441 // condition that is the most likely source of the "uninitialized
442 // branch condition." We do a recursive walk of the condition's
443 // subexpressions and roughly look for the most nested subexpression
444 // that binds to Undefined. We then highlight that expression's range.
Ted Kremenek78d46242008-07-22 16:21:24 +0000445 BlockEdge B = cast<BlockEdge>((*I)->getLocation());
446 Expr* Ex = cast<Expr>(B.getSrc()->getTerminatorCondition());
447 assert (Ex && "Block must have a terminator.");
448
449 // Get the predecessor node and check if is a PostStmt with the Stmt
450 // being the terminator condition. We want to inspect the state
451 // of that node instead because it will contain main information about
452 // the subexpressions.
Ted Kremenek78d46242008-07-22 16:21:24 +0000453 assert (!(*I)->pred_empty());
454
455 // Note: any predecessor will do. They should have identical state,
456 // since all the BlockEdge did was act as an error sink since the value
457 // had to already be undefined.
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000458 ExplodedNode<GRState> *N = *(*I)->pred_begin();
Ted Kremenek78d46242008-07-22 16:21:24 +0000459 ProgramPoint P = N->getLocation();
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000460 const GRState* St = (*I)->getState();
Ted Kremenek78d46242008-07-22 16:21:24 +0000461
462 if (PostStmt* PS = dyn_cast<PostStmt>(&P))
463 if (PS->getStmt() == Ex)
464 St = N->getState();
465
466 FindUndefExpr FindIt(Eng.getStateManager(), St);
467 Ex = FindIt.FindExpr(Ex);
468
Ted Kremenek85ac9342009-05-15 05:25:09 +0000469 ArgReport *R = new ArgReport(*this, desc.c_str(), *I, Ex);
Ted Kremenekcf118d42009-02-04 23:49:09 +0000470 R->addRange(Ex->getSourceRange());
471 BR.EmitReport(R);
Ted Kremenek78d46242008-07-22 16:21:24 +0000472 }
473 }
Ted Kremenek85ac9342009-05-15 05:25:09 +0000474
475 void registerInitialVisitors(BugReporterContext& BRC,
476 const ExplodedNode<GRState>* N,
477 BuiltinBugReport *R) {
478 registerTrackNullOrUndefValue(BRC, static_cast<ArgReport*>(R)->getArg(),
479 N);
480 }
Ted Kremenek78d46242008-07-22 16:21:24 +0000481};
482
Zhongxing Xu1c0c2332008-11-23 05:52:28 +0000483class VISIBILITY_HIDDEN OutOfBoundMemoryAccess : public BuiltinBug {
484public:
Ted Kremenekcf118d42009-02-04 23:49:09 +0000485 OutOfBoundMemoryAccess(GRExprEngine* eng)
Ted Kremenek5d88ff82009-04-02 02:40:26 +0000486 : BuiltinBug(eng,"Out-of-bounds memory access",
Ted Kremenekcf118d42009-02-04 23:49:09 +0000487 "Load or store into an out-of-bound memory position.") {}
Zhongxing Xu1c0c2332008-11-23 05:52:28 +0000488
Ted Kremenekcf118d42009-02-04 23:49:09 +0000489 void FlushReportsImpl(BugReporter& BR, GRExprEngine& Eng) {
Zhongxing Xu1c0c2332008-11-23 05:52:28 +0000490 Emit(BR, Eng.explicit_oob_memacc_begin(), Eng.explicit_oob_memacc_end());
491 }
492};
Ted Kremenekefd59942008-12-08 22:47:34 +0000493
Ted Kremenek159d2482008-12-09 00:44:16 +0000494class VISIBILITY_HIDDEN BadSizeVLA : public BuiltinBug {
Ted Kremenekefd59942008-12-08 22:47:34 +0000495public:
Ted Kremenek5d88ff82009-04-02 02:40:26 +0000496 BadSizeVLA(GRExprEngine* eng) :
497 BuiltinBug(eng, "Bad variable-length array (VLA) size") {}
Ted Kremenekefd59942008-12-08 22:47:34 +0000498
Ted Kremenekcf118d42009-02-04 23:49:09 +0000499 void FlushReportsImpl(BugReporter& BR, GRExprEngine& Eng) {
Ted Kremenekefd59942008-12-08 22:47:34 +0000500 for (GRExprEngine::ErrorNodes::iterator
Ted Kremenek159d2482008-12-09 00:44:16 +0000501 I = Eng.ExplicitBadSizedVLA.begin(),
502 E = Eng.ExplicitBadSizedVLA.end(); I!=E; ++I) {
503
504 // Determine whether this was a 'zero-sized' VLA or a VLA with an
505 // undefined size.
506 GRExprEngine::NodeTy* N = *I;
507 PostStmt PS = cast<PostStmt>(N->getLocation());
Ted Kremenekefd59942008-12-08 22:47:34 +0000508 DeclStmt *DS = cast<DeclStmt>(PS.getStmt());
509 VarDecl* VD = cast<VarDecl>(*DS->decl_begin());
510 QualType T = Eng.getContext().getCanonicalType(VD->getType());
511 VariableArrayType* VT = cast<VariableArrayType>(T);
Ted Kremenek159d2482008-12-09 00:44:16 +0000512 Expr* SizeExpr = VT->getSizeExpr();
Ted Kremenekefd59942008-12-08 22:47:34 +0000513
Ted Kremenek159d2482008-12-09 00:44:16 +0000514 std::string buf;
515 llvm::raw_string_ostream os(buf);
Ted Kremenek5d88ff82009-04-02 02:40:26 +0000516 os << "The expression used to specify the number of elements in the "
517 "variable-length array (VLA) '"
Ted Kremenekcf118d42009-02-04 23:49:09 +0000518 << VD->getNameAsString() << "' evaluates to ";
Ted Kremenek159d2482008-12-09 00:44:16 +0000519
Ted Kremenek23ec48c2009-06-18 23:58:37 +0000520 bool isUndefined = N->getState()->getSVal(SizeExpr).isUndef();
Ted Kremenekd49967f2009-04-29 21:58:13 +0000521
522 if (isUndefined)
Ted Kremenek159d2482008-12-09 00:44:16 +0000523 os << "an undefined or garbage value.";
Ted Kremenekcf118d42009-02-04 23:49:09 +0000524 else
525 os << "0. VLAs with no elements have undefined behavior.";
Ted Kremenekd49967f2009-04-29 21:58:13 +0000526
527 std::string shortBuf;
528 llvm::raw_string_ostream os_short(shortBuf);
529 os_short << "Variable-length array '" << VD->getNameAsString() << "' "
Ted Kremenekeaedfea2009-05-10 05:11:21 +0000530 << (isUndefined ? "garbage value for array size"
531 : "has zero elements (undefined behavior)");
Ted Kremenek159d2482008-12-09 00:44:16 +0000532
Ted Kremenek85ac9342009-05-15 05:25:09 +0000533 ArgReport *report = new ArgReport(*this, os_short.str().c_str(),
534 os.str().c_str(), N, SizeExpr);
535
Ted Kremenekcf118d42009-02-04 23:49:09 +0000536 report->addRange(SizeExpr->getSourceRange());
537 BR.EmitReport(report);
Ted Kremenekefd59942008-12-08 22:47:34 +0000538 }
539 }
Ted Kremenek85ac9342009-05-15 05:25:09 +0000540
541 void registerInitialVisitors(BugReporterContext& BRC,
542 const ExplodedNode<GRState>* N,
543 BuiltinBugReport *R) {
544 registerTrackNullOrUndefValue(BRC, static_cast<ArgReport*>(R)->getArg(),
545 N);
546 }
Ted Kremenekefd59942008-12-08 22:47:34 +0000547};
Zhongxing Xu1c0c2332008-11-23 05:52:28 +0000548
Ted Kremenek78d46242008-07-22 16:21:24 +0000549//===----------------------------------------------------------------------===//
550// __attribute__(nonnull) checking
551
552class VISIBILITY_HIDDEN CheckAttrNonNull : public GRSimpleAPICheck {
Ted Kremenekcf118d42009-02-04 23:49:09 +0000553 BugType *BT;
554 BugReporter &BR;
Ted Kremenek78d46242008-07-22 16:21:24 +0000555
556public:
Ted Kremenekcf118d42009-02-04 23:49:09 +0000557 CheckAttrNonNull(BugReporter &br) : BT(0), BR(br) {}
Ted Kremenek78d46242008-07-22 16:21:24 +0000558
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000559 virtual bool Audit(ExplodedNode<GRState>* N, GRStateManager& VMgr) {
Ted Kremenek78d46242008-07-22 16:21:24 +0000560 CallExpr* CE = cast<CallExpr>(cast<PostStmt>(N->getLocation()).getStmt());
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000561 const GRState* state = N->getState();
Ted Kremenek78d46242008-07-22 16:21:24 +0000562
Ted Kremenek23ec48c2009-06-18 23:58:37 +0000563 SVal X = state->getSVal(CE->getCallee());
Zhongxing Xu369f4472009-04-20 05:24:46 +0000564
565 const FunctionDecl* FD = X.getAsFunctionDecl();
566 if (!FD)
Ted Kremenek78d46242008-07-22 16:21:24 +0000567 return false;
Zhongxing Xu369f4472009-04-20 05:24:46 +0000568
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000569 const NonNullAttr* Att = FD->getAttr<NonNullAttr>();
Ted Kremenek78d46242008-07-22 16:21:24 +0000570
571 if (!Att)
572 return false;
573
574 // Iterate through the arguments of CE and check them for null.
Ted Kremenek78d46242008-07-22 16:21:24 +0000575 unsigned idx = 0;
576 bool hasError = false;
577
578 for (CallExpr::arg_iterator I=CE->arg_begin(), E=CE->arg_end(); I!=E;
579 ++I, ++idx) {
580
581 if (!VMgr.isEqual(state, *I, 0) || !Att->isNonNull(idx))
582 continue;
Ted Kremenekcf118d42009-02-04 23:49:09 +0000583
584 // Lazily allocate the BugType object if it hasn't already been created.
585 // Ownership is transferred to the BugReporter object once the BugReport
586 // is passed to 'EmitWarning'.
Ted Kremenek5d88ff82009-04-02 02:40:26 +0000587 if (!BT) BT =
588 new BugType("Argument with 'nonnull' attribute passed null", "API");
Ted Kremenek78d46242008-07-22 16:21:24 +0000589
Ted Kremenekcf118d42009-02-04 23:49:09 +0000590 RangedBugReport *R = new RangedBugReport(*BT,
591 "Null pointer passed as an argument to a "
592 "'nonnull' parameter", N);
593
594 R->addRange((*I)->getSourceRange());
595 BR.EmitReport(R);
Ted Kremenek78d46242008-07-22 16:21:24 +0000596 hasError = true;
597 }
598
599 return hasError;
600 }
Ted Kremenek78d46242008-07-22 16:21:24 +0000601};
602} // end anonymous namespace
603
604//===----------------------------------------------------------------------===//
Ted Kremenekdd986cc2009-05-07 00:45:33 +0000605// Definitions for bug reporter visitors.
606//===----------------------------------------------------------------------===//
607
Ted Kremenek85ac9342009-05-15 05:25:09 +0000608static const Stmt *GetDerefExpr(const ExplodedNode<GRState> *N) {
609 // Pattern match for a few useful cases (do something smarter later):
610 // a[0], p->f, *p
611 const Stmt *S = N->getLocationAs<PostStmt>()->getStmt();
612
613 if (const UnaryOperator *U = dyn_cast<UnaryOperator>(S)) {
614 if (U->getOpcode() == UnaryOperator::Deref)
615 return U->getSubExpr()->IgnoreParenCasts();
616 }
617 else if (const MemberExpr *ME = dyn_cast<MemberExpr>(S)) {
618 return ME->getBase()->IgnoreParenCasts();
619 }
620 else if (const ArraySubscriptExpr *AE = dyn_cast<ArraySubscriptExpr>(S)) {
621 // Retrieve the base for arrays since BasicStoreManager doesn't know how
622 // to reason about them.
623 return AE->getBase();
624 }
625
626 return NULL;
627}
628
629static const Stmt *GetReceiverExpr(const ExplodedNode<GRState> *N) {
630 const Stmt *S = N->getLocationAs<PostStmt>()->getStmt();
Ted Kremenek725b7472009-05-15 05:34:49 +0000631 if (const ObjCMessageExpr *ME = dyn_cast<ObjCMessageExpr>(S))
632 return ME->getReceiver();
633 return NULL;
Ted Kremenek85ac9342009-05-15 05:25:09 +0000634}
635
636static const Stmt *GetDenomExpr(const ExplodedNode<GRState> *N) {
637 const Stmt *S = N->getLocationAs<PostStmt>()->getStmt();
Ted Kremenek725b7472009-05-15 05:34:49 +0000638 if (const BinaryOperator *BE = dyn_cast<BinaryOperator>(S))
639 return BE->getRHS();
640 return NULL;
Ted Kremenek85ac9342009-05-15 05:25:09 +0000641}
642
643static const Stmt *GetCalleeExpr(const ExplodedNode<GRState> *N) {
644 const Stmt *S = N->getLocationAs<PostStmt>()->getStmt();
Ted Kremenek725b7472009-05-15 05:34:49 +0000645 if (const CallExpr *CE = dyn_cast<CallExpr>(S))
646 return CE->getCallee();
647 return NULL;
Ted Kremenek85ac9342009-05-15 05:25:09 +0000648}
649
650static const Stmt *GetRetValExpr(const ExplodedNode<GRState> *N) {
651 const Stmt *S = N->getLocationAs<PostStmt>()->getStmt();
Ted Kremenek725b7472009-05-15 05:34:49 +0000652 if (const ReturnStmt *RS = dyn_cast<ReturnStmt>(S))
653 return RS->getRetValue();
654 return NULL;
Ted Kremenek85ac9342009-05-15 05:25:09 +0000655}
656
Ted Kremenekdd986cc2009-05-07 00:45:33 +0000657namespace {
Ted Kremenek0c313172009-05-13 19:16:35 +0000658class VISIBILITY_HIDDEN FindLastStoreBRVisitor : public BugReporterVisitor {
Ted Kremenek7704a332009-05-07 21:49:45 +0000659 const MemRegion *R;
Ted Kremenek0c313172009-05-13 19:16:35 +0000660 SVal V;
661 bool satisfied;
662 const ExplodedNode<GRState> *StoreSite;
Ted Kremenek7704a332009-05-07 21:49:45 +0000663public:
Ted Kremenek0c313172009-05-13 19:16:35 +0000664 FindLastStoreBRVisitor(SVal v, const MemRegion *r)
665 : R(r), V(v), satisfied(false), StoreSite(0) {}
666
Ted Kremenek7704a332009-05-07 21:49:45 +0000667 PathDiagnosticPiece* VisitNode(const ExplodedNode<GRState> *N,
668 const ExplodedNode<GRState> *PrevN,
669 BugReporterContext& BRC) {
Ted Kremenek0c313172009-05-13 19:16:35 +0000670
671 if (satisfied)
Ted Kremenek7704a332009-05-07 21:49:45 +0000672 return NULL;
Ted Kremenek7704a332009-05-07 21:49:45 +0000673
Ted Kremenek0c313172009-05-13 19:16:35 +0000674 if (!StoreSite) {
Ted Kremenek0c313172009-05-13 19:16:35 +0000675 const ExplodedNode<GRState> *Node = N, *Last = NULL;
676
677 for ( ; Node ; Last = Node, Node = Node->getFirstPred()) {
678
679 if (const VarRegion *VR = dyn_cast<VarRegion>(R)) {
680 if (const PostStmt *P = Node->getLocationAs<PostStmt>())
681 if (const DeclStmt *DS = P->getStmtAs<DeclStmt>())
682 if (DS->getSingleDecl() == VR->getDecl()) {
683 Last = Node;
684 break;
685 }
686 }
687
Ted Kremenek233e9132009-06-24 22:15:30 +0000688 if (Node->getState()->getSVal(R) != V)
Ted Kremenek0c313172009-05-13 19:16:35 +0000689 break;
690 }
691
692 if (!Node || !Last) {
693 satisfied = true;
694 return NULL;
695 }
696
697 StoreSite = Last;
698 }
Ted Kremenek7704a332009-05-07 21:49:45 +0000699
Ted Kremenek0c313172009-05-13 19:16:35 +0000700 if (StoreSite != N)
701 return NULL;
702
703 satisfied = true;
704 std::string sbuf;
705 llvm::raw_string_ostream os(sbuf);
Ted Kremenek7704a332009-05-07 21:49:45 +0000706
Ted Kremenek0c313172009-05-13 19:16:35 +0000707 if (const PostStmt *PS = N->getLocationAs<PostStmt>()) {
708 if (const DeclStmt *DS = PS->getStmtAs<DeclStmt>()) {
709
710 if (const VarRegion *VR = dyn_cast<VarRegion>(R)) {
711 os << "Variable '" << VR->getDecl()->getNameAsString() << "' ";
712 }
713 else
714 return NULL;
715
716 if (isa<loc::ConcreteInt>(V)) {
717 bool b = false;
718 ASTContext &C = BRC.getASTContext();
Ted Kremeneka43484a2009-06-23 00:46:41 +0000719 if (R->isBoundable()) {
Ted Kremenek0c313172009-05-13 19:16:35 +0000720 if (const TypedRegion *TR = dyn_cast<TypedRegion>(R)) {
Steve Narofff4954562009-07-16 15:41:00 +0000721 if (TR->getValueType(C)->isObjCObjectPointerType()) {
Ted Kremenek0c313172009-05-13 19:16:35 +0000722 os << "initialized to nil";
723 b = true;
724 }
725 }
726 }
727
728 if (!b)
729 os << "initialized to a null pointer value";
730 }
Ted Kremenek85ac9342009-05-15 05:25:09 +0000731 else if (isa<nonloc::ConcreteInt>(V)) {
732 os << "initialized to " << cast<nonloc::ConcreteInt>(V).getValue();
733 }
Ted Kremenek0c313172009-05-13 19:16:35 +0000734 else if (V.isUndef()) {
735 if (isa<VarRegion>(R)) {
736 const VarDecl *VD = cast<VarDecl>(DS->getSingleDecl());
737 if (VD->getInit())
738 os << "initialized to a garbage value";
739 else
740 os << "declared without an initial value";
741 }
742 }
743 }
744 }
Ted Kremenek85ac9342009-05-15 05:25:09 +0000745
Ted Kremenek0c313172009-05-13 19:16:35 +0000746 if (os.str().empty()) {
747 if (isa<loc::ConcreteInt>(V)) {
748 bool b = false;
749 ASTContext &C = BRC.getASTContext();
Ted Kremeneka43484a2009-06-23 00:46:41 +0000750 if (R->isBoundable()) {
Ted Kremenek0c313172009-05-13 19:16:35 +0000751 if (const TypedRegion *TR = dyn_cast<TypedRegion>(R)) {
Steve Narofff4954562009-07-16 15:41:00 +0000752 if (TR->getValueType(C)->isObjCObjectPointerType()) {
Ted Kremenek0c313172009-05-13 19:16:35 +0000753 os << "nil object reference stored to ";
754 b = true;
755 }
756 }
757 }
758
759 if (!b)
760 os << "Null pointer value stored to ";
761 }
762 else if (V.isUndef()) {
763 os << "Uninitialized value stored to ";
764 }
765 else
766 return NULL;
Ted Kremenek7704a332009-05-07 21:49:45 +0000767
Ted Kremenek0c313172009-05-13 19:16:35 +0000768 if (const VarRegion *VR = dyn_cast<VarRegion>(R)) {
769 os << '\'' << VR->getDecl()->getNameAsString() << '\'';
770 }
771 else
772 return NULL;
773 }
774
775 // FIXME: Refactor this into BugReporterContext.
776 Stmt *S = 0;
777 ProgramPoint P = N->getLocation();
778
779 if (BlockEdge *BE = dyn_cast<BlockEdge>(&P)) {
780 CFGBlock *BSrc = BE->getSrc();
781 S = BSrc->getTerminatorCondition();
782 }
783 else if (PostStmt *PS = dyn_cast<PostStmt>(&P)) {
784 S = PS->getStmt();
785 }
786
787 if (!S)
788 return NULL;
789
790 // Construct a new PathDiagnosticPiece.
791 PathDiagnosticLocation L(S, BRC.getSourceManager());
792 return new PathDiagnosticEventPiece(L, os.str());
793 }
Ted Kremenek7704a332009-05-07 21:49:45 +0000794};
Ted Kremenek7704a332009-05-07 21:49:45 +0000795
Ted Kremenek0c313172009-05-13 19:16:35 +0000796
797static void registerFindLastStore(BugReporterContext& BRC, const MemRegion *R,
798 SVal V) {
799 BRC.addVisitor(new FindLastStoreBRVisitor(V, R));
800}
801
Ted Kremenekdd986cc2009-05-07 00:45:33 +0000802class VISIBILITY_HIDDEN TrackConstraintBRVisitor : public BugReporterVisitor {
803 SVal Constraint;
804 const bool Assumption;
805 bool isSatisfied;
806public:
807 TrackConstraintBRVisitor(SVal constraint, bool assumption)
808 : Constraint(constraint), Assumption(assumption), isSatisfied(false) {}
809
Ted Kremenek7704a332009-05-07 21:49:45 +0000810 PathDiagnosticPiece* VisitNode(const ExplodedNode<GRState> *N,
811 const ExplodedNode<GRState> *PrevN,
Ted Kremenekdd986cc2009-05-07 00:45:33 +0000812 BugReporterContext& BRC) {
813 if (isSatisfied)
814 return NULL;
815
816 // Check if in the previous state it was feasible for this constraint
817 // to *not* be true.
Ted Kremeneka591bc02009-06-18 22:57:13 +0000818 if (PrevN->getState()->assume(Constraint, !Assumption)) {
Ted Kremenekdd986cc2009-05-07 00:45:33 +0000819
820 isSatisfied = true;
821
822 // As a sanity check, make sure that the negation of the constraint
823 // was infeasible in the current state. If it is feasible, we somehow
824 // missed the transition point.
Ted Kremeneka591bc02009-06-18 22:57:13 +0000825 if (N->getState()->assume(Constraint, !Assumption))
Ted Kremenekdd986cc2009-05-07 00:45:33 +0000826 return NULL;
Ted Kremenekdd986cc2009-05-07 00:45:33 +0000827
828 // We found the transition point for the constraint. We now need to
829 // pretty-print the constraint. (work-in-progress)
830 std::string sbuf;
831 llvm::raw_string_ostream os(sbuf);
832
833 if (isa<Loc>(Constraint)) {
834 os << "Assuming pointer value is ";
Ted Kremenek85ac9342009-05-15 05:25:09 +0000835 os << (Assumption ? "non-null" : "null");
Ted Kremenekdd986cc2009-05-07 00:45:33 +0000836 }
837
838 if (os.str().empty())
839 return NULL;
840
841 // FIXME: Refactor this into BugReporterContext.
842 Stmt *S = 0;
843 ProgramPoint P = N->getLocation();
844
845 if (BlockEdge *BE = dyn_cast<BlockEdge>(&P)) {
846 CFGBlock *BSrc = BE->getSrc();
847 S = BSrc->getTerminatorCondition();
848 }
849 else if (PostStmt *PS = dyn_cast<PostStmt>(&P)) {
850 S = PS->getStmt();
851 }
852
853 if (!S)
854 return NULL;
855
856 // Construct a new PathDiagnosticPiece.
857 PathDiagnosticLocation L(S, BRC.getSourceManager());
858 return new PathDiagnosticEventPiece(L, os.str());
859 }
860
861 return NULL;
862 }
863};
864} // end anonymous namespace
865
866static void registerTrackConstraint(BugReporterContext& BRC, SVal Constraint,
867 bool Assumption) {
868 BRC.addVisitor(new TrackConstraintBRVisitor(Constraint, Assumption));
869}
870
Ted Kremenek0c313172009-05-13 19:16:35 +0000871static void registerTrackNullOrUndefValue(BugReporterContext& BRC,
Ted Kremenek85ac9342009-05-15 05:25:09 +0000872 const Stmt *S,
873 const ExplodedNode<GRState>* N) {
Ted Kremenekdd986cc2009-05-07 00:45:33 +0000874
Ted Kremenek85ac9342009-05-15 05:25:09 +0000875 if (!S)
Ted Kremenekdd986cc2009-05-07 00:45:33 +0000876 return;
Ted Kremenek85ac9342009-05-15 05:25:09 +0000877
Ted Kremenek0c313172009-05-13 19:16:35 +0000878 GRStateManager &StateMgr = BRC.getStateManager();
Ted Kremenek85ac9342009-05-15 05:25:09 +0000879 const GRState *state = N->getState();
Ted Kremenekdd986cc2009-05-07 00:45:33 +0000880
Ted Kremenek85ac9342009-05-15 05:25:09 +0000881 if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(S)) {
Ted Kremenek0c313172009-05-13 19:16:35 +0000882 if (const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl())) {
883 const VarRegion *R =
884 StateMgr.getRegionManager().getVarRegion(VD);
885
886 // What did we load?
Ted Kremenek23ec48c2009-06-18 23:58:37 +0000887 SVal V = state->getSVal(S);
Ted Kremenek0c313172009-05-13 19:16:35 +0000888
Ted Kremenek85ac9342009-05-15 05:25:09 +0000889 if (isa<loc::ConcreteInt>(V) || isa<nonloc::ConcreteInt>(V)
890 || V.isUndef()) {
Ted Kremenek0c313172009-05-13 19:16:35 +0000891 registerFindLastStore(BRC, R, V);
892 }
893 }
894 }
Ted Kremenek0c313172009-05-13 19:16:35 +0000895
Ted Kremenek23ec48c2009-06-18 23:58:37 +0000896 SVal V = state->getSValAsScalarOrLoc(S);
Ted Kremenekdd986cc2009-05-07 00:45:33 +0000897
898 // Uncomment this to find cases where we aren't properly getting the
899 // base value that was dereferenced.
900 // assert(!V.isUnknownOrUndef());
901
Ted Kremenek7704a332009-05-07 21:49:45 +0000902 // Is it a symbolic value?
Ted Kremenekdd986cc2009-05-07 00:45:33 +0000903 if (loc::MemRegionVal *L = dyn_cast<loc::MemRegionVal>(&V)) {
904 const SubRegion *R = cast<SubRegion>(L->getRegion());
905 while (R && !isa<SymbolicRegion>(R)) {
906 R = dyn_cast<SubRegion>(R->getSuperRegion());
907 }
908
909 if (R) {
910 assert(isa<SymbolicRegion>(R));
911 registerTrackConstraint(BRC, loc::MemRegionVal(R), false);
912 }
913 }
914}
915
916//===----------------------------------------------------------------------===//
Ted Kremenek78d46242008-07-22 16:21:24 +0000917// Check registration.
Ted Kremenekcf118d42009-02-04 23:49:09 +0000918//===----------------------------------------------------------------------===//
Ted Kremenek78d46242008-07-22 16:21:24 +0000919
920void GRExprEngine::RegisterInternalChecks() {
Ted Kremenekcf118d42009-02-04 23:49:09 +0000921 // Register internal "built-in" BugTypes with the BugReporter. These BugTypes
922 // are different than what probably many checks will do since they don't
923 // create BugReports on-the-fly but instead wait until GRExprEngine finishes
924 // analyzing a function. Generation of BugReport objects is done via a call
925 // to 'FlushReports' from BugReporter.
926 BR.Register(new NullDeref(this));
927 BR.Register(new UndefinedDeref(this));
928 BR.Register(new UndefBranch(this));
929 BR.Register(new DivZero(this));
930 BR.Register(new UndefResult(this));
931 BR.Register(new BadCall(this));
932 BR.Register(new RetStack(this));
933 BR.Register(new RetUndef(this));
934 BR.Register(new BadArg(this));
935 BR.Register(new BadMsgExprArg(this));
936 BR.Register(new BadReceiver(this));
937 BR.Register(new OutOfBoundMemoryAccess(this));
938 BR.Register(new BadSizeVLA(this));
Ted Kremenek21fe8372009-02-19 04:06:22 +0000939 BR.Register(new NilReceiverStructRet(this));
Ted Kremenek899b3de2009-04-08 03:07:17 +0000940 BR.Register(new NilReceiverLargerThanVoidPtrRet(this));
Ted Kremenekcf118d42009-02-04 23:49:09 +0000941
942 // The following checks do not need to have their associated BugTypes
943 // explicitly registered with the BugReporter. If they issue any BugReports,
944 // their associated BugType will get registered with the BugReporter
945 // automatically. Note that the check itself is owned by the GRExprEngine
946 // object.
947 AddCheck(new CheckAttrNonNull(BR), Stmt::CallExprClass);
Ted Kremenek78d46242008-07-22 16:21:24 +0000948}