1 //=== MallocChecker.cpp - A malloc/free checker -------------------*- 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 file defines malloc/free checker, which checks for potential memory 11 // leaks, double free, and use-after-free problems. 12 // 13 //===----------------------------------------------------------------------===// 14 15 #include "ClangSACheckers.h" 16 #include "InterCheckerAPI.h" 17 #include "clang/StaticAnalyzer/Core/Checker.h" 18 #include "clang/StaticAnalyzer/Core/CheckerManager.h" 19 #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h" 20 #include "clang/StaticAnalyzer/Core/BugReporter/BugType.h" 21 #include "clang/StaticAnalyzer/Core/PathSensitive/Calls.h" 22 #include "clang/StaticAnalyzer/Core/PathSensitive/ProgramState.h" 23 #include "clang/StaticAnalyzer/Core/PathSensitive/ProgramStateTrait.h" 24 #include "clang/StaticAnalyzer/Core/PathSensitive/SymbolManager.h" 25 #include "clang/Basic/SourceManager.h" 26 #include "llvm/ADT/ImmutableMap.h" 27 #include "llvm/ADT/SmallString.h" 28 #include "llvm/ADT/STLExtras.h" 29 #include <climits> 30 31 using namespace clang; 32 using namespace ento; 33 34 namespace { 35 36 class RefState { 37 enum Kind { // Reference to allocated memory. 38 Allocated, 39 // Reference to released/freed memory. 40 Released, 41 // Reference to escaped memory - no assumptions can be made of 42 // the state after the reference escapes. 43 Escaped, 44 // The responsibility for freeing resources has transfered from 45 // this reference. A relinquished symbol should not be freed. 46 Relinquished } K; 47 const Stmt *S; 48 49 public: 50 RefState(Kind k, const Stmt *s) : K(k), S(s) {} 51 52 bool isAllocated() const { return K == Allocated; } 53 bool isReleased() const { return K == Released; } 54 bool isRelinquished() const { return K == Relinquished; } 55 56 const Stmt *getStmt() const { return S; } 57 58 bool operator==(const RefState &X) const { 59 return K == X.K && S == X.S; 60 } 61 62 static RefState getAllocated(const Stmt *s) { 63 return RefState(Allocated, s); 64 } 65 static RefState getReleased(const Stmt *s) { return RefState(Released, s); } 66 static RefState getEscaped(const Stmt *s) { return RefState(Escaped, s); } 67 static RefState getRelinquished(const Stmt *s) { 68 return RefState(Relinquished, s); 69 } 70 71 void Profile(llvm::FoldingSetNodeID &ID) const { 72 ID.AddInteger(K); 73 ID.AddPointer(S); 74 } 75 }; 76 77 struct ReallocPair { 78 SymbolRef ReallocatedSym; 79 bool IsFreeOnFailure; 80 ReallocPair(SymbolRef S, bool F) : ReallocatedSym(S), IsFreeOnFailure(F) {} 81 void Profile(llvm::FoldingSetNodeID &ID) const { 82 ID.AddInteger(IsFreeOnFailure); 83 ID.AddPointer(ReallocatedSym); 84 } 85 bool operator==(const ReallocPair &X) const { 86 return ReallocatedSym == X.ReallocatedSym && 87 IsFreeOnFailure == X.IsFreeOnFailure; 88 } 89 }; 90 91 typedef std::pair<const Stmt*, const MemRegion*> LeakInfo; 92 93 class MallocChecker : public Checker<check::DeadSymbols, 94 check::EndPath, 95 check::PreStmt<ReturnStmt>, 96 check::PreStmt<CallExpr>, 97 check::PostStmt<CallExpr>, 98 check::PostStmt<BlockExpr>, 99 check::PreObjCMessage, 100 check::Location, 101 check::Bind, 102 eval::Assume, 103 check::RegionChanges> 104 { 105 mutable OwningPtr<BugType> BT_DoubleFree; 106 mutable OwningPtr<BugType> BT_Leak; 107 mutable OwningPtr<BugType> BT_UseFree; 108 mutable OwningPtr<BugType> BT_BadFree; 109 mutable IdentifierInfo *II_malloc, *II_free, *II_realloc, *II_calloc, 110 *II_valloc, *II_reallocf, *II_strndup, *II_strdup; 111 112 public: 113 MallocChecker() : II_malloc(0), II_free(0), II_realloc(0), II_calloc(0), 114 II_valloc(0), II_reallocf(0), II_strndup(0), II_strdup(0) {} 115 116 /// In pessimistic mode, the checker assumes that it does not know which 117 /// functions might free the memory. 118 struct ChecksFilter { 119 DefaultBool CMallocPessimistic; 120 DefaultBool CMallocOptimistic; 121 }; 122 123 ChecksFilter Filter; 124 125 void checkPreStmt(const CallExpr *S, CheckerContext &C) const; 126 void checkPostStmt(const CallExpr *CE, CheckerContext &C) const; 127 void checkPreObjCMessage(const ObjCMessage &Msg, CheckerContext &C) const; 128 void checkPostStmt(const BlockExpr *BE, CheckerContext &C) const; 129 void checkDeadSymbols(SymbolReaper &SymReaper, CheckerContext &C) const; 130 void checkEndPath(CheckerContext &C) const; 131 void checkPreStmt(const ReturnStmt *S, CheckerContext &C) const; 132 ProgramStateRef evalAssume(ProgramStateRef state, SVal Cond, 133 bool Assumption) const; 134 void checkLocation(SVal l, bool isLoad, const Stmt *S, 135 CheckerContext &C) const; 136 void checkBind(SVal location, SVal val, const Stmt*S, 137 CheckerContext &C) const; 138 ProgramStateRef 139 checkRegionChanges(ProgramStateRef state, 140 const StoreManager::InvalidatedSymbols *invalidated, 141 ArrayRef<const MemRegion *> ExplicitRegions, 142 ArrayRef<const MemRegion *> Regions, 143 const CallEvent *Call) const; 144 bool wantsRegionChangeUpdate(ProgramStateRef state) const { 145 return true; 146 } 147 148 void printState(raw_ostream &Out, ProgramStateRef State, 149 const char *NL, const char *Sep) const; 150 151 private: 152 void initIdentifierInfo(ASTContext &C) const; 153 154 /// Check if this is one of the functions which can allocate/reallocate memory 155 /// pointed to by one of its arguments. 156 bool isMemFunction(const FunctionDecl *FD, ASTContext &C) const; 157 bool isFreeFunction(const FunctionDecl *FD, ASTContext &C) const; 158 bool isAllocationFunction(const FunctionDecl *FD, ASTContext &C) const; 159 160 static ProgramStateRef MallocMemReturnsAttr(CheckerContext &C, 161 const CallExpr *CE, 162 const OwnershipAttr* Att); 163 static ProgramStateRef MallocMemAux(CheckerContext &C, const CallExpr *CE, 164 const Expr *SizeEx, SVal Init, 165 ProgramStateRef state) { 166 return MallocMemAux(C, CE, 167 state->getSVal(SizeEx, C.getLocationContext()), 168 Init, state); 169 } 170 171 static ProgramStateRef MallocMemAux(CheckerContext &C, const CallExpr *CE, 172 SVal SizeEx, SVal Init, 173 ProgramStateRef state); 174 175 /// Update the RefState to reflect the new memory allocation. 176 static ProgramStateRef MallocUpdateRefState(CheckerContext &C, 177 const CallExpr *CE, 178 ProgramStateRef state); 179 180 ProgramStateRef FreeMemAttr(CheckerContext &C, const CallExpr *CE, 181 const OwnershipAttr* Att) const; 182 ProgramStateRef FreeMemAux(CheckerContext &C, const CallExpr *CE, 183 ProgramStateRef state, unsigned Num, 184 bool Hold) const; 185 ProgramStateRef FreeMemAux(CheckerContext &C, const Expr *Arg, 186 const Expr *ParentExpr, 187 ProgramStateRef state, 188 bool Hold) const; 189 190 ProgramStateRef ReallocMem(CheckerContext &C, const CallExpr *CE, 191 bool FreesMemOnFailure) const; 192 static ProgramStateRef CallocMem(CheckerContext &C, const CallExpr *CE); 193 194 ///\brief Check if the memory associated with this symbol was released. 195 bool isReleased(SymbolRef Sym, CheckerContext &C) const; 196 197 bool checkEscape(SymbolRef Sym, const Stmt *S, CheckerContext &C) const; 198 bool checkUseAfterFree(SymbolRef Sym, CheckerContext &C, 199 const Stmt *S = 0) const; 200 201 /// Check if the function is not known to us. So, for example, we could 202 /// conservatively assume it can free/reallocate it's pointer arguments. 203 bool doesNotFreeMemory(const CallEvent *Call, 204 ProgramStateRef State) const; 205 206 static bool SummarizeValue(raw_ostream &os, SVal V); 207 static bool SummarizeRegion(raw_ostream &os, const MemRegion *MR); 208 void ReportBadFree(CheckerContext &C, SVal ArgVal, SourceRange range) const; 209 210 /// Find the location of the allocation for Sym on the path leading to the 211 /// exploded node N. 212 LeakInfo getAllocationSite(const ExplodedNode *N, SymbolRef Sym, 213 CheckerContext &C) const; 214 215 void reportLeak(SymbolRef Sym, ExplodedNode *N, CheckerContext &C) const; 216 217 /// The bug visitor which allows us to print extra diagnostics along the 218 /// BugReport path. For example, showing the allocation site of the leaked 219 /// region. 220 class MallocBugVisitor : public BugReporterVisitorImpl<MallocBugVisitor> { 221 protected: 222 enum NotificationMode { 223 Normal, 224 ReallocationFailed 225 }; 226 227 // The allocated region symbol tracked by the main analysis. 228 SymbolRef Sym; 229 230 // The mode we are in, i.e. what kind of diagnostics will be emitted. 231 NotificationMode Mode; 232 233 // A symbol from when the primary region should have been reallocated. 234 SymbolRef FailedReallocSymbol; 235 236 bool IsLeak; 237 238 public: 239 MallocBugVisitor(SymbolRef S, bool isLeak = false) 240 : Sym(S), Mode(Normal), FailedReallocSymbol(0), IsLeak(isLeak) {} 241 242 virtual ~MallocBugVisitor() {} 243 244 void Profile(llvm::FoldingSetNodeID &ID) const { 245 static int X = 0; 246 ID.AddPointer(&X); 247 ID.AddPointer(Sym); 248 } 249 250 inline bool isAllocated(const RefState *S, const RefState *SPrev, 251 const Stmt *Stmt) { 252 // Did not track -> allocated. Other state (released) -> allocated. 253 return (Stmt && isa<CallExpr>(Stmt) && 254 (S && S->isAllocated()) && (!SPrev || !SPrev->isAllocated())); 255 } 256 257 inline bool isReleased(const RefState *S, const RefState *SPrev, 258 const Stmt *Stmt) { 259 // Did not track -> released. Other state (allocated) -> released. 260 return (Stmt && isa<CallExpr>(Stmt) && 261 (S && S->isReleased()) && (!SPrev || !SPrev->isReleased())); 262 } 263 264 inline bool isRelinquished(const RefState *S, const RefState *SPrev, 265 const Stmt *Stmt) { 266 // Did not track -> relinquished. Other state (allocated) -> relinquished. 267 return (Stmt && (isa<CallExpr>(Stmt) || isa<ObjCMessageExpr>(Stmt) || 268 isa<ObjCPropertyRefExpr>(Stmt)) && 269 (S && S->isRelinquished()) && 270 (!SPrev || !SPrev->isRelinquished())); 271 } 272 273 inline bool isReallocFailedCheck(const RefState *S, const RefState *SPrev, 274 const Stmt *Stmt) { 275 // If the expression is not a call, and the state change is 276 // released -> allocated, it must be the realloc return value 277 // check. If we have to handle more cases here, it might be cleaner just 278 // to track this extra bit in the state itself. 279 return ((!Stmt || !isa<CallExpr>(Stmt)) && 280 (S && S->isAllocated()) && (SPrev && !SPrev->isAllocated())); 281 } 282 283 PathDiagnosticPiece *VisitNode(const ExplodedNode *N, 284 const ExplodedNode *PrevN, 285 BugReporterContext &BRC, 286 BugReport &BR); 287 288 PathDiagnosticPiece* getEndPath(BugReporterContext &BRC, 289 const ExplodedNode *EndPathNode, 290 BugReport &BR) { 291 if (!IsLeak) 292 return 0; 293 294 PathDiagnosticLocation L = 295 PathDiagnosticLocation::createEndOfPath(EndPathNode, 296 BRC.getSourceManager()); 297 // Do not add the statement itself as a range in case of leak. 298 return new PathDiagnosticEventPiece(L, BR.getDescription(), false); 299 } 300 301 private: 302 class StackHintGeneratorForReallocationFailed 303 : public StackHintGeneratorForSymbol { 304 public: 305 StackHintGeneratorForReallocationFailed(SymbolRef S, StringRef M) 306 : StackHintGeneratorForSymbol(S, M) {} 307 308 virtual std::string getMessageForArg(const Expr *ArgE, unsigned ArgIndex) { 309 SmallString<200> buf; 310 llvm::raw_svector_ostream os(buf); 311 312 os << "Reallocation of "; 313 // Printed parameters start at 1, not 0. 314 printOrdinal(++ArgIndex, os); 315 os << " parameter failed"; 316 317 return os.str(); 318 } 319 320 virtual std::string getMessageForReturn(const CallExpr *CallExpr) { 321 return "Reallocation of returned value failed"; 322 } 323 }; 324 }; 325 }; 326 } // end anonymous namespace 327 328 typedef llvm::ImmutableMap<SymbolRef, RefState> RegionStateTy; 329 typedef llvm::ImmutableMap<SymbolRef, ReallocPair > ReallocMap; 330 class RegionState {}; 331 class ReallocPairs {}; 332 namespace clang { 333 namespace ento { 334 template <> 335 struct ProgramStateTrait<RegionState> 336 : public ProgramStatePartialTrait<RegionStateTy> { 337 static void *GDMIndex() { static int x; return &x; } 338 }; 339 340 template <> 341 struct ProgramStateTrait<ReallocPairs> 342 : public ProgramStatePartialTrait<ReallocMap> { 343 static void *GDMIndex() { static int x; return &x; } 344 }; 345 } 346 } 347 348 namespace { 349 class StopTrackingCallback : public SymbolVisitor { 350 ProgramStateRef state; 351 public: 352 StopTrackingCallback(ProgramStateRef st) : state(st) {} 353 ProgramStateRef getState() const { return state; } 354 355 bool VisitSymbol(SymbolRef sym) { 356 state = state->remove<RegionState>(sym); 357 return true; 358 } 359 }; 360 } // end anonymous namespace 361 362 void MallocChecker::initIdentifierInfo(ASTContext &Ctx) const { 363 if (II_malloc) 364 return; 365 II_malloc = &Ctx.Idents.get("malloc"); 366 II_free = &Ctx.Idents.get("free"); 367 II_realloc = &Ctx.Idents.get("realloc"); 368 II_reallocf = &Ctx.Idents.get("reallocf"); 369 II_calloc = &Ctx.Idents.get("calloc"); 370 II_valloc = &Ctx.Idents.get("valloc"); 371 II_strdup = &Ctx.Idents.get("strdup"); 372 II_strndup = &Ctx.Idents.get("strndup"); 373 } 374 375 bool MallocChecker::isMemFunction(const FunctionDecl *FD, ASTContext &C) const { 376 if (isFreeFunction(FD, C)) 377 return true; 378 379 if (isAllocationFunction(FD, C)) 380 return true; 381 382 return false; 383 } 384 385 bool MallocChecker::isAllocationFunction(const FunctionDecl *FD, 386 ASTContext &C) const { 387 if (!FD) 388 return false; 389 390 IdentifierInfo *FunI = FD->getIdentifier(); 391 if (!FunI) 392 return false; 393 394 initIdentifierInfo(C); 395 396 if (FunI == II_malloc || FunI == II_realloc || 397 FunI == II_reallocf || FunI == II_calloc || FunI == II_valloc || 398 FunI == II_strdup || FunI == II_strndup) 399 return true; 400 401 if (Filter.CMallocOptimistic && FD->hasAttrs()) 402 for (specific_attr_iterator<OwnershipAttr> 403 i = FD->specific_attr_begin<OwnershipAttr>(), 404 e = FD->specific_attr_end<OwnershipAttr>(); 405 i != e; ++i) 406 if ((*i)->getOwnKind() == OwnershipAttr::Returns) 407 return true; 408 return false; 409 } 410 411 bool MallocChecker::isFreeFunction(const FunctionDecl *FD, ASTContext &C) const { 412 if (!FD) 413 return false; 414 415 IdentifierInfo *FunI = FD->getIdentifier(); 416 if (!FunI) 417 return false; 418 419 initIdentifierInfo(C); 420 421 if (FunI == II_free || FunI == II_realloc || FunI == II_reallocf) 422 return true; 423 424 if (Filter.CMallocOptimistic && FD->hasAttrs()) 425 for (specific_attr_iterator<OwnershipAttr> 426 i = FD->specific_attr_begin<OwnershipAttr>(), 427 e = FD->specific_attr_end<OwnershipAttr>(); 428 i != e; ++i) 429 if ((*i)->getOwnKind() == OwnershipAttr::Takes || 430 (*i)->getOwnKind() == OwnershipAttr::Holds) 431 return true; 432 return false; 433 } 434 435 void MallocChecker::checkPostStmt(const CallExpr *CE, CheckerContext &C) const { 436 const FunctionDecl *FD = C.getCalleeDecl(CE); 437 if (!FD) 438 return; 439 440 initIdentifierInfo(C.getASTContext()); 441 IdentifierInfo *FunI = FD->getIdentifier(); 442 if (!FunI) 443 return; 444 445 ProgramStateRef State = C.getState(); 446 if (FunI == II_malloc || FunI == II_valloc) { 447 if (CE->getNumArgs() < 1) 448 return; 449 State = MallocMemAux(C, CE, CE->getArg(0), UndefinedVal(), State); 450 } else if (FunI == II_realloc) { 451 State = ReallocMem(C, CE, false); 452 } else if (FunI == II_reallocf) { 453 State = ReallocMem(C, CE, true); 454 } else if (FunI == II_calloc) { 455 State = CallocMem(C, CE); 456 } else if (FunI == II_free) { 457 State = FreeMemAux(C, CE, C.getState(), 0, false); 458 } else if (FunI == II_strdup) { 459 State = MallocUpdateRefState(C, CE, State); 460 } else if (FunI == II_strndup) { 461 State = MallocUpdateRefState(C, CE, State); 462 } else if (Filter.CMallocOptimistic) { 463 // Check all the attributes, if there are any. 464 // There can be multiple of these attributes. 465 if (FD->hasAttrs()) 466 for (specific_attr_iterator<OwnershipAttr> 467 i = FD->specific_attr_begin<OwnershipAttr>(), 468 e = FD->specific_attr_end<OwnershipAttr>(); 469 i != e; ++i) { 470 switch ((*i)->getOwnKind()) { 471 case OwnershipAttr::Returns: 472 State = MallocMemReturnsAttr(C, CE, *i); 473 break; 474 case OwnershipAttr::Takes: 475 case OwnershipAttr::Holds: 476 State = FreeMemAttr(C, CE, *i); 477 break; 478 } 479 } 480 } 481 C.addTransition(State); 482 } 483 484 static bool isFreeWhenDoneSetToZero(const ObjCMessageInvocation &Call, 485 Selector &S) { 486 for (unsigned i = 1; i < S.getNumArgs(); ++i) 487 if (S.getNameForSlot(i).equals("freeWhenDone")) 488 if (Call.getArgSVal(i).isConstant(0)) 489 return true; 490 491 return false; 492 } 493 494 void MallocChecker::checkPreObjCMessage(const ObjCMessage &Msg, 495 CheckerContext &C) const { 496 const ObjCMethodDecl *MD = Msg.getMethodDecl(); 497 if (!MD) 498 return; 499 500 ObjCMessageInvocation Call(Msg, C.getState(), C.getLocationContext()); 501 Selector S = Msg.getSelector(); 502 503 // If the first selector is dataWithBytesNoCopy, assume that the memory will 504 // be released with 'free' by the new object. 505 // Ex: [NSData dataWithBytesNoCopy:bytes length:10]; 506 // Unless 'freeWhenDone' param set to 0. 507 // TODO: Check that the memory was allocated with malloc. 508 if ((S.getNameForSlot(0) == "dataWithBytesNoCopy" || 509 S.getNameForSlot(0) == "initWithBytesNoCopy" || 510 S.getNameForSlot(0) == "initWithCharactersNoCopy") && 511 !isFreeWhenDoneSetToZero(Call, S)){ 512 unsigned int argIdx = 0; 513 C.addTransition(FreeMemAux(C, Call.getArgExpr(argIdx), 514 Msg.getMessageExpr(), C.getState(), true)); 515 } 516 } 517 518 ProgramStateRef MallocChecker::MallocMemReturnsAttr(CheckerContext &C, 519 const CallExpr *CE, 520 const OwnershipAttr* Att) { 521 if (Att->getModule() != "malloc") 522 return 0; 523 524 OwnershipAttr::args_iterator I = Att->args_begin(), E = Att->args_end(); 525 if (I != E) { 526 return MallocMemAux(C, CE, CE->getArg(*I), UndefinedVal(), C.getState()); 527 } 528 return MallocMemAux(C, CE, UnknownVal(), UndefinedVal(), C.getState()); 529 } 530 531 ProgramStateRef MallocChecker::MallocMemAux(CheckerContext &C, 532 const CallExpr *CE, 533 SVal Size, SVal Init, 534 ProgramStateRef state) { 535 536 // Bind the return value to the symbolic value from the heap region. 537 // TODO: We could rewrite post visit to eval call; 'malloc' does not have 538 // side effects other than what we model here. 539 unsigned Count = C.getCurrentBlockCount(); 540 SValBuilder &svalBuilder = C.getSValBuilder(); 541 const LocationContext *LCtx = C.getPredecessor()->getLocationContext(); 542 DefinedSVal RetVal = 543 cast<DefinedSVal>(svalBuilder.getConjuredHeapSymbolVal(CE, LCtx, Count)); 544 state = state->BindExpr(CE, C.getLocationContext(), RetVal); 545 546 // We expect the malloc functions to return a pointer. 547 if (!isa<Loc>(RetVal)) 548 return 0; 549 550 // Fill the region with the initialization value. 551 state = state->bindDefault(RetVal, Init); 552 553 // Set the region's extent equal to the Size parameter. 554 const SymbolicRegion *R = 555 dyn_cast_or_null<SymbolicRegion>(RetVal.getAsRegion()); 556 if (!R) 557 return 0; 558 if (isa<DefinedOrUnknownSVal>(Size)) { 559 SValBuilder &svalBuilder = C.getSValBuilder(); 560 DefinedOrUnknownSVal Extent = R->getExtent(svalBuilder); 561 DefinedOrUnknownSVal DefinedSize = cast<DefinedOrUnknownSVal>(Size); 562 DefinedOrUnknownSVal extentMatchesSize = 563 svalBuilder.evalEQ(state, Extent, DefinedSize); 564 565 state = state->assume(extentMatchesSize, true); 566 assert(state); 567 } 568 569 return MallocUpdateRefState(C, CE, state); 570 } 571 572 ProgramStateRef MallocChecker::MallocUpdateRefState(CheckerContext &C, 573 const CallExpr *CE, 574 ProgramStateRef state) { 575 // Get the return value. 576 SVal retVal = state->getSVal(CE, C.getLocationContext()); 577 578 // We expect the malloc functions to return a pointer. 579 if (!isa<Loc>(retVal)) 580 return 0; 581 582 SymbolRef Sym = retVal.getAsLocSymbol(); 583 assert(Sym); 584 585 // Set the symbol's state to Allocated. 586 return state->set<RegionState>(Sym, RefState::getAllocated(CE)); 587 588 } 589 590 ProgramStateRef MallocChecker::FreeMemAttr(CheckerContext &C, 591 const CallExpr *CE, 592 const OwnershipAttr* Att) const { 593 if (Att->getModule() != "malloc") 594 return 0; 595 596 ProgramStateRef State = C.getState(); 597 598 for (OwnershipAttr::args_iterator I = Att->args_begin(), E = Att->args_end(); 599 I != E; ++I) { 600 ProgramStateRef StateI = FreeMemAux(C, CE, State, *I, 601 Att->getOwnKind() == OwnershipAttr::Holds); 602 if (StateI) 603 State = StateI; 604 } 605 return State; 606 } 607 608 ProgramStateRef MallocChecker::FreeMemAux(CheckerContext &C, 609 const CallExpr *CE, 610 ProgramStateRef state, 611 unsigned Num, 612 bool Hold) const { 613 if (CE->getNumArgs() < (Num + 1)) 614 return 0; 615 616 return FreeMemAux(C, CE->getArg(Num), CE, state, Hold); 617 } 618 619 ProgramStateRef MallocChecker::FreeMemAux(CheckerContext &C, 620 const Expr *ArgExpr, 621 const Expr *ParentExpr, 622 ProgramStateRef state, 623 bool Hold) const { 624 625 SVal ArgVal = state->getSVal(ArgExpr, C.getLocationContext()); 626 if (!isa<DefinedOrUnknownSVal>(ArgVal)) 627 return 0; 628 DefinedOrUnknownSVal location = cast<DefinedOrUnknownSVal>(ArgVal); 629 630 // Check for null dereferences. 631 if (!isa<Loc>(location)) 632 return 0; 633 634 // The explicit NULL case, no operation is performed. 635 ProgramStateRef notNullState, nullState; 636 llvm::tie(notNullState, nullState) = state->assume(location); 637 if (nullState && !notNullState) 638 return 0; 639 640 // Unknown values could easily be okay 641 // Undefined values are handled elsewhere 642 if (ArgVal.isUnknownOrUndef()) 643 return 0; 644 645 const MemRegion *R = ArgVal.getAsRegion(); 646 647 // Nonlocs can't be freed, of course. 648 // Non-region locations (labels and fixed addresses) also shouldn't be freed. 649 if (!R) { 650 ReportBadFree(C, ArgVal, ArgExpr->getSourceRange()); 651 return 0; 652 } 653 654 R = R->StripCasts(); 655 656 // Blocks might show up as heap data, but should not be free()d 657 if (isa<BlockDataRegion>(R)) { 658 ReportBadFree(C, ArgVal, ArgExpr->getSourceRange()); 659 return 0; 660 } 661 662 const MemSpaceRegion *MS = R->getMemorySpace(); 663 664 // Parameters, locals, statics, and globals shouldn't be freed. 665 if (!(isa<UnknownSpaceRegion>(MS) || isa<HeapSpaceRegion>(MS))) { 666 // FIXME: at the time this code was written, malloc() regions were 667 // represented by conjured symbols, which are all in UnknownSpaceRegion. 668 // This means that there isn't actually anything from HeapSpaceRegion 669 // that should be freed, even though we allow it here. 670 // Of course, free() can work on memory allocated outside the current 671 // function, so UnknownSpaceRegion is always a possibility. 672 // False negatives are better than false positives. 673 674 ReportBadFree(C, ArgVal, ArgExpr->getSourceRange()); 675 return 0; 676 } 677 678 const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(R); 679 // Various cases could lead to non-symbol values here. 680 // For now, ignore them. 681 if (!SR) 682 return 0; 683 684 SymbolRef Sym = SR->getSymbol(); 685 const RefState *RS = state->get<RegionState>(Sym); 686 687 // If the symbol has not been tracked, return. This is possible when free() is 688 // called on a pointer that does not get its pointee directly from malloc(). 689 // Full support of this requires inter-procedural analysis. 690 if (!RS) 691 return 0; 692 693 // Check double free. 694 if (RS->isReleased() || RS->isRelinquished()) { 695 if (ExplodedNode *N = C.generateSink()) { 696 if (!BT_DoubleFree) 697 BT_DoubleFree.reset( 698 new BugType("Double free", "Memory Error")); 699 BugReport *R = new BugReport(*BT_DoubleFree, 700 (RS->isReleased() ? "Attempt to free released memory" : 701 "Attempt to free non-owned memory"), N); 702 R->addRange(ArgExpr->getSourceRange()); 703 R->markInteresting(Sym); 704 R->addVisitor(new MallocBugVisitor(Sym)); 705 C.EmitReport(R); 706 } 707 return 0; 708 } 709 710 // Normal free. 711 if (Hold) 712 return state->set<RegionState>(Sym, RefState::getRelinquished(ParentExpr)); 713 return state->set<RegionState>(Sym, RefState::getReleased(ParentExpr)); 714 } 715 716 bool MallocChecker::SummarizeValue(raw_ostream &os, SVal V) { 717 if (nonloc::ConcreteInt *IntVal = dyn_cast<nonloc::ConcreteInt>(&V)) 718 os << "an integer (" << IntVal->getValue() << ")"; 719 else if (loc::ConcreteInt *ConstAddr = dyn_cast<loc::ConcreteInt>(&V)) 720 os << "a constant address (" << ConstAddr->getValue() << ")"; 721 else if (loc::GotoLabel *Label = dyn_cast<loc::GotoLabel>(&V)) 722 os << "the address of the label '" << Label->getLabel()->getName() << "'"; 723 else 724 return false; 725 726 return true; 727 } 728 729 bool MallocChecker::SummarizeRegion(raw_ostream &os, 730 const MemRegion *MR) { 731 switch (MR->getKind()) { 732 case MemRegion::FunctionTextRegionKind: { 733 const FunctionDecl *FD = cast<FunctionTextRegion>(MR)->getDecl(); 734 if (FD) 735 os << "the address of the function '" << *FD << '\''; 736 else 737 os << "the address of a function"; 738 return true; 739 } 740 case MemRegion::BlockTextRegionKind: 741 os << "block text"; 742 return true; 743 case MemRegion::BlockDataRegionKind: 744 // FIXME: where the block came from? 745 os << "a block"; 746 return true; 747 default: { 748 const MemSpaceRegion *MS = MR->getMemorySpace(); 749 750 if (isa<StackLocalsSpaceRegion>(MS)) { 751 const VarRegion *VR = dyn_cast<VarRegion>(MR); 752 const VarDecl *VD; 753 if (VR) 754 VD = VR->getDecl(); 755 else 756 VD = NULL; 757 758 if (VD) 759 os << "the address of the local variable '" << VD->getName() << "'"; 760 else 761 os << "the address of a local stack variable"; 762 return true; 763 } 764 765 if (isa<StackArgumentsSpaceRegion>(MS)) { 766 const VarRegion *VR = dyn_cast<VarRegion>(MR); 767 const VarDecl *VD; 768 if (VR) 769 VD = VR->getDecl(); 770 else 771 VD = NULL; 772 773 if (VD) 774 os << "the address of the parameter '" << VD->getName() << "'"; 775 else 776 os << "the address of a parameter"; 777 return true; 778 } 779 780 if (isa<GlobalsSpaceRegion>(MS)) { 781 const VarRegion *VR = dyn_cast<VarRegion>(MR); 782 const VarDecl *VD; 783 if (VR) 784 VD = VR->getDecl(); 785 else 786 VD = NULL; 787 788 if (VD) { 789 if (VD->isStaticLocal()) 790 os << "the address of the static variable '" << VD->getName() << "'"; 791 else 792 os << "the address of the global variable '" << VD->getName() << "'"; 793 } else 794 os << "the address of a global variable"; 795 return true; 796 } 797 798 return false; 799 } 800 } 801 } 802 803 void MallocChecker::ReportBadFree(CheckerContext &C, SVal ArgVal, 804 SourceRange range) const { 805 if (ExplodedNode *N = C.generateSink()) { 806 if (!BT_BadFree) 807 BT_BadFree.reset(new BugType("Bad free", "Memory Error")); 808 809 SmallString<100> buf; 810 llvm::raw_svector_ostream os(buf); 811 812 const MemRegion *MR = ArgVal.getAsRegion(); 813 if (MR) { 814 while (const ElementRegion *ER = dyn_cast<ElementRegion>(MR)) 815 MR = ER->getSuperRegion(); 816 817 // Special case for alloca() 818 if (isa<AllocaRegion>(MR)) 819 os << "Argument to free() was allocated by alloca(), not malloc()"; 820 else { 821 os << "Argument to free() is "; 822 if (SummarizeRegion(os, MR)) 823 os << ", which is not memory allocated by malloc()"; 824 else 825 os << "not memory allocated by malloc()"; 826 } 827 } else { 828 os << "Argument to free() is "; 829 if (SummarizeValue(os, ArgVal)) 830 os << ", which is not memory allocated by malloc()"; 831 else 832 os << "not memory allocated by malloc()"; 833 } 834 835 BugReport *R = new BugReport(*BT_BadFree, os.str(), N); 836 R->markInteresting(MR); 837 R->addRange(range); 838 C.EmitReport(R); 839 } 840 } 841 842 ProgramStateRef MallocChecker::ReallocMem(CheckerContext &C, 843 const CallExpr *CE, 844 bool FreesOnFail) const { 845 if (CE->getNumArgs() < 2) 846 return 0; 847 848 ProgramStateRef state = C.getState(); 849 const Expr *arg0Expr = CE->getArg(0); 850 const LocationContext *LCtx = C.getLocationContext(); 851 SVal Arg0Val = state->getSVal(arg0Expr, LCtx); 852 if (!isa<DefinedOrUnknownSVal>(Arg0Val)) 853 return 0; 854 DefinedOrUnknownSVal arg0Val = cast<DefinedOrUnknownSVal>(Arg0Val); 855 856 SValBuilder &svalBuilder = C.getSValBuilder(); 857 858 DefinedOrUnknownSVal PtrEQ = 859 svalBuilder.evalEQ(state, arg0Val, svalBuilder.makeNull()); 860 861 // Get the size argument. If there is no size arg then give up. 862 const Expr *Arg1 = CE->getArg(1); 863 if (!Arg1) 864 return 0; 865 866 // Get the value of the size argument. 867 SVal Arg1ValG = state->getSVal(Arg1, LCtx); 868 if (!isa<DefinedOrUnknownSVal>(Arg1ValG)) 869 return 0; 870 DefinedOrUnknownSVal Arg1Val = cast<DefinedOrUnknownSVal>(Arg1ValG); 871 872 // Compare the size argument to 0. 873 DefinedOrUnknownSVal SizeZero = 874 svalBuilder.evalEQ(state, Arg1Val, 875 svalBuilder.makeIntValWithPtrWidth(0, false)); 876 877 ProgramStateRef StatePtrIsNull, StatePtrNotNull; 878 llvm::tie(StatePtrIsNull, StatePtrNotNull) = state->assume(PtrEQ); 879 ProgramStateRef StateSizeIsZero, StateSizeNotZero; 880 llvm::tie(StateSizeIsZero, StateSizeNotZero) = state->assume(SizeZero); 881 // We only assume exceptional states if they are definitely true; if the 882 // state is under-constrained, assume regular realloc behavior. 883 bool PrtIsNull = StatePtrIsNull && !StatePtrNotNull; 884 bool SizeIsZero = StateSizeIsZero && !StateSizeNotZero; 885 886 // If the ptr is NULL and the size is not 0, the call is equivalent to 887 // malloc(size). 888 if ( PrtIsNull && !SizeIsZero) { 889 ProgramStateRef stateMalloc = MallocMemAux(C, CE, CE->getArg(1), 890 UndefinedVal(), StatePtrIsNull); 891 return stateMalloc; 892 } 893 894 if (PrtIsNull && SizeIsZero) 895 return 0; 896 897 // Get the from and to pointer symbols as in toPtr = realloc(fromPtr, size). 898 assert(!PrtIsNull); 899 SymbolRef FromPtr = arg0Val.getAsSymbol(); 900 SVal RetVal = state->getSVal(CE, LCtx); 901 SymbolRef ToPtr = RetVal.getAsSymbol(); 902 if (!FromPtr || !ToPtr) 903 return 0; 904 905 // If the size is 0, free the memory. 906 if (SizeIsZero) 907 if (ProgramStateRef stateFree = FreeMemAux(C, CE, StateSizeIsZero,0,false)){ 908 // The semantics of the return value are: 909 // If size was equal to 0, either NULL or a pointer suitable to be passed 910 // to free() is returned. 911 stateFree = stateFree->set<ReallocPairs>(ToPtr, 912 ReallocPair(FromPtr, FreesOnFail)); 913 C.getSymbolManager().addSymbolDependency(ToPtr, FromPtr); 914 return stateFree; 915 } 916 917 // Default behavior. 918 if (ProgramStateRef stateFree = FreeMemAux(C, CE, state, 0, false)) { 919 // FIXME: We should copy the content of the original buffer. 920 ProgramStateRef stateRealloc = MallocMemAux(C, CE, CE->getArg(1), 921 UnknownVal(), stateFree); 922 if (!stateRealloc) 923 return 0; 924 stateRealloc = stateRealloc->set<ReallocPairs>(ToPtr, 925 ReallocPair(FromPtr, FreesOnFail)); 926 C.getSymbolManager().addSymbolDependency(ToPtr, FromPtr); 927 return stateRealloc; 928 } 929 return 0; 930 } 931 932 ProgramStateRef MallocChecker::CallocMem(CheckerContext &C, const CallExpr *CE){ 933 if (CE->getNumArgs() < 2) 934 return 0; 935 936 ProgramStateRef state = C.getState(); 937 SValBuilder &svalBuilder = C.getSValBuilder(); 938 const LocationContext *LCtx = C.getLocationContext(); 939 SVal count = state->getSVal(CE->getArg(0), LCtx); 940 SVal elementSize = state->getSVal(CE->getArg(1), LCtx); 941 SVal TotalSize = svalBuilder.evalBinOp(state, BO_Mul, count, elementSize, 942 svalBuilder.getContext().getSizeType()); 943 SVal zeroVal = svalBuilder.makeZeroVal(svalBuilder.getContext().CharTy); 944 945 return MallocMemAux(C, CE, TotalSize, zeroVal, state); 946 } 947 948 LeakInfo 949 MallocChecker::getAllocationSite(const ExplodedNode *N, SymbolRef Sym, 950 CheckerContext &C) const { 951 const LocationContext *LeakContext = N->getLocationContext(); 952 // Walk the ExplodedGraph backwards and find the first node that referred to 953 // the tracked symbol. 954 const ExplodedNode *AllocNode = N; 955 const MemRegion *ReferenceRegion = 0; 956 957 while (N) { 958 ProgramStateRef State = N->getState(); 959 if (!State->get<RegionState>(Sym)) 960 break; 961 962 // Find the most recent expression bound to the symbol in the current 963 // context. 964 if (!ReferenceRegion) { 965 if (const MemRegion *MR = C.getLocationRegionIfPostStore(N)) { 966 SVal Val = State->getSVal(MR); 967 if (Val.getAsLocSymbol() == Sym) 968 ReferenceRegion = MR; 969 } 970 } 971 972 // Allocation node, is the last node in the current context in which the 973 // symbol was tracked. 974 if (N->getLocationContext() == LeakContext) 975 AllocNode = N; 976 N = N->pred_empty() ? NULL : *(N->pred_begin()); 977 } 978 979 ProgramPoint P = AllocNode->getLocation(); 980 const Stmt *AllocationStmt = 0; 981 if (isa<StmtPoint>(P)) 982 AllocationStmt = cast<StmtPoint>(P).getStmt(); 983 984 return LeakInfo(AllocationStmt, ReferenceRegion); 985 } 986 987 void MallocChecker::reportLeak(SymbolRef Sym, ExplodedNode *N, 988 CheckerContext &C) const { 989 assert(N); 990 if (!BT_Leak) { 991 BT_Leak.reset(new BugType("Memory leak", "Memory Error")); 992 // Leaks should not be reported if they are post-dominated by a sink: 993 // (1) Sinks are higher importance bugs. 994 // (2) NoReturnFunctionChecker uses sink nodes to represent paths ending 995 // with __noreturn functions such as assert() or exit(). We choose not 996 // to report leaks on such paths. 997 BT_Leak->setSuppressOnSink(true); 998 } 999 1000 // Most bug reports are cached at the location where they occurred. 1001 // With leaks, we want to unique them by the location where they were 1002 // allocated, and only report a single path. 1003 PathDiagnosticLocation LocUsedForUniqueing; 1004 const Stmt *AllocStmt = 0; 1005 const MemRegion *Region = 0; 1006 llvm::tie(AllocStmt, Region) = getAllocationSite(N, Sym, C); 1007 if (AllocStmt) 1008 LocUsedForUniqueing = PathDiagnosticLocation::createBegin(AllocStmt, 1009 C.getSourceManager(), N->getLocationContext()); 1010 1011 SmallString<200> buf; 1012 llvm::raw_svector_ostream os(buf); 1013 os << "Memory is never released; potential leak"; 1014 if (Region) { 1015 os << " of memory pointed to by '"; 1016 Region->dumpPretty(os); 1017 os <<'\''; 1018 } 1019 1020 BugReport *R = new BugReport(*BT_Leak, os.str(), N, LocUsedForUniqueing); 1021 R->markInteresting(Sym); 1022 R->addVisitor(new MallocBugVisitor(Sym, true)); 1023 C.EmitReport(R); 1024 } 1025 1026 void MallocChecker::checkDeadSymbols(SymbolReaper &SymReaper, 1027 CheckerContext &C) const 1028 { 1029 if (!SymReaper.hasDeadSymbols()) 1030 return; 1031 1032 ProgramStateRef state = C.getState(); 1033 RegionStateTy RS = state->get<RegionState>(); 1034 RegionStateTy::Factory &F = state->get_context<RegionState>(); 1035 1036 bool generateReport = false; 1037 llvm::SmallVector<SymbolRef, 2> Errors; 1038 for (RegionStateTy::iterator I = RS.begin(), E = RS.end(); I != E; ++I) { 1039 if (SymReaper.isDead(I->first)) { 1040 if (I->second.isAllocated()) { 1041 generateReport = true; 1042 Errors.push_back(I->first); 1043 } 1044 // Remove the dead symbol from the map. 1045 RS = F.remove(RS, I->first); 1046 1047 } 1048 } 1049 1050 // Cleanup the Realloc Pairs Map. 1051 ReallocMap RP = state->get<ReallocPairs>(); 1052 for (ReallocMap::iterator I = RP.begin(), E = RP.end(); I != E; ++I) { 1053 if (SymReaper.isDead(I->first) || 1054 SymReaper.isDead(I->second.ReallocatedSym)) { 1055 state = state->remove<ReallocPairs>(I->first); 1056 } 1057 } 1058 1059 // Generate leak node. 1060 static SimpleProgramPointTag Tag("MallocChecker : DeadSymbolsLeak"); 1061 ExplodedNode *N = C.addTransition(C.getState(), C.getPredecessor(), &Tag); 1062 1063 if (generateReport) { 1064 for (llvm::SmallVector<SymbolRef, 2>::iterator 1065 I = Errors.begin(), E = Errors.end(); I != E; ++I) { 1066 reportLeak(*I, N, C); 1067 } 1068 } 1069 C.addTransition(state->set<RegionState>(RS), N); 1070 } 1071 1072 void MallocChecker::checkEndPath(CheckerContext &C) const { 1073 ProgramStateRef state = C.getState(); 1074 RegionStateTy M = state->get<RegionState>(); 1075 1076 // If inside inlined call, skip it. 1077 if (C.getLocationContext()->getParent() != 0) 1078 return; 1079 1080 for (RegionStateTy::iterator I = M.begin(), E = M.end(); I != E; ++I) { 1081 RefState RS = I->second; 1082 if (RS.isAllocated()) { 1083 ExplodedNode *N = C.addTransition(state); 1084 if (N) 1085 reportLeak(I->first, N, C); 1086 } 1087 } 1088 } 1089 1090 bool MallocChecker::checkEscape(SymbolRef Sym, const Stmt *S, 1091 CheckerContext &C) const { 1092 ProgramStateRef state = C.getState(); 1093 const RefState *RS = state->get<RegionState>(Sym); 1094 if (!RS) 1095 return false; 1096 1097 if (RS->isAllocated()) { 1098 state = state->set<RegionState>(Sym, RefState::getEscaped(S)); 1099 C.addTransition(state); 1100 return true; 1101 } 1102 return false; 1103 } 1104 1105 void MallocChecker::checkPreStmt(const CallExpr *CE, CheckerContext &C) const { 1106 // We will check for double free in the post visit. 1107 if (isFreeFunction(C.getCalleeDecl(CE), C.getASTContext())) 1108 return; 1109 1110 // Check use after free, when a freed pointer is passed to a call. 1111 ProgramStateRef State = C.getState(); 1112 for (CallExpr::const_arg_iterator I = CE->arg_begin(), 1113 E = CE->arg_end(); I != E; ++I) { 1114 const Expr *A = *I; 1115 if (A->getType().getTypePtr()->isAnyPointerType()) { 1116 SymbolRef Sym = State->getSVal(A, C.getLocationContext()).getAsSymbol(); 1117 if (!Sym) 1118 continue; 1119 if (checkUseAfterFree(Sym, C, A)) 1120 return; 1121 } 1122 } 1123 } 1124 1125 void MallocChecker::checkPreStmt(const ReturnStmt *S, CheckerContext &C) const { 1126 const Expr *E = S->getRetValue(); 1127 if (!E) 1128 return; 1129 1130 // Check if we are returning a symbol. 1131 SVal RetVal = C.getState()->getSVal(E, C.getLocationContext()); 1132 SymbolRef Sym = RetVal.getAsSymbol(); 1133 if (!Sym) 1134 // If we are returning a field of the allocated struct or an array element, 1135 // the callee could still free the memory. 1136 // TODO: This logic should be a part of generic symbol escape callback. 1137 if (const MemRegion *MR = RetVal.getAsRegion()) 1138 if (isa<FieldRegion>(MR) || isa<ElementRegion>(MR)) 1139 if (const SymbolicRegion *BMR = 1140 dyn_cast<SymbolicRegion>(MR->getBaseRegion())) 1141 Sym = BMR->getSymbol(); 1142 if (!Sym) 1143 return; 1144 1145 // Check if we are returning freed memory. 1146 if (checkUseAfterFree(Sym, C, E)) 1147 return; 1148 1149 // If this function body is not inlined, check if the symbol is escaping. 1150 if (C.getLocationContext()->getParent() == 0) 1151 checkEscape(Sym, E, C); 1152 } 1153 1154 // TODO: Blocks should be either inlined or should call invalidate regions 1155 // upon invocation. After that's in place, special casing here will not be 1156 // needed. 1157 void MallocChecker::checkPostStmt(const BlockExpr *BE, 1158 CheckerContext &C) const { 1159 1160 // Scan the BlockDecRefExprs for any object the retain count checker 1161 // may be tracking. 1162 if (!BE->getBlockDecl()->hasCaptures()) 1163 return; 1164 1165 ProgramStateRef state = C.getState(); 1166 const BlockDataRegion *R = 1167 cast<BlockDataRegion>(state->getSVal(BE, 1168 C.getLocationContext()).getAsRegion()); 1169 1170 BlockDataRegion::referenced_vars_iterator I = R->referenced_vars_begin(), 1171 E = R->referenced_vars_end(); 1172 1173 if (I == E) 1174 return; 1175 1176 SmallVector<const MemRegion*, 10> Regions; 1177 const LocationContext *LC = C.getLocationContext(); 1178 MemRegionManager &MemMgr = C.getSValBuilder().getRegionManager(); 1179 1180 for ( ; I != E; ++I) { 1181 const VarRegion *VR = *I; 1182 if (VR->getSuperRegion() == R) { 1183 VR = MemMgr.getVarRegion(VR->getDecl(), LC); 1184 } 1185 Regions.push_back(VR); 1186 } 1187 1188 state = 1189 state->scanReachableSymbols<StopTrackingCallback>(Regions.data(), 1190 Regions.data() + Regions.size()).getState(); 1191 C.addTransition(state); 1192 } 1193 1194 bool MallocChecker::isReleased(SymbolRef Sym, CheckerContext &C) const { 1195 assert(Sym); 1196 const RefState *RS = C.getState()->get<RegionState>(Sym); 1197 return (RS && RS->isReleased()); 1198 } 1199 1200 bool MallocChecker::checkUseAfterFree(SymbolRef Sym, CheckerContext &C, 1201 const Stmt *S) const { 1202 if (isReleased(Sym, C)) { 1203 if (ExplodedNode *N = C.generateSink()) { 1204 if (!BT_UseFree) 1205 BT_UseFree.reset(new BugType("Use-after-free", "Memory Error")); 1206 1207 BugReport *R = new BugReport(*BT_UseFree, 1208 "Use of memory after it is freed",N); 1209 if (S) 1210 R->addRange(S->getSourceRange()); 1211 R->markInteresting(Sym); 1212 R->addVisitor(new MallocBugVisitor(Sym)); 1213 C.EmitReport(R); 1214 return true; 1215 } 1216 } 1217 return false; 1218 } 1219 1220 // Check if the location is a freed symbolic region. 1221 void MallocChecker::checkLocation(SVal l, bool isLoad, const Stmt *S, 1222 CheckerContext &C) const { 1223 SymbolRef Sym = l.getLocSymbolInBase(); 1224 if (Sym) 1225 checkUseAfterFree(Sym, C, S); 1226 } 1227 1228 //===----------------------------------------------------------------------===// 1229 // Check various ways a symbol can be invalidated. 1230 // TODO: This logic (the next 3 functions) is copied/similar to the 1231 // RetainRelease checker. We might want to factor this out. 1232 //===----------------------------------------------------------------------===// 1233 1234 // Stop tracking symbols when a value escapes as a result of checkBind. 1235 // A value escapes in three possible cases: 1236 // (1) we are binding to something that is not a memory region. 1237 // (2) we are binding to a memregion that does not have stack storage 1238 // (3) we are binding to a memregion with stack storage that the store 1239 // does not understand. 1240 void MallocChecker::checkBind(SVal loc, SVal val, const Stmt *S, 1241 CheckerContext &C) const { 1242 // Are we storing to something that causes the value to "escape"? 1243 bool escapes = true; 1244 ProgramStateRef state = C.getState(); 1245 1246 if (loc::MemRegionVal *regionLoc = dyn_cast<loc::MemRegionVal>(&loc)) { 1247 escapes = !regionLoc->getRegion()->hasStackStorage(); 1248 1249 if (!escapes) { 1250 // To test (3), generate a new state with the binding added. If it is 1251 // the same state, then it escapes (since the store cannot represent 1252 // the binding). 1253 // Do this only if we know that the store is not supposed to generate the 1254 // same state. 1255 SVal StoredVal = state->getSVal(regionLoc->getRegion()); 1256 if (StoredVal != val) 1257 escapes = (state == (state->bindLoc(*regionLoc, val))); 1258 } 1259 if (!escapes) { 1260 // Case 4: We do not currently model what happens when a symbol is 1261 // assigned to a struct field, so be conservative here and let the symbol 1262 // go. TODO: This could definitely be improved upon. 1263 escapes = !isa<VarRegion>(regionLoc->getRegion()); 1264 } 1265 } 1266 1267 // If our store can represent the binding and we aren't storing to something 1268 // that doesn't have local storage then just return and have the simulation 1269 // state continue as is. 1270 if (!escapes) 1271 return; 1272 1273 // Otherwise, find all symbols referenced by 'val' that we are tracking 1274 // and stop tracking them. 1275 state = state->scanReachableSymbols<StopTrackingCallback>(val).getState(); 1276 C.addTransition(state); 1277 } 1278 1279 // If a symbolic region is assumed to NULL (or another constant), stop tracking 1280 // it - assuming that allocation failed on this path. 1281 ProgramStateRef MallocChecker::evalAssume(ProgramStateRef state, 1282 SVal Cond, 1283 bool Assumption) const { 1284 RegionStateTy RS = state->get<RegionState>(); 1285 for (RegionStateTy::iterator I = RS.begin(), E = RS.end(); I != E; ++I) { 1286 // If the symbol is assumed to NULL or another constant, this will 1287 // return an APSInt*. 1288 if (state->getSymVal(I.getKey())) 1289 state = state->remove<RegionState>(I.getKey()); 1290 } 1291 1292 // Realloc returns 0 when reallocation fails, which means that we should 1293 // restore the state of the pointer being reallocated. 1294 ReallocMap RP = state->get<ReallocPairs>(); 1295 for (ReallocMap::iterator I = RP.begin(), E = RP.end(); I != E; ++I) { 1296 // If the symbol is assumed to NULL or another constant, this will 1297 // return an APSInt*. 1298 if (state->getSymVal(I.getKey())) { 1299 SymbolRef ReallocSym = I.getData().ReallocatedSym; 1300 const RefState *RS = state->get<RegionState>(ReallocSym); 1301 if (RS) { 1302 if (RS->isReleased() && ! I.getData().IsFreeOnFailure) 1303 state = state->set<RegionState>(ReallocSym, 1304 RefState::getAllocated(RS->getStmt())); 1305 } 1306 state = state->remove<ReallocPairs>(I.getKey()); 1307 } 1308 } 1309 1310 return state; 1311 } 1312 1313 // Check if the function is known to us. So, for example, we could 1314 // conservatively assume it can free/reallocate its pointer arguments. 1315 // (We assume that the pointers cannot escape through calls to system 1316 // functions not handled by this checker.) 1317 bool MallocChecker::doesNotFreeMemory(const CallEvent *Call, 1318 ProgramStateRef State) const { 1319 if (!Call) 1320 return false; 1321 1322 // For now, assume that any C++ call can free memory. 1323 // TODO: If we want to be more optimistic here, we'll need to make sure that 1324 // regions escape to C++ containers. They seem to do that even now, but for 1325 // mysterious reasons. 1326 if (!(isa<FunctionCall>(Call) || isa<ObjCMessageInvocation>(Call))) 1327 return false; 1328 1329 // If the call has a callback as an argument, assume the memory 1330 // can be freed. 1331 if (Call->hasNonZeroCallbackArg()) 1332 return false; 1333 1334 // Check Objective-C messages by selector name. 1335 if (const ObjCMessageInvocation *Msg = dyn_cast<ObjCMessageInvocation>(Call)){ 1336 // If it's not a framework call, assume it frees memory. 1337 if (!Call->isInSystemHeader()) 1338 return false; 1339 1340 Selector S = Msg->getSelector(); 1341 1342 // Whitelist the ObjC methods which do free memory. 1343 // - Anything containing 'freeWhenDone' param set to 1. 1344 // Ex: dataWithBytesNoCopy:length:freeWhenDone. 1345 for (unsigned i = 1; i < S.getNumArgs(); ++i) { 1346 if (S.getNameForSlot(i).equals("freeWhenDone")) { 1347 if (Call->getArgSVal(i).isConstant(1)) 1348 return false; 1349 else 1350 return true; 1351 } 1352 } 1353 1354 // If the first selector ends with NoCopy, assume that the ownership is 1355 // transferred as well. 1356 // Ex: [NSData dataWithBytesNoCopy:bytes length:10]; 1357 StringRef FirstSlot = S.getNameForSlot(0); 1358 if (FirstSlot.endswith("NoCopy")) 1359 return false; 1360 1361 // If the first selector starts with addPointer, insertPointer, 1362 // or replacePointer, assume we are dealing with NSPointerArray or similar. 1363 // This is similar to C++ containers (vector); we still might want to check 1364 // that the pointers get freed by following the container itself. 1365 if (FirstSlot.startswith("addPointer") || 1366 FirstSlot.startswith("insertPointer") || 1367 FirstSlot.startswith("replacePointer")) { 1368 return false; 1369 } 1370 1371 // Otherwise, assume that the method does not free memory. 1372 // Most framework methods do not free memory. 1373 return true; 1374 } 1375 1376 // At this point the only thing left to handle is straight function calls. 1377 const FunctionDecl *FD = cast<FunctionCall>(Call)->getDecl(); 1378 if (!FD) 1379 return false; 1380 1381 ASTContext &ASTC = State->getStateManager().getContext(); 1382 1383 // If it's one of the allocation functions we can reason about, we model 1384 // its behavior explicitly. 1385 if (isMemFunction(FD, ASTC)) 1386 return true; 1387 1388 // If it's not a system call, assume it frees memory. 1389 if (!Call->isInSystemHeader()) 1390 return false; 1391 1392 // White list the system functions whose arguments escape. 1393 const IdentifierInfo *II = FD->getIdentifier(); 1394 if (!II) 1395 return false; 1396 StringRef FName = II->getName(); 1397 1398 // White list thread local storage. 1399 if (FName.equals("pthread_setspecific")) 1400 return false; 1401 if (FName.equals("xpc_connection_set_context")) 1402 return false; 1403 1404 // White list the 'XXXNoCopy' CoreFoundation functions. 1405 if (FName.endswith("NoCopy")) { 1406 // Look for the deallocator argument. We know that the memory ownership 1407 // is not transferred only if the deallocator argument is 1408 // 'kCFAllocatorNull'. 1409 for (unsigned i = 1; i < Call->getNumArgs(); ++i) { 1410 const Expr *ArgE = Call->getArgExpr(i)->IgnoreParenCasts(); 1411 if (const DeclRefExpr *DE = dyn_cast<DeclRefExpr>(ArgE)) { 1412 StringRef DeallocatorName = DE->getFoundDecl()->getName(); 1413 if (DeallocatorName == "kCFAllocatorNull") 1414 return true; 1415 } 1416 } 1417 return false; 1418 } 1419 1420 // PR12101 1421 // Many CoreFoundation and CoreGraphics might allow a tracked object 1422 // to escape. 1423 if (CallOrObjCMessage::isCFCGAllowingEscape(FName)) 1424 return false; 1425 1426 // Associating streams with malloced buffers. The pointer can escape if 1427 // 'closefn' is specified (and if that function does free memory). 1428 // Currently, we do not inspect the 'closefn' function (PR12101). 1429 if (FName == "funopen") 1430 if (Call->getNumArgs() >= 4 && !Call->getArgSVal(4).isConstant(0)) 1431 return false; 1432 1433 // Do not warn on pointers passed to 'setbuf' when used with std streams, 1434 // these leaks might be intentional when setting the buffer for stdio. 1435 // http://stackoverflow.com/questions/2671151/who-frees-setvbuf-buffer 1436 if (FName == "setbuf" || FName =="setbuffer" || 1437 FName == "setlinebuf" || FName == "setvbuf") { 1438 if (Call->getNumArgs() >= 1) { 1439 const Expr *ArgE = Call->getArgExpr(0)->IgnoreParenCasts(); 1440 if (const DeclRefExpr *ArgDRE = dyn_cast<DeclRefExpr>(ArgE)) 1441 if (const VarDecl *D = dyn_cast<VarDecl>(ArgDRE->getDecl())) 1442 if (D->getCanonicalDecl()->getName().find("std") != StringRef::npos) 1443 return false; 1444 } 1445 } 1446 1447 // A bunch of other functions which either take ownership of a pointer or 1448 // wrap the result up in a struct or object, meaning it can be freed later. 1449 // (See RetainCountChecker.) Not all the parameters here are invalidated, 1450 // but the Malloc checker cannot differentiate between them. The right way 1451 // of doing this would be to implement a pointer escapes callback. 1452 if (FName == "CGBitmapContextCreate" || 1453 FName == "CGBitmapContextCreateWithData" || 1454 FName == "CVPixelBufferCreateWithBytes" || 1455 FName == "CVPixelBufferCreateWithPlanarBytes" || 1456 FName == "OSAtomicEnqueue") { 1457 return false; 1458 } 1459 1460 // Whitelist NSXXInsertXX, for example NSMapInsertIfAbsent, since they can 1461 // be deallocated by NSMapRemove. 1462 if (FName.startswith("NS") && (FName.find("Insert") != StringRef::npos)) 1463 return false; 1464 1465 // Otherwise, assume that the function does not free memory. 1466 // Most system calls do not free the memory. 1467 return true; 1468 } 1469 1470 // If the symbol we are tracking is invalidated, but not explicitly (ex: the &p 1471 // escapes, when we are tracking p), do not track the symbol as we cannot reason 1472 // about it anymore. 1473 ProgramStateRef 1474 MallocChecker::checkRegionChanges(ProgramStateRef State, 1475 const StoreManager::InvalidatedSymbols *invalidated, 1476 ArrayRef<const MemRegion *> ExplicitRegions, 1477 ArrayRef<const MemRegion *> Regions, 1478 const CallEvent *Call) const { 1479 if (!invalidated || invalidated->empty()) 1480 return State; 1481 llvm::SmallPtrSet<SymbolRef, 8> WhitelistedSymbols; 1482 1483 // If it's a call which might free or reallocate memory, we assume that all 1484 // regions (explicit and implicit) escaped. 1485 1486 // Otherwise, whitelist explicit pointers; we still can track them. 1487 if (!Call || doesNotFreeMemory(Call, State)) { 1488 for (ArrayRef<const MemRegion *>::iterator I = ExplicitRegions.begin(), 1489 E = ExplicitRegions.end(); I != E; ++I) { 1490 if (const SymbolicRegion *R = (*I)->StripCasts()->getAs<SymbolicRegion>()) 1491 WhitelistedSymbols.insert(R->getSymbol()); 1492 } 1493 } 1494 1495 for (StoreManager::InvalidatedSymbols::const_iterator I=invalidated->begin(), 1496 E = invalidated->end(); I!=E; ++I) { 1497 SymbolRef sym = *I; 1498 if (WhitelistedSymbols.count(sym)) 1499 continue; 1500 // The symbol escaped. Note, we assume that if the symbol is released, 1501 // passing it out will result in a use after free. We also keep tracking 1502 // relinquished symbols. 1503 if (const RefState *RS = State->get<RegionState>(sym)) { 1504 if (RS->isAllocated()) 1505 State = State->set<RegionState>(sym, 1506 RefState::getEscaped(RS->getStmt())); 1507 } 1508 } 1509 return State; 1510 } 1511 1512 static SymbolRef findFailedReallocSymbol(ProgramStateRef currState, 1513 ProgramStateRef prevState) { 1514 ReallocMap currMap = currState->get<ReallocPairs>(); 1515 ReallocMap prevMap = prevState->get<ReallocPairs>(); 1516 1517 for (ReallocMap::iterator I = prevMap.begin(), E = prevMap.end(); 1518 I != E; ++I) { 1519 SymbolRef sym = I.getKey(); 1520 if (!currMap.lookup(sym)) 1521 return sym; 1522 } 1523 1524 return NULL; 1525 } 1526 1527 PathDiagnosticPiece * 1528 MallocChecker::MallocBugVisitor::VisitNode(const ExplodedNode *N, 1529 const ExplodedNode *PrevN, 1530 BugReporterContext &BRC, 1531 BugReport &BR) { 1532 ProgramStateRef state = N->getState(); 1533 ProgramStateRef statePrev = PrevN->getState(); 1534 1535 const RefState *RS = state->get<RegionState>(Sym); 1536 const RefState *RSPrev = statePrev->get<RegionState>(Sym); 1537 if (!RS && !RSPrev) 1538 return 0; 1539 1540 const Stmt *S = 0; 1541 const char *Msg = 0; 1542 StackHintGeneratorForSymbol *StackHint = 0; 1543 1544 // Retrieve the associated statement. 1545 ProgramPoint ProgLoc = N->getLocation(); 1546 if (isa<StmtPoint>(ProgLoc)) 1547 S = cast<StmtPoint>(ProgLoc).getStmt(); 1548 // If an assumption was made on a branch, it should be caught 1549 // here by looking at the state transition. 1550 if (isa<BlockEdge>(ProgLoc)) { 1551 const CFGBlock *srcBlk = cast<BlockEdge>(ProgLoc).getSrc(); 1552 S = srcBlk->getTerminator(); 1553 } 1554 if (!S) 1555 return 0; 1556 1557 // Find out if this is an interesting point and what is the kind. 1558 if (Mode == Normal) { 1559 if (isAllocated(RS, RSPrev, S)) { 1560 Msg = "Memory is allocated"; 1561 StackHint = new StackHintGeneratorForSymbol(Sym, 1562 "Returned allocated memory"); 1563 } else if (isReleased(RS, RSPrev, S)) { 1564 Msg = "Memory is released"; 1565 StackHint = new StackHintGeneratorForSymbol(Sym, 1566 "Returned released memory"); 1567 } else if (isRelinquished(RS, RSPrev, S)) { 1568 Msg = "Memory ownership is transfered"; 1569 StackHint = new StackHintGeneratorForSymbol(Sym, ""); 1570 } else if (isReallocFailedCheck(RS, RSPrev, S)) { 1571 Mode = ReallocationFailed; 1572 Msg = "Reallocation failed"; 1573 StackHint = new StackHintGeneratorForReallocationFailed(Sym, 1574 "Reallocation failed"); 1575 1576 if (SymbolRef sym = findFailedReallocSymbol(state, statePrev)) { 1577 // Is it possible to fail two reallocs WITHOUT testing in between? 1578 assert((!FailedReallocSymbol || FailedReallocSymbol == sym) && 1579 "We only support one failed realloc at a time."); 1580 BR.markInteresting(sym); 1581 FailedReallocSymbol = sym; 1582 } 1583 } 1584 1585 // We are in a special mode if a reallocation failed later in the path. 1586 } else if (Mode == ReallocationFailed) { 1587 assert(FailedReallocSymbol && "No symbol to look for."); 1588 1589 // Is this is the first appearance of the reallocated symbol? 1590 if (!statePrev->get<RegionState>(FailedReallocSymbol)) { 1591 // If we ever hit this assert, that means BugReporter has decided to skip 1592 // node pairs or visit them out of order. 1593 assert(state->get<RegionState>(FailedReallocSymbol) && 1594 "Missed the reallocation point"); 1595 1596 // We're at the reallocation point. 1597 Msg = "Attempt to reallocate memory"; 1598 StackHint = new StackHintGeneratorForSymbol(Sym, 1599 "Returned reallocated memory"); 1600 FailedReallocSymbol = NULL; 1601 Mode = Normal; 1602 } 1603 } 1604 1605 if (!Msg) 1606 return 0; 1607 assert(StackHint); 1608 1609 // Generate the extra diagnostic. 1610 PathDiagnosticLocation Pos(S, BRC.getSourceManager(), 1611 N->getLocationContext()); 1612 return new PathDiagnosticEventPiece(Pos, Msg, true, StackHint); 1613 } 1614 1615 void MallocChecker::printState(raw_ostream &Out, ProgramStateRef State, 1616 const char *NL, const char *Sep) const { 1617 1618 RegionStateTy RS = State->get<RegionState>(); 1619 1620 if (!RS.isEmpty()) 1621 Out << "Has Malloc data" << NL; 1622 } 1623 1624 #define REGISTER_CHECKER(name) \ 1625 void ento::register##name(CheckerManager &mgr) {\ 1626 registerCStringCheckerBasic(mgr); \ 1627 mgr.registerChecker<MallocChecker>()->Filter.C##name = true;\ 1628 } 1629 1630 REGISTER_CHECKER(MallocPessimistic) 1631 REGISTER_CHECKER(MallocOptimistic) 1632