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