Chris Lattner | be1a7a0 | 2008-03-15 23:59:48 +0000 | [diff] [blame] | 1 | // CFRefCount.cpp - Transfer functions for tracking simple values -*- C++ -*--// |
Ted Kremenek | 827f93b | 2008-03-06 00:08:09 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
Gabor Greif | 2224fcb | 2008-03-06 10:40:09 +0000 | [diff] [blame] | 10 | // This file defines the methods for CFRefCount, which implements |
Ted Kremenek | 827f93b | 2008-03-06 00:08:09 +0000 | [diff] [blame] | 11 | // a reference count checker for Core Foundation (Mac OS X). |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
Ted Kremenek | a7338b4 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 15 | #include "GRSimpleVals.h" |
Ted Kremenek | fe30beb | 2008-04-30 23:47:44 +0000 | [diff] [blame] | 16 | #include "clang/Basic/LangOptions.h" |
Ted Kremenek | fe4d231 | 2008-05-01 23:13:35 +0000 | [diff] [blame] | 17 | #include "clang/Basic/SourceManager.h" |
Ted Kremenek | a42be30 | 2009-02-14 01:43:44 +0000 | [diff] [blame] | 18 | #include "clang/Analysis/PathSensitive/GRExprEngineBuilders.h" |
Ted Kremenek | 9178120 | 2008-08-17 03:20:02 +0000 | [diff] [blame] | 19 | #include "clang/Analysis/PathSensitive/GRStateTrait.h" |
Ted Kremenek | dd0126b | 2008-03-31 18:26:32 +0000 | [diff] [blame] | 20 | #include "clang/Analysis/PathDiagnostic.h" |
Ted Kremenek | 827f93b | 2008-03-06 00:08:09 +0000 | [diff] [blame] | 21 | #include "clang/Analysis/LocalCheckers.h" |
Ted Kremenek | 10fe66d | 2008-04-09 01:10:13 +0000 | [diff] [blame] | 22 | #include "clang/Analysis/PathDiagnostic.h" |
| 23 | #include "clang/Analysis/PathSensitive/BugReporter.h" |
Ted Kremenek | 2ddb4b2 | 2009-02-14 03:16:10 +0000 | [diff] [blame] | 24 | #include "clang/Analysis/PathSensitive/SymbolManager.h" |
Ted Kremenek | c3bc6c8 | 2009-05-06 21:39:49 +0000 | [diff] [blame] | 25 | #include "clang/AST/DeclObjC.h" |
Ted Kremenek | a7338b4 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 26 | #include "llvm/ADT/DenseMap.h" |
| 27 | #include "llvm/ADT/FoldingSet.h" |
| 28 | #include "llvm/ADT/ImmutableMap.h" |
Ted Kremenek | c8c8d2c | 2008-10-21 15:53:15 +0000 | [diff] [blame] | 29 | #include "llvm/ADT/ImmutableList.h" |
Ted Kremenek | 2ac4ba6 | 2008-05-07 18:36:45 +0000 | [diff] [blame] | 30 | #include "llvm/ADT/StringExtras.h" |
Ted Kremenek | 10fe66d | 2008-04-09 01:10:13 +0000 | [diff] [blame] | 31 | #include "llvm/Support/Compiler.h" |
Ted Kremenek | d7e2678 | 2008-05-16 18:33:44 +0000 | [diff] [blame] | 32 | #include "llvm/ADT/STLExtras.h" |
Ted Kremenek | 3b11f7a | 2008-03-11 19:44:10 +0000 | [diff] [blame] | 33 | #include <ostream> |
Ted Kremenek | 9449ca9 | 2008-08-12 20:41:56 +0000 | [diff] [blame] | 34 | #include <stdarg.h> |
Ted Kremenek | 827f93b | 2008-03-06 00:08:09 +0000 | [diff] [blame] | 35 | |
| 36 | using namespace clang; |
Ted Kremenek | b6f0954 | 2008-10-24 21:18:08 +0000 | [diff] [blame] | 37 | |
| 38 | //===----------------------------------------------------------------------===// |
| 39 | // Utility functions. |
| 40 | //===----------------------------------------------------------------------===// |
| 41 | |
Ted Kremenek | b6f0954 | 2008-10-24 21:18:08 +0000 | [diff] [blame] | 42 | // The "fundamental rule" for naming conventions of methods: |
| 43 | // (url broken into two lines) |
| 44 | // http://developer.apple.com/documentation/Cocoa/Conceptual/ |
| 45 | // MemoryMgmt/Tasks/MemoryManagementRules.html |
| 46 | // |
| 47 | // "You take ownership of an object if you create it using a method whose name |
| 48 | // begins with “alloc” or “new” or contains “copy” (for example, alloc, |
| 49 | // newObject, or mutableCopy), or if you send it a retain message. You are |
| 50 | // responsible for relinquishing ownership of objects you own using release |
| 51 | // or autorelease. Any other time you receive an object, you must |
| 52 | // not release it." |
| 53 | // |
Ted Kremenek | 4395b45 | 2009-02-21 05:13:43 +0000 | [diff] [blame] | 54 | |
| 55 | using llvm::CStrInCStrNoCase; |
Ted Kremenek | fd42ffc | 2009-02-21 18:26:02 +0000 | [diff] [blame] | 56 | using llvm::StringsEqualNoCase; |
Ted Kremenek | 4395b45 | 2009-02-21 05:13:43 +0000 | [diff] [blame] | 57 | |
| 58 | enum NamingConvention { NoConvention, CreateRule, InitRule }; |
| 59 | |
| 60 | static inline bool isWordEnd(char ch, char prev, char next) { |
| 61 | return ch == '\0' |
| 62 | || (islower(prev) && isupper(ch)) // xxxC |
| 63 | || (isupper(prev) && isupper(ch) && islower(next)) // XXCreate |
| 64 | || !isalpha(ch); |
| 65 | } |
| 66 | |
| 67 | static inline const char* parseWord(const char* s) { |
| 68 | char ch = *s, prev = '\0'; |
| 69 | assert(ch != '\0'); |
| 70 | char next = *(s+1); |
| 71 | while (!isWordEnd(ch, prev, next)) { |
| 72 | prev = ch; |
| 73 | ch = next; |
| 74 | next = *((++s)+1); |
| 75 | } |
| 76 | return s; |
| 77 | } |
| 78 | |
| 79 | static NamingConvention deriveNamingConvention(const char* s) { |
| 80 | // A method/function name may contain a prefix. We don't know it is there, |
| 81 | // however, until we encounter the first '_'. |
| 82 | bool InPossiblePrefix = true; |
| 83 | bool AtBeginning = true; |
| 84 | NamingConvention C = NoConvention; |
| 85 | |
| 86 | while (*s != '\0') { |
| 87 | // Skip '_'. |
| 88 | if (*s == '_') { |
| 89 | if (InPossiblePrefix) { |
| 90 | InPossiblePrefix = false; |
| 91 | AtBeginning = true; |
| 92 | // Discard whatever 'convention' we |
| 93 | // had already derived since it occurs |
| 94 | // in the prefix. |
| 95 | C = NoConvention; |
| 96 | } |
| 97 | ++s; |
| 98 | continue; |
| 99 | } |
| 100 | |
| 101 | // Skip numbers, ':', etc. |
| 102 | if (!isalpha(*s)) { |
| 103 | ++s; |
| 104 | continue; |
| 105 | } |
| 106 | |
| 107 | const char *wordEnd = parseWord(s); |
| 108 | assert(wordEnd > s); |
| 109 | unsigned len = wordEnd - s; |
| 110 | |
| 111 | switch (len) { |
| 112 | default: |
| 113 | break; |
| 114 | case 3: |
| 115 | // Methods starting with 'new' follow the create rule. |
Ted Kremenek | fd42ffc | 2009-02-21 18:26:02 +0000 | [diff] [blame] | 116 | if (AtBeginning && StringsEqualNoCase("new", s, len)) |
Ted Kremenek | 4395b45 | 2009-02-21 05:13:43 +0000 | [diff] [blame] | 117 | C = CreateRule; |
| 118 | break; |
| 119 | case 4: |
| 120 | // Methods starting with 'alloc' or contain 'copy' follow the |
| 121 | // create rule |
Ted Kremenek | 91b7953 | 2009-03-13 20:27:06 +0000 | [diff] [blame] | 122 | if (C == NoConvention && StringsEqualNoCase("copy", s, len)) |
Ted Kremenek | 4395b45 | 2009-02-21 05:13:43 +0000 | [diff] [blame] | 123 | C = CreateRule; |
| 124 | else // Methods starting with 'init' follow the init rule. |
Ted Kremenek | fd42ffc | 2009-02-21 18:26:02 +0000 | [diff] [blame] | 125 | if (AtBeginning && StringsEqualNoCase("init", s, len)) |
Ted Kremenek | 91b7953 | 2009-03-13 20:27:06 +0000 | [diff] [blame] | 126 | C = InitRule; |
| 127 | break; |
| 128 | case 5: |
| 129 | if (AtBeginning && StringsEqualNoCase("alloc", s, len)) |
| 130 | C = CreateRule; |
Ted Kremenek | 4395b45 | 2009-02-21 05:13:43 +0000 | [diff] [blame] | 131 | break; |
| 132 | } |
| 133 | |
| 134 | // If we aren't in the prefix and have a derived convention then just |
| 135 | // return it now. |
| 136 | if (!InPossiblePrefix && C != NoConvention) |
| 137 | return C; |
| 138 | |
| 139 | AtBeginning = false; |
| 140 | s = wordEnd; |
| 141 | } |
| 142 | |
| 143 | // We will get here if there wasn't more than one word |
| 144 | // after the prefix. |
| 145 | return C; |
| 146 | } |
| 147 | |
Ted Kremenek | b6f0954 | 2008-10-24 21:18:08 +0000 | [diff] [blame] | 148 | static bool followsFundamentalRule(const char* s) { |
Ted Kremenek | 4395b45 | 2009-02-21 05:13:43 +0000 | [diff] [blame] | 149 | return deriveNamingConvention(s) == CreateRule; |
Ted Kremenek | cdd3bb2 | 2008-11-05 16:54:44 +0000 | [diff] [blame] | 150 | } |
| 151 | |
Ted Kremenek | 314b195 | 2009-04-29 23:03:22 +0000 | [diff] [blame] | 152 | static const ObjCMethodDecl* |
| 153 | ResolveToInterfaceMethodDecl(const ObjCMethodDecl *MD, ASTContext &Context) { |
| 154 | ObjCInterfaceDecl *ID = |
| 155 | const_cast<ObjCInterfaceDecl*>(MD->getClassInterface()); |
| 156 | |
| 157 | return MD->isInstanceMethod() |
| 158 | ? ID->lookupInstanceMethod(Context, MD->getSelector()) |
| 159 | : ID->lookupClassMethod(Context, MD->getSelector()); |
Ted Kremenek | cdd3bb2 | 2008-11-05 16:54:44 +0000 | [diff] [blame] | 160 | } |
Ted Kremenek | b6f0954 | 2008-10-24 21:18:08 +0000 | [diff] [blame] | 161 | |
Ted Kremenek | 7d421f3 | 2008-04-09 23:49:11 +0000 | [diff] [blame] | 162 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 272aa85 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 163 | // Selector creation functions. |
Ted Kremenek | d9ccf68 | 2008-04-17 18:12:53 +0000 | [diff] [blame] | 164 | //===----------------------------------------------------------------------===// |
| 165 | |
Ted Kremenek | 1bd6ddb | 2008-05-01 18:31:44 +0000 | [diff] [blame] | 166 | static inline Selector GetNullarySelector(const char* name, ASTContext& Ctx) { |
Ted Kremenek | d9ccf68 | 2008-04-17 18:12:53 +0000 | [diff] [blame] | 167 | IdentifierInfo* II = &Ctx.Idents.get(name); |
| 168 | return Ctx.Selectors.getSelector(0, &II); |
| 169 | } |
| 170 | |
Ted Kremenek | 0e344d4 | 2008-05-06 00:30:21 +0000 | [diff] [blame] | 171 | static inline Selector GetUnarySelector(const char* name, ASTContext& Ctx) { |
| 172 | IdentifierInfo* II = &Ctx.Idents.get(name); |
| 173 | return Ctx.Selectors.getSelector(1, &II); |
| 174 | } |
| 175 | |
Ted Kremenek | 272aa85 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 176 | //===----------------------------------------------------------------------===// |
| 177 | // Type querying functions. |
| 178 | //===----------------------------------------------------------------------===// |
| 179 | |
Ted Kremenek | 17144e8 | 2009-01-12 21:45:02 +0000 | [diff] [blame] | 180 | static bool hasPrefix(const char* s, const char* prefix) { |
| 181 | if (!prefix) |
| 182 | return true; |
Ted Kremenek | 62820d8 | 2008-05-07 20:06:41 +0000 | [diff] [blame] | 183 | |
Ted Kremenek | 17144e8 | 2009-01-12 21:45:02 +0000 | [diff] [blame] | 184 | char c = *s; |
| 185 | char cP = *prefix; |
Ted Kremenek | 62820d8 | 2008-05-07 20:06:41 +0000 | [diff] [blame] | 186 | |
Ted Kremenek | 17144e8 | 2009-01-12 21:45:02 +0000 | [diff] [blame] | 187 | while (c != '\0' && cP != '\0') { |
| 188 | if (c != cP) break; |
| 189 | c = *(++s); |
| 190 | cP = *(++prefix); |
| 191 | } |
Ted Kremenek | 62820d8 | 2008-05-07 20:06:41 +0000 | [diff] [blame] | 192 | |
Ted Kremenek | 17144e8 | 2009-01-12 21:45:02 +0000 | [diff] [blame] | 193 | return cP == '\0'; |
Ted Kremenek | 62820d8 | 2008-05-07 20:06:41 +0000 | [diff] [blame] | 194 | } |
| 195 | |
Ted Kremenek | 17144e8 | 2009-01-12 21:45:02 +0000 | [diff] [blame] | 196 | static bool hasSuffix(const char* s, const char* suffix) { |
| 197 | const char* loc = strstr(s, suffix); |
| 198 | return loc && strcmp(suffix, loc) == 0; |
| 199 | } |
| 200 | |
| 201 | static bool isRefType(QualType RetTy, const char* prefix, |
| 202 | ASTContext* Ctx = 0, const char* name = 0) { |
Ted Kremenek | 4c5378c | 2008-07-15 16:50:12 +0000 | [diff] [blame] | 203 | |
Ted Kremenek | 17144e8 | 2009-01-12 21:45:02 +0000 | [diff] [blame] | 204 | if (TypedefType* TD = dyn_cast<TypedefType>(RetTy.getTypePtr())) { |
| 205 | const char* TDName = TD->getDecl()->getIdentifier()->getName(); |
| 206 | return hasPrefix(TDName, prefix) && hasSuffix(TDName, "Ref"); |
| 207 | } |
| 208 | |
| 209 | if (!Ctx || !name) |
Ted Kremenek | 4c5378c | 2008-07-15 16:50:12 +0000 | [diff] [blame] | 210 | return false; |
Ted Kremenek | 17144e8 | 2009-01-12 21:45:02 +0000 | [diff] [blame] | 211 | |
| 212 | // Is the type void*? |
| 213 | const PointerType* PT = RetTy->getAsPointerType(); |
| 214 | if (!(PT->getPointeeType().getUnqualifiedType() == Ctx->VoidTy)) |
Ted Kremenek | 4c5378c | 2008-07-15 16:50:12 +0000 | [diff] [blame] | 215 | return false; |
Ted Kremenek | 17144e8 | 2009-01-12 21:45:02 +0000 | [diff] [blame] | 216 | |
| 217 | // Does the name start with the prefix? |
| 218 | return hasPrefix(name, prefix); |
Ted Kremenek | 4c5378c | 2008-07-15 16:50:12 +0000 | [diff] [blame] | 219 | } |
| 220 | |
Ted Kremenek | d9ccf68 | 2008-04-17 18:12:53 +0000 | [diff] [blame] | 221 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 272aa85 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 222 | // Primitives used for constructing summaries for function/method calls. |
Ted Kremenek | 7d421f3 | 2008-04-09 23:49:11 +0000 | [diff] [blame] | 223 | //===----------------------------------------------------------------------===// |
| 224 | |
Ted Kremenek | 272aa85 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 225 | /// ArgEffect is used to summarize a function/method call's effect on a |
| 226 | /// particular argument. |
Ted Kremenek | 6537a64 | 2009-03-17 19:42:23 +0000 | [diff] [blame] | 227 | enum ArgEffect { Autorelease, Dealloc, DecRef, DecRefMsg, DoNothing, |
| 228 | DoNothingByRef, IncRefMsg, IncRef, MakeCollectable, MayEscape, |
| 229 | NewAutoreleasePool, SelfOwn, StopTracking }; |
Ted Kremenek | 272aa85 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 230 | |
Ted Kremenek | a7338b4 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 231 | namespace llvm { |
Ted Kremenek | a56ae16 | 2009-05-03 05:20:50 +0000 | [diff] [blame] | 232 | template <> struct FoldingSetTrait<ArgEffect> { |
| 233 | static inline void Profile(const ArgEffect X, FoldingSetNodeID& ID) { |
| 234 | ID.AddInteger((unsigned) X); |
| 235 | } |
Ted Kremenek | 272aa85 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 236 | }; |
Ted Kremenek | a7338b4 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 237 | } // end llvm namespace |
| 238 | |
Ted Kremenek | a56ae16 | 2009-05-03 05:20:50 +0000 | [diff] [blame] | 239 | /// ArgEffects summarizes the effects of a function/method call on all of |
| 240 | /// its arguments. |
| 241 | typedef llvm::ImmutableMap<unsigned,ArgEffect> ArgEffects; |
| 242 | |
Ted Kremenek | a7338b4 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 243 | namespace { |
Ted Kremenek | 272aa85 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 244 | |
| 245 | /// RetEffect is used to summarize a function/method call's behavior with |
| 246 | /// respect to its return value. |
| 247 | class VISIBILITY_HIDDEN RetEffect { |
Ted Kremenek | a7338b4 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 248 | public: |
Ted Kremenek | 6a1cc25 | 2008-06-23 18:02:52 +0000 | [diff] [blame] | 249 | enum Kind { NoRet, Alias, OwnedSymbol, OwnedAllocatedSymbol, |
Ted Kremenek | 382fb4e | 2009-04-27 19:14:45 +0000 | [diff] [blame] | 250 | NotOwnedSymbol, GCNotOwnedSymbol, ReceiverAlias }; |
Ted Kremenek | 68621b9 | 2009-01-28 05:56:51 +0000 | [diff] [blame] | 251 | |
| 252 | enum ObjKind { CF, ObjC, AnyObj }; |
| 253 | |
Ted Kremenek | a7338b4 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 254 | private: |
Ted Kremenek | 68621b9 | 2009-01-28 05:56:51 +0000 | [diff] [blame] | 255 | Kind K; |
| 256 | ObjKind O; |
| 257 | unsigned index; |
| 258 | |
| 259 | RetEffect(Kind k, unsigned idx = 0) : K(k), O(AnyObj), index(idx) {} |
| 260 | RetEffect(Kind k, ObjKind o) : K(k), O(o), index(0) {} |
Ted Kremenek | 827f93b | 2008-03-06 00:08:09 +0000 | [diff] [blame] | 261 | |
Ted Kremenek | a7338b4 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 262 | public: |
Ted Kremenek | 68621b9 | 2009-01-28 05:56:51 +0000 | [diff] [blame] | 263 | Kind getKind() const { return K; } |
| 264 | |
| 265 | ObjKind getObjKind() const { return O; } |
Ted Kremenek | 272aa85 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 266 | |
| 267 | unsigned getIndex() const { |
Ted Kremenek | a7338b4 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 268 | assert(getKind() == Alias); |
Ted Kremenek | 68621b9 | 2009-01-28 05:56:51 +0000 | [diff] [blame] | 269 | return index; |
Ted Kremenek | a7338b4 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 270 | } |
Ted Kremenek | 827f93b | 2008-03-06 00:08:09 +0000 | [diff] [blame] | 271 | |
Ted Kremenek | 314b195 | 2009-04-29 23:03:22 +0000 | [diff] [blame] | 272 | bool isOwned() const { |
| 273 | return K == OwnedSymbol || K == OwnedAllocatedSymbol; |
| 274 | } |
| 275 | |
Ted Kremenek | 272aa85 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 276 | static RetEffect MakeAlias(unsigned Idx) { |
| 277 | return RetEffect(Alias, Idx); |
| 278 | } |
| 279 | static RetEffect MakeReceiverAlias() { |
| 280 | return RetEffect(ReceiverAlias); |
| 281 | } |
Ted Kremenek | 68621b9 | 2009-01-28 05:56:51 +0000 | [diff] [blame] | 282 | static RetEffect MakeOwned(ObjKind o, bool isAllocated = false) { |
| 283 | return RetEffect(isAllocated ? OwnedAllocatedSymbol : OwnedSymbol, o); |
Ted Kremenek | 272aa85 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 284 | } |
Ted Kremenek | 68621b9 | 2009-01-28 05:56:51 +0000 | [diff] [blame] | 285 | static RetEffect MakeNotOwned(ObjKind o) { |
| 286 | return RetEffect(NotOwnedSymbol, o); |
Ted Kremenek | 382fb4e | 2009-04-27 19:14:45 +0000 | [diff] [blame] | 287 | } |
| 288 | static RetEffect MakeGCNotOwned() { |
| 289 | return RetEffect(GCNotOwnedSymbol, ObjC); |
| 290 | } |
| 291 | |
Ted Kremenek | 272aa85 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 292 | static RetEffect MakeNoRet() { |
| 293 | return RetEffect(NoRet); |
Ted Kremenek | 6a1cc25 | 2008-06-23 18:02:52 +0000 | [diff] [blame] | 294 | } |
Ted Kremenek | 827f93b | 2008-03-06 00:08:09 +0000 | [diff] [blame] | 295 | |
Ted Kremenek | 272aa85 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 296 | void Profile(llvm::FoldingSetNodeID& ID) const { |
Ted Kremenek | 68621b9 | 2009-01-28 05:56:51 +0000 | [diff] [blame] | 297 | ID.AddInteger((unsigned)K); |
| 298 | ID.AddInteger((unsigned)O); |
| 299 | ID.AddInteger(index); |
Ted Kremenek | 272aa85 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 300 | } |
Ted Kremenek | a7338b4 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 301 | }; |
Ted Kremenek | a7338b4 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 302 | |
Ted Kremenek | 272aa85 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 303 | |
Ted Kremenek | 2f22673 | 2009-05-04 05:31:22 +0000 | [diff] [blame] | 304 | class VISIBILITY_HIDDEN RetainSummary { |
Ted Kremenek | bcaff79 | 2008-05-06 15:44:25 +0000 | [diff] [blame] | 305 | /// Args - an ordered vector of (index, ArgEffect) pairs, where index |
| 306 | /// specifies the argument (starting from 0). This can be sparsely |
| 307 | /// populated; arguments with no entry in Args use 'DefaultArgEffect'. |
Ted Kremenek | a56ae16 | 2009-05-03 05:20:50 +0000 | [diff] [blame] | 308 | ArgEffects Args; |
Ted Kremenek | bcaff79 | 2008-05-06 15:44:25 +0000 | [diff] [blame] | 309 | |
| 310 | /// DefaultArgEffect - The default ArgEffect to apply to arguments that |
| 311 | /// do not have an entry in Args. |
| 312 | ArgEffect DefaultArgEffect; |
| 313 | |
Ted Kremenek | 272aa85 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 314 | /// Receiver - If this summary applies to an Objective-C message expression, |
| 315 | /// this is the effect applied to the state of the receiver. |
Ted Kremenek | 266d8b6 | 2008-05-06 02:26:56 +0000 | [diff] [blame] | 316 | ArgEffect Receiver; |
Ted Kremenek | 272aa85 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 317 | |
| 318 | /// Ret - The effect on the return value. Used to indicate if the |
| 319 | /// function/method call returns a new tracked symbol, returns an |
| 320 | /// alias of one of the arguments in the call, and so on. |
Ted Kremenek | a7338b4 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 321 | RetEffect Ret; |
Ted Kremenek | 272aa85 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 322 | |
Ted Kremenek | f2717b0 | 2008-07-18 17:24:20 +0000 | [diff] [blame] | 323 | /// EndPath - Indicates that execution of this method/function should |
| 324 | /// terminate the simulation of a path. |
| 325 | bool EndPath; |
| 326 | |
Ted Kremenek | a7338b4 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 327 | public: |
Ted Kremenek | a56ae16 | 2009-05-03 05:20:50 +0000 | [diff] [blame] | 328 | RetainSummary(ArgEffects A, RetEffect R, ArgEffect defaultEff, |
Ted Kremenek | f2717b0 | 2008-07-18 17:24:20 +0000 | [diff] [blame] | 329 | ArgEffect ReceiverEff, bool endpath = false) |
| 330 | : Args(A), DefaultArgEffect(defaultEff), Receiver(ReceiverEff), Ret(R), |
| 331 | EndPath(endpath) {} |
Ted Kremenek | a7338b4 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 332 | |
Ted Kremenek | 272aa85 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 333 | /// getArg - Return the argument effect on the argument specified by |
| 334 | /// idx (starting from 0). |
Ted Kremenek | 0d72157 | 2008-03-11 17:48:22 +0000 | [diff] [blame] | 335 | ArgEffect getArg(unsigned idx) const { |
Ted Kremenek | a56ae16 | 2009-05-03 05:20:50 +0000 | [diff] [blame] | 336 | if (const ArgEffect *AE = Args.lookup(idx)) |
| 337 | return *AE; |
Ted Kremenek | ae855d4 | 2008-04-24 17:22:33 +0000 | [diff] [blame] | 338 | |
Ted Kremenek | bcaff79 | 2008-05-06 15:44:25 +0000 | [diff] [blame] | 339 | return DefaultArgEffect; |
Ted Kremenek | 0d72157 | 2008-03-11 17:48:22 +0000 | [diff] [blame] | 340 | } |
| 341 | |
Ted Kremenek | 2f22673 | 2009-05-04 05:31:22 +0000 | [diff] [blame] | 342 | /// setDefaultArgEffect - Set the default argument effect. |
| 343 | void setDefaultArgEffect(ArgEffect E) { |
| 344 | DefaultArgEffect = E; |
| 345 | } |
| 346 | |
| 347 | /// setArg - Set the argument effect on the argument specified by idx. |
| 348 | void setArgEffect(ArgEffects::Factory& AF, unsigned idx, ArgEffect E) { |
| 349 | Args = AF.Add(Args, idx, E); |
| 350 | } |
| 351 | |
Ted Kremenek | 272aa85 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 352 | /// getRetEffect - Returns the effect on the return value of the call. |
Ted Kremenek | a56ae16 | 2009-05-03 05:20:50 +0000 | [diff] [blame] | 353 | RetEffect getRetEffect() const { return Ret; } |
Ted Kremenek | ce3ed1e | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 354 | |
Ted Kremenek | 2f22673 | 2009-05-04 05:31:22 +0000 | [diff] [blame] | 355 | /// setRetEffect - Set the effect of the return value of the call. |
| 356 | void setRetEffect(RetEffect E) { Ret = E; } |
| 357 | |
Ted Kremenek | f2717b0 | 2008-07-18 17:24:20 +0000 | [diff] [blame] | 358 | /// isEndPath - Returns true if executing the given method/function should |
| 359 | /// terminate the path. |
| 360 | bool isEndPath() const { return EndPath; } |
| 361 | |
Ted Kremenek | 272aa85 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 362 | /// getReceiverEffect - Returns the effect on the receiver of the call. |
| 363 | /// This is only meaningful if the summary applies to an ObjCMessageExpr*. |
Ted Kremenek | a56ae16 | 2009-05-03 05:20:50 +0000 | [diff] [blame] | 364 | ArgEffect getReceiverEffect() const { return Receiver; } |
Ted Kremenek | 266d8b6 | 2008-05-06 02:26:56 +0000 | [diff] [blame] | 365 | |
Ted Kremenek | 2f22673 | 2009-05-04 05:31:22 +0000 | [diff] [blame] | 366 | /// setReceiverEffect - Set the effect on the receiver of the call. |
| 367 | void setReceiverEffect(ArgEffect E) { Receiver = E; } |
| 368 | |
Ted Kremenek | a56ae16 | 2009-05-03 05:20:50 +0000 | [diff] [blame] | 369 | typedef ArgEffects::iterator ExprIterator; |
Ted Kremenek | a7338b4 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 370 | |
Ted Kremenek | a56ae16 | 2009-05-03 05:20:50 +0000 | [diff] [blame] | 371 | ExprIterator begin_args() const { return Args.begin(); } |
| 372 | ExprIterator end_args() const { return Args.end(); } |
Ted Kremenek | a7338b4 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 373 | |
Ted Kremenek | a56ae16 | 2009-05-03 05:20:50 +0000 | [diff] [blame] | 374 | static void Profile(llvm::FoldingSetNodeID& ID, ArgEffects A, |
Ted Kremenek | bcaff79 | 2008-05-06 15:44:25 +0000 | [diff] [blame] | 375 | RetEffect RetEff, ArgEffect DefaultEff, |
Ted Kremenek | 6fbecac | 2008-07-18 17:39:56 +0000 | [diff] [blame] | 376 | ArgEffect ReceiverEff, bool EndPath) { |
Ted Kremenek | a56ae16 | 2009-05-03 05:20:50 +0000 | [diff] [blame] | 377 | ID.Add(A); |
Ted Kremenek | 266d8b6 | 2008-05-06 02:26:56 +0000 | [diff] [blame] | 378 | ID.Add(RetEff); |
Ted Kremenek | bcaff79 | 2008-05-06 15:44:25 +0000 | [diff] [blame] | 379 | ID.AddInteger((unsigned) DefaultEff); |
Ted Kremenek | 266d8b6 | 2008-05-06 02:26:56 +0000 | [diff] [blame] | 380 | ID.AddInteger((unsigned) ReceiverEff); |
Ted Kremenek | 6fbecac | 2008-07-18 17:39:56 +0000 | [diff] [blame] | 381 | ID.AddInteger((unsigned) EndPath); |
Ted Kremenek | a7338b4 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 382 | } |
| 383 | |
| 384 | void Profile(llvm::FoldingSetNodeID& ID) const { |
Ted Kremenek | 6fbecac | 2008-07-18 17:39:56 +0000 | [diff] [blame] | 385 | Profile(ID, Args, Ret, DefaultArgEffect, Receiver, EndPath); |
Ted Kremenek | a7338b4 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 386 | } |
| 387 | }; |
Ted Kremenek | 84f010c | 2008-06-23 23:30:29 +0000 | [diff] [blame] | 388 | } // end anonymous namespace |
Ted Kremenek | a7338b4 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 389 | |
Ted Kremenek | 272aa85 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 390 | //===----------------------------------------------------------------------===// |
| 391 | // Data structures for constructing summaries. |
| 392 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 9f0fc79 | 2008-06-24 03:49:48 +0000 | [diff] [blame] | 393 | |
Ted Kremenek | 272aa85 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 394 | namespace { |
| 395 | class VISIBILITY_HIDDEN ObjCSummaryKey { |
| 396 | IdentifierInfo* II; |
| 397 | Selector S; |
| 398 | public: |
| 399 | ObjCSummaryKey(IdentifierInfo* ii, Selector s) |
| 400 | : II(ii), S(s) {} |
| 401 | |
Ted Kremenek | 314b195 | 2009-04-29 23:03:22 +0000 | [diff] [blame] | 402 | ObjCSummaryKey(const ObjCInterfaceDecl* d, Selector s) |
Ted Kremenek | 272aa85 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 403 | : II(d ? d->getIdentifier() : 0), S(s) {} |
| 404 | |
| 405 | ObjCSummaryKey(Selector s) |
| 406 | : II(0), S(s) {} |
| 407 | |
| 408 | IdentifierInfo* getIdentifier() const { return II; } |
| 409 | Selector getSelector() const { return S; } |
| 410 | }; |
Ted Kremenek | 84f010c | 2008-06-23 23:30:29 +0000 | [diff] [blame] | 411 | } |
| 412 | |
| 413 | namespace llvm { |
Ted Kremenek | 272aa85 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 414 | template <> struct DenseMapInfo<ObjCSummaryKey> { |
| 415 | static inline ObjCSummaryKey getEmptyKey() { |
| 416 | return ObjCSummaryKey(DenseMapInfo<IdentifierInfo*>::getEmptyKey(), |
| 417 | DenseMapInfo<Selector>::getEmptyKey()); |
| 418 | } |
Ted Kremenek | 84f010c | 2008-06-23 23:30:29 +0000 | [diff] [blame] | 419 | |
Ted Kremenek | 272aa85 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 420 | static inline ObjCSummaryKey getTombstoneKey() { |
| 421 | return ObjCSummaryKey(DenseMapInfo<IdentifierInfo*>::getTombstoneKey(), |
| 422 | DenseMapInfo<Selector>::getTombstoneKey()); |
| 423 | } |
| 424 | |
| 425 | static unsigned getHashValue(const ObjCSummaryKey &V) { |
| 426 | return (DenseMapInfo<IdentifierInfo*>::getHashValue(V.getIdentifier()) |
| 427 | & 0x88888888) |
| 428 | | (DenseMapInfo<Selector>::getHashValue(V.getSelector()) |
| 429 | & 0x55555555); |
| 430 | } |
| 431 | |
| 432 | static bool isEqual(const ObjCSummaryKey& LHS, const ObjCSummaryKey& RHS) { |
| 433 | return DenseMapInfo<IdentifierInfo*>::isEqual(LHS.getIdentifier(), |
| 434 | RHS.getIdentifier()) && |
| 435 | DenseMapInfo<Selector>::isEqual(LHS.getSelector(), |
| 436 | RHS.getSelector()); |
| 437 | } |
| 438 | |
| 439 | static bool isPod() { |
| 440 | return DenseMapInfo<ObjCInterfaceDecl*>::isPod() && |
| 441 | DenseMapInfo<Selector>::isPod(); |
| 442 | } |
| 443 | }; |
Ted Kremenek | 84f010c | 2008-06-23 23:30:29 +0000 | [diff] [blame] | 444 | } // end llvm namespace |
Ted Kremenek | a7338b4 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 445 | |
Ted Kremenek | 84f010c | 2008-06-23 23:30:29 +0000 | [diff] [blame] | 446 | namespace { |
Ted Kremenek | 272aa85 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 447 | class VISIBILITY_HIDDEN ObjCSummaryCache { |
| 448 | typedef llvm::DenseMap<ObjCSummaryKey, RetainSummary*> MapTy; |
| 449 | MapTy M; |
| 450 | public: |
| 451 | ObjCSummaryCache() {} |
| 452 | |
| 453 | typedef MapTy::iterator iterator; |
| 454 | |
Ted Kremenek | 314b195 | 2009-04-29 23:03:22 +0000 | [diff] [blame] | 455 | iterator find(const ObjCInterfaceDecl* D, IdentifierInfo *ClsName, |
| 456 | Selector S) { |
Ted Kremenek | a821b79 | 2009-04-29 05:04:30 +0000 | [diff] [blame] | 457 | // Lookup the method using the decl for the class @interface. If we |
| 458 | // have no decl, lookup using the class name. |
| 459 | return D ? find(D, S) : find(ClsName, S); |
| 460 | } |
| 461 | |
Ted Kremenek | 314b195 | 2009-04-29 23:03:22 +0000 | [diff] [blame] | 462 | iterator find(const ObjCInterfaceDecl* D, Selector S) { |
Ted Kremenek | 272aa85 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 463 | // Do a lookup with the (D,S) pair. If we find a match return |
| 464 | // the iterator. |
| 465 | ObjCSummaryKey K(D, S); |
| 466 | MapTy::iterator I = M.find(K); |
| 467 | |
| 468 | if (I != M.end() || !D) |
| 469 | return I; |
| 470 | |
| 471 | // Walk the super chain. If we find a hit with a parent, we'll end |
| 472 | // up returning that summary. We actually allow that key (null,S), as |
| 473 | // we cache summaries for the null ObjCInterfaceDecl* to allow us to |
| 474 | // generate initial summaries without having to worry about NSObject |
| 475 | // being declared. |
| 476 | // FIXME: We may change this at some point. |
| 477 | for (ObjCInterfaceDecl* C=D->getSuperClass() ;; C=C->getSuperClass()) { |
| 478 | if ((I = M.find(ObjCSummaryKey(C, S))) != M.end()) |
| 479 | break; |
| 480 | |
| 481 | if (!C) |
| 482 | return I; |
| 483 | } |
| 484 | |
| 485 | // Cache the summary with original key to make the next lookup faster |
| 486 | // and return the iterator. |
| 487 | M[K] = I->second; |
| 488 | return I; |
| 489 | } |
| 490 | |
Ted Kremenek | 9449ca9 | 2008-08-12 20:41:56 +0000 | [diff] [blame] | 491 | |
Ted Kremenek | 272aa85 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 492 | iterator find(Expr* Receiver, Selector S) { |
| 493 | return find(getReceiverDecl(Receiver), S); |
| 494 | } |
| 495 | |
| 496 | iterator find(IdentifierInfo* II, Selector S) { |
| 497 | // FIXME: Class method lookup. Right now we dont' have a good way |
| 498 | // of going between IdentifierInfo* and the class hierarchy. |
| 499 | iterator I = M.find(ObjCSummaryKey(II, S)); |
| 500 | return I == M.end() ? M.find(ObjCSummaryKey(S)) : I; |
| 501 | } |
| 502 | |
| 503 | ObjCInterfaceDecl* getReceiverDecl(Expr* E) { |
| 504 | |
| 505 | const PointerType* PT = E->getType()->getAsPointerType(); |
| 506 | if (!PT) return 0; |
| 507 | |
| 508 | ObjCInterfaceType* OI = dyn_cast<ObjCInterfaceType>(PT->getPointeeType()); |
| 509 | if (!OI) return 0; |
| 510 | |
| 511 | return OI ? OI->getDecl() : 0; |
| 512 | } |
| 513 | |
| 514 | iterator end() { return M.end(); } |
| 515 | |
| 516 | RetainSummary*& operator[](ObjCMessageExpr* ME) { |
| 517 | |
| 518 | Selector S = ME->getSelector(); |
| 519 | |
| 520 | if (Expr* Receiver = ME->getReceiver()) { |
| 521 | ObjCInterfaceDecl* OD = getReceiverDecl(Receiver); |
| 522 | return OD ? M[ObjCSummaryKey(OD->getIdentifier(), S)] : M[S]; |
| 523 | } |
| 524 | |
| 525 | return M[ObjCSummaryKey(ME->getClassName(), S)]; |
| 526 | } |
| 527 | |
| 528 | RetainSummary*& operator[](ObjCSummaryKey K) { |
| 529 | return M[K]; |
| 530 | } |
| 531 | |
| 532 | RetainSummary*& operator[](Selector S) { |
| 533 | return M[ ObjCSummaryKey(S) ]; |
| 534 | } |
| 535 | }; |
| 536 | } // end anonymous namespace |
| 537 | |
| 538 | //===----------------------------------------------------------------------===// |
| 539 | // Data structures for managing collections of summaries. |
| 540 | //===----------------------------------------------------------------------===// |
| 541 | |
| 542 | namespace { |
| 543 | class VISIBILITY_HIDDEN RetainSummaryManager { |
Ted Kremenek | a8c3c43 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 544 | |
| 545 | //==-----------------------------------------------------------------==// |
| 546 | // Typedefs. |
| 547 | //==-----------------------------------------------------------------==// |
Ted Kremenek | a7338b4 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 548 | |
Ted Kremenek | a8c3c43 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 549 | typedef llvm::DenseMap<FunctionDecl*, RetainSummary*> |
| 550 | FuncSummariesTy; |
| 551 | |
Ted Kremenek | 84f010c | 2008-06-23 23:30:29 +0000 | [diff] [blame] | 552 | typedef ObjCSummaryCache ObjCMethodSummariesTy; |
Ted Kremenek | a8c3c43 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 553 | |
| 554 | //==-----------------------------------------------------------------==// |
| 555 | // Data. |
| 556 | //==-----------------------------------------------------------------==// |
| 557 | |
Ted Kremenek | 272aa85 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 558 | /// Ctx - The ASTContext object for the analyzed ASTs. |
Ted Kremenek | 9b0c09c | 2008-04-29 05:33:51 +0000 | [diff] [blame] | 559 | ASTContext& Ctx; |
Ted Kremenek | e44927e | 2008-07-01 17:21:27 +0000 | [diff] [blame] | 560 | |
Ted Kremenek | ede40b7 | 2008-07-09 18:11:16 +0000 | [diff] [blame] | 561 | /// CFDictionaryCreateII - An IdentifierInfo* representing the indentifier |
| 562 | /// "CFDictionaryCreate". |
| 563 | IdentifierInfo* CFDictionaryCreateII; |
| 564 | |
Ted Kremenek | 272aa85 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 565 | /// GCEnabled - Records whether or not the analyzed code runs in GC mode. |
Ted Kremenek | 9b0c09c | 2008-04-29 05:33:51 +0000 | [diff] [blame] | 566 | const bool GCEnabled; |
Ted Kremenek | ee64908 | 2009-05-04 04:30:18 +0000 | [diff] [blame] | 567 | |
Ted Kremenek | 272aa85 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 568 | /// FuncSummaries - A map from FunctionDecls to summaries. |
Ted Kremenek | a8c3c43 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 569 | FuncSummariesTy FuncSummaries; |
| 570 | |
Ted Kremenek | 272aa85 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 571 | /// ObjCClassMethodSummaries - A map from selectors (for instance methods) |
| 572 | /// to summaries. |
Ted Kremenek | 97c1e0c | 2008-06-23 22:21:20 +0000 | [diff] [blame] | 573 | ObjCMethodSummariesTy ObjCClassMethodSummaries; |
Ted Kremenek | a8c3c43 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 574 | |
Ted Kremenek | 272aa85 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 575 | /// ObjCMethodSummaries - A map from selectors to summaries. |
Ted Kremenek | 97c1e0c | 2008-06-23 22:21:20 +0000 | [diff] [blame] | 576 | ObjCMethodSummariesTy ObjCMethodSummaries; |
Ted Kremenek | a8c3c43 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 577 | |
Ted Kremenek | 272aa85 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 578 | /// BPAlloc - A BumpPtrAllocator used for allocating summaries, ArgEffects, |
| 579 | /// and all other data used by the checker. |
Ted Kremenek | a8c3c43 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 580 | llvm::BumpPtrAllocator BPAlloc; |
| 581 | |
Ted Kremenek | a56ae16 | 2009-05-03 05:20:50 +0000 | [diff] [blame] | 582 | /// AF - A factory for ArgEffects objects. |
| 583 | ArgEffects::Factory AF; |
| 584 | |
Ted Kremenek | 272aa85 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 585 | /// ScratchArgs - A holding buffer for construct ArgEffects. |
Ted Kremenek | a8c3c43 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 586 | ArgEffects ScratchArgs; |
| 587 | |
Ted Kremenek | 5535e5e | 2009-05-07 23:40:42 +0000 | [diff] [blame] | 588 | /// ObjCAllocRetE - Default return effect for methods returning Objective-C |
| 589 | /// objects. |
| 590 | RetEffect ObjCAllocRetE; |
| 591 | |
Ted Kremenek | 286e985 | 2009-05-04 04:57:00 +0000 | [diff] [blame] | 592 | RetainSummary DefaultSummary; |
Ted Kremenek | b3a44e7 | 2008-05-06 18:11:36 +0000 | [diff] [blame] | 593 | RetainSummary* StopSummary; |
| 594 | |
Ted Kremenek | a8c3c43 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 595 | //==-----------------------------------------------------------------==// |
| 596 | // Methods. |
| 597 | //==-----------------------------------------------------------------==// |
| 598 | |
Ted Kremenek | 272aa85 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 599 | /// getArgEffects - Returns a persistent ArgEffects object based on the |
| 600 | /// data in ScratchArgs. |
Ted Kremenek | a56ae16 | 2009-05-03 05:20:50 +0000 | [diff] [blame] | 601 | ArgEffects getArgEffects(); |
Ted Kremenek | a7338b4 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 602 | |
Ted Kremenek | 562c130 | 2008-05-05 16:51:50 +0000 | [diff] [blame] | 603 | enum UnaryFuncKind { cfretain, cfrelease, cfmakecollectable }; |
Ted Kremenek | 63d09ae | 2008-10-23 01:56:15 +0000 | [diff] [blame] | 604 | |
| 605 | public: |
Ted Kremenek | 2f22673 | 2009-05-04 05:31:22 +0000 | [diff] [blame] | 606 | RetainSummary *getDefaultSummary() { |
| 607 | RetainSummary *Summ = (RetainSummary*) BPAlloc.Allocate<RetainSummary>(); |
| 608 | return new (Summ) RetainSummary(DefaultSummary); |
| 609 | } |
Ted Kremenek | 286e985 | 2009-05-04 04:57:00 +0000 | [diff] [blame] | 610 | |
Ted Kremenek | 064ef32 | 2009-02-23 16:51:39 +0000 | [diff] [blame] | 611 | RetainSummary* getUnarySummary(const FunctionType* FT, UnaryFuncKind func); |
Ted Kremenek | ce3ed1e | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 612 | |
Ted Kremenek | a8c3c43 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 613 | RetainSummary* getCFSummaryCreateRule(FunctionDecl* FD); |
| 614 | RetainSummary* getCFSummaryGetRule(FunctionDecl* FD); |
Ted Kremenek | 17144e8 | 2009-01-12 21:45:02 +0000 | [diff] [blame] | 615 | RetainSummary* getCFCreateGetRuleSummary(FunctionDecl* FD, const char* FName); |
Ted Kremenek | ce3ed1e | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 616 | |
Ted Kremenek | a56ae16 | 2009-05-03 05:20:50 +0000 | [diff] [blame] | 617 | RetainSummary* getPersistentSummary(ArgEffects AE, RetEffect RetEff, |
Ted Kremenek | bcaff79 | 2008-05-06 15:44:25 +0000 | [diff] [blame] | 618 | ArgEffect ReceiverEff = DoNothing, |
Ted Kremenek | f2717b0 | 2008-07-18 17:24:20 +0000 | [diff] [blame] | 619 | ArgEffect DefaultEff = MayEscape, |
| 620 | bool isEndPath = false); |
Ted Kremenek | 45d0b50 | 2008-10-29 04:07:07 +0000 | [diff] [blame] | 621 | |
Ted Kremenek | 266d8b6 | 2008-05-06 02:26:56 +0000 | [diff] [blame] | 622 | RetainSummary* getPersistentSummary(RetEffect RE, |
Ted Kremenek | bcaff79 | 2008-05-06 15:44:25 +0000 | [diff] [blame] | 623 | ArgEffect ReceiverEff = DoNothing, |
Ted Kremenek | a3f30dd | 2008-05-22 17:31:13 +0000 | [diff] [blame] | 624 | ArgEffect DefaultEff = MayEscape) { |
Ted Kremenek | bcaff79 | 2008-05-06 15:44:25 +0000 | [diff] [blame] | 625 | return getPersistentSummary(getArgEffects(), RE, ReceiverEff, DefaultEff); |
Ted Kremenek | 0e344d4 | 2008-05-06 00:30:21 +0000 | [diff] [blame] | 626 | } |
Ted Kremenek | 42ea032 | 2008-05-05 23:55:01 +0000 | [diff] [blame] | 627 | |
Ted Kremenek | a821b79 | 2009-04-29 05:04:30 +0000 | [diff] [blame] | 628 | RetainSummary *getPersistentStopSummary() { |
Ted Kremenek | b3a44e7 | 2008-05-06 18:11:36 +0000 | [diff] [blame] | 629 | if (StopSummary) |
| 630 | return StopSummary; |
| 631 | |
| 632 | StopSummary = getPersistentSummary(RetEffect::MakeNoRet(), |
| 633 | StopTracking, StopTracking); |
Ted Kremenek | 45d0b50 | 2008-10-29 04:07:07 +0000 | [diff] [blame] | 634 | |
Ted Kremenek | b3a44e7 | 2008-05-06 18:11:36 +0000 | [diff] [blame] | 635 | return StopSummary; |
Ted Kremenek | bcaff79 | 2008-05-06 15:44:25 +0000 | [diff] [blame] | 636 | } |
Ted Kremenek | 926abf2 | 2008-05-06 04:20:12 +0000 | [diff] [blame] | 637 | |
Ted Kremenek | a821b79 | 2009-04-29 05:04:30 +0000 | [diff] [blame] | 638 | RetainSummary *getInitMethodSummary(QualType RetTy); |
Ted Kremenek | 42ea032 | 2008-05-05 23:55:01 +0000 | [diff] [blame] | 639 | |
Ted Kremenek | 97c1e0c | 2008-06-23 22:21:20 +0000 | [diff] [blame] | 640 | void InitializeClassMethodSummaries(); |
| 641 | void InitializeMethodSummaries(); |
Ted Kremenek | 63d09ae | 2008-10-23 01:56:15 +0000 | [diff] [blame] | 642 | |
Ted Kremenek | 9b42e06 | 2009-05-03 04:42:10 +0000 | [diff] [blame] | 643 | bool isTrackedObjCObjectType(QualType T); |
Ted Kremenek | a9cdbc3 | 2009-05-03 06:08:32 +0000 | [diff] [blame] | 644 | bool isTrackedCFObjectType(QualType T); |
Ted Kremenek | 35920ed | 2009-01-07 00:39:56 +0000 | [diff] [blame] | 645 | |
Ted Kremenek | 63d09ae | 2008-10-23 01:56:15 +0000 | [diff] [blame] | 646 | private: |
| 647 | |
Ted Kremenek | f2717b0 | 2008-07-18 17:24:20 +0000 | [diff] [blame] | 648 | void addClsMethSummary(IdentifierInfo* ClsII, Selector S, |
| 649 | RetainSummary* Summ) { |
| 650 | ObjCClassMethodSummaries[ObjCSummaryKey(ClsII, S)] = Summ; |
| 651 | } |
| 652 | |
Ted Kremenek | 272aa85 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 653 | void addNSObjectClsMethSummary(Selector S, RetainSummary *Summ) { |
| 654 | ObjCClassMethodSummaries[S] = Summ; |
| 655 | } |
| 656 | |
| 657 | void addNSObjectMethSummary(Selector S, RetainSummary *Summ) { |
| 658 | ObjCMethodSummaries[S] = Summ; |
| 659 | } |
Ted Kremenek | fbf2dc5 | 2009-03-04 23:30:42 +0000 | [diff] [blame] | 660 | |
| 661 | void addClassMethSummary(const char* Cls, const char* nullaryName, |
| 662 | RetainSummary *Summ) { |
| 663 | IdentifierInfo* ClsII = &Ctx.Idents.get(Cls); |
| 664 | Selector S = GetNullarySelector(nullaryName, Ctx); |
| 665 | ObjCClassMethodSummaries[ObjCSummaryKey(ClsII, S)] = Summ; |
| 666 | } |
Ted Kremenek | 272aa85 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 667 | |
Ted Kremenek | 1b4b656 | 2009-02-25 02:54:57 +0000 | [diff] [blame] | 668 | void addInstMethSummary(const char* Cls, const char* nullaryName, |
| 669 | RetainSummary *Summ) { |
| 670 | IdentifierInfo* ClsII = &Ctx.Idents.get(Cls); |
| 671 | Selector S = GetNullarySelector(nullaryName, Ctx); |
| 672 | ObjCMethodSummaries[ObjCSummaryKey(ClsII, S)] = Summ; |
| 673 | } |
Ted Kremenek | ccbe79a | 2009-04-24 17:50:11 +0000 | [diff] [blame] | 674 | |
| 675 | Selector generateSelector(va_list argp) { |
Ted Kremenek | 3d6ddbb | 2008-08-12 18:30:56 +0000 | [diff] [blame] | 676 | llvm::SmallVector<IdentifierInfo*, 10> II; |
Ted Kremenek | ccbe79a | 2009-04-24 17:50:11 +0000 | [diff] [blame] | 677 | |
Ted Kremenek | 3d6ddbb | 2008-08-12 18:30:56 +0000 | [diff] [blame] | 678 | while (const char* s = va_arg(argp, const char*)) |
| 679 | II.push_back(&Ctx.Idents.get(s)); |
Ted Kremenek | ccbe79a | 2009-04-24 17:50:11 +0000 | [diff] [blame] | 680 | |
| 681 | return Ctx.Selectors.getSelector(II.size(), &II[0]); |
| 682 | } |
| 683 | |
| 684 | void addMethodSummary(IdentifierInfo *ClsII, ObjCMethodSummariesTy& Summaries, |
| 685 | RetainSummary* Summ, va_list argp) { |
| 686 | Selector S = generateSelector(argp); |
| 687 | Summaries[ObjCSummaryKey(ClsII, S)] = Summ; |
Ted Kremenek | f2717b0 | 2008-07-18 17:24:20 +0000 | [diff] [blame] | 688 | } |
Ted Kremenek | 45642a4 | 2008-08-12 18:48:50 +0000 | [diff] [blame] | 689 | |
| 690 | void addInstMethSummary(const char* Cls, RetainSummary* Summ, ...) { |
| 691 | va_list argp; |
| 692 | va_start(argp, Summ); |
Ted Kremenek | ccbe79a | 2009-04-24 17:50:11 +0000 | [diff] [blame] | 693 | addMethodSummary(&Ctx.Idents.get(Cls), ObjCMethodSummaries, Summ, argp); |
Ted Kremenek | 45642a4 | 2008-08-12 18:48:50 +0000 | [diff] [blame] | 694 | va_end(argp); |
| 695 | } |
Ted Kremenek | ccbe79a | 2009-04-24 17:50:11 +0000 | [diff] [blame] | 696 | |
| 697 | void addClsMethSummary(const char* Cls, RetainSummary* Summ, ...) { |
| 698 | va_list argp; |
| 699 | va_start(argp, Summ); |
| 700 | addMethodSummary(&Ctx.Idents.get(Cls),ObjCClassMethodSummaries, Summ, argp); |
| 701 | va_end(argp); |
| 702 | } |
| 703 | |
| 704 | void addClsMethSummary(IdentifierInfo *II, RetainSummary* Summ, ...) { |
| 705 | va_list argp; |
| 706 | va_start(argp, Summ); |
| 707 | addMethodSummary(II, ObjCClassMethodSummaries, Summ, argp); |
| 708 | va_end(argp); |
| 709 | } |
| 710 | |
Ted Kremenek | 3d6ddbb | 2008-08-12 18:30:56 +0000 | [diff] [blame] | 711 | void addPanicSummary(const char* Cls, ...) { |
Ted Kremenek | a56ae16 | 2009-05-03 05:20:50 +0000 | [diff] [blame] | 712 | RetainSummary* Summ = getPersistentSummary(AF.GetEmptyMap(), |
| 713 | RetEffect::MakeNoRet(), |
Ted Kremenek | 3d6ddbb | 2008-08-12 18:30:56 +0000 | [diff] [blame] | 714 | DoNothing, DoNothing, true); |
| 715 | va_list argp; |
| 716 | va_start (argp, Cls); |
Ted Kremenek | ccbe79a | 2009-04-24 17:50:11 +0000 | [diff] [blame] | 717 | addMethodSummary(&Ctx.Idents.get(Cls), ObjCMethodSummaries, Summ, argp); |
Ted Kremenek | 3d6ddbb | 2008-08-12 18:30:56 +0000 | [diff] [blame] | 718 | va_end(argp); |
Ted Kremenek | ccbe79a | 2009-04-24 17:50:11 +0000 | [diff] [blame] | 719 | } |
Ted Kremenek | f2717b0 | 2008-07-18 17:24:20 +0000 | [diff] [blame] | 720 | |
Ted Kremenek | a7338b4 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 721 | public: |
Ted Kremenek | a8c3c43 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 722 | |
| 723 | RetainSummaryManager(ASTContext& ctx, bool gcenabled) |
Ted Kremenek | e44927e | 2008-07-01 17:21:27 +0000 | [diff] [blame] | 724 | : Ctx(ctx), |
Ted Kremenek | ede40b7 | 2008-07-09 18:11:16 +0000 | [diff] [blame] | 725 | CFDictionaryCreateII(&ctx.Idents.get("CFDictionaryCreate")), |
Ted Kremenek | a56ae16 | 2009-05-03 05:20:50 +0000 | [diff] [blame] | 726 | GCEnabled(gcenabled), AF(BPAlloc), ScratchArgs(AF.GetEmptyMap()), |
Ted Kremenek | 5535e5e | 2009-05-07 23:40:42 +0000 | [diff] [blame] | 727 | ObjCAllocRetE(gcenabled ? RetEffect::MakeGCNotOwned() |
| 728 | : RetEffect::MakeOwned(RetEffect::ObjC, true)), |
Ted Kremenek | 286e985 | 2009-05-04 04:57:00 +0000 | [diff] [blame] | 729 | DefaultSummary(AF.GetEmptyMap() /* per-argument effects (none) */, |
| 730 | RetEffect::MakeNoRet() /* return effect */, |
| 731 | DoNothing /* receiver effect */, |
| 732 | MayEscape /* default argument effect */), |
Ted Kremenek | a56ae16 | 2009-05-03 05:20:50 +0000 | [diff] [blame] | 733 | StopSummary(0) { |
Ted Kremenek | 272aa85 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 734 | |
| 735 | InitializeClassMethodSummaries(); |
| 736 | InitializeMethodSummaries(); |
| 737 | } |
Ted Kremenek | 9b0c09c | 2008-04-29 05:33:51 +0000 | [diff] [blame] | 738 | |
Ted Kremenek | a8c3c43 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 739 | ~RetainSummaryManager(); |
Ted Kremenek | a7338b4 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 740 | |
Ted Kremenek | d13c187 | 2008-06-24 03:56:45 +0000 | [diff] [blame] | 741 | RetainSummary* getSummary(FunctionDecl* FD); |
Ted Kremenek | a821b79 | 2009-04-29 05:04:30 +0000 | [diff] [blame] | 742 | |
Ted Kremenek | 314b195 | 2009-04-29 23:03:22 +0000 | [diff] [blame] | 743 | RetainSummary* getInstanceMethodSummary(ObjCMessageExpr* ME, |
| 744 | const ObjCInterfaceDecl* ID) { |
Ted Kremenek | 04e0030 | 2009-04-29 17:09:14 +0000 | [diff] [blame] | 745 | return getInstanceMethodSummary(ME->getSelector(), ME->getClassName(), |
Ted Kremenek | a821b79 | 2009-04-29 05:04:30 +0000 | [diff] [blame] | 746 | ID, ME->getMethodDecl(), ME->getType()); |
| 747 | } |
| 748 | |
Ted Kremenek | 04e0030 | 2009-04-29 17:09:14 +0000 | [diff] [blame] | 749 | RetainSummary* getInstanceMethodSummary(Selector S, IdentifierInfo *ClsName, |
Ted Kremenek | 314b195 | 2009-04-29 23:03:22 +0000 | [diff] [blame] | 750 | const ObjCInterfaceDecl* ID, |
| 751 | const ObjCMethodDecl *MD, |
| 752 | QualType RetTy); |
Ted Kremenek | 578498a | 2009-04-29 00:42:39 +0000 | [diff] [blame] | 753 | |
| 754 | RetainSummary *getClassMethodSummary(Selector S, IdentifierInfo *ClsName, |
Ted Kremenek | 314b195 | 2009-04-29 23:03:22 +0000 | [diff] [blame] | 755 | const ObjCInterfaceDecl *ID, |
| 756 | const ObjCMethodDecl *MD, |
| 757 | QualType RetTy); |
Ted Kremenek | 578498a | 2009-04-29 00:42:39 +0000 | [diff] [blame] | 758 | |
| 759 | RetainSummary *getClassMethodSummary(ObjCMessageExpr *ME) { |
| 760 | return getClassMethodSummary(ME->getSelector(), ME->getClassName(), |
| 761 | ME->getClassInfo().first, |
| 762 | ME->getMethodDecl(), ME->getType()); |
| 763 | } |
Ted Kremenek | 91b89a4 | 2009-04-29 17:17:48 +0000 | [diff] [blame] | 764 | |
| 765 | /// getMethodSummary - This version of getMethodSummary is used to query |
| 766 | /// the summary for the current method being analyzed. |
Ted Kremenek | 314b195 | 2009-04-29 23:03:22 +0000 | [diff] [blame] | 767 | RetainSummary *getMethodSummary(const ObjCMethodDecl *MD) { |
| 768 | // FIXME: Eventually this should be unneeded. |
Ted Kremenek | 314b195 | 2009-04-29 23:03:22 +0000 | [diff] [blame] | 769 | const ObjCInterfaceDecl *ID = MD->getClassInterface(); |
Ted Kremenek | 1447cc9 | 2009-04-30 05:41:14 +0000 | [diff] [blame] | 770 | Selector S = MD->getSelector(); |
Ted Kremenek | 91b89a4 | 2009-04-29 17:17:48 +0000 | [diff] [blame] | 771 | IdentifierInfo *ClsName = ID->getIdentifier(); |
| 772 | QualType ResultTy = MD->getResultType(); |
| 773 | |
Ted Kremenek | 81eb464 | 2009-04-30 05:47:23 +0000 | [diff] [blame] | 774 | // Resolve the method decl last. |
| 775 | if (const ObjCMethodDecl *InterfaceMD = |
| 776 | ResolveToInterfaceMethodDecl(MD, Ctx)) |
| 777 | MD = InterfaceMD; |
Ted Kremenek | 1447cc9 | 2009-04-30 05:41:14 +0000 | [diff] [blame] | 778 | |
Ted Kremenek | 91b89a4 | 2009-04-29 17:17:48 +0000 | [diff] [blame] | 779 | if (MD->isInstanceMethod()) |
| 780 | return getInstanceMethodSummary(S, ClsName, ID, MD, ResultTy); |
| 781 | else |
| 782 | return getClassMethodSummary(S, ClsName, ID, MD, ResultTy); |
| 783 | } |
Ted Kremenek | 578498a | 2009-04-29 00:42:39 +0000 | [diff] [blame] | 784 | |
Ted Kremenek | 314b195 | 2009-04-29 23:03:22 +0000 | [diff] [blame] | 785 | RetainSummary* getCommonMethodSummary(const ObjCMethodDecl* MD, |
| 786 | Selector S, QualType RetTy); |
| 787 | |
Ted Kremenek | a8c3c43 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 788 | bool isGCEnabled() const { return GCEnabled; } |
Ted Kremenek | 2f22673 | 2009-05-04 05:31:22 +0000 | [diff] [blame] | 789 | |
| 790 | RetainSummary *copySummary(RetainSummary *OldSumm) { |
| 791 | RetainSummary *Summ = (RetainSummary*) BPAlloc.Allocate<RetainSummary>(); |
| 792 | new (Summ) RetainSummary(*OldSumm); |
| 793 | return Summ; |
| 794 | } |
Ted Kremenek | a7338b4 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 795 | }; |
| 796 | |
| 797 | } // end anonymous namespace |
| 798 | |
| 799 | //===----------------------------------------------------------------------===// |
| 800 | // Implementation of checker data structures. |
| 801 | //===----------------------------------------------------------------------===// |
| 802 | |
Ted Kremenek | a56ae16 | 2009-05-03 05:20:50 +0000 | [diff] [blame] | 803 | RetainSummaryManager::~RetainSummaryManager() {} |
Ted Kremenek | a7338b4 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 804 | |
Ted Kremenek | a56ae16 | 2009-05-03 05:20:50 +0000 | [diff] [blame] | 805 | ArgEffects RetainSummaryManager::getArgEffects() { |
| 806 | ArgEffects AE = ScratchArgs; |
| 807 | ScratchArgs = AF.GetEmptyMap(); |
| 808 | return AE; |
Ted Kremenek | ce3ed1e | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 809 | } |
| 810 | |
Ted Kremenek | 266d8b6 | 2008-05-06 02:26:56 +0000 | [diff] [blame] | 811 | RetainSummary* |
Ted Kremenek | a56ae16 | 2009-05-03 05:20:50 +0000 | [diff] [blame] | 812 | RetainSummaryManager::getPersistentSummary(ArgEffects AE, RetEffect RetEff, |
Ted Kremenek | bcaff79 | 2008-05-06 15:44:25 +0000 | [diff] [blame] | 813 | ArgEffect ReceiverEff, |
Ted Kremenek | f2717b0 | 2008-07-18 17:24:20 +0000 | [diff] [blame] | 814 | ArgEffect DefaultEff, |
Ted Kremenek | ee64908 | 2009-05-04 04:30:18 +0000 | [diff] [blame] | 815 | bool isEndPath) { |
Ted Kremenek | ae855d4 | 2008-04-24 17:22:33 +0000 | [diff] [blame] | 816 | // Create the summary and return it. |
Ted Kremenek | ee64908 | 2009-05-04 04:30:18 +0000 | [diff] [blame] | 817 | RetainSummary *Summ = (RetainSummary*) BPAlloc.Allocate<RetainSummary>(); |
Ted Kremenek | f2717b0 | 2008-07-18 17:24:20 +0000 | [diff] [blame] | 818 | new (Summ) RetainSummary(AE, RetEff, DefaultEff, ReceiverEff, isEndPath); |
Ted Kremenek | ce3ed1e | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 819 | return Summ; |
| 820 | } |
| 821 | |
Ted Kremenek | a8c3c43 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 822 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 35920ed | 2009-01-07 00:39:56 +0000 | [diff] [blame] | 823 | // Predicates. |
| 824 | //===----------------------------------------------------------------------===// |
| 825 | |
Ted Kremenek | 9b42e06 | 2009-05-03 04:42:10 +0000 | [diff] [blame] | 826 | bool RetainSummaryManager::isTrackedObjCObjectType(QualType Ty) { |
Ted Kremenek | 0d81355 | 2009-04-23 22:11:07 +0000 | [diff] [blame] | 827 | if (!Ctx.isObjCObjectPointerType(Ty)) |
Ted Kremenek | 35920ed | 2009-01-07 00:39:56 +0000 | [diff] [blame] | 828 | return false; |
| 829 | |
Ted Kremenek | 0d81355 | 2009-04-23 22:11:07 +0000 | [diff] [blame] | 830 | // We assume that id<..>, id, and "Class" all represent tracked objects. |
| 831 | const PointerType *PT = Ty->getAsPointerType(); |
| 832 | if (PT == 0) |
| 833 | return true; |
| 834 | |
| 835 | const ObjCInterfaceType *OT = PT->getPointeeType()->getAsObjCInterfaceType(); |
Ted Kremenek | 35920ed | 2009-01-07 00:39:56 +0000 | [diff] [blame] | 836 | |
| 837 | // We assume that id<..>, id, and "Class" all represent tracked objects. |
| 838 | if (!OT) |
| 839 | return true; |
Ted Kremenek | 0d81355 | 2009-04-23 22:11:07 +0000 | [diff] [blame] | 840 | |
| 841 | // Does the interface subclass NSObject? |
Ted Kremenek | 35920ed | 2009-01-07 00:39:56 +0000 | [diff] [blame] | 842 | // FIXME: We can memoize here if this gets too expensive. |
| 843 | IdentifierInfo* NSObjectII = &Ctx.Idents.get("NSObject"); |
| 844 | ObjCInterfaceDecl* ID = OT->getDecl(); |
| 845 | |
| 846 | for ( ; ID ; ID = ID->getSuperClass()) |
| 847 | if (ID->getIdentifier() == NSObjectII) |
| 848 | return true; |
| 849 | |
| 850 | return false; |
| 851 | } |
| 852 | |
Ted Kremenek | a9cdbc3 | 2009-05-03 06:08:32 +0000 | [diff] [blame] | 853 | bool RetainSummaryManager::isTrackedCFObjectType(QualType T) { |
| 854 | return isRefType(T, "CF") || // Core Foundation. |
| 855 | isRefType(T, "CG") || // Core Graphics. |
| 856 | isRefType(T, "DADisk") || // Disk Arbitration API. |
| 857 | isRefType(T, "DADissenter") || |
| 858 | isRefType(T, "DASessionRef"); |
| 859 | } |
| 860 | |
Ted Kremenek | 35920ed | 2009-01-07 00:39:56 +0000 | [diff] [blame] | 861 | //===----------------------------------------------------------------------===// |
Ted Kremenek | a8c3c43 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 862 | // Summary creation for functions (largely uses of Core Foundation). |
| 863 | //===----------------------------------------------------------------------===// |
Ted Kremenek | ce3ed1e | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 864 | |
Ted Kremenek | 17144e8 | 2009-01-12 21:45:02 +0000 | [diff] [blame] | 865 | static bool isRetain(FunctionDecl* FD, const char* FName) { |
| 866 | const char* loc = strstr(FName, "Retain"); |
| 867 | return loc && loc[sizeof("Retain")-1] == '\0'; |
| 868 | } |
| 869 | |
| 870 | static bool isRelease(FunctionDecl* FD, const char* FName) { |
| 871 | const char* loc = strstr(FName, "Release"); |
| 872 | return loc && loc[sizeof("Release")-1] == '\0'; |
| 873 | } |
| 874 | |
Ted Kremenek | d13c187 | 2008-06-24 03:56:45 +0000 | [diff] [blame] | 875 | RetainSummary* RetainSummaryManager::getSummary(FunctionDecl* FD) { |
Ted Kremenek | ae855d4 | 2008-04-24 17:22:33 +0000 | [diff] [blame] | 876 | // Look up a summary in our cache of FunctionDecls -> Summaries. |
Ted Kremenek | a8c3c43 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 877 | FuncSummariesTy::iterator I = FuncSummaries.find(FD); |
Ted Kremenek | a8c3c43 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 878 | if (I != FuncSummaries.end()) |
Ted Kremenek | ae855d4 | 2008-04-24 17:22:33 +0000 | [diff] [blame] | 879 | return I->second; |
| 880 | |
Ted Kremenek | 64cddf1 | 2009-05-04 15:34:07 +0000 | [diff] [blame] | 881 | // No summary? Generate one. |
Ted Kremenek | 17144e8 | 2009-01-12 21:45:02 +0000 | [diff] [blame] | 882 | RetainSummary *S = 0; |
Ted Kremenek | 562c130 | 2008-05-05 16:51:50 +0000 | [diff] [blame] | 883 | |
Ted Kremenek | 4c5378c | 2008-07-15 16:50:12 +0000 | [diff] [blame] | 884 | do { |
Ted Kremenek | 17144e8 | 2009-01-12 21:45:02 +0000 | [diff] [blame] | 885 | // We generate "stop" summaries for implicitly defined functions. |
| 886 | if (FD->isImplicit()) { |
| 887 | S = getPersistentStopSummary(); |
| 888 | break; |
Ted Kremenek | 4c5378c | 2008-07-15 16:50:12 +0000 | [diff] [blame] | 889 | } |
Ted Kremenek | eafcc2f | 2008-11-04 00:36:12 +0000 | [diff] [blame] | 890 | |
Ted Kremenek | 064ef32 | 2009-02-23 16:51:39 +0000 | [diff] [blame] | 891 | // [PR 3337] Use 'getAsFunctionType' to strip away any typedefs on the |
Ted Kremenek | c239b9c | 2009-01-16 18:40:33 +0000 | [diff] [blame] | 892 | // function's type. |
Ted Kremenek | 064ef32 | 2009-02-23 16:51:39 +0000 | [diff] [blame] | 893 | const FunctionType* FT = FD->getType()->getAsFunctionType(); |
Ted Kremenek | 17144e8 | 2009-01-12 21:45:02 +0000 | [diff] [blame] | 894 | const char* FName = FD->getIdentifier()->getName(); |
| 895 | |
Ted Kremenek | 38c6f02 | 2009-03-05 22:11:14 +0000 | [diff] [blame] | 896 | // Strip away preceding '_'. Doing this here will effect all the checks |
| 897 | // down below. |
| 898 | while (*FName == '_') ++FName; |
| 899 | |
Ted Kremenek | 17144e8 | 2009-01-12 21:45:02 +0000 | [diff] [blame] | 900 | // Inspect the result type. |
| 901 | QualType RetTy = FT->getResultType(); |
| 902 | |
| 903 | // FIXME: This should all be refactored into a chain of "summary lookup" |
| 904 | // filters. |
| 905 | if (strcmp(FName, "IOServiceGetMatchingServices") == 0) { |
| 906 | // FIXES: <rdar://problem/6326900> |
| 907 | // This should be addressed using a API table. This strcmp is also |
| 908 | // a little gross, but there is no need to super optimize here. |
Ted Kremenek | a56ae16 | 2009-05-03 05:20:50 +0000 | [diff] [blame] | 909 | assert (ScratchArgs.isEmpty()); |
| 910 | ScratchArgs = AF.Add(ScratchArgs, 1, DecRef); |
Ted Kremenek | 17144e8 | 2009-01-12 21:45:02 +0000 | [diff] [blame] | 911 | S = getPersistentSummary(RetEffect::MakeNoRet(), DoNothing, DoNothing); |
| 912 | break; |
Ted Kremenek | cfc50c7 | 2008-10-22 20:54:52 +0000 | [diff] [blame] | 913 | } |
Ted Kremenek | 7b88c89 | 2009-03-17 22:43:44 +0000 | [diff] [blame] | 914 | |
| 915 | // Enable this code once the semantics of NSDeallocateObject are resolved |
| 916 | // for GC. <rdar://problem/6619988> |
| 917 | #if 0 |
| 918 | // Handle: NSDeallocateObject(id anObject); |
| 919 | // This method does allow 'nil' (although we don't check it now). |
| 920 | if (strcmp(FName, "NSDeallocateObject") == 0) { |
| 921 | return RetTy == Ctx.VoidTy |
| 922 | ? getPersistentSummary(RetEffect::MakeNoRet(), DoNothing, Dealloc) |
| 923 | : getPersistentStopSummary(); |
| 924 | } |
| 925 | #endif |
Ted Kremenek | 17144e8 | 2009-01-12 21:45:02 +0000 | [diff] [blame] | 926 | |
| 927 | // Handle: id NSMakeCollectable(CFTypeRef) |
| 928 | if (strcmp(FName, "NSMakeCollectable") == 0) { |
| 929 | S = (RetTy == Ctx.getObjCIdType()) |
| 930 | ? getUnarySummary(FT, cfmakecollectable) |
| 931 | : getPersistentStopSummary(); |
| 932 | |
| 933 | break; |
| 934 | } |
| 935 | |
| 936 | if (RetTy->isPointerType()) { |
| 937 | // For CoreFoundation ('CF') types. |
| 938 | if (isRefType(RetTy, "CF", &Ctx, FName)) { |
| 939 | if (isRetain(FD, FName)) |
| 940 | S = getUnarySummary(FT, cfretain); |
| 941 | else if (strstr(FName, "MakeCollectable")) |
| 942 | S = getUnarySummary(FT, cfmakecollectable); |
| 943 | else |
| 944 | S = getCFCreateGetRuleSummary(FD, FName); |
| 945 | |
| 946 | break; |
| 947 | } |
| 948 | |
| 949 | // For CoreGraphics ('CG') types. |
| 950 | if (isRefType(RetTy, "CG", &Ctx, FName)) { |
| 951 | if (isRetain(FD, FName)) |
| 952 | S = getUnarySummary(FT, cfretain); |
| 953 | else |
| 954 | S = getCFCreateGetRuleSummary(FD, FName); |
| 955 | |
| 956 | break; |
| 957 | } |
| 958 | |
| 959 | // For the Disk Arbitration API (DiskArbitration/DADisk.h) |
| 960 | if (isRefType(RetTy, "DADisk") || |
| 961 | isRefType(RetTy, "DADissenter") || |
| 962 | isRefType(RetTy, "DASessionRef")) { |
| 963 | S = getCFCreateGetRuleSummary(FD, FName); |
| 964 | break; |
| 965 | } |
| 966 | |
| 967 | break; |
| 968 | } |
| 969 | |
| 970 | // Check for release functions, the only kind of functions that we care |
| 971 | // about that don't return a pointer type. |
| 972 | if (FName[0] == 'C' && (FName[1] == 'F' || FName[1] == 'G')) { |
Ted Kremenek | 38c6f02 | 2009-03-05 22:11:14 +0000 | [diff] [blame] | 973 | // Test for 'CGCF'. |
| 974 | if (FName[1] == 'G' && FName[2] == 'C' && FName[3] == 'F') |
| 975 | FName += 4; |
| 976 | else |
| 977 | FName += 2; |
| 978 | |
| 979 | if (isRelease(FD, FName)) |
Ted Kremenek | 17144e8 | 2009-01-12 21:45:02 +0000 | [diff] [blame] | 980 | S = getUnarySummary(FT, cfrelease); |
| 981 | else { |
Ted Kremenek | a56ae16 | 2009-05-03 05:20:50 +0000 | [diff] [blame] | 982 | assert (ScratchArgs.isEmpty()); |
Ted Kremenek | 7b29368 | 2009-01-29 22:45:13 +0000 | [diff] [blame] | 983 | // Remaining CoreFoundation and CoreGraphics functions. |
| 984 | // We use to assume that they all strictly followed the ownership idiom |
| 985 | // and that ownership cannot be transferred. While this is technically |
| 986 | // correct, many methods allow a tracked object to escape. For example: |
| 987 | // |
| 988 | // CFMutableDictionaryRef x = CFDictionaryCreateMutable(...); |
| 989 | // CFDictionaryAddValue(y, key, x); |
| 990 | // CFRelease(x); |
| 991 | // ... it is okay to use 'x' since 'y' has a reference to it |
| 992 | // |
| 993 | // We handle this and similar cases with the follow heuristic. If the |
| 994 | // function name contains "InsertValue", "SetValue" or "AddValue" then |
| 995 | // we assume that arguments may "escape." |
| 996 | // |
| 997 | ArgEffect E = (CStrInCStrNoCase(FName, "InsertValue") || |
| 998 | CStrInCStrNoCase(FName, "AddValue") || |
Ted Kremenek | cf07125 | 2009-02-05 22:34:53 +0000 | [diff] [blame] | 999 | CStrInCStrNoCase(FName, "SetValue") || |
| 1000 | CStrInCStrNoCase(FName, "AppendValue")) |
Ted Kremenek | 7b29368 | 2009-01-29 22:45:13 +0000 | [diff] [blame] | 1001 | ? MayEscape : DoNothing; |
| 1002 | |
| 1003 | S = getPersistentSummary(RetEffect::MakeNoRet(), DoNothing, E); |
Ted Kremenek | 17144e8 | 2009-01-12 21:45:02 +0000 | [diff] [blame] | 1004 | } |
| 1005 | } |
Ted Kremenek | 4c5378c | 2008-07-15 16:50:12 +0000 | [diff] [blame] | 1006 | } |
| 1007 | while (0); |
Ted Kremenek | 2f22673 | 2009-05-04 05:31:22 +0000 | [diff] [blame] | 1008 | |
| 1009 | if (!S) |
| 1010 | S = getDefaultSummary(); |
Ted Kremenek | ae855d4 | 2008-04-24 17:22:33 +0000 | [diff] [blame] | 1011 | |
Ted Kremenek | a8c3c43 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 1012 | FuncSummaries[FD] = S; |
Ted Kremenek | 562c130 | 2008-05-05 16:51:50 +0000 | [diff] [blame] | 1013 | return S; |
Ted Kremenek | 827f93b | 2008-03-06 00:08:09 +0000 | [diff] [blame] | 1014 | } |
| 1015 | |
Ted Kremenek | 4c5378c | 2008-07-15 16:50:12 +0000 | [diff] [blame] | 1016 | RetainSummary* |
| 1017 | RetainSummaryManager::getCFCreateGetRuleSummary(FunctionDecl* FD, |
| 1018 | const char* FName) { |
| 1019 | |
Ted Kremenek | 562c130 | 2008-05-05 16:51:50 +0000 | [diff] [blame] | 1020 | if (strstr(FName, "Create") || strstr(FName, "Copy")) |
| 1021 | return getCFSummaryCreateRule(FD); |
Ted Kremenek | 4c5378c | 2008-07-15 16:50:12 +0000 | [diff] [blame] | 1022 | |
Ted Kremenek | 562c130 | 2008-05-05 16:51:50 +0000 | [diff] [blame] | 1023 | if (strstr(FName, "Get")) |
| 1024 | return getCFSummaryGetRule(FD); |
| 1025 | |
Ted Kremenek | 286e985 | 2009-05-04 04:57:00 +0000 | [diff] [blame] | 1026 | return getDefaultSummary(); |
Ted Kremenek | 562c130 | 2008-05-05 16:51:50 +0000 | [diff] [blame] | 1027 | } |
| 1028 | |
Ted Kremenek | a8c3c43 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 1029 | RetainSummary* |
Ted Kremenek | 064ef32 | 2009-02-23 16:51:39 +0000 | [diff] [blame] | 1030 | RetainSummaryManager::getUnarySummary(const FunctionType* FT, |
| 1031 | UnaryFuncKind func) { |
| 1032 | |
Ted Kremenek | 17144e8 | 2009-01-12 21:45:02 +0000 | [diff] [blame] | 1033 | // Sanity check that this is *really* a unary function. This can |
| 1034 | // happen if people do weird things. |
Douglas Gregor | 4fa5890 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1035 | const FunctionProtoType* FTP = dyn_cast<FunctionProtoType>(FT); |
Ted Kremenek | 17144e8 | 2009-01-12 21:45:02 +0000 | [diff] [blame] | 1036 | if (!FTP || FTP->getNumArgs() != 1) |
| 1037 | return getPersistentStopSummary(); |
Ted Kremenek | a8c3c43 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 1038 | |
Ted Kremenek | a56ae16 | 2009-05-03 05:20:50 +0000 | [diff] [blame] | 1039 | assert (ScratchArgs.isEmpty()); |
Ted Kremenek | ce3ed1e | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 1040 | |
Ted Kremenek | 9b0c09c | 2008-04-29 05:33:51 +0000 | [diff] [blame] | 1041 | switch (func) { |
Ted Kremenek | a56ae16 | 2009-05-03 05:20:50 +0000 | [diff] [blame] | 1042 | case cfretain: { |
| 1043 | ScratchArgs = AF.Add(ScratchArgs, 0, IncRef); |
Ted Kremenek | a3f30dd | 2008-05-22 17:31:13 +0000 | [diff] [blame] | 1044 | return getPersistentSummary(RetEffect::MakeAlias(0), |
| 1045 | DoNothing, DoNothing); |
Ted Kremenek | 9b0c09c | 2008-04-29 05:33:51 +0000 | [diff] [blame] | 1046 | } |
| 1047 | |
| 1048 | case cfrelease: { |
Ted Kremenek | a56ae16 | 2009-05-03 05:20:50 +0000 | [diff] [blame] | 1049 | ScratchArgs = AF.Add(ScratchArgs, 0, DecRef); |
Ted Kremenek | a3f30dd | 2008-05-22 17:31:13 +0000 | [diff] [blame] | 1050 | return getPersistentSummary(RetEffect::MakeNoRet(), |
| 1051 | DoNothing, DoNothing); |
Ted Kremenek | 9b0c09c | 2008-04-29 05:33:51 +0000 | [diff] [blame] | 1052 | } |
| 1053 | |
| 1054 | case cfmakecollectable: { |
Ted Kremenek | a56ae16 | 2009-05-03 05:20:50 +0000 | [diff] [blame] | 1055 | ScratchArgs = AF.Add(ScratchArgs, 0, MakeCollectable); |
Ted Kremenek | 2126bef | 2009-02-18 21:57:45 +0000 | [diff] [blame] | 1056 | return getPersistentSummary(RetEffect::MakeAlias(0),DoNothing, DoNothing); |
Ted Kremenek | 9b0c09c | 2008-04-29 05:33:51 +0000 | [diff] [blame] | 1057 | } |
| 1058 | |
| 1059 | default: |
Ted Kremenek | 562c130 | 2008-05-05 16:51:50 +0000 | [diff] [blame] | 1060 | assert (false && "Not a supported unary function."); |
Ted Kremenek | 286e985 | 2009-05-04 04:57:00 +0000 | [diff] [blame] | 1061 | return getDefaultSummary(); |
Ted Kremenek | ab2fa2a | 2008-04-10 23:44:06 +0000 | [diff] [blame] | 1062 | } |
Ted Kremenek | ce3ed1e | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 1063 | } |
| 1064 | |
Ted Kremenek | a8c3c43 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 1065 | RetainSummary* RetainSummaryManager::getCFSummaryCreateRule(FunctionDecl* FD) { |
Ted Kremenek | a56ae16 | 2009-05-03 05:20:50 +0000 | [diff] [blame] | 1066 | assert (ScratchArgs.isEmpty()); |
Ted Kremenek | ede40b7 | 2008-07-09 18:11:16 +0000 | [diff] [blame] | 1067 | |
| 1068 | if (FD->getIdentifier() == CFDictionaryCreateII) { |
Ted Kremenek | a56ae16 | 2009-05-03 05:20:50 +0000 | [diff] [blame] | 1069 | ScratchArgs = AF.Add(ScratchArgs, 1, DoNothingByRef); |
| 1070 | ScratchArgs = AF.Add(ScratchArgs, 2, DoNothingByRef); |
Ted Kremenek | ede40b7 | 2008-07-09 18:11:16 +0000 | [diff] [blame] | 1071 | } |
| 1072 | |
Ted Kremenek | 68621b9 | 2009-01-28 05:56:51 +0000 | [diff] [blame] | 1073 | return getPersistentSummary(RetEffect::MakeOwned(RetEffect::CF, true)); |
Ted Kremenek | ce3ed1e | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 1074 | } |
| 1075 | |
Ted Kremenek | a8c3c43 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 1076 | RetainSummary* RetainSummaryManager::getCFSummaryGetRule(FunctionDecl* FD) { |
Ted Kremenek | a56ae16 | 2009-05-03 05:20:50 +0000 | [diff] [blame] | 1077 | assert (ScratchArgs.isEmpty()); |
Ted Kremenek | 68621b9 | 2009-01-28 05:56:51 +0000 | [diff] [blame] | 1078 | return getPersistentSummary(RetEffect::MakeNotOwned(RetEffect::CF), |
| 1079 | DoNothing, DoNothing); |
Ted Kremenek | ce3ed1e | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 1080 | } |
| 1081 | |
Ted Kremenek | a7338b4 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 1082 | //===----------------------------------------------------------------------===// |
Ted Kremenek | a8c3c43 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 1083 | // Summary creation for Selectors. |
| 1084 | //===----------------------------------------------------------------------===// |
| 1085 | |
Ted Kremenek | bcaff79 | 2008-05-06 15:44:25 +0000 | [diff] [blame] | 1086 | RetainSummary* |
Ted Kremenek | a821b79 | 2009-04-29 05:04:30 +0000 | [diff] [blame] | 1087 | RetainSummaryManager::getInitMethodSummary(QualType RetTy) { |
Ted Kremenek | a56ae16 | 2009-05-03 05:20:50 +0000 | [diff] [blame] | 1088 | assert(ScratchArgs.isEmpty()); |
Ted Kremenek | 42ea032 | 2008-05-05 23:55:01 +0000 | [diff] [blame] | 1089 | |
Ted Kremenek | 802cfc7 | 2009-02-20 00:05:35 +0000 | [diff] [blame] | 1090 | // 'init' methods only return an alias if the return type is a location type. |
Ted Kremenek | a821b79 | 2009-04-29 05:04:30 +0000 | [diff] [blame] | 1091 | return getPersistentSummary(Loc::IsLocType(RetTy) |
| 1092 | ? RetEffect::MakeReceiverAlias() |
Ted Kremenek | 03d242e | 2009-05-05 18:44:20 +0000 | [diff] [blame] | 1093 | : RetEffect::MakeNoRet()); |
Ted Kremenek | 42ea032 | 2008-05-05 23:55:01 +0000 | [diff] [blame] | 1094 | } |
Ted Kremenek | 03d242e | 2009-05-05 18:44:20 +0000 | [diff] [blame] | 1095 | |
Ted Kremenek | bcaff79 | 2008-05-06 15:44:25 +0000 | [diff] [blame] | 1096 | RetainSummary* |
Ted Kremenek | 314b195 | 2009-04-29 23:03:22 +0000 | [diff] [blame] | 1097 | RetainSummaryManager::getCommonMethodSummary(const ObjCMethodDecl* MD, |
| 1098 | Selector S, QualType RetTy) { |
Ted Kremenek | f936b3f | 2009-04-24 21:56:17 +0000 | [diff] [blame] | 1099 | |
Ted Kremenek | 578498a | 2009-04-29 00:42:39 +0000 | [diff] [blame] | 1100 | if (MD) { |
Ted Kremenek | 3fc3e11 | 2009-04-24 18:00:17 +0000 | [diff] [blame] | 1101 | // Scan the method decl for 'void*' arguments. These should be treated |
| 1102 | // as 'StopTracking' because they are often used with delegates. |
| 1103 | // Delegates are a frequent form of false positives with the retain |
| 1104 | // count checker. |
| 1105 | unsigned i = 0; |
| 1106 | for (ObjCMethodDecl::param_iterator I = MD->param_begin(), |
| 1107 | E = MD->param_end(); I != E; ++I, ++i) |
| 1108 | if (ParmVarDecl *PD = *I) { |
| 1109 | QualType Ty = Ctx.getCanonicalType(PD->getType()); |
| 1110 | if (Ty.getUnqualifiedType() == Ctx.VoidPtrTy) |
Ted Kremenek | a56ae16 | 2009-05-03 05:20:50 +0000 | [diff] [blame] | 1111 | ScratchArgs = AF.Add(ScratchArgs, i, StopTracking); |
Ted Kremenek | 3fc3e11 | 2009-04-24 18:00:17 +0000 | [diff] [blame] | 1112 | } |
| 1113 | } |
| 1114 | |
Ted Kremenek | f936b3f | 2009-04-24 21:56:17 +0000 | [diff] [blame] | 1115 | // Any special effect for the receiver? |
| 1116 | ArgEffect ReceiverEff = DoNothing; |
| 1117 | |
| 1118 | // If one of the arguments in the selector has the keyword 'delegate' we |
| 1119 | // should stop tracking the reference count for the receiver. This is |
| 1120 | // because the reference count is quite possibly handled by a delegate |
| 1121 | // method. |
| 1122 | if (S.isKeywordSelector()) { |
| 1123 | const std::string &str = S.getAsString(); |
| 1124 | assert(!str.empty()); |
| 1125 | if (CStrInCStrNoCase(&str[0], "delegate:")) ReceiverEff = StopTracking; |
| 1126 | } |
| 1127 | |
Ted Kremenek | 174a077 | 2009-04-23 23:08:22 +0000 | [diff] [blame] | 1128 | // Look for methods that return an owned object. |
Ted Kremenek | a9cdbc3 | 2009-05-03 06:08:32 +0000 | [diff] [blame] | 1129 | if (isTrackedObjCObjectType(RetTy)) { |
| 1130 | // EXPERIMENTAL: Assume the Cocoa conventions for all objects returned |
| 1131 | // by instance methods. |
Ted Kremenek | a9cdbc3 | 2009-05-03 06:08:32 +0000 | [diff] [blame] | 1132 | RetEffect E = |
| 1133 | followsFundamentalRule(S.getIdentifierInfoForSlot(0)->getName()) |
Ted Kremenek | 5535e5e | 2009-05-07 23:40:42 +0000 | [diff] [blame] | 1134 | ? ObjCAllocRetE : RetEffect::MakeNotOwned(RetEffect::ObjC); |
Ted Kremenek | a9cdbc3 | 2009-05-03 06:08:32 +0000 | [diff] [blame] | 1135 | |
| 1136 | return getPersistentSummary(E, ReceiverEff, MayEscape); |
Ted Kremenek | 3fc3e11 | 2009-04-24 18:00:17 +0000 | [diff] [blame] | 1137 | } |
Ted Kremenek | 174a077 | 2009-04-23 23:08:22 +0000 | [diff] [blame] | 1138 | |
Ted Kremenek | a9cdbc3 | 2009-05-03 06:08:32 +0000 | [diff] [blame] | 1139 | // Look for methods that return an owned core foundation object. |
| 1140 | if (isTrackedCFObjectType(RetTy)) { |
| 1141 | RetEffect E = |
| 1142 | followsFundamentalRule(S.getIdentifierInfoForSlot(0)->getName()) |
| 1143 | ? RetEffect::MakeOwned(RetEffect::CF, true) |
| 1144 | : RetEffect::MakeNotOwned(RetEffect::CF); |
| 1145 | |
| 1146 | return getPersistentSummary(E, ReceiverEff, MayEscape); |
| 1147 | } |
Ted Kremenek | 174a077 | 2009-04-23 23:08:22 +0000 | [diff] [blame] | 1148 | |
Ted Kremenek | a9cdbc3 | 2009-05-03 06:08:32 +0000 | [diff] [blame] | 1149 | if (ScratchArgs.isEmpty() && ReceiverEff == DoNothing) |
Ted Kremenek | 286e985 | 2009-05-04 04:57:00 +0000 | [diff] [blame] | 1150 | return getDefaultSummary(); |
Ted Kremenek | 174a077 | 2009-04-23 23:08:22 +0000 | [diff] [blame] | 1151 | |
Ted Kremenek | 2f22673 | 2009-05-04 05:31:22 +0000 | [diff] [blame] | 1152 | return getPersistentSummary(RetEffect::MakeNoRet(), ReceiverEff, MayEscape); |
Ted Kremenek | 174a077 | 2009-04-23 23:08:22 +0000 | [diff] [blame] | 1153 | } |
| 1154 | |
| 1155 | RetainSummary* |
Ted Kremenek | 04e0030 | 2009-04-29 17:09:14 +0000 | [diff] [blame] | 1156 | RetainSummaryManager::getInstanceMethodSummary(Selector S, |
| 1157 | IdentifierInfo *ClsName, |
Ted Kremenek | 314b195 | 2009-04-29 23:03:22 +0000 | [diff] [blame] | 1158 | const ObjCInterfaceDecl* ID, |
| 1159 | const ObjCMethodDecl *MD, |
Ted Kremenek | 04e0030 | 2009-04-29 17:09:14 +0000 | [diff] [blame] | 1160 | QualType RetTy) { |
Ted Kremenek | bcaff79 | 2008-05-06 15:44:25 +0000 | [diff] [blame] | 1161 | |
Ted Kremenek | a821b79 | 2009-04-29 05:04:30 +0000 | [diff] [blame] | 1162 | // Look up a summary in our summary cache. |
| 1163 | ObjCMethodSummariesTy::iterator I = ObjCMethodSummaries.find(ID, ClsName, S); |
Ted Kremenek | 42ea032 | 2008-05-05 23:55:01 +0000 | [diff] [blame] | 1164 | |
Ted Kremenek | 97c1e0c | 2008-06-23 22:21:20 +0000 | [diff] [blame] | 1165 | if (I != ObjCMethodSummaries.end()) |
Ted Kremenek | 42ea032 | 2008-05-05 23:55:01 +0000 | [diff] [blame] | 1166 | return I->second; |
Ted Kremenek | 42ea032 | 2008-05-05 23:55:01 +0000 | [diff] [blame] | 1167 | |
Ted Kremenek | a56ae16 | 2009-05-03 05:20:50 +0000 | [diff] [blame] | 1168 | assert(ScratchArgs.isEmpty()); |
Ted Kremenek | 2f22673 | 2009-05-04 05:31:22 +0000 | [diff] [blame] | 1169 | RetainSummary *Summ = 0; |
Ted Kremenek | 1d3d956 | 2008-05-06 06:09:09 +0000 | [diff] [blame] | 1170 | |
Ted Kremenek | 2f22673 | 2009-05-04 05:31:22 +0000 | [diff] [blame] | 1171 | // "initXXX": pass-through for receiver. |
| 1172 | if (deriveNamingConvention(S.getIdentifierInfoForSlot(0)->getName()) |
| 1173 | == InitRule) |
| 1174 | Summ = getInitMethodSummary(RetTy); |
| 1175 | else |
| 1176 | Summ = getCommonMethodSummary(MD, S, RetTy); |
| 1177 | |
Ted Kremenek | 2f22673 | 2009-05-04 05:31:22 +0000 | [diff] [blame] | 1178 | // Memoize the summary. |
Ted Kremenek | a821b79 | 2009-04-29 05:04:30 +0000 | [diff] [blame] | 1179 | ObjCMethodSummaries[ObjCSummaryKey(ClsName, S)] = Summ; |
Ted Kremenek | e415850 | 2009-04-23 19:11:35 +0000 | [diff] [blame] | 1180 | return Summ; |
Ted Kremenek | 42ea032 | 2008-05-05 23:55:01 +0000 | [diff] [blame] | 1181 | } |
| 1182 | |
Ted Kremenek | a7722b7 | 2008-05-06 21:26:51 +0000 | [diff] [blame] | 1183 | RetainSummary* |
Ted Kremenek | 578498a | 2009-04-29 00:42:39 +0000 | [diff] [blame] | 1184 | RetainSummaryManager::getClassMethodSummary(Selector S, IdentifierInfo *ClsName, |
Ted Kremenek | 314b195 | 2009-04-29 23:03:22 +0000 | [diff] [blame] | 1185 | const ObjCInterfaceDecl *ID, |
| 1186 | const ObjCMethodDecl *MD, |
| 1187 | QualType RetTy) { |
Ted Kremenek | ccbe79a | 2009-04-24 17:50:11 +0000 | [diff] [blame] | 1188 | |
Ted Kremenek | 578498a | 2009-04-29 00:42:39 +0000 | [diff] [blame] | 1189 | assert(ClsName && "Class name must be specified."); |
Ted Kremenek | a821b79 | 2009-04-29 05:04:30 +0000 | [diff] [blame] | 1190 | ObjCMethodSummariesTy::iterator I = |
| 1191 | ObjCClassMethodSummaries.find(ID, ClsName, S); |
Ted Kremenek | a7722b7 | 2008-05-06 21:26:51 +0000 | [diff] [blame] | 1192 | |
Ted Kremenek | 97c1e0c | 2008-06-23 22:21:20 +0000 | [diff] [blame] | 1193 | if (I != ObjCClassMethodSummaries.end()) |
Ted Kremenek | a7722b7 | 2008-05-06 21:26:51 +0000 | [diff] [blame] | 1194 | return I->second; |
Ted Kremenek | 2f22673 | 2009-05-04 05:31:22 +0000 | [diff] [blame] | 1195 | |
| 1196 | RetainSummary *Summ = getCommonMethodSummary(MD, S, RetTy); |
| 1197 | |
Ted Kremenek | 2f22673 | 2009-05-04 05:31:22 +0000 | [diff] [blame] | 1198 | // Memoize the summary. |
Ted Kremenek | 578498a | 2009-04-29 00:42:39 +0000 | [diff] [blame] | 1199 | ObjCClassMethodSummaries[ObjCSummaryKey(ClsName, S)] = Summ; |
Ted Kremenek | e415850 | 2009-04-23 19:11:35 +0000 | [diff] [blame] | 1200 | return Summ; |
Ted Kremenek | a7722b7 | 2008-05-06 21:26:51 +0000 | [diff] [blame] | 1201 | } |
| 1202 | |
Ted Kremenek | 5535e5e | 2009-05-07 23:40:42 +0000 | [diff] [blame] | 1203 | void RetainSummaryManager::InitializeClassMethodSummaries() { |
| 1204 | assert(ScratchArgs.isEmpty()); |
| 1205 | RetainSummary* Summ = getPersistentSummary(ObjCAllocRetE); |
Ted Kremenek | 0e344d4 | 2008-05-06 00:30:21 +0000 | [diff] [blame] | 1206 | |
Ted Kremenek | 272aa85 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 1207 | // Create the summaries for "alloc", "new", and "allocWithZone:" for |
| 1208 | // NSObject and its derivatives. |
| 1209 | addNSObjectClsMethSummary(GetNullarySelector("alloc", Ctx), Summ); |
| 1210 | addNSObjectClsMethSummary(GetNullarySelector("new", Ctx), Summ); |
| 1211 | addNSObjectClsMethSummary(GetUnarySelector("allocWithZone", Ctx), Summ); |
Ted Kremenek | f2717b0 | 2008-07-18 17:24:20 +0000 | [diff] [blame] | 1212 | |
| 1213 | // Create the [NSAssertionHandler currentHander] summary. |
Ted Kremenek | 3d6ddbb | 2008-08-12 18:30:56 +0000 | [diff] [blame] | 1214 | addClsMethSummary(&Ctx.Idents.get("NSAssertionHandler"), |
Ted Kremenek | 68621b9 | 2009-01-28 05:56:51 +0000 | [diff] [blame] | 1215 | GetNullarySelector("currentHandler", Ctx), |
| 1216 | getPersistentSummary(RetEffect::MakeNotOwned(RetEffect::ObjC))); |
Ted Kremenek | c8c8d2c | 2008-10-21 15:53:15 +0000 | [diff] [blame] | 1217 | |
| 1218 | // Create the [NSAutoreleasePool addObject:] summary. |
Ted Kremenek | a56ae16 | 2009-05-03 05:20:50 +0000 | [diff] [blame] | 1219 | ScratchArgs = AF.Add(ScratchArgs, 0, Autorelease); |
Ted Kremenek | 9b112d2 | 2009-01-28 21:44:40 +0000 | [diff] [blame] | 1220 | addClsMethSummary(&Ctx.Idents.get("NSAutoreleasePool"), |
| 1221 | GetUnarySelector("addObject", Ctx), |
| 1222 | getPersistentSummary(RetEffect::MakeNoRet(), |
Ted Kremenek | f21cb24 | 2009-02-23 02:31:16 +0000 | [diff] [blame] | 1223 | DoNothing, Autorelease)); |
Ted Kremenek | ccbe79a | 2009-04-24 17:50:11 +0000 | [diff] [blame] | 1224 | |
| 1225 | // Create the summaries for [NSObject performSelector...]. We treat |
| 1226 | // these as 'stop tracking' for the arguments because they are often |
| 1227 | // used for delegates that can release the object. When we have better |
| 1228 | // inter-procedural analysis we can potentially do something better. This |
| 1229 | // workaround is to remove false positives. |
| 1230 | Summ = getPersistentSummary(RetEffect::MakeNoRet(), DoNothing, StopTracking); |
| 1231 | IdentifierInfo *NSObjectII = &Ctx.Idents.get("NSObject"); |
| 1232 | addClsMethSummary(NSObjectII, Summ, "performSelector", "withObject", |
| 1233 | "afterDelay", NULL); |
| 1234 | addClsMethSummary(NSObjectII, Summ, "performSelector", "withObject", |
| 1235 | "afterDelay", "inModes", NULL); |
| 1236 | addClsMethSummary(NSObjectII, Summ, "performSelectorOnMainThread", |
| 1237 | "withObject", "waitUntilDone", NULL); |
| 1238 | addClsMethSummary(NSObjectII, Summ, "performSelectorOnMainThread", |
| 1239 | "withObject", "waitUntilDone", "modes", NULL); |
| 1240 | addClsMethSummary(NSObjectII, Summ, "performSelector", "onThread", |
| 1241 | "withObject", "waitUntilDone", NULL); |
| 1242 | addClsMethSummary(NSObjectII, Summ, "performSelector", "onThread", |
| 1243 | "withObject", "waitUntilDone", "modes", NULL); |
| 1244 | addClsMethSummary(NSObjectII, Summ, "performSelectorInBackground", |
| 1245 | "withObject", NULL); |
Ted Kremenek | 0e344d4 | 2008-05-06 00:30:21 +0000 | [diff] [blame] | 1246 | } |
| 1247 | |
Ted Kremenek | 97c1e0c | 2008-06-23 22:21:20 +0000 | [diff] [blame] | 1248 | void RetainSummaryManager::InitializeMethodSummaries() { |
Ted Kremenek | 83b2cde | 2008-05-06 00:38:54 +0000 | [diff] [blame] | 1249 | |
Ted Kremenek | a56ae16 | 2009-05-03 05:20:50 +0000 | [diff] [blame] | 1250 | assert (ScratchArgs.isEmpty()); |
Ted Kremenek | 83b2cde | 2008-05-06 00:38:54 +0000 | [diff] [blame] | 1251 | |
Ted Kremenek | a7722b7 | 2008-05-06 21:26:51 +0000 | [diff] [blame] | 1252 | // Create the "init" selector. It just acts as a pass-through for the |
| 1253 | // receiver. |
Ted Kremenek | 56c70aa | 2009-02-23 16:54:00 +0000 | [diff] [blame] | 1254 | RetainSummary* InitSumm = |
| 1255 | getPersistentSummary(RetEffect::MakeReceiverAlias()); |
Ted Kremenek | e44927e | 2008-07-01 17:21:27 +0000 | [diff] [blame] | 1256 | addNSObjectMethSummary(GetNullarySelector("init", Ctx), InitSumm); |
Ted Kremenek | a7722b7 | 2008-05-06 21:26:51 +0000 | [diff] [blame] | 1257 | |
| 1258 | // The next methods are allocators. |
Ted Kremenek | 5535e5e | 2009-05-07 23:40:42 +0000 | [diff] [blame] | 1259 | RetainSummary* Summ = getPersistentSummary(ObjCAllocRetE); |
Ted Kremenek | a7722b7 | 2008-05-06 21:26:51 +0000 | [diff] [blame] | 1260 | |
| 1261 | // Create the "copy" selector. |
Ted Kremenek | 9449ca9 | 2008-08-12 20:41:56 +0000 | [diff] [blame] | 1262 | addNSObjectMethSummary(GetNullarySelector("copy", Ctx), Summ); |
| 1263 | |
Ted Kremenek | 83b2cde | 2008-05-06 00:38:54 +0000 | [diff] [blame] | 1264 | // Create the "mutableCopy" selector. |
Ted Kremenek | 272aa85 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 1265 | addNSObjectMethSummary(GetNullarySelector("mutableCopy", Ctx), Summ); |
Ted Kremenek | 9449ca9 | 2008-08-12 20:41:56 +0000 | [diff] [blame] | 1266 | |
Ted Kremenek | 266d8b6 | 2008-05-06 02:26:56 +0000 | [diff] [blame] | 1267 | // Create the "retain" selector. |
Ted Kremenek | 5535e5e | 2009-05-07 23:40:42 +0000 | [diff] [blame] | 1268 | RetEffect E = RetEffect::MakeReceiverAlias(); |
Ted Kremenek | 58dd95b | 2009-02-18 18:54:33 +0000 | [diff] [blame] | 1269 | Summ = getPersistentSummary(E, IncRefMsg); |
Ted Kremenek | 272aa85 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 1270 | addNSObjectMethSummary(GetNullarySelector("retain", Ctx), Summ); |
Ted Kremenek | 266d8b6 | 2008-05-06 02:26:56 +0000 | [diff] [blame] | 1271 | |
| 1272 | // Create the "release" selector. |
Ted Kremenek | 58dd95b | 2009-02-18 18:54:33 +0000 | [diff] [blame] | 1273 | Summ = getPersistentSummary(E, DecRefMsg); |
Ted Kremenek | 272aa85 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 1274 | addNSObjectMethSummary(GetNullarySelector("release", Ctx), Summ); |
Ted Kremenek | c00b32b | 2008-05-07 21:17:39 +0000 | [diff] [blame] | 1275 | |
| 1276 | // Create the "drain" selector. |
| 1277 | Summ = getPersistentSummary(E, isGCEnabled() ? DoNothing : DecRef); |
Ted Kremenek | 272aa85 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 1278 | addNSObjectMethSummary(GetNullarySelector("drain", Ctx), Summ); |
Ted Kremenek | 6537a64 | 2009-03-17 19:42:23 +0000 | [diff] [blame] | 1279 | |
| 1280 | // Create the -dealloc summary. |
| 1281 | Summ = getPersistentSummary(RetEffect::MakeNoRet(), Dealloc); |
| 1282 | addNSObjectMethSummary(GetNullarySelector("dealloc", Ctx), Summ); |
Ted Kremenek | 266d8b6 | 2008-05-06 02:26:56 +0000 | [diff] [blame] | 1283 | |
| 1284 | // Create the "autorelease" selector. |
Ted Kremenek | 9b112d2 | 2009-01-28 21:44:40 +0000 | [diff] [blame] | 1285 | Summ = getPersistentSummary(E, Autorelease); |
Ted Kremenek | 272aa85 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 1286 | addNSObjectMethSummary(GetNullarySelector("autorelease", Ctx), Summ); |
Ted Kremenek | 9449ca9 | 2008-08-12 20:41:56 +0000 | [diff] [blame] | 1287 | |
Ted Kremenek | aac8283 | 2009-02-23 17:45:03 +0000 | [diff] [blame] | 1288 | // Specially handle NSAutoreleasePool. |
Ted Kremenek | 1b4b656 | 2009-02-25 02:54:57 +0000 | [diff] [blame] | 1289 | addInstMethSummary("NSAutoreleasePool", "init", |
Ted Kremenek | aac8283 | 2009-02-23 17:45:03 +0000 | [diff] [blame] | 1290 | getPersistentSummary(RetEffect::MakeReceiverAlias(), |
Ted Kremenek | 1b4b656 | 2009-02-25 02:54:57 +0000 | [diff] [blame] | 1291 | NewAutoreleasePool)); |
Ted Kremenek | aac8283 | 2009-02-23 17:45:03 +0000 | [diff] [blame] | 1292 | |
Ted Kremenek | 45642a4 | 2008-08-12 18:48:50 +0000 | [diff] [blame] | 1293 | // For NSWindow, allocated objects are (initially) self-owned. |
Ted Kremenek | 7e3a327 | 2009-02-23 02:51:29 +0000 | [diff] [blame] | 1294 | // FIXME: For now we opt for false negatives with NSWindow, as these objects |
| 1295 | // self-own themselves. However, they only do this once they are displayed. |
| 1296 | // Thus, we need to track an NSWindow's display status. |
| 1297 | // This is tracked in <rdar://problem/6062711>. |
Ted Kremenek | fbf2dc5 | 2009-03-04 23:30:42 +0000 | [diff] [blame] | 1298 | // See also http://llvm.org/bugs/show_bug.cgi?id=3714. |
Ted Kremenek | e5a036a | 2009-04-03 19:02:51 +0000 | [diff] [blame] | 1299 | RetainSummary *NoTrackYet = getPersistentSummary(RetEffect::MakeNoRet()); |
| 1300 | |
| 1301 | addClassMethSummary("NSWindow", "alloc", NoTrackYet); |
| 1302 | |
Ted Kremenek | fbf2dc5 | 2009-03-04 23:30:42 +0000 | [diff] [blame] | 1303 | |
| 1304 | #if 0 |
Ted Kremenek | e44927e | 2008-07-01 17:21:27 +0000 | [diff] [blame] | 1305 | RetainSummary *NSWindowSumm = |
Ted Kremenek | 7e3a327 | 2009-02-23 02:51:29 +0000 | [diff] [blame] | 1306 | getPersistentSummary(RetEffect::MakeReceiverAlias(), StopTracking); |
Ted Kremenek | 45642a4 | 2008-08-12 18:48:50 +0000 | [diff] [blame] | 1307 | |
| 1308 | addInstMethSummary("NSWindow", NSWindowSumm, "initWithContentRect", |
| 1309 | "styleMask", "backing", "defer", NULL); |
| 1310 | |
| 1311 | addInstMethSummary("NSWindow", NSWindowSumm, "initWithContentRect", |
| 1312 | "styleMask", "backing", "defer", "screen", NULL); |
Ted Kremenek | fbf2dc5 | 2009-03-04 23:30:42 +0000 | [diff] [blame] | 1313 | #endif |
Ted Kremenek | 45642a4 | 2008-08-12 18:48:50 +0000 | [diff] [blame] | 1314 | |
| 1315 | // For NSPanel (which subclasses NSWindow), allocated objects are not |
| 1316 | // self-owned. |
Ted Kremenek | e5a036a | 2009-04-03 19:02:51 +0000 | [diff] [blame] | 1317 | // FIXME: For now we don't track NSPanels. object for the same reason |
| 1318 | // as for NSWindow objects. |
| 1319 | addClassMethSummary("NSPanel", "alloc", NoTrackYet); |
| 1320 | |
Ted Kremenek | 45642a4 | 2008-08-12 18:48:50 +0000 | [diff] [blame] | 1321 | addInstMethSummary("NSPanel", InitSumm, "initWithContentRect", |
| 1322 | "styleMask", "backing", "defer", NULL); |
| 1323 | |
| 1324 | addInstMethSummary("NSPanel", InitSumm, "initWithContentRect", |
| 1325 | "styleMask", "backing", "defer", "screen", NULL); |
Ted Kremenek | 272aa85 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 1326 | |
Ted Kremenek | f2717b0 | 2008-07-18 17:24:20 +0000 | [diff] [blame] | 1327 | // Create NSAssertionHandler summaries. |
Ted Kremenek | 3d6ddbb | 2008-08-12 18:30:56 +0000 | [diff] [blame] | 1328 | addPanicSummary("NSAssertionHandler", "handleFailureInFunction", "file", |
| 1329 | "lineNumber", "description", NULL); |
Ted Kremenek | f2717b0 | 2008-07-18 17:24:20 +0000 | [diff] [blame] | 1330 | |
Ted Kremenek | 3d6ddbb | 2008-08-12 18:30:56 +0000 | [diff] [blame] | 1331 | addPanicSummary("NSAssertionHandler", "handleFailureInMethod", "object", |
| 1332 | "file", "lineNumber", "description", NULL); |
Ted Kremenek | 83b2cde | 2008-05-06 00:38:54 +0000 | [diff] [blame] | 1333 | } |
| 1334 | |
Ted Kremenek | a8c3c43 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 1335 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 7aef484 | 2008-04-16 20:40:59 +0000 | [diff] [blame] | 1336 | // Reference-counting logic (typestate + counts). |
Ted Kremenek | a7338b4 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 1337 | //===----------------------------------------------------------------------===// |
| 1338 | |
Ted Kremenek | a7338b4 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 1339 | namespace { |
| 1340 | |
Ted Kremenek | 7d421f3 | 2008-04-09 23:49:11 +0000 | [diff] [blame] | 1341 | class VISIBILITY_HIDDEN RefVal { |
Ted Kremenek | d9ccf68 | 2008-04-17 18:12:53 +0000 | [diff] [blame] | 1342 | public: |
Ted Kremenek | d9ccf68 | 2008-04-17 18:12:53 +0000 | [diff] [blame] | 1343 | enum Kind { |
| 1344 | Owned = 0, // Owning reference. |
| 1345 | NotOwned, // Reference is not owned by still valid (not freed). |
| 1346 | Released, // Object has been released. |
| 1347 | ReturnedOwned, // Returned object passes ownership to caller. |
| 1348 | ReturnedNotOwned, // Return object does not pass ownership to caller. |
Ted Kremenek | 6537a64 | 2009-03-17 19:42:23 +0000 | [diff] [blame] | 1349 | ERROR_START, |
| 1350 | ErrorDeallocNotOwned, // -dealloc called on non-owned object. |
| 1351 | ErrorDeallocGC, // Calling -dealloc with GC enabled. |
Ted Kremenek | d9ccf68 | 2008-04-17 18:12:53 +0000 | [diff] [blame] | 1352 | ErrorUseAfterRelease, // Object used after released. |
| 1353 | ErrorReleaseNotOwned, // Release of an object that was not owned. |
Ted Kremenek | 6537a64 | 2009-03-17 19:42:23 +0000 | [diff] [blame] | 1354 | ERROR_LEAK_START, |
Ted Kremenek | 311f3d4 | 2008-10-22 23:56:21 +0000 | [diff] [blame] | 1355 | ErrorLeak, // A memory leak due to excessive reference counts. |
| 1356 | ErrorLeakReturned // A memory leak due to the returning method not having |
| 1357 | // the correct naming conventions. |
Ted Kremenek | d9ccf68 | 2008-04-17 18:12:53 +0000 | [diff] [blame] | 1358 | }; |
Ted Kremenek | 68621b9 | 2009-01-28 05:56:51 +0000 | [diff] [blame] | 1359 | |
| 1360 | private: |
Ted Kremenek | d9ccf68 | 2008-04-17 18:12:53 +0000 | [diff] [blame] | 1361 | Kind kind; |
Ted Kremenek | 68621b9 | 2009-01-28 05:56:51 +0000 | [diff] [blame] | 1362 | RetEffect::ObjKind okind; |
Ted Kremenek | d9ccf68 | 2008-04-17 18:12:53 +0000 | [diff] [blame] | 1363 | unsigned Cnt; |
Ted Kremenek | 4d99d34 | 2009-05-08 20:01:42 +0000 | [diff] [blame^] | 1364 | unsigned ACnt; |
Ted Kremenek | 272aa85 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 1365 | QualType T; |
| 1366 | |
Ted Kremenek | 4d99d34 | 2009-05-08 20:01:42 +0000 | [diff] [blame^] | 1367 | RefVal(Kind k, RetEffect::ObjKind o, unsigned cnt, unsigned acnt, QualType t) |
| 1368 | : kind(k), okind(o), Cnt(cnt), ACnt(acnt), T(t) {} |
Ted Kremenek | 0d72157 | 2008-03-11 17:48:22 +0000 | [diff] [blame] | 1369 | |
Ted Kremenek | 68621b9 | 2009-01-28 05:56:51 +0000 | [diff] [blame] | 1370 | RefVal(Kind k, unsigned cnt = 0) |
Ted Kremenek | 4d99d34 | 2009-05-08 20:01:42 +0000 | [diff] [blame^] | 1371 | : kind(k), okind(RetEffect::AnyObj), Cnt(cnt), ACnt(0) {} |
Ted Kremenek | 68621b9 | 2009-01-28 05:56:51 +0000 | [diff] [blame] | 1372 | |
| 1373 | public: |
Ted Kremenek | d9ccf68 | 2008-04-17 18:12:53 +0000 | [diff] [blame] | 1374 | Kind getKind() const { return kind; } |
Ted Kremenek | 68621b9 | 2009-01-28 05:56:51 +0000 | [diff] [blame] | 1375 | |
| 1376 | RetEffect::ObjKind getObjKind() const { return okind; } |
Ted Kremenek | 0d72157 | 2008-03-11 17:48:22 +0000 | [diff] [blame] | 1377 | |
Ted Kremenek | 4d99d34 | 2009-05-08 20:01:42 +0000 | [diff] [blame^] | 1378 | unsigned getCount() const { return Cnt; } |
| 1379 | unsigned getAutoreleaseCount() const { return ACnt; } |
| 1380 | unsigned getCombinedCounts() const { return Cnt + ACnt; } |
| 1381 | void clearCounts() { Cnt = 0; ACnt = 0; } |
Ted Kremenek | 6537a64 | 2009-03-17 19:42:23 +0000 | [diff] [blame] | 1382 | |
Ted Kremenek | 272aa85 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 1383 | QualType getType() const { return T; } |
Ted Kremenek | d9ccf68 | 2008-04-17 18:12:53 +0000 | [diff] [blame] | 1384 | |
| 1385 | // Useful predicates. |
Ted Kremenek | 0d72157 | 2008-03-11 17:48:22 +0000 | [diff] [blame] | 1386 | |
Ted Kremenek | 6537a64 | 2009-03-17 19:42:23 +0000 | [diff] [blame] | 1387 | static bool isError(Kind k) { return k >= ERROR_START; } |
Ted Kremenek | 1daa16c | 2008-03-11 18:14:09 +0000 | [diff] [blame] | 1388 | |
Ted Kremenek | 6537a64 | 2009-03-17 19:42:23 +0000 | [diff] [blame] | 1389 | static bool isLeak(Kind k) { return k >= ERROR_LEAK_START; } |
Ted Kremenek | 3f3c9c8 | 2008-04-16 22:32:20 +0000 | [diff] [blame] | 1390 | |
Ted Kremenek | ffefc35 | 2008-04-11 22:25:11 +0000 | [diff] [blame] | 1391 | bool isOwned() const { |
| 1392 | return getKind() == Owned; |
| 1393 | } |
| 1394 | |
Ted Kremenek | 3f3c9c8 | 2008-04-16 22:32:20 +0000 | [diff] [blame] | 1395 | bool isNotOwned() const { |
| 1396 | return getKind() == NotOwned; |
| 1397 | } |
| 1398 | |
Ted Kremenek | d9ccf68 | 2008-04-17 18:12:53 +0000 | [diff] [blame] | 1399 | bool isReturnedOwned() const { |
| 1400 | return getKind() == ReturnedOwned; |
| 1401 | } |
| 1402 | |
| 1403 | bool isReturnedNotOwned() const { |
| 1404 | return getKind() == ReturnedNotOwned; |
| 1405 | } |
| 1406 | |
| 1407 | bool isNonLeakError() const { |
| 1408 | Kind k = getKind(); |
| 1409 | return isError(k) && !isLeak(k); |
| 1410 | } |
| 1411 | |
Ted Kremenek | 68621b9 | 2009-01-28 05:56:51 +0000 | [diff] [blame] | 1412 | static RefVal makeOwned(RetEffect::ObjKind o, QualType t, |
| 1413 | unsigned Count = 1) { |
Ted Kremenek | 4d99d34 | 2009-05-08 20:01:42 +0000 | [diff] [blame^] | 1414 | return RefVal(Owned, o, Count, 0, t); |
Ted Kremenek | c4f8102 | 2008-04-10 23:09:18 +0000 | [diff] [blame] | 1415 | } |
| 1416 | |
Ted Kremenek | 68621b9 | 2009-01-28 05:56:51 +0000 | [diff] [blame] | 1417 | static RefVal makeNotOwned(RetEffect::ObjKind o, QualType t, |
| 1418 | unsigned Count = 0) { |
Ted Kremenek | 4d99d34 | 2009-05-08 20:01:42 +0000 | [diff] [blame^] | 1419 | return RefVal(NotOwned, o, Count, 0, t); |
Ted Kremenek | c4f8102 | 2008-04-10 23:09:18 +0000 | [diff] [blame] | 1420 | } |
Ted Kremenek | d9ccf68 | 2008-04-17 18:12:53 +0000 | [diff] [blame] | 1421 | |
| 1422 | static RefVal makeReturnedOwned(unsigned Count) { |
| 1423 | return RefVal(ReturnedOwned, Count); |
| 1424 | } |
| 1425 | |
| 1426 | static RefVal makeReturnedNotOwned() { |
| 1427 | return RefVal(ReturnedNotOwned); |
| 1428 | } |
| 1429 | |
Ted Kremenek | d9ccf68 | 2008-04-17 18:12:53 +0000 | [diff] [blame] | 1430 | // Comparison, profiling, and pretty-printing. |
Ted Kremenek | 0d72157 | 2008-03-11 17:48:22 +0000 | [diff] [blame] | 1431 | |
Ted Kremenek | d9ccf68 | 2008-04-17 18:12:53 +0000 | [diff] [blame] | 1432 | bool operator==(const RefVal& X) const { |
Ted Kremenek | 272aa85 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 1433 | return kind == X.kind && Cnt == X.Cnt && T == X.T; |
Ted Kremenek | d9ccf68 | 2008-04-17 18:12:53 +0000 | [diff] [blame] | 1434 | } |
Ted Kremenek | 3b11f7a | 2008-03-11 19:44:10 +0000 | [diff] [blame] | 1435 | |
Ted Kremenek | 272aa85 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 1436 | RefVal operator-(size_t i) const { |
Ted Kremenek | 4d99d34 | 2009-05-08 20:01:42 +0000 | [diff] [blame^] | 1437 | return RefVal(getKind(), getObjKind(), getCount() - i, |
| 1438 | getAutoreleaseCount(), getType()); |
Ted Kremenek | 272aa85 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 1439 | } |
| 1440 | |
| 1441 | RefVal operator+(size_t i) const { |
Ted Kremenek | 4d99d34 | 2009-05-08 20:01:42 +0000 | [diff] [blame^] | 1442 | return RefVal(getKind(), getObjKind(), getCount() + i, |
| 1443 | getAutoreleaseCount(), getType()); |
Ted Kremenek | 272aa85 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 1444 | } |
| 1445 | |
| 1446 | RefVal operator^(Kind k) const { |
Ted Kremenek | 4d99d34 | 2009-05-08 20:01:42 +0000 | [diff] [blame^] | 1447 | return RefVal(k, getObjKind(), getCount(), getAutoreleaseCount(), |
| 1448 | getType()); |
| 1449 | } |
| 1450 | |
| 1451 | RefVal autorelease() const { |
| 1452 | return RefVal(getKind(), getObjKind(), getCount(), getAutoreleaseCount()+1, |
| 1453 | getType()); |
Ted Kremenek | 272aa85 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 1454 | } |
Ted Kremenek | 6537a64 | 2009-03-17 19:42:23 +0000 | [diff] [blame] | 1455 | |
Ted Kremenek | d9ccf68 | 2008-04-17 18:12:53 +0000 | [diff] [blame] | 1456 | void Profile(llvm::FoldingSetNodeID& ID) const { |
| 1457 | ID.AddInteger((unsigned) kind); |
| 1458 | ID.AddInteger(Cnt); |
Ted Kremenek | 4d99d34 | 2009-05-08 20:01:42 +0000 | [diff] [blame^] | 1459 | ID.AddInteger(ACnt); |
Ted Kremenek | 272aa85 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 1460 | ID.Add(T); |
Ted Kremenek | d9ccf68 | 2008-04-17 18:12:53 +0000 | [diff] [blame] | 1461 | } |
| 1462 | |
Ted Kremenek | 3b11f7a | 2008-03-11 19:44:10 +0000 | [diff] [blame] | 1463 | void print(std::ostream& Out) const; |
Ted Kremenek | 0d72157 | 2008-03-11 17:48:22 +0000 | [diff] [blame] | 1464 | }; |
Ted Kremenek | 3b11f7a | 2008-03-11 19:44:10 +0000 | [diff] [blame] | 1465 | |
| 1466 | void RefVal::print(std::ostream& Out) const { |
Ted Kremenek | 272aa85 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 1467 | if (!T.isNull()) |
| 1468 | Out << "Tracked Type:" << T.getAsString() << '\n'; |
| 1469 | |
Ted Kremenek | 3b11f7a | 2008-03-11 19:44:10 +0000 | [diff] [blame] | 1470 | switch (getKind()) { |
| 1471 | default: assert(false); |
Ted Kremenek | c4f8102 | 2008-04-10 23:09:18 +0000 | [diff] [blame] | 1472 | case Owned: { |
| 1473 | Out << "Owned"; |
| 1474 | unsigned cnt = getCount(); |
| 1475 | if (cnt) Out << " (+ " << cnt << ")"; |
Ted Kremenek | 3b11f7a | 2008-03-11 19:44:10 +0000 | [diff] [blame] | 1476 | break; |
Ted Kremenek | c4f8102 | 2008-04-10 23:09:18 +0000 | [diff] [blame] | 1477 | } |
Ted Kremenek | 3b11f7a | 2008-03-11 19:44:10 +0000 | [diff] [blame] | 1478 | |
Ted Kremenek | c4f8102 | 2008-04-10 23:09:18 +0000 | [diff] [blame] | 1479 | case NotOwned: { |
Ted Kremenek | d9ccf68 | 2008-04-17 18:12:53 +0000 | [diff] [blame] | 1480 | Out << "NotOwned"; |
Ted Kremenek | c4f8102 | 2008-04-10 23:09:18 +0000 | [diff] [blame] | 1481 | unsigned cnt = getCount(); |
| 1482 | if (cnt) Out << " (+ " << cnt << ")"; |
Ted Kremenek | 3b11f7a | 2008-03-11 19:44:10 +0000 | [diff] [blame] | 1483 | break; |
Ted Kremenek | c4f8102 | 2008-04-10 23:09:18 +0000 | [diff] [blame] | 1484 | } |
Ted Kremenek | 3b11f7a | 2008-03-11 19:44:10 +0000 | [diff] [blame] | 1485 | |
Ted Kremenek | d9ccf68 | 2008-04-17 18:12:53 +0000 | [diff] [blame] | 1486 | case ReturnedOwned: { |
| 1487 | Out << "ReturnedOwned"; |
| 1488 | unsigned cnt = getCount(); |
| 1489 | if (cnt) Out << " (+ " << cnt << ")"; |
| 1490 | break; |
| 1491 | } |
| 1492 | |
| 1493 | case ReturnedNotOwned: { |
| 1494 | Out << "ReturnedNotOwned"; |
| 1495 | unsigned cnt = getCount(); |
| 1496 | if (cnt) Out << " (+ " << cnt << ")"; |
| 1497 | break; |
| 1498 | } |
| 1499 | |
Ted Kremenek | 3b11f7a | 2008-03-11 19:44:10 +0000 | [diff] [blame] | 1500 | case Released: |
| 1501 | Out << "Released"; |
| 1502 | break; |
Ted Kremenek | 6537a64 | 2009-03-17 19:42:23 +0000 | [diff] [blame] | 1503 | |
| 1504 | case ErrorDeallocGC: |
| 1505 | Out << "-dealloc (GC)"; |
| 1506 | break; |
| 1507 | |
| 1508 | case ErrorDeallocNotOwned: |
| 1509 | Out << "-dealloc (not-owned)"; |
| 1510 | break; |
Ted Kremenek | 3b11f7a | 2008-03-11 19:44:10 +0000 | [diff] [blame] | 1511 | |
Ted Kremenek | 3f3c9c8 | 2008-04-16 22:32:20 +0000 | [diff] [blame] | 1512 | case ErrorLeak: |
| 1513 | Out << "Leaked"; |
| 1514 | break; |
| 1515 | |
Ted Kremenek | 311f3d4 | 2008-10-22 23:56:21 +0000 | [diff] [blame] | 1516 | case ErrorLeakReturned: |
| 1517 | Out << "Leaked (Bad naming)"; |
| 1518 | break; |
| 1519 | |
Ted Kremenek | 3b11f7a | 2008-03-11 19:44:10 +0000 | [diff] [blame] | 1520 | case ErrorUseAfterRelease: |
| 1521 | Out << "Use-After-Release [ERROR]"; |
| 1522 | break; |
| 1523 | |
| 1524 | case ErrorReleaseNotOwned: |
| 1525 | Out << "Release of Not-Owned [ERROR]"; |
| 1526 | break; |
| 1527 | } |
Ted Kremenek | 4d99d34 | 2009-05-08 20:01:42 +0000 | [diff] [blame^] | 1528 | |
| 1529 | if (ACnt) { |
| 1530 | Out << " [ARC +" << ACnt << ']'; |
| 1531 | } |
Ted Kremenek | 3b11f7a | 2008-03-11 19:44:10 +0000 | [diff] [blame] | 1532 | } |
Ted Kremenek | 0d72157 | 2008-03-11 17:48:22 +0000 | [diff] [blame] | 1533 | |
Ted Kremenek | 4ae925c | 2008-08-14 21:16:54 +0000 | [diff] [blame] | 1534 | } // end anonymous namespace |
| 1535 | |
| 1536 | //===----------------------------------------------------------------------===// |
| 1537 | // RefBindings - State used to track object reference counts. |
| 1538 | //===----------------------------------------------------------------------===// |
| 1539 | |
Ted Kremenek | b9cd9a7 | 2008-12-05 02:27:51 +0000 | [diff] [blame] | 1540 | typedef llvm::ImmutableMap<SymbolRef, RefVal> RefBindings; |
Ted Kremenek | 4ae925c | 2008-08-14 21:16:54 +0000 | [diff] [blame] | 1541 | static int RefBIndex = 0; |
Ted Kremenek | 876d8df | 2009-02-19 23:47:02 +0000 | [diff] [blame] | 1542 | static std::pair<const void*, const void*> LeakProgramPointTag(&RefBIndex, 0); |
Ted Kremenek | 4ae925c | 2008-08-14 21:16:54 +0000 | [diff] [blame] | 1543 | |
| 1544 | namespace clang { |
Ted Kremenek | 9178120 | 2008-08-17 03:20:02 +0000 | [diff] [blame] | 1545 | template<> |
| 1546 | struct GRStateTrait<RefBindings> : public GRStatePartialTrait<RefBindings> { |
| 1547 | static inline void* GDMIndex() { return &RefBIndex; } |
| 1548 | }; |
| 1549 | } |
Ted Kremenek | c8c8d2c | 2008-10-21 15:53:15 +0000 | [diff] [blame] | 1550 | |
| 1551 | //===----------------------------------------------------------------------===// |
Ted Kremenek | b657894 | 2009-02-24 19:15:11 +0000 | [diff] [blame] | 1552 | // AutoreleaseBindings - State used to track objects in autorelease pools. |
Ted Kremenek | c8c8d2c | 2008-10-21 15:53:15 +0000 | [diff] [blame] | 1553 | //===----------------------------------------------------------------------===// |
| 1554 | |
Ted Kremenek | b657894 | 2009-02-24 19:15:11 +0000 | [diff] [blame] | 1555 | typedef llvm::ImmutableMap<SymbolRef, unsigned> ARCounts; |
| 1556 | typedef llvm::ImmutableMap<SymbolRef, ARCounts> ARPoolContents; |
| 1557 | typedef llvm::ImmutableList<SymbolRef> ARStack; |
Ted Kremenek | aac8283 | 2009-02-23 17:45:03 +0000 | [diff] [blame] | 1558 | |
Ted Kremenek | b657894 | 2009-02-24 19:15:11 +0000 | [diff] [blame] | 1559 | static int AutoRCIndex = 0; |
Ted Kremenek | c8c8d2c | 2008-10-21 15:53:15 +0000 | [diff] [blame] | 1560 | static int AutoRBIndex = 0; |
| 1561 | |
Ted Kremenek | b657894 | 2009-02-24 19:15:11 +0000 | [diff] [blame] | 1562 | namespace { class VISIBILITY_HIDDEN AutoreleasePoolContents {}; } |
Ted Kremenek | 1b4b656 | 2009-02-25 02:54:57 +0000 | [diff] [blame] | 1563 | namespace { class VISIBILITY_HIDDEN AutoreleaseStack {}; } |
Ted Kremenek | b657894 | 2009-02-24 19:15:11 +0000 | [diff] [blame] | 1564 | |
Ted Kremenek | c8c8d2c | 2008-10-21 15:53:15 +0000 | [diff] [blame] | 1565 | namespace clang { |
Ted Kremenek | 1b4b656 | 2009-02-25 02:54:57 +0000 | [diff] [blame] | 1566 | template<> struct GRStateTrait<AutoreleaseStack> |
Ted Kremenek | b657894 | 2009-02-24 19:15:11 +0000 | [diff] [blame] | 1567 | : public GRStatePartialTrait<ARStack> { |
| 1568 | static inline void* GDMIndex() { return &AutoRBIndex; } |
| 1569 | }; |
| 1570 | |
| 1571 | template<> struct GRStateTrait<AutoreleasePoolContents> |
| 1572 | : public GRStatePartialTrait<ARPoolContents> { |
| 1573 | static inline void* GDMIndex() { return &AutoRCIndex; } |
| 1574 | }; |
| 1575 | } // end clang namespace |
Ted Kremenek | c8c8d2c | 2008-10-21 15:53:15 +0000 | [diff] [blame] | 1576 | |
Ted Kremenek | 681fb35 | 2009-03-20 17:34:15 +0000 | [diff] [blame] | 1577 | static SymbolRef GetCurrentAutoreleasePool(const GRState* state) { |
| 1578 | ARStack stack = state->get<AutoreleaseStack>(); |
| 1579 | return stack.isEmpty() ? SymbolRef() : stack.getHead(); |
| 1580 | } |
| 1581 | |
| 1582 | static GRStateRef SendAutorelease(GRStateRef state, ARCounts::Factory &F, |
| 1583 | SymbolRef sym) { |
| 1584 | |
| 1585 | SymbolRef pool = GetCurrentAutoreleasePool(state); |
| 1586 | const ARCounts *cnts = state.get<AutoreleasePoolContents>(pool); |
| 1587 | ARCounts newCnts(0); |
| 1588 | |
| 1589 | if (cnts) { |
| 1590 | const unsigned *cnt = (*cnts).lookup(sym); |
| 1591 | newCnts = F.Add(*cnts, sym, cnt ? *cnt + 1 : 1); |
| 1592 | } |
| 1593 | else |
| 1594 | newCnts = F.Add(F.GetEmptyMap(), sym, 1); |
| 1595 | |
| 1596 | return state.set<AutoreleasePoolContents>(pool, newCnts); |
| 1597 | } |
| 1598 | |
Ted Kremenek | 7aef484 | 2008-04-16 20:40:59 +0000 | [diff] [blame] | 1599 | //===----------------------------------------------------------------------===// |
| 1600 | // Transfer functions. |
| 1601 | //===----------------------------------------------------------------------===// |
| 1602 | |
Ted Kremenek | 4ae925c | 2008-08-14 21:16:54 +0000 | [diff] [blame] | 1603 | namespace { |
| 1604 | |
Ted Kremenek | 7d421f3 | 2008-04-09 23:49:11 +0000 | [diff] [blame] | 1605 | class VISIBILITY_HIDDEN CFRefCount : public GRSimpleVals { |
Ted Kremenek | 2be7ddb | 2008-04-18 03:39:05 +0000 | [diff] [blame] | 1606 | public: |
Ted Kremenek | bccfbcc | 2008-08-13 21:24:49 +0000 | [diff] [blame] | 1607 | class BindingsPrinter : public GRState::Printer { |
Ted Kremenek | 3b11f7a | 2008-03-11 19:44:10 +0000 | [diff] [blame] | 1608 | public: |
Ted Kremenek | bccfbcc | 2008-08-13 21:24:49 +0000 | [diff] [blame] | 1609 | virtual void Print(std::ostream& Out, const GRState* state, |
| 1610 | const char* nl, const char* sep); |
Ted Kremenek | 3b11f7a | 2008-03-11 19:44:10 +0000 | [diff] [blame] | 1611 | }; |
Ted Kremenek | 2be7ddb | 2008-04-18 03:39:05 +0000 | [diff] [blame] | 1612 | |
| 1613 | private: |
Ted Kremenek | c26c469 | 2009-02-18 03:48:14 +0000 | [diff] [blame] | 1614 | typedef llvm::DenseMap<const GRExprEngine::NodeTy*, const RetainSummary*> |
| 1615 | SummaryLogTy; |
| 1616 | |
Ted Kremenek | a8c3c43 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 1617 | RetainSummaryManager Summaries; |
Ted Kremenek | c26c469 | 2009-02-18 03:48:14 +0000 | [diff] [blame] | 1618 | SummaryLogTy SummaryLog; |
Ted Kremenek | a8c3c43 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 1619 | const LangOptions& LOpts; |
Ted Kremenek | b657894 | 2009-02-24 19:15:11 +0000 | [diff] [blame] | 1620 | ARCounts::Factory ARCountFactory; |
Ted Kremenek | 9178120 | 2008-08-17 03:20:02 +0000 | [diff] [blame] | 1621 | |
Ted Kremenek | 708af04 | 2009-02-05 06:50:21 +0000 | [diff] [blame] | 1622 | BugType *useAfterRelease, *releaseNotOwned; |
Ted Kremenek | 6537a64 | 2009-03-17 19:42:23 +0000 | [diff] [blame] | 1623 | BugType *deallocGC, *deallocNotOwned; |
Ted Kremenek | 708af04 | 2009-02-05 06:50:21 +0000 | [diff] [blame] | 1624 | BugType *leakWithinFunction, *leakAtReturn; |
| 1625 | BugReporter *BR; |
Ted Kremenek | a7338b4 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 1626 | |
Ted Kremenek | b657894 | 2009-02-24 19:15:11 +0000 | [diff] [blame] | 1627 | GRStateRef Update(GRStateRef state, SymbolRef sym, RefVal V, ArgEffect E, |
| 1628 | RefVal::Kind& hasErr); |
| 1629 | |
Ted Kremenek | abd89ac | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 1630 | void ProcessNonLeakError(ExplodedNodeSet<GRState>& Dst, |
| 1631 | GRStmtNodeBuilder<GRState>& Builder, |
Ted Kremenek | 3f3c9c8 | 2008-04-16 22:32:20 +0000 | [diff] [blame] | 1632 | Expr* NodeExpr, Expr* ErrorExpr, |
Ted Kremenek | abd89ac | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 1633 | ExplodedNode<GRState>* Pred, |
| 1634 | const GRState* St, |
Ted Kremenek | b9cd9a7 | 2008-12-05 02:27:51 +0000 | [diff] [blame] | 1635 | RefVal::Kind hasErr, SymbolRef Sym); |
Ted Kremenek | 3f3c9c8 | 2008-04-16 22:32:20 +0000 | [diff] [blame] | 1636 | |
Ted Kremenek | 0106e20 | 2008-10-24 20:32:50 +0000 | [diff] [blame] | 1637 | std::pair<GRStateRef, bool> |
| 1638 | HandleSymbolDeath(GRStateManager& VMgr, const GRState* St, |
Ted Kremenek | b9cd9a7 | 2008-12-05 02:27:51 +0000 | [diff] [blame] | 1639 | const Decl* CD, SymbolRef sid, RefVal V, bool& hasLeak); |
Ted Kremenek | 3f3c9c8 | 2008-04-16 22:32:20 +0000 | [diff] [blame] | 1640 | |
Ted Kremenek | b657894 | 2009-02-24 19:15:11 +0000 | [diff] [blame] | 1641 | public: |
Ted Kremenek | 9f20c7c | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 1642 | CFRefCount(ASTContext& Ctx, bool gcenabled, const LangOptions& lopts) |
Ted Kremenek | 9b0c09c | 2008-04-29 05:33:51 +0000 | [diff] [blame] | 1643 | : Summaries(Ctx, gcenabled), |
Ted Kremenek | 6537a64 | 2009-03-17 19:42:23 +0000 | [diff] [blame] | 1644 | LOpts(lopts), useAfterRelease(0), releaseNotOwned(0), |
| 1645 | deallocGC(0), deallocNotOwned(0), |
Ted Kremenek | 708af04 | 2009-02-05 06:50:21 +0000 | [diff] [blame] | 1646 | leakWithinFunction(0), leakAtReturn(0), BR(0) {} |
Ted Kremenek | 1feab29 | 2008-04-16 04:28:53 +0000 | [diff] [blame] | 1647 | |
Ted Kremenek | 708af04 | 2009-02-05 06:50:21 +0000 | [diff] [blame] | 1648 | virtual ~CFRefCount() {} |
Ted Kremenek | 7d421f3 | 2008-04-09 23:49:11 +0000 | [diff] [blame] | 1649 | |
Ted Kremenek | bf6babf | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 1650 | void RegisterChecks(BugReporter &BR); |
Ted Kremenek | 3b11f7a | 2008-03-11 19:44:10 +0000 | [diff] [blame] | 1651 | |
Ted Kremenek | b0f2b9e | 2008-08-16 00:49:49 +0000 | [diff] [blame] | 1652 | virtual void RegisterPrinters(std::vector<GRState::Printer*>& Printers) { |
| 1653 | Printers.push_back(new BindingsPrinter()); |
Ted Kremenek | 3b11f7a | 2008-03-11 19:44:10 +0000 | [diff] [blame] | 1654 | } |
Ted Kremenek | a7338b4 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 1655 | |
Ted Kremenek | a8c3c43 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 1656 | bool isGCEnabled() const { return Summaries.isGCEnabled(); } |
Ted Kremenek | fe30beb | 2008-04-30 23:47:44 +0000 | [diff] [blame] | 1657 | const LangOptions& getLangOptions() const { return LOpts; } |
| 1658 | |
Ted Kremenek | c26c469 | 2009-02-18 03:48:14 +0000 | [diff] [blame] | 1659 | const RetainSummary *getSummaryOfNode(const ExplodedNode<GRState> *N) const { |
| 1660 | SummaryLogTy::const_iterator I = SummaryLog.find(N); |
| 1661 | return I == SummaryLog.end() ? 0 : I->second; |
| 1662 | } |
| 1663 | |
Ted Kremenek | a7338b4 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 1664 | // Calls. |
Ted Kremenek | a8c3c43 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 1665 | |
Ted Kremenek | abd89ac | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 1666 | void EvalSummary(ExplodedNodeSet<GRState>& Dst, |
Ted Kremenek | a8c3c43 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 1667 | GRExprEngine& Eng, |
Ted Kremenek | abd89ac | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 1668 | GRStmtNodeBuilder<GRState>& Builder, |
Ted Kremenek | a8c3c43 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 1669 | Expr* Ex, |
| 1670 | Expr* Receiver, |
Ted Kremenek | 286e985 | 2009-05-04 04:57:00 +0000 | [diff] [blame] | 1671 | const RetainSummary& Summ, |
Ted Kremenek | 2719e98 | 2008-06-17 02:43:46 +0000 | [diff] [blame] | 1672 | ExprIterator arg_beg, ExprIterator arg_end, |
Ted Kremenek | abd89ac | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 1673 | ExplodedNode<GRState>* Pred); |
Ted Kremenek | a8c3c43 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 1674 | |
Ted Kremenek | abd89ac | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 1675 | virtual void EvalCall(ExplodedNodeSet<GRState>& Dst, |
Ted Kremenek | ce0767f | 2008-03-12 21:06:49 +0000 | [diff] [blame] | 1676 | GRExprEngine& Eng, |
Ted Kremenek | abd89ac | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 1677 | GRStmtNodeBuilder<GRState>& Builder, |
Zhongxing Xu | 097fc98 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 1678 | CallExpr* CE, SVal L, |
Ted Kremenek | abd89ac | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 1679 | ExplodedNode<GRState>* Pred); |
Ted Kremenek | 10fe66d | 2008-04-09 01:10:13 +0000 | [diff] [blame] | 1680 | |
Ted Kremenek | a8c3c43 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 1681 | |
Ted Kremenek | abd89ac | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 1682 | virtual void EvalObjCMessageExpr(ExplodedNodeSet<GRState>& Dst, |
Ted Kremenek | 4b4738b | 2008-04-15 23:44:31 +0000 | [diff] [blame] | 1683 | GRExprEngine& Engine, |
Ted Kremenek | abd89ac | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 1684 | GRStmtNodeBuilder<GRState>& Builder, |
Ted Kremenek | 4b4738b | 2008-04-15 23:44:31 +0000 | [diff] [blame] | 1685 | ObjCMessageExpr* ME, |
Ted Kremenek | abd89ac | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 1686 | ExplodedNode<GRState>* Pred); |
Ted Kremenek | 4b4738b | 2008-04-15 23:44:31 +0000 | [diff] [blame] | 1687 | |
Ted Kremenek | abd89ac | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 1688 | bool EvalObjCMessageExprAux(ExplodedNodeSet<GRState>& Dst, |
Ted Kremenek | 4b4738b | 2008-04-15 23:44:31 +0000 | [diff] [blame] | 1689 | GRExprEngine& Engine, |
Ted Kremenek | abd89ac | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 1690 | GRStmtNodeBuilder<GRState>& Builder, |
Ted Kremenek | 4b4738b | 2008-04-15 23:44:31 +0000 | [diff] [blame] | 1691 | ObjCMessageExpr* ME, |
Ted Kremenek | abd89ac | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 1692 | ExplodedNode<GRState>* Pred); |
Ted Kremenek | 4b4738b | 2008-04-15 23:44:31 +0000 | [diff] [blame] | 1693 | |
Ted Kremenek | a42be30 | 2009-02-14 01:43:44 +0000 | [diff] [blame] | 1694 | // Stores. |
| 1695 | virtual void EvalBind(GRStmtNodeBuilderRef& B, SVal location, SVal val); |
| 1696 | |
Ted Kremenek | ffefc35 | 2008-04-11 22:25:11 +0000 | [diff] [blame] | 1697 | // End-of-path. |
| 1698 | |
| 1699 | virtual void EvalEndPath(GRExprEngine& Engine, |
Ted Kremenek | abd89ac | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 1700 | GREndPathNodeBuilder<GRState>& Builder); |
Ted Kremenek | ffefc35 | 2008-04-11 22:25:11 +0000 | [diff] [blame] | 1701 | |
Ted Kremenek | abd89ac | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 1702 | virtual void EvalDeadSymbols(ExplodedNodeSet<GRState>& Dst, |
Ted Kremenek | 541db37 | 2008-04-24 23:57:27 +0000 | [diff] [blame] | 1703 | GRExprEngine& Engine, |
Ted Kremenek | abd89ac | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 1704 | GRStmtNodeBuilder<GRState>& Builder, |
| 1705 | ExplodedNode<GRState>* Pred, |
Ted Kremenek | 5c0729b | 2009-01-21 22:26:05 +0000 | [diff] [blame] | 1706 | Stmt* S, const GRState* state, |
| 1707 | SymbolReaper& SymReaper); |
| 1708 | |
Ted Kremenek | d9ccf68 | 2008-04-17 18:12:53 +0000 | [diff] [blame] | 1709 | // Return statements. |
| 1710 | |
Ted Kremenek | abd89ac | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 1711 | virtual void EvalReturn(ExplodedNodeSet<GRState>& Dst, |
Ted Kremenek | d9ccf68 | 2008-04-17 18:12:53 +0000 | [diff] [blame] | 1712 | GRExprEngine& Engine, |
Ted Kremenek | abd89ac | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 1713 | GRStmtNodeBuilder<GRState>& Builder, |
Ted Kremenek | d9ccf68 | 2008-04-17 18:12:53 +0000 | [diff] [blame] | 1714 | ReturnStmt* S, |
Ted Kremenek | abd89ac | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 1715 | ExplodedNode<GRState>* Pred); |
Ted Kremenek | eef8f1e | 2008-04-18 19:23:43 +0000 | [diff] [blame] | 1716 | |
| 1717 | // Assumptions. |
| 1718 | |
Ted Kremenek | abd89ac | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 1719 | virtual const GRState* EvalAssume(GRStateManager& VMgr, |
Zhongxing Xu | 097fc98 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 1720 | const GRState* St, SVal Cond, |
Ted Kremenek | f22f868 | 2008-07-10 22:03:41 +0000 | [diff] [blame] | 1721 | bool Assumption, bool& isFeasible); |
Ted Kremenek | a7338b4 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 1722 | }; |
| 1723 | |
| 1724 | } // end anonymous namespace |
| 1725 | |
Ted Kremenek | 681fb35 | 2009-03-20 17:34:15 +0000 | [diff] [blame] | 1726 | static void PrintPool(std::ostream &Out, SymbolRef Sym, const GRState *state) { |
| 1727 | Out << ' '; |
Ted Kremenek | 74556a1 | 2009-03-26 03:35:11 +0000 | [diff] [blame] | 1728 | if (Sym) |
| 1729 | Out << Sym->getSymbolID(); |
Ted Kremenek | 681fb35 | 2009-03-20 17:34:15 +0000 | [diff] [blame] | 1730 | else |
| 1731 | Out << "<pool>"; |
| 1732 | Out << ":{"; |
| 1733 | |
| 1734 | // Get the contents of the pool. |
| 1735 | if (const ARCounts *cnts = state->get<AutoreleasePoolContents>(Sym)) |
| 1736 | for (ARCounts::iterator J=cnts->begin(), EJ=cnts->end(); J != EJ; ++J) |
| 1737 | Out << '(' << J.getKey() << ',' << J.getData() << ')'; |
| 1738 | |
| 1739 | Out << '}'; |
| 1740 | } |
Ted Kremenek | 2be7ddb | 2008-04-18 03:39:05 +0000 | [diff] [blame] | 1741 | |
Ted Kremenek | bccfbcc | 2008-08-13 21:24:49 +0000 | [diff] [blame] | 1742 | void CFRefCount::BindingsPrinter::Print(std::ostream& Out, const GRState* state, |
| 1743 | const char* nl, const char* sep) { |
Ted Kremenek | 681fb35 | 2009-03-20 17:34:15 +0000 | [diff] [blame] | 1744 | |
| 1745 | |
Ted Kremenek | bccfbcc | 2008-08-13 21:24:49 +0000 | [diff] [blame] | 1746 | |
Ted Kremenek | 4ae925c | 2008-08-14 21:16:54 +0000 | [diff] [blame] | 1747 | RefBindings B = state->get<RefBindings>(); |
Ted Kremenek | 3b11f7a | 2008-03-11 19:44:10 +0000 | [diff] [blame] | 1748 | |
Ted Kremenek | bccfbcc | 2008-08-13 21:24:49 +0000 | [diff] [blame] | 1749 | if (!B.isEmpty()) |
Ted Kremenek | 3b11f7a | 2008-03-11 19:44:10 +0000 | [diff] [blame] | 1750 | Out << sep << nl; |
| 1751 | |
| 1752 | for (RefBindings::iterator I=B.begin(), E=B.end(); I!=E; ++I) { |
| 1753 | Out << (*I).first << " : "; |
| 1754 | (*I).second.print(Out); |
| 1755 | Out << nl; |
| 1756 | } |
Ted Kremenek | 1b4b656 | 2009-02-25 02:54:57 +0000 | [diff] [blame] | 1757 | |
| 1758 | // Print the autorelease stack. |
Ted Kremenek | 681fb35 | 2009-03-20 17:34:15 +0000 | [diff] [blame] | 1759 | Out << sep << nl << "AR pool stack:"; |
Ted Kremenek | 1b4b656 | 2009-02-25 02:54:57 +0000 | [diff] [blame] | 1760 | ARStack stack = state->get<AutoreleaseStack>(); |
Ted Kremenek | 1b4b656 | 2009-02-25 02:54:57 +0000 | [diff] [blame] | 1761 | |
Ted Kremenek | 681fb35 | 2009-03-20 17:34:15 +0000 | [diff] [blame] | 1762 | PrintPool(Out, SymbolRef(), state); // Print the caller's pool. |
| 1763 | for (ARStack::iterator I=stack.begin(), E=stack.end(); I!=E; ++I) |
| 1764 | PrintPool(Out, *I, state); |
| 1765 | |
| 1766 | Out << nl; |
Ted Kremenek | 3b11f7a | 2008-03-11 19:44:10 +0000 | [diff] [blame] | 1767 | } |
| 1768 | |
Ted Kremenek | 47a7242 | 2009-04-29 18:50:19 +0000 | [diff] [blame] | 1769 | //===----------------------------------------------------------------------===// |
| 1770 | // Error reporting. |
| 1771 | //===----------------------------------------------------------------------===// |
| 1772 | |
| 1773 | namespace { |
| 1774 | |
| 1775 | //===-------------===// |
| 1776 | // Bug Descriptions. // |
| 1777 | //===-------------===// |
| 1778 | |
| 1779 | class VISIBILITY_HIDDEN CFRefBug : public BugType { |
| 1780 | protected: |
| 1781 | CFRefCount& TF; |
| 1782 | |
| 1783 | CFRefBug(CFRefCount* tf, const char* name) |
| 1784 | : BugType(name, "Memory (Core Foundation/Objective-C)"), TF(*tf) {} |
| 1785 | public: |
| 1786 | |
| 1787 | CFRefCount& getTF() { return TF; } |
| 1788 | const CFRefCount& getTF() const { return TF; } |
| 1789 | |
| 1790 | // FIXME: Eventually remove. |
| 1791 | virtual const char* getDescription() const = 0; |
| 1792 | |
| 1793 | virtual bool isLeak() const { return false; } |
| 1794 | }; |
| 1795 | |
| 1796 | class VISIBILITY_HIDDEN UseAfterRelease : public CFRefBug { |
| 1797 | public: |
| 1798 | UseAfterRelease(CFRefCount* tf) |
| 1799 | : CFRefBug(tf, "Use-after-release") {} |
| 1800 | |
| 1801 | const char* getDescription() const { |
| 1802 | return "Reference-counted object is used after it is released"; |
| 1803 | } |
| 1804 | }; |
| 1805 | |
| 1806 | class VISIBILITY_HIDDEN BadRelease : public CFRefBug { |
| 1807 | public: |
| 1808 | BadRelease(CFRefCount* tf) : CFRefBug(tf, "Bad release") {} |
| 1809 | |
| 1810 | const char* getDescription() const { |
| 1811 | return "Incorrect decrement of the reference count of an " |
| 1812 | "object is not owned at this point by the caller"; |
| 1813 | } |
| 1814 | }; |
| 1815 | |
| 1816 | class VISIBILITY_HIDDEN DeallocGC : public CFRefBug { |
| 1817 | public: |
| 1818 | DeallocGC(CFRefCount *tf) : CFRefBug(tf, |
| 1819 | "-dealloc called while using GC") {} |
| 1820 | |
| 1821 | const char *getDescription() const { |
| 1822 | return "-dealloc called while using GC"; |
| 1823 | } |
| 1824 | }; |
| 1825 | |
| 1826 | class VISIBILITY_HIDDEN DeallocNotOwned : public CFRefBug { |
| 1827 | public: |
| 1828 | DeallocNotOwned(CFRefCount *tf) : CFRefBug(tf, |
| 1829 | "-dealloc sent to non-exclusively owned object") {} |
| 1830 | |
| 1831 | const char *getDescription() const { |
| 1832 | return "-dealloc sent to object that may be referenced elsewhere"; |
| 1833 | } |
| 1834 | }; |
| 1835 | |
| 1836 | class VISIBILITY_HIDDEN Leak : public CFRefBug { |
| 1837 | const bool isReturn; |
| 1838 | protected: |
| 1839 | Leak(CFRefCount* tf, const char* name, bool isRet) |
| 1840 | : CFRefBug(tf, name), isReturn(isRet) {} |
| 1841 | public: |
| 1842 | |
| 1843 | const char* getDescription() const { return ""; } |
| 1844 | |
| 1845 | bool isLeak() const { return true; } |
| 1846 | }; |
| 1847 | |
| 1848 | class VISIBILITY_HIDDEN LeakAtReturn : public Leak { |
| 1849 | public: |
| 1850 | LeakAtReturn(CFRefCount* tf, const char* name) |
| 1851 | : Leak(tf, name, true) {} |
| 1852 | }; |
| 1853 | |
| 1854 | class VISIBILITY_HIDDEN LeakWithinFunction : public Leak { |
| 1855 | public: |
| 1856 | LeakWithinFunction(CFRefCount* tf, const char* name) |
| 1857 | : Leak(tf, name, false) {} |
| 1858 | }; |
| 1859 | |
| 1860 | //===---------===// |
| 1861 | // Bug Reports. // |
| 1862 | //===---------===// |
| 1863 | |
| 1864 | class VISIBILITY_HIDDEN CFRefReport : public RangedBugReport { |
| 1865 | protected: |
| 1866 | SymbolRef Sym; |
| 1867 | const CFRefCount &TF; |
| 1868 | public: |
| 1869 | CFRefReport(CFRefBug& D, const CFRefCount &tf, |
| 1870 | ExplodedNode<GRState> *n, SymbolRef sym) |
| 1871 | : RangedBugReport(D, D.getDescription(), n), Sym(sym), TF(tf) {} |
| 1872 | |
| 1873 | virtual ~CFRefReport() {} |
| 1874 | |
| 1875 | CFRefBug& getBugType() { |
| 1876 | return (CFRefBug&) RangedBugReport::getBugType(); |
| 1877 | } |
| 1878 | const CFRefBug& getBugType() const { |
| 1879 | return (const CFRefBug&) RangedBugReport::getBugType(); |
| 1880 | } |
| 1881 | |
| 1882 | virtual void getRanges(BugReporter& BR, const SourceRange*& beg, |
| 1883 | const SourceRange*& end) { |
| 1884 | |
| 1885 | if (!getBugType().isLeak()) |
| 1886 | RangedBugReport::getRanges(BR, beg, end); |
| 1887 | else |
| 1888 | beg = end = 0; |
| 1889 | } |
| 1890 | |
| 1891 | SymbolRef getSymbol() const { return Sym; } |
| 1892 | |
Ted Kremenek | c3bc6c8 | 2009-05-06 21:39:49 +0000 | [diff] [blame] | 1893 | PathDiagnosticPiece* getEndPath(BugReporterContext& BRC, |
Ted Kremenek | 47a7242 | 2009-04-29 18:50:19 +0000 | [diff] [blame] | 1894 | const ExplodedNode<GRState>* N); |
| 1895 | |
| 1896 | std::pair<const char**,const char**> getExtraDescriptiveText(); |
| 1897 | |
| 1898 | PathDiagnosticPiece* VisitNode(const ExplodedNode<GRState>* N, |
| 1899 | const ExplodedNode<GRState>* PrevN, |
Ted Kremenek | c3bc6c8 | 2009-05-06 21:39:49 +0000 | [diff] [blame] | 1900 | BugReporterContext& BRC); |
Ted Kremenek | 47a7242 | 2009-04-29 18:50:19 +0000 | [diff] [blame] | 1901 | }; |
| 1902 | |
| 1903 | class VISIBILITY_HIDDEN CFRefLeakReport : public CFRefReport { |
| 1904 | SourceLocation AllocSite; |
| 1905 | const MemRegion* AllocBinding; |
| 1906 | public: |
| 1907 | CFRefLeakReport(CFRefBug& D, const CFRefCount &tf, |
| 1908 | ExplodedNode<GRState> *n, SymbolRef sym, |
| 1909 | GRExprEngine& Eng); |
| 1910 | |
Ted Kremenek | c3bc6c8 | 2009-05-06 21:39:49 +0000 | [diff] [blame] | 1911 | PathDiagnosticPiece* getEndPath(BugReporterContext& BRC, |
Ted Kremenek | 47a7242 | 2009-04-29 18:50:19 +0000 | [diff] [blame] | 1912 | const ExplodedNode<GRState>* N); |
| 1913 | |
| 1914 | SourceLocation getLocation() const { return AllocSite; } |
| 1915 | }; |
| 1916 | } // end anonymous namespace |
| 1917 | |
| 1918 | void CFRefCount::RegisterChecks(BugReporter& BR) { |
| 1919 | useAfterRelease = new UseAfterRelease(this); |
| 1920 | BR.Register(useAfterRelease); |
| 1921 | |
| 1922 | releaseNotOwned = new BadRelease(this); |
| 1923 | BR.Register(releaseNotOwned); |
| 1924 | |
| 1925 | deallocGC = new DeallocGC(this); |
| 1926 | BR.Register(deallocGC); |
| 1927 | |
| 1928 | deallocNotOwned = new DeallocNotOwned(this); |
| 1929 | BR.Register(deallocNotOwned); |
| 1930 | |
| 1931 | // First register "return" leaks. |
| 1932 | const char* name = 0; |
| 1933 | |
| 1934 | if (isGCEnabled()) |
| 1935 | name = "Leak of returned object when using garbage collection"; |
| 1936 | else if (getLangOptions().getGCMode() == LangOptions::HybridGC) |
| 1937 | name = "Leak of returned object when not using garbage collection (GC) in " |
| 1938 | "dual GC/non-GC code"; |
| 1939 | else { |
| 1940 | assert(getLangOptions().getGCMode() == LangOptions::NonGC); |
| 1941 | name = "Leak of returned object"; |
| 1942 | } |
| 1943 | |
| 1944 | leakAtReturn = new LeakAtReturn(this, name); |
| 1945 | BR.Register(leakAtReturn); |
| 1946 | |
| 1947 | // Second, register leaks within a function/method. |
| 1948 | if (isGCEnabled()) |
| 1949 | name = "Leak of object when using garbage collection"; |
| 1950 | else if (getLangOptions().getGCMode() == LangOptions::HybridGC) |
| 1951 | name = "Leak of object when not using garbage collection (GC) in " |
| 1952 | "dual GC/non-GC code"; |
| 1953 | else { |
| 1954 | assert(getLangOptions().getGCMode() == LangOptions::NonGC); |
| 1955 | name = "Leak"; |
| 1956 | } |
| 1957 | |
| 1958 | leakWithinFunction = new LeakWithinFunction(this, name); |
| 1959 | BR.Register(leakWithinFunction); |
| 1960 | |
| 1961 | // Save the reference to the BugReporter. |
| 1962 | this->BR = &BR; |
| 1963 | } |
| 1964 | |
| 1965 | static const char* Msgs[] = { |
| 1966 | // GC only |
| 1967 | "Code is compiled to only use garbage collection", |
| 1968 | // No GC. |
| 1969 | "Code is compiled to use reference counts", |
| 1970 | // Hybrid, with GC. |
| 1971 | "Code is compiled to use either garbage collection (GC) or reference counts" |
| 1972 | " (non-GC). The bug occurs with GC enabled", |
| 1973 | // Hybrid, without GC |
| 1974 | "Code is compiled to use either garbage collection (GC) or reference counts" |
| 1975 | " (non-GC). The bug occurs in non-GC mode" |
| 1976 | }; |
| 1977 | |
| 1978 | std::pair<const char**,const char**> CFRefReport::getExtraDescriptiveText() { |
| 1979 | CFRefCount& TF = static_cast<CFRefBug&>(getBugType()).getTF(); |
| 1980 | |
| 1981 | switch (TF.getLangOptions().getGCMode()) { |
| 1982 | default: |
| 1983 | assert(false); |
| 1984 | |
| 1985 | case LangOptions::GCOnly: |
| 1986 | assert (TF.isGCEnabled()); |
| 1987 | return std::make_pair(&Msgs[0], &Msgs[0]+1); |
| 1988 | |
| 1989 | case LangOptions::NonGC: |
| 1990 | assert (!TF.isGCEnabled()); |
| 1991 | return std::make_pair(&Msgs[1], &Msgs[1]+1); |
| 1992 | |
| 1993 | case LangOptions::HybridGC: |
| 1994 | if (TF.isGCEnabled()) |
| 1995 | return std::make_pair(&Msgs[2], &Msgs[2]+1); |
| 1996 | else |
| 1997 | return std::make_pair(&Msgs[3], &Msgs[3]+1); |
| 1998 | } |
| 1999 | } |
| 2000 | |
| 2001 | static inline bool contains(const llvm::SmallVectorImpl<ArgEffect>& V, |
| 2002 | ArgEffect X) { |
| 2003 | for (llvm::SmallVectorImpl<ArgEffect>::const_iterator I=V.begin(), E=V.end(); |
| 2004 | I!=E; ++I) |
| 2005 | if (*I == X) return true; |
| 2006 | |
| 2007 | return false; |
| 2008 | } |
| 2009 | |
| 2010 | PathDiagnosticPiece* CFRefReport::VisitNode(const ExplodedNode<GRState>* N, |
| 2011 | const ExplodedNode<GRState>* PrevN, |
Ted Kremenek | c3bc6c8 | 2009-05-06 21:39:49 +0000 | [diff] [blame] | 2012 | BugReporterContext& BRC) { |
Ted Kremenek | 47a7242 | 2009-04-29 18:50:19 +0000 | [diff] [blame] | 2013 | |
Ted Kremenek | c3bc6c8 | 2009-05-06 21:39:49 +0000 | [diff] [blame] | 2014 | // Check if the type state has changed. |
| 2015 | GRStateManager &StMgr = BRC.getStateManager(); |
Ted Kremenek | 47a7242 | 2009-04-29 18:50:19 +0000 | [diff] [blame] | 2016 | GRStateRef PrevSt(PrevN->getState(), StMgr); |
| 2017 | GRStateRef CurrSt(N->getState(), StMgr); |
| 2018 | |
| 2019 | const RefVal* CurrT = CurrSt.get<RefBindings>(Sym); |
| 2020 | if (!CurrT) return NULL; |
| 2021 | |
| 2022 | const RefVal& CurrV = *CurrT; |
| 2023 | const RefVal* PrevT = PrevSt.get<RefBindings>(Sym); |
| 2024 | |
| 2025 | // Create a string buffer to constain all the useful things we want |
| 2026 | // to tell the user. |
| 2027 | std::string sbuf; |
| 2028 | llvm::raw_string_ostream os(sbuf); |
| 2029 | |
| 2030 | // This is the allocation site since the previous node had no bindings |
| 2031 | // for this symbol. |
| 2032 | if (!PrevT) { |
| 2033 | Stmt* S = cast<PostStmt>(N->getLocation()).getStmt(); |
| 2034 | |
| 2035 | if (CallExpr *CE = dyn_cast<CallExpr>(S)) { |
| 2036 | // Get the name of the callee (if it is available). |
| 2037 | SVal X = CurrSt.GetSValAsScalarOrLoc(CE->getCallee()); |
| 2038 | if (const FunctionDecl* FD = X.getAsFunctionDecl()) |
| 2039 | os << "Call to function '" << FD->getNameAsString() <<'\''; |
| 2040 | else |
| 2041 | os << "function call"; |
| 2042 | } |
| 2043 | else { |
| 2044 | assert (isa<ObjCMessageExpr>(S)); |
| 2045 | os << "Method"; |
| 2046 | } |
| 2047 | |
| 2048 | if (CurrV.getObjKind() == RetEffect::CF) { |
| 2049 | os << " returns a Core Foundation object with a "; |
| 2050 | } |
| 2051 | else { |
| 2052 | assert (CurrV.getObjKind() == RetEffect::ObjC); |
| 2053 | os << " returns an Objective-C object with a "; |
| 2054 | } |
| 2055 | |
| 2056 | if (CurrV.isOwned()) { |
| 2057 | os << "+1 retain count (owning reference)."; |
| 2058 | |
| 2059 | if (static_cast<CFRefBug&>(getBugType()).getTF().isGCEnabled()) { |
| 2060 | assert(CurrV.getObjKind() == RetEffect::CF); |
| 2061 | os << " " |
| 2062 | "Core Foundation objects are not automatically garbage collected."; |
| 2063 | } |
| 2064 | } |
| 2065 | else { |
| 2066 | assert (CurrV.isNotOwned()); |
| 2067 | os << "+0 retain count (non-owning reference)."; |
| 2068 | } |
| 2069 | |
Ted Kremenek | c3bc6c8 | 2009-05-06 21:39:49 +0000 | [diff] [blame] | 2070 | PathDiagnosticLocation Pos(S, BRC.getSourceManager()); |
Ted Kremenek | 47a7242 | 2009-04-29 18:50:19 +0000 | [diff] [blame] | 2071 | return new PathDiagnosticEventPiece(Pos, os.str()); |
| 2072 | } |
| 2073 | |
| 2074 | // Gather up the effects that were performed on the object at this |
| 2075 | // program point |
| 2076 | llvm::SmallVector<ArgEffect, 2> AEffects; |
| 2077 | |
Ted Kremenek | c3bc6c8 | 2009-05-06 21:39:49 +0000 | [diff] [blame] | 2078 | if (const RetainSummary *Summ = |
| 2079 | TF.getSummaryOfNode(BRC.getNodeResolver().getOriginalNode(N))) { |
Ted Kremenek | 47a7242 | 2009-04-29 18:50:19 +0000 | [diff] [blame] | 2080 | // We only have summaries attached to nodes after evaluating CallExpr and |
| 2081 | // ObjCMessageExprs. |
| 2082 | Stmt* S = cast<PostStmt>(N->getLocation()).getStmt(); |
| 2083 | |
| 2084 | if (CallExpr *CE = dyn_cast<CallExpr>(S)) { |
| 2085 | // Iterate through the parameter expressions and see if the symbol |
| 2086 | // was ever passed as an argument. |
| 2087 | unsigned i = 0; |
| 2088 | |
| 2089 | for (CallExpr::arg_iterator AI=CE->arg_begin(), AE=CE->arg_end(); |
| 2090 | AI!=AE; ++AI, ++i) { |
| 2091 | |
| 2092 | // Retrieve the value of the argument. Is it the symbol |
| 2093 | // we are interested in? |
| 2094 | if (CurrSt.GetSValAsScalarOrLoc(*AI).getAsLocSymbol() != Sym) |
| 2095 | continue; |
| 2096 | |
| 2097 | // We have an argument. Get the effect! |
| 2098 | AEffects.push_back(Summ->getArg(i)); |
| 2099 | } |
| 2100 | } |
| 2101 | else if (ObjCMessageExpr *ME = dyn_cast<ObjCMessageExpr>(S)) { |
| 2102 | if (Expr *receiver = ME->getReceiver()) |
| 2103 | if (CurrSt.GetSValAsScalarOrLoc(receiver).getAsLocSymbol() == Sym) { |
| 2104 | // The symbol we are tracking is the receiver. |
| 2105 | AEffects.push_back(Summ->getReceiverEffect()); |
| 2106 | } |
| 2107 | } |
| 2108 | } |
| 2109 | |
| 2110 | do { |
| 2111 | // Get the previous type state. |
| 2112 | RefVal PrevV = *PrevT; |
| 2113 | |
| 2114 | // Specially handle -dealloc. |
| 2115 | if (!TF.isGCEnabled() && contains(AEffects, Dealloc)) { |
| 2116 | // Determine if the object's reference count was pushed to zero. |
| 2117 | assert(!(PrevV == CurrV) && "The typestate *must* have changed."); |
| 2118 | // We may not have transitioned to 'release' if we hit an error. |
| 2119 | // This case is handled elsewhere. |
| 2120 | if (CurrV.getKind() == RefVal::Released) { |
Ted Kremenek | 4d99d34 | 2009-05-08 20:01:42 +0000 | [diff] [blame^] | 2121 | assert(CurrV.getCombinedCounts() == 0); |
Ted Kremenek | 47a7242 | 2009-04-29 18:50:19 +0000 | [diff] [blame] | 2122 | os << "Object released by directly sending the '-dealloc' message"; |
| 2123 | break; |
| 2124 | } |
| 2125 | } |
| 2126 | |
| 2127 | // Specially handle CFMakeCollectable and friends. |
| 2128 | if (contains(AEffects, MakeCollectable)) { |
| 2129 | // Get the name of the function. |
| 2130 | Stmt* S = cast<PostStmt>(N->getLocation()).getStmt(); |
| 2131 | SVal X = CurrSt.GetSValAsScalarOrLoc(cast<CallExpr>(S)->getCallee()); |
| 2132 | const FunctionDecl* FD = X.getAsFunctionDecl(); |
| 2133 | const std::string& FName = FD->getNameAsString(); |
| 2134 | |
| 2135 | if (TF.isGCEnabled()) { |
| 2136 | // Determine if the object's reference count was pushed to zero. |
| 2137 | assert(!(PrevV == CurrV) && "The typestate *must* have changed."); |
| 2138 | |
| 2139 | os << "In GC mode a call to '" << FName |
| 2140 | << "' decrements an object's retain count and registers the " |
| 2141 | "object with the garbage collector. "; |
| 2142 | |
| 2143 | if (CurrV.getKind() == RefVal::Released) { |
| 2144 | assert(CurrV.getCount() == 0); |
| 2145 | os << "Since it now has a 0 retain count the object can be " |
| 2146 | "automatically collected by the garbage collector."; |
| 2147 | } |
| 2148 | else |
| 2149 | os << "An object must have a 0 retain count to be garbage collected. " |
| 2150 | "After this call its retain count is +" << CurrV.getCount() |
| 2151 | << '.'; |
| 2152 | } |
| 2153 | else |
| 2154 | os << "When GC is not enabled a call to '" << FName |
| 2155 | << "' has no effect on its argument."; |
| 2156 | |
| 2157 | // Nothing more to say. |
| 2158 | break; |
| 2159 | } |
| 2160 | |
| 2161 | // Determine if the typestate has changed. |
| 2162 | if (!(PrevV == CurrV)) |
| 2163 | switch (CurrV.getKind()) { |
| 2164 | case RefVal::Owned: |
| 2165 | case RefVal::NotOwned: |
| 2166 | |
Ted Kremenek | 4d99d34 | 2009-05-08 20:01:42 +0000 | [diff] [blame^] | 2167 | if (PrevV.getCount() == CurrV.getCount()) { |
| 2168 | // Did an autorelease message get sent? |
| 2169 | if (PrevV.getAutoreleaseCount() == CurrV.getAutoreleaseCount()) |
| 2170 | return 0; |
| 2171 | |
| 2172 | assert(PrevV.getAutoreleaseCount() < CurrV.getAutoreleaseCount()); |
| 2173 | os << "Object added to autorelease pool."; |
| 2174 | break; |
| 2175 | } |
Ted Kremenek | 47a7242 | 2009-04-29 18:50:19 +0000 | [diff] [blame] | 2176 | |
| 2177 | if (PrevV.getCount() > CurrV.getCount()) |
| 2178 | os << "Reference count decremented."; |
| 2179 | else |
| 2180 | os << "Reference count incremented."; |
| 2181 | |
| 2182 | if (unsigned Count = CurrV.getCount()) |
| 2183 | os << " The object now has a +" << Count << " retain count."; |
| 2184 | |
| 2185 | if (PrevV.getKind() == RefVal::Released) { |
| 2186 | assert(TF.isGCEnabled() && CurrV.getCount() > 0); |
| 2187 | os << " The object is not eligible for garbage collection until the " |
| 2188 | "retain count reaches 0 again."; |
| 2189 | } |
| 2190 | |
| 2191 | break; |
| 2192 | |
| 2193 | case RefVal::Released: |
| 2194 | os << "Object released."; |
| 2195 | break; |
| 2196 | |
| 2197 | case RefVal::ReturnedOwned: |
| 2198 | os << "Object returned to caller as an owning reference (single retain " |
| 2199 | "count transferred to caller)."; |
| 2200 | break; |
| 2201 | |
| 2202 | case RefVal::ReturnedNotOwned: |
| 2203 | os << "Object returned to caller with a +0 (non-owning) retain count."; |
| 2204 | break; |
| 2205 | |
| 2206 | default: |
| 2207 | return NULL; |
| 2208 | } |
| 2209 | |
| 2210 | // Emit any remaining diagnostics for the argument effects (if any). |
| 2211 | for (llvm::SmallVectorImpl<ArgEffect>::iterator I=AEffects.begin(), |
| 2212 | E=AEffects.end(); I != E; ++I) { |
| 2213 | |
| 2214 | // A bunch of things have alternate behavior under GC. |
| 2215 | if (TF.isGCEnabled()) |
| 2216 | switch (*I) { |
| 2217 | default: break; |
| 2218 | case Autorelease: |
| 2219 | os << "In GC mode an 'autorelease' has no effect."; |
| 2220 | continue; |
| 2221 | case IncRefMsg: |
| 2222 | os << "In GC mode the 'retain' message has no effect."; |
| 2223 | continue; |
| 2224 | case DecRefMsg: |
| 2225 | os << "In GC mode the 'release' message has no effect."; |
| 2226 | continue; |
| 2227 | } |
| 2228 | } |
| 2229 | } while(0); |
| 2230 | |
| 2231 | if (os.str().empty()) |
| 2232 | return 0; // We have nothing to say! |
| 2233 | |
| 2234 | Stmt* S = cast<PostStmt>(N->getLocation()).getStmt(); |
Ted Kremenek | c3bc6c8 | 2009-05-06 21:39:49 +0000 | [diff] [blame] | 2235 | PathDiagnosticLocation Pos(S, BRC.getSourceManager()); |
Ted Kremenek | 47a7242 | 2009-04-29 18:50:19 +0000 | [diff] [blame] | 2236 | PathDiagnosticPiece* P = new PathDiagnosticEventPiece(Pos, os.str()); |
| 2237 | |
| 2238 | // Add the range by scanning the children of the statement for any bindings |
| 2239 | // to Sym. |
| 2240 | for (Stmt::child_iterator I = S->child_begin(), E = S->child_end(); I!=E; ++I) |
| 2241 | if (Expr* Exp = dyn_cast_or_null<Expr>(*I)) |
| 2242 | if (CurrSt.GetSValAsScalarOrLoc(Exp).getAsLocSymbol() == Sym) { |
| 2243 | P->addRange(Exp->getSourceRange()); |
| 2244 | break; |
| 2245 | } |
| 2246 | |
| 2247 | return P; |
| 2248 | } |
| 2249 | |
| 2250 | namespace { |
| 2251 | class VISIBILITY_HIDDEN FindUniqueBinding : |
| 2252 | public StoreManager::BindingsHandler { |
| 2253 | SymbolRef Sym; |
| 2254 | const MemRegion* Binding; |
| 2255 | bool First; |
| 2256 | |
| 2257 | public: |
| 2258 | FindUniqueBinding(SymbolRef sym) : Sym(sym), Binding(0), First(true) {} |
| 2259 | |
| 2260 | bool HandleBinding(StoreManager& SMgr, Store store, const MemRegion* R, |
| 2261 | SVal val) { |
| 2262 | |
| 2263 | SymbolRef SymV = val.getAsSymbol(); |
| 2264 | if (!SymV || SymV != Sym) |
| 2265 | return true; |
| 2266 | |
| 2267 | if (Binding) { |
| 2268 | First = false; |
| 2269 | return false; |
| 2270 | } |
| 2271 | else |
| 2272 | Binding = R; |
| 2273 | |
| 2274 | return true; |
| 2275 | } |
| 2276 | |
| 2277 | operator bool() { return First && Binding; } |
| 2278 | const MemRegion* getRegion() { return Binding; } |
| 2279 | }; |
| 2280 | } |
| 2281 | |
| 2282 | static std::pair<const ExplodedNode<GRState>*,const MemRegion*> |
| 2283 | GetAllocationSite(GRStateManager& StateMgr, const ExplodedNode<GRState>* N, |
| 2284 | SymbolRef Sym) { |
| 2285 | |
| 2286 | // Find both first node that referred to the tracked symbol and the |
| 2287 | // memory location that value was store to. |
| 2288 | const ExplodedNode<GRState>* Last = N; |
| 2289 | const MemRegion* FirstBinding = 0; |
| 2290 | |
| 2291 | while (N) { |
| 2292 | const GRState* St = N->getState(); |
| 2293 | RefBindings B = St->get<RefBindings>(); |
| 2294 | |
| 2295 | if (!B.lookup(Sym)) |
| 2296 | break; |
| 2297 | |
| 2298 | FindUniqueBinding FB(Sym); |
| 2299 | StateMgr.iterBindings(St, FB); |
| 2300 | if (FB) FirstBinding = FB.getRegion(); |
| 2301 | |
| 2302 | Last = N; |
| 2303 | N = N->pred_empty() ? NULL : *(N->pred_begin()); |
| 2304 | } |
| 2305 | |
| 2306 | return std::make_pair(Last, FirstBinding); |
| 2307 | } |
| 2308 | |
| 2309 | PathDiagnosticPiece* |
Ted Kremenek | c3bc6c8 | 2009-05-06 21:39:49 +0000 | [diff] [blame] | 2310 | CFRefReport::getEndPath(BugReporterContext& BRC, |
| 2311 | const ExplodedNode<GRState>* EndN) { |
| 2312 | // Tell the BugReporterContext to report cases when the tracked symbol is |
Ted Kremenek | 47a7242 | 2009-04-29 18:50:19 +0000 | [diff] [blame] | 2313 | // assigned to different variables, etc. |
Ted Kremenek | c3bc6c8 | 2009-05-06 21:39:49 +0000 | [diff] [blame] | 2314 | BRC.addNotableSymbol(Sym); |
| 2315 | return RangedBugReport::getEndPath(BRC, EndN); |
Ted Kremenek | 47a7242 | 2009-04-29 18:50:19 +0000 | [diff] [blame] | 2316 | } |
| 2317 | |
| 2318 | PathDiagnosticPiece* |
Ted Kremenek | c3bc6c8 | 2009-05-06 21:39:49 +0000 | [diff] [blame] | 2319 | CFRefLeakReport::getEndPath(BugReporterContext& BRC, |
| 2320 | const ExplodedNode<GRState>* EndN){ |
Ted Kremenek | 47a7242 | 2009-04-29 18:50:19 +0000 | [diff] [blame] | 2321 | |
Ted Kremenek | c3bc6c8 | 2009-05-06 21:39:49 +0000 | [diff] [blame] | 2322 | // Tell the BugReporterContext to report cases when the tracked symbol is |
Ted Kremenek | 47a7242 | 2009-04-29 18:50:19 +0000 | [diff] [blame] | 2323 | // assigned to different variables, etc. |
Ted Kremenek | c3bc6c8 | 2009-05-06 21:39:49 +0000 | [diff] [blame] | 2324 | BRC.addNotableSymbol(Sym); |
Ted Kremenek | 47a7242 | 2009-04-29 18:50:19 +0000 | [diff] [blame] | 2325 | |
| 2326 | // We are reporting a leak. Walk up the graph to get to the first node where |
| 2327 | // the symbol appeared, and also get the first VarDecl that tracked object |
| 2328 | // is stored to. |
| 2329 | const ExplodedNode<GRState>* AllocNode = 0; |
| 2330 | const MemRegion* FirstBinding = 0; |
| 2331 | |
| 2332 | llvm::tie(AllocNode, FirstBinding) = |
Ted Kremenek | c3bc6c8 | 2009-05-06 21:39:49 +0000 | [diff] [blame] | 2333 | GetAllocationSite(BRC.getStateManager(), EndN, Sym); |
Ted Kremenek | 47a7242 | 2009-04-29 18:50:19 +0000 | [diff] [blame] | 2334 | |
| 2335 | // Get the allocate site. |
| 2336 | assert(AllocNode); |
| 2337 | Stmt* FirstStmt = cast<PostStmt>(AllocNode->getLocation()).getStmt(); |
| 2338 | |
Ted Kremenek | c3bc6c8 | 2009-05-06 21:39:49 +0000 | [diff] [blame] | 2339 | SourceManager& SMgr = BRC.getSourceManager(); |
Ted Kremenek | 47a7242 | 2009-04-29 18:50:19 +0000 | [diff] [blame] | 2340 | unsigned AllocLine =SMgr.getInstantiationLineNumber(FirstStmt->getLocStart()); |
| 2341 | |
| 2342 | // Compute an actual location for the leak. Sometimes a leak doesn't |
| 2343 | // occur at an actual statement (e.g., transition between blocks; end |
| 2344 | // of function) so we need to walk the graph and compute a real location. |
| 2345 | const ExplodedNode<GRState>* LeakN = EndN; |
| 2346 | PathDiagnosticLocation L; |
| 2347 | |
| 2348 | while (LeakN) { |
| 2349 | ProgramPoint P = LeakN->getLocation(); |
| 2350 | |
| 2351 | if (const PostStmt *PS = dyn_cast<PostStmt>(&P)) { |
| 2352 | L = PathDiagnosticLocation(PS->getStmt()->getLocStart(), SMgr); |
| 2353 | break; |
| 2354 | } |
| 2355 | else if (const BlockEdge *BE = dyn_cast<BlockEdge>(&P)) { |
| 2356 | if (const Stmt* Term = BE->getSrc()->getTerminator()) { |
| 2357 | L = PathDiagnosticLocation(Term->getLocStart(), SMgr); |
| 2358 | break; |
| 2359 | } |
| 2360 | } |
| 2361 | |
| 2362 | LeakN = LeakN->succ_empty() ? 0 : *(LeakN->succ_begin()); |
| 2363 | } |
| 2364 | |
| 2365 | if (!L.isValid()) { |
Ted Kremenek | c3bc6c8 | 2009-05-06 21:39:49 +0000 | [diff] [blame] | 2366 | const Decl &D = BRC.getCodeDecl(); |
| 2367 | L = PathDiagnosticLocation(D.getBodyRBrace(BRC.getASTContext()), SMgr); |
Ted Kremenek | 47a7242 | 2009-04-29 18:50:19 +0000 | [diff] [blame] | 2368 | } |
| 2369 | |
| 2370 | std::string sbuf; |
| 2371 | llvm::raw_string_ostream os(sbuf); |
| 2372 | |
| 2373 | os << "Object allocated on line " << AllocLine; |
| 2374 | |
| 2375 | if (FirstBinding) |
| 2376 | os << " and stored into '" << FirstBinding->getString() << '\''; |
| 2377 | |
| 2378 | // Get the retain count. |
| 2379 | const RefVal* RV = EndN->getState()->get<RefBindings>(Sym); |
| 2380 | |
| 2381 | if (RV->getKind() == RefVal::ErrorLeakReturned) { |
| 2382 | // FIXME: Per comments in rdar://6320065, "create" only applies to CF |
| 2383 | // ojbects. Only "copy", "alloc", "retain" and "new" transfer ownership |
| 2384 | // to the caller for NS objects. |
Ted Kremenek | c3bc6c8 | 2009-05-06 21:39:49 +0000 | [diff] [blame] | 2385 | ObjCMethodDecl& MD = cast<ObjCMethodDecl>(BRC.getCodeDecl()); |
Ted Kremenek | 47a7242 | 2009-04-29 18:50:19 +0000 | [diff] [blame] | 2386 | os << " is returned from a method whose name ('" |
Ted Kremenek | 314b195 | 2009-04-29 23:03:22 +0000 | [diff] [blame] | 2387 | << MD.getSelector().getAsString() |
Ted Kremenek | 47a7242 | 2009-04-29 18:50:19 +0000 | [diff] [blame] | 2388 | << "') does not contain 'copy' or otherwise starts with" |
| 2389 | " 'new' or 'alloc'. This violates the naming convention rules given" |
Ted Kremenek | 2a410c9 | 2009-04-29 22:25:52 +0000 | [diff] [blame] | 2390 | " in the Memory Management Guide for Cocoa (object leaked)"; |
Ted Kremenek | 47a7242 | 2009-04-29 18:50:19 +0000 | [diff] [blame] | 2391 | } |
| 2392 | else |
| 2393 | os << " is no longer referenced after this point and has a retain count of" |
Ted Kremenek | 2a410c9 | 2009-04-29 22:25:52 +0000 | [diff] [blame] | 2394 | " +" << RV->getCount() << " (object leaked)"; |
Ted Kremenek | 47a7242 | 2009-04-29 18:50:19 +0000 | [diff] [blame] | 2395 | |
| 2396 | return new PathDiagnosticEventPiece(L, os.str()); |
| 2397 | } |
| 2398 | |
| 2399 | |
| 2400 | CFRefLeakReport::CFRefLeakReport(CFRefBug& D, const CFRefCount &tf, |
| 2401 | ExplodedNode<GRState> *n, |
| 2402 | SymbolRef sym, GRExprEngine& Eng) |
| 2403 | : CFRefReport(D, tf, n, sym) |
| 2404 | { |
| 2405 | |
| 2406 | // Most bug reports are cached at the location where they occured. |
| 2407 | // With leaks, we want to unique them by the location where they were |
| 2408 | // allocated, and only report a single path. To do this, we need to find |
| 2409 | // the allocation site of a piece of tracked memory, which we do via a |
| 2410 | // call to GetAllocationSite. This will walk the ExplodedGraph backwards. |
| 2411 | // Note that this is *not* the trimmed graph; we are guaranteed, however, |
| 2412 | // that all ancestor nodes that represent the allocation site have the |
| 2413 | // same SourceLocation. |
| 2414 | const ExplodedNode<GRState>* AllocNode = 0; |
| 2415 | |
| 2416 | llvm::tie(AllocNode, AllocBinding) = // Set AllocBinding. |
| 2417 | GetAllocationSite(Eng.getStateManager(), getEndNode(), getSymbol()); |
| 2418 | |
| 2419 | // Get the SourceLocation for the allocation site. |
| 2420 | ProgramPoint P = AllocNode->getLocation(); |
| 2421 | AllocSite = cast<PostStmt>(P).getStmt()->getLocStart(); |
| 2422 | |
| 2423 | // Fill in the description of the bug. |
| 2424 | Description.clear(); |
| 2425 | llvm::raw_string_ostream os(Description); |
| 2426 | SourceManager& SMgr = Eng.getContext().getSourceManager(); |
| 2427 | unsigned AllocLine = SMgr.getInstantiationLineNumber(AllocSite); |
Ted Kremenek | 2e9d030 | 2009-05-02 19:05:19 +0000 | [diff] [blame] | 2428 | os << "Potential leak "; |
| 2429 | if (tf.isGCEnabled()) { |
| 2430 | os << "(when using garbage collection) "; |
| 2431 | } |
| 2432 | os << "of an object allocated on line " << AllocLine; |
Ted Kremenek | 47a7242 | 2009-04-29 18:50:19 +0000 | [diff] [blame] | 2433 | |
| 2434 | // FIXME: AllocBinding doesn't get populated for RegionStore yet. |
| 2435 | if (AllocBinding) |
| 2436 | os << " and stored into '" << AllocBinding->getString() << '\''; |
| 2437 | } |
| 2438 | |
| 2439 | //===----------------------------------------------------------------------===// |
| 2440 | // Main checker logic. |
| 2441 | //===----------------------------------------------------------------------===// |
| 2442 | |
Ted Kremenek | 272aa85 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 2443 | /// GetReturnType - Used to get the return type of a message expression or |
| 2444 | /// function call with the intention of affixing that type to a tracked symbol. |
| 2445 | /// While the the return type can be queried directly from RetEx, when |
| 2446 | /// invoking class methods we augment to the return type to be that of |
| 2447 | /// a pointer to the class (as opposed it just being id). |
| 2448 | static QualType GetReturnType(Expr* RetE, ASTContext& Ctx) { |
| 2449 | |
| 2450 | QualType RetTy = RetE->getType(); |
| 2451 | |
| 2452 | // FIXME: We aren't handling id<...>. |
Chris Lattner | b724ab2 | 2008-07-26 22:36:27 +0000 | [diff] [blame] | 2453 | const PointerType* PT = RetTy->getAsPointerType(); |
Ted Kremenek | 272aa85 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 2454 | if (!PT) |
| 2455 | return RetTy; |
| 2456 | |
| 2457 | // If RetEx is not a message expression just return its type. |
| 2458 | // If RetEx is a message expression, return its types if it is something |
| 2459 | /// more specific than id. |
| 2460 | |
| 2461 | ObjCMessageExpr* ME = dyn_cast<ObjCMessageExpr>(RetE); |
| 2462 | |
Steve Naroff | 17c0382 | 2009-02-12 17:52:19 +0000 | [diff] [blame] | 2463 | if (!ME || !Ctx.isObjCIdStructType(PT->getPointeeType())) |
Ted Kremenek | 272aa85 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 2464 | return RetTy; |
| 2465 | |
| 2466 | ObjCInterfaceDecl* D = ME->getClassInfo().first; |
| 2467 | |
| 2468 | // At this point we know the return type of the message expression is id. |
| 2469 | // If we have an ObjCInterceDecl, we know this is a call to a class method |
| 2470 | // whose type we can resolve. In such cases, promote the return type to |
| 2471 | // Class*. |
| 2472 | return !D ? RetTy : Ctx.getPointerType(Ctx.getObjCInterfaceType(D)); |
| 2473 | } |
| 2474 | |
| 2475 | |
Ted Kremenek | abd89ac | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 2476 | void CFRefCount::EvalSummary(ExplodedNodeSet<GRState>& Dst, |
Ted Kremenek | a8c3c43 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 2477 | GRExprEngine& Eng, |
Ted Kremenek | abd89ac | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 2478 | GRStmtNodeBuilder<GRState>& Builder, |
Ted Kremenek | a8c3c43 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 2479 | Expr* Ex, |
| 2480 | Expr* Receiver, |
Ted Kremenek | 286e985 | 2009-05-04 04:57:00 +0000 | [diff] [blame] | 2481 | const RetainSummary& Summ, |
Zhongxing Xu | cac107a | 2009-04-20 05:24:46 +0000 | [diff] [blame] | 2482 | ExprIterator arg_beg, ExprIterator arg_end, |
Ted Kremenek | abd89ac | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 2483 | ExplodedNode<GRState>* Pred) { |
Ted Kremenek | ce3ed1e | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 2484 | |
Ted Kremenek | a7338b4 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 2485 | // Get the state. |
Ted Kremenek | 4ae925c | 2008-08-14 21:16:54 +0000 | [diff] [blame] | 2486 | GRStateRef state(Builder.GetState(Pred), Eng.getStateManager()); |
Ted Kremenek | 0106e20 | 2008-10-24 20:32:50 +0000 | [diff] [blame] | 2487 | ASTContext& Ctx = Eng.getStateManager().getContext(); |
Ted Kremenek | 227c537 | 2008-05-06 02:41:27 +0000 | [diff] [blame] | 2488 | |
| 2489 | // Evaluate the effect of the arguments. |
Ted Kremenek | 1feab29 | 2008-04-16 04:28:53 +0000 | [diff] [blame] | 2490 | RefVal::Kind hasErr = (RefVal::Kind) 0; |
Ted Kremenek | ce3ed1e | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 2491 | unsigned idx = 0; |
Ted Kremenek | 99b0ecb | 2008-04-11 18:40:51 +0000 | [diff] [blame] | 2492 | Expr* ErrorExpr = NULL; |
Ted Kremenek | b9cd9a7 | 2008-12-05 02:27:51 +0000 | [diff] [blame] | 2493 | SymbolRef ErrorSym = 0; |
Ted Kremenek | 99b0ecb | 2008-04-11 18:40:51 +0000 | [diff] [blame] | 2494 | |
Ted Kremenek | 4ae925c | 2008-08-14 21:16:54 +0000 | [diff] [blame] | 2495 | for (ExprIterator I = arg_beg; I != arg_end; ++I, ++idx) { |
Ted Kremenek | b6ac0e5 | 2009-03-04 00:13:50 +0000 | [diff] [blame] | 2496 | SVal V = state.GetSValAsScalarOrLoc(*I); |
Ted Kremenek | 9577c1e | 2009-03-03 22:06:47 +0000 | [diff] [blame] | 2497 | SymbolRef Sym = V.getAsLocSymbol(); |
Ted Kremenek | b6ac0e5 | 2009-03-04 00:13:50 +0000 | [diff] [blame] | 2498 | |
Ted Kremenek | 74556a1 | 2009-03-26 03:35:11 +0000 | [diff] [blame] | 2499 | if (Sym) |
Ted Kremenek | b657894 | 2009-02-24 19:15:11 +0000 | [diff] [blame] | 2500 | if (RefBindings::data_type* T = state.get<RefBindings>(Sym)) { |
Ted Kremenek | 286e985 | 2009-05-04 04:57:00 +0000 | [diff] [blame] | 2501 | state = Update(state, Sym, *T, Summ.getArg(idx), hasErr); |
Ted Kremenek | b657894 | 2009-02-24 19:15:11 +0000 | [diff] [blame] | 2502 | if (hasErr) { |
Ted Kremenek | 99b0ecb | 2008-04-11 18:40:51 +0000 | [diff] [blame] | 2503 | ErrorExpr = *I; |
Ted Kremenek | 6064a36 | 2008-07-07 16:21:19 +0000 | [diff] [blame] | 2504 | ErrorSym = Sym; |
Ted Kremenek | 99b0ecb | 2008-04-11 18:40:51 +0000 | [diff] [blame] | 2505 | break; |
Ted Kremenek | 9577c1e | 2009-03-03 22:06:47 +0000 | [diff] [blame] | 2506 | } |
| 2507 | continue; |
Ted Kremenek | b657894 | 2009-02-24 19:15:11 +0000 | [diff] [blame] | 2508 | } |
Ted Kremenek | ede40b7 | 2008-07-09 18:11:16 +0000 | [diff] [blame] | 2509 | |
Ted Kremenek | 9577c1e | 2009-03-03 22:06:47 +0000 | [diff] [blame] | 2510 | if (isa<Loc>(V)) { |
| 2511 | if (loc::MemRegionVal* MR = dyn_cast<loc::MemRegionVal>(&V)) { |
Ted Kremenek | 286e985 | 2009-05-04 04:57:00 +0000 | [diff] [blame] | 2512 | if (Summ.getArg(idx) == DoNothingByRef) |
Ted Kremenek | ede40b7 | 2008-07-09 18:11:16 +0000 | [diff] [blame] | 2513 | continue; |
| 2514 | |
| 2515 | // Invalidate the value of the variable passed by reference. |
Ted Kremenek | 852e3ca | 2008-07-03 23:26:32 +0000 | [diff] [blame] | 2516 | |
| 2517 | // FIXME: Either this logic should also be replicated in GRSimpleVals |
| 2518 | // or should be pulled into a separate "constraint engine." |
Ted Kremenek | ede40b7 | 2008-07-09 18:11:16 +0000 | [diff] [blame] | 2519 | |
Ted Kremenek | 852e3ca | 2008-07-03 23:26:32 +0000 | [diff] [blame] | 2520 | // FIXME: We can have collisions on the conjured symbol if the |
| 2521 | // expression *I also creates conjured symbols. We probably want |
| 2522 | // to identify conjured symbols by an expression pair: the enclosing |
| 2523 | // expression (the context) and the expression itself. This should |
Ted Kremenek | ede40b7 | 2008-07-09 18:11:16 +0000 | [diff] [blame] | 2524 | // disambiguate conjured symbols. |
Ted Kremenek | b15eba4 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 2525 | |
Ted Kremenek | 38a4b4b | 2008-10-17 20:28:54 +0000 | [diff] [blame] | 2526 | const TypedRegion* R = dyn_cast<TypedRegion>(MR->getRegion()); |
Zhongxing Xu | b9d47a4 | 2009-04-29 02:30:09 +0000 | [diff] [blame] | 2527 | |
Ted Kremenek | 73ec773 | 2009-05-06 18:19:24 +0000 | [diff] [blame] | 2528 | if (R) { |
| 2529 | // Are we dealing with an ElementRegion? If the element type is |
| 2530 | // a basic integer type (e.g., char, int) and the underying region |
| 2531 | // is also typed then strip off the ElementRegion. |
| 2532 | // FIXME: We really need to think about this for the general case |
| 2533 | // as sometimes we are reasoning about arrays and other times |
| 2534 | // about (char*), etc., is just a form of passing raw bytes. |
| 2535 | // e.g., void *p = alloca(); foo((char*)p); |
| 2536 | if (const ElementRegion *ER = dyn_cast<ElementRegion>(R)) { |
| 2537 | // Checking for 'integral type' is probably too promiscuous, but |
| 2538 | // we'll leave it in for now until we have a systematic way of |
| 2539 | // handling all of these cases. Eventually we need to come up |
| 2540 | // with an interface to StoreManager so that this logic can be |
| 2541 | // approriately delegated to the respective StoreManagers while |
| 2542 | // still allowing us to do checker-specific logic (e.g., |
| 2543 | // invalidating reference counts), probably via callbacks. |
| 2544 | if (ER->getElementType()->isIntegralType()) |
| 2545 | if (const TypedRegion *superReg = |
| 2546 | dyn_cast<TypedRegion>(ER->getSuperRegion())) |
| 2547 | R = superReg; |
| 2548 | // FIXME: What about layers of ElementRegions? |
| 2549 | } |
| 2550 | |
Ted Kremenek | 618c6cd | 2008-12-18 23:34:57 +0000 | [diff] [blame] | 2551 | // Is the invalidated variable something that we were tracking? |
Ted Kremenek | b6ac0e5 | 2009-03-04 00:13:50 +0000 | [diff] [blame] | 2552 | SymbolRef Sym = state.GetSValAsScalarOrLoc(R).getAsLocSymbol(); |
Ted Kremenek | 618c6cd | 2008-12-18 23:34:57 +0000 | [diff] [blame] | 2553 | |
Ted Kremenek | 53b2418 | 2009-03-04 22:56:43 +0000 | [diff] [blame] | 2554 | // Remove any existing reference-count binding. |
Ted Kremenek | 74556a1 | 2009-03-26 03:35:11 +0000 | [diff] [blame] | 2555 | if (Sym) state = state.remove<RefBindings>(Sym); |
Ted Kremenek | b15eba4 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 2556 | |
Ted Kremenek | 53b2418 | 2009-03-04 22:56:43 +0000 | [diff] [blame] | 2557 | if (R->isBoundable(Ctx)) { |
| 2558 | // Set the value of the variable to be a conjured symbol. |
| 2559 | unsigned Count = Builder.getCurrentBlockCount(); |
| 2560 | QualType T = R->getRValueType(Ctx); |
| 2561 | |
Zhongxing Xu | 079dc35 | 2009-04-09 06:03:54 +0000 | [diff] [blame] | 2562 | if (Loc::IsLocType(T) || (T->isIntegerType() && T->isScalarType())){ |
Ted Kremenek | e4cb3c8 | 2009-04-09 22:22:44 +0000 | [diff] [blame] | 2563 | ValueManager &ValMgr = Eng.getValueManager(); |
| 2564 | SVal V = ValMgr.getConjuredSymbolVal(*I, T, Count); |
Zhongxing Xu | 079dc35 | 2009-04-09 06:03:54 +0000 | [diff] [blame] | 2565 | state = state.BindLoc(Loc::MakeVal(R), V); |
Ted Kremenek | 53b2418 | 2009-03-04 22:56:43 +0000 | [diff] [blame] | 2566 | } |
| 2567 | else if (const RecordType *RT = T->getAsStructureType()) { |
| 2568 | // Handle structs in a not so awesome way. Here we just |
| 2569 | // eagerly bind new symbols to the fields. In reality we |
| 2570 | // should have the store manager handle this. The idea is just |
| 2571 | // to prototype some basic functionality here. All of this logic |
| 2572 | // should one day soon just go away. |
| 2573 | const RecordDecl *RD = RT->getDecl()->getDefinition(Ctx); |
| 2574 | |
| 2575 | // No record definition. There is nothing we can do. |
| 2576 | if (!RD) |
| 2577 | continue; |
| 2578 | |
| 2579 | MemRegionManager &MRMgr = state.getManager().getRegionManager(); |
| 2580 | |
| 2581 | // Iterate through the fields and construct new symbols. |
Douglas Gregor | c55b0b0 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 2582 | for (RecordDecl::field_iterator FI=RD->field_begin(Ctx), |
| 2583 | FE=RD->field_end(Ctx); FI!=FE; ++FI) { |
Ted Kremenek | 53b2418 | 2009-03-04 22:56:43 +0000 | [diff] [blame] | 2584 | |
| 2585 | // For now just handle scalar fields. |
| 2586 | FieldDecl *FD = *FI; |
| 2587 | QualType FT = FD->getType(); |
| 2588 | |
| 2589 | if (Loc::IsLocType(FT) || |
Ted Kremenek | e4cb3c8 | 2009-04-09 22:22:44 +0000 | [diff] [blame] | 2590 | (FT->isIntegerType() && FT->isScalarType())) { |
Ted Kremenek | 53b2418 | 2009-03-04 22:56:43 +0000 | [diff] [blame] | 2591 | const FieldRegion* FR = MRMgr.getFieldRegion(FD, R); |
Ted Kremenek | e4cb3c8 | 2009-04-09 22:22:44 +0000 | [diff] [blame] | 2592 | ValueManager &ValMgr = Eng.getValueManager(); |
| 2593 | SVal V = ValMgr.getConjuredSymbolVal(*I, FT, Count); |
Zhongxing Xu | c458e32 | 2009-04-09 06:32:20 +0000 | [diff] [blame] | 2594 | state = state.BindLoc(Loc::MakeVal(FR), V); |
Ted Kremenek | 53b2418 | 2009-03-04 22:56:43 +0000 | [diff] [blame] | 2595 | } |
| 2596 | } |
| 2597 | } |
| 2598 | else { |
| 2599 | // Just blast away other values. |
| 2600 | state = state.BindLoc(*MR, UnknownVal()); |
| 2601 | } |
Ted Kremenek | 8f90e71 | 2008-10-17 22:23:12 +0000 | [diff] [blame] | 2602 | } |
Ted Kremenek | b15eba4 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 2603 | } |
| 2604 | else |
Ted Kremenek | 09102db | 2008-11-12 19:22:09 +0000 | [diff] [blame] | 2605 | state = state.BindLoc(*MR, UnknownVal()); |
Ted Kremenek | 852e3ca | 2008-07-03 23:26:32 +0000 | [diff] [blame] | 2606 | } |
| 2607 | else { |
| 2608 | // Nuke all other arguments passed by reference. |
Zhongxing Xu | 097fc98 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 2609 | state = state.Unbind(cast<Loc>(V)); |
Ted Kremenek | 852e3ca | 2008-07-03 23:26:32 +0000 | [diff] [blame] | 2610 | } |
Ted Kremenek | e492420 | 2008-04-11 20:51:02 +0000 | [diff] [blame] | 2611 | } |
Zhongxing Xu | 097fc98 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 2612 | else if (isa<nonloc::LocAsInteger>(V)) |
| 2613 | state = state.Unbind(cast<nonloc::LocAsInteger>(V).getLoc()); |
Ted Kremenek | a8c3c43 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 2614 | } |
Ted Kremenek | 1feab29 | 2008-04-16 04:28:53 +0000 | [diff] [blame] | 2615 | |
Ted Kremenek | 272aa85 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 2616 | // Evaluate the effect on the message receiver. |
Ted Kremenek | 227c537 | 2008-05-06 02:41:27 +0000 | [diff] [blame] | 2617 | if (!ErrorExpr && Receiver) { |
Ted Kremenek | b6ac0e5 | 2009-03-04 00:13:50 +0000 | [diff] [blame] | 2618 | SymbolRef Sym = state.GetSValAsScalarOrLoc(Receiver).getAsLocSymbol(); |
Ted Kremenek | 74556a1 | 2009-03-26 03:35:11 +0000 | [diff] [blame] | 2619 | if (Sym) { |
Ted Kremenek | b657894 | 2009-02-24 19:15:11 +0000 | [diff] [blame] | 2620 | if (const RefVal* T = state.get<RefBindings>(Sym)) { |
Ted Kremenek | 286e985 | 2009-05-04 04:57:00 +0000 | [diff] [blame] | 2621 | state = Update(state, Sym, *T, Summ.getReceiverEffect(), hasErr); |
Ted Kremenek | b657894 | 2009-02-24 19:15:11 +0000 | [diff] [blame] | 2622 | if (hasErr) { |
Ted Kremenek | 227c537 | 2008-05-06 02:41:27 +0000 | [diff] [blame] | 2623 | ErrorExpr = Receiver; |
Ted Kremenek | 6064a36 | 2008-07-07 16:21:19 +0000 | [diff] [blame] | 2624 | ErrorSym = Sym; |
Ted Kremenek | 227c537 | 2008-05-06 02:41:27 +0000 | [diff] [blame] | 2625 | } |
Ted Kremenek | b657894 | 2009-02-24 19:15:11 +0000 | [diff] [blame] | 2626 | } |
Ted Kremenek | 227c537 | 2008-05-06 02:41:27 +0000 | [diff] [blame] | 2627 | } |
| 2628 | } |
Ted Kremenek | a8c3c43 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 2629 | |
Ted Kremenek | 272aa85 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 2630 | // Process any errors. |
Ted Kremenek | 1feab29 | 2008-04-16 04:28:53 +0000 | [diff] [blame] | 2631 | if (hasErr) { |
Ted Kremenek | 4ae925c | 2008-08-14 21:16:54 +0000 | [diff] [blame] | 2632 | ProcessNonLeakError(Dst, Builder, Ex, ErrorExpr, Pred, state, |
Ted Kremenek | 2be7ddb | 2008-04-18 03:39:05 +0000 | [diff] [blame] | 2633 | hasErr, ErrorSym); |
Ted Kremenek | ce3ed1e | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 2634 | return; |
Ted Kremenek | 0d72157 | 2008-03-11 17:48:22 +0000 | [diff] [blame] | 2635 | } |
Ted Kremenek | a8c3c43 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 2636 | |
Ted Kremenek | f2717b0 | 2008-07-18 17:24:20 +0000 | [diff] [blame] | 2637 | // Consult the summary for the return value. |
Ted Kremenek | 286e985 | 2009-05-04 04:57:00 +0000 | [diff] [blame] | 2638 | RetEffect RE = Summ.getRetEffect(); |
Ted Kremenek | ce3ed1e | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 2639 | |
| 2640 | switch (RE.getKind()) { |
| 2641 | default: |
| 2642 | assert (false && "Unhandled RetEffect."); break; |
Ted Kremenek | a8c3c43 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 2643 | |
Ted Kremenek | 8f90e71 | 2008-10-17 22:23:12 +0000 | [diff] [blame] | 2644 | case RetEffect::NoRet: { |
Ted Kremenek | a8c3c43 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 2645 | |
Ted Kremenek | 455dd86 | 2008-04-11 20:23:24 +0000 | [diff] [blame] | 2646 | // Make up a symbol for the return value (not reference counted). |
Ted Kremenek | e492420 | 2008-04-11 20:51:02 +0000 | [diff] [blame] | 2647 | // FIXME: This is basically copy-and-paste from GRSimpleVals. We |
| 2648 | // should compose behavior, not copy it. |
Ted Kremenek | 455dd86 | 2008-04-11 20:23:24 +0000 | [diff] [blame] | 2649 | |
Ted Kremenek | 8f90e71 | 2008-10-17 22:23:12 +0000 | [diff] [blame] | 2650 | // FIXME: We eventually should handle structs and other compound types |
| 2651 | // that are returned by value. |
| 2652 | |
| 2653 | QualType T = Ex->getType(); |
| 2654 | |
Ted Kremenek | 79413a5 | 2008-11-13 06:10:40 +0000 | [diff] [blame] | 2655 | if (Loc::IsLocType(T) || (T->isIntegerType() && T->isScalarType())) { |
Ted Kremenek | 455dd86 | 2008-04-11 20:23:24 +0000 | [diff] [blame] | 2656 | unsigned Count = Builder.getCurrentBlockCount(); |
Ted Kremenek | e4cb3c8 | 2009-04-09 22:22:44 +0000 | [diff] [blame] | 2657 | ValueManager &ValMgr = Eng.getValueManager(); |
| 2658 | SVal X = ValMgr.getConjuredSymbolVal(Ex, T, Count); |
Ted Kremenek | 09102db | 2008-11-12 19:22:09 +0000 | [diff] [blame] | 2659 | state = state.BindExpr(Ex, X, false); |
Ted Kremenek | 455dd86 | 2008-04-11 20:23:24 +0000 | [diff] [blame] | 2660 | } |
| 2661 | |
Ted Kremenek | ab2fa2a | 2008-04-10 23:44:06 +0000 | [diff] [blame] | 2662 | break; |
Ted Kremenek | 8f90e71 | 2008-10-17 22:23:12 +0000 | [diff] [blame] | 2663 | } |
Ted Kremenek | ab2fa2a | 2008-04-10 23:44:06 +0000 | [diff] [blame] | 2664 | |
Ted Kremenek | ce3ed1e | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 2665 | case RetEffect::Alias: { |
Ted Kremenek | 272aa85 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 2666 | unsigned idx = RE.getIndex(); |
Ted Kremenek | 2719e98 | 2008-06-17 02:43:46 +0000 | [diff] [blame] | 2667 | assert (arg_end >= arg_beg); |
Ted Kremenek | a8c3c43 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 2668 | assert (idx < (unsigned) (arg_end - arg_beg)); |
Ted Kremenek | b6ac0e5 | 2009-03-04 00:13:50 +0000 | [diff] [blame] | 2669 | SVal V = state.GetSValAsScalarOrLoc(*(arg_beg+idx)); |
Ted Kremenek | 09102db | 2008-11-12 19:22:09 +0000 | [diff] [blame] | 2670 | state = state.BindExpr(Ex, V, false); |
Ted Kremenek | ce3ed1e | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 2671 | break; |
| 2672 | } |
| 2673 | |
Ted Kremenek | 227c537 | 2008-05-06 02:41:27 +0000 | [diff] [blame] | 2674 | case RetEffect::ReceiverAlias: { |
| 2675 | assert (Receiver); |
Ted Kremenek | b6ac0e5 | 2009-03-04 00:13:50 +0000 | [diff] [blame] | 2676 | SVal V = state.GetSValAsScalarOrLoc(Receiver); |
Ted Kremenek | 09102db | 2008-11-12 19:22:09 +0000 | [diff] [blame] | 2677 | state = state.BindExpr(Ex, V, false); |
Ted Kremenek | 227c537 | 2008-05-06 02:41:27 +0000 | [diff] [blame] | 2678 | break; |
| 2679 | } |
| 2680 | |
Ted Kremenek | 6a1cc25 | 2008-06-23 18:02:52 +0000 | [diff] [blame] | 2681 | case RetEffect::OwnedAllocatedSymbol: |
Ted Kremenek | ce3ed1e | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 2682 | case RetEffect::OwnedSymbol: { |
| 2683 | unsigned Count = Builder.getCurrentBlockCount(); |
Ted Kremenek | e9e726e | 2009-04-09 16:13:17 +0000 | [diff] [blame] | 2684 | ValueManager &ValMgr = Eng.getValueManager(); |
| 2685 | SymbolRef Sym = ValMgr.getConjuredSymbol(Ex, Count); |
| 2686 | QualType RetT = GetReturnType(Ex, ValMgr.getContext()); |
| 2687 | state = state.set<RefBindings>(Sym, RefVal::makeOwned(RE.getObjKind(), |
| 2688 | RetT)); |
| 2689 | state = state.BindExpr(Ex, ValMgr.makeRegionVal(Sym), false); |
Ted Kremenek | 45c52a1 | 2009-03-09 22:46:49 +0000 | [diff] [blame] | 2690 | |
| 2691 | // FIXME: Add a flag to the checker where allocations are assumed to |
| 2692 | // *not fail. |
| 2693 | #if 0 |
Ted Kremenek | e62fd05 | 2009-01-28 22:27:59 +0000 | [diff] [blame] | 2694 | if (RE.getKind() == RetEffect::OwnedAllocatedSymbol) { |
| 2695 | bool isFeasible; |
| 2696 | state = state.Assume(loc::SymbolVal(Sym), true, isFeasible); |
| 2697 | assert(isFeasible && "Cannot assume fresh symbol is non-null."); |
| 2698 | } |
Ted Kremenek | 45c52a1 | 2009-03-09 22:46:49 +0000 | [diff] [blame] | 2699 | #endif |
Ted Kremenek | 6a1cc25 | 2008-06-23 18:02:52 +0000 | [diff] [blame] | 2700 | |
Ted Kremenek | ce3ed1e | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 2701 | break; |
| 2702 | } |
Ted Kremenek | 382fb4e | 2009-04-27 19:14:45 +0000 | [diff] [blame] | 2703 | |
| 2704 | case RetEffect::GCNotOwnedSymbol: |
Ted Kremenek | ce3ed1e | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 2705 | case RetEffect::NotOwnedSymbol: { |
| 2706 | unsigned Count = Builder.getCurrentBlockCount(); |
Ted Kremenek | e9e726e | 2009-04-09 16:13:17 +0000 | [diff] [blame] | 2707 | ValueManager &ValMgr = Eng.getValueManager(); |
| 2708 | SymbolRef Sym = ValMgr.getConjuredSymbol(Ex, Count); |
| 2709 | QualType RetT = GetReturnType(Ex, ValMgr.getContext()); |
| 2710 | state = state.set<RefBindings>(Sym, RefVal::makeNotOwned(RE.getObjKind(), |
| 2711 | RetT)); |
| 2712 | state = state.BindExpr(Ex, ValMgr.makeRegionVal(Sym), false); |
Ted Kremenek | ce3ed1e | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 2713 | break; |
| 2714 | } |
| 2715 | } |
Ted Kremenek | a8c3c43 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 2716 | |
Ted Kremenek | 0dd6501 | 2009-02-18 02:00:25 +0000 | [diff] [blame] | 2717 | // Generate a sink node if we are at the end of a path. |
| 2718 | GRExprEngine::NodeTy *NewNode = |
Ted Kremenek | 286e985 | 2009-05-04 04:57:00 +0000 | [diff] [blame] | 2719 | Summ.isEndPath() ? Builder.MakeSinkNode(Dst, Ex, Pred, state) |
| 2720 | : Builder.MakeNode(Dst, Ex, Pred, state); |
Ted Kremenek | 0dd6501 | 2009-02-18 02:00:25 +0000 | [diff] [blame] | 2721 | |
| 2722 | // Annotate the edge with summary we used. |
Ted Kremenek | 286e985 | 2009-05-04 04:57:00 +0000 | [diff] [blame] | 2723 | if (NewNode) SummaryLog[NewNode] = &Summ; |
Ted Kremenek | a8c3c43 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 2724 | } |
| 2725 | |
| 2726 | |
Ted Kremenek | abd89ac | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 2727 | void CFRefCount::EvalCall(ExplodedNodeSet<GRState>& Dst, |
Ted Kremenek | a8c3c43 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 2728 | GRExprEngine& Eng, |
Ted Kremenek | abd89ac | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 2729 | GRStmtNodeBuilder<GRState>& Builder, |
Zhongxing Xu | 097fc98 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 2730 | CallExpr* CE, SVal L, |
Ted Kremenek | abd89ac | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 2731 | ExplodedNode<GRState>* Pred) { |
Zhongxing Xu | cac107a | 2009-04-20 05:24:46 +0000 | [diff] [blame] | 2732 | const FunctionDecl* FD = L.getAsFunctionDecl(); |
Ted Kremenek | 286e985 | 2009-05-04 04:57:00 +0000 | [diff] [blame] | 2733 | RetainSummary* Summ = !FD ? Summaries.getDefaultSummary() |
Zhongxing Xu | cac107a | 2009-04-20 05:24:46 +0000 | [diff] [blame] | 2734 | : Summaries.getSummary(const_cast<FunctionDecl*>(FD)); |
Ted Kremenek | a8c3c43 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 2735 | |
Ted Kremenek | 286e985 | 2009-05-04 04:57:00 +0000 | [diff] [blame] | 2736 | assert(Summ); |
| 2737 | EvalSummary(Dst, Eng, Builder, CE, 0, *Summ, |
Ted Kremenek | a8c3c43 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 2738 | CE->arg_begin(), CE->arg_end(), Pred); |
Ted Kremenek | 827f93b | 2008-03-06 00:08:09 +0000 | [diff] [blame] | 2739 | } |
Ted Kremenek | a7338b4 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 2740 | |
Ted Kremenek | abd89ac | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 2741 | void CFRefCount::EvalObjCMessageExpr(ExplodedNodeSet<GRState>& Dst, |
Ted Kremenek | 4b4738b | 2008-04-15 23:44:31 +0000 | [diff] [blame] | 2742 | GRExprEngine& Eng, |
Ted Kremenek | abd89ac | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 2743 | GRStmtNodeBuilder<GRState>& Builder, |
Ted Kremenek | 4b4738b | 2008-04-15 23:44:31 +0000 | [diff] [blame] | 2744 | ObjCMessageExpr* ME, |
Ted Kremenek | abd89ac | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 2745 | ExplodedNode<GRState>* Pred) { |
Ted Kremenek | 286e985 | 2009-05-04 04:57:00 +0000 | [diff] [blame] | 2746 | RetainSummary* Summ = 0; |
Ted Kremenek | 3366180 | 2008-05-01 21:31:50 +0000 | [diff] [blame] | 2747 | |
Ted Kremenek | 272aa85 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 2748 | if (Expr* Receiver = ME->getReceiver()) { |
| 2749 | // We need the type-information of the tracked receiver object |
| 2750 | // Retrieve it from the state. |
| 2751 | ObjCInterfaceDecl* ID = 0; |
| 2752 | |
| 2753 | // FIXME: Wouldn't it be great if this code could be reduced? It's just |
| 2754 | // a chain of lookups. |
Ted Kremenek | a821b79 | 2009-04-29 05:04:30 +0000 | [diff] [blame] | 2755 | // FIXME: Is this really working as expected? There are cases where |
| 2756 | // we just use the 'ID' from the message expression. |
Ted Kremenek | abd89ac | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 2757 | const GRState* St = Builder.GetState(Pred); |
Ted Kremenek | b6ac0e5 | 2009-03-04 00:13:50 +0000 | [diff] [blame] | 2758 | SVal V = Eng.getStateManager().GetSValAsScalarOrLoc(St, Receiver); |
Ted Kremenek | 272aa85 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 2759 | |
Ted Kremenek | 9577c1e | 2009-03-03 22:06:47 +0000 | [diff] [blame] | 2760 | SymbolRef Sym = V.getAsLocSymbol(); |
Ted Kremenek | 74556a1 | 2009-03-26 03:35:11 +0000 | [diff] [blame] | 2761 | if (Sym) { |
Ted Kremenek | 4ae925c | 2008-08-14 21:16:54 +0000 | [diff] [blame] | 2762 | if (const RefVal* T = St->get<RefBindings>(Sym)) { |
Ted Kremenek | 6064a36 | 2008-07-07 16:21:19 +0000 | [diff] [blame] | 2763 | QualType Ty = T->getType(); |
Ted Kremenek | 272aa85 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 2764 | |
| 2765 | if (const PointerType* PT = Ty->getAsPointerType()) { |
| 2766 | QualType PointeeTy = PT->getPointeeType(); |
| 2767 | |
| 2768 | if (ObjCInterfaceType* IT = dyn_cast<ObjCInterfaceType>(PointeeTy)) |
| 2769 | ID = IT->getDecl(); |
| 2770 | } |
| 2771 | } |
| 2772 | } |
| 2773 | |
Ted Kremenek | 04e0030 | 2009-04-29 17:09:14 +0000 | [diff] [blame] | 2774 | // FIXME: The receiver could be a reference to a class, meaning that |
| 2775 | // we should use the class method. |
| 2776 | Summ = Summaries.getInstanceMethodSummary(ME, ID); |
Ted Kremenek | 0106e20 | 2008-10-24 20:32:50 +0000 | [diff] [blame] | 2777 | |
Ted Kremenek | 63d09ae | 2008-10-23 01:56:15 +0000 | [diff] [blame] | 2778 | // Special-case: are we sending a mesage to "self"? |
| 2779 | // This is a hack. When we have full-IP this should be removed. |
Ted Kremenek | 2f22673 | 2009-05-04 05:31:22 +0000 | [diff] [blame] | 2780 | if (isa<ObjCMethodDecl>(&Eng.getGraph().getCodeDecl())) { |
| 2781 | if (Expr* Receiver = ME->getReceiver()) { |
| 2782 | SVal X = Eng.getStateManager().GetSValAsScalarOrLoc(St, Receiver); |
| 2783 | if (loc::MemRegionVal* L = dyn_cast<loc::MemRegionVal>(&X)) |
| 2784 | if (L->getRegion() == Eng.getStateManager().getSelfRegion(St)) { |
| 2785 | // Update the summary to make the default argument effect |
| 2786 | // 'StopTracking'. |
| 2787 | Summ = Summaries.copySummary(Summ); |
| 2788 | Summ->setDefaultArgEffect(StopTracking); |
| 2789 | } |
Ted Kremenek | 63d09ae | 2008-10-23 01:56:15 +0000 | [diff] [blame] | 2790 | } |
| 2791 | } |
Ted Kremenek | 272aa85 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 2792 | } |
Ted Kremenek | 1feab29 | 2008-04-16 04:28:53 +0000 | [diff] [blame] | 2793 | else |
Ted Kremenek | b17fa95 | 2009-04-23 21:25:57 +0000 | [diff] [blame] | 2794 | Summ = Summaries.getClassMethodSummary(ME); |
Ted Kremenek | 1feab29 | 2008-04-16 04:28:53 +0000 | [diff] [blame] | 2795 | |
Ted Kremenek | 286e985 | 2009-05-04 04:57:00 +0000 | [diff] [blame] | 2796 | if (!Summ) |
| 2797 | Summ = Summaries.getDefaultSummary(); |
Ted Kremenek | ccbe79a | 2009-04-24 17:50:11 +0000 | [diff] [blame] | 2798 | |
Ted Kremenek | 286e985 | 2009-05-04 04:57:00 +0000 | [diff] [blame] | 2799 | EvalSummary(Dst, Eng, Builder, ME, ME->getReceiver(), *Summ, |
Ted Kremenek | 926abf2 | 2008-05-06 04:20:12 +0000 | [diff] [blame] | 2800 | ME->arg_begin(), ME->arg_end(), Pred); |
Ted Kremenek | 4b4738b | 2008-04-15 23:44:31 +0000 | [diff] [blame] | 2801 | } |
Ted Kremenek | 2ddb4b2 | 2009-02-14 03:16:10 +0000 | [diff] [blame] | 2802 | |
| 2803 | namespace { |
| 2804 | class VISIBILITY_HIDDEN StopTrackingCallback : public SymbolVisitor { |
| 2805 | GRStateRef state; |
| 2806 | public: |
| 2807 | StopTrackingCallback(GRStateRef st) : state(st) {} |
| 2808 | GRStateRef getState() { return state; } |
| 2809 | |
| 2810 | bool VisitSymbol(SymbolRef sym) { |
| 2811 | state = state.remove<RefBindings>(sym); |
| 2812 | return true; |
| 2813 | } |
Ted Kremenek | 926abf2 | 2008-05-06 04:20:12 +0000 | [diff] [blame] | 2814 | |
Ted Kremenek | 2ddb4b2 | 2009-02-14 03:16:10 +0000 | [diff] [blame] | 2815 | const GRState* getState() const { return state.getState(); } |
| 2816 | }; |
| 2817 | } // end anonymous namespace |
| 2818 | |
| 2819 | |
Ted Kremenek | a42be30 | 2009-02-14 01:43:44 +0000 | [diff] [blame] | 2820 | void CFRefCount::EvalBind(GRStmtNodeBuilderRef& B, SVal location, SVal val) { |
Ted Kremenek | a42be30 | 2009-02-14 01:43:44 +0000 | [diff] [blame] | 2821 | // Are we storing to something that causes the value to "escape"? |
Ted Kremenek | 7aef484 | 2008-04-16 20:40:59 +0000 | [diff] [blame] | 2822 | bool escapes = false; |
| 2823 | |
Ted Kremenek | 28d7eef | 2008-10-18 03:49:51 +0000 | [diff] [blame] | 2824 | // A value escapes in three possible cases (this may change): |
| 2825 | // |
| 2826 | // (1) we are binding to something that is not a memory region. |
| 2827 | // (2) we are binding to a memregion that does not have stack storage |
| 2828 | // (3) we are binding to a memregion with stack storage that the store |
Ted Kremenek | a42be30 | 2009-02-14 01:43:44 +0000 | [diff] [blame] | 2829 | // does not understand. |
Ted Kremenek | a42be30 | 2009-02-14 01:43:44 +0000 | [diff] [blame] | 2830 | GRStateRef state = B.getState(); |
Ted Kremenek | 28d7eef | 2008-10-18 03:49:51 +0000 | [diff] [blame] | 2831 | |
Ted Kremenek | a42be30 | 2009-02-14 01:43:44 +0000 | [diff] [blame] | 2832 | if (!isa<loc::MemRegionVal>(location)) |
Ted Kremenek | 7aef484 | 2008-04-16 20:40:59 +0000 | [diff] [blame] | 2833 | escapes = true; |
Ted Kremenek | b15eba4 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 2834 | else { |
Ted Kremenek | a42be30 | 2009-02-14 01:43:44 +0000 | [diff] [blame] | 2835 | const MemRegion* R = cast<loc::MemRegionVal>(location).getRegion(); |
| 2836 | escapes = !B.getStateManager().hasStackStorage(R); |
Ted Kremenek | 28d7eef | 2008-10-18 03:49:51 +0000 | [diff] [blame] | 2837 | |
| 2838 | if (!escapes) { |
| 2839 | // To test (3), generate a new state with the binding removed. If it is |
| 2840 | // the same state, then it escapes (since the store cannot represent |
| 2841 | // the binding). |
Ted Kremenek | a42be30 | 2009-02-14 01:43:44 +0000 | [diff] [blame] | 2842 | escapes = (state == (state.BindLoc(cast<Loc>(location), UnknownVal()))); |
Ted Kremenek | 28d7eef | 2008-10-18 03:49:51 +0000 | [diff] [blame] | 2843 | } |
Ted Kremenek | b15eba4 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 2844 | } |
Ted Kremenek | a42be30 | 2009-02-14 01:43:44 +0000 | [diff] [blame] | 2845 | |
Ted Kremenek | 2ddb4b2 | 2009-02-14 03:16:10 +0000 | [diff] [blame] | 2846 | // If our store can represent the binding and we aren't storing to something |
| 2847 | // that doesn't have local storage then just return and have the simulation |
| 2848 | // state continue as is. |
| 2849 | if (!escapes) |
| 2850 | return; |
Ted Kremenek | 28d7eef | 2008-10-18 03:49:51 +0000 | [diff] [blame] | 2851 | |
Ted Kremenek | 2ddb4b2 | 2009-02-14 03:16:10 +0000 | [diff] [blame] | 2852 | // Otherwise, find all symbols referenced by 'val' that we are tracking |
| 2853 | // and stop tracking them. |
| 2854 | B.MakeNode(state.scanReachableSymbols<StopTrackingCallback>(val).getState()); |
Ted Kremenek | 3f3c9c8 | 2008-04-16 22:32:20 +0000 | [diff] [blame] | 2855 | } |
| 2856 | |
Ted Kremenek | 0106e20 | 2008-10-24 20:32:50 +0000 | [diff] [blame] | 2857 | std::pair<GRStateRef,bool> |
| 2858 | CFRefCount::HandleSymbolDeath(GRStateManager& VMgr, |
| 2859 | const GRState* St, const Decl* CD, |
Ted Kremenek | b9cd9a7 | 2008-12-05 02:27:51 +0000 | [diff] [blame] | 2860 | SymbolRef sid, |
Ted Kremenek | 0106e20 | 2008-10-24 20:32:50 +0000 | [diff] [blame] | 2861 | RefVal V, bool& hasLeak) { |
Ted Kremenek | 3f3c9c8 | 2008-04-16 22:32:20 +0000 | [diff] [blame] | 2862 | |
Ted Kremenek | 47a7242 | 2009-04-29 18:50:19 +0000 | [diff] [blame] | 2863 | // Any remaining leaks? |
Ted Kremenek | 311f3d4 | 2008-10-22 23:56:21 +0000 | [diff] [blame] | 2864 | hasLeak = V.isOwned() || |
| 2865 | ((V.isNotOwned() || V.isReturnedOwned()) && V.getCount() > 0); |
Ted Kremenek | 4ae925c | 2008-08-14 21:16:54 +0000 | [diff] [blame] | 2866 | |
Ted Kremenek | 47a7242 | 2009-04-29 18:50:19 +0000 | [diff] [blame] | 2867 | GRStateRef state(St, VMgr); |
| 2868 | |
Ted Kremenek | 3f3c9c8 | 2008-04-16 22:32:20 +0000 | [diff] [blame] | 2869 | if (!hasLeak) |
Ted Kremenek | 0106e20 | 2008-10-24 20:32:50 +0000 | [diff] [blame] | 2870 | return std::make_pair(state.remove<RefBindings>(sid), false); |
Ted Kremenek | 3f3c9c8 | 2008-04-16 22:32:20 +0000 | [diff] [blame] | 2871 | |
Ted Kremenek | 0106e20 | 2008-10-24 20:32:50 +0000 | [diff] [blame] | 2872 | return std::make_pair(state.set<RefBindings>(sid, V ^ RefVal::ErrorLeak), |
| 2873 | false); |
Ted Kremenek | 3f3c9c8 | 2008-04-16 22:32:20 +0000 | [diff] [blame] | 2874 | } |
| 2875 | |
Ted Kremenek | 541db37 | 2008-04-24 23:57:27 +0000 | [diff] [blame] | 2876 | |
Ted Kremenek | ffefc35 | 2008-04-11 22:25:11 +0000 | [diff] [blame] | 2877 | |
Ted Kremenek | 541db37 | 2008-04-24 23:57:27 +0000 | [diff] [blame] | 2878 | // Dead symbols. |
| 2879 | |
Ted Kremenek | 708af04 | 2009-02-05 06:50:21 +0000 | [diff] [blame] | 2880 | |
Ted Kremenek | 541db37 | 2008-04-24 23:57:27 +0000 | [diff] [blame] | 2881 | |
Ted Kremenek | d9ccf68 | 2008-04-17 18:12:53 +0000 | [diff] [blame] | 2882 | // Return statements. |
| 2883 | |
Ted Kremenek | abd89ac | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 2884 | void CFRefCount::EvalReturn(ExplodedNodeSet<GRState>& Dst, |
Ted Kremenek | d9ccf68 | 2008-04-17 18:12:53 +0000 | [diff] [blame] | 2885 | GRExprEngine& Eng, |
Ted Kremenek | abd89ac | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 2886 | GRStmtNodeBuilder<GRState>& Builder, |
Ted Kremenek | d9ccf68 | 2008-04-17 18:12:53 +0000 | [diff] [blame] | 2887 | ReturnStmt* S, |
Ted Kremenek | abd89ac | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 2888 | ExplodedNode<GRState>* Pred) { |
Ted Kremenek | d9ccf68 | 2008-04-17 18:12:53 +0000 | [diff] [blame] | 2889 | |
| 2890 | Expr* RetE = S->getRetValue(); |
Ted Kremenek | 9577c1e | 2009-03-03 22:06:47 +0000 | [diff] [blame] | 2891 | if (!RetE) |
Ted Kremenek | d9ccf68 | 2008-04-17 18:12:53 +0000 | [diff] [blame] | 2892 | return; |
| 2893 | |
Ted Kremenek | 9577c1e | 2009-03-03 22:06:47 +0000 | [diff] [blame] | 2894 | GRStateRef state(Builder.GetState(Pred), Eng.getStateManager()); |
Ted Kremenek | b6ac0e5 | 2009-03-04 00:13:50 +0000 | [diff] [blame] | 2895 | SymbolRef Sym = state.GetSValAsScalarOrLoc(RetE).getAsLocSymbol(); |
Ted Kremenek | 9577c1e | 2009-03-03 22:06:47 +0000 | [diff] [blame] | 2896 | |
Ted Kremenek | 74556a1 | 2009-03-26 03:35:11 +0000 | [diff] [blame] | 2897 | if (!Sym) |
Ted Kremenek | 9577c1e | 2009-03-03 22:06:47 +0000 | [diff] [blame] | 2898 | return; |
| 2899 | |
Ted Kremenek | d9ccf68 | 2008-04-17 18:12:53 +0000 | [diff] [blame] | 2900 | // Get the reference count binding (if any). |
Ted Kremenek | 4ae925c | 2008-08-14 21:16:54 +0000 | [diff] [blame] | 2901 | const RefVal* T = state.get<RefBindings>(Sym); |
Ted Kremenek | d9ccf68 | 2008-04-17 18:12:53 +0000 | [diff] [blame] | 2902 | |
| 2903 | if (!T) |
| 2904 | return; |
| 2905 | |
Ted Kremenek | 4ae925c | 2008-08-14 21:16:54 +0000 | [diff] [blame] | 2906 | // Change the reference count. |
Ted Kremenek | 6064a36 | 2008-07-07 16:21:19 +0000 | [diff] [blame] | 2907 | RefVal X = *T; |
Ted Kremenek | d9ccf68 | 2008-04-17 18:12:53 +0000 | [diff] [blame] | 2908 | |
Ted Kremenek | 4ae925c | 2008-08-14 21:16:54 +0000 | [diff] [blame] | 2909 | switch (X.getKind()) { |
Ted Kremenek | d9ccf68 | 2008-04-17 18:12:53 +0000 | [diff] [blame] | 2910 | case RefVal::Owned: { |
| 2911 | unsigned cnt = X.getCount(); |
Ted Kremenek | a3f30dd | 2008-05-22 17:31:13 +0000 | [diff] [blame] | 2912 | assert (cnt > 0); |
| 2913 | X = RefVal::makeReturnedOwned(cnt - 1); |
Ted Kremenek | d9ccf68 | 2008-04-17 18:12:53 +0000 | [diff] [blame] | 2914 | break; |
| 2915 | } |
| 2916 | |
| 2917 | case RefVal::NotOwned: { |
| 2918 | unsigned cnt = X.getCount(); |
| 2919 | X = cnt ? RefVal::makeReturnedOwned(cnt - 1) |
| 2920 | : RefVal::makeReturnedNotOwned(); |
| 2921 | break; |
| 2922 | } |
| 2923 | |
| 2924 | default: |
Ted Kremenek | d9ccf68 | 2008-04-17 18:12:53 +0000 | [diff] [blame] | 2925 | return; |
| 2926 | } |
| 2927 | |
| 2928 | // Update the binding. |
Ted Kremenek | 9178120 | 2008-08-17 03:20:02 +0000 | [diff] [blame] | 2929 | state = state.set<RefBindings>(Sym, X); |
Ted Kremenek | 47a7242 | 2009-04-29 18:50:19 +0000 | [diff] [blame] | 2930 | Pred = Builder.MakeNode(Dst, S, Pred, state); |
| 2931 | |
Ted Kremenek | a208d0c | 2009-04-30 05:51:50 +0000 | [diff] [blame] | 2932 | // Did we cache out? |
| 2933 | if (!Pred) |
| 2934 | return; |
| 2935 | |
Ted Kremenek | 47a7242 | 2009-04-29 18:50:19 +0000 | [diff] [blame] | 2936 | // Any leaks or other errors? |
| 2937 | if (X.isReturnedOwned() && X.getCount() == 0) { |
| 2938 | const Decl *CD = &Eng.getStateManager().getCodeDecl(); |
| 2939 | |
Ted Kremenek | 314b195 | 2009-04-29 23:03:22 +0000 | [diff] [blame] | 2940 | if (const ObjCMethodDecl* MD = dyn_cast<ObjCMethodDecl>(CD)) { |
Ted Kremenek | 286e985 | 2009-05-04 04:57:00 +0000 | [diff] [blame] | 2941 | const RetainSummary &Summ = *Summaries.getMethodSummary(MD); |
| 2942 | if (!Summ.getRetEffect().isOwned()) { |
Ted Kremenek | 47a7242 | 2009-04-29 18:50:19 +0000 | [diff] [blame] | 2943 | static int ReturnOwnLeakTag = 0; |
| 2944 | state = state.set<RefBindings>(Sym, X ^ RefVal::ErrorLeakReturned); |
Ted Kremenek | 47a7242 | 2009-04-29 18:50:19 +0000 | [diff] [blame] | 2945 | // Generate an error node. |
Ted Kremenek | a208d0c | 2009-04-30 05:51:50 +0000 | [diff] [blame] | 2946 | if (ExplodedNode<GRState> *N = |
| 2947 | Builder.generateNode(PostStmt(S, &ReturnOwnLeakTag), state, Pred)) { |
| 2948 | CFRefLeakReport *report = |
| 2949 | new CFRefLeakReport(*static_cast<CFRefBug*>(leakAtReturn), *this, |
| 2950 | N, Sym, Eng); |
| 2951 | BR->EmitReport(report); |
| 2952 | } |
Ted Kremenek | 47a7242 | 2009-04-29 18:50:19 +0000 | [diff] [blame] | 2953 | } |
| 2954 | } |
| 2955 | } |
Ted Kremenek | d9ccf68 | 2008-04-17 18:12:53 +0000 | [diff] [blame] | 2956 | } |
| 2957 | |
Ted Kremenek | eef8f1e | 2008-04-18 19:23:43 +0000 | [diff] [blame] | 2958 | // Assumptions. |
| 2959 | |
Ted Kremenek | abd89ac | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 2960 | const GRState* CFRefCount::EvalAssume(GRStateManager& VMgr, |
| 2961 | const GRState* St, |
Zhongxing Xu | 097fc98 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 2962 | SVal Cond, bool Assumption, |
Ted Kremenek | f22f868 | 2008-07-10 22:03:41 +0000 | [diff] [blame] | 2963 | bool& isFeasible) { |
Ted Kremenek | eef8f1e | 2008-04-18 19:23:43 +0000 | [diff] [blame] | 2964 | |
| 2965 | // FIXME: We may add to the interface of EvalAssume the list of symbols |
| 2966 | // whose assumptions have changed. For now we just iterate through the |
| 2967 | // bindings and check if any of the tracked symbols are NULL. This isn't |
| 2968 | // too bad since the number of symbols we will track in practice are |
| 2969 | // probably small and EvalAssume is only called at branches and a few |
| 2970 | // other places. |
Ted Kremenek | 4ae925c | 2008-08-14 21:16:54 +0000 | [diff] [blame] | 2971 | RefBindings B = St->get<RefBindings>(); |
Ted Kremenek | eef8f1e | 2008-04-18 19:23:43 +0000 | [diff] [blame] | 2972 | |
| 2973 | if (B.isEmpty()) |
| 2974 | return St; |
| 2975 | |
| 2976 | bool changed = false; |
Ted Kremenek | 9178120 | 2008-08-17 03:20:02 +0000 | [diff] [blame] | 2977 | |
| 2978 | GRStateRef state(St, VMgr); |
| 2979 | RefBindings::Factory& RefBFactory = state.get_context<RefBindings>(); |
Ted Kremenek | eef8f1e | 2008-04-18 19:23:43 +0000 | [diff] [blame] | 2980 | |
| 2981 | for (RefBindings::iterator I=B.begin(), E=B.end(); I!=E; ++I) { |
Ted Kremenek | eef8f1e | 2008-04-18 19:23:43 +0000 | [diff] [blame] | 2982 | // Check if the symbol is null (or equal to any constant). |
| 2983 | // If this is the case, stop tracking the symbol. |
Zhongxing Xu | c6b27d0 | 2008-08-29 14:52:36 +0000 | [diff] [blame] | 2984 | if (VMgr.getSymVal(St, I.getKey())) { |
Ted Kremenek | eef8f1e | 2008-04-18 19:23:43 +0000 | [diff] [blame] | 2985 | changed = true; |
| 2986 | B = RefBFactory.Remove(B, I.getKey()); |
| 2987 | } |
| 2988 | } |
| 2989 | |
Ted Kremenek | 9178120 | 2008-08-17 03:20:02 +0000 | [diff] [blame] | 2990 | if (changed) |
| 2991 | state = state.set<RefBindings>(B); |
Ted Kremenek | eef8f1e | 2008-04-18 19:23:43 +0000 | [diff] [blame] | 2992 | |
Ted Kremenek | 4ae925c | 2008-08-14 21:16:54 +0000 | [diff] [blame] | 2993 | return state; |
Ted Kremenek | eef8f1e | 2008-04-18 19:23:43 +0000 | [diff] [blame] | 2994 | } |
Ted Kremenek | a7338b4 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 2995 | |
Ted Kremenek | b657894 | 2009-02-24 19:15:11 +0000 | [diff] [blame] | 2996 | GRStateRef CFRefCount::Update(GRStateRef state, SymbolRef sym, |
| 2997 | RefVal V, ArgEffect E, |
| 2998 | RefVal::Kind& hasErr) { |
Ted Kremenek | 58dd95b | 2009-02-18 18:54:33 +0000 | [diff] [blame] | 2999 | |
| 3000 | // In GC mode [... release] and [... retain] do nothing. |
| 3001 | switch (E) { |
| 3002 | default: break; |
| 3003 | case IncRefMsg: E = isGCEnabled() ? DoNothing : IncRef; break; |
| 3004 | case DecRefMsg: E = isGCEnabled() ? DoNothing : DecRef; break; |
Ted Kremenek | 2126bef | 2009-02-18 21:57:45 +0000 | [diff] [blame] | 3005 | case MakeCollectable: E = isGCEnabled() ? DecRef : DoNothing; break; |
Ted Kremenek | aac8283 | 2009-02-23 17:45:03 +0000 | [diff] [blame] | 3006 | case NewAutoreleasePool: E = isGCEnabled() ? DoNothing : |
| 3007 | NewAutoreleasePool; break; |
Ted Kremenek | 58dd95b | 2009-02-18 18:54:33 +0000 | [diff] [blame] | 3008 | } |
Ted Kremenek | a7338b4 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 3009 | |
Ted Kremenek | 6537a64 | 2009-03-17 19:42:23 +0000 | [diff] [blame] | 3010 | // Handle all use-after-releases. |
| 3011 | if (!isGCEnabled() && V.getKind() == RefVal::Released) { |
| 3012 | V = V ^ RefVal::ErrorUseAfterRelease; |
| 3013 | hasErr = V.getKind(); |
| 3014 | return state.set<RefBindings>(sym, V); |
| 3015 | } |
| 3016 | |
Ted Kremenek | 0d72157 | 2008-03-11 17:48:22 +0000 | [diff] [blame] | 3017 | switch (E) { |
| 3018 | default: |
| 3019 | assert (false && "Unhandled CFRef transition."); |
Ted Kremenek | 6537a64 | 2009-03-17 19:42:23 +0000 | [diff] [blame] | 3020 | |
| 3021 | case Dealloc: |
| 3022 | // Any use of -dealloc in GC is *bad*. |
| 3023 | if (isGCEnabled()) { |
| 3024 | V = V ^ RefVal::ErrorDeallocGC; |
| 3025 | hasErr = V.getKind(); |
| 3026 | break; |
| 3027 | } |
| 3028 | |
| 3029 | switch (V.getKind()) { |
| 3030 | default: |
| 3031 | assert(false && "Invalid case."); |
| 3032 | case RefVal::Owned: |
| 3033 | // The object immediately transitions to the released state. |
| 3034 | V = V ^ RefVal::Released; |
| 3035 | V.clearCounts(); |
| 3036 | return state.set<RefBindings>(sym, V); |
| 3037 | case RefVal::NotOwned: |
| 3038 | V = V ^ RefVal::ErrorDeallocNotOwned; |
| 3039 | hasErr = V.getKind(); |
| 3040 | break; |
| 3041 | } |
| 3042 | break; |
Ted Kremenek | a3f30dd | 2008-05-22 17:31:13 +0000 | [diff] [blame] | 3043 | |
Ted Kremenek | b7826ab | 2009-02-25 23:11:49 +0000 | [diff] [blame] | 3044 | case NewAutoreleasePool: |
| 3045 | assert(!isGCEnabled()); |
| 3046 | return state.add<AutoreleaseStack>(sym); |
| 3047 | |
Ted Kremenek | a3f30dd | 2008-05-22 17:31:13 +0000 | [diff] [blame] | 3048 | case MayEscape: |
| 3049 | if (V.getKind() == RefVal::Owned) { |
Ted Kremenek | 272aa85 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 3050 | V = V ^ RefVal::NotOwned; |
Ted Kremenek | a3f30dd | 2008-05-22 17:31:13 +0000 | [diff] [blame] | 3051 | break; |
| 3052 | } |
Ted Kremenek | 6537a64 | 2009-03-17 19:42:23 +0000 | [diff] [blame] | 3053 | |
Ted Kremenek | a3f30dd | 2008-05-22 17:31:13 +0000 | [diff] [blame] | 3054 | // Fall-through. |
Ted Kremenek | 1b4b656 | 2009-02-25 02:54:57 +0000 | [diff] [blame] | 3055 | |
Ted Kremenek | ede40b7 | 2008-07-09 18:11:16 +0000 | [diff] [blame] | 3056 | case DoNothingByRef: |
Ted Kremenek | 0d72157 | 2008-03-11 17:48:22 +0000 | [diff] [blame] | 3057 | case DoNothing: |
Ted Kremenek | b657894 | 2009-02-24 19:15:11 +0000 | [diff] [blame] | 3058 | return state; |
Ted Kremenek | e5a4bb0 | 2008-06-30 16:57:41 +0000 | [diff] [blame] | 3059 | |
Ted Kremenek | 9b112d2 | 2009-01-28 21:44:40 +0000 | [diff] [blame] | 3060 | case Autorelease: |
Ted Kremenek | 6537a64 | 2009-03-17 19:42:23 +0000 | [diff] [blame] | 3061 | if (isGCEnabled()) |
| 3062 | return state; |
Ted Kremenek | 681fb35 | 2009-03-20 17:34:15 +0000 | [diff] [blame] | 3063 | |
| 3064 | // Update the autorelease counts. |
| 3065 | state = SendAutorelease(state, ARCountFactory, sym); |
Ted Kremenek | 4d99d34 | 2009-05-08 20:01:42 +0000 | [diff] [blame^] | 3066 | V = V.autorelease(); |
Ted Kremenek | 6537a64 | 2009-03-17 19:42:23 +0000 | [diff] [blame] | 3067 | |
Ted Kremenek | 227c537 | 2008-05-06 02:41:27 +0000 | [diff] [blame] | 3068 | case StopTracking: |
Ted Kremenek | b657894 | 2009-02-24 19:15:11 +0000 | [diff] [blame] | 3069 | return state.remove<RefBindings>(sym); |
Ted Kremenek | 3d6ddbb | 2008-08-12 18:30:56 +0000 | [diff] [blame] | 3070 | |
Ted Kremenek | 0d72157 | 2008-03-11 17:48:22 +0000 | [diff] [blame] | 3071 | case IncRef: |
| 3072 | switch (V.getKind()) { |
| 3073 | default: |
| 3074 | assert(false); |
| 3075 | |
| 3076 | case RefVal::Owned: |
Ted Kremenek | 0d72157 | 2008-03-11 17:48:22 +0000 | [diff] [blame] | 3077 | case RefVal::NotOwned: |
Ted Kremenek | 272aa85 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 3078 | V = V + 1; |
Ted Kremenek | 3d6ddbb | 2008-08-12 18:30:56 +0000 | [diff] [blame] | 3079 | break; |
Ted Kremenek | 0d72157 | 2008-03-11 17:48:22 +0000 | [diff] [blame] | 3080 | case RefVal::Released: |
Ted Kremenek | 6537a64 | 2009-03-17 19:42:23 +0000 | [diff] [blame] | 3081 | // Non-GC cases are handled above. |
| 3082 | assert(isGCEnabled()); |
| 3083 | V = (V ^ RefVal::Owned) + 1; |
Ted Kremenek | 0d72157 | 2008-03-11 17:48:22 +0000 | [diff] [blame] | 3084 | break; |
Ted Kremenek | 3d6ddbb | 2008-08-12 18:30:56 +0000 | [diff] [blame] | 3085 | } |
Ted Kremenek | ab2fa2a | 2008-04-10 23:44:06 +0000 | [diff] [blame] | 3086 | break; |
| 3087 | |
Ted Kremenek | 272aa85 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 3088 | case SelfOwn: |
| 3089 | V = V ^ RefVal::NotOwned; |
Ted Kremenek | 58dd95b | 2009-02-18 18:54:33 +0000 | [diff] [blame] | 3090 | // Fall-through. |
Ted Kremenek | 0d72157 | 2008-03-11 17:48:22 +0000 | [diff] [blame] | 3091 | case DecRef: |
| 3092 | switch (V.getKind()) { |
| 3093 | default: |
Ted Kremenek | 6537a64 | 2009-03-17 19:42:23 +0000 | [diff] [blame] | 3094 | // case 'RefVal::Released' handled above. |
Ted Kremenek | 0d72157 | 2008-03-11 17:48:22 +0000 | [diff] [blame] | 3095 | assert (false); |
Ted Kremenek | 3d6ddbb | 2008-08-12 18:30:56 +0000 | [diff] [blame] | 3096 | |
Ted Kremenek | 272aa85 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 3097 | case RefVal::Owned: |
Ted Kremenek | b7d9c9e | 2009-02-18 22:57:22 +0000 | [diff] [blame] | 3098 | assert(V.getCount() > 0); |
| 3099 | if (V.getCount() == 1) V = V ^ RefVal::Released; |
| 3100 | V = V - 1; |
Ted Kremenek | 0d72157 | 2008-03-11 17:48:22 +0000 | [diff] [blame] | 3101 | break; |
Ted Kremenek | 0d72157 | 2008-03-11 17:48:22 +0000 | [diff] [blame] | 3102 | |
Ted Kremenek | 272aa85 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 3103 | case RefVal::NotOwned: |
| 3104 | if (V.getCount() > 0) |
| 3105 | V = V - 1; |
Ted Kremenek | c4f8102 | 2008-04-10 23:09:18 +0000 | [diff] [blame] | 3106 | else { |
Ted Kremenek | 272aa85 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 3107 | V = V ^ RefVal::ErrorReleaseNotOwned; |
Ted Kremenek | 1feab29 | 2008-04-16 04:28:53 +0000 | [diff] [blame] | 3108 | hasErr = V.getKind(); |
Ted Kremenek | 3d6ddbb | 2008-08-12 18:30:56 +0000 | [diff] [blame] | 3109 | } |
Ted Kremenek | 0d72157 | 2008-03-11 17:48:22 +0000 | [diff] [blame] | 3110 | break; |
Ted Kremenek | 6537a64 | 2009-03-17 19:42:23 +0000 | [diff] [blame] | 3111 | |
Ted Kremenek | 0d72157 | 2008-03-11 17:48:22 +0000 | [diff] [blame] | 3112 | case RefVal::Released: |
Ted Kremenek | 6537a64 | 2009-03-17 19:42:23 +0000 | [diff] [blame] | 3113 | // Non-GC cases are handled above. |
| 3114 | assert(isGCEnabled()); |
Ted Kremenek | 272aa85 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 3115 | V = V ^ RefVal::ErrorUseAfterRelease; |
Ted Kremenek | 1feab29 | 2008-04-16 04:28:53 +0000 | [diff] [blame] | 3116 | hasErr = V.getKind(); |
Ted Kremenek | 6537a64 | 2009-03-17 19:42:23 +0000 | [diff] [blame] | 3117 | break; |
Ted Kremenek | 3d6ddbb | 2008-08-12 18:30:56 +0000 | [diff] [blame] | 3118 | } |
Ted Kremenek | ab2fa2a | 2008-04-10 23:44:06 +0000 | [diff] [blame] | 3119 | break; |
Ted Kremenek | 0d72157 | 2008-03-11 17:48:22 +0000 | [diff] [blame] | 3120 | } |
Ted Kremenek | b657894 | 2009-02-24 19:15:11 +0000 | [diff] [blame] | 3121 | return state.set<RefBindings>(sym, V); |
Ted Kremenek | a7338b4 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 3122 | } |
| 3123 | |
Ted Kremenek | 10fe66d | 2008-04-09 01:10:13 +0000 | [diff] [blame] | 3124 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 708af04 | 2009-02-05 06:50:21 +0000 | [diff] [blame] | 3125 | // Handle dead symbols and end-of-path. |
| 3126 | //===----------------------------------------------------------------------===// |
| 3127 | |
| 3128 | void CFRefCount::EvalEndPath(GRExprEngine& Eng, |
| 3129 | GREndPathNodeBuilder<GRState>& Builder) { |
| 3130 | |
| 3131 | const GRState* St = Builder.getState(); |
| 3132 | RefBindings B = St->get<RefBindings>(); |
| 3133 | |
| 3134 | llvm::SmallVector<std::pair<SymbolRef, bool>, 10> Leaked; |
| 3135 | const Decl* CodeDecl = &Eng.getGraph().getCodeDecl(); |
| 3136 | |
| 3137 | for (RefBindings::iterator I = B.begin(), E = B.end(); I != E; ++I) { |
| 3138 | bool hasLeak = false; |
| 3139 | |
| 3140 | std::pair<GRStateRef, bool> X = |
Ted Kremenek | 9577c1e | 2009-03-03 22:06:47 +0000 | [diff] [blame] | 3141 | HandleSymbolDeath(Eng.getStateManager(), St, CodeDecl, |
| 3142 | (*I).first, (*I).second, hasLeak); |
Ted Kremenek | 708af04 | 2009-02-05 06:50:21 +0000 | [diff] [blame] | 3143 | |
| 3144 | St = X.first; |
| 3145 | if (hasLeak) Leaked.push_back(std::make_pair((*I).first, X.second)); |
| 3146 | } |
| 3147 | |
| 3148 | if (Leaked.empty()) |
| 3149 | return; |
| 3150 | |
| 3151 | ExplodedNode<GRState>* N = Builder.MakeNode(St); |
| 3152 | |
| 3153 | if (!N) |
| 3154 | return; |
| 3155 | |
| 3156 | for (llvm::SmallVector<std::pair<SymbolRef,bool>, 10>::iterator |
| 3157 | I = Leaked.begin(), E = Leaked.end(); I != E; ++I) { |
| 3158 | |
| 3159 | CFRefBug *BT = static_cast<CFRefBug*>(I->second ? leakAtReturn |
| 3160 | : leakWithinFunction); |
| 3161 | assert(BT && "BugType not initialized."); |
Ted Kremenek | c034f71 | 2009-04-07 05:07:44 +0000 | [diff] [blame] | 3162 | CFRefLeakReport* report = new CFRefLeakReport(*BT, *this, N, I->first, Eng); |
Ted Kremenek | 708af04 | 2009-02-05 06:50:21 +0000 | [diff] [blame] | 3163 | BR->EmitReport(report); |
| 3164 | } |
| 3165 | } |
| 3166 | |
| 3167 | void CFRefCount::EvalDeadSymbols(ExplodedNodeSet<GRState>& Dst, |
| 3168 | GRExprEngine& Eng, |
| 3169 | GRStmtNodeBuilder<GRState>& Builder, |
| 3170 | ExplodedNode<GRState>* Pred, |
| 3171 | Stmt* S, |
| 3172 | const GRState* St, |
| 3173 | SymbolReaper& SymReaper) { |
| 3174 | |
Ted Kremenek | 876d8df | 2009-02-19 23:47:02 +0000 | [diff] [blame] | 3175 | // FIXME: a lot of copy-and-paste from EvalEndPath. Refactor. |
Ted Kremenek | 708af04 | 2009-02-05 06:50:21 +0000 | [diff] [blame] | 3176 | RefBindings B = St->get<RefBindings>(); |
| 3177 | llvm::SmallVector<std::pair<SymbolRef,bool>, 10> Leaked; |
| 3178 | |
| 3179 | for (SymbolReaper::dead_iterator I = SymReaper.dead_begin(), |
| 3180 | E = SymReaper.dead_end(); I != E; ++I) { |
| 3181 | |
| 3182 | const RefVal* T = B.lookup(*I); |
| 3183 | if (!T) continue; |
| 3184 | |
| 3185 | bool hasLeak = false; |
| 3186 | |
| 3187 | std::pair<GRStateRef, bool> X |
Ted Kremenek | 876d8df | 2009-02-19 23:47:02 +0000 | [diff] [blame] | 3188 | = HandleSymbolDeath(Eng.getStateManager(), St, 0, *I, *T, hasLeak); |
Ted Kremenek | 708af04 | 2009-02-05 06:50:21 +0000 | [diff] [blame] | 3189 | |
| 3190 | St = X.first; |
| 3191 | |
| 3192 | if (hasLeak) |
| 3193 | Leaked.push_back(std::make_pair(*I,X.second)); |
| 3194 | } |
| 3195 | |
Ted Kremenek | 876d8df | 2009-02-19 23:47:02 +0000 | [diff] [blame] | 3196 | if (!Leaked.empty()) { |
| 3197 | // Create a new intermediate node representing the leak point. We |
| 3198 | // use a special program point that represents this checker-specific |
| 3199 | // transition. We use the address of RefBIndex as a unique tag for this |
| 3200 | // checker. We will create another node (if we don't cache out) that |
| 3201 | // removes the retain-count bindings from the state. |
| 3202 | // NOTE: We use 'generateNode' so that it does interplay with the |
| 3203 | // auto-transition logic. |
| 3204 | ExplodedNode<GRState>* N = |
| 3205 | Builder.generateNode(PostStmtCustom(S, &LeakProgramPointTag), St, Pred); |
Ted Kremenek | 708af04 | 2009-02-05 06:50:21 +0000 | [diff] [blame] | 3206 | |
Ted Kremenek | 876d8df | 2009-02-19 23:47:02 +0000 | [diff] [blame] | 3207 | if (!N) |
| 3208 | return; |
| 3209 | |
| 3210 | // Generate the bug reports. |
| 3211 | for (llvm::SmallVectorImpl<std::pair<SymbolRef,bool> >::iterator |
| 3212 | I = Leaked.begin(), E = Leaked.end(); I != E; ++I) { |
| 3213 | |
| 3214 | CFRefBug *BT = static_cast<CFRefBug*>(I->second ? leakAtReturn |
| 3215 | : leakWithinFunction); |
| 3216 | assert(BT && "BugType not initialized."); |
Ted Kremenek | 56c70aa | 2009-02-23 16:54:00 +0000 | [diff] [blame] | 3217 | CFRefLeakReport* report = new CFRefLeakReport(*BT, *this, N, |
| 3218 | I->first, Eng); |
Ted Kremenek | 876d8df | 2009-02-19 23:47:02 +0000 | [diff] [blame] | 3219 | BR->EmitReport(report); |
| 3220 | } |
Ted Kremenek | 708af04 | 2009-02-05 06:50:21 +0000 | [diff] [blame] | 3221 | |
Ted Kremenek | 876d8df | 2009-02-19 23:47:02 +0000 | [diff] [blame] | 3222 | Pred = N; |
Ted Kremenek | 708af04 | 2009-02-05 06:50:21 +0000 | [diff] [blame] | 3223 | } |
Ted Kremenek | 876d8df | 2009-02-19 23:47:02 +0000 | [diff] [blame] | 3224 | |
| 3225 | // Now generate a new node that nukes the old bindings. |
| 3226 | GRStateRef state(St, Eng.getStateManager()); |
| 3227 | RefBindings::Factory& F = state.get_context<RefBindings>(); |
| 3228 | |
| 3229 | for (SymbolReaper::dead_iterator I = SymReaper.dead_begin(), |
| 3230 | E = SymReaper.dead_end(); I!=E; ++I) |
| 3231 | B = F.Remove(B, *I); |
| 3232 | |
| 3233 | state = state.set<RefBindings>(B); |
| 3234 | Builder.MakeNode(Dst, S, Pred, state); |
Ted Kremenek | 708af04 | 2009-02-05 06:50:21 +0000 | [diff] [blame] | 3235 | } |
| 3236 | |
| 3237 | void CFRefCount::ProcessNonLeakError(ExplodedNodeSet<GRState>& Dst, |
| 3238 | GRStmtNodeBuilder<GRState>& Builder, |
| 3239 | Expr* NodeExpr, Expr* ErrorExpr, |
| 3240 | ExplodedNode<GRState>* Pred, |
| 3241 | const GRState* St, |
| 3242 | RefVal::Kind hasErr, SymbolRef Sym) { |
| 3243 | Builder.BuildSinks = true; |
| 3244 | GRExprEngine::NodeTy* N = Builder.MakeNode(Dst, NodeExpr, Pred, St); |
| 3245 | |
| 3246 | if (!N) return; |
| 3247 | |
| 3248 | CFRefBug *BT = 0; |
| 3249 | |
Ted Kremenek | 6537a64 | 2009-03-17 19:42:23 +0000 | [diff] [blame] | 3250 | switch (hasErr) { |
| 3251 | default: |
| 3252 | assert(false && "Unhandled error."); |
| 3253 | return; |
| 3254 | case RefVal::ErrorUseAfterRelease: |
| 3255 | BT = static_cast<CFRefBug*>(useAfterRelease); |
| 3256 | break; |
| 3257 | case RefVal::ErrorReleaseNotOwned: |
| 3258 | BT = static_cast<CFRefBug*>(releaseNotOwned); |
| 3259 | break; |
| 3260 | case RefVal::ErrorDeallocGC: |
| 3261 | BT = static_cast<CFRefBug*>(deallocGC); |
| 3262 | break; |
| 3263 | case RefVal::ErrorDeallocNotOwned: |
| 3264 | BT = static_cast<CFRefBug*>(deallocNotOwned); |
| 3265 | break; |
Ted Kremenek | 708af04 | 2009-02-05 06:50:21 +0000 | [diff] [blame] | 3266 | } |
| 3267 | |
Ted Kremenek | c26c469 | 2009-02-18 03:48:14 +0000 | [diff] [blame] | 3268 | CFRefReport *report = new CFRefReport(*BT, *this, N, Sym); |
Ted Kremenek | 708af04 | 2009-02-05 06:50:21 +0000 | [diff] [blame] | 3269 | report->addRange(ErrorExpr->getSourceRange()); |
| 3270 | BR->EmitReport(report); |
| 3271 | } |
| 3272 | |
| 3273 | //===----------------------------------------------------------------------===// |
Ted Kremenek | b1983ba | 2008-04-10 22:16:52 +0000 | [diff] [blame] | 3274 | // Transfer function creation for external clients. |
Ted Kremenek | a7338b4 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 3275 | //===----------------------------------------------------------------------===// |
| 3276 | |
Ted Kremenek | fe30beb | 2008-04-30 23:47:44 +0000 | [diff] [blame] | 3277 | GRTransferFuncs* clang::MakeCFRefCountTF(ASTContext& Ctx, bool GCEnabled, |
| 3278 | const LangOptions& lopts) { |
Ted Kremenek | 9f20c7c | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 3279 | return new CFRefCount(Ctx, GCEnabled, lopts); |
Ted Kremenek | a4c7429 | 2008-04-10 22:58:08 +0000 | [diff] [blame] | 3280 | } |