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