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