MallocChecker.cpp revision 218fce0f113f82fc28f2fb0c4b555ae2901b8a93
1603c4be006d8c53905d736bf1f19a49f5ce98276Alexey Samsonov//=== MallocChecker.cpp - A malloc/free checker -------------------*- C++ -*--//
27ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany//
37ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany//                     The LLVM Compiler Infrastructure
47ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany//
57ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany// This file is distributed under the University of Illinois Open Source
67ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany// License. See LICENSE.TXT for details.
77ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany//
87ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany//===----------------------------------------------------------------------===//
97ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany//
107ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany// This file defines malloc/free checker, which checks for potential memory
117ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany// leaks, double free, and use-after-free problems.
127ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany//
137ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany//===----------------------------------------------------------------------===//
147ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany
157ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany#include "ClangSACheckers.h"
167ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany#include "InterCheckerAPI.h"
177ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany#include "clang/StaticAnalyzer/Core/Checker.h"
187ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany#include "clang/StaticAnalyzer/Core/CheckerManager.h"
197ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
207ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany#include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
217ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany#include "clang/StaticAnalyzer/Core/PathSensitive/ObjCMessage.h"
227ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany#include "clang/StaticAnalyzer/Core/PathSensitive/ProgramState.h"
237ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany#include "clang/StaticAnalyzer/Core/PathSensitive/ProgramStateTrait.h"
247ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany#include "clang/StaticAnalyzer/Core/PathSensitive/SymbolManager.h"
257ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany#include "clang/Basic/SourceManager.h"
267ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany#include "llvm/ADT/ImmutableMap.h"
277ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany#include "llvm/ADT/SmallString.h"
287ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany#include "llvm/ADT/STLExtras.h"
297ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany#include <climits>
307ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany
317ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryanyusing namespace clang;
327ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryanyusing namespace ento;
337ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany
347ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryanynamespace {
357ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany
367ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryanyclass RefState {
377ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany  enum Kind { AllocateUnchecked, AllocateFailed, Released, Escaped,
387ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany              Relinquished } K;
397ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany  const Stmt *S;
407ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany
417ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryanypublic:
427ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany  RefState(Kind k, const Stmt *s) : K(k), S(s) {}
437ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany
447ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany  bool isAllocated() const { return K == AllocateUnchecked; }
457ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany  bool isReleased() const { return K == Released; }
467ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany
477ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany  const Stmt *getStmt() const { return S; }
487ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany
497ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany  bool operator==(const RefState &X) const {
507ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany    return K == X.K && S == X.S;
517ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany  }
527ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany
537ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany  static RefState getAllocateUnchecked(const Stmt *s) {
547ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany    return RefState(AllocateUnchecked, s);
557ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany  }
567ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany  static RefState getAllocateFailed() {
577ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany    return RefState(AllocateFailed, 0);
587ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany  }
597ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany  static RefState getReleased(const Stmt *s) { return RefState(Released, s); }
607ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany  static RefState getEscaped(const Stmt *s) { return RefState(Escaped, s); }
617ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany  static RefState getRelinquished(const Stmt *s) {
627ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany    return RefState(Relinquished, s);
637ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany  }
647ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany
657ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany  void Profile(llvm::FoldingSetNodeID &ID) const {
667ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany    ID.AddInteger(K);
677ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany    ID.AddPointer(S);
687ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany  }
697ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany};
707ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany
717ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryanystruct ReallocPair {
727ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany  SymbolRef ReallocatedSym;
737ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany  bool IsFreeOnFailure;
747ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany  ReallocPair(SymbolRef S, bool F) : ReallocatedSym(S), IsFreeOnFailure(F) {}
757ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany  void Profile(llvm::FoldingSetNodeID &ID) const {
767ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany    ID.AddInteger(IsFreeOnFailure);
777ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany    ID.AddPointer(ReallocatedSym);
787ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany  }
797ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany  bool operator==(const ReallocPair &X) const {
807ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany    return ReallocatedSym == X.ReallocatedSym &&
817ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany           IsFreeOnFailure == X.IsFreeOnFailure;
827ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany  }
837ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany};
847ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany
857ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryanyclass MallocChecker : public Checker<check::DeadSymbols,
867ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany                                     check::EndPath,
877ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany                                     check::PreStmt<ReturnStmt>,
887ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany                                     check::PreStmt<CallExpr>,
897ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany                                     check::PostStmt<CallExpr>,
907ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany                                     check::Location,
917ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany                                     check::Bind,
927ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany                                     eval::Assume,
937ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany                                     check::RegionChanges>
947ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany{
957ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany  mutable OwningPtr<BugType> BT_DoubleFree;
967ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany  mutable OwningPtr<BugType> BT_Leak;
977ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany  mutable OwningPtr<BugType> BT_UseFree;
987ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany  mutable OwningPtr<BugType> BT_BadFree;
997ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany  mutable IdentifierInfo *II_malloc, *II_free, *II_realloc, *II_calloc,
1007ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany                         *II_valloc, *II_reallocf, *II_strndup, *II_strdup;
1017ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany
1027ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryanypublic:
1037ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany  MallocChecker() : II_malloc(0), II_free(0), II_realloc(0), II_calloc(0),
1047ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany                    II_valloc(0), II_reallocf(0), II_strndup(0), II_strdup(0) {}
1057ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany
1067ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany  /// In pessimistic mode, the checker assumes that it does not know which
1077ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany  /// functions might free the memory.
1087ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany  struct ChecksFilter {
1097ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany    DefaultBool CMallocPessimistic;
1107ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany    DefaultBool CMallocOptimistic;
1117ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany  };
1127ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany
1137ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany  ChecksFilter Filter;
1147ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany
1157ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany  void checkPreStmt(const CallExpr *S, CheckerContext &C) const;
1167ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany  void checkPostStmt(const CallExpr *CE, CheckerContext &C) const;
1177ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany  void checkDeadSymbols(SymbolReaper &SymReaper, CheckerContext &C) const;
1187ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany  void checkEndPath(CheckerContext &C) const;
1197ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany  void checkPreStmt(const ReturnStmt *S, CheckerContext &C) const;
1207ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany  ProgramStateRef evalAssume(ProgramStateRef state, SVal Cond,
1217ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany                            bool Assumption) const;
1227ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany  void checkLocation(SVal l, bool isLoad, const Stmt *S,
1237ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany                     CheckerContext &C) const;
1247ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany  void checkBind(SVal location, SVal val, const Stmt*S,
1257ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany                 CheckerContext &C) const;
1267ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany  ProgramStateRef
1277ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany  checkRegionChanges(ProgramStateRef state,
1287ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany                     const StoreManager::InvalidatedSymbols *invalidated,
1297ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany                     ArrayRef<const MemRegion *> ExplicitRegions,
1307ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany                     ArrayRef<const MemRegion *> Regions,
1317ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany                     const CallOrObjCMessage *Call) const;
1327ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany  bool wantsRegionChangeUpdate(ProgramStateRef state) const {
1337ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany    return true;
1347ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany  }
1357ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany
1367ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryanyprivate:
1377ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany  void initIdentifierInfo(ASTContext &C) const;
1387ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany
1397ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany  /// Check if this is one of the functions which can allocate/reallocate memory
1407ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany  /// pointed to by one of its arguments.
1417ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany  bool isMemFunction(const FunctionDecl *FD, ASTContext &C) const;
1427ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany
1437ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany  static ProgramStateRef MallocMemReturnsAttr(CheckerContext &C,
1447ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany                                              const CallExpr *CE,
1457ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany                                              const OwnershipAttr* Att);
1467ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany  static ProgramStateRef MallocMemAux(CheckerContext &C, const CallExpr *CE,
1477ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany                                     const Expr *SizeEx, SVal Init,
1487ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany                                     ProgramStateRef state) {
1497ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany    return MallocMemAux(C, CE,
1507ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany                        state->getSVal(SizeEx, C.getLocationContext()),
1517ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany                        Init, state);
1527ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany  }
1537ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany
1547ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany  static ProgramStateRef MallocMemAux(CheckerContext &C, const CallExpr *CE,
1557ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany                                     SVal SizeEx, SVal Init,
1567ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany                                     ProgramStateRef state);
1577ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany
1587ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany  /// Update the RefState to reflect the new memory allocation.
1597ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany  static ProgramStateRef MallocUpdateRefState(CheckerContext &C,
1607ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany                                              const CallExpr *CE,
1617ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany                                              ProgramStateRef state);
1627ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany
1637ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany  ProgramStateRef FreeMemAttr(CheckerContext &C, const CallExpr *CE,
1647ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany                              const OwnershipAttr* Att) const;
1657ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany  ProgramStateRef FreeMemAux(CheckerContext &C, const CallExpr *CE,
1667ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany                                 ProgramStateRef state, unsigned Num,
1677ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany                                 bool Hold) const;
1687ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany
169d51a1a10cba87be50e9ada9fa21337c387edb237Dmitry Vyukov  ProgramStateRef ReallocMem(CheckerContext &C, const CallExpr *CE,
1707ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany                             bool FreesMemOnFailure) const;
1717ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany  static ProgramStateRef CallocMem(CheckerContext &C, const CallExpr *CE);
1727ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany
173d51a1a10cba87be50e9ada9fa21337c387edb237Dmitry Vyukov  bool checkEscape(SymbolRef Sym, const Stmt *S, CheckerContext &C) const;
1747ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany  bool checkUseAfterFree(SymbolRef Sym, CheckerContext &C,
1757ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany                         const Stmt *S = 0) const;
1767ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany
1777ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany  /// Check if the function is not known to us. So, for example, we could
1787ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany  /// conservatively assume it can free/reallocate it's pointer arguments.
1797ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany  bool doesNotFreeMemory(const CallOrObjCMessage *Call,
1807ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany                         ProgramStateRef State) const;
1817ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany
1827ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany  static bool SummarizeValue(raw_ostream &os, SVal V);
1837ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany  static bool SummarizeRegion(raw_ostream &os, const MemRegion *MR);
184d51a1a10cba87be50e9ada9fa21337c387edb237Dmitry Vyukov  void ReportBadFree(CheckerContext &C, SVal ArgVal, SourceRange range) const;
1857ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany
1867ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany  /// Find the location of the allocation for Sym on the path leading to the
1877ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany  /// exploded node N.
1887ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany  const Stmt *getAllocationSite(const ExplodedNode *N, SymbolRef Sym,
1897ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany                                CheckerContext &C) const;
1907ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany
1917ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany  void reportLeak(SymbolRef Sym, ExplodedNode *N, CheckerContext &C) const;
1927ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany
1937ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany  /// The bug visitor which allows us to print extra diagnostics along the
1947ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany  /// BugReport path. For example, showing the allocation site of the leaked
1957ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany  /// region.
1967ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany  class MallocBugVisitor : public BugReporterVisitor {
197d51a1a10cba87be50e9ada9fa21337c387edb237Dmitry Vyukov  protected:
1987ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany    enum NotificationMode {
1997ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany      Normal,
2007ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany      Complete,
2017ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany      ReallocationFailed
2027ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany    };
203d51a1a10cba87be50e9ada9fa21337c387edb237Dmitry Vyukov
2047ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany    // The allocated region symbol tracked by the main analysis.
2057ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany    SymbolRef Sym;
2067ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany    NotificationMode Mode;
2077ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany
2087ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany  public:
2097ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany    MallocBugVisitor(SymbolRef S) : Sym(S), Mode(Normal) {}
2107ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany    virtual ~MallocBugVisitor() {}
2117ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany
2127ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany    void Profile(llvm::FoldingSetNodeID &ID) const {
2137ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany      static int X = 0;
2147ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany      ID.AddPointer(&X);
2157ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany      ID.AddPointer(Sym);
2167ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany    }
2177ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany
2187ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany    inline bool isAllocated(const RefState *S, const RefState *SPrev,
2197ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany                            const Stmt *Stmt) {
2207ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany      // Did not track -> allocated. Other state (released) -> allocated.
2217ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany      return (Stmt && isa<CallExpr>(Stmt) &&
2227ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany              (S && S->isAllocated()) && (!SPrev || !SPrev->isAllocated()));
2237ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany    }
2247ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany
2257ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany    inline bool isReleased(const RefState *S, const RefState *SPrev,
2267ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany                           const Stmt *Stmt) {
2277ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany      // Did not track -> released. Other state (allocated) -> released.
2287ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany      return (Stmt && isa<CallExpr>(Stmt) &&
2297ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany              (S && S->isReleased()) && (!SPrev || !SPrev->isReleased()));
2307ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany    }
2317ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany
2327ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany    inline bool isReallocFailedCheck(const RefState *S, const RefState *SPrev,
2337ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany                                     const Stmt *Stmt) {
234d51a1a10cba87be50e9ada9fa21337c387edb237Dmitry Vyukov      // If the expression is not a call, and the state change is
2357ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany      // released -> allocated, it must be the realloc return value
2367ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany      // check. If we have to handle more cases here, it might be cleaner just
2377ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany      // to track this extra bit in the state itself.
2387ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany      return ((!Stmt || !isa<CallExpr>(Stmt)) &&
2397ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany              (S && S->isAllocated()) && (SPrev && !SPrev->isAllocated()));
2407ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany    }
2417ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany
2427ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany    PathDiagnosticPiece *VisitNode(const ExplodedNode *N,
2437ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany                                   const ExplodedNode *PrevN,
2447ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany                                   BugReporterContext &BRC,
245ba5e99668e3030cc5bab357a04271a1bdbac209cAlexey Samsonov                                   BugReport &BR);
246  };
247};
248} // end anonymous namespace
249
250typedef llvm::ImmutableMap<SymbolRef, RefState> RegionStateTy;
251typedef llvm::ImmutableMap<SymbolRef, ReallocPair > ReallocMap;
252class RegionState {};
253class ReallocPairs {};
254namespace clang {
255namespace ento {
256  template <>
257  struct ProgramStateTrait<RegionState>
258    : public ProgramStatePartialTrait<RegionStateTy> {
259    static void *GDMIndex() { static int x; return &x; }
260  };
261
262  template <>
263  struct ProgramStateTrait<ReallocPairs>
264    : public ProgramStatePartialTrait<ReallocMap> {
265    static void *GDMIndex() { static int x; return &x; }
266  };
267}
268}
269
270namespace {
271class StopTrackingCallback : public SymbolVisitor {
272  ProgramStateRef state;
273public:
274  StopTrackingCallback(ProgramStateRef st) : state(st) {}
275  ProgramStateRef getState() const { return state; }
276
277  bool VisitSymbol(SymbolRef sym) {
278    state = state->remove<RegionState>(sym);
279    return true;
280  }
281};
282} // end anonymous namespace
283
284void MallocChecker::initIdentifierInfo(ASTContext &Ctx) const {
285  if (!II_malloc)
286    II_malloc = &Ctx.Idents.get("malloc");
287  if (!II_free)
288    II_free = &Ctx.Idents.get("free");
289  if (!II_realloc)
290    II_realloc = &Ctx.Idents.get("realloc");
291  if (!II_reallocf)
292    II_reallocf = &Ctx.Idents.get("reallocf");
293  if (!II_calloc)
294    II_calloc = &Ctx.Idents.get("calloc");
295  if (!II_valloc)
296    II_valloc = &Ctx.Idents.get("valloc");
297  if (!II_strdup)
298    II_strdup = &Ctx.Idents.get("strdup");
299  if (!II_strndup)
300    II_strndup = &Ctx.Idents.get("strndup");
301}
302
303bool MallocChecker::isMemFunction(const FunctionDecl *FD, ASTContext &C) const {
304  if (!FD)
305    return false;
306  IdentifierInfo *FunI = FD->getIdentifier();
307  if (!FunI)
308    return false;
309
310  initIdentifierInfo(C);
311
312  if (FunI == II_malloc || FunI == II_free || FunI == II_realloc ||
313      FunI == II_reallocf || FunI == II_calloc || FunI == II_valloc ||
314      FunI == II_strdup || FunI == II_strndup)
315    return true;
316
317  if (Filter.CMallocOptimistic && FD->hasAttrs() &&
318      FD->specific_attr_begin<OwnershipAttr>() !=
319          FD->specific_attr_end<OwnershipAttr>())
320    return true;
321
322
323  return false;
324}
325
326void MallocChecker::checkPostStmt(const CallExpr *CE, CheckerContext &C) const {
327  const FunctionDecl *FD = C.getCalleeDecl(CE);
328  if (!FD)
329    return;
330
331  initIdentifierInfo(C.getASTContext());
332  IdentifierInfo *FunI = FD->getIdentifier();
333  if (!FunI)
334    return;
335
336  ProgramStateRef State = C.getState();
337  if (FunI == II_malloc || FunI == II_valloc) {
338    State = MallocMemAux(C, CE, CE->getArg(0), UndefinedVal(), State);
339  } else if (FunI == II_realloc) {
340    State = ReallocMem(C, CE, false);
341  } else if (FunI == II_reallocf) {
342    State = ReallocMem(C, CE, true);
343  } else if (FunI == II_calloc) {
344    State = CallocMem(C, CE);
345  } else if (FunI == II_free) {
346    State = FreeMemAux(C, CE, C.getState(), 0, false);
347  } else if (FunI == II_strdup) {
348    State = MallocUpdateRefState(C, CE, State);
349  } else if (FunI == II_strndup) {
350    State = MallocUpdateRefState(C, CE, State);
351  } else if (Filter.CMallocOptimistic) {
352    // Check all the attributes, if there are any.
353    // There can be multiple of these attributes.
354    if (FD->hasAttrs())
355      for (specific_attr_iterator<OwnershipAttr>
356          i = FD->specific_attr_begin<OwnershipAttr>(),
357          e = FD->specific_attr_end<OwnershipAttr>();
358          i != e; ++i) {
359        switch ((*i)->getOwnKind()) {
360        case OwnershipAttr::Returns:
361          State = MallocMemReturnsAttr(C, CE, *i);
362          break;
363        case OwnershipAttr::Takes:
364        case OwnershipAttr::Holds:
365          State = FreeMemAttr(C, CE, *i);
366          break;
367        }
368      }
369  }
370  C.addTransition(State);
371}
372
373ProgramStateRef MallocChecker::MallocMemReturnsAttr(CheckerContext &C,
374                                                    const CallExpr *CE,
375                                                    const OwnershipAttr* Att) {
376  if (Att->getModule() != "malloc")
377    return 0;
378
379  OwnershipAttr::args_iterator I = Att->args_begin(), E = Att->args_end();
380  if (I != E) {
381    return MallocMemAux(C, CE, CE->getArg(*I), UndefinedVal(), C.getState());
382  }
383  return MallocMemAux(C, CE, UnknownVal(), UndefinedVal(), C.getState());
384}
385
386ProgramStateRef MallocChecker::MallocMemAux(CheckerContext &C,
387                                           const CallExpr *CE,
388                                           SVal Size, SVal Init,
389                                           ProgramStateRef state) {
390  // Get the return value.
391  SVal retVal = state->getSVal(CE, C.getLocationContext());
392
393  // We expect the malloc functions to return a pointer.
394  if (!isa<Loc>(retVal))
395    return 0;
396
397  // Fill the region with the initialization value.
398  state = state->bindDefault(retVal, Init);
399
400  // Set the region's extent equal to the Size parameter.
401  const SymbolicRegion *R =
402      dyn_cast_or_null<SymbolicRegion>(retVal.getAsRegion());
403  if (!R)
404    return 0;
405  if (isa<DefinedOrUnknownSVal>(Size)) {
406    SValBuilder &svalBuilder = C.getSValBuilder();
407    DefinedOrUnknownSVal Extent = R->getExtent(svalBuilder);
408    DefinedOrUnknownSVal DefinedSize = cast<DefinedOrUnknownSVal>(Size);
409    DefinedOrUnknownSVal extentMatchesSize =
410        svalBuilder.evalEQ(state, Extent, DefinedSize);
411
412    state = state->assume(extentMatchesSize, true);
413    assert(state);
414  }
415
416  return MallocUpdateRefState(C, CE, state);
417}
418
419ProgramStateRef MallocChecker::MallocUpdateRefState(CheckerContext &C,
420                                                    const CallExpr *CE,
421                                                    ProgramStateRef state) {
422  // Get the return value.
423  SVal retVal = state->getSVal(CE, C.getLocationContext());
424
425  // We expect the malloc functions to return a pointer.
426  if (!isa<Loc>(retVal))
427    return 0;
428
429  SymbolRef Sym = retVal.getAsLocSymbol();
430  assert(Sym);
431
432  // Set the symbol's state to Allocated.
433  return state->set<RegionState>(Sym, RefState::getAllocateUnchecked(CE));
434
435}
436
437ProgramStateRef MallocChecker::FreeMemAttr(CheckerContext &C,
438                                           const CallExpr *CE,
439                                           const OwnershipAttr* Att) const {
440  if (Att->getModule() != "malloc")
441    return 0;
442
443  ProgramStateRef State = C.getState();
444
445  for (OwnershipAttr::args_iterator I = Att->args_begin(), E = Att->args_end();
446       I != E; ++I) {
447    ProgramStateRef StateI = FreeMemAux(C, CE, State, *I,
448                               Att->getOwnKind() == OwnershipAttr::Holds);
449    if (StateI)
450      State = StateI;
451  }
452  return State;
453}
454
455ProgramStateRef MallocChecker::FreeMemAux(CheckerContext &C,
456                                          const CallExpr *CE,
457                                          ProgramStateRef state,
458                                          unsigned Num,
459                                          bool Hold) const {
460  const Expr *ArgExpr = CE->getArg(Num);
461  SVal ArgVal = state->getSVal(ArgExpr, C.getLocationContext());
462  if (!isa<DefinedOrUnknownSVal>(ArgVal))
463    return 0;
464  DefinedOrUnknownSVal location = cast<DefinedOrUnknownSVal>(ArgVal);
465
466  // Check for null dereferences.
467  if (!isa<Loc>(location))
468    return 0;
469
470  // The explicit NULL case, no operation is performed.
471  ProgramStateRef notNullState, nullState;
472  llvm::tie(notNullState, nullState) = state->assume(location);
473  if (nullState && !notNullState)
474    return 0;
475
476  // Unknown values could easily be okay
477  // Undefined values are handled elsewhere
478  if (ArgVal.isUnknownOrUndef())
479    return 0;
480
481  const MemRegion *R = ArgVal.getAsRegion();
482
483  // Nonlocs can't be freed, of course.
484  // Non-region locations (labels and fixed addresses) also shouldn't be freed.
485  if (!R) {
486    ReportBadFree(C, ArgVal, ArgExpr->getSourceRange());
487    return 0;
488  }
489
490  R = R->StripCasts();
491
492  // Blocks might show up as heap data, but should not be free()d
493  if (isa<BlockDataRegion>(R)) {
494    ReportBadFree(C, ArgVal, ArgExpr->getSourceRange());
495    return 0;
496  }
497
498  const MemSpaceRegion *MS = R->getMemorySpace();
499
500  // Parameters, locals, statics, and globals shouldn't be freed.
501  if (!(isa<UnknownSpaceRegion>(MS) || isa<HeapSpaceRegion>(MS))) {
502    // FIXME: at the time this code was written, malloc() regions were
503    // represented by conjured symbols, which are all in UnknownSpaceRegion.
504    // This means that there isn't actually anything from HeapSpaceRegion
505    // that should be freed, even though we allow it here.
506    // Of course, free() can work on memory allocated outside the current
507    // function, so UnknownSpaceRegion is always a possibility.
508    // False negatives are better than false positives.
509
510    ReportBadFree(C, ArgVal, ArgExpr->getSourceRange());
511    return 0;
512  }
513
514  const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(R);
515  // Various cases could lead to non-symbol values here.
516  // For now, ignore them.
517  if (!SR)
518    return 0;
519
520  SymbolRef Sym = SR->getSymbol();
521  const RefState *RS = state->get<RegionState>(Sym);
522
523  // If the symbol has not been tracked, return. This is possible when free() is
524  // called on a pointer that does not get its pointee directly from malloc().
525  // Full support of this requires inter-procedural analysis.
526  if (!RS)
527    return 0;
528
529  // Check double free.
530  if (RS->isReleased()) {
531    if (ExplodedNode *N = C.generateSink()) {
532      if (!BT_DoubleFree)
533        BT_DoubleFree.reset(
534          new BugType("Double free", "Memory Error"));
535      BugReport *R = new BugReport(*BT_DoubleFree,
536                        "Attempt to free released memory", N);
537      R->addRange(ArgExpr->getSourceRange());
538      R->addVisitor(new MallocBugVisitor(Sym));
539      C.EmitReport(R);
540    }
541    return 0;
542  }
543
544  // Normal free.
545  if (Hold)
546    return state->set<RegionState>(Sym, RefState::getRelinquished(CE));
547  return state->set<RegionState>(Sym, RefState::getReleased(CE));
548}
549
550bool MallocChecker::SummarizeValue(raw_ostream &os, SVal V) {
551  if (nonloc::ConcreteInt *IntVal = dyn_cast<nonloc::ConcreteInt>(&V))
552    os << "an integer (" << IntVal->getValue() << ")";
553  else if (loc::ConcreteInt *ConstAddr = dyn_cast<loc::ConcreteInt>(&V))
554    os << "a constant address (" << ConstAddr->getValue() << ")";
555  else if (loc::GotoLabel *Label = dyn_cast<loc::GotoLabel>(&V))
556    os << "the address of the label '" << Label->getLabel()->getName() << "'";
557  else
558    return false;
559
560  return true;
561}
562
563bool MallocChecker::SummarizeRegion(raw_ostream &os,
564                                    const MemRegion *MR) {
565  switch (MR->getKind()) {
566  case MemRegion::FunctionTextRegionKind: {
567    const FunctionDecl *FD = cast<FunctionTextRegion>(MR)->getDecl();
568    if (FD)
569      os << "the address of the function '" << *FD << '\'';
570    else
571      os << "the address of a function";
572    return true;
573  }
574  case MemRegion::BlockTextRegionKind:
575    os << "block text";
576    return true;
577  case MemRegion::BlockDataRegionKind:
578    // FIXME: where the block came from?
579    os << "a block";
580    return true;
581  default: {
582    const MemSpaceRegion *MS = MR->getMemorySpace();
583
584    if (isa<StackLocalsSpaceRegion>(MS)) {
585      const VarRegion *VR = dyn_cast<VarRegion>(MR);
586      const VarDecl *VD;
587      if (VR)
588        VD = VR->getDecl();
589      else
590        VD = NULL;
591
592      if (VD)
593        os << "the address of the local variable '" << VD->getName() << "'";
594      else
595        os << "the address of a local stack variable";
596      return true;
597    }
598
599    if (isa<StackArgumentsSpaceRegion>(MS)) {
600      const VarRegion *VR = dyn_cast<VarRegion>(MR);
601      const VarDecl *VD;
602      if (VR)
603        VD = VR->getDecl();
604      else
605        VD = NULL;
606
607      if (VD)
608        os << "the address of the parameter '" << VD->getName() << "'";
609      else
610        os << "the address of a parameter";
611      return true;
612    }
613
614    if (isa<GlobalsSpaceRegion>(MS)) {
615      const VarRegion *VR = dyn_cast<VarRegion>(MR);
616      const VarDecl *VD;
617      if (VR)
618        VD = VR->getDecl();
619      else
620        VD = NULL;
621
622      if (VD) {
623        if (VD->isStaticLocal())
624          os << "the address of the static variable '" << VD->getName() << "'";
625        else
626          os << "the address of the global variable '" << VD->getName() << "'";
627      } else
628        os << "the address of a global variable";
629      return true;
630    }
631
632    return false;
633  }
634  }
635}
636
637void MallocChecker::ReportBadFree(CheckerContext &C, SVal ArgVal,
638                                  SourceRange range) const {
639  if (ExplodedNode *N = C.generateSink()) {
640    if (!BT_BadFree)
641      BT_BadFree.reset(new BugType("Bad free", "Memory Error"));
642
643    SmallString<100> buf;
644    llvm::raw_svector_ostream os(buf);
645
646    const MemRegion *MR = ArgVal.getAsRegion();
647    if (MR) {
648      while (const ElementRegion *ER = dyn_cast<ElementRegion>(MR))
649        MR = ER->getSuperRegion();
650
651      // Special case for alloca()
652      if (isa<AllocaRegion>(MR))
653        os << "Argument to free() was allocated by alloca(), not malloc()";
654      else {
655        os << "Argument to free() is ";
656        if (SummarizeRegion(os, MR))
657          os << ", which is not memory allocated by malloc()";
658        else
659          os << "not memory allocated by malloc()";
660      }
661    } else {
662      os << "Argument to free() is ";
663      if (SummarizeValue(os, ArgVal))
664        os << ", which is not memory allocated by malloc()";
665      else
666        os << "not memory allocated by malloc()";
667    }
668
669    BugReport *R = new BugReport(*BT_BadFree, os.str(), N);
670    R->addRange(range);
671    C.EmitReport(R);
672  }
673}
674
675ProgramStateRef MallocChecker::ReallocMem(CheckerContext &C,
676                                          const CallExpr *CE,
677                                          bool FreesOnFail) const {
678  ProgramStateRef state = C.getState();
679  const Expr *arg0Expr = CE->getArg(0);
680  const LocationContext *LCtx = C.getLocationContext();
681  SVal Arg0Val = state->getSVal(arg0Expr, LCtx);
682  if (!isa<DefinedOrUnknownSVal>(Arg0Val))
683    return 0;
684  DefinedOrUnknownSVal arg0Val = cast<DefinedOrUnknownSVal>(Arg0Val);
685
686  SValBuilder &svalBuilder = C.getSValBuilder();
687
688  DefinedOrUnknownSVal PtrEQ =
689    svalBuilder.evalEQ(state, arg0Val, svalBuilder.makeNull());
690
691  // Get the size argument. If there is no size arg then give up.
692  const Expr *Arg1 = CE->getArg(1);
693  if (!Arg1)
694    return 0;
695
696  // Get the value of the size argument.
697  SVal Arg1ValG = state->getSVal(Arg1, LCtx);
698  if (!isa<DefinedOrUnknownSVal>(Arg1ValG))
699    return 0;
700  DefinedOrUnknownSVal Arg1Val = cast<DefinedOrUnknownSVal>(Arg1ValG);
701
702  // Compare the size argument to 0.
703  DefinedOrUnknownSVal SizeZero =
704    svalBuilder.evalEQ(state, Arg1Val,
705                       svalBuilder.makeIntValWithPtrWidth(0, false));
706
707  ProgramStateRef StatePtrIsNull, StatePtrNotNull;
708  llvm::tie(StatePtrIsNull, StatePtrNotNull) = state->assume(PtrEQ);
709  ProgramStateRef StateSizeIsZero, StateSizeNotZero;
710  llvm::tie(StateSizeIsZero, StateSizeNotZero) = state->assume(SizeZero);
711  // We only assume exceptional states if they are definitely true; if the
712  // state is under-constrained, assume regular realloc behavior.
713  bool PrtIsNull = StatePtrIsNull && !StatePtrNotNull;
714  bool SizeIsZero = StateSizeIsZero && !StateSizeNotZero;
715
716  // If the ptr is NULL and the size is not 0, the call is equivalent to
717  // malloc(size).
718  if ( PrtIsNull && !SizeIsZero) {
719    ProgramStateRef stateMalloc = MallocMemAux(C, CE, CE->getArg(1),
720                                               UndefinedVal(), StatePtrIsNull);
721    return stateMalloc;
722  }
723
724  if (PrtIsNull && SizeIsZero)
725    return 0;
726
727  // Get the from and to pointer symbols as in toPtr = realloc(fromPtr, size).
728  assert(!PrtIsNull);
729  SymbolRef FromPtr = arg0Val.getAsSymbol();
730  SVal RetVal = state->getSVal(CE, LCtx);
731  SymbolRef ToPtr = RetVal.getAsSymbol();
732  if (!FromPtr || !ToPtr)
733    return 0;
734
735  // If the size is 0, free the memory.
736  if (SizeIsZero)
737    if (ProgramStateRef stateFree = FreeMemAux(C, CE, StateSizeIsZero,0,false)){
738      // The semantics of the return value are:
739      // If size was equal to 0, either NULL or a pointer suitable to be passed
740      // to free() is returned.
741      stateFree = stateFree->set<ReallocPairs>(ToPtr,
742                                            ReallocPair(FromPtr, FreesOnFail));
743      C.getSymbolManager().addSymbolDependency(ToPtr, FromPtr);
744      return stateFree;
745    }
746
747  // Default behavior.
748  if (ProgramStateRef stateFree = FreeMemAux(C, CE, state, 0, false)) {
749    // FIXME: We should copy the content of the original buffer.
750    ProgramStateRef stateRealloc = MallocMemAux(C, CE, CE->getArg(1),
751                                                UnknownVal(), stateFree);
752    if (!stateRealloc)
753      return 0;
754    stateRealloc = stateRealloc->set<ReallocPairs>(ToPtr,
755                                            ReallocPair(FromPtr, FreesOnFail));
756    C.getSymbolManager().addSymbolDependency(ToPtr, FromPtr);
757    return stateRealloc;
758  }
759  return 0;
760}
761
762ProgramStateRef MallocChecker::CallocMem(CheckerContext &C, const CallExpr *CE){
763  ProgramStateRef state = C.getState();
764  SValBuilder &svalBuilder = C.getSValBuilder();
765  const LocationContext *LCtx = C.getLocationContext();
766  SVal count = state->getSVal(CE->getArg(0), LCtx);
767  SVal elementSize = state->getSVal(CE->getArg(1), LCtx);
768  SVal TotalSize = svalBuilder.evalBinOp(state, BO_Mul, count, elementSize,
769                                        svalBuilder.getContext().getSizeType());
770  SVal zeroVal = svalBuilder.makeZeroVal(svalBuilder.getContext().CharTy);
771
772  return MallocMemAux(C, CE, TotalSize, zeroVal, state);
773}
774
775const Stmt *
776MallocChecker::getAllocationSite(const ExplodedNode *N, SymbolRef Sym,
777                                 CheckerContext &C) const {
778  const LocationContext *LeakContext = N->getLocationContext();
779  // Walk the ExplodedGraph backwards and find the first node that referred to
780  // the tracked symbol.
781  const ExplodedNode *AllocNode = N;
782
783  while (N) {
784    if (!N->getState()->get<RegionState>(Sym))
785      break;
786    // Allocation node, is the last node in the current context in which the
787    // symbol was tracked.
788    if (N->getLocationContext() == LeakContext)
789      AllocNode = N;
790    N = N->pred_empty() ? NULL : *(N->pred_begin());
791  }
792
793  ProgramPoint P = AllocNode->getLocation();
794  if (!isa<StmtPoint>(P))
795    return 0;
796
797  return cast<StmtPoint>(P).getStmt();
798}
799
800void MallocChecker::reportLeak(SymbolRef Sym, ExplodedNode *N,
801                               CheckerContext &C) const {
802  assert(N);
803  if (!BT_Leak) {
804    BT_Leak.reset(new BugType("Memory leak", "Memory Error"));
805    // Leaks should not be reported if they are post-dominated by a sink:
806    // (1) Sinks are higher importance bugs.
807    // (2) NoReturnFunctionChecker uses sink nodes to represent paths ending
808    //     with __noreturn functions such as assert() or exit(). We choose not
809    //     to report leaks on such paths.
810    BT_Leak->setSuppressOnSink(true);
811  }
812
813  // Most bug reports are cached at the location where they occurred.
814  // With leaks, we want to unique them by the location where they were
815  // allocated, and only report a single path.
816  PathDiagnosticLocation LocUsedForUniqueing;
817  if (const Stmt *AllocStmt = getAllocationSite(N, Sym, C))
818    LocUsedForUniqueing = PathDiagnosticLocation::createBegin(AllocStmt,
819                            C.getSourceManager(), N->getLocationContext());
820
821  BugReport *R = new BugReport(*BT_Leak,
822    "Memory is never released; potential memory leak", N, LocUsedForUniqueing);
823  R->addVisitor(new MallocBugVisitor(Sym));
824  C.EmitReport(R);
825}
826
827void MallocChecker::checkDeadSymbols(SymbolReaper &SymReaper,
828                                     CheckerContext &C) const
829{
830  if (!SymReaper.hasDeadSymbols())
831    return;
832
833  ProgramStateRef state = C.getState();
834  RegionStateTy RS = state->get<RegionState>();
835  RegionStateTy::Factory &F = state->get_context<RegionState>();
836
837  bool generateReport = false;
838  llvm::SmallVector<SymbolRef, 2> Errors;
839  for (RegionStateTy::iterator I = RS.begin(), E = RS.end(); I != E; ++I) {
840    if (SymReaper.isDead(I->first)) {
841      if (I->second.isAllocated()) {
842        generateReport = true;
843        Errors.push_back(I->first);
844      }
845      // Remove the dead symbol from the map.
846      RS = F.remove(RS, I->first);
847
848    }
849  }
850
851  // Cleanup the Realloc Pairs Map.
852  ReallocMap RP = state->get<ReallocPairs>();
853  for (ReallocMap::iterator I = RP.begin(), E = RP.end(); I != E; ++I) {
854    if (SymReaper.isDead(I->first) ||
855        SymReaper.isDead(I->second.ReallocatedSym)) {
856      state = state->remove<ReallocPairs>(I->first);
857    }
858  }
859
860  // Generate leak node.
861  static SimpleProgramPointTag Tag("MallocChecker : DeadSymbolsLeak");
862  ExplodedNode *N = C.addTransition(C.getState(), C.getPredecessor(), &Tag);
863
864  if (generateReport) {
865    for (llvm::SmallVector<SymbolRef, 2>::iterator
866         I = Errors.begin(), E = Errors.end(); I != E; ++I) {
867      reportLeak(*I, N, C);
868    }
869  }
870  C.addTransition(state->set<RegionState>(RS), N);
871}
872
873void MallocChecker::checkEndPath(CheckerContext &C) const {
874  ProgramStateRef state = C.getState();
875  RegionStateTy M = state->get<RegionState>();
876
877  // If inside inlined call, skip it.
878  if (C.getLocationContext()->getParent() != 0)
879    return;
880
881  for (RegionStateTy::iterator I = M.begin(), E = M.end(); I != E; ++I) {
882    RefState RS = I->second;
883    if (RS.isAllocated()) {
884      ExplodedNode *N = C.addTransition(state);
885      if (N)
886        reportLeak(I->first, N, C);
887    }
888  }
889}
890
891bool MallocChecker::checkEscape(SymbolRef Sym, const Stmt *S,
892                                CheckerContext &C) const {
893  ProgramStateRef state = C.getState();
894  const RefState *RS = state->get<RegionState>(Sym);
895  if (!RS)
896    return false;
897
898  if (RS->isAllocated()) {
899    state = state->set<RegionState>(Sym, RefState::getEscaped(S));
900    C.addTransition(state);
901    return true;
902  }
903  return false;
904}
905
906void MallocChecker::checkPreStmt(const CallExpr *CE, CheckerContext &C) const {
907  if (isMemFunction(C.getCalleeDecl(CE), C.getASTContext()))
908    return;
909
910  // Check use after free, when a freed pointer is passed to a call.
911  ProgramStateRef State = C.getState();
912  for (CallExpr::const_arg_iterator I = CE->arg_begin(),
913                                    E = CE->arg_end(); I != E; ++I) {
914    const Expr *A = *I;
915    if (A->getType().getTypePtr()->isAnyPointerType()) {
916      SymbolRef Sym = State->getSVal(A, C.getLocationContext()).getAsSymbol();
917      if (!Sym)
918        continue;
919      if (checkUseAfterFree(Sym, C, A))
920        return;
921    }
922  }
923}
924
925void MallocChecker::checkPreStmt(const ReturnStmt *S, CheckerContext &C) const {
926  const Expr *E = S->getRetValue();
927  if (!E)
928    return;
929
930  // Check if we are returning a symbol.
931  SVal RetVal = C.getState()->getSVal(E, C.getLocationContext());
932  SymbolRef Sym = RetVal.getAsSymbol();
933  if (!Sym)
934    // If we are returning a field of the allocated struct or an array element,
935    // the callee could still free the memory.
936    // TODO: This logic should be a part of generic symbol escape callback.
937    if (const MemRegion *MR = RetVal.getAsRegion())
938      if (isa<FieldRegion>(MR) || isa<ElementRegion>(MR))
939        if (const SymbolicRegion *BMR =
940              dyn_cast<SymbolicRegion>(MR->getBaseRegion()))
941          Sym = BMR->getSymbol();
942  if (!Sym)
943    return;
944
945  // Check if we are returning freed memory.
946  if (checkUseAfterFree(Sym, C, E))
947    return;
948
949  // If this function body is not inlined, check if the symbol is escaping.
950  if (C.getLocationContext()->getParent() == 0)
951    checkEscape(Sym, E, C);
952}
953
954bool MallocChecker::checkUseAfterFree(SymbolRef Sym, CheckerContext &C,
955                                      const Stmt *S) const {
956  assert(Sym);
957  const RefState *RS = C.getState()->get<RegionState>(Sym);
958  if (RS && RS->isReleased()) {
959    if (ExplodedNode *N = C.generateSink()) {
960      if (!BT_UseFree)
961        BT_UseFree.reset(new BugType("Use-after-free", "Memory Error"));
962
963      BugReport *R = new BugReport(*BT_UseFree,
964                                   "Use of memory after it is freed",N);
965      if (S)
966        R->addRange(S->getSourceRange());
967      R->addVisitor(new MallocBugVisitor(Sym));
968      C.EmitReport(R);
969      return true;
970    }
971  }
972  return false;
973}
974
975// Check if the location is a freed symbolic region.
976void MallocChecker::checkLocation(SVal l, bool isLoad, const Stmt *S,
977                                  CheckerContext &C) const {
978  SymbolRef Sym = l.getLocSymbolInBase();
979  if (Sym)
980    checkUseAfterFree(Sym, C);
981}
982
983//===----------------------------------------------------------------------===//
984// Check various ways a symbol can be invalidated.
985// TODO: This logic (the next 3 functions) is copied/similar to the
986// RetainRelease checker. We might want to factor this out.
987//===----------------------------------------------------------------------===//
988
989// Stop tracking symbols when a value escapes as a result of checkBind.
990// A value escapes in three possible cases:
991// (1) we are binding to something that is not a memory region.
992// (2) we are binding to a memregion that does not have stack storage
993// (3) we are binding to a memregion with stack storage that the store
994//     does not understand.
995void MallocChecker::checkBind(SVal loc, SVal val, const Stmt *S,
996                              CheckerContext &C) const {
997  // Are we storing to something that causes the value to "escape"?
998  bool escapes = true;
999  ProgramStateRef state = C.getState();
1000
1001  if (loc::MemRegionVal *regionLoc = dyn_cast<loc::MemRegionVal>(&loc)) {
1002    escapes = !regionLoc->getRegion()->hasStackStorage();
1003
1004    if (!escapes) {
1005      // To test (3), generate a new state with the binding added.  If it is
1006      // the same state, then it escapes (since the store cannot represent
1007      // the binding).
1008      escapes = (state == (state->bindLoc(*regionLoc, val)));
1009    }
1010    if (!escapes) {
1011      // Case 4: We do not currently model what happens when a symbol is
1012      // assigned to a struct field, so be conservative here and let the symbol
1013      // go. TODO: This could definitely be improved upon.
1014      escapes = !isa<VarRegion>(regionLoc->getRegion());
1015    }
1016  }
1017
1018  // If our store can represent the binding and we aren't storing to something
1019  // that doesn't have local storage then just return and have the simulation
1020  // state continue as is.
1021  if (!escapes)
1022      return;
1023
1024  // Otherwise, find all symbols referenced by 'val' that we are tracking
1025  // and stop tracking them.
1026  state = state->scanReachableSymbols<StopTrackingCallback>(val).getState();
1027  C.addTransition(state);
1028}
1029
1030// If a symbolic region is assumed to NULL (or another constant), stop tracking
1031// it - assuming that allocation failed on this path.
1032ProgramStateRef MallocChecker::evalAssume(ProgramStateRef state,
1033                                              SVal Cond,
1034                                              bool Assumption) const {
1035  RegionStateTy RS = state->get<RegionState>();
1036  for (RegionStateTy::iterator I = RS.begin(), E = RS.end(); I != E; ++I) {
1037    // If the symbol is assumed to NULL or another constant, this will
1038    // return an APSInt*.
1039    if (state->getSymVal(I.getKey()))
1040      state = state->remove<RegionState>(I.getKey());
1041  }
1042
1043  // Realloc returns 0 when reallocation fails, which means that we should
1044  // restore the state of the pointer being reallocated.
1045  ReallocMap RP = state->get<ReallocPairs>();
1046  for (ReallocMap::iterator I = RP.begin(), E = RP.end(); I != E; ++I) {
1047    // If the symbol is assumed to NULL or another constant, this will
1048    // return an APSInt*.
1049    if (state->getSymVal(I.getKey())) {
1050      SymbolRef ReallocSym = I.getData().ReallocatedSym;
1051      const RefState *RS = state->get<RegionState>(ReallocSym);
1052      if (RS) {
1053        if (RS->isReleased() && ! I.getData().IsFreeOnFailure)
1054          state = state->set<RegionState>(ReallocSym,
1055                             RefState::getAllocateUnchecked(RS->getStmt()));
1056      }
1057      state = state->remove<ReallocPairs>(I.getKey());
1058    }
1059  }
1060
1061  return state;
1062}
1063
1064// Check if the function is known to us. So, for example, we could
1065// conservatively assume it can free/reallocate it's pointer arguments.
1066// (We assume that the pointers cannot escape through calls to system
1067// functions not handled by this checker.)
1068bool MallocChecker::doesNotFreeMemory(const CallOrObjCMessage *Call,
1069                                      ProgramStateRef State) const {
1070  if (!Call)
1071    return false;
1072
1073  // For now, assume that any C++ call can free memory.
1074  // TODO: If we want to be more optimistic here, we'll need to make sure that
1075  // regions escape to C++ containers. They seem to do that even now, but for
1076  // mysterious reasons.
1077  if (Call->isCXXCall())
1078    return false;
1079
1080  const Decl *D = Call->getDecl();
1081  if (!D)
1082    return false;
1083
1084  ASTContext &ASTC = State->getStateManager().getContext();
1085
1086  // If it's one of the allocation functions we can reason about, we model
1087  // its behavior explicitly.
1088  if (isa<FunctionDecl>(D) && isMemFunction(cast<FunctionDecl>(D), ASTC)) {
1089    return true;
1090  }
1091
1092  // If it's not a system call, assume it frees memory.
1093  SourceManager &SM = ASTC.getSourceManager();
1094  if (!SM.isInSystemHeader(D->getLocation()))
1095    return false;
1096
1097  // Process C/ObjC functions.
1098  if (const FunctionDecl *FD  = dyn_cast<FunctionDecl>(D)) {
1099    // White list the system functions whose arguments escape.
1100    const IdentifierInfo *II = FD->getIdentifier();
1101    if (!II)
1102      return true;
1103    StringRef FName = II->getName();
1104
1105    // White list thread local storage.
1106    if (FName.equals("pthread_setspecific"))
1107      return false;
1108
1109    // White list the 'XXXNoCopy' ObjC functions.
1110    if (FName.endswith("NoCopy")) {
1111      // Look for the deallocator argument. We know that the memory ownership
1112      // is not transfered only if the deallocator argument is
1113      // 'kCFAllocatorNull'.
1114      for (unsigned i = 1; i < Call->getNumArgs(); ++i) {
1115        const Expr *ArgE = Call->getArg(i)->IgnoreParenCasts();
1116        if (const DeclRefExpr *DE = dyn_cast<DeclRefExpr>(ArgE)) {
1117          StringRef DeallocatorName = DE->getFoundDecl()->getName();
1118          if (DeallocatorName == "kCFAllocatorNull")
1119            return true;
1120        }
1121      }
1122      return false;
1123    }
1124
1125    // PR12101
1126    // Many CoreFoundation and CoreGraphics might allow a tracked object
1127    // to escape.
1128    if (Call->isCFCGAllowingEscape(FName))
1129      return false;
1130
1131    // Associating streams with malloced buffers. The pointer can escape if
1132    // 'closefn' is specified (and if that function does free memory).
1133    // Currently, we do not inspect the 'closefn' function (PR12101).
1134    if (FName == "funopen")
1135      if (Call->getNumArgs() >= 4 && !Call->getArgSVal(4).isConstant(0))
1136        return false;
1137
1138    // Do not warn on pointers passed to 'setbuf' when used with std streams,
1139    // these leaks might be intentional when setting the buffer for stdio.
1140    // http://stackoverflow.com/questions/2671151/who-frees-setvbuf-buffer
1141    if (FName == "setbuf" || FName =="setbuffer" ||
1142        FName == "setlinebuf" || FName == "setvbuf") {
1143      if (Call->getNumArgs() >= 1)
1144        if (const DeclRefExpr *Arg =
1145              dyn_cast<DeclRefExpr>(Call->getArg(0)->IgnoreParenCasts()))
1146          if (const VarDecl *D = dyn_cast<VarDecl>(Arg->getDecl()))
1147              if (D->getCanonicalDecl()->getName().find("std")
1148                                                   != StringRef::npos)
1149                return false;
1150    }
1151
1152    // A bunch of other functions, which take ownership of a pointer (See retain
1153    // release checker). Not all the parameters here are invalidated, but the
1154    // Malloc checker cannot differentiate between them. The right way of doing
1155    // this would be to implement a pointer escapes callback.
1156    if (FName == "CVPixelBufferCreateWithBytes" ||
1157        FName == "CGBitmapContextCreateWithData" ||
1158        FName == "CVPixelBufferCreateWithPlanarBytes") {
1159      return false;
1160    }
1161
1162    // Otherwise, assume that the function does not free memory.
1163    // Most system calls, do not free the memory.
1164    return true;
1165
1166  // Process ObjC functions.
1167  } else if (const ObjCMethodDecl * ObjCD = dyn_cast<ObjCMethodDecl>(D)) {
1168    Selector S = ObjCD->getSelector();
1169
1170    // White list the ObjC functions which do free memory.
1171    // - Anything containing 'freeWhenDone' param set to 1.
1172    //   Ex: dataWithBytesNoCopy:length:freeWhenDone.
1173    for (unsigned i = 1; i < S.getNumArgs(); ++i) {
1174      if (S.getNameForSlot(i).equals("freeWhenDone")) {
1175        if (Call->getArgSVal(i).isConstant(1))
1176          return false;
1177        else
1178          return true;
1179      }
1180    }
1181
1182    // If the first selector ends with NoCopy, assume that the ownership is
1183    // transfered as well.
1184    // Ex:  [NSData dataWithBytesNoCopy:bytes length:10];
1185    if (S.getNameForSlot(0).endswith("NoCopy")) {
1186      return false;
1187    }
1188
1189    // Otherwise, assume that the function does not free memory.
1190    // Most system calls, do not free the memory.
1191    return true;
1192  }
1193
1194  // Otherwise, assume that the function can free memory.
1195  return false;
1196
1197}
1198
1199// If the symbol we are tracking is invalidated, but not explicitly (ex: the &p
1200// escapes, when we are tracking p), do not track the symbol as we cannot reason
1201// about it anymore.
1202ProgramStateRef
1203MallocChecker::checkRegionChanges(ProgramStateRef State,
1204                            const StoreManager::InvalidatedSymbols *invalidated,
1205                                    ArrayRef<const MemRegion *> ExplicitRegions,
1206                                    ArrayRef<const MemRegion *> Regions,
1207                                    const CallOrObjCMessage *Call) const {
1208  if (!invalidated || invalidated->empty())
1209    return State;
1210  llvm::SmallPtrSet<SymbolRef, 8> WhitelistedSymbols;
1211
1212  // If it's a call which might free or reallocate memory, we assume that all
1213  // regions (explicit and implicit) escaped.
1214
1215  // Otherwise, whitelist explicit pointers; we still can track them.
1216  if (!Call || doesNotFreeMemory(Call, State)) {
1217    for (ArrayRef<const MemRegion *>::iterator I = ExplicitRegions.begin(),
1218        E = ExplicitRegions.end(); I != E; ++I) {
1219      if (const SymbolicRegion *R = (*I)->StripCasts()->getAs<SymbolicRegion>())
1220        WhitelistedSymbols.insert(R->getSymbol());
1221    }
1222  }
1223
1224  for (StoreManager::InvalidatedSymbols::const_iterator I=invalidated->begin(),
1225       E = invalidated->end(); I!=E; ++I) {
1226    SymbolRef sym = *I;
1227    if (WhitelistedSymbols.count(sym))
1228      continue;
1229    // The symbol escaped.
1230    if (const RefState *RS = State->get<RegionState>(sym))
1231      State = State->set<RegionState>(sym, RefState::getEscaped(RS->getStmt()));
1232  }
1233  return State;
1234}
1235
1236PathDiagnosticPiece *
1237MallocChecker::MallocBugVisitor::VisitNode(const ExplodedNode *N,
1238                                           const ExplodedNode *PrevN,
1239                                           BugReporterContext &BRC,
1240                                           BugReport &BR) {
1241  const RefState *RS = N->getState()->get<RegionState>(Sym);
1242  const RefState *RSPrev = PrevN->getState()->get<RegionState>(Sym);
1243  if (!RS && !RSPrev)
1244    return 0;
1245
1246  const Stmt *S = 0;
1247  const char *Msg = 0;
1248
1249  // Retrieve the associated statement.
1250  ProgramPoint ProgLoc = N->getLocation();
1251  if (isa<StmtPoint>(ProgLoc))
1252    S = cast<StmtPoint>(ProgLoc).getStmt();
1253  // If an assumption was made on a branch, it should be caught
1254  // here by looking at the state transition.
1255  if (isa<BlockEdge>(ProgLoc)) {
1256    const CFGBlock *srcBlk = cast<BlockEdge>(ProgLoc).getSrc();
1257    S = srcBlk->getTerminator();
1258  }
1259  if (!S)
1260    return 0;
1261
1262  // Find out if this is an interesting point and what is the kind.
1263  if (Mode == Normal) {
1264    if (isAllocated(RS, RSPrev, S))
1265      Msg = "Memory is allocated";
1266    else if (isReleased(RS, RSPrev, S))
1267      Msg = "Memory is released";
1268    else if (isReallocFailedCheck(RS, RSPrev, S)) {
1269      Mode = ReallocationFailed;
1270      Msg = "Reallocation failed";
1271    }
1272
1273  // We are in a special mode if a reallocation failed later in the path.
1274  } else if (Mode == ReallocationFailed) {
1275    // Generate a special diagnostic for the first realloc we find.
1276    if (!isAllocated(RS, RSPrev, S) && !isReleased(RS, RSPrev, S))
1277      return 0;
1278
1279    // Check that the name of the function is realloc.
1280    const CallExpr *CE = dyn_cast<CallExpr>(S);
1281    if (!CE)
1282      return 0;
1283    const FunctionDecl *funDecl = CE->getDirectCallee();
1284    if (!funDecl)
1285      return 0;
1286    StringRef FunName = funDecl->getName();
1287    if (!(FunName.equals("realloc") || FunName.equals("reallocf")))
1288      return 0;
1289    Msg = "Attempt to reallocate memory";
1290    Mode = Normal;
1291  }
1292
1293  if (!Msg)
1294    return 0;
1295
1296  // Generate the extra diagnostic.
1297  PathDiagnosticLocation Pos(S, BRC.getSourceManager(),
1298                             N->getLocationContext());
1299  return new PathDiagnosticEventPiece(Pos, Msg);
1300}
1301
1302
1303#define REGISTER_CHECKER(name) \
1304void ento::register##name(CheckerManager &mgr) {\
1305  registerCStringCheckerBasic(mgr); \
1306  mgr.registerChecker<MallocChecker>()->Filter.C##name = true;\
1307}
1308
1309REGISTER_CHECKER(MallocPessimistic)
1310REGISTER_CHECKER(MallocOptimistic)
1311