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