xref: /llvm-project/clang/lib/StaticAnalyzer/Checkers/MallocChecker.cpp (revision d1ff1cbe231b58e8a9fd91a30e4568fbce7528aa)
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 "clang/StaticAnalyzer/Core/Checker.h"
17 #include "clang/StaticAnalyzer/Core/CheckerManager.h"
18 #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
19 #include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
20 #include "clang/StaticAnalyzer/Core/PathSensitive/ObjCMessage.h"
21 #include "clang/StaticAnalyzer/Core/PathSensitive/ProgramState.h"
22 #include "clang/StaticAnalyzer/Core/PathSensitive/ProgramStateTrait.h"
23 #include "clang/StaticAnalyzer/Core/PathSensitive/SymbolManager.h"
24 #include "clang/Basic/SourceManager.h"
25 #include "llvm/ADT/ImmutableMap.h"
26 #include "llvm/ADT/SmallString.h"
27 #include "llvm/ADT/STLExtras.h"
28 using namespace clang;
29 using namespace ento;
30 
31 namespace {
32 
33 class RefState {
34   enum Kind { AllocateUnchecked, AllocateFailed, Released, Escaped,
35               Relinquished } K;
36   const Stmt *S;
37 
38 public:
39   RefState(Kind k, const Stmt *s) : K(k), S(s) {}
40 
41   bool isAllocated() const { return K == AllocateUnchecked; }
42   //bool isFailed() const { return K == AllocateFailed; }
43   bool isReleased() const { return K == Released; }
44   //bool isEscaped() const { return K == Escaped; }
45   //bool isRelinquished() const { return K == Relinquished; }
46   const Stmt *getStmt() const { return S; }
47 
48   bool operator==(const RefState &X) const {
49     return K == X.K && S == X.S;
50   }
51 
52   static RefState getAllocateUnchecked(const Stmt *s) {
53     return RefState(AllocateUnchecked, s);
54   }
55   static RefState getAllocateFailed() {
56     return RefState(AllocateFailed, 0);
57   }
58   static RefState getReleased(const Stmt *s) { return RefState(Released, s); }
59   static RefState getEscaped(const Stmt *s) { return RefState(Escaped, s); }
60   static RefState getRelinquished(const Stmt *s) {
61     return RefState(Relinquished, s);
62   }
63 
64   void Profile(llvm::FoldingSetNodeID &ID) const {
65     ID.AddInteger(K);
66     ID.AddPointer(S);
67   }
68 };
69 
70 struct ReallocPair {
71   SymbolRef ReallocatedSym;
72   bool IsFreeOnFailure;
73   ReallocPair(SymbolRef S, bool F) : ReallocatedSym(S), IsFreeOnFailure(F) {}
74   void Profile(llvm::FoldingSetNodeID &ID) const {
75     ID.AddInteger(IsFreeOnFailure);
76     ID.AddPointer(ReallocatedSym);
77   }
78   bool operator==(const ReallocPair &X) const {
79     return ReallocatedSym == X.ReallocatedSym &&
80            IsFreeOnFailure == X.IsFreeOnFailure;
81   }
82 };
83 
84 class MallocChecker : public Checker<check::DeadSymbols,
85                                      check::EndPath,
86                                      check::PreStmt<ReturnStmt>,
87                                      check::PreStmt<CallExpr>,
88                                      check::PostStmt<CallExpr>,
89                                      check::Location,
90                                      check::Bind,
91                                      eval::Assume,
92                                      check::RegionChanges>
93 {
94   mutable OwningPtr<BuiltinBug> BT_DoubleFree;
95   mutable OwningPtr<BuiltinBug> BT_Leak;
96   mutable OwningPtr<BuiltinBug> BT_UseFree;
97   mutable OwningPtr<BuiltinBug> BT_UseRelinquished;
98   mutable OwningPtr<BuiltinBug> BT_BadFree;
99   mutable IdentifierInfo *II_malloc, *II_free, *II_realloc, *II_calloc,
100                          *II_valloc, *II_reallocf;
101 
102 public:
103   MallocChecker() : II_malloc(0), II_free(0), II_realloc(0), II_calloc(0),
104                     II_valloc(0), II_reallocf(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 void MallocMem(CheckerContext &C, const CallExpr *CE);
144   static void MallocMemReturnsAttr(CheckerContext &C, 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   static ProgramStateRef MallocMemAux(CheckerContext &C, const CallExpr *CE,
154                                      SVal SizeEx, SVal Init,
155                                      ProgramStateRef state);
156 
157   void FreeMem(CheckerContext &C, const CallExpr *CE) const;
158   void FreeMemAttr(CheckerContext &C, const CallExpr *CE,
159                    const OwnershipAttr* Att) const;
160   ProgramStateRef FreeMemAux(CheckerContext &C, const CallExpr *CE,
161                                  ProgramStateRef state, unsigned Num,
162                                  bool Hold) const;
163 
164   void ReallocMem(CheckerContext &C, const CallExpr *CE,
165                   bool FreesMemOnFailure) const;
166   static void CallocMem(CheckerContext &C, const CallExpr *CE);
167 
168   bool checkEscape(SymbolRef Sym, const Stmt *S, CheckerContext &C) const;
169   bool checkUseAfterFree(SymbolRef Sym, CheckerContext &C,
170                          const Stmt *S = 0) const;
171 
172   /// Check if the function is not known to us. So, for example, we could
173   /// conservatively assume it can free/reallocate it's pointer arguments.
174   bool hasUnknownBehavior(const FunctionDecl *FD, ProgramStateRef State) const;
175 
176   static bool SummarizeValue(raw_ostream &os, SVal V);
177   static bool SummarizeRegion(raw_ostream &os, const MemRegion *MR);
178   void ReportBadFree(CheckerContext &C, SVal ArgVal, SourceRange range) const;
179 
180   void reportLeak(SymbolRef Sym, ExplodedNode *N, CheckerContext &C) const;
181 
182   /// The bug visitor which allows us to print extra diagnostics along the
183   /// BugReport path. For example, showing the allocation site of the leaked
184   /// region.
185   class MallocBugVisitor : public BugReporterVisitor {
186   protected:
187     // The allocated region symbol tracked by the main analysis.
188     SymbolRef Sym;
189 
190   public:
191     MallocBugVisitor(SymbolRef S) : Sym(S) {}
192     virtual ~MallocBugVisitor() {}
193 
194     void Profile(llvm::FoldingSetNodeID &ID) const {
195       static int X = 0;
196       ID.AddPointer(&X);
197       ID.AddPointer(Sym);
198     }
199 
200     inline bool isAllocated(const RefState *S, const RefState *SPrev) {
201       // Did not track -> allocated. Other state (released) -> allocated.
202       return ((S && S->isAllocated()) && (!SPrev || !SPrev->isAllocated()));
203     }
204 
205     inline bool isReleased(const RefState *S, const RefState *SPrev) {
206       // Did not track -> released. Other state (allocated) -> released.
207       return ((S && S->isReleased()) && (!SPrev || !SPrev->isReleased()));
208     }
209 
210     PathDiagnosticPiece *VisitNode(const ExplodedNode *N,
211                                    const ExplodedNode *PrevN,
212                                    BugReporterContext &BRC,
213                                    BugReport &BR);
214   };
215 };
216 } // end anonymous namespace
217 
218 typedef llvm::ImmutableMap<SymbolRef, RefState> RegionStateTy;
219 typedef llvm::ImmutableMap<SymbolRef, ReallocPair > ReallocMap;
220 class RegionState {};
221 class ReallocPairs {};
222 namespace clang {
223 namespace ento {
224   template <>
225   struct ProgramStateTrait<RegionState>
226     : public ProgramStatePartialTrait<RegionStateTy> {
227     static void *GDMIndex() { static int x; return &x; }
228   };
229 
230   template <>
231   struct ProgramStateTrait<ReallocPairs>
232     : public ProgramStatePartialTrait<ReallocMap> {
233     static void *GDMIndex() { static int x; return &x; }
234   };
235 }
236 }
237 
238 namespace {
239 class StopTrackingCallback : public SymbolVisitor {
240   ProgramStateRef state;
241 public:
242   StopTrackingCallback(ProgramStateRef st) : state(st) {}
243   ProgramStateRef getState() const { return state; }
244 
245   bool VisitSymbol(SymbolRef sym) {
246     state = state->remove<RegionState>(sym);
247     return true;
248   }
249 };
250 } // end anonymous namespace
251 
252 void MallocChecker::initIdentifierInfo(ASTContext &Ctx) const {
253   if (!II_malloc)
254     II_malloc = &Ctx.Idents.get("malloc");
255   if (!II_free)
256     II_free = &Ctx.Idents.get("free");
257   if (!II_realloc)
258     II_realloc = &Ctx.Idents.get("realloc");
259   if (!II_reallocf)
260     II_reallocf = &Ctx.Idents.get("reallocf");
261   if (!II_calloc)
262     II_calloc = &Ctx.Idents.get("calloc");
263   if (!II_valloc)
264     II_valloc = &Ctx.Idents.get("valloc");
265 }
266 
267 bool MallocChecker::isMemFunction(const FunctionDecl *FD, ASTContext &C) const {
268   if (!FD)
269     return false;
270   IdentifierInfo *FunI = FD->getIdentifier();
271   if (!FunI)
272     return false;
273 
274   initIdentifierInfo(C);
275 
276   // TODO: Add more here : ex: reallocf!
277   if (FunI == II_malloc || FunI == II_free || FunI == II_realloc ||
278       FunI == II_reallocf || FunI == II_calloc || FunI == II_valloc)
279     return true;
280 
281   if (Filter.CMallocOptimistic && FD->hasAttrs() &&
282       FD->specific_attr_begin<OwnershipAttr>() !=
283           FD->specific_attr_end<OwnershipAttr>())
284     return true;
285 
286 
287   return false;
288 }
289 
290 void MallocChecker::checkPostStmt(const CallExpr *CE, CheckerContext &C) const {
291   const FunctionDecl *FD = C.getCalleeDecl(CE);
292   if (!FD)
293     return;
294 
295   initIdentifierInfo(C.getASTContext());
296   IdentifierInfo *FunI = FD->getIdentifier();
297   if (!FunI)
298     return;
299 
300   if (FunI == II_malloc || FunI == II_valloc) {
301     MallocMem(C, CE);
302     return;
303   } else if (FunI == II_realloc) {
304     ReallocMem(C, CE, false);
305     return;
306   } else if (FunI == II_reallocf) {
307     ReallocMem(C, CE, true);
308     return;
309   } else if (FunI == II_calloc) {
310     CallocMem(C, CE);
311     return;
312   }else if (FunI == II_free) {
313     FreeMem(C, CE);
314     return;
315   }
316 
317   if (Filter.CMallocOptimistic)
318   // Check all the attributes, if there are any.
319   // There can be multiple of these attributes.
320   if (FD->hasAttrs()) {
321     for (specific_attr_iterator<OwnershipAttr>
322                   i = FD->specific_attr_begin<OwnershipAttr>(),
323                   e = FD->specific_attr_end<OwnershipAttr>();
324          i != e; ++i) {
325       switch ((*i)->getOwnKind()) {
326       case OwnershipAttr::Returns: {
327         MallocMemReturnsAttr(C, CE, *i);
328         return;
329       }
330       case OwnershipAttr::Takes:
331       case OwnershipAttr::Holds: {
332         FreeMemAttr(C, CE, *i);
333         return;
334       }
335       }
336     }
337   }
338 }
339 
340 void MallocChecker::MallocMem(CheckerContext &C, const CallExpr *CE) {
341   ProgramStateRef state = MallocMemAux(C, CE, CE->getArg(0), UndefinedVal(),
342                                       C.getState());
343   C.addTransition(state);
344 }
345 
346 void MallocChecker::MallocMemReturnsAttr(CheckerContext &C, const CallExpr *CE,
347                                          const OwnershipAttr* Att) {
348   if (Att->getModule() != "malloc")
349     return;
350 
351   OwnershipAttr::args_iterator I = Att->args_begin(), E = Att->args_end();
352   if (I != E) {
353     ProgramStateRef state =
354         MallocMemAux(C, CE, CE->getArg(*I), UndefinedVal(), C.getState());
355     C.addTransition(state);
356     return;
357   }
358   ProgramStateRef state = MallocMemAux(C, CE, UnknownVal(), UndefinedVal(),
359                                         C.getState());
360   C.addTransition(state);
361 }
362 
363 ProgramStateRef MallocChecker::MallocMemAux(CheckerContext &C,
364                                            const CallExpr *CE,
365                                            SVal Size, SVal Init,
366                                            ProgramStateRef state) {
367   SValBuilder &svalBuilder = C.getSValBuilder();
368 
369   // Get the return value.
370   SVal retVal = state->getSVal(CE, C.getLocationContext());
371 
372   // We expect the malloc functions to return a pointer.
373   if (!isa<Loc>(retVal))
374     return 0;
375 
376   // Fill the region with the initialization value.
377   state = state->bindDefault(retVal, Init);
378 
379   // Set the region's extent equal to the Size parameter.
380   const SymbolicRegion *R =
381       dyn_cast_or_null<SymbolicRegion>(retVal.getAsRegion());
382   if (!R || !isa<DefinedOrUnknownSVal>(Size))
383     return 0;
384 
385   DefinedOrUnknownSVal Extent = R->getExtent(svalBuilder);
386   DefinedOrUnknownSVal DefinedSize = cast<DefinedOrUnknownSVal>(Size);
387   DefinedOrUnknownSVal extentMatchesSize =
388     svalBuilder.evalEQ(state, Extent, DefinedSize);
389 
390   state = state->assume(extentMatchesSize, true);
391   assert(state);
392 
393   SymbolRef Sym = retVal.getAsLocSymbol();
394   assert(Sym);
395 
396   // Set the symbol's state to Allocated.
397   return state->set<RegionState>(Sym, RefState::getAllocateUnchecked(CE));
398 }
399 
400 void MallocChecker::FreeMem(CheckerContext &C, const CallExpr *CE) const {
401   ProgramStateRef state = FreeMemAux(C, CE, C.getState(), 0, false);
402 
403   if (state)
404     C.addTransition(state);
405 }
406 
407 void MallocChecker::FreeMemAttr(CheckerContext &C, const CallExpr *CE,
408                                 const OwnershipAttr* Att) const {
409   if (Att->getModule() != "malloc")
410     return;
411 
412   for (OwnershipAttr::args_iterator I = Att->args_begin(), E = Att->args_end();
413        I != E; ++I) {
414     ProgramStateRef state =
415       FreeMemAux(C, CE, C.getState(), *I,
416                  Att->getOwnKind() == OwnershipAttr::Holds);
417     if (state)
418       C.addTransition(state);
419   }
420 }
421 
422 ProgramStateRef MallocChecker::FreeMemAux(CheckerContext &C,
423                                           const CallExpr *CE,
424                                           ProgramStateRef state,
425                                           unsigned Num,
426                                           bool Hold) const {
427   const Expr *ArgExpr = CE->getArg(Num);
428   SVal ArgVal = state->getSVal(ArgExpr, C.getLocationContext());
429   if (!isa<DefinedOrUnknownSVal>(ArgVal))
430     return 0;
431   DefinedOrUnknownSVal location = cast<DefinedOrUnknownSVal>(ArgVal);
432 
433   // Check for null dereferences.
434   if (!isa<Loc>(location))
435     return 0;
436 
437   // The explicit NULL case, no operation is performed.
438   ProgramStateRef notNullState, nullState;
439   llvm::tie(notNullState, nullState) = state->assume(location);
440   if (nullState && !notNullState)
441     return 0;
442 
443   // Unknown values could easily be okay
444   // Undefined values are handled elsewhere
445   if (ArgVal.isUnknownOrUndef())
446     return 0;
447 
448   const MemRegion *R = ArgVal.getAsRegion();
449 
450   // Nonlocs can't be freed, of course.
451   // Non-region locations (labels and fixed addresses) also shouldn't be freed.
452   if (!R) {
453     ReportBadFree(C, ArgVal, ArgExpr->getSourceRange());
454     return 0;
455   }
456 
457   R = R->StripCasts();
458 
459   // Blocks might show up as heap data, but should not be free()d
460   if (isa<BlockDataRegion>(R)) {
461     ReportBadFree(C, ArgVal, ArgExpr->getSourceRange());
462     return 0;
463   }
464 
465   const MemSpaceRegion *MS = R->getMemorySpace();
466 
467   // Parameters, locals, statics, and globals shouldn't be freed.
468   if (!(isa<UnknownSpaceRegion>(MS) || isa<HeapSpaceRegion>(MS))) {
469     // FIXME: at the time this code was written, malloc() regions were
470     // represented by conjured symbols, which are all in UnknownSpaceRegion.
471     // This means that there isn't actually anything from HeapSpaceRegion
472     // that should be freed, even though we allow it here.
473     // Of course, free() can work on memory allocated outside the current
474     // function, so UnknownSpaceRegion is always a possibility.
475     // False negatives are better than false positives.
476 
477     ReportBadFree(C, ArgVal, ArgExpr->getSourceRange());
478     return 0;
479   }
480 
481   const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(R);
482   // Various cases could lead to non-symbol values here.
483   // For now, ignore them.
484   if (!SR)
485     return 0;
486 
487   SymbolRef Sym = SR->getSymbol();
488   const RefState *RS = state->get<RegionState>(Sym);
489 
490   // If the symbol has not been tracked, return. This is possible when free() is
491   // called on a pointer that does not get its pointee directly from malloc().
492   // Full support of this requires inter-procedural analysis.
493   if (!RS)
494     return 0;
495 
496   // Check double free.
497   if (RS->isReleased()) {
498     if (ExplodedNode *N = C.generateSink()) {
499       if (!BT_DoubleFree)
500         BT_DoubleFree.reset(
501           new BuiltinBug("Double free",
502                          "Try to free a memory block that has been released"));
503       BugReport *R = new BugReport(*BT_DoubleFree,
504                                    BT_DoubleFree->getDescription(), N);
505       R->addVisitor(new MallocBugVisitor(Sym));
506       C.EmitReport(R);
507     }
508     return 0;
509   }
510 
511   // Normal free.
512   if (Hold)
513     return state->set<RegionState>(Sym, RefState::getRelinquished(CE));
514   return state->set<RegionState>(Sym, RefState::getReleased(CE));
515 }
516 
517 bool MallocChecker::SummarizeValue(raw_ostream &os, SVal V) {
518   if (nonloc::ConcreteInt *IntVal = dyn_cast<nonloc::ConcreteInt>(&V))
519     os << "an integer (" << IntVal->getValue() << ")";
520   else if (loc::ConcreteInt *ConstAddr = dyn_cast<loc::ConcreteInt>(&V))
521     os << "a constant address (" << ConstAddr->getValue() << ")";
522   else if (loc::GotoLabel *Label = dyn_cast<loc::GotoLabel>(&V))
523     os << "the address of the label '" << Label->getLabel()->getName() << "'";
524   else
525     return false;
526 
527   return true;
528 }
529 
530 bool MallocChecker::SummarizeRegion(raw_ostream &os,
531                                     const MemRegion *MR) {
532   switch (MR->getKind()) {
533   case MemRegion::FunctionTextRegionKind: {
534     const FunctionDecl *FD = cast<FunctionTextRegion>(MR)->getDecl();
535     if (FD)
536       os << "the address of the function '" << *FD << '\'';
537     else
538       os << "the address of a function";
539     return true;
540   }
541   case MemRegion::BlockTextRegionKind:
542     os << "block text";
543     return true;
544   case MemRegion::BlockDataRegionKind:
545     // FIXME: where the block came from?
546     os << "a block";
547     return true;
548   default: {
549     const MemSpaceRegion *MS = MR->getMemorySpace();
550 
551     if (isa<StackLocalsSpaceRegion>(MS)) {
552       const VarRegion *VR = dyn_cast<VarRegion>(MR);
553       const VarDecl *VD;
554       if (VR)
555         VD = VR->getDecl();
556       else
557         VD = NULL;
558 
559       if (VD)
560         os << "the address of the local variable '" << VD->getName() << "'";
561       else
562         os << "the address of a local stack variable";
563       return true;
564     }
565 
566     if (isa<StackArgumentsSpaceRegion>(MS)) {
567       const VarRegion *VR = dyn_cast<VarRegion>(MR);
568       const VarDecl *VD;
569       if (VR)
570         VD = VR->getDecl();
571       else
572         VD = NULL;
573 
574       if (VD)
575         os << "the address of the parameter '" << VD->getName() << "'";
576       else
577         os << "the address of a parameter";
578       return true;
579     }
580 
581     if (isa<GlobalsSpaceRegion>(MS)) {
582       const VarRegion *VR = dyn_cast<VarRegion>(MR);
583       const VarDecl *VD;
584       if (VR)
585         VD = VR->getDecl();
586       else
587         VD = NULL;
588 
589       if (VD) {
590         if (VD->isStaticLocal())
591           os << "the address of the static variable '" << VD->getName() << "'";
592         else
593           os << "the address of the global variable '" << VD->getName() << "'";
594       } else
595         os << "the address of a global variable";
596       return true;
597     }
598 
599     return false;
600   }
601   }
602 }
603 
604 void MallocChecker::ReportBadFree(CheckerContext &C, SVal ArgVal,
605                                   SourceRange range) const {
606   if (ExplodedNode *N = C.generateSink()) {
607     if (!BT_BadFree)
608       BT_BadFree.reset(new BuiltinBug("Bad free"));
609 
610     SmallString<100> buf;
611     llvm::raw_svector_ostream os(buf);
612 
613     const MemRegion *MR = ArgVal.getAsRegion();
614     if (MR) {
615       while (const ElementRegion *ER = dyn_cast<ElementRegion>(MR))
616         MR = ER->getSuperRegion();
617 
618       // Special case for alloca()
619       if (isa<AllocaRegion>(MR))
620         os << "Argument to free() was allocated by alloca(), not malloc()";
621       else {
622         os << "Argument to free() is ";
623         if (SummarizeRegion(os, MR))
624           os << ", which is not memory allocated by malloc()";
625         else
626           os << "not memory allocated by malloc()";
627       }
628     } else {
629       os << "Argument to free() is ";
630       if (SummarizeValue(os, ArgVal))
631         os << ", which is not memory allocated by malloc()";
632       else
633         os << "not memory allocated by malloc()";
634     }
635 
636     BugReport *R = new BugReport(*BT_BadFree, os.str(), N);
637     R->addRange(range);
638     C.EmitReport(R);
639   }
640 }
641 
642 void MallocChecker::ReallocMem(CheckerContext &C, const CallExpr *CE,
643                                bool FreesOnFail) const {
644   ProgramStateRef state = C.getState();
645   const Expr *arg0Expr = CE->getArg(0);
646   const LocationContext *LCtx = C.getLocationContext();
647   SVal Arg0Val = state->getSVal(arg0Expr, LCtx);
648   if (!isa<DefinedOrUnknownSVal>(Arg0Val))
649     return;
650   DefinedOrUnknownSVal arg0Val = cast<DefinedOrUnknownSVal>(Arg0Val);
651 
652   SValBuilder &svalBuilder = C.getSValBuilder();
653 
654   DefinedOrUnknownSVal PtrEQ =
655     svalBuilder.evalEQ(state, arg0Val, svalBuilder.makeNull());
656 
657   // Get the size argument. If there is no size arg then give up.
658   const Expr *Arg1 = CE->getArg(1);
659   if (!Arg1)
660     return;
661 
662   // Get the value of the size argument.
663   SVal Arg1ValG = state->getSVal(Arg1, LCtx);
664   if (!isa<DefinedOrUnknownSVal>(Arg1ValG))
665     return;
666   DefinedOrUnknownSVal Arg1Val = cast<DefinedOrUnknownSVal>(Arg1ValG);
667 
668   // Compare the size argument to 0.
669   DefinedOrUnknownSVal SizeZero =
670     svalBuilder.evalEQ(state, Arg1Val,
671                        svalBuilder.makeIntValWithPtrWidth(0, false));
672 
673   ProgramStateRef StatePtrIsNull, StatePtrNotNull;
674   llvm::tie(StatePtrIsNull, StatePtrNotNull) = state->assume(PtrEQ);
675   ProgramStateRef StateSizeIsZero, StateSizeNotZero;
676   llvm::tie(StateSizeIsZero, StateSizeNotZero) = state->assume(SizeZero);
677   // We only assume exceptional states if they are definitely true; if the
678   // state is under-constrained, assume regular realloc behavior.
679   bool PrtIsNull = StatePtrIsNull && !StatePtrNotNull;
680   bool SizeIsZero = StateSizeIsZero && !StateSizeNotZero;
681 
682   // If the ptr is NULL and the size is not 0, the call is equivalent to
683   // malloc(size).
684   if ( PrtIsNull && !SizeIsZero) {
685     ProgramStateRef stateMalloc = MallocMemAux(C, CE, CE->getArg(1),
686                                                UndefinedVal(), StatePtrIsNull);
687     C.addTransition(stateMalloc);
688     return;
689   }
690 
691   if (PrtIsNull && SizeIsZero)
692     return;
693 
694   // Get the from and to pointer symbols as in toPtr = realloc(fromPtr, size).
695   assert(!PrtIsNull);
696   SymbolRef FromPtr = arg0Val.getAsSymbol();
697   SVal RetVal = state->getSVal(CE, LCtx);
698   SymbolRef ToPtr = RetVal.getAsSymbol();
699   if (!FromPtr || !ToPtr)
700     return;
701 
702   // If the size is 0, free the memory.
703   if (SizeIsZero)
704     if (ProgramStateRef stateFree = FreeMemAux(C, CE, StateSizeIsZero,0,false)){
705       // The semantics of the return value are:
706       // If size was equal to 0, either NULL or a pointer suitable to be passed
707       // to free() is returned.
708       stateFree = stateFree->set<ReallocPairs>(ToPtr,
709                                             ReallocPair(FromPtr, FreesOnFail));
710       C.getSymbolManager().addSymbolDependency(ToPtr, FromPtr);
711       C.addTransition(stateFree);
712       return;
713     }
714 
715   // Default behavior.
716   if (ProgramStateRef stateFree = FreeMemAux(C, CE, state, 0, false)) {
717     // FIXME: We should copy the content of the original buffer.
718     ProgramStateRef stateRealloc = MallocMemAux(C, CE, CE->getArg(1),
719                                                 UnknownVal(), stateFree);
720     if (!stateRealloc)
721       return;
722     stateRealloc = stateRealloc->set<ReallocPairs>(ToPtr,
723                                             ReallocPair(FromPtr, FreesOnFail));
724     C.getSymbolManager().addSymbolDependency(ToPtr, FromPtr);
725     C.addTransition(stateRealloc);
726     return;
727   }
728 }
729 
730 void MallocChecker::CallocMem(CheckerContext &C, const CallExpr *CE) {
731   ProgramStateRef state = C.getState();
732   SValBuilder &svalBuilder = C.getSValBuilder();
733   const LocationContext *LCtx = C.getLocationContext();
734   SVal count = state->getSVal(CE->getArg(0), LCtx);
735   SVal elementSize = state->getSVal(CE->getArg(1), LCtx);
736   SVal TotalSize = svalBuilder.evalBinOp(state, BO_Mul, count, elementSize,
737                                         svalBuilder.getContext().getSizeType());
738   SVal zeroVal = svalBuilder.makeZeroVal(svalBuilder.getContext().CharTy);
739 
740   C.addTransition(MallocMemAux(C, CE, TotalSize, zeroVal, state));
741 }
742 
743 void MallocChecker::reportLeak(SymbolRef Sym, ExplodedNode *N,
744                                CheckerContext &C) const {
745   assert(N);
746   if (!BT_Leak) {
747     BT_Leak.reset(new BuiltinBug("Memory leak",
748         "Allocated memory never released. Potential memory leak."));
749     // Leaks should not be reported if they are post-dominated by a sink:
750     // (1) Sinks are higher importance bugs.
751     // (2) NoReturnFunctionChecker uses sink nodes to represent paths ending
752     //     with __noreturn functions such as assert() or exit(). We choose not
753     //     to report leaks on such paths.
754     BT_Leak->setSuppressOnSink(true);
755   }
756 
757   BugReport *R = new BugReport(*BT_Leak, BT_Leak->getDescription(), N);
758   R->addVisitor(new MallocBugVisitor(Sym));
759   C.EmitReport(R);
760 }
761 
762 void MallocChecker::checkDeadSymbols(SymbolReaper &SymReaper,
763                                      CheckerContext &C) const
764 {
765   if (!SymReaper.hasDeadSymbols())
766     return;
767 
768   ProgramStateRef state = C.getState();
769   RegionStateTy RS = state->get<RegionState>();
770   RegionStateTy::Factory &F = state->get_context<RegionState>();
771 
772   bool generateReport = false;
773   llvm::SmallVector<SymbolRef, 2> Errors;
774   for (RegionStateTy::iterator I = RS.begin(), E = RS.end(); I != E; ++I) {
775     if (SymReaper.isDead(I->first)) {
776       if (I->second.isAllocated()) {
777         generateReport = true;
778         Errors.push_back(I->first);
779       }
780       // Remove the dead symbol from the map.
781       RS = F.remove(RS, I->first);
782 
783     }
784   }
785 
786   // Cleanup the Realloc Pairs Map.
787   ReallocMap RP = state->get<ReallocPairs>();
788   for (ReallocMap::iterator I = RP.begin(), E = RP.end(); I != E; ++I) {
789     if (SymReaper.isDead(I->first) ||
790         SymReaper.isDead(I->second.ReallocatedSym)) {
791       state = state->remove<ReallocPairs>(I->first);
792     }
793   }
794 
795   ExplodedNode *N = C.addTransition(state->set<RegionState>(RS));
796 
797   if (N && generateReport) {
798     for (llvm::SmallVector<SymbolRef, 2>::iterator
799          I = Errors.begin(), E = Errors.end(); I != E; ++I) {
800       reportLeak(*I, N, C);
801     }
802   }
803 }
804 
805 void MallocChecker::checkEndPath(CheckerContext &C) const {
806   ProgramStateRef state = C.getState();
807   RegionStateTy M = state->get<RegionState>();
808 
809   for (RegionStateTy::iterator I = M.begin(), E = M.end(); I != E; ++I) {
810     RefState RS = I->second;
811     if (RS.isAllocated()) {
812       ExplodedNode *N = C.addTransition(state);
813       if (N)
814         reportLeak(I->first, N, C);
815     }
816   }
817 }
818 
819 bool MallocChecker::checkEscape(SymbolRef Sym, const Stmt *S,
820                                 CheckerContext &C) const {
821   ProgramStateRef state = C.getState();
822   const RefState *RS = state->get<RegionState>(Sym);
823   if (!RS)
824     return false;
825 
826   if (RS->isAllocated()) {
827     state = state->set<RegionState>(Sym, RefState::getEscaped(S));
828     C.addTransition(state);
829     return true;
830   }
831   return false;
832 }
833 
834 void MallocChecker::checkPreStmt(const CallExpr *CE, CheckerContext &C) const {
835   if (isMemFunction(C.getCalleeDecl(CE), C.getASTContext()))
836     return;
837 
838   // Check use after free, when a freed pointer is passed to a call.
839   ProgramStateRef State = C.getState();
840   for (CallExpr::const_arg_iterator I = CE->arg_begin(),
841                                     E = CE->arg_end(); I != E; ++I) {
842     const Expr *A = *I;
843     if (A->getType().getTypePtr()->isAnyPointerType()) {
844       SymbolRef Sym = State->getSVal(A, C.getLocationContext()).getAsSymbol();
845       if (!Sym)
846         continue;
847       if (checkUseAfterFree(Sym, C, A))
848         return;
849     }
850   }
851 }
852 
853 void MallocChecker::checkPreStmt(const ReturnStmt *S, CheckerContext &C) const {
854   const Expr *E = S->getRetValue();
855   if (!E)
856     return;
857 
858   // Check if we are returning a symbol.
859   SymbolRef Sym = C.getState()->getSVal(E, C.getLocationContext()).getAsSymbol();
860   if (!Sym)
861     return;
862 
863   // Check if we are returning freed memory.
864   if (checkUseAfterFree(Sym, C, S))
865     return;
866 
867   // Check if the symbol is escaping.
868   checkEscape(Sym, S, C);
869 }
870 
871 bool MallocChecker::checkUseAfterFree(SymbolRef Sym, CheckerContext &C,
872                                       const Stmt *S) const {
873   assert(Sym);
874   const RefState *RS = C.getState()->get<RegionState>(Sym);
875   if (RS && RS->isReleased()) {
876     if (ExplodedNode *N = C.generateSink()) {
877       if (!BT_UseFree)
878         BT_UseFree.reset(new BuiltinBug("Use of dynamically allocated memory "
879             "after it is freed."));
880 
881       BugReport *R = new BugReport(*BT_UseFree, BT_UseFree->getDescription(),N);
882       if (S)
883         R->addRange(S->getSourceRange());
884       R->addVisitor(new MallocBugVisitor(Sym));
885       C.EmitReport(R);
886       return true;
887     }
888   }
889   return false;
890 }
891 
892 // Check if the location is a freed symbolic region.
893 void MallocChecker::checkLocation(SVal l, bool isLoad, const Stmt *S,
894                                   CheckerContext &C) const {
895   SymbolRef Sym = l.getLocSymbolInBase();
896   if (Sym)
897     checkUseAfterFree(Sym, C);
898 }
899 
900 //===----------------------------------------------------------------------===//
901 // Check various ways a symbol can be invalidated.
902 // TODO: This logic (the next 3 functions) is copied/similar to the
903 // RetainRelease checker. We might want to factor this out.
904 //===----------------------------------------------------------------------===//
905 
906 // Stop tracking symbols when a value escapes as a result of checkBind.
907 // A value escapes in three possible cases:
908 // (1) we are binding to something that is not a memory region.
909 // (2) we are binding to a memregion that does not have stack storage
910 // (3) we are binding to a memregion with stack storage that the store
911 //     does not understand.
912 void MallocChecker::checkBind(SVal loc, SVal val, const Stmt *S,
913                               CheckerContext &C) const {
914   // Are we storing to something that causes the value to "escape"?
915   bool escapes = true;
916   ProgramStateRef state = C.getState();
917 
918   if (loc::MemRegionVal *regionLoc = dyn_cast<loc::MemRegionVal>(&loc)) {
919     escapes = !regionLoc->getRegion()->hasStackStorage();
920 
921     if (!escapes) {
922       // To test (3), generate a new state with the binding added.  If it is
923       // the same state, then it escapes (since the store cannot represent
924       // the binding).
925       escapes = (state == (state->bindLoc(*regionLoc, val)));
926     }
927   }
928 
929   // If our store can represent the binding and we aren't storing to something
930   // that doesn't have local storage then just return and have the simulation
931   // state continue as is.
932   if (!escapes)
933       return;
934 
935   // Otherwise, find all symbols referenced by 'val' that we are tracking
936   // and stop tracking them.
937   state = state->scanReachableSymbols<StopTrackingCallback>(val).getState();
938   C.addTransition(state);
939 }
940 
941 // If a symbolic region is assumed to NULL (or another constant), stop tracking
942 // it - assuming that allocation failed on this path.
943 ProgramStateRef MallocChecker::evalAssume(ProgramStateRef state,
944                                               SVal Cond,
945                                               bool Assumption) const {
946   RegionStateTy RS = state->get<RegionState>();
947   for (RegionStateTy::iterator I = RS.begin(), E = RS.end(); I != E; ++I) {
948     // If the symbol is assumed to NULL or another constant, this will
949     // return an APSInt*.
950     if (state->getSymVal(I.getKey()))
951       state = state->remove<RegionState>(I.getKey());
952   }
953 
954   // Realloc returns 0 when reallocation fails, which means that we should
955   // restore the state of the pointer being reallocated.
956   ReallocMap RP = state->get<ReallocPairs>();
957   for (ReallocMap::iterator I = RP.begin(), E = RP.end(); I != E; ++I) {
958     // If the symbol is assumed to NULL or another constant, this will
959     // return an APSInt*.
960     if (state->getSymVal(I.getKey())) {
961       SymbolRef ReallocSym = I.getData().ReallocatedSym;
962       const RefState *RS = state->get<RegionState>(ReallocSym);
963       if (RS) {
964         if (RS->isReleased() && ! I.getData().IsFreeOnFailure)
965           state = state->set<RegionState>(ReallocSym,
966                              RefState::getAllocateUnchecked(RS->getStmt()));
967       }
968       state = state->remove<ReallocPairs>(I.getKey());
969     }
970   }
971 
972   return state;
973 }
974 
975 // Check if the function is not known to us. So, for example, we could
976 // conservatively assume it can free/reallocate it's pointer arguments.
977 // (We assume that the pointers cannot escape through calls to system
978 // functions not handled by this checker.)
979 bool MallocChecker::hasUnknownBehavior(const FunctionDecl *FD,
980                                        ProgramStateRef State) const {
981   ASTContext &ASTC = State->getStateManager().getContext();
982 
983   // If it's one of the allocation functions we can reason about, we model it's
984   // behavior explicitly.
985   if (isMemFunction(FD, ASTC)) {
986     return false;
987   }
988 
989   // If it's a system call, we know it does not free the memory.
990   SourceManager &SM = ASTC.getSourceManager();
991   if (SM.isInSystemHeader(FD->getLocation())) {
992     return false;
993   }
994 
995   // Otherwise, assume that the function can free memory.
996   return true;
997 }
998 
999 // If the symbol we are tracking is invalidated, but not explicitly (ex: the &p
1000 // escapes, when we are tracking p), do not track the symbol as we cannot reason
1001 // about it anymore.
1002 ProgramStateRef
1003 MallocChecker::checkRegionChanges(ProgramStateRef State,
1004                             const StoreManager::InvalidatedSymbols *invalidated,
1005                                     ArrayRef<const MemRegion *> ExplicitRegions,
1006                                     ArrayRef<const MemRegion *> Regions,
1007                                     const CallOrObjCMessage *Call) const {
1008   if (!invalidated)
1009     return State;
1010   llvm::SmallPtrSet<SymbolRef, 8> WhitelistedSymbols;
1011 
1012   const FunctionDecl *FD = (Call ?
1013                             dyn_cast_or_null<FunctionDecl>(Call->getDecl()) :0);
1014 
1015   // If it's a call which might free or reallocate memory, we assume that all
1016   // regions (explicit and implicit) escaped. Otherwise, whitelist explicit
1017   // pointers; we still can track them.
1018   if (!(FD && hasUnknownBehavior(FD, State))) {
1019     for (ArrayRef<const MemRegion *>::iterator I = ExplicitRegions.begin(),
1020         E = ExplicitRegions.end(); I != E; ++I) {
1021       if (const SymbolicRegion *R = (*I)->StripCasts()->getAs<SymbolicRegion>())
1022         WhitelistedSymbols.insert(R->getSymbol());
1023     }
1024   }
1025 
1026   for (StoreManager::InvalidatedSymbols::const_iterator I=invalidated->begin(),
1027        E = invalidated->end(); I!=E; ++I) {
1028     SymbolRef sym = *I;
1029     if (WhitelistedSymbols.count(sym))
1030       continue;
1031     // The symbol escaped.
1032     if (const RefState *RS = State->get<RegionState>(sym))
1033       State = State->set<RegionState>(sym, RefState::getEscaped(RS->getStmt()));
1034   }
1035   return State;
1036 }
1037 
1038 PathDiagnosticPiece *
1039 MallocChecker::MallocBugVisitor::VisitNode(const ExplodedNode *N,
1040                                            const ExplodedNode *PrevN,
1041                                            BugReporterContext &BRC,
1042                                            BugReport &BR) {
1043   const RefState *RS = N->getState()->get<RegionState>(Sym);
1044   const RefState *RSPrev = PrevN->getState()->get<RegionState>(Sym);
1045   if (!RS && !RSPrev)
1046     return 0;
1047 
1048   // We expect the interesting locations be StmtPoints corresponding to call
1049   // expressions. We do not support indirect function calls as of now.
1050   const CallExpr *CE = 0;
1051   if (isa<StmtPoint>(N->getLocation()))
1052     CE = dyn_cast<CallExpr>(cast<StmtPoint>(N->getLocation()).getStmt());
1053   if (!CE)
1054     return 0;
1055   const FunctionDecl *funDecl = CE->getDirectCallee();
1056   if (!funDecl)
1057     return 0;
1058 
1059   // Find out if this is an interesting point and what is the kind.
1060   const char *Msg = 0;
1061   if (isAllocated(RS, RSPrev))
1062     Msg = "Memory is allocated here";
1063   else if (isReleased(RS, RSPrev))
1064     Msg = "Memory is released here";
1065   if (!Msg)
1066     return 0;
1067 
1068   // Generate the extra diagnostic.
1069   PathDiagnosticLocation Pos(CE, BRC.getSourceManager(),
1070                              N->getLocationContext());
1071   return new PathDiagnosticEventPiece(Pos, Msg);
1072 }
1073 
1074 
1075 #define REGISTER_CHECKER(name) \
1076 void ento::register##name(CheckerManager &mgr) {\
1077   mgr.registerChecker<MallocChecker>()->Filter.C##name = true;\
1078 }
1079 
1080 REGISTER_CHECKER(MallocPessimistic)
1081 REGISTER_CHECKER(MallocOptimistic)
1082