blob: ab6874ad60db157c868057330d6703319a8b28c2 [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()
129 << "') to be garbage or otherwise undefined.";
130
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
164 << " 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 Kremenek5d88ff82009-04-02 02:40:26 +0000209 UndefResult(GRExprEngine* eng) : BuiltinBug(eng,"Undefined result",
Ted Kremenek78d46242008-07-22 16:21:24 +0000210 "Result of operation is undefined.") {}
Mike Stump1eb44332009-09-09 15:08:12 +0000211
Ted Kremenekcf118d42009-02-04 23:49:09 +0000212 void FlushReportsImpl(BugReporter& BR, GRExprEngine& Eng) {
Ted Kremenek78d46242008-07-22 16:21:24 +0000213 Emit(BR, Eng.undef_results_begin(), Eng.undef_results_end());
214 }
215};
Mike Stump1eb44332009-09-09 15:08:12 +0000216
Ted Kremenek78d46242008-07-22 16:21:24 +0000217class VISIBILITY_HIDDEN BadCall : public BuiltinBug {
218public:
Zhongxing Xud99f3612009-09-02 08:10:35 +0000219 BadCall(GRExprEngine *eng = 0)
Ted Kremenek5d88ff82009-04-02 02:40:26 +0000220 : BuiltinBug(eng, "Invalid function call",
Ted Kremenek17a8e072009-03-01 05:43:22 +0000221 "Called function pointer is a null or undefined pointer value") {}
Mike Stump1eb44332009-09-09 15:08:12 +0000222
Ted Kremenek85ac9342009-05-15 05:25:09 +0000223 void registerInitialVisitors(BugReporterContext& BRC,
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000224 const ExplodedNode* N,
Ted Kremenek85ac9342009-05-15 05:25:09 +0000225 BuiltinBugReport *R) {
226 registerTrackNullOrUndefValue(BRC, GetCalleeExpr(N), N);
227 }
228};
229
230
231class VISIBILITY_HIDDEN ArgReport : public BuiltinBugReport {
232 const Stmt *Arg;
233public:
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000234 ArgReport(BugType& bt, const char* desc, ExplodedNode *n,
Ted Kremenek85ac9342009-05-15 05:25:09 +0000235 const Stmt *arg)
236 : BuiltinBugReport(bt, desc, n), Arg(arg) {}
Mike Stump1eb44332009-09-09 15:08:12 +0000237
Ted Kremenek85ac9342009-05-15 05:25:09 +0000238 ArgReport(BugType& bt, const char *shortDesc, const char *desc,
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000239 ExplodedNode *n, const Stmt *arg)
Mike Stump1eb44332009-09-09 15:08:12 +0000240 : BuiltinBugReport(bt, shortDesc, desc, n), Arg(arg) {}
241
242 const Stmt *getArg() const { return Arg; }
Ted Kremenek78d46242008-07-22 16:21:24 +0000243};
244
245class VISIBILITY_HIDDEN BadArg : public BuiltinBug {
Mike Stump1eb44332009-09-09 15:08:12 +0000246public:
247 BadArg(GRExprEngine* eng=0) : BuiltinBug(eng,"Uninitialized argument",
Ted Kremenek17a8e072009-03-01 05:43:22 +0000248 "Pass-by-value argument in function call is undefined.") {}
Ted Kremenek78d46242008-07-22 16:21:24 +0000249
Ted Kremenekcf118d42009-02-04 23:49:09 +0000250 BadArg(GRExprEngine* eng, const char* d)
Ted Kremenek5d88ff82009-04-02 02:40:26 +0000251 : BuiltinBug(eng,"Uninitialized argument", d) {}
Mike Stump1eb44332009-09-09 15:08:12 +0000252
Ted Kremenek85ac9342009-05-15 05:25:09 +0000253 void registerInitialVisitors(BugReporterContext& BRC,
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000254 const ExplodedNode* N,
Ted Kremenek85ac9342009-05-15 05:25:09 +0000255 BuiltinBugReport *R) {
256 registerTrackNullOrUndefValue(BRC, static_cast<ArgReport*>(R)->getArg(),
257 N);
Mike Stump1eb44332009-09-09 15:08:12 +0000258 }
Ted Kremenek78d46242008-07-22 16:21:24 +0000259};
Mike Stump1eb44332009-09-09 15:08:12 +0000260
Ted Kremenek78d46242008-07-22 16:21:24 +0000261class VISIBILITY_HIDDEN BadMsgExprArg : public BadArg {
262public:
Mike Stump1eb44332009-09-09 15:08:12 +0000263 BadMsgExprArg(GRExprEngine* eng)
Ted Kremenek5d88ff82009-04-02 02:40:26 +0000264 : BadArg(eng,"Pass-by-value argument in message expression is undefined"){}
Mike Stump1eb44332009-09-09 15:08:12 +0000265
Ted Kremenekcf118d42009-02-04 23:49:09 +0000266 void FlushReportsImpl(BugReporter& BR, GRExprEngine& Eng) {
Ted Kremenek78d46242008-07-22 16:21:24 +0000267 for (GRExprEngine::UndefArgsTy::iterator I=Eng.msg_expr_undef_arg_begin(),
Mike Stump1eb44332009-09-09 15:08:12 +0000268 E = Eng.msg_expr_undef_arg_end(); I!=E; ++I) {
Ted Kremenek78d46242008-07-22 16:21:24 +0000269 // Generate a report for this bug.
Ted Kremenek85ac9342009-05-15 05:25:09 +0000270 ArgReport *report = new ArgReport(*this, desc.c_str(), I->first,
271 I->second);
Ted Kremenekcf118d42009-02-04 23:49:09 +0000272 report->addRange(I->second->getSourceRange());
273 BR.EmitReport(report);
Mike Stump1eb44332009-09-09 15:08:12 +0000274 }
275 }
Ted Kremenek78d46242008-07-22 16:21:24 +0000276};
Mike Stump1eb44332009-09-09 15:08:12 +0000277
Ted Kremenek78d46242008-07-22 16:21:24 +0000278class VISIBILITY_HIDDEN BadReceiver : public BuiltinBug {
Mike Stump1eb44332009-09-09 15:08:12 +0000279public:
Ted Kremenekcf118d42009-02-04 23:49:09 +0000280 BadReceiver(GRExprEngine* eng)
Ted Kremenek5d88ff82009-04-02 02:40:26 +0000281 : BuiltinBug(eng,"Uninitialized receiver",
282 "Receiver in message expression is an uninitialized value") {}
Mike Stump1eb44332009-09-09 15:08:12 +0000283
Ted Kremenekcf118d42009-02-04 23:49:09 +0000284 void FlushReportsImpl(BugReporter& BR, GRExprEngine& Eng) {
Ted Kremenekefd59942008-12-08 22:47:34 +0000285 for (GRExprEngine::ErrorNodes::iterator I=Eng.undef_receivers_begin(),
Ted Kremenek78d46242008-07-22 16:21:24 +0000286 End = Eng.undef_receivers_end(); I!=End; ++I) {
Mike Stump1eb44332009-09-09 15:08:12 +0000287
Ted Kremenek78d46242008-07-22 16:21:24 +0000288 // Generate a report for this bug.
Ted Kremenek0c313172009-05-13 19:16:35 +0000289 BuiltinBugReport *report = new BuiltinBugReport(*this, desc.c_str(), *I);
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000290 ExplodedNode* N = *I;
Ted Kremenek5f85e172009-07-22 22:35:28 +0000291 const Stmt *S = cast<PostStmt>(N->getLocation()).getStmt();
292 const Expr* E = cast<ObjCMessageExpr>(S)->getReceiver();
Ted Kremenek78d46242008-07-22 16:21:24 +0000293 assert (E && "Receiver cannot be NULL");
Ted Kremenekcf118d42009-02-04 23:49:09 +0000294 report->addRange(E->getSourceRange());
295 BR.EmitReport(report);
Ted Kremenek0c313172009-05-13 19:16:35 +0000296 }
297 }
Ted Kremenek85ac9342009-05-15 05:25:09 +0000298
Ted Kremenek0c313172009-05-13 19:16:35 +0000299 void registerInitialVisitors(BugReporterContext& BRC,
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000300 const ExplodedNode* N,
Ted Kremenek0c313172009-05-13 19:16:35 +0000301 BuiltinBugReport *R) {
Ted Kremenek85ac9342009-05-15 05:25:09 +0000302 registerTrackNullOrUndefValue(BRC, GetReceiverExpr(N), N);
Mike Stump1eb44332009-09-09 15:08:12 +0000303 }
Ted Kremenek78d46242008-07-22 16:21:24 +0000304};
Ted Kremenek5917d782008-11-21 00:27:44 +0000305
Ted Kremenek78d46242008-07-22 16:21:24 +0000306class VISIBILITY_HIDDEN RetStack : public BuiltinBug {
307public:
Ted Kremenek17a8e072009-03-01 05:43:22 +0000308 RetStack(GRExprEngine* eng)
Ted Kremenek5d88ff82009-04-02 02:40:26 +0000309 : BuiltinBug(eng, "Return of address to stack-allocated memory") {}
Mike Stump1eb44332009-09-09 15:08:12 +0000310
Ted Kremenekcf118d42009-02-04 23:49:09 +0000311 void FlushReportsImpl(BugReporter& BR, GRExprEngine& Eng) {
Ted Kremenekb7714b22008-07-30 17:49:12 +0000312 for (GRExprEngine::ret_stackaddr_iterator I=Eng.ret_stackaddr_begin(),
313 End = Eng.ret_stackaddr_end(); I!=End; ++I) {
Ted Kremenek22bda882008-07-31 20:31:27 +0000314
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000315 ExplodedNode* N = *I;
Ted Kremenek5f85e172009-07-22 22:35:28 +0000316 const Stmt *S = cast<PostStmt>(N->getLocation()).getStmt();
317 const Expr* E = cast<ReturnStmt>(S)->getRetValue();
318 assert(E && "Return expression cannot be NULL");
Mike Stump1eb44332009-09-09 15:08:12 +0000319
Ted Kremenek22bda882008-07-31 20:31:27 +0000320 // Get the value associated with E.
Ted Kremenek23ec48c2009-06-18 23:58:37 +0000321 loc::MemRegionVal V = cast<loc::MemRegionVal>(N->getState()->getSVal(E));
Mike Stump1eb44332009-09-09 15:08:12 +0000322
Ted Kremenek22bda882008-07-31 20:31:27 +0000323 // Generate a report for this bug.
Ted Kremenekad51a602008-10-31 00:18:30 +0000324 std::string buf;
325 llvm::raw_string_ostream os(buf);
Ted Kremenek8aed8062008-10-31 00:13:20 +0000326 SourceRange R;
Mike Stump1eb44332009-09-09 15:08:12 +0000327
Ted Kremenek8aed8062008-10-31 00:13:20 +0000328 // Check if the region is a compound literal.
Mike Stump1eb44332009-09-09 15:08:12 +0000329 if (const CompoundLiteralRegion* CR =
Ted Kremenek8aed8062008-10-31 00:13:20 +0000330 dyn_cast<CompoundLiteralRegion>(V.getRegion())) {
Mike Stump1eb44332009-09-09 15:08:12 +0000331
Ted Kremenek8aed8062008-10-31 00:13:20 +0000332 const CompoundLiteralExpr* CL = CR->getLiteralExpr();
333 os << "Address of stack memory associated with a compound literal "
334 "declared on line "
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000335 << BR.getSourceManager()
336 .getInstantiationLineNumber(CL->getLocStart())
Ted Kremenek8aed8062008-10-31 00:13:20 +0000337 << " returned.";
Mike Stump1eb44332009-09-09 15:08:12 +0000338
Ted Kremenek8aed8062008-10-31 00:13:20 +0000339 R = CL->getSourceRange();
340 }
Ted Kremenekde8cd192008-11-02 00:35:25 +0000341 else if (const AllocaRegion* AR = dyn_cast<AllocaRegion>(V.getRegion())) {
342 const Expr* ARE = AR->getExpr();
343 SourceLocation L = ARE->getLocStart();
344 R = ARE->getSourceRange();
Mike Stump1eb44332009-09-09 15:08:12 +0000345
Ted Kremenekde8cd192008-11-02 00:35:25 +0000346 os << "Address of stack memory allocated by call to alloca() on line "
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000347 << BR.getSourceManager().getInstantiationLineNumber(L)
Ted Kremenekde8cd192008-11-02 00:35:25 +0000348 << " returned.";
Mike Stump1eb44332009-09-09 15:08:12 +0000349 }
350 else {
Ted Kremenek8aed8062008-10-31 00:13:20 +0000351 os << "Address of stack memory associated with local variable '"
352 << V.getRegion()->getString() << "' returned.";
353 }
Mike Stump1eb44332009-09-09 15:08:12 +0000354
Ted Kremenekcf118d42009-02-04 23:49:09 +0000355 RangedBugReport *report = new RangedBugReport(*this, os.str().c_str(), N);
356 report->addRange(E->getSourceRange());
357 if (R.isValid()) report->addRange(R);
358 BR.EmitReport(report);
Ted Kremenekb7714b22008-07-30 17:49:12 +0000359 }
Ted Kremenek78d46242008-07-22 16:21:24 +0000360 }
361};
Mike Stump1eb44332009-09-09 15:08:12 +0000362
Ted Kremenek5917d782008-11-21 00:27:44 +0000363class VISIBILITY_HIDDEN RetUndef : public BuiltinBug {
364public:
Ted Kremenek5d88ff82009-04-02 02:40:26 +0000365 RetUndef(GRExprEngine* eng) : BuiltinBug(eng, "Uninitialized return value",
Ted Kremenek0c313172009-05-13 19:16:35 +0000366 "Uninitialized or undefined value returned to caller.") {}
Mike Stump1eb44332009-09-09 15:08:12 +0000367
Ted Kremenekcf118d42009-02-04 23:49:09 +0000368 void FlushReportsImpl(BugReporter& BR, GRExprEngine& Eng) {
Ted Kremenek5917d782008-11-21 00:27:44 +0000369 Emit(BR, Eng.ret_undef_begin(), Eng.ret_undef_end());
370 }
Mike Stump1eb44332009-09-09 15:08:12 +0000371
Ted Kremenek0c313172009-05-13 19:16:35 +0000372 void registerInitialVisitors(BugReporterContext& BRC,
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000373 const ExplodedNode* N,
Ted Kremenek0c313172009-05-13 19:16:35 +0000374 BuiltinBugReport *R) {
Ted Kremenek85ac9342009-05-15 05:25:09 +0000375 registerTrackNullOrUndefValue(BRC, GetRetValExpr(N), N);
Mike Stump1eb44332009-09-09 15:08:12 +0000376 }
Ted Kremenek5917d782008-11-21 00:27:44 +0000377};
Ted Kremenek78d46242008-07-22 16:21:24 +0000378
379class VISIBILITY_HIDDEN UndefBranch : public BuiltinBug {
380 struct VISIBILITY_HIDDEN FindUndefExpr {
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000381 GRStateManager& VM;
382 const GRState* St;
Mike Stump1eb44332009-09-09 15:08:12 +0000383
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000384 FindUndefExpr(GRStateManager& V, const GRState* S) : VM(V), St(S) {}
Mike Stump1eb44332009-09-09 15:08:12 +0000385
386 Expr* FindExpr(Expr* Ex) {
Ted Kremenek78d46242008-07-22 16:21:24 +0000387 if (!MatchesCriteria(Ex))
Ted Kremenekb7714b22008-07-30 17:49:12 +0000388 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000389
Ted Kremenekb7714b22008-07-30 17:49:12 +0000390 for (Stmt::child_iterator I=Ex->child_begin(), E=Ex->child_end();I!=E;++I)
Ted Kremenek78d46242008-07-22 16:21:24 +0000391 if (Expr* ExI = dyn_cast_or_null<Expr>(*I)) {
392 Expr* E2 = FindExpr(ExI);
393 if (E2) return E2;
394 }
Mike Stump1eb44332009-09-09 15:08:12 +0000395
Ted Kremenek78d46242008-07-22 16:21:24 +0000396 return Ex;
397 }
Mike Stump1eb44332009-09-09 15:08:12 +0000398
Ted Kremenek23ec48c2009-06-18 23:58:37 +0000399 bool MatchesCriteria(Expr* Ex) { return St->getSVal(Ex).isUndef(); }
Ted Kremenek78d46242008-07-22 16:21:24 +0000400 };
Mike Stump1eb44332009-09-09 15:08:12 +0000401
Ted Kremenek78d46242008-07-22 16:21:24 +0000402public:
Ted Kremenekcf118d42009-02-04 23:49:09 +0000403 UndefBranch(GRExprEngine *eng)
Ted Kremenek5d88ff82009-04-02 02:40:26 +0000404 : BuiltinBug(eng,"Use of uninitialized value",
Ted Kremenek78d46242008-07-22 16:21:24 +0000405 "Branch condition evaluates to an uninitialized value.") {}
Mike Stump1eb44332009-09-09 15:08:12 +0000406
Ted Kremenekcf118d42009-02-04 23:49:09 +0000407 void FlushReportsImpl(BugReporter& BR, GRExprEngine& Eng) {
Ted Kremenek78d46242008-07-22 16:21:24 +0000408 for (GRExprEngine::undef_branch_iterator I=Eng.undef_branches_begin(),
409 E=Eng.undef_branches_end(); I!=E; ++I) {
410
411 // What's going on here: we want to highlight the subexpression of the
412 // condition that is the most likely source of the "uninitialized
413 // branch condition." We do a recursive walk of the condition's
414 // subexpressions and roughly look for the most nested subexpression
415 // that binds to Undefined. We then highlight that expression's range.
Ted Kremenek78d46242008-07-22 16:21:24 +0000416 BlockEdge B = cast<BlockEdge>((*I)->getLocation());
417 Expr* Ex = cast<Expr>(B.getSrc()->getTerminatorCondition());
418 assert (Ex && "Block must have a terminator.");
419
420 // Get the predecessor node and check if is a PostStmt with the Stmt
421 // being the terminator condition. We want to inspect the state
422 // of that node instead because it will contain main information about
423 // the subexpressions.
Ted Kremenek78d46242008-07-22 16:21:24 +0000424 assert (!(*I)->pred_empty());
425
426 // Note: any predecessor will do. They should have identical state,
427 // since all the BlockEdge did was act as an error sink since the value
428 // had to already be undefined.
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000429 ExplodedNode *N = *(*I)->pred_begin();
Ted Kremenek78d46242008-07-22 16:21:24 +0000430 ProgramPoint P = N->getLocation();
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000431 const GRState* St = (*I)->getState();
Ted Kremenek78d46242008-07-22 16:21:24 +0000432
433 if (PostStmt* PS = dyn_cast<PostStmt>(&P))
434 if (PS->getStmt() == Ex)
435 St = N->getState();
436
437 FindUndefExpr FindIt(Eng.getStateManager(), St);
438 Ex = FindIt.FindExpr(Ex);
439
Ted Kremenek85ac9342009-05-15 05:25:09 +0000440 ArgReport *R = new ArgReport(*this, desc.c_str(), *I, Ex);
Ted Kremenekcf118d42009-02-04 23:49:09 +0000441 R->addRange(Ex->getSourceRange());
442 BR.EmitReport(R);
Ted Kremenek78d46242008-07-22 16:21:24 +0000443 }
444 }
Mike Stump1eb44332009-09-09 15:08:12 +0000445
Ted Kremenek85ac9342009-05-15 05:25:09 +0000446 void registerInitialVisitors(BugReporterContext& BRC,
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000447 const ExplodedNode* N,
Ted Kremenek85ac9342009-05-15 05:25:09 +0000448 BuiltinBugReport *R) {
449 registerTrackNullOrUndefValue(BRC, static_cast<ArgReport*>(R)->getArg(),
450 N);
451 }
Ted Kremenek78d46242008-07-22 16:21:24 +0000452};
453
Zhongxing Xu1c0c2332008-11-23 05:52:28 +0000454class VISIBILITY_HIDDEN OutOfBoundMemoryAccess : public BuiltinBug {
455public:
Ted Kremenekcf118d42009-02-04 23:49:09 +0000456 OutOfBoundMemoryAccess(GRExprEngine* eng)
Ted Kremenek5d88ff82009-04-02 02:40:26 +0000457 : BuiltinBug(eng,"Out-of-bounds memory access",
Ted Kremenekcf118d42009-02-04 23:49:09 +0000458 "Load or store into an out-of-bound memory position.") {}
Zhongxing Xu1c0c2332008-11-23 05:52:28 +0000459
Ted Kremenekcf118d42009-02-04 23:49:09 +0000460 void FlushReportsImpl(BugReporter& BR, GRExprEngine& Eng) {
Zhongxing Xu1c0c2332008-11-23 05:52:28 +0000461 Emit(BR, Eng.explicit_oob_memacc_begin(), Eng.explicit_oob_memacc_end());
462 }
463};
Mike Stump1eb44332009-09-09 15:08:12 +0000464
Ted Kremenek159d2482008-12-09 00:44:16 +0000465class VISIBILITY_HIDDEN BadSizeVLA : public BuiltinBug {
Ted Kremenekefd59942008-12-08 22:47:34 +0000466public:
Ted Kremenek5d88ff82009-04-02 02:40:26 +0000467 BadSizeVLA(GRExprEngine* eng) :
468 BuiltinBug(eng, "Bad variable-length array (VLA) size") {}
Mike Stump1eb44332009-09-09 15:08:12 +0000469
Ted Kremenekcf118d42009-02-04 23:49:09 +0000470 void FlushReportsImpl(BugReporter& BR, GRExprEngine& Eng) {
Ted Kremenekefd59942008-12-08 22:47:34 +0000471 for (GRExprEngine::ErrorNodes::iterator
Ted Kremenek159d2482008-12-09 00:44:16 +0000472 I = Eng.ExplicitBadSizedVLA.begin(),
473 E = Eng.ExplicitBadSizedVLA.end(); I!=E; ++I) {
474
475 // Determine whether this was a 'zero-sized' VLA or a VLA with an
476 // undefined size.
Zhongxing Xu031ccc02009-08-06 12:48:26 +0000477 ExplodedNode* N = *I;
Mike Stump1eb44332009-09-09 15:08:12 +0000478 PostStmt PS = cast<PostStmt>(N->getLocation());
Ted Kremenek5f85e172009-07-22 22:35:28 +0000479 const DeclStmt *DS = cast<DeclStmt>(PS.getStmt());
Ted Kremenekefd59942008-12-08 22:47:34 +0000480 VarDecl* VD = cast<VarDecl>(*DS->decl_begin());
481 QualType T = Eng.getContext().getCanonicalType(VD->getType());
482 VariableArrayType* VT = cast<VariableArrayType>(T);
Ted Kremenek159d2482008-12-09 00:44:16 +0000483 Expr* SizeExpr = VT->getSizeExpr();
Mike Stump1eb44332009-09-09 15:08:12 +0000484
Ted Kremenek159d2482008-12-09 00:44:16 +0000485 std::string buf;
486 llvm::raw_string_ostream os(buf);
Ted Kremenek5d88ff82009-04-02 02:40:26 +0000487 os << "The expression used to specify the number of elements in the "
488 "variable-length array (VLA) '"
Ted Kremenekcf118d42009-02-04 23:49:09 +0000489 << VD->getNameAsString() << "' evaluates to ";
Mike Stump1eb44332009-09-09 15:08:12 +0000490
Ted Kremenek23ec48c2009-06-18 23:58:37 +0000491 bool isUndefined = N->getState()->getSVal(SizeExpr).isUndef();
Mike Stump1eb44332009-09-09 15:08:12 +0000492
Ted Kremenekd49967f2009-04-29 21:58:13 +0000493 if (isUndefined)
Ted Kremenek159d2482008-12-09 00:44:16 +0000494 os << "an undefined or garbage value.";
Ted Kremenekcf118d42009-02-04 23:49:09 +0000495 else
496 os << "0. VLAs with no elements have undefined behavior.";
Mike Stump1eb44332009-09-09 15:08:12 +0000497
Ted Kremenekd49967f2009-04-29 21:58:13 +0000498 std::string shortBuf;
499 llvm::raw_string_ostream os_short(shortBuf);
500 os_short << "Variable-length array '" << VD->getNameAsString() << "' "
Ted Kremenekeaedfea2009-05-10 05:11:21 +0000501 << (isUndefined ? "garbage value for array size"
502 : "has zero elements (undefined behavior)");
Ted Kremenek159d2482008-12-09 00:44:16 +0000503
Ted Kremenek85ac9342009-05-15 05:25:09 +0000504 ArgReport *report = new ArgReport(*this, os_short.str().c_str(),
505 os.str().c_str(), N, SizeExpr);
506
Ted Kremenekcf118d42009-02-04 23:49:09 +0000507 report->addRange(SizeExpr->getSourceRange());
508 BR.EmitReport(report);
Ted Kremenekefd59942008-12-08 22:47:34 +0000509 }
510 }
Mike Stump1eb44332009-09-09 15:08:12 +0000511
Ted Kremenek85ac9342009-05-15 05:25:09 +0000512 void registerInitialVisitors(BugReporterContext& BRC,
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000513 const ExplodedNode* N,
Ted Kremenek85ac9342009-05-15 05:25:09 +0000514 BuiltinBugReport *R) {
515 registerTrackNullOrUndefValue(BRC, static_cast<ArgReport*>(R)->getArg(),
516 N);
517 }
Ted Kremenekefd59942008-12-08 22:47:34 +0000518};
Zhongxing Xu1c0c2332008-11-23 05:52:28 +0000519
Ted Kremenek78d46242008-07-22 16:21:24 +0000520//===----------------------------------------------------------------------===//
521// __attribute__(nonnull) checking
522
Ted Kremenekc26a8b02009-07-22 21:46:56 +0000523class VISIBILITY_HIDDEN CheckAttrNonNull :
524 public CheckerVisitor<CheckAttrNonNull> {
525
Ted Kremenekcf118d42009-02-04 23:49:09 +0000526 BugType *BT;
Mike Stump1eb44332009-09-09 15:08:12 +0000527
Ted Kremenek78d46242008-07-22 16:21:24 +0000528public:
Ted Kremenekc26a8b02009-07-22 21:46:56 +0000529 CheckAttrNonNull() : BT(0) {}
Ted Kremenek31112182009-07-24 00:40:31 +0000530 ~CheckAttrNonNull() {}
Ted Kremenek78d46242008-07-22 16:21:24 +0000531
Ted Kremenekc26a8b02009-07-22 21:46:56 +0000532 const void *getTag() {
533 static int x = 0;
534 return &x;
535 }
536
537 void PreVisitCallExpr(CheckerContext &C, const CallExpr *CE) {
538 const GRState *state = C.getState();
539 const GRState *originalState = state;
Mike Stump1eb44332009-09-09 15:08:12 +0000540
Ted Kremenekc26a8b02009-07-22 21:46:56 +0000541 // Check if the callee has a 'nonnull' attribute.
Ted Kremenek23ec48c2009-06-18 23:58:37 +0000542 SVal X = state->getSVal(CE->getCallee());
Mike Stump1eb44332009-09-09 15:08:12 +0000543
Zhongxing Xu369f4472009-04-20 05:24:46 +0000544 const FunctionDecl* FD = X.getAsFunctionDecl();
545 if (!FD)
Ted Kremenekc26a8b02009-07-22 21:46:56 +0000546 return;
Zhongxing Xu369f4472009-04-20 05:24:46 +0000547
Mike Stump1eb44332009-09-09 15:08:12 +0000548 const NonNullAttr* Att = FD->getAttr<NonNullAttr>();
Ted Kremenek78d46242008-07-22 16:21:24 +0000549 if (!Att)
Ted Kremenekc26a8b02009-07-22 21:46:56 +0000550 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000551
Ted Kremenek78d46242008-07-22 16:21:24 +0000552 // Iterate through the arguments of CE and check them for null.
Ted Kremenek78d46242008-07-22 16:21:24 +0000553 unsigned idx = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000554
Ted Kremenekc26a8b02009-07-22 21:46:56 +0000555 for (CallExpr::const_arg_iterator I=CE->arg_begin(), E=CE->arg_end(); I!=E;
Ted Kremenek78d46242008-07-22 16:21:24 +0000556 ++I, ++idx) {
Mike Stump1eb44332009-09-09 15:08:12 +0000557
Ted Kremenekc26a8b02009-07-22 21:46:56 +0000558 if (!Att->isNonNull(idx))
559 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000560
Ted Kremenek08780072009-08-24 22:47:34 +0000561 const SVal &V = state->getSVal(*I);
562 const DefinedSVal *DV = dyn_cast<DefinedSVal>(&V);
Mike Stump1eb44332009-09-09 15:08:12 +0000563
Ted Kremenek08780072009-08-24 22:47:34 +0000564 if (!DV)
565 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000566
Ted Kremenekc26a8b02009-07-22 21:46:56 +0000567 ConstraintManager &CM = C.getConstraintManager();
568 const GRState *stateNotNull, *stateNull;
Ted Kremenek08780072009-08-24 22:47:34 +0000569 llvm::tie(stateNotNull, stateNull) = CM.AssumeDual(state, *DV);
Mike Stump1eb44332009-09-09 15:08:12 +0000570
Ted Kremenekc26a8b02009-07-22 21:46:56 +0000571 if (stateNull && !stateNotNull) {
572 // Generate an error node. Check for a null node in case
573 // we cache out.
Zhongxing Xu6403b572009-09-02 13:26:26 +0000574 if (ExplodedNode *errorNode = C.GenerateNode(CE, stateNull, true)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000575
Ted Kremenekc26a8b02009-07-22 21:46:56 +0000576 // Lazily allocate the BugType object if it hasn't already been
577 // created. Ownership is transferred to the BugReporter object once
578 // the BugReport is passed to 'EmitWarning'.
579 if (!BT)
580 BT = new BugType("Argument with 'nonnull' attribute passed null",
581 "API");
Mike Stump1eb44332009-09-09 15:08:12 +0000582
Ted Kremenek592362b2009-08-18 01:05:30 +0000583 EnhancedBugReport *R =
584 new EnhancedBugReport(*BT,
585 "Null pointer passed as an argument to a "
586 "'nonnull' parameter", errorNode);
Mike Stump1eb44332009-09-09 15:08:12 +0000587
Ted Kremenekc26a8b02009-07-22 21:46:56 +0000588 // Highlight the range of the argument that was null.
Ted Kremenek592362b2009-08-18 01:05:30 +0000589 const Expr *arg = *I;
590 R->addRange(arg->getSourceRange());
591 R->addVisitorCreator(registerTrackNullOrUndefValue, arg);
Mike Stump1eb44332009-09-09 15:08:12 +0000592
Ted Kremenekc26a8b02009-07-22 21:46:56 +0000593 // Emit the bug report.
594 C.EmitReport(R);
595 }
Mike Stump1eb44332009-09-09 15:08:12 +0000596
Ted Kremenekc26a8b02009-07-22 21:46:56 +0000597 // Always return. Either we cached out or we just emitted an error.
598 return;
599 }
Mike Stump1eb44332009-09-09 15:08:12 +0000600
Ted Kremenekc26a8b02009-07-22 21:46:56 +0000601 // If a pointer value passed the check we should assume that it is
602 // indeed not null from this point forward.
603 assert(stateNotNull);
604 state = stateNotNull;
605 }
Mike Stump1eb44332009-09-09 15:08:12 +0000606
Ted Kremenekc26a8b02009-07-22 21:46:56 +0000607 // If we reach here all of the arguments passed the nonnull check.
608 // If 'state' has been updated generated a new node.
609 if (state != originalState)
Zhongxing Xu6403b572009-09-02 13:26:26 +0000610 C.addTransition(C.GenerateNode(CE, state));
Ted Kremenek78d46242008-07-22 16:21:24 +0000611 }
Ted Kremenek78d46242008-07-22 16:21:24 +0000612};
613} // end anonymous namespace
614
Zhongxing Xu9a5bca32009-08-29 02:11:01 +0000615// Undefined arguments checking.
616namespace {
Mike Stump1eb44332009-09-09 15:08:12 +0000617class VISIBILITY_HIDDEN CheckUndefinedArg
Zhongxing Xu9a5bca32009-08-29 02:11:01 +0000618 : public CheckerVisitor<CheckUndefinedArg> {
619
Zhongxing Xu904e1e32009-09-02 07:09:39 +0000620 BadArg *BT;
Zhongxing Xu9a5bca32009-08-29 02:11:01 +0000621
622public:
623 CheckUndefinedArg() : BT(0) {}
624 ~CheckUndefinedArg() {}
625
626 const void *getTag() {
627 static int x = 0;
628 return &x;
629 }
630
631 void PreVisitCallExpr(CheckerContext &C, const CallExpr *CE);
632};
633
634void CheckUndefinedArg::PreVisitCallExpr(CheckerContext &C, const CallExpr *CE){
635 for (CallExpr::const_arg_iterator I = CE->arg_begin(), E = CE->arg_end();
636 I != E; ++I) {
637 if (C.getState()->getSVal(*I).isUndef()) {
Zhongxing Xu6403b572009-09-02 13:26:26 +0000638 if (ExplodedNode *ErrorNode = C.GenerateNode(CE, true)) {
Zhongxing Xu9a5bca32009-08-29 02:11:01 +0000639 if (!BT)
Zhongxing Xu904e1e32009-09-02 07:09:39 +0000640 BT = new BadArg();
Zhongxing Xu9a5bca32009-08-29 02:11:01 +0000641 // Generate a report for this bug.
Zhongxing Xu904e1e32009-09-02 07:09:39 +0000642 ArgReport *Report = new ArgReport(*BT, BT->getDescription().c_str(),
Zhongxing Xu9a5bca32009-08-29 02:11:01 +0000643 ErrorNode, *I);
644 Report->addRange((*I)->getSourceRange());
645 C.EmitReport(Report);
646 }
647 }
648 }
649}
650
Zhongxing Xud99f3612009-09-02 08:10:35 +0000651class VISIBILITY_HIDDEN CheckBadCall : public CheckerVisitor<CheckBadCall> {
652 BadCall *BT;
653
654public:
655 CheckBadCall() : BT(0) {}
656 ~CheckBadCall() {}
657
658 const void *getTag() {
659 static int x = 0;
660 return &x;
661 }
662
663 void PreVisitCallExpr(CheckerContext &C, const CallExpr *CE);
664};
665
666void CheckBadCall::PreVisitCallExpr(CheckerContext &C, const CallExpr *CE) {
667 const Expr *Callee = CE->getCallee()->IgnoreParens();
668 SVal L = C.getState()->getSVal(Callee);
669
670 if (L.isUndef() || isa<loc::ConcreteInt>(L)) {
Zhongxing Xu6403b572009-09-02 13:26:26 +0000671 if (ExplodedNode *N = C.GenerateNode(CE, true)) {
Zhongxing Xud99f3612009-09-02 08:10:35 +0000672 if (!BT)
673 BT = new BadCall();
674 C.EmitReport(new BuiltinBugReport(*BT, BT->getDescription().c_str(), N));
675 }
676 }
677}
678
Zhongxing Xu6403b572009-09-02 13:26:26 +0000679class VISIBILITY_HIDDEN CheckBadDiv : public CheckerVisitor<CheckBadDiv> {
680 DivZero *BT;
681public:
682 CheckBadDiv() : BT(0) {}
683 ~CheckBadDiv() {}
684
685 const void *getTag() {
686 static int x;
687 return &x;
688 }
689
690 void PreVisitBinaryOperator(CheckerContext &C, const BinaryOperator *B);
691};
692
Mike Stump1eb44332009-09-09 15:08:12 +0000693void CheckBadDiv::PreVisitBinaryOperator(CheckerContext &C,
Zhongxing Xu6403b572009-09-02 13:26:26 +0000694 const BinaryOperator *B) {
695 BinaryOperator::Opcode Op = B->getOpcode();
696 if (Op != BinaryOperator::Div &&
697 Op != BinaryOperator::Rem &&
698 Op != BinaryOperator::DivAssign &&
699 Op != BinaryOperator::RemAssign)
700 return;
701
702 if (!B->getRHS()->getType()->isIntegerType() ||
703 !B->getRHS()->getType()->isScalarType())
704 return;
705
Zhongxing Xu6403b572009-09-02 13:26:26 +0000706 // Check for divide by undefined.
707 SVal Denom = C.getState()->getSVal(B->getRHS());
708
709 if (Denom.isUndef()) {
710 if (ExplodedNode *N = C.GenerateNode(B, true)) {
711 if (!BT)
712 BT = new DivZero();
713
714 C.EmitReport(new BuiltinBugReport(*BT, BT->getDescription().c_str(), N));
715 }
716 return;
717 }
718
Ted Kremenek970e03a2009-09-03 01:48:03 +0000719 // Handle the case where 'Denom' is UnknownVal.
720 const DefinedSVal *DV = dyn_cast<DefinedSVal>(&Denom);
721
Mike Stump1eb44332009-09-09 15:08:12 +0000722 if (!DV)
Ted Kremenek970e03a2009-09-03 01:48:03 +0000723 return;
724
Zhongxing Xu6403b572009-09-02 13:26:26 +0000725 // Check for divide by zero.
726 ConstraintManager &CM = C.getConstraintManager();
727 const GRState *stateNotZero, *stateZero;
Ted Kremenek970e03a2009-09-03 01:48:03 +0000728 llvm::tie(stateNotZero, stateZero) = CM.AssumeDual(C.getState(), *DV);
Mike Stump1eb44332009-09-09 15:08:12 +0000729
Zhongxing Xu6403b572009-09-02 13:26:26 +0000730 if (stateZero && !stateNotZero) {
731 if (ExplodedNode *N = C.GenerateNode(B, stateZero, true)) {
732 if (!BT)
733 BT = new DivZero();
Mike Stump1eb44332009-09-09 15:08:12 +0000734
Zhongxing Xu6403b572009-09-02 13:26:26 +0000735 C.EmitReport(new BuiltinBugReport(*BT, BT->getDescription().c_str(), N));
736 }
737 return;
738 }
739
740 // If we get here, then the denom should not be zero.
741 if (stateNotZero != C.getState())
742 C.addTransition(C.GenerateNode(B, stateNotZero));
743}
Zhongxing Xu9a5bca32009-08-29 02:11:01 +0000744}
Ted Kremenek78d46242008-07-22 16:21:24 +0000745//===----------------------------------------------------------------------===//
746// Check registration.
Ted Kremenekcf118d42009-02-04 23:49:09 +0000747//===----------------------------------------------------------------------===//
Ted Kremenek78d46242008-07-22 16:21:24 +0000748
749void GRExprEngine::RegisterInternalChecks() {
Ted Kremenekcf118d42009-02-04 23:49:09 +0000750 // Register internal "built-in" BugTypes with the BugReporter. These BugTypes
751 // are different than what probably many checks will do since they don't
752 // create BugReports on-the-fly but instead wait until GRExprEngine finishes
753 // analyzing a function. Generation of BugReport objects is done via a call
754 // to 'FlushReports' from BugReporter.
755 BR.Register(new NullDeref(this));
756 BR.Register(new UndefinedDeref(this));
757 BR.Register(new UndefBranch(this));
Ted Kremenekcf118d42009-02-04 23:49:09 +0000758 BR.Register(new UndefResult(this));
Ted Kremenekcf118d42009-02-04 23:49:09 +0000759 BR.Register(new RetStack(this));
760 BR.Register(new RetUndef(this));
Ted Kremenekcf118d42009-02-04 23:49:09 +0000761 BR.Register(new BadMsgExprArg(this));
762 BR.Register(new BadReceiver(this));
763 BR.Register(new OutOfBoundMemoryAccess(this));
764 BR.Register(new BadSizeVLA(this));
Ted Kremenek21fe8372009-02-19 04:06:22 +0000765 BR.Register(new NilReceiverStructRet(this));
Ted Kremenek899b3de2009-04-08 03:07:17 +0000766 BR.Register(new NilReceiverLargerThanVoidPtrRet(this));
Mike Stump1eb44332009-09-09 15:08:12 +0000767
Ted Kremenekcf118d42009-02-04 23:49:09 +0000768 // The following checks do not need to have their associated BugTypes
769 // explicitly registered with the BugReporter. If they issue any BugReports,
770 // their associated BugType will get registered with the BugReporter
771 // automatically. Note that the check itself is owned by the GRExprEngine
772 // object.
Ted Kremenekc26a8b02009-07-22 21:46:56 +0000773 registerCheck(new CheckAttrNonNull());
Zhongxing Xu9a5bca32009-08-29 02:11:01 +0000774 registerCheck(new CheckUndefinedArg());
Zhongxing Xud99f3612009-09-02 08:10:35 +0000775 registerCheck(new CheckBadCall());
Zhongxing Xu6403b572009-09-02 13:26:26 +0000776 registerCheck(new CheckBadDiv());
Ted Kremenek78d46242008-07-22 16:21:24 +0000777}