xref: /openbsd-src/gnu/llvm/clang/lib/StaticAnalyzer/Checkers/CheckObjCDealloc.cpp (revision 12c855180aad702bbcca06e0398d774beeafb155)
1e5dd7070Spatrick //==- CheckObjCDealloc.cpp - Check ObjC -dealloc implementation --*- C++ -*-==//
2e5dd7070Spatrick //
3e5dd7070Spatrick // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4e5dd7070Spatrick // See https://llvm.org/LICENSE.txt for license information.
5e5dd7070Spatrick // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6e5dd7070Spatrick //
7e5dd7070Spatrick //===----------------------------------------------------------------------===//
8e5dd7070Spatrick //
9e5dd7070Spatrick //  This checker analyzes Objective-C -dealloc methods and their callees
10e5dd7070Spatrick //  to warn about improper releasing of instance variables that back synthesized
11e5dd7070Spatrick // properties. It warns about missing releases in the following cases:
12e5dd7070Spatrick //  - When a class has a synthesized instance variable for a 'retain' or 'copy'
13e5dd7070Spatrick //    property and lacks a -dealloc method in its implementation.
14e5dd7070Spatrick //  - When a class has a synthesized instance variable for a 'retain'/'copy'
15e5dd7070Spatrick //   property but the ivar is not released in -dealloc by either -release
16e5dd7070Spatrick //   or by nilling out the property.
17e5dd7070Spatrick //
18e5dd7070Spatrick //  It warns about extra releases in -dealloc (but not in callees) when a
19e5dd7070Spatrick //  synthesized instance variable is released in the following cases:
20e5dd7070Spatrick //  - When the property is 'assign' and is not 'readonly'.
21e5dd7070Spatrick //  - When the property is 'weak'.
22e5dd7070Spatrick //
23e5dd7070Spatrick //  This checker only warns for instance variables synthesized to back
24e5dd7070Spatrick //  properties. Handling the more general case would require inferring whether
25e5dd7070Spatrick //  an instance variable is stored retained or not. For synthesized properties,
26e5dd7070Spatrick //  this is specified in the property declaration itself.
27e5dd7070Spatrick //
28e5dd7070Spatrick //===----------------------------------------------------------------------===//
29e5dd7070Spatrick 
30e5dd7070Spatrick #include "clang/StaticAnalyzer/Checkers/BuiltinCheckerRegistration.h"
31e5dd7070Spatrick #include "clang/Analysis/PathDiagnostic.h"
32e5dd7070Spatrick #include "clang/AST/Attr.h"
33e5dd7070Spatrick #include "clang/AST/DeclObjC.h"
34e5dd7070Spatrick #include "clang/AST/Expr.h"
35e5dd7070Spatrick #include "clang/AST/ExprObjC.h"
36e5dd7070Spatrick #include "clang/Basic/LangOptions.h"
37e5dd7070Spatrick #include "clang/Basic/TargetInfo.h"
38e5dd7070Spatrick #include "clang/StaticAnalyzer/Core/BugReporter/BugReporter.h"
39e5dd7070Spatrick #include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
40e5dd7070Spatrick #include "clang/StaticAnalyzer/Core/Checker.h"
41e5dd7070Spatrick #include "clang/StaticAnalyzer/Core/PathSensitive/AnalysisManager.h"
42e5dd7070Spatrick #include "clang/StaticAnalyzer/Core/PathSensitive/CallEvent.h"
43e5dd7070Spatrick #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
44e5dd7070Spatrick #include "clang/StaticAnalyzer/Core/PathSensitive/ProgramState.h"
45e5dd7070Spatrick #include "clang/StaticAnalyzer/Core/PathSensitive/ProgramStateTrait.h"
46e5dd7070Spatrick #include "clang/StaticAnalyzer/Core/PathSensitive/SymbolManager.h"
47e5dd7070Spatrick #include "llvm/Support/raw_ostream.h"
48*12c85518Srobert #include <optional>
49e5dd7070Spatrick 
50e5dd7070Spatrick using namespace clang;
51e5dd7070Spatrick using namespace ento;
52e5dd7070Spatrick 
53e5dd7070Spatrick /// Indicates whether an instance variable is required to be released in
54e5dd7070Spatrick /// -dealloc.
55e5dd7070Spatrick enum class ReleaseRequirement {
56e5dd7070Spatrick   /// The instance variable must be released, either by calling
57e5dd7070Spatrick   /// -release on it directly or by nilling it out with a property setter.
58e5dd7070Spatrick   MustRelease,
59e5dd7070Spatrick 
60e5dd7070Spatrick   /// The instance variable must not be directly released with -release.
61e5dd7070Spatrick   MustNotReleaseDirectly,
62e5dd7070Spatrick 
63e5dd7070Spatrick   /// The requirement for the instance variable could not be determined.
64e5dd7070Spatrick   Unknown
65e5dd7070Spatrick };
66e5dd7070Spatrick 
67e5dd7070Spatrick /// Returns true if the property implementation is synthesized and the
68e5dd7070Spatrick /// type of the property is retainable.
isSynthesizedRetainableProperty(const ObjCPropertyImplDecl * I,const ObjCIvarDecl ** ID,const ObjCPropertyDecl ** PD)69e5dd7070Spatrick static bool isSynthesizedRetainableProperty(const ObjCPropertyImplDecl *I,
70e5dd7070Spatrick                                             const ObjCIvarDecl **ID,
71e5dd7070Spatrick                                             const ObjCPropertyDecl **PD) {
72e5dd7070Spatrick 
73e5dd7070Spatrick   if (I->getPropertyImplementation() != ObjCPropertyImplDecl::Synthesize)
74e5dd7070Spatrick     return false;
75e5dd7070Spatrick 
76e5dd7070Spatrick   (*ID) = I->getPropertyIvarDecl();
77e5dd7070Spatrick   if (!(*ID))
78e5dd7070Spatrick     return false;
79e5dd7070Spatrick 
80e5dd7070Spatrick   QualType T = (*ID)->getType();
81e5dd7070Spatrick   if (!T->isObjCRetainableType())
82e5dd7070Spatrick     return false;
83e5dd7070Spatrick 
84e5dd7070Spatrick   (*PD) = I->getPropertyDecl();
85e5dd7070Spatrick   // Shouldn't be able to synthesize a property that doesn't exist.
86e5dd7070Spatrick   assert(*PD);
87e5dd7070Spatrick 
88e5dd7070Spatrick   return true;
89e5dd7070Spatrick }
90e5dd7070Spatrick 
91e5dd7070Spatrick namespace {
92e5dd7070Spatrick 
93e5dd7070Spatrick class ObjCDeallocChecker
94e5dd7070Spatrick     : public Checker<check::ASTDecl<ObjCImplementationDecl>,
95e5dd7070Spatrick                      check::PreObjCMessage, check::PostObjCMessage,
96e5dd7070Spatrick                      check::PreCall,
97e5dd7070Spatrick                      check::BeginFunction, check::EndFunction,
98e5dd7070Spatrick                      eval::Assume,
99e5dd7070Spatrick                      check::PointerEscape,
100e5dd7070Spatrick                      check::PreStmt<ReturnStmt>> {
101e5dd7070Spatrick 
102e5dd7070Spatrick   mutable IdentifierInfo *NSObjectII, *SenTestCaseII, *XCTestCaseII,
103e5dd7070Spatrick       *Block_releaseII, *CIFilterII;
104e5dd7070Spatrick 
105e5dd7070Spatrick   mutable Selector DeallocSel, ReleaseSel;
106e5dd7070Spatrick 
107e5dd7070Spatrick   std::unique_ptr<BugType> MissingReleaseBugType;
108e5dd7070Spatrick   std::unique_ptr<BugType> ExtraReleaseBugType;
109e5dd7070Spatrick   std::unique_ptr<BugType> MistakenDeallocBugType;
110e5dd7070Spatrick 
111e5dd7070Spatrick public:
112e5dd7070Spatrick   ObjCDeallocChecker();
113e5dd7070Spatrick 
114e5dd7070Spatrick   void checkASTDecl(const ObjCImplementationDecl *D, AnalysisManager& Mgr,
115e5dd7070Spatrick                     BugReporter &BR) const;
116e5dd7070Spatrick   void checkBeginFunction(CheckerContext &Ctx) const;
117e5dd7070Spatrick   void checkPreObjCMessage(const ObjCMethodCall &M, CheckerContext &C) const;
118e5dd7070Spatrick   void checkPreCall(const CallEvent &Call, CheckerContext &C) const;
119e5dd7070Spatrick   void checkPostObjCMessage(const ObjCMethodCall &M, CheckerContext &C) const;
120e5dd7070Spatrick 
121e5dd7070Spatrick   ProgramStateRef evalAssume(ProgramStateRef State, SVal Cond,
122e5dd7070Spatrick                              bool Assumption) const;
123e5dd7070Spatrick 
124e5dd7070Spatrick   ProgramStateRef checkPointerEscape(ProgramStateRef State,
125e5dd7070Spatrick                                      const InvalidatedSymbols &Escaped,
126e5dd7070Spatrick                                      const CallEvent *Call,
127e5dd7070Spatrick                                      PointerEscapeKind Kind) const;
128e5dd7070Spatrick   void checkPreStmt(const ReturnStmt *RS, CheckerContext &C) const;
129e5dd7070Spatrick   void checkEndFunction(const ReturnStmt *RS, CheckerContext &Ctx) const;
130e5dd7070Spatrick 
131e5dd7070Spatrick private:
132e5dd7070Spatrick   void diagnoseMissingReleases(CheckerContext &C) const;
133e5dd7070Spatrick 
134e5dd7070Spatrick   bool diagnoseExtraRelease(SymbolRef ReleasedValue, const ObjCMethodCall &M,
135e5dd7070Spatrick                             CheckerContext &C) const;
136e5dd7070Spatrick 
137e5dd7070Spatrick   bool diagnoseMistakenDealloc(SymbolRef DeallocedValue,
138e5dd7070Spatrick                                const ObjCMethodCall &M,
139e5dd7070Spatrick                                CheckerContext &C) const;
140e5dd7070Spatrick 
141e5dd7070Spatrick   SymbolRef getValueReleasedByNillingOut(const ObjCMethodCall &M,
142e5dd7070Spatrick                                          CheckerContext &C) const;
143e5dd7070Spatrick 
144e5dd7070Spatrick   const ObjCIvarRegion *getIvarRegionForIvarSymbol(SymbolRef IvarSym) const;
145e5dd7070Spatrick   SymbolRef getInstanceSymbolFromIvarSymbol(SymbolRef IvarSym) const;
146e5dd7070Spatrick 
147e5dd7070Spatrick   const ObjCPropertyImplDecl*
148e5dd7070Spatrick   findPropertyOnDeallocatingInstance(SymbolRef IvarSym,
149e5dd7070Spatrick                                      CheckerContext &C) const;
150e5dd7070Spatrick 
151e5dd7070Spatrick   ReleaseRequirement
152e5dd7070Spatrick   getDeallocReleaseRequirement(const ObjCPropertyImplDecl *PropImpl) const;
153e5dd7070Spatrick 
154e5dd7070Spatrick   bool isInInstanceDealloc(const CheckerContext &C, SVal &SelfValOut) const;
155e5dd7070Spatrick   bool isInInstanceDealloc(const CheckerContext &C, const LocationContext *LCtx,
156e5dd7070Spatrick                            SVal &SelfValOut) const;
157e5dd7070Spatrick   bool instanceDeallocIsOnStack(const CheckerContext &C,
158e5dd7070Spatrick                                 SVal &InstanceValOut) const;
159e5dd7070Spatrick 
160e5dd7070Spatrick   bool isSuperDeallocMessage(const ObjCMethodCall &M) const;
161e5dd7070Spatrick 
162e5dd7070Spatrick   const ObjCImplDecl *getContainingObjCImpl(const LocationContext *LCtx) const;
163e5dd7070Spatrick 
164e5dd7070Spatrick   const ObjCPropertyDecl *
165e5dd7070Spatrick   findShadowedPropertyDecl(const ObjCPropertyImplDecl *PropImpl) const;
166e5dd7070Spatrick 
167e5dd7070Spatrick   void transitionToReleaseValue(CheckerContext &C, SymbolRef Value) const;
168e5dd7070Spatrick   ProgramStateRef removeValueRequiringRelease(ProgramStateRef State,
169e5dd7070Spatrick                                               SymbolRef InstanceSym,
170e5dd7070Spatrick                                               SymbolRef ValueSym) const;
171e5dd7070Spatrick 
172e5dd7070Spatrick   void initIdentifierInfoAndSelectors(ASTContext &Ctx) const;
173e5dd7070Spatrick 
174e5dd7070Spatrick   bool classHasSeparateTeardown(const ObjCInterfaceDecl *ID) const;
175e5dd7070Spatrick 
176e5dd7070Spatrick   bool isReleasedByCIFilterDealloc(const ObjCPropertyImplDecl *PropImpl) const;
177e5dd7070Spatrick   bool isNibLoadedIvarWithoutRetain(const ObjCPropertyImplDecl *PropImpl) const;
178e5dd7070Spatrick };
179e5dd7070Spatrick } // End anonymous namespace.
180e5dd7070Spatrick 
181e5dd7070Spatrick 
182e5dd7070Spatrick /// Maps from the symbol for a class instance to the set of
183e5dd7070Spatrick /// symbols remaining that must be released in -dealloc.
REGISTER_SET_FACTORY_WITH_PROGRAMSTATE(SymbolSet,SymbolRef)184e5dd7070Spatrick REGISTER_SET_FACTORY_WITH_PROGRAMSTATE(SymbolSet, SymbolRef)
185e5dd7070Spatrick REGISTER_MAP_WITH_PROGRAMSTATE(UnreleasedIvarMap, SymbolRef, SymbolSet)
186e5dd7070Spatrick 
187e5dd7070Spatrick 
188e5dd7070Spatrick /// An AST check that diagnose when the class requires a -dealloc method and
189e5dd7070Spatrick /// is missing one.
190e5dd7070Spatrick void ObjCDeallocChecker::checkASTDecl(const ObjCImplementationDecl *D,
191e5dd7070Spatrick                                       AnalysisManager &Mgr,
192e5dd7070Spatrick                                       BugReporter &BR) const {
193e5dd7070Spatrick   assert(Mgr.getLangOpts().getGC() != LangOptions::GCOnly);
194e5dd7070Spatrick   assert(!Mgr.getLangOpts().ObjCAutoRefCount);
195e5dd7070Spatrick   initIdentifierInfoAndSelectors(Mgr.getASTContext());
196e5dd7070Spatrick 
197e5dd7070Spatrick   const ObjCInterfaceDecl *ID = D->getClassInterface();
198e5dd7070Spatrick   // If the class is known to have a lifecycle with a separate teardown method
199e5dd7070Spatrick   // then it may not require a -dealloc method.
200e5dd7070Spatrick   if (classHasSeparateTeardown(ID))
201e5dd7070Spatrick     return;
202e5dd7070Spatrick 
203e5dd7070Spatrick   // Does the class contain any synthesized properties that are retainable?
204e5dd7070Spatrick   // If not, skip the check entirely.
205e5dd7070Spatrick   const ObjCPropertyImplDecl *PropImplRequiringRelease = nullptr;
206e5dd7070Spatrick   bool HasOthers = false;
207e5dd7070Spatrick   for (const auto *I : D->property_impls()) {
208e5dd7070Spatrick     if (getDeallocReleaseRequirement(I) == ReleaseRequirement::MustRelease) {
209e5dd7070Spatrick       if (!PropImplRequiringRelease)
210e5dd7070Spatrick         PropImplRequiringRelease = I;
211e5dd7070Spatrick       else {
212e5dd7070Spatrick         HasOthers = true;
213e5dd7070Spatrick         break;
214e5dd7070Spatrick       }
215e5dd7070Spatrick     }
216e5dd7070Spatrick   }
217e5dd7070Spatrick 
218e5dd7070Spatrick   if (!PropImplRequiringRelease)
219e5dd7070Spatrick     return;
220e5dd7070Spatrick 
221e5dd7070Spatrick   const ObjCMethodDecl *MD = nullptr;
222e5dd7070Spatrick 
223e5dd7070Spatrick   // Scan the instance methods for "dealloc".
224e5dd7070Spatrick   for (const auto *I : D->instance_methods()) {
225e5dd7070Spatrick     if (I->getSelector() == DeallocSel) {
226e5dd7070Spatrick       MD = I;
227e5dd7070Spatrick       break;
228e5dd7070Spatrick     }
229e5dd7070Spatrick   }
230e5dd7070Spatrick 
231e5dd7070Spatrick   if (!MD) { // No dealloc found.
232e5dd7070Spatrick     const char* Name = "Missing -dealloc";
233e5dd7070Spatrick 
234e5dd7070Spatrick     std::string Buf;
235e5dd7070Spatrick     llvm::raw_string_ostream OS(Buf);
236e5dd7070Spatrick     OS << "'" << *D << "' lacks a 'dealloc' instance method but "
237e5dd7070Spatrick        << "must release '" << *PropImplRequiringRelease->getPropertyIvarDecl()
238e5dd7070Spatrick        << "'";
239e5dd7070Spatrick 
240e5dd7070Spatrick     if (HasOthers)
241e5dd7070Spatrick       OS << " and others";
242e5dd7070Spatrick     PathDiagnosticLocation DLoc =
243e5dd7070Spatrick         PathDiagnosticLocation::createBegin(D, BR.getSourceManager());
244e5dd7070Spatrick 
245e5dd7070Spatrick     BR.EmitBasicReport(D, this, Name, categories::CoreFoundationObjectiveC,
246e5dd7070Spatrick                        OS.str(), DLoc);
247e5dd7070Spatrick     return;
248e5dd7070Spatrick   }
249e5dd7070Spatrick }
250e5dd7070Spatrick 
251e5dd7070Spatrick /// If this is the beginning of -dealloc, mark the values initially stored in
252e5dd7070Spatrick /// instance variables that must be released by the end of -dealloc
253e5dd7070Spatrick /// as unreleased in the state.
checkBeginFunction(CheckerContext & C) const254e5dd7070Spatrick void ObjCDeallocChecker::checkBeginFunction(
255e5dd7070Spatrick     CheckerContext &C) const {
256e5dd7070Spatrick   initIdentifierInfoAndSelectors(C.getASTContext());
257e5dd7070Spatrick 
258e5dd7070Spatrick   // Only do this if the current method is -dealloc.
259e5dd7070Spatrick   SVal SelfVal;
260e5dd7070Spatrick   if (!isInInstanceDealloc(C, SelfVal))
261e5dd7070Spatrick     return;
262e5dd7070Spatrick 
263e5dd7070Spatrick   SymbolRef SelfSymbol = SelfVal.getAsSymbol();
264e5dd7070Spatrick 
265e5dd7070Spatrick   const LocationContext *LCtx = C.getLocationContext();
266e5dd7070Spatrick   ProgramStateRef InitialState = C.getState();
267e5dd7070Spatrick 
268e5dd7070Spatrick   ProgramStateRef State = InitialState;
269e5dd7070Spatrick 
270e5dd7070Spatrick   SymbolSet::Factory &F = State->getStateManager().get_context<SymbolSet>();
271e5dd7070Spatrick 
272e5dd7070Spatrick   // Symbols that must be released by the end of the -dealloc;
273e5dd7070Spatrick   SymbolSet RequiredReleases = F.getEmptySet();
274e5dd7070Spatrick 
275e5dd7070Spatrick   // If we're an inlined -dealloc, we should add our symbols to the existing
276e5dd7070Spatrick   // set from our subclass.
277e5dd7070Spatrick   if (const SymbolSet *CurrSet = State->get<UnreleasedIvarMap>(SelfSymbol))
278e5dd7070Spatrick     RequiredReleases = *CurrSet;
279e5dd7070Spatrick 
280e5dd7070Spatrick   for (auto *PropImpl : getContainingObjCImpl(LCtx)->property_impls()) {
281e5dd7070Spatrick     ReleaseRequirement Requirement = getDeallocReleaseRequirement(PropImpl);
282e5dd7070Spatrick     if (Requirement != ReleaseRequirement::MustRelease)
283e5dd7070Spatrick       continue;
284e5dd7070Spatrick 
285e5dd7070Spatrick     SVal LVal = State->getLValue(PropImpl->getPropertyIvarDecl(), SelfVal);
286*12c85518Srobert     std::optional<Loc> LValLoc = LVal.getAs<Loc>();
287e5dd7070Spatrick     if (!LValLoc)
288e5dd7070Spatrick       continue;
289e5dd7070Spatrick 
290*12c85518Srobert     SVal InitialVal = State->getSVal(*LValLoc);
291e5dd7070Spatrick     SymbolRef Symbol = InitialVal.getAsSymbol();
292e5dd7070Spatrick     if (!Symbol || !isa<SymbolRegionValue>(Symbol))
293e5dd7070Spatrick       continue;
294e5dd7070Spatrick 
295e5dd7070Spatrick     // Mark the value as requiring a release.
296e5dd7070Spatrick     RequiredReleases = F.add(RequiredReleases, Symbol);
297e5dd7070Spatrick   }
298e5dd7070Spatrick 
299e5dd7070Spatrick   if (!RequiredReleases.isEmpty()) {
300e5dd7070Spatrick     State = State->set<UnreleasedIvarMap>(SelfSymbol, RequiredReleases);
301e5dd7070Spatrick   }
302e5dd7070Spatrick 
303e5dd7070Spatrick   if (State != InitialState) {
304e5dd7070Spatrick     C.addTransition(State);
305e5dd7070Spatrick   }
306e5dd7070Spatrick }
307e5dd7070Spatrick 
308e5dd7070Spatrick /// Given a symbol for an ivar, return the ivar region it was loaded from.
309e5dd7070Spatrick /// Returns nullptr if the instance symbol cannot be found.
310e5dd7070Spatrick const ObjCIvarRegion *
getIvarRegionForIvarSymbol(SymbolRef IvarSym) const311e5dd7070Spatrick ObjCDeallocChecker::getIvarRegionForIvarSymbol(SymbolRef IvarSym) const {
312e5dd7070Spatrick   return dyn_cast_or_null<ObjCIvarRegion>(IvarSym->getOriginRegion());
313e5dd7070Spatrick }
314e5dd7070Spatrick 
315e5dd7070Spatrick /// Given a symbol for an ivar, return a symbol for the instance containing
316e5dd7070Spatrick /// the ivar. Returns nullptr if the instance symbol cannot be found.
317e5dd7070Spatrick SymbolRef
getInstanceSymbolFromIvarSymbol(SymbolRef IvarSym) const318e5dd7070Spatrick ObjCDeallocChecker::getInstanceSymbolFromIvarSymbol(SymbolRef IvarSym) const {
319e5dd7070Spatrick 
320e5dd7070Spatrick   const ObjCIvarRegion *IvarRegion = getIvarRegionForIvarSymbol(IvarSym);
321e5dd7070Spatrick   if (!IvarRegion)
322e5dd7070Spatrick     return nullptr;
323e5dd7070Spatrick 
324e5dd7070Spatrick   return IvarRegion->getSymbolicBase()->getSymbol();
325e5dd7070Spatrick }
326e5dd7070Spatrick 
327e5dd7070Spatrick /// If we are in -dealloc or -dealloc is on the stack, handle the call if it is
328e5dd7070Spatrick /// a release or a nilling-out property setter.
checkPreObjCMessage(const ObjCMethodCall & M,CheckerContext & C) const329e5dd7070Spatrick void ObjCDeallocChecker::checkPreObjCMessage(
330e5dd7070Spatrick     const ObjCMethodCall &M, CheckerContext &C) const {
331e5dd7070Spatrick   // Only run if -dealloc is on the stack.
332e5dd7070Spatrick   SVal DeallocedInstance;
333e5dd7070Spatrick   if (!instanceDeallocIsOnStack(C, DeallocedInstance))
334e5dd7070Spatrick     return;
335e5dd7070Spatrick 
336e5dd7070Spatrick   SymbolRef ReleasedValue = nullptr;
337e5dd7070Spatrick 
338e5dd7070Spatrick   if (M.getSelector() == ReleaseSel) {
339e5dd7070Spatrick     ReleasedValue = M.getReceiverSVal().getAsSymbol();
340e5dd7070Spatrick   } else if (M.getSelector() == DeallocSel && !M.isReceiverSelfOrSuper()) {
341e5dd7070Spatrick     if (diagnoseMistakenDealloc(M.getReceiverSVal().getAsSymbol(), M, C))
342e5dd7070Spatrick       return;
343e5dd7070Spatrick   }
344e5dd7070Spatrick 
345e5dd7070Spatrick   if (ReleasedValue) {
346e5dd7070Spatrick     // An instance variable symbol was released with -release:
347e5dd7070Spatrick     //    [_property release];
348e5dd7070Spatrick     if (diagnoseExtraRelease(ReleasedValue,M, C))
349e5dd7070Spatrick       return;
350e5dd7070Spatrick   } else {
351e5dd7070Spatrick     // An instance variable symbol was released nilling out its property:
352e5dd7070Spatrick     //    self.property = nil;
353e5dd7070Spatrick     ReleasedValue = getValueReleasedByNillingOut(M, C);
354e5dd7070Spatrick   }
355e5dd7070Spatrick 
356e5dd7070Spatrick   if (!ReleasedValue)
357e5dd7070Spatrick     return;
358e5dd7070Spatrick 
359e5dd7070Spatrick   transitionToReleaseValue(C, ReleasedValue);
360e5dd7070Spatrick }
361e5dd7070Spatrick 
362e5dd7070Spatrick /// If we are in -dealloc or -dealloc is on the stack, handle the call if it is
363e5dd7070Spatrick /// call to Block_release().
checkPreCall(const CallEvent & Call,CheckerContext & C) const364e5dd7070Spatrick void ObjCDeallocChecker::checkPreCall(const CallEvent &Call,
365e5dd7070Spatrick                                       CheckerContext &C) const {
366e5dd7070Spatrick   const IdentifierInfo *II = Call.getCalleeIdentifier();
367e5dd7070Spatrick   if (II != Block_releaseII)
368e5dd7070Spatrick     return;
369e5dd7070Spatrick 
370e5dd7070Spatrick   if (Call.getNumArgs() != 1)
371e5dd7070Spatrick     return;
372e5dd7070Spatrick 
373e5dd7070Spatrick   SymbolRef ReleasedValue = Call.getArgSVal(0).getAsSymbol();
374e5dd7070Spatrick   if (!ReleasedValue)
375e5dd7070Spatrick     return;
376e5dd7070Spatrick 
377e5dd7070Spatrick   transitionToReleaseValue(C, ReleasedValue);
378e5dd7070Spatrick }
379e5dd7070Spatrick /// If the message was a call to '[super dealloc]', diagnose any missing
380e5dd7070Spatrick /// releases.
checkPostObjCMessage(const ObjCMethodCall & M,CheckerContext & C) const381e5dd7070Spatrick void ObjCDeallocChecker::checkPostObjCMessage(
382e5dd7070Spatrick     const ObjCMethodCall &M, CheckerContext &C) const {
383e5dd7070Spatrick   // We perform this check post-message so that if the super -dealloc
384e5dd7070Spatrick   // calls a helper method and that this class overrides, any ivars released in
385e5dd7070Spatrick   // the helper method will be recorded before checking.
386e5dd7070Spatrick   if (isSuperDeallocMessage(M))
387e5dd7070Spatrick     diagnoseMissingReleases(C);
388e5dd7070Spatrick }
389e5dd7070Spatrick 
390e5dd7070Spatrick /// Check for missing releases even when -dealloc does not call
391e5dd7070Spatrick /// '[super dealloc]'.
checkEndFunction(const ReturnStmt * RS,CheckerContext & C) const392e5dd7070Spatrick void ObjCDeallocChecker::checkEndFunction(
393e5dd7070Spatrick     const ReturnStmt *RS, CheckerContext &C) const {
394e5dd7070Spatrick   diagnoseMissingReleases(C);
395e5dd7070Spatrick }
396e5dd7070Spatrick 
397e5dd7070Spatrick /// Check for missing releases on early return.
checkPreStmt(const ReturnStmt * RS,CheckerContext & C) const398e5dd7070Spatrick void ObjCDeallocChecker::checkPreStmt(
399e5dd7070Spatrick     const ReturnStmt *RS, CheckerContext &C) const {
400e5dd7070Spatrick   diagnoseMissingReleases(C);
401e5dd7070Spatrick }
402e5dd7070Spatrick 
403e5dd7070Spatrick /// When a symbol is assumed to be nil, remove it from the set of symbols
404e5dd7070Spatrick /// require to be nil.
evalAssume(ProgramStateRef State,SVal Cond,bool Assumption) const405e5dd7070Spatrick ProgramStateRef ObjCDeallocChecker::evalAssume(ProgramStateRef State, SVal Cond,
406e5dd7070Spatrick                                                bool Assumption) const {
407e5dd7070Spatrick   if (State->get<UnreleasedIvarMap>().isEmpty())
408e5dd7070Spatrick     return State;
409e5dd7070Spatrick 
410a9ac8606Spatrick   auto *CondBSE = dyn_cast_or_null<BinarySymExpr>(Cond.getAsSymbol());
411e5dd7070Spatrick   if (!CondBSE)
412e5dd7070Spatrick     return State;
413e5dd7070Spatrick 
414e5dd7070Spatrick   BinaryOperator::Opcode OpCode = CondBSE->getOpcode();
415e5dd7070Spatrick   if (Assumption) {
416e5dd7070Spatrick     if (OpCode != BO_EQ)
417e5dd7070Spatrick       return State;
418e5dd7070Spatrick   } else {
419e5dd7070Spatrick     if (OpCode != BO_NE)
420e5dd7070Spatrick       return State;
421e5dd7070Spatrick   }
422e5dd7070Spatrick 
423e5dd7070Spatrick   SymbolRef NullSymbol = nullptr;
424e5dd7070Spatrick   if (auto *SIE = dyn_cast<SymIntExpr>(CondBSE)) {
425e5dd7070Spatrick     const llvm::APInt &RHS = SIE->getRHS();
426e5dd7070Spatrick     if (RHS != 0)
427e5dd7070Spatrick       return State;
428e5dd7070Spatrick     NullSymbol = SIE->getLHS();
429e5dd7070Spatrick   } else if (auto *SIE = dyn_cast<IntSymExpr>(CondBSE)) {
430e5dd7070Spatrick     const llvm::APInt &LHS = SIE->getLHS();
431e5dd7070Spatrick     if (LHS != 0)
432e5dd7070Spatrick       return State;
433e5dd7070Spatrick     NullSymbol = SIE->getRHS();
434e5dd7070Spatrick   } else {
435e5dd7070Spatrick     return State;
436e5dd7070Spatrick   }
437e5dd7070Spatrick 
438e5dd7070Spatrick   SymbolRef InstanceSymbol = getInstanceSymbolFromIvarSymbol(NullSymbol);
439e5dd7070Spatrick   if (!InstanceSymbol)
440e5dd7070Spatrick     return State;
441e5dd7070Spatrick 
442e5dd7070Spatrick   State = removeValueRequiringRelease(State, InstanceSymbol, NullSymbol);
443e5dd7070Spatrick 
444e5dd7070Spatrick   return State;
445e5dd7070Spatrick }
446e5dd7070Spatrick 
447e5dd7070Spatrick /// If a symbol escapes conservatively assume unseen code released it.
checkPointerEscape(ProgramStateRef State,const InvalidatedSymbols & Escaped,const CallEvent * Call,PointerEscapeKind Kind) const448e5dd7070Spatrick ProgramStateRef ObjCDeallocChecker::checkPointerEscape(
449e5dd7070Spatrick     ProgramStateRef State, const InvalidatedSymbols &Escaped,
450e5dd7070Spatrick     const CallEvent *Call, PointerEscapeKind Kind) const {
451e5dd7070Spatrick 
452e5dd7070Spatrick   if (State->get<UnreleasedIvarMap>().isEmpty())
453e5dd7070Spatrick     return State;
454e5dd7070Spatrick 
455e5dd7070Spatrick   // Don't treat calls to '[super dealloc]' as escaping for the purposes
456e5dd7070Spatrick   // of this checker. Because the checker diagnoses missing releases in the
457e5dd7070Spatrick   // post-message handler for '[super dealloc], escaping here would cause
458e5dd7070Spatrick   // the checker to never warn.
459e5dd7070Spatrick   auto *OMC = dyn_cast_or_null<ObjCMethodCall>(Call);
460e5dd7070Spatrick   if (OMC && isSuperDeallocMessage(*OMC))
461e5dd7070Spatrick     return State;
462e5dd7070Spatrick 
463e5dd7070Spatrick   for (const auto &Sym : Escaped) {
464e5dd7070Spatrick     if (!Call || (Call && !Call->isInSystemHeader())) {
465e5dd7070Spatrick       // If Sym is a symbol for an object with instance variables that
466e5dd7070Spatrick       // must be released, remove these obligations when the object escapes
467e5dd7070Spatrick       // unless via a call to a system function. System functions are
468e5dd7070Spatrick       // very unlikely to release instance variables on objects passed to them,
469e5dd7070Spatrick       // and are frequently called on 'self' in -dealloc (e.g., to remove
470e5dd7070Spatrick       // observers) -- we want to avoid false negatives from escaping on
471e5dd7070Spatrick       // them.
472e5dd7070Spatrick       State = State->remove<UnreleasedIvarMap>(Sym);
473e5dd7070Spatrick     }
474e5dd7070Spatrick 
475e5dd7070Spatrick 
476e5dd7070Spatrick     SymbolRef InstanceSymbol = getInstanceSymbolFromIvarSymbol(Sym);
477e5dd7070Spatrick     if (!InstanceSymbol)
478e5dd7070Spatrick       continue;
479e5dd7070Spatrick 
480e5dd7070Spatrick     State = removeValueRequiringRelease(State, InstanceSymbol, Sym);
481e5dd7070Spatrick   }
482e5dd7070Spatrick 
483e5dd7070Spatrick   return State;
484e5dd7070Spatrick }
485e5dd7070Spatrick 
486e5dd7070Spatrick /// Report any unreleased instance variables for the current instance being
487e5dd7070Spatrick /// dealloced.
diagnoseMissingReleases(CheckerContext & C) const488e5dd7070Spatrick void ObjCDeallocChecker::diagnoseMissingReleases(CheckerContext &C) const {
489e5dd7070Spatrick   ProgramStateRef State = C.getState();
490e5dd7070Spatrick 
491e5dd7070Spatrick   SVal SelfVal;
492e5dd7070Spatrick   if (!isInInstanceDealloc(C, SelfVal))
493e5dd7070Spatrick     return;
494e5dd7070Spatrick 
495e5dd7070Spatrick   const MemRegion *SelfRegion = SelfVal.castAs<loc::MemRegionVal>().getRegion();
496e5dd7070Spatrick   const LocationContext *LCtx = C.getLocationContext();
497e5dd7070Spatrick 
498e5dd7070Spatrick   ExplodedNode *ErrNode = nullptr;
499e5dd7070Spatrick 
500e5dd7070Spatrick   SymbolRef SelfSym = SelfVal.getAsSymbol();
501e5dd7070Spatrick   if (!SelfSym)
502e5dd7070Spatrick     return;
503e5dd7070Spatrick 
504e5dd7070Spatrick   const SymbolSet *OldUnreleased = State->get<UnreleasedIvarMap>(SelfSym);
505e5dd7070Spatrick   if (!OldUnreleased)
506e5dd7070Spatrick     return;
507e5dd7070Spatrick 
508e5dd7070Spatrick   SymbolSet NewUnreleased = *OldUnreleased;
509e5dd7070Spatrick   SymbolSet::Factory &F = State->getStateManager().get_context<SymbolSet>();
510e5dd7070Spatrick 
511e5dd7070Spatrick   ProgramStateRef InitialState = State;
512e5dd7070Spatrick 
513e5dd7070Spatrick   for (auto *IvarSymbol : *OldUnreleased) {
514e5dd7070Spatrick     const TypedValueRegion *TVR =
515e5dd7070Spatrick         cast<SymbolRegionValue>(IvarSymbol)->getRegion();
516e5dd7070Spatrick     const ObjCIvarRegion *IvarRegion = cast<ObjCIvarRegion>(TVR);
517e5dd7070Spatrick 
518e5dd7070Spatrick     // Don't warn if the ivar is not for this instance.
519e5dd7070Spatrick     if (SelfRegion != IvarRegion->getSuperRegion())
520e5dd7070Spatrick       continue;
521e5dd7070Spatrick 
522e5dd7070Spatrick     const ObjCIvarDecl *IvarDecl = IvarRegion->getDecl();
523e5dd7070Spatrick     // Prevent an inlined call to -dealloc in a super class from warning
524e5dd7070Spatrick     // about the values the subclass's -dealloc should release.
525e5dd7070Spatrick     if (IvarDecl->getContainingInterface() !=
526e5dd7070Spatrick         cast<ObjCMethodDecl>(LCtx->getDecl())->getClassInterface())
527e5dd7070Spatrick       continue;
528e5dd7070Spatrick 
529e5dd7070Spatrick     // Prevents diagnosing multiple times for the same instance variable
530e5dd7070Spatrick     // at, for example, both a return and at the end of the function.
531e5dd7070Spatrick     NewUnreleased = F.remove(NewUnreleased, IvarSymbol);
532e5dd7070Spatrick 
533e5dd7070Spatrick     if (State->getStateManager()
534e5dd7070Spatrick             .getConstraintManager()
535e5dd7070Spatrick             .isNull(State, IvarSymbol)
536e5dd7070Spatrick             .isConstrainedTrue()) {
537e5dd7070Spatrick       continue;
538e5dd7070Spatrick     }
539e5dd7070Spatrick 
540e5dd7070Spatrick     // A missing release manifests as a leak, so treat as a non-fatal error.
541e5dd7070Spatrick     if (!ErrNode)
542e5dd7070Spatrick       ErrNode = C.generateNonFatalErrorNode();
543e5dd7070Spatrick     // If we've already reached this node on another path, return without
544e5dd7070Spatrick     // diagnosing.
545e5dd7070Spatrick     if (!ErrNode)
546e5dd7070Spatrick       return;
547e5dd7070Spatrick 
548e5dd7070Spatrick     std::string Buf;
549e5dd7070Spatrick     llvm::raw_string_ostream OS(Buf);
550e5dd7070Spatrick 
551e5dd7070Spatrick     const ObjCInterfaceDecl *Interface = IvarDecl->getContainingInterface();
552e5dd7070Spatrick     // If the class is known to have a lifecycle with teardown that is
553e5dd7070Spatrick     // separate from -dealloc, do not warn about missing releases. We
554e5dd7070Spatrick     // suppress here (rather than not tracking for instance variables in
555e5dd7070Spatrick     // such classes) because these classes are rare.
556e5dd7070Spatrick     if (classHasSeparateTeardown(Interface))
557e5dd7070Spatrick       return;
558e5dd7070Spatrick 
559e5dd7070Spatrick     ObjCImplDecl *ImplDecl = Interface->getImplementation();
560e5dd7070Spatrick 
561e5dd7070Spatrick     const ObjCPropertyImplDecl *PropImpl =
562e5dd7070Spatrick         ImplDecl->FindPropertyImplIvarDecl(IvarDecl->getIdentifier());
563e5dd7070Spatrick 
564e5dd7070Spatrick     const ObjCPropertyDecl *PropDecl = PropImpl->getPropertyDecl();
565e5dd7070Spatrick 
566e5dd7070Spatrick     assert(PropDecl->getSetterKind() == ObjCPropertyDecl::Copy ||
567e5dd7070Spatrick            PropDecl->getSetterKind() == ObjCPropertyDecl::Retain);
568e5dd7070Spatrick 
569e5dd7070Spatrick     OS << "The '" << *IvarDecl << "' ivar in '" << *ImplDecl
570e5dd7070Spatrick        << "' was ";
571e5dd7070Spatrick 
572e5dd7070Spatrick     if (PropDecl->getSetterKind() == ObjCPropertyDecl::Retain)
573e5dd7070Spatrick       OS << "retained";
574e5dd7070Spatrick     else
575e5dd7070Spatrick       OS << "copied";
576e5dd7070Spatrick 
577e5dd7070Spatrick     OS << " by a synthesized property but not released"
578e5dd7070Spatrick           " before '[super dealloc]'";
579e5dd7070Spatrick 
580e5dd7070Spatrick     auto BR = std::make_unique<PathSensitiveBugReport>(*MissingReleaseBugType,
581e5dd7070Spatrick                                                        OS.str(), ErrNode);
582e5dd7070Spatrick     C.emitReport(std::move(BR));
583e5dd7070Spatrick   }
584e5dd7070Spatrick 
585e5dd7070Spatrick   if (NewUnreleased.isEmpty()) {
586e5dd7070Spatrick     State = State->remove<UnreleasedIvarMap>(SelfSym);
587e5dd7070Spatrick   } else {
588e5dd7070Spatrick     State = State->set<UnreleasedIvarMap>(SelfSym, NewUnreleased);
589e5dd7070Spatrick   }
590e5dd7070Spatrick 
591e5dd7070Spatrick   if (ErrNode) {
592e5dd7070Spatrick     C.addTransition(State, ErrNode);
593e5dd7070Spatrick   } else if (State != InitialState) {
594e5dd7070Spatrick     C.addTransition(State);
595e5dd7070Spatrick   }
596e5dd7070Spatrick 
597e5dd7070Spatrick   // Make sure that after checking in the top-most frame the list of
598e5dd7070Spatrick   // tracked ivars is empty. This is intended to detect accidental leaks in
599e5dd7070Spatrick   // the UnreleasedIvarMap program state.
600e5dd7070Spatrick   assert(!LCtx->inTopFrame() || State->get<UnreleasedIvarMap>().isEmpty());
601e5dd7070Spatrick }
602e5dd7070Spatrick 
603e5dd7070Spatrick /// Given a symbol, determine whether the symbol refers to an ivar on
604e5dd7070Spatrick /// the top-most deallocating instance. If so, find the property for that
605e5dd7070Spatrick /// ivar, if one exists. Otherwise return null.
606e5dd7070Spatrick const ObjCPropertyImplDecl *
findPropertyOnDeallocatingInstance(SymbolRef IvarSym,CheckerContext & C) const607e5dd7070Spatrick ObjCDeallocChecker::findPropertyOnDeallocatingInstance(
608e5dd7070Spatrick     SymbolRef IvarSym, CheckerContext &C) const {
609e5dd7070Spatrick   SVal DeallocedInstance;
610e5dd7070Spatrick   if (!isInInstanceDealloc(C, DeallocedInstance))
611e5dd7070Spatrick     return nullptr;
612e5dd7070Spatrick 
613e5dd7070Spatrick   // Try to get the region from which the ivar value was loaded.
614e5dd7070Spatrick   auto *IvarRegion = getIvarRegionForIvarSymbol(IvarSym);
615e5dd7070Spatrick   if (!IvarRegion)
616e5dd7070Spatrick     return nullptr;
617e5dd7070Spatrick 
618e5dd7070Spatrick   // Don't try to find the property if the ivar was not loaded from the
619e5dd7070Spatrick   // given instance.
620e5dd7070Spatrick   if (DeallocedInstance.castAs<loc::MemRegionVal>().getRegion() !=
621e5dd7070Spatrick       IvarRegion->getSuperRegion())
622e5dd7070Spatrick     return nullptr;
623e5dd7070Spatrick 
624e5dd7070Spatrick   const LocationContext *LCtx = C.getLocationContext();
625e5dd7070Spatrick   const ObjCIvarDecl *IvarDecl = IvarRegion->getDecl();
626e5dd7070Spatrick 
627e5dd7070Spatrick   const ObjCImplDecl *Container = getContainingObjCImpl(LCtx);
628e5dd7070Spatrick   const ObjCPropertyImplDecl *PropImpl =
629e5dd7070Spatrick       Container->FindPropertyImplIvarDecl(IvarDecl->getIdentifier());
630e5dd7070Spatrick   return PropImpl;
631e5dd7070Spatrick }
632e5dd7070Spatrick 
633e5dd7070Spatrick /// Emits a warning if the current context is -dealloc and ReleasedValue
634e5dd7070Spatrick /// must not be directly released in a -dealloc. Returns true if a diagnostic
635e5dd7070Spatrick /// was emitted.
diagnoseExtraRelease(SymbolRef ReleasedValue,const ObjCMethodCall & M,CheckerContext & C) const636e5dd7070Spatrick bool ObjCDeallocChecker::diagnoseExtraRelease(SymbolRef ReleasedValue,
637e5dd7070Spatrick                                               const ObjCMethodCall &M,
638e5dd7070Spatrick                                               CheckerContext &C) const {
639e5dd7070Spatrick   // Try to get the region from which the released value was loaded.
640e5dd7070Spatrick   // Note that, unlike diagnosing for missing releases, here we don't track
641e5dd7070Spatrick   // values that must not be released in the state. This is because even if
642e5dd7070Spatrick   // these values escape, it is still an error under the rules of MRR to
643e5dd7070Spatrick   // release them in -dealloc.
644e5dd7070Spatrick   const ObjCPropertyImplDecl *PropImpl =
645e5dd7070Spatrick       findPropertyOnDeallocatingInstance(ReleasedValue, C);
646e5dd7070Spatrick 
647e5dd7070Spatrick   if (!PropImpl)
648e5dd7070Spatrick     return false;
649e5dd7070Spatrick 
650e5dd7070Spatrick   // If the ivar belongs to a property that must not be released directly
651e5dd7070Spatrick   // in dealloc, emit a warning.
652e5dd7070Spatrick   if (getDeallocReleaseRequirement(PropImpl) !=
653e5dd7070Spatrick       ReleaseRequirement::MustNotReleaseDirectly) {
654e5dd7070Spatrick     return false;
655e5dd7070Spatrick   }
656e5dd7070Spatrick 
657e5dd7070Spatrick   // If the property is readwrite but it shadows a read-only property in its
658e5dd7070Spatrick   // external interface, treat the property a read-only. If the outside
659e5dd7070Spatrick   // world cannot write to a property then the internal implementation is free
660e5dd7070Spatrick   // to make its own convention about whether the value is stored retained
661e5dd7070Spatrick   // or not. We look up the shadow here rather than in
662e5dd7070Spatrick   // getDeallocReleaseRequirement() because doing so can be expensive.
663e5dd7070Spatrick   const ObjCPropertyDecl *PropDecl = findShadowedPropertyDecl(PropImpl);
664e5dd7070Spatrick   if (PropDecl) {
665e5dd7070Spatrick     if (PropDecl->isReadOnly())
666e5dd7070Spatrick       return false;
667e5dd7070Spatrick   } else {
668e5dd7070Spatrick     PropDecl = PropImpl->getPropertyDecl();
669e5dd7070Spatrick   }
670e5dd7070Spatrick 
671e5dd7070Spatrick   ExplodedNode *ErrNode = C.generateNonFatalErrorNode();
672e5dd7070Spatrick   if (!ErrNode)
673e5dd7070Spatrick     return false;
674e5dd7070Spatrick 
675e5dd7070Spatrick   std::string Buf;
676e5dd7070Spatrick   llvm::raw_string_ostream OS(Buf);
677e5dd7070Spatrick 
678e5dd7070Spatrick   assert(PropDecl->getSetterKind() == ObjCPropertyDecl::Weak ||
679e5dd7070Spatrick          (PropDecl->getSetterKind() == ObjCPropertyDecl::Assign &&
680e5dd7070Spatrick           !PropDecl->isReadOnly()) ||
681e5dd7070Spatrick          isReleasedByCIFilterDealloc(PropImpl)
682e5dd7070Spatrick          );
683e5dd7070Spatrick 
684e5dd7070Spatrick   const ObjCImplDecl *Container = getContainingObjCImpl(C.getLocationContext());
685e5dd7070Spatrick   OS << "The '" << *PropImpl->getPropertyIvarDecl()
686e5dd7070Spatrick      << "' ivar in '" << *Container;
687e5dd7070Spatrick 
688e5dd7070Spatrick 
689e5dd7070Spatrick   if (isReleasedByCIFilterDealloc(PropImpl)) {
690e5dd7070Spatrick     OS << "' will be released by '-[CIFilter dealloc]' but also released here";
691e5dd7070Spatrick   } else {
692e5dd7070Spatrick     OS << "' was synthesized for ";
693e5dd7070Spatrick 
694e5dd7070Spatrick     if (PropDecl->getSetterKind() == ObjCPropertyDecl::Weak)
695e5dd7070Spatrick       OS << "a weak";
696e5dd7070Spatrick     else
697e5dd7070Spatrick       OS << "an assign, readwrite";
698e5dd7070Spatrick 
699e5dd7070Spatrick     OS <<  " property but was released in 'dealloc'";
700e5dd7070Spatrick   }
701e5dd7070Spatrick 
702e5dd7070Spatrick   auto BR = std::make_unique<PathSensitiveBugReport>(*ExtraReleaseBugType,
703e5dd7070Spatrick                                                      OS.str(), ErrNode);
704e5dd7070Spatrick   BR->addRange(M.getOriginExpr()->getSourceRange());
705e5dd7070Spatrick 
706e5dd7070Spatrick   C.emitReport(std::move(BR));
707e5dd7070Spatrick 
708e5dd7070Spatrick   return true;
709e5dd7070Spatrick }
710e5dd7070Spatrick 
711e5dd7070Spatrick /// Emits a warning if the current context is -dealloc and DeallocedValue
712e5dd7070Spatrick /// must not be directly dealloced in a -dealloc. Returns true if a diagnostic
713e5dd7070Spatrick /// was emitted.
diagnoseMistakenDealloc(SymbolRef DeallocedValue,const ObjCMethodCall & M,CheckerContext & C) const714e5dd7070Spatrick bool ObjCDeallocChecker::diagnoseMistakenDealloc(SymbolRef DeallocedValue,
715e5dd7070Spatrick                                                  const ObjCMethodCall &M,
716e5dd7070Spatrick                                                  CheckerContext &C) const {
717e5dd7070Spatrick   // TODO: Apart from unknown/undefined receivers, this may happen when
718e5dd7070Spatrick   // dealloc is called as a class method. Should we warn?
719e5dd7070Spatrick   if (!DeallocedValue)
720e5dd7070Spatrick     return false;
721e5dd7070Spatrick 
722e5dd7070Spatrick   // Find the property backing the instance variable that M
723e5dd7070Spatrick   // is dealloc'ing.
724e5dd7070Spatrick   const ObjCPropertyImplDecl *PropImpl =
725e5dd7070Spatrick       findPropertyOnDeallocatingInstance(DeallocedValue, C);
726e5dd7070Spatrick   if (!PropImpl)
727e5dd7070Spatrick     return false;
728e5dd7070Spatrick 
729e5dd7070Spatrick   if (getDeallocReleaseRequirement(PropImpl) !=
730e5dd7070Spatrick       ReleaseRequirement::MustRelease) {
731e5dd7070Spatrick     return false;
732e5dd7070Spatrick   }
733e5dd7070Spatrick 
734e5dd7070Spatrick   ExplodedNode *ErrNode = C.generateErrorNode();
735e5dd7070Spatrick   if (!ErrNode)
736e5dd7070Spatrick     return false;
737e5dd7070Spatrick 
738e5dd7070Spatrick   std::string Buf;
739e5dd7070Spatrick   llvm::raw_string_ostream OS(Buf);
740e5dd7070Spatrick 
741e5dd7070Spatrick   OS << "'" << *PropImpl->getPropertyIvarDecl()
742e5dd7070Spatrick      << "' should be released rather than deallocated";
743e5dd7070Spatrick 
744e5dd7070Spatrick   auto BR = std::make_unique<PathSensitiveBugReport>(*MistakenDeallocBugType,
745e5dd7070Spatrick                                                      OS.str(), ErrNode);
746e5dd7070Spatrick   BR->addRange(M.getOriginExpr()->getSourceRange());
747e5dd7070Spatrick 
748e5dd7070Spatrick   C.emitReport(std::move(BR));
749e5dd7070Spatrick 
750e5dd7070Spatrick   return true;
751e5dd7070Spatrick }
752e5dd7070Spatrick 
ObjCDeallocChecker()753e5dd7070Spatrick ObjCDeallocChecker::ObjCDeallocChecker()
754e5dd7070Spatrick     : NSObjectII(nullptr), SenTestCaseII(nullptr), XCTestCaseII(nullptr),
755e5dd7070Spatrick       CIFilterII(nullptr) {
756e5dd7070Spatrick 
757e5dd7070Spatrick   MissingReleaseBugType.reset(
758e5dd7070Spatrick       new BugType(this, "Missing ivar release (leak)",
759e5dd7070Spatrick                   categories::MemoryRefCount));
760e5dd7070Spatrick 
761e5dd7070Spatrick   ExtraReleaseBugType.reset(
762e5dd7070Spatrick       new BugType(this, "Extra ivar release",
763e5dd7070Spatrick                   categories::MemoryRefCount));
764e5dd7070Spatrick 
765e5dd7070Spatrick   MistakenDeallocBugType.reset(
766e5dd7070Spatrick       new BugType(this, "Mistaken dealloc",
767e5dd7070Spatrick                   categories::MemoryRefCount));
768e5dd7070Spatrick }
769e5dd7070Spatrick 
initIdentifierInfoAndSelectors(ASTContext & Ctx) const770e5dd7070Spatrick void ObjCDeallocChecker::initIdentifierInfoAndSelectors(
771e5dd7070Spatrick     ASTContext &Ctx) const {
772e5dd7070Spatrick   if (NSObjectII)
773e5dd7070Spatrick     return;
774e5dd7070Spatrick 
775e5dd7070Spatrick   NSObjectII = &Ctx.Idents.get("NSObject");
776e5dd7070Spatrick   SenTestCaseII = &Ctx.Idents.get("SenTestCase");
777e5dd7070Spatrick   XCTestCaseII = &Ctx.Idents.get("XCTestCase");
778e5dd7070Spatrick   Block_releaseII = &Ctx.Idents.get("_Block_release");
779e5dd7070Spatrick   CIFilterII = &Ctx.Idents.get("CIFilter");
780e5dd7070Spatrick 
781e5dd7070Spatrick   IdentifierInfo *DeallocII = &Ctx.Idents.get("dealloc");
782e5dd7070Spatrick   IdentifierInfo *ReleaseII = &Ctx.Idents.get("release");
783e5dd7070Spatrick   DeallocSel = Ctx.Selectors.getSelector(0, &DeallocII);
784e5dd7070Spatrick   ReleaseSel = Ctx.Selectors.getSelector(0, &ReleaseII);
785e5dd7070Spatrick }
786e5dd7070Spatrick 
787e5dd7070Spatrick /// Returns true if M is a call to '[super dealloc]'.
isSuperDeallocMessage(const ObjCMethodCall & M) const788e5dd7070Spatrick bool ObjCDeallocChecker::isSuperDeallocMessage(
789e5dd7070Spatrick     const ObjCMethodCall &M) const {
790e5dd7070Spatrick   if (M.getOriginExpr()->getReceiverKind() != ObjCMessageExpr::SuperInstance)
791e5dd7070Spatrick     return false;
792e5dd7070Spatrick 
793e5dd7070Spatrick   return M.getSelector() == DeallocSel;
794e5dd7070Spatrick }
795e5dd7070Spatrick 
796e5dd7070Spatrick /// Returns the ObjCImplDecl containing the method declaration in LCtx.
797e5dd7070Spatrick const ObjCImplDecl *
getContainingObjCImpl(const LocationContext * LCtx) const798e5dd7070Spatrick ObjCDeallocChecker::getContainingObjCImpl(const LocationContext *LCtx) const {
799e5dd7070Spatrick   auto *MD = cast<ObjCMethodDecl>(LCtx->getDecl());
800e5dd7070Spatrick   return cast<ObjCImplDecl>(MD->getDeclContext());
801e5dd7070Spatrick }
802e5dd7070Spatrick 
803e5dd7070Spatrick /// Returns the property that shadowed by PropImpl if one exists and
804e5dd7070Spatrick /// nullptr otherwise.
findShadowedPropertyDecl(const ObjCPropertyImplDecl * PropImpl) const805e5dd7070Spatrick const ObjCPropertyDecl *ObjCDeallocChecker::findShadowedPropertyDecl(
806e5dd7070Spatrick     const ObjCPropertyImplDecl *PropImpl) const {
807e5dd7070Spatrick   const ObjCPropertyDecl *PropDecl = PropImpl->getPropertyDecl();
808e5dd7070Spatrick 
809e5dd7070Spatrick   // Only readwrite properties can shadow.
810e5dd7070Spatrick   if (PropDecl->isReadOnly())
811e5dd7070Spatrick     return nullptr;
812e5dd7070Spatrick 
813e5dd7070Spatrick   auto *CatDecl = dyn_cast<ObjCCategoryDecl>(PropDecl->getDeclContext());
814e5dd7070Spatrick 
815e5dd7070Spatrick   // Only class extensions can contain shadowing properties.
816e5dd7070Spatrick   if (!CatDecl || !CatDecl->IsClassExtension())
817e5dd7070Spatrick     return nullptr;
818e5dd7070Spatrick 
819e5dd7070Spatrick   IdentifierInfo *ID = PropDecl->getIdentifier();
820e5dd7070Spatrick   DeclContext::lookup_result R = CatDecl->getClassInterface()->lookup(ID);
821e5dd7070Spatrick   for (DeclContext::lookup_iterator I = R.begin(), E = R.end(); I != E; ++I) {
822e5dd7070Spatrick     auto *ShadowedPropDecl = dyn_cast<ObjCPropertyDecl>(*I);
823e5dd7070Spatrick     if (!ShadowedPropDecl)
824e5dd7070Spatrick       continue;
825e5dd7070Spatrick 
826e5dd7070Spatrick     if (ShadowedPropDecl->isInstanceProperty()) {
827e5dd7070Spatrick       assert(ShadowedPropDecl->isReadOnly());
828e5dd7070Spatrick       return ShadowedPropDecl;
829e5dd7070Spatrick     }
830e5dd7070Spatrick   }
831e5dd7070Spatrick 
832e5dd7070Spatrick   return nullptr;
833e5dd7070Spatrick }
834e5dd7070Spatrick 
835e5dd7070Spatrick /// Add a transition noting the release of the given value.
transitionToReleaseValue(CheckerContext & C,SymbolRef Value) const836e5dd7070Spatrick void ObjCDeallocChecker::transitionToReleaseValue(CheckerContext &C,
837e5dd7070Spatrick                                                   SymbolRef Value) const {
838e5dd7070Spatrick   assert(Value);
839e5dd7070Spatrick   SymbolRef InstanceSym = getInstanceSymbolFromIvarSymbol(Value);
840e5dd7070Spatrick   if (!InstanceSym)
841e5dd7070Spatrick     return;
842e5dd7070Spatrick   ProgramStateRef InitialState = C.getState();
843e5dd7070Spatrick 
844e5dd7070Spatrick   ProgramStateRef ReleasedState =
845e5dd7070Spatrick       removeValueRequiringRelease(InitialState, InstanceSym, Value);
846e5dd7070Spatrick 
847e5dd7070Spatrick   if (ReleasedState != InitialState) {
848e5dd7070Spatrick     C.addTransition(ReleasedState);
849e5dd7070Spatrick   }
850e5dd7070Spatrick }
851e5dd7070Spatrick 
852e5dd7070Spatrick /// Remove the Value requiring a release from the tracked set for
853e5dd7070Spatrick /// Instance and return the resultant state.
removeValueRequiringRelease(ProgramStateRef State,SymbolRef Instance,SymbolRef Value) const854e5dd7070Spatrick ProgramStateRef ObjCDeallocChecker::removeValueRequiringRelease(
855e5dd7070Spatrick     ProgramStateRef State, SymbolRef Instance, SymbolRef Value) const {
856e5dd7070Spatrick   assert(Instance);
857e5dd7070Spatrick   assert(Value);
858e5dd7070Spatrick   const ObjCIvarRegion *RemovedRegion = getIvarRegionForIvarSymbol(Value);
859e5dd7070Spatrick   if (!RemovedRegion)
860e5dd7070Spatrick     return State;
861e5dd7070Spatrick 
862e5dd7070Spatrick   const SymbolSet *Unreleased = State->get<UnreleasedIvarMap>(Instance);
863e5dd7070Spatrick   if (!Unreleased)
864e5dd7070Spatrick     return State;
865e5dd7070Spatrick 
866e5dd7070Spatrick   // Mark the value as no longer requiring a release.
867e5dd7070Spatrick   SymbolSet::Factory &F = State->getStateManager().get_context<SymbolSet>();
868e5dd7070Spatrick   SymbolSet NewUnreleased = *Unreleased;
869e5dd7070Spatrick   for (auto &Sym : *Unreleased) {
870e5dd7070Spatrick     const ObjCIvarRegion *UnreleasedRegion = getIvarRegionForIvarSymbol(Sym);
871e5dd7070Spatrick     assert(UnreleasedRegion);
872e5dd7070Spatrick     if (RemovedRegion->getDecl() == UnreleasedRegion->getDecl()) {
873e5dd7070Spatrick       NewUnreleased = F.remove(NewUnreleased, Sym);
874e5dd7070Spatrick     }
875e5dd7070Spatrick   }
876e5dd7070Spatrick 
877e5dd7070Spatrick   if (NewUnreleased.isEmpty()) {
878e5dd7070Spatrick     return State->remove<UnreleasedIvarMap>(Instance);
879e5dd7070Spatrick   }
880e5dd7070Spatrick 
881e5dd7070Spatrick   return State->set<UnreleasedIvarMap>(Instance, NewUnreleased);
882e5dd7070Spatrick }
883e5dd7070Spatrick 
884e5dd7070Spatrick /// Determines whether the instance variable for \p PropImpl must or must not be
885e5dd7070Spatrick /// released in -dealloc or whether it cannot be determined.
getDeallocReleaseRequirement(const ObjCPropertyImplDecl * PropImpl) const886e5dd7070Spatrick ReleaseRequirement ObjCDeallocChecker::getDeallocReleaseRequirement(
887e5dd7070Spatrick     const ObjCPropertyImplDecl *PropImpl) const {
888e5dd7070Spatrick   const ObjCIvarDecl *IvarDecl;
889e5dd7070Spatrick   const ObjCPropertyDecl *PropDecl;
890e5dd7070Spatrick   if (!isSynthesizedRetainableProperty(PropImpl, &IvarDecl, &PropDecl))
891e5dd7070Spatrick     return ReleaseRequirement::Unknown;
892e5dd7070Spatrick 
893e5dd7070Spatrick   ObjCPropertyDecl::SetterKind SK = PropDecl->getSetterKind();
894e5dd7070Spatrick 
895e5dd7070Spatrick   switch (SK) {
896e5dd7070Spatrick   // Retain and copy setters retain/copy their values before storing and so
897e5dd7070Spatrick   // the value in their instance variables must be released in -dealloc.
898e5dd7070Spatrick   case ObjCPropertyDecl::Retain:
899e5dd7070Spatrick   case ObjCPropertyDecl::Copy:
900e5dd7070Spatrick     if (isReleasedByCIFilterDealloc(PropImpl))
901e5dd7070Spatrick       return ReleaseRequirement::MustNotReleaseDirectly;
902e5dd7070Spatrick 
903e5dd7070Spatrick     if (isNibLoadedIvarWithoutRetain(PropImpl))
904e5dd7070Spatrick       return ReleaseRequirement::Unknown;
905e5dd7070Spatrick 
906e5dd7070Spatrick     return ReleaseRequirement::MustRelease;
907e5dd7070Spatrick 
908e5dd7070Spatrick   case ObjCPropertyDecl::Weak:
909e5dd7070Spatrick     return ReleaseRequirement::MustNotReleaseDirectly;
910e5dd7070Spatrick 
911e5dd7070Spatrick   case ObjCPropertyDecl::Assign:
912e5dd7070Spatrick     // It is common for the ivars for read-only assign properties to
913e5dd7070Spatrick     // always be stored retained, so their release requirement cannot be
914e5dd7070Spatrick     // be determined.
915e5dd7070Spatrick     if (PropDecl->isReadOnly())
916e5dd7070Spatrick       return ReleaseRequirement::Unknown;
917e5dd7070Spatrick 
918e5dd7070Spatrick     return ReleaseRequirement::MustNotReleaseDirectly;
919e5dd7070Spatrick   }
920e5dd7070Spatrick   llvm_unreachable("Unrecognized setter kind");
921e5dd7070Spatrick }
922e5dd7070Spatrick 
923e5dd7070Spatrick /// Returns the released value if M is a call a setter that releases
924e5dd7070Spatrick /// and nils out its underlying instance variable.
925e5dd7070Spatrick SymbolRef
getValueReleasedByNillingOut(const ObjCMethodCall & M,CheckerContext & C) const926e5dd7070Spatrick ObjCDeallocChecker::getValueReleasedByNillingOut(const ObjCMethodCall &M,
927e5dd7070Spatrick                                                  CheckerContext &C) const {
928e5dd7070Spatrick   SVal ReceiverVal = M.getReceiverSVal();
929e5dd7070Spatrick   if (!ReceiverVal.isValid())
930e5dd7070Spatrick     return nullptr;
931e5dd7070Spatrick 
932e5dd7070Spatrick   if (M.getNumArgs() == 0)
933e5dd7070Spatrick     return nullptr;
934e5dd7070Spatrick 
935e5dd7070Spatrick   if (!M.getArgExpr(0)->getType()->isObjCRetainableType())
936e5dd7070Spatrick     return nullptr;
937e5dd7070Spatrick 
938e5dd7070Spatrick   // Is the first argument nil?
939e5dd7070Spatrick   SVal Arg = M.getArgSVal(0);
940e5dd7070Spatrick   ProgramStateRef notNilState, nilState;
941e5dd7070Spatrick   std::tie(notNilState, nilState) =
942e5dd7070Spatrick       M.getState()->assume(Arg.castAs<DefinedOrUnknownSVal>());
943e5dd7070Spatrick   if (!(nilState && !notNilState))
944e5dd7070Spatrick     return nullptr;
945e5dd7070Spatrick 
946e5dd7070Spatrick   const ObjCPropertyDecl *Prop = M.getAccessedProperty();
947e5dd7070Spatrick   if (!Prop)
948e5dd7070Spatrick     return nullptr;
949e5dd7070Spatrick 
950e5dd7070Spatrick   ObjCIvarDecl *PropIvarDecl = Prop->getPropertyIvarDecl();
951e5dd7070Spatrick   if (!PropIvarDecl)
952e5dd7070Spatrick     return nullptr;
953e5dd7070Spatrick 
954e5dd7070Spatrick   ProgramStateRef State = C.getState();
955e5dd7070Spatrick 
956e5dd7070Spatrick   SVal LVal = State->getLValue(PropIvarDecl, ReceiverVal);
957*12c85518Srobert   std::optional<Loc> LValLoc = LVal.getAs<Loc>();
958e5dd7070Spatrick   if (!LValLoc)
959e5dd7070Spatrick     return nullptr;
960e5dd7070Spatrick 
961*12c85518Srobert   SVal CurrentValInIvar = State->getSVal(*LValLoc);
962e5dd7070Spatrick   return CurrentValInIvar.getAsSymbol();
963e5dd7070Spatrick }
964e5dd7070Spatrick 
965e5dd7070Spatrick /// Returns true if the current context is a call to -dealloc and false
966e5dd7070Spatrick /// otherwise. If true, it also sets SelfValOut to the value of
967e5dd7070Spatrick /// 'self'.
isInInstanceDealloc(const CheckerContext & C,SVal & SelfValOut) const968e5dd7070Spatrick bool ObjCDeallocChecker::isInInstanceDealloc(const CheckerContext &C,
969e5dd7070Spatrick                                              SVal &SelfValOut) const {
970e5dd7070Spatrick   return isInInstanceDealloc(C, C.getLocationContext(), SelfValOut);
971e5dd7070Spatrick }
972e5dd7070Spatrick 
973e5dd7070Spatrick /// Returns true if LCtx is a call to -dealloc and false
974e5dd7070Spatrick /// otherwise. If true, it also sets SelfValOut to the value of
975e5dd7070Spatrick /// 'self'.
isInInstanceDealloc(const CheckerContext & C,const LocationContext * LCtx,SVal & SelfValOut) const976e5dd7070Spatrick bool ObjCDeallocChecker::isInInstanceDealloc(const CheckerContext &C,
977e5dd7070Spatrick                                              const LocationContext *LCtx,
978e5dd7070Spatrick                                              SVal &SelfValOut) const {
979e5dd7070Spatrick   auto *MD = dyn_cast<ObjCMethodDecl>(LCtx->getDecl());
980e5dd7070Spatrick   if (!MD || !MD->isInstanceMethod() || MD->getSelector() != DeallocSel)
981e5dd7070Spatrick     return false;
982e5dd7070Spatrick 
983e5dd7070Spatrick   const ImplicitParamDecl *SelfDecl = LCtx->getSelfDecl();
984e5dd7070Spatrick   assert(SelfDecl && "No self in -dealloc?");
985e5dd7070Spatrick 
986e5dd7070Spatrick   ProgramStateRef State = C.getState();
987e5dd7070Spatrick   SelfValOut = State->getSVal(State->getRegion(SelfDecl, LCtx));
988e5dd7070Spatrick   return true;
989e5dd7070Spatrick }
990e5dd7070Spatrick 
991e5dd7070Spatrick /// Returns true if there is a call to -dealloc anywhere on the stack and false
992e5dd7070Spatrick /// otherwise. If true, it also sets InstanceValOut to the value of
993e5dd7070Spatrick /// 'self' in the frame for -dealloc.
instanceDeallocIsOnStack(const CheckerContext & C,SVal & InstanceValOut) const994e5dd7070Spatrick bool ObjCDeallocChecker::instanceDeallocIsOnStack(const CheckerContext &C,
995e5dd7070Spatrick                                                   SVal &InstanceValOut) const {
996e5dd7070Spatrick   const LocationContext *LCtx = C.getLocationContext();
997e5dd7070Spatrick 
998e5dd7070Spatrick   while (LCtx) {
999e5dd7070Spatrick     if (isInInstanceDealloc(C, LCtx, InstanceValOut))
1000e5dd7070Spatrick       return true;
1001e5dd7070Spatrick 
1002e5dd7070Spatrick     LCtx = LCtx->getParent();
1003e5dd7070Spatrick   }
1004e5dd7070Spatrick 
1005e5dd7070Spatrick   return false;
1006e5dd7070Spatrick }
1007e5dd7070Spatrick 
1008*12c85518Srobert /// Returns true if the ID is a class in which is known to have
1009e5dd7070Spatrick /// a separate teardown lifecycle. In this case, -dealloc warnings
1010e5dd7070Spatrick /// about missing releases should be suppressed.
classHasSeparateTeardown(const ObjCInterfaceDecl * ID) const1011e5dd7070Spatrick bool ObjCDeallocChecker::classHasSeparateTeardown(
1012e5dd7070Spatrick     const ObjCInterfaceDecl *ID) const {
1013e5dd7070Spatrick   // Suppress if the class is not a subclass of NSObject.
1014e5dd7070Spatrick   for ( ; ID ; ID = ID->getSuperClass()) {
1015e5dd7070Spatrick     IdentifierInfo *II = ID->getIdentifier();
1016e5dd7070Spatrick 
1017e5dd7070Spatrick     if (II == NSObjectII)
1018e5dd7070Spatrick       return false;
1019e5dd7070Spatrick 
1020e5dd7070Spatrick     // FIXME: For now, ignore classes that subclass SenTestCase and XCTestCase,
1021e5dd7070Spatrick     // as these don't need to implement -dealloc.  They implement tear down in
1022e5dd7070Spatrick     // another way, which we should try and catch later.
1023e5dd7070Spatrick     //  http://llvm.org/bugs/show_bug.cgi?id=3187
1024e5dd7070Spatrick     if (II == XCTestCaseII || II == SenTestCaseII)
1025e5dd7070Spatrick       return true;
1026e5dd7070Spatrick   }
1027e5dd7070Spatrick 
1028e5dd7070Spatrick   return true;
1029e5dd7070Spatrick }
1030e5dd7070Spatrick 
1031e5dd7070Spatrick /// The -dealloc method in CIFilter highly unusual in that is will release
1032e5dd7070Spatrick /// instance variables belonging to its *subclasses* if the variable name
1033e5dd7070Spatrick /// starts with "input" or backs a property whose name starts with "input".
1034e5dd7070Spatrick /// Subclasses should not release these ivars in their own -dealloc method --
1035e5dd7070Spatrick /// doing so could result in an over release.
1036e5dd7070Spatrick ///
1037e5dd7070Spatrick /// This method returns true if the property will be released by
1038e5dd7070Spatrick /// -[CIFilter dealloc].
isReleasedByCIFilterDealloc(const ObjCPropertyImplDecl * PropImpl) const1039e5dd7070Spatrick bool ObjCDeallocChecker::isReleasedByCIFilterDealloc(
1040e5dd7070Spatrick     const ObjCPropertyImplDecl *PropImpl) const {
1041e5dd7070Spatrick   assert(PropImpl->getPropertyIvarDecl());
1042e5dd7070Spatrick   StringRef PropName = PropImpl->getPropertyDecl()->getName();
1043e5dd7070Spatrick   StringRef IvarName = PropImpl->getPropertyIvarDecl()->getName();
1044e5dd7070Spatrick 
1045e5dd7070Spatrick   const char *ReleasePrefix = "input";
1046e5dd7070Spatrick   if (!(PropName.startswith(ReleasePrefix) ||
1047e5dd7070Spatrick         IvarName.startswith(ReleasePrefix))) {
1048e5dd7070Spatrick     return false;
1049e5dd7070Spatrick   }
1050e5dd7070Spatrick 
1051e5dd7070Spatrick   const ObjCInterfaceDecl *ID =
1052e5dd7070Spatrick       PropImpl->getPropertyIvarDecl()->getContainingInterface();
1053e5dd7070Spatrick   for ( ; ID ; ID = ID->getSuperClass()) {
1054e5dd7070Spatrick     IdentifierInfo *II = ID->getIdentifier();
1055e5dd7070Spatrick     if (II == CIFilterII)
1056e5dd7070Spatrick       return true;
1057e5dd7070Spatrick   }
1058e5dd7070Spatrick 
1059e5dd7070Spatrick   return false;
1060e5dd7070Spatrick }
1061e5dd7070Spatrick 
1062e5dd7070Spatrick /// Returns whether the ivar backing the property is an IBOutlet that
1063e5dd7070Spatrick /// has its value set by nib loading code without retaining the value.
1064e5dd7070Spatrick ///
1065e5dd7070Spatrick /// On macOS, if there is no setter, the nib-loading code sets the ivar
1066e5dd7070Spatrick /// directly, without retaining the value,
1067e5dd7070Spatrick ///
1068e5dd7070Spatrick /// On iOS and its derivatives, the nib-loading code will call
1069e5dd7070Spatrick /// -setValue:forKey:, which retains the value before directly setting the ivar.
isNibLoadedIvarWithoutRetain(const ObjCPropertyImplDecl * PropImpl) const1070e5dd7070Spatrick bool ObjCDeallocChecker::isNibLoadedIvarWithoutRetain(
1071e5dd7070Spatrick     const ObjCPropertyImplDecl *PropImpl) const {
1072e5dd7070Spatrick   const ObjCIvarDecl *IvarDecl = PropImpl->getPropertyIvarDecl();
1073e5dd7070Spatrick   if (!IvarDecl->hasAttr<IBOutletAttr>())
1074e5dd7070Spatrick     return false;
1075e5dd7070Spatrick 
1076e5dd7070Spatrick   const llvm::Triple &Target =
1077e5dd7070Spatrick       IvarDecl->getASTContext().getTargetInfo().getTriple();
1078e5dd7070Spatrick 
1079e5dd7070Spatrick   if (!Target.isMacOSX())
1080e5dd7070Spatrick     return false;
1081e5dd7070Spatrick 
1082e5dd7070Spatrick   if (PropImpl->getPropertyDecl()->getSetterMethodDecl())
1083e5dd7070Spatrick     return false;
1084e5dd7070Spatrick 
1085e5dd7070Spatrick   return true;
1086e5dd7070Spatrick }
1087e5dd7070Spatrick 
registerObjCDeallocChecker(CheckerManager & Mgr)1088e5dd7070Spatrick void ento::registerObjCDeallocChecker(CheckerManager &Mgr) {
1089e5dd7070Spatrick   Mgr.registerChecker<ObjCDeallocChecker>();
1090e5dd7070Spatrick }
1091e5dd7070Spatrick 
shouldRegisterObjCDeallocChecker(const CheckerManager & mgr)1092ec727ea7Spatrick bool ento::shouldRegisterObjCDeallocChecker(const CheckerManager &mgr) {
1093e5dd7070Spatrick   // These checker only makes sense under MRR.
1094ec727ea7Spatrick   const LangOptions &LO = mgr.getLangOpts();
1095e5dd7070Spatrick   return LO.getGC() != LangOptions::GCOnly && !LO.ObjCAutoRefCount;
1096e5dd7070Spatrick }
1097