blob: 92c448cb2f4ef451fefa942c5405ece22261bd82 [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"
17#include "llvm/Support/Compiler.h"
Ted Kremenek22bda882008-07-31 20:31:27 +000018#include <sstream>
Ted Kremenek78d46242008-07-22 16:21:24 +000019
20using namespace clang;
21
22//===----------------------------------------------------------------------===//
23// Utility functions.
24//===----------------------------------------------------------------------===//
25
26template <typename ITERATOR> inline
Ted Kremenek4adc81e2008-08-13 04:27:00 +000027ExplodedNode<GRState>* GetNode(ITERATOR I) {
Ted Kremenek78d46242008-07-22 16:21:24 +000028 return *I;
29}
30
31template <> inline
Ted Kremenek4adc81e2008-08-13 04:27:00 +000032ExplodedNode<GRState>* GetNode(GRExprEngine::undef_arg_iterator I) {
Ted Kremenek78d46242008-07-22 16:21:24 +000033 return I->first;
34}
35
36//===----------------------------------------------------------------------===//
37// Bug Descriptions.
38//===----------------------------------------------------------------------===//
39
40namespace {
41class VISIBILITY_HIDDEN BuiltinBug : public BugTypeCacheLocation {
42 const char* name;
43 const char* desc;
44public:
Ted Kremenek22bda882008-07-31 20:31:27 +000045 BuiltinBug(const char* n, const char* d = 0) : name(n), desc(d) {}
Ted Kremenek78d46242008-07-22 16:21:24 +000046 virtual const char* getName() const { return name; }
Ted Kremenek22bda882008-07-31 20:31:27 +000047 virtual const char* getDescription() const {
48 return desc ? desc : name;
49 }
50
Ted Kremenek78d46242008-07-22 16:21:24 +000051 virtual void EmitBuiltinWarnings(BugReporter& BR, GRExprEngine& Eng) = 0;
52 virtual void EmitWarnings(BugReporter& BR) {
53 EmitBuiltinWarnings(BR, cast<GRBugReporter>(BR).getEngine());
54 }
55
56 template <typename ITER>
57 void Emit(BugReporter& BR, ITER I, ITER E) {
58 for (; I != E; ++I) {
59 BugReport R(*this, GetNode(I));
60 BR.EmitWarning(R);
61 }
62 }
63};
64
65class VISIBILITY_HIDDEN NullDeref : public BuiltinBug {
66public:
67 NullDeref() : BuiltinBug("null dereference",
68 "Dereference of null pointer.") {}
69
70 virtual void EmitBuiltinWarnings(BugReporter& BR, GRExprEngine& Eng) {
71 Emit(BR, Eng.null_derefs_begin(), Eng.null_derefs_end());
72 }
73};
74
75class VISIBILITY_HIDDEN UndefinedDeref : public BuiltinBug {
76public:
77 UndefinedDeref() : BuiltinBug("bad dereference",
78 "Dereference of undefined value.") {}
79
80 virtual void EmitBuiltinWarnings(BugReporter& BR, GRExprEngine& Eng) {
81 Emit(BR, Eng.undef_derefs_begin(), Eng.undef_derefs_end());
82 }
83};
84
85class VISIBILITY_HIDDEN DivZero : public BuiltinBug {
86public:
87 DivZero() : BuiltinBug("divide-by-zero",
88 "Division by zero/undefined value.") {}
89
90 virtual void EmitBuiltinWarnings(BugReporter& BR, GRExprEngine& Eng) {
91 Emit(BR, Eng.explicit_bad_divides_begin(), Eng.explicit_bad_divides_end());
92 }
93};
94
95class VISIBILITY_HIDDEN UndefResult : public BuiltinBug {
96public:
97 UndefResult() : BuiltinBug("undefined result",
98 "Result of operation is undefined.") {}
99
100 virtual void EmitBuiltinWarnings(BugReporter& BR, GRExprEngine& Eng) {
101 Emit(BR, Eng.undef_results_begin(), Eng.undef_results_end());
102 }
103};
104
105class VISIBILITY_HIDDEN BadCall : public BuiltinBug {
106public:
107 BadCall()
108 : BuiltinBug("invalid function call",
109 "Called function is a NULL or undefined function pointer value.") {}
110
111 virtual void EmitBuiltinWarnings(BugReporter& BR, GRExprEngine& Eng) {
112 Emit(BR, Eng.bad_calls_begin(), Eng.bad_calls_end());
113 }
114};
115
116class VISIBILITY_HIDDEN BadArg : public BuiltinBug {
117public:
118 BadArg() : BuiltinBug("bad argument",
119 "Pass-by-value argument in function is undefined.") {}
120
121 BadArg(const char* d) : BuiltinBug("bad argument", d) {}
122
123 virtual void EmitBuiltinWarnings(BugReporter& BR, GRExprEngine& Eng) {
124 for (GRExprEngine::UndefArgsTy::iterator I = Eng.undef_arg_begin(),
125 E = Eng.undef_arg_end(); I!=E; ++I) {
126
127 // Generate a report for this bug.
128 RangedBugReport report(*this, I->first);
129 report.addRange(I->second->getSourceRange());
130
131 // Emit the warning.
132 BR.EmitWarning(report);
133 }
134 }
135};
136
137class VISIBILITY_HIDDEN BadMsgExprArg : public BadArg {
138public:
139 BadMsgExprArg()
140 : BadArg("Pass-by-value argument in message expression is undefined.") {}
141
142 virtual void EmitBuiltinWarnings(BugReporter& BR, GRExprEngine& Eng) {
143 for (GRExprEngine::UndefArgsTy::iterator I=Eng.msg_expr_undef_arg_begin(),
144 E = Eng.msg_expr_undef_arg_end(); I!=E; ++I) {
145
146 // Generate a report for this bug.
147 RangedBugReport report(*this, I->first);
148 report.addRange(I->second->getSourceRange());
149
150 // Emit the warning.
151 BR.EmitWarning(report);
152 }
153 }
154};
155
156class VISIBILITY_HIDDEN BadReceiver : public BuiltinBug {
157public:
158 BadReceiver()
159 : BuiltinBug("bad receiver",
160 "Receiver in message expression is an uninitialized value.") {}
161
162 virtual void EmitBuiltinWarnings(BugReporter& BR, GRExprEngine& Eng) {
163 for (GRExprEngine::UndefReceiversTy::iterator I=Eng.undef_receivers_begin(),
164 End = Eng.undef_receivers_end(); I!=End; ++I) {
165
166 // Generate a report for this bug.
167 RangedBugReport report(*this, *I);
168
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000169 ExplodedNode<GRState>* N = *I;
Ted Kremenek78d46242008-07-22 16:21:24 +0000170 Stmt *S = cast<PostStmt>(N->getLocation()).getStmt();
171 Expr* E = cast<ObjCMessageExpr>(S)->getReceiver();
172 assert (E && "Receiver cannot be NULL");
173 report.addRange(E->getSourceRange());
174
175 // Emit the warning.
176 BR.EmitWarning(report);
177 }
178 }
179};
180
181class VISIBILITY_HIDDEN RetStack : public BuiltinBug {
182public:
Ted Kremenek22bda882008-07-31 20:31:27 +0000183 RetStack() : BuiltinBug("return of stack address") {}
Ted Kremenek78d46242008-07-22 16:21:24 +0000184
185 virtual void EmitBuiltinWarnings(BugReporter& BR, GRExprEngine& Eng) {
Ted Kremenekb7714b22008-07-30 17:49:12 +0000186 for (GRExprEngine::ret_stackaddr_iterator I=Eng.ret_stackaddr_begin(),
187 End = Eng.ret_stackaddr_end(); I!=End; ++I) {
Ted Kremenek22bda882008-07-31 20:31:27 +0000188
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000189 ExplodedNode<GRState>* N = *I;
Ted Kremenekb7714b22008-07-30 17:49:12 +0000190 Stmt *S = cast<PostStmt>(N->getLocation()).getStmt();
191 Expr* E = cast<ReturnStmt>(S)->getRetValue();
192 assert (E && "Return expression cannot be NULL");
Ted Kremenek22bda882008-07-31 20:31:27 +0000193
194 // Get the value associated with E.
195 lval::DeclVal V =
196 cast<lval::DeclVal>(Eng.getStateManager().GetRVal(N->getState(), E));
197
198 // Generate a report for this bug.
199 std::ostringstream os;
200 os << "Address of stack memory associated with local variable '"
201 << V.getDecl()->getName() << "' returned.";
202
203 std::string s = os.str();
204
205 RangedBugReport report(*this, N, s.c_str());
Ted Kremenekb7714b22008-07-30 17:49:12 +0000206 report.addRange(E->getSourceRange());
207
208 // Emit the warning.
209 BR.EmitWarning(report);
210 }
Ted Kremenek78d46242008-07-22 16:21:24 +0000211 }
212};
213
214
215class VISIBILITY_HIDDEN UndefBranch : public BuiltinBug {
216 struct VISIBILITY_HIDDEN FindUndefExpr {
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000217 GRStateManager& VM;
218 const GRState* St;
Ted Kremenek78d46242008-07-22 16:21:24 +0000219
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000220 FindUndefExpr(GRStateManager& V, const GRState* S) : VM(V), St(S) {}
Ted Kremenek78d46242008-07-22 16:21:24 +0000221
Ted Kremenekb7714b22008-07-30 17:49:12 +0000222 Expr* FindExpr(Expr* Ex) {
Ted Kremenek78d46242008-07-22 16:21:24 +0000223 if (!MatchesCriteria(Ex))
Ted Kremenekb7714b22008-07-30 17:49:12 +0000224 return 0;
Ted Kremenek78d46242008-07-22 16:21:24 +0000225
Ted Kremenekb7714b22008-07-30 17:49:12 +0000226 for (Stmt::child_iterator I=Ex->child_begin(), E=Ex->child_end();I!=E;++I)
Ted Kremenek78d46242008-07-22 16:21:24 +0000227 if (Expr* ExI = dyn_cast_or_null<Expr>(*I)) {
228 Expr* E2 = FindExpr(ExI);
229 if (E2) return E2;
230 }
231
232 return Ex;
233 }
234
235 bool MatchesCriteria(Expr* Ex) { return VM.GetRVal(St, Ex).isUndef(); }
236 };
237
238public:
239 UndefBranch()
240 : BuiltinBug("uninitialized value",
241 "Branch condition evaluates to an uninitialized value.") {}
242
243 virtual void EmitBuiltinWarnings(BugReporter& BR, GRExprEngine& Eng) {
244 for (GRExprEngine::undef_branch_iterator I=Eng.undef_branches_begin(),
245 E=Eng.undef_branches_end(); I!=E; ++I) {
246
247 // What's going on here: we want to highlight the subexpression of the
248 // condition that is the most likely source of the "uninitialized
249 // branch condition." We do a recursive walk of the condition's
250 // subexpressions and roughly look for the most nested subexpression
251 // that binds to Undefined. We then highlight that expression's range.
252
253 BlockEdge B = cast<BlockEdge>((*I)->getLocation());
254 Expr* Ex = cast<Expr>(B.getSrc()->getTerminatorCondition());
255 assert (Ex && "Block must have a terminator.");
256
257 // Get the predecessor node and check if is a PostStmt with the Stmt
258 // being the terminator condition. We want to inspect the state
259 // of that node instead because it will contain main information about
260 // the subexpressions.
261
262 assert (!(*I)->pred_empty());
263
264 // Note: any predecessor will do. They should have identical state,
265 // since all the BlockEdge did was act as an error sink since the value
266 // had to already be undefined.
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000267 ExplodedNode<GRState> *N = *(*I)->pred_begin();
Ted Kremenek78d46242008-07-22 16:21:24 +0000268 ProgramPoint P = N->getLocation();
269
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000270 const GRState* St = (*I)->getState();
Ted Kremenek78d46242008-07-22 16:21:24 +0000271
272 if (PostStmt* PS = dyn_cast<PostStmt>(&P))
273 if (PS->getStmt() == Ex)
274 St = N->getState();
275
276 FindUndefExpr FindIt(Eng.getStateManager(), St);
277 Ex = FindIt.FindExpr(Ex);
278
279 RangedBugReport R(*this, *I);
280 R.addRange(Ex->getSourceRange());
281
282 BR.EmitWarning(R);
283 }
284 }
285};
286
287//===----------------------------------------------------------------------===//
288// __attribute__(nonnull) checking
289
290class VISIBILITY_HIDDEN CheckAttrNonNull : public GRSimpleAPICheck {
291 SimpleBugType BT;
292 std::list<RangedBugReport> Reports;
293
294public:
295 CheckAttrNonNull() :
Ted Kremenek8c036c72008-09-20 04:23:38 +0000296 BT("'nonnull' argument passed null", "API",
Ted Kremenek78d46242008-07-22 16:21:24 +0000297 "Null pointer passed as an argument to a 'nonnull' parameter") {}
298
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000299 virtual bool Audit(ExplodedNode<GRState>* N, GRStateManager& VMgr) {
Ted Kremenek78d46242008-07-22 16:21:24 +0000300 CallExpr* CE = cast<CallExpr>(cast<PostStmt>(N->getLocation()).getStmt());
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000301 const GRState* state = N->getState();
Ted Kremenek78d46242008-07-22 16:21:24 +0000302
303 RVal X = VMgr.GetRVal(state, CE->getCallee());
304
305 if (!isa<lval::FuncVal>(X))
306 return false;
307
308 FunctionDecl* FD = dyn_cast<FunctionDecl>(cast<lval::FuncVal>(X).getDecl());
309 const NonNullAttr* Att = FD->getAttr<NonNullAttr>();
310
311 if (!Att)
312 return false;
313
314 // Iterate through the arguments of CE and check them for null.
315
316 unsigned idx = 0;
317 bool hasError = false;
318
319 for (CallExpr::arg_iterator I=CE->arg_begin(), E=CE->arg_end(); I!=E;
320 ++I, ++idx) {
321
322 if (!VMgr.isEqual(state, *I, 0) || !Att->isNonNull(idx))
323 continue;
324
325 RangedBugReport R(BT, N);
326 R.addRange((*I)->getSourceRange());
327 Reports.push_back(R);
328 hasError = true;
329 }
330
331 return hasError;
332 }
333
334 virtual void EmitWarnings(BugReporter& BR) {
335 for (std::list<RangedBugReport>::iterator I=Reports.begin(),
336 E=Reports.end(); I!=E; ++I)
337 BR.EmitWarning(*I);
338 }
339};
340} // end anonymous namespace
341
342//===----------------------------------------------------------------------===//
343// Check registration.
344
345void GRExprEngine::RegisterInternalChecks() {
346 Register(new NullDeref());
347 Register(new UndefinedDeref());
348 Register(new UndefBranch());
349 Register(new DivZero());
350 Register(new UndefResult());
351 Register(new BadCall());
352 Register(new RetStack());
353 Register(new BadArg());
354 Register(new BadMsgExprArg());
355 Register(new BadReceiver());
356 AddCheck(new CheckAttrNonNull(), Stmt::CallExprClass);
357}