blob: a0de13f702818a7da6add281861127201881b829 [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.
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000350 loc::MemRegionVal V =
351 cast<loc::MemRegionVal>(Eng.getStateManager().GetSVal(N->getState(),
Ted Kremenek9e240492008-10-04 05:50:14 +0000352 E));
Ted Kremenek22bda882008-07-31 20:31:27 +0000353
354 // Generate a report for this bug.
Ted Kremenekad51a602008-10-31 00:18:30 +0000355 std::string buf;
356 llvm::raw_string_ostream os(buf);
Ted Kremenek8aed8062008-10-31 00:13:20 +0000357 SourceRange R;
Ted Kremenek22bda882008-07-31 20:31:27 +0000358
Ted Kremenek8aed8062008-10-31 00:13:20 +0000359 // Check if the region is a compound literal.
360 if (const CompoundLiteralRegion* CR =
361 dyn_cast<CompoundLiteralRegion>(V.getRegion())) {
362
363 const CompoundLiteralExpr* CL = CR->getLiteralExpr();
364 os << "Address of stack memory associated with a compound literal "
365 "declared on line "
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000366 << BR.getSourceManager()
367 .getInstantiationLineNumber(CL->getLocStart())
Ted Kremenek8aed8062008-10-31 00:13:20 +0000368 << " returned.";
369
370 R = CL->getSourceRange();
371 }
Ted Kremenekde8cd192008-11-02 00:35:25 +0000372 else if (const AllocaRegion* AR = dyn_cast<AllocaRegion>(V.getRegion())) {
373 const Expr* ARE = AR->getExpr();
374 SourceLocation L = ARE->getLocStart();
375 R = ARE->getSourceRange();
376
377 os << "Address of stack memory allocated by call to alloca() on line "
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000378 << BR.getSourceManager().getInstantiationLineNumber(L)
Ted Kremenekde8cd192008-11-02 00:35:25 +0000379 << " returned.";
380 }
Ted Kremenek8aed8062008-10-31 00:13:20 +0000381 else {
382 os << "Address of stack memory associated with local variable '"
383 << V.getRegion()->getString() << "' returned.";
384 }
Ted Kremenek22bda882008-07-31 20:31:27 +0000385
Ted Kremenekcf118d42009-02-04 23:49:09 +0000386 RangedBugReport *report = new RangedBugReport(*this, os.str().c_str(), N);
387 report->addRange(E->getSourceRange());
388 if (R.isValid()) report->addRange(R);
389 BR.EmitReport(report);
Ted Kremenekb7714b22008-07-30 17:49:12 +0000390 }
Ted Kremenek78d46242008-07-22 16:21:24 +0000391 }
392};
Ted Kremenek5917d782008-11-21 00:27:44 +0000393
394class VISIBILITY_HIDDEN RetUndef : public BuiltinBug {
395public:
Ted Kremenek5d88ff82009-04-02 02:40:26 +0000396 RetUndef(GRExprEngine* eng) : BuiltinBug(eng, "Uninitialized return value",
Ted Kremenek0c313172009-05-13 19:16:35 +0000397 "Uninitialized or undefined value returned to caller.") {}
Ted Kremenek5917d782008-11-21 00:27:44 +0000398
Ted Kremenekcf118d42009-02-04 23:49:09 +0000399 void FlushReportsImpl(BugReporter& BR, GRExprEngine& Eng) {
Ted Kremenek5917d782008-11-21 00:27:44 +0000400 Emit(BR, Eng.ret_undef_begin(), Eng.ret_undef_end());
401 }
Ted Kremenek0c313172009-05-13 19:16:35 +0000402
403 void registerInitialVisitors(BugReporterContext& BRC,
404 const ExplodedNode<GRState>* N,
405 BuiltinBugReport *R) {
Ted Kremenek85ac9342009-05-15 05:25:09 +0000406 registerTrackNullOrUndefValue(BRC, GetRetValExpr(N), N);
407 }
Ted Kremenek5917d782008-11-21 00:27:44 +0000408};
Ted Kremenek78d46242008-07-22 16:21:24 +0000409
410class VISIBILITY_HIDDEN UndefBranch : public BuiltinBug {
411 struct VISIBILITY_HIDDEN FindUndefExpr {
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000412 GRStateManager& VM;
413 const GRState* St;
Ted Kremenek78d46242008-07-22 16:21:24 +0000414
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000415 FindUndefExpr(GRStateManager& V, const GRState* S) : VM(V), St(S) {}
Ted Kremenek78d46242008-07-22 16:21:24 +0000416
Ted Kremenekb7714b22008-07-30 17:49:12 +0000417 Expr* FindExpr(Expr* Ex) {
Ted Kremenek78d46242008-07-22 16:21:24 +0000418 if (!MatchesCriteria(Ex))
Ted Kremenekb7714b22008-07-30 17:49:12 +0000419 return 0;
Ted Kremenek78d46242008-07-22 16:21:24 +0000420
Ted Kremenekb7714b22008-07-30 17:49:12 +0000421 for (Stmt::child_iterator I=Ex->child_begin(), E=Ex->child_end();I!=E;++I)
Ted Kremenek78d46242008-07-22 16:21:24 +0000422 if (Expr* ExI = dyn_cast_or_null<Expr>(*I)) {
423 Expr* E2 = FindExpr(ExI);
424 if (E2) return E2;
425 }
426
427 return Ex;
428 }
429
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000430 bool MatchesCriteria(Expr* Ex) { return VM.GetSVal(St, Ex).isUndef(); }
Ted Kremenek78d46242008-07-22 16:21:24 +0000431 };
432
433public:
Ted Kremenekcf118d42009-02-04 23:49:09 +0000434 UndefBranch(GRExprEngine *eng)
Ted Kremenek5d88ff82009-04-02 02:40:26 +0000435 : BuiltinBug(eng,"Use of uninitialized value",
Ted Kremenek78d46242008-07-22 16:21:24 +0000436 "Branch condition evaluates to an uninitialized value.") {}
437
Ted Kremenekcf118d42009-02-04 23:49:09 +0000438 void FlushReportsImpl(BugReporter& BR, GRExprEngine& Eng) {
Ted Kremenek78d46242008-07-22 16:21:24 +0000439 for (GRExprEngine::undef_branch_iterator I=Eng.undef_branches_begin(),
440 E=Eng.undef_branches_end(); I!=E; ++I) {
441
442 // What's going on here: we want to highlight the subexpression of the
443 // condition that is the most likely source of the "uninitialized
444 // branch condition." We do a recursive walk of the condition's
445 // subexpressions and roughly look for the most nested subexpression
446 // that binds to Undefined. We then highlight that expression's range.
Ted Kremenek78d46242008-07-22 16:21:24 +0000447 BlockEdge B = cast<BlockEdge>((*I)->getLocation());
448 Expr* Ex = cast<Expr>(B.getSrc()->getTerminatorCondition());
449 assert (Ex && "Block must have a terminator.");
450
451 // Get the predecessor node and check if is a PostStmt with the Stmt
452 // being the terminator condition. We want to inspect the state
453 // of that node instead because it will contain main information about
454 // the subexpressions.
Ted Kremenek78d46242008-07-22 16:21:24 +0000455 assert (!(*I)->pred_empty());
456
457 // Note: any predecessor will do. They should have identical state,
458 // since all the BlockEdge did was act as an error sink since the value
459 // had to already be undefined.
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000460 ExplodedNode<GRState> *N = *(*I)->pred_begin();
Ted Kremenek78d46242008-07-22 16:21:24 +0000461 ProgramPoint P = N->getLocation();
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000462 const GRState* St = (*I)->getState();
Ted Kremenek78d46242008-07-22 16:21:24 +0000463
464 if (PostStmt* PS = dyn_cast<PostStmt>(&P))
465 if (PS->getStmt() == Ex)
466 St = N->getState();
467
468 FindUndefExpr FindIt(Eng.getStateManager(), St);
469 Ex = FindIt.FindExpr(Ex);
470
Ted Kremenek85ac9342009-05-15 05:25:09 +0000471 ArgReport *R = new ArgReport(*this, desc.c_str(), *I, Ex);
Ted Kremenekcf118d42009-02-04 23:49:09 +0000472 R->addRange(Ex->getSourceRange());
473 BR.EmitReport(R);
Ted Kremenek78d46242008-07-22 16:21:24 +0000474 }
475 }
Ted Kremenek85ac9342009-05-15 05:25:09 +0000476
477 void registerInitialVisitors(BugReporterContext& BRC,
478 const ExplodedNode<GRState>* N,
479 BuiltinBugReport *R) {
480 registerTrackNullOrUndefValue(BRC, static_cast<ArgReport*>(R)->getArg(),
481 N);
482 }
Ted Kremenek78d46242008-07-22 16:21:24 +0000483};
484
Zhongxing Xu1c0c2332008-11-23 05:52:28 +0000485class VISIBILITY_HIDDEN OutOfBoundMemoryAccess : public BuiltinBug {
486public:
Ted Kremenekcf118d42009-02-04 23:49:09 +0000487 OutOfBoundMemoryAccess(GRExprEngine* eng)
Ted Kremenek5d88ff82009-04-02 02:40:26 +0000488 : BuiltinBug(eng,"Out-of-bounds memory access",
Ted Kremenekcf118d42009-02-04 23:49:09 +0000489 "Load or store into an out-of-bound memory position.") {}
Zhongxing Xu1c0c2332008-11-23 05:52:28 +0000490
Ted Kremenekcf118d42009-02-04 23:49:09 +0000491 void FlushReportsImpl(BugReporter& BR, GRExprEngine& Eng) {
Zhongxing Xu1c0c2332008-11-23 05:52:28 +0000492 Emit(BR, Eng.explicit_oob_memacc_begin(), Eng.explicit_oob_memacc_end());
493 }
494};
Ted Kremenekefd59942008-12-08 22:47:34 +0000495
Ted Kremenek159d2482008-12-09 00:44:16 +0000496class VISIBILITY_HIDDEN BadSizeVLA : public BuiltinBug {
Ted Kremenekefd59942008-12-08 22:47:34 +0000497public:
Ted Kremenek5d88ff82009-04-02 02:40:26 +0000498 BadSizeVLA(GRExprEngine* eng) :
499 BuiltinBug(eng, "Bad variable-length array (VLA) size") {}
Ted Kremenekefd59942008-12-08 22:47:34 +0000500
Ted Kremenekcf118d42009-02-04 23:49:09 +0000501 void FlushReportsImpl(BugReporter& BR, GRExprEngine& Eng) {
Ted Kremenekefd59942008-12-08 22:47:34 +0000502 for (GRExprEngine::ErrorNodes::iterator
Ted Kremenek159d2482008-12-09 00:44:16 +0000503 I = Eng.ExplicitBadSizedVLA.begin(),
504 E = Eng.ExplicitBadSizedVLA.end(); I!=E; ++I) {
505
506 // Determine whether this was a 'zero-sized' VLA or a VLA with an
507 // undefined size.
508 GRExprEngine::NodeTy* N = *I;
509 PostStmt PS = cast<PostStmt>(N->getLocation());
Ted Kremenekefd59942008-12-08 22:47:34 +0000510 DeclStmt *DS = cast<DeclStmt>(PS.getStmt());
511 VarDecl* VD = cast<VarDecl>(*DS->decl_begin());
512 QualType T = Eng.getContext().getCanonicalType(VD->getType());
513 VariableArrayType* VT = cast<VariableArrayType>(T);
Ted Kremenek159d2482008-12-09 00:44:16 +0000514 Expr* SizeExpr = VT->getSizeExpr();
Ted Kremenekefd59942008-12-08 22:47:34 +0000515
Ted Kremenek159d2482008-12-09 00:44:16 +0000516 std::string buf;
517 llvm::raw_string_ostream os(buf);
Ted Kremenek5d88ff82009-04-02 02:40:26 +0000518 os << "The expression used to specify the number of elements in the "
519 "variable-length array (VLA) '"
Ted Kremenekcf118d42009-02-04 23:49:09 +0000520 << VD->getNameAsString() << "' evaluates to ";
Ted Kremenek159d2482008-12-09 00:44:16 +0000521
Ted Kremenekd49967f2009-04-29 21:58:13 +0000522 bool isUndefined = Eng.getStateManager().GetSVal(N->getState(),
523 SizeExpr).isUndef();
524
525 if (isUndefined)
Ted Kremenek159d2482008-12-09 00:44:16 +0000526 os << "an undefined or garbage value.";
Ted Kremenekcf118d42009-02-04 23:49:09 +0000527 else
528 os << "0. VLAs with no elements have undefined behavior.";
Ted Kremenekd49967f2009-04-29 21:58:13 +0000529
530 std::string shortBuf;
531 llvm::raw_string_ostream os_short(shortBuf);
532 os_short << "Variable-length array '" << VD->getNameAsString() << "' "
Ted Kremenekeaedfea2009-05-10 05:11:21 +0000533 << (isUndefined ? "garbage value for array size"
534 : "has zero elements (undefined behavior)");
Ted Kremenek159d2482008-12-09 00:44:16 +0000535
Ted Kremenek85ac9342009-05-15 05:25:09 +0000536 ArgReport *report = new ArgReport(*this, os_short.str().c_str(),
537 os.str().c_str(), N, SizeExpr);
538
Ted Kremenekcf118d42009-02-04 23:49:09 +0000539 report->addRange(SizeExpr->getSourceRange());
540 BR.EmitReport(report);
Ted Kremenekefd59942008-12-08 22:47:34 +0000541 }
542 }
Ted Kremenek85ac9342009-05-15 05:25:09 +0000543
544 void registerInitialVisitors(BugReporterContext& BRC,
545 const ExplodedNode<GRState>* N,
546 BuiltinBugReport *R) {
547 registerTrackNullOrUndefValue(BRC, static_cast<ArgReport*>(R)->getArg(),
548 N);
549 }
Ted Kremenekefd59942008-12-08 22:47:34 +0000550};
Zhongxing Xu1c0c2332008-11-23 05:52:28 +0000551
Ted Kremenek78d46242008-07-22 16:21:24 +0000552//===----------------------------------------------------------------------===//
553// __attribute__(nonnull) checking
554
555class VISIBILITY_HIDDEN CheckAttrNonNull : public GRSimpleAPICheck {
Ted Kremenekcf118d42009-02-04 23:49:09 +0000556 BugType *BT;
557 BugReporter &BR;
Ted Kremenek78d46242008-07-22 16:21:24 +0000558
559public:
Ted Kremenekcf118d42009-02-04 23:49:09 +0000560 CheckAttrNonNull(BugReporter &br) : BT(0), BR(br) {}
Ted Kremenek78d46242008-07-22 16:21:24 +0000561
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000562 virtual bool Audit(ExplodedNode<GRState>* N, GRStateManager& VMgr) {
Ted Kremenek78d46242008-07-22 16:21:24 +0000563 CallExpr* CE = cast<CallExpr>(cast<PostStmt>(N->getLocation()).getStmt());
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000564 const GRState* state = N->getState();
Ted Kremenek78d46242008-07-22 16:21:24 +0000565
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000566 SVal X = VMgr.GetSVal(state, CE->getCallee());
Zhongxing Xu369f4472009-04-20 05:24:46 +0000567
568 const FunctionDecl* FD = X.getAsFunctionDecl();
569 if (!FD)
Ted Kremenek78d46242008-07-22 16:21:24 +0000570 return false;
Zhongxing Xu369f4472009-04-20 05:24:46 +0000571
Douglas Gregor68584ed2009-06-18 16:11:24 +0000572 const NonNullAttr* Att = FD->getAttr<NonNullAttr>(BR.getContext());
Ted Kremenek78d46242008-07-22 16:21:24 +0000573
574 if (!Att)
575 return false;
576
577 // Iterate through the arguments of CE and check them for null.
Ted Kremenek78d46242008-07-22 16:21:24 +0000578 unsigned idx = 0;
579 bool hasError = false;
580
581 for (CallExpr::arg_iterator I=CE->arg_begin(), E=CE->arg_end(); I!=E;
582 ++I, ++idx) {
583
584 if (!VMgr.isEqual(state, *I, 0) || !Att->isNonNull(idx))
585 continue;
Ted Kremenekcf118d42009-02-04 23:49:09 +0000586
587 // Lazily allocate the BugType object if it hasn't already been created.
588 // Ownership is transferred to the BugReporter object once the BugReport
589 // is passed to 'EmitWarning'.
Ted Kremenek5d88ff82009-04-02 02:40:26 +0000590 if (!BT) BT =
591 new BugType("Argument with 'nonnull' attribute passed null", "API");
Ted Kremenek78d46242008-07-22 16:21:24 +0000592
Ted Kremenekcf118d42009-02-04 23:49:09 +0000593 RangedBugReport *R = new RangedBugReport(*BT,
594 "Null pointer passed as an argument to a "
595 "'nonnull' parameter", N);
596
597 R->addRange((*I)->getSourceRange());
598 BR.EmitReport(R);
Ted Kremenek78d46242008-07-22 16:21:24 +0000599 hasError = true;
600 }
601
602 return hasError;
603 }
Ted Kremenek78d46242008-07-22 16:21:24 +0000604};
605} // end anonymous namespace
606
607//===----------------------------------------------------------------------===//
Ted Kremenekdd986cc2009-05-07 00:45:33 +0000608// Definitions for bug reporter visitors.
609//===----------------------------------------------------------------------===//
610
Ted Kremenek85ac9342009-05-15 05:25:09 +0000611static const Stmt *GetDerefExpr(const ExplodedNode<GRState> *N) {
612 // Pattern match for a few useful cases (do something smarter later):
613 // a[0], p->f, *p
614 const Stmt *S = N->getLocationAs<PostStmt>()->getStmt();
615
616 if (const UnaryOperator *U = dyn_cast<UnaryOperator>(S)) {
617 if (U->getOpcode() == UnaryOperator::Deref)
618 return U->getSubExpr()->IgnoreParenCasts();
619 }
620 else if (const MemberExpr *ME = dyn_cast<MemberExpr>(S)) {
621 return ME->getBase()->IgnoreParenCasts();
622 }
623 else if (const ArraySubscriptExpr *AE = dyn_cast<ArraySubscriptExpr>(S)) {
624 // Retrieve the base for arrays since BasicStoreManager doesn't know how
625 // to reason about them.
626 return AE->getBase();
627 }
628
629 return NULL;
630}
631
632static const Stmt *GetReceiverExpr(const ExplodedNode<GRState> *N) {
633 const Stmt *S = N->getLocationAs<PostStmt>()->getStmt();
Ted Kremenek725b7472009-05-15 05:34:49 +0000634 if (const ObjCMessageExpr *ME = dyn_cast<ObjCMessageExpr>(S))
635 return ME->getReceiver();
636 return NULL;
Ted Kremenek85ac9342009-05-15 05:25:09 +0000637}
638
639static const Stmt *GetDenomExpr(const ExplodedNode<GRState> *N) {
640 const Stmt *S = N->getLocationAs<PostStmt>()->getStmt();
Ted Kremenek725b7472009-05-15 05:34:49 +0000641 if (const BinaryOperator *BE = dyn_cast<BinaryOperator>(S))
642 return BE->getRHS();
643 return NULL;
Ted Kremenek85ac9342009-05-15 05:25:09 +0000644}
645
646static const Stmt *GetCalleeExpr(const ExplodedNode<GRState> *N) {
647 const Stmt *S = N->getLocationAs<PostStmt>()->getStmt();
Ted Kremenek725b7472009-05-15 05:34:49 +0000648 if (const CallExpr *CE = dyn_cast<CallExpr>(S))
649 return CE->getCallee();
650 return NULL;
Ted Kremenek85ac9342009-05-15 05:25:09 +0000651}
652
653static const Stmt *GetRetValExpr(const ExplodedNode<GRState> *N) {
654 const Stmt *S = N->getLocationAs<PostStmt>()->getStmt();
Ted Kremenek725b7472009-05-15 05:34:49 +0000655 if (const ReturnStmt *RS = dyn_cast<ReturnStmt>(S))
656 return RS->getRetValue();
657 return NULL;
Ted Kremenek85ac9342009-05-15 05:25:09 +0000658}
659
Ted Kremenekdd986cc2009-05-07 00:45:33 +0000660namespace {
Ted Kremenek0c313172009-05-13 19:16:35 +0000661class VISIBILITY_HIDDEN FindLastStoreBRVisitor : public BugReporterVisitor {
Ted Kremenek7704a332009-05-07 21:49:45 +0000662 const MemRegion *R;
Ted Kremenek0c313172009-05-13 19:16:35 +0000663 SVal V;
664 bool satisfied;
665 const ExplodedNode<GRState> *StoreSite;
Ted Kremenek7704a332009-05-07 21:49:45 +0000666public:
Ted Kremenek0c313172009-05-13 19:16:35 +0000667 FindLastStoreBRVisitor(SVal v, const MemRegion *r)
668 : R(r), V(v), satisfied(false), StoreSite(0) {}
669
Ted Kremenek7704a332009-05-07 21:49:45 +0000670 PathDiagnosticPiece* VisitNode(const ExplodedNode<GRState> *N,
671 const ExplodedNode<GRState> *PrevN,
672 BugReporterContext& BRC) {
Ted Kremenek0c313172009-05-13 19:16:35 +0000673
674 if (satisfied)
Ted Kremenek7704a332009-05-07 21:49:45 +0000675 return NULL;
Ted Kremenek7704a332009-05-07 21:49:45 +0000676
Ted Kremenek0c313172009-05-13 19:16:35 +0000677 if (!StoreSite) {
678 GRStateManager &StateMgr = BRC.getStateManager();
679 const ExplodedNode<GRState> *Node = N, *Last = NULL;
680
681 for ( ; Node ; Last = Node, Node = Node->getFirstPred()) {
682
683 if (const VarRegion *VR = dyn_cast<VarRegion>(R)) {
684 if (const PostStmt *P = Node->getLocationAs<PostStmt>())
685 if (const DeclStmt *DS = P->getStmtAs<DeclStmt>())
686 if (DS->getSingleDecl() == VR->getDecl()) {
687 Last = Node;
688 break;
689 }
690 }
691
692 if (StateMgr.GetSVal(Node->getState(), R) != V)
693 break;
694 }
695
696 if (!Node || !Last) {
697 satisfied = true;
698 return NULL;
699 }
700
701 StoreSite = Last;
702 }
Ted Kremenek7704a332009-05-07 21:49:45 +0000703
Ted Kremenek0c313172009-05-13 19:16:35 +0000704 if (StoreSite != N)
705 return NULL;
706
707 satisfied = true;
708 std::string sbuf;
709 llvm::raw_string_ostream os(sbuf);
Ted Kremenek7704a332009-05-07 21:49:45 +0000710
Ted Kremenek0c313172009-05-13 19:16:35 +0000711 if (const PostStmt *PS = N->getLocationAs<PostStmt>()) {
712 if (const DeclStmt *DS = PS->getStmtAs<DeclStmt>()) {
713
714 if (const VarRegion *VR = dyn_cast<VarRegion>(R)) {
715 os << "Variable '" << VR->getDecl()->getNameAsString() << "' ";
716 }
717 else
718 return NULL;
719
720 if (isa<loc::ConcreteInt>(V)) {
721 bool b = false;
722 ASTContext &C = BRC.getASTContext();
723 if (R->isBoundable(C)) {
724 if (const TypedRegion *TR = dyn_cast<TypedRegion>(R)) {
725 if (C.isObjCObjectPointerType(TR->getValueType(C))) {
726 os << "initialized to nil";
727 b = true;
728 }
729 }
730 }
731
732 if (!b)
733 os << "initialized to a null pointer value";
734 }
Ted Kremenek85ac9342009-05-15 05:25:09 +0000735 else if (isa<nonloc::ConcreteInt>(V)) {
736 os << "initialized to " << cast<nonloc::ConcreteInt>(V).getValue();
737 }
Ted Kremenek0c313172009-05-13 19:16:35 +0000738 else if (V.isUndef()) {
739 if (isa<VarRegion>(R)) {
740 const VarDecl *VD = cast<VarDecl>(DS->getSingleDecl());
741 if (VD->getInit())
742 os << "initialized to a garbage value";
743 else
744 os << "declared without an initial value";
745 }
746 }
747 }
748 }
Ted Kremenek85ac9342009-05-15 05:25:09 +0000749
Ted Kremenek0c313172009-05-13 19:16:35 +0000750 if (os.str().empty()) {
751 if (isa<loc::ConcreteInt>(V)) {
752 bool b = false;
753 ASTContext &C = BRC.getASTContext();
754 if (R->isBoundable(C)) {
755 if (const TypedRegion *TR = dyn_cast<TypedRegion>(R)) {
756 if (C.isObjCObjectPointerType(TR->getValueType(C))) {
757 os << "nil object reference stored to ";
758 b = true;
759 }
760 }
761 }
762
763 if (!b)
764 os << "Null pointer value stored to ";
765 }
766 else if (V.isUndef()) {
767 os << "Uninitialized value stored to ";
768 }
769 else
770 return NULL;
Ted Kremenek7704a332009-05-07 21:49:45 +0000771
Ted Kremenek0c313172009-05-13 19:16:35 +0000772 if (const VarRegion *VR = dyn_cast<VarRegion>(R)) {
773 os << '\'' << VR->getDecl()->getNameAsString() << '\'';
774 }
775 else
776 return NULL;
777 }
778
779 // FIXME: Refactor this into BugReporterContext.
780 Stmt *S = 0;
781 ProgramPoint P = N->getLocation();
782
783 if (BlockEdge *BE = dyn_cast<BlockEdge>(&P)) {
784 CFGBlock *BSrc = BE->getSrc();
785 S = BSrc->getTerminatorCondition();
786 }
787 else if (PostStmt *PS = dyn_cast<PostStmt>(&P)) {
788 S = PS->getStmt();
789 }
790
791 if (!S)
792 return NULL;
793
794 // Construct a new PathDiagnosticPiece.
795 PathDiagnosticLocation L(S, BRC.getSourceManager());
796 return new PathDiagnosticEventPiece(L, os.str());
797 }
Ted Kremenek7704a332009-05-07 21:49:45 +0000798};
Ted Kremenek7704a332009-05-07 21:49:45 +0000799
Ted Kremenek0c313172009-05-13 19:16:35 +0000800
801static void registerFindLastStore(BugReporterContext& BRC, const MemRegion *R,
802 SVal V) {
803 BRC.addVisitor(new FindLastStoreBRVisitor(V, R));
804}
805
Ted Kremenekdd986cc2009-05-07 00:45:33 +0000806class VISIBILITY_HIDDEN TrackConstraintBRVisitor : public BugReporterVisitor {
807 SVal Constraint;
808 const bool Assumption;
809 bool isSatisfied;
810public:
811 TrackConstraintBRVisitor(SVal constraint, bool assumption)
812 : Constraint(constraint), Assumption(assumption), isSatisfied(false) {}
813
Ted Kremenek7704a332009-05-07 21:49:45 +0000814 PathDiagnosticPiece* VisitNode(const ExplodedNode<GRState> *N,
815 const ExplodedNode<GRState> *PrevN,
Ted Kremenekdd986cc2009-05-07 00:45:33 +0000816 BugReporterContext& BRC) {
817 if (isSatisfied)
818 return NULL;
819
820 // Check if in the previous state it was feasible for this constraint
821 // to *not* be true.
822
823 GRStateManager &StateMgr = BRC.getStateManager();
824 bool isFeasible = false;
825 if (StateMgr.Assume(PrevN->getState(), Constraint, !Assumption,
826 isFeasible)) {
827 assert(isFeasible); // Eventually we don't need 'isFeasible'.
828
829 isSatisfied = true;
830
831 // As a sanity check, make sure that the negation of the constraint
832 // was infeasible in the current state. If it is feasible, we somehow
833 // missed the transition point.
834 isFeasible = false;
835 if (StateMgr.Assume(N->getState(), Constraint, !Assumption,
836 isFeasible)) {
837 assert(isFeasible);
838 return NULL;
839 }
840
841 // We found the transition point for the constraint. We now need to
842 // pretty-print the constraint. (work-in-progress)
843 std::string sbuf;
844 llvm::raw_string_ostream os(sbuf);
845
846 if (isa<Loc>(Constraint)) {
847 os << "Assuming pointer value is ";
Ted Kremenek85ac9342009-05-15 05:25:09 +0000848 os << (Assumption ? "non-null" : "null");
Ted Kremenekdd986cc2009-05-07 00:45:33 +0000849 }
850
851 if (os.str().empty())
852 return NULL;
853
854 // FIXME: Refactor this into BugReporterContext.
855 Stmt *S = 0;
856 ProgramPoint P = N->getLocation();
857
858 if (BlockEdge *BE = dyn_cast<BlockEdge>(&P)) {
859 CFGBlock *BSrc = BE->getSrc();
860 S = BSrc->getTerminatorCondition();
861 }
862 else if (PostStmt *PS = dyn_cast<PostStmt>(&P)) {
863 S = PS->getStmt();
864 }
865
866 if (!S)
867 return NULL;
868
869 // Construct a new PathDiagnosticPiece.
870 PathDiagnosticLocation L(S, BRC.getSourceManager());
871 return new PathDiagnosticEventPiece(L, os.str());
872 }
873
874 return NULL;
875 }
876};
877} // end anonymous namespace
878
879static void registerTrackConstraint(BugReporterContext& BRC, SVal Constraint,
880 bool Assumption) {
881 BRC.addVisitor(new TrackConstraintBRVisitor(Constraint, Assumption));
882}
883
Ted Kremenek0c313172009-05-13 19:16:35 +0000884static void registerTrackNullOrUndefValue(BugReporterContext& BRC,
Ted Kremenek85ac9342009-05-15 05:25:09 +0000885 const Stmt *S,
886 const ExplodedNode<GRState>* N) {
Ted Kremenekdd986cc2009-05-07 00:45:33 +0000887
Ted Kremenek85ac9342009-05-15 05:25:09 +0000888 if (!S)
Ted Kremenekdd986cc2009-05-07 00:45:33 +0000889 return;
Ted Kremenek85ac9342009-05-15 05:25:09 +0000890
Ted Kremenek0c313172009-05-13 19:16:35 +0000891 GRStateManager &StateMgr = BRC.getStateManager();
Ted Kremenek85ac9342009-05-15 05:25:09 +0000892 const GRState *state = N->getState();
Ted Kremenekdd986cc2009-05-07 00:45:33 +0000893
Ted Kremenek85ac9342009-05-15 05:25:09 +0000894 if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(S)) {
Ted Kremenek0c313172009-05-13 19:16:35 +0000895 if (const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl())) {
896 const VarRegion *R =
897 StateMgr.getRegionManager().getVarRegion(VD);
898
899 // What did we load?
Ted Kremenek85ac9342009-05-15 05:25:09 +0000900 SVal V = StateMgr.GetSVal(state, S);
Ted Kremenek0c313172009-05-13 19:16:35 +0000901
Ted Kremenek85ac9342009-05-15 05:25:09 +0000902 if (isa<loc::ConcreteInt>(V) || isa<nonloc::ConcreteInt>(V)
903 || V.isUndef()) {
Ted Kremenek0c313172009-05-13 19:16:35 +0000904 registerFindLastStore(BRC, R, V);
905 }
906 }
907 }
Ted Kremenek0c313172009-05-13 19:16:35 +0000908
909 SVal V = StateMgr.GetSValAsScalarOrLoc(state, S);
Ted Kremenekdd986cc2009-05-07 00:45:33 +0000910
911 // Uncomment this to find cases where we aren't properly getting the
912 // base value that was dereferenced.
913 // assert(!V.isUnknownOrUndef());
914
Ted Kremenek7704a332009-05-07 21:49:45 +0000915 // Is it a symbolic value?
Ted Kremenekdd986cc2009-05-07 00:45:33 +0000916 if (loc::MemRegionVal *L = dyn_cast<loc::MemRegionVal>(&V)) {
917 const SubRegion *R = cast<SubRegion>(L->getRegion());
918 while (R && !isa<SymbolicRegion>(R)) {
919 R = dyn_cast<SubRegion>(R->getSuperRegion());
920 }
921
922 if (R) {
923 assert(isa<SymbolicRegion>(R));
924 registerTrackConstraint(BRC, loc::MemRegionVal(R), false);
925 }
926 }
927}
928
929//===----------------------------------------------------------------------===//
Ted Kremenek78d46242008-07-22 16:21:24 +0000930// Check registration.
Ted Kremenekcf118d42009-02-04 23:49:09 +0000931//===----------------------------------------------------------------------===//
Ted Kremenek78d46242008-07-22 16:21:24 +0000932
933void GRExprEngine::RegisterInternalChecks() {
Ted Kremenekcf118d42009-02-04 23:49:09 +0000934 // Register internal "built-in" BugTypes with the BugReporter. These BugTypes
935 // are different than what probably many checks will do since they don't
936 // create BugReports on-the-fly but instead wait until GRExprEngine finishes
937 // analyzing a function. Generation of BugReport objects is done via a call
938 // to 'FlushReports' from BugReporter.
939 BR.Register(new NullDeref(this));
940 BR.Register(new UndefinedDeref(this));
941 BR.Register(new UndefBranch(this));
942 BR.Register(new DivZero(this));
943 BR.Register(new UndefResult(this));
944 BR.Register(new BadCall(this));
945 BR.Register(new RetStack(this));
946 BR.Register(new RetUndef(this));
947 BR.Register(new BadArg(this));
948 BR.Register(new BadMsgExprArg(this));
949 BR.Register(new BadReceiver(this));
950 BR.Register(new OutOfBoundMemoryAccess(this));
951 BR.Register(new BadSizeVLA(this));
Ted Kremenek21fe8372009-02-19 04:06:22 +0000952 BR.Register(new NilReceiverStructRet(this));
Ted Kremenek899b3de2009-04-08 03:07:17 +0000953 BR.Register(new NilReceiverLargerThanVoidPtrRet(this));
Ted Kremenekcf118d42009-02-04 23:49:09 +0000954
955 // The following checks do not need to have their associated BugTypes
956 // explicitly registered with the BugReporter. If they issue any BugReports,
957 // their associated BugType will get registered with the BugReporter
958 // automatically. Note that the check itself is owned by the GRExprEngine
959 // object.
960 AddCheck(new CheckAttrNonNull(BR), Stmt::CallExprClass);
Ted Kremenek78d46242008-07-22 16:21:24 +0000961}