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