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