blob: 4f247ea14670372b3d0184a070627f3fe0af2eb3 [file] [log] [blame]
Argyrios Kyrtzidisd7a31ba2011-01-11 19:45:25 +00001//== 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
Argyrios Kyrtzidis027a6ab2011-02-15 07:42:33 +000049#include "ClangSACheckers.h"
Argyrios Kyrtzidis695fb502011-02-17 21:39:17 +000050#include "clang/StaticAnalyzer/Core/CheckerManager.h"
Ted Kremenek9b663712011-02-10 01:03:03 +000051#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerVisitor.h"
52#include "clang/StaticAnalyzer/Core/PathSensitive/GRStateTrait.h"
53#include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
Argyrios Kyrtzidisd7a31ba2011-01-11 19:45:25 +000054#include "clang/Analysis/DomainSpecific/CocoaConventions.h"
55#include "clang/AST/ParentMap.h"
56
57using namespace clang;
58using namespace ento;
59
60static bool shouldRunOnFunctionOrMethod(const NamedDecl *ND);
61static bool isInitializationMethod(const ObjCMethodDecl *MD);
Argyrios Kyrtzidis432424d2011-01-25 00:03:53 +000062static bool isInitMessage(const ObjCMessage &msg);
Argyrios Kyrtzidisd7a31ba2011-01-11 19:45:25 +000063static bool isSelfVar(SVal location, CheckerContext &C);
64
65namespace {
66enum SelfFlagEnum {
67 /// \brief No flag set.
68 SelfFlag_None = 0x0,
69 /// \brief Value came from 'self'.
70 SelfFlag_Self = 0x1,
71 /// \brief Value came from the result of an initializer (e.g. [super init]).
72 SelfFlag_InitRes = 0x2
73};
74}
75
76namespace {
77class ObjCSelfInitChecker : public CheckerVisitor<ObjCSelfInitChecker> {
78 /// \brief A call receiving a reference to 'self' invalidates the object that
79 /// 'self' contains. This field keeps the "self flags" assigned to the 'self'
80 /// object before the call and assign them to the new object that 'self'
81 /// points to after the call.
82 SelfFlagEnum preCallSelfFlags;
83
84public:
85 static void *getTag() { static int tag = 0; return &tag; }
Argyrios Kyrtzidis432424d2011-01-25 00:03:53 +000086 void postVisitObjCMessage(CheckerContext &C, ObjCMessage msg);
Argyrios Kyrtzidisd7a31ba2011-01-11 19:45:25 +000087 void PostVisitObjCIvarRefExpr(CheckerContext &C, const ObjCIvarRefExpr *E);
88 void PreVisitReturnStmt(CheckerContext &C, const ReturnStmt *S);
89 void PreVisitGenericCall(CheckerContext &C, const CallExpr *CE);
90 void PostVisitGenericCall(CheckerContext &C, const CallExpr *CE);
91 virtual void visitLocation(CheckerContext &C, const Stmt *S, SVal location,
92 bool isLoad);
93};
94} // end anonymous namespace
95
Argyrios Kyrtzidis695fb502011-02-17 21:39:17 +000096static void RegisterObjCSelfInitChecker(ExprEngine &Eng) {
Argyrios Kyrtzidisd7a31ba2011-01-11 19:45:25 +000097 if (Eng.getContext().getLangOptions().ObjC1)
98 Eng.registerCheck(new ObjCSelfInitChecker());
99}
100
Argyrios Kyrtzidis695fb502011-02-17 21:39:17 +0000101void ento::registerObjCSelfInitChecker(CheckerManager &mgr) {
102 mgr.addCheckerRegisterFunction(RegisterObjCSelfInitChecker);
103}
104
Argyrios Kyrtzidisd7a31ba2011-01-11 19:45:25 +0000105namespace {
106
107class InitSelfBug : public BugType {
108 const std::string desc;
109public:
Argyrios Kyrtzidis4717f162011-01-26 01:26:41 +0000110 InitSelfBug() : BugType("missing \"self = [(super or self) init...]\"",
111 "missing \"self = [(super or self) init...]\"") {}
Argyrios Kyrtzidisd7a31ba2011-01-11 19:45:25 +0000112};
113
114} // end anonymous namespace
115
116typedef llvm::ImmutableMap<SymbolRef, unsigned> SelfFlag;
Ted Kremenekb715a7c2011-02-12 03:03:54 +0000117namespace { struct CalledInit {}; }
Argyrios Kyrtzidisd7a31ba2011-01-11 19:45:25 +0000118
119namespace clang {
120namespace ento {
121 template<>
122 struct GRStateTrait<SelfFlag> : public GRStatePartialTrait<SelfFlag> {
123 static void* GDMIndex() {
124 static int index = 0;
125 return &index;
126 }
127 };
Ted Kremenekb715a7c2011-02-12 03:03:54 +0000128 template <>
129 struct GRStateTrait<CalledInit> : public GRStatePartialTrait<bool> {
130 static void *GDMIndex() { static int index = 0; return &index; }
131 };
Argyrios Kyrtzidisd7a31ba2011-01-11 19:45:25 +0000132}
133}
134
135static SelfFlagEnum getSelfFlags(SVal val, const GRState *state) {
136 if (SymbolRef sym = val.getAsSymbol())
137 if (const unsigned *attachedFlags = state->get<SelfFlag>(sym))
138 return (SelfFlagEnum)*attachedFlags;
139 return SelfFlag_None;
140}
141
142static SelfFlagEnum getSelfFlags(SVal val, CheckerContext &C) {
143 return getSelfFlags(val, C.getState());
144}
145
Ted Kremenekb715a7c2011-02-12 03:03:54 +0000146static void addSelfFlag(const GRState *state, SVal val,
147 SelfFlagEnum flag, CheckerContext &C) {
Argyrios Kyrtzidis0ca10402011-02-05 05:54:53 +0000148 // We tag the symbol that the SVal wraps.
Argyrios Kyrtzidisd7a31ba2011-01-11 19:45:25 +0000149 if (SymbolRef sym = val.getAsSymbol())
150 C.addTransition(state->set<SelfFlag>(sym, getSelfFlags(val, C) | flag));
151}
152
153static bool hasSelfFlag(SVal val, SelfFlagEnum flag, CheckerContext &C) {
154 return getSelfFlags(val, C) & flag;
155}
156
157/// \brief Returns true of the value of the expression is the object that 'self'
158/// points to and is an object that did not come from the result of calling
159/// an initializer.
160static bool isInvalidSelf(const Expr *E, CheckerContext &C) {
161 SVal exprVal = C.getState()->getSVal(E);
162 if (!hasSelfFlag(exprVal, SelfFlag_Self, C))
163 return false; // value did not come from 'self'.
164 if (hasSelfFlag(exprVal, SelfFlag_InitRes, C))
165 return false; // 'self' is properly initialized.
166
167 return true;
168}
169
170static void checkForInvalidSelf(const Expr *E, CheckerContext &C,
171 const char *errorStr) {
172 if (!E)
173 return;
Ted Kremenekb715a7c2011-02-12 03:03:54 +0000174
175 if (!C.getState()->get<CalledInit>())
176 return;
177
Argyrios Kyrtzidisd7a31ba2011-01-11 19:45:25 +0000178 if (!isInvalidSelf(E, C))
179 return;
Ted Kremenekb715a7c2011-02-12 03:03:54 +0000180
Argyrios Kyrtzidisd7a31ba2011-01-11 19:45:25 +0000181 // Generate an error node.
182 ExplodedNode *N = C.generateSink();
183 if (!N)
184 return;
185
186 EnhancedBugReport *report =
187 new EnhancedBugReport(*new InitSelfBug(), errorStr, N);
188 C.EmitReport(report);
189}
190
Argyrios Kyrtzidis432424d2011-01-25 00:03:53 +0000191void ObjCSelfInitChecker::postVisitObjCMessage(CheckerContext &C,
192 ObjCMessage msg) {
Argyrios Kyrtzidisd7a31ba2011-01-11 19:45:25 +0000193 // When encountering a message that does initialization (init rule),
194 // tag the return value so that we know later on that if self has this value
195 // then it is properly initialized.
196
197 // FIXME: A callback should disable checkers at the start of functions.
198 if (!shouldRunOnFunctionOrMethod(dyn_cast<NamedDecl>(
199 C.getCurrentAnalysisContext()->getDecl())))
200 return;
201
Argyrios Kyrtzidis432424d2011-01-25 00:03:53 +0000202 if (isInitMessage(msg)) {
Argyrios Kyrtzidisd7a31ba2011-01-11 19:45:25 +0000203 // Tag the return value as the result of an initializer.
204 const GRState *state = C.getState();
Ted Kremenekb715a7c2011-02-12 03:03:54 +0000205
206 // FIXME this really should be context sensitive, where we record
207 // the current stack frame (for IPA). Also, we need to clean this
208 // value out when we return from this method.
209 state = state->set<CalledInit>(true);
210
Argyrios Kyrtzidis432424d2011-01-25 00:03:53 +0000211 SVal V = state->getSVal(msg.getOriginExpr());
Ted Kremenekb715a7c2011-02-12 03:03:54 +0000212 addSelfFlag(state, V, SelfFlag_InitRes, C);
Argyrios Kyrtzidisd7a31ba2011-01-11 19:45:25 +0000213 return;
214 }
215
216 // We don't check for an invalid 'self' in an obj-c message expression to cut
217 // down false positives where logging functions get information from self
218 // (like its class) or doing "invalidation" on self when the initialization
219 // fails.
220}
221
222void ObjCSelfInitChecker::PostVisitObjCIvarRefExpr(CheckerContext &C,
223 const ObjCIvarRefExpr *E) {
224 // FIXME: A callback should disable checkers at the start of functions.
225 if (!shouldRunOnFunctionOrMethod(dyn_cast<NamedDecl>(
226 C.getCurrentAnalysisContext()->getDecl())))
227 return;
228
229 checkForInvalidSelf(E->getBase(), C,
Argyrios Kyrtzidisbe29d8d2011-02-01 19:32:55 +0000230 "Instance variable used while 'self' is not set to the result of "
Argyrios Kyrtzidis4717f162011-01-26 01:26:41 +0000231 "'[(super or self) init...]'");
Argyrios Kyrtzidisd7a31ba2011-01-11 19:45:25 +0000232}
233
234void ObjCSelfInitChecker::PreVisitReturnStmt(CheckerContext &C,
235 const ReturnStmt *S) {
236 // FIXME: A callback should disable checkers at the start of functions.
237 if (!shouldRunOnFunctionOrMethod(dyn_cast<NamedDecl>(
238 C.getCurrentAnalysisContext()->getDecl())))
239 return;
240
241 checkForInvalidSelf(S->getRetValue(), C,
Argyrios Kyrtzidis63eeade2011-02-01 20:33:05 +0000242 "Returning 'self' while it is not set to the result of "
Argyrios Kyrtzidis4717f162011-01-26 01:26:41 +0000243 "'[(super or self) init...]'");
Argyrios Kyrtzidisd7a31ba2011-01-11 19:45:25 +0000244}
245
246// When a call receives a reference to 'self', [Pre/Post]VisitGenericCall pass
247// the SelfFlags from the object 'self' point to before the call, to the new
Argyrios Kyrtzidis0ca10402011-02-05 05:54:53 +0000248// object after the call. This is to avoid invalidation of 'self' by logging
249// functions.
250// Another common pattern in classes with multiple initializers is to put the
251// subclass's common initialization bits into a static function that receives
252// the value of 'self', e.g:
253// @code
254// if (!(self = [super init]))
255// return nil;
256// if (!(self = _commonInit(self)))
257// return nil;
258// @endcode
259// Until we can use inter-procedural analysis, in such a call, transfer the
260// SelfFlags to the result of the call.
Argyrios Kyrtzidisd7a31ba2011-01-11 19:45:25 +0000261
262void ObjCSelfInitChecker::PreVisitGenericCall(CheckerContext &C,
263 const CallExpr *CE) {
264 const GRState *state = C.getState();
265 for (CallExpr::const_arg_iterator
266 I = CE->arg_begin(), E = CE->arg_end(); I != E; ++I) {
267 SVal argV = state->getSVal(*I);
268 if (isSelfVar(argV, C)) {
269 preCallSelfFlags = getSelfFlags(state->getSVal(cast<Loc>(argV)), C);
270 return;
Argyrios Kyrtzidis0ca10402011-02-05 05:54:53 +0000271 } else if (hasSelfFlag(argV, SelfFlag_Self, C)) {
272 preCallSelfFlags = getSelfFlags(argV, C);
273 return;
Argyrios Kyrtzidisd7a31ba2011-01-11 19:45:25 +0000274 }
275 }
276}
277
278void ObjCSelfInitChecker::PostVisitGenericCall(CheckerContext &C,
279 const CallExpr *CE) {
280 const GRState *state = C.getState();
281 for (CallExpr::const_arg_iterator
282 I = CE->arg_begin(), E = CE->arg_end(); I != E; ++I) {
283 SVal argV = state->getSVal(*I);
284 if (isSelfVar(argV, C)) {
Ted Kremenekb715a7c2011-02-12 03:03:54 +0000285 addSelfFlag(state, state->getSVal(cast<Loc>(argV)), preCallSelfFlags, C);
Argyrios Kyrtzidisd7a31ba2011-01-11 19:45:25 +0000286 return;
Argyrios Kyrtzidis0ca10402011-02-05 05:54:53 +0000287 } else if (hasSelfFlag(argV, SelfFlag_Self, C)) {
Ted Kremenekb715a7c2011-02-12 03:03:54 +0000288 addSelfFlag(state, state->getSVal(CE), preCallSelfFlags, C);
Argyrios Kyrtzidis0ca10402011-02-05 05:54:53 +0000289 return;
Argyrios Kyrtzidisd7a31ba2011-01-11 19:45:25 +0000290 }
291 }
292}
293
294void ObjCSelfInitChecker::visitLocation(CheckerContext &C, const Stmt *S,
295 SVal location, bool isLoad) {
296 // Tag the result of a load from 'self' so that we can easily know that the
297 // value is the object that 'self' points to.
298 const GRState *state = C.getState();
299 if (isSelfVar(location, C))
Ted Kremenekb715a7c2011-02-12 03:03:54 +0000300 addSelfFlag(state, state->getSVal(cast<Loc>(location)), SelfFlag_Self, C);
Argyrios Kyrtzidisd7a31ba2011-01-11 19:45:25 +0000301}
302
303// FIXME: A callback should disable checkers at the start of functions.
304static bool shouldRunOnFunctionOrMethod(const NamedDecl *ND) {
305 if (!ND)
306 return false;
307
308 const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(ND);
309 if (!MD)
310 return false;
Argyrios Kyrtzidisd7a31ba2011-01-11 19:45:25 +0000311 if (!isInitializationMethod(MD))
312 return false;
313
Argyrios Kyrtzidiseaf969b2011-01-25 23:54:44 +0000314 // self = [super init] applies only to NSObject subclasses.
315 // For instance, NSProxy doesn't implement -init.
316 ASTContext& Ctx = MD->getASTContext();
317 IdentifierInfo* NSObjectII = &Ctx.Idents.get("NSObject");
318 ObjCInterfaceDecl* ID = MD->getClassInterface()->getSuperClass();
319 for ( ; ID ; ID = ID->getSuperClass()) {
320 IdentifierInfo *II = ID->getIdentifier();
321
322 if (II == NSObjectII)
323 break;
324 }
325 if (!ID)
326 return false;
327
Argyrios Kyrtzidisd7a31ba2011-01-11 19:45:25 +0000328 return true;
329}
330
331/// \brief Returns true if the location is 'self'.
332static bool isSelfVar(SVal location, CheckerContext &C) {
333 AnalysisContext *analCtx = C.getCurrentAnalysisContext();
334 if (!analCtx->getSelfDecl())
335 return false;
336 if (!isa<loc::MemRegionVal>(location))
337 return false;
338
339 loc::MemRegionVal MRV = cast<loc::MemRegionVal>(location);
340 if (const DeclRegion *DR = dyn_cast<DeclRegion>(MRV.getRegion()))
341 return (DR->getDecl() == analCtx->getSelfDecl());
342
343 return false;
344}
345
346static bool isInitializationMethod(const ObjCMethodDecl *MD) {
347 // Init methods with prefix like '-(id)_init' are private and the requirements
348 // are less strict so we don't check those.
349 return MD->isInstanceMethod() &&
350 cocoa::deriveNamingConvention(MD->getSelector(),
351 /*ignorePrefix=*/false) == cocoa::InitRule;
352}
353
Argyrios Kyrtzidis432424d2011-01-25 00:03:53 +0000354static bool isInitMessage(const ObjCMessage &msg) {
355 return cocoa::deriveNamingConvention(msg.getSelector()) == cocoa::InitRule;
Argyrios Kyrtzidisd7a31ba2011-01-11 19:45:25 +0000356}