blob: 21f146520479aa17f5fa2391b868f8a0d40c6a6f [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();
Ted Kremenek24c411b2009-09-15 17:43:54 +0000225 const Expr *Ex = NULL;
Ted Kremenek5b9bd212009-09-11 22:07:28 +0000226
227 if (ST->getSVal(B->getLHS()).isUndef()) {
228 Ex = B->getLHS()->IgnoreParenCasts();
229 OS << "The left operand of the '";
230 }
Ted Kremenek24c411b2009-09-15 17:43:54 +0000231 else if (ST->getSVal(B->getRHS()).isUndef()) {
Ted Kremenek5b9bd212009-09-11 22:07:28 +0000232 Ex = B->getRHS()->IgnoreParenCasts();
233 OS << "The right operand of the '";
234 }
Ted Kremenek24c411b2009-09-15 17:43:54 +0000235
236 if (Ex) {
237 OS << BinaryOperator::getOpcodeStr(B->getOpcode())
238 << "' expression is an undefined "
239 "or otherwise garbage value";
240 }
241 else {
242 // We KNOW that the result was undefined.
243 OS << "The result of the '"
244 << BinaryOperator::getOpcodeStr(B->getOpcode())
245 << "' expression is undefined";
246 }
247
Ted Kremenek5b9bd212009-09-11 22:07:28 +0000248 // FIXME: Use StringRefs to pass string information.
249 report = new BuiltinBugReport(*this, OS.str().str().c_str(), N);
Ted Kremenek24c411b2009-09-15 17:43:54 +0000250 if (Ex) report->addRange(Ex->getSourceRange());
Ted Kremenek5b9bd212009-09-11 22:07:28 +0000251 }
252 else {
253 report = new BuiltinBugReport(*this,
254 "Expression evaluates to an uninitialized"
255 " or undefined value", N);
256 }
257
258 BR.EmitReport(report);
259 }
260 }
261
262 void registerInitialVisitors(BugReporterContext& BRC,
263 const ExplodedNode* N,
264 BuiltinBugReport *R) {
265
266 const Stmt *S = N->getLocationAs<StmtPoint>()->getStmt();
267 const Stmt *X = S;
268
269 if (const BinaryOperator *B = dyn_cast<BinaryOperator>(S)) {
270 const GRState *ST = N->getState();
271 X = ST->getSVal(B->getLHS()).isUndef()
272 ? B->getLHS()->IgnoreParenCasts() : B->getRHS()->IgnoreParenCasts();
273 }
274
275 registerTrackNullOrUndefValue(BRC, X, N);
Ted Kremenek78d46242008-07-22 16:21:24 +0000276 }
277};
Mike Stump1eb44332009-09-09 15:08:12 +0000278
Ted Kremenek78d46242008-07-22 16:21:24 +0000279class VISIBILITY_HIDDEN BadCall : public BuiltinBug {
280public:
Zhongxing Xud99f3612009-09-02 08:10:35 +0000281 BadCall(GRExprEngine *eng = 0)
Ted Kremenek5d88ff82009-04-02 02:40:26 +0000282 : BuiltinBug(eng, "Invalid function call",
Ted Kremenek17a8e072009-03-01 05:43:22 +0000283 "Called function pointer is a null or undefined pointer value") {}
Mike Stump1eb44332009-09-09 15:08:12 +0000284
Ted Kremenek85ac9342009-05-15 05:25:09 +0000285 void registerInitialVisitors(BugReporterContext& BRC,
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000286 const ExplodedNode* N,
Ted Kremenek85ac9342009-05-15 05:25:09 +0000287 BuiltinBugReport *R) {
288 registerTrackNullOrUndefValue(BRC, GetCalleeExpr(N), N);
289 }
290};
291
292
293class VISIBILITY_HIDDEN ArgReport : public BuiltinBugReport {
294 const Stmt *Arg;
295public:
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000296 ArgReport(BugType& bt, const char* desc, ExplodedNode *n,
Ted Kremenek85ac9342009-05-15 05:25:09 +0000297 const Stmt *arg)
298 : BuiltinBugReport(bt, desc, n), Arg(arg) {}
Mike Stump1eb44332009-09-09 15:08:12 +0000299
Ted Kremenek85ac9342009-05-15 05:25:09 +0000300 ArgReport(BugType& bt, const char *shortDesc, const char *desc,
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000301 ExplodedNode *n, const Stmt *arg)
Mike Stump1eb44332009-09-09 15:08:12 +0000302 : BuiltinBugReport(bt, shortDesc, desc, n), Arg(arg) {}
303
304 const Stmt *getArg() const { return Arg; }
Ted Kremenek78d46242008-07-22 16:21:24 +0000305};
306
307class VISIBILITY_HIDDEN BadArg : public BuiltinBug {
Mike Stump1eb44332009-09-09 15:08:12 +0000308public:
309 BadArg(GRExprEngine* eng=0) : BuiltinBug(eng,"Uninitialized argument",
Ted Kremenek5b9bd212009-09-11 22:07:28 +0000310 "Pass-by-value argument in function call is undefined") {}
Ted Kremenek78d46242008-07-22 16:21:24 +0000311
Ted Kremenekcf118d42009-02-04 23:49:09 +0000312 BadArg(GRExprEngine* eng, const char* d)
Ted Kremenek5d88ff82009-04-02 02:40:26 +0000313 : BuiltinBug(eng,"Uninitialized argument", d) {}
Mike Stump1eb44332009-09-09 15:08:12 +0000314
Ted Kremenek85ac9342009-05-15 05:25:09 +0000315 void registerInitialVisitors(BugReporterContext& BRC,
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000316 const ExplodedNode* N,
Ted Kremenek85ac9342009-05-15 05:25:09 +0000317 BuiltinBugReport *R) {
318 registerTrackNullOrUndefValue(BRC, static_cast<ArgReport*>(R)->getArg(),
319 N);
Mike Stump1eb44332009-09-09 15:08:12 +0000320 }
Ted Kremenek78d46242008-07-22 16:21:24 +0000321};
Mike Stump1eb44332009-09-09 15:08:12 +0000322
Ted Kremenek78d46242008-07-22 16:21:24 +0000323class VISIBILITY_HIDDEN BadMsgExprArg : public BadArg {
324public:
Mike Stump1eb44332009-09-09 15:08:12 +0000325 BadMsgExprArg(GRExprEngine* eng)
Ted Kremenek5d88ff82009-04-02 02:40:26 +0000326 : BadArg(eng,"Pass-by-value argument in message expression is undefined"){}
Mike Stump1eb44332009-09-09 15:08:12 +0000327
Ted Kremenekcf118d42009-02-04 23:49:09 +0000328 void FlushReportsImpl(BugReporter& BR, GRExprEngine& Eng) {
Ted Kremenek78d46242008-07-22 16:21:24 +0000329 for (GRExprEngine::UndefArgsTy::iterator I=Eng.msg_expr_undef_arg_begin(),
Mike Stump1eb44332009-09-09 15:08:12 +0000330 E = Eng.msg_expr_undef_arg_end(); I!=E; ++I) {
Ted Kremenek78d46242008-07-22 16:21:24 +0000331 // Generate a report for this bug.
Ted Kremenek85ac9342009-05-15 05:25:09 +0000332 ArgReport *report = new ArgReport(*this, desc.c_str(), I->first,
333 I->second);
Ted Kremenekcf118d42009-02-04 23:49:09 +0000334 report->addRange(I->second->getSourceRange());
335 BR.EmitReport(report);
Mike Stump1eb44332009-09-09 15:08:12 +0000336 }
337 }
Ted Kremenek78d46242008-07-22 16:21:24 +0000338};
Mike Stump1eb44332009-09-09 15:08:12 +0000339
Ted Kremenek78d46242008-07-22 16:21:24 +0000340class VISIBILITY_HIDDEN BadReceiver : public BuiltinBug {
Mike Stump1eb44332009-09-09 15:08:12 +0000341public:
Ted Kremenekcf118d42009-02-04 23:49:09 +0000342 BadReceiver(GRExprEngine* eng)
Ted Kremenek5d88ff82009-04-02 02:40:26 +0000343 : BuiltinBug(eng,"Uninitialized receiver",
344 "Receiver in message expression is an uninitialized value") {}
Mike Stump1eb44332009-09-09 15:08:12 +0000345
Ted Kremenekcf118d42009-02-04 23:49:09 +0000346 void FlushReportsImpl(BugReporter& BR, GRExprEngine& Eng) {
Ted Kremenekefd59942008-12-08 22:47:34 +0000347 for (GRExprEngine::ErrorNodes::iterator I=Eng.undef_receivers_begin(),
Ted Kremenek78d46242008-07-22 16:21:24 +0000348 End = Eng.undef_receivers_end(); I!=End; ++I) {
Mike Stump1eb44332009-09-09 15:08:12 +0000349
Ted Kremenek78d46242008-07-22 16:21:24 +0000350 // Generate a report for this bug.
Ted Kremenek0c313172009-05-13 19:16:35 +0000351 BuiltinBugReport *report = new BuiltinBugReport(*this, desc.c_str(), *I);
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000352 ExplodedNode* N = *I;
Ted Kremenek5f85e172009-07-22 22:35:28 +0000353 const Stmt *S = cast<PostStmt>(N->getLocation()).getStmt();
354 const Expr* E = cast<ObjCMessageExpr>(S)->getReceiver();
Ted Kremenek78d46242008-07-22 16:21:24 +0000355 assert (E && "Receiver cannot be NULL");
Ted Kremenekcf118d42009-02-04 23:49:09 +0000356 report->addRange(E->getSourceRange());
357 BR.EmitReport(report);
Ted Kremenek0c313172009-05-13 19:16:35 +0000358 }
359 }
Ted Kremenek85ac9342009-05-15 05:25:09 +0000360
Ted Kremenek0c313172009-05-13 19:16:35 +0000361 void registerInitialVisitors(BugReporterContext& BRC,
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000362 const ExplodedNode* N,
Ted Kremenek0c313172009-05-13 19:16:35 +0000363 BuiltinBugReport *R) {
Ted Kremenek85ac9342009-05-15 05:25:09 +0000364 registerTrackNullOrUndefValue(BRC, GetReceiverExpr(N), N);
Mike Stump1eb44332009-09-09 15:08:12 +0000365 }
Ted Kremenek78d46242008-07-22 16:21:24 +0000366};
Ted Kremenek5917d782008-11-21 00:27:44 +0000367
Ted Kremenek78d46242008-07-22 16:21:24 +0000368class VISIBILITY_HIDDEN RetStack : public BuiltinBug {
369public:
Ted Kremenek17a8e072009-03-01 05:43:22 +0000370 RetStack(GRExprEngine* eng)
Ted Kremenek5d88ff82009-04-02 02:40:26 +0000371 : BuiltinBug(eng, "Return of address to stack-allocated memory") {}
Mike Stump1eb44332009-09-09 15:08:12 +0000372
Ted Kremenekcf118d42009-02-04 23:49:09 +0000373 void FlushReportsImpl(BugReporter& BR, GRExprEngine& Eng) {
Ted Kremenekb7714b22008-07-30 17:49:12 +0000374 for (GRExprEngine::ret_stackaddr_iterator I=Eng.ret_stackaddr_begin(),
375 End = Eng.ret_stackaddr_end(); I!=End; ++I) {
Ted Kremenek22bda882008-07-31 20:31:27 +0000376
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000377 ExplodedNode* N = *I;
Ted Kremenek5f85e172009-07-22 22:35:28 +0000378 const Stmt *S = cast<PostStmt>(N->getLocation()).getStmt();
379 const Expr* E = cast<ReturnStmt>(S)->getRetValue();
380 assert(E && "Return expression cannot be NULL");
Mike Stump1eb44332009-09-09 15:08:12 +0000381
Ted Kremenek22bda882008-07-31 20:31:27 +0000382 // Get the value associated with E.
Ted Kremenek23ec48c2009-06-18 23:58:37 +0000383 loc::MemRegionVal V = cast<loc::MemRegionVal>(N->getState()->getSVal(E));
Mike Stump1eb44332009-09-09 15:08:12 +0000384
Ted Kremenek22bda882008-07-31 20:31:27 +0000385 // Generate a report for this bug.
Ted Kremenekad51a602008-10-31 00:18:30 +0000386 std::string buf;
387 llvm::raw_string_ostream os(buf);
Ted Kremenek8aed8062008-10-31 00:13:20 +0000388 SourceRange R;
Mike Stump1eb44332009-09-09 15:08:12 +0000389
Ted Kremenek8aed8062008-10-31 00:13:20 +0000390 // Check if the region is a compound literal.
Mike Stump1eb44332009-09-09 15:08:12 +0000391 if (const CompoundLiteralRegion* CR =
Ted Kremenek8aed8062008-10-31 00:13:20 +0000392 dyn_cast<CompoundLiteralRegion>(V.getRegion())) {
Mike Stump1eb44332009-09-09 15:08:12 +0000393
Ted Kremenek8aed8062008-10-31 00:13:20 +0000394 const CompoundLiteralExpr* CL = CR->getLiteralExpr();
395 os << "Address of stack memory associated with a compound literal "
396 "declared on line "
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000397 << BR.getSourceManager()
398 .getInstantiationLineNumber(CL->getLocStart())
Ted Kremenek8aed8062008-10-31 00:13:20 +0000399 << " returned.";
Mike Stump1eb44332009-09-09 15:08:12 +0000400
Ted Kremenek8aed8062008-10-31 00:13:20 +0000401 R = CL->getSourceRange();
402 }
Ted Kremenekde8cd192008-11-02 00:35:25 +0000403 else if (const AllocaRegion* AR = dyn_cast<AllocaRegion>(V.getRegion())) {
404 const Expr* ARE = AR->getExpr();
405 SourceLocation L = ARE->getLocStart();
406 R = ARE->getSourceRange();
Mike Stump1eb44332009-09-09 15:08:12 +0000407
Ted Kremenekde8cd192008-11-02 00:35:25 +0000408 os << "Address of stack memory allocated by call to alloca() on line "
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000409 << BR.getSourceManager().getInstantiationLineNumber(L)
Ted Kremenekde8cd192008-11-02 00:35:25 +0000410 << " returned.";
Mike Stump1eb44332009-09-09 15:08:12 +0000411 }
412 else {
Ted Kremenek8aed8062008-10-31 00:13:20 +0000413 os << "Address of stack memory associated with local variable '"
414 << V.getRegion()->getString() << "' returned.";
415 }
Mike Stump1eb44332009-09-09 15:08:12 +0000416
Ted Kremenekcf118d42009-02-04 23:49:09 +0000417 RangedBugReport *report = new RangedBugReport(*this, os.str().c_str(), N);
418 report->addRange(E->getSourceRange());
419 if (R.isValid()) report->addRange(R);
420 BR.EmitReport(report);
Ted Kremenekb7714b22008-07-30 17:49:12 +0000421 }
Ted Kremenek78d46242008-07-22 16:21:24 +0000422 }
423};
Mike Stump1eb44332009-09-09 15:08:12 +0000424
Ted Kremenek5917d782008-11-21 00:27:44 +0000425class VISIBILITY_HIDDEN RetUndef : public BuiltinBug {
426public:
Ted Kremenek5b9bd212009-09-11 22:07:28 +0000427 RetUndef(GRExprEngine* eng) : BuiltinBug(eng, "Garbage return value",
428 "Undefined or garbage value returned to caller") {}
Mike Stump1eb44332009-09-09 15:08:12 +0000429
Ted Kremenekcf118d42009-02-04 23:49:09 +0000430 void FlushReportsImpl(BugReporter& BR, GRExprEngine& Eng) {
Ted Kremenek5917d782008-11-21 00:27:44 +0000431 Emit(BR, Eng.ret_undef_begin(), Eng.ret_undef_end());
432 }
Mike Stump1eb44332009-09-09 15:08:12 +0000433
Ted Kremenek0c313172009-05-13 19:16:35 +0000434 void registerInitialVisitors(BugReporterContext& BRC,
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000435 const ExplodedNode* N,
Ted Kremenek0c313172009-05-13 19:16:35 +0000436 BuiltinBugReport *R) {
Ted Kremenek85ac9342009-05-15 05:25:09 +0000437 registerTrackNullOrUndefValue(BRC, GetRetValExpr(N), N);
Mike Stump1eb44332009-09-09 15:08:12 +0000438 }
Ted Kremenek5917d782008-11-21 00:27:44 +0000439};
Ted Kremenek78d46242008-07-22 16:21:24 +0000440
441class VISIBILITY_HIDDEN UndefBranch : public BuiltinBug {
442 struct VISIBILITY_HIDDEN FindUndefExpr {
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000443 GRStateManager& VM;
444 const GRState* St;
Mike Stump1eb44332009-09-09 15:08:12 +0000445
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000446 FindUndefExpr(GRStateManager& V, const GRState* S) : VM(V), St(S) {}
Mike Stump1eb44332009-09-09 15:08:12 +0000447
448 Expr* FindExpr(Expr* Ex) {
Ted Kremenek78d46242008-07-22 16:21:24 +0000449 if (!MatchesCriteria(Ex))
Ted Kremenekb7714b22008-07-30 17:49:12 +0000450 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000451
Ted Kremenekb7714b22008-07-30 17:49:12 +0000452 for (Stmt::child_iterator I=Ex->child_begin(), E=Ex->child_end();I!=E;++I)
Ted Kremenek78d46242008-07-22 16:21:24 +0000453 if (Expr* ExI = dyn_cast_or_null<Expr>(*I)) {
454 Expr* E2 = FindExpr(ExI);
455 if (E2) return E2;
456 }
Mike Stump1eb44332009-09-09 15:08:12 +0000457
Ted Kremenek78d46242008-07-22 16:21:24 +0000458 return Ex;
459 }
Mike Stump1eb44332009-09-09 15:08:12 +0000460
Ted Kremenek23ec48c2009-06-18 23:58:37 +0000461 bool MatchesCriteria(Expr* Ex) { return St->getSVal(Ex).isUndef(); }
Ted Kremenek78d46242008-07-22 16:21:24 +0000462 };
Mike Stump1eb44332009-09-09 15:08:12 +0000463
Ted Kremenek78d46242008-07-22 16:21:24 +0000464public:
Ted Kremenekcf118d42009-02-04 23:49:09 +0000465 UndefBranch(GRExprEngine *eng)
Ted Kremenek5b9bd212009-09-11 22:07:28 +0000466 : BuiltinBug(eng,"Use of garbage value",
467 "Branch condition evaluates to an undefined or garbage value")
468 {}
Mike Stump1eb44332009-09-09 15:08:12 +0000469
Ted Kremenekcf118d42009-02-04 23:49:09 +0000470 void FlushReportsImpl(BugReporter& BR, GRExprEngine& Eng) {
Ted Kremenek78d46242008-07-22 16:21:24 +0000471 for (GRExprEngine::undef_branch_iterator I=Eng.undef_branches_begin(),
472 E=Eng.undef_branches_end(); I!=E; ++I) {
473
474 // What's going on here: we want to highlight the subexpression of the
475 // condition that is the most likely source of the "uninitialized
476 // branch condition." We do a recursive walk of the condition's
477 // subexpressions and roughly look for the most nested subexpression
478 // that binds to Undefined. We then highlight that expression's range.
Ted Kremenek78d46242008-07-22 16:21:24 +0000479 BlockEdge B = cast<BlockEdge>((*I)->getLocation());
480 Expr* Ex = cast<Expr>(B.getSrc()->getTerminatorCondition());
481 assert (Ex && "Block must have a terminator.");
482
483 // Get the predecessor node and check if is a PostStmt with the Stmt
484 // being the terminator condition. We want to inspect the state
485 // of that node instead because it will contain main information about
486 // the subexpressions.
Ted Kremenek78d46242008-07-22 16:21:24 +0000487 assert (!(*I)->pred_empty());
488
489 // Note: any predecessor will do. They should have identical state,
490 // since all the BlockEdge did was act as an error sink since the value
491 // had to already be undefined.
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000492 ExplodedNode *N = *(*I)->pred_begin();
Ted Kremenek78d46242008-07-22 16:21:24 +0000493 ProgramPoint P = N->getLocation();
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000494 const GRState* St = (*I)->getState();
Ted Kremenek78d46242008-07-22 16:21:24 +0000495
496 if (PostStmt* PS = dyn_cast<PostStmt>(&P))
497 if (PS->getStmt() == Ex)
498 St = N->getState();
499
500 FindUndefExpr FindIt(Eng.getStateManager(), St);
501 Ex = FindIt.FindExpr(Ex);
502
Ted Kremenek85ac9342009-05-15 05:25:09 +0000503 ArgReport *R = new ArgReport(*this, desc.c_str(), *I, Ex);
Ted Kremenekcf118d42009-02-04 23:49:09 +0000504 R->addRange(Ex->getSourceRange());
505 BR.EmitReport(R);
Ted Kremenek78d46242008-07-22 16:21:24 +0000506 }
507 }
Mike Stump1eb44332009-09-09 15:08:12 +0000508
Ted Kremenek85ac9342009-05-15 05:25:09 +0000509 void registerInitialVisitors(BugReporterContext& BRC,
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000510 const ExplodedNode* N,
Ted Kremenek85ac9342009-05-15 05:25:09 +0000511 BuiltinBugReport *R) {
512 registerTrackNullOrUndefValue(BRC, static_cast<ArgReport*>(R)->getArg(),
513 N);
514 }
Ted Kremenek78d46242008-07-22 16:21:24 +0000515};
516
Zhongxing Xu1c0c2332008-11-23 05:52:28 +0000517class VISIBILITY_HIDDEN OutOfBoundMemoryAccess : public BuiltinBug {
518public:
Ted Kremenekcf118d42009-02-04 23:49:09 +0000519 OutOfBoundMemoryAccess(GRExprEngine* eng)
Ted Kremenek5d88ff82009-04-02 02:40:26 +0000520 : BuiltinBug(eng,"Out-of-bounds memory access",
Ted Kremenekcf118d42009-02-04 23:49:09 +0000521 "Load or store into an out-of-bound memory position.") {}
Zhongxing Xu1c0c2332008-11-23 05:52:28 +0000522
Ted Kremenekcf118d42009-02-04 23:49:09 +0000523 void FlushReportsImpl(BugReporter& BR, GRExprEngine& Eng) {
Zhongxing Xu1c0c2332008-11-23 05:52:28 +0000524 Emit(BR, Eng.explicit_oob_memacc_begin(), Eng.explicit_oob_memacc_end());
525 }
526};
Mike Stump1eb44332009-09-09 15:08:12 +0000527
Ted Kremenek159d2482008-12-09 00:44:16 +0000528class VISIBILITY_HIDDEN BadSizeVLA : public BuiltinBug {
Ted Kremenekefd59942008-12-08 22:47:34 +0000529public:
Ted Kremenek5d88ff82009-04-02 02:40:26 +0000530 BadSizeVLA(GRExprEngine* eng) :
531 BuiltinBug(eng, "Bad variable-length array (VLA) size") {}
Mike Stump1eb44332009-09-09 15:08:12 +0000532
Ted Kremenekcf118d42009-02-04 23:49:09 +0000533 void FlushReportsImpl(BugReporter& BR, GRExprEngine& Eng) {
Ted Kremenekefd59942008-12-08 22:47:34 +0000534 for (GRExprEngine::ErrorNodes::iterator
Ted Kremenek159d2482008-12-09 00:44:16 +0000535 I = Eng.ExplicitBadSizedVLA.begin(),
536 E = Eng.ExplicitBadSizedVLA.end(); I!=E; ++I) {
537
538 // Determine whether this was a 'zero-sized' VLA or a VLA with an
539 // undefined size.
Zhongxing Xu031ccc02009-08-06 12:48:26 +0000540 ExplodedNode* N = *I;
Mike Stump1eb44332009-09-09 15:08:12 +0000541 PostStmt PS = cast<PostStmt>(N->getLocation());
Ted Kremenek5f85e172009-07-22 22:35:28 +0000542 const DeclStmt *DS = cast<DeclStmt>(PS.getStmt());
Ted Kremenekefd59942008-12-08 22:47:34 +0000543 VarDecl* VD = cast<VarDecl>(*DS->decl_begin());
544 QualType T = Eng.getContext().getCanonicalType(VD->getType());
545 VariableArrayType* VT = cast<VariableArrayType>(T);
Ted Kremenek159d2482008-12-09 00:44:16 +0000546 Expr* SizeExpr = VT->getSizeExpr();
Mike Stump1eb44332009-09-09 15:08:12 +0000547
Ted Kremenek159d2482008-12-09 00:44:16 +0000548 std::string buf;
549 llvm::raw_string_ostream os(buf);
Ted Kremenek5d88ff82009-04-02 02:40:26 +0000550 os << "The expression used to specify the number of elements in the "
551 "variable-length array (VLA) '"
Ted Kremenekcf118d42009-02-04 23:49:09 +0000552 << VD->getNameAsString() << "' evaluates to ";
Mike Stump1eb44332009-09-09 15:08:12 +0000553
Ted Kremenek23ec48c2009-06-18 23:58:37 +0000554 bool isUndefined = N->getState()->getSVal(SizeExpr).isUndef();
Mike Stump1eb44332009-09-09 15:08:12 +0000555
Ted Kremenekd49967f2009-04-29 21:58:13 +0000556 if (isUndefined)
Ted Kremenek159d2482008-12-09 00:44:16 +0000557 os << "an undefined or garbage value.";
Ted Kremenekcf118d42009-02-04 23:49:09 +0000558 else
559 os << "0. VLAs with no elements have undefined behavior.";
Mike Stump1eb44332009-09-09 15:08:12 +0000560
Ted Kremenekd49967f2009-04-29 21:58:13 +0000561 std::string shortBuf;
562 llvm::raw_string_ostream os_short(shortBuf);
563 os_short << "Variable-length array '" << VD->getNameAsString() << "' "
Ted Kremenekeaedfea2009-05-10 05:11:21 +0000564 << (isUndefined ? "garbage value for array size"
565 : "has zero elements (undefined behavior)");
Ted Kremenek159d2482008-12-09 00:44:16 +0000566
Ted Kremenek85ac9342009-05-15 05:25:09 +0000567 ArgReport *report = new ArgReport(*this, os_short.str().c_str(),
568 os.str().c_str(), N, SizeExpr);
569
Ted Kremenekcf118d42009-02-04 23:49:09 +0000570 report->addRange(SizeExpr->getSourceRange());
571 BR.EmitReport(report);
Ted Kremenekefd59942008-12-08 22:47:34 +0000572 }
573 }
Mike Stump1eb44332009-09-09 15:08:12 +0000574
Ted Kremenek85ac9342009-05-15 05:25:09 +0000575 void registerInitialVisitors(BugReporterContext& BRC,
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000576 const ExplodedNode* N,
Ted Kremenek85ac9342009-05-15 05:25:09 +0000577 BuiltinBugReport *R) {
578 registerTrackNullOrUndefValue(BRC, static_cast<ArgReport*>(R)->getArg(),
579 N);
580 }
Ted Kremenekefd59942008-12-08 22:47:34 +0000581};
Zhongxing Xu1c0c2332008-11-23 05:52:28 +0000582
Ted Kremenek78d46242008-07-22 16:21:24 +0000583//===----------------------------------------------------------------------===//
584// __attribute__(nonnull) checking
585
Ted Kremenekc26a8b02009-07-22 21:46:56 +0000586class VISIBILITY_HIDDEN CheckAttrNonNull :
587 public CheckerVisitor<CheckAttrNonNull> {
588
Ted Kremenekcf118d42009-02-04 23:49:09 +0000589 BugType *BT;
Mike Stump1eb44332009-09-09 15:08:12 +0000590
Ted Kremenek78d46242008-07-22 16:21:24 +0000591public:
Ted Kremenekc26a8b02009-07-22 21:46:56 +0000592 CheckAttrNonNull() : BT(0) {}
Ted Kremenek31112182009-07-24 00:40:31 +0000593 ~CheckAttrNonNull() {}
Ted Kremenek78d46242008-07-22 16:21:24 +0000594
Ted Kremenekc26a8b02009-07-22 21:46:56 +0000595 const void *getTag() {
596 static int x = 0;
597 return &x;
598 }
599
600 void PreVisitCallExpr(CheckerContext &C, const CallExpr *CE) {
601 const GRState *state = C.getState();
602 const GRState *originalState = state;
Mike Stump1eb44332009-09-09 15:08:12 +0000603
Ted Kremenekc26a8b02009-07-22 21:46:56 +0000604 // Check if the callee has a 'nonnull' attribute.
Ted Kremenek23ec48c2009-06-18 23:58:37 +0000605 SVal X = state->getSVal(CE->getCallee());
Mike Stump1eb44332009-09-09 15:08:12 +0000606
Zhongxing Xu369f4472009-04-20 05:24:46 +0000607 const FunctionDecl* FD = X.getAsFunctionDecl();
608 if (!FD)
Ted Kremenekc26a8b02009-07-22 21:46:56 +0000609 return;
Zhongxing Xu369f4472009-04-20 05:24:46 +0000610
Mike Stump1eb44332009-09-09 15:08:12 +0000611 const NonNullAttr* Att = FD->getAttr<NonNullAttr>();
Ted Kremenek78d46242008-07-22 16:21:24 +0000612 if (!Att)
Ted Kremenekc26a8b02009-07-22 21:46:56 +0000613 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000614
Ted Kremenek78d46242008-07-22 16:21:24 +0000615 // Iterate through the arguments of CE and check them for null.
Ted Kremenek78d46242008-07-22 16:21:24 +0000616 unsigned idx = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000617
Ted Kremenekc26a8b02009-07-22 21:46:56 +0000618 for (CallExpr::const_arg_iterator I=CE->arg_begin(), E=CE->arg_end(); I!=E;
Ted Kremenek78d46242008-07-22 16:21:24 +0000619 ++I, ++idx) {
Mike Stump1eb44332009-09-09 15:08:12 +0000620
Ted Kremenekc26a8b02009-07-22 21:46:56 +0000621 if (!Att->isNonNull(idx))
622 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000623
Ted Kremenek08780072009-08-24 22:47:34 +0000624 const SVal &V = state->getSVal(*I);
625 const DefinedSVal *DV = dyn_cast<DefinedSVal>(&V);
Mike Stump1eb44332009-09-09 15:08:12 +0000626
Ted Kremenek08780072009-08-24 22:47:34 +0000627 if (!DV)
628 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000629
Ted Kremenekc26a8b02009-07-22 21:46:56 +0000630 ConstraintManager &CM = C.getConstraintManager();
631 const GRState *stateNotNull, *stateNull;
Ted Kremenek08780072009-08-24 22:47:34 +0000632 llvm::tie(stateNotNull, stateNull) = CM.AssumeDual(state, *DV);
Mike Stump1eb44332009-09-09 15:08:12 +0000633
Ted Kremenekc26a8b02009-07-22 21:46:56 +0000634 if (stateNull && !stateNotNull) {
635 // Generate an error node. Check for a null node in case
636 // we cache out.
Zhongxing Xu6403b572009-09-02 13:26:26 +0000637 if (ExplodedNode *errorNode = C.GenerateNode(CE, stateNull, true)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000638
Ted Kremenekc26a8b02009-07-22 21:46:56 +0000639 // Lazily allocate the BugType object if it hasn't already been
640 // created. Ownership is transferred to the BugReporter object once
641 // the BugReport is passed to 'EmitWarning'.
642 if (!BT)
643 BT = new BugType("Argument with 'nonnull' attribute passed null",
644 "API");
Mike Stump1eb44332009-09-09 15:08:12 +0000645
Ted Kremenek592362b2009-08-18 01:05:30 +0000646 EnhancedBugReport *R =
647 new EnhancedBugReport(*BT,
648 "Null pointer passed as an argument to a "
649 "'nonnull' parameter", errorNode);
Mike Stump1eb44332009-09-09 15:08:12 +0000650
Ted Kremenekc26a8b02009-07-22 21:46:56 +0000651 // Highlight the range of the argument that was null.
Ted Kremenek592362b2009-08-18 01:05:30 +0000652 const Expr *arg = *I;
653 R->addRange(arg->getSourceRange());
654 R->addVisitorCreator(registerTrackNullOrUndefValue, arg);
Mike Stump1eb44332009-09-09 15:08:12 +0000655
Ted Kremenekc26a8b02009-07-22 21:46:56 +0000656 // Emit the bug report.
657 C.EmitReport(R);
658 }
Mike Stump1eb44332009-09-09 15:08:12 +0000659
Ted Kremenekc26a8b02009-07-22 21:46:56 +0000660 // Always return. Either we cached out or we just emitted an error.
661 return;
662 }
Mike Stump1eb44332009-09-09 15:08:12 +0000663
Ted Kremenekc26a8b02009-07-22 21:46:56 +0000664 // If a pointer value passed the check we should assume that it is
665 // indeed not null from this point forward.
666 assert(stateNotNull);
667 state = stateNotNull;
668 }
Mike Stump1eb44332009-09-09 15:08:12 +0000669
Ted Kremenekc26a8b02009-07-22 21:46:56 +0000670 // If we reach here all of the arguments passed the nonnull check.
671 // If 'state' has been updated generated a new node.
672 if (state != originalState)
Zhongxing Xu6403b572009-09-02 13:26:26 +0000673 C.addTransition(C.GenerateNode(CE, state));
Ted Kremenek78d46242008-07-22 16:21:24 +0000674 }
Ted Kremenek78d46242008-07-22 16:21:24 +0000675};
676} // end anonymous namespace
677
Zhongxing Xu9a5bca32009-08-29 02:11:01 +0000678// Undefined arguments checking.
679namespace {
Mike Stump1eb44332009-09-09 15:08:12 +0000680class VISIBILITY_HIDDEN CheckUndefinedArg
Zhongxing Xu9a5bca32009-08-29 02:11:01 +0000681 : public CheckerVisitor<CheckUndefinedArg> {
682
Zhongxing Xu904e1e32009-09-02 07:09:39 +0000683 BadArg *BT;
Zhongxing Xu9a5bca32009-08-29 02:11:01 +0000684
685public:
686 CheckUndefinedArg() : BT(0) {}
687 ~CheckUndefinedArg() {}
688
689 const void *getTag() {
690 static int x = 0;
691 return &x;
692 }
693
694 void PreVisitCallExpr(CheckerContext &C, const CallExpr *CE);
695};
696
697void CheckUndefinedArg::PreVisitCallExpr(CheckerContext &C, const CallExpr *CE){
698 for (CallExpr::const_arg_iterator I = CE->arg_begin(), E = CE->arg_end();
699 I != E; ++I) {
700 if (C.getState()->getSVal(*I).isUndef()) {
Zhongxing Xu6403b572009-09-02 13:26:26 +0000701 if (ExplodedNode *ErrorNode = C.GenerateNode(CE, true)) {
Zhongxing Xu9a5bca32009-08-29 02:11:01 +0000702 if (!BT)
Zhongxing Xu904e1e32009-09-02 07:09:39 +0000703 BT = new BadArg();
Zhongxing Xu9a5bca32009-08-29 02:11:01 +0000704 // Generate a report for this bug.
Zhongxing Xu904e1e32009-09-02 07:09:39 +0000705 ArgReport *Report = new ArgReport(*BT, BT->getDescription().c_str(),
Zhongxing Xu9a5bca32009-08-29 02:11:01 +0000706 ErrorNode, *I);
707 Report->addRange((*I)->getSourceRange());
708 C.EmitReport(Report);
709 }
710 }
711 }
712}
713
Zhongxing Xud99f3612009-09-02 08:10:35 +0000714class VISIBILITY_HIDDEN CheckBadCall : public CheckerVisitor<CheckBadCall> {
715 BadCall *BT;
716
717public:
718 CheckBadCall() : BT(0) {}
719 ~CheckBadCall() {}
720
721 const void *getTag() {
722 static int x = 0;
723 return &x;
724 }
725
726 void PreVisitCallExpr(CheckerContext &C, const CallExpr *CE);
727};
728
729void CheckBadCall::PreVisitCallExpr(CheckerContext &C, const CallExpr *CE) {
730 const Expr *Callee = CE->getCallee()->IgnoreParens();
731 SVal L = C.getState()->getSVal(Callee);
732
733 if (L.isUndef() || isa<loc::ConcreteInt>(L)) {
Zhongxing Xu6403b572009-09-02 13:26:26 +0000734 if (ExplodedNode *N = C.GenerateNode(CE, true)) {
Zhongxing Xud99f3612009-09-02 08:10:35 +0000735 if (!BT)
736 BT = new BadCall();
737 C.EmitReport(new BuiltinBugReport(*BT, BT->getDescription().c_str(), N));
738 }
739 }
740}
741
Zhongxing Xu6403b572009-09-02 13:26:26 +0000742class VISIBILITY_HIDDEN CheckBadDiv : public CheckerVisitor<CheckBadDiv> {
743 DivZero *BT;
744public:
745 CheckBadDiv() : BT(0) {}
746 ~CheckBadDiv() {}
747
748 const void *getTag() {
749 static int x;
750 return &x;
751 }
752
753 void PreVisitBinaryOperator(CheckerContext &C, const BinaryOperator *B);
754};
755
Mike Stump1eb44332009-09-09 15:08:12 +0000756void CheckBadDiv::PreVisitBinaryOperator(CheckerContext &C,
Zhongxing Xu6403b572009-09-02 13:26:26 +0000757 const BinaryOperator *B) {
758 BinaryOperator::Opcode Op = B->getOpcode();
759 if (Op != BinaryOperator::Div &&
760 Op != BinaryOperator::Rem &&
761 Op != BinaryOperator::DivAssign &&
762 Op != BinaryOperator::RemAssign)
763 return;
764
765 if (!B->getRHS()->getType()->isIntegerType() ||
766 !B->getRHS()->getType()->isScalarType())
767 return;
768
Zhongxing Xu6403b572009-09-02 13:26:26 +0000769 // Check for divide by undefined.
770 SVal Denom = C.getState()->getSVal(B->getRHS());
771
772 if (Denom.isUndef()) {
773 if (ExplodedNode *N = C.GenerateNode(B, true)) {
774 if (!BT)
775 BT = new DivZero();
776
777 C.EmitReport(new BuiltinBugReport(*BT, BT->getDescription().c_str(), N));
778 }
779 return;
780 }
781
Ted Kremenek970e03a2009-09-03 01:48:03 +0000782 // Handle the case where 'Denom' is UnknownVal.
783 const DefinedSVal *DV = dyn_cast<DefinedSVal>(&Denom);
784
Mike Stump1eb44332009-09-09 15:08:12 +0000785 if (!DV)
Ted Kremenek970e03a2009-09-03 01:48:03 +0000786 return;
787
Zhongxing Xu6403b572009-09-02 13:26:26 +0000788 // Check for divide by zero.
789 ConstraintManager &CM = C.getConstraintManager();
790 const GRState *stateNotZero, *stateZero;
Ted Kremenek970e03a2009-09-03 01:48:03 +0000791 llvm::tie(stateNotZero, stateZero) = CM.AssumeDual(C.getState(), *DV);
Mike Stump1eb44332009-09-09 15:08:12 +0000792
Zhongxing Xu6403b572009-09-02 13:26:26 +0000793 if (stateZero && !stateNotZero) {
794 if (ExplodedNode *N = C.GenerateNode(B, stateZero, true)) {
795 if (!BT)
796 BT = new DivZero();
Mike Stump1eb44332009-09-09 15:08:12 +0000797
Zhongxing Xu6403b572009-09-02 13:26:26 +0000798 C.EmitReport(new BuiltinBugReport(*BT, BT->getDescription().c_str(), N));
799 }
800 return;
801 }
802
803 // If we get here, then the denom should not be zero.
804 if (stateNotZero != C.getState())
805 C.addTransition(C.GenerateNode(B, stateNotZero));
806}
Zhongxing Xu9a5bca32009-08-29 02:11:01 +0000807}
Ted Kremenek78d46242008-07-22 16:21:24 +0000808//===----------------------------------------------------------------------===//
809// Check registration.
Ted Kremenekcf118d42009-02-04 23:49:09 +0000810//===----------------------------------------------------------------------===//
Ted Kremenek78d46242008-07-22 16:21:24 +0000811
812void GRExprEngine::RegisterInternalChecks() {
Ted Kremenekcf118d42009-02-04 23:49:09 +0000813 // Register internal "built-in" BugTypes with the BugReporter. These BugTypes
814 // are different than what probably many checks will do since they don't
815 // create BugReports on-the-fly but instead wait until GRExprEngine finishes
816 // analyzing a function. Generation of BugReport objects is done via a call
817 // to 'FlushReports' from BugReporter.
818 BR.Register(new NullDeref(this));
819 BR.Register(new UndefinedDeref(this));
820 BR.Register(new UndefBranch(this));
Ted Kremenekcf118d42009-02-04 23:49:09 +0000821 BR.Register(new UndefResult(this));
Ted Kremenekcf118d42009-02-04 23:49:09 +0000822 BR.Register(new RetStack(this));
823 BR.Register(new RetUndef(this));
Ted Kremenekcf118d42009-02-04 23:49:09 +0000824 BR.Register(new BadMsgExprArg(this));
825 BR.Register(new BadReceiver(this));
826 BR.Register(new OutOfBoundMemoryAccess(this));
827 BR.Register(new BadSizeVLA(this));
Ted Kremenek21fe8372009-02-19 04:06:22 +0000828 BR.Register(new NilReceiverStructRet(this));
Ted Kremenek899b3de2009-04-08 03:07:17 +0000829 BR.Register(new NilReceiverLargerThanVoidPtrRet(this));
Mike Stump1eb44332009-09-09 15:08:12 +0000830
Ted Kremenekcf118d42009-02-04 23:49:09 +0000831 // The following checks do not need to have their associated BugTypes
832 // explicitly registered with the BugReporter. If they issue any BugReports,
833 // their associated BugType will get registered with the BugReporter
834 // automatically. Note that the check itself is owned by the GRExprEngine
835 // object.
Ted Kremenekc26a8b02009-07-22 21:46:56 +0000836 registerCheck(new CheckAttrNonNull());
Zhongxing Xu9a5bca32009-08-29 02:11:01 +0000837 registerCheck(new CheckUndefinedArg());
Zhongxing Xud99f3612009-09-02 08:10:35 +0000838 registerCheck(new CheckBadCall());
Zhongxing Xu6403b572009-09-02 13:26:26 +0000839 registerCheck(new CheckBadDiv());
Ted Kremenek78d46242008-07-22 16:21:24 +0000840}