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