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