blob: 37e7bfdf619ae511e1ae3d9cebf2e8eb6ae22661 [file] [log] [blame]
Ted Kremenek1f352db2008-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 Kremenekbee01e52009-11-06 02:24:13 +000015#include "GRExprEngineInternalChecks.h"
Ted Kremenek1f352db2008-07-22 16:21:24 +000016#include "clang/Analysis/PathSensitive/BugReporter.h"
17#include "clang/Analysis/PathSensitive/GRExprEngine.h"
Ted Kremenek915c3512009-07-22 21:46:56 +000018#include "clang/Analysis/PathSensitive/CheckerVisitor.h"
Ted Kremenek8d43a6a2009-11-03 18:41:06 +000019#include "clang/Analysis/PathSensitive/Checkers/DereferenceChecker.h"
Zhongxing Xu0deca342009-11-03 05:48:04 +000020#include "clang/Analysis/PathSensitive/Checkers/BadCallChecker.h"
Zhongxing Xuab162e12009-11-03 06:46:03 +000021#include "clang/Analysis/PathSensitive/Checkers/UndefinedArgChecker.h"
Ted Kremenekef910042009-11-04 04:24:16 +000022#include "clang/Analysis/PathSensitive/Checkers/UndefinedAssignmentChecker.h"
Zhongxing Xu9b9d7312009-11-03 07:35:33 +000023#include "clang/Analysis/PathSensitive/Checkers/AttrNonNullChecker.h"
Ted Kremenek9abe4742009-05-07 00:45:33 +000024#include "clang/Analysis/PathDiagnostic.h"
Ted Kremenek83214f92008-10-31 00:13:20 +000025#include "clang/Basic/SourceManager.h"
Ted Kremenek1f352db2008-07-22 16:21:24 +000026#include "llvm/Support/Compiler.h"
Ted Kremenek289c37a2008-10-31 00:18:30 +000027#include "llvm/Support/raw_ostream.h"
Ted Kremenek1f352db2008-07-22 16:21:24 +000028
29using namespace clang;
Ted Kremenekf1282072009-07-22 17:55:28 +000030using namespace clang::bugreporter;
Ted Kremenek1f352db2008-07-22 16:21:24 +000031
32//===----------------------------------------------------------------------===//
33// Utility functions.
34//===----------------------------------------------------------------------===//
35
36template <typename ITERATOR> inline
Zhongxing Xu20227f72009-08-06 01:32:16 +000037ExplodedNode* GetNode(ITERATOR I) {
Ted Kremenek1f352db2008-07-22 16:21:24 +000038 return *I;
39}
40
41template <> inline
Zhongxing Xu20227f72009-08-06 01:32:16 +000042ExplodedNode* GetNode(GRExprEngine::undef_arg_iterator I) {
Ted Kremenek1f352db2008-07-22 16:21:24 +000043 return I->first;
44}
45
46//===----------------------------------------------------------------------===//
47// Bug Descriptions.
48//===----------------------------------------------------------------------===//
Zhongxing Xu6b8bfb32009-10-29 02:09:30 +000049namespace clang {
50class BuiltinBugReport : public RangedBugReport {
Ted Kremenek9abe4742009-05-07 00:45:33 +000051public:
52 BuiltinBugReport(BugType& bt, const char* desc,
Zhongxing Xu20227f72009-08-06 01:32:16 +000053 ExplodedNode *n)
Ted Kremenekbae77722009-05-13 19:16:35 +000054 : RangedBugReport(bt, desc, n) {}
Mike Stump11289f42009-09-09 15:08:12 +000055
Ted Kremenek608a6172009-05-15 05:25:09 +000056 BuiltinBugReport(BugType& bt, const char *shortDesc, const char *desc,
Zhongxing Xu20227f72009-08-06 01:32:16 +000057 ExplodedNode *n)
Mike Stump11289f42009-09-09 15:08:12 +000058 : RangedBugReport(bt, shortDesc, desc, n) {}
59
Ted Kremenek9abe4742009-05-07 00:45:33 +000060 void registerInitialVisitors(BugReporterContext& BRC,
Zhongxing Xu20227f72009-08-06 01:32:16 +000061 const ExplodedNode* N);
Mike Stump11289f42009-09-09 15:08:12 +000062};
63
Zhongxing Xu6b8bfb32009-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 Stump11289f42009-09-09 15:08:12 +000068
Ted Kremenek9abe4742009-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 Stump11289f42009-09-09 15:08:12 +000073}
Mike Stump11289f42009-09-09 15:08:12 +000074
Ted Kremenekbae77722009-05-13 19:16:35 +000075class VISIBILITY_HIDDEN NilReceiverStructRet : public BuiltinBug {
Ted Kremenek66d9edc2009-02-19 04:06:22 +000076public:
77 NilReceiverStructRet(GRExprEngine* eng) :
Ted Kremenekbae77722009-05-13 19:16:35 +000078 BuiltinBug(eng, "'nil' receiver with struct return type") {}
Ted Kremenek66d9edc2009-02-19 04:06:22 +000079
Ted Kremenekbae77722009-05-13 19:16:35 +000080 void FlushReportsImpl(BugReporter& BR, GRExprEngine& Eng) {
Ted Kremenek66d9edc2009-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 Kremenekbfd28fd2009-07-22 22:35:28 +000088 const ObjCMessageExpr *ME = cast<ObjCMessageExpr>(P.getStmt());
Ted Kremenek66d9edc2009-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 Kremenek7020eae2009-09-11 22:07:28 +000092 << "') to be garbage or otherwise undefined";
Ted Kremenek66d9edc2009-02-19 04:06:22 +000093
Ted Kremenekbae77722009-05-13 19:16:35 +000094 BuiltinBugReport *R = new BuiltinBugReport(*this, os.str().c_str(), *I);
Ted Kremenek66d9edc2009-02-19 04:06:22 +000095 R->addRange(ME->getReceiver()->getSourceRange());
96 BR.EmitReport(R);
97 }
98 }
Mike Stump11289f42009-09-09 15:08:12 +000099
Ted Kremenekbae77722009-05-13 19:16:35 +0000100 void registerInitialVisitors(BugReporterContext& BRC,
Zhongxing Xu20227f72009-08-06 01:32:16 +0000101 const ExplodedNode* N,
Ted Kremenekbae77722009-05-13 19:16:35 +0000102 BuiltinBugReport *R) {
Ted Kremenek608a6172009-05-15 05:25:09 +0000103 registerTrackNullOrUndefValue(BRC, GetReceiverExpr(N), N);
Ted Kremenekbae77722009-05-13 19:16:35 +0000104 }
Ted Kremenek66d9edc2009-02-19 04:06:22 +0000105};
Ted Kremenek605fee82009-04-08 03:07:17 +0000106
Ted Kremenekbae77722009-05-13 19:16:35 +0000107class VISIBILITY_HIDDEN NilReceiverLargerThanVoidPtrRet : public BuiltinBug {
Ted Kremenek605fee82009-04-08 03:07:17 +0000108public:
109 NilReceiverLargerThanVoidPtrRet(GRExprEngine* eng) :
Ted Kremenekbae77722009-05-13 19:16:35 +0000110 BuiltinBug(eng,
111 "'nil' receiver with return type larger than sizeof(void *)") {}
Mike Stump11289f42009-09-09 15:08:12 +0000112
Ted Kremenekbae77722009-05-13 19:16:35 +0000113 void FlushReportsImpl(BugReporter& BR, GRExprEngine& Eng) {
Ted Kremenek605fee82009-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 Stump11289f42009-09-09 15:08:12 +0000117
Ted Kremenek605fee82009-04-08 03:07:17 +0000118 std::string sbuf;
119 llvm::raw_string_ostream os(sbuf);
120 PostStmt P = cast<PostStmt>((*I)->getLocation());
Ted Kremenekbfd28fd2009-07-22 22:35:28 +0000121 const ObjCMessageExpr *ME = cast<ObjCMessageExpr>(P.getStmt());
Ted Kremenek605fee82009-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 Kremenek7020eae2009-09-11 22:07:28 +0000127 << " bytes) to be garbage or otherwise undefined";
Mike Stump11289f42009-09-09 15:08:12 +0000128
Ted Kremenekbae77722009-05-13 19:16:35 +0000129 BuiltinBugReport *R = new BuiltinBugReport(*this, os.str().c_str(), *I);
Ted Kremenek605fee82009-04-08 03:07:17 +0000130 R->addRange(ME->getReceiver()->getSourceRange());
131 BR.EmitReport(R);
132 }
Mike Stump11289f42009-09-09 15:08:12 +0000133 }
Ted Kremenekbae77722009-05-13 19:16:35 +0000134 void registerInitialVisitors(BugReporterContext& BRC,
Zhongxing Xu20227f72009-08-06 01:32:16 +0000135 const ExplodedNode* N,
Ted Kremenekbae77722009-05-13 19:16:35 +0000136 BuiltinBugReport *R) {
Ted Kremenek608a6172009-05-15 05:25:09 +0000137 registerTrackNullOrUndefValue(BRC, GetReceiverExpr(N), N);
Ted Kremenek605fee82009-04-08 03:07:17 +0000138 }
139};
Mike Stump11289f42009-09-09 15:08:12 +0000140
Ted Kremenek1f352db2008-07-22 16:21:24 +0000141class VISIBILITY_HIDDEN UndefResult : public BuiltinBug {
142public:
Ted Kremenek7020eae2009-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 Stump11289f42009-09-09 15:08:12 +0000146
Ted Kremenekfc5d0672009-02-04 23:49:09 +0000147 void FlushReportsImpl(BugReporter& BR, GRExprEngine& Eng) {
Ted Kremenek7020eae2009-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 Kremenek4f335c32009-09-16 06:04:26 +0000159 const Expr *Ex = NULL;
160 bool isLeft = true;
Ted Kremenek7020eae2009-09-11 22:07:28 +0000161
162 if (ST->getSVal(B->getLHS()).isUndef()) {
163 Ex = B->getLHS()->IgnoreParenCasts();
Ted Kremenek4f335c32009-09-16 06:04:26 +0000164 isLeft = true;
Ted Kremenek7020eae2009-09-11 22:07:28 +0000165 }
Ted Kremenek188f62f2009-09-15 17:43:54 +0000166 else if (ST->getSVal(B->getRHS()).isUndef()) {
Ted Kremenek7020eae2009-09-11 22:07:28 +0000167 Ex = B->getRHS()->IgnoreParenCasts();
Ted Kremenek4f335c32009-09-16 06:04:26 +0000168 isLeft = false;
Ted Kremenek7020eae2009-09-11 22:07:28 +0000169 }
Ted Kremenek4f335c32009-09-16 06:04:26 +0000170
171 if (Ex) {
172 OS << "The " << (isLeft ? "left" : "right")
Ted Kremenekd9120d32009-09-24 00:44:26 +0000173 << " operand of '"
Ted Kremenek4f335c32009-09-16 06:04:26 +0000174 << BinaryOperator::getOpcodeStr(B->getOpcode())
Ted Kremenekd9120d32009-09-24 00:44:26 +0000175 << "' is a garbage value";
Ted Kremenek188f62f2009-09-15 17:43:54 +0000176 }
177 else {
Ted Kremenek4f335c32009-09-16 06:04:26 +0000178 // Neither operand was undefined, but the result is undefined.
Ted Kremenek188f62f2009-09-15 17:43:54 +0000179 OS << "The result of the '"
180 << BinaryOperator::getOpcodeStr(B->getOpcode())
181 << "' expression is undefined";
182 }
183
Ted Kremenek7020eae2009-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 Kremenek188f62f2009-09-15 17:43:54 +0000186 if (Ex) report->addRange(Ex->getSourceRange());
Ted Kremenek7020eae2009-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 Kremenek4f335c32009-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 Kremenek7020eae2009-09-11 22:07:28 +0000211 }
212
213 registerTrackNullOrUndefValue(BRC, X, N);
Ted Kremenek1f352db2008-07-22 16:21:24 +0000214 }
215};
Mike Stump11289f42009-09-09 15:08:12 +0000216
Ted Kremenek608a6172009-05-15 05:25:09 +0000217class VISIBILITY_HIDDEN ArgReport : public BuiltinBugReport {
218 const Stmt *Arg;
219public:
Zhongxing Xu20227f72009-08-06 01:32:16 +0000220 ArgReport(BugType& bt, const char* desc, ExplodedNode *n,
Ted Kremenek608a6172009-05-15 05:25:09 +0000221 const Stmt *arg)
222 : BuiltinBugReport(bt, desc, n), Arg(arg) {}
Mike Stump11289f42009-09-09 15:08:12 +0000223
Ted Kremenek608a6172009-05-15 05:25:09 +0000224 ArgReport(BugType& bt, const char *shortDesc, const char *desc,
Zhongxing Xu20227f72009-08-06 01:32:16 +0000225 ExplodedNode *n, const Stmt *arg)
Mike Stump11289f42009-09-09 15:08:12 +0000226 : BuiltinBugReport(bt, shortDesc, desc, n), Arg(arg) {}
227
228 const Stmt *getArg() const { return Arg; }
Ted Kremenek1f352db2008-07-22 16:21:24 +0000229};
230
231class VISIBILITY_HIDDEN BadArg : public BuiltinBug {
Mike Stump11289f42009-09-09 15:08:12 +0000232public:
233 BadArg(GRExprEngine* eng=0) : BuiltinBug(eng,"Uninitialized argument",
Ted Kremenek7020eae2009-09-11 22:07:28 +0000234 "Pass-by-value argument in function call is undefined") {}
Ted Kremenek1f352db2008-07-22 16:21:24 +0000235
Ted Kremenekfc5d0672009-02-04 23:49:09 +0000236 BadArg(GRExprEngine* eng, const char* d)
Ted Kremenekb82dd532009-04-02 02:40:26 +0000237 : BuiltinBug(eng,"Uninitialized argument", d) {}
Mike Stump11289f42009-09-09 15:08:12 +0000238
Ted Kremenek608a6172009-05-15 05:25:09 +0000239 void registerInitialVisitors(BugReporterContext& BRC,
Zhongxing Xu20227f72009-08-06 01:32:16 +0000240 const ExplodedNode* N,
Ted Kremenek608a6172009-05-15 05:25:09 +0000241 BuiltinBugReport *R) {
242 registerTrackNullOrUndefValue(BRC, static_cast<ArgReport*>(R)->getArg(),
243 N);
Mike Stump11289f42009-09-09 15:08:12 +0000244 }
Ted Kremenek1f352db2008-07-22 16:21:24 +0000245};
Mike Stump11289f42009-09-09 15:08:12 +0000246
Ted Kremenek1f352db2008-07-22 16:21:24 +0000247class VISIBILITY_HIDDEN BadMsgExprArg : public BadArg {
248public:
Mike Stump11289f42009-09-09 15:08:12 +0000249 BadMsgExprArg(GRExprEngine* eng)
Ted Kremenekb82dd532009-04-02 02:40:26 +0000250 : BadArg(eng,"Pass-by-value argument in message expression is undefined"){}
Mike Stump11289f42009-09-09 15:08:12 +0000251
Ted Kremenekfc5d0672009-02-04 23:49:09 +0000252 void FlushReportsImpl(BugReporter& BR, GRExprEngine& Eng) {
Ted Kremenek1f352db2008-07-22 16:21:24 +0000253 for (GRExprEngine::UndefArgsTy::iterator I=Eng.msg_expr_undef_arg_begin(),
Mike Stump11289f42009-09-09 15:08:12 +0000254 E = Eng.msg_expr_undef_arg_end(); I!=E; ++I) {
Ted Kremenek1f352db2008-07-22 16:21:24 +0000255 // Generate a report for this bug.
Ted Kremenek608a6172009-05-15 05:25:09 +0000256 ArgReport *report = new ArgReport(*this, desc.c_str(), I->first,
257 I->second);
Ted Kremenekfc5d0672009-02-04 23:49:09 +0000258 report->addRange(I->second->getSourceRange());
259 BR.EmitReport(report);
Mike Stump11289f42009-09-09 15:08:12 +0000260 }
261 }
Ted Kremenek1f352db2008-07-22 16:21:24 +0000262};
Mike Stump11289f42009-09-09 15:08:12 +0000263
Ted Kremenek1f352db2008-07-22 16:21:24 +0000264class VISIBILITY_HIDDEN BadReceiver : public BuiltinBug {
Mike Stump11289f42009-09-09 15:08:12 +0000265public:
Ted Kremenekfc5d0672009-02-04 23:49:09 +0000266 BadReceiver(GRExprEngine* eng)
Ted Kremenekb82dd532009-04-02 02:40:26 +0000267 : BuiltinBug(eng,"Uninitialized receiver",
268 "Receiver in message expression is an uninitialized value") {}
Mike Stump11289f42009-09-09 15:08:12 +0000269
Ted Kremenekfc5d0672009-02-04 23:49:09 +0000270 void FlushReportsImpl(BugReporter& BR, GRExprEngine& Eng) {
Ted Kremenek8f7afdd2008-12-08 22:47:34 +0000271 for (GRExprEngine::ErrorNodes::iterator I=Eng.undef_receivers_begin(),
Ted Kremenek1f352db2008-07-22 16:21:24 +0000272 End = Eng.undef_receivers_end(); I!=End; ++I) {
Mike Stump11289f42009-09-09 15:08:12 +0000273
Ted Kremenek1f352db2008-07-22 16:21:24 +0000274 // Generate a report for this bug.
Ted Kremenekbae77722009-05-13 19:16:35 +0000275 BuiltinBugReport *report = new BuiltinBugReport(*this, desc.c_str(), *I);
Zhongxing Xu20227f72009-08-06 01:32:16 +0000276 ExplodedNode* N = *I;
Ted Kremenekbfd28fd2009-07-22 22:35:28 +0000277 const Stmt *S = cast<PostStmt>(N->getLocation()).getStmt();
278 const Expr* E = cast<ObjCMessageExpr>(S)->getReceiver();
Ted Kremenek1f352db2008-07-22 16:21:24 +0000279 assert (E && "Receiver cannot be NULL");
Ted Kremenekfc5d0672009-02-04 23:49:09 +0000280 report->addRange(E->getSourceRange());
281 BR.EmitReport(report);
Ted Kremenekbae77722009-05-13 19:16:35 +0000282 }
283 }
Ted Kremenek608a6172009-05-15 05:25:09 +0000284
Ted Kremenekbae77722009-05-13 19:16:35 +0000285 void registerInitialVisitors(BugReporterContext& BRC,
Zhongxing Xu20227f72009-08-06 01:32:16 +0000286 const ExplodedNode* N,
Ted Kremenekbae77722009-05-13 19:16:35 +0000287 BuiltinBugReport *R) {
Ted Kremenek608a6172009-05-15 05:25:09 +0000288 registerTrackNullOrUndefValue(BRC, GetReceiverExpr(N), N);
Mike Stump11289f42009-09-09 15:08:12 +0000289 }
Ted Kremenek1f352db2008-07-22 16:21:24 +0000290};
Ted Kremenek0b63f962008-11-21 00:27:44 +0000291
Ted Kremenek1f352db2008-07-22 16:21:24 +0000292class VISIBILITY_HIDDEN UndefBranch : public BuiltinBug {
293 struct VISIBILITY_HIDDEN FindUndefExpr {
Ted Kremenek5ab5a1b2008-08-13 04:27:00 +0000294 GRStateManager& VM;
295 const GRState* St;
Mike Stump11289f42009-09-09 15:08:12 +0000296
Ted Kremenek5ab5a1b2008-08-13 04:27:00 +0000297 FindUndefExpr(GRStateManager& V, const GRState* S) : VM(V), St(S) {}
Mike Stump11289f42009-09-09 15:08:12 +0000298
299 Expr* FindExpr(Expr* Ex) {
Ted Kremenek1f352db2008-07-22 16:21:24 +0000300 if (!MatchesCriteria(Ex))
Ted Kremeneka8b8ce42008-07-30 17:49:12 +0000301 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000302
Ted Kremeneka8b8ce42008-07-30 17:49:12 +0000303 for (Stmt::child_iterator I=Ex->child_begin(), E=Ex->child_end();I!=E;++I)
Ted Kremenek1f352db2008-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 Stump11289f42009-09-09 15:08:12 +0000308
Ted Kremenek1f352db2008-07-22 16:21:24 +0000309 return Ex;
310 }
Mike Stump11289f42009-09-09 15:08:12 +0000311
Ted Kremenek095f1a92009-06-18 23:58:37 +0000312 bool MatchesCriteria(Expr* Ex) { return St->getSVal(Ex).isUndef(); }
Ted Kremenek1f352db2008-07-22 16:21:24 +0000313 };
Mike Stump11289f42009-09-09 15:08:12 +0000314
Ted Kremenek1f352db2008-07-22 16:21:24 +0000315public:
Ted Kremenekfc5d0672009-02-04 23:49:09 +0000316 UndefBranch(GRExprEngine *eng)
Ted Kremenek7020eae2009-09-11 22:07:28 +0000317 : BuiltinBug(eng,"Use of garbage value",
318 "Branch condition evaluates to an undefined or garbage value")
319 {}
Mike Stump11289f42009-09-09 15:08:12 +0000320
Ted Kremenekfc5d0672009-02-04 23:49:09 +0000321 void FlushReportsImpl(BugReporter& BR, GRExprEngine& Eng) {
Ted Kremenek1f352db2008-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 Kremenek1f352db2008-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 Kremenek1f352db2008-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 Xu20227f72009-08-06 01:32:16 +0000343 ExplodedNode *N = *(*I)->pred_begin();
Ted Kremenek1f352db2008-07-22 16:21:24 +0000344 ProgramPoint P = N->getLocation();
Ted Kremenek5ab5a1b2008-08-13 04:27:00 +0000345 const GRState* St = (*I)->getState();
Ted Kremenek1f352db2008-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 Kremenek608a6172009-05-15 05:25:09 +0000354 ArgReport *R = new ArgReport(*this, desc.c_str(), *I, Ex);
Ted Kremenekfc5d0672009-02-04 23:49:09 +0000355 R->addRange(Ex->getSourceRange());
356 BR.EmitReport(R);
Ted Kremenek1f352db2008-07-22 16:21:24 +0000357 }
358 }
Mike Stump11289f42009-09-09 15:08:12 +0000359
Ted Kremenek608a6172009-05-15 05:25:09 +0000360 void registerInitialVisitors(BugReporterContext& BRC,
Zhongxing Xu20227f72009-08-06 01:32:16 +0000361 const ExplodedNode* N,
Ted Kremenek608a6172009-05-15 05:25:09 +0000362 BuiltinBugReport *R) {
363 registerTrackNullOrUndefValue(BRC, static_cast<ArgReport*>(R)->getArg(),
364 N);
365 }
Ted Kremenek1f352db2008-07-22 16:21:24 +0000366};
367
Zhongxing Xuaa86cff2008-11-23 05:52:28 +0000368class VISIBILITY_HIDDEN OutOfBoundMemoryAccess : public BuiltinBug {
369public:
Ted Kremenekfc5d0672009-02-04 23:49:09 +0000370 OutOfBoundMemoryAccess(GRExprEngine* eng)
Ted Kremenekb82dd532009-04-02 02:40:26 +0000371 : BuiltinBug(eng,"Out-of-bounds memory access",
Ted Kremenekfc5d0672009-02-04 23:49:09 +0000372 "Load or store into an out-of-bound memory position.") {}
Zhongxing Xuaa86cff2008-11-23 05:52:28 +0000373
Ted Kremenekfc5d0672009-02-04 23:49:09 +0000374 void FlushReportsImpl(BugReporter& BR, GRExprEngine& Eng) {
Zhongxing Xuaa86cff2008-11-23 05:52:28 +0000375 Emit(BR, Eng.explicit_oob_memacc_begin(), Eng.explicit_oob_memacc_end());
376 }
377};
Mike Stump11289f42009-09-09 15:08:12 +0000378
Zhongxing Xu6b8bfb32009-10-29 02:09:30 +0000379} // end clang namespace
Ted Kremenekf613e892009-10-30 17:24:47 +0000380
Ted Kremenek1f352db2008-07-22 16:21:24 +0000381//===----------------------------------------------------------------------===//
382// Check registration.
Ted Kremenekfc5d0672009-02-04 23:49:09 +0000383//===----------------------------------------------------------------------===//
Ted Kremenek1f352db2008-07-22 16:21:24 +0000384
385void GRExprEngine::RegisterInternalChecks() {
Ted Kremenekfc5d0672009-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 Kremenekfc5d0672009-02-04 23:49:09 +0000391 BR.Register(new UndefBranch(this));
Ted Kremenekfc5d0672009-02-04 23:49:09 +0000392 BR.Register(new UndefResult(this));
Ted Kremenekfc5d0672009-02-04 23:49:09 +0000393 BR.Register(new BadMsgExprArg(this));
394 BR.Register(new BadReceiver(this));
395 BR.Register(new OutOfBoundMemoryAccess(this));
Ted Kremenek66d9edc2009-02-19 04:06:22 +0000396 BR.Register(new NilReceiverStructRet(this));
Ted Kremenek605fee82009-04-08 03:07:17 +0000397 BR.Register(new NilReceiverLargerThanVoidPtrRet(this));
Mike Stump11289f42009-09-09 15:08:12 +0000398
Ted Kremenekfc5d0672009-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 Kremenek53a70c02009-11-06 20:47:51 +0000403 // object.
Ted Kremenekef910042009-11-04 04:24:16 +0000404 registerCheck(new AttrNonNullChecker());
405 registerCheck(new UndefinedArgChecker());
406 registerCheck(new UndefinedAssignmentChecker());
407 registerCheck(new BadCallChecker());
Ted Kremenekef910042009-11-04 04:24:16 +0000408 registerCheck(new UndefDerefChecker());
409 registerCheck(new NullDerefChecker());
Ted Kremenek795c6112009-11-06 21:51:50 +0000410
411 RegisterVLASizeChecker(*this);
412 RegisterDivZeroChecker(*this);
413 RegisterReturnStackAddressChecker(*this);
414 RegisterReturnUndefChecker(*this);
Zhongxing Xu86b1e012009-11-09 05:34:10 +0000415 RegisterPointerSubChecker(*this);
Zhongxing Xu6c306c82009-11-09 06:52:44 +0000416 RegisterFixedAddressChecker(*this);
Ted Kremenek795c6112009-11-06 21:51:50 +0000417 // Note that this must be registered after ReturnStackAddressChecker.
418 RegisterReturnPointerRangeChecker(*this);
Ted Kremenek1f352db2008-07-22 16:21:24 +0000419}