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