Argyrios Kyrtzidis | d7a31ba | 2011-01-11 19:45:25 +0000 | [diff] [blame] | 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 | // |
Chris Lattner | fc8f0e1 | 2011-04-15 05:22:18 +0000 | [diff] [blame] | 19 | // To perform the required checking, values are tagged with flags that indicate |
Argyrios Kyrtzidis | d7a31ba | 2011-01-11 19:45:25 +0000 | [diff] [blame] | 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 | // |
Argyrios Kyrtzidis | d7a31ba | 2011-01-11 19:45:25 +0000 | [diff] [blame] | 37 | //===----------------------------------------------------------------------===// |
| 38 | |
Argyrios Kyrtzidis | 027a6ab | 2011-02-15 07:42:33 +0000 | [diff] [blame] | 39 | #include "ClangSACheckers.h" |
Argyrios Kyrtzidis | ec8605f | 2011-03-01 01:16:21 +0000 | [diff] [blame] | 40 | #include "clang/StaticAnalyzer/Core/Checker.h" |
Argyrios Kyrtzidis | 695fb50 | 2011-02-17 21:39:17 +0000 | [diff] [blame] | 41 | #include "clang/StaticAnalyzer/Core/CheckerManager.h" |
Argyrios Kyrtzidis | 769ce3e | 2011-02-22 17:30:38 +0000 | [diff] [blame] | 42 | #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h" |
Ted Kremenek | 18c66fd | 2011-08-15 22:09:50 +0000 | [diff] [blame] | 43 | #include "clang/StaticAnalyzer/Core/PathSensitive/ProgramStateTrait.h" |
Jordy Rose | d1e5a89 | 2011-09-02 08:02:59 +0000 | [diff] [blame] | 44 | #include "clang/StaticAnalyzer/Core/PathSensitive/ObjCMessage.h" |
Ted Kremenek | 9b66371 | 2011-02-10 01:03:03 +0000 | [diff] [blame] | 45 | #include "clang/StaticAnalyzer/Core/BugReporter/BugType.h" |
Argyrios Kyrtzidis | d7a31ba | 2011-01-11 19:45:25 +0000 | [diff] [blame] | 46 | #include "clang/AST/ParentMap.h" |
| 47 | |
| 48 | using namespace clang; |
| 49 | using namespace ento; |
| 50 | |
| 51 | static bool shouldRunOnFunctionOrMethod(const NamedDecl *ND); |
| 52 | static bool isInitializationMethod(const ObjCMethodDecl *MD); |
Argyrios Kyrtzidis | 432424d | 2011-01-25 00:03:53 +0000 | [diff] [blame] | 53 | static bool isInitMessage(const ObjCMessage &msg); |
Argyrios Kyrtzidis | d7a31ba | 2011-01-11 19:45:25 +0000 | [diff] [blame] | 54 | static bool isSelfVar(SVal location, CheckerContext &C); |
| 55 | |
| 56 | namespace { |
Anna Zaks | f420fe3 | 2012-03-05 18:58:25 +0000 | [diff] [blame^] | 57 | class ObjCSelfInitChecker : public Checker< check::PreObjCMessage, |
Argyrios Kyrtzidis | 769ce3e | 2011-02-22 17:30:38 +0000 | [diff] [blame] | 58 | check::PostObjCMessage, |
| 59 | check::PostStmt<ObjCIvarRefExpr>, |
| 60 | check::PreStmt<ReturnStmt>, |
| 61 | check::PreStmt<CallExpr>, |
| 62 | check::PostStmt<CallExpr>, |
| 63 | check::Location > { |
Argyrios Kyrtzidis | d7a31ba | 2011-01-11 19:45:25 +0000 | [diff] [blame] | 64 | public: |
Anna Zaks | f420fe3 | 2012-03-05 18:58:25 +0000 | [diff] [blame^] | 65 | void checkPreObjCMessage(ObjCMessage msg, CheckerContext &C) const; |
Argyrios Kyrtzidis | 769ce3e | 2011-02-22 17:30:38 +0000 | [diff] [blame] | 66 | void checkPostObjCMessage(ObjCMessage msg, CheckerContext &C) const; |
| 67 | void checkPostStmt(const ObjCIvarRefExpr *E, CheckerContext &C) const; |
| 68 | void checkPreStmt(const ReturnStmt *S, CheckerContext &C) const; |
| 69 | void checkPreStmt(const CallExpr *CE, CheckerContext &C) const; |
| 70 | void checkPostStmt(const CallExpr *CE, CheckerContext &C) const; |
Anna Zaks | 390909c | 2011-10-06 00:43:15 +0000 | [diff] [blame] | 71 | void checkLocation(SVal location, bool isLoad, const Stmt *S, |
| 72 | CheckerContext &C) const; |
Anna Zaks | f420fe3 | 2012-03-05 18:58:25 +0000 | [diff] [blame^] | 73 | |
| 74 | void checkPreStmt(const CallOrObjCMessage &CE, CheckerContext &C) const; |
| 75 | void checkPostStmt(const CallOrObjCMessage &CE, CheckerContext &C) const; |
| 76 | |
Argyrios Kyrtzidis | d7a31ba | 2011-01-11 19:45:25 +0000 | [diff] [blame] | 77 | }; |
| 78 | } // end anonymous namespace |
| 79 | |
Argyrios Kyrtzidis | d7a31ba | 2011-01-11 19:45:25 +0000 | [diff] [blame] | 80 | namespace { |
| 81 | |
| 82 | class InitSelfBug : public BugType { |
| 83 | const std::string desc; |
| 84 | public: |
Anna Zaks | 1efcc42 | 2012-02-04 02:31:37 +0000 | [diff] [blame] | 85 | InitSelfBug() : BugType("Missing \"self = [(super or self) init...]\"", |
| 86 | "Core Foundation/Objective-C") {} |
Argyrios Kyrtzidis | d7a31ba | 2011-01-11 19:45:25 +0000 | [diff] [blame] | 87 | }; |
| 88 | |
| 89 | } // end anonymous namespace |
| 90 | |
Argyrios Kyrtzidis | 769ce3e | 2011-02-22 17:30:38 +0000 | [diff] [blame] | 91 | namespace { |
| 92 | enum SelfFlagEnum { |
| 93 | /// \brief No flag set. |
| 94 | SelfFlag_None = 0x0, |
| 95 | /// \brief Value came from 'self'. |
| 96 | SelfFlag_Self = 0x1, |
| 97 | /// \brief Value came from the result of an initializer (e.g. [super init]). |
| 98 | SelfFlag_InitRes = 0x2 |
| 99 | }; |
| 100 | } |
| 101 | |
Argyrios Kyrtzidis | d7a31ba | 2011-01-11 19:45:25 +0000 | [diff] [blame] | 102 | typedef llvm::ImmutableMap<SymbolRef, unsigned> SelfFlag; |
Ted Kremenek | b715a7c | 2011-02-12 03:03:54 +0000 | [diff] [blame] | 103 | namespace { struct CalledInit {}; } |
Argyrios Kyrtzidis | 769ce3e | 2011-02-22 17:30:38 +0000 | [diff] [blame] | 104 | namespace { struct PreCallSelfFlags {}; } |
Argyrios Kyrtzidis | d7a31ba | 2011-01-11 19:45:25 +0000 | [diff] [blame] | 105 | |
| 106 | namespace clang { |
| 107 | namespace ento { |
| 108 | template<> |
Ted Kremenek | 18c66fd | 2011-08-15 22:09:50 +0000 | [diff] [blame] | 109 | struct ProgramStateTrait<SelfFlag> : public ProgramStatePartialTrait<SelfFlag> { |
Ted Kremenek | 9c378f7 | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 110 | static void *GDMIndex() { static int index = 0; return &index; } |
Argyrios Kyrtzidis | d7a31ba | 2011-01-11 19:45:25 +0000 | [diff] [blame] | 111 | }; |
Ted Kremenek | b715a7c | 2011-02-12 03:03:54 +0000 | [diff] [blame] | 112 | template <> |
Ted Kremenek | 18c66fd | 2011-08-15 22:09:50 +0000 | [diff] [blame] | 113 | struct ProgramStateTrait<CalledInit> : public ProgramStatePartialTrait<bool> { |
Ted Kremenek | b715a7c | 2011-02-12 03:03:54 +0000 | [diff] [blame] | 114 | static void *GDMIndex() { static int index = 0; return &index; } |
| 115 | }; |
Argyrios Kyrtzidis | 769ce3e | 2011-02-22 17:30:38 +0000 | [diff] [blame] | 116 | |
| 117 | /// \brief A call receiving a reference to 'self' invalidates the object that |
| 118 | /// 'self' contains. This keeps the "self flags" assigned to the 'self' |
| 119 | /// object before the call so we can assign them to the new object that 'self' |
| 120 | /// points to after the call. |
| 121 | template <> |
Ted Kremenek | 18c66fd | 2011-08-15 22:09:50 +0000 | [diff] [blame] | 122 | struct ProgramStateTrait<PreCallSelfFlags> : public ProgramStatePartialTrait<unsigned> { |
Argyrios Kyrtzidis | 769ce3e | 2011-02-22 17:30:38 +0000 | [diff] [blame] | 123 | static void *GDMIndex() { static int index = 0; return &index; } |
| 124 | }; |
Argyrios Kyrtzidis | d7a31ba | 2011-01-11 19:45:25 +0000 | [diff] [blame] | 125 | } |
| 126 | } |
| 127 | |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 128 | static SelfFlagEnum getSelfFlags(SVal val, ProgramStateRef state) { |
Argyrios Kyrtzidis | d7a31ba | 2011-01-11 19:45:25 +0000 | [diff] [blame] | 129 | if (SymbolRef sym = val.getAsSymbol()) |
| 130 | if (const unsigned *attachedFlags = state->get<SelfFlag>(sym)) |
| 131 | return (SelfFlagEnum)*attachedFlags; |
| 132 | return SelfFlag_None; |
| 133 | } |
| 134 | |
| 135 | static SelfFlagEnum getSelfFlags(SVal val, CheckerContext &C) { |
| 136 | return getSelfFlags(val, C.getState()); |
| 137 | } |
| 138 | |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 139 | static void addSelfFlag(ProgramStateRef state, SVal val, |
Ted Kremenek | b715a7c | 2011-02-12 03:03:54 +0000 | [diff] [blame] | 140 | SelfFlagEnum flag, CheckerContext &C) { |
Argyrios Kyrtzidis | 0ca1040 | 2011-02-05 05:54:53 +0000 | [diff] [blame] | 141 | // We tag the symbol that the SVal wraps. |
Argyrios Kyrtzidis | d7a31ba | 2011-01-11 19:45:25 +0000 | [diff] [blame] | 142 | if (SymbolRef sym = val.getAsSymbol()) |
Anna Zaks | 0bd6b11 | 2011-10-26 21:06:34 +0000 | [diff] [blame] | 143 | C.addTransition(state->set<SelfFlag>(sym, getSelfFlags(val, C) | flag)); |
Argyrios Kyrtzidis | d7a31ba | 2011-01-11 19:45:25 +0000 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | static bool hasSelfFlag(SVal val, SelfFlagEnum flag, CheckerContext &C) { |
| 147 | return getSelfFlags(val, C) & flag; |
| 148 | } |
| 149 | |
| 150 | /// \brief Returns true of the value of the expression is the object that 'self' |
| 151 | /// points to and is an object that did not come from the result of calling |
| 152 | /// an initializer. |
| 153 | static bool isInvalidSelf(const Expr *E, CheckerContext &C) { |
Ted Kremenek | 5eca482 | 2012-01-06 22:09:28 +0000 | [diff] [blame] | 154 | SVal exprVal = C.getState()->getSVal(E, C.getLocationContext()); |
Argyrios Kyrtzidis | d7a31ba | 2011-01-11 19:45:25 +0000 | [diff] [blame] | 155 | if (!hasSelfFlag(exprVal, SelfFlag_Self, C)) |
| 156 | return false; // value did not come from 'self'. |
| 157 | if (hasSelfFlag(exprVal, SelfFlag_InitRes, C)) |
| 158 | return false; // 'self' is properly initialized. |
| 159 | |
| 160 | return true; |
| 161 | } |
| 162 | |
| 163 | static void checkForInvalidSelf(const Expr *E, CheckerContext &C, |
| 164 | const char *errorStr) { |
| 165 | if (!E) |
| 166 | return; |
Ted Kremenek | b715a7c | 2011-02-12 03:03:54 +0000 | [diff] [blame] | 167 | |
| 168 | if (!C.getState()->get<CalledInit>()) |
| 169 | return; |
| 170 | |
Argyrios Kyrtzidis | d7a31ba | 2011-01-11 19:45:25 +0000 | [diff] [blame] | 171 | if (!isInvalidSelf(E, C)) |
| 172 | return; |
Ted Kremenek | b715a7c | 2011-02-12 03:03:54 +0000 | [diff] [blame] | 173 | |
Argyrios Kyrtzidis | d7a31ba | 2011-01-11 19:45:25 +0000 | [diff] [blame] | 174 | // Generate an error node. |
| 175 | ExplodedNode *N = C.generateSink(); |
| 176 | if (!N) |
| 177 | return; |
| 178 | |
Anna Zaks | e172e8b | 2011-08-17 23:00:25 +0000 | [diff] [blame] | 179 | BugReport *report = |
| 180 | new BugReport(*new InitSelfBug(), errorStr, N); |
Argyrios Kyrtzidis | d7a31ba | 2011-01-11 19:45:25 +0000 | [diff] [blame] | 181 | C.EmitReport(report); |
| 182 | } |
| 183 | |
Argyrios Kyrtzidis | 769ce3e | 2011-02-22 17:30:38 +0000 | [diff] [blame] | 184 | void ObjCSelfInitChecker::checkPostObjCMessage(ObjCMessage msg, |
| 185 | CheckerContext &C) const { |
Anna Zaks | f420fe3 | 2012-03-05 18:58:25 +0000 | [diff] [blame^] | 186 | CallOrObjCMessage MsgWrapper(msg, C.getState(), C.getLocationContext()); |
| 187 | checkPostStmt(MsgWrapper, C); |
| 188 | |
Argyrios Kyrtzidis | d7a31ba | 2011-01-11 19:45:25 +0000 | [diff] [blame] | 189 | // When encountering a message that does initialization (init rule), |
| 190 | // tag the return value so that we know later on that if self has this value |
| 191 | // then it is properly initialized. |
| 192 | |
| 193 | // FIXME: A callback should disable checkers at the start of functions. |
| 194 | if (!shouldRunOnFunctionOrMethod(dyn_cast<NamedDecl>( |
Anna Zaks | 1efcc42 | 2012-02-04 02:31:37 +0000 | [diff] [blame] | 195 | C.getCurrentAnalysisDeclContext()->getDecl()))) |
Argyrios Kyrtzidis | d7a31ba | 2011-01-11 19:45:25 +0000 | [diff] [blame] | 196 | return; |
| 197 | |
Argyrios Kyrtzidis | 432424d | 2011-01-25 00:03:53 +0000 | [diff] [blame] | 198 | if (isInitMessage(msg)) { |
Argyrios Kyrtzidis | d7a31ba | 2011-01-11 19:45:25 +0000 | [diff] [blame] | 199 | // Tag the return value as the result of an initializer. |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 200 | ProgramStateRef state = C.getState(); |
Ted Kremenek | b715a7c | 2011-02-12 03:03:54 +0000 | [diff] [blame] | 201 | |
| 202 | // FIXME this really should be context sensitive, where we record |
| 203 | // the current stack frame (for IPA). Also, we need to clean this |
| 204 | // value out when we return from this method. |
| 205 | state = state->set<CalledInit>(true); |
| 206 | |
Ted Kremenek | b673a41 | 2012-02-18 20:53:30 +0000 | [diff] [blame] | 207 | SVal V = state->getSVal(msg.getMessageExpr(), C.getLocationContext()); |
Ted Kremenek | b715a7c | 2011-02-12 03:03:54 +0000 | [diff] [blame] | 208 | addSelfFlag(state, V, SelfFlag_InitRes, C); |
Argyrios Kyrtzidis | d7a31ba | 2011-01-11 19:45:25 +0000 | [diff] [blame] | 209 | return; |
| 210 | } |
| 211 | |
| 212 | // We don't check for an invalid 'self' in an obj-c message expression to cut |
| 213 | // down false positives where logging functions get information from self |
| 214 | // (like its class) or doing "invalidation" on self when the initialization |
| 215 | // fails. |
| 216 | } |
| 217 | |
Argyrios Kyrtzidis | 769ce3e | 2011-02-22 17:30:38 +0000 | [diff] [blame] | 218 | void ObjCSelfInitChecker::checkPostStmt(const ObjCIvarRefExpr *E, |
| 219 | CheckerContext &C) const { |
Argyrios Kyrtzidis | d7a31ba | 2011-01-11 19:45:25 +0000 | [diff] [blame] | 220 | // FIXME: A callback should disable checkers at the start of functions. |
| 221 | if (!shouldRunOnFunctionOrMethod(dyn_cast<NamedDecl>( |
Anna Zaks | 1efcc42 | 2012-02-04 02:31:37 +0000 | [diff] [blame] | 222 | C.getCurrentAnalysisDeclContext()->getDecl()))) |
Argyrios Kyrtzidis | d7a31ba | 2011-01-11 19:45:25 +0000 | [diff] [blame] | 223 | return; |
| 224 | |
| 225 | checkForInvalidSelf(E->getBase(), C, |
Argyrios Kyrtzidis | be29d8d | 2011-02-01 19:32:55 +0000 | [diff] [blame] | 226 | "Instance variable used while 'self' is not set to the result of " |
Argyrios Kyrtzidis | 4717f16 | 2011-01-26 01:26:41 +0000 | [diff] [blame] | 227 | "'[(super or self) init...]'"); |
Argyrios Kyrtzidis | d7a31ba | 2011-01-11 19:45:25 +0000 | [diff] [blame] | 228 | } |
| 229 | |
Argyrios Kyrtzidis | 769ce3e | 2011-02-22 17:30:38 +0000 | [diff] [blame] | 230 | void ObjCSelfInitChecker::checkPreStmt(const ReturnStmt *S, |
| 231 | CheckerContext &C) const { |
Argyrios Kyrtzidis | d7a31ba | 2011-01-11 19:45:25 +0000 | [diff] [blame] | 232 | // FIXME: A callback should disable checkers at the start of functions. |
| 233 | if (!shouldRunOnFunctionOrMethod(dyn_cast<NamedDecl>( |
Anna Zaks | 1efcc42 | 2012-02-04 02:31:37 +0000 | [diff] [blame] | 234 | C.getCurrentAnalysisDeclContext()->getDecl()))) |
Argyrios Kyrtzidis | d7a31ba | 2011-01-11 19:45:25 +0000 | [diff] [blame] | 235 | return; |
| 236 | |
| 237 | checkForInvalidSelf(S->getRetValue(), C, |
Argyrios Kyrtzidis | 63eeade | 2011-02-01 20:33:05 +0000 | [diff] [blame] | 238 | "Returning 'self' while it is not set to the result of " |
Argyrios Kyrtzidis | 4717f16 | 2011-01-26 01:26:41 +0000 | [diff] [blame] | 239 | "'[(super or self) init...]'"); |
Argyrios Kyrtzidis | d7a31ba | 2011-01-11 19:45:25 +0000 | [diff] [blame] | 240 | } |
| 241 | |
| 242 | // When a call receives a reference to 'self', [Pre/Post]VisitGenericCall pass |
| 243 | // the SelfFlags from the object 'self' point to before the call, to the new |
Argyrios Kyrtzidis | 0ca1040 | 2011-02-05 05:54:53 +0000 | [diff] [blame] | 244 | // object after the call. This is to avoid invalidation of 'self' by logging |
| 245 | // functions. |
| 246 | // Another common pattern in classes with multiple initializers is to put the |
| 247 | // subclass's common initialization bits into a static function that receives |
| 248 | // the value of 'self', e.g: |
| 249 | // @code |
| 250 | // if (!(self = [super init])) |
| 251 | // return nil; |
| 252 | // if (!(self = _commonInit(self))) |
| 253 | // return nil; |
| 254 | // @endcode |
| 255 | // Until we can use inter-procedural analysis, in such a call, transfer the |
| 256 | // SelfFlags to the result of the call. |
Argyrios Kyrtzidis | d7a31ba | 2011-01-11 19:45:25 +0000 | [diff] [blame] | 257 | |
Argyrios Kyrtzidis | 769ce3e | 2011-02-22 17:30:38 +0000 | [diff] [blame] | 258 | void ObjCSelfInitChecker::checkPreStmt(const CallExpr *CE, |
| 259 | CheckerContext &C) const { |
Anna Zaks | f420fe3 | 2012-03-05 18:58:25 +0000 | [diff] [blame^] | 260 | CallOrObjCMessage CEWrapper(CE, C.getState(), C.getLocationContext()); |
| 261 | checkPreStmt(CEWrapper, C); |
| 262 | } |
| 263 | |
| 264 | void ObjCSelfInitChecker::checkPostStmt(const CallExpr *CE, |
| 265 | CheckerContext &C) const { |
| 266 | CallOrObjCMessage CEWrapper(CE, C.getState(), C.getLocationContext()); |
| 267 | checkPostStmt(CEWrapper, C); |
| 268 | } |
| 269 | |
| 270 | void ObjCSelfInitChecker::checkPreObjCMessage(ObjCMessage Msg, |
| 271 | CheckerContext &C) const { |
| 272 | CallOrObjCMessage MsgWrapper(Msg, C.getState(), C.getLocationContext()); |
| 273 | checkPreStmt(MsgWrapper, C); |
| 274 | } |
| 275 | |
| 276 | void ObjCSelfInitChecker::checkPreStmt(const CallOrObjCMessage &CE, |
| 277 | CheckerContext &C) const { |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 278 | ProgramStateRef state = C.getState(); |
Anna Zaks | f420fe3 | 2012-03-05 18:58:25 +0000 | [diff] [blame^] | 279 | unsigned NumArgs = CE.getNumArgs(); |
| 280 | for (unsigned i = 0; i < NumArgs; ++i) { |
| 281 | SVal argV = CE.getArgSVal(i); |
Argyrios Kyrtzidis | d7a31ba | 2011-01-11 19:45:25 +0000 | [diff] [blame] | 282 | if (isSelfVar(argV, C)) { |
Argyrios Kyrtzidis | 769ce3e | 2011-02-22 17:30:38 +0000 | [diff] [blame] | 283 | unsigned selfFlags = getSelfFlags(state->getSVal(cast<Loc>(argV)), C); |
Anna Zaks | 0bd6b11 | 2011-10-26 21:06:34 +0000 | [diff] [blame] | 284 | C.addTransition(state->set<PreCallSelfFlags>(selfFlags)); |
Argyrios Kyrtzidis | d7a31ba | 2011-01-11 19:45:25 +0000 | [diff] [blame] | 285 | return; |
Argyrios Kyrtzidis | 0ca1040 | 2011-02-05 05:54:53 +0000 | [diff] [blame] | 286 | } else if (hasSelfFlag(argV, SelfFlag_Self, C)) { |
Argyrios Kyrtzidis | 769ce3e | 2011-02-22 17:30:38 +0000 | [diff] [blame] | 287 | unsigned selfFlags = getSelfFlags(argV, C); |
Anna Zaks | 0bd6b11 | 2011-10-26 21:06:34 +0000 | [diff] [blame] | 288 | C.addTransition(state->set<PreCallSelfFlags>(selfFlags)); |
Argyrios Kyrtzidis | 0ca1040 | 2011-02-05 05:54:53 +0000 | [diff] [blame] | 289 | return; |
Argyrios Kyrtzidis | d7a31ba | 2011-01-11 19:45:25 +0000 | [diff] [blame] | 290 | } |
| 291 | } |
| 292 | } |
| 293 | |
Anna Zaks | f420fe3 | 2012-03-05 18:58:25 +0000 | [diff] [blame^] | 294 | void ObjCSelfInitChecker::checkPostStmt(const CallOrObjCMessage &CE, |
Argyrios Kyrtzidis | 769ce3e | 2011-02-22 17:30:38 +0000 | [diff] [blame] | 295 | CheckerContext &C) const { |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 296 | ProgramStateRef state = C.getState(); |
Anna Zaks | f420fe3 | 2012-03-05 18:58:25 +0000 | [diff] [blame^] | 297 | unsigned NumArgs = CE.getNumArgs(); |
| 298 | for (unsigned i = 0; i < NumArgs; ++i) { |
| 299 | SVal argV = CE.getArgSVal(i); |
Argyrios Kyrtzidis | d7a31ba | 2011-01-11 19:45:25 +0000 | [diff] [blame] | 300 | if (isSelfVar(argV, C)) { |
Argyrios Kyrtzidis | 769ce3e | 2011-02-22 17:30:38 +0000 | [diff] [blame] | 301 | SelfFlagEnum prevFlags = (SelfFlagEnum)state->get<PreCallSelfFlags>(); |
| 302 | state = state->remove<PreCallSelfFlags>(); |
| 303 | addSelfFlag(state, state->getSVal(cast<Loc>(argV)), prevFlags, C); |
Argyrios Kyrtzidis | d7a31ba | 2011-01-11 19:45:25 +0000 | [diff] [blame] | 304 | return; |
Argyrios Kyrtzidis | 0ca1040 | 2011-02-05 05:54:53 +0000 | [diff] [blame] | 305 | } else if (hasSelfFlag(argV, SelfFlag_Self, C)) { |
Argyrios Kyrtzidis | 769ce3e | 2011-02-22 17:30:38 +0000 | [diff] [blame] | 306 | SelfFlagEnum prevFlags = (SelfFlagEnum)state->get<PreCallSelfFlags>(); |
| 307 | state = state->remove<PreCallSelfFlags>(); |
Anna Zaks | f420fe3 | 2012-03-05 18:58:25 +0000 | [diff] [blame^] | 308 | addSelfFlag(state, state->getSVal(cast<Loc>(argV)), prevFlags, C); |
Argyrios Kyrtzidis | 0ca1040 | 2011-02-05 05:54:53 +0000 | [diff] [blame] | 309 | return; |
Argyrios Kyrtzidis | d7a31ba | 2011-01-11 19:45:25 +0000 | [diff] [blame] | 310 | } |
| 311 | } |
| 312 | } |
| 313 | |
Argyrios Kyrtzidis | 769ce3e | 2011-02-22 17:30:38 +0000 | [diff] [blame] | 314 | void ObjCSelfInitChecker::checkLocation(SVal location, bool isLoad, |
Anna Zaks | 390909c | 2011-10-06 00:43:15 +0000 | [diff] [blame] | 315 | const Stmt *S, |
Argyrios Kyrtzidis | 769ce3e | 2011-02-22 17:30:38 +0000 | [diff] [blame] | 316 | CheckerContext &C) const { |
Argyrios Kyrtzidis | d7a31ba | 2011-01-11 19:45:25 +0000 | [diff] [blame] | 317 | // Tag the result of a load from 'self' so that we can easily know that the |
| 318 | // value is the object that 'self' points to. |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 319 | ProgramStateRef state = C.getState(); |
Argyrios Kyrtzidis | d7a31ba | 2011-01-11 19:45:25 +0000 | [diff] [blame] | 320 | if (isSelfVar(location, C)) |
Ted Kremenek | b715a7c | 2011-02-12 03:03:54 +0000 | [diff] [blame] | 321 | addSelfFlag(state, state->getSVal(cast<Loc>(location)), SelfFlag_Self, C); |
Argyrios Kyrtzidis | d7a31ba | 2011-01-11 19:45:25 +0000 | [diff] [blame] | 322 | } |
| 323 | |
| 324 | // FIXME: A callback should disable checkers at the start of functions. |
| 325 | static bool shouldRunOnFunctionOrMethod(const NamedDecl *ND) { |
| 326 | if (!ND) |
| 327 | return false; |
| 328 | |
| 329 | const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(ND); |
| 330 | if (!MD) |
| 331 | return false; |
Argyrios Kyrtzidis | d7a31ba | 2011-01-11 19:45:25 +0000 | [diff] [blame] | 332 | if (!isInitializationMethod(MD)) |
| 333 | return false; |
| 334 | |
Argyrios Kyrtzidis | eaf969b | 2011-01-25 23:54:44 +0000 | [diff] [blame] | 335 | // self = [super init] applies only to NSObject subclasses. |
| 336 | // For instance, NSProxy doesn't implement -init. |
Ted Kremenek | 9c378f7 | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 337 | ASTContext &Ctx = MD->getASTContext(); |
Argyrios Kyrtzidis | eaf969b | 2011-01-25 23:54:44 +0000 | [diff] [blame] | 338 | IdentifierInfo* NSObjectII = &Ctx.Idents.get("NSObject"); |
Ted Kremenek | 9c378f7 | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 339 | ObjCInterfaceDecl *ID = MD->getClassInterface()->getSuperClass(); |
Argyrios Kyrtzidis | eaf969b | 2011-01-25 23:54:44 +0000 | [diff] [blame] | 340 | for ( ; ID ; ID = ID->getSuperClass()) { |
| 341 | IdentifierInfo *II = ID->getIdentifier(); |
| 342 | |
| 343 | if (II == NSObjectII) |
| 344 | break; |
| 345 | } |
| 346 | if (!ID) |
| 347 | return false; |
| 348 | |
Argyrios Kyrtzidis | d7a31ba | 2011-01-11 19:45:25 +0000 | [diff] [blame] | 349 | return true; |
| 350 | } |
| 351 | |
| 352 | /// \brief Returns true if the location is 'self'. |
| 353 | static bool isSelfVar(SVal location, CheckerContext &C) { |
Ted Kremenek | 1d26f48 | 2011-10-24 01:32:45 +0000 | [diff] [blame] | 354 | AnalysisDeclContext *analCtx = C.getCurrentAnalysisDeclContext(); |
Argyrios Kyrtzidis | d7a31ba | 2011-01-11 19:45:25 +0000 | [diff] [blame] | 355 | if (!analCtx->getSelfDecl()) |
| 356 | return false; |
| 357 | if (!isa<loc::MemRegionVal>(location)) |
| 358 | return false; |
| 359 | |
| 360 | loc::MemRegionVal MRV = cast<loc::MemRegionVal>(location); |
| 361 | if (const DeclRegion *DR = dyn_cast<DeclRegion>(MRV.getRegion())) |
| 362 | return (DR->getDecl() == analCtx->getSelfDecl()); |
| 363 | |
| 364 | return false; |
| 365 | } |
| 366 | |
| 367 | static bool isInitializationMethod(const ObjCMethodDecl *MD) { |
John McCall | 85f3d76 | 2011-03-02 01:50:55 +0000 | [diff] [blame] | 368 | return MD->getMethodFamily() == OMF_init; |
Argyrios Kyrtzidis | d7a31ba | 2011-01-11 19:45:25 +0000 | [diff] [blame] | 369 | } |
| 370 | |
Argyrios Kyrtzidis | 432424d | 2011-01-25 00:03:53 +0000 | [diff] [blame] | 371 | static bool isInitMessage(const ObjCMessage &msg) { |
John McCall | 85f3d76 | 2011-03-02 01:50:55 +0000 | [diff] [blame] | 372 | return msg.getMethodFamily() == OMF_init; |
Argyrios Kyrtzidis | d7a31ba | 2011-01-11 19:45:25 +0000 | [diff] [blame] | 373 | } |
Argyrios Kyrtzidis | 769ce3e | 2011-02-22 17:30:38 +0000 | [diff] [blame] | 374 | |
| 375 | //===----------------------------------------------------------------------===// |
| 376 | // Registration. |
| 377 | //===----------------------------------------------------------------------===// |
| 378 | |
| 379 | void ento::registerObjCSelfInitChecker(CheckerManager &mgr) { |
| 380 | mgr.registerChecker<ObjCSelfInitChecker>(); |
| 381 | } |