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