ObjCSelfInitChecker.cpp revision eaf969bf4b657f0c4577f38a39f8c4ef1d9272fc
1//== ObjCSelfInitChecker.cpp - Checker for 'self' initialization -*- 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//
10// This defines ObjCSelfInitChecker, a builtin check that checks for uses of
11// 'self' before proper initialization.
12//
13//===----------------------------------------------------------------------===//
14
15// This checks initialization methods to verify that they assign 'self' to the
16// result of an initialization call (e.g. [super init], or [self initWith..])
17// before using 'self' or any instance variable.
18//
19// To perform the required checking, values are tagged wih flags that indicate
20// 1) if the object is the one pointed to by 'self', and 2) if the object
21// is the result of an initializer (e.g. [super init]).
22//
23// Uses of an object that is true for 1) but not 2) trigger a diagnostic.
24// The uses that are currently checked are:
25//  - Using instance variables.
26//  - Returning the object.
27//
28// Note that we don't check for an invalid 'self' that is the receiver of an
29// obj-c message expression to cut down false positives where logging functions
30// get information from self (like its class) or doing "invalidation" on self
31// when the initialization fails.
32//
33// Because the object that 'self' points to gets invalidated when a call
34// receives a reference to 'self', the checker keeps track and passes the flags
35// for 1) and 2) to the new object that 'self' points to after the call.
36//
37// FIXME (rdar://7937506): In the case of:
38//   [super init];
39//   return self;
40// Have an extra PathDiagnosticPiece in the path that says "called [super init],
41// but didn't assign the result to self."
42
43//===----------------------------------------------------------------------===//
44
45// FIXME: Somehow stick the link to Apple's documentation about initializing
46// objects in the diagnostics.
47// http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/ObjectiveC/Articles/ocAllocInit.html
48
49#include "ExprEngineInternalChecks.h"
50#include "clang/StaticAnalyzer/PathSensitive/CheckerVisitor.h"
51#include "clang/StaticAnalyzer/PathSensitive/GRStateTrait.h"
52#include "clang/StaticAnalyzer/BugReporter/BugType.h"
53#include "clang/Analysis/DomainSpecific/CocoaConventions.h"
54#include "clang/AST/ParentMap.h"
55
56using namespace clang;
57using namespace ento;
58
59static bool shouldRunOnFunctionOrMethod(const NamedDecl *ND);
60static bool isInitializationMethod(const ObjCMethodDecl *MD);
61static bool isInitMessage(const ObjCMessage &msg);
62static bool isSelfVar(SVal location, CheckerContext &C);
63
64namespace {
65enum SelfFlagEnum {
66  /// \brief No flag set.
67  SelfFlag_None = 0x0,
68  /// \brief Value came from 'self'.
69  SelfFlag_Self    = 0x1,
70  /// \brief Value came from the result of an initializer (e.g. [super init]).
71  SelfFlag_InitRes = 0x2
72};
73}
74
75namespace {
76class ObjCSelfInitChecker : public CheckerVisitor<ObjCSelfInitChecker> {
77  /// \brief A call receiving a reference to 'self' invalidates the object that
78  /// 'self' contains. This field keeps the "self flags" assigned to the 'self'
79  /// object before the call and assign them to the new object that 'self'
80  /// points to after the call.
81  SelfFlagEnum preCallSelfFlags;
82
83public:
84  static void *getTag() { static int tag = 0; return &tag; }
85  void postVisitObjCMessage(CheckerContext &C, ObjCMessage msg);
86  void PostVisitObjCIvarRefExpr(CheckerContext &C, const ObjCIvarRefExpr *E);
87  void PreVisitReturnStmt(CheckerContext &C, const ReturnStmt *S);
88  void PreVisitGenericCall(CheckerContext &C, const CallExpr *CE);
89  void PostVisitGenericCall(CheckerContext &C, const CallExpr *CE);
90  virtual void visitLocation(CheckerContext &C, const Stmt *S, SVal location,
91                             bool isLoad);
92};
93} // end anonymous namespace
94
95void ento::registerObjCSelfInitChecker(ExprEngine &Eng) {
96  if (Eng.getContext().getLangOptions().ObjC1)
97    Eng.registerCheck(new ObjCSelfInitChecker());
98}
99
100namespace {
101
102class InitSelfBug : public BugType {
103  const std::string desc;
104public:
105  InitSelfBug() : BugType("missing \"self = [{initializer}]\"",
106                          "missing \"self = [{initializer}]\"") {}
107};
108
109} // end anonymous namespace
110
111typedef llvm::ImmutableMap<SymbolRef, unsigned> SelfFlag;
112
113namespace clang {
114namespace ento {
115  template<>
116  struct GRStateTrait<SelfFlag> : public GRStatePartialTrait<SelfFlag> {
117    static void* GDMIndex() {
118      static int index = 0;
119      return &index;
120    }
121  };
122}
123}
124
125static SelfFlagEnum getSelfFlags(SVal val, const GRState *state) {
126  if (SymbolRef sym = val.getAsSymbol())
127    if (const unsigned *attachedFlags = state->get<SelfFlag>(sym))
128      return (SelfFlagEnum)*attachedFlags;
129  return SelfFlag_None;
130}
131
132static SelfFlagEnum getSelfFlags(SVal val, CheckerContext &C) {
133  return getSelfFlags(val, C.getState());
134}
135
136static void addSelfFlag(SVal val, SelfFlagEnum flag, CheckerContext &C) {
137  const GRState *state = C.getState();
138  // FIXME: We tag the symbol that the SVal wraps but this is conceptually
139  // wrong, we should tag the SVal; the fact that there is a symbol behind the
140  // SVal is irrelevant.
141  if (SymbolRef sym = val.getAsSymbol())
142    C.addTransition(state->set<SelfFlag>(sym, getSelfFlags(val, C) | flag));
143}
144
145static bool hasSelfFlag(SVal val, SelfFlagEnum flag, CheckerContext &C) {
146  return getSelfFlags(val, C) & flag;
147}
148
149/// \brief Returns true of the value of the expression is the object that 'self'
150/// points to and is an object that did not come from the result of calling
151/// an initializer.
152static bool isInvalidSelf(const Expr *E, CheckerContext &C) {
153  SVal exprVal = C.getState()->getSVal(E);
154  if (!hasSelfFlag(exprVal, SelfFlag_Self, C))
155    return false; // value did not come from 'self'.
156  if (hasSelfFlag(exprVal, SelfFlag_InitRes, C))
157    return false; // 'self' is properly initialized.
158
159  return true;
160}
161
162static void checkForInvalidSelf(const Expr *E, CheckerContext &C,
163                                const char *errorStr) {
164  if (!E)
165    return;
166  if (!isInvalidSelf(E, C))
167    return;
168
169  // Generate an error node.
170  ExplodedNode *N = C.generateSink();
171  if (!N)
172    return;
173
174  EnhancedBugReport *report =
175    new EnhancedBugReport(*new InitSelfBug(), errorStr, N);
176  C.EmitReport(report);
177}
178
179void ObjCSelfInitChecker::postVisitObjCMessage(CheckerContext &C,
180                                               ObjCMessage msg) {
181  // When encountering a message that does initialization (init rule),
182  // tag the return value so that we know later on that if self has this value
183  // then it is properly initialized.
184
185  // FIXME: A callback should disable checkers at the start of functions.
186  if (!shouldRunOnFunctionOrMethod(dyn_cast<NamedDecl>(
187                                     C.getCurrentAnalysisContext()->getDecl())))
188    return;
189
190  if (isInitMessage(msg)) {
191    // Tag the return value as the result of an initializer.
192    const GRState *state = C.getState();
193    SVal V = state->getSVal(msg.getOriginExpr());
194    addSelfFlag(V, SelfFlag_InitRes, C);
195    return;
196  }
197
198  // We don't check for an invalid 'self' in an obj-c message expression to cut
199  // down false positives where logging functions get information from self
200  // (like its class) or doing "invalidation" on self when the initialization
201  // fails.
202}
203
204void ObjCSelfInitChecker::PostVisitObjCIvarRefExpr(CheckerContext &C,
205                                                   const ObjCIvarRefExpr *E) {
206  // FIXME: A callback should disable checkers at the start of functions.
207  if (!shouldRunOnFunctionOrMethod(dyn_cast<NamedDecl>(
208                                     C.getCurrentAnalysisContext()->getDecl())))
209    return;
210
211  checkForInvalidSelf(E->getBase(), C,
212         "Using an ivar before setting 'self' to the result of an initializer");
213}
214
215void ObjCSelfInitChecker::PreVisitReturnStmt(CheckerContext &C,
216                                             const ReturnStmt *S) {
217  // FIXME: A callback should disable checkers at the start of functions.
218  if (!shouldRunOnFunctionOrMethod(dyn_cast<NamedDecl>(
219                                     C.getCurrentAnalysisContext()->getDecl())))
220    return;
221
222  checkForInvalidSelf(S->getRetValue(), C,
223          "Returning 'self' before setting it to the result of an initializer");
224}
225
226// When a call receives a reference to 'self', [Pre/Post]VisitGenericCall pass
227// the SelfFlags from the object 'self' point to before the call, to the new
228// object after the call.
229
230void ObjCSelfInitChecker::PreVisitGenericCall(CheckerContext &C,
231                                              const CallExpr *CE) {
232  const GRState *state = C.getState();
233  for (CallExpr::const_arg_iterator
234         I = CE->arg_begin(), E = CE->arg_end(); I != E; ++I) {
235    SVal argV = state->getSVal(*I);
236    if (isSelfVar(argV, C)) {
237      preCallSelfFlags = getSelfFlags(state->getSVal(cast<Loc>(argV)), C);
238      return;
239    }
240  }
241}
242
243void ObjCSelfInitChecker::PostVisitGenericCall(CheckerContext &C,
244                                               const CallExpr *CE) {
245  const GRState *state = C.getState();
246  for (CallExpr::const_arg_iterator
247         I = CE->arg_begin(), E = CE->arg_end(); I != E; ++I) {
248    SVal argV = state->getSVal(*I);
249    if (isSelfVar(argV, C)) {
250      addSelfFlag(state->getSVal(cast<Loc>(argV)), preCallSelfFlags, C);
251      return;
252    }
253  }
254}
255
256void ObjCSelfInitChecker::visitLocation(CheckerContext &C, const Stmt *S,
257                                        SVal location, bool isLoad) {
258  // Tag the result of a load from 'self' so that we can easily know that the
259  // value is the object that 'self' points to.
260  const GRState *state = C.getState();
261  if (isSelfVar(location, C))
262    addSelfFlag(state->getSVal(cast<Loc>(location)), SelfFlag_Self, C);
263}
264
265// FIXME: A callback should disable checkers at the start of functions.
266static bool shouldRunOnFunctionOrMethod(const NamedDecl *ND) {
267  if (!ND)
268    return false;
269
270  const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(ND);
271  if (!MD)
272    return false;
273  if (!isInitializationMethod(MD))
274    return false;
275
276  // self = [super init] applies only to NSObject subclasses.
277  // For instance, NSProxy doesn't implement -init.
278  ASTContext& Ctx = MD->getASTContext();
279  IdentifierInfo* NSObjectII = &Ctx.Idents.get("NSObject");
280  ObjCInterfaceDecl* ID = MD->getClassInterface()->getSuperClass();
281  for ( ; ID ; ID = ID->getSuperClass()) {
282    IdentifierInfo *II = ID->getIdentifier();
283
284    if (II == NSObjectII)
285      break;
286  }
287  if (!ID)
288    return false;
289
290  return true;
291}
292
293/// \brief Returns true if the location is 'self'.
294static bool isSelfVar(SVal location, CheckerContext &C) {
295  AnalysisContext *analCtx = C.getCurrentAnalysisContext();
296  if (!analCtx->getSelfDecl())
297    return false;
298  if (!isa<loc::MemRegionVal>(location))
299    return false;
300
301  loc::MemRegionVal MRV = cast<loc::MemRegionVal>(location);
302  if (const DeclRegion *DR = dyn_cast<DeclRegion>(MRV.getRegion()))
303    return (DR->getDecl() == analCtx->getSelfDecl());
304
305  return false;
306}
307
308static bool isInitializationMethod(const ObjCMethodDecl *MD) {
309  // Init methods with prefix like '-(id)_init' are private and the requirements
310  // are less strict so we don't check those.
311  return MD->isInstanceMethod() &&
312      cocoa::deriveNamingConvention(MD->getSelector(),
313                                    /*ignorePrefix=*/false) == cocoa::InitRule;
314}
315
316static bool isInitMessage(const ObjCMessage &msg) {
317  return cocoa::deriveNamingConvention(msg.getSelector()) == cocoa::InitRule;
318}
319