blob: fcc2db467fec1050f2d2c39607e75319688a2fd8 [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 Kremenekc26a8b02009-07-22 21:46:56 +000017#include "clang/Analysis/PathSensitive/CheckerVisitor.h"
Ted Kremenekdd986cc2009-05-07 00:45:33 +000018#include "clang/Analysis/PathDiagnostic.h"
Ted Kremenek8aed8062008-10-31 00:13:20 +000019#include "clang/Basic/SourceManager.h"
Ted Kremenek78d46242008-07-22 16:21:24 +000020#include "llvm/Support/Compiler.h"
Ted Kremenekad51a602008-10-31 00:18:30 +000021#include "llvm/Support/raw_ostream.h"
Ted Kremenek78d46242008-07-22 16:21:24 +000022
23using namespace clang;
Ted Kremenek53500662009-07-22 17:55:28 +000024using namespace clang::bugreporter;
Ted Kremenek78d46242008-07-22 16:21:24 +000025
26//===----------------------------------------------------------------------===//
27// Utility functions.
28//===----------------------------------------------------------------------===//
29
30template <typename ITERATOR> inline
Zhongxing Xuc5619d92009-08-06 01:32:16 +000031ExplodedNode* GetNode(ITERATOR I) {
Ted Kremenek78d46242008-07-22 16:21:24 +000032 return *I;
33}
34
35template <> inline
Zhongxing Xuc5619d92009-08-06 01:32:16 +000036ExplodedNode* GetNode(GRExprEngine::undef_arg_iterator I) {
Ted Kremenek78d46242008-07-22 16:21:24 +000037 return I->first;
38}
39
40//===----------------------------------------------------------------------===//
41// Bug Descriptions.
42//===----------------------------------------------------------------------===//
43
44namespace {
Ted Kremenekdd986cc2009-05-07 00:45:33 +000045
Ted Kremenek0c313172009-05-13 19:16:35 +000046class VISIBILITY_HIDDEN BuiltinBugReport : public RangedBugReport {
Ted Kremenekdd986cc2009-05-07 00:45:33 +000047public:
48 BuiltinBugReport(BugType& bt, const char* desc,
Zhongxing Xuc5619d92009-08-06 01:32:16 +000049 ExplodedNode *n)
Ted Kremenek0c313172009-05-13 19:16:35 +000050 : RangedBugReport(bt, desc, n) {}
Mike Stump1eb44332009-09-09 15:08:12 +000051
Ted Kremenek85ac9342009-05-15 05:25:09 +000052 BuiltinBugReport(BugType& bt, const char *shortDesc, const char *desc,
Zhongxing Xuc5619d92009-08-06 01:32:16 +000053 ExplodedNode *n)
Mike Stump1eb44332009-09-09 15:08:12 +000054 : RangedBugReport(bt, shortDesc, desc, n) {}
55
Ted Kremenekdd986cc2009-05-07 00:45:33 +000056 void registerInitialVisitors(BugReporterContext& BRC,
Zhongxing Xuc5619d92009-08-06 01:32:16 +000057 const ExplodedNode* N);
Mike Stump1eb44332009-09-09 15:08:12 +000058};
59
Ted Kremenekcf118d42009-02-04 23:49:09 +000060class VISIBILITY_HIDDEN BuiltinBug : public BugType {
61 GRExprEngine &Eng;
Ted Kremenek159d2482008-12-09 00:44:16 +000062protected:
Ted Kremenekcf118d42009-02-04 23:49:09 +000063 const std::string desc;
Ted Kremenek78d46242008-07-22 16:21:24 +000064public:
Ted Kremenekcf118d42009-02-04 23:49:09 +000065 BuiltinBug(GRExprEngine *eng, const char* n, const char* d)
Ted Kremenek0c313172009-05-13 19:16:35 +000066 : BugType(n, "Logic errors"), Eng(*eng), desc(d) {}
Ted Kremenekcf118d42009-02-04 23:49:09 +000067
68 BuiltinBug(GRExprEngine *eng, const char* n)
Ted Kremenek0c313172009-05-13 19:16:35 +000069 : BugType(n, "Logic errors"), Eng(*eng), desc(n) {}
Zhongxing Xu904e1e32009-09-02 07:09:39 +000070
71 const std::string &getDescription() const { return desc; }
Mike Stump1eb44332009-09-09 15:08:12 +000072
Zhongxing Xud99f3612009-09-02 08:10:35 +000073 virtual void FlushReportsImpl(BugReporter& BR, GRExprEngine& Eng) {}
Ted Kremenekcf118d42009-02-04 23:49:09 +000074
75 void FlushReports(BugReporter& BR) { FlushReportsImpl(BR, Eng); }
Mike Stump1eb44332009-09-09 15:08:12 +000076
Ted Kremenekdd986cc2009-05-07 00:45:33 +000077 virtual void registerInitialVisitors(BugReporterContext& BRC,
Zhongxing Xuc5619d92009-08-06 01:32:16 +000078 const ExplodedNode* N,
Ted Kremenekdd986cc2009-05-07 00:45:33 +000079 BuiltinBugReport *R) {}
Mike Stump1eb44332009-09-09 15:08:12 +000080
Ted Kremenekdd986cc2009-05-07 00:45:33 +000081 template <typename ITER> void Emit(BugReporter& BR, ITER I, ITER E);
Ted Kremenek78d46242008-07-22 16:21:24 +000082};
Mike Stump1eb44332009-09-09 15:08:12 +000083
84
Ted Kremenekdd986cc2009-05-07 00:45:33 +000085template <typename ITER>
86void BuiltinBug::Emit(BugReporter& BR, ITER I, ITER E) {
87 for (; I != E; ++I) BR.EmitReport(new BuiltinBugReport(*this, desc.c_str(),
88 GetNode(I)));
Mike Stump1eb44332009-09-09 15:08:12 +000089}
Ted Kremenekdd986cc2009-05-07 00:45:33 +000090
91void BuiltinBugReport::registerInitialVisitors(BugReporterContext& BRC,
Zhongxing Xuc5619d92009-08-06 01:32:16 +000092 const ExplodedNode* N) {
Ted Kremenekdd986cc2009-05-07 00:45:33 +000093 static_cast<BuiltinBug&>(getBugType()).registerInitialVisitors(BRC, N, this);
Mike Stump1eb44332009-09-09 15:08:12 +000094}
95
Ted Kremenek78d46242008-07-22 16:21:24 +000096class VISIBILITY_HIDDEN NullDeref : public BuiltinBug {
97public:
Ted Kremenekcf118d42009-02-04 23:49:09 +000098 NullDeref(GRExprEngine* eng)
Ted Kremenek0fa96542009-04-07 04:54:31 +000099 : BuiltinBug(eng,"Null dereference", "Dereference of null pointer") {}
Ted Kremenek78d46242008-07-22 16:21:24 +0000100
Ted Kremenekcf118d42009-02-04 23:49:09 +0000101 void FlushReportsImpl(BugReporter& BR, GRExprEngine& Eng) {
Ted Kremenek78d46242008-07-22 16:21:24 +0000102 Emit(BR, Eng.null_derefs_begin(), Eng.null_derefs_end());
103 }
Mike Stump1eb44332009-09-09 15:08:12 +0000104
Ted Kremenekdd986cc2009-05-07 00:45:33 +0000105 void registerInitialVisitors(BugReporterContext& BRC,
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000106 const ExplodedNode* N,
Ted Kremenekdd986cc2009-05-07 00:45:33 +0000107 BuiltinBugReport *R) {
Ted Kremenek85ac9342009-05-15 05:25:09 +0000108 registerTrackNullOrUndefValue(BRC, GetDerefExpr(N), N);
Ted Kremenekdd986cc2009-05-07 00:45:33 +0000109 }
Ted Kremenek78d46242008-07-22 16:21:24 +0000110};
Mike Stump1eb44332009-09-09 15:08:12 +0000111
Ted Kremenek0c313172009-05-13 19:16:35 +0000112class VISIBILITY_HIDDEN NilReceiverStructRet : public BuiltinBug {
Ted Kremenek21fe8372009-02-19 04:06:22 +0000113public:
114 NilReceiverStructRet(GRExprEngine* eng) :
Ted Kremenek0c313172009-05-13 19:16:35 +0000115 BuiltinBug(eng, "'nil' receiver with struct return type") {}
Ted Kremenek21fe8372009-02-19 04:06:22 +0000116
Ted Kremenek0c313172009-05-13 19:16:35 +0000117 void FlushReportsImpl(BugReporter& BR, GRExprEngine& Eng) {
Ted Kremenek21fe8372009-02-19 04:06:22 +0000118 for (GRExprEngine::nil_receiver_struct_ret_iterator
119 I=Eng.nil_receiver_struct_ret_begin(),
120 E=Eng.nil_receiver_struct_ret_end(); I!=E; ++I) {
121
122 std::string sbuf;
123 llvm::raw_string_ostream os(sbuf);
124 PostStmt P = cast<PostStmt>((*I)->getLocation());
Ted Kremenek5f85e172009-07-22 22:35:28 +0000125 const ObjCMessageExpr *ME = cast<ObjCMessageExpr>(P.getStmt());
Ted Kremenek21fe8372009-02-19 04:06:22 +0000126 os << "The receiver in the message expression is 'nil' and results in the"
127 " returned value (of type '"
128 << ME->getType().getAsString()
Ted Kremenek5b9bd212009-09-11 22:07:28 +0000129 << "') to be garbage or otherwise undefined";
Ted Kremenek21fe8372009-02-19 04:06:22 +0000130
Ted Kremenek0c313172009-05-13 19:16:35 +0000131 BuiltinBugReport *R = new BuiltinBugReport(*this, os.str().c_str(), *I);
Ted Kremenek21fe8372009-02-19 04:06:22 +0000132 R->addRange(ME->getReceiver()->getSourceRange());
133 BR.EmitReport(R);
134 }
135 }
Mike Stump1eb44332009-09-09 15:08:12 +0000136
Ted Kremenek0c313172009-05-13 19:16:35 +0000137 void registerInitialVisitors(BugReporterContext& BRC,
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000138 const ExplodedNode* N,
Ted Kremenek0c313172009-05-13 19:16:35 +0000139 BuiltinBugReport *R) {
Ted Kremenek85ac9342009-05-15 05:25:09 +0000140 registerTrackNullOrUndefValue(BRC, GetReceiverExpr(N), N);
Ted Kremenek0c313172009-05-13 19:16:35 +0000141 }
Ted Kremenek21fe8372009-02-19 04:06:22 +0000142};
Ted Kremenek899b3de2009-04-08 03:07:17 +0000143
Ted Kremenek0c313172009-05-13 19:16:35 +0000144class VISIBILITY_HIDDEN NilReceiverLargerThanVoidPtrRet : public BuiltinBug {
Ted Kremenek899b3de2009-04-08 03:07:17 +0000145public:
146 NilReceiverLargerThanVoidPtrRet(GRExprEngine* eng) :
Ted Kremenek0c313172009-05-13 19:16:35 +0000147 BuiltinBug(eng,
148 "'nil' receiver with return type larger than sizeof(void *)") {}
Mike Stump1eb44332009-09-09 15:08:12 +0000149
Ted Kremenek0c313172009-05-13 19:16:35 +0000150 void FlushReportsImpl(BugReporter& BR, GRExprEngine& Eng) {
Ted Kremenek899b3de2009-04-08 03:07:17 +0000151 for (GRExprEngine::nil_receiver_larger_than_voidptr_ret_iterator
152 I=Eng.nil_receiver_larger_than_voidptr_ret_begin(),
153 E=Eng.nil_receiver_larger_than_voidptr_ret_end(); I!=E; ++I) {
Mike Stump1eb44332009-09-09 15:08:12 +0000154
Ted Kremenek899b3de2009-04-08 03:07:17 +0000155 std::string sbuf;
156 llvm::raw_string_ostream os(sbuf);
157 PostStmt P = cast<PostStmt>((*I)->getLocation());
Ted Kremenek5f85e172009-07-22 22:35:28 +0000158 const ObjCMessageExpr *ME = cast<ObjCMessageExpr>(P.getStmt());
Ted Kremenek899b3de2009-04-08 03:07:17 +0000159 os << "The receiver in the message expression is 'nil' and results in the"
160 " returned value (of type '"
161 << ME->getType().getAsString()
162 << "' and of size "
163 << Eng.getContext().getTypeSize(ME->getType()) / 8
Ted Kremenek5b9bd212009-09-11 22:07:28 +0000164 << " bytes) to be garbage or otherwise undefined";
Mike Stump1eb44332009-09-09 15:08:12 +0000165
Ted Kremenek0c313172009-05-13 19:16:35 +0000166 BuiltinBugReport *R = new BuiltinBugReport(*this, os.str().c_str(), *I);
Ted Kremenek899b3de2009-04-08 03:07:17 +0000167 R->addRange(ME->getReceiver()->getSourceRange());
168 BR.EmitReport(R);
169 }
Mike Stump1eb44332009-09-09 15:08:12 +0000170 }
Ted Kremenek0c313172009-05-13 19:16:35 +0000171 void registerInitialVisitors(BugReporterContext& BRC,
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000172 const ExplodedNode* N,
Ted Kremenek0c313172009-05-13 19:16:35 +0000173 BuiltinBugReport *R) {
Ted Kremenek85ac9342009-05-15 05:25:09 +0000174 registerTrackNullOrUndefValue(BRC, GetReceiverExpr(N), N);
Ted Kremenek899b3de2009-04-08 03:07:17 +0000175 }
176};
Mike Stump1eb44332009-09-09 15:08:12 +0000177
Ted Kremenek78d46242008-07-22 16:21:24 +0000178class VISIBILITY_HIDDEN UndefinedDeref : public BuiltinBug {
179public:
Ted Kremenekcf118d42009-02-04 23:49:09 +0000180 UndefinedDeref(GRExprEngine* eng)
Ted Kremenek17a8e072009-03-01 05:43:22 +0000181 : BuiltinBug(eng,"Dereference of undefined pointer value") {}
Mike Stump1eb44332009-09-09 15:08:12 +0000182
Ted Kremenekcf118d42009-02-04 23:49:09 +0000183 void FlushReportsImpl(BugReporter& BR, GRExprEngine& Eng) {
Ted Kremenek78d46242008-07-22 16:21:24 +0000184 Emit(BR, Eng.undef_derefs_begin(), Eng.undef_derefs_end());
185 }
Mike Stump1eb44332009-09-09 15:08:12 +0000186
Ted Kremenek0c313172009-05-13 19:16:35 +0000187 void registerInitialVisitors(BugReporterContext& BRC,
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000188 const ExplodedNode* N,
Ted Kremenek0c313172009-05-13 19:16:35 +0000189 BuiltinBugReport *R) {
Ted Kremenek85ac9342009-05-15 05:25:09 +0000190 registerTrackNullOrUndefValue(BRC, GetDerefExpr(N), N);
Ted Kremenek0c313172009-05-13 19:16:35 +0000191 }
Ted Kremenek78d46242008-07-22 16:21:24 +0000192};
193
194class VISIBILITY_HIDDEN DivZero : public BuiltinBug {
195public:
Zhongxing Xu6403b572009-09-02 13:26:26 +0000196 DivZero(GRExprEngine* eng = 0)
Ted Kremenek5d88ff82009-04-02 02:40:26 +0000197 : BuiltinBug(eng,"Division-by-zero",
198 "Division by zero or undefined value.") {}
Mike Stump1eb44332009-09-09 15:08:12 +0000199
Ted Kremenek85ac9342009-05-15 05:25:09 +0000200 void registerInitialVisitors(BugReporterContext& BRC,
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000201 const ExplodedNode* N,
Ted Kremenek85ac9342009-05-15 05:25:09 +0000202 BuiltinBugReport *R) {
203 registerTrackNullOrUndefValue(BRC, GetDenomExpr(N), N);
204 }
Ted Kremenek78d46242008-07-22 16:21:24 +0000205};
Mike Stump1eb44332009-09-09 15:08:12 +0000206
Ted Kremenek78d46242008-07-22 16:21:24 +0000207class VISIBILITY_HIDDEN UndefResult : public BuiltinBug {
208public:
Ted Kremenek5b9bd212009-09-11 22:07:28 +0000209 UndefResult(GRExprEngine* eng)
210 : BuiltinBug(eng,"Undefined or garbage result",
211 "Result of operation is garbage or undefined") {}
Mike Stump1eb44332009-09-09 15:08:12 +0000212
Ted Kremenekcf118d42009-02-04 23:49:09 +0000213 void FlushReportsImpl(BugReporter& BR, GRExprEngine& Eng) {
Ted Kremenek5b9bd212009-09-11 22:07:28 +0000214 for (GRExprEngine::undef_result_iterator I=Eng.undef_results_begin(),
215 E = Eng.undef_results_end(); I!=E; ++I) {
216
217 ExplodedNode *N = *I;
218 const Stmt *S = N->getLocationAs<PostStmt>()->getStmt();
219 BuiltinBugReport *report = NULL;
220
221 if (const BinaryOperator *B = dyn_cast<BinaryOperator>(S)) {
222 llvm::SmallString<256> sbuf;
223 llvm::raw_svector_ostream OS(sbuf);
224 const GRState *ST = N->getState();
225 const Expr *Ex;
226
227 if (ST->getSVal(B->getLHS()).isUndef()) {
228 Ex = B->getLHS()->IgnoreParenCasts();
229 OS << "The left operand of the '";
230 }
231 else {
232 assert(ST->getSVal(B->getRHS()).isUndef());
233 Ex = B->getRHS()->IgnoreParenCasts();
234 OS << "The right operand of the '";
235 }
236
237 OS << BinaryOperator::getOpcodeStr(B->getOpcode())
238 << "' expression is an undefined "
239 "or otherwise garbage value";
240
241 // FIXME: Use StringRefs to pass string information.
242 report = new BuiltinBugReport(*this, OS.str().str().c_str(), N);
243 report->addRange(Ex->getSourceRange());
244 }
245 else {
246 report = new BuiltinBugReport(*this,
247 "Expression evaluates to an uninitialized"
248 " or undefined value", N);
249 }
250
251 BR.EmitReport(report);
252 }
253 }
254
255 void registerInitialVisitors(BugReporterContext& BRC,
256 const ExplodedNode* N,
257 BuiltinBugReport *R) {
258
259 const Stmt *S = N->getLocationAs<StmtPoint>()->getStmt();
260 const Stmt *X = S;
261
262 if (const BinaryOperator *B = dyn_cast<BinaryOperator>(S)) {
263 const GRState *ST = N->getState();
264 X = ST->getSVal(B->getLHS()).isUndef()
265 ? B->getLHS()->IgnoreParenCasts() : B->getRHS()->IgnoreParenCasts();
266 }
267
268 registerTrackNullOrUndefValue(BRC, X, N);
Ted Kremenek78d46242008-07-22 16:21:24 +0000269 }
270};
Mike Stump1eb44332009-09-09 15:08:12 +0000271
Ted Kremenek78d46242008-07-22 16:21:24 +0000272class VISIBILITY_HIDDEN BadCall : public BuiltinBug {
273public:
Zhongxing Xud99f3612009-09-02 08:10:35 +0000274 BadCall(GRExprEngine *eng = 0)
Ted Kremenek5d88ff82009-04-02 02:40:26 +0000275 : BuiltinBug(eng, "Invalid function call",
Ted Kremenek17a8e072009-03-01 05:43:22 +0000276 "Called function pointer is a null or undefined pointer value") {}
Mike Stump1eb44332009-09-09 15:08:12 +0000277
Ted Kremenek85ac9342009-05-15 05:25:09 +0000278 void registerInitialVisitors(BugReporterContext& BRC,
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000279 const ExplodedNode* N,
Ted Kremenek85ac9342009-05-15 05:25:09 +0000280 BuiltinBugReport *R) {
281 registerTrackNullOrUndefValue(BRC, GetCalleeExpr(N), N);
282 }
283};
284
285
286class VISIBILITY_HIDDEN ArgReport : public BuiltinBugReport {
287 const Stmt *Arg;
288public:
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000289 ArgReport(BugType& bt, const char* desc, ExplodedNode *n,
Ted Kremenek85ac9342009-05-15 05:25:09 +0000290 const Stmt *arg)
291 : BuiltinBugReport(bt, desc, n), Arg(arg) {}
Mike Stump1eb44332009-09-09 15:08:12 +0000292
Ted Kremenek85ac9342009-05-15 05:25:09 +0000293 ArgReport(BugType& bt, const char *shortDesc, const char *desc,
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000294 ExplodedNode *n, const Stmt *arg)
Mike Stump1eb44332009-09-09 15:08:12 +0000295 : BuiltinBugReport(bt, shortDesc, desc, n), Arg(arg) {}
296
297 const Stmt *getArg() const { return Arg; }
Ted Kremenek78d46242008-07-22 16:21:24 +0000298};
299
300class VISIBILITY_HIDDEN BadArg : public BuiltinBug {
Mike Stump1eb44332009-09-09 15:08:12 +0000301public:
302 BadArg(GRExprEngine* eng=0) : BuiltinBug(eng,"Uninitialized argument",
Ted Kremenek5b9bd212009-09-11 22:07:28 +0000303 "Pass-by-value argument in function call is undefined") {}
Ted Kremenek78d46242008-07-22 16:21:24 +0000304
Ted Kremenekcf118d42009-02-04 23:49:09 +0000305 BadArg(GRExprEngine* eng, const char* d)
Ted Kremenek5d88ff82009-04-02 02:40:26 +0000306 : BuiltinBug(eng,"Uninitialized argument", d) {}
Mike Stump1eb44332009-09-09 15:08:12 +0000307
Ted Kremenek85ac9342009-05-15 05:25:09 +0000308 void registerInitialVisitors(BugReporterContext& BRC,
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000309 const ExplodedNode* N,
Ted Kremenek85ac9342009-05-15 05:25:09 +0000310 BuiltinBugReport *R) {
311 registerTrackNullOrUndefValue(BRC, static_cast<ArgReport*>(R)->getArg(),
312 N);
Mike Stump1eb44332009-09-09 15:08:12 +0000313 }
Ted Kremenek78d46242008-07-22 16:21:24 +0000314};
Mike Stump1eb44332009-09-09 15:08:12 +0000315
Ted Kremenek78d46242008-07-22 16:21:24 +0000316class VISIBILITY_HIDDEN BadMsgExprArg : public BadArg {
317public:
Mike Stump1eb44332009-09-09 15:08:12 +0000318 BadMsgExprArg(GRExprEngine* eng)
Ted Kremenek5d88ff82009-04-02 02:40:26 +0000319 : BadArg(eng,"Pass-by-value argument in message expression is undefined"){}
Mike Stump1eb44332009-09-09 15:08:12 +0000320
Ted Kremenekcf118d42009-02-04 23:49:09 +0000321 void FlushReportsImpl(BugReporter& BR, GRExprEngine& Eng) {
Ted Kremenek78d46242008-07-22 16:21:24 +0000322 for (GRExprEngine::UndefArgsTy::iterator I=Eng.msg_expr_undef_arg_begin(),
Mike Stump1eb44332009-09-09 15:08:12 +0000323 E = Eng.msg_expr_undef_arg_end(); I!=E; ++I) {
Ted Kremenek78d46242008-07-22 16:21:24 +0000324 // Generate a report for this bug.
Ted Kremenek85ac9342009-05-15 05:25:09 +0000325 ArgReport *report = new ArgReport(*this, desc.c_str(), I->first,
326 I->second);
Ted Kremenekcf118d42009-02-04 23:49:09 +0000327 report->addRange(I->second->getSourceRange());
328 BR.EmitReport(report);
Mike Stump1eb44332009-09-09 15:08:12 +0000329 }
330 }
Ted Kremenek78d46242008-07-22 16:21:24 +0000331};
Mike Stump1eb44332009-09-09 15:08:12 +0000332
Ted Kremenek78d46242008-07-22 16:21:24 +0000333class VISIBILITY_HIDDEN BadReceiver : public BuiltinBug {
Mike Stump1eb44332009-09-09 15:08:12 +0000334public:
Ted Kremenekcf118d42009-02-04 23:49:09 +0000335 BadReceiver(GRExprEngine* eng)
Ted Kremenek5d88ff82009-04-02 02:40:26 +0000336 : BuiltinBug(eng,"Uninitialized receiver",
337 "Receiver in message expression is an uninitialized value") {}
Mike Stump1eb44332009-09-09 15:08:12 +0000338
Ted Kremenekcf118d42009-02-04 23:49:09 +0000339 void FlushReportsImpl(BugReporter& BR, GRExprEngine& Eng) {
Ted Kremenekefd59942008-12-08 22:47:34 +0000340 for (GRExprEngine::ErrorNodes::iterator I=Eng.undef_receivers_begin(),
Ted Kremenek78d46242008-07-22 16:21:24 +0000341 End = Eng.undef_receivers_end(); I!=End; ++I) {
Mike Stump1eb44332009-09-09 15:08:12 +0000342
Ted Kremenek78d46242008-07-22 16:21:24 +0000343 // Generate a report for this bug.
Ted Kremenek0c313172009-05-13 19:16:35 +0000344 BuiltinBugReport *report = new BuiltinBugReport(*this, desc.c_str(), *I);
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000345 ExplodedNode* N = *I;
Ted Kremenek5f85e172009-07-22 22:35:28 +0000346 const Stmt *S = cast<PostStmt>(N->getLocation()).getStmt();
347 const Expr* E = cast<ObjCMessageExpr>(S)->getReceiver();
Ted Kremenek78d46242008-07-22 16:21:24 +0000348 assert (E && "Receiver cannot be NULL");
Ted Kremenekcf118d42009-02-04 23:49:09 +0000349 report->addRange(E->getSourceRange());
350 BR.EmitReport(report);
Ted Kremenek0c313172009-05-13 19:16:35 +0000351 }
352 }
Ted Kremenek85ac9342009-05-15 05:25:09 +0000353
Ted Kremenek0c313172009-05-13 19:16:35 +0000354 void registerInitialVisitors(BugReporterContext& BRC,
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000355 const ExplodedNode* N,
Ted Kremenek0c313172009-05-13 19:16:35 +0000356 BuiltinBugReport *R) {
Ted Kremenek85ac9342009-05-15 05:25:09 +0000357 registerTrackNullOrUndefValue(BRC, GetReceiverExpr(N), N);
Mike Stump1eb44332009-09-09 15:08:12 +0000358 }
Ted Kremenek78d46242008-07-22 16:21:24 +0000359};
Ted Kremenek5917d782008-11-21 00:27:44 +0000360
Ted Kremenek78d46242008-07-22 16:21:24 +0000361class VISIBILITY_HIDDEN RetStack : public BuiltinBug {
362public:
Ted Kremenek17a8e072009-03-01 05:43:22 +0000363 RetStack(GRExprEngine* eng)
Ted Kremenek5d88ff82009-04-02 02:40:26 +0000364 : BuiltinBug(eng, "Return of address to stack-allocated memory") {}
Mike Stump1eb44332009-09-09 15:08:12 +0000365
Ted Kremenekcf118d42009-02-04 23:49:09 +0000366 void FlushReportsImpl(BugReporter& BR, GRExprEngine& Eng) {
Ted Kremenekb7714b22008-07-30 17:49:12 +0000367 for (GRExprEngine::ret_stackaddr_iterator I=Eng.ret_stackaddr_begin(),
368 End = Eng.ret_stackaddr_end(); I!=End; ++I) {
Ted Kremenek22bda882008-07-31 20:31:27 +0000369
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000370 ExplodedNode* N = *I;
Ted Kremenek5f85e172009-07-22 22:35:28 +0000371 const Stmt *S = cast<PostStmt>(N->getLocation()).getStmt();
372 const Expr* E = cast<ReturnStmt>(S)->getRetValue();
373 assert(E && "Return expression cannot be NULL");
Mike Stump1eb44332009-09-09 15:08:12 +0000374
Ted Kremenek22bda882008-07-31 20:31:27 +0000375 // Get the value associated with E.
Ted Kremenek23ec48c2009-06-18 23:58:37 +0000376 loc::MemRegionVal V = cast<loc::MemRegionVal>(N->getState()->getSVal(E));
Mike Stump1eb44332009-09-09 15:08:12 +0000377
Ted Kremenek22bda882008-07-31 20:31:27 +0000378 // Generate a report for this bug.
Ted Kremenekad51a602008-10-31 00:18:30 +0000379 std::string buf;
380 llvm::raw_string_ostream os(buf);
Ted Kremenek8aed8062008-10-31 00:13:20 +0000381 SourceRange R;
Mike Stump1eb44332009-09-09 15:08:12 +0000382
Ted Kremenek8aed8062008-10-31 00:13:20 +0000383 // Check if the region is a compound literal.
Mike Stump1eb44332009-09-09 15:08:12 +0000384 if (const CompoundLiteralRegion* CR =
Ted Kremenek8aed8062008-10-31 00:13:20 +0000385 dyn_cast<CompoundLiteralRegion>(V.getRegion())) {
Mike Stump1eb44332009-09-09 15:08:12 +0000386
Ted Kremenek8aed8062008-10-31 00:13:20 +0000387 const CompoundLiteralExpr* CL = CR->getLiteralExpr();
388 os << "Address of stack memory associated with a compound literal "
389 "declared on line "
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000390 << BR.getSourceManager()
391 .getInstantiationLineNumber(CL->getLocStart())
Ted Kremenek8aed8062008-10-31 00:13:20 +0000392 << " returned.";
Mike Stump1eb44332009-09-09 15:08:12 +0000393
Ted Kremenek8aed8062008-10-31 00:13:20 +0000394 R = CL->getSourceRange();
395 }
Ted Kremenekde8cd192008-11-02 00:35:25 +0000396 else if (const AllocaRegion* AR = dyn_cast<AllocaRegion>(V.getRegion())) {
397 const Expr* ARE = AR->getExpr();
398 SourceLocation L = ARE->getLocStart();
399 R = ARE->getSourceRange();
Mike Stump1eb44332009-09-09 15:08:12 +0000400
Ted Kremenekde8cd192008-11-02 00:35:25 +0000401 os << "Address of stack memory allocated by call to alloca() on line "
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000402 << BR.getSourceManager().getInstantiationLineNumber(L)
Ted Kremenekde8cd192008-11-02 00:35:25 +0000403 << " returned.";
Mike Stump1eb44332009-09-09 15:08:12 +0000404 }
405 else {
Ted Kremenek8aed8062008-10-31 00:13:20 +0000406 os << "Address of stack memory associated with local variable '"
407 << V.getRegion()->getString() << "' returned.";
408 }
Mike Stump1eb44332009-09-09 15:08:12 +0000409
Ted Kremenekcf118d42009-02-04 23:49:09 +0000410 RangedBugReport *report = new RangedBugReport(*this, os.str().c_str(), N);
411 report->addRange(E->getSourceRange());
412 if (R.isValid()) report->addRange(R);
413 BR.EmitReport(report);
Ted Kremenekb7714b22008-07-30 17:49:12 +0000414 }
Ted Kremenek78d46242008-07-22 16:21:24 +0000415 }
416};
Mike Stump1eb44332009-09-09 15:08:12 +0000417
Ted Kremenek5917d782008-11-21 00:27:44 +0000418class VISIBILITY_HIDDEN RetUndef : public BuiltinBug {
419public:
Ted Kremenek5b9bd212009-09-11 22:07:28 +0000420 RetUndef(GRExprEngine* eng) : BuiltinBug(eng, "Garbage return value",
421 "Undefined or garbage value returned to caller") {}
Mike Stump1eb44332009-09-09 15:08:12 +0000422
Ted Kremenekcf118d42009-02-04 23:49:09 +0000423 void FlushReportsImpl(BugReporter& BR, GRExprEngine& Eng) {
Ted Kremenek5917d782008-11-21 00:27:44 +0000424 Emit(BR, Eng.ret_undef_begin(), Eng.ret_undef_end());
425 }
Mike Stump1eb44332009-09-09 15:08:12 +0000426
Ted Kremenek0c313172009-05-13 19:16:35 +0000427 void registerInitialVisitors(BugReporterContext& BRC,
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000428 const ExplodedNode* N,
Ted Kremenek0c313172009-05-13 19:16:35 +0000429 BuiltinBugReport *R) {
Ted Kremenek85ac9342009-05-15 05:25:09 +0000430 registerTrackNullOrUndefValue(BRC, GetRetValExpr(N), N);
Mike Stump1eb44332009-09-09 15:08:12 +0000431 }
Ted Kremenek5917d782008-11-21 00:27:44 +0000432};
Ted Kremenek78d46242008-07-22 16:21:24 +0000433
434class VISIBILITY_HIDDEN UndefBranch : public BuiltinBug {
435 struct VISIBILITY_HIDDEN FindUndefExpr {
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000436 GRStateManager& VM;
437 const GRState* St;
Mike Stump1eb44332009-09-09 15:08:12 +0000438
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000439 FindUndefExpr(GRStateManager& V, const GRState* S) : VM(V), St(S) {}
Mike Stump1eb44332009-09-09 15:08:12 +0000440
441 Expr* FindExpr(Expr* Ex) {
Ted Kremenek78d46242008-07-22 16:21:24 +0000442 if (!MatchesCriteria(Ex))
Ted Kremenekb7714b22008-07-30 17:49:12 +0000443 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000444
Ted Kremenekb7714b22008-07-30 17:49:12 +0000445 for (Stmt::child_iterator I=Ex->child_begin(), E=Ex->child_end();I!=E;++I)
Ted Kremenek78d46242008-07-22 16:21:24 +0000446 if (Expr* ExI = dyn_cast_or_null<Expr>(*I)) {
447 Expr* E2 = FindExpr(ExI);
448 if (E2) return E2;
449 }
Mike Stump1eb44332009-09-09 15:08:12 +0000450
Ted Kremenek78d46242008-07-22 16:21:24 +0000451 return Ex;
452 }
Mike Stump1eb44332009-09-09 15:08:12 +0000453
Ted Kremenek23ec48c2009-06-18 23:58:37 +0000454 bool MatchesCriteria(Expr* Ex) { return St->getSVal(Ex).isUndef(); }
Ted Kremenek78d46242008-07-22 16:21:24 +0000455 };
Mike Stump1eb44332009-09-09 15:08:12 +0000456
Ted Kremenek78d46242008-07-22 16:21:24 +0000457public:
Ted Kremenekcf118d42009-02-04 23:49:09 +0000458 UndefBranch(GRExprEngine *eng)
Ted Kremenek5b9bd212009-09-11 22:07:28 +0000459 : BuiltinBug(eng,"Use of garbage value",
460 "Branch condition evaluates to an undefined or garbage value")
461 {}
Mike Stump1eb44332009-09-09 15:08:12 +0000462
Ted Kremenekcf118d42009-02-04 23:49:09 +0000463 void FlushReportsImpl(BugReporter& BR, GRExprEngine& Eng) {
Ted Kremenek78d46242008-07-22 16:21:24 +0000464 for (GRExprEngine::undef_branch_iterator I=Eng.undef_branches_begin(),
465 E=Eng.undef_branches_end(); I!=E; ++I) {
466
467 // What's going on here: we want to highlight the subexpression of the
468 // condition that is the most likely source of the "uninitialized
469 // branch condition." We do a recursive walk of the condition's
470 // subexpressions and roughly look for the most nested subexpression
471 // that binds to Undefined. We then highlight that expression's range.
Ted Kremenek78d46242008-07-22 16:21:24 +0000472 BlockEdge B = cast<BlockEdge>((*I)->getLocation());
473 Expr* Ex = cast<Expr>(B.getSrc()->getTerminatorCondition());
474 assert (Ex && "Block must have a terminator.");
475
476 // Get the predecessor node and check if is a PostStmt with the Stmt
477 // being the terminator condition. We want to inspect the state
478 // of that node instead because it will contain main information about
479 // the subexpressions.
Ted Kremenek78d46242008-07-22 16:21:24 +0000480 assert (!(*I)->pred_empty());
481
482 // Note: any predecessor will do. They should have identical state,
483 // since all the BlockEdge did was act as an error sink since the value
484 // had to already be undefined.
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000485 ExplodedNode *N = *(*I)->pred_begin();
Ted Kremenek78d46242008-07-22 16:21:24 +0000486 ProgramPoint P = N->getLocation();
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000487 const GRState* St = (*I)->getState();
Ted Kremenek78d46242008-07-22 16:21:24 +0000488
489 if (PostStmt* PS = dyn_cast<PostStmt>(&P))
490 if (PS->getStmt() == Ex)
491 St = N->getState();
492
493 FindUndefExpr FindIt(Eng.getStateManager(), St);
494 Ex = FindIt.FindExpr(Ex);
495
Ted Kremenek85ac9342009-05-15 05:25:09 +0000496 ArgReport *R = new ArgReport(*this, desc.c_str(), *I, Ex);
Ted Kremenekcf118d42009-02-04 23:49:09 +0000497 R->addRange(Ex->getSourceRange());
498 BR.EmitReport(R);
Ted Kremenek78d46242008-07-22 16:21:24 +0000499 }
500 }
Mike Stump1eb44332009-09-09 15:08:12 +0000501
Ted Kremenek85ac9342009-05-15 05:25:09 +0000502 void registerInitialVisitors(BugReporterContext& BRC,
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000503 const ExplodedNode* N,
Ted Kremenek85ac9342009-05-15 05:25:09 +0000504 BuiltinBugReport *R) {
505 registerTrackNullOrUndefValue(BRC, static_cast<ArgReport*>(R)->getArg(),
506 N);
507 }
Ted Kremenek78d46242008-07-22 16:21:24 +0000508};
509
Zhongxing Xu1c0c2332008-11-23 05:52:28 +0000510class VISIBILITY_HIDDEN OutOfBoundMemoryAccess : public BuiltinBug {
511public:
Ted Kremenekcf118d42009-02-04 23:49:09 +0000512 OutOfBoundMemoryAccess(GRExprEngine* eng)
Ted Kremenek5d88ff82009-04-02 02:40:26 +0000513 : BuiltinBug(eng,"Out-of-bounds memory access",
Ted Kremenekcf118d42009-02-04 23:49:09 +0000514 "Load or store into an out-of-bound memory position.") {}
Zhongxing Xu1c0c2332008-11-23 05:52:28 +0000515
Ted Kremenekcf118d42009-02-04 23:49:09 +0000516 void FlushReportsImpl(BugReporter& BR, GRExprEngine& Eng) {
Zhongxing Xu1c0c2332008-11-23 05:52:28 +0000517 Emit(BR, Eng.explicit_oob_memacc_begin(), Eng.explicit_oob_memacc_end());
518 }
519};
Mike Stump1eb44332009-09-09 15:08:12 +0000520
Ted Kremenek159d2482008-12-09 00:44:16 +0000521class VISIBILITY_HIDDEN BadSizeVLA : public BuiltinBug {
Ted Kremenekefd59942008-12-08 22:47:34 +0000522public:
Ted Kremenek5d88ff82009-04-02 02:40:26 +0000523 BadSizeVLA(GRExprEngine* eng) :
524 BuiltinBug(eng, "Bad variable-length array (VLA) size") {}
Mike Stump1eb44332009-09-09 15:08:12 +0000525
Ted Kremenekcf118d42009-02-04 23:49:09 +0000526 void FlushReportsImpl(BugReporter& BR, GRExprEngine& Eng) {
Ted Kremenekefd59942008-12-08 22:47:34 +0000527 for (GRExprEngine::ErrorNodes::iterator
Ted Kremenek159d2482008-12-09 00:44:16 +0000528 I = Eng.ExplicitBadSizedVLA.begin(),
529 E = Eng.ExplicitBadSizedVLA.end(); I!=E; ++I) {
530
531 // Determine whether this was a 'zero-sized' VLA or a VLA with an
532 // undefined size.
Zhongxing Xu031ccc02009-08-06 12:48:26 +0000533 ExplodedNode* N = *I;
Mike Stump1eb44332009-09-09 15:08:12 +0000534 PostStmt PS = cast<PostStmt>(N->getLocation());
Ted Kremenek5f85e172009-07-22 22:35:28 +0000535 const DeclStmt *DS = cast<DeclStmt>(PS.getStmt());
Ted Kremenekefd59942008-12-08 22:47:34 +0000536 VarDecl* VD = cast<VarDecl>(*DS->decl_begin());
537 QualType T = Eng.getContext().getCanonicalType(VD->getType());
538 VariableArrayType* VT = cast<VariableArrayType>(T);
Ted Kremenek159d2482008-12-09 00:44:16 +0000539 Expr* SizeExpr = VT->getSizeExpr();
Mike Stump1eb44332009-09-09 15:08:12 +0000540
Ted Kremenek159d2482008-12-09 00:44:16 +0000541 std::string buf;
542 llvm::raw_string_ostream os(buf);
Ted Kremenek5d88ff82009-04-02 02:40:26 +0000543 os << "The expression used to specify the number of elements in the "
544 "variable-length array (VLA) '"
Ted Kremenekcf118d42009-02-04 23:49:09 +0000545 << VD->getNameAsString() << "' evaluates to ";
Mike Stump1eb44332009-09-09 15:08:12 +0000546
Ted Kremenek23ec48c2009-06-18 23:58:37 +0000547 bool isUndefined = N->getState()->getSVal(SizeExpr).isUndef();
Mike Stump1eb44332009-09-09 15:08:12 +0000548
Ted Kremenekd49967f2009-04-29 21:58:13 +0000549 if (isUndefined)
Ted Kremenek159d2482008-12-09 00:44:16 +0000550 os << "an undefined or garbage value.";
Ted Kremenekcf118d42009-02-04 23:49:09 +0000551 else
552 os << "0. VLAs with no elements have undefined behavior.";
Mike Stump1eb44332009-09-09 15:08:12 +0000553
Ted Kremenekd49967f2009-04-29 21:58:13 +0000554 std::string shortBuf;
555 llvm::raw_string_ostream os_short(shortBuf);
556 os_short << "Variable-length array '" << VD->getNameAsString() << "' "
Ted Kremenekeaedfea2009-05-10 05:11:21 +0000557 << (isUndefined ? "garbage value for array size"
558 : "has zero elements (undefined behavior)");
Ted Kremenek159d2482008-12-09 00:44:16 +0000559
Ted Kremenek85ac9342009-05-15 05:25:09 +0000560 ArgReport *report = new ArgReport(*this, os_short.str().c_str(),
561 os.str().c_str(), N, SizeExpr);
562
Ted Kremenekcf118d42009-02-04 23:49:09 +0000563 report->addRange(SizeExpr->getSourceRange());
564 BR.EmitReport(report);
Ted Kremenekefd59942008-12-08 22:47:34 +0000565 }
566 }
Mike Stump1eb44332009-09-09 15:08:12 +0000567
Ted Kremenek85ac9342009-05-15 05:25:09 +0000568 void registerInitialVisitors(BugReporterContext& BRC,
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000569 const ExplodedNode* N,
Ted Kremenek85ac9342009-05-15 05:25:09 +0000570 BuiltinBugReport *R) {
571 registerTrackNullOrUndefValue(BRC, static_cast<ArgReport*>(R)->getArg(),
572 N);
573 }
Ted Kremenekefd59942008-12-08 22:47:34 +0000574};
Zhongxing Xu1c0c2332008-11-23 05:52:28 +0000575
Ted Kremenek78d46242008-07-22 16:21:24 +0000576//===----------------------------------------------------------------------===//
577// __attribute__(nonnull) checking
578
Ted Kremenekc26a8b02009-07-22 21:46:56 +0000579class VISIBILITY_HIDDEN CheckAttrNonNull :
580 public CheckerVisitor<CheckAttrNonNull> {
581
Ted Kremenekcf118d42009-02-04 23:49:09 +0000582 BugType *BT;
Mike Stump1eb44332009-09-09 15:08:12 +0000583
Ted Kremenek78d46242008-07-22 16:21:24 +0000584public:
Ted Kremenekc26a8b02009-07-22 21:46:56 +0000585 CheckAttrNonNull() : BT(0) {}
Ted Kremenek31112182009-07-24 00:40:31 +0000586 ~CheckAttrNonNull() {}
Ted Kremenek78d46242008-07-22 16:21:24 +0000587
Ted Kremenekc26a8b02009-07-22 21:46:56 +0000588 const void *getTag() {
589 static int x = 0;
590 return &x;
591 }
592
593 void PreVisitCallExpr(CheckerContext &C, const CallExpr *CE) {
594 const GRState *state = C.getState();
595 const GRState *originalState = state;
Mike Stump1eb44332009-09-09 15:08:12 +0000596
Ted Kremenekc26a8b02009-07-22 21:46:56 +0000597 // Check if the callee has a 'nonnull' attribute.
Ted Kremenek23ec48c2009-06-18 23:58:37 +0000598 SVal X = state->getSVal(CE->getCallee());
Mike Stump1eb44332009-09-09 15:08:12 +0000599
Zhongxing Xu369f4472009-04-20 05:24:46 +0000600 const FunctionDecl* FD = X.getAsFunctionDecl();
601 if (!FD)
Ted Kremenekc26a8b02009-07-22 21:46:56 +0000602 return;
Zhongxing Xu369f4472009-04-20 05:24:46 +0000603
Mike Stump1eb44332009-09-09 15:08:12 +0000604 const NonNullAttr* Att = FD->getAttr<NonNullAttr>();
Ted Kremenek78d46242008-07-22 16:21:24 +0000605 if (!Att)
Ted Kremenekc26a8b02009-07-22 21:46:56 +0000606 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000607
Ted Kremenek78d46242008-07-22 16:21:24 +0000608 // Iterate through the arguments of CE and check them for null.
Ted Kremenek78d46242008-07-22 16:21:24 +0000609 unsigned idx = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000610
Ted Kremenekc26a8b02009-07-22 21:46:56 +0000611 for (CallExpr::const_arg_iterator I=CE->arg_begin(), E=CE->arg_end(); I!=E;
Ted Kremenek78d46242008-07-22 16:21:24 +0000612 ++I, ++idx) {
Mike Stump1eb44332009-09-09 15:08:12 +0000613
Ted Kremenekc26a8b02009-07-22 21:46:56 +0000614 if (!Att->isNonNull(idx))
615 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000616
Ted Kremenek08780072009-08-24 22:47:34 +0000617 const SVal &V = state->getSVal(*I);
618 const DefinedSVal *DV = dyn_cast<DefinedSVal>(&V);
Mike Stump1eb44332009-09-09 15:08:12 +0000619
Ted Kremenek08780072009-08-24 22:47:34 +0000620 if (!DV)
621 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000622
Ted Kremenekc26a8b02009-07-22 21:46:56 +0000623 ConstraintManager &CM = C.getConstraintManager();
624 const GRState *stateNotNull, *stateNull;
Ted Kremenek08780072009-08-24 22:47:34 +0000625 llvm::tie(stateNotNull, stateNull) = CM.AssumeDual(state, *DV);
Mike Stump1eb44332009-09-09 15:08:12 +0000626
Ted Kremenekc26a8b02009-07-22 21:46:56 +0000627 if (stateNull && !stateNotNull) {
628 // Generate an error node. Check for a null node in case
629 // we cache out.
Zhongxing Xu6403b572009-09-02 13:26:26 +0000630 if (ExplodedNode *errorNode = C.GenerateNode(CE, stateNull, true)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000631
Ted Kremenekc26a8b02009-07-22 21:46:56 +0000632 // Lazily allocate the BugType object if it hasn't already been
633 // created. Ownership is transferred to the BugReporter object once
634 // the BugReport is passed to 'EmitWarning'.
635 if (!BT)
636 BT = new BugType("Argument with 'nonnull' attribute passed null",
637 "API");
Mike Stump1eb44332009-09-09 15:08:12 +0000638
Ted Kremenek592362b2009-08-18 01:05:30 +0000639 EnhancedBugReport *R =
640 new EnhancedBugReport(*BT,
641 "Null pointer passed as an argument to a "
642 "'nonnull' parameter", errorNode);
Mike Stump1eb44332009-09-09 15:08:12 +0000643
Ted Kremenekc26a8b02009-07-22 21:46:56 +0000644 // Highlight the range of the argument that was null.
Ted Kremenek592362b2009-08-18 01:05:30 +0000645 const Expr *arg = *I;
646 R->addRange(arg->getSourceRange());
647 R->addVisitorCreator(registerTrackNullOrUndefValue, arg);
Mike Stump1eb44332009-09-09 15:08:12 +0000648
Ted Kremenekc26a8b02009-07-22 21:46:56 +0000649 // Emit the bug report.
650 C.EmitReport(R);
651 }
Mike Stump1eb44332009-09-09 15:08:12 +0000652
Ted Kremenekc26a8b02009-07-22 21:46:56 +0000653 // Always return. Either we cached out or we just emitted an error.
654 return;
655 }
Mike Stump1eb44332009-09-09 15:08:12 +0000656
Ted Kremenekc26a8b02009-07-22 21:46:56 +0000657 // If a pointer value passed the check we should assume that it is
658 // indeed not null from this point forward.
659 assert(stateNotNull);
660 state = stateNotNull;
661 }
Mike Stump1eb44332009-09-09 15:08:12 +0000662
Ted Kremenekc26a8b02009-07-22 21:46:56 +0000663 // If we reach here all of the arguments passed the nonnull check.
664 // If 'state' has been updated generated a new node.
665 if (state != originalState)
Zhongxing Xu6403b572009-09-02 13:26:26 +0000666 C.addTransition(C.GenerateNode(CE, state));
Ted Kremenek78d46242008-07-22 16:21:24 +0000667 }
Ted Kremenek78d46242008-07-22 16:21:24 +0000668};
669} // end anonymous namespace
670
Zhongxing Xu9a5bca32009-08-29 02:11:01 +0000671// Undefined arguments checking.
672namespace {
Mike Stump1eb44332009-09-09 15:08:12 +0000673class VISIBILITY_HIDDEN CheckUndefinedArg
Zhongxing Xu9a5bca32009-08-29 02:11:01 +0000674 : public CheckerVisitor<CheckUndefinedArg> {
675
Zhongxing Xu904e1e32009-09-02 07:09:39 +0000676 BadArg *BT;
Zhongxing Xu9a5bca32009-08-29 02:11:01 +0000677
678public:
679 CheckUndefinedArg() : BT(0) {}
680 ~CheckUndefinedArg() {}
681
682 const void *getTag() {
683 static int x = 0;
684 return &x;
685 }
686
687 void PreVisitCallExpr(CheckerContext &C, const CallExpr *CE);
688};
689
690void CheckUndefinedArg::PreVisitCallExpr(CheckerContext &C, const CallExpr *CE){
691 for (CallExpr::const_arg_iterator I = CE->arg_begin(), E = CE->arg_end();
692 I != E; ++I) {
693 if (C.getState()->getSVal(*I).isUndef()) {
Zhongxing Xu6403b572009-09-02 13:26:26 +0000694 if (ExplodedNode *ErrorNode = C.GenerateNode(CE, true)) {
Zhongxing Xu9a5bca32009-08-29 02:11:01 +0000695 if (!BT)
Zhongxing Xu904e1e32009-09-02 07:09:39 +0000696 BT = new BadArg();
Zhongxing Xu9a5bca32009-08-29 02:11:01 +0000697 // Generate a report for this bug.
Zhongxing Xu904e1e32009-09-02 07:09:39 +0000698 ArgReport *Report = new ArgReport(*BT, BT->getDescription().c_str(),
Zhongxing Xu9a5bca32009-08-29 02:11:01 +0000699 ErrorNode, *I);
700 Report->addRange((*I)->getSourceRange());
701 C.EmitReport(Report);
702 }
703 }
704 }
705}
706
Zhongxing Xud99f3612009-09-02 08:10:35 +0000707class VISIBILITY_HIDDEN CheckBadCall : public CheckerVisitor<CheckBadCall> {
708 BadCall *BT;
709
710public:
711 CheckBadCall() : BT(0) {}
712 ~CheckBadCall() {}
713
714 const void *getTag() {
715 static int x = 0;
716 return &x;
717 }
718
719 void PreVisitCallExpr(CheckerContext &C, const CallExpr *CE);
720};
721
722void CheckBadCall::PreVisitCallExpr(CheckerContext &C, const CallExpr *CE) {
723 const Expr *Callee = CE->getCallee()->IgnoreParens();
724 SVal L = C.getState()->getSVal(Callee);
725
726 if (L.isUndef() || isa<loc::ConcreteInt>(L)) {
Zhongxing Xu6403b572009-09-02 13:26:26 +0000727 if (ExplodedNode *N = C.GenerateNode(CE, true)) {
Zhongxing Xud99f3612009-09-02 08:10:35 +0000728 if (!BT)
729 BT = new BadCall();
730 C.EmitReport(new BuiltinBugReport(*BT, BT->getDescription().c_str(), N));
731 }
732 }
733}
734
Zhongxing Xu6403b572009-09-02 13:26:26 +0000735class VISIBILITY_HIDDEN CheckBadDiv : public CheckerVisitor<CheckBadDiv> {
736 DivZero *BT;
737public:
738 CheckBadDiv() : BT(0) {}
739 ~CheckBadDiv() {}
740
741 const void *getTag() {
742 static int x;
743 return &x;
744 }
745
746 void PreVisitBinaryOperator(CheckerContext &C, const BinaryOperator *B);
747};
748
Mike Stump1eb44332009-09-09 15:08:12 +0000749void CheckBadDiv::PreVisitBinaryOperator(CheckerContext &C,
Zhongxing Xu6403b572009-09-02 13:26:26 +0000750 const BinaryOperator *B) {
751 BinaryOperator::Opcode Op = B->getOpcode();
752 if (Op != BinaryOperator::Div &&
753 Op != BinaryOperator::Rem &&
754 Op != BinaryOperator::DivAssign &&
755 Op != BinaryOperator::RemAssign)
756 return;
757
758 if (!B->getRHS()->getType()->isIntegerType() ||
759 !B->getRHS()->getType()->isScalarType())
760 return;
761
Zhongxing Xu6403b572009-09-02 13:26:26 +0000762 // Check for divide by undefined.
763 SVal Denom = C.getState()->getSVal(B->getRHS());
764
765 if (Denom.isUndef()) {
766 if (ExplodedNode *N = C.GenerateNode(B, true)) {
767 if (!BT)
768 BT = new DivZero();
769
770 C.EmitReport(new BuiltinBugReport(*BT, BT->getDescription().c_str(), N));
771 }
772 return;
773 }
774
Ted Kremenek970e03a2009-09-03 01:48:03 +0000775 // Handle the case where 'Denom' is UnknownVal.
776 const DefinedSVal *DV = dyn_cast<DefinedSVal>(&Denom);
777
Mike Stump1eb44332009-09-09 15:08:12 +0000778 if (!DV)
Ted Kremenek970e03a2009-09-03 01:48:03 +0000779 return;
780
Zhongxing Xu6403b572009-09-02 13:26:26 +0000781 // Check for divide by zero.
782 ConstraintManager &CM = C.getConstraintManager();
783 const GRState *stateNotZero, *stateZero;
Ted Kremenek970e03a2009-09-03 01:48:03 +0000784 llvm::tie(stateNotZero, stateZero) = CM.AssumeDual(C.getState(), *DV);
Mike Stump1eb44332009-09-09 15:08:12 +0000785
Zhongxing Xu6403b572009-09-02 13:26:26 +0000786 if (stateZero && !stateNotZero) {
787 if (ExplodedNode *N = C.GenerateNode(B, stateZero, true)) {
788 if (!BT)
789 BT = new DivZero();
Mike Stump1eb44332009-09-09 15:08:12 +0000790
Zhongxing Xu6403b572009-09-02 13:26:26 +0000791 C.EmitReport(new BuiltinBugReport(*BT, BT->getDescription().c_str(), N));
792 }
793 return;
794 }
795
796 // If we get here, then the denom should not be zero.
797 if (stateNotZero != C.getState())
798 C.addTransition(C.GenerateNode(B, stateNotZero));
799}
Zhongxing Xu9a5bca32009-08-29 02:11:01 +0000800}
Ted Kremenek78d46242008-07-22 16:21:24 +0000801//===----------------------------------------------------------------------===//
802// Check registration.
Ted Kremenekcf118d42009-02-04 23:49:09 +0000803//===----------------------------------------------------------------------===//
Ted Kremenek78d46242008-07-22 16:21:24 +0000804
805void GRExprEngine::RegisterInternalChecks() {
Ted Kremenekcf118d42009-02-04 23:49:09 +0000806 // Register internal "built-in" BugTypes with the BugReporter. These BugTypes
807 // are different than what probably many checks will do since they don't
808 // create BugReports on-the-fly but instead wait until GRExprEngine finishes
809 // analyzing a function. Generation of BugReport objects is done via a call
810 // to 'FlushReports' from BugReporter.
811 BR.Register(new NullDeref(this));
812 BR.Register(new UndefinedDeref(this));
813 BR.Register(new UndefBranch(this));
Ted Kremenekcf118d42009-02-04 23:49:09 +0000814 BR.Register(new UndefResult(this));
Ted Kremenekcf118d42009-02-04 23:49:09 +0000815 BR.Register(new RetStack(this));
816 BR.Register(new RetUndef(this));
Ted Kremenekcf118d42009-02-04 23:49:09 +0000817 BR.Register(new BadMsgExprArg(this));
818 BR.Register(new BadReceiver(this));
819 BR.Register(new OutOfBoundMemoryAccess(this));
820 BR.Register(new BadSizeVLA(this));
Ted Kremenek21fe8372009-02-19 04:06:22 +0000821 BR.Register(new NilReceiverStructRet(this));
Ted Kremenek899b3de2009-04-08 03:07:17 +0000822 BR.Register(new NilReceiverLargerThanVoidPtrRet(this));
Mike Stump1eb44332009-09-09 15:08:12 +0000823
Ted Kremenekcf118d42009-02-04 23:49:09 +0000824 // The following checks do not need to have their associated BugTypes
825 // explicitly registered with the BugReporter. If they issue any BugReports,
826 // their associated BugType will get registered with the BugReporter
827 // automatically. Note that the check itself is owned by the GRExprEngine
828 // object.
Ted Kremenekc26a8b02009-07-22 21:46:56 +0000829 registerCheck(new CheckAttrNonNull());
Zhongxing Xu9a5bca32009-08-29 02:11:01 +0000830 registerCheck(new CheckUndefinedArg());
Zhongxing Xud99f3612009-09-02 08:10:35 +0000831 registerCheck(new CheckBadCall());
Zhongxing Xu6403b572009-09-02 13:26:26 +0000832 registerCheck(new CheckBadDiv());
Ted Kremenek78d46242008-07-22 16:21:24 +0000833}