1 //===--- CallAndMessageChecker.cpp ------------------------------*- 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 CallAndMessageChecker, a builtin checker that checks for various 11 // errors of call and objc message expressions. 12 // 13 //===----------------------------------------------------------------------===// 14 15 #include "ClangSACheckers.h" 16 #include "clang/AST/ParentMap.h" 17 #include "clang/Basic/TargetInfo.h" 18 #include "clang/StaticAnalyzer/Core/BugReporter/BugType.h" 19 #include "clang/StaticAnalyzer/Core/Checker.h" 20 #include "clang/StaticAnalyzer/Core/CheckerManager.h" 21 #include "clang/StaticAnalyzer/Core/PathSensitive/CallEvent.h" 22 #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h" 23 #include "llvm/ADT/SmallString.h" 24 #include "llvm/Support/raw_ostream.h" 25 26 using namespace clang; 27 using namespace ento; 28 29 namespace { 30 31 struct ChecksFilter { 32 DefaultBool Check_CallAndMessageUnInitRefArg; 33 DefaultBool Check_CallAndMessageChecker; 34 35 CheckName CheckName_CallAndMessageUnInitRefArg; 36 CheckName CheckName_CallAndMessageChecker; 37 }; 38 39 class CallAndMessageChecker 40 : public Checker< check::PreStmt<CallExpr>, 41 check::PreStmt<CXXDeleteExpr>, 42 check::PreObjCMessage, 43 check::ObjCMessageNil, 44 check::PreCall > { 45 mutable std::unique_ptr<BugType> BT_call_null; 46 mutable std::unique_ptr<BugType> BT_call_undef; 47 mutable std::unique_ptr<BugType> BT_cxx_call_null; 48 mutable std::unique_ptr<BugType> BT_cxx_call_undef; 49 mutable std::unique_ptr<BugType> BT_call_arg; 50 mutable std::unique_ptr<BugType> BT_cxx_delete_undef; 51 mutable std::unique_ptr<BugType> BT_msg_undef; 52 mutable std::unique_ptr<BugType> BT_objc_prop_undef; 53 mutable std::unique_ptr<BugType> BT_objc_subscript_undef; 54 mutable std::unique_ptr<BugType> BT_msg_arg; 55 mutable std::unique_ptr<BugType> BT_msg_ret; 56 mutable std::unique_ptr<BugType> BT_call_few_args; 57 58 public: 59 ChecksFilter Filter; 60 61 void checkPreStmt(const CallExpr *CE, CheckerContext &C) const; 62 void checkPreStmt(const CXXDeleteExpr *DE, CheckerContext &C) const; 63 void checkPreObjCMessage(const ObjCMethodCall &msg, CheckerContext &C) const; 64 65 /// Fill in the return value that results from messaging nil based on the 66 /// return type and architecture and diagnose if the return value will be 67 /// garbage. 68 void checkObjCMessageNil(const ObjCMethodCall &msg, CheckerContext &C) const; 69 70 void checkPreCall(const CallEvent &Call, CheckerContext &C) const; 71 72 private: 73 bool PreVisitProcessArg(CheckerContext &C, SVal V, SourceRange ArgRange, 74 const Expr *ArgEx, bool IsFirstArgument, 75 bool CheckUninitFields, const CallEvent &Call, 76 std::unique_ptr<BugType> &BT, 77 const ParmVarDecl *ParamDecl) const; 78 79 static void emitBadCall(BugType *BT, CheckerContext &C, const Expr *BadE); 80 void emitNilReceiverBug(CheckerContext &C, const ObjCMethodCall &msg, 81 ExplodedNode *N) const; 82 83 void HandleNilReceiver(CheckerContext &C, 84 ProgramStateRef state, 85 const ObjCMethodCall &msg) const; 86 87 void LazyInit_BT(const char *desc, std::unique_ptr<BugType> &BT) const { 88 if (!BT) 89 BT.reset(new BuiltinBug(this, desc)); 90 } 91 bool uninitRefOrPointer(CheckerContext &C, const SVal &V, 92 SourceRange ArgRange, 93 const Expr *ArgEx, std::unique_ptr<BugType> &BT, 94 const ParmVarDecl *ParamDecl, const char *BD) const; 95 }; 96 } // end anonymous namespace 97 98 void CallAndMessageChecker::emitBadCall(BugType *BT, CheckerContext &C, 99 const Expr *BadE) { 100 ExplodedNode *N = C.generateErrorNode(); 101 if (!N) 102 return; 103 104 auto R = llvm::make_unique<BugReport>(*BT, BT->getName(), N); 105 if (BadE) { 106 R->addRange(BadE->getSourceRange()); 107 if (BadE->isGLValue()) 108 BadE = bugreporter::getDerefExpr(BadE); 109 bugreporter::trackNullOrUndefValue(N, BadE, *R); 110 } 111 C.emitReport(std::move(R)); 112 } 113 114 static StringRef describeUninitializedArgumentInCall(const CallEvent &Call, 115 bool IsFirstArgument) { 116 switch (Call.getKind()) { 117 case CE_ObjCMessage: { 118 const ObjCMethodCall &Msg = cast<ObjCMethodCall>(Call); 119 switch (Msg.getMessageKind()) { 120 case OCM_Message: 121 return "Argument in message expression is an uninitialized value"; 122 case OCM_PropertyAccess: 123 assert(Msg.isSetter() && "Getters have no args"); 124 return "Argument for property setter is an uninitialized value"; 125 case OCM_Subscript: 126 if (Msg.isSetter() && IsFirstArgument) 127 return "Argument for subscript setter is an uninitialized value"; 128 return "Subscript index is an uninitialized value"; 129 } 130 llvm_unreachable("Unknown message kind."); 131 } 132 case CE_Block: 133 return "Block call argument is an uninitialized value"; 134 default: 135 return "Function call argument is an uninitialized value"; 136 } 137 } 138 139 bool CallAndMessageChecker::uninitRefOrPointer(CheckerContext &C, 140 const SVal &V, 141 SourceRange ArgRange, 142 const Expr *ArgEx, 143 std::unique_ptr<BugType> &BT, 144 const ParmVarDecl *ParamDecl, 145 const char *BD) const { 146 if (!Filter.Check_CallAndMessageUnInitRefArg) 147 return false; 148 149 // No parameter declaration available, i.e. variadic function argument. 150 if(!ParamDecl) 151 return false; 152 153 // If parameter is declared as pointer to const in function declaration, 154 // then check if corresponding argument in function call is 155 // pointing to undefined symbol value (uninitialized memory). 156 StringRef Message; 157 158 if (ParamDecl->getType()->isPointerType()) { 159 Message = "Function call argument is a pointer to uninitialized value"; 160 } else if (ParamDecl->getType()->isReferenceType()) { 161 Message = "Function call argument is an uninitialized value"; 162 } else 163 return false; 164 165 if(!ParamDecl->getType()->getPointeeType().isConstQualified()) 166 return false; 167 168 if (const MemRegion *SValMemRegion = V.getAsRegion()) { 169 const ProgramStateRef State = C.getState(); 170 const SVal PSV = State->getSVal(SValMemRegion); 171 if (PSV.isUndef()) { 172 if (ExplodedNode *N = C.generateErrorNode()) { 173 LazyInit_BT(BD, BT); 174 auto R = llvm::make_unique<BugReport>(*BT, Message, N); 175 R->addRange(ArgRange); 176 if (ArgEx) { 177 bugreporter::trackNullOrUndefValue(N, ArgEx, *R); 178 } 179 C.emitReport(std::move(R)); 180 } 181 return true; 182 } 183 } 184 return false; 185 } 186 187 bool CallAndMessageChecker::PreVisitProcessArg(CheckerContext &C, 188 SVal V, 189 SourceRange ArgRange, 190 const Expr *ArgEx, 191 bool IsFirstArgument, 192 bool CheckUninitFields, 193 const CallEvent &Call, 194 std::unique_ptr<BugType> &BT, 195 const ParmVarDecl *ParamDecl 196 ) const { 197 const char *BD = "Uninitialized argument value"; 198 199 if (uninitRefOrPointer(C, V, ArgRange, ArgEx, BT, ParamDecl, BD)) 200 return true; 201 202 if (V.isUndef()) { 203 if (ExplodedNode *N = C.generateErrorNode()) { 204 LazyInit_BT(BD, BT); 205 206 // Generate a report for this bug. 207 StringRef Desc = 208 describeUninitializedArgumentInCall(Call, IsFirstArgument); 209 auto R = llvm::make_unique<BugReport>(*BT, Desc, N); 210 R->addRange(ArgRange); 211 if (ArgEx) 212 bugreporter::trackNullOrUndefValue(N, ArgEx, *R); 213 C.emitReport(std::move(R)); 214 } 215 return true; 216 } 217 218 if (!CheckUninitFields) 219 return false; 220 221 if (Optional<nonloc::LazyCompoundVal> LV = 222 V.getAs<nonloc::LazyCompoundVal>()) { 223 224 class FindUninitializedField { 225 public: 226 SmallVector<const FieldDecl *, 10> FieldChain; 227 private: 228 StoreManager &StoreMgr; 229 MemRegionManager &MrMgr; 230 Store store; 231 public: 232 FindUninitializedField(StoreManager &storeMgr, 233 MemRegionManager &mrMgr, Store s) 234 : StoreMgr(storeMgr), MrMgr(mrMgr), store(s) {} 235 236 bool Find(const TypedValueRegion *R) { 237 QualType T = R->getValueType(); 238 if (const RecordType *RT = T->getAsStructureType()) { 239 const RecordDecl *RD = RT->getDecl()->getDefinition(); 240 assert(RD && "Referred record has no definition"); 241 for (const auto *I : RD->fields()) { 242 const FieldRegion *FR = MrMgr.getFieldRegion(I, R); 243 FieldChain.push_back(I); 244 T = I->getType(); 245 if (T->getAsStructureType()) { 246 if (Find(FR)) 247 return true; 248 } 249 else { 250 const SVal &V = StoreMgr.getBinding(store, loc::MemRegionVal(FR)); 251 if (V.isUndef()) 252 return true; 253 } 254 FieldChain.pop_back(); 255 } 256 } 257 258 return false; 259 } 260 }; 261 262 const LazyCompoundValData *D = LV->getCVData(); 263 FindUninitializedField F(C.getState()->getStateManager().getStoreManager(), 264 C.getSValBuilder().getRegionManager(), 265 D->getStore()); 266 267 if (F.Find(D->getRegion())) { 268 if (ExplodedNode *N = C.generateErrorNode()) { 269 LazyInit_BT(BD, BT); 270 SmallString<512> Str; 271 llvm::raw_svector_ostream os(Str); 272 os << "Passed-by-value struct argument contains uninitialized data"; 273 274 if (F.FieldChain.size() == 1) 275 os << " (e.g., field: '" << *F.FieldChain[0] << "')"; 276 else { 277 os << " (e.g., via the field chain: '"; 278 bool first = true; 279 for (SmallVectorImpl<const FieldDecl *>::iterator 280 DI = F.FieldChain.begin(), DE = F.FieldChain.end(); DI!=DE;++DI){ 281 if (first) 282 first = false; 283 else 284 os << '.'; 285 os << **DI; 286 } 287 os << "')"; 288 } 289 290 // Generate a report for this bug. 291 auto R = llvm::make_unique<BugReport>(*BT, os.str(), N); 292 R->addRange(ArgRange); 293 294 // FIXME: enhance track back for uninitialized value for arbitrary 295 // memregions 296 C.emitReport(std::move(R)); 297 } 298 return true; 299 } 300 } 301 302 return false; 303 } 304 305 void CallAndMessageChecker::checkPreStmt(const CallExpr *CE, 306 CheckerContext &C) const{ 307 308 const Expr *Callee = CE->getCallee()->IgnoreParens(); 309 ProgramStateRef State = C.getState(); 310 const LocationContext *LCtx = C.getLocationContext(); 311 SVal L = State->getSVal(Callee, LCtx); 312 313 if (L.isUndef()) { 314 if (!BT_call_undef) 315 BT_call_undef.reset(new BuiltinBug( 316 this, "Called function pointer is an uninitialized pointer value")); 317 emitBadCall(BT_call_undef.get(), C, Callee); 318 return; 319 } 320 321 ProgramStateRef StNonNull, StNull; 322 std::tie(StNonNull, StNull) = State->assume(L.castAs<DefinedOrUnknownSVal>()); 323 324 if (StNull && !StNonNull) { 325 if (!BT_call_null) 326 BT_call_null.reset(new BuiltinBug( 327 this, "Called function pointer is null (null dereference)")); 328 emitBadCall(BT_call_null.get(), C, Callee); 329 return; 330 } 331 332 C.addTransition(StNonNull); 333 } 334 335 void CallAndMessageChecker::checkPreStmt(const CXXDeleteExpr *DE, 336 CheckerContext &C) const { 337 338 SVal Arg = C.getSVal(DE->getArgument()); 339 if (Arg.isUndef()) { 340 StringRef Desc; 341 ExplodedNode *N = C.generateErrorNode(); 342 if (!N) 343 return; 344 if (!BT_cxx_delete_undef) 345 BT_cxx_delete_undef.reset( 346 new BuiltinBug(this, "Uninitialized argument value")); 347 if (DE->isArrayFormAsWritten()) 348 Desc = "Argument to 'delete[]' is uninitialized"; 349 else 350 Desc = "Argument to 'delete' is uninitialized"; 351 BugType *BT = BT_cxx_delete_undef.get(); 352 auto R = llvm::make_unique<BugReport>(*BT, Desc, N); 353 bugreporter::trackNullOrUndefValue(N, DE, *R); 354 C.emitReport(std::move(R)); 355 return; 356 } 357 } 358 359 void CallAndMessageChecker::checkPreCall(const CallEvent &Call, 360 CheckerContext &C) const { 361 ProgramStateRef State = C.getState(); 362 363 // If this is a call to a C++ method, check if the callee is null or 364 // undefined. 365 if (const CXXInstanceCall *CC = dyn_cast<CXXInstanceCall>(&Call)) { 366 SVal V = CC->getCXXThisVal(); 367 if (V.isUndef()) { 368 if (!BT_cxx_call_undef) 369 BT_cxx_call_undef.reset( 370 new BuiltinBug(this, "Called C++ object pointer is uninitialized")); 371 emitBadCall(BT_cxx_call_undef.get(), C, CC->getCXXThisExpr()); 372 return; 373 } 374 375 ProgramStateRef StNonNull, StNull; 376 std::tie(StNonNull, StNull) = 377 State->assume(V.castAs<DefinedOrUnknownSVal>()); 378 379 if (StNull && !StNonNull) { 380 if (!BT_cxx_call_null) 381 BT_cxx_call_null.reset( 382 new BuiltinBug(this, "Called C++ object pointer is null")); 383 emitBadCall(BT_cxx_call_null.get(), C, CC->getCXXThisExpr()); 384 return; 385 } 386 387 State = StNonNull; 388 } 389 390 const Decl *D = Call.getDecl(); 391 if (D && (isa<FunctionDecl>(D) || isa<BlockDecl>(D))) { 392 // If we have a function or block declaration, we can make sure we pass 393 // enough parameters. 394 unsigned Params = Call.parameters().size(); 395 if (Call.getNumArgs() < Params) { 396 ExplodedNode *N = C.generateErrorNode(); 397 if (!N) 398 return; 399 400 LazyInit_BT("Function call with too few arguments", BT_call_few_args); 401 402 SmallString<512> Str; 403 llvm::raw_svector_ostream os(Str); 404 if (isa<FunctionDecl>(D)) { 405 os << "Function "; 406 } else { 407 assert(isa<BlockDecl>(D)); 408 os << "Block "; 409 } 410 os << "taking " << Params << " argument" 411 << (Params == 1 ? "" : "s") << " is called with fewer (" 412 << Call.getNumArgs() << ")"; 413 414 C.emitReport( 415 llvm::make_unique<BugReport>(*BT_call_few_args, os.str(), N)); 416 } 417 } 418 419 // Don't check for uninitialized field values in arguments if the 420 // caller has a body that is available and we have the chance to inline it. 421 // This is a hack, but is a reasonable compromise betweens sometimes warning 422 // and sometimes not depending on if we decide to inline a function. 423 const bool checkUninitFields = 424 !(C.getAnalysisManager().shouldInlineCall() && (D && D->getBody())); 425 426 std::unique_ptr<BugType> *BT; 427 if (isa<ObjCMethodCall>(Call)) 428 BT = &BT_msg_arg; 429 else 430 BT = &BT_call_arg; 431 432 const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D); 433 for (unsigned i = 0, e = Call.getNumArgs(); i != e; ++i) { 434 const ParmVarDecl *ParamDecl = nullptr; 435 if(FD && i < FD->getNumParams()) 436 ParamDecl = FD->getParamDecl(i); 437 if (PreVisitProcessArg(C, Call.getArgSVal(i), Call.getArgSourceRange(i), 438 Call.getArgExpr(i), /*IsFirstArgument=*/i == 0, 439 checkUninitFields, Call, *BT, ParamDecl)) 440 return; 441 } 442 443 // If we make it here, record our assumptions about the callee. 444 C.addTransition(State); 445 } 446 447 void CallAndMessageChecker::checkPreObjCMessage(const ObjCMethodCall &msg, 448 CheckerContext &C) const { 449 SVal recVal = msg.getReceiverSVal(); 450 if (recVal.isUndef()) { 451 if (ExplodedNode *N = C.generateErrorNode()) { 452 BugType *BT = nullptr; 453 switch (msg.getMessageKind()) { 454 case OCM_Message: 455 if (!BT_msg_undef) 456 BT_msg_undef.reset(new BuiltinBug(this, 457 "Receiver in message expression " 458 "is an uninitialized value")); 459 BT = BT_msg_undef.get(); 460 break; 461 case OCM_PropertyAccess: 462 if (!BT_objc_prop_undef) 463 BT_objc_prop_undef.reset(new BuiltinBug( 464 this, "Property access on an uninitialized object pointer")); 465 BT = BT_objc_prop_undef.get(); 466 break; 467 case OCM_Subscript: 468 if (!BT_objc_subscript_undef) 469 BT_objc_subscript_undef.reset(new BuiltinBug( 470 this, "Subscript access on an uninitialized object pointer")); 471 BT = BT_objc_subscript_undef.get(); 472 break; 473 } 474 assert(BT && "Unknown message kind."); 475 476 auto R = llvm::make_unique<BugReport>(*BT, BT->getName(), N); 477 const ObjCMessageExpr *ME = msg.getOriginExpr(); 478 R->addRange(ME->getReceiverRange()); 479 480 // FIXME: getTrackNullOrUndefValueVisitor can't handle "super" yet. 481 if (const Expr *ReceiverE = ME->getInstanceReceiver()) 482 bugreporter::trackNullOrUndefValue(N, ReceiverE, *R); 483 C.emitReport(std::move(R)); 484 } 485 return; 486 } 487 } 488 489 void CallAndMessageChecker::checkObjCMessageNil(const ObjCMethodCall &msg, 490 CheckerContext &C) const { 491 HandleNilReceiver(C, C.getState(), msg); 492 } 493 494 void CallAndMessageChecker::emitNilReceiverBug(CheckerContext &C, 495 const ObjCMethodCall &msg, 496 ExplodedNode *N) const { 497 498 if (!BT_msg_ret) 499 BT_msg_ret.reset( 500 new BuiltinBug(this, "Receiver in message expression is 'nil'")); 501 502 const ObjCMessageExpr *ME = msg.getOriginExpr(); 503 504 QualType ResTy = msg.getResultType(); 505 506 SmallString<200> buf; 507 llvm::raw_svector_ostream os(buf); 508 os << "The receiver of message '"; 509 ME->getSelector().print(os); 510 os << "' is nil"; 511 if (ResTy->isReferenceType()) { 512 os << ", which results in forming a null reference"; 513 } else { 514 os << " and returns a value of type '"; 515 msg.getResultType().print(os, C.getLangOpts()); 516 os << "' that will be garbage"; 517 } 518 519 auto report = llvm::make_unique<BugReport>(*BT_msg_ret, os.str(), N); 520 report->addRange(ME->getReceiverRange()); 521 // FIXME: This won't track "self" in messages to super. 522 if (const Expr *receiver = ME->getInstanceReceiver()) { 523 bugreporter::trackNullOrUndefValue(N, receiver, *report); 524 } 525 C.emitReport(std::move(report)); 526 } 527 528 static bool supportsNilWithFloatRet(const llvm::Triple &triple) { 529 return (triple.getVendor() == llvm::Triple::Apple && 530 (triple.isiOS() || triple.isWatchOS() || 531 !triple.isMacOSXVersionLT(10,5))); 532 } 533 534 void CallAndMessageChecker::HandleNilReceiver(CheckerContext &C, 535 ProgramStateRef state, 536 const ObjCMethodCall &Msg) const { 537 ASTContext &Ctx = C.getASTContext(); 538 static CheckerProgramPointTag Tag(this, "NilReceiver"); 539 540 // Check the return type of the message expression. A message to nil will 541 // return different values depending on the return type and the architecture. 542 QualType RetTy = Msg.getResultType(); 543 CanQualType CanRetTy = Ctx.getCanonicalType(RetTy); 544 const LocationContext *LCtx = C.getLocationContext(); 545 546 if (CanRetTy->isStructureOrClassType()) { 547 // Structure returns are safe since the compiler zeroes them out. 548 SVal V = C.getSValBuilder().makeZeroVal(RetTy); 549 C.addTransition(state->BindExpr(Msg.getOriginExpr(), LCtx, V), &Tag); 550 return; 551 } 552 553 // Other cases: check if sizeof(return type) > sizeof(void*) 554 if (CanRetTy != Ctx.VoidTy && C.getLocationContext()->getParentMap() 555 .isConsumedExpr(Msg.getOriginExpr())) { 556 // Compute: sizeof(void *) and sizeof(return type) 557 const uint64_t voidPtrSize = Ctx.getTypeSize(Ctx.VoidPtrTy); 558 const uint64_t returnTypeSize = Ctx.getTypeSize(CanRetTy); 559 560 if (CanRetTy.getTypePtr()->isReferenceType()|| 561 (voidPtrSize < returnTypeSize && 562 !(supportsNilWithFloatRet(Ctx.getTargetInfo().getTriple()) && 563 (Ctx.FloatTy == CanRetTy || 564 Ctx.DoubleTy == CanRetTy || 565 Ctx.LongDoubleTy == CanRetTy || 566 Ctx.LongLongTy == CanRetTy || 567 Ctx.UnsignedLongLongTy == CanRetTy)))) { 568 if (ExplodedNode *N = C.generateErrorNode(state, &Tag)) 569 emitNilReceiverBug(C, Msg, N); 570 return; 571 } 572 573 // Handle the safe cases where the return value is 0 if the 574 // receiver is nil. 575 // 576 // FIXME: For now take the conservative approach that we only 577 // return null values if we *know* that the receiver is nil. 578 // This is because we can have surprises like: 579 // 580 // ... = [[NSScreens screens] objectAtIndex:0]; 581 // 582 // What can happen is that [... screens] could return nil, but 583 // it most likely isn't nil. We should assume the semantics 584 // of this case unless we have *a lot* more knowledge. 585 // 586 SVal V = C.getSValBuilder().makeZeroVal(RetTy); 587 C.addTransition(state->BindExpr(Msg.getOriginExpr(), LCtx, V), &Tag); 588 return; 589 } 590 591 C.addTransition(state); 592 } 593 594 #define REGISTER_CHECKER(name) \ 595 void ento::register##name(CheckerManager &mgr) { \ 596 CallAndMessageChecker *Checker = \ 597 mgr.registerChecker<CallAndMessageChecker>(); \ 598 Checker->Filter.Check_##name = true; \ 599 Checker->Filter.CheckName_##name = mgr.getCurrentCheckName(); \ 600 } 601 602 REGISTER_CHECKER(CallAndMessageUnInitRefArg) 603 REGISTER_CHECKER(CallAndMessageChecker) 604