blob: 7690aa1a25b1589ee04cb7d26a0e03d585fbce3d [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 Kremenek0c313172009-05-13 19:16:35 +000042static void registerTrackNullOrUndefValue(BugReporterContext& BRC,
Ted Kremenekdd986cc2009-05-07 00:45:33 +000043 const ExplodedNode<GRState>* N);
44
45//===----------------------------------------------------------------------===//
Ted Kremenek78d46242008-07-22 16:21:24 +000046// Bug Descriptions.
47//===----------------------------------------------------------------------===//
48
49namespace {
Ted Kremenekdd986cc2009-05-07 00:45:33 +000050
Ted Kremenek0c313172009-05-13 19:16:35 +000051class VISIBILITY_HIDDEN BuiltinBugReport : public RangedBugReport {
Ted Kremenekdd986cc2009-05-07 00:45:33 +000052public:
53 BuiltinBugReport(BugType& bt, const char* desc,
Ted Kremenek0c313172009-05-13 19:16:35 +000054 ExplodedNode<GRState> *n)
55 : RangedBugReport(bt, desc, n) {}
Ted Kremenekdd986cc2009-05-07 00:45:33 +000056
57 void registerInitialVisitors(BugReporterContext& BRC,
58 const ExplodedNode<GRState>* N);
59};
60
Ted Kremenekcf118d42009-02-04 23:49:09 +000061class VISIBILITY_HIDDEN BuiltinBug : public BugType {
62 GRExprEngine &Eng;
Ted Kremenek159d2482008-12-09 00:44:16 +000063protected:
Ted Kremenekcf118d42009-02-04 23:49:09 +000064 const std::string desc;
Ted Kremenek78d46242008-07-22 16:21:24 +000065public:
Ted Kremenekcf118d42009-02-04 23:49:09 +000066 BuiltinBug(GRExprEngine *eng, const char* n, const char* d)
Ted Kremenek0c313172009-05-13 19:16:35 +000067 : BugType(n, "Logic errors"), Eng(*eng), desc(d) {}
Ted Kremenekcf118d42009-02-04 23:49:09 +000068
69 BuiltinBug(GRExprEngine *eng, const char* n)
Ted Kremenek0c313172009-05-13 19:16:35 +000070 : BugType(n, "Logic errors"), Eng(*eng), desc(n) {}
Ted Kremenek22bda882008-07-31 20:31:27 +000071
Ted Kremenekcf118d42009-02-04 23:49:09 +000072 virtual void FlushReportsImpl(BugReporter& BR, GRExprEngine& Eng) = 0;
73
74 void FlushReports(BugReporter& BR) { FlushReportsImpl(BR, Eng); }
Ted Kremenek78d46242008-07-22 16:21:24 +000075
Ted Kremenekdd986cc2009-05-07 00:45:33 +000076 virtual void registerInitialVisitors(BugReporterContext& BRC,
77 const ExplodedNode<GRState>* N,
78 BuiltinBugReport *R) {}
79
80 template <typename ITER> void Emit(BugReporter& BR, ITER I, ITER E);
Ted Kremenek78d46242008-07-22 16:21:24 +000081};
82
Ted Kremenekdd986cc2009-05-07 00:45:33 +000083
84template <typename ITER>
85void BuiltinBug::Emit(BugReporter& BR, ITER I, ITER E) {
86 for (; I != E; ++I) BR.EmitReport(new BuiltinBugReport(*this, desc.c_str(),
87 GetNode(I)));
88}
89
90void BuiltinBugReport::registerInitialVisitors(BugReporterContext& BRC,
91 const ExplodedNode<GRState>* N) {
92 static_cast<BuiltinBug&>(getBugType()).registerInitialVisitors(BRC, N, this);
93}
94
Ted Kremenek78d46242008-07-22 16:21:24 +000095class VISIBILITY_HIDDEN NullDeref : public BuiltinBug {
96public:
Ted Kremenekcf118d42009-02-04 23:49:09 +000097 NullDeref(GRExprEngine* eng)
Ted Kremenek0fa96542009-04-07 04:54:31 +000098 : BuiltinBug(eng,"Null dereference", "Dereference of null pointer") {}
Ted Kremenek78d46242008-07-22 16:21:24 +000099
Ted Kremenekcf118d42009-02-04 23:49:09 +0000100 void FlushReportsImpl(BugReporter& BR, GRExprEngine& Eng) {
Ted Kremenek78d46242008-07-22 16:21:24 +0000101 Emit(BR, Eng.null_derefs_begin(), Eng.null_derefs_end());
102 }
Ted Kremenekdd986cc2009-05-07 00:45:33 +0000103
104 void registerInitialVisitors(BugReporterContext& BRC,
105 const ExplodedNode<GRState>* N,
106 BuiltinBugReport *R) {
Ted Kremenek0c313172009-05-13 19:16:35 +0000107 registerTrackNullOrUndefValue(BRC, N);
Ted Kremenekdd986cc2009-05-07 00:45:33 +0000108 }
Ted Kremenek78d46242008-07-22 16:21:24 +0000109};
110
Ted Kremenek0c313172009-05-13 19:16:35 +0000111class VISIBILITY_HIDDEN NilReceiverStructRet : public BuiltinBug {
Ted Kremenek21fe8372009-02-19 04:06:22 +0000112public:
113 NilReceiverStructRet(GRExprEngine* eng) :
Ted Kremenek0c313172009-05-13 19:16:35 +0000114 BuiltinBug(eng, "'nil' receiver with struct return type") {}
Ted Kremenek21fe8372009-02-19 04:06:22 +0000115
Ted Kremenek0c313172009-05-13 19:16:35 +0000116 void FlushReportsImpl(BugReporter& BR, GRExprEngine& Eng) {
Ted Kremenek21fe8372009-02-19 04:06:22 +0000117 for (GRExprEngine::nil_receiver_struct_ret_iterator
118 I=Eng.nil_receiver_struct_ret_begin(),
119 E=Eng.nil_receiver_struct_ret_end(); I!=E; ++I) {
120
121 std::string sbuf;
122 llvm::raw_string_ostream os(sbuf);
123 PostStmt P = cast<PostStmt>((*I)->getLocation());
124 ObjCMessageExpr *ME = cast<ObjCMessageExpr>(P.getStmt());
125 os << "The receiver in the message expression is 'nil' and results in the"
126 " returned value (of type '"
127 << ME->getType().getAsString()
128 << "') to be garbage or otherwise undefined.";
129
Ted Kremenek0c313172009-05-13 19:16:35 +0000130 BuiltinBugReport *R = new BuiltinBugReport(*this, os.str().c_str(), *I);
Ted Kremenek21fe8372009-02-19 04:06:22 +0000131 R->addRange(ME->getReceiver()->getSourceRange());
132 BR.EmitReport(R);
133 }
134 }
Ted Kremenek0c313172009-05-13 19:16:35 +0000135
136 void registerInitialVisitors(BugReporterContext& BRC,
137 const ExplodedNode<GRState>* N,
138 BuiltinBugReport *R) {
139 registerTrackNullOrUndefValue(BRC, N);
140 }
Ted Kremenek21fe8372009-02-19 04:06:22 +0000141};
Ted Kremenek899b3de2009-04-08 03:07:17 +0000142
Ted Kremenek0c313172009-05-13 19:16:35 +0000143class VISIBILITY_HIDDEN NilReceiverLargerThanVoidPtrRet : public BuiltinBug {
Ted Kremenek899b3de2009-04-08 03:07:17 +0000144public:
145 NilReceiverLargerThanVoidPtrRet(GRExprEngine* eng) :
Ted Kremenek0c313172009-05-13 19:16:35 +0000146 BuiltinBug(eng,
147 "'nil' receiver with return type larger than sizeof(void *)") {}
Ted Kremenek899b3de2009-04-08 03:07:17 +0000148
Ted Kremenek0c313172009-05-13 19:16:35 +0000149 void FlushReportsImpl(BugReporter& BR, GRExprEngine& Eng) {
Ted Kremenek899b3de2009-04-08 03:07:17 +0000150 for (GRExprEngine::nil_receiver_larger_than_voidptr_ret_iterator
151 I=Eng.nil_receiver_larger_than_voidptr_ret_begin(),
152 E=Eng.nil_receiver_larger_than_voidptr_ret_end(); I!=E; ++I) {
153
154 std::string sbuf;
155 llvm::raw_string_ostream os(sbuf);
156 PostStmt P = cast<PostStmt>((*I)->getLocation());
157 ObjCMessageExpr *ME = cast<ObjCMessageExpr>(P.getStmt());
158 os << "The receiver in the message expression is 'nil' and results in the"
159 " returned value (of type '"
160 << ME->getType().getAsString()
161 << "' and of size "
162 << Eng.getContext().getTypeSize(ME->getType()) / 8
163 << " bytes) to be garbage or otherwise undefined.";
164
Ted Kremenek0c313172009-05-13 19:16:35 +0000165 BuiltinBugReport *R = new BuiltinBugReport(*this, os.str().c_str(), *I);
Ted Kremenek899b3de2009-04-08 03:07:17 +0000166 R->addRange(ME->getReceiver()->getSourceRange());
167 BR.EmitReport(R);
168 }
Ted Kremenek0c313172009-05-13 19:16:35 +0000169 }
170 void registerInitialVisitors(BugReporterContext& BRC,
171 const ExplodedNode<GRState>* N,
172 BuiltinBugReport *R) {
173 registerTrackNullOrUndefValue(BRC, N);
Ted Kremenek899b3de2009-04-08 03:07:17 +0000174 }
175};
Ted Kremenek21fe8372009-02-19 04:06:22 +0000176
Ted Kremenek78d46242008-07-22 16:21:24 +0000177class VISIBILITY_HIDDEN UndefinedDeref : public BuiltinBug {
178public:
Ted Kremenekcf118d42009-02-04 23:49:09 +0000179 UndefinedDeref(GRExprEngine* eng)
Ted Kremenek17a8e072009-03-01 05:43:22 +0000180 : BuiltinBug(eng,"Dereference of undefined pointer value") {}
Ted Kremenek78d46242008-07-22 16:21:24 +0000181
Ted Kremenekcf118d42009-02-04 23:49:09 +0000182 void FlushReportsImpl(BugReporter& BR, GRExprEngine& Eng) {
Ted Kremenek78d46242008-07-22 16:21:24 +0000183 Emit(BR, Eng.undef_derefs_begin(), Eng.undef_derefs_end());
184 }
Ted Kremenek0c313172009-05-13 19:16:35 +0000185
186 void registerInitialVisitors(BugReporterContext& BRC,
187 const ExplodedNode<GRState>* N,
188 BuiltinBugReport *R) {
189 registerTrackNullOrUndefValue(BRC, N);
190 }
Ted Kremenek78d46242008-07-22 16:21:24 +0000191};
192
193class VISIBILITY_HIDDEN DivZero : public BuiltinBug {
194public:
Ted Kremenekcf118d42009-02-04 23:49:09 +0000195 DivZero(GRExprEngine* eng)
Ted Kremenek5d88ff82009-04-02 02:40:26 +0000196 : BuiltinBug(eng,"Division-by-zero",
197 "Division by zero or undefined value.") {}
Ted Kremenek78d46242008-07-22 16:21:24 +0000198
Ted Kremenekcf118d42009-02-04 23:49:09 +0000199 void FlushReportsImpl(BugReporter& BR, GRExprEngine& Eng) {
Ted Kremenek78d46242008-07-22 16:21:24 +0000200 Emit(BR, Eng.explicit_bad_divides_begin(), Eng.explicit_bad_divides_end());
201 }
202};
203
204class VISIBILITY_HIDDEN UndefResult : public BuiltinBug {
205public:
Ted Kremenek5d88ff82009-04-02 02:40:26 +0000206 UndefResult(GRExprEngine* eng) : BuiltinBug(eng,"Undefined result",
Ted Kremenek78d46242008-07-22 16:21:24 +0000207 "Result of operation is undefined.") {}
208
Ted Kremenekcf118d42009-02-04 23:49:09 +0000209 void FlushReportsImpl(BugReporter& BR, GRExprEngine& Eng) {
Ted Kremenek78d46242008-07-22 16:21:24 +0000210 Emit(BR, Eng.undef_results_begin(), Eng.undef_results_end());
211 }
212};
213
214class VISIBILITY_HIDDEN BadCall : public BuiltinBug {
215public:
Ted Kremenekcf118d42009-02-04 23:49:09 +0000216 BadCall(GRExprEngine *eng)
Ted Kremenek5d88ff82009-04-02 02:40:26 +0000217 : BuiltinBug(eng, "Invalid function call",
Ted Kremenek17a8e072009-03-01 05:43:22 +0000218 "Called function pointer is a null or undefined pointer value") {}
Ted Kremenek78d46242008-07-22 16:21:24 +0000219
Ted Kremenekcf118d42009-02-04 23:49:09 +0000220 void FlushReportsImpl(BugReporter& BR, GRExprEngine& Eng) {
Ted Kremenek78d46242008-07-22 16:21:24 +0000221 Emit(BR, Eng.bad_calls_begin(), Eng.bad_calls_end());
222 }
223};
224
225class VISIBILITY_HIDDEN BadArg : public BuiltinBug {
226public:
Ted Kremenek5d88ff82009-04-02 02:40:26 +0000227 BadArg(GRExprEngine* eng) : BuiltinBug(eng,"Uninitialized argument",
Ted Kremenek17a8e072009-03-01 05:43:22 +0000228 "Pass-by-value argument in function call is undefined.") {}
Ted Kremenek78d46242008-07-22 16:21:24 +0000229
Ted Kremenekcf118d42009-02-04 23:49:09 +0000230 BadArg(GRExprEngine* eng, const char* d)
Ted Kremenek5d88ff82009-04-02 02:40:26 +0000231 : BuiltinBug(eng,"Uninitialized argument", d) {}
Ted Kremenek78d46242008-07-22 16:21:24 +0000232
Ted Kremenekcf118d42009-02-04 23:49:09 +0000233 void FlushReportsImpl(BugReporter& BR, GRExprEngine& Eng) {
Ted Kremenek78d46242008-07-22 16:21:24 +0000234 for (GRExprEngine::UndefArgsTy::iterator I = Eng.undef_arg_begin(),
235 E = Eng.undef_arg_end(); I!=E; ++I) {
Ted Kremenek78d46242008-07-22 16:21:24 +0000236 // Generate a report for this bug.
Ted Kremenekcf118d42009-02-04 23:49:09 +0000237 RangedBugReport *report = new RangedBugReport(*this, desc.c_str(),
238 I->first);
239 report->addRange(I->second->getSourceRange());
240 BR.EmitReport(report);
Ted Kremenek78d46242008-07-22 16:21:24 +0000241 }
242 }
243};
244
245class VISIBILITY_HIDDEN BadMsgExprArg : public BadArg {
246public:
Ted Kremenekcf118d42009-02-04 23:49:09 +0000247 BadMsgExprArg(GRExprEngine* eng)
Ted Kremenek5d88ff82009-04-02 02:40:26 +0000248 : BadArg(eng,"Pass-by-value argument in message expression is undefined"){}
Ted Kremenek78d46242008-07-22 16:21:24 +0000249
Ted Kremenekcf118d42009-02-04 23:49:09 +0000250 void FlushReportsImpl(BugReporter& BR, GRExprEngine& Eng) {
Ted Kremenek78d46242008-07-22 16:21:24 +0000251 for (GRExprEngine::UndefArgsTy::iterator I=Eng.msg_expr_undef_arg_begin(),
Ted Kremenekcf118d42009-02-04 23:49:09 +0000252 E = Eng.msg_expr_undef_arg_end(); I!=E; ++I) {
Ted Kremenek78d46242008-07-22 16:21:24 +0000253 // Generate a report for this bug.
Ted Kremenek0c313172009-05-13 19:16:35 +0000254 BuiltinBugReport *report = new BuiltinBugReport(*this, desc.c_str(),
255 I->first);
Ted Kremenekcf118d42009-02-04 23:49:09 +0000256 report->addRange(I->second->getSourceRange());
257 BR.EmitReport(report);
Ted Kremenek78d46242008-07-22 16:21:24 +0000258 }
259 }
260};
261
262class VISIBILITY_HIDDEN BadReceiver : public BuiltinBug {
263public:
Ted Kremenekcf118d42009-02-04 23:49:09 +0000264 BadReceiver(GRExprEngine* eng)
Ted Kremenek5d88ff82009-04-02 02:40:26 +0000265 : BuiltinBug(eng,"Uninitialized receiver",
266 "Receiver in message expression is an uninitialized value") {}
Ted Kremenek78d46242008-07-22 16:21:24 +0000267
Ted Kremenekcf118d42009-02-04 23:49:09 +0000268 void FlushReportsImpl(BugReporter& BR, GRExprEngine& Eng) {
Ted Kremenekefd59942008-12-08 22:47:34 +0000269 for (GRExprEngine::ErrorNodes::iterator I=Eng.undef_receivers_begin(),
Ted Kremenek78d46242008-07-22 16:21:24 +0000270 End = Eng.undef_receivers_end(); I!=End; ++I) {
271
272 // Generate a report for this bug.
Ted Kremenek0c313172009-05-13 19:16:35 +0000273 BuiltinBugReport *report = new BuiltinBugReport(*this, desc.c_str(), *I);
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000274 ExplodedNode<GRState>* N = *I;
Ted Kremenek78d46242008-07-22 16:21:24 +0000275 Stmt *S = cast<PostStmt>(N->getLocation()).getStmt();
276 Expr* E = cast<ObjCMessageExpr>(S)->getReceiver();
277 assert (E && "Receiver cannot be NULL");
Ted Kremenekcf118d42009-02-04 23:49:09 +0000278 report->addRange(E->getSourceRange());
279 BR.EmitReport(report);
Ted Kremenek0c313172009-05-13 19:16:35 +0000280 }
281 }
282
283 void registerInitialVisitors(BugReporterContext& BRC,
284 const ExplodedNode<GRState>* N,
285 BuiltinBugReport *R) {
286 registerTrackNullOrUndefValue(BRC, N);
Ted Kremenek78d46242008-07-22 16:21:24 +0000287 }
288};
Ted Kremenek5917d782008-11-21 00:27:44 +0000289
Ted Kremenek78d46242008-07-22 16:21:24 +0000290class VISIBILITY_HIDDEN RetStack : public BuiltinBug {
291public:
Ted Kremenek17a8e072009-03-01 05:43:22 +0000292 RetStack(GRExprEngine* eng)
Ted Kremenek5d88ff82009-04-02 02:40:26 +0000293 : BuiltinBug(eng, "Return of address to stack-allocated memory") {}
Ted Kremenek78d46242008-07-22 16:21:24 +0000294
Ted Kremenekcf118d42009-02-04 23:49:09 +0000295 void FlushReportsImpl(BugReporter& BR, GRExprEngine& Eng) {
Ted Kremenekb7714b22008-07-30 17:49:12 +0000296 for (GRExprEngine::ret_stackaddr_iterator I=Eng.ret_stackaddr_begin(),
297 End = Eng.ret_stackaddr_end(); I!=End; ++I) {
Ted Kremenek22bda882008-07-31 20:31:27 +0000298
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000299 ExplodedNode<GRState>* N = *I;
Ted Kremenekb7714b22008-07-30 17:49:12 +0000300 Stmt *S = cast<PostStmt>(N->getLocation()).getStmt();
301 Expr* E = cast<ReturnStmt>(S)->getRetValue();
302 assert (E && "Return expression cannot be NULL");
Ted Kremenek22bda882008-07-31 20:31:27 +0000303
304 // Get the value associated with E.
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000305 loc::MemRegionVal V =
306 cast<loc::MemRegionVal>(Eng.getStateManager().GetSVal(N->getState(),
Ted Kremenek9e240492008-10-04 05:50:14 +0000307 E));
Ted Kremenek22bda882008-07-31 20:31:27 +0000308
309 // Generate a report for this bug.
Ted Kremenekad51a602008-10-31 00:18:30 +0000310 std::string buf;
311 llvm::raw_string_ostream os(buf);
Ted Kremenek8aed8062008-10-31 00:13:20 +0000312 SourceRange R;
Ted Kremenek22bda882008-07-31 20:31:27 +0000313
Ted Kremenek8aed8062008-10-31 00:13:20 +0000314 // Check if the region is a compound literal.
315 if (const CompoundLiteralRegion* CR =
316 dyn_cast<CompoundLiteralRegion>(V.getRegion())) {
317
318 const CompoundLiteralExpr* CL = CR->getLiteralExpr();
319 os << "Address of stack memory associated with a compound literal "
320 "declared on line "
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000321 << BR.getSourceManager()
322 .getInstantiationLineNumber(CL->getLocStart())
Ted Kremenek8aed8062008-10-31 00:13:20 +0000323 << " returned.";
324
325 R = CL->getSourceRange();
326 }
Ted Kremenekde8cd192008-11-02 00:35:25 +0000327 else if (const AllocaRegion* AR = dyn_cast<AllocaRegion>(V.getRegion())) {
328 const Expr* ARE = AR->getExpr();
329 SourceLocation L = ARE->getLocStart();
330 R = ARE->getSourceRange();
331
332 os << "Address of stack memory allocated by call to alloca() on line "
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000333 << BR.getSourceManager().getInstantiationLineNumber(L)
Ted Kremenekde8cd192008-11-02 00:35:25 +0000334 << " returned.";
335 }
Ted Kremenek8aed8062008-10-31 00:13:20 +0000336 else {
337 os << "Address of stack memory associated with local variable '"
338 << V.getRegion()->getString() << "' returned.";
339 }
Ted Kremenek22bda882008-07-31 20:31:27 +0000340
Ted Kremenekcf118d42009-02-04 23:49:09 +0000341 RangedBugReport *report = new RangedBugReport(*this, os.str().c_str(), N);
342 report->addRange(E->getSourceRange());
343 if (R.isValid()) report->addRange(R);
344 BR.EmitReport(report);
Ted Kremenekb7714b22008-07-30 17:49:12 +0000345 }
Ted Kremenek78d46242008-07-22 16:21:24 +0000346 }
347};
Ted Kremenek5917d782008-11-21 00:27:44 +0000348
349class VISIBILITY_HIDDEN RetUndef : public BuiltinBug {
350public:
Ted Kremenek5d88ff82009-04-02 02:40:26 +0000351 RetUndef(GRExprEngine* eng) : BuiltinBug(eng, "Uninitialized return value",
Ted Kremenek0c313172009-05-13 19:16:35 +0000352 "Uninitialized or undefined value returned to caller.") {}
Ted Kremenek5917d782008-11-21 00:27:44 +0000353
Ted Kremenekcf118d42009-02-04 23:49:09 +0000354 void FlushReportsImpl(BugReporter& BR, GRExprEngine& Eng) {
Ted Kremenek5917d782008-11-21 00:27:44 +0000355 Emit(BR, Eng.ret_undef_begin(), Eng.ret_undef_end());
356 }
Ted Kremenek0c313172009-05-13 19:16:35 +0000357
358 void registerInitialVisitors(BugReporterContext& BRC,
359 const ExplodedNode<GRState>* N,
360 BuiltinBugReport *R) {
361 registerTrackNullOrUndefValue(BRC, N);
362 }
Ted Kremenek5917d782008-11-21 00:27:44 +0000363};
Ted Kremenek78d46242008-07-22 16:21:24 +0000364
365class VISIBILITY_HIDDEN UndefBranch : public BuiltinBug {
366 struct VISIBILITY_HIDDEN FindUndefExpr {
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000367 GRStateManager& VM;
368 const GRState* St;
Ted Kremenek78d46242008-07-22 16:21:24 +0000369
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000370 FindUndefExpr(GRStateManager& V, const GRState* S) : VM(V), St(S) {}
Ted Kremenek78d46242008-07-22 16:21:24 +0000371
Ted Kremenekb7714b22008-07-30 17:49:12 +0000372 Expr* FindExpr(Expr* Ex) {
Ted Kremenek78d46242008-07-22 16:21:24 +0000373 if (!MatchesCriteria(Ex))
Ted Kremenekb7714b22008-07-30 17:49:12 +0000374 return 0;
Ted Kremenek78d46242008-07-22 16:21:24 +0000375
Ted Kremenekb7714b22008-07-30 17:49:12 +0000376 for (Stmt::child_iterator I=Ex->child_begin(), E=Ex->child_end();I!=E;++I)
Ted Kremenek78d46242008-07-22 16:21:24 +0000377 if (Expr* ExI = dyn_cast_or_null<Expr>(*I)) {
378 Expr* E2 = FindExpr(ExI);
379 if (E2) return E2;
380 }
381
382 return Ex;
383 }
384
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000385 bool MatchesCriteria(Expr* Ex) { return VM.GetSVal(St, Ex).isUndef(); }
Ted Kremenek78d46242008-07-22 16:21:24 +0000386 };
387
388public:
Ted Kremenekcf118d42009-02-04 23:49:09 +0000389 UndefBranch(GRExprEngine *eng)
Ted Kremenek5d88ff82009-04-02 02:40:26 +0000390 : BuiltinBug(eng,"Use of uninitialized value",
Ted Kremenek78d46242008-07-22 16:21:24 +0000391 "Branch condition evaluates to an uninitialized value.") {}
392
Ted Kremenekcf118d42009-02-04 23:49:09 +0000393 void FlushReportsImpl(BugReporter& BR, GRExprEngine& Eng) {
Ted Kremenek78d46242008-07-22 16:21:24 +0000394 for (GRExprEngine::undef_branch_iterator I=Eng.undef_branches_begin(),
395 E=Eng.undef_branches_end(); I!=E; ++I) {
396
397 // What's going on here: we want to highlight the subexpression of the
398 // condition that is the most likely source of the "uninitialized
399 // branch condition." We do a recursive walk of the condition's
400 // subexpressions and roughly look for the most nested subexpression
401 // that binds to Undefined. We then highlight that expression's range.
Ted Kremenek78d46242008-07-22 16:21:24 +0000402 BlockEdge B = cast<BlockEdge>((*I)->getLocation());
403 Expr* Ex = cast<Expr>(B.getSrc()->getTerminatorCondition());
404 assert (Ex && "Block must have a terminator.");
405
406 // Get the predecessor node and check if is a PostStmt with the Stmt
407 // being the terminator condition. We want to inspect the state
408 // of that node instead because it will contain main information about
409 // the subexpressions.
Ted Kremenek78d46242008-07-22 16:21:24 +0000410 assert (!(*I)->pred_empty());
411
412 // Note: any predecessor will do. They should have identical state,
413 // since all the BlockEdge did was act as an error sink since the value
414 // had to already be undefined.
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000415 ExplodedNode<GRState> *N = *(*I)->pred_begin();
Ted Kremenek78d46242008-07-22 16:21:24 +0000416 ProgramPoint P = N->getLocation();
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000417 const GRState* St = (*I)->getState();
Ted Kremenek78d46242008-07-22 16:21:24 +0000418
419 if (PostStmt* PS = dyn_cast<PostStmt>(&P))
420 if (PS->getStmt() == Ex)
421 St = N->getState();
422
423 FindUndefExpr FindIt(Eng.getStateManager(), St);
424 Ex = FindIt.FindExpr(Ex);
425
Ted Kremenekcf118d42009-02-04 23:49:09 +0000426 RangedBugReport *R = new RangedBugReport(*this, desc.c_str(), *I);
427 R->addRange(Ex->getSourceRange());
428 BR.EmitReport(R);
Ted Kremenek78d46242008-07-22 16:21:24 +0000429 }
430 }
431};
432
Zhongxing Xu1c0c2332008-11-23 05:52:28 +0000433class VISIBILITY_HIDDEN OutOfBoundMemoryAccess : public BuiltinBug {
434public:
Ted Kremenekcf118d42009-02-04 23:49:09 +0000435 OutOfBoundMemoryAccess(GRExprEngine* eng)
Ted Kremenek5d88ff82009-04-02 02:40:26 +0000436 : BuiltinBug(eng,"Out-of-bounds memory access",
Ted Kremenekcf118d42009-02-04 23:49:09 +0000437 "Load or store into an out-of-bound memory position.") {}
Zhongxing Xu1c0c2332008-11-23 05:52:28 +0000438
Ted Kremenekcf118d42009-02-04 23:49:09 +0000439 void FlushReportsImpl(BugReporter& BR, GRExprEngine& Eng) {
Zhongxing Xu1c0c2332008-11-23 05:52:28 +0000440 Emit(BR, Eng.explicit_oob_memacc_begin(), Eng.explicit_oob_memacc_end());
441 }
442};
Ted Kremenekefd59942008-12-08 22:47:34 +0000443
Ted Kremenek159d2482008-12-09 00:44:16 +0000444class VISIBILITY_HIDDEN BadSizeVLA : public BuiltinBug {
Ted Kremenekefd59942008-12-08 22:47:34 +0000445public:
Ted Kremenek5d88ff82009-04-02 02:40:26 +0000446 BadSizeVLA(GRExprEngine* eng) :
447 BuiltinBug(eng, "Bad variable-length array (VLA) size") {}
Ted Kremenekefd59942008-12-08 22:47:34 +0000448
Ted Kremenekcf118d42009-02-04 23:49:09 +0000449 void FlushReportsImpl(BugReporter& BR, GRExprEngine& Eng) {
Ted Kremenekefd59942008-12-08 22:47:34 +0000450 for (GRExprEngine::ErrorNodes::iterator
Ted Kremenek159d2482008-12-09 00:44:16 +0000451 I = Eng.ExplicitBadSizedVLA.begin(),
452 E = Eng.ExplicitBadSizedVLA.end(); I!=E; ++I) {
453
454 // Determine whether this was a 'zero-sized' VLA or a VLA with an
455 // undefined size.
456 GRExprEngine::NodeTy* N = *I;
457 PostStmt PS = cast<PostStmt>(N->getLocation());
Ted Kremenekefd59942008-12-08 22:47:34 +0000458 DeclStmt *DS = cast<DeclStmt>(PS.getStmt());
459 VarDecl* VD = cast<VarDecl>(*DS->decl_begin());
460 QualType T = Eng.getContext().getCanonicalType(VD->getType());
461 VariableArrayType* VT = cast<VariableArrayType>(T);
Ted Kremenek159d2482008-12-09 00:44:16 +0000462 Expr* SizeExpr = VT->getSizeExpr();
Ted Kremenekefd59942008-12-08 22:47:34 +0000463
Ted Kremenek159d2482008-12-09 00:44:16 +0000464 std::string buf;
465 llvm::raw_string_ostream os(buf);
Ted Kremenek5d88ff82009-04-02 02:40:26 +0000466 os << "The expression used to specify the number of elements in the "
467 "variable-length array (VLA) '"
Ted Kremenekcf118d42009-02-04 23:49:09 +0000468 << VD->getNameAsString() << "' evaluates to ";
Ted Kremenek159d2482008-12-09 00:44:16 +0000469
Ted Kremenekd49967f2009-04-29 21:58:13 +0000470 bool isUndefined = Eng.getStateManager().GetSVal(N->getState(),
471 SizeExpr).isUndef();
472
473 if (isUndefined)
Ted Kremenek159d2482008-12-09 00:44:16 +0000474 os << "an undefined or garbage value.";
Ted Kremenekcf118d42009-02-04 23:49:09 +0000475 else
476 os << "0. VLAs with no elements have undefined behavior.";
Ted Kremenekd49967f2009-04-29 21:58:13 +0000477
478 std::string shortBuf;
479 llvm::raw_string_ostream os_short(shortBuf);
480 os_short << "Variable-length array '" << VD->getNameAsString() << "' "
Ted Kremenekeaedfea2009-05-10 05:11:21 +0000481 << (isUndefined ? "garbage value for array size"
482 : "has zero elements (undefined behavior)");
Ted Kremenek159d2482008-12-09 00:44:16 +0000483
Ted Kremenekd49967f2009-04-29 21:58:13 +0000484 RangedBugReport *report = new RangedBugReport(*this,
485 os_short.str().c_str(),
486 os.str().c_str(), N);
Ted Kremenekcf118d42009-02-04 23:49:09 +0000487 report->addRange(SizeExpr->getSourceRange());
488 BR.EmitReport(report);
Ted Kremenekefd59942008-12-08 22:47:34 +0000489 }
490 }
491};
Zhongxing Xu1c0c2332008-11-23 05:52:28 +0000492
Ted Kremenek78d46242008-07-22 16:21:24 +0000493//===----------------------------------------------------------------------===//
494// __attribute__(nonnull) checking
495
496class VISIBILITY_HIDDEN CheckAttrNonNull : public GRSimpleAPICheck {
Ted Kremenekcf118d42009-02-04 23:49:09 +0000497 BugType *BT;
498 BugReporter &BR;
Ted Kremenek78d46242008-07-22 16:21:24 +0000499
500public:
Ted Kremenekcf118d42009-02-04 23:49:09 +0000501 CheckAttrNonNull(BugReporter &br) : BT(0), BR(br) {}
Ted Kremenek78d46242008-07-22 16:21:24 +0000502
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000503 virtual bool Audit(ExplodedNode<GRState>* N, GRStateManager& VMgr) {
Ted Kremenek78d46242008-07-22 16:21:24 +0000504 CallExpr* CE = cast<CallExpr>(cast<PostStmt>(N->getLocation()).getStmt());
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000505 const GRState* state = N->getState();
Ted Kremenek78d46242008-07-22 16:21:24 +0000506
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000507 SVal X = VMgr.GetSVal(state, CE->getCallee());
Zhongxing Xu369f4472009-04-20 05:24:46 +0000508
509 const FunctionDecl* FD = X.getAsFunctionDecl();
510 if (!FD)
Ted Kremenek78d46242008-07-22 16:21:24 +0000511 return false;
Zhongxing Xu369f4472009-04-20 05:24:46 +0000512
Ted Kremenek78d46242008-07-22 16:21:24 +0000513 const NonNullAttr* Att = FD->getAttr<NonNullAttr>();
514
515 if (!Att)
516 return false;
517
518 // Iterate through the arguments of CE and check them for null.
Ted Kremenek78d46242008-07-22 16:21:24 +0000519 unsigned idx = 0;
520 bool hasError = false;
521
522 for (CallExpr::arg_iterator I=CE->arg_begin(), E=CE->arg_end(); I!=E;
523 ++I, ++idx) {
524
525 if (!VMgr.isEqual(state, *I, 0) || !Att->isNonNull(idx))
526 continue;
Ted Kremenekcf118d42009-02-04 23:49:09 +0000527
528 // Lazily allocate the BugType object if it hasn't already been created.
529 // Ownership is transferred to the BugReporter object once the BugReport
530 // is passed to 'EmitWarning'.
Ted Kremenek5d88ff82009-04-02 02:40:26 +0000531 if (!BT) BT =
532 new BugType("Argument with 'nonnull' attribute passed null", "API");
Ted Kremenek78d46242008-07-22 16:21:24 +0000533
Ted Kremenekcf118d42009-02-04 23:49:09 +0000534 RangedBugReport *R = new RangedBugReport(*BT,
535 "Null pointer passed as an argument to a "
536 "'nonnull' parameter", N);
537
538 R->addRange((*I)->getSourceRange());
539 BR.EmitReport(R);
Ted Kremenek78d46242008-07-22 16:21:24 +0000540 hasError = true;
541 }
542
543 return hasError;
544 }
Ted Kremenek78d46242008-07-22 16:21:24 +0000545};
546} // end anonymous namespace
547
548//===----------------------------------------------------------------------===//
Ted Kremenekdd986cc2009-05-07 00:45:33 +0000549// Definitions for bug reporter visitors.
550//===----------------------------------------------------------------------===//
551
552namespace {
Ted Kremenek0c313172009-05-13 19:16:35 +0000553class VISIBILITY_HIDDEN FindLastStoreBRVisitor : public BugReporterVisitor {
Ted Kremenek7704a332009-05-07 21:49:45 +0000554 const MemRegion *R;
Ted Kremenek0c313172009-05-13 19:16:35 +0000555 SVal V;
556 bool satisfied;
557 const ExplodedNode<GRState> *StoreSite;
Ted Kremenek7704a332009-05-07 21:49:45 +0000558public:
Ted Kremenek0c313172009-05-13 19:16:35 +0000559 FindLastStoreBRVisitor(SVal v, const MemRegion *r)
560 : R(r), V(v), satisfied(false), StoreSite(0) {}
561
Ted Kremenek7704a332009-05-07 21:49:45 +0000562 PathDiagnosticPiece* VisitNode(const ExplodedNode<GRState> *N,
563 const ExplodedNode<GRState> *PrevN,
564 BugReporterContext& BRC) {
Ted Kremenek0c313172009-05-13 19:16:35 +0000565
566 if (satisfied)
Ted Kremenek7704a332009-05-07 21:49:45 +0000567 return NULL;
Ted Kremenek7704a332009-05-07 21:49:45 +0000568
Ted Kremenek0c313172009-05-13 19:16:35 +0000569 if (!StoreSite) {
570 GRStateManager &StateMgr = BRC.getStateManager();
571 const ExplodedNode<GRState> *Node = N, *Last = NULL;
572
573 for ( ; Node ; Last = Node, Node = Node->getFirstPred()) {
574
575 if (const VarRegion *VR = dyn_cast<VarRegion>(R)) {
576 if (const PostStmt *P = Node->getLocationAs<PostStmt>())
577 if (const DeclStmt *DS = P->getStmtAs<DeclStmt>())
578 if (DS->getSingleDecl() == VR->getDecl()) {
579 Last = Node;
580 break;
581 }
582 }
583
584 if (StateMgr.GetSVal(Node->getState(), R) != V)
585 break;
586 }
587
588 if (!Node || !Last) {
589 satisfied = true;
590 return NULL;
591 }
592
593 StoreSite = Last;
594 }
Ted Kremenek7704a332009-05-07 21:49:45 +0000595
Ted Kremenek0c313172009-05-13 19:16:35 +0000596 if (StoreSite != N)
597 return NULL;
598
599 satisfied = true;
600 std::string sbuf;
601 llvm::raw_string_ostream os(sbuf);
Ted Kremenek7704a332009-05-07 21:49:45 +0000602
Ted Kremenek0c313172009-05-13 19:16:35 +0000603 if (const PostStmt *PS = N->getLocationAs<PostStmt>()) {
604 if (const DeclStmt *DS = PS->getStmtAs<DeclStmt>()) {
605
606 if (const VarRegion *VR = dyn_cast<VarRegion>(R)) {
607 os << "Variable '" << VR->getDecl()->getNameAsString() << "' ";
608 }
609 else
610 return NULL;
611
612 if (isa<loc::ConcreteInt>(V)) {
613 bool b = false;
614 ASTContext &C = BRC.getASTContext();
615 if (R->isBoundable(C)) {
616 if (const TypedRegion *TR = dyn_cast<TypedRegion>(R)) {
617 if (C.isObjCObjectPointerType(TR->getValueType(C))) {
618 os << "initialized to nil";
619 b = true;
620 }
621 }
622 }
623
624 if (!b)
625 os << "initialized to a null pointer value";
626 }
627 else if (V.isUndef()) {
628 if (isa<VarRegion>(R)) {
629 const VarDecl *VD = cast<VarDecl>(DS->getSingleDecl());
630 if (VD->getInit())
631 os << "initialized to a garbage value";
632 else
633 os << "declared without an initial value";
634 }
635 }
636 }
637 }
638
639 if (os.str().empty()) {
640 if (isa<loc::ConcreteInt>(V)) {
641 bool b = false;
642 ASTContext &C = BRC.getASTContext();
643 if (R->isBoundable(C)) {
644 if (const TypedRegion *TR = dyn_cast<TypedRegion>(R)) {
645 if (C.isObjCObjectPointerType(TR->getValueType(C))) {
646 os << "nil object reference stored to ";
647 b = true;
648 }
649 }
650 }
651
652 if (!b)
653 os << "Null pointer value stored to ";
654 }
655 else if (V.isUndef()) {
656 os << "Uninitialized value stored to ";
657 }
658 else
659 return NULL;
Ted Kremenek7704a332009-05-07 21:49:45 +0000660
Ted Kremenek0c313172009-05-13 19:16:35 +0000661 if (const VarRegion *VR = dyn_cast<VarRegion>(R)) {
662 os << '\'' << VR->getDecl()->getNameAsString() << '\'';
663 }
664 else
665 return NULL;
666 }
667
668 // FIXME: Refactor this into BugReporterContext.
669 Stmt *S = 0;
670 ProgramPoint P = N->getLocation();
671
672 if (BlockEdge *BE = dyn_cast<BlockEdge>(&P)) {
673 CFGBlock *BSrc = BE->getSrc();
674 S = BSrc->getTerminatorCondition();
675 }
676 else if (PostStmt *PS = dyn_cast<PostStmt>(&P)) {
677 S = PS->getStmt();
678 }
679
680 if (!S)
681 return NULL;
682
683 // Construct a new PathDiagnosticPiece.
684 PathDiagnosticLocation L(S, BRC.getSourceManager());
685 return new PathDiagnosticEventPiece(L, os.str());
686 }
Ted Kremenek7704a332009-05-07 21:49:45 +0000687};
Ted Kremenek7704a332009-05-07 21:49:45 +0000688
Ted Kremenek0c313172009-05-13 19:16:35 +0000689
690static void registerFindLastStore(BugReporterContext& BRC, const MemRegion *R,
691 SVal V) {
692 BRC.addVisitor(new FindLastStoreBRVisitor(V, R));
693}
694
Ted Kremenekdd986cc2009-05-07 00:45:33 +0000695class VISIBILITY_HIDDEN TrackConstraintBRVisitor : public BugReporterVisitor {
696 SVal Constraint;
697 const bool Assumption;
698 bool isSatisfied;
699public:
700 TrackConstraintBRVisitor(SVal constraint, bool assumption)
701 : Constraint(constraint), Assumption(assumption), isSatisfied(false) {}
702
Ted Kremenek7704a332009-05-07 21:49:45 +0000703 PathDiagnosticPiece* VisitNode(const ExplodedNode<GRState> *N,
704 const ExplodedNode<GRState> *PrevN,
Ted Kremenekdd986cc2009-05-07 00:45:33 +0000705 BugReporterContext& BRC) {
706 if (isSatisfied)
707 return NULL;
708
709 // Check if in the previous state it was feasible for this constraint
710 // to *not* be true.
711
712 GRStateManager &StateMgr = BRC.getStateManager();
713 bool isFeasible = false;
714 if (StateMgr.Assume(PrevN->getState(), Constraint, !Assumption,
715 isFeasible)) {
716 assert(isFeasible); // Eventually we don't need 'isFeasible'.
717
718 isSatisfied = true;
719
720 // As a sanity check, make sure that the negation of the constraint
721 // was infeasible in the current state. If it is feasible, we somehow
722 // missed the transition point.
723 isFeasible = false;
724 if (StateMgr.Assume(N->getState(), Constraint, !Assumption,
725 isFeasible)) {
726 assert(isFeasible);
727 return NULL;
728 }
729
730 // We found the transition point for the constraint. We now need to
731 // pretty-print the constraint. (work-in-progress)
732 std::string sbuf;
733 llvm::raw_string_ostream os(sbuf);
734
735 if (isa<Loc>(Constraint)) {
736 os << "Assuming pointer value is ";
737 os << (Assumption ? "non-NULL" : "NULL");
738 }
739
740 if (os.str().empty())
741 return NULL;
742
743 // FIXME: Refactor this into BugReporterContext.
744 Stmt *S = 0;
745 ProgramPoint P = N->getLocation();
746
747 if (BlockEdge *BE = dyn_cast<BlockEdge>(&P)) {
748 CFGBlock *BSrc = BE->getSrc();
749 S = BSrc->getTerminatorCondition();
750 }
751 else if (PostStmt *PS = dyn_cast<PostStmt>(&P)) {
752 S = PS->getStmt();
753 }
754
755 if (!S)
756 return NULL;
757
758 // Construct a new PathDiagnosticPiece.
759 PathDiagnosticLocation L(S, BRC.getSourceManager());
760 return new PathDiagnosticEventPiece(L, os.str());
761 }
762
763 return NULL;
764 }
765};
766} // end anonymous namespace
767
768static void registerTrackConstraint(BugReporterContext& BRC, SVal Constraint,
769 bool Assumption) {
770 BRC.addVisitor(new TrackConstraintBRVisitor(Constraint, Assumption));
771}
772
Ted Kremenek0c313172009-05-13 19:16:35 +0000773static void registerTrackNullOrUndefValue(BugReporterContext& BRC,
Ted Kremenekdd986cc2009-05-07 00:45:33 +0000774 const ExplodedNode<GRState>* N) {
775
776 ProgramPoint P = N->getLocation();
777 PostStmt *PS = dyn_cast<PostStmt>(&P);
778
779 if (!PS)
780 return;
781
782 Stmt *S = PS->getStmt();
Ted Kremenek0c313172009-05-13 19:16:35 +0000783 GRStateManager &StateMgr = BRC.getStateManager();
784 const GRState *state = N->getState();
Ted Kremenekdd986cc2009-05-07 00:45:33 +0000785
Ted Kremenek0c313172009-05-13 19:16:35 +0000786 // Pattern match for a few useful cases (do something smarter later):
787 // a[0], p->f, *p
788 const DeclRefExpr *DR = 0;
789
790 if (const UnaryOperator *U = dyn_cast<UnaryOperator>(S)) {
791 if (U->getOpcode() == UnaryOperator::Deref)
792 DR = dyn_cast<DeclRefExpr>(U->getSubExpr()->IgnoreParenCasts());
793 }
794 else if (const MemberExpr *ME = dyn_cast<MemberExpr>(S)) {
795 DR = dyn_cast<DeclRefExpr>(ME->getBase()->IgnoreParenCasts());
796 }
797 else if (const ObjCMessageExpr *MSE = dyn_cast<ObjCMessageExpr>(S)) {
798 // FIXME: We should probably distinguish between cases where we had
799 // a nil receiver and null dereferences.
800 const Expr *Receiver = MSE->getReceiver();
801 if (Receiver) {
802 DR = dyn_cast<DeclRefExpr>(Receiver->IgnoreParenCasts());
803 }
804 }
805 else if (const ReturnStmt *RS = dyn_cast<ReturnStmt>(S)) {
806 if (const Expr *Ret = RS->getRetValue())
807 DR = dyn_cast<DeclRefExpr>(Ret->IgnoreParenCasts());
808 }
809 else if (const Expr *Ex = dyn_cast<Expr>(S)) {
810 // Keep this case last.
811 DR = dyn_cast<DeclRefExpr>(Ex->IgnoreParenCasts());
812 }
813
814 if (DR) {
815 if (const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl())) {
816 const VarRegion *R =
817 StateMgr.getRegionManager().getVarRegion(VD);
818
819 // What did we load?
820 SVal V = StateMgr.GetSVal(state, R);
821
822 if (isa<loc::ConcreteInt>(V) || V.isUndef()) {
823 registerFindLastStore(BRC, R, V);
824 }
825 }
826 }
827
828 // Retrieve the base for arrays since BasicStoreManager doesn't know how
829 // to reason about them.
Ted Kremenekdd986cc2009-05-07 00:45:33 +0000830 if (ArraySubscriptExpr *AE = dyn_cast<ArraySubscriptExpr>(S)) {
831 S = AE->getBase();
832 }
Ted Kremenek0c313172009-05-13 19:16:35 +0000833
834 SVal V = StateMgr.GetSValAsScalarOrLoc(state, S);
Ted Kremenekdd986cc2009-05-07 00:45:33 +0000835
836 // Uncomment this to find cases where we aren't properly getting the
837 // base value that was dereferenced.
838 // assert(!V.isUnknownOrUndef());
839
Ted Kremenek7704a332009-05-07 21:49:45 +0000840 // Is it a symbolic value?
Ted Kremenekdd986cc2009-05-07 00:45:33 +0000841 if (loc::MemRegionVal *L = dyn_cast<loc::MemRegionVal>(&V)) {
842 const SubRegion *R = cast<SubRegion>(L->getRegion());
843 while (R && !isa<SymbolicRegion>(R)) {
844 R = dyn_cast<SubRegion>(R->getSuperRegion());
845 }
846
847 if (R) {
848 assert(isa<SymbolicRegion>(R));
849 registerTrackConstraint(BRC, loc::MemRegionVal(R), false);
850 }
851 }
Ted Kremenek7704a332009-05-07 21:49:45 +0000852
853 // Was it a hard integer?
854// if (isa<nonloc::ConcreteInt>(V))
855// registerTrackValue(BRC, S, V, N);
Ted Kremenekdd986cc2009-05-07 00:45:33 +0000856}
857
858//===----------------------------------------------------------------------===//
Ted Kremenek78d46242008-07-22 16:21:24 +0000859// Check registration.
Ted Kremenekcf118d42009-02-04 23:49:09 +0000860//===----------------------------------------------------------------------===//
Ted Kremenek78d46242008-07-22 16:21:24 +0000861
862void GRExprEngine::RegisterInternalChecks() {
Ted Kremenekcf118d42009-02-04 23:49:09 +0000863 // Register internal "built-in" BugTypes with the BugReporter. These BugTypes
864 // are different than what probably many checks will do since they don't
865 // create BugReports on-the-fly but instead wait until GRExprEngine finishes
866 // analyzing a function. Generation of BugReport objects is done via a call
867 // to 'FlushReports' from BugReporter.
868 BR.Register(new NullDeref(this));
869 BR.Register(new UndefinedDeref(this));
870 BR.Register(new UndefBranch(this));
871 BR.Register(new DivZero(this));
872 BR.Register(new UndefResult(this));
873 BR.Register(new BadCall(this));
874 BR.Register(new RetStack(this));
875 BR.Register(new RetUndef(this));
876 BR.Register(new BadArg(this));
877 BR.Register(new BadMsgExprArg(this));
878 BR.Register(new BadReceiver(this));
879 BR.Register(new OutOfBoundMemoryAccess(this));
880 BR.Register(new BadSizeVLA(this));
Ted Kremenek21fe8372009-02-19 04:06:22 +0000881 BR.Register(new NilReceiverStructRet(this));
Ted Kremenek899b3de2009-04-08 03:07:17 +0000882 BR.Register(new NilReceiverLargerThanVoidPtrRet(this));
Ted Kremenekcf118d42009-02-04 23:49:09 +0000883
884 // The following checks do not need to have their associated BugTypes
885 // explicitly registered with the BugReporter. If they issue any BugReports,
886 // their associated BugType will get registered with the BugReporter
887 // automatically. Note that the check itself is owned by the GRExprEngine
888 // object.
889 AddCheck(new CheckAttrNonNull(BR), Stmt::CallExprClass);
Ted Kremenek78d46242008-07-22 16:21:24 +0000890}