blob: 695cc037e86033914ebedc5d36aecc3c920a113b [file] [log] [blame]
Jordy Rose910c4052011-09-02 06:44:22 +00001//==-- RetainCountChecker.cpp - Checks for leaks and other issues -*- C++ -*--//
Ted Kremenek2fff37e2008-03-06 00:08:09 +00002//
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//
Jordy Rose910c4052011-09-02 06:44:22 +000010// This file defines the methods for RetainCountChecker, which implements
11// a reference count checker for Core Foundation and Cocoa on (Mac OS X).
Ted Kremenek2fff37e2008-03-06 00:08:09 +000012//
13//===----------------------------------------------------------------------===//
14
Jordy Rose910c4052011-09-02 06:44:22 +000015#include "ClangSACheckers.h"
Mike Stump1eb44332009-09-09 15:08:12 +000016#include "clang/AST/DeclObjC.h"
Ted Kremenekb2771592011-03-30 17:41:19 +000017#include "clang/AST/DeclCXX.h"
Ted Kremenek0b526b42010-02-18 00:05:58 +000018#include "clang/Basic/LangOptions.h"
19#include "clang/Basic/SourceManager.h"
Jordy Rose910c4052011-09-02 06:44:22 +000020#include "clang/Analysis/DomainSpecific/CocoaConventions.h"
21#include "clang/StaticAnalyzer/Core/Checker.h"
22#include "clang/StaticAnalyzer/Core/CheckerManager.h"
Ted Kremenek9b663712011-02-10 01:03:03 +000023#include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
24#include "clang/StaticAnalyzer/Core/BugReporter/PathDiagnostic.h"
Jordy Rose910c4052011-09-02 06:44:22 +000025#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
Ted Kremenek9b663712011-02-10 01:03:03 +000026#include "clang/StaticAnalyzer/Core/PathSensitive/ExprEngineBuilders.h"
Ted Kremenek18c66fd2011-08-15 22:09:50 +000027#include "clang/StaticAnalyzer/Core/PathSensitive/ProgramStateTrait.h"
Ted Kremenek9b663712011-02-10 01:03:03 +000028#include "clang/StaticAnalyzer/Core/PathSensitive/SymbolManager.h"
Jordy Rosed1e5a892011-09-02 08:02:59 +000029#include "clang/StaticAnalyzer/Core/PathSensitive/ObjCMessage.h"
Ted Kremenek6b3a0f72008-03-11 06:39:11 +000030#include "llvm/ADT/DenseMap.h"
31#include "llvm/ADT/FoldingSet.h"
Ted Kremenek6d348932008-10-21 15:53:15 +000032#include "llvm/ADT/ImmutableList.h"
Ted Kremenek0b526b42010-02-18 00:05:58 +000033#include "llvm/ADT/ImmutableMap.h"
Ted Kremenek6ed9afc2008-05-16 18:33:44 +000034#include "llvm/ADT/STLExtras.h"
Ted Kremenek0b526b42010-02-18 00:05:58 +000035#include "llvm/ADT/StringExtras.h"
Chris Lattner5f9e2722011-07-23 10:55:15 +000036#include <cstdarg>
Ted Kremenek2fff37e2008-03-06 00:08:09 +000037
38using namespace clang;
Ted Kremenek9ef65372010-12-23 07:20:52 +000039using namespace ento;
Ted Kremeneka64e89b2010-01-27 06:13:48 +000040using llvm::StrInStrNoCase;
Ted Kremenek4c79e552008-11-05 16:54:44 +000041
Ted Kremenekd775c662010-05-21 21:57:00 +000042namespace {
Jordy Rose910c4052011-09-02 06:44:22 +000043/// Wrapper around different kinds of node builder, so that helper functions
44/// can have a common interface.
Francois Pichetea097852011-01-11 10:41:37 +000045class GenericNodeBuilderRefCount {
Argyrios Kyrtzidisd2592a32010-12-22 18:53:44 +000046 StmtNodeBuilder *SNB;
Zhongxing Xu03509ae2010-07-20 06:22:24 +000047 const Stmt *S;
Ted Kremenekca804532011-08-12 23:04:46 +000048 const ProgramPointTag *tag;
Ted Kremeneke36de1f2011-01-11 02:34:45 +000049 EndOfFunctionNodeBuilder *ENB;
Ted Kremenek9d9d3a62009-05-08 23:09:42 +000050public:
Francois Pichetea097852011-01-11 10:41:37 +000051 GenericNodeBuilderRefCount(StmtNodeBuilder &snb, const Stmt *s,
Ted Kremenekca804532011-08-12 23:04:46 +000052 const ProgramPointTag *t)
Ted Kremenek9d9d3a62009-05-08 23:09:42 +000053 : SNB(&snb), S(s), tag(t), ENB(0) {}
Zhongxing Xu031ccc02009-08-06 12:48:26 +000054
Francois Pichetea097852011-01-11 10:41:37 +000055 GenericNodeBuilderRefCount(EndOfFunctionNodeBuilder &enb)
Ted Kremenek9d9d3a62009-05-08 23:09:42 +000056 : SNB(0), S(0), tag(0), ENB(&enb) {}
Mike Stump1eb44332009-09-09 15:08:12 +000057
Ted Kremenek18c66fd2011-08-15 22:09:50 +000058 ExplodedNode *MakeNode(const ProgramState *state, ExplodedNode *Pred) {
Ted Kremenek9d9d3a62009-05-08 23:09:42 +000059 if (SNB)
Mike Stump1eb44332009-09-09 15:08:12 +000060 return SNB->generateNode(PostStmt(S, Pred->getLocationContext(), tag),
Zhongxing Xu25e695b2009-08-15 03:17:38 +000061 state, Pred);
Mike Stump1eb44332009-09-09 15:08:12 +000062
Ted Kremenek9d9d3a62009-05-08 23:09:42 +000063 assert(ENB);
Ted Kremenek80c24182009-05-09 00:44:07 +000064 return ENB->generateNode(state, Pred);
Ted Kremenek9d9d3a62009-05-08 23:09:42 +000065 }
66};
67} // end anonymous namespace
68
Ted Kremenek05cbe1a2008-04-09 23:49:11 +000069//===----------------------------------------------------------------------===//
Ted Kremenek553cf182008-06-25 21:21:56 +000070// Primitives used for constructing summaries for function/method calls.
Ted Kremenek05cbe1a2008-04-09 23:49:11 +000071//===----------------------------------------------------------------------===//
72
Ted Kremenek553cf182008-06-25 21:21:56 +000073/// ArgEffect is used to summarize a function/method call's effect on a
74/// particular argument.
Jordy Rosebd85b132011-08-24 19:10:50 +000075enum ArgEffect { DoNothing, Autorelease, Dealloc, DecRef, DecRefMsg,
John McCallf85e1932011-06-15 23:02:42 +000076 DecRefBridgedTransfered,
Jordy Rosebd85b132011-08-24 19:10:50 +000077 IncRefMsg, IncRef, MakeCollectable, MayEscape,
Ted Kremenekf95e9fc2009-03-17 19:42:23 +000078 NewAutoreleasePool, SelfOwn, StopTracking };
Ted Kremenek553cf182008-06-25 21:21:56 +000079
Ted Kremenek6b3a0f72008-03-11 06:39:11 +000080namespace llvm {
Ted Kremenekb77449c2009-05-03 05:20:50 +000081template <> struct FoldingSetTrait<ArgEffect> {
82static inline void Profile(const ArgEffect X, FoldingSetNodeID& ID) {
83 ID.AddInteger((unsigned) X);
84}
Ted Kremenek553cf182008-06-25 21:21:56 +000085};
Ted Kremenek6b3a0f72008-03-11 06:39:11 +000086} // end llvm namespace
87
Ted Kremenekb77449c2009-05-03 05:20:50 +000088/// ArgEffects summarizes the effects of a function/method call on all of
89/// its arguments.
90typedef llvm::ImmutableMap<unsigned,ArgEffect> ArgEffects;
91
Ted Kremenek6b3a0f72008-03-11 06:39:11 +000092namespace {
Ted Kremenek553cf182008-06-25 21:21:56 +000093
94/// RetEffect is used to summarize a function/method call's behavior with
Mike Stump1eb44332009-09-09 15:08:12 +000095/// respect to its return value.
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +000096class RetEffect {
Ted Kremenek6b3a0f72008-03-11 06:39:11 +000097public:
Jordy Rose76c506f2011-08-21 21:58:18 +000098 enum Kind { NoRet, OwnedSymbol, OwnedAllocatedSymbol,
John McCallf85e1932011-06-15 23:02:42 +000099 NotOwnedSymbol, GCNotOwnedSymbol, ARCNotOwnedSymbol,
Ted Kremenek78a35a32009-05-12 20:06:54 +0000100 OwnedWhenTrackedReceiver };
Mike Stump1eb44332009-09-09 15:08:12 +0000101
102 enum ObjKind { CF, ObjC, AnyObj };
Ted Kremenek2d1652e2009-01-28 05:56:51 +0000103
Ted Kremenek6b3a0f72008-03-11 06:39:11 +0000104private:
Ted Kremenek2d1652e2009-01-28 05:56:51 +0000105 Kind K;
106 ObjKind O;
Ted Kremenek2d1652e2009-01-28 05:56:51 +0000107
Jordy Rose76c506f2011-08-21 21:58:18 +0000108 RetEffect(Kind k, ObjKind o = AnyObj) : K(k), O(o) {}
Mike Stump1eb44332009-09-09 15:08:12 +0000109
Ted Kremenek6b3a0f72008-03-11 06:39:11 +0000110public:
Ted Kremenek2d1652e2009-01-28 05:56:51 +0000111 Kind getKind() const { return K; }
112
113 ObjKind getObjKind() const { return O; }
Mike Stump1eb44332009-09-09 15:08:12 +0000114
Ted Kremeneka8833552009-04-29 23:03:22 +0000115 bool isOwned() const {
Ted Kremenek78a35a32009-05-12 20:06:54 +0000116 return K == OwnedSymbol || K == OwnedAllocatedSymbol ||
117 K == OwnedWhenTrackedReceiver;
Ted Kremeneka8833552009-04-29 23:03:22 +0000118 }
Mike Stump1eb44332009-09-09 15:08:12 +0000119
Jordy Rose4df54fe2011-08-23 04:27:15 +0000120 bool operator==(const RetEffect &Other) const {
121 return K == Other.K && O == Other.O;
122 }
123
Ted Kremenek78a35a32009-05-12 20:06:54 +0000124 static RetEffect MakeOwnedWhenTrackedReceiver() {
125 return RetEffect(OwnedWhenTrackedReceiver, ObjC);
126 }
Mike Stump1eb44332009-09-09 15:08:12 +0000127
Ted Kremenek2d1652e2009-01-28 05:56:51 +0000128 static RetEffect MakeOwned(ObjKind o, bool isAllocated = false) {
129 return RetEffect(isAllocated ? OwnedAllocatedSymbol : OwnedSymbol, o);
Mike Stump1eb44332009-09-09 15:08:12 +0000130 }
Ted Kremenek2d1652e2009-01-28 05:56:51 +0000131 static RetEffect MakeNotOwned(ObjKind o) {
132 return RetEffect(NotOwnedSymbol, o);
Ted Kremeneke798e7c2009-04-27 19:14:45 +0000133 }
134 static RetEffect MakeGCNotOwned() {
135 return RetEffect(GCNotOwnedSymbol, ObjC);
136 }
John McCallf85e1932011-06-15 23:02:42 +0000137 static RetEffect MakeARCNotOwned() {
138 return RetEffect(ARCNotOwnedSymbol, ObjC);
139 }
Ted Kremenek553cf182008-06-25 21:21:56 +0000140 static RetEffect MakeNoRet() {
141 return RetEffect(NoRet);
Ted Kremeneka7344702008-06-23 18:02:52 +0000142 }
Ted Kremenek6b3a0f72008-03-11 06:39:11 +0000143};
Mike Stump1eb44332009-09-09 15:08:12 +0000144
Ted Kremenekb7ddd9b2009-11-13 01:54:21 +0000145//===----------------------------------------------------------------------===//
146// Reference-counting logic (typestate + counts).
147//===----------------------------------------------------------------------===//
Mike Stump1eb44332009-09-09 15:08:12 +0000148
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +0000149class RefVal {
Ted Kremenekb7ddd9b2009-11-13 01:54:21 +0000150public:
151 enum Kind {
152 Owned = 0, // Owning reference.
153 NotOwned, // Reference is not owned by still valid (not freed).
154 Released, // Object has been released.
155 ReturnedOwned, // Returned object passes ownership to caller.
156 ReturnedNotOwned, // Return object does not pass ownership to caller.
157 ERROR_START,
158 ErrorDeallocNotOwned, // -dealloc called on non-owned object.
159 ErrorDeallocGC, // Calling -dealloc with GC enabled.
160 ErrorUseAfterRelease, // Object used after released.
161 ErrorReleaseNotOwned, // Release of an object that was not owned.
162 ERROR_LEAK_START,
163 ErrorLeak, // A memory leak due to excessive reference counts.
164 ErrorLeakReturned, // A memory leak due to the returning method not having
165 // the correct naming conventions.
166 ErrorGCLeakReturned,
167 ErrorOverAutorelease,
168 ErrorReturnedNotOwned
169 };
Ted Kremenekdcee3ce2010-07-01 20:16:50 +0000170
Ted Kremenekb7ddd9b2009-11-13 01:54:21 +0000171private:
172 Kind kind;
173 RetEffect::ObjKind okind;
174 unsigned Cnt;
175 unsigned ACnt;
176 QualType T;
Ted Kremenekdcee3ce2010-07-01 20:16:50 +0000177
Ted Kremenekb7ddd9b2009-11-13 01:54:21 +0000178 RefVal(Kind k, RetEffect::ObjKind o, unsigned cnt, unsigned acnt, QualType t)
179 : kind(k), okind(o), Cnt(cnt), ACnt(acnt), T(t) {}
Ted Kremenekdcee3ce2010-07-01 20:16:50 +0000180
Ted Kremenekb7ddd9b2009-11-13 01:54:21 +0000181public:
182 Kind getKind() const { return kind; }
Ted Kremenekdcee3ce2010-07-01 20:16:50 +0000183
Ted Kremenekb7ddd9b2009-11-13 01:54:21 +0000184 RetEffect::ObjKind getObjKind() const { return okind; }
Ted Kremenekdcee3ce2010-07-01 20:16:50 +0000185
Ted Kremenekb7ddd9b2009-11-13 01:54:21 +0000186 unsigned getCount() const { return Cnt; }
187 unsigned getAutoreleaseCount() const { return ACnt; }
188 unsigned getCombinedCounts() const { return Cnt + ACnt; }
189 void clearCounts() { Cnt = 0; ACnt = 0; }
190 void setCount(unsigned i) { Cnt = i; }
191 void setAutoreleaseCount(unsigned i) { ACnt = i; }
Ted Kremenekdcee3ce2010-07-01 20:16:50 +0000192
Ted Kremenekb7ddd9b2009-11-13 01:54:21 +0000193 QualType getType() const { return T; }
Ted Kremenekdcee3ce2010-07-01 20:16:50 +0000194
Ted Kremenekb7ddd9b2009-11-13 01:54:21 +0000195 bool isOwned() const {
196 return getKind() == Owned;
197 }
Ted Kremenekdcee3ce2010-07-01 20:16:50 +0000198
Ted Kremenekb7ddd9b2009-11-13 01:54:21 +0000199 bool isNotOwned() const {
200 return getKind() == NotOwned;
201 }
Ted Kremenekdcee3ce2010-07-01 20:16:50 +0000202
Ted Kremenekb7ddd9b2009-11-13 01:54:21 +0000203 bool isReturnedOwned() const {
204 return getKind() == ReturnedOwned;
205 }
Ted Kremenekdcee3ce2010-07-01 20:16:50 +0000206
Ted Kremenekb7ddd9b2009-11-13 01:54:21 +0000207 bool isReturnedNotOwned() const {
208 return getKind() == ReturnedNotOwned;
209 }
Ted Kremenekdcee3ce2010-07-01 20:16:50 +0000210
Ted Kremenekb7ddd9b2009-11-13 01:54:21 +0000211 static RefVal makeOwned(RetEffect::ObjKind o, QualType t,
212 unsigned Count = 1) {
213 return RefVal(Owned, o, Count, 0, t);
214 }
Ted Kremenekdcee3ce2010-07-01 20:16:50 +0000215
Ted Kremenekb7ddd9b2009-11-13 01:54:21 +0000216 static RefVal makeNotOwned(RetEffect::ObjKind o, QualType t,
217 unsigned Count = 0) {
218 return RefVal(NotOwned, o, Count, 0, t);
219 }
Ted Kremenekdcee3ce2010-07-01 20:16:50 +0000220
Ted Kremenekb7ddd9b2009-11-13 01:54:21 +0000221 // Comparison, profiling, and pretty-printing.
Ted Kremenekdcee3ce2010-07-01 20:16:50 +0000222
Ted Kremenekb7ddd9b2009-11-13 01:54:21 +0000223 bool operator==(const RefVal& X) const {
224 return kind == X.kind && Cnt == X.Cnt && T == X.T && ACnt == X.ACnt;
225 }
Ted Kremenekdcee3ce2010-07-01 20:16:50 +0000226
Ted Kremenekb7ddd9b2009-11-13 01:54:21 +0000227 RefVal operator-(size_t i) const {
228 return RefVal(getKind(), getObjKind(), getCount() - i,
229 getAutoreleaseCount(), getType());
230 }
Ted Kremenekdcee3ce2010-07-01 20:16:50 +0000231
Ted Kremenekb7ddd9b2009-11-13 01:54:21 +0000232 RefVal operator+(size_t i) const {
233 return RefVal(getKind(), getObjKind(), getCount() + i,
234 getAutoreleaseCount(), getType());
235 }
Ted Kremenekdcee3ce2010-07-01 20:16:50 +0000236
Ted Kremenekb7ddd9b2009-11-13 01:54:21 +0000237 RefVal operator^(Kind k) const {
238 return RefVal(k, getObjKind(), getCount(), getAutoreleaseCount(),
239 getType());
240 }
Ted Kremenekdcee3ce2010-07-01 20:16:50 +0000241
Ted Kremenekb7ddd9b2009-11-13 01:54:21 +0000242 RefVal autorelease() const {
243 return RefVal(getKind(), getObjKind(), getCount(), getAutoreleaseCount()+1,
244 getType());
245 }
Ted Kremenekdcee3ce2010-07-01 20:16:50 +0000246
Ted Kremenekb7ddd9b2009-11-13 01:54:21 +0000247 void Profile(llvm::FoldingSetNodeID& ID) const {
248 ID.AddInteger((unsigned) kind);
249 ID.AddInteger(Cnt);
250 ID.AddInteger(ACnt);
251 ID.Add(T);
252 }
Ted Kremenekdcee3ce2010-07-01 20:16:50 +0000253
Ted Kremenek9c378f72011-08-12 23:37:29 +0000254 void print(raw_ostream &Out) const;
Ted Kremenekb7ddd9b2009-11-13 01:54:21 +0000255};
256
Ted Kremenek9c378f72011-08-12 23:37:29 +0000257void RefVal::print(raw_ostream &Out) const {
Ted Kremenekb7ddd9b2009-11-13 01:54:21 +0000258 if (!T.isNull())
Jordy Rosedbd658e2011-08-28 19:11:56 +0000259 Out << "Tracked " << T.getAsString() << '/';
Ted Kremenekdcee3ce2010-07-01 20:16:50 +0000260
Ted Kremenekb7ddd9b2009-11-13 01:54:21 +0000261 switch (getKind()) {
Jordy Rose910c4052011-09-02 06:44:22 +0000262 default: llvm_unreachable("Invalid RefVal kind");
Ted Kremenekb7ddd9b2009-11-13 01:54:21 +0000263 case Owned: {
264 Out << "Owned";
265 unsigned cnt = getCount();
266 if (cnt) Out << " (+ " << cnt << ")";
267 break;
268 }
Ted Kremenekdcee3ce2010-07-01 20:16:50 +0000269
Ted Kremenekb7ddd9b2009-11-13 01:54:21 +0000270 case NotOwned: {
271 Out << "NotOwned";
272 unsigned cnt = getCount();
273 if (cnt) Out << " (+ " << cnt << ")";
274 break;
275 }
Ted Kremenekdcee3ce2010-07-01 20:16:50 +0000276
Ted Kremenekb7ddd9b2009-11-13 01:54:21 +0000277 case ReturnedOwned: {
278 Out << "ReturnedOwned";
279 unsigned cnt = getCount();
280 if (cnt) Out << " (+ " << cnt << ")";
281 break;
282 }
Ted Kremenekdcee3ce2010-07-01 20:16:50 +0000283
Ted Kremenekb7ddd9b2009-11-13 01:54:21 +0000284 case ReturnedNotOwned: {
285 Out << "ReturnedNotOwned";
286 unsigned cnt = getCount();
287 if (cnt) Out << " (+ " << cnt << ")";
288 break;
289 }
Ted Kremenekdcee3ce2010-07-01 20:16:50 +0000290
Ted Kremenekb7ddd9b2009-11-13 01:54:21 +0000291 case Released:
292 Out << "Released";
293 break;
Ted Kremenekdcee3ce2010-07-01 20:16:50 +0000294
Ted Kremenekb7ddd9b2009-11-13 01:54:21 +0000295 case ErrorDeallocGC:
296 Out << "-dealloc (GC)";
297 break;
Ted Kremenekdcee3ce2010-07-01 20:16:50 +0000298
Ted Kremenekb7ddd9b2009-11-13 01:54:21 +0000299 case ErrorDeallocNotOwned:
300 Out << "-dealloc (not-owned)";
301 break;
Ted Kremenekdcee3ce2010-07-01 20:16:50 +0000302
Ted Kremenekb7ddd9b2009-11-13 01:54:21 +0000303 case ErrorLeak:
304 Out << "Leaked";
305 break;
Ted Kremenekdcee3ce2010-07-01 20:16:50 +0000306
Ted Kremenekb7ddd9b2009-11-13 01:54:21 +0000307 case ErrorLeakReturned:
308 Out << "Leaked (Bad naming)";
309 break;
Ted Kremenekdcee3ce2010-07-01 20:16:50 +0000310
Ted Kremenekb7ddd9b2009-11-13 01:54:21 +0000311 case ErrorGCLeakReturned:
312 Out << "Leaked (GC-ed at return)";
313 break;
Ted Kremenekdcee3ce2010-07-01 20:16:50 +0000314
Ted Kremenekb7ddd9b2009-11-13 01:54:21 +0000315 case ErrorUseAfterRelease:
316 Out << "Use-After-Release [ERROR]";
317 break;
Ted Kremenekdcee3ce2010-07-01 20:16:50 +0000318
Ted Kremenekb7ddd9b2009-11-13 01:54:21 +0000319 case ErrorReleaseNotOwned:
320 Out << "Release of Not-Owned [ERROR]";
321 break;
Ted Kremenekdcee3ce2010-07-01 20:16:50 +0000322
Ted Kremenekb7ddd9b2009-11-13 01:54:21 +0000323 case RefVal::ErrorOverAutorelease:
324 Out << "Over autoreleased";
325 break;
Ted Kremenekdcee3ce2010-07-01 20:16:50 +0000326
Ted Kremenekb7ddd9b2009-11-13 01:54:21 +0000327 case RefVal::ErrorReturnedNotOwned:
328 Out << "Non-owned object returned instead of owned";
329 break;
330 }
Ted Kremenekdcee3ce2010-07-01 20:16:50 +0000331
Ted Kremenekb7ddd9b2009-11-13 01:54:21 +0000332 if (ACnt) {
333 Out << " [ARC +" << ACnt << ']';
334 }
335}
336} //end anonymous namespace
337
338//===----------------------------------------------------------------------===//
339// RefBindings - State used to track object reference counts.
340//===----------------------------------------------------------------------===//
341
342typedef llvm::ImmutableMap<SymbolRef, RefVal> RefBindings;
Ted Kremenekb7ddd9b2009-11-13 01:54:21 +0000343
344namespace clang {
Ted Kremenek9ef65372010-12-23 07:20:52 +0000345namespace ento {
Ted Kremenek18c66fd2011-08-15 22:09:50 +0000346template<>
347struct ProgramStateTrait<RefBindings>
348 : public ProgramStatePartialTrait<RefBindings> {
349 static void *GDMIndex() {
350 static int RefBIndex = 0;
351 return &RefBIndex;
352 }
353};
Ted Kremenekb7ddd9b2009-11-13 01:54:21 +0000354}
Argyrios Kyrtzidis5a4f98f2010-12-22 18:53:20 +0000355}
Ted Kremenekb7ddd9b2009-11-13 01:54:21 +0000356
357//===----------------------------------------------------------------------===//
Jordy Rose910c4052011-09-02 06:44:22 +0000358// Function/Method behavior summaries.
Ted Kremenekb7ddd9b2009-11-13 01:54:21 +0000359//===----------------------------------------------------------------------===//
360
361namespace {
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +0000362class RetainSummary {
Ted Kremenek1bffd742008-05-06 15:44:25 +0000363 /// Args - an ordered vector of (index, ArgEffect) pairs, where index
364 /// specifies the argument (starting from 0). This can be sparsely
365 /// populated; arguments with no entry in Args use 'DefaultArgEffect'.
Ted Kremenekb77449c2009-05-03 05:20:50 +0000366 ArgEffects Args;
Mike Stump1eb44332009-09-09 15:08:12 +0000367
Ted Kremenek1bffd742008-05-06 15:44:25 +0000368 /// DefaultArgEffect - The default ArgEffect to apply to arguments that
369 /// do not have an entry in Args.
370 ArgEffect DefaultArgEffect;
Mike Stump1eb44332009-09-09 15:08:12 +0000371
Ted Kremenek553cf182008-06-25 21:21:56 +0000372 /// Receiver - If this summary applies to an Objective-C message expression,
373 /// this is the effect applied to the state of the receiver.
Ted Kremenek3c0cea32008-05-06 02:26:56 +0000374 ArgEffect Receiver;
Mike Stump1eb44332009-09-09 15:08:12 +0000375
Ted Kremenek553cf182008-06-25 21:21:56 +0000376 /// Ret - The effect on the return value. Used to indicate if the
Jordy Rose76c506f2011-08-21 21:58:18 +0000377 /// function/method call returns a new tracked symbol.
Ted Kremenek6b3a0f72008-03-11 06:39:11 +0000378 RetEffect Ret;
Mike Stump1eb44332009-09-09 15:08:12 +0000379
Ted Kremenek6b3a0f72008-03-11 06:39:11 +0000380public:
Ted Kremenekb77449c2009-05-03 05:20:50 +0000381 RetainSummary(ArgEffects A, RetEffect R, ArgEffect defaultEff,
Jordy Rosee62e87b2011-08-20 20:55:40 +0000382 ArgEffect ReceiverEff)
383 : Args(A), DefaultArgEffect(defaultEff), Receiver(ReceiverEff), Ret(R) {}
Mike Stump1eb44332009-09-09 15:08:12 +0000384
Ted Kremenek553cf182008-06-25 21:21:56 +0000385 /// getArg - Return the argument effect on the argument specified by
386 /// idx (starting from 0).
Ted Kremenek1ac08d62008-03-11 17:48:22 +0000387 ArgEffect getArg(unsigned idx) const {
Ted Kremenekb77449c2009-05-03 05:20:50 +0000388 if (const ArgEffect *AE = Args.lookup(idx))
389 return *AE;
Mike Stump1eb44332009-09-09 15:08:12 +0000390
Ted Kremenek1bffd742008-05-06 15:44:25 +0000391 return DefaultArgEffect;
Ted Kremenek1ac08d62008-03-11 17:48:22 +0000392 }
Ted Kremenek11fe1752011-01-27 18:43:03 +0000393
394 void addArg(ArgEffects::Factory &af, unsigned idx, ArgEffect e) {
395 Args = af.add(Args, idx, e);
396 }
Mike Stump1eb44332009-09-09 15:08:12 +0000397
Ted Kremenek885c27b2009-05-04 05:31:22 +0000398 /// setDefaultArgEffect - Set the default argument effect.
399 void setDefaultArgEffect(ArgEffect E) {
400 DefaultArgEffect = E;
401 }
Mike Stump1eb44332009-09-09 15:08:12 +0000402
Ted Kremenek553cf182008-06-25 21:21:56 +0000403 /// getRetEffect - Returns the effect on the return value of the call.
Ted Kremenekb77449c2009-05-03 05:20:50 +0000404 RetEffect getRetEffect() const { return Ret; }
Mike Stump1eb44332009-09-09 15:08:12 +0000405
Ted Kremenek885c27b2009-05-04 05:31:22 +0000406 /// setRetEffect - Set the effect of the return value of the call.
407 void setRetEffect(RetEffect E) { Ret = E; }
Mike Stump1eb44332009-09-09 15:08:12 +0000408
Ted Kremenek12b94342011-01-27 06:54:14 +0000409
410 /// Sets the effect on the receiver of the message.
411 void setReceiverEffect(ArgEffect e) { Receiver = e; }
412
Ted Kremenek553cf182008-06-25 21:21:56 +0000413 /// getReceiverEffect - Returns the effect on the receiver of the call.
414 /// This is only meaningful if the summary applies to an ObjCMessageExpr*.
Ted Kremenekb77449c2009-05-03 05:20:50 +0000415 ArgEffect getReceiverEffect() const { return Receiver; }
Jordy Rose4df54fe2011-08-23 04:27:15 +0000416
417 /// Test if two retain summaries are identical. Note that merely equivalent
418 /// summaries are not necessarily identical (for example, if an explicit
419 /// argument effect matches the default effect).
420 bool operator==(const RetainSummary &Other) const {
421 return Args == Other.Args && DefaultArgEffect == Other.DefaultArgEffect &&
422 Receiver == Other.Receiver && Ret == Other.Ret;
423 }
Ted Kremenek6b3a0f72008-03-11 06:39:11 +0000424};
Ted Kremenek4f22a782008-06-23 23:30:29 +0000425} // end anonymous namespace
Ted Kremenek6b3a0f72008-03-11 06:39:11 +0000426
Ted Kremenek553cf182008-06-25 21:21:56 +0000427//===----------------------------------------------------------------------===//
428// Data structures for constructing summaries.
429//===----------------------------------------------------------------------===//
Ted Kremenek53301ba2008-06-24 03:49:48 +0000430
Ted Kremenek553cf182008-06-25 21:21:56 +0000431namespace {
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +0000432class ObjCSummaryKey {
Ted Kremenek553cf182008-06-25 21:21:56 +0000433 IdentifierInfo* II;
434 Selector S;
Mike Stump1eb44332009-09-09 15:08:12 +0000435public:
Ted Kremenek553cf182008-06-25 21:21:56 +0000436 ObjCSummaryKey(IdentifierInfo* ii, Selector s)
437 : II(ii), S(s) {}
438
Ted Kremenek9c378f72011-08-12 23:37:29 +0000439 ObjCSummaryKey(const ObjCInterfaceDecl *d, Selector s)
Ted Kremenek553cf182008-06-25 21:21:56 +0000440 : II(d ? d->getIdentifier() : 0), S(s) {}
Ted Kremenek70b6a832009-05-13 18:16:01 +0000441
Ted Kremenek9c378f72011-08-12 23:37:29 +0000442 ObjCSummaryKey(const ObjCInterfaceDecl *d, IdentifierInfo *ii, Selector s)
Ted Kremenek70b6a832009-05-13 18:16:01 +0000443 : II(d ? d->getIdentifier() : ii), S(s) {}
Mike Stump1eb44332009-09-09 15:08:12 +0000444
Ted Kremenek553cf182008-06-25 21:21:56 +0000445 ObjCSummaryKey(Selector s)
446 : II(0), S(s) {}
Mike Stump1eb44332009-09-09 15:08:12 +0000447
Ted Kremenek553cf182008-06-25 21:21:56 +0000448 IdentifierInfo* getIdentifier() const { return II; }
449 Selector getSelector() const { return S; }
450};
Ted Kremenek4f22a782008-06-23 23:30:29 +0000451}
452
453namespace llvm {
Ted Kremenek553cf182008-06-25 21:21:56 +0000454template <> struct DenseMapInfo<ObjCSummaryKey> {
455 static inline ObjCSummaryKey getEmptyKey() {
456 return ObjCSummaryKey(DenseMapInfo<IdentifierInfo*>::getEmptyKey(),
457 DenseMapInfo<Selector>::getEmptyKey());
458 }
Mike Stump1eb44332009-09-09 15:08:12 +0000459
Ted Kremenek553cf182008-06-25 21:21:56 +0000460 static inline ObjCSummaryKey getTombstoneKey() {
461 return ObjCSummaryKey(DenseMapInfo<IdentifierInfo*>::getTombstoneKey(),
Mike Stump1eb44332009-09-09 15:08:12 +0000462 DenseMapInfo<Selector>::getTombstoneKey());
Ted Kremenek553cf182008-06-25 21:21:56 +0000463 }
Mike Stump1eb44332009-09-09 15:08:12 +0000464
Ted Kremenek553cf182008-06-25 21:21:56 +0000465 static unsigned getHashValue(const ObjCSummaryKey &V) {
466 return (DenseMapInfo<IdentifierInfo*>::getHashValue(V.getIdentifier())
Mike Stump1eb44332009-09-09 15:08:12 +0000467 & 0x88888888)
Ted Kremenek553cf182008-06-25 21:21:56 +0000468 | (DenseMapInfo<Selector>::getHashValue(V.getSelector())
469 & 0x55555555);
470 }
Mike Stump1eb44332009-09-09 15:08:12 +0000471
Ted Kremenek553cf182008-06-25 21:21:56 +0000472 static bool isEqual(const ObjCSummaryKey& LHS, const ObjCSummaryKey& RHS) {
473 return DenseMapInfo<IdentifierInfo*>::isEqual(LHS.getIdentifier(),
474 RHS.getIdentifier()) &&
475 DenseMapInfo<Selector>::isEqual(LHS.getSelector(),
476 RHS.getSelector());
477 }
Mike Stump1eb44332009-09-09 15:08:12 +0000478
Ted Kremenek553cf182008-06-25 21:21:56 +0000479};
Chris Lattner06159e82009-12-15 07:26:51 +0000480template <>
481struct isPodLike<ObjCSummaryKey> { static const bool value = true; };
Ted Kremenek4f22a782008-06-23 23:30:29 +0000482} // end llvm namespace
Mike Stump1eb44332009-09-09 15:08:12 +0000483
Ted Kremenek4f22a782008-06-23 23:30:29 +0000484namespace {
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +0000485class ObjCSummaryCache {
Ted Kremenek553cf182008-06-25 21:21:56 +0000486 typedef llvm::DenseMap<ObjCSummaryKey, RetainSummary*> MapTy;
487 MapTy M;
488public:
489 ObjCSummaryCache() {}
Mike Stump1eb44332009-09-09 15:08:12 +0000490
Ted Kremenek9c378f72011-08-12 23:37:29 +0000491 RetainSummary* find(const ObjCInterfaceDecl *D, IdentifierInfo *ClsName,
Ted Kremeneka8833552009-04-29 23:03:22 +0000492 Selector S) {
Ted Kremenek8711c032009-04-29 05:04:30 +0000493 // Lookup the method using the decl for the class @interface. If we
494 // have no decl, lookup using the class name.
495 return D ? find(D, S) : find(ClsName, S);
496 }
Mike Stump1eb44332009-09-09 15:08:12 +0000497
Ted Kremenek9c378f72011-08-12 23:37:29 +0000498 RetainSummary* find(const ObjCInterfaceDecl *D, Selector S) {
Ted Kremenek553cf182008-06-25 21:21:56 +0000499 // Do a lookup with the (D,S) pair. If we find a match return
500 // the iterator.
501 ObjCSummaryKey K(D, S);
502 MapTy::iterator I = M.find(K);
Mike Stump1eb44332009-09-09 15:08:12 +0000503
Ted Kremenek553cf182008-06-25 21:21:56 +0000504 if (I != M.end() || !D)
Ted Kremenek614cc542009-07-21 23:27:57 +0000505 return I->second;
Mike Stump1eb44332009-09-09 15:08:12 +0000506
Ted Kremenek553cf182008-06-25 21:21:56 +0000507 // Walk the super chain. If we find a hit with a parent, we'll end
508 // up returning that summary. We actually allow that key (null,S), as
509 // we cache summaries for the null ObjCInterfaceDecl* to allow us to
510 // generate initial summaries without having to worry about NSObject
511 // being declared.
512 // FIXME: We may change this at some point.
Ted Kremenek9c378f72011-08-12 23:37:29 +0000513 for (ObjCInterfaceDecl *C=D->getSuperClass() ;; C=C->getSuperClass()) {
Ted Kremenek553cf182008-06-25 21:21:56 +0000514 if ((I = M.find(ObjCSummaryKey(C, S))) != M.end())
515 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000516
Ted Kremenek553cf182008-06-25 21:21:56 +0000517 if (!C)
Ted Kremenek614cc542009-07-21 23:27:57 +0000518 return NULL;
Ted Kremenek553cf182008-06-25 21:21:56 +0000519 }
Mike Stump1eb44332009-09-09 15:08:12 +0000520
521 // Cache the summary with original key to make the next lookup faster
Ted Kremenek553cf182008-06-25 21:21:56 +0000522 // and return the iterator.
Ted Kremenek614cc542009-07-21 23:27:57 +0000523 RetainSummary *Summ = I->second;
524 M[K] = Summ;
525 return Summ;
Ted Kremenek553cf182008-06-25 21:21:56 +0000526 }
Mike Stump1eb44332009-09-09 15:08:12 +0000527
Ted Kremenek614cc542009-07-21 23:27:57 +0000528 RetainSummary* find(IdentifierInfo* II, Selector S) {
Ted Kremenek553cf182008-06-25 21:21:56 +0000529 // FIXME: Class method lookup. Right now we dont' have a good way
530 // of going between IdentifierInfo* and the class hierarchy.
Ted Kremenek614cc542009-07-21 23:27:57 +0000531 MapTy::iterator I = M.find(ObjCSummaryKey(II, S));
Mike Stump1eb44332009-09-09 15:08:12 +0000532
Ted Kremenek614cc542009-07-21 23:27:57 +0000533 if (I == M.end())
534 I = M.find(ObjCSummaryKey(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000535
Ted Kremenek614cc542009-07-21 23:27:57 +0000536 return I == M.end() ? NULL : I->second;
Ted Kremenek553cf182008-06-25 21:21:56 +0000537 }
Mike Stump1eb44332009-09-09 15:08:12 +0000538
Ted Kremenek553cf182008-06-25 21:21:56 +0000539 RetainSummary*& operator[](ObjCSummaryKey K) {
540 return M[K];
541 }
Mike Stump1eb44332009-09-09 15:08:12 +0000542
Ted Kremenek553cf182008-06-25 21:21:56 +0000543 RetainSummary*& operator[](Selector S) {
544 return M[ ObjCSummaryKey(S) ];
545 }
Mike Stump1eb44332009-09-09 15:08:12 +0000546};
Ted Kremenek553cf182008-06-25 21:21:56 +0000547} // end anonymous namespace
548
549//===----------------------------------------------------------------------===//
550// Data structures for managing collections of summaries.
551//===----------------------------------------------------------------------===//
552
553namespace {
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +0000554class RetainSummaryManager {
Ted Kremenekd3dbcf42008-05-05 22:11:16 +0000555
556 //==-----------------------------------------------------------------==//
557 // Typedefs.
558 //==-----------------------------------------------------------------==//
Mike Stump1eb44332009-09-09 15:08:12 +0000559
Zhongxing Xubc5495b2010-07-20 02:56:49 +0000560 typedef llvm::DenseMap<const FunctionDecl*, RetainSummary*>
Ted Kremenekd3dbcf42008-05-05 22:11:16 +0000561 FuncSummariesTy;
Mike Stump1eb44332009-09-09 15:08:12 +0000562
Ted Kremenek4f22a782008-06-23 23:30:29 +0000563 typedef ObjCSummaryCache ObjCMethodSummariesTy;
Mike Stump1eb44332009-09-09 15:08:12 +0000564
Ted Kremenekd3dbcf42008-05-05 22:11:16 +0000565 //==-----------------------------------------------------------------==//
566 // Data.
567 //==-----------------------------------------------------------------==//
Mike Stump1eb44332009-09-09 15:08:12 +0000568
Ted Kremenek553cf182008-06-25 21:21:56 +0000569 /// Ctx - The ASTContext object for the analyzed ASTs.
Ted Kremenek9c378f72011-08-12 23:37:29 +0000570 ASTContext &Ctx;
Ted Kremenek179064e2008-07-01 17:21:27 +0000571
Ted Kremenek553cf182008-06-25 21:21:56 +0000572 /// GCEnabled - Records whether or not the analyzed code runs in GC mode.
Ted Kremenek377e2302008-04-29 05:33:51 +0000573 const bool GCEnabled;
Mike Stump1eb44332009-09-09 15:08:12 +0000574
John McCallf85e1932011-06-15 23:02:42 +0000575 /// Records whether or not the analyzed code runs in ARC mode.
576 const bool ARCEnabled;
577
Ted Kremenek553cf182008-06-25 21:21:56 +0000578 /// FuncSummaries - A map from FunctionDecls to summaries.
Mike Stump1eb44332009-09-09 15:08:12 +0000579 FuncSummariesTy FuncSummaries;
580
Ted Kremenek553cf182008-06-25 21:21:56 +0000581 /// ObjCClassMethodSummaries - A map from selectors (for instance methods)
582 /// to summaries.
Ted Kremenek1f180c32008-06-23 22:21:20 +0000583 ObjCMethodSummariesTy ObjCClassMethodSummaries;
Ted Kremenekd3dbcf42008-05-05 22:11:16 +0000584
Ted Kremenek553cf182008-06-25 21:21:56 +0000585 /// ObjCMethodSummaries - A map from selectors to summaries.
Ted Kremenek1f180c32008-06-23 22:21:20 +0000586 ObjCMethodSummariesTy ObjCMethodSummaries;
Ted Kremenekd3dbcf42008-05-05 22:11:16 +0000587
Ted Kremenek553cf182008-06-25 21:21:56 +0000588 /// BPAlloc - A BumpPtrAllocator used for allocating summaries, ArgEffects,
589 /// and all other data used by the checker.
Ted Kremenekd3dbcf42008-05-05 22:11:16 +0000590 llvm::BumpPtrAllocator BPAlloc;
Mike Stump1eb44332009-09-09 15:08:12 +0000591
Ted Kremenekb77449c2009-05-03 05:20:50 +0000592 /// AF - A factory for ArgEffects objects.
Mike Stump1eb44332009-09-09 15:08:12 +0000593 ArgEffects::Factory AF;
594
Ted Kremenek553cf182008-06-25 21:21:56 +0000595 /// ScratchArgs - A holding buffer for construct ArgEffects.
Ted Kremenekd3dbcf42008-05-05 22:11:16 +0000596 ArgEffects ScratchArgs;
Mike Stump1eb44332009-09-09 15:08:12 +0000597
Ted Kremenekec315332009-05-07 23:40:42 +0000598 /// ObjCAllocRetE - Default return effect for methods returning Objective-C
599 /// objects.
600 RetEffect ObjCAllocRetE;
Ted Kremenek547d4952009-06-05 23:18:01 +0000601
Mike Stump1eb44332009-09-09 15:08:12 +0000602 /// ObjCInitRetE - Default return effect for init methods returning
Ted Kremenekac02f202009-08-20 05:13:36 +0000603 /// Objective-C objects.
Ted Kremenek547d4952009-06-05 23:18:01 +0000604 RetEffect ObjCInitRetE;
Mike Stump1eb44332009-09-09 15:08:12 +0000605
Ted Kremenek7faca822009-05-04 04:57:00 +0000606 RetainSummary DefaultSummary;
Ted Kremenek432af592008-05-06 18:11:36 +0000607 RetainSummary* StopSummary;
Mike Stump1eb44332009-09-09 15:08:12 +0000608
Ted Kremenekd3dbcf42008-05-05 22:11:16 +0000609 //==-----------------------------------------------------------------==//
610 // Methods.
611 //==-----------------------------------------------------------------==//
Mike Stump1eb44332009-09-09 15:08:12 +0000612
Ted Kremenek553cf182008-06-25 21:21:56 +0000613 /// getArgEffects - Returns a persistent ArgEffects object based on the
614 /// data in ScratchArgs.
Ted Kremenekb77449c2009-05-03 05:20:50 +0000615 ArgEffects getArgEffects();
Ted Kremenek6b3a0f72008-03-11 06:39:11 +0000616
Mike Stump1eb44332009-09-09 15:08:12 +0000617 enum UnaryFuncKind { cfretain, cfrelease, cfmakecollectable };
618
Ted Kremenek896cd9d2008-10-23 01:56:15 +0000619public:
Ted Kremenek78a35a32009-05-12 20:06:54 +0000620 RetEffect getObjAllocRetEffect() const { return ObjCAllocRetE; }
621
Ted Kremenek6ad315a2009-02-23 16:51:39 +0000622 RetainSummary* getUnarySummary(const FunctionType* FT, UnaryFuncKind func);
Mike Stump1eb44332009-09-09 15:08:12 +0000623
Ted Kremenek9c378f72011-08-12 23:37:29 +0000624 RetainSummary* getCFSummaryCreateRule(const FunctionDecl *FD);
625 RetainSummary* getCFSummaryGetRule(const FunctionDecl *FD);
626 RetainSummary* getCFCreateGetRuleSummary(const FunctionDecl *FD,
Zhongxing Xubc5495b2010-07-20 02:56:49 +0000627 StringRef FName);
Mike Stump1eb44332009-09-09 15:08:12 +0000628
Ted Kremenekb77449c2009-05-03 05:20:50 +0000629 RetainSummary* getPersistentSummary(ArgEffects AE, RetEffect RetEff,
Ted Kremenek1bffd742008-05-06 15:44:25 +0000630 ArgEffect ReceiverEff = DoNothing,
Jordy Rosee62e87b2011-08-20 20:55:40 +0000631 ArgEffect DefaultEff = MayEscape);
Ted Kremenek706522f2008-10-29 04:07:07 +0000632
Ted Kremenek3c0cea32008-05-06 02:26:56 +0000633 RetainSummary* getPersistentSummary(RetEffect RE,
Ted Kremenek1bffd742008-05-06 15:44:25 +0000634 ArgEffect ReceiverEff = DoNothing,
Ted Kremenek3eabf1c2008-05-22 17:31:13 +0000635 ArgEffect DefaultEff = MayEscape) {
Ted Kremenek1bffd742008-05-06 15:44:25 +0000636 return getPersistentSummary(getArgEffects(), RE, ReceiverEff, DefaultEff);
Ted Kremenek9c32d082008-05-06 00:30:21 +0000637 }
Mike Stump1eb44332009-09-09 15:08:12 +0000638
Ted Kremenek8711c032009-04-29 05:04:30 +0000639 RetainSummary *getPersistentStopSummary() {
Ted Kremenek432af592008-05-06 18:11:36 +0000640 if (StopSummary)
641 return StopSummary;
Mike Stump1eb44332009-09-09 15:08:12 +0000642
Ted Kremenek432af592008-05-06 18:11:36 +0000643 StopSummary = getPersistentSummary(RetEffect::MakeNoRet(),
644 StopTracking, StopTracking);
Ted Kremenek706522f2008-10-29 04:07:07 +0000645
Ted Kremenek432af592008-05-06 18:11:36 +0000646 return StopSummary;
Mike Stump1eb44332009-09-09 15:08:12 +0000647 }
Ted Kremenekb3095252008-05-06 04:20:12 +0000648
Ted Kremenek8711c032009-04-29 05:04:30 +0000649 RetainSummary *getInitMethodSummary(QualType RetTy);
Ted Kremenek46e49ee2008-05-05 23:55:01 +0000650
Ted Kremenek1f180c32008-06-23 22:21:20 +0000651 void InitializeClassMethodSummaries();
652 void InitializeMethodSummaries();
Ted Kremenek896cd9d2008-10-23 01:56:15 +0000653private:
Ted Kremenek553cf182008-06-25 21:21:56 +0000654 void addNSObjectClsMethSummary(Selector S, RetainSummary *Summ) {
655 ObjCClassMethodSummaries[S] = Summ;
656 }
Mike Stump1eb44332009-09-09 15:08:12 +0000657
Ted Kremenek553cf182008-06-25 21:21:56 +0000658 void addNSObjectMethSummary(Selector S, RetainSummary *Summ) {
659 ObjCMethodSummaries[S] = Summ;
660 }
Ted Kremenek3aa7ecd2009-03-04 23:30:42 +0000661
662 void addClassMethSummary(const char* Cls, const char* nullaryName,
663 RetainSummary *Summ) {
664 IdentifierInfo* ClsII = &Ctx.Idents.get(Cls);
665 Selector S = GetNullarySelector(nullaryName, Ctx);
666 ObjCClassMethodSummaries[ObjCSummaryKey(ClsII, S)] = Summ;
667 }
Mike Stump1eb44332009-09-09 15:08:12 +0000668
Ted Kremenek6c4becb2009-02-25 02:54:57 +0000669 void addInstMethSummary(const char* Cls, const char* nullaryName,
670 RetainSummary *Summ) {
671 IdentifierInfo* ClsII = &Ctx.Idents.get(Cls);
672 Selector S = GetNullarySelector(nullaryName, Ctx);
673 ObjCMethodSummaries[ObjCSummaryKey(ClsII, S)] = Summ;
674 }
Mike Stump1eb44332009-09-09 15:08:12 +0000675
Ted Kremenekde4d5332009-04-24 17:50:11 +0000676 Selector generateSelector(va_list argp) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000677 SmallVector<IdentifierInfo*, 10> II;
Ted Kremenekde4d5332009-04-24 17:50:11 +0000678
Ted Kremenek9e476de2008-08-12 18:30:56 +0000679 while (const char* s = va_arg(argp, const char*))
680 II.push_back(&Ctx.Idents.get(s));
Ted Kremenekde4d5332009-04-24 17:50:11 +0000681
Mike Stump1eb44332009-09-09 15:08:12 +0000682 return Ctx.Selectors.getSelector(II.size(), &II[0]);
Ted Kremenekde4d5332009-04-24 17:50:11 +0000683 }
Mike Stump1eb44332009-09-09 15:08:12 +0000684
Ted Kremenekde4d5332009-04-24 17:50:11 +0000685 void addMethodSummary(IdentifierInfo *ClsII, ObjCMethodSummariesTy& Summaries,
686 RetainSummary* Summ, va_list argp) {
687 Selector S = generateSelector(argp);
688 Summaries[ObjCSummaryKey(ClsII, S)] = Summ;
Ted Kremenek70a733e2008-07-18 17:24:20 +0000689 }
Mike Stump1eb44332009-09-09 15:08:12 +0000690
Ted Kremenekaf9dc272008-08-12 18:48:50 +0000691 void addInstMethSummary(const char* Cls, RetainSummary* Summ, ...) {
692 va_list argp;
693 va_start(argp, Summ);
Ted Kremenekde4d5332009-04-24 17:50:11 +0000694 addMethodSummary(&Ctx.Idents.get(Cls), ObjCMethodSummaries, Summ, argp);
Mike Stump1eb44332009-09-09 15:08:12 +0000695 va_end(argp);
Ted Kremenekaf9dc272008-08-12 18:48:50 +0000696 }
Mike Stump1eb44332009-09-09 15:08:12 +0000697
Ted Kremenekde4d5332009-04-24 17:50:11 +0000698 void addClsMethSummary(const char* Cls, RetainSummary* Summ, ...) {
699 va_list argp;
700 va_start(argp, Summ);
701 addMethodSummary(&Ctx.Idents.get(Cls),ObjCClassMethodSummaries, Summ, argp);
702 va_end(argp);
703 }
Mike Stump1eb44332009-09-09 15:08:12 +0000704
Ted Kremenekde4d5332009-04-24 17:50:11 +0000705 void addClsMethSummary(IdentifierInfo *II, RetainSummary* Summ, ...) {
706 va_list argp;
707 va_start(argp, Summ);
708 addMethodSummary(II, ObjCClassMethodSummaries, Summ, argp);
709 va_end(argp);
710 }
711
Ted Kremenek6b3a0f72008-03-11 06:39:11 +0000712public:
Mike Stump1eb44332009-09-09 15:08:12 +0000713
Ted Kremenek9c378f72011-08-12 23:37:29 +0000714 RetainSummaryManager(ASTContext &ctx, bool gcenabled, bool usesARC)
Ted Kremenek179064e2008-07-01 17:21:27 +0000715 : Ctx(ctx),
John McCallf85e1932011-06-15 23:02:42 +0000716 GCEnabled(gcenabled),
717 ARCEnabled(usesARC),
718 AF(BPAlloc), ScratchArgs(AF.getEmptyMap()),
719 ObjCAllocRetE(gcenabled
720 ? RetEffect::MakeGCNotOwned()
721 : (usesARC ? RetEffect::MakeARCNotOwned()
722 : RetEffect::MakeOwned(RetEffect::ObjC, true))),
723 ObjCInitRetE(gcenabled
724 ? RetEffect::MakeGCNotOwned()
725 : (usesARC ? RetEffect::MakeARCNotOwned()
726 : RetEffect::MakeOwnedWhenTrackedReceiver())),
Ted Kremenek3baf6722010-11-24 00:54:37 +0000727 DefaultSummary(AF.getEmptyMap() /* per-argument effects (none) */,
Ted Kremenek7faca822009-05-04 04:57:00 +0000728 RetEffect::MakeNoRet() /* return effect */,
Ted Kremenekebd5a2d2009-05-11 18:30:24 +0000729 MayEscape, /* default argument effect */
730 DoNothing /* receiver effect */),
Ted Kremenekb77449c2009-05-03 05:20:50 +0000731 StopSummary(0) {
Ted Kremenek553cf182008-06-25 21:21:56 +0000732
733 InitializeClassMethodSummaries();
734 InitializeMethodSummaries();
735 }
Mike Stump1eb44332009-09-09 15:08:12 +0000736
Ted Kremenek9c378f72011-08-12 23:37:29 +0000737 RetainSummary* getSummary(const FunctionDecl *FD);
Mike Stump1eb44332009-09-09 15:08:12 +0000738
Argyrios Kyrtzidis432424d2011-01-25 00:03:53 +0000739 RetainSummary *getInstanceMethodSummary(const ObjCMessage &msg,
Ted Kremenek18c66fd2011-08-15 22:09:50 +0000740 const ProgramState *state,
Ted Kremenekb7ddd9b2009-11-13 01:54:21 +0000741 const LocationContext *LC);
Ted Kremenekdcee3ce2010-07-01 20:16:50 +0000742
Argyrios Kyrtzidis432424d2011-01-25 00:03:53 +0000743 RetainSummary* getInstanceMethodSummary(const ObjCMessage &msg,
Ted Kremenek9c378f72011-08-12 23:37:29 +0000744 const ObjCInterfaceDecl *ID) {
Argyrios Kyrtzidis432424d2011-01-25 00:03:53 +0000745 return getInstanceMethodSummary(msg.getSelector(), 0,
746 ID, msg.getMethodDecl(), msg.getType(Ctx));
Ted Kremenek8711c032009-04-29 05:04:30 +0000747 }
Mike Stump1eb44332009-09-09 15:08:12 +0000748
Ted Kremenekce8a41d2009-04-29 17:09:14 +0000749 RetainSummary* getInstanceMethodSummary(Selector S, IdentifierInfo *ClsName,
Ted Kremenek9c378f72011-08-12 23:37:29 +0000750 const ObjCInterfaceDecl *ID,
Ted Kremeneka8833552009-04-29 23:03:22 +0000751 const ObjCMethodDecl *MD,
752 QualType RetTy);
Ted Kremenekfcd7c6f2009-04-29 00:42:39 +0000753
754 RetainSummary *getClassMethodSummary(Selector S, IdentifierInfo *ClsName,
Ted Kremeneka8833552009-04-29 23:03:22 +0000755 const ObjCInterfaceDecl *ID,
756 const ObjCMethodDecl *MD,
757 QualType RetTy);
Mike Stump1eb44332009-09-09 15:08:12 +0000758
Argyrios Kyrtzidis432424d2011-01-25 00:03:53 +0000759 RetainSummary *getClassMethodSummary(const ObjCMessage &msg) {
760 const ObjCInterfaceDecl *Class = 0;
761 if (!msg.isInstanceMessage())
762 Class = msg.getReceiverInterface();
Douglas Gregor04badcf2010-04-21 00:45:42 +0000763
Argyrios Kyrtzidis432424d2011-01-25 00:03:53 +0000764 return getClassMethodSummary(msg.getSelector(),
Douglas Gregor04badcf2010-04-21 00:45:42 +0000765 Class? Class->getIdentifier() : 0,
766 Class,
Argyrios Kyrtzidis432424d2011-01-25 00:03:53 +0000767 msg.getMethodDecl(), msg.getType(Ctx));
Ted Kremenekfcd7c6f2009-04-29 00:42:39 +0000768 }
Ted Kremenek552333c2009-04-29 17:17:48 +0000769
770 /// getMethodSummary - This version of getMethodSummary is used to query
771 /// the summary for the current method being analyzed.
Ted Kremeneka8833552009-04-29 23:03:22 +0000772 RetainSummary *getMethodSummary(const ObjCMethodDecl *MD) {
773 // FIXME: Eventually this should be unneeded.
Ted Kremeneka8833552009-04-29 23:03:22 +0000774 const ObjCInterfaceDecl *ID = MD->getClassInterface();
Ted Kremenek70a65762009-04-30 05:41:14 +0000775 Selector S = MD->getSelector();
Ted Kremenek552333c2009-04-29 17:17:48 +0000776 IdentifierInfo *ClsName = ID->getIdentifier();
777 QualType ResultTy = MD->getResultType();
Mike Stump1eb44332009-09-09 15:08:12 +0000778
Ted Kremenek552333c2009-04-29 17:17:48 +0000779 if (MD->isInstanceMethod())
780 return getInstanceMethodSummary(S, ClsName, ID, MD, ResultTy);
781 else
782 return getClassMethodSummary(S, ClsName, ID, MD, ResultTy);
783 }
Mike Stump1eb44332009-09-09 15:08:12 +0000784
Ted Kremenek9c378f72011-08-12 23:37:29 +0000785 RetainSummary* getCommonMethodSummary(const ObjCMethodDecl *MD,
Ted Kremeneka8833552009-04-29 23:03:22 +0000786 Selector S, QualType RetTy);
787
Jordy Rose4df54fe2011-08-23 04:27:15 +0000788 void updateSummaryFromAnnotations(RetainSummary *&Summ,
Ted Kremenek4dd8fb42009-05-09 02:58:13 +0000789 const ObjCMethodDecl *MD);
790
Jordy Rose4df54fe2011-08-23 04:27:15 +0000791 void updateSummaryFromAnnotations(RetainSummary *&Summ,
Ted Kremenek4dd8fb42009-05-09 02:58:13 +0000792 const FunctionDecl *FD);
793
Ted Kremenekd3dbcf42008-05-05 22:11:16 +0000794 bool isGCEnabled() const { return GCEnabled; }
Mike Stump1eb44332009-09-09 15:08:12 +0000795
John McCallf85e1932011-06-15 23:02:42 +0000796 bool isARCEnabled() const { return ARCEnabled; }
797
798 bool isARCorGCEnabled() const { return GCEnabled || ARCEnabled; }
799
Ted Kremenek885c27b2009-05-04 05:31:22 +0000800 RetainSummary *copySummary(RetainSummary *OldSumm) {
801 RetainSummary *Summ = (RetainSummary*) BPAlloc.Allocate<RetainSummary>();
802 new (Summ) RetainSummary(*OldSumm);
803 return Summ;
Mike Stump1eb44332009-09-09 15:08:12 +0000804 }
Ted Kremenek6b3a0f72008-03-11 06:39:11 +0000805};
Mike Stump1eb44332009-09-09 15:08:12 +0000806
Jordy Rose0fe62f82011-08-24 09:02:37 +0000807// Used to avoid allocating long-term (BPAlloc'd) memory for default retain
808// summaries. If a function or method looks like it has a default summary, but
809// it has annotations, the annotations are added to the stack-based template
810// and then copied into managed memory.
811class RetainSummaryTemplate {
812 RetainSummaryManager &Manager;
813 RetainSummary *&RealSummary;
814 RetainSummary ScratchSummary;
815 bool Accessed;
816public:
817 RetainSummaryTemplate(RetainSummary *&real, const RetainSummary &base,
818 RetainSummaryManager &manager)
819 : Manager(manager), RealSummary(real), ScratchSummary(base), Accessed(false)
820 {}
821
822 ~RetainSummaryTemplate() {
823 if (!RealSummary && Accessed)
824 RealSummary = Manager.copySummary(&ScratchSummary);
825 }
826
827 RetainSummary &operator*() {
828 Accessed = true;
829 return RealSummary ? *RealSummary : ScratchSummary;
830 }
831
832 RetainSummary *operator->() {
833 Accessed = true;
834 return RealSummary ? RealSummary : &ScratchSummary;
835 }
836};
837
Ted Kremenek6b3a0f72008-03-11 06:39:11 +0000838} // end anonymous namespace
839
840//===----------------------------------------------------------------------===//
841// Implementation of checker data structures.
842//===----------------------------------------------------------------------===//
843
Ted Kremenekb77449c2009-05-03 05:20:50 +0000844ArgEffects RetainSummaryManager::getArgEffects() {
845 ArgEffects AE = ScratchArgs;
Ted Kremenek3baf6722010-11-24 00:54:37 +0000846 ScratchArgs = AF.getEmptyMap();
Ted Kremenekb77449c2009-05-03 05:20:50 +0000847 return AE;
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000848}
849
Ted Kremenek3c0cea32008-05-06 02:26:56 +0000850RetainSummary*
Ted Kremenekb77449c2009-05-03 05:20:50 +0000851RetainSummaryManager::getPersistentSummary(ArgEffects AE, RetEffect RetEff,
Ted Kremenek1bffd742008-05-06 15:44:25 +0000852 ArgEffect ReceiverEff,
Jordy Rosee62e87b2011-08-20 20:55:40 +0000853 ArgEffect DefaultEff) {
Ted Kremenek891d5cc2008-04-24 17:22:33 +0000854 // Create the summary and return it.
Ted Kremenek22fe2482009-05-04 04:30:18 +0000855 RetainSummary *Summ = (RetainSummary*) BPAlloc.Allocate<RetainSummary>();
Jordy Rosee62e87b2011-08-20 20:55:40 +0000856 new (Summ) RetainSummary(AE, RetEff, DefaultEff, ReceiverEff);
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000857 return Summ;
858}
859
Ted Kremenekd3dbcf42008-05-05 22:11:16 +0000860//===----------------------------------------------------------------------===//
861// Summary creation for functions (largely uses of Core Foundation).
862//===----------------------------------------------------------------------===//
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000863
Ted Kremenek9c378f72011-08-12 23:37:29 +0000864static bool isRetain(const FunctionDecl *FD, StringRef FName) {
Benjamin Kramerb6f3c702010-02-08 18:38:55 +0000865 return FName.endswith("Retain");
Ted Kremenek12619382009-01-12 21:45:02 +0000866}
867
Ted Kremenek9c378f72011-08-12 23:37:29 +0000868static bool isRelease(const FunctionDecl *FD, StringRef FName) {
Benjamin Kramerb6f3c702010-02-08 18:38:55 +0000869 return FName.endswith("Release");
Ted Kremenek12619382009-01-12 21:45:02 +0000870}
871
Jordy Rose76c506f2011-08-21 21:58:18 +0000872static bool isMakeCollectable(const FunctionDecl *FD, StringRef FName) {
873 // FIXME: Remove FunctionDecl parameter.
874 // FIXME: Is it really okay if MakeCollectable isn't a suffix?
875 return FName.find("MakeCollectable") != StringRef::npos;
876}
877
Ted Kremenek9c378f72011-08-12 23:37:29 +0000878RetainSummary* RetainSummaryManager::getSummary(const FunctionDecl *FD) {
Ted Kremenek891d5cc2008-04-24 17:22:33 +0000879 // Look up a summary in our cache of FunctionDecls -> Summaries.
Ted Kremenekd3dbcf42008-05-05 22:11:16 +0000880 FuncSummariesTy::iterator I = FuncSummaries.find(FD);
Ted Kremenekd3dbcf42008-05-05 22:11:16 +0000881 if (I != FuncSummaries.end())
Ted Kremenek891d5cc2008-04-24 17:22:33 +0000882 return I->second;
883
Ted Kremeneke401a0c2009-05-04 15:34:07 +0000884 // No summary? Generate one.
Ted Kremenek12619382009-01-12 21:45:02 +0000885 RetainSummary *S = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000886
Ted Kremenek37d785b2008-07-15 16:50:12 +0000887 do {
Ted Kremenek12619382009-01-12 21:45:02 +0000888 // We generate "stop" summaries for implicitly defined functions.
889 if (FD->isImplicit()) {
890 S = getPersistentStopSummary();
891 break;
Ted Kremenek37d785b2008-07-15 16:50:12 +0000892 }
Ted Kremenek9ca28512011-05-02 21:21:42 +0000893 // For C++ methods, generate an implicit "stop" summary as well. We
894 // can relax this once we have a clear policy for C++ methods and
895 // ownership attributes.
896 if (isa<CXXMethodDecl>(FD)) {
897 S = getPersistentStopSummary();
898 break;
899 }
Mike Stump1eb44332009-09-09 15:08:12 +0000900
John McCall183700f2009-09-21 23:43:11 +0000901 // [PR 3337] Use 'getAs<FunctionType>' to strip away any typedefs on the
Ted Kremenek99890652009-01-16 18:40:33 +0000902 // function's type.
John McCall183700f2009-09-21 23:43:11 +0000903 const FunctionType* FT = FD->getType()->getAs<FunctionType>();
Ted Kremenek48c6d182009-12-16 06:06:43 +0000904 const IdentifierInfo *II = FD->getIdentifier();
905 if (!II)
906 break;
Benjamin Kramerb6f3c702010-02-08 18:38:55 +0000907
908 StringRef FName = II->getName();
Mike Stump1eb44332009-09-09 15:08:12 +0000909
Ted Kremenekbf0a4dd2009-03-05 22:11:14 +0000910 // Strip away preceding '_'. Doing this here will effect all the checks
911 // down below.
Benjamin Kramerb6f3c702010-02-08 18:38:55 +0000912 FName = FName.substr(FName.find_first_not_of('_'));
Mike Stump1eb44332009-09-09 15:08:12 +0000913
Ted Kremenek12619382009-01-12 21:45:02 +0000914 // Inspect the result type.
915 QualType RetTy = FT->getResultType();
Mike Stump1eb44332009-09-09 15:08:12 +0000916
Ted Kremenek12619382009-01-12 21:45:02 +0000917 // FIXME: This should all be refactored into a chain of "summary lookup"
918 // filters.
Ted Kremenek008636a2009-10-14 00:27:24 +0000919 assert(ScratchArgs.isEmpty());
Ted Kremenek39d88b02009-06-15 20:36:07 +0000920
Benjamin Kramerb6f3c702010-02-08 18:38:55 +0000921 if (FName == "pthread_create") {
922 // Part of: <rdar://problem/7299394>. This will be addressed
923 // better with IPA.
924 S = getPersistentStopSummary();
925 } else if (FName == "NSMakeCollectable") {
926 // Handle: id NSMakeCollectable(CFTypeRef)
927 S = (RetTy->isObjCIdType())
928 ? getUnarySummary(FT, cfmakecollectable)
929 : getPersistentStopSummary();
930 } else if (FName == "IOBSDNameMatching" ||
931 FName == "IOServiceMatching" ||
932 FName == "IOServiceNameMatching" ||
933 FName == "IORegistryEntryIDMatching" ||
934 FName == "IOOpenFirmwarePathMatching") {
935 // Part of <rdar://problem/6961230>. (IOKit)
936 // This should be addressed using a API table.
937 S = getPersistentSummary(RetEffect::MakeOwned(RetEffect::CF, true),
938 DoNothing, DoNothing);
939 } else if (FName == "IOServiceGetMatchingService" ||
940 FName == "IOServiceGetMatchingServices") {
941 // FIXES: <rdar://problem/6326900>
942 // This should be addressed using a API table. This strcmp is also
943 // a little gross, but there is no need to super optimize here.
Ted Kremenek3baf6722010-11-24 00:54:37 +0000944 ScratchArgs = AF.add(ScratchArgs, 1, DecRef);
Benjamin Kramerb6f3c702010-02-08 18:38:55 +0000945 S = getPersistentSummary(RetEffect::MakeNoRet(), DoNothing, DoNothing);
946 } else if (FName == "IOServiceAddNotification" ||
947 FName == "IOServiceAddMatchingNotification") {
948 // Part of <rdar://problem/6961230>. (IOKit)
949 // This should be addressed using a API table.
Ted Kremenek3baf6722010-11-24 00:54:37 +0000950 ScratchArgs = AF.add(ScratchArgs, 2, DecRef);
Benjamin Kramerb6f3c702010-02-08 18:38:55 +0000951 S = getPersistentSummary(RetEffect::MakeNoRet(), DoNothing, DoNothing);
952 } else if (FName == "CVPixelBufferCreateWithBytes") {
953 // FIXES: <rdar://problem/7283567>
954 // Eventually this can be improved by recognizing that the pixel
955 // buffer passed to CVPixelBufferCreateWithBytes is released via
956 // a callback and doing full IPA to make sure this is done correctly.
957 // FIXME: This function has an out parameter that returns an
958 // allocated object.
Ted Kremenek3baf6722010-11-24 00:54:37 +0000959 ScratchArgs = AF.add(ScratchArgs, 7, StopTracking);
Benjamin Kramerb6f3c702010-02-08 18:38:55 +0000960 S = getPersistentSummary(RetEffect::MakeNoRet(), DoNothing, DoNothing);
961 } else if (FName == "CGBitmapContextCreateWithData") {
962 // FIXES: <rdar://problem/7358899>
963 // Eventually this can be improved by recognizing that 'releaseInfo'
964 // passed to CGBitmapContextCreateWithData is released via
965 // a callback and doing full IPA to make sure this is done correctly.
Ted Kremenek3baf6722010-11-24 00:54:37 +0000966 ScratchArgs = AF.add(ScratchArgs, 8, StopTracking);
Benjamin Kramerb6f3c702010-02-08 18:38:55 +0000967 S = getPersistentSummary(RetEffect::MakeOwned(RetEffect::CF, true),
968 DoNothing, DoNothing);
969 } else if (FName == "CVPixelBufferCreateWithPlanarBytes") {
970 // FIXES: <rdar://problem/7283567>
971 // Eventually this can be improved by recognizing that the pixel
972 // buffer passed to CVPixelBufferCreateWithPlanarBytes is released
973 // via a callback and doing full IPA to make sure this is done
974 // correctly.
Ted Kremenek3baf6722010-11-24 00:54:37 +0000975 ScratchArgs = AF.add(ScratchArgs, 12, StopTracking);
Benjamin Kramerb6f3c702010-02-08 18:38:55 +0000976 S = getPersistentSummary(RetEffect::MakeNoRet(), DoNothing, DoNothing);
Ted Kremenekb04cb592009-06-11 18:17:24 +0000977 }
Mike Stump1eb44332009-09-09 15:08:12 +0000978
Ted Kremenekb04cb592009-06-11 18:17:24 +0000979 // Did we get a summary?
980 if (S)
981 break;
Ted Kremenek61991902009-03-17 22:43:44 +0000982
983 // Enable this code once the semantics of NSDeallocateObject are resolved
984 // for GC. <rdar://problem/6619988>
985#if 0
986 // Handle: NSDeallocateObject(id anObject);
987 // This method does allow 'nil' (although we don't check it now).
Mike Stump1eb44332009-09-09 15:08:12 +0000988 if (strcmp(FName, "NSDeallocateObject") == 0) {
Ted Kremenek61991902009-03-17 22:43:44 +0000989 return RetTy == Ctx.VoidTy
990 ? getPersistentSummary(RetEffect::MakeNoRet(), DoNothing, Dealloc)
991 : getPersistentStopSummary();
992 }
993#endif
Ted Kremenek12619382009-01-12 21:45:02 +0000994
995 if (RetTy->isPointerType()) {
996 // For CoreFoundation ('CF') types.
Ted Kremenek78acdbf2010-01-27 18:00:17 +0000997 if (cocoa::isRefType(RetTy, "CF", FName)) {
Ted Kremenek12619382009-01-12 21:45:02 +0000998 if (isRetain(FD, FName))
999 S = getUnarySummary(FT, cfretain);
Jordy Rose76c506f2011-08-21 21:58:18 +00001000 else if (isMakeCollectable(FD, FName))
Ted Kremenek12619382009-01-12 21:45:02 +00001001 S = getUnarySummary(FT, cfmakecollectable);
Mike Stump1eb44332009-09-09 15:08:12 +00001002 else
Ted Kremenek12619382009-01-12 21:45:02 +00001003 S = getCFCreateGetRuleSummary(FD, FName);
1004
1005 break;
1006 }
1007
1008 // For CoreGraphics ('CG') types.
Ted Kremenek78acdbf2010-01-27 18:00:17 +00001009 if (cocoa::isRefType(RetTy, "CG", FName)) {
Ted Kremenek12619382009-01-12 21:45:02 +00001010 if (isRetain(FD, FName))
1011 S = getUnarySummary(FT, cfretain);
1012 else
1013 S = getCFCreateGetRuleSummary(FD, FName);
1014
1015 break;
1016 }
1017
1018 // For the Disk Arbitration API (DiskArbitration/DADisk.h)
Ted Kremenek78acdbf2010-01-27 18:00:17 +00001019 if (cocoa::isRefType(RetTy, "DADisk") ||
1020 cocoa::isRefType(RetTy, "DADissenter") ||
1021 cocoa::isRefType(RetTy, "DASessionRef")) {
Ted Kremenek12619382009-01-12 21:45:02 +00001022 S = getCFCreateGetRuleSummary(FD, FName);
1023 break;
1024 }
Mike Stump1eb44332009-09-09 15:08:12 +00001025
Ted Kremenek12619382009-01-12 21:45:02 +00001026 break;
1027 }
1028
1029 // Check for release functions, the only kind of functions that we care
1030 // about that don't return a pointer type.
1031 if (FName[0] == 'C' && (FName[1] == 'F' || FName[1] == 'G')) {
Ted Kremeneke7d03122010-02-08 16:45:01 +00001032 // Test for 'CGCF'.
Benjamin Kramerb6f3c702010-02-08 18:38:55 +00001033 FName = FName.substr(FName.startswith("CGCF") ? 4 : 2);
Ted Kremeneke7d03122010-02-08 16:45:01 +00001034
Ted Kremenekbf0a4dd2009-03-05 22:11:14 +00001035 if (isRelease(FD, FName))
Ted Kremenek12619382009-01-12 21:45:02 +00001036 S = getUnarySummary(FT, cfrelease);
1037 else {
Ted Kremenekb77449c2009-05-03 05:20:50 +00001038 assert (ScratchArgs.isEmpty());
Ted Kremenek68189282009-01-29 22:45:13 +00001039 // Remaining CoreFoundation and CoreGraphics functions.
1040 // We use to assume that they all strictly followed the ownership idiom
1041 // and that ownership cannot be transferred. While this is technically
1042 // correct, many methods allow a tracked object to escape. For example:
1043 //
Mike Stump1eb44332009-09-09 15:08:12 +00001044 // CFMutableDictionaryRef x = CFDictionaryCreateMutable(...);
Ted Kremenek68189282009-01-29 22:45:13 +00001045 // CFDictionaryAddValue(y, key, x);
Mike Stump1eb44332009-09-09 15:08:12 +00001046 // CFRelease(x);
Ted Kremenek68189282009-01-29 22:45:13 +00001047 // ... it is okay to use 'x' since 'y' has a reference to it
1048 //
1049 // We handle this and similar cases with the follow heuristic. If the
Ted Kremenekc4843812009-08-20 00:57:22 +00001050 // function name contains "InsertValue", "SetValue", "AddValue",
1051 // "AppendValue", or "SetAttribute", then we assume that arguments may
1052 // "escape." This means that something else holds on to the object,
1053 // allowing it be used even after its local retain count drops to 0.
Benjamin Kramere45c1492010-01-11 19:46:28 +00001054 ArgEffect E = (StrInStrNoCase(FName, "InsertValue") != StringRef::npos||
1055 StrInStrNoCase(FName, "AddValue") != StringRef::npos ||
1056 StrInStrNoCase(FName, "SetValue") != StringRef::npos ||
1057 StrInStrNoCase(FName, "AppendValue") != StringRef::npos||
Benjamin Kramerc027e542010-01-11 20:15:06 +00001058 StrInStrNoCase(FName, "SetAttribute") != StringRef::npos)
Ted Kremenek68189282009-01-29 22:45:13 +00001059 ? MayEscape : DoNothing;
Mike Stump1eb44332009-09-09 15:08:12 +00001060
Ted Kremenek68189282009-01-29 22:45:13 +00001061 S = getPersistentSummary(RetEffect::MakeNoRet(), DoNothing, E);
Ted Kremenek12619382009-01-12 21:45:02 +00001062 }
1063 }
Ted Kremenek37d785b2008-07-15 16:50:12 +00001064 }
1065 while (0);
Mike Stump1eb44332009-09-09 15:08:12 +00001066
Ted Kremenek4dd8fb42009-05-09 02:58:13 +00001067 // Annotations override defaults.
Jordy Rose4df54fe2011-08-23 04:27:15 +00001068 updateSummaryFromAnnotations(S, FD);
Mike Stump1eb44332009-09-09 15:08:12 +00001069
Ted Kremenekd3dbcf42008-05-05 22:11:16 +00001070 FuncSummaries[FD] = S;
Mike Stump1eb44332009-09-09 15:08:12 +00001071 return S;
Ted Kremenek2fff37e2008-03-06 00:08:09 +00001072}
1073
Ted Kremenek37d785b2008-07-15 16:50:12 +00001074RetainSummary*
Ted Kremenek9c378f72011-08-12 23:37:29 +00001075RetainSummaryManager::getCFCreateGetRuleSummary(const FunctionDecl *FD,
Benjamin Kramerb6f3c702010-02-08 18:38:55 +00001076 StringRef FName) {
Ted Kremenek05560482011-07-16 19:50:32 +00001077 if (coreFoundation::followsCreateRule(FName))
Ted Kremenek86ad3bc2008-05-05 16:51:50 +00001078 return getCFSummaryCreateRule(FD);
Mike Stump1eb44332009-09-09 15:08:12 +00001079
Ted Kremenekd368d712011-05-25 06:19:45 +00001080 return getCFSummaryGetRule(FD);
Ted Kremenek86ad3bc2008-05-05 16:51:50 +00001081}
1082
Ted Kremenekd3dbcf42008-05-05 22:11:16 +00001083RetainSummary*
Ted Kremenek6ad315a2009-02-23 16:51:39 +00001084RetainSummaryManager::getUnarySummary(const FunctionType* FT,
1085 UnaryFuncKind func) {
1086
Ted Kremenek12619382009-01-12 21:45:02 +00001087 // Sanity check that this is *really* a unary function. This can
1088 // happen if people do weird things.
Douglas Gregor72564e72009-02-26 23:50:07 +00001089 const FunctionProtoType* FTP = dyn_cast<FunctionProtoType>(FT);
Ted Kremenek12619382009-01-12 21:45:02 +00001090 if (!FTP || FTP->getNumArgs() != 1)
1091 return getPersistentStopSummary();
Mike Stump1eb44332009-09-09 15:08:12 +00001092
Ted Kremenekb77449c2009-05-03 05:20:50 +00001093 assert (ScratchArgs.isEmpty());
Mike Stump1eb44332009-09-09 15:08:12 +00001094
Jordy Rose76c506f2011-08-21 21:58:18 +00001095 ArgEffect Effect;
Ted Kremenek377e2302008-04-29 05:33:51 +00001096 switch (func) {
Jordy Rose76c506f2011-08-21 21:58:18 +00001097 case cfretain: Effect = IncRef; break;
1098 case cfrelease: Effect = DecRef; break;
1099 case cfmakecollectable: Effect = MakeCollectable; break;
1100 default: llvm_unreachable("Not a supported unary function.");
Ted Kremenek940b1d82008-04-10 23:44:06 +00001101 }
Jordy Rose76c506f2011-08-21 21:58:18 +00001102
1103 ScratchArgs = AF.add(ScratchArgs, 0, Effect);
1104 return getPersistentSummary(RetEffect::MakeNoRet(), DoNothing, DoNothing);
Ted Kremenek00a3a5f2008-03-12 01:21:45 +00001105}
1106
Zhongxing Xubc5495b2010-07-20 02:56:49 +00001107RetainSummary*
Ted Kremenek9c378f72011-08-12 23:37:29 +00001108RetainSummaryManager::getCFSummaryCreateRule(const FunctionDecl *FD) {
Ted Kremenekb77449c2009-05-03 05:20:50 +00001109 assert (ScratchArgs.isEmpty());
Mike Stump1eb44332009-09-09 15:08:12 +00001110
Ted Kremenek2d1652e2009-01-28 05:56:51 +00001111 return getPersistentSummary(RetEffect::MakeOwned(RetEffect::CF, true));
Ted Kremenek00a3a5f2008-03-12 01:21:45 +00001112}
1113
Zhongxing Xubc5495b2010-07-20 02:56:49 +00001114RetainSummary*
Ted Kremenek9c378f72011-08-12 23:37:29 +00001115RetainSummaryManager::getCFSummaryGetRule(const FunctionDecl *FD) {
Mike Stump1eb44332009-09-09 15:08:12 +00001116 assert (ScratchArgs.isEmpty());
Ted Kremenek2d1652e2009-01-28 05:56:51 +00001117 return getPersistentSummary(RetEffect::MakeNotOwned(RetEffect::CF),
1118 DoNothing, DoNothing);
Ted Kremenek00a3a5f2008-03-12 01:21:45 +00001119}
1120
Ted Kremenek6b3a0f72008-03-11 06:39:11 +00001121//===----------------------------------------------------------------------===//
Ted Kremenekd3dbcf42008-05-05 22:11:16 +00001122// Summary creation for Selectors.
1123//===----------------------------------------------------------------------===//
1124
Ted Kremenek1bffd742008-05-06 15:44:25 +00001125RetainSummary*
Ted Kremenek8711c032009-04-29 05:04:30 +00001126RetainSummaryManager::getInitMethodSummary(QualType RetTy) {
Mike Stump1eb44332009-09-09 15:08:12 +00001127 assert(ScratchArgs.isEmpty());
Ted Kremenek78a35a32009-05-12 20:06:54 +00001128 // 'init' methods conceptually return a newly allocated object and claim
Mike Stump1eb44332009-09-09 15:08:12 +00001129 // the receiver.
Ted Kremenek05560482011-07-16 19:50:32 +00001130 if (cocoa::isCocoaObjectRef(RetTy) ||
1131 coreFoundation::isCFObjectRef(RetTy))
Ted Kremenek547d4952009-06-05 23:18:01 +00001132 return getPersistentSummary(ObjCInitRetE, DecRefMsg);
Mike Stump1eb44332009-09-09 15:08:12 +00001133
Jordy Rose4df54fe2011-08-23 04:27:15 +00001134 return 0;
Ted Kremenek46e49ee2008-05-05 23:55:01 +00001135}
Ted Kremenek4dd8fb42009-05-09 02:58:13 +00001136
1137void
Jordy Rose4df54fe2011-08-23 04:27:15 +00001138RetainSummaryManager::updateSummaryFromAnnotations(RetainSummary *&Summ,
Ted Kremenek4dd8fb42009-05-09 02:58:13 +00001139 const FunctionDecl *FD) {
1140 if (!FD)
1141 return;
1142
Jordy Rose0fe62f82011-08-24 09:02:37 +00001143 RetainSummaryTemplate Template(Summ, DefaultSummary, *this);
Jordy Rose4df54fe2011-08-23 04:27:15 +00001144
Ted Kremenek11fe1752011-01-27 18:43:03 +00001145 // Effects on the parameters.
1146 unsigned parm_idx = 0;
1147 for (FunctionDecl::param_const_iterator pi = FD->param_begin(),
John McCall98b8f162011-04-06 09:02:12 +00001148 pe = FD->param_end(); pi != pe; ++pi, ++parm_idx) {
Ted Kremenek11fe1752011-01-27 18:43:03 +00001149 const ParmVarDecl *pd = *pi;
1150 if (pd->getAttr<NSConsumedAttr>()) {
Jordy Rose4df54fe2011-08-23 04:27:15 +00001151 if (!GCEnabled) {
Jordy Rose0fe62f82011-08-24 09:02:37 +00001152 Template->addArg(AF, parm_idx, DecRef);
Jordy Rose4df54fe2011-08-23 04:27:15 +00001153 }
1154 } else if (pd->getAttr<CFConsumedAttr>()) {
Jordy Rose0fe62f82011-08-24 09:02:37 +00001155 Template->addArg(AF, parm_idx, DecRef);
Ted Kremenek11fe1752011-01-27 18:43:03 +00001156 }
1157 }
1158
Ted Kremenekb04cb592009-06-11 18:17:24 +00001159 QualType RetTy = FD->getResultType();
Mike Stump1eb44332009-09-09 15:08:12 +00001160
Ted Kremenek4dd8fb42009-05-09 02:58:13 +00001161 // Determine if there is a special return effect for this method.
Ted Kremenek78acdbf2010-01-27 18:00:17 +00001162 if (cocoa::isCocoaObjectRef(RetTy)) {
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001163 if (FD->getAttr<NSReturnsRetainedAttr>()) {
Jordy Rose0fe62f82011-08-24 09:02:37 +00001164 Template->setRetEffect(ObjCAllocRetE);
Ted Kremenek4dd8fb42009-05-09 02:58:13 +00001165 }
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001166 else if (FD->getAttr<CFReturnsRetainedAttr>()) {
Jordy Rose0fe62f82011-08-24 09:02:37 +00001167 Template->setRetEffect(RetEffect::MakeOwned(RetEffect::CF, true));
Ted Kremenekb04cb592009-06-11 18:17:24 +00001168 }
Ted Kremenek60411112010-02-18 00:06:12 +00001169 else if (FD->getAttr<NSReturnsNotRetainedAttr>()) {
Jordy Rose0fe62f82011-08-24 09:02:37 +00001170 Template->setRetEffect(RetEffect::MakeNotOwned(RetEffect::ObjC));
Ted Kremenek60411112010-02-18 00:06:12 +00001171 }
1172 else if (FD->getAttr<CFReturnsNotRetainedAttr>()) {
Jordy Rose0fe62f82011-08-24 09:02:37 +00001173 Template->setRetEffect(RetEffect::MakeNotOwned(RetEffect::CF));
Jordy Rose4df54fe2011-08-23 04:27:15 +00001174 }
1175 } else if (RetTy->getAs<PointerType>()) {
1176 if (FD->getAttr<CFReturnsRetainedAttr>()) {
Jordy Rose0fe62f82011-08-24 09:02:37 +00001177 Template->setRetEffect(RetEffect::MakeOwned(RetEffect::CF, true));
Jordy Rose4df54fe2011-08-23 04:27:15 +00001178 }
1179 else if (FD->getAttr<CFReturnsNotRetainedAttr>()) {
Jordy Rose0fe62f82011-08-24 09:02:37 +00001180 Template->setRetEffect(RetEffect::MakeNotOwned(RetEffect::CF));
Ted Kremenek60411112010-02-18 00:06:12 +00001181 }
Ted Kremenekb04cb592009-06-11 18:17:24 +00001182 }
Ted Kremenek4dd8fb42009-05-09 02:58:13 +00001183}
1184
1185void
Jordy Rose4df54fe2011-08-23 04:27:15 +00001186RetainSummaryManager::updateSummaryFromAnnotations(RetainSummary *&Summ,
Ted Kremenek4dd8fb42009-05-09 02:58:13 +00001187 const ObjCMethodDecl *MD) {
1188 if (!MD)
1189 return;
1190
Jordy Rose0fe62f82011-08-24 09:02:37 +00001191 RetainSummaryTemplate Template(Summ, DefaultSummary, *this);
Jordy Rose4df54fe2011-08-23 04:27:15 +00001192
Ted Kremenek6d4b76d2009-07-06 18:30:43 +00001193 bool isTrackedLoc = false;
Mike Stump1eb44332009-09-09 15:08:12 +00001194
Ted Kremenek12b94342011-01-27 06:54:14 +00001195 // Effects on the receiver.
1196 if (MD->getAttr<NSConsumesSelfAttr>()) {
Ted Kremenek11fe1752011-01-27 18:43:03 +00001197 if (!GCEnabled)
Jordy Rose0fe62f82011-08-24 09:02:37 +00001198 Template->setReceiverEffect(DecRefMsg);
Ted Kremenek11fe1752011-01-27 18:43:03 +00001199 }
1200
1201 // Effects on the parameters.
1202 unsigned parm_idx = 0;
1203 for (ObjCMethodDecl::param_iterator pi=MD->param_begin(), pe=MD->param_end();
1204 pi != pe; ++pi, ++parm_idx) {
1205 const ParmVarDecl *pd = *pi;
1206 if (pd->getAttr<NSConsumedAttr>()) {
1207 if (!GCEnabled)
Jordy Rose0fe62f82011-08-24 09:02:37 +00001208 Template->addArg(AF, parm_idx, DecRef);
Ted Kremenek11fe1752011-01-27 18:43:03 +00001209 }
1210 else if(pd->getAttr<CFConsumedAttr>()) {
Jordy Rose0fe62f82011-08-24 09:02:37 +00001211 Template->addArg(AF, parm_idx, DecRef);
Ted Kremenek11fe1752011-01-27 18:43:03 +00001212 }
Ted Kremenek12b94342011-01-27 06:54:14 +00001213 }
1214
Ted Kremenek4dd8fb42009-05-09 02:58:13 +00001215 // Determine if there is a special return effect for this method.
Ted Kremenek78acdbf2010-01-27 18:00:17 +00001216 if (cocoa::isCocoaObjectRef(MD->getResultType())) {
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001217 if (MD->getAttr<NSReturnsRetainedAttr>()) {
Jordy Rose0fe62f82011-08-24 09:02:37 +00001218 Template->setRetEffect(ObjCAllocRetE);
Ted Kremenek6d4b76d2009-07-06 18:30:43 +00001219 return;
Ted Kremenek4dd8fb42009-05-09 02:58:13 +00001220 }
Ted Kremenek60411112010-02-18 00:06:12 +00001221 if (MD->getAttr<NSReturnsNotRetainedAttr>()) {
Jordy Rose0fe62f82011-08-24 09:02:37 +00001222 Template->setRetEffect(RetEffect::MakeNotOwned(RetEffect::ObjC));
Ted Kremenek60411112010-02-18 00:06:12 +00001223 return;
1224 }
Mike Stump1eb44332009-09-09 15:08:12 +00001225
Ted Kremenek6d4b76d2009-07-06 18:30:43 +00001226 isTrackedLoc = true;
Jordy Rose0fe62f82011-08-24 09:02:37 +00001227 } else {
Ted Kremenek6217b802009-07-29 21:53:49 +00001228 isTrackedLoc = MD->getResultType()->getAs<PointerType>() != NULL;
Jordy Rose0fe62f82011-08-24 09:02:37 +00001229 }
Mike Stump1eb44332009-09-09 15:08:12 +00001230
Ted Kremenek60411112010-02-18 00:06:12 +00001231 if (isTrackedLoc) {
1232 if (MD->getAttr<CFReturnsRetainedAttr>())
Jordy Rose0fe62f82011-08-24 09:02:37 +00001233 Template->setRetEffect(RetEffect::MakeOwned(RetEffect::CF, true));
Ted Kremenek60411112010-02-18 00:06:12 +00001234 else if (MD->getAttr<CFReturnsNotRetainedAttr>())
Jordy Rose0fe62f82011-08-24 09:02:37 +00001235 Template->setRetEffect(RetEffect::MakeNotOwned(RetEffect::CF));
Ted Kremenek60411112010-02-18 00:06:12 +00001236 }
Ted Kremenek4dd8fb42009-05-09 02:58:13 +00001237}
1238
Ted Kremenek1bffd742008-05-06 15:44:25 +00001239RetainSummary*
Ted Kremenek9c378f72011-08-12 23:37:29 +00001240RetainSummaryManager::getCommonMethodSummary(const ObjCMethodDecl *MD,
Ted Kremeneka8833552009-04-29 23:03:22 +00001241 Selector S, QualType RetTy) {
Ted Kremenek8ee885b2009-04-24 21:56:17 +00001242
Ted Kremenekfcd7c6f2009-04-29 00:42:39 +00001243 if (MD) {
Ted Kremenek376d1e72009-04-24 18:00:17 +00001244 // Scan the method decl for 'void*' arguments. These should be treated
1245 // as 'StopTracking' because they are often used with delegates.
1246 // Delegates are a frequent form of false positives with the retain
1247 // count checker.
1248 unsigned i = 0;
1249 for (ObjCMethodDecl::param_iterator I = MD->param_begin(),
1250 E = MD->param_end(); I != E; ++I, ++i)
1251 if (ParmVarDecl *PD = *I) {
1252 QualType Ty = Ctx.getCanonicalType(PD->getType());
Douglas Gregora4923eb2009-11-16 21:35:15 +00001253 if (Ty.getLocalUnqualifiedType() == Ctx.VoidPtrTy)
Ted Kremenek3baf6722010-11-24 00:54:37 +00001254 ScratchArgs = AF.add(ScratchArgs, i, StopTracking);
Ted Kremenek376d1e72009-04-24 18:00:17 +00001255 }
1256 }
Mike Stump1eb44332009-09-09 15:08:12 +00001257
Ted Kremenek8ee885b2009-04-24 21:56:17 +00001258 // Any special effect for the receiver?
1259 ArgEffect ReceiverEff = DoNothing;
Mike Stump1eb44332009-09-09 15:08:12 +00001260
Ted Kremenek8ee885b2009-04-24 21:56:17 +00001261 // If one of the arguments in the selector has the keyword 'delegate' we
1262 // should stop tracking the reference count for the receiver. This is
1263 // because the reference count is quite possibly handled by a delegate
1264 // method.
1265 if (S.isKeywordSelector()) {
1266 const std::string &str = S.getAsString();
1267 assert(!str.empty());
Benjamin Kramere45c1492010-01-11 19:46:28 +00001268 if (StrInStrNoCase(str, "delegate:") != StringRef::npos)
1269 ReceiverEff = StopTracking;
Ted Kremenek8ee885b2009-04-24 21:56:17 +00001270 }
Mike Stump1eb44332009-09-09 15:08:12 +00001271
Ted Kremenek250b1fa2009-04-23 23:08:22 +00001272 // Look for methods that return an owned object.
Ted Kremenek78acdbf2010-01-27 18:00:17 +00001273 if (cocoa::isCocoaObjectRef(RetTy)) {
Ted Kremenek28f47b92010-12-01 22:16:56 +00001274 // EXPERIMENTAL: assume the Cocoa conventions for all objects returned
Ted Kremenek92511432009-05-03 06:08:32 +00001275 // by instance methods.
Douglas Gregor786dcd92011-07-06 16:00:34 +00001276 RetEffect E = cocoa::followsFundamentalRule(S, MD)
Ted Kremenek7db16042009-05-15 15:49:00 +00001277 ? ObjCAllocRetE : RetEffect::MakeNotOwned(RetEffect::ObjC);
Mike Stump1eb44332009-09-09 15:08:12 +00001278
1279 return getPersistentSummary(E, ReceiverEff, MayEscape);
Ted Kremenek376d1e72009-04-24 18:00:17 +00001280 }
Mike Stump1eb44332009-09-09 15:08:12 +00001281
Ted Kremenek92511432009-05-03 06:08:32 +00001282 // Look for methods that return an owned core foundation object.
Ted Kremenek05560482011-07-16 19:50:32 +00001283 if (coreFoundation::isCFObjectRef(RetTy)) {
Douglas Gregor786dcd92011-07-06 16:00:34 +00001284 RetEffect E = cocoa::followsFundamentalRule(S, MD)
Ted Kremenek7db16042009-05-15 15:49:00 +00001285 ? RetEffect::MakeOwned(RetEffect::CF, true)
1286 : RetEffect::MakeNotOwned(RetEffect::CF);
Mike Stump1eb44332009-09-09 15:08:12 +00001287
Ted Kremenek92511432009-05-03 06:08:32 +00001288 return getPersistentSummary(E, ReceiverEff, MayEscape);
1289 }
Mike Stump1eb44332009-09-09 15:08:12 +00001290
Ted Kremenek92511432009-05-03 06:08:32 +00001291 if (ScratchArgs.isEmpty() && ReceiverEff == DoNothing)
Jordy Rose4df54fe2011-08-23 04:27:15 +00001292 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001293
Ted Kremenek885c27b2009-05-04 05:31:22 +00001294 return getPersistentSummary(RetEffect::MakeNoRet(), ReceiverEff, MayEscape);
Ted Kremenek250b1fa2009-04-23 23:08:22 +00001295}
1296
1297RetainSummary*
Argyrios Kyrtzidis432424d2011-01-25 00:03:53 +00001298RetainSummaryManager::getInstanceMethodSummary(const ObjCMessage &msg,
Ted Kremenek18c66fd2011-08-15 22:09:50 +00001299 const ProgramState *state,
Ted Kremenekb7ddd9b2009-11-13 01:54:21 +00001300 const LocationContext *LC) {
1301
1302 // We need the type-information of the tracked receiver object
1303 // Retrieve it from the state.
Argyrios Kyrtzidis432424d2011-01-25 00:03:53 +00001304 const Expr *Receiver = msg.getInstanceReceiver();
Ted Kremenek9c378f72011-08-12 23:37:29 +00001305 const ObjCInterfaceDecl *ID = 0;
Ted Kremenekb7ddd9b2009-11-13 01:54:21 +00001306
1307 // FIXME: Is this really working as expected? There are cases where
1308 // we just use the 'ID' from the message expression.
Douglas Gregor04badcf2010-04-21 00:45:42 +00001309 SVal receiverV;
1310
Ted Kremenek8f326752010-05-21 21:56:53 +00001311 if (Receiver) {
Douglas Gregor04badcf2010-04-21 00:45:42 +00001312 receiverV = state->getSValAsScalarOrLoc(Receiver);
Ted Kremenekdcee3ce2010-07-01 20:16:50 +00001313
Douglas Gregor04badcf2010-04-21 00:45:42 +00001314 // FIXME: Eventually replace the use of state->get<RefBindings> with
1315 // a generic API for reasoning about the Objective-C types of symbolic
1316 // objects.
1317 if (SymbolRef Sym = receiverV.getAsLocSymbol())
1318 if (const RefVal *T = state->get<RefBindings>(Sym))
Ted Kremenekdcee3ce2010-07-01 20:16:50 +00001319 if (const ObjCObjectPointerType* PT =
Ted Kremenekb7ddd9b2009-11-13 01:54:21 +00001320 T->getType()->getAs<ObjCObjectPointerType>())
Douglas Gregor04badcf2010-04-21 00:45:42 +00001321 ID = PT->getInterfaceDecl();
Ted Kremenekdcee3ce2010-07-01 20:16:50 +00001322
Douglas Gregor04badcf2010-04-21 00:45:42 +00001323 // FIXME: this is a hack. This may or may not be the actual method
1324 // that is called.
1325 if (!ID) {
1326 if (const ObjCObjectPointerType *PT =
1327 Receiver->getType()->getAs<ObjCObjectPointerType>())
Ted Kremenekb7ddd9b2009-11-13 01:54:21 +00001328 ID = PT->getInterfaceDecl();
Douglas Gregor04badcf2010-04-21 00:45:42 +00001329 }
1330 } else {
1331 // FIXME: Hack for 'super'.
Argyrios Kyrtzidis432424d2011-01-25 00:03:53 +00001332 ID = msg.getReceiverInterface();
Ted Kremenekb7ddd9b2009-11-13 01:54:21 +00001333 }
Douglas Gregor04badcf2010-04-21 00:45:42 +00001334
Ted Kremenekb7ddd9b2009-11-13 01:54:21 +00001335 // FIXME: The receiver could be a reference to a class, meaning that
1336 // we should use the class method.
Jordy Rose4df54fe2011-08-23 04:27:15 +00001337 return getInstanceMethodSummary(msg, ID);
Ted Kremenekb7ddd9b2009-11-13 01:54:21 +00001338}
1339
1340RetainSummary*
Ted Kremenekce8a41d2009-04-29 17:09:14 +00001341RetainSummaryManager::getInstanceMethodSummary(Selector S,
1342 IdentifierInfo *ClsName,
Ted Kremenek9c378f72011-08-12 23:37:29 +00001343 const ObjCInterfaceDecl *ID,
Ted Kremeneka8833552009-04-29 23:03:22 +00001344 const ObjCMethodDecl *MD,
Ted Kremenekce8a41d2009-04-29 17:09:14 +00001345 QualType RetTy) {
Ted Kremenek1bffd742008-05-06 15:44:25 +00001346
Ted Kremenek8711c032009-04-29 05:04:30 +00001347 // Look up a summary in our summary cache.
Ted Kremenek614cc542009-07-21 23:27:57 +00001348 RetainSummary *Summ = ObjCMethodSummaries.find(ID, ClsName, S);
Mike Stump1eb44332009-09-09 15:08:12 +00001349
Ted Kremenek614cc542009-07-21 23:27:57 +00001350 if (!Summ) {
1351 assert(ScratchArgs.isEmpty());
Mike Stump1eb44332009-09-09 15:08:12 +00001352
Ted Kremenek614cc542009-07-21 23:27:57 +00001353 // "initXXX": pass-through for receiver.
Douglas Gregor786dcd92011-07-06 16:00:34 +00001354 if (cocoa::deriveNamingConvention(S, MD) == cocoa::InitRule)
Ted Kremenek614cc542009-07-21 23:27:57 +00001355 Summ = getInitMethodSummary(RetTy);
1356 else
1357 Summ = getCommonMethodSummary(MD, S, RetTy);
Mike Stump1eb44332009-09-09 15:08:12 +00001358
Ted Kremenek614cc542009-07-21 23:27:57 +00001359 // Annotations override defaults.
Jordy Rose4df54fe2011-08-23 04:27:15 +00001360 updateSummaryFromAnnotations(Summ, MD);
Mike Stump1eb44332009-09-09 15:08:12 +00001361
Ted Kremenek614cc542009-07-21 23:27:57 +00001362 // Memoize the summary.
1363 ObjCMethodSummaries[ObjCSummaryKey(ID, ClsName, S)] = Summ;
1364 }
Mike Stump1eb44332009-09-09 15:08:12 +00001365
Ted Kremeneke87450e2009-04-23 19:11:35 +00001366 return Summ;
Ted Kremenek46e49ee2008-05-05 23:55:01 +00001367}
1368
Ted Kremenekc8395602008-05-06 21:26:51 +00001369RetainSummary*
Ted Kremenekfcd7c6f2009-04-29 00:42:39 +00001370RetainSummaryManager::getClassMethodSummary(Selector S, IdentifierInfo *ClsName,
Ted Kremeneka8833552009-04-29 23:03:22 +00001371 const ObjCInterfaceDecl *ID,
1372 const ObjCMethodDecl *MD,
1373 QualType RetTy) {
Ted Kremenekde4d5332009-04-24 17:50:11 +00001374
Ted Kremenekfcd7c6f2009-04-29 00:42:39 +00001375 assert(ClsName && "Class name must be specified.");
Mike Stump1eb44332009-09-09 15:08:12 +00001376 RetainSummary *Summ = ObjCClassMethodSummaries.find(ID, ClsName, S);
1377
Ted Kremenek614cc542009-07-21 23:27:57 +00001378 if (!Summ) {
1379 Summ = getCommonMethodSummary(MD, S, RetTy);
Jordy Rose4df54fe2011-08-23 04:27:15 +00001380
Ted Kremenek614cc542009-07-21 23:27:57 +00001381 // Annotations override defaults.
Jordy Rose4df54fe2011-08-23 04:27:15 +00001382 updateSummaryFromAnnotations(Summ, MD);
1383
Ted Kremenek614cc542009-07-21 23:27:57 +00001384 // Memoize the summary.
1385 ObjCClassMethodSummaries[ObjCSummaryKey(ID, ClsName, S)] = Summ;
1386 }
Mike Stump1eb44332009-09-09 15:08:12 +00001387
Ted Kremeneke87450e2009-04-23 19:11:35 +00001388 return Summ;
Ted Kremenekc8395602008-05-06 21:26:51 +00001389}
1390
Mike Stump1eb44332009-09-09 15:08:12 +00001391void RetainSummaryManager::InitializeClassMethodSummaries() {
Ted Kremenekec315332009-05-07 23:40:42 +00001392 assert(ScratchArgs.isEmpty());
Mike Stump1eb44332009-09-09 15:08:12 +00001393 // Create the [NSAssertionHandler currentHander] summary.
Ted Kremenek6fe2b7a2009-10-15 22:25:12 +00001394 addClassMethSummary("NSAssertionHandler", "currentHandler",
Ted Kremenek2d1652e2009-01-28 05:56:51 +00001395 getPersistentSummary(RetEffect::MakeNotOwned(RetEffect::ObjC)));
Mike Stump1eb44332009-09-09 15:08:12 +00001396
Ted Kremenek6d348932008-10-21 15:53:15 +00001397 // Create the [NSAutoreleasePool addObject:] summary.
Ted Kremenek3baf6722010-11-24 00:54:37 +00001398 ScratchArgs = AF.add(ScratchArgs, 0, Autorelease);
Ted Kremenek6fe2b7a2009-10-15 22:25:12 +00001399 addClassMethSummary("NSAutoreleasePool", "addObject",
1400 getPersistentSummary(RetEffect::MakeNoRet(),
1401 DoNothing, Autorelease));
Mike Stump1eb44332009-09-09 15:08:12 +00001402
Ted Kremenekde4d5332009-04-24 17:50:11 +00001403 // Create the summaries for [NSObject performSelector...]. We treat
1404 // these as 'stop tracking' for the arguments because they are often
1405 // used for delegates that can release the object. When we have better
1406 // inter-procedural analysis we can potentially do something better. This
1407 // workaround is to remove false positives.
Ted Kremenek012614e2011-08-17 21:04:19 +00001408 RetainSummary *Summ =
1409 getPersistentSummary(RetEffect::MakeNoRet(), DoNothing, StopTracking);
Ted Kremenekde4d5332009-04-24 17:50:11 +00001410 IdentifierInfo *NSObjectII = &Ctx.Idents.get("NSObject");
1411 addClsMethSummary(NSObjectII, Summ, "performSelector", "withObject",
1412 "afterDelay", NULL);
1413 addClsMethSummary(NSObjectII, Summ, "performSelector", "withObject",
1414 "afterDelay", "inModes", NULL);
1415 addClsMethSummary(NSObjectII, Summ, "performSelectorOnMainThread",
1416 "withObject", "waitUntilDone", NULL);
1417 addClsMethSummary(NSObjectII, Summ, "performSelectorOnMainThread",
1418 "withObject", "waitUntilDone", "modes", NULL);
1419 addClsMethSummary(NSObjectII, Summ, "performSelector", "onThread",
1420 "withObject", "waitUntilDone", NULL);
1421 addClsMethSummary(NSObjectII, Summ, "performSelector", "onThread",
1422 "withObject", "waitUntilDone", "modes", NULL);
1423 addClsMethSummary(NSObjectII, Summ, "performSelectorInBackground",
1424 "withObject", NULL);
Ted Kremenek9c32d082008-05-06 00:30:21 +00001425}
1426
Ted Kremenek1f180c32008-06-23 22:21:20 +00001427void RetainSummaryManager::InitializeMethodSummaries() {
Mike Stump1eb44332009-09-09 15:08:12 +00001428
1429 assert (ScratchArgs.isEmpty());
1430
Ted Kremenekc8395602008-05-06 21:26:51 +00001431 // Create the "init" selector. It just acts as a pass-through for the
1432 // receiver.
Mike Stump1eb44332009-09-09 15:08:12 +00001433 RetainSummary *InitSumm = getPersistentSummary(ObjCInitRetE, DecRefMsg);
Ted Kremenekac02f202009-08-20 05:13:36 +00001434 addNSObjectMethSummary(GetNullarySelector("init", Ctx), InitSumm);
1435
1436 // awakeAfterUsingCoder: behaves basically like an 'init' method. It
1437 // claims the receiver and returns a retained object.
1438 addNSObjectMethSummary(GetUnarySelector("awakeAfterUsingCoder", Ctx),
1439 InitSumm);
Mike Stump1eb44332009-09-09 15:08:12 +00001440
Ted Kremenekc8395602008-05-06 21:26:51 +00001441 // The next methods are allocators.
Ted Kremeneka834fb42009-08-28 19:52:12 +00001442 RetainSummary *AllocSumm = getPersistentSummary(ObjCAllocRetE);
Mike Stump1eb44332009-09-09 15:08:12 +00001443 RetainSummary *CFAllocSumm =
Ted Kremeneka834fb42009-08-28 19:52:12 +00001444 getPersistentSummary(RetEffect::MakeOwned(RetEffect::CF, true));
Mike Stump1eb44332009-09-09 15:08:12 +00001445
Ted Kremenek3c0cea32008-05-06 02:26:56 +00001446 // Create the "retain" selector.
Jordy Rose500abad2011-08-21 19:41:36 +00001447 RetEffect NoRet = RetEffect::MakeNoRet();
1448 RetainSummary *Summ = getPersistentSummary(NoRet, IncRefMsg);
Ted Kremenek553cf182008-06-25 21:21:56 +00001449 addNSObjectMethSummary(GetNullarySelector("retain", Ctx), Summ);
Mike Stump1eb44332009-09-09 15:08:12 +00001450
Ted Kremenek3c0cea32008-05-06 02:26:56 +00001451 // Create the "release" selector.
Jordy Rose500abad2011-08-21 19:41:36 +00001452 Summ = getPersistentSummary(NoRet, DecRefMsg);
Ted Kremenek553cf182008-06-25 21:21:56 +00001453 addNSObjectMethSummary(GetNullarySelector("release", Ctx), Summ);
Mike Stump1eb44332009-09-09 15:08:12 +00001454
Ted Kremenek299e8152008-05-07 21:17:39 +00001455 // Create the "drain" selector.
Jordy Rose500abad2011-08-21 19:41:36 +00001456 Summ = getPersistentSummary(NoRet, isGCEnabled() ? DoNothing : DecRef);
Ted Kremenek553cf182008-06-25 21:21:56 +00001457 addNSObjectMethSummary(GetNullarySelector("drain", Ctx), Summ);
Mike Stump1eb44332009-09-09 15:08:12 +00001458
Ted Kremenekf95e9fc2009-03-17 19:42:23 +00001459 // Create the -dealloc summary.
Jordy Rose500abad2011-08-21 19:41:36 +00001460 Summ = getPersistentSummary(NoRet, Dealloc);
Ted Kremenekf95e9fc2009-03-17 19:42:23 +00001461 addNSObjectMethSummary(GetNullarySelector("dealloc", Ctx), Summ);
Ted Kremenek3c0cea32008-05-06 02:26:56 +00001462
1463 // Create the "autorelease" selector.
Jordy Rose500abad2011-08-21 19:41:36 +00001464 Summ = getPersistentSummary(NoRet, Autorelease);
Ted Kremenek553cf182008-06-25 21:21:56 +00001465 addNSObjectMethSummary(GetNullarySelector("autorelease", Ctx), Summ);
Mike Stump1eb44332009-09-09 15:08:12 +00001466
Ted Kremenekf9a8e2e2009-02-23 17:45:03 +00001467 // Specially handle NSAutoreleasePool.
Ted Kremenek6c4becb2009-02-25 02:54:57 +00001468 addInstMethSummary("NSAutoreleasePool", "init",
Jordy Rose500abad2011-08-21 19:41:36 +00001469 getPersistentSummary(NoRet, NewAutoreleasePool));
Mike Stump1eb44332009-09-09 15:08:12 +00001470
1471 // For NSWindow, allocated objects are (initially) self-owned.
Ted Kremenek89e202d2009-02-23 02:51:29 +00001472 // FIXME: For now we opt for false negatives with NSWindow, as these objects
1473 // self-own themselves. However, they only do this once they are displayed.
1474 // Thus, we need to track an NSWindow's display status.
1475 // This is tracked in <rdar://problem/6062711>.
Ted Kremenek3aa7ecd2009-03-04 23:30:42 +00001476 // See also http://llvm.org/bugs/show_bug.cgi?id=3714.
Ted Kremenek78a35a32009-05-12 20:06:54 +00001477 RetainSummary *NoTrackYet = getPersistentSummary(RetEffect::MakeNoRet(),
1478 StopTracking,
1479 StopTracking);
Mike Stump1eb44332009-09-09 15:08:12 +00001480
Ted Kremenek99d02692009-04-03 19:02:51 +00001481 addClassMethSummary("NSWindow", "alloc", NoTrackYet);
1482
Ted Kremenek3aa7ecd2009-03-04 23:30:42 +00001483#if 0
Ted Kremenek78a35a32009-05-12 20:06:54 +00001484 addInstMethSummary("NSWindow", NoTrackYet, "initWithContentRect",
Ted Kremenekaf9dc272008-08-12 18:48:50 +00001485 "styleMask", "backing", "defer", NULL);
Mike Stump1eb44332009-09-09 15:08:12 +00001486
Ted Kremenek78a35a32009-05-12 20:06:54 +00001487 addInstMethSummary("NSWindow", NoTrackYet, "initWithContentRect",
Ted Kremenekaf9dc272008-08-12 18:48:50 +00001488 "styleMask", "backing", "defer", "screen", NULL);
Ted Kremenek3aa7ecd2009-03-04 23:30:42 +00001489#endif
Mike Stump1eb44332009-09-09 15:08:12 +00001490
Ted Kremenekaf9dc272008-08-12 18:48:50 +00001491 // For NSPanel (which subclasses NSWindow), allocated objects are not
1492 // self-owned.
Ted Kremenek99d02692009-04-03 19:02:51 +00001493 // FIXME: For now we don't track NSPanels. object for the same reason
1494 // as for NSWindow objects.
1495 addClassMethSummary("NSPanel", "alloc", NoTrackYet);
Mike Stump1eb44332009-09-09 15:08:12 +00001496
Ted Kremenek78a35a32009-05-12 20:06:54 +00001497#if 0
1498 addInstMethSummary("NSPanel", NoTrackYet, "initWithContentRect",
Ted Kremenekaf9dc272008-08-12 18:48:50 +00001499 "styleMask", "backing", "defer", NULL);
Mike Stump1eb44332009-09-09 15:08:12 +00001500
Ted Kremenek78a35a32009-05-12 20:06:54 +00001501 addInstMethSummary("NSPanel", NoTrackYet, "initWithContentRect",
Ted Kremenekaf9dc272008-08-12 18:48:50 +00001502 "styleMask", "backing", "defer", "screen", NULL);
Ted Kremenek78a35a32009-05-12 20:06:54 +00001503#endif
Mike Stump1eb44332009-09-09 15:08:12 +00001504
Ted Kremenekba67f6a2009-05-18 23:14:34 +00001505 // Don't track allocated autorelease pools yet, as it is okay to prematurely
1506 // exit a method.
1507 addClassMethSummary("NSAutoreleasePool", "alloc", NoTrackYet);
Ted Kremenek553cf182008-06-25 21:21:56 +00001508
Ted Kremenek767d6492009-05-20 22:39:57 +00001509 // Create summaries QCRenderer/QCView -createSnapShotImageOfType:
1510 addInstMethSummary("QCRenderer", AllocSumm,
1511 "createSnapshotImageOfType", NULL);
1512 addInstMethSummary("QCView", AllocSumm,
1513 "createSnapshotImageOfType", NULL);
1514
Ted Kremenek211a9c62009-06-15 20:58:58 +00001515 // Create summaries for CIContext, 'createCGImage' and
Ted Kremeneka834fb42009-08-28 19:52:12 +00001516 // 'createCGLayerWithSize'. These objects are CF objects, and are not
1517 // automatically garbage collected.
1518 addInstMethSummary("CIContext", CFAllocSumm,
Ted Kremenek767d6492009-05-20 22:39:57 +00001519 "createCGImage", "fromRect", NULL);
Ted Kremeneka834fb42009-08-28 19:52:12 +00001520 addInstMethSummary("CIContext", CFAllocSumm,
Mike Stump1eb44332009-09-09 15:08:12 +00001521 "createCGImage", "fromRect", "format", "colorSpace", NULL);
Ted Kremeneka834fb42009-08-28 19:52:12 +00001522 addInstMethSummary("CIContext", CFAllocSumm, "createCGLayerWithSize",
Ted Kremenek211a9c62009-06-15 20:58:58 +00001523 "info", NULL);
Ted Kremenekb3c3c282008-05-06 00:38:54 +00001524}
1525
Ted Kremenekd3dbcf42008-05-05 22:11:16 +00001526//===----------------------------------------------------------------------===//
Ted Kremenek4d3957d2009-02-24 19:15:11 +00001527// AutoreleaseBindings - State used to track objects in autorelease pools.
Ted Kremenek6d348932008-10-21 15:53:15 +00001528//===----------------------------------------------------------------------===//
1529
Ted Kremenek4d3957d2009-02-24 19:15:11 +00001530typedef llvm::ImmutableMap<SymbolRef, unsigned> ARCounts;
1531typedef llvm::ImmutableMap<SymbolRef, ARCounts> ARPoolContents;
1532typedef llvm::ImmutableList<SymbolRef> ARStack;
Ted Kremenekf9a8e2e2009-02-23 17:45:03 +00001533
Ted Kremenek4d3957d2009-02-24 19:15:11 +00001534static int AutoRCIndex = 0;
Ted Kremenek6d348932008-10-21 15:53:15 +00001535static int AutoRBIndex = 0;
1536
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +00001537namespace { class AutoreleasePoolContents {}; }
1538namespace { class AutoreleaseStack {}; }
Ted Kremenek4d3957d2009-02-24 19:15:11 +00001539
Ted Kremenek6d348932008-10-21 15:53:15 +00001540namespace clang {
Ted Kremenek9ef65372010-12-23 07:20:52 +00001541namespace ento {
Ted Kremenek18c66fd2011-08-15 22:09:50 +00001542template<> struct ProgramStateTrait<AutoreleaseStack>
1543 : public ProgramStatePartialTrait<ARStack> {
Ted Kremenek9c378f72011-08-12 23:37:29 +00001544 static inline void *GDMIndex() { return &AutoRBIndex; }
Ted Kremenek4d3957d2009-02-24 19:15:11 +00001545};
1546
Ted Kremenek18c66fd2011-08-15 22:09:50 +00001547template<> struct ProgramStateTrait<AutoreleasePoolContents>
1548 : public ProgramStatePartialTrait<ARPoolContents> {
Ted Kremenek9c378f72011-08-12 23:37:29 +00001549 static inline void *GDMIndex() { return &AutoRCIndex; }
Ted Kremenek4d3957d2009-02-24 19:15:11 +00001550};
Argyrios Kyrtzidis5a4f98f2010-12-22 18:53:20 +00001551} // end GR namespace
Ted Kremenek4d3957d2009-02-24 19:15:11 +00001552} // end clang namespace
Ted Kremenek6d348932008-10-21 15:53:15 +00001553
Ted Kremenek18c66fd2011-08-15 22:09:50 +00001554static SymbolRef GetCurrentAutoreleasePool(const ProgramState *state) {
Ted Kremenek7037ab82009-03-20 17:34:15 +00001555 ARStack stack = state->get<AutoreleaseStack>();
1556 return stack.isEmpty() ? SymbolRef() : stack.getHead();
1557}
1558
Ted Kremenek18c66fd2011-08-15 22:09:50 +00001559static const ProgramState *
1560SendAutorelease(const ProgramState *state,
1561 ARCounts::Factory &F,
1562 SymbolRef sym) {
Ted Kremenek7037ab82009-03-20 17:34:15 +00001563 SymbolRef pool = GetCurrentAutoreleasePool(state);
Ted Kremenekb65be702009-06-18 01:23:53 +00001564 const ARCounts *cnts = state->get<AutoreleasePoolContents>(pool);
Ted Kremenek7037ab82009-03-20 17:34:15 +00001565 ARCounts newCnts(0);
Mike Stump1eb44332009-09-09 15:08:12 +00001566
Ted Kremenek7037ab82009-03-20 17:34:15 +00001567 if (cnts) {
1568 const unsigned *cnt = (*cnts).lookup(sym);
Ted Kremenek3baf6722010-11-24 00:54:37 +00001569 newCnts = F.add(*cnts, sym, cnt ? *cnt + 1 : 1);
Ted Kremenek7037ab82009-03-20 17:34:15 +00001570 }
1571 else
Ted Kremenek3baf6722010-11-24 00:54:37 +00001572 newCnts = F.add(F.getEmptyMap(), sym, 1);
Mike Stump1eb44332009-09-09 15:08:12 +00001573
Ted Kremenekb65be702009-06-18 01:23:53 +00001574 return state->set<AutoreleasePoolContents>(pool, newCnts);
Ted Kremenek7037ab82009-03-20 17:34:15 +00001575}
1576
Ted Kremenek13922612008-04-16 20:40:59 +00001577//===----------------------------------------------------------------------===//
Ted Kremenekc887d132009-04-29 18:50:19 +00001578// Error reporting.
1579//===----------------------------------------------------------------------===//
Ted Kremenekc887d132009-04-29 18:50:19 +00001580namespace {
Jordy Roseec9ef852011-08-23 20:55:48 +00001581 typedef llvm::DenseMap<const ExplodedNode *, const RetainSummary *>
1582 SummaryLogTy;
1583
Ted Kremenekc887d132009-04-29 18:50:19 +00001584 //===-------------===//
1585 // Bug Descriptions. //
Mike Stump1eb44332009-09-09 15:08:12 +00001586 //===-------------===//
1587
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +00001588 class CFRefBug : public BugType {
Ted Kremenekc887d132009-04-29 18:50:19 +00001589 protected:
Jordy Rose35c86952011-08-24 05:47:39 +00001590 CFRefBug(StringRef name)
1591 : BugType(name, "Memory (Core Foundation/Objective-C)") {}
Ted Kremenekc887d132009-04-29 18:50:19 +00001592 public:
Mike Stump1eb44332009-09-09 15:08:12 +00001593
Ted Kremenekc887d132009-04-29 18:50:19 +00001594 // FIXME: Eventually remove.
Jordy Rose35c86952011-08-24 05:47:39 +00001595 virtual const char *getDescription() const = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001596
Ted Kremenekc887d132009-04-29 18:50:19 +00001597 virtual bool isLeak() const { return false; }
1598 };
Mike Stump1eb44332009-09-09 15:08:12 +00001599
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +00001600 class UseAfterRelease : public CFRefBug {
Ted Kremenekc887d132009-04-29 18:50:19 +00001601 public:
Jordy Rose35c86952011-08-24 05:47:39 +00001602 UseAfterRelease() : CFRefBug("Use-after-release") {}
Mike Stump1eb44332009-09-09 15:08:12 +00001603
Jordy Rose35c86952011-08-24 05:47:39 +00001604 const char *getDescription() const {
Ted Kremenekc887d132009-04-29 18:50:19 +00001605 return "Reference-counted object is used after it is released";
Mike Stump1eb44332009-09-09 15:08:12 +00001606 }
Ted Kremenekc887d132009-04-29 18:50:19 +00001607 };
Mike Stump1eb44332009-09-09 15:08:12 +00001608
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +00001609 class BadRelease : public CFRefBug {
Ted Kremenekc887d132009-04-29 18:50:19 +00001610 public:
Jordy Rose35c86952011-08-24 05:47:39 +00001611 BadRelease() : CFRefBug("Bad release") {}
Mike Stump1eb44332009-09-09 15:08:12 +00001612
Jordy Rose35c86952011-08-24 05:47:39 +00001613 const char *getDescription() const {
Ted Kremenekbb206fd2009-10-01 17:31:50 +00001614 return "Incorrect decrement of the reference count of an object that is "
1615 "not owned at this point by the caller";
Ted Kremenekc887d132009-04-29 18:50:19 +00001616 }
1617 };
Mike Stump1eb44332009-09-09 15:08:12 +00001618
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +00001619 class DeallocGC : public CFRefBug {
Ted Kremenekc887d132009-04-29 18:50:19 +00001620 public:
Jordy Rose35c86952011-08-24 05:47:39 +00001621 DeallocGC()
1622 : CFRefBug("-dealloc called while using garbage collection") {}
Mike Stump1eb44332009-09-09 15:08:12 +00001623
Ted Kremenekc887d132009-04-29 18:50:19 +00001624 const char *getDescription() const {
Ted Kremenek369de562009-05-09 00:10:05 +00001625 return "-dealloc called while using garbage collection";
Ted Kremenekc887d132009-04-29 18:50:19 +00001626 }
1627 };
Mike Stump1eb44332009-09-09 15:08:12 +00001628
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +00001629 class DeallocNotOwned : public CFRefBug {
Ted Kremenekc887d132009-04-29 18:50:19 +00001630 public:
Jordy Rose35c86952011-08-24 05:47:39 +00001631 DeallocNotOwned()
1632 : CFRefBug("-dealloc sent to non-exclusively owned object") {}
Mike Stump1eb44332009-09-09 15:08:12 +00001633
Ted Kremenekc887d132009-04-29 18:50:19 +00001634 const char *getDescription() const {
1635 return "-dealloc sent to object that may be referenced elsewhere";
1636 }
Mike Stump1eb44332009-09-09 15:08:12 +00001637 };
1638
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +00001639 class OverAutorelease : public CFRefBug {
Ted Kremenek369de562009-05-09 00:10:05 +00001640 public:
Jordy Rose35c86952011-08-24 05:47:39 +00001641 OverAutorelease()
1642 : CFRefBug("Object sent -autorelease too many times") {}
Mike Stump1eb44332009-09-09 15:08:12 +00001643
Ted Kremenek369de562009-05-09 00:10:05 +00001644 const char *getDescription() const {
Ted Kremenekeaedfea2009-05-10 05:11:21 +00001645 return "Object sent -autorelease too many times";
Ted Kremenek369de562009-05-09 00:10:05 +00001646 }
1647 };
Mike Stump1eb44332009-09-09 15:08:12 +00001648
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +00001649 class ReturnedNotOwnedForOwned : public CFRefBug {
Ted Kremeneke8720ce2009-05-10 06:25:57 +00001650 public:
Jordy Rose35c86952011-08-24 05:47:39 +00001651 ReturnedNotOwnedForOwned()
1652 : CFRefBug("Method should return an owned object") {}
Mike Stump1eb44332009-09-09 15:08:12 +00001653
Ted Kremeneke8720ce2009-05-10 06:25:57 +00001654 const char *getDescription() const {
Jordy Rose5b5402b2011-07-15 22:17:54 +00001655 return "Object with a +0 retain count returned to caller where a +1 "
Ted Kremeneke8720ce2009-05-10 06:25:57 +00001656 "(owning) retain count is expected";
1657 }
1658 };
Mike Stump1eb44332009-09-09 15:08:12 +00001659
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +00001660 class Leak : public CFRefBug {
Ted Kremenekc887d132009-04-29 18:50:19 +00001661 const bool isReturn;
1662 protected:
Jordy Rose35c86952011-08-24 05:47:39 +00001663 Leak(StringRef name, bool isRet)
Jordy Rosedb92bb62011-08-25 01:14:38 +00001664 : CFRefBug(name), isReturn(isRet) {
1665 // Leaks should not be reported if they are post-dominated by a sink.
1666 setSuppressOnSink(true);
1667 }
Ted Kremenekc887d132009-04-29 18:50:19 +00001668 public:
Mike Stump1eb44332009-09-09 15:08:12 +00001669
Jordy Rose35c86952011-08-24 05:47:39 +00001670 const char *getDescription() const { return ""; }
Mike Stump1eb44332009-09-09 15:08:12 +00001671
Ted Kremenekc887d132009-04-29 18:50:19 +00001672 bool isLeak() const { return true; }
1673 };
Mike Stump1eb44332009-09-09 15:08:12 +00001674
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +00001675 class LeakAtReturn : public Leak {
Ted Kremenekc887d132009-04-29 18:50:19 +00001676 public:
Jordy Rose35c86952011-08-24 05:47:39 +00001677 LeakAtReturn(StringRef name)
1678 : Leak(name, true) {}
Ted Kremenekc887d132009-04-29 18:50:19 +00001679 };
Mike Stump1eb44332009-09-09 15:08:12 +00001680
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +00001681 class LeakWithinFunction : public Leak {
Ted Kremenekc887d132009-04-29 18:50:19 +00001682 public:
Jordy Rose35c86952011-08-24 05:47:39 +00001683 LeakWithinFunction(StringRef name)
1684 : Leak(name, false) {}
Mike Stump1eb44332009-09-09 15:08:12 +00001685 };
1686
Ted Kremenekc887d132009-04-29 18:50:19 +00001687 //===---------===//
1688 // Bug Reports. //
1689 //===---------===//
Mike Stump1eb44332009-09-09 15:08:12 +00001690
Anna Zaksdc757b02011-08-19 23:21:56 +00001691 class CFRefReportVisitor : public BugReporterVisitor {
Anna Zaks23f395e2011-08-20 01:27:22 +00001692 protected:
Anna Zaksdc757b02011-08-19 23:21:56 +00001693 SymbolRef Sym;
Jordy Roseec9ef852011-08-23 20:55:48 +00001694 const SummaryLogTy &SummaryLog;
Jordy Rose35c86952011-08-24 05:47:39 +00001695 bool GCEnabled;
Anna Zaks23f395e2011-08-20 01:27:22 +00001696
Anna Zaksdc757b02011-08-19 23:21:56 +00001697 public:
Jordy Rose35c86952011-08-24 05:47:39 +00001698 CFRefReportVisitor(SymbolRef sym, bool gcEnabled, const SummaryLogTy &log)
1699 : Sym(sym), SummaryLog(log), GCEnabled(gcEnabled) {}
Anna Zaksdc757b02011-08-19 23:21:56 +00001700
Anna Zaks23f395e2011-08-20 01:27:22 +00001701 virtual void Profile(llvm::FoldingSetNodeID &ID) const {
Anna Zaksdc757b02011-08-19 23:21:56 +00001702 static int x = 0;
1703 ID.AddPointer(&x);
1704 ID.AddPointer(Sym);
1705 }
1706
Anna Zaks23f395e2011-08-20 01:27:22 +00001707 virtual PathDiagnosticPiece *VisitNode(const ExplodedNode *N,
1708 const ExplodedNode *PrevN,
1709 BugReporterContext &BRC,
1710 BugReport &BR);
1711
1712 virtual PathDiagnosticPiece *getEndPath(BugReporterContext &BRC,
1713 const ExplodedNode *N,
1714 BugReport &BR);
1715 };
1716
1717 class CFRefLeakReportVisitor : public CFRefReportVisitor {
1718 public:
Jordy Rose35c86952011-08-24 05:47:39 +00001719 CFRefLeakReportVisitor(SymbolRef sym, bool GCEnabled,
Jordy Roseec9ef852011-08-23 20:55:48 +00001720 const SummaryLogTy &log)
Jordy Rose35c86952011-08-24 05:47:39 +00001721 : CFRefReportVisitor(sym, GCEnabled, log) {}
Anna Zaks23f395e2011-08-20 01:27:22 +00001722
1723 PathDiagnosticPiece *getEndPath(BugReporterContext &BRC,
1724 const ExplodedNode *N,
1725 BugReport &BR);
Anna Zaksdc757b02011-08-19 23:21:56 +00001726 };
1727
Anna Zakse172e8b2011-08-17 23:00:25 +00001728 class CFRefReport : public BugReport {
Jordy Rose20589562011-08-24 22:39:09 +00001729 void addGCModeDescription(const LangOptions &LOpts, bool GCEnabled);
Jordy Rose35c86952011-08-24 05:47:39 +00001730
Ted Kremenekc887d132009-04-29 18:50:19 +00001731 public:
Jordy Rose20589562011-08-24 22:39:09 +00001732 CFRefReport(CFRefBug &D, const LangOptions &LOpts, bool GCEnabled,
1733 const SummaryLogTy &Log, ExplodedNode *n, SymbolRef sym,
1734 bool registerVisitor = true)
Anna Zaksedf4dae2011-08-22 18:54:07 +00001735 : BugReport(D, D.getDescription(), n) {
Anna Zaks23f395e2011-08-20 01:27:22 +00001736 if (registerVisitor)
Jordy Rose20589562011-08-24 22:39:09 +00001737 addVisitor(new CFRefReportVisitor(sym, GCEnabled, Log));
1738 addGCModeDescription(LOpts, GCEnabled);
Anna Zaksdc757b02011-08-19 23:21:56 +00001739 }
Ted Kremenekeaedfea2009-05-10 05:11:21 +00001740
Jordy Rose20589562011-08-24 22:39:09 +00001741 CFRefReport(CFRefBug &D, const LangOptions &LOpts, bool GCEnabled,
1742 const SummaryLogTy &Log, ExplodedNode *n, SymbolRef sym,
1743 StringRef endText)
Anna Zaksedf4dae2011-08-22 18:54:07 +00001744 : BugReport(D, D.getDescription(), endText, n) {
Jordy Rose20589562011-08-24 22:39:09 +00001745 addVisitor(new CFRefReportVisitor(sym, GCEnabled, Log));
1746 addGCModeDescription(LOpts, GCEnabled);
Anna Zaksdc757b02011-08-19 23:21:56 +00001747 }
Mike Stump1eb44332009-09-09 15:08:12 +00001748
Anna Zakse172e8b2011-08-17 23:00:25 +00001749 virtual std::pair<ranges_iterator, ranges_iterator> getRanges() {
Anna Zaksedf4dae2011-08-22 18:54:07 +00001750 const CFRefBug& BugTy = static_cast<CFRefBug&>(getBugType());
1751 if (!BugTy.isLeak())
Anna Zakse172e8b2011-08-17 23:00:25 +00001752 return BugReport::getRanges();
Ted Kremenekc887d132009-04-29 18:50:19 +00001753 else
Argyrios Kyrtzidis640ccf02010-12-04 01:12:15 +00001754 return std::make_pair(ranges_iterator(), ranges_iterator());
Ted Kremenekc887d132009-04-29 18:50:19 +00001755 }
Ted Kremenekc887d132009-04-29 18:50:19 +00001756 };
Ted Kremenekeaedfea2009-05-10 05:11:21 +00001757
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +00001758 class CFRefLeakReport : public CFRefReport {
Ted Kremenekc887d132009-04-29 18:50:19 +00001759 const MemRegion* AllocBinding;
Anna Zaks23f395e2011-08-20 01:27:22 +00001760
Ted Kremenekc887d132009-04-29 18:50:19 +00001761 public:
Jordy Rose20589562011-08-24 22:39:09 +00001762 CFRefLeakReport(CFRefBug &D, const LangOptions &LOpts, bool GCEnabled,
1763 const SummaryLogTy &Log, ExplodedNode *n, SymbolRef sym,
1764 ExprEngine &Eng);
Mike Stump1eb44332009-09-09 15:08:12 +00001765
Anna Zaks590dd8e2011-09-20 21:38:35 +00001766 PathDiagnosticLocation getLocation(const SourceManager &SM) const {
1767 assert(Location.isValid());
1768 return Location;
1769 }
Mike Stump1eb44332009-09-09 15:08:12 +00001770 };
Ted Kremenekc887d132009-04-29 18:50:19 +00001771} // end anonymous namespace
1772
Jordy Rose20589562011-08-24 22:39:09 +00001773void CFRefReport::addGCModeDescription(const LangOptions &LOpts,
1774 bool GCEnabled) {
Jordy Rosef95b19d2011-08-24 20:38:42 +00001775 const char *GCModeDescription = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001776
Douglas Gregore289d812011-09-13 17:21:33 +00001777 switch (LOpts.getGC()) {
Anna Zaks7f2531c2011-08-22 20:31:28 +00001778 case LangOptions::GCOnly:
Jordy Rose20589562011-08-24 22:39:09 +00001779 assert(GCEnabled);
Jordy Rose35c86952011-08-24 05:47:39 +00001780 GCModeDescription = "Code is compiled to only use garbage collection";
1781 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001782
Anna Zaks7f2531c2011-08-22 20:31:28 +00001783 case LangOptions::NonGC:
Jordy Rose20589562011-08-24 22:39:09 +00001784 assert(!GCEnabled);
Jordy Rose35c86952011-08-24 05:47:39 +00001785 GCModeDescription = "Code is compiled to use reference counts";
1786 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001787
Anna Zaks7f2531c2011-08-22 20:31:28 +00001788 case LangOptions::HybridGC:
Jordy Rose20589562011-08-24 22:39:09 +00001789 if (GCEnabled) {
Jordy Rose35c86952011-08-24 05:47:39 +00001790 GCModeDescription = "Code is compiled to use either garbage collection "
1791 "(GC) or reference counts (non-GC). The bug occurs "
1792 "with GC enabled";
1793 break;
1794 } else {
1795 GCModeDescription = "Code is compiled to use either garbage collection "
1796 "(GC) or reference counts (non-GC). The bug occurs "
1797 "in non-GC mode";
1798 break;
Anna Zaks7f2531c2011-08-22 20:31:28 +00001799 }
Ted Kremenekc887d132009-04-29 18:50:19 +00001800 }
Jordy Rose35c86952011-08-24 05:47:39 +00001801
Jordy Rosef95b19d2011-08-24 20:38:42 +00001802 assert(GCModeDescription && "invalid/unknown GC mode");
Jordy Rose35c86952011-08-24 05:47:39 +00001803 addExtraText(GCModeDescription);
Ted Kremenekc887d132009-04-29 18:50:19 +00001804}
1805
Jordy Rose910c4052011-09-02 06:44:22 +00001806// FIXME: This should be a method on SmallVector.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001807static inline bool contains(const SmallVectorImpl<ArgEffect>& V,
Ted Kremenekc887d132009-04-29 18:50:19 +00001808 ArgEffect X) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00001809 for (SmallVectorImpl<ArgEffect>::const_iterator I=V.begin(), E=V.end();
Ted Kremenekc887d132009-04-29 18:50:19 +00001810 I!=E; ++I)
1811 if (*I == X) return true;
Mike Stump1eb44332009-09-09 15:08:12 +00001812
Ted Kremenekc887d132009-04-29 18:50:19 +00001813 return false;
1814}
1815
Anna Zaksdc757b02011-08-19 23:21:56 +00001816PathDiagnosticPiece *CFRefReportVisitor::VisitNode(const ExplodedNode *N,
1817 const ExplodedNode *PrevN,
1818 BugReporterContext &BRC,
1819 BugReport &BR) {
Mike Stump1eb44332009-09-09 15:08:12 +00001820
Jordy Rosef53e8c72011-08-23 19:43:16 +00001821 if (!isa<StmtPoint>(N->getLocation()))
Ted Kremenek2033a952009-05-13 07:12:33 +00001822 return NULL;
Mike Stump1eb44332009-09-09 15:08:12 +00001823
Ted Kremenek8966bc12009-05-06 21:39:49 +00001824 // Check if the type state has changed.
Ted Kremenek18c66fd2011-08-15 22:09:50 +00001825 const ProgramState *PrevSt = PrevN->getState();
1826 const ProgramState *CurrSt = N->getState();
Mike Stump1eb44332009-09-09 15:08:12 +00001827
1828 const RefVal* CurrT = CurrSt->get<RefBindings>(Sym);
Ted Kremenekc887d132009-04-29 18:50:19 +00001829 if (!CurrT) return NULL;
Mike Stump1eb44332009-09-09 15:08:12 +00001830
Ted Kremenekb65be702009-06-18 01:23:53 +00001831 const RefVal &CurrV = *CurrT;
1832 const RefVal *PrevT = PrevSt->get<RefBindings>(Sym);
Mike Stump1eb44332009-09-09 15:08:12 +00001833
Ted Kremenekc887d132009-04-29 18:50:19 +00001834 // Create a string buffer to constain all the useful things we want
1835 // to tell the user.
1836 std::string sbuf;
1837 llvm::raw_string_ostream os(sbuf);
Mike Stump1eb44332009-09-09 15:08:12 +00001838
Ted Kremenekc887d132009-04-29 18:50:19 +00001839 // This is the allocation site since the previous node had no bindings
1840 // for this symbol.
1841 if (!PrevT) {
Jordy Rosef53e8c72011-08-23 19:43:16 +00001842 const Stmt *S = cast<StmtPoint>(N->getLocation()).getStmt();
Mike Stump1eb44332009-09-09 15:08:12 +00001843
Ted Kremenek5f85e172009-07-22 22:35:28 +00001844 if (const CallExpr *CE = dyn_cast<CallExpr>(S)) {
Ted Kremenekc887d132009-04-29 18:50:19 +00001845 // Get the name of the callee (if it is available).
Ted Kremenek13976632010-02-08 16:18:51 +00001846 SVal X = CurrSt->getSValAsScalarOrLoc(CE->getCallee());
Ted Kremenek9c378f72011-08-12 23:37:29 +00001847 if (const FunctionDecl *FD = X.getAsFunctionDecl())
Benjamin Kramer900fc632010-04-17 09:33:03 +00001848 os << "Call to function '" << FD << '\'';
Ted Kremenekc887d132009-04-29 18:50:19 +00001849 else
Mike Stump1eb44332009-09-09 15:08:12 +00001850 os << "function call";
1851 }
Argyrios Kyrtzidis14429b92011-01-25 00:04:03 +00001852 else if (isa<ObjCMessageExpr>(S)) {
Ted Kremenekc887d132009-04-29 18:50:19 +00001853 os << "Method";
Argyrios Kyrtzidis14429b92011-01-25 00:04:03 +00001854 } else {
1855 os << "Property";
Ted Kremenekc887d132009-04-29 18:50:19 +00001856 }
Mike Stump1eb44332009-09-09 15:08:12 +00001857
Ted Kremenekc887d132009-04-29 18:50:19 +00001858 if (CurrV.getObjKind() == RetEffect::CF) {
1859 os << " returns a Core Foundation object with a ";
1860 }
1861 else {
1862 assert (CurrV.getObjKind() == RetEffect::ObjC);
1863 os << " returns an Objective-C object with a ";
1864 }
Mike Stump1eb44332009-09-09 15:08:12 +00001865
Ted Kremenekc887d132009-04-29 18:50:19 +00001866 if (CurrV.isOwned()) {
Ted Kremenekf1365462011-05-26 18:45:44 +00001867 os << "+1 retain count";
Mike Stump1eb44332009-09-09 15:08:12 +00001868
Jordy Rose35c86952011-08-24 05:47:39 +00001869 if (GCEnabled) {
Ted Kremenekc887d132009-04-29 18:50:19 +00001870 assert(CurrV.getObjKind() == RetEffect::CF);
Jordy Rose5b5402b2011-07-15 22:17:54 +00001871 os << ". "
Ted Kremenekc887d132009-04-29 18:50:19 +00001872 "Core Foundation objects are not automatically garbage collected.";
1873 }
1874 }
1875 else {
1876 assert (CurrV.isNotOwned());
Ted Kremenekf1365462011-05-26 18:45:44 +00001877 os << "+0 retain count";
Ted Kremenekc887d132009-04-29 18:50:19 +00001878 }
Mike Stump1eb44332009-09-09 15:08:12 +00001879
Anna Zaks220ac8c2011-09-15 01:08:34 +00001880 PathDiagnosticLocation Pos(S, BRC.getSourceManager(),
1881 N->getLocationContext());
Ted Kremenekc887d132009-04-29 18:50:19 +00001882 return new PathDiagnosticEventPiece(Pos, os.str());
1883 }
Mike Stump1eb44332009-09-09 15:08:12 +00001884
Ted Kremenekc887d132009-04-29 18:50:19 +00001885 // Gather up the effects that were performed on the object at this
1886 // program point
Chris Lattner5f9e2722011-07-23 10:55:15 +00001887 SmallVector<ArgEffect, 2> AEffects;
Mike Stump1eb44332009-09-09 15:08:12 +00001888
Jordy Roseec9ef852011-08-23 20:55:48 +00001889 const ExplodedNode *OrigNode = BRC.getNodeResolver().getOriginalNode(N);
1890 if (const RetainSummary *Summ = SummaryLog.lookup(OrigNode)) {
Ted Kremenekc887d132009-04-29 18:50:19 +00001891 // We only have summaries attached to nodes after evaluating CallExpr and
1892 // ObjCMessageExprs.
Jordy Rosef53e8c72011-08-23 19:43:16 +00001893 const Stmt *S = cast<StmtPoint>(N->getLocation()).getStmt();
Mike Stump1eb44332009-09-09 15:08:12 +00001894
Ted Kremenek5f85e172009-07-22 22:35:28 +00001895 if (const CallExpr *CE = dyn_cast<CallExpr>(S)) {
Ted Kremenekc887d132009-04-29 18:50:19 +00001896 // Iterate through the parameter expressions and see if the symbol
1897 // was ever passed as an argument.
1898 unsigned i = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001899
Ted Kremenek5f85e172009-07-22 22:35:28 +00001900 for (CallExpr::const_arg_iterator AI=CE->arg_begin(), AE=CE->arg_end();
Ted Kremenekc887d132009-04-29 18:50:19 +00001901 AI!=AE; ++AI, ++i) {
Mike Stump1eb44332009-09-09 15:08:12 +00001902
Ted Kremenekc887d132009-04-29 18:50:19 +00001903 // Retrieve the value of the argument. Is it the symbol
1904 // we are interested in?
Ted Kremenek13976632010-02-08 16:18:51 +00001905 if (CurrSt->getSValAsScalarOrLoc(*AI).getAsLocSymbol() != Sym)
Ted Kremenekc887d132009-04-29 18:50:19 +00001906 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001907
Ted Kremenekc887d132009-04-29 18:50:19 +00001908 // We have an argument. Get the effect!
1909 AEffects.push_back(Summ->getArg(i));
1910 }
1911 }
Mike Stump1eb44332009-09-09 15:08:12 +00001912 else if (const ObjCMessageExpr *ME = dyn_cast<ObjCMessageExpr>(S)) {
Douglas Gregor04badcf2010-04-21 00:45:42 +00001913 if (const Expr *receiver = ME->getInstanceReceiver())
Ted Kremenek13976632010-02-08 16:18:51 +00001914 if (CurrSt->getSValAsScalarOrLoc(receiver).getAsLocSymbol() == Sym) {
Ted Kremenekc887d132009-04-29 18:50:19 +00001915 // The symbol we are tracking is the receiver.
1916 AEffects.push_back(Summ->getReceiverEffect());
1917 }
1918 }
1919 }
Mike Stump1eb44332009-09-09 15:08:12 +00001920
Ted Kremenekc887d132009-04-29 18:50:19 +00001921 do {
1922 // Get the previous type state.
1923 RefVal PrevV = *PrevT;
Mike Stump1eb44332009-09-09 15:08:12 +00001924
Ted Kremenekc887d132009-04-29 18:50:19 +00001925 // Specially handle -dealloc.
Jordy Rose35c86952011-08-24 05:47:39 +00001926 if (!GCEnabled && contains(AEffects, Dealloc)) {
Ted Kremenekc887d132009-04-29 18:50:19 +00001927 // Determine if the object's reference count was pushed to zero.
1928 assert(!(PrevV == CurrV) && "The typestate *must* have changed.");
1929 // We may not have transitioned to 'release' if we hit an error.
1930 // This case is handled elsewhere.
1931 if (CurrV.getKind() == RefVal::Released) {
Ted Kremenekf21332e2009-05-08 20:01:42 +00001932 assert(CurrV.getCombinedCounts() == 0);
Ted Kremenekc887d132009-04-29 18:50:19 +00001933 os << "Object released by directly sending the '-dealloc' message";
1934 break;
1935 }
1936 }
Mike Stump1eb44332009-09-09 15:08:12 +00001937
Ted Kremenekc887d132009-04-29 18:50:19 +00001938 // Specially handle CFMakeCollectable and friends.
1939 if (contains(AEffects, MakeCollectable)) {
1940 // Get the name of the function.
Jordy Rosef53e8c72011-08-23 19:43:16 +00001941 const Stmt *S = cast<StmtPoint>(N->getLocation()).getStmt();
Ted Kremenek13976632010-02-08 16:18:51 +00001942 SVal X = CurrSt->getSValAsScalarOrLoc(cast<CallExpr>(S)->getCallee());
Ted Kremenek9c378f72011-08-12 23:37:29 +00001943 const FunctionDecl *FD = X.getAsFunctionDecl();
Mike Stump1eb44332009-09-09 15:08:12 +00001944
Jordy Rose35c86952011-08-24 05:47:39 +00001945 if (GCEnabled) {
Ted Kremenekc887d132009-04-29 18:50:19 +00001946 // Determine if the object's reference count was pushed to zero.
1947 assert(!(PrevV == CurrV) && "The typestate *must* have changed.");
Mike Stump1eb44332009-09-09 15:08:12 +00001948
Jordy Rose7df12342011-08-21 05:25:15 +00001949 os << "In GC mode a call to '" << FD
Ted Kremenekc887d132009-04-29 18:50:19 +00001950 << "' decrements an object's retain count and registers the "
1951 "object with the garbage collector. ";
Mike Stump1eb44332009-09-09 15:08:12 +00001952
Ted Kremenekc887d132009-04-29 18:50:19 +00001953 if (CurrV.getKind() == RefVal::Released) {
1954 assert(CurrV.getCount() == 0);
1955 os << "Since it now has a 0 retain count the object can be "
1956 "automatically collected by the garbage collector.";
1957 }
1958 else
1959 os << "An object must have a 0 retain count to be garbage collected. "
1960 "After this call its retain count is +" << CurrV.getCount()
1961 << '.';
1962 }
Mike Stump1eb44332009-09-09 15:08:12 +00001963 else
Jordy Rose7df12342011-08-21 05:25:15 +00001964 os << "When GC is not enabled a call to '" << FD
Ted Kremenekc887d132009-04-29 18:50:19 +00001965 << "' has no effect on its argument.";
Mike Stump1eb44332009-09-09 15:08:12 +00001966
Ted Kremenekc887d132009-04-29 18:50:19 +00001967 // Nothing more to say.
1968 break;
1969 }
Mike Stump1eb44332009-09-09 15:08:12 +00001970
1971 // Determine if the typestate has changed.
Ted Kremenekc887d132009-04-29 18:50:19 +00001972 if (!(PrevV == CurrV))
1973 switch (CurrV.getKind()) {
1974 case RefVal::Owned:
1975 case RefVal::NotOwned:
Mike Stump1eb44332009-09-09 15:08:12 +00001976
Ted Kremenekf21332e2009-05-08 20:01:42 +00001977 if (PrevV.getCount() == CurrV.getCount()) {
1978 // Did an autorelease message get sent?
1979 if (PrevV.getAutoreleaseCount() == CurrV.getAutoreleaseCount())
1980 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001981
Zhongxing Xu264e9372009-05-12 10:10:00 +00001982 assert(PrevV.getAutoreleaseCount() < CurrV.getAutoreleaseCount());
Ted Kremenekeaedfea2009-05-10 05:11:21 +00001983 os << "Object sent -autorelease message";
Ted Kremenekf21332e2009-05-08 20:01:42 +00001984 break;
1985 }
Mike Stump1eb44332009-09-09 15:08:12 +00001986
Ted Kremenekc887d132009-04-29 18:50:19 +00001987 if (PrevV.getCount() > CurrV.getCount())
1988 os << "Reference count decremented.";
1989 else
1990 os << "Reference count incremented.";
Mike Stump1eb44332009-09-09 15:08:12 +00001991
Ted Kremenekc887d132009-04-29 18:50:19 +00001992 if (unsigned Count = CurrV.getCount())
1993 os << " The object now has a +" << Count << " retain count.";
Mike Stump1eb44332009-09-09 15:08:12 +00001994
Ted Kremenekc887d132009-04-29 18:50:19 +00001995 if (PrevV.getKind() == RefVal::Released) {
Jordy Rose35c86952011-08-24 05:47:39 +00001996 assert(GCEnabled && CurrV.getCount() > 0);
Ted Kremenekc887d132009-04-29 18:50:19 +00001997 os << " The object is not eligible for garbage collection until the "
1998 "retain count reaches 0 again.";
1999 }
Mike Stump1eb44332009-09-09 15:08:12 +00002000
Ted Kremenekc887d132009-04-29 18:50:19 +00002001 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002002
Ted Kremenekc887d132009-04-29 18:50:19 +00002003 case RefVal::Released:
2004 os << "Object released.";
2005 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002006
Ted Kremenekc887d132009-04-29 18:50:19 +00002007 case RefVal::ReturnedOwned:
2008 os << "Object returned to caller as an owning reference (single retain "
Ted Kremenekf1365462011-05-26 18:45:44 +00002009 "count transferred to caller)";
Ted Kremenekc887d132009-04-29 18:50:19 +00002010 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002011
Ted Kremenekc887d132009-04-29 18:50:19 +00002012 case RefVal::ReturnedNotOwned:
Ted Kremenekf1365462011-05-26 18:45:44 +00002013 os << "Object returned to caller with a +0 retain count";
Ted Kremenekc887d132009-04-29 18:50:19 +00002014 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002015
Ted Kremenekc887d132009-04-29 18:50:19 +00002016 default:
2017 return NULL;
2018 }
Mike Stump1eb44332009-09-09 15:08:12 +00002019
Ted Kremenekc887d132009-04-29 18:50:19 +00002020 // Emit any remaining diagnostics for the argument effects (if any).
Chris Lattner5f9e2722011-07-23 10:55:15 +00002021 for (SmallVectorImpl<ArgEffect>::iterator I=AEffects.begin(),
Ted Kremenekc887d132009-04-29 18:50:19 +00002022 E=AEffects.end(); I != E; ++I) {
Mike Stump1eb44332009-09-09 15:08:12 +00002023
Ted Kremenekc887d132009-04-29 18:50:19 +00002024 // A bunch of things have alternate behavior under GC.
Jordy Rose35c86952011-08-24 05:47:39 +00002025 if (GCEnabled)
Ted Kremenekc887d132009-04-29 18:50:19 +00002026 switch (*I) {
2027 default: break;
2028 case Autorelease:
2029 os << "In GC mode an 'autorelease' has no effect.";
2030 continue;
2031 case IncRefMsg:
2032 os << "In GC mode the 'retain' message has no effect.";
2033 continue;
2034 case DecRefMsg:
2035 os << "In GC mode the 'release' message has no effect.";
2036 continue;
2037 }
2038 }
Mike Stump1eb44332009-09-09 15:08:12 +00002039 } while (0);
2040
Ted Kremenekc887d132009-04-29 18:50:19 +00002041 if (os.str().empty())
2042 return 0; // We have nothing to say!
Ted Kremenek2033a952009-05-13 07:12:33 +00002043
Jordy Rosef53e8c72011-08-23 19:43:16 +00002044 const Stmt *S = cast<StmtPoint>(N->getLocation()).getStmt();
Anna Zaks220ac8c2011-09-15 01:08:34 +00002045 PathDiagnosticLocation Pos(S, BRC.getSourceManager(),
2046 N->getLocationContext());
Ted Kremenek9c378f72011-08-12 23:37:29 +00002047 PathDiagnosticPiece *P = new PathDiagnosticEventPiece(Pos, os.str());
Mike Stump1eb44332009-09-09 15:08:12 +00002048
Ted Kremenekc887d132009-04-29 18:50:19 +00002049 // Add the range by scanning the children of the statement for any bindings
2050 // to Sym.
Mike Stump1eb44332009-09-09 15:08:12 +00002051 for (Stmt::const_child_iterator I = S->child_begin(), E = S->child_end();
Ted Kremenek5f85e172009-07-22 22:35:28 +00002052 I!=E; ++I)
Ted Kremenek9c378f72011-08-12 23:37:29 +00002053 if (const Expr *Exp = dyn_cast_or_null<Expr>(*I))
Ted Kremenek13976632010-02-08 16:18:51 +00002054 if (CurrSt->getSValAsScalarOrLoc(Exp).getAsLocSymbol() == Sym) {
Ted Kremenekc887d132009-04-29 18:50:19 +00002055 P->addRange(Exp->getSourceRange());
2056 break;
2057 }
Mike Stump1eb44332009-09-09 15:08:12 +00002058
Ted Kremenekc887d132009-04-29 18:50:19 +00002059 return P;
2060}
2061
2062namespace {
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +00002063 class FindUniqueBinding :
Ted Kremenekc887d132009-04-29 18:50:19 +00002064 public StoreManager::BindingsHandler {
2065 SymbolRef Sym;
2066 const MemRegion* Binding;
2067 bool First;
Mike Stump1eb44332009-09-09 15:08:12 +00002068
Ted Kremenekc887d132009-04-29 18:50:19 +00002069 public:
2070 FindUniqueBinding(SymbolRef sym) : Sym(sym), Binding(0), First(true) {}
Mike Stump1eb44332009-09-09 15:08:12 +00002071
Ted Kremenekc887d132009-04-29 18:50:19 +00002072 bool HandleBinding(StoreManager& SMgr, Store store, const MemRegion* R,
2073 SVal val) {
Mike Stump1eb44332009-09-09 15:08:12 +00002074
2075 SymbolRef SymV = val.getAsSymbol();
Ted Kremenekc887d132009-04-29 18:50:19 +00002076 if (!SymV || SymV != Sym)
2077 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00002078
Ted Kremenekc887d132009-04-29 18:50:19 +00002079 if (Binding) {
2080 First = false;
2081 return false;
2082 }
2083 else
2084 Binding = R;
Mike Stump1eb44332009-09-09 15:08:12 +00002085
2086 return true;
Ted Kremenekc887d132009-04-29 18:50:19 +00002087 }
Mike Stump1eb44332009-09-09 15:08:12 +00002088
Ted Kremenekc887d132009-04-29 18:50:19 +00002089 operator bool() { return First && Binding; }
2090 const MemRegion* getRegion() { return Binding; }
Mike Stump1eb44332009-09-09 15:08:12 +00002091 };
Ted Kremenekc887d132009-04-29 18:50:19 +00002092}
2093
Zhongxing Xuc5619d92009-08-06 01:32:16 +00002094static std::pair<const ExplodedNode*,const MemRegion*>
Ted Kremenek18c66fd2011-08-15 22:09:50 +00002095GetAllocationSite(ProgramStateManager& StateMgr, const ExplodedNode *N,
Ted Kremenekc887d132009-04-29 18:50:19 +00002096 SymbolRef Sym) {
Mike Stump1eb44332009-09-09 15:08:12 +00002097
Ted Kremenekc887d132009-04-29 18:50:19 +00002098 // Find both first node that referred to the tracked symbol and the
2099 // memory location that value was store to.
Ted Kremenek9c378f72011-08-12 23:37:29 +00002100 const ExplodedNode *Last = N;
Mike Stump1eb44332009-09-09 15:08:12 +00002101 const MemRegion* FirstBinding = 0;
2102
Ted Kremenekc887d132009-04-29 18:50:19 +00002103 while (N) {
Ted Kremenek18c66fd2011-08-15 22:09:50 +00002104 const ProgramState *St = N->getState();
Ted Kremenekc887d132009-04-29 18:50:19 +00002105 RefBindings B = St->get<RefBindings>();
Mike Stump1eb44332009-09-09 15:08:12 +00002106
Ted Kremenekc887d132009-04-29 18:50:19 +00002107 if (!B.lookup(Sym))
2108 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002109
Ted Kremenekc887d132009-04-29 18:50:19 +00002110 FindUniqueBinding FB(Sym);
Mike Stump1eb44332009-09-09 15:08:12 +00002111 StateMgr.iterBindings(St, FB);
2112 if (FB) FirstBinding = FB.getRegion();
2113
Ted Kremenekc887d132009-04-29 18:50:19 +00002114 Last = N;
Mike Stump1eb44332009-09-09 15:08:12 +00002115 N = N->pred_empty() ? NULL : *(N->pred_begin());
Ted Kremenekc887d132009-04-29 18:50:19 +00002116 }
Mike Stump1eb44332009-09-09 15:08:12 +00002117
Ted Kremenekc887d132009-04-29 18:50:19 +00002118 return std::make_pair(Last, FirstBinding);
2119}
2120
2121PathDiagnosticPiece*
Anna Zaks23f395e2011-08-20 01:27:22 +00002122CFRefReportVisitor::getEndPath(BugReporterContext &BRC,
2123 const ExplodedNode *EndN,
2124 BugReport &BR) {
Ted Kremenek8966bc12009-05-06 21:39:49 +00002125 // Tell the BugReporterContext to report cases when the tracked symbol is
Ted Kremenekc887d132009-04-29 18:50:19 +00002126 // assigned to different variables, etc.
Ted Kremenek8966bc12009-05-06 21:39:49 +00002127 BRC.addNotableSymbol(Sym);
Anna Zaks23f395e2011-08-20 01:27:22 +00002128 return BugReporterVisitor::getDefaultEndPath(BRC, EndN, BR);
Ted Kremenekc887d132009-04-29 18:50:19 +00002129}
2130
2131PathDiagnosticPiece*
Anna Zaks23f395e2011-08-20 01:27:22 +00002132CFRefLeakReportVisitor::getEndPath(BugReporterContext &BRC,
2133 const ExplodedNode *EndN,
2134 BugReport &BR) {
Mike Stump1eb44332009-09-09 15:08:12 +00002135
Ted Kremenek8966bc12009-05-06 21:39:49 +00002136 // Tell the BugReporterContext to report cases when the tracked symbol is
Ted Kremenekc887d132009-04-29 18:50:19 +00002137 // assigned to different variables, etc.
Ted Kremenek8966bc12009-05-06 21:39:49 +00002138 BRC.addNotableSymbol(Sym);
Mike Stump1eb44332009-09-09 15:08:12 +00002139
Ted Kremenekc887d132009-04-29 18:50:19 +00002140 // We are reporting a leak. Walk up the graph to get to the first node where
2141 // the symbol appeared, and also get the first VarDecl that tracked object
2142 // is stored to.
Ted Kremenek9c378f72011-08-12 23:37:29 +00002143 const ExplodedNode *AllocNode = 0;
Ted Kremenekc887d132009-04-29 18:50:19 +00002144 const MemRegion* FirstBinding = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00002145
Ted Kremenekc887d132009-04-29 18:50:19 +00002146 llvm::tie(AllocNode, FirstBinding) =
Ted Kremenekf04dced2009-05-08 23:32:51 +00002147 GetAllocationSite(BRC.getStateManager(), EndN, Sym);
Mike Stump1eb44332009-09-09 15:08:12 +00002148
Anna Zaks4fdf97b2011-09-15 18:56:07 +00002149 SourceManager& SM = BRC.getSourceManager();
Mike Stump1eb44332009-09-09 15:08:12 +00002150
Ted Kremenekc887d132009-04-29 18:50:19 +00002151 // Compute an actual location for the leak. Sometimes a leak doesn't
2152 // occur at an actual statement (e.g., transition between blocks; end
2153 // of function) so we need to walk the graph and compute a real location.
Ted Kremenek9c378f72011-08-12 23:37:29 +00002154 const ExplodedNode *LeakN = EndN;
Anna Zaks4fdf97b2011-09-15 18:56:07 +00002155 PathDiagnosticLocation L = PathDiagnosticLocation::createEndOfPath(LeakN, SM);
Mike Stump1eb44332009-09-09 15:08:12 +00002156
Ted Kremenekc887d132009-04-29 18:50:19 +00002157 std::string sbuf;
2158 llvm::raw_string_ostream os(sbuf);
Mike Stump1eb44332009-09-09 15:08:12 +00002159
Ted Kremenekf1365462011-05-26 18:45:44 +00002160 os << "Object leaked: ";
Mike Stump1eb44332009-09-09 15:08:12 +00002161
Ted Kremenekf1365462011-05-26 18:45:44 +00002162 if (FirstBinding) {
2163 os << "object allocated and stored into '"
2164 << FirstBinding->getString() << '\'';
2165 }
2166 else
2167 os << "allocated object";
Mike Stump1eb44332009-09-09 15:08:12 +00002168
Ted Kremenekc887d132009-04-29 18:50:19 +00002169 // Get the retain count.
2170 const RefVal* RV = EndN->getState()->get<RefBindings>(Sym);
Mike Stump1eb44332009-09-09 15:08:12 +00002171
Ted Kremenekc887d132009-04-29 18:50:19 +00002172 if (RV->getKind() == RefVal::ErrorLeakReturned) {
2173 // FIXME: Per comments in rdar://6320065, "create" only applies to CF
Jordy Rose5b5402b2011-07-15 22:17:54 +00002174 // objects. Only "copy", "alloc", "retain" and "new" transfer ownership
Ted Kremenekc887d132009-04-29 18:50:19 +00002175 // to the caller for NS objects.
Ted Kremenekd368d712011-05-25 06:19:45 +00002176 const Decl *D = &EndN->getCodeDecl();
2177 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) {
2178 os << " is returned from a method whose name ('"
2179 << MD->getSelector().getAsString()
2180 << "') does not start with 'copy', 'mutableCopy', 'alloc' or 'new'."
Jordy Rose5b5402b2011-07-15 22:17:54 +00002181 " This violates the naming convention rules"
Ted Kremenekf1365462011-05-26 18:45:44 +00002182 " given in the Memory Management Guide for Cocoa";
Ted Kremenekd368d712011-05-25 06:19:45 +00002183 }
2184 else {
2185 const FunctionDecl *FD = cast<FunctionDecl>(D);
2186 os << " is return from a function whose name ('"
2187 << FD->getNameAsString()
2188 << "') does not contain 'Copy' or 'Create'. This violates the naming"
Jordy Rose5b5402b2011-07-15 22:17:54 +00002189 " convention rules given the Memory Management Guide for Core"
Ted Kremenekf1365462011-05-26 18:45:44 +00002190 " Foundation";
Ted Kremenekd368d712011-05-25 06:19:45 +00002191 }
Ted Kremenekc887d132009-04-29 18:50:19 +00002192 }
Ted Kremeneke8720ce2009-05-10 06:25:57 +00002193 else if (RV->getKind() == RefVal::ErrorGCLeakReturned) {
Ted Kremenek9c378f72011-08-12 23:37:29 +00002194 ObjCMethodDecl &MD = cast<ObjCMethodDecl>(EndN->getCodeDecl());
Ted Kremeneke8720ce2009-05-10 06:25:57 +00002195 os << " and returned from method '" << MD.getSelector().getAsString()
Ted Kremenek82f2be52009-05-10 16:52:15 +00002196 << "' is potentially leaked when using garbage collection. Callers "
2197 "of this method do not expect a returned object with a +1 retain "
2198 "count since they expect the object to be managed by the garbage "
2199 "collector";
Ted Kremeneke8720ce2009-05-10 06:25:57 +00002200 }
Ted Kremenekc887d132009-04-29 18:50:19 +00002201 else
Ted Kremenekabf517c2010-10-15 22:50:23 +00002202 os << " is not referenced later in this execution path and has a retain "
Ted Kremenekf1365462011-05-26 18:45:44 +00002203 "count of +" << RV->getCount();
Mike Stump1eb44332009-09-09 15:08:12 +00002204
Ted Kremenekc887d132009-04-29 18:50:19 +00002205 return new PathDiagnosticEventPiece(L, os.str());
2206}
2207
Jordy Rose20589562011-08-24 22:39:09 +00002208CFRefLeakReport::CFRefLeakReport(CFRefBug &D, const LangOptions &LOpts,
2209 bool GCEnabled, const SummaryLogTy &Log,
2210 ExplodedNode *n, SymbolRef sym,
2211 ExprEngine &Eng)
2212: CFRefReport(D, LOpts, GCEnabled, Log, n, sym, false) {
Mike Stump1eb44332009-09-09 15:08:12 +00002213
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00002214 // Most bug reports are cached at the location where they occurred.
Ted Kremenekc887d132009-04-29 18:50:19 +00002215 // With leaks, we want to unique them by the location where they were
2216 // allocated, and only report a single path. To do this, we need to find
2217 // the allocation site of a piece of tracked memory, which we do via a
2218 // call to GetAllocationSite. This will walk the ExplodedGraph backwards.
2219 // Note that this is *not* the trimmed graph; we are guaranteed, however,
2220 // that all ancestor nodes that represent the allocation site have the
2221 // same SourceLocation.
Ted Kremenek9c378f72011-08-12 23:37:29 +00002222 const ExplodedNode *AllocNode = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00002223
Anna Zaks590dd8e2011-09-20 21:38:35 +00002224 const SourceManager& SMgr = Eng.getContext().getSourceManager();
2225
Ted Kremenekc887d132009-04-29 18:50:19 +00002226 llvm::tie(AllocNode, AllocBinding) = // Set AllocBinding.
Anna Zaksedf4dae2011-08-22 18:54:07 +00002227 GetAllocationSite(Eng.getStateManager(), getErrorNode(), sym);
Mike Stump1eb44332009-09-09 15:08:12 +00002228
Ted Kremenekc887d132009-04-29 18:50:19 +00002229 // Get the SourceLocation for the allocation site.
2230 ProgramPoint P = AllocNode->getLocation();
Anna Zaks590dd8e2011-09-20 21:38:35 +00002231 const Stmt *AllocStmt = cast<PostStmt>(P).getStmt();
2232 Location = PathDiagnosticLocation::createBegin(AllocStmt, SMgr,
2233 n->getLocationContext());
Ted Kremenekc887d132009-04-29 18:50:19 +00002234 // Fill in the description of the bug.
2235 Description.clear();
2236 llvm::raw_string_ostream os(Description);
Anna Zaks590dd8e2011-09-20 21:38:35 +00002237 unsigned AllocLine = SMgr.getExpansionLineNumber(AllocStmt->getLocStart());
Ted Kremenekdd924e22009-05-02 19:05:19 +00002238 os << "Potential leak ";
Jordy Rose20589562011-08-24 22:39:09 +00002239 if (GCEnabled)
Ted Kremenekdd924e22009-05-02 19:05:19 +00002240 os << "(when using garbage collection) ";
Ted Kremenekdd924e22009-05-02 19:05:19 +00002241 os << "of an object allocated on line " << AllocLine;
Mike Stump1eb44332009-09-09 15:08:12 +00002242
Ted Kremenekc887d132009-04-29 18:50:19 +00002243 // FIXME: AllocBinding doesn't get populated for RegionStore yet.
2244 if (AllocBinding)
2245 os << " and stored into '" << AllocBinding->getString() << '\'';
Anna Zaksdc757b02011-08-19 23:21:56 +00002246
Jordy Rose20589562011-08-24 22:39:09 +00002247 addVisitor(new CFRefLeakReportVisitor(sym, GCEnabled, Log));
Ted Kremenekc887d132009-04-29 18:50:19 +00002248}
2249
2250//===----------------------------------------------------------------------===//
2251// Main checker logic.
2252//===----------------------------------------------------------------------===//
2253
Ted Kremenekd593eb92009-11-25 22:17:44 +00002254namespace {
Jordy Rose910c4052011-09-02 06:44:22 +00002255class RetainCountChecker
Jordy Rose9c083b72011-08-24 18:56:32 +00002256 : public Checker< check::Bind,
Jordy Rose38f17d62011-08-23 19:01:07 +00002257 check::DeadSymbols,
Jordy Rose9c083b72011-08-24 18:56:32 +00002258 check::EndAnalysis,
Jordy Rose38f17d62011-08-23 19:01:07 +00002259 check::EndPath,
Jordy Rose67044292011-08-17 21:27:39 +00002260 check::PostStmt<BlockExpr>,
John McCallf85e1932011-06-15 23:02:42 +00002261 check::PostStmt<CastExpr>,
Jordy Rose294396b2011-08-22 23:48:23 +00002262 check::PostStmt<CallExpr>,
Jordy Rose537716a2011-08-27 22:51:26 +00002263 check::PostStmt<CXXConstructExpr>,
Jordy Rose294396b2011-08-22 23:48:23 +00002264 check::PostObjCMessage,
Jordy Rosef53e8c72011-08-23 19:43:16 +00002265 check::PreStmt<ReturnStmt>,
Jordy Rose67044292011-08-17 21:27:39 +00002266 check::RegionChanges,
Jordy Rose76c506f2011-08-21 21:58:18 +00002267 eval::Assume,
2268 eval::Call > {
Jordy Rosed6334e12011-08-25 00:34:03 +00002269 mutable llvm::OwningPtr<CFRefBug> useAfterRelease, releaseNotOwned;
2270 mutable llvm::OwningPtr<CFRefBug> deallocGC, deallocNotOwned;
2271 mutable llvm::OwningPtr<CFRefBug> overAutorelease, returnNotOwnedForOwned;
Jordy Rosedb92bb62011-08-25 01:14:38 +00002272 mutable llvm::OwningPtr<CFRefBug> leakWithinFunction, leakAtReturn;
2273 mutable llvm::OwningPtr<CFRefBug> leakWithinFunctionGC, leakAtReturnGC;
Jordy Rose38f17d62011-08-23 19:01:07 +00002274
2275 typedef llvm::DenseMap<SymbolRef, const SimpleProgramPointTag *> SymbolTagMap;
2276
2277 // This map is only used to ensure proper deletion of any allocated tags.
2278 mutable SymbolTagMap DeadSymbolTags;
2279
Jordy Roseb6cfc092011-08-25 00:10:37 +00002280 mutable llvm::OwningPtr<RetainSummaryManager> Summaries;
2281 mutable llvm::OwningPtr<RetainSummaryManager> SummariesGC;
2282
Jordy Rosee0a5d322011-08-23 20:27:16 +00002283 mutable ARCounts::Factory ARCountFactory;
2284
Jordy Rose9c083b72011-08-24 18:56:32 +00002285 mutable SummaryLogTy SummaryLog;
2286 mutable bool ShouldResetSummaryLog;
2287
Jordy Rose2f9a66d2011-08-20 21:17:59 +00002288public:
Jordy Rose910c4052011-09-02 06:44:22 +00002289 RetainCountChecker() : ShouldResetSummaryLog(false) {}
Jordy Rose38f17d62011-08-23 19:01:07 +00002290
Jordy Rose910c4052011-09-02 06:44:22 +00002291 virtual ~RetainCountChecker() {
Jordy Rose38f17d62011-08-23 19:01:07 +00002292 DeleteContainerSeconds(DeadSymbolTags);
2293 }
2294
Jordy Rose9c083b72011-08-24 18:56:32 +00002295 void checkEndAnalysis(ExplodedGraph &G, BugReporter &BR,
2296 ExprEngine &Eng) const {
2297 // FIXME: This is a hack to make sure the summary log gets cleared between
2298 // analyses of different code bodies.
2299 //
2300 // Why is this necessary? Because a checker's lifetime is tied to a
2301 // translation unit, but an ExplodedGraph's lifetime is just a code body.
2302 // Once in a blue moon, a new ExplodedNode will have the same address as an
2303 // old one with an associated summary, and the bug report visitor gets very
2304 // confused. (To make things worse, the summary lifetime is currently also
2305 // tied to a code body, so we get a crash instead of incorrect results.)
Jordy Rose1ab51c72011-08-24 09:27:24 +00002306 //
2307 // Why is this a bad solution? Because if the lifetime of the ExplodedGraph
2308 // changes, things will start going wrong again. Really the lifetime of this
2309 // log needs to be tied to either the specific nodes in it or the entire
2310 // ExplodedGraph, not to a specific part of the code being analyzed.
2311 //
Jordy Rose9c083b72011-08-24 18:56:32 +00002312 // (Also, having stateful local data means that the same checker can't be
2313 // used from multiple threads, but a lot of checkers have incorrect
2314 // assumptions about that anyway. So that wasn't a priority at the time of
2315 // this fix.)
Jordy Rose1ab51c72011-08-24 09:27:24 +00002316 //
Jordy Rose9c083b72011-08-24 18:56:32 +00002317 // This happens at the end of analysis, but bug reports are emitted /after/
2318 // this point. So we can't just clear the summary log now. Instead, we mark
2319 // that the next time we access the summary log, it should be cleared.
2320
2321 // If we never reset the summary log during /this/ code body analysis,
2322 // there were no new summaries. There might still have been summaries from
2323 // the /last/ analysis, so clear them out to make sure the bug report
2324 // visitors don't get confused.
2325 if (ShouldResetSummaryLog)
2326 SummaryLog.clear();
2327
2328 ShouldResetSummaryLog = !SummaryLog.empty();
Jordy Rose1ab51c72011-08-24 09:27:24 +00002329 }
2330
Jordy Rose17a38e22011-09-02 05:55:19 +00002331 CFRefBug *getLeakWithinFunctionBug(const LangOptions &LOpts,
2332 bool GCEnabled) const {
2333 if (GCEnabled) {
Jordy Rosedb92bb62011-08-25 01:14:38 +00002334 if (!leakWithinFunctionGC)
2335 leakWithinFunctionGC.reset(new LeakWithinFunction("Leak of object when "
2336 "using garbage "
2337 "collection"));
Jordy Rose17a38e22011-09-02 05:55:19 +00002338 return leakWithinFunctionGC.get();
Jordy Rosedb92bb62011-08-25 01:14:38 +00002339 } else {
2340 if (!leakWithinFunction) {
Douglas Gregore289d812011-09-13 17:21:33 +00002341 if (LOpts.getGC() == LangOptions::HybridGC) {
Jordy Rosedb92bb62011-08-25 01:14:38 +00002342 leakWithinFunction.reset(new LeakWithinFunction("Leak of object when "
2343 "not using garbage "
2344 "collection (GC) in "
2345 "dual GC/non-GC "
2346 "code"));
2347 } else {
2348 leakWithinFunction.reset(new LeakWithinFunction("Leak"));
2349 }
2350 }
Jordy Rose17a38e22011-09-02 05:55:19 +00002351 return leakWithinFunction.get();
Jordy Rosedb92bb62011-08-25 01:14:38 +00002352 }
2353 }
2354
Jordy Rose17a38e22011-09-02 05:55:19 +00002355 CFRefBug *getLeakAtReturnBug(const LangOptions &LOpts, bool GCEnabled) const {
2356 if (GCEnabled) {
Jordy Rosedb92bb62011-08-25 01:14:38 +00002357 if (!leakAtReturnGC)
2358 leakAtReturnGC.reset(new LeakAtReturn("Leak of returned object when "
2359 "using garbage collection"));
Jordy Rose17a38e22011-09-02 05:55:19 +00002360 return leakAtReturnGC.get();
Jordy Rosedb92bb62011-08-25 01:14:38 +00002361 } else {
2362 if (!leakAtReturn) {
Douglas Gregore289d812011-09-13 17:21:33 +00002363 if (LOpts.getGC() == LangOptions::HybridGC) {
Jordy Rosedb92bb62011-08-25 01:14:38 +00002364 leakAtReturn.reset(new LeakAtReturn("Leak of returned object when "
2365 "not using garbage collection "
2366 "(GC) in dual GC/non-GC code"));
2367 } else {
2368 leakAtReturn.reset(new LeakAtReturn("Leak of returned object"));
2369 }
2370 }
Jordy Rose17a38e22011-09-02 05:55:19 +00002371 return leakAtReturn.get();
Jordy Rosedb92bb62011-08-25 01:14:38 +00002372 }
2373 }
2374
Jordy Rose17a38e22011-09-02 05:55:19 +00002375 RetainSummaryManager &getSummaryManager(ASTContext &Ctx,
2376 bool GCEnabled) const {
2377 // FIXME: We don't support ARC being turned on and off during one analysis.
2378 // (nor, for that matter, do we support changing ASTContexts)
2379 bool ARCEnabled = (bool)Ctx.getLangOptions().ObjCAutoRefCount;
2380 if (GCEnabled) {
2381 if (!SummariesGC)
Jordy Roseb6cfc092011-08-25 00:10:37 +00002382 SummariesGC.reset(new RetainSummaryManager(Ctx, true, ARCEnabled));
Jordy Rose17a38e22011-09-02 05:55:19 +00002383 else
2384 assert(SummariesGC->isARCEnabled() == ARCEnabled);
Jordy Roseb6cfc092011-08-25 00:10:37 +00002385 return *SummariesGC;
2386 } else {
Jordy Rose17a38e22011-09-02 05:55:19 +00002387 if (!Summaries)
Jordy Roseb6cfc092011-08-25 00:10:37 +00002388 Summaries.reset(new RetainSummaryManager(Ctx, false, ARCEnabled));
Jordy Rose17a38e22011-09-02 05:55:19 +00002389 else
2390 assert(Summaries->isARCEnabled() == ARCEnabled);
Jordy Roseb6cfc092011-08-25 00:10:37 +00002391 return *Summaries;
2392 }
2393 }
2394
Jordy Rose17a38e22011-09-02 05:55:19 +00002395 RetainSummaryManager &getSummaryManager(CheckerContext &C) const {
2396 return getSummaryManager(C.getASTContext(), C.isObjCGCEnabled());
2397 }
2398
Jordy Rosedbd658e2011-08-28 19:11:56 +00002399 void printState(raw_ostream &Out, const ProgramState *State,
2400 const char *NL, const char *Sep) const;
2401
Jordy Roseab027fd2011-08-20 21:16:58 +00002402 void checkBind(SVal loc, SVal val, CheckerContext &C) const;
2403 void checkPostStmt(const BlockExpr *BE, CheckerContext &C) const;
2404 void checkPostStmt(const CastExpr *CE, CheckerContext &C) const;
John McCallf85e1932011-06-15 23:02:42 +00002405
Jordy Rose294396b2011-08-22 23:48:23 +00002406 void checkPostStmt(const CallExpr *CE, CheckerContext &C) const;
Jordy Rose537716a2011-08-27 22:51:26 +00002407 void checkPostStmt(const CXXConstructExpr *CE, CheckerContext &C) const;
Jordy Rose294396b2011-08-22 23:48:23 +00002408 void checkPostObjCMessage(const ObjCMessage &Msg, CheckerContext &C) const;
2409 void checkSummary(const RetainSummary &Summ, const CallOrObjCMessage &Call,
Jordy Rosee38dd952011-08-28 05:16:28 +00002410 CheckerContext &C) const;
Jordy Rose294396b2011-08-22 23:48:23 +00002411
Jordy Rose76c506f2011-08-21 21:58:18 +00002412 bool evalCall(const CallExpr *CE, CheckerContext &C) const;
2413
Jordy Roseab027fd2011-08-20 21:16:58 +00002414 const ProgramState *evalAssume(const ProgramState *state, SVal Cond,
2415 bool Assumption) const;
Jordy Rose67044292011-08-17 21:27:39 +00002416
Jordy Rose537716a2011-08-27 22:51:26 +00002417 const ProgramState *
2418 checkRegionChanges(const ProgramState *state,
2419 const StoreManager::InvalidatedSymbols *invalidated,
2420 ArrayRef<const MemRegion *> ExplicitRegions,
2421 ArrayRef<const MemRegion *> Regions) const;
Jordy Roseab027fd2011-08-20 21:16:58 +00002422
2423 bool wantsRegionChangeUpdate(const ProgramState *state) const {
Jordy Rose2f9a66d2011-08-20 21:17:59 +00002424 return true;
Jordy Roseab027fd2011-08-20 21:16:58 +00002425 }
Jordy Rose294396b2011-08-22 23:48:23 +00002426
Jordy Rosef53e8c72011-08-23 19:43:16 +00002427 void checkPreStmt(const ReturnStmt *S, CheckerContext &C) const;
2428 void checkReturnWithRetEffect(const ReturnStmt *S, CheckerContext &C,
2429 ExplodedNode *Pred, RetEffect RE, RefVal X,
2430 SymbolRef Sym, const ProgramState *state) const;
2431
Jordy Rose38f17d62011-08-23 19:01:07 +00002432 void checkDeadSymbols(SymbolReaper &SymReaper, CheckerContext &C) const;
2433 void checkEndPath(EndOfFunctionNodeBuilder &Builder, ExprEngine &Eng) const;
2434
Jordy Rosee0a5d322011-08-23 20:27:16 +00002435 const ProgramState *updateSymbol(const ProgramState *state, SymbolRef sym,
Jordy Rose17a38e22011-09-02 05:55:19 +00002436 RefVal V, ArgEffect E, RefVal::Kind &hasErr,
2437 CheckerContext &C) const;
Jordy Rosee0a5d322011-08-23 20:27:16 +00002438
Jordy Rose294396b2011-08-22 23:48:23 +00002439 void processNonLeakError(const ProgramState *St, SourceRange ErrorRange,
2440 RefVal::Kind ErrorKind, SymbolRef Sym,
2441 CheckerContext &C) const;
2442
Jordy Rose38f17d62011-08-23 19:01:07 +00002443 const ProgramPointTag *getDeadSymbolTag(SymbolRef sym) const;
2444
2445 const ProgramState *handleSymbolDeath(const ProgramState *state,
2446 SymbolRef sid, RefVal V,
2447 SmallVectorImpl<SymbolRef> &Leaked) const;
2448
Jordy Rose8d228632011-08-23 20:07:14 +00002449 std::pair<ExplodedNode *, const ProgramState *>
2450 handleAutoreleaseCounts(const ProgramState *state,
2451 GenericNodeBuilderRefCount Bd, ExplodedNode *Pred,
2452 ExprEngine &Eng, SymbolRef Sym, RefVal V) const;
2453
Jordy Rose38f17d62011-08-23 19:01:07 +00002454 ExplodedNode *processLeaks(const ProgramState *state,
2455 SmallVectorImpl<SymbolRef> &Leaked,
2456 GenericNodeBuilderRefCount &Builder,
2457 ExprEngine &Eng,
2458 ExplodedNode *Pred = 0) const;
Ted Kremenekd593eb92009-11-25 22:17:44 +00002459};
2460} // end anonymous namespace
2461
Jordy Rose67044292011-08-17 21:27:39 +00002462namespace {
2463class StopTrackingCallback : public SymbolVisitor {
2464 const ProgramState *state;
2465public:
2466 StopTrackingCallback(const ProgramState *st) : state(st) {}
2467 const ProgramState *getState() const { return state; }
2468
2469 bool VisitSymbol(SymbolRef sym) {
2470 state = state->remove<RefBindings>(sym);
2471 return true;
2472 }
2473};
2474} // end anonymous namespace
2475
Jordy Rose910c4052011-09-02 06:44:22 +00002476//===----------------------------------------------------------------------===//
2477// Handle statements that may have an effect on refcounts.
2478//===----------------------------------------------------------------------===//
Jordy Rose67044292011-08-17 21:27:39 +00002479
Jordy Rose910c4052011-09-02 06:44:22 +00002480void RetainCountChecker::checkPostStmt(const BlockExpr *BE,
2481 CheckerContext &C) const {
Jordy Rose67044292011-08-17 21:27:39 +00002482
Jordy Rose910c4052011-09-02 06:44:22 +00002483 // Scan the BlockDecRefExprs for any object the retain count checker
Ted Kremenekdcee3ce2010-07-01 20:16:50 +00002484 // may be tracking.
John McCall469a1eb2011-02-02 13:00:07 +00002485 if (!BE->getBlockDecl()->hasCaptures())
Ted Kremenek38cc6bc2009-11-26 02:38:19 +00002486 return;
Ted Kremenekdcee3ce2010-07-01 20:16:50 +00002487
Ted Kremenek18c66fd2011-08-15 22:09:50 +00002488 const ProgramState *state = C.getState();
Ted Kremenek38cc6bc2009-11-26 02:38:19 +00002489 const BlockDataRegion *R =
Ted Kremenek13976632010-02-08 16:18:51 +00002490 cast<BlockDataRegion>(state->getSVal(BE).getAsRegion());
Ted Kremenekdcee3ce2010-07-01 20:16:50 +00002491
Ted Kremenek38cc6bc2009-11-26 02:38:19 +00002492 BlockDataRegion::referenced_vars_iterator I = R->referenced_vars_begin(),
2493 E = R->referenced_vars_end();
Ted Kremenekdcee3ce2010-07-01 20:16:50 +00002494
Ted Kremenek38cc6bc2009-11-26 02:38:19 +00002495 if (I == E)
2496 return;
Ted Kremenekdcee3ce2010-07-01 20:16:50 +00002497
Ted Kremenek67d12872009-12-07 22:05:27 +00002498 // FIXME: For now we invalidate the tracking of all symbols passed to blocks
2499 // via captured variables, even though captured variables result in a copy
2500 // and in implicit increment/decrement of a retain count.
Chris Lattner5f9e2722011-07-23 10:55:15 +00002501 SmallVector<const MemRegion*, 10> Regions;
Ted Kremenek67d12872009-12-07 22:05:27 +00002502 const LocationContext *LC = C.getPredecessor()->getLocationContext();
Ted Kremenekc8413fd2010-12-02 07:49:45 +00002503 MemRegionManager &MemMgr = C.getSValBuilder().getRegionManager();
Ted Kremenekdcee3ce2010-07-01 20:16:50 +00002504
Ted Kremenek67d12872009-12-07 22:05:27 +00002505 for ( ; I != E; ++I) {
2506 const VarRegion *VR = *I;
2507 if (VR->getSuperRegion() == R) {
2508 VR = MemMgr.getVarRegion(VR->getDecl(), LC);
2509 }
2510 Regions.push_back(VR);
2511 }
Ted Kremenekdcee3ce2010-07-01 20:16:50 +00002512
Ted Kremenek67d12872009-12-07 22:05:27 +00002513 state =
2514 state->scanReachableSymbols<StopTrackingCallback>(Regions.data(),
2515 Regions.data() + Regions.size()).getState();
Ted Kremenek38cc6bc2009-11-26 02:38:19 +00002516 C.addTransition(state);
2517}
2518
Jordy Rose910c4052011-09-02 06:44:22 +00002519void RetainCountChecker::checkPostStmt(const CastExpr *CE,
2520 CheckerContext &C) const {
John McCallf85e1932011-06-15 23:02:42 +00002521 const ObjCBridgedCastExpr *BE = dyn_cast<ObjCBridgedCastExpr>(CE);
2522 if (!BE)
2523 return;
2524
John McCall71c482c2011-06-17 06:50:50 +00002525 ArgEffect AE = IncRef;
John McCallf85e1932011-06-15 23:02:42 +00002526
2527 switch (BE->getBridgeKind()) {
2528 case clang::OBC_Bridge:
2529 // Do nothing.
2530 return;
2531 case clang::OBC_BridgeRetained:
2532 AE = IncRef;
2533 break;
2534 case clang::OBC_BridgeTransfer:
2535 AE = DecRefBridgedTransfered;
2536 break;
2537 }
2538
Ted Kremenek18c66fd2011-08-15 22:09:50 +00002539 const ProgramState *state = C.getState();
John McCallf85e1932011-06-15 23:02:42 +00002540 SymbolRef Sym = state->getSVal(CE).getAsLocSymbol();
2541 if (!Sym)
2542 return;
2543 const RefVal* T = state->get<RefBindings>(Sym);
2544 if (!T)
2545 return;
2546
John McCallf85e1932011-06-15 23:02:42 +00002547 RefVal::Kind hasErr = (RefVal::Kind) 0;
Jordy Rose17a38e22011-09-02 05:55:19 +00002548 state = updateSymbol(state, Sym, *T, AE, hasErr, C);
John McCallf85e1932011-06-15 23:02:42 +00002549
2550 if (hasErr) {
Jordy Rosee0a5d322011-08-23 20:27:16 +00002551 // FIXME: If we get an error during a bridge cast, should we report it?
2552 // Should we assert that there is no error?
John McCallf85e1932011-06-15 23:02:42 +00002553 return;
2554 }
2555
2556 C.generateNode(state);
2557}
2558
Jordy Rose910c4052011-09-02 06:44:22 +00002559void RetainCountChecker::checkPostStmt(const CallExpr *CE,
2560 CheckerContext &C) const {
Jordy Rose294396b2011-08-22 23:48:23 +00002561 // Get the callee.
2562 const ProgramState *state = C.getState();
2563 const Expr *Callee = CE->getCallee();
2564 SVal L = state->getSVal(Callee);
2565
Jordy Rose17a38e22011-09-02 05:55:19 +00002566 RetainSummaryManager &Summaries = getSummaryManager(C);
Jordy Rose294396b2011-08-22 23:48:23 +00002567 RetainSummary *Summ = 0;
2568
2569 // FIXME: Better support for blocks. For now we stop tracking anything
2570 // that is passed to blocks.
2571 // FIXME: Need to handle variables that are "captured" by the block.
2572 if (dyn_cast_or_null<BlockDataRegion>(L.getAsRegion())) {
2573 Summ = Summaries.getPersistentStopSummary();
2574 } else if (const FunctionDecl *FD = L.getAsFunctionDecl()) {
2575 Summ = Summaries.getSummary(FD);
2576 } else if (const CXXMemberCallExpr *me = dyn_cast<CXXMemberCallExpr>(CE)) {
2577 if (const CXXMethodDecl *MD = me->getMethodDecl())
2578 Summ = Summaries.getSummary(MD);
2579 }
2580
2581 // If we didn't get a summary, this function doesn't affect retain counts.
2582 if (!Summ)
2583 return;
2584
Jordy Rosee38dd952011-08-28 05:16:28 +00002585 checkSummary(*Summ, CallOrObjCMessage(CE, state), C);
Jordy Rose294396b2011-08-22 23:48:23 +00002586}
2587
Jordy Rose910c4052011-09-02 06:44:22 +00002588void RetainCountChecker::checkPostStmt(const CXXConstructExpr *CE,
2589 CheckerContext &C) const {
Jordy Rose537716a2011-08-27 22:51:26 +00002590 const CXXConstructorDecl *Ctor = CE->getConstructor();
2591 if (!Ctor)
2592 return;
2593
Jordy Rose17a38e22011-09-02 05:55:19 +00002594 RetainSummaryManager &Summaries = getSummaryManager(C);
Jordy Rose537716a2011-08-27 22:51:26 +00002595 RetainSummary *Summ = Summaries.getSummary(Ctor);
2596
2597 // If we didn't get a summary, this constructor doesn't affect retain counts.
2598 if (!Summ)
2599 return;
2600
2601 const ProgramState *state = C.getState();
Jordy Rosee38dd952011-08-28 05:16:28 +00002602 checkSummary(*Summ, CallOrObjCMessage(CE, state), C);
Jordy Rose537716a2011-08-27 22:51:26 +00002603}
2604
Jordy Rose910c4052011-09-02 06:44:22 +00002605void RetainCountChecker::checkPostObjCMessage(const ObjCMessage &Msg,
2606 CheckerContext &C) const {
Jordy Rose294396b2011-08-22 23:48:23 +00002607 const ProgramState *state = C.getState();
2608 ExplodedNode *Pred = C.getPredecessor();
2609
Jordy Rose17a38e22011-09-02 05:55:19 +00002610 RetainSummaryManager &Summaries = getSummaryManager(C);
Jordy Roseb6cfc092011-08-25 00:10:37 +00002611
Jordy Rose294396b2011-08-22 23:48:23 +00002612 RetainSummary *Summ;
2613 if (Msg.isInstanceMessage()) {
2614 const LocationContext *LC = Pred->getLocationContext();
2615 Summ = Summaries.getInstanceMethodSummary(Msg, state, LC);
2616 } else {
2617 Summ = Summaries.getClassMethodSummary(Msg);
2618 }
2619
2620 // If we didn't get a summary, this message doesn't affect retain counts.
2621 if (!Summ)
2622 return;
2623
Jordy Rosee38dd952011-08-28 05:16:28 +00002624 checkSummary(*Summ, CallOrObjCMessage(Msg, state), C);
Jordy Rose294396b2011-08-22 23:48:23 +00002625}
2626
Jordy Rose910c4052011-09-02 06:44:22 +00002627/// GetReturnType - Used to get the return type of a message expression or
2628/// function call with the intention of affixing that type to a tracked symbol.
2629/// While the the return type can be queried directly from RetEx, when
2630/// invoking class methods we augment to the return type to be that of
2631/// a pointer to the class (as opposed it just being id).
2632// FIXME: We may be able to do this with related result types instead.
2633// This function is probably overestimating.
2634static QualType GetReturnType(const Expr *RetE, ASTContext &Ctx) {
2635 QualType RetTy = RetE->getType();
2636 // If RetE is not a message expression just return its type.
2637 // If RetE is a message expression, return its types if it is something
2638 /// more specific than id.
2639 if (const ObjCMessageExpr *ME = dyn_cast<ObjCMessageExpr>(RetE))
2640 if (const ObjCObjectPointerType *PT = RetTy->getAs<ObjCObjectPointerType>())
2641 if (PT->isObjCQualifiedIdType() || PT->isObjCIdType() ||
2642 PT->isObjCClassType()) {
2643 // At this point we know the return type of the message expression is
2644 // id, id<...>, or Class. If we have an ObjCInterfaceDecl, we know this
2645 // is a call to a class method whose type we can resolve. In such
2646 // cases, promote the return type to XXX* (where XXX is the class).
2647 const ObjCInterfaceDecl *D = ME->getReceiverInterface();
2648 return !D ? RetTy :
2649 Ctx.getObjCObjectPointerType(Ctx.getObjCInterfaceType(D));
2650 }
2651
2652 return RetTy;
2653}
2654
2655void RetainCountChecker::checkSummary(const RetainSummary &Summ,
2656 const CallOrObjCMessage &CallOrMsg,
2657 CheckerContext &C) const {
Jordy Rose294396b2011-08-22 23:48:23 +00002658 const ProgramState *state = C.getState();
2659
2660 // Evaluate the effect of the arguments.
2661 RefVal::Kind hasErr = (RefVal::Kind) 0;
2662 SourceRange ErrorRange;
2663 SymbolRef ErrorSym = 0;
2664
2665 for (unsigned idx = 0, e = CallOrMsg.getNumArgs(); idx != e; ++idx) {
Jordy Rose537716a2011-08-27 22:51:26 +00002666 SVal V = CallOrMsg.getArgSVal(idx);
Jordy Rose294396b2011-08-22 23:48:23 +00002667
2668 if (SymbolRef Sym = V.getAsLocSymbol()) {
2669 if (RefBindings::data_type *T = state->get<RefBindings>(Sym)) {
Jordy Rose17a38e22011-09-02 05:55:19 +00002670 state = updateSymbol(state, Sym, *T, Summ.getArg(idx), hasErr, C);
Jordy Rose294396b2011-08-22 23:48:23 +00002671 if (hasErr) {
2672 ErrorRange = CallOrMsg.getArgSourceRange(idx);
2673 ErrorSym = Sym;
2674 break;
2675 }
2676 }
2677 }
2678 }
2679
2680 // Evaluate the effect on the message receiver.
2681 bool ReceiverIsTracked = false;
Jordy Rosee38dd952011-08-28 05:16:28 +00002682 if (!hasErr && CallOrMsg.isObjCMessage()) {
2683 const LocationContext *LC = C.getPredecessor()->getLocationContext();
2684 SVal Receiver = CallOrMsg.getInstanceMessageReceiver(LC);
2685 if (SymbolRef Sym = Receiver.getAsLocSymbol()) {
Jordy Rose294396b2011-08-22 23:48:23 +00002686 if (const RefVal *T = state->get<RefBindings>(Sym)) {
2687 ReceiverIsTracked = true;
Jordy Rose17a38e22011-09-02 05:55:19 +00002688 state = updateSymbol(state, Sym, *T, Summ.getReceiverEffect(),
2689 hasErr, C);
Jordy Rose294396b2011-08-22 23:48:23 +00002690 if (hasErr) {
Jordy Rosee38dd952011-08-28 05:16:28 +00002691 ErrorRange = CallOrMsg.getReceiverSourceRange();
Jordy Rose294396b2011-08-22 23:48:23 +00002692 ErrorSym = Sym;
2693 }
2694 }
2695 }
2696 }
2697
2698 // Process any errors.
2699 if (hasErr) {
2700 processNonLeakError(state, ErrorRange, hasErr, ErrorSym, C);
2701 return;
2702 }
2703
2704 // Consult the summary for the return value.
2705 RetEffect RE = Summ.getRetEffect();
2706
2707 if (RE.getKind() == RetEffect::OwnedWhenTrackedReceiver) {
Jordy Roseb6cfc092011-08-25 00:10:37 +00002708 if (ReceiverIsTracked)
Jordy Rose17a38e22011-09-02 05:55:19 +00002709 RE = getSummaryManager(C).getObjAllocRetEffect();
Jordy Roseb6cfc092011-08-25 00:10:37 +00002710 else
Jordy Rose294396b2011-08-22 23:48:23 +00002711 RE = RetEffect::MakeNoRet();
2712 }
2713
2714 switch (RE.getKind()) {
2715 default:
2716 llvm_unreachable("Unhandled RetEffect."); break;
2717
2718 case RetEffect::NoRet:
2719 // No work necessary.
2720 break;
2721
2722 case RetEffect::OwnedAllocatedSymbol:
2723 case RetEffect::OwnedSymbol: {
2724 SymbolRef Sym = state->getSVal(CallOrMsg.getOriginExpr()).getAsSymbol();
2725 if (!Sym)
2726 break;
2727
2728 // Use the result type from callOrMsg as it automatically adjusts
2729 // for methods/functions that return references.
2730 QualType ResultTy = CallOrMsg.getResultType(C.getASTContext());
2731 state = state->set<RefBindings>(Sym, RefVal::makeOwned(RE.getObjKind(),
2732 ResultTy));
2733
2734 // FIXME: Add a flag to the checker where allocations are assumed to
2735 // *not* fail. (The code below is out-of-date, though.)
2736#if 0
2737 if (RE.getKind() == RetEffect::OwnedAllocatedSymbol) {
2738 bool isFeasible;
2739 state = state.assume(loc::SymbolVal(Sym), true, isFeasible);
2740 assert(isFeasible && "Cannot assume fresh symbol is non-null.");
2741 }
2742#endif
2743
2744 break;
2745 }
2746
2747 case RetEffect::GCNotOwnedSymbol:
2748 case RetEffect::ARCNotOwnedSymbol:
2749 case RetEffect::NotOwnedSymbol: {
2750 const Expr *Ex = CallOrMsg.getOriginExpr();
2751 SymbolRef Sym = state->getSVal(Ex).getAsSymbol();
2752 if (!Sym)
2753 break;
2754
2755 // Use GetReturnType in order to give [NSFoo alloc] the type NSFoo *.
2756 QualType ResultTy = GetReturnType(Ex, C.getASTContext());
2757 state = state->set<RefBindings>(Sym, RefVal::makeNotOwned(RE.getObjKind(),
2758 ResultTy));
2759 break;
2760 }
2761 }
2762
2763 // This check is actually necessary; otherwise the statement builder thinks
2764 // we've hit a previously-found path.
2765 // Normally addTransition takes care of this, but we want the node pointer.
2766 ExplodedNode *NewNode;
2767 if (state == C.getState()) {
2768 NewNode = C.getPredecessor();
2769 } else {
2770 NewNode = C.generateNode(state);
2771 }
2772
Jordy Rose9c083b72011-08-24 18:56:32 +00002773 // Annotate the node with summary we used.
2774 if (NewNode) {
2775 // FIXME: This is ugly. See checkEndAnalysis for why it's necessary.
2776 if (ShouldResetSummaryLog) {
2777 SummaryLog.clear();
2778 ShouldResetSummaryLog = false;
2779 }
Jordy Roseec9ef852011-08-23 20:55:48 +00002780 SummaryLog[NewNode] = &Summ;
Jordy Rose9c083b72011-08-24 18:56:32 +00002781 }
Jordy Rose294396b2011-08-22 23:48:23 +00002782}
2783
Jordy Rosee0a5d322011-08-23 20:27:16 +00002784
2785const ProgramState *
Jordy Rose910c4052011-09-02 06:44:22 +00002786RetainCountChecker::updateSymbol(const ProgramState *state, SymbolRef sym,
2787 RefVal V, ArgEffect E, RefVal::Kind &hasErr,
2788 CheckerContext &C) const {
Jordy Rosee0a5d322011-08-23 20:27:16 +00002789 // In GC mode [... release] and [... retain] do nothing.
Jordy Rose910c4052011-09-02 06:44:22 +00002790 // In ARC mode they shouldn't exist at all, but we just ignore them.
Jordy Rose17a38e22011-09-02 05:55:19 +00002791 bool IgnoreRetainMsg = C.isObjCGCEnabled();
2792 if (!IgnoreRetainMsg)
2793 IgnoreRetainMsg = (bool)C.getASTContext().getLangOptions().ObjCAutoRefCount;
2794
Jordy Rosee0a5d322011-08-23 20:27:16 +00002795 switch (E) {
2796 default: break;
Jordy Rose17a38e22011-09-02 05:55:19 +00002797 case IncRefMsg: E = IgnoreRetainMsg ? DoNothing : IncRef; break;
2798 case DecRefMsg: E = IgnoreRetainMsg ? DoNothing : DecRef; break;
2799 case MakeCollectable: E = C.isObjCGCEnabled() ? DecRef : DoNothing; break;
2800 case NewAutoreleasePool: E = C.isObjCGCEnabled() ? DoNothing :
2801 NewAutoreleasePool; break;
Jordy Rosee0a5d322011-08-23 20:27:16 +00002802 }
2803
2804 // Handle all use-after-releases.
Jordy Rose17a38e22011-09-02 05:55:19 +00002805 if (!C.isObjCGCEnabled() && V.getKind() == RefVal::Released) {
Jordy Rosee0a5d322011-08-23 20:27:16 +00002806 V = V ^ RefVal::ErrorUseAfterRelease;
2807 hasErr = V.getKind();
2808 return state->set<RefBindings>(sym, V);
2809 }
2810
2811 switch (E) {
2812 case DecRefMsg:
2813 case IncRefMsg:
2814 case MakeCollectable:
2815 llvm_unreachable("DecRefMsg/IncRefMsg/MakeCollectable already converted");
2816 return state;
2817
2818 case Dealloc:
2819 // Any use of -dealloc in GC is *bad*.
Jordy Rose17a38e22011-09-02 05:55:19 +00002820 if (C.isObjCGCEnabled()) {
Jordy Rosee0a5d322011-08-23 20:27:16 +00002821 V = V ^ RefVal::ErrorDeallocGC;
2822 hasErr = V.getKind();
2823 break;
2824 }
2825
2826 switch (V.getKind()) {
2827 default:
2828 llvm_unreachable("Invalid RefVal state for an explicit dealloc.");
2829 break;
2830 case RefVal::Owned:
2831 // The object immediately transitions to the released state.
2832 V = V ^ RefVal::Released;
2833 V.clearCounts();
2834 return state->set<RefBindings>(sym, V);
2835 case RefVal::NotOwned:
2836 V = V ^ RefVal::ErrorDeallocNotOwned;
2837 hasErr = V.getKind();
2838 break;
2839 }
2840 break;
2841
2842 case NewAutoreleasePool:
Jordy Rose17a38e22011-09-02 05:55:19 +00002843 assert(!C.isObjCGCEnabled());
Jordy Rosee0a5d322011-08-23 20:27:16 +00002844 return state->add<AutoreleaseStack>(sym);
2845
2846 case MayEscape:
2847 if (V.getKind() == RefVal::Owned) {
2848 V = V ^ RefVal::NotOwned;
2849 break;
2850 }
2851
2852 // Fall-through.
2853
Jordy Rosee0a5d322011-08-23 20:27:16 +00002854 case DoNothing:
2855 return state;
2856
2857 case Autorelease:
Jordy Rose17a38e22011-09-02 05:55:19 +00002858 if (C.isObjCGCEnabled())
Jordy Rosee0a5d322011-08-23 20:27:16 +00002859 return state;
2860
2861 // Update the autorelease counts.
2862 state = SendAutorelease(state, ARCountFactory, sym);
2863 V = V.autorelease();
2864 break;
2865
2866 case StopTracking:
2867 return state->remove<RefBindings>(sym);
2868
2869 case IncRef:
2870 switch (V.getKind()) {
2871 default:
2872 llvm_unreachable("Invalid RefVal state for a retain.");
2873 break;
2874 case RefVal::Owned:
2875 case RefVal::NotOwned:
2876 V = V + 1;
2877 break;
2878 case RefVal::Released:
2879 // Non-GC cases are handled above.
Jordy Rose17a38e22011-09-02 05:55:19 +00002880 assert(C.isObjCGCEnabled());
Jordy Rosee0a5d322011-08-23 20:27:16 +00002881 V = (V ^ RefVal::Owned) + 1;
2882 break;
2883 }
2884 break;
2885
2886 case SelfOwn:
2887 V = V ^ RefVal::NotOwned;
2888 // Fall-through.
2889 case DecRef:
2890 case DecRefBridgedTransfered:
2891 switch (V.getKind()) {
2892 default:
2893 // case 'RefVal::Released' handled above.
2894 llvm_unreachable("Invalid RefVal state for a release.");
2895 break;
2896
2897 case RefVal::Owned:
2898 assert(V.getCount() > 0);
2899 if (V.getCount() == 1)
2900 V = V ^ (E == DecRefBridgedTransfered ?
2901 RefVal::NotOwned : RefVal::Released);
2902 V = V - 1;
2903 break;
2904
2905 case RefVal::NotOwned:
2906 if (V.getCount() > 0)
2907 V = V - 1;
2908 else {
2909 V = V ^ RefVal::ErrorReleaseNotOwned;
2910 hasErr = V.getKind();
2911 }
2912 break;
2913
2914 case RefVal::Released:
2915 // Non-GC cases are handled above.
Jordy Rose17a38e22011-09-02 05:55:19 +00002916 assert(C.isObjCGCEnabled());
Jordy Rosee0a5d322011-08-23 20:27:16 +00002917 V = V ^ RefVal::ErrorUseAfterRelease;
2918 hasErr = V.getKind();
2919 break;
2920 }
2921 break;
2922 }
2923 return state->set<RefBindings>(sym, V);
2924}
2925
Jordy Rose910c4052011-09-02 06:44:22 +00002926void RetainCountChecker::processNonLeakError(const ProgramState *St,
2927 SourceRange ErrorRange,
2928 RefVal::Kind ErrorKind,
2929 SymbolRef Sym,
2930 CheckerContext &C) const {
Jordy Rose294396b2011-08-22 23:48:23 +00002931 ExplodedNode *N = C.generateSink(St);
2932 if (!N)
2933 return;
2934
Jordy Rose294396b2011-08-22 23:48:23 +00002935 CFRefBug *BT;
2936 switch (ErrorKind) {
2937 default:
2938 llvm_unreachable("Unhandled error.");
2939 return;
2940 case RefVal::ErrorUseAfterRelease:
Jordy Rosed6334e12011-08-25 00:34:03 +00002941 if (!useAfterRelease)
2942 useAfterRelease.reset(new UseAfterRelease());
2943 BT = &*useAfterRelease;
Jordy Rose294396b2011-08-22 23:48:23 +00002944 break;
2945 case RefVal::ErrorReleaseNotOwned:
Jordy Rosed6334e12011-08-25 00:34:03 +00002946 if (!releaseNotOwned)
2947 releaseNotOwned.reset(new BadRelease());
2948 BT = &*releaseNotOwned;
Jordy Rose294396b2011-08-22 23:48:23 +00002949 break;
2950 case RefVal::ErrorDeallocGC:
Jordy Rosed6334e12011-08-25 00:34:03 +00002951 if (!deallocGC)
2952 deallocGC.reset(new DeallocGC());
2953 BT = &*deallocGC;
Jordy Rose294396b2011-08-22 23:48:23 +00002954 break;
2955 case RefVal::ErrorDeallocNotOwned:
Jordy Rosed6334e12011-08-25 00:34:03 +00002956 if (!deallocNotOwned)
2957 deallocNotOwned.reset(new DeallocNotOwned());
2958 BT = &*deallocNotOwned;
Jordy Rose294396b2011-08-22 23:48:23 +00002959 break;
2960 }
2961
Jordy Rosed6334e12011-08-25 00:34:03 +00002962 assert(BT);
Jordy Rose20589562011-08-24 22:39:09 +00002963 CFRefReport *report = new CFRefReport(*BT, C.getASTContext().getLangOptions(),
Jordy Rose17a38e22011-09-02 05:55:19 +00002964 C.isObjCGCEnabled(), SummaryLog,
2965 N, Sym);
Jordy Rose294396b2011-08-22 23:48:23 +00002966 report->addRange(ErrorRange);
2967 C.EmitReport(report);
2968}
2969
Jordy Rose910c4052011-09-02 06:44:22 +00002970//===----------------------------------------------------------------------===//
2971// Handle the return values of retain-count-related functions.
2972//===----------------------------------------------------------------------===//
2973
2974bool RetainCountChecker::evalCall(const CallExpr *CE, CheckerContext &C) const {
Jordy Rose76c506f2011-08-21 21:58:18 +00002975 // Get the callee. We're only interested in simple C functions.
2976 const ProgramState *state = C.getState();
2977 const Expr *Callee = CE->getCallee();
2978 SVal L = state->getSVal(Callee);
2979
2980 const FunctionDecl *FD = L.getAsFunctionDecl();
2981 if (!FD)
2982 return false;
2983
2984 IdentifierInfo *II = FD->getIdentifier();
2985 if (!II)
2986 return false;
2987
2988 // For now, we're only handling the functions that return aliases of their
2989 // arguments: CFRetain and CFMakeCollectable (and their families).
2990 // Eventually we should add other functions we can model entirely,
2991 // such as CFRelease, which don't invalidate their arguments or globals.
2992 if (CE->getNumArgs() != 1)
2993 return false;
2994
2995 // Get the name of the function.
2996 StringRef FName = II->getName();
2997 FName = FName.substr(FName.find_first_not_of('_'));
2998
2999 // See if it's one of the specific functions we know how to eval.
3000 bool canEval = false;
3001
3002 QualType ResultTy = FD->getResultType();
3003 if (ResultTy->isObjCIdType()) {
3004 // Handle: id NSMakeCollectable(CFTypeRef)
3005 canEval = II->isStr("NSMakeCollectable");
3006 } else if (ResultTy->isPointerType()) {
3007 // Handle: (CF|CG)Retain
3008 // CFMakeCollectable
3009 // It's okay to be a little sloppy here (CGMakeCollectable doesn't exist).
3010 if (cocoa::isRefType(ResultTy, "CF", FName) ||
3011 cocoa::isRefType(ResultTy, "CG", FName)) {
3012 canEval = isRetain(FD, FName) || isMakeCollectable(FD, FName);
3013 }
3014 }
3015
3016 if (!canEval)
3017 return false;
3018
3019 // Bind the return value.
3020 SVal RetVal = state->getSVal(CE->getArg(0));
3021 if (RetVal.isUnknown()) {
3022 // If the receiver is unknown, conjure a return value.
3023 SValBuilder &SVB = C.getSValBuilder();
3024 unsigned Count = C.getNodeBuilder().getCurrentBlockCount();
3025 SVal RetVal = SVB.getConjuredSymbolVal(0, CE, ResultTy, Count);
3026 }
3027 state = state->BindExpr(CE, RetVal, false);
3028
Jordy Rose294396b2011-08-22 23:48:23 +00003029 // FIXME: This should not be necessary, but otherwise the argument seems to be
3030 // considered alive during the next statement.
3031 if (const MemRegion *ArgRegion = RetVal.getAsRegion()) {
3032 // Save the refcount status of the argument.
3033 SymbolRef Sym = RetVal.getAsLocSymbol();
3034 RefBindings::data_type *Binding = 0;
3035 if (Sym)
3036 Binding = state->get<RefBindings>(Sym);
Jordy Rose76c506f2011-08-21 21:58:18 +00003037
Jordy Rose294396b2011-08-22 23:48:23 +00003038 // Invalidate the argument region.
3039 unsigned Count = C.getNodeBuilder().getCurrentBlockCount();
Jordy Rose537716a2011-08-27 22:51:26 +00003040 state = state->invalidateRegions(ArgRegion, CE, Count);
Jordy Rose76c506f2011-08-21 21:58:18 +00003041
Jordy Rose294396b2011-08-22 23:48:23 +00003042 // Restore the refcount status of the argument.
3043 if (Binding)
3044 state = state->set<RefBindings>(Sym, *Binding);
3045 }
3046
3047 C.addTransition(state);
Jordy Rose76c506f2011-08-21 21:58:18 +00003048 return true;
3049}
3050
Jordy Rose910c4052011-09-02 06:44:22 +00003051//===----------------------------------------------------------------------===//
3052// Handle return statements.
3053//===----------------------------------------------------------------------===//
Jordy Rosef53e8c72011-08-23 19:43:16 +00003054
Jordy Rose910c4052011-09-02 06:44:22 +00003055void RetainCountChecker::checkPreStmt(const ReturnStmt *S,
3056 CheckerContext &C) const {
Jordy Rosef53e8c72011-08-23 19:43:16 +00003057 const Expr *RetE = S->getRetValue();
3058 if (!RetE)
3059 return;
3060
3061 const ProgramState *state = C.getState();
3062 SymbolRef Sym = state->getSValAsScalarOrLoc(RetE).getAsLocSymbol();
3063 if (!Sym)
3064 return;
3065
3066 // Get the reference count binding (if any).
3067 const RefVal *T = state->get<RefBindings>(Sym);
3068 if (!T)
3069 return;
3070
3071 // Change the reference count.
3072 RefVal X = *T;
3073
3074 switch (X.getKind()) {
3075 case RefVal::Owned: {
3076 unsigned cnt = X.getCount();
3077 assert(cnt > 0);
3078 X.setCount(cnt - 1);
3079 X = X ^ RefVal::ReturnedOwned;
3080 break;
3081 }
3082
3083 case RefVal::NotOwned: {
3084 unsigned cnt = X.getCount();
3085 if (cnt) {
3086 X.setCount(cnt - 1);
3087 X = X ^ RefVal::ReturnedOwned;
3088 }
3089 else {
3090 X = X ^ RefVal::ReturnedNotOwned;
3091 }
3092 break;
3093 }
3094
3095 default:
3096 return;
3097 }
3098
3099 // Update the binding.
3100 state = state->set<RefBindings>(Sym, X);
3101 ExplodedNode *Pred = C.generateNode(state);
3102
3103 // At this point we have updated the state properly.
3104 // Everything after this is merely checking to see if the return value has
3105 // been over- or under-retained.
3106
3107 // Did we cache out?
3108 if (!Pred)
3109 return;
3110
Jordy Rosef53e8c72011-08-23 19:43:16 +00003111 // Update the autorelease counts.
3112 static SimpleProgramPointTag
Jordy Rose910c4052011-09-02 06:44:22 +00003113 AutoreleaseTag("RetainCountChecker : Autorelease");
Jordy Rosef53e8c72011-08-23 19:43:16 +00003114 GenericNodeBuilderRefCount Bd(C.getNodeBuilder(), S, &AutoreleaseTag);
Jordy Rose35c86952011-08-24 05:47:39 +00003115 llvm::tie(Pred, state) = handleAutoreleaseCounts(state, Bd, Pred,
3116 C.getEngine(), Sym, X);
Jordy Rosef53e8c72011-08-23 19:43:16 +00003117
3118 // Did we cache out?
Jordy Rose8d228632011-08-23 20:07:14 +00003119 if (!Pred)
Jordy Rosef53e8c72011-08-23 19:43:16 +00003120 return;
3121
3122 // Get the updated binding.
3123 T = state->get<RefBindings>(Sym);
3124 assert(T);
3125 X = *T;
3126
3127 // Consult the summary of the enclosing method.
Jordy Rose17a38e22011-09-02 05:55:19 +00003128 RetainSummaryManager &Summaries = getSummaryManager(C);
Jordy Rosef53e8c72011-08-23 19:43:16 +00003129 const Decl *CD = &Pred->getCodeDecl();
3130
3131 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(CD)) {
3132 // Unlike regular functions, /all/ ObjC methods are assumed to always
3133 // follow Cocoa retain-count conventions, not just those with special
3134 // names or attributes.
Jordy Roseb6cfc092011-08-25 00:10:37 +00003135 const RetainSummary *Summ = Summaries.getMethodSummary(MD);
Jordy Rosef53e8c72011-08-23 19:43:16 +00003136 RetEffect RE = Summ ? Summ->getRetEffect() : RetEffect::MakeNoRet();
3137 checkReturnWithRetEffect(S, C, Pred, RE, X, Sym, state);
3138 }
3139
3140 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(CD)) {
3141 if (!isa<CXXMethodDecl>(FD))
Jordy Roseb6cfc092011-08-25 00:10:37 +00003142 if (const RetainSummary *Summ = Summaries.getSummary(FD))
Jordy Rosef53e8c72011-08-23 19:43:16 +00003143 checkReturnWithRetEffect(S, C, Pred, Summ->getRetEffect(), X,
3144 Sym, state);
3145 }
3146}
3147
Jordy Rose910c4052011-09-02 06:44:22 +00003148void RetainCountChecker::checkReturnWithRetEffect(const ReturnStmt *S,
3149 CheckerContext &C,
3150 ExplodedNode *Pred,
3151 RetEffect RE, RefVal X,
3152 SymbolRef Sym,
Jordy Rosef53e8c72011-08-23 19:43:16 +00003153 const ProgramState *state) const {
Jordy Rosef53e8c72011-08-23 19:43:16 +00003154 // Any leaks or other errors?
3155 if (X.isReturnedOwned() && X.getCount() == 0) {
3156 if (RE.getKind() != RetEffect::NoRet) {
3157 bool hasError = false;
Jordy Rose17a38e22011-09-02 05:55:19 +00003158 if (C.isObjCGCEnabled() && RE.getObjKind() == RetEffect::ObjC) {
Jordy Rosef53e8c72011-08-23 19:43:16 +00003159 // Things are more complicated with garbage collection. If the
3160 // returned object is suppose to be an Objective-C object, we have
3161 // a leak (as the caller expects a GC'ed object) because no
3162 // method should return ownership unless it returns a CF object.
3163 hasError = true;
3164 X = X ^ RefVal::ErrorGCLeakReturned;
3165 }
3166 else if (!RE.isOwned()) {
3167 // Either we are using GC and the returned object is a CF type
3168 // or we aren't using GC. In either case, we expect that the
3169 // enclosing method is expected to return ownership.
3170 hasError = true;
3171 X = X ^ RefVal::ErrorLeakReturned;
3172 }
3173
3174 if (hasError) {
3175 // Generate an error node.
3176 state = state->set<RefBindings>(Sym, X);
3177 StmtNodeBuilder &Builder = C.getNodeBuilder();
3178
3179 static SimpleProgramPointTag
Jordy Rose910c4052011-09-02 06:44:22 +00003180 ReturnOwnLeakTag("RetainCountChecker : ReturnsOwnLeak");
Jordy Rosef53e8c72011-08-23 19:43:16 +00003181 ExplodedNode *N = Builder.generateNode(S, state, Pred,
3182 &ReturnOwnLeakTag);
3183 if (N) {
Jordy Rose17a38e22011-09-02 05:55:19 +00003184 const LangOptions &LOpts = C.getASTContext().getLangOptions();
3185 bool GCEnabled = C.isObjCGCEnabled();
Jordy Rosef53e8c72011-08-23 19:43:16 +00003186 CFRefReport *report =
Jordy Rose17a38e22011-09-02 05:55:19 +00003187 new CFRefLeakReport(*getLeakAtReturnBug(LOpts, GCEnabled),
3188 LOpts, GCEnabled, SummaryLog,
3189 N, Sym, C.getEngine());
Jordy Rosef53e8c72011-08-23 19:43:16 +00003190 C.EmitReport(report);
3191 }
3192 }
3193 }
3194 } else if (X.isReturnedNotOwned()) {
3195 if (RE.isOwned()) {
3196 // Trying to return a not owned object to a caller expecting an
3197 // owned object.
3198 state = state->set<RefBindings>(Sym, X ^ RefVal::ErrorReturnedNotOwned);
3199 StmtNodeBuilder &Builder = C.getNodeBuilder();
3200
3201 static SimpleProgramPointTag
Jordy Rose910c4052011-09-02 06:44:22 +00003202 ReturnNotOwnedTag("RetainCountChecker : ReturnNotOwnedForOwned");
Jordy Rosef53e8c72011-08-23 19:43:16 +00003203 ExplodedNode *N = Builder.generateNode(S, state, Pred,
3204 &ReturnNotOwnedTag);
3205 if (N) {
Jordy Rosed6334e12011-08-25 00:34:03 +00003206 if (!returnNotOwnedForOwned)
3207 returnNotOwnedForOwned.reset(new ReturnedNotOwnedForOwned());
3208
Jordy Rosef53e8c72011-08-23 19:43:16 +00003209 CFRefReport *report =
Jordy Rosed6334e12011-08-25 00:34:03 +00003210 new CFRefReport(*returnNotOwnedForOwned,
Jordy Rose17a38e22011-09-02 05:55:19 +00003211 C.getASTContext().getLangOptions(),
3212 C.isObjCGCEnabled(), SummaryLog, N, Sym);
Jordy Rosef53e8c72011-08-23 19:43:16 +00003213 C.EmitReport(report);
3214 }
3215 }
3216 }
3217}
3218
Jordy Rose8d228632011-08-23 20:07:14 +00003219//===----------------------------------------------------------------------===//
Jordy Rose910c4052011-09-02 06:44:22 +00003220// Check various ways a symbol can be invalidated.
3221//===----------------------------------------------------------------------===//
3222
3223void RetainCountChecker::checkBind(SVal loc, SVal val,
3224 CheckerContext &C) const {
3225 // Are we storing to something that causes the value to "escape"?
3226 bool escapes = true;
3227
3228 // A value escapes in three possible cases (this may change):
3229 //
3230 // (1) we are binding to something that is not a memory region.
3231 // (2) we are binding to a memregion that does not have stack storage
3232 // (3) we are binding to a memregion with stack storage that the store
3233 // does not understand.
3234 const ProgramState *state = C.getState();
3235
3236 if (loc::MemRegionVal *regionLoc = dyn_cast<loc::MemRegionVal>(&loc)) {
3237 escapes = !regionLoc->getRegion()->hasStackStorage();
3238
3239 if (!escapes) {
3240 // To test (3), generate a new state with the binding added. If it is
3241 // the same state, then it escapes (since the store cannot represent
3242 // the binding).
3243 escapes = (state == (state->bindLoc(*regionLoc, val)));
3244 }
3245 }
3246
3247 // If our store can represent the binding and we aren't storing to something
3248 // that doesn't have local storage then just return and have the simulation
3249 // state continue as is.
3250 if (!escapes)
3251 return;
3252
3253 // Otherwise, find all symbols referenced by 'val' that we are tracking
3254 // and stop tracking them.
3255 state = state->scanReachableSymbols<StopTrackingCallback>(val).getState();
3256 C.addTransition(state);
3257}
3258
3259const ProgramState *RetainCountChecker::evalAssume(const ProgramState *state,
3260 SVal Cond,
3261 bool Assumption) const {
3262
3263 // FIXME: We may add to the interface of evalAssume the list of symbols
3264 // whose assumptions have changed. For now we just iterate through the
3265 // bindings and check if any of the tracked symbols are NULL. This isn't
3266 // too bad since the number of symbols we will track in practice are
3267 // probably small and evalAssume is only called at branches and a few
3268 // other places.
3269 RefBindings B = state->get<RefBindings>();
3270
3271 if (B.isEmpty())
3272 return state;
3273
3274 bool changed = false;
3275 RefBindings::Factory &RefBFactory = state->get_context<RefBindings>();
3276
3277 for (RefBindings::iterator I = B.begin(), E = B.end(); I != E; ++I) {
3278 // Check if the symbol is null (or equal to any constant).
3279 // If this is the case, stop tracking the symbol.
3280 if (state->getSymVal(I.getKey())) {
3281 changed = true;
3282 B = RefBFactory.remove(B, I.getKey());
3283 }
3284 }
3285
3286 if (changed)
3287 state = state->set<RefBindings>(B);
3288
3289 return state;
3290}
3291
3292const ProgramState *
3293RetainCountChecker::checkRegionChanges(const ProgramState *state,
3294 const StoreManager::InvalidatedSymbols *invalidated,
3295 ArrayRef<const MemRegion *> ExplicitRegions,
3296 ArrayRef<const MemRegion *> Regions) const {
3297 if (!invalidated)
3298 return state;
3299
3300 llvm::SmallPtrSet<SymbolRef, 8> WhitelistedSymbols;
3301 for (ArrayRef<const MemRegion *>::iterator I = ExplicitRegions.begin(),
3302 E = ExplicitRegions.end(); I != E; ++I) {
3303 if (const SymbolicRegion *SR = (*I)->StripCasts()->getAs<SymbolicRegion>())
3304 WhitelistedSymbols.insert(SR->getSymbol());
3305 }
3306
3307 for (StoreManager::InvalidatedSymbols::const_iterator I=invalidated->begin(),
3308 E = invalidated->end(); I!=E; ++I) {
3309 SymbolRef sym = *I;
3310 if (WhitelistedSymbols.count(sym))
3311 continue;
3312 // Remove any existing reference-count binding.
3313 state = state->remove<RefBindings>(sym);
3314 }
3315 return state;
3316}
3317
3318//===----------------------------------------------------------------------===//
Jordy Rose8d228632011-08-23 20:07:14 +00003319// Handle dead symbols and end-of-path.
3320//===----------------------------------------------------------------------===//
3321
3322std::pair<ExplodedNode *, const ProgramState *>
Jordy Rose910c4052011-09-02 06:44:22 +00003323RetainCountChecker::handleAutoreleaseCounts(const ProgramState *state,
3324 GenericNodeBuilderRefCount Bd,
3325 ExplodedNode *Pred, ExprEngine &Eng,
3326 SymbolRef Sym, RefVal V) const {
Jordy Rose8d228632011-08-23 20:07:14 +00003327 unsigned ACnt = V.getAutoreleaseCount();
3328
3329 // No autorelease counts? Nothing to be done.
3330 if (!ACnt)
3331 return std::make_pair(Pred, state);
3332
Jordy Rose17a38e22011-09-02 05:55:19 +00003333 assert(!Eng.isObjCGCEnabled() && "Autorelease counts in GC mode?");
Jordy Rose8d228632011-08-23 20:07:14 +00003334 unsigned Cnt = V.getCount();
3335
3336 // FIXME: Handle sending 'autorelease' to already released object.
3337
3338 if (V.getKind() == RefVal::ReturnedOwned)
3339 ++Cnt;
3340
3341 if (ACnt <= Cnt) {
3342 if (ACnt == Cnt) {
3343 V.clearCounts();
3344 if (V.getKind() == RefVal::ReturnedOwned)
3345 V = V ^ RefVal::ReturnedNotOwned;
3346 else
3347 V = V ^ RefVal::NotOwned;
3348 } else {
3349 V.setCount(Cnt - ACnt);
3350 V.setAutoreleaseCount(0);
3351 }
3352 state = state->set<RefBindings>(Sym, V);
3353 ExplodedNode *N = Bd.MakeNode(state, Pred);
3354 if (N == 0)
3355 state = 0;
3356 return std::make_pair(N, state);
3357 }
3358
3359 // Woah! More autorelease counts then retain counts left.
3360 // Emit hard error.
3361 V = V ^ RefVal::ErrorOverAutorelease;
3362 state = state->set<RefBindings>(Sym, V);
3363
3364 if (ExplodedNode *N = Bd.MakeNode(state, Pred)) {
3365 N->markAsSink();
3366
3367 llvm::SmallString<128> sbuf;
3368 llvm::raw_svector_ostream os(sbuf);
3369 os << "Object over-autoreleased: object was sent -autorelease ";
3370 if (V.getAutoreleaseCount() > 1)
3371 os << V.getAutoreleaseCount() << " times ";
3372 os << "but the object has a +" << V.getCount() << " retain count";
3373
Jordy Rosed6334e12011-08-25 00:34:03 +00003374 if (!overAutorelease)
3375 overAutorelease.reset(new OverAutorelease());
3376
Jordy Rose20589562011-08-24 22:39:09 +00003377 const LangOptions &LOpts = Eng.getContext().getLangOptions();
Jordy Rose8d228632011-08-23 20:07:14 +00003378 CFRefReport *report =
Jordy Rosed6334e12011-08-25 00:34:03 +00003379 new CFRefReport(*overAutorelease, LOpts, /* GCEnabled = */ false,
3380 SummaryLog, N, Sym, os.str());
Jordy Rose8d228632011-08-23 20:07:14 +00003381 Eng.getBugReporter().EmitReport(report);
3382 }
3383
3384 return std::make_pair((ExplodedNode *)0, (const ProgramState *)0);
3385}
Jordy Rose38f17d62011-08-23 19:01:07 +00003386
3387const ProgramState *
Jordy Rose910c4052011-09-02 06:44:22 +00003388RetainCountChecker::handleSymbolDeath(const ProgramState *state,
3389 SymbolRef sid, RefVal V,
Jordy Rose38f17d62011-08-23 19:01:07 +00003390 SmallVectorImpl<SymbolRef> &Leaked) const {
Jordy Rose53376122011-08-24 04:48:19 +00003391 bool hasLeak = false;
Jordy Rose38f17d62011-08-23 19:01:07 +00003392 if (V.isOwned())
3393 hasLeak = true;
3394 else if (V.isNotOwned() || V.isReturnedOwned())
3395 hasLeak = (V.getCount() > 0);
3396
3397 if (!hasLeak)
3398 return state->remove<RefBindings>(sid);
3399
3400 Leaked.push_back(sid);
3401 return state->set<RefBindings>(sid, V ^ RefVal::ErrorLeak);
3402}
3403
3404ExplodedNode *
Jordy Rose910c4052011-09-02 06:44:22 +00003405RetainCountChecker::processLeaks(const ProgramState *state,
3406 SmallVectorImpl<SymbolRef> &Leaked,
3407 GenericNodeBuilderRefCount &Builder,
3408 ExprEngine &Eng, ExplodedNode *Pred) const {
Jordy Rose38f17d62011-08-23 19:01:07 +00003409 if (Leaked.empty())
3410 return Pred;
3411
3412 // Generate an intermediate node representing the leak point.
3413 ExplodedNode *N = Builder.MakeNode(state, Pred);
3414
3415 if (N) {
3416 for (SmallVectorImpl<SymbolRef>::iterator
3417 I = Leaked.begin(), E = Leaked.end(); I != E; ++I) {
3418
Jordy Rose17a38e22011-09-02 05:55:19 +00003419 const LangOptions &LOpts = Eng.getContext().getLangOptions();
3420 bool GCEnabled = Eng.isObjCGCEnabled();
3421 CFRefBug *BT = Pred ? getLeakWithinFunctionBug(LOpts, GCEnabled)
3422 : getLeakAtReturnBug(LOpts, GCEnabled);
Jordy Rose38f17d62011-08-23 19:01:07 +00003423 assert(BT && "BugType not initialized.");
Jordy Rose20589562011-08-24 22:39:09 +00003424
Jordy Rose17a38e22011-09-02 05:55:19 +00003425 CFRefLeakReport *report = new CFRefLeakReport(*BT, LOpts, GCEnabled,
Jordy Rose20589562011-08-24 22:39:09 +00003426 SummaryLog, N, *I, Eng);
Jordy Rose38f17d62011-08-23 19:01:07 +00003427 Eng.getBugReporter().EmitReport(report);
3428 }
3429 }
3430
3431 return N;
3432}
3433
Jordy Rose910c4052011-09-02 06:44:22 +00003434void RetainCountChecker::checkEndPath(EndOfFunctionNodeBuilder &Builder,
3435 ExprEngine &Eng) const {
Jordy Rose38f17d62011-08-23 19:01:07 +00003436 const ProgramState *state = Builder.getState();
3437 GenericNodeBuilderRefCount Bd(Builder);
3438 RefBindings B = state->get<RefBindings>();
Anna Zaks06588792011-09-30 02:19:19 +00003439 ExplodedNode *Pred = Builder.getPredecessor();
Jordy Rose38f17d62011-08-23 19:01:07 +00003440
3441 for (RefBindings::iterator I = B.begin(), E = B.end(); I != E; ++I) {
Jordy Rose8d228632011-08-23 20:07:14 +00003442 llvm::tie(Pred, state) = handleAutoreleaseCounts(state, Bd, Pred, Eng,
3443 I->first, I->second);
3444 if (!state)
Jordy Rose38f17d62011-08-23 19:01:07 +00003445 return;
3446 }
3447
3448 B = state->get<RefBindings>();
3449 SmallVector<SymbolRef, 10> Leaked;
3450
3451 for (RefBindings::iterator I = B.begin(), E = B.end(); I != E; ++I)
Jordy Rose8d228632011-08-23 20:07:14 +00003452 state = handleSymbolDeath(state, I->first, I->second, Leaked);
Jordy Rose38f17d62011-08-23 19:01:07 +00003453
3454 processLeaks(state, Leaked, Bd, Eng, Pred);
3455}
3456
3457const ProgramPointTag *
Jordy Rose910c4052011-09-02 06:44:22 +00003458RetainCountChecker::getDeadSymbolTag(SymbolRef sym) const {
Jordy Rose38f17d62011-08-23 19:01:07 +00003459 const SimpleProgramPointTag *&tag = DeadSymbolTags[sym];
3460 if (!tag) {
3461 llvm::SmallString<64> buf;
3462 llvm::raw_svector_ostream out(buf);
Jordy Rose910c4052011-09-02 06:44:22 +00003463 out << "RetainCountChecker : Dead Symbol : " << sym->getSymbolID();
Jordy Rose38f17d62011-08-23 19:01:07 +00003464 tag = new SimpleProgramPointTag(out.str());
3465 }
3466 return tag;
3467}
3468
Jordy Rose910c4052011-09-02 06:44:22 +00003469void RetainCountChecker::checkDeadSymbols(SymbolReaper &SymReaper,
3470 CheckerContext &C) const {
Jordy Rose38f17d62011-08-23 19:01:07 +00003471 StmtNodeBuilder &Builder = C.getNodeBuilder();
3472 ExprEngine &Eng = C.getEngine();
3473 const Stmt *S = C.getStmt();
3474 ExplodedNode *Pred = C.getPredecessor();
3475
Jordy Rose38f17d62011-08-23 19:01:07 +00003476 const ProgramState *state = C.getState();
3477 RefBindings B = state->get<RefBindings>();
3478
3479 // Update counts from autorelease pools
3480 for (SymbolReaper::dead_iterator I = SymReaper.dead_begin(),
3481 E = SymReaper.dead_end(); I != E; ++I) {
3482 SymbolRef Sym = *I;
3483 if (const RefVal *T = B.lookup(Sym)){
3484 // Use the symbol as the tag.
3485 // FIXME: This might not be as unique as we would like.
3486 GenericNodeBuilderRefCount Bd(Builder, S, getDeadSymbolTag(Sym));
Jordy Rose8d228632011-08-23 20:07:14 +00003487 llvm::tie(Pred, state) = handleAutoreleaseCounts(state, Bd, Pred, Eng,
3488 Sym, *T);
3489 if (!state)
Jordy Rose38f17d62011-08-23 19:01:07 +00003490 return;
3491 }
3492 }
3493
3494 B = state->get<RefBindings>();
3495 SmallVector<SymbolRef, 10> Leaked;
3496
3497 for (SymbolReaper::dead_iterator I = SymReaper.dead_begin(),
3498 E = SymReaper.dead_end(); I != E; ++I) {
3499 if (const RefVal *T = B.lookup(*I))
3500 state = handleSymbolDeath(state, *I, *T, Leaked);
3501 }
3502
3503 {
3504 GenericNodeBuilderRefCount Bd(Builder, S, this);
3505 Pred = processLeaks(state, Leaked, Bd, Eng, Pred);
3506 }
3507
3508 // Did we cache out?
3509 if (!Pred)
3510 return;
3511
3512 // Now generate a new node that nukes the old bindings.
3513 RefBindings::Factory &F = state->get_context<RefBindings>();
3514
3515 for (SymbolReaper::dead_iterator I = SymReaper.dead_begin(),
3516 E = SymReaper.dead_end(); I != E; ++I)
3517 B = F.remove(B, *I);
3518
3519 state = state->set<RefBindings>(B);
3520 C.generateNode(state, Pred);
3521}
3522
Ted Kremenekd593eb92009-11-25 22:17:44 +00003523//===----------------------------------------------------------------------===//
Jordy Rosedbd658e2011-08-28 19:11:56 +00003524// Debug printing of refcount bindings and autorelease pools.
3525//===----------------------------------------------------------------------===//
3526
3527static void PrintPool(raw_ostream &Out, SymbolRef Sym,
3528 const ProgramState *State) {
3529 Out << ' ';
3530 if (Sym)
3531 Out << Sym->getSymbolID();
3532 else
3533 Out << "<pool>";
3534 Out << ":{";
3535
3536 // Get the contents of the pool.
3537 if (const ARCounts *Cnts = State->get<AutoreleasePoolContents>(Sym))
3538 for (ARCounts::iterator I = Cnts->begin(), E = Cnts->end(); I != E; ++I)
3539 Out << '(' << I.getKey() << ',' << I.getData() << ')';
3540
3541 Out << '}';
3542}
3543
Benjamin Kramerd77ba892011-09-03 03:30:59 +00003544static bool UsesAutorelease(const ProgramState *state) {
Jordy Rosedbd658e2011-08-28 19:11:56 +00003545 // A state uses autorelease if it allocated an autorelease pool or if it has
3546 // objects in the caller's autorelease pool.
3547 return !state->get<AutoreleaseStack>().isEmpty() ||
3548 state->get<AutoreleasePoolContents>(SymbolRef());
3549}
3550
Jordy Rose910c4052011-09-02 06:44:22 +00003551void RetainCountChecker::printState(raw_ostream &Out, const ProgramState *State,
3552 const char *NL, const char *Sep) const {
Jordy Rosedbd658e2011-08-28 19:11:56 +00003553
3554 RefBindings B = State->get<RefBindings>();
3555
3556 if (!B.isEmpty())
3557 Out << Sep << NL;
3558
3559 for (RefBindings::iterator I = B.begin(), E = B.end(); I != E; ++I) {
3560 Out << I->first << " : ";
3561 I->second.print(Out);
3562 Out << NL;
3563 }
3564
3565 // Print the autorelease stack.
3566 if (UsesAutorelease(State)) {
3567 Out << Sep << NL << "AR pool stack:";
3568 ARStack Stack = State->get<AutoreleaseStack>();
3569
3570 PrintPool(Out, SymbolRef(), State); // Print the caller's pool.
3571 for (ARStack::iterator I = Stack.begin(), E = Stack.end(); I != E; ++I)
3572 PrintPool(Out, *I, State);
3573
3574 Out << NL;
3575 }
3576}
3577
3578//===----------------------------------------------------------------------===//
Jordy Rose910c4052011-09-02 06:44:22 +00003579// Checker registration.
Ted Kremenek6b3a0f72008-03-11 06:39:11 +00003580//===----------------------------------------------------------------------===//
3581
Jordy Rose17a38e22011-09-02 05:55:19 +00003582void ento::registerRetainCountChecker(CheckerManager &Mgr) {
Jordy Rose910c4052011-09-02 06:44:22 +00003583 Mgr.registerChecker<RetainCountChecker>();
Jordy Rose17a38e22011-09-02 05:55:19 +00003584}
3585