blob: d772744f17cc7c9e7a403e749f4625d342641576 [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 Kremenek8aed8062008-10-31 00:13:20 +000017#include "clang/Basic/SourceManager.h"
Ted Kremenek78d46242008-07-22 16:21:24 +000018#include "llvm/Support/Compiler.h"
Ted Kremenekad51a602008-10-31 00:18:30 +000019#include "llvm/Support/raw_ostream.h"
Ted Kremenek78d46242008-07-22 16:21:24 +000020
21using namespace clang;
22
23//===----------------------------------------------------------------------===//
24// Utility functions.
25//===----------------------------------------------------------------------===//
26
27template <typename ITERATOR> inline
Ted Kremenek4adc81e2008-08-13 04:27:00 +000028ExplodedNode<GRState>* GetNode(ITERATOR I) {
Ted Kremenek78d46242008-07-22 16:21:24 +000029 return *I;
30}
31
32template <> inline
Ted Kremenek4adc81e2008-08-13 04:27:00 +000033ExplodedNode<GRState>* GetNode(GRExprEngine::undef_arg_iterator I) {
Ted Kremenek78d46242008-07-22 16:21:24 +000034 return I->first;
35}
36
37//===----------------------------------------------------------------------===//
38// Bug Descriptions.
39//===----------------------------------------------------------------------===//
40
41namespace {
42class VISIBILITY_HIDDEN BuiltinBug : public BugTypeCacheLocation {
43 const char* name;
44 const char* desc;
45public:
Ted Kremenek22bda882008-07-31 20:31:27 +000046 BuiltinBug(const char* n, const char* d = 0) : name(n), desc(d) {}
Ted Kremenek78d46242008-07-22 16:21:24 +000047 virtual const char* getName() const { return name; }
Ted Kremenek22bda882008-07-31 20:31:27 +000048 virtual const char* getDescription() const {
49 return desc ? desc : name;
50 }
51
Ted Kremenek78d46242008-07-22 16:21:24 +000052 virtual void EmitBuiltinWarnings(BugReporter& BR, GRExprEngine& Eng) = 0;
53 virtual void EmitWarnings(BugReporter& BR) {
54 EmitBuiltinWarnings(BR, cast<GRBugReporter>(BR).getEngine());
55 }
56
57 template <typename ITER>
58 void Emit(BugReporter& BR, ITER I, ITER E) {
59 for (; I != E; ++I) {
60 BugReport R(*this, GetNode(I));
61 BR.EmitWarning(R);
62 }
63 }
Ted Kremenek27133472008-09-21 18:57:28 +000064
65 virtual const char* getCategory() const { return "Logic Errors"; }
Ted Kremenek78d46242008-07-22 16:21:24 +000066};
67
68class VISIBILITY_HIDDEN NullDeref : public BuiltinBug {
69public:
70 NullDeref() : BuiltinBug("null dereference",
71 "Dereference of null pointer.") {}
72
73 virtual void EmitBuiltinWarnings(BugReporter& BR, GRExprEngine& Eng) {
74 Emit(BR, Eng.null_derefs_begin(), Eng.null_derefs_end());
75 }
76};
77
78class VISIBILITY_HIDDEN UndefinedDeref : public BuiltinBug {
79public:
Ted Kremenek27133472008-09-21 18:57:28 +000080 UndefinedDeref() : BuiltinBug("uninitialized pointer dereference",
Ted Kremenek78d46242008-07-22 16:21:24 +000081 "Dereference of undefined value.") {}
82
83 virtual void EmitBuiltinWarnings(BugReporter& BR, GRExprEngine& Eng) {
84 Emit(BR, Eng.undef_derefs_begin(), Eng.undef_derefs_end());
85 }
86};
87
88class VISIBILITY_HIDDEN DivZero : public BuiltinBug {
89public:
90 DivZero() : BuiltinBug("divide-by-zero",
91 "Division by zero/undefined value.") {}
92
93 virtual void EmitBuiltinWarnings(BugReporter& BR, GRExprEngine& Eng) {
94 Emit(BR, Eng.explicit_bad_divides_begin(), Eng.explicit_bad_divides_end());
95 }
96};
97
98class VISIBILITY_HIDDEN UndefResult : public BuiltinBug {
99public:
100 UndefResult() : BuiltinBug("undefined result",
101 "Result of operation is undefined.") {}
102
103 virtual void EmitBuiltinWarnings(BugReporter& BR, GRExprEngine& Eng) {
104 Emit(BR, Eng.undef_results_begin(), Eng.undef_results_end());
105 }
106};
107
108class VISIBILITY_HIDDEN BadCall : public BuiltinBug {
109public:
110 BadCall()
111 : BuiltinBug("invalid function call",
112 "Called function is a NULL or undefined function pointer value.") {}
113
114 virtual void EmitBuiltinWarnings(BugReporter& BR, GRExprEngine& Eng) {
115 Emit(BR, Eng.bad_calls_begin(), Eng.bad_calls_end());
116 }
117};
118
119class VISIBILITY_HIDDEN BadArg : public BuiltinBug {
120public:
Ted Kremenek27133472008-09-21 18:57:28 +0000121 BadArg() : BuiltinBug("uninitialized argument",
Ted Kremenek78d46242008-07-22 16:21:24 +0000122 "Pass-by-value argument in function is undefined.") {}
123
Ted Kremenek27133472008-09-21 18:57:28 +0000124 BadArg(const char* d) : BuiltinBug("uninitialized argument", d) {}
Ted Kremenek78d46242008-07-22 16:21:24 +0000125
126 virtual void EmitBuiltinWarnings(BugReporter& BR, GRExprEngine& Eng) {
127 for (GRExprEngine::UndefArgsTy::iterator I = Eng.undef_arg_begin(),
128 E = Eng.undef_arg_end(); I!=E; ++I) {
129
130 // Generate a report for this bug.
131 RangedBugReport report(*this, I->first);
132 report.addRange(I->second->getSourceRange());
133
134 // Emit the warning.
135 BR.EmitWarning(report);
136 }
137 }
138};
139
140class VISIBILITY_HIDDEN BadMsgExprArg : public BadArg {
141public:
142 BadMsgExprArg()
143 : BadArg("Pass-by-value argument in message expression is undefined.") {}
144
145 virtual void EmitBuiltinWarnings(BugReporter& BR, GRExprEngine& Eng) {
146 for (GRExprEngine::UndefArgsTy::iterator I=Eng.msg_expr_undef_arg_begin(),
147 E = Eng.msg_expr_undef_arg_end(); I!=E; ++I) {
148
149 // Generate a report for this bug.
150 RangedBugReport report(*this, I->first);
151 report.addRange(I->second->getSourceRange());
152
153 // Emit the warning.
154 BR.EmitWarning(report);
155 }
156 }
157};
158
159class VISIBILITY_HIDDEN BadReceiver : public BuiltinBug {
160public:
161 BadReceiver()
Ted Kremenek27133472008-09-21 18:57:28 +0000162 : BuiltinBug("uninitialized receiver",
Ted Kremenek78d46242008-07-22 16:21:24 +0000163 "Receiver in message expression is an uninitialized value.") {}
164
165 virtual void EmitBuiltinWarnings(BugReporter& BR, GRExprEngine& Eng) {
166 for (GRExprEngine::UndefReceiversTy::iterator I=Eng.undef_receivers_begin(),
167 End = Eng.undef_receivers_end(); I!=End; ++I) {
168
169 // Generate a report for this bug.
170 RangedBugReport report(*this, *I);
171
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000172 ExplodedNode<GRState>* N = *I;
Ted Kremenek78d46242008-07-22 16:21:24 +0000173 Stmt *S = cast<PostStmt>(N->getLocation()).getStmt();
174 Expr* E = cast<ObjCMessageExpr>(S)->getReceiver();
175 assert (E && "Receiver cannot be NULL");
176 report.addRange(E->getSourceRange());
177
178 // Emit the warning.
179 BR.EmitWarning(report);
180 }
181 }
182};
183
184class VISIBILITY_HIDDEN RetStack : public BuiltinBug {
185public:
Ted Kremenek22bda882008-07-31 20:31:27 +0000186 RetStack() : BuiltinBug("return of stack address") {}
Ted Kremenek78d46242008-07-22 16:21:24 +0000187
188 virtual void EmitBuiltinWarnings(BugReporter& BR, GRExprEngine& Eng) {
Ted Kremenekb7714b22008-07-30 17:49:12 +0000189 for (GRExprEngine::ret_stackaddr_iterator I=Eng.ret_stackaddr_begin(),
190 End = Eng.ret_stackaddr_end(); I!=End; ++I) {
Ted Kremenek22bda882008-07-31 20:31:27 +0000191
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000192 ExplodedNode<GRState>* N = *I;
Ted Kremenekb7714b22008-07-30 17:49:12 +0000193 Stmt *S = cast<PostStmt>(N->getLocation()).getStmt();
194 Expr* E = cast<ReturnStmt>(S)->getRetValue();
195 assert (E && "Return expression cannot be NULL");
Ted Kremenek22bda882008-07-31 20:31:27 +0000196
197 // Get the value associated with E.
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000198 loc::MemRegionVal V =
199 cast<loc::MemRegionVal>(Eng.getStateManager().GetSVal(N->getState(),
Ted Kremenek9e240492008-10-04 05:50:14 +0000200 E));
Ted Kremenek22bda882008-07-31 20:31:27 +0000201
202 // Generate a report for this bug.
Ted Kremenekad51a602008-10-31 00:18:30 +0000203 std::string buf;
204 llvm::raw_string_ostream os(buf);
Ted Kremenek8aed8062008-10-31 00:13:20 +0000205 SourceRange R;
Ted Kremenek22bda882008-07-31 20:31:27 +0000206
Ted Kremenek8aed8062008-10-31 00:13:20 +0000207 // Check if the region is a compound literal.
208 if (const CompoundLiteralRegion* CR =
209 dyn_cast<CompoundLiteralRegion>(V.getRegion())) {
210
211 const CompoundLiteralExpr* CL = CR->getLiteralExpr();
212 os << "Address of stack memory associated with a compound literal "
213 "declared on line "
214 << BR.getSourceManager().getLogicalLineNumber(CL->getLocStart())
215 << " returned.";
216
217 R = CL->getSourceRange();
218 }
219 else {
220 os << "Address of stack memory associated with local variable '"
221 << V.getRegion()->getString() << "' returned.";
222 }
Ted Kremenek22bda882008-07-31 20:31:27 +0000223
Ted Kremenekad51a602008-10-31 00:18:30 +0000224 RangedBugReport report(*this, N, os.str().c_str());
Ted Kremenekb7714b22008-07-30 17:49:12 +0000225 report.addRange(E->getSourceRange());
Ted Kremenek8aed8062008-10-31 00:13:20 +0000226 if (R.isValid()) report.addRange(R);
Ted Kremenekb7714b22008-07-30 17:49:12 +0000227
228 // Emit the warning.
229 BR.EmitWarning(report);
230 }
Ted Kremenek78d46242008-07-22 16:21:24 +0000231 }
232};
233
234
235class VISIBILITY_HIDDEN UndefBranch : public BuiltinBug {
236 struct VISIBILITY_HIDDEN FindUndefExpr {
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000237 GRStateManager& VM;
238 const GRState* St;
Ted Kremenek78d46242008-07-22 16:21:24 +0000239
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000240 FindUndefExpr(GRStateManager& V, const GRState* S) : VM(V), St(S) {}
Ted Kremenek78d46242008-07-22 16:21:24 +0000241
Ted Kremenekb7714b22008-07-30 17:49:12 +0000242 Expr* FindExpr(Expr* Ex) {
Ted Kremenek78d46242008-07-22 16:21:24 +0000243 if (!MatchesCriteria(Ex))
Ted Kremenekb7714b22008-07-30 17:49:12 +0000244 return 0;
Ted Kremenek78d46242008-07-22 16:21:24 +0000245
Ted Kremenekb7714b22008-07-30 17:49:12 +0000246 for (Stmt::child_iterator I=Ex->child_begin(), E=Ex->child_end();I!=E;++I)
Ted Kremenek78d46242008-07-22 16:21:24 +0000247 if (Expr* ExI = dyn_cast_or_null<Expr>(*I)) {
248 Expr* E2 = FindExpr(ExI);
249 if (E2) return E2;
250 }
251
252 return Ex;
253 }
254
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000255 bool MatchesCriteria(Expr* Ex) { return VM.GetSVal(St, Ex).isUndef(); }
Ted Kremenek78d46242008-07-22 16:21:24 +0000256 };
257
258public:
259 UndefBranch()
260 : BuiltinBug("uninitialized value",
261 "Branch condition evaluates to an uninitialized value.") {}
262
263 virtual void EmitBuiltinWarnings(BugReporter& BR, GRExprEngine& Eng) {
264 for (GRExprEngine::undef_branch_iterator I=Eng.undef_branches_begin(),
265 E=Eng.undef_branches_end(); I!=E; ++I) {
266
267 // What's going on here: we want to highlight the subexpression of the
268 // condition that is the most likely source of the "uninitialized
269 // branch condition." We do a recursive walk of the condition's
270 // subexpressions and roughly look for the most nested subexpression
271 // that binds to Undefined. We then highlight that expression's range.
272
273 BlockEdge B = cast<BlockEdge>((*I)->getLocation());
274 Expr* Ex = cast<Expr>(B.getSrc()->getTerminatorCondition());
275 assert (Ex && "Block must have a terminator.");
276
277 // Get the predecessor node and check if is a PostStmt with the Stmt
278 // being the terminator condition. We want to inspect the state
279 // of that node instead because it will contain main information about
280 // the subexpressions.
281
282 assert (!(*I)->pred_empty());
283
284 // Note: any predecessor will do. They should have identical state,
285 // since all the BlockEdge did was act as an error sink since the value
286 // had to already be undefined.
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000287 ExplodedNode<GRState> *N = *(*I)->pred_begin();
Ted Kremenek78d46242008-07-22 16:21:24 +0000288 ProgramPoint P = N->getLocation();
289
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000290 const GRState* St = (*I)->getState();
Ted Kremenek78d46242008-07-22 16:21:24 +0000291
292 if (PostStmt* PS = dyn_cast<PostStmt>(&P))
293 if (PS->getStmt() == Ex)
294 St = N->getState();
295
296 FindUndefExpr FindIt(Eng.getStateManager(), St);
297 Ex = FindIt.FindExpr(Ex);
298
299 RangedBugReport R(*this, *I);
300 R.addRange(Ex->getSourceRange());
301
302 BR.EmitWarning(R);
303 }
304 }
305};
306
307//===----------------------------------------------------------------------===//
308// __attribute__(nonnull) checking
309
310class VISIBILITY_HIDDEN CheckAttrNonNull : public GRSimpleAPICheck {
311 SimpleBugType BT;
312 std::list<RangedBugReport> Reports;
313
314public:
315 CheckAttrNonNull() :
Ted Kremenek8c036c72008-09-20 04:23:38 +0000316 BT("'nonnull' argument passed null", "API",
Ted Kremenek78d46242008-07-22 16:21:24 +0000317 "Null pointer passed as an argument to a 'nonnull' parameter") {}
318
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000319 virtual bool Audit(ExplodedNode<GRState>* N, GRStateManager& VMgr) {
Ted Kremenek78d46242008-07-22 16:21:24 +0000320 CallExpr* CE = cast<CallExpr>(cast<PostStmt>(N->getLocation()).getStmt());
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000321 const GRState* state = N->getState();
Ted Kremenek78d46242008-07-22 16:21:24 +0000322
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000323 SVal X = VMgr.GetSVal(state, CE->getCallee());
Ted Kremenek78d46242008-07-22 16:21:24 +0000324
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000325 if (!isa<loc::FuncVal>(X))
Ted Kremenek78d46242008-07-22 16:21:24 +0000326 return false;
327
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000328 FunctionDecl* FD = dyn_cast<FunctionDecl>(cast<loc::FuncVal>(X).getDecl());
Ted Kremenek78d46242008-07-22 16:21:24 +0000329 const NonNullAttr* Att = FD->getAttr<NonNullAttr>();
330
331 if (!Att)
332 return false;
333
334 // Iterate through the arguments of CE and check them for null.
335
336 unsigned idx = 0;
337 bool hasError = false;
338
339 for (CallExpr::arg_iterator I=CE->arg_begin(), E=CE->arg_end(); I!=E;
340 ++I, ++idx) {
341
342 if (!VMgr.isEqual(state, *I, 0) || !Att->isNonNull(idx))
343 continue;
344
345 RangedBugReport R(BT, N);
346 R.addRange((*I)->getSourceRange());
347 Reports.push_back(R);
348 hasError = true;
349 }
350
351 return hasError;
352 }
353
354 virtual void EmitWarnings(BugReporter& BR) {
355 for (std::list<RangedBugReport>::iterator I=Reports.begin(),
356 E=Reports.end(); I!=E; ++I)
357 BR.EmitWarning(*I);
358 }
359};
360} // end anonymous namespace
361
362//===----------------------------------------------------------------------===//
363// Check registration.
364
365void GRExprEngine::RegisterInternalChecks() {
366 Register(new NullDeref());
367 Register(new UndefinedDeref());
368 Register(new UndefBranch());
369 Register(new DivZero());
370 Register(new UndefResult());
371 Register(new BadCall());
372 Register(new RetStack());
373 Register(new BadArg());
374 Register(new BadMsgExprArg());
375 Register(new BadReceiver());
376 AddCheck(new CheckAttrNonNull(), Stmt::CallExprClass);
377}