blob: d2d7ab8d041799df9eaa68e8cfe2b700278890c7 [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
Ted Kremenek6b3a0f72008-03-11 06:39:11 +000015#include "GRSimpleVals.h"
Ted Kremenek072192b2008-04-30 23:47:44 +000016#include "clang/Basic/LangOptions.h"
Ted Kremenekc9fa2f72008-05-01 23:13:35 +000017#include "clang/Basic/SourceManager.h"
Ted Kremenek4adc81e2008-08-13 04:27:00 +000018#include "clang/Analysis/PathSensitive/GRState.h"
Ted Kremenekb9d17f92008-08-17 03:20:02 +000019#include "clang/Analysis/PathSensitive/GRStateTrait.h"
Ted Kremenek4dc41cc2008-03-31 18:26:32 +000020#include "clang/Analysis/PathDiagnostic.h"
Ted Kremenek2fff37e2008-03-06 00:08:09 +000021#include "clang/Analysis/LocalCheckers.h"
Ted Kremenekfa34b332008-04-09 01:10:13 +000022#include "clang/Analysis/PathDiagnostic.h"
23#include "clang/Analysis/PathSensitive/BugReporter.h"
Daniel Dunbarc4a1dea2008-08-11 05:35:13 +000024#include "clang/AST/DeclObjC.h"
Ted Kremenek6b3a0f72008-03-11 06:39:11 +000025#include "llvm/ADT/DenseMap.h"
26#include "llvm/ADT/FoldingSet.h"
27#include "llvm/ADT/ImmutableMap.h"
Ted Kremenek6d348932008-10-21 15:53:15 +000028#include "llvm/ADT/ImmutableList.h"
Ted Kremenek900a2d72008-05-07 18:36:45 +000029#include "llvm/ADT/StringExtras.h"
Ted Kremenekfa34b332008-04-09 01:10:13 +000030#include "llvm/Support/Compiler.h"
Ted Kremenek6ed9afc2008-05-16 18:33:44 +000031#include "llvm/ADT/STLExtras.h"
Ted Kremenekf3948042008-03-11 19:44:10 +000032#include <ostream>
Ted Kremenek2cf943a2008-04-18 04:55:01 +000033#include <sstream>
Ted Kremenek98530452008-08-12 20:41:56 +000034#include <stdarg.h>
Ted Kremenek2fff37e2008-03-06 00:08:09 +000035
36using namespace clang;
Ted Kremenek5c74d502008-10-24 21:18:08 +000037
38//===----------------------------------------------------------------------===//
39// Utility functions.
40//===----------------------------------------------------------------------===//
41
Ted Kremenek900a2d72008-05-07 18:36:45 +000042using llvm::CStrInCStrNoCase;
Ted Kremenek2fff37e2008-03-06 00:08:09 +000043
Ted Kremenek5c74d502008-10-24 21:18:08 +000044// The "fundamental rule" for naming conventions of methods:
45// (url broken into two lines)
46// http://developer.apple.com/documentation/Cocoa/Conceptual/
47// MemoryMgmt/Tasks/MemoryManagementRules.html
48//
49// "You take ownership of an object if you create it using a method whose name
50// begins with “alloc” or “new” or contains “copy” (for example, alloc,
51// newObject, or mutableCopy), or if you send it a retain message. You are
52// responsible for relinquishing ownership of objects you own using release
53// or autorelease. Any other time you receive an object, you must
54// not release it."
55//
56static bool followsFundamentalRule(const char* s) {
Ted Kremeneke1e91af2008-10-30 23:14:58 +000057 while (*s == '_') ++s;
Ted Kremenek234a4c22009-01-07 00:39:56 +000058 return CStrInCStrNoCase(s, "copy")
59 || CStrInCStrNoCase(s, "new") == s
60 || CStrInCStrNoCase(s, "alloc") == s;
Ted Kremenek4c79e552008-11-05 16:54:44 +000061}
62
63static bool followsReturnRule(const char* s) {
64 while (*s == '_') ++s;
65 return followsFundamentalRule(s) || CStrInCStrNoCase(s, "init") == s;
66}
Ted Kremenek5c74d502008-10-24 21:18:08 +000067
Ted Kremenek05cbe1a2008-04-09 23:49:11 +000068//===----------------------------------------------------------------------===//
Ted Kremenek553cf182008-06-25 21:21:56 +000069// Selector creation functions.
Ted Kremenek4fd88972008-04-17 18:12:53 +000070//===----------------------------------------------------------------------===//
71
Ted Kremenekb83e02e2008-05-01 18:31:44 +000072static inline Selector GetNullarySelector(const char* name, ASTContext& Ctx) {
Ted Kremenek4fd88972008-04-17 18:12:53 +000073 IdentifierInfo* II = &Ctx.Idents.get(name);
74 return Ctx.Selectors.getSelector(0, &II);
75}
76
Ted Kremenek9c32d082008-05-06 00:30:21 +000077static inline Selector GetUnarySelector(const char* name, ASTContext& Ctx) {
78 IdentifierInfo* II = &Ctx.Idents.get(name);
79 return Ctx.Selectors.getSelector(1, &II);
80}
81
Ted Kremenek553cf182008-06-25 21:21:56 +000082//===----------------------------------------------------------------------===//
83// Type querying functions.
84//===----------------------------------------------------------------------===//
85
Ted Kremenek12619382009-01-12 21:45:02 +000086static bool hasPrefix(const char* s, const char* prefix) {
87 if (!prefix)
88 return true;
Ted Kremenek0fcbf8e2008-05-07 20:06:41 +000089
Ted Kremenek12619382009-01-12 21:45:02 +000090 char c = *s;
91 char cP = *prefix;
Ted Kremenek0fcbf8e2008-05-07 20:06:41 +000092
Ted Kremenek12619382009-01-12 21:45:02 +000093 while (c != '\0' && cP != '\0') {
94 if (c != cP) break;
95 c = *(++s);
96 cP = *(++prefix);
97 }
Ted Kremenek0fcbf8e2008-05-07 20:06:41 +000098
Ted Kremenek12619382009-01-12 21:45:02 +000099 return cP == '\0';
Ted Kremenek0fcbf8e2008-05-07 20:06:41 +0000100}
101
Ted Kremenek12619382009-01-12 21:45:02 +0000102static bool hasSuffix(const char* s, const char* suffix) {
103 const char* loc = strstr(s, suffix);
104 return loc && strcmp(suffix, loc) == 0;
105}
106
107static bool isRefType(QualType RetTy, const char* prefix,
108 ASTContext* Ctx = 0, const char* name = 0) {
Ted Kremenek37d785b2008-07-15 16:50:12 +0000109
Ted Kremenek12619382009-01-12 21:45:02 +0000110 if (TypedefType* TD = dyn_cast<TypedefType>(RetTy.getTypePtr())) {
111 const char* TDName = TD->getDecl()->getIdentifier()->getName();
112 return hasPrefix(TDName, prefix) && hasSuffix(TDName, "Ref");
113 }
114
115 if (!Ctx || !name)
Ted Kremenek37d785b2008-07-15 16:50:12 +0000116 return false;
Ted Kremenek12619382009-01-12 21:45:02 +0000117
118 // Is the type void*?
119 const PointerType* PT = RetTy->getAsPointerType();
120 if (!(PT->getPointeeType().getUnqualifiedType() == Ctx->VoidTy))
Ted Kremenek37d785b2008-07-15 16:50:12 +0000121 return false;
Ted Kremenek12619382009-01-12 21:45:02 +0000122
123 // Does the name start with the prefix?
124 return hasPrefix(name, prefix);
Ted Kremenek37d785b2008-07-15 16:50:12 +0000125}
126
Ted Kremenek4fd88972008-04-17 18:12:53 +0000127//===----------------------------------------------------------------------===//
Ted Kremenek553cf182008-06-25 21:21:56 +0000128// Primitives used for constructing summaries for function/method calls.
Ted Kremenek05cbe1a2008-04-09 23:49:11 +0000129//===----------------------------------------------------------------------===//
130
Ted Kremenek553cf182008-06-25 21:21:56 +0000131namespace {
132/// ArgEffect is used to summarize a function/method call's effect on a
133/// particular argument.
Ted Kremenek070a8252008-07-09 18:11:16 +0000134enum ArgEffect { IncRef, DecRef, DoNothing, DoNothingByRef,
135 StopTracking, MayEscape, SelfOwn, Autorelease };
Ted Kremenek553cf182008-06-25 21:21:56 +0000136
137/// ArgEffects summarizes the effects of a function/method call on all of
138/// its arguments.
139typedef std::vector<std::pair<unsigned,ArgEffect> > ArgEffects;
Ted Kremenek6b3a0f72008-03-11 06:39:11 +0000140}
Ted Kremenek2fff37e2008-03-06 00:08:09 +0000141
Ted Kremenek6b3a0f72008-03-11 06:39:11 +0000142namespace llvm {
Ted Kremenek553cf182008-06-25 21:21:56 +0000143template <> struct FoldingSetTrait<ArgEffects> {
144 static void Profile(const ArgEffects& X, FoldingSetNodeID& ID) {
145 for (ArgEffects::const_iterator I = X.begin(), E = X.end(); I!= E; ++I) {
146 ID.AddInteger(I->first);
147 ID.AddInteger((unsigned) I->second);
148 }
149 }
150};
Ted Kremenek6b3a0f72008-03-11 06:39:11 +0000151} // end llvm namespace
152
153namespace {
Ted Kremenek553cf182008-06-25 21:21:56 +0000154
155/// RetEffect is used to summarize a function/method call's behavior with
156/// respect to its return value.
157class VISIBILITY_HIDDEN RetEffect {
Ted Kremenek6b3a0f72008-03-11 06:39:11 +0000158public:
Ted Kremeneka7344702008-06-23 18:02:52 +0000159 enum Kind { NoRet, Alias, OwnedSymbol, OwnedAllocatedSymbol,
160 NotOwnedSymbol, ReceiverAlias };
Ted Kremenek553cf182008-06-25 21:21:56 +0000161
Ted Kremenek6b3a0f72008-03-11 06:39:11 +0000162private:
163 unsigned Data;
Ted Kremenek553cf182008-06-25 21:21:56 +0000164 RetEffect(Kind k, unsigned D = 0) { Data = (D << 3) | (unsigned) k; }
Ted Kremenek2fff37e2008-03-06 00:08:09 +0000165
Ted Kremenek6b3a0f72008-03-11 06:39:11 +0000166public:
Ted Kremenek553cf182008-06-25 21:21:56 +0000167
Ted Kremenekd3dbcf42008-05-05 22:11:16 +0000168 Kind getKind() const { return (Kind) (Data & 0x7); }
Ted Kremenek553cf182008-06-25 21:21:56 +0000169
170 unsigned getIndex() const {
Ted Kremenek6b3a0f72008-03-11 06:39:11 +0000171 assert(getKind() == Alias);
Ted Kremenekd3dbcf42008-05-05 22:11:16 +0000172 return Data >> 3;
Ted Kremenek6b3a0f72008-03-11 06:39:11 +0000173 }
Ted Kremenek2fff37e2008-03-06 00:08:09 +0000174
Ted Kremenek553cf182008-06-25 21:21:56 +0000175 static RetEffect MakeAlias(unsigned Idx) {
176 return RetEffect(Alias, Idx);
177 }
178 static RetEffect MakeReceiverAlias() {
179 return RetEffect(ReceiverAlias);
180 }
Ted Kremeneka7344702008-06-23 18:02:52 +0000181 static RetEffect MakeOwned(bool isAllocated = false) {
Ted Kremenek553cf182008-06-25 21:21:56 +0000182 return RetEffect(isAllocated ? OwnedAllocatedSymbol : OwnedSymbol);
183 }
184 static RetEffect MakeNotOwned() {
185 return RetEffect(NotOwnedSymbol);
186 }
187 static RetEffect MakeNoRet() {
188 return RetEffect(NoRet);
Ted Kremeneka7344702008-06-23 18:02:52 +0000189 }
Ted Kremenek2fff37e2008-03-06 00:08:09 +0000190
Ted Kremenek553cf182008-06-25 21:21:56 +0000191 operator Kind() const {
192 return getKind();
193 }
Ted Kremenek6b3a0f72008-03-11 06:39:11 +0000194
Ted Kremenek553cf182008-06-25 21:21:56 +0000195 void Profile(llvm::FoldingSetNodeID& ID) const {
196 ID.AddInteger(Data);
197 }
Ted Kremenek6b3a0f72008-03-11 06:39:11 +0000198};
Ted Kremenek6b3a0f72008-03-11 06:39:11 +0000199
Ted Kremenek553cf182008-06-25 21:21:56 +0000200
201class VISIBILITY_HIDDEN RetainSummary : public llvm::FoldingSetNode {
Ted Kremenek1bffd742008-05-06 15:44:25 +0000202 /// Args - an ordered vector of (index, ArgEffect) pairs, where index
203 /// specifies the argument (starting from 0). This can be sparsely
204 /// populated; arguments with no entry in Args use 'DefaultArgEffect'.
Ted Kremenek6b3a0f72008-03-11 06:39:11 +0000205 ArgEffects* Args;
Ted Kremenek1bffd742008-05-06 15:44:25 +0000206
207 /// DefaultArgEffect - The default ArgEffect to apply to arguments that
208 /// do not have an entry in Args.
209 ArgEffect DefaultArgEffect;
210
Ted Kremenek553cf182008-06-25 21:21:56 +0000211 /// Receiver - If this summary applies to an Objective-C message expression,
212 /// this is the effect applied to the state of the receiver.
Ted Kremenek3c0cea32008-05-06 02:26:56 +0000213 ArgEffect Receiver;
Ted Kremenek553cf182008-06-25 21:21:56 +0000214
215 /// Ret - The effect on the return value. Used to indicate if the
216 /// function/method call returns a new tracked symbol, returns an
217 /// alias of one of the arguments in the call, and so on.
Ted Kremenek6b3a0f72008-03-11 06:39:11 +0000218 RetEffect Ret;
Ted Kremenek553cf182008-06-25 21:21:56 +0000219
Ted Kremenek70a733e2008-07-18 17:24:20 +0000220 /// EndPath - Indicates that execution of this method/function should
221 /// terminate the simulation of a path.
222 bool EndPath;
223
Ted Kremenek6b3a0f72008-03-11 06:39:11 +0000224public:
225
Ted Kremenek1bffd742008-05-06 15:44:25 +0000226 RetainSummary(ArgEffects* A, RetEffect R, ArgEffect defaultEff,
Ted Kremenek70a733e2008-07-18 17:24:20 +0000227 ArgEffect ReceiverEff, bool endpath = false)
228 : Args(A), DefaultArgEffect(defaultEff), Receiver(ReceiverEff), Ret(R),
229 EndPath(endpath) {}
Ted Kremenek6b3a0f72008-03-11 06:39:11 +0000230
Ted Kremenek553cf182008-06-25 21:21:56 +0000231 /// getArg - Return the argument effect on the argument specified by
232 /// idx (starting from 0).
Ted Kremenek1ac08d62008-03-11 17:48:22 +0000233 ArgEffect getArg(unsigned idx) const {
Ted Kremenek1bffd742008-05-06 15:44:25 +0000234
Ted Kremenek891d5cc2008-04-24 17:22:33 +0000235 if (!Args)
Ted Kremenek1bffd742008-05-06 15:44:25 +0000236 return DefaultArgEffect;
Ted Kremenek891d5cc2008-04-24 17:22:33 +0000237
238 // If Args is present, it is likely to contain only 1 element.
239 // Just do a linear search. Do it from the back because functions with
240 // large numbers of arguments will be tail heavy with respect to which
Ted Kremenek553cf182008-06-25 21:21:56 +0000241 // argument they actually modify with respect to the reference count.
Ted Kremenek891d5cc2008-04-24 17:22:33 +0000242 for (ArgEffects::reverse_iterator I=Args->rbegin(), E=Args->rend();
243 I!=E; ++I) {
244
245 if (idx > I->first)
Ted Kremenek1bffd742008-05-06 15:44:25 +0000246 return DefaultArgEffect;
Ted Kremenek891d5cc2008-04-24 17:22:33 +0000247
248 if (idx == I->first)
249 return I->second;
250 }
251
Ted Kremenek1bffd742008-05-06 15:44:25 +0000252 return DefaultArgEffect;
Ted Kremenek1ac08d62008-03-11 17:48:22 +0000253 }
254
Ted Kremenek553cf182008-06-25 21:21:56 +0000255 /// getRetEffect - Returns the effect on the return value of the call.
Ted Kremenek3c0cea32008-05-06 02:26:56 +0000256 RetEffect getRetEffect() const {
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000257 return Ret;
258 }
259
Ted Kremenek70a733e2008-07-18 17:24:20 +0000260 /// isEndPath - Returns true if executing the given method/function should
261 /// terminate the path.
262 bool isEndPath() const { return EndPath; }
263
Ted Kremenek553cf182008-06-25 21:21:56 +0000264 /// getReceiverEffect - Returns the effect on the receiver of the call.
265 /// This is only meaningful if the summary applies to an ObjCMessageExpr*.
Ted Kremenek3c0cea32008-05-06 02:26:56 +0000266 ArgEffect getReceiverEffect() const {
267 return Receiver;
268 }
269
Ted Kremenek55499762008-06-17 02:43:46 +0000270 typedef ArgEffects::const_iterator ExprIterator;
Ted Kremenek6b3a0f72008-03-11 06:39:11 +0000271
Ted Kremenek55499762008-06-17 02:43:46 +0000272 ExprIterator begin_args() const { return Args->begin(); }
273 ExprIterator end_args() const { return Args->end(); }
Ted Kremenek6b3a0f72008-03-11 06:39:11 +0000274
Ted Kremenek3c0cea32008-05-06 02:26:56 +0000275 static void Profile(llvm::FoldingSetNodeID& ID, ArgEffects* A,
Ted Kremenek1bffd742008-05-06 15:44:25 +0000276 RetEffect RetEff, ArgEffect DefaultEff,
Ted Kremenek2d1086c2008-07-18 17:39:56 +0000277 ArgEffect ReceiverEff, bool EndPath) {
Ted Kremenek6b3a0f72008-03-11 06:39:11 +0000278 ID.AddPointer(A);
Ted Kremenek3c0cea32008-05-06 02:26:56 +0000279 ID.Add(RetEff);
Ted Kremenek1bffd742008-05-06 15:44:25 +0000280 ID.AddInteger((unsigned) DefaultEff);
Ted Kremenek3c0cea32008-05-06 02:26:56 +0000281 ID.AddInteger((unsigned) ReceiverEff);
Ted Kremenek2d1086c2008-07-18 17:39:56 +0000282 ID.AddInteger((unsigned) EndPath);
Ted Kremenek6b3a0f72008-03-11 06:39:11 +0000283 }
284
285 void Profile(llvm::FoldingSetNodeID& ID) const {
Ted Kremenek2d1086c2008-07-18 17:39:56 +0000286 Profile(ID, Args, Ret, DefaultArgEffect, Receiver, EndPath);
Ted Kremenek6b3a0f72008-03-11 06:39:11 +0000287 }
288};
Ted Kremenek4f22a782008-06-23 23:30:29 +0000289} // end anonymous namespace
Ted Kremenek6b3a0f72008-03-11 06:39:11 +0000290
Ted Kremenek553cf182008-06-25 21:21:56 +0000291//===----------------------------------------------------------------------===//
292// Data structures for constructing summaries.
293//===----------------------------------------------------------------------===//
Ted Kremenek53301ba2008-06-24 03:49:48 +0000294
Ted Kremenek553cf182008-06-25 21:21:56 +0000295namespace {
296class VISIBILITY_HIDDEN ObjCSummaryKey {
297 IdentifierInfo* II;
298 Selector S;
299public:
300 ObjCSummaryKey(IdentifierInfo* ii, Selector s)
301 : II(ii), S(s) {}
302
303 ObjCSummaryKey(ObjCInterfaceDecl* d, Selector s)
304 : II(d ? d->getIdentifier() : 0), S(s) {}
305
306 ObjCSummaryKey(Selector s)
307 : II(0), S(s) {}
308
309 IdentifierInfo* getIdentifier() const { return II; }
310 Selector getSelector() const { return S; }
311};
Ted Kremenek4f22a782008-06-23 23:30:29 +0000312}
313
314namespace llvm {
Ted Kremenek553cf182008-06-25 21:21:56 +0000315template <> struct DenseMapInfo<ObjCSummaryKey> {
316 static inline ObjCSummaryKey getEmptyKey() {
317 return ObjCSummaryKey(DenseMapInfo<IdentifierInfo*>::getEmptyKey(),
318 DenseMapInfo<Selector>::getEmptyKey());
319 }
Ted Kremenek4f22a782008-06-23 23:30:29 +0000320
Ted Kremenek553cf182008-06-25 21:21:56 +0000321 static inline ObjCSummaryKey getTombstoneKey() {
322 return ObjCSummaryKey(DenseMapInfo<IdentifierInfo*>::getTombstoneKey(),
323 DenseMapInfo<Selector>::getTombstoneKey());
324 }
325
326 static unsigned getHashValue(const ObjCSummaryKey &V) {
327 return (DenseMapInfo<IdentifierInfo*>::getHashValue(V.getIdentifier())
328 & 0x88888888)
329 | (DenseMapInfo<Selector>::getHashValue(V.getSelector())
330 & 0x55555555);
331 }
332
333 static bool isEqual(const ObjCSummaryKey& LHS, const ObjCSummaryKey& RHS) {
334 return DenseMapInfo<IdentifierInfo*>::isEqual(LHS.getIdentifier(),
335 RHS.getIdentifier()) &&
336 DenseMapInfo<Selector>::isEqual(LHS.getSelector(),
337 RHS.getSelector());
338 }
339
340 static bool isPod() {
341 return DenseMapInfo<ObjCInterfaceDecl*>::isPod() &&
342 DenseMapInfo<Selector>::isPod();
343 }
344};
Ted Kremenek4f22a782008-06-23 23:30:29 +0000345} // end llvm namespace
Ted Kremenek6b3a0f72008-03-11 06:39:11 +0000346
Ted Kremenek4f22a782008-06-23 23:30:29 +0000347namespace {
Ted Kremenek553cf182008-06-25 21:21:56 +0000348class VISIBILITY_HIDDEN ObjCSummaryCache {
349 typedef llvm::DenseMap<ObjCSummaryKey, RetainSummary*> MapTy;
350 MapTy M;
351public:
352 ObjCSummaryCache() {}
353
354 typedef MapTy::iterator iterator;
355
356 iterator find(ObjCInterfaceDecl* D, Selector S) {
357
358 // Do a lookup with the (D,S) pair. If we find a match return
359 // the iterator.
360 ObjCSummaryKey K(D, S);
361 MapTy::iterator I = M.find(K);
362
363 if (I != M.end() || !D)
364 return I;
365
366 // Walk the super chain. If we find a hit with a parent, we'll end
367 // up returning that summary. We actually allow that key (null,S), as
368 // we cache summaries for the null ObjCInterfaceDecl* to allow us to
369 // generate initial summaries without having to worry about NSObject
370 // being declared.
371 // FIXME: We may change this at some point.
372 for (ObjCInterfaceDecl* C=D->getSuperClass() ;; C=C->getSuperClass()) {
373 if ((I = M.find(ObjCSummaryKey(C, S))) != M.end())
374 break;
375
376 if (!C)
377 return I;
378 }
379
380 // Cache the summary with original key to make the next lookup faster
381 // and return the iterator.
382 M[K] = I->second;
383 return I;
384 }
385
Ted Kremenek98530452008-08-12 20:41:56 +0000386
Ted Kremenek553cf182008-06-25 21:21:56 +0000387 iterator find(Expr* Receiver, Selector S) {
388 return find(getReceiverDecl(Receiver), S);
389 }
390
391 iterator find(IdentifierInfo* II, Selector S) {
392 // FIXME: Class method lookup. Right now we dont' have a good way
393 // of going between IdentifierInfo* and the class hierarchy.
394 iterator I = M.find(ObjCSummaryKey(II, S));
395 return I == M.end() ? M.find(ObjCSummaryKey(S)) : I;
396 }
397
398 ObjCInterfaceDecl* getReceiverDecl(Expr* E) {
399
400 const PointerType* PT = E->getType()->getAsPointerType();
401 if (!PT) return 0;
402
403 ObjCInterfaceType* OI = dyn_cast<ObjCInterfaceType>(PT->getPointeeType());
404 if (!OI) return 0;
405
406 return OI ? OI->getDecl() : 0;
407 }
408
409 iterator end() { return M.end(); }
410
411 RetainSummary*& operator[](ObjCMessageExpr* ME) {
412
413 Selector S = ME->getSelector();
414
415 if (Expr* Receiver = ME->getReceiver()) {
416 ObjCInterfaceDecl* OD = getReceiverDecl(Receiver);
417 return OD ? M[ObjCSummaryKey(OD->getIdentifier(), S)] : M[S];
418 }
419
420 return M[ObjCSummaryKey(ME->getClassName(), S)];
421 }
422
423 RetainSummary*& operator[](ObjCSummaryKey K) {
424 return M[K];
425 }
426
427 RetainSummary*& operator[](Selector S) {
428 return M[ ObjCSummaryKey(S) ];
429 }
430};
431} // end anonymous namespace
432
433//===----------------------------------------------------------------------===//
434// Data structures for managing collections of summaries.
435//===----------------------------------------------------------------------===//
436
437namespace {
438class VISIBILITY_HIDDEN RetainSummaryManager {
Ted Kremenekd3dbcf42008-05-05 22:11:16 +0000439
440 //==-----------------------------------------------------------------==//
441 // Typedefs.
442 //==-----------------------------------------------------------------==//
Ted Kremenek6b3a0f72008-03-11 06:39:11 +0000443
Ted Kremenekd3dbcf42008-05-05 22:11:16 +0000444 typedef llvm::FoldingSet<llvm::FoldingSetNodeWrapper<ArgEffects> >
445 ArgEffectsSetTy;
446
447 typedef llvm::FoldingSet<RetainSummary>
448 SummarySetTy;
449
450 typedef llvm::DenseMap<FunctionDecl*, RetainSummary*>
451 FuncSummariesTy;
452
Ted Kremenek4f22a782008-06-23 23:30:29 +0000453 typedef ObjCSummaryCache ObjCMethodSummariesTy;
Ted Kremenekd3dbcf42008-05-05 22:11:16 +0000454
455 //==-----------------------------------------------------------------==//
456 // Data.
457 //==-----------------------------------------------------------------==//
458
Ted Kremenek553cf182008-06-25 21:21:56 +0000459 /// Ctx - The ASTContext object for the analyzed ASTs.
Ted Kremenek377e2302008-04-29 05:33:51 +0000460 ASTContext& Ctx;
Ted Kremenek179064e2008-07-01 17:21:27 +0000461
Ted Kremenek070a8252008-07-09 18:11:16 +0000462 /// CFDictionaryCreateII - An IdentifierInfo* representing the indentifier
463 /// "CFDictionaryCreate".
464 IdentifierInfo* CFDictionaryCreateII;
465
Ted Kremenek553cf182008-06-25 21:21:56 +0000466 /// GCEnabled - Records whether or not the analyzed code runs in GC mode.
Ted Kremenek377e2302008-04-29 05:33:51 +0000467 const bool GCEnabled;
468
Ted Kremenek553cf182008-06-25 21:21:56 +0000469 /// SummarySet - A FoldingSet of uniqued summaries.
Ted Kremenek3ea0b6a2008-04-10 22:58:08 +0000470 SummarySetTy SummarySet;
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000471
Ted Kremenek553cf182008-06-25 21:21:56 +0000472 /// FuncSummaries - A map from FunctionDecls to summaries.
Ted Kremenekd3dbcf42008-05-05 22:11:16 +0000473 FuncSummariesTy FuncSummaries;
474
Ted Kremenek553cf182008-06-25 21:21:56 +0000475 /// ObjCClassMethodSummaries - A map from selectors (for instance methods)
476 /// to summaries.
Ted Kremenek1f180c32008-06-23 22:21:20 +0000477 ObjCMethodSummariesTy ObjCClassMethodSummaries;
Ted Kremenekd3dbcf42008-05-05 22:11:16 +0000478
Ted Kremenek553cf182008-06-25 21:21:56 +0000479 /// ObjCMethodSummaries - A map from selectors to summaries.
Ted Kremenek1f180c32008-06-23 22:21:20 +0000480 ObjCMethodSummariesTy ObjCMethodSummaries;
Ted Kremenekd3dbcf42008-05-05 22:11:16 +0000481
Ted Kremenek553cf182008-06-25 21:21:56 +0000482 /// ArgEffectsSet - A FoldingSet of uniqued ArgEffects.
Ted Kremenekd3dbcf42008-05-05 22:11:16 +0000483 ArgEffectsSetTy ArgEffectsSet;
484
Ted Kremenek553cf182008-06-25 21:21:56 +0000485 /// BPAlloc - A BumpPtrAllocator used for allocating summaries, ArgEffects,
486 /// and all other data used by the checker.
Ted Kremenekd3dbcf42008-05-05 22:11:16 +0000487 llvm::BumpPtrAllocator BPAlloc;
488
Ted Kremenek553cf182008-06-25 21:21:56 +0000489 /// ScratchArgs - A holding buffer for construct ArgEffects.
Ted Kremenekd3dbcf42008-05-05 22:11:16 +0000490 ArgEffects ScratchArgs;
491
Ted Kremenek432af592008-05-06 18:11:36 +0000492 RetainSummary* StopSummary;
493
Ted Kremenekd3dbcf42008-05-05 22:11:16 +0000494 //==-----------------------------------------------------------------==//
495 // Methods.
496 //==-----------------------------------------------------------------==//
497
Ted Kremenek553cf182008-06-25 21:21:56 +0000498 /// getArgEffects - Returns a persistent ArgEffects object based on the
499 /// data in ScratchArgs.
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000500 ArgEffects* getArgEffects();
Ted Kremenek6b3a0f72008-03-11 06:39:11 +0000501
Ted Kremenek86ad3bc2008-05-05 16:51:50 +0000502 enum UnaryFuncKind { cfretain, cfrelease, cfmakecollectable };
Ted Kremenek896cd9d2008-10-23 01:56:15 +0000503
504public:
Ted Kremenek12619382009-01-12 21:45:02 +0000505 RetainSummary* getUnarySummary(FunctionType* FT, UnaryFuncKind func);
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000506
Ted Kremenekd3dbcf42008-05-05 22:11:16 +0000507 RetainSummary* getCFSummaryCreateRule(FunctionDecl* FD);
508 RetainSummary* getCFSummaryGetRule(FunctionDecl* FD);
Ted Kremenek12619382009-01-12 21:45:02 +0000509 RetainSummary* getCFCreateGetRuleSummary(FunctionDecl* FD, const char* FName);
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000510
Ted Kremenek3c0cea32008-05-06 02:26:56 +0000511 RetainSummary* getPersistentSummary(ArgEffects* AE, RetEffect RetEff,
Ted Kremenek1bffd742008-05-06 15:44:25 +0000512 ArgEffect ReceiverEff = DoNothing,
Ted Kremenek70a733e2008-07-18 17:24:20 +0000513 ArgEffect DefaultEff = MayEscape,
514 bool isEndPath = false);
Ted Kremenek706522f2008-10-29 04:07:07 +0000515
Ted Kremenek3c0cea32008-05-06 02:26:56 +0000516 RetainSummary* getPersistentSummary(RetEffect RE,
Ted Kremenek1bffd742008-05-06 15:44:25 +0000517 ArgEffect ReceiverEff = DoNothing,
Ted Kremenek3eabf1c2008-05-22 17:31:13 +0000518 ArgEffect DefaultEff = MayEscape) {
Ted Kremenek1bffd742008-05-06 15:44:25 +0000519 return getPersistentSummary(getArgEffects(), RE, ReceiverEff, DefaultEff);
Ted Kremenek9c32d082008-05-06 00:30:21 +0000520 }
Ted Kremenek46e49ee2008-05-05 23:55:01 +0000521
Ted Kremenek1bffd742008-05-06 15:44:25 +0000522 RetainSummary* getPersistentStopSummary() {
Ted Kremenek432af592008-05-06 18:11:36 +0000523 if (StopSummary)
524 return StopSummary;
525
526 StopSummary = getPersistentSummary(RetEffect::MakeNoRet(),
527 StopTracking, StopTracking);
Ted Kremenek706522f2008-10-29 04:07:07 +0000528
Ted Kremenek432af592008-05-06 18:11:36 +0000529 return StopSummary;
Ted Kremenek1bffd742008-05-06 15:44:25 +0000530 }
Ted Kremenekb3095252008-05-06 04:20:12 +0000531
Ted Kremenek553cf182008-06-25 21:21:56 +0000532 RetainSummary* getInitMethodSummary(ObjCMessageExpr* ME);
Ted Kremenek46e49ee2008-05-05 23:55:01 +0000533
Ted Kremenek1f180c32008-06-23 22:21:20 +0000534 void InitializeClassMethodSummaries();
535 void InitializeMethodSummaries();
Ted Kremenek896cd9d2008-10-23 01:56:15 +0000536
Ted Kremenek234a4c22009-01-07 00:39:56 +0000537 bool isTrackedObjectType(QualType T);
538
Ted Kremenek896cd9d2008-10-23 01:56:15 +0000539private:
540
Ted Kremenek70a733e2008-07-18 17:24:20 +0000541 void addClsMethSummary(IdentifierInfo* ClsII, Selector S,
542 RetainSummary* Summ) {
543 ObjCClassMethodSummaries[ObjCSummaryKey(ClsII, S)] = Summ;
544 }
545
Ted Kremenek553cf182008-06-25 21:21:56 +0000546 void addNSObjectClsMethSummary(Selector S, RetainSummary *Summ) {
547 ObjCClassMethodSummaries[S] = Summ;
548 }
549
550 void addNSObjectMethSummary(Selector S, RetainSummary *Summ) {
551 ObjCMethodSummaries[S] = Summ;
552 }
553
Ted Kremenekaf9dc272008-08-12 18:48:50 +0000554 void addInstMethSummary(const char* Cls, RetainSummary* Summ, va_list argp) {
Ted Kremenek70a733e2008-07-18 17:24:20 +0000555
Ted Kremenek9e476de2008-08-12 18:30:56 +0000556 IdentifierInfo* ClsII = &Ctx.Idents.get(Cls);
557 llvm::SmallVector<IdentifierInfo*, 10> II;
558
559 while (const char* s = va_arg(argp, const char*))
560 II.push_back(&Ctx.Idents.get(s));
561
562 Selector S = Ctx.Selectors.getSelector(II.size(), &II[0]);
Ted Kremenek70a733e2008-07-18 17:24:20 +0000563 ObjCMethodSummaries[ObjCSummaryKey(ClsII, S)] = Summ;
564 }
Ted Kremenekaf9dc272008-08-12 18:48:50 +0000565
566 void addInstMethSummary(const char* Cls, RetainSummary* Summ, ...) {
567 va_list argp;
568 va_start(argp, Summ);
569 addInstMethSummary(Cls, Summ, argp);
570 va_end(argp);
571 }
Ted Kremenek9e476de2008-08-12 18:30:56 +0000572
573 void addPanicSummary(const char* Cls, ...) {
574 RetainSummary* Summ = getPersistentSummary(0, RetEffect::MakeNoRet(),
575 DoNothing, DoNothing, true);
576 va_list argp;
577 va_start (argp, Cls);
Ted Kremenekaf9dc272008-08-12 18:48:50 +0000578 addInstMethSummary(Cls, Summ, argp);
Ted Kremenek9e476de2008-08-12 18:30:56 +0000579 va_end(argp);
580 }
Ted Kremenek70a733e2008-07-18 17:24:20 +0000581
Ted Kremenek6b3a0f72008-03-11 06:39:11 +0000582public:
Ted Kremenekd3dbcf42008-05-05 22:11:16 +0000583
584 RetainSummaryManager(ASTContext& ctx, bool gcenabled)
Ted Kremenek179064e2008-07-01 17:21:27 +0000585 : Ctx(ctx),
Ted Kremenek070a8252008-07-09 18:11:16 +0000586 CFDictionaryCreateII(&ctx.Idents.get("CFDictionaryCreate")),
Ted Kremenek553cf182008-06-25 21:21:56 +0000587 GCEnabled(gcenabled), StopSummary(0) {
588
589 InitializeClassMethodSummaries();
590 InitializeMethodSummaries();
591 }
Ted Kremenek377e2302008-04-29 05:33:51 +0000592
Ted Kremenekd3dbcf42008-05-05 22:11:16 +0000593 ~RetainSummaryManager();
Ted Kremenek6b3a0f72008-03-11 06:39:11 +0000594
Ted Kremenekab592272008-06-24 03:56:45 +0000595 RetainSummary* getSummary(FunctionDecl* FD);
Ted Kremenek553cf182008-06-25 21:21:56 +0000596 RetainSummary* getMethodSummary(ObjCMessageExpr* ME, ObjCInterfaceDecl* ID);
Ted Kremenek1f180c32008-06-23 22:21:20 +0000597 RetainSummary* getClassMethodSummary(IdentifierInfo* ClsName, Selector S);
Ted Kremenekb3095252008-05-06 04:20:12 +0000598
Ted Kremenekd3dbcf42008-05-05 22:11:16 +0000599 bool isGCEnabled() const { return GCEnabled; }
Ted Kremenek6b3a0f72008-03-11 06:39:11 +0000600};
601
602} // end anonymous namespace
603
Ted Kremenek234a4c22009-01-07 00:39:56 +0000604
605
Ted Kremenek6b3a0f72008-03-11 06:39:11 +0000606//===----------------------------------------------------------------------===//
607// Implementation of checker data structures.
608//===----------------------------------------------------------------------===//
609
Ted Kremenekd3dbcf42008-05-05 22:11:16 +0000610RetainSummaryManager::~RetainSummaryManager() {
Ted Kremenek6b3a0f72008-03-11 06:39:11 +0000611
612 // FIXME: The ArgEffects could eventually be allocated from BPAlloc,
613 // mitigating the need to do explicit cleanup of the
614 // Argument-Effect summaries.
615
Ted Kremenek46e49ee2008-05-05 23:55:01 +0000616 for (ArgEffectsSetTy::iterator I = ArgEffectsSet.begin(),
617 E = ArgEffectsSet.end(); I!=E; ++I)
Ted Kremenek6b3a0f72008-03-11 06:39:11 +0000618 I->getValue().~ArgEffects();
Ted Kremenek2fff37e2008-03-06 00:08:09 +0000619}
Ted Kremenek6b3a0f72008-03-11 06:39:11 +0000620
Ted Kremenekd3dbcf42008-05-05 22:11:16 +0000621ArgEffects* RetainSummaryManager::getArgEffects() {
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000622
Ted Kremenek891d5cc2008-04-24 17:22:33 +0000623 if (ScratchArgs.empty())
624 return NULL;
625
626 // Compute a profile for a non-empty ScratchArgs.
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000627 llvm::FoldingSetNodeID profile;
628 profile.Add(ScratchArgs);
629 void* InsertPos;
630
Ted Kremenek891d5cc2008-04-24 17:22:33 +0000631 // Look up the uniqued copy, or create a new one.
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000632 llvm::FoldingSetNodeWrapper<ArgEffects>* E =
Ted Kremenekd3dbcf42008-05-05 22:11:16 +0000633 ArgEffectsSet.FindNodeOrInsertPos(profile, InsertPos);
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000634
Ted Kremenek891d5cc2008-04-24 17:22:33 +0000635 if (E) {
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000636 ScratchArgs.clear();
637 return &E->getValue();
638 }
639
640 E = (llvm::FoldingSetNodeWrapper<ArgEffects>*)
Ted Kremenek553cf182008-06-25 21:21:56 +0000641 BPAlloc.Allocate<llvm::FoldingSetNodeWrapper<ArgEffects> >();
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000642
643 new (E) llvm::FoldingSetNodeWrapper<ArgEffects>(ScratchArgs);
Ted Kremenekd3dbcf42008-05-05 22:11:16 +0000644 ArgEffectsSet.InsertNode(E, InsertPos);
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000645
646 ScratchArgs.clear();
647 return &E->getValue();
648}
649
Ted Kremenek3c0cea32008-05-06 02:26:56 +0000650RetainSummary*
651RetainSummaryManager::getPersistentSummary(ArgEffects* AE, RetEffect RetEff,
Ted Kremenek1bffd742008-05-06 15:44:25 +0000652 ArgEffect ReceiverEff,
Ted Kremenek70a733e2008-07-18 17:24:20 +0000653 ArgEffect DefaultEff,
654 bool isEndPath) {
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000655
Ted Kremenek891d5cc2008-04-24 17:22:33 +0000656 // Generate a profile for the summary.
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000657 llvm::FoldingSetNodeID profile;
Ted Kremenek2d1086c2008-07-18 17:39:56 +0000658 RetainSummary::Profile(profile, AE, RetEff, DefaultEff, ReceiverEff,
659 isEndPath);
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000660
Ted Kremenek891d5cc2008-04-24 17:22:33 +0000661 // Look up the uniqued summary, or create one if it doesn't exist.
662 void* InsertPos;
Ted Kremenekd3dbcf42008-05-05 22:11:16 +0000663 RetainSummary* Summ = SummarySet.FindNodeOrInsertPos(profile, InsertPos);
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000664
665 if (Summ)
666 return Summ;
667
Ted Kremenek891d5cc2008-04-24 17:22:33 +0000668 // Create the summary and return it.
Ted Kremenekd3dbcf42008-05-05 22:11:16 +0000669 Summ = (RetainSummary*) BPAlloc.Allocate<RetainSummary>();
Ted Kremenek70a733e2008-07-18 17:24:20 +0000670 new (Summ) RetainSummary(AE, RetEff, DefaultEff, ReceiverEff, isEndPath);
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000671 SummarySet.InsertNode(Summ, InsertPos);
672
673 return Summ;
674}
675
Ted Kremenekd3dbcf42008-05-05 22:11:16 +0000676//===----------------------------------------------------------------------===//
Ted Kremenek234a4c22009-01-07 00:39:56 +0000677// Predicates.
678//===----------------------------------------------------------------------===//
679
680bool RetainSummaryManager::isTrackedObjectType(QualType T) {
681 if (!Ctx.isObjCObjectPointerType(T))
682 return false;
683
684 // Does it subclass NSObject?
685 ObjCInterfaceType* OT = dyn_cast<ObjCInterfaceType>(T.getTypePtr());
686
687 // We assume that id<..>, id, and "Class" all represent tracked objects.
688 if (!OT)
689 return true;
690
691 // Does the object type subclass NSObject?
692 // FIXME: We can memoize here if this gets too expensive.
693 IdentifierInfo* NSObjectII = &Ctx.Idents.get("NSObject");
694 ObjCInterfaceDecl* ID = OT->getDecl();
695
696 for ( ; ID ; ID = ID->getSuperClass())
697 if (ID->getIdentifier() == NSObjectII)
698 return true;
699
700 return false;
701}
702
703//===----------------------------------------------------------------------===//
Ted Kremenekd3dbcf42008-05-05 22:11:16 +0000704// Summary creation for functions (largely uses of Core Foundation).
705//===----------------------------------------------------------------------===//
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000706
Ted Kremenek12619382009-01-12 21:45:02 +0000707static bool isRetain(FunctionDecl* FD, const char* FName) {
708 const char* loc = strstr(FName, "Retain");
709 return loc && loc[sizeof("Retain")-1] == '\0';
710}
711
712static bool isRelease(FunctionDecl* FD, const char* FName) {
713 const char* loc = strstr(FName, "Release");
714 return loc && loc[sizeof("Release")-1] == '\0';
715}
716
Ted Kremenekab592272008-06-24 03:56:45 +0000717RetainSummary* RetainSummaryManager::getSummary(FunctionDecl* FD) {
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000718
719 SourceLocation Loc = FD->getLocation();
720
721 if (!Loc.isFileID())
722 return NULL;
Ted Kremenek2fff37e2008-03-06 00:08:09 +0000723
Ted Kremenek891d5cc2008-04-24 17:22:33 +0000724 // Look up a summary in our cache of FunctionDecls -> Summaries.
Ted Kremenekd3dbcf42008-05-05 22:11:16 +0000725 FuncSummariesTy::iterator I = FuncSummaries.find(FD);
Ted Kremenek891d5cc2008-04-24 17:22:33 +0000726
Ted Kremenekd3dbcf42008-05-05 22:11:16 +0000727 if (I != FuncSummaries.end())
Ted Kremenek891d5cc2008-04-24 17:22:33 +0000728 return I->second;
729
730 // No summary. Generate one.
Ted Kremenek12619382009-01-12 21:45:02 +0000731 RetainSummary *S = 0;
Ted Kremenek86ad3bc2008-05-05 16:51:50 +0000732
Ted Kremenek37d785b2008-07-15 16:50:12 +0000733 do {
Ted Kremenek12619382009-01-12 21:45:02 +0000734 // We generate "stop" summaries for implicitly defined functions.
735 if (FD->isImplicit()) {
736 S = getPersistentStopSummary();
737 break;
Ted Kremenek37d785b2008-07-15 16:50:12 +0000738 }
Ted Kremenek6ca31912008-11-04 00:36:12 +0000739
Ted Kremenek12619382009-01-12 21:45:02 +0000740 FunctionType* FT = cast<FunctionType>(FD->getType());
741 const char* FName = FD->getIdentifier()->getName();
742
743 // Inspect the result type.
744 QualType RetTy = FT->getResultType();
745
746 // FIXME: This should all be refactored into a chain of "summary lookup"
747 // filters.
748 if (strcmp(FName, "IOServiceGetMatchingServices") == 0) {
749 // FIXES: <rdar://problem/6326900>
750 // This should be addressed using a API table. This strcmp is also
751 // a little gross, but there is no need to super optimize here.
752 assert (ScratchArgs.empty());
753 ScratchArgs.push_back(std::make_pair(1, DecRef));
754 S = getPersistentSummary(RetEffect::MakeNoRet(), DoNothing, DoNothing);
755 break;
Ted Kremenek64e859a2008-10-22 20:54:52 +0000756 }
Ted Kremenek12619382009-01-12 21:45:02 +0000757
758 // Handle: id NSMakeCollectable(CFTypeRef)
759 if (strcmp(FName, "NSMakeCollectable") == 0) {
760 S = (RetTy == Ctx.getObjCIdType())
761 ? getUnarySummary(FT, cfmakecollectable)
762 : getPersistentStopSummary();
763
764 break;
765 }
766
767 if (RetTy->isPointerType()) {
768 // For CoreFoundation ('CF') types.
769 if (isRefType(RetTy, "CF", &Ctx, FName)) {
770 if (isRetain(FD, FName))
771 S = getUnarySummary(FT, cfretain);
772 else if (strstr(FName, "MakeCollectable"))
773 S = getUnarySummary(FT, cfmakecollectable);
774 else
775 S = getCFCreateGetRuleSummary(FD, FName);
776
777 break;
778 }
779
780 // For CoreGraphics ('CG') types.
781 if (isRefType(RetTy, "CG", &Ctx, FName)) {
782 if (isRetain(FD, FName))
783 S = getUnarySummary(FT, cfretain);
784 else
785 S = getCFCreateGetRuleSummary(FD, FName);
786
787 break;
788 }
789
790 // For the Disk Arbitration API (DiskArbitration/DADisk.h)
791 if (isRefType(RetTy, "DADisk") ||
792 isRefType(RetTy, "DADissenter") ||
793 isRefType(RetTy, "DASessionRef")) {
794 S = getCFCreateGetRuleSummary(FD, FName);
795 break;
796 }
797
798 break;
799 }
800
801 // Check for release functions, the only kind of functions that we care
802 // about that don't return a pointer type.
803 if (FName[0] == 'C' && (FName[1] == 'F' || FName[1] == 'G')) {
804 if (isRelease(FD, FName+2))
805 S = getUnarySummary(FT, cfrelease);
806 else {
807 // For CoreFoundation and CoreGraphics functions we assume they
808 // follow the ownership idiom strictly and thus do not cause
809 // ownership to "escape".
810 assert (ScratchArgs.empty());
811 S = getPersistentSummary(RetEffect::MakeNoRet(), DoNothing,
812 DoNothing);
813 }
814 }
Ted Kremenek37d785b2008-07-15 16:50:12 +0000815 }
816 while (0);
Ted Kremenek891d5cc2008-04-24 17:22:33 +0000817
Ted Kremenekd3dbcf42008-05-05 22:11:16 +0000818 FuncSummaries[FD] = S;
Ted Kremenek86ad3bc2008-05-05 16:51:50 +0000819 return S;
Ted Kremenek2fff37e2008-03-06 00:08:09 +0000820}
821
Ted Kremenek37d785b2008-07-15 16:50:12 +0000822RetainSummary*
823RetainSummaryManager::getCFCreateGetRuleSummary(FunctionDecl* FD,
824 const char* FName) {
825
Ted Kremenek86ad3bc2008-05-05 16:51:50 +0000826 if (strstr(FName, "Create") || strstr(FName, "Copy"))
827 return getCFSummaryCreateRule(FD);
Ted Kremenek37d785b2008-07-15 16:50:12 +0000828
Ted Kremenek86ad3bc2008-05-05 16:51:50 +0000829 if (strstr(FName, "Get"))
830 return getCFSummaryGetRule(FD);
831
832 return 0;
833}
834
Ted Kremenekd3dbcf42008-05-05 22:11:16 +0000835RetainSummary*
Ted Kremenek12619382009-01-12 21:45:02 +0000836RetainSummaryManager::getUnarySummary(FunctionType* FT, UnaryFuncKind func) {
837 // Sanity check that this is *really* a unary function. This can
838 // happen if people do weird things.
839 FunctionTypeProto* FTP = dyn_cast<FunctionTypeProto>(FT);
840 if (!FTP || FTP->getNumArgs() != 1)
841 return getPersistentStopSummary();
Ted Kremenekd3dbcf42008-05-05 22:11:16 +0000842
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000843 assert (ScratchArgs.empty());
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000844
Ted Kremenek377e2302008-04-29 05:33:51 +0000845 switch (func) {
Ted Kremenek12619382009-01-12 21:45:02 +0000846 case cfretain: {
Ted Kremenek377e2302008-04-29 05:33:51 +0000847 ScratchArgs.push_back(std::make_pair(0, IncRef));
Ted Kremenek3eabf1c2008-05-22 17:31:13 +0000848 return getPersistentSummary(RetEffect::MakeAlias(0),
849 DoNothing, DoNothing);
Ted Kremenek377e2302008-04-29 05:33:51 +0000850 }
851
852 case cfrelease: {
Ted Kremenek377e2302008-04-29 05:33:51 +0000853 ScratchArgs.push_back(std::make_pair(0, DecRef));
Ted Kremenek3eabf1c2008-05-22 17:31:13 +0000854 return getPersistentSummary(RetEffect::MakeNoRet(),
855 DoNothing, DoNothing);
Ted Kremenek377e2302008-04-29 05:33:51 +0000856 }
857
858 case cfmakecollectable: {
Ted Kremenek377e2302008-04-29 05:33:51 +0000859 if (GCEnabled)
860 ScratchArgs.push_back(std::make_pair(0, DecRef));
861
Ted Kremenek3eabf1c2008-05-22 17:31:13 +0000862 return getPersistentSummary(RetEffect::MakeAlias(0),
863 DoNothing, DoNothing);
Ted Kremenek377e2302008-04-29 05:33:51 +0000864 }
865
866 default:
Ted Kremenek86ad3bc2008-05-05 16:51:50 +0000867 assert (false && "Not a supported unary function.");
Ted Kremenek98530452008-08-12 20:41:56 +0000868 return 0;
Ted Kremenek940b1d82008-04-10 23:44:06 +0000869 }
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000870}
871
Ted Kremenekd3dbcf42008-05-05 22:11:16 +0000872RetainSummary* RetainSummaryManager::getCFSummaryCreateRule(FunctionDecl* FD) {
Ted Kremenek891d5cc2008-04-24 17:22:33 +0000873 assert (ScratchArgs.empty());
Ted Kremenek070a8252008-07-09 18:11:16 +0000874
875 if (FD->getIdentifier() == CFDictionaryCreateII) {
876 ScratchArgs.push_back(std::make_pair(1, DoNothingByRef));
877 ScratchArgs.push_back(std::make_pair(2, DoNothingByRef));
878 }
879
Ted Kremeneka7344702008-06-23 18:02:52 +0000880 return getPersistentSummary(RetEffect::MakeOwned(true));
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000881}
882
Ted Kremenekd3dbcf42008-05-05 22:11:16 +0000883RetainSummary* RetainSummaryManager::getCFSummaryGetRule(FunctionDecl* FD) {
Ted Kremenek891d5cc2008-04-24 17:22:33 +0000884 assert (ScratchArgs.empty());
Ted Kremenek3eabf1c2008-05-22 17:31:13 +0000885 return getPersistentSummary(RetEffect::MakeNotOwned(), DoNothing, DoNothing);
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000886}
887
Ted Kremenek6b3a0f72008-03-11 06:39:11 +0000888//===----------------------------------------------------------------------===//
Ted Kremenekd3dbcf42008-05-05 22:11:16 +0000889// Summary creation for Selectors.
890//===----------------------------------------------------------------------===//
891
Ted Kremenek1bffd742008-05-06 15:44:25 +0000892RetainSummary*
Ted Kremenek553cf182008-06-25 21:21:56 +0000893RetainSummaryManager::getInitMethodSummary(ObjCMessageExpr* ME) {
Ted Kremenek46e49ee2008-05-05 23:55:01 +0000894 assert(ScratchArgs.empty());
895
896 RetainSummary* Summ =
Ted Kremenek9c32d082008-05-06 00:30:21 +0000897 getPersistentSummary(RetEffect::MakeReceiverAlias());
Ted Kremenek46e49ee2008-05-05 23:55:01 +0000898
Ted Kremenek553cf182008-06-25 21:21:56 +0000899 ObjCMethodSummaries[ME] = Summ;
Ted Kremenek46e49ee2008-05-05 23:55:01 +0000900 return Summ;
901}
Ted Kremenekd3dbcf42008-05-05 22:11:16 +0000902
Ted Kremenek553cf182008-06-25 21:21:56 +0000903
Ted Kremenek1bffd742008-05-06 15:44:25 +0000904RetainSummary*
Ted Kremenek553cf182008-06-25 21:21:56 +0000905RetainSummaryManager::getMethodSummary(ObjCMessageExpr* ME,
906 ObjCInterfaceDecl* ID) {
Ted Kremenek1bffd742008-05-06 15:44:25 +0000907
908 Selector S = ME->getSelector();
Ted Kremenek46e49ee2008-05-05 23:55:01 +0000909
Ted Kremenek553cf182008-06-25 21:21:56 +0000910 // Look up a summary in our summary cache.
911 ObjCMethodSummariesTy::iterator I = ObjCMethodSummaries.find(ID, S);
Ted Kremenek46e49ee2008-05-05 23:55:01 +0000912
Ted Kremenek1f180c32008-06-23 22:21:20 +0000913 if (I != ObjCMethodSummaries.end())
Ted Kremenek46e49ee2008-05-05 23:55:01 +0000914 return I->second;
Ted Kremenek46e49ee2008-05-05 23:55:01 +0000915
Ted Kremenek234a4c22009-01-07 00:39:56 +0000916 // "initXXX": pass-through for receiver.
Ted Kremenek46e49ee2008-05-05 23:55:01 +0000917 const char* s = S.getIdentifierInfoForSlot(0)->getName();
Ted Kremeneka4b695a2008-05-07 03:45:05 +0000918 assert (ScratchArgs.empty());
Ted Kremenekaee9e572008-05-06 06:09:09 +0000919
Ted Kremenek0327f772008-06-02 17:14:13 +0000920 if (strncmp(s, "init", 4) == 0 || strncmp(s, "_init", 5) == 0)
Ted Kremenek234a4c22009-01-07 00:39:56 +0000921 return getInitMethodSummary(ME);
Ted Kremenek1bffd742008-05-06 15:44:25 +0000922
Ted Kremenek234a4c22009-01-07 00:39:56 +0000923 // Look for methods that return an owned object.
924 if (!isTrackedObjectType(Ctx.getCanonicalType(ME->getType())))
Ted Kremenek84060db2008-05-07 04:25:59 +0000925 return 0;
Ted Kremeneka4b695a2008-05-07 03:45:05 +0000926
Ted Kremenek234a4c22009-01-07 00:39:56 +0000927 if (followsFundamentalRule(s)) {
928 RetEffect E = isGCEnabled() ? RetEffect::MakeNoRet()
929 : RetEffect::MakeOwned(true);
Ted Kremeneka4b695a2008-05-07 03:45:05 +0000930 RetainSummary* Summ = getPersistentSummary(E);
Ted Kremenek553cf182008-06-25 21:21:56 +0000931 ObjCMethodSummaries[ME] = Summ;
Ted Kremenek1bffd742008-05-06 15:44:25 +0000932 return Summ;
933 }
Ted Kremenek1bffd742008-05-06 15:44:25 +0000934
Ted Kremenek46e49ee2008-05-05 23:55:01 +0000935 return 0;
936}
937
Ted Kremenekc8395602008-05-06 21:26:51 +0000938RetainSummary*
Ted Kremenek1f180c32008-06-23 22:21:20 +0000939RetainSummaryManager::getClassMethodSummary(IdentifierInfo* ClsName,
940 Selector S) {
Ted Kremenekc8395602008-05-06 21:26:51 +0000941
Ted Kremenek553cf182008-06-25 21:21:56 +0000942 // FIXME: Eventually we should properly do class method summaries, but
943 // it requires us being able to walk the type hierarchy. Unfortunately,
944 // we cannot do this with just an IdentifierInfo* for the class name.
945
Ted Kremenekc8395602008-05-06 21:26:51 +0000946 // Look up a summary in our cache of Selectors -> Summaries.
Ted Kremenek553cf182008-06-25 21:21:56 +0000947 ObjCMethodSummariesTy::iterator I = ObjCClassMethodSummaries.find(ClsName, S);
Ted Kremenekc8395602008-05-06 21:26:51 +0000948
Ted Kremenek1f180c32008-06-23 22:21:20 +0000949 if (I != ObjCClassMethodSummaries.end())
Ted Kremenekc8395602008-05-06 21:26:51 +0000950 return I->second;
951
Ted Kremeneka22cc2f2008-05-06 23:07:13 +0000952 return 0;
Ted Kremenekc8395602008-05-06 21:26:51 +0000953}
954
Ted Kremenek1f180c32008-06-23 22:21:20 +0000955void RetainSummaryManager::InitializeClassMethodSummaries() {
Ted Kremenek9c32d082008-05-06 00:30:21 +0000956
957 assert (ScratchArgs.empty());
958
Ted Kremeneka7344702008-06-23 18:02:52 +0000959 RetEffect E = isGCEnabled() ? RetEffect::MakeNoRet()
960 : RetEffect::MakeOwned(true);
961
Ted Kremenek9c32d082008-05-06 00:30:21 +0000962 RetainSummary* Summ = getPersistentSummary(E);
963
Ted Kremenek553cf182008-06-25 21:21:56 +0000964 // Create the summaries for "alloc", "new", and "allocWithZone:" for
965 // NSObject and its derivatives.
966 addNSObjectClsMethSummary(GetNullarySelector("alloc", Ctx), Summ);
967 addNSObjectClsMethSummary(GetNullarySelector("new", Ctx), Summ);
968 addNSObjectClsMethSummary(GetUnarySelector("allocWithZone", Ctx), Summ);
Ted Kremenek70a733e2008-07-18 17:24:20 +0000969
970 // Create the [NSAssertionHandler currentHander] summary.
Ted Kremenek9e476de2008-08-12 18:30:56 +0000971 addClsMethSummary(&Ctx.Idents.get("NSAssertionHandler"),
Ted Kremenek1a804482008-07-18 18:14:26 +0000972 GetNullarySelector("currentHandler", Ctx),
Ted Kremenek6d348932008-10-21 15:53:15 +0000973 getPersistentSummary(RetEffect::MakeNotOwned()));
974
975 // Create the [NSAutoreleasePool addObject:] summary.
976 if (!isGCEnabled()) {
977 ScratchArgs.push_back(std::make_pair(0, Autorelease));
978 addClsMethSummary(&Ctx.Idents.get("NSAutoreleasePool"),
979 GetUnarySelector("addObject", Ctx),
980 getPersistentSummary(RetEffect::MakeNoRet(),
981 DoNothing, DoNothing));
982 }
Ted Kremenek9c32d082008-05-06 00:30:21 +0000983}
984
Ted Kremenek1f180c32008-06-23 22:21:20 +0000985void RetainSummaryManager::InitializeMethodSummaries() {
Ted Kremenekb3c3c282008-05-06 00:38:54 +0000986
987 assert (ScratchArgs.empty());
988
Ted Kremenekc8395602008-05-06 21:26:51 +0000989 // Create the "init" selector. It just acts as a pass-through for the
990 // receiver.
Ted Kremenek179064e2008-07-01 17:21:27 +0000991 RetainSummary* InitSumm = getPersistentSummary(RetEffect::MakeReceiverAlias());
992 addNSObjectMethSummary(GetNullarySelector("init", Ctx), InitSumm);
Ted Kremenekc8395602008-05-06 21:26:51 +0000993
994 // The next methods are allocators.
Ted Kremeneka7344702008-06-23 18:02:52 +0000995 RetEffect E = isGCEnabled() ? RetEffect::MakeNoRet()
996 : RetEffect::MakeOwned(true);
997
Ted Kremenek179064e2008-07-01 17:21:27 +0000998 RetainSummary* Summ = getPersistentSummary(E);
Ted Kremenekc8395602008-05-06 21:26:51 +0000999
1000 // Create the "copy" selector.
Ted Kremenek98530452008-08-12 20:41:56 +00001001 addNSObjectMethSummary(GetNullarySelector("copy", Ctx), Summ);
1002
Ted Kremenekb3c3c282008-05-06 00:38:54 +00001003 // Create the "mutableCopy" selector.
Ted Kremenek553cf182008-06-25 21:21:56 +00001004 addNSObjectMethSummary(GetNullarySelector("mutableCopy", Ctx), Summ);
Ted Kremenek98530452008-08-12 20:41:56 +00001005
Ted Kremenek3c0cea32008-05-06 02:26:56 +00001006 // Create the "retain" selector.
1007 E = RetEffect::MakeReceiverAlias();
1008 Summ = getPersistentSummary(E, isGCEnabled() ? DoNothing : IncRef);
Ted Kremenek553cf182008-06-25 21:21:56 +00001009 addNSObjectMethSummary(GetNullarySelector("retain", Ctx), Summ);
Ted Kremenek3c0cea32008-05-06 02:26:56 +00001010
1011 // Create the "release" selector.
1012 Summ = getPersistentSummary(E, isGCEnabled() ? DoNothing : DecRef);
Ted Kremenek553cf182008-06-25 21:21:56 +00001013 addNSObjectMethSummary(GetNullarySelector("release", Ctx), Summ);
Ted Kremenek299e8152008-05-07 21:17:39 +00001014
1015 // Create the "drain" selector.
1016 Summ = getPersistentSummary(E, isGCEnabled() ? DoNothing : DecRef);
Ted Kremenek553cf182008-06-25 21:21:56 +00001017 addNSObjectMethSummary(GetNullarySelector("drain", Ctx), Summ);
Ted Kremenek3c0cea32008-05-06 02:26:56 +00001018
1019 // Create the "autorelease" selector.
Ted Kremeneke19f4492008-06-30 16:57:41 +00001020 Summ = getPersistentSummary(E, isGCEnabled() ? DoNothing : Autorelease);
Ted Kremenek553cf182008-06-25 21:21:56 +00001021 addNSObjectMethSummary(GetNullarySelector("autorelease", Ctx), Summ);
Ted Kremenek98530452008-08-12 20:41:56 +00001022
Ted Kremenekaf9dc272008-08-12 18:48:50 +00001023 // For NSWindow, allocated objects are (initially) self-owned.
Ted Kremenek179064e2008-07-01 17:21:27 +00001024 RetainSummary *NSWindowSumm =
1025 getPersistentSummary(RetEffect::MakeReceiverAlias(), SelfOwn);
Ted Kremenekaf9dc272008-08-12 18:48:50 +00001026
1027 addInstMethSummary("NSWindow", NSWindowSumm, "initWithContentRect",
1028 "styleMask", "backing", "defer", NULL);
1029
1030 addInstMethSummary("NSWindow", NSWindowSumm, "initWithContentRect",
1031 "styleMask", "backing", "defer", "screen", NULL);
1032
1033 // For NSPanel (which subclasses NSWindow), allocated objects are not
1034 // self-owned.
1035 addInstMethSummary("NSPanel", InitSumm, "initWithContentRect",
1036 "styleMask", "backing", "defer", NULL);
1037
1038 addInstMethSummary("NSPanel", InitSumm, "initWithContentRect",
1039 "styleMask", "backing", "defer", "screen", NULL);
Ted Kremenek553cf182008-06-25 21:21:56 +00001040
Ted Kremenek70a733e2008-07-18 17:24:20 +00001041 // Create NSAssertionHandler summaries.
Ted Kremenek9e476de2008-08-12 18:30:56 +00001042 addPanicSummary("NSAssertionHandler", "handleFailureInFunction", "file",
1043 "lineNumber", "description", NULL);
Ted Kremenek70a733e2008-07-18 17:24:20 +00001044
Ted Kremenek9e476de2008-08-12 18:30:56 +00001045 addPanicSummary("NSAssertionHandler", "handleFailureInMethod", "object",
1046 "file", "lineNumber", "description", NULL);
Ted Kremenekb3c3c282008-05-06 00:38:54 +00001047}
1048
Ted Kremenekd3dbcf42008-05-05 22:11:16 +00001049//===----------------------------------------------------------------------===//
Ted Kremenek13922612008-04-16 20:40:59 +00001050// Reference-counting logic (typestate + counts).
Ted Kremenek6b3a0f72008-03-11 06:39:11 +00001051//===----------------------------------------------------------------------===//
1052
Ted Kremenek6b3a0f72008-03-11 06:39:11 +00001053namespace {
1054
Ted Kremenek05cbe1a2008-04-09 23:49:11 +00001055class VISIBILITY_HIDDEN RefVal {
Ted Kremenek4fd88972008-04-17 18:12:53 +00001056public:
Ted Kremenek1ac08d62008-03-11 17:48:22 +00001057
Ted Kremenek4fd88972008-04-17 18:12:53 +00001058 enum Kind {
1059 Owned = 0, // Owning reference.
1060 NotOwned, // Reference is not owned by still valid (not freed).
1061 Released, // Object has been released.
1062 ReturnedOwned, // Returned object passes ownership to caller.
1063 ReturnedNotOwned, // Return object does not pass ownership to caller.
1064 ErrorUseAfterRelease, // Object used after released.
1065 ErrorReleaseNotOwned, // Release of an object that was not owned.
Ted Kremenek3ad2cc82008-10-22 23:56:21 +00001066 ErrorLeak, // A memory leak due to excessive reference counts.
1067 ErrorLeakReturned // A memory leak due to the returning method not having
1068 // the correct naming conventions.
Ted Kremenek4fd88972008-04-17 18:12:53 +00001069 };
Ted Kremenek1ac08d62008-03-11 17:48:22 +00001070
Ted Kremenek4fd88972008-04-17 18:12:53 +00001071private:
1072
1073 Kind kind;
1074 unsigned Cnt;
Ted Kremenek553cf182008-06-25 21:21:56 +00001075 QualType T;
1076
1077 RefVal(Kind k, unsigned cnt, QualType t) : kind(k), Cnt(cnt), T(t) {}
1078 RefVal(Kind k, unsigned cnt = 0) : kind(k), Cnt(cnt) {}
Ted Kremenek1ac08d62008-03-11 17:48:22 +00001079
1080public:
Ted Kremenekdb863712008-04-16 22:32:20 +00001081
Ted Kremenek4fd88972008-04-17 18:12:53 +00001082 Kind getKind() const { return kind; }
Ted Kremenek1ac08d62008-03-11 17:48:22 +00001083
Ted Kremenek553cf182008-06-25 21:21:56 +00001084 unsigned getCount() const { return Cnt; }
1085 QualType getType() const { return T; }
Ted Kremenek4fd88972008-04-17 18:12:53 +00001086
1087 // Useful predicates.
Ted Kremenek1ac08d62008-03-11 17:48:22 +00001088
Ted Kremenek73c750b2008-03-11 18:14:09 +00001089 static bool isError(Kind k) { return k >= ErrorUseAfterRelease; }
1090
Ted Kremenekf9790ae2008-10-24 20:32:50 +00001091 static bool isLeak(Kind k) { return k >= ErrorLeak; }
Ted Kremenekdb863712008-04-16 22:32:20 +00001092
Ted Kremeneke7bd9c22008-04-11 22:25:11 +00001093 bool isOwned() const {
1094 return getKind() == Owned;
1095 }
1096
Ted Kremenekdb863712008-04-16 22:32:20 +00001097 bool isNotOwned() const {
1098 return getKind() == NotOwned;
1099 }
1100
Ted Kremenek4fd88972008-04-17 18:12:53 +00001101 bool isReturnedOwned() const {
1102 return getKind() == ReturnedOwned;
1103 }
1104
1105 bool isReturnedNotOwned() const {
1106 return getKind() == ReturnedNotOwned;
1107 }
1108
1109 bool isNonLeakError() const {
1110 Kind k = getKind();
1111 return isError(k) && !isLeak(k);
1112 }
1113
1114 // State creation: normal state.
1115
Ted Kremenek553cf182008-06-25 21:21:56 +00001116 static RefVal makeOwned(QualType t, unsigned Count = 1) {
1117 return RefVal(Owned, Count, t);
Ted Kremenek61b9f872008-04-10 23:09:18 +00001118 }
1119
Ted Kremenek553cf182008-06-25 21:21:56 +00001120 static RefVal makeNotOwned(QualType t, unsigned Count = 0) {
1121 return RefVal(NotOwned, Count, t);
Ted Kremenek61b9f872008-04-10 23:09:18 +00001122 }
Ted Kremenek4fd88972008-04-17 18:12:53 +00001123
1124 static RefVal makeReturnedOwned(unsigned Count) {
1125 return RefVal(ReturnedOwned, Count);
1126 }
1127
1128 static RefVal makeReturnedNotOwned() {
1129 return RefVal(ReturnedNotOwned);
1130 }
1131
Ted Kremenek4fd88972008-04-17 18:12:53 +00001132 // Comparison, profiling, and pretty-printing.
Ted Kremenek1ac08d62008-03-11 17:48:22 +00001133
Ted Kremenek4fd88972008-04-17 18:12:53 +00001134 bool operator==(const RefVal& X) const {
Ted Kremenek553cf182008-06-25 21:21:56 +00001135 return kind == X.kind && Cnt == X.Cnt && T == X.T;
Ted Kremenek4fd88972008-04-17 18:12:53 +00001136 }
Ted Kremenekf3948042008-03-11 19:44:10 +00001137
Ted Kremenek553cf182008-06-25 21:21:56 +00001138 RefVal operator-(size_t i) const {
1139 return RefVal(getKind(), getCount() - i, getType());
1140 }
1141
1142 RefVal operator+(size_t i) const {
1143 return RefVal(getKind(), getCount() + i, getType());
1144 }
1145
1146 RefVal operator^(Kind k) const {
1147 return RefVal(k, getCount(), getType());
1148 }
1149
1150
Ted Kremenek4fd88972008-04-17 18:12:53 +00001151 void Profile(llvm::FoldingSetNodeID& ID) const {
1152 ID.AddInteger((unsigned) kind);
1153 ID.AddInteger(Cnt);
Ted Kremenek553cf182008-06-25 21:21:56 +00001154 ID.Add(T);
Ted Kremenek4fd88972008-04-17 18:12:53 +00001155 }
1156
Ted Kremenekf3948042008-03-11 19:44:10 +00001157 void print(std::ostream& Out) const;
Ted Kremenek1ac08d62008-03-11 17:48:22 +00001158};
Ted Kremenekf3948042008-03-11 19:44:10 +00001159
1160void RefVal::print(std::ostream& Out) const {
Ted Kremenek553cf182008-06-25 21:21:56 +00001161 if (!T.isNull())
1162 Out << "Tracked Type:" << T.getAsString() << '\n';
1163
Ted Kremenekf3948042008-03-11 19:44:10 +00001164 switch (getKind()) {
1165 default: assert(false);
Ted Kremenek61b9f872008-04-10 23:09:18 +00001166 case Owned: {
1167 Out << "Owned";
1168 unsigned cnt = getCount();
1169 if (cnt) Out << " (+ " << cnt << ")";
Ted Kremenekf3948042008-03-11 19:44:10 +00001170 break;
Ted Kremenek61b9f872008-04-10 23:09:18 +00001171 }
Ted Kremenekf3948042008-03-11 19:44:10 +00001172
Ted Kremenek61b9f872008-04-10 23:09:18 +00001173 case NotOwned: {
Ted Kremenek4fd88972008-04-17 18:12:53 +00001174 Out << "NotOwned";
Ted Kremenek61b9f872008-04-10 23:09:18 +00001175 unsigned cnt = getCount();
1176 if (cnt) Out << " (+ " << cnt << ")";
Ted Kremenekf3948042008-03-11 19:44:10 +00001177 break;
Ted Kremenek61b9f872008-04-10 23:09:18 +00001178 }
Ted Kremenekf3948042008-03-11 19:44:10 +00001179
Ted Kremenek4fd88972008-04-17 18:12:53 +00001180 case ReturnedOwned: {
1181 Out << "ReturnedOwned";
1182 unsigned cnt = getCount();
1183 if (cnt) Out << " (+ " << cnt << ")";
1184 break;
1185 }
1186
1187 case ReturnedNotOwned: {
1188 Out << "ReturnedNotOwned";
1189 unsigned cnt = getCount();
1190 if (cnt) Out << " (+ " << cnt << ")";
1191 break;
1192 }
1193
Ted Kremenekf3948042008-03-11 19:44:10 +00001194 case Released:
1195 Out << "Released";
1196 break;
1197
Ted Kremenekdb863712008-04-16 22:32:20 +00001198 case ErrorLeak:
1199 Out << "Leaked";
1200 break;
1201
Ted Kremenek3ad2cc82008-10-22 23:56:21 +00001202 case ErrorLeakReturned:
1203 Out << "Leaked (Bad naming)";
1204 break;
1205
Ted Kremenekf3948042008-03-11 19:44:10 +00001206 case ErrorUseAfterRelease:
1207 Out << "Use-After-Release [ERROR]";
1208 break;
1209
1210 case ErrorReleaseNotOwned:
1211 Out << "Release of Not-Owned [ERROR]";
1212 break;
1213 }
1214}
Ted Kremenek1ac08d62008-03-11 17:48:22 +00001215
Ted Kremenek72cd17f2008-08-14 21:16:54 +00001216} // end anonymous namespace
1217
1218//===----------------------------------------------------------------------===//
1219// RefBindings - State used to track object reference counts.
1220//===----------------------------------------------------------------------===//
1221
Ted Kremenek2dabd432008-12-05 02:27:51 +00001222typedef llvm::ImmutableMap<SymbolRef, RefVal> RefBindings;
Ted Kremenek72cd17f2008-08-14 21:16:54 +00001223static int RefBIndex = 0;
1224
1225namespace clang {
Ted Kremenekb9d17f92008-08-17 03:20:02 +00001226 template<>
1227 struct GRStateTrait<RefBindings> : public GRStatePartialTrait<RefBindings> {
1228 static inline void* GDMIndex() { return &RefBIndex; }
1229 };
1230}
Ted Kremenek6d348932008-10-21 15:53:15 +00001231
1232//===----------------------------------------------------------------------===//
1233// ARBindings - State used to track objects in autorelease pools.
1234//===----------------------------------------------------------------------===//
1235
Ted Kremenek2dabd432008-12-05 02:27:51 +00001236typedef llvm::ImmutableSet<SymbolRef> ARPoolContents;
1237typedef llvm::ImmutableList< std::pair<SymbolRef, ARPoolContents*> > ARBindings;
Ted Kremenek6d348932008-10-21 15:53:15 +00001238static int AutoRBIndex = 0;
1239
1240namespace clang {
1241 template<>
1242 struct GRStateTrait<ARBindings> : public GRStatePartialTrait<ARBindings> {
1243 static inline void* GDMIndex() { return &AutoRBIndex; }
1244 };
1245}
1246
Ted Kremenek13922612008-04-16 20:40:59 +00001247//===----------------------------------------------------------------------===//
1248// Transfer functions.
1249//===----------------------------------------------------------------------===//
1250
Ted Kremenek72cd17f2008-08-14 21:16:54 +00001251namespace {
1252
Ted Kremenek05cbe1a2008-04-09 23:49:11 +00001253class VISIBILITY_HIDDEN CFRefCount : public GRSimpleVals {
Ted Kremenek8dd56462008-04-18 03:39:05 +00001254public:
Ted Kremenek553cf182008-06-25 21:21:56 +00001255 // Type definitions.
Ted Kremenek2dabd432008-12-05 02:27:51 +00001256 typedef llvm::DenseMap<GRExprEngine::NodeTy*,std::pair<Expr*, SymbolRef> >
Ted Kremenek8dd56462008-04-18 03:39:05 +00001257 ReleasesNotOwnedTy;
Ted Kremenek72cd17f2008-08-14 21:16:54 +00001258
Ted Kremenek8dd56462008-04-18 03:39:05 +00001259 typedef ReleasesNotOwnedTy UseAfterReleasesTy;
1260
Ted Kremenekf9790ae2008-10-24 20:32:50 +00001261 typedef llvm::DenseMap<GRExprEngine::NodeTy*,
Ted Kremenek2dabd432008-12-05 02:27:51 +00001262 std::vector<std::pair<SymbolRef,bool> >*>
Ted Kremenekdb863712008-04-16 22:32:20 +00001263 LeaksTy;
Ted Kremenek8dd56462008-04-18 03:39:05 +00001264
Ted Kremenekae6814e2008-08-13 21:24:49 +00001265 class BindingsPrinter : public GRState::Printer {
Ted Kremenekf3948042008-03-11 19:44:10 +00001266 public:
Ted Kremenekae6814e2008-08-13 21:24:49 +00001267 virtual void Print(std::ostream& Out, const GRState* state,
1268 const char* nl, const char* sep);
Ted Kremenekf3948042008-03-11 19:44:10 +00001269 };
Ted Kremenek8dd56462008-04-18 03:39:05 +00001270
1271private:
Ted Kremenekd3dbcf42008-05-05 22:11:16 +00001272 RetainSummaryManager Summaries;
Ted Kremenekd3dbcf42008-05-05 22:11:16 +00001273 const LangOptions& LOpts;
Ted Kremenekb9d17f92008-08-17 03:20:02 +00001274
Ted Kremenek9e476de2008-08-12 18:30:56 +00001275 UseAfterReleasesTy UseAfterReleases;
1276 ReleasesNotOwnedTy ReleasesNotOwned;
1277 LeaksTy Leaks;
Ted Kremenek6b3a0f72008-03-11 06:39:11 +00001278
Ted Kremenek2dabd432008-12-05 02:27:51 +00001279 RefBindings Update(RefBindings B, SymbolRef sym, RefVal V, ArgEffect E,
Ted Kremenekb9d17f92008-08-17 03:20:02 +00001280 RefVal::Kind& hasErr, RefBindings::Factory& RefBFactory);
Ted Kremenek9ed18e62008-04-16 04:28:53 +00001281
Ted Kremenek2dabd432008-12-05 02:27:51 +00001282 RefVal::Kind& Update(GRStateRef& state, SymbolRef sym, RefVal V,
Ted Kremenek72cd17f2008-08-14 21:16:54 +00001283 ArgEffect E, RefVal::Kind& hasErr) {
1284
1285 state = state.set<RefBindings>(Update(state.get<RefBindings>(), sym, V,
Ted Kremenekb9d17f92008-08-17 03:20:02 +00001286 E, hasErr,
1287 state.get_context<RefBindings>()));
Ted Kremenek72cd17f2008-08-14 21:16:54 +00001288 return hasErr;
1289 }
1290
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001291 void ProcessNonLeakError(ExplodedNodeSet<GRState>& Dst,
1292 GRStmtNodeBuilder<GRState>& Builder,
Ted Kremenekdb863712008-04-16 22:32:20 +00001293 Expr* NodeExpr, Expr* ErrorExpr,
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001294 ExplodedNode<GRState>* Pred,
1295 const GRState* St,
Ted Kremenek2dabd432008-12-05 02:27:51 +00001296 RefVal::Kind hasErr, SymbolRef Sym);
Ted Kremenekdb863712008-04-16 22:32:20 +00001297
Ted Kremenekf9790ae2008-10-24 20:32:50 +00001298 std::pair<GRStateRef, bool>
1299 HandleSymbolDeath(GRStateManager& VMgr, const GRState* St,
Ted Kremenek2dabd432008-12-05 02:27:51 +00001300 const Decl* CD, SymbolRef sid, RefVal V, bool& hasLeak);
Ted Kremenekdb863712008-04-16 22:32:20 +00001301
Ted Kremenek6b3a0f72008-03-11 06:39:11 +00001302public:
Ted Kremenek13922612008-04-16 20:40:59 +00001303
Ted Kremenek78d46242008-07-22 16:21:24 +00001304 CFRefCount(ASTContext& Ctx, bool gcenabled, const LangOptions& lopts)
Ted Kremenek377e2302008-04-29 05:33:51 +00001305 : Summaries(Ctx, gcenabled),
Ted Kremenek9e476de2008-08-12 18:30:56 +00001306 LOpts(lopts) {}
Ted Kremenek9ed18e62008-04-16 04:28:53 +00001307
Ted Kremenek8dd56462008-04-18 03:39:05 +00001308 virtual ~CFRefCount() {
1309 for (LeaksTy::iterator I = Leaks.begin(), E = Leaks.end(); I!=E; ++I)
1310 delete I->second;
1311 }
Ted Kremenek05cbe1a2008-04-09 23:49:11 +00001312
1313 virtual void RegisterChecks(GRExprEngine& Eng);
Ted Kremenekf3948042008-03-11 19:44:10 +00001314
Ted Kremenek1c72ef02008-08-16 00:49:49 +00001315 virtual void RegisterPrinters(std::vector<GRState::Printer*>& Printers) {
1316 Printers.push_back(new BindingsPrinter());
Ted Kremenekf3948042008-03-11 19:44:10 +00001317 }
Ted Kremenek6b3a0f72008-03-11 06:39:11 +00001318
Ted Kremenekd3dbcf42008-05-05 22:11:16 +00001319 bool isGCEnabled() const { return Summaries.isGCEnabled(); }
Ted Kremenek072192b2008-04-30 23:47:44 +00001320 const LangOptions& getLangOptions() const { return LOpts; }
1321
Ted Kremenek6b3a0f72008-03-11 06:39:11 +00001322 // Calls.
Ted Kremenekd3dbcf42008-05-05 22:11:16 +00001323
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001324 void EvalSummary(ExplodedNodeSet<GRState>& Dst,
Ted Kremenekd3dbcf42008-05-05 22:11:16 +00001325 GRExprEngine& Eng,
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001326 GRStmtNodeBuilder<GRState>& Builder,
Ted Kremenekd3dbcf42008-05-05 22:11:16 +00001327 Expr* Ex,
1328 Expr* Receiver,
1329 RetainSummary* Summ,
Ted Kremenek55499762008-06-17 02:43:46 +00001330 ExprIterator arg_beg, ExprIterator arg_end,
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001331 ExplodedNode<GRState>* Pred);
Ted Kremenekd3dbcf42008-05-05 22:11:16 +00001332
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001333 virtual void EvalCall(ExplodedNodeSet<GRState>& Dst,
Ted Kremenek199e1a02008-03-12 21:06:49 +00001334 GRExprEngine& Eng,
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001335 GRStmtNodeBuilder<GRState>& Builder,
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001336 CallExpr* CE, SVal L,
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001337 ExplodedNode<GRState>* Pred);
Ted Kremenekfa34b332008-04-09 01:10:13 +00001338
Ted Kremenekd3dbcf42008-05-05 22:11:16 +00001339
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001340 virtual void EvalObjCMessageExpr(ExplodedNodeSet<GRState>& Dst,
Ted Kremenek85348202008-04-15 23:44:31 +00001341 GRExprEngine& Engine,
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001342 GRStmtNodeBuilder<GRState>& Builder,
Ted Kremenek85348202008-04-15 23:44:31 +00001343 ObjCMessageExpr* ME,
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001344 ExplodedNode<GRState>* Pred);
Ted Kremenek85348202008-04-15 23:44:31 +00001345
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001346 bool EvalObjCMessageExprAux(ExplodedNodeSet<GRState>& Dst,
Ted Kremenek85348202008-04-15 23:44:31 +00001347 GRExprEngine& Engine,
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001348 GRStmtNodeBuilder<GRState>& Builder,
Ted Kremenek85348202008-04-15 23:44:31 +00001349 ObjCMessageExpr* ME,
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001350 ExplodedNode<GRState>* Pred);
Ted Kremenek85348202008-04-15 23:44:31 +00001351
Ted Kremenek13922612008-04-16 20:40:59 +00001352 // Stores.
1353
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001354 virtual void EvalStore(ExplodedNodeSet<GRState>& Dst,
Ted Kremenek13922612008-04-16 20:40:59 +00001355 GRExprEngine& Engine,
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001356 GRStmtNodeBuilder<GRState>& Builder,
1357 Expr* E, ExplodedNode<GRState>* Pred,
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001358 const GRState* St, SVal TargetLV, SVal Val);
Ted Kremeneke7bd9c22008-04-11 22:25:11 +00001359 // End-of-path.
1360
1361 virtual void EvalEndPath(GRExprEngine& Engine,
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001362 GREndPathNodeBuilder<GRState>& Builder);
Ted Kremeneke7bd9c22008-04-11 22:25:11 +00001363
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001364 virtual void EvalDeadSymbols(ExplodedNodeSet<GRState>& Dst,
Ted Kremenek652adc62008-04-24 23:57:27 +00001365 GRExprEngine& Engine,
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001366 GRStmtNodeBuilder<GRState>& Builder,
1367 ExplodedNode<GRState>* Pred,
Ted Kremenek910e9992008-04-25 01:25:15 +00001368 Stmt* S,
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001369 const GRState* St,
1370 const GRStateManager::DeadSymbolsTy& Dead);
Ted Kremenek4fd88972008-04-17 18:12:53 +00001371 // Return statements.
1372
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001373 virtual void EvalReturn(ExplodedNodeSet<GRState>& Dst,
Ted Kremenek4fd88972008-04-17 18:12:53 +00001374 GRExprEngine& Engine,
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001375 GRStmtNodeBuilder<GRState>& Builder,
Ted Kremenek4fd88972008-04-17 18:12:53 +00001376 ReturnStmt* S,
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001377 ExplodedNode<GRState>* Pred);
Ted Kremenekcb612922008-04-18 19:23:43 +00001378
1379 // Assumptions.
1380
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001381 virtual const GRState* EvalAssume(GRStateManager& VMgr,
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001382 const GRState* St, SVal Cond,
Ted Kremenek4323a572008-07-10 22:03:41 +00001383 bool Assumption, bool& isFeasible);
Ted Kremenekcb612922008-04-18 19:23:43 +00001384
Ted Kremenekfa34b332008-04-09 01:10:13 +00001385 // Error iterators.
1386
1387 typedef UseAfterReleasesTy::iterator use_after_iterator;
1388 typedef ReleasesNotOwnedTy::iterator bad_release_iterator;
Ted Kremenek989d5192008-04-17 23:43:50 +00001389 typedef LeaksTy::iterator leaks_iterator;
Ted Kremenekfa34b332008-04-09 01:10:13 +00001390
Ted Kremenek05cbe1a2008-04-09 23:49:11 +00001391 use_after_iterator use_after_begin() { return UseAfterReleases.begin(); }
1392 use_after_iterator use_after_end() { return UseAfterReleases.end(); }
Ted Kremenekfa34b332008-04-09 01:10:13 +00001393
Ted Kremenek05cbe1a2008-04-09 23:49:11 +00001394 bad_release_iterator bad_release_begin() { return ReleasesNotOwned.begin(); }
1395 bad_release_iterator bad_release_end() { return ReleasesNotOwned.end(); }
Ted Kremenek989d5192008-04-17 23:43:50 +00001396
1397 leaks_iterator leaks_begin() { return Leaks.begin(); }
1398 leaks_iterator leaks_end() { return Leaks.end(); }
Ted Kremenek6b3a0f72008-03-11 06:39:11 +00001399};
1400
1401} // end anonymous namespace
1402
Ted Kremenek8dd56462008-04-18 03:39:05 +00001403
Ted Kremenek05cbe1a2008-04-09 23:49:11 +00001404
1405
Ted Kremenekae6814e2008-08-13 21:24:49 +00001406void CFRefCount::BindingsPrinter::Print(std::ostream& Out, const GRState* state,
1407 const char* nl, const char* sep) {
1408
Ted Kremenek72cd17f2008-08-14 21:16:54 +00001409 RefBindings B = state->get<RefBindings>();
Ted Kremenekf3948042008-03-11 19:44:10 +00001410
Ted Kremenekae6814e2008-08-13 21:24:49 +00001411 if (!B.isEmpty())
Ted Kremenekf3948042008-03-11 19:44:10 +00001412 Out << sep << nl;
1413
1414 for (RefBindings::iterator I=B.begin(), E=B.end(); I!=E; ++I) {
1415 Out << (*I).first << " : ";
1416 (*I).second.print(Out);
1417 Out << nl;
1418 }
1419}
1420
Ted Kremenekd3dbcf42008-05-05 22:11:16 +00001421static inline ArgEffect GetArgE(RetainSummary* Summ, unsigned idx) {
Ted Kremenek3eabf1c2008-05-22 17:31:13 +00001422 return Summ ? Summ->getArg(idx) : MayEscape;
Ted Kremenekf9561e52008-04-11 20:23:24 +00001423}
1424
Ted Kremenek3c0cea32008-05-06 02:26:56 +00001425static inline RetEffect GetRetEffect(RetainSummary* Summ) {
1426 return Summ ? Summ->getRetEffect() : RetEffect::MakeNoRet();
Ted Kremenekf9561e52008-04-11 20:23:24 +00001427}
1428
Ted Kremenek14993892008-05-06 02:41:27 +00001429static inline ArgEffect GetReceiverE(RetainSummary* Summ) {
1430 return Summ ? Summ->getReceiverEffect() : DoNothing;
1431}
1432
Ted Kremenek70a733e2008-07-18 17:24:20 +00001433static inline bool IsEndPath(RetainSummary* Summ) {
1434 return Summ ? Summ->isEndPath() : false;
1435}
1436
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001437void CFRefCount::ProcessNonLeakError(ExplodedNodeSet<GRState>& Dst,
1438 GRStmtNodeBuilder<GRState>& Builder,
Ted Kremenekdb863712008-04-16 22:32:20 +00001439 Expr* NodeExpr, Expr* ErrorExpr,
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001440 ExplodedNode<GRState>* Pred,
1441 const GRState* St,
Ted Kremenek2dabd432008-12-05 02:27:51 +00001442 RefVal::Kind hasErr, SymbolRef Sym) {
Ted Kremenek9ed18e62008-04-16 04:28:53 +00001443 Builder.BuildSinks = true;
1444 GRExprEngine::NodeTy* N = Builder.MakeNode(Dst, NodeExpr, Pred, St);
1445
1446 if (!N) return;
1447
1448 switch (hasErr) {
1449 default: assert(false);
1450 case RefVal::ErrorUseAfterRelease:
Ted Kremenek8dd56462008-04-18 03:39:05 +00001451 UseAfterReleases[N] = std::make_pair(ErrorExpr, Sym);
Ted Kremenek9ed18e62008-04-16 04:28:53 +00001452 break;
1453
1454 case RefVal::ErrorReleaseNotOwned:
Ted Kremenek8dd56462008-04-18 03:39:05 +00001455 ReleasesNotOwned[N] = std::make_pair(ErrorExpr, Sym);
Ted Kremenek9ed18e62008-04-16 04:28:53 +00001456 break;
1457 }
1458}
1459
Ted Kremenek553cf182008-06-25 21:21:56 +00001460/// GetReturnType - Used to get the return type of a message expression or
1461/// function call with the intention of affixing that type to a tracked symbol.
1462/// While the the return type can be queried directly from RetEx, when
1463/// invoking class methods we augment to the return type to be that of
1464/// a pointer to the class (as opposed it just being id).
1465static QualType GetReturnType(Expr* RetE, ASTContext& Ctx) {
1466
1467 QualType RetTy = RetE->getType();
1468
1469 // FIXME: We aren't handling id<...>.
Chris Lattner8b51fd72008-07-26 22:36:27 +00001470 const PointerType* PT = RetTy->getAsPointerType();
Ted Kremenek553cf182008-06-25 21:21:56 +00001471 if (!PT)
1472 return RetTy;
1473
1474 // If RetEx is not a message expression just return its type.
1475 // If RetEx is a message expression, return its types if it is something
1476 /// more specific than id.
1477
1478 ObjCMessageExpr* ME = dyn_cast<ObjCMessageExpr>(RetE);
1479
1480 if (!ME || !Ctx.isObjCIdType(PT->getPointeeType()))
1481 return RetTy;
1482
1483 ObjCInterfaceDecl* D = ME->getClassInfo().first;
1484
1485 // At this point we know the return type of the message expression is id.
1486 // If we have an ObjCInterceDecl, we know this is a call to a class method
1487 // whose type we can resolve. In such cases, promote the return type to
1488 // Class*.
1489 return !D ? RetTy : Ctx.getPointerType(Ctx.getObjCInterfaceType(D));
1490}
1491
1492
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001493void CFRefCount::EvalSummary(ExplodedNodeSet<GRState>& Dst,
Ted Kremenekd3dbcf42008-05-05 22:11:16 +00001494 GRExprEngine& Eng,
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001495 GRStmtNodeBuilder<GRState>& Builder,
Ted Kremenekd3dbcf42008-05-05 22:11:16 +00001496 Expr* Ex,
1497 Expr* Receiver,
1498 RetainSummary* Summ,
Ted Kremenek55499762008-06-17 02:43:46 +00001499 ExprIterator arg_beg, ExprIterator arg_end,
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001500 ExplodedNode<GRState>* Pred) {
Ted Kremenek00a3a5f2008-03-12 01:21:45 +00001501
Ted Kremenek6b3a0f72008-03-11 06:39:11 +00001502 // Get the state.
Ted Kremenek72cd17f2008-08-14 21:16:54 +00001503 GRStateRef state(Builder.GetState(Pred), Eng.getStateManager());
Ted Kremenekf9790ae2008-10-24 20:32:50 +00001504 ASTContext& Ctx = Eng.getStateManager().getContext();
Ted Kremenek14993892008-05-06 02:41:27 +00001505
1506 // Evaluate the effect of the arguments.
Ted Kremenek9ed18e62008-04-16 04:28:53 +00001507 RefVal::Kind hasErr = (RefVal::Kind) 0;
Ted Kremenek00a3a5f2008-03-12 01:21:45 +00001508 unsigned idx = 0;
Ted Kremenekbcf50ad2008-04-11 18:40:51 +00001509 Expr* ErrorExpr = NULL;
Ted Kremenek2dabd432008-12-05 02:27:51 +00001510 SymbolRef ErrorSym = 0;
Ted Kremenekbcf50ad2008-04-11 18:40:51 +00001511
Ted Kremenek72cd17f2008-08-14 21:16:54 +00001512 for (ExprIterator I = arg_beg; I != arg_end; ++I, ++idx) {
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001513 SVal V = state.GetSVal(*I);
Ted Kremenek6b3a0f72008-03-11 06:39:11 +00001514
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001515 if (isa<loc::SymbolVal>(V)) {
Ted Kremenek2dabd432008-12-05 02:27:51 +00001516 SymbolRef Sym = cast<loc::SymbolVal>(V).getSymbol();
Ted Kremenek72cd17f2008-08-14 21:16:54 +00001517 if (RefBindings::data_type* T = state.get<RefBindings>(Sym))
1518 if (Update(state, Sym, *T, GetArgE(Summ, idx), hasErr)) {
Ted Kremenekbcf50ad2008-04-11 18:40:51 +00001519 ErrorExpr = *I;
Ted Kremeneke8fdc832008-07-07 16:21:19 +00001520 ErrorSym = Sym;
Ted Kremenekbcf50ad2008-04-11 18:40:51 +00001521 break;
1522 }
Ted Kremenekb8873552008-04-11 20:51:02 +00001523 }
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001524 else if (isa<Loc>(V)) {
Ted Kremenek8c5633e2008-07-03 23:26:32 +00001525#if 0
Ted Kremenekd3dbcf42008-05-05 22:11:16 +00001526 // Nuke all arguments passed by reference.
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001527 StateMgr.Unbind(StVals, cast<Loc>(V));
Ted Kremenek8c5633e2008-07-03 23:26:32 +00001528#else
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001529 if (loc::MemRegionVal* MR = dyn_cast<loc::MemRegionVal>(&V)) {
Ted Kremenek070a8252008-07-09 18:11:16 +00001530
1531 if (GetArgE(Summ, idx) == DoNothingByRef)
1532 continue;
1533
1534 // Invalidate the value of the variable passed by reference.
Ted Kremenek8c5633e2008-07-03 23:26:32 +00001535
1536 // FIXME: Either this logic should also be replicated in GRSimpleVals
1537 // or should be pulled into a separate "constraint engine."
Ted Kremenek070a8252008-07-09 18:11:16 +00001538
Ted Kremenek8c5633e2008-07-03 23:26:32 +00001539 // FIXME: We can have collisions on the conjured symbol if the
1540 // expression *I also creates conjured symbols. We probably want
1541 // to identify conjured symbols by an expression pair: the enclosing
1542 // expression (the context) and the expression itself. This should
Ted Kremenek070a8252008-07-09 18:11:16 +00001543 // disambiguate conjured symbols.
Ted Kremenek9e240492008-10-04 05:50:14 +00001544
Ted Kremenek993f1c72008-10-17 20:28:54 +00001545 const TypedRegion* R = dyn_cast<TypedRegion>(MR->getRegion());
Ted Kremenek90b32362008-12-17 19:42:34 +00001546
1547 // Blast through AnonTypedRegions to get the original region type.
1548 while (R) {
1549 const AnonTypedRegion* ATR = dyn_cast<AnonTypedRegion>(R);
1550 if (!ATR) break;
1551 R = dyn_cast<TypedRegion>(ATR->getSuperRegion());
1552 }
1553
Ted Kremenek9e240492008-10-04 05:50:14 +00001554 if (R) {
Ted Kremenek40e86d92008-12-18 23:34:57 +00001555
1556 // Is the invalidated variable something that we were tracking?
1557 SVal X = state.GetSVal(Loc::MakeVal(R));
1558
1559 if (isa<loc::SymbolVal>(X)) {
1560 SymbolRef Sym = cast<loc::SymbolVal>(X).getSymbol();
1561 state = state.remove<RefBindings>(Sym);
1562 }
1563
Ted Kremenek9e240492008-10-04 05:50:14 +00001564 // Set the value of the variable to be a conjured symbol.
1565 unsigned Count = Builder.getCurrentBlockCount();
Ted Kremenek6eddeb12008-12-13 21:49:13 +00001566 QualType T = R->getRValueType(Ctx);
Ted Kremenek9e240492008-10-04 05:50:14 +00001567
Ted Kremenekfd301942008-10-17 22:23:12 +00001568 // FIXME: handle structs.
Ted Kremenek062e2f92008-11-13 06:10:40 +00001569 if (Loc::IsLocType(T) || (T->isIntegerType() && T->isScalarType())) {
Ted Kremenek2dabd432008-12-05 02:27:51 +00001570 SymbolRef NewSym =
Ted Kremenekfd301942008-10-17 22:23:12 +00001571 Eng.getSymbolManager().getConjuredSymbol(*I, T, Count);
1572
Ted Kremenek90b32362008-12-17 19:42:34 +00001573 state = state.BindLoc(Loc::MakeVal(R),
Ted Kremenekfd301942008-10-17 22:23:12 +00001574 Loc::IsLocType(T)
1575 ? cast<SVal>(loc::SymbolVal(NewSym))
1576 : cast<SVal>(nonloc::SymbolVal(NewSym)));
1577 }
1578 else {
Ted Kremeneka441b7e2008-11-12 19:22:09 +00001579 state = state.BindLoc(*MR, UnknownVal());
Ted Kremenekfd301942008-10-17 22:23:12 +00001580 }
Ted Kremenek9e240492008-10-04 05:50:14 +00001581 }
1582 else
Ted Kremeneka441b7e2008-11-12 19:22:09 +00001583 state = state.BindLoc(*MR, UnknownVal());
Ted Kremenek8c5633e2008-07-03 23:26:32 +00001584 }
1585 else {
1586 // Nuke all other arguments passed by reference.
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001587 state = state.Unbind(cast<Loc>(V));
Ted Kremenek8c5633e2008-07-03 23:26:32 +00001588 }
1589#endif
Ted Kremenekb8873552008-04-11 20:51:02 +00001590 }
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001591 else if (isa<nonloc::LocAsInteger>(V))
1592 state = state.Unbind(cast<nonloc::LocAsInteger>(V).getLoc());
Ted Kremenekd3dbcf42008-05-05 22:11:16 +00001593 }
Ted Kremenek9ed18e62008-04-16 04:28:53 +00001594
Ted Kremenek553cf182008-06-25 21:21:56 +00001595 // Evaluate the effect on the message receiver.
Ted Kremenek14993892008-05-06 02:41:27 +00001596 if (!ErrorExpr && Receiver) {
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001597 SVal V = state.GetSVal(Receiver);
1598 if (isa<loc::SymbolVal>(V)) {
Ted Kremenek2dabd432008-12-05 02:27:51 +00001599 SymbolRef Sym = cast<loc::SymbolVal>(V).getSymbol();
Ted Kremenek72cd17f2008-08-14 21:16:54 +00001600 if (const RefVal* T = state.get<RefBindings>(Sym))
1601 if (Update(state, Sym, *T, GetReceiverE(Summ), hasErr)) {
Ted Kremenek14993892008-05-06 02:41:27 +00001602 ErrorExpr = Receiver;
Ted Kremeneke8fdc832008-07-07 16:21:19 +00001603 ErrorSym = Sym;
Ted Kremenek14993892008-05-06 02:41:27 +00001604 }
Ted Kremenek14993892008-05-06 02:41:27 +00001605 }
1606 }
Ted Kremenekd3dbcf42008-05-05 22:11:16 +00001607
Ted Kremenek553cf182008-06-25 21:21:56 +00001608 // Process any errors.
Ted Kremenek9ed18e62008-04-16 04:28:53 +00001609 if (hasErr) {
Ted Kremenek72cd17f2008-08-14 21:16:54 +00001610 ProcessNonLeakError(Dst, Builder, Ex, ErrorExpr, Pred, state,
Ted Kremenek8dd56462008-04-18 03:39:05 +00001611 hasErr, ErrorSym);
Ted Kremenek00a3a5f2008-03-12 01:21:45 +00001612 return;
Ted Kremenek1ac08d62008-03-11 17:48:22 +00001613 }
Ted Kremenekd3dbcf42008-05-05 22:11:16 +00001614
Ted Kremenek70a733e2008-07-18 17:24:20 +00001615 // Consult the summary for the return value.
Ted Kremenek3c0cea32008-05-06 02:26:56 +00001616 RetEffect RE = GetRetEffect(Summ);
Ted Kremenek00a3a5f2008-03-12 01:21:45 +00001617
1618 switch (RE.getKind()) {
1619 default:
1620 assert (false && "Unhandled RetEffect."); break;
Ted Kremenekd3dbcf42008-05-05 22:11:16 +00001621
Ted Kremenekfd301942008-10-17 22:23:12 +00001622 case RetEffect::NoRet: {
Ted Kremenekd3dbcf42008-05-05 22:11:16 +00001623
Ted Kremenekf9561e52008-04-11 20:23:24 +00001624 // Make up a symbol for the return value (not reference counted).
Ted Kremenekb8873552008-04-11 20:51:02 +00001625 // FIXME: This is basically copy-and-paste from GRSimpleVals. We
1626 // should compose behavior, not copy it.
Ted Kremenekf9561e52008-04-11 20:23:24 +00001627
Ted Kremenekfd301942008-10-17 22:23:12 +00001628 // FIXME: We eventually should handle structs and other compound types
1629 // that are returned by value.
1630
1631 QualType T = Ex->getType();
1632
Ted Kremenek062e2f92008-11-13 06:10:40 +00001633 if (Loc::IsLocType(T) || (T->isIntegerType() && T->isScalarType())) {
Ted Kremenekf9561e52008-04-11 20:23:24 +00001634 unsigned Count = Builder.getCurrentBlockCount();
Ted Kremenek2dabd432008-12-05 02:27:51 +00001635 SymbolRef Sym = Eng.getSymbolManager().getConjuredSymbol(Ex, Count);
Ted Kremenekf9561e52008-04-11 20:23:24 +00001636
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001637 SVal X = Loc::IsLocType(Ex->getType())
1638 ? cast<SVal>(loc::SymbolVal(Sym))
1639 : cast<SVal>(nonloc::SymbolVal(Sym));
Ted Kremenekf9561e52008-04-11 20:23:24 +00001640
Ted Kremeneka441b7e2008-11-12 19:22:09 +00001641 state = state.BindExpr(Ex, X, false);
Ted Kremenekf9561e52008-04-11 20:23:24 +00001642 }
1643
Ted Kremenek940b1d82008-04-10 23:44:06 +00001644 break;
Ted Kremenekfd301942008-10-17 22:23:12 +00001645 }
Ted Kremenek940b1d82008-04-10 23:44:06 +00001646
Ted Kremenek00a3a5f2008-03-12 01:21:45 +00001647 case RetEffect::Alias: {
Ted Kremenek553cf182008-06-25 21:21:56 +00001648 unsigned idx = RE.getIndex();
Ted Kremenek55499762008-06-17 02:43:46 +00001649 assert (arg_end >= arg_beg);
Ted Kremenekd3dbcf42008-05-05 22:11:16 +00001650 assert (idx < (unsigned) (arg_end - arg_beg));
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001651 SVal V = state.GetSVal(*(arg_beg+idx));
Ted Kremeneka441b7e2008-11-12 19:22:09 +00001652 state = state.BindExpr(Ex, V, false);
Ted Kremenek00a3a5f2008-03-12 01:21:45 +00001653 break;
1654 }
1655
Ted Kremenek14993892008-05-06 02:41:27 +00001656 case RetEffect::ReceiverAlias: {
1657 assert (Receiver);
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001658 SVal V = state.GetSVal(Receiver);
Ted Kremeneka441b7e2008-11-12 19:22:09 +00001659 state = state.BindExpr(Ex, V, false);
Ted Kremenek14993892008-05-06 02:41:27 +00001660 break;
1661 }
1662
Ted Kremeneka7344702008-06-23 18:02:52 +00001663 case RetEffect::OwnedAllocatedSymbol:
Ted Kremenek00a3a5f2008-03-12 01:21:45 +00001664 case RetEffect::OwnedSymbol: {
1665 unsigned Count = Builder.getCurrentBlockCount();
Ted Kremenek2dabd432008-12-05 02:27:51 +00001666 SymbolRef Sym = Eng.getSymbolManager().getConjuredSymbol(Ex, Count);
Ted Kremenek553cf182008-06-25 21:21:56 +00001667 QualType RetT = GetReturnType(Ex, Eng.getContext());
Ted Kremenekd3dbcf42008-05-05 22:11:16 +00001668
Ted Kremenekb9d17f92008-08-17 03:20:02 +00001669 state = state.set<RefBindings>(Sym, RefVal::makeOwned(RetT));
Ted Kremeneka441b7e2008-11-12 19:22:09 +00001670 state = state.BindExpr(Ex, loc::SymbolVal(Sym), false);
Ted Kremenek72cd17f2008-08-14 21:16:54 +00001671
1672#if 0
Ted Kremenek00a3a5f2008-03-12 01:21:45 +00001673 RefBindings B = GetRefBindings(StImpl);
Ted Kremenek553cf182008-06-25 21:21:56 +00001674 SetRefBindings(StImpl, RefBFactory.Add(B, Sym, RefVal::makeOwned(RetT)));
Ted Kremenek72cd17f2008-08-14 21:16:54 +00001675#endif
1676
Ted Kremeneka7344702008-06-23 18:02:52 +00001677 // FIXME: Add a flag to the checker where allocations are allowed to fail.
1678 if (RE.getKind() == RetEffect::OwnedAllocatedSymbol)
Ted Kremenek72cd17f2008-08-14 21:16:54 +00001679 state = state.AddNE(Sym, Eng.getBasicVals().getZeroWithPtrWidth());
Ted Kremeneka7344702008-06-23 18:02:52 +00001680
Ted Kremenek00a3a5f2008-03-12 01:21:45 +00001681 break;
1682 }
1683
1684 case RetEffect::NotOwnedSymbol: {
1685 unsigned Count = Builder.getCurrentBlockCount();
Ted Kremenek2dabd432008-12-05 02:27:51 +00001686 SymbolRef Sym = Eng.getSymbolManager().getConjuredSymbol(Ex, Count);
Ted Kremenek553cf182008-06-25 21:21:56 +00001687 QualType RetT = GetReturnType(Ex, Eng.getContext());
Ted Kremenek00a3a5f2008-03-12 01:21:45 +00001688
Ted Kremenekb9d17f92008-08-17 03:20:02 +00001689 state = state.set<RefBindings>(Sym, RefVal::makeNotOwned(RetT));
Ted Kremeneka441b7e2008-11-12 19:22:09 +00001690 state = state.BindExpr(Ex, loc::SymbolVal(Sym), false);
Ted Kremenek00a3a5f2008-03-12 01:21:45 +00001691 break;
1692 }
1693 }
Ted Kremenekd3dbcf42008-05-05 22:11:16 +00001694
Ted Kremenek70a733e2008-07-18 17:24:20 +00001695 // Is this a sink?
1696 if (IsEndPath(Summ))
Ted Kremenek72cd17f2008-08-14 21:16:54 +00001697 Builder.MakeSinkNode(Dst, Ex, Pred, state);
Ted Kremenek70a733e2008-07-18 17:24:20 +00001698 else
Ted Kremenek72cd17f2008-08-14 21:16:54 +00001699 Builder.MakeNode(Dst, Ex, Pred, state);
Ted Kremenekd3dbcf42008-05-05 22:11:16 +00001700}
1701
1702
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001703void CFRefCount::EvalCall(ExplodedNodeSet<GRState>& Dst,
Ted Kremenekd3dbcf42008-05-05 22:11:16 +00001704 GRExprEngine& Eng,
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001705 GRStmtNodeBuilder<GRState>& Builder,
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001706 CallExpr* CE, SVal L,
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001707 ExplodedNode<GRState>* Pred) {
Ted Kremenekd3dbcf42008-05-05 22:11:16 +00001708
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001709 RetainSummary* Summ = !isa<loc::FuncVal>(L) ? 0
1710 : Summaries.getSummary(cast<loc::FuncVal>(L).getDecl());
Ted Kremenekd3dbcf42008-05-05 22:11:16 +00001711
1712 EvalSummary(Dst, Eng, Builder, CE, 0, Summ,
1713 CE->arg_begin(), CE->arg_end(), Pred);
Ted Kremenek2fff37e2008-03-06 00:08:09 +00001714}
Ted Kremenek6b3a0f72008-03-11 06:39:11 +00001715
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001716void CFRefCount::EvalObjCMessageExpr(ExplodedNodeSet<GRState>& Dst,
Ted Kremenek85348202008-04-15 23:44:31 +00001717 GRExprEngine& Eng,
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001718 GRStmtNodeBuilder<GRState>& Builder,
Ted Kremenek85348202008-04-15 23:44:31 +00001719 ObjCMessageExpr* ME,
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001720 ExplodedNode<GRState>* Pred) {
Ted Kremenekb3095252008-05-06 04:20:12 +00001721 RetainSummary* Summ;
Ted Kremenek9040c652008-05-01 21:31:50 +00001722
Ted Kremenek553cf182008-06-25 21:21:56 +00001723 if (Expr* Receiver = ME->getReceiver()) {
1724 // We need the type-information of the tracked receiver object
1725 // Retrieve it from the state.
1726 ObjCInterfaceDecl* ID = 0;
1727
1728 // FIXME: Wouldn't it be great if this code could be reduced? It's just
1729 // a chain of lookups.
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001730 const GRState* St = Builder.GetState(Pred);
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001731 SVal V = Eng.getStateManager().GetSVal(St, Receiver );
Ted Kremenek553cf182008-06-25 21:21:56 +00001732
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001733 if (isa<loc::SymbolVal>(V)) {
Ted Kremenek2dabd432008-12-05 02:27:51 +00001734 SymbolRef Sym = cast<loc::SymbolVal>(V).getSymbol();
Ted Kremenek553cf182008-06-25 21:21:56 +00001735
Ted Kremenek72cd17f2008-08-14 21:16:54 +00001736 if (const RefVal* T = St->get<RefBindings>(Sym)) {
Ted Kremeneke8fdc832008-07-07 16:21:19 +00001737 QualType Ty = T->getType();
Ted Kremenek553cf182008-06-25 21:21:56 +00001738
1739 if (const PointerType* PT = Ty->getAsPointerType()) {
1740 QualType PointeeTy = PT->getPointeeType();
1741
1742 if (ObjCInterfaceType* IT = dyn_cast<ObjCInterfaceType>(PointeeTy))
1743 ID = IT->getDecl();
1744 }
1745 }
1746 }
1747
1748 Summ = Summaries.getMethodSummary(ME, ID);
Ted Kremenekf9790ae2008-10-24 20:32:50 +00001749
Ted Kremenek896cd9d2008-10-23 01:56:15 +00001750 // Special-case: are we sending a mesage to "self"?
1751 // This is a hack. When we have full-IP this should be removed.
1752 if (!Summ) {
1753 ObjCMethodDecl* MD =
1754 dyn_cast<ObjCMethodDecl>(&Eng.getGraph().getCodeDecl());
1755
1756 if (MD) {
1757 if (Expr* Receiver = ME->getReceiver()) {
1758 SVal X = Eng.getStateManager().GetSVal(St, Receiver);
1759 if (loc::MemRegionVal* L = dyn_cast<loc::MemRegionVal>(&X))
Ted Kremenekf9790ae2008-10-24 20:32:50 +00001760 if (L->getRegion() == Eng.getStateManager().getSelfRegion(St)) {
1761 // Create a summmary where all of the arguments "StopTracking".
1762 Summ = Summaries.getPersistentSummary(RetEffect::MakeNoRet(),
1763 DoNothing,
1764 StopTracking);
1765 }
Ted Kremenek896cd9d2008-10-23 01:56:15 +00001766 }
1767 }
1768 }
Ted Kremenek553cf182008-06-25 21:21:56 +00001769 }
Ted Kremenek9ed18e62008-04-16 04:28:53 +00001770 else
Ted Kremenek1f180c32008-06-23 22:21:20 +00001771 Summ = Summaries.getClassMethodSummary(ME->getClassName(),
1772 ME->getSelector());
Ted Kremenek9ed18e62008-04-16 04:28:53 +00001773
Ted Kremenekb3095252008-05-06 04:20:12 +00001774 EvalSummary(Dst, Eng, Builder, ME, ME->getReceiver(), Summ,
1775 ME->arg_begin(), ME->arg_end(), Pred);
Ted Kremenek85348202008-04-15 23:44:31 +00001776}
Ted Kremenekb3095252008-05-06 04:20:12 +00001777
Ted Kremenek13922612008-04-16 20:40:59 +00001778// Stores.
1779
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001780void CFRefCount::EvalStore(ExplodedNodeSet<GRState>& Dst,
Ted Kremenek13922612008-04-16 20:40:59 +00001781 GRExprEngine& Eng,
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001782 GRStmtNodeBuilder<GRState>& Builder,
1783 Expr* E, ExplodedNode<GRState>* Pred,
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001784 const GRState* St, SVal TargetLV, SVal Val) {
Ted Kremenek13922612008-04-16 20:40:59 +00001785
1786 // Check if we have a binding for "Val" and if we are storing it to something
1787 // we don't understand or otherwise the value "escapes" the function.
1788
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001789 if (!isa<loc::SymbolVal>(Val))
Ted Kremenek13922612008-04-16 20:40:59 +00001790 return;
1791
1792 // Are we storing to something that causes the value to "escape"?
1793
1794 bool escapes = false;
1795
Ted Kremeneka496d162008-10-18 03:49:51 +00001796 // A value escapes in three possible cases (this may change):
1797 //
1798 // (1) we are binding to something that is not a memory region.
1799 // (2) we are binding to a memregion that does not have stack storage
1800 // (3) we are binding to a memregion with stack storage that the store
1801 // does not understand.
1802
Ted Kremenek2dabd432008-12-05 02:27:51 +00001803 SymbolRef Sym = cast<loc::SymbolVal>(Val).getSymbol();
Ted Kremeneka496d162008-10-18 03:49:51 +00001804 GRStateRef state(St, Eng.getStateManager());
1805
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001806 if (!isa<loc::MemRegionVal>(TargetLV))
Ted Kremenek13922612008-04-16 20:40:59 +00001807 escapes = true;
Ted Kremenek9e240492008-10-04 05:50:14 +00001808 else {
Ted Kremenek993f1c72008-10-17 20:28:54 +00001809 const MemRegion* R = cast<loc::MemRegionVal>(TargetLV).getRegion();
Ted Kremenek9e240492008-10-04 05:50:14 +00001810 escapes = !Eng.getStateManager().hasStackStorage(R);
Ted Kremeneka496d162008-10-18 03:49:51 +00001811
1812 if (!escapes) {
1813 // To test (3), generate a new state with the binding removed. If it is
1814 // the same state, then it escapes (since the store cannot represent
1815 // the binding).
Ted Kremeneka441b7e2008-11-12 19:22:09 +00001816 GRStateRef stateNew = state.BindLoc(cast<Loc>(TargetLV), Val);
Ted Kremeneka496d162008-10-18 03:49:51 +00001817 escapes = (stateNew == state);
1818 }
Ted Kremenek9e240492008-10-04 05:50:14 +00001819 }
Ted Kremenek13922612008-04-16 20:40:59 +00001820
1821 if (!escapes)
1822 return;
Ted Kremeneka496d162008-10-18 03:49:51 +00001823
1824 // Do we have a reference count binding?
1825 // FIXME: Is this step even needed? We do blow away the binding anyway.
Ted Kremenek72cd17f2008-08-14 21:16:54 +00001826 if (!state.get<RefBindings>(Sym))
Ted Kremenek13922612008-04-16 20:40:59 +00001827 return;
1828
Ted Kremenek72cd17f2008-08-14 21:16:54 +00001829 // Nuke the binding.
Ted Kremenekb9d17f92008-08-17 03:20:02 +00001830 state = state.remove<RefBindings>(Sym);
Ted Kremenek72cd17f2008-08-14 21:16:54 +00001831
Ted Kremenek13922612008-04-16 20:40:59 +00001832 // Hand of the remaining logic to the parent implementation.
Ted Kremenek72cd17f2008-08-14 21:16:54 +00001833 GRSimpleVals::EvalStore(Dst, Eng, Builder, E, Pred, state, TargetLV, Val);
Ted Kremenekdb863712008-04-16 22:32:20 +00001834}
1835
Ted Kremeneke7bd9c22008-04-11 22:25:11 +00001836// End-of-path.
1837
Ted Kremenek3ad2cc82008-10-22 23:56:21 +00001838
Ted Kremenekf9790ae2008-10-24 20:32:50 +00001839std::pair<GRStateRef,bool>
1840CFRefCount::HandleSymbolDeath(GRStateManager& VMgr,
1841 const GRState* St, const Decl* CD,
Ted Kremenek2dabd432008-12-05 02:27:51 +00001842 SymbolRef sid,
Ted Kremenekf9790ae2008-10-24 20:32:50 +00001843 RefVal V, bool& hasLeak) {
Ted Kremenekdb863712008-04-16 22:32:20 +00001844
Ted Kremenek72cd17f2008-08-14 21:16:54 +00001845 GRStateRef state(St, VMgr);
Sanjiv Gupta31fc07d2008-10-31 09:52:39 +00001846 assert ((!V.isReturnedOwned() || CD) &&
Ted Kremenek3ad2cc82008-10-22 23:56:21 +00001847 "CodeDecl must be available for reporting ReturnOwned errors.");
Ted Kremenek896cd9d2008-10-23 01:56:15 +00001848
Ted Kremenek3ad2cc82008-10-22 23:56:21 +00001849 if (V.isReturnedOwned() && V.getCount() == 0)
1850 if (const ObjCMethodDecl* MD = dyn_cast<ObjCMethodDecl>(CD)) {
Chris Lattner077bf5e2008-11-24 03:33:13 +00001851 std::string s = MD->getSelector().getAsString();
Ted Kremenek4c79e552008-11-05 16:54:44 +00001852 if (!followsReturnRule(s.c_str())) {
Ted Kremenek3ad2cc82008-10-22 23:56:21 +00001853 hasLeak = true;
Ted Kremenekf9790ae2008-10-24 20:32:50 +00001854 state = state.set<RefBindings>(sid, V ^ RefVal::ErrorLeakReturned);
1855 return std::make_pair(state, true);
Ted Kremenek3ad2cc82008-10-22 23:56:21 +00001856 }
1857 }
Ted Kremenek896cd9d2008-10-23 01:56:15 +00001858
Ted Kremenek3ad2cc82008-10-22 23:56:21 +00001859 // All other cases.
1860
1861 hasLeak = V.isOwned() ||
1862 ((V.isNotOwned() || V.isReturnedOwned()) && V.getCount() > 0);
Ted Kremenek72cd17f2008-08-14 21:16:54 +00001863
Ted Kremenekdb863712008-04-16 22:32:20 +00001864 if (!hasLeak)
Ted Kremenekf9790ae2008-10-24 20:32:50 +00001865 return std::make_pair(state.remove<RefBindings>(sid), false);
Ted Kremenekdb863712008-04-16 22:32:20 +00001866
Ted Kremenekf9790ae2008-10-24 20:32:50 +00001867 return std::make_pair(state.set<RefBindings>(sid, V ^ RefVal::ErrorLeak),
1868 false);
Ted Kremenekdb863712008-04-16 22:32:20 +00001869}
1870
1871void CFRefCount::EvalEndPath(GRExprEngine& Eng,
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001872 GREndPathNodeBuilder<GRState>& Builder) {
Ted Kremeneke7bd9c22008-04-11 22:25:11 +00001873
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001874 const GRState* St = Builder.getState();
Ted Kremenek72cd17f2008-08-14 21:16:54 +00001875 RefBindings B = St->get<RefBindings>();
Ted Kremeneke7bd9c22008-04-11 22:25:11 +00001876
Ted Kremenek2dabd432008-12-05 02:27:51 +00001877 llvm::SmallVector<std::pair<SymbolRef, bool>, 10> Leaked;
Ted Kremenek3ad2cc82008-10-22 23:56:21 +00001878 const Decl* CodeDecl = &Eng.getGraph().getCodeDecl();
Ted Kremeneke7bd9c22008-04-11 22:25:11 +00001879
Ted Kremenekdb863712008-04-16 22:32:20 +00001880 for (RefBindings::iterator I = B.begin(), E = B.end(); I != E; ++I) {
1881 bool hasLeak = false;
Ted Kremeneke7bd9c22008-04-11 22:25:11 +00001882
Ted Kremenekf9790ae2008-10-24 20:32:50 +00001883 std::pair<GRStateRef, bool> X =
1884 HandleSymbolDeath(Eng.getStateManager(), St, CodeDecl,
1885 (*I).first, (*I).second, hasLeak);
Ted Kremenekdb863712008-04-16 22:32:20 +00001886
Ted Kremenekf9790ae2008-10-24 20:32:50 +00001887 St = X.first;
1888 if (hasLeak) Leaked.push_back(std::make_pair((*I).first, X.second));
Ted Kremenekdb863712008-04-16 22:32:20 +00001889 }
Ted Kremenek652adc62008-04-24 23:57:27 +00001890
1891 if (Leaked.empty())
1892 return;
1893
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001894 ExplodedNode<GRState>* N = Builder.MakeNode(St);
Ted Kremenek4f285152008-04-18 16:30:14 +00001895
Ted Kremenek652adc62008-04-24 23:57:27 +00001896 if (!N)
Ted Kremenek4f285152008-04-18 16:30:14 +00001897 return;
Ted Kremenekcb612922008-04-18 19:23:43 +00001898
Ted Kremenek2dabd432008-12-05 02:27:51 +00001899 std::vector<std::pair<SymbolRef,bool> >*& LeaksAtNode = Leaks[N];
Ted Kremenek8dd56462008-04-18 03:39:05 +00001900 assert (!LeaksAtNode);
Ted Kremenek2dabd432008-12-05 02:27:51 +00001901 LeaksAtNode = new std::vector<std::pair<SymbolRef,bool> >();
Ted Kremenekdb863712008-04-16 22:32:20 +00001902
Ted Kremenek2dabd432008-12-05 02:27:51 +00001903 for (llvm::SmallVector<std::pair<SymbolRef,bool>, 10>::iterator
Ted Kremenekf9790ae2008-10-24 20:32:50 +00001904 I = Leaked.begin(), E = Leaked.end(); I != E; ++I)
Ted Kremenek8dd56462008-04-18 03:39:05 +00001905 (*LeaksAtNode).push_back(*I);
Ted Kremeneke7bd9c22008-04-11 22:25:11 +00001906}
1907
Ted Kremenek652adc62008-04-24 23:57:27 +00001908// Dead symbols.
1909
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001910void CFRefCount::EvalDeadSymbols(ExplodedNodeSet<GRState>& Dst,
Ted Kremenek652adc62008-04-24 23:57:27 +00001911 GRExprEngine& Eng,
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001912 GRStmtNodeBuilder<GRState>& Builder,
1913 ExplodedNode<GRState>* Pred,
Ted Kremenek910e9992008-04-25 01:25:15 +00001914 Stmt* S,
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001915 const GRState* St,
1916 const GRStateManager::DeadSymbolsTy& Dead) {
Ted Kremenek910e9992008-04-25 01:25:15 +00001917
Ted Kremenek652adc62008-04-24 23:57:27 +00001918 // FIXME: a lot of copy-and-paste from EvalEndPath. Refactor.
1919
Ted Kremenek72cd17f2008-08-14 21:16:54 +00001920 RefBindings B = St->get<RefBindings>();
Ted Kremenek2dabd432008-12-05 02:27:51 +00001921 llvm::SmallVector<std::pair<SymbolRef,bool>, 10> Leaked;
Ted Kremenek652adc62008-04-24 23:57:27 +00001922
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001923 for (GRStateManager::DeadSymbolsTy::const_iterator
Ted Kremenek652adc62008-04-24 23:57:27 +00001924 I=Dead.begin(), E=Dead.end(); I!=E; ++I) {
1925
Ted Kremeneke8fdc832008-07-07 16:21:19 +00001926 const RefVal* T = B.lookup(*I);
Ted Kremenek652adc62008-04-24 23:57:27 +00001927
1928 if (!T)
1929 continue;
1930
1931 bool hasLeak = false;
1932
Ted Kremenekf9790ae2008-10-24 20:32:50 +00001933 std::pair<GRStateRef, bool> X
1934 = HandleSymbolDeath(Eng.getStateManager(), St, 0, *I, *T, hasLeak);
1935
1936 St = X.first;
Ted Kremenek652adc62008-04-24 23:57:27 +00001937
Ted Kremeneke8fdc832008-07-07 16:21:19 +00001938 if (hasLeak)
Ted Kremenekf9790ae2008-10-24 20:32:50 +00001939 Leaked.push_back(std::make_pair(*I,X.second));
Ted Kremenek652adc62008-04-24 23:57:27 +00001940 }
1941
1942 if (Leaked.empty())
1943 return;
1944
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001945 ExplodedNode<GRState>* N = Builder.MakeNode(Dst, S, Pred, St);
Ted Kremenek652adc62008-04-24 23:57:27 +00001946
1947 if (!N)
1948 return;
1949
Ted Kremenek2dabd432008-12-05 02:27:51 +00001950 std::vector<std::pair<SymbolRef,bool> >*& LeaksAtNode = Leaks[N];
Ted Kremenek652adc62008-04-24 23:57:27 +00001951 assert (!LeaksAtNode);
Ted Kremenek2dabd432008-12-05 02:27:51 +00001952 LeaksAtNode = new std::vector<std::pair<SymbolRef,bool> >();
Ted Kremenek652adc62008-04-24 23:57:27 +00001953
Ted Kremenek2dabd432008-12-05 02:27:51 +00001954 for (llvm::SmallVector<std::pair<SymbolRef,bool>, 10>::iterator
Ted Kremenekf9790ae2008-10-24 20:32:50 +00001955 I = Leaked.begin(), E = Leaked.end(); I != E; ++I)
Ted Kremenek652adc62008-04-24 23:57:27 +00001956 (*LeaksAtNode).push_back(*I);
1957}
1958
Ted Kremenek4fd88972008-04-17 18:12:53 +00001959 // Return statements.
1960
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001961void CFRefCount::EvalReturn(ExplodedNodeSet<GRState>& Dst,
Ted Kremenek4fd88972008-04-17 18:12:53 +00001962 GRExprEngine& Eng,
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001963 GRStmtNodeBuilder<GRState>& Builder,
Ted Kremenek4fd88972008-04-17 18:12:53 +00001964 ReturnStmt* S,
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001965 ExplodedNode<GRState>* Pred) {
Ted Kremenek4fd88972008-04-17 18:12:53 +00001966
1967 Expr* RetE = S->getRetValue();
1968 if (!RetE) return;
1969
Ted Kremenek72cd17f2008-08-14 21:16:54 +00001970 GRStateRef state(Builder.GetState(Pred), Eng.getStateManager());
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001971 SVal V = state.GetSVal(RetE);
Ted Kremenek4fd88972008-04-17 18:12:53 +00001972
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001973 if (!isa<loc::SymbolVal>(V))
Ted Kremenek4fd88972008-04-17 18:12:53 +00001974 return;
1975
1976 // Get the reference count binding (if any).
Ted Kremenek2dabd432008-12-05 02:27:51 +00001977 SymbolRef Sym = cast<loc::SymbolVal>(V).getSymbol();
Ted Kremenek72cd17f2008-08-14 21:16:54 +00001978 const RefVal* T = state.get<RefBindings>(Sym);
Ted Kremenek4fd88972008-04-17 18:12:53 +00001979
1980 if (!T)
1981 return;
1982
Ted Kremenek72cd17f2008-08-14 21:16:54 +00001983 // Change the reference count.
Ted Kremeneke8fdc832008-07-07 16:21:19 +00001984 RefVal X = *T;
Ted Kremenek4fd88972008-04-17 18:12:53 +00001985
Ted Kremenek72cd17f2008-08-14 21:16:54 +00001986 switch (X.getKind()) {
Ted Kremenek4fd88972008-04-17 18:12:53 +00001987 case RefVal::Owned: {
1988 unsigned cnt = X.getCount();
Ted Kremenek3eabf1c2008-05-22 17:31:13 +00001989 assert (cnt > 0);
1990 X = RefVal::makeReturnedOwned(cnt - 1);
Ted Kremenek4fd88972008-04-17 18:12:53 +00001991 break;
1992 }
1993
1994 case RefVal::NotOwned: {
1995 unsigned cnt = X.getCount();
1996 X = cnt ? RefVal::makeReturnedOwned(cnt - 1)
1997 : RefVal::makeReturnedNotOwned();
1998 break;
1999 }
2000
2001 default:
Ted Kremenek4fd88972008-04-17 18:12:53 +00002002 return;
2003 }
2004
2005 // Update the binding.
Ted Kremenekb9d17f92008-08-17 03:20:02 +00002006 state = state.set<RefBindings>(Sym, X);
Ted Kremenek72cd17f2008-08-14 21:16:54 +00002007 Builder.MakeNode(Dst, S, Pred, state);
Ted Kremenek4fd88972008-04-17 18:12:53 +00002008}
2009
Ted Kremenekcb612922008-04-18 19:23:43 +00002010// Assumptions.
2011
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002012const GRState* CFRefCount::EvalAssume(GRStateManager& VMgr,
2013 const GRState* St,
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002014 SVal Cond, bool Assumption,
Ted Kremenek4323a572008-07-10 22:03:41 +00002015 bool& isFeasible) {
Ted Kremenekcb612922008-04-18 19:23:43 +00002016
2017 // FIXME: We may add to the interface of EvalAssume the list of symbols
2018 // whose assumptions have changed. For now we just iterate through the
2019 // bindings and check if any of the tracked symbols are NULL. This isn't
2020 // too bad since the number of symbols we will track in practice are
2021 // probably small and EvalAssume is only called at branches and a few
2022 // other places.
Ted Kremenek72cd17f2008-08-14 21:16:54 +00002023 RefBindings B = St->get<RefBindings>();
Ted Kremenekcb612922008-04-18 19:23:43 +00002024
2025 if (B.isEmpty())
2026 return St;
2027
2028 bool changed = false;
Ted Kremenekb9d17f92008-08-17 03:20:02 +00002029
2030 GRStateRef state(St, VMgr);
2031 RefBindings::Factory& RefBFactory = state.get_context<RefBindings>();
Ted Kremenekcb612922008-04-18 19:23:43 +00002032
2033 for (RefBindings::iterator I=B.begin(), E=B.end(); I!=E; ++I) {
Ted Kremenekcb612922008-04-18 19:23:43 +00002034 // Check if the symbol is null (or equal to any constant).
2035 // If this is the case, stop tracking the symbol.
Zhongxing Xu39cfed32008-08-29 14:52:36 +00002036 if (VMgr.getSymVal(St, I.getKey())) {
Ted Kremenekcb612922008-04-18 19:23:43 +00002037 changed = true;
2038 B = RefBFactory.Remove(B, I.getKey());
2039 }
2040 }
2041
Ted Kremenekb9d17f92008-08-17 03:20:02 +00002042 if (changed)
2043 state = state.set<RefBindings>(B);
Ted Kremenekcb612922008-04-18 19:23:43 +00002044
Ted Kremenek72cd17f2008-08-14 21:16:54 +00002045 return state;
Ted Kremenekcb612922008-04-18 19:23:43 +00002046}
Ted Kremenek6b3a0f72008-03-11 06:39:11 +00002047
Ted Kremenek2dabd432008-12-05 02:27:51 +00002048RefBindings CFRefCount::Update(RefBindings B, SymbolRef sym,
Ted Kremenek72cd17f2008-08-14 21:16:54 +00002049 RefVal V, ArgEffect E,
Ted Kremenekb9d17f92008-08-17 03:20:02 +00002050 RefVal::Kind& hasErr,
2051 RefBindings::Factory& RefBFactory) {
Ted Kremenek6b3a0f72008-03-11 06:39:11 +00002052
Ted Kremenek1ac08d62008-03-11 17:48:22 +00002053 // FIXME: This dispatch can potentially be sped up by unifiying it into
2054 // a single switch statement. Opt for simplicity for now.
Ted Kremenek6b3a0f72008-03-11 06:39:11 +00002055
Ted Kremenek1ac08d62008-03-11 17:48:22 +00002056 switch (E) {
2057 default:
2058 assert (false && "Unhandled CFRef transition.");
Ted Kremenek3eabf1c2008-05-22 17:31:13 +00002059
2060 case MayEscape:
2061 if (V.getKind() == RefVal::Owned) {
Ted Kremenek553cf182008-06-25 21:21:56 +00002062 V = V ^ RefVal::NotOwned;
Ted Kremenek3eabf1c2008-05-22 17:31:13 +00002063 break;
2064 }
Ted Kremenek3eabf1c2008-05-22 17:31:13 +00002065 // Fall-through.
Ted Kremenek070a8252008-07-09 18:11:16 +00002066 case DoNothingByRef:
Ted Kremenek1ac08d62008-03-11 17:48:22 +00002067 case DoNothing:
Ted Kremenekd3dbcf42008-05-05 22:11:16 +00002068 if (!isGCEnabled() && V.getKind() == RefVal::Released) {
Ted Kremenek553cf182008-06-25 21:21:56 +00002069 V = V ^ RefVal::ErrorUseAfterRelease;
Ted Kremenek9ed18e62008-04-16 04:28:53 +00002070 hasErr = V.getKind();
Ted Kremenek00a3a5f2008-03-12 01:21:45 +00002071 break;
Ted Kremenek9e476de2008-08-12 18:30:56 +00002072 }
Ted Kremenek1ac08d62008-03-11 17:48:22 +00002073 return B;
Ted Kremeneke19f4492008-06-30 16:57:41 +00002074
Ted Kremenek80d753f2008-07-01 00:01:02 +00002075 case Autorelease:
Ted Kremenek14993892008-05-06 02:41:27 +00002076 case StopTracking:
2077 return RefBFactory.Remove(B, sym);
Ted Kremenek9e476de2008-08-12 18:30:56 +00002078
Ted Kremenek1ac08d62008-03-11 17:48:22 +00002079 case IncRef:
2080 switch (V.getKind()) {
2081 default:
2082 assert(false);
2083
2084 case RefVal::Owned:
Ted Kremenek1ac08d62008-03-11 17:48:22 +00002085 case RefVal::NotOwned:
Ted Kremenek553cf182008-06-25 21:21:56 +00002086 V = V + 1;
Ted Kremenek9e476de2008-08-12 18:30:56 +00002087 break;
Ted Kremenek1ac08d62008-03-11 17:48:22 +00002088 case RefVal::Released:
Ted Kremenekd3dbcf42008-05-05 22:11:16 +00002089 if (isGCEnabled())
Ted Kremenek553cf182008-06-25 21:21:56 +00002090 V = V ^ RefVal::Owned;
Ted Kremenek65c91652008-04-29 05:44:10 +00002091 else {
Ted Kremenek553cf182008-06-25 21:21:56 +00002092 V = V ^ RefVal::ErrorUseAfterRelease;
Ted Kremenek65c91652008-04-29 05:44:10 +00002093 hasErr = V.getKind();
2094 }
Ted Kremenek1ac08d62008-03-11 17:48:22 +00002095 break;
Ted Kremenek9e476de2008-08-12 18:30:56 +00002096 }
Ted Kremenek940b1d82008-04-10 23:44:06 +00002097 break;
2098
Ted Kremenek553cf182008-06-25 21:21:56 +00002099 case SelfOwn:
2100 V = V ^ RefVal::NotOwned;
Ted Kremenek9e476de2008-08-12 18:30:56 +00002101 // Fall-through.
Ted Kremenek1ac08d62008-03-11 17:48:22 +00002102 case DecRef:
2103 switch (V.getKind()) {
2104 default:
2105 assert (false);
Ted Kremenek9e476de2008-08-12 18:30:56 +00002106
Ted Kremenek553cf182008-06-25 21:21:56 +00002107 case RefVal::Owned:
2108 V = V.getCount() > 1 ? V - 1 : V ^ RefVal::Released;
Ted Kremenek1ac08d62008-03-11 17:48:22 +00002109 break;
Ted Kremenek1ac08d62008-03-11 17:48:22 +00002110
Ted Kremenek553cf182008-06-25 21:21:56 +00002111 case RefVal::NotOwned:
2112 if (V.getCount() > 0)
2113 V = V - 1;
Ted Kremenek61b9f872008-04-10 23:09:18 +00002114 else {
Ted Kremenek553cf182008-06-25 21:21:56 +00002115 V = V ^ RefVal::ErrorReleaseNotOwned;
Ted Kremenek9ed18e62008-04-16 04:28:53 +00002116 hasErr = V.getKind();
Ted Kremenek9e476de2008-08-12 18:30:56 +00002117 }
Ted Kremenek1ac08d62008-03-11 17:48:22 +00002118 break;
Ted Kremenek1ac08d62008-03-11 17:48:22 +00002119
2120 case RefVal::Released:
Ted Kremenek553cf182008-06-25 21:21:56 +00002121 V = V ^ RefVal::ErrorUseAfterRelease;
Ted Kremenek9ed18e62008-04-16 04:28:53 +00002122 hasErr = V.getKind();
Ted Kremenek1ac08d62008-03-11 17:48:22 +00002123 break;
Ted Kremenek9e476de2008-08-12 18:30:56 +00002124 }
Ted Kremenek940b1d82008-04-10 23:44:06 +00002125 break;
Ted Kremenek1ac08d62008-03-11 17:48:22 +00002126 }
Ted Kremenek1ac08d62008-03-11 17:48:22 +00002127 return RefBFactory.Add(B, sym, V);
Ted Kremenek6b3a0f72008-03-11 06:39:11 +00002128}
2129
Ted Kremenekfa34b332008-04-09 01:10:13 +00002130//===----------------------------------------------------------------------===//
Ted Kremenek05cbe1a2008-04-09 23:49:11 +00002131// Error reporting.
Ted Kremenekfa34b332008-04-09 01:10:13 +00002132//===----------------------------------------------------------------------===//
2133
Ted Kremenek8dd56462008-04-18 03:39:05 +00002134namespace {
2135
2136 //===-------------===//
2137 // Bug Descriptions. //
2138 //===-------------===//
2139
Ted Kremenek95cc1ba2008-04-18 20:54:29 +00002140 class VISIBILITY_HIDDEN CFRefBug : public BugTypeCacheLocation {
Ted Kremenek8dd56462008-04-18 03:39:05 +00002141 protected:
2142 CFRefCount& TF;
2143
2144 public:
2145 CFRefBug(CFRefCount& tf) : TF(tf) {}
Ted Kremenek072192b2008-04-30 23:47:44 +00002146
Ted Kremenekbb77e9b2008-05-01 22:50:36 +00002147 CFRefCount& getTF() { return TF; }
Ted Kremenek789deac2008-05-05 23:16:31 +00002148 const CFRefCount& getTF() const { return TF; }
2149
Ted Kremenekc9fa2f72008-05-01 23:13:35 +00002150 virtual bool isLeak() const { return false; }
Ted Kremenek8c036c72008-09-20 04:23:38 +00002151
2152 const char* getCategory() const {
Ted Kremenek062bae02008-09-27 22:02:42 +00002153 return "Memory (Core Foundation/Objective-C)";
Ted Kremenek8c036c72008-09-20 04:23:38 +00002154 }
Ted Kremenek8dd56462008-04-18 03:39:05 +00002155 };
2156
2157 class VISIBILITY_HIDDEN UseAfterRelease : public CFRefBug {
2158 public:
2159 UseAfterRelease(CFRefCount& tf) : CFRefBug(tf) {}
2160
2161 virtual const char* getName() const {
Ted Kremenek8c036c72008-09-20 04:23:38 +00002162 return "use-after-release";
Ted Kremenek8dd56462008-04-18 03:39:05 +00002163 }
2164 virtual const char* getDescription() const {
Ted Kremenek9e476de2008-08-12 18:30:56 +00002165 return "Reference-counted object is used after it is released.";
Ted Kremenek8dd56462008-04-18 03:39:05 +00002166 }
2167
2168 virtual void EmitWarnings(BugReporter& BR);
Ted Kremenek8dd56462008-04-18 03:39:05 +00002169 };
2170
2171 class VISIBILITY_HIDDEN BadRelease : public CFRefBug {
2172 public:
2173 BadRelease(CFRefCount& tf) : CFRefBug(tf) {}
2174
2175 virtual const char* getName() const {
Ted Kremenek8c036c72008-09-20 04:23:38 +00002176 return "bad release";
Ted Kremenek8dd56462008-04-18 03:39:05 +00002177 }
2178 virtual const char* getDescription() const {
2179 return "Incorrect decrement of the reference count of a "
Ted Kremenek2cf943a2008-04-18 04:55:01 +00002180 "CoreFoundation object: "
Ted Kremenek8dd56462008-04-18 03:39:05 +00002181 "The object is not owned at this point by the caller.";
2182 }
2183
2184 virtual void EmitWarnings(BugReporter& BR);
2185 };
2186
2187 class VISIBILITY_HIDDEN Leak : public CFRefBug {
Ted Kremenekf9790ae2008-10-24 20:32:50 +00002188 bool isReturn;
Ted Kremenek8dd56462008-04-18 03:39:05 +00002189 public:
2190 Leak(CFRefCount& tf) : CFRefBug(tf) {}
2191
Ted Kremenekf9790ae2008-10-24 20:32:50 +00002192 void setIsReturn(bool x) { isReturn = x; }
2193
Ted Kremenek8dd56462008-04-18 03:39:05 +00002194 virtual const char* getName() const {
Ted Kremenek432af592008-05-06 18:11:36 +00002195
Ted Kremenekf9790ae2008-10-24 20:32:50 +00002196 if (!isReturn) {
2197 if (getTF().isGCEnabled())
2198 return "leak (GC)";
2199
2200 if (getTF().getLangOptions().getGCMode() == LangOptions::HybridGC)
2201 return "leak (hybrid MM, non-GC)";
2202
2203 assert (getTF().getLangOptions().getGCMode() == LangOptions::NonGC);
2204 return "leak";
2205 }
2206 else {
2207 if (getTF().isGCEnabled())
Ted Kremenek9d1d5702008-10-24 21:22:44 +00002208 return "[naming convention] leak of returned object (GC)";
Ted Kremenekf9790ae2008-10-24 20:32:50 +00002209
2210 if (getTF().getLangOptions().getGCMode() == LangOptions::HybridGC)
Ted Kremenek9d1d5702008-10-24 21:22:44 +00002211 return "[naming convention] leak of returned object (hybrid MM, "
2212 "non-GC)";
Ted Kremenekf9790ae2008-10-24 20:32:50 +00002213
2214 assert (getTF().getLangOptions().getGCMode() == LangOptions::NonGC);
Ted Kremenek9d1d5702008-10-24 21:22:44 +00002215 return "[naming convention] leak of returned object";
Ted Kremenekf9790ae2008-10-24 20:32:50 +00002216 }
Ted Kremenek8dd56462008-04-18 03:39:05 +00002217 }
2218
2219 virtual const char* getDescription() const {
Ted Kremenek9e476de2008-08-12 18:30:56 +00002220 return "Object leaked";
Ted Kremenek8dd56462008-04-18 03:39:05 +00002221 }
2222
2223 virtual void EmitWarnings(BugReporter& BR);
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002224 virtual void GetErrorNodes(std::vector<ExplodedNode<GRState>*>& Nodes);
Ted Kremenekc9fa2f72008-05-01 23:13:35 +00002225 virtual bool isLeak() const { return true; }
Ted Kremenek6ed9afc2008-05-16 18:33:44 +00002226 virtual bool isCached(BugReport& R);
Ted Kremenek8dd56462008-04-18 03:39:05 +00002227 };
2228
2229 //===---------===//
2230 // Bug Reports. //
2231 //===---------===//
2232
2233 class VISIBILITY_HIDDEN CFRefReport : public RangedBugReport {
Ted Kremenek2dabd432008-12-05 02:27:51 +00002234 SymbolRef Sym;
Ted Kremenek8dd56462008-04-18 03:39:05 +00002235 public:
Ted Kremenek2dabd432008-12-05 02:27:51 +00002236 CFRefReport(CFRefBug& D, ExplodedNode<GRState> *n, SymbolRef sym)
Ted Kremenek8dd56462008-04-18 03:39:05 +00002237 : RangedBugReport(D, n), Sym(sym) {}
2238
2239 virtual ~CFRefReport() {}
2240
Ted Kremenekbb77e9b2008-05-01 22:50:36 +00002241 CFRefBug& getBugType() {
2242 return (CFRefBug&) RangedBugReport::getBugType();
2243 }
2244 const CFRefBug& getBugType() const {
2245 return (const CFRefBug&) RangedBugReport::getBugType();
2246 }
2247
2248 virtual void getRanges(BugReporter& BR, const SourceRange*& beg,
2249 const SourceRange*& end) {
2250
Ted Kremeneke92c1b22008-05-02 20:53:50 +00002251 if (!getBugType().isLeak())
Ted Kremenekbb77e9b2008-05-01 22:50:36 +00002252 RangedBugReport::getRanges(BR, beg, end);
Ted Kremenek9e476de2008-08-12 18:30:56 +00002253 else
2254 beg = end = 0;
Ted Kremenekbb77e9b2008-05-01 22:50:36 +00002255 }
2256
Ted Kremenek2dabd432008-12-05 02:27:51 +00002257 SymbolRef getSymbol() const { return Sym; }
Ted Kremenek6ed9afc2008-05-16 18:33:44 +00002258
Ted Kremenekc9fa2f72008-05-01 23:13:35 +00002259 virtual PathDiagnosticPiece* getEndPath(BugReporter& BR,
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002260 ExplodedNode<GRState>* N);
Ted Kremenekc9fa2f72008-05-01 23:13:35 +00002261
Ted Kremenek072192b2008-04-30 23:47:44 +00002262 virtual std::pair<const char**,const char**> getExtraDescriptiveText();
Ted Kremenek8dd56462008-04-18 03:39:05 +00002263
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002264 virtual PathDiagnosticPiece* VisitNode(ExplodedNode<GRState>* N,
2265 ExplodedNode<GRState>* PrevN,
2266 ExplodedGraph<GRState>& G,
Ted Kremenek8dd56462008-04-18 03:39:05 +00002267 BugReporter& BR);
2268 };
2269
2270
2271} // end anonymous namespace
2272
2273void CFRefCount::RegisterChecks(GRExprEngine& Eng) {
Ted Kremenek8dd56462008-04-18 03:39:05 +00002274 Eng.Register(new UseAfterRelease(*this));
2275 Eng.Register(new BadRelease(*this));
2276 Eng.Register(new Leak(*this));
2277}
2278
Ted Kremenek072192b2008-04-30 23:47:44 +00002279
2280static const char* Msgs[] = {
2281 "Code is compiled in garbage collection only mode" // GC only
2282 " (the bug occurs with garbage collection enabled).",
2283
2284 "Code is compiled without garbage collection.", // No GC.
2285
2286 "Code is compiled for use with and without garbage collection (GC)."
2287 " The bug occurs with GC enabled.", // Hybrid, with GC.
2288
2289 "Code is compiled for use with and without garbage collection (GC)."
2290 " The bug occurs in non-GC mode." // Hyrbird, without GC/
2291};
2292
2293std::pair<const char**,const char**> CFRefReport::getExtraDescriptiveText() {
2294 CFRefCount& TF = static_cast<CFRefBug&>(getBugType()).getTF();
2295
2296 switch (TF.getLangOptions().getGCMode()) {
2297 default:
2298 assert(false);
Ted Kremenek31593ac2008-05-01 04:02:04 +00002299
2300 case LangOptions::GCOnly:
2301 assert (TF.isGCEnabled());
Ted Kremenek9e476de2008-08-12 18:30:56 +00002302 return std::make_pair(&Msgs[0], &Msgs[0]+1);
2303
Ted Kremenek072192b2008-04-30 23:47:44 +00002304 case LangOptions::NonGC:
2305 assert (!TF.isGCEnabled());
Ted Kremenek072192b2008-04-30 23:47:44 +00002306 return std::make_pair(&Msgs[1], &Msgs[1]+1);
2307
2308 case LangOptions::HybridGC:
2309 if (TF.isGCEnabled())
2310 return std::make_pair(&Msgs[2], &Msgs[2]+1);
2311 else
2312 return std::make_pair(&Msgs[3], &Msgs[3]+1);
2313 }
2314}
2315
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002316PathDiagnosticPiece* CFRefReport::VisitNode(ExplodedNode<GRState>* N,
2317 ExplodedNode<GRState>* PrevN,
2318 ExplodedGraph<GRState>& G,
Ted Kremenek8dd56462008-04-18 03:39:05 +00002319 BugReporter& BR) {
2320
2321 // Check if the type state has changed.
2322
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002323 const GRState* PrevSt = PrevN->getState();
2324 const GRState* CurrSt = N->getState();
Ted Kremenek8dd56462008-04-18 03:39:05 +00002325
Ted Kremenek72cd17f2008-08-14 21:16:54 +00002326 RefBindings PrevB = PrevSt->get<RefBindings>();
2327 RefBindings CurrB = CurrSt->get<RefBindings>();
Ted Kremenek8dd56462008-04-18 03:39:05 +00002328
Ted Kremeneke8fdc832008-07-07 16:21:19 +00002329 const RefVal* PrevT = PrevB.lookup(Sym);
2330 const RefVal* CurrT = CurrB.lookup(Sym);
Ted Kremenek8dd56462008-04-18 03:39:05 +00002331
Ted Kremenek2cf943a2008-04-18 04:55:01 +00002332 if (!CurrT)
2333 return NULL;
Ted Kremenek8dd56462008-04-18 03:39:05 +00002334
Ted Kremenek2cf943a2008-04-18 04:55:01 +00002335 const char* Msg = NULL;
Ted Kremeneke8fdc832008-07-07 16:21:19 +00002336 const RefVal& CurrV = *CurrB.lookup(Sym);
Ted Kremenekce48e002008-05-05 17:53:17 +00002337
Ted Kremenek2cf943a2008-04-18 04:55:01 +00002338 if (!PrevT) {
2339
Ted Kremenekce48e002008-05-05 17:53:17 +00002340 Stmt* S = cast<PostStmt>(N->getLocation()).getStmt();
2341
2342 if (CurrV.isOwned()) {
2343
2344 if (isa<CallExpr>(S))
2345 Msg = "Function call returns an object with a +1 retain count"
2346 " (owning reference).";
2347 else {
2348 assert (isa<ObjCMessageExpr>(S));
2349 Msg = "Method returns an object with a +1 retain count"
2350 " (owning reference).";
2351 }
2352 }
Ted Kremenek2cf943a2008-04-18 04:55:01 +00002353 else {
2354 assert (CurrV.isNotOwned());
Ted Kremenekce48e002008-05-05 17:53:17 +00002355
2356 if (isa<CallExpr>(S))
2357 Msg = "Function call returns an object with a +0 retain count"
2358 " (non-owning reference).";
2359 else {
2360 assert (isa<ObjCMessageExpr>(S));
2361 Msg = "Method returns an object with a +0 retain count"
2362 " (non-owning reference).";
2363 }
Ted Kremenek2cf943a2008-04-18 04:55:01 +00002364 }
Ted Kremenekce48e002008-05-05 17:53:17 +00002365
Ted Kremenek2cf943a2008-04-18 04:55:01 +00002366 FullSourceLoc Pos(S->getLocStart(), BR.getContext().getSourceManager());
2367 PathDiagnosticPiece* P = new PathDiagnosticPiece(Pos, Msg);
2368
2369 if (Expr* Exp = dyn_cast<Expr>(S))
2370 P->addRange(Exp->getSourceRange());
2371
2372 return P;
2373 }
2374
Ted Kremeneke8fdc832008-07-07 16:21:19 +00002375 // Determine if the typestate has changed.
2376 RefVal PrevV = *PrevB.lookup(Sym);
Ted Kremenek2cf943a2008-04-18 04:55:01 +00002377
2378 if (PrevV == CurrV)
2379 return NULL;
2380
2381 // The typestate has changed.
2382
2383 std::ostringstream os;
Ted Kremenek72cd17f2008-08-14 21:16:54 +00002384 std::string s;
Ted Kremenek2cf943a2008-04-18 04:55:01 +00002385
2386 switch (CurrV.getKind()) {
2387 case RefVal::Owned:
2388 case RefVal::NotOwned:
Ted Kremenek3eabf1c2008-05-22 17:31:13 +00002389
2390 if (PrevV.getCount() == CurrV.getCount())
2391 return 0;
Ted Kremenek2cf943a2008-04-18 04:55:01 +00002392
2393 if (PrevV.getCount() > CurrV.getCount())
2394 os << "Reference count decremented.";
2395 else
2396 os << "Reference count incremented.";
2397
Ted Kremenek3eabf1c2008-05-22 17:31:13 +00002398 if (unsigned Count = CurrV.getCount()) {
Ted Kremenekce48e002008-05-05 17:53:17 +00002399
2400 os << " Object has +" << Count;
Ted Kremenek79c140b2008-04-18 05:32:44 +00002401
Ted Kremenekce48e002008-05-05 17:53:17 +00002402 if (Count > 1)
2403 os << " retain counts.";
Ted Kremenek79c140b2008-04-18 05:32:44 +00002404 else
Ted Kremenekce48e002008-05-05 17:53:17 +00002405 os << " retain count.";
Ted Kremenek79c140b2008-04-18 05:32:44 +00002406 }
Ted Kremenek2cf943a2008-04-18 04:55:01 +00002407
Ted Kremenek72cd17f2008-08-14 21:16:54 +00002408 s = os.str();
2409 Msg = s.c_str();
Ted Kremenek2cf943a2008-04-18 04:55:01 +00002410
2411 break;
2412
2413 case RefVal::Released:
2414 Msg = "Object released.";
2415 break;
2416
2417 case RefVal::ReturnedOwned:
Ted Kremenekf9790ae2008-10-24 20:32:50 +00002418 Msg = "Object returned to caller as an owning reference (single retain "
2419 "count transferred to caller).";
Ted Kremenek2cf943a2008-04-18 04:55:01 +00002420 break;
2421
2422 case RefVal::ReturnedNotOwned:
Ted Kremenekce48e002008-05-05 17:53:17 +00002423 Msg = "Object returned to caller with a +0 (non-owning) retain count.";
Ted Kremenek2cf943a2008-04-18 04:55:01 +00002424 break;
2425
2426 default:
2427 return NULL;
2428 }
2429
2430 Stmt* S = cast<PostStmt>(N->getLocation()).getStmt();
2431 FullSourceLoc Pos(S->getLocStart(), BR.getContext().getSourceManager());
2432 PathDiagnosticPiece* P = new PathDiagnosticPiece(Pos, Msg);
2433
2434 // Add the range by scanning the children of the statement for any bindings
2435 // to Sym.
2436
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002437 GRStateManager& VSM = cast<GRBugReporter>(BR).getStateManager();
Ted Kremenek2cf943a2008-04-18 04:55:01 +00002438
2439 for (Stmt::child_iterator I = S->child_begin(), E = S->child_end(); I!=E; ++I)
2440 if (Expr* Exp = dyn_cast_or_null<Expr>(*I)) {
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002441 SVal X = VSM.GetSVal(CurrSt, Exp);
Ted Kremenek2cf943a2008-04-18 04:55:01 +00002442
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002443 if (loc::SymbolVal* SV = dyn_cast<loc::SymbolVal>(&X))
Ted Kremenek2cf943a2008-04-18 04:55:01 +00002444 if (SV->getSymbol() == Sym) {
2445 P->addRange(Exp->getSourceRange()); break;
2446 }
2447 }
2448
2449 return P;
Ted Kremenek8dd56462008-04-18 03:39:05 +00002450}
2451
Ted Kremenek9e240492008-10-04 05:50:14 +00002452namespace {
2453class VISIBILITY_HIDDEN FindUniqueBinding :
2454 public StoreManager::BindingsHandler {
Ted Kremenek2dabd432008-12-05 02:27:51 +00002455 SymbolRef Sym;
Ted Kremenek9e240492008-10-04 05:50:14 +00002456 MemRegion* Binding;
2457 bool First;
2458
2459 public:
Ted Kremenek2dabd432008-12-05 02:27:51 +00002460 FindUniqueBinding(SymbolRef sym) : Sym(sym), Binding(0), First(true) {}
Ted Kremenek9e240492008-10-04 05:50:14 +00002461
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002462 bool HandleBinding(StoreManager& SMgr, Store store, MemRegion* R, SVal val) {
2463 if (const loc::SymbolVal* SV = dyn_cast<loc::SymbolVal>(&val)) {
Ted Kremenek9e240492008-10-04 05:50:14 +00002464 if (SV->getSymbol() != Sym)
2465 return true;
2466 }
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002467 else if (const nonloc::SymbolVal* SV=dyn_cast<nonloc::SymbolVal>(&val)) {
Ted Kremenek9e240492008-10-04 05:50:14 +00002468 if (SV->getSymbol() != Sym)
2469 return true;
2470 }
2471 else
2472 return true;
2473
2474 if (Binding) {
2475 First = false;
2476 return false;
2477 }
2478 else
2479 Binding = R;
2480
2481 return true;
2482 }
2483
2484 operator bool() { return First && Binding; }
2485 MemRegion* getRegion() { return Binding; }
2486};
2487}
2488
2489static std::pair<ExplodedNode<GRState>*,MemRegion*>
Ted Kremenek2bc39c62008-08-29 00:47:32 +00002490GetAllocationSite(GRStateManager* StateMgr, ExplodedNode<GRState>* N,
Ted Kremenek2dabd432008-12-05 02:27:51 +00002491 SymbolRef Sym) {
Ted Kremenek6ed9afc2008-05-16 18:33:44 +00002492
Ted Kremenek2bc39c62008-08-29 00:47:32 +00002493 // Find both first node that referred to the tracked symbol and the
2494 // memory location that value was store to.
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002495 ExplodedNode<GRState>* Last = N;
Ted Kremenek9e240492008-10-04 05:50:14 +00002496 MemRegion* FirstBinding = 0;
Ted Kremenek6ed9afc2008-05-16 18:33:44 +00002497
2498 while (N) {
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002499 const GRState* St = N->getState();
Ted Kremenek72cd17f2008-08-14 21:16:54 +00002500 RefBindings B = St->get<RefBindings>();
Ted Kremenek6ed9afc2008-05-16 18:33:44 +00002501
Ted Kremeneke8fdc832008-07-07 16:21:19 +00002502 if (!B.lookup(Sym))
Ted Kremenek6ed9afc2008-05-16 18:33:44 +00002503 break;
Ted Kremenek2bc39c62008-08-29 00:47:32 +00002504
2505 if (StateMgr) {
Ted Kremenek9e240492008-10-04 05:50:14 +00002506 FindUniqueBinding FB(Sym);
2507 StateMgr->iterBindings(St, FB);
2508 if (FB) FirstBinding = FB.getRegion();
Ted Kremenek6ed9afc2008-05-16 18:33:44 +00002509 }
2510
Ted Kremenek6ed9afc2008-05-16 18:33:44 +00002511 Last = N;
2512 N = N->pred_empty() ? NULL : *(N->pred_begin());
2513 }
2514
Ted Kremenek2bc39c62008-08-29 00:47:32 +00002515 return std::make_pair(Last, FirstBinding);
Ted Kremenek6ed9afc2008-05-16 18:33:44 +00002516}
Ted Kremeneka22cc2f2008-05-06 23:07:13 +00002517
Ted Kremenek2bc39c62008-08-29 00:47:32 +00002518PathDiagnosticPiece* CFRefReport::getEndPath(BugReporter& br,
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002519 ExplodedNode<GRState>* EndN) {
Ted Kremenek1aa44c72008-05-22 23:45:19 +00002520
Ted Kremenek2bc39c62008-08-29 00:47:32 +00002521 GRBugReporter& BR = cast<GRBugReporter>(br);
2522
Ted Kremenek1aa44c72008-05-22 23:45:19 +00002523 // Tell the BugReporter to report cases when the tracked symbol is
2524 // assigned to different variables, etc.
Ted Kremenekc0959972008-07-02 21:24:01 +00002525 cast<GRBugReporter>(BR).addNotableSymbol(Sym);
Ted Kremenekc9fa2f72008-05-01 23:13:35 +00002526
2527 if (!getBugType().isLeak())
Ted Kremeneke28565b2008-05-05 18:50:19 +00002528 return RangedBugReport::getEndPath(BR, EndN);
Ted Kremeneke8fdc832008-07-07 16:21:19 +00002529
Ted Kremenekc9fa2f72008-05-01 23:13:35 +00002530 // We are a leak. Walk up the graph to get to the first node where the
Ted Kremenek6ed9afc2008-05-16 18:33:44 +00002531 // symbol appeared, and also get the first VarDecl that tracked object
2532 // is stored to.
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002533 ExplodedNode<GRState>* AllocNode = 0;
Ted Kremenek9e240492008-10-04 05:50:14 +00002534 MemRegion* FirstBinding = 0;
Ted Kremenek2bc39c62008-08-29 00:47:32 +00002535
2536 llvm::tie(AllocNode, FirstBinding) =
2537 GetAllocationSite(&BR.getStateManager(), EndN, Sym);
Ted Kremenekc9fa2f72008-05-01 23:13:35 +00002538
Ted Kremenek6ed9afc2008-05-16 18:33:44 +00002539 // Get the allocate site.
2540 assert (AllocNode);
2541 Stmt* FirstStmt = cast<PostStmt>(AllocNode->getLocation()).getStmt();
Ted Kremenekc9fa2f72008-05-01 23:13:35 +00002542
Ted Kremeneke28565b2008-05-05 18:50:19 +00002543 SourceManager& SMgr = BR.getContext().getSourceManager();
2544 unsigned AllocLine = SMgr.getLogicalLineNumber(FirstStmt->getLocStart());
Ted Kremenekc9fa2f72008-05-01 23:13:35 +00002545
Ted Kremeneke28565b2008-05-05 18:50:19 +00002546 // Get the leak site. We may have multiple ExplodedNodes (one with the
2547 // leak) that occur on the same line number; if the node with the leak
2548 // has any immediate predecessor nodes with the same line number, find
2549 // any transitive-successors that have a different statement and use that
2550 // line number instead. This avoids emiting a diagnostic like:
2551 //
2552 // // 'y' is leaked.
2553 // int x = foo(y);
2554 //
2555 // instead we want:
2556 //
2557 // int x = foo(y);
2558 // // 'y' is leaked.
2559
2560 Stmt* S = getStmt(BR); // This is the statement where the leak occured.
2561 assert (S);
2562 unsigned EndLine = SMgr.getLogicalLineNumber(S->getLocStart());
2563
2564 // Look in the *trimmed* graph at the immediate predecessor of EndN. Does
2565 // it occur on the same line?
Ted Kremeneka22cc2f2008-05-06 23:07:13 +00002566 PathDiagnosticPiece::DisplayHint Hint = PathDiagnosticPiece::Above;
Ted Kremeneke28565b2008-05-05 18:50:19 +00002567
2568 assert (!EndN->pred_empty()); // Not possible to have 0 predecessors.
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002569 ExplodedNode<GRState> *Pred = *(EndN->pred_begin());
Ted Kremeneka22cc2f2008-05-06 23:07:13 +00002570 ProgramPoint PredPos = Pred->getLocation();
Ted Kremeneke28565b2008-05-05 18:50:19 +00002571
Ted Kremeneka22cc2f2008-05-06 23:07:13 +00002572 if (PostStmt* PredPS = dyn_cast<PostStmt>(&PredPos)) {
Ted Kremeneke28565b2008-05-05 18:50:19 +00002573
Ted Kremeneka22cc2f2008-05-06 23:07:13 +00002574 Stmt* SPred = PredPS->getStmt();
Ted Kremeneke28565b2008-05-05 18:50:19 +00002575
2576 // Predecessor at same line?
Ted Kremeneka22cc2f2008-05-06 23:07:13 +00002577 if (SMgr.getLogicalLineNumber(SPred->getLocStart()) != EndLine) {
2578 Hint = PathDiagnosticPiece::Below;
2579 S = SPred;
2580 }
Ted Kremeneke28565b2008-05-05 18:50:19 +00002581 }
Ted Kremeneke28565b2008-05-05 18:50:19 +00002582
2583 // Generate the diagnostic.
Ted Kremeneka22cc2f2008-05-06 23:07:13 +00002584 FullSourceLoc L( S->getLocStart(), SMgr);
Ted Kremenekc9fa2f72008-05-01 23:13:35 +00002585 std::ostringstream os;
Ted Kremeneke92c1b22008-05-02 20:53:50 +00002586
Ted Kremeneke28565b2008-05-05 18:50:19 +00002587 os << "Object allocated on line " << AllocLine;
Ted Kremeneke92c1b22008-05-02 20:53:50 +00002588
Ted Kremenek2bc39c62008-08-29 00:47:32 +00002589 if (FirstBinding)
Ted Kremenek9e240492008-10-04 05:50:14 +00002590 os << " and stored into '" << FirstBinding->getString() << '\'';
Ted Kremenek3ad2cc82008-10-22 23:56:21 +00002591
Ted Kremenek9e240492008-10-04 05:50:14 +00002592
Ted Kremenek3ad2cc82008-10-22 23:56:21 +00002593 // Get the retain count.
2594 const RefVal* RV = EndN->getState()->get<RefBindings>(Sym);
2595
2596 if (RV->getKind() == RefVal::ErrorLeakReturned) {
Ted Kremenek04f9d462008-12-02 01:26:07 +00002597 // FIXME: Per comments in rdar://6320065, "create" only applies to CF
2598 // ojbects. Only "copy", "alloc", "retain" and "new" transfer ownership
2599 // to the caller for NS objects.
Ted Kremenek3ad2cc82008-10-22 23:56:21 +00002600 ObjCMethodDecl& MD = cast<ObjCMethodDecl>(BR.getGraph().getCodeDecl());
2601 os << " is returned from a method whose name ('"
Chris Lattner077bf5e2008-11-24 03:33:13 +00002602 << MD.getSelector().getAsString()
Ted Kremenek234a4c22009-01-07 00:39:56 +00002603 << "') does not contain 'copy' or otherwise starts with"
Ted Kremenek9d1d5702008-10-24 21:22:44 +00002604 " 'new' or 'alloc'. This violates the naming convention rules given"
Ted Kremenek3ad2cc82008-10-22 23:56:21 +00002605 " in the Memory Management Guide for Cocoa (object leaked).";
2606 }
2607 else
Ted Kremenek9d1d5702008-10-24 21:22:44 +00002608 os << " is no longer referenced after this point and has a retain count of"
2609 " +"
Ted Kremenek3ad2cc82008-10-22 23:56:21 +00002610 << RV->getCount() << " (object leaked).";
Ted Kremenekc9fa2f72008-05-01 23:13:35 +00002611
Ted Kremeneka22cc2f2008-05-06 23:07:13 +00002612 return new PathDiagnosticPiece(L, os.str(), Hint);
Ted Kremenekc9fa2f72008-05-01 23:13:35 +00002613}
2614
Ted Kremenek05cbe1a2008-04-09 23:49:11 +00002615void UseAfterRelease::EmitWarnings(BugReporter& BR) {
Ted Kremenekfa34b332008-04-09 01:10:13 +00002616
Ted Kremenek05cbe1a2008-04-09 23:49:11 +00002617 for (CFRefCount::use_after_iterator I = TF.use_after_begin(),
2618 E = TF.use_after_end(); I != E; ++I) {
2619
Ted Kremenek8dd56462008-04-18 03:39:05 +00002620 CFRefReport report(*this, I->first, I->second.second);
2621 report.addRange(I->second.first->getSourceRange());
Ted Kremenek75840e12008-04-18 01:56:37 +00002622 BR.EmitWarning(report);
Ted Kremenekfa34b332008-04-09 01:10:13 +00002623 }
Ted Kremenek05cbe1a2008-04-09 23:49:11 +00002624}
2625
2626void BadRelease::EmitWarnings(BugReporter& BR) {
Ted Kremenekfa34b332008-04-09 01:10:13 +00002627
Ted Kremenek05cbe1a2008-04-09 23:49:11 +00002628 for (CFRefCount::bad_release_iterator I = TF.bad_release_begin(),
2629 E = TF.bad_release_end(); I != E; ++I) {
2630
Ted Kremenek8dd56462008-04-18 03:39:05 +00002631 CFRefReport report(*this, I->first, I->second.second);
2632 report.addRange(I->second.first->getSourceRange());
2633 BR.EmitWarning(report);
Ted Kremenek05cbe1a2008-04-09 23:49:11 +00002634 }
2635}
Ted Kremenekfa34b332008-04-09 01:10:13 +00002636
Ted Kremenek989d5192008-04-17 23:43:50 +00002637void Leak::EmitWarnings(BugReporter& BR) {
2638
2639 for (CFRefCount::leaks_iterator I = TF.leaks_begin(),
2640 E = TF.leaks_end(); I != E; ++I) {
2641
Ted Kremenek2dabd432008-12-05 02:27:51 +00002642 std::vector<std::pair<SymbolRef, bool> >& SymV = *(I->second);
Ted Kremenek8dd56462008-04-18 03:39:05 +00002643 unsigned n = SymV.size();
2644
2645 for (unsigned i = 0; i < n; ++i) {
Ted Kremenekf9790ae2008-10-24 20:32:50 +00002646 setIsReturn(SymV[i].second);
2647 CFRefReport report(*this, I->first, SymV[i].first);
Ted Kremenek8dd56462008-04-18 03:39:05 +00002648 BR.EmitWarning(report);
2649 }
Ted Kremenek989d5192008-04-17 23:43:50 +00002650 }
2651}
2652
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002653void Leak::GetErrorNodes(std::vector<ExplodedNode<GRState>*>& Nodes) {
Ted Kremenekcb612922008-04-18 19:23:43 +00002654 for (CFRefCount::leaks_iterator I=TF.leaks_begin(), E=TF.leaks_end();
2655 I!=E; ++I)
2656 Nodes.push_back(I->first);
2657}
2658
Ted Kremenek6ed9afc2008-05-16 18:33:44 +00002659bool Leak::isCached(BugReport& R) {
2660
2661 // Most bug reports are cached at the location where they occured.
2662 // With leaks, we want to unique them by the location where they were
Ted Kremenekf9790ae2008-10-24 20:32:50 +00002663 // allocated, and only report a single path.
Ted Kremenek6ed9afc2008-05-16 18:33:44 +00002664
Ted Kremenek2dabd432008-12-05 02:27:51 +00002665 SymbolRef Sym = static_cast<CFRefReport&>(R).getSymbol();
Ted Kremenek6ed9afc2008-05-16 18:33:44 +00002666
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002667 ExplodedNode<GRState>* AllocNode =
Ted Kremenek2bc39c62008-08-29 00:47:32 +00002668 GetAllocationSite(0, R.getEndNode(), Sym).first;
Ted Kremenek6ed9afc2008-05-16 18:33:44 +00002669
2670 if (!AllocNode)
2671 return false;
2672
2673 return BugTypeCacheLocation::isCached(AllocNode->getLocation());
2674}
2675
Ted Kremenek6b3a0f72008-03-11 06:39:11 +00002676//===----------------------------------------------------------------------===//
Ted Kremenekd71ed262008-04-10 22:16:52 +00002677// Transfer function creation for external clients.
Ted Kremenek6b3a0f72008-03-11 06:39:11 +00002678//===----------------------------------------------------------------------===//
2679
Ted Kremenek072192b2008-04-30 23:47:44 +00002680GRTransferFuncs* clang::MakeCFRefCountTF(ASTContext& Ctx, bool GCEnabled,
2681 const LangOptions& lopts) {
Ted Kremenek78d46242008-07-22 16:21:24 +00002682 return new CFRefCount(Ctx, GCEnabled, lopts);
Ted Kremenek3ea0b6a2008-04-10 22:58:08 +00002683}