blob: 5fd223c471166d9c987a7297bea23e811024302c [file] [log] [blame]
Chris Lattnerbda0b622008-03-15 23:59:48 +00001// CFRefCount.cpp - Transfer functions for tracking simple values -*- 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//
Gabor Greif843e9342008-03-06 10:40:09 +000010// This file defines the methods for CFRefCount, which implements
Ted Kremenek2fff37e2008-03-06 00:08:09 +000011// a reference count checker for Core Foundation (Mac OS X).
12//
13//===----------------------------------------------------------------------===//
14
Mike Stump1eb44332009-09-09 15:08:12 +000015#include "clang/AST/DeclObjC.h"
Ted Kremenek38cc6bc2009-11-26 02:38:19 +000016#include "clang/AST/StmtVisitor.h"
Ted Kremenek0b526b42010-02-18 00:05:58 +000017#include "clang/Basic/LangOptions.h"
18#include "clang/Basic/SourceManager.h"
Ted Kremenek21142582010-12-23 19:38:26 +000019#include "clang/StaticAnalyzer/BugReporter/BugType.h"
20#include "clang/StaticAnalyzer/BugReporter/PathDiagnostic.h"
21#include "clang/StaticAnalyzer/Checkers/LocalCheckers.h"
Ted Kremenekbb8fef32010-12-17 05:21:58 +000022#include "clang/Analysis/DomainSpecific/CocoaConventions.h"
Ted Kremenek21142582010-12-23 19:38:26 +000023#include "clang/StaticAnalyzer/PathSensitive/CheckerVisitor.h"
24#include "clang/StaticAnalyzer/PathSensitive/ExprEngineBuilders.h"
25#include "clang/StaticAnalyzer/PathSensitive/GRStateTrait.h"
26#include "clang/StaticAnalyzer/PathSensitive/TransferFuncs.h"
27#include "clang/StaticAnalyzer/PathSensitive/SymbolManager.h"
Ted Kremenek6b3a0f72008-03-11 06:39:11 +000028#include "llvm/ADT/DenseMap.h"
29#include "llvm/ADT/FoldingSet.h"
Ted Kremenek6d348932008-10-21 15:53:15 +000030#include "llvm/ADT/ImmutableList.h"
Ted Kremenek0b526b42010-02-18 00:05:58 +000031#include "llvm/ADT/ImmutableMap.h"
Ted Kremenek6ed9afc2008-05-16 18:33:44 +000032#include "llvm/ADT/STLExtras.h"
Ted Kremenek0b526b42010-02-18 00:05:58 +000033#include "llvm/ADT/StringExtras.h"
Ted Kremenek98530452008-08-12 20:41:56 +000034#include <stdarg.h>
Ted Kremenek2fff37e2008-03-06 00:08:09 +000035
36using namespace clang;
Ted Kremenek9ef65372010-12-23 07:20:52 +000037using namespace ento;
Benjamin Kramere45c1492010-01-11 19:46:28 +000038using llvm::StringRef;
Ted Kremeneka64e89b2010-01-27 06:13:48 +000039using llvm::StrInStrNoCase;
Ted Kremenek4c79e552008-11-05 16:54:44 +000040
Ted Kremenekd775c662010-05-21 21:57:00 +000041namespace {
42class InstanceReceiver {
43 const ObjCMessageExpr *ME;
44 const LocationContext *LC;
45public:
46 InstanceReceiver(const ObjCMessageExpr *me = 0,
47 const LocationContext *lc = 0) : ME(me), LC(lc) {}
48
49 bool isValid() const {
50 return ME && ME->isInstanceMessage();
51 }
52 operator bool() const {
53 return isValid();
54 }
55
56 SVal getSValAsScalarOrLoc(const GRState *state) {
57 assert(isValid());
58 // We have an expression for the receiver? Fetch the value
59 // of that expression.
60 if (const Expr *Ex = ME->getInstanceReceiver())
61 return state->getSValAsScalarOrLoc(Ex);
62
63 // Otherwise we are sending a message to super. In this case the
64 // object reference is the same as 'self'.
65 if (const ImplicitParamDecl *SelfDecl = LC->getSelfDecl())
66 return state->getSVal(state->getRegion(SelfDecl, LC));
67
68 return UnknownVal();
69 }
70
71 SourceRange getSourceRange() const {
72 assert(isValid());
73 if (const Expr *Ex = ME->getInstanceReceiver())
74 return Ex->getSourceRange();
75
76 // Otherwise we are sending a message to super.
77 SourceLocation L = ME->getSuperLoc();
78 assert(L.isValid());
79 return SourceRange(L, L);
80 }
81};
82}
83
Ted Kremeneka8833552009-04-29 23:03:22 +000084static const ObjCMethodDecl*
Mike Stump1eb44332009-09-09 15:08:12 +000085ResolveToInterfaceMethodDecl(const ObjCMethodDecl *MD) {
Zhongxing Xubf9c2b22010-07-20 02:53:15 +000086 const ObjCInterfaceDecl *ID = MD->getClassInterface();
Mike Stump1eb44332009-09-09 15:08:12 +000087
Ted Kremeneka8833552009-04-29 23:03:22 +000088 return MD->isInstanceMethod()
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +000089 ? ID->lookupInstanceMethod(MD->getSelector())
90 : ID->lookupClassMethod(MD->getSelector());
Ted Kremenek4c79e552008-11-05 16:54:44 +000091}
Ted Kremenek5c74d502008-10-24 21:18:08 +000092
Ted Kremenek9d9d3a62009-05-08 23:09:42 +000093namespace {
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +000094class GenericNodeBuilder {
Argyrios Kyrtzidisd2592a32010-12-22 18:53:44 +000095 StmtNodeBuilder *SNB;
Zhongxing Xu03509ae2010-07-20 06:22:24 +000096 const Stmt *S;
Ted Kremenek9d9d3a62009-05-08 23:09:42 +000097 const void *tag;
Argyrios Kyrtzidisd2592a32010-12-22 18:53:44 +000098 EndPathNodeBuilder *ENB;
Ted Kremenek9d9d3a62009-05-08 23:09:42 +000099public:
Argyrios Kyrtzidisd2592a32010-12-22 18:53:44 +0000100 GenericNodeBuilder(StmtNodeBuilder &snb, const Stmt *s,
Ted Kremenek9d9d3a62009-05-08 23:09:42 +0000101 const void *t)
102 : SNB(&snb), S(s), tag(t), ENB(0) {}
Zhongxing Xu031ccc02009-08-06 12:48:26 +0000103
Argyrios Kyrtzidisd2592a32010-12-22 18:53:44 +0000104 GenericNodeBuilder(EndPathNodeBuilder &enb)
Ted Kremenek9d9d3a62009-05-08 23:09:42 +0000105 : SNB(0), S(0), tag(0), ENB(&enb) {}
Mike Stump1eb44332009-09-09 15:08:12 +0000106
Zhongxing Xu031ccc02009-08-06 12:48:26 +0000107 ExplodedNode *MakeNode(const GRState *state, ExplodedNode *Pred) {
Ted Kremenek9d9d3a62009-05-08 23:09:42 +0000108 if (SNB)
Mike Stump1eb44332009-09-09 15:08:12 +0000109 return SNB->generateNode(PostStmt(S, Pred->getLocationContext(), tag),
Zhongxing Xu25e695b2009-08-15 03:17:38 +0000110 state, Pred);
Mike Stump1eb44332009-09-09 15:08:12 +0000111
Ted Kremenek9d9d3a62009-05-08 23:09:42 +0000112 assert(ENB);
Ted Kremenek80c24182009-05-09 00:44:07 +0000113 return ENB->generateNode(state, Pred);
Ted Kremenek9d9d3a62009-05-08 23:09:42 +0000114 }
115};
116} // end anonymous namespace
117
Ted Kremenek05cbe1a2008-04-09 23:49:11 +0000118//===----------------------------------------------------------------------===//
Ted Kremenek553cf182008-06-25 21:21:56 +0000119// Primitives used for constructing summaries for function/method calls.
Ted Kremenek05cbe1a2008-04-09 23:49:11 +0000120//===----------------------------------------------------------------------===//
121
Ted Kremenek553cf182008-06-25 21:21:56 +0000122/// ArgEffect is used to summarize a function/method call's effect on a
123/// particular argument.
Ted Kremenekf95e9fc2009-03-17 19:42:23 +0000124enum ArgEffect { Autorelease, Dealloc, DecRef, DecRefMsg, DoNothing,
125 DoNothingByRef, IncRefMsg, IncRef, MakeCollectable, MayEscape,
126 NewAutoreleasePool, SelfOwn, StopTracking };
Ted Kremenek553cf182008-06-25 21:21:56 +0000127
Ted Kremenek6b3a0f72008-03-11 06:39:11 +0000128namespace llvm {
Ted Kremenekb77449c2009-05-03 05:20:50 +0000129template <> struct FoldingSetTrait<ArgEffect> {
130static inline void Profile(const ArgEffect X, FoldingSetNodeID& ID) {
131 ID.AddInteger((unsigned) X);
132}
Ted Kremenek553cf182008-06-25 21:21:56 +0000133};
Ted Kremenek6b3a0f72008-03-11 06:39:11 +0000134} // end llvm namespace
135
Ted Kremenekb77449c2009-05-03 05:20:50 +0000136/// ArgEffects summarizes the effects of a function/method call on all of
137/// its arguments.
138typedef llvm::ImmutableMap<unsigned,ArgEffect> ArgEffects;
139
Ted Kremenek6b3a0f72008-03-11 06:39:11 +0000140namespace {
Ted Kremenek553cf182008-06-25 21:21:56 +0000141
142/// RetEffect is used to summarize a function/method call's behavior with
Mike Stump1eb44332009-09-09 15:08:12 +0000143/// respect to its return value.
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +0000144class RetEffect {
Ted Kremenek6b3a0f72008-03-11 06:39:11 +0000145public:
Ted Kremeneka7344702008-06-23 18:02:52 +0000146 enum Kind { NoRet, Alias, OwnedSymbol, OwnedAllocatedSymbol,
Ted Kremenek78a35a32009-05-12 20:06:54 +0000147 NotOwnedSymbol, GCNotOwnedSymbol, ReceiverAlias,
148 OwnedWhenTrackedReceiver };
Mike Stump1eb44332009-09-09 15:08:12 +0000149
150 enum ObjKind { CF, ObjC, AnyObj };
Ted Kremenek2d1652e2009-01-28 05:56:51 +0000151
Ted Kremenek6b3a0f72008-03-11 06:39:11 +0000152private:
Ted Kremenek2d1652e2009-01-28 05:56:51 +0000153 Kind K;
154 ObjKind O;
155 unsigned index;
156
157 RetEffect(Kind k, unsigned idx = 0) : K(k), O(AnyObj), index(idx) {}
158 RetEffect(Kind k, ObjKind o) : K(k), O(o), index(0) {}
Mike Stump1eb44332009-09-09 15:08:12 +0000159
Ted Kremenek6b3a0f72008-03-11 06:39:11 +0000160public:
Ted Kremenek2d1652e2009-01-28 05:56:51 +0000161 Kind getKind() const { return K; }
162
163 ObjKind getObjKind() const { return O; }
Mike Stump1eb44332009-09-09 15:08:12 +0000164
165 unsigned getIndex() const {
Ted Kremenek6b3a0f72008-03-11 06:39:11 +0000166 assert(getKind() == Alias);
Ted Kremenek2d1652e2009-01-28 05:56:51 +0000167 return index;
Ted Kremenek6b3a0f72008-03-11 06:39:11 +0000168 }
Mike Stump1eb44332009-09-09 15:08:12 +0000169
Ted Kremeneka8833552009-04-29 23:03:22 +0000170 bool isOwned() const {
Ted Kremenek78a35a32009-05-12 20:06:54 +0000171 return K == OwnedSymbol || K == OwnedAllocatedSymbol ||
172 K == OwnedWhenTrackedReceiver;
Ted Kremeneka8833552009-04-29 23:03:22 +0000173 }
Mike Stump1eb44332009-09-09 15:08:12 +0000174
Ted Kremenek78a35a32009-05-12 20:06:54 +0000175 static RetEffect MakeOwnedWhenTrackedReceiver() {
176 return RetEffect(OwnedWhenTrackedReceiver, ObjC);
177 }
Mike Stump1eb44332009-09-09 15:08:12 +0000178
Ted Kremenek553cf182008-06-25 21:21:56 +0000179 static RetEffect MakeAlias(unsigned Idx) {
180 return RetEffect(Alias, Idx);
181 }
182 static RetEffect MakeReceiverAlias() {
183 return RetEffect(ReceiverAlias);
Mike Stump1eb44332009-09-09 15:08:12 +0000184 }
Ted Kremenek2d1652e2009-01-28 05:56:51 +0000185 static RetEffect MakeOwned(ObjKind o, bool isAllocated = false) {
186 return RetEffect(isAllocated ? OwnedAllocatedSymbol : OwnedSymbol, o);
Mike Stump1eb44332009-09-09 15:08:12 +0000187 }
Ted Kremenek2d1652e2009-01-28 05:56:51 +0000188 static RetEffect MakeNotOwned(ObjKind o) {
189 return RetEffect(NotOwnedSymbol, o);
Ted Kremeneke798e7c2009-04-27 19:14:45 +0000190 }
191 static RetEffect MakeGCNotOwned() {
192 return RetEffect(GCNotOwnedSymbol, ObjC);
193 }
Mike Stump1eb44332009-09-09 15:08:12 +0000194
Ted Kremenek553cf182008-06-25 21:21:56 +0000195 static RetEffect MakeNoRet() {
196 return RetEffect(NoRet);
Ted Kremeneka7344702008-06-23 18:02:52 +0000197 }
Ted Kremenek6b3a0f72008-03-11 06:39:11 +0000198};
Mike Stump1eb44332009-09-09 15:08:12 +0000199
Ted Kremenekb7ddd9b2009-11-13 01:54:21 +0000200//===----------------------------------------------------------------------===//
201// Reference-counting logic (typestate + counts).
202//===----------------------------------------------------------------------===//
Mike Stump1eb44332009-09-09 15:08:12 +0000203
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +0000204class RefVal {
Ted Kremenekb7ddd9b2009-11-13 01:54:21 +0000205public:
206 enum Kind {
207 Owned = 0, // Owning reference.
208 NotOwned, // Reference is not owned by still valid (not freed).
209 Released, // Object has been released.
210 ReturnedOwned, // Returned object passes ownership to caller.
211 ReturnedNotOwned, // Return object does not pass ownership to caller.
212 ERROR_START,
213 ErrorDeallocNotOwned, // -dealloc called on non-owned object.
214 ErrorDeallocGC, // Calling -dealloc with GC enabled.
215 ErrorUseAfterRelease, // Object used after released.
216 ErrorReleaseNotOwned, // Release of an object that was not owned.
217 ERROR_LEAK_START,
218 ErrorLeak, // A memory leak due to excessive reference counts.
219 ErrorLeakReturned, // A memory leak due to the returning method not having
220 // the correct naming conventions.
221 ErrorGCLeakReturned,
222 ErrorOverAutorelease,
223 ErrorReturnedNotOwned
224 };
Ted Kremenekdcee3ce2010-07-01 20:16:50 +0000225
Ted Kremenekb7ddd9b2009-11-13 01:54:21 +0000226private:
227 Kind kind;
228 RetEffect::ObjKind okind;
229 unsigned Cnt;
230 unsigned ACnt;
231 QualType T;
Ted Kremenekdcee3ce2010-07-01 20:16:50 +0000232
Ted Kremenekb7ddd9b2009-11-13 01:54:21 +0000233 RefVal(Kind k, RetEffect::ObjKind o, unsigned cnt, unsigned acnt, QualType t)
234 : kind(k), okind(o), Cnt(cnt), ACnt(acnt), T(t) {}
Ted Kremenekdcee3ce2010-07-01 20:16:50 +0000235
Ted Kremenekb7ddd9b2009-11-13 01:54:21 +0000236public:
237 Kind getKind() const { return kind; }
Ted Kremenekdcee3ce2010-07-01 20:16:50 +0000238
Ted Kremenekb7ddd9b2009-11-13 01:54:21 +0000239 RetEffect::ObjKind getObjKind() const { return okind; }
Ted Kremenekdcee3ce2010-07-01 20:16:50 +0000240
Ted Kremenekb7ddd9b2009-11-13 01:54:21 +0000241 unsigned getCount() const { return Cnt; }
242 unsigned getAutoreleaseCount() const { return ACnt; }
243 unsigned getCombinedCounts() const { return Cnt + ACnt; }
244 void clearCounts() { Cnt = 0; ACnt = 0; }
245 void setCount(unsigned i) { Cnt = i; }
246 void setAutoreleaseCount(unsigned i) { ACnt = i; }
Ted Kremenekdcee3ce2010-07-01 20:16:50 +0000247
Ted Kremenekb7ddd9b2009-11-13 01:54:21 +0000248 QualType getType() const { return T; }
Ted Kremenekdcee3ce2010-07-01 20:16:50 +0000249
Ted Kremenekb7ddd9b2009-11-13 01:54:21 +0000250 bool isOwned() const {
251 return getKind() == Owned;
252 }
Ted Kremenekdcee3ce2010-07-01 20:16:50 +0000253
Ted Kremenekb7ddd9b2009-11-13 01:54:21 +0000254 bool isNotOwned() const {
255 return getKind() == NotOwned;
256 }
Ted Kremenekdcee3ce2010-07-01 20:16:50 +0000257
Ted Kremenekb7ddd9b2009-11-13 01:54:21 +0000258 bool isReturnedOwned() const {
259 return getKind() == ReturnedOwned;
260 }
Ted Kremenekdcee3ce2010-07-01 20:16:50 +0000261
Ted Kremenekb7ddd9b2009-11-13 01:54:21 +0000262 bool isReturnedNotOwned() const {
263 return getKind() == ReturnedNotOwned;
264 }
Ted Kremenekdcee3ce2010-07-01 20:16:50 +0000265
Ted Kremenekb7ddd9b2009-11-13 01:54:21 +0000266 static RefVal makeOwned(RetEffect::ObjKind o, QualType t,
267 unsigned Count = 1) {
268 return RefVal(Owned, o, Count, 0, t);
269 }
Ted Kremenekdcee3ce2010-07-01 20:16:50 +0000270
Ted Kremenekb7ddd9b2009-11-13 01:54:21 +0000271 static RefVal makeNotOwned(RetEffect::ObjKind o, QualType t,
272 unsigned Count = 0) {
273 return RefVal(NotOwned, o, Count, 0, t);
274 }
Ted Kremenekdcee3ce2010-07-01 20:16:50 +0000275
Ted Kremenekb7ddd9b2009-11-13 01:54:21 +0000276 // Comparison, profiling, and pretty-printing.
Ted Kremenekdcee3ce2010-07-01 20:16:50 +0000277
Ted Kremenekb7ddd9b2009-11-13 01:54:21 +0000278 bool operator==(const RefVal& X) const {
279 return kind == X.kind && Cnt == X.Cnt && T == X.T && ACnt == X.ACnt;
280 }
Ted Kremenekdcee3ce2010-07-01 20:16:50 +0000281
Ted Kremenekb7ddd9b2009-11-13 01:54:21 +0000282 RefVal operator-(size_t i) const {
283 return RefVal(getKind(), getObjKind(), getCount() - i,
284 getAutoreleaseCount(), getType());
285 }
Ted Kremenekdcee3ce2010-07-01 20:16:50 +0000286
Ted Kremenekb7ddd9b2009-11-13 01:54:21 +0000287 RefVal operator+(size_t i) const {
288 return RefVal(getKind(), getObjKind(), getCount() + i,
289 getAutoreleaseCount(), getType());
290 }
Ted Kremenekdcee3ce2010-07-01 20:16:50 +0000291
Ted Kremenekb7ddd9b2009-11-13 01:54:21 +0000292 RefVal operator^(Kind k) const {
293 return RefVal(k, getObjKind(), getCount(), getAutoreleaseCount(),
294 getType());
295 }
Ted Kremenekdcee3ce2010-07-01 20:16:50 +0000296
Ted Kremenekb7ddd9b2009-11-13 01:54:21 +0000297 RefVal autorelease() const {
298 return RefVal(getKind(), getObjKind(), getCount(), getAutoreleaseCount()+1,
299 getType());
300 }
Ted Kremenekdcee3ce2010-07-01 20:16:50 +0000301
Ted Kremenekb7ddd9b2009-11-13 01:54:21 +0000302 void Profile(llvm::FoldingSetNodeID& ID) const {
303 ID.AddInteger((unsigned) kind);
304 ID.AddInteger(Cnt);
305 ID.AddInteger(ACnt);
306 ID.Add(T);
307 }
Ted Kremenekdcee3ce2010-07-01 20:16:50 +0000308
Ted Kremenekb7ddd9b2009-11-13 01:54:21 +0000309 void print(llvm::raw_ostream& Out) const;
310};
311
312void RefVal::print(llvm::raw_ostream& Out) const {
313 if (!T.isNull())
314 Out << "Tracked Type:" << T.getAsString() << '\n';
Ted Kremenekdcee3ce2010-07-01 20:16:50 +0000315
Ted Kremenekb7ddd9b2009-11-13 01:54:21 +0000316 switch (getKind()) {
317 default: assert(false);
318 case Owned: {
319 Out << "Owned";
320 unsigned cnt = getCount();
321 if (cnt) Out << " (+ " << cnt << ")";
322 break;
323 }
Ted Kremenekdcee3ce2010-07-01 20:16:50 +0000324
Ted Kremenekb7ddd9b2009-11-13 01:54:21 +0000325 case NotOwned: {
326 Out << "NotOwned";
327 unsigned cnt = getCount();
328 if (cnt) Out << " (+ " << cnt << ")";
329 break;
330 }
Ted Kremenekdcee3ce2010-07-01 20:16:50 +0000331
Ted Kremenekb7ddd9b2009-11-13 01:54:21 +0000332 case ReturnedOwned: {
333 Out << "ReturnedOwned";
334 unsigned cnt = getCount();
335 if (cnt) Out << " (+ " << cnt << ")";
336 break;
337 }
Ted Kremenekdcee3ce2010-07-01 20:16:50 +0000338
Ted Kremenekb7ddd9b2009-11-13 01:54:21 +0000339 case ReturnedNotOwned: {
340 Out << "ReturnedNotOwned";
341 unsigned cnt = getCount();
342 if (cnt) Out << " (+ " << cnt << ")";
343 break;
344 }
Ted Kremenekdcee3ce2010-07-01 20:16:50 +0000345
Ted Kremenekb7ddd9b2009-11-13 01:54:21 +0000346 case Released:
347 Out << "Released";
348 break;
Ted Kremenekdcee3ce2010-07-01 20:16:50 +0000349
Ted Kremenekb7ddd9b2009-11-13 01:54:21 +0000350 case ErrorDeallocGC:
351 Out << "-dealloc (GC)";
352 break;
Ted Kremenekdcee3ce2010-07-01 20:16:50 +0000353
Ted Kremenekb7ddd9b2009-11-13 01:54:21 +0000354 case ErrorDeallocNotOwned:
355 Out << "-dealloc (not-owned)";
356 break;
Ted Kremenekdcee3ce2010-07-01 20:16:50 +0000357
Ted Kremenekb7ddd9b2009-11-13 01:54:21 +0000358 case ErrorLeak:
359 Out << "Leaked";
360 break;
Ted Kremenekdcee3ce2010-07-01 20:16:50 +0000361
Ted Kremenekb7ddd9b2009-11-13 01:54:21 +0000362 case ErrorLeakReturned:
363 Out << "Leaked (Bad naming)";
364 break;
Ted Kremenekdcee3ce2010-07-01 20:16:50 +0000365
Ted Kremenekb7ddd9b2009-11-13 01:54:21 +0000366 case ErrorGCLeakReturned:
367 Out << "Leaked (GC-ed at return)";
368 break;
Ted Kremenekdcee3ce2010-07-01 20:16:50 +0000369
Ted Kremenekb7ddd9b2009-11-13 01:54:21 +0000370 case ErrorUseAfterRelease:
371 Out << "Use-After-Release [ERROR]";
372 break;
Ted Kremenekdcee3ce2010-07-01 20:16:50 +0000373
Ted Kremenekb7ddd9b2009-11-13 01:54:21 +0000374 case ErrorReleaseNotOwned:
375 Out << "Release of Not-Owned [ERROR]";
376 break;
Ted Kremenekdcee3ce2010-07-01 20:16:50 +0000377
Ted Kremenekb7ddd9b2009-11-13 01:54:21 +0000378 case RefVal::ErrorOverAutorelease:
379 Out << "Over autoreleased";
380 break;
Ted Kremenekdcee3ce2010-07-01 20:16:50 +0000381
Ted Kremenekb7ddd9b2009-11-13 01:54:21 +0000382 case RefVal::ErrorReturnedNotOwned:
383 Out << "Non-owned object returned instead of owned";
384 break;
385 }
Ted Kremenekdcee3ce2010-07-01 20:16:50 +0000386
Ted Kremenekb7ddd9b2009-11-13 01:54:21 +0000387 if (ACnt) {
388 Out << " [ARC +" << ACnt << ']';
389 }
390}
391} //end anonymous namespace
392
393//===----------------------------------------------------------------------===//
394// RefBindings - State used to track object reference counts.
395//===----------------------------------------------------------------------===//
396
397typedef llvm::ImmutableMap<SymbolRef, RefVal> RefBindings;
Ted Kremenekb7ddd9b2009-11-13 01:54:21 +0000398
399namespace clang {
Ted Kremenek9ef65372010-12-23 07:20:52 +0000400namespace ento {
Ted Kremenekb7ddd9b2009-11-13 01:54:21 +0000401 template<>
402 struct GRStateTrait<RefBindings> : public GRStatePartialTrait<RefBindings> {
Ted Kremenekf0d8fff2009-11-13 01:58:01 +0000403 static void* GDMIndex() {
404 static int RefBIndex = 0;
405 return &RefBIndex;
406 }
Ted Kremenekb7ddd9b2009-11-13 01:54:21 +0000407 };
408}
Argyrios Kyrtzidis5a4f98f2010-12-22 18:53:20 +0000409}
Ted Kremenekb7ddd9b2009-11-13 01:54:21 +0000410
411//===----------------------------------------------------------------------===//
412// Summaries
413//===----------------------------------------------------------------------===//
414
415namespace {
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +0000416class RetainSummary {
Ted Kremenek1bffd742008-05-06 15:44:25 +0000417 /// Args - an ordered vector of (index, ArgEffect) pairs, where index
418 /// specifies the argument (starting from 0). This can be sparsely
419 /// populated; arguments with no entry in Args use 'DefaultArgEffect'.
Ted Kremenekb77449c2009-05-03 05:20:50 +0000420 ArgEffects Args;
Mike Stump1eb44332009-09-09 15:08:12 +0000421
Ted Kremenek1bffd742008-05-06 15:44:25 +0000422 /// DefaultArgEffect - The default ArgEffect to apply to arguments that
423 /// do not have an entry in Args.
424 ArgEffect DefaultArgEffect;
Mike Stump1eb44332009-09-09 15:08:12 +0000425
Ted Kremenek553cf182008-06-25 21:21:56 +0000426 /// Receiver - If this summary applies to an Objective-C message expression,
427 /// this is the effect applied to the state of the receiver.
Ted Kremenek3c0cea32008-05-06 02:26:56 +0000428 ArgEffect Receiver;
Mike Stump1eb44332009-09-09 15:08:12 +0000429
Ted Kremenek553cf182008-06-25 21:21:56 +0000430 /// Ret - The effect on the return value. Used to indicate if the
431 /// function/method call returns a new tracked symbol, returns an
432 /// alias of one of the arguments in the call, and so on.
Ted Kremenek6b3a0f72008-03-11 06:39:11 +0000433 RetEffect Ret;
Mike Stump1eb44332009-09-09 15:08:12 +0000434
Ted Kremenek70a733e2008-07-18 17:24:20 +0000435 /// EndPath - Indicates that execution of this method/function should
436 /// terminate the simulation of a path.
437 bool EndPath;
Mike Stump1eb44332009-09-09 15:08:12 +0000438
Ted Kremenek6b3a0f72008-03-11 06:39:11 +0000439public:
Ted Kremenekb77449c2009-05-03 05:20:50 +0000440 RetainSummary(ArgEffects A, RetEffect R, ArgEffect defaultEff,
Ted Kremenek70a733e2008-07-18 17:24:20 +0000441 ArgEffect ReceiverEff, bool endpath = false)
442 : Args(A), DefaultArgEffect(defaultEff), Receiver(ReceiverEff), Ret(R),
Mike Stump1eb44332009-09-09 15:08:12 +0000443 EndPath(endpath) {}
444
Ted Kremenek553cf182008-06-25 21:21:56 +0000445 /// getArg - Return the argument effect on the argument specified by
446 /// idx (starting from 0).
Ted Kremenek1ac08d62008-03-11 17:48:22 +0000447 ArgEffect getArg(unsigned idx) const {
Ted Kremenekb77449c2009-05-03 05:20:50 +0000448 if (const ArgEffect *AE = Args.lookup(idx))
449 return *AE;
Mike Stump1eb44332009-09-09 15:08:12 +0000450
Ted Kremenek1bffd742008-05-06 15:44:25 +0000451 return DefaultArgEffect;
Ted Kremenek1ac08d62008-03-11 17:48:22 +0000452 }
Mike Stump1eb44332009-09-09 15:08:12 +0000453
Ted Kremenek885c27b2009-05-04 05:31:22 +0000454 /// setDefaultArgEffect - Set the default argument effect.
455 void setDefaultArgEffect(ArgEffect E) {
456 DefaultArgEffect = E;
457 }
Mike Stump1eb44332009-09-09 15:08:12 +0000458
Ted Kremenek553cf182008-06-25 21:21:56 +0000459 /// getRetEffect - Returns the effect on the return value of the call.
Ted Kremenekb77449c2009-05-03 05:20:50 +0000460 RetEffect getRetEffect() const { return Ret; }
Mike Stump1eb44332009-09-09 15:08:12 +0000461
Ted Kremenek885c27b2009-05-04 05:31:22 +0000462 /// setRetEffect - Set the effect of the return value of the call.
463 void setRetEffect(RetEffect E) { Ret = E; }
Mike Stump1eb44332009-09-09 15:08:12 +0000464
Ted Kremenek70a733e2008-07-18 17:24:20 +0000465 /// isEndPath - Returns true if executing the given method/function should
466 /// terminate the path.
467 bool isEndPath() const { return EndPath; }
Mike Stump1eb44332009-09-09 15:08:12 +0000468
Ted Kremenek553cf182008-06-25 21:21:56 +0000469 /// getReceiverEffect - Returns the effect on the receiver of the call.
470 /// This is only meaningful if the summary applies to an ObjCMessageExpr*.
Ted Kremenekb77449c2009-05-03 05:20:50 +0000471 ArgEffect getReceiverEffect() const { return Receiver; }
Ted Kremenek6b3a0f72008-03-11 06:39:11 +0000472};
Ted Kremenek4f22a782008-06-23 23:30:29 +0000473} // end anonymous namespace
Ted Kremenek6b3a0f72008-03-11 06:39:11 +0000474
Ted Kremenek553cf182008-06-25 21:21:56 +0000475//===----------------------------------------------------------------------===//
476// Data structures for constructing summaries.
477//===----------------------------------------------------------------------===//
Ted Kremenek53301ba2008-06-24 03:49:48 +0000478
Ted Kremenek553cf182008-06-25 21:21:56 +0000479namespace {
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +0000480class ObjCSummaryKey {
Ted Kremenek553cf182008-06-25 21:21:56 +0000481 IdentifierInfo* II;
482 Selector S;
Mike Stump1eb44332009-09-09 15:08:12 +0000483public:
Ted Kremenek553cf182008-06-25 21:21:56 +0000484 ObjCSummaryKey(IdentifierInfo* ii, Selector s)
485 : II(ii), S(s) {}
486
Ted Kremeneka8833552009-04-29 23:03:22 +0000487 ObjCSummaryKey(const ObjCInterfaceDecl* d, Selector s)
Ted Kremenek553cf182008-06-25 21:21:56 +0000488 : II(d ? d->getIdentifier() : 0), S(s) {}
Ted Kremenek70b6a832009-05-13 18:16:01 +0000489
490 ObjCSummaryKey(const ObjCInterfaceDecl* d, IdentifierInfo *ii, Selector s)
491 : II(d ? d->getIdentifier() : ii), S(s) {}
Mike Stump1eb44332009-09-09 15:08:12 +0000492
Ted Kremenek553cf182008-06-25 21:21:56 +0000493 ObjCSummaryKey(Selector s)
494 : II(0), S(s) {}
Mike Stump1eb44332009-09-09 15:08:12 +0000495
Ted Kremenek553cf182008-06-25 21:21:56 +0000496 IdentifierInfo* getIdentifier() const { return II; }
497 Selector getSelector() const { return S; }
498};
Ted Kremenek4f22a782008-06-23 23:30:29 +0000499}
500
501namespace llvm {
Ted Kremenek553cf182008-06-25 21:21:56 +0000502template <> struct DenseMapInfo<ObjCSummaryKey> {
503 static inline ObjCSummaryKey getEmptyKey() {
504 return ObjCSummaryKey(DenseMapInfo<IdentifierInfo*>::getEmptyKey(),
505 DenseMapInfo<Selector>::getEmptyKey());
506 }
Mike Stump1eb44332009-09-09 15:08:12 +0000507
Ted Kremenek553cf182008-06-25 21:21:56 +0000508 static inline ObjCSummaryKey getTombstoneKey() {
509 return ObjCSummaryKey(DenseMapInfo<IdentifierInfo*>::getTombstoneKey(),
Mike Stump1eb44332009-09-09 15:08:12 +0000510 DenseMapInfo<Selector>::getTombstoneKey());
Ted Kremenek553cf182008-06-25 21:21:56 +0000511 }
Mike Stump1eb44332009-09-09 15:08:12 +0000512
Ted Kremenek553cf182008-06-25 21:21:56 +0000513 static unsigned getHashValue(const ObjCSummaryKey &V) {
514 return (DenseMapInfo<IdentifierInfo*>::getHashValue(V.getIdentifier())
Mike Stump1eb44332009-09-09 15:08:12 +0000515 & 0x88888888)
Ted Kremenek553cf182008-06-25 21:21:56 +0000516 | (DenseMapInfo<Selector>::getHashValue(V.getSelector())
517 & 0x55555555);
518 }
Mike Stump1eb44332009-09-09 15:08:12 +0000519
Ted Kremenek553cf182008-06-25 21:21:56 +0000520 static bool isEqual(const ObjCSummaryKey& LHS, const ObjCSummaryKey& RHS) {
521 return DenseMapInfo<IdentifierInfo*>::isEqual(LHS.getIdentifier(),
522 RHS.getIdentifier()) &&
523 DenseMapInfo<Selector>::isEqual(LHS.getSelector(),
524 RHS.getSelector());
525 }
Mike Stump1eb44332009-09-09 15:08:12 +0000526
Ted Kremenek553cf182008-06-25 21:21:56 +0000527};
Chris Lattner06159e82009-12-15 07:26:51 +0000528template <>
529struct isPodLike<ObjCSummaryKey> { static const bool value = true; };
Ted Kremenek4f22a782008-06-23 23:30:29 +0000530} // end llvm namespace
Mike Stump1eb44332009-09-09 15:08:12 +0000531
Ted Kremenek4f22a782008-06-23 23:30:29 +0000532namespace {
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +0000533class ObjCSummaryCache {
Ted Kremenek553cf182008-06-25 21:21:56 +0000534 typedef llvm::DenseMap<ObjCSummaryKey, RetainSummary*> MapTy;
535 MapTy M;
536public:
537 ObjCSummaryCache() {}
Mike Stump1eb44332009-09-09 15:08:12 +0000538
Ted Kremenek614cc542009-07-21 23:27:57 +0000539 RetainSummary* find(const ObjCInterfaceDecl* D, IdentifierInfo *ClsName,
Ted Kremeneka8833552009-04-29 23:03:22 +0000540 Selector S) {
Ted Kremenek8711c032009-04-29 05:04:30 +0000541 // Lookup the method using the decl for the class @interface. If we
542 // have no decl, lookup using the class name.
543 return D ? find(D, S) : find(ClsName, S);
544 }
Mike Stump1eb44332009-09-09 15:08:12 +0000545
546 RetainSummary* find(const ObjCInterfaceDecl* D, Selector S) {
Ted Kremenek553cf182008-06-25 21:21:56 +0000547 // Do a lookup with the (D,S) pair. If we find a match return
548 // the iterator.
549 ObjCSummaryKey K(D, S);
550 MapTy::iterator I = M.find(K);
Mike Stump1eb44332009-09-09 15:08:12 +0000551
Ted Kremenek553cf182008-06-25 21:21:56 +0000552 if (I != M.end() || !D)
Ted Kremenek614cc542009-07-21 23:27:57 +0000553 return I->second;
Mike Stump1eb44332009-09-09 15:08:12 +0000554
Ted Kremenek553cf182008-06-25 21:21:56 +0000555 // Walk the super chain. If we find a hit with a parent, we'll end
556 // up returning that summary. We actually allow that key (null,S), as
557 // we cache summaries for the null ObjCInterfaceDecl* to allow us to
558 // generate initial summaries without having to worry about NSObject
559 // being declared.
560 // FIXME: We may change this at some point.
561 for (ObjCInterfaceDecl* C=D->getSuperClass() ;; C=C->getSuperClass()) {
562 if ((I = M.find(ObjCSummaryKey(C, S))) != M.end())
563 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000564
Ted Kremenek553cf182008-06-25 21:21:56 +0000565 if (!C)
Ted Kremenek614cc542009-07-21 23:27:57 +0000566 return NULL;
Ted Kremenek553cf182008-06-25 21:21:56 +0000567 }
Mike Stump1eb44332009-09-09 15:08:12 +0000568
569 // Cache the summary with original key to make the next lookup faster
Ted Kremenek553cf182008-06-25 21:21:56 +0000570 // and return the iterator.
Ted Kremenek614cc542009-07-21 23:27:57 +0000571 RetainSummary *Summ = I->second;
572 M[K] = Summ;
573 return Summ;
Ted Kremenek553cf182008-06-25 21:21:56 +0000574 }
Mike Stump1eb44332009-09-09 15:08:12 +0000575
Ted Kremenek614cc542009-07-21 23:27:57 +0000576 RetainSummary* find(IdentifierInfo* II, Selector S) {
Ted Kremenek553cf182008-06-25 21:21:56 +0000577 // FIXME: Class method lookup. Right now we dont' have a good way
578 // of going between IdentifierInfo* and the class hierarchy.
Ted Kremenek614cc542009-07-21 23:27:57 +0000579 MapTy::iterator I = M.find(ObjCSummaryKey(II, S));
Mike Stump1eb44332009-09-09 15:08:12 +0000580
Ted Kremenek614cc542009-07-21 23:27:57 +0000581 if (I == M.end())
582 I = M.find(ObjCSummaryKey(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000583
Ted Kremenek614cc542009-07-21 23:27:57 +0000584 return I == M.end() ? NULL : I->second;
Ted Kremenek553cf182008-06-25 21:21:56 +0000585 }
Mike Stump1eb44332009-09-09 15:08:12 +0000586
Ted Kremenek553cf182008-06-25 21:21:56 +0000587 RetainSummary*& operator[](ObjCSummaryKey K) {
588 return M[K];
589 }
Mike Stump1eb44332009-09-09 15:08:12 +0000590
Ted Kremenek553cf182008-06-25 21:21:56 +0000591 RetainSummary*& operator[](Selector S) {
592 return M[ ObjCSummaryKey(S) ];
593 }
Mike Stump1eb44332009-09-09 15:08:12 +0000594};
Ted Kremenek553cf182008-06-25 21:21:56 +0000595} // end anonymous namespace
596
597//===----------------------------------------------------------------------===//
598// Data structures for managing collections of summaries.
599//===----------------------------------------------------------------------===//
600
601namespace {
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +0000602class RetainSummaryManager {
Ted Kremenekd3dbcf42008-05-05 22:11:16 +0000603
604 //==-----------------------------------------------------------------==//
605 // Typedefs.
606 //==-----------------------------------------------------------------==//
Mike Stump1eb44332009-09-09 15:08:12 +0000607
Zhongxing Xubc5495b2010-07-20 02:56:49 +0000608 typedef llvm::DenseMap<const FunctionDecl*, RetainSummary*>
Ted Kremenekd3dbcf42008-05-05 22:11:16 +0000609 FuncSummariesTy;
Mike Stump1eb44332009-09-09 15:08:12 +0000610
Ted Kremenek4f22a782008-06-23 23:30:29 +0000611 typedef ObjCSummaryCache ObjCMethodSummariesTy;
Mike Stump1eb44332009-09-09 15:08:12 +0000612
Ted Kremenekd3dbcf42008-05-05 22:11:16 +0000613 //==-----------------------------------------------------------------==//
614 // Data.
615 //==-----------------------------------------------------------------==//
Mike Stump1eb44332009-09-09 15:08:12 +0000616
Ted Kremenek553cf182008-06-25 21:21:56 +0000617 /// Ctx - The ASTContext object for the analyzed ASTs.
Ted Kremenek377e2302008-04-29 05:33:51 +0000618 ASTContext& Ctx;
Ted Kremenek179064e2008-07-01 17:21:27 +0000619
Ted Kremenek070a8252008-07-09 18:11:16 +0000620 /// CFDictionaryCreateII - An IdentifierInfo* representing the indentifier
621 /// "CFDictionaryCreate".
622 IdentifierInfo* CFDictionaryCreateII;
Mike Stump1eb44332009-09-09 15:08:12 +0000623
Ted Kremenek553cf182008-06-25 21:21:56 +0000624 /// GCEnabled - Records whether or not the analyzed code runs in GC mode.
Ted Kremenek377e2302008-04-29 05:33:51 +0000625 const bool GCEnabled;
Mike Stump1eb44332009-09-09 15:08:12 +0000626
Ted Kremenek553cf182008-06-25 21:21:56 +0000627 /// FuncSummaries - A map from FunctionDecls to summaries.
Mike Stump1eb44332009-09-09 15:08:12 +0000628 FuncSummariesTy FuncSummaries;
629
Ted Kremenek553cf182008-06-25 21:21:56 +0000630 /// ObjCClassMethodSummaries - A map from selectors (for instance methods)
631 /// to summaries.
Ted Kremenek1f180c32008-06-23 22:21:20 +0000632 ObjCMethodSummariesTy ObjCClassMethodSummaries;
Ted Kremenekd3dbcf42008-05-05 22:11:16 +0000633
Ted Kremenek553cf182008-06-25 21:21:56 +0000634 /// ObjCMethodSummaries - A map from selectors to summaries.
Ted Kremenek1f180c32008-06-23 22:21:20 +0000635 ObjCMethodSummariesTy ObjCMethodSummaries;
Ted Kremenekd3dbcf42008-05-05 22:11:16 +0000636
Ted Kremenek553cf182008-06-25 21:21:56 +0000637 /// BPAlloc - A BumpPtrAllocator used for allocating summaries, ArgEffects,
638 /// and all other data used by the checker.
Ted Kremenekd3dbcf42008-05-05 22:11:16 +0000639 llvm::BumpPtrAllocator BPAlloc;
Mike Stump1eb44332009-09-09 15:08:12 +0000640
Ted Kremenekb77449c2009-05-03 05:20:50 +0000641 /// AF - A factory for ArgEffects objects.
Mike Stump1eb44332009-09-09 15:08:12 +0000642 ArgEffects::Factory AF;
643
Ted Kremenek553cf182008-06-25 21:21:56 +0000644 /// ScratchArgs - A holding buffer for construct ArgEffects.
Ted Kremenekd3dbcf42008-05-05 22:11:16 +0000645 ArgEffects ScratchArgs;
Mike Stump1eb44332009-09-09 15:08:12 +0000646
Ted Kremenekec315332009-05-07 23:40:42 +0000647 /// ObjCAllocRetE - Default return effect for methods returning Objective-C
648 /// objects.
649 RetEffect ObjCAllocRetE;
Ted Kremenek547d4952009-06-05 23:18:01 +0000650
Mike Stump1eb44332009-09-09 15:08:12 +0000651 /// ObjCInitRetE - Default return effect for init methods returning
Ted Kremenekac02f202009-08-20 05:13:36 +0000652 /// Objective-C objects.
Ted Kremenek547d4952009-06-05 23:18:01 +0000653 RetEffect ObjCInitRetE;
Mike Stump1eb44332009-09-09 15:08:12 +0000654
Ted Kremenek7faca822009-05-04 04:57:00 +0000655 RetainSummary DefaultSummary;
Ted Kremenek432af592008-05-06 18:11:36 +0000656 RetainSummary* StopSummary;
Mike Stump1eb44332009-09-09 15:08:12 +0000657
Ted Kremenekd3dbcf42008-05-05 22:11:16 +0000658 //==-----------------------------------------------------------------==//
659 // Methods.
660 //==-----------------------------------------------------------------==//
Mike Stump1eb44332009-09-09 15:08:12 +0000661
Ted Kremenek553cf182008-06-25 21:21:56 +0000662 /// getArgEffects - Returns a persistent ArgEffects object based on the
663 /// data in ScratchArgs.
Ted Kremenekb77449c2009-05-03 05:20:50 +0000664 ArgEffects getArgEffects();
Ted Kremenek6b3a0f72008-03-11 06:39:11 +0000665
Mike Stump1eb44332009-09-09 15:08:12 +0000666 enum UnaryFuncKind { cfretain, cfrelease, cfmakecollectable };
667
Ted Kremenek896cd9d2008-10-23 01:56:15 +0000668public:
Ted Kremenek78a35a32009-05-12 20:06:54 +0000669 RetEffect getObjAllocRetEffect() const { return ObjCAllocRetE; }
670
Ted Kremenek885c27b2009-05-04 05:31:22 +0000671 RetainSummary *getDefaultSummary() {
672 RetainSummary *Summ = (RetainSummary*) BPAlloc.Allocate<RetainSummary>();
673 return new (Summ) RetainSummary(DefaultSummary);
674 }
Mike Stump1eb44332009-09-09 15:08:12 +0000675
Ted Kremenek6ad315a2009-02-23 16:51:39 +0000676 RetainSummary* getUnarySummary(const FunctionType* FT, UnaryFuncKind func);
Mike Stump1eb44332009-09-09 15:08:12 +0000677
Zhongxing Xubc5495b2010-07-20 02:56:49 +0000678 RetainSummary* getCFSummaryCreateRule(const FunctionDecl* FD);
679 RetainSummary* getCFSummaryGetRule(const FunctionDecl* FD);
680 RetainSummary* getCFCreateGetRuleSummary(const FunctionDecl* FD,
681 StringRef FName);
Mike Stump1eb44332009-09-09 15:08:12 +0000682
Ted Kremenekb77449c2009-05-03 05:20:50 +0000683 RetainSummary* getPersistentSummary(ArgEffects AE, RetEffect RetEff,
Ted Kremenek1bffd742008-05-06 15:44:25 +0000684 ArgEffect ReceiverEff = DoNothing,
Ted Kremenek70a733e2008-07-18 17:24:20 +0000685 ArgEffect DefaultEff = MayEscape,
686 bool isEndPath = false);
Ted Kremenek706522f2008-10-29 04:07:07 +0000687
Ted Kremenek3c0cea32008-05-06 02:26:56 +0000688 RetainSummary* getPersistentSummary(RetEffect RE,
Ted Kremenek1bffd742008-05-06 15:44:25 +0000689 ArgEffect ReceiverEff = DoNothing,
Ted Kremenek3eabf1c2008-05-22 17:31:13 +0000690 ArgEffect DefaultEff = MayEscape) {
Ted Kremenek1bffd742008-05-06 15:44:25 +0000691 return getPersistentSummary(getArgEffects(), RE, ReceiverEff, DefaultEff);
Ted Kremenek9c32d082008-05-06 00:30:21 +0000692 }
Mike Stump1eb44332009-09-09 15:08:12 +0000693
Ted Kremenek8711c032009-04-29 05:04:30 +0000694 RetainSummary *getPersistentStopSummary() {
Ted Kremenek432af592008-05-06 18:11:36 +0000695 if (StopSummary)
696 return StopSummary;
Mike Stump1eb44332009-09-09 15:08:12 +0000697
Ted Kremenek432af592008-05-06 18:11:36 +0000698 StopSummary = getPersistentSummary(RetEffect::MakeNoRet(),
699 StopTracking, StopTracking);
Ted Kremenek706522f2008-10-29 04:07:07 +0000700
Ted Kremenek432af592008-05-06 18:11:36 +0000701 return StopSummary;
Mike Stump1eb44332009-09-09 15:08:12 +0000702 }
Ted Kremenekb3095252008-05-06 04:20:12 +0000703
Ted Kremenek8711c032009-04-29 05:04:30 +0000704 RetainSummary *getInitMethodSummary(QualType RetTy);
Ted Kremenek46e49ee2008-05-05 23:55:01 +0000705
Ted Kremenek1f180c32008-06-23 22:21:20 +0000706 void InitializeClassMethodSummaries();
707 void InitializeMethodSummaries();
Ted Kremenek896cd9d2008-10-23 01:56:15 +0000708private:
Ted Kremenek553cf182008-06-25 21:21:56 +0000709 void addNSObjectClsMethSummary(Selector S, RetainSummary *Summ) {
710 ObjCClassMethodSummaries[S] = Summ;
711 }
Mike Stump1eb44332009-09-09 15:08:12 +0000712
Ted Kremenek553cf182008-06-25 21:21:56 +0000713 void addNSObjectMethSummary(Selector S, RetainSummary *Summ) {
714 ObjCMethodSummaries[S] = Summ;
715 }
Ted Kremenek3aa7ecd2009-03-04 23:30:42 +0000716
717 void addClassMethSummary(const char* Cls, const char* nullaryName,
718 RetainSummary *Summ) {
719 IdentifierInfo* ClsII = &Ctx.Idents.get(Cls);
720 Selector S = GetNullarySelector(nullaryName, Ctx);
721 ObjCClassMethodSummaries[ObjCSummaryKey(ClsII, S)] = Summ;
722 }
Mike Stump1eb44332009-09-09 15:08:12 +0000723
Ted Kremenek6c4becb2009-02-25 02:54:57 +0000724 void addInstMethSummary(const char* Cls, const char* nullaryName,
725 RetainSummary *Summ) {
726 IdentifierInfo* ClsII = &Ctx.Idents.get(Cls);
727 Selector S = GetNullarySelector(nullaryName, Ctx);
728 ObjCMethodSummaries[ObjCSummaryKey(ClsII, S)] = Summ;
729 }
Mike Stump1eb44332009-09-09 15:08:12 +0000730
Ted Kremenekde4d5332009-04-24 17:50:11 +0000731 Selector generateSelector(va_list argp) {
Ted Kremenek9e476de2008-08-12 18:30:56 +0000732 llvm::SmallVector<IdentifierInfo*, 10> II;
Ted Kremenekde4d5332009-04-24 17:50:11 +0000733
Ted Kremenek9e476de2008-08-12 18:30:56 +0000734 while (const char* s = va_arg(argp, const char*))
735 II.push_back(&Ctx.Idents.get(s));
Ted Kremenekde4d5332009-04-24 17:50:11 +0000736
Mike Stump1eb44332009-09-09 15:08:12 +0000737 return Ctx.Selectors.getSelector(II.size(), &II[0]);
Ted Kremenekde4d5332009-04-24 17:50:11 +0000738 }
Mike Stump1eb44332009-09-09 15:08:12 +0000739
Ted Kremenekde4d5332009-04-24 17:50:11 +0000740 void addMethodSummary(IdentifierInfo *ClsII, ObjCMethodSummariesTy& Summaries,
741 RetainSummary* Summ, va_list argp) {
742 Selector S = generateSelector(argp);
743 Summaries[ObjCSummaryKey(ClsII, S)] = Summ;
Ted Kremenek70a733e2008-07-18 17:24:20 +0000744 }
Mike Stump1eb44332009-09-09 15:08:12 +0000745
Ted Kremenekaf9dc272008-08-12 18:48:50 +0000746 void addInstMethSummary(const char* Cls, RetainSummary* Summ, ...) {
747 va_list argp;
748 va_start(argp, Summ);
Ted Kremenekde4d5332009-04-24 17:50:11 +0000749 addMethodSummary(&Ctx.Idents.get(Cls), ObjCMethodSummaries, Summ, argp);
Mike Stump1eb44332009-09-09 15:08:12 +0000750 va_end(argp);
Ted Kremenekaf9dc272008-08-12 18:48:50 +0000751 }
Mike Stump1eb44332009-09-09 15:08:12 +0000752
Ted Kremenekde4d5332009-04-24 17:50:11 +0000753 void addClsMethSummary(const char* Cls, RetainSummary* Summ, ...) {
754 va_list argp;
755 va_start(argp, Summ);
756 addMethodSummary(&Ctx.Idents.get(Cls),ObjCClassMethodSummaries, Summ, argp);
757 va_end(argp);
758 }
Mike Stump1eb44332009-09-09 15:08:12 +0000759
Ted Kremenekde4d5332009-04-24 17:50:11 +0000760 void addClsMethSummary(IdentifierInfo *II, RetainSummary* Summ, ...) {
761 va_list argp;
762 va_start(argp, Summ);
763 addMethodSummary(II, ObjCClassMethodSummaries, Summ, argp);
764 va_end(argp);
765 }
766
Ted Kremenek9e476de2008-08-12 18:30:56 +0000767 void addPanicSummary(const char* Cls, ...) {
Ted Kremenek3baf6722010-11-24 00:54:37 +0000768 RetainSummary* Summ = getPersistentSummary(AF.getEmptyMap(),
Ted Kremenekb77449c2009-05-03 05:20:50 +0000769 RetEffect::MakeNoRet(),
Ted Kremenek9e476de2008-08-12 18:30:56 +0000770 DoNothing, DoNothing, true);
771 va_list argp;
772 va_start (argp, Cls);
Ted Kremenekde4d5332009-04-24 17:50:11 +0000773 addMethodSummary(&Ctx.Idents.get(Cls), ObjCMethodSummaries, Summ, argp);
Ted Kremenek9e476de2008-08-12 18:30:56 +0000774 va_end(argp);
Ted Kremenekde4d5332009-04-24 17:50:11 +0000775 }
Mike Stump1eb44332009-09-09 15:08:12 +0000776
Ted Kremenek6b3a0f72008-03-11 06:39:11 +0000777public:
Mike Stump1eb44332009-09-09 15:08:12 +0000778
Ted Kremenekd3dbcf42008-05-05 22:11:16 +0000779 RetainSummaryManager(ASTContext& ctx, bool gcenabled)
Ted Kremenek179064e2008-07-01 17:21:27 +0000780 : Ctx(ctx),
Ted Kremenek070a8252008-07-09 18:11:16 +0000781 CFDictionaryCreateII(&ctx.Idents.get("CFDictionaryCreate")),
Ted Kremenek3baf6722010-11-24 00:54:37 +0000782 GCEnabled(gcenabled), AF(BPAlloc), ScratchArgs(AF.getEmptyMap()),
Ted Kremenekec315332009-05-07 23:40:42 +0000783 ObjCAllocRetE(gcenabled ? RetEffect::MakeGCNotOwned()
784 : RetEffect::MakeOwned(RetEffect::ObjC, true)),
Ted Kremenekb04cb592009-06-11 18:17:24 +0000785 ObjCInitRetE(gcenabled ? RetEffect::MakeGCNotOwned()
786 : RetEffect::MakeOwnedWhenTrackedReceiver()),
Ted Kremenek3baf6722010-11-24 00:54:37 +0000787 DefaultSummary(AF.getEmptyMap() /* per-argument effects (none) */,
Ted Kremenek7faca822009-05-04 04:57:00 +0000788 RetEffect::MakeNoRet() /* return effect */,
Ted Kremenekebd5a2d2009-05-11 18:30:24 +0000789 MayEscape, /* default argument effect */
790 DoNothing /* receiver effect */),
Ted Kremenekb77449c2009-05-03 05:20:50 +0000791 StopSummary(0) {
Ted Kremenek553cf182008-06-25 21:21:56 +0000792
793 InitializeClassMethodSummaries();
794 InitializeMethodSummaries();
795 }
Mike Stump1eb44332009-09-09 15:08:12 +0000796
Ted Kremenekd3dbcf42008-05-05 22:11:16 +0000797 ~RetainSummaryManager();
Mike Stump1eb44332009-09-09 15:08:12 +0000798
Zhongxing Xubc5495b2010-07-20 02:56:49 +0000799 RetainSummary* getSummary(const FunctionDecl* FD);
Mike Stump1eb44332009-09-09 15:08:12 +0000800
Ted Kremenekb7ddd9b2009-11-13 01:54:21 +0000801 RetainSummary *getInstanceMethodSummary(const ObjCMessageExpr *ME,
802 const GRState *state,
803 const LocationContext *LC);
Ted Kremenekdcee3ce2010-07-01 20:16:50 +0000804
Ted Kremenekb7ddd9b2009-11-13 01:54:21 +0000805 RetainSummary* getInstanceMethodSummary(const ObjCMessageExpr* ME,
Ted Kremeneka8833552009-04-29 23:03:22 +0000806 const ObjCInterfaceDecl* ID) {
Douglas Gregor04badcf2010-04-21 00:45:42 +0000807 return getInstanceMethodSummary(ME->getSelector(), 0,
Mike Stump1eb44332009-09-09 15:08:12 +0000808 ID, ME->getMethodDecl(), ME->getType());
Ted Kremenek8711c032009-04-29 05:04:30 +0000809 }
Mike Stump1eb44332009-09-09 15:08:12 +0000810
Ted Kremenekce8a41d2009-04-29 17:09:14 +0000811 RetainSummary* getInstanceMethodSummary(Selector S, IdentifierInfo *ClsName,
Ted Kremeneka8833552009-04-29 23:03:22 +0000812 const ObjCInterfaceDecl* ID,
813 const ObjCMethodDecl *MD,
814 QualType RetTy);
Ted Kremenekfcd7c6f2009-04-29 00:42:39 +0000815
816 RetainSummary *getClassMethodSummary(Selector S, IdentifierInfo *ClsName,
Ted Kremeneka8833552009-04-29 23:03:22 +0000817 const ObjCInterfaceDecl *ID,
818 const ObjCMethodDecl *MD,
819 QualType RetTy);
Mike Stump1eb44332009-09-09 15:08:12 +0000820
Ted Kremenekb7ddd9b2009-11-13 01:54:21 +0000821 RetainSummary *getClassMethodSummary(const ObjCMessageExpr *ME) {
Douglas Gregor04badcf2010-04-21 00:45:42 +0000822 ObjCInterfaceDecl *Class = 0;
823 switch (ME->getReceiverKind()) {
824 case ObjCMessageExpr::Class:
825 case ObjCMessageExpr::SuperClass:
826 Class = ME->getReceiverInterface();
827 break;
828
829 case ObjCMessageExpr::Instance:
830 case ObjCMessageExpr::SuperInstance:
831 break;
832 }
833
Ted Kremenekdcee3ce2010-07-01 20:16:50 +0000834 return getClassMethodSummary(ME->getSelector(),
Douglas Gregor04badcf2010-04-21 00:45:42 +0000835 Class? Class->getIdentifier() : 0,
836 Class,
Ted Kremenekfcd7c6f2009-04-29 00:42:39 +0000837 ME->getMethodDecl(), ME->getType());
838 }
Ted Kremenek552333c2009-04-29 17:17:48 +0000839
840 /// getMethodSummary - This version of getMethodSummary is used to query
841 /// the summary for the current method being analyzed.
Ted Kremeneka8833552009-04-29 23:03:22 +0000842 RetainSummary *getMethodSummary(const ObjCMethodDecl *MD) {
843 // FIXME: Eventually this should be unneeded.
Ted Kremeneka8833552009-04-29 23:03:22 +0000844 const ObjCInterfaceDecl *ID = MD->getClassInterface();
Ted Kremenek70a65762009-04-30 05:41:14 +0000845 Selector S = MD->getSelector();
Ted Kremenek552333c2009-04-29 17:17:48 +0000846 IdentifierInfo *ClsName = ID->getIdentifier();
847 QualType ResultTy = MD->getResultType();
Mike Stump1eb44332009-09-09 15:08:12 +0000848
849 // Resolve the method decl last.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000850 if (const ObjCMethodDecl *InterfaceMD = ResolveToInterfaceMethodDecl(MD))
Ted Kremenek76a50e32009-04-30 05:47:23 +0000851 MD = InterfaceMD;
Mike Stump1eb44332009-09-09 15:08:12 +0000852
Ted Kremenek552333c2009-04-29 17:17:48 +0000853 if (MD->isInstanceMethod())
854 return getInstanceMethodSummary(S, ClsName, ID, MD, ResultTy);
855 else
856 return getClassMethodSummary(S, ClsName, ID, MD, ResultTy);
857 }
Mike Stump1eb44332009-09-09 15:08:12 +0000858
Ted Kremeneka8833552009-04-29 23:03:22 +0000859 RetainSummary* getCommonMethodSummary(const ObjCMethodDecl* MD,
860 Selector S, QualType RetTy);
861
Ted Kremenek4dd8fb42009-05-09 02:58:13 +0000862 void updateSummaryFromAnnotations(RetainSummary &Summ,
863 const ObjCMethodDecl *MD);
864
865 void updateSummaryFromAnnotations(RetainSummary &Summ,
866 const FunctionDecl *FD);
867
Ted Kremenekd3dbcf42008-05-05 22:11:16 +0000868 bool isGCEnabled() const { return GCEnabled; }
Mike Stump1eb44332009-09-09 15:08:12 +0000869
Ted Kremenek885c27b2009-05-04 05:31:22 +0000870 RetainSummary *copySummary(RetainSummary *OldSumm) {
871 RetainSummary *Summ = (RetainSummary*) BPAlloc.Allocate<RetainSummary>();
872 new (Summ) RetainSummary(*OldSumm);
873 return Summ;
Mike Stump1eb44332009-09-09 15:08:12 +0000874 }
Ted Kremenek6b3a0f72008-03-11 06:39:11 +0000875};
Mike Stump1eb44332009-09-09 15:08:12 +0000876
Ted Kremenek6b3a0f72008-03-11 06:39:11 +0000877} // end anonymous namespace
878
879//===----------------------------------------------------------------------===//
880// Implementation of checker data structures.
881//===----------------------------------------------------------------------===//
882
Ted Kremenekb77449c2009-05-03 05:20:50 +0000883RetainSummaryManager::~RetainSummaryManager() {}
Ted Kremenek6b3a0f72008-03-11 06:39:11 +0000884
Ted Kremenekb77449c2009-05-03 05:20:50 +0000885ArgEffects RetainSummaryManager::getArgEffects() {
886 ArgEffects AE = ScratchArgs;
Ted Kremenek3baf6722010-11-24 00:54:37 +0000887 ScratchArgs = AF.getEmptyMap();
Ted Kremenekb77449c2009-05-03 05:20:50 +0000888 return AE;
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000889}
890
Ted Kremenek3c0cea32008-05-06 02:26:56 +0000891RetainSummary*
Ted Kremenekb77449c2009-05-03 05:20:50 +0000892RetainSummaryManager::getPersistentSummary(ArgEffects AE, RetEffect RetEff,
Ted Kremenek1bffd742008-05-06 15:44:25 +0000893 ArgEffect ReceiverEff,
Ted Kremenek70a733e2008-07-18 17:24:20 +0000894 ArgEffect DefaultEff,
Mike Stump1eb44332009-09-09 15:08:12 +0000895 bool isEndPath) {
Ted Kremenek891d5cc2008-04-24 17:22:33 +0000896 // Create the summary and return it.
Ted Kremenek22fe2482009-05-04 04:30:18 +0000897 RetainSummary *Summ = (RetainSummary*) BPAlloc.Allocate<RetainSummary>();
Ted Kremenek70a733e2008-07-18 17:24:20 +0000898 new (Summ) RetainSummary(AE, RetEff, DefaultEff, ReceiverEff, isEndPath);
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000899 return Summ;
900}
901
Ted Kremenekd3dbcf42008-05-05 22:11:16 +0000902//===----------------------------------------------------------------------===//
903// Summary creation for functions (largely uses of Core Foundation).
904//===----------------------------------------------------------------------===//
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000905
Zhongxing Xubc5495b2010-07-20 02:56:49 +0000906static bool isRetain(const FunctionDecl* FD, StringRef FName) {
Benjamin Kramerb6f3c702010-02-08 18:38:55 +0000907 return FName.endswith("Retain");
Ted Kremenek12619382009-01-12 21:45:02 +0000908}
909
Zhongxing Xubc5495b2010-07-20 02:56:49 +0000910static bool isRelease(const FunctionDecl* FD, StringRef FName) {
Benjamin Kramerb6f3c702010-02-08 18:38:55 +0000911 return FName.endswith("Release");
Ted Kremenek12619382009-01-12 21:45:02 +0000912}
913
Zhongxing Xubc5495b2010-07-20 02:56:49 +0000914RetainSummary* RetainSummaryManager::getSummary(const FunctionDecl* FD) {
Ted Kremenek891d5cc2008-04-24 17:22:33 +0000915 // Look up a summary in our cache of FunctionDecls -> Summaries.
Ted Kremenekd3dbcf42008-05-05 22:11:16 +0000916 FuncSummariesTy::iterator I = FuncSummaries.find(FD);
Ted Kremenekd3dbcf42008-05-05 22:11:16 +0000917 if (I != FuncSummaries.end())
Ted Kremenek891d5cc2008-04-24 17:22:33 +0000918 return I->second;
919
Ted Kremeneke401a0c2009-05-04 15:34:07 +0000920 // No summary? Generate one.
Ted Kremenek12619382009-01-12 21:45:02 +0000921 RetainSummary *S = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000922
Ted Kremenek37d785b2008-07-15 16:50:12 +0000923 do {
Ted Kremenek12619382009-01-12 21:45:02 +0000924 // We generate "stop" summaries for implicitly defined functions.
925 if (FD->isImplicit()) {
926 S = getPersistentStopSummary();
927 break;
Ted Kremenek37d785b2008-07-15 16:50:12 +0000928 }
Mike Stump1eb44332009-09-09 15:08:12 +0000929
John McCall183700f2009-09-21 23:43:11 +0000930 // [PR 3337] Use 'getAs<FunctionType>' to strip away any typedefs on the
Ted Kremenek99890652009-01-16 18:40:33 +0000931 // function's type.
John McCall183700f2009-09-21 23:43:11 +0000932 const FunctionType* FT = FD->getType()->getAs<FunctionType>();
Ted Kremenek48c6d182009-12-16 06:06:43 +0000933 const IdentifierInfo *II = FD->getIdentifier();
934 if (!II)
935 break;
Benjamin Kramerb6f3c702010-02-08 18:38:55 +0000936
937 StringRef FName = II->getName();
Mike Stump1eb44332009-09-09 15:08:12 +0000938
Ted Kremenekbf0a4dd2009-03-05 22:11:14 +0000939 // Strip away preceding '_'. Doing this here will effect all the checks
940 // down below.
Benjamin Kramerb6f3c702010-02-08 18:38:55 +0000941 FName = FName.substr(FName.find_first_not_of('_'));
Mike Stump1eb44332009-09-09 15:08:12 +0000942
Ted Kremenek12619382009-01-12 21:45:02 +0000943 // Inspect the result type.
944 QualType RetTy = FT->getResultType();
Mike Stump1eb44332009-09-09 15:08:12 +0000945
Ted Kremenek12619382009-01-12 21:45:02 +0000946 // FIXME: This should all be refactored into a chain of "summary lookup"
947 // filters.
Ted Kremenek008636a2009-10-14 00:27:24 +0000948 assert(ScratchArgs.isEmpty());
Ted Kremenek39d88b02009-06-15 20:36:07 +0000949
Benjamin Kramerb6f3c702010-02-08 18:38:55 +0000950 if (FName == "pthread_create") {
951 // Part of: <rdar://problem/7299394>. This will be addressed
952 // better with IPA.
953 S = getPersistentStopSummary();
954 } else if (FName == "NSMakeCollectable") {
955 // Handle: id NSMakeCollectable(CFTypeRef)
956 S = (RetTy->isObjCIdType())
957 ? getUnarySummary(FT, cfmakecollectable)
958 : getPersistentStopSummary();
959 } else if (FName == "IOBSDNameMatching" ||
960 FName == "IOServiceMatching" ||
961 FName == "IOServiceNameMatching" ||
962 FName == "IORegistryEntryIDMatching" ||
963 FName == "IOOpenFirmwarePathMatching") {
964 // Part of <rdar://problem/6961230>. (IOKit)
965 // This should be addressed using a API table.
966 S = getPersistentSummary(RetEffect::MakeOwned(RetEffect::CF, true),
967 DoNothing, DoNothing);
968 } else if (FName == "IOServiceGetMatchingService" ||
969 FName == "IOServiceGetMatchingServices") {
970 // FIXES: <rdar://problem/6326900>
971 // This should be addressed using a API table. This strcmp is also
972 // a little gross, but there is no need to super optimize here.
Ted Kremenek3baf6722010-11-24 00:54:37 +0000973 ScratchArgs = AF.add(ScratchArgs, 1, DecRef);
Benjamin Kramerb6f3c702010-02-08 18:38:55 +0000974 S = getPersistentSummary(RetEffect::MakeNoRet(), DoNothing, DoNothing);
975 } else if (FName == "IOServiceAddNotification" ||
976 FName == "IOServiceAddMatchingNotification") {
977 // Part of <rdar://problem/6961230>. (IOKit)
978 // This should be addressed using a API table.
Ted Kremenek3baf6722010-11-24 00:54:37 +0000979 ScratchArgs = AF.add(ScratchArgs, 2, DecRef);
Benjamin Kramerb6f3c702010-02-08 18:38:55 +0000980 S = getPersistentSummary(RetEffect::MakeNoRet(), DoNothing, DoNothing);
981 } else if (FName == "CVPixelBufferCreateWithBytes") {
982 // FIXES: <rdar://problem/7283567>
983 // Eventually this can be improved by recognizing that the pixel
984 // buffer passed to CVPixelBufferCreateWithBytes is released via
985 // a callback and doing full IPA to make sure this is done correctly.
986 // FIXME: This function has an out parameter that returns an
987 // allocated object.
Ted Kremenek3baf6722010-11-24 00:54:37 +0000988 ScratchArgs = AF.add(ScratchArgs, 7, StopTracking);
Benjamin Kramerb6f3c702010-02-08 18:38:55 +0000989 S = getPersistentSummary(RetEffect::MakeNoRet(), DoNothing, DoNothing);
990 } else if (FName == "CGBitmapContextCreateWithData") {
991 // FIXES: <rdar://problem/7358899>
992 // Eventually this can be improved by recognizing that 'releaseInfo'
993 // passed to CGBitmapContextCreateWithData is released via
994 // a callback and doing full IPA to make sure this is done correctly.
Ted Kremenek3baf6722010-11-24 00:54:37 +0000995 ScratchArgs = AF.add(ScratchArgs, 8, StopTracking);
Benjamin Kramerb6f3c702010-02-08 18:38:55 +0000996 S = getPersistentSummary(RetEffect::MakeOwned(RetEffect::CF, true),
997 DoNothing, DoNothing);
998 } else if (FName == "CVPixelBufferCreateWithPlanarBytes") {
999 // FIXES: <rdar://problem/7283567>
1000 // Eventually this can be improved by recognizing that the pixel
1001 // buffer passed to CVPixelBufferCreateWithPlanarBytes is released
1002 // via a callback and doing full IPA to make sure this is done
1003 // correctly.
Ted Kremenek3baf6722010-11-24 00:54:37 +00001004 ScratchArgs = AF.add(ScratchArgs, 12, StopTracking);
Benjamin Kramerb6f3c702010-02-08 18:38:55 +00001005 S = getPersistentSummary(RetEffect::MakeNoRet(), DoNothing, DoNothing);
Ted Kremenekb04cb592009-06-11 18:17:24 +00001006 }
Mike Stump1eb44332009-09-09 15:08:12 +00001007
Ted Kremenekb04cb592009-06-11 18:17:24 +00001008 // Did we get a summary?
1009 if (S)
1010 break;
Ted Kremenek61991902009-03-17 22:43:44 +00001011
1012 // Enable this code once the semantics of NSDeallocateObject are resolved
1013 // for GC. <rdar://problem/6619988>
1014#if 0
1015 // Handle: NSDeallocateObject(id anObject);
1016 // This method does allow 'nil' (although we don't check it now).
Mike Stump1eb44332009-09-09 15:08:12 +00001017 if (strcmp(FName, "NSDeallocateObject") == 0) {
Ted Kremenek61991902009-03-17 22:43:44 +00001018 return RetTy == Ctx.VoidTy
1019 ? getPersistentSummary(RetEffect::MakeNoRet(), DoNothing, Dealloc)
1020 : getPersistentStopSummary();
1021 }
1022#endif
Ted Kremenek12619382009-01-12 21:45:02 +00001023
1024 if (RetTy->isPointerType()) {
1025 // For CoreFoundation ('CF') types.
Ted Kremenek78acdbf2010-01-27 18:00:17 +00001026 if (cocoa::isRefType(RetTy, "CF", FName)) {
Ted Kremenek12619382009-01-12 21:45:02 +00001027 if (isRetain(FD, FName))
1028 S = getUnarySummary(FT, cfretain);
Benjamin Kramerb6f3c702010-02-08 18:38:55 +00001029 else if (FName.find("MakeCollectable") != StringRef::npos)
Ted Kremenek12619382009-01-12 21:45:02 +00001030 S = getUnarySummary(FT, cfmakecollectable);
Mike Stump1eb44332009-09-09 15:08:12 +00001031 else
Ted Kremenek12619382009-01-12 21:45:02 +00001032 S = getCFCreateGetRuleSummary(FD, FName);
1033
1034 break;
1035 }
1036
1037 // For CoreGraphics ('CG') types.
Ted Kremenek78acdbf2010-01-27 18:00:17 +00001038 if (cocoa::isRefType(RetTy, "CG", FName)) {
Ted Kremenek12619382009-01-12 21:45:02 +00001039 if (isRetain(FD, FName))
1040 S = getUnarySummary(FT, cfretain);
1041 else
1042 S = getCFCreateGetRuleSummary(FD, FName);
1043
1044 break;
1045 }
1046
1047 // For the Disk Arbitration API (DiskArbitration/DADisk.h)
Ted Kremenek78acdbf2010-01-27 18:00:17 +00001048 if (cocoa::isRefType(RetTy, "DADisk") ||
1049 cocoa::isRefType(RetTy, "DADissenter") ||
1050 cocoa::isRefType(RetTy, "DASessionRef")) {
Ted Kremenek12619382009-01-12 21:45:02 +00001051 S = getCFCreateGetRuleSummary(FD, FName);
1052 break;
1053 }
Mike Stump1eb44332009-09-09 15:08:12 +00001054
Ted Kremenek12619382009-01-12 21:45:02 +00001055 break;
1056 }
1057
1058 // Check for release functions, the only kind of functions that we care
1059 // about that don't return a pointer type.
1060 if (FName[0] == 'C' && (FName[1] == 'F' || FName[1] == 'G')) {
Ted Kremeneke7d03122010-02-08 16:45:01 +00001061 // Test for 'CGCF'.
Benjamin Kramerb6f3c702010-02-08 18:38:55 +00001062 FName = FName.substr(FName.startswith("CGCF") ? 4 : 2);
Ted Kremeneke7d03122010-02-08 16:45:01 +00001063
Ted Kremenekbf0a4dd2009-03-05 22:11:14 +00001064 if (isRelease(FD, FName))
Ted Kremenek12619382009-01-12 21:45:02 +00001065 S = getUnarySummary(FT, cfrelease);
1066 else {
Ted Kremenekb77449c2009-05-03 05:20:50 +00001067 assert (ScratchArgs.isEmpty());
Ted Kremenek68189282009-01-29 22:45:13 +00001068 // Remaining CoreFoundation and CoreGraphics functions.
1069 // We use to assume that they all strictly followed the ownership idiom
1070 // and that ownership cannot be transferred. While this is technically
1071 // correct, many methods allow a tracked object to escape. For example:
1072 //
Mike Stump1eb44332009-09-09 15:08:12 +00001073 // CFMutableDictionaryRef x = CFDictionaryCreateMutable(...);
Ted Kremenek68189282009-01-29 22:45:13 +00001074 // CFDictionaryAddValue(y, key, x);
Mike Stump1eb44332009-09-09 15:08:12 +00001075 // CFRelease(x);
Ted Kremenek68189282009-01-29 22:45:13 +00001076 // ... it is okay to use 'x' since 'y' has a reference to it
1077 //
1078 // We handle this and similar cases with the follow heuristic. If the
Ted Kremenekc4843812009-08-20 00:57:22 +00001079 // function name contains "InsertValue", "SetValue", "AddValue",
1080 // "AppendValue", or "SetAttribute", then we assume that arguments may
1081 // "escape." This means that something else holds on to the object,
1082 // allowing it be used even after its local retain count drops to 0.
Benjamin Kramere45c1492010-01-11 19:46:28 +00001083 ArgEffect E = (StrInStrNoCase(FName, "InsertValue") != StringRef::npos||
1084 StrInStrNoCase(FName, "AddValue") != StringRef::npos ||
1085 StrInStrNoCase(FName, "SetValue") != StringRef::npos ||
1086 StrInStrNoCase(FName, "AppendValue") != StringRef::npos||
Benjamin Kramerc027e542010-01-11 20:15:06 +00001087 StrInStrNoCase(FName, "SetAttribute") != StringRef::npos)
Ted Kremenek68189282009-01-29 22:45:13 +00001088 ? MayEscape : DoNothing;
Mike Stump1eb44332009-09-09 15:08:12 +00001089
Ted Kremenek68189282009-01-29 22:45:13 +00001090 S = getPersistentSummary(RetEffect::MakeNoRet(), DoNothing, E);
Ted Kremenek12619382009-01-12 21:45:02 +00001091 }
1092 }
Ted Kremenek37d785b2008-07-15 16:50:12 +00001093 }
1094 while (0);
Mike Stump1eb44332009-09-09 15:08:12 +00001095
Ted Kremenek885c27b2009-05-04 05:31:22 +00001096 if (!S)
1097 S = getDefaultSummary();
Ted Kremenek891d5cc2008-04-24 17:22:33 +00001098
Ted Kremenek4dd8fb42009-05-09 02:58:13 +00001099 // Annotations override defaults.
1100 assert(S);
1101 updateSummaryFromAnnotations(*S, FD);
Mike Stump1eb44332009-09-09 15:08:12 +00001102
Ted Kremenekd3dbcf42008-05-05 22:11:16 +00001103 FuncSummaries[FD] = S;
Mike Stump1eb44332009-09-09 15:08:12 +00001104 return S;
Ted Kremenek2fff37e2008-03-06 00:08:09 +00001105}
1106
Ted Kremenek37d785b2008-07-15 16:50:12 +00001107RetainSummary*
Zhongxing Xubc5495b2010-07-20 02:56:49 +00001108RetainSummaryManager::getCFCreateGetRuleSummary(const FunctionDecl* FD,
Benjamin Kramerb6f3c702010-02-08 18:38:55 +00001109 StringRef FName) {
Mike Stump1eb44332009-09-09 15:08:12 +00001110
Benjamin Kramerb6f3c702010-02-08 18:38:55 +00001111 if (FName.find("Create") != StringRef::npos ||
1112 FName.find("Copy") != StringRef::npos)
Ted Kremenek86ad3bc2008-05-05 16:51:50 +00001113 return getCFSummaryCreateRule(FD);
Mike Stump1eb44332009-09-09 15:08:12 +00001114
Benjamin Kramerb6f3c702010-02-08 18:38:55 +00001115 if (FName.find("Get") != StringRef::npos)
Ted Kremenek86ad3bc2008-05-05 16:51:50 +00001116 return getCFSummaryGetRule(FD);
Mike Stump1eb44332009-09-09 15:08:12 +00001117
Ted Kremenek7faca822009-05-04 04:57:00 +00001118 return getDefaultSummary();
Ted Kremenek86ad3bc2008-05-05 16:51:50 +00001119}
1120
Ted Kremenekd3dbcf42008-05-05 22:11:16 +00001121RetainSummary*
Ted Kremenek6ad315a2009-02-23 16:51:39 +00001122RetainSummaryManager::getUnarySummary(const FunctionType* FT,
1123 UnaryFuncKind func) {
1124
Ted Kremenek12619382009-01-12 21:45:02 +00001125 // Sanity check that this is *really* a unary function. This can
1126 // happen if people do weird things.
Douglas Gregor72564e72009-02-26 23:50:07 +00001127 const FunctionProtoType* FTP = dyn_cast<FunctionProtoType>(FT);
Ted Kremenek12619382009-01-12 21:45:02 +00001128 if (!FTP || FTP->getNumArgs() != 1)
1129 return getPersistentStopSummary();
Mike Stump1eb44332009-09-09 15:08:12 +00001130
Ted Kremenekb77449c2009-05-03 05:20:50 +00001131 assert (ScratchArgs.isEmpty());
Mike Stump1eb44332009-09-09 15:08:12 +00001132
Ted Kremenek377e2302008-04-29 05:33:51 +00001133 switch (func) {
Ted Kremenekb77449c2009-05-03 05:20:50 +00001134 case cfretain: {
Ted Kremenek3baf6722010-11-24 00:54:37 +00001135 ScratchArgs = AF.add(ScratchArgs, 0, IncRef);
Ted Kremenek3eabf1c2008-05-22 17:31:13 +00001136 return getPersistentSummary(RetEffect::MakeAlias(0),
1137 DoNothing, DoNothing);
Ted Kremenek377e2302008-04-29 05:33:51 +00001138 }
Mike Stump1eb44332009-09-09 15:08:12 +00001139
Ted Kremenek377e2302008-04-29 05:33:51 +00001140 case cfrelease: {
Ted Kremenek3baf6722010-11-24 00:54:37 +00001141 ScratchArgs = AF.add(ScratchArgs, 0, DecRef);
Ted Kremenek3eabf1c2008-05-22 17:31:13 +00001142 return getPersistentSummary(RetEffect::MakeNoRet(),
1143 DoNothing, DoNothing);
Ted Kremenek377e2302008-04-29 05:33:51 +00001144 }
Mike Stump1eb44332009-09-09 15:08:12 +00001145
Ted Kremenek377e2302008-04-29 05:33:51 +00001146 case cfmakecollectable: {
Ted Kremenek3baf6722010-11-24 00:54:37 +00001147 ScratchArgs = AF.add(ScratchArgs, 0, MakeCollectable);
Mike Stump1eb44332009-09-09 15:08:12 +00001148 return getPersistentSummary(RetEffect::MakeAlias(0),DoNothing, DoNothing);
Ted Kremenek377e2302008-04-29 05:33:51 +00001149 }
Mike Stump1eb44332009-09-09 15:08:12 +00001150
Ted Kremenek377e2302008-04-29 05:33:51 +00001151 default:
Ted Kremenek86ad3bc2008-05-05 16:51:50 +00001152 assert (false && "Not a supported unary function.");
Ted Kremenek7faca822009-05-04 04:57:00 +00001153 return getDefaultSummary();
Ted Kremenek940b1d82008-04-10 23:44:06 +00001154 }
Ted Kremenek00a3a5f2008-03-12 01:21:45 +00001155}
1156
Zhongxing Xubc5495b2010-07-20 02:56:49 +00001157RetainSummary*
1158RetainSummaryManager::getCFSummaryCreateRule(const FunctionDecl* FD) {
Ted Kremenekb77449c2009-05-03 05:20:50 +00001159 assert (ScratchArgs.isEmpty());
Mike Stump1eb44332009-09-09 15:08:12 +00001160
Ted Kremenek070a8252008-07-09 18:11:16 +00001161 if (FD->getIdentifier() == CFDictionaryCreateII) {
Ted Kremenek3baf6722010-11-24 00:54:37 +00001162 ScratchArgs = AF.add(ScratchArgs, 1, DoNothingByRef);
1163 ScratchArgs = AF.add(ScratchArgs, 2, DoNothingByRef);
Ted Kremenek070a8252008-07-09 18:11:16 +00001164 }
Mike Stump1eb44332009-09-09 15:08:12 +00001165
Ted Kremenek2d1652e2009-01-28 05:56:51 +00001166 return getPersistentSummary(RetEffect::MakeOwned(RetEffect::CF, true));
Ted Kremenek00a3a5f2008-03-12 01:21:45 +00001167}
1168
Zhongxing Xubc5495b2010-07-20 02:56:49 +00001169RetainSummary*
1170RetainSummaryManager::getCFSummaryGetRule(const FunctionDecl* FD) {
Mike Stump1eb44332009-09-09 15:08:12 +00001171 assert (ScratchArgs.isEmpty());
Ted Kremenek2d1652e2009-01-28 05:56:51 +00001172 return getPersistentSummary(RetEffect::MakeNotOwned(RetEffect::CF),
1173 DoNothing, DoNothing);
Ted Kremenek00a3a5f2008-03-12 01:21:45 +00001174}
1175
Ted Kremenek6b3a0f72008-03-11 06:39:11 +00001176//===----------------------------------------------------------------------===//
Ted Kremenekd3dbcf42008-05-05 22:11:16 +00001177// Summary creation for Selectors.
1178//===----------------------------------------------------------------------===//
1179
Ted Kremenek1bffd742008-05-06 15:44:25 +00001180RetainSummary*
Ted Kremenek8711c032009-04-29 05:04:30 +00001181RetainSummaryManager::getInitMethodSummary(QualType RetTy) {
Mike Stump1eb44332009-09-09 15:08:12 +00001182 assert(ScratchArgs.isEmpty());
Ted Kremenek78a35a32009-05-12 20:06:54 +00001183 // 'init' methods conceptually return a newly allocated object and claim
Mike Stump1eb44332009-09-09 15:08:12 +00001184 // the receiver.
Ted Kremenek78acdbf2010-01-27 18:00:17 +00001185 if (cocoa::isCocoaObjectRef(RetTy) || cocoa::isCFObjectRef(RetTy))
Ted Kremenek547d4952009-06-05 23:18:01 +00001186 return getPersistentSummary(ObjCInitRetE, DecRefMsg);
Mike Stump1eb44332009-09-09 15:08:12 +00001187
Ted Kremenek78a35a32009-05-12 20:06:54 +00001188 return getDefaultSummary();
Ted Kremenek46e49ee2008-05-05 23:55:01 +00001189}
Ted Kremenek4dd8fb42009-05-09 02:58:13 +00001190
1191void
1192RetainSummaryManager::updateSummaryFromAnnotations(RetainSummary &Summ,
1193 const FunctionDecl *FD) {
1194 if (!FD)
1195 return;
1196
Ted Kremenekb04cb592009-06-11 18:17:24 +00001197 QualType RetTy = FD->getResultType();
Mike Stump1eb44332009-09-09 15:08:12 +00001198
Ted Kremenek4dd8fb42009-05-09 02:58:13 +00001199 // Determine if there is a special return effect for this method.
Ted Kremenek78acdbf2010-01-27 18:00:17 +00001200 if (cocoa::isCocoaObjectRef(RetTy)) {
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001201 if (FD->getAttr<NSReturnsRetainedAttr>()) {
Ted Kremenek4dd8fb42009-05-09 02:58:13 +00001202 Summ.setRetEffect(ObjCAllocRetE);
1203 }
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001204 else if (FD->getAttr<CFReturnsRetainedAttr>()) {
Ted Kremenekb9d8db82009-06-05 23:00:33 +00001205 Summ.setRetEffect(RetEffect::MakeOwned(RetEffect::CF, true));
Ted Kremenekb04cb592009-06-11 18:17:24 +00001206 }
Ted Kremenek60411112010-02-18 00:06:12 +00001207 else if (FD->getAttr<NSReturnsNotRetainedAttr>()) {
1208 Summ.setRetEffect(RetEffect::MakeNotOwned(RetEffect::ObjC));
1209 }
1210 else if (FD->getAttr<CFReturnsNotRetainedAttr>()) {
1211 Summ.setRetEffect(RetEffect::MakeNotOwned(RetEffect::CF));
1212 }
Ted Kremenekb04cb592009-06-11 18:17:24 +00001213 }
Ted Kremenek6217b802009-07-29 21:53:49 +00001214 else if (RetTy->getAs<PointerType>()) {
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001215 if (FD->getAttr<CFReturnsRetainedAttr>()) {
Ted Kremenek4dd8fb42009-05-09 02:58:13 +00001216 Summ.setRetEffect(RetEffect::MakeOwned(RetEffect::CF, true));
1217 }
1218 }
1219}
1220
1221void
1222RetainSummaryManager::updateSummaryFromAnnotations(RetainSummary &Summ,
1223 const ObjCMethodDecl *MD) {
1224 if (!MD)
1225 return;
1226
Ted Kremenek6d4b76d2009-07-06 18:30:43 +00001227 bool isTrackedLoc = false;
Mike Stump1eb44332009-09-09 15:08:12 +00001228
Ted Kremenek4dd8fb42009-05-09 02:58:13 +00001229 // Determine if there is a special return effect for this method.
Ted Kremenek78acdbf2010-01-27 18:00:17 +00001230 if (cocoa::isCocoaObjectRef(MD->getResultType())) {
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001231 if (MD->getAttr<NSReturnsRetainedAttr>()) {
Ted Kremenek4dd8fb42009-05-09 02:58:13 +00001232 Summ.setRetEffect(ObjCAllocRetE);
Ted Kremenek6d4b76d2009-07-06 18:30:43 +00001233 return;
Ted Kremenek4dd8fb42009-05-09 02:58:13 +00001234 }
Ted Kremenek60411112010-02-18 00:06:12 +00001235 if (MD->getAttr<NSReturnsNotRetainedAttr>()) {
1236 Summ.setRetEffect(RetEffect::MakeNotOwned(RetEffect::ObjC));
1237 return;
1238 }
Mike Stump1eb44332009-09-09 15:08:12 +00001239
Ted Kremenek6d4b76d2009-07-06 18:30:43 +00001240 isTrackedLoc = true;
Ted Kremenek4dd8fb42009-05-09 02:58:13 +00001241 }
Mike Stump1eb44332009-09-09 15:08:12 +00001242
Ted Kremenek6d4b76d2009-07-06 18:30:43 +00001243 if (!isTrackedLoc)
Ted Kremenek6217b802009-07-29 21:53:49 +00001244 isTrackedLoc = MD->getResultType()->getAs<PointerType>() != NULL;
Mike Stump1eb44332009-09-09 15:08:12 +00001245
Ted Kremenek60411112010-02-18 00:06:12 +00001246 if (isTrackedLoc) {
1247 if (MD->getAttr<CFReturnsRetainedAttr>())
1248 Summ.setRetEffect(RetEffect::MakeOwned(RetEffect::CF, true));
1249 else if (MD->getAttr<CFReturnsNotRetainedAttr>())
1250 Summ.setRetEffect(RetEffect::MakeNotOwned(RetEffect::CF));
1251 }
Ted Kremenek4dd8fb42009-05-09 02:58:13 +00001252}
1253
Ted Kremenek1bffd742008-05-06 15:44:25 +00001254RetainSummary*
Ted Kremeneka8833552009-04-29 23:03:22 +00001255RetainSummaryManager::getCommonMethodSummary(const ObjCMethodDecl* MD,
1256 Selector S, QualType RetTy) {
Ted Kremenek8ee885b2009-04-24 21:56:17 +00001257
Ted Kremenekfcd7c6f2009-04-29 00:42:39 +00001258 if (MD) {
Ted Kremenek376d1e72009-04-24 18:00:17 +00001259 // Scan the method decl for 'void*' arguments. These should be treated
1260 // as 'StopTracking' because they are often used with delegates.
1261 // Delegates are a frequent form of false positives with the retain
1262 // count checker.
1263 unsigned i = 0;
1264 for (ObjCMethodDecl::param_iterator I = MD->param_begin(),
1265 E = MD->param_end(); I != E; ++I, ++i)
1266 if (ParmVarDecl *PD = *I) {
1267 QualType Ty = Ctx.getCanonicalType(PD->getType());
Douglas Gregora4923eb2009-11-16 21:35:15 +00001268 if (Ty.getLocalUnqualifiedType() == Ctx.VoidPtrTy)
Ted Kremenek3baf6722010-11-24 00:54:37 +00001269 ScratchArgs = AF.add(ScratchArgs, i, StopTracking);
Ted Kremenek376d1e72009-04-24 18:00:17 +00001270 }
1271 }
Mike Stump1eb44332009-09-09 15:08:12 +00001272
Ted Kremenek8ee885b2009-04-24 21:56:17 +00001273 // Any special effect for the receiver?
1274 ArgEffect ReceiverEff = DoNothing;
Mike Stump1eb44332009-09-09 15:08:12 +00001275
Ted Kremenek8ee885b2009-04-24 21:56:17 +00001276 // If one of the arguments in the selector has the keyword 'delegate' we
1277 // should stop tracking the reference count for the receiver. This is
1278 // because the reference count is quite possibly handled by a delegate
1279 // method.
1280 if (S.isKeywordSelector()) {
1281 const std::string &str = S.getAsString();
1282 assert(!str.empty());
Benjamin Kramere45c1492010-01-11 19:46:28 +00001283 if (StrInStrNoCase(str, "delegate:") != StringRef::npos)
1284 ReceiverEff = StopTracking;
Ted Kremenek8ee885b2009-04-24 21:56:17 +00001285 }
Mike Stump1eb44332009-09-09 15:08:12 +00001286
Ted Kremenek250b1fa2009-04-23 23:08:22 +00001287 // Look for methods that return an owned object.
Ted Kremenek78acdbf2010-01-27 18:00:17 +00001288 if (cocoa::isCocoaObjectRef(RetTy)) {
Ted Kremenek28f47b92010-12-01 22:16:56 +00001289 // EXPERIMENTAL: assume the Cocoa conventions for all objects returned
Ted Kremenek92511432009-05-03 06:08:32 +00001290 // by instance methods.
Ted Kremeneka64e89b2010-01-27 06:13:48 +00001291 RetEffect E = cocoa::followsFundamentalRule(S)
Ted Kremenek7db16042009-05-15 15:49:00 +00001292 ? ObjCAllocRetE : RetEffect::MakeNotOwned(RetEffect::ObjC);
Mike Stump1eb44332009-09-09 15:08:12 +00001293
1294 return getPersistentSummary(E, ReceiverEff, MayEscape);
Ted Kremenek376d1e72009-04-24 18:00:17 +00001295 }
Mike Stump1eb44332009-09-09 15:08:12 +00001296
Ted Kremenek92511432009-05-03 06:08:32 +00001297 // Look for methods that return an owned core foundation object.
Ted Kremenek78acdbf2010-01-27 18:00:17 +00001298 if (cocoa::isCFObjectRef(RetTy)) {
Ted Kremeneka64e89b2010-01-27 06:13:48 +00001299 RetEffect E = cocoa::followsFundamentalRule(S)
Ted Kremenek7db16042009-05-15 15:49:00 +00001300 ? RetEffect::MakeOwned(RetEffect::CF, true)
1301 : RetEffect::MakeNotOwned(RetEffect::CF);
Mike Stump1eb44332009-09-09 15:08:12 +00001302
Ted Kremenek92511432009-05-03 06:08:32 +00001303 return getPersistentSummary(E, ReceiverEff, MayEscape);
1304 }
Mike Stump1eb44332009-09-09 15:08:12 +00001305
Ted Kremenek92511432009-05-03 06:08:32 +00001306 if (ScratchArgs.isEmpty() && ReceiverEff == DoNothing)
Ted Kremenek7faca822009-05-04 04:57:00 +00001307 return getDefaultSummary();
Mike Stump1eb44332009-09-09 15:08:12 +00001308
Ted Kremenek885c27b2009-05-04 05:31:22 +00001309 return getPersistentSummary(RetEffect::MakeNoRet(), ReceiverEff, MayEscape);
Ted Kremenek250b1fa2009-04-23 23:08:22 +00001310}
1311
1312RetainSummary*
Ted Kremenekb7ddd9b2009-11-13 01:54:21 +00001313RetainSummaryManager::getInstanceMethodSummary(const ObjCMessageExpr *ME,
1314 const GRState *state,
1315 const LocationContext *LC) {
1316
1317 // We need the type-information of the tracked receiver object
1318 // Retrieve it from the state.
Douglas Gregor04badcf2010-04-21 00:45:42 +00001319 const Expr *Receiver = ME->getInstanceReceiver();
Ted Kremenekb7ddd9b2009-11-13 01:54:21 +00001320 const ObjCInterfaceDecl* ID = 0;
1321
1322 // FIXME: Is this really working as expected? There are cases where
1323 // we just use the 'ID' from the message expression.
Douglas Gregor04badcf2010-04-21 00:45:42 +00001324 SVal receiverV;
1325
Ted Kremenek8f326752010-05-21 21:56:53 +00001326 if (Receiver) {
Douglas Gregor04badcf2010-04-21 00:45:42 +00001327 receiverV = state->getSValAsScalarOrLoc(Receiver);
Ted Kremenekdcee3ce2010-07-01 20:16:50 +00001328
Douglas Gregor04badcf2010-04-21 00:45:42 +00001329 // FIXME: Eventually replace the use of state->get<RefBindings> with
1330 // a generic API for reasoning about the Objective-C types of symbolic
1331 // objects.
1332 if (SymbolRef Sym = receiverV.getAsLocSymbol())
1333 if (const RefVal *T = state->get<RefBindings>(Sym))
Ted Kremenekdcee3ce2010-07-01 20:16:50 +00001334 if (const ObjCObjectPointerType* PT =
Ted Kremenekb7ddd9b2009-11-13 01:54:21 +00001335 T->getType()->getAs<ObjCObjectPointerType>())
Douglas Gregor04badcf2010-04-21 00:45:42 +00001336 ID = PT->getInterfaceDecl();
Ted Kremenekdcee3ce2010-07-01 20:16:50 +00001337
Douglas Gregor04badcf2010-04-21 00:45:42 +00001338 // FIXME: this is a hack. This may or may not be the actual method
1339 // that is called.
1340 if (!ID) {
1341 if (const ObjCObjectPointerType *PT =
1342 Receiver->getType()->getAs<ObjCObjectPointerType>())
Ted Kremenekb7ddd9b2009-11-13 01:54:21 +00001343 ID = PT->getInterfaceDecl();
Douglas Gregor04badcf2010-04-21 00:45:42 +00001344 }
1345 } else {
1346 // FIXME: Hack for 'super'.
1347 ID = ME->getReceiverInterface();
Ted Kremenekb7ddd9b2009-11-13 01:54:21 +00001348 }
Douglas Gregor04badcf2010-04-21 00:45:42 +00001349
Ted Kremenekb7ddd9b2009-11-13 01:54:21 +00001350 // FIXME: The receiver could be a reference to a class, meaning that
1351 // we should use the class method.
1352 RetainSummary *Summ = getInstanceMethodSummary(ME, ID);
Ted Kremenekdcee3ce2010-07-01 20:16:50 +00001353
Ted Kremenekb7ddd9b2009-11-13 01:54:21 +00001354 // Special-case: are we sending a mesage to "self"?
1355 // This is a hack. When we have full-IP this should be removed.
Douglas Gregor04badcf2010-04-21 00:45:42 +00001356 if (isa<ObjCMethodDecl>(LC->getDecl()) && Receiver) {
Ted Kremenekb7ddd9b2009-11-13 01:54:21 +00001357 if (const loc::MemRegionVal *L = dyn_cast<loc::MemRegionVal>(&receiverV)) {
1358 // Get the region associated with 'self'.
1359 if (const ImplicitParamDecl *SelfDecl = LC->getSelfDecl()) {
Ted Kremenek13976632010-02-08 16:18:51 +00001360 SVal SelfVal = state->getSVal(state->getRegion(SelfDecl, LC));
Ted Kremenekb7ddd9b2009-11-13 01:54:21 +00001361 if (L->StripCasts() == SelfVal.getAsRegion()) {
1362 // Update the summary to make the default argument effect
1363 // 'StopTracking'.
1364 Summ = copySummary(Summ);
1365 Summ->setDefaultArgEffect(StopTracking);
1366 }
1367 }
1368 }
1369 }
Ted Kremenekdcee3ce2010-07-01 20:16:50 +00001370
Ted Kremenekb7ddd9b2009-11-13 01:54:21 +00001371 return Summ ? Summ : getDefaultSummary();
1372}
1373
1374RetainSummary*
Ted Kremenekce8a41d2009-04-29 17:09:14 +00001375RetainSummaryManager::getInstanceMethodSummary(Selector S,
1376 IdentifierInfo *ClsName,
Ted Kremeneka8833552009-04-29 23:03:22 +00001377 const ObjCInterfaceDecl* ID,
1378 const ObjCMethodDecl *MD,
Ted Kremenekce8a41d2009-04-29 17:09:14 +00001379 QualType RetTy) {
Ted Kremenek1bffd742008-05-06 15:44:25 +00001380
Ted Kremenek8711c032009-04-29 05:04:30 +00001381 // Look up a summary in our summary cache.
Ted Kremenek614cc542009-07-21 23:27:57 +00001382 RetainSummary *Summ = ObjCMethodSummaries.find(ID, ClsName, S);
Mike Stump1eb44332009-09-09 15:08:12 +00001383
Ted Kremenek614cc542009-07-21 23:27:57 +00001384 if (!Summ) {
1385 assert(ScratchArgs.isEmpty());
Mike Stump1eb44332009-09-09 15:08:12 +00001386
Ted Kremenek614cc542009-07-21 23:27:57 +00001387 // "initXXX": pass-through for receiver.
Ted Kremeneka64e89b2010-01-27 06:13:48 +00001388 if (cocoa::deriveNamingConvention(S) == cocoa::InitRule)
Ted Kremenek614cc542009-07-21 23:27:57 +00001389 Summ = getInitMethodSummary(RetTy);
1390 else
1391 Summ = getCommonMethodSummary(MD, S, RetTy);
Mike Stump1eb44332009-09-09 15:08:12 +00001392
Ted Kremenek614cc542009-07-21 23:27:57 +00001393 // Annotations override defaults.
1394 updateSummaryFromAnnotations(*Summ, MD);
Mike Stump1eb44332009-09-09 15:08:12 +00001395
Ted Kremenek614cc542009-07-21 23:27:57 +00001396 // Memoize the summary.
1397 ObjCMethodSummaries[ObjCSummaryKey(ID, ClsName, S)] = Summ;
1398 }
Mike Stump1eb44332009-09-09 15:08:12 +00001399
Ted Kremeneke87450e2009-04-23 19:11:35 +00001400 return Summ;
Ted Kremenek46e49ee2008-05-05 23:55:01 +00001401}
1402
Ted Kremenekc8395602008-05-06 21:26:51 +00001403RetainSummary*
Ted Kremenekfcd7c6f2009-04-29 00:42:39 +00001404RetainSummaryManager::getClassMethodSummary(Selector S, IdentifierInfo *ClsName,
Ted Kremeneka8833552009-04-29 23:03:22 +00001405 const ObjCInterfaceDecl *ID,
1406 const ObjCMethodDecl *MD,
1407 QualType RetTy) {
Ted Kremenekde4d5332009-04-24 17:50:11 +00001408
Ted Kremenekfcd7c6f2009-04-29 00:42:39 +00001409 assert(ClsName && "Class name must be specified.");
Mike Stump1eb44332009-09-09 15:08:12 +00001410 RetainSummary *Summ = ObjCClassMethodSummaries.find(ID, ClsName, S);
1411
Ted Kremenek614cc542009-07-21 23:27:57 +00001412 if (!Summ) {
1413 Summ = getCommonMethodSummary(MD, S, RetTy);
1414 // Annotations override defaults.
1415 updateSummaryFromAnnotations(*Summ, MD);
1416 // Memoize the summary.
1417 ObjCClassMethodSummaries[ObjCSummaryKey(ID, ClsName, S)] = Summ;
1418 }
Mike Stump1eb44332009-09-09 15:08:12 +00001419
Ted Kremeneke87450e2009-04-23 19:11:35 +00001420 return Summ;
Ted Kremenekc8395602008-05-06 21:26:51 +00001421}
1422
Mike Stump1eb44332009-09-09 15:08:12 +00001423void RetainSummaryManager::InitializeClassMethodSummaries() {
Ted Kremenekec315332009-05-07 23:40:42 +00001424 assert(ScratchArgs.isEmpty());
1425 RetainSummary* Summ = getPersistentSummary(ObjCAllocRetE);
Mike Stump1eb44332009-09-09 15:08:12 +00001426
Mike Stump1eb44332009-09-09 15:08:12 +00001427 // Create the [NSAssertionHandler currentHander] summary.
Ted Kremenek6fe2b7a2009-10-15 22:25:12 +00001428 addClassMethSummary("NSAssertionHandler", "currentHandler",
Ted Kremenek2d1652e2009-01-28 05:56:51 +00001429 getPersistentSummary(RetEffect::MakeNotOwned(RetEffect::ObjC)));
Mike Stump1eb44332009-09-09 15:08:12 +00001430
Ted Kremenek6d348932008-10-21 15:53:15 +00001431 // Create the [NSAutoreleasePool addObject:] summary.
Ted Kremenek3baf6722010-11-24 00:54:37 +00001432 ScratchArgs = AF.add(ScratchArgs, 0, Autorelease);
Ted Kremenek6fe2b7a2009-10-15 22:25:12 +00001433 addClassMethSummary("NSAutoreleasePool", "addObject",
1434 getPersistentSummary(RetEffect::MakeNoRet(),
1435 DoNothing, Autorelease));
Mike Stump1eb44332009-09-09 15:08:12 +00001436
Ted Kremenekde4d5332009-04-24 17:50:11 +00001437 // Create the summaries for [NSObject performSelector...]. We treat
1438 // these as 'stop tracking' for the arguments because they are often
1439 // used for delegates that can release the object. When we have better
1440 // inter-procedural analysis we can potentially do something better. This
1441 // workaround is to remove false positives.
1442 Summ = getPersistentSummary(RetEffect::MakeNoRet(), DoNothing, StopTracking);
1443 IdentifierInfo *NSObjectII = &Ctx.Idents.get("NSObject");
1444 addClsMethSummary(NSObjectII, Summ, "performSelector", "withObject",
1445 "afterDelay", NULL);
1446 addClsMethSummary(NSObjectII, Summ, "performSelector", "withObject",
1447 "afterDelay", "inModes", NULL);
1448 addClsMethSummary(NSObjectII, Summ, "performSelectorOnMainThread",
1449 "withObject", "waitUntilDone", NULL);
1450 addClsMethSummary(NSObjectII, Summ, "performSelectorOnMainThread",
1451 "withObject", "waitUntilDone", "modes", NULL);
1452 addClsMethSummary(NSObjectII, Summ, "performSelector", "onThread",
1453 "withObject", "waitUntilDone", NULL);
1454 addClsMethSummary(NSObjectII, Summ, "performSelector", "onThread",
1455 "withObject", "waitUntilDone", "modes", NULL);
1456 addClsMethSummary(NSObjectII, Summ, "performSelectorInBackground",
1457 "withObject", NULL);
Ted Kremenek9c32d082008-05-06 00:30:21 +00001458}
1459
Ted Kremenek1f180c32008-06-23 22:21:20 +00001460void RetainSummaryManager::InitializeMethodSummaries() {
Mike Stump1eb44332009-09-09 15:08:12 +00001461
1462 assert (ScratchArgs.isEmpty());
1463
Ted Kremenekc8395602008-05-06 21:26:51 +00001464 // Create the "init" selector. It just acts as a pass-through for the
1465 // receiver.
Mike Stump1eb44332009-09-09 15:08:12 +00001466 RetainSummary *InitSumm = getPersistentSummary(ObjCInitRetE, DecRefMsg);
Ted Kremenekac02f202009-08-20 05:13:36 +00001467 addNSObjectMethSummary(GetNullarySelector("init", Ctx), InitSumm);
1468
1469 // awakeAfterUsingCoder: behaves basically like an 'init' method. It
1470 // claims the receiver and returns a retained object.
1471 addNSObjectMethSummary(GetUnarySelector("awakeAfterUsingCoder", Ctx),
1472 InitSumm);
Mike Stump1eb44332009-09-09 15:08:12 +00001473
Ted Kremenekc8395602008-05-06 21:26:51 +00001474 // The next methods are allocators.
Ted Kremeneka834fb42009-08-28 19:52:12 +00001475 RetainSummary *AllocSumm = getPersistentSummary(ObjCAllocRetE);
Mike Stump1eb44332009-09-09 15:08:12 +00001476 RetainSummary *CFAllocSumm =
Ted Kremeneka834fb42009-08-28 19:52:12 +00001477 getPersistentSummary(RetEffect::MakeOwned(RetEffect::CF, true));
Mike Stump1eb44332009-09-09 15:08:12 +00001478
Ted Kremenek3c0cea32008-05-06 02:26:56 +00001479 // Create the "retain" selector.
Ted Kremenekec315332009-05-07 23:40:42 +00001480 RetEffect E = RetEffect::MakeReceiverAlias();
Ted Kremenek767d6492009-05-20 22:39:57 +00001481 RetainSummary *Summ = getPersistentSummary(E, IncRefMsg);
Ted Kremenek553cf182008-06-25 21:21:56 +00001482 addNSObjectMethSummary(GetNullarySelector("retain", Ctx), Summ);
Mike Stump1eb44332009-09-09 15:08:12 +00001483
Ted Kremenek3c0cea32008-05-06 02:26:56 +00001484 // Create the "release" selector.
Ted Kremenek1c512f52009-02-18 18:54:33 +00001485 Summ = getPersistentSummary(E, DecRefMsg);
Ted Kremenek553cf182008-06-25 21:21:56 +00001486 addNSObjectMethSummary(GetNullarySelector("release", Ctx), Summ);
Mike Stump1eb44332009-09-09 15:08:12 +00001487
Ted Kremenek299e8152008-05-07 21:17:39 +00001488 // Create the "drain" selector.
1489 Summ = getPersistentSummary(E, isGCEnabled() ? DoNothing : DecRef);
Ted Kremenek553cf182008-06-25 21:21:56 +00001490 addNSObjectMethSummary(GetNullarySelector("drain", Ctx), Summ);
Mike Stump1eb44332009-09-09 15:08:12 +00001491
Ted Kremenekf95e9fc2009-03-17 19:42:23 +00001492 // Create the -dealloc summary.
1493 Summ = getPersistentSummary(RetEffect::MakeNoRet(), Dealloc);
1494 addNSObjectMethSummary(GetNullarySelector("dealloc", Ctx), Summ);
Ted Kremenek3c0cea32008-05-06 02:26:56 +00001495
1496 // Create the "autorelease" selector.
Ted Kremenekabf43972009-01-28 21:44:40 +00001497 Summ = getPersistentSummary(E, Autorelease);
Ted Kremenek553cf182008-06-25 21:21:56 +00001498 addNSObjectMethSummary(GetNullarySelector("autorelease", Ctx), Summ);
Mike Stump1eb44332009-09-09 15:08:12 +00001499
Ted Kremenekf9a8e2e2009-02-23 17:45:03 +00001500 // Specially handle NSAutoreleasePool.
Ted Kremenek6c4becb2009-02-25 02:54:57 +00001501 addInstMethSummary("NSAutoreleasePool", "init",
Ted Kremenekf9a8e2e2009-02-23 17:45:03 +00001502 getPersistentSummary(RetEffect::MakeReceiverAlias(),
Ted Kremenek6c4becb2009-02-25 02:54:57 +00001503 NewAutoreleasePool));
Mike Stump1eb44332009-09-09 15:08:12 +00001504
1505 // For NSWindow, allocated objects are (initially) self-owned.
Ted Kremenek89e202d2009-02-23 02:51:29 +00001506 // FIXME: For now we opt for false negatives with NSWindow, as these objects
1507 // self-own themselves. However, they only do this once they are displayed.
1508 // Thus, we need to track an NSWindow's display status.
1509 // This is tracked in <rdar://problem/6062711>.
Ted Kremenek3aa7ecd2009-03-04 23:30:42 +00001510 // See also http://llvm.org/bugs/show_bug.cgi?id=3714.
Ted Kremenek78a35a32009-05-12 20:06:54 +00001511 RetainSummary *NoTrackYet = getPersistentSummary(RetEffect::MakeNoRet(),
1512 StopTracking,
1513 StopTracking);
Mike Stump1eb44332009-09-09 15:08:12 +00001514
Ted Kremenek99d02692009-04-03 19:02:51 +00001515 addClassMethSummary("NSWindow", "alloc", NoTrackYet);
1516
Ted Kremenek3aa7ecd2009-03-04 23:30:42 +00001517#if 0
Ted Kremenek78a35a32009-05-12 20:06:54 +00001518 addInstMethSummary("NSWindow", NoTrackYet, "initWithContentRect",
Ted Kremenekaf9dc272008-08-12 18:48:50 +00001519 "styleMask", "backing", "defer", NULL);
Mike Stump1eb44332009-09-09 15:08:12 +00001520
Ted Kremenek78a35a32009-05-12 20:06:54 +00001521 addInstMethSummary("NSWindow", NoTrackYet, "initWithContentRect",
Ted Kremenekaf9dc272008-08-12 18:48:50 +00001522 "styleMask", "backing", "defer", "screen", NULL);
Ted Kremenek3aa7ecd2009-03-04 23:30:42 +00001523#endif
Mike Stump1eb44332009-09-09 15:08:12 +00001524
Ted Kremenekaf9dc272008-08-12 18:48:50 +00001525 // For NSPanel (which subclasses NSWindow), allocated objects are not
1526 // self-owned.
Ted Kremenek99d02692009-04-03 19:02:51 +00001527 // FIXME: For now we don't track NSPanels. object for the same reason
1528 // as for NSWindow objects.
1529 addClassMethSummary("NSPanel", "alloc", NoTrackYet);
Mike Stump1eb44332009-09-09 15:08:12 +00001530
Ted Kremenek78a35a32009-05-12 20:06:54 +00001531#if 0
1532 addInstMethSummary("NSPanel", NoTrackYet, "initWithContentRect",
Ted Kremenekaf9dc272008-08-12 18:48:50 +00001533 "styleMask", "backing", "defer", NULL);
Mike Stump1eb44332009-09-09 15:08:12 +00001534
Ted Kremenek78a35a32009-05-12 20:06:54 +00001535 addInstMethSummary("NSPanel", NoTrackYet, "initWithContentRect",
Ted Kremenekaf9dc272008-08-12 18:48:50 +00001536 "styleMask", "backing", "defer", "screen", NULL);
Ted Kremenek78a35a32009-05-12 20:06:54 +00001537#endif
Mike Stump1eb44332009-09-09 15:08:12 +00001538
Ted Kremenekba67f6a2009-05-18 23:14:34 +00001539 // Don't track allocated autorelease pools yet, as it is okay to prematurely
1540 // exit a method.
1541 addClassMethSummary("NSAutoreleasePool", "alloc", NoTrackYet);
Ted Kremenek553cf182008-06-25 21:21:56 +00001542
Ted Kremenek70a733e2008-07-18 17:24:20 +00001543 // Create NSAssertionHandler summaries.
Ted Kremenek9e476de2008-08-12 18:30:56 +00001544 addPanicSummary("NSAssertionHandler", "handleFailureInFunction", "file",
Mike Stump1eb44332009-09-09 15:08:12 +00001545 "lineNumber", "description", NULL);
1546
Ted Kremenek9e476de2008-08-12 18:30:56 +00001547 addPanicSummary("NSAssertionHandler", "handleFailureInMethod", "object",
1548 "file", "lineNumber", "description", NULL);
Mike Stump1eb44332009-09-09 15:08:12 +00001549
Ted Kremenek767d6492009-05-20 22:39:57 +00001550 // Create summaries QCRenderer/QCView -createSnapShotImageOfType:
1551 addInstMethSummary("QCRenderer", AllocSumm,
1552 "createSnapshotImageOfType", NULL);
1553 addInstMethSummary("QCView", AllocSumm,
1554 "createSnapshotImageOfType", NULL);
1555
Ted Kremenek211a9c62009-06-15 20:58:58 +00001556 // Create summaries for CIContext, 'createCGImage' and
Ted Kremeneka834fb42009-08-28 19:52:12 +00001557 // 'createCGLayerWithSize'. These objects are CF objects, and are not
1558 // automatically garbage collected.
1559 addInstMethSummary("CIContext", CFAllocSumm,
Ted Kremenek767d6492009-05-20 22:39:57 +00001560 "createCGImage", "fromRect", NULL);
Ted Kremeneka834fb42009-08-28 19:52:12 +00001561 addInstMethSummary("CIContext", CFAllocSumm,
Mike Stump1eb44332009-09-09 15:08:12 +00001562 "createCGImage", "fromRect", "format", "colorSpace", NULL);
Ted Kremeneka834fb42009-08-28 19:52:12 +00001563 addInstMethSummary("CIContext", CFAllocSumm, "createCGLayerWithSize",
Ted Kremenek211a9c62009-06-15 20:58:58 +00001564 "info", NULL);
Ted Kremenekb3c3c282008-05-06 00:38:54 +00001565}
1566
Ted Kremenekd3dbcf42008-05-05 22:11:16 +00001567//===----------------------------------------------------------------------===//
Ted Kremenek4d3957d2009-02-24 19:15:11 +00001568// AutoreleaseBindings - State used to track objects in autorelease pools.
Ted Kremenek6d348932008-10-21 15:53:15 +00001569//===----------------------------------------------------------------------===//
1570
Ted Kremenek4d3957d2009-02-24 19:15:11 +00001571typedef llvm::ImmutableMap<SymbolRef, unsigned> ARCounts;
1572typedef llvm::ImmutableMap<SymbolRef, ARCounts> ARPoolContents;
1573typedef llvm::ImmutableList<SymbolRef> ARStack;
Ted Kremenekf9a8e2e2009-02-23 17:45:03 +00001574
Ted Kremenek4d3957d2009-02-24 19:15:11 +00001575static int AutoRCIndex = 0;
Ted Kremenek6d348932008-10-21 15:53:15 +00001576static int AutoRBIndex = 0;
1577
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +00001578namespace { class AutoreleasePoolContents {}; }
1579namespace { class AutoreleaseStack {}; }
Ted Kremenek4d3957d2009-02-24 19:15:11 +00001580
Ted Kremenek6d348932008-10-21 15:53:15 +00001581namespace clang {
Ted Kremenek9ef65372010-12-23 07:20:52 +00001582namespace ento {
Ted Kremenek6c4becb2009-02-25 02:54:57 +00001583template<> struct GRStateTrait<AutoreleaseStack>
Ted Kremenek4d3957d2009-02-24 19:15:11 +00001584 : public GRStatePartialTrait<ARStack> {
Mike Stump1eb44332009-09-09 15:08:12 +00001585 static inline void* GDMIndex() { return &AutoRBIndex; }
Ted Kremenek4d3957d2009-02-24 19:15:11 +00001586};
1587
1588template<> struct GRStateTrait<AutoreleasePoolContents>
1589 : public GRStatePartialTrait<ARPoolContents> {
Mike Stump1eb44332009-09-09 15:08:12 +00001590 static inline void* GDMIndex() { return &AutoRCIndex; }
Ted Kremenek4d3957d2009-02-24 19:15:11 +00001591};
Argyrios Kyrtzidis5a4f98f2010-12-22 18:53:20 +00001592} // end GR namespace
Ted Kremenek4d3957d2009-02-24 19:15:11 +00001593} // end clang namespace
Ted Kremenek6d348932008-10-21 15:53:15 +00001594
Ted Kremenek7037ab82009-03-20 17:34:15 +00001595static SymbolRef GetCurrentAutoreleasePool(const GRState* state) {
1596 ARStack stack = state->get<AutoreleaseStack>();
1597 return stack.isEmpty() ? SymbolRef() : stack.getHead();
1598}
1599
Ted Kremenekb65be702009-06-18 01:23:53 +00001600static const GRState * SendAutorelease(const GRState *state,
1601 ARCounts::Factory &F, SymbolRef sym) {
Ted Kremenek7037ab82009-03-20 17:34:15 +00001602
1603 SymbolRef pool = GetCurrentAutoreleasePool(state);
Ted Kremenekb65be702009-06-18 01:23:53 +00001604 const ARCounts *cnts = state->get<AutoreleasePoolContents>(pool);
Ted Kremenek7037ab82009-03-20 17:34:15 +00001605 ARCounts newCnts(0);
Mike Stump1eb44332009-09-09 15:08:12 +00001606
Ted Kremenek7037ab82009-03-20 17:34:15 +00001607 if (cnts) {
1608 const unsigned *cnt = (*cnts).lookup(sym);
Ted Kremenek3baf6722010-11-24 00:54:37 +00001609 newCnts = F.add(*cnts, sym, cnt ? *cnt + 1 : 1);
Ted Kremenek7037ab82009-03-20 17:34:15 +00001610 }
1611 else
Ted Kremenek3baf6722010-11-24 00:54:37 +00001612 newCnts = F.add(F.getEmptyMap(), sym, 1);
Mike Stump1eb44332009-09-09 15:08:12 +00001613
Ted Kremenekb65be702009-06-18 01:23:53 +00001614 return state->set<AutoreleasePoolContents>(pool, newCnts);
Ted Kremenek7037ab82009-03-20 17:34:15 +00001615}
1616
Ted Kremenek13922612008-04-16 20:40:59 +00001617//===----------------------------------------------------------------------===//
1618// Transfer functions.
1619//===----------------------------------------------------------------------===//
1620
Ted Kremenek72cd17f2008-08-14 21:16:54 +00001621namespace {
Mike Stump1eb44332009-09-09 15:08:12 +00001622
Argyrios Kyrtzidisd2592a32010-12-22 18:53:44 +00001623class CFRefCount : public TransferFuncs {
Ted Kremenek8dd56462008-04-18 03:39:05 +00001624public:
Ted Kremenekae6814e2008-08-13 21:24:49 +00001625 class BindingsPrinter : public GRState::Printer {
Ted Kremenekf3948042008-03-11 19:44:10 +00001626 public:
Ted Kremenek53ba0b62009-06-24 23:06:47 +00001627 virtual void Print(llvm::raw_ostream& Out, const GRState* state,
Ted Kremenekae6814e2008-08-13 21:24:49 +00001628 const char* nl, const char* sep);
Ted Kremenekf3948042008-03-11 19:44:10 +00001629 };
Ted Kremenek8dd56462008-04-18 03:39:05 +00001630
1631private:
Zhongxing Xu031ccc02009-08-06 12:48:26 +00001632 typedef llvm::DenseMap<const ExplodedNode*, const RetainSummary*>
Mike Stump1eb44332009-09-09 15:08:12 +00001633 SummaryLogTy;
Ted Kremenekfe9e5432009-02-18 03:48:14 +00001634
Mike Stump1eb44332009-09-09 15:08:12 +00001635 RetainSummaryManager Summaries;
Ted Kremenekfe9e5432009-02-18 03:48:14 +00001636 SummaryLogTy SummaryLog;
Ted Kremenekd3dbcf42008-05-05 22:11:16 +00001637 const LangOptions& LOpts;
Ted Kremenek4d3957d2009-02-24 19:15:11 +00001638 ARCounts::Factory ARCountFactory;
Ted Kremenekb9d17f92008-08-17 03:20:02 +00001639
Ted Kremenekcf701772009-02-05 06:50:21 +00001640 BugType *useAfterRelease, *releaseNotOwned;
Ted Kremenekf95e9fc2009-03-17 19:42:23 +00001641 BugType *deallocGC, *deallocNotOwned;
Ted Kremenekcf701772009-02-05 06:50:21 +00001642 BugType *leakWithinFunction, *leakAtReturn;
Ted Kremenek369de562009-05-09 00:10:05 +00001643 BugType *overAutorelease;
Ted Kremeneke8720ce2009-05-10 06:25:57 +00001644 BugType *returnNotOwnedForOwned;
Ted Kremenekcf701772009-02-05 06:50:21 +00001645 BugReporter *BR;
Mike Stump1eb44332009-09-09 15:08:12 +00001646
Ted Kremenekb65be702009-06-18 01:23:53 +00001647 const GRState * Update(const GRState * state, SymbolRef sym, RefVal V, ArgEffect E,
Ted Kremenek4d3957d2009-02-24 19:15:11 +00001648 RefVal::Kind& hasErr);
1649
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001650 void ProcessNonLeakError(ExplodedNodeSet& Dst,
Argyrios Kyrtzidisd2592a32010-12-22 18:53:44 +00001651 StmtNodeBuilder& Builder,
Zhongxing Xu03509ae2010-07-20 06:22:24 +00001652 const Expr* NodeExpr, SourceRange ErrorRange,
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001653 ExplodedNode* Pred,
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001654 const GRState* St,
Ted Kremenek2dabd432008-12-05 02:27:51 +00001655 RefVal::Kind hasErr, SymbolRef Sym);
Mike Stump1eb44332009-09-09 15:08:12 +00001656
Ted Kremenekb65be702009-06-18 01:23:53 +00001657 const GRState * HandleSymbolDeath(const GRState * state, SymbolRef sid, RefVal V,
Ted Kremenek9d9d3a62009-05-08 23:09:42 +00001658 llvm::SmallVectorImpl<SymbolRef> &Leaked);
Mike Stump1eb44332009-09-09 15:08:12 +00001659
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001660 ExplodedNode* ProcessLeaks(const GRState * state,
Ted Kremenek9d9d3a62009-05-08 23:09:42 +00001661 llvm::SmallVectorImpl<SymbolRef> &Leaked,
1662 GenericNodeBuilder &Builder,
Argyrios Kyrtzidisd2592a32010-12-22 18:53:44 +00001663 ExprEngine &Eng,
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001664 ExplodedNode *Pred = 0);
Mike Stump1eb44332009-09-09 15:08:12 +00001665
1666public:
Ted Kremenek78d46242008-07-22 16:21:24 +00001667 CFRefCount(ASTContext& Ctx, bool gcenabled, const LangOptions& lopts)
Ted Kremenek377e2302008-04-29 05:33:51 +00001668 : Summaries(Ctx, gcenabled),
Ted Kremenekf95e9fc2009-03-17 19:42:23 +00001669 LOpts(lopts), useAfterRelease(0), releaseNotOwned(0),
1670 deallocGC(0), deallocNotOwned(0),
Ted Kremeneke8720ce2009-05-10 06:25:57 +00001671 leakWithinFunction(0), leakAtReturn(0), overAutorelease(0),
1672 returnNotOwnedForOwned(0), BR(0) {}
Mike Stump1eb44332009-09-09 15:08:12 +00001673
Ted Kremenekcf701772009-02-05 06:50:21 +00001674 virtual ~CFRefCount() {}
Mike Stump1eb44332009-09-09 15:08:12 +00001675
Argyrios Kyrtzidisd2592a32010-12-22 18:53:44 +00001676 void RegisterChecks(ExprEngine &Eng);
Mike Stump1eb44332009-09-09 15:08:12 +00001677
Ted Kremenek1c72ef02008-08-16 00:49:49 +00001678 virtual void RegisterPrinters(std::vector<GRState::Printer*>& Printers) {
1679 Printers.push_back(new BindingsPrinter());
Ted Kremenekf3948042008-03-11 19:44:10 +00001680 }
Mike Stump1eb44332009-09-09 15:08:12 +00001681
Ted Kremenekd3dbcf42008-05-05 22:11:16 +00001682 bool isGCEnabled() const { return Summaries.isGCEnabled(); }
Ted Kremenek072192b2008-04-30 23:47:44 +00001683 const LangOptions& getLangOptions() const { return LOpts; }
Mike Stump1eb44332009-09-09 15:08:12 +00001684
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001685 const RetainSummary *getSummaryOfNode(const ExplodedNode *N) const {
Ted Kremenekfe9e5432009-02-18 03:48:14 +00001686 SummaryLogTy::const_iterator I = SummaryLog.find(N);
1687 return I == SummaryLog.end() ? 0 : I->second;
1688 }
Mike Stump1eb44332009-09-09 15:08:12 +00001689
Ted Kremenek6b3a0f72008-03-11 06:39:11 +00001690 // Calls.
Ted Kremenekd3dbcf42008-05-05 22:11:16 +00001691
Ted Kremenek9c149532010-12-01 21:57:22 +00001692 void evalSummary(ExplodedNodeSet& Dst,
Argyrios Kyrtzidisd2592a32010-12-22 18:53:44 +00001693 ExprEngine& Eng,
1694 StmtNodeBuilder& Builder,
Zhongxing Xu03509ae2010-07-20 06:22:24 +00001695 const Expr* Ex,
Ted Kremenekd775c662010-05-21 21:57:00 +00001696 InstanceReceiver Receiver,
Ted Kremenek7faca822009-05-04 04:57:00 +00001697 const RetainSummary& Summ,
Ted Kremenek2ffbfd92009-12-03 08:25:47 +00001698 const MemRegion *Callee,
Zhongxing Xu03509ae2010-07-20 06:22:24 +00001699 ConstExprIterator arg_beg, ConstExprIterator arg_end,
Zhongxing Xua46e4d92009-12-02 05:49:12 +00001700 ExplodedNode* Pred, const GRState *state);
Mike Stump1eb44332009-09-09 15:08:12 +00001701
Ted Kremenek9c149532010-12-01 21:57:22 +00001702 virtual void evalCall(ExplodedNodeSet& Dst,
Argyrios Kyrtzidisd2592a32010-12-22 18:53:44 +00001703 ExprEngine& Eng,
1704 StmtNodeBuilder& Builder,
Zhongxing Xu03509ae2010-07-20 06:22:24 +00001705 const CallExpr* CE, SVal L,
Mike Stump1eb44332009-09-09 15:08:12 +00001706 ExplodedNode* Pred);
1707
1708
Ted Kremenek9c149532010-12-01 21:57:22 +00001709 virtual void evalObjCMessageExpr(ExplodedNodeSet& Dst,
Argyrios Kyrtzidisd2592a32010-12-22 18:53:44 +00001710 ExprEngine& Engine,
1711 StmtNodeBuilder& Builder,
Zhongxing Xu03509ae2010-07-20 06:22:24 +00001712 const ObjCMessageExpr* ME,
Zhongxing Xua46e4d92009-12-02 05:49:12 +00001713 ExplodedNode* Pred,
1714 const GRState *state);
Mike Stump1eb44332009-09-09 15:08:12 +00001715 // Stores.
Argyrios Kyrtzidisd2592a32010-12-22 18:53:44 +00001716 virtual void evalBind(StmtNodeBuilderRef& B, SVal location, SVal val);
Ted Kremenek41573eb2009-02-14 01:43:44 +00001717
Ted Kremeneke7bd9c22008-04-11 22:25:11 +00001718 // End-of-path.
Mike Stump1eb44332009-09-09 15:08:12 +00001719
Argyrios Kyrtzidisd2592a32010-12-22 18:53:44 +00001720 virtual void evalEndPath(ExprEngine& Engine,
1721 EndPathNodeBuilder& Builder);
Mike Stump1eb44332009-09-09 15:08:12 +00001722
Ted Kremenek9c149532010-12-01 21:57:22 +00001723 virtual void evalDeadSymbols(ExplodedNodeSet& Dst,
Argyrios Kyrtzidisd2592a32010-12-22 18:53:44 +00001724 ExprEngine& Engine,
1725 StmtNodeBuilder& Builder,
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001726 ExplodedNode* Pred,
Jordy Rose7dadf792010-07-01 20:09:55 +00001727 const GRState* state,
Ted Kremenek241677a2009-01-21 22:26:05 +00001728 SymbolReaper& SymReaper);
Mike Stump1eb44332009-09-09 15:08:12 +00001729
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001730 std::pair<ExplodedNode*, const GRState *>
Ted Kremenekb65be702009-06-18 01:23:53 +00001731 HandleAutoreleaseCounts(const GRState * state, GenericNodeBuilder Bd,
Argyrios Kyrtzidisd2592a32010-12-22 18:53:44 +00001732 ExplodedNode* Pred, ExprEngine &Eng,
Ted Kremenek369de562009-05-09 00:10:05 +00001733 SymbolRef Sym, RefVal V, bool &stop);
Ted Kremenek4fd88972008-04-17 18:12:53 +00001734 // Return statements.
Mike Stump1eb44332009-09-09 15:08:12 +00001735
Ted Kremenek9c149532010-12-01 21:57:22 +00001736 virtual void evalReturn(ExplodedNodeSet& Dst,
Argyrios Kyrtzidisd2592a32010-12-22 18:53:44 +00001737 ExprEngine& Engine,
1738 StmtNodeBuilder& Builder,
Zhongxing Xu03509ae2010-07-20 06:22:24 +00001739 const ReturnStmt* S,
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001740 ExplodedNode* Pred);
Ted Kremenekcb612922008-04-18 19:23:43 +00001741
1742 // Assumptions.
1743
Ted Kremenek9c149532010-12-01 21:57:22 +00001744 virtual const GRState *evalAssume(const GRState* state, SVal condition,
Ted Kremeneka591bc02009-06-18 22:57:13 +00001745 bool assumption);
Ted Kremenek6b3a0f72008-03-11 06:39:11 +00001746};
1747
1748} // end anonymous namespace
1749
Ted Kremenek53ba0b62009-06-24 23:06:47 +00001750static void PrintPool(llvm::raw_ostream &Out, SymbolRef Sym,
1751 const GRState *state) {
Ted Kremenek7037ab82009-03-20 17:34:15 +00001752 Out << ' ';
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +00001753 if (Sym)
1754 Out << Sym->getSymbolID();
Ted Kremenek7037ab82009-03-20 17:34:15 +00001755 else
1756 Out << "<pool>";
1757 Out << ":{";
Mike Stump1eb44332009-09-09 15:08:12 +00001758
Ted Kremenek7037ab82009-03-20 17:34:15 +00001759 // Get the contents of the pool.
1760 if (const ARCounts *cnts = state->get<AutoreleasePoolContents>(Sym))
1761 for (ARCounts::iterator J=cnts->begin(), EJ=cnts->end(); J != EJ; ++J)
1762 Out << '(' << J.getKey() << ',' << J.getData() << ')';
1763
Mike Stump1eb44332009-09-09 15:08:12 +00001764 Out << '}';
Ted Kremenek7037ab82009-03-20 17:34:15 +00001765}
Ted Kremenek8dd56462008-04-18 03:39:05 +00001766
Ted Kremenek53ba0b62009-06-24 23:06:47 +00001767void CFRefCount::BindingsPrinter::Print(llvm::raw_ostream& Out,
1768 const GRState* state,
Ted Kremenekae6814e2008-08-13 21:24:49 +00001769 const char* nl, const char* sep) {
Mike Stump1eb44332009-09-09 15:08:12 +00001770
Ted Kremenek72cd17f2008-08-14 21:16:54 +00001771 RefBindings B = state->get<RefBindings>();
Mike Stump1eb44332009-09-09 15:08:12 +00001772
Ted Kremenekae6814e2008-08-13 21:24:49 +00001773 if (!B.isEmpty())
Ted Kremenekf3948042008-03-11 19:44:10 +00001774 Out << sep << nl;
Mike Stump1eb44332009-09-09 15:08:12 +00001775
Ted Kremenekf3948042008-03-11 19:44:10 +00001776 for (RefBindings::iterator I=B.begin(), E=B.end(); I!=E; ++I) {
1777 Out << (*I).first << " : ";
1778 (*I).second.print(Out);
1779 Out << nl;
1780 }
Mike Stump1eb44332009-09-09 15:08:12 +00001781
Ted Kremenek6c4becb2009-02-25 02:54:57 +00001782 // Print the autorelease stack.
Ted Kremenek7037ab82009-03-20 17:34:15 +00001783 Out << sep << nl << "AR pool stack:";
Ted Kremenek6c4becb2009-02-25 02:54:57 +00001784 ARStack stack = state->get<AutoreleaseStack>();
Mike Stump1eb44332009-09-09 15:08:12 +00001785
Ted Kremenek7037ab82009-03-20 17:34:15 +00001786 PrintPool(Out, SymbolRef(), state); // Print the caller's pool.
1787 for (ARStack::iterator I=stack.begin(), E=stack.end(); I!=E; ++I)
1788 PrintPool(Out, *I, state);
1789
1790 Out << nl;
Ted Kremenekf3948042008-03-11 19:44:10 +00001791}
1792
Ted Kremenekc887d132009-04-29 18:50:19 +00001793//===----------------------------------------------------------------------===//
1794// Error reporting.
1795//===----------------------------------------------------------------------===//
1796
1797namespace {
Mike Stump1eb44332009-09-09 15:08:12 +00001798
Ted Kremenekc887d132009-04-29 18:50:19 +00001799 //===-------------===//
1800 // Bug Descriptions. //
Mike Stump1eb44332009-09-09 15:08:12 +00001801 //===-------------===//
1802
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +00001803 class CFRefBug : public BugType {
Ted Kremenekc887d132009-04-29 18:50:19 +00001804 protected:
1805 CFRefCount& TF;
Mike Stump1eb44332009-09-09 15:08:12 +00001806
Benjamin Kramerf0171732009-11-29 18:27:55 +00001807 CFRefBug(CFRefCount* tf, llvm::StringRef name)
Mike Stump1eb44332009-09-09 15:08:12 +00001808 : BugType(name, "Memory (Core Foundation/Objective-C)"), TF(*tf) {}
Ted Kremenekc887d132009-04-29 18:50:19 +00001809 public:
Mike Stump1eb44332009-09-09 15:08:12 +00001810
Ted Kremenekc887d132009-04-29 18:50:19 +00001811 CFRefCount& getTF() { return TF; }
Mike Stump1eb44332009-09-09 15:08:12 +00001812
Ted Kremenekc887d132009-04-29 18:50:19 +00001813 // FIXME: Eventually remove.
1814 virtual const char* getDescription() const = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001815
Ted Kremenekc887d132009-04-29 18:50:19 +00001816 virtual bool isLeak() const { return false; }
1817 };
Mike Stump1eb44332009-09-09 15:08:12 +00001818
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +00001819 class UseAfterRelease : public CFRefBug {
Ted Kremenekc887d132009-04-29 18:50:19 +00001820 public:
1821 UseAfterRelease(CFRefCount* tf)
1822 : CFRefBug(tf, "Use-after-release") {}
Mike Stump1eb44332009-09-09 15:08:12 +00001823
Ted Kremenekc887d132009-04-29 18:50:19 +00001824 const char* getDescription() const {
1825 return "Reference-counted object is used after it is released";
Mike Stump1eb44332009-09-09 15:08:12 +00001826 }
Ted Kremenekc887d132009-04-29 18:50:19 +00001827 };
Mike Stump1eb44332009-09-09 15:08:12 +00001828
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +00001829 class BadRelease : public CFRefBug {
Ted Kremenekc887d132009-04-29 18:50:19 +00001830 public:
1831 BadRelease(CFRefCount* tf) : CFRefBug(tf, "Bad release") {}
Mike Stump1eb44332009-09-09 15:08:12 +00001832
Ted Kremenekc887d132009-04-29 18:50:19 +00001833 const char* getDescription() const {
Ted Kremenekbb206fd2009-10-01 17:31:50 +00001834 return "Incorrect decrement of the reference count of an object that is "
1835 "not owned at this point by the caller";
Ted Kremenekc887d132009-04-29 18:50:19 +00001836 }
1837 };
Mike Stump1eb44332009-09-09 15:08:12 +00001838
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +00001839 class DeallocGC : public CFRefBug {
Ted Kremenekc887d132009-04-29 18:50:19 +00001840 public:
Ted Kremenek369de562009-05-09 00:10:05 +00001841 DeallocGC(CFRefCount *tf)
1842 : CFRefBug(tf, "-dealloc called while using garbage collection") {}
Mike Stump1eb44332009-09-09 15:08:12 +00001843
Ted Kremenekc887d132009-04-29 18:50:19 +00001844 const char *getDescription() const {
Ted Kremenek369de562009-05-09 00:10:05 +00001845 return "-dealloc called while using garbage collection";
Ted Kremenekc887d132009-04-29 18:50:19 +00001846 }
1847 };
Mike Stump1eb44332009-09-09 15:08:12 +00001848
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +00001849 class DeallocNotOwned : public CFRefBug {
Ted Kremenekc887d132009-04-29 18:50:19 +00001850 public:
Ted Kremenek369de562009-05-09 00:10:05 +00001851 DeallocNotOwned(CFRefCount *tf)
1852 : CFRefBug(tf, "-dealloc sent to non-exclusively owned object") {}
Mike Stump1eb44332009-09-09 15:08:12 +00001853
Ted Kremenekc887d132009-04-29 18:50:19 +00001854 const char *getDescription() const {
1855 return "-dealloc sent to object that may be referenced elsewhere";
1856 }
Mike Stump1eb44332009-09-09 15:08:12 +00001857 };
1858
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +00001859 class OverAutorelease : public CFRefBug {
Ted Kremenek369de562009-05-09 00:10:05 +00001860 public:
Mike Stump1eb44332009-09-09 15:08:12 +00001861 OverAutorelease(CFRefCount *tf) :
Ted Kremenek369de562009-05-09 00:10:05 +00001862 CFRefBug(tf, "Object sent -autorelease too many times") {}
Mike Stump1eb44332009-09-09 15:08:12 +00001863
Ted Kremenek369de562009-05-09 00:10:05 +00001864 const char *getDescription() const {
Ted Kremenekeaedfea2009-05-10 05:11:21 +00001865 return "Object sent -autorelease too many times";
Ted Kremenek369de562009-05-09 00:10:05 +00001866 }
1867 };
Mike Stump1eb44332009-09-09 15:08:12 +00001868
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +00001869 class ReturnedNotOwnedForOwned : public CFRefBug {
Ted Kremeneke8720ce2009-05-10 06:25:57 +00001870 public:
1871 ReturnedNotOwnedForOwned(CFRefCount *tf) :
1872 CFRefBug(tf, "Method should return an owned object") {}
Mike Stump1eb44332009-09-09 15:08:12 +00001873
Ted Kremeneke8720ce2009-05-10 06:25:57 +00001874 const char *getDescription() const {
1875 return "Object with +0 retain counts returned to caller where a +1 "
1876 "(owning) retain count is expected";
1877 }
1878 };
Mike Stump1eb44332009-09-09 15:08:12 +00001879
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +00001880 class Leak : public CFRefBug {
Ted Kremenekc887d132009-04-29 18:50:19 +00001881 const bool isReturn;
1882 protected:
Benjamin Kramerf0171732009-11-29 18:27:55 +00001883 Leak(CFRefCount* tf, llvm::StringRef name, bool isRet)
Ted Kremenekc887d132009-04-29 18:50:19 +00001884 : CFRefBug(tf, name), isReturn(isRet) {}
1885 public:
Mike Stump1eb44332009-09-09 15:08:12 +00001886
Ted Kremenekc887d132009-04-29 18:50:19 +00001887 const char* getDescription() const { return ""; }
Mike Stump1eb44332009-09-09 15:08:12 +00001888
Ted Kremenekc887d132009-04-29 18:50:19 +00001889 bool isLeak() const { return true; }
1890 };
Mike Stump1eb44332009-09-09 15:08:12 +00001891
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +00001892 class LeakAtReturn : public Leak {
Ted Kremenekc887d132009-04-29 18:50:19 +00001893 public:
Benjamin Kramerf0171732009-11-29 18:27:55 +00001894 LeakAtReturn(CFRefCount* tf, llvm::StringRef name)
Ted Kremenekc887d132009-04-29 18:50:19 +00001895 : Leak(tf, name, true) {}
1896 };
Mike Stump1eb44332009-09-09 15:08:12 +00001897
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +00001898 class LeakWithinFunction : public Leak {
Ted Kremenekc887d132009-04-29 18:50:19 +00001899 public:
Benjamin Kramerf0171732009-11-29 18:27:55 +00001900 LeakWithinFunction(CFRefCount* tf, llvm::StringRef name)
Ted Kremenekc887d132009-04-29 18:50:19 +00001901 : Leak(tf, name, false) {}
Mike Stump1eb44332009-09-09 15:08:12 +00001902 };
1903
Ted Kremenekc887d132009-04-29 18:50:19 +00001904 //===---------===//
1905 // Bug Reports. //
1906 //===---------===//
Mike Stump1eb44332009-09-09 15:08:12 +00001907
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +00001908 class CFRefReport : public RangedBugReport {
Ted Kremenekc887d132009-04-29 18:50:19 +00001909 protected:
1910 SymbolRef Sym;
1911 const CFRefCount &TF;
1912 public:
1913 CFRefReport(CFRefBug& D, const CFRefCount &tf,
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001914 ExplodedNode *n, SymbolRef sym)
Ted Kremenekeaedfea2009-05-10 05:11:21 +00001915 : RangedBugReport(D, D.getDescription(), n), Sym(sym), TF(tf) {}
1916
1917 CFRefReport(CFRefBug& D, const CFRefCount &tf,
Benjamin Kramerf0171732009-11-29 18:27:55 +00001918 ExplodedNode *n, SymbolRef sym, llvm::StringRef endText)
Zhongxing Xu264e9372009-05-12 10:10:00 +00001919 : RangedBugReport(D, D.getDescription(), endText, n), Sym(sym), TF(tf) {}
Mike Stump1eb44332009-09-09 15:08:12 +00001920
Ted Kremenekc887d132009-04-29 18:50:19 +00001921 virtual ~CFRefReport() {}
Mike Stump1eb44332009-09-09 15:08:12 +00001922
Argyrios Kyrtzidis640ccf02010-12-04 01:12:15 +00001923 CFRefBug& getBugType() const {
Ted Kremenekc887d132009-04-29 18:50:19 +00001924 return (CFRefBug&) RangedBugReport::getBugType();
1925 }
Mike Stump1eb44332009-09-09 15:08:12 +00001926
Argyrios Kyrtzidis640ccf02010-12-04 01:12:15 +00001927 virtual std::pair<ranges_iterator, ranges_iterator> getRanges() const {
Ted Kremenekc887d132009-04-29 18:50:19 +00001928 if (!getBugType().isLeak())
Argyrios Kyrtzidis640ccf02010-12-04 01:12:15 +00001929 return RangedBugReport::getRanges();
Ted Kremenekc887d132009-04-29 18:50:19 +00001930 else
Argyrios Kyrtzidis640ccf02010-12-04 01:12:15 +00001931 return std::make_pair(ranges_iterator(), ranges_iterator());
Ted Kremenekc887d132009-04-29 18:50:19 +00001932 }
Mike Stump1eb44332009-09-09 15:08:12 +00001933
Ted Kremenekc887d132009-04-29 18:50:19 +00001934 SymbolRef getSymbol() const { return Sym; }
Mike Stump1eb44332009-09-09 15:08:12 +00001935
Ted Kremenek8966bc12009-05-06 21:39:49 +00001936 PathDiagnosticPiece* getEndPath(BugReporterContext& BRC,
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001937 const ExplodedNode* N);
Mike Stump1eb44332009-09-09 15:08:12 +00001938
Ted Kremenekc887d132009-04-29 18:50:19 +00001939 std::pair<const char**,const char**> getExtraDescriptiveText();
Mike Stump1eb44332009-09-09 15:08:12 +00001940
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001941 PathDiagnosticPiece* VisitNode(const ExplodedNode* N,
1942 const ExplodedNode* PrevN,
Ted Kremenek8966bc12009-05-06 21:39:49 +00001943 BugReporterContext& BRC);
Ted Kremenekc887d132009-04-29 18:50:19 +00001944 };
Ted Kremenekeaedfea2009-05-10 05:11:21 +00001945
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +00001946 class CFRefLeakReport : public CFRefReport {
Ted Kremenekc887d132009-04-29 18:50:19 +00001947 SourceLocation AllocSite;
1948 const MemRegion* AllocBinding;
1949 public:
1950 CFRefLeakReport(CFRefBug& D, const CFRefCount &tf,
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001951 ExplodedNode *n, SymbolRef sym,
Argyrios Kyrtzidisd2592a32010-12-22 18:53:44 +00001952 ExprEngine& Eng);
Mike Stump1eb44332009-09-09 15:08:12 +00001953
Ted Kremenek8966bc12009-05-06 21:39:49 +00001954 PathDiagnosticPiece* getEndPath(BugReporterContext& BRC,
Zhongxing Xuc5619d92009-08-06 01:32:16 +00001955 const ExplodedNode* N);
Mike Stump1eb44332009-09-09 15:08:12 +00001956
Ted Kremenekc887d132009-04-29 18:50:19 +00001957 SourceLocation getLocation() const { return AllocSite; }
Mike Stump1eb44332009-09-09 15:08:12 +00001958 };
Ted Kremenekc887d132009-04-29 18:50:19 +00001959} // end anonymous namespace
1960
Mike Stump1eb44332009-09-09 15:08:12 +00001961
Ted Kremenekc887d132009-04-29 18:50:19 +00001962
1963static const char* Msgs[] = {
1964 // GC only
Mike Stump1eb44332009-09-09 15:08:12 +00001965 "Code is compiled to only use garbage collection",
Ted Kremenekc887d132009-04-29 18:50:19 +00001966 // No GC.
1967 "Code is compiled to use reference counts",
1968 // Hybrid, with GC.
1969 "Code is compiled to use either garbage collection (GC) or reference counts"
Mike Stump1eb44332009-09-09 15:08:12 +00001970 " (non-GC). The bug occurs with GC enabled",
Ted Kremenekc887d132009-04-29 18:50:19 +00001971 // Hybrid, without GC
1972 "Code is compiled to use either garbage collection (GC) or reference counts"
1973 " (non-GC). The bug occurs in non-GC mode"
1974};
1975
1976std::pair<const char**,const char**> CFRefReport::getExtraDescriptiveText() {
1977 CFRefCount& TF = static_cast<CFRefBug&>(getBugType()).getTF();
Mike Stump1eb44332009-09-09 15:08:12 +00001978
Ted Kremenekc887d132009-04-29 18:50:19 +00001979 switch (TF.getLangOptions().getGCMode()) {
1980 default:
1981 assert(false);
Mike Stump1eb44332009-09-09 15:08:12 +00001982
Ted Kremenekc887d132009-04-29 18:50:19 +00001983 case LangOptions::GCOnly:
1984 assert (TF.isGCEnabled());
Mike Stump1eb44332009-09-09 15:08:12 +00001985 return std::make_pair(&Msgs[0], &Msgs[0]+1);
1986
Ted Kremenekc887d132009-04-29 18:50:19 +00001987 case LangOptions::NonGC:
1988 assert (!TF.isGCEnabled());
1989 return std::make_pair(&Msgs[1], &Msgs[1]+1);
Mike Stump1eb44332009-09-09 15:08:12 +00001990
Ted Kremenekc887d132009-04-29 18:50:19 +00001991 case LangOptions::HybridGC:
1992 if (TF.isGCEnabled())
1993 return std::make_pair(&Msgs[2], &Msgs[2]+1);
1994 else
1995 return std::make_pair(&Msgs[3], &Msgs[3]+1);
1996 }
1997}
1998
1999static inline bool contains(const llvm::SmallVectorImpl<ArgEffect>& V,
2000 ArgEffect X) {
2001 for (llvm::SmallVectorImpl<ArgEffect>::const_iterator I=V.begin(), E=V.end();
2002 I!=E; ++I)
2003 if (*I == X) return true;
Mike Stump1eb44332009-09-09 15:08:12 +00002004
Ted Kremenekc887d132009-04-29 18:50:19 +00002005 return false;
2006}
2007
Zhongxing Xuc5619d92009-08-06 01:32:16 +00002008PathDiagnosticPiece* CFRefReport::VisitNode(const ExplodedNode* N,
2009 const ExplodedNode* PrevN,
Ted Kremenek8966bc12009-05-06 21:39:49 +00002010 BugReporterContext& BRC) {
Mike Stump1eb44332009-09-09 15:08:12 +00002011
Ted Kremenek2033a952009-05-13 07:12:33 +00002012 if (!isa<PostStmt>(N->getLocation()))
2013 return NULL;
Mike Stump1eb44332009-09-09 15:08:12 +00002014
Ted Kremenek8966bc12009-05-06 21:39:49 +00002015 // Check if the type state has changed.
Ted Kremenekb65be702009-06-18 01:23:53 +00002016 const GRState *PrevSt = PrevN->getState();
2017 const GRState *CurrSt = N->getState();
Mike Stump1eb44332009-09-09 15:08:12 +00002018
2019 const RefVal* CurrT = CurrSt->get<RefBindings>(Sym);
Ted Kremenekc887d132009-04-29 18:50:19 +00002020 if (!CurrT) return NULL;
Mike Stump1eb44332009-09-09 15:08:12 +00002021
Ted Kremenekb65be702009-06-18 01:23:53 +00002022 const RefVal &CurrV = *CurrT;
2023 const RefVal *PrevT = PrevSt->get<RefBindings>(Sym);
Mike Stump1eb44332009-09-09 15:08:12 +00002024
Ted Kremenekc887d132009-04-29 18:50:19 +00002025 // Create a string buffer to constain all the useful things we want
2026 // to tell the user.
2027 std::string sbuf;
2028 llvm::raw_string_ostream os(sbuf);
Mike Stump1eb44332009-09-09 15:08:12 +00002029
Ted Kremenekc887d132009-04-29 18:50:19 +00002030 // This is the allocation site since the previous node had no bindings
2031 // for this symbol.
2032 if (!PrevT) {
Ted Kremenek5f85e172009-07-22 22:35:28 +00002033 const Stmt* S = cast<PostStmt>(N->getLocation()).getStmt();
Mike Stump1eb44332009-09-09 15:08:12 +00002034
Ted Kremenek5f85e172009-07-22 22:35:28 +00002035 if (const CallExpr *CE = dyn_cast<CallExpr>(S)) {
Ted Kremenekc887d132009-04-29 18:50:19 +00002036 // Get the name of the callee (if it is available).
Ted Kremenek13976632010-02-08 16:18:51 +00002037 SVal X = CurrSt->getSValAsScalarOrLoc(CE->getCallee());
Ted Kremenekc887d132009-04-29 18:50:19 +00002038 if (const FunctionDecl* FD = X.getAsFunctionDecl())
Benjamin Kramer900fc632010-04-17 09:33:03 +00002039 os << "Call to function '" << FD << '\'';
Ted Kremenekc887d132009-04-29 18:50:19 +00002040 else
Mike Stump1eb44332009-09-09 15:08:12 +00002041 os << "function call";
2042 }
Ted Kremenekc887d132009-04-29 18:50:19 +00002043 else {
2044 assert (isa<ObjCMessageExpr>(S));
2045 os << "Method";
2046 }
Mike Stump1eb44332009-09-09 15:08:12 +00002047
Ted Kremenekc887d132009-04-29 18:50:19 +00002048 if (CurrV.getObjKind() == RetEffect::CF) {
2049 os << " returns a Core Foundation object with a ";
2050 }
2051 else {
2052 assert (CurrV.getObjKind() == RetEffect::ObjC);
2053 os << " returns an Objective-C object with a ";
2054 }
Mike Stump1eb44332009-09-09 15:08:12 +00002055
Ted Kremenekc887d132009-04-29 18:50:19 +00002056 if (CurrV.isOwned()) {
2057 os << "+1 retain count (owning reference).";
Mike Stump1eb44332009-09-09 15:08:12 +00002058
Ted Kremenekc887d132009-04-29 18:50:19 +00002059 if (static_cast<CFRefBug&>(getBugType()).getTF().isGCEnabled()) {
2060 assert(CurrV.getObjKind() == RetEffect::CF);
2061 os << " "
2062 "Core Foundation objects are not automatically garbage collected.";
2063 }
2064 }
2065 else {
2066 assert (CurrV.isNotOwned());
2067 os << "+0 retain count (non-owning reference).";
2068 }
Mike Stump1eb44332009-09-09 15:08:12 +00002069
Ted Kremenek8966bc12009-05-06 21:39:49 +00002070 PathDiagnosticLocation Pos(S, BRC.getSourceManager());
Ted Kremenekc887d132009-04-29 18:50:19 +00002071 return new PathDiagnosticEventPiece(Pos, os.str());
2072 }
Mike Stump1eb44332009-09-09 15:08:12 +00002073
Ted Kremenekc887d132009-04-29 18:50:19 +00002074 // Gather up the effects that were performed on the object at this
2075 // program point
2076 llvm::SmallVector<ArgEffect, 2> AEffects;
Mike Stump1eb44332009-09-09 15:08:12 +00002077
Ted Kremenek8966bc12009-05-06 21:39:49 +00002078 if (const RetainSummary *Summ =
2079 TF.getSummaryOfNode(BRC.getNodeResolver().getOriginalNode(N))) {
Ted Kremenekc887d132009-04-29 18:50:19 +00002080 // We only have summaries attached to nodes after evaluating CallExpr and
2081 // ObjCMessageExprs.
Ted Kremenek5f85e172009-07-22 22:35:28 +00002082 const Stmt* S = cast<PostStmt>(N->getLocation()).getStmt();
Mike Stump1eb44332009-09-09 15:08:12 +00002083
Ted Kremenek5f85e172009-07-22 22:35:28 +00002084 if (const CallExpr *CE = dyn_cast<CallExpr>(S)) {
Ted Kremenekc887d132009-04-29 18:50:19 +00002085 // Iterate through the parameter expressions and see if the symbol
2086 // was ever passed as an argument.
2087 unsigned i = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00002088
Ted Kremenek5f85e172009-07-22 22:35:28 +00002089 for (CallExpr::const_arg_iterator AI=CE->arg_begin(), AE=CE->arg_end();
Ted Kremenekc887d132009-04-29 18:50:19 +00002090 AI!=AE; ++AI, ++i) {
Mike Stump1eb44332009-09-09 15:08:12 +00002091
Ted Kremenekc887d132009-04-29 18:50:19 +00002092 // Retrieve the value of the argument. Is it the symbol
2093 // we are interested in?
Ted Kremenek13976632010-02-08 16:18:51 +00002094 if (CurrSt->getSValAsScalarOrLoc(*AI).getAsLocSymbol() != Sym)
Ted Kremenekc887d132009-04-29 18:50:19 +00002095 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00002096
Ted Kremenekc887d132009-04-29 18:50:19 +00002097 // We have an argument. Get the effect!
2098 AEffects.push_back(Summ->getArg(i));
2099 }
2100 }
Mike Stump1eb44332009-09-09 15:08:12 +00002101 else if (const ObjCMessageExpr *ME = dyn_cast<ObjCMessageExpr>(S)) {
Douglas Gregor04badcf2010-04-21 00:45:42 +00002102 if (const Expr *receiver = ME->getInstanceReceiver())
Ted Kremenek13976632010-02-08 16:18:51 +00002103 if (CurrSt->getSValAsScalarOrLoc(receiver).getAsLocSymbol() == Sym) {
Ted Kremenekc887d132009-04-29 18:50:19 +00002104 // The symbol we are tracking is the receiver.
2105 AEffects.push_back(Summ->getReceiverEffect());
2106 }
2107 }
2108 }
Mike Stump1eb44332009-09-09 15:08:12 +00002109
Ted Kremenekc887d132009-04-29 18:50:19 +00002110 do {
2111 // Get the previous type state.
2112 RefVal PrevV = *PrevT;
Mike Stump1eb44332009-09-09 15:08:12 +00002113
Ted Kremenekc887d132009-04-29 18:50:19 +00002114 // Specially handle -dealloc.
2115 if (!TF.isGCEnabled() && contains(AEffects, Dealloc)) {
2116 // Determine if the object's reference count was pushed to zero.
2117 assert(!(PrevV == CurrV) && "The typestate *must* have changed.");
2118 // We may not have transitioned to 'release' if we hit an error.
2119 // This case is handled elsewhere.
2120 if (CurrV.getKind() == RefVal::Released) {
Ted Kremenekf21332e2009-05-08 20:01:42 +00002121 assert(CurrV.getCombinedCounts() == 0);
Ted Kremenekc887d132009-04-29 18:50:19 +00002122 os << "Object released by directly sending the '-dealloc' message";
2123 break;
2124 }
2125 }
Mike Stump1eb44332009-09-09 15:08:12 +00002126
Ted Kremenekc887d132009-04-29 18:50:19 +00002127 // Specially handle CFMakeCollectable and friends.
2128 if (contains(AEffects, MakeCollectable)) {
2129 // Get the name of the function.
Ted Kremenek5f85e172009-07-22 22:35:28 +00002130 const Stmt* S = cast<PostStmt>(N->getLocation()).getStmt();
Ted Kremenek13976632010-02-08 16:18:51 +00002131 SVal X = CurrSt->getSValAsScalarOrLoc(cast<CallExpr>(S)->getCallee());
Ted Kremenekc887d132009-04-29 18:50:19 +00002132 const FunctionDecl* FD = X.getAsFunctionDecl();
2133 const std::string& FName = FD->getNameAsString();
Mike Stump1eb44332009-09-09 15:08:12 +00002134
Ted Kremenekc887d132009-04-29 18:50:19 +00002135 if (TF.isGCEnabled()) {
2136 // Determine if the object's reference count was pushed to zero.
2137 assert(!(PrevV == CurrV) && "The typestate *must* have changed.");
Mike Stump1eb44332009-09-09 15:08:12 +00002138
Ted Kremenekc887d132009-04-29 18:50:19 +00002139 os << "In GC mode a call to '" << FName
2140 << "' decrements an object's retain count and registers the "
2141 "object with the garbage collector. ";
Mike Stump1eb44332009-09-09 15:08:12 +00002142
Ted Kremenekc887d132009-04-29 18:50:19 +00002143 if (CurrV.getKind() == RefVal::Released) {
2144 assert(CurrV.getCount() == 0);
2145 os << "Since it now has a 0 retain count the object can be "
2146 "automatically collected by the garbage collector.";
2147 }
2148 else
2149 os << "An object must have a 0 retain count to be garbage collected. "
2150 "After this call its retain count is +" << CurrV.getCount()
2151 << '.';
2152 }
Mike Stump1eb44332009-09-09 15:08:12 +00002153 else
Ted Kremenekc887d132009-04-29 18:50:19 +00002154 os << "When GC is not enabled a call to '" << FName
2155 << "' has no effect on its argument.";
Mike Stump1eb44332009-09-09 15:08:12 +00002156
Ted Kremenekc887d132009-04-29 18:50:19 +00002157 // Nothing more to say.
2158 break;
2159 }
Mike Stump1eb44332009-09-09 15:08:12 +00002160
2161 // Determine if the typestate has changed.
Ted Kremenekc887d132009-04-29 18:50:19 +00002162 if (!(PrevV == CurrV))
2163 switch (CurrV.getKind()) {
2164 case RefVal::Owned:
2165 case RefVal::NotOwned:
Mike Stump1eb44332009-09-09 15:08:12 +00002166
Ted Kremenekf21332e2009-05-08 20:01:42 +00002167 if (PrevV.getCount() == CurrV.getCount()) {
2168 // Did an autorelease message get sent?
2169 if (PrevV.getAutoreleaseCount() == CurrV.getAutoreleaseCount())
2170 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00002171
Zhongxing Xu264e9372009-05-12 10:10:00 +00002172 assert(PrevV.getAutoreleaseCount() < CurrV.getAutoreleaseCount());
Ted Kremenekeaedfea2009-05-10 05:11:21 +00002173 os << "Object sent -autorelease message";
Ted Kremenekf21332e2009-05-08 20:01:42 +00002174 break;
2175 }
Mike Stump1eb44332009-09-09 15:08:12 +00002176
Ted Kremenekc887d132009-04-29 18:50:19 +00002177 if (PrevV.getCount() > CurrV.getCount())
2178 os << "Reference count decremented.";
2179 else
2180 os << "Reference count incremented.";
Mike Stump1eb44332009-09-09 15:08:12 +00002181
Ted Kremenekc887d132009-04-29 18:50:19 +00002182 if (unsigned Count = CurrV.getCount())
2183 os << " The object now has a +" << Count << " retain count.";
Mike Stump1eb44332009-09-09 15:08:12 +00002184
Ted Kremenekc887d132009-04-29 18:50:19 +00002185 if (PrevV.getKind() == RefVal::Released) {
2186 assert(TF.isGCEnabled() && CurrV.getCount() > 0);
2187 os << " The object is not eligible for garbage collection until the "
2188 "retain count reaches 0 again.";
2189 }
Mike Stump1eb44332009-09-09 15:08:12 +00002190
Ted Kremenekc887d132009-04-29 18:50:19 +00002191 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002192
Ted Kremenekc887d132009-04-29 18:50:19 +00002193 case RefVal::Released:
2194 os << "Object released.";
2195 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002196
Ted Kremenekc887d132009-04-29 18:50:19 +00002197 case RefVal::ReturnedOwned:
2198 os << "Object returned to caller as an owning reference (single retain "
2199 "count transferred to caller).";
2200 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002201
Ted Kremenekc887d132009-04-29 18:50:19 +00002202 case RefVal::ReturnedNotOwned:
2203 os << "Object returned to caller with a +0 (non-owning) retain count.";
2204 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002205
Ted Kremenekc887d132009-04-29 18:50:19 +00002206 default:
2207 return NULL;
2208 }
Mike Stump1eb44332009-09-09 15:08:12 +00002209
Ted Kremenekc887d132009-04-29 18:50:19 +00002210 // Emit any remaining diagnostics for the argument effects (if any).
2211 for (llvm::SmallVectorImpl<ArgEffect>::iterator I=AEffects.begin(),
2212 E=AEffects.end(); I != E; ++I) {
Mike Stump1eb44332009-09-09 15:08:12 +00002213
Ted Kremenekc887d132009-04-29 18:50:19 +00002214 // A bunch of things have alternate behavior under GC.
2215 if (TF.isGCEnabled())
2216 switch (*I) {
2217 default: break;
2218 case Autorelease:
2219 os << "In GC mode an 'autorelease' has no effect.";
2220 continue;
2221 case IncRefMsg:
2222 os << "In GC mode the 'retain' message has no effect.";
2223 continue;
2224 case DecRefMsg:
2225 os << "In GC mode the 'release' message has no effect.";
2226 continue;
2227 }
2228 }
Mike Stump1eb44332009-09-09 15:08:12 +00002229 } while (0);
2230
Ted Kremenekc887d132009-04-29 18:50:19 +00002231 if (os.str().empty())
2232 return 0; // We have nothing to say!
Ted Kremenek2033a952009-05-13 07:12:33 +00002233
Ted Kremenek5f85e172009-07-22 22:35:28 +00002234 const Stmt* S = cast<PostStmt>(N->getLocation()).getStmt();
Ted Kremenek8966bc12009-05-06 21:39:49 +00002235 PathDiagnosticLocation Pos(S, BRC.getSourceManager());
Ted Kremenekc887d132009-04-29 18:50:19 +00002236 PathDiagnosticPiece* P = new PathDiagnosticEventPiece(Pos, os.str());
Mike Stump1eb44332009-09-09 15:08:12 +00002237
Ted Kremenekc887d132009-04-29 18:50:19 +00002238 // Add the range by scanning the children of the statement for any bindings
2239 // to Sym.
Mike Stump1eb44332009-09-09 15:08:12 +00002240 for (Stmt::const_child_iterator I = S->child_begin(), E = S->child_end();
Ted Kremenek5f85e172009-07-22 22:35:28 +00002241 I!=E; ++I)
2242 if (const Expr* Exp = dyn_cast_or_null<Expr>(*I))
Ted Kremenek13976632010-02-08 16:18:51 +00002243 if (CurrSt->getSValAsScalarOrLoc(Exp).getAsLocSymbol() == Sym) {
Ted Kremenekc887d132009-04-29 18:50:19 +00002244 P->addRange(Exp->getSourceRange());
2245 break;
2246 }
Mike Stump1eb44332009-09-09 15:08:12 +00002247
Ted Kremenekc887d132009-04-29 18:50:19 +00002248 return P;
2249}
2250
2251namespace {
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +00002252 class FindUniqueBinding :
Ted Kremenekc887d132009-04-29 18:50:19 +00002253 public StoreManager::BindingsHandler {
2254 SymbolRef Sym;
2255 const MemRegion* Binding;
2256 bool First;
Mike Stump1eb44332009-09-09 15:08:12 +00002257
Ted Kremenekc887d132009-04-29 18:50:19 +00002258 public:
2259 FindUniqueBinding(SymbolRef sym) : Sym(sym), Binding(0), First(true) {}
Mike Stump1eb44332009-09-09 15:08:12 +00002260
Ted Kremenekc887d132009-04-29 18:50:19 +00002261 bool HandleBinding(StoreManager& SMgr, Store store, const MemRegion* R,
2262 SVal val) {
Mike Stump1eb44332009-09-09 15:08:12 +00002263
2264 SymbolRef SymV = val.getAsSymbol();
Ted Kremenekc887d132009-04-29 18:50:19 +00002265 if (!SymV || SymV != Sym)
2266 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00002267
Ted Kremenekc887d132009-04-29 18:50:19 +00002268 if (Binding) {
2269 First = false;
2270 return false;
2271 }
2272 else
2273 Binding = R;
Mike Stump1eb44332009-09-09 15:08:12 +00002274
2275 return true;
Ted Kremenekc887d132009-04-29 18:50:19 +00002276 }
Mike Stump1eb44332009-09-09 15:08:12 +00002277
Ted Kremenekc887d132009-04-29 18:50:19 +00002278 operator bool() { return First && Binding; }
2279 const MemRegion* getRegion() { return Binding; }
Mike Stump1eb44332009-09-09 15:08:12 +00002280 };
Ted Kremenekc887d132009-04-29 18:50:19 +00002281}
2282
Zhongxing Xuc5619d92009-08-06 01:32:16 +00002283static std::pair<const ExplodedNode*,const MemRegion*>
2284GetAllocationSite(GRStateManager& StateMgr, const ExplodedNode* N,
Ted Kremenekc887d132009-04-29 18:50:19 +00002285 SymbolRef Sym) {
Mike Stump1eb44332009-09-09 15:08:12 +00002286
Ted Kremenekc887d132009-04-29 18:50:19 +00002287 // Find both first node that referred to the tracked symbol and the
2288 // memory location that value was store to.
Zhongxing Xuc5619d92009-08-06 01:32:16 +00002289 const ExplodedNode* Last = N;
Mike Stump1eb44332009-09-09 15:08:12 +00002290 const MemRegion* FirstBinding = 0;
2291
Ted Kremenekc887d132009-04-29 18:50:19 +00002292 while (N) {
2293 const GRState* St = N->getState();
2294 RefBindings B = St->get<RefBindings>();
Mike Stump1eb44332009-09-09 15:08:12 +00002295
Ted Kremenekc887d132009-04-29 18:50:19 +00002296 if (!B.lookup(Sym))
2297 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002298
Ted Kremenekc887d132009-04-29 18:50:19 +00002299 FindUniqueBinding FB(Sym);
Mike Stump1eb44332009-09-09 15:08:12 +00002300 StateMgr.iterBindings(St, FB);
2301 if (FB) FirstBinding = FB.getRegion();
2302
Ted Kremenekc887d132009-04-29 18:50:19 +00002303 Last = N;
Mike Stump1eb44332009-09-09 15:08:12 +00002304 N = N->pred_empty() ? NULL : *(N->pred_begin());
Ted Kremenekc887d132009-04-29 18:50:19 +00002305 }
Mike Stump1eb44332009-09-09 15:08:12 +00002306
Ted Kremenekc887d132009-04-29 18:50:19 +00002307 return std::make_pair(Last, FirstBinding);
2308}
2309
2310PathDiagnosticPiece*
Ted Kremenek8966bc12009-05-06 21:39:49 +00002311CFRefReport::getEndPath(BugReporterContext& BRC,
Zhongxing Xuc5619d92009-08-06 01:32:16 +00002312 const ExplodedNode* EndN) {
Ted Kremenek8966bc12009-05-06 21:39:49 +00002313 // Tell the BugReporterContext to report cases when the tracked symbol is
Ted Kremenekc887d132009-04-29 18:50:19 +00002314 // assigned to different variables, etc.
Ted Kremenek8966bc12009-05-06 21:39:49 +00002315 BRC.addNotableSymbol(Sym);
2316 return RangedBugReport::getEndPath(BRC, EndN);
Ted Kremenekc887d132009-04-29 18:50:19 +00002317}
2318
2319PathDiagnosticPiece*
Ted Kremenek8966bc12009-05-06 21:39:49 +00002320CFRefLeakReport::getEndPath(BugReporterContext& BRC,
Zhongxing Xuc5619d92009-08-06 01:32:16 +00002321 const ExplodedNode* EndN){
Mike Stump1eb44332009-09-09 15:08:12 +00002322
Ted Kremenek8966bc12009-05-06 21:39:49 +00002323 // Tell the BugReporterContext to report cases when the tracked symbol is
Ted Kremenekc887d132009-04-29 18:50:19 +00002324 // assigned to different variables, etc.
Ted Kremenek8966bc12009-05-06 21:39:49 +00002325 BRC.addNotableSymbol(Sym);
Mike Stump1eb44332009-09-09 15:08:12 +00002326
Ted Kremenekc887d132009-04-29 18:50:19 +00002327 // We are reporting a leak. Walk up the graph to get to the first node where
2328 // the symbol appeared, and also get the first VarDecl that tracked object
2329 // is stored to.
Zhongxing Xuc5619d92009-08-06 01:32:16 +00002330 const ExplodedNode* AllocNode = 0;
Ted Kremenekc887d132009-04-29 18:50:19 +00002331 const MemRegion* FirstBinding = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00002332
Ted Kremenekc887d132009-04-29 18:50:19 +00002333 llvm::tie(AllocNode, FirstBinding) =
Ted Kremenekf04dced2009-05-08 23:32:51 +00002334 GetAllocationSite(BRC.getStateManager(), EndN, Sym);
Mike Stump1eb44332009-09-09 15:08:12 +00002335
2336 // Get the allocate site.
Ted Kremenekc887d132009-04-29 18:50:19 +00002337 assert(AllocNode);
Ted Kremenek5f85e172009-07-22 22:35:28 +00002338 const Stmt* FirstStmt = cast<PostStmt>(AllocNode->getLocation()).getStmt();
Mike Stump1eb44332009-09-09 15:08:12 +00002339
Ted Kremenek8966bc12009-05-06 21:39:49 +00002340 SourceManager& SMgr = BRC.getSourceManager();
Ted Kremenekc887d132009-04-29 18:50:19 +00002341 unsigned AllocLine =SMgr.getInstantiationLineNumber(FirstStmt->getLocStart());
Mike Stump1eb44332009-09-09 15:08:12 +00002342
Ted Kremenekc887d132009-04-29 18:50:19 +00002343 // Compute an actual location for the leak. Sometimes a leak doesn't
2344 // occur at an actual statement (e.g., transition between blocks; end
2345 // of function) so we need to walk the graph and compute a real location.
Zhongxing Xuc5619d92009-08-06 01:32:16 +00002346 const ExplodedNode* LeakN = EndN;
Ted Kremenekc887d132009-04-29 18:50:19 +00002347 PathDiagnosticLocation L;
Mike Stump1eb44332009-09-09 15:08:12 +00002348
Ted Kremenekc887d132009-04-29 18:50:19 +00002349 while (LeakN) {
2350 ProgramPoint P = LeakN->getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +00002351
Ted Kremenekc887d132009-04-29 18:50:19 +00002352 if (const PostStmt *PS = dyn_cast<PostStmt>(&P)) {
2353 L = PathDiagnosticLocation(PS->getStmt()->getLocStart(), SMgr);
2354 break;
2355 }
2356 else if (const BlockEdge *BE = dyn_cast<BlockEdge>(&P)) {
2357 if (const Stmt* Term = BE->getSrc()->getTerminator()) {
2358 L = PathDiagnosticLocation(Term->getLocStart(), SMgr);
2359 break;
2360 }
2361 }
Mike Stump1eb44332009-09-09 15:08:12 +00002362
Ted Kremenekc887d132009-04-29 18:50:19 +00002363 LeakN = LeakN->succ_empty() ? 0 : *(LeakN->succ_begin());
2364 }
Mike Stump1eb44332009-09-09 15:08:12 +00002365
Ted Kremenekc887d132009-04-29 18:50:19 +00002366 if (!L.isValid()) {
Zhongxing Xub317f8f2009-09-10 05:44:00 +00002367 const Decl &D = EndN->getCodeDecl();
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +00002368 L = PathDiagnosticLocation(D.getBodyRBrace(), SMgr);
Ted Kremenekc887d132009-04-29 18:50:19 +00002369 }
Mike Stump1eb44332009-09-09 15:08:12 +00002370
Ted Kremenekc887d132009-04-29 18:50:19 +00002371 std::string sbuf;
2372 llvm::raw_string_ostream os(sbuf);
Mike Stump1eb44332009-09-09 15:08:12 +00002373
Ted Kremenekc887d132009-04-29 18:50:19 +00002374 os << "Object allocated on line " << AllocLine;
Mike Stump1eb44332009-09-09 15:08:12 +00002375
Ted Kremenekc887d132009-04-29 18:50:19 +00002376 if (FirstBinding)
Mike Stump1eb44332009-09-09 15:08:12 +00002377 os << " and stored into '" << FirstBinding->getString() << '\'';
2378
Ted Kremenekc887d132009-04-29 18:50:19 +00002379 // Get the retain count.
2380 const RefVal* RV = EndN->getState()->get<RefBindings>(Sym);
Mike Stump1eb44332009-09-09 15:08:12 +00002381
Ted Kremenekc887d132009-04-29 18:50:19 +00002382 if (RV->getKind() == RefVal::ErrorLeakReturned) {
2383 // FIXME: Per comments in rdar://6320065, "create" only applies to CF
2384 // ojbects. Only "copy", "alloc", "retain" and "new" transfer ownership
2385 // to the caller for NS objects.
Zhongxing Xub317f8f2009-09-10 05:44:00 +00002386 ObjCMethodDecl& MD = cast<ObjCMethodDecl>(EndN->getCodeDecl());
Ted Kremenekc887d132009-04-29 18:50:19 +00002387 os << " is returned from a method whose name ('"
Ted Kremeneka8833552009-04-29 23:03:22 +00002388 << MD.getSelector().getAsString()
Ted Kremenekc887d132009-04-29 18:50:19 +00002389 << "') does not contain 'copy' or otherwise starts with"
2390 " 'new' or 'alloc'. This violates the naming convention rules given"
Ted Kremenek8987a022009-04-29 22:25:52 +00002391 " in the Memory Management Guide for Cocoa (object leaked)";
Ted Kremenekc887d132009-04-29 18:50:19 +00002392 }
Ted Kremeneke8720ce2009-05-10 06:25:57 +00002393 else if (RV->getKind() == RefVal::ErrorGCLeakReturned) {
Zhongxing Xub317f8f2009-09-10 05:44:00 +00002394 ObjCMethodDecl& MD = cast<ObjCMethodDecl>(EndN->getCodeDecl());
Ted Kremeneke8720ce2009-05-10 06:25:57 +00002395 os << " and returned from method '" << MD.getSelector().getAsString()
Ted Kremenek82f2be52009-05-10 16:52:15 +00002396 << "' is potentially leaked when using garbage collection. Callers "
2397 "of this method do not expect a returned object with a +1 retain "
2398 "count since they expect the object to be managed by the garbage "
2399 "collector";
Ted Kremeneke8720ce2009-05-10 06:25:57 +00002400 }
Ted Kremenekc887d132009-04-29 18:50:19 +00002401 else
Ted Kremenekabf517c2010-10-15 22:50:23 +00002402 os << " is not referenced later in this execution path and has a retain "
2403 "count of +" << RV->getCount() << " (object leaked)";
Mike Stump1eb44332009-09-09 15:08:12 +00002404
Ted Kremenekc887d132009-04-29 18:50:19 +00002405 return new PathDiagnosticEventPiece(L, os.str());
2406}
2407
Ted Kremenekc887d132009-04-29 18:50:19 +00002408CFRefLeakReport::CFRefLeakReport(CFRefBug& D, const CFRefCount &tf,
Zhongxing Xuc5619d92009-08-06 01:32:16 +00002409 ExplodedNode *n,
Argyrios Kyrtzidisd2592a32010-12-22 18:53:44 +00002410 SymbolRef sym, ExprEngine& Eng)
Mike Stump1eb44332009-09-09 15:08:12 +00002411: CFRefReport(D, tf, n, sym) {
2412
Ted Kremenekc887d132009-04-29 18:50:19 +00002413 // Most bug reports are cached at the location where they occured.
2414 // With leaks, we want to unique them by the location where they were
2415 // allocated, and only report a single path. To do this, we need to find
2416 // the allocation site of a piece of tracked memory, which we do via a
2417 // call to GetAllocationSite. This will walk the ExplodedGraph backwards.
2418 // Note that this is *not* the trimmed graph; we are guaranteed, however,
2419 // that all ancestor nodes that represent the allocation site have the
2420 // same SourceLocation.
Zhongxing Xuc5619d92009-08-06 01:32:16 +00002421 const ExplodedNode* AllocNode = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00002422
Ted Kremenekc887d132009-04-29 18:50:19 +00002423 llvm::tie(AllocNode, AllocBinding) = // Set AllocBinding.
Tom Care212f6d32010-09-16 03:50:38 +00002424 GetAllocationSite(Eng.getStateManager(), getErrorNode(), getSymbol());
Mike Stump1eb44332009-09-09 15:08:12 +00002425
Ted Kremenekc887d132009-04-29 18:50:19 +00002426 // Get the SourceLocation for the allocation site.
2427 ProgramPoint P = AllocNode->getLocation();
2428 AllocSite = cast<PostStmt>(P).getStmt()->getLocStart();
Mike Stump1eb44332009-09-09 15:08:12 +00002429
Ted Kremenekc887d132009-04-29 18:50:19 +00002430 // Fill in the description of the bug.
2431 Description.clear();
2432 llvm::raw_string_ostream os(Description);
2433 SourceManager& SMgr = Eng.getContext().getSourceManager();
2434 unsigned AllocLine = SMgr.getInstantiationLineNumber(AllocSite);
Ted Kremenekdd924e22009-05-02 19:05:19 +00002435 os << "Potential leak ";
2436 if (tf.isGCEnabled()) {
2437 os << "(when using garbage collection) ";
Mike Stump1eb44332009-09-09 15:08:12 +00002438 }
Ted Kremenekdd924e22009-05-02 19:05:19 +00002439 os << "of an object allocated on line " << AllocLine;
Mike Stump1eb44332009-09-09 15:08:12 +00002440
Ted Kremenekc887d132009-04-29 18:50:19 +00002441 // FIXME: AllocBinding doesn't get populated for RegionStore yet.
2442 if (AllocBinding)
2443 os << " and stored into '" << AllocBinding->getString() << '\'';
2444}
2445
2446//===----------------------------------------------------------------------===//
2447// Main checker logic.
2448//===----------------------------------------------------------------------===//
2449
Ted Kremenek553cf182008-06-25 21:21:56 +00002450/// GetReturnType - Used to get the return type of a message expression or
2451/// function call with the intention of affixing that type to a tracked symbol.
2452/// While the the return type can be queried directly from RetEx, when
2453/// invoking class methods we augment to the return type to be that of
2454/// a pointer to the class (as opposed it just being id).
Steve Naroff14108da2009-07-10 23:34:53 +00002455static QualType GetReturnType(const Expr* RetE, ASTContext& Ctx) {
Ted Kremenek553cf182008-06-25 21:21:56 +00002456 QualType RetTy = RetE->getType();
Steve Naroff14108da2009-07-10 23:34:53 +00002457 // If RetE is not a message expression just return its type.
2458 // If RetE is a message expression, return its types if it is something
Ted Kremenek553cf182008-06-25 21:21:56 +00002459 /// more specific than id.
Steve Naroff14108da2009-07-10 23:34:53 +00002460 if (const ObjCMessageExpr *ME = dyn_cast<ObjCMessageExpr>(RetE))
John McCall183700f2009-09-21 23:43:11 +00002461 if (const ObjCObjectPointerType *PT = RetTy->getAs<ObjCObjectPointerType>())
Mike Stump1eb44332009-09-09 15:08:12 +00002462 if (PT->isObjCQualifiedIdType() || PT->isObjCIdType() ||
Steve Naroff14108da2009-07-10 23:34:53 +00002463 PT->isObjCClassType()) {
2464 // At this point we know the return type of the message expression is
2465 // id, id<...>, or Class. If we have an ObjCInterfaceDecl, we know this
2466 // is a call to a class method whose type we can resolve. In such
2467 // cases, promote the return type to XXX* (where XXX is the class).
Douglas Gregor04badcf2010-04-21 00:45:42 +00002468 const ObjCInterfaceDecl *D = ME->getReceiverInterface();
John McCallc12c5bb2010-05-15 11:32:37 +00002469 return !D ? RetTy :
2470 Ctx.getObjCObjectPointerType(Ctx.getObjCInterfaceType(D));
Steve Naroff14108da2009-07-10 23:34:53 +00002471 }
Mike Stump1eb44332009-09-09 15:08:12 +00002472
Steve Naroff14108da2009-07-10 23:34:53 +00002473 return RetTy;
Ted Kremenek553cf182008-06-25 21:21:56 +00002474}
2475
Ted Kremenek9c149532010-12-01 21:57:22 +00002476void CFRefCount::evalSummary(ExplodedNodeSet& Dst,
Argyrios Kyrtzidisd2592a32010-12-22 18:53:44 +00002477 ExprEngine& Eng,
2478 StmtNodeBuilder& Builder,
Zhongxing Xu03509ae2010-07-20 06:22:24 +00002479 const Expr* Ex,
Ted Kremenekd775c662010-05-21 21:57:00 +00002480 InstanceReceiver Receiver,
Ted Kremenek7faca822009-05-04 04:57:00 +00002481 const RetainSummary& Summ,
Ted Kremenek2ffbfd92009-12-03 08:25:47 +00002482 const MemRegion *Callee,
Zhongxing Xu03509ae2010-07-20 06:22:24 +00002483 ConstExprIterator arg_beg,
2484 ConstExprIterator arg_end,
Zhongxing Xua46e4d92009-12-02 05:49:12 +00002485 ExplodedNode* Pred, const GRState *state) {
Ted Kremenek14993892008-05-06 02:41:27 +00002486
2487 // Evaluate the effect of the arguments.
Ted Kremenek9ed18e62008-04-16 04:28:53 +00002488 RefVal::Kind hasErr = (RefVal::Kind) 0;
Ted Kremenek00a3a5f2008-03-12 01:21:45 +00002489 unsigned idx = 0;
Ted Kremenek8f326752010-05-21 21:56:53 +00002490 SourceRange ErrorRange;
Mike Stump1eb44332009-09-09 15:08:12 +00002491 SymbolRef ErrorSym = 0;
2492
Ted Kremenek81a95832009-12-03 03:27:11 +00002493 llvm::SmallVector<const MemRegion*, 10> RegionsToInvalidate;
Ted Kremenekdcee3ce2010-07-01 20:16:50 +00002494
Ted Kremenek074ae352010-08-02 21:59:12 +00002495 // HACK: Symbols that have ref-count state that are referenced directly
2496 // (not as structure or array elements, or via bindings) by an argument
2497 // should not have their ref-count state stripped after we have
2498 // done an invalidation pass.
2499 llvm::DenseSet<SymbolRef> WhitelistedSymbols;
2500
Zhongxing Xu03509ae2010-07-20 06:22:24 +00002501 for (ConstExprIterator I = arg_beg; I != arg_end; ++I, ++idx) {
Ted Kremenek13976632010-02-08 16:18:51 +00002502 SVal V = state->getSValAsScalarOrLoc(*I);
Ted Kremenek94c96982009-03-03 22:06:47 +00002503 SymbolRef Sym = V.getAsLocSymbol();
Ted Kremenek3f4d5ab2009-03-04 00:13:50 +00002504
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +00002505 if (Sym)
Ted Kremenekb65be702009-06-18 01:23:53 +00002506 if (RefBindings::data_type* T = state->get<RefBindings>(Sym)) {
Ted Kremenek074ae352010-08-02 21:59:12 +00002507 WhitelistedSymbols.insert(Sym);
Ted Kremenek7faca822009-05-04 04:57:00 +00002508 state = Update(state, Sym, *T, Summ.getArg(idx), hasErr);
Ted Kremenek4d3957d2009-02-24 19:15:11 +00002509 if (hasErr) {
Ted Kremenek8f326752010-05-21 21:56:53 +00002510 ErrorRange = (*I)->getSourceRange();
Ted Kremeneke8fdc832008-07-07 16:21:19 +00002511 ErrorSym = Sym;
Ted Kremenekbcf50ad2008-04-11 18:40:51 +00002512 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002513 }
Ted Kremenek4d3957d2009-02-24 19:15:11 +00002514 }
Ted Kremenek070a8252008-07-09 18:11:16 +00002515
Ted Kremenekcc969fd2009-09-22 04:48:39 +00002516 tryAgain:
Ted Kremenek94c96982009-03-03 22:06:47 +00002517 if (isa<Loc>(V)) {
2518 if (loc::MemRegionVal* MR = dyn_cast<loc::MemRegionVal>(&V)) {
Ted Kremenek7faca822009-05-04 04:57:00 +00002519 if (Summ.getArg(idx) == DoNothingByRef)
Ted Kremenek070a8252008-07-09 18:11:16 +00002520 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00002521
2522 // Invalidate the value of the variable passed by reference.
Zhongxing Xu313b6da2009-07-06 06:01:24 +00002523 const MemRegion *R = MR->getRegion();
Ted Kremenek81a95832009-12-03 03:27:11 +00002524
Zhongxing Xu313b6da2009-07-06 06:01:24 +00002525 // Are we dealing with an ElementRegion? If the element type is
2526 // a basic integer type (e.g., char, int) and the underying region
2527 // is a variable region then strip off the ElementRegion.
2528 // FIXME: We really need to think about this for the general case
2529 // as sometimes we are reasoning about arrays and other times
2530 // about (char*), etc., is just a form of passing raw bytes.
2531 // e.g., void *p = alloca(); foo((char*)p);
2532 if (const ElementRegion *ER = dyn_cast<ElementRegion>(R)) {
2533 // Checking for 'integral type' is probably too promiscuous, but
2534 // we'll leave it in for now until we have a systematic way of
2535 // handling all of these cases. Eventually we need to come up
2536 // with an interface to StoreManager so that this logic can be
2537 // approriately delegated to the respective StoreManagers while
2538 // still allowing us to do checker-specific logic (e.g.,
2539 // invalidating reference counts), probably via callbacks.
Douglas Gregor2ade35e2010-06-16 00:17:44 +00002540 if (ER->getElementType()->isIntegralOrEnumerationType()) {
Zhongxing Xu313b6da2009-07-06 06:01:24 +00002541 const MemRegion *superReg = ER->getSuperRegion();
2542 if (isa<VarRegion>(superReg) || isa<FieldRegion>(superReg) ||
2543 isa<ObjCIvarRegion>(superReg))
2544 R = cast<TypedRegion>(superReg);
Ted Kremenek42530512009-05-06 18:19:24 +00002545 }
Zhongxing Xu313b6da2009-07-06 06:01:24 +00002546 // FIXME: What about layers of ElementRegions?
2547 }
Ted Kremenekdcee3ce2010-07-01 20:16:50 +00002548
Ted Kremenek81a95832009-12-03 03:27:11 +00002549 // Mark this region for invalidation. We batch invalidate regions
2550 // below for efficiency.
2551 RegionsToInvalidate.push_back(R);
2552 continue;
Ted Kremenek8c5633e2008-07-03 23:26:32 +00002553 }
2554 else {
2555 // Nuke all other arguments passed by reference.
Ted Kremenekcc969fd2009-09-22 04:48:39 +00002556 // FIXME: is this necessary or correct? This handles the non-Region
2557 // cases. Is it ever valid to store to these?
Ted Kremenekb65be702009-06-18 01:23:53 +00002558 state = state->unbindLoc(cast<Loc>(V));
Ted Kremenek8c5633e2008-07-03 23:26:32 +00002559 }
Ted Kremenekb8873552008-04-11 20:51:02 +00002560 }
Ted Kremenekcc969fd2009-09-22 04:48:39 +00002561 else if (isa<nonloc::LocAsInteger>(V)) {
2562 // If we are passing a location wrapped as an integer, unwrap it and
2563 // invalidate the values referred by the location.
2564 V = cast<nonloc::LocAsInteger>(V).getLoc();
2565 goto tryAgain;
2566 }
Mike Stump1eb44332009-09-09 15:08:12 +00002567 }
Ted Kremenekdcee3ce2010-07-01 20:16:50 +00002568
Ted Kremenek2ffbfd92009-12-03 08:25:47 +00002569 // Block calls result in all captured values passed-via-reference to be
2570 // invalidated.
2571 if (const BlockDataRegion *BR = dyn_cast_or_null<BlockDataRegion>(Callee)) {
2572 RegionsToInvalidate.push_back(BR);
2573 }
Ted Kremenekdcee3ce2010-07-01 20:16:50 +00002574
Ted Kremenek81a95832009-12-03 03:27:11 +00002575 // Invalidate regions we designed for invalidation use the batch invalidation
2576 // API.
Ted Kremenek81a95832009-12-03 03:27:11 +00002577
Ted Kremenekdcee3ce2010-07-01 20:16:50 +00002578 // FIXME: We can have collisions on the conjured symbol if the
2579 // expression *I also creates conjured symbols. We probably want
2580 // to identify conjured symbols by an expression pair: the enclosing
2581 // expression (the context) and the expression itself. This should
2582 // disambiguate conjured symbols.
2583 unsigned Count = Builder.getCurrentBlockCount();
Ted Kremenekdcee3ce2010-07-01 20:16:50 +00002584 StoreManager::InvalidatedSymbols IS;
Ted Kremenekdcee3ce2010-07-01 20:16:50 +00002585
2586 // NOTE: Even if RegionsToInvalidate is empty, we must still invalidate
2587 // global variables.
Jordy Roseff59efd2010-08-03 20:44:35 +00002588 state = state->InvalidateRegions(RegionsToInvalidate.data(),
2589 RegionsToInvalidate.data() +
2590 RegionsToInvalidate.size(),
2591 Ex, Count, &IS,
2592 /* invalidateGlobals = */ true);
Ted Kremenekdcee3ce2010-07-01 20:16:50 +00002593
Ted Kremenekdcee3ce2010-07-01 20:16:50 +00002594 for (StoreManager::InvalidatedSymbols::iterator I = IS.begin(),
2595 E = IS.end(); I!=E; ++I) {
Ted Kremenek074ae352010-08-02 21:59:12 +00002596 SymbolRef sym = *I;
2597 if (WhitelistedSymbols.count(sym))
2598 continue;
2599 // Remove any existing reference-count binding.
Ted Kremenekdcee3ce2010-07-01 20:16:50 +00002600 state = state->remove<RefBindings>(*I);
Ted Kremenek81a95832009-12-03 03:27:11 +00002601 }
Mike Stump1eb44332009-09-09 15:08:12 +00002602
2603 // Evaluate the effect on the message receiver.
Ted Kremenek8f326752010-05-21 21:56:53 +00002604 if (!ErrorRange.isValid() && Receiver) {
Ted Kremenekd775c662010-05-21 21:57:00 +00002605 SymbolRef Sym = Receiver.getSValAsScalarOrLoc(state).getAsLocSymbol();
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +00002606 if (Sym) {
Ted Kremenekb65be702009-06-18 01:23:53 +00002607 if (const RefVal* T = state->get<RefBindings>(Sym)) {
Ted Kremenek7faca822009-05-04 04:57:00 +00002608 state = Update(state, Sym, *T, Summ.getReceiverEffect(), hasErr);
Ted Kremenek4d3957d2009-02-24 19:15:11 +00002609 if (hasErr) {
Ted Kremenekd775c662010-05-21 21:57:00 +00002610 ErrorRange = Receiver.getSourceRange();
Ted Kremeneke8fdc832008-07-07 16:21:19 +00002611 ErrorSym = Sym;
Ted Kremenek14993892008-05-06 02:41:27 +00002612 }
Ted Kremenek4d3957d2009-02-24 19:15:11 +00002613 }
Ted Kremenek14993892008-05-06 02:41:27 +00002614 }
2615 }
Mike Stump1eb44332009-09-09 15:08:12 +00002616
2617 // Process any errors.
Ted Kremenek9ed18e62008-04-16 04:28:53 +00002618 if (hasErr) {
Ted Kremenek8f326752010-05-21 21:56:53 +00002619 ProcessNonLeakError(Dst, Builder, Ex, ErrorRange, Pred, state,
Ted Kremenek8dd56462008-04-18 03:39:05 +00002620 hasErr, ErrorSym);
Ted Kremenek00a3a5f2008-03-12 01:21:45 +00002621 return;
Ted Kremenek1ac08d62008-03-11 17:48:22 +00002622 }
Mike Stump1eb44332009-09-09 15:08:12 +00002623
2624 // Consult the summary for the return value.
Ted Kremenek7faca822009-05-04 04:57:00 +00002625 RetEffect RE = Summ.getRetEffect();
Mike Stump1eb44332009-09-09 15:08:12 +00002626
Ted Kremenek78a35a32009-05-12 20:06:54 +00002627 if (RE.getKind() == RetEffect::OwnedWhenTrackedReceiver) {
Ted Kremenek78a35a32009-05-12 20:06:54 +00002628 bool found = false;
Douglas Gregor04badcf2010-04-21 00:45:42 +00002629 if (Receiver) {
Ted Kremenekd775c662010-05-21 21:57:00 +00002630 SVal V = Receiver.getSValAsScalarOrLoc(state);
Douglas Gregor04badcf2010-04-21 00:45:42 +00002631 if (SymbolRef Sym = V.getAsLocSymbol())
2632 if (state->get<RefBindings>(Sym)) {
2633 found = true;
2634 RE = Summaries.getObjAllocRetEffect();
2635 }
2636 } // FIXME: Otherwise, this is a send-to-super instance message.
Ted Kremenek78a35a32009-05-12 20:06:54 +00002637 if (!found)
2638 RE = RetEffect::MakeNoRet();
Mike Stump1eb44332009-09-09 15:08:12 +00002639 }
2640
Ted Kremenek00a3a5f2008-03-12 01:21:45 +00002641 switch (RE.getKind()) {
2642 default:
2643 assert (false && "Unhandled RetEffect."); break;
Mike Stump1eb44332009-09-09 15:08:12 +00002644
2645 case RetEffect::NoRet: {
Ted Kremenekf9561e52008-04-11 20:23:24 +00002646 // Make up a symbol for the return value (not reference counted).
Ted Kremenek6c07bdb2009-06-26 00:05:51 +00002647 // FIXME: Most of this logic is not specific to the retain/release
2648 // checker.
Mike Stump1eb44332009-09-09 15:08:12 +00002649
Ted Kremenekfd301942008-10-17 22:23:12 +00002650 // FIXME: We eventually should handle structs and other compound types
2651 // that are returned by value.
Mike Stump1eb44332009-09-09 15:08:12 +00002652
Ted Kremenekfd301942008-10-17 22:23:12 +00002653 QualType T = Ex->getType();
Mike Stump1eb44332009-09-09 15:08:12 +00002654
Zhongxing Xu910e4082009-12-19 03:17:55 +00002655 // For CallExpr, use the result type to know if it returns a reference.
2656 if (const CallExpr *CE = dyn_cast<CallExpr>(Ex)) {
2657 const Expr *Callee = CE->getCallee();
Ted Kremenek13976632010-02-08 16:18:51 +00002658 if (const FunctionDecl *FD = state->getSVal(Callee).getAsFunctionDecl())
Zhongxing Xu910e4082009-12-19 03:17:55 +00002659 T = FD->getResultType();
2660 }
Ted Kremenek38ac4f52009-12-22 22:13:46 +00002661 else if (const ObjCMessageExpr *ME = dyn_cast<ObjCMessageExpr>(Ex)) {
2662 if (const ObjCMethodDecl *MD = ME->getMethodDecl())
2663 T = MD->getResultType();
2664 }
Zhongxing Xu910e4082009-12-19 03:17:55 +00002665
Ted Kremenek062e2f92008-11-13 06:10:40 +00002666 if (Loc::IsLocType(T) || (T->isIntegerType() && T->isScalarType())) {
Ted Kremenekf9561e52008-04-11 20:23:24 +00002667 unsigned Count = Builder.getCurrentBlockCount();
Ted Kremenekc8413fd2010-12-02 07:49:45 +00002668 SValBuilder &svalBuilder = Eng.getSValBuilder();
2669 SVal X = svalBuilder.getConjuredSymbolVal(NULL, Ex, T, Count);
Ted Kremenek8e029342009-08-27 22:17:37 +00002670 state = state->BindExpr(Ex, X, false);
Mike Stump1eb44332009-09-09 15:08:12 +00002671 }
2672
Ted Kremenek940b1d82008-04-10 23:44:06 +00002673 break;
Ted Kremenekfd301942008-10-17 22:23:12 +00002674 }
Mike Stump1eb44332009-09-09 15:08:12 +00002675
Ted Kremenek00a3a5f2008-03-12 01:21:45 +00002676 case RetEffect::Alias: {
Ted Kremenek553cf182008-06-25 21:21:56 +00002677 unsigned idx = RE.getIndex();
Ted Kremenek55499762008-06-17 02:43:46 +00002678 assert (arg_end >= arg_beg);
Ted Kremenekd3dbcf42008-05-05 22:11:16 +00002679 assert (idx < (unsigned) (arg_end - arg_beg));
Ted Kremenek13976632010-02-08 16:18:51 +00002680 SVal V = state->getSValAsScalarOrLoc(*(arg_beg+idx));
Ted Kremenek8e029342009-08-27 22:17:37 +00002681 state = state->BindExpr(Ex, V, false);
Ted Kremenek00a3a5f2008-03-12 01:21:45 +00002682 break;
2683 }
Mike Stump1eb44332009-09-09 15:08:12 +00002684
Ted Kremenek14993892008-05-06 02:41:27 +00002685 case RetEffect::ReceiverAlias: {
Ted Kremenek8f326752010-05-21 21:56:53 +00002686 assert(Receiver);
Ted Kremenekd775c662010-05-21 21:57:00 +00002687 SVal V = Receiver.getSValAsScalarOrLoc(state);
Ted Kremenek8e029342009-08-27 22:17:37 +00002688 state = state->BindExpr(Ex, V, false);
Ted Kremenek14993892008-05-06 02:41:27 +00002689 break;
2690 }
Mike Stump1eb44332009-09-09 15:08:12 +00002691
Ted Kremeneka7344702008-06-23 18:02:52 +00002692 case RetEffect::OwnedAllocatedSymbol:
Ted Kremenek00a3a5f2008-03-12 01:21:45 +00002693 case RetEffect::OwnedSymbol: {
2694 unsigned Count = Builder.getCurrentBlockCount();
Ted Kremenekc8413fd2010-12-02 07:49:45 +00002695 SValBuilder &svalBuilder = Eng.getSValBuilder();
2696 SymbolRef Sym = svalBuilder.getConjuredSymbol(Ex, Count);
2697 QualType RetT = GetReturnType(Ex, svalBuilder.getContext());
Ted Kremenekb65be702009-06-18 01:23:53 +00002698 state = state->set<RefBindings>(Sym, RefVal::makeOwned(RE.getObjKind(),
Ted Kremenek044b6f02009-04-09 16:13:17 +00002699 RetT));
Ted Kremenekc8413fd2010-12-02 07:49:45 +00002700 state = state->BindExpr(Ex, svalBuilder.makeLoc(Sym), false);
Ted Kremenek25d01ba2009-03-09 22:46:49 +00002701
2702 // FIXME: Add a flag to the checker where allocations are assumed to
2703 // *not fail.
2704#if 0
Ted Kremenekb2bf7cd2009-01-28 22:27:59 +00002705 if (RE.getKind() == RetEffect::OwnedAllocatedSymbol) {
2706 bool isFeasible;
Ted Kremenek28f47b92010-12-01 22:16:56 +00002707 state = state.assume(loc::SymbolVal(Sym), true, isFeasible);
Mike Stump1eb44332009-09-09 15:08:12 +00002708 assert(isFeasible && "Cannot assume fresh symbol is non-null.");
Ted Kremenekb2bf7cd2009-01-28 22:27:59 +00002709 }
Ted Kremenek25d01ba2009-03-09 22:46:49 +00002710#endif
Mike Stump1eb44332009-09-09 15:08:12 +00002711
Ted Kremenek00a3a5f2008-03-12 01:21:45 +00002712 break;
2713 }
Mike Stump1eb44332009-09-09 15:08:12 +00002714
Ted Kremeneke798e7c2009-04-27 19:14:45 +00002715 case RetEffect::GCNotOwnedSymbol:
Ted Kremenek00a3a5f2008-03-12 01:21:45 +00002716 case RetEffect::NotOwnedSymbol: {
2717 unsigned Count = Builder.getCurrentBlockCount();
Ted Kremenekc8413fd2010-12-02 07:49:45 +00002718 SValBuilder &svalBuilder = Eng.getSValBuilder();
2719 SymbolRef Sym = svalBuilder.getConjuredSymbol(Ex, Count);
2720 QualType RetT = GetReturnType(Ex, svalBuilder.getContext());
Ted Kremenekb65be702009-06-18 01:23:53 +00002721 state = state->set<RefBindings>(Sym, RefVal::makeNotOwned(RE.getObjKind(),
Ted Kremenek044b6f02009-04-09 16:13:17 +00002722 RetT));
Ted Kremenekc8413fd2010-12-02 07:49:45 +00002723 state = state->BindExpr(Ex, svalBuilder.makeLoc(Sym), false);
Ted Kremenek00a3a5f2008-03-12 01:21:45 +00002724 break;
2725 }
2726 }
Mike Stump1eb44332009-09-09 15:08:12 +00002727
Ted Kremenekf5b34b12009-02-18 02:00:25 +00002728 // Generate a sink node if we are at the end of a path.
Zhongxing Xu031ccc02009-08-06 12:48:26 +00002729 ExplodedNode *NewNode =
Ted Kremenek7faca822009-05-04 04:57:00 +00002730 Summ.isEndPath() ? Builder.MakeSinkNode(Dst, Ex, Pred, state)
2731 : Builder.MakeNode(Dst, Ex, Pred, state);
Mike Stump1eb44332009-09-09 15:08:12 +00002732
Ted Kremenekf5b34b12009-02-18 02:00:25 +00002733 // Annotate the edge with summary we used.
Ted Kremenek7faca822009-05-04 04:57:00 +00002734 if (NewNode) SummaryLog[NewNode] = &Summ;
Ted Kremenekd3dbcf42008-05-05 22:11:16 +00002735}
2736
2737
Ted Kremenek9c149532010-12-01 21:57:22 +00002738void CFRefCount::evalCall(ExplodedNodeSet& Dst,
Argyrios Kyrtzidisd2592a32010-12-22 18:53:44 +00002739 ExprEngine& Eng,
2740 StmtNodeBuilder& Builder,
Zhongxing Xu03509ae2010-07-20 06:22:24 +00002741 const CallExpr* CE, SVal L,
Zhongxing Xuc5619d92009-08-06 01:32:16 +00002742 ExplodedNode* Pred) {
Ted Kremenek772250c2009-11-25 01:35:18 +00002743
2744 RetainSummary *Summ = 0;
Ted Kremenekdcee3ce2010-07-01 20:16:50 +00002745
Ted Kremenek772250c2009-11-25 01:35:18 +00002746 // FIXME: Better support for blocks. For now we stop tracking anything
2747 // that is passed to blocks.
2748 // FIXME: Need to handle variables that are "captured" by the block.
Ted Kremenek0a8112a2009-11-25 23:53:07 +00002749 if (dyn_cast_or_null<BlockDataRegion>(L.getAsRegion())) {
Ted Kremenek772250c2009-11-25 01:35:18 +00002750 Summ = Summaries.getPersistentStopSummary();
2751 }
2752 else {
2753 const FunctionDecl* FD = L.getAsFunctionDecl();
2754 Summ = !FD ? Summaries.getDefaultSummary() :
Zhongxing Xubc5495b2010-07-20 02:56:49 +00002755 Summaries.getSummary(FD);
Ted Kremenek772250c2009-11-25 01:35:18 +00002756 }
Mike Stump1eb44332009-09-09 15:08:12 +00002757
Ted Kremenek7faca822009-05-04 04:57:00 +00002758 assert(Summ);
Ted Kremenek9c149532010-12-01 21:57:22 +00002759 evalSummary(Dst, Eng, Builder, CE, 0, *Summ, L.getAsRegion(),
Zhongxing Xua46e4d92009-12-02 05:49:12 +00002760 CE->arg_begin(), CE->arg_end(), Pred, Builder.GetState(Pred));
Ted Kremenek2fff37e2008-03-06 00:08:09 +00002761}
Ted Kremenek6b3a0f72008-03-11 06:39:11 +00002762
Ted Kremenek9c149532010-12-01 21:57:22 +00002763void CFRefCount::evalObjCMessageExpr(ExplodedNodeSet& Dst,
Argyrios Kyrtzidisd2592a32010-12-22 18:53:44 +00002764 ExprEngine& Eng,
2765 StmtNodeBuilder& Builder,
Zhongxing Xu03509ae2010-07-20 06:22:24 +00002766 const ObjCMessageExpr* ME,
Zhongxing Xua46e4d92009-12-02 05:49:12 +00002767 ExplodedNode* Pred,
2768 const GRState *state) {
Ted Kremenekb7ddd9b2009-11-13 01:54:21 +00002769 RetainSummary *Summ =
Douglas Gregor04badcf2010-04-21 00:45:42 +00002770 ME->isInstanceMessage()
Zhongxing Xua46e4d92009-12-02 05:49:12 +00002771 ? Summaries.getInstanceMethodSummary(ME, state,Pred->getLocationContext())
Ted Kremenekb7ddd9b2009-11-13 01:54:21 +00002772 : Summaries.getClassMethodSummary(ME);
Mike Stump1eb44332009-09-09 15:08:12 +00002773
Ted Kremenekb7ddd9b2009-11-13 01:54:21 +00002774 assert(Summ && "RetainSummary is null");
Ted Kremenek9c149532010-12-01 21:57:22 +00002775 evalSummary(Dst, Eng, Builder, ME,
Ted Kremenekd775c662010-05-21 21:57:00 +00002776 InstanceReceiver(ME, Pred->getLocationContext()), *Summ, NULL,
Zhongxing Xua46e4d92009-12-02 05:49:12 +00002777 ME->arg_begin(), ME->arg_end(), Pred, state);
Ted Kremenek85348202008-04-15 23:44:31 +00002778}
Ted Kremenek5216ad72009-02-14 03:16:10 +00002779
2780namespace {
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +00002781class StopTrackingCallback : public SymbolVisitor {
Ted Kremenek3a772032009-06-18 00:49:02 +00002782 const GRState *state;
Ted Kremenek5216ad72009-02-14 03:16:10 +00002783public:
Ted Kremenek3a772032009-06-18 00:49:02 +00002784 StopTrackingCallback(const GRState *st) : state(st) {}
2785 const GRState *getState() const { return state; }
Ted Kremenek5216ad72009-02-14 03:16:10 +00002786
2787 bool VisitSymbol(SymbolRef sym) {
Ted Kremenek3a772032009-06-18 00:49:02 +00002788 state = state->remove<RefBindings>(sym);
Ted Kremenek5216ad72009-02-14 03:16:10 +00002789 return true;
2790 }
Ted Kremenek5216ad72009-02-14 03:16:10 +00002791};
2792} // end anonymous namespace
Ted Kremenek5216ad72009-02-14 03:16:10 +00002793
Mike Stump1eb44332009-09-09 15:08:12 +00002794
Argyrios Kyrtzidisd2592a32010-12-22 18:53:44 +00002795void CFRefCount::evalBind(StmtNodeBuilderRef& B, SVal location, SVal val) {
Mike Stump1eb44332009-09-09 15:08:12 +00002796 // Are we storing to something that causes the value to "escape"?
Ted Kremenek13922612008-04-16 20:40:59 +00002797 bool escapes = false;
Mike Stump1eb44332009-09-09 15:08:12 +00002798
Ted Kremeneka496d162008-10-18 03:49:51 +00002799 // A value escapes in three possible cases (this may change):
2800 //
2801 // (1) we are binding to something that is not a memory region.
2802 // (2) we are binding to a memregion that does not have stack storage
2803 // (3) we are binding to a memregion with stack storage that the store
Mike Stump1eb44332009-09-09 15:08:12 +00002804 // does not understand.
Ted Kremenek3a772032009-06-18 00:49:02 +00002805 const GRState *state = B.getState();
Ted Kremeneka496d162008-10-18 03:49:51 +00002806
Ted Kremenek41573eb2009-02-14 01:43:44 +00002807 if (!isa<loc::MemRegionVal>(location))
Ted Kremenek13922612008-04-16 20:40:59 +00002808 escapes = true;
Ted Kremenek9e240492008-10-04 05:50:14 +00002809 else {
Ted Kremenek41573eb2009-02-14 01:43:44 +00002810 const MemRegion* R = cast<loc::MemRegionVal>(location).getRegion();
Ted Kremenekea20cd72009-06-23 18:05:21 +00002811 escapes = !R->hasStackStorage();
Mike Stump1eb44332009-09-09 15:08:12 +00002812
Ted Kremeneka496d162008-10-18 03:49:51 +00002813 if (!escapes) {
2814 // To test (3), generate a new state with the binding removed. If it is
2815 // the same state, then it escapes (since the store cannot represent
2816 // the binding).
Ted Kremenekb65be702009-06-18 01:23:53 +00002817 escapes = (state == (state->bindLoc(cast<Loc>(location), UnknownVal())));
Ted Kremeneka496d162008-10-18 03:49:51 +00002818 }
Ted Kremenek9e240492008-10-04 05:50:14 +00002819 }
Ted Kremenek41573eb2009-02-14 01:43:44 +00002820
Ted Kremenek5216ad72009-02-14 03:16:10 +00002821 // If our store can represent the binding and we aren't storing to something
2822 // that doesn't have local storage then just return and have the simulation
2823 // state continue as is.
2824 if (!escapes)
2825 return;
Ted Kremeneka496d162008-10-18 03:49:51 +00002826
Ted Kremenek5216ad72009-02-14 03:16:10 +00002827 // Otherwise, find all symbols referenced by 'val' that we are tracking
2828 // and stop tracking them.
Ted Kremenek3a772032009-06-18 00:49:02 +00002829 B.MakeNode(state->scanReachableSymbols<StopTrackingCallback>(val).getState());
Ted Kremenekdb863712008-04-16 22:32:20 +00002830}
2831
Ted Kremenek4fd88972008-04-17 18:12:53 +00002832 // Return statements.
2833
Ted Kremenek9c149532010-12-01 21:57:22 +00002834void CFRefCount::evalReturn(ExplodedNodeSet& Dst,
Argyrios Kyrtzidisd2592a32010-12-22 18:53:44 +00002835 ExprEngine& Eng,
2836 StmtNodeBuilder& Builder,
Zhongxing Xu03509ae2010-07-20 06:22:24 +00002837 const ReturnStmt* S,
Zhongxing Xuc5619d92009-08-06 01:32:16 +00002838 ExplodedNode* Pred) {
Mike Stump1eb44332009-09-09 15:08:12 +00002839
Zhongxing Xu03509ae2010-07-20 06:22:24 +00002840 const Expr* RetE = S->getRetValue();
Ted Kremenek94c96982009-03-03 22:06:47 +00002841 if (!RetE)
Ted Kremenek4fd88972008-04-17 18:12:53 +00002842 return;
Mike Stump1eb44332009-09-09 15:08:12 +00002843
Ted Kremenekb65be702009-06-18 01:23:53 +00002844 const GRState *state = Builder.GetState(Pred);
Ted Kremenek13976632010-02-08 16:18:51 +00002845 SymbolRef Sym = state->getSValAsScalarOrLoc(RetE).getAsLocSymbol();
Mike Stump1eb44332009-09-09 15:08:12 +00002846
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +00002847 if (!Sym)
Ted Kremenek94c96982009-03-03 22:06:47 +00002848 return;
Mike Stump1eb44332009-09-09 15:08:12 +00002849
Ted Kremenek4fd88972008-04-17 18:12:53 +00002850 // Get the reference count binding (if any).
Ted Kremenekb65be702009-06-18 01:23:53 +00002851 const RefVal* T = state->get<RefBindings>(Sym);
Mike Stump1eb44332009-09-09 15:08:12 +00002852
Ted Kremenek4fd88972008-04-17 18:12:53 +00002853 if (!T)
2854 return;
Mike Stump1eb44332009-09-09 15:08:12 +00002855
2856 // Change the reference count.
2857 RefVal X = *T;
2858
2859 switch (X.getKind()) {
2860 case RefVal::Owned: {
Ted Kremenek4fd88972008-04-17 18:12:53 +00002861 unsigned cnt = X.getCount();
Ted Kremenek3eabf1c2008-05-22 17:31:13 +00002862 assert (cnt > 0);
Ted Kremenekeaedfea2009-05-10 05:11:21 +00002863 X.setCount(cnt - 1);
2864 X = X ^ RefVal::ReturnedOwned;
Ted Kremenek4fd88972008-04-17 18:12:53 +00002865 break;
2866 }
Mike Stump1eb44332009-09-09 15:08:12 +00002867
Ted Kremenek4fd88972008-04-17 18:12:53 +00002868 case RefVal::NotOwned: {
2869 unsigned cnt = X.getCount();
Ted Kremenekeaedfea2009-05-10 05:11:21 +00002870 if (cnt) {
2871 X.setCount(cnt - 1);
2872 X = X ^ RefVal::ReturnedOwned;
2873 }
2874 else {
2875 X = X ^ RefVal::ReturnedNotOwned;
2876 }
Ted Kremenek4fd88972008-04-17 18:12:53 +00002877 break;
2878 }
Mike Stump1eb44332009-09-09 15:08:12 +00002879
2880 default:
Ted Kremenek4fd88972008-04-17 18:12:53 +00002881 return;
2882 }
Mike Stump1eb44332009-09-09 15:08:12 +00002883
Ted Kremenek4fd88972008-04-17 18:12:53 +00002884 // Update the binding.
Ted Kremenekb65be702009-06-18 01:23:53 +00002885 state = state->set<RefBindings>(Sym, X);
Ted Kremenekc887d132009-04-29 18:50:19 +00002886 Pred = Builder.MakeNode(Dst, S, Pred, state);
Mike Stump1eb44332009-09-09 15:08:12 +00002887
Ted Kremenek9f246b62009-04-30 05:51:50 +00002888 // Did we cache out?
2889 if (!Pred)
2890 return;
Mike Stump1eb44332009-09-09 15:08:12 +00002891
Ted Kremenekeaedfea2009-05-10 05:11:21 +00002892 // Update the autorelease counts.
2893 static unsigned autoreleasetag = 0;
2894 GenericNodeBuilder Bd(Builder, S, &autoreleasetag);
2895 bool stop = false;
2896 llvm::tie(Pred, state) = HandleAutoreleaseCounts(state , Bd, Pred, Eng, Sym,
2897 X, stop);
Mike Stump1eb44332009-09-09 15:08:12 +00002898
Ted Kremenekeaedfea2009-05-10 05:11:21 +00002899 // Did we cache out?
2900 if (!Pred || stop)
2901 return;
Mike Stump1eb44332009-09-09 15:08:12 +00002902
Ted Kremenekeaedfea2009-05-10 05:11:21 +00002903 // Get the updated binding.
Ted Kremenekb65be702009-06-18 01:23:53 +00002904 T = state->get<RefBindings>(Sym);
Ted Kremenekeaedfea2009-05-10 05:11:21 +00002905 assert(T);
2906 X = *T;
Mike Stump1eb44332009-09-09 15:08:12 +00002907
Ted Kremenekc887d132009-04-29 18:50:19 +00002908 // Any leaks or other errors?
2909 if (X.isReturnedOwned() && X.getCount() == 0) {
Zhongxing Xub317f8f2009-09-10 05:44:00 +00002910 Decl const *CD = &Pred->getCodeDecl();
Mike Stump1eb44332009-09-09 15:08:12 +00002911 if (const ObjCMethodDecl* MD = dyn_cast<ObjCMethodDecl>(CD)) {
Ted Kremenek7faca822009-05-04 04:57:00 +00002912 const RetainSummary &Summ = *Summaries.getMethodSummary(MD);
Ted Kremeneke8720ce2009-05-10 06:25:57 +00002913 RetEffect RE = Summ.getRetEffect();
2914 bool hasError = false;
2915
Ted Kremenekfae664a2009-05-16 01:38:01 +00002916 if (RE.getKind() != RetEffect::NoRet) {
2917 if (isGCEnabled() && RE.getObjKind() == RetEffect::ObjC) {
2918 // Things are more complicated with garbage collection. If the
2919 // returned object is suppose to be an Objective-C object, we have
2920 // a leak (as the caller expects a GC'ed object) because no
2921 // method should return ownership unless it returns a CF object.
Ted Kremenekfae664a2009-05-16 01:38:01 +00002922 hasError = true;
Ted Kremenek3bc4ffd2009-10-14 23:58:34 +00002923 X = X ^ RefVal::ErrorGCLeakReturned;
Ted Kremenekfae664a2009-05-16 01:38:01 +00002924 }
2925 else if (!RE.isOwned()) {
2926 // Either we are using GC and the returned object is a CF type
2927 // or we aren't using GC. In either case, we expect that the
Mike Stump1eb44332009-09-09 15:08:12 +00002928 // enclosing method is expected to return ownership.
Ted Kremenekfae664a2009-05-16 01:38:01 +00002929 hasError = true;
2930 X = X ^ RefVal::ErrorLeakReturned;
2931 }
Ted Kremeneke8720ce2009-05-10 06:25:57 +00002932 }
Mike Stump1eb44332009-09-09 15:08:12 +00002933
2934 if (hasError) {
Ted Kremenekc887d132009-04-29 18:50:19 +00002935 // Generate an error node.
Ted Kremeneke8720ce2009-05-10 06:25:57 +00002936 static int ReturnOwnLeakTag = 0;
Ted Kremenekb65be702009-06-18 01:23:53 +00002937 state = state->set<RefBindings>(Sym, X);
Zhongxing Xuc5619d92009-08-06 01:32:16 +00002938 ExplodedNode *N =
Zhongxing Xu25e695b2009-08-15 03:17:38 +00002939 Builder.generateNode(PostStmt(S, Pred->getLocationContext(),
2940 &ReturnOwnLeakTag), state, Pred);
Ted Kremeneke8720ce2009-05-10 06:25:57 +00002941 if (N) {
2942 CFRefReport *report =
Ted Kremenek9f246b62009-04-30 05:51:50 +00002943 new CFRefLeakReport(*static_cast<CFRefBug*>(leakAtReturn), *this,
2944 N, Sym, Eng);
2945 BR->EmitReport(report);
2946 }
Ted Kremenekc887d132009-04-29 18:50:19 +00002947 }
Mike Stump1eb44332009-09-09 15:08:12 +00002948 }
Ted Kremeneke8720ce2009-05-10 06:25:57 +00002949 }
2950 else if (X.isReturnedNotOwned()) {
Zhongxing Xub317f8f2009-09-10 05:44:00 +00002951 Decl const *CD = &Pred->getCodeDecl();
Ted Kremeneke8720ce2009-05-10 06:25:57 +00002952 if (const ObjCMethodDecl* MD = dyn_cast<ObjCMethodDecl>(CD)) {
2953 const RetainSummary &Summ = *Summaries.getMethodSummary(MD);
2954 if (Summ.getRetEffect().isOwned()) {
2955 // Trying to return a not owned object to a caller expecting an
2956 // owned object.
Mike Stump1eb44332009-09-09 15:08:12 +00002957
Ted Kremeneke8720ce2009-05-10 06:25:57 +00002958 static int ReturnNotOwnedForOwnedTag = 0;
Ted Kremenekb65be702009-06-18 01:23:53 +00002959 state = state->set<RefBindings>(Sym, X ^ RefVal::ErrorReturnedNotOwned);
Zhongxing Xuc5619d92009-08-06 01:32:16 +00002960 if (ExplodedNode *N =
Zhongxing Xu25e695b2009-08-15 03:17:38 +00002961 Builder.generateNode(PostStmt(S, Pred->getLocationContext(),
2962 &ReturnNotOwnedForOwnedTag),
2963 state, Pred)) {
Ted Kremeneke8720ce2009-05-10 06:25:57 +00002964 CFRefReport *report =
2965 new CFRefReport(*static_cast<CFRefBug*>(returnNotOwnedForOwned),
2966 *this, N, Sym);
2967 BR->EmitReport(report);
2968 }
2969 }
Ted Kremenekc887d132009-04-29 18:50:19 +00002970 }
2971 }
Ted Kremenek4fd88972008-04-17 18:12:53 +00002972}
2973
Ted Kremenekcb612922008-04-18 19:23:43 +00002974// Assumptions.
2975
Ted Kremenek9c149532010-12-01 21:57:22 +00002976const GRState* CFRefCount::evalAssume(const GRState *state,
Ted Kremeneka591bc02009-06-18 22:57:13 +00002977 SVal Cond, bool Assumption) {
Ted Kremenekcb612922008-04-18 19:23:43 +00002978
Ted Kremenek9c149532010-12-01 21:57:22 +00002979 // FIXME: We may add to the interface of evalAssume the list of symbols
Ted Kremenekcb612922008-04-18 19:23:43 +00002980 // whose assumptions have changed. For now we just iterate through the
2981 // bindings and check if any of the tracked symbols are NULL. This isn't
Mike Stump1eb44332009-09-09 15:08:12 +00002982 // too bad since the number of symbols we will track in practice are
Ted Kremenek9c149532010-12-01 21:57:22 +00002983 // probably small and evalAssume is only called at branches and a few
Ted Kremenekcb612922008-04-18 19:23:43 +00002984 // other places.
Ted Kremenekb65be702009-06-18 01:23:53 +00002985 RefBindings B = state->get<RefBindings>();
Mike Stump1eb44332009-09-09 15:08:12 +00002986
Ted Kremenekcb612922008-04-18 19:23:43 +00002987 if (B.isEmpty())
Ted Kremenekb65be702009-06-18 01:23:53 +00002988 return state;
Mike Stump1eb44332009-09-09 15:08:12 +00002989
2990 bool changed = false;
Ted Kremenekb65be702009-06-18 01:23:53 +00002991 RefBindings::Factory& RefBFactory = state->get_context<RefBindings>();
Ted Kremenekcb612922008-04-18 19:23:43 +00002992
Mike Stump1eb44332009-09-09 15:08:12 +00002993 for (RefBindings::iterator I=B.begin(), E=B.end(); I!=E; ++I) {
Ted Kremenekcb612922008-04-18 19:23:43 +00002994 // Check if the symbol is null (or equal to any constant).
2995 // If this is the case, stop tracking the symbol.
Ted Kremeneka591bc02009-06-18 22:57:13 +00002996 if (state->getSymVal(I.getKey())) {
Ted Kremenekcb612922008-04-18 19:23:43 +00002997 changed = true;
Ted Kremenek3baf6722010-11-24 00:54:37 +00002998 B = RefBFactory.remove(B, I.getKey());
Ted Kremenekcb612922008-04-18 19:23:43 +00002999 }
3000 }
Mike Stump1eb44332009-09-09 15:08:12 +00003001
Ted Kremenekb9d17f92008-08-17 03:20:02 +00003002 if (changed)
Ted Kremenekb65be702009-06-18 01:23:53 +00003003 state = state->set<RefBindings>(B);
Mike Stump1eb44332009-09-09 15:08:12 +00003004
Ted Kremenek72cd17f2008-08-14 21:16:54 +00003005 return state;
Ted Kremenekcb612922008-04-18 19:23:43 +00003006}
Ted Kremenek6b3a0f72008-03-11 06:39:11 +00003007
Ted Kremenekb65be702009-06-18 01:23:53 +00003008const GRState * CFRefCount::Update(const GRState * state, SymbolRef sym,
Ted Kremenek4d3957d2009-02-24 19:15:11 +00003009 RefVal V, ArgEffect E,
3010 RefVal::Kind& hasErr) {
Ted Kremenek1c512f52009-02-18 18:54:33 +00003011
3012 // In GC mode [... release] and [... retain] do nothing.
3013 switch (E) {
3014 default: break;
3015 case IncRefMsg: E = isGCEnabled() ? DoNothing : IncRef; break;
3016 case DecRefMsg: E = isGCEnabled() ? DoNothing : DecRef; break;
Ted Kremenek27019002009-02-18 21:57:45 +00003017 case MakeCollectable: E = isGCEnabled() ? DecRef : DoNothing; break;
Mike Stump1eb44332009-09-09 15:08:12 +00003018 case NewAutoreleasePool: E = isGCEnabled() ? DoNothing :
Ted Kremenekf9a8e2e2009-02-23 17:45:03 +00003019 NewAutoreleasePool; break;
Ted Kremenek1c512f52009-02-18 18:54:33 +00003020 }
Mike Stump1eb44332009-09-09 15:08:12 +00003021
Ted Kremenekf95e9fc2009-03-17 19:42:23 +00003022 // Handle all use-after-releases.
3023 if (!isGCEnabled() && V.getKind() == RefVal::Released) {
3024 V = V ^ RefVal::ErrorUseAfterRelease;
3025 hasErr = V.getKind();
Ted Kremenekb65be702009-06-18 01:23:53 +00003026 return state->set<RefBindings>(sym, V);
Mike Stump1eb44332009-09-09 15:08:12 +00003027 }
3028
Ted Kremenek1ac08d62008-03-11 17:48:22 +00003029 switch (E) {
3030 default:
3031 assert (false && "Unhandled CFRef transition.");
Mike Stump1eb44332009-09-09 15:08:12 +00003032
Ted Kremenekf95e9fc2009-03-17 19:42:23 +00003033 case Dealloc:
3034 // Any use of -dealloc in GC is *bad*.
3035 if (isGCEnabled()) {
3036 V = V ^ RefVal::ErrorDeallocGC;
3037 hasErr = V.getKind();
3038 break;
3039 }
Mike Stump1eb44332009-09-09 15:08:12 +00003040
Ted Kremenekf95e9fc2009-03-17 19:42:23 +00003041 switch (V.getKind()) {
3042 default:
3043 assert(false && "Invalid case.");
3044 case RefVal::Owned:
3045 // The object immediately transitions to the released state.
3046 V = V ^ RefVal::Released;
3047 V.clearCounts();
Ted Kremenekb65be702009-06-18 01:23:53 +00003048 return state->set<RefBindings>(sym, V);
Ted Kremenekf95e9fc2009-03-17 19:42:23 +00003049 case RefVal::NotOwned:
3050 V = V ^ RefVal::ErrorDeallocNotOwned;
3051 hasErr = V.getKind();
3052 break;
Mike Stump1eb44332009-09-09 15:08:12 +00003053 }
Ted Kremenekf95e9fc2009-03-17 19:42:23 +00003054 break;
Ted Kremenek3eabf1c2008-05-22 17:31:13 +00003055
Ted Kremenek35790732009-02-25 23:11:49 +00003056 case NewAutoreleasePool:
3057 assert(!isGCEnabled());
Ted Kremenekb65be702009-06-18 01:23:53 +00003058 return state->add<AutoreleaseStack>(sym);
Mike Stump1eb44332009-09-09 15:08:12 +00003059
Ted Kremenek3eabf1c2008-05-22 17:31:13 +00003060 case MayEscape:
3061 if (V.getKind() == RefVal::Owned) {
Ted Kremenek553cf182008-06-25 21:21:56 +00003062 V = V ^ RefVal::NotOwned;
Ted Kremenek3eabf1c2008-05-22 17:31:13 +00003063 break;
3064 }
Ted Kremenekf95e9fc2009-03-17 19:42:23 +00003065
Ted Kremenek3eabf1c2008-05-22 17:31:13 +00003066 // Fall-through.
Mike Stump1eb44332009-09-09 15:08:12 +00003067
Ted Kremenek070a8252008-07-09 18:11:16 +00003068 case DoNothingByRef:
Ted Kremenek1ac08d62008-03-11 17:48:22 +00003069 case DoNothing:
Ted Kremenek4d3957d2009-02-24 19:15:11 +00003070 return state;
Ted Kremeneke19f4492008-06-30 16:57:41 +00003071
Ted Kremenekabf43972009-01-28 21:44:40 +00003072 case Autorelease:
Ted Kremenekf95e9fc2009-03-17 19:42:23 +00003073 if (isGCEnabled())
3074 return state;
Mike Stump1eb44332009-09-09 15:08:12 +00003075
Ted Kremenek7037ab82009-03-20 17:34:15 +00003076 // Update the autorelease counts.
3077 state = SendAutorelease(state, ARCountFactory, sym);
Ted Kremenekf21332e2009-05-08 20:01:42 +00003078 V = V.autorelease();
Ted Kremenek6b62ec92009-05-09 01:50:57 +00003079 break;
Ted Kremenek369de562009-05-09 00:10:05 +00003080
Ted Kremenek14993892008-05-06 02:41:27 +00003081 case StopTracking:
Ted Kremenekb65be702009-06-18 01:23:53 +00003082 return state->remove<RefBindings>(sym);
Ted Kremenek9e476de2008-08-12 18:30:56 +00003083
Mike Stump1eb44332009-09-09 15:08:12 +00003084 case IncRef:
Ted Kremenek1ac08d62008-03-11 17:48:22 +00003085 switch (V.getKind()) {
3086 default:
3087 assert(false);
3088
3089 case RefVal::Owned:
Ted Kremenek1ac08d62008-03-11 17:48:22 +00003090 case RefVal::NotOwned:
Ted Kremenek553cf182008-06-25 21:21:56 +00003091 V = V + 1;
Mike Stump1eb44332009-09-09 15:08:12 +00003092 break;
Ted Kremenek1ac08d62008-03-11 17:48:22 +00003093 case RefVal::Released:
Ted Kremenekf95e9fc2009-03-17 19:42:23 +00003094 // Non-GC cases are handled above.
3095 assert(isGCEnabled());
3096 V = (V ^ RefVal::Owned) + 1;
Ted Kremenek1ac08d62008-03-11 17:48:22 +00003097 break;
Mike Stump1eb44332009-09-09 15:08:12 +00003098 }
Ted Kremenek940b1d82008-04-10 23:44:06 +00003099 break;
Mike Stump1eb44332009-09-09 15:08:12 +00003100
Ted Kremenek553cf182008-06-25 21:21:56 +00003101 case SelfOwn:
3102 V = V ^ RefVal::NotOwned;
Ted Kremenek1c512f52009-02-18 18:54:33 +00003103 // Fall-through.
Ted Kremenek1ac08d62008-03-11 17:48:22 +00003104 case DecRef:
3105 switch (V.getKind()) {
3106 default:
Ted Kremenekf95e9fc2009-03-17 19:42:23 +00003107 // case 'RefVal::Released' handled above.
Ted Kremenek1ac08d62008-03-11 17:48:22 +00003108 assert (false);
Ted Kremenek9e476de2008-08-12 18:30:56 +00003109
Ted Kremenek553cf182008-06-25 21:21:56 +00003110 case RefVal::Owned:
Ted Kremenekbb8c5aa2009-02-18 22:57:22 +00003111 assert(V.getCount() > 0);
3112 if (V.getCount() == 1) V = V ^ RefVal::Released;
3113 V = V - 1;
Ted Kremenek1ac08d62008-03-11 17:48:22 +00003114 break;
Mike Stump1eb44332009-09-09 15:08:12 +00003115
Ted Kremenek553cf182008-06-25 21:21:56 +00003116 case RefVal::NotOwned:
3117 if (V.getCount() > 0)
3118 V = V - 1;
Ted Kremenek61b9f872008-04-10 23:09:18 +00003119 else {
Ted Kremenek553cf182008-06-25 21:21:56 +00003120 V = V ^ RefVal::ErrorReleaseNotOwned;
Ted Kremenek9ed18e62008-04-16 04:28:53 +00003121 hasErr = V.getKind();
Mike Stump1eb44332009-09-09 15:08:12 +00003122 }
Ted Kremenek1ac08d62008-03-11 17:48:22 +00003123 break;
Mike Stump1eb44332009-09-09 15:08:12 +00003124
Ted Kremenek1ac08d62008-03-11 17:48:22 +00003125 case RefVal::Released:
Ted Kremenekf95e9fc2009-03-17 19:42:23 +00003126 // Non-GC cases are handled above.
3127 assert(isGCEnabled());
Ted Kremenek553cf182008-06-25 21:21:56 +00003128 V = V ^ RefVal::ErrorUseAfterRelease;
Ted Kremenek9ed18e62008-04-16 04:28:53 +00003129 hasErr = V.getKind();
Mike Stump1eb44332009-09-09 15:08:12 +00003130 break;
3131 }
Ted Kremenek940b1d82008-04-10 23:44:06 +00003132 break;
Ted Kremenek1ac08d62008-03-11 17:48:22 +00003133 }
Ted Kremenekb65be702009-06-18 01:23:53 +00003134 return state->set<RefBindings>(sym, V);
Ted Kremenek6b3a0f72008-03-11 06:39:11 +00003135}
3136
Ted Kremenekfa34b332008-04-09 01:10:13 +00003137//===----------------------------------------------------------------------===//
Ted Kremenekcf701772009-02-05 06:50:21 +00003138// Handle dead symbols and end-of-path.
3139//===----------------------------------------------------------------------===//
3140
Zhongxing Xuc5619d92009-08-06 01:32:16 +00003141std::pair<ExplodedNode*, const GRState *>
Ted Kremenekb65be702009-06-18 01:23:53 +00003142CFRefCount::HandleAutoreleaseCounts(const GRState * state, GenericNodeBuilder Bd,
Zhongxing Xuc5619d92009-08-06 01:32:16 +00003143 ExplodedNode* Pred,
Argyrios Kyrtzidisd2592a32010-12-22 18:53:44 +00003144 ExprEngine &Eng,
Ted Kremenek369de562009-05-09 00:10:05 +00003145 SymbolRef Sym, RefVal V, bool &stop) {
Mike Stump1eb44332009-09-09 15:08:12 +00003146
Ted Kremenek369de562009-05-09 00:10:05 +00003147 unsigned ACnt = V.getAutoreleaseCount();
3148 stop = false;
3149
3150 // No autorelease counts? Nothing to be done.
3151 if (!ACnt)
3152 return std::make_pair(Pred, state);
Mike Stump1eb44332009-09-09 15:08:12 +00003153
3154 assert(!isGCEnabled() && "Autorelease counts in GC mode?");
Ted Kremenek369de562009-05-09 00:10:05 +00003155 unsigned Cnt = V.getCount();
Mike Stump1eb44332009-09-09 15:08:12 +00003156
Ted Kremenek95d3b902009-05-11 15:26:06 +00003157 // FIXME: Handle sending 'autorelease' to already released object.
3158
3159 if (V.getKind() == RefVal::ReturnedOwned)
3160 ++Cnt;
Mike Stump1eb44332009-09-09 15:08:12 +00003161
Ted Kremenek369de562009-05-09 00:10:05 +00003162 if (ACnt <= Cnt) {
Ted Kremenek80c24182009-05-09 00:44:07 +00003163 if (ACnt == Cnt) {
3164 V.clearCounts();
Ted Kremenek95d3b902009-05-11 15:26:06 +00003165 if (V.getKind() == RefVal::ReturnedOwned)
3166 V = V ^ RefVal::ReturnedNotOwned;
3167 else
3168 V = V ^ RefVal::NotOwned;
Ted Kremenek80c24182009-05-09 00:44:07 +00003169 }
Ted Kremenek95d3b902009-05-11 15:26:06 +00003170 else {
Ted Kremenek80c24182009-05-09 00:44:07 +00003171 V.setCount(Cnt - ACnt);
3172 V.setAutoreleaseCount(0);
3173 }
Ted Kremenekb65be702009-06-18 01:23:53 +00003174 state = state->set<RefBindings>(Sym, V);
Zhongxing Xuc5619d92009-08-06 01:32:16 +00003175 ExplodedNode *N = Bd.MakeNode(state, Pred);
Ted Kremenek369de562009-05-09 00:10:05 +00003176 stop = (N == 0);
3177 return std::make_pair(N, state);
Mike Stump1eb44332009-09-09 15:08:12 +00003178 }
Ted Kremenek369de562009-05-09 00:10:05 +00003179
3180 // Woah! More autorelease counts then retain counts left.
3181 // Emit hard error.
3182 stop = true;
3183 V = V ^ RefVal::ErrorOverAutorelease;
Ted Kremenekb65be702009-06-18 01:23:53 +00003184 state = state->set<RefBindings>(Sym, V);
Ted Kremenek369de562009-05-09 00:10:05 +00003185
Zhongxing Xuc5619d92009-08-06 01:32:16 +00003186 if (ExplodedNode *N = Bd.MakeNode(state, Pred)) {
Ted Kremenek80c24182009-05-09 00:44:07 +00003187 N->markAsSink();
Mike Stump1eb44332009-09-09 15:08:12 +00003188
Ted Kremenekeaedfea2009-05-10 05:11:21 +00003189 std::string sbuf;
3190 llvm::raw_string_ostream os(sbuf);
Ted Kremenekdaec1452009-05-15 06:02:08 +00003191 os << "Object over-autoreleased: object was sent -autorelease";
Ted Kremenekeaedfea2009-05-10 05:11:21 +00003192 if (V.getAutoreleaseCount() > 1)
3193 os << V.getAutoreleaseCount() << " times";
3194 os << " but the object has ";
3195 if (V.getCount() == 0)
3196 os << "zero (locally visible)";
3197 else
3198 os << "+" << V.getCount();
3199 os << " retain counts";
Mike Stump1eb44332009-09-09 15:08:12 +00003200
Ted Kremenek369de562009-05-09 00:10:05 +00003201 CFRefReport *report =
3202 new CFRefReport(*static_cast<CFRefBug*>(overAutorelease),
Benjamin Kramerf0171732009-11-29 18:27:55 +00003203 *this, N, Sym, os.str());
Ted Kremenek369de562009-05-09 00:10:05 +00003204 BR->EmitReport(report);
3205 }
Mike Stump1eb44332009-09-09 15:08:12 +00003206
Zhongxing Xuc5619d92009-08-06 01:32:16 +00003207 return std::make_pair((ExplodedNode*)0, state);
Ted Kremenekf04dced2009-05-08 23:32:51 +00003208}
Ted Kremenek9d9d3a62009-05-08 23:09:42 +00003209
Ted Kremenekb65be702009-06-18 01:23:53 +00003210const GRState *
3211CFRefCount::HandleSymbolDeath(const GRState * state, SymbolRef sid, RefVal V,
Ted Kremenek9d9d3a62009-05-08 23:09:42 +00003212 llvm::SmallVectorImpl<SymbolRef> &Leaked) {
Mike Stump1eb44332009-09-09 15:08:12 +00003213
3214 bool hasLeak = V.isOwned() ||
Ted Kremenek9d9d3a62009-05-08 23:09:42 +00003215 ((V.isNotOwned() || V.isReturnedOwned()) && V.getCount() > 0);
Mike Stump1eb44332009-09-09 15:08:12 +00003216
Ted Kremenek9d9d3a62009-05-08 23:09:42 +00003217 if (!hasLeak)
Ted Kremenekb65be702009-06-18 01:23:53 +00003218 return state->remove<RefBindings>(sid);
Mike Stump1eb44332009-09-09 15:08:12 +00003219
Ted Kremenek9d9d3a62009-05-08 23:09:42 +00003220 Leaked.push_back(sid);
Ted Kremenekb65be702009-06-18 01:23:53 +00003221 return state->set<RefBindings>(sid, V ^ RefVal::ErrorLeak);
Ted Kremenek9d9d3a62009-05-08 23:09:42 +00003222}
3223
Zhongxing Xuc5619d92009-08-06 01:32:16 +00003224ExplodedNode*
Ted Kremenekb65be702009-06-18 01:23:53 +00003225CFRefCount::ProcessLeaks(const GRState * state,
Ted Kremenek9d9d3a62009-05-08 23:09:42 +00003226 llvm::SmallVectorImpl<SymbolRef> &Leaked,
3227 GenericNodeBuilder &Builder,
Argyrios Kyrtzidisd2592a32010-12-22 18:53:44 +00003228 ExprEngine& Eng,
Zhongxing Xuc5619d92009-08-06 01:32:16 +00003229 ExplodedNode *Pred) {
Mike Stump1eb44332009-09-09 15:08:12 +00003230
Ted Kremenek9d9d3a62009-05-08 23:09:42 +00003231 if (Leaked.empty())
3232 return Pred;
Mike Stump1eb44332009-09-09 15:08:12 +00003233
Ted Kremenekf04dced2009-05-08 23:32:51 +00003234 // Generate an intermediate node representing the leak point.
Zhongxing Xuc5619d92009-08-06 01:32:16 +00003235 ExplodedNode *N = Builder.MakeNode(state, Pred);
Mike Stump1eb44332009-09-09 15:08:12 +00003236
Ted Kremenek9d9d3a62009-05-08 23:09:42 +00003237 if (N) {
3238 for (llvm::SmallVectorImpl<SymbolRef>::iterator
3239 I = Leaked.begin(), E = Leaked.end(); I != E; ++I) {
Mike Stump1eb44332009-09-09 15:08:12 +00003240
3241 CFRefBug *BT = static_cast<CFRefBug*>(Pred ? leakWithinFunction
Ted Kremenek9d9d3a62009-05-08 23:09:42 +00003242 : leakAtReturn);
3243 assert(BT && "BugType not initialized.");
3244 CFRefLeakReport* report = new CFRefLeakReport(*BT, *this, N, *I, Eng);
3245 BR->EmitReport(report);
3246 }
3247 }
Mike Stump1eb44332009-09-09 15:08:12 +00003248
Ted Kremenek9d9d3a62009-05-08 23:09:42 +00003249 return N;
3250}
3251
Argyrios Kyrtzidisd2592a32010-12-22 18:53:44 +00003252void CFRefCount::evalEndPath(ExprEngine& Eng,
3253 EndPathNodeBuilder& Builder) {
Mike Stump1eb44332009-09-09 15:08:12 +00003254
Ted Kremenekb65be702009-06-18 01:23:53 +00003255 const GRState *state = Builder.getState();
Ted Kremenekf04dced2009-05-08 23:32:51 +00003256 GenericNodeBuilder Bd(Builder);
Mike Stump1eb44332009-09-09 15:08:12 +00003257 RefBindings B = state->get<RefBindings>();
Zhongxing Xuc5619d92009-08-06 01:32:16 +00003258 ExplodedNode *Pred = 0;
Ted Kremenekf04dced2009-05-08 23:32:51 +00003259
3260 for (RefBindings::iterator I = B.begin(), E = B.end(); I != E; ++I) {
Ted Kremenek369de562009-05-09 00:10:05 +00003261 bool stop = false;
3262 llvm::tie(Pred, state) = HandleAutoreleaseCounts(state, Bd, Pred, Eng,
3263 (*I).first,
Mike Stump1eb44332009-09-09 15:08:12 +00003264 (*I).second, stop);
Ted Kremenek369de562009-05-09 00:10:05 +00003265
3266 if (stop)
3267 return;
Ted Kremenekf04dced2009-05-08 23:32:51 +00003268 }
Mike Stump1eb44332009-09-09 15:08:12 +00003269
3270 B = state->get<RefBindings>();
3271 llvm::SmallVector<SymbolRef, 10> Leaked;
3272
Ted Kremenek9d9d3a62009-05-08 23:09:42 +00003273 for (RefBindings::iterator I = B.begin(), E = B.end(); I != E; ++I)
3274 state = HandleSymbolDeath(state, (*I).first, (*I).second, Leaked);
3275
Ted Kremenekf04dced2009-05-08 23:32:51 +00003276 ProcessLeaks(state, Leaked, Bd, Eng, Pred);
Ted Kremenekcf701772009-02-05 06:50:21 +00003277}
3278
Ted Kremenek9c149532010-12-01 21:57:22 +00003279void CFRefCount::evalDeadSymbols(ExplodedNodeSet& Dst,
Argyrios Kyrtzidisd2592a32010-12-22 18:53:44 +00003280 ExprEngine& Eng,
3281 StmtNodeBuilder& Builder,
Zhongxing Xuc5619d92009-08-06 01:32:16 +00003282 ExplodedNode* Pred,
Ted Kremenekb65be702009-06-18 01:23:53 +00003283 const GRState* state,
Ted Kremenekcf701772009-02-05 06:50:21 +00003284 SymbolReaper& SymReaper) {
Zhongxing Xu03509ae2010-07-20 06:22:24 +00003285 const Stmt *S = Builder.getStmt();
Ted Kremenekb65be702009-06-18 01:23:53 +00003286 RefBindings B = state->get<RefBindings>();
Mike Stump1eb44332009-09-09 15:08:12 +00003287
Ted Kremenekf04dced2009-05-08 23:32:51 +00003288 // Update counts from autorelease pools
3289 for (SymbolReaper::dead_iterator I = SymReaper.dead_begin(),
3290 E = SymReaper.dead_end(); I != E; ++I) {
3291 SymbolRef Sym = *I;
3292 if (const RefVal* T = B.lookup(Sym)){
3293 // Use the symbol as the tag.
3294 // FIXME: This might not be as unique as we would like.
3295 GenericNodeBuilder Bd(Builder, S, Sym);
Ted Kremenek369de562009-05-09 00:10:05 +00003296 bool stop = false;
3297 llvm::tie(Pred, state) = HandleAutoreleaseCounts(state, Bd, Pred, Eng,
3298 Sym, *T, stop);
3299 if (stop)
3300 return;
Ted Kremenekf04dced2009-05-08 23:32:51 +00003301 }
3302 }
Mike Stump1eb44332009-09-09 15:08:12 +00003303
Ted Kremenekb65be702009-06-18 01:23:53 +00003304 B = state->get<RefBindings>();
Ted Kremenek9d9d3a62009-05-08 23:09:42 +00003305 llvm::SmallVector<SymbolRef, 10> Leaked;
Mike Stump1eb44332009-09-09 15:08:12 +00003306
Ted Kremenekcf701772009-02-05 06:50:21 +00003307 for (SymbolReaper::dead_iterator I = SymReaper.dead_begin(),
Mike Stump1eb44332009-09-09 15:08:12 +00003308 E = SymReaper.dead_end(); I != E; ++I) {
Ted Kremenek9d9d3a62009-05-08 23:09:42 +00003309 if (const RefVal* T = B.lookup(*I))
3310 state = HandleSymbolDeath(state, *I, *T, Leaked);
Mike Stump1eb44332009-09-09 15:08:12 +00003311 }
3312
Ted Kremenek9d9d3a62009-05-08 23:09:42 +00003313 static unsigned LeakPPTag = 0;
Ted Kremenekf04dced2009-05-08 23:32:51 +00003314 {
3315 GenericNodeBuilder Bd(Builder, S, &LeakPPTag);
3316 Pred = ProcessLeaks(state, Leaked, Bd, Eng, Pred);
3317 }
Mike Stump1eb44332009-09-09 15:08:12 +00003318
Ted Kremenek9d9d3a62009-05-08 23:09:42 +00003319 // Did we cache out?
3320 if (!Pred)
3321 return;
Mike Stump1eb44332009-09-09 15:08:12 +00003322
Ted Kremenek33b6f632009-02-19 23:47:02 +00003323 // Now generate a new node that nukes the old bindings.
Ted Kremenekb65be702009-06-18 01:23:53 +00003324 RefBindings::Factory& F = state->get_context<RefBindings>();
Mike Stump1eb44332009-09-09 15:08:12 +00003325
Ted Kremenek33b6f632009-02-19 23:47:02 +00003326 for (SymbolReaper::dead_iterator I = SymReaper.dead_begin(),
Ted Kremenek3baf6722010-11-24 00:54:37 +00003327 E = SymReaper.dead_end(); I!=E; ++I) B = F.remove(B, *I);
Mike Stump1eb44332009-09-09 15:08:12 +00003328
Ted Kremenekb65be702009-06-18 01:23:53 +00003329 state = state->set<RefBindings>(B);
Ted Kremenek33b6f632009-02-19 23:47:02 +00003330 Builder.MakeNode(Dst, S, Pred, state);
Ted Kremenekcf701772009-02-05 06:50:21 +00003331}
3332
Zhongxing Xuc5619d92009-08-06 01:32:16 +00003333void CFRefCount::ProcessNonLeakError(ExplodedNodeSet& Dst,
Argyrios Kyrtzidisd2592a32010-12-22 18:53:44 +00003334 StmtNodeBuilder& Builder,
Zhongxing Xu03509ae2010-07-20 06:22:24 +00003335 const Expr* NodeExpr,
3336 SourceRange ErrorRange,
Zhongxing Xuc5619d92009-08-06 01:32:16 +00003337 ExplodedNode* Pred,
Ted Kremenekcf701772009-02-05 06:50:21 +00003338 const GRState* St,
3339 RefVal::Kind hasErr, SymbolRef Sym) {
3340 Builder.BuildSinks = true;
Zhongxing Xu031ccc02009-08-06 12:48:26 +00003341 ExplodedNode *N = Builder.MakeNode(Dst, NodeExpr, Pred, St);
Mike Stump1eb44332009-09-09 15:08:12 +00003342
Ted Kremenek6b62ec92009-05-09 01:50:57 +00003343 if (!N)
3344 return;
Mike Stump1eb44332009-09-09 15:08:12 +00003345
Ted Kremenekcf701772009-02-05 06:50:21 +00003346 CFRefBug *BT = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00003347
Ted Kremenekf95e9fc2009-03-17 19:42:23 +00003348 switch (hasErr) {
3349 default:
3350 assert(false && "Unhandled error.");
3351 return;
3352 case RefVal::ErrorUseAfterRelease:
3353 BT = static_cast<CFRefBug*>(useAfterRelease);
Mike Stump1eb44332009-09-09 15:08:12 +00003354 break;
Ted Kremenekf95e9fc2009-03-17 19:42:23 +00003355 case RefVal::ErrorReleaseNotOwned:
3356 BT = static_cast<CFRefBug*>(releaseNotOwned);
3357 break;
3358 case RefVal::ErrorDeallocGC:
3359 BT = static_cast<CFRefBug*>(deallocGC);
3360 break;
3361 case RefVal::ErrorDeallocNotOwned:
3362 BT = static_cast<CFRefBug*>(deallocNotOwned);
3363 break;
Ted Kremenekcf701772009-02-05 06:50:21 +00003364 }
Mike Stump1eb44332009-09-09 15:08:12 +00003365
Ted Kremenekfe9e5432009-02-18 03:48:14 +00003366 CFRefReport *report = new CFRefReport(*BT, *this, N, Sym);
Ted Kremenek8f326752010-05-21 21:56:53 +00003367 report->addRange(ErrorRange);
Ted Kremenekcf701772009-02-05 06:50:21 +00003368 BR->EmitReport(report);
3369}
3370
3371//===----------------------------------------------------------------------===//
Ted Kremenekd593eb92009-11-25 22:17:44 +00003372// Pieces of the retain/release checker implemented using a CheckerVisitor.
3373// More pieces of the retain/release checker will be migrated to this interface
3374// (ideally, all of it some day).
3375//===----------------------------------------------------------------------===//
3376
3377namespace {
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +00003378class RetainReleaseChecker
Ted Kremenekd593eb92009-11-25 22:17:44 +00003379 : public CheckerVisitor<RetainReleaseChecker> {
3380 CFRefCount *TF;
3381public:
3382 RetainReleaseChecker(CFRefCount *tf) : TF(tf) {}
Ted Kremenek38cc6bc2009-11-26 02:38:19 +00003383 static void* getTag() { static int x = 0; return &x; }
Ted Kremenekdcee3ce2010-07-01 20:16:50 +00003384
Ted Kremenek38cc6bc2009-11-26 02:38:19 +00003385 void PostVisitBlockExpr(CheckerContext &C, const BlockExpr *BE);
Ted Kremenekd593eb92009-11-25 22:17:44 +00003386};
3387} // end anonymous namespace
3388
Ted Kremenek38cc6bc2009-11-26 02:38:19 +00003389
3390void RetainReleaseChecker::PostVisitBlockExpr(CheckerContext &C,
3391 const BlockExpr *BE) {
Ted Kremenekdcee3ce2010-07-01 20:16:50 +00003392
Ted Kremenek38cc6bc2009-11-26 02:38:19 +00003393 // Scan the BlockDecRefExprs for any object the retain/release checker
Ted Kremenekdcee3ce2010-07-01 20:16:50 +00003394 // may be tracking.
Ted Kremenek38cc6bc2009-11-26 02:38:19 +00003395 if (!BE->hasBlockDeclRefExprs())
3396 return;
Ted Kremenekdcee3ce2010-07-01 20:16:50 +00003397
Ted Kremenek38cc6bc2009-11-26 02:38:19 +00003398 const GRState *state = C.getState();
3399 const BlockDataRegion *R =
Ted Kremenek13976632010-02-08 16:18:51 +00003400 cast<BlockDataRegion>(state->getSVal(BE).getAsRegion());
Ted Kremenekdcee3ce2010-07-01 20:16:50 +00003401
Ted Kremenek38cc6bc2009-11-26 02:38:19 +00003402 BlockDataRegion::referenced_vars_iterator I = R->referenced_vars_begin(),
3403 E = R->referenced_vars_end();
Ted Kremenekdcee3ce2010-07-01 20:16:50 +00003404
Ted Kremenek38cc6bc2009-11-26 02:38:19 +00003405 if (I == E)
3406 return;
Ted Kremenekdcee3ce2010-07-01 20:16:50 +00003407
Ted Kremenek67d12872009-12-07 22:05:27 +00003408 // FIXME: For now we invalidate the tracking of all symbols passed to blocks
3409 // via captured variables, even though captured variables result in a copy
3410 // and in implicit increment/decrement of a retain count.
3411 llvm::SmallVector<const MemRegion*, 10> Regions;
3412 const LocationContext *LC = C.getPredecessor()->getLocationContext();
Ted Kremenekc8413fd2010-12-02 07:49:45 +00003413 MemRegionManager &MemMgr = C.getSValBuilder().getRegionManager();
Ted Kremenekdcee3ce2010-07-01 20:16:50 +00003414
Ted Kremenek67d12872009-12-07 22:05:27 +00003415 for ( ; I != E; ++I) {
3416 const VarRegion *VR = *I;
3417 if (VR->getSuperRegion() == R) {
3418 VR = MemMgr.getVarRegion(VR->getDecl(), LC);
3419 }
3420 Regions.push_back(VR);
3421 }
Ted Kremenekdcee3ce2010-07-01 20:16:50 +00003422
Ted Kremenek67d12872009-12-07 22:05:27 +00003423 state =
3424 state->scanReachableSymbols<StopTrackingCallback>(Regions.data(),
3425 Regions.data() + Regions.size()).getState();
Ted Kremenek38cc6bc2009-11-26 02:38:19 +00003426 C.addTransition(state);
3427}
3428
Ted Kremenekd593eb92009-11-25 22:17:44 +00003429//===----------------------------------------------------------------------===//
Ted Kremenekd71ed262008-04-10 22:16:52 +00003430// Transfer function creation for external clients.
Ted Kremenek6b3a0f72008-03-11 06:39:11 +00003431//===----------------------------------------------------------------------===//
3432
Argyrios Kyrtzidisd2592a32010-12-22 18:53:44 +00003433void CFRefCount::RegisterChecks(ExprEngine& Eng) {
Ted Kremenekd295bae2009-11-25 22:08:49 +00003434 BugReporter &BR = Eng.getBugReporter();
Ted Kremenekdcee3ce2010-07-01 20:16:50 +00003435
Ted Kremenekd295bae2009-11-25 22:08:49 +00003436 useAfterRelease = new UseAfterRelease(this);
3437 BR.Register(useAfterRelease);
Ted Kremenekdcee3ce2010-07-01 20:16:50 +00003438
Ted Kremenekd295bae2009-11-25 22:08:49 +00003439 releaseNotOwned = new BadRelease(this);
3440 BR.Register(releaseNotOwned);
Ted Kremenekdcee3ce2010-07-01 20:16:50 +00003441
Ted Kremenekd295bae2009-11-25 22:08:49 +00003442 deallocGC = new DeallocGC(this);
3443 BR.Register(deallocGC);
Ted Kremenekdcee3ce2010-07-01 20:16:50 +00003444
Ted Kremenekd295bae2009-11-25 22:08:49 +00003445 deallocNotOwned = new DeallocNotOwned(this);
3446 BR.Register(deallocNotOwned);
Ted Kremenekdcee3ce2010-07-01 20:16:50 +00003447
Ted Kremenekd295bae2009-11-25 22:08:49 +00003448 overAutorelease = new OverAutorelease(this);
3449 BR.Register(overAutorelease);
Ted Kremenekdcee3ce2010-07-01 20:16:50 +00003450
Ted Kremenekd295bae2009-11-25 22:08:49 +00003451 returnNotOwnedForOwned = new ReturnedNotOwnedForOwned(this);
3452 BR.Register(returnNotOwnedForOwned);
Ted Kremenekdcee3ce2010-07-01 20:16:50 +00003453
Ted Kremenekd295bae2009-11-25 22:08:49 +00003454 // First register "return" leaks.
3455 const char* name = 0;
Ted Kremenekdcee3ce2010-07-01 20:16:50 +00003456
Ted Kremenekd295bae2009-11-25 22:08:49 +00003457 if (isGCEnabled())
3458 name = "Leak of returned object when using garbage collection";
3459 else if (getLangOptions().getGCMode() == LangOptions::HybridGC)
3460 name = "Leak of returned object when not using garbage collection (GC) in "
3461 "dual GC/non-GC code";
3462 else {
3463 assert(getLangOptions().getGCMode() == LangOptions::NonGC);
3464 name = "Leak of returned object";
3465 }
Ted Kremenekdcee3ce2010-07-01 20:16:50 +00003466
Ted Kremenekd295bae2009-11-25 22:08:49 +00003467 // Leaks should not be reported if they are post-dominated by a sink.
3468 leakAtReturn = new LeakAtReturn(this, name);
3469 leakAtReturn->setSuppressOnSink(true);
3470 BR.Register(leakAtReturn);
Ted Kremenekdcee3ce2010-07-01 20:16:50 +00003471
Ted Kremenekd295bae2009-11-25 22:08:49 +00003472 // Second, register leaks within a function/method.
3473 if (isGCEnabled())
3474 name = "Leak of object when using garbage collection";
3475 else if (getLangOptions().getGCMode() == LangOptions::HybridGC)
3476 name = "Leak of object when not using garbage collection (GC) in "
3477 "dual GC/non-GC code";
3478 else {
3479 assert(getLangOptions().getGCMode() == LangOptions::NonGC);
3480 name = "Leak";
3481 }
Ted Kremenekdcee3ce2010-07-01 20:16:50 +00003482
Ted Kremenekd295bae2009-11-25 22:08:49 +00003483 // Leaks should not be reported if they are post-dominated by sinks.
3484 leakWithinFunction = new LeakWithinFunction(this, name);
3485 leakWithinFunction->setSuppressOnSink(true);
3486 BR.Register(leakWithinFunction);
Ted Kremenekdcee3ce2010-07-01 20:16:50 +00003487
Ted Kremenekd295bae2009-11-25 22:08:49 +00003488 // Save the reference to the BugReporter.
3489 this->BR = &BR;
Ted Kremenekdcee3ce2010-07-01 20:16:50 +00003490
Argyrios Kyrtzidisd2592a32010-12-22 18:53:44 +00003491 // Register the RetainReleaseChecker with the ExprEngine object.
Ted Kremenekd593eb92009-11-25 22:17:44 +00003492 // Functionality in CFRefCount will be migrated to RetainReleaseChecker
3493 // over time.
3494 Eng.registerCheck(new RetainReleaseChecker(this));
Ted Kremenekd295bae2009-11-25 22:08:49 +00003495}
3496
Ted Kremenek9ef65372010-12-23 07:20:52 +00003497TransferFuncs* ento::MakeCFRefCountTF(ASTContext& Ctx, bool GCEnabled,
Ted Kremenek072192b2008-04-30 23:47:44 +00003498 const LangOptions& lopts) {
Ted Kremenek78d46242008-07-22 16:21:24 +00003499 return new CFRefCount(Ctx, GCEnabled, lopts);
Mike Stump1eb44332009-09-09 15:08:12 +00003500}