blob: 902babfe502caed67569165d1516b3335ff4775c [file] [log] [blame]
Ted Kremenek0e7d2522008-07-03 04:29:21 +00001//==- CheckObjCDealloc.cpp - Check ObjC -dealloc implementation --*- C++ -*-==//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
Devin Coughlinea02bba2016-02-25 19:13:43 +000010// This file defines a CheckObjCDealloc, a checker that
11// analyzes an Objective-C class's implementation to determine if it
12// correctly implements -dealloc.
Ted Kremenek0e7d2522008-07-03 04:29:21 +000013//
14//===----------------------------------------------------------------------===//
15
Argyrios Kyrtzidisaf45aca2011-02-17 21:39:33 +000016#include "ClangSACheckers.h"
Benjamin Kramerea70eb32012-12-01 15:09:41 +000017#include "clang/AST/Attr.h"
Ted Kremenek0e7d2522008-07-03 04:29:21 +000018#include "clang/AST/DeclObjC.h"
Benjamin Kramerea70eb32012-12-01 15:09:41 +000019#include "clang/AST/Expr.h"
20#include "clang/AST/ExprObjC.h"
Ted Kremeneke66ca6f2008-07-03 14:35:01 +000021#include "clang/Basic/LangOptions.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000022#include "clang/StaticAnalyzer/Core/BugReporter/BugReporter.h"
23#include "clang/StaticAnalyzer/Core/BugReporter/PathDiagnostic.h"
24#include "clang/StaticAnalyzer/Core/Checker.h"
25#include "clang/StaticAnalyzer/Core/PathSensitive/AnalysisManager.h"
Ted Kremenek3f049492008-10-29 04:30:28 +000026#include "llvm/Support/raw_ostream.h"
Ted Kremenek0e7d2522008-07-03 04:29:21 +000027
28using namespace clang;
Ted Kremenek98857c92010-12-23 07:20:52 +000029using namespace ento;
Ted Kremenek0e7d2522008-07-03 04:29:21 +000030
Devin Coughlinea02bba2016-02-25 19:13:43 +000031// FIXME: This was taken from IvarInvalidationChecker.cpp
32static const Expr *peel(const Expr *E) {
33 E = E->IgnoreParenCasts();
34 if (const PseudoObjectExpr *POE = dyn_cast<PseudoObjectExpr>(E))
35 E = POE->getSyntacticForm()->IgnoreParenCasts();
36 if (const OpaqueValueExpr *OVE = dyn_cast<OpaqueValueExpr>(E))
37 E = OVE->getSourceExpr()->IgnoreParenCasts();
38 return E;
39}
Devin Coughlin982c42d2016-02-11 22:13:20 +000040
Devin Coughlinea02bba2016-02-25 19:13:43 +000041static bool scan_ivar_release(Stmt *S, const ObjCIvarDecl *ID,
42 const ObjCPropertyDecl *PD,
43 Selector Release,
44 IdentifierInfo* SelfII,
45 ASTContext &Ctx) {
Mike Stump11289f42009-09-09 15:08:12 +000046
Devin Coughlinea02bba2016-02-25 19:13:43 +000047 // [mMyIvar release]
48 if (ObjCMessageExpr *ME = dyn_cast<ObjCMessageExpr>(S))
49 if (ME->getSelector() == Release)
50 if (ME->getInstanceReceiver())
51 if (const Expr *Receiver = peel(ME->getInstanceReceiver()))
52 if (auto *E = dyn_cast<ObjCIvarRefExpr>(Receiver))
53 if (E->getDecl() == ID)
54 return true;
Ted Kremenek3f049492008-10-29 04:30:28 +000055
Devin Coughlinea02bba2016-02-25 19:13:43 +000056 // [self setMyIvar:nil];
57 if (ObjCMessageExpr *ME = dyn_cast<ObjCMessageExpr>(S))
58 if (ME->getInstanceReceiver())
59 if (const Expr *Receiver = peel(ME->getInstanceReceiver()))
60 if (auto *E = dyn_cast<DeclRefExpr>(Receiver))
61 if (E->getDecl()->getIdentifier() == SelfII)
62 if (ME->getMethodDecl() == PD->getSetterMethodDecl() &&
63 ME->getNumArgs() == 1 &&
64 peel(ME->getArg(0))->isNullPointerConstant(Ctx,
65 Expr::NPC_ValueDependentIsNull))
66 return true;
67
68 // self.myIvar = nil;
69 if (BinaryOperator* BO = dyn_cast<BinaryOperator>(S))
70 if (BO->isAssignmentOp())
71 if (auto *PRE = dyn_cast<ObjCPropertyRefExpr>(peel(BO->getLHS())))
72 if (PRE->isExplicitProperty() && PRE->getExplicitProperty() == PD)
73 if (peel(BO->getRHS())->isNullPointerConstant(Ctx,
74 Expr::NPC_ValueDependentIsNull)) {
75 // This is only a 'release' if the property kind is not
76 // 'assign'.
77 return PD->getSetterKind() != ObjCPropertyDecl::Assign;
78 }
79
80 // Recurse to children.
81 for (Stmt *SubStmt : S->children())
82 if (SubStmt && scan_ivar_release(SubStmt, ID, PD, Release, SelfII, Ctx))
83 return true;
84
85 return false;
86}
87
Devin Coughlin30751342016-01-27 01:41:58 +000088static bool isSynthesizedRetainableProperty(const ObjCPropertyImplDecl *I,
89 const ObjCIvarDecl **ID,
90 const ObjCPropertyDecl **PD) {
91
92 if (I->getPropertyImplementation() != ObjCPropertyImplDecl::Synthesize)
93 return false;
94
95 (*ID) = I->getPropertyIvarDecl();
96 if (!(*ID))
97 return false;
98
99 QualType T = (*ID)->getType();
100 if (!T->isObjCRetainableType())
101 return false;
102
103 (*PD) = I->getPropertyDecl();
104 // Shouldn't be able to synthesize a property that doesn't exist.
105 assert(*PD);
106
107 return true;
108}
109
Devin Coughlinea02bba2016-02-25 19:13:43 +0000110static bool synthesizedPropertyRequiresRelease(const ObjCPropertyDecl *PD) {
111 // A synthesized property must be released if and only if the kind of setter
112 // was neither 'assign' or 'weak'.
113 ObjCPropertyDecl::SetterKind SK = PD->getSetterKind();
114 return (SK != ObjCPropertyDecl::Assign && SK != ObjCPropertyDecl::Weak);
115}
Devin Coughlin30751342016-01-27 01:41:58 +0000116
Devin Coughlinea02bba2016-02-25 19:13:43 +0000117static void checkObjCDealloc(const CheckerBase *Checker,
118 const ObjCImplementationDecl *D,
119 const LangOptions &LOpts, BugReporter &BR) {
Ted Kremenek0e7d2522008-07-03 04:29:21 +0000120
Devin Coughlinea02bba2016-02-25 19:13:43 +0000121 assert(LOpts.getGC() != LangOptions::GCOnly);
122 assert(!LOpts.ObjCAutoRefCount);
Mike Stump11289f42009-09-09 15:08:12 +0000123
Devin Coughlinea02bba2016-02-25 19:13:43 +0000124 ASTContext &Ctx = BR.getContext();
Ted Kremenek5ef32db2011-08-12 23:37:29 +0000125 const ObjCInterfaceDecl *ID = D->getClassInterface();
Mike Stump11289f42009-09-09 15:08:12 +0000126
Devin Coughlin30751342016-01-27 01:41:58 +0000127 // Does the class contain any synthesized properties that are retainable?
Ted Kremenek37a2c0d2008-07-07 06:36:08 +0000128 // If not, skip the check entirely.
Devin Coughlin30751342016-01-27 01:41:58 +0000129 bool containsRetainedSynthesizedProperty = false;
130 for (const auto *I : D->property_impls()) {
Devin Coughlinea02bba2016-02-25 19:13:43 +0000131 const ObjCIvarDecl *ID = nullptr;
132 const ObjCPropertyDecl *PD = nullptr;
133 if (!isSynthesizedRetainableProperty(I, &ID, &PD))
134 continue;
135
136 if (synthesizedPropertyRequiresRelease(PD)) {
Devin Coughlin30751342016-01-27 01:41:58 +0000137 containsRetainedSynthesizedProperty = true;
138 break;
139 }
Ted Kremenek37a2c0d2008-07-07 06:36:08 +0000140 }
Mike Stump11289f42009-09-09 15:08:12 +0000141
Devin Coughlin30751342016-01-27 01:41:58 +0000142 if (!containsRetainedSynthesizedProperty)
Ted Kremenek37a2c0d2008-07-07 06:36:08 +0000143 return;
Mike Stump11289f42009-09-09 15:08:12 +0000144
Devin Coughlinea02bba2016-02-25 19:13:43 +0000145 // Determine if the class subclasses NSObject.
146 IdentifierInfo* NSObjectII = &Ctx.Idents.get("NSObject");
147 IdentifierInfo* SenTestCaseII = &Ctx.Idents.get("SenTestCase");
Devin Coughlin88691c12016-02-25 18:55:24 +0000148
Devin Coughlin88691c12016-02-25 18:55:24 +0000149 for ( ; ID ; ID = ID->getSuperClass()) {
150 IdentifierInfo *II = ID->getIdentifier();
151
152 if (II == NSObjectII)
Devin Coughlinea02bba2016-02-25 19:13:43 +0000153 break;
Devin Coughlin88691c12016-02-25 18:55:24 +0000154
155 // FIXME: For now, ignore classes that subclass SenTestCase, as these don't
156 // need to implement -dealloc. They implement tear down in another way,
157 // which we should try and catch later.
158 // http://llvm.org/bugs/show_bug.cgi?id=3187
159 if (II == SenTestCaseII)
Devin Coughlinea02bba2016-02-25 19:13:43 +0000160 return;
Devin Coughlin88691c12016-02-25 18:55:24 +0000161 }
162
Devin Coughlinea02bba2016-02-25 19:13:43 +0000163 if (!ID)
Devin Coughlin88691c12016-02-25 18:55:24 +0000164 return;
165
Devin Coughlinea02bba2016-02-25 19:13:43 +0000166 // Get the "dealloc" selector.
167 IdentifierInfo* II = &Ctx.Idents.get("dealloc");
168 Selector S = Ctx.Selectors.getSelector(0, &II);
169 const ObjCMethodDecl *MD = nullptr;
170
171 // Scan the instance methods for "dealloc".
172 for (const auto *I : D->instance_methods()) {
173 if (I->getSelector() == S) {
174 MD = I;
175 break;
176 }
177 }
178
179 if (!MD) { // No dealloc found.
180
181 const char* name = LOpts.getGC() == LangOptions::NonGC
182 ? "missing -dealloc"
183 : "missing -dealloc (Hybrid MM, non-GC)";
184
185 std::string buf;
186 llvm::raw_string_ostream os(buf);
187 os << "Objective-C class '" << *D << "' lacks a 'dealloc' instance method";
188
189 PathDiagnosticLocation DLoc =
190 PathDiagnosticLocation::createBegin(D, BR.getSourceManager());
191
192 BR.EmitBasicReport(D, Checker, name, categories::CoreFoundationObjectiveC,
193 os.str(), DLoc);
194 return;
195 }
196
197 // Get the "release" selector.
198 IdentifierInfo* RII = &Ctx.Idents.get("release");
199 Selector RS = Ctx.Selectors.getSelector(0, &RII);
200
201 // Get the "self" identifier
202 IdentifierInfo* SelfII = &Ctx.Idents.get("self");
203
204 // Scan for missing and extra releases of ivars used by implementations
205 // of synthesized properties
206 for (const auto *I : D->property_impls()) {
207 const ObjCIvarDecl *ID = nullptr;
208 const ObjCPropertyDecl *PD = nullptr;
209 if (!isSynthesizedRetainableProperty(I, &ID, &PD))
210 continue;
211
212 bool requiresRelease = synthesizedPropertyRequiresRelease(PD);
213 if (scan_ivar_release(MD->getBody(), ID, PD, RS, SelfII, Ctx)
214 != requiresRelease) {
215 const char *name = nullptr;
216 std::string buf;
217 llvm::raw_string_ostream os(buf);
218
219 if (requiresRelease) {
220 name = LOpts.getGC() == LangOptions::NonGC
221 ? "missing ivar release (leak)"
222 : "missing ivar release (Hybrid MM, non-GC)";
223
224 os << "The '" << *ID << "' instance variable in '" << *D
225 << "' was retained by a synthesized property "
226 "but was not released in 'dealloc'";
227 } else {
228 // It is common for the ivars for read-only assign properties to
229 // always be stored retained, so don't warn for a release in
230 // dealloc for the ivar backing these properties.
231 if (PD->isReadOnly())
232 continue;
233
234 name = LOpts.getGC() == LangOptions::NonGC
235 ? "extra ivar release (use-after-release)"
236 : "extra ivar release (Hybrid MM, non-GC)";
237
238 os << "The '" << *ID << "' instance variable in '" << *D
239 << "' was not retained by a synthesized property "
240 "but was released in 'dealloc'";
241 }
242
243 // If @synthesize statement is missing, fall back to @property statement.
244 const Decl *SPDecl = I->getLocation().isValid()
245 ? static_cast<const Decl *>(I)
246 : static_cast<const Decl *>(PD);
247 PathDiagnosticLocation SPLoc =
248 PathDiagnosticLocation::createBegin(SPDecl, BR.getSourceManager());
249
250 BR.EmitBasicReport(MD, Checker, name,
251 categories::CoreFoundationObjectiveC, os.str(), SPLoc);
252 }
253 }
254}
255
256//===----------------------------------------------------------------------===//
257// ObjCDeallocChecker
258//===----------------------------------------------------------------------===//
259
260namespace {
261class ObjCDeallocChecker : public Checker<
262 check::ASTDecl<ObjCImplementationDecl> > {
263public:
264 void checkASTDecl(const ObjCImplementationDecl *D, AnalysisManager& mgr,
265 BugReporter &BR) const {
266 if (mgr.getLangOpts().getGC() == LangOptions::GCOnly ||
267 mgr.getLangOpts().ObjCAutoRefCount)
268 return;
269 checkObjCDealloc(this, cast<ObjCImplementationDecl>(D), mgr.getLangOpts(),
270 BR);
271 }
272};
273}
274
275void ento::registerObjCDeallocChecker(CheckerManager &mgr) {
276 mgr.registerChecker<ObjCDeallocChecker>();
Argyrios Kyrtzidisaf45aca2011-02-17 21:39:33 +0000277}