Argyrios Kyrtzidis | 4b7433f | 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 | 57540c5 | 2011-04-15 05:22:18 +0000 | [diff] [blame] | 19 | // To perform the required checking, values are tagged with flags that indicate |
Argyrios Kyrtzidis | 4b7433f | 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 | 4b7433f | 2011-01-11 19:45:25 +0000 | [diff] [blame] | 37 | //===----------------------------------------------------------------------===// |
| 38 | |
Argyrios Kyrtzidis | a6d04d5 | 2011-02-15 07:42:33 +0000 | [diff] [blame] | 39 | #include "ClangSACheckers.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 40 | #include "clang/AST/ParentMap.h" |
| 41 | #include "clang/StaticAnalyzer/Core/BugReporter/BugType.h" |
Argyrios Kyrtzidis | 6a5674f | 2011-03-01 01:16:21 +0000 | [diff] [blame] | 42 | #include "clang/StaticAnalyzer/Core/Checker.h" |
Argyrios Kyrtzidis | 507ff53 | 2011-02-17 21:39:17 +0000 | [diff] [blame] | 43 | #include "clang/StaticAnalyzer/Core/CheckerManager.h" |
Jordan Rose | 4f7df9b | 2012-07-26 21:39:41 +0000 | [diff] [blame] | 44 | #include "clang/StaticAnalyzer/Core/PathSensitive/CallEvent.h" |
Argyrios Kyrtzidis | ed35cf2 | 2011-02-22 17:30:38 +0000 | [diff] [blame] | 45 | #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h" |
Ted Kremenek | 001fd5b | 2011-08-15 22:09:50 +0000 | [diff] [blame] | 46 | #include "clang/StaticAnalyzer/Core/PathSensitive/ProgramStateTrait.h" |
Benjamin Kramer | cfe5aed | 2012-12-01 17:54:07 +0000 | [diff] [blame] | 47 | #include "llvm/Support/raw_ostream.h" |
Argyrios Kyrtzidis | 4b7433f | 2011-01-11 19:45:25 +0000 | [diff] [blame] | 48 | |
| 49 | using namespace clang; |
| 50 | using namespace ento; |
| 51 | |
| 52 | static bool shouldRunOnFunctionOrMethod(const NamedDecl *ND); |
| 53 | static bool isInitializationMethod(const ObjCMethodDecl *MD); |
Jordan Rose | 547060b | 2012-07-02 19:28:04 +0000 | [diff] [blame] | 54 | static bool isInitMessage(const ObjCMethodCall &Msg); |
Argyrios Kyrtzidis | 4b7433f | 2011-01-11 19:45:25 +0000 | [diff] [blame] | 55 | static bool isSelfVar(SVal location, CheckerContext &C); |
| 56 | |
| 57 | namespace { |
Jordan Rose | 682b316 | 2012-07-02 19:28:21 +0000 | [diff] [blame] | 58 | class ObjCSelfInitChecker : public Checker< check::PostObjCMessage, |
Argyrios Kyrtzidis | ed35cf2 | 2011-02-22 17:30:38 +0000 | [diff] [blame] | 59 | check::PostStmt<ObjCIvarRefExpr>, |
| 60 | check::PreStmt<ReturnStmt>, |
Jordan Rose | 682b316 | 2012-07-02 19:28:21 +0000 | [diff] [blame] | 61 | check::PreCall, |
| 62 | check::PostCall, |
Anna Zaks | 6684348 | 2012-05-08 21:19:21 +0000 | [diff] [blame] | 63 | check::Location, |
| 64 | check::Bind > { |
Jordan Rose | 49afeb0 | 2014-05-07 03:30:04 +0000 | [diff] [blame] | 65 | mutable std::unique_ptr<BugType> BT; |
Nico Weber | 7ce830b | 2014-05-06 17:33:42 +0000 | [diff] [blame] | 66 | |
| 67 | void checkForInvalidSelf(const Expr *E, CheckerContext &C, |
| 68 | const char *errorStr) const; |
| 69 | |
Argyrios Kyrtzidis | 4b7433f | 2011-01-11 19:45:25 +0000 | [diff] [blame] | 70 | public: |
Jordan Rose | 49afeb0 | 2014-05-07 03:30:04 +0000 | [diff] [blame] | 71 | ObjCSelfInitChecker() {} |
Jordan Rose | 547060b | 2012-07-02 19:28:04 +0000 | [diff] [blame] | 72 | void checkPostObjCMessage(const ObjCMethodCall &Msg, CheckerContext &C) const; |
Argyrios Kyrtzidis | ed35cf2 | 2011-02-22 17:30:38 +0000 | [diff] [blame] | 73 | void checkPostStmt(const ObjCIvarRefExpr *E, CheckerContext &C) const; |
| 74 | void checkPreStmt(const ReturnStmt *S, CheckerContext &C) const; |
Anna Zaks | 3e0f415 | 2011-10-06 00:43:15 +0000 | [diff] [blame] | 75 | void checkLocation(SVal location, bool isLoad, const Stmt *S, |
| 76 | CheckerContext &C) const; |
Anna Zaks | 6684348 | 2012-05-08 21:19:21 +0000 | [diff] [blame] | 77 | void checkBind(SVal loc, SVal val, const Stmt *S, CheckerContext &C) const; |
Anna Zaks | 53a0b6c | 2012-03-05 18:58:25 +0000 | [diff] [blame] | 78 | |
Jordan Rose | 682b316 | 2012-07-02 19:28:21 +0000 | [diff] [blame] | 79 | void checkPreCall(const CallEvent &CE, CheckerContext &C) const; |
| 80 | void checkPostCall(const CallEvent &CE, CheckerContext &C) const; |
Anna Zaks | 53a0b6c | 2012-03-05 18:58:25 +0000 | [diff] [blame] | 81 | |
Jordan Rose | bd94e5d | 2012-09-08 01:47:11 +0000 | [diff] [blame] | 82 | void printState(raw_ostream &Out, ProgramStateRef State, |
Craig Topper | fb6b25b | 2014-03-15 04:29:04 +0000 | [diff] [blame] | 83 | const char *NL, const char *Sep) const override; |
Argyrios Kyrtzidis | 4b7433f | 2011-01-11 19:45:25 +0000 | [diff] [blame] | 84 | }; |
| 85 | } // end anonymous namespace |
| 86 | |
Argyrios Kyrtzidis | 4b7433f | 2011-01-11 19:45:25 +0000 | [diff] [blame] | 87 | namespace { |
Argyrios Kyrtzidis | ed35cf2 | 2011-02-22 17:30:38 +0000 | [diff] [blame] | 88 | enum SelfFlagEnum { |
| 89 | /// \brief No flag set. |
| 90 | SelfFlag_None = 0x0, |
| 91 | /// \brief Value came from 'self'. |
| 92 | SelfFlag_Self = 0x1, |
| 93 | /// \brief Value came from the result of an initializer (e.g. [super init]). |
| 94 | SelfFlag_InitRes = 0x2 |
| 95 | }; |
| 96 | } |
| 97 | |
Jordan Rose | b9ed61f | 2012-11-02 01:54:42 +0000 | [diff] [blame] | 98 | REGISTER_MAP_WITH_PROGRAMSTATE(SelfFlag, SymbolRef, unsigned) |
| 99 | REGISTER_TRAIT_WITH_PROGRAMSTATE(CalledInit, bool) |
Argyrios Kyrtzidis | 4b7433f | 2011-01-11 19:45:25 +0000 | [diff] [blame] | 100 | |
Jordan Rose | b9ed61f | 2012-11-02 01:54:42 +0000 | [diff] [blame] | 101 | /// \brief A call receiving a reference to 'self' invalidates the object that |
| 102 | /// 'self' contains. This keeps the "self flags" assigned to the 'self' |
| 103 | /// object before the call so we can assign them to the new object that 'self' |
| 104 | /// points to after the call. |
| 105 | REGISTER_TRAIT_WITH_PROGRAMSTATE(PreCallSelfFlags, unsigned) |
Argyrios Kyrtzidis | 4b7433f | 2011-01-11 19:45:25 +0000 | [diff] [blame] | 106 | |
Ted Kremenek | 49b1e38 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 107 | static SelfFlagEnum getSelfFlags(SVal val, ProgramStateRef state) { |
Argyrios Kyrtzidis | 4b7433f | 2011-01-11 19:45:25 +0000 | [diff] [blame] | 108 | if (SymbolRef sym = val.getAsSymbol()) |
| 109 | if (const unsigned *attachedFlags = state->get<SelfFlag>(sym)) |
| 110 | return (SelfFlagEnum)*attachedFlags; |
| 111 | return SelfFlag_None; |
| 112 | } |
| 113 | |
| 114 | static SelfFlagEnum getSelfFlags(SVal val, CheckerContext &C) { |
| 115 | return getSelfFlags(val, C.getState()); |
| 116 | } |
| 117 | |
Ted Kremenek | 49b1e38 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 118 | static void addSelfFlag(ProgramStateRef state, SVal val, |
Ted Kremenek | 70aeefa | 2011-02-12 03:03:54 +0000 | [diff] [blame] | 119 | SelfFlagEnum flag, CheckerContext &C) { |
Argyrios Kyrtzidis | dd03d8d | 2011-02-05 05:54:53 +0000 | [diff] [blame] | 120 | // We tag the symbol that the SVal wraps. |
Anna Zaks | 3f12949 | 2012-12-13 00:42:19 +0000 | [diff] [blame] | 121 | if (SymbolRef sym = val.getAsSymbol()) { |
Jordan Rose | 5481cfe | 2012-09-08 01:47:28 +0000 | [diff] [blame] | 122 | state = state->set<SelfFlag>(sym, getSelfFlags(val, state) | flag); |
Anna Zaks | 3f12949 | 2012-12-13 00:42:19 +0000 | [diff] [blame] | 123 | C.addTransition(state); |
| 124 | } |
Argyrios Kyrtzidis | 4b7433f | 2011-01-11 19:45:25 +0000 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | static bool hasSelfFlag(SVal val, SelfFlagEnum flag, CheckerContext &C) { |
| 128 | return getSelfFlags(val, C) & flag; |
| 129 | } |
| 130 | |
| 131 | /// \brief Returns true of the value of the expression is the object that 'self' |
| 132 | /// points to and is an object that did not come from the result of calling |
| 133 | /// an initializer. |
| 134 | static bool isInvalidSelf(const Expr *E, CheckerContext &C) { |
George Karpenkov | d703ec9 | 2018-01-17 20:27:29 +0000 | [diff] [blame] | 135 | SVal exprVal = C.getSVal(E); |
Argyrios Kyrtzidis | 4b7433f | 2011-01-11 19:45:25 +0000 | [diff] [blame] | 136 | if (!hasSelfFlag(exprVal, SelfFlag_Self, C)) |
| 137 | return false; // value did not come from 'self'. |
| 138 | if (hasSelfFlag(exprVal, SelfFlag_InitRes, C)) |
| 139 | return false; // 'self' is properly initialized. |
| 140 | |
| 141 | return true; |
| 142 | } |
| 143 | |
Nico Weber | 7ce830b | 2014-05-06 17:33:42 +0000 | [diff] [blame] | 144 | void ObjCSelfInitChecker::checkForInvalidSelf(const Expr *E, CheckerContext &C, |
| 145 | const char *errorStr) const { |
Argyrios Kyrtzidis | 4b7433f | 2011-01-11 19:45:25 +0000 | [diff] [blame] | 146 | if (!E) |
| 147 | return; |
Ted Kremenek | 3a0678e | 2015-09-08 03:50:52 +0000 | [diff] [blame] | 148 | |
Ted Kremenek | 70aeefa | 2011-02-12 03:03:54 +0000 | [diff] [blame] | 149 | if (!C.getState()->get<CalledInit>()) |
| 150 | return; |
Ted Kremenek | 3a0678e | 2015-09-08 03:50:52 +0000 | [diff] [blame] | 151 | |
Argyrios Kyrtzidis | 4b7433f | 2011-01-11 19:45:25 +0000 | [diff] [blame] | 152 | if (!isInvalidSelf(E, C)) |
| 153 | return; |
Ted Kremenek | 3a0678e | 2015-09-08 03:50:52 +0000 | [diff] [blame] | 154 | |
Argyrios Kyrtzidis | 4b7433f | 2011-01-11 19:45:25 +0000 | [diff] [blame] | 155 | // Generate an error node. |
Devin Coughlin | e39bd40 | 2015-09-16 22:03:05 +0000 | [diff] [blame] | 156 | ExplodedNode *N = C.generateErrorNode(); |
Argyrios Kyrtzidis | 4b7433f | 2011-01-11 19:45:25 +0000 | [diff] [blame] | 157 | if (!N) |
| 158 | return; |
| 159 | |
Jordan Rose | 49afeb0 | 2014-05-07 03:30:04 +0000 | [diff] [blame] | 160 | if (!BT) |
| 161 | BT.reset(new BugType(this, "Missing \"self = [(super or self) init...]\"", |
| 162 | categories::CoreFoundationObjectiveC)); |
Aaron Ballman | 8d3a7a5 | 2015-06-23 13:15:32 +0000 | [diff] [blame] | 163 | C.emitReport(llvm::make_unique<BugReport>(*BT, errorStr, N)); |
Argyrios Kyrtzidis | 4b7433f | 2011-01-11 19:45:25 +0000 | [diff] [blame] | 164 | } |
| 165 | |
Jordan Rose | 547060b | 2012-07-02 19:28:04 +0000 | [diff] [blame] | 166 | void ObjCSelfInitChecker::checkPostObjCMessage(const ObjCMethodCall &Msg, |
Argyrios Kyrtzidis | ed35cf2 | 2011-02-22 17:30:38 +0000 | [diff] [blame] | 167 | CheckerContext &C) const { |
Argyrios Kyrtzidis | 4b7433f | 2011-01-11 19:45:25 +0000 | [diff] [blame] | 168 | // When encountering a message that does initialization (init rule), |
| 169 | // tag the return value so that we know later on that if self has this value |
| 170 | // then it is properly initialized. |
| 171 | |
| 172 | // FIXME: A callback should disable checkers at the start of functions. |
| 173 | if (!shouldRunOnFunctionOrMethod(dyn_cast<NamedDecl>( |
Anna Zaks | 00790d9 | 2012-02-04 02:31:37 +0000 | [diff] [blame] | 174 | C.getCurrentAnalysisDeclContext()->getDecl()))) |
Argyrios Kyrtzidis | 4b7433f | 2011-01-11 19:45:25 +0000 | [diff] [blame] | 175 | return; |
| 176 | |
Jordan Rose | 547060b | 2012-07-02 19:28:04 +0000 | [diff] [blame] | 177 | if (isInitMessage(Msg)) { |
Argyrios Kyrtzidis | 4b7433f | 2011-01-11 19:45:25 +0000 | [diff] [blame] | 178 | // Tag the return value as the result of an initializer. |
Ted Kremenek | 49b1e38 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 179 | ProgramStateRef state = C.getState(); |
Ted Kremenek | 3a0678e | 2015-09-08 03:50:52 +0000 | [diff] [blame] | 180 | |
Ted Kremenek | 70aeefa | 2011-02-12 03:03:54 +0000 | [diff] [blame] | 181 | // FIXME this really should be context sensitive, where we record |
| 182 | // the current stack frame (for IPA). Also, we need to clean this |
| 183 | // value out when we return from this method. |
| 184 | state = state->set<CalledInit>(true); |
Ted Kremenek | 3a0678e | 2015-09-08 03:50:52 +0000 | [diff] [blame] | 185 | |
George Karpenkov | d703ec9 | 2018-01-17 20:27:29 +0000 | [diff] [blame] | 186 | SVal V = C.getSVal(Msg.getOriginExpr()); |
Ted Kremenek | 70aeefa | 2011-02-12 03:03:54 +0000 | [diff] [blame] | 187 | addSelfFlag(state, V, SelfFlag_InitRes, C); |
Argyrios Kyrtzidis | 4b7433f | 2011-01-11 19:45:25 +0000 | [diff] [blame] | 188 | return; |
| 189 | } |
| 190 | |
| 191 | // We don't check for an invalid 'self' in an obj-c message expression to cut |
| 192 | // down false positives where logging functions get information from self |
| 193 | // (like its class) or doing "invalidation" on self when the initialization |
| 194 | // fails. |
| 195 | } |
| 196 | |
Argyrios Kyrtzidis | ed35cf2 | 2011-02-22 17:30:38 +0000 | [diff] [blame] | 197 | void ObjCSelfInitChecker::checkPostStmt(const ObjCIvarRefExpr *E, |
| 198 | CheckerContext &C) const { |
Argyrios Kyrtzidis | 4b7433f | 2011-01-11 19:45:25 +0000 | [diff] [blame] | 199 | // FIXME: A callback should disable checkers at the start of functions. |
| 200 | if (!shouldRunOnFunctionOrMethod(dyn_cast<NamedDecl>( |
Anna Zaks | 00790d9 | 2012-02-04 02:31:37 +0000 | [diff] [blame] | 201 | C.getCurrentAnalysisDeclContext()->getDecl()))) |
Argyrios Kyrtzidis | 4b7433f | 2011-01-11 19:45:25 +0000 | [diff] [blame] | 202 | return; |
| 203 | |
Alexander Kornienko | 4aca9b1 | 2014-02-11 21:49:21 +0000 | [diff] [blame] | 204 | checkForInvalidSelf( |
| 205 | E->getBase(), C, |
| 206 | "Instance variable used while 'self' is not set to the result of " |
Nico Weber | 7ce830b | 2014-05-06 17:33:42 +0000 | [diff] [blame] | 207 | "'[(super or self) init...]'"); |
Argyrios Kyrtzidis | 4b7433f | 2011-01-11 19:45:25 +0000 | [diff] [blame] | 208 | } |
| 209 | |
Argyrios Kyrtzidis | ed35cf2 | 2011-02-22 17:30:38 +0000 | [diff] [blame] | 210 | void ObjCSelfInitChecker::checkPreStmt(const ReturnStmt *S, |
| 211 | CheckerContext &C) const { |
Argyrios Kyrtzidis | 4b7433f | 2011-01-11 19:45:25 +0000 | [diff] [blame] | 212 | // FIXME: A callback should disable checkers at the start of functions. |
| 213 | if (!shouldRunOnFunctionOrMethod(dyn_cast<NamedDecl>( |
Anna Zaks | 00790d9 | 2012-02-04 02:31:37 +0000 | [diff] [blame] | 214 | C.getCurrentAnalysisDeclContext()->getDecl()))) |
Argyrios Kyrtzidis | 4b7433f | 2011-01-11 19:45:25 +0000 | [diff] [blame] | 215 | return; |
| 216 | |
| 217 | checkForInvalidSelf(S->getRetValue(), C, |
Alexander Kornienko | 4aca9b1 | 2014-02-11 21:49:21 +0000 | [diff] [blame] | 218 | "Returning 'self' while it is not set to the result of " |
Nico Weber | 7ce830b | 2014-05-06 17:33:42 +0000 | [diff] [blame] | 219 | "'[(super or self) init...]'"); |
Argyrios Kyrtzidis | 4b7433f | 2011-01-11 19:45:25 +0000 | [diff] [blame] | 220 | } |
| 221 | |
Jordan Rose | 682b316 | 2012-07-02 19:28:21 +0000 | [diff] [blame] | 222 | // When a call receives a reference to 'self', [Pre/Post]Call pass |
| 223 | // the SelfFlags from the object 'self' points to before the call to the new |
Argyrios Kyrtzidis | dd03d8d | 2011-02-05 05:54:53 +0000 | [diff] [blame] | 224 | // object after the call. This is to avoid invalidation of 'self' by logging |
| 225 | // functions. |
| 226 | // Another common pattern in classes with multiple initializers is to put the |
| 227 | // subclass's common initialization bits into a static function that receives |
| 228 | // the value of 'self', e.g: |
| 229 | // @code |
| 230 | // if (!(self = [super init])) |
| 231 | // return nil; |
| 232 | // if (!(self = _commonInit(self))) |
| 233 | // return nil; |
| 234 | // @endcode |
| 235 | // Until we can use inter-procedural analysis, in such a call, transfer the |
| 236 | // SelfFlags to the result of the call. |
Argyrios Kyrtzidis | 4b7433f | 2011-01-11 19:45:25 +0000 | [diff] [blame] | 237 | |
Jordan Rose | 682b316 | 2012-07-02 19:28:21 +0000 | [diff] [blame] | 238 | void ObjCSelfInitChecker::checkPreCall(const CallEvent &CE, |
Argyrios Kyrtzidis | ed35cf2 | 2011-02-22 17:30:38 +0000 | [diff] [blame] | 239 | CheckerContext &C) const { |
Jordan Rose | 682b316 | 2012-07-02 19:28:21 +0000 | [diff] [blame] | 240 | // FIXME: A callback should disable checkers at the start of functions. |
| 241 | if (!shouldRunOnFunctionOrMethod(dyn_cast<NamedDecl>( |
| 242 | C.getCurrentAnalysisDeclContext()->getDecl()))) |
| 243 | return; |
Jordan Rose | 2995349 | 2012-07-02 19:27:46 +0000 | [diff] [blame] | 244 | |
Ted Kremenek | 49b1e38 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 245 | ProgramStateRef state = C.getState(); |
Anna Zaks | 53a0b6c | 2012-03-05 18:58:25 +0000 | [diff] [blame] | 246 | unsigned NumArgs = CE.getNumArgs(); |
Anna Zaks | 51244c2 | 2012-04-16 21:51:09 +0000 | [diff] [blame] | 247 | // If we passed 'self' as and argument to the call, record it in the state |
| 248 | // to be propagated after the call. |
| 249 | // Note, we could have just given up, but try to be more optimistic here and |
| 250 | // assume that the functions are going to continue initialization or will not |
| 251 | // modify self. |
Anna Zaks | 53a0b6c | 2012-03-05 18:58:25 +0000 | [diff] [blame] | 252 | for (unsigned i = 0; i < NumArgs; ++i) { |
| 253 | SVal argV = CE.getArgSVal(i); |
Argyrios Kyrtzidis | 4b7433f | 2011-01-11 19:45:25 +0000 | [diff] [blame] | 254 | if (isSelfVar(argV, C)) { |
David Blaikie | 2fdacbc | 2013-02-20 05:52:05 +0000 | [diff] [blame] | 255 | unsigned selfFlags = getSelfFlags(state->getSVal(argV.castAs<Loc>()), C); |
Anna Zaks | da4c8d6 | 2011-10-26 21:06:34 +0000 | [diff] [blame] | 256 | C.addTransition(state->set<PreCallSelfFlags>(selfFlags)); |
Argyrios Kyrtzidis | 4b7433f | 2011-01-11 19:45:25 +0000 | [diff] [blame] | 257 | return; |
Argyrios Kyrtzidis | dd03d8d | 2011-02-05 05:54:53 +0000 | [diff] [blame] | 258 | } else if (hasSelfFlag(argV, SelfFlag_Self, C)) { |
Argyrios Kyrtzidis | ed35cf2 | 2011-02-22 17:30:38 +0000 | [diff] [blame] | 259 | unsigned selfFlags = getSelfFlags(argV, C); |
Anna Zaks | da4c8d6 | 2011-10-26 21:06:34 +0000 | [diff] [blame] | 260 | C.addTransition(state->set<PreCallSelfFlags>(selfFlags)); |
Argyrios Kyrtzidis | dd03d8d | 2011-02-05 05:54:53 +0000 | [diff] [blame] | 261 | return; |
Argyrios Kyrtzidis | 4b7433f | 2011-01-11 19:45:25 +0000 | [diff] [blame] | 262 | } |
| 263 | } |
| 264 | } |
| 265 | |
Jordan Rose | 682b316 | 2012-07-02 19:28:21 +0000 | [diff] [blame] | 266 | void ObjCSelfInitChecker::checkPostCall(const CallEvent &CE, |
Argyrios Kyrtzidis | ed35cf2 | 2011-02-22 17:30:38 +0000 | [diff] [blame] | 267 | CheckerContext &C) const { |
Jordan Rose | 682b316 | 2012-07-02 19:28:21 +0000 | [diff] [blame] | 268 | // FIXME: A callback should disable checkers at the start of functions. |
| 269 | if (!shouldRunOnFunctionOrMethod(dyn_cast<NamedDecl>( |
| 270 | C.getCurrentAnalysisDeclContext()->getDecl()))) |
| 271 | return; |
| 272 | |
Ted Kremenek | 49b1e38 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 273 | ProgramStateRef state = C.getState(); |
Jordan Rose | 682b316 | 2012-07-02 19:28:21 +0000 | [diff] [blame] | 274 | SelfFlagEnum prevFlags = (SelfFlagEnum)state->get<PreCallSelfFlags>(); |
| 275 | if (!prevFlags) |
| 276 | return; |
| 277 | state = state->remove<PreCallSelfFlags>(); |
| 278 | |
Anna Zaks | 53a0b6c | 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 | 4b7433f | 2011-01-11 19:45:25 +0000 | [diff] [blame] | 282 | if (isSelfVar(argV, C)) { |
Anna Zaks | 51244c2 | 2012-04-16 21:51:09 +0000 | [diff] [blame] | 283 | // If the address of 'self' is being passed to the call, assume that the |
| 284 | // 'self' after the call will have the same flags. |
| 285 | // EX: log(&self) |
David Blaikie | 2fdacbc | 2013-02-20 05:52:05 +0000 | [diff] [blame] | 286 | addSelfFlag(state, state->getSVal(argV.castAs<Loc>()), prevFlags, C); |
Argyrios Kyrtzidis | 4b7433f | 2011-01-11 19:45:25 +0000 | [diff] [blame] | 287 | return; |
Argyrios Kyrtzidis | dd03d8d | 2011-02-05 05:54:53 +0000 | [diff] [blame] | 288 | } else if (hasSelfFlag(argV, SelfFlag_Self, C)) { |
Anna Zaks | 51244c2 | 2012-04-16 21:51:09 +0000 | [diff] [blame] | 289 | // If 'self' is passed to the call by value, assume that the function |
| 290 | // returns 'self'. So assign the flags, which were set on 'self' to the |
| 291 | // return value. |
| 292 | // EX: self = performMoreInitialization(self) |
Jordan Rose | 829c383 | 2012-11-02 23:49:29 +0000 | [diff] [blame] | 293 | addSelfFlag(state, CE.getReturnValue(), prevFlags, C); |
Argyrios Kyrtzidis | dd03d8d | 2011-02-05 05:54:53 +0000 | [diff] [blame] | 294 | return; |
Argyrios Kyrtzidis | 4b7433f | 2011-01-11 19:45:25 +0000 | [diff] [blame] | 295 | } |
| 296 | } |
Jordan Rose | 829c383 | 2012-11-02 23:49:29 +0000 | [diff] [blame] | 297 | |
| 298 | C.addTransition(state); |
Argyrios Kyrtzidis | 4b7433f | 2011-01-11 19:45:25 +0000 | [diff] [blame] | 299 | } |
| 300 | |
Argyrios Kyrtzidis | ed35cf2 | 2011-02-22 17:30:38 +0000 | [diff] [blame] | 301 | void ObjCSelfInitChecker::checkLocation(SVal location, bool isLoad, |
Anna Zaks | 3e0f415 | 2011-10-06 00:43:15 +0000 | [diff] [blame] | 302 | const Stmt *S, |
Argyrios Kyrtzidis | ed35cf2 | 2011-02-22 17:30:38 +0000 | [diff] [blame] | 303 | CheckerContext &C) const { |
Anna Zaks | 3f12949 | 2012-12-13 00:42:19 +0000 | [diff] [blame] | 304 | if (!shouldRunOnFunctionOrMethod(dyn_cast<NamedDecl>( |
| 305 | C.getCurrentAnalysisDeclContext()->getDecl()))) |
| 306 | return; |
| 307 | |
Argyrios Kyrtzidis | 4b7433f | 2011-01-11 19:45:25 +0000 | [diff] [blame] | 308 | // Tag the result of a load from 'self' so that we can easily know that the |
| 309 | // value is the object that 'self' points to. |
Ted Kremenek | 49b1e38 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 310 | ProgramStateRef state = C.getState(); |
Argyrios Kyrtzidis | 4b7433f | 2011-01-11 19:45:25 +0000 | [diff] [blame] | 311 | if (isSelfVar(location, C)) |
David Blaikie | 2fdacbc | 2013-02-20 05:52:05 +0000 | [diff] [blame] | 312 | addSelfFlag(state, state->getSVal(location.castAs<Loc>()), SelfFlag_Self, |
| 313 | C); |
Argyrios Kyrtzidis | 4b7433f | 2011-01-11 19:45:25 +0000 | [diff] [blame] | 314 | } |
| 315 | |
Anna Zaks | 6684348 | 2012-05-08 21:19:21 +0000 | [diff] [blame] | 316 | |
| 317 | void ObjCSelfInitChecker::checkBind(SVal loc, SVal val, const Stmt *S, |
| 318 | CheckerContext &C) const { |
| 319 | // Allow assignment of anything to self. Self is a local variable in the |
| 320 | // initializer, so it is legal to assign anything to it, like results of |
Ted Kremenek | 3a0678e | 2015-09-08 03:50:52 +0000 | [diff] [blame] | 321 | // static functions/method calls. After self is assigned something we cannot |
Anna Zaks | 6684348 | 2012-05-08 21:19:21 +0000 | [diff] [blame] | 322 | // reason about, stop enforcing the rules. |
| 323 | // (Only continue checking if the assigned value should be treated as self.) |
| 324 | if ((isSelfVar(loc, C)) && |
| 325 | !hasSelfFlag(val, SelfFlag_InitRes, C) && |
| 326 | !hasSelfFlag(val, SelfFlag_Self, C) && |
| 327 | !isSelfVar(val, C)) { |
| 328 | |
| 329 | // Stop tracking the checker-specific state in the state. |
| 330 | ProgramStateRef State = C.getState(); |
| 331 | State = State->remove<CalledInit>(); |
| 332 | if (SymbolRef sym = loc.getAsSymbol()) |
| 333 | State = State->remove<SelfFlag>(sym); |
| 334 | C.addTransition(State); |
| 335 | } |
| 336 | } |
| 337 | |
Jordan Rose | bd94e5d | 2012-09-08 01:47:11 +0000 | [diff] [blame] | 338 | void ObjCSelfInitChecker::printState(raw_ostream &Out, ProgramStateRef State, |
| 339 | const char *NL, const char *Sep) const { |
Jordan Rose | b9ed61f | 2012-11-02 01:54:42 +0000 | [diff] [blame] | 340 | SelfFlagTy FlagMap = State->get<SelfFlag>(); |
Jordan Rose | bd94e5d | 2012-09-08 01:47:11 +0000 | [diff] [blame] | 341 | bool DidCallInit = State->get<CalledInit>(); |
| 342 | SelfFlagEnum PreCallFlags = (SelfFlagEnum)State->get<PreCallSelfFlags>(); |
| 343 | |
| 344 | if (FlagMap.isEmpty() && !DidCallInit && !PreCallFlags) |
| 345 | return; |
| 346 | |
Anton Yartsev | 6a61922 | 2014-02-17 18:25:34 +0000 | [diff] [blame] | 347 | Out << Sep << NL << *this << " :" << NL; |
Jordan Rose | bd94e5d | 2012-09-08 01:47:11 +0000 | [diff] [blame] | 348 | |
| 349 | if (DidCallInit) |
| 350 | Out << " An init method has been called." << NL; |
| 351 | |
| 352 | if (PreCallFlags != SelfFlag_None) { |
| 353 | if (PreCallFlags & SelfFlag_Self) { |
| 354 | Out << " An argument of the current call came from the 'self' variable." |
| 355 | << NL; |
| 356 | } |
| 357 | if (PreCallFlags & SelfFlag_InitRes) { |
| 358 | Out << " An argument of the current call came from an init method." |
| 359 | << NL; |
| 360 | } |
| 361 | } |
| 362 | |
| 363 | Out << NL; |
Jordan Rose | b9ed61f | 2012-11-02 01:54:42 +0000 | [diff] [blame] | 364 | for (SelfFlagTy::iterator I = FlagMap.begin(), E = FlagMap.end(); |
| 365 | I != E; ++I) { |
Jordan Rose | bd94e5d | 2012-09-08 01:47:11 +0000 | [diff] [blame] | 366 | Out << I->first << " : "; |
| 367 | |
| 368 | if (I->second == SelfFlag_None) |
| 369 | Out << "none"; |
| 370 | |
| 371 | if (I->second & SelfFlag_Self) |
| 372 | Out << "self variable"; |
| 373 | |
| 374 | if (I->second & SelfFlag_InitRes) { |
| 375 | if (I->second != SelfFlag_InitRes) |
| 376 | Out << " | "; |
| 377 | Out << "result of init method"; |
| 378 | } |
| 379 | |
| 380 | Out << NL; |
| 381 | } |
| 382 | } |
| 383 | |
| 384 | |
Argyrios Kyrtzidis | 4b7433f | 2011-01-11 19:45:25 +0000 | [diff] [blame] | 385 | // FIXME: A callback should disable checkers at the start of functions. |
| 386 | static bool shouldRunOnFunctionOrMethod(const NamedDecl *ND) { |
| 387 | if (!ND) |
| 388 | return false; |
| 389 | |
| 390 | const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(ND); |
| 391 | if (!MD) |
| 392 | return false; |
Argyrios Kyrtzidis | 4b7433f | 2011-01-11 19:45:25 +0000 | [diff] [blame] | 393 | if (!isInitializationMethod(MD)) |
| 394 | return false; |
| 395 | |
Argyrios Kyrtzidis | 3ae681e | 2011-01-25 23:54:44 +0000 | [diff] [blame] | 396 | // self = [super init] applies only to NSObject subclasses. |
| 397 | // For instance, NSProxy doesn't implement -init. |
Ted Kremenek | 5ef32db | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 398 | ASTContext &Ctx = MD->getASTContext(); |
Argyrios Kyrtzidis | 3ae681e | 2011-01-25 23:54:44 +0000 | [diff] [blame] | 399 | IdentifierInfo* NSObjectII = &Ctx.Idents.get("NSObject"); |
Ted Kremenek | 5ef32db | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 400 | ObjCInterfaceDecl *ID = MD->getClassInterface()->getSuperClass(); |
Argyrios Kyrtzidis | 3ae681e | 2011-01-25 23:54:44 +0000 | [diff] [blame] | 401 | for ( ; ID ; ID = ID->getSuperClass()) { |
| 402 | IdentifierInfo *II = ID->getIdentifier(); |
| 403 | |
| 404 | if (II == NSObjectII) |
| 405 | break; |
| 406 | } |
Alexander Kornienko | 9c10490 | 2015-12-28 13:06:58 +0000 | [diff] [blame] | 407 | return ID != nullptr; |
Argyrios Kyrtzidis | 4b7433f | 2011-01-11 19:45:25 +0000 | [diff] [blame] | 408 | } |
| 409 | |
| 410 | /// \brief Returns true if the location is 'self'. |
| 411 | static bool isSelfVar(SVal location, CheckerContext &C) { |
Ted Kremenek | 3a0678e | 2015-09-08 03:50:52 +0000 | [diff] [blame] | 412 | AnalysisDeclContext *analCtx = C.getCurrentAnalysisDeclContext(); |
Argyrios Kyrtzidis | 4b7433f | 2011-01-11 19:45:25 +0000 | [diff] [blame] | 413 | if (!analCtx->getSelfDecl()) |
| 414 | return false; |
David Blaikie | 2fdacbc | 2013-02-20 05:52:05 +0000 | [diff] [blame] | 415 | if (!location.getAs<loc::MemRegionVal>()) |
Argyrios Kyrtzidis | 4b7433f | 2011-01-11 19:45:25 +0000 | [diff] [blame] | 416 | return false; |
| 417 | |
David Blaikie | 2fdacbc | 2013-02-20 05:52:05 +0000 | [diff] [blame] | 418 | loc::MemRegionVal MRV = location.castAs<loc::MemRegionVal>(); |
Anna Zaks | 51244c2 | 2012-04-16 21:51:09 +0000 | [diff] [blame] | 419 | if (const DeclRegion *DR = dyn_cast<DeclRegion>(MRV.stripCasts())) |
Argyrios Kyrtzidis | 4b7433f | 2011-01-11 19:45:25 +0000 | [diff] [blame] | 420 | return (DR->getDecl() == analCtx->getSelfDecl()); |
| 421 | |
| 422 | return false; |
| 423 | } |
| 424 | |
| 425 | static bool isInitializationMethod(const ObjCMethodDecl *MD) { |
John McCall | b452625 | 2011-03-02 01:50:55 +0000 | [diff] [blame] | 426 | return MD->getMethodFamily() == OMF_init; |
Argyrios Kyrtzidis | 4b7433f | 2011-01-11 19:45:25 +0000 | [diff] [blame] | 427 | } |
| 428 | |
Jordan Rose | 547060b | 2012-07-02 19:28:04 +0000 | [diff] [blame] | 429 | static bool isInitMessage(const ObjCMethodCall &Call) { |
| 430 | return Call.getMethodFamily() == OMF_init; |
Argyrios Kyrtzidis | 4b7433f | 2011-01-11 19:45:25 +0000 | [diff] [blame] | 431 | } |
Argyrios Kyrtzidis | ed35cf2 | 2011-02-22 17:30:38 +0000 | [diff] [blame] | 432 | |
| 433 | //===----------------------------------------------------------------------===// |
| 434 | // Registration. |
| 435 | //===----------------------------------------------------------------------===// |
| 436 | |
| 437 | void ento::registerObjCSelfInitChecker(CheckerManager &mgr) { |
| 438 | mgr.registerChecker<ObjCSelfInitChecker>(); |
| 439 | } |