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