Ted Kremenek | 0e7d252 | 2008-07-03 04:29:21 +0000 | [diff] [blame] | 1 | //==- 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 Coughlin | ea02bba | 2016-02-25 19:13:43 +0000 | [diff] [blame^] | 10 | // 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 Kremenek | 0e7d252 | 2008-07-03 04:29:21 +0000 | [diff] [blame] | 13 | // |
| 14 | //===----------------------------------------------------------------------===// |
| 15 | |
Argyrios Kyrtzidis | af45aca | 2011-02-17 21:39:33 +0000 | [diff] [blame] | 16 | #include "ClangSACheckers.h" |
Benjamin Kramer | ea70eb3 | 2012-12-01 15:09:41 +0000 | [diff] [blame] | 17 | #include "clang/AST/Attr.h" |
Ted Kremenek | 0e7d252 | 2008-07-03 04:29:21 +0000 | [diff] [blame] | 18 | #include "clang/AST/DeclObjC.h" |
Benjamin Kramer | ea70eb3 | 2012-12-01 15:09:41 +0000 | [diff] [blame] | 19 | #include "clang/AST/Expr.h" |
| 20 | #include "clang/AST/ExprObjC.h" |
Ted Kremenek | e66ca6f | 2008-07-03 14:35:01 +0000 | [diff] [blame] | 21 | #include "clang/Basic/LangOptions.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 22 | #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 Kremenek | 3f04949 | 2008-10-29 04:30:28 +0000 | [diff] [blame] | 26 | #include "llvm/Support/raw_ostream.h" |
Ted Kremenek | 0e7d252 | 2008-07-03 04:29:21 +0000 | [diff] [blame] | 27 | |
| 28 | using namespace clang; |
Ted Kremenek | 98857c9 | 2010-12-23 07:20:52 +0000 | [diff] [blame] | 29 | using namespace ento; |
Ted Kremenek | 0e7d252 | 2008-07-03 04:29:21 +0000 | [diff] [blame] | 30 | |
Devin Coughlin | ea02bba | 2016-02-25 19:13:43 +0000 | [diff] [blame^] | 31 | // FIXME: This was taken from IvarInvalidationChecker.cpp |
| 32 | static 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 Coughlin | 982c42d | 2016-02-11 22:13:20 +0000 | [diff] [blame] | 40 | |
Devin Coughlin | ea02bba | 2016-02-25 19:13:43 +0000 | [diff] [blame^] | 41 | static bool scan_ivar_release(Stmt *S, const ObjCIvarDecl *ID, |
| 42 | const ObjCPropertyDecl *PD, |
| 43 | Selector Release, |
| 44 | IdentifierInfo* SelfII, |
| 45 | ASTContext &Ctx) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 46 | |
Devin Coughlin | ea02bba | 2016-02-25 19:13:43 +0000 | [diff] [blame^] | 47 | // [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 Kremenek | 3f04949 | 2008-10-29 04:30:28 +0000 | [diff] [blame] | 55 | |
Devin Coughlin | ea02bba | 2016-02-25 19:13:43 +0000 | [diff] [blame^] | 56 | // [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 Coughlin | 3075134 | 2016-01-27 01:41:58 +0000 | [diff] [blame] | 88 | static 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 Coughlin | ea02bba | 2016-02-25 19:13:43 +0000 | [diff] [blame^] | 110 | static 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 Coughlin | 3075134 | 2016-01-27 01:41:58 +0000 | [diff] [blame] | 116 | |
Devin Coughlin | ea02bba | 2016-02-25 19:13:43 +0000 | [diff] [blame^] | 117 | static void checkObjCDealloc(const CheckerBase *Checker, |
| 118 | const ObjCImplementationDecl *D, |
| 119 | const LangOptions &LOpts, BugReporter &BR) { |
Ted Kremenek | 0e7d252 | 2008-07-03 04:29:21 +0000 | [diff] [blame] | 120 | |
Devin Coughlin | ea02bba | 2016-02-25 19:13:43 +0000 | [diff] [blame^] | 121 | assert(LOpts.getGC() != LangOptions::GCOnly); |
| 122 | assert(!LOpts.ObjCAutoRefCount); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 123 | |
Devin Coughlin | ea02bba | 2016-02-25 19:13:43 +0000 | [diff] [blame^] | 124 | ASTContext &Ctx = BR.getContext(); |
Ted Kremenek | 5ef32db | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 125 | const ObjCInterfaceDecl *ID = D->getClassInterface(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 126 | |
Devin Coughlin | 3075134 | 2016-01-27 01:41:58 +0000 | [diff] [blame] | 127 | // Does the class contain any synthesized properties that are retainable? |
Ted Kremenek | 37a2c0d | 2008-07-07 06:36:08 +0000 | [diff] [blame] | 128 | // If not, skip the check entirely. |
Devin Coughlin | 3075134 | 2016-01-27 01:41:58 +0000 | [diff] [blame] | 129 | bool containsRetainedSynthesizedProperty = false; |
| 130 | for (const auto *I : D->property_impls()) { |
Devin Coughlin | ea02bba | 2016-02-25 19:13:43 +0000 | [diff] [blame^] | 131 | const ObjCIvarDecl *ID = nullptr; |
| 132 | const ObjCPropertyDecl *PD = nullptr; |
| 133 | if (!isSynthesizedRetainableProperty(I, &ID, &PD)) |
| 134 | continue; |
| 135 | |
| 136 | if (synthesizedPropertyRequiresRelease(PD)) { |
Devin Coughlin | 3075134 | 2016-01-27 01:41:58 +0000 | [diff] [blame] | 137 | containsRetainedSynthesizedProperty = true; |
| 138 | break; |
| 139 | } |
Ted Kremenek | 37a2c0d | 2008-07-07 06:36:08 +0000 | [diff] [blame] | 140 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 141 | |
Devin Coughlin | 3075134 | 2016-01-27 01:41:58 +0000 | [diff] [blame] | 142 | if (!containsRetainedSynthesizedProperty) |
Ted Kremenek | 37a2c0d | 2008-07-07 06:36:08 +0000 | [diff] [blame] | 143 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 144 | |
Devin Coughlin | ea02bba | 2016-02-25 19:13:43 +0000 | [diff] [blame^] | 145 | // Determine if the class subclasses NSObject. |
| 146 | IdentifierInfo* NSObjectII = &Ctx.Idents.get("NSObject"); |
| 147 | IdentifierInfo* SenTestCaseII = &Ctx.Idents.get("SenTestCase"); |
Devin Coughlin | 88691c1 | 2016-02-25 18:55:24 +0000 | [diff] [blame] | 148 | |
Devin Coughlin | 88691c1 | 2016-02-25 18:55:24 +0000 | [diff] [blame] | 149 | for ( ; ID ; ID = ID->getSuperClass()) { |
| 150 | IdentifierInfo *II = ID->getIdentifier(); |
| 151 | |
| 152 | if (II == NSObjectII) |
Devin Coughlin | ea02bba | 2016-02-25 19:13:43 +0000 | [diff] [blame^] | 153 | break; |
Devin Coughlin | 88691c1 | 2016-02-25 18:55:24 +0000 | [diff] [blame] | 154 | |
| 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 Coughlin | ea02bba | 2016-02-25 19:13:43 +0000 | [diff] [blame^] | 160 | return; |
Devin Coughlin | 88691c1 | 2016-02-25 18:55:24 +0000 | [diff] [blame] | 161 | } |
| 162 | |
Devin Coughlin | ea02bba | 2016-02-25 19:13:43 +0000 | [diff] [blame^] | 163 | if (!ID) |
Devin Coughlin | 88691c1 | 2016-02-25 18:55:24 +0000 | [diff] [blame] | 164 | return; |
| 165 | |
Devin Coughlin | ea02bba | 2016-02-25 19:13:43 +0000 | [diff] [blame^] | 166 | // 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 | |
| 260 | namespace { |
| 261 | class ObjCDeallocChecker : public Checker< |
| 262 | check::ASTDecl<ObjCImplementationDecl> > { |
| 263 | public: |
| 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 | |
| 275 | void ento::registerObjCDeallocChecker(CheckerManager &mgr) { |
| 276 | mgr.registerChecker<ObjCDeallocChecker>(); |
Argyrios Kyrtzidis | af45aca | 2011-02-17 21:39:33 +0000 | [diff] [blame] | 277 | } |