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