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