Jordy Rose | 910c405 | 2011-09-02 06:44:22 +0000 | [diff] [blame] | 1 | //==-- RetainCountChecker.cpp - Checks for leaks and other issues -*- C++ -*--// |
Ted Kremenek | 2fff37e | 2008-03-06 00:08:09 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
Jordy Rose | 910c405 | 2011-09-02 06:44:22 +0000 | [diff] [blame] | 10 | // This file defines the methods for RetainCountChecker, which implements |
| 11 | // a reference count checker for Core Foundation and Cocoa on (Mac OS X). |
Ted Kremenek | 2fff37e | 2008-03-06 00:08:09 +0000 | [diff] [blame] | 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
Jordy Rose | 910c405 | 2011-09-02 06:44:22 +0000 | [diff] [blame] | 15 | #include "ClangSACheckers.h" |
Benjamin Kramer | 2fa67ef | 2012-12-01 15:09:41 +0000 | [diff] [blame] | 16 | #include "clang/AST/Attr.h" |
Ted Kremenek | b277159 | 2011-03-30 17:41:19 +0000 | [diff] [blame] | 17 | #include "clang/AST/DeclCXX.h" |
Benjamin Kramer | 2fa67ef | 2012-12-01 15:09:41 +0000 | [diff] [blame] | 18 | #include "clang/AST/DeclObjC.h" |
| 19 | #include "clang/AST/ParentMap.h" |
| 20 | #include "clang/Analysis/DomainSpecific/CocoaConventions.h" |
Ted Kremenek | 0b526b4 | 2010-02-18 00:05:58 +0000 | [diff] [blame] | 21 | #include "clang/Basic/LangOptions.h" |
| 22 | #include "clang/Basic/SourceManager.h" |
Ted Kremenek | 9b66371 | 2011-02-10 01:03:03 +0000 | [diff] [blame] | 23 | #include "clang/StaticAnalyzer/Core/BugReporter/BugType.h" |
| 24 | #include "clang/StaticAnalyzer/Core/BugReporter/PathDiagnostic.h" |
Chandler Carruth | 55fc873 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 25 | #include "clang/StaticAnalyzer/Core/Checker.h" |
| 26 | #include "clang/StaticAnalyzer/Core/CheckerManager.h" |
Jordan Rose | f540c54 | 2012-07-26 21:39:41 +0000 | [diff] [blame] | 27 | #include "clang/StaticAnalyzer/Core/PathSensitive/CallEvent.h" |
Jordy Rose | 910c405 | 2011-09-02 06:44:22 +0000 | [diff] [blame] | 28 | #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h" |
Ted Kremenek | 18c66fd | 2011-08-15 22:09:50 +0000 | [diff] [blame] | 29 | #include "clang/StaticAnalyzer/Core/PathSensitive/ProgramStateTrait.h" |
Ted Kremenek | 9b66371 | 2011-02-10 01:03:03 +0000 | [diff] [blame] | 30 | #include "clang/StaticAnalyzer/Core/PathSensitive/SymbolManager.h" |
Ted Kremenek | 6b3a0f7 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 31 | #include "llvm/ADT/DenseMap.h" |
| 32 | #include "llvm/ADT/FoldingSet.h" |
Ted Kremenek | 6d34893 | 2008-10-21 15:53:15 +0000 | [diff] [blame] | 33 | #include "llvm/ADT/ImmutableList.h" |
Ted Kremenek | 0b526b4 | 2010-02-18 00:05:58 +0000 | [diff] [blame] | 34 | #include "llvm/ADT/ImmutableMap.h" |
Ted Kremenek | 6ed9afc | 2008-05-16 18:33:44 +0000 | [diff] [blame] | 35 | #include "llvm/ADT/STLExtras.h" |
Benjamin Kramer | 2fa67ef | 2012-12-01 15:09:41 +0000 | [diff] [blame] | 36 | #include "llvm/ADT/SmallString.h" |
Ted Kremenek | 0b526b4 | 2010-02-18 00:05:58 +0000 | [diff] [blame] | 37 | #include "llvm/ADT/StringExtras.h" |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 38 | #include <cstdarg> |
Ted Kremenek | 2fff37e | 2008-03-06 00:08:09 +0000 | [diff] [blame] | 39 | |
| 40 | using namespace clang; |
Ted Kremenek | 9ef6537 | 2010-12-23 07:20:52 +0000 | [diff] [blame] | 41 | using namespace ento; |
Ted Kremenek | a64e89b | 2010-01-27 06:13:48 +0000 | [diff] [blame] | 42 | using llvm::StrInStrNoCase; |
Ted Kremenek | 4c79e55 | 2008-11-05 16:54:44 +0000 | [diff] [blame] | 43 | |
Ted Kremenek | 05cbe1a | 2008-04-09 23:49:11 +0000 | [diff] [blame] | 44 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 45 | // Primitives used for constructing summaries for function/method calls. |
Ted Kremenek | 05cbe1a | 2008-04-09 23:49:11 +0000 | [diff] [blame] | 46 | //===----------------------------------------------------------------------===// |
| 47 | |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 48 | /// ArgEffect is used to summarize a function/method call's effect on a |
| 49 | /// particular argument. |
Jordy Rose | bd85b13 | 2011-08-24 19:10:50 +0000 | [diff] [blame] | 50 | enum ArgEffect { DoNothing, Autorelease, Dealloc, DecRef, DecRefMsg, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 51 | DecRefBridgedTransfered, |
Jordy Rose | bd85b13 | 2011-08-24 19:10:50 +0000 | [diff] [blame] | 52 | IncRefMsg, IncRef, MakeCollectable, MayEscape, |
Anna Zaks | 554067f | 2012-08-29 23:23:43 +0000 | [diff] [blame] | 53 | NewAutoreleasePool, |
| 54 | |
| 55 | // Stop tracking the argument - the effect of the call is |
| 56 | // unknown. |
| 57 | StopTracking, |
| 58 | |
| 59 | // In some cases, we obtain a better summary for this checker |
| 60 | // by looking at the call site than by inlining the function. |
| 61 | // Signifies that we should stop tracking the symbol even if |
| 62 | // the function is inlined. |
| 63 | StopTrackingHard, |
| 64 | |
| 65 | // The function decrements the reference count and the checker |
| 66 | // should stop tracking the argument. |
| 67 | DecRefAndStopTrackingHard, DecRefMsgAndStopTrackingHard |
| 68 | }; |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 69 | |
Ted Kremenek | 6b3a0f7 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 70 | namespace llvm { |
Ted Kremenek | b77449c | 2009-05-03 05:20:50 +0000 | [diff] [blame] | 71 | template <> struct FoldingSetTrait<ArgEffect> { |
| 72 | static inline void Profile(const ArgEffect X, FoldingSetNodeID& ID) { |
| 73 | ID.AddInteger((unsigned) X); |
| 74 | } |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 75 | }; |
Ted Kremenek | 6b3a0f7 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 76 | } // end llvm namespace |
| 77 | |
Ted Kremenek | b77449c | 2009-05-03 05:20:50 +0000 | [diff] [blame] | 78 | /// ArgEffects summarizes the effects of a function/method call on all of |
| 79 | /// its arguments. |
| 80 | typedef llvm::ImmutableMap<unsigned,ArgEffect> ArgEffects; |
| 81 | |
Ted Kremenek | 6b3a0f7 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 82 | namespace { |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 83 | |
| 84 | /// RetEffect is used to summarize a function/method call's behavior with |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 85 | /// respect to its return value. |
Kovarththanan Rajaratnam | ba5fb5a | 2009-11-28 06:07:30 +0000 | [diff] [blame] | 86 | class RetEffect { |
Ted Kremenek | 6b3a0f7 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 87 | public: |
Jordy Rose | 76c506f | 2011-08-21 21:58:18 +0000 | [diff] [blame] | 88 | enum Kind { NoRet, OwnedSymbol, OwnedAllocatedSymbol, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 89 | NotOwnedSymbol, GCNotOwnedSymbol, ARCNotOwnedSymbol, |
Anna Zaks | 554067f | 2012-08-29 23:23:43 +0000 | [diff] [blame] | 90 | OwnedWhenTrackedReceiver, |
| 91 | // Treat this function as returning a non-tracked symbol even if |
| 92 | // the function has been inlined. This is used where the call |
| 93 | // site summary is more presise than the summary indirectly produced |
| 94 | // by inlining the function |
| 95 | NoRetHard |
| 96 | }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 97 | |
| 98 | enum ObjKind { CF, ObjC, AnyObj }; |
Ted Kremenek | 2d1652e | 2009-01-28 05:56:51 +0000 | [diff] [blame] | 99 | |
Ted Kremenek | 6b3a0f7 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 100 | private: |
Ted Kremenek | 2d1652e | 2009-01-28 05:56:51 +0000 | [diff] [blame] | 101 | Kind K; |
| 102 | ObjKind O; |
Ted Kremenek | 2d1652e | 2009-01-28 05:56:51 +0000 | [diff] [blame] | 103 | |
Jordy Rose | 76c506f | 2011-08-21 21:58:18 +0000 | [diff] [blame] | 104 | RetEffect(Kind k, ObjKind o = AnyObj) : K(k), O(o) {} |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 105 | |
Ted Kremenek | 6b3a0f7 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 106 | public: |
Ted Kremenek | 2d1652e | 2009-01-28 05:56:51 +0000 | [diff] [blame] | 107 | Kind getKind() const { return K; } |
| 108 | |
| 109 | ObjKind getObjKind() const { return O; } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 110 | |
Ted Kremenek | a883355 | 2009-04-29 23:03:22 +0000 | [diff] [blame] | 111 | bool isOwned() const { |
Ted Kremenek | 78a35a3 | 2009-05-12 20:06:54 +0000 | [diff] [blame] | 112 | return K == OwnedSymbol || K == OwnedAllocatedSymbol || |
| 113 | K == OwnedWhenTrackedReceiver; |
Ted Kremenek | a883355 | 2009-04-29 23:03:22 +0000 | [diff] [blame] | 114 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 115 | |
Jordy Rose | 4df54fe | 2011-08-23 04:27:15 +0000 | [diff] [blame] | 116 | bool operator==(const RetEffect &Other) const { |
| 117 | return K == Other.K && O == Other.O; |
| 118 | } |
| 119 | |
Ted Kremenek | 78a35a3 | 2009-05-12 20:06:54 +0000 | [diff] [blame] | 120 | static RetEffect MakeOwnedWhenTrackedReceiver() { |
| 121 | return RetEffect(OwnedWhenTrackedReceiver, ObjC); |
| 122 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 123 | |
Ted Kremenek | 2d1652e | 2009-01-28 05:56:51 +0000 | [diff] [blame] | 124 | static RetEffect MakeOwned(ObjKind o, bool isAllocated = false) { |
| 125 | return RetEffect(isAllocated ? OwnedAllocatedSymbol : OwnedSymbol, o); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 126 | } |
Ted Kremenek | 2d1652e | 2009-01-28 05:56:51 +0000 | [diff] [blame] | 127 | static RetEffect MakeNotOwned(ObjKind o) { |
| 128 | return RetEffect(NotOwnedSymbol, o); |
Ted Kremenek | e798e7c | 2009-04-27 19:14:45 +0000 | [diff] [blame] | 129 | } |
| 130 | static RetEffect MakeGCNotOwned() { |
| 131 | return RetEffect(GCNotOwnedSymbol, ObjC); |
| 132 | } |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 133 | static RetEffect MakeARCNotOwned() { |
| 134 | return RetEffect(ARCNotOwnedSymbol, ObjC); |
| 135 | } |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 136 | static RetEffect MakeNoRet() { |
| 137 | return RetEffect(NoRet); |
Ted Kremenek | a734470 | 2008-06-23 18:02:52 +0000 | [diff] [blame] | 138 | } |
Anna Zaks | 554067f | 2012-08-29 23:23:43 +0000 | [diff] [blame] | 139 | static RetEffect MakeNoRetHard() { |
| 140 | return RetEffect(NoRetHard); |
| 141 | } |
Jordy Rose | ef94588 | 2012-03-18 01:26:10 +0000 | [diff] [blame] | 142 | |
| 143 | void Profile(llvm::FoldingSetNodeID& ID) const { |
| 144 | ID.AddInteger((unsigned) K); |
| 145 | ID.AddInteger((unsigned) O); |
| 146 | } |
Ted Kremenek | 6b3a0f7 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 147 | }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 148 | |
Ted Kremenek | b7ddd9b | 2009-11-13 01:54:21 +0000 | [diff] [blame] | 149 | //===----------------------------------------------------------------------===// |
| 150 | // Reference-counting logic (typestate + counts). |
| 151 | //===----------------------------------------------------------------------===// |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 152 | |
Kovarththanan Rajaratnam | ba5fb5a | 2009-11-28 06:07:30 +0000 | [diff] [blame] | 153 | class RefVal { |
Ted Kremenek | b7ddd9b | 2009-11-13 01:54:21 +0000 | [diff] [blame] | 154 | public: |
| 155 | enum Kind { |
| 156 | Owned = 0, // Owning reference. |
| 157 | NotOwned, // Reference is not owned by still valid (not freed). |
| 158 | Released, // Object has been released. |
| 159 | ReturnedOwned, // Returned object passes ownership to caller. |
| 160 | ReturnedNotOwned, // Return object does not pass ownership to caller. |
| 161 | ERROR_START, |
| 162 | ErrorDeallocNotOwned, // -dealloc called on non-owned object. |
| 163 | ErrorDeallocGC, // Calling -dealloc with GC enabled. |
| 164 | ErrorUseAfterRelease, // Object used after released. |
| 165 | ErrorReleaseNotOwned, // Release of an object that was not owned. |
| 166 | ERROR_LEAK_START, |
| 167 | ErrorLeak, // A memory leak due to excessive reference counts. |
| 168 | ErrorLeakReturned, // A memory leak due to the returning method not having |
| 169 | // the correct naming conventions. |
| 170 | ErrorGCLeakReturned, |
| 171 | ErrorOverAutorelease, |
| 172 | ErrorReturnedNotOwned |
| 173 | }; |
Ted Kremenek | dcee3ce | 2010-07-01 20:16:50 +0000 | [diff] [blame] | 174 | |
Ted Kremenek | b7ddd9b | 2009-11-13 01:54:21 +0000 | [diff] [blame] | 175 | private: |
| 176 | Kind kind; |
| 177 | RetEffect::ObjKind okind; |
| 178 | unsigned Cnt; |
| 179 | unsigned ACnt; |
| 180 | QualType T; |
Ted Kremenek | dcee3ce | 2010-07-01 20:16:50 +0000 | [diff] [blame] | 181 | |
Ted Kremenek | b7ddd9b | 2009-11-13 01:54:21 +0000 | [diff] [blame] | 182 | RefVal(Kind k, RetEffect::ObjKind o, unsigned cnt, unsigned acnt, QualType t) |
| 183 | : kind(k), okind(o), Cnt(cnt), ACnt(acnt), T(t) {} |
Ted Kremenek | dcee3ce | 2010-07-01 20:16:50 +0000 | [diff] [blame] | 184 | |
Ted Kremenek | b7ddd9b | 2009-11-13 01:54:21 +0000 | [diff] [blame] | 185 | public: |
| 186 | Kind getKind() const { return kind; } |
Ted Kremenek | dcee3ce | 2010-07-01 20:16:50 +0000 | [diff] [blame] | 187 | |
Ted Kremenek | b7ddd9b | 2009-11-13 01:54:21 +0000 | [diff] [blame] | 188 | RetEffect::ObjKind getObjKind() const { return okind; } |
Ted Kremenek | dcee3ce | 2010-07-01 20:16:50 +0000 | [diff] [blame] | 189 | |
Ted Kremenek | b7ddd9b | 2009-11-13 01:54:21 +0000 | [diff] [blame] | 190 | unsigned getCount() const { return Cnt; } |
| 191 | unsigned getAutoreleaseCount() const { return ACnt; } |
| 192 | unsigned getCombinedCounts() const { return Cnt + ACnt; } |
| 193 | void clearCounts() { Cnt = 0; ACnt = 0; } |
| 194 | void setCount(unsigned i) { Cnt = i; } |
| 195 | void setAutoreleaseCount(unsigned i) { ACnt = i; } |
Ted Kremenek | dcee3ce | 2010-07-01 20:16:50 +0000 | [diff] [blame] | 196 | |
Ted Kremenek | b7ddd9b | 2009-11-13 01:54:21 +0000 | [diff] [blame] | 197 | QualType getType() const { return T; } |
Ted Kremenek | dcee3ce | 2010-07-01 20:16:50 +0000 | [diff] [blame] | 198 | |
Ted Kremenek | b7ddd9b | 2009-11-13 01:54:21 +0000 | [diff] [blame] | 199 | bool isOwned() const { |
| 200 | return getKind() == Owned; |
| 201 | } |
Ted Kremenek | dcee3ce | 2010-07-01 20:16:50 +0000 | [diff] [blame] | 202 | |
Ted Kremenek | b7ddd9b | 2009-11-13 01:54:21 +0000 | [diff] [blame] | 203 | bool isNotOwned() const { |
| 204 | return getKind() == NotOwned; |
| 205 | } |
Ted Kremenek | dcee3ce | 2010-07-01 20:16:50 +0000 | [diff] [blame] | 206 | |
Ted Kremenek | b7ddd9b | 2009-11-13 01:54:21 +0000 | [diff] [blame] | 207 | bool isReturnedOwned() const { |
| 208 | return getKind() == ReturnedOwned; |
| 209 | } |
Ted Kremenek | dcee3ce | 2010-07-01 20:16:50 +0000 | [diff] [blame] | 210 | |
Ted Kremenek | b7ddd9b | 2009-11-13 01:54:21 +0000 | [diff] [blame] | 211 | bool isReturnedNotOwned() const { |
| 212 | return getKind() == ReturnedNotOwned; |
| 213 | } |
Ted Kremenek | dcee3ce | 2010-07-01 20:16:50 +0000 | [diff] [blame] | 214 | |
Ted Kremenek | b7ddd9b | 2009-11-13 01:54:21 +0000 | [diff] [blame] | 215 | static RefVal makeOwned(RetEffect::ObjKind o, QualType t, |
| 216 | unsigned Count = 1) { |
| 217 | return RefVal(Owned, o, Count, 0, t); |
| 218 | } |
Ted Kremenek | dcee3ce | 2010-07-01 20:16:50 +0000 | [diff] [blame] | 219 | |
Ted Kremenek | b7ddd9b | 2009-11-13 01:54:21 +0000 | [diff] [blame] | 220 | static RefVal makeNotOwned(RetEffect::ObjKind o, QualType t, |
| 221 | unsigned Count = 0) { |
| 222 | return RefVal(NotOwned, o, Count, 0, t); |
| 223 | } |
Ted Kremenek | dcee3ce | 2010-07-01 20:16:50 +0000 | [diff] [blame] | 224 | |
Ted Kremenek | b7ddd9b | 2009-11-13 01:54:21 +0000 | [diff] [blame] | 225 | // Comparison, profiling, and pretty-printing. |
Ted Kremenek | dcee3ce | 2010-07-01 20:16:50 +0000 | [diff] [blame] | 226 | |
Ted Kremenek | b7ddd9b | 2009-11-13 01:54:21 +0000 | [diff] [blame] | 227 | bool operator==(const RefVal& X) const { |
| 228 | return kind == X.kind && Cnt == X.Cnt && T == X.T && ACnt == X.ACnt; |
| 229 | } |
Ted Kremenek | dcee3ce | 2010-07-01 20:16:50 +0000 | [diff] [blame] | 230 | |
Ted Kremenek | b7ddd9b | 2009-11-13 01:54:21 +0000 | [diff] [blame] | 231 | RefVal operator-(size_t i) const { |
| 232 | return RefVal(getKind(), getObjKind(), getCount() - i, |
| 233 | getAutoreleaseCount(), getType()); |
| 234 | } |
Ted Kremenek | dcee3ce | 2010-07-01 20:16:50 +0000 | [diff] [blame] | 235 | |
Ted Kremenek | b7ddd9b | 2009-11-13 01:54:21 +0000 | [diff] [blame] | 236 | RefVal operator+(size_t i) const { |
| 237 | return RefVal(getKind(), getObjKind(), getCount() + i, |
| 238 | getAutoreleaseCount(), getType()); |
| 239 | } |
Ted Kremenek | dcee3ce | 2010-07-01 20:16:50 +0000 | [diff] [blame] | 240 | |
Ted Kremenek | b7ddd9b | 2009-11-13 01:54:21 +0000 | [diff] [blame] | 241 | RefVal operator^(Kind k) const { |
| 242 | return RefVal(k, getObjKind(), getCount(), getAutoreleaseCount(), |
| 243 | getType()); |
| 244 | } |
Ted Kremenek | dcee3ce | 2010-07-01 20:16:50 +0000 | [diff] [blame] | 245 | |
Ted Kremenek | b7ddd9b | 2009-11-13 01:54:21 +0000 | [diff] [blame] | 246 | RefVal autorelease() const { |
| 247 | return RefVal(getKind(), getObjKind(), getCount(), getAutoreleaseCount()+1, |
| 248 | getType()); |
| 249 | } |
Ted Kremenek | dcee3ce | 2010-07-01 20:16:50 +0000 | [diff] [blame] | 250 | |
Ted Kremenek | b7ddd9b | 2009-11-13 01:54:21 +0000 | [diff] [blame] | 251 | void Profile(llvm::FoldingSetNodeID& ID) const { |
| 252 | ID.AddInteger((unsigned) kind); |
| 253 | ID.AddInteger(Cnt); |
| 254 | ID.AddInteger(ACnt); |
| 255 | ID.Add(T); |
| 256 | } |
Ted Kremenek | dcee3ce | 2010-07-01 20:16:50 +0000 | [diff] [blame] | 257 | |
Ted Kremenek | 9c378f7 | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 258 | void print(raw_ostream &Out) const; |
Ted Kremenek | b7ddd9b | 2009-11-13 01:54:21 +0000 | [diff] [blame] | 259 | }; |
| 260 | |
Ted Kremenek | 9c378f7 | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 261 | void RefVal::print(raw_ostream &Out) const { |
Ted Kremenek | b7ddd9b | 2009-11-13 01:54:21 +0000 | [diff] [blame] | 262 | if (!T.isNull()) |
Jordy Rose | dbd658e | 2011-08-28 19:11:56 +0000 | [diff] [blame] | 263 | Out << "Tracked " << T.getAsString() << '/'; |
Ted Kremenek | dcee3ce | 2010-07-01 20:16:50 +0000 | [diff] [blame] | 264 | |
Ted Kremenek | b7ddd9b | 2009-11-13 01:54:21 +0000 | [diff] [blame] | 265 | switch (getKind()) { |
Jordy Rose | 910c405 | 2011-09-02 06:44:22 +0000 | [diff] [blame] | 266 | default: llvm_unreachable("Invalid RefVal kind"); |
Ted Kremenek | b7ddd9b | 2009-11-13 01:54:21 +0000 | [diff] [blame] | 267 | case Owned: { |
| 268 | Out << "Owned"; |
| 269 | unsigned cnt = getCount(); |
| 270 | if (cnt) Out << " (+ " << cnt << ")"; |
| 271 | break; |
| 272 | } |
Ted Kremenek | dcee3ce | 2010-07-01 20:16:50 +0000 | [diff] [blame] | 273 | |
Ted Kremenek | b7ddd9b | 2009-11-13 01:54:21 +0000 | [diff] [blame] | 274 | case NotOwned: { |
| 275 | Out << "NotOwned"; |
| 276 | unsigned cnt = getCount(); |
| 277 | if (cnt) Out << " (+ " << cnt << ")"; |
| 278 | break; |
| 279 | } |
Ted Kremenek | dcee3ce | 2010-07-01 20:16:50 +0000 | [diff] [blame] | 280 | |
Ted Kremenek | b7ddd9b | 2009-11-13 01:54:21 +0000 | [diff] [blame] | 281 | case ReturnedOwned: { |
| 282 | Out << "ReturnedOwned"; |
| 283 | unsigned cnt = getCount(); |
| 284 | if (cnt) Out << " (+ " << cnt << ")"; |
| 285 | break; |
| 286 | } |
Ted Kremenek | dcee3ce | 2010-07-01 20:16:50 +0000 | [diff] [blame] | 287 | |
Ted Kremenek | b7ddd9b | 2009-11-13 01:54:21 +0000 | [diff] [blame] | 288 | case ReturnedNotOwned: { |
| 289 | Out << "ReturnedNotOwned"; |
| 290 | unsigned cnt = getCount(); |
| 291 | if (cnt) Out << " (+ " << cnt << ")"; |
| 292 | break; |
| 293 | } |
Ted Kremenek | dcee3ce | 2010-07-01 20:16:50 +0000 | [diff] [blame] | 294 | |
Ted Kremenek | b7ddd9b | 2009-11-13 01:54:21 +0000 | [diff] [blame] | 295 | case Released: |
| 296 | Out << "Released"; |
| 297 | break; |
Ted Kremenek | dcee3ce | 2010-07-01 20:16:50 +0000 | [diff] [blame] | 298 | |
Ted Kremenek | b7ddd9b | 2009-11-13 01:54:21 +0000 | [diff] [blame] | 299 | case ErrorDeallocGC: |
| 300 | Out << "-dealloc (GC)"; |
| 301 | break; |
Ted Kremenek | dcee3ce | 2010-07-01 20:16:50 +0000 | [diff] [blame] | 302 | |
Ted Kremenek | b7ddd9b | 2009-11-13 01:54:21 +0000 | [diff] [blame] | 303 | case ErrorDeallocNotOwned: |
| 304 | Out << "-dealloc (not-owned)"; |
| 305 | break; |
Ted Kremenek | dcee3ce | 2010-07-01 20:16:50 +0000 | [diff] [blame] | 306 | |
Ted Kremenek | b7ddd9b | 2009-11-13 01:54:21 +0000 | [diff] [blame] | 307 | case ErrorLeak: |
| 308 | Out << "Leaked"; |
| 309 | break; |
Ted Kremenek | dcee3ce | 2010-07-01 20:16:50 +0000 | [diff] [blame] | 310 | |
Ted Kremenek | b7ddd9b | 2009-11-13 01:54:21 +0000 | [diff] [blame] | 311 | case ErrorLeakReturned: |
| 312 | Out << "Leaked (Bad naming)"; |
| 313 | break; |
Ted Kremenek | dcee3ce | 2010-07-01 20:16:50 +0000 | [diff] [blame] | 314 | |
Ted Kremenek | b7ddd9b | 2009-11-13 01:54:21 +0000 | [diff] [blame] | 315 | case ErrorGCLeakReturned: |
| 316 | Out << "Leaked (GC-ed at return)"; |
| 317 | break; |
Ted Kremenek | dcee3ce | 2010-07-01 20:16:50 +0000 | [diff] [blame] | 318 | |
Ted Kremenek | b7ddd9b | 2009-11-13 01:54:21 +0000 | [diff] [blame] | 319 | case ErrorUseAfterRelease: |
| 320 | Out << "Use-After-Release [ERROR]"; |
| 321 | break; |
Ted Kremenek | dcee3ce | 2010-07-01 20:16:50 +0000 | [diff] [blame] | 322 | |
Ted Kremenek | b7ddd9b | 2009-11-13 01:54:21 +0000 | [diff] [blame] | 323 | case ErrorReleaseNotOwned: |
| 324 | Out << "Release of Not-Owned [ERROR]"; |
| 325 | break; |
Ted Kremenek | dcee3ce | 2010-07-01 20:16:50 +0000 | [diff] [blame] | 326 | |
Ted Kremenek | b7ddd9b | 2009-11-13 01:54:21 +0000 | [diff] [blame] | 327 | case RefVal::ErrorOverAutorelease: |
| 328 | Out << "Over autoreleased"; |
| 329 | break; |
Ted Kremenek | dcee3ce | 2010-07-01 20:16:50 +0000 | [diff] [blame] | 330 | |
Ted Kremenek | b7ddd9b | 2009-11-13 01:54:21 +0000 | [diff] [blame] | 331 | case RefVal::ErrorReturnedNotOwned: |
| 332 | Out << "Non-owned object returned instead of owned"; |
| 333 | break; |
| 334 | } |
Ted Kremenek | dcee3ce | 2010-07-01 20:16:50 +0000 | [diff] [blame] | 335 | |
Ted Kremenek | b7ddd9b | 2009-11-13 01:54:21 +0000 | [diff] [blame] | 336 | if (ACnt) { |
| 337 | Out << " [ARC +" << ACnt << ']'; |
| 338 | } |
| 339 | } |
| 340 | } //end anonymous namespace |
| 341 | |
| 342 | //===----------------------------------------------------------------------===// |
| 343 | // RefBindings - State used to track object reference counts. |
| 344 | //===----------------------------------------------------------------------===// |
| 345 | |
Jordan Rose | 166d502 | 2012-11-02 01:54:06 +0000 | [diff] [blame] | 346 | REGISTER_MAP_WITH_PROGRAMSTATE(RefBindings, SymbolRef, RefVal) |
Ted Kremenek | b7ddd9b | 2009-11-13 01:54:21 +0000 | [diff] [blame] | 347 | |
Anna Zaks | 8d6b43c | 2012-08-14 00:36:15 +0000 | [diff] [blame] | 348 | static inline const RefVal *getRefBinding(ProgramStateRef State, |
| 349 | SymbolRef Sym) { |
| 350 | return State->get<RefBindings>(Sym); |
| 351 | } |
| 352 | |
| 353 | static inline ProgramStateRef setRefBinding(ProgramStateRef State, |
| 354 | SymbolRef Sym, RefVal Val) { |
| 355 | return State->set<RefBindings>(Sym, Val); |
| 356 | } |
| 357 | |
| 358 | static ProgramStateRef removeRefBinding(ProgramStateRef State, SymbolRef Sym) { |
| 359 | return State->remove<RefBindings>(Sym); |
| 360 | } |
| 361 | |
Ted Kremenek | b7ddd9b | 2009-11-13 01:54:21 +0000 | [diff] [blame] | 362 | //===----------------------------------------------------------------------===// |
Jordy Rose | 910c405 | 2011-09-02 06:44:22 +0000 | [diff] [blame] | 363 | // Function/Method behavior summaries. |
Ted Kremenek | b7ddd9b | 2009-11-13 01:54:21 +0000 | [diff] [blame] | 364 | //===----------------------------------------------------------------------===// |
| 365 | |
| 366 | namespace { |
Kovarththanan Rajaratnam | ba5fb5a | 2009-11-28 06:07:30 +0000 | [diff] [blame] | 367 | class RetainSummary { |
Jordy Rose | ef94588 | 2012-03-18 01:26:10 +0000 | [diff] [blame] | 368 | /// Args - a map of (index, ArgEffect) pairs, where index |
Ted Kremenek | 1bffd74 | 2008-05-06 15:44:25 +0000 | [diff] [blame] | 369 | /// specifies the argument (starting from 0). This can be sparsely |
| 370 | /// populated; arguments with no entry in Args use 'DefaultArgEffect'. |
Ted Kremenek | b77449c | 2009-05-03 05:20:50 +0000 | [diff] [blame] | 371 | ArgEffects Args; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 372 | |
Ted Kremenek | 1bffd74 | 2008-05-06 15:44:25 +0000 | [diff] [blame] | 373 | /// DefaultArgEffect - The default ArgEffect to apply to arguments that |
| 374 | /// do not have an entry in Args. |
Ted Kremenek | 0507f7e | 2012-01-04 00:35:45 +0000 | [diff] [blame] | 375 | ArgEffect DefaultArgEffect; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 376 | |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 377 | /// Receiver - If this summary applies to an Objective-C message expression, |
| 378 | /// this is the effect applied to the state of the receiver. |
Ted Kremenek | 0507f7e | 2012-01-04 00:35:45 +0000 | [diff] [blame] | 379 | ArgEffect Receiver; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 380 | |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 381 | /// Ret - The effect on the return value. Used to indicate if the |
Jordy Rose | 76c506f | 2011-08-21 21:58:18 +0000 | [diff] [blame] | 382 | /// function/method call returns a new tracked symbol. |
Ted Kremenek | 0507f7e | 2012-01-04 00:35:45 +0000 | [diff] [blame] | 383 | RetEffect Ret; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 384 | |
Ted Kremenek | 6b3a0f7 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 385 | public: |
Ted Kremenek | b77449c | 2009-05-03 05:20:50 +0000 | [diff] [blame] | 386 | RetainSummary(ArgEffects A, RetEffect R, ArgEffect defaultEff, |
Jordy Rose | e62e87b | 2011-08-20 20:55:40 +0000 | [diff] [blame] | 387 | ArgEffect ReceiverEff) |
| 388 | : Args(A), DefaultArgEffect(defaultEff), Receiver(ReceiverEff), Ret(R) {} |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 389 | |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 390 | /// getArg - Return the argument effect on the argument specified by |
| 391 | /// idx (starting from 0). |
Ted Kremenek | 1ac08d6 | 2008-03-11 17:48:22 +0000 | [diff] [blame] | 392 | ArgEffect getArg(unsigned idx) const { |
Ted Kremenek | b77449c | 2009-05-03 05:20:50 +0000 | [diff] [blame] | 393 | if (const ArgEffect *AE = Args.lookup(idx)) |
| 394 | return *AE; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 395 | |
Ted Kremenek | 1bffd74 | 2008-05-06 15:44:25 +0000 | [diff] [blame] | 396 | return DefaultArgEffect; |
Ted Kremenek | 1ac08d6 | 2008-03-11 17:48:22 +0000 | [diff] [blame] | 397 | } |
Ted Kremenek | 11fe175 | 2011-01-27 18:43:03 +0000 | [diff] [blame] | 398 | |
| 399 | void addArg(ArgEffects::Factory &af, unsigned idx, ArgEffect e) { |
| 400 | Args = af.add(Args, idx, e); |
| 401 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 402 | |
Ted Kremenek | 885c27b | 2009-05-04 05:31:22 +0000 | [diff] [blame] | 403 | /// setDefaultArgEffect - Set the default argument effect. |
| 404 | void setDefaultArgEffect(ArgEffect E) { |
| 405 | DefaultArgEffect = E; |
| 406 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 407 | |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 408 | /// getRetEffect - Returns the effect on the return value of the call. |
Ted Kremenek | b77449c | 2009-05-03 05:20:50 +0000 | [diff] [blame] | 409 | RetEffect getRetEffect() const { return Ret; } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 410 | |
Ted Kremenek | 885c27b | 2009-05-04 05:31:22 +0000 | [diff] [blame] | 411 | /// setRetEffect - Set the effect of the return value of the call. |
| 412 | void setRetEffect(RetEffect E) { Ret = E; } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 413 | |
Ted Kremenek | 12b9434 | 2011-01-27 06:54:14 +0000 | [diff] [blame] | 414 | |
| 415 | /// Sets the effect on the receiver of the message. |
| 416 | void setReceiverEffect(ArgEffect e) { Receiver = e; } |
| 417 | |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 418 | /// getReceiverEffect - Returns the effect on the receiver of the call. |
| 419 | /// This is only meaningful if the summary applies to an ObjCMessageExpr*. |
Ted Kremenek | b77449c | 2009-05-03 05:20:50 +0000 | [diff] [blame] | 420 | ArgEffect getReceiverEffect() const { return Receiver; } |
Jordy Rose | 4df54fe | 2011-08-23 04:27:15 +0000 | [diff] [blame] | 421 | |
| 422 | /// Test if two retain summaries are identical. Note that merely equivalent |
| 423 | /// summaries are not necessarily identical (for example, if an explicit |
| 424 | /// argument effect matches the default effect). |
| 425 | bool operator==(const RetainSummary &Other) const { |
| 426 | return Args == Other.Args && DefaultArgEffect == Other.DefaultArgEffect && |
| 427 | Receiver == Other.Receiver && Ret == Other.Ret; |
| 428 | } |
Jordy Rose | ef94588 | 2012-03-18 01:26:10 +0000 | [diff] [blame] | 429 | |
| 430 | /// Profile this summary for inclusion in a FoldingSet. |
| 431 | void Profile(llvm::FoldingSetNodeID& ID) const { |
| 432 | ID.Add(Args); |
| 433 | ID.Add(DefaultArgEffect); |
| 434 | ID.Add(Receiver); |
| 435 | ID.Add(Ret); |
| 436 | } |
| 437 | |
| 438 | /// A retain summary is simple if it has no ArgEffects other than the default. |
| 439 | bool isSimple() const { |
| 440 | return Args.isEmpty(); |
| 441 | } |
Jordan Rose | 4531b7d | 2012-07-02 19:27:43 +0000 | [diff] [blame] | 442 | |
| 443 | private: |
| 444 | ArgEffects getArgEffects() const { return Args; } |
| 445 | ArgEffect getDefaultArgEffect() const { return DefaultArgEffect; } |
| 446 | |
| 447 | friend class RetainSummaryManager; |
Ted Kremenek | 6b3a0f7 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 448 | }; |
Ted Kremenek | 4f22a78 | 2008-06-23 23:30:29 +0000 | [diff] [blame] | 449 | } // end anonymous namespace |
Ted Kremenek | 6b3a0f7 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 450 | |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 451 | //===----------------------------------------------------------------------===// |
| 452 | // Data structures for constructing summaries. |
| 453 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 53301ba | 2008-06-24 03:49:48 +0000 | [diff] [blame] | 454 | |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 455 | namespace { |
Kovarththanan Rajaratnam | ba5fb5a | 2009-11-28 06:07:30 +0000 | [diff] [blame] | 456 | class ObjCSummaryKey { |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 457 | IdentifierInfo* II; |
| 458 | Selector S; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 459 | public: |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 460 | ObjCSummaryKey(IdentifierInfo* ii, Selector s) |
| 461 | : II(ii), S(s) {} |
| 462 | |
Ted Kremenek | 9c378f7 | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 463 | ObjCSummaryKey(const ObjCInterfaceDecl *d, Selector s) |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 464 | : II(d ? d->getIdentifier() : 0), S(s) {} |
Ted Kremenek | 70b6a83 | 2009-05-13 18:16:01 +0000 | [diff] [blame] | 465 | |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 466 | ObjCSummaryKey(Selector s) |
| 467 | : II(0), S(s) {} |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 468 | |
Ted Kremenek | 0507f7e | 2012-01-04 00:35:45 +0000 | [diff] [blame] | 469 | IdentifierInfo *getIdentifier() const { return II; } |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 470 | Selector getSelector() const { return S; } |
| 471 | }; |
Ted Kremenek | 4f22a78 | 2008-06-23 23:30:29 +0000 | [diff] [blame] | 472 | } |
| 473 | |
| 474 | namespace llvm { |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 475 | template <> struct DenseMapInfo<ObjCSummaryKey> { |
| 476 | static inline ObjCSummaryKey getEmptyKey() { |
| 477 | return ObjCSummaryKey(DenseMapInfo<IdentifierInfo*>::getEmptyKey(), |
| 478 | DenseMapInfo<Selector>::getEmptyKey()); |
| 479 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 480 | |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 481 | static inline ObjCSummaryKey getTombstoneKey() { |
| 482 | return ObjCSummaryKey(DenseMapInfo<IdentifierInfo*>::getTombstoneKey(), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 483 | DenseMapInfo<Selector>::getTombstoneKey()); |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 484 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 485 | |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 486 | static unsigned getHashValue(const ObjCSummaryKey &V) { |
Benjamin Kramer | 28b2307 | 2012-05-27 13:28:44 +0000 | [diff] [blame] | 487 | typedef std::pair<IdentifierInfo*, Selector> PairTy; |
| 488 | return DenseMapInfo<PairTy>::getHashValue(PairTy(V.getIdentifier(), |
| 489 | V.getSelector())); |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 490 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 491 | |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 492 | static bool isEqual(const ObjCSummaryKey& LHS, const ObjCSummaryKey& RHS) { |
Benjamin Kramer | 28b2307 | 2012-05-27 13:28:44 +0000 | [diff] [blame] | 493 | return LHS.getIdentifier() == RHS.getIdentifier() && |
| 494 | LHS.getSelector() == RHS.getSelector(); |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 495 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 496 | |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 497 | }; |
Chris Lattner | 06159e8 | 2009-12-15 07:26:51 +0000 | [diff] [blame] | 498 | template <> |
| 499 | struct isPodLike<ObjCSummaryKey> { static const bool value = true; }; |
Ted Kremenek | 4f22a78 | 2008-06-23 23:30:29 +0000 | [diff] [blame] | 500 | } // end llvm namespace |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 501 | |
Ted Kremenek | 4f22a78 | 2008-06-23 23:30:29 +0000 | [diff] [blame] | 502 | namespace { |
Kovarththanan Rajaratnam | ba5fb5a | 2009-11-28 06:07:30 +0000 | [diff] [blame] | 503 | class ObjCSummaryCache { |
Ted Kremenek | 93edbc5 | 2011-10-05 23:54:29 +0000 | [diff] [blame] | 504 | typedef llvm::DenseMap<ObjCSummaryKey, const RetainSummary *> MapTy; |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 505 | MapTy M; |
| 506 | public: |
| 507 | ObjCSummaryCache() {} |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 508 | |
Ted Kremenek | 93edbc5 | 2011-10-05 23:54:29 +0000 | [diff] [blame] | 509 | const RetainSummary * find(const ObjCInterfaceDecl *D, Selector S) { |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 510 | // Do a lookup with the (D,S) pair. If we find a match return |
| 511 | // the iterator. |
| 512 | ObjCSummaryKey K(D, S); |
| 513 | MapTy::iterator I = M.find(K); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 514 | |
Jordan Rose | 4531b7d | 2012-07-02 19:27:43 +0000 | [diff] [blame] | 515 | if (I != M.end()) |
Ted Kremenek | 614cc54 | 2009-07-21 23:27:57 +0000 | [diff] [blame] | 516 | return I->second; |
Jordan Rose | 4531b7d | 2012-07-02 19:27:43 +0000 | [diff] [blame] | 517 | if (!D) |
| 518 | return NULL; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 519 | |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 520 | // Walk the super chain. If we find a hit with a parent, we'll end |
| 521 | // up returning that summary. We actually allow that key (null,S), as |
| 522 | // we cache summaries for the null ObjCInterfaceDecl* to allow us to |
| 523 | // generate initial summaries without having to worry about NSObject |
| 524 | // being declared. |
| 525 | // FIXME: We may change this at some point. |
Ted Kremenek | 9c378f7 | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 526 | for (ObjCInterfaceDecl *C=D->getSuperClass() ;; C=C->getSuperClass()) { |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 527 | if ((I = M.find(ObjCSummaryKey(C, S))) != M.end()) |
| 528 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 529 | |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 530 | if (!C) |
Ted Kremenek | 614cc54 | 2009-07-21 23:27:57 +0000 | [diff] [blame] | 531 | return NULL; |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 532 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 533 | |
| 534 | // Cache the summary with original key to make the next lookup faster |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 535 | // and return the iterator. |
Ted Kremenek | 93edbc5 | 2011-10-05 23:54:29 +0000 | [diff] [blame] | 536 | const RetainSummary *Summ = I->second; |
Ted Kremenek | 614cc54 | 2009-07-21 23:27:57 +0000 | [diff] [blame] | 537 | M[K] = Summ; |
| 538 | return Summ; |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 539 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 540 | |
Ted Kremenek | 0507f7e | 2012-01-04 00:35:45 +0000 | [diff] [blame] | 541 | const RetainSummary *find(IdentifierInfo* II, Selector S) { |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 542 | // FIXME: Class method lookup. Right now we dont' have a good way |
| 543 | // of going between IdentifierInfo* and the class hierarchy. |
Ted Kremenek | 614cc54 | 2009-07-21 23:27:57 +0000 | [diff] [blame] | 544 | MapTy::iterator I = M.find(ObjCSummaryKey(II, S)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 545 | |
Ted Kremenek | 614cc54 | 2009-07-21 23:27:57 +0000 | [diff] [blame] | 546 | if (I == M.end()) |
| 547 | I = M.find(ObjCSummaryKey(S)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 548 | |
Ted Kremenek | 614cc54 | 2009-07-21 23:27:57 +0000 | [diff] [blame] | 549 | return I == M.end() ? NULL : I->second; |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 550 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 551 | |
Ted Kremenek | 93edbc5 | 2011-10-05 23:54:29 +0000 | [diff] [blame] | 552 | const RetainSummary *& operator[](ObjCSummaryKey K) { |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 553 | return M[K]; |
| 554 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 555 | |
Ted Kremenek | 93edbc5 | 2011-10-05 23:54:29 +0000 | [diff] [blame] | 556 | const RetainSummary *& operator[](Selector S) { |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 557 | return M[ ObjCSummaryKey(S) ]; |
| 558 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 559 | }; |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 560 | } // end anonymous namespace |
| 561 | |
| 562 | //===----------------------------------------------------------------------===// |
| 563 | // Data structures for managing collections of summaries. |
| 564 | //===----------------------------------------------------------------------===// |
| 565 | |
| 566 | namespace { |
Kovarththanan Rajaratnam | ba5fb5a | 2009-11-28 06:07:30 +0000 | [diff] [blame] | 567 | class RetainSummaryManager { |
Ted Kremenek | d3dbcf4 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 568 | |
| 569 | //==-----------------------------------------------------------------==// |
| 570 | // Typedefs. |
| 571 | //==-----------------------------------------------------------------==// |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 572 | |
Ted Kremenek | 93edbc5 | 2011-10-05 23:54:29 +0000 | [diff] [blame] | 573 | typedef llvm::DenseMap<const FunctionDecl*, const RetainSummary *> |
Ted Kremenek | d3dbcf4 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 574 | FuncSummariesTy; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 575 | |
Ted Kremenek | 4f22a78 | 2008-06-23 23:30:29 +0000 | [diff] [blame] | 576 | typedef ObjCSummaryCache ObjCMethodSummariesTy; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 577 | |
Jordy Rose | ef94588 | 2012-03-18 01:26:10 +0000 | [diff] [blame] | 578 | typedef llvm::FoldingSetNodeWrapper<RetainSummary> CachedSummaryNode; |
| 579 | |
Ted Kremenek | d3dbcf4 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 580 | //==-----------------------------------------------------------------==// |
| 581 | // Data. |
| 582 | //==-----------------------------------------------------------------==// |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 583 | |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 584 | /// Ctx - The ASTContext object for the analyzed ASTs. |
Ted Kremenek | 9c378f7 | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 585 | ASTContext &Ctx; |
Ted Kremenek | 179064e | 2008-07-01 17:21:27 +0000 | [diff] [blame] | 586 | |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 587 | /// GCEnabled - Records whether or not the analyzed code runs in GC mode. |
Ted Kremenek | 377e230 | 2008-04-29 05:33:51 +0000 | [diff] [blame] | 588 | const bool GCEnabled; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 589 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 590 | /// Records whether or not the analyzed code runs in ARC mode. |
| 591 | const bool ARCEnabled; |
| 592 | |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 593 | /// FuncSummaries - A map from FunctionDecls to summaries. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 594 | FuncSummariesTy FuncSummaries; |
| 595 | |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 596 | /// ObjCClassMethodSummaries - A map from selectors (for instance methods) |
| 597 | /// to summaries. |
Ted Kremenek | 1f180c3 | 2008-06-23 22:21:20 +0000 | [diff] [blame] | 598 | ObjCMethodSummariesTy ObjCClassMethodSummaries; |
Ted Kremenek | d3dbcf4 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 599 | |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 600 | /// ObjCMethodSummaries - A map from selectors to summaries. |
Ted Kremenek | 1f180c3 | 2008-06-23 22:21:20 +0000 | [diff] [blame] | 601 | ObjCMethodSummariesTy ObjCMethodSummaries; |
Ted Kremenek | d3dbcf4 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 602 | |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 603 | /// BPAlloc - A BumpPtrAllocator used for allocating summaries, ArgEffects, |
| 604 | /// and all other data used by the checker. |
Ted Kremenek | d3dbcf4 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 605 | llvm::BumpPtrAllocator BPAlloc; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 606 | |
Ted Kremenek | b77449c | 2009-05-03 05:20:50 +0000 | [diff] [blame] | 607 | /// AF - A factory for ArgEffects objects. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 608 | ArgEffects::Factory AF; |
| 609 | |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 610 | /// ScratchArgs - A holding buffer for construct ArgEffects. |
Ted Kremenek | 0507f7e | 2012-01-04 00:35:45 +0000 | [diff] [blame] | 611 | ArgEffects ScratchArgs; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 612 | |
Ted Kremenek | ec31533 | 2009-05-07 23:40:42 +0000 | [diff] [blame] | 613 | /// ObjCAllocRetE - Default return effect for methods returning Objective-C |
| 614 | /// objects. |
| 615 | RetEffect ObjCAllocRetE; |
Ted Kremenek | 547d495 | 2009-06-05 23:18:01 +0000 | [diff] [blame] | 616 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 617 | /// ObjCInitRetE - Default return effect for init methods returning |
Ted Kremenek | ac02f20 | 2009-08-20 05:13:36 +0000 | [diff] [blame] | 618 | /// Objective-C objects. |
Ted Kremenek | 547d495 | 2009-06-05 23:18:01 +0000 | [diff] [blame] | 619 | RetEffect ObjCInitRetE; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 620 | |
Jordy Rose | ef94588 | 2012-03-18 01:26:10 +0000 | [diff] [blame] | 621 | /// SimpleSummaries - Used for uniquing summaries that don't have special |
| 622 | /// effects. |
| 623 | llvm::FoldingSet<CachedSummaryNode> SimpleSummaries; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 624 | |
Ted Kremenek | d3dbcf4 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 625 | //==-----------------------------------------------------------------==// |
| 626 | // Methods. |
| 627 | //==-----------------------------------------------------------------==// |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 628 | |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 629 | /// getArgEffects - Returns a persistent ArgEffects object based on the |
| 630 | /// data in ScratchArgs. |
Ted Kremenek | b77449c | 2009-05-03 05:20:50 +0000 | [diff] [blame] | 631 | ArgEffects getArgEffects(); |
Ted Kremenek | 6b3a0f7 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 632 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 633 | enum UnaryFuncKind { cfretain, cfrelease, cfmakecollectable }; |
Ted Kremenek | 93edbc5 | 2011-10-05 23:54:29 +0000 | [diff] [blame] | 634 | |
Ted Kremenek | 0507f7e | 2012-01-04 00:35:45 +0000 | [diff] [blame] | 635 | const RetainSummary *getUnarySummary(const FunctionType* FT, |
Ted Kremenek | 93edbc5 | 2011-10-05 23:54:29 +0000 | [diff] [blame] | 636 | UnaryFuncKind func); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 637 | |
Ted Kremenek | 0507f7e | 2012-01-04 00:35:45 +0000 | [diff] [blame] | 638 | const RetainSummary *getCFSummaryCreateRule(const FunctionDecl *FD); |
| 639 | const RetainSummary *getCFSummaryGetRule(const FunctionDecl *FD); |
| 640 | const RetainSummary *getCFCreateGetRuleSummary(const FunctionDecl *FD); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 641 | |
Jordy Rose | ef94588 | 2012-03-18 01:26:10 +0000 | [diff] [blame] | 642 | const RetainSummary *getPersistentSummary(const RetainSummary &OldSumm); |
Ted Kremenek | 706522f | 2008-10-29 04:07:07 +0000 | [diff] [blame] | 643 | |
Jordy Rose | ef94588 | 2012-03-18 01:26:10 +0000 | [diff] [blame] | 644 | const RetainSummary *getPersistentSummary(RetEffect RetEff, |
Ted Kremenek | 93edbc5 | 2011-10-05 23:54:29 +0000 | [diff] [blame] | 645 | ArgEffect ReceiverEff = DoNothing, |
| 646 | ArgEffect DefaultEff = MayEscape) { |
Jordy Rose | ef94588 | 2012-03-18 01:26:10 +0000 | [diff] [blame] | 647 | RetainSummary Summ(getArgEffects(), RetEff, DefaultEff, ReceiverEff); |
| 648 | return getPersistentSummary(Summ); |
| 649 | } |
| 650 | |
Ted Kremenek | c91fdf6 | 2012-05-08 00:12:09 +0000 | [diff] [blame] | 651 | const RetainSummary *getDoNothingSummary() { |
| 652 | return getPersistentSummary(RetEffect::MakeNoRet(), DoNothing, DoNothing); |
| 653 | } |
| 654 | |
Jordy Rose | ef94588 | 2012-03-18 01:26:10 +0000 | [diff] [blame] | 655 | const RetainSummary *getDefaultSummary() { |
| 656 | return getPersistentSummary(RetEffect::MakeNoRet(), |
| 657 | DoNothing, MayEscape); |
Ted Kremenek | 9c32d08 | 2008-05-06 00:30:21 +0000 | [diff] [blame] | 658 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 659 | |
Ted Kremenek | 93edbc5 | 2011-10-05 23:54:29 +0000 | [diff] [blame] | 660 | const RetainSummary *getPersistentStopSummary() { |
Jordy Rose | ef94588 | 2012-03-18 01:26:10 +0000 | [diff] [blame] | 661 | return getPersistentSummary(RetEffect::MakeNoRet(), |
| 662 | StopTracking, StopTracking); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 663 | } |
Ted Kremenek | b309525 | 2008-05-06 04:20:12 +0000 | [diff] [blame] | 664 | |
Ted Kremenek | 1f180c3 | 2008-06-23 22:21:20 +0000 | [diff] [blame] | 665 | void InitializeClassMethodSummaries(); |
| 666 | void InitializeMethodSummaries(); |
Ted Kremenek | 896cd9d | 2008-10-23 01:56:15 +0000 | [diff] [blame] | 667 | private: |
Ted Kremenek | 93edbc5 | 2011-10-05 23:54:29 +0000 | [diff] [blame] | 668 | void addNSObjectClsMethSummary(Selector S, const RetainSummary *Summ) { |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 669 | ObjCClassMethodSummaries[S] = Summ; |
| 670 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 671 | |
Ted Kremenek | 93edbc5 | 2011-10-05 23:54:29 +0000 | [diff] [blame] | 672 | void addNSObjectMethSummary(Selector S, const RetainSummary *Summ) { |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 673 | ObjCMethodSummaries[S] = Summ; |
| 674 | } |
Ted Kremenek | 3aa7ecd | 2009-03-04 23:30:42 +0000 | [diff] [blame] | 675 | |
Ted Kremenek | a979712 | 2012-02-18 21:37:48 +0000 | [diff] [blame] | 676 | void addClassMethSummary(const char* Cls, const char* name, |
| 677 | const RetainSummary *Summ, bool isNullary = true) { |
Ted Kremenek | 3aa7ecd | 2009-03-04 23:30:42 +0000 | [diff] [blame] | 678 | IdentifierInfo* ClsII = &Ctx.Idents.get(Cls); |
Ted Kremenek | a979712 | 2012-02-18 21:37:48 +0000 | [diff] [blame] | 679 | Selector S = isNullary ? GetNullarySelector(name, Ctx) |
| 680 | : GetUnarySelector(name, Ctx); |
Ted Kremenek | 3aa7ecd | 2009-03-04 23:30:42 +0000 | [diff] [blame] | 681 | ObjCClassMethodSummaries[ObjCSummaryKey(ClsII, S)] = Summ; |
| 682 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 683 | |
Ted Kremenek | 6c4becb | 2009-02-25 02:54:57 +0000 | [diff] [blame] | 684 | void addInstMethSummary(const char* Cls, const char* nullaryName, |
Ted Kremenek | 93edbc5 | 2011-10-05 23:54:29 +0000 | [diff] [blame] | 685 | const RetainSummary *Summ) { |
Ted Kremenek | 6c4becb | 2009-02-25 02:54:57 +0000 | [diff] [blame] | 686 | IdentifierInfo* ClsII = &Ctx.Idents.get(Cls); |
| 687 | Selector S = GetNullarySelector(nullaryName, Ctx); |
| 688 | ObjCMethodSummaries[ObjCSummaryKey(ClsII, S)] = Summ; |
| 689 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 690 | |
Ted Kremenek | de4d533 | 2009-04-24 17:50:11 +0000 | [diff] [blame] | 691 | Selector generateSelector(va_list argp) { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 692 | SmallVector<IdentifierInfo*, 10> II; |
Ted Kremenek | de4d533 | 2009-04-24 17:50:11 +0000 | [diff] [blame] | 693 | |
Ted Kremenek | 9e476de | 2008-08-12 18:30:56 +0000 | [diff] [blame] | 694 | while (const char* s = va_arg(argp, const char*)) |
| 695 | II.push_back(&Ctx.Idents.get(s)); |
Ted Kremenek | de4d533 | 2009-04-24 17:50:11 +0000 | [diff] [blame] | 696 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 697 | return Ctx.Selectors.getSelector(II.size(), &II[0]); |
Ted Kremenek | de4d533 | 2009-04-24 17:50:11 +0000 | [diff] [blame] | 698 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 699 | |
Ted Kremenek | de4d533 | 2009-04-24 17:50:11 +0000 | [diff] [blame] | 700 | void addMethodSummary(IdentifierInfo *ClsII, ObjCMethodSummariesTy& Summaries, |
Ted Kremenek | 93edbc5 | 2011-10-05 23:54:29 +0000 | [diff] [blame] | 701 | const RetainSummary * Summ, va_list argp) { |
Ted Kremenek | de4d533 | 2009-04-24 17:50:11 +0000 | [diff] [blame] | 702 | Selector S = generateSelector(argp); |
| 703 | Summaries[ObjCSummaryKey(ClsII, S)] = Summ; |
Ted Kremenek | 70a733e | 2008-07-18 17:24:20 +0000 | [diff] [blame] | 704 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 705 | |
Ted Kremenek | 93edbc5 | 2011-10-05 23:54:29 +0000 | [diff] [blame] | 706 | void addInstMethSummary(const char* Cls, const RetainSummary * Summ, ...) { |
Ted Kremenek | af9dc27 | 2008-08-12 18:48:50 +0000 | [diff] [blame] | 707 | va_list argp; |
| 708 | va_start(argp, Summ); |
Ted Kremenek | de4d533 | 2009-04-24 17:50:11 +0000 | [diff] [blame] | 709 | addMethodSummary(&Ctx.Idents.get(Cls), ObjCMethodSummaries, Summ, argp); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 710 | va_end(argp); |
Ted Kremenek | af9dc27 | 2008-08-12 18:48:50 +0000 | [diff] [blame] | 711 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 712 | |
Ted Kremenek | 93edbc5 | 2011-10-05 23:54:29 +0000 | [diff] [blame] | 713 | void addClsMethSummary(const char* Cls, const RetainSummary * Summ, ...) { |
Ted Kremenek | de4d533 | 2009-04-24 17:50:11 +0000 | [diff] [blame] | 714 | va_list argp; |
| 715 | va_start(argp, Summ); |
| 716 | addMethodSummary(&Ctx.Idents.get(Cls),ObjCClassMethodSummaries, Summ, argp); |
| 717 | va_end(argp); |
| 718 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 719 | |
Ted Kremenek | 93edbc5 | 2011-10-05 23:54:29 +0000 | [diff] [blame] | 720 | void addClsMethSummary(IdentifierInfo *II, const RetainSummary * Summ, ...) { |
Ted Kremenek | de4d533 | 2009-04-24 17:50:11 +0000 | [diff] [blame] | 721 | va_list argp; |
| 722 | va_start(argp, Summ); |
| 723 | addMethodSummary(II, ObjCClassMethodSummaries, Summ, argp); |
| 724 | va_end(argp); |
| 725 | } |
| 726 | |
Ted Kremenek | 6b3a0f7 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 727 | public: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 728 | |
Ted Kremenek | 9c378f7 | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 729 | RetainSummaryManager(ASTContext &ctx, bool gcenabled, bool usesARC) |
Ted Kremenek | 179064e | 2008-07-01 17:21:27 +0000 | [diff] [blame] | 730 | : Ctx(ctx), |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 731 | GCEnabled(gcenabled), |
| 732 | ARCEnabled(usesARC), |
| 733 | AF(BPAlloc), ScratchArgs(AF.getEmptyMap()), |
| 734 | ObjCAllocRetE(gcenabled |
| 735 | ? RetEffect::MakeGCNotOwned() |
| 736 | : (usesARC ? RetEffect::MakeARCNotOwned() |
| 737 | : RetEffect::MakeOwned(RetEffect::ObjC, true))), |
| 738 | ObjCInitRetE(gcenabled |
| 739 | ? RetEffect::MakeGCNotOwned() |
| 740 | : (usesARC ? RetEffect::MakeARCNotOwned() |
Jordy Rose | ef94588 | 2012-03-18 01:26:10 +0000 | [diff] [blame] | 741 | : RetEffect::MakeOwnedWhenTrackedReceiver())) { |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 742 | InitializeClassMethodSummaries(); |
| 743 | InitializeMethodSummaries(); |
| 744 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 745 | |
Jordan Rose | 4531b7d | 2012-07-02 19:27:43 +0000 | [diff] [blame] | 746 | const RetainSummary *getSummary(const CallEvent &Call, |
| 747 | ProgramStateRef State = 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 748 | |
Jordan Rose | 4531b7d | 2012-07-02 19:27:43 +0000 | [diff] [blame] | 749 | const RetainSummary *getFunctionSummary(const FunctionDecl *FD); |
| 750 | |
| 751 | const RetainSummary *getMethodSummary(Selector S, const ObjCInterfaceDecl *ID, |
Jordy Rose | f3aae58 | 2012-03-17 21:13:07 +0000 | [diff] [blame] | 752 | const ObjCMethodDecl *MD, |
| 753 | QualType RetTy, |
| 754 | ObjCMethodSummariesTy &CachedSummaries); |
| 755 | |
Jordan Rose | cde8cdb | 2012-07-02 19:27:56 +0000 | [diff] [blame] | 756 | const RetainSummary *getInstanceMethodSummary(const ObjCMethodCall &M, |
Jordan Rose | 4531b7d | 2012-07-02 19:27:43 +0000 | [diff] [blame] | 757 | ProgramStateRef State); |
Ted Kremenek | dcee3ce | 2010-07-01 20:16:50 +0000 | [diff] [blame] | 758 | |
Jordan Rose | cde8cdb | 2012-07-02 19:27:56 +0000 | [diff] [blame] | 759 | const RetainSummary *getClassMethodSummary(const ObjCMethodCall &M) { |
Jordan Rose | 4531b7d | 2012-07-02 19:27:43 +0000 | [diff] [blame] | 760 | assert(!M.isInstanceMessage()); |
| 761 | const ObjCInterfaceDecl *Class = M.getReceiverInterface(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 762 | |
Jordan Rose | 4531b7d | 2012-07-02 19:27:43 +0000 | [diff] [blame] | 763 | return getMethodSummary(M.getSelector(), Class, M.getDecl(), |
| 764 | M.getResultType(), ObjCClassMethodSummaries); |
Ted Kremenek | fcd7c6f | 2009-04-29 00:42:39 +0000 | [diff] [blame] | 765 | } |
Ted Kremenek | 552333c | 2009-04-29 17:17:48 +0000 | [diff] [blame] | 766 | |
| 767 | /// getMethodSummary - This version of getMethodSummary is used to query |
| 768 | /// the summary for the current method being analyzed. |
Ted Kremenek | 93edbc5 | 2011-10-05 23:54:29 +0000 | [diff] [blame] | 769 | const RetainSummary *getMethodSummary(const ObjCMethodDecl *MD) { |
Ted Kremenek | a883355 | 2009-04-29 23:03:22 +0000 | [diff] [blame] | 770 | const ObjCInterfaceDecl *ID = MD->getClassInterface(); |
Ted Kremenek | 70a6576 | 2009-04-30 05:41:14 +0000 | [diff] [blame] | 771 | Selector S = MD->getSelector(); |
Ted Kremenek | 552333c | 2009-04-29 17:17:48 +0000 | [diff] [blame] | 772 | QualType ResultTy = MD->getResultType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 773 | |
Jordy Rose | f3aae58 | 2012-03-17 21:13:07 +0000 | [diff] [blame] | 774 | ObjCMethodSummariesTy *CachedSummaries; |
Ted Kremenek | 552333c | 2009-04-29 17:17:48 +0000 | [diff] [blame] | 775 | if (MD->isInstanceMethod()) |
Jordy Rose | f3aae58 | 2012-03-17 21:13:07 +0000 | [diff] [blame] | 776 | CachedSummaries = &ObjCMethodSummaries; |
Ted Kremenek | 552333c | 2009-04-29 17:17:48 +0000 | [diff] [blame] | 777 | else |
Jordy Rose | f3aae58 | 2012-03-17 21:13:07 +0000 | [diff] [blame] | 778 | CachedSummaries = &ObjCClassMethodSummaries; |
| 779 | |
Jordan Rose | 4531b7d | 2012-07-02 19:27:43 +0000 | [diff] [blame] | 780 | return getMethodSummary(S, ID, MD, ResultTy, *CachedSummaries); |
Ted Kremenek | 552333c | 2009-04-29 17:17:48 +0000 | [diff] [blame] | 781 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 782 | |
Jordy Rose | f3aae58 | 2012-03-17 21:13:07 +0000 | [diff] [blame] | 783 | const RetainSummary *getStandardMethodSummary(const ObjCMethodDecl *MD, |
Jordan Rose | 4531b7d | 2012-07-02 19:27:43 +0000 | [diff] [blame] | 784 | Selector S, QualType RetTy); |
Ted Kremenek | a883355 | 2009-04-29 23:03:22 +0000 | [diff] [blame] | 785 | |
Ted Kremenek | 93edbc5 | 2011-10-05 23:54:29 +0000 | [diff] [blame] | 786 | void updateSummaryFromAnnotations(const RetainSummary *&Summ, |
Ted Kremenek | 4dd8fb4 | 2009-05-09 02:58:13 +0000 | [diff] [blame] | 787 | const ObjCMethodDecl *MD); |
| 788 | |
Ted Kremenek | 93edbc5 | 2011-10-05 23:54:29 +0000 | [diff] [blame] | 789 | void updateSummaryFromAnnotations(const RetainSummary *&Summ, |
Ted Kremenek | 4dd8fb4 | 2009-05-09 02:58:13 +0000 | [diff] [blame] | 790 | const FunctionDecl *FD); |
| 791 | |
Jordan Rose | 4531b7d | 2012-07-02 19:27:43 +0000 | [diff] [blame] | 792 | void updateSummaryForCall(const RetainSummary *&Summ, |
| 793 | const CallEvent &Call); |
| 794 | |
Ted Kremenek | d3dbcf4 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 795 | bool isGCEnabled() const { return GCEnabled; } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 796 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 797 | bool isARCEnabled() const { return ARCEnabled; } |
| 798 | |
| 799 | bool isARCorGCEnabled() const { return GCEnabled || ARCEnabled; } |
Jordan Rose | 4531b7d | 2012-07-02 19:27:43 +0000 | [diff] [blame] | 800 | |
| 801 | RetEffect getObjAllocRetEffect() const { return ObjCAllocRetE; } |
| 802 | |
| 803 | friend class RetainSummaryTemplate; |
Ted Kremenek | 6b3a0f7 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 804 | }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 805 | |
Jordy Rose | 0fe62f8 | 2011-08-24 09:02:37 +0000 | [diff] [blame] | 806 | // Used to avoid allocating long-term (BPAlloc'd) memory for default retain |
| 807 | // summaries. If a function or method looks like it has a default summary, but |
| 808 | // it has annotations, the annotations are added to the stack-based template |
| 809 | // and then copied into managed memory. |
| 810 | class RetainSummaryTemplate { |
| 811 | RetainSummaryManager &Manager; |
Ted Kremenek | 93edbc5 | 2011-10-05 23:54:29 +0000 | [diff] [blame] | 812 | const RetainSummary *&RealSummary; |
Jordy Rose | 0fe62f8 | 2011-08-24 09:02:37 +0000 | [diff] [blame] | 813 | RetainSummary ScratchSummary; |
| 814 | bool Accessed; |
| 815 | public: |
Jordan Rose | 4531b7d | 2012-07-02 19:27:43 +0000 | [diff] [blame] | 816 | RetainSummaryTemplate(const RetainSummary *&real, RetainSummaryManager &mgr) |
| 817 | : Manager(mgr), RealSummary(real), ScratchSummary(*real), Accessed(false) {} |
Jordy Rose | 0fe62f8 | 2011-08-24 09:02:37 +0000 | [diff] [blame] | 818 | |
| 819 | ~RetainSummaryTemplate() { |
Ted Kremenek | 93edbc5 | 2011-10-05 23:54:29 +0000 | [diff] [blame] | 820 | if (Accessed) |
Jordy Rose | ef94588 | 2012-03-18 01:26:10 +0000 | [diff] [blame] | 821 | RealSummary = Manager.getPersistentSummary(ScratchSummary); |
Jordy Rose | 0fe62f8 | 2011-08-24 09:02:37 +0000 | [diff] [blame] | 822 | } |
| 823 | |
| 824 | RetainSummary &operator*() { |
| 825 | Accessed = true; |
Ted Kremenek | 93edbc5 | 2011-10-05 23:54:29 +0000 | [diff] [blame] | 826 | return ScratchSummary; |
Jordy Rose | 0fe62f8 | 2011-08-24 09:02:37 +0000 | [diff] [blame] | 827 | } |
| 828 | |
| 829 | RetainSummary *operator->() { |
| 830 | Accessed = true; |
Ted Kremenek | 93edbc5 | 2011-10-05 23:54:29 +0000 | [diff] [blame] | 831 | return &ScratchSummary; |
Jordy Rose | 0fe62f8 | 2011-08-24 09:02:37 +0000 | [diff] [blame] | 832 | } |
| 833 | }; |
| 834 | |
Ted Kremenek | 6b3a0f7 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 835 | } // end anonymous namespace |
| 836 | |
| 837 | //===----------------------------------------------------------------------===// |
| 838 | // Implementation of checker data structures. |
| 839 | //===----------------------------------------------------------------------===// |
| 840 | |
Ted Kremenek | b77449c | 2009-05-03 05:20:50 +0000 | [diff] [blame] | 841 | ArgEffects RetainSummaryManager::getArgEffects() { |
| 842 | ArgEffects AE = ScratchArgs; |
Ted Kremenek | 3baf672 | 2010-11-24 00:54:37 +0000 | [diff] [blame] | 843 | ScratchArgs = AF.getEmptyMap(); |
Ted Kremenek | b77449c | 2009-05-03 05:20:50 +0000 | [diff] [blame] | 844 | return AE; |
Ted Kremenek | 00a3a5f | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 845 | } |
| 846 | |
Ted Kremenek | 93edbc5 | 2011-10-05 23:54:29 +0000 | [diff] [blame] | 847 | const RetainSummary * |
Jordy Rose | ef94588 | 2012-03-18 01:26:10 +0000 | [diff] [blame] | 848 | RetainSummaryManager::getPersistentSummary(const RetainSummary &OldSumm) { |
| 849 | // Unique "simple" summaries -- those without ArgEffects. |
| 850 | if (OldSumm.isSimple()) { |
| 851 | llvm::FoldingSetNodeID ID; |
| 852 | OldSumm.Profile(ID); |
| 853 | |
| 854 | void *Pos; |
| 855 | CachedSummaryNode *N = SimpleSummaries.FindNodeOrInsertPos(ID, Pos); |
| 856 | |
| 857 | if (!N) { |
| 858 | N = (CachedSummaryNode *) BPAlloc.Allocate<CachedSummaryNode>(); |
| 859 | new (N) CachedSummaryNode(OldSumm); |
| 860 | SimpleSummaries.InsertNode(N, Pos); |
| 861 | } |
| 862 | |
| 863 | return &N->getValue(); |
| 864 | } |
| 865 | |
Ted Kremenek | 93edbc5 | 2011-10-05 23:54:29 +0000 | [diff] [blame] | 866 | RetainSummary *Summ = (RetainSummary *) BPAlloc.Allocate<RetainSummary>(); |
Jordy Rose | ef94588 | 2012-03-18 01:26:10 +0000 | [diff] [blame] | 867 | new (Summ) RetainSummary(OldSumm); |
Ted Kremenek | 00a3a5f | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 868 | return Summ; |
| 869 | } |
| 870 | |
Ted Kremenek | d3dbcf4 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 871 | //===----------------------------------------------------------------------===// |
| 872 | // Summary creation for functions (largely uses of Core Foundation). |
| 873 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 00a3a5f | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 874 | |
Ted Kremenek | 9c378f7 | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 875 | static bool isRetain(const FunctionDecl *FD, StringRef FName) { |
Benjamin Kramer | b6f3c70 | 2010-02-08 18:38:55 +0000 | [diff] [blame] | 876 | return FName.endswith("Retain"); |
Ted Kremenek | 1261938 | 2009-01-12 21:45:02 +0000 | [diff] [blame] | 877 | } |
| 878 | |
Ted Kremenek | 9c378f7 | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 879 | static bool isRelease(const FunctionDecl *FD, StringRef FName) { |
Benjamin Kramer | b6f3c70 | 2010-02-08 18:38:55 +0000 | [diff] [blame] | 880 | return FName.endswith("Release"); |
Ted Kremenek | 1261938 | 2009-01-12 21:45:02 +0000 | [diff] [blame] | 881 | } |
| 882 | |
Jordy Rose | 76c506f | 2011-08-21 21:58:18 +0000 | [diff] [blame] | 883 | static bool isMakeCollectable(const FunctionDecl *FD, StringRef FName) { |
| 884 | // FIXME: Remove FunctionDecl parameter. |
| 885 | // FIXME: Is it really okay if MakeCollectable isn't a suffix? |
| 886 | return FName.find("MakeCollectable") != StringRef::npos; |
| 887 | } |
| 888 | |
Anna Zaks | 554067f | 2012-08-29 23:23:43 +0000 | [diff] [blame] | 889 | static ArgEffect getStopTrackingHardEquivalent(ArgEffect E) { |
Jordan Rose | 4531b7d | 2012-07-02 19:27:43 +0000 | [diff] [blame] | 890 | switch (E) { |
| 891 | case DoNothing: |
| 892 | case Autorelease: |
| 893 | case DecRefBridgedTransfered: |
| 894 | case IncRef: |
| 895 | case IncRefMsg: |
| 896 | case MakeCollectable: |
| 897 | case MayEscape: |
| 898 | case NewAutoreleasePool: |
| 899 | case StopTracking: |
Anna Zaks | 554067f | 2012-08-29 23:23:43 +0000 | [diff] [blame] | 900 | case StopTrackingHard: |
| 901 | return StopTrackingHard; |
Jordan Rose | 4531b7d | 2012-07-02 19:27:43 +0000 | [diff] [blame] | 902 | case DecRef: |
Anna Zaks | 554067f | 2012-08-29 23:23:43 +0000 | [diff] [blame] | 903 | case DecRefAndStopTrackingHard: |
| 904 | return DecRefAndStopTrackingHard; |
Jordan Rose | 4531b7d | 2012-07-02 19:27:43 +0000 | [diff] [blame] | 905 | case DecRefMsg: |
Anna Zaks | 554067f | 2012-08-29 23:23:43 +0000 | [diff] [blame] | 906 | case DecRefMsgAndStopTrackingHard: |
| 907 | return DecRefMsgAndStopTrackingHard; |
Jordan Rose | 4531b7d | 2012-07-02 19:27:43 +0000 | [diff] [blame] | 908 | case Dealloc: |
| 909 | return Dealloc; |
| 910 | } |
| 911 | |
| 912 | llvm_unreachable("Unknown ArgEffect kind"); |
| 913 | } |
| 914 | |
| 915 | void RetainSummaryManager::updateSummaryForCall(const RetainSummary *&S, |
| 916 | const CallEvent &Call) { |
| 917 | if (Call.hasNonZeroCallbackArg()) { |
Anna Zaks | 554067f | 2012-08-29 23:23:43 +0000 | [diff] [blame] | 918 | ArgEffect RecEffect = |
| 919 | getStopTrackingHardEquivalent(S->getReceiverEffect()); |
| 920 | ArgEffect DefEffect = |
| 921 | getStopTrackingHardEquivalent(S->getDefaultArgEffect()); |
Jordan Rose | 4531b7d | 2012-07-02 19:27:43 +0000 | [diff] [blame] | 922 | |
| 923 | ArgEffects CustomArgEffects = S->getArgEffects(); |
| 924 | for (ArgEffects::iterator I = CustomArgEffects.begin(), |
| 925 | E = CustomArgEffects.end(); |
| 926 | I != E; ++I) { |
Anna Zaks | 554067f | 2012-08-29 23:23:43 +0000 | [diff] [blame] | 927 | ArgEffect Translated = getStopTrackingHardEquivalent(I->second); |
Jordan Rose | 4531b7d | 2012-07-02 19:27:43 +0000 | [diff] [blame] | 928 | if (Translated != DefEffect) |
| 929 | ScratchArgs = AF.add(ScratchArgs, I->first, Translated); |
| 930 | } |
| 931 | |
Anna Zaks | 554067f | 2012-08-29 23:23:43 +0000 | [diff] [blame] | 932 | RetEffect RE = RetEffect::MakeNoRetHard(); |
Jordan Rose | 4531b7d | 2012-07-02 19:27:43 +0000 | [diff] [blame] | 933 | |
| 934 | // Special cases where the callback argument CANNOT free the return value. |
| 935 | // This can generally only happen if we know that the callback will only be |
| 936 | // called when the return value is already being deallocated. |
| 937 | if (const FunctionCall *FC = dyn_cast<FunctionCall>(&Call)) { |
Jordan Rose | 4a25f30 | 2012-09-01 17:39:13 +0000 | [diff] [blame] | 938 | if (IdentifierInfo *Name = FC->getDecl()->getIdentifier()) { |
| 939 | // When the CGBitmapContext is deallocated, the callback here will free |
| 940 | // the associated data buffer. |
Jordan Rose | a89f719 | 2012-08-31 18:19:18 +0000 | [diff] [blame] | 941 | if (Name->isStr("CGBitmapContextCreateWithData")) |
| 942 | RE = S->getRetEffect(); |
Jordan Rose | 4a25f30 | 2012-09-01 17:39:13 +0000 | [diff] [blame] | 943 | } |
Jordan Rose | 4531b7d | 2012-07-02 19:27:43 +0000 | [diff] [blame] | 944 | } |
| 945 | |
| 946 | S = getPersistentSummary(RE, RecEffect, DefEffect); |
| 947 | } |
Anna Zaks | 5a90193 | 2012-08-24 00:06:12 +0000 | [diff] [blame] | 948 | |
| 949 | // Special case '[super init];' and '[self init];' |
| 950 | // |
| 951 | // Even though calling '[super init]' without assigning the result to self |
| 952 | // and checking if the parent returns 'nil' is a bad pattern, it is common. |
| 953 | // Additionally, our Self Init checker already warns about it. To avoid |
| 954 | // overwhelming the user with messages from both checkers, we model the case |
| 955 | // of '[super init]' in cases when it is not consumed by another expression |
| 956 | // as if the call preserves the value of 'self'; essentially, assuming it can |
| 957 | // never fail and return 'nil'. |
| 958 | // Note, we don't want to just stop tracking the value since we want the |
| 959 | // RetainCount checker to report leaks and use-after-free if SelfInit checker |
| 960 | // is turned off. |
| 961 | if (const ObjCMethodCall *MC = dyn_cast<ObjCMethodCall>(&Call)) { |
| 962 | if (MC->getMethodFamily() == OMF_init && MC->isReceiverSelfOrSuper()) { |
| 963 | |
| 964 | // Check if the message is not consumed, we know it will not be used in |
| 965 | // an assignment, ex: "self = [super init]". |
| 966 | const Expr *ME = MC->getOriginExpr(); |
| 967 | const LocationContext *LCtx = MC->getLocationContext(); |
| 968 | ParentMap &PM = LCtx->getAnalysisDeclContext()->getParentMap(); |
| 969 | if (!PM.isConsumedExpr(ME)) { |
| 970 | RetainSummaryTemplate ModifiableSummaryTemplate(S, *this); |
| 971 | ModifiableSummaryTemplate->setReceiverEffect(DoNothing); |
| 972 | ModifiableSummaryTemplate->setRetEffect(RetEffect::MakeNoRet()); |
| 973 | } |
| 974 | } |
| 975 | |
| 976 | } |
Jordan Rose | 4531b7d | 2012-07-02 19:27:43 +0000 | [diff] [blame] | 977 | } |
| 978 | |
Anna Zaks | 58822c4 | 2012-05-04 22:18:39 +0000 | [diff] [blame] | 979 | const RetainSummary * |
Jordan Rose | 4531b7d | 2012-07-02 19:27:43 +0000 | [diff] [blame] | 980 | RetainSummaryManager::getSummary(const CallEvent &Call, |
| 981 | ProgramStateRef State) { |
| 982 | const RetainSummary *Summ; |
| 983 | switch (Call.getKind()) { |
| 984 | case CE_Function: |
| 985 | Summ = getFunctionSummary(cast<FunctionCall>(Call).getDecl()); |
| 986 | break; |
| 987 | case CE_CXXMember: |
Jordan Rose | fdaa338 | 2012-07-03 22:55:57 +0000 | [diff] [blame] | 988 | case CE_CXXMemberOperator: |
Jordan Rose | 4531b7d | 2012-07-02 19:27:43 +0000 | [diff] [blame] | 989 | case CE_Block: |
| 990 | case CE_CXXConstructor: |
Jordan Rose | 8d276d3 | 2012-07-10 22:07:47 +0000 | [diff] [blame] | 991 | case CE_CXXDestructor: |
Jordan Rose | 70cbf3c | 2012-07-02 22:21:47 +0000 | [diff] [blame] | 992 | case CE_CXXAllocator: |
Jordan Rose | 4531b7d | 2012-07-02 19:27:43 +0000 | [diff] [blame] | 993 | // FIXME: These calls are currently unsupported. |
| 994 | return getPersistentStopSummary(); |
Jordan Rose | 8919e68 | 2012-07-18 21:59:51 +0000 | [diff] [blame] | 995 | case CE_ObjCMessage: { |
Jordan Rose | cde8cdb | 2012-07-02 19:27:56 +0000 | [diff] [blame] | 996 | const ObjCMethodCall &Msg = cast<ObjCMethodCall>(Call); |
Jordan Rose | 4531b7d | 2012-07-02 19:27:43 +0000 | [diff] [blame] | 997 | if (Msg.isInstanceMessage()) |
| 998 | Summ = getInstanceMethodSummary(Msg, State); |
| 999 | else |
| 1000 | Summ = getClassMethodSummary(Msg); |
| 1001 | break; |
| 1002 | } |
| 1003 | } |
| 1004 | |
| 1005 | updateSummaryForCall(Summ, Call); |
| 1006 | |
| 1007 | assert(Summ && "Unknown call type?"); |
| 1008 | return Summ; |
| 1009 | } |
| 1010 | |
| 1011 | const RetainSummary * |
| 1012 | RetainSummaryManager::getFunctionSummary(const FunctionDecl *FD) { |
| 1013 | // If we don't know what function we're calling, use our default summary. |
| 1014 | if (!FD) |
| 1015 | return getDefaultSummary(); |
| 1016 | |
Ted Kremenek | 891d5cc | 2008-04-24 17:22:33 +0000 | [diff] [blame] | 1017 | // Look up a summary in our cache of FunctionDecls -> Summaries. |
Ted Kremenek | d3dbcf4 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 1018 | FuncSummariesTy::iterator I = FuncSummaries.find(FD); |
Ted Kremenek | d3dbcf4 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 1019 | if (I != FuncSummaries.end()) |
Ted Kremenek | 891d5cc | 2008-04-24 17:22:33 +0000 | [diff] [blame] | 1020 | return I->second; |
| 1021 | |
Ted Kremenek | e401a0c | 2009-05-04 15:34:07 +0000 | [diff] [blame] | 1022 | // No summary? Generate one. |
Ted Kremenek | 93edbc5 | 2011-10-05 23:54:29 +0000 | [diff] [blame] | 1023 | const RetainSummary *S = 0; |
Jordan Rose | 15d18e1 | 2012-08-06 21:28:02 +0000 | [diff] [blame] | 1024 | bool AllowAnnotations = true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1025 | |
Ted Kremenek | 37d785b | 2008-07-15 16:50:12 +0000 | [diff] [blame] | 1026 | do { |
Ted Kremenek | 1261938 | 2009-01-12 21:45:02 +0000 | [diff] [blame] | 1027 | // We generate "stop" summaries for implicitly defined functions. |
| 1028 | if (FD->isImplicit()) { |
| 1029 | S = getPersistentStopSummary(); |
| 1030 | break; |
Ted Kremenek | 37d785b | 2008-07-15 16:50:12 +0000 | [diff] [blame] | 1031 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1032 | |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1033 | // [PR 3337] Use 'getAs<FunctionType>' to strip away any typedefs on the |
Ted Kremenek | 9989065 | 2009-01-16 18:40:33 +0000 | [diff] [blame] | 1034 | // function's type. |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1035 | const FunctionType* FT = FD->getType()->getAs<FunctionType>(); |
Ted Kremenek | 48c6d18 | 2009-12-16 06:06:43 +0000 | [diff] [blame] | 1036 | const IdentifierInfo *II = FD->getIdentifier(); |
| 1037 | if (!II) |
| 1038 | break; |
Benjamin Kramer | b6f3c70 | 2010-02-08 18:38:55 +0000 | [diff] [blame] | 1039 | |
| 1040 | StringRef FName = II->getName(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1041 | |
Ted Kremenek | bf0a4dd | 2009-03-05 22:11:14 +0000 | [diff] [blame] | 1042 | // Strip away preceding '_'. Doing this here will effect all the checks |
| 1043 | // down below. |
Benjamin Kramer | b6f3c70 | 2010-02-08 18:38:55 +0000 | [diff] [blame] | 1044 | FName = FName.substr(FName.find_first_not_of('_')); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1045 | |
Ted Kremenek | 1261938 | 2009-01-12 21:45:02 +0000 | [diff] [blame] | 1046 | // Inspect the result type. |
| 1047 | QualType RetTy = FT->getResultType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1048 | |
Ted Kremenek | 1261938 | 2009-01-12 21:45:02 +0000 | [diff] [blame] | 1049 | // FIXME: This should all be refactored into a chain of "summary lookup" |
| 1050 | // filters. |
Ted Kremenek | 008636a | 2009-10-14 00:27:24 +0000 | [diff] [blame] | 1051 | assert(ScratchArgs.isEmpty()); |
Ted Kremenek | 39d88b0 | 2009-06-15 20:36:07 +0000 | [diff] [blame] | 1052 | |
Ted Kremenek | befc6d2 | 2012-04-26 04:32:23 +0000 | [diff] [blame] | 1053 | if (FName == "pthread_create" || FName == "pthread_setspecific") { |
| 1054 | // Part of: <rdar://problem/7299394> and <rdar://problem/11282706>. |
| 1055 | // This will be addressed better with IPA. |
Benjamin Kramer | b6f3c70 | 2010-02-08 18:38:55 +0000 | [diff] [blame] | 1056 | S = getPersistentStopSummary(); |
| 1057 | } else if (FName == "NSMakeCollectable") { |
| 1058 | // Handle: id NSMakeCollectable(CFTypeRef) |
| 1059 | S = (RetTy->isObjCIdType()) |
| 1060 | ? getUnarySummary(FT, cfmakecollectable) |
| 1061 | : getPersistentStopSummary(); |
Jordan Rose | 15d18e1 | 2012-08-06 21:28:02 +0000 | [diff] [blame] | 1062 | // The headers on OS X 10.8 use cf_consumed/ns_returns_retained, |
| 1063 | // but we can fully model NSMakeCollectable ourselves. |
| 1064 | AllowAnnotations = false; |
Ted Kremenek | 061707a | 2012-09-06 23:47:02 +0000 | [diff] [blame] | 1065 | } else if (FName == "CFPlugInInstanceCreate") { |
| 1066 | S = getPersistentSummary(RetEffect::MakeNoRet()); |
Benjamin Kramer | b6f3c70 | 2010-02-08 18:38:55 +0000 | [diff] [blame] | 1067 | } else if (FName == "IOBSDNameMatching" || |
| 1068 | FName == "IOServiceMatching" || |
| 1069 | FName == "IOServiceNameMatching" || |
Ted Kremenek | 537dd3a | 2012-05-01 05:28:27 +0000 | [diff] [blame] | 1070 | FName == "IORegistryEntrySearchCFProperty" || |
Benjamin Kramer | b6f3c70 | 2010-02-08 18:38:55 +0000 | [diff] [blame] | 1071 | FName == "IORegistryEntryIDMatching" || |
| 1072 | FName == "IOOpenFirmwarePathMatching") { |
| 1073 | // Part of <rdar://problem/6961230>. (IOKit) |
| 1074 | // This should be addressed using a API table. |
| 1075 | S = getPersistentSummary(RetEffect::MakeOwned(RetEffect::CF, true), |
| 1076 | DoNothing, DoNothing); |
| 1077 | } else if (FName == "IOServiceGetMatchingService" || |
| 1078 | FName == "IOServiceGetMatchingServices") { |
| 1079 | // FIXES: <rdar://problem/6326900> |
| 1080 | // This should be addressed using a API table. This strcmp is also |
| 1081 | // a little gross, but there is no need to super optimize here. |
Ted Kremenek | 3baf672 | 2010-11-24 00:54:37 +0000 | [diff] [blame] | 1082 | ScratchArgs = AF.add(ScratchArgs, 1, DecRef); |
Benjamin Kramer | b6f3c70 | 2010-02-08 18:38:55 +0000 | [diff] [blame] | 1083 | S = getPersistentSummary(RetEffect::MakeNoRet(), DoNothing, DoNothing); |
| 1084 | } else if (FName == "IOServiceAddNotification" || |
| 1085 | FName == "IOServiceAddMatchingNotification") { |
| 1086 | // Part of <rdar://problem/6961230>. (IOKit) |
| 1087 | // This should be addressed using a API table. |
Ted Kremenek | 3baf672 | 2010-11-24 00:54:37 +0000 | [diff] [blame] | 1088 | ScratchArgs = AF.add(ScratchArgs, 2, DecRef); |
Benjamin Kramer | b6f3c70 | 2010-02-08 18:38:55 +0000 | [diff] [blame] | 1089 | S = getPersistentSummary(RetEffect::MakeNoRet(), DoNothing, DoNothing); |
| 1090 | } else if (FName == "CVPixelBufferCreateWithBytes") { |
| 1091 | // FIXES: <rdar://problem/7283567> |
| 1092 | // Eventually this can be improved by recognizing that the pixel |
| 1093 | // buffer passed to CVPixelBufferCreateWithBytes is released via |
| 1094 | // a callback and doing full IPA to make sure this is done correctly. |
| 1095 | // FIXME: This function has an out parameter that returns an |
| 1096 | // allocated object. |
Ted Kremenek | 3baf672 | 2010-11-24 00:54:37 +0000 | [diff] [blame] | 1097 | ScratchArgs = AF.add(ScratchArgs, 7, StopTracking); |
Benjamin Kramer | b6f3c70 | 2010-02-08 18:38:55 +0000 | [diff] [blame] | 1098 | S = getPersistentSummary(RetEffect::MakeNoRet(), DoNothing, DoNothing); |
| 1099 | } else if (FName == "CGBitmapContextCreateWithData") { |
| 1100 | // FIXES: <rdar://problem/7358899> |
| 1101 | // Eventually this can be improved by recognizing that 'releaseInfo' |
| 1102 | // passed to CGBitmapContextCreateWithData is released via |
| 1103 | // a callback and doing full IPA to make sure this is done correctly. |
Ted Kremenek | 3baf672 | 2010-11-24 00:54:37 +0000 | [diff] [blame] | 1104 | ScratchArgs = AF.add(ScratchArgs, 8, StopTracking); |
Benjamin Kramer | b6f3c70 | 2010-02-08 18:38:55 +0000 | [diff] [blame] | 1105 | S = getPersistentSummary(RetEffect::MakeOwned(RetEffect::CF, true), |
| 1106 | DoNothing, DoNothing); |
| 1107 | } else if (FName == "CVPixelBufferCreateWithPlanarBytes") { |
| 1108 | // FIXES: <rdar://problem/7283567> |
| 1109 | // Eventually this can be improved by recognizing that the pixel |
| 1110 | // buffer passed to CVPixelBufferCreateWithPlanarBytes is released |
| 1111 | // via a callback and doing full IPA to make sure this is done |
| 1112 | // correctly. |
Ted Kremenek | 3baf672 | 2010-11-24 00:54:37 +0000 | [diff] [blame] | 1113 | ScratchArgs = AF.add(ScratchArgs, 12, StopTracking); |
Benjamin Kramer | b6f3c70 | 2010-02-08 18:38:55 +0000 | [diff] [blame] | 1114 | S = getPersistentSummary(RetEffect::MakeNoRet(), DoNothing, DoNothing); |
Ted Kremenek | 06911d4 | 2012-03-22 06:29:41 +0000 | [diff] [blame] | 1115 | } else if (FName == "dispatch_set_context") { |
| 1116 | // <rdar://problem/11059275> - The analyzer currently doesn't have |
| 1117 | // a good way to reason about the finalizer function for libdispatch. |
| 1118 | // If we pass a context object that is memory managed, stop tracking it. |
| 1119 | // FIXME: this hack should possibly go away once we can handle |
| 1120 | // libdispatch finalizers. |
| 1121 | ScratchArgs = AF.add(ScratchArgs, 1, StopTracking); |
| 1122 | S = getPersistentSummary(RetEffect::MakeNoRet(), DoNothing, DoNothing); |
Ted Kremenek | c91fdf6 | 2012-05-08 00:12:09 +0000 | [diff] [blame] | 1123 | } else if (FName.startswith("NSLog")) { |
| 1124 | S = getDoNothingSummary(); |
Anna Zaks | 62a5c34 | 2012-03-30 05:48:16 +0000 | [diff] [blame] | 1125 | } else if (FName.startswith("NS") && |
| 1126 | (FName.find("Insert") != StringRef::npos)) { |
| 1127 | // Whitelist NSXXInsertXX, for example NSMapInsertIfAbsent, since they can |
| 1128 | // be deallocated by NSMapRemove. (radar://11152419) |
| 1129 | ScratchArgs = AF.add(ScratchArgs, 1, StopTracking); |
| 1130 | ScratchArgs = AF.add(ScratchArgs, 2, StopTracking); |
| 1131 | S = getPersistentSummary(RetEffect::MakeNoRet(), DoNothing, DoNothing); |
Ted Kremenek | b04cb59 | 2009-06-11 18:17:24 +0000 | [diff] [blame] | 1132 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1133 | |
Ted Kremenek | b04cb59 | 2009-06-11 18:17:24 +0000 | [diff] [blame] | 1134 | // Did we get a summary? |
| 1135 | if (S) |
| 1136 | break; |
Ted Kremenek | 6199190 | 2009-03-17 22:43:44 +0000 | [diff] [blame] | 1137 | |
Ted Kremenek | 1261938 | 2009-01-12 21:45:02 +0000 | [diff] [blame] | 1138 | if (RetTy->isPointerType()) { |
Ted Kremenek | e788365 | 2012-08-30 19:27:02 +0000 | [diff] [blame] | 1139 | if (FD->getAttr<CFAuditedTransferAttr>()) { |
| 1140 | S = getCFCreateGetRuleSummary(FD); |
| 1141 | break; |
| 1142 | } |
| 1143 | |
Ted Kremenek | 1261938 | 2009-01-12 21:45:02 +0000 | [diff] [blame] | 1144 | // For CoreFoundation ('CF') types. |
Ted Kremenek | 78acdbf | 2010-01-27 18:00:17 +0000 | [diff] [blame] | 1145 | if (cocoa::isRefType(RetTy, "CF", FName)) { |
Ted Kremenek | 1261938 | 2009-01-12 21:45:02 +0000 | [diff] [blame] | 1146 | if (isRetain(FD, FName)) |
| 1147 | S = getUnarySummary(FT, cfretain); |
Jordy Rose | 76c506f | 2011-08-21 21:58:18 +0000 | [diff] [blame] | 1148 | else if (isMakeCollectable(FD, FName)) |
Ted Kremenek | 1261938 | 2009-01-12 21:45:02 +0000 | [diff] [blame] | 1149 | S = getUnarySummary(FT, cfmakecollectable); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1150 | else |
John McCall | 7df2ff4 | 2011-10-01 00:48:56 +0000 | [diff] [blame] | 1151 | S = getCFCreateGetRuleSummary(FD); |
Ted Kremenek | 1261938 | 2009-01-12 21:45:02 +0000 | [diff] [blame] | 1152 | |
| 1153 | break; |
| 1154 | } |
| 1155 | |
| 1156 | // For CoreGraphics ('CG') types. |
Ted Kremenek | 78acdbf | 2010-01-27 18:00:17 +0000 | [diff] [blame] | 1157 | if (cocoa::isRefType(RetTy, "CG", FName)) { |
Ted Kremenek | 1261938 | 2009-01-12 21:45:02 +0000 | [diff] [blame] | 1158 | if (isRetain(FD, FName)) |
| 1159 | S = getUnarySummary(FT, cfretain); |
| 1160 | else |
John McCall | 7df2ff4 | 2011-10-01 00:48:56 +0000 | [diff] [blame] | 1161 | S = getCFCreateGetRuleSummary(FD); |
Ted Kremenek | 1261938 | 2009-01-12 21:45:02 +0000 | [diff] [blame] | 1162 | |
| 1163 | break; |
| 1164 | } |
| 1165 | |
| 1166 | // For the Disk Arbitration API (DiskArbitration/DADisk.h) |
Ted Kremenek | 78acdbf | 2010-01-27 18:00:17 +0000 | [diff] [blame] | 1167 | if (cocoa::isRefType(RetTy, "DADisk") || |
| 1168 | cocoa::isRefType(RetTy, "DADissenter") || |
| 1169 | cocoa::isRefType(RetTy, "DASessionRef")) { |
John McCall | 7df2ff4 | 2011-10-01 00:48:56 +0000 | [diff] [blame] | 1170 | S = getCFCreateGetRuleSummary(FD); |
Ted Kremenek | 1261938 | 2009-01-12 21:45:02 +0000 | [diff] [blame] | 1171 | break; |
| 1172 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1173 | |
Ted Kremenek | 1261938 | 2009-01-12 21:45:02 +0000 | [diff] [blame] | 1174 | break; |
| 1175 | } |
| 1176 | |
| 1177 | // Check for release functions, the only kind of functions that we care |
| 1178 | // about that don't return a pointer type. |
| 1179 | if (FName[0] == 'C' && (FName[1] == 'F' || FName[1] == 'G')) { |
Ted Kremenek | e7d0312 | 2010-02-08 16:45:01 +0000 | [diff] [blame] | 1180 | // Test for 'CGCF'. |
Benjamin Kramer | b6f3c70 | 2010-02-08 18:38:55 +0000 | [diff] [blame] | 1181 | FName = FName.substr(FName.startswith("CGCF") ? 4 : 2); |
Ted Kremenek | e7d0312 | 2010-02-08 16:45:01 +0000 | [diff] [blame] | 1182 | |
Ted Kremenek | bf0a4dd | 2009-03-05 22:11:14 +0000 | [diff] [blame] | 1183 | if (isRelease(FD, FName)) |
Ted Kremenek | 1261938 | 2009-01-12 21:45:02 +0000 | [diff] [blame] | 1184 | S = getUnarySummary(FT, cfrelease); |
| 1185 | else { |
Ted Kremenek | b77449c | 2009-05-03 05:20:50 +0000 | [diff] [blame] | 1186 | assert (ScratchArgs.isEmpty()); |
Ted Kremenek | 6818928 | 2009-01-29 22:45:13 +0000 | [diff] [blame] | 1187 | // Remaining CoreFoundation and CoreGraphics functions. |
| 1188 | // We use to assume that they all strictly followed the ownership idiom |
| 1189 | // and that ownership cannot be transferred. While this is technically |
| 1190 | // correct, many methods allow a tracked object to escape. For example: |
| 1191 | // |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1192 | // CFMutableDictionaryRef x = CFDictionaryCreateMutable(...); |
Ted Kremenek | 6818928 | 2009-01-29 22:45:13 +0000 | [diff] [blame] | 1193 | // CFDictionaryAddValue(y, key, x); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1194 | // CFRelease(x); |
Ted Kremenek | 6818928 | 2009-01-29 22:45:13 +0000 | [diff] [blame] | 1195 | // ... it is okay to use 'x' since 'y' has a reference to it |
| 1196 | // |
| 1197 | // We handle this and similar cases with the follow heuristic. If the |
Ted Kremenek | c484381 | 2009-08-20 00:57:22 +0000 | [diff] [blame] | 1198 | // function name contains "InsertValue", "SetValue", "AddValue", |
| 1199 | // "AppendValue", or "SetAttribute", then we assume that arguments may |
| 1200 | // "escape." This means that something else holds on to the object, |
| 1201 | // allowing it be used even after its local retain count drops to 0. |
Benjamin Kramer | e45c149 | 2010-01-11 19:46:28 +0000 | [diff] [blame] | 1202 | ArgEffect E = (StrInStrNoCase(FName, "InsertValue") != StringRef::npos|| |
| 1203 | StrInStrNoCase(FName, "AddValue") != StringRef::npos || |
| 1204 | StrInStrNoCase(FName, "SetValue") != StringRef::npos || |
| 1205 | StrInStrNoCase(FName, "AppendValue") != StringRef::npos|| |
Benjamin Kramer | c027e54 | 2010-01-11 20:15:06 +0000 | [diff] [blame] | 1206 | StrInStrNoCase(FName, "SetAttribute") != StringRef::npos) |
Ted Kremenek | 6818928 | 2009-01-29 22:45:13 +0000 | [diff] [blame] | 1207 | ? MayEscape : DoNothing; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1208 | |
Ted Kremenek | 6818928 | 2009-01-29 22:45:13 +0000 | [diff] [blame] | 1209 | S = getPersistentSummary(RetEffect::MakeNoRet(), DoNothing, E); |
Ted Kremenek | 1261938 | 2009-01-12 21:45:02 +0000 | [diff] [blame] | 1210 | } |
| 1211 | } |
Ted Kremenek | 37d785b | 2008-07-15 16:50:12 +0000 | [diff] [blame] | 1212 | } |
| 1213 | while (0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1214 | |
Jordan Rose | 4531b7d | 2012-07-02 19:27:43 +0000 | [diff] [blame] | 1215 | // If we got all the way here without any luck, use a default summary. |
| 1216 | if (!S) |
| 1217 | S = getDefaultSummary(); |
| 1218 | |
Ted Kremenek | 4dd8fb4 | 2009-05-09 02:58:13 +0000 | [diff] [blame] | 1219 | // Annotations override defaults. |
Jordan Rose | 15d18e1 | 2012-08-06 21:28:02 +0000 | [diff] [blame] | 1220 | if (AllowAnnotations) |
| 1221 | updateSummaryFromAnnotations(S, FD); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1222 | |
Ted Kremenek | d3dbcf4 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 1223 | FuncSummaries[FD] = S; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1224 | return S; |
Ted Kremenek | 2fff37e | 2008-03-06 00:08:09 +0000 | [diff] [blame] | 1225 | } |
| 1226 | |
Ted Kremenek | 93edbc5 | 2011-10-05 23:54:29 +0000 | [diff] [blame] | 1227 | const RetainSummary * |
John McCall | 7df2ff4 | 2011-10-01 00:48:56 +0000 | [diff] [blame] | 1228 | RetainSummaryManager::getCFCreateGetRuleSummary(const FunctionDecl *FD) { |
| 1229 | if (coreFoundation::followsCreateRule(FD)) |
Ted Kremenek | 86ad3bc | 2008-05-05 16:51:50 +0000 | [diff] [blame] | 1230 | return getCFSummaryCreateRule(FD); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1231 | |
Ted Kremenek | d368d71 | 2011-05-25 06:19:45 +0000 | [diff] [blame] | 1232 | return getCFSummaryGetRule(FD); |
Ted Kremenek | 86ad3bc | 2008-05-05 16:51:50 +0000 | [diff] [blame] | 1233 | } |
| 1234 | |
Ted Kremenek | 93edbc5 | 2011-10-05 23:54:29 +0000 | [diff] [blame] | 1235 | const RetainSummary * |
Ted Kremenek | 6ad315a | 2009-02-23 16:51:39 +0000 | [diff] [blame] | 1236 | RetainSummaryManager::getUnarySummary(const FunctionType* FT, |
| 1237 | UnaryFuncKind func) { |
| 1238 | |
Ted Kremenek | 1261938 | 2009-01-12 21:45:02 +0000 | [diff] [blame] | 1239 | // Sanity check that this is *really* a unary function. This can |
| 1240 | // happen if people do weird things. |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1241 | const FunctionProtoType* FTP = dyn_cast<FunctionProtoType>(FT); |
Ted Kremenek | 1261938 | 2009-01-12 21:45:02 +0000 | [diff] [blame] | 1242 | if (!FTP || FTP->getNumArgs() != 1) |
| 1243 | return getPersistentStopSummary(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1244 | |
Ted Kremenek | b77449c | 2009-05-03 05:20:50 +0000 | [diff] [blame] | 1245 | assert (ScratchArgs.isEmpty()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1246 | |
Jordy Rose | 76c506f | 2011-08-21 21:58:18 +0000 | [diff] [blame] | 1247 | ArgEffect Effect; |
Ted Kremenek | 377e230 | 2008-04-29 05:33:51 +0000 | [diff] [blame] | 1248 | switch (func) { |
Jordy Rose | 76c506f | 2011-08-21 21:58:18 +0000 | [diff] [blame] | 1249 | case cfretain: Effect = IncRef; break; |
| 1250 | case cfrelease: Effect = DecRef; break; |
| 1251 | case cfmakecollectable: Effect = MakeCollectable; break; |
Ted Kremenek | 940b1d8 | 2008-04-10 23:44:06 +0000 | [diff] [blame] | 1252 | } |
Jordy Rose | 76c506f | 2011-08-21 21:58:18 +0000 | [diff] [blame] | 1253 | |
| 1254 | ScratchArgs = AF.add(ScratchArgs, 0, Effect); |
| 1255 | return getPersistentSummary(RetEffect::MakeNoRet(), DoNothing, DoNothing); |
Ted Kremenek | 00a3a5f | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 1256 | } |
| 1257 | |
Ted Kremenek | 93edbc5 | 2011-10-05 23:54:29 +0000 | [diff] [blame] | 1258 | const RetainSummary * |
Ted Kremenek | 9c378f7 | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 1259 | RetainSummaryManager::getCFSummaryCreateRule(const FunctionDecl *FD) { |
Ted Kremenek | b77449c | 2009-05-03 05:20:50 +0000 | [diff] [blame] | 1260 | assert (ScratchArgs.isEmpty()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1261 | |
Ted Kremenek | 2d1652e | 2009-01-28 05:56:51 +0000 | [diff] [blame] | 1262 | return getPersistentSummary(RetEffect::MakeOwned(RetEffect::CF, true)); |
Ted Kremenek | 00a3a5f | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 1263 | } |
| 1264 | |
Ted Kremenek | 93edbc5 | 2011-10-05 23:54:29 +0000 | [diff] [blame] | 1265 | const RetainSummary * |
Ted Kremenek | 9c378f7 | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 1266 | RetainSummaryManager::getCFSummaryGetRule(const FunctionDecl *FD) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1267 | assert (ScratchArgs.isEmpty()); |
Ted Kremenek | 2d1652e | 2009-01-28 05:56:51 +0000 | [diff] [blame] | 1268 | return getPersistentSummary(RetEffect::MakeNotOwned(RetEffect::CF), |
| 1269 | DoNothing, DoNothing); |
Ted Kremenek | 00a3a5f | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 1270 | } |
| 1271 | |
Ted Kremenek | 6b3a0f7 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 1272 | //===----------------------------------------------------------------------===// |
Ted Kremenek | d3dbcf4 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 1273 | // Summary creation for Selectors. |
| 1274 | //===----------------------------------------------------------------------===// |
| 1275 | |
Ted Kremenek | 4dd8fb4 | 2009-05-09 02:58:13 +0000 | [diff] [blame] | 1276 | void |
Ted Kremenek | 93edbc5 | 2011-10-05 23:54:29 +0000 | [diff] [blame] | 1277 | RetainSummaryManager::updateSummaryFromAnnotations(const RetainSummary *&Summ, |
Ted Kremenek | 4dd8fb4 | 2009-05-09 02:58:13 +0000 | [diff] [blame] | 1278 | const FunctionDecl *FD) { |
| 1279 | if (!FD) |
| 1280 | return; |
| 1281 | |
Jordan Rose | 4531b7d | 2012-07-02 19:27:43 +0000 | [diff] [blame] | 1282 | assert(Summ && "Must have a summary to add annotations to."); |
| 1283 | RetainSummaryTemplate Template(Summ, *this); |
Jordy Rose | 4df54fe | 2011-08-23 04:27:15 +0000 | [diff] [blame] | 1284 | |
Ted Kremenek | 11fe175 | 2011-01-27 18:43:03 +0000 | [diff] [blame] | 1285 | // Effects on the parameters. |
| 1286 | unsigned parm_idx = 0; |
| 1287 | for (FunctionDecl::param_const_iterator pi = FD->param_begin(), |
John McCall | 98b8f16 | 2011-04-06 09:02:12 +0000 | [diff] [blame] | 1288 | pe = FD->param_end(); pi != pe; ++pi, ++parm_idx) { |
Ted Kremenek | 11fe175 | 2011-01-27 18:43:03 +0000 | [diff] [blame] | 1289 | const ParmVarDecl *pd = *pi; |
| 1290 | if (pd->getAttr<NSConsumedAttr>()) { |
Jordy Rose | 4df54fe | 2011-08-23 04:27:15 +0000 | [diff] [blame] | 1291 | if (!GCEnabled) { |
Jordy Rose | 0fe62f8 | 2011-08-24 09:02:37 +0000 | [diff] [blame] | 1292 | Template->addArg(AF, parm_idx, DecRef); |
Jordy Rose | 4df54fe | 2011-08-23 04:27:15 +0000 | [diff] [blame] | 1293 | } |
| 1294 | } else if (pd->getAttr<CFConsumedAttr>()) { |
Jordy Rose | 0fe62f8 | 2011-08-24 09:02:37 +0000 | [diff] [blame] | 1295 | Template->addArg(AF, parm_idx, DecRef); |
Ted Kremenek | 11fe175 | 2011-01-27 18:43:03 +0000 | [diff] [blame] | 1296 | } |
| 1297 | } |
| 1298 | |
Ted Kremenek | b04cb59 | 2009-06-11 18:17:24 +0000 | [diff] [blame] | 1299 | QualType RetTy = FD->getResultType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1300 | |
Ted Kremenek | 4dd8fb4 | 2009-05-09 02:58:13 +0000 | [diff] [blame] | 1301 | // Determine if there is a special return effect for this method. |
Ted Kremenek | 78acdbf | 2010-01-27 18:00:17 +0000 | [diff] [blame] | 1302 | if (cocoa::isCocoaObjectRef(RetTy)) { |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 1303 | if (FD->getAttr<NSReturnsRetainedAttr>()) { |
Jordy Rose | 0fe62f8 | 2011-08-24 09:02:37 +0000 | [diff] [blame] | 1304 | Template->setRetEffect(ObjCAllocRetE); |
Ted Kremenek | 4dd8fb4 | 2009-05-09 02:58:13 +0000 | [diff] [blame] | 1305 | } |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 1306 | else if (FD->getAttr<CFReturnsRetainedAttr>()) { |
Jordy Rose | 0fe62f8 | 2011-08-24 09:02:37 +0000 | [diff] [blame] | 1307 | Template->setRetEffect(RetEffect::MakeOwned(RetEffect::CF, true)); |
Ted Kremenek | b04cb59 | 2009-06-11 18:17:24 +0000 | [diff] [blame] | 1308 | } |
Ted Kremenek | 6041111 | 2010-02-18 00:06:12 +0000 | [diff] [blame] | 1309 | else if (FD->getAttr<NSReturnsNotRetainedAttr>()) { |
Jordy Rose | 0fe62f8 | 2011-08-24 09:02:37 +0000 | [diff] [blame] | 1310 | Template->setRetEffect(RetEffect::MakeNotOwned(RetEffect::ObjC)); |
Ted Kremenek | 6041111 | 2010-02-18 00:06:12 +0000 | [diff] [blame] | 1311 | } |
| 1312 | else if (FD->getAttr<CFReturnsNotRetainedAttr>()) { |
Jordy Rose | 0fe62f8 | 2011-08-24 09:02:37 +0000 | [diff] [blame] | 1313 | Template->setRetEffect(RetEffect::MakeNotOwned(RetEffect::CF)); |
Jordy Rose | 4df54fe | 2011-08-23 04:27:15 +0000 | [diff] [blame] | 1314 | } |
| 1315 | } else if (RetTy->getAs<PointerType>()) { |
| 1316 | if (FD->getAttr<CFReturnsRetainedAttr>()) { |
Jordy Rose | 0fe62f8 | 2011-08-24 09:02:37 +0000 | [diff] [blame] | 1317 | Template->setRetEffect(RetEffect::MakeOwned(RetEffect::CF, true)); |
Jordy Rose | 4df54fe | 2011-08-23 04:27:15 +0000 | [diff] [blame] | 1318 | } |
| 1319 | else if (FD->getAttr<CFReturnsNotRetainedAttr>()) { |
Jordy Rose | 0fe62f8 | 2011-08-24 09:02:37 +0000 | [diff] [blame] | 1320 | Template->setRetEffect(RetEffect::MakeNotOwned(RetEffect::CF)); |
Ted Kremenek | 6041111 | 2010-02-18 00:06:12 +0000 | [diff] [blame] | 1321 | } |
Ted Kremenek | b04cb59 | 2009-06-11 18:17:24 +0000 | [diff] [blame] | 1322 | } |
Ted Kremenek | 4dd8fb4 | 2009-05-09 02:58:13 +0000 | [diff] [blame] | 1323 | } |
| 1324 | |
| 1325 | void |
Ted Kremenek | 93edbc5 | 2011-10-05 23:54:29 +0000 | [diff] [blame] | 1326 | RetainSummaryManager::updateSummaryFromAnnotations(const RetainSummary *&Summ, |
| 1327 | const ObjCMethodDecl *MD) { |
Ted Kremenek | 4dd8fb4 | 2009-05-09 02:58:13 +0000 | [diff] [blame] | 1328 | if (!MD) |
| 1329 | return; |
| 1330 | |
Jordan Rose | 4531b7d | 2012-07-02 19:27:43 +0000 | [diff] [blame] | 1331 | assert(Summ && "Must have a valid summary to add annotations to"); |
| 1332 | RetainSummaryTemplate Template(Summ, *this); |
Ted Kremenek | 6d4b76d | 2009-07-06 18:30:43 +0000 | [diff] [blame] | 1333 | bool isTrackedLoc = false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1334 | |
Ted Kremenek | 12b9434 | 2011-01-27 06:54:14 +0000 | [diff] [blame] | 1335 | // Effects on the receiver. |
| 1336 | if (MD->getAttr<NSConsumesSelfAttr>()) { |
Ted Kremenek | 11fe175 | 2011-01-27 18:43:03 +0000 | [diff] [blame] | 1337 | if (!GCEnabled) |
Jordy Rose | 0fe62f8 | 2011-08-24 09:02:37 +0000 | [diff] [blame] | 1338 | Template->setReceiverEffect(DecRefMsg); |
Ted Kremenek | 11fe175 | 2011-01-27 18:43:03 +0000 | [diff] [blame] | 1339 | } |
| 1340 | |
| 1341 | // Effects on the parameters. |
| 1342 | unsigned parm_idx = 0; |
Argyrios Kyrtzidis | 491306a | 2011-10-03 06:37:04 +0000 | [diff] [blame] | 1343 | for (ObjCMethodDecl::param_const_iterator |
| 1344 | pi=MD->param_begin(), pe=MD->param_end(); |
Ted Kremenek | 11fe175 | 2011-01-27 18:43:03 +0000 | [diff] [blame] | 1345 | pi != pe; ++pi, ++parm_idx) { |
| 1346 | const ParmVarDecl *pd = *pi; |
| 1347 | if (pd->getAttr<NSConsumedAttr>()) { |
| 1348 | if (!GCEnabled) |
Jordy Rose | 0fe62f8 | 2011-08-24 09:02:37 +0000 | [diff] [blame] | 1349 | Template->addArg(AF, parm_idx, DecRef); |
Ted Kremenek | 11fe175 | 2011-01-27 18:43:03 +0000 | [diff] [blame] | 1350 | } |
| 1351 | else if(pd->getAttr<CFConsumedAttr>()) { |
Jordy Rose | 0fe62f8 | 2011-08-24 09:02:37 +0000 | [diff] [blame] | 1352 | Template->addArg(AF, parm_idx, DecRef); |
Ted Kremenek | 11fe175 | 2011-01-27 18:43:03 +0000 | [diff] [blame] | 1353 | } |
Ted Kremenek | 12b9434 | 2011-01-27 06:54:14 +0000 | [diff] [blame] | 1354 | } |
| 1355 | |
Ted Kremenek | 4dd8fb4 | 2009-05-09 02:58:13 +0000 | [diff] [blame] | 1356 | // Determine if there is a special return effect for this method. |
Ted Kremenek | 78acdbf | 2010-01-27 18:00:17 +0000 | [diff] [blame] | 1357 | if (cocoa::isCocoaObjectRef(MD->getResultType())) { |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 1358 | if (MD->getAttr<NSReturnsRetainedAttr>()) { |
Jordy Rose | 0fe62f8 | 2011-08-24 09:02:37 +0000 | [diff] [blame] | 1359 | Template->setRetEffect(ObjCAllocRetE); |
Ted Kremenek | 6d4b76d | 2009-07-06 18:30:43 +0000 | [diff] [blame] | 1360 | return; |
Ted Kremenek | 4dd8fb4 | 2009-05-09 02:58:13 +0000 | [diff] [blame] | 1361 | } |
Ted Kremenek | 6041111 | 2010-02-18 00:06:12 +0000 | [diff] [blame] | 1362 | if (MD->getAttr<NSReturnsNotRetainedAttr>()) { |
Jordy Rose | 0fe62f8 | 2011-08-24 09:02:37 +0000 | [diff] [blame] | 1363 | Template->setRetEffect(RetEffect::MakeNotOwned(RetEffect::ObjC)); |
Ted Kremenek | 6041111 | 2010-02-18 00:06:12 +0000 | [diff] [blame] | 1364 | return; |
| 1365 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1366 | |
Ted Kremenek | 6d4b76d | 2009-07-06 18:30:43 +0000 | [diff] [blame] | 1367 | isTrackedLoc = true; |
Jordy Rose | 0fe62f8 | 2011-08-24 09:02:37 +0000 | [diff] [blame] | 1368 | } else { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1369 | isTrackedLoc = MD->getResultType()->getAs<PointerType>() != NULL; |
Jordy Rose | 0fe62f8 | 2011-08-24 09:02:37 +0000 | [diff] [blame] | 1370 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1371 | |
Ted Kremenek | 6041111 | 2010-02-18 00:06:12 +0000 | [diff] [blame] | 1372 | if (isTrackedLoc) { |
| 1373 | if (MD->getAttr<CFReturnsRetainedAttr>()) |
Jordy Rose | 0fe62f8 | 2011-08-24 09:02:37 +0000 | [diff] [blame] | 1374 | Template->setRetEffect(RetEffect::MakeOwned(RetEffect::CF, true)); |
Ted Kremenek | 6041111 | 2010-02-18 00:06:12 +0000 | [diff] [blame] | 1375 | else if (MD->getAttr<CFReturnsNotRetainedAttr>()) |
Jordy Rose | 0fe62f8 | 2011-08-24 09:02:37 +0000 | [diff] [blame] | 1376 | Template->setRetEffect(RetEffect::MakeNotOwned(RetEffect::CF)); |
Ted Kremenek | 6041111 | 2010-02-18 00:06:12 +0000 | [diff] [blame] | 1377 | } |
Ted Kremenek | 4dd8fb4 | 2009-05-09 02:58:13 +0000 | [diff] [blame] | 1378 | } |
| 1379 | |
Ted Kremenek | 93edbc5 | 2011-10-05 23:54:29 +0000 | [diff] [blame] | 1380 | const RetainSummary * |
Jordy Rose | f3aae58 | 2012-03-17 21:13:07 +0000 | [diff] [blame] | 1381 | RetainSummaryManager::getStandardMethodSummary(const ObjCMethodDecl *MD, |
| 1382 | Selector S, QualType RetTy) { |
Jordy Rose | e921b1a | 2012-03-17 19:53:04 +0000 | [diff] [blame] | 1383 | // Any special effects? |
Ted Kremenek | 8ee885b | 2009-04-24 21:56:17 +0000 | [diff] [blame] | 1384 | ArgEffect ReceiverEff = DoNothing; |
Jordy Rose | e921b1a | 2012-03-17 19:53:04 +0000 | [diff] [blame] | 1385 | RetEffect ResultEff = RetEffect::MakeNoRet(); |
| 1386 | |
| 1387 | // Check the method family, and apply any default annotations. |
| 1388 | switch (MD ? MD->getMethodFamily() : S.getMethodFamily()) { |
| 1389 | case OMF_None: |
| 1390 | case OMF_performSelector: |
| 1391 | // Assume all Objective-C methods follow Cocoa Memory Management rules. |
| 1392 | // FIXME: Does the non-threaded performSelector family really belong here? |
| 1393 | // The selector could be, say, @selector(copy). |
| 1394 | if (cocoa::isCocoaObjectRef(RetTy)) |
| 1395 | ResultEff = RetEffect::MakeNotOwned(RetEffect::ObjC); |
| 1396 | else if (coreFoundation::isCFObjectRef(RetTy)) { |
| 1397 | // ObjCMethodDecl currently doesn't consider CF objects as valid return |
| 1398 | // values for alloc, new, copy, or mutableCopy, so we have to |
| 1399 | // double-check with the selector. This is ugly, but there aren't that |
| 1400 | // many Objective-C methods that return CF objects, right? |
| 1401 | if (MD) { |
| 1402 | switch (S.getMethodFamily()) { |
| 1403 | case OMF_alloc: |
| 1404 | case OMF_new: |
| 1405 | case OMF_copy: |
| 1406 | case OMF_mutableCopy: |
| 1407 | ResultEff = RetEffect::MakeOwned(RetEffect::CF, true); |
| 1408 | break; |
| 1409 | default: |
| 1410 | ResultEff = RetEffect::MakeNotOwned(RetEffect::CF); |
| 1411 | break; |
| 1412 | } |
| 1413 | } else { |
| 1414 | ResultEff = RetEffect::MakeNotOwned(RetEffect::CF); |
| 1415 | } |
| 1416 | } |
| 1417 | break; |
| 1418 | case OMF_init: |
| 1419 | ResultEff = ObjCInitRetE; |
| 1420 | ReceiverEff = DecRefMsg; |
| 1421 | break; |
| 1422 | case OMF_alloc: |
| 1423 | case OMF_new: |
| 1424 | case OMF_copy: |
| 1425 | case OMF_mutableCopy: |
| 1426 | if (cocoa::isCocoaObjectRef(RetTy)) |
| 1427 | ResultEff = ObjCAllocRetE; |
| 1428 | else if (coreFoundation::isCFObjectRef(RetTy)) |
| 1429 | ResultEff = RetEffect::MakeOwned(RetEffect::CF, true); |
| 1430 | break; |
| 1431 | case OMF_autorelease: |
| 1432 | ReceiverEff = Autorelease; |
| 1433 | break; |
| 1434 | case OMF_retain: |
| 1435 | ReceiverEff = IncRefMsg; |
| 1436 | break; |
| 1437 | case OMF_release: |
| 1438 | ReceiverEff = DecRefMsg; |
| 1439 | break; |
| 1440 | case OMF_dealloc: |
| 1441 | ReceiverEff = Dealloc; |
| 1442 | break; |
| 1443 | case OMF_self: |
| 1444 | // -self is handled specially by the ExprEngine to propagate the receiver. |
| 1445 | break; |
| 1446 | case OMF_retainCount: |
| 1447 | case OMF_finalize: |
| 1448 | // These methods don't return objects. |
| 1449 | break; |
| 1450 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1451 | |
Ted Kremenek | 8ee885b | 2009-04-24 21:56:17 +0000 | [diff] [blame] | 1452 | // If one of the arguments in the selector has the keyword 'delegate' we |
| 1453 | // should stop tracking the reference count for the receiver. This is |
| 1454 | // because the reference count is quite possibly handled by a delegate |
| 1455 | // method. |
| 1456 | if (S.isKeywordSelector()) { |
Jordan Rose | 50571a9 | 2012-06-15 18:19:52 +0000 | [diff] [blame] | 1457 | for (unsigned i = 0, e = S.getNumArgs(); i != e; ++i) { |
| 1458 | StringRef Slot = S.getNameForSlot(i); |
| 1459 | if (Slot.substr(Slot.size() - 8).equals_lower("delegate")) { |
| 1460 | if (ResultEff == ObjCInitRetE) |
Anna Zaks | 554067f | 2012-08-29 23:23:43 +0000 | [diff] [blame] | 1461 | ResultEff = RetEffect::MakeNoRetHard(); |
Jordan Rose | 50571a9 | 2012-06-15 18:19:52 +0000 | [diff] [blame] | 1462 | else |
Anna Zaks | 554067f | 2012-08-29 23:23:43 +0000 | [diff] [blame] | 1463 | ReceiverEff = StopTrackingHard; |
Jordan Rose | 50571a9 | 2012-06-15 18:19:52 +0000 | [diff] [blame] | 1464 | } |
| 1465 | } |
Ted Kremenek | 8ee885b | 2009-04-24 21:56:17 +0000 | [diff] [blame] | 1466 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1467 | |
Jordy Rose | e921b1a | 2012-03-17 19:53:04 +0000 | [diff] [blame] | 1468 | if (ScratchArgs.isEmpty() && ReceiverEff == DoNothing && |
| 1469 | ResultEff.getKind() == RetEffect::NoRet) |
Ted Kremenek | 93edbc5 | 2011-10-05 23:54:29 +0000 | [diff] [blame] | 1470 | return getDefaultSummary(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1471 | |
Jordy Rose | e921b1a | 2012-03-17 19:53:04 +0000 | [diff] [blame] | 1472 | return getPersistentSummary(ResultEff, ReceiverEff, MayEscape); |
Ted Kremenek | 250b1fa | 2009-04-23 23:08:22 +0000 | [diff] [blame] | 1473 | } |
| 1474 | |
Ted Kremenek | 93edbc5 | 2011-10-05 23:54:29 +0000 | [diff] [blame] | 1475 | const RetainSummary * |
Jordan Rose | cde8cdb | 2012-07-02 19:27:56 +0000 | [diff] [blame] | 1476 | RetainSummaryManager::getInstanceMethodSummary(const ObjCMethodCall &Msg, |
Jordan Rose | 4531b7d | 2012-07-02 19:27:43 +0000 | [diff] [blame] | 1477 | ProgramStateRef State) { |
| 1478 | const ObjCInterfaceDecl *ReceiverClass = 0; |
Ted Kremenek | b7ddd9b | 2009-11-13 01:54:21 +0000 | [diff] [blame] | 1479 | |
Jordan Rose | 4531b7d | 2012-07-02 19:27:43 +0000 | [diff] [blame] | 1480 | // We do better tracking of the type of the object than the core ExprEngine. |
| 1481 | // See if we have its type in our private state. |
| 1482 | // FIXME: Eventually replace the use of state->get<RefBindings> with |
| 1483 | // a generic API for reasoning about the Objective-C types of symbolic |
| 1484 | // objects. |
| 1485 | SVal ReceiverV = Msg.getReceiverSVal(); |
| 1486 | if (SymbolRef Sym = ReceiverV.getAsLocSymbol()) |
Anna Zaks | 8d6b43c | 2012-08-14 00:36:15 +0000 | [diff] [blame] | 1487 | if (const RefVal *T = getRefBinding(State, Sym)) |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 1488 | if (const ObjCObjectPointerType *PT = |
Jordan Rose | 4531b7d | 2012-07-02 19:27:43 +0000 | [diff] [blame] | 1489 | T->getType()->getAs<ObjCObjectPointerType>()) |
| 1490 | ReceiverClass = PT->getInterfaceDecl(); |
| 1491 | |
| 1492 | // If we don't know what kind of object this is, fall back to its static type. |
| 1493 | if (!ReceiverClass) |
| 1494 | ReceiverClass = Msg.getReceiverInterface(); |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 1495 | |
Ted Kremenek | b7ddd9b | 2009-11-13 01:54:21 +0000 | [diff] [blame] | 1496 | // FIXME: The receiver could be a reference to a class, meaning that |
| 1497 | // we should use the class method. |
Jordan Rose | 4531b7d | 2012-07-02 19:27:43 +0000 | [diff] [blame] | 1498 | // id x = [NSObject class]; |
| 1499 | // [x performSelector:... withObject:... afterDelay:...]; |
| 1500 | Selector S = Msg.getSelector(); |
| 1501 | const ObjCMethodDecl *Method = Msg.getDecl(); |
| 1502 | if (!Method && ReceiverClass) |
| 1503 | Method = ReceiverClass->getInstanceMethod(S); |
| 1504 | |
| 1505 | return getMethodSummary(S, ReceiverClass, Method, Msg.getResultType(), |
| 1506 | ObjCMethodSummaries); |
Ted Kremenek | b7ddd9b | 2009-11-13 01:54:21 +0000 | [diff] [blame] | 1507 | } |
| 1508 | |
Ted Kremenek | 93edbc5 | 2011-10-05 23:54:29 +0000 | [diff] [blame] | 1509 | const RetainSummary * |
Jordan Rose | 4531b7d | 2012-07-02 19:27:43 +0000 | [diff] [blame] | 1510 | RetainSummaryManager::getMethodSummary(Selector S, const ObjCInterfaceDecl *ID, |
Jordy Rose | f3aae58 | 2012-03-17 21:13:07 +0000 | [diff] [blame] | 1511 | const ObjCMethodDecl *MD, QualType RetTy, |
| 1512 | ObjCMethodSummariesTy &CachedSummaries) { |
Ted Kremenek | 1bffd74 | 2008-05-06 15:44:25 +0000 | [diff] [blame] | 1513 | |
Ted Kremenek | 8711c03 | 2009-04-29 05:04:30 +0000 | [diff] [blame] | 1514 | // Look up a summary in our summary cache. |
Jordan Rose | 4531b7d | 2012-07-02 19:27:43 +0000 | [diff] [blame] | 1515 | const RetainSummary *Summ = CachedSummaries.find(ID, S); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1516 | |
Ted Kremenek | 614cc54 | 2009-07-21 23:27:57 +0000 | [diff] [blame] | 1517 | if (!Summ) { |
Jordy Rose | f3aae58 | 2012-03-17 21:13:07 +0000 | [diff] [blame] | 1518 | Summ = getStandardMethodSummary(MD, S, RetTy); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1519 | |
Ted Kremenek | 614cc54 | 2009-07-21 23:27:57 +0000 | [diff] [blame] | 1520 | // Annotations override defaults. |
Jordy Rose | 4df54fe | 2011-08-23 04:27:15 +0000 | [diff] [blame] | 1521 | updateSummaryFromAnnotations(Summ, MD); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1522 | |
Ted Kremenek | 614cc54 | 2009-07-21 23:27:57 +0000 | [diff] [blame] | 1523 | // Memoize the summary. |
Jordan Rose | 4531b7d | 2012-07-02 19:27:43 +0000 | [diff] [blame] | 1524 | CachedSummaries[ObjCSummaryKey(ID, S)] = Summ; |
Ted Kremenek | 614cc54 | 2009-07-21 23:27:57 +0000 | [diff] [blame] | 1525 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1526 | |
Ted Kremenek | e87450e | 2009-04-23 19:11:35 +0000 | [diff] [blame] | 1527 | return Summ; |
Ted Kremenek | c839560 | 2008-05-06 21:26:51 +0000 | [diff] [blame] | 1528 | } |
| 1529 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1530 | void RetainSummaryManager::InitializeClassMethodSummaries() { |
Ted Kremenek | ec31533 | 2009-05-07 23:40:42 +0000 | [diff] [blame] | 1531 | assert(ScratchArgs.isEmpty()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1532 | // Create the [NSAssertionHandler currentHander] summary. |
Ted Kremenek | 6fe2b7a | 2009-10-15 22:25:12 +0000 | [diff] [blame] | 1533 | addClassMethSummary("NSAssertionHandler", "currentHandler", |
Ted Kremenek | 2d1652e | 2009-01-28 05:56:51 +0000 | [diff] [blame] | 1534 | getPersistentSummary(RetEffect::MakeNotOwned(RetEffect::ObjC))); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1535 | |
Ted Kremenek | 6d34893 | 2008-10-21 15:53:15 +0000 | [diff] [blame] | 1536 | // Create the [NSAutoreleasePool addObject:] summary. |
Ted Kremenek | 3baf672 | 2010-11-24 00:54:37 +0000 | [diff] [blame] | 1537 | ScratchArgs = AF.add(ScratchArgs, 0, Autorelease); |
Ted Kremenek | 6fe2b7a | 2009-10-15 22:25:12 +0000 | [diff] [blame] | 1538 | addClassMethSummary("NSAutoreleasePool", "addObject", |
| 1539 | getPersistentSummary(RetEffect::MakeNoRet(), |
| 1540 | DoNothing, Autorelease)); |
Ted Kremenek | 9c32d08 | 2008-05-06 00:30:21 +0000 | [diff] [blame] | 1541 | } |
| 1542 | |
Ted Kremenek | 1f180c3 | 2008-06-23 22:21:20 +0000 | [diff] [blame] | 1543 | void RetainSummaryManager::InitializeMethodSummaries() { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1544 | |
| 1545 | assert (ScratchArgs.isEmpty()); |
| 1546 | |
Ted Kremenek | c839560 | 2008-05-06 21:26:51 +0000 | [diff] [blame] | 1547 | // Create the "init" selector. It just acts as a pass-through for the |
| 1548 | // receiver. |
Ted Kremenek | 93edbc5 | 2011-10-05 23:54:29 +0000 | [diff] [blame] | 1549 | const RetainSummary *InitSumm = getPersistentSummary(ObjCInitRetE, DecRefMsg); |
Ted Kremenek | ac02f20 | 2009-08-20 05:13:36 +0000 | [diff] [blame] | 1550 | addNSObjectMethSummary(GetNullarySelector("init", Ctx), InitSumm); |
| 1551 | |
| 1552 | // awakeAfterUsingCoder: behaves basically like an 'init' method. It |
| 1553 | // claims the receiver and returns a retained object. |
| 1554 | addNSObjectMethSummary(GetUnarySelector("awakeAfterUsingCoder", Ctx), |
| 1555 | InitSumm); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1556 | |
Ted Kremenek | c839560 | 2008-05-06 21:26:51 +0000 | [diff] [blame] | 1557 | // The next methods are allocators. |
Ted Kremenek | 93edbc5 | 2011-10-05 23:54:29 +0000 | [diff] [blame] | 1558 | const RetainSummary *AllocSumm = getPersistentSummary(ObjCAllocRetE); |
| 1559 | const RetainSummary *CFAllocSumm = |
Ted Kremenek | a834fb4 | 2009-08-28 19:52:12 +0000 | [diff] [blame] | 1560 | getPersistentSummary(RetEffect::MakeOwned(RetEffect::CF, true)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1561 | |
Ted Kremenek | 3c0cea3 | 2008-05-06 02:26:56 +0000 | [diff] [blame] | 1562 | // Create the "retain" selector. |
Jordy Rose | 500abad | 2011-08-21 19:41:36 +0000 | [diff] [blame] | 1563 | RetEffect NoRet = RetEffect::MakeNoRet(); |
Ted Kremenek | 93edbc5 | 2011-10-05 23:54:29 +0000 | [diff] [blame] | 1564 | const RetainSummary *Summ = getPersistentSummary(NoRet, IncRefMsg); |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 1565 | addNSObjectMethSummary(GetNullarySelector("retain", Ctx), Summ); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1566 | |
Ted Kremenek | 3c0cea3 | 2008-05-06 02:26:56 +0000 | [diff] [blame] | 1567 | // Create the "release" selector. |
Jordy Rose | 500abad | 2011-08-21 19:41:36 +0000 | [diff] [blame] | 1568 | Summ = getPersistentSummary(NoRet, DecRefMsg); |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 1569 | addNSObjectMethSummary(GetNullarySelector("release", Ctx), Summ); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1570 | |
Ted Kremenek | 299e815 | 2008-05-07 21:17:39 +0000 | [diff] [blame] | 1571 | // Create the "drain" selector. |
Jordy Rose | 500abad | 2011-08-21 19:41:36 +0000 | [diff] [blame] | 1572 | Summ = getPersistentSummary(NoRet, isGCEnabled() ? DoNothing : DecRef); |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 1573 | addNSObjectMethSummary(GetNullarySelector("drain", Ctx), Summ); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1574 | |
Ted Kremenek | f95e9fc | 2009-03-17 19:42:23 +0000 | [diff] [blame] | 1575 | // Create the -dealloc summary. |
Jordy Rose | 500abad | 2011-08-21 19:41:36 +0000 | [diff] [blame] | 1576 | Summ = getPersistentSummary(NoRet, Dealloc); |
Ted Kremenek | f95e9fc | 2009-03-17 19:42:23 +0000 | [diff] [blame] | 1577 | addNSObjectMethSummary(GetNullarySelector("dealloc", Ctx), Summ); |
Ted Kremenek | 3c0cea3 | 2008-05-06 02:26:56 +0000 | [diff] [blame] | 1578 | |
| 1579 | // Create the "autorelease" selector. |
Jordy Rose | 500abad | 2011-08-21 19:41:36 +0000 | [diff] [blame] | 1580 | Summ = getPersistentSummary(NoRet, Autorelease); |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 1581 | addNSObjectMethSummary(GetNullarySelector("autorelease", Ctx), Summ); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1582 | |
Ted Kremenek | f9a8e2e | 2009-02-23 17:45:03 +0000 | [diff] [blame] | 1583 | // Specially handle NSAutoreleasePool. |
Ted Kremenek | 6c4becb | 2009-02-25 02:54:57 +0000 | [diff] [blame] | 1584 | addInstMethSummary("NSAutoreleasePool", "init", |
Jordy Rose | 500abad | 2011-08-21 19:41:36 +0000 | [diff] [blame] | 1585 | getPersistentSummary(NoRet, NewAutoreleasePool)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1586 | |
| 1587 | // For NSWindow, allocated objects are (initially) self-owned. |
Ted Kremenek | 89e202d | 2009-02-23 02:51:29 +0000 | [diff] [blame] | 1588 | // FIXME: For now we opt for false negatives with NSWindow, as these objects |
| 1589 | // self-own themselves. However, they only do this once they are displayed. |
| 1590 | // Thus, we need to track an NSWindow's display status. |
| 1591 | // This is tracked in <rdar://problem/6062711>. |
Ted Kremenek | 3aa7ecd | 2009-03-04 23:30:42 +0000 | [diff] [blame] | 1592 | // See also http://llvm.org/bugs/show_bug.cgi?id=3714. |
Ted Kremenek | 93edbc5 | 2011-10-05 23:54:29 +0000 | [diff] [blame] | 1593 | const RetainSummary *NoTrackYet = getPersistentSummary(RetEffect::MakeNoRet(), |
Ted Kremenek | 78a35a3 | 2009-05-12 20:06:54 +0000 | [diff] [blame] | 1594 | StopTracking, |
| 1595 | StopTracking); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1596 | |
Ted Kremenek | 99d0269 | 2009-04-03 19:02:51 +0000 | [diff] [blame] | 1597 | addClassMethSummary("NSWindow", "alloc", NoTrackYet); |
| 1598 | |
Ted Kremenek | af9dc27 | 2008-08-12 18:48:50 +0000 | [diff] [blame] | 1599 | // For NSPanel (which subclasses NSWindow), allocated objects are not |
| 1600 | // self-owned. |
Ted Kremenek | 99d0269 | 2009-04-03 19:02:51 +0000 | [diff] [blame] | 1601 | // FIXME: For now we don't track NSPanels. object for the same reason |
| 1602 | // as for NSWindow objects. |
| 1603 | addClassMethSummary("NSPanel", "alloc", NoTrackYet); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1604 | |
Ted Kremenek | ba67f6a | 2009-05-18 23:14:34 +0000 | [diff] [blame] | 1605 | // Don't track allocated autorelease pools yet, as it is okay to prematurely |
| 1606 | // exit a method. |
| 1607 | addClassMethSummary("NSAutoreleasePool", "alloc", NoTrackYet); |
Ted Kremenek | a979712 | 2012-02-18 21:37:48 +0000 | [diff] [blame] | 1608 | addClassMethSummary("NSAutoreleasePool", "allocWithZone", NoTrackYet, false); |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 1609 | |
Ted Kremenek | 767d649 | 2009-05-20 22:39:57 +0000 | [diff] [blame] | 1610 | // Create summaries QCRenderer/QCView -createSnapShotImageOfType: |
| 1611 | addInstMethSummary("QCRenderer", AllocSumm, |
| 1612 | "createSnapshotImageOfType", NULL); |
| 1613 | addInstMethSummary("QCView", AllocSumm, |
| 1614 | "createSnapshotImageOfType", NULL); |
| 1615 | |
Ted Kremenek | 211a9c6 | 2009-06-15 20:58:58 +0000 | [diff] [blame] | 1616 | // Create summaries for CIContext, 'createCGImage' and |
Ted Kremenek | a834fb4 | 2009-08-28 19:52:12 +0000 | [diff] [blame] | 1617 | // 'createCGLayerWithSize'. These objects are CF objects, and are not |
| 1618 | // automatically garbage collected. |
| 1619 | addInstMethSummary("CIContext", CFAllocSumm, |
Ted Kremenek | 767d649 | 2009-05-20 22:39:57 +0000 | [diff] [blame] | 1620 | "createCGImage", "fromRect", NULL); |
Ted Kremenek | a834fb4 | 2009-08-28 19:52:12 +0000 | [diff] [blame] | 1621 | addInstMethSummary("CIContext", CFAllocSumm, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1622 | "createCGImage", "fromRect", "format", "colorSpace", NULL); |
Ted Kremenek | a834fb4 | 2009-08-28 19:52:12 +0000 | [diff] [blame] | 1623 | addInstMethSummary("CIContext", CFAllocSumm, "createCGLayerWithSize", |
Ted Kremenek | 211a9c6 | 2009-06-15 20:58:58 +0000 | [diff] [blame] | 1624 | "info", NULL); |
Ted Kremenek | b3c3c28 | 2008-05-06 00:38:54 +0000 | [diff] [blame] | 1625 | } |
| 1626 | |
Ted Kremenek | d3dbcf4 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 1627 | //===----------------------------------------------------------------------===// |
Ted Kremenek | c887d13 | 2009-04-29 18:50:19 +0000 | [diff] [blame] | 1628 | // Error reporting. |
| 1629 | //===----------------------------------------------------------------------===// |
Ted Kremenek | c887d13 | 2009-04-29 18:50:19 +0000 | [diff] [blame] | 1630 | namespace { |
Jordy Rose | ec9ef85 | 2011-08-23 20:55:48 +0000 | [diff] [blame] | 1631 | typedef llvm::DenseMap<const ExplodedNode *, const RetainSummary *> |
| 1632 | SummaryLogTy; |
| 1633 | |
Ted Kremenek | c887d13 | 2009-04-29 18:50:19 +0000 | [diff] [blame] | 1634 | //===-------------===// |
| 1635 | // Bug Descriptions. // |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1636 | //===-------------===// |
| 1637 | |
Kovarththanan Rajaratnam | ba5fb5a | 2009-11-28 06:07:30 +0000 | [diff] [blame] | 1638 | class CFRefBug : public BugType { |
Ted Kremenek | c887d13 | 2009-04-29 18:50:19 +0000 | [diff] [blame] | 1639 | protected: |
Jordy Rose | 35c8695 | 2011-08-24 05:47:39 +0000 | [diff] [blame] | 1640 | CFRefBug(StringRef name) |
Ted Kremenek | 6fd4505 | 2012-04-05 20:43:28 +0000 | [diff] [blame] | 1641 | : BugType(name, categories::MemoryCoreFoundationObjectiveC) {} |
Ted Kremenek | c887d13 | 2009-04-29 18:50:19 +0000 | [diff] [blame] | 1642 | public: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1643 | |
Ted Kremenek | c887d13 | 2009-04-29 18:50:19 +0000 | [diff] [blame] | 1644 | // FIXME: Eventually remove. |
Jordy Rose | 35c8695 | 2011-08-24 05:47:39 +0000 | [diff] [blame] | 1645 | virtual const char *getDescription() const = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1646 | |
Ted Kremenek | c887d13 | 2009-04-29 18:50:19 +0000 | [diff] [blame] | 1647 | virtual bool isLeak() const { return false; } |
| 1648 | }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1649 | |
Kovarththanan Rajaratnam | ba5fb5a | 2009-11-28 06:07:30 +0000 | [diff] [blame] | 1650 | class UseAfterRelease : public CFRefBug { |
Ted Kremenek | c887d13 | 2009-04-29 18:50:19 +0000 | [diff] [blame] | 1651 | public: |
Jordy Rose | 35c8695 | 2011-08-24 05:47:39 +0000 | [diff] [blame] | 1652 | UseAfterRelease() : CFRefBug("Use-after-release") {} |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1653 | |
Jordy Rose | 35c8695 | 2011-08-24 05:47:39 +0000 | [diff] [blame] | 1654 | const char *getDescription() const { |
Ted Kremenek | c887d13 | 2009-04-29 18:50:19 +0000 | [diff] [blame] | 1655 | return "Reference-counted object is used after it is released"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1656 | } |
Ted Kremenek | c887d13 | 2009-04-29 18:50:19 +0000 | [diff] [blame] | 1657 | }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1658 | |
Kovarththanan Rajaratnam | ba5fb5a | 2009-11-28 06:07:30 +0000 | [diff] [blame] | 1659 | class BadRelease : public CFRefBug { |
Ted Kremenek | c887d13 | 2009-04-29 18:50:19 +0000 | [diff] [blame] | 1660 | public: |
Jordy Rose | 35c8695 | 2011-08-24 05:47:39 +0000 | [diff] [blame] | 1661 | BadRelease() : CFRefBug("Bad release") {} |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1662 | |
Jordy Rose | 35c8695 | 2011-08-24 05:47:39 +0000 | [diff] [blame] | 1663 | const char *getDescription() const { |
Ted Kremenek | bb206fd | 2009-10-01 17:31:50 +0000 | [diff] [blame] | 1664 | return "Incorrect decrement of the reference count of an object that is " |
| 1665 | "not owned at this point by the caller"; |
Ted Kremenek | c887d13 | 2009-04-29 18:50:19 +0000 | [diff] [blame] | 1666 | } |
| 1667 | }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1668 | |
Kovarththanan Rajaratnam | ba5fb5a | 2009-11-28 06:07:30 +0000 | [diff] [blame] | 1669 | class DeallocGC : public CFRefBug { |
Ted Kremenek | c887d13 | 2009-04-29 18:50:19 +0000 | [diff] [blame] | 1670 | public: |
Jordy Rose | 35c8695 | 2011-08-24 05:47:39 +0000 | [diff] [blame] | 1671 | DeallocGC() |
| 1672 | : CFRefBug("-dealloc called while using garbage collection") {} |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1673 | |
Ted Kremenek | c887d13 | 2009-04-29 18:50:19 +0000 | [diff] [blame] | 1674 | const char *getDescription() const { |
Ted Kremenek | 369de56 | 2009-05-09 00:10:05 +0000 | [diff] [blame] | 1675 | return "-dealloc called while using garbage collection"; |
Ted Kremenek | c887d13 | 2009-04-29 18:50:19 +0000 | [diff] [blame] | 1676 | } |
| 1677 | }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1678 | |
Kovarththanan Rajaratnam | ba5fb5a | 2009-11-28 06:07:30 +0000 | [diff] [blame] | 1679 | class DeallocNotOwned : public CFRefBug { |
Ted Kremenek | c887d13 | 2009-04-29 18:50:19 +0000 | [diff] [blame] | 1680 | public: |
Jordy Rose | 35c8695 | 2011-08-24 05:47:39 +0000 | [diff] [blame] | 1681 | DeallocNotOwned() |
| 1682 | : CFRefBug("-dealloc sent to non-exclusively owned object") {} |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1683 | |
Ted Kremenek | c887d13 | 2009-04-29 18:50:19 +0000 | [diff] [blame] | 1684 | const char *getDescription() const { |
| 1685 | return "-dealloc sent to object that may be referenced elsewhere"; |
| 1686 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1687 | }; |
| 1688 | |
Kovarththanan Rajaratnam | ba5fb5a | 2009-11-28 06:07:30 +0000 | [diff] [blame] | 1689 | class OverAutorelease : public CFRefBug { |
Ted Kremenek | 369de56 | 2009-05-09 00:10:05 +0000 | [diff] [blame] | 1690 | public: |
Jordy Rose | 35c8695 | 2011-08-24 05:47:39 +0000 | [diff] [blame] | 1691 | OverAutorelease() |
| 1692 | : CFRefBug("Object sent -autorelease too many times") {} |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1693 | |
Ted Kremenek | 369de56 | 2009-05-09 00:10:05 +0000 | [diff] [blame] | 1694 | const char *getDescription() const { |
Ted Kremenek | eaedfea | 2009-05-10 05:11:21 +0000 | [diff] [blame] | 1695 | return "Object sent -autorelease too many times"; |
Ted Kremenek | 369de56 | 2009-05-09 00:10:05 +0000 | [diff] [blame] | 1696 | } |
| 1697 | }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1698 | |
Kovarththanan Rajaratnam | ba5fb5a | 2009-11-28 06:07:30 +0000 | [diff] [blame] | 1699 | class ReturnedNotOwnedForOwned : public CFRefBug { |
Ted Kremenek | e8720ce | 2009-05-10 06:25:57 +0000 | [diff] [blame] | 1700 | public: |
Jordy Rose | 35c8695 | 2011-08-24 05:47:39 +0000 | [diff] [blame] | 1701 | ReturnedNotOwnedForOwned() |
| 1702 | : CFRefBug("Method should return an owned object") {} |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1703 | |
Ted Kremenek | e8720ce | 2009-05-10 06:25:57 +0000 | [diff] [blame] | 1704 | const char *getDescription() const { |
Jordy Rose | 5b5402b | 2011-07-15 22:17:54 +0000 | [diff] [blame] | 1705 | return "Object with a +0 retain count returned to caller where a +1 " |
Ted Kremenek | e8720ce | 2009-05-10 06:25:57 +0000 | [diff] [blame] | 1706 | "(owning) retain count is expected"; |
| 1707 | } |
| 1708 | }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1709 | |
Kovarththanan Rajaratnam | ba5fb5a | 2009-11-28 06:07:30 +0000 | [diff] [blame] | 1710 | class Leak : public CFRefBug { |
Benjamin Kramer | facde17 | 2012-06-06 17:32:50 +0000 | [diff] [blame] | 1711 | public: |
| 1712 | Leak(StringRef name) |
| 1713 | : CFRefBug(name) { |
Jordy Rose | db92bb6 | 2011-08-25 01:14:38 +0000 | [diff] [blame] | 1714 | // Leaks should not be reported if they are post-dominated by a sink. |
| 1715 | setSuppressOnSink(true); |
| 1716 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1717 | |
Jordy Rose | 35c8695 | 2011-08-24 05:47:39 +0000 | [diff] [blame] | 1718 | const char *getDescription() const { return ""; } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1719 | |
Ted Kremenek | c887d13 | 2009-04-29 18:50:19 +0000 | [diff] [blame] | 1720 | bool isLeak() const { return true; } |
| 1721 | }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1722 | |
Ted Kremenek | c887d13 | 2009-04-29 18:50:19 +0000 | [diff] [blame] | 1723 | //===---------===// |
| 1724 | // Bug Reports. // |
| 1725 | //===---------===// |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1726 | |
Jordy Rose | 0115349 | 2012-03-24 02:45:35 +0000 | [diff] [blame] | 1727 | class CFRefReportVisitor : public BugReporterVisitorImpl<CFRefReportVisitor> { |
Anna Zaks | 23f395e | 2011-08-20 01:27:22 +0000 | [diff] [blame] | 1728 | protected: |
Anna Zaks | dc757b0 | 2011-08-19 23:21:56 +0000 | [diff] [blame] | 1729 | SymbolRef Sym; |
Jordy Rose | ec9ef85 | 2011-08-23 20:55:48 +0000 | [diff] [blame] | 1730 | const SummaryLogTy &SummaryLog; |
Jordy Rose | 35c8695 | 2011-08-24 05:47:39 +0000 | [diff] [blame] | 1731 | bool GCEnabled; |
Anna Zaks | 23f395e | 2011-08-20 01:27:22 +0000 | [diff] [blame] | 1732 | |
Anna Zaks | dc757b0 | 2011-08-19 23:21:56 +0000 | [diff] [blame] | 1733 | public: |
Jordy Rose | 35c8695 | 2011-08-24 05:47:39 +0000 | [diff] [blame] | 1734 | CFRefReportVisitor(SymbolRef sym, bool gcEnabled, const SummaryLogTy &log) |
| 1735 | : Sym(sym), SummaryLog(log), GCEnabled(gcEnabled) {} |
Anna Zaks | dc757b0 | 2011-08-19 23:21:56 +0000 | [diff] [blame] | 1736 | |
Anna Zaks | 23f395e | 2011-08-20 01:27:22 +0000 | [diff] [blame] | 1737 | virtual void Profile(llvm::FoldingSetNodeID &ID) const { |
Anna Zaks | dc757b0 | 2011-08-19 23:21:56 +0000 | [diff] [blame] | 1738 | static int x = 0; |
| 1739 | ID.AddPointer(&x); |
| 1740 | ID.AddPointer(Sym); |
| 1741 | } |
| 1742 | |
Anna Zaks | 23f395e | 2011-08-20 01:27:22 +0000 | [diff] [blame] | 1743 | virtual PathDiagnosticPiece *VisitNode(const ExplodedNode *N, |
| 1744 | const ExplodedNode *PrevN, |
| 1745 | BugReporterContext &BRC, |
| 1746 | BugReport &BR); |
| 1747 | |
| 1748 | virtual PathDiagnosticPiece *getEndPath(BugReporterContext &BRC, |
| 1749 | const ExplodedNode *N, |
| 1750 | BugReport &BR); |
| 1751 | }; |
| 1752 | |
| 1753 | class CFRefLeakReportVisitor : public CFRefReportVisitor { |
| 1754 | public: |
Jordy Rose | 35c8695 | 2011-08-24 05:47:39 +0000 | [diff] [blame] | 1755 | CFRefLeakReportVisitor(SymbolRef sym, bool GCEnabled, |
Jordy Rose | ec9ef85 | 2011-08-23 20:55:48 +0000 | [diff] [blame] | 1756 | const SummaryLogTy &log) |
Jordy Rose | 35c8695 | 2011-08-24 05:47:39 +0000 | [diff] [blame] | 1757 | : CFRefReportVisitor(sym, GCEnabled, log) {} |
Anna Zaks | 23f395e | 2011-08-20 01:27:22 +0000 | [diff] [blame] | 1758 | |
| 1759 | PathDiagnosticPiece *getEndPath(BugReporterContext &BRC, |
| 1760 | const ExplodedNode *N, |
| 1761 | BugReport &BR); |
Jordy Rose | 0115349 | 2012-03-24 02:45:35 +0000 | [diff] [blame] | 1762 | |
| 1763 | virtual BugReporterVisitor *clone() const { |
| 1764 | // The curiously-recurring template pattern only works for one level of |
| 1765 | // subclassing. Rather than make a new template base for |
| 1766 | // CFRefReportVisitor, we simply override clone() to do the right thing. |
| 1767 | // This could be trouble someday if BugReporterVisitorImpl is ever |
| 1768 | // used for something else besides a convenient implementation of clone(). |
| 1769 | return new CFRefLeakReportVisitor(*this); |
| 1770 | } |
Anna Zaks | dc757b0 | 2011-08-19 23:21:56 +0000 | [diff] [blame] | 1771 | }; |
| 1772 | |
Anna Zaks | e172e8b | 2011-08-17 23:00:25 +0000 | [diff] [blame] | 1773 | class CFRefReport : public BugReport { |
Jordy Rose | 2058956 | 2011-08-24 22:39:09 +0000 | [diff] [blame] | 1774 | void addGCModeDescription(const LangOptions &LOpts, bool GCEnabled); |
Jordy Rose | 35c8695 | 2011-08-24 05:47:39 +0000 | [diff] [blame] | 1775 | |
Ted Kremenek | c887d13 | 2009-04-29 18:50:19 +0000 | [diff] [blame] | 1776 | public: |
Jordy Rose | 2058956 | 2011-08-24 22:39:09 +0000 | [diff] [blame] | 1777 | CFRefReport(CFRefBug &D, const LangOptions &LOpts, bool GCEnabled, |
| 1778 | const SummaryLogTy &Log, ExplodedNode *n, SymbolRef sym, |
| 1779 | bool registerVisitor = true) |
Anna Zaks | edf4dae | 2011-08-22 18:54:07 +0000 | [diff] [blame] | 1780 | : BugReport(D, D.getDescription(), n) { |
Anna Zaks | 23f395e | 2011-08-20 01:27:22 +0000 | [diff] [blame] | 1781 | if (registerVisitor) |
Jordy Rose | 2058956 | 2011-08-24 22:39:09 +0000 | [diff] [blame] | 1782 | addVisitor(new CFRefReportVisitor(sym, GCEnabled, Log)); |
| 1783 | addGCModeDescription(LOpts, GCEnabled); |
Anna Zaks | dc757b0 | 2011-08-19 23:21:56 +0000 | [diff] [blame] | 1784 | } |
Ted Kremenek | eaedfea | 2009-05-10 05:11:21 +0000 | [diff] [blame] | 1785 | |
Jordy Rose | 2058956 | 2011-08-24 22:39:09 +0000 | [diff] [blame] | 1786 | CFRefReport(CFRefBug &D, const LangOptions &LOpts, bool GCEnabled, |
| 1787 | const SummaryLogTy &Log, ExplodedNode *n, SymbolRef sym, |
| 1788 | StringRef endText) |
Anna Zaks | edf4dae | 2011-08-22 18:54:07 +0000 | [diff] [blame] | 1789 | : BugReport(D, D.getDescription(), endText, n) { |
Jordy Rose | 2058956 | 2011-08-24 22:39:09 +0000 | [diff] [blame] | 1790 | addVisitor(new CFRefReportVisitor(sym, GCEnabled, Log)); |
| 1791 | addGCModeDescription(LOpts, GCEnabled); |
Anna Zaks | dc757b0 | 2011-08-19 23:21:56 +0000 | [diff] [blame] | 1792 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1793 | |
Anna Zaks | e172e8b | 2011-08-17 23:00:25 +0000 | [diff] [blame] | 1794 | virtual std::pair<ranges_iterator, ranges_iterator> getRanges() { |
Anna Zaks | edf4dae | 2011-08-22 18:54:07 +0000 | [diff] [blame] | 1795 | const CFRefBug& BugTy = static_cast<CFRefBug&>(getBugType()); |
| 1796 | if (!BugTy.isLeak()) |
Anna Zaks | e172e8b | 2011-08-17 23:00:25 +0000 | [diff] [blame] | 1797 | return BugReport::getRanges(); |
Ted Kremenek | c887d13 | 2009-04-29 18:50:19 +0000 | [diff] [blame] | 1798 | else |
Argyrios Kyrtzidis | 640ccf0 | 2010-12-04 01:12:15 +0000 | [diff] [blame] | 1799 | return std::make_pair(ranges_iterator(), ranges_iterator()); |
Ted Kremenek | c887d13 | 2009-04-29 18:50:19 +0000 | [diff] [blame] | 1800 | } |
Ted Kremenek | c887d13 | 2009-04-29 18:50:19 +0000 | [diff] [blame] | 1801 | }; |
Ted Kremenek | eaedfea | 2009-05-10 05:11:21 +0000 | [diff] [blame] | 1802 | |
Kovarththanan Rajaratnam | ba5fb5a | 2009-11-28 06:07:30 +0000 | [diff] [blame] | 1803 | class CFRefLeakReport : public CFRefReport { |
Ted Kremenek | c887d13 | 2009-04-29 18:50:19 +0000 | [diff] [blame] | 1804 | const MemRegion* AllocBinding; |
Anna Zaks | 23f395e | 2011-08-20 01:27:22 +0000 | [diff] [blame] | 1805 | |
Ted Kremenek | c887d13 | 2009-04-29 18:50:19 +0000 | [diff] [blame] | 1806 | public: |
Jordy Rose | 2058956 | 2011-08-24 22:39:09 +0000 | [diff] [blame] | 1807 | CFRefLeakReport(CFRefBug &D, const LangOptions &LOpts, bool GCEnabled, |
| 1808 | const SummaryLogTy &Log, ExplodedNode *n, SymbolRef sym, |
Anna Zaks | 6a93bd5 | 2011-10-25 19:57:11 +0000 | [diff] [blame] | 1809 | CheckerContext &Ctx); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1810 | |
Anna Zaks | 590dd8e | 2011-09-20 21:38:35 +0000 | [diff] [blame] | 1811 | PathDiagnosticLocation getLocation(const SourceManager &SM) const { |
| 1812 | assert(Location.isValid()); |
| 1813 | return Location; |
| 1814 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1815 | }; |
Ted Kremenek | c887d13 | 2009-04-29 18:50:19 +0000 | [diff] [blame] | 1816 | } // end anonymous namespace |
| 1817 | |
Jordy Rose | 2058956 | 2011-08-24 22:39:09 +0000 | [diff] [blame] | 1818 | void CFRefReport::addGCModeDescription(const LangOptions &LOpts, |
| 1819 | bool GCEnabled) { |
Jordy Rose | f95b19d | 2011-08-24 20:38:42 +0000 | [diff] [blame] | 1820 | const char *GCModeDescription = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1821 | |
Douglas Gregor | e289d81 | 2011-09-13 17:21:33 +0000 | [diff] [blame] | 1822 | switch (LOpts.getGC()) { |
Anna Zaks | 7f2531c | 2011-08-22 20:31:28 +0000 | [diff] [blame] | 1823 | case LangOptions::GCOnly: |
Jordy Rose | 2058956 | 2011-08-24 22:39:09 +0000 | [diff] [blame] | 1824 | assert(GCEnabled); |
Jordy Rose | 35c8695 | 2011-08-24 05:47:39 +0000 | [diff] [blame] | 1825 | GCModeDescription = "Code is compiled to only use garbage collection"; |
| 1826 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1827 | |
Anna Zaks | 7f2531c | 2011-08-22 20:31:28 +0000 | [diff] [blame] | 1828 | case LangOptions::NonGC: |
Jordy Rose | 2058956 | 2011-08-24 22:39:09 +0000 | [diff] [blame] | 1829 | assert(!GCEnabled); |
Jordy Rose | 35c8695 | 2011-08-24 05:47:39 +0000 | [diff] [blame] | 1830 | GCModeDescription = "Code is compiled to use reference counts"; |
| 1831 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1832 | |
Anna Zaks | 7f2531c | 2011-08-22 20:31:28 +0000 | [diff] [blame] | 1833 | case LangOptions::HybridGC: |
Jordy Rose | 2058956 | 2011-08-24 22:39:09 +0000 | [diff] [blame] | 1834 | if (GCEnabled) { |
Jordy Rose | 35c8695 | 2011-08-24 05:47:39 +0000 | [diff] [blame] | 1835 | GCModeDescription = "Code is compiled to use either garbage collection " |
| 1836 | "(GC) or reference counts (non-GC). The bug occurs " |
| 1837 | "with GC enabled"; |
| 1838 | break; |
| 1839 | } else { |
| 1840 | GCModeDescription = "Code is compiled to use either garbage collection " |
| 1841 | "(GC) or reference counts (non-GC). The bug occurs " |
| 1842 | "in non-GC mode"; |
| 1843 | break; |
Anna Zaks | 7f2531c | 2011-08-22 20:31:28 +0000 | [diff] [blame] | 1844 | } |
Ted Kremenek | c887d13 | 2009-04-29 18:50:19 +0000 | [diff] [blame] | 1845 | } |
Jordy Rose | 35c8695 | 2011-08-24 05:47:39 +0000 | [diff] [blame] | 1846 | |
Jordy Rose | f95b19d | 2011-08-24 20:38:42 +0000 | [diff] [blame] | 1847 | assert(GCModeDescription && "invalid/unknown GC mode"); |
Jordy Rose | 35c8695 | 2011-08-24 05:47:39 +0000 | [diff] [blame] | 1848 | addExtraText(GCModeDescription); |
Ted Kremenek | c887d13 | 2009-04-29 18:50:19 +0000 | [diff] [blame] | 1849 | } |
| 1850 | |
Jordy Rose | 910c405 | 2011-09-02 06:44:22 +0000 | [diff] [blame] | 1851 | // FIXME: This should be a method on SmallVector. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1852 | static inline bool contains(const SmallVectorImpl<ArgEffect>& V, |
Ted Kremenek | c887d13 | 2009-04-29 18:50:19 +0000 | [diff] [blame] | 1853 | ArgEffect X) { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1854 | for (SmallVectorImpl<ArgEffect>::const_iterator I=V.begin(), E=V.end(); |
Ted Kremenek | c887d13 | 2009-04-29 18:50:19 +0000 | [diff] [blame] | 1855 | I!=E; ++I) |
| 1856 | if (*I == X) return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1857 | |
Ted Kremenek | c887d13 | 2009-04-29 18:50:19 +0000 | [diff] [blame] | 1858 | return false; |
| 1859 | } |
| 1860 | |
Jordy Rose | 70fdbc3 | 2012-05-12 05:10:43 +0000 | [diff] [blame] | 1861 | static bool isNumericLiteralExpression(const Expr *E) { |
| 1862 | // FIXME: This set of cases was copied from SemaExprObjC. |
| 1863 | return isa<IntegerLiteral>(E) || |
| 1864 | isa<CharacterLiteral>(E) || |
| 1865 | isa<FloatingLiteral>(E) || |
| 1866 | isa<ObjCBoolLiteralExpr>(E) || |
| 1867 | isa<CXXBoolLiteralExpr>(E); |
| 1868 | } |
| 1869 | |
Anna Zaks | dc757b0 | 2011-08-19 23:21:56 +0000 | [diff] [blame] | 1870 | PathDiagnosticPiece *CFRefReportVisitor::VisitNode(const ExplodedNode *N, |
| 1871 | const ExplodedNode *PrevN, |
| 1872 | BugReporterContext &BRC, |
| 1873 | BugReport &BR) { |
Jordan Rose | 28038f3 | 2012-07-10 22:07:42 +0000 | [diff] [blame] | 1874 | // FIXME: We will eventually need to handle non-statement-based events |
| 1875 | // (__attribute__((cleanup))). |
Jordy Rose | f53e8c7 | 2011-08-23 19:43:16 +0000 | [diff] [blame] | 1876 | if (!isa<StmtPoint>(N->getLocation())) |
Ted Kremenek | 2033a95 | 2009-05-13 07:12:33 +0000 | [diff] [blame] | 1877 | return NULL; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1878 | |
Ted Kremenek | 8966bc1 | 2009-05-06 21:39:49 +0000 | [diff] [blame] | 1879 | // Check if the type state has changed. |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 1880 | ProgramStateRef PrevSt = PrevN->getState(); |
| 1881 | ProgramStateRef CurrSt = N->getState(); |
Ted Kremenek | 5eca482 | 2012-01-06 22:09:28 +0000 | [diff] [blame] | 1882 | const LocationContext *LCtx = N->getLocationContext(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1883 | |
Anna Zaks | 8d6b43c | 2012-08-14 00:36:15 +0000 | [diff] [blame] | 1884 | const RefVal* CurrT = getRefBinding(CurrSt, Sym); |
Ted Kremenek | c887d13 | 2009-04-29 18:50:19 +0000 | [diff] [blame] | 1885 | if (!CurrT) return NULL; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1886 | |
Ted Kremenek | b65be70 | 2009-06-18 01:23:53 +0000 | [diff] [blame] | 1887 | const RefVal &CurrV = *CurrT; |
Anna Zaks | 8d6b43c | 2012-08-14 00:36:15 +0000 | [diff] [blame] | 1888 | const RefVal *PrevT = getRefBinding(PrevSt, Sym); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1889 | |
Ted Kremenek | c887d13 | 2009-04-29 18:50:19 +0000 | [diff] [blame] | 1890 | // Create a string buffer to constain all the useful things we want |
| 1891 | // to tell the user. |
| 1892 | std::string sbuf; |
| 1893 | llvm::raw_string_ostream os(sbuf); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1894 | |
Ted Kremenek | c887d13 | 2009-04-29 18:50:19 +0000 | [diff] [blame] | 1895 | // This is the allocation site since the previous node had no bindings |
| 1896 | // for this symbol. |
| 1897 | if (!PrevT) { |
Jordy Rose | f53e8c7 | 2011-08-23 19:43:16 +0000 | [diff] [blame] | 1898 | const Stmt *S = cast<StmtPoint>(N->getLocation()).getStmt(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1899 | |
Ted Kremenek | 1a45a5f | 2012-03-06 20:06:12 +0000 | [diff] [blame] | 1900 | if (isa<ObjCArrayLiteral>(S)) { |
| 1901 | os << "NSArray literal is an object with a +0 retain count"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1902 | } |
Ted Kremenek | 1a45a5f | 2012-03-06 20:06:12 +0000 | [diff] [blame] | 1903 | else if (isa<ObjCDictionaryLiteral>(S)) { |
| 1904 | os << "NSDictionary literal is an object with a +0 retain count"; |
Ted Kremenek | c887d13 | 2009-04-29 18:50:19 +0000 | [diff] [blame] | 1905 | } |
Jordy Rose | 70fdbc3 | 2012-05-12 05:10:43 +0000 | [diff] [blame] | 1906 | else if (const ObjCBoxedExpr *BL = dyn_cast<ObjCBoxedExpr>(S)) { |
| 1907 | if (isNumericLiteralExpression(BL->getSubExpr())) |
| 1908 | os << "NSNumber literal is an object with a +0 retain count"; |
| 1909 | else { |
| 1910 | const ObjCInterfaceDecl *BoxClass = 0; |
| 1911 | if (const ObjCMethodDecl *Method = BL->getBoxingMethod()) |
| 1912 | BoxClass = Method->getClassInterface(); |
| 1913 | |
| 1914 | // We should always be able to find the boxing class interface, |
| 1915 | // but consider this future-proofing. |
| 1916 | if (BoxClass) |
| 1917 | os << *BoxClass << " b"; |
| 1918 | else |
| 1919 | os << "B"; |
| 1920 | |
| 1921 | os << "oxed expression produces an object with a +0 retain count"; |
| 1922 | } |
| 1923 | } |
Ted Kremenek | 1a45a5f | 2012-03-06 20:06:12 +0000 | [diff] [blame] | 1924 | else { |
| 1925 | if (const CallExpr *CE = dyn_cast<CallExpr>(S)) { |
| 1926 | // Get the name of the callee (if it is available). |
| 1927 | SVal X = CurrSt->getSValAsScalarOrLoc(CE->getCallee(), LCtx); |
| 1928 | if (const FunctionDecl *FD = X.getAsFunctionDecl()) |
| 1929 | os << "Call to function '" << *FD << '\''; |
| 1930 | else |
| 1931 | os << "function call"; |
Ted Kremenek | c887d13 | 2009-04-29 18:50:19 +0000 | [diff] [blame] | 1932 | } |
Ted Kremenek | 1a45a5f | 2012-03-06 20:06:12 +0000 | [diff] [blame] | 1933 | else { |
Jordan Rose | 8919e68 | 2012-07-18 21:59:51 +0000 | [diff] [blame] | 1934 | assert(isa<ObjCMessageExpr>(S)); |
Jordan Rose | d563d3f | 2012-07-30 20:22:09 +0000 | [diff] [blame] | 1935 | CallEventManager &Mgr = CurrSt->getStateManager().getCallEventManager(); |
| 1936 | CallEventRef<ObjCMethodCall> Call |
| 1937 | = Mgr.getObjCMethodCall(cast<ObjCMessageExpr>(S), CurrSt, LCtx); |
| 1938 | |
| 1939 | switch (Call->getMessageKind()) { |
Jordan Rose | 8919e68 | 2012-07-18 21:59:51 +0000 | [diff] [blame] | 1940 | case OCM_Message: |
| 1941 | os << "Method"; |
| 1942 | break; |
| 1943 | case OCM_PropertyAccess: |
| 1944 | os << "Property"; |
| 1945 | break; |
| 1946 | case OCM_Subscript: |
| 1947 | os << "Subscript"; |
| 1948 | break; |
| 1949 | } |
Ted Kremenek | 1a45a5f | 2012-03-06 20:06:12 +0000 | [diff] [blame] | 1950 | } |
| 1951 | |
| 1952 | if (CurrV.getObjKind() == RetEffect::CF) { |
| 1953 | os << " returns a Core Foundation object with a "; |
| 1954 | } |
| 1955 | else { |
| 1956 | assert (CurrV.getObjKind() == RetEffect::ObjC); |
| 1957 | os << " returns an Objective-C object with a "; |
| 1958 | } |
| 1959 | |
| 1960 | if (CurrV.isOwned()) { |
| 1961 | os << "+1 retain count"; |
| 1962 | |
| 1963 | if (GCEnabled) { |
| 1964 | assert(CurrV.getObjKind() == RetEffect::CF); |
| 1965 | os << ". " |
| 1966 | "Core Foundation objects are not automatically garbage collected."; |
| 1967 | } |
| 1968 | } |
| 1969 | else { |
| 1970 | assert (CurrV.isNotOwned()); |
| 1971 | os << "+0 retain count"; |
| 1972 | } |
Ted Kremenek | c887d13 | 2009-04-29 18:50:19 +0000 | [diff] [blame] | 1973 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1974 | |
Anna Zaks | 220ac8c | 2011-09-15 01:08:34 +0000 | [diff] [blame] | 1975 | PathDiagnosticLocation Pos(S, BRC.getSourceManager(), |
| 1976 | N->getLocationContext()); |
Ted Kremenek | c887d13 | 2009-04-29 18:50:19 +0000 | [diff] [blame] | 1977 | return new PathDiagnosticEventPiece(Pos, os.str()); |
| 1978 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1979 | |
Ted Kremenek | c887d13 | 2009-04-29 18:50:19 +0000 | [diff] [blame] | 1980 | // Gather up the effects that were performed on the object at this |
| 1981 | // program point |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1982 | SmallVector<ArgEffect, 2> AEffects; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1983 | |
Jordy Rose | ec9ef85 | 2011-08-23 20:55:48 +0000 | [diff] [blame] | 1984 | const ExplodedNode *OrigNode = BRC.getNodeResolver().getOriginalNode(N); |
| 1985 | if (const RetainSummary *Summ = SummaryLog.lookup(OrigNode)) { |
Ted Kremenek | c887d13 | 2009-04-29 18:50:19 +0000 | [diff] [blame] | 1986 | // We only have summaries attached to nodes after evaluating CallExpr and |
| 1987 | // ObjCMessageExprs. |
Jordy Rose | f53e8c7 | 2011-08-23 19:43:16 +0000 | [diff] [blame] | 1988 | const Stmt *S = cast<StmtPoint>(N->getLocation()).getStmt(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1989 | |
Ted Kremenek | 5f85e17 | 2009-07-22 22:35:28 +0000 | [diff] [blame] | 1990 | if (const CallExpr *CE = dyn_cast<CallExpr>(S)) { |
Ted Kremenek | c887d13 | 2009-04-29 18:50:19 +0000 | [diff] [blame] | 1991 | // Iterate through the parameter expressions and see if the symbol |
| 1992 | // was ever passed as an argument. |
| 1993 | unsigned i = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1994 | |
Ted Kremenek | 5f85e17 | 2009-07-22 22:35:28 +0000 | [diff] [blame] | 1995 | for (CallExpr::const_arg_iterator AI=CE->arg_begin(), AE=CE->arg_end(); |
Ted Kremenek | c887d13 | 2009-04-29 18:50:19 +0000 | [diff] [blame] | 1996 | AI!=AE; ++AI, ++i) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1997 | |
Ted Kremenek | c887d13 | 2009-04-29 18:50:19 +0000 | [diff] [blame] | 1998 | // Retrieve the value of the argument. Is it the symbol |
| 1999 | // we are interested in? |
Ted Kremenek | 5eca482 | 2012-01-06 22:09:28 +0000 | [diff] [blame] | 2000 | if (CurrSt->getSValAsScalarOrLoc(*AI, LCtx).getAsLocSymbol() != Sym) |
Ted Kremenek | c887d13 | 2009-04-29 18:50:19 +0000 | [diff] [blame] | 2001 | continue; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2002 | |
Ted Kremenek | c887d13 | 2009-04-29 18:50:19 +0000 | [diff] [blame] | 2003 | // We have an argument. Get the effect! |
| 2004 | AEffects.push_back(Summ->getArg(i)); |
| 2005 | } |
| 2006 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2007 | else if (const ObjCMessageExpr *ME = dyn_cast<ObjCMessageExpr>(S)) { |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2008 | if (const Expr *receiver = ME->getInstanceReceiver()) |
Ted Kremenek | 5eca482 | 2012-01-06 22:09:28 +0000 | [diff] [blame] | 2009 | if (CurrSt->getSValAsScalarOrLoc(receiver, LCtx) |
| 2010 | .getAsLocSymbol() == Sym) { |
Ted Kremenek | c887d13 | 2009-04-29 18:50:19 +0000 | [diff] [blame] | 2011 | // The symbol we are tracking is the receiver. |
| 2012 | AEffects.push_back(Summ->getReceiverEffect()); |
| 2013 | } |
| 2014 | } |
| 2015 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2016 | |
Ted Kremenek | c887d13 | 2009-04-29 18:50:19 +0000 | [diff] [blame] | 2017 | do { |
| 2018 | // Get the previous type state. |
| 2019 | RefVal PrevV = *PrevT; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2020 | |
Ted Kremenek | c887d13 | 2009-04-29 18:50:19 +0000 | [diff] [blame] | 2021 | // Specially handle -dealloc. |
Jordy Rose | 35c8695 | 2011-08-24 05:47:39 +0000 | [diff] [blame] | 2022 | if (!GCEnabled && contains(AEffects, Dealloc)) { |
Ted Kremenek | c887d13 | 2009-04-29 18:50:19 +0000 | [diff] [blame] | 2023 | // Determine if the object's reference count was pushed to zero. |
| 2024 | assert(!(PrevV == CurrV) && "The typestate *must* have changed."); |
| 2025 | // We may not have transitioned to 'release' if we hit an error. |
| 2026 | // This case is handled elsewhere. |
| 2027 | if (CurrV.getKind() == RefVal::Released) { |
Ted Kremenek | f21332e | 2009-05-08 20:01:42 +0000 | [diff] [blame] | 2028 | assert(CurrV.getCombinedCounts() == 0); |
Ted Kremenek | c887d13 | 2009-04-29 18:50:19 +0000 | [diff] [blame] | 2029 | os << "Object released by directly sending the '-dealloc' message"; |
| 2030 | break; |
| 2031 | } |
| 2032 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2033 | |
Ted Kremenek | c887d13 | 2009-04-29 18:50:19 +0000 | [diff] [blame] | 2034 | // Specially handle CFMakeCollectable and friends. |
| 2035 | if (contains(AEffects, MakeCollectable)) { |
| 2036 | // Get the name of the function. |
Jordy Rose | f53e8c7 | 2011-08-23 19:43:16 +0000 | [diff] [blame] | 2037 | const Stmt *S = cast<StmtPoint>(N->getLocation()).getStmt(); |
Ted Kremenek | 5eca482 | 2012-01-06 22:09:28 +0000 | [diff] [blame] | 2038 | SVal X = |
| 2039 | CurrSt->getSValAsScalarOrLoc(cast<CallExpr>(S)->getCallee(), LCtx); |
Ted Kremenek | 9c378f7 | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 2040 | const FunctionDecl *FD = X.getAsFunctionDecl(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2041 | |
Jordy Rose | 35c8695 | 2011-08-24 05:47:39 +0000 | [diff] [blame] | 2042 | if (GCEnabled) { |
Ted Kremenek | c887d13 | 2009-04-29 18:50:19 +0000 | [diff] [blame] | 2043 | // Determine if the object's reference count was pushed to zero. |
| 2044 | assert(!(PrevV == CurrV) && "The typestate *must* have changed."); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2045 | |
Benjamin Kramer | b8989f2 | 2011-10-14 18:45:37 +0000 | [diff] [blame] | 2046 | os << "In GC mode a call to '" << *FD |
Ted Kremenek | c887d13 | 2009-04-29 18:50:19 +0000 | [diff] [blame] | 2047 | << "' decrements an object's retain count and registers the " |
| 2048 | "object with the garbage collector. "; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2049 | |
Ted Kremenek | c887d13 | 2009-04-29 18:50:19 +0000 | [diff] [blame] | 2050 | if (CurrV.getKind() == RefVal::Released) { |
| 2051 | assert(CurrV.getCount() == 0); |
| 2052 | os << "Since it now has a 0 retain count the object can be " |
| 2053 | "automatically collected by the garbage collector."; |
| 2054 | } |
| 2055 | else |
| 2056 | os << "An object must have a 0 retain count to be garbage collected. " |
| 2057 | "After this call its retain count is +" << CurrV.getCount() |
| 2058 | << '.'; |
| 2059 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2060 | else |
Benjamin Kramer | b8989f2 | 2011-10-14 18:45:37 +0000 | [diff] [blame] | 2061 | os << "When GC is not enabled a call to '" << *FD |
Ted Kremenek | c887d13 | 2009-04-29 18:50:19 +0000 | [diff] [blame] | 2062 | << "' has no effect on its argument."; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2063 | |
Ted Kremenek | c887d13 | 2009-04-29 18:50:19 +0000 | [diff] [blame] | 2064 | // Nothing more to say. |
| 2065 | break; |
| 2066 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2067 | |
| 2068 | // Determine if the typestate has changed. |
Ted Kremenek | c887d13 | 2009-04-29 18:50:19 +0000 | [diff] [blame] | 2069 | if (!(PrevV == CurrV)) |
| 2070 | switch (CurrV.getKind()) { |
| 2071 | case RefVal::Owned: |
| 2072 | case RefVal::NotOwned: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2073 | |
Ted Kremenek | f21332e | 2009-05-08 20:01:42 +0000 | [diff] [blame] | 2074 | if (PrevV.getCount() == CurrV.getCount()) { |
| 2075 | // Did an autorelease message get sent? |
| 2076 | if (PrevV.getAutoreleaseCount() == CurrV.getAutoreleaseCount()) |
| 2077 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2078 | |
Zhongxing Xu | 264e937 | 2009-05-12 10:10:00 +0000 | [diff] [blame] | 2079 | assert(PrevV.getAutoreleaseCount() < CurrV.getAutoreleaseCount()); |
Ted Kremenek | eaedfea | 2009-05-10 05:11:21 +0000 | [diff] [blame] | 2080 | os << "Object sent -autorelease message"; |
Ted Kremenek | f21332e | 2009-05-08 20:01:42 +0000 | [diff] [blame] | 2081 | break; |
| 2082 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2083 | |
Ted Kremenek | c887d13 | 2009-04-29 18:50:19 +0000 | [diff] [blame] | 2084 | if (PrevV.getCount() > CurrV.getCount()) |
| 2085 | os << "Reference count decremented."; |
| 2086 | else |
| 2087 | os << "Reference count incremented."; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2088 | |
Ted Kremenek | c887d13 | 2009-04-29 18:50:19 +0000 | [diff] [blame] | 2089 | if (unsigned Count = CurrV.getCount()) |
| 2090 | os << " The object now has a +" << Count << " retain count."; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2091 | |
Ted Kremenek | c887d13 | 2009-04-29 18:50:19 +0000 | [diff] [blame] | 2092 | if (PrevV.getKind() == RefVal::Released) { |
Jordy Rose | 35c8695 | 2011-08-24 05:47:39 +0000 | [diff] [blame] | 2093 | assert(GCEnabled && CurrV.getCount() > 0); |
Jordy Rose | 74b7b2b | 2012-03-17 05:49:15 +0000 | [diff] [blame] | 2094 | os << " The object is not eligible for garbage collection until " |
| 2095 | "the retain count reaches 0 again."; |
Ted Kremenek | c887d13 | 2009-04-29 18:50:19 +0000 | [diff] [blame] | 2096 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2097 | |
Ted Kremenek | c887d13 | 2009-04-29 18:50:19 +0000 | [diff] [blame] | 2098 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2099 | |
Ted Kremenek | c887d13 | 2009-04-29 18:50:19 +0000 | [diff] [blame] | 2100 | case RefVal::Released: |
| 2101 | os << "Object released."; |
| 2102 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2103 | |
Ted Kremenek | c887d13 | 2009-04-29 18:50:19 +0000 | [diff] [blame] | 2104 | case RefVal::ReturnedOwned: |
Jordy Rose | 74b7b2b | 2012-03-17 05:49:15 +0000 | [diff] [blame] | 2105 | // Autoreleases can be applied after marking a node ReturnedOwned. |
| 2106 | if (CurrV.getAutoreleaseCount()) |
| 2107 | return NULL; |
| 2108 | |
| 2109 | os << "Object returned to caller as an owning reference (single " |
| 2110 | "retain count transferred to caller)"; |
Ted Kremenek | c887d13 | 2009-04-29 18:50:19 +0000 | [diff] [blame] | 2111 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2112 | |
Ted Kremenek | c887d13 | 2009-04-29 18:50:19 +0000 | [diff] [blame] | 2113 | case RefVal::ReturnedNotOwned: |
Ted Kremenek | f136546 | 2011-05-26 18:45:44 +0000 | [diff] [blame] | 2114 | os << "Object returned to caller with a +0 retain count"; |
Ted Kremenek | c887d13 | 2009-04-29 18:50:19 +0000 | [diff] [blame] | 2115 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2116 | |
Ted Kremenek | c887d13 | 2009-04-29 18:50:19 +0000 | [diff] [blame] | 2117 | default: |
| 2118 | return NULL; |
| 2119 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2120 | |
Ted Kremenek | c887d13 | 2009-04-29 18:50:19 +0000 | [diff] [blame] | 2121 | // Emit any remaining diagnostics for the argument effects (if any). |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2122 | for (SmallVectorImpl<ArgEffect>::iterator I=AEffects.begin(), |
Ted Kremenek | c887d13 | 2009-04-29 18:50:19 +0000 | [diff] [blame] | 2123 | E=AEffects.end(); I != E; ++I) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2124 | |
Ted Kremenek | c887d13 | 2009-04-29 18:50:19 +0000 | [diff] [blame] | 2125 | // A bunch of things have alternate behavior under GC. |
Jordy Rose | 35c8695 | 2011-08-24 05:47:39 +0000 | [diff] [blame] | 2126 | if (GCEnabled) |
Ted Kremenek | c887d13 | 2009-04-29 18:50:19 +0000 | [diff] [blame] | 2127 | switch (*I) { |
| 2128 | default: break; |
| 2129 | case Autorelease: |
| 2130 | os << "In GC mode an 'autorelease' has no effect."; |
| 2131 | continue; |
| 2132 | case IncRefMsg: |
| 2133 | os << "In GC mode the 'retain' message has no effect."; |
| 2134 | continue; |
| 2135 | case DecRefMsg: |
| 2136 | os << "In GC mode the 'release' message has no effect."; |
| 2137 | continue; |
| 2138 | } |
| 2139 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2140 | } while (0); |
| 2141 | |
Ted Kremenek | c887d13 | 2009-04-29 18:50:19 +0000 | [diff] [blame] | 2142 | if (os.str().empty()) |
| 2143 | return 0; // We have nothing to say! |
Ted Kremenek | 2033a95 | 2009-05-13 07:12:33 +0000 | [diff] [blame] | 2144 | |
Jordy Rose | f53e8c7 | 2011-08-23 19:43:16 +0000 | [diff] [blame] | 2145 | const Stmt *S = cast<StmtPoint>(N->getLocation()).getStmt(); |
Anna Zaks | 220ac8c | 2011-09-15 01:08:34 +0000 | [diff] [blame] | 2146 | PathDiagnosticLocation Pos(S, BRC.getSourceManager(), |
| 2147 | N->getLocationContext()); |
Ted Kremenek | 9c378f7 | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 2148 | PathDiagnosticPiece *P = new PathDiagnosticEventPiece(Pos, os.str()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2149 | |
Ted Kremenek | c887d13 | 2009-04-29 18:50:19 +0000 | [diff] [blame] | 2150 | // Add the range by scanning the children of the statement for any bindings |
| 2151 | // to Sym. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2152 | for (Stmt::const_child_iterator I = S->child_begin(), E = S->child_end(); |
Ted Kremenek | 5f85e17 | 2009-07-22 22:35:28 +0000 | [diff] [blame] | 2153 | I!=E; ++I) |
Ted Kremenek | 9c378f7 | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 2154 | if (const Expr *Exp = dyn_cast_or_null<Expr>(*I)) |
Ted Kremenek | 5eca482 | 2012-01-06 22:09:28 +0000 | [diff] [blame] | 2155 | if (CurrSt->getSValAsScalarOrLoc(Exp, LCtx).getAsLocSymbol() == Sym) { |
Ted Kremenek | c887d13 | 2009-04-29 18:50:19 +0000 | [diff] [blame] | 2156 | P->addRange(Exp->getSourceRange()); |
| 2157 | break; |
| 2158 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2159 | |
Ted Kremenek | c887d13 | 2009-04-29 18:50:19 +0000 | [diff] [blame] | 2160 | return P; |
| 2161 | } |
| 2162 | |
Anna Zaks | e7e0168 | 2012-02-28 22:39:22 +0000 | [diff] [blame] | 2163 | // Find the first node in the current function context that referred to the |
| 2164 | // tracked symbol and the memory location that value was stored to. Note, the |
| 2165 | // value is only reported if the allocation occurred in the same function as |
| 2166 | // the leak. |
Zhongxing Xu | c5619d9 | 2009-08-06 01:32:16 +0000 | [diff] [blame] | 2167 | static std::pair<const ExplodedNode*,const MemRegion*> |
Ted Kremenek | 18c66fd | 2011-08-15 22:09:50 +0000 | [diff] [blame] | 2168 | GetAllocationSite(ProgramStateManager& StateMgr, const ExplodedNode *N, |
Ted Kremenek | c887d13 | 2009-04-29 18:50:19 +0000 | [diff] [blame] | 2169 | SymbolRef Sym) { |
Ted Kremenek | 9c378f7 | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 2170 | const ExplodedNode *Last = N; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2171 | const MemRegion* FirstBinding = 0; |
Anna Zaks | e7e0168 | 2012-02-28 22:39:22 +0000 | [diff] [blame] | 2172 | const LocationContext *LeakContext = N->getLocationContext(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2173 | |
Ted Kremenek | c887d13 | 2009-04-29 18:50:19 +0000 | [diff] [blame] | 2174 | while (N) { |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 2175 | ProgramStateRef St = N->getState(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2176 | |
Anna Zaks | 8d6b43c | 2012-08-14 00:36:15 +0000 | [diff] [blame] | 2177 | if (!getRefBinding(St, Sym)) |
Ted Kremenek | c887d13 | 2009-04-29 18:50:19 +0000 | [diff] [blame] | 2178 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2179 | |
Anna Zaks | 27b867e | 2012-03-21 19:45:01 +0000 | [diff] [blame] | 2180 | StoreManager::FindUniqueBinding FB(Sym); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2181 | StateMgr.iterBindings(St, FB); |
| 2182 | if (FB) FirstBinding = FB.getRegion(); |
| 2183 | |
Anna Zaks | e7e0168 | 2012-02-28 22:39:22 +0000 | [diff] [blame] | 2184 | // Allocation node, is the last node in the current context in which the |
| 2185 | // symbol was tracked. |
| 2186 | if (N->getLocationContext() == LeakContext) |
| 2187 | Last = N; |
| 2188 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2189 | N = N->pred_empty() ? NULL : *(N->pred_begin()); |
Ted Kremenek | c887d13 | 2009-04-29 18:50:19 +0000 | [diff] [blame] | 2190 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2191 | |
Anna Zaks | e7e0168 | 2012-02-28 22:39:22 +0000 | [diff] [blame] | 2192 | // If allocation happened in a function different from the leak node context, |
| 2193 | // do not report the binding. |
Ted Kremenek | 5a8fc88 | 2012-10-12 22:56:40 +0000 | [diff] [blame] | 2194 | assert(N && "Could not find allocation node"); |
Anna Zaks | e7e0168 | 2012-02-28 22:39:22 +0000 | [diff] [blame] | 2195 | if (N->getLocationContext() != LeakContext) { |
| 2196 | FirstBinding = 0; |
| 2197 | } |
| 2198 | |
Ted Kremenek | c887d13 | 2009-04-29 18:50:19 +0000 | [diff] [blame] | 2199 | return std::make_pair(Last, FirstBinding); |
| 2200 | } |
| 2201 | |
| 2202 | PathDiagnosticPiece* |
Anna Zaks | 23f395e | 2011-08-20 01:27:22 +0000 | [diff] [blame] | 2203 | CFRefReportVisitor::getEndPath(BugReporterContext &BRC, |
| 2204 | const ExplodedNode *EndN, |
| 2205 | BugReport &BR) { |
Ted Kremenek | 76aadc3 | 2012-03-09 01:13:14 +0000 | [diff] [blame] | 2206 | BR.markInteresting(Sym); |
Anna Zaks | 23f395e | 2011-08-20 01:27:22 +0000 | [diff] [blame] | 2207 | return BugReporterVisitor::getDefaultEndPath(BRC, EndN, BR); |
Ted Kremenek | c887d13 | 2009-04-29 18:50:19 +0000 | [diff] [blame] | 2208 | } |
| 2209 | |
| 2210 | PathDiagnosticPiece* |
Anna Zaks | 23f395e | 2011-08-20 01:27:22 +0000 | [diff] [blame] | 2211 | CFRefLeakReportVisitor::getEndPath(BugReporterContext &BRC, |
| 2212 | const ExplodedNode *EndN, |
| 2213 | BugReport &BR) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2214 | |
Ted Kremenek | 8966bc1 | 2009-05-06 21:39:49 +0000 | [diff] [blame] | 2215 | // Tell the BugReporterContext to report cases when the tracked symbol is |
Ted Kremenek | c887d13 | 2009-04-29 18:50:19 +0000 | [diff] [blame] | 2216 | // assigned to different variables, etc. |
Ted Kremenek | 76aadc3 | 2012-03-09 01:13:14 +0000 | [diff] [blame] | 2217 | BR.markInteresting(Sym); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2218 | |
Ted Kremenek | c887d13 | 2009-04-29 18:50:19 +0000 | [diff] [blame] | 2219 | // We are reporting a leak. Walk up the graph to get to the first node where |
| 2220 | // the symbol appeared, and also get the first VarDecl that tracked object |
| 2221 | // is stored to. |
Ted Kremenek | 9c378f7 | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 2222 | const ExplodedNode *AllocNode = 0; |
Ted Kremenek | c887d13 | 2009-04-29 18:50:19 +0000 | [diff] [blame] | 2223 | const MemRegion* FirstBinding = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2224 | |
Ted Kremenek | c887d13 | 2009-04-29 18:50:19 +0000 | [diff] [blame] | 2225 | llvm::tie(AllocNode, FirstBinding) = |
Ted Kremenek | f04dced | 2009-05-08 23:32:51 +0000 | [diff] [blame] | 2226 | GetAllocationSite(BRC.getStateManager(), EndN, Sym); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2227 | |
Anna Zaks | 4fdf97b | 2011-09-15 18:56:07 +0000 | [diff] [blame] | 2228 | SourceManager& SM = BRC.getSourceManager(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2229 | |
Ted Kremenek | c887d13 | 2009-04-29 18:50:19 +0000 | [diff] [blame] | 2230 | // Compute an actual location for the leak. Sometimes a leak doesn't |
| 2231 | // occur at an actual statement (e.g., transition between blocks; end |
| 2232 | // of function) so we need to walk the graph and compute a real location. |
Ted Kremenek | 9c378f7 | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 2233 | const ExplodedNode *LeakN = EndN; |
Anna Zaks | 4fdf97b | 2011-09-15 18:56:07 +0000 | [diff] [blame] | 2234 | PathDiagnosticLocation L = PathDiagnosticLocation::createEndOfPath(LeakN, SM); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2235 | |
Ted Kremenek | c887d13 | 2009-04-29 18:50:19 +0000 | [diff] [blame] | 2236 | std::string sbuf; |
| 2237 | llvm::raw_string_ostream os(sbuf); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2238 | |
Ted Kremenek | f136546 | 2011-05-26 18:45:44 +0000 | [diff] [blame] | 2239 | os << "Object leaked: "; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2240 | |
Ted Kremenek | f136546 | 2011-05-26 18:45:44 +0000 | [diff] [blame] | 2241 | if (FirstBinding) { |
| 2242 | os << "object allocated and stored into '" |
| 2243 | << FirstBinding->getString() << '\''; |
| 2244 | } |
| 2245 | else |
| 2246 | os << "allocated object"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2247 | |
Ted Kremenek | c887d13 | 2009-04-29 18:50:19 +0000 | [diff] [blame] | 2248 | // Get the retain count. |
Anna Zaks | 8d6b43c | 2012-08-14 00:36:15 +0000 | [diff] [blame] | 2249 | const RefVal* RV = getRefBinding(EndN->getState(), Sym); |
Ted Kremenek | 5a8fc88 | 2012-10-12 22:56:40 +0000 | [diff] [blame] | 2250 | assert(RV); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2251 | |
Ted Kremenek | c887d13 | 2009-04-29 18:50:19 +0000 | [diff] [blame] | 2252 | if (RV->getKind() == RefVal::ErrorLeakReturned) { |
| 2253 | // FIXME: Per comments in rdar://6320065, "create" only applies to CF |
Jordy Rose | 5b5402b | 2011-07-15 22:17:54 +0000 | [diff] [blame] | 2254 | // objects. Only "copy", "alloc", "retain" and "new" transfer ownership |
Ted Kremenek | c887d13 | 2009-04-29 18:50:19 +0000 | [diff] [blame] | 2255 | // to the caller for NS objects. |
Ted Kremenek | d368d71 | 2011-05-25 06:19:45 +0000 | [diff] [blame] | 2256 | const Decl *D = &EndN->getCodeDecl(); |
Ted Kremenek | ec9f36e | 2012-09-06 23:03:07 +0000 | [diff] [blame] | 2257 | |
| 2258 | os << (isa<ObjCMethodDecl>(D) ? " is returned from a method " |
| 2259 | : " is returned from a function "); |
| 2260 | |
| 2261 | if (D->getAttr<CFReturnsNotRetainedAttr>()) |
| 2262 | os << "that is annotated as CF_RETURNS_NOT_RETAINED"; |
| 2263 | else if (D->getAttr<NSReturnsNotRetainedAttr>()) |
| 2264 | os << "that is annotated as NS_RETURNS_NOT_RETAINED"; |
Ted Kremenek | d368d71 | 2011-05-25 06:19:45 +0000 | [diff] [blame] | 2265 | else { |
Ted Kremenek | ec9f36e | 2012-09-06 23:03:07 +0000 | [diff] [blame] | 2266 | if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) { |
| 2267 | os << "whose name ('" << MD->getSelector().getAsString() |
| 2268 | << "') does not start with 'copy', 'mutableCopy', 'alloc' or 'new'." |
| 2269 | " This violates the naming convention rules" |
| 2270 | " given in the Memory Management Guide for Cocoa"; |
| 2271 | } |
| 2272 | else { |
| 2273 | const FunctionDecl *FD = cast<FunctionDecl>(D); |
| 2274 | os << "whose name ('" << *FD |
| 2275 | << "') does not contain 'Copy' or 'Create'. This violates the naming" |
| 2276 | " convention rules given in the Memory Management Guide for Core" |
| 2277 | " Foundation"; |
| 2278 | } |
| 2279 | } |
Ted Kremenek | c887d13 | 2009-04-29 18:50:19 +0000 | [diff] [blame] | 2280 | } |
Ted Kremenek | e8720ce | 2009-05-10 06:25:57 +0000 | [diff] [blame] | 2281 | else if (RV->getKind() == RefVal::ErrorGCLeakReturned) { |
Ted Kremenek | 9c378f7 | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 2282 | ObjCMethodDecl &MD = cast<ObjCMethodDecl>(EndN->getCodeDecl()); |
Ted Kremenek | e8720ce | 2009-05-10 06:25:57 +0000 | [diff] [blame] | 2283 | os << " and returned from method '" << MD.getSelector().getAsString() |
Ted Kremenek | 82f2be5 | 2009-05-10 16:52:15 +0000 | [diff] [blame] | 2284 | << "' is potentially leaked when using garbage collection. Callers " |
| 2285 | "of this method do not expect a returned object with a +1 retain " |
| 2286 | "count since they expect the object to be managed by the garbage " |
| 2287 | "collector"; |
Ted Kremenek | e8720ce | 2009-05-10 06:25:57 +0000 | [diff] [blame] | 2288 | } |
Ted Kremenek | c887d13 | 2009-04-29 18:50:19 +0000 | [diff] [blame] | 2289 | else |
Ted Kremenek | abf517c | 2010-10-15 22:50:23 +0000 | [diff] [blame] | 2290 | os << " is not referenced later in this execution path and has a retain " |
Ted Kremenek | f136546 | 2011-05-26 18:45:44 +0000 | [diff] [blame] | 2291 | "count of +" << RV->getCount(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2292 | |
Ted Kremenek | c887d13 | 2009-04-29 18:50:19 +0000 | [diff] [blame] | 2293 | return new PathDiagnosticEventPiece(L, os.str()); |
| 2294 | } |
| 2295 | |
Jordy Rose | 2058956 | 2011-08-24 22:39:09 +0000 | [diff] [blame] | 2296 | CFRefLeakReport::CFRefLeakReport(CFRefBug &D, const LangOptions &LOpts, |
| 2297 | bool GCEnabled, const SummaryLogTy &Log, |
| 2298 | ExplodedNode *n, SymbolRef sym, |
Anna Zaks | 6a93bd5 | 2011-10-25 19:57:11 +0000 | [diff] [blame] | 2299 | CheckerContext &Ctx) |
Jordy Rose | 2058956 | 2011-08-24 22:39:09 +0000 | [diff] [blame] | 2300 | : CFRefReport(D, LOpts, GCEnabled, Log, n, sym, false) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2301 | |
Chris Lattner | fc8f0e1 | 2011-04-15 05:22:18 +0000 | [diff] [blame] | 2302 | // Most bug reports are cached at the location where they occurred. |
Ted Kremenek | c887d13 | 2009-04-29 18:50:19 +0000 | [diff] [blame] | 2303 | // With leaks, we want to unique them by the location where they were |
| 2304 | // allocated, and only report a single path. To do this, we need to find |
| 2305 | // the allocation site of a piece of tracked memory, which we do via a |
| 2306 | // call to GetAllocationSite. This will walk the ExplodedGraph backwards. |
| 2307 | // Note that this is *not* the trimmed graph; we are guaranteed, however, |
| 2308 | // that all ancestor nodes that represent the allocation site have the |
| 2309 | // same SourceLocation. |
Ted Kremenek | 9c378f7 | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 2310 | const ExplodedNode *AllocNode = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2311 | |
Anna Zaks | 6a93bd5 | 2011-10-25 19:57:11 +0000 | [diff] [blame] | 2312 | const SourceManager& SMgr = Ctx.getSourceManager(); |
Anna Zaks | 590dd8e | 2011-09-20 21:38:35 +0000 | [diff] [blame] | 2313 | |
Ted Kremenek | c887d13 | 2009-04-29 18:50:19 +0000 | [diff] [blame] | 2314 | llvm::tie(AllocNode, AllocBinding) = // Set AllocBinding. |
Anna Zaks | 6a93bd5 | 2011-10-25 19:57:11 +0000 | [diff] [blame] | 2315 | GetAllocationSite(Ctx.getStateManager(), getErrorNode(), sym); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2316 | |
Ted Kremenek | c887d13 | 2009-04-29 18:50:19 +0000 | [diff] [blame] | 2317 | // Get the SourceLocation for the allocation site. |
Jordan Rose | 852aa0d | 2012-07-10 22:07:52 +0000 | [diff] [blame] | 2318 | // FIXME: This will crash the analyzer if an allocation comes from an |
| 2319 | // implicit call. (Currently there are no such allocations in Cocoa, though.) |
| 2320 | const Stmt *AllocStmt; |
Ted Kremenek | c887d13 | 2009-04-29 18:50:19 +0000 | [diff] [blame] | 2321 | ProgramPoint P = AllocNode->getLocation(); |
Jordan Rose | 852aa0d | 2012-07-10 22:07:52 +0000 | [diff] [blame] | 2322 | if (CallExitEnd *Exit = dyn_cast<CallExitEnd>(&P)) |
| 2323 | AllocStmt = Exit->getCalleeContext()->getCallSite(); |
| 2324 | else |
| 2325 | AllocStmt = cast<PostStmt>(P).getStmt(); |
| 2326 | assert(AllocStmt && "All allocations must come from explicit calls"); |
Anna Zaks | 590dd8e | 2011-09-20 21:38:35 +0000 | [diff] [blame] | 2327 | Location = PathDiagnosticLocation::createBegin(AllocStmt, SMgr, |
| 2328 | n->getLocationContext()); |
Ted Kremenek | c887d13 | 2009-04-29 18:50:19 +0000 | [diff] [blame] | 2329 | // Fill in the description of the bug. |
| 2330 | Description.clear(); |
| 2331 | llvm::raw_string_ostream os(Description); |
Ted Kremenek | dd924e2 | 2009-05-02 19:05:19 +0000 | [diff] [blame] | 2332 | os << "Potential leak "; |
Jordy Rose | 2058956 | 2011-08-24 22:39:09 +0000 | [diff] [blame] | 2333 | if (GCEnabled) |
Ted Kremenek | dd924e2 | 2009-05-02 19:05:19 +0000 | [diff] [blame] | 2334 | os << "(when using garbage collection) "; |
Anna Zaks | 212000e | 2012-02-28 21:49:08 +0000 | [diff] [blame] | 2335 | os << "of an object"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2336 | |
Ted Kremenek | c887d13 | 2009-04-29 18:50:19 +0000 | [diff] [blame] | 2337 | // FIXME: AllocBinding doesn't get populated for RegionStore yet. |
| 2338 | if (AllocBinding) |
Anna Zaks | 212000e | 2012-02-28 21:49:08 +0000 | [diff] [blame] | 2339 | os << " stored into '" << AllocBinding->getString() << '\''; |
Anna Zaks | dc757b0 | 2011-08-19 23:21:56 +0000 | [diff] [blame] | 2340 | |
Jordy Rose | 2058956 | 2011-08-24 22:39:09 +0000 | [diff] [blame] | 2341 | addVisitor(new CFRefLeakReportVisitor(sym, GCEnabled, Log)); |
Ted Kremenek | c887d13 | 2009-04-29 18:50:19 +0000 | [diff] [blame] | 2342 | } |
| 2343 | |
| 2344 | //===----------------------------------------------------------------------===// |
| 2345 | // Main checker logic. |
| 2346 | //===----------------------------------------------------------------------===// |
| 2347 | |
Ted Kremenek | d593eb9 | 2009-11-25 22:17:44 +0000 | [diff] [blame] | 2348 | namespace { |
Jordy Rose | 910c405 | 2011-09-02 06:44:22 +0000 | [diff] [blame] | 2349 | class RetainCountChecker |
Jordy Rose | 9c083b7 | 2011-08-24 18:56:32 +0000 | [diff] [blame] | 2350 | : public Checker< check::Bind, |
Jordy Rose | 38f17d6 | 2011-08-23 19:01:07 +0000 | [diff] [blame] | 2351 | check::DeadSymbols, |
Jordy Rose | 9c083b7 | 2011-08-24 18:56:32 +0000 | [diff] [blame] | 2352 | check::EndAnalysis, |
Jordy Rose | 38f17d6 | 2011-08-23 19:01:07 +0000 | [diff] [blame] | 2353 | check::EndPath, |
Jordy Rose | 6704429 | 2011-08-17 21:27:39 +0000 | [diff] [blame] | 2354 | check::PostStmt<BlockExpr>, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2355 | check::PostStmt<CastExpr>, |
Ted Kremenek | 1a45a5f | 2012-03-06 20:06:12 +0000 | [diff] [blame] | 2356 | check::PostStmt<ObjCArrayLiteral>, |
| 2357 | check::PostStmt<ObjCDictionaryLiteral>, |
Jordy Rose | 70fdbc3 | 2012-05-12 05:10:43 +0000 | [diff] [blame] | 2358 | check::PostStmt<ObjCBoxedExpr>, |
Jordan Rose | fe6a011 | 2012-07-02 19:28:21 +0000 | [diff] [blame] | 2359 | check::PostCall, |
Jordy Rose | f53e8c7 | 2011-08-23 19:43:16 +0000 | [diff] [blame] | 2360 | check::PreStmt<ReturnStmt>, |
Jordy Rose | 6704429 | 2011-08-17 21:27:39 +0000 | [diff] [blame] | 2361 | check::RegionChanges, |
Jordy Rose | 76c506f | 2011-08-21 21:58:18 +0000 | [diff] [blame] | 2362 | eval::Assume, |
| 2363 | eval::Call > { |
Dylan Noblesmith | 6f42b62 | 2012-02-05 02:12:40 +0000 | [diff] [blame] | 2364 | mutable OwningPtr<CFRefBug> useAfterRelease, releaseNotOwned; |
| 2365 | mutable OwningPtr<CFRefBug> deallocGC, deallocNotOwned; |
| 2366 | mutable OwningPtr<CFRefBug> overAutorelease, returnNotOwnedForOwned; |
| 2367 | mutable OwningPtr<CFRefBug> leakWithinFunction, leakAtReturn; |
| 2368 | mutable OwningPtr<CFRefBug> leakWithinFunctionGC, leakAtReturnGC; |
Jordy Rose | 38f17d6 | 2011-08-23 19:01:07 +0000 | [diff] [blame] | 2369 | |
| 2370 | typedef llvm::DenseMap<SymbolRef, const SimpleProgramPointTag *> SymbolTagMap; |
| 2371 | |
| 2372 | // This map is only used to ensure proper deletion of any allocated tags. |
| 2373 | mutable SymbolTagMap DeadSymbolTags; |
| 2374 | |
Dylan Noblesmith | 6f42b62 | 2012-02-05 02:12:40 +0000 | [diff] [blame] | 2375 | mutable OwningPtr<RetainSummaryManager> Summaries; |
| 2376 | mutable OwningPtr<RetainSummaryManager> SummariesGC; |
Jordy Rose | 9c083b7 | 2011-08-24 18:56:32 +0000 | [diff] [blame] | 2377 | mutable SummaryLogTy SummaryLog; |
| 2378 | mutable bool ShouldResetSummaryLog; |
| 2379 | |
Jordy Rose | 2f9a66d | 2011-08-20 21:17:59 +0000 | [diff] [blame] | 2380 | public: |
Jordy Rose | 910c405 | 2011-09-02 06:44:22 +0000 | [diff] [blame] | 2381 | RetainCountChecker() : ShouldResetSummaryLog(false) {} |
Jordy Rose | 38f17d6 | 2011-08-23 19:01:07 +0000 | [diff] [blame] | 2382 | |
Jordy Rose | 910c405 | 2011-09-02 06:44:22 +0000 | [diff] [blame] | 2383 | virtual ~RetainCountChecker() { |
Jordy Rose | 38f17d6 | 2011-08-23 19:01:07 +0000 | [diff] [blame] | 2384 | DeleteContainerSeconds(DeadSymbolTags); |
| 2385 | } |
| 2386 | |
Jordy Rose | 9c083b7 | 2011-08-24 18:56:32 +0000 | [diff] [blame] | 2387 | void checkEndAnalysis(ExplodedGraph &G, BugReporter &BR, |
| 2388 | ExprEngine &Eng) const { |
| 2389 | // FIXME: This is a hack to make sure the summary log gets cleared between |
| 2390 | // analyses of different code bodies. |
| 2391 | // |
| 2392 | // Why is this necessary? Because a checker's lifetime is tied to a |
| 2393 | // translation unit, but an ExplodedGraph's lifetime is just a code body. |
| 2394 | // Once in a blue moon, a new ExplodedNode will have the same address as an |
| 2395 | // old one with an associated summary, and the bug report visitor gets very |
| 2396 | // confused. (To make things worse, the summary lifetime is currently also |
| 2397 | // tied to a code body, so we get a crash instead of incorrect results.) |
Jordy Rose | 1ab51c7 | 2011-08-24 09:27:24 +0000 | [diff] [blame] | 2398 | // |
| 2399 | // Why is this a bad solution? Because if the lifetime of the ExplodedGraph |
| 2400 | // changes, things will start going wrong again. Really the lifetime of this |
| 2401 | // log needs to be tied to either the specific nodes in it or the entire |
| 2402 | // ExplodedGraph, not to a specific part of the code being analyzed. |
| 2403 | // |
Jordy Rose | 9c083b7 | 2011-08-24 18:56:32 +0000 | [diff] [blame] | 2404 | // (Also, having stateful local data means that the same checker can't be |
| 2405 | // used from multiple threads, but a lot of checkers have incorrect |
| 2406 | // assumptions about that anyway. So that wasn't a priority at the time of |
| 2407 | // this fix.) |
Jordy Rose | 1ab51c7 | 2011-08-24 09:27:24 +0000 | [diff] [blame] | 2408 | // |
Jordy Rose | 9c083b7 | 2011-08-24 18:56:32 +0000 | [diff] [blame] | 2409 | // This happens at the end of analysis, but bug reports are emitted /after/ |
| 2410 | // this point. So we can't just clear the summary log now. Instead, we mark |
| 2411 | // that the next time we access the summary log, it should be cleared. |
| 2412 | |
| 2413 | // If we never reset the summary log during /this/ code body analysis, |
| 2414 | // there were no new summaries. There might still have been summaries from |
| 2415 | // the /last/ analysis, so clear them out to make sure the bug report |
| 2416 | // visitors don't get confused. |
| 2417 | if (ShouldResetSummaryLog) |
| 2418 | SummaryLog.clear(); |
| 2419 | |
| 2420 | ShouldResetSummaryLog = !SummaryLog.empty(); |
Jordy Rose | 1ab51c7 | 2011-08-24 09:27:24 +0000 | [diff] [blame] | 2421 | } |
| 2422 | |
Jordy Rose | 17a38e2 | 2011-09-02 05:55:19 +0000 | [diff] [blame] | 2423 | CFRefBug *getLeakWithinFunctionBug(const LangOptions &LOpts, |
| 2424 | bool GCEnabled) const { |
| 2425 | if (GCEnabled) { |
Jordy Rose | db92bb6 | 2011-08-25 01:14:38 +0000 | [diff] [blame] | 2426 | if (!leakWithinFunctionGC) |
Benjamin Kramer | facde17 | 2012-06-06 17:32:50 +0000 | [diff] [blame] | 2427 | leakWithinFunctionGC.reset(new Leak("Leak of object when using " |
| 2428 | "garbage collection")); |
Jordy Rose | 17a38e2 | 2011-09-02 05:55:19 +0000 | [diff] [blame] | 2429 | return leakWithinFunctionGC.get(); |
Jordy Rose | db92bb6 | 2011-08-25 01:14:38 +0000 | [diff] [blame] | 2430 | } else { |
| 2431 | if (!leakWithinFunction) { |
Douglas Gregor | e289d81 | 2011-09-13 17:21:33 +0000 | [diff] [blame] | 2432 | if (LOpts.getGC() == LangOptions::HybridGC) { |
Benjamin Kramer | facde17 | 2012-06-06 17:32:50 +0000 | [diff] [blame] | 2433 | leakWithinFunction.reset(new Leak("Leak of object when not using " |
| 2434 | "garbage collection (GC) in " |
| 2435 | "dual GC/non-GC code")); |
Jordy Rose | db92bb6 | 2011-08-25 01:14:38 +0000 | [diff] [blame] | 2436 | } else { |
Benjamin Kramer | facde17 | 2012-06-06 17:32:50 +0000 | [diff] [blame] | 2437 | leakWithinFunction.reset(new Leak("Leak")); |
Jordy Rose | db92bb6 | 2011-08-25 01:14:38 +0000 | [diff] [blame] | 2438 | } |
| 2439 | } |
Jordy Rose | 17a38e2 | 2011-09-02 05:55:19 +0000 | [diff] [blame] | 2440 | return leakWithinFunction.get(); |
Jordy Rose | db92bb6 | 2011-08-25 01:14:38 +0000 | [diff] [blame] | 2441 | } |
| 2442 | } |
| 2443 | |
Jordy Rose | 17a38e2 | 2011-09-02 05:55:19 +0000 | [diff] [blame] | 2444 | CFRefBug *getLeakAtReturnBug(const LangOptions &LOpts, bool GCEnabled) const { |
| 2445 | if (GCEnabled) { |
Jordy Rose | db92bb6 | 2011-08-25 01:14:38 +0000 | [diff] [blame] | 2446 | if (!leakAtReturnGC) |
Benjamin Kramer | facde17 | 2012-06-06 17:32:50 +0000 | [diff] [blame] | 2447 | leakAtReturnGC.reset(new Leak("Leak of returned object when using " |
| 2448 | "garbage collection")); |
Jordy Rose | 17a38e2 | 2011-09-02 05:55:19 +0000 | [diff] [blame] | 2449 | return leakAtReturnGC.get(); |
Jordy Rose | db92bb6 | 2011-08-25 01:14:38 +0000 | [diff] [blame] | 2450 | } else { |
| 2451 | if (!leakAtReturn) { |
Douglas Gregor | e289d81 | 2011-09-13 17:21:33 +0000 | [diff] [blame] | 2452 | if (LOpts.getGC() == LangOptions::HybridGC) { |
Benjamin Kramer | facde17 | 2012-06-06 17:32:50 +0000 | [diff] [blame] | 2453 | leakAtReturn.reset(new Leak("Leak of returned object when not using " |
| 2454 | "garbage collection (GC) in dual " |
| 2455 | "GC/non-GC code")); |
Jordy Rose | db92bb6 | 2011-08-25 01:14:38 +0000 | [diff] [blame] | 2456 | } else { |
Benjamin Kramer | facde17 | 2012-06-06 17:32:50 +0000 | [diff] [blame] | 2457 | leakAtReturn.reset(new Leak("Leak of returned object")); |
Jordy Rose | db92bb6 | 2011-08-25 01:14:38 +0000 | [diff] [blame] | 2458 | } |
| 2459 | } |
Jordy Rose | 17a38e2 | 2011-09-02 05:55:19 +0000 | [diff] [blame] | 2460 | return leakAtReturn.get(); |
Jordy Rose | db92bb6 | 2011-08-25 01:14:38 +0000 | [diff] [blame] | 2461 | } |
| 2462 | } |
| 2463 | |
Jordy Rose | 17a38e2 | 2011-09-02 05:55:19 +0000 | [diff] [blame] | 2464 | RetainSummaryManager &getSummaryManager(ASTContext &Ctx, |
| 2465 | bool GCEnabled) const { |
| 2466 | // FIXME: We don't support ARC being turned on and off during one analysis. |
| 2467 | // (nor, for that matter, do we support changing ASTContexts) |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2468 | bool ARCEnabled = (bool)Ctx.getLangOpts().ObjCAutoRefCount; |
Jordy Rose | 17a38e2 | 2011-09-02 05:55:19 +0000 | [diff] [blame] | 2469 | if (GCEnabled) { |
| 2470 | if (!SummariesGC) |
Jordy Rose | b6cfc09 | 2011-08-25 00:10:37 +0000 | [diff] [blame] | 2471 | SummariesGC.reset(new RetainSummaryManager(Ctx, true, ARCEnabled)); |
Jordy Rose | 17a38e2 | 2011-09-02 05:55:19 +0000 | [diff] [blame] | 2472 | else |
| 2473 | assert(SummariesGC->isARCEnabled() == ARCEnabled); |
Jordy Rose | b6cfc09 | 2011-08-25 00:10:37 +0000 | [diff] [blame] | 2474 | return *SummariesGC; |
| 2475 | } else { |
Jordy Rose | 17a38e2 | 2011-09-02 05:55:19 +0000 | [diff] [blame] | 2476 | if (!Summaries) |
Jordy Rose | b6cfc09 | 2011-08-25 00:10:37 +0000 | [diff] [blame] | 2477 | Summaries.reset(new RetainSummaryManager(Ctx, false, ARCEnabled)); |
Jordy Rose | 17a38e2 | 2011-09-02 05:55:19 +0000 | [diff] [blame] | 2478 | else |
| 2479 | assert(Summaries->isARCEnabled() == ARCEnabled); |
Jordy Rose | b6cfc09 | 2011-08-25 00:10:37 +0000 | [diff] [blame] | 2480 | return *Summaries; |
| 2481 | } |
| 2482 | } |
| 2483 | |
Jordy Rose | 17a38e2 | 2011-09-02 05:55:19 +0000 | [diff] [blame] | 2484 | RetainSummaryManager &getSummaryManager(CheckerContext &C) const { |
| 2485 | return getSummaryManager(C.getASTContext(), C.isObjCGCEnabled()); |
| 2486 | } |
| 2487 | |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 2488 | void printState(raw_ostream &Out, ProgramStateRef State, |
Jordy Rose | dbd658e | 2011-08-28 19:11:56 +0000 | [diff] [blame] | 2489 | const char *NL, const char *Sep) const; |
| 2490 | |
Anna Zaks | 390909c | 2011-10-06 00:43:15 +0000 | [diff] [blame] | 2491 | void checkBind(SVal loc, SVal val, const Stmt *S, CheckerContext &C) const; |
Jordy Rose | ab027fd | 2011-08-20 21:16:58 +0000 | [diff] [blame] | 2492 | void checkPostStmt(const BlockExpr *BE, CheckerContext &C) const; |
| 2493 | void checkPostStmt(const CastExpr *CE, CheckerContext &C) const; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2494 | |
Ted Kremenek | 1a45a5f | 2012-03-06 20:06:12 +0000 | [diff] [blame] | 2495 | void checkPostStmt(const ObjCArrayLiteral *AL, CheckerContext &C) const; |
| 2496 | void checkPostStmt(const ObjCDictionaryLiteral *DL, CheckerContext &C) const; |
Jordy Rose | 70fdbc3 | 2012-05-12 05:10:43 +0000 | [diff] [blame] | 2497 | void checkPostStmt(const ObjCBoxedExpr *BE, CheckerContext &C) const; |
| 2498 | |
Jordan Rose | fe6a011 | 2012-07-02 19:28:21 +0000 | [diff] [blame] | 2499 | void checkPostCall(const CallEvent &Call, CheckerContext &C) const; |
Ted Kremenek | 1a45a5f | 2012-03-06 20:06:12 +0000 | [diff] [blame] | 2500 | |
Jordan Rose | 4531b7d | 2012-07-02 19:27:43 +0000 | [diff] [blame] | 2501 | void checkSummary(const RetainSummary &Summ, const CallEvent &Call, |
Jordy Rose | e38dd95 | 2011-08-28 05:16:28 +0000 | [diff] [blame] | 2502 | CheckerContext &C) const; |
Jordy Rose | 294396b | 2011-08-22 23:48:23 +0000 | [diff] [blame] | 2503 | |
Anna Zaks | 554067f | 2012-08-29 23:23:43 +0000 | [diff] [blame] | 2504 | void processSummaryOfInlined(const RetainSummary &Summ, |
| 2505 | const CallEvent &Call, |
| 2506 | CheckerContext &C) const; |
| 2507 | |
Jordy Rose | 76c506f | 2011-08-21 21:58:18 +0000 | [diff] [blame] | 2508 | bool evalCall(const CallExpr *CE, CheckerContext &C) const; |
| 2509 | |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 2510 | ProgramStateRef evalAssume(ProgramStateRef state, SVal Cond, |
Jordy Rose | ab027fd | 2011-08-20 21:16:58 +0000 | [diff] [blame] | 2511 | bool Assumption) const; |
Jordy Rose | 6704429 | 2011-08-17 21:27:39 +0000 | [diff] [blame] | 2512 | |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 2513 | ProgramStateRef |
| 2514 | checkRegionChanges(ProgramStateRef state, |
Anna Zaks | bf53dfa | 2012-12-20 00:38:25 +0000 | [diff] [blame^] | 2515 | const InvalidatedSymbols *invalidated, |
Jordy Rose | 537716a | 2011-08-27 22:51:26 +0000 | [diff] [blame] | 2516 | ArrayRef<const MemRegion *> ExplicitRegions, |
Anna Zaks | 66c4040 | 2012-02-14 21:55:24 +0000 | [diff] [blame] | 2517 | ArrayRef<const MemRegion *> Regions, |
Jordan Rose | 740d490 | 2012-07-02 19:27:35 +0000 | [diff] [blame] | 2518 | const CallEvent *Call) const; |
Jordy Rose | ab027fd | 2011-08-20 21:16:58 +0000 | [diff] [blame] | 2519 | |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 2520 | bool wantsRegionChangeUpdate(ProgramStateRef state) const { |
Jordy Rose | 2f9a66d | 2011-08-20 21:17:59 +0000 | [diff] [blame] | 2521 | return true; |
Jordy Rose | ab027fd | 2011-08-20 21:16:58 +0000 | [diff] [blame] | 2522 | } |
Jordy Rose | 294396b | 2011-08-22 23:48:23 +0000 | [diff] [blame] | 2523 | |
Jordy Rose | f53e8c7 | 2011-08-23 19:43:16 +0000 | [diff] [blame] | 2524 | void checkPreStmt(const ReturnStmt *S, CheckerContext &C) const; |
| 2525 | void checkReturnWithRetEffect(const ReturnStmt *S, CheckerContext &C, |
| 2526 | ExplodedNode *Pred, RetEffect RE, RefVal X, |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 2527 | SymbolRef Sym, ProgramStateRef state) const; |
Jordy Rose | f53e8c7 | 2011-08-23 19:43:16 +0000 | [diff] [blame] | 2528 | |
Jordy Rose | 38f17d6 | 2011-08-23 19:01:07 +0000 | [diff] [blame] | 2529 | void checkDeadSymbols(SymbolReaper &SymReaper, CheckerContext &C) const; |
Anna Zaks | af498a2 | 2011-10-25 19:56:48 +0000 | [diff] [blame] | 2530 | void checkEndPath(CheckerContext &C) const; |
Jordy Rose | 38f17d6 | 2011-08-23 19:01:07 +0000 | [diff] [blame] | 2531 | |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 2532 | ProgramStateRef updateSymbol(ProgramStateRef state, SymbolRef sym, |
Anna Zaks | 554067f | 2012-08-29 23:23:43 +0000 | [diff] [blame] | 2533 | RefVal V, ArgEffect E, RefVal::Kind &hasErr, |
| 2534 | CheckerContext &C) const; |
Jordy Rose | e0a5d32 | 2011-08-23 20:27:16 +0000 | [diff] [blame] | 2535 | |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 2536 | void processNonLeakError(ProgramStateRef St, SourceRange ErrorRange, |
Jordy Rose | 294396b | 2011-08-22 23:48:23 +0000 | [diff] [blame] | 2537 | RefVal::Kind ErrorKind, SymbolRef Sym, |
| 2538 | CheckerContext &C) const; |
Ted Kremenek | 1a45a5f | 2012-03-06 20:06:12 +0000 | [diff] [blame] | 2539 | |
| 2540 | void processObjCLiterals(CheckerContext &C, const Expr *Ex) const; |
Jordy Rose | 294396b | 2011-08-22 23:48:23 +0000 | [diff] [blame] | 2541 | |
Jordy Rose | 38f17d6 | 2011-08-23 19:01:07 +0000 | [diff] [blame] | 2542 | const ProgramPointTag *getDeadSymbolTag(SymbolRef sym) const; |
| 2543 | |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 2544 | ProgramStateRef handleSymbolDeath(ProgramStateRef state, |
Anna Zaks | 8d6b43c | 2012-08-14 00:36:15 +0000 | [diff] [blame] | 2545 | SymbolRef sid, RefVal V, |
| 2546 | SmallVectorImpl<SymbolRef> &Leaked) const; |
Jordy Rose | 38f17d6 | 2011-08-23 19:01:07 +0000 | [diff] [blame] | 2547 | |
Jordan Rose | 4ee1c55 | 2012-12-06 18:58:18 +0000 | [diff] [blame] | 2548 | ProgramStateRef |
Jordan Rose | 2bce86c | 2012-08-18 00:30:16 +0000 | [diff] [blame] | 2549 | handleAutoreleaseCounts(ProgramStateRef state, ExplodedNode *Pred, |
| 2550 | const ProgramPointTag *Tag, CheckerContext &Ctx, |
| 2551 | SymbolRef Sym, RefVal V) const; |
Jordy Rose | 8d22863 | 2011-08-23 20:07:14 +0000 | [diff] [blame] | 2552 | |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 2553 | ExplodedNode *processLeaks(ProgramStateRef state, |
Jordy Rose | 38f17d6 | 2011-08-23 19:01:07 +0000 | [diff] [blame] | 2554 | SmallVectorImpl<SymbolRef> &Leaked, |
Anna Zaks | 6a93bd5 | 2011-10-25 19:57:11 +0000 | [diff] [blame] | 2555 | CheckerContext &Ctx, |
Jordy Rose | 38f17d6 | 2011-08-23 19:01:07 +0000 | [diff] [blame] | 2556 | ExplodedNode *Pred = 0) const; |
Ted Kremenek | d593eb9 | 2009-11-25 22:17:44 +0000 | [diff] [blame] | 2557 | }; |
| 2558 | } // end anonymous namespace |
| 2559 | |
Jordy Rose | 6704429 | 2011-08-17 21:27:39 +0000 | [diff] [blame] | 2560 | namespace { |
| 2561 | class StopTrackingCallback : public SymbolVisitor { |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 2562 | ProgramStateRef state; |
Jordy Rose | 6704429 | 2011-08-17 21:27:39 +0000 | [diff] [blame] | 2563 | public: |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 2564 | StopTrackingCallback(ProgramStateRef st) : state(st) {} |
| 2565 | ProgramStateRef getState() const { return state; } |
Jordy Rose | 6704429 | 2011-08-17 21:27:39 +0000 | [diff] [blame] | 2566 | |
| 2567 | bool VisitSymbol(SymbolRef sym) { |
| 2568 | state = state->remove<RefBindings>(sym); |
| 2569 | return true; |
| 2570 | } |
| 2571 | }; |
| 2572 | } // end anonymous namespace |
| 2573 | |
Jordy Rose | 910c405 | 2011-09-02 06:44:22 +0000 | [diff] [blame] | 2574 | //===----------------------------------------------------------------------===// |
| 2575 | // Handle statements that may have an effect on refcounts. |
| 2576 | //===----------------------------------------------------------------------===// |
Jordy Rose | 6704429 | 2011-08-17 21:27:39 +0000 | [diff] [blame] | 2577 | |
Jordy Rose | 910c405 | 2011-09-02 06:44:22 +0000 | [diff] [blame] | 2578 | void RetainCountChecker::checkPostStmt(const BlockExpr *BE, |
| 2579 | CheckerContext &C) const { |
Jordy Rose | 6704429 | 2011-08-17 21:27:39 +0000 | [diff] [blame] | 2580 | |
Jordy Rose | 910c405 | 2011-09-02 06:44:22 +0000 | [diff] [blame] | 2581 | // Scan the BlockDecRefExprs for any object the retain count checker |
Ted Kremenek | dcee3ce | 2010-07-01 20:16:50 +0000 | [diff] [blame] | 2582 | // may be tracking. |
John McCall | 469a1eb | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 2583 | if (!BE->getBlockDecl()->hasCaptures()) |
Ted Kremenek | 38cc6bc | 2009-11-26 02:38:19 +0000 | [diff] [blame] | 2584 | return; |
Ted Kremenek | dcee3ce | 2010-07-01 20:16:50 +0000 | [diff] [blame] | 2585 | |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 2586 | ProgramStateRef state = C.getState(); |
Ted Kremenek | 38cc6bc | 2009-11-26 02:38:19 +0000 | [diff] [blame] | 2587 | const BlockDataRegion *R = |
Ted Kremenek | 5eca482 | 2012-01-06 22:09:28 +0000 | [diff] [blame] | 2588 | cast<BlockDataRegion>(state->getSVal(BE, |
| 2589 | C.getLocationContext()).getAsRegion()); |
Ted Kremenek | dcee3ce | 2010-07-01 20:16:50 +0000 | [diff] [blame] | 2590 | |
Ted Kremenek | 38cc6bc | 2009-11-26 02:38:19 +0000 | [diff] [blame] | 2591 | BlockDataRegion::referenced_vars_iterator I = R->referenced_vars_begin(), |
| 2592 | E = R->referenced_vars_end(); |
Ted Kremenek | dcee3ce | 2010-07-01 20:16:50 +0000 | [diff] [blame] | 2593 | |
Ted Kremenek | 38cc6bc | 2009-11-26 02:38:19 +0000 | [diff] [blame] | 2594 | if (I == E) |
| 2595 | return; |
Ted Kremenek | dcee3ce | 2010-07-01 20:16:50 +0000 | [diff] [blame] | 2596 | |
Ted Kremenek | 67d1287 | 2009-12-07 22:05:27 +0000 | [diff] [blame] | 2597 | // FIXME: For now we invalidate the tracking of all symbols passed to blocks |
| 2598 | // via captured variables, even though captured variables result in a copy |
| 2599 | // and in implicit increment/decrement of a retain count. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2600 | SmallVector<const MemRegion*, 10> Regions; |
Anna Zaks | 39ac187 | 2011-10-26 21:06:44 +0000 | [diff] [blame] | 2601 | const LocationContext *LC = C.getLocationContext(); |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 2602 | MemRegionManager &MemMgr = C.getSValBuilder().getRegionManager(); |
Ted Kremenek | dcee3ce | 2010-07-01 20:16:50 +0000 | [diff] [blame] | 2603 | |
Ted Kremenek | 67d1287 | 2009-12-07 22:05:27 +0000 | [diff] [blame] | 2604 | for ( ; I != E; ++I) { |
Ted Kremenek | e3ce2c1 | 2012-12-06 07:17:20 +0000 | [diff] [blame] | 2605 | const VarRegion *VR = I.getCapturedRegion(); |
Ted Kremenek | 67d1287 | 2009-12-07 22:05:27 +0000 | [diff] [blame] | 2606 | if (VR->getSuperRegion() == R) { |
| 2607 | VR = MemMgr.getVarRegion(VR->getDecl(), LC); |
| 2608 | } |
| 2609 | Regions.push_back(VR); |
| 2610 | } |
Ted Kremenek | dcee3ce | 2010-07-01 20:16:50 +0000 | [diff] [blame] | 2611 | |
Ted Kremenek | 67d1287 | 2009-12-07 22:05:27 +0000 | [diff] [blame] | 2612 | state = |
| 2613 | state->scanReachableSymbols<StopTrackingCallback>(Regions.data(), |
| 2614 | Regions.data() + Regions.size()).getState(); |
Anna Zaks | 0bd6b11 | 2011-10-26 21:06:34 +0000 | [diff] [blame] | 2615 | C.addTransition(state); |
Ted Kremenek | 38cc6bc | 2009-11-26 02:38:19 +0000 | [diff] [blame] | 2616 | } |
| 2617 | |
Jordy Rose | 910c405 | 2011-09-02 06:44:22 +0000 | [diff] [blame] | 2618 | void RetainCountChecker::checkPostStmt(const CastExpr *CE, |
| 2619 | CheckerContext &C) const { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2620 | const ObjCBridgedCastExpr *BE = dyn_cast<ObjCBridgedCastExpr>(CE); |
| 2621 | if (!BE) |
| 2622 | return; |
| 2623 | |
John McCall | 71c482c | 2011-06-17 06:50:50 +0000 | [diff] [blame] | 2624 | ArgEffect AE = IncRef; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2625 | |
| 2626 | switch (BE->getBridgeKind()) { |
| 2627 | case clang::OBC_Bridge: |
| 2628 | // Do nothing. |
| 2629 | return; |
| 2630 | case clang::OBC_BridgeRetained: |
| 2631 | AE = IncRef; |
| 2632 | break; |
| 2633 | case clang::OBC_BridgeTransfer: |
| 2634 | AE = DecRefBridgedTransfered; |
| 2635 | break; |
| 2636 | } |
| 2637 | |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 2638 | ProgramStateRef state = C.getState(); |
Ted Kremenek | 5eca482 | 2012-01-06 22:09:28 +0000 | [diff] [blame] | 2639 | SymbolRef Sym = state->getSVal(CE, C.getLocationContext()).getAsLocSymbol(); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2640 | if (!Sym) |
| 2641 | return; |
Anna Zaks | 8d6b43c | 2012-08-14 00:36:15 +0000 | [diff] [blame] | 2642 | const RefVal* T = getRefBinding(state, Sym); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2643 | if (!T) |
| 2644 | return; |
| 2645 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2646 | RefVal::Kind hasErr = (RefVal::Kind) 0; |
Jordy Rose | 17a38e2 | 2011-09-02 05:55:19 +0000 | [diff] [blame] | 2647 | state = updateSymbol(state, Sym, *T, AE, hasErr, C); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2648 | |
| 2649 | if (hasErr) { |
Jordy Rose | e0a5d32 | 2011-08-23 20:27:16 +0000 | [diff] [blame] | 2650 | // FIXME: If we get an error during a bridge cast, should we report it? |
| 2651 | // Should we assert that there is no error? |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2652 | return; |
| 2653 | } |
| 2654 | |
Anna Zaks | 0bd6b11 | 2011-10-26 21:06:34 +0000 | [diff] [blame] | 2655 | C.addTransition(state); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2656 | } |
| 2657 | |
Ted Kremenek | 1a45a5f | 2012-03-06 20:06:12 +0000 | [diff] [blame] | 2658 | void RetainCountChecker::processObjCLiterals(CheckerContext &C, |
| 2659 | const Expr *Ex) const { |
| 2660 | ProgramStateRef state = C.getState(); |
| 2661 | const ExplodedNode *pred = C.getPredecessor(); |
| 2662 | for (Stmt::const_child_iterator it = Ex->child_begin(), et = Ex->child_end() ; |
| 2663 | it != et ; ++it) { |
| 2664 | const Stmt *child = *it; |
| 2665 | SVal V = state->getSVal(child, pred->getLocationContext()); |
| 2666 | if (SymbolRef sym = V.getAsSymbol()) |
Anna Zaks | 8d6b43c | 2012-08-14 00:36:15 +0000 | [diff] [blame] | 2667 | if (const RefVal* T = getRefBinding(state, sym)) { |
Ted Kremenek | 1a45a5f | 2012-03-06 20:06:12 +0000 | [diff] [blame] | 2668 | RefVal::Kind hasErr = (RefVal::Kind) 0; |
| 2669 | state = updateSymbol(state, sym, *T, MayEscape, hasErr, C); |
| 2670 | if (hasErr) { |
| 2671 | processNonLeakError(state, child->getSourceRange(), hasErr, sym, C); |
| 2672 | return; |
| 2673 | } |
| 2674 | } |
| 2675 | } |
| 2676 | |
| 2677 | // Return the object as autoreleased. |
| 2678 | // RetEffect RE = RetEffect::MakeNotOwned(RetEffect::ObjC); |
| 2679 | if (SymbolRef sym = |
| 2680 | state->getSVal(Ex, pred->getLocationContext()).getAsSymbol()) { |
| 2681 | QualType ResultTy = Ex->getType(); |
Anna Zaks | 8d6b43c | 2012-08-14 00:36:15 +0000 | [diff] [blame] | 2682 | state = setRefBinding(state, sym, |
| 2683 | RefVal::makeNotOwned(RetEffect::ObjC, ResultTy)); |
Ted Kremenek | 1a45a5f | 2012-03-06 20:06:12 +0000 | [diff] [blame] | 2684 | } |
| 2685 | |
| 2686 | C.addTransition(state); |
| 2687 | } |
| 2688 | |
| 2689 | void RetainCountChecker::checkPostStmt(const ObjCArrayLiteral *AL, |
| 2690 | CheckerContext &C) const { |
| 2691 | // Apply the 'MayEscape' to all values. |
| 2692 | processObjCLiterals(C, AL); |
| 2693 | } |
| 2694 | |
| 2695 | void RetainCountChecker::checkPostStmt(const ObjCDictionaryLiteral *DL, |
| 2696 | CheckerContext &C) const { |
| 2697 | // Apply the 'MayEscape' to all keys and values. |
| 2698 | processObjCLiterals(C, DL); |
| 2699 | } |
| 2700 | |
Jordy Rose | 70fdbc3 | 2012-05-12 05:10:43 +0000 | [diff] [blame] | 2701 | void RetainCountChecker::checkPostStmt(const ObjCBoxedExpr *Ex, |
| 2702 | CheckerContext &C) const { |
| 2703 | const ExplodedNode *Pred = C.getPredecessor(); |
| 2704 | const LocationContext *LCtx = Pred->getLocationContext(); |
| 2705 | ProgramStateRef State = Pred->getState(); |
| 2706 | |
| 2707 | if (SymbolRef Sym = State->getSVal(Ex, LCtx).getAsSymbol()) { |
| 2708 | QualType ResultTy = Ex->getType(); |
Anna Zaks | 8d6b43c | 2012-08-14 00:36:15 +0000 | [diff] [blame] | 2709 | State = setRefBinding(State, Sym, |
| 2710 | RefVal::makeNotOwned(RetEffect::ObjC, ResultTy)); |
Jordy Rose | 70fdbc3 | 2012-05-12 05:10:43 +0000 | [diff] [blame] | 2711 | } |
| 2712 | |
| 2713 | C.addTransition(State); |
| 2714 | } |
| 2715 | |
Jordan Rose | fe6a011 | 2012-07-02 19:28:21 +0000 | [diff] [blame] | 2716 | void RetainCountChecker::checkPostCall(const CallEvent &Call, |
| 2717 | CheckerContext &C) const { |
Jordan Rose | fe6a011 | 2012-07-02 19:28:21 +0000 | [diff] [blame] | 2718 | RetainSummaryManager &Summaries = getSummaryManager(C); |
| 2719 | const RetainSummary *Summ = Summaries.getSummary(Call, C.getState()); |
Anna Zaks | 554067f | 2012-08-29 23:23:43 +0000 | [diff] [blame] | 2720 | |
| 2721 | if (C.wasInlined) { |
| 2722 | processSummaryOfInlined(*Summ, Call, C); |
| 2723 | return; |
| 2724 | } |
Jordan Rose | fe6a011 | 2012-07-02 19:28:21 +0000 | [diff] [blame] | 2725 | checkSummary(*Summ, Call, C); |
Jordy Rose | 294396b | 2011-08-22 23:48:23 +0000 | [diff] [blame] | 2726 | } |
| 2727 | |
Jordy Rose | 910c405 | 2011-09-02 06:44:22 +0000 | [diff] [blame] | 2728 | /// GetReturnType - Used to get the return type of a message expression or |
| 2729 | /// function call with the intention of affixing that type to a tracked symbol. |
Sylvestre Ledru | bed28ac | 2012-07-23 08:59:39 +0000 | [diff] [blame] | 2730 | /// While the return type can be queried directly from RetEx, when |
Jordy Rose | 910c405 | 2011-09-02 06:44:22 +0000 | [diff] [blame] | 2731 | /// invoking class methods we augment to the return type to be that of |
| 2732 | /// a pointer to the class (as opposed it just being id). |
| 2733 | // FIXME: We may be able to do this with related result types instead. |
| 2734 | // This function is probably overestimating. |
| 2735 | static QualType GetReturnType(const Expr *RetE, ASTContext &Ctx) { |
| 2736 | QualType RetTy = RetE->getType(); |
| 2737 | // If RetE is not a message expression just return its type. |
| 2738 | // If RetE is a message expression, return its types if it is something |
| 2739 | /// more specific than id. |
| 2740 | if (const ObjCMessageExpr *ME = dyn_cast<ObjCMessageExpr>(RetE)) |
| 2741 | if (const ObjCObjectPointerType *PT = RetTy->getAs<ObjCObjectPointerType>()) |
| 2742 | if (PT->isObjCQualifiedIdType() || PT->isObjCIdType() || |
| 2743 | PT->isObjCClassType()) { |
| 2744 | // At this point we know the return type of the message expression is |
| 2745 | // id, id<...>, or Class. If we have an ObjCInterfaceDecl, we know this |
| 2746 | // is a call to a class method whose type we can resolve. In such |
| 2747 | // cases, promote the return type to XXX* (where XXX is the class). |
| 2748 | const ObjCInterfaceDecl *D = ME->getReceiverInterface(); |
| 2749 | return !D ? RetTy : |
| 2750 | Ctx.getObjCObjectPointerType(Ctx.getObjCInterfaceType(D)); |
| 2751 | } |
| 2752 | |
| 2753 | return RetTy; |
| 2754 | } |
| 2755 | |
Anna Zaks | 554067f | 2012-08-29 23:23:43 +0000 | [diff] [blame] | 2756 | // We don't always get the exact modeling of the function with regards to the |
| 2757 | // retain count checker even when the function is inlined. For example, we need |
| 2758 | // to stop tracking the symbols which were marked with StopTrackingHard. |
| 2759 | void RetainCountChecker::processSummaryOfInlined(const RetainSummary &Summ, |
| 2760 | const CallEvent &CallOrMsg, |
| 2761 | CheckerContext &C) const { |
| 2762 | ProgramStateRef state = C.getState(); |
| 2763 | |
| 2764 | // Evaluate the effect of the arguments. |
| 2765 | for (unsigned idx = 0, e = CallOrMsg.getNumArgs(); idx != e; ++idx) { |
| 2766 | if (Summ.getArg(idx) == StopTrackingHard) { |
| 2767 | SVal V = CallOrMsg.getArgSVal(idx); |
| 2768 | if (SymbolRef Sym = V.getAsLocSymbol()) { |
| 2769 | state = removeRefBinding(state, Sym); |
| 2770 | } |
| 2771 | } |
| 2772 | } |
| 2773 | |
| 2774 | // Evaluate the effect on the message receiver. |
| 2775 | const ObjCMethodCall *MsgInvocation = dyn_cast<ObjCMethodCall>(&CallOrMsg); |
| 2776 | if (MsgInvocation) { |
| 2777 | if (SymbolRef Sym = MsgInvocation->getReceiverSVal().getAsLocSymbol()) { |
| 2778 | if (Summ.getReceiverEffect() == StopTrackingHard) { |
| 2779 | state = removeRefBinding(state, Sym); |
| 2780 | } |
| 2781 | } |
| 2782 | } |
| 2783 | |
| 2784 | // Consult the summary for the return value. |
| 2785 | RetEffect RE = Summ.getRetEffect(); |
| 2786 | if (RE.getKind() == RetEffect::NoRetHard) { |
Jordan Rose | 2f3017f | 2012-11-02 23:49:29 +0000 | [diff] [blame] | 2787 | SymbolRef Sym = CallOrMsg.getReturnValue().getAsSymbol(); |
Anna Zaks | 554067f | 2012-08-29 23:23:43 +0000 | [diff] [blame] | 2788 | if (Sym) |
| 2789 | state = removeRefBinding(state, Sym); |
| 2790 | } |
| 2791 | |
| 2792 | C.addTransition(state); |
| 2793 | } |
| 2794 | |
Jordy Rose | 910c405 | 2011-09-02 06:44:22 +0000 | [diff] [blame] | 2795 | void RetainCountChecker::checkSummary(const RetainSummary &Summ, |
Jordan Rose | 4531b7d | 2012-07-02 19:27:43 +0000 | [diff] [blame] | 2796 | const CallEvent &CallOrMsg, |
Jordy Rose | 910c405 | 2011-09-02 06:44:22 +0000 | [diff] [blame] | 2797 | CheckerContext &C) const { |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 2798 | ProgramStateRef state = C.getState(); |
Jordy Rose | 294396b | 2011-08-22 23:48:23 +0000 | [diff] [blame] | 2799 | |
| 2800 | // Evaluate the effect of the arguments. |
| 2801 | RefVal::Kind hasErr = (RefVal::Kind) 0; |
| 2802 | SourceRange ErrorRange; |
| 2803 | SymbolRef ErrorSym = 0; |
| 2804 | |
| 2805 | for (unsigned idx = 0, e = CallOrMsg.getNumArgs(); idx != e; ++idx) { |
Jordy Rose | 537716a | 2011-08-27 22:51:26 +0000 | [diff] [blame] | 2806 | SVal V = CallOrMsg.getArgSVal(idx); |
Jordy Rose | 294396b | 2011-08-22 23:48:23 +0000 | [diff] [blame] | 2807 | |
| 2808 | if (SymbolRef Sym = V.getAsLocSymbol()) { |
Anna Zaks | 8d6b43c | 2012-08-14 00:36:15 +0000 | [diff] [blame] | 2809 | if (const RefVal *T = getRefBinding(state, Sym)) { |
Jordy Rose | 17a38e2 | 2011-09-02 05:55:19 +0000 | [diff] [blame] | 2810 | state = updateSymbol(state, Sym, *T, Summ.getArg(idx), hasErr, C); |
Jordy Rose | 294396b | 2011-08-22 23:48:23 +0000 | [diff] [blame] | 2811 | if (hasErr) { |
| 2812 | ErrorRange = CallOrMsg.getArgSourceRange(idx); |
| 2813 | ErrorSym = Sym; |
| 2814 | break; |
| 2815 | } |
| 2816 | } |
| 2817 | } |
| 2818 | } |
| 2819 | |
| 2820 | // Evaluate the effect on the message receiver. |
| 2821 | bool ReceiverIsTracked = false; |
Jordan Rose | 4531b7d | 2012-07-02 19:27:43 +0000 | [diff] [blame] | 2822 | if (!hasErr) { |
Jordan Rose | cde8cdb | 2012-07-02 19:27:56 +0000 | [diff] [blame] | 2823 | const ObjCMethodCall *MsgInvocation = dyn_cast<ObjCMethodCall>(&CallOrMsg); |
Jordan Rose | 4531b7d | 2012-07-02 19:27:43 +0000 | [diff] [blame] | 2824 | if (MsgInvocation) { |
| 2825 | if (SymbolRef Sym = MsgInvocation->getReceiverSVal().getAsLocSymbol()) { |
Anna Zaks | 8d6b43c | 2012-08-14 00:36:15 +0000 | [diff] [blame] | 2826 | if (const RefVal *T = getRefBinding(state, Sym)) { |
Jordan Rose | 4531b7d | 2012-07-02 19:27:43 +0000 | [diff] [blame] | 2827 | ReceiverIsTracked = true; |
| 2828 | state = updateSymbol(state, Sym, *T, Summ.getReceiverEffect(), |
Anna Zaks | 554067f | 2012-08-29 23:23:43 +0000 | [diff] [blame] | 2829 | hasErr, C); |
Jordan Rose | 4531b7d | 2012-07-02 19:27:43 +0000 | [diff] [blame] | 2830 | if (hasErr) { |
Jordan Rose | 8919e68 | 2012-07-18 21:59:51 +0000 | [diff] [blame] | 2831 | ErrorRange = MsgInvocation->getOriginExpr()->getReceiverRange(); |
Jordan Rose | 4531b7d | 2012-07-02 19:27:43 +0000 | [diff] [blame] | 2832 | ErrorSym = Sym; |
| 2833 | } |
Jordy Rose | 294396b | 2011-08-22 23:48:23 +0000 | [diff] [blame] | 2834 | } |
| 2835 | } |
| 2836 | } |
| 2837 | } |
| 2838 | |
| 2839 | // Process any errors. |
| 2840 | if (hasErr) { |
| 2841 | processNonLeakError(state, ErrorRange, hasErr, ErrorSym, C); |
| 2842 | return; |
| 2843 | } |
| 2844 | |
| 2845 | // Consult the summary for the return value. |
| 2846 | RetEffect RE = Summ.getRetEffect(); |
| 2847 | |
| 2848 | if (RE.getKind() == RetEffect::OwnedWhenTrackedReceiver) { |
Jordy Rose | b6cfc09 | 2011-08-25 00:10:37 +0000 | [diff] [blame] | 2849 | if (ReceiverIsTracked) |
Jordy Rose | 17a38e2 | 2011-09-02 05:55:19 +0000 | [diff] [blame] | 2850 | RE = getSummaryManager(C).getObjAllocRetEffect(); |
Jordy Rose | b6cfc09 | 2011-08-25 00:10:37 +0000 | [diff] [blame] | 2851 | else |
Jordy Rose | 294396b | 2011-08-22 23:48:23 +0000 | [diff] [blame] | 2852 | RE = RetEffect::MakeNoRet(); |
| 2853 | } |
| 2854 | |
| 2855 | switch (RE.getKind()) { |
| 2856 | default: |
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 2857 | llvm_unreachable("Unhandled RetEffect."); |
Jordy Rose | 294396b | 2011-08-22 23:48:23 +0000 | [diff] [blame] | 2858 | |
| 2859 | case RetEffect::NoRet: |
Anna Zaks | 554067f | 2012-08-29 23:23:43 +0000 | [diff] [blame] | 2860 | case RetEffect::NoRetHard: |
Jordy Rose | 294396b | 2011-08-22 23:48:23 +0000 | [diff] [blame] | 2861 | // No work necessary. |
| 2862 | break; |
| 2863 | |
| 2864 | case RetEffect::OwnedAllocatedSymbol: |
| 2865 | case RetEffect::OwnedSymbol: { |
Jordan Rose | 2f3017f | 2012-11-02 23:49:29 +0000 | [diff] [blame] | 2866 | SymbolRef Sym = CallOrMsg.getReturnValue().getAsSymbol(); |
Jordy Rose | 294396b | 2011-08-22 23:48:23 +0000 | [diff] [blame] | 2867 | if (!Sym) |
| 2868 | break; |
| 2869 | |
Jordan Rose | 4531b7d | 2012-07-02 19:27:43 +0000 | [diff] [blame] | 2870 | // Use the result type from the CallEvent as it automatically adjusts |
Jordy Rose | 294396b | 2011-08-22 23:48:23 +0000 | [diff] [blame] | 2871 | // for methods/functions that return references. |
Jordan Rose | 4531b7d | 2012-07-02 19:27:43 +0000 | [diff] [blame] | 2872 | QualType ResultTy = CallOrMsg.getResultType(); |
Anna Zaks | 8d6b43c | 2012-08-14 00:36:15 +0000 | [diff] [blame] | 2873 | state = setRefBinding(state, Sym, RefVal::makeOwned(RE.getObjKind(), |
| 2874 | ResultTy)); |
Jordy Rose | 294396b | 2011-08-22 23:48:23 +0000 | [diff] [blame] | 2875 | |
| 2876 | // FIXME: Add a flag to the checker where allocations are assumed to |
Anna Zaks | c6ba23f | 2012-08-14 15:39:13 +0000 | [diff] [blame] | 2877 | // *not* fail. |
Jordy Rose | 294396b | 2011-08-22 23:48:23 +0000 | [diff] [blame] | 2878 | break; |
| 2879 | } |
| 2880 | |
| 2881 | case RetEffect::GCNotOwnedSymbol: |
| 2882 | case RetEffect::ARCNotOwnedSymbol: |
| 2883 | case RetEffect::NotOwnedSymbol: { |
| 2884 | const Expr *Ex = CallOrMsg.getOriginExpr(); |
Jordan Rose | 2f3017f | 2012-11-02 23:49:29 +0000 | [diff] [blame] | 2885 | SymbolRef Sym = CallOrMsg.getReturnValue().getAsSymbol(); |
Jordy Rose | 294396b | 2011-08-22 23:48:23 +0000 | [diff] [blame] | 2886 | if (!Sym) |
| 2887 | break; |
Ted Kremenek | 7461682 | 2012-10-12 22:56:45 +0000 | [diff] [blame] | 2888 | assert(Ex); |
Jordy Rose | 294396b | 2011-08-22 23:48:23 +0000 | [diff] [blame] | 2889 | // Use GetReturnType in order to give [NSFoo alloc] the type NSFoo *. |
| 2890 | QualType ResultTy = GetReturnType(Ex, C.getASTContext()); |
Anna Zaks | 8d6b43c | 2012-08-14 00:36:15 +0000 | [diff] [blame] | 2891 | state = setRefBinding(state, Sym, RefVal::makeNotOwned(RE.getObjKind(), |
| 2892 | ResultTy)); |
Jordy Rose | 294396b | 2011-08-22 23:48:23 +0000 | [diff] [blame] | 2893 | break; |
| 2894 | } |
| 2895 | } |
| 2896 | |
| 2897 | // This check is actually necessary; otherwise the statement builder thinks |
| 2898 | // we've hit a previously-found path. |
| 2899 | // Normally addTransition takes care of this, but we want the node pointer. |
| 2900 | ExplodedNode *NewNode; |
| 2901 | if (state == C.getState()) { |
| 2902 | NewNode = C.getPredecessor(); |
| 2903 | } else { |
Anna Zaks | 0bd6b11 | 2011-10-26 21:06:34 +0000 | [diff] [blame] | 2904 | NewNode = C.addTransition(state); |
Jordy Rose | 294396b | 2011-08-22 23:48:23 +0000 | [diff] [blame] | 2905 | } |
| 2906 | |
Jordy Rose | 9c083b7 | 2011-08-24 18:56:32 +0000 | [diff] [blame] | 2907 | // Annotate the node with summary we used. |
| 2908 | if (NewNode) { |
| 2909 | // FIXME: This is ugly. See checkEndAnalysis for why it's necessary. |
| 2910 | if (ShouldResetSummaryLog) { |
| 2911 | SummaryLog.clear(); |
| 2912 | ShouldResetSummaryLog = false; |
| 2913 | } |
Jordy Rose | ec9ef85 | 2011-08-23 20:55:48 +0000 | [diff] [blame] | 2914 | SummaryLog[NewNode] = &Summ; |
Jordy Rose | 9c083b7 | 2011-08-24 18:56:32 +0000 | [diff] [blame] | 2915 | } |
Jordy Rose | 294396b | 2011-08-22 23:48:23 +0000 | [diff] [blame] | 2916 | } |
| 2917 | |
Jordy Rose | e0a5d32 | 2011-08-23 20:27:16 +0000 | [diff] [blame] | 2918 | |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 2919 | ProgramStateRef |
| 2920 | RetainCountChecker::updateSymbol(ProgramStateRef state, SymbolRef sym, |
Jordy Rose | 910c405 | 2011-09-02 06:44:22 +0000 | [diff] [blame] | 2921 | RefVal V, ArgEffect E, RefVal::Kind &hasErr, |
| 2922 | CheckerContext &C) const { |
Jordy Rose | e0a5d32 | 2011-08-23 20:27:16 +0000 | [diff] [blame] | 2923 | // In GC mode [... release] and [... retain] do nothing. |
Jordy Rose | 910c405 | 2011-09-02 06:44:22 +0000 | [diff] [blame] | 2924 | // In ARC mode they shouldn't exist at all, but we just ignore them. |
Jordy Rose | 17a38e2 | 2011-09-02 05:55:19 +0000 | [diff] [blame] | 2925 | bool IgnoreRetainMsg = C.isObjCGCEnabled(); |
| 2926 | if (!IgnoreRetainMsg) |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2927 | IgnoreRetainMsg = (bool)C.getASTContext().getLangOpts().ObjCAutoRefCount; |
Jordy Rose | 17a38e2 | 2011-09-02 05:55:19 +0000 | [diff] [blame] | 2928 | |
Jordy Rose | e0a5d32 | 2011-08-23 20:27:16 +0000 | [diff] [blame] | 2929 | switch (E) { |
Jordan Rose | 4531b7d | 2012-07-02 19:27:43 +0000 | [diff] [blame] | 2930 | default: |
| 2931 | break; |
| 2932 | case IncRefMsg: |
| 2933 | E = IgnoreRetainMsg ? DoNothing : IncRef; |
| 2934 | break; |
| 2935 | case DecRefMsg: |
| 2936 | E = IgnoreRetainMsg ? DoNothing : DecRef; |
| 2937 | break; |
Anna Zaks | 554067f | 2012-08-29 23:23:43 +0000 | [diff] [blame] | 2938 | case DecRefMsgAndStopTrackingHard: |
| 2939 | E = IgnoreRetainMsg ? StopTracking : DecRefAndStopTrackingHard; |
Jordan Rose | 4531b7d | 2012-07-02 19:27:43 +0000 | [diff] [blame] | 2940 | break; |
| 2941 | case MakeCollectable: |
| 2942 | E = C.isObjCGCEnabled() ? DecRef : DoNothing; |
| 2943 | break; |
| 2944 | case NewAutoreleasePool: |
| 2945 | E = C.isObjCGCEnabled() ? DoNothing : NewAutoreleasePool; |
| 2946 | break; |
Jordy Rose | e0a5d32 | 2011-08-23 20:27:16 +0000 | [diff] [blame] | 2947 | } |
| 2948 | |
| 2949 | // Handle all use-after-releases. |
Jordy Rose | 17a38e2 | 2011-09-02 05:55:19 +0000 | [diff] [blame] | 2950 | if (!C.isObjCGCEnabled() && V.getKind() == RefVal::Released) { |
Jordy Rose | e0a5d32 | 2011-08-23 20:27:16 +0000 | [diff] [blame] | 2951 | V = V ^ RefVal::ErrorUseAfterRelease; |
| 2952 | hasErr = V.getKind(); |
Anna Zaks | 8d6b43c | 2012-08-14 00:36:15 +0000 | [diff] [blame] | 2953 | return setRefBinding(state, sym, V); |
Jordy Rose | e0a5d32 | 2011-08-23 20:27:16 +0000 | [diff] [blame] | 2954 | } |
| 2955 | |
| 2956 | switch (E) { |
| 2957 | case DecRefMsg: |
| 2958 | case IncRefMsg: |
| 2959 | case MakeCollectable: |
Anna Zaks | 554067f | 2012-08-29 23:23:43 +0000 | [diff] [blame] | 2960 | case DecRefMsgAndStopTrackingHard: |
Jordy Rose | e0a5d32 | 2011-08-23 20:27:16 +0000 | [diff] [blame] | 2961 | llvm_unreachable("DecRefMsg/IncRefMsg/MakeCollectable already converted"); |
Jordy Rose | e0a5d32 | 2011-08-23 20:27:16 +0000 | [diff] [blame] | 2962 | |
| 2963 | case Dealloc: |
| 2964 | // Any use of -dealloc in GC is *bad*. |
Jordy Rose | 17a38e2 | 2011-09-02 05:55:19 +0000 | [diff] [blame] | 2965 | if (C.isObjCGCEnabled()) { |
Jordy Rose | e0a5d32 | 2011-08-23 20:27:16 +0000 | [diff] [blame] | 2966 | V = V ^ RefVal::ErrorDeallocGC; |
| 2967 | hasErr = V.getKind(); |
| 2968 | break; |
| 2969 | } |
| 2970 | |
| 2971 | switch (V.getKind()) { |
| 2972 | default: |
| 2973 | llvm_unreachable("Invalid RefVal state for an explicit dealloc."); |
Jordy Rose | e0a5d32 | 2011-08-23 20:27:16 +0000 | [diff] [blame] | 2974 | case RefVal::Owned: |
| 2975 | // The object immediately transitions to the released state. |
| 2976 | V = V ^ RefVal::Released; |
| 2977 | V.clearCounts(); |
Anna Zaks | 8d6b43c | 2012-08-14 00:36:15 +0000 | [diff] [blame] | 2978 | return setRefBinding(state, sym, V); |
Jordy Rose | e0a5d32 | 2011-08-23 20:27:16 +0000 | [diff] [blame] | 2979 | case RefVal::NotOwned: |
| 2980 | V = V ^ RefVal::ErrorDeallocNotOwned; |
| 2981 | hasErr = V.getKind(); |
| 2982 | break; |
| 2983 | } |
| 2984 | break; |
| 2985 | |
| 2986 | case NewAutoreleasePool: |
Jordy Rose | 17a38e2 | 2011-09-02 05:55:19 +0000 | [diff] [blame] | 2987 | assert(!C.isObjCGCEnabled()); |
Anna Zaks | c95bb76 | 2012-08-14 00:36:17 +0000 | [diff] [blame] | 2988 | return state; |
Jordy Rose | e0a5d32 | 2011-08-23 20:27:16 +0000 | [diff] [blame] | 2989 | |
| 2990 | case MayEscape: |
| 2991 | if (V.getKind() == RefVal::Owned) { |
| 2992 | V = V ^ RefVal::NotOwned; |
| 2993 | break; |
| 2994 | } |
| 2995 | |
| 2996 | // Fall-through. |
| 2997 | |
Jordy Rose | e0a5d32 | 2011-08-23 20:27:16 +0000 | [diff] [blame] | 2998 | case DoNothing: |
| 2999 | return state; |
| 3000 | |
| 3001 | case Autorelease: |
Jordy Rose | 17a38e2 | 2011-09-02 05:55:19 +0000 | [diff] [blame] | 3002 | if (C.isObjCGCEnabled()) |
Jordy Rose | e0a5d32 | 2011-08-23 20:27:16 +0000 | [diff] [blame] | 3003 | return state; |
Jordy Rose | e0a5d32 | 2011-08-23 20:27:16 +0000 | [diff] [blame] | 3004 | // Update the autorelease counts. |
Jordy Rose | e0a5d32 | 2011-08-23 20:27:16 +0000 | [diff] [blame] | 3005 | V = V.autorelease(); |
| 3006 | break; |
| 3007 | |
| 3008 | case StopTracking: |
Anna Zaks | 554067f | 2012-08-29 23:23:43 +0000 | [diff] [blame] | 3009 | case StopTrackingHard: |
Anna Zaks | 8d6b43c | 2012-08-14 00:36:15 +0000 | [diff] [blame] | 3010 | return removeRefBinding(state, sym); |
Jordy Rose | e0a5d32 | 2011-08-23 20:27:16 +0000 | [diff] [blame] | 3011 | |
| 3012 | case IncRef: |
| 3013 | switch (V.getKind()) { |
| 3014 | default: |
| 3015 | llvm_unreachable("Invalid RefVal state for a retain."); |
Jordy Rose | e0a5d32 | 2011-08-23 20:27:16 +0000 | [diff] [blame] | 3016 | case RefVal::Owned: |
| 3017 | case RefVal::NotOwned: |
| 3018 | V = V + 1; |
| 3019 | break; |
| 3020 | case RefVal::Released: |
| 3021 | // Non-GC cases are handled above. |
Jordy Rose | 17a38e2 | 2011-09-02 05:55:19 +0000 | [diff] [blame] | 3022 | assert(C.isObjCGCEnabled()); |
Jordy Rose | e0a5d32 | 2011-08-23 20:27:16 +0000 | [diff] [blame] | 3023 | V = (V ^ RefVal::Owned) + 1; |
| 3024 | break; |
| 3025 | } |
| 3026 | break; |
| 3027 | |
Jordy Rose | e0a5d32 | 2011-08-23 20:27:16 +0000 | [diff] [blame] | 3028 | case DecRef: |
| 3029 | case DecRefBridgedTransfered: |
Anna Zaks | 554067f | 2012-08-29 23:23:43 +0000 | [diff] [blame] | 3030 | case DecRefAndStopTrackingHard: |
Jordy Rose | e0a5d32 | 2011-08-23 20:27:16 +0000 | [diff] [blame] | 3031 | switch (V.getKind()) { |
| 3032 | default: |
| 3033 | // case 'RefVal::Released' handled above. |
| 3034 | llvm_unreachable("Invalid RefVal state for a release."); |
Jordy Rose | e0a5d32 | 2011-08-23 20:27:16 +0000 | [diff] [blame] | 3035 | |
| 3036 | case RefVal::Owned: |
| 3037 | assert(V.getCount() > 0); |
| 3038 | if (V.getCount() == 1) |
| 3039 | V = V ^ (E == DecRefBridgedTransfered ? |
| 3040 | RefVal::NotOwned : RefVal::Released); |
Anna Zaks | 554067f | 2012-08-29 23:23:43 +0000 | [diff] [blame] | 3041 | else if (E == DecRefAndStopTrackingHard) |
Anna Zaks | 8d6b43c | 2012-08-14 00:36:15 +0000 | [diff] [blame] | 3042 | return removeRefBinding(state, sym); |
Jordan Rose | 4531b7d | 2012-07-02 19:27:43 +0000 | [diff] [blame] | 3043 | |
Jordy Rose | e0a5d32 | 2011-08-23 20:27:16 +0000 | [diff] [blame] | 3044 | V = V - 1; |
| 3045 | break; |
| 3046 | |
| 3047 | case RefVal::NotOwned: |
Jordan Rose | 4531b7d | 2012-07-02 19:27:43 +0000 | [diff] [blame] | 3048 | if (V.getCount() > 0) { |
Anna Zaks | 554067f | 2012-08-29 23:23:43 +0000 | [diff] [blame] | 3049 | if (E == DecRefAndStopTrackingHard) |
Anna Zaks | 8d6b43c | 2012-08-14 00:36:15 +0000 | [diff] [blame] | 3050 | return removeRefBinding(state, sym); |
Jordy Rose | e0a5d32 | 2011-08-23 20:27:16 +0000 | [diff] [blame] | 3051 | V = V - 1; |
Jordan Rose | 4531b7d | 2012-07-02 19:27:43 +0000 | [diff] [blame] | 3052 | } else { |
Jordy Rose | e0a5d32 | 2011-08-23 20:27:16 +0000 | [diff] [blame] | 3053 | V = V ^ RefVal::ErrorReleaseNotOwned; |
| 3054 | hasErr = V.getKind(); |
| 3055 | } |
| 3056 | break; |
| 3057 | |
| 3058 | case RefVal::Released: |
| 3059 | // Non-GC cases are handled above. |
Jordy Rose | 17a38e2 | 2011-09-02 05:55:19 +0000 | [diff] [blame] | 3060 | assert(C.isObjCGCEnabled()); |
Jordy Rose | e0a5d32 | 2011-08-23 20:27:16 +0000 | [diff] [blame] | 3061 | V = V ^ RefVal::ErrorUseAfterRelease; |
| 3062 | hasErr = V.getKind(); |
| 3063 | break; |
| 3064 | } |
| 3065 | break; |
| 3066 | } |
Anna Zaks | 8d6b43c | 2012-08-14 00:36:15 +0000 | [diff] [blame] | 3067 | return setRefBinding(state, sym, V); |
Jordy Rose | e0a5d32 | 2011-08-23 20:27:16 +0000 | [diff] [blame] | 3068 | } |
| 3069 | |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 3070 | void RetainCountChecker::processNonLeakError(ProgramStateRef St, |
Jordy Rose | 910c405 | 2011-09-02 06:44:22 +0000 | [diff] [blame] | 3071 | SourceRange ErrorRange, |
| 3072 | RefVal::Kind ErrorKind, |
| 3073 | SymbolRef Sym, |
| 3074 | CheckerContext &C) const { |
Jordy Rose | 294396b | 2011-08-22 23:48:23 +0000 | [diff] [blame] | 3075 | ExplodedNode *N = C.generateSink(St); |
| 3076 | if (!N) |
| 3077 | return; |
| 3078 | |
Jordy Rose | 294396b | 2011-08-22 23:48:23 +0000 | [diff] [blame] | 3079 | CFRefBug *BT; |
| 3080 | switch (ErrorKind) { |
| 3081 | default: |
| 3082 | llvm_unreachable("Unhandled error."); |
Jordy Rose | 294396b | 2011-08-22 23:48:23 +0000 | [diff] [blame] | 3083 | case RefVal::ErrorUseAfterRelease: |
Jordy Rose | d6334e1 | 2011-08-25 00:34:03 +0000 | [diff] [blame] | 3084 | if (!useAfterRelease) |
| 3085 | useAfterRelease.reset(new UseAfterRelease()); |
| 3086 | BT = &*useAfterRelease; |
Jordy Rose | 294396b | 2011-08-22 23:48:23 +0000 | [diff] [blame] | 3087 | break; |
| 3088 | case RefVal::ErrorReleaseNotOwned: |
Jordy Rose | d6334e1 | 2011-08-25 00:34:03 +0000 | [diff] [blame] | 3089 | if (!releaseNotOwned) |
| 3090 | releaseNotOwned.reset(new BadRelease()); |
| 3091 | BT = &*releaseNotOwned; |
Jordy Rose | 294396b | 2011-08-22 23:48:23 +0000 | [diff] [blame] | 3092 | break; |
| 3093 | case RefVal::ErrorDeallocGC: |
Jordy Rose | d6334e1 | 2011-08-25 00:34:03 +0000 | [diff] [blame] | 3094 | if (!deallocGC) |
| 3095 | deallocGC.reset(new DeallocGC()); |
| 3096 | BT = &*deallocGC; |
Jordy Rose | 294396b | 2011-08-22 23:48:23 +0000 | [diff] [blame] | 3097 | break; |
| 3098 | case RefVal::ErrorDeallocNotOwned: |
Jordy Rose | d6334e1 | 2011-08-25 00:34:03 +0000 | [diff] [blame] | 3099 | if (!deallocNotOwned) |
| 3100 | deallocNotOwned.reset(new DeallocNotOwned()); |
| 3101 | BT = &*deallocNotOwned; |
Jordy Rose | 294396b | 2011-08-22 23:48:23 +0000 | [diff] [blame] | 3102 | break; |
| 3103 | } |
| 3104 | |
Jordy Rose | d6334e1 | 2011-08-25 00:34:03 +0000 | [diff] [blame] | 3105 | assert(BT); |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3106 | CFRefReport *report = new CFRefReport(*BT, C.getASTContext().getLangOpts(), |
Jordy Rose | 17a38e2 | 2011-09-02 05:55:19 +0000 | [diff] [blame] | 3107 | C.isObjCGCEnabled(), SummaryLog, |
| 3108 | N, Sym); |
Jordy Rose | 294396b | 2011-08-22 23:48:23 +0000 | [diff] [blame] | 3109 | report->addRange(ErrorRange); |
Jordan Rose | 785950e | 2012-11-02 01:53:40 +0000 | [diff] [blame] | 3110 | C.emitReport(report); |
Jordy Rose | 294396b | 2011-08-22 23:48:23 +0000 | [diff] [blame] | 3111 | } |
| 3112 | |
Jordy Rose | 910c405 | 2011-09-02 06:44:22 +0000 | [diff] [blame] | 3113 | //===----------------------------------------------------------------------===// |
| 3114 | // Handle the return values of retain-count-related functions. |
| 3115 | //===----------------------------------------------------------------------===// |
| 3116 | |
| 3117 | bool RetainCountChecker::evalCall(const CallExpr *CE, CheckerContext &C) const { |
Jordy Rose | 76c506f | 2011-08-21 21:58:18 +0000 | [diff] [blame] | 3118 | // Get the callee. We're only interested in simple C functions. |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 3119 | ProgramStateRef state = C.getState(); |
Anna Zaks | b805c8f | 2011-12-01 05:57:37 +0000 | [diff] [blame] | 3120 | const FunctionDecl *FD = C.getCalleeDecl(CE); |
Jordy Rose | 76c506f | 2011-08-21 21:58:18 +0000 | [diff] [blame] | 3121 | if (!FD) |
| 3122 | return false; |
| 3123 | |
| 3124 | IdentifierInfo *II = FD->getIdentifier(); |
| 3125 | if (!II) |
| 3126 | return false; |
| 3127 | |
| 3128 | // For now, we're only handling the functions that return aliases of their |
| 3129 | // arguments: CFRetain and CFMakeCollectable (and their families). |
| 3130 | // Eventually we should add other functions we can model entirely, |
| 3131 | // such as CFRelease, which don't invalidate their arguments or globals. |
| 3132 | if (CE->getNumArgs() != 1) |
| 3133 | return false; |
| 3134 | |
| 3135 | // Get the name of the function. |
| 3136 | StringRef FName = II->getName(); |
| 3137 | FName = FName.substr(FName.find_first_not_of('_')); |
| 3138 | |
| 3139 | // See if it's one of the specific functions we know how to eval. |
| 3140 | bool canEval = false; |
| 3141 | |
Anna Zaks | b805c8f | 2011-12-01 05:57:37 +0000 | [diff] [blame] | 3142 | QualType ResultTy = CE->getCallReturnType(); |
Jordy Rose | 76c506f | 2011-08-21 21:58:18 +0000 | [diff] [blame] | 3143 | if (ResultTy->isObjCIdType()) { |
| 3144 | // Handle: id NSMakeCollectable(CFTypeRef) |
| 3145 | canEval = II->isStr("NSMakeCollectable"); |
| 3146 | } else if (ResultTy->isPointerType()) { |
| 3147 | // Handle: (CF|CG)Retain |
| 3148 | // CFMakeCollectable |
| 3149 | // It's okay to be a little sloppy here (CGMakeCollectable doesn't exist). |
| 3150 | if (cocoa::isRefType(ResultTy, "CF", FName) || |
| 3151 | cocoa::isRefType(ResultTy, "CG", FName)) { |
| 3152 | canEval = isRetain(FD, FName) || isMakeCollectable(FD, FName); |
| 3153 | } |
| 3154 | } |
| 3155 | |
| 3156 | if (!canEval) |
| 3157 | return false; |
| 3158 | |
| 3159 | // Bind the return value. |
Ted Kremenek | 5eca482 | 2012-01-06 22:09:28 +0000 | [diff] [blame] | 3160 | const LocationContext *LCtx = C.getLocationContext(); |
| 3161 | SVal RetVal = state->getSVal(CE->getArg(0), LCtx); |
Jordy Rose | 76c506f | 2011-08-21 21:58:18 +0000 | [diff] [blame] | 3162 | if (RetVal.isUnknown()) { |
| 3163 | // If the receiver is unknown, conjure a return value. |
| 3164 | SValBuilder &SVB = C.getSValBuilder(); |
Ted Kremenek | 66c486f | 2012-08-22 06:26:15 +0000 | [diff] [blame] | 3165 | RetVal = SVB.conjureSymbolVal(0, CE, LCtx, ResultTy, C.blockCount()); |
Jordy Rose | 76c506f | 2011-08-21 21:58:18 +0000 | [diff] [blame] | 3166 | } |
Ted Kremenek | 5eca482 | 2012-01-06 22:09:28 +0000 | [diff] [blame] | 3167 | state = state->BindExpr(CE, LCtx, RetVal, false); |
Jordy Rose | 76c506f | 2011-08-21 21:58:18 +0000 | [diff] [blame] | 3168 | |
Jordy Rose | 294396b | 2011-08-22 23:48:23 +0000 | [diff] [blame] | 3169 | // FIXME: This should not be necessary, but otherwise the argument seems to be |
| 3170 | // considered alive during the next statement. |
| 3171 | if (const MemRegion *ArgRegion = RetVal.getAsRegion()) { |
| 3172 | // Save the refcount status of the argument. |
| 3173 | SymbolRef Sym = RetVal.getAsLocSymbol(); |
Anna Zaks | 8d6b43c | 2012-08-14 00:36:15 +0000 | [diff] [blame] | 3174 | const RefVal *Binding = 0; |
Jordy Rose | 294396b | 2011-08-22 23:48:23 +0000 | [diff] [blame] | 3175 | if (Sym) |
Anna Zaks | 8d6b43c | 2012-08-14 00:36:15 +0000 | [diff] [blame] | 3176 | Binding = getRefBinding(state, Sym); |
Jordy Rose | 76c506f | 2011-08-21 21:58:18 +0000 | [diff] [blame] | 3177 | |
Jordy Rose | 294396b | 2011-08-22 23:48:23 +0000 | [diff] [blame] | 3178 | // Invalidate the argument region. |
Anna Zaks | bf53dfa | 2012-12-20 00:38:25 +0000 | [diff] [blame^] | 3179 | state = state->invalidateRegions(ArgRegion, CE, C.blockCount(), LCtx, |
| 3180 | /*ResultsInPointerEscape*/ false); |
Jordy Rose | 76c506f | 2011-08-21 21:58:18 +0000 | [diff] [blame] | 3181 | |
Jordy Rose | 294396b | 2011-08-22 23:48:23 +0000 | [diff] [blame] | 3182 | // Restore the refcount status of the argument. |
| 3183 | if (Binding) |
Anna Zaks | 8d6b43c | 2012-08-14 00:36:15 +0000 | [diff] [blame] | 3184 | state = setRefBinding(state, Sym, *Binding); |
Jordy Rose | 294396b | 2011-08-22 23:48:23 +0000 | [diff] [blame] | 3185 | } |
| 3186 | |
Anna Zaks | 0bd6b11 | 2011-10-26 21:06:34 +0000 | [diff] [blame] | 3187 | C.addTransition(state); |
Jordy Rose | 76c506f | 2011-08-21 21:58:18 +0000 | [diff] [blame] | 3188 | return true; |
| 3189 | } |
| 3190 | |
Jordy Rose | 910c405 | 2011-09-02 06:44:22 +0000 | [diff] [blame] | 3191 | //===----------------------------------------------------------------------===// |
| 3192 | // Handle return statements. |
| 3193 | //===----------------------------------------------------------------------===// |
Jordy Rose | f53e8c7 | 2011-08-23 19:43:16 +0000 | [diff] [blame] | 3194 | |
Jordy Rose | 910c405 | 2011-09-02 06:44:22 +0000 | [diff] [blame] | 3195 | void RetainCountChecker::checkPreStmt(const ReturnStmt *S, |
| 3196 | CheckerContext &C) const { |
Ted Kremenek | e571578 | 2012-02-25 02:09:09 +0000 | [diff] [blame] | 3197 | |
| 3198 | // Only adjust the reference count if this is the top-level call frame, |
| 3199 | // and not the result of inlining. In the future, we should do |
| 3200 | // better checking even for inlined calls, and see if they match |
| 3201 | // with their expected semantics (e.g., the method should return a retained |
| 3202 | // object, etc.). |
Anna Zaks | fadcd5d | 2012-11-03 02:54:16 +0000 | [diff] [blame] | 3203 | if (!C.inTopFrame()) |
Ted Kremenek | e571578 | 2012-02-25 02:09:09 +0000 | [diff] [blame] | 3204 | return; |
| 3205 | |
Jordy Rose | f53e8c7 | 2011-08-23 19:43:16 +0000 | [diff] [blame] | 3206 | const Expr *RetE = S->getRetValue(); |
| 3207 | if (!RetE) |
| 3208 | return; |
| 3209 | |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 3210 | ProgramStateRef state = C.getState(); |
Ted Kremenek | 5eca482 | 2012-01-06 22:09:28 +0000 | [diff] [blame] | 3211 | SymbolRef Sym = |
| 3212 | state->getSValAsScalarOrLoc(RetE, C.getLocationContext()).getAsLocSymbol(); |
Jordy Rose | f53e8c7 | 2011-08-23 19:43:16 +0000 | [diff] [blame] | 3213 | if (!Sym) |
| 3214 | return; |
| 3215 | |
| 3216 | // Get the reference count binding (if any). |
Anna Zaks | 8d6b43c | 2012-08-14 00:36:15 +0000 | [diff] [blame] | 3217 | const RefVal *T = getRefBinding(state, Sym); |
Jordy Rose | f53e8c7 | 2011-08-23 19:43:16 +0000 | [diff] [blame] | 3218 | if (!T) |
| 3219 | return; |
| 3220 | |
| 3221 | // Change the reference count. |
| 3222 | RefVal X = *T; |
| 3223 | |
| 3224 | switch (X.getKind()) { |
| 3225 | case RefVal::Owned: { |
| 3226 | unsigned cnt = X.getCount(); |
| 3227 | assert(cnt > 0); |
| 3228 | X.setCount(cnt - 1); |
| 3229 | X = X ^ RefVal::ReturnedOwned; |
| 3230 | break; |
| 3231 | } |
| 3232 | |
| 3233 | case RefVal::NotOwned: { |
| 3234 | unsigned cnt = X.getCount(); |
| 3235 | if (cnt) { |
| 3236 | X.setCount(cnt - 1); |
| 3237 | X = X ^ RefVal::ReturnedOwned; |
| 3238 | } |
| 3239 | else { |
| 3240 | X = X ^ RefVal::ReturnedNotOwned; |
| 3241 | } |
| 3242 | break; |
| 3243 | } |
| 3244 | |
| 3245 | default: |
| 3246 | return; |
| 3247 | } |
| 3248 | |
| 3249 | // Update the binding. |
Anna Zaks | 8d6b43c | 2012-08-14 00:36:15 +0000 | [diff] [blame] | 3250 | state = setRefBinding(state, Sym, X); |
Anna Zaks | 0bd6b11 | 2011-10-26 21:06:34 +0000 | [diff] [blame] | 3251 | ExplodedNode *Pred = C.addTransition(state); |
Jordy Rose | f53e8c7 | 2011-08-23 19:43:16 +0000 | [diff] [blame] | 3252 | |
| 3253 | // At this point we have updated the state properly. |
| 3254 | // Everything after this is merely checking to see if the return value has |
| 3255 | // been over- or under-retained. |
| 3256 | |
| 3257 | // Did we cache out? |
| 3258 | if (!Pred) |
| 3259 | return; |
| 3260 | |
Jordy Rose | f53e8c7 | 2011-08-23 19:43:16 +0000 | [diff] [blame] | 3261 | // Update the autorelease counts. |
| 3262 | static SimpleProgramPointTag |
Jordy Rose | 910c405 | 2011-09-02 06:44:22 +0000 | [diff] [blame] | 3263 | AutoreleaseTag("RetainCountChecker : Autorelease"); |
Jordan Rose | 4ee1c55 | 2012-12-06 18:58:18 +0000 | [diff] [blame] | 3264 | state = handleAutoreleaseCounts(state, Pred, &AutoreleaseTag, C, Sym, X); |
Jordy Rose | f53e8c7 | 2011-08-23 19:43:16 +0000 | [diff] [blame] | 3265 | |
| 3266 | // Did we cache out? |
Jordan Rose | 4ee1c55 | 2012-12-06 18:58:18 +0000 | [diff] [blame] | 3267 | if (!state) |
Jordy Rose | f53e8c7 | 2011-08-23 19:43:16 +0000 | [diff] [blame] | 3268 | return; |
| 3269 | |
| 3270 | // Get the updated binding. |
Anna Zaks | 8d6b43c | 2012-08-14 00:36:15 +0000 | [diff] [blame] | 3271 | T = getRefBinding(state, Sym); |
Jordy Rose | f53e8c7 | 2011-08-23 19:43:16 +0000 | [diff] [blame] | 3272 | assert(T); |
| 3273 | X = *T; |
| 3274 | |
| 3275 | // Consult the summary of the enclosing method. |
Jordy Rose | 17a38e2 | 2011-09-02 05:55:19 +0000 | [diff] [blame] | 3276 | RetainSummaryManager &Summaries = getSummaryManager(C); |
Jordy Rose | f53e8c7 | 2011-08-23 19:43:16 +0000 | [diff] [blame] | 3277 | const Decl *CD = &Pred->getCodeDecl(); |
Jordan Rose | 4531b7d | 2012-07-02 19:27:43 +0000 | [diff] [blame] | 3278 | RetEffect RE = RetEffect::MakeNoRet(); |
Jordy Rose | f53e8c7 | 2011-08-23 19:43:16 +0000 | [diff] [blame] | 3279 | |
Jordan Rose | 4531b7d | 2012-07-02 19:27:43 +0000 | [diff] [blame] | 3280 | // FIXME: What is the convention for blocks? Is there one? |
Jordy Rose | f53e8c7 | 2011-08-23 19:43:16 +0000 | [diff] [blame] | 3281 | if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(CD)) { |
Jordy Rose | b6cfc09 | 2011-08-25 00:10:37 +0000 | [diff] [blame] | 3282 | const RetainSummary *Summ = Summaries.getMethodSummary(MD); |
Jordan Rose | 4531b7d | 2012-07-02 19:27:43 +0000 | [diff] [blame] | 3283 | RE = Summ->getRetEffect(); |
| 3284 | } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(CD)) { |
| 3285 | if (!isa<CXXMethodDecl>(FD)) { |
| 3286 | const RetainSummary *Summ = Summaries.getFunctionSummary(FD); |
| 3287 | RE = Summ->getRetEffect(); |
| 3288 | } |
Jordy Rose | f53e8c7 | 2011-08-23 19:43:16 +0000 | [diff] [blame] | 3289 | } |
| 3290 | |
Jordan Rose | 4531b7d | 2012-07-02 19:27:43 +0000 | [diff] [blame] | 3291 | checkReturnWithRetEffect(S, C, Pred, RE, X, Sym, state); |
Jordy Rose | f53e8c7 | 2011-08-23 19:43:16 +0000 | [diff] [blame] | 3292 | } |
| 3293 | |
Jordy Rose | 910c405 | 2011-09-02 06:44:22 +0000 | [diff] [blame] | 3294 | void RetainCountChecker::checkReturnWithRetEffect(const ReturnStmt *S, |
| 3295 | CheckerContext &C, |
| 3296 | ExplodedNode *Pred, |
| 3297 | RetEffect RE, RefVal X, |
| 3298 | SymbolRef Sym, |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 3299 | ProgramStateRef state) const { |
Jordy Rose | f53e8c7 | 2011-08-23 19:43:16 +0000 | [diff] [blame] | 3300 | // Any leaks or other errors? |
| 3301 | if (X.isReturnedOwned() && X.getCount() == 0) { |
| 3302 | if (RE.getKind() != RetEffect::NoRet) { |
| 3303 | bool hasError = false; |
Jordy Rose | 17a38e2 | 2011-09-02 05:55:19 +0000 | [diff] [blame] | 3304 | if (C.isObjCGCEnabled() && RE.getObjKind() == RetEffect::ObjC) { |
Jordy Rose | f53e8c7 | 2011-08-23 19:43:16 +0000 | [diff] [blame] | 3305 | // Things are more complicated with garbage collection. If the |
| 3306 | // returned object is suppose to be an Objective-C object, we have |
| 3307 | // a leak (as the caller expects a GC'ed object) because no |
| 3308 | // method should return ownership unless it returns a CF object. |
| 3309 | hasError = true; |
| 3310 | X = X ^ RefVal::ErrorGCLeakReturned; |
| 3311 | } |
| 3312 | else if (!RE.isOwned()) { |
| 3313 | // Either we are using GC and the returned object is a CF type |
| 3314 | // or we aren't using GC. In either case, we expect that the |
| 3315 | // enclosing method is expected to return ownership. |
| 3316 | hasError = true; |
| 3317 | X = X ^ RefVal::ErrorLeakReturned; |
| 3318 | } |
| 3319 | |
| 3320 | if (hasError) { |
| 3321 | // Generate an error node. |
Anna Zaks | 8d6b43c | 2012-08-14 00:36:15 +0000 | [diff] [blame] | 3322 | state = setRefBinding(state, Sym, X); |
Jordy Rose | f53e8c7 | 2011-08-23 19:43:16 +0000 | [diff] [blame] | 3323 | |
| 3324 | static SimpleProgramPointTag |
Jordy Rose | 910c405 | 2011-09-02 06:44:22 +0000 | [diff] [blame] | 3325 | ReturnOwnLeakTag("RetainCountChecker : ReturnsOwnLeak"); |
Anna Zaks | 0bd6b11 | 2011-10-26 21:06:34 +0000 | [diff] [blame] | 3326 | ExplodedNode *N = C.addTransition(state, Pred, &ReturnOwnLeakTag); |
Jordy Rose | f53e8c7 | 2011-08-23 19:43:16 +0000 | [diff] [blame] | 3327 | if (N) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3328 | const LangOptions &LOpts = C.getASTContext().getLangOpts(); |
Jordy Rose | 17a38e2 | 2011-09-02 05:55:19 +0000 | [diff] [blame] | 3329 | bool GCEnabled = C.isObjCGCEnabled(); |
Jordy Rose | f53e8c7 | 2011-08-23 19:43:16 +0000 | [diff] [blame] | 3330 | CFRefReport *report = |
Jordy Rose | 17a38e2 | 2011-09-02 05:55:19 +0000 | [diff] [blame] | 3331 | new CFRefLeakReport(*getLeakAtReturnBug(LOpts, GCEnabled), |
| 3332 | LOpts, GCEnabled, SummaryLog, |
Anna Zaks | 6a93bd5 | 2011-10-25 19:57:11 +0000 | [diff] [blame] | 3333 | N, Sym, C); |
Jordan Rose | 785950e | 2012-11-02 01:53:40 +0000 | [diff] [blame] | 3334 | C.emitReport(report); |
Jordy Rose | f53e8c7 | 2011-08-23 19:43:16 +0000 | [diff] [blame] | 3335 | } |
| 3336 | } |
| 3337 | } |
| 3338 | } else if (X.isReturnedNotOwned()) { |
| 3339 | if (RE.isOwned()) { |
| 3340 | // Trying to return a not owned object to a caller expecting an |
| 3341 | // owned object. |
Anna Zaks | 8d6b43c | 2012-08-14 00:36:15 +0000 | [diff] [blame] | 3342 | state = setRefBinding(state, Sym, X ^ RefVal::ErrorReturnedNotOwned); |
Jordy Rose | f53e8c7 | 2011-08-23 19:43:16 +0000 | [diff] [blame] | 3343 | |
| 3344 | static SimpleProgramPointTag |
Jordy Rose | 910c405 | 2011-09-02 06:44:22 +0000 | [diff] [blame] | 3345 | ReturnNotOwnedTag("RetainCountChecker : ReturnNotOwnedForOwned"); |
Anna Zaks | 0bd6b11 | 2011-10-26 21:06:34 +0000 | [diff] [blame] | 3346 | ExplodedNode *N = C.addTransition(state, Pred, &ReturnNotOwnedTag); |
Jordy Rose | f53e8c7 | 2011-08-23 19:43:16 +0000 | [diff] [blame] | 3347 | if (N) { |
Jordy Rose | d6334e1 | 2011-08-25 00:34:03 +0000 | [diff] [blame] | 3348 | if (!returnNotOwnedForOwned) |
| 3349 | returnNotOwnedForOwned.reset(new ReturnedNotOwnedForOwned()); |
| 3350 | |
Jordy Rose | f53e8c7 | 2011-08-23 19:43:16 +0000 | [diff] [blame] | 3351 | CFRefReport *report = |
Jordy Rose | d6334e1 | 2011-08-25 00:34:03 +0000 | [diff] [blame] | 3352 | new CFRefReport(*returnNotOwnedForOwned, |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3353 | C.getASTContext().getLangOpts(), |
Jordy Rose | 17a38e2 | 2011-09-02 05:55:19 +0000 | [diff] [blame] | 3354 | C.isObjCGCEnabled(), SummaryLog, N, Sym); |
Jordan Rose | 785950e | 2012-11-02 01:53:40 +0000 | [diff] [blame] | 3355 | C.emitReport(report); |
Jordy Rose | f53e8c7 | 2011-08-23 19:43:16 +0000 | [diff] [blame] | 3356 | } |
| 3357 | } |
| 3358 | } |
| 3359 | } |
| 3360 | |
Jordy Rose | 8d22863 | 2011-08-23 20:07:14 +0000 | [diff] [blame] | 3361 | //===----------------------------------------------------------------------===// |
Jordy Rose | 910c405 | 2011-09-02 06:44:22 +0000 | [diff] [blame] | 3362 | // Check various ways a symbol can be invalidated. |
| 3363 | //===----------------------------------------------------------------------===// |
| 3364 | |
Anna Zaks | 390909c | 2011-10-06 00:43:15 +0000 | [diff] [blame] | 3365 | void RetainCountChecker::checkBind(SVal loc, SVal val, const Stmt *S, |
Jordy Rose | 910c405 | 2011-09-02 06:44:22 +0000 | [diff] [blame] | 3366 | CheckerContext &C) const { |
| 3367 | // Are we storing to something that causes the value to "escape"? |
| 3368 | bool escapes = true; |
| 3369 | |
| 3370 | // A value escapes in three possible cases (this may change): |
| 3371 | // |
| 3372 | // (1) we are binding to something that is not a memory region. |
| 3373 | // (2) we are binding to a memregion that does not have stack storage |
| 3374 | // (3) we are binding to a memregion with stack storage that the store |
| 3375 | // does not understand. |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 3376 | ProgramStateRef state = C.getState(); |
Jordy Rose | 910c405 | 2011-09-02 06:44:22 +0000 | [diff] [blame] | 3377 | |
| 3378 | if (loc::MemRegionVal *regionLoc = dyn_cast<loc::MemRegionVal>(&loc)) { |
| 3379 | escapes = !regionLoc->getRegion()->hasStackStorage(); |
| 3380 | |
| 3381 | if (!escapes) { |
| 3382 | // To test (3), generate a new state with the binding added. If it is |
| 3383 | // the same state, then it escapes (since the store cannot represent |
| 3384 | // the binding). |
Anna Zaks | e7958da | 2012-05-02 00:15:40 +0000 | [diff] [blame] | 3385 | // Do this only if we know that the store is not supposed to generate the |
| 3386 | // same state. |
| 3387 | SVal StoredVal = state->getSVal(regionLoc->getRegion()); |
| 3388 | if (StoredVal != val) |
| 3389 | escapes = (state == (state->bindLoc(*regionLoc, val))); |
Jordy Rose | 910c405 | 2011-09-02 06:44:22 +0000 | [diff] [blame] | 3390 | } |
Ted Kremenek | de5b4fb | 2012-03-27 01:12:45 +0000 | [diff] [blame] | 3391 | if (!escapes) { |
| 3392 | // Case 4: We do not currently model what happens when a symbol is |
| 3393 | // assigned to a struct field, so be conservative here and let the symbol |
| 3394 | // go. TODO: This could definitely be improved upon. |
| 3395 | escapes = !isa<VarRegion>(regionLoc->getRegion()); |
| 3396 | } |
Jordy Rose | 910c405 | 2011-09-02 06:44:22 +0000 | [diff] [blame] | 3397 | } |
| 3398 | |
| 3399 | // If our store can represent the binding and we aren't storing to something |
| 3400 | // that doesn't have local storage then just return and have the simulation |
| 3401 | // state continue as is. |
| 3402 | if (!escapes) |
| 3403 | return; |
| 3404 | |
| 3405 | // Otherwise, find all symbols referenced by 'val' that we are tracking |
| 3406 | // and stop tracking them. |
| 3407 | state = state->scanReachableSymbols<StopTrackingCallback>(val).getState(); |
Anna Zaks | 0bd6b11 | 2011-10-26 21:06:34 +0000 | [diff] [blame] | 3408 | C.addTransition(state); |
Jordy Rose | 910c405 | 2011-09-02 06:44:22 +0000 | [diff] [blame] | 3409 | } |
| 3410 | |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 3411 | ProgramStateRef RetainCountChecker::evalAssume(ProgramStateRef state, |
Jordy Rose | 910c405 | 2011-09-02 06:44:22 +0000 | [diff] [blame] | 3412 | SVal Cond, |
| 3413 | bool Assumption) const { |
| 3414 | |
| 3415 | // FIXME: We may add to the interface of evalAssume the list of symbols |
| 3416 | // whose assumptions have changed. For now we just iterate through the |
| 3417 | // bindings and check if any of the tracked symbols are NULL. This isn't |
| 3418 | // too bad since the number of symbols we will track in practice are |
| 3419 | // probably small and evalAssume is only called at branches and a few |
| 3420 | // other places. |
Jordan Rose | 166d502 | 2012-11-02 01:54:06 +0000 | [diff] [blame] | 3421 | RefBindingsTy B = state->get<RefBindings>(); |
Jordy Rose | 910c405 | 2011-09-02 06:44:22 +0000 | [diff] [blame] | 3422 | |
| 3423 | if (B.isEmpty()) |
| 3424 | return state; |
| 3425 | |
| 3426 | bool changed = false; |
Jordan Rose | 166d502 | 2012-11-02 01:54:06 +0000 | [diff] [blame] | 3427 | RefBindingsTy::Factory &RefBFactory = state->get_context<RefBindings>(); |
Jordy Rose | 910c405 | 2011-09-02 06:44:22 +0000 | [diff] [blame] | 3428 | |
Jordan Rose | 166d502 | 2012-11-02 01:54:06 +0000 | [diff] [blame] | 3429 | for (RefBindingsTy::iterator I = B.begin(), E = B.end(); I != E; ++I) { |
Ted Kremenek | 47cbd0f | 2012-09-07 22:31:01 +0000 | [diff] [blame] | 3430 | // Check if the symbol is null stop tracking the symbol. |
Jordan Rose | ec8d420 | 2012-11-01 00:18:27 +0000 | [diff] [blame] | 3431 | ConstraintManager &CMgr = state->getConstraintManager(); |
| 3432 | ConditionTruthVal AllocFailed = CMgr.isNull(state, I.getKey()); |
| 3433 | if (AllocFailed.isConstrainedTrue()) { |
Jordy Rose | 910c405 | 2011-09-02 06:44:22 +0000 | [diff] [blame] | 3434 | changed = true; |
| 3435 | B = RefBFactory.remove(B, I.getKey()); |
| 3436 | } |
| 3437 | } |
| 3438 | |
| 3439 | if (changed) |
| 3440 | state = state->set<RefBindings>(B); |
| 3441 | |
| 3442 | return state; |
| 3443 | } |
| 3444 | |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 3445 | ProgramStateRef |
| 3446 | RetainCountChecker::checkRegionChanges(ProgramStateRef state, |
Anna Zaks | bf53dfa | 2012-12-20 00:38:25 +0000 | [diff] [blame^] | 3447 | const InvalidatedSymbols *invalidated, |
Jordy Rose | 910c405 | 2011-09-02 06:44:22 +0000 | [diff] [blame] | 3448 | ArrayRef<const MemRegion *> ExplicitRegions, |
Anna Zaks | 66c4040 | 2012-02-14 21:55:24 +0000 | [diff] [blame] | 3449 | ArrayRef<const MemRegion *> Regions, |
Jordan Rose | 740d490 | 2012-07-02 19:27:35 +0000 | [diff] [blame] | 3450 | const CallEvent *Call) const { |
Jordy Rose | 910c405 | 2011-09-02 06:44:22 +0000 | [diff] [blame] | 3451 | if (!invalidated) |
| 3452 | return state; |
| 3453 | |
| 3454 | llvm::SmallPtrSet<SymbolRef, 8> WhitelistedSymbols; |
| 3455 | for (ArrayRef<const MemRegion *>::iterator I = ExplicitRegions.begin(), |
| 3456 | E = ExplicitRegions.end(); I != E; ++I) { |
| 3457 | if (const SymbolicRegion *SR = (*I)->StripCasts()->getAs<SymbolicRegion>()) |
| 3458 | WhitelistedSymbols.insert(SR->getSymbol()); |
| 3459 | } |
| 3460 | |
Anna Zaks | bf53dfa | 2012-12-20 00:38:25 +0000 | [diff] [blame^] | 3461 | for (InvalidatedSymbols::const_iterator I=invalidated->begin(), |
Jordy Rose | 910c405 | 2011-09-02 06:44:22 +0000 | [diff] [blame] | 3462 | E = invalidated->end(); I!=E; ++I) { |
| 3463 | SymbolRef sym = *I; |
| 3464 | if (WhitelistedSymbols.count(sym)) |
| 3465 | continue; |
| 3466 | // Remove any existing reference-count binding. |
Anna Zaks | 8d6b43c | 2012-08-14 00:36:15 +0000 | [diff] [blame] | 3467 | state = removeRefBinding(state, sym); |
Jordy Rose | 910c405 | 2011-09-02 06:44:22 +0000 | [diff] [blame] | 3468 | } |
| 3469 | return state; |
| 3470 | } |
| 3471 | |
| 3472 | //===----------------------------------------------------------------------===// |
Jordy Rose | 8d22863 | 2011-08-23 20:07:14 +0000 | [diff] [blame] | 3473 | // Handle dead symbols and end-of-path. |
| 3474 | //===----------------------------------------------------------------------===// |
| 3475 | |
Jordan Rose | 4ee1c55 | 2012-12-06 18:58:18 +0000 | [diff] [blame] | 3476 | ProgramStateRef |
| 3477 | RetainCountChecker::handleAutoreleaseCounts(ProgramStateRef state, |
Anna Zaks | 6a93bd5 | 2011-10-25 19:57:11 +0000 | [diff] [blame] | 3478 | ExplodedNode *Pred, |
Jordan Rose | 2bce86c | 2012-08-18 00:30:16 +0000 | [diff] [blame] | 3479 | const ProgramPointTag *Tag, |
Anna Zaks | 6a93bd5 | 2011-10-25 19:57:11 +0000 | [diff] [blame] | 3480 | CheckerContext &Ctx, |
Jordy Rose | 910c405 | 2011-09-02 06:44:22 +0000 | [diff] [blame] | 3481 | SymbolRef Sym, RefVal V) const { |
Jordy Rose | 8d22863 | 2011-08-23 20:07:14 +0000 | [diff] [blame] | 3482 | unsigned ACnt = V.getAutoreleaseCount(); |
| 3483 | |
| 3484 | // No autorelease counts? Nothing to be done. |
| 3485 | if (!ACnt) |
Jordan Rose | 4ee1c55 | 2012-12-06 18:58:18 +0000 | [diff] [blame] | 3486 | return state; |
Jordy Rose | 8d22863 | 2011-08-23 20:07:14 +0000 | [diff] [blame] | 3487 | |
Anna Zaks | 6a93bd5 | 2011-10-25 19:57:11 +0000 | [diff] [blame] | 3488 | assert(!Ctx.isObjCGCEnabled() && "Autorelease counts in GC mode?"); |
Jordy Rose | 8d22863 | 2011-08-23 20:07:14 +0000 | [diff] [blame] | 3489 | unsigned Cnt = V.getCount(); |
| 3490 | |
| 3491 | // FIXME: Handle sending 'autorelease' to already released object. |
| 3492 | |
| 3493 | if (V.getKind() == RefVal::ReturnedOwned) |
| 3494 | ++Cnt; |
| 3495 | |
| 3496 | if (ACnt <= Cnt) { |
| 3497 | if (ACnt == Cnt) { |
| 3498 | V.clearCounts(); |
| 3499 | if (V.getKind() == RefVal::ReturnedOwned) |
| 3500 | V = V ^ RefVal::ReturnedNotOwned; |
| 3501 | else |
| 3502 | V = V ^ RefVal::NotOwned; |
| 3503 | } else { |
| 3504 | V.setCount(Cnt - ACnt); |
| 3505 | V.setAutoreleaseCount(0); |
| 3506 | } |
Jordan Rose | 4ee1c55 | 2012-12-06 18:58:18 +0000 | [diff] [blame] | 3507 | return setRefBinding(state, Sym, V); |
Jordy Rose | 8d22863 | 2011-08-23 20:07:14 +0000 | [diff] [blame] | 3508 | } |
| 3509 | |
| 3510 | // Woah! More autorelease counts then retain counts left. |
| 3511 | // Emit hard error. |
| 3512 | V = V ^ RefVal::ErrorOverAutorelease; |
Anna Zaks | 8d6b43c | 2012-08-14 00:36:15 +0000 | [diff] [blame] | 3513 | state = setRefBinding(state, Sym, V); |
Jordy Rose | 8d22863 | 2011-08-23 20:07:14 +0000 | [diff] [blame] | 3514 | |
Jordan Rose | fa06f04 | 2012-08-20 18:43:42 +0000 | [diff] [blame] | 3515 | ExplodedNode *N = Ctx.generateSink(state, Pred, Tag); |
Jordan Rose | 2bce86c | 2012-08-18 00:30:16 +0000 | [diff] [blame] | 3516 | if (N) { |
Dylan Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 3517 | SmallString<128> sbuf; |
Jordy Rose | 8d22863 | 2011-08-23 20:07:14 +0000 | [diff] [blame] | 3518 | llvm::raw_svector_ostream os(sbuf); |
| 3519 | os << "Object over-autoreleased: object was sent -autorelease "; |
| 3520 | if (V.getAutoreleaseCount() > 1) |
| 3521 | os << V.getAutoreleaseCount() << " times "; |
| 3522 | os << "but the object has a +" << V.getCount() << " retain count"; |
| 3523 | |
Jordy Rose | d6334e1 | 2011-08-25 00:34:03 +0000 | [diff] [blame] | 3524 | if (!overAutorelease) |
| 3525 | overAutorelease.reset(new OverAutorelease()); |
| 3526 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3527 | const LangOptions &LOpts = Ctx.getASTContext().getLangOpts(); |
Jordy Rose | 8d22863 | 2011-08-23 20:07:14 +0000 | [diff] [blame] | 3528 | CFRefReport *report = |
Jordy Rose | d6334e1 | 2011-08-25 00:34:03 +0000 | [diff] [blame] | 3529 | new CFRefReport(*overAutorelease, LOpts, /* GCEnabled = */ false, |
| 3530 | SummaryLog, N, Sym, os.str()); |
Jordan Rose | 785950e | 2012-11-02 01:53:40 +0000 | [diff] [blame] | 3531 | Ctx.emitReport(report); |
Jordy Rose | 8d22863 | 2011-08-23 20:07:14 +0000 | [diff] [blame] | 3532 | } |
| 3533 | |
Jordan Rose | 4ee1c55 | 2012-12-06 18:58:18 +0000 | [diff] [blame] | 3534 | return 0; |
Jordy Rose | 8d22863 | 2011-08-23 20:07:14 +0000 | [diff] [blame] | 3535 | } |
Jordy Rose | 38f17d6 | 2011-08-23 19:01:07 +0000 | [diff] [blame] | 3536 | |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 3537 | ProgramStateRef |
| 3538 | RetainCountChecker::handleSymbolDeath(ProgramStateRef state, |
Jordy Rose | 910c405 | 2011-09-02 06:44:22 +0000 | [diff] [blame] | 3539 | SymbolRef sid, RefVal V, |
Jordy Rose | 38f17d6 | 2011-08-23 19:01:07 +0000 | [diff] [blame] | 3540 | SmallVectorImpl<SymbolRef> &Leaked) const { |
Jordy Rose | 5337612 | 2011-08-24 04:48:19 +0000 | [diff] [blame] | 3541 | bool hasLeak = false; |
Jordy Rose | 38f17d6 | 2011-08-23 19:01:07 +0000 | [diff] [blame] | 3542 | if (V.isOwned()) |
| 3543 | hasLeak = true; |
| 3544 | else if (V.isNotOwned() || V.isReturnedOwned()) |
| 3545 | hasLeak = (V.getCount() > 0); |
| 3546 | |
| 3547 | if (!hasLeak) |
Anna Zaks | 8d6b43c | 2012-08-14 00:36:15 +0000 | [diff] [blame] | 3548 | return removeRefBinding(state, sid); |
Jordy Rose | 38f17d6 | 2011-08-23 19:01:07 +0000 | [diff] [blame] | 3549 | |
| 3550 | Leaked.push_back(sid); |
Anna Zaks | 8d6b43c | 2012-08-14 00:36:15 +0000 | [diff] [blame] | 3551 | return setRefBinding(state, sid, V ^ RefVal::ErrorLeak); |
Jordy Rose | 38f17d6 | 2011-08-23 19:01:07 +0000 | [diff] [blame] | 3552 | } |
| 3553 | |
| 3554 | ExplodedNode * |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 3555 | RetainCountChecker::processLeaks(ProgramStateRef state, |
Jordy Rose | 910c405 | 2011-09-02 06:44:22 +0000 | [diff] [blame] | 3556 | SmallVectorImpl<SymbolRef> &Leaked, |
Anna Zaks | 6a93bd5 | 2011-10-25 19:57:11 +0000 | [diff] [blame] | 3557 | CheckerContext &Ctx, |
| 3558 | ExplodedNode *Pred) const { |
Jordy Rose | 38f17d6 | 2011-08-23 19:01:07 +0000 | [diff] [blame] | 3559 | // Generate an intermediate node representing the leak point. |
Jordan Rose | 2bce86c | 2012-08-18 00:30:16 +0000 | [diff] [blame] | 3560 | ExplodedNode *N = Ctx.addTransition(state, Pred); |
Jordy Rose | 38f17d6 | 2011-08-23 19:01:07 +0000 | [diff] [blame] | 3561 | |
| 3562 | if (N) { |
| 3563 | for (SmallVectorImpl<SymbolRef>::iterator |
| 3564 | I = Leaked.begin(), E = Leaked.end(); I != E; ++I) { |
| 3565 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3566 | const LangOptions &LOpts = Ctx.getASTContext().getLangOpts(); |
Anna Zaks | 6a93bd5 | 2011-10-25 19:57:11 +0000 | [diff] [blame] | 3567 | bool GCEnabled = Ctx.isObjCGCEnabled(); |
Jordy Rose | 17a38e2 | 2011-09-02 05:55:19 +0000 | [diff] [blame] | 3568 | CFRefBug *BT = Pred ? getLeakWithinFunctionBug(LOpts, GCEnabled) |
| 3569 | : getLeakAtReturnBug(LOpts, GCEnabled); |
Jordy Rose | 38f17d6 | 2011-08-23 19:01:07 +0000 | [diff] [blame] | 3570 | assert(BT && "BugType not initialized."); |
Jordy Rose | 2058956 | 2011-08-24 22:39:09 +0000 | [diff] [blame] | 3571 | |
Jordy Rose | 17a38e2 | 2011-09-02 05:55:19 +0000 | [diff] [blame] | 3572 | CFRefLeakReport *report = new CFRefLeakReport(*BT, LOpts, GCEnabled, |
Anna Zaks | 6a93bd5 | 2011-10-25 19:57:11 +0000 | [diff] [blame] | 3573 | SummaryLog, N, *I, Ctx); |
Jordan Rose | 785950e | 2012-11-02 01:53:40 +0000 | [diff] [blame] | 3574 | Ctx.emitReport(report); |
Jordy Rose | 38f17d6 | 2011-08-23 19:01:07 +0000 | [diff] [blame] | 3575 | } |
| 3576 | } |
| 3577 | |
| 3578 | return N; |
| 3579 | } |
| 3580 | |
Anna Zaks | af498a2 | 2011-10-25 19:56:48 +0000 | [diff] [blame] | 3581 | void RetainCountChecker::checkEndPath(CheckerContext &Ctx) const { |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 3582 | ProgramStateRef state = Ctx.getState(); |
Jordan Rose | 166d502 | 2012-11-02 01:54:06 +0000 | [diff] [blame] | 3583 | RefBindingsTy B = state->get<RefBindings>(); |
Anna Zaks | af498a2 | 2011-10-25 19:56:48 +0000 | [diff] [blame] | 3584 | ExplodedNode *Pred = Ctx.getPredecessor(); |
Jordy Rose | 38f17d6 | 2011-08-23 19:01:07 +0000 | [diff] [blame] | 3585 | |
Jordan Rose | 166d502 | 2012-11-02 01:54:06 +0000 | [diff] [blame] | 3586 | for (RefBindingsTy::iterator I = B.begin(), E = B.end(); I != E; ++I) { |
Jordan Rose | 4ee1c55 | 2012-12-06 18:58:18 +0000 | [diff] [blame] | 3587 | state = handleAutoreleaseCounts(state, Pred, /*Tag=*/0, Ctx, |
| 3588 | I->first, I->second); |
Jordy Rose | 8d22863 | 2011-08-23 20:07:14 +0000 | [diff] [blame] | 3589 | if (!state) |
Jordy Rose | 38f17d6 | 2011-08-23 19:01:07 +0000 | [diff] [blame] | 3590 | return; |
| 3591 | } |
| 3592 | |
Ted Kremenek | 0cf3d47 | 2012-02-07 00:24:33 +0000 | [diff] [blame] | 3593 | // If the current LocationContext has a parent, don't check for leaks. |
| 3594 | // We will do that later. |
Anna Zaks | 8d6b43c | 2012-08-14 00:36:15 +0000 | [diff] [blame] | 3595 | // FIXME: we should instead check for imbalances of the retain/releases, |
Ted Kremenek | 0cf3d47 | 2012-02-07 00:24:33 +0000 | [diff] [blame] | 3596 | // and suggest annotations. |
| 3597 | if (Ctx.getLocationContext()->getParent()) |
| 3598 | return; |
| 3599 | |
Jordy Rose | 38f17d6 | 2011-08-23 19:01:07 +0000 | [diff] [blame] | 3600 | B = state->get<RefBindings>(); |
| 3601 | SmallVector<SymbolRef, 10> Leaked; |
| 3602 | |
Jordan Rose | 166d502 | 2012-11-02 01:54:06 +0000 | [diff] [blame] | 3603 | for (RefBindingsTy::iterator I = B.begin(), E = B.end(); I != E; ++I) |
Jordy Rose | 8d22863 | 2011-08-23 20:07:14 +0000 | [diff] [blame] | 3604 | state = handleSymbolDeath(state, I->first, I->second, Leaked); |
Jordy Rose | 38f17d6 | 2011-08-23 19:01:07 +0000 | [diff] [blame] | 3605 | |
Jordan Rose | 2bce86c | 2012-08-18 00:30:16 +0000 | [diff] [blame] | 3606 | processLeaks(state, Leaked, Ctx, Pred); |
Jordy Rose | 38f17d6 | 2011-08-23 19:01:07 +0000 | [diff] [blame] | 3607 | } |
| 3608 | |
| 3609 | const ProgramPointTag * |
Jordy Rose | 910c405 | 2011-09-02 06:44:22 +0000 | [diff] [blame] | 3610 | RetainCountChecker::getDeadSymbolTag(SymbolRef sym) const { |
Jordy Rose | 38f17d6 | 2011-08-23 19:01:07 +0000 | [diff] [blame] | 3611 | const SimpleProgramPointTag *&tag = DeadSymbolTags[sym]; |
| 3612 | if (!tag) { |
Dylan Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 3613 | SmallString<64> buf; |
Jordy Rose | 38f17d6 | 2011-08-23 19:01:07 +0000 | [diff] [blame] | 3614 | llvm::raw_svector_ostream out(buf); |
Anna Zaks | f62ceec | 2011-12-05 18:58:11 +0000 | [diff] [blame] | 3615 | out << "RetainCountChecker : Dead Symbol : "; |
| 3616 | sym->dumpToStream(out); |
Jordy Rose | 38f17d6 | 2011-08-23 19:01:07 +0000 | [diff] [blame] | 3617 | tag = new SimpleProgramPointTag(out.str()); |
| 3618 | } |
| 3619 | return tag; |
| 3620 | } |
| 3621 | |
Jordy Rose | 910c405 | 2011-09-02 06:44:22 +0000 | [diff] [blame] | 3622 | void RetainCountChecker::checkDeadSymbols(SymbolReaper &SymReaper, |
| 3623 | CheckerContext &C) const { |
Jordy Rose | 38f17d6 | 2011-08-23 19:01:07 +0000 | [diff] [blame] | 3624 | ExplodedNode *Pred = C.getPredecessor(); |
| 3625 | |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 3626 | ProgramStateRef state = C.getState(); |
Jordan Rose | 166d502 | 2012-11-02 01:54:06 +0000 | [diff] [blame] | 3627 | RefBindingsTy B = state->get<RefBindings>(); |
Jordan Rose | 4ee1c55 | 2012-12-06 18:58:18 +0000 | [diff] [blame] | 3628 | SmallVector<SymbolRef, 10> Leaked; |
Jordy Rose | 38f17d6 | 2011-08-23 19:01:07 +0000 | [diff] [blame] | 3629 | |
| 3630 | // Update counts from autorelease pools |
| 3631 | for (SymbolReaper::dead_iterator I = SymReaper.dead_begin(), |
| 3632 | E = SymReaper.dead_end(); I != E; ++I) { |
| 3633 | SymbolRef Sym = *I; |
| 3634 | if (const RefVal *T = B.lookup(Sym)){ |
| 3635 | // Use the symbol as the tag. |
| 3636 | // FIXME: This might not be as unique as we would like. |
Jordan Rose | 2bce86c | 2012-08-18 00:30:16 +0000 | [diff] [blame] | 3637 | const ProgramPointTag *Tag = getDeadSymbolTag(Sym); |
Jordan Rose | 4ee1c55 | 2012-12-06 18:58:18 +0000 | [diff] [blame] | 3638 | state = handleAutoreleaseCounts(state, Pred, Tag, C, Sym, *T); |
Jordy Rose | 8d22863 | 2011-08-23 20:07:14 +0000 | [diff] [blame] | 3639 | if (!state) |
Jordy Rose | 38f17d6 | 2011-08-23 19:01:07 +0000 | [diff] [blame] | 3640 | return; |
Jordan Rose | 4ee1c55 | 2012-12-06 18:58:18 +0000 | [diff] [blame] | 3641 | |
| 3642 | // Fetch the new reference count from the state, and use it to handle |
| 3643 | // this symbol. |
| 3644 | state = handleSymbolDeath(state, *I, *getRefBinding(state, Sym), Leaked); |
Jordy Rose | 38f17d6 | 2011-08-23 19:01:07 +0000 | [diff] [blame] | 3645 | } |
| 3646 | } |
| 3647 | |
Jordan Rose | 4ee1c55 | 2012-12-06 18:58:18 +0000 | [diff] [blame] | 3648 | if (Leaked.empty()) { |
| 3649 | C.addTransition(state); |
| 3650 | return; |
Jordy Rose | 38f17d6 | 2011-08-23 19:01:07 +0000 | [diff] [blame] | 3651 | } |
| 3652 | |
Jordan Rose | 2bce86c | 2012-08-18 00:30:16 +0000 | [diff] [blame] | 3653 | Pred = processLeaks(state, Leaked, C, Pred); |
Jordy Rose | 38f17d6 | 2011-08-23 19:01:07 +0000 | [diff] [blame] | 3654 | |
| 3655 | // Did we cache out? |
| 3656 | if (!Pred) |
| 3657 | return; |
| 3658 | |
| 3659 | // Now generate a new node that nukes the old bindings. |
Jordan Rose | 4ee1c55 | 2012-12-06 18:58:18 +0000 | [diff] [blame] | 3660 | // The only bindings left at this point are the leaked symbols. |
Jordan Rose | 166d502 | 2012-11-02 01:54:06 +0000 | [diff] [blame] | 3661 | RefBindingsTy::Factory &F = state->get_context<RefBindings>(); |
Jordan Rose | 4ee1c55 | 2012-12-06 18:58:18 +0000 | [diff] [blame] | 3662 | B = state->get<RefBindings>(); |
Jordy Rose | 38f17d6 | 2011-08-23 19:01:07 +0000 | [diff] [blame] | 3663 | |
Jordan Rose | 4ee1c55 | 2012-12-06 18:58:18 +0000 | [diff] [blame] | 3664 | for (SmallVectorImpl<SymbolRef>::iterator I = Leaked.begin(), |
| 3665 | E = Leaked.end(); |
| 3666 | I != E; ++I) |
Jordy Rose | 38f17d6 | 2011-08-23 19:01:07 +0000 | [diff] [blame] | 3667 | B = F.remove(B, *I); |
| 3668 | |
| 3669 | state = state->set<RefBindings>(B); |
Anna Zaks | 0bd6b11 | 2011-10-26 21:06:34 +0000 | [diff] [blame] | 3670 | C.addTransition(state, Pred); |
Jordy Rose | 38f17d6 | 2011-08-23 19:01:07 +0000 | [diff] [blame] | 3671 | } |
| 3672 | |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 3673 | void RetainCountChecker::printState(raw_ostream &Out, ProgramStateRef State, |
Jordy Rose | 910c405 | 2011-09-02 06:44:22 +0000 | [diff] [blame] | 3674 | const char *NL, const char *Sep) const { |
Jordy Rose | dbd658e | 2011-08-28 19:11:56 +0000 | [diff] [blame] | 3675 | |
Jordan Rose | 166d502 | 2012-11-02 01:54:06 +0000 | [diff] [blame] | 3676 | RefBindingsTy B = State->get<RefBindings>(); |
Jordy Rose | dbd658e | 2011-08-28 19:11:56 +0000 | [diff] [blame] | 3677 | |
| 3678 | if (!B.isEmpty()) |
| 3679 | Out << Sep << NL; |
| 3680 | |
Jordan Rose | 166d502 | 2012-11-02 01:54:06 +0000 | [diff] [blame] | 3681 | for (RefBindingsTy::iterator I = B.begin(), E = B.end(); I != E; ++I) { |
Jordy Rose | dbd658e | 2011-08-28 19:11:56 +0000 | [diff] [blame] | 3682 | Out << I->first << " : "; |
| 3683 | I->second.print(Out); |
| 3684 | Out << NL; |
| 3685 | } |
Jordy Rose | dbd658e | 2011-08-28 19:11:56 +0000 | [diff] [blame] | 3686 | } |
| 3687 | |
| 3688 | //===----------------------------------------------------------------------===// |
Jordy Rose | 910c405 | 2011-09-02 06:44:22 +0000 | [diff] [blame] | 3689 | // Checker registration. |
Ted Kremenek | 6b3a0f7 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 3690 | //===----------------------------------------------------------------------===// |
| 3691 | |
Jordy Rose | 17a38e2 | 2011-09-02 05:55:19 +0000 | [diff] [blame] | 3692 | void ento::registerRetainCountChecker(CheckerManager &Mgr) { |
Jordy Rose | 910c405 | 2011-09-02 06:44:22 +0000 | [diff] [blame] | 3693 | Mgr.registerChecker<RetainCountChecker>(); |
Jordy Rose | 17a38e2 | 2011-09-02 05:55:19 +0000 | [diff] [blame] | 3694 | } |
| 3695 | |