blob: 8977b198233ccf093acd585763e943e795796af1 [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
Ted Kremenek1053d242009-11-06 02:24:13 +000015#include "GRExprEngineInternalChecks.h"
Ted Kremenek78d46242008-07-22 16:21:24 +000016#include "clang/Analysis/PathSensitive/BugReporter.h"
17#include "clang/Analysis/PathSensitive/GRExprEngine.h"
Ted Kremenekc26a8b02009-07-22 21:46:56 +000018#include "clang/Analysis/PathSensitive/CheckerVisitor.h"
Ted Kremenekdc998c12009-11-03 18:41:06 +000019#include "clang/Analysis/PathSensitive/Checkers/DereferenceChecker.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"
Ted Kremenekb107c4b2009-11-04 04:24:16 +000022#include "clang/Analysis/PathSensitive/Checkers/UndefinedAssignmentChecker.h"
Zhongxing Xu94943b62009-11-03 07:35:33 +000023#include "clang/Analysis/PathSensitive/Checkers/AttrNonNullChecker.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 UndefBranch : public BuiltinBug {
293 struct VISIBILITY_HIDDEN FindUndefExpr {
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000294 GRStateManager& VM;
295 const GRState* St;
Mike Stump1eb44332009-09-09 15:08:12 +0000296
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000297 FindUndefExpr(GRStateManager& V, const GRState* S) : VM(V), St(S) {}
Mike Stump1eb44332009-09-09 15:08:12 +0000298
299 Expr* FindExpr(Expr* Ex) {
Ted Kremenek78d46242008-07-22 16:21:24 +0000300 if (!MatchesCriteria(Ex))
Ted Kremenekb7714b22008-07-30 17:49:12 +0000301 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000302
Ted Kremenekb7714b22008-07-30 17:49:12 +0000303 for (Stmt::child_iterator I=Ex->child_begin(), E=Ex->child_end();I!=E;++I)
Ted Kremenek78d46242008-07-22 16:21:24 +0000304 if (Expr* ExI = dyn_cast_or_null<Expr>(*I)) {
305 Expr* E2 = FindExpr(ExI);
306 if (E2) return E2;
307 }
Mike Stump1eb44332009-09-09 15:08:12 +0000308
Ted Kremenek78d46242008-07-22 16:21:24 +0000309 return Ex;
310 }
Mike Stump1eb44332009-09-09 15:08:12 +0000311
Ted Kremenek23ec48c2009-06-18 23:58:37 +0000312 bool MatchesCriteria(Expr* Ex) { return St->getSVal(Ex).isUndef(); }
Ted Kremenek78d46242008-07-22 16:21:24 +0000313 };
Mike Stump1eb44332009-09-09 15:08:12 +0000314
Ted Kremenek78d46242008-07-22 16:21:24 +0000315public:
Ted Kremenekcf118d42009-02-04 23:49:09 +0000316 UndefBranch(GRExprEngine *eng)
Ted Kremenek5b9bd212009-09-11 22:07:28 +0000317 : BuiltinBug(eng,"Use of garbage value",
318 "Branch condition evaluates to an undefined or garbage value")
319 {}
Mike Stump1eb44332009-09-09 15:08:12 +0000320
Ted Kremenekcf118d42009-02-04 23:49:09 +0000321 void FlushReportsImpl(BugReporter& BR, GRExprEngine& Eng) {
Ted Kremenek78d46242008-07-22 16:21:24 +0000322 for (GRExprEngine::undef_branch_iterator I=Eng.undef_branches_begin(),
323 E=Eng.undef_branches_end(); I!=E; ++I) {
324
325 // What's going on here: we want to highlight the subexpression of the
326 // condition that is the most likely source of the "uninitialized
327 // branch condition." We do a recursive walk of the condition's
328 // subexpressions and roughly look for the most nested subexpression
329 // that binds to Undefined. We then highlight that expression's range.
Ted Kremenek78d46242008-07-22 16:21:24 +0000330 BlockEdge B = cast<BlockEdge>((*I)->getLocation());
331 Expr* Ex = cast<Expr>(B.getSrc()->getTerminatorCondition());
332 assert (Ex && "Block must have a terminator.");
333
334 // Get the predecessor node and check if is a PostStmt with the Stmt
335 // being the terminator condition. We want to inspect the state
336 // of that node instead because it will contain main information about
337 // the subexpressions.
Ted Kremenek78d46242008-07-22 16:21:24 +0000338 assert (!(*I)->pred_empty());
339
340 // Note: any predecessor will do. They should have identical state,
341 // since all the BlockEdge did was act as an error sink since the value
342 // had to already be undefined.
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000343 ExplodedNode *N = *(*I)->pred_begin();
Ted Kremenek78d46242008-07-22 16:21:24 +0000344 ProgramPoint P = N->getLocation();
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000345 const GRState* St = (*I)->getState();
Ted Kremenek78d46242008-07-22 16:21:24 +0000346
347 if (PostStmt* PS = dyn_cast<PostStmt>(&P))
348 if (PS->getStmt() == Ex)
349 St = N->getState();
350
351 FindUndefExpr FindIt(Eng.getStateManager(), St);
352 Ex = FindIt.FindExpr(Ex);
353
Ted Kremenek85ac9342009-05-15 05:25:09 +0000354 ArgReport *R = new ArgReport(*this, desc.c_str(), *I, Ex);
Ted Kremenekcf118d42009-02-04 23:49:09 +0000355 R->addRange(Ex->getSourceRange());
356 BR.EmitReport(R);
Ted Kremenek78d46242008-07-22 16:21:24 +0000357 }
358 }
Mike Stump1eb44332009-09-09 15:08:12 +0000359
Ted Kremenek85ac9342009-05-15 05:25:09 +0000360 void registerInitialVisitors(BugReporterContext& BRC,
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000361 const ExplodedNode* N,
Ted Kremenek85ac9342009-05-15 05:25:09 +0000362 BuiltinBugReport *R) {
363 registerTrackNullOrUndefValue(BRC, static_cast<ArgReport*>(R)->getArg(),
364 N);
365 }
Ted Kremenek78d46242008-07-22 16:21:24 +0000366};
367
Zhongxing Xu1c0c2332008-11-23 05:52:28 +0000368class VISIBILITY_HIDDEN OutOfBoundMemoryAccess : public BuiltinBug {
369public:
Ted Kremenekcf118d42009-02-04 23:49:09 +0000370 OutOfBoundMemoryAccess(GRExprEngine* eng)
Ted Kremenek5d88ff82009-04-02 02:40:26 +0000371 : BuiltinBug(eng,"Out-of-bounds memory access",
Ted Kremenekcf118d42009-02-04 23:49:09 +0000372 "Load or store into an out-of-bound memory position.") {}
Zhongxing Xu1c0c2332008-11-23 05:52:28 +0000373
Ted Kremenekcf118d42009-02-04 23:49:09 +0000374 void FlushReportsImpl(BugReporter& BR, GRExprEngine& Eng) {
Zhongxing Xu1c0c2332008-11-23 05:52:28 +0000375 Emit(BR, Eng.explicit_oob_memacc_begin(), Eng.explicit_oob_memacc_end());
376 }
377};
Mike Stump1eb44332009-09-09 15:08:12 +0000378
Zhongxing Xuec9227f2009-10-29 02:09:30 +0000379} // end clang namespace
Ted Kremenekbc3a0212009-10-30 17:24:47 +0000380
Ted Kremenek78d46242008-07-22 16:21:24 +0000381//===----------------------------------------------------------------------===//
382// Check registration.
Ted Kremenekcf118d42009-02-04 23:49:09 +0000383//===----------------------------------------------------------------------===//
Ted Kremenek78d46242008-07-22 16:21:24 +0000384
385void GRExprEngine::RegisterInternalChecks() {
Ted Kremenekcf118d42009-02-04 23:49:09 +0000386 // Register internal "built-in" BugTypes with the BugReporter. These BugTypes
387 // are different than what probably many checks will do since they don't
388 // create BugReports on-the-fly but instead wait until GRExprEngine finishes
389 // analyzing a function. Generation of BugReport objects is done via a call
390 // to 'FlushReports' from BugReporter.
Ted Kremenekcf118d42009-02-04 23:49:09 +0000391 BR.Register(new UndefBranch(this));
Ted Kremenekcf118d42009-02-04 23:49:09 +0000392 BR.Register(new UndefResult(this));
Ted Kremenekcf118d42009-02-04 23:49:09 +0000393 BR.Register(new BadMsgExprArg(this));
394 BR.Register(new BadReceiver(this));
395 BR.Register(new OutOfBoundMemoryAccess(this));
Ted Kremenek21fe8372009-02-19 04:06:22 +0000396 BR.Register(new NilReceiverStructRet(this));
Ted Kremenek899b3de2009-04-08 03:07:17 +0000397 BR.Register(new NilReceiverLargerThanVoidPtrRet(this));
Mike Stump1eb44332009-09-09 15:08:12 +0000398
Ted Kremenekcf118d42009-02-04 23:49:09 +0000399 // The following checks do not need to have their associated BugTypes
400 // explicitly registered with the BugReporter. If they issue any BugReports,
401 // their associated BugType will get registered with the BugReporter
402 // automatically. Note that the check itself is owned by the GRExprEngine
Ted Kremenek36df58a2009-11-06 20:47:51 +0000403 // object.
Ted Kremenekb107c4b2009-11-04 04:24:16 +0000404 registerCheck(new AttrNonNullChecker());
405 registerCheck(new UndefinedArgChecker());
406 registerCheck(new UndefinedAssignmentChecker());
407 registerCheck(new BadCallChecker());
Ted Kremenekb107c4b2009-11-04 04:24:16 +0000408 registerCheck(new UndefDerefChecker());
409 registerCheck(new NullDerefChecker());
Ted Kremenek84b35952009-11-06 21:51:50 +0000410
411 RegisterVLASizeChecker(*this);
412 RegisterDivZeroChecker(*this);
413 RegisterReturnStackAddressChecker(*this);
414 RegisterReturnUndefChecker(*this);
Zhongxing Xu3ce2dc32009-11-09 05:34:10 +0000415 RegisterPointerSubChecker(*this);
Ted Kremenek84b35952009-11-06 21:51:50 +0000416
417 // Note that this must be registered after ReturnStackAddressChecker.
418 RegisterReturnPointerRangeChecker(*this);
Ted Kremenek78d46242008-07-22 16:21:24 +0000419}