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