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