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