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