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