blob: e0112ee9843adfec0fe3fba3884a62ffebdf591c [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 Kremenekdc998c12009-11-03 18:41:06 +000018#include "clang/Analysis/PathSensitive/Checkers/DereferenceChecker.h"
Zhongxing Xu9e56d232009-10-31 10:02:37 +000019#include "clang/Analysis/PathSensitive/Checkers/DivZeroChecker.h"
Zhongxing Xu4f64e5f2009-11-03 05:48:04 +000020#include "clang/Analysis/PathSensitive/Checkers/BadCallChecker.h"
Zhongxing Xu8958fff2009-11-03 06:46:03 +000021#include "clang/Analysis/PathSensitive/Checkers/UndefinedArgChecker.h"
Zhongxing Xu94943b62009-11-03 07:35:33 +000022#include "clang/Analysis/PathSensitive/Checkers/AttrNonNullChecker.h"
Zhongxing Xu05a23382009-11-04 01:43:07 +000023#include "clang/Analysis/PathSensitive/Checkers/VLASizeChecker.h"
Ted Kremenekdd986cc2009-05-07 00:45:33 +000024#include "clang/Analysis/PathDiagnostic.h"
Ted Kremenek8aed8062008-10-31 00:13:20 +000025#include "clang/Basic/SourceManager.h"
Ted Kremenek78d46242008-07-22 16:21:24 +000026#include "llvm/Support/Compiler.h"
Ted Kremenekad51a602008-10-31 00:18:30 +000027#include "llvm/Support/raw_ostream.h"
Ted Kremenek78d46242008-07-22 16:21:24 +000028
29using namespace clang;
Ted Kremenek53500662009-07-22 17:55:28 +000030using namespace clang::bugreporter;
Ted Kremenek78d46242008-07-22 16:21:24 +000031
32//===----------------------------------------------------------------------===//
33// Utility functions.
34//===----------------------------------------------------------------------===//
35
36template <typename ITERATOR> inline
Zhongxing Xuc5619d92009-08-06 01:32:16 +000037ExplodedNode* GetNode(ITERATOR I) {
Ted Kremenek78d46242008-07-22 16:21:24 +000038 return *I;
39}
40
41template <> inline
Zhongxing Xuc5619d92009-08-06 01:32:16 +000042ExplodedNode* GetNode(GRExprEngine::undef_arg_iterator I) {
Ted Kremenek78d46242008-07-22 16:21:24 +000043 return I->first;
44}
45
46//===----------------------------------------------------------------------===//
47// Bug Descriptions.
48//===----------------------------------------------------------------------===//
Zhongxing Xuec9227f2009-10-29 02:09:30 +000049namespace clang {
50class BuiltinBugReport : public RangedBugReport {
Ted Kremenekdd986cc2009-05-07 00:45:33 +000051public:
52 BuiltinBugReport(BugType& bt, const char* desc,
Zhongxing Xuc5619d92009-08-06 01:32:16 +000053 ExplodedNode *n)
Ted Kremenek0c313172009-05-13 19:16:35 +000054 : RangedBugReport(bt, desc, n) {}
Mike Stump1eb44332009-09-09 15:08:12 +000055
Ted Kremenek85ac9342009-05-15 05:25:09 +000056 BuiltinBugReport(BugType& bt, const char *shortDesc, const char *desc,
Zhongxing Xuc5619d92009-08-06 01:32:16 +000057 ExplodedNode *n)
Mike Stump1eb44332009-09-09 15:08:12 +000058 : RangedBugReport(bt, shortDesc, desc, n) {}
59
Ted Kremenekdd986cc2009-05-07 00:45:33 +000060 void registerInitialVisitors(BugReporterContext& BRC,
Zhongxing Xuc5619d92009-08-06 01:32:16 +000061 const ExplodedNode* N);
Mike Stump1eb44332009-09-09 15:08:12 +000062};
63
Zhongxing Xuec9227f2009-10-29 02:09:30 +000064void BuiltinBugReport::registerInitialVisitors(BugReporterContext& BRC,
65 const ExplodedNode* N) {
66 static_cast<BuiltinBug&>(getBugType()).registerInitialVisitors(BRC, N, this);
67}
Mike Stump1eb44332009-09-09 15:08:12 +000068
Ted Kremenekdd986cc2009-05-07 00:45:33 +000069template <typename ITER>
70void BuiltinBug::Emit(BugReporter& BR, ITER I, ITER E) {
71 for (; I != E; ++I) BR.EmitReport(new BuiltinBugReport(*this, desc.c_str(),
72 GetNode(I)));
Mike Stump1eb44332009-09-09 15:08:12 +000073}
Mike Stump1eb44332009-09-09 15:08:12 +000074
Ted Kremenek0c313172009-05-13 19:16:35 +000075class VISIBILITY_HIDDEN NilReceiverStructRet : public BuiltinBug {
Ted Kremenek21fe8372009-02-19 04:06:22 +000076public:
77 NilReceiverStructRet(GRExprEngine* eng) :
Ted Kremenek0c313172009-05-13 19:16:35 +000078 BuiltinBug(eng, "'nil' receiver with struct return type") {}
Ted Kremenek21fe8372009-02-19 04:06:22 +000079
Ted Kremenek0c313172009-05-13 19:16:35 +000080 void FlushReportsImpl(BugReporter& BR, GRExprEngine& Eng) {
Ted Kremenek21fe8372009-02-19 04:06:22 +000081 for (GRExprEngine::nil_receiver_struct_ret_iterator
82 I=Eng.nil_receiver_struct_ret_begin(),
83 E=Eng.nil_receiver_struct_ret_end(); I!=E; ++I) {
84
85 std::string sbuf;
86 llvm::raw_string_ostream os(sbuf);
87 PostStmt P = cast<PostStmt>((*I)->getLocation());
Ted Kremenek5f85e172009-07-22 22:35:28 +000088 const ObjCMessageExpr *ME = cast<ObjCMessageExpr>(P.getStmt());
Ted Kremenek21fe8372009-02-19 04:06:22 +000089 os << "The receiver in the message expression is 'nil' and results in the"
90 " returned value (of type '"
91 << ME->getType().getAsString()
Ted Kremenek5b9bd212009-09-11 22:07:28 +000092 << "') to be garbage or otherwise undefined";
Ted Kremenek21fe8372009-02-19 04:06:22 +000093
Ted Kremenek0c313172009-05-13 19:16:35 +000094 BuiltinBugReport *R = new BuiltinBugReport(*this, os.str().c_str(), *I);
Ted Kremenek21fe8372009-02-19 04:06:22 +000095 R->addRange(ME->getReceiver()->getSourceRange());
96 BR.EmitReport(R);
97 }
98 }
Mike Stump1eb44332009-09-09 15:08:12 +000099
Ted Kremenek0c313172009-05-13 19:16:35 +0000100 void registerInitialVisitors(BugReporterContext& BRC,
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000101 const ExplodedNode* N,
Ted Kremenek0c313172009-05-13 19:16:35 +0000102 BuiltinBugReport *R) {
Ted Kremenek85ac9342009-05-15 05:25:09 +0000103 registerTrackNullOrUndefValue(BRC, GetReceiverExpr(N), N);
Ted Kremenek0c313172009-05-13 19:16:35 +0000104 }
Ted Kremenek21fe8372009-02-19 04:06:22 +0000105};
Ted Kremenek899b3de2009-04-08 03:07:17 +0000106
Ted Kremenek0c313172009-05-13 19:16:35 +0000107class VISIBILITY_HIDDEN NilReceiverLargerThanVoidPtrRet : public BuiltinBug {
Ted Kremenek899b3de2009-04-08 03:07:17 +0000108public:
109 NilReceiverLargerThanVoidPtrRet(GRExprEngine* eng) :
Ted Kremenek0c313172009-05-13 19:16:35 +0000110 BuiltinBug(eng,
111 "'nil' receiver with return type larger than sizeof(void *)") {}
Mike Stump1eb44332009-09-09 15:08:12 +0000112
Ted Kremenek0c313172009-05-13 19:16:35 +0000113 void FlushReportsImpl(BugReporter& BR, GRExprEngine& Eng) {
Ted Kremenek899b3de2009-04-08 03:07:17 +0000114 for (GRExprEngine::nil_receiver_larger_than_voidptr_ret_iterator
115 I=Eng.nil_receiver_larger_than_voidptr_ret_begin(),
116 E=Eng.nil_receiver_larger_than_voidptr_ret_end(); I!=E; ++I) {
Mike Stump1eb44332009-09-09 15:08:12 +0000117
Ted Kremenek899b3de2009-04-08 03:07:17 +0000118 std::string sbuf;
119 llvm::raw_string_ostream os(sbuf);
120 PostStmt P = cast<PostStmt>((*I)->getLocation());
Ted Kremenek5f85e172009-07-22 22:35:28 +0000121 const ObjCMessageExpr *ME = cast<ObjCMessageExpr>(P.getStmt());
Ted Kremenek899b3de2009-04-08 03:07:17 +0000122 os << "The receiver in the message expression is 'nil' and results in the"
123 " returned value (of type '"
124 << ME->getType().getAsString()
125 << "' and of size "
126 << Eng.getContext().getTypeSize(ME->getType()) / 8
Ted Kremenek5b9bd212009-09-11 22:07:28 +0000127 << " bytes) to be garbage or otherwise undefined";
Mike Stump1eb44332009-09-09 15:08:12 +0000128
Ted Kremenek0c313172009-05-13 19:16:35 +0000129 BuiltinBugReport *R = new BuiltinBugReport(*this, os.str().c_str(), *I);
Ted Kremenek899b3de2009-04-08 03:07:17 +0000130 R->addRange(ME->getReceiver()->getSourceRange());
131 BR.EmitReport(R);
132 }
Mike Stump1eb44332009-09-09 15:08:12 +0000133 }
Ted Kremenek0c313172009-05-13 19:16:35 +0000134 void registerInitialVisitors(BugReporterContext& BRC,
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000135 const ExplodedNode* N,
Ted Kremenek0c313172009-05-13 19:16:35 +0000136 BuiltinBugReport *R) {
Ted Kremenek85ac9342009-05-15 05:25:09 +0000137 registerTrackNullOrUndefValue(BRC, GetReceiverExpr(N), N);
Ted Kremenek899b3de2009-04-08 03:07:17 +0000138 }
139};
Mike Stump1eb44332009-09-09 15:08:12 +0000140
Ted Kremenek78d46242008-07-22 16:21:24 +0000141class VISIBILITY_HIDDEN UndefResult : public BuiltinBug {
142public:
Ted Kremenek5b9bd212009-09-11 22:07:28 +0000143 UndefResult(GRExprEngine* eng)
144 : BuiltinBug(eng,"Undefined or garbage result",
145 "Result of operation is garbage or undefined") {}
Mike Stump1eb44332009-09-09 15:08:12 +0000146
Ted Kremenekcf118d42009-02-04 23:49:09 +0000147 void FlushReportsImpl(BugReporter& BR, GRExprEngine& Eng) {
Ted Kremenek5b9bd212009-09-11 22:07:28 +0000148 for (GRExprEngine::undef_result_iterator I=Eng.undef_results_begin(),
149 E = Eng.undef_results_end(); I!=E; ++I) {
150
151 ExplodedNode *N = *I;
152 const Stmt *S = N->getLocationAs<PostStmt>()->getStmt();
153 BuiltinBugReport *report = NULL;
154
155 if (const BinaryOperator *B = dyn_cast<BinaryOperator>(S)) {
156 llvm::SmallString<256> sbuf;
157 llvm::raw_svector_ostream OS(sbuf);
158 const GRState *ST = N->getState();
Ted Kremenek7c039bf2009-09-16 06:04:26 +0000159 const Expr *Ex = NULL;
160 bool isLeft = true;
Ted Kremenek5b9bd212009-09-11 22:07:28 +0000161
162 if (ST->getSVal(B->getLHS()).isUndef()) {
163 Ex = B->getLHS()->IgnoreParenCasts();
Ted Kremenek7c039bf2009-09-16 06:04:26 +0000164 isLeft = true;
Ted Kremenek5b9bd212009-09-11 22:07:28 +0000165 }
Ted Kremenek24c411b2009-09-15 17:43:54 +0000166 else if (ST->getSVal(B->getRHS()).isUndef()) {
Ted Kremenek5b9bd212009-09-11 22:07:28 +0000167 Ex = B->getRHS()->IgnoreParenCasts();
Ted Kremenek7c039bf2009-09-16 06:04:26 +0000168 isLeft = false;
Ted Kremenek5b9bd212009-09-11 22:07:28 +0000169 }
Ted Kremenek7c039bf2009-09-16 06:04:26 +0000170
171 if (Ex) {
172 OS << "The " << (isLeft ? "left" : "right")
Ted Kremenek112ba7e2009-09-24 00:44:26 +0000173 << " operand of '"
Ted Kremenek7c039bf2009-09-16 06:04:26 +0000174 << BinaryOperator::getOpcodeStr(B->getOpcode())
Ted Kremenek112ba7e2009-09-24 00:44:26 +0000175 << "' is a garbage value";
Ted Kremenek24c411b2009-09-15 17:43:54 +0000176 }
177 else {
Ted Kremenek7c039bf2009-09-16 06:04:26 +0000178 // Neither operand was undefined, but the result is undefined.
Ted Kremenek24c411b2009-09-15 17:43:54 +0000179 OS << "The result of the '"
180 << BinaryOperator::getOpcodeStr(B->getOpcode())
181 << "' expression is undefined";
182 }
183
Ted Kremenek5b9bd212009-09-11 22:07:28 +0000184 // FIXME: Use StringRefs to pass string information.
185 report = new BuiltinBugReport(*this, OS.str().str().c_str(), N);
Ted Kremenek24c411b2009-09-15 17:43:54 +0000186 if (Ex) report->addRange(Ex->getSourceRange());
Ted Kremenek5b9bd212009-09-11 22:07:28 +0000187 }
188 else {
189 report = new BuiltinBugReport(*this,
190 "Expression evaluates to an uninitialized"
191 " or undefined value", N);
192 }
193
194 BR.EmitReport(report);
195 }
196 }
197
198 void registerInitialVisitors(BugReporterContext& BRC,
199 const ExplodedNode* N,
200 BuiltinBugReport *R) {
201
202 const Stmt *S = N->getLocationAs<StmtPoint>()->getStmt();
203 const Stmt *X = S;
204
205 if (const BinaryOperator *B = dyn_cast<BinaryOperator>(S)) {
206 const GRState *ST = N->getState();
Ted Kremenek7c039bf2009-09-16 06:04:26 +0000207 if (ST->getSVal(B->getLHS()).isUndef())
208 X = B->getLHS();
209 else if (ST->getSVal(B->getRHS()).isUndef())
210 X = B->getRHS();
Ted Kremenek5b9bd212009-09-11 22:07:28 +0000211 }
212
213 registerTrackNullOrUndefValue(BRC, X, N);
Ted Kremenek78d46242008-07-22 16:21:24 +0000214 }
215};
Mike Stump1eb44332009-09-09 15:08:12 +0000216
Ted Kremenek85ac9342009-05-15 05:25:09 +0000217class VISIBILITY_HIDDEN ArgReport : public BuiltinBugReport {
218 const Stmt *Arg;
219public:
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000220 ArgReport(BugType& bt, const char* desc, ExplodedNode *n,
Ted Kremenek85ac9342009-05-15 05:25:09 +0000221 const Stmt *arg)
222 : BuiltinBugReport(bt, desc, n), Arg(arg) {}
Mike Stump1eb44332009-09-09 15:08:12 +0000223
Ted Kremenek85ac9342009-05-15 05:25:09 +0000224 ArgReport(BugType& bt, const char *shortDesc, const char *desc,
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000225 ExplodedNode *n, const Stmt *arg)
Mike Stump1eb44332009-09-09 15:08:12 +0000226 : BuiltinBugReport(bt, shortDesc, desc, n), Arg(arg) {}
227
228 const Stmt *getArg() const { return Arg; }
Ted Kremenek78d46242008-07-22 16:21:24 +0000229};
230
231class VISIBILITY_HIDDEN BadArg : public BuiltinBug {
Mike Stump1eb44332009-09-09 15:08:12 +0000232public:
233 BadArg(GRExprEngine* eng=0) : BuiltinBug(eng,"Uninitialized argument",
Ted Kremenek5b9bd212009-09-11 22:07:28 +0000234 "Pass-by-value argument in function call is undefined") {}
Ted Kremenek78d46242008-07-22 16:21:24 +0000235
Ted Kremenekcf118d42009-02-04 23:49:09 +0000236 BadArg(GRExprEngine* eng, const char* d)
Ted Kremenek5d88ff82009-04-02 02:40:26 +0000237 : BuiltinBug(eng,"Uninitialized argument", d) {}
Mike Stump1eb44332009-09-09 15:08:12 +0000238
Ted Kremenek85ac9342009-05-15 05:25:09 +0000239 void registerInitialVisitors(BugReporterContext& BRC,
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000240 const ExplodedNode* N,
Ted Kremenek85ac9342009-05-15 05:25:09 +0000241 BuiltinBugReport *R) {
242 registerTrackNullOrUndefValue(BRC, static_cast<ArgReport*>(R)->getArg(),
243 N);
Mike Stump1eb44332009-09-09 15:08:12 +0000244 }
Ted Kremenek78d46242008-07-22 16:21:24 +0000245};
Mike Stump1eb44332009-09-09 15:08:12 +0000246
Ted Kremenek78d46242008-07-22 16:21:24 +0000247class VISIBILITY_HIDDEN BadMsgExprArg : public BadArg {
248public:
Mike Stump1eb44332009-09-09 15:08:12 +0000249 BadMsgExprArg(GRExprEngine* eng)
Ted Kremenek5d88ff82009-04-02 02:40:26 +0000250 : BadArg(eng,"Pass-by-value argument in message expression is undefined"){}
Mike Stump1eb44332009-09-09 15:08:12 +0000251
Ted Kremenekcf118d42009-02-04 23:49:09 +0000252 void FlushReportsImpl(BugReporter& BR, GRExprEngine& Eng) {
Ted Kremenek78d46242008-07-22 16:21:24 +0000253 for (GRExprEngine::UndefArgsTy::iterator I=Eng.msg_expr_undef_arg_begin(),
Mike Stump1eb44332009-09-09 15:08:12 +0000254 E = Eng.msg_expr_undef_arg_end(); I!=E; ++I) {
Ted Kremenek78d46242008-07-22 16:21:24 +0000255 // Generate a report for this bug.
Ted Kremenek85ac9342009-05-15 05:25:09 +0000256 ArgReport *report = new ArgReport(*this, desc.c_str(), I->first,
257 I->second);
Ted Kremenekcf118d42009-02-04 23:49:09 +0000258 report->addRange(I->second->getSourceRange());
259 BR.EmitReport(report);
Mike Stump1eb44332009-09-09 15:08:12 +0000260 }
261 }
Ted Kremenek78d46242008-07-22 16:21:24 +0000262};
Mike Stump1eb44332009-09-09 15:08:12 +0000263
Ted Kremenek78d46242008-07-22 16:21:24 +0000264class VISIBILITY_HIDDEN BadReceiver : public BuiltinBug {
Mike Stump1eb44332009-09-09 15:08:12 +0000265public:
Ted Kremenekcf118d42009-02-04 23:49:09 +0000266 BadReceiver(GRExprEngine* eng)
Ted Kremenek5d88ff82009-04-02 02:40:26 +0000267 : BuiltinBug(eng,"Uninitialized receiver",
268 "Receiver in message expression is an uninitialized value") {}
Mike Stump1eb44332009-09-09 15:08:12 +0000269
Ted Kremenekcf118d42009-02-04 23:49:09 +0000270 void FlushReportsImpl(BugReporter& BR, GRExprEngine& Eng) {
Ted Kremenekefd59942008-12-08 22:47:34 +0000271 for (GRExprEngine::ErrorNodes::iterator I=Eng.undef_receivers_begin(),
Ted Kremenek78d46242008-07-22 16:21:24 +0000272 End = Eng.undef_receivers_end(); I!=End; ++I) {
Mike Stump1eb44332009-09-09 15:08:12 +0000273
Ted Kremenek78d46242008-07-22 16:21:24 +0000274 // Generate a report for this bug.
Ted Kremenek0c313172009-05-13 19:16:35 +0000275 BuiltinBugReport *report = new BuiltinBugReport(*this, desc.c_str(), *I);
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000276 ExplodedNode* N = *I;
Ted Kremenek5f85e172009-07-22 22:35:28 +0000277 const Stmt *S = cast<PostStmt>(N->getLocation()).getStmt();
278 const Expr* E = cast<ObjCMessageExpr>(S)->getReceiver();
Ted Kremenek78d46242008-07-22 16:21:24 +0000279 assert (E && "Receiver cannot be NULL");
Ted Kremenekcf118d42009-02-04 23:49:09 +0000280 report->addRange(E->getSourceRange());
281 BR.EmitReport(report);
Ted Kremenek0c313172009-05-13 19:16:35 +0000282 }
283 }
Ted Kremenek85ac9342009-05-15 05:25:09 +0000284
Ted Kremenek0c313172009-05-13 19:16:35 +0000285 void registerInitialVisitors(BugReporterContext& BRC,
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000286 const ExplodedNode* N,
Ted Kremenek0c313172009-05-13 19:16:35 +0000287 BuiltinBugReport *R) {
Ted Kremenek85ac9342009-05-15 05:25:09 +0000288 registerTrackNullOrUndefValue(BRC, GetReceiverExpr(N), N);
Mike Stump1eb44332009-09-09 15:08:12 +0000289 }
Ted Kremenek78d46242008-07-22 16:21:24 +0000290};
Ted Kremenek5917d782008-11-21 00:27:44 +0000291
Ted Kremenek78d46242008-07-22 16:21:24 +0000292class VISIBILITY_HIDDEN RetStack : public BuiltinBug {
293public:
Ted Kremenek17a8e072009-03-01 05:43:22 +0000294 RetStack(GRExprEngine* eng)
Ted Kremenek5d88ff82009-04-02 02:40:26 +0000295 : BuiltinBug(eng, "Return of address to stack-allocated memory") {}
Mike Stump1eb44332009-09-09 15:08:12 +0000296
Ted Kremenekcf118d42009-02-04 23:49:09 +0000297 void FlushReportsImpl(BugReporter& BR, GRExprEngine& Eng) {
Ted Kremenekb7714b22008-07-30 17:49:12 +0000298 for (GRExprEngine::ret_stackaddr_iterator I=Eng.ret_stackaddr_begin(),
299 End = Eng.ret_stackaddr_end(); I!=End; ++I) {
Ted Kremenek22bda882008-07-31 20:31:27 +0000300
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000301 ExplodedNode* N = *I;
Ted Kremenek5f85e172009-07-22 22:35:28 +0000302 const Stmt *S = cast<PostStmt>(N->getLocation()).getStmt();
303 const Expr* E = cast<ReturnStmt>(S)->getRetValue();
304 assert(E && "Return expression cannot be NULL");
Mike Stump1eb44332009-09-09 15:08:12 +0000305
Ted Kremenek22bda882008-07-31 20:31:27 +0000306 // Get the value associated with E.
Ted Kremenek23ec48c2009-06-18 23:58:37 +0000307 loc::MemRegionVal V = cast<loc::MemRegionVal>(N->getState()->getSVal(E));
Mike Stump1eb44332009-09-09 15:08:12 +0000308
Ted Kremenek22bda882008-07-31 20:31:27 +0000309 // Generate a report for this bug.
Ted Kremenekad51a602008-10-31 00:18:30 +0000310 std::string buf;
311 llvm::raw_string_ostream os(buf);
Ted Kremenek8aed8062008-10-31 00:13:20 +0000312 SourceRange R;
Mike Stump1eb44332009-09-09 15:08:12 +0000313
Ted Kremenek8aed8062008-10-31 00:13:20 +0000314 // Check if the region is a compound literal.
Mike Stump1eb44332009-09-09 15:08:12 +0000315 if (const CompoundLiteralRegion* CR =
Ted Kremenek8aed8062008-10-31 00:13:20 +0000316 dyn_cast<CompoundLiteralRegion>(V.getRegion())) {
Mike Stump1eb44332009-09-09 15:08:12 +0000317
Ted Kremenek8aed8062008-10-31 00:13:20 +0000318 const CompoundLiteralExpr* CL = CR->getLiteralExpr();
319 os << "Address of stack memory associated with a compound literal "
320 "declared on line "
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000321 << BR.getSourceManager()
322 .getInstantiationLineNumber(CL->getLocStart())
Ted Kremenek8aed8062008-10-31 00:13:20 +0000323 << " returned.";
Mike Stump1eb44332009-09-09 15:08:12 +0000324
Ted Kremenek8aed8062008-10-31 00:13:20 +0000325 R = CL->getSourceRange();
326 }
Ted Kremenekde8cd192008-11-02 00:35:25 +0000327 else if (const AllocaRegion* AR = dyn_cast<AllocaRegion>(V.getRegion())) {
328 const Expr* ARE = AR->getExpr();
329 SourceLocation L = ARE->getLocStart();
330 R = ARE->getSourceRange();
Mike Stump1eb44332009-09-09 15:08:12 +0000331
Ted Kremenekde8cd192008-11-02 00:35:25 +0000332 os << "Address of stack memory allocated by call to alloca() on line "
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000333 << BR.getSourceManager().getInstantiationLineNumber(L)
Ted Kremenekde8cd192008-11-02 00:35:25 +0000334 << " returned.";
Mike Stump1eb44332009-09-09 15:08:12 +0000335 }
336 else {
Ted Kremenek8aed8062008-10-31 00:13:20 +0000337 os << "Address of stack memory associated with local variable '"
338 << V.getRegion()->getString() << "' returned.";
339 }
Mike Stump1eb44332009-09-09 15:08:12 +0000340
Ted Kremenekcf118d42009-02-04 23:49:09 +0000341 RangedBugReport *report = new RangedBugReport(*this, os.str().c_str(), N);
342 report->addRange(E->getSourceRange());
343 if (R.isValid()) report->addRange(R);
344 BR.EmitReport(report);
Ted Kremenekb7714b22008-07-30 17:49:12 +0000345 }
Ted Kremenek78d46242008-07-22 16:21:24 +0000346 }
347};
Mike Stump1eb44332009-09-09 15:08:12 +0000348
Ted Kremenek5917d782008-11-21 00:27:44 +0000349class VISIBILITY_HIDDEN RetUndef : public BuiltinBug {
350public:
Ted Kremenek5b9bd212009-09-11 22:07:28 +0000351 RetUndef(GRExprEngine* eng) : BuiltinBug(eng, "Garbage return value",
352 "Undefined or garbage value returned to caller") {}
Mike Stump1eb44332009-09-09 15:08:12 +0000353
Ted Kremenekcf118d42009-02-04 23:49:09 +0000354 void FlushReportsImpl(BugReporter& BR, GRExprEngine& Eng) {
Ted Kremenek5917d782008-11-21 00:27:44 +0000355 Emit(BR, Eng.ret_undef_begin(), Eng.ret_undef_end());
356 }
Mike Stump1eb44332009-09-09 15:08:12 +0000357
Ted Kremenek0c313172009-05-13 19:16:35 +0000358 void registerInitialVisitors(BugReporterContext& BRC,
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000359 const ExplodedNode* N,
Ted Kremenek0c313172009-05-13 19:16:35 +0000360 BuiltinBugReport *R) {
Ted Kremenek85ac9342009-05-15 05:25:09 +0000361 registerTrackNullOrUndefValue(BRC, GetRetValExpr(N), N);
Mike Stump1eb44332009-09-09 15:08:12 +0000362 }
Ted Kremenek5917d782008-11-21 00:27:44 +0000363};
Ted Kremenek78d46242008-07-22 16:21:24 +0000364
365class VISIBILITY_HIDDEN UndefBranch : public BuiltinBug {
366 struct VISIBILITY_HIDDEN FindUndefExpr {
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000367 GRStateManager& VM;
368 const GRState* St;
Mike Stump1eb44332009-09-09 15:08:12 +0000369
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000370 FindUndefExpr(GRStateManager& V, const GRState* S) : VM(V), St(S) {}
Mike Stump1eb44332009-09-09 15:08:12 +0000371
372 Expr* FindExpr(Expr* Ex) {
Ted Kremenek78d46242008-07-22 16:21:24 +0000373 if (!MatchesCriteria(Ex))
Ted Kremenekb7714b22008-07-30 17:49:12 +0000374 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000375
Ted Kremenekb7714b22008-07-30 17:49:12 +0000376 for (Stmt::child_iterator I=Ex->child_begin(), E=Ex->child_end();I!=E;++I)
Ted Kremenek78d46242008-07-22 16:21:24 +0000377 if (Expr* ExI = dyn_cast_or_null<Expr>(*I)) {
378 Expr* E2 = FindExpr(ExI);
379 if (E2) return E2;
380 }
Mike Stump1eb44332009-09-09 15:08:12 +0000381
Ted Kremenek78d46242008-07-22 16:21:24 +0000382 return Ex;
383 }
Mike Stump1eb44332009-09-09 15:08:12 +0000384
Ted Kremenek23ec48c2009-06-18 23:58:37 +0000385 bool MatchesCriteria(Expr* Ex) { return St->getSVal(Ex).isUndef(); }
Ted Kremenek78d46242008-07-22 16:21:24 +0000386 };
Mike Stump1eb44332009-09-09 15:08:12 +0000387
Ted Kremenek78d46242008-07-22 16:21:24 +0000388public:
Ted Kremenekcf118d42009-02-04 23:49:09 +0000389 UndefBranch(GRExprEngine *eng)
Ted Kremenek5b9bd212009-09-11 22:07:28 +0000390 : BuiltinBug(eng,"Use of garbage value",
391 "Branch condition evaluates to an undefined or garbage value")
392 {}
Mike Stump1eb44332009-09-09 15:08:12 +0000393
Ted Kremenekcf118d42009-02-04 23:49:09 +0000394 void FlushReportsImpl(BugReporter& BR, GRExprEngine& Eng) {
Ted Kremenek78d46242008-07-22 16:21:24 +0000395 for (GRExprEngine::undef_branch_iterator I=Eng.undef_branches_begin(),
396 E=Eng.undef_branches_end(); I!=E; ++I) {
397
398 // What's going on here: we want to highlight the subexpression of the
399 // condition that is the most likely source of the "uninitialized
400 // branch condition." We do a recursive walk of the condition's
401 // subexpressions and roughly look for the most nested subexpression
402 // that binds to Undefined. We then highlight that expression's range.
Ted Kremenek78d46242008-07-22 16:21:24 +0000403 BlockEdge B = cast<BlockEdge>((*I)->getLocation());
404 Expr* Ex = cast<Expr>(B.getSrc()->getTerminatorCondition());
405 assert (Ex && "Block must have a terminator.");
406
407 // Get the predecessor node and check if is a PostStmt with the Stmt
408 // being the terminator condition. We want to inspect the state
409 // of that node instead because it will contain main information about
410 // the subexpressions.
Ted Kremenek78d46242008-07-22 16:21:24 +0000411 assert (!(*I)->pred_empty());
412
413 // Note: any predecessor will do. They should have identical state,
414 // since all the BlockEdge did was act as an error sink since the value
415 // had to already be undefined.
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000416 ExplodedNode *N = *(*I)->pred_begin();
Ted Kremenek78d46242008-07-22 16:21:24 +0000417 ProgramPoint P = N->getLocation();
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000418 const GRState* St = (*I)->getState();
Ted Kremenek78d46242008-07-22 16:21:24 +0000419
420 if (PostStmt* PS = dyn_cast<PostStmt>(&P))
421 if (PS->getStmt() == Ex)
422 St = N->getState();
423
424 FindUndefExpr FindIt(Eng.getStateManager(), St);
425 Ex = FindIt.FindExpr(Ex);
426
Ted Kremenek85ac9342009-05-15 05:25:09 +0000427 ArgReport *R = new ArgReport(*this, desc.c_str(), *I, Ex);
Ted Kremenekcf118d42009-02-04 23:49:09 +0000428 R->addRange(Ex->getSourceRange());
429 BR.EmitReport(R);
Ted Kremenek78d46242008-07-22 16:21:24 +0000430 }
431 }
Mike Stump1eb44332009-09-09 15:08:12 +0000432
Ted Kremenek85ac9342009-05-15 05:25:09 +0000433 void registerInitialVisitors(BugReporterContext& BRC,
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000434 const ExplodedNode* N,
Ted Kremenek85ac9342009-05-15 05:25:09 +0000435 BuiltinBugReport *R) {
436 registerTrackNullOrUndefValue(BRC, static_cast<ArgReport*>(R)->getArg(),
437 N);
438 }
Ted Kremenek78d46242008-07-22 16:21:24 +0000439};
440
Zhongxing Xu1c0c2332008-11-23 05:52:28 +0000441class VISIBILITY_HIDDEN OutOfBoundMemoryAccess : public BuiltinBug {
442public:
Ted Kremenekcf118d42009-02-04 23:49:09 +0000443 OutOfBoundMemoryAccess(GRExprEngine* eng)
Ted Kremenek5d88ff82009-04-02 02:40:26 +0000444 : BuiltinBug(eng,"Out-of-bounds memory access",
Ted Kremenekcf118d42009-02-04 23:49:09 +0000445 "Load or store into an out-of-bound memory position.") {}
Zhongxing Xu1c0c2332008-11-23 05:52:28 +0000446
Ted Kremenekcf118d42009-02-04 23:49:09 +0000447 void FlushReportsImpl(BugReporter& BR, GRExprEngine& Eng) {
Zhongxing Xu1c0c2332008-11-23 05:52:28 +0000448 Emit(BR, Eng.explicit_oob_memacc_begin(), Eng.explicit_oob_memacc_end());
449 }
450};
Mike Stump1eb44332009-09-09 15:08:12 +0000451
Zhongxing Xuec9227f2009-10-29 02:09:30 +0000452} // end clang namespace
Ted Kremenekbc3a0212009-10-30 17:24:47 +0000453
Ted Kremenek78d46242008-07-22 16:21:24 +0000454//===----------------------------------------------------------------------===//
455// Check registration.
Ted Kremenekcf118d42009-02-04 23:49:09 +0000456//===----------------------------------------------------------------------===//
Ted Kremenek78d46242008-07-22 16:21:24 +0000457
458void GRExprEngine::RegisterInternalChecks() {
Ted Kremenekcf118d42009-02-04 23:49:09 +0000459 // Register internal "built-in" BugTypes with the BugReporter. These BugTypes
460 // are different than what probably many checks will do since they don't
461 // create BugReports on-the-fly but instead wait until GRExprEngine finishes
462 // analyzing a function. Generation of BugReport objects is done via a call
463 // to 'FlushReports' from BugReporter.
Ted Kremenekcf118d42009-02-04 23:49:09 +0000464 BR.Register(new UndefBranch(this));
Ted Kremenekcf118d42009-02-04 23:49:09 +0000465 BR.Register(new UndefResult(this));
Ted Kremenekcf118d42009-02-04 23:49:09 +0000466 BR.Register(new RetStack(this));
467 BR.Register(new RetUndef(this));
Ted Kremenekcf118d42009-02-04 23:49:09 +0000468 BR.Register(new BadMsgExprArg(this));
469 BR.Register(new BadReceiver(this));
470 BR.Register(new OutOfBoundMemoryAccess(this));
Ted Kremenek21fe8372009-02-19 04:06:22 +0000471 BR.Register(new NilReceiverStructRet(this));
Ted Kremenek899b3de2009-04-08 03:07:17 +0000472 BR.Register(new NilReceiverLargerThanVoidPtrRet(this));
Mike Stump1eb44332009-09-09 15:08:12 +0000473
Ted Kremenekcf118d42009-02-04 23:49:09 +0000474 // The following checks do not need to have their associated BugTypes
475 // explicitly registered with the BugReporter. If they issue any BugReports,
476 // their associated BugType will get registered with the BugReporter
477 // automatically. Note that the check itself is owned by the GRExprEngine
478 // object.
Zhongxing Xu94943b62009-11-03 07:35:33 +0000479 registerCheck<AttrNonNullChecker>(new AttrNonNullChecker());
Zhongxing Xu8958fff2009-11-03 06:46:03 +0000480 registerCheck<UndefinedArgChecker>(new UndefinedArgChecker());
Zhongxing Xu4f64e5f2009-11-03 05:48:04 +0000481 registerCheck<BadCallChecker>(new BadCallChecker());
Zhongxing Xu9e56d232009-10-31 10:02:37 +0000482 registerCheck<DivZeroChecker>(new DivZeroChecker());
Zhongxing Xu246a9ad2009-10-31 08:44:33 +0000483 registerCheck<UndefDerefChecker>(new UndefDerefChecker());
Zhongxing Xuec9227f2009-10-29 02:09:30 +0000484 registerCheck<NullDerefChecker>(new NullDerefChecker());
Zhongxing Xu5206f0b2009-11-03 12:13:38 +0000485 registerCheck<UndefSizedVLAChecker>(new UndefSizedVLAChecker());
486 registerCheck<ZeroSizedVLAChecker>(new ZeroSizedVLAChecker());
Ted Kremenek78d46242008-07-22 16:21:24 +0000487}