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/CallEvent.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 ObjCMethodCall &Call, 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 if (FD->getKind() == Decl::Function) { 391 IdentifierInfo *FunI = FD->getIdentifier(); 392 initIdentifierInfo(C); 393 394 if (FunI == II_malloc || FunI == II_realloc || 395 FunI == II_reallocf || FunI == II_calloc || FunI == II_valloc || 396 FunI == II_strdup || FunI == II_strndup) 397 return true; 398 } 399 400 if (Filter.CMallocOptimistic && FD->hasAttrs()) 401 for (specific_attr_iterator<OwnershipAttr> 402 i = FD->specific_attr_begin<OwnershipAttr>(), 403 e = FD->specific_attr_end<OwnershipAttr>(); 404 i != e; ++i) 405 if ((*i)->getOwnKind() == OwnershipAttr::Returns) 406 return true; 407 return false; 408 } 409 410 bool MallocChecker::isFreeFunction(const FunctionDecl *FD, ASTContext &C) const { 411 if (!FD) 412 return false; 413 414 if (FD->getKind() == Decl::Function) { 415 IdentifierInfo *FunI = FD->getIdentifier(); 416 initIdentifierInfo(C); 417 418 if (FunI == II_free || FunI == II_realloc || FunI == II_reallocf) 419 return true; 420 } 421 422 if (Filter.CMallocOptimistic && FD->hasAttrs()) 423 for (specific_attr_iterator<OwnershipAttr> 424 i = FD->specific_attr_begin<OwnershipAttr>(), 425 e = FD->specific_attr_end<OwnershipAttr>(); 426 i != e; ++i) 427 if ((*i)->getOwnKind() == OwnershipAttr::Takes || 428 (*i)->getOwnKind() == OwnershipAttr::Holds) 429 return true; 430 return false; 431 } 432 433 void MallocChecker::checkPostStmt(const CallExpr *CE, CheckerContext &C) const { 434 const FunctionDecl *FD = C.getCalleeDecl(CE); 435 if (!FD) 436 return; 437 438 ProgramStateRef State = C.getState(); 439 440 if (FD->getKind() == Decl::Function) { 441 initIdentifierInfo(C.getASTContext()); 442 IdentifierInfo *FunI = FD->getIdentifier(); 443 444 if (FunI == II_malloc || FunI == II_valloc) { 445 if (CE->getNumArgs() < 1) 446 return; 447 State = MallocMemAux(C, CE, CE->getArg(0), UndefinedVal(), State); 448 } else if (FunI == II_realloc) { 449 State = ReallocMem(C, CE, false); 450 } else if (FunI == II_reallocf) { 451 State = ReallocMem(C, CE, true); 452 } else if (FunI == II_calloc) { 453 State = CallocMem(C, CE); 454 } else if (FunI == II_free) { 455 State = FreeMemAux(C, CE, State, 0, false); 456 } else if (FunI == II_strdup) { 457 State = MallocUpdateRefState(C, CE, State); 458 } else if (FunI == II_strndup) { 459 State = MallocUpdateRefState(C, CE, State); 460 } 461 } 462 463 if (Filter.CMallocOptimistic) { 464 // Check all the attributes, if there are any. 465 // There can be multiple of these attributes. 466 if (FD->hasAttrs()) 467 for (specific_attr_iterator<OwnershipAttr> 468 i = FD->specific_attr_begin<OwnershipAttr>(), 469 e = FD->specific_attr_end<OwnershipAttr>(); 470 i != e; ++i) { 471 switch ((*i)->getOwnKind()) { 472 case OwnershipAttr::Returns: 473 State = MallocMemReturnsAttr(C, CE, *i); 474 break; 475 case OwnershipAttr::Takes: 476 case OwnershipAttr::Holds: 477 State = FreeMemAttr(C, CE, *i); 478 break; 479 } 480 } 481 } 482 C.addTransition(State); 483 } 484 485 static bool isFreeWhenDoneSetToZero(const ObjCMethodCall &Call) { 486 Selector S = Call.getSelector(); 487 for (unsigned i = 1; i < S.getNumArgs(); ++i) 488 if (S.getNameForSlot(i).equals("freeWhenDone")) 489 if (Call.getArgSVal(i).isConstant(0)) 490 return true; 491 492 return false; 493 } 494 495 void MallocChecker::checkPreObjCMessage(const ObjCMethodCall &Call, 496 CheckerContext &C) const { 497 // If the first selector is dataWithBytesNoCopy, assume that the memory will 498 // be released with 'free' by the new object. 499 // Ex: [NSData dataWithBytesNoCopy:bytes length:10]; 500 // Unless 'freeWhenDone' param set to 0. 501 // TODO: Check that the memory was allocated with malloc. 502 Selector S = Call.getSelector(); 503 if ((S.getNameForSlot(0) == "dataWithBytesNoCopy" || 504 S.getNameForSlot(0) == "initWithBytesNoCopy" || 505 S.getNameForSlot(0) == "initWithCharactersNoCopy") && 506 !isFreeWhenDoneSetToZero(Call)){ 507 unsigned int argIdx = 0; 508 C.addTransition(FreeMemAux(C, Call.getArgExpr(argIdx), 509 Call.getOriginExpr(), C.getState(), true)); 510 } 511 } 512 513 ProgramStateRef MallocChecker::MallocMemReturnsAttr(CheckerContext &C, 514 const CallExpr *CE, 515 const OwnershipAttr* Att) { 516 if (Att->getModule() != "malloc") 517 return 0; 518 519 OwnershipAttr::args_iterator I = Att->args_begin(), E = Att->args_end(); 520 if (I != E) { 521 return MallocMemAux(C, CE, CE->getArg(*I), UndefinedVal(), C.getState()); 522 } 523 return MallocMemAux(C, CE, UnknownVal(), UndefinedVal(), C.getState()); 524 } 525 526 ProgramStateRef MallocChecker::MallocMemAux(CheckerContext &C, 527 const CallExpr *CE, 528 SVal Size, SVal Init, 529 ProgramStateRef state) { 530 531 // Bind the return value to the symbolic value from the heap region. 532 // TODO: We could rewrite post visit to eval call; 'malloc' does not have 533 // side effects other than what we model here. 534 unsigned Count = C.getCurrentBlockCount(); 535 SValBuilder &svalBuilder = C.getSValBuilder(); 536 const LocationContext *LCtx = C.getPredecessor()->getLocationContext(); 537 DefinedSVal RetVal = 538 cast<DefinedSVal>(svalBuilder.getConjuredHeapSymbolVal(CE, LCtx, Count)); 539 state = state->BindExpr(CE, C.getLocationContext(), RetVal); 540 541 // We expect the malloc functions to return a pointer. 542 if (!isa<Loc>(RetVal)) 543 return 0; 544 545 // Fill the region with the initialization value. 546 state = state->bindDefault(RetVal, Init); 547 548 // Set the region's extent equal to the Size parameter. 549 const SymbolicRegion *R = 550 dyn_cast_or_null<SymbolicRegion>(RetVal.getAsRegion()); 551 if (!R) 552 return 0; 553 if (isa<DefinedOrUnknownSVal>(Size)) { 554 SValBuilder &svalBuilder = C.getSValBuilder(); 555 DefinedOrUnknownSVal Extent = R->getExtent(svalBuilder); 556 DefinedOrUnknownSVal DefinedSize = cast<DefinedOrUnknownSVal>(Size); 557 DefinedOrUnknownSVal extentMatchesSize = 558 svalBuilder.evalEQ(state, Extent, DefinedSize); 559 560 state = state->assume(extentMatchesSize, true); 561 assert(state); 562 } 563 564 return MallocUpdateRefState(C, CE, state); 565 } 566 567 ProgramStateRef MallocChecker::MallocUpdateRefState(CheckerContext &C, 568 const CallExpr *CE, 569 ProgramStateRef state) { 570 // Get the return value. 571 SVal retVal = state->getSVal(CE, C.getLocationContext()); 572 573 // We expect the malloc functions to return a pointer. 574 if (!isa<Loc>(retVal)) 575 return 0; 576 577 SymbolRef Sym = retVal.getAsLocSymbol(); 578 assert(Sym); 579 580 // Set the symbol's state to Allocated. 581 return state->set<RegionState>(Sym, RefState::getAllocated(CE)); 582 583 } 584 585 ProgramStateRef MallocChecker::FreeMemAttr(CheckerContext &C, 586 const CallExpr *CE, 587 const OwnershipAttr* Att) const { 588 if (Att->getModule() != "malloc") 589 return 0; 590 591 ProgramStateRef State = C.getState(); 592 593 for (OwnershipAttr::args_iterator I = Att->args_begin(), E = Att->args_end(); 594 I != E; ++I) { 595 ProgramStateRef StateI = FreeMemAux(C, CE, State, *I, 596 Att->getOwnKind() == OwnershipAttr::Holds); 597 if (StateI) 598 State = StateI; 599 } 600 return State; 601 } 602 603 ProgramStateRef MallocChecker::FreeMemAux(CheckerContext &C, 604 const CallExpr *CE, 605 ProgramStateRef state, 606 unsigned Num, 607 bool Hold) const { 608 if (CE->getNumArgs() < (Num + 1)) 609 return 0; 610 611 return FreeMemAux(C, CE->getArg(Num), CE, state, Hold); 612 } 613 614 ProgramStateRef MallocChecker::FreeMemAux(CheckerContext &C, 615 const Expr *ArgExpr, 616 const Expr *ParentExpr, 617 ProgramStateRef state, 618 bool Hold) const { 619 620 SVal ArgVal = state->getSVal(ArgExpr, C.getLocationContext()); 621 if (!isa<DefinedOrUnknownSVal>(ArgVal)) 622 return 0; 623 DefinedOrUnknownSVal location = cast<DefinedOrUnknownSVal>(ArgVal); 624 625 // Check for null dereferences. 626 if (!isa<Loc>(location)) 627 return 0; 628 629 // The explicit NULL case, no operation is performed. 630 ProgramStateRef notNullState, nullState; 631 llvm::tie(notNullState, nullState) = state->assume(location); 632 if (nullState && !notNullState) 633 return 0; 634 635 // Unknown values could easily be okay 636 // Undefined values are handled elsewhere 637 if (ArgVal.isUnknownOrUndef()) 638 return 0; 639 640 const MemRegion *R = ArgVal.getAsRegion(); 641 642 // Nonlocs can't be freed, of course. 643 // Non-region locations (labels and fixed addresses) also shouldn't be freed. 644 if (!R) { 645 ReportBadFree(C, ArgVal, ArgExpr->getSourceRange()); 646 return 0; 647 } 648 649 R = R->StripCasts(); 650 651 // Blocks might show up as heap data, but should not be free()d 652 if (isa<BlockDataRegion>(R)) { 653 ReportBadFree(C, ArgVal, ArgExpr->getSourceRange()); 654 return 0; 655 } 656 657 const MemSpaceRegion *MS = R->getMemorySpace(); 658 659 // Parameters, locals, statics, and globals shouldn't be freed. 660 if (!(isa<UnknownSpaceRegion>(MS) || isa<HeapSpaceRegion>(MS))) { 661 // FIXME: at the time this code was written, malloc() regions were 662 // represented by conjured symbols, which are all in UnknownSpaceRegion. 663 // This means that there isn't actually anything from HeapSpaceRegion 664 // that should be freed, even though we allow it here. 665 // Of course, free() can work on memory allocated outside the current 666 // function, so UnknownSpaceRegion is always a possibility. 667 // False negatives are better than false positives. 668 669 ReportBadFree(C, ArgVal, ArgExpr->getSourceRange()); 670 return 0; 671 } 672 673 const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(R); 674 // Various cases could lead to non-symbol values here. 675 // For now, ignore them. 676 if (!SR) 677 return 0; 678 679 SymbolRef Sym = SR->getSymbol(); 680 const RefState *RS = state->get<RegionState>(Sym); 681 682 // Check double free. 683 if (RS && (RS->isReleased() || RS->isRelinquished())) { 684 if (ExplodedNode *N = C.generateSink()) { 685 if (!BT_DoubleFree) 686 BT_DoubleFree.reset( 687 new BugType("Double free", "Memory Error")); 688 BugReport *R = new BugReport(*BT_DoubleFree, 689 (RS->isReleased() ? "Attempt to free released memory" : 690 "Attempt to free non-owned memory"), N); 691 R->addRange(ArgExpr->getSourceRange()); 692 R->markInteresting(Sym); 693 R->addVisitor(new MallocBugVisitor(Sym)); 694 C.EmitReport(R); 695 } 696 return 0; 697 } 698 699 // Normal free. 700 if (Hold) 701 return state->set<RegionState>(Sym, RefState::getRelinquished(ParentExpr)); 702 return state->set<RegionState>(Sym, RefState::getReleased(ParentExpr)); 703 } 704 705 bool MallocChecker::SummarizeValue(raw_ostream &os, SVal V) { 706 if (nonloc::ConcreteInt *IntVal = dyn_cast<nonloc::ConcreteInt>(&V)) 707 os << "an integer (" << IntVal->getValue() << ")"; 708 else if (loc::ConcreteInt *ConstAddr = dyn_cast<loc::ConcreteInt>(&V)) 709 os << "a constant address (" << ConstAddr->getValue() << ")"; 710 else if (loc::GotoLabel *Label = dyn_cast<loc::GotoLabel>(&V)) 711 os << "the address of the label '" << Label->getLabel()->getName() << "'"; 712 else 713 return false; 714 715 return true; 716 } 717 718 bool MallocChecker::SummarizeRegion(raw_ostream &os, 719 const MemRegion *MR) { 720 switch (MR->getKind()) { 721 case MemRegion::FunctionTextRegionKind: { 722 const FunctionDecl *FD = cast<FunctionTextRegion>(MR)->getDecl(); 723 if (FD) 724 os << "the address of the function '" << *FD << '\''; 725 else 726 os << "the address of a function"; 727 return true; 728 } 729 case MemRegion::BlockTextRegionKind: 730 os << "block text"; 731 return true; 732 case MemRegion::BlockDataRegionKind: 733 // FIXME: where the block came from? 734 os << "a block"; 735 return true; 736 default: { 737 const MemSpaceRegion *MS = MR->getMemorySpace(); 738 739 if (isa<StackLocalsSpaceRegion>(MS)) { 740 const VarRegion *VR = dyn_cast<VarRegion>(MR); 741 const VarDecl *VD; 742 if (VR) 743 VD = VR->getDecl(); 744 else 745 VD = NULL; 746 747 if (VD) 748 os << "the address of the local variable '" << VD->getName() << "'"; 749 else 750 os << "the address of a local stack variable"; 751 return true; 752 } 753 754 if (isa<StackArgumentsSpaceRegion>(MS)) { 755 const VarRegion *VR = dyn_cast<VarRegion>(MR); 756 const VarDecl *VD; 757 if (VR) 758 VD = VR->getDecl(); 759 else 760 VD = NULL; 761 762 if (VD) 763 os << "the address of the parameter '" << VD->getName() << "'"; 764 else 765 os << "the address of a parameter"; 766 return true; 767 } 768 769 if (isa<GlobalsSpaceRegion>(MS)) { 770 const VarRegion *VR = dyn_cast<VarRegion>(MR); 771 const VarDecl *VD; 772 if (VR) 773 VD = VR->getDecl(); 774 else 775 VD = NULL; 776 777 if (VD) { 778 if (VD->isStaticLocal()) 779 os << "the address of the static variable '" << VD->getName() << "'"; 780 else 781 os << "the address of the global variable '" << VD->getName() << "'"; 782 } else 783 os << "the address of a global variable"; 784 return true; 785 } 786 787 return false; 788 } 789 } 790 } 791 792 void MallocChecker::ReportBadFree(CheckerContext &C, SVal ArgVal, 793 SourceRange range) const { 794 if (ExplodedNode *N = C.generateSink()) { 795 if (!BT_BadFree) 796 BT_BadFree.reset(new BugType("Bad free", "Memory Error")); 797 798 SmallString<100> buf; 799 llvm::raw_svector_ostream os(buf); 800 801 const MemRegion *MR = ArgVal.getAsRegion(); 802 if (MR) { 803 while (const ElementRegion *ER = dyn_cast<ElementRegion>(MR)) 804 MR = ER->getSuperRegion(); 805 806 // Special case for alloca() 807 if (isa<AllocaRegion>(MR)) 808 os << "Argument to free() was allocated by alloca(), not malloc()"; 809 else { 810 os << "Argument to free() is "; 811 if (SummarizeRegion(os, MR)) 812 os << ", which is not memory allocated by malloc()"; 813 else 814 os << "not memory allocated by malloc()"; 815 } 816 } else { 817 os << "Argument to free() is "; 818 if (SummarizeValue(os, ArgVal)) 819 os << ", which is not memory allocated by malloc()"; 820 else 821 os << "not memory allocated by malloc()"; 822 } 823 824 BugReport *R = new BugReport(*BT_BadFree, os.str(), N); 825 R->markInteresting(MR); 826 R->addRange(range); 827 C.EmitReport(R); 828 } 829 } 830 831 ProgramStateRef MallocChecker::ReallocMem(CheckerContext &C, 832 const CallExpr *CE, 833 bool FreesOnFail) const { 834 if (CE->getNumArgs() < 2) 835 return 0; 836 837 ProgramStateRef state = C.getState(); 838 const Expr *arg0Expr = CE->getArg(0); 839 const LocationContext *LCtx = C.getLocationContext(); 840 SVal Arg0Val = state->getSVal(arg0Expr, LCtx); 841 if (!isa<DefinedOrUnknownSVal>(Arg0Val)) 842 return 0; 843 DefinedOrUnknownSVal arg0Val = cast<DefinedOrUnknownSVal>(Arg0Val); 844 845 SValBuilder &svalBuilder = C.getSValBuilder(); 846 847 DefinedOrUnknownSVal PtrEQ = 848 svalBuilder.evalEQ(state, arg0Val, svalBuilder.makeNull()); 849 850 // Get the size argument. If there is no size arg then give up. 851 const Expr *Arg1 = CE->getArg(1); 852 if (!Arg1) 853 return 0; 854 855 // Get the value of the size argument. 856 SVal Arg1ValG = state->getSVal(Arg1, LCtx); 857 if (!isa<DefinedOrUnknownSVal>(Arg1ValG)) 858 return 0; 859 DefinedOrUnknownSVal Arg1Val = cast<DefinedOrUnknownSVal>(Arg1ValG); 860 861 // Compare the size argument to 0. 862 DefinedOrUnknownSVal SizeZero = 863 svalBuilder.evalEQ(state, Arg1Val, 864 svalBuilder.makeIntValWithPtrWidth(0, false)); 865 866 ProgramStateRef StatePtrIsNull, StatePtrNotNull; 867 llvm::tie(StatePtrIsNull, StatePtrNotNull) = state->assume(PtrEQ); 868 ProgramStateRef StateSizeIsZero, StateSizeNotZero; 869 llvm::tie(StateSizeIsZero, StateSizeNotZero) = state->assume(SizeZero); 870 // We only assume exceptional states if they are definitely true; if the 871 // state is under-constrained, assume regular realloc behavior. 872 bool PrtIsNull = StatePtrIsNull && !StatePtrNotNull; 873 bool SizeIsZero = StateSizeIsZero && !StateSizeNotZero; 874 875 // If the ptr is NULL and the size is not 0, the call is equivalent to 876 // malloc(size). 877 if ( PrtIsNull && !SizeIsZero) { 878 ProgramStateRef stateMalloc = MallocMemAux(C, CE, CE->getArg(1), 879 UndefinedVal(), StatePtrIsNull); 880 return stateMalloc; 881 } 882 883 if (PrtIsNull && SizeIsZero) 884 return 0; 885 886 // Get the from and to pointer symbols as in toPtr = realloc(fromPtr, size). 887 assert(!PrtIsNull); 888 SymbolRef FromPtr = arg0Val.getAsSymbol(); 889 SVal RetVal = state->getSVal(CE, LCtx); 890 SymbolRef ToPtr = RetVal.getAsSymbol(); 891 if (!FromPtr || !ToPtr) 892 return 0; 893 894 // If the size is 0, free the memory. 895 if (SizeIsZero) 896 if (ProgramStateRef stateFree = FreeMemAux(C, CE, StateSizeIsZero,0,false)){ 897 // The semantics of the return value are: 898 // If size was equal to 0, either NULL or a pointer suitable to be passed 899 // to free() is returned. We just free the input pointer and do not add 900 // any constrains on the output pointer. 901 return stateFree; 902 } 903 904 // Default behavior. 905 if (ProgramStateRef stateFree = FreeMemAux(C, CE, state, 0, false)) { 906 // FIXME: We should copy the content of the original buffer. 907 ProgramStateRef stateRealloc = MallocMemAux(C, CE, CE->getArg(1), 908 UnknownVal(), stateFree); 909 if (!stateRealloc) 910 return 0; 911 stateRealloc = stateRealloc->set<ReallocPairs>(ToPtr, 912 ReallocPair(FromPtr, FreesOnFail)); 913 C.getSymbolManager().addSymbolDependency(ToPtr, FromPtr); 914 return stateRealloc; 915 } 916 return 0; 917 } 918 919 ProgramStateRef MallocChecker::CallocMem(CheckerContext &C, const CallExpr *CE){ 920 if (CE->getNumArgs() < 2) 921 return 0; 922 923 ProgramStateRef state = C.getState(); 924 SValBuilder &svalBuilder = C.getSValBuilder(); 925 const LocationContext *LCtx = C.getLocationContext(); 926 SVal count = state->getSVal(CE->getArg(0), LCtx); 927 SVal elementSize = state->getSVal(CE->getArg(1), LCtx); 928 SVal TotalSize = svalBuilder.evalBinOp(state, BO_Mul, count, elementSize, 929 svalBuilder.getContext().getSizeType()); 930 SVal zeroVal = svalBuilder.makeZeroVal(svalBuilder.getContext().CharTy); 931 932 return MallocMemAux(C, CE, TotalSize, zeroVal, state); 933 } 934 935 LeakInfo 936 MallocChecker::getAllocationSite(const ExplodedNode *N, SymbolRef Sym, 937 CheckerContext &C) const { 938 const LocationContext *LeakContext = N->getLocationContext(); 939 // Walk the ExplodedGraph backwards and find the first node that referred to 940 // the tracked symbol. 941 const ExplodedNode *AllocNode = N; 942 const MemRegion *ReferenceRegion = 0; 943 944 while (N) { 945 ProgramStateRef State = N->getState(); 946 if (!State->get<RegionState>(Sym)) 947 break; 948 949 // Find the most recent expression bound to the symbol in the current 950 // context. 951 if (!ReferenceRegion) { 952 if (const MemRegion *MR = C.getLocationRegionIfPostStore(N)) { 953 SVal Val = State->getSVal(MR); 954 if (Val.getAsLocSymbol() == Sym) 955 ReferenceRegion = MR; 956 } 957 } 958 959 // Allocation node, is the last node in the current context in which the 960 // symbol was tracked. 961 if (N->getLocationContext() == LeakContext) 962 AllocNode = N; 963 N = N->pred_empty() ? NULL : *(N->pred_begin()); 964 } 965 966 ProgramPoint P = AllocNode->getLocation(); 967 const Stmt *AllocationStmt = 0; 968 if (CallExitEnd *Exit = dyn_cast<CallExitEnd>(&P)) 969 AllocationStmt = Exit->getCalleeContext()->getCallSite(); 970 else if (StmtPoint *SP = dyn_cast<StmtPoint>(&P)) 971 AllocationStmt = SP->getStmt(); 972 973 return LeakInfo(AllocationStmt, ReferenceRegion); 974 } 975 976 void MallocChecker::reportLeak(SymbolRef Sym, ExplodedNode *N, 977 CheckerContext &C) const { 978 assert(N); 979 if (!BT_Leak) { 980 BT_Leak.reset(new BugType("Memory leak", "Memory Error")); 981 // Leaks should not be reported if they are post-dominated by a sink: 982 // (1) Sinks are higher importance bugs. 983 // (2) NoReturnFunctionChecker uses sink nodes to represent paths ending 984 // with __noreturn functions such as assert() or exit(). We choose not 985 // to report leaks on such paths. 986 BT_Leak->setSuppressOnSink(true); 987 } 988 989 // Most bug reports are cached at the location where they occurred. 990 // With leaks, we want to unique them by the location where they were 991 // allocated, and only report a single path. 992 PathDiagnosticLocation LocUsedForUniqueing; 993 const Stmt *AllocStmt = 0; 994 const MemRegion *Region = 0; 995 llvm::tie(AllocStmt, Region) = getAllocationSite(N, Sym, C); 996 if (AllocStmt) 997 LocUsedForUniqueing = PathDiagnosticLocation::createBegin(AllocStmt, 998 C.getSourceManager(), N->getLocationContext()); 999 1000 SmallString<200> buf; 1001 llvm::raw_svector_ostream os(buf); 1002 os << "Memory is never released; potential leak"; 1003 // FIXME: Make all region pretty-printing nice enough to show. 1004 if (Region && isa<VarRegion>(Region)) { 1005 os << " of memory pointed to by '"; 1006 Region->dumpPretty(os); 1007 os << '\''; 1008 } 1009 1010 BugReport *R = new BugReport(*BT_Leak, os.str(), N, LocUsedForUniqueing); 1011 R->markInteresting(Sym); 1012 R->addVisitor(new MallocBugVisitor(Sym, true)); 1013 C.EmitReport(R); 1014 } 1015 1016 void MallocChecker::checkDeadSymbols(SymbolReaper &SymReaper, 1017 CheckerContext &C) const 1018 { 1019 if (!SymReaper.hasDeadSymbols()) 1020 return; 1021 1022 ProgramStateRef state = C.getState(); 1023 RegionStateTy RS = state->get<RegionState>(); 1024 RegionStateTy::Factory &F = state->get_context<RegionState>(); 1025 1026 bool generateReport = false; 1027 llvm::SmallVector<SymbolRef, 2> Errors; 1028 for (RegionStateTy::iterator I = RS.begin(), E = RS.end(); I != E; ++I) { 1029 if (SymReaper.isDead(I->first)) { 1030 if (I->second.isAllocated()) { 1031 generateReport = true; 1032 Errors.push_back(I->first); 1033 } 1034 // Remove the dead symbol from the map. 1035 RS = F.remove(RS, I->first); 1036 1037 } 1038 } 1039 1040 // Cleanup the Realloc Pairs Map. 1041 ReallocMap RP = state->get<ReallocPairs>(); 1042 for (ReallocMap::iterator I = RP.begin(), E = RP.end(); I != E; ++I) { 1043 if (SymReaper.isDead(I->first) || 1044 SymReaper.isDead(I->second.ReallocatedSym)) { 1045 state = state->remove<ReallocPairs>(I->first); 1046 } 1047 } 1048 1049 // Generate leak node. 1050 static SimpleProgramPointTag Tag("MallocChecker : DeadSymbolsLeak"); 1051 ExplodedNode *N = C.addTransition(C.getState(), C.getPredecessor(), &Tag); 1052 1053 if (generateReport) { 1054 for (llvm::SmallVector<SymbolRef, 2>::iterator 1055 I = Errors.begin(), E = Errors.end(); I != E; ++I) { 1056 reportLeak(*I, N, C); 1057 } 1058 } 1059 C.addTransition(state->set<RegionState>(RS), N); 1060 } 1061 1062 void MallocChecker::checkEndPath(CheckerContext &C) const { 1063 ProgramStateRef state = C.getState(); 1064 RegionStateTy M = state->get<RegionState>(); 1065 1066 // If inside inlined call, skip it. 1067 if (C.getLocationContext()->getParent() != 0) 1068 return; 1069 1070 for (RegionStateTy::iterator I = M.begin(), E = M.end(); I != E; ++I) { 1071 RefState RS = I->second; 1072 if (RS.isAllocated()) { 1073 ExplodedNode *N = C.addTransition(state); 1074 if (N) 1075 reportLeak(I->first, N, C); 1076 } 1077 } 1078 } 1079 1080 bool MallocChecker::checkEscape(SymbolRef Sym, const Stmt *S, 1081 CheckerContext &C) const { 1082 ProgramStateRef state = C.getState(); 1083 const RefState *RS = state->get<RegionState>(Sym); 1084 if (!RS) 1085 return false; 1086 1087 if (RS->isAllocated()) { 1088 state = state->set<RegionState>(Sym, RefState::getEscaped(S)); 1089 C.addTransition(state); 1090 return true; 1091 } 1092 return false; 1093 } 1094 1095 void MallocChecker::checkPreStmt(const CallExpr *CE, CheckerContext &C) const { 1096 // We will check for double free in the post visit. 1097 if (isFreeFunction(C.getCalleeDecl(CE), C.getASTContext())) 1098 return; 1099 1100 // Check use after free, when a freed pointer is passed to a call. 1101 ProgramStateRef State = C.getState(); 1102 for (CallExpr::const_arg_iterator I = CE->arg_begin(), 1103 E = CE->arg_end(); I != E; ++I) { 1104 const Expr *A = *I; 1105 if (A->getType().getTypePtr()->isAnyPointerType()) { 1106 SymbolRef Sym = State->getSVal(A, C.getLocationContext()).getAsSymbol(); 1107 if (!Sym) 1108 continue; 1109 if (checkUseAfterFree(Sym, C, A)) 1110 return; 1111 } 1112 } 1113 } 1114 1115 void MallocChecker::checkPreStmt(const ReturnStmt *S, CheckerContext &C) const { 1116 const Expr *E = S->getRetValue(); 1117 if (!E) 1118 return; 1119 1120 // Check if we are returning a symbol. 1121 ProgramStateRef State = C.getState(); 1122 SVal RetVal = State->getSVal(E, C.getLocationContext()); 1123 SymbolRef Sym = RetVal.getAsSymbol(); 1124 if (!Sym) 1125 // If we are returning a field of the allocated struct or an array element, 1126 // the callee could still free the memory. 1127 // TODO: This logic should be a part of generic symbol escape callback. 1128 if (const MemRegion *MR = RetVal.getAsRegion()) 1129 if (isa<FieldRegion>(MR) || isa<ElementRegion>(MR)) 1130 if (const SymbolicRegion *BMR = 1131 dyn_cast<SymbolicRegion>(MR->getBaseRegion())) 1132 Sym = BMR->getSymbol(); 1133 1134 // Check if we are returning freed memory. 1135 if (Sym) 1136 if (checkUseAfterFree(Sym, C, E)) 1137 return; 1138 1139 // If this function body is not inlined, stop tracking any returned symbols. 1140 if (C.getLocationContext()->getParent() == 0) { 1141 State = 1142 State->scanReachableSymbols<StopTrackingCallback>(RetVal).getState(); 1143 C.addTransition(State); 1144 } 1145 } 1146 1147 // TODO: Blocks should be either inlined or should call invalidate regions 1148 // upon invocation. After that's in place, special casing here will not be 1149 // needed. 1150 void MallocChecker::checkPostStmt(const BlockExpr *BE, 1151 CheckerContext &C) const { 1152 1153 // Scan the BlockDecRefExprs for any object the retain count checker 1154 // may be tracking. 1155 if (!BE->getBlockDecl()->hasCaptures()) 1156 return; 1157 1158 ProgramStateRef state = C.getState(); 1159 const BlockDataRegion *R = 1160 cast<BlockDataRegion>(state->getSVal(BE, 1161 C.getLocationContext()).getAsRegion()); 1162 1163 BlockDataRegion::referenced_vars_iterator I = R->referenced_vars_begin(), 1164 E = R->referenced_vars_end(); 1165 1166 if (I == E) 1167 return; 1168 1169 SmallVector<const MemRegion*, 10> Regions; 1170 const LocationContext *LC = C.getLocationContext(); 1171 MemRegionManager &MemMgr = C.getSValBuilder().getRegionManager(); 1172 1173 for ( ; I != E; ++I) { 1174 const VarRegion *VR = *I; 1175 if (VR->getSuperRegion() == R) { 1176 VR = MemMgr.getVarRegion(VR->getDecl(), LC); 1177 } 1178 Regions.push_back(VR); 1179 } 1180 1181 state = 1182 state->scanReachableSymbols<StopTrackingCallback>(Regions.data(), 1183 Regions.data() + Regions.size()).getState(); 1184 C.addTransition(state); 1185 } 1186 1187 bool MallocChecker::isReleased(SymbolRef Sym, CheckerContext &C) const { 1188 assert(Sym); 1189 const RefState *RS = C.getState()->get<RegionState>(Sym); 1190 return (RS && RS->isReleased()); 1191 } 1192 1193 bool MallocChecker::checkUseAfterFree(SymbolRef Sym, CheckerContext &C, 1194 const Stmt *S) const { 1195 if (isReleased(Sym, C)) { 1196 if (ExplodedNode *N = C.generateSink()) { 1197 if (!BT_UseFree) 1198 BT_UseFree.reset(new BugType("Use-after-free", "Memory Error")); 1199 1200 BugReport *R = new BugReport(*BT_UseFree, 1201 "Use of memory after it is freed",N); 1202 if (S) 1203 R->addRange(S->getSourceRange()); 1204 R->markInteresting(Sym); 1205 R->addVisitor(new MallocBugVisitor(Sym)); 1206 C.EmitReport(R); 1207 return true; 1208 } 1209 } 1210 return false; 1211 } 1212 1213 // Check if the location is a freed symbolic region. 1214 void MallocChecker::checkLocation(SVal l, bool isLoad, const Stmt *S, 1215 CheckerContext &C) const { 1216 SymbolRef Sym = l.getLocSymbolInBase(); 1217 if (Sym) 1218 checkUseAfterFree(Sym, C, S); 1219 } 1220 1221 //===----------------------------------------------------------------------===// 1222 // Check various ways a symbol can be invalidated. 1223 // TODO: This logic (the next 3 functions) is copied/similar to the 1224 // RetainRelease checker. We might want to factor this out. 1225 //===----------------------------------------------------------------------===// 1226 1227 // Stop tracking symbols when a value escapes as a result of checkBind. 1228 // A value escapes in three possible cases: 1229 // (1) we are binding to something that is not a memory region. 1230 // (2) we are binding to a memregion that does not have stack storage 1231 // (3) we are binding to a memregion with stack storage that the store 1232 // does not understand. 1233 void MallocChecker::checkBind(SVal loc, SVal val, const Stmt *S, 1234 CheckerContext &C) const { 1235 // Are we storing to something that causes the value to "escape"? 1236 bool escapes = true; 1237 ProgramStateRef state = C.getState(); 1238 1239 if (loc::MemRegionVal *regionLoc = dyn_cast<loc::MemRegionVal>(&loc)) { 1240 escapes = !regionLoc->getRegion()->hasStackStorage(); 1241 1242 if (!escapes) { 1243 // To test (3), generate a new state with the binding added. If it is 1244 // the same state, then it escapes (since the store cannot represent 1245 // the binding). 1246 // Do this only if we know that the store is not supposed to generate the 1247 // same state. 1248 SVal StoredVal = state->getSVal(regionLoc->getRegion()); 1249 if (StoredVal != val) 1250 escapes = (state == (state->bindLoc(*regionLoc, val))); 1251 } 1252 } 1253 1254 // If our store can represent the binding and we aren't storing to something 1255 // that doesn't have local storage then just return and have the simulation 1256 // state continue as is. 1257 if (!escapes) 1258 return; 1259 1260 // Otherwise, find all symbols referenced by 'val' that we are tracking 1261 // and stop tracking them. 1262 state = state->scanReachableSymbols<StopTrackingCallback>(val).getState(); 1263 C.addTransition(state); 1264 } 1265 1266 // If a symbolic region is assumed to NULL (or another constant), stop tracking 1267 // it - assuming that allocation failed on this path. 1268 ProgramStateRef MallocChecker::evalAssume(ProgramStateRef state, 1269 SVal Cond, 1270 bool Assumption) const { 1271 RegionStateTy RS = state->get<RegionState>(); 1272 for (RegionStateTy::iterator I = RS.begin(), E = RS.end(); I != E; ++I) { 1273 // If the symbol is assumed to NULL or another constant, this will 1274 // return an APSInt*. 1275 if (state->getSymVal(I.getKey())) 1276 state = state->remove<RegionState>(I.getKey()); 1277 } 1278 1279 // Realloc returns 0 when reallocation fails, which means that we should 1280 // restore the state of the pointer being reallocated. 1281 ReallocMap RP = state->get<ReallocPairs>(); 1282 for (ReallocMap::iterator I = RP.begin(), E = RP.end(); I != E; ++I) { 1283 // If the symbol is assumed to NULL or another constant, this will 1284 // return an APSInt*. 1285 if (state->getSymVal(I.getKey())) { 1286 SymbolRef ReallocSym = I.getData().ReallocatedSym; 1287 const RefState *RS = state->get<RegionState>(ReallocSym); 1288 if (RS) { 1289 if (RS->isReleased() && ! I.getData().IsFreeOnFailure) 1290 state = state->set<RegionState>(ReallocSym, 1291 RefState::getAllocated(RS->getStmt())); 1292 } 1293 state = state->remove<ReallocPairs>(I.getKey()); 1294 } 1295 } 1296 1297 return state; 1298 } 1299 1300 // Check if the function is known to us. So, for example, we could 1301 // conservatively assume it can free/reallocate its pointer arguments. 1302 // (We assume that the pointers cannot escape through calls to system 1303 // functions not handled by this checker.) 1304 bool MallocChecker::doesNotFreeMemory(const CallEvent *Call, 1305 ProgramStateRef State) const { 1306 assert(Call); 1307 1308 // For now, assume that any C++ call can free memory. 1309 // TODO: If we want to be more optimistic here, we'll need to make sure that 1310 // regions escape to C++ containers. They seem to do that even now, but for 1311 // mysterious reasons. 1312 if (!(isa<FunctionCall>(Call) || isa<ObjCMethodCall>(Call))) 1313 return false; 1314 1315 // Check Objective-C messages by selector name. 1316 if (const ObjCMethodCall *Msg = dyn_cast<ObjCMethodCall>(Call)) { 1317 // If it's not a framework call, or if it takes a callback, assume it 1318 // can free memory. 1319 if (!Call->isInSystemHeader() || Call->hasNonZeroCallbackArg()) 1320 return false; 1321 1322 Selector S = Msg->getSelector(); 1323 1324 // Whitelist the ObjC methods which do free memory. 1325 // - Anything containing 'freeWhenDone' param set to 1. 1326 // Ex: dataWithBytesNoCopy:length:freeWhenDone. 1327 for (unsigned i = 1; i < S.getNumArgs(); ++i) { 1328 if (S.getNameForSlot(i).equals("freeWhenDone")) { 1329 if (Call->getArgSVal(i).isConstant(1)) 1330 return false; 1331 else 1332 return true; 1333 } 1334 } 1335 1336 // If the first selector ends with NoCopy, assume that the ownership is 1337 // transferred as well. 1338 // Ex: [NSData dataWithBytesNoCopy:bytes length:10]; 1339 StringRef FirstSlot = S.getNameForSlot(0); 1340 if (FirstSlot.endswith("NoCopy")) 1341 return false; 1342 1343 // If the first selector starts with addPointer, insertPointer, 1344 // or replacePointer, assume we are dealing with NSPointerArray or similar. 1345 // This is similar to C++ containers (vector); we still might want to check 1346 // that the pointers get freed by following the container itself. 1347 if (FirstSlot.startswith("addPointer") || 1348 FirstSlot.startswith("insertPointer") || 1349 FirstSlot.startswith("replacePointer")) { 1350 return false; 1351 } 1352 1353 // Otherwise, assume that the method does not free memory. 1354 // Most framework methods do not free memory. 1355 return true; 1356 } 1357 1358 // At this point the only thing left to handle is straight function calls. 1359 const FunctionDecl *FD = cast<FunctionCall>(Call)->getDecl(); 1360 if (!FD) 1361 return false; 1362 1363 ASTContext &ASTC = State->getStateManager().getContext(); 1364 1365 // If it's one of the allocation functions we can reason about, we model 1366 // its behavior explicitly. 1367 if (isMemFunction(FD, ASTC)) 1368 return true; 1369 1370 // If it's not a system call, assume it frees memory. 1371 if (!Call->isInSystemHeader()) 1372 return false; 1373 1374 // White list the system functions whose arguments escape. 1375 const IdentifierInfo *II = FD->getIdentifier(); 1376 if (!II) 1377 return false; 1378 StringRef FName = II->getName(); 1379 1380 // White list the 'XXXNoCopy' CoreFoundation functions. 1381 // We specifically check these before 1382 if (FName.endswith("NoCopy")) { 1383 // Look for the deallocator argument. We know that the memory ownership 1384 // is not transferred only if the deallocator argument is 1385 // 'kCFAllocatorNull'. 1386 for (unsigned i = 1; i < Call->getNumArgs(); ++i) { 1387 const Expr *ArgE = Call->getArgExpr(i)->IgnoreParenCasts(); 1388 if (const DeclRefExpr *DE = dyn_cast<DeclRefExpr>(ArgE)) { 1389 StringRef DeallocatorName = DE->getFoundDecl()->getName(); 1390 if (DeallocatorName == "kCFAllocatorNull") 1391 return true; 1392 } 1393 } 1394 return false; 1395 } 1396 1397 // Associating streams with malloced buffers. The pointer can escape if 1398 // 'closefn' is specified (and if that function does free memory), 1399 // but it will not if closefn is not specified. 1400 // Currently, we do not inspect the 'closefn' function (PR12101). 1401 if (FName == "funopen") 1402 if (Call->getNumArgs() >= 4 && Call->getArgSVal(4).isConstant(0)) 1403 return true; 1404 1405 // Do not warn on pointers passed to 'setbuf' when used with std streams, 1406 // these leaks might be intentional when setting the buffer for stdio. 1407 // http://stackoverflow.com/questions/2671151/who-frees-setvbuf-buffer 1408 if (FName == "setbuf" || FName =="setbuffer" || 1409 FName == "setlinebuf" || FName == "setvbuf") { 1410 if (Call->getNumArgs() >= 1) { 1411 const Expr *ArgE = Call->getArgExpr(0)->IgnoreParenCasts(); 1412 if (const DeclRefExpr *ArgDRE = dyn_cast<DeclRefExpr>(ArgE)) 1413 if (const VarDecl *D = dyn_cast<VarDecl>(ArgDRE->getDecl())) 1414 if (D->getCanonicalDecl()->getName().find("std") != StringRef::npos) 1415 return false; 1416 } 1417 } 1418 1419 // A bunch of other functions which either take ownership of a pointer or 1420 // wrap the result up in a struct or object, meaning it can be freed later. 1421 // (See RetainCountChecker.) Not all the parameters here are invalidated, 1422 // but the Malloc checker cannot differentiate between them. The right way 1423 // of doing this would be to implement a pointer escapes callback. 1424 if (FName == "CGBitmapContextCreate" || 1425 FName == "CGBitmapContextCreateWithData" || 1426 FName == "CVPixelBufferCreateWithBytes" || 1427 FName == "CVPixelBufferCreateWithPlanarBytes" || 1428 FName == "OSAtomicEnqueue") { 1429 return false; 1430 } 1431 1432 // Handle cases where we know a buffer's /address/ can escape. 1433 // Note that the above checks handle some special cases where we know that 1434 // even though the address escapes, it's still our responsibility to free the 1435 // buffer. 1436 if (Call->argumentsMayEscape()) 1437 return false; 1438 1439 // Otherwise, assume that the function does not free memory. 1440 // Most system calls do not free the memory. 1441 return true; 1442 } 1443 1444 // If the symbol we are tracking is invalidated, but not explicitly (ex: the &p 1445 // escapes, when we are tracking p), do not track the symbol as we cannot reason 1446 // about it anymore. 1447 ProgramStateRef 1448 MallocChecker::checkRegionChanges(ProgramStateRef State, 1449 const StoreManager::InvalidatedSymbols *invalidated, 1450 ArrayRef<const MemRegion *> ExplicitRegions, 1451 ArrayRef<const MemRegion *> Regions, 1452 const CallEvent *Call) const { 1453 if (!invalidated || invalidated->empty()) 1454 return State; 1455 llvm::SmallPtrSet<SymbolRef, 8> WhitelistedSymbols; 1456 1457 // If it's a call which might free or reallocate memory, we assume that all 1458 // regions (explicit and implicit) escaped. 1459 1460 // Otherwise, whitelist explicit pointers; we still can track them. 1461 if (!Call || doesNotFreeMemory(Call, State)) { 1462 for (ArrayRef<const MemRegion *>::iterator I = ExplicitRegions.begin(), 1463 E = ExplicitRegions.end(); I != E; ++I) { 1464 if (const SymbolicRegion *R = (*I)->StripCasts()->getAs<SymbolicRegion>()) 1465 WhitelistedSymbols.insert(R->getSymbol()); 1466 } 1467 } 1468 1469 for (StoreManager::InvalidatedSymbols::const_iterator I=invalidated->begin(), 1470 E = invalidated->end(); I!=E; ++I) { 1471 SymbolRef sym = *I; 1472 if (WhitelistedSymbols.count(sym)) 1473 continue; 1474 // The symbol escaped. Note, we assume that if the symbol is released, 1475 // passing it out will result in a use after free. We also keep tracking 1476 // relinquished symbols. 1477 if (const RefState *RS = State->get<RegionState>(sym)) { 1478 if (RS->isAllocated()) 1479 State = State->set<RegionState>(sym, 1480 RefState::getEscaped(RS->getStmt())); 1481 } 1482 } 1483 return State; 1484 } 1485 1486 static SymbolRef findFailedReallocSymbol(ProgramStateRef currState, 1487 ProgramStateRef prevState) { 1488 ReallocMap currMap = currState->get<ReallocPairs>(); 1489 ReallocMap prevMap = prevState->get<ReallocPairs>(); 1490 1491 for (ReallocMap::iterator I = prevMap.begin(), E = prevMap.end(); 1492 I != E; ++I) { 1493 SymbolRef sym = I.getKey(); 1494 if (!currMap.lookup(sym)) 1495 return sym; 1496 } 1497 1498 return NULL; 1499 } 1500 1501 PathDiagnosticPiece * 1502 MallocChecker::MallocBugVisitor::VisitNode(const ExplodedNode *N, 1503 const ExplodedNode *PrevN, 1504 BugReporterContext &BRC, 1505 BugReport &BR) { 1506 ProgramStateRef state = N->getState(); 1507 ProgramStateRef statePrev = PrevN->getState(); 1508 1509 const RefState *RS = state->get<RegionState>(Sym); 1510 const RefState *RSPrev = statePrev->get<RegionState>(Sym); 1511 if (!RS) 1512 return 0; 1513 1514 const Stmt *S = 0; 1515 const char *Msg = 0; 1516 StackHintGeneratorForSymbol *StackHint = 0; 1517 1518 // Retrieve the associated statement. 1519 ProgramPoint ProgLoc = N->getLocation(); 1520 if (StmtPoint *SP = dyn_cast<StmtPoint>(&ProgLoc)) 1521 S = SP->getStmt(); 1522 else if (CallExitEnd *Exit = dyn_cast<CallExitEnd>(&ProgLoc)) 1523 S = Exit->getCalleeContext()->getCallSite(); 1524 // If an assumption was made on a branch, it should be caught 1525 // here by looking at the state transition. 1526 else if (BlockEdge *Edge = dyn_cast<BlockEdge>(&ProgLoc)) { 1527 const CFGBlock *srcBlk = Edge->getSrc(); 1528 S = srcBlk->getTerminator(); 1529 } 1530 if (!S) 1531 return 0; 1532 1533 // FIXME: We will eventually need to handle non-statement-based events 1534 // (__attribute__((cleanup))). 1535 1536 // Find out if this is an interesting point and what is the kind. 1537 if (Mode == Normal) { 1538 if (isAllocated(RS, RSPrev, S)) { 1539 Msg = "Memory is allocated"; 1540 StackHint = new StackHintGeneratorForSymbol(Sym, 1541 "Returned allocated memory"); 1542 } else if (isReleased(RS, RSPrev, S)) { 1543 Msg = "Memory is released"; 1544 StackHint = new StackHintGeneratorForSymbol(Sym, 1545 "Returned released memory"); 1546 } else if (isRelinquished(RS, RSPrev, S)) { 1547 Msg = "Memory ownership is transfered"; 1548 StackHint = new StackHintGeneratorForSymbol(Sym, ""); 1549 } else if (isReallocFailedCheck(RS, RSPrev, S)) { 1550 Mode = ReallocationFailed; 1551 Msg = "Reallocation failed"; 1552 StackHint = new StackHintGeneratorForReallocationFailed(Sym, 1553 "Reallocation failed"); 1554 1555 if (SymbolRef sym = findFailedReallocSymbol(state, statePrev)) { 1556 // Is it possible to fail two reallocs WITHOUT testing in between? 1557 assert((!FailedReallocSymbol || FailedReallocSymbol == sym) && 1558 "We only support one failed realloc at a time."); 1559 BR.markInteresting(sym); 1560 FailedReallocSymbol = sym; 1561 } 1562 } 1563 1564 // We are in a special mode if a reallocation failed later in the path. 1565 } else if (Mode == ReallocationFailed) { 1566 assert(FailedReallocSymbol && "No symbol to look for."); 1567 1568 // Is this is the first appearance of the reallocated symbol? 1569 if (!statePrev->get<RegionState>(FailedReallocSymbol)) { 1570 // We're at the reallocation point. 1571 Msg = "Attempt to reallocate memory"; 1572 StackHint = new StackHintGeneratorForSymbol(Sym, 1573 "Returned reallocated memory"); 1574 FailedReallocSymbol = NULL; 1575 Mode = Normal; 1576 } 1577 } 1578 1579 if (!Msg) 1580 return 0; 1581 assert(StackHint); 1582 1583 // Generate the extra diagnostic. 1584 PathDiagnosticLocation Pos(S, BRC.getSourceManager(), 1585 N->getLocationContext()); 1586 return new PathDiagnosticEventPiece(Pos, Msg, true, StackHint); 1587 } 1588 1589 void MallocChecker::printState(raw_ostream &Out, ProgramStateRef State, 1590 const char *NL, const char *Sep) const { 1591 1592 RegionStateTy RS = State->get<RegionState>(); 1593 1594 if (!RS.isEmpty()) 1595 Out << "Has Malloc data" << NL; 1596 } 1597 1598 #define REGISTER_CHECKER(name) \ 1599 void ento::register##name(CheckerManager &mgr) {\ 1600 registerCStringCheckerBasic(mgr); \ 1601 mgr.registerChecker<MallocChecker>()->Filter.C##name = true;\ 1602 } 1603 1604 REGISTER_CHECKER(MallocPessimistic) 1605 REGISTER_CHECKER(MallocOptimistic) 1606