| Anna Zaks | 461f239 | 2012-09-27 19:45:15 +0000 | [diff] [blame] | 1 | //=- DirectIvarAssignment.cpp - Check rules on ObjC properties -*- C++ ----*-==// |
| 2 | // |
| Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| Anna Zaks | 461f239 | 2012-09-27 19:45:15 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| Anna Zaks | 0e9c941 | 2013-01-17 23:24:58 +0000 | [diff] [blame] | 9 | // Check that Objective C properties are set with the setter, not though a |
| 10 | // direct assignment. |
| 11 | // |
| 12 | // Two versions of a checker exist: one that checks all methods and the other |
| 13 | // that only checks the methods annotated with |
| 14 | // __attribute__((annotate("objc_no_direct_instance_variable_assignment"))) |
| 15 | // |
| 16 | // The checker does not warn about assignments to Ivars, annotated with |
| 17 | // __attribute__((objc_allow_direct_instance_variable_assignment"))). This |
| 18 | // annotation serves as a false positive suppression mechanism for the |
| 19 | // checker. The annotation is allowed on properties and Ivars. |
| Anna Zaks | 461f239 | 2012-09-27 19:45:15 +0000 | [diff] [blame] | 20 | // |
| 21 | //===----------------------------------------------------------------------===// |
| 22 | |
| Kristof Umann | 76a2150 | 2018-12-15 16:23:51 +0000 | [diff] [blame] | 23 | #include "clang/StaticAnalyzer/Checkers/BuiltinCheckerRegistration.h" |
| Anna Zaks | 25dd07c | 2012-12-05 01:14:37 +0000 | [diff] [blame] | 24 | #include "clang/AST/Attr.h" |
| Anna Zaks | 461f239 | 2012-09-27 19:45:15 +0000 | [diff] [blame] | 25 | #include "clang/AST/DeclObjC.h" |
| 26 | #include "clang/AST/StmtVisitor.h" |
| Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 27 | #include "clang/StaticAnalyzer/Core/BugReporter/BugReporter.h" |
| 28 | #include "clang/StaticAnalyzer/Core/Checker.h" |
| 29 | #include "clang/StaticAnalyzer/Core/PathSensitive/AnalysisManager.h" |
| Anna Zaks | 461f239 | 2012-09-27 19:45:15 +0000 | [diff] [blame] | 30 | #include "llvm/ADT/DenseMap.h" |
| 31 | |
| 32 | using namespace clang; |
| 33 | using namespace ento; |
| 34 | |
| 35 | namespace { |
| 36 | |
| Anna Zaks | 25dd07c | 2012-12-05 01:14:37 +0000 | [diff] [blame] | 37 | /// The default method filter, which is used to filter out the methods on which |
| 38 | /// the check should not be performed. |
| 39 | /// |
| 40 | /// Checks for the init, dealloc, and any other functions that might be allowed |
| 41 | /// to perform direct instance variable assignment based on their name. |
| Benjamin Kramer | 27bc504 | 2013-08-09 17:17:42 +0000 | [diff] [blame] | 42 | static bool DefaultMethodFilter(const ObjCMethodDecl *M) { |
| Alexander Kornienko | 9c10490 | 2015-12-28 13:06:58 +0000 | [diff] [blame] | 43 | return M->getMethodFamily() == OMF_init || |
| 44 | M->getMethodFamily() == OMF_dealloc || |
| 45 | M->getMethodFamily() == OMF_copy || |
| 46 | M->getMethodFamily() == OMF_mutableCopy || |
| 47 | M->getSelector().getNameForSlot(0).find("init") != StringRef::npos || |
| 48 | M->getSelector().getNameForSlot(0).find("Init") != StringRef::npos; |
| Benjamin Kramer | 27bc504 | 2013-08-09 17:17:42 +0000 | [diff] [blame] | 49 | } |
| Anna Zaks | 25dd07c | 2012-12-05 01:14:37 +0000 | [diff] [blame] | 50 | |
| Anna Zaks | 461f239 | 2012-09-27 19:45:15 +0000 | [diff] [blame] | 51 | class DirectIvarAssignment : |
| 52 | public Checker<check::ASTDecl<ObjCImplementationDecl> > { |
| 53 | |
| 54 | typedef llvm::DenseMap<const ObjCIvarDecl*, |
| 55 | const ObjCPropertyDecl*> IvarToPropertyMapTy; |
| 56 | |
| 57 | /// A helper class, which walks the AST and locates all assignments to ivars |
| 58 | /// in the given function. |
| 59 | class MethodCrawler : public ConstStmtVisitor<MethodCrawler> { |
| 60 | const IvarToPropertyMapTy &IvarToPropMap; |
| 61 | const ObjCMethodDecl *MD; |
| 62 | const ObjCInterfaceDecl *InterfD; |
| 63 | BugReporter &BR; |
| Alexander Kornienko | 4aca9b1 | 2014-02-11 21:49:21 +0000 | [diff] [blame] | 64 | const CheckerBase *Checker; |
| Anna Zaks | 461f239 | 2012-09-27 19:45:15 +0000 | [diff] [blame] | 65 | LocationOrAnalysisDeclContext DCtx; |
| 66 | |
| 67 | public: |
| 68 | MethodCrawler(const IvarToPropertyMapTy &InMap, const ObjCMethodDecl *InMD, |
| Alexander Kornienko | 4aca9b1 | 2014-02-11 21:49:21 +0000 | [diff] [blame] | 69 | const ObjCInterfaceDecl *InID, BugReporter &InBR, |
| 70 | const CheckerBase *Checker, AnalysisDeclContext *InDCtx) |
| 71 | : IvarToPropMap(InMap), MD(InMD), InterfD(InID), BR(InBR), |
| 72 | Checker(Checker), DCtx(InDCtx) {} |
| Anna Zaks | 461f239 | 2012-09-27 19:45:15 +0000 | [diff] [blame] | 73 | |
| 74 | void VisitStmt(const Stmt *S) { VisitChildren(S); } |
| 75 | |
| 76 | void VisitBinaryOperator(const BinaryOperator *BO); |
| 77 | |
| 78 | void VisitChildren(const Stmt *S) { |
| Benjamin Kramer | 642f173 | 2015-07-02 21:03:14 +0000 | [diff] [blame] | 79 | for (const Stmt *Child : S->children()) |
| 80 | if (Child) |
| 81 | this->Visit(Child); |
| Anna Zaks | 461f239 | 2012-09-27 19:45:15 +0000 | [diff] [blame] | 82 | } |
| 83 | }; |
| 84 | |
| 85 | public: |
| Benjamin Kramer | 27bc504 | 2013-08-09 17:17:42 +0000 | [diff] [blame] | 86 | bool (*ShouldSkipMethod)(const ObjCMethodDecl *); |
| Anna Zaks | 25dd07c | 2012-12-05 01:14:37 +0000 | [diff] [blame] | 87 | |
| 88 | DirectIvarAssignment() : ShouldSkipMethod(&DefaultMethodFilter) {} |
| 89 | |
| Anna Zaks | 461f239 | 2012-09-27 19:45:15 +0000 | [diff] [blame] | 90 | void checkASTDecl(const ObjCImplementationDecl *D, AnalysisManager& Mgr, |
| 91 | BugReporter &BR) const; |
| 92 | }; |
| 93 | |
| 94 | static const ObjCIvarDecl *findPropertyBackingIvar(const ObjCPropertyDecl *PD, |
| Anna Zaks | 6aef455 | 2012-09-27 21:57:17 +0000 | [diff] [blame] | 95 | const ObjCInterfaceDecl *InterD, |
| 96 | ASTContext &Ctx) { |
| Anna Zaks | 461f239 | 2012-09-27 19:45:15 +0000 | [diff] [blame] | 97 | // Check for synthesized ivars. |
| 98 | ObjCIvarDecl *ID = PD->getPropertyIvarDecl(); |
| 99 | if (ID) |
| 100 | return ID; |
| 101 | |
| Anna Zaks | 6aef455 | 2012-09-27 21:57:17 +0000 | [diff] [blame] | 102 | ObjCInterfaceDecl *NonConstInterD = const_cast<ObjCInterfaceDecl*>(InterD); |
| 103 | |
| Anna Zaks | 461f239 | 2012-09-27 19:45:15 +0000 | [diff] [blame] | 104 | // Check for existing "_PropName". |
| Anna Zaks | 6aef455 | 2012-09-27 21:57:17 +0000 | [diff] [blame] | 105 | ID = NonConstInterD->lookupInstanceVariable(PD->getDefaultSynthIvarName(Ctx)); |
| Anna Zaks | 461f239 | 2012-09-27 19:45:15 +0000 | [diff] [blame] | 106 | if (ID) |
| 107 | return ID; |
| 108 | |
| 109 | // Check for existing "PropName". |
| 110 | IdentifierInfo *PropIdent = PD->getIdentifier(); |
| Anna Zaks | 6aef455 | 2012-09-27 21:57:17 +0000 | [diff] [blame] | 111 | ID = NonConstInterD->lookupInstanceVariable(PropIdent); |
| Anna Zaks | 461f239 | 2012-09-27 19:45:15 +0000 | [diff] [blame] | 112 | |
| 113 | return ID; |
| 114 | } |
| 115 | |
| 116 | void DirectIvarAssignment::checkASTDecl(const ObjCImplementationDecl *D, |
| 117 | AnalysisManager& Mgr, |
| 118 | BugReporter &BR) const { |
| 119 | const ObjCInterfaceDecl *InterD = D->getClassInterface(); |
| 120 | |
| 121 | |
| 122 | IvarToPropertyMapTy IvarToPropMap; |
| 123 | |
| 124 | // Find all properties for this class. |
| Manman Ren | a7a8b1f | 2016-01-26 18:05:23 +0000 | [diff] [blame] | 125 | for (const auto *PD : InterD->instance_properties()) { |
| Anna Zaks | 461f239 | 2012-09-27 19:45:15 +0000 | [diff] [blame] | 126 | // Find the corresponding IVar. |
| Anna Zaks | 6aef455 | 2012-09-27 21:57:17 +0000 | [diff] [blame] | 127 | const ObjCIvarDecl *ID = findPropertyBackingIvar(PD, InterD, |
| 128 | Mgr.getASTContext()); |
| Anna Zaks | 461f239 | 2012-09-27 19:45:15 +0000 | [diff] [blame] | 129 | |
| 130 | if (!ID) |
| 131 | continue; |
| 132 | |
| 133 | // Store the IVar to property mapping. |
| 134 | IvarToPropMap[ID] = PD; |
| 135 | } |
| 136 | |
| 137 | if (IvarToPropMap.empty()) |
| 138 | return; |
| 139 | |
| Aaron Ballman | f26acce | 2014-03-13 19:50:17 +0000 | [diff] [blame] | 140 | for (const auto *M : D->instance_methods()) { |
| Anna Zaks | 461f239 | 2012-09-27 19:45:15 +0000 | [diff] [blame] | 141 | AnalysisDeclContext *DCtx = Mgr.getAnalysisDeclContext(M); |
| 142 | |
| Anna Zaks | 25dd07c | 2012-12-05 01:14:37 +0000 | [diff] [blame] | 143 | if ((*ShouldSkipMethod)(M)) |
| Anna Zaks | 461f239 | 2012-09-27 19:45:15 +0000 | [diff] [blame] | 144 | continue; |
| 145 | |
| 146 | const Stmt *Body = M->getBody(); |
| Anna Zaks | 6aef455 | 2012-09-27 21:57:17 +0000 | [diff] [blame] | 147 | assert(Body); |
| Anna Zaks | 461f239 | 2012-09-27 19:45:15 +0000 | [diff] [blame] | 148 | |
| Alexander Kornienko | 4aca9b1 | 2014-02-11 21:49:21 +0000 | [diff] [blame] | 149 | MethodCrawler MC(IvarToPropMap, M->getCanonicalDecl(), InterD, BR, this, |
| 150 | DCtx); |
| Anna Zaks | 461f239 | 2012-09-27 19:45:15 +0000 | [diff] [blame] | 151 | MC.VisitStmt(Body); |
| 152 | } |
| 153 | } |
| 154 | |
| Anna Zaks | 0e9c941 | 2013-01-17 23:24:58 +0000 | [diff] [blame] | 155 | static bool isAnnotatedToAllowDirectAssignment(const Decl *D) { |
| Aaron Ballman | be22bcb | 2014-03-10 17:08:28 +0000 | [diff] [blame] | 156 | for (const auto *Ann : D->specific_attrs<AnnotateAttr>()) |
| Anna Zaks | 6519564 | 2013-01-16 01:36:00 +0000 | [diff] [blame] | 157 | if (Ann->getAnnotation() == |
| 158 | "objc_allow_direct_instance_variable_assignment") |
| 159 | return true; |
| Anna Zaks | 6519564 | 2013-01-16 01:36:00 +0000 | [diff] [blame] | 160 | return false; |
| 161 | } |
| 162 | |
| Anna Zaks | 461f239 | 2012-09-27 19:45:15 +0000 | [diff] [blame] | 163 | void DirectIvarAssignment::MethodCrawler::VisitBinaryOperator( |
| 164 | const BinaryOperator *BO) { |
| 165 | if (!BO->isAssignmentOp()) |
| 166 | return; |
| 167 | |
| Anna Zaks | 6aef455 | 2012-09-27 21:57:17 +0000 | [diff] [blame] | 168 | const ObjCIvarRefExpr *IvarRef = |
| 169 | dyn_cast<ObjCIvarRefExpr>(BO->getLHS()->IgnoreParenCasts()); |
| Anna Zaks | 461f239 | 2012-09-27 19:45:15 +0000 | [diff] [blame] | 170 | |
| 171 | if (!IvarRef) |
| 172 | return; |
| 173 | |
| 174 | if (const ObjCIvarDecl *D = IvarRef->getDecl()) { |
| 175 | IvarToPropertyMapTy::const_iterator I = IvarToPropMap.find(D); |
| Anna Zaks | 6519564 | 2013-01-16 01:36:00 +0000 | [diff] [blame] | 176 | |
| Anna Zaks | 461f239 | 2012-09-27 19:45:15 +0000 | [diff] [blame] | 177 | if (I != IvarToPropMap.end()) { |
| 178 | const ObjCPropertyDecl *PD = I->second; |
| Anna Zaks | 0e9c941 | 2013-01-17 23:24:58 +0000 | [diff] [blame] | 179 | // Skip warnings on Ivars, annotated with |
| Anna Zaks | 6519564 | 2013-01-16 01:36:00 +0000 | [diff] [blame] | 180 | // objc_allow_direct_instance_variable_assignment. This annotation serves |
| Anna Zaks | 0e9c941 | 2013-01-17 23:24:58 +0000 | [diff] [blame] | 181 | // as a false positive suppression mechanism for the checker. The |
| 182 | // annotation is allowed on properties and ivars. |
| 183 | if (isAnnotatedToAllowDirectAssignment(PD) || |
| 184 | isAnnotatedToAllowDirectAssignment(D)) |
| Anna Zaks | 6519564 | 2013-01-16 01:36:00 +0000 | [diff] [blame] | 185 | return; |
| Anna Zaks | 461f239 | 2012-09-27 19:45:15 +0000 | [diff] [blame] | 186 | |
| 187 | ObjCMethodDecl *GetterMethod = |
| 188 | InterfD->getInstanceMethod(PD->getGetterName()); |
| 189 | ObjCMethodDecl *SetterMethod = |
| 190 | InterfD->getInstanceMethod(PD->getSetterName()); |
| 191 | |
| 192 | if (SetterMethod && SetterMethod->getCanonicalDecl() == MD) |
| 193 | return; |
| 194 | |
| 195 | if (GetterMethod && GetterMethod->getCanonicalDecl() == MD) |
| 196 | return; |
| 197 | |
| Alexander Kornienko | 4aca9b1 | 2014-02-11 21:49:21 +0000 | [diff] [blame] | 198 | BR.EmitBasicReport( |
| 199 | MD, Checker, "Property access", categories::CoreFoundationObjectiveC, |
| Anna Zaks | 461f239 | 2012-09-27 19:45:15 +0000 | [diff] [blame] | 200 | "Direct assignment to an instance variable backing a property; " |
| Alexander Kornienko | 4aca9b1 | 2014-02-11 21:49:21 +0000 | [diff] [blame] | 201 | "use the setter instead", |
| 202 | PathDiagnosticLocation(IvarRef, BR.getSourceManager(), DCtx)); |
| Anna Zaks | 461f239 | 2012-09-27 19:45:15 +0000 | [diff] [blame] | 203 | } |
| 204 | } |
| 205 | } |
| Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 206 | } |
| Anna Zaks | 461f239 | 2012-09-27 19:45:15 +0000 | [diff] [blame] | 207 | |
| Anna Zaks | 25dd07c | 2012-12-05 01:14:37 +0000 | [diff] [blame] | 208 | // Register the checker that checks for direct accesses in functions annotated |
| Ted Kremenek | c632467 | 2012-12-22 00:34:48 +0000 | [diff] [blame] | 209 | // with __attribute__((annotate("objc_no_direct_instance_variable_assignment"))). |
| Benjamin Kramer | 27bc504 | 2013-08-09 17:17:42 +0000 | [diff] [blame] | 210 | static bool AttrFilter(const ObjCMethodDecl *M) { |
| Aaron Ballman | be22bcb | 2014-03-10 17:08:28 +0000 | [diff] [blame] | 211 | for (const auto *Ann : M->specific_attrs<AnnotateAttr>()) |
| Benjamin Kramer | 27bc504 | 2013-08-09 17:17:42 +0000 | [diff] [blame] | 212 | if (Ann->getAnnotation() == "objc_no_direct_instance_variable_assignment") |
| 213 | return false; |
| Benjamin Kramer | 27bc504 | 2013-08-09 17:17:42 +0000 | [diff] [blame] | 214 | return true; |
| Anna Zaks | 25dd07c | 2012-12-05 01:14:37 +0000 | [diff] [blame] | 215 | } |
| 216 | |
| Kristof Umann | 058a7a4 | 2019-01-26 14:23:08 +0000 | [diff] [blame] | 217 | // Register the checker that checks for direct accesses in all functions, |
| 218 | // except for the initialization and copy routines. |
| 219 | void ento::registerDirectIvarAssignment(CheckerManager &mgr) { |
| 220 | mgr.registerChecker<DirectIvarAssignment>(); |
| 221 | } |
| 222 | |
| 223 | bool ento::shouldRegisterDirectIvarAssignment(const LangOptions &LO) { |
| 224 | return true; |
| 225 | } |
| 226 | |
| Anna Zaks | 25dd07c | 2012-12-05 01:14:37 +0000 | [diff] [blame] | 227 | void ento::registerDirectIvarAssignmentForAnnotatedFunctions( |
| 228 | CheckerManager &mgr) { |
| Kristof Umann | 204bf2b | 2019-01-26 21:41:50 +0000 | [diff] [blame^] | 229 | mgr.getChecker<DirectIvarAssignment>()->ShouldSkipMethod = &AttrFilter; |
| Anna Zaks | 25dd07c | 2012-12-05 01:14:37 +0000 | [diff] [blame] | 230 | } |
| Kristof Umann | 058a7a4 | 2019-01-26 14:23:08 +0000 | [diff] [blame] | 231 | |
| 232 | bool ento::shouldRegisterDirectIvarAssignmentForAnnotatedFunctions( |
| 233 | const LangOptions &LO) { |
| 234 | return true; |
| 235 | } |