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