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