blob: d0a8591f3abf9bae7ae136c31bf99f4351d73c8e [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)
Ted Kremenek5d88ff82009-04-02 02:40:26 +000067 : 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) :
Ted Kremenek5d88ff82009-04-02 02:40:26 +000078 BugType("'nil' receiver with struct return type", "Logic Errors"),
Ted Kremenek21fe8372009-02-19 04:06:22 +000079 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 Kremenek5d88ff82009-04-02 02:40:26 +0000115 : BuiltinBug(eng,"Division-by-zero",
116 "Division by zero or undefined value.") {}
Ted Kremenek78d46242008-07-22 16:21:24 +0000117
Ted Kremenekcf118d42009-02-04 23:49:09 +0000118 void FlushReportsImpl(BugReporter& BR, GRExprEngine& Eng) {
Ted Kremenek78d46242008-07-22 16:21:24 +0000119 Emit(BR, Eng.explicit_bad_divides_begin(), Eng.explicit_bad_divides_end());
120 }
121};
122
123class VISIBILITY_HIDDEN UndefResult : public BuiltinBug {
124public:
Ted Kremenek5d88ff82009-04-02 02:40:26 +0000125 UndefResult(GRExprEngine* eng) : BuiltinBug(eng,"Undefined result",
Ted Kremenek78d46242008-07-22 16:21:24 +0000126 "Result of operation is undefined.") {}
127
Ted Kremenekcf118d42009-02-04 23:49:09 +0000128 void FlushReportsImpl(BugReporter& BR, GRExprEngine& Eng) {
Ted Kremenek78d46242008-07-22 16:21:24 +0000129 Emit(BR, Eng.undef_results_begin(), Eng.undef_results_end());
130 }
131};
132
133class VISIBILITY_HIDDEN BadCall : public BuiltinBug {
134public:
Ted Kremenekcf118d42009-02-04 23:49:09 +0000135 BadCall(GRExprEngine *eng)
Ted Kremenek5d88ff82009-04-02 02:40:26 +0000136 : BuiltinBug(eng, "Invalid function call",
Ted Kremenek17a8e072009-03-01 05:43:22 +0000137 "Called function pointer is a null or undefined pointer value") {}
Ted Kremenek78d46242008-07-22 16:21:24 +0000138
Ted Kremenekcf118d42009-02-04 23:49:09 +0000139 void FlushReportsImpl(BugReporter& BR, GRExprEngine& Eng) {
Ted Kremenek78d46242008-07-22 16:21:24 +0000140 Emit(BR, Eng.bad_calls_begin(), Eng.bad_calls_end());
141 }
142};
143
144class VISIBILITY_HIDDEN BadArg : public BuiltinBug {
145public:
Ted Kremenek5d88ff82009-04-02 02:40:26 +0000146 BadArg(GRExprEngine* eng) : BuiltinBug(eng,"Uninitialized argument",
Ted Kremenek17a8e072009-03-01 05:43:22 +0000147 "Pass-by-value argument in function call is undefined.") {}
Ted Kremenek78d46242008-07-22 16:21:24 +0000148
Ted Kremenekcf118d42009-02-04 23:49:09 +0000149 BadArg(GRExprEngine* eng, const char* d)
Ted Kremenek5d88ff82009-04-02 02:40:26 +0000150 : BuiltinBug(eng,"Uninitialized argument", d) {}
Ted Kremenek78d46242008-07-22 16:21:24 +0000151
Ted Kremenekcf118d42009-02-04 23:49:09 +0000152 void FlushReportsImpl(BugReporter& BR, GRExprEngine& Eng) {
Ted Kremenek78d46242008-07-22 16:21:24 +0000153 for (GRExprEngine::UndefArgsTy::iterator I = Eng.undef_arg_begin(),
154 E = Eng.undef_arg_end(); I!=E; ++I) {
Ted Kremenek78d46242008-07-22 16:21:24 +0000155 // Generate a report for this bug.
Ted Kremenekcf118d42009-02-04 23:49:09 +0000156 RangedBugReport *report = new RangedBugReport(*this, desc.c_str(),
157 I->first);
158 report->addRange(I->second->getSourceRange());
159 BR.EmitReport(report);
Ted Kremenek78d46242008-07-22 16:21:24 +0000160 }
161 }
162};
163
164class VISIBILITY_HIDDEN BadMsgExprArg : public BadArg {
165public:
Ted Kremenekcf118d42009-02-04 23:49:09 +0000166 BadMsgExprArg(GRExprEngine* eng)
Ted Kremenek5d88ff82009-04-02 02:40:26 +0000167 : BadArg(eng,"Pass-by-value argument in message expression is undefined"){}
Ted Kremenek78d46242008-07-22 16:21:24 +0000168
Ted Kremenekcf118d42009-02-04 23:49:09 +0000169 void FlushReportsImpl(BugReporter& BR, GRExprEngine& Eng) {
Ted Kremenek78d46242008-07-22 16:21:24 +0000170 for (GRExprEngine::UndefArgsTy::iterator I=Eng.msg_expr_undef_arg_begin(),
Ted Kremenekcf118d42009-02-04 23:49:09 +0000171 E = Eng.msg_expr_undef_arg_end(); I!=E; ++I) {
Ted Kremenek78d46242008-07-22 16:21:24 +0000172 // Generate a report for this bug.
Ted Kremenek21fe8372009-02-19 04:06:22 +0000173 RangedBugReport *report = new RangedBugReport(*this, desc.c_str(),
174 I->first);
Ted Kremenekcf118d42009-02-04 23:49:09 +0000175 report->addRange(I->second->getSourceRange());
176 BR.EmitReport(report);
Ted Kremenek78d46242008-07-22 16:21:24 +0000177 }
178 }
179};
180
181class VISIBILITY_HIDDEN BadReceiver : public BuiltinBug {
182public:
Ted Kremenekcf118d42009-02-04 23:49:09 +0000183 BadReceiver(GRExprEngine* eng)
Ted Kremenek5d88ff82009-04-02 02:40:26 +0000184 : BuiltinBug(eng,"Uninitialized receiver",
185 "Receiver in message expression is an uninitialized value") {}
Ted Kremenek78d46242008-07-22 16:21:24 +0000186
Ted Kremenekcf118d42009-02-04 23:49:09 +0000187 void FlushReportsImpl(BugReporter& BR, GRExprEngine& Eng) {
Ted Kremenekefd59942008-12-08 22:47:34 +0000188 for (GRExprEngine::ErrorNodes::iterator I=Eng.undef_receivers_begin(),
Ted Kremenek78d46242008-07-22 16:21:24 +0000189 End = Eng.undef_receivers_end(); I!=End; ++I) {
190
191 // Generate a report for this bug.
Ted Kremenek5d88ff82009-04-02 02:40:26 +0000192 RangedBugReport *report = new RangedBugReport(*this, desc.c_str(), *I);
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000193 ExplodedNode<GRState>* N = *I;
Ted Kremenek78d46242008-07-22 16:21:24 +0000194 Stmt *S = cast<PostStmt>(N->getLocation()).getStmt();
195 Expr* E = cast<ObjCMessageExpr>(S)->getReceiver();
196 assert (E && "Receiver cannot be NULL");
Ted Kremenekcf118d42009-02-04 23:49:09 +0000197 report->addRange(E->getSourceRange());
198 BR.EmitReport(report);
Ted Kremenek78d46242008-07-22 16:21:24 +0000199 }
200 }
201};
Ted Kremenek5917d782008-11-21 00:27:44 +0000202
Ted Kremenek78d46242008-07-22 16:21:24 +0000203class VISIBILITY_HIDDEN RetStack : public BuiltinBug {
204public:
Ted Kremenek17a8e072009-03-01 05:43:22 +0000205 RetStack(GRExprEngine* eng)
Ted Kremenek5d88ff82009-04-02 02:40:26 +0000206 : BuiltinBug(eng, "Return of address to stack-allocated memory") {}
Ted Kremenek78d46242008-07-22 16:21:24 +0000207
Ted Kremenekcf118d42009-02-04 23:49:09 +0000208 void FlushReportsImpl(BugReporter& BR, GRExprEngine& Eng) {
Ted Kremenekb7714b22008-07-30 17:49:12 +0000209 for (GRExprEngine::ret_stackaddr_iterator I=Eng.ret_stackaddr_begin(),
210 End = Eng.ret_stackaddr_end(); I!=End; ++I) {
Ted Kremenek22bda882008-07-31 20:31:27 +0000211
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000212 ExplodedNode<GRState>* N = *I;
Ted Kremenekb7714b22008-07-30 17:49:12 +0000213 Stmt *S = cast<PostStmt>(N->getLocation()).getStmt();
214 Expr* E = cast<ReturnStmt>(S)->getRetValue();
215 assert (E && "Return expression cannot be NULL");
Ted Kremenek22bda882008-07-31 20:31:27 +0000216
217 // Get the value associated with E.
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000218 loc::MemRegionVal V =
219 cast<loc::MemRegionVal>(Eng.getStateManager().GetSVal(N->getState(),
Ted Kremenek9e240492008-10-04 05:50:14 +0000220 E));
Ted Kremenek22bda882008-07-31 20:31:27 +0000221
222 // Generate a report for this bug.
Ted Kremenekad51a602008-10-31 00:18:30 +0000223 std::string buf;
224 llvm::raw_string_ostream os(buf);
Ted Kremenek8aed8062008-10-31 00:13:20 +0000225 SourceRange R;
Ted Kremenek22bda882008-07-31 20:31:27 +0000226
Ted Kremenek8aed8062008-10-31 00:13:20 +0000227 // Check if the region is a compound literal.
228 if (const CompoundLiteralRegion* CR =
229 dyn_cast<CompoundLiteralRegion>(V.getRegion())) {
230
231 const CompoundLiteralExpr* CL = CR->getLiteralExpr();
232 os << "Address of stack memory associated with a compound literal "
233 "declared on line "
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000234 << BR.getSourceManager()
235 .getInstantiationLineNumber(CL->getLocStart())
Ted Kremenek8aed8062008-10-31 00:13:20 +0000236 << " returned.";
237
238 R = CL->getSourceRange();
239 }
Ted Kremenekde8cd192008-11-02 00:35:25 +0000240 else if (const AllocaRegion* AR = dyn_cast<AllocaRegion>(V.getRegion())) {
241 const Expr* ARE = AR->getExpr();
242 SourceLocation L = ARE->getLocStart();
243 R = ARE->getSourceRange();
244
245 os << "Address of stack memory allocated by call to alloca() on line "
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000246 << BR.getSourceManager().getInstantiationLineNumber(L)
Ted Kremenekde8cd192008-11-02 00:35:25 +0000247 << " returned.";
248 }
Ted Kremenek8aed8062008-10-31 00:13:20 +0000249 else {
250 os << "Address of stack memory associated with local variable '"
251 << V.getRegion()->getString() << "' returned.";
252 }
Ted Kremenek22bda882008-07-31 20:31:27 +0000253
Ted Kremenekcf118d42009-02-04 23:49:09 +0000254 RangedBugReport *report = new RangedBugReport(*this, os.str().c_str(), N);
255 report->addRange(E->getSourceRange());
256 if (R.isValid()) report->addRange(R);
257 BR.EmitReport(report);
Ted Kremenekb7714b22008-07-30 17:49:12 +0000258 }
Ted Kremenek78d46242008-07-22 16:21:24 +0000259 }
260};
Ted Kremenek5917d782008-11-21 00:27:44 +0000261
262class VISIBILITY_HIDDEN RetUndef : public BuiltinBug {
263public:
Ted Kremenek5d88ff82009-04-02 02:40:26 +0000264 RetUndef(GRExprEngine* eng) : BuiltinBug(eng, "Uninitialized return value",
Ted Kremenek5917d782008-11-21 00:27:44 +0000265 "Uninitialized or undefined return value returned to caller.") {}
266
Ted Kremenekcf118d42009-02-04 23:49:09 +0000267 void FlushReportsImpl(BugReporter& BR, GRExprEngine& Eng) {
Ted Kremenek5917d782008-11-21 00:27:44 +0000268 Emit(BR, Eng.ret_undef_begin(), Eng.ret_undef_end());
269 }
270};
Ted Kremenek78d46242008-07-22 16:21:24 +0000271
272class VISIBILITY_HIDDEN UndefBranch : public BuiltinBug {
273 struct VISIBILITY_HIDDEN FindUndefExpr {
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000274 GRStateManager& VM;
275 const GRState* St;
Ted Kremenek78d46242008-07-22 16:21:24 +0000276
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000277 FindUndefExpr(GRStateManager& V, const GRState* S) : VM(V), St(S) {}
Ted Kremenek78d46242008-07-22 16:21:24 +0000278
Ted Kremenekb7714b22008-07-30 17:49:12 +0000279 Expr* FindExpr(Expr* Ex) {
Ted Kremenek78d46242008-07-22 16:21:24 +0000280 if (!MatchesCriteria(Ex))
Ted Kremenekb7714b22008-07-30 17:49:12 +0000281 return 0;
Ted Kremenek78d46242008-07-22 16:21:24 +0000282
Ted Kremenekb7714b22008-07-30 17:49:12 +0000283 for (Stmt::child_iterator I=Ex->child_begin(), E=Ex->child_end();I!=E;++I)
Ted Kremenek78d46242008-07-22 16:21:24 +0000284 if (Expr* ExI = dyn_cast_or_null<Expr>(*I)) {
285 Expr* E2 = FindExpr(ExI);
286 if (E2) return E2;
287 }
288
289 return Ex;
290 }
291
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000292 bool MatchesCriteria(Expr* Ex) { return VM.GetSVal(St, Ex).isUndef(); }
Ted Kremenek78d46242008-07-22 16:21:24 +0000293 };
294
295public:
Ted Kremenekcf118d42009-02-04 23:49:09 +0000296 UndefBranch(GRExprEngine *eng)
Ted Kremenek5d88ff82009-04-02 02:40:26 +0000297 : BuiltinBug(eng,"Use of uninitialized value",
Ted Kremenek78d46242008-07-22 16:21:24 +0000298 "Branch condition evaluates to an uninitialized value.") {}
299
Ted Kremenekcf118d42009-02-04 23:49:09 +0000300 void FlushReportsImpl(BugReporter& BR, GRExprEngine& Eng) {
Ted Kremenek78d46242008-07-22 16:21:24 +0000301 for (GRExprEngine::undef_branch_iterator I=Eng.undef_branches_begin(),
302 E=Eng.undef_branches_end(); I!=E; ++I) {
303
304 // What's going on here: we want to highlight the subexpression of the
305 // condition that is the most likely source of the "uninitialized
306 // branch condition." We do a recursive walk of the condition's
307 // subexpressions and roughly look for the most nested subexpression
308 // that binds to Undefined. We then highlight that expression's range.
Ted Kremenek78d46242008-07-22 16:21:24 +0000309 BlockEdge B = cast<BlockEdge>((*I)->getLocation());
310 Expr* Ex = cast<Expr>(B.getSrc()->getTerminatorCondition());
311 assert (Ex && "Block must have a terminator.");
312
313 // Get the predecessor node and check if is a PostStmt with the Stmt
314 // being the terminator condition. We want to inspect the state
315 // of that node instead because it will contain main information about
316 // the subexpressions.
Ted Kremenek78d46242008-07-22 16:21:24 +0000317 assert (!(*I)->pred_empty());
318
319 // Note: any predecessor will do. They should have identical state,
320 // since all the BlockEdge did was act as an error sink since the value
321 // had to already be undefined.
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000322 ExplodedNode<GRState> *N = *(*I)->pred_begin();
Ted Kremenek78d46242008-07-22 16:21:24 +0000323 ProgramPoint P = N->getLocation();
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000324 const GRState* St = (*I)->getState();
Ted Kremenek78d46242008-07-22 16:21:24 +0000325
326 if (PostStmt* PS = dyn_cast<PostStmt>(&P))
327 if (PS->getStmt() == Ex)
328 St = N->getState();
329
330 FindUndefExpr FindIt(Eng.getStateManager(), St);
331 Ex = FindIt.FindExpr(Ex);
332
Ted Kremenekcf118d42009-02-04 23:49:09 +0000333 RangedBugReport *R = new RangedBugReport(*this, desc.c_str(), *I);
334 R->addRange(Ex->getSourceRange());
335 BR.EmitReport(R);
Ted Kremenek78d46242008-07-22 16:21:24 +0000336 }
337 }
338};
339
Zhongxing Xu1c0c2332008-11-23 05:52:28 +0000340class VISIBILITY_HIDDEN OutOfBoundMemoryAccess : public BuiltinBug {
341public:
Ted Kremenekcf118d42009-02-04 23:49:09 +0000342 OutOfBoundMemoryAccess(GRExprEngine* eng)
Ted Kremenek5d88ff82009-04-02 02:40:26 +0000343 : BuiltinBug(eng,"Out-of-bounds memory access",
Ted Kremenekcf118d42009-02-04 23:49:09 +0000344 "Load or store into an out-of-bound memory position.") {}
Zhongxing Xu1c0c2332008-11-23 05:52:28 +0000345
Ted Kremenekcf118d42009-02-04 23:49:09 +0000346 void FlushReportsImpl(BugReporter& BR, GRExprEngine& Eng) {
Zhongxing Xu1c0c2332008-11-23 05:52:28 +0000347 Emit(BR, Eng.explicit_oob_memacc_begin(), Eng.explicit_oob_memacc_end());
348 }
349};
Ted Kremenekefd59942008-12-08 22:47:34 +0000350
Ted Kremenek159d2482008-12-09 00:44:16 +0000351class VISIBILITY_HIDDEN BadSizeVLA : public BuiltinBug {
Ted Kremenekefd59942008-12-08 22:47:34 +0000352public:
Ted Kremenek5d88ff82009-04-02 02:40:26 +0000353 BadSizeVLA(GRExprEngine* eng) :
354 BuiltinBug(eng, "Bad variable-length array (VLA) size") {}
Ted Kremenekefd59942008-12-08 22:47:34 +0000355
Ted Kremenekcf118d42009-02-04 23:49:09 +0000356 void FlushReportsImpl(BugReporter& BR, GRExprEngine& Eng) {
Ted Kremenekefd59942008-12-08 22:47:34 +0000357 for (GRExprEngine::ErrorNodes::iterator
Ted Kremenek159d2482008-12-09 00:44:16 +0000358 I = Eng.ExplicitBadSizedVLA.begin(),
359 E = Eng.ExplicitBadSizedVLA.end(); I!=E; ++I) {
360
361 // Determine whether this was a 'zero-sized' VLA or a VLA with an
362 // undefined size.
363 GRExprEngine::NodeTy* N = *I;
364 PostStmt PS = cast<PostStmt>(N->getLocation());
Ted Kremenekefd59942008-12-08 22:47:34 +0000365 DeclStmt *DS = cast<DeclStmt>(PS.getStmt());
366 VarDecl* VD = cast<VarDecl>(*DS->decl_begin());
367 QualType T = Eng.getContext().getCanonicalType(VD->getType());
368 VariableArrayType* VT = cast<VariableArrayType>(T);
Ted Kremenek159d2482008-12-09 00:44:16 +0000369 Expr* SizeExpr = VT->getSizeExpr();
Ted Kremenekefd59942008-12-08 22:47:34 +0000370
Ted Kremenek159d2482008-12-09 00:44:16 +0000371 std::string buf;
372 llvm::raw_string_ostream os(buf);
Ted Kremenek5d88ff82009-04-02 02:40:26 +0000373 os << "The expression used to specify the number of elements in the "
374 "variable-length array (VLA) '"
Ted Kremenekcf118d42009-02-04 23:49:09 +0000375 << VD->getNameAsString() << "' evaluates to ";
Ted Kremenek159d2482008-12-09 00:44:16 +0000376
Ted Kremenekcf118d42009-02-04 23:49:09 +0000377 if (Eng.getStateManager().GetSVal(N->getState(), SizeExpr).isUndef())
Ted Kremenek159d2482008-12-09 00:44:16 +0000378 os << "an undefined or garbage value.";
Ted Kremenekcf118d42009-02-04 23:49:09 +0000379 else
380 os << "0. VLAs with no elements have undefined behavior.";
Ted Kremenek159d2482008-12-09 00:44:16 +0000381
Ted Kremenekcf118d42009-02-04 23:49:09 +0000382 RangedBugReport *report = new RangedBugReport(*this, os.str().c_str(), N);
383 report->addRange(SizeExpr->getSourceRange());
384 BR.EmitReport(report);
Ted Kremenekefd59942008-12-08 22:47:34 +0000385 }
386 }
387};
Zhongxing Xu1c0c2332008-11-23 05:52:28 +0000388
Ted Kremenek78d46242008-07-22 16:21:24 +0000389//===----------------------------------------------------------------------===//
390// __attribute__(nonnull) checking
391
392class VISIBILITY_HIDDEN CheckAttrNonNull : public GRSimpleAPICheck {
Ted Kremenekcf118d42009-02-04 23:49:09 +0000393 BugType *BT;
394 BugReporter &BR;
Ted Kremenek78d46242008-07-22 16:21:24 +0000395
396public:
Ted Kremenekcf118d42009-02-04 23:49:09 +0000397 CheckAttrNonNull(BugReporter &br) : BT(0), BR(br) {}
Ted Kremenek78d46242008-07-22 16:21:24 +0000398
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000399 virtual bool Audit(ExplodedNode<GRState>* N, GRStateManager& VMgr) {
Ted Kremenek78d46242008-07-22 16:21:24 +0000400 CallExpr* CE = cast<CallExpr>(cast<PostStmt>(N->getLocation()).getStmt());
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000401 const GRState* state = N->getState();
Ted Kremenek78d46242008-07-22 16:21:24 +0000402
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000403 SVal X = VMgr.GetSVal(state, CE->getCallee());
Ted Kremenek78d46242008-07-22 16:21:24 +0000404
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000405 if (!isa<loc::FuncVal>(X))
Ted Kremenek78d46242008-07-22 16:21:24 +0000406 return false;
407
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000408 FunctionDecl* FD = dyn_cast<FunctionDecl>(cast<loc::FuncVal>(X).getDecl());
Ted Kremenek78d46242008-07-22 16:21:24 +0000409 const NonNullAttr* Att = FD->getAttr<NonNullAttr>();
410
411 if (!Att)
412 return false;
413
414 // Iterate through the arguments of CE and check them for null.
Ted Kremenek78d46242008-07-22 16:21:24 +0000415 unsigned idx = 0;
416 bool hasError = false;
417
418 for (CallExpr::arg_iterator I=CE->arg_begin(), E=CE->arg_end(); I!=E;
419 ++I, ++idx) {
420
421 if (!VMgr.isEqual(state, *I, 0) || !Att->isNonNull(idx))
422 continue;
Ted Kremenekcf118d42009-02-04 23:49:09 +0000423
424 // Lazily allocate the BugType object if it hasn't already been created.
425 // Ownership is transferred to the BugReporter object once the BugReport
426 // is passed to 'EmitWarning'.
Ted Kremenek5d88ff82009-04-02 02:40:26 +0000427 if (!BT) BT =
428 new BugType("Argument with 'nonnull' attribute passed null", "API");
Ted Kremenek78d46242008-07-22 16:21:24 +0000429
Ted Kremenekcf118d42009-02-04 23:49:09 +0000430 RangedBugReport *R = new RangedBugReport(*BT,
431 "Null pointer passed as an argument to a "
432 "'nonnull' parameter", N);
433
434 R->addRange((*I)->getSourceRange());
435 BR.EmitReport(R);
Ted Kremenek78d46242008-07-22 16:21:24 +0000436 hasError = true;
437 }
438
439 return hasError;
440 }
Ted Kremenek78d46242008-07-22 16:21:24 +0000441};
442} // end anonymous namespace
443
444//===----------------------------------------------------------------------===//
445// Check registration.
Ted Kremenekcf118d42009-02-04 23:49:09 +0000446//===----------------------------------------------------------------------===//
Ted Kremenek78d46242008-07-22 16:21:24 +0000447
448void GRExprEngine::RegisterInternalChecks() {
Ted Kremenekcf118d42009-02-04 23:49:09 +0000449 // Register internal "built-in" BugTypes with the BugReporter. These BugTypes
450 // are different than what probably many checks will do since they don't
451 // create BugReports on-the-fly but instead wait until GRExprEngine finishes
452 // analyzing a function. Generation of BugReport objects is done via a call
453 // to 'FlushReports' from BugReporter.
454 BR.Register(new NullDeref(this));
455 BR.Register(new UndefinedDeref(this));
456 BR.Register(new UndefBranch(this));
457 BR.Register(new DivZero(this));
458 BR.Register(new UndefResult(this));
459 BR.Register(new BadCall(this));
460 BR.Register(new RetStack(this));
461 BR.Register(new RetUndef(this));
462 BR.Register(new BadArg(this));
463 BR.Register(new BadMsgExprArg(this));
464 BR.Register(new BadReceiver(this));
465 BR.Register(new OutOfBoundMemoryAccess(this));
466 BR.Register(new BadSizeVLA(this));
Ted Kremenek21fe8372009-02-19 04:06:22 +0000467 BR.Register(new NilReceiverStructRet(this));
Ted Kremenekcf118d42009-02-04 23:49:09 +0000468
469 // The following checks do not need to have their associated BugTypes
470 // explicitly registered with the BugReporter. If they issue any BugReports,
471 // their associated BugType will get registered with the BugReporter
472 // automatically. Note that the check itself is owned by the GRExprEngine
473 // object.
474 AddCheck(new CheckAttrNonNull(BR), Stmt::CallExprClass);
Ted Kremenek78d46242008-07-22 16:21:24 +0000475}