| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | |
| 13 | |
| 14 | |
| 15 | |
| 16 | |
| 17 | |
| 18 | |
| 19 | |
| 20 | |
| 21 | |
| 22 | |
| 23 | |
| 24 | |
| 25 | |
| 26 | |
| 27 | |
| 28 | |
| 29 | |
| 30 | |
| 31 | |
| 32 | |
| 33 | |
| 34 | |
| 35 | |
| 36 | |
| 37 | |
| 38 | #include "clang/StaticAnalyzer/Checkers/BuiltinCheckerRegistration.h" |
| 39 | #include "clang/AST/ParentMap.h" |
| 40 | #include "clang/StaticAnalyzer/Core/BugReporter/BugType.h" |
| 41 | #include "clang/StaticAnalyzer/Core/Checker.h" |
| 42 | #include "clang/StaticAnalyzer/Core/CheckerManager.h" |
| 43 | #include "clang/StaticAnalyzer/Core/PathSensitive/CallEvent.h" |
| 44 | #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h" |
| 45 | #include "clang/StaticAnalyzer/Core/PathSensitive/ProgramStateTrait.h" |
| 46 | #include "llvm/Support/raw_ostream.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); |
| 53 | static bool isInitMessage(const ObjCMethodCall &Msg); |
| 54 | static bool isSelfVar(SVal location, CheckerContext &C); |
| 55 | |
| 56 | namespace { |
| 57 | class ObjCSelfInitChecker : public Checker< check::PostObjCMessage, |
| 58 | check::PostStmt<ObjCIvarRefExpr>, |
| 59 | check::PreStmt<ReturnStmt>, |
| 60 | check::PreCall, |
| 61 | check::PostCall, |
| 62 | check::Location, |
| 63 | check::Bind > { |
| 64 | mutable std::unique_ptr<BugType> BT; |
| 65 | |
| 66 | void checkForInvalidSelf(const Expr *E, CheckerContext &C, |
| 67 | const char *errorStr) const; |
| 68 | |
| 69 | public: |
| 70 | ObjCSelfInitChecker() {} |
| 71 | void checkPostObjCMessage(const ObjCMethodCall &Msg, CheckerContext &C) const; |
| 72 | void checkPostStmt(const ObjCIvarRefExpr *E, CheckerContext &C) const; |
| 73 | void checkPreStmt(const ReturnStmt *S, CheckerContext &C) const; |
| 74 | void checkLocation(SVal location, bool isLoad, const Stmt *S, |
| 75 | CheckerContext &C) const; |
| 76 | void checkBind(SVal loc, SVal val, const Stmt *S, CheckerContext &C) const; |
| 77 | |
| 78 | void checkPreCall(const CallEvent &CE, CheckerContext &C) const; |
| 79 | void checkPostCall(const CallEvent &CE, CheckerContext &C) const; |
| 80 | |
| 81 | void printState(raw_ostream &Out, ProgramStateRef State, |
| 82 | const char *NL, const char *Sep) const override; |
| 83 | }; |
| 84 | } |
| 85 | |
| 86 | namespace { |
| 87 | enum SelfFlagEnum { |
| 88 | |
| 89 | SelfFlag_None = 0x0, |
| 90 | |
| 91 | SelfFlag_Self = 0x1, |
| 92 | |
| 93 | SelfFlag_InitRes = 0x2 |
| 94 | }; |
| 95 | } |
| 96 | |
| 97 | REGISTER_MAP_WITH_PROGRAMSTATE(SelfFlag, SymbolRef, unsigned) |
| 98 | REGISTER_TRAIT_WITH_PROGRAMSTATE(CalledInit, bool) |
| 99 | |
| 100 | |
| 101 | |
| 102 | |
| 103 | |
| 104 | REGISTER_TRAIT_WITH_PROGRAMSTATE(PreCallSelfFlags, unsigned) |
| 105 | |
| 106 | static SelfFlagEnum getSelfFlags(SVal val, ProgramStateRef state) { |
| 107 | if (SymbolRef sym = val.getAsSymbol()) |
| 108 | if (const unsigned *attachedFlags = state->get<SelfFlag>(sym)) |
| 109 | return (SelfFlagEnum)*attachedFlags; |
| 110 | return SelfFlag_None; |
| 111 | } |
| 112 | |
| 113 | static SelfFlagEnum getSelfFlags(SVal val, CheckerContext &C) { |
| 114 | return getSelfFlags(val, C.getState()); |
| 115 | } |
| 116 | |
| 117 | static void addSelfFlag(ProgramStateRef state, SVal val, |
| 118 | SelfFlagEnum flag, CheckerContext &C) { |
| 119 | |
| 120 | if (SymbolRef sym = val.getAsSymbol()) { |
| 121 | state = state->set<SelfFlag>(sym, getSelfFlags(val, state) | flag); |
| 122 | C.addTransition(state); |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | static bool hasSelfFlag(SVal val, SelfFlagEnum flag, CheckerContext &C) { |
| 127 | return getSelfFlags(val, C) & flag; |
| 128 | } |
| 129 | |
| 130 | |
| 131 | |
| 132 | |
| 133 | static bool isInvalidSelf(const Expr *E, CheckerContext &C) { |
| 134 | SVal exprVal = C.getSVal(E); |
| 135 | if (!hasSelfFlag(exprVal, SelfFlag_Self, C)) |
| 136 | return false; |
| 137 | if (hasSelfFlag(exprVal, SelfFlag_InitRes, C)) |
| 138 | return false; |
| 139 | |
| 140 | return true; |
| 141 | } |
| 142 | |
| 143 | void ObjCSelfInitChecker::checkForInvalidSelf(const Expr *E, CheckerContext &C, |
| 144 | const char *errorStr) const { |
| 145 | if (!E) |
| 146 | return; |
| 147 | |
| 148 | if (!C.getState()->get<CalledInit>()) |
| 149 | return; |
| 150 | |
| 151 | if (!isInvalidSelf(E, C)) |
| 152 | return; |
| 153 | |
| 154 | |
| 155 | ExplodedNode *N = C.generateErrorNode(); |
| 156 | if (!N) |
| 157 | return; |
| 158 | |
| 159 | if (!BT) |
| 160 | BT.reset(new BugType(this, "Missing \"self = [(super or self) init...]\"", |
| 161 | categories::CoreFoundationObjectiveC)); |
| 162 | C.emitReport(llvm::make_unique<BugReport>(*BT, errorStr, N)); |
| 163 | } |
| 164 | |
| 165 | void ObjCSelfInitChecker::checkPostObjCMessage(const ObjCMethodCall &Msg, |
| 166 | CheckerContext &C) const { |
| 167 | |
| 168 | |
| 169 | |
| 170 | |
| 171 | |
| 172 | if (!shouldRunOnFunctionOrMethod(dyn_cast<NamedDecl>( |
| 173 | C.getCurrentAnalysisDeclContext()->getDecl()))) |
| 174 | return; |
| 175 | |
| 176 | if (isInitMessage(Msg)) { |
| 177 | |
| 178 | ProgramStateRef state = C.getState(); |
| 179 | |
| 180 | |
| 181 | |
| 182 | |
| 183 | state = state->set<CalledInit>(true); |
| 184 | |
| 185 | SVal V = C.getSVal(Msg.getOriginExpr()); |
| 186 | addSelfFlag(state, V, SelfFlag_InitRes, C); |
| 187 | return; |
| 188 | } |
| 189 | |
| 190 | |
| 191 | |
| 192 | |
| 193 | |
| 194 | } |
| 195 | |
| 196 | void ObjCSelfInitChecker::checkPostStmt(const ObjCIvarRefExpr *E, |
| 197 | CheckerContext &C) const { |
| 198 | |
| 199 | if (!shouldRunOnFunctionOrMethod(dyn_cast<NamedDecl>( |
| 200 | C.getCurrentAnalysisDeclContext()->getDecl()))) |
| 201 | return; |
| 202 | |
| 203 | checkForInvalidSelf( |
| 204 | E->getBase(), C, |
| 205 | "Instance variable used while 'self' is not set to the result of " |
| 206 | "'[(super or self) init...]'"); |
| 207 | } |
| 208 | |
| 209 | void ObjCSelfInitChecker::checkPreStmt(const ReturnStmt *S, |
| 210 | CheckerContext &C) const { |
| 211 | |
| 212 | if (!shouldRunOnFunctionOrMethod(dyn_cast<NamedDecl>( |
| 213 | C.getCurrentAnalysisDeclContext()->getDecl()))) |
| 214 | return; |
| 215 | |
| 216 | checkForInvalidSelf(S->getRetValue(), C, |
| 217 | "Returning 'self' while it is not set to the result of " |
| 218 | "'[(super or self) init...]'"); |
| 219 | } |
| 220 | |
| 221 | |
| 222 | |
| 223 | |
| 224 | |
| 225 | |
| 226 | |
| 227 | |
| 228 | |
| 229 | |
| 230 | |
| 231 | |
| 232 | |
| 233 | |
| 234 | |
| 235 | |
| 236 | |
| 237 | void ObjCSelfInitChecker::checkPreCall(const CallEvent &CE, |
| 238 | CheckerContext &C) const { |
| 239 | |
| 240 | if (!shouldRunOnFunctionOrMethod(dyn_cast<NamedDecl>( |
| 241 | C.getCurrentAnalysisDeclContext()->getDecl()))) |
| 242 | return; |
| 243 | |
| 244 | ProgramStateRef state = C.getState(); |
| 245 | unsigned NumArgs = CE.getNumArgs(); |
| 246 | |
| 247 | |
| 248 | |
| 249 | |
| 250 | |
| 251 | for (unsigned i = 0; i < NumArgs; ++i) { |
| 252 | SVal argV = CE.getArgSVal(i); |
| 253 | if (isSelfVar(argV, C)) { |
| 254 | unsigned selfFlags = getSelfFlags(state->getSVal(argV.castAs<Loc>()), C); |
| 255 | C.addTransition(state->set<PreCallSelfFlags>(selfFlags)); |
| 256 | return; |
| 257 | } else if (hasSelfFlag(argV, SelfFlag_Self, C)) { |
| 258 | unsigned selfFlags = getSelfFlags(argV, C); |
| 259 | C.addTransition(state->set<PreCallSelfFlags>(selfFlags)); |
| 260 | return; |
| 261 | } |
| 262 | } |
| 263 | } |
| 264 | |
| 265 | void ObjCSelfInitChecker::checkPostCall(const CallEvent &CE, |
| 266 | CheckerContext &C) const { |
| 267 | |
| 268 | if (!shouldRunOnFunctionOrMethod(dyn_cast<NamedDecl>( |
| 269 | C.getCurrentAnalysisDeclContext()->getDecl()))) |
| 270 | return; |
| 271 | |
| 272 | ProgramStateRef state = C.getState(); |
| 273 | SelfFlagEnum prevFlags = (SelfFlagEnum)state->get<PreCallSelfFlags>(); |
| 274 | if (!prevFlags) |
| 275 | return; |
| 276 | state = state->remove<PreCallSelfFlags>(); |
| 277 | |
| 278 | unsigned NumArgs = CE.getNumArgs(); |
| 279 | for (unsigned i = 0; i < NumArgs; ++i) { |
| 280 | SVal argV = CE.getArgSVal(i); |
| 281 | if (isSelfVar(argV, C)) { |
| 282 | |
| 283 | |
| 284 | |
| 285 | addSelfFlag(state, state->getSVal(argV.castAs<Loc>()), prevFlags, C); |
| 286 | return; |
| 287 | } else if (hasSelfFlag(argV, SelfFlag_Self, C)) { |
| 288 | |
| 289 | |
| 290 | |
| 291 | |
| 292 | addSelfFlag(state, CE.getReturnValue(), prevFlags, C); |
| 293 | return; |
| 294 | } |
| 295 | } |
| 296 | |
| 297 | C.addTransition(state); |
| 298 | } |
| 299 | |
| 300 | void ObjCSelfInitChecker::checkLocation(SVal location, bool isLoad, |
| 301 | const Stmt *S, |
| 302 | CheckerContext &C) const { |
| 303 | if (!shouldRunOnFunctionOrMethod(dyn_cast<NamedDecl>( |
| 304 | C.getCurrentAnalysisDeclContext()->getDecl()))) |
| 305 | return; |
| 306 | |
| 307 | |
| 308 | |
| 309 | ProgramStateRef state = C.getState(); |
| 310 | if (isSelfVar(location, C)) |
| 311 | addSelfFlag(state, state->getSVal(location.castAs<Loc>()), SelfFlag_Self, |
| 312 | C); |
| 313 | } |
| 314 | |
| 315 | |
| 316 | void ObjCSelfInitChecker::checkBind(SVal loc, SVal val, const Stmt *S, |
| 317 | CheckerContext &C) const { |
| 318 | |
| 319 | |
| 320 | |
| 321 | |
| 322 | |
| 323 | if ((isSelfVar(loc, C)) && |
| 324 | !hasSelfFlag(val, SelfFlag_InitRes, C) && |
| 325 | !hasSelfFlag(val, SelfFlag_Self, C) && |
| 326 | !isSelfVar(val, C)) { |
| 327 | |
| 328 | |
| 329 | ProgramStateRef State = C.getState(); |
| 330 | State = State->remove<CalledInit>(); |
| 331 | if (SymbolRef sym = loc.getAsSymbol()) |
| 332 | State = State->remove<SelfFlag>(sym); |
| 333 | C.addTransition(State); |
| 334 | } |
| 335 | } |
| 336 | |
| 337 | void ObjCSelfInitChecker::printState(raw_ostream &Out, ProgramStateRef State, |
| 338 | const char *NL, const char *Sep) const { |
| 339 | SelfFlagTy FlagMap = State->get<SelfFlag>(); |
| 340 | bool DidCallInit = State->get<CalledInit>(); |
| 341 | SelfFlagEnum PreCallFlags = (SelfFlagEnum)State->get<PreCallSelfFlags>(); |
| 342 | |
| 343 | if (FlagMap.isEmpty() && !DidCallInit && !PreCallFlags) |
| 344 | return; |
| 345 | |
| 346 | Out << Sep << NL << *this << " :" << NL; |
| 347 | |
| 348 | if (DidCallInit) |
| 349 | Out << " An init method has been called." << NL; |
| 350 | |
| 351 | if (PreCallFlags != SelfFlag_None) { |
| 352 | if (PreCallFlags & SelfFlag_Self) { |
| 353 | Out << " An argument of the current call came from the 'self' variable." |
| 354 | << NL; |
| 355 | } |
| 356 | if (PreCallFlags & SelfFlag_InitRes) { |
| 357 | Out << " An argument of the current call came from an init method." |
| 358 | << NL; |
| 359 | } |
| 360 | } |
| 361 | |
| 362 | Out << NL; |
| 363 | for (SelfFlagTy::iterator I = FlagMap.begin(), E = FlagMap.end(); |
| 364 | I != E; ++I) { |
| 365 | Out << I->first << " : "; |
| 366 | |
| 367 | if (I->second == SelfFlag_None) |
| 368 | Out << "none"; |
| 369 | |
| 370 | if (I->second & SelfFlag_Self) |
| 371 | Out << "self variable"; |
| 372 | |
| 373 | if (I->second & SelfFlag_InitRes) { |
| 374 | if (I->second != SelfFlag_InitRes) |
| 375 | Out << " | "; |
| 376 | Out << "result of init method"; |
| 377 | } |
| 378 | |
| 379 | Out << NL; |
| 380 | } |
| 381 | } |
| 382 | |
| 383 | |
| 384 | |
| 385 | static bool shouldRunOnFunctionOrMethod(const NamedDecl *ND) { |
| 386 | if (!ND) |
| 387 | return false; |
| 388 | |
| 389 | const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(ND); |
| 390 | if (!MD) |
| 391 | return false; |
| 392 | if (!isInitializationMethod(MD)) |
| 393 | return false; |
| 394 | |
| 395 | |
| 396 | |
| 397 | ASTContext &Ctx = MD->getASTContext(); |
| 398 | IdentifierInfo* NSObjectII = &Ctx.Idents.get("NSObject"); |
| 399 | ObjCInterfaceDecl *ID = MD->getClassInterface()->getSuperClass(); |
| 400 | for ( ; ID ; ID = ID->getSuperClass()) { |
| 401 | IdentifierInfo *II = ID->getIdentifier(); |
| 402 | |
| 403 | if (II == NSObjectII) |
| 404 | break; |
| 405 | } |
| 406 | return ID != nullptr; |
| 407 | } |
| 408 | |
| 409 | |
| 410 | static bool isSelfVar(SVal location, CheckerContext &C) { |
| 411 | AnalysisDeclContext *analCtx = C.getCurrentAnalysisDeclContext(); |
| 412 | if (!analCtx->getSelfDecl()) |
| 413 | return false; |
| 414 | if (!location.getAs<loc::MemRegionVal>()) |
| 415 | return false; |
| 416 | |
| 417 | loc::MemRegionVal MRV = location.castAs<loc::MemRegionVal>(); |
| 418 | if (const DeclRegion *DR = dyn_cast<DeclRegion>(MRV.stripCasts())) |
| 419 | return (DR->getDecl() == analCtx->getSelfDecl()); |
| 420 | |
| 421 | return false; |
| 422 | } |
| 423 | |
| 424 | static bool isInitializationMethod(const ObjCMethodDecl *MD) { |
| 425 | return MD->getMethodFamily() == OMF_init; |
| 426 | } |
| 427 | |
| 428 | static bool isInitMessage(const ObjCMethodCall &Call) { |
| 429 | return Call.getMethodFamily() == OMF_init; |
| 430 | } |
| 431 | |
| 432 | |
| 433 | |
| 434 | |
| 435 | |
| 436 | void ento::registerObjCSelfInitChecker(CheckerManager &mgr) { |
| 437 | mgr.registerChecker<ObjCSelfInitChecker>(); |
| 438 | } |
| 439 | |
| 440 | bool ento::shouldRegisterObjCSelfInitChecker(const LangOptions &LO) { |
| 441 | return true; |
| 442 | } |
| 443 | |