blob: a29862952989b32dded96418f02ff00d7791adf7 [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
Ted Kremenek78d46242008-07-22 16:21:24 +0000572 const NonNullAttr* Att = FD->getAttr<NonNullAttr>();
573
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();
634 return cast<ObjCMessageExpr>(S)->getReceiver();
635}
636
637static const Stmt *GetDenomExpr(const ExplodedNode<GRState> *N) {
638 const Stmt *S = N->getLocationAs<PostStmt>()->getStmt();
639 return cast<BinaryOperator>(S)->getRHS();
640}
641
642static const Stmt *GetCalleeExpr(const ExplodedNode<GRState> *N) {
643 const Stmt *S = N->getLocationAs<PostStmt>()->getStmt();
644 return cast<CallExpr>(S)->getCallee();
645}
646
647static const Stmt *GetRetValExpr(const ExplodedNode<GRState> *N) {
648 const Stmt *S = N->getLocationAs<PostStmt>()->getStmt();
649 return cast<ReturnStmt>(S)->getRetValue();
650}
651
Ted Kremenekdd986cc2009-05-07 00:45:33 +0000652namespace {
Ted Kremenek0c313172009-05-13 19:16:35 +0000653class VISIBILITY_HIDDEN FindLastStoreBRVisitor : public BugReporterVisitor {
Ted Kremenek7704a332009-05-07 21:49:45 +0000654 const MemRegion *R;
Ted Kremenek0c313172009-05-13 19:16:35 +0000655 SVal V;
656 bool satisfied;
657 const ExplodedNode<GRState> *StoreSite;
Ted Kremenek7704a332009-05-07 21:49:45 +0000658public:
Ted Kremenek0c313172009-05-13 19:16:35 +0000659 FindLastStoreBRVisitor(SVal v, const MemRegion *r)
660 : R(r), V(v), satisfied(false), StoreSite(0) {}
661
Ted Kremenek7704a332009-05-07 21:49:45 +0000662 PathDiagnosticPiece* VisitNode(const ExplodedNode<GRState> *N,
663 const ExplodedNode<GRState> *PrevN,
664 BugReporterContext& BRC) {
Ted Kremenek0c313172009-05-13 19:16:35 +0000665
666 if (satisfied)
Ted Kremenek7704a332009-05-07 21:49:45 +0000667 return NULL;
Ted Kremenek7704a332009-05-07 21:49:45 +0000668
Ted Kremenek0c313172009-05-13 19:16:35 +0000669 if (!StoreSite) {
670 GRStateManager &StateMgr = BRC.getStateManager();
671 const ExplodedNode<GRState> *Node = N, *Last = NULL;
672
673 for ( ; Node ; Last = Node, Node = Node->getFirstPred()) {
674
675 if (const VarRegion *VR = dyn_cast<VarRegion>(R)) {
676 if (const PostStmt *P = Node->getLocationAs<PostStmt>())
677 if (const DeclStmt *DS = P->getStmtAs<DeclStmt>())
678 if (DS->getSingleDecl() == VR->getDecl()) {
679 Last = Node;
680 break;
681 }
682 }
683
684 if (StateMgr.GetSVal(Node->getState(), R) != V)
685 break;
686 }
687
688 if (!Node || !Last) {
689 satisfied = true;
690 return NULL;
691 }
692
693 StoreSite = Last;
694 }
Ted Kremenek7704a332009-05-07 21:49:45 +0000695
Ted Kremenek0c313172009-05-13 19:16:35 +0000696 if (StoreSite != N)
697 return NULL;
698
699 satisfied = true;
700 std::string sbuf;
701 llvm::raw_string_ostream os(sbuf);
Ted Kremenek7704a332009-05-07 21:49:45 +0000702
Ted Kremenek0c313172009-05-13 19:16:35 +0000703 if (const PostStmt *PS = N->getLocationAs<PostStmt>()) {
704 if (const DeclStmt *DS = PS->getStmtAs<DeclStmt>()) {
705
706 if (const VarRegion *VR = dyn_cast<VarRegion>(R)) {
707 os << "Variable '" << VR->getDecl()->getNameAsString() << "' ";
708 }
709 else
710 return NULL;
711
712 if (isa<loc::ConcreteInt>(V)) {
713 bool b = false;
714 ASTContext &C = BRC.getASTContext();
715 if (R->isBoundable(C)) {
716 if (const TypedRegion *TR = dyn_cast<TypedRegion>(R)) {
717 if (C.isObjCObjectPointerType(TR->getValueType(C))) {
718 os << "initialized to nil";
719 b = true;
720 }
721 }
722 }
723
724 if (!b)
725 os << "initialized to a null pointer value";
726 }
Ted Kremenek85ac9342009-05-15 05:25:09 +0000727 else if (isa<nonloc::ConcreteInt>(V)) {
728 os << "initialized to " << cast<nonloc::ConcreteInt>(V).getValue();
729 }
Ted Kremenek0c313172009-05-13 19:16:35 +0000730 else if (V.isUndef()) {
731 if (isa<VarRegion>(R)) {
732 const VarDecl *VD = cast<VarDecl>(DS->getSingleDecl());
733 if (VD->getInit())
734 os << "initialized to a garbage value";
735 else
736 os << "declared without an initial value";
737 }
738 }
739 }
740 }
Ted Kremenek85ac9342009-05-15 05:25:09 +0000741
Ted Kremenek0c313172009-05-13 19:16:35 +0000742 if (os.str().empty()) {
743 if (isa<loc::ConcreteInt>(V)) {
744 bool b = false;
745 ASTContext &C = BRC.getASTContext();
746 if (R->isBoundable(C)) {
747 if (const TypedRegion *TR = dyn_cast<TypedRegion>(R)) {
748 if (C.isObjCObjectPointerType(TR->getValueType(C))) {
749 os << "nil object reference stored to ";
750 b = true;
751 }
752 }
753 }
754
755 if (!b)
756 os << "Null pointer value stored to ";
757 }
758 else if (V.isUndef()) {
759 os << "Uninitialized value stored to ";
760 }
761 else
762 return NULL;
Ted Kremenek7704a332009-05-07 21:49:45 +0000763
Ted Kremenek0c313172009-05-13 19:16:35 +0000764 if (const VarRegion *VR = dyn_cast<VarRegion>(R)) {
765 os << '\'' << VR->getDecl()->getNameAsString() << '\'';
766 }
767 else
768 return NULL;
769 }
770
771 // FIXME: Refactor this into BugReporterContext.
772 Stmt *S = 0;
773 ProgramPoint P = N->getLocation();
774
775 if (BlockEdge *BE = dyn_cast<BlockEdge>(&P)) {
776 CFGBlock *BSrc = BE->getSrc();
777 S = BSrc->getTerminatorCondition();
778 }
779 else if (PostStmt *PS = dyn_cast<PostStmt>(&P)) {
780 S = PS->getStmt();
781 }
782
783 if (!S)
784 return NULL;
785
786 // Construct a new PathDiagnosticPiece.
787 PathDiagnosticLocation L(S, BRC.getSourceManager());
788 return new PathDiagnosticEventPiece(L, os.str());
789 }
Ted Kremenek7704a332009-05-07 21:49:45 +0000790};
Ted Kremenek7704a332009-05-07 21:49:45 +0000791
Ted Kremenek0c313172009-05-13 19:16:35 +0000792
793static void registerFindLastStore(BugReporterContext& BRC, const MemRegion *R,
794 SVal V) {
795 BRC.addVisitor(new FindLastStoreBRVisitor(V, R));
796}
797
Ted Kremenekdd986cc2009-05-07 00:45:33 +0000798class VISIBILITY_HIDDEN TrackConstraintBRVisitor : public BugReporterVisitor {
799 SVal Constraint;
800 const bool Assumption;
801 bool isSatisfied;
802public:
803 TrackConstraintBRVisitor(SVal constraint, bool assumption)
804 : Constraint(constraint), Assumption(assumption), isSatisfied(false) {}
805
Ted Kremenek7704a332009-05-07 21:49:45 +0000806 PathDiagnosticPiece* VisitNode(const ExplodedNode<GRState> *N,
807 const ExplodedNode<GRState> *PrevN,
Ted Kremenekdd986cc2009-05-07 00:45:33 +0000808 BugReporterContext& BRC) {
809 if (isSatisfied)
810 return NULL;
811
812 // Check if in the previous state it was feasible for this constraint
813 // to *not* be true.
814
815 GRStateManager &StateMgr = BRC.getStateManager();
816 bool isFeasible = false;
817 if (StateMgr.Assume(PrevN->getState(), Constraint, !Assumption,
818 isFeasible)) {
819 assert(isFeasible); // Eventually we don't need 'isFeasible'.
820
821 isSatisfied = true;
822
823 // As a sanity check, make sure that the negation of the constraint
824 // was infeasible in the current state. If it is feasible, we somehow
825 // missed the transition point.
826 isFeasible = false;
827 if (StateMgr.Assume(N->getState(), Constraint, !Assumption,
828 isFeasible)) {
829 assert(isFeasible);
830 return NULL;
831 }
832
833 // We found the transition point for the constraint. We now need to
834 // pretty-print the constraint. (work-in-progress)
835 std::string sbuf;
836 llvm::raw_string_ostream os(sbuf);
837
838 if (isa<Loc>(Constraint)) {
839 os << "Assuming pointer value is ";
Ted Kremenek85ac9342009-05-15 05:25:09 +0000840 os << (Assumption ? "non-null" : "null");
Ted Kremenekdd986cc2009-05-07 00:45:33 +0000841 }
842
843 if (os.str().empty())
844 return NULL;
845
846 // FIXME: Refactor this into BugReporterContext.
847 Stmt *S = 0;
848 ProgramPoint P = N->getLocation();
849
850 if (BlockEdge *BE = dyn_cast<BlockEdge>(&P)) {
851 CFGBlock *BSrc = BE->getSrc();
852 S = BSrc->getTerminatorCondition();
853 }
854 else if (PostStmt *PS = dyn_cast<PostStmt>(&P)) {
855 S = PS->getStmt();
856 }
857
858 if (!S)
859 return NULL;
860
861 // Construct a new PathDiagnosticPiece.
862 PathDiagnosticLocation L(S, BRC.getSourceManager());
863 return new PathDiagnosticEventPiece(L, os.str());
864 }
865
866 return NULL;
867 }
868};
869} // end anonymous namespace
870
871static void registerTrackConstraint(BugReporterContext& BRC, SVal Constraint,
872 bool Assumption) {
873 BRC.addVisitor(new TrackConstraintBRVisitor(Constraint, Assumption));
874}
875
Ted Kremenek0c313172009-05-13 19:16:35 +0000876static void registerTrackNullOrUndefValue(BugReporterContext& BRC,
Ted Kremenek85ac9342009-05-15 05:25:09 +0000877 const Stmt *S,
878 const ExplodedNode<GRState>* N) {
Ted Kremenekdd986cc2009-05-07 00:45:33 +0000879
Ted Kremenek85ac9342009-05-15 05:25:09 +0000880 if (!S)
Ted Kremenekdd986cc2009-05-07 00:45:33 +0000881 return;
Ted Kremenek85ac9342009-05-15 05:25:09 +0000882
Ted Kremenek0c313172009-05-13 19:16:35 +0000883 GRStateManager &StateMgr = BRC.getStateManager();
Ted Kremenek85ac9342009-05-15 05:25:09 +0000884 const GRState *state = N->getState();
Ted Kremenekdd986cc2009-05-07 00:45:33 +0000885
Ted Kremenek85ac9342009-05-15 05:25:09 +0000886 if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(S)) {
Ted Kremenek0c313172009-05-13 19:16:35 +0000887 if (const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl())) {
888 const VarRegion *R =
889 StateMgr.getRegionManager().getVarRegion(VD);
890
891 // What did we load?
Ted Kremenek85ac9342009-05-15 05:25:09 +0000892 SVal V = StateMgr.GetSVal(state, S);
Ted Kremenek0c313172009-05-13 19:16:35 +0000893
Ted Kremenek85ac9342009-05-15 05:25:09 +0000894 if (isa<loc::ConcreteInt>(V) || isa<nonloc::ConcreteInt>(V)
895 || V.isUndef()) {
Ted Kremenek0c313172009-05-13 19:16:35 +0000896 registerFindLastStore(BRC, R, V);
897 }
898 }
899 }
Ted Kremenek0c313172009-05-13 19:16:35 +0000900
901 SVal V = StateMgr.GetSValAsScalarOrLoc(state, S);
Ted Kremenekdd986cc2009-05-07 00:45:33 +0000902
903 // Uncomment this to find cases where we aren't properly getting the
904 // base value that was dereferenced.
905 // assert(!V.isUnknownOrUndef());
906
Ted Kremenek7704a332009-05-07 21:49:45 +0000907 // Is it a symbolic value?
Ted Kremenekdd986cc2009-05-07 00:45:33 +0000908 if (loc::MemRegionVal *L = dyn_cast<loc::MemRegionVal>(&V)) {
909 const SubRegion *R = cast<SubRegion>(L->getRegion());
910 while (R && !isa<SymbolicRegion>(R)) {
911 R = dyn_cast<SubRegion>(R->getSuperRegion());
912 }
913
914 if (R) {
915 assert(isa<SymbolicRegion>(R));
916 registerTrackConstraint(BRC, loc::MemRegionVal(R), false);
917 }
918 }
919}
920
921//===----------------------------------------------------------------------===//
Ted Kremenek78d46242008-07-22 16:21:24 +0000922// Check registration.
Ted Kremenekcf118d42009-02-04 23:49:09 +0000923//===----------------------------------------------------------------------===//
Ted Kremenek78d46242008-07-22 16:21:24 +0000924
925void GRExprEngine::RegisterInternalChecks() {
Ted Kremenekcf118d42009-02-04 23:49:09 +0000926 // Register internal "built-in" BugTypes with the BugReporter. These BugTypes
927 // are different than what probably many checks will do since they don't
928 // create BugReports on-the-fly but instead wait until GRExprEngine finishes
929 // analyzing a function. Generation of BugReport objects is done via a call
930 // to 'FlushReports' from BugReporter.
931 BR.Register(new NullDeref(this));
932 BR.Register(new UndefinedDeref(this));
933 BR.Register(new UndefBranch(this));
934 BR.Register(new DivZero(this));
935 BR.Register(new UndefResult(this));
936 BR.Register(new BadCall(this));
937 BR.Register(new RetStack(this));
938 BR.Register(new RetUndef(this));
939 BR.Register(new BadArg(this));
940 BR.Register(new BadMsgExprArg(this));
941 BR.Register(new BadReceiver(this));
942 BR.Register(new OutOfBoundMemoryAccess(this));
943 BR.Register(new BadSizeVLA(this));
Ted Kremenek21fe8372009-02-19 04:06:22 +0000944 BR.Register(new NilReceiverStructRet(this));
Ted Kremenek899b3de2009-04-08 03:07:17 +0000945 BR.Register(new NilReceiverLargerThanVoidPtrRet(this));
Ted Kremenekcf118d42009-02-04 23:49:09 +0000946
947 // The following checks do not need to have their associated BugTypes
948 // explicitly registered with the BugReporter. If they issue any BugReports,
949 // their associated BugType will get registered with the BugReporter
950 // automatically. Note that the check itself is owned by the GRExprEngine
951 // object.
952 AddCheck(new CheckAttrNonNull(BR), Stmt::CallExprClass);
Ted Kremenek78d46242008-07-22 16:21:24 +0000953}