blob: f17d783d16a8c2039590d2bfc0e5ae6816bc2173 [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 Kremenek8aed8062008-10-31 00:13:20 +000017#include "clang/Basic/SourceManager.h"
Ted Kremenek78d46242008-07-22 16:21:24 +000018#include "llvm/Support/Compiler.h"
Ted Kremenekad51a602008-10-31 00:18:30 +000019#include "llvm/Support/raw_ostream.h"
Ted Kremenek78d46242008-07-22 16:21:24 +000020
21using namespace clang;
22
23//===----------------------------------------------------------------------===//
24// Utility functions.
25//===----------------------------------------------------------------------===//
26
27template <typename ITERATOR> inline
Ted Kremenek4adc81e2008-08-13 04:27:00 +000028ExplodedNode<GRState>* GetNode(ITERATOR I) {
Ted Kremenek78d46242008-07-22 16:21:24 +000029 return *I;
30}
31
32template <> inline
Ted Kremenek4adc81e2008-08-13 04:27:00 +000033ExplodedNode<GRState>* GetNode(GRExprEngine::undef_arg_iterator I) {
Ted Kremenek78d46242008-07-22 16:21:24 +000034 return I->first;
35}
36
37//===----------------------------------------------------------------------===//
38// Bug Descriptions.
39//===----------------------------------------------------------------------===//
40
41namespace {
Ted Kremenekcf118d42009-02-04 23:49:09 +000042class VISIBILITY_HIDDEN BuiltinBug : public BugType {
43 GRExprEngine &Eng;
Ted Kremenek159d2482008-12-09 00:44:16 +000044protected:
Ted Kremenekcf118d42009-02-04 23:49:09 +000045 const std::string desc;
Ted Kremenek78d46242008-07-22 16:21:24 +000046public:
Ted Kremenekcf118d42009-02-04 23:49:09 +000047 BuiltinBug(GRExprEngine *eng, const char* n, const char* d)
48 : BugType(n, "Logic Errors"), Eng(*eng), desc(d) {}
49
50 BuiltinBug(GRExprEngine *eng, const char* n)
51 : BugType(n, "Logic Errors"), Eng(*eng), desc(n) {}
Ted Kremenek22bda882008-07-31 20:31:27 +000052
Ted Kremenekcf118d42009-02-04 23:49:09 +000053 virtual void FlushReportsImpl(BugReporter& BR, GRExprEngine& Eng) = 0;
54
55 void FlushReports(BugReporter& BR) { FlushReportsImpl(BR, Eng); }
Ted Kremenek78d46242008-07-22 16:21:24 +000056
57 template <typename ITER>
58 void Emit(BugReporter& BR, ITER I, ITER E) {
Ted Kremenekcf118d42009-02-04 23:49:09 +000059 for (; I != E; ++I) BR.EmitReport(new BugReport(*this, desc.c_str(),
60 GetNode(I)));
61 }
Ted Kremenek78d46242008-07-22 16:21:24 +000062};
63
64class VISIBILITY_HIDDEN NullDeref : public BuiltinBug {
65public:
Ted Kremenekcf118d42009-02-04 23:49:09 +000066 NullDeref(GRExprEngine* eng)
67 : BuiltinBug(eng,"null dereference", "Dereference of null pointer.") {}
Ted Kremenek78d46242008-07-22 16:21:24 +000068
Ted Kremenekcf118d42009-02-04 23:49:09 +000069 void FlushReportsImpl(BugReporter& BR, GRExprEngine& Eng) {
Ted Kremenek78d46242008-07-22 16:21:24 +000070 Emit(BR, Eng.null_derefs_begin(), Eng.null_derefs_end());
71 }
72};
73
Ted Kremenek21fe8372009-02-19 04:06:22 +000074class VISIBILITY_HIDDEN NilReceiverStructRet : public BugType {
75 GRExprEngine &Eng;
76public:
77 NilReceiverStructRet(GRExprEngine* eng) :
78 BugType("nil receiver with struct return type", "Logic Errors"),
79 Eng(*eng) {}
80
81 void FlushReports(BugReporter& BR) {
82 for (GRExprEngine::nil_receiver_struct_ret_iterator
83 I=Eng.nil_receiver_struct_ret_begin(),
84 E=Eng.nil_receiver_struct_ret_end(); I!=E; ++I) {
85
86 std::string sbuf;
87 llvm::raw_string_ostream os(sbuf);
88 PostStmt P = cast<PostStmt>((*I)->getLocation());
89 ObjCMessageExpr *ME = cast<ObjCMessageExpr>(P.getStmt());
90 os << "The receiver in the message expression is 'nil' and results in the"
91 " returned value (of type '"
92 << ME->getType().getAsString()
93 << "') to be garbage or otherwise undefined.";
94
95 RangedBugReport *R = new RangedBugReport(*this, os.str().c_str(), *I);
96 R->addRange(ME->getReceiver()->getSourceRange());
97 BR.EmitReport(R);
98 }
99 }
100};
101
Ted Kremenek78d46242008-07-22 16:21:24 +0000102class VISIBILITY_HIDDEN UndefinedDeref : public BuiltinBug {
103public:
Ted Kremenekcf118d42009-02-04 23:49:09 +0000104 UndefinedDeref(GRExprEngine* eng)
Ted Kremenek17a8e072009-03-01 05:43:22 +0000105 : BuiltinBug(eng,"Dereference of undefined pointer value") {}
Ted Kremenek78d46242008-07-22 16:21:24 +0000106
Ted Kremenekcf118d42009-02-04 23:49:09 +0000107 void FlushReportsImpl(BugReporter& BR, GRExprEngine& Eng) {
Ted Kremenek78d46242008-07-22 16:21:24 +0000108 Emit(BR, Eng.undef_derefs_begin(), Eng.undef_derefs_end());
109 }
110};
111
112class VISIBILITY_HIDDEN DivZero : public BuiltinBug {
113public:
Ted Kremenekcf118d42009-02-04 23:49:09 +0000114 DivZero(GRExprEngine* eng)
Ted Kremenek17a8e072009-03-01 05:43:22 +0000115 : BuiltinBug(eng,"divide-by-zero", "Division by zero or undefined value.") {}
Ted Kremenek78d46242008-07-22 16:21:24 +0000116
Ted Kremenekcf118d42009-02-04 23:49:09 +0000117 void FlushReportsImpl(BugReporter& BR, GRExprEngine& Eng) {
Ted Kremenek78d46242008-07-22 16:21:24 +0000118 Emit(BR, Eng.explicit_bad_divides_begin(), Eng.explicit_bad_divides_end());
119 }
120};
121
122class VISIBILITY_HIDDEN UndefResult : public BuiltinBug {
123public:
Ted Kremenekcf118d42009-02-04 23:49:09 +0000124 UndefResult(GRExprEngine* eng) : BuiltinBug(eng,"undefined result",
Ted Kremenek78d46242008-07-22 16:21:24 +0000125 "Result of operation is undefined.") {}
126
Ted Kremenekcf118d42009-02-04 23:49:09 +0000127 void FlushReportsImpl(BugReporter& BR, GRExprEngine& Eng) {
Ted Kremenek78d46242008-07-22 16:21:24 +0000128 Emit(BR, Eng.undef_results_begin(), Eng.undef_results_end());
129 }
130};
131
132class VISIBILITY_HIDDEN BadCall : public BuiltinBug {
133public:
Ted Kremenekcf118d42009-02-04 23:49:09 +0000134 BadCall(GRExprEngine *eng)
135 : BuiltinBug(eng,"invalid function call",
Ted Kremenek17a8e072009-03-01 05:43:22 +0000136 "Called function pointer is a null or undefined pointer value") {}
Ted Kremenek78d46242008-07-22 16:21:24 +0000137
Ted Kremenekcf118d42009-02-04 23:49:09 +0000138 void FlushReportsImpl(BugReporter& BR, GRExprEngine& Eng) {
Ted Kremenek78d46242008-07-22 16:21:24 +0000139 Emit(BR, Eng.bad_calls_begin(), Eng.bad_calls_end());
140 }
141};
142
143class VISIBILITY_HIDDEN BadArg : public BuiltinBug {
144public:
Ted Kremenekcf118d42009-02-04 23:49:09 +0000145 BadArg(GRExprEngine* eng) : BuiltinBug(eng,"uninitialized argument",
Ted Kremenek17a8e072009-03-01 05:43:22 +0000146 "Pass-by-value argument in function call is undefined.") {}
Ted Kremenek78d46242008-07-22 16:21:24 +0000147
Ted Kremenekcf118d42009-02-04 23:49:09 +0000148 BadArg(GRExprEngine* eng, const char* d)
149 : BuiltinBug(eng,"uninitialized argument", d) {}
Ted Kremenek78d46242008-07-22 16:21:24 +0000150
Ted Kremenekcf118d42009-02-04 23:49:09 +0000151 void FlushReportsImpl(BugReporter& BR, GRExprEngine& Eng) {
Ted Kremenek78d46242008-07-22 16:21:24 +0000152 for (GRExprEngine::UndefArgsTy::iterator I = Eng.undef_arg_begin(),
153 E = Eng.undef_arg_end(); I!=E; ++I) {
Ted Kremenek78d46242008-07-22 16:21:24 +0000154 // Generate a report for this bug.
Ted Kremenekcf118d42009-02-04 23:49:09 +0000155 RangedBugReport *report = new RangedBugReport(*this, desc.c_str(),
156 I->first);
157 report->addRange(I->second->getSourceRange());
158 BR.EmitReport(report);
Ted Kremenek78d46242008-07-22 16:21:24 +0000159 }
160 }
161};
162
163class VISIBILITY_HIDDEN BadMsgExprArg : public BadArg {
164public:
Ted Kremenekcf118d42009-02-04 23:49:09 +0000165 BadMsgExprArg(GRExprEngine* eng)
166 : BadArg(eng,"Pass-by-value argument in message expression is undefined."){}
Ted Kremenek78d46242008-07-22 16:21:24 +0000167
Ted Kremenekcf118d42009-02-04 23:49:09 +0000168 void FlushReportsImpl(BugReporter& BR, GRExprEngine& Eng) {
Ted Kremenek78d46242008-07-22 16:21:24 +0000169 for (GRExprEngine::UndefArgsTy::iterator I=Eng.msg_expr_undef_arg_begin(),
Ted Kremenekcf118d42009-02-04 23:49:09 +0000170 E = Eng.msg_expr_undef_arg_end(); I!=E; ++I) {
Ted Kremenek78d46242008-07-22 16:21:24 +0000171 // Generate a report for this bug.
Ted Kremenek21fe8372009-02-19 04:06:22 +0000172 RangedBugReport *report = new RangedBugReport(*this, desc.c_str(),
173 I->first);
Ted Kremenekcf118d42009-02-04 23:49:09 +0000174 report->addRange(I->second->getSourceRange());
175 BR.EmitReport(report);
Ted Kremenek78d46242008-07-22 16:21:24 +0000176 }
177 }
178};
179
180class VISIBILITY_HIDDEN BadReceiver : public BuiltinBug {
181public:
Ted Kremenekcf118d42009-02-04 23:49:09 +0000182 BadReceiver(GRExprEngine* eng)
183 : BuiltinBug(eng,"uninitialized receiver",
Ted Kremenek78d46242008-07-22 16:21:24 +0000184 "Receiver in message expression is an uninitialized value.") {}
185
Ted Kremenekcf118d42009-02-04 23:49:09 +0000186 void FlushReportsImpl(BugReporter& BR, GRExprEngine& Eng) {
Ted Kremenekefd59942008-12-08 22:47:34 +0000187 for (GRExprEngine::ErrorNodes::iterator I=Eng.undef_receivers_begin(),
Ted Kremenek78d46242008-07-22 16:21:24 +0000188 End = Eng.undef_receivers_end(); I!=End; ++I) {
189
190 // Generate a report for this bug.
Ted Kremenekcf118d42009-02-04 23:49:09 +0000191 RangedBugReport *report = new RangedBugReport(*this, desc.c_str(), *I);
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000192 ExplodedNode<GRState>* N = *I;
Ted Kremenek78d46242008-07-22 16:21:24 +0000193 Stmt *S = cast<PostStmt>(N->getLocation()).getStmt();
194 Expr* E = cast<ObjCMessageExpr>(S)->getReceiver();
195 assert (E && "Receiver cannot be NULL");
Ted Kremenekcf118d42009-02-04 23:49:09 +0000196 report->addRange(E->getSourceRange());
197 BR.EmitReport(report);
Ted Kremenek78d46242008-07-22 16:21:24 +0000198 }
199 }
200};
Ted Kremenek5917d782008-11-21 00:27:44 +0000201
Ted Kremenek78d46242008-07-22 16:21:24 +0000202class VISIBILITY_HIDDEN RetStack : public BuiltinBug {
203public:
Ted Kremenek17a8e072009-03-01 05:43:22 +0000204 RetStack(GRExprEngine* eng)
205 : BuiltinBug(eng, "return of address to stack-allocated memory") {}
Ted Kremenek78d46242008-07-22 16:21:24 +0000206
Ted Kremenekcf118d42009-02-04 23:49:09 +0000207 void FlushReportsImpl(BugReporter& BR, GRExprEngine& Eng) {
Ted Kremenekb7714b22008-07-30 17:49:12 +0000208 for (GRExprEngine::ret_stackaddr_iterator I=Eng.ret_stackaddr_begin(),
209 End = Eng.ret_stackaddr_end(); I!=End; ++I) {
Ted Kremenek22bda882008-07-31 20:31:27 +0000210
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000211 ExplodedNode<GRState>* N = *I;
Ted Kremenekb7714b22008-07-30 17:49:12 +0000212 Stmt *S = cast<PostStmt>(N->getLocation()).getStmt();
213 Expr* E = cast<ReturnStmt>(S)->getRetValue();
214 assert (E && "Return expression cannot be NULL");
Ted Kremenek22bda882008-07-31 20:31:27 +0000215
216 // Get the value associated with E.
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000217 loc::MemRegionVal V =
218 cast<loc::MemRegionVal>(Eng.getStateManager().GetSVal(N->getState(),
Ted Kremenek9e240492008-10-04 05:50:14 +0000219 E));
Ted Kremenek22bda882008-07-31 20:31:27 +0000220
221 // Generate a report for this bug.
Ted Kremenekad51a602008-10-31 00:18:30 +0000222 std::string buf;
223 llvm::raw_string_ostream os(buf);
Ted Kremenek8aed8062008-10-31 00:13:20 +0000224 SourceRange R;
Ted Kremenek22bda882008-07-31 20:31:27 +0000225
Ted Kremenek8aed8062008-10-31 00:13:20 +0000226 // Check if the region is a compound literal.
227 if (const CompoundLiteralRegion* CR =
228 dyn_cast<CompoundLiteralRegion>(V.getRegion())) {
229
230 const CompoundLiteralExpr* CL = CR->getLiteralExpr();
231 os << "Address of stack memory associated with a compound literal "
232 "declared on line "
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000233 << BR.getSourceManager()
234 .getInstantiationLineNumber(CL->getLocStart())
Ted Kremenek8aed8062008-10-31 00:13:20 +0000235 << " returned.";
236
237 R = CL->getSourceRange();
238 }
Ted Kremenekde8cd192008-11-02 00:35:25 +0000239 else if (const AllocaRegion* AR = dyn_cast<AllocaRegion>(V.getRegion())) {
240 const Expr* ARE = AR->getExpr();
241 SourceLocation L = ARE->getLocStart();
242 R = ARE->getSourceRange();
243
244 os << "Address of stack memory allocated by call to alloca() on line "
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000245 << BR.getSourceManager().getInstantiationLineNumber(L)
Ted Kremenekde8cd192008-11-02 00:35:25 +0000246 << " returned.";
247 }
Ted Kremenek8aed8062008-10-31 00:13:20 +0000248 else {
249 os << "Address of stack memory associated with local variable '"
250 << V.getRegion()->getString() << "' returned.";
251 }
Ted Kremenek22bda882008-07-31 20:31:27 +0000252
Ted Kremenekcf118d42009-02-04 23:49:09 +0000253 RangedBugReport *report = new RangedBugReport(*this, os.str().c_str(), N);
254 report->addRange(E->getSourceRange());
255 if (R.isValid()) report->addRange(R);
256 BR.EmitReport(report);
Ted Kremenekb7714b22008-07-30 17:49:12 +0000257 }
Ted Kremenek78d46242008-07-22 16:21:24 +0000258 }
259};
Ted Kremenek5917d782008-11-21 00:27:44 +0000260
261class VISIBILITY_HIDDEN RetUndef : public BuiltinBug {
262public:
Ted Kremenekcf118d42009-02-04 23:49:09 +0000263 RetUndef(GRExprEngine* eng) : BuiltinBug(eng,"uninitialized return value",
Ted Kremenek5917d782008-11-21 00:27:44 +0000264 "Uninitialized or undefined return value returned to caller.") {}
265
Ted Kremenekcf118d42009-02-04 23:49:09 +0000266 void FlushReportsImpl(BugReporter& BR, GRExprEngine& Eng) {
Ted Kremenek5917d782008-11-21 00:27:44 +0000267 Emit(BR, Eng.ret_undef_begin(), Eng.ret_undef_end());
268 }
269};
Ted Kremenek78d46242008-07-22 16:21:24 +0000270
271class VISIBILITY_HIDDEN UndefBranch : public BuiltinBug {
272 struct VISIBILITY_HIDDEN FindUndefExpr {
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000273 GRStateManager& VM;
274 const GRState* St;
Ted Kremenek78d46242008-07-22 16:21:24 +0000275
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000276 FindUndefExpr(GRStateManager& V, const GRState* S) : VM(V), St(S) {}
Ted Kremenek78d46242008-07-22 16:21:24 +0000277
Ted Kremenekb7714b22008-07-30 17:49:12 +0000278 Expr* FindExpr(Expr* Ex) {
Ted Kremenek78d46242008-07-22 16:21:24 +0000279 if (!MatchesCriteria(Ex))
Ted Kremenekb7714b22008-07-30 17:49:12 +0000280 return 0;
Ted Kremenek78d46242008-07-22 16:21:24 +0000281
Ted Kremenekb7714b22008-07-30 17:49:12 +0000282 for (Stmt::child_iterator I=Ex->child_begin(), E=Ex->child_end();I!=E;++I)
Ted Kremenek78d46242008-07-22 16:21:24 +0000283 if (Expr* ExI = dyn_cast_or_null<Expr>(*I)) {
284 Expr* E2 = FindExpr(ExI);
285 if (E2) return E2;
286 }
287
288 return Ex;
289 }
290
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000291 bool MatchesCriteria(Expr* Ex) { return VM.GetSVal(St, Ex).isUndef(); }
Ted Kremenek78d46242008-07-22 16:21:24 +0000292 };
293
294public:
Ted Kremenekcf118d42009-02-04 23:49:09 +0000295 UndefBranch(GRExprEngine *eng)
296 : BuiltinBug(eng,"uninitialized value",
Ted Kremenek78d46242008-07-22 16:21:24 +0000297 "Branch condition evaluates to an uninitialized value.") {}
298
Ted Kremenekcf118d42009-02-04 23:49:09 +0000299 void FlushReportsImpl(BugReporter& BR, GRExprEngine& Eng) {
Ted Kremenek78d46242008-07-22 16:21:24 +0000300 for (GRExprEngine::undef_branch_iterator I=Eng.undef_branches_begin(),
301 E=Eng.undef_branches_end(); I!=E; ++I) {
302
303 // What's going on here: we want to highlight the subexpression of the
304 // condition that is the most likely source of the "uninitialized
305 // branch condition." We do a recursive walk of the condition's
306 // subexpressions and roughly look for the most nested subexpression
307 // that binds to Undefined. We then highlight that expression's range.
Ted Kremenek78d46242008-07-22 16:21:24 +0000308 BlockEdge B = cast<BlockEdge>((*I)->getLocation());
309 Expr* Ex = cast<Expr>(B.getSrc()->getTerminatorCondition());
310 assert (Ex && "Block must have a terminator.");
311
312 // Get the predecessor node and check if is a PostStmt with the Stmt
313 // being the terminator condition. We want to inspect the state
314 // of that node instead because it will contain main information about
315 // the subexpressions.
Ted Kremenek78d46242008-07-22 16:21:24 +0000316 assert (!(*I)->pred_empty());
317
318 // Note: any predecessor will do. They should have identical state,
319 // since all the BlockEdge did was act as an error sink since the value
320 // had to already be undefined.
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000321 ExplodedNode<GRState> *N = *(*I)->pred_begin();
Ted Kremenek78d46242008-07-22 16:21:24 +0000322 ProgramPoint P = N->getLocation();
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000323 const GRState* St = (*I)->getState();
Ted Kremenek78d46242008-07-22 16:21:24 +0000324
325 if (PostStmt* PS = dyn_cast<PostStmt>(&P))
326 if (PS->getStmt() == Ex)
327 St = N->getState();
328
329 FindUndefExpr FindIt(Eng.getStateManager(), St);
330 Ex = FindIt.FindExpr(Ex);
331
Ted Kremenekcf118d42009-02-04 23:49:09 +0000332 RangedBugReport *R = new RangedBugReport(*this, desc.c_str(), *I);
333 R->addRange(Ex->getSourceRange());
334 BR.EmitReport(R);
Ted Kremenek78d46242008-07-22 16:21:24 +0000335 }
336 }
337};
338
Zhongxing Xu1c0c2332008-11-23 05:52:28 +0000339class VISIBILITY_HIDDEN OutOfBoundMemoryAccess : public BuiltinBug {
340public:
Ted Kremenekcf118d42009-02-04 23:49:09 +0000341 OutOfBoundMemoryAccess(GRExprEngine* eng)
Ted Kremenek17a8e072009-03-01 05:43:22 +0000342 : BuiltinBug(eng,"out-of-bounds memory access",
Ted Kremenekcf118d42009-02-04 23:49:09 +0000343 "Load or store into an out-of-bound memory position.") {}
Zhongxing Xu1c0c2332008-11-23 05:52:28 +0000344
Ted Kremenekcf118d42009-02-04 23:49:09 +0000345 void FlushReportsImpl(BugReporter& BR, GRExprEngine& Eng) {
Zhongxing Xu1c0c2332008-11-23 05:52:28 +0000346 Emit(BR, Eng.explicit_oob_memacc_begin(), Eng.explicit_oob_memacc_end());
347 }
348};
Ted Kremenekefd59942008-12-08 22:47:34 +0000349
Ted Kremenek159d2482008-12-09 00:44:16 +0000350class VISIBILITY_HIDDEN BadSizeVLA : public BuiltinBug {
Ted Kremenekefd59942008-12-08 22:47:34 +0000351public:
Ted Kremenekcf118d42009-02-04 23:49:09 +0000352 BadSizeVLA(GRExprEngine* eng) : BuiltinBug(eng, "bad VLA size") {}
Ted Kremenekefd59942008-12-08 22:47:34 +0000353
Ted Kremenekcf118d42009-02-04 23:49:09 +0000354 void FlushReportsImpl(BugReporter& BR, GRExprEngine& Eng) {
Ted Kremenekefd59942008-12-08 22:47:34 +0000355 for (GRExprEngine::ErrorNodes::iterator
Ted Kremenek159d2482008-12-09 00:44:16 +0000356 I = Eng.ExplicitBadSizedVLA.begin(),
357 E = Eng.ExplicitBadSizedVLA.end(); I!=E; ++I) {
358
359 // Determine whether this was a 'zero-sized' VLA or a VLA with an
360 // undefined size.
361 GRExprEngine::NodeTy* N = *I;
362 PostStmt PS = cast<PostStmt>(N->getLocation());
Ted Kremenekefd59942008-12-08 22:47:34 +0000363 DeclStmt *DS = cast<DeclStmt>(PS.getStmt());
364 VarDecl* VD = cast<VarDecl>(*DS->decl_begin());
365 QualType T = Eng.getContext().getCanonicalType(VD->getType());
366 VariableArrayType* VT = cast<VariableArrayType>(T);
Ted Kremenek159d2482008-12-09 00:44:16 +0000367 Expr* SizeExpr = VT->getSizeExpr();
Ted Kremenekefd59942008-12-08 22:47:34 +0000368
Ted Kremenek159d2482008-12-09 00:44:16 +0000369 std::string buf;
370 llvm::raw_string_ostream os(buf);
371 os << "The expression used to specify the number of elements in the VLA '"
Ted Kremenekcf118d42009-02-04 23:49:09 +0000372 << VD->getNameAsString() << "' evaluates to ";
Ted Kremenek159d2482008-12-09 00:44:16 +0000373
Ted Kremenekcf118d42009-02-04 23:49:09 +0000374 if (Eng.getStateManager().GetSVal(N->getState(), SizeExpr).isUndef())
Ted Kremenek159d2482008-12-09 00:44:16 +0000375 os << "an undefined or garbage value.";
Ted Kremenekcf118d42009-02-04 23:49:09 +0000376 else
377 os << "0. VLAs with no elements have undefined behavior.";
Ted Kremenek159d2482008-12-09 00:44:16 +0000378
Ted Kremenekcf118d42009-02-04 23:49:09 +0000379 RangedBugReport *report = new RangedBugReport(*this, os.str().c_str(), N);
380 report->addRange(SizeExpr->getSourceRange());
381 BR.EmitReport(report);
Ted Kremenekefd59942008-12-08 22:47:34 +0000382 }
383 }
384};
Zhongxing Xu1c0c2332008-11-23 05:52:28 +0000385
Ted Kremenek78d46242008-07-22 16:21:24 +0000386//===----------------------------------------------------------------------===//
387// __attribute__(nonnull) checking
388
389class VISIBILITY_HIDDEN CheckAttrNonNull : public GRSimpleAPICheck {
Ted Kremenekcf118d42009-02-04 23:49:09 +0000390 BugType *BT;
391 BugReporter &BR;
Ted Kremenek78d46242008-07-22 16:21:24 +0000392
393public:
Ted Kremenekcf118d42009-02-04 23:49:09 +0000394 CheckAttrNonNull(BugReporter &br) : BT(0), BR(br) {}
Ted Kremenek78d46242008-07-22 16:21:24 +0000395
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000396 virtual bool Audit(ExplodedNode<GRState>* N, GRStateManager& VMgr) {
Ted Kremenek78d46242008-07-22 16:21:24 +0000397 CallExpr* CE = cast<CallExpr>(cast<PostStmt>(N->getLocation()).getStmt());
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000398 const GRState* state = N->getState();
Ted Kremenek78d46242008-07-22 16:21:24 +0000399
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000400 SVal X = VMgr.GetSVal(state, CE->getCallee());
Ted Kremenek78d46242008-07-22 16:21:24 +0000401
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000402 if (!isa<loc::FuncVal>(X))
Ted Kremenek78d46242008-07-22 16:21:24 +0000403 return false;
404
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000405 FunctionDecl* FD = dyn_cast<FunctionDecl>(cast<loc::FuncVal>(X).getDecl());
Ted Kremenek78d46242008-07-22 16:21:24 +0000406 const NonNullAttr* Att = FD->getAttr<NonNullAttr>();
407
408 if (!Att)
409 return false;
410
411 // Iterate through the arguments of CE and check them for null.
Ted Kremenek78d46242008-07-22 16:21:24 +0000412 unsigned idx = 0;
413 bool hasError = false;
414
415 for (CallExpr::arg_iterator I=CE->arg_begin(), E=CE->arg_end(); I!=E;
416 ++I, ++idx) {
417
418 if (!VMgr.isEqual(state, *I, 0) || !Att->isNonNull(idx))
419 continue;
Ted Kremenekcf118d42009-02-04 23:49:09 +0000420
421 // Lazily allocate the BugType object if it hasn't already been created.
422 // Ownership is transferred to the BugReporter object once the BugReport
423 // is passed to 'EmitWarning'.
Ted Kremenek17a8e072009-03-01 05:43:22 +0000424 if (!BT) BT = new BugType("argument with 'nonnull' attribute passed null", "API");
Ted Kremenek78d46242008-07-22 16:21:24 +0000425
Ted Kremenekcf118d42009-02-04 23:49:09 +0000426 RangedBugReport *R = new RangedBugReport(*BT,
427 "Null pointer passed as an argument to a "
428 "'nonnull' parameter", N);
429
430 R->addRange((*I)->getSourceRange());
431 BR.EmitReport(R);
Ted Kremenek78d46242008-07-22 16:21:24 +0000432 hasError = true;
433 }
434
435 return hasError;
436 }
Ted Kremenek78d46242008-07-22 16:21:24 +0000437};
438} // end anonymous namespace
439
440//===----------------------------------------------------------------------===//
441// Check registration.
Ted Kremenekcf118d42009-02-04 23:49:09 +0000442//===----------------------------------------------------------------------===//
Ted Kremenek78d46242008-07-22 16:21:24 +0000443
444void GRExprEngine::RegisterInternalChecks() {
Ted Kremenekcf118d42009-02-04 23:49:09 +0000445 // Register internal "built-in" BugTypes with the BugReporter. These BugTypes
446 // are different than what probably many checks will do since they don't
447 // create BugReports on-the-fly but instead wait until GRExprEngine finishes
448 // analyzing a function. Generation of BugReport objects is done via a call
449 // to 'FlushReports' from BugReporter.
450 BR.Register(new NullDeref(this));
451 BR.Register(new UndefinedDeref(this));
452 BR.Register(new UndefBranch(this));
453 BR.Register(new DivZero(this));
454 BR.Register(new UndefResult(this));
455 BR.Register(new BadCall(this));
456 BR.Register(new RetStack(this));
457 BR.Register(new RetUndef(this));
458 BR.Register(new BadArg(this));
459 BR.Register(new BadMsgExprArg(this));
460 BR.Register(new BadReceiver(this));
461 BR.Register(new OutOfBoundMemoryAccess(this));
462 BR.Register(new BadSizeVLA(this));
Ted Kremenek21fe8372009-02-19 04:06:22 +0000463 BR.Register(new NilReceiverStructRet(this));
Ted Kremenekcf118d42009-02-04 23:49:09 +0000464
465 // The following checks do not need to have their associated BugTypes
466 // explicitly registered with the BugReporter. If they issue any BugReports,
467 // their associated BugType will get registered with the BugReporter
468 // automatically. Note that the check itself is owned by the GRExprEngine
469 // object.
470 AddCheck(new CheckAttrNonNull(BR), Stmt::CallExprClass);
Ted Kremenek78d46242008-07-22 16:21:24 +0000471}